Last active
November 7, 2017 14:06
-
-
Save greut/a0963a7f09c6d3a13071b8a1278931fb to your computer and use it in GitHub Desktop.
pandoc filter for the french
This file contains hidden or 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 python3 | |
""" | |
Convert the CodeBlock into english text. Very useful if the base language adds | |
some special magic rules which are altering the context. E.g. french puts a | |
space before the colon (:). | |
:: | |
$ pandoc \ | |
--filter ./code-block-in-english.py \ | |
... | |
If the ``otherlangs`` properties is set, make sure it contains "en" already. | |
:: | |
lang: fr | |
otherlangs: [en] | |
""" | |
from pandocfilters import (elt, toJSONFilter, Para, RawBlock, CodeBlock, #noqa | |
RawInline, Code, Str) | |
# those are not present in pandocfilters. | |
MetaInlines = elt('MetaInlines', 1) | |
MetaList = elt('MetaList', 1) | |
def other(lang, full_lang): | |
""" | |
Create a filter that changes the language before a CodeBlock or a RawBlock. | |
""" | |
before = RawInline('tex', | |
r'\begin{{otherlanguage}}{{{0}}}'.format(full_lang)) | |
after = RawInline('tex', r'\end{otherlanguage}') | |
def fn(key, value, format, meta): | |
"""The filter.""" | |
# Adds the otherlangs properties if not found. | |
if 'otherlangs' not in meta: | |
meta['otherlangs'] = MetaList([MetaInlines([Str(lang)])]) | |
if key in ('CodeBlock', 'RawBlock'): | |
# Uses CodeBlock and RawBlock, hence the #noqa above. | |
block = globals()[key](*value) | |
return [Para([before]), block, Para([after])] | |
if key == 'Code': | |
return [before, Code(*value), after] | |
return fn | |
if __name__ == "__main__": | |
toJSONFilter(other('en', 'english')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment