This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
md-pdf/md_pdf/build/pandoc.py

55 lines
1.5 KiB
Python
Raw Normal View History

2017-03-27 09:17:49 +01:00
import pypandoc
from bs4 import BeautifulSoup
2017-03-30 08:44:50 +01:00
import os
2017-04-01 16:12:03 +01:00
from md_pdf.consts import CSL_DIR
2017-03-30 18:01:48 +01:00
from jinja2 import Template
2017-04-01 16:12:03 +01:00
import logging
logger = logging.getLogger(__file__)
2017-03-27 09:17:49 +01:00
def fix_references_title(content):
2017-04-04 22:04:25 +01:00
logger.debug("Adding Reference Title...")
2017-03-27 09:17:49 +01:00
soup = BeautifulSoup(content, 'html.parser')
2017-03-28 09:21:06 +01:00
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)
2017-03-27 09:17:49 +01:00
return soup.prettify()
2017-03-28 21:59:11 +01:00
def output_html(html, out_dir):
2017-04-04 22:04:25 +01:00
logger.info("Outputting HTML...")
2017-03-28 21:59:11 +01:00
with open(os.path.join(out_dir, 'output.html'), 'w') as f:
f.write(html)
2017-03-30 18:01:48 +01:00
def parse_template(html, context):
2017-04-04 22:04:25 +01:00
logger.debug("Rendering Template...")
2017-03-30 18:01:48 +01:00
template = Template(html)
return template.render(context)
def build_document(files_content, bibliography, context):
2017-03-28 09:21:06 +01:00
args = [
'-s',
]
filters = []
if bibliography is not None:
args += [
2017-03-30 18:26:06 +01:00
'--bibliography={}'.format(os.path.abspath(bibliography['references'])),
'--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography['csl'])))
2017-03-27 09:17:49 +01:00
]
2017-03-28 09:21:06 +01:00
filters.append('pandoc-citeproc')
2017-04-04 22:04:25 +01:00
logger.info("Rendering Document...")
2017-03-30 18:01:48 +01:00
html = fix_references_title(pypandoc.convert_text(
2017-03-30 09:00:42 +01:00
files_content,
'html',
format='md',
2017-03-28 09:21:06 +01:00
extra_args=args,
filters=filters
2017-03-30 18:01:48 +01:00
))
2017-03-27 09:17:49 +01:00
2017-03-30 18:26:06 +01:00
return parse_template(html, context)