Skip to content

Commit b23611c

Browse files
committed
Move table handling patterns and function to shared location
1 parent a9b9320 commit b23611c

File tree

3 files changed

+70
-65
lines changed

3 files changed

+70
-65
lines changed

src/resources/filters/common/tables.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,40 @@ function createTableCaption(caption, attr)
105105
end
106106
end
107107

108+
109+
function countTables(div)
110+
local tables = 0
111+
pandoc.walk_block(div, {
112+
Table = function(table)
113+
tables = tables + 1
114+
end,
115+
RawBlock = function(raw)
116+
if hasRawHtmlTable(raw) or hasRawLatexTable(raw) then
117+
tables = tables + 1
118+
end
119+
end
120+
})
121+
return tables
122+
end
123+
124+
125+
function hasRawHtmlTable(raw)
126+
if _quarto.format.isRawHtml(raw) and _quarto.format.isHtmlOutput() then
127+
return raw.text:match(htmlTablePattern())
128+
else
129+
return false
130+
end
131+
end
132+
133+
function hasRawLatexTable(raw)
134+
if _quarto.format.isRawLatex(raw) and _quarto.format.isLatexOutput() then
135+
for i,pattern in ipairs(_quarto.patterns.latexTablePatterns) do
136+
if raw.text:match(pattern) then
137+
return true
138+
end
139+
end
140+
return false
141+
else
142+
return false
143+
end
144+
end

src/resources/filters/quarto-pre/table-captions.lua

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,6 @@
44
kTblCap = "tbl-cap"
55
kTblSubCap = "tbl-subcap"
66

7-
local latexTableWithOptionsPattern = "(\\begin{table}%[%w+%])(.*)(\\end{table})"
8-
local latexTablePattern = "(\\begin{table})(.*)(\\end{table})"
9-
local latexLongtablePatternwWithPosAndAlign = "(\\begin{longtable}%[[^%]]+%]{.*})(.*)(\\end{longtable})"
10-
local latexLongtablePatternWithPos = "(\\begin{longtable}%[[^%]]+%])(.*)(\\end{longtable})"
11-
local latexLongtablePatternWithAlign = "(\\begin{longtable}{.*})(.*)(\\end{longtable})"
12-
local latexLongtablePattern = "(\\begin{longtable})(.*)(\\end{longtable})"
13-
local latexTabularPatternWithPosAndAlign = "(\\begin{tabular}%[[^%]]+%]{.*})(.*)(\\end{tabular})"
14-
local latexTabularPatternWithPos = "(\\begin{tabular}%[[^%]]+%])(.*)(\\end{tabular})"
15-
local latexTabularPatternWithAlign = "(\\begin{tabular}{.*})(.*)(\\end{tabular})"
16-
local latexTabularPattern = "(\\begin{tabular})(.*)(\\end{tabular})"
17-
18-
local latexTablePatterns = pandoc.List({
19-
latexTableWithOptionsPattern,
20-
latexTablePattern,
21-
latexLongtablePatternwWithPosAndAlign,
22-
latexLongtablePatternWithPos,
23-
latexLongtablePatternWithAlign,
24-
latexLongtablePattern,
25-
latexTabularPatternWithPosAndAlign,
26-
latexTabularPatternWithPos,
27-
latexTabularPatternWithAlign,
28-
latexTabularPattern,
29-
})
30-
317
local latexCaptionPattern = "(\\caption{)(.-)(}[^\n]*\n)"
328

339
function tableCaptions()
@@ -52,8 +28,8 @@ function tableCaptions()
5228
el = pandoc.walk_block(el, {
5329
RawBlock = function(raw)
5430
if _quarto.format.isRawLatex(raw) then
55-
if raw.text:match(latexTabularPattern) and not raw.text:match(latexTablePattern) then
56-
raw.text = raw.text:gsub(latexTabularPattern,
31+
if raw.text:match(_quarto.patterns.latexTabularPattern) and not raw.text:match(_quarto.patterns.latexTablePattern) then
32+
raw.text = raw.text:gsub(_quarto.patterns.latexTabularPattern,
5733
"\\begin{table}\n\\centering\n%1%2%3\n\\end{table}\n",
5834
1)
5935
return raw
@@ -198,7 +174,7 @@ function applyTableCaptions(el, tblCaptions, tblLabels)
198174
end
199175
raw.text = raw.text:gsub(captionPattern, "%1" .. captionText:gsub("%%", "%%%%") .. "%3", 1)
200176
elseif hasRawLatexTable(raw) then
201-
for i,pattern in ipairs(latexTablePatterns) do
177+
for i,pattern in ipairs(_quarto.patterns.latexTablePatterns) do
202178
if raw.text:match(pattern) then
203179
raw.text = applyLatexTableCaption(raw.text, tblCaptions[idx], tblLabels[idx], pattern)
204180
break
@@ -250,40 +226,3 @@ function extractTblCapAttrib(el, name, subcap)
250226
end
251227
return nil
252228
end
253-
254-
function countTables(div)
255-
local tables = 0
256-
pandoc.walk_block(div, {
257-
Table = function(table)
258-
tables = tables + 1
259-
end,
260-
RawBlock = function(raw)
261-
if hasRawHtmlTable(raw) or hasRawLatexTable(raw) then
262-
tables = tables + 1
263-
end
264-
end
265-
})
266-
return tables
267-
end
268-
269-
270-
function hasRawHtmlTable(raw)
271-
if _quarto.format.isRawHtml(raw) and _quarto.format.isHtmlOutput() then
272-
return raw.text:match(htmlTablePattern())
273-
else
274-
return false
275-
end
276-
end
277-
278-
function hasRawLatexTable(raw)
279-
if _quarto.format.isRawLatex(raw) and _quarto.format.isLatexOutput() then
280-
for i,pattern in ipairs(latexTablePatterns) do
281-
if raw.text:match(pattern) then
282-
return true
283-
end
284-
end
285-
return false
286-
else
287-
return false
288-
end
289-
end

src/resources/pandoc/datadir/init.lua

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,11 +1463,40 @@ local function resolveDependencyLinkTags(linkTags)
14631463
end
14641464
end
14651465

1466+
local latexTableWithOptionsPattern = "(\\begin{table}%[%w+%])(.*)(\\end{table})"
1467+
local latexTablePattern = "(\\begin{table})(.*)(\\end{table})"
1468+
local latexLongtablePatternwWithPosAndAlign = "(\\begin{longtable}%[[^%]]+%]{.*})(.*)(\\end{longtable})"
1469+
local latexLongtablePatternWithPos = "(\\begin{longtable}%[[^%]]+%])(.*)(\\end{longtable})"
1470+
local latexLongtablePatternWithAlign = "(\\begin{longtable}{.*})(.*)(\\end{longtable})"
1471+
local latexLongtablePattern = "(\\begin{longtable})(.*)(\\end{longtable})"
1472+
local latexTabularPatternWithPosAndAlign = "(\\begin{tabular}%[[^%]]+%]{.*})(.*)(\\end{tabular})"
1473+
local latexTabularPatternWithPos = "(\\begin{tabular}%[[^%]]+%])(.*)(\\end{tabular})"
1474+
local latexTabularPatternWithAlign = "(\\begin{tabular}{.*})(.*)(\\end{tabular})"
1475+
local latexTabularPattern = "(\\begin{tabular})(.*)(\\end{tabular})"
1476+
1477+
local latexTablePatterns = pandoc.List({
1478+
latexTableWithOptionsPattern,
1479+
latexTablePattern,
1480+
latexLongtablePatternwWithPosAndAlign,
1481+
latexLongtablePatternWithPos,
1482+
latexLongtablePatternWithAlign,
1483+
latexLongtablePattern,
1484+
latexTabularPatternWithPosAndAlign,
1485+
latexTabularPatternWithPos,
1486+
latexTabularPatternWithAlign,
1487+
latexTabularPattern,
1488+
})
1489+
14661490
-- Quarto internal module - makes functions available
14671491
-- through the filters
14681492
_quarto = {
14691493
processDependencies = processDependencies,
1470-
format = format
1494+
format = format,
1495+
patterns = {
1496+
latexTabularPattern = latexTabularPattern,
1497+
latexTablePattern = latexTablePattern,
1498+
latexTablePatterns = latexTablePatterns
1499+
}
14711500
}
14721501

14731502
-- The main exports of the quarto module

0 commit comments

Comments
 (0)