Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MarkdownExtra_Parser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MarkdownExtra_Parser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
1703 | class MarkdownExtra_Parser extends Markdown_Parser { |
||
1704 | |||
1705 | ### Configuration Variables ### |
||
1706 | |||
1707 | # Prefix for footnote ids. |
||
1708 | var $fn_id_prefix = ""; |
||
1709 | |||
1710 | # Optional title attribute for footnote links and backlinks. |
||
1711 | var $fn_link_title = MARKDOWN_FN_LINK_TITLE; |
||
1712 | var $fn_backlink_title = MARKDOWN_FN_BACKLINK_TITLE; |
||
1713 | |||
1714 | # Optional class attribute for footnote links and backlinks. |
||
1715 | var $fn_link_class = MARKDOWN_FN_LINK_CLASS; |
||
1716 | var $fn_backlink_class = MARKDOWN_FN_BACKLINK_CLASS; |
||
1717 | |||
1718 | # Optional class prefix for fenced code block. |
||
1719 | var $code_class_prefix = MARKDOWN_CODE_CLASS_PREFIX; |
||
1720 | # Class attribute for code blocks goes on the `code` tag; |
||
1721 | # setting this to true will put attributes on the `pre` tag instead. |
||
1722 | var $code_attr_on_pre = MARKDOWN_CODE_ATTR_ON_PRE; |
||
1723 | |||
1724 | # Predefined abbreviations. |
||
1725 | var $predef_abbr = array(); |
||
1726 | |||
1727 | |||
1728 | ### Parser Implementation ### |
||
1729 | |||
1730 | function MarkdownExtra_Parser() { |
||
1731 | # |
||
1732 | # Constructor function. Initialize the parser object. |
||
1733 | # |
||
1734 | # Add extra escapable characters before parent constructor |
||
1735 | # initialize the table. |
||
1736 | $this->escape_chars .= ':|'; |
||
1737 | |||
1738 | # Insert extra document, block, and span transformations. |
||
1739 | # Parent constructor will do the sorting. |
||
1740 | $this->document_gamut += array( |
||
1741 | "doFencedCodeBlocks" => 5, |
||
1742 | "stripFootnotes" => 15, |
||
1743 | "stripAbbreviations" => 25, |
||
1744 | "appendFootnotes" => 50, |
||
1745 | ); |
||
1746 | $this->block_gamut += array( |
||
1747 | "doFencedCodeBlocks" => 5, |
||
1748 | "doTables" => 15, |
||
1749 | "doDefLists" => 45, |
||
1750 | ); |
||
1751 | $this->span_gamut += array( |
||
1752 | "doFootnotes" => 5, |
||
1753 | "doAbbreviations" => 70, |
||
1754 | ); |
||
1755 | |||
1756 | parent::Markdown_Parser(); |
||
1757 | } |
||
1758 | |||
1759 | |||
1760 | # Extra variables used during extra transformations. |
||
1761 | var $footnotes = array(); |
||
1762 | var $footnotes_ordered = array(); |
||
1763 | var $footnotes_ref_count = array(); |
||
1764 | var $footnotes_numbers = array(); |
||
1765 | var $abbr_desciptions = array(); |
||
1766 | var $abbr_word_re = ''; |
||
1767 | |||
1768 | # Give the current footnote number. |
||
1769 | var $footnote_counter = 1; |
||
1770 | |||
1771 | |||
1772 | function setup() { |
||
1773 | # |
||
1774 | # Setting up Extra-specific variables. |
||
1775 | # |
||
1776 | parent::setup(); |
||
1777 | |||
1778 | $this->footnotes = array(); |
||
1779 | $this->footnotes_ordered = array(); |
||
1780 | $this->footnotes_ref_count = array(); |
||
1781 | $this->footnotes_numbers = array(); |
||
1782 | $this->abbr_desciptions = array(); |
||
1783 | $this->abbr_word_re = ''; |
||
1784 | $this->footnote_counter = 1; |
||
1785 | |||
1786 | foreach ($this->predef_abbr as $abbr_word => $abbr_desc) { |
||
1787 | if ($this->abbr_word_re) |
||
1788 | $this->abbr_word_re .= '|'; |
||
1789 | $this->abbr_word_re .= preg_quote($abbr_word); |
||
1790 | $this->abbr_desciptions[$abbr_word] = trim($abbr_desc); |
||
1791 | } |
||
1792 | } |
||
1793 | |||
1794 | function teardown() { |
||
1795 | # |
||
1796 | # Clearing Extra-specific variables. |
||
1797 | # |
||
1798 | $this->footnotes = array(); |
||
1799 | $this->footnotes_ordered = array(); |
||
1800 | $this->footnotes_ref_count = array(); |
||
1801 | $this->footnotes_numbers = array(); |
||
1802 | $this->abbr_desciptions = array(); |
||
1803 | $this->abbr_word_re = ''; |
||
1804 | |||
1805 | parent::teardown(); |
||
1806 | } |
||
1807 | |||
1808 | |||
1809 | ### Extra Attribute Parser ### |
||
1810 | |||
1811 | # Expression to use to catch attributes (includes the braces) |
||
1812 | var $id_class_attr_catch_re = '\{((?:[ ]*[#.][-_:a-zA-Z0-9]+){1,})[ ]*\}'; |
||
1813 | # Expression to use when parsing in a context when no capture is desired |
||
1814 | var $id_class_attr_nocatch_re = '\{(?:[ ]*[#.][-_:a-zA-Z0-9]+){1,}[ ]*\}'; |
||
1815 | |||
1816 | function doExtraAttributes($tag_name, $attr) { |
||
1817 | # |
||
1818 | # Parse attributes caught by the $this->id_class_attr_catch_re expression |
||
1819 | # and return the HTML-formatted list of attributes. |
||
1820 | # |
||
1821 | # Currently supported attributes are .class and #id. |
||
1822 | # |
||
1823 | if (empty($attr)) return ""; |
||
1824 | |||
1825 | # Split on components |
||
1826 | preg_match_all('/[#.][-_:a-zA-Z0-9]+/', $attr, $matches); |
||
1827 | $elements = $matches[0]; |
||
1828 | |||
1829 | # handle classes and ids (only first id taken into account) |
||
1830 | $classes = array(); |
||
1831 | $id = false; |
||
1832 | foreach ($elements as $element) { |
||
1833 | if ($element{0} == '.') { |
||
1834 | $classes[] = substr($element, 1); |
||
1835 | } else if ($element{0} == '#') { |
||
1836 | if ($id === false) $id = substr($element, 1); |
||
1837 | } |
||
1838 | } |
||
1839 | |||
1840 | # compose attributes as string |
||
1841 | $attr_str = ""; |
||
1842 | if (!empty($id)) { |
||
1843 | $attr_str .= ' id="'.$id.'"'; |
||
1844 | } |
||
1845 | if (!empty($classes)) { |
||
1846 | $attr_str .= ' class="'.implode(" ", $classes).'"'; |
||
1847 | } |
||
1848 | return $attr_str; |
||
1849 | } |
||
1850 | |||
1851 | |||
1852 | function stripLinkDefinitions($text) { |
||
1853 | # |
||
1854 | # Strips link definitions from text, stores the URLs and titles in |
||
1855 | # hash references. |
||
1856 | # |
||
1857 | $less_than_tab = $this->tab_width - 1; |
||
1858 | |||
1859 | # Link defs are in the form: ^[id]: url "optional title" |
||
1860 | $text = preg_replace_callback('{ |
||
1861 | ^[ ]{0,'.$less_than_tab.'}\[(.+)\][ ]?: # id = $1 |
||
1862 | [ ]* |
||
1863 | \n? # maybe *one* newline |
||
1864 | [ ]* |
||
1865 | (?: |
||
1866 | <(.+?)> # url = $2 |
||
1867 | | |
||
1868 | (\S+?) # url = $3 |
||
1869 | ) |
||
1870 | [ ]* |
||
1871 | \n? # maybe one newline |
||
1872 | [ ]* |
||
1873 | (?: |
||
1874 | (?<=\s) # lookbehind for whitespace |
||
1875 | ["(] |
||
1876 | (.*?) # title = $4 |
||
1877 | [")] |
||
1878 | [ ]* |
||
1879 | )? # title is optional |
||
1880 | (?:[ ]* '.$this->id_class_attr_catch_re.' )? # $5 = extra id & class attr |
||
1881 | (?:\n+|\Z) |
||
1882 | }xm', |
||
1883 | array(&$this, '_stripLinkDefinitions_callback'), |
||
1884 | $text); |
||
1885 | return $text; |
||
1886 | } |
||
1887 | function _stripLinkDefinitions_callback($matches) { |
||
1888 | $link_id = strtolower($matches[1]); |
||
1889 | $url = $matches[2] == '' ? $matches[3] : $matches[2]; |
||
1890 | $this->urls[$link_id] = $url; |
||
1891 | $this->titles[$link_id] =& $matches[4]; |
||
1892 | $this->ref_attr[$link_id] = $this->doExtraAttributes("", $dummy =& $matches[5]); |
||
1893 | return ''; # String that will replace the block |
||
1894 | } |
||
1895 | |||
1896 | |||
1897 | ### HTML Block Parser ### |
||
1898 | |||
1899 | # Tags that are always treated as block tags: |
||
1900 | var $block_tags_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend|article|section|nav|aside|hgroup|header|footer|figcaption'; |
||
1901 | |||
1902 | # Tags treated as block tags only if the opening tag is alone on its line: |
||
1903 | var $context_block_tags_re = 'script|noscript|ins|del|iframe|object|source|track|param|math|svg|canvas|audio|video'; |
||
1904 | |||
1905 | # Tags where markdown="1" default to span mode: |
||
1906 | var $contain_span_tags_re = 'p|h[1-6]|li|dd|dt|td|th|legend|address'; |
||
1907 | |||
1908 | # Tags which must not have their contents modified, no matter where |
||
1909 | # they appear: |
||
1910 | var $clean_tags_re = 'script|math|svg'; |
||
1911 | |||
1912 | # Tags that do not need to be closed. |
||
1913 | var $auto_close_tags_re = 'hr|img|param|source|track'; |
||
1914 | |||
1915 | |||
1916 | function hashHTMLBlocks($text) { |
||
1917 | # |
||
1918 | # Hashify HTML Blocks and "clean tags". |
||
1919 | # |
||
1920 | # We only want to do this for block-level HTML tags, such as headers, |
||
1921 | # lists, and tables. That's because we still want to wrap <p>s around |
||
1922 | # "paragraphs" that are wrapped in non-block-level tags, such as anchors, |
||
1923 | # phrase emphasis, and spans. The list of tags we're looking for is |
||
1924 | # hard-coded. |
||
1925 | # |
||
1926 | # This works by calling _HashHTMLBlocks_InMarkdown, which then calls |
||
1927 | # _HashHTMLBlocks_InHTML when it encounter block tags. When the markdown="1" |
||
1928 | # attribute is found within a tag, _HashHTMLBlocks_InHTML calls back |
||
1929 | # _HashHTMLBlocks_InMarkdown to handle the Markdown syntax within the tag. |
||
1930 | # These two functions are calling each other. It's recursive! |
||
1931 | # |
||
1932 | if ($this->no_markup) return $text; |
||
1933 | |||
1934 | # |
||
1935 | # Call the HTML-in-Markdown hasher. |
||
1936 | # |
||
1937 | list($text, ) = $this->_hashHTMLBlocks_inMarkdown($text); |
||
1938 | |||
1939 | return $text; |
||
1940 | } |
||
1941 | function _hashHTMLBlocks_inMarkdown($text, $indent = 0, |
||
1942 | $enclosing_tag_re = '', $span = false) |
||
1943 | { |
||
1944 | # |
||
1945 | # Parse markdown text, calling _HashHTMLBlocks_InHTML for block tags. |
||
1946 | # |
||
1947 | # * $indent is the number of space to be ignored when checking for code |
||
1948 | # blocks. This is important because if we don't take the indent into |
||
1949 | # account, something like this (which looks right) won't work as expected: |
||
1950 | # |
||
1951 | # <div> |
||
1952 | # <div markdown="1"> |
||
1953 | # Hello World. <-- Is this a Markdown code block or text? |
||
1954 | # </div> <-- Is this a Markdown code block or a real tag? |
||
1955 | # <div> |
||
1956 | # |
||
1957 | # If you don't like this, just don't indent the tag on which |
||
1958 | # you apply the markdown="1" attribute. |
||
1959 | # |
||
1960 | # * If $enclosing_tag_re is not empty, stops at the first unmatched closing |
||
1961 | # tag with that name. Nested tags supported. |
||
1962 | # |
||
1963 | # * If $span is true, text inside must treated as span. So any double |
||
1964 | # newline will be replaced by a single newline so that it does not create |
||
1965 | # paragraphs. |
||
1966 | # |
||
1967 | # Returns an array of that form: ( processed text , remaining text ) |
||
1968 | # |
||
1969 | if ($text === '') return array('', ''); |
||
1970 | |||
1971 | # Regex to check for the presense of newlines around a block tag. |
||
1972 | $newline_before_re = '/(?:^\n?|\n\n)*$/'; |
||
1973 | $newline_after_re = |
||
1974 | '{ |
||
1975 | ^ # Start of text following the tag. |
||
1976 | (?>[ ]*<!--.*?-->)? # Optional comment. |
||
1977 | [ ]*\n # Must be followed by newline. |
||
1978 | }xs'; |
||
1979 | |||
1980 | # Regex to match any tag. |
||
1981 | $block_tag_re = |
||
1982 | '{ |
||
1983 | ( # $2: Capture whole tag. |
||
1984 | </? # Any opening or closing tag. |
||
1985 | (?> # Tag name. |
||
1986 | '.$this->block_tags_re.' | |
||
1987 | '.$this->context_block_tags_re.' | |
||
1988 | '.$this->clean_tags_re.' | |
||
1989 | (?!\s)'.$enclosing_tag_re.' |
||
1990 | ) |
||
1991 | (?: |
||
1992 | (?=[\s"\'/a-zA-Z0-9]) # Allowed characters after tag name. |
||
1993 | (?> |
||
1994 | ".*?" | # Double quotes (can contain `>`) |
||
1995 | \'.*?\' | # Single quotes (can contain `>`) |
||
1996 | .+? # Anything but quotes and `>`. |
||
1997 | )*? |
||
1998 | )? |
||
1999 | > # End of tag. |
||
2000 | | |
||
2001 | <!-- .*? --> # HTML Comment |
||
2002 | | |
||
2003 | <\?.*?\?> | <%.*?%> # Processing instruction |
||
2004 | | |
||
2005 | <!\[CDATA\[.*?\]\]> # CData Block |
||
2006 | '. ( !$span ? ' # If not in span. |
||
2007 | | |
||
2008 | # Indented code block |
||
2009 | (?: ^[ ]*\n | ^ | \n[ ]*\n ) |
||
2010 | [ ]{'.($indent+4).'}[^\n]* \n |
||
2011 | (?> |
||
2012 | (?: [ ]{'.($indent+4).'}[^\n]* | [ ]* ) \n |
||
2013 | )* |
||
2014 | | |
||
2015 | # Fenced code block marker |
||
2016 | (?<= ^ | \n ) |
||
2017 | [ ]{0,'.($indent+3).'}(?:~{3,}|`{3,}) |
||
2018 | [ ]* |
||
2019 | (?: |
||
2020 | \.?[-_:a-zA-Z0-9]+ # standalone class name |
||
2021 | | |
||
2022 | '.$this->id_class_attr_nocatch_re.' # extra attributes |
||
2023 | )? |
||
2024 | [ ]* |
||
2025 | (?= \n ) |
||
2026 | ' : '' ). ' # End (if not is span). |
||
2027 | | |
||
2028 | # Code span marker |
||
2029 | # Note, this regex needs to go after backtick fenced |
||
2030 | # code blocks but it should also be kept outside of the |
||
2031 | # "if not in span" condition adding backticks to the parser |
||
2032 | `+ |
||
2033 | ) |
||
2034 | }xs'; |
||
2035 | |||
2036 | |||
2037 | $depth = 0; # Current depth inside the tag tree. |
||
2038 | $parsed = ""; # Parsed text that will be returned. |
||
2039 | |||
2040 | # |
||
2041 | # Loop through every tag until we find the closing tag of the parent |
||
2042 | # or loop until reaching the end of text if no parent tag specified. |
||
2043 | # |
||
2044 | do { |
||
2045 | # |
||
2046 | # Split the text using the first $tag_match pattern found. |
||
2047 | # Text before pattern will be first in the array, text after |
||
2048 | # pattern will be at the end, and between will be any catches made |
||
2049 | # by the pattern. |
||
2050 | # |
||
2051 | $parts = preg_split($block_tag_re, $text, 2, |
||
2052 | PREG_SPLIT_DELIM_CAPTURE); |
||
2053 | |||
2054 | # If in Markdown span mode, add a empty-string span-level hash |
||
2055 | # after each newline to prevent triggering any block element. |
||
2056 | if ($span) { |
||
2057 | $void = $this->hashPart("", ':'); |
||
2058 | $newline = "$void\n"; |
||
2059 | $parts[0] = $void . str_replace("\n", $newline, $parts[0]) . $void; |
||
2060 | } |
||
2061 | |||
2062 | $parsed .= $parts[0]; # Text before current tag. |
||
2063 | |||
2064 | # If end of $text has been reached. Stop loop. |
||
2065 | if (count($parts) < 3) { |
||
2066 | $text = ""; |
||
2067 | break; |
||
2068 | } |
||
2069 | |||
2070 | $tag = $parts[1]; # Tag to handle. |
||
2071 | $text = $parts[2]; # Remaining text after current tag. |
||
2072 | $tag_re = preg_quote($tag); # For use in a regular expression. |
||
2073 | |||
2074 | # |
||
2075 | # Check for: Fenced code block marker. |
||
2076 | # Note: need to recheck the whole tag to disambiguate backtick |
||
2077 | # fences from code spans |
||
2078 | # |
||
2079 | if (preg_match('{^\n?([ ]{0,'.($indent+3).'})(~{3,}|`{3,})[ ]*(?:\.?[-_:a-zA-Z0-9]+|'.$this->id_class_attr_nocatch_re.')?[ ]*\n?$}', $tag, $capture)) { |
||
2080 | # Fenced code block marker: find matching end marker. |
||
2081 | $fence_indent = strlen($capture[1]); # use captured indent in re |
||
2082 | $fence_re = $capture[2]; # use captured fence in re |
||
2083 | if (preg_match('{^(?>.*\n)*?[ ]{'.($fence_indent).'}'.$fence_re.'[ ]*(?:\n|$)}', $text, |
||
2084 | $matches)) |
||
2085 | { |
||
2086 | # End marker found: pass text unchanged until marker. |
||
2087 | $parsed .= $tag . $matches[0]; |
||
2088 | $text = substr($text, strlen($matches[0])); |
||
2089 | } |
||
2090 | else { |
||
2091 | # No end marker: just skip it. |
||
2092 | $parsed .= $tag; |
||
2093 | } |
||
2094 | } |
||
2095 | # |
||
2096 | # Check for: Indented code block. |
||
2097 | # |
||
2098 | else if ($tag{0} == "\n" || $tag{0} == " ") { |
||
2099 | # Indented code block: pass it unchanged, will be handled |
||
2100 | # later. |
||
2101 | $parsed .= $tag; |
||
2102 | } |
||
2103 | # |
||
2104 | # Check for: Code span marker |
||
2105 | # Note: need to check this after backtick fenced code blocks |
||
2106 | # |
||
2107 | else if ($tag{0} == "`") { |
||
2108 | # Find corresponding end marker. |
||
2109 | $tag_re = preg_quote($tag); |
||
2110 | if (preg_match('{^(?>.+?|\n(?!\n))*?(?<!`)'.$tag_re.'(?!`)}', |
||
2111 | $text, $matches)) |
||
2112 | { |
||
2113 | # End marker found: pass text unchanged until marker. |
||
2114 | $parsed .= $tag . $matches[0]; |
||
2115 | $text = substr($text, strlen($matches[0])); |
||
2116 | } |
||
2117 | else { |
||
2118 | # Unmatched marker: just skip it. |
||
2119 | $parsed .= $tag; |
||
2120 | } |
||
2121 | } |
||
2122 | # |
||
2123 | # Check for: Opening Block level tag or |
||
2124 | # Opening Context Block tag (like ins and del) |
||
2125 | # used as a block tag (tag is alone on it's line). |
||
2126 | # |
||
2127 | else if (preg_match('{^<(?:'.$this->block_tags_re.')\b}', $tag) || |
||
2128 | ( preg_match('{^<(?:'.$this->context_block_tags_re.')\b}', $tag) && |
||
2129 | preg_match($newline_before_re, $parsed) && |
||
2130 | preg_match($newline_after_re, $text) ) |
||
2131 | ) |
||
2132 | { |
||
2133 | # Need to parse tag and following text using the HTML parser. |
||
2134 | list($block_text, $text) = |
||
2135 | $this->_hashHTMLBlocks_inHTML($tag . $text, "hashBlock", true); |
||
2136 | |||
2137 | # Make sure it stays outside of any paragraph by adding newlines. |
||
2138 | $parsed .= "\n\n$block_text\n\n"; |
||
2139 | } |
||
2140 | # |
||
2141 | # Check for: Clean tag (like script, math) |
||
2142 | # HTML Comments, processing instructions. |
||
2143 | # |
||
2144 | else if (preg_match('{^<(?:'.$this->clean_tags_re.')\b}', $tag) || |
||
2145 | $tag{1} == '!' || $tag{1} == '?') |
||
2146 | { |
||
2147 | # Need to parse tag and following text using the HTML parser. |
||
2148 | # (don't check for markdown attribute) |
||
2149 | list($block_text, $text) = |
||
2150 | $this->_hashHTMLBlocks_inHTML($tag . $text, "hashClean", false); |
||
2151 | |||
2152 | $parsed .= $block_text; |
||
2153 | } |
||
2154 | # |
||
2155 | # Check for: Tag with same name as enclosing tag. |
||
2156 | # |
||
2157 | else if ($enclosing_tag_re !== '' && |
||
2158 | # Same name as enclosing tag. |
||
2159 | preg_match('{^</?(?:'.$enclosing_tag_re.')\b}', $tag)) |
||
2160 | { |
||
2161 | # |
||
2162 | # Increase/decrease nested tag count. |
||
2163 | # |
||
2164 | if ($tag{1} == '/') $depth--; |
||
2165 | else if ($tag{strlen($tag)-2} != '/') $depth++; |
||
2166 | |||
2167 | if ($depth < 0) { |
||
2168 | # |
||
2169 | # Going out of parent element. Clean up and break so we |
||
2170 | # return to the calling function. |
||
2171 | # |
||
2172 | $text = $tag . $text; |
||
2173 | break; |
||
2174 | } |
||
2175 | |||
2176 | $parsed .= $tag; |
||
2177 | } |
||
2178 | else { |
||
2179 | $parsed .= $tag; |
||
2180 | } |
||
2181 | } while ($depth >= 0); |
||
2182 | |||
2183 | return array($parsed, $text); |
||
2184 | } |
||
2185 | function _hashHTMLBlocks_inHTML($text, $hash_method, $md_attr) { |
||
2186 | # |
||
2187 | # Parse HTML, calling _HashHTMLBlocks_InMarkdown for block tags. |
||
2188 | # |
||
2189 | # * Calls $hash_method to convert any blocks. |
||
2190 | # * Stops when the first opening tag closes. |
||
2191 | # * $md_attr indicate if the use of the `markdown="1"` attribute is allowed. |
||
2192 | # (it is not inside clean tags) |
||
2193 | # |
||
2194 | # Returns an array of that form: ( processed text , remaining text ) |
||
2195 | # |
||
2196 | if ($text === '') return array('', ''); |
||
2197 | |||
2198 | # Regex to match `markdown` attribute inside of a tag. |
||
2199 | $markdown_attr_re = ' |
||
2200 | { |
||
2201 | \s* # Eat whitespace before the `markdown` attribute |
||
2202 | markdown |
||
2203 | \s*=\s* |
||
2204 | (?> |
||
2205 | (["\']) # $1: quote delimiter |
||
2206 | (.*?) # $2: attribute value |
||
2207 | \1 # matching delimiter |
||
2208 | | |
||
2209 | ([^\s>]*) # $3: unquoted attribute value |
||
2210 | ) |
||
2211 | () # $4: make $3 always defined (avoid warnings) |
||
2212 | }xs'; |
||
2213 | |||
2214 | # Regex to match any tag. |
||
2215 | $tag_re = '{ |
||
2216 | ( # $2: Capture whole tag. |
||
2217 | </? # Any opening or closing tag. |
||
2218 | [\w:$]+ # Tag name. |
||
2219 | (?: |
||
2220 | (?=[\s"\'/a-zA-Z0-9]) # Allowed characters after tag name. |
||
2221 | (?> |
||
2222 | ".*?" | # Double quotes (can contain `>`) |
||
2223 | \'.*?\' | # Single quotes (can contain `>`) |
||
2224 | .+? # Anything but quotes and `>`. |
||
2225 | )*? |
||
2226 | )? |
||
2227 | > # End of tag. |
||
2228 | | |
||
2229 | <!-- .*? --> # HTML Comment |
||
2230 | | |
||
2231 | <\?.*?\?> | <%.*?%> # Processing instruction |
||
2232 | | |
||
2233 | <!\[CDATA\[.*?\]\]> # CData Block |
||
2234 | ) |
||
2235 | }xs'; |
||
2236 | |||
2237 | $original_text = $text; # Save original text in case of faliure. |
||
2238 | |||
2239 | $depth = 0; # Current depth inside the tag tree. |
||
2240 | $block_text = ""; # Temporary text holder for current text. |
||
2241 | $parsed = ""; # Parsed text that will be returned. |
||
2242 | |||
2243 | # |
||
2244 | # Get the name of the starting tag. |
||
2245 | # (This pattern makes $base_tag_name_re safe without quoting.) |
||
2246 | # |
||
2247 | if (preg_match('/^<([\w:$]*)\b/', $text, $matches)) |
||
2248 | $base_tag_name_re = $matches[1]; |
||
2249 | |||
2250 | # |
||
2251 | # Loop through every tag until we find the corresponding closing tag. |
||
2252 | # |
||
2253 | do { |
||
2254 | # |
||
2255 | # Split the text using the first $tag_match pattern found. |
||
2256 | # Text before pattern will be first in the array, text after |
||
2257 | # pattern will be at the end, and between will be any catches made |
||
2258 | # by the pattern. |
||
2259 | # |
||
2260 | $parts = preg_split($tag_re, $text, 2, PREG_SPLIT_DELIM_CAPTURE); |
||
2261 | |||
2262 | if (count($parts) < 3) { |
||
2263 | # |
||
2264 | # End of $text reached with unbalenced tag(s). |
||
2265 | # In that case, we return original text unchanged and pass the |
||
2266 | # first character as filtered to prevent an infinite loop in the |
||
2267 | # parent function. |
||
2268 | # |
||
2269 | return array($original_text{0}, substr($original_text, 1)); |
||
2270 | } |
||
2271 | |||
2272 | $block_text .= $parts[0]; # Text before current tag. |
||
2273 | $tag = $parts[1]; # Tag to handle. |
||
2274 | $text = $parts[2]; # Remaining text after current tag. |
||
2275 | |||
2276 | # |
||
2277 | # Check for: Auto-close tag (like <hr/>) |
||
2278 | # Comments and Processing Instructions. |
||
2279 | # |
||
2280 | if (preg_match('{^</?(?:'.$this->auto_close_tags_re.')\b}', $tag) || |
||
2281 | $tag{1} == '!' || $tag{1} == '?') |
||
2282 | { |
||
2283 | # Just add the tag to the block as if it was text. |
||
2284 | $block_text .= $tag; |
||
2285 | } |
||
2286 | else { |
||
2287 | # |
||
2288 | # Increase/decrease nested tag count. Only do so if |
||
2289 | # the tag's name match base tag's. |
||
2290 | # |
||
2291 | if (preg_match('{^</?'.$base_tag_name_re.'\b}', $tag)) { |
||
2292 | if ($tag{1} == '/') $depth--; |
||
2293 | else if ($tag{strlen($tag)-2} != '/') $depth++; |
||
2294 | } |
||
2295 | |||
2296 | # |
||
2297 | # Check for `markdown="1"` attribute and handle it. |
||
2298 | # |
||
2299 | if ($md_attr && |
||
2300 | preg_match($markdown_attr_re, $tag, $attr_m) && |
||
2301 | preg_match('/^1|block|span$/', $attr_m[2] . $attr_m[3])) |
||
2302 | { |
||
2303 | # Remove `markdown` attribute from opening tag. |
||
2304 | $tag = preg_replace($markdown_attr_re, '', $tag); |
||
2305 | |||
2306 | # Check if text inside this tag must be parsed in span mode. |
||
2307 | $this->mode = $attr_m[2] . $attr_m[3]; |
||
2308 | $span_mode = $this->mode == 'span' || $this->mode != 'block' && |
||
2309 | preg_match('{^<(?:'.$this->contain_span_tags_re.')\b}', $tag); |
||
2310 | |||
2311 | # Calculate indent before tag. |
||
2312 | if (preg_match('/(?:^|\n)( *?)(?! ).*?$/', $block_text, $matches)) { |
||
2313 | $strlen = $this->utf8_strlen; |
||
2314 | $indent = $strlen($matches[1], 'UTF-8'); |
||
2315 | } else { |
||
2316 | $indent = 0; |
||
2317 | } |
||
2318 | |||
2319 | # End preceding block with this tag. |
||
2320 | $block_text .= $tag; |
||
2321 | $parsed .= $this->$hash_method($block_text); |
||
2322 | |||
2323 | # Get enclosing tag name for the ParseMarkdown function. |
||
2324 | # (This pattern makes $tag_name_re safe without quoting.) |
||
2325 | preg_match('/^<([\w:$]*)\b/', $tag, $matches); |
||
2326 | $tag_name_re = $matches[1]; |
||
2327 | |||
2328 | # Parse the content using the HTML-in-Markdown parser. |
||
2329 | list ($block_text, $text) |
||
2330 | = $this->_hashHTMLBlocks_inMarkdown($text, $indent, |
||
2331 | $tag_name_re, $span_mode); |
||
2332 | |||
2333 | # Outdent markdown text. |
||
2334 | if ($indent > 0) { |
||
2335 | $block_text = preg_replace("/^[ ]{1,$indent}/m", "", |
||
2336 | $block_text); |
||
2337 | } |
||
2338 | |||
2339 | # Append tag content to parsed text. |
||
2340 | if (!$span_mode) $parsed .= "\n\n$block_text\n\n"; |
||
2341 | else $parsed .= "$block_text"; |
||
2342 | |||
2343 | # Start over with a new block. |
||
2344 | $block_text = ""; |
||
2345 | } |
||
2346 | else $block_text .= $tag; |
||
2347 | } |
||
2348 | |||
2349 | } while ($depth > 0); |
||
2350 | |||
2351 | # |
||
2352 | # Hash last block text that wasn't processed inside the loop. |
||
2353 | # |
||
2354 | $parsed .= $this->$hash_method($block_text); |
||
2355 | |||
2356 | return array($parsed, $text); |
||
2357 | } |
||
2358 | |||
2359 | |||
2360 | function hashClean($text) { |
||
2361 | # |
||
2362 | # Called whenever a tag must be hashed when a function inserts a "clean" tag |
||
2363 | # in $text, it passes through this function and is automaticaly escaped, |
||
2364 | # blocking invalid nested overlap. |
||
2365 | # |
||
2366 | return $this->hashPart($text, 'C'); |
||
2367 | } |
||
2368 | |||
2369 | |||
2370 | function doAnchors($text) { |
||
2371 | # |
||
2372 | # Turn Markdown link shortcuts into XHTML <a> tags. |
||
2373 | # |
||
2374 | if ($this->in_anchor) return $text; |
||
2375 | $this->in_anchor = true; |
||
2376 | |||
2377 | # |
||
2378 | # First, handle reference-style links: [link text] [id] |
||
2379 | # |
||
2380 | $text = preg_replace_callback('{ |
||
2381 | ( # wrap whole match in $1 |
||
2382 | \[ |
||
2383 | ('.$this->nested_brackets_re.') # link text = $2 |
||
2384 | \] |
||
2385 | |||
2386 | [ ]? # one optional space |
||
2387 | (?:\n[ ]*)? # one optional newline followed by spaces |
||
2388 | |||
2389 | \[ |
||
2390 | (.*?) # id = $3 |
||
2391 | \] |
||
2392 | ) |
||
2393 | }xs', |
||
2394 | array(&$this, '_doAnchors_reference_callback'), $text); |
||
2395 | |||
2396 | # |
||
2397 | # Next, inline-style links: [link text](url "optional title") |
||
2398 | # |
||
2399 | $text = preg_replace_callback('{ |
||
2400 | ( # wrap whole match in $1 |
||
2401 | \[ |
||
2402 | ('.$this->nested_brackets_re.') # link text = $2 |
||
2403 | \] |
||
2404 | \( # literal paren |
||
2405 | [ \n]* |
||
2406 | (?: |
||
2407 | <(.+?)> # href = $3 |
||
2408 | | |
||
2409 | ('.$this->nested_url_parenthesis_re.') # href = $4 |
||
2410 | ) |
||
2411 | [ \n]* |
||
2412 | ( # $5 |
||
2413 | ([\'"]) # quote char = $6 |
||
2414 | (.*?) # Title = $7 |
||
2415 | \6 # matching quote |
||
2416 | [ \n]* # ignore any spaces/tabs between closing quote and ) |
||
2417 | )? # title is optional |
||
2418 | \) |
||
2419 | (?:[ ]? '.$this->id_class_attr_catch_re.' )? # $8 = id/class attributes |
||
2420 | ) |
||
2421 | }xs', |
||
2422 | array(&$this, '_doAnchors_inline_callback'), $text); |
||
2423 | |||
2424 | # |
||
2425 | # Last, handle reference-style shortcuts: [link text] |
||
2426 | # These must come last in case you've also got [link text][1] |
||
2427 | # or [link text](/foo) |
||
2428 | # |
||
2429 | $text = preg_replace_callback('{ |
||
2430 | ( # wrap whole match in $1 |
||
2431 | \[ |
||
2432 | ([^\[\]]+) # link text = $2; can\'t contain [ or ] |
||
2433 | \] |
||
2434 | ) |
||
2435 | }xs', |
||
2436 | array(&$this, '_doAnchors_reference_callback'), $text); |
||
2437 | |||
2438 | $this->in_anchor = false; |
||
2439 | return $text; |
||
2440 | } |
||
2441 | function _doAnchors_reference_callback($matches) { |
||
2442 | $whole_match = $matches[1]; |
||
2443 | $link_text = $matches[2]; |
||
2444 | $link_id =& $matches[3]; |
||
2445 | |||
2446 | if ($link_id == "") { |
||
2447 | # for shortcut links like [this][] or [this]. |
||
2448 | $link_id = $link_text; |
||
2449 | } |
||
2450 | |||
2451 | # lower-case and turn embedded newlines into spaces |
||
2452 | $link_id = strtolower($link_id); |
||
2453 | $link_id = preg_replace('{[ ]?\n}', ' ', $link_id); |
||
2454 | |||
2455 | if (isset($this->urls[$link_id])) { |
||
2456 | $url = $this->urls[$link_id]; |
||
2457 | $url = $this->encodeAttribute($url); |
||
2458 | |||
2459 | $result = "<a href=\"$url\""; |
||
2460 | if ( isset( $this->titles[$link_id] ) ) { |
||
2461 | $title = $this->titles[$link_id]; |
||
2462 | $title = $this->encodeAttribute($title); |
||
2463 | $result .= " title=\"$title\""; |
||
2464 | } |
||
2465 | if (isset($this->ref_attr[$link_id])) |
||
2466 | $result .= $this->ref_attr[$link_id]; |
||
2467 | |||
2468 | $link_text = $this->runSpanGamut($link_text); |
||
2469 | $result .= ">$link_text</a>"; |
||
2470 | $result = $this->hashPart($result); |
||
2471 | } |
||
2472 | else { |
||
2473 | $result = $whole_match; |
||
2474 | } |
||
2475 | return $result; |
||
2476 | } |
||
2477 | function _doAnchors_inline_callback($matches) { |
||
2478 | $whole_match = $matches[1]; |
||
2479 | $link_text = $this->runSpanGamut($matches[2]); |
||
2480 | $url = $matches[3] == '' ? $matches[4] : $matches[3]; |
||
2481 | $title =& $matches[7]; |
||
2482 | $attr = $this->doExtraAttributes("a", $dummy =& $matches[8]); |
||
2483 | |||
2484 | |||
2485 | $url = $this->encodeAttribute($url); |
||
2486 | |||
2487 | $result = "<a href=\"$url\""; |
||
2488 | if (isset($title)) { |
||
2489 | $title = $this->encodeAttribute($title); |
||
2490 | $result .= " title=\"$title\""; |
||
2491 | } |
||
2492 | $result .= $attr; |
||
2493 | |||
2494 | $link_text = $this->runSpanGamut($link_text); |
||
2495 | $result .= ">$link_text</a>"; |
||
2496 | |||
2497 | return $this->hashPart($result); |
||
2498 | } |
||
2499 | |||
2500 | |||
2501 | function doImages($text) { |
||
2502 | # |
||
2503 | # Turn Markdown image shortcuts into <img> tags. |
||
2504 | # |
||
2505 | # |
||
2506 | # First, handle reference-style labeled images: ![alt text][id] |
||
2507 | # |
||
2508 | $text = preg_replace_callback('{ |
||
2509 | ( # wrap whole match in $1 |
||
2510 | !\[ |
||
2511 | ('.$this->nested_brackets_re.') # alt text = $2 |
||
2512 | \] |
||
2513 | |||
2514 | [ ]? # one optional space |
||
2515 | (?:\n[ ]*)? # one optional newline followed by spaces |
||
2516 | |||
2517 | \[ |
||
2518 | (.*?) # id = $3 |
||
2519 | \] |
||
2520 | |||
2521 | ) |
||
2522 | }xs', |
||
2523 | array(&$this, '_doImages_reference_callback'), $text); |
||
2524 | |||
2525 | # |
||
2526 | # Next, handle inline images:  |
||
2527 | # Don't forget: encode * and _ |
||
2528 | # |
||
2529 | $text = preg_replace_callback('{ |
||
2530 | ( # wrap whole match in $1 |
||
2531 | !\[ |
||
2532 | ('.$this->nested_brackets_re.') # alt text = $2 |
||
2533 | \] |
||
2534 | \s? # One optional whitespace character |
||
2535 | \( # literal paren |
||
2536 | [ \n]* |
||
2537 | (?: |
||
2538 | <(\S*)> # src url = $3 |
||
2539 | | |
||
2540 | ('.$this->nested_url_parenthesis_re.') # src url = $4 |
||
2541 | ) |
||
2542 | [ \n]* |
||
2543 | ( # $5 |
||
2544 | ([\'"]) # quote char = $6 |
||
2545 | (.*?) # title = $7 |
||
2546 | \6 # matching quote |
||
2547 | [ \n]* |
||
2548 | )? # title is optional |
||
2549 | \) |
||
2550 | (?:[ ]? '.$this->id_class_attr_catch_re.' )? # $8 = id/class attributes |
||
2551 | ) |
||
2552 | }xs', |
||
2553 | array(&$this, '_doImages_inline_callback'), $text); |
||
2554 | |||
2555 | return $text; |
||
2556 | } |
||
2557 | function _doImages_reference_callback($matches) { |
||
2558 | $whole_match = $matches[1]; |
||
2559 | $alt_text = $matches[2]; |
||
2560 | $link_id = strtolower($matches[3]); |
||
2561 | |||
2562 | if ($link_id == "") { |
||
2563 | $link_id = strtolower($alt_text); # for shortcut links like ![this][]. |
||
2564 | } |
||
2565 | |||
2566 | $alt_text = $this->encodeAttribute($alt_text); |
||
2567 | if (isset($this->urls[$link_id])) { |
||
2568 | $url = $this->encodeAttribute($this->urls[$link_id]); |
||
2569 | $result = "<img src=\"$url\" alt=\"$alt_text\""; |
||
2570 | if (isset($this->titles[$link_id])) { |
||
2571 | $title = $this->titles[$link_id]; |
||
2572 | $title = $this->encodeAttribute($title); |
||
2573 | $result .= " title=\"$title\""; |
||
2574 | } |
||
2575 | if (isset($this->ref_attr[$link_id])) |
||
2576 | $result .= $this->ref_attr[$link_id]; |
||
2577 | $result .= $this->empty_element_suffix; |
||
2578 | $result = $this->hashPart($result); |
||
2579 | } |
||
2580 | else { |
||
2581 | # If there's no such link ID, leave intact: |
||
2582 | $result = $whole_match; |
||
2583 | } |
||
2584 | |||
2585 | return $result; |
||
2586 | } |
||
2587 | function _doImages_inline_callback($matches) { |
||
2588 | $whole_match = $matches[1]; |
||
2589 | $alt_text = $matches[2]; |
||
2590 | $url = $matches[3] == '' ? $matches[4] : $matches[3]; |
||
2591 | $title =& $matches[7]; |
||
2592 | $attr = $this->doExtraAttributes("img", $dummy =& $matches[8]); |
||
2593 | |||
2594 | $alt_text = $this->encodeAttribute($alt_text); |
||
2595 | $url = $this->encodeAttribute($url); |
||
2596 | $result = "<img src=\"$url\" alt=\"$alt_text\""; |
||
2597 | if (isset($title)) { |
||
2598 | $title = $this->encodeAttribute($title); |
||
2599 | $result .= " title=\"$title\""; # $title already quoted |
||
2600 | } |
||
2601 | $result .= $attr; |
||
2602 | $result .= $this->empty_element_suffix; |
||
2603 | |||
2604 | return $this->hashPart($result); |
||
2605 | } |
||
2606 | |||
2607 | |||
2608 | function doHeaders($text) { |
||
2609 | # |
||
2610 | # Redefined to add id and class attribute support. |
||
2611 | # |
||
2612 | # Setext-style headers: |
||
2613 | # Header 1 {#header1} |
||
2614 | # ======== |
||
2615 | # |
||
2616 | # Header 2 {#header2 .class1 .class2} |
||
2617 | # -------- |
||
2618 | # |
||
2619 | $text = preg_replace_callback( |
||
2620 | '{ |
||
2621 | (^.+?) # $1: Header text |
||
2622 | (?:[ ]+ '.$this->id_class_attr_catch_re.' )? # $3 = id/class attributes |
||
2623 | [ ]*\n(=+|-+)[ ]*\n+ # $3: Header footer |
||
2624 | }mx', |
||
2625 | array(&$this, '_doHeaders_callback_setext'), $text); |
||
2626 | |||
2627 | # atx-style headers: |
||
2628 | # # Header 1 {#header1} |
||
2629 | # ## Header 2 {#header2} |
||
2630 | # ## Header 2 with closing hashes ## {#header3.class1.class2} |
||
2631 | # ... |
||
2632 | # ###### Header 6 {.class2} |
||
2633 | # |
||
2634 | $text = preg_replace_callback('{ |
||
2635 | ^(\#{1,6}) # $1 = string of #\'s |
||
2636 | [ ]* |
||
2637 | (.+?) # $2 = Header text |
||
2638 | [ ]* |
||
2639 | \#* # optional closing #\'s (not counted) |
||
2640 | (?:[ ]+ '.$this->id_class_attr_catch_re.' )? # $3 = id/class attributes |
||
2641 | [ ]* |
||
2642 | \n+ |
||
2643 | }xm', |
||
2644 | array(&$this, '_doHeaders_callback_atx'), $text); |
||
2645 | |||
2646 | return $text; |
||
2647 | } |
||
2648 | function _doHeaders_callback_setext($matches) { |
||
2649 | if ($matches[3] == '-' && preg_match('{^- }', $matches[1])) |
||
2650 | return $matches[0]; |
||
2651 | $level = $matches[3]{0} == '=' ? 1 : 2; |
||
2652 | $attr = $this->doExtraAttributes("h$level", $dummy =& $matches[2]); |
||
2653 | $block = "<h$level$attr>".$this->runSpanGamut($matches[1])."</h$level>"; |
||
2654 | return "\n" . $this->hashBlock($block) . "\n\n"; |
||
2655 | } |
||
2656 | function _doHeaders_callback_atx($matches) { |
||
2657 | $level = strlen($matches[1]); |
||
2658 | $attr = $this->doExtraAttributes("h$level", $dummy =& $matches[3]); |
||
2659 | $block = "<h$level$attr>".$this->runSpanGamut($matches[2])."</h$level>"; |
||
2660 | return "\n" . $this->hashBlock($block) . "\n\n"; |
||
2661 | } |
||
2662 | |||
2663 | |||
2664 | function doTables($text) { |
||
2665 | # |
||
2666 | # Form HTML tables. |
||
2667 | # |
||
2668 | $less_than_tab = $this->tab_width - 1; |
||
2669 | # |
||
2670 | # Find tables with leading pipe. |
||
2671 | # |
||
2672 | # | Header 1 | Header 2 |
||
2673 | # | -------- | -------- |
||
2674 | # | Cell 1 | Cell 2 |
||
2675 | # | Cell 3 | Cell 4 |
||
2676 | # |
||
2677 | $text = preg_replace_callback(' |
||
2678 | { |
||
2679 | ^ # Start of a line |
||
2680 | [ ]{0,'.$less_than_tab.'} # Allowed whitespace. |
||
2681 | [|] # Optional leading pipe (present) |
||
2682 | (.+) \n # $1: Header row (at least one pipe) |
||
2683 | |||
2684 | [ ]{0,'.$less_than_tab.'} # Allowed whitespace. |
||
2685 | [|] ([ ]*[-:]+[-| :]*) \n # $2: Header underline |
||
2686 | |||
2687 | ( # $3: Cells |
||
2688 | (?> |
||
2689 | [ ]* # Allowed whitespace. |
||
2690 | [|] .* \n # Row content. |
||
2691 | )* |
||
2692 | ) |
||
2693 | (?=\n|\Z) # Stop at final double newline. |
||
2694 | }xm', |
||
2695 | array(&$this, '_doTable_leadingPipe_callback'), $text); |
||
2696 | |||
2697 | # |
||
2698 | # Find tables without leading pipe. |
||
2699 | # |
||
2700 | # Header 1 | Header 2 |
||
2701 | # -------- | -------- |
||
2702 | # Cell 1 | Cell 2 |
||
2703 | # Cell 3 | Cell 4 |
||
2704 | # |
||
2705 | $text = preg_replace_callback(' |
||
2706 | { |
||
2707 | ^ # Start of a line |
||
2708 | [ ]{0,'.$less_than_tab.'} # Allowed whitespace. |
||
2709 | (\S.*[|].*) \n # $1: Header row (at least one pipe) |
||
2710 | |||
2711 | [ ]{0,'.$less_than_tab.'} # Allowed whitespace. |
||
2712 | ([-:]+[ ]*[|][-| :]*) \n # $2: Header underline |
||
2713 | |||
2714 | ( # $3: Cells |
||
2715 | (?> |
||
2716 | .* [|] .* \n # Row content |
||
2717 | )* |
||
2718 | ) |
||
2719 | (?=\n|\Z) # Stop at final double newline. |
||
2720 | }xm', |
||
2721 | array(&$this, '_DoTable_callback'), $text); |
||
2722 | |||
2723 | return $text; |
||
2724 | } |
||
2725 | function _doTable_leadingPipe_callback($matches) { |
||
2726 | $head = $matches[1]; |
||
2727 | $underline = $matches[2]; |
||
2728 | $content = $matches[3]; |
||
2729 | |||
2730 | # Remove leading pipe for each row. |
||
2731 | $content = preg_replace('/^ *[|]/m', '', $content); |
||
2732 | |||
2733 | return $this->_doTable_callback(array($matches[0], $head, $underline, $content)); |
||
2734 | } |
||
2735 | function _doTable_callback($matches) { |
||
2736 | $head = $matches[1]; |
||
2737 | $underline = $matches[2]; |
||
2738 | $content = $matches[3]; |
||
2739 | |||
2740 | # Remove any tailing pipes for each line. |
||
2741 | $head = preg_replace('/[|] *$/m', '', $head); |
||
2742 | $underline = preg_replace('/[|] *$/m', '', $underline); |
||
2743 | $content = preg_replace('/[|] *$/m', '', $content); |
||
2744 | |||
2745 | # Reading alignement from header underline. |
||
2746 | $separators = preg_split('/ *[|] */', $underline); |
||
2747 | foreach ($separators as $n => $s) { |
||
2748 | if (preg_match('/^ *-+: *$/', $s)) $attr[$n] = ' align="right"'; |
||
2749 | else if (preg_match('/^ *:-+: *$/', $s))$attr[$n] = ' align="center"'; |
||
2750 | else if (preg_match('/^ *:-+ *$/', $s)) $attr[$n] = ' align="left"'; |
||
2751 | else $attr[$n] = ''; |
||
2752 | } |
||
2753 | |||
2754 | # Parsing span elements, including code spans, character escapes, |
||
2755 | # and inline HTML tags, so that pipes inside those gets ignored. |
||
2756 | $head = $this->parseSpan($head); |
||
2757 | $headers = preg_split('/ *[|] */', $head); |
||
2758 | $col_count = count($headers); |
||
2759 | $attr = array_pad($attr, $col_count, ''); |
||
2760 | |||
2761 | # Write column headers. |
||
2762 | $text = "<table>\n"; |
||
2763 | $text .= "<thead>\n"; |
||
2764 | $text .= "<tr>\n"; |
||
2765 | foreach ($headers as $n => $header) |
||
2766 | $text .= " <th$attr[$n]>".$this->runSpanGamut(trim($header))."</th>\n"; |
||
2767 | $text .= "</tr>\n"; |
||
2768 | $text .= "</thead>\n"; |
||
2769 | |||
2770 | # Split content by row. |
||
2771 | $rows = explode("\n", trim($content, "\n")); |
||
2772 | |||
2773 | $text .= "<tbody>\n"; |
||
2774 | foreach ($rows as $row) { |
||
2775 | # Parsing span elements, including code spans, character escapes, |
||
2776 | # and inline HTML tags, so that pipes inside those gets ignored. |
||
2777 | $row = $this->parseSpan($row); |
||
2778 | |||
2779 | # Split row by cell. |
||
2780 | $row_cells = preg_split('/ *[|] */', $row, $col_count); |
||
2781 | $row_cells = array_pad($row_cells, $col_count, ''); |
||
2782 | |||
2783 | $text .= "<tr>\n"; |
||
2784 | foreach ($row_cells as $n => $cell) |
||
2785 | $text .= " <td$attr[$n]>".$this->runSpanGamut(trim($cell))."</td>\n"; |
||
2786 | $text .= "</tr>\n"; |
||
2787 | } |
||
2788 | $text .= "</tbody>\n"; |
||
2789 | $text .= "</table>"; |
||
2790 | |||
2791 | return $this->hashBlock($text) . "\n"; |
||
2792 | } |
||
2793 | |||
2794 | |||
2795 | function doDefLists($text) { |
||
2796 | # |
||
2797 | # Form HTML definition lists. |
||
2798 | # |
||
2799 | $less_than_tab = $this->tab_width - 1; |
||
2800 | |||
2801 | # Re-usable pattern to match any entire dl list: |
||
2802 | $whole_list_re = '(?> |
||
2803 | ( # $1 = whole list |
||
2804 | ( # $2 |
||
2805 | [ ]{0,'.$less_than_tab.'} |
||
2806 | ((?>.*\S.*\n)+) # $3 = defined term |
||
2807 | \n? |
||
2808 | [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition |
||
2809 | ) |
||
2810 | (?s:.+?) |
||
2811 | ( # $4 |
||
2812 | \z |
||
2813 | | |
||
2814 | \n{2,} |
||
2815 | (?=\S) |
||
2816 | (?! # Negative lookahead for another term |
||
2817 | [ ]{0,'.$less_than_tab.'} |
||
2818 | (?: \S.*\n )+? # defined term |
||
2819 | \n? |
||
2820 | [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition |
||
2821 | ) |
||
2822 | (?! # Negative lookahead for another definition |
||
2823 | [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition |
||
2824 | ) |
||
2825 | ) |
||
2826 | ) |
||
2827 | )'; // mx |
||
2828 | |||
2829 | $text = preg_replace_callback('{ |
||
2830 | (?>\A\n?|(?<=\n\n)) |
||
2831 | '.$whole_list_re.' |
||
2832 | }mx', |
||
2833 | array(&$this, '_doDefLists_callback'), $text); |
||
2834 | |||
2835 | return $text; |
||
2836 | } |
||
2837 | function _doDefLists_callback($matches) { |
||
2838 | # Re-usable patterns to match list item bullets and number markers: |
||
2839 | $list = $matches[1]; |
||
2840 | |||
2841 | # Turn double returns into triple returns, so that we can make a |
||
2842 | # paragraph for the last item in a list, if necessary: |
||
2843 | $result = trim($this->processDefListItems($list)); |
||
2844 | $result = "<dl>\n" . $result . "\n</dl>"; |
||
2845 | return $this->hashBlock($result) . "\n\n"; |
||
2846 | } |
||
2847 | |||
2848 | |||
2849 | function processDefListItems($list_str) { |
||
2850 | # |
||
2851 | # Process the contents of a single definition list, splitting it |
||
2852 | # into individual term and definition list items. |
||
2853 | # |
||
2854 | $less_than_tab = $this->tab_width - 1; |
||
2855 | |||
2856 | # trim trailing blank lines: |
||
2857 | $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str); |
||
2858 | |||
2859 | # Process definition terms. |
||
2860 | $list_str = preg_replace_callback('{ |
||
2861 | (?>\A\n?|\n\n+) # leading line |
||
2862 | ( # definition terms = $1 |
||
2863 | [ ]{0,'.$less_than_tab.'} # leading whitespace |
||
2864 | (?!\:[ ]|[ ]) # negative lookahead for a definition |
||
2865 | # mark (colon) or more whitespace. |
||
2866 | (?> \S.* \n)+? # actual term (not whitespace). |
||
2867 | ) |
||
2868 | (?=\n?[ ]{0,3}:[ ]) # lookahead for following line feed |
||
2869 | # with a definition mark. |
||
2870 | }xm', |
||
2871 | array(&$this, '_processDefListItems_callback_dt'), $list_str); |
||
2872 | |||
2873 | # Process actual definitions. |
||
2874 | $list_str = preg_replace_callback('{ |
||
2875 | \n(\n+)? # leading line = $1 |
||
2876 | ( # marker space = $2 |
||
2877 | [ ]{0,'.$less_than_tab.'} # whitespace before colon |
||
2878 | \:[ ]+ # definition mark (colon) |
||
2879 | ) |
||
2880 | ((?s:.+?)) # definition text = $3 |
||
2881 | (?= \n+ # stop at next definition mark, |
||
2882 | (?: # next term or end of text |
||
2883 | [ ]{0,'.$less_than_tab.'} \:[ ] | |
||
2884 | <dt> | \z |
||
2885 | ) |
||
2886 | ) |
||
2887 | }xm', |
||
2888 | array(&$this, '_processDefListItems_callback_dd'), $list_str); |
||
2889 | |||
2890 | return $list_str; |
||
2891 | } |
||
2892 | function _processDefListItems_callback_dt($matches) { |
||
2893 | $terms = explode("\n", trim($matches[1])); |
||
2894 | $text = ''; |
||
2895 | foreach ($terms as $term) { |
||
2896 | $term = $this->runSpanGamut(trim($term)); |
||
2897 | $text .= "\n<dt>" . $term . "</dt>"; |
||
2898 | } |
||
2899 | return $text . "\n"; |
||
2900 | } |
||
2901 | function _processDefListItems_callback_dd($matches) { |
||
2902 | $leading_line = $matches[1]; |
||
2903 | $marker_space = $matches[2]; |
||
2904 | $def = $matches[3]; |
||
2905 | |||
2906 | if ($leading_line || preg_match('/\n{2,}/', $def)) { |
||
2907 | # Replace marker with the appropriate whitespace indentation |
||
2908 | $def = str_repeat(' ', strlen($marker_space)) . $def; |
||
2909 | $def = $this->runBlockGamut($this->outdent($def . "\n\n")); |
||
2910 | $def = "\n". $def ."\n"; |
||
2911 | } |
||
2912 | else { |
||
2913 | $def = rtrim($def); |
||
2914 | $def = $this->runSpanGamut($this->outdent($def)); |
||
2915 | } |
||
2916 | |||
2917 | return "\n<dd>" . $def . "</dd>\n"; |
||
2918 | } |
||
2919 | |||
2920 | |||
2921 | function doFencedCodeBlocks($text) { |
||
2922 | # |
||
2923 | # Adding the fenced code block syntax to regular Markdown: |
||
2924 | # |
||
2925 | # ~~~ |
||
2926 | # Code block |
||
2927 | # ~~~ |
||
2928 | # |
||
2929 | $less_than_tab = $this->tab_width; |
||
2930 | |||
2931 | $text = preg_replace_callback('{ |
||
2932 | (?:\n|\A) |
||
2933 | # 1: Opening marker |
||
2934 | ( |
||
2935 | (?:~{3,}|`{3,}) # 3 or more tildes/backticks. |
||
2936 | ) |
||
2937 | [ ]* |
||
2938 | (?: |
||
2939 | \.?([-_:a-zA-Z0-9]+) # 2: standalone class name |
||
2940 | | |
||
2941 | '.$this->id_class_attr_catch_re.' # 3: Extra attributes |
||
2942 | )? |
||
2943 | [ ]* \n # Whitespace and newline following marker. |
||
2944 | |||
2945 | # 4: Content |
||
2946 | ( |
||
2947 | (?> |
||
2948 | (?!\1 [ ]* \n) # Not a closing marker. |
||
2949 | .*\n+ |
||
2950 | )+ |
||
2951 | ) |
||
2952 | |||
2953 | # Closing marker. |
||
2954 | \1 [ ]* (?= \n ) |
||
2955 | }xm', |
||
2956 | array(&$this, '_doFencedCodeBlocks_callback'), $text); |
||
2957 | |||
2958 | return $text; |
||
2959 | } |
||
2960 | function _doFencedCodeBlocks_callback($matches) { |
||
2961 | $classname =& $matches[2]; |
||
2962 | $attrs =& $matches[3]; |
||
2963 | $codeblock = $matches[4]; |
||
2964 | $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES); |
||
2965 | $codeblock = preg_replace_callback('/^\n+/', |
||
2966 | array(&$this, '_doFencedCodeBlocks_newlines'), $codeblock); |
||
2967 | |||
2968 | if ($classname != "") { |
||
2969 | if ($classname{0} == '.') |
||
2970 | $classname = substr($classname, 1); |
||
2971 | $attr_str = ' class="'.$this->code_class_prefix.$classname.'"'; |
||
2972 | } else { |
||
2973 | $attr_str = $this->doExtraAttributes($this->code_attr_on_pre ? "pre" : "code", $attrs); |
||
2974 | } |
||
2975 | $pre_attr_str = $this->code_attr_on_pre ? $attr_str : ''; |
||
2976 | $code_attr_str = $this->code_attr_on_pre ? '' : $attr_str; |
||
2977 | $codeblock = "<pre$pre_attr_str><code$code_attr_str>$codeblock</code></pre>"; |
||
2978 | |||
2979 | return "\n\n".$this->hashBlock($codeblock)."\n\n"; |
||
2980 | } |
||
2981 | function _doFencedCodeBlocks_newlines($matches) { |
||
2982 | return str_repeat("<br$this->empty_element_suffix", |
||
2983 | strlen($matches[0])); |
||
2984 | } |
||
2985 | |||
2986 | |||
2987 | # |
||
2988 | # Redefining emphasis markers so that emphasis by underscore does not |
||
2989 | # work in the middle of a word. |
||
2990 | # |
||
2991 | var $em_relist = array( |
||
2992 | '' => '(?:(?<!\*)\*(?!\*)|(?<![a-zA-Z0-9_])_(?!_))(?=\S|$)(?![\.,:;]\s)', |
||
2993 | '*' => '(?<=\S|^)(?<!\*)\*(?!\*)', |
||
2994 | '_' => '(?<=\S|^)(?<!_)_(?![a-zA-Z0-9_])', |
||
2995 | ); |
||
2996 | var $strong_relist = array( |
||
2997 | '' => '(?:(?<!\*)\*\*(?!\*)|(?<![a-zA-Z0-9_])__(?!_))(?=\S|$)(?![\.,:;]\s)', |
||
2998 | '**' => '(?<=\S|^)(?<!\*)\*\*(?!\*)', |
||
2999 | '__' => '(?<=\S|^)(?<!_)__(?![a-zA-Z0-9_])', |
||
3000 | ); |
||
3001 | var $em_strong_relist = array( |
||
3002 | '' => '(?:(?<!\*)\*\*\*(?!\*)|(?<![a-zA-Z0-9_])___(?!_))(?=\S|$)(?![\.,:;]\s)', |
||
3003 | '***' => '(?<=\S|^)(?<!\*)\*\*\*(?!\*)', |
||
3004 | '___' => '(?<=\S|^)(?<!_)___(?![a-zA-Z0-9_])', |
||
3005 | ); |
||
3006 | |||
3007 | |||
3008 | function formParagraphs($text) { |
||
3009 | # |
||
3010 | # Params: |
||
3011 | # $text - string to process with html <p> tags |
||
3012 | # |
||
3013 | # Strip leading and trailing lines: |
||
3014 | $text = preg_replace('/\A\n+|\n+\z/', '', $text); |
||
3015 | |||
3016 | $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY); |
||
3017 | |||
3018 | # |
||
3019 | # Wrap <p> tags and unhashify HTML blocks |
||
3020 | # |
||
3021 | foreach ($grafs as $key => $value) { |
||
3022 | $value = trim($this->runSpanGamut($value)); |
||
3023 | |||
3024 | # Check if this should be enclosed in a paragraph. |
||
3025 | # Clean tag hashes & block tag hashes are left alone. |
||
3026 | $is_p = !preg_match('/^B\x1A[0-9]+B|^C\x1A[0-9]+C$/', $value); |
||
3027 | |||
3028 | if ($is_p) { |
||
3029 | $value = "<p>$value</p>"; |
||
3030 | } |
||
3031 | $grafs[$key] = $value; |
||
3032 | } |
||
3033 | |||
3034 | # Join grafs in one text, then unhash HTML tags. |
||
3035 | $text = implode("\n\n", $grafs); |
||
3036 | |||
3037 | # Finish by removing any tag hashes still present in $text. |
||
3038 | $text = $this->unhash($text); |
||
3039 | |||
3040 | return $text; |
||
3041 | } |
||
3042 | |||
3043 | |||
3044 | ### Footnotes |
||
3045 | |||
3046 | function stripFootnotes($text) { |
||
3047 | # |
||
3048 | # Strips link definitions from text, stores the URLs and titles in |
||
3049 | # hash references. |
||
3050 | # |
||
3051 | $less_than_tab = $this->tab_width - 1; |
||
3052 | |||
3053 | # Link defs are in the form: [^id]: url "optional title" |
||
3054 | $text = preg_replace_callback('{ |
||
3055 | ^[ ]{0,'.$less_than_tab.'}\[\^(.+?)\][ ]?: # note_id = $1 |
||
3056 | [ ]* |
||
3057 | \n? # maybe *one* newline |
||
3058 | ( # text = $2 (no blank lines allowed) |
||
3059 | (?: |
||
3060 | .+ # actual text |
||
3061 | | |
||
3062 | \n # newlines but |
||
3063 | (?!\[\^.+?\]:\s)# negative lookahead for footnote marker. |
||
3064 | (?!\n+[ ]{0,3}\S)# ensure line is not blank and followed |
||
3065 | # by non-indented content |
||
3066 | )* |
||
3067 | ) |
||
3068 | }xm', |
||
3069 | array(&$this, '_stripFootnotes_callback'), |
||
3070 | $text); |
||
3071 | return $text; |
||
3072 | } |
||
3073 | function _stripFootnotes_callback($matches) { |
||
3074 | $note_id = $this->fn_id_prefix . $matches[1]; |
||
3075 | $this->footnotes[$note_id] = $this->outdent($matches[2]); |
||
3076 | return ''; # String that will replace the block |
||
3077 | } |
||
3078 | |||
3079 | |||
3080 | function doFootnotes($text) { |
||
3081 | # |
||
3082 | # Replace footnote references in $text [^id] with a special text-token |
||
3083 | # which will be replaced by the actual footnote marker in appendFootnotes. |
||
3084 | # |
||
3085 | if (!$this->in_anchor) { |
||
3086 | $text = preg_replace('{\[\^(.+?)\]}', "F\x1Afn:\\1\x1A:", $text); |
||
3087 | } |
||
3088 | return $text; |
||
3089 | } |
||
3090 | |||
3091 | |||
3092 | function appendFootnotes($text) { |
||
3093 | # |
||
3094 | # Append footnote list to text. |
||
3095 | # |
||
3096 | $text = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}', |
||
3097 | array(&$this, '_appendFootnotes_callback'), $text); |
||
3098 | |||
3099 | if (!empty($this->footnotes_ordered)) { |
||
3100 | $text .= "\n\n"; |
||
3101 | $text .= "<div class=\"footnotes\">\n"; |
||
3102 | $text .= "<hr". $this->empty_element_suffix ."\n"; |
||
3103 | $text .= "<ol>\n\n"; |
||
3104 | |||
3105 | $attr = " rev=\"footnote\""; |
||
3106 | if ($this->fn_backlink_class != "") { |
||
3107 | $class = $this->fn_backlink_class; |
||
3108 | $class = $this->encodeAttribute($class); |
||
3109 | $attr .= " class=\"$class\""; |
||
3110 | } |
||
3111 | if ($this->fn_backlink_title != "") { |
||
3112 | $title = $this->fn_backlink_title; |
||
3113 | $title = $this->encodeAttribute($title); |
||
3114 | $attr .= " title=\"$title\""; |
||
3115 | } |
||
3116 | $num = 0; |
||
3117 | |||
3118 | while (!empty($this->footnotes_ordered)) { |
||
3119 | $footnote = reset($this->footnotes_ordered); |
||
3120 | $note_id = key($this->footnotes_ordered); |
||
3121 | unset($this->footnotes_ordered[$note_id]); |
||
3122 | $ref_count = $this->footnotes_ref_count[$note_id]; |
||
3123 | unset($this->footnotes_ref_count[$note_id]); |
||
3124 | unset($this->footnotes[$note_id]); |
||
3125 | |||
3126 | $footnote .= "\n"; # Need to append newline before parsing. |
||
3127 | $footnote = $this->runBlockGamut("$footnote\n"); |
||
3128 | $footnote = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}', |
||
3129 | array(&$this, '_appendFootnotes_callback'), $footnote); |
||
3130 | |||
3131 | $attr = str_replace("%%", ++$num, $attr); |
||
3132 | $note_id = $this->encodeAttribute($note_id); |
||
3133 | |||
3134 | # Prepare backlink, multiple backlinks if multiple references |
||
3135 | $backlink = "<a href=\"#fnref:$note_id\"$attr>↩</a>"; |
||
3136 | for ($ref_num = 2; $ref_num <= $ref_count; ++$ref_num) { |
||
3137 | $backlink .= " <a href=\"#fnref$ref_num:$note_id\"$attr>↩</a>"; |
||
3138 | } |
||
3139 | # Add backlink to last paragraph; create new paragraph if needed. |
||
3140 | if (preg_match('{</p>$}', $footnote)) { |
||
3141 | $footnote = substr($footnote, 0, -4) . " $backlink</p>"; |
||
3142 | } else { |
||
3143 | $footnote .= "\n\n<p>$backlink</p>"; |
||
3144 | } |
||
3145 | |||
3146 | $text .= "<li id=\"fn:$note_id\">\n"; |
||
3147 | $text .= $footnote . "\n"; |
||
3148 | $text .= "</li>\n\n"; |
||
3149 | } |
||
3150 | |||
3151 | $text .= "</ol>\n"; |
||
3152 | $text .= "</div>"; |
||
3153 | } |
||
3154 | return $text; |
||
3155 | } |
||
3156 | function _appendFootnotes_callback($matches) { |
||
3157 | $node_id = $this->fn_id_prefix . $matches[1]; |
||
3158 | |||
3159 | # Create footnote marker only if it has a corresponding footnote *and* |
||
3160 | # the footnote hasn't been used by another marker. |
||
3161 | if (isset($this->footnotes[$node_id])) { |
||
3162 | $num =& $this->footnotes_numbers[$node_id]; |
||
3163 | if (!isset($num)) { |
||
3164 | # Transfer footnote content to the ordered list and give it its |
||
3165 | # number |
||
3166 | $this->footnotes_ordered[$node_id] = $this->footnotes[$node_id]; |
||
3167 | $this->footnotes_ref_count[$node_id] = 1; |
||
3168 | $num = $this->footnote_counter++; |
||
3169 | $ref_count_mark = ''; |
||
3170 | } else { |
||
3171 | $ref_count_mark = $this->footnotes_ref_count[$node_id] += 1; |
||
3172 | } |
||
3173 | |||
3174 | $attr = " rel=\"footnote\""; |
||
3175 | if ($this->fn_link_class != "") { |
||
3176 | $class = $this->fn_link_class; |
||
3177 | $class = $this->encodeAttribute($class); |
||
3178 | $attr .= " class=\"$class\""; |
||
3179 | } |
||
3180 | if ($this->fn_link_title != "") { |
||
3181 | $title = $this->fn_link_title; |
||
3182 | $title = $this->encodeAttribute($title); |
||
3183 | $attr .= " title=\"$title\""; |
||
3184 | } |
||
3185 | |||
3186 | $attr = str_replace("%%", $num, $attr); |
||
3187 | $node_id = $this->encodeAttribute($node_id); |
||
3188 | |||
3189 | return |
||
3190 | "<sup id=\"fnref$ref_count_mark:$node_id\">". |
||
3191 | "<a href=\"#fn:$node_id\"$attr>$num</a>". |
||
3192 | "</sup>"; |
||
3193 | } |
||
3194 | |||
3195 | return "[^".$matches[1]."]"; |
||
3196 | } |
||
3197 | |||
3198 | |||
3199 | ### Abbreviations ### |
||
3200 | |||
3201 | function stripAbbreviations($text) { |
||
3202 | # |
||
3203 | # Strips abbreviations from text, stores titles in hash references. |
||
3204 | # |
||
3205 | $less_than_tab = $this->tab_width - 1; |
||
3206 | |||
3207 | # Link defs are in the form: [id]*: url "optional title" |
||
3208 | $text = preg_replace_callback('{ |
||
3209 | ^[ ]{0,'.$less_than_tab.'}\*\[(.+?)\][ ]?: # abbr_id = $1 |
||
3210 | (.*) # text = $2 (no blank lines allowed) |
||
3211 | }xm', |
||
3212 | array(&$this, '_stripAbbreviations_callback'), |
||
3213 | $text); |
||
3214 | return $text; |
||
3215 | } |
||
3216 | function _stripAbbreviations_callback($matches) { |
||
3217 | $abbr_word = $matches[1]; |
||
3218 | $abbr_desc = $matches[2]; |
||
3219 | if ($this->abbr_word_re) |
||
3220 | $this->abbr_word_re .= '|'; |
||
3221 | $this->abbr_word_re .= preg_quote($abbr_word); |
||
3222 | $this->abbr_desciptions[$abbr_word] = trim($abbr_desc); |
||
3223 | return ''; # String that will replace the block |
||
3224 | } |
||
3225 | |||
3226 | |||
3227 | function doAbbreviations($text) { |
||
3228 | # |
||
3229 | # Find defined abbreviations in text and wrap them in <abbr> elements. |
||
3230 | # |
||
3231 | if ($this->abbr_word_re) { |
||
3232 | // cannot use the /x modifier because abbr_word_re may |
||
3233 | // contain significant spaces: |
||
3234 | $text = preg_replace_callback('{'. |
||
3235 | '(?<![\w\x1A])'. |
||
3236 | '(?:'.$this->abbr_word_re.')'. |
||
3237 | '(?![\w\x1A])'. |
||
3238 | '}', |
||
3239 | array(&$this, '_doAbbreviations_callback'), $text); |
||
3240 | } |
||
3241 | return $text; |
||
3242 | } |
||
3243 | function _doAbbreviations_callback($matches) { |
||
3244 | $abbr = $matches[0]; |
||
3245 | if (isset($this->abbr_desciptions[$abbr])) { |
||
3246 | $desc = $this->abbr_desciptions[$abbr]; |
||
3247 | if (empty($desc)) { |
||
3248 | return $this->hashPart("<abbr>$abbr</abbr>"); |
||
3249 | } else { |
||
3250 | $desc = $this->encodeAttribute($desc); |
||
3251 | return $this->hashPart("<abbr title=\"$desc\">$abbr</abbr>"); |
||
3252 | } |
||
3253 | } else { |
||
3254 | return $matches[0]; |
||
3255 | } |
||
3256 | } |
||
3257 | |||
3258 | } |
||
3259 | |||
3348 |