Skip to content

Commit 2cc9018

Browse files
committed
Allow 'signifiant' class names to be in random position in the list
as suggested by Benct Philip Jonsson Plus other minor refactoring and some details in README.md Some enhencements in the sample file.
1 parent 9d650d4 commit 2cc9018

File tree

5 files changed

+73
-32
lines changed

5 files changed

+73
-32
lines changed

column-div/README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Column Div - leverage Pandoc native divs to make columns
3-
an other things"
3+
and other things"
44
author: "Christophe Agathon"
55
---
66

@@ -32,6 +32,14 @@ PDF document from markdown sources.
3232
The main purpose of this filter is to make it possible and give
3333
similar formating features for both Latex/PDF and HTML outputs.
3434

35+
My guidelines are :
36+
37+
1) Use Pandoc divs like many already have proposed for uneven and even columns
38+
2) Same functionalities and rendering in HTML and Latex+PDF
39+
3) Mess the least possible with plain Pandoc processing which is quite OK already for HTML (miss only column-count for even columning).
40+
4) Allow users to use unknown Latex environments from exotic packages if they wish, provided they include them in the preamble.
41+
42+
3543
Usage
3644
-----
3745

@@ -56,10 +64,10 @@ Pandoc markdown files.
5664
### Formating the document
5765

5866
Everything is done with Pandoc's fenced divs with class names and
59-
attributes. The attributes are similar to those from Latex and/or
60-
HTML styling.
67+
attributes. The attributes are similar to those from HTML styling and/or
68+
Latex.
6169

62-
#### Multiple balanced columns
70+
#### Multiple even columns
6371
For Latex and PDF output, you will need to call the multicol
6472
package. This can be done un the YAML header.
6573

@@ -85,7 +93,7 @@ Some text formatted on 2 columns
8593
* Latex output is done with `multicols` environment.
8694
* HTML output uses `style="column-count: 2"` on a div block.
8795

88-
#### Unbalanced columns
96+
#### Uneven columns
8997

9098
No specific Latex package are needed. We use Nested Pandoc divs in
9199
the same way that columns and column environments are used in
@@ -112,14 +120,17 @@ well already (based on divs with `width` attributes).
112120

113121
#### Other usages
114122

115-
HTML : you can already create divs with whatever class names youl
123+
For HTML outputs, you already can create divs with whatever class names you
116124
like and style them with `style=" … "` attributes. This is
117-
proccessed by Pandoc and as nothing to do with this filter.
125+
processed by Pandoc and has nothing to do with this filter.
118126

119127
This filter allows to do the same in Latex (and PDF).
120-
The class name is used as the environment name and a
121-
`data-latex=" … "` attribute allows you to pass options and
122-
parameters to the `\begin` instruction.
128+
You can create whatever environment you need. The environment name is the
129+
class name given to the fenced div. In case of multiple class names, the
130+
first one is used. Other are ignored but allowed to help you to maintain
131+
a single markdown source for PDF and HTML outputs.
132+
The `data-latex=" … "` attribute allows you to pass options and
133+
parameters to the `\begin` environment instruction.
123134

124135
To Do
125136
-----
@@ -129,7 +140,7 @@ spacing, rules, etc.
129140

130141
Since Pandoc does a very good job with the `width` styling
131142
attribute to implement variable column width, it could easily
132-
support HTML balanced column via the `column-count` attribute.
143+
support HTML even column via the `column-count` attribute.
133144

134145
Contributing
135146
------------

column-div/column-div.lua

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,28 @@ Note: You need to include multicol latex package to get balanced columns
2626
local List = require 'pandoc.List'
2727

2828
function Div(div)
29-
options = ''
30-
local env = div.classes[1]
29+
local options = ''
30+
local env = ''
3131
local returned_list
3232
local begin_env
3333
local end_env
3434
local opt
3535

3636
-- if the div has no class, the object is left unchanged
37-
if not env then return nil end
37+
-- if the div has no class but an id, div.classes ~= nil
38+
-- TODO: use a div with no class to build a 'scope' in Latex
39+
-- usefull for user who would throw inline Latex code and limit it's
40+
-- effect.
41+
if not div.classes or #div.classes == 0 then return nil end
3842

3943
-- if the output is beamer do columns
4044
if FORMAT:match 'beamer' then
45+
-- only arbitrary environment support in beamer for now. Environment has to
46+
-- be the firs class name.
47+
env = div.classes[1]
48+
if options == '' and div.attributes['data-latex'] then
49+
options = div.attributes['data-latex']
50+
end
4151
-- build the returned list of blocks
4252
begin_env = List:new{pandoc.RawBlock('tex',
4353
'\\begin' .. '{' .. env .. '}' .. options)}
@@ -47,8 +57,8 @@ function Div(div)
4757
-- if the format is latex then do minipage and others (like multicol)
4858
elseif FORMAT:match 'latex' then
4959
-- build the returned list of blocks
50-
if env == 'column' then
51-
--opt = div.attributes['width']
60+
if div.classes:includes('column') then
61+
env = 'column'
5262
opt = div.attributes.width
5363
if opt then
5464
local width=tonumber(string.match(opt,'(%f[%d]%d[,.%d]*%f[%D])%%'))/100
@@ -63,7 +73,8 @@ function Div(div)
6373
end_env = List:new{pandoc.RawBlock('tex', '\\end{' .. 'minipage' .. '}')}
6474
returned_list = begin_env .. div.content .. end_env
6575

