git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1286 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
350 lines
11 KiB
PHP
350 lines
11 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 = "";
|
|
$mark = htmlentities ($mark, ENT_QUOTES);
|
|
|
|
// Here are the regexp on multilines
|
|
$search = array ();
|
|
$replace = array ();
|
|
// Titles with underline (SeText)
|
|
// Titre1
|
|
// ======
|
|
$search[] = "/^(.+)\\n==+$\\n/Um";
|
|
$replace[] = "\n<h1>\\1</h1>\n";
|
|
// Titre2
|
|
// ------
|
|
$search[] = "/^(.+)\\n--+$\\n/Um";
|
|
$replace[] = "\n<h2>\\1</h2>\n";
|
|
|
|
$mark = preg_replace ($search, $replace, $mark);
|
|
|
|
|
|
$res = $this->paragraph ($mark);
|
|
|
|
return $res;
|
|
}
|
|
|
|
/** Translate the Markdown paragraph in HTML
|
|
return the html */
|
|
private function paragraph ($mark)
|
|
{
|
|
$timeStart = microtime (TRUE);
|
|
// Initialization of convertions
|
|
$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>";
|
|
// End of line with double space : <br/>
|
|
$search[] = "/( )$/Um"; $replace[] = "<br/>";
|
|
|
|
// End of line with continuous on second line : add blank
|
|
// $search[] = "/(.)\\n([A-Za-z0-9])/Um"; $replace[] = "\\1 \\2";
|
|
|
|
// Cleanning the markdown text
|
|
$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);
|
|
$timeInit = microtime (TRUE) - $timeStart;
|
|
foreach ($lines as $nb=>$line)
|
|
{
|
|
debugMKD ("DEBUT:$line");
|
|
if (substr (ltrim ($line), 0, 1) === "<")
|
|
{
|
|
debugMKD ("HTML : Skipped");
|
|
$res .= $line;
|
|
continue;
|
|
}
|
|
$type = $this->paragraphType ($line);
|
|
debugMKD ("DEBUT: Type='$type'");
|
|
$matches = array ();
|
|
switch ($type)
|
|
{
|
|
case "ol" :
|
|
preg_match ("/^( *)[0-9]+\. +(.*)/", $line, $matches);
|
|
if (!isset ($matches[2]))
|
|
$lineTxt = $line;
|
|
else
|
|
$lineTxt = $matches[2];
|
|
break ;
|
|
case "ul" :
|
|
preg_match ("/^( *)[-+*] +(.*)/", $line, $matches);
|
|
if (!isset ($matches[2]))
|
|
$lineTxt = $line;
|
|
else
|
|
$lineTxt = $matches[2];
|
|
break ;
|
|
default:
|
|
$lineTxt = $line;
|
|
}
|
|
|
|
$indent = strspn ($line, " ");
|
|
debugMKD ("DEBUT: Indent='$indent'");
|
|
debugMKD ("DEBUT: indentStack=".print_r ($indentStack, TRUE));
|
|
debugMKD ("DEBUT: typeStack=".print_r ($typeStack, TRUE));
|
|
|
|
// 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);
|
|
if ($oldType === "code")
|
|
{
|
|
debugMKD (str_repeat (" ", end ($indentStack))."</code></pre>");
|
|
$res .= str_repeat (" ", end ($indentStack))."</code></pre>\n";
|
|
array_pop ($htmlStack);
|
|
}
|
|
else
|
|
{
|
|
debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
|
|
$res .= str_repeat (" ", end ($indentStack))."</$oldType>\n";
|
|
}
|
|
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) && $type !== "" && end ($typeStack))
|
|
{
|
|
debugMKD ("DEB1 : Continuous block $type/".end ($typeStack));
|
|
if (end ($htmlStack) === "li")
|
|
{
|
|
debugMKD ("Pending <Li> : closing");
|
|
debugMKD ("</li>");
|
|
$res .= "</li>";
|
|
array_pop ($htmlStack);
|
|
}
|
|
|
|
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) && end ($typeStack) !== "code")
|
|
{
|
|
// The code indentation should not be parsed as a new code : the
|
|
// <code></code> continue until the end of paragraph
|
|
debugMKD ("DEB1 : Starting a new block");
|
|
if ($type === "")
|
|
{
|
|
debugMKD ("No type : skipped");
|
|
continue;
|
|
}
|
|
|
|
if (end ($indentStack))
|
|
array_pop ($indentStack);
|
|
if ($type === "code")
|
|
{
|
|
// Code need a pre before code
|
|
if (end ($typeStack))
|
|
{
|
|
debugMKD ("DEB2 : CODE : Close older HTML");
|
|
$oldType = array_pop ($typeStack);
|
|
debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
|
|
$res .= str_repeat (" ", end ($indentStack))."</$oldType>";
|
|
array_pop ($htmlStack);
|
|
}
|
|
$typetmp = "pre><code";
|
|
$htmlStack[] = "pre";
|
|
$htmlStack[] = "code";
|
|
array_push ($typeStack, "code");
|
|
}
|
|
else
|
|
{
|
|
$typetmp = $type;
|
|
$htmlStack[] = $type;
|
|
array_push ($typeStack, $type);
|
|
}
|
|
array_push ($indentStack, $indent);
|
|
debugMKD (str_repeat (" ", $indent)."<$typetmp>");
|
|
$res .= "\n".str_repeat (" ", $indent)."<$typetmp>";
|
|
if ($type === "ol" || $type === "ul")
|
|
{
|
|
debugMKD ("DEB2 : Adding li");
|
|
$htmlStack[] = "li";
|
|
debugMKD (str_repeat (" ", $indent)."<li>");
|
|
$res .= "\n".str_repeat (" ", $indent)."<li>";
|
|
}
|
|
}
|
|
|
|
if ($type === "" && end ($indentStack))
|
|
{
|
|
debugMKD ("DEB2 : Empty type");
|
|
$oldType = array_pop ($typeStack);
|
|
debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
|
|
$res .= "\n".str_repeat (" ", end ($indentStack))."</$oldType>";
|
|
array_pop ($htmlStack);
|
|
}
|
|
|
|
// New paragraphs
|
|
/* if ($type === "p")
|
|
{
|
|
debugMKD ("DEB1 : Starting a new paragraph");
|
|
debugMKD (str_repeat (" ", $indent)."<$type>");
|
|
$res .= "\n".str_repeat (" ", $indent)."<$type>";
|
|
array_push ($typeStack, $type);
|
|
}*/
|
|
|
|
// If code, there is no emphasis, email, and other convertions
|
|
if ($type !== "code")
|
|
{
|
|
$timetmp = microtime (TRUE);
|
|
$lineTxt = preg_replace ($search, $replace, $lineTxt);
|
|
$timeregex += (microtime (TRUE) - $timetmp);
|
|
}
|
|
|
|
debugMKD ("$lineTxt");
|
|
$res .= "$lineTxt\n";
|
|
}
|
|
|
|
debugMKD ("DEB1 : End of loop");
|
|
$htmlStack = array_reverse ($htmlStack);
|
|
foreach ($htmlStack as $type)
|
|
{
|
|
debugMKD ("FIN</$type>");
|
|
$res .= "</$type>\n";
|
|
}
|
|
|
|
debugMKD ("TimeInit=".($timeInit*1000)."ms");
|
|
debugMKD ("TimeRegex=".($timeregex*1000)."ms");
|
|
debugMKD ("TimeAll=".((microtime (TRUE) - $timeStart)*1000)."ms");
|
|
debugMKD ("-----------\n");
|
|
return $res;
|
|
}
|
|
|
|
/** Return the Type of object in the provided line
|
|
p, ul, ol, code */
|
|
private function paragraphType ($line)
|
|
{
|
|
if (! isset ($line{0}))
|
|
return "";
|
|
if (preg_match ("/^[ \t]*[+*-] /", $line) === 1)
|
|
return "ul";
|
|
if (preg_match ("/^[ \t]*[0-9]+\. /", $line) === 1)
|
|
return "ol";
|
|
if (preg_match ("/^( |\t)+/", $line) === 1)
|
|
return "code";
|
|
return "p";
|
|
}
|
|
}
|