diff options
| author | 2021-04-22 20:22:47 -0400 | |
|---|---|---|
| committer | 2021-04-22 20:22:47 -0400 | |
| commit | 8eeed31eb2f86ac982fa4b26f93b15828289c56d (patch) | |
| tree | 2d95f6a10afaeb9f5e25dabde5fe38de3f8458d1 /nikola/plugins/compile/pandoc.py | |
| parent | 3a0d66f07b112b6d2bdc2b57bbf717a89a351ce6 (diff) | |
New upstream version 8.1.3.upstream/8.1.3
Diffstat (limited to 'nikola/plugins/compile/pandoc.py')
| -rw-r--r-- | nikola/plugins/compile/pandoc.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/nikola/plugins/compile/pandoc.py b/nikola/plugins/compile/pandoc.py index af14344..62d3e88 100644 --- a/nikola/plugins/compile/pandoc.py +++ b/nikola/plugins/compile/pandoc.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright © 2012-2020 Roberto Alsina and others. +# Copyright © 2012-2021 Roberto Alsina and others. # Permission is hereby granted, free of charge, to any # person obtaining a copy of this software and associated @@ -33,6 +33,8 @@ You will need, of course, to install pandoc import io import os import subprocess +from typing import List +from pathlib import Path from nikola.plugin_categories import PageCompiler from nikola.utils import req_missing, makedirs, write_metadata @@ -49,11 +51,29 @@ class CompilePandoc(PageCompiler): self.config_dependencies = [str(site.config['PANDOC_OPTIONS'])] super().set_site(site) + def _get_pandoc_options(self, source: str) -> List[str]: + """Obtain pandoc args from config depending on type and file extensions.""" + # Union[List[str], Dict[str, List[str]]] + config_options = self.site.config['PANDOC_OPTIONS'] + if isinstance(config_options, (list, tuple)): + pandoc_options = list(config_options) + elif isinstance(config_options, dict): + ext = Path(source).suffix + try: + pandoc_options = list(config_options[ext]) + except KeyError: + self.logger.warn('Setting PANDOC_OPTIONS to [], because extension {} is not defined in PANDOC_OPTIONS: {}.'.format(ext, config_options)) + pandoc_options = [] + else: + self.logger.warn('Setting PANDOC_OPTIONS to [], because PANDOC_OPTIONS is expected to be of type Union[List[str], Dict[str, List[str]]] but this is not: {}'.format(config_options)) + pandoc_options = [] + return pandoc_options + def compile(self, source, dest, is_two_file=True, post=None, lang=None): """Compile the source file into HTML and save as dest.""" makedirs(os.path.dirname(dest)) try: - subprocess.check_call(['pandoc', '-o', dest, source] + self.site.config['PANDOC_OPTIONS']) + subprocess.check_call(['pandoc', '-o', dest, source] + self._get_pandoc_options(source)) with open(dest, 'r', encoding='utf-8-sig') as inf: output, shortcode_deps = self.site.apply_shortcodes(inf.read()) with open(dest, 'w', encoding='utf-8') as outf: |
