2010-11-30 2 views
3

私は現在、私のプロジェクトのためにマークダウンコンバータに取り組んでいます。私はそれで迷ってコードを実行し、リストを変換するための膨大な量があることに気づいた。私のコードは以下の通りですが、私はリストを扱うコードを提供しました。コードを短くするための提案がありました。PHPでマークダウンリストをhtmlリストに変換する

<?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 as &$line) { 
     $line = htmlentities($line, ENT_QUOTES, 'UTF-8'); 
     foreach (self::$list_patterns as $pattern => $replace) { 
     if (!is_array($line) && preg_match($pattern, $line)) { 
      $para = false; 
      $line = preg_replace($pattern, $replace, $line); 
      $type = 0; 
      foreach (self::$list_types as $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); 
    } 

} 

?> 

基本的にコードは、パターンマッチングの多くを行い、リスト以外の任意のパターンのためには、リストには、リストの種類、深さと文字列の配列を作成し、配列「$マークダウン」の文字列として残します。テキスト変換の最後に、 "$ markdown"配列をループし、次の項目が配列かどうかを調べることでネストされたループ構造を構築することができます。

申し訳ありませんが、質問があいまいであると思われる場合は、私はロードを記述したようにコードをどのように短縮するかについてのヒントが必要です。事前に

おかげ

ルーク

+9

あなたは既に野生の完全なPHPマークダウンの実装が実現していますか? BSD/GPLのライセンスを受けています(あなたが好むもの)ので、ソフトウェアに埋め込むことは問題ではありません。 http://michelf.com/projects/php-markdown/ – ThiefMaster

+0

@ thiefは言った。あなたが本当に車輪に興味がない限り、それらを再開発しないでください。 – deceze

+0

私はこのクラスを見て、私のために不要なものをすべて取り除いた後(私はワードプレスのものなどは必要ありません)、そのコードはまだ1500行あります。私はその1500行に大きなコメントがあることを知っていますが、私はちょうどそれがより小さなパッケージで達成可能であることを望んでいました。そして、この種の車輪は私のためにいくつかの興味を持っています。しかし、コメントをありがとう。 – Luke

答えて

0

をお楽しみください。コードを書くことを始めるにもかかわらず、良い学習経験。

2

@Theifmasterは、おそらく前方より良い方法です。しかし、私は2行のコードと未使用の変数$ paraを削除することができました。 @Theifmasterが言ったように

<?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)) as &$line) { 
     $line = htmlentities($line, ENT_QUOTES, 'UTF-8'); 
     foreach (self::$list_patterns as $pattern => $replace) { 
     if (!is_array($line) && preg_match($pattern, $line)) { 
      $line = preg_replace($pattern, $replace, $line); 
      $type = 0; 
      foreach (self::$list_types as $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); 
    } 

} 

はので、私は、私は本当にそれをより便利に何かを追加するために管理するためのつもりではなかったとして、それを使用して終了michelf.com/projects/php-markdownがすでに包括的で素晴らしいです、

+0

2行を保存してくれてありがとうございました:) +1 – Luke

関連する問題