2013-05-27 89 views
1

我聽說.NET手冊是從代碼構建的,並且最重要的部分後來被手動改進。從PHP代碼構建文檔

到目前爲止Googleing還沒有出現。我發現的大部分內容都與編碼和記錄最佳實踐有關。但沒有記錄可以幫助以後編寫手冊,特別是不能做到這一點的解決方案。即使不限制搜索到PHP。

有誰知道一個解決方案,最好使用PHP構建,可以從PHP代碼構建類似的文檔?

回答

0

既然這看起來很有趣,我建立了這個實踐和教育。

我發現它實際上比其他所有東西都更方便,因爲它可以讓我根據需要構建文檔。

我也爲它建立了一個HTML生成器,如果有人想要它,我會發布它。

請不要在評論中激進。

<?php 

/**moDO(Classes)(Modo)(Modo_Parse) 

    @(Description) 
    Parses Many One standard code comments and produces an array that can be used to build web manuals. 


    @(Syntax){quote} 
    object `Modo_Parse` (string ~$contents~) 


    @(Parameters) 
    @(Parameters)($contents)(1){info} 
    Contents of the code files that contain code comments to be parsed. 

    @(Parameters)($contents)(2){note warn} 
    Please note very long contents can cause performance issues. 
    It is recommended that you run the parser file by file. 


    @(Return) 
    Returns an object that contains a variable `parsed` that contains the resulting array. 


    @(Examples) 
    @(Examples)(1){info} 
    `Example 1:` Basic usage example. 

    @(Examples)(2){code php} 
    $contents = file_get_contents("Modo_Parse.php"); 
    $parser = new Modo_Parse($contents); 
    print_r($parser->parsed); 


    @(Changelog){list} 
    (1.0) ~Initial release.~ 

*/ 

    /** 
    * Parses Many One standard code comments and produces an array that can be used to build manuals. 
    * @syntax new Modo_Parse($contents); 
    * @contents string containing codes with comments to be parsed 
    */ 
    class Modo_Parse { 
    /** 
    * Takes the parameter and calls the parser function. 
    */ 
    function __construct($contents) { 
     // standardise line breaks 
     $contents = str_replace(Array("\r\n", "\n\r", "\n"), Array("\n", "\n", "\r\n"), $contents); 

     $this->parsed = $this->parser((string)$contents); 
    } 

    /** 
    * The parser function that does all the work. 
    */ 
    private function parser($contents) { 
     $return = Array(); 

     // Identify docs 
     preg_match_all('/(\/\*\*moDO(\([a-z \$_-]+\))+\s+)(.*?)(\s*\r\n*\*\/\r\n)/is', $contents, $docs); 

     foreach($docs[0] AS $doc) { 
     $records = Array(); 

     // Extract and prepare log header 
     $header = explode("\n", trim($doc)); 
     $header = trim(rtrim(str_replace("/**moDO", "", $header[0]))); 
     $header = explode("|", str_replace(Array(")(", "(", ")"), Array("|", "", ""), $header)); 

     // Identify log records 
     preg_match_all('/((@\([a-z0-9 \$_-]+\)+(\{[a-z0-9 _-]+\})?))(.*?)+([email protected]\([a-z0-9 \$_-]+\)|\*\/)/is', $doc, $log_records); 

     foreach($log_records[0] AS $record) { 
      // Separate header and text 
      preg_match("/(@(\([a-z0-9 \$_-]+\))+(\{[a-z0-9 _-]+\})?)(.*)/is", trim($record), $separated); 

      // Extract and prepare record header and style 
      $record_head = str_replace(Array("@", ")(", "(", ")", "{", "}"), Array("", "|", "", "", "~", ""), $separated[1]); 
      $record_head = explode("~", $record_head); 
      if(count($record_head) == 1) { 
      $record_head[] = ""; 
      } 
      $record_head = Array(explode("|", $record_head[0]), $record_head[1]); 

      // Prepare record text 
      if(!strstr($record_head[1], "code")) { 
      $separated[4] = trim(implode("\n", array_map('trim', explode("\n", $separated[4])))); 
      } 
      $separated[4] = preg_replace("/^(\r\n)(.*)/", "$2", rtrim($separated[4])); 

      $record_text = preg_replace(Array("/</", "/>/", "/`(.*?)`/", "/~(.*?)~/"), Array("&lt;", "&gt;", "<b>$1</b>", "<i>$1</i>"), $separated[4]); 

      // Find and set Links 
      preg_match_all("/(\&gt\;)([a-z-.:\/]+)(()([a-z .,;:-_]+))?(\&lt\;)/i", $record_text, $anchors, PREG_SET_ORDER); 
      if(!empty($anchors)) { 
      foreach($anchors AS $anchor) { 
       if(empty($anchor[5])) { 
       $anchor[5] = $anchor[2]; 
       } 
       $record_text = str_replace($anchor[0], '<a href="' . $anchor[2] . '" target="_blank">' . $anchor[5] . '</a>', $record_text); 
      } 
      } 

      // Merge the data together 
      $records = $this->array_merge_recursive_distinct($records, $this->array_chain($record_head[0], Array("style" => $record_head[1], "text" => $record_text))); 
     } 

     $return[] = Array("log" => $header, "data" => $records); 
     } 

     return $return; 
    } 

    /** 
    * Turns a list into a chain of keys with the value in the last key. 
    */ 
    private function array_chain(Array $keys, $value) { 
     $first = array_shift($keys); 

     if (count($keys) === 0) { 
     $return = Array($first => $value); 
     } 
     else { 
     $return = Array($first => $this->array_chain($keys, $value)); 
     } 

     return $return; 
    } 

    /** 
    * Distinct recursive array merge. 
    */ 
    private function array_merge_recursive_distinct(Array &$array1, Array &$array2) { 
     $merged = $array1; 

     foreach($array2 as $key => &$value) { 
     if(is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { 
      $merged[$key] = $this->array_merge_recursive_distinct($merged[$key], $value); 
     } 
     else { 
      $merged[$key] = $value; 
     } 
     } 

     return $merged; 
    } 
    } 
2

我相信phpdoc是您正在尋找的術語:

http://en.wikipedia.org/wiki/PHPDoc

我本人來說使用http://www.phpdoc.org/實施

例如:你定義一個函數

/** 
    * This is the short description for a DocBlock. 
    * 
    * This is the long description for a DocBlock. This text may contain 
    * multiple lines and even some _markdown_. 
    * 
    * 
    * @author Mike van Riel 
    * 
    * @since 1.0 
    * 
    * @param int $example This is an example function/method parameter description. 
    * @param string $example2 This is a second example. 
    * @return int 
    */ 
    function docBlock($example, $example2){ 
     return $example*intval($example2); 
    } 
+0

你能爲我提供了一個鏈接到包括複雜的文檔和示例:瓦片說明,語法,參數,返回,更新日誌,筆記,例子嗎? – transilvlad

+0

也許在http://www.phpdoc.org/docs/latest/for-users/phpdoc-reference.html文件的前幾行將給你主要的想法是如何工作的。 – ulkas

+0

我得到了一般想法,但沒有深入的想法:)我一直在尋找他們的網站,但沒有找到一個與使用示例,更新日誌,筆記,警告等複雜的例子。會產生這樣的事情:http:// php.net/manual/en/function.mail.php – transilvlad

0

之前把這個PhpDocumentorApiGen是很好去。然而,PHP.net可能會使用一個特殊的命令,僅僅爲他們(或者從數據庫中獲取額外的信息,比如更新日誌,以及用戶示例和投票)。

非常重要的是,請記住,沒有文檔標準,並且每個解決方案都可以根據自己的需求或願景實施自己的解決方案。幸運的是,他們都試圖像Javadoc一樣做。

我已經Google了一下,沒有看到Javadoc的@changelog標籤,所以也許唯一的辦法就是在每個docblock中做自己的結構,然後在生成文檔後用一些樣式解析它。