ó Šç\c@@sþdZddlmZddlmZddlmZddlmZddlm Z m Z dd l Z dd l Z dd l Z e jd ƒZe jd ƒZd „Zd „Zd„Zd„Zdefd„ƒYZdefd„ƒYZd d„Zd S(uŽ 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

Some Header

All header IDs are unique: >>> text = ''' ... #Header ... #Header ... #Header''' >>> md = markdown.markdown(text, ['headerid']) >>> print md

Header

Header

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)']) >>> print md

Some Header

Next Level

Works with inline markup. >>> text = '#Some *Header* with [markup](http://example.com).' >>> md = markdown.markdown(text, ['headerid']) >>> print md

Some Header with markup.

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/) i(tabsolute_import(tunicode_literalsi(t Extensioni(t Treeprocessor(tHTML_PLACEHOLDER_REtparseBoolValueNuMARKDOWNu^(.*)_([0-9]+)$cC@s_tjd|ƒjddƒ}tjdd|jdƒƒjƒjƒ}tjd|||ƒS(u, Slugify a string, to make it URL friendly. uNFKDuasciiuignoreu[^\w\s-]uu[%s\s]+(t unicodedatat normalizetencodetretsubtdecodetstriptlower(tvaluet separator((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pytslugify[s*cC@sxk||ks| rmtj|ƒ}|rZd|jdƒt|jdƒƒdf}qd|df}qW|j|ƒ|S(u@ Ensure id is unique in set of ids. Append '_1', '_2'... if not u%s_%dii(t IDCOUNT_REtmatchtgrouptinttadd(tidtidstm((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pytuniquebs/ cc@sY|jr|jVnx>|D]6}xt|ƒD] }|Vq.W|jr|jVqqWdS(uˆ Loop through all children and return text only. Reimplements method of same name added to ElementTree in Python 2.7 N(ttexttitertextttail(telemtets((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pyRns     c@s‡fd†}tj||ƒS(uC Extract raw HTML, reduce to plain text and swap with placeholder. c@sty)ˆjjt|jdƒƒ\}}Wn!ttfk rL|jdƒSXˆjra| radStjdd|ƒS(u& Substitute raw html with plain text. iiuu(<[^>]+>)|(&[\#a-zA-Z0-9]+;)( t htmlStasht rawHtmlBlocksRRt IndexErrort TypeErrortsafeModeR R (Rtrawtsafe(tmd(s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pyt _html_subs)(RR (RR'R(((R's@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pytstashedHTML2text}s tHeaderIdTreeprocessorcB@s)eZdZeƒZd„Zd„ZRS(u Assign IDs to headers. c C@s|jƒ\}}|jd}|jd}xÞ|jƒD]Ð}|jdkr9|rÄd |jkru|jd ƒ}n0td jt|ƒƒ|j ƒ}|||ƒ}|j d t ||j ƒƒn|r t |jd ƒ|}|d kröd }nd ||_q q9q9WdS(Nuslugifyu separatoruh1uh2uh3uh4uh5uh6uiduiÿÿÿÿiuh%d(uh1uh2uh3uh4uh5uh6(t _get_metatconfigt getiteratorttagtattribtgetR)tjoinRR'tsetRtIDsR( tselftdoct start_leveltforce_idRtsepRRtlevel((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pytrun’s   !  cC@s«t|jdƒd}t|jdƒ}t|jdƒr¡d|jjkrot|jjddƒd}nd|jjkr¡t|jjddƒ}q¡n||fS(u2 Return meta data suported by this ext as a tuple uleveliuforceiduMetau header_leveliuheader_forceid(RR,RthasattrR'tMeta(R4R9tforce((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pyR+¦s! (t__name__t __module__t__doc__R2R3R:R+(((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pyR*s  tHeaderIdExtensioncB@s#eZd„Zd„Zd„ZRS(cC@shiddgd6ddgd6ddgd 6td gd 6|_x$|D]\}}|j||ƒqDWdS( Nu1uBase level for headers.uleveluTrueu Force all headers to have an id.uforceidu-uWord separator.u separatoruCallable to generate anchorsuslugify(RR,t setConfig(R4tconfigstkeyR((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pyt__init__³s   cC@s…|j|ƒtƒ|_||j_|jƒ|j_d|jjƒkrh|jjd|jdƒn|jjd|jdƒdS(Nu attr_listuheaderidu >attr_listu >prettify( tregisterExtensionR*t processorR't getConfigsR,ttreeprocessorstkeysR(R4R't md_globals((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pytextendMarkdown¿s   cC@stƒ|j_dS(N(R2RGR3(R4((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pytresetËs(R>R?RERLRM(((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pyRA²s cC@s td|ƒS(NRC(RA(RC((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pyt makeExtensionÏs(R@t __future__RRtRRIRtutilRRR tloggingRt getLoggertloggertcompileRRRRR)R*RAtNoneRN(((s@/usr/lib/python2.7/site-packages/markdown/extensions/headerid.pytKs"      %