_html: Refine python script

This commit is contained in:
Stig-Ørjan Smelror 2024-02-01 15:15:36 +01:00
parent 8f87e41867
commit 48466907fd

6
qsgen2
View File

@ -805,10 +805,10 @@ _html() {
# Define the Python code for parsing and replacing specific QStags # Define the Python code for parsing and replacing specific QStags
local python_code=$(cat <<EOF local python_code=$(cat <<EOF
import re import re
# Define a function to replace QStags
# Define a function to replace specific QStags
def replace_qstags(match): def replace_qstags(match):
qstag = match.group(0) qstag = match.group(0)
return qstag_dict.get(qstag, qstag) # Replace using the table or return unchanged
qstag_dict = { qstag_dict = {
"#H1": "<h1>", "#H1": "<h1>",
"#EH1": "</h1>", "#EH1": "</h1>",
@ -875,10 +875,10 @@ def replace_qstags(match):
"#EH5": "</h5>", "#EH5": "</h5>",
"#EH6": "</h6>", "#EH6": "</h6>",
} }
return qstag_dict.get(qstag, qstag) # Replace or return unchanged
# Define the regular expression pattern for QStags, excluding specific ones # Define the regular expression pattern for QStags, excluding specific ones
pattern = r'(?<=\s)(#(?!link|image|ytvideo|updated|version)\w+)' pattern = r'(?<=\s)(#(?!link|image|ytvideo|updated|version)\w+)'
# Replace QStags using the defined function # Replace QStags using the defined function
result = re.sub(pattern, replace_qstags, '''$content''', flags=re.MULTILINE) result = re.sub(pattern, replace_qstags, '''$content''', flags=re.MULTILINE)