_html: Let's try Perl again

This commit is contained in:
Stig-Ørjan Smelror 2024-02-01 15:53:48 +01:00
parent 48466907fd
commit cc5d770939

137
qsgen2
View File

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