Skip to content

Commit cbd600a

Browse files
authored
Provides not-in-format filter (#153)
The code comments are in LDoc format.
1 parent c705b8f commit cbd600a

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

not-in-format/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Julien Dutant
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

not-in-format/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
DIFF ?= diff --strip-trailing-cr -u
2+
3+
.PHONY: test
4+
5+
test: test_html test_latex
6+
7+
test_html: sample.md expected.html not-in-format.lua
8+
@pandoc --lua-filter not-in-format.lua --to=html $< \
9+
| $(DIFF) expected.html -
10+
11+
test_latex: sample.md expected.tex not-in-format.lua
12+
@pandoc --lua-filter not-in-format.lua --to=latex $< \
13+
| $(DIFF) expected.tex -
14+
15+
expected.html: sample.md not-in-format.lua
16+
pandoc --lua-filter not-in-format.lua --output $@ $<
17+
18+
expected.tex: sample.md not-in-format.lua
19+
pandoc --lua-filter not-in-format.lua --output $@ $<

not-in-format/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: "Not-in-format - Keep document parts out of selected output
3+
formats"
4+
author: "Julien Dutant"
5+
---
6+
7+
Not-in-format
8+
=======
9+
10+
Keeps parts of a document out of selected output formats.
11+
12+
v1.0. Copyright: © 2021 Julien Dutant <[email protected]>
13+
License: MIT - see LICENSE file for details.
14+
15+
Usage
16+
-----
17+
18+
This Lua filter for Pandoc that keeps parts of a document out of selected outputs formats.
19+
20+
*Use case*. Sometimes a bit of markdown will convert well through Pandoc
21+
to some formats not others. Say, you have a table that is reasonably
22+
well converted to html and docx, but you want more control on the
23+
LaTeX output. This filter allows you to:
24+
25+
* Enter the desired LaTeX table output as a [fenced block with a raw
26+
attribute](https://pandoc.org/MANUAL.html#extension-raw_attribute).
27+
* Mark up the markdown table so that it is output only in formats other
28+
than LaTeX.
29+
30+
*Usage*. Mark up the part you want to keep out of some output format(s)
31+
as a native Div with the attributes `not-in-format` and the names of the
32+
format(s) in question:
33+
34+
```markdown
35+
::: {.not-in-format .latex .beamer}
36+
37+
---------------------------
38+
This table is not kept
39+
----------- ----------------
40+
in formats latex, beamer
41+
----------------------------
42+
43+
:::
44+
```
45+
46+
Use format names for [Pandoc's list of output formats](https://pandoc.org/MANUAL.html#option--to), which you can print out by running ```pandoc --list-output-formats```.
47+
48+
You would normally provide a raw equivalent for the part in question in
49+
the format(s) in question, using Pandoc's [fenced blocks with a raw
50+
attribute](https://pandoc.org/MANUAL.html#extension-raw_attribute):
51+
52+
```markdown
53+
::: {.not-in-format .latex .beamer}
54+
55+
---------------------------
56+
This table is not kept
57+
----------- ----------------
58+
in formats latex, beamer
59+
----------------------------
60+
61+
:::
62+
63+
~~~{=latex}
64+
65+
begin{tabular}{|c|c|}
66+
\toprule
67+
This table & is not kept \\ \addlinespace
68+
\midrule
69+
\endhead
70+
in formats & latex, beamer \\ \addlinespace
71+
\bottomrule
72+
\end{tabluar}
73+
74+
~~~
75+
```
76+
77+
A fenced block with a raw attribute can only have one attribute, so you
78+
need one for each format for which the markdown is left out. But Pandoc
79+
will output the raw block in equivalent formats: a `latex` block will
80+
be output in `latex` and `beamer`, a `html5` block in `html` and `epub3`
81+
and so on. See [Pandoc's manual](https://pandoc.org/MANUAL.html#extension-raw_attribute).
82+
83+
Installation
84+
------------
85+
86+
Copy `not-in-format.lua` in your document folder or in your pandoc data
87+
dir path.
88+
89+
Contributing
90+
------------
91+
92+
PRs welcome.

not-in-format/expected.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<p>The table below exists in two versions. The LaTeX version will be used in <code>latex</code> and <code>beamer</code> and the markdown version in all other output formats.</p>
2+
<div class="not-in-format latex beamer">
3+
<table style="width:40%;">
4+
<colgroup>
5+
<col style="width: 16%" />
6+
<col style="width: 23%" />
7+
</colgroup>
8+
<thead>
9+
<tr class="header">
10+
<th style="text-align: left;">This table</th>
11+
<th style="text-align: center;">is not kept</th>
12+
</tr>
13+
</thead>
14+
<tbody>
15+
<tr class="odd">
16+
<td style="text-align: left;">in formats</td>
17+
<td style="text-align: center;">latex, beamer</td>
18+
</tr>
19+
</tbody>
20+
</table>
21+
</div>

not-in-format/expected.tex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The table below exists in two versions. The LaTeX version will be used
2+
in \texttt{latex} and \texttt{beamer} and the markdown version in all
3+
other output formats.
4+
5+
begin{tabular}{|c|c|}
6+
\toprule
7+
This table & is used \\ \addlinespace
8+
\midrule
9+
\endhead
10+
in formats & latex, beamer \\ \addlinespace
11+
\bottomrule
12+
\end{tabluar}

not-in-format/not-in-format.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--- # not-in-format - keep document parts out of selected output formats.
2+
--
3+
-- This Lua filter for Pandoc that keeps parts of a document out of
4+
-- selected outputs formats.
5+
--
6+
-- @author Julien Dutant <[email protected]>, Albert Krewinkel
7+
-- @copyright 2021 Julien Dutant, Albert Krewinkel
8+
-- @license MIT - see LICENSE file for details.
9+
-- @release 1.0
10+
11+
--- Main Div filter.
12+
-- Removes Divs tagged with `not-in-format` if the
13+
-- output format matches one of the Div's attributes
14+
-- @param element a Div element
15+
-- @return an empty block if the Div classes include `not-in-format`
16+
-- and the target format
17+
function Div(element)
18+
19+
-- the find_if method returns the first value x in the list
20+
-- element.classes for which FORMAT:match(x) evaluates as true,
21+
-- and nil otherwise. Within the if clause that value evaluates
22+
-- as true and nil evaluates as false.
23+
24+
if element.classes:includes('not-in-format') and
25+
element.classes:find_if(function (x) return FORMAT:match(x) end) then
26+
return {}
27+
end
28+
29+
-- else the function returns `nil` and Pandoc leaves the element as is
30+
31+
end

not-in-format/sample.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
The table below exists in two versions. The LaTeX version will be used in `latex` and `beamer` and the markdown version in all other output formats.
2+
3+
::: {.not-in-format .latex .beamer}
4+
5+
---------------------------
6+
This table is not kept
7+
----------- ----------------
8+
in formats latex, beamer
9+
----------------------------
10+
11+
:::
12+
13+
~~~{=latex}
14+
15+
begin{tabular}{|c|c|}
16+
\toprule
17+
This table & is used \\ \addlinespace
18+
\midrule
19+
\endhead
20+
in formats & latex, beamer \\ \addlinespace
21+
\bottomrule
22+
\end{tabluar}
23+
24+
~~~

0 commit comments

Comments
 (0)