_html: Let's see if Python does a better job

This commit is contained in:
Stig-Ørjan Smelror 2024-02-01 13:19:21 +01:00
parent 66ca7ee995
commit 42bfda8d0a

119
qsgen2
View File

@ -796,60 +796,93 @@ function _cleanup() {
echo "${cleaned_content}" echo "${cleaned_content}"
} }
function _html() { _html() {
local content="${1}" local content="${1}"
local debug=false local debug=false
if ${debug}; then _msg debug "_html: Converting QStags in content"; fi if ${debug}; then _msg debug "_html: Converting QStags in content"; fi
# Convert QStags to HTML using Perl, strictly matching the QStags list # Define the Python code for parsing and replacing specific QStags
local html_content=$(echo "${content}" | perl -pe ' local python_code=$(cat <<EOF
BEGIN { import re
# Define a hash of QStags to their HTML equivalents
%qstags = ( # Define a function to replace specific QStags
"#BR" => "<br/>\\n", def replace_qstags(match):
"#BD" => "<b>", "#EBD" => "</b>\\n", qstag = match.group(0)
"#I" => "<i>", "#EI" => "</i>\\n", qstag_dict = {
"#P" => "<p>", "#EP" => "</p>\\n", "#H1": "<h1>",
"#Q" => "<blockquote>", "#EQ" => "</blockquote>\\n", "#EH1": "</h1>",
"#C" => "<code>", "#EC" => "</code>\\n", "#H2": "<h2>",
"#H1" => "<h1>", "#EH1" => "</h1>\\n", "#EH2": "</h2>",
"#H2" => "<h2>", "#EH2" => "</h2>\\n", "#P": "<p>",
"#H3" => "<h3>", "#EH3" => "</h3>\\n", "#EP": "</p>",
"#H4" => "<h4>", "#EH4" => "</h4>\\n", "#BR": "<br/>",
"#H5" => "<h5>", "#EH5" => "</h5>\\n", "#BD": "<b>",
"#H6" => "<h6>", "#EH6" => "</h6>\\n", "#EBD": "</b>",
"#STRONG" => "<strong>", "#ESTRONG" => "</strong>\\n", "#I": "<i>",
"#EM" => "<em>", "#EEM" => "</em>\\n", "#EI": "</i>",
"#DV" => "<div>", "#EDV" => "</div>\\n", "#STRONG": "<strong>",
"#SPN" => "<span>", "#ESPN" => "</span>\\n", "#ESTRONG": "</strong>",
"#UL" => "<ul>", "#EUL" => "</ul>\\n", "#EM": "<em>",
"#OL" => "<ol>", "#EOL" => "</ol>\\n", "#EM": "</em>",
"#LI" => "<li>", "#ELI" => "</li>\\n", "#DIV": "<div>",
"#U" => "<u>", "#EU" => "</u>\\n", "#EDIV": "</div>",
"#TBL" => "<table>", "#ETBL" => "</table>\\n", "#SPAN": "<span>",
"#TR" => "<tr>", "#ETR" => "</tr>\\n", "#ESPAN": "</span>",
"#TD" => "<td>", "#ETD" => "</td>\\n", "#UL": "<ul>",
"#TH" => "<th>", "#ETH" => "</th>\\n", "#EUL": "</ul>",
"#ART" => "<article>", "#EART" => "</article>\\n", "#OL": "<ol>",
"#SEC" => "<section>", "#ESEC" => "</section>\\n", "#EOL": "</ol>",
"#ASIDE" => "<aside>", "#EASIDE" => "</aside>\\n", "#LI": "<li>",
"#NAV" => "<nav>", "#ENAV" => "</nav>\\n", "#ELI": "</li>",
"#BTN" => "<button>", "#EBTN" => "</button>\\n", "#U": "<u>",
"#SEL" => "<select>", "#ESEL" => "</select>\\n", "#EU": "</u>",
"#OPT" => "<option>", "#EOPT" => "</option>\\n", "#TABLE": "<table>",
"#LT" => "&lt;", "#GT" => "&gt;", "#NUM" => "&num;" "#ETABLE": "</table>",
); "#TR": "<tr>",
"#ETR": "</tr>",
"#TD": "<td>",
"#ETD": "</td>",
"#TH": "<th>",
"#ETH": "</th>",
"#ART": "<article>",
"#EART": "</article>",
"#SEC": "<section>",
"#ESEC": "</section>",
"#ASIDE": "<aside>",
"#EASIDE": "</aside>",
"#NAV": "<nav>",
"#ENAV": "</nav>",
"#BTN": "<button>",
"#EBTN": "</button>",
"#SEL": "<select>",
"#ESEL": "</select>",
"#OPT": "<option>",
"#EOPT": "</option>",
"#LT": "&lt;",
"#GT": "&gt;",
"#NUM": "&num;",
} }
while (my ($key, $value) = each %qstags) { return qstag_dict.get(qstag, qstag) # Replace or return unchanged
s/(?<!\S)\Q$key\E\b/$value/g;
} # Define the regular expression pattern for QStags
'); pattern = r'#\w+'
# Replace QStags using the defined function
result = re.sub(pattern, replace_qstags, '$content')
print(result)
EOF
)
# Execute the Python code and capture the output
local html_content=$(python -c "${python_code}")
echo "${html_content}" echo "${html_content}"
} }
# Time to test the first function # Time to test the first function
#_msg std "Running function blogs" #_msg std "Running function blogs"
_blogs _blogs