Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/pandoc/datadir/_format.lua
12926 views
1
-- does the table contain a value
2
local function tcontains(t,value)
3
if t and type(t)=="table" and value then
4
for _, v in ipairs (t) do
5
if v == value then
6
return true
7
end
8
end
9
return false
10
end
11
return false
12
end
13
14
local function _main()
15
-- Element format checks
16
17
local function isRaw(el)
18
return el.t == "RawBlock" or el.t == "RawInline"
19
end
20
21
local function isRawHtml(rawEl)
22
return isRaw(rawEl) and string.find(rawEl.format, "^html")
23
end
24
25
local function isRawLatex(rawEl)
26
return isRaw(rawEl) and (rawEl.format == "tex" or rawEl.format == "latex")
27
end
28
29
-- Format checks
30
31
-- Format checks based on FORMAT
32
33
-- check for latex output
34
local function isLatexOutput()
35
return FORMAT == "latex" or FORMAT == "beamer" or FORMAT == "pdf"
36
end
37
38
local function isAsciiDocOutput()
39
return FORMAT == "asciidoc" or FORMAT == "asciidoctor"
40
end
41
42
local function isBeamerOutput()
43
return FORMAT == "beamer"
44
end
45
46
-- check for docx output
47
local function isDocxOutput()
48
return FORMAT == "docx"
49
end
50
51
-- check for rtf output
52
local function isRtfOutput()
53
return FORMAT == "rtf"
54
end
55
56
-- check for odt output
57
local function isOdtOutput()
58
return FORMAT == "odt" or FORMAT == "opendocument"
59
end
60
61
-- check for word processor output
62
local function isWordProcessorOutput()
63
return FORMAT == "docx" or FORMAT == "rtf" or isOdtOutput()
64
end
65
66
-- check for powerpoint output
67
local function isPowerPointOutput()
68
return FORMAT == "pptx"
69
end
70
71
-- check for revealjs output
72
local function isRevealJsOutput()
73
return FORMAT == "revealjs"
74
end
75
76
local function isHtmlSlideOutput()
77
local formats = {
78
"s5",
79
"dzslides",
80
"slidy",
81
"slideous",
82
"revealjs",
83
}
84
return tcontains(formats, FORMAT)
85
end
86
87
-- check for slide output
88
local function isSlideOutput()
89
return isHtmlSlideOutput() or isBeamerOutput() or isPowerPointOutput()
90
end
91
92
-- check for epub output
93
local function isEpubOutput()
94
local formats = {
95
"epub",
96
"epub2",
97
"epub3"
98
}
99
return tcontains(formats, FORMAT)
100
end
101
102
-- check for bibliography formats
103
local function isBibliographyOutput()
104
local formats = {
105
"bibtex",
106
"biblatex",
107
"csljson"
108
}
109
return tcontains(formats, FORMAT)
110
end
111
112
-- check for ipynb output
113
local function isIpynbOutput()
114
return FORMAT == "ipynb"
115
end
116
117
118
-- format checks based on quarto-custom-format
119
120
local function isDocusaurusOutput()
121
return param("quarto-custom-format", "") == "docusaurus"
122
end
123
124
local function isConfluenceOutput()
125
return param("quarto-custom-format", "") == "confluence"
126
end
127
128
-- check for markdown output
129
local function isMarkdownOutput()
130
local formats = {
131
"markdown",
132
"markdown_github",
133
"markdown_mmd",
134
"markdown_phpextra",
135
"markdown_strict",
136
"gfm",
137
"commonmark",
138
"commonmark_x",
139
"markua"
140
}
141
return tcontains(formats, FORMAT) or isDocusaurusOutput()
142
end
143
144
-- check for markdown with raw_html enabled
145
local function isMarkdownWithHtmlOutput()
146
return (isMarkdownOutput() and tcontains(PANDOC_WRITER_OPTIONS.extensions, "raw_html")) or isDocusaurusOutput()
147
end
148
149
-- check for html output
150
local function isHtmlOutput()
151
local formats = {
152
"html",
153
"html4",
154
"html5",
155
"epub",
156
"epub2",
157
"epub3"
158
}
159
return tcontains(formats, FORMAT) or isHtmlSlideOutput()
160
end
161
162
local function parse_format(raw_format)
163
local pattern = "^([%a_]+)([-+_%a]*)"
164
local i, j, format, extensions = raw_format:find(pattern)
165
if format == nil then
166
error("Warning: Invalid format " .. raw_format .. ". Assuming 'markdown'.")
167
return {
168
format = "markdown",
169
extensions = {}
170
}
171
end
172
173
local result = {
174
format = format,
175
extensions = {}
176
}
177
178
local sign_table = {
179
["-"] = false,
180
["+"] = true
181
}
182
183
if extensions ~= nil then
184
while #extensions > 0 do
185
local i, j, sign, variant = extensions:find("^([-+])([%a_]+)")
186
result.extensions[variant] = sign_table[sign]
187
extensions = extensions:sub(j+1)
188
end
189
end
190
191
return result
192
end
193
194
local function isNativeOutput()
195
return FORMAT == "native"
196
end
197
198
local function isJsonOutput()
199
return FORMAT == "json"
200
end
201
202
local function isAstOutput()
203
return isNativeOutput() or isJsonOutput()
204
end
205
206
local function isJatsOutput()
207
local formats = {
208
"jats",
209
"jats_archiving",
210
"jats_articleauthoring",
211
"jats_publishing",
212
}
213
return tcontains(formats, FORMAT)
214
end
215
216
local function isTypstOutput()
217
return FORMAT == "typst"
218
end
219
220
-- format checks based on format-identifier.base-format
221
222
local function isEmailOutput()
223
return param("format-identifier", {})["base-format"] == "email"
224
end
225
226
local function isDashboardOutput()
227
return param("format-identifier", {})["base-format"] == "dashboard"
228
end
229
230
local function isGithubMarkdownOutput()
231
return param("format-identifier", {})["base-format"] == "gfm"
232
end
233
234
235
-- format checks based on format-identifier.target-format
236
237
local function isHugoMarkdownOutput()
238
return param("format-identifier", {})["target-format"] == "hugo-md"
239
end
240
241
-- Functions used by other exports
242
243
-- we have some special rules to allow formats to behave more intuitively
244
local function isFormat(to)
245
if FORMAT == to then
246
return true
247
else
248
-- latex and pdf are synonyms
249
if to == "latex" or to == "pdf" then
250
return isLatexOutput()
251
-- odt and opendocument are synonyms
252
elseif to == "odt" or to == "opendocument" then
253
return isOdtOutput()
254
-- epub: epub, epub2, or epub3
255
elseif to:match 'epub' then
256
return isEpubOutput()
257
-- html: html, html4, html4, epub*, or slides (e.g. revealjs)
258
elseif to == "html" then
259
return isHtmlOutput()
260
elseif to == "html:js" then
261
-- html formats that support javascript (excluding epub)
262
return isHtmlOutput() and not isEpubOutput()
263
-- markdown: markdown*, commonmark*, gfm, markua
264
elseif to == "markdown" then
265
return isMarkdownOutput()
266
elseif to == "asciidoc" or to == "asciidoctor" then
267
return isAsciiDocOutput()
268
elseif to == "confluence" then
269
return isConfluenceOutput()
270
elseif to == "docusaurus" or to == "docusaurus-md" then
271
return isDocusaurusOutput()
272
elseif to == "email" then
273
return isEmailOutput()
274
elseif to == "dashboard" then
275
return isDashboardOutput()
276
elseif to == "gfm" then
277
return isGithubMarkdownOutput()
278
elseif to == "hugo-md" or to == 'hugo' then
279
return isHugoMarkdownOutput()
280
--[[ Not working yet
281
elseif to == "ipynb" then
282
return isIpynbOutput()
283
]]--
284
else
285
return false
286
end
287
end
288
end
289
290
return {
291
format_identifier = function()
292
return param("format-identifier", {})
293
end,
294
isAsciiDocOutput = isAsciiDocOutput,
295
is_asciidoc_output = isAsciiDocOutput,
296
isRawHtml = isRawHtml,
297
is_raw_html = isRawHtml,
298
isRawLatex = isRawLatex,
299
is_raw_latex = isRawLatex,
300
isFormat = isFormat,
301
is_format = isFormat,
302
isLatexOutput = isLatexOutput,
303
is_latex_output = isLatexOutput,
304
isBeamerOutput = isBeamerOutput,
305
is_beamer_output = isBeamerOutput,
306
isDocxOutput = isDocxOutput,
307
is_docx_output = isDocxOutput,
308
isRtfOutput = isRtfOutput,
309
is_rtf_output = isRtfOutput,
310
isOdtOutput = isOdtOutput,
311
is_odt_output = isOdtOutput,
312
isWordProcessorOutput = isWordProcessorOutput,
313
is_word_processor_output = isWordProcessorOutput,
314
isPowerPointOutput = isPowerPointOutput,
315
is_powerpoint_output = isPowerPointOutput,
316
isRevealJsOutput = isRevealJsOutput,
317
is_revealjs_output = isRevealJsOutput,
318
isSlideOutput = isSlideOutput,
319
is_slide_output = isSlideOutput,
320
isEpubOutput = isEpubOutput,
321
is_epub_output = isEpubOutput,
322
isGithubMarkdownOutput = isGithubMarkdownOutput,
323
is_github_markdown_output = isGithubMarkdownOutput,
324
isHugoMarkdownOutput = isHugoMarkdownOutput,
325
is_hugo_markdown_output = isHugoMarkdownOutput,
326
isMarkdownOutput = isMarkdownOutput,
327
is_markdown_output = isMarkdownOutput,
328
isMarkdownWithHtmlOutput = isMarkdownWithHtmlOutput,
329
is_markdown_with_html_output = isMarkdownWithHtmlOutput,
330
isIpynbOutput = isIpynbOutput,
331
is_ipynb_output = isIpynbOutput,
332
isHtmlOutput = isHtmlOutput,
333
is_html_output = isHtmlOutput,
334
isHtmlSlideOutput = isHtmlSlideOutput,
335
is_html_slide_output = isHtmlSlideOutput,
336
isBibliographyOutput = isBibliographyOutput,
337
is_bibliography_output = isBibliographyOutput,
338
isNativeOutput = isNativeOutput,
339
is_native_output = isNativeOutput,
340
isJsonOutput = isJsonOutput,
341
is_json_output = isJsonOutput,
342
isAstOutput = isAstOutput,
343
is_ast_output = isAstOutput,
344
isJatsOutput = isJatsOutput,
345
is_jats_output = isJatsOutput,
346
isTypstOutput = isTypstOutput,
347
is_typst_output = isTypstOutput,
348
isConfluenceOutput = isConfluenceOutput,
349
is_confluence_output = isConfluenceOutput,
350
isDocusaurusOutput = isDocusaurusOutput,
351
is_docusaurus_output = isDocusaurusOutput,
352
isDashboardOutput = isDashboardOutput,
353
is_dashboard_output = isDashboardOutput,
354
isEmailOutput = isEmailOutput,
355
is_email_output = isEmailOutput,
356
parse_format = parse_format
357
}
358
end
359
360
return _main()
361
362