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