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