Alpha Framework alpha--util
[ class tree: alpha--util ] [ index: alpha--util ] [ all elements ]

Source for file AlphaMarkdown.inc

Documentation is available at AlphaMarkdown.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/lib/markdown/markdown.php';
  4. require_once $config->get('sysRoot').'alpha/view/widgets/Image.inc';
  5.  
  6. /**
  7.  *
  8.  * A custom version of the markdown class which uses the geshi library for rendering code
  9.  * 
  10.  * @package alpha::util
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: AlphaMarkdown.inc 1454 2011-12-04 15:14:05Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class AlphaMarkdown extends MarkdownExtra_Parser {
  52.     
  53.     /**
  54.      * Custom version of the _doCodeBlocks_callback method which invokes a Gheshi
  55.      * object to render code.
  56.      * 
  57.      * @since 1.0
  58.      */
  59.     public function _doCodeBlocks_callback($matches{        
  60.         $codeblock $matches[1];    
  61.         
  62.         $codeblock $this->outdent($codeblock);
  63.         
  64.         // trim leading newlines and trailing whitespace
  65.         $codeblock preg_replace(array('/\A\n+/''/\n+\z/')''$codeblock);        
  66.     
  67.         // find the code block and replace it with a blank
  68.         $codeTypeTag array();        
  69.         preg_match('/codeType=\[.*\]/'$codeblock$codeTypeTag);
  70.         $codeblock preg_replace('/codeType=\[.*\]\n/'''$codeblock);    
  71.         
  72.         if(isset($codeTypeTag[0])) {
  73.             $start strpos($codeTypeTag[0]'[');
  74.             $end strpos($codeTypeTag[0]']');
  75.             $language substr($codeTypeTag[0]$start+1$end-($start+1));            
  76.         }else{
  77.             // will use php as a defualt language type when none is provided
  78.             $language 'php';            
  79.         }
  80.         
  81.         $geshiInstalled true;
  82.         
  83.         try{
  84.             fopen('geshi.php''r'true);
  85.             require_once 'geshi.php';
  86.         }catch (Exception $e{
  87.             $geshiInstalled false;
  88.         }
  89.         
  90.         if($geshiInstalled{
  91.             $geshi new GeSHi($codeblock$language);
  92.             $codeblock $geshi->parse_code();
  93.         }
  94.         
  95.         $result "\n\n".$this->hashBlock("<pre><code>" $codeblock "\n</code></pre>")."\n\n";    
  96.         
  97.         return $result;        
  98.     }
  99.     
  100.     /**
  101.      * Custom _doAnchors_inline_callback that renders links to external sites with a
  102.      * target attribute and an icon.
  103.      * 
  104.      * @since 1.0
  105.      */
  106.     public function _doAnchors_inline_callback($matches{
  107.         global $config;
  108.         
  109.         $whole_match    =  $matches[1];
  110.         $link_text        =  $this->runSpanGamut($matches[2]);
  111.         $url            =  $matches[3];
  112.         $title            =$matches[6];
  113.         
  114.         $external false;
  115.         
  116.         $parts parse_url($url);
  117.  
  118.         /*
  119.          * Only an external link if:
  120.          * 
  121.          * 1. $url parses to a valid URL
  122.          * 2. $url has a host part
  123.          * 3. $url does not contain $config->get('sysURL'), i.e. points to a local resource.
  124.          */
  125.         if(is_array($parts&& isset($parts['host']&& strpos($url$config->get('sysURL')) === false)
  126.             $external true;            
  127.         
  128.         $url $this->encodeAmpsAndAngles($url);
  129.  
  130.         $result "<a href=\"$url\"";
  131.         if (isset($title)) {
  132.             $title str_replace('"''&quot;'$title);
  133.             $title $this->encodeAmpsAndAngles($title);
  134.             $result .=  " title=\"$title\"";
  135.         }
  136.         if ($external{
  137.             $result .= " target=\"$url\"";
  138.         }
  139.         
  140.         $link_text $this->runSpanGamut($link_text);
  141.         if (!$external{
  142.             $result .= ">$link_text</a>";
  143.         }else{
  144.             $result .= '>'.$link_text.'<img src="'.$config->get('sysURL').'alpha/images/icons/page_go.png'.'" class="externalLink"/></a>';
  145.         }
  146.  
  147.         return $this->hashSpan($result);
  148.     }
  149.     
  150.     /**
  151.      * Custom version of the _doTable_callback(...) method which sets the table border and CSS style
  152.      * 
  153.      * @since 1.0
  154.      */
  155.     public function _doTable_callback($matches{
  156.         $head        $matches[1];
  157.         $underline    $matches[2];
  158.         $content    $matches[3];
  159.  
  160.         # Remove any tailing pipes for each line.
  161.         $head        preg_replace('/[|] *$/m'''$head);
  162.         $underline    preg_replace('/[|] *$/m'''$underline);
  163.         $content    preg_replace('/[|] *$/m'''$content);
  164.         
  165.         # Reading alignement from header underline.
  166.         $separators    preg_split('/ *[|] */'$underline);
  167.         foreach ($separators as $n => $s{
  168.             if (preg_match('/^ *-+: *$/'$s))        $attr[$n' align="right"';
  169.             else if (preg_match('/^ *:-+: *$/'$s))$attr[$n' align="center"';
  170.             else if (preg_match('/^ *:-+ *$/'$s))    $attr[$n' align="left"';
  171.             else                                    $attr[$n'';
  172.         }
  173.         
  174.         # Creating code spans before splitting the row is an easy way to 
  175.         # handle a code span containg pipes.
  176.         $head    $this->doCodeSpans($head);
  177.         $headers    preg_split('/ *[|] */'$head);
  178.         $col_count    count($headers);
  179.         
  180.         # Write column headers.
  181.         $text "<table class=\"bordered\" border=\"1\">\n";
  182.         $text .= "<thead>\n";
  183.         $text .= "<tr>\n";
  184.         foreach ($headers as $n => $header)
  185.             $text .= "  <th$attr[$n]>".$this->runSpanGamut(trim($header))."</th>\n";
  186.         $text .= "</tr>\n";
  187.         $text .= "</thead>\n";
  188.         
  189.         # Split content by row.
  190.         $rows explode("\n"trim($content"\n"));
  191.         
  192.         $text .= "<tbody>\n";
  193.         foreach ($rows as $row{
  194.             # Creating code spans before splitting the row is an easy way to 
  195.             # handle a code span containg pipes.
  196.             $row $this->doCodeSpans($row);
  197.             
  198.             # Split row by cell.
  199.             $row_cells preg_split('/ *[|] */'$row$col_count);
  200.             $row_cells array_pad($row_cells$col_count'');
  201.             
  202.             $text .= "<tr>\n";
  203.             foreach ($row_cells as $n => $cell)
  204.                 $text .= "  <td$attr[$n]>".$this->runSpanGamut(trim($cell))."</td>\n";
  205.             $text .= "</tr>\n";
  206.         }
  207.         $text .= "</tbody>\n";
  208.         $text .= "</table>";
  209.         
  210.         return $this->hashBlock($text"\n";
  211.     }
  212. }
  213.  
  214. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:10 +0000 by phpDocumentor 1.4.3