Specify output formats

This commit is contained in:
Jake Howard 2017-03-29 19:16:12 +01:00
parent 5ab9211958
commit 1a60c4c1bc
5 changed files with 35 additions and 10 deletions

View file

@ -6,9 +6,11 @@ from md_pdf.build.pdf import export_pdf
import os import os
def build(args, config): def build(config):
data = read_files(os.path.join(WORKING_DIR, '*.md')) data = read_files(os.path.join(WORKING_DIR, '*.md'))
doc = build_document(data, os.path.join(WORKING_DIR, 'bib.yaml')) doc = build_document(data, os.path.join(WORKING_DIR, 'bib.yaml'))
if 'html' in config.output_formats:
output_html(doc, os.path.join(WORKING_DIR, 'out')) output_html(doc, os.path.join(WORKING_DIR, 'out'))
if 'pdf' in config.output_formats:
render_cover() render_cover()
export_pdf(doc, os.path.join(WORKING_DIR, 'out')) export_pdf(doc, os.path.join(WORKING_DIR, 'out'))

View file

@ -1,6 +1,6 @@
import logging import logging
from md_pdf.args import parse_args from md_pdf.args import parse_args
from md_pdf.exceptions import PrematureExit from md_pdf.exceptions import PrematureExit, BaseException
from md_pdf.build import build from md_pdf.build import build
from md_pdf.config.read import load_config from md_pdf.config.read import load_config
from md_pdf.config.validate import validate_config from md_pdf.config.validate import validate_config
@ -15,10 +15,10 @@ def cli():
try: try:
config = load_config() config = load_config()
validate_config(config) validate_config(config)
build(args, config) build(config)
except PrematureExit: except PrematureExit:
return 0 return 0
except Exception as e: except BaseException as e:
logging.error(str(e)) logging.error(str(e))
return 1 return 1
return 0 return 0

View file

@ -1,8 +1,10 @@
from md_pdf.exceptions import ConfigValidationException from md_pdf.exceptions import ConfigValidationException
import os
REQUIRED_KEYS = [ REQUIRED_KEYS = [
'output', 'output_dir',
'output_formats',
'input' 'input'
] ]
@ -13,7 +15,19 @@ def check_required_keys(config):
raise ConfigValidationException("Missing required keys: {}".format(", ".join(missing_keys))) raise ConfigValidationException("Missing required keys: {}".format(", ".join(missing_keys)))
def test_output(config):
abs_output_dir = os.path.abspath(config.output_dir)
if not os.path.isdir(abs_output_dir):
raise ConfigValidationException("Can't find output directory '{}'".format(abs_output_dir))
if not config.output_formats:
raise ConfigValidationException("No output formats specified")
invalid_formats = [key for key in config.output_formats if key not in ['html', 'pdf']]
if invalid_formats:
raise ConfigValidationException("Invalid output formats provided: '{}'".format(", ".join(invalid_formats)))
def validate_config(config): def validate_config(config):
check_required_keys(config) check_required_keys(config)
test_output(config)

View file

@ -1,6 +1,10 @@
class PrematureExit(Exception): class BaseException(Exception):
pass pass
class ConfigValidationException(Exception): class PrematureExit(BaseException):
pass
class ConfigValidationException(BaseException):
pass pass

View file

@ -0,0 +1,5 @@
input: "*.md"
output_formats:
- html
- pdf
output_dir: out/