61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
import csv
|
|
import re
|
|
|
|
csv_path = '/home/x79/sisvietnamvn_01/Design/List of all WP action hooks and filters -- Adam Brown, BYU Political Science.csv'
|
|
java_path = '/home/x79/sisvietnamvn_01/sisvietnamvn_main/src/main/java/com/sisvietnamvn/web/hook/WpHooks.java'
|
|
|
|
def sanitize_constant_name(hook_name):
|
|
# Convert {$plugin} to PLUGIN
|
|
name = re.sub(r'\{\$([a-zA-Z0-9_]+)\}', r'\1', hook_name)
|
|
name = re.sub(r'[^a-zA-Z0-9_]', '_', name).upper()
|
|
|
|
# Handle multiple underscores or leading numbers
|
|
name = re.sub(r'_+', '_', name)
|
|
name = name.strip('_')
|
|
|
|
if name and name[0].isdigit():
|
|
name = "HOOK_" + name
|
|
|
|
return name
|
|
|
|
with open(csv_path, 'r', encoding='utf-8-sig') as f:
|
|
reader = csv.reader(f)
|
|
next(reader) # Skip header
|
|
|
|
hooks = set()
|
|
for row in reader:
|
|
if len(row) > 1 and row[1].strip():
|
|
hooks.add(row[1].strip())
|
|
|
|
with open(java_path, 'w', encoding='utf-8') as f:
|
|
f.write("package com.sisvietnamvn.web.hook;\n\n")
|
|
f.write("/**\n")
|
|
f.write(" * Automatically generated dictionary of all WordPress Hooks.\n")
|
|
f.write(" * Contains " + str(len(hooks)) + " constants.\n")
|
|
f.write(" */\n")
|
|
f.write("public final class WpHooks {\n\n")
|
|
f.write(" private WpHooks() {}\n\n")
|
|
|
|
# Track written constants to avoid duplicates
|
|
written = set()
|
|
|
|
for hook in sorted(list(hooks)):
|
|
const_name = sanitize_constant_name(hook)
|
|
if not const_name:
|
|
continue
|
|
|
|
if const_name in written:
|
|
const_name = const_name + "_2" # Simple deduplication
|
|
|
|
written.add(const_name)
|
|
|
|
# Add comment with original hook name
|
|
f.write(f' /**\n')
|
|
f.write(f' * Original hook name: {hook}\n')
|
|
f.write(f' */\n')
|
|
f.write(f' public static final String {const_name} = "{hook}";\n\n')
|
|
|
|
f.write("}\n")
|
|
print(f"Generated {len(written)} hook constants in WpHooks.java")
|
|
|