66-
elseif env == 'columns' then
76+
elseif div.classes:includes('columns') then
77+
env = 'columns'
6778
-- merge two consecutives RawBlocks (\end... and \begin...)
6879
-- to get rid of the unwanted blank line
6980
local blocks = div.content
@@ -82,13 +93,20 @@ function Div(div)
8293

8394
else
8495
-- other environments ex: multicols
85-
86-
-- process supported options
87-
opt = div.attributes['column-count'] -- this synthax needed due to '_'
88-
if opt then options = '{' .. opt .. '}' end
89-
90-
-- default if no known options
91-
if options == '' then options = div.attributes.data-latex end
96+
if div.classes:includes('multicols') then
97+
env = 'multicols'
98+
-- process supported options
99+
opt = div.attributes['column-count']
100+
if opt then options = '{' .. opt .. '}' end
101+
else
102+
-- Latex skilled users can use arbitrary environments passed as
103+
-- the first (and only signifiant) class name.
104+
env = div.classes[1]
105+
-- default if no known options
106+
if options == '' and div.attributes['data-latex'] then
107+
options = div.attributes['data-latex']
108+
end
109+
end
92110

93111
begin_env = List:new{pandoc.RawBlock('tex',
94112
'\\begin' .. '{' .. env .. '}' .. options)}
@@ -105,9 +123,11 @@ function Div(div)
105123
div.attributes.style = div.attributes.style ..
106124
'; column-count: ' .. opt
107125
else
108-
div.attributes.style = 'column-count:' .. opt
126+
div.attributes.style = 'column-count: ' .. opt
109127
end
110128
div.attributes['column-count'] = nil
129+
-- column-count is "consumed" by the filter otherwise it would appear as
130+
-- data-column-count="…" in the resulting document
111131
returned_list = List:new{pandoc.Div(div.content, div.attr)}
112132
end
113133
end

column-div/expected.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ <h1 class="title">Test</h1>
2121
</header>
2222
<h1 id="column-div-test">column-div test</h1>
2323
<p>content…</p>
24+
<div id="thisdivdoesnothing">
25+
<p>content…</p>
26+
</div>
2427
<h2 id="three-columns">Three columns</h2>
25-
<div class="multicols" style="column-count:3">
28+
<div class="anotherclassname multicols" style="column-count: 3">
2629
<p>content…</p>
2730
<p>content…</p>
2831
<p>content…</p>
2932
</div>
30-
<h2 id="two-unbalanced-columns">Two unbalanced columns</h2>
33+
<h2 id="two-uneven-columns">Two uneven columns</h2>
3134
<div class="columns">
3235
<div class="column" data-valign="b" style="width:40%;">
3336
<p>contents…</p>
@@ -36,7 +39,7 @@ <h2 id="two-unbalanced-columns">Two unbalanced columns</h2>
3639
</div>
3740
</div>
3841
<h2 id="columns-in-columns">Columns in columns</h2>
39-
<div class="multicols" style="column-count:3">
42+
<div class="multicols" style="column-count: 3">
4043
<div class="columns">
4144
<div class="column" data-valign="b" style="width:20%;">
4245
<p>contents…</p>

column-div/expected.tex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ \section{column-div test}\label{column-div-test}}
33

44
content\ldots{}
55

6+
\leavevmode\hypertarget{thisdivdoesnothing}{}%
7+
content\ldots{}
8+
69
\hypertarget{three-columns}{%
710
\subsection{Three columns}\label{three-columns}}
811

@@ -16,8 +19,8 @@ \subsection{Three columns}\label{three-columns}}
1619

1720
\end{multicols}
1821

19-
\hypertarget{two-unbalanced-columns}{%
20-
\subsection{Two unbalanced columns}\label{two-unbalanced-columns}}
22+
\hypertarget{two-uneven-columns}{%
23+
\subsection{Two uneven columns}\label{two-uneven-columns}}
2124

2225
\begin{minipage}[b]{0.4\columnwidth}
2326

column-div/sample.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ header-includes:
1111
# column-div test
1212
content...
1313

14+
::: {#thisdivdoesnothing}
15+
content...
16+
:::
17+
1418
## Three columns
15-
::: {.multicols column-count="3"}
19+
::: {.anotherclassname .multicols column-count="3"}
1620
content...
1721

1822
content...
1923

2024
content...
2125
:::
2226

23-
## Two unbalanced columns
27+
## Two uneven columns
2428
:::::: {.columns}
2529
::: {.column width="40%" valign="b"}
2630
contents...

0 commit comments

Comments
 (0)