|
| 1 | +--[[ |
| 2 | +# MIT License |
| 3 | +# |
| 4 | +# Copyright (c) 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 function ensure_html_deps() |
| 26 | + quarto.doc.add_html_dependency({ |
| 27 | + name = 'iconify', |
| 28 | + version = '1.0.8', |
| 29 | + scripts = {"iconify-icon.min.js"} |
| 30 | + }) |
| 31 | +end |
| 32 | + |
| 33 | +local function is_empty(s) |
| 34 | + return s == nil or s == '' |
| 35 | +end |
| 36 | + |
| 37 | +local function is_valid_size(size) |
| 38 | + if is_empty(size) then |
| 39 | + return '' |
| 40 | + end |
| 41 | + local size_table = { |
| 42 | + ["tiny"] = "0.5em", |
| 43 | + ["scriptsize"] = "0.7em", |
| 44 | + ["footnotesize"] = "0.8em", |
| 45 | + ["small"] = "0.9em", |
| 46 | + ["normalsize"] = "1em", |
| 47 | + ["large"] = "1.2em", |
| 48 | + ["Large"] = "1.5em", |
| 49 | + ["LARGE"] = "1.75em", |
| 50 | + ["huge"] = "2em", |
| 51 | + ["Huge"] = "2.5em", |
| 52 | + ["1x"] = "1em", |
| 53 | + ["2x"] = "2em", |
| 54 | + ["3x"] = "3em", |
| 55 | + ["4x"] = "4em", |
| 56 | + ["5x"] = "5em", |
| 57 | + ["6x"] = "6em", |
| 58 | + ["7x"] = "7em", |
| 59 | + ["8x"] = "8em", |
| 60 | + ["9x"] = "9em", |
| 61 | + ["10x"] = "10em", |
| 62 | + ["2xs"] = "0.625em", |
| 63 | + ["xs"] = "0.75em", |
| 64 | + ["sm"] = "0.875em", |
| 65 | + ["lg"] = "1.25em", |
| 66 | + ["xl"] = "1.5em", |
| 67 | + ["2xl"] = "2em" |
| 68 | + } |
| 69 | + for key, value in pairs(size_table) do |
| 70 | + if key == size then |
| 71 | + return 'font-size: ' .. value .. ';' |
| 72 | + end |
| 73 | + end |
| 74 | + return 'font-size: ' .. size .. ';' |
| 75 | +end |
| 76 | + |
| 77 | +return { |
| 78 | + ["iconify"] = function(args, kwargs) |
| 79 | + -- detect html (excluding epub which won't handle fa) |
| 80 | + if quarto.doc.is_format("html:js") then |
| 81 | + ensure_html_deps() |
| 82 | + local set = "fluent-emoji" |
| 83 | + local icon = pandoc.utils.stringify(args[1]) |
| 84 | + if #args > 1 then |
| 85 | + set = icon |
| 86 | + icon = pandoc.utils.stringify(args[2]) |
| 87 | + end |
| 88 | + |
| 89 | + local attributes = ' icon="' .. set .. ':' .. icon .. '"' |
| 90 | + local label = '"Icon ' .. icon .. ' from ' .. set .. ' Iconify.design set."' |
| 91 | + |
| 92 | + local size = is_valid_size(pandoc.utils.stringify(kwargs["size"])) |
| 93 | + if not is_empty(size) then |
| 94 | + attributes = attributes .. ' style="' .. size .. '"' |
| 95 | + end |
| 96 | + |
| 97 | + local aria_label = pandoc.utils.stringify(kwargs["label"]) |
| 98 | + if is_empty(aria_label) then |
| 99 | + aria_label = ' aria-label="' .. label .. '"' |
| 100 | + else |
| 101 | + attributes = attributes .. aria_label |
| 102 | + end |
| 103 | + local title = pandoc.utils.stringify(kwargs["title"]) |
| 104 | + if is_empty(title) then |
| 105 | + title = ' title="' .. label .. '"' |
| 106 | + else |
| 107 | + attributes = attributes .. title |
| 108 | + end |
| 109 | + -- local style = pandoc.utils.stringify(kwargs["style"]) |
| 110 | + -- if not is_empty(style) then |
| 111 | + -- local attributes = attributes .. ' style="' .. style .. '"' |
| 112 | + -- end |
| 113 | + local width = pandoc.utils.stringify(kwargs["width"]) |
| 114 | + if not is_empty(width) and is_empty(size) then |
| 115 | + attributes = attributes .. ' width="' .. width .. '"' |
| 116 | + end |
| 117 | + local height = pandoc.utils.stringify(kwargs["height"]) |
| 118 | + if not is_empty(height) and is_empty(size) then |
| 119 | + attributes = attributes .. ' height="' .. height .. '"' |
| 120 | + end |
| 121 | + local flip = pandoc.utils.stringify(kwargs["flip"]) |
| 122 | + if not is_empty(flip) then |
| 123 | + attributes = attributes .. ' flip="' .. flip.. '"' |
| 124 | + end |
| 125 | + local rotate = pandoc.utils.stringify(kwargs["rotate"]) |
| 126 | + if not is_empty(rotate) then |
| 127 | + attributes = attributes .. ' rotate="' .. rotate .. '"' |
| 128 | + end |
| 129 | + |
| 130 | + return pandoc.RawInline( |
| 131 | + 'html', |
| 132 | + '<iconify-icon inline' .. attributes .. '></iconify-icon>' |
| 133 | + ) |
| 134 | + else |
| 135 | + return pandoc.Null() |
| 136 | + end |
| 137 | + end |
| 138 | +} |
0 commit comments