⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 markdown.py

📁 SQLAlchemy. 经典的Python ORM框架。学习必看。
💻 PY
📖 第 1 页 / 共 4 页
字号:
                        continue                    elif not block[1] == "!":                        # if is block level tag and is not complete                        items.append(block.strip())                        in_tag = True                        continue                                    new_blocks.append(block)            else:                items.append(block.strip())                                right_tag = self._get_right_tag(left_tag, block)                if self._equal_tags(left_tag, right_tag):                    # if find closing tag                    in_tag = False                    new_blocks.append(                        self.stash.store('\n\n'.join(items)))                    items = []                            return "\n\n".join(new_blocks).split("\n")HTML_BLOCK_PREPROCESSOR = HtmlBlockPreprocessor()class ReferencePreprocessor (Preprocessor):    def run (self, lines) :        new_text = [];        for line in lines:            m = RE.regExp['reference-def'].match(line)            if m:                id = m.group(2).strip().lower()                t = m.group(4).strip()  # potential title                if not t :                    self.references[id] = (m.group(3), t)                elif (len(t) >= 2                      and (t[0] == t[-1] == "\""                           or t[0] == t[-1] == "\'"                           or (t[0] == "(" and t[-1] == ")") ) ) :                    self.references[id] = (m.group(3), t[1:-1])                else :                    new_text.append(line)            else:                new_text.append(line)        return new_text #+ "\n"REFERENCE_PREPROCESSOR = ReferencePreprocessor()"""================================================================================================ INLINE PATTERNS =================================================================================================Inline patterns such as *emphasis* are handled by means of auxiliaryobjects, one per pattern.  Pattern objects must be instances of classesthat extend markdown.Pattern.  Each pattern object uses a single regularexpression and needs support the following methods:  pattern.getCompiledRegExp() - returns a regular expression  pattern.handleMatch(m, doc) - takes a match object and returns                                a NanoDom node (as a part of the provided                                doc) or NoneAll of python markdown's built-in patterns subclass from Patter,but you can add additional patterns that don't.Also note that all the regular expressions used by inline mustcapture the whole block.  For this reason, they all start with'^(.*)' and end with '(.*)!'.  In case with built-in expressionPattern takes care of adding the "^(.*)" and "(.*)!".Finally, the order in which regular expressions are applied is veryimportant - e.g. if we first replace http://.../ links with <a> tagsand _then_ try to replace inline html, we would end up with a mess.So, we apply the expressions in the following order:       * escape and backticks have to go before everything else, so         that we can preempt any markdown patterns by escaping them.       * then we handle auto-links (must be done before inline html)       * then we handle inline HTML.  At this point we will simply         replace all inline HTML strings with a placeholder and add         the actual HTML to a hash.       * then inline images (must be done before links)       * then bracketed links, first regular then reference-style       * finally we apply strong and emphasis"""NOBRACKET = r'[^\]\[]*'BRK = ( r'\[('        + (NOBRACKET + r'(\['+NOBRACKET)*6        + (NOBRACKET+ r'\])*'+NOBRACKET)*6        + NOBRACKET + r')\]' )BACKTICK_RE = r'\`([^\`]*)\`'                    # `e= m*c^2`DOUBLE_BACKTICK_RE =  r'\`\`(.*)\`\`'            # ``e=f("`")``ESCAPE_RE = r'\\(.)'                             # \<EMPHASIS_RE = r'\*([^\*]*)\*'                    # *emphasis*STRONG_RE = r'\*\*(.*)\*\*'                      # **strong**STRONG_EM_RE = r'\*\*\*([^_]*)\*\*\*'            # ***strong***if SMART_EMPHASIS:    EMPHASIS_2_RE = r'(?<!\S)_(\S[^_]*)_'        # _emphasis_else :    EMPHASIS_2_RE = r'_([^_]*)_'                 # _emphasis_STRONG_2_RE = r'__([^_]*)__'                     # __strong__STRONG_EM_2_RE = r'___([^_]*)___'                # ___strong___LINK_RE = BRK + r'\s*\(([^\)]*)\)'               # [text](url)LINK_ANGLED_RE = BRK + r'\s*\(<([^\)]*)>\)'      # [text](<url>)IMAGE_LINK_RE = r'\!' + BRK + r'\s*\(([^\)]*)\)' # ![alttxt](http://x.com/)REFERENCE_RE = BRK+ r'\s*\[([^\]]*)\]'           # [Google][3]IMAGE_REFERENCE_RE = r'\!' + BRK + '\s*\[([^\]]*)\]' # ![alt text][2]NOT_STRONG_RE = r'( \* )'                        # stand-alone * or _AUTOLINK_RE = r'<(http://[^>]*)>'                # <http://www.123.com>AUTOMAIL_RE = r'<([^> \!]*@[^> ]*)>'               # <me@example.com>#HTML_RE = r'(\<[^\>]*\>)'                        # <...>HTML_RE = r'(\<[a-zA-Z/][^\>]*\>)'               # <...>ENTITY_RE = r'(&[\#a-zA-Z0-9]*;)'                # &amp;class Pattern:    def __init__ (self, pattern) :        self.pattern = pattern        self.compiled_re = re.compile("^(.*)%s(.*)$" % pattern, re.DOTALL)    def getCompiledRegExp (self) :        return self.compiled_reBasePattern = Pattern # for backward compatibilityclass SimpleTextPattern (Pattern) :    def handleMatch(self, m, doc) :        return doc.createTextNode(m.group(2))class SimpleTagPattern (Pattern):    def __init__ (self, pattern, tag) :        Pattern.__init__(self, pattern)        self.tag = tag    def handleMatch(self, m, doc) :        el = doc.createElement(self.tag)        el.appendChild(doc.createTextNode(m.group(2)))        return elclass BacktickPattern (Pattern):    def __init__ (self, pattern):        Pattern.__init__(self, pattern)        self.tag = "code"    def handleMatch(self, m, doc) :        el = doc.createElement(self.tag)        text = m.group(2).strip()        #text = text.replace("&", "&amp;")        el.appendChild(doc.createTextNode(text))        return elclass DoubleTagPattern (SimpleTagPattern) :    def handleMatch(self, m, doc) :        tag1, tag2 = self.tag.split(",")        el1 = doc.createElement(tag1)        el2 = doc.createElement(tag2)        el1.appendChild(el2)        el2.appendChild(doc.createTextNode(m.group(2)))        return el1class HtmlPattern (Pattern):    def handleMatch (self, m, doc) :        place_holder = self.stash.store(m.group(2))        return doc.createTextNode(place_holder)class LinkPattern (Pattern):    def handleMatch(self, m, doc) :        el = doc.createElement('a')        el.appendChild(doc.createTextNode(m.group(2)))        parts = m.group(9).split()        # We should now have [], [href], or [href, title]        if parts :            el.setAttribute('href', parts[0])        else :            el.setAttribute('href', "")        if len(parts) > 1 :            # we also got a title            title = " ".join(parts[1:]).strip()            title = dequote(title) #.replace('"', "&quot;")            el.setAttribute('title', title)        return elclass ImagePattern (Pattern):    def handleMatch(self, m, doc):        el = doc.createElement('img')        src_parts = m.group(9).split()        el.setAttribute('src', src_parts[0])        if len(src_parts) > 1 :            el.setAttribute('title', dequote(" ".join(src_parts[1:])))        if ENABLE_ATTRIBUTES :            text = doc.createTextNode(m.group(2))            el.appendChild(text)            text.handleAttributes()            truealt = text.value            el.childNodes.remove(text)        else:            truealt = m.group(2)        el.setAttribute('alt', truealt)        return elclass ReferencePattern (Pattern):    def handleMatch(self, m, doc):        if m.group(9) :            id = m.group(9).lower()        else :            # if we got something like "[Google][]"            # we'll use "google" as the id            id = m.group(2).lower()        if not self.references.has_key(id) : # ignore undefined refs            return None        href, title = self.references[id]        text = m.group(2)        return self.makeTag(href, title, text, doc)    def makeTag(self, href, title, text, doc):        el = doc.createElement('a')        el.setAttribute('href', href)        if title :            el.setAttribute('title', title)        el.appendChild(doc.createTextNode(text))        return elclass ImageReferencePattern (ReferencePattern):    def makeTag(self, href, title, text, doc):        el = doc.createElement('img')        el.setAttribute('src', href)        if title :            el.setAttribute('title', title)        el.setAttribute('alt', text)        return elclass AutolinkPattern (Pattern):    def handleMatch(self, m, doc):        el = doc.createElement('a')        el.setAttribute('href', m.group(2))        el.appendChild(doc.createTextNode(m.group(2)))        return elclass AutomailPattern (Pattern):    def handleMatch(self, m, doc) :        el = doc.createElement('a')        email = m.group(2)        if email.startswith("mailto:"):            email = email[len("mailto:"):]        for letter in email:            entity = doc.createEntityReference("#%d" % ord(letter))            el.appendChild(entity)        mailto = "mailto:" + email        mailto = "".join(['&#%d;' % ord(letter) for letter in mailto])        el.setAttribute('href', mailto)        return elESCAPE_PATTERN          = SimpleTextPattern(ESCAPE_RE)NOT_STRONG_PATTERN      = SimpleTextPattern(NOT_STRONG_RE)BACKTICK_PATTERN        = BacktickPattern(BACKTICK_RE)DOUBLE_BACKTICK_PATTERN = BacktickPattern(DOUBLE_BACKTICK_RE)STRONG_PATTERN          = SimpleTagPattern(STRONG_RE, 'strong')STRONG_PATTERN_2        = SimpleTagPattern(STRONG_2_RE, 'strong')EMPHASIS_PATTERN        = SimpleTagPattern(EMPHASIS_RE, 'em')EMPHASIS_PATTERN_2      = SimpleTagPattern(EMPHASIS_2_RE, 'em')STRONG_EM_PATTERN       = DoubleTagPattern(STRONG_EM_RE, 'strong,em')STRONG_EM_PATTERN_2     = DoubleTagPattern(STRONG_EM_2_RE, 'strong,em')LINK_PATTERN            = LinkPattern(LINK_RE)LINK_ANGLED_PATTERN     = LinkPattern(LINK_ANGLED_RE)IMAGE_LINK_PATTERN      = ImagePattern(IMAGE_LINK_RE)IMAGE_REFERENCE_PATTERN = ImageReferencePattern(IMAGE_REFERENCE_RE)REFERENCE_PATTERN       = ReferencePattern(REFERENCE_RE)HTML_PATTERN            = HtmlPattern(HTML_RE)ENTITY_PATTERN          = HtmlPattern(ENTITY_RE)AUTOLINK_PATTERN        = AutolinkPattern(AUTOLINK_RE)AUTOMAIL_PATTERN        = AutomailPattern(AUTOMAIL_RE)"""================================================================================================ POST-PROCESSORS =================================================================================================Markdown also allows post-processors, which are similar topreprocessors in that they need to implement a "run" method.  Unlikepre-processors, they take a NanoDom document as a parameter and workwith that.Post-Processor should extend markdown.Postprocessor.There are currently no standard post-processors, but the footnoteextension below uses one."""class Postprocessor :    pass"""================================================================================================ MISC AUXILIARY CLASSES =========================================================================================="""class HtmlStash :    """This class is used for stashing HTML objects that we extract        in the beginning and replace with place-holders."""    def __init__ (self) :        self.html_counter = 0 # for counting inline html segments        self.rawHtmlBlocks=[]    def store(self, html) :        """Saves an HTML segment for later reinsertion.  Returns a           placeholder string that needs to be inserted into the           document.           @param html: an html segment           @returns : a placeholder string """        self.rawHtmlBlocks.append(html)        placeholder = HTML_PLACEHOLDER % self.html_counter        self.html_counter += 1        return placeholderclass BlockGuru :    def _findHead(self, lines, fn, allowBlank=0) :        """Functional magic to help determine boundaries of indented           blocks.           @param lines: an array of strings           @param fn: a function that returns a substring of a string                      if the string matches the necessary criteria           @param allowBlank: specifies whether it's ok to have blank                      lines between matching functions           @returns: a list of post processes items and the unused                      remainder of the original list"""        items = []        item = -1        i = 0 # to keep track of where we are        for line in lines :            if not line.strip() and not allowBlank:                return items, lines[i:]            if not line.strip() and allowBlank:                # If we see a blank line, this _might_ be the end                i += 1                # Find the next non-blank line                for j in range(i, len(lines)) :                    if lines[j].strip() :                        next = lines[j]                        break                else :                    # There is no more text => this is the end                    break                # Check if the next non-blank line is still a part of the list                part = fn(next)                if part :                    items.append("")                    continue                else :                    break # found end of the list            part = fn(line)            if part :                items.append(part)                i += 1                continue            else :                return items, lines[i:]        else :            i += 1        return items, lines[i:]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -