Skip to content

Commit

Permalink
Merge pull request #3501 from ickc/issue-3492
Browse files Browse the repository at this point in the history
dispatch PANDOC_OPTIONS base on input extensions
  • Loading branch information
Kwpolska committed Jan 15, 2021
2 parents 4e41f17 + c31707c commit 25d0344
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* `James C. McPherson <https://github.com/jmcp>`_
* `Kade For <https://github.com/kadefor>`_
* `Kay Hayen <https://github.com/kayhayen>`_
* `Kolen Cheung <https://github.com/ickc>`_
* `lbiaggi <https://github.com/lbiaggi>`_
* `Leandro Poblet <https://github.com/DoctorMalboro>`_
* `Luis Miguel Morillas <https://github.com/lmorillas>`_
Expand Down
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Features
* Provide the full ``GLOBAL_CONTEXT`` to the post list shortcode plugin
(Issue #3481)
* Add ``BasePlugin.register_auto_watched_folder()``
* dispatch PANDOC_OPTIONS base on input extensions (Issue #3492)

New in v8.1.2
=============
Expand Down
10 changes: 9 additions & 1 deletion nikola/conf.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,16 @@ MARKDOWN_EXTENSIONS = ['markdown.extensions.fenced_code', 'markdown.extensions.c


# Extra options to pass to the pandoc command.
# by default, it's empty, is a list of strings, for example
# by default, it's an empty list
# it can be is a list of strings, for example
# ['-F', 'pandoc-citeproc', '--bibliography=/Users/foo/references.bib']
# it can be a dict, where the keys are the extensions in COMPILERS['pandoc'], for example
# COMPILERS['pandoc'] = ['.rst', '.md', '.txt']
# PANDOC_OPTIONS = {
# '.rst': ['-t', 'rst'],
# '.md': ['-t', 'markdown'],
# '.txt': ['-t', 'markdown-raw_html'],
# }
# Pandoc does not demote headers by default. To enable this, you can use, for example
# ['--base-header-level=2']
# PANDOC_OPTIONS = []
Expand Down
23 changes: 22 additions & 1 deletion nikola/plugins/compile/pandoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
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
Expand All @@ -49,11 +51,30 @@ def set_site(self, site):
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']
type_ = type(config_options)
if type_ is list:
pandoc_options = config_options
elif type_ is dict:
ext = Path(source).suffix
try:
pandoc_options = 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:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ def test_command_version():

def test_importing_plugin_task_galleries():
import nikola.plugins.task.galleries # NOQA


def test_importing_plugin_compile_pandoc():
import nikola.plugins.compile.pandoc # NOQA

0 comments on commit 25d0344

Please sign in to comment.