_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

121
qsgen2
View File

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