Corrects the engine after unit testing
gemTextParser * Removes the last empty lines of text files. * Removes the last spaces (rtrim) even on preformated texts. * Lines like "# " are possible, meaning an empty title. * The line "=>" means an empty link. GemtextTranslate_gemtext * FIX: writes the alt text of preformated blocks * Adds a space when quotation lines have value, otherwise lets ">". * Adds a space when the link has values, otherwise not. * A bit of reformating
This commit is contained in:
parent
46d239b8b3
commit
750632eaff
|
@ -43,4 +43,3 @@ Décoration de texte : **gras** //italique// __souligné__ ~~barré~~
|
||||||
=> mailto:adress@foo.invalid
|
=> mailto:adress@foo.invalid
|
||||||
|
|
||||||
=> mumble:adress.mumble.invalid
|
=> mumble:adress.mumble.invalid
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,8 @@ mb_regex_encoding("UTF-8");
|
||||||
* @param str $fileContents the gemtext to parse
|
* @param str $fileContents the gemtext to parse
|
||||||
*/
|
*/
|
||||||
function gemtextParser($fileContents) {
|
function gemtextParser($fileContents) {
|
||||||
|
if (empty($fileContents)) return array();
|
||||||
|
$fileContents = rtrim($fileContents); // removes last empty line
|
||||||
$fileLines = explode("\n", $fileContents);
|
$fileLines = explode("\n", $fileContents);
|
||||||
$mode = null;
|
$mode = null;
|
||||||
$current = array();
|
$current = array();
|
||||||
|
@ -30,11 +32,11 @@ function gemtextParser($fileContents) {
|
||||||
if ('^^^' == $line3) {
|
if ('^^^' == $line3) {
|
||||||
yield array("mode" => "^^^");
|
yield array("mode" => "^^^");
|
||||||
} elseif ("#" == $line1) {
|
} elseif ("#" == $line1) {
|
||||||
preg_match("/^(#{1,3})\s*(.+)/", $line, $matches);
|
preg_match("/^(#{1,3})\s*(.+)?/", $line, $matches);
|
||||||
yield array("mode" => $matches[1], "title" => trim($matches[2]));
|
yield array("mode" => $matches[1], "title" => trim(@$matches[2]));
|
||||||
} elseif ("=>" == $line2) {
|
} elseif ("=>" == $line2) {
|
||||||
preg_match("/^=>\s*([^\s]+)(?:\s+(.*))?$/", $line, $matches);
|
preg_match("/^=>\s*([^\s]+)(?:\s+(.*))?$/", $line, $matches);
|
||||||
yield array("mode" => "=>", "link" => trim($matches[1]), "text" => trim(@$matches[2]));
|
yield array("mode" => "=>", "link" => trim(@$matches[1]), "text" => trim(@$matches[2]));
|
||||||
} elseif ("```" == $line3) {
|
} elseif ("```" == $line3) {
|
||||||
preg_match("/^```\s*(.*)$/", $line, $matches);
|
preg_match("/^```\s*(.*)$/", $line, $matches);
|
||||||
$current = array("mode" => "```", "alt" => trim($matches[1]), "texts" => array());
|
$current = array("mode" => "```", "alt" => trim($matches[1]), "texts" => array());
|
||||||
|
@ -49,7 +51,7 @@ function gemtextParser($fileContents) {
|
||||||
$mode = "*";
|
$mode = "*";
|
||||||
} else {
|
} else {
|
||||||
// text_line
|
// text_line
|
||||||
yield array("mode"=>"", "text" => trim($line));
|
yield array("mode"=>"", "text" => rtrim($line));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ("```"==$mode) {
|
if ("```"==$mode) {
|
||||||
|
@ -58,7 +60,7 @@ function gemtextParser($fileContents) {
|
||||||
$current = array();
|
$current = array();
|
||||||
$mode = null;
|
$mode = null;
|
||||||
} else {
|
} else {
|
||||||
$current["texts"] []= $line; // No trim() as it’s a preformated text!
|
$current["texts"] []= rtrim($line); // No ltrim() as it’s a preformated text!
|
||||||
}
|
}
|
||||||
} elseif (">"==$mode) {
|
} elseif (">"==$mode) {
|
||||||
if (">" == $line1) {
|
if (">" == $line1) {
|
||||||
|
@ -102,7 +104,11 @@ function gemtextParser($fileContents) {
|
||||||
class GemtextTranslate_gemtext {
|
class GemtextTranslate_gemtext {
|
||||||
|
|
||||||
function __construct($parsedGemtext) {
|
function __construct($parsedGemtext) {
|
||||||
$this->parsedGemtext = $parsedGemtext;
|
if (empty($parsedGemtext)) $parsedGemtext = "";
|
||||||
|
// to delete the last empty lines
|
||||||
|
$parsedGemtext = rtrim($parsedGemtext);
|
||||||
|
// The text must be parsed
|
||||||
|
$this->parsedGemtext = gemtextParser($parsedGemtext);
|
||||||
$this->translate();
|
$this->translate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +126,11 @@ class GemtextTranslate_gemtext {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "```":
|
case "```":
|
||||||
$output .= "```\n";
|
$alt = $node["alt"];
|
||||||
|
if (empty($alt))
|
||||||
|
$output .= "```\n";
|
||||||
|
else
|
||||||
|
$output .= "``` $alt\n";
|
||||||
foreach ($node["texts"] as $text) {
|
foreach ($node["texts"] as $text) {
|
||||||
$output .= "$text\n";
|
$output .= "$text\n";
|
||||||
}
|
}
|
||||||
|
@ -128,13 +138,18 @@ class GemtextTranslate_gemtext {
|
||||||
break;
|
break;
|
||||||
case ">":
|
case ">":
|
||||||
foreach ($node["texts"] as $text) {
|
foreach ($node["texts"] as $text) {
|
||||||
$output .= "> $text\n";
|
if (empty($text))
|
||||||
|
$output .= ">\n";
|
||||||
|
else
|
||||||
|
$output .= "> $text\n";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "=>":
|
case "=>":
|
||||||
$linkText = $node["text"];
|
$linkText = $node["text"];
|
||||||
|
$link = $node["link"];
|
||||||
if (!empty($linkText)) $linkText = " $linkText";
|
if (!empty($linkText)) $linkText = " $linkText";
|
||||||
$output .= "=> ".$node["link"].$linkText."\n";
|
if (!empty($link)) $link = " $link";
|
||||||
|
$output .= "=>".$link.$linkText."\n";
|
||||||
break;
|
break;
|
||||||
case "#":
|
case "#":
|
||||||
case "##":
|
case "##":
|
||||||
|
@ -168,13 +183,11 @@ class GemtextTranslate_html {
|
||||||
public $translatedGemtext;
|
public $translatedGemtext;
|
||||||
|
|
||||||
function __construct($parsedGemtext, $textDecoration=true) {
|
function __construct($parsedGemtext, $textDecoration=true) {
|
||||||
if (empty($parsedGemtext))
|
if (empty($parsedGemtext)) $parsedGemtext = "";
|
||||||
$parsedGemtext = "";
|
// to delete the last empty lines
|
||||||
elseif (is_string($parsedGemtext))
|
$parsedGemtext = rtrim($parsedGemtext);
|
||||||
// to delete the last empty line, <p> </p> in HTML
|
// The text must be parsed
|
||||||
$parsedGemtext = rtrim($parsedGemtext);
|
$parsedGemtext = gemtextParser($parsedGemtext);
|
||||||
// The text must be parsed
|
|
||||||
$parsedGemtext = gemtextParser($parsedGemtext);
|
|
||||||
$this->parsedGemtext = $parsedGemtext;
|
$this->parsedGemtext = $parsedGemtext;
|
||||||
$this->translate($textDecoration);
|
$this->translate($textDecoration);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue