Skip to content

Commit 6e6335f

Browse files
authored
refactor: update utils.lua to generic version with get_extension_config (#5)
1 parent 2bc06af commit 6e6335f

3 files changed

Lines changed: 784 additions & 64 deletions

File tree

_extensions/code-window/_modules/utils.lua

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ end
3232
--- @return string The escaped string
3333
--- @usage local escaped = M.escape_pattern("user/repo#123")
3434
function M.escape_pattern(s)
35-
local escaped = s:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
35+
local escaped = s:gsub('([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1')
3636
return escaped
3737
end
3838

@@ -43,7 +43,7 @@ end
4343
--- @usage local parts = M.split("a.b.c", ".")
4444
function M.split(str, sep)
4545
local fields = {}
46-
local pattern = string.format("([^%s]+)", sep)
46+
local pattern = string.format('([^%s]+)', sep)
4747
str:gsub(pattern, function(c) fields[#fields + 1] = c end)
4848
return fields
4949
end
@@ -80,43 +80,43 @@ end
8080
--- @param text string The text to escape
8181
--- @return string The escaped text safe for LaTeX
8282
function M.escape_latex(text)
83-
text = string.gsub(text, "\\", "\\textbackslash{}")
84-
text = string.gsub(text, "%{", "\\{")
85-
text = string.gsub(text, "%}", "\\}")
86-
text = string.gsub(text, "%$", "\\$")
87-
text = string.gsub(text, "%&", "\\&")
88-
text = string.gsub(text, "%%", "\\%%")
89-
text = string.gsub(text, "%#", "\\#")
90-
text = string.gsub(text, "%^", "\\textasciicircum{}")
91-
text = string.gsub(text, "%_", "\\_")
92-
text = string.gsub(text, "~", "\\textasciitilde{}")
83+
text = string.gsub(text, '\\', '\\textbackslash{}')
84+
text = string.gsub(text, '%{', '\\{')
85+
text = string.gsub(text, '%}', '\\}')
86+
text = string.gsub(text, '%$', '\\$')
87+
text = string.gsub(text, '%&', '\\&')
88+
text = string.gsub(text, '%%', '\\%%')
89+
text = string.gsub(text, '%#', '\\#')
90+
text = string.gsub(text, '%^', '\\textasciicircum{}')
91+
text = string.gsub(text, '%_', '\\_')
92+
text = string.gsub(text, '~', '\\textasciitilde{}')
9393
return text
9494
end
9595

9696
--- Escape special Typst characters in text.
9797
--- @param text string The text to escape
9898
--- @return string The escaped text safe for Typst
9999
function M.escape_typst(text)
100-
text = string.gsub(text, "%#", "\\#")
100+
text = string.gsub(text, '%#', '\\#')
101101
return text
102102
end
103103

104104
--- Escape special Lua pattern characters for use in string.gsub.
105105
--- @param text string The text containing characters to escape
106106
--- @return string The escaped text safe for Lua patterns
107107
function M.escape_lua_pattern(text)
108-
text = string.gsub(text, "%%", "%%%%")
109-
text = string.gsub(text, "%^", "%%^")
110-
text = string.gsub(text, "%$", "%%$")
111-
text = string.gsub(text, "%(", "%%(")
112-
text = string.gsub(text, "%)", "%%)")
113-
text = string.gsub(text, "%.", "%%.")
114-
text = string.gsub(text, "%[", "%%[")
115-
text = string.gsub(text, "%]", "%%]")
116-
text = string.gsub(text, "%*", "%%*")
117-
text = string.gsub(text, "%+", "%%+")
118-
text = string.gsub(text, "%-", "%%-")
119-
text = string.gsub(text, "%?", "%%?")
108+
text = string.gsub(text, '%%', '%%%%')
109+
text = string.gsub(text, '%^', '%%^')
110+
text = string.gsub(text, '%$', '%%$')
111+
text = string.gsub(text, '%(', '%%(')
112+
text = string.gsub(text, '%)', '%%)')
113+
text = string.gsub(text, '%.', '%%.')
114+
text = string.gsub(text, '%[', '%%[')
115+
text = string.gsub(text, '%]', '%%]')
116+
text = string.gsub(text, '%*', '%%*')
117+
text = string.gsub(text, '%+', '%%+')
118+
text = string.gsub(text, '%-', '%%-')
119+
text = string.gsub(text, '%?', '%%?')
120120
return text
121121
end
122122

@@ -168,42 +168,31 @@ function M.escape_text(text, format)
168168
if escape then
169169
return escape(text)
170170
else
171-
error("Unsupported escape format: " .. format)
171+
error('Unsupported escape format: ' .. format)
172172
end
173173
end
174174

175175
--- Converts a string to a valid HTML id by lowercasing and replacing spaces.
176176
--- @param text string The text to convert
177177
--- @return string The HTML id
178178
function M.ascii_id(text)
179-
local id = text:lower():gsub("[^a-z0-9 ]", ""):gsub(" +", "-")
179+
local id = text:lower():gsub('[^a-z0-9 ]', ''):gsub(' +', '-')
180180
return id
181181
end
182182

183183
-- ============================================================================
184184
-- METADATA UTILITIES
185185
-- ============================================================================
186186

187-
--- Get configuration from extensions.mcanouil namespace.
187+
--- Get configuration from extensions.name namespace.
188188
--- @param meta table Document metadata
189-
--- @param key string The key to retrieve
190-
--- @return any The value or nil
191-
--- @usage local value = M.get_mcanouil_config(meta, 'section-outline')
192-
function M.get_mcanouil_config(meta, key)
193-
local mcanouil_ext = meta.extensions and meta.extensions.mcanouil
194-
if not mcanouil_ext then return nil end
195-
return mcanouil_ext[key]
196-
end
197-
198-
--- Get a section config table from extensions.mcanouil.{section}.
199-
--- @param meta table Document metadata
200-
--- @param section string The section name (e.g., 'code-window', 'typst-markdown')
201-
--- @return table|nil The section config table or nil
202-
--- @usage local config = M.get_mcanouil_section(meta, 'code-window')
203-
function M.get_mcanouil_section(meta, section)
204-
local mcanouil_ext = meta.extensions and meta.extensions.mcanouil
205-
if not mcanouil_ext then return nil end
206-
return mcanouil_ext[section]
189+
--- @param extension_name string The extension name (e.g., "github", "iconify")
190+
--- @return any The value/table or nil
191+
--- @usage local value = M.get_extension_config(meta, 'section-outline')
192+
function M.get_extension_config(meta, extension_name)
193+
local config_ext = meta.extensions and meta.extensions[extension_name]
194+
if not config_ext then return nil end
195+
return config_ext
207196
end
208197

209198
--- Extract metadata value from document meta using nested structure.
@@ -314,18 +303,18 @@ end
314303
--- @return string The output format ("pptx", "html", "latex", "typst", "docx", or "unknown")
315304
--- @return string The language of the output format
316305
function M.get_quarto_format()
317-
if quarto.doc.is_format("html:js") then
318-
return "html", "html"
319-
elseif quarto.doc.is_format("latex") then
320-
return "latex", "latex"
321-
elseif quarto.doc.is_format("typst") then
322-
return "typst", "typst"
323-
elseif quarto.doc.is_format("docx") then
324-
return "docx", "openxml"
325-
elseif quarto.doc.is_format("pptx") then
326-
return "pptx", "openxml"
306+
if quarto.doc.is_format('html:js') then
307+
return 'html', 'html'
308+
elseif quarto.doc.is_format('latex') then
309+
return 'latex', 'latex'
310+
elseif quarto.doc.is_format('typst') then
311+
return 'typst', 'typst'
312+
elseif quarto.doc.is_format('docx') then
313+
return 'docx', 'openxml'
314+
elseif quarto.doc.is_format('pptx') then
315+
return 'pptx', 'openxml'
327316
else
328-
return "unknown", "unknown"
317+
return 'unknown', 'unknown'
329318
end
330319
end
331320

@@ -346,7 +335,7 @@ function M.is_object_empty(obj)
346335
end
347336
return count
348337
end
349-
if pandoc.utils.type(obj) == "table" or pandoc.utils.type(obj) == "List" then
338+
if pandoc.utils.type(obj) == 'table' or pandoc.utils.type(obj) == 'List' then
350339
return obj == nil or obj == '' or length(obj) == 0
351340
else
352341
return obj == nil or obj == ''
@@ -357,14 +346,14 @@ end
357346
--- @param obj any The object to check
358347
--- @return boolean true if the object is a string, number, or boolean
359348
function M.is_type_simple(obj)
360-
return pandoc.utils.type(obj) == "string" or pandoc.utils.type(obj) == "number" or pandoc.utils.type(obj) == "boolean"
349+
return pandoc.utils.type(obj) == 'string' or pandoc.utils.type(obj) == 'number' or pandoc.utils.type(obj) == 'boolean'
361350
end
362351

363352
--- Check if an object is a function or userdata
364353
--- @param obj any The object to check
365354
--- @return boolean true if the object is a function or userdata
366355
function M.is_function_userdata(obj)
367-
return pandoc.utils.type(obj) == "function" or pandoc.utils.type(obj) == "userdata"
356+
return pandoc.utils.type(obj) == 'function' or pandoc.utils.type(obj) == 'userdata'
368357
end
369358

370359
--- Get nested value from object using field path
@@ -561,7 +550,7 @@ end
561550
--- @param message string The error message to display
562551
--- @usage M.log_error("external", "Could not open file 'example.md'.")
563552
function M.log_error(extension_name, message)
564-
quarto.log.error("[" .. extension_name .. "] " .. message)
553+
quarto.log.error('[' .. extension_name .. '] ' .. message)
565554
end
566555

567556
--- Format and log a warning message with extension prefix.
@@ -572,7 +561,7 @@ end
572561
--- @param message string The warning message to display
573562
--- @usage M.log_warning("lua-env", "No variable name provided.")
574563
function M.log_warning(extension_name, message)
575-
quarto.log.warning("[" .. extension_name .. "] " .. message)
564+
quarto.log.warning('[' .. extension_name .. '] ' .. message)
576565
end
577566

578567
--- Format and log an output message with extension prefix.
@@ -583,7 +572,7 @@ end
583572
--- @param message string The informational message to display
584573
--- @usage M.log_output("lua-env", "Exported metadata to: output.json")
585574
function M.log_output(extension_name, message)
586-
quarto.log.output("[" .. extension_name .. "] " .. message)
575+
quarto.log.output('[' .. extension_name .. '] ' .. message)
587576
end
588577

589578
-- ============================================================================
@@ -603,7 +592,7 @@ function M.resolve_project_path(path)
603592
return path
604593
end
605594

606-
if path:sub(1, 1) == "/" then
595+
if path:sub(1, 1) == '/' then
607596
if quarto.project.directory then
608597
-- Prepend project directory to absolute path
609598
return quarto.project.directory .. path

example-typst.pdf

89.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)