summaryrefslogtreecommitdiff
path: root/_extensions/jmbuhr/qrcode/qrcode.lua
blob: 4b371db0076b38c02ace3cef98580198ca4fac58 (plain)
  1. -- for development:
  2. local p = quarto.log.warning
  3. ---Format string like in bash or python,
  4. ---e.g. f('Hello ${one}', {one = 'world'})
  5. ---@param s string The string to format
  6. ---@param kwargs {[string]: string} A table with key-value replacemen pairs
  7. ---@return string
  8. local function f(s, kwargs)
  9. return (s:gsub('($%b{})', function(w) return kwargs[w:sub(3, -2)] or w end))
  10. end
  11. ---Merge user provided options with defaults
  12. ---@param userOptions table
  13. ---@return string JSON string to pass to molstar
  14. local function mergeOptions(url, userOptions)
  15. local defaultOptions = {
  16. text = url,
  17. width = 128,
  18. height = 128,
  19. colorDark = "#000000",
  20. colorLight = "#ffffff",
  21. }
  22. if userOptions == nil then
  23. return quarto.json.encode(defaultOptions)
  24. end
  25. for k, v in pairs(userOptions) do
  26. local value = pandoc.utils.stringify(v)
  27. if value == 'true' then value = true end
  28. if value == 'false' then value = false end
  29. defaultOptions[k] = value
  30. end
  31. return quarto.json.encode(defaultOptions)
  32. end
  33. ---@return string
  34. local function wrapInlineDiv(options)
  35. return [[
  36. <div id="${id}" class="qrcode"></div>
  37. <script type="text/javascript">
  38. var qrcode = new QRCode("${id}", ]]
  39. .. options .. [[);
  40. </script>
  41. ]]
  42. end
  43. ---@return string
  44. local function wrapInlineTex(url, opts)
  45. return [[
  46. \qrcode[]] .. opts .. [[]{]] .. url .. [[}
  47. ]]
  48. end
  49. return {
  50. ['qrcode'] = function(args, kwargs, _)
  51. if quarto.doc.is_format("html:js") then
  52. quarto.doc.add_html_dependency {
  53. name = 'qrcodejs',
  54. version = 'v1.0.0',
  55. scripts = { './assets/qrcode.js' },
  56. }
  57. local url = pandoc.utils.stringify(args[1])
  58. local id = 'qrcode'
  59. local maybeId = args[2]
  60. if maybeId ~= nil then
  61. id = pandoc.utils.stringify(maybeId)
  62. end
  63. local options = mergeOptions(url, kwargs)
  64. local text = wrapInlineDiv(options)
  65. return pandoc.RawBlock(
  66. 'html',
  67. f(text, { id = id })
  68. )
  69. elseif quarto.doc.is_format("pdf") then
  70. quarto.doc.use_latex_package("qrcode")
  71. local url = pandoc.utils.stringify(args[1])
  72. local opts = ""
  73. for k, v in pairs(kwargs) do
  74. if string.match(k, "^pdf") then
  75. k = string.sub(k, 4)
  76. opts = opts .. k .. "=" .. v .. ", "
  77. end
  78. end
  79. for _, v in ipairs(args) do
  80. if string.match(v, "^pdf") then
  81. v = string.sub(v, 4)
  82. opts = opts .. v .. ", "
  83. end
  84. end
  85. if string.len(opts) then
  86. opts = string.sub(opts, 1, string.len(opts) - 2)
  87. end
  88. local text = wrapInlineTex(url, opts)
  89. return pandoc.RawBlock(
  90. 'tex',
  91. text
  92. )
  93. end
  94. end,
  95. }