def read_file(filename, fmt=None, settings=None): """Return a reader object using the given format.""" if not fmt: fmt = filename.split('.')[-1] if fmt not in _EXTENSIONS: raise TypeError('Pelican does not know how to parse %s' % filename) reader = _EXTENSIONS[fmt](settings) settings_key = '%s_EXTENSIONS' % fmt.upper() if settings and settings_key in settings: reader.extensions = settings[settings_key] if not reader.enabled: raise ValueError("Missing dependencies for %s" % fmt) content, metadata = reader.read(filename) # eventually filter the content with typogrify if asked so if settings and settings['TYPOGRIFY']: from typogrify import Typogrify content = Typogrify.typogrify(content) metadata['title'] = Typogrify.typogrify(metadata['title']) return content, metadata
def caps(text): """Wraps multiple capital letters in ``<span class="caps">`` so they can be styled with CSS. >>> caps("A message from KU") u'A message from <span class="caps">KU</span>' Uses the smartypants tokenizer to not screw with HTML or with tags it shouldn't. >>> caps("<PRE>CAPS</pre> more CAPS") u'<PRE>CAPS</pre> more <span class="caps">CAPS</span>' >>> caps("A message from 2KU2 with digits") u'A message from <span class="caps">2KU2</span> with digits' >>> caps("Dotted caps followed by spaces should never include them in the wrap D.O.T. like so.") u'Dotted caps followed by spaces should never include them in the wrap <span class="caps">D.O.T.</span> like so.' All caps with with apostrophes in them shouldn't break. Only handles dump apostrophes though. >>> caps("JIMMY'S") u'<span class="caps">JIMMY\\'S</span>' >>> caps("<i>D.O.T.</i>HE34T<b>RFID</b>") u'<i><span class="caps">D.O.T.</span></i><span class="caps">HE34T</span><b><span class="caps">RFID</span></b>' """ return Typogrify.caps(text)
def amp(text): """Wraps apersands in HTML with ``<span class="amp">`` so they can be styled with CSS. Apersands are also normalized to ``&``. Requires ampersands to have whitespace or an `` `` on both sides. >>> amp('One & two') u'One <span class="amp">&</span> two' >>> amp('One & two') u'One <span class="amp">&</span> two' >>> amp('One & two') u'One <span class="amp">&</span> two' >>> amp('One & two') u'One <span class="amp">&</span> two' It won't mess up & that are already wrapped, in entities or URLs >>> amp('One <span class="amp">&</span> two') u'One <span class="amp">&</span> two' >>> amp('“this” & <a href="/?that&test">that</a>') u'“this” <span class="amp">&</span> <a href="/?that&test">that</a>' It should ignore standalone amps that are in attributes >>> amp('<link href="xyz.html" title="One & Two">xyz</link>') u'<link href="xyz.html" title="One & Two">xyz</link>' """ return Typogrify.amp(text)
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') u'The “Green” man' """ return Typogrify.smartypants(text)
def titlecase(text): """Support for titlecase.py's titlecasing >>> titlecase("this V that") u'This v That' >>> titlecase("this is just an example.com") u'This Is Just an example.com' """ return Typogrify.titlecase(text)
def typogrify(text): """The super typography filter Applies the following filters: widont, smartypants, caps, amp, initial_quotes >>> typogrify('<h2>"Jayhawks" & KU fans act extremely obnoxiously</h2>') Markup(u'<h2><span class="dquo">“</span>Jayhawks” <span class="amp">&</span> <span class="caps">KU</span> fans act extremely obnoxiously</h2>') Each filters properly handles autoescaping. >>> jinja2.escape(typogrify('<h2>"Jayhawks" & KU fans act extremely obnoxiously</h2>')) Markup(u'<h2><span class="dquo">“</span>Jayhawks” <span class="amp">&</span> <span class="caps">KU</span> fans act extremely obnoxiously</h2>') """ return Typogrify.typogrify(text)
def initial_quotes(text): """Wraps initial quotes in ``class="dquo"`` for double quotes or ``class="quo"`` for single quotes. Works in these block tags ``(h1-h6, p, li, dt, dd)`` and also accounts for potential opening inline elements ``a, em, strong, span, b, i`` >>> initial_quotes('"With primes"') u'<span class="dquo">"</span>With primes"' >>> initial_quotes("'With single primes'") u'<span class="quo">\\'</span>With single primes\\'' >>> initial_quotes('<a href="#">"With primes and a link"</a>') u'<a href="#"><span class="dquo">"</span>With primes and a link"</a>' >>> initial_quotes('“With smartypanted quotes”') u'<span class="dquo">“</span>With smartypanted quotes”' """ return Typogrify.initial_quotes(text)
def widont(text): """Replaces the space between the last two words in a string with `` `` Works in these block tags ``(h1-h6, p, li, dd, dt)`` and also accounts for potential closing inline elements ``a, em, strong, span, b, i`` >>> widont('A very simple test') u'A very simple test' Single word items shouldn't be changed >>> widont('Test') u'Test' >>> widont(' Test') u' Test' >>> widont('<ul><li>Test</p></li><ul>') u'<ul><li>Test</p></li><ul>' >>> widont('<ul><li> Test</p></li><ul>') u'<ul><li> Test</p></li><ul>' >>> widont('<p>In a couple of paragraphs</p><p>paragraph two</p>') u'<p>In a couple of paragraphs</p><p>paragraph two</p>' >>> widont('<h1><a href="#">In a link inside a heading</i> </a></h1>') u'<h1><a href="#">In a link inside a heading</i> </a></h1>' >>> widont('<h1><a href="#">In a link</a> followed by other text</h1>') u'<h1><a href="#">In a link</a> followed by other text</h1>' Empty HTMLs shouldn't error >>> widont('<h1><a href="#"></a></h1>') u'<h1><a href="#"></a></h1>' >>> widont('<div>Divs get no love!</div>') u'<div>Divs get no love!</div>' >>> widont('<pre>Neither do PREs</pre>') u'<pre>Neither do PREs</pre>' >>> widont('<div><p>But divs with paragraphs do!</p></div>') u'<div><p>But divs with paragraphs do!</p></div>' """ return Typogrify.widont(text)
def ligature(text): """ Replaces ff, fi, fl, ffi, and ffl with ligatures. """ return Typogrify.ligature(text)
def typogrify_no_widont(value): value = Typogrify.amp(value) value = Typogrify.smartypants(value) value = Typogrify.caps(value) value = Typogrify.initial_quotes(value) return value
def run(content): return Typogrify.typogrify(content)