From 953efa7c35a96394b308e136f9b205b97058e067 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 27 Mar 2017 09:17:49 +0100 Subject: [PATCH] Add skeleton of mvp code --- src/build/cli.py | 5 +++++ src/build/cover.py | 9 +++++++++ src/build/md.py | 11 +++++++++++ src/build/pandoc.py | 25 +++++++++++++++++++++++++ src/build/pdf.py | 31 +++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 src/build/cover.py create mode 100644 src/build/md.py create mode 100644 src/build/pandoc.py create mode 100644 src/build/pdf.py diff --git a/src/build/cli.py b/src/build/cli.py index e69de29..4c68dd7 100644 --- a/src/build/cli.py +++ b/src/build/cli.py @@ -0,0 +1,5 @@ +import click + +@click.command('build', short_help="Build document") +def cli(): + pass diff --git a/src/build/cover.py b/src/build/cover.py new file mode 100644 index 0000000..13e1899 --- /dev/null +++ b/src/build/cover.py @@ -0,0 +1,9 @@ +from jinja2 import Template + +def render_cover(context): + with open("cover-template.html") as f: + template = Template(f.read()) + with open("cover.html", "w") as f: + cover = template.render(context) + f.write(cover) + return cover diff --git a/src/build/md.py b/src/build/md.py new file mode 100644 index 0000000..07188ad --- /dev/null +++ b/src/build/md.py @@ -0,0 +1,11 @@ +import glob + +def get_files_content(filenames): + for filename in filenames: + with open(filename) as f: + yield f.read() + + +def read_files(files_glob): + filenames = glob.glob(files_glob) + return '\n'.join(list(get_files_content(filenames))) diff --git a/src/build/pandoc.py b/src/build/pandoc.py new file mode 100644 index 0000000..9080b10 --- /dev/null +++ b/src/build/pandoc.py @@ -0,0 +1,25 @@ +import pypandoc +from bs4 import BeautifulSoup + + +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) + 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' + ] + ) + + return fix_references_title(html) diff --git a/src/build/pdf.py b/src/build/pdf.py new file mode 100644 index 0000000..fecb86f --- /dev/null +++ b/src/build/pdf.py @@ -0,0 +1,31 @@ +import pdfkit + +pdf_options = { + "quiet": "", + "no-pdf-compression": "", + + "margin-top": '0.6in', + "margin-bottom": '0.6in', + "margin-left": '0.4in', + "margin-right": '0.4in', + + "header-html": "header.html", + "footer-html": "footer.html", + "footer-spacing": 5, + "header-spacing": 5, + + "title": "Title thing", + "replace": [ + + ] +} + + +def export_pdf(content): + return pdfkit.from_string( + content, + 'out.pdf', + options=pdf_options, + css="style.css", + cover="cover.html" + )