From 6075d540d81d8a8053be940ec0ff106c81c210be Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 28 Mar 2017 09:21:06 +0100 Subject: [PATCH] Render md using pandoc with references --- md_pdf/assets/harverd.csl | 658 ++++++++++++++++++++++++++++++++++++++ md_pdf/build/cli.py | 9 +- md_pdf/build/pandoc.py | 35 +- test-files/bib.yaml | 0 test-files/test.md | 1 + 5 files changed, 689 insertions(+), 14 deletions(-) create mode 100644 md_pdf/assets/harverd.csl create mode 100644 test-files/bib.yaml create mode 100644 test-files/test.md diff --git a/md_pdf/assets/harverd.csl b/md_pdf/assets/harverd.csl new file mode 100644 index 0000000..23d6744 --- /dev/null +++ b/md_pdf/assets/harverd.csl @@ -0,0 +1,658 @@ + + diff --git a/md_pdf/build/cli.py b/md_pdf/build/cli.py index 0eb2ee2..29f70c9 100644 --- a/md_pdf/build/cli.py +++ b/md_pdf/build/cli.py @@ -1,9 +1,14 @@ import click from md_pdf.build.md import read_files +from md_pdf.build.pandoc import build_document + @click.command('build', short_help="Build document") @click.argument('in_files', type=click.Path(dir_okay=False, resolve_path=True, writable=True), nargs=-1) +@click.option('--bibliography', '-b', type=click.Path(dir_okay=False, resolve_path=True, writable=True), default=None) @click.option('--output', '-o', type=click.Path(file_okay=False, resolve_path=True, writable=True), default='out/') -def cli(in_files, output): +def cli(in_files, bibliography, output): data = read_files(in_files) - print(data) + doc = build_document(data, bibliography) + print(doc) + diff --git a/md_pdf/build/pandoc.py b/md_pdf/build/pandoc.py index 9080b10..bfc8826 100644 --- a/md_pdf/build/pandoc.py +++ b/md_pdf/build/pandoc.py @@ -1,25 +1,36 @@ import pypandoc from bs4 import BeautifulSoup +import os.path +from md_pdf.utils import PROJECT_DIR +CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl') + def fix_references_title(content): soup = BeautifulSoup(content, 'html.parser') - title = soup.new_tag('h1') - title.string = "References" - soup.find('div', class_="references").insert_before(title) + reference_element = soup.find('div', class_='references') + if reference_element is not None: + title = soup.new_tag('h1') + title.string = "References" + reference_element.insert_before(title) return soup.prettify() -def build_document(files_content): - html = pypandoc.convert_text(files_content, 'html', format='md', - extra_args=[ - '-s', - '--bibliography=bib.yml', - '--csl=harvard.csl' - ], - filters=[ - 'pandoc-citeproc' +def build_document(files_content, bibliography): + args = [ + '-s', + ] + filters = [] + if bibliography is not None: + args += [ + '--bibliography={}'.format(bibliography), + '--csl={}'.format(CSL_FILE) ] + filters.append('pandoc-citeproc') + + html = pypandoc.convert_text(files_content, 'html', format='md', + extra_args=args, + filters=filters ) return fix_references_title(html) diff --git a/test-files/bib.yaml b/test-files/bib.yaml new file mode 100644 index 0000000..e69de29 diff --git a/test-files/test.md b/test-files/test.md new file mode 100644 index 0000000..8ae0569 --- /dev/null +++ b/test-files/test.md @@ -0,0 +1 @@ +# Test