Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@momikey
Created March 27, 2017 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save momikey/9b0d5d52d84704d264eb59df9297a13f to your computer and use it in GitHub Desktop.
Save momikey/9b0d5d52d84704d264eb59df9297a13f to your computer and use it in GitHub Desktop.
Pandoc filters for Memoir
#!/usr/bin/env python
"""
Pandoc filter to turn the first letter of a new chapter into a raised initial,
and the rest of the word into small caps, using the lettrine package.
"""
from pandocfilters import toJSONFilter, walk, RawInline, Header, Para, Str, Quoted
import sys
firstpara = False
backmatter = False
endquote = RawInline('latex', "''")
def lettrine(v, withquote=False):
if withquote == True:
latex = RawInline('latex', '\\lettrine[lines=2,ante=``]{%s}{%s}' % (v['c'][0], v['c'][1:]))
return latex
else:
latex = RawInline('latex', '\\lettrine[lines=2]{%s}{%s}' % (v['c'][0], v['c'][1:]))
return latex
def action(k, v, fmt, meta):
global firstpara
global backmatter
if k == 'RawInline' and v[1].find('backmatter') != -1:
# I don't really like raised initials/dropcaps in the back sections,
# so I'll disable them once I see "\backmatter".
backmatter = True
if k == 'Header' and v[0] == 1:
firstpara = True
return None
elif firstpara == True:
# We add the lettrine markup to the 1st paragraph after a Heading 1
if k == 'Para':
firstpara = False
if v[0]['t'] == 'Quoted':
# If the paragraph starts with a quotation mark,
# things get a bit more complicated.
# These two lines break up Pandoc's "Quoted" structure
# and add in the close quote manually.
# Hacky, but it works.
inner = v[0]['c'][1]
par = Para([lettrine(inner[0], True)] + inner[1:] + [endquote] + v[1:])
else:
par = Para([lettrine(v[0])] + v[1:])
return par
else:
return None
else:
firstpara = False
return None
if __name__ == "__main__":
toJSONFilter(action)
sys.stdout.flush()
#!/usr/bin/env python
"""
Pandoc filter to convert horizontal rules (* * *)
to the "plain or fancy break" used by the LaTeX memoir class
"""
from pandocfilters import toJSONFilter, RawBlock, HorizontalRule
import sys
def action(k, v, fmt, meta):
if k == 'HorizontalRule':
return RawBlock('latex', '\\pfbreak')
if __name__ == "__main__":
toJSONFilter(action)
sys.stdout.flush()
#!/usr/bin/env python
"""
Pandoc filter to turn the first letter of a new chapter into a raised initial,
and the rest of the word into small caps, using the lettrine package.
"""
from pandocfilters import toJSONFilter, walk, RawInline, Header, Para, Str, Quoted
import sys
firstpara = False
backmatter = False
endquote = RawInline('latex', "''")
def lettrine(v, withquote=False):
latex = RawInline('latex', '\\lettrine[lines=1]{%s}{%s}' % (v['c'][0], v['c'][1:]))
return latex
def action(k, v, fmt, meta):
global firstpara
global backmatter
if k == 'RawInline' and v[1].find('backmatter') != -1:
# I don't really like raised initials/dropcaps in the back sections,
# so I'll disable them once I see "\backmatter".
backmatter = True
if k == 'Header' and v[0] == 1 and not backmatter:
firstpara = True
return None
elif firstpara == True:
# We add the lettrine markup to the 1st paragraph after a Heading 1
if k == 'Para':
firstpara = False
if v[0]['t'] == 'Quoted':
# If the paragraph starts with a quotation mark,
# things get a bit more complicated.
# These two lines break up Pandoc's "Quoted" structure
# and add in the close quote manually.
# Hacky, but it works.
inner = v[0]['c'][1]
par = Para([lettrine(inner[0])] + inner[1:] + [endquote] + v[1:])
else:
par = Para([lettrine(v[0])] + v[1:])
return par
else:
return None
else:
firstpara = False
return None
if __name__ == "__main__":
toJSONFilter(action)
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment