Skip to content

Commit

Permalink
feat: create a lua shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurData committed Sep 23, 2024
1 parent 405238b commit fcd782b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
41 changes: 41 additions & 0 deletions _extensions/i18n/18n_select.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

local function isEmpty(o)
return o == nil or o == ''
end

local function split(inputstr, sep)
sep = sep or "%s"
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end

local function generateI18nSelect(choices, selected)
local selectTag = '<select id="data-i18n-switcher" class="reaveljs-select">\n'

for _, choice in ipairs(choices) do
local value, label = table.unpack(split(choice, ":"))
label = label or value
local isSelected = (value == selected) and ' selected' or ''
selectTag = selectTag .. string.format('<option class="drop-item" value="%s"%s>%s</option>\n', value, isSelected, label)
end

selectTag = selectTag .. '</select>\n'
return selectTag
end

return {
['i18n-select'] = function(args, kwargs)
if not quarto.doc.isFormat("html:js") then
return pandoc.Null()
end

local choices = split(kwargs["choices"] or "", ",%s*")
local selected = kwargs["selected"] or choices[1] or ""
local text = generateI18nSelect(choices, selected)

return pandoc.RawBlock('html', text)
end
}
2 changes: 2 additions & 0 deletions _extensions/i18n/_extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ author: Arthur Bréant
version: 1.0.0
quarto-required: ">=1.5.0"
contributes:
shortcodes:
- 18n_select.lua
revealjs-plugins:
- name: RevealI18n
stylesheet:
Expand Down
9 changes: 2 additions & 7 deletions example.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ title: Revealjs i18n
format:
revealjs:
footer: |
<select id="data-i18n-switcher" class="reaveljs-select">
<option selected class="drop-item" value="en">English</option>
<option class="drop-item" value="fr">French</option>
<option class="drop-item" value="de">Deutsch</option>
<option class="drop-item" value="it">Italian</option>
</select>
{{< i18n-select choices="fr:Français, en:English, de:Deutsch, it:Italiano" selected="fr" >}}
i18n:
defaultLocale: "en"
defaultLocale: "fr"
fr:
morning: "Le matin"
getting-up: "Se lever"
Expand Down

0 comments on commit fcd782b

Please sign in to comment.