3
\ @ s d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z m
Z
dd lZdd lZdd l
Z
ejd
ZejdZdd
Zdd Zdd Zdd ZG dd deZG dd deZdddZd S )a
HeaderID Extension for Python-Markdown
======================================
Auto-generate id attributes for HTML headers.
Basic usage:
>>> import markdown
>>> text = "# Some Header #"
>>> md = markdown.markdown(text, ['headerid'])
>>> print md
All header IDs are unique:
>>> text = '''
... #Header
... #Header
... #Header'''
>>> md = markdown.markdown(text, ['headerid'])
>>> print md
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)'])
>>> print md
Next Level
Works with inline markup.
>>> text = '#Some *Header* with [markup](http://example.com).'
>>> md = markdown.markdown(text, ['headerid'])
>>> print md
Turn off auto generated IDs:
>>> text = '''
... # Some Header
... # Another Header'''
>>> md = markdown.markdown(text, ['headerid(forceid=False)'])
>>> print md
Some Header
Another Header
Use with MetaData extension:
>>> text = '''header_level: 2
... header_forceid: Off
...
... # A Header'''
>>> md = markdown.markdown(text, ['headerid', 'meta'])
>>> print md
A Header
Copyright 2007-2011 [Waylan Limberg](http://achinghead.com/).
Project website:
Contact: markdown@freewisdom.org
License: BSD (see ../docs/LICENSE for details)
Dependencies:
* [Python 2.3+](http://python.org)
* [Markdown 2.0+](http://packages.python.org/Markdown/)
)absolute_import)unicode_literals ) Extension )
Treeprocessor)HTML_PLACEHOLDER_REparseBoolValueNMARKDOWNz^(.*)_([0-9]+)$c C sB t jd| jdd} tjdd| jdj j } tjd| || S )z, Slugify a string, to make it URL friendly. ZNFKDasciiignorez[^\w\s-] z[%s\s]+)unicodedata normalizeencoderesubdecodestriplower)value separator r )build/lib/markdown/extensions/headerid.pyslugify[ s r c C s^ xN| |ks| rNt j| }|r@d|jdt|jdd f } qd| df } qW |j| | S )z@ Ensure id is unique in set of ids. Append '_1', '_2'... if not z%s_%dr r )
IDCOUNT_REmatchgroupintadd)ididsmr r r uniqueb s
"
r# c c sF | j r| j V x2| D ]*}xt|D ]
}|V q"W |jr|jV qW dS )z Loop through all children and return text only.
Reimplements method of same name added to ElementTree in Python 2.7
N)textitertexttail)elemesr r r r% n s
r% c s fdd}t j|| S )zC Extract raw HTML, reduce to plain text and swap with placeholder. c s\ y j jt| jd \}}W n ttfk
r< | jdS X jrN| rNdS tjdd|S )z& Substitute raw html with plain text. r r r
z(<[^>]+>)|(&[\#a-zA-Z0-9]+;)) htmlStash
rawHtmlBlocksr r
IndexError TypeErrorsafeModer r )r" rawsafe)mdr r _html_sub s z#stashedHTML2text.._html_sub)r r )r$ r1 r2 r )r1 r stashedHTML2text} s r3 c @ s&