Skip to content

Instantly share code, notes, and snippets.

@bpj
Last active May 23, 2016 17:19
Show Gist options
  • Save bpj/dc09a4943bc51ca64327c65623a121e9 to your computer and use it in GitHub Desktop.
Save bpj/dc09a4943bc51ca64327c65623a121e9 to your computer and use it in GitHub Desktop.
Pandoc filter which fakes sans and small-caps syntax by overloading link syntax
#!/usr/bin/env python
"""
Pandoc filter which fakes sans and small-caps syntax through
overloading link syntax. It wraps the text of links with the
pseudo-urls ``-sf`` and ``-sc`` in HTML spans with the classes
"sans" and "small-caps" or LaTeX ``\textsf{...}`` and ``\textsc{...}``
commands::
[this is sans text](-sf)
[this is small-caps text](-sc)
<p><span class="sans">this is sans text</span>
<span class="small-caps">this is small-caps text</span></p>
\\textsf{this is sans text}
\\textsc{this is small-caps text}
Limitation: you can't have links inside such text, or such text inside link text!
"""
import pandocfilters as pf
wrapper = lambda f, s: [ pf.RawInline(f, s) ]
span_wrapper = lambda c: [ pf.RawInline('html', r'<span class="{0}">'.format(c)) ]
cmd_wrapper = lambda c: [ pf.RawInline('latex', r'\{0}{{'.format(c)) ]
wrap = {
'html': {
'-sf': span_wrapper('sans'),
'-sc': span_wrapper('small-caps'),
'end': wrapper('html', r'</span>'),
},
'latex': {
'-sf': cmd_wrapper('textsf'),
'-sc': cmd_wrapper('textsc'),
'end': wrapper('latex', r'}'),
},
}
def filter_func(key,val,fmt,meta):
if not fmt in wrap:
return None
if 'Link' != key:
return None
(attr, content, target) = val
(url, title) = target
if not url in wrap[fmt] or 'end' == url:
return None
return wrap[fmt][url] + content + wrap[fmt]['end']
if __name__ == "__main__":
pf.toJSONFilter(filter_func)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment