-
-
Save momikey/9b0d5d52d84704d264eb59df9297a13f to your computer and use it in GitHub Desktop.
Pandoc filters for Memoir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Pandoc filter that replaces link elements with their targets in parentheses. | |
The link address will be written directly after the alt text, as preformatted text. | |
""" | |
from pandocfilters import toJSONFilter, Str, Space, Code | |
import sys | |
def action(k, v, fmt, meta): | |
if k == 'Link': | |
linktext = Code(('',[],[]), v[2][0]) | |
writeout = [Space(), Str('('), linktext, Str(')')] | |
return v[1] + writeout | |
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