From bcc437814eb90c60120d0e7a9c1ece85bf66aea3 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Fri, 2 May 2008 22:26:41 -0400 Subject: Updated tests in wikilink extension to match current markdown (use convert, unicode output and trimmed whitespace) and added unobstrusive meta-data support. --- mdx_wikilink.py | 70 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/mdx_wikilink.py b/mdx_wikilink.py index 4279c1a..46d6c3f 100644 --- a/mdx_wikilink.py +++ b/mdx_wikilink.py @@ -12,7 +12,7 @@ Basic usage: >>> text = "Some text with a WikiLink." >>> md = markdown.markdown(text, ['wikilink']) >>> md - '\\n

Some text with a WikiLink.\\n

\\n\\n\\n' + u'

Some text with a WikiLink.\\n

' To define custom settings the simple way: @@ -20,20 +20,35 @@ To define custom settings the simple way: ... ['wikilink(base_url=/wiki/,end_url=.html,html_class=foo)'] ... ) >>> md - '\\n

Some text with a WikiLink.\\n

\\n\\n\\n' + u'

Some text with a WikiLink.\\n

' Custom settings the complex way: - >>> md = markdown.Markdown(text, + >>> md = markdown.Markdown( ... extensions = ['wikilink'], ... extension_configs = {'wikilink': [ ... ('base_url', 'http://example.com/'), ... ('end_url', '.html'), ... ('html_class', '') ]}, - ... encoding='utf8', ... safe_mode = True) - >>> str(md) - '\\n

Some text with a WikiLink.\\n

\\n\\n\\n' + >>> md.convert(text) + u'

Some text with a WikiLink.\\n

' + +Use MetaData with mdx_meta.py (Note the blank html_class in MetaData): + + >>> text = """wiki_base_url: http://example.com/ + ... wiki_end_url: .html + ... wiki_html_class: + ... + ... Some text with a WikiLink.""" + >>> md = markdown.Markdown(extensions=['meta', 'wikilink']) + >>> md.convert(text) + u'

Some text with a WikiLink.\\n

' + +MetaData should not carry over to next document: + + >>> md.convert("No MetaData here.") + u'

No MetaData here.\\n

' From the command line: @@ -46,13 +61,11 @@ Contact: waylan [at] gmail [dot] com License: [BSD](http://www.opensource.org/licenses/bsd-license.php) -Version: 0.4 (Oct 14, 2006) +Version: 0.6 (May 2, 2008) Dependencies: * [Python 2.3+](http://python.org) * [Markdown 1.6+](http://www.freewisdom.org/projects/python-markdown/) -* For older dependencies use [WikiLink Version 0.3] -(http://code.limberg.name/svn/projects/py-markdown-ext/wikilinks/tags/release-0.3/) ''' import markdown @@ -68,16 +81,16 @@ class WikiLinkExtension (markdown.Extension) : # Override defaults with user settings for key, value in configs : - # self.config[key][0] = value self.setConfig(key, value) def extendMarkdown(self, md, md_globals): self.md = md - #md.registerExtension(self) #??? # append to end of inline patterns WIKILINK_RE = r'''(?P\\|\b)(?P([A-Z]+[a-z-_]+){2,})\b''' - md.inlinePatterns.append(WikiLinks(WIKILINK_RE, self.config)) + WIKILINK_PATTERN = WikiLinks(WIKILINK_RE, self.config) + WIKILINK_PATTERN.md = md + md.inlinePatterns.append(WIKILINK_PATTERN) class WikiLinks (markdown.BasePattern) : def __init__(self, pattern, config): @@ -87,19 +100,36 @@ class WikiLinks (markdown.BasePattern) : def handleMatch(self, m, doc) : if m.group('escape') == '\\': a = doc.createTextNode(m.group('camelcase')) - else : - url = '%s%s%s'% (self.config['base_url'][0], m.group('camelcase'), self.config['end_url'][0]) + else: + base_url, end_url, html_class = self._getMeta() + url = '%s%s%s'% (base_url, m.group('camelcase'), end_url) label = m.group('camelcase').replace('_', ' ') a = doc.createElement('a') a.appendChild(doc.createTextNode(label)) a.setAttribute('href', url) - if self.config['html_class'][0] : - a.setAttribute('class', self.config['html_class'][0]) + if html_class: + a.setAttribute('class', html_class) return a - + + def _getMeta(self): + """ Return meta data or config data. """ + base_url = self.config['base_url'][0] + end_url = self.config['end_url'][0] + html_class = self.config['html_class'][0] + if hasattr(self.md, 'Meta'): + if self.md.Meta.has_key('wiki_base_url'): + base_url = self.md.Meta['wiki_base_url'][0] + if self.md.Meta.has_key('wiki_end_url'): + end_url = self.md.Meta['wiki_end_url'][0] + if self.md.Meta.has_key('wiki_html_class'): + html_class = self.md.Meta['wiki_html_class'][0] + return base_url, end_url, html_class + + def makeExtension(configs=None) : return WikiLinkExtension(configs=configs) -if __name__ == "__main__": - import doctest - doctest.testmod() \ No newline at end of file +if __name__ == "__main__": + import doctest + doctest.testmod() + -- cgit v1.2.3 From 180248415c13911628708d5bcd21674609d4fe0d Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Fri, 2 May 2008 22:35:12 -0400 Subject: Cleaned up doc strings and greatly simplified the codehilite extension. Removed support or enscript and dp highlighing engines - only pygments is supported. --- mdx_codehilite.py | 328 ++++++++++++++++++++++-------------------------------- 1 file changed, 136 insertions(+), 192 deletions(-) diff --git a/mdx_codehilite.py b/mdx_codehilite.py index ea83e9a..6f81598 100644 --- a/mdx_codehilite.py +++ b/mdx_codehilite.py @@ -1,197 +1,141 @@ #!/usr/bin/python + +""" +CodeHilite Extension for Python-Markdown +======================================= + +Adds code/syntax highlighting to standard Python-Markdown code blocks. + +By [Waylan Limberg](http://achinghead.com/). + +Project website: http://achinghead.com/markdown-wikilinks/ +Contact: waylan [at] gmail [dot] com + +License: [BSD](http://www.opensource.org/licenses/bsd-license.php) + +Version: 0.2 (April 30, 2008) + +Dependencies: +* [Python 2.3+](http://python.org/) +* [Markdown 1.7+](http://www.freewisdom.org/projects/python-markdown/) +* [Pygments](http://pygments.org/) + +""" + import markdown # --------------- CONSTANTS YOU MIGHT WANT TO MODIFY ----------------- -DEFAULT_HILITER = 'pygments' # one of 'enscript', 'dp', or 'pygments' try: TAB_LENGTH = markdown.TAB_LENGTH except AttributeError: TAB_LENGTH = 4 -# --------------- THE CODE ------------------------------------------- -# --------------- hiliter utility functions -------------------------- -def escape(txt) : - '''basic html escaping''' - txt = txt.replace('&', '&') - txt = txt.replace('<', '<') - txt = txt.replace('>', '>') - txt = txt.replace('"', '"') - return txt -def number(txt): - '''use
    for line numbering''' - # Fix Whitespace - txt = txt.replace('\t', ' '*TAB_LENGTH) - txt = txt.replace(" "*4, "    ") - txt = txt.replace(" "*3, "   ") - txt = txt.replace(" "*2, "  ") - - # Add line numbers - lines = txt.splitlines() - txt = '
      \n' - for line in lines: - txt += '\t
    1. %s
    2. \n'% line - txt += '
    \n' - return txt - -# ---------------- The hiliters --------------------------------------- -def enscript(src, lang=None, num=True): - ''' -Pass source code on to [enscript] (http://www.codento.com/people/mtr/genscript/) -command line utility for hiliting. - -Usage: - >>> enscript(src [, lang [, num ]] ) - - @param src: Can be a string or any object with a .readline attribute. - - @param lang: The language of code. Basic escaping only, if None. +# ------------------ The Main CodeHilite Class ---------------------- +class CodeHilite: + """ + Determine language of source code, and pass it into the pygments hilighter. - @param num: (Boolen) Turns line numbering 'on' or 'off' (on by default). + Basic Usage: + >>> code = CodeHilite(src = text) + >>> html = code.hilite() + + * src: Can be a string or any object with a .readline attribute. - @returns : A string of html. - ''' - if lang: - cmd = 'enscript --highlight=%s --color --language=html --tabsize=%d --output=-'% (lang, TAB_LENGTH) - from os import popen3 - (i, out, err) = popen3(cmd) - i.write(src) - i.close() - # check for errors - e = err.read() - if e != 'output left in -\n' : - # error - just escape - txt = escape(src) - else : - import re - pattern = re.compile(r'
    (?P.*?)
    ', re.DOTALL) - txt = pattern.search(out.read()).group('code') - # fix enscripts output - txt = txt.replace('\n', '\n').strip() - html_map = {'' : '', - '' : '', - '' : '', - '' : '', - '' : '' - } - for k, v in html_map.items() : - txt = txt.replace(k, v) - else: - txt = escape(src) - if num : - txt = number(txt) - else : - txt = '
    %s
    \n'% txt - return txt - - -def dp(src, lang=None, num=True): - ''' -Pass source code to a textarea for the [dp.SyntaxHighlighter] (http://www.dreamprojections.com/syntaxhighlighter/Default.aspx) - -Usage: - >>> dp(src [, lang [, num ]] ) - - @param src: A string. - - @param lang: The language of code. Undefined if None. - - @param num: (Boolen) Turns line numbering 'on' or 'off' (on by default). + * linenos: (Boolen) Turns line numbering 'on' or 'off' (off by default). - @returns : A string of html. - ''' - gutter = '' - if not num: - gutter = ':nogutter' - if not lang: - lang = '' - - return '
    \n'% (lang, gutter, src) + Low Level Usage: + >>> code = CodeHilite() + >>> code.src = text # String or anything with a .readline attribute + >>> code.linenos = True # True or False; Turns line numbering on or of. + >>> html = code.hilite() -def pygment(src, lang = None, num = True): - ''' -Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with -optional line numbers. The output should then be styled with css to your liking. -No styles are applied by default - only styling hooks (i.e.: ). + """ -Usage: - >>> pygment(src [, lang [, num ]] ) + def __init__(self, src=None, linenos = False): + self.src = src + self.lang = None + self.linenos = linenos - @param src: Can be a string or any object with a .readline attribute. + def hilite(self): + """ + Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with + optional line numbers. The output should then be styled with css to + your liking. No styles are applied by default - only styling hooks + (i.e.: ). - @param lang: The language of code. Pygments will try to guess language if None. + returns : A string of html. + + """ + + self.src = self.src.strip('\n') + + self._getLang() - @param num: (Boolen) Turns line numbering 'on' or 'off' (on by default). - - @returns : A string of html. - ''' - try: - from pygments import highlight - from pygments.lexers import get_lexer_by_name, guess_lexer, TextLexer - from pygments.formatters import HtmlFormatter - except ImportError: - # just escape and pass through - txt = escape(src) - if num: - txt = number(txt) - else : - txt = '
    %s
    \n'% txt - return txt - else: try: - lexer = get_lexer_by_name(lang) - except ValueError: + from pygments import highlight + from pygments.lexers import get_lexer_by_name, guess_lexer, TextLexer + from pygments.formatters import HtmlFormatter + except ImportError: + # just escape and pass through + txt = self._escape(self.src) + if num: + txt = self._number(txt) + else : + txt = '
    %s
    \n'% txt + return txt + else: try: - lexer = guess_lexer(src) + lexer = get_lexer_by_name(self.lang) except ValueError: - lexer = TextLexer() - formatter = HtmlFormatter(linenos=num, cssclass="codehilite") - return highlight(src, lexer, formatter) - - -# ------------------ The Main CodeHilite Class ---------------------- -class CodeHilite: - ''' -A wrapper class providing a single API for various hilighting engines. Takes source code, determines which language it containes (if not provided), and passes it into the hiliter specified. - -Basic Usage: - >>> code = CodeHilite(src = text) - >>> html = code.hilite() - - @param src: Can be a string or any object with a .readline attribute. - - @param lang: A string. Accepted values determined by hiliter used. Overrides _getLang() - - @param linenos: (Boolen) Turns line numbering 'on' or 'off' (off by default). - - @param hiliter: A string. One of 'enscript', 'dp', or 'pygments'. + try: + lexer = guess_lexer(self.src) + except ValueError: + lexer = TextLexer() + formatter = HtmlFormatter(linenos=self.linenos, cssclass="codehilite") + return highlight(self.src, lexer, formatter) + + def _escape(self, txt): + """ basic html escaping """ + txt = txt.replace('&', '&') + txt = txt.replace('<', '<') + txt = txt.replace('>', '>') + txt = txt.replace('"', '"') + return txt -Low Level Usage: - >>> code = CodeHilite() - >>> code.src = text # Can be a string or any object with a .readline attribute. - >>> code.lang = 'python' # Setting this will override _getLang() - >>> code.linenos = True # True or False; Turns line numbering on or off. - >>> code.hiliter = MyCustomHiliter # Where MyCustomHiliter is callable, takes three arguments (src, lang, linenos) and returns a string. - >>> html = code.hilite() - ''' - def __init__(self, src=None, lang=None, linenos = False, hiliter=DEFAULT_HILITER): - self.src = src - self.lang = lang - self.linenos = linenos - # map of highlighters - hl_map = { 'enscript' : enscript, 'dp' : dp, 'pygments' : pygment } - try : - self.hiliter = hl_map[hiliter] - except KeyError: - raise "Please provide a valid hiliter as a string. One of 'enscript', 'dp', or 'pygments'" + def _number(self, txt): + """ Use
      for line numbering """ + # Fix Whitespace + txt = txt.replace('\t', ' '*TAB_LENGTH) + txt = txt.replace(" "*4, "    ") + txt = txt.replace(" "*3, "   ") + txt = txt.replace(" "*2, "  ") + + # Add line numbers + lines = txt.splitlines() + txt = '
        \n' + for line in lines: + txt += '\t
      1. %s
      2. \n'% line + txt += '
      \n' + return txt def _getLang(self): - ''' -Determines language of a code block from shebang lines and whether said line should be removed or left in place. If the sheband line contains a path (even a single /) then it is assumed to be a real shebang lines and left alone. However, if no path is given (e.i.: #!python or :::python) then it is assumed to be a mock shebang for language identifitation of a code fragment and removed from the code block prior to processing for code highlighting. When a mock shebang (e.i: #!python) is found, line numbering is turned on. When colons are found in place of a shebang (e.i.: :::python), line numbering is left in the current state - off by default. - ''' + """ + Determines language of a code block from shebang lines and whether said + line should be removed or left in place. If the sheband line contains a + path (even a single /) then it is assumed to be a real shebang lines and + left alone. However, if no path is given (e.i.: #!python or :::python) + then it is assumed to be a mock shebang for language identifitation of a + code fragment and removed from the code block prior to processing for + code highlighting. When a mock shebang (e.i: #!python) is found, line + numbering is turned on. When colons are found in place of a shebang + (e.i.: :::python), line numbering is left in the current state - off + by default. + + """ + import re #split text into lines @@ -224,47 +168,47 @@ Determines language of a code block from shebang lines and whether said line sho self.src = "\n".join(lines).strip("\n") - def hilite(self): - '''The wrapper function which brings it all togeather''' - self.src = self.src.strip('\n') - - if not self.lang : self._getLang() - - return self.hiliter(self.src, self.lang, self.linenos) -# ------------------ The Markdown Extention ------------------------------- -class CodeHiliteExtention (markdown.Extension) : +# ------------------ The Markdown Extension ------------------------------- +class CodeHiliteExtention(markdown.Extension): def __init__(self, configs): # define default configs - self.config = {'hiliter' : [DEFAULT_HILITER, "one of 'enscript', 'dp', or 'pygments'"], - 'force_linenos' : [False, "Force line numbers - Default: False"] } + self.config = { + 'force_linenos' : [False, "Force line numbers - Default: False"] + } # Override defaults with user settings - for key, value in configs : + for key, value in configs: # self.config[key][0] = value self.setConfig(key, value) - def extendMarkdown(self, md, md_globals) : + def extendMarkdown(self, md, md_globals): def _hiliteCodeBlock(parent_elem, lines, inList): - """Overrides function of same name in standard Markdown class and - sends code blocks to a code highlighting proccessor. The result - is then stored in the HtmlStash, a placeholder is inserted into - the dom and the remainder of the text file is processed recursively. + """ + Overrides `_processCodeBlock` method in standard Markdown class + and sends code blocks to a code highlighting proccessor. The result + is then stored in the HtmlStash, a placeholder is inserted into + the dom and the remainder of the text file is processed recursively. + + * parent_elem: DOM element to which the content will be added + * lines: a list of lines + * inList: a level + + returns: None + + """ - @param parent_elem: DOM element to which the content will be added - @param lines: a list of lines - @param inList: a level - @returns: None""" detabbed, theRest = md.blockGuru.detectTabbed(lines) text = "\n".join(detabbed).rstrip()+"\n" - code = CodeHilite(text, hiliter=self.config['hiliter'][0], linenos=self.config['force_linenos'][0]) - placeholder = md.htmlStash.store(code.hilite()) + code = CodeHilite(text, linenos=self.config['force_linenos'][0]) + placeholder = md.htmlStash.store(code.hilite(), safe=True) parent_elem.appendChild(md.doc.createTextNode(placeholder)) md._processSection(parent_elem, theRest, inList) md._processCodeBlock = _hiliteCodeBlock -def makeExtension(configs=None) : +def makeExtension(configs={}): return CodeHiliteExtention(configs=configs) + -- cgit v1.2.3 From 19ea0eedcbca5189df9cf9f6ef658eade79abd79 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Mon, 12 May 2008 00:00:11 -0400 Subject: Added fenced_code extension and .gitignore file. --- .gitignore | 1 + mdx_fenced_code.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 mdx_fenced_code.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/mdx_fenced_code.py b/mdx_fenced_code.py new file mode 100644 index 0000000..4b0e406 --- /dev/null +++ b/mdx_fenced_code.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python + +""" +Fenced Code Extension for Python Markdown +========================================= + +This extension adds Fenced Code Blocks to Python-Markdown. + + >>> import markdown + >>> text = ''' + ... A paragraph before a fenced code block: + ... + ... ~~~ + ... Fenced code block + ... ~~~ + ... ''' + >>> html = markdown.markdown(text, extensions=['fenced_code']) + >>> html + u'

      A paragraph before a fenced code block:\\n

      \\n
      Fenced code block\\n
      '
      +
      +Works with safe_mode also (we check this because we are using the HtmlStash):
      +
      +    >>> markdown.markdown(text, extensions=['fenced_code'], safe_mode='replace')
      +    u'

      A paragraph before a fenced code block:\\n

      \\n
      Fenced code block\\n
      '
      +    
      +Include tilde's in a code block and wrap with blank lines:
      +
      +    >>> text = '''
      +    ... ~~~~~~~~
      +    ... 
      +    ... ~~~~
      +    ... 
      +    ... ~~~~~~~~'''
      +    >>> markdown.markdown(text, extensions=['fenced_code'])
      +    u'
      \\n~~~~\\n\\n
      '
      +
      +Multiple blocks and language tags:
      +
      +    >>> text = '''
      +    ... ~~~~
      +    ... block one
      +    ... ~~~~{.python}
      +    ... 
      +    ... ~~~~
      +    ... 

      block two

      + ... ~~~~{.html}''' + >>> markdown.markdown(text, extensions=['fenced_code']) + u'
      block one\\n
      \\n\\n
      <p>block two</p>\\n
      '
      +
      +"""
      +
      +import markdown, re
      +
      +# Global vars
      +FENCED_BLOCK_RE = re.compile( \
      +    r'(?P^~{3,})[ ]*\n(?P.*?)(?P=fence)[ ]*(\{\.(?P[a-zA-Z0-9_-]*)\})?[ ]*$', 
      +    re.MULTILINE|re.DOTALL
      +    )
      +CODE_WRAP = '
      %s
      '
      +LANG_TAG = ' class="%s"'
      +
      +
      +class FencedCodeExtension(markdown.Extension):
      +
      +    def extendMarkdown(self, md, md_globals):
      +        """ Add FencedBlockPreprocessor to the Markdown instance. """
      +
      +        FENCED_BLOCK_PREPROCESSOR = FencedBlockPreprocessor()
      +        FENCED_BLOCK_PREPROCESSOR.md = md
      +        md.textPreprocessors.insert(0, FENCED_BLOCK_PREPROCESSOR)
      +
      +
      +class FencedBlockPreprocessor(markdown.TextPreprocessor):
      +    
      +    def run(self, text):
      +        """ Match and store Fenced Code Blocks in the HtmlStash. """
      +        while 1:
      +            m = FENCED_BLOCK_RE.search(text)
      +            if m:
      +                lang = ''
      +                if m.group('lang'):
      +                    lang = LANG_TAG % m.group('lang')
      +                code = CODE_WRAP % (lang, self._escape(m.group('code')))
      +                placeholder = self.md.htmlStash.store(code, safe=True)
      +                text = '%s\n%s\n%s'% (text[:m.start()], placeholder, text[m.end():])
      +            else:
      +                break
      +        return text
      +
      +    def _escape(self, txt):
      +        """ basic html escaping """
      +        txt = txt.replace('&', '&')
      +        txt = txt.replace('<', '<')
      +        txt = txt.replace('>', '>')
      +        txt = txt.replace('"', '"')
      +        return txt
      +
      +
      +def makeExtension(configs=None):
      +    return FencedCodeExtension()
      +
      +
      +if __name__ == "__main__":
      +    import doctest
      +    doctest.testmod()
      -- 
      cgit v1.2.3
      
      
      From c0c88a2777a1641d4312b6aafe454f466fe104b2 Mon Sep 17 00:00:00 2001
      From: Waylan Limberg 
      Date: Mon, 12 May 2008 00:52:14 -0400
      Subject: Added Meta-Data and HeaderId extensions. Updated setup.py to include
       all packages extensions.
      
      ---
       mdx_headerid.py | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       mdx_meta.py     |  81 ++++++++++++++++++++++++++
       setup.py        |  12 +++-
       3 files changed, 265 insertions(+), 1 deletion(-)
       create mode 100644 mdx_headerid.py
       create mode 100644 mdx_meta.py
      
      diff --git a/mdx_headerid.py b/mdx_headerid.py
      new file mode 100644
      index 0000000..2e658cd
      --- /dev/null
      +++ b/mdx_headerid.py
      @@ -0,0 +1,173 @@
      +#!/usr/bin/python
      +
      +"""
      +HeaderID Extension for Python-Markdown
      +======================================
      +
      +Adds ability to set HTML IDs for headers.
      +
      +Basic usage:
      +
      +    >>> import markdown
      +    >>> text = "# Some Header # {#some_id}"
      +    >>> md = markdown.markdown(text, ['headerid'])
      +    >>> md
      +    u'

      Some Header

      ' + +All header IDs are unique: + + >>> text = ''' + ... #Header + ... #Another Header {#header} + ... #Third Header {#header}''' + >>> md = markdown.markdown(text, ['headerid']) + >>> md + u'

      Header

      \\n\\n

      Another Header

      \\n\\n

      Third Header

      ' + +To fit within a html template's hierarchy, set the header base level: + + >>> text = ''' + ... #Some Header + ... ## Next Level''' + >>> md = markdown.markdown(text, ['headerid(level=3)']) + >>> md + u'

      Some Header

      \\n\\n

      Next Level

      ' + +Turn off auto generated IDs: + + >>> text = ''' + ... # Some Header + ... # Header with ID # { #foo }''' + >>> md = markdown.markdown(text, ['headerid(forceid=False)']) + >>> md + u'

      Some Header

      \\n\\n

      Header with ID

      ' + +Use with MetaData extension: + + >>> text = '''header_level: 2 + ... header_forceid: Off + ... + ... # A Header''' + >>> md = markdown.markdown(text, ['headerid', 'meta']) + >>> md + u'

      A Header

      ' + +By [Waylan Limberg](http://achinghead.com/). + +Project website: http://achinghead.com/markdown-headerid/ +Contact: waylan [at] gmail [dot] com + +License: [BSD](http://www.opensource.org/licenses/bsd-license.php) + +Version: 0.1 (May 2, 2008) + +Dependencies: +* [Python 2.3+](http://python.org) +* [Markdown 1.7+](http://www.freewisdom.org/projects/python-markdown/) + +""" + +import markdown +import re +from string import ascii_lowercase, digits, punctuation + +ID_CHARS = ascii_lowercase + digits + '-_' + +HEADER_RE = re.compile(r'''^(\#{1,6}) # group(1) = string of hashes + ( [^{^#]*) # group(2) = Header text + [\#]* # optional closing hashes (not counted) + (?:[ \t]*\{[ \t]*\#([-_:a-zA-Z0-9]+)[ \t]*\})? # group(3) = id attr''', + re.VERBOSE) + +IDCOUNT_RE = re.compile(r'^(.*)_([0-9]+)$') + +class HeaderIdExtension (markdown.Extension) : + def __init__(self, configs): + # set defaults + self.config = { + 'level' : ['1', 'Base level for headers.'], + 'forceid' : ['True', 'Force all headers to have an id.'] + } + + for key, value in configs: + self.setConfig(key, value) + + + def extendMarkdown(self, md, md_globals) : + + md.IDs = [] + + def _processHeaderId(parent_elem, paragraph) : + ''' + Overrides _processHeader of Markdown() and + adds an 'id' to the header. + ''' + m = HEADER_RE.match(paragraph[0]) + if m : + start_level, force_id = _get_meta() + level = len(m.group(1)) + start_level + if level > 6: level = 6 + h = md.doc.createElement("h%d" % level) + parent_elem.appendChild(h) + for item in md._handleInline(m.group(2).strip()) : + h.appendChild(item) + if m.group(3) : + h.setAttribute('id', _unique_id(m.group(3))) + elif force_id: + h.setAttribute('id', _create_id(m.group(2).strip())) + else : + message(CRITICAL, "We've got a problem header!") + + md._processHeader = _processHeaderId + + def _get_meta(): + ''' Return meta data suported by this ext as a tuple ''' + level = int(self.config['level'][0]) - 1 + force = _str2bool(self.config['forceid'][0]) + if hasattr(md, 'Meta'): + if md.Meta.has_key('header_level'): + level = int(md.Meta['header_level'][0]) - 1 + if md.Meta.has_key('header_forceid'): + force = _str2bool(md.Meta['header_forceid'][0]) + return level, force + + def _str2bool(s, default=False): + ''' Convert a string to a booleen value. ''' + s = str(s) + if s.lower() in ['0', 'f', 'false', 'off', 'no', 'n']: + return False + elif s.lower() in ['1', 't', 'true', 'on', 'yes', 'y']: + return True + return default + + def _unique_id(id): + ''' Ensure ID is unique. Append '_1', '_2'... if not ''' + while id in md.IDs: + m = IDCOUNT_RE.match(id) + if m: + id = '%s_%d'% (m.group(1), int(m.group(2))+1) + else: + id = '%s_%d'% (id, 1) + md.IDs.append(id) + return id + + + def _create_id(header): + ''' Return ID from Header text. ''' + h = '' + for c in header.lower().replace(' ', '_'): + if c in ID_CHARS: + h += c + elif c not in punctuation: + h += '+' + return _unique_id(h) + + + +def makeExtension(configs=None) : + return HeaderIdExtension(configs=configs) + +if __name__ == "__main__": + import doctest + doctest.testmod() + diff --git a/mdx_meta.py b/mdx_meta.py new file mode 100644 index 0000000..30dea8a --- /dev/null +++ b/mdx_meta.py @@ -0,0 +1,81 @@ +#!usr/bin/python + +''' +Meta Data Extension for Python-Markdown +========================================== + +This extension adds Meta Data handling to markdown. + + >>> import markdown + >>> text = """Title: A Test Doc. + ... Author: Waylan Limberg + ... John Doe + ... Blank_Data: + ... + ... The body. This is paragraph one. + ... """ + >>> md = markdown.Markdown(text, ['meta']) + >>> md.convert() + u'

      The body. This is paragraph one.\\n

      ' + >>> md.Meta + {u'blank_data': [u''], u'author': [u'Waylan Limberg', u'John Doe'], u'title': [u'A Test Doc.']} + +Make sure text without Meta Data still works (markdown < 1.6b returns a

      ). + + >>> text = ' Some Code - not extra lines of meta data.' + >>> md = markdown.Markdown(text, ['meta']) + >>> md.convert() + u'

      Some Code - not extra lines of meta data.\\n
      ' + >>> md.Meta + {} + +''' + +import markdown, re + +# Global Vars +META_RE = re.compile(r'^[ ]{0,3}(?P[A-Za-z0-9_-]+):\s*(?P.*)') +META_MORE_RE = re.compile(r'^[ ]{4,}(?P.*)') + +class MetaExtension (markdown.Extension) : + def __init__(self, configs): + pass + + def extendMarkdown(self, md, md_globals) : + self.md = md + + # Insert meta preprocessor first + META_PREPROCESSOR = MetaPreprocessor() + META_PREPROCESSOR.md = md + md.preprocessors.insert(0, META_PREPROCESSOR) + +class MetaPreprocessor(markdown.Preprocessor) : + def run(self, lines) : + meta = {} + key = None + while 1: + line = lines.pop(0) + if line.strip() == '': + break # blank line - done + m1 = META_RE.match(line) + if m1: + key = m1.group('key').lower().strip() + meta[key] = [m1.group('value').strip()] + else: + m2 = META_MORE_RE.match(line) + if m2 and key: + # Add another line to existing key + meta[key].append(m2.group('value').strip()) + else: + lines.insert(0, line) + break # no meta data - done + self.md.Meta = meta + return lines + + +def makeExtension(configs=None) : + return MetaExtension(configs=configs) + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/setup.py b/setup.py index de36dfc..5596eca 100644 --- a/setup.py +++ b/setup.py @@ -10,5 +10,15 @@ setup( maintainer_email = "waylan [at] gmail.com", url = "http://www.freewisdom.org/projects/python-markdown", license = "BSD License, GNU Public License (GPL)", - py_modules = ["markdown","mdx_footnotes", "mdx_rss"], + py_modules = ["markdown", + "mdx_codehilite", + "mdx_fenced_code", + "mdx_footnotes", + "mdx_headerid", + "mdx_imagelinks", + "mdx_meta", + "mdx_rss", + "mdx_tables", + "mdx_wikilink", + ], ) -- cgit v1.2.3 From a91e308363eeacd77270cdcb3df2109e0cfe2d18 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Wed, 14 May 2008 23:13:18 -0400 Subject: Allow hashes (#) in body of headers -- with tests. Thank you John Szakmeister for the bug report and patch. --- .gitignore | 3 +++ markdown.py | 2 +- tests/misc/headers.html | 4 +++- tests/misc/headers.txt | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 0d20b64..2ac1b58 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ *.pyc +*.bak +*.tmp +tmp/* diff --git a/markdown.py b/markdown.py index acab09a..3348acc 100644 --- a/markdown.py +++ b/markdown.py @@ -1295,7 +1295,7 @@ class CorePatterns: """ patterns = { - 'header': r'(#*)([^#]*)(#*)', # # A title + 'header': r'(#{1,6})[ \t]*(.*?)[ \t]*(#*)', # # A title 'reference-def': r'(\ ?\ ?\ ?)\[([^\]]*)\]:\s*([^ ]*)(.*)', # [Google]: http://www.google.com/ 'containsline': r'([-]*)$|^([=]*)', # -----, =====, etc. diff --git a/tests/misc/headers.html b/tests/misc/headers.html index 61bc266..7041eda 100644 --- a/tests/misc/headers.html +++ b/tests/misc/headers.html @@ -7,4 +7,6 @@

      Markdown

      -

      [5]: http://foo.com/

      \ No newline at end of file +

      [5]: http://foo.com/

      + +

      Issue #1: Markdown

      diff --git a/tests/misc/headers.txt b/tests/misc/headers.txt index 1cb9bc0..2ddb391 100644 --- a/tests/misc/headers.txt +++ b/tests/misc/headers.txt @@ -7,3 +7,5 @@ Line 3 # [Markdown](http://some.link.com/) # [5]: http://foo.com/ + +# Issue #1: Markdown -- cgit v1.2.3 From 80172e52c04683af35ab4dce1f5c285d73754824 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Sun, 18 May 2008 16:05:34 -0400 Subject: The code & pre tags in Fenced Code Blocks are now closed properly. --- mdx_fenced_code.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mdx_fenced_code.py b/mdx_fenced_code.py index 4b0e406..c3d9f7f 100644 --- a/mdx_fenced_code.py +++ b/mdx_fenced_code.py @@ -16,12 +16,12 @@ This extension adds Fenced Code Blocks to Python-Markdown. ... ''' >>> html = markdown.markdown(text, extensions=['fenced_code']) >>> html - u'

      A paragraph before a fenced code block:\\n

      \\n
      Fenced code block\\n
      '
      +    u'

      A paragraph before a fenced code block:\\n

      \\n
      Fenced code block\\n
      ' Works with safe_mode also (we check this because we are using the HtmlStash): >>> markdown.markdown(text, extensions=['fenced_code'], safe_mode='replace') - u'

      A paragraph before a fenced code block:\\n

      \\n
      Fenced code block\\n
      '
      +    u'

      A paragraph before a fenced code block:\\n

      \\n
      Fenced code block\\n
      ' Include tilde's in a code block and wrap with blank lines: @@ -32,7 +32,7 @@ Include tilde's in a code block and wrap with blank lines: ... ... ~~~~~~~~''' >>> markdown.markdown(text, extensions=['fenced_code']) - u'
      \\n~~~~\\n\\n
      '
      +    u'
      \\n~~~~\\n\\n
      ' Multiple blocks and language tags: @@ -45,7 +45,7 @@ Multiple blocks and language tags: ...

      block two

      ... ~~~~{.html}''' >>> markdown.markdown(text, extensions=['fenced_code']) - u'
      block one\\n
      \\n\\n
      <p>block two</p>\\n
      '
      +    u'
      block one\\n
      \\n\\n
      <p>block two</p>\\n
      ' """ @@ -56,7 +56,7 @@ FENCED_BLOCK_RE = re.compile( \ r'(?P^~{3,})[ ]*\n(?P.*?)(?P=fence)[ ]*(\{\.(?P[a-zA-Z0-9_-]*)\})?[ ]*$', re.MULTILINE|re.DOTALL ) -CODE_WRAP = '
      %s
      '
      +CODE_WRAP = '
      %s
      ' LANG_TAG = ' class="%s"' -- cgit v1.2.3