Merge remote-tracking branch 'origin' into documentation

This commit is contained in:
Jake Howard 2017-05-10 20:11:15 +01:00
commit d4fc5672ce
6 changed files with 76 additions and 26 deletions

View file

@ -1,3 +1,6 @@
$image-spacing: 30px;
body.cover { body.cover {
margin: 0 auto; margin: 0 auto;
text-align: center; text-align: center;
@ -16,7 +19,20 @@ body.cover {
} }
} }
body, html {
body.content {
line-height: 1.5; line-height: 1.5;
font-size: 12px; font-size: 12px;
img {
margin-top: $image-spacing;
width: 100%;
}
p.caption {
margin: 1px 5px $image-spacing;
padding: 0;
font-style: italic;
}
} }

View file

@ -3,6 +3,7 @@ from md_pdf.build.pandoc import build_document, output_html
from md_pdf.build.cover import render_cover from md_pdf.build.cover import render_cover
from md_pdf.build.css import render_css from md_pdf.build.css import render_css
from md_pdf.build.pdf import export_pdf from md_pdf.build.pdf import export_pdf
from md_pdf.build.template import parse_template
import os import os
import logging import logging
@ -13,9 +14,10 @@ def build(config):
logger.debug("Starting Build...") logger.debug("Starting Build...")
data = read_files(os.path.abspath(config['input'])) data = read_files(os.path.abspath(config['input']))
doc = build_document(data, config.get('bibliography'), config.get('context')) doc = build_document(data, config.get('bibliography'), config.get('context'))
parsed_template = parse_template(doc, config)
if 'html' in config['output_formats']: if 'html' in config['output_formats']:
output_html(doc, os.path.abspath(config['output_dir'])) output_html(parsed_template, os.path.abspath(config['output_dir']))
if 'pdf' in config['output_formats']: if 'pdf' in config['output_formats']:
render_cover(config) render_cover(config)
render_css() render_css()
export_pdf(doc, config) export_pdf(parsed_template, config)

View file

@ -1,36 +1,17 @@
import pypandoc import pypandoc
from bs4 import BeautifulSoup
import os import os
from md_pdf.consts import CSL_DIR from md_pdf.consts import CSL_DIR
from jinja2 import Template
import logging import logging
logger = logging.getLogger(__file__) logger = logging.getLogger(__file__)
def fix_references_title(content):
logger.debug("Adding Reference Title...")
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): def output_html(html, out_dir):
logger.info("Outputting HTML...") logger.info("Outputting HTML...")
with open(os.path.join(out_dir, 'output.html'), 'w') as f: with open(os.path.join(out_dir, 'output.html'), 'w') as f:
f.write(html) f.write(html)
def parse_template(html, context):
logger.debug("Rendering Template...")
template = Template(html)
return template.render(context)
def build_document(files_content, bibliography, context): def build_document(files_content, bibliography, context):
args = [ args = [
'-s', '-s',
@ -43,12 +24,10 @@ def build_document(files_content, bibliography, context):
] ]
filters.append('pandoc-citeproc') filters.append('pandoc-citeproc')
logger.info("Rendering Document...") logger.info("Rendering Document...")
html = fix_references_title(pypandoc.convert_text( return pypandoc.convert_text(
files_content, files_content,
'html', 'html',
format='md', format='md',
extra_args=args, extra_args=args,
filters=filters filters=filters
)) )
return parse_template(html, context)

49
md_pdf/build/template.py Normal file
View file

@ -0,0 +1,49 @@
from jinja2 import Template
from bs4 import BeautifulSoup
import os
import logging
logger = logging.getLogger(__file__)
def fix_references_title(content, config):
logger.debug("Adding Reference Title...")
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 add_base_tag(doc, config):
logger.debug("Adding Base Tag...")
soup = BeautifulSoup(doc, 'html.parser')
base_tag = soup.new_tag('base', href=os.path.abspath(config['output_dir']))
soup.head.insert(0, base_tag)
return soup.prettify()
def add_body_class(doc, config):
logger.debug("Adding Body Class...")
soup = BeautifulSoup(doc, 'html.parser')
soup.body['class'] = 'content'
return soup.prettify()
def render_template(html, config):
logger.debug("Rendering Template...")
template = Template(html)
return template.render(config)
def parse_template(doc, config):
parsed_doc = doc
for parser in [
fix_references_title,
add_base_tag,
add_body_class,
]:
parsed_doc = parser(parsed_doc, config)
return render_template(parsed_doc, config)

4
test-files/3-image.md Normal file
View file

@ -0,0 +1,4 @@
# Test Image
![title](./test-image.png)

BIN
test-files/test-image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 801 B