|
| 1 | +--[[ |
| 2 | +# MIT License |
| 3 | +# |
| 4 | +# Copyright (c) 2025 Mickaël Canouil |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | +]] |
| 24 | + |
| 25 | +local stringify = pandoc.utils.stringify |
| 26 | + |
| 27 | +--- Ensure Iconify HTML dependencies are included. |
| 28 | +--- @return nil |
| 29 | +local function ensure_html_deps() |
| 30 | + quarto.doc.add_html_dependency({ |
| 31 | + name = 'iconify', |
| 32 | + version = '3.0.0', |
| 33 | + scripts = { 'iconify-icon.min.js' } |
| 34 | + }) |
| 35 | +end |
| 36 | + |
| 37 | +--- Check if a string is empty or nil. |
| 38 | +--- @param s string|nil |
| 39 | +--- @return boolean |
| 40 | +local function is_empty(s) |
| 41 | + return s == nil or s == '' |
| 42 | +end |
| 43 | + |
| 44 | +--- Validate and convert size keyword to CSS font-size. |
| 45 | +--- @param size string|nil |
| 46 | +--- @return string |
| 47 | +local function is_valid_size(size) |
| 48 | + if is_empty(size) then |
| 49 | + return '' |
| 50 | + end |
| 51 | + local size_table = { |
| 52 | + ["tiny"] = "0.5em", |
| 53 | + ["scriptsize"] = "0.7em", |
| 54 | + ["footnotesize"] = "0.8em", |
| 55 | + ["small"] = "0.9em", |
| 56 | + ["normalsize"] = "1em", |
| 57 | + ["large"] = "1.2em", |
| 58 | + ["Large"] = "1.5em", |
| 59 | + ["LARGE"] = "1.75em", |
| 60 | + ["huge"] = "2em", |
| 61 | + ["Huge"] = "2.5em", |
| 62 | + ["1x"] = "1em", |
| 63 | + ["2x"] = "2em", |
| 64 | + ["3x"] = "3em", |
| 65 | + ["4x"] = "4em", |
| 66 | + ["5x"] = "5em", |
| 67 | + ["6x"] = "6em", |
| 68 | + ["7x"] = "7em", |
| 69 | + ["8x"] = "8em", |
| 70 | + ["9x"] = "9em", |
| 71 | + ["10x"] = "10em", |
| 72 | + ["2xs"] = "0.625em", |
| 73 | + ["xs"] = "0.75em", |
| 74 | + ["sm"] = "0.875em", |
| 75 | + ["lg"] = "1.25em", |
| 76 | + ["xl"] = "1.5em", |
| 77 | + ["2xl"] = "2em" |
| 78 | + } |
| 79 | + for key, value in pairs(size_table) do |
| 80 | + if key == size then |
| 81 | + return 'font-size: ' .. value .. ';' |
| 82 | + end |
| 83 | + end |
| 84 | + return 'font-size: ' .. size .. ';' |
| 85 | +end |
| 86 | + |
| 87 | +--- Get iconify option from arguments or metadata. |
| 88 | +--- @param x string |
| 89 | +--- @param arg table |
| 90 | +--- @param meta table |
| 91 | +--- @return string |
| 92 | +local function get_iconify_options(x, arg, meta) |
| 93 | + local arg_value = stringify(arg[x]) |
| 94 | + if is_empty(meta['iconify']) or not is_empty(arg_value) then |
| 95 | + return arg_value |
| 96 | + end |
| 97 | + if not is_empty(meta['iconify'][x]) then |
| 98 | + return stringify(meta['iconify'][x]) |
| 99 | + end |
| 100 | + return arg_value |
| 101 | +end |
| 102 | + |
| 103 | +--- Render an Iconify icon as a Pandoc RawInline for HTML output. |
| 104 | +--- @param args table Icon arguments (icon set and name). |
| 105 | +--- @param kwargs table Key-value options for the icon. |
| 106 | +--- @param meta table Document metadata. |
| 107 | +--- @return pandoc.Inline|pandoc.Null |
| 108 | +function iconify(args, kwargs, meta) |
| 109 | + -- detect html (excluding epub which won't handle fa) |
| 110 | + if quarto.doc.is_format("html:js") then |
| 111 | + ensure_html_deps() |
| 112 | + local icon = stringify(args[1]) |
| 113 | + local set = 'octicon' |
| 114 | + if not is_empty(meta['iconify']) and not is_empty(meta['iconify']['set']) then |
| 115 | + set = stringify(meta['iconify']['set']) |
| 116 | + end |
| 117 | + |
| 118 | + if #args > 1 and string.find(stringify(args[2]), ':') then |
| 119 | + quarto.log.warning( |
| 120 | + 'Use "set:icon" or "set icon" syntax, not both! ' .. |
| 121 | + 'Using "set:icon" syntax and discarding first argument!' |
| 122 | + ) |
| 123 | + icon = stringify(args[2]) |
| 124 | + end |
| 125 | + |
| 126 | + if string.find(icon, ":") then |
| 127 | + set = string.sub(icon, 1, string.find(icon, ":") - 1) |
| 128 | + icon = string.sub(icon, string.find(icon, ":") + 1) |
| 129 | + elseif #args > 1 then |
| 130 | + set = icon |
| 131 | + icon = stringify(args[2]) |
| 132 | + end |
| 133 | + |
| 134 | + local attributes = ' icon="' .. set .. ':' .. icon .. '"' |
| 135 | + local default_label = 'Icon ' .. icon .. ' from ' .. set .. ' Iconify.design set.' |
| 136 | + |
| 137 | + local size = is_valid_size(get_iconify_options("size", kwargs, meta)) |
| 138 | + local style = get_iconify_options("style", kwargs, meta) |
| 139 | + |
| 140 | + if is_empty(style) and not is_empty(size) then |
| 141 | + attributes = attributes .. ' style="' .. size .. '"' |
| 142 | + elseif not is_empty(style) and not is_empty(size) then |
| 143 | + attributes = attributes .. ' style="' .. style .. ';' .. size .. '"' |
| 144 | + elseif not is_empty(style) then |
| 145 | + attributes = attributes .. ' style="' .. style .. '"' |
| 146 | + end |
| 147 | + |
| 148 | + local aria_label = stringify(kwargs["label"]) |
| 149 | + if is_empty(aria_label) then |
| 150 | + aria_label = ' aria-label="' .. default_label .. '"' |
| 151 | + else |
| 152 | + aria_label = ' aria-label="' .. aria_label .. '"' |
| 153 | + end |
| 154 | + |
| 155 | + local title = stringify(kwargs["title"]) |
| 156 | + if is_empty(title) then |
| 157 | + title = ' title="' .. default_label .. '"' |
| 158 | + else |
| 159 | + title = ' title="' .. title .. '"' |
| 160 | + end |
| 161 | + |
| 162 | + attributes = attributes .. aria_label .. title |
| 163 | + |
| 164 | + local width = get_iconify_options("width", kwargs, meta) |
| 165 | + if not is_empty(width) and is_empty(size) then |
| 166 | + attributes = attributes .. ' width="' .. width .. '"' |
| 167 | + end |
| 168 | + local height = get_iconify_options("height", kwargs, meta) |
| 169 | + if not is_empty(height) and is_empty(size) then |
| 170 | + attributes = attributes .. ' height="' .. height .. '"' |
| 171 | + end |
| 172 | + local flip = get_iconify_options("flip", kwargs, meta) |
| 173 | + if not is_empty(flip) then |
| 174 | + attributes = attributes .. ' flip="' .. flip.. '"' |
| 175 | + end |
| 176 | + local rotate = get_iconify_options("rotate", kwargs, meta) |
| 177 | + if not is_empty(rotate) then |
| 178 | + attributes = attributes .. ' rotate="' .. rotate .. '"' |
| 179 | + end |
| 180 | + |
| 181 | + local inline = get_iconify_options("inline", kwargs, meta) |
| 182 | + if is_empty(inline) or inline ~= "false" then |
| 183 | + attributes = ' inline ' .. attributes |
| 184 | + end |
| 185 | + |
| 186 | + local mode = get_iconify_options("mode", kwargs, meta) |
| 187 | + local valid_modes = { svg = true, style = true, bg = true, mask = true } |
| 188 | + if not is_empty(mode) and valid_modes[mode] then |
| 189 | + attributes = attributes .. ' mode="' .. mode .. '"' |
| 190 | + end |
| 191 | + |
| 192 | + return pandoc.RawInline( |
| 193 | + 'html', |
| 194 | + '<iconify-icon role="img"' .. attributes .. '></iconify-icon>' |
| 195 | + ) |
| 196 | + else |
| 197 | + return pandoc.Null() |
| 198 | + end |
| 199 | +end |
| 200 | + |
| 201 | +return { |
| 202 | + ["iconify"] = iconify |
| 203 | +} |
0 commit comments