2017-03-27 09:17:49 +01:00
|
|
|
import pypandoc
|
|
|
|
from bs4 import BeautifulSoup
|
2017-03-30 08:44:50 +01:00
|
|
|
import os
|
|
|
|
from md_pdf.consts import PROJECT_DIR, CSL_DIR
|
2017-03-27 09:17:49 +01:00
|
|
|
|
|
|
|
|
2017-03-28 09:21:06 +01:00
|
|
|
CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl')
|
|
|
|
|
2017-03-27 09:17:49 +01:00
|
|
|
def fix_references_title(content):
|
|
|
|
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):
|
|
|
|
with open(os.path.join(out_dir, 'output.html'), 'w') as f:
|
|
|
|
f.write(html)
|
|
|
|
|
|
|
|
|
2017-03-30 08:44:50 +01:00
|
|
|
def build_document(files_content, bibliography):
|
2017-03-28 09:21:06 +01:00
|
|
|
args = [
|
|
|
|
'-s',
|
|
|
|
]
|
|
|
|
filters = []
|
|
|
|
if bibliography is not None:
|
|
|
|
args += [
|
2017-03-30 08:44:50 +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')
|
|
|
|
|
|
|
|
html = pypandoc.convert_text(files_content, 'html', format='md',
|
|
|
|
extra_args=args,
|
|
|
|
filters=filters
|
2017-03-27 09:17:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return fix_references_title(html)
|