regex - converting markdown lists to html lists in PHP -
i have been working on markdown converter today project of mine. got lost in , ran through code , noticed had huge amount converting lists. code below , have supplied code deals lists, suggestions on shortening code, cant see past have written , need fresh pair of eyes.
<?php class markdown { private static $html = ''; private static $list_types = array( array( '>>', '<ul>', '</ul>' ), array( '>>>', '<ol>', '</ol>' ) ); private static $list_patterns = array( '/-[ ]+(.+)/' => ' >>\1', '/[0-9]{1}\. (.+)/' => ' >>>\1' ); public static function converttext($markdown = array()) { $markdown = explode("\n", strip_tags($markdown)); foreach ($markdown &$line) { $line = htmlentities($line, ent_quotes, 'utf-8'); foreach (self::$list_patterns $pattern => $replace) { if (!is_array($line) && preg_match($pattern, $line)) { $para = false; $line = preg_replace($pattern, $replace, $line); $type = 0; foreach (self::$list_types $key => $val) { if (preg_match('/ ' . $val[0] . ' /', $line)) $type = $key; } $line = preg_split('/' . self::$list_types[$type][0] . '/', $line); $line = array('depth' => strlen($line[0]), 'string' => $line[1], 'type' => $type); } } } while (!empty($markdown)) { $snippet = array_shift($markdown); if (is_array($snippet)) self::makelist($snippet, $markdown); else self::$html .= $snippet; } return self::$html; } private static function makelist($snippet, &$markdown, $last_depth = 0, $close_tag = '') { if ($last_depth == $snippet['depth']) self::$html .= sprintf('</li><li>%s', $snippet['string']); elseif ($last_depth < $snippet['depth']) self::$html .= sprintf('%s<li>%s', self::$list_types[$snippet['type']][1], $snippet['string']); elseif ($last_depth > $snippet['depth']) self::$html .= sprintf('</li>%s<li>%s', $close_tag, $snippet['string']); $next_snippet = array_shift($markdown); if (is_array($next_snippet)) self::makelist($next_snippet, $markdown, $snippet['depth'], self::$list_types[$snippet['type']][2]); else array_unshift($markdown, $next_snippet); self::$html .= sprintf('</li>%s', $close_tag); } } ?>
basically code lots of pattern matching , patterns except list leave string in array "$markdown", lists creates array list type, depth , string. there fore @ end of text conversion can loop through "$markdown" array , build nested loop structure checking if next item array or not.
sorry if question seems vague, not ment be, want hint on how shorten code seems have written loads.
thanks in advance
luke
@theifmaster better way forward. but, managed remove 2 lines of code, , unused variable $para;
<?php class markdown { private static $html = ''; private static $list_types = array( array('>>','<ul>','</ul>'), array('>>>','<ol>','</ol>') ); private static $list_patterns = array( '/-[ ]+(.+)/' => ' >>\1', '/[0-9]{1}\. (.+)/' => ' >>>\1' ); public static function converttext($markdown = array()) { foreach (explode("\n", strip_tags($markdown)) &$line) { $line = htmlentities($line, ent_quotes, 'utf-8'); foreach (self::$list_patterns $pattern => $replace) { if (!is_array($line) && preg_match($pattern, $line)) { $line = preg_replace($pattern, $replace, $line); $type = 0; foreach (self::$list_types $key => $val) { if (preg_match('/ ' . $val[0] . ' /', $line)) $type = $key; } $line = preg_split('/' . self::$list_types[$type][0] . '/', $line); $line = array('depth' => strlen($line[0]), 'string' => $line[1], 'type' => $type); } } } while (!empty($markdown)) { $snippet = array_shift($markdown); if (is_array($snippet)) self::makelist($snippet, $markdown); else self::$html .= $snippet; } return self::$html; } private static function makelist($snippet, &$markdown, $last_depth = 0, $close_tag = '') { if ($last_depth == $snippet['depth']) self::$html .= sprintf('</li><li>%s', $snippet['string']); elseif ($last_depth < $snippet['depth']) self::$html .= sprintf('%s<li>%s', self::$list_types[$snippet['type']][1], $snippet['string']); elseif ($last_depth > $snippet['depth']) self::$html .= sprintf('</li>%s<li>%s', $close_tag, $snippet['string']); $next_snippet = array_shift($markdown); if (is_array($next_snippet)) self::makelist($next_snippet, $markdown, $snippet['depth'], self::$list_types[$snippet['type']][2]); else array_unshift($markdown, $next_snippet); self::$html .= sprintf('</li>%s', $close_tag); } }
enjoy
Comments
Post a Comment