Note that there are some explanatory texts on larger screens.

plurals
  1. POconverting markdown lists to html lists in PHP
    primarykey
    data
    text
    <p>I have been working on a markdown converter today for a project of mine. I got lost in it and just ran through the code and noticed I had a huge amount for converting lists. My code is below , I have just supplied the code that deals with lists, I could really do with some suggestions on shortening the code, I just cant see past what I have written and need a fresh pair of eyes.</p> <pre><code>&lt;?php class Markdown { private static $html = ''; private static $list_types = array( array( '&gt;&gt;', '&lt;ul&gt;', '&lt;/ul&gt;' ), array( '&gt;&gt;&gt;', '&lt;ol&gt;', '&lt;/ol&gt;' ) ); private static $list_patterns = array( '/-[ ]+(.+)/' =&gt; ' &gt;&gt;\1', '/[0-9]{1}\. (.+)/' =&gt; ' &gt;&gt;&gt;\1' ); public static function convertText($markdown = array()) { $markdown = explode("\n", strip_tags($markdown)); foreach ($markdown as &amp;$line) { $line = htmlentities($line, ENT_QUOTES, 'UTF-8'); foreach (self::$list_patterns as $pattern =&gt; $replace) { if (!is_array($line) &amp;&amp; preg_match($pattern, $line)) { $para = false; $line = preg_replace($pattern, $replace, $line); $type = 0; foreach (self::$list_types as $key =&gt; $val) { if (preg_match('/ ' . $val[0] . ' /', $line)) $type = $key; } $line = preg_split('/' . self::$list_types[$type][0] . '/', $line); $line = array('depth' =&gt; strlen($line[0]), 'string' =&gt; $line[1], 'type' =&gt; $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, &amp;$markdown, $last_depth = 0, $close_tag = '') { if ($last_depth == $snippet['depth']) self::$html .= sprintf('&lt;/li&gt;&lt;li&gt;%s', $snippet['string']); elseif ($last_depth &lt; $snippet['depth']) self::$html .= sprintf('%s&lt;li&gt;%s', self::$list_types[$snippet['type']][1], $snippet['string']); elseif ($last_depth &gt; $snippet['depth']) self::$html .= sprintf('&lt;/li&gt;%s&lt;li&gt;%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('&lt;/li&gt;%s', $close_tag); } } ?&gt; </code></pre> <p>Basically the code does lots of pattern matching and for any patterns except list it will leave as a string in the array "$markdown", for lists it creates an array with the list type, depth and string. There fore at the end of the text conversion I can loop through the "$markdown" array and build the nested loop structure by checking if the next item is an array or not.</p> <p>Sorry if the question seems vague, its not ment to be, I just want some hint on how to shorten my code as it seems like I have written loads.</p> <p>Thanks in advance</p> <p>Luke</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload