Skip to content

Commit

Permalink
parse markdown in posts
Browse files Browse the repository at this point in the history
  • Loading branch information
johnroper100 committed Aug 17, 2018
1 parent e2dba4b commit 353e313
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function showStyleInput(that) {
</div>
<h2 id="postTitle"><?php echo($postTitle); ?></h2>
<h3 id="postSubtitle"><?php echo($postDate); ?></h3>
<div id="postContent"><?php echo($postContent); ?></div>
<div id="postContent"><?php echo(Slimdown::render($postContent)); ?></div>
<div id="footer">
<p class="footerText"><?php echo($blogCopyright); ?></p>
</div>
Expand Down Expand Up @@ -193,7 +193,7 @@ function showStyleInput(that) {
foreach($posts as $post) {
include $post;
$index--;
echo("<div class=\"post\"><a href=\"posts/$index\"><h2 id=\"postTitle\">$postTitle</h2></a><h3 id=\"postSubtitle\">$postDate</h3><div id=\"postContent\">$postContent</div></div>");
echo("<div class=\"post\"><a href=\"posts/$index\"><h2 id=\"postTitle\">$postTitle</h2></a><h3 id=\"postSubtitle\">$postDate</h3><div id=\"postContent\">".substr(Slimdown::render($postContent), 0, 250)."</div></div>");
}
?>
</div>
Expand All @@ -213,5 +213,63 @@ function test_input($data) {
$data = htmlspecialchars($data);
return $data;
}
class Slimdown {
public static $rules = array (
'/(#+)(.*)/' => 'self::header', // headers
'/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href=\'\2\'>\1</a>', // links
'/\n\*(.*)/' => 'self::ul_list', // ul lists
'/(\*\*|__)(.*?)\1/' => '<strong>\2</strong>', // bold
'/(\*|_)(.*?)\1/' => '<em>\2</em>', // emphasis
'/\~\~(.*?)\~\~/' => '<del>\1</del>', // del
'/\:\"(.*?)\"\:/' => '<q>\1</q>', // quote
'/`(.*?)`/' => '<code>\1</code>', // inline code
'/\n[0-9]+\.(.*)/' => 'self::ol_list', // ol lists
'/\n(&gt;|\>)(.*)/' => 'self::blockquote ', // blockquotes
'/\n-{5,}/' => "\n<hr />", // horizontal rule
'/\n([^\n]+)\n/' => 'self::para', // add paragraphs
'/<\/ul>\s?<ul>/' => '', // fix extra ul
'/<\/ol>\s?<ol>/' => '', // fix extra ol
'/<\/blockquote><blockquote>/' => "\n" // fix extra blockquote
);
private static function para ($regs) {
$line = $regs[1];
$trimmed = trim ($line);
if (preg_match ('/^<\/?(ul|ol|li|h|p|bl)/', $trimmed)) {
return "\n" . $line . "\n";
}
return sprintf ("\n<p>%s</p>\n", $trimmed);
}
private static function ul_list ($regs) {
$item = $regs[1];
return sprintf ("\n<ul>\n\t<li>%s</li>\n</ul>", trim ($item));
}
private static function ol_list ($regs) {
$item = $regs[1];
return sprintf ("\n<ol>\n\t<li>%s</li>\n</ol>", trim ($item));
}
private static function blockquote ($regs) {
$item = $regs[2];
return sprintf ("\n<blockquote>%s</blockquote>", trim ($item));
}
private static function header ($regs) {
list ($tmp, $chars, $header) = $regs;
$level = strlen ($chars);
return sprintf ('<h%d>%s</h%d>', $level, trim ($header), $level);
}
/**
* Render some Markdown into HTML.
*/
public static function render ($text) {
$text = "\n" . $text . "\n";
foreach (self::$rules as $regex => $replacement) {
if (is_callable ( $replacement)) {
$text = preg_replace_callback ($regex, $replacement, $text);
} else {
$text = preg_replace ($regex, $replacement, $text);
}
}
return trim ($text);
}
}
?>
</html>

0 comments on commit 353e313

Please sign in to comment.