Allow custom CSL

This commit is contained in:
Jake Howard 2017-03-30 08:44:50 +01:00
parent 2ea30932ea
commit e4692dbfc0
4 changed files with 17 additions and 10 deletions

View file

@ -8,9 +8,7 @@ import os
def build(config): def build(config):
data = read_files(os.path.abspath(config.input)) data = read_files(os.path.abspath(config.input))
doc = build_document(data, doc = build_document(data, getattr(config, 'bibliography', None))
os.path.abspath(config.bibliography) if 'bibliography' in config else None
)
if 'html' in config.output_formats: if 'html' in config.output_formats:
output_html(doc, os.path.abspath(config.output_dir)) output_html(doc, os.path.abspath(config.output_dir))
if 'pdf' in config.output_formats: if 'pdf' in config.output_formats:

View file

@ -1,7 +1,7 @@
import pypandoc import pypandoc
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import os.path import os
from md_pdf.consts import PROJECT_DIR from md_pdf.consts import PROJECT_DIR, CSL_DIR
CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl') CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl')
@ -21,15 +21,15 @@ def output_html(html, out_dir):
f.write(html) f.write(html)
def build_document(files_content, bibliography=None): def build_document(files_content, bibliography):
args = [ args = [
'-s', '-s',
] ]
filters = [] filters = []
if bibliography is not None: if bibliography is not None:
args += [ args += [
'--bibliography={}'.format(bibliography), '--bibliography={}'.format(os.path.abspath(bibliography.references)),
'--csl={}'.format(CSL_FILE) '--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography.csl)))
] ]
filters.append('pandoc-citeproc') filters.append('pandoc-citeproc')

View file

@ -1,4 +1,5 @@
from md_pdf.exceptions import ConfigValidationException from md_pdf.exceptions import ConfigValidationException
from md_pdf.consts import CSL_DIR
import os import os
@ -29,9 +30,15 @@ def test_output(config):
def validate_bibliography(config): def validate_bibliography(config):
if 'bibliography' not in config: if 'bibliography' not in config:
return return
abs_bibliography = os.path.abspath(config.bibliography) if 'references' not in config.bibliography:
raise ConfigValidationException("Missing References Path")
abs_bibliography = os.path.abspath(config.bibliography.references)
if not os.path.isfile(abs_bibliography): if not os.path.isfile(abs_bibliography):
raise ConfigValidationException("Invalid bibliography path: '{}'".format(abs_bibliography)) raise ConfigValidationException("Invalid bibliography path: '{}'".format(abs_bibliography))
if 'csl' in config.bibliography:
if not os.path.isfile(os.path.join(CSL_DIR, "{}.csl".format(config.bibliography.csl))):
raise ConfigValidationException("Could not find CSL '{}'".format(config.bibliography.csl))
def validate_config(config): def validate_config(config):

View file

@ -3,4 +3,6 @@ output_formats:
- html - html
- pdf - pdf
output_dir: out/ output_dir: out/
bibliography: bib.yaml bibliography:
references: bib.yaml
csl: apa