-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from moggers87/24-external
External command filter
- Loading branch information
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
exhibition.filters.external module | ||
================================== | ||
|
||
.. automodule:: exhibition.filters.external | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ Submodules | |
|
||
.. toctree:: | ||
|
||
exhibition.filters.external | ||
exhibition.filters.jinja2 | ||
|
||
Module contents | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
## | ||
# | ||
# Copyright (C) 2018 Matt Molyneaux | ||
# | ||
# This file is part of Exhibition. | ||
# | ||
# Exhibition is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Exhibition is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Exhibition. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
## | ||
|
||
""" | ||
External command filter | ||
Use an external command to process a file, like so: | ||
.. code-block:: yaml | ||
filter: exhibition.filters.external | ||
external_cmd: sed 's/this/that/g' {INPUT} > {OUTPUT} | ||
""" | ||
|
||
from tempfile import TemporaryDirectory | ||
import pathlib | ||
import subprocess | ||
|
||
DEFAULT_GLOB = "*.*" | ||
|
||
INPUT_NAME = "input" | ||
OUTPUT_NAME = "output" | ||
|
||
INPUT_KEY = "INPUT" | ||
OUTPUT_KEY = "OUTPUT" | ||
|
||
|
||
def content_filter(node, content): | ||
""" | ||
This is the actual content filter called by :class:`exhibition.main.Node` | ||
on appropiate nodes. | ||
:param node: | ||
The node being rendered | ||
:param content: | ||
The content of the node, stripped of any YAML frontmatter | ||
""" | ||
tmp_dir = TemporaryDirectory() | ||
input_file = pathlib.Path(tmp_dir.name, INPUT_NAME) | ||
output_file = pathlib.Path(tmp_dir.name, OUTPUT_NAME) | ||
|
||
cmd = node.meta["external_cmd"] | ||
cmd = cmd.format(**{ | ||
INPUT_KEY: input_file, | ||
OUTPUT_KEY: output_file, | ||
}) | ||
|
||
with input_file.open("w") as f: | ||
f.write(content) | ||
|
||
subprocess.run(cmd, shell=True) | ||
|
||
with output_file.open("r") as f: | ||
output = f.read() | ||
|
||
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters