Skip to content

Commit

Permalink
feat(pandoc_imagine): change gnuplot class logic
Browse files Browse the repository at this point in the history
no more stdout is used, but directly specify output file
patching the original code block

also add two additional envs (useful only for debugging/testing)
 * `MCB_PASS_OVERRIDE` - to permit output minted code block
   even if the document output format is different from "tex"
 * `TEX_PASS_OVERRIDE` - to permit imagine "latex" format
   for image even if the document output format is
    different from "latex"
  • Loading branch information
andros21 committed Aug 25, 2023
1 parent 28e18bf commit 145fb2b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
3 changes: 2 additions & 1 deletion _readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
```

## A pandoc filter to process codeblocks

<a href="https://github.com/andros21/imagine/actions/workflows/ci.yml">
<img src="https://img.shields.io/github/actions/workflow/status/andros21/imagine/ci.yml?branch=master&label=ci&logo=github" alt="ci">
</a>
Expand Down Expand Up @@ -59,7 +60,6 @@ codeblock (syntax highlight on!)
#### [Gnuplot](http://www.gnuplot.info)

```{.gnuplot im_fmt="png" im_out="img"}
set terminal png
set dummy u,v
set key bmargin center horizontal Right noreverse enhanced autotitles nobox
set parametric
Expand Down Expand Up @@ -123,6 +123,7 @@ xmas-fifth-day:
turtle-doves: two
@endyaml
```

#### [Matplotlib](https://matplotlib.org/)

```{.matplotlib im_fmt="png" im_out="img"}
Expand Down
30 changes: 19 additions & 11 deletions pandoc_imagine.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,17 @@ def url(self):
# 'pf.Image' is an Inline element. Callers usually wrap it in a 'pf.Figure'
# When {im_fmt} is 'tex' and pandoc fmt is 'latex'
# return a pf.RawInline('latex') instead, wrapped in 'pf.Figure' too
if self.im_fmt == "tex" and self.fmt == "latex":
# TEX_PASS_OVERRIDE=1 to permit output image as \input latex
# useful when debugging and essential in tests
if self.im_fmt == "tex" and (
self.fmt == "latex" or os.getenv("TEX_PASS_OVERRIDE", False)
):
return pf.RawInline(f"\input{{{self.outfile[:-4]}}}", format="tex")
if self.im_fmt == "tex" and self.fmt != "latex":
sys.stderr.write(
"Could not render tex image when output format is not latex\n"
)
sys.exit(1)
else:
return pf.Image(url=self.outfile)

Expand Down Expand Up @@ -321,7 +330,9 @@ def result(self):
rv.append(pf.Para(pf.Str(msg)))

elif output_elm == "mcb":
if self.fmt == "latex":
# MCB_PASS_OVERRIDE=1 to force creation of minted block
# useful when debugging and essential in tests
if self.fmt == "latex" or os.getenv("MCB_PASS_OVERRIDE", False):
rv.append(self.minted_codeblock())
else:
rv.append(
Expand Down Expand Up @@ -425,27 +436,24 @@ class GnuPlot(Handler):
sudo apt-get install gnuplot
http://www.gnuplot.info
notes:
- graphic data is printed to stdout
- exception, imagine adds the following lines to the top of the script
- Imagine adds the following lines to the top of the script
set terminal {im_fmt}
set output {outfile}
only when {im_fmt} == 'tex'
when `self.im_fmt == 'tex'` terminal is omitted
"""

cmdmap = {"gnuplot": "gnuplot"}

def image(self):
"gnuplot {im_opt} <fname>.gnuplot > <fname>.{im_fmt}"
"gnuplot {im_opt} <fname>.gnuplot"
if "img" in self.im_out:
# stdout captures the graphic image
args = self.im_opt + [self.inpfile]
code = self.code
if self.im_fmt == "tex":
code = f"set output '{self.outfile}'\n{self.code}"
code = f"set output '{self.outfile}'\n{self.code}"
if self.im_fmt != "tex":
code = f"set terminal {self.im_fmt}\n{code}"
self.write("w", code, self.inpfile)
if self.cmd(self.im_prg, *args):
if self.stdout:
self.write("wb", self.stdout, self.outfile)
return self.result()
else:
return self.result()
Expand Down

0 comments on commit 145fb2b

Please sign in to comment.