Added a system to extract the link map...
authorRobert Sesek <rsesek@bluestatic.org>
Tue, 3 Apr 2007 20:06:32 +0000 (20:06 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Tue, 3 Apr 2007 20:06:32 +0000 (20:06 +0000)
Markdown.php

index 1a022353260c296bc5f2c1c91d6f732b33407896..2481b3f5b2d44dd8ff1620903c5b80b024a4d8f9 100644 (file)
@@ -66,6 +66,12 @@ class BSMarkdown
                'noscript'
        );
        
+       /**
+       * Map of link IDs to their link
+       * @var string
+       */
+       private $linkMap = array();
+       
        // ###################################################################
        /**
        * Quick parsing function that uses the system defaults for parsing.
@@ -108,6 +114,7 @@ class BSMarkdown
                $this->text = preg_replace('/^\s*?$/m', '', $this->text);
                
                $this->_extractHtmlBlocks();
+               $this->_extractLinkMap();
                
                $this->_convertHardLineBreaks();
                $this->_convertAtxHeaders();
@@ -174,6 +181,26 @@ class BSMarkdown
                return str_replace(array_keys($this->htmlBlockMap), array_values($this->htmlBlockMap), $text);
        }
        
+       // ###################################################################
+       /**
+       * Extracts all links in the "[id]: link" form
+       */
+       public function _extractLinkMap()
+       {
+               $this->text = preg_replace_callback('/\[(\w+)\]:\s*<?(\S+)>?(\s*(("|\')|\()(.*?)(\4|\)))?\n?/', array(&$this, '_extractLinkMapCallback'), $this->text);
+       }
+       
+       // ###################################################################
+       /**
+       * Converts extracted link definitions into the map
+       *
+       * @param        array   Matches array
+       */
+       private function _extractLinkMapCallback($matches)
+       {
+               $this->linkMap[$matches[1]] = array($matches[2], $matches[6]);
+       }
+       
        // ###################################################################
        /**
        * Converts text surrounded by #sings to headers (## Heading 2)
@@ -191,7 +218,7 @@ class BSMarkdown
        */
        private function _convertAtxHeadersCallback($matches)
        {
-               var_dump($matches);
+               // var_dump($matches);
                $html = '<h' . strlen($matches[1]) . '>' . $this->_expandHtmlBlocks($matches[2]) . '</h' . strlen($matches[1]) . '>';
                $hash = md5($html . microtime());
                $this->htmlBlockMap[$hash] = $html;