1
0
mirror of https://tildegit.org/sbgodin/HtmGem.git synced 2023-08-25 13:53:12 +02:00
This commit is contained in:
Christophe HENRY 2021-03-03 16:07:56 +01:00
parent b2d5ecf990
commit 96c1303663
2 changed files with 76 additions and 15 deletions

View File

@ -37,24 +37,59 @@ echo(<<<EOL
</style> </style>
</head> </head>
<body> <body>
EOL); EOL);
function addTextAttributes($line) { /**
$output = $line; * Replaces markups things like __underlined__ to <u>underlined</u>.
$output = preg_replace("#__([^_]+)(?:__)?#", "<u>\$1</u>", $output); * @param $instruction the characters to replace, ex. _
$output = preg_replace("#//([^/]+)(?://)?#", "<i>\$1</i>", $output); * @param $markup the markup to replace to, ex. "u" to get <u></u>
$output = preg_replace("#~~([^~]+)(?:~~)?#", "<del>\$1</del>", $output); * @param &$text where to replace.
$output = preg_replace("#\*\*([^\*]+)(?:\*\*)?#", "<b>\$1</b>", $output); */
return $output; function markupPreg($instruction, $markup, &$text) {
#return preg_replace("#{$instruction}((?!{$instruction}.)+)(?:{$instruction})?#", "<{$markup}>$1</{$markup}>", $text);
$output = $text;
# Replaces couples "__word__" into "<i>word</i>".
$output = preg_replace("#${instruction}(.+?)${instruction}#", "<{$markup}>$1</{$markup}>", $output);
# Replaces a remaining __ into "<i>…</i>" to the end of the line.
$output = preg_replace("#${instruction}(.+)#", "<{$markup}>$1</{$markup}>", $output);
$text = $output;
}
/**
* Adds text attributes sucj as underline, bold, to $line
* @param $line the line to process
*/
function addTextAttributes(&$line) {
markupPreg("__", "u", $line);
markupPreg("\*\*", "b", $line);
markupPreg("//", "i", $line);
markupPreg("~~", "del", $line);
}
/**
* Escapes the HTML entities yet contained in the Gemtext, keeps multiple spaces.
* @param $text1, $text2 texts to process
*/
function htmlEscape(&$text1, &$text2=null) {
$text1 = htmlspecialchars($text1, ENT_HTML5, "UTF-8", false);
# https://en.wikipedia.org/wiki/Whitespace_character#Unicode
$text1 = preg_replace("# #", "&puncsp;&puncsp;", $text1); #TODO: a way to not touch single spaces as "&ensp;" > " "
if (!is_null($text2)) return htmlEscape($text2);
} }
$mode = null; $mode = null;
$mode_textAttributes = true;
foreach ($fileLines as $line) { foreach ($fileLines as $line) {
$reDo = true; $reDo = true;
$line1 = substr($line, 0, 1); $line1 = substr($line, 0, 1);
$line2 = substr($line, 0, 2); $line2 = substr($line, 0, 2);
$line3 = substr($line, 0, 3); $line3 = substr($line, 0, 3);
$line = htmlspecialchars($line, ENT_HTML5, "UTF-8", false);
while ($reDo) { while ($reDo) {
$reDo = false; # Change in modes need to redo one loop as they cant handle the case $reDo = false; # Change in modes need to redo one loop as they cant handle the case
if (is_null($mode)) { if (is_null($mode)) {
@ -64,6 +99,7 @@ foreach ($fileLines as $line) {
preg_match("/^(#{1,3})\s*(.*)/", $line, $sharps); preg_match("/^(#{1,3})\s*(.*)/", $line, $sharps);
$h_level = strlen($sharps[1]); $h_level = strlen($sharps[1]);
$text = $sharps[2]; $text = $sharps[2];
htmlEscape($text);
switch ($h_level) { switch ($h_level) {
case 1: print("<h1>".$text."</h1>\n"); break; case 1: print("<h1>".$text."</h1>\n"); break;
case 2: print("<h2>".$text."</h2>\n"); break; case 2: print("<h2>".$text."</h2>\n"); break;
@ -74,7 +110,12 @@ foreach ($fileLines as $line) {
$url_link = $linkParts[1]; $url_link = $linkParts[1];
$url_label = $linkParts[2]; $url_label = $linkParts[2];
if (empty($url_label)) $url_label = $url_link; if (empty($url_label)) $url_label = $url_link;
htmlEscape($url_link, $url_label);
if ($mode_textAttributes) addTextAttributes($url_label);
print("<p><a href='".$url_link."'>".$url_label."</a></p>\n"); print("<p><a href='".$url_link."'>".$url_label."</a></p>\n");
} elseif ('"""' == $line3) {
$mode_textAttributes = !$mode_textAttributes;
$reDo = true;
} elseif ("```" == $line3) { } elseif ("```" == $line3) {
$mode="pre"; $mode="pre";
print("<pre>\n"); print("<pre>\n");
@ -86,19 +127,24 @@ foreach ($fileLines as $line) {
if (empty($quote)) if (empty($quote))
print("<p>&nbsp;</p>\n"); print("<p>&nbsp;</p>\n");
else else
print("<p>".$quoteParts[1]."</p>\n"); htmlEscape($quote);
print("<p>".$quote."</p>\n");
} elseif ("*" == $line1 && "**" != $line2) { } elseif ("*" == $line1 && "**" != $line2) {
$mode = "ul"; $mode = "ul";
$reDo = true; $reDo = true;
print("<ul>\n"); print("<ul>\n");
} else { } else {
print("<p>".addTextAttributes($line)."</p>\n"); htmlEscape($line);
if ($mode_textAttributes) addTextAttributes($line);
print("<p>$line</p>\n");
} }
} elseif ("pre"==$mode) { } elseif ("pre"==$mode) {
if ("```" == $line3) { if ("```" == $line3) {
$mode=null; $mode=null;
print("</pre>\n"); print("</pre>\n");
} else { } else {
htmlEscape($line);
if ($mode_textAttributes) addTextAttributes($line);
print($line."\n"); print($line."\n");
} }
} elseif ("quote"==$mode) { } elseif ("quote"==$mode) {
@ -108,6 +154,7 @@ foreach ($fileLines as $line) {
if (empty($quote)) if (empty($quote))
print("<p>&nbsp;</p>\n"); print("<p>&nbsp;</p>\n");
else else
htmlEscape($quote);
print("<p>".$quote."</p>\n"); print("<p>".$quote."</p>\n");
} else { } else {
print("</blockquote>\n"); print("</blockquote>\n");
@ -121,16 +168,18 @@ foreach ($fileLines as $line) {
if (empty($li)) if (empty($li))
print("<li>&nbsp;\n"); print("<li>&nbsp;\n");
else else
print("<li>".$ulParts[1]."\n"); htmlEscape($li);
print("<li>".$li."\n");
} else { } else {
$mode = null; $mode = null;
print("</ul>\n"); print("</ul>\n");
$reDo = true; $reDo = true;
} }
} }
$line = "";
} }
} }
echo "</body>\n</html>\n";
ob_end_flush(); ob_end_flush();
?> ?>

View File

@ -4,15 +4,19 @@
### h3 ### h3
#### h4 (Should be read as h3) #### h4 (Should be read as h3)
Four spaces between the quotes " "
** Text using <b>. ** Text using <b>.
//Text using <i>. //Text using <i>.
~~Text using <del>. ~~Text using <del>.
__Text using <u>. __Text using <u>.
Text having words in **bold**, //italic//, ~~deleted~~, __underlined__. And some other mixed **bold AND //bold italic// and ~~bold deleted~~**. Text having words in **bold**, //italic//, ~~deleted~~, __underlined__.
And some other mixed: **//bold italic// ~~bold deleted~~** //~~**italic deleted bold.
Text attributes __underlined, plus ~~deleted having underlined stop now__ then deleted after~~. __This line starts with two _ and none at the end.
The formatting stays in the physical line.
Below, headers with spaces at the beginning of the line: Below, headers with spaces at the beginning of the line:
# Missed h1, there's a space as a first character. # Missed h1, there's a space as a first character.
@ -39,6 +43,7 @@ Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium dolor
=> ... => ...
# Preformatted text # Preformatted text
"""
``` Preformatted text ``` Preformatted text
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
@ -50,8 +55,15 @@ At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praese
||----w | ||----w |
|| || || ||
<b>Not in HTML bold</b> and not in **markup bold**
``` ```
"""
```
Preformatted text with **bold**, //italic//, and so on…
```
# Quotations # Quotations
## Two consecutives lines of quotations ## Two consecutives lines of quotations