Ejemplo n.º 1
0
 /**
  * Main parsing method.
  * Splits on patterns to separate comments from code
  * @return `array`
  *    array("code"=>`array of code lines`, "docs"=>`array of doc blocks`)
  **/
 public function parse($file)
 {
     // Load the file
     $file = trim(file_get_contents($file));
     // Determine if we start with a documentation or code block
     $is_start_doc = '<!--' == substr($file, 0, 4);
     // Explode by start comment delimiter.
     // If we start with a comment block every array entry will contain
     // documentation first followed by code. Otherwise we start with a entry
     // containing nothing but code followed by documentation-code entries.
     $file = explode('<!--', $file);
     // Explode every entry by end comment delimiter.
     foreach ($file as &$f) {
         $f = explode('-->', $f);
     }
     // Every entry should now be an array containing documentation first and
     // code second. If we started with a code only block then we add an empty
     // documentation block to keep this structure.
     if (!$is_start_doc) {
         array_unshift($file[0], '');
     }
     // Separate entries into code and documentation
     $docs = array();
     $code = array();
     for ($i = 0; $i < sizeof($file); $i++) {
         $docs[] = trim($file[$i][0]);
         $code[] = $file[$i][1];
     }
     // Passes code onto pygmentize to add syntax highlighting
     // Assemble the code into a single string we can pass on to pygmentize.
     // Each block is separated by a delimiter.
     $code = implode("\n<!--CODEBLOCK-->\n", $code);
     $pyg = new Pygment();
     $code = $pyg->pygmentize("xml", $code);
     // Separate by syntax highlighted delimiter again
     $code = explode('<span class="c">&lt;!--CODEBLOCK--&gt;</span>', $code);
     foreach ($code as &$val) {
         $val = rtrim($val);
         $val = str_replace("\t", " ", $val);
         $val = '<div class="highlight"><pre>' . trim($val, "\n\r") . '</pre></div>';
     }
     // Pass documentation through Markdown
     foreach ($docs as &$doc) {
         $doc = Markdown::defaultTransform($doc);
     }
     // Our final array of code mapped to comments
     return array("code" => $code, "docs" => $docs);
 }
Ejemplo n.º 2
0
 /**
  * Main parsing method.
  * Uses the php tokenizer to separate comments from code
  * @return `array` 
  *    array("code"=>`array of code lines`, "docs"=>`array of doc blocks`)
  *
  **/
 public function parse($file)
 {
     $fname = $file;
     $file = file_get_contents($file);
     $code = "";
     $all_matches = array();
     $prev_token = false;
     foreach (token_get_all($file) as $tok) {
         if (is_array($tok)) {
             if (in_array(token_name($tok[0]), $this->comment_tokens)) {
                 if (token_name($prev_tok[0]) == "T_COMMENT" && token_name($tok[0]) == "T_COMMENT") {
                     $last = array_pop($all_matches);
                     $tok[1] = str_replace("\t", "  ", $tok[1]);
                     $all_matches[] = $last . preg_replace($this->comment_blocks, "\$1", $tok[1]);
                 } else {
                     $tok[1] = str_replace("\t", "  ", $tok[1]);
                     $all_matches[] = preg_replace($this->comment_blocks, "\$1", $tok[1]);
                     $code .= "\n//CODEBLOCK\n";
                 }
             } else {
                 $code .= $tok[1];
             }
         } else {
             $code .= $tok;
         }
         $prev_token = $tok;
     }
     // Passes code onto pygmentize to add syntax highlighting
     $pyg = new Pygment();
     $code = $pyg->pygmentize("php", $code);
     $code = explode('<span class="c1">//CODEBLOCK</span>', $code);
     array_shift($code);
     foreach ($code as &$val) {
         $val = rtrim($val);
         $val = str_replace("\t", " ", $val);
         $val = '<div class="highlight"><pre>' . trim($val, "\n\r") . '</pre></div>';
     }
     // Adds html markup to identify php docblock parameters
     foreach ($all_matches as $match) {
         $match = preg_replace($this->doc_params, "<em class='docparam'>\$1</em>", $match);
         $docs[] = Markdown($match);
     }
     // Our final array of code mapped to comments
     return array("code" => $code, "docs" => $docs);
 }