15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# -*- coding: utf-8 -*-
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# markdown is released under the BSD license
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later)
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Copyright 2004 Manfred Stienstra (the original version)
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# All rights reserved.
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Redistribution and use in source and binary forms, with or without
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# modification, are permitted provided that the following conditions are met:
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# *   Redistributions of source code must retain the above copyright
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#     notice, this list of conditions and the following disclaimer.
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# *   Redistributions in binary form must reproduce the above copyright
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#     notice, this list of conditions and the following disclaimer in the
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#     documentation and/or other materials provided with the distribution.
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# *   Neither the name of the <organization> nor the
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#     names of its contributors may be used to endorse or promote products
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#     derived from this software without specific prior written permission.
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# POSSIBILITY OF SUCH DAMAGE.
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)from __future__ import unicode_literals
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import re
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import sys
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Python 3 Stuff
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=============================================================================
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PY3 = sys.version_info[0] == 3
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)if PY3:
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    string_type = str
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    text_type = str
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int2str = chr
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)else:
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    string_type = basestring
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    text_type = unicode
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int2str = unichr
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Constants you might want to modify
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)-----------------------------------------------------------------------------
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BLOCK_LEVEL_ELEMENTS = re.compile("^(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul"
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  "|script|noscript|form|fieldset|iframe|math"
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  "|hr|hr/|style|li|dt|dd|thead|tbody"
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  "|tr|th|td|section|footer|header|group|figure"
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  "|figcaption|aside|article|canvas|output"
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  "|progress|video)$", re.IGNORECASE)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Placeholders
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)STX = '\u0002'  # Use STX ("Start of text") for start-of-placeholder
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ETX = '\u0003'  # Use ETX ("End of text") for end-of-placeholder
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)INLINE_PLACEHOLDER_PREFIX = STX+"klzzwxh:"
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)INLINE_PLACEHOLDER = INLINE_PLACEHOLDER_PREFIX + "%s" + ETX
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)INLINE_PLACEHOLDER_RE = re.compile(INLINE_PLACEHOLDER % r'([0-9]{4})')
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)AMP_SUBSTITUTE = STX+"amp"+ETX
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Constants you probably do not need to change
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)-----------------------------------------------------------------------------
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)RTL_BIDI_RANGES = ( ('\u0590', '\u07FF'),
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     # Hebrew (0590-05FF), Arabic (0600-06FF),
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     # Syriac (0700-074F), Arabic supplement (0750-077F),
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     # Thaana (0780-07BF), Nko (07C0-07FF).
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    ('\u2D30', '\u2D7F'), # Tifinagh
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    )
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Extensions should use "markdown.util.etree" instead of "etree" (or do `from
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# markdown.util import etree`).  Do not import it by yourself.
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)try: # Is the C implemenation of ElementTree available?
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    import xml.etree.cElementTree as etree
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    from xml.etree.ElementTree import Comment
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    # Serializers (including ours) test with non-c Comment
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    etree.test_comment = Comment
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if etree.VERSION < "1.0.5":
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        raise RuntimeError("cElementTree version 1.0.5 or higher is required.")
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)except (ImportError, RuntimeError):
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    # Use the Python implementation of ElementTree?
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    import xml.etree.ElementTree as etree
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if etree.VERSION < "1.1":
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        raise RuntimeError("ElementTree version 1.1 or higher is required")
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)AUXILIARY GLOBAL FUNCTIONS
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=============================================================================
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def isBlockLevel(tag):
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    """Check if the tag is a block level HTML tag."""
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if isinstance(tag, string_type):
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return BLOCK_LEVEL_ELEMENTS.match(tag)
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    # Some ElementTree tags are not strings, so return False.
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return False
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MISC AUXILIARY CLASSES
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=============================================================================
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class AtomicString(text_type):
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    """A string which should not be further processed."""
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    pass
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class Processor(object):
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    def __init__(self, markdown_instance=None):
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if markdown_instance:
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            self.markdown = markdown_instance
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class HtmlStash(object):
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    """
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    This class is used for stashing HTML objects that we extract
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    in the beginning and replace with place-holders.
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    """
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    def __init__ (self):
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        """ Create a HtmlStash. """
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        self.html_counter = 0 # for counting inline html segments
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        self.rawHtmlBlocks=[]
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    def store(self, html, safe=False):
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        """
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        Saves an HTML segment for later reinsertion.  Returns a
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        placeholder string that needs to be inserted into the
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        document.
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        Keyword arguments:
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        * html: an html segment
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        * safe: label an html segment as safe for safemode
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        Returns : a placeholder string
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        """
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        self.rawHtmlBlocks.append((html, safe))
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        placeholder = self.get_placeholder(self.html_counter)
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        self.html_counter += 1
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return placeholder
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    def reset(self):
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        self.html_counter = 0
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        self.rawHtmlBlocks = []
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    def get_placeholder(self, key):
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return "%swzxhzdk:%d%s" % (STX, key, ETX)
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
169