From 9ea3e8e87acbd3f8b31f126db315f6213fc5904a Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 10 May 2017 17:22:55 +0100 Subject: [PATCH 1/5] Add image support --- md_pdf/build/__init__.py | 6 ++++-- md_pdf/build/pandoc.py | 25 ++----------------------- md_pdf/build/template.py | 38 ++++++++++++++++++++++++++++++++++++++ test-files/test-image.png | Bin 0 -> 1132 bytes test-files/test.md | 3 +++ 5 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 md_pdf/build/template.py create mode 100644 test-files/test-image.png diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index ab4ffd6..3a34605 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -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.css import render_css from md_pdf.build.pdf import export_pdf +from md_pdf.build.template import parse_template import os import logging @@ -13,9 +14,10 @@ def build(config): logger.debug("Starting Build...") data = read_files(os.path.abspath(config['input'])) doc = build_document(data, config.get('bibliography'), config.get('context')) + parsed_template = parse_template(doc, config) 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']: render_cover(config) render_css() - export_pdf(doc, config) + export_pdf(parsed_template, config) diff --git a/md_pdf/build/pandoc.py b/md_pdf/build/pandoc.py index 984a631..7bd86a2 100644 --- a/md_pdf/build/pandoc.py +++ b/md_pdf/build/pandoc.py @@ -1,36 +1,17 @@ import pypandoc -from bs4 import BeautifulSoup import os from md_pdf.consts import CSL_DIR -from jinja2 import Template import logging 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): logger.info("Outputting HTML...") with open(os.path.join(out_dir, 'output.html'), 'w') as f: 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): args = [ '-s', @@ -43,12 +24,10 @@ def build_document(files_content, bibliography, context): ] filters.append('pandoc-citeproc') logger.info("Rendering Document...") - html = fix_references_title(pypandoc.convert_text( + return pypandoc.convert_text( files_content, 'html', format='md', extra_args=args, filters=filters - )) - - return parse_template(html, context) + ) diff --git a/md_pdf/build/template.py b/md_pdf/build/template.py new file mode 100644 index 0000000..c50d7b9 --- /dev/null +++ b/md_pdf/build/template.py @@ -0,0 +1,38 @@ +from jinja2 import Template +from bs4 import BeautifulSoup +import os +import logging + +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 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 render_template(html, config): + logger.debug("Rendering Template...") + template = Template(html) + return template.render(config) + + +def parse_template(doc, config): + doc = fix_references_title(doc) + doc = add_base_tag(doc, config) + return render_template(doc, config) + + diff --git a/test-files/test-image.png b/test-files/test-image.png new file mode 100644 index 0000000000000000000000000000000000000000..a91d8ec029825c5572cce5661d040339fbc32c93 GIT binary patch literal 1132 zcmeAS@N?(olHy`uVBq!ia0vp^n?RU@4M;xgnwkNm6iQqpN`eD|To^KQ6Vp?5^AZh= z^a_f~^itBw^@>ge<;Zx+r7Zq&Y z{yr~eqUrCP({kslV&_=}pO_xmsbmVV=8$eXAE|K7G1G`>Nja|GZE5-!J3%q&p#9;fde` zM>ffnQG)z1IId9t^Ovo@nCJJ8Sv4o6C%yZ6`shWAA4Wf)eUtBPySHx1?OU6T=bha> zq2l@DO6Tg0UYF*^Y}21s{&m?sk82mN&KB>U@;c_OthSYw=k{%DYq|3NwcfbA#qiYH z)06T}UVAqq-o`}pdc|kgk}X#k6=xP-;^n)e6_;i&^!~R;*s9o*!TW!mzPdLsGs|z+ ztx0{FKP>HJe~aaK)J=1Z-ZlBZ>#XAEp;K0EeUiS@w!(Uu0^W#z63MXh&NgkJSFUgR zx1Qau=0yR|7k;0L=34VdpO+s0zh}91+oN}fitA(c#`K@n-90()@vO%eBjvk}F{fY7 z{Xg^3zAfqpcdxqh^|ekq|F`3Z8`byU4Y Date: Wed, 10 May 2017 17:41:49 +0100 Subject: [PATCH 2/5] Better syntax for parsers --- md_pdf/build/template.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/md_pdf/build/template.py b/md_pdf/build/template.py index c50d7b9..835162d 100644 --- a/md_pdf/build/template.py +++ b/md_pdf/build/template.py @@ -5,7 +5,8 @@ import logging logger = logging.getLogger(__file__) -def fix_references_title(content): + +def fix_references_title(content, config): logger.debug("Adding Reference Title...") soup = BeautifulSoup(content, 'html.parser') reference_element = soup.find('div', class_='references') @@ -31,8 +32,13 @@ def render_template(html, config): def parse_template(doc, config): - doc = fix_references_title(doc) - doc = add_base_tag(doc, config) - return render_template(doc, config) + parsed_doc = doc + for parser in [ + fix_references_title, + add_base_tag, + render_template + ]: + parsed_doc = parser(parsed_doc, config) + return parsed_doc From d620bc52b0f4538dd263ceb296153277c6ee57d4 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 10 May 2017 18:14:30 +0100 Subject: [PATCH 3/5] Style images --- md_pdf/assets/static/style.scss | 17 ++++++++++++++++- md_pdf/build/template.py | 11 +++++++++-- test-files/test-image.png | Bin 1132 -> 801 bytes 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/md_pdf/assets/static/style.scss b/md_pdf/assets/static/style.scss index d158fa2..419f740 100644 --- a/md_pdf/assets/static/style.scss +++ b/md_pdf/assets/static/style.scss @@ -16,7 +16,22 @@ body.cover { } } -body, html { + +$image_spacing: 30px; + +body.content { line-height: 1.5; font-size: 12px; + + img { + width: 100%; + margin-top: $image_spacing + } + + p.caption { + margin: 0 5px $image_spacing; + padding: 0; + font-style: italic; + } + } diff --git a/md_pdf/build/template.py b/md_pdf/build/template.py index 835162d..7533106 100644 --- a/md_pdf/build/template.py +++ b/md_pdf/build/template.py @@ -25,6 +25,13 @@ def add_base_tag(doc, config): 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) @@ -36,9 +43,9 @@ def parse_template(doc, config): for parser in [ fix_references_title, add_base_tag, - render_template + add_body_class, ]: parsed_doc = parser(parsed_doc, config) - return parsed_doc + return render_template(parsed_doc, config) diff --git a/test-files/test-image.png b/test-files/test-image.png index a91d8ec029825c5572cce5661d040339fbc32c93..05e2bc8b733aa72867675a384c2fbc842a789ae8 100644 GIT binary patch literal 801 zcmeAS@N?(olHy`uVBq!ia0vp^8-TcpgAGVNV|Drgq}Y|gW!U_%O?XxI14-? ziy0WWg+Z8+Vb&Z8pdfpRr>`sfLsl*UORFUNU#}S$nAUl^IEGZ*dVAeAJ2+6{z{lsy zJcD0$2V`oOPA=QDAfP}%Muw#{oIU(6L)Yqf2OZUOyEHlcN()z)eBkwVeY0T2jia}^ zx}0aL^?r}DZ}2);w4(KX{#jN3b7%k0xs#F_Ic;K4w$`Fb)0Z9VXEz&uDNvkrdxzY` znf_Lfo;(V;v#-?RqtmI59pZv-mjp{GAAfW%;nvm=rZPiDa;RCzro)s&twu4uE<83mfBD=Cr<7-9 zem_}d-Tg02{pZ=Nt)EVOHhS5!rz7I_<+&BAsVQ%D{=U2s+xu@{)vbp9IXh;S39fBD zG3)o_a}p=2AN_oF_euPTt&K-3g5o``9)G=Y(88)z@nz2%jievP{;n{&EEhL@y-fe~ z>3V8*jn{8=GM`h^u{tZ0S+D!WStYGb{)b+BwSQGz z{x_?F?bv(^lwkY6HY@e9>n%?E@a=n^e3=<+XlVHHyz;A5({E<{xxW|htK21$KVSFn z*RN6MD|*U%m+XzTZ+m=t@y&>n_RFM$#3q(TdETqqU?lZ>mz%QEqsx!K&d5Cf_v5bj zKZ|O#r){^@mzK2*c=ttbR-VuCouAXoYs~F4&K&hg&O2^@{P~~SJz3}9of0lwV47{h4D2`t?sAF9@+sDGlQqApUXO@geCwu(nGcY literal 1132 zcmeAS@N?(olHy`uVBq!ia0vp^n?RU@4M;xgnwkNm6iQqpN`eD|To^KQ6Vp?5^AZh= z^a_f~^itBw^@>ge<;Zx+r7Zq&Y z{yr~eqUrCP({kslV&_=}pO_xmsbmVV=8$eXAE|K7G1G`>Nja|GZE5-!J3%q&p#9;fde` zM>ffnQG)z1IId9t^Ovo@nCJJ8Sv4o6C%yZ6`shWAA4Wf)eUtBPySHx1?OU6T=bha> zq2l@DO6Tg0UYF*^Y}21s{&m?sk82mN&KB>U@;c_OthSYw=k{%DYq|3NwcfbA#qiYH z)06T}UVAqq-o`}pdc|kgk}X#k6=xP-;^n)e6_;i&^!~R;*s9o*!TW!mzPdLsGs|z+ ztx0{FKP>HJe~aaK)J=1Z-ZlBZ>#XAEp;K0EeUiS@w!(Uu0^W#z63MXh&NgkJSFUgR zx1Qau=0yR|7k;0L=34VdpO+s0zh}91+oN}fitA(c#`K@n-90()@vO%eBjvk}F{fY7 z{Xg^3zAfqpcdxqh^|ekq|F`3Z8`byU4Y Date: Wed, 10 May 2017 18:42:36 +0100 Subject: [PATCH 4/5] Style image more --- md_pdf/assets/static/style.scss | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/md_pdf/assets/static/style.scss b/md_pdf/assets/static/style.scss index 419f740..92b0380 100644 --- a/md_pdf/assets/static/style.scss +++ b/md_pdf/assets/static/style.scss @@ -1,3 +1,6 @@ +$image_spacing: 30px; + + body.cover { margin: 0 auto; text-align: center; @@ -17,8 +20,6 @@ body.cover { } -$image_spacing: 30px; - body.content { line-height: 1.5; font-size: 12px; @@ -29,7 +30,7 @@ body.content { } p.caption { - margin: 0 5px $image_spacing; + margin: 1px 5px $image_spacing; padding: 0; font-style: italic; } From cf36f85708d5e34b83a01e739922b2e40a73515f Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 10 May 2017 19:02:19 +0100 Subject: [PATCH 5/5] Fix linter --- md_pdf/assets/static/style.scss | 6 +++--- md_pdf/build/template.py | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/md_pdf/assets/static/style.scss b/md_pdf/assets/static/style.scss index 92b0380..2295a1b 100644 --- a/md_pdf/assets/static/style.scss +++ b/md_pdf/assets/static/style.scss @@ -1,4 +1,4 @@ -$image_spacing: 30px; +$image-spacing: 30px; body.cover { @@ -25,12 +25,12 @@ body.content { font-size: 12px; img { + margin-top: $image-spacing; width: 100%; - margin-top: $image_spacing } p.caption { - margin: 1px 5px $image_spacing; + margin: 1px 5px $image-spacing; padding: 0; font-style: italic; } diff --git a/md_pdf/build/template.py b/md_pdf/build/template.py index 7533106..b74a1d3 100644 --- a/md_pdf/build/template.py +++ b/md_pdf/build/template.py @@ -47,5 +47,3 @@ def parse_template(doc, config): ]: parsed_doc = parser(parsed_doc, config) return render_template(parsed_doc, config) - -