Files
DomFramework/markdown.php
Dominique Fournier 3d5cce734b Better support of imbricated lists
Better support of empty texts


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1283 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
2014-05-10 18:44:27 +00:00

253 lines
7.7 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
function debugMKD ($msg)
{
//return;
$trace = debug_backtrace();
$back = reset ($trace);
file_put_contents ("/tmp/debug", "[".$back["line"]."] $msg\n", FILE_APPEND);
}
error_reporting (E_ALL);
/** Markdown management */
class markdown
{
/** Convert the markdown language to HTML
Return the HTML string
@param string $mark Message in markdown syntax to display */
public function html ($mark)
{
$res = "";
$search = array ();
$replace = array ();
// SEPARATORS : *** --- ___ * * * - - - _ _ _
// Must be placed before EMPHASIS
$search[] = "/\\n^[*_-] ?[*_-] ?[*_-]$/Um";
$replace[] = "</p>\n<hr/>\n<p>";
// EMPHASIS : _ or * for normal, __ or ** for strong
$search[] = "/__(.+)__/"; $replace[] = "<strong>\\1</strong>";
$search[] = "/_(.+)_/"; $replace[] = "<em>\\1</em>";
$search[] = "/\*\*(.+)\*\*/"; $replace[] = "<strong>\\1</strong>";
$search[] = "/\*(.+)\*/"; $replace[] = "<em>\\1</em>";
// CODE : `code` -> <code>
$search[] = "/\\n?\`((\\n|.)+)\`/Um";
$replace[] = " <code>\\1</code>";
// LINKS
// [Google Site](http://google.fr/ "With help bubble")
$search[] = "(\[(.+)\]\((https?://.+) \"(.+)\"\))";
$replace[] = "<a href='\\2' title='\\3'>\\1</a>";
// [Google Site](http://google.fr/)
$search[] = "(\[(.+)\]\((http.+)\))"; $replace[] = "<a href='\\2'>\\1</a>";
// Automatics links :
// <http://dominique.fournier38.fr>
// <dominique@fournier38.fr>
$search[] = "(\<(https?://.+)\>)"; $replace[] = "<a href='\\1'>\\1</a>";
$search[] = "(\<(.+@.+)\>)"; $replace[] = "<a href='mailto:\\1'>\\1</a>";
// TODO : Links by reference :
// Voici un petit texte écrit par [Michel Fortin][mf].
// [mf]: http://michelf.ca/ "Mon site web"
// TITLES
// Titles ATX (Optionnal sharp at the end)
// ###### Title6
$search[] = "/\\n^###### ([^#]+)(#*)$\\n/Um";
$replace[] = "</p>\n<h6>\\1</h6>\n<p>";
// ##### Title5
$search[] = "/\\n^##### ([^#]+)(#*)$\\n/Um";
$replace[] = "</p>\n<h5>\\1</h5>\n<p>";
// #### Title4
$search[] = "/\\n^#### ([^#]+)(#*)$\\n/Um";
$replace[] = "</p>\n<h4>\\1</h4>\n<p>";
// ### Title3
$search[] = "/\\n^### ([^#]+)(#*)$\\n/Um";
$replace[] = "</p>\n<h3>\\1</h3>\n<p>";
// ## Title2
$search[] = "/\\n^## ([^#]+)(#*)$\\n/Um";
$replace[] = "</p>\n<h2>\\1</h2>\n<p>";
// # Title1
$search[] = "/\\n^# ([^#]+)(#*)$\\n/Um";
$replace[] = "</p>\n<h1>\\1</h1>\n<p>";
// Titles with underline (SeText)
// Titre1
// ======
$search[] = "/\\n^(.+)\\n==+$\\n/Um";
$replace[] = "</p>\n<h1>\\1</h1>\n<p>";
// Titre2
// ------
$search[] = "/\\n^(.+)\\n--+$\\n/Um";
$replace[] = "</p>\n<h2>\\1</h2>\n<p>";
// End of line with double space : <br/>
$search[] = "/( )$/Um"; $replace[] = "<br/>";
$res = preg_replace ($search, $replace, $mark);
$res = $this->paragraph ($res);
return $res;
}
/** Translate the Markdown paragraph in HTML
return the html */
private function paragraph ($mark)
{
$mark = str_replace ("\t", " ", $mark);
$mark = trim ($mark);
if ($mark === "")
return "";
$spacer = " ";
$res = "";
// P, OL, UL (but not LI !)
// Use to found the changing of types
$typeStack = array ();
// Number of spaces
$indentStack = array (-1);
// All the HTML stack (with LI)
$htmlStack = array ();
$lines = explode ("\n", $mark);
foreach ($lines as $line)
{
debugMKD ( "DEBUT:$line");
$type = $this->paragraphType ($line);
debugMKD ( "DEBUT: Type='$type'");
$matches = array ();
switch ($type)
{
case "ol" :
preg_match ("/^( *)[0-9]+\. +(.*)/", $line, $matches);
$lineTxt = $matches[2];
break ;
case "ul" :
preg_match ("/^( *)[-+*] +(.*)/", $line, $matches);
$lineTxt = $matches[2];
break ;
default:
$lineTxt = $line;
}
$indent = strspn ($line, " ");
debugMKD ( "DEBUT: Indent='$indent'");
// Spacing
if ($indent < end ($indentStack))
{
debugMKD ( "DEB1 : Ending of block");
if (end ($htmlStack) === "li")
{
debugMKD ("Pending <Li> : closing");
debugMKD ("</li>");
$res .= "</li>";
array_pop ($htmlStack);
}
$oldType = array_pop ($typeStack);
debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
$res .= "\n".str_repeat (" ", end ($indentStack))."</$oldType>";
array_pop ($htmlStack);
array_pop ($indentStack);
if ($type === "ol" || $type === "ul")
{
debugMKD ("DEB2 : Pending <Li> : closing");
debugMKD ("</li>");
$res .= "\n</li>";
array_pop ($htmlStack);
}
if ($type === "")
{
debugMKD ("DEB3 : End of block");
array_pop ($indentStack);
$res .= "\n";
continue;
}
}
if ($indent == end ($indentStack))
{
debugMKD ( "DEB1 : Continuous block $type/".end ($typeStack));
if (end ($htmlStack) === "li")
{
debugMKD ("Pending <Li> : closing");
debugMKD ("</li>");
$res .= "</li>";
array_pop ($htmlStack);
}
debugMKD ( "DEB1 : Continuous block $type/".end ($typeStack)." SECOND");
if ($type !== end ($typeStack))
{
debugMKD ( "DEB2 : Continuous Block but type change");
$oldType = array_pop ($typeStack);
debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
$res .= "\n".str_repeat (" ", end ($indentStack))."</$oldType>";
array_pop ($htmlStack);
debugMKD (str_repeat (" ", end ($indentStack))."<$type>");
$res .= "\n".str_repeat (" ", end ($indentStack))."<$type>";
$htmlStack[] = $type;
array_push ($indentStack, $indent);
array_push ($typeStack, $type);
}
if ($type === "ol" || $type === "ul")
{
debugMKD ("DEB2 : Adding li");
$htmlStack[] = "li";
debugMKD ( str_repeat (" ", $indent)."<li>");
$res .= "\n".str_repeat (" ", $indent)."<li>";
}
}
if ($indent > end ($indentStack))
{
debugMKD ( "DEB1 : Starting a new block");
if (end ($indentStack))
array_pop ($indentStack);
array_push ($indentStack, $indent);
array_push ($typeStack, $type);
debugMKD ( str_repeat (" ", $indent)."<$type>");
$res .= "\n".str_repeat (" ", $indent)."<$type>";
$htmlStack[] = $type;
if ($type === "ol" || $type === "ul")
{
debugMKD ("DEB2 : Adding li");
$htmlStack[] = "li";
debugMKD ( str_repeat (" ", $indent)."<li>");
$res .= "\n".str_repeat (" ", $indent)."<li>";
}
}
debugMKD ("$lineTxt");
$res .= "$lineTxt";
}
debugMKD ( "DEB1 : End of loop");
$htmlStack = array_reverse ($htmlStack);
foreach ($htmlStack as $type)
{
debugMKD ( "FIN</$type>");
$res .= "</$type>\n";
}
debugMKD ( "-----------\n");
return $res;
}
/** Return the Type of object in the provided line
p, ul, ol, code */
private function paragraphType ($line)
{
$line = ltrim ($line);
if (! isset ($line{0}))
return "";
if ($line{0} === "*" || $line{0} === "-" || $line{0} === "+")
return "ul";
if (preg_match ("/^[0-9]+\. /", $line) === 1)
return "ol";
return "p";
}
}