_html: Rewrite the function to use Perl instead of sed.

This commit is contained in:
Stig-Ørjan Smelror 2024-02-01 10:58:18 +01:00
parent abf4821fe2
commit 08d20b0550

121
qsgen2
View File

@ -798,78 +798,71 @@ function _cleanup() {
function _html() { function _html() {
# This function converts formatting tags into their HTML equivalents in a provided string
# using QStags (QS-tags)
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
# Perform HTML tag substitutions handling both lowercase and uppercase QStags # Convert QStags to HTML using Perl
local html_content=$( echo "${content}" | sed \ local html_content=$(perl -pe '
-e "s|#[bB][rR]|<br/>\n|g" \ next if /#link|#image|#ytvideo/;
-e "s|#[bB]|<b>|g" \ s/(?<!\S)#BR\b/<br\/>\n/gi;
-e "s|#[eE][bB]|</b>\n|g" \ s/(?<!\S)#B\b/<b>/gi;
-e "s|#[iI]|<i>|g" \ s/(?<!\S)#EB\b/<\/b>\n/gi;
-e "s|#[eE][iI]|</i>|g" \ s/(?<!\S)#I\b/<i>/gi;
-e "s|#[pP]|<p>|g" \ s/(?<!\S)#EI\b/<\/i>/gi;
-e "s|#[eE][pP]|</p>\n|g" \ s/(?<!\S)#P\b/<p>/gi;
-e "s|#[hH]1|<h1>|g" \ s/(?<!\S)#EP\b/<\/p>\n/gi;
-e "s|#[hH]2|<h2>|g" \ s/(?<!\S)#H([1-6])\b/<h\1>/gi;
-e "s|#[hH]3|<h3>|g" \ s/(?<!\S)#EH([1-6])\b/<\/h\1>\n/gi;
-e "s|#[hH]4|<h4>|g" \ s/(?<!\S)#STRONG\b/<strong>/gi;
-e "s|#[hH]5|<h5>|g" \ s/(?<!\S)#ESTRONG\b/<\/strong>/gi;
-e "s|#[hH]6|<h6>|g" \ s/(?<!\S)#EM\b/<em>/gi;
-e "s|#[eE][hH]1|</h1>\n|g" \ s/(?<!\S)#MEM\b/<\/em>/gi;
-e "s|#[eE][hH]2|</h2>\n|g" \ s/(?<!\S)#DV\b/<div>/gi;
-e "s|#[eE][hH]3|</h3>\n|g" \ s/(?<!\S)#EDV\b/<\/div>\n/gi;
-e "s|#[eE][hH]4|</h4>\n|g" \ s/(?<!\S)#SPN\b/<span>/gi;
-e "s|#[eE][hH]5|</h5>\n|g" \ s/(?<!\S)#ESPN\b/<\/span>\n/gi;
-e "s|#[eE][hH]6|</h6>\n|g" \ s/(?<!\S)#UL\b/\n<ul>/gi;
-e "s|#[sS][tT][rR][oO][nN][gG]|<strong>|g" \ s/(?<!\S)#EUL\b/\n<\/ul>\n/gi;
-e "s|#[eE][sS][tT][rR][oO][nN][gG]|</strong>|g" \ s(?<!\S)#OL\b/\n<ol>/gi;
-e "s|#[eE][mM]|<em>|g" \ s/(?<!\S)#EOL\b/\n<\/ol>\n/gi;
-e "s|#[eE][eE][mM]|</em>|g" \ s(?<!\S)#LI\b/<li>/gi;
-e "s|#[dD][iI][vV]|<div>|g" \ s(?<!\S)#ELI\b/<\/li>\n/gi;
-e "s|#[eE][dD][iI][vV]|</div>\n|g" \ s(?<!\S)#U\b/<u>/gi;
-e "s|#[sS][pP][aA][nN]|<span>|g" \ s(?<!\S)#EU\b/<\/u>/gi;
-e "s|#[eE][sS][pP][aA][nN]|</span>\n|g" \ s(?<!\S)#TBL\b/<table>/gi;
-e "s|#[uU][lL]|\n<ul>|g" \ s(?<!\S)#ETBL\b/<\/table>\n/gi;
-e "s|#[oO][lL]|\n<ol>|g" \ s(?<!\S)#TR\b/<tr>/gi;
-e "s|#[lL][iI]|\n<li>|g" \ s(?<!\S)#ETR\b/<\/tr>\n/gi;
-e "s|#[eE][uU][lL]|\n</ul>\n|g" \ s(?<!\S)#TD\b/<td>/gi;
-e "s|#[eE][oO][lL]|\n</ol>\n|g" \ s(?<!\S)#ETD\b/<\/td>\n/gi;
-e "s|#[eE][lL][iI]|</li>\n|g" \ s(?<!\S)#TH\b/<th>/gi;
-e "s|#[uU]|<u>|g" \ s(?<!\S)#ETH\b/<\/th>\n/gi;
-e "s|#[eE][uU]|</u>|g" \ s(?<!\S)#ART\b/<article>/gi;
-e "s|#[tT][aA][bB][lL][eE]|<table>|g" \ s(?<!\S)#EART\b/<\/article>\n/gi;
-e "s|#[eE][tT][aA][bB][lL][eE]|</table>\n|g" \ s(?<!\S)#SEC\b/<section>/gi;
-e "s|#[tT][rR]|<tr>|g" \ s(?<!\S)#ESEC\b/<\/section>\n/gi;
-e "s|#[eE][tT][rR]|</tr>\n|g" \ s(?<!\S)#ASIDE\b/<aside>/gi;
-e "s|#[tT][dD]|<td>|g" \ s(?<!\S)#EASIDE\b/<\/aside>\n/gi;
-e "s|#[eE][tT][dD]|</td>\n|g" \ s(?<!\S)#NAV\b/<nav>/gi;
-e "s|#[tT][hH]|<th>|g" \ s(?<!\S)#ENAV\b/<\/nav>\n/gi;
-e "s|#[eE][tT][hH]|</th>\n|g" \ s(?<!\S)#BTN\b/<button>/gi;
-e "s|#[aA][rR][tT]|<article>|g" \ s(?<!\S)#EBTN\b/<\/button>\n/gi;
-e "s|#[eE][aA][rR][tT]|</article>|g" \ s(?<!\S)#SEL\b/<select>/gi;
-e "s|#[sS][eE][cC][tT]|<section>|g" \ s(?<!\S)#ESEL\b/<\/select>\n/gi;
-e "s|#[eE][sS][eE][cC][tT]|</section>\n|g" \ s(?<!\S)#OPT\b/<option>/gi;
-e "s|#[aA][sS][iI][dD][eE]|<aside>|g" \ s(?<!\S)#EOPT\b/<\/option>\n/gi;
-e "s|#[eE][aA][sS][iI][dD][eE]|</aside>|g" \ s/(?<!\S)\#LT\b/\&lt;/gi;
-e "s|#[nN][aA][vV]|<nav>|g" \ s/(?<!\S)\#GT\b/\&gt;/gi;
-e "s|#[eE][nN][aA][vV]|</nav>\n|g" \ s/(?<!\S)\#NUM\b/\&num;/gi;
-e "s|#[bB][tT][nN]|<button>|g" \ ' <<< "$content")
-e "s|#[eE][bB][tT][nN]|</button>|g" \
-e "s|#[sS][lL][cC][tT]|<select>|g" \
-e "s|#[eE][sS][lL][cC][tT]|</select>|g" \
-e "s|#[oO][pP][tT]|<option>|g" \
-e "s|#[eE][oO][pP][tT]|</option>|g" )
# Return the HTML content
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