archive
/
md-pdf
Archived
1
Fork 0
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

52 lines
1.3 KiB
Python

import pypandoc
from bs4 import BeautifulSoup
import os
from md_pdf.consts import PROJECT_DIR, CSL_DIR
from jinja2 import Template
CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl')
def fix_references_title(content):
soup = BeautifulSoup(content, 'html.parser')
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 output_html(html, out_dir):
with open(os.path.join(out_dir, 'output.html'), 'w') as f:
f.write(html)
def parse_template(html, context):
template = Template(html)
return template.render(context)
def build_document(files_content, bibliography, context):
args = [
'-s',
]
filters = []
if bibliography is not None:
args += [
'--bibliography={}'.format(os.path.abspath(bibliography['references'])),
'--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography['csl'])))
]
filters.append('pandoc-citeproc')
html = fix_references_title(pypandoc.convert_text(
files_content,
'html',
format='md',
extra_args=args,
filters=filters
))
return parse_template(html, context)