Skip to content

Commit c887e85

Browse files
committed
add color (html and latex) and background-color (html) processing plus
small code refactoring
1 parent 2cc9018 commit c887e85

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

column-div/column-div.lua

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--[[
22
column-div - leverage Pandoc native divs to make balanced and unbalanced column
3-
and other things based on class name and attirbutes.
3+
and other things based on class name and attributes.
44
55
Copyright: © 2021 Christophe Agathon <[email protected]>
66
License: MIT – see LICENSE file for details
@@ -50,7 +50,7 @@ function Div(div)
5050
end
5151
-- build the returned list of blocks
5252
begin_env = List:new{pandoc.RawBlock('tex',
53-
'\\begin' .. '{' .. env .. '}' .. options)}
53+
'\\begin{' .. env .. '}' .. options)}
5454
end_env = List:new{pandoc.RawBlock('tex', '\\end{' .. env .. '}')}
5555
returned_list = begin_env .. div.content .. end_env
5656

@@ -69,8 +69,17 @@ function Div(div)
6969
if opt then options = '[' .. opt .. ']' .. options end
7070

7171
begin_env = List:new{pandoc.RawBlock('tex',
72-
'\\begin' .. '{' .. 'minipage' .. '}' .. options)}
73-
end_env = List:new{pandoc.RawBlock('tex', '\\end{' .. 'minipage' .. '}')}
72+
'\\begin{minipage}' .. options)}
73+
end_env = List:new{pandoc.RawBlock('tex', '\\end{minipage}')}
74+
75+
-- add support for color TODO: background
76+
opt = div.attributes.color
77+
if opt then
78+
begin_env = begin_env .. List:new{pandoc.RawBlock('tex',
79+
'\\color{' .. opt .. '}')}
80+
div.attributes.color = nil -- consume attribute
81+
end
82+
7483
returned_list = begin_env .. div.content .. end_env
7584

7685
elseif div.classes:includes('columns') then
@@ -109,25 +118,39 @@ function Div(div)
109118
end
110119

111120
begin_env = List:new{pandoc.RawBlock('tex',
112-
'\\begin' .. '{' .. env .. '}' .. options)}
121+
'\\begin{' .. env .. '}' .. options)}
113122
end_env = List:new{pandoc.RawBlock('tex', '\\end{' .. env .. '}')}
114123
returned_list = begin_env .. div.content .. end_env
115124
end
116125

117-
-- if the format is html add support for multi columns
126+
-- if the format is html add what is not already done by plain pandoc
118127
elseif FORMAT:match 'html' then
128+
local style
129+
-- add support for multi columns
119130
opt = div.attributes['column-count']
120131
if opt then
121-
-- add column-count to style if it exists
122-
if div.attributes.style then
123-
div.attributes.style = div.attributes.style ..
124-
'; column-count: ' .. opt
125-
else
126-
div.attributes.style = 'column-count: ' .. opt
127-
end
132+
-- add column-count to style
133+
style = 'column-count: ' .. opt .. ';' .. (style or '')
128134
div.attributes['column-count'] = nil
129135
-- column-count is "consumed" by the filter otherwise it would appear as
130136
-- data-column-count="…" in the resulting document
137+
end
138+
-- add support for color TODO: latex counterpart
139+
opt = div.attributes.color
140+
if opt then
141+
-- add color to style
142+
style = 'color: ' .. opt .. ';' .. (style or '')
143+
div.attributes.color = nil -- consume attribute
144+
end
145+
opt = div.attributes['background-color']
146+
if opt then
147+
-- add color to style
148+
style = 'background-color: ' .. opt .. ';' .. (style or '')
149+
div.attributes['background-color'] = nil -- consume attribute
150+
end
151+
-- if we have style then build returned list
152+
if style then
153+
div.attributes.style = style .. (div.attributes.style or '')
131154
returned_list = List:new{pandoc.Div(div.content, div.attr)}
132155
end
133156
end

0 commit comments

Comments
 (0)