Skip to content

Instantly share code, notes, and snippets.

@kbro237
Created January 2, 2017 13:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbro237/7937d04120ac27fcfb1955ae15773b05 to your computer and use it in GitHub Desktop.
Save kbro237/7937d04120ac27fcfb1955ae15773b05 to your computer and use it in GitHub Desktop.
pandoc filter to change the date format of metadata
#!/usr/bin/env python3
"""
Pandoc filter to change the date formatting in metadata from
whatever it is to whatever strftime format you specify. This
allows you to keep your source documents in one format, but
output to another format.
"""
from panflute import run_filter, MetaMap, Str
from dateutil import parser
from datetime import datetime
def make_pretty(elem, doc):
if type(elem) == Str:
dt = parser.parse(elem.text)
pretty_date = dt.strftime('%B %d, %Y')
return Str(text=pretty_date)
def second_date(elem, doc):
if isinstance(elem, MetaMap) and 'date' in elem.content:
elem.content['date'].walk(make_pretty, doc)
def main(doc=None):
return run_filter(second_date, doc=doc)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment