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):
data = read_files(os.path.abspath(config.input))
doc = build_document(data,
os.path.abspath(config.bibliography) if 'bibliography' in config else None
)
doc = build_document(data, getattr(config, 'bibliography', None))
if 'html' in config.output_formats:
output_html(doc, os.path.abspath(config.output_dir))
if 'pdf' in config.output_formats:

View file

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

View file

@ -1,4 +1,5 @@
from md_pdf.exceptions import ConfigValidationException
from md_pdf.consts import CSL_DIR
import os
@ -29,9 +30,15 @@ def test_output(config):
def validate_bibliography(config):
if 'bibliography' not in config:
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):
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):

View file

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