Skip to content

Commit

Permalink
Use unknown attributes as image attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed Aug 15, 2024
1 parent d67533b commit 9a506ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
56 changes: 35 additions & 21 deletions _extensions/diagram/diagram.lua
Original file line number Diff line number Diff line change
Expand Up @@ -388,37 +388,51 @@ local function diagram_options (cb, comment_start)
attribs[key] = value
end

-- Read caption attribute as Markdown
local caption = attribs.caption
and pandoc.read(attribs.caption).blocks
or nil
local fig_attr = {
id = cb.identifier ~= '' and cb.identifier or attribs.label,
name = attribs.name,
}
local alt
local caption
local fig_attr = {id = cb.identifier ~= '' and cb.identifier}
local filename
local image_attr = {}
local user_opt = {}

for k, v in pairs(attribs) do
local prefix, key = k:match '^(%a+)%-(%a[-%w]*)$'
if prefix == 'fig' then
fig_attr[key] = v
elseif prefix == 'opt' then
user_opt[key] = v
for attr_name, value in pairs(attribs) do
if attr_name == 'alt' then
alt = value
elseif attr_name == 'caption' then
-- Read caption attribute as Markdown
caption = attribs.caption
and pandoc.read(attribs.caption).blocks
or nil
elseif attr_name == 'filename' then
filename = value
elseif attr_name == 'label' then
fig_attr.id = value
elseif attr_name == 'name' then
fig_attr.name = value
else
-- Check for prefixed attributes
local prefix, key = attr_name:match '^(%a+)%-(%a[-%w]*)$'
if prefix == 'fig' then
fig_attr[key] = value
elseif prefix == 'image' or prefix == 'img' then
image_attr[key] = value
elseif prefix == 'opt' then
user_opt[key] = value
else
-- Use as image attribute
image_attr[attr_name] = value
end
end
end

return {
['alt'] = attribs.alt or
['alt'] = alt or
(caption and pandoc.utils.blocks_to_inlines(caption)) or
{},
['caption'] = caption,
['fig-attr'] = fig_attr,
['filename'] = attribs.filename,
['image-attr'] = {
height = attribs.height,
width = attribs.width,
style = attribs.style,
},
['filename'] = filename,
['image-attr'] = image_attr,
['opt'] = user_opt,
}
end
Expand Down
2 changes: 1 addition & 1 deletion test/expected-plantuml.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 id="authentication">Authentication</h1>
<figure id="fig-auth">
<img src="auth.svg" style="width:50.0%"
<img src="auth.svg" class="important" style="width:50.0%"
alt="This is an image, created by PlantUML." />
<figcaption aria-hidden="true"><p>This is an image, created by
<strong>PlantUML</strong>.</p></figcaption>
Expand Down
1 change: 1 addition & 0 deletions test/input-plantuml.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

```{.plantuml caption="This is an image, created by **PlantUML**." width=50% filename=auth}
'| label: fig-auth
'| class: important
@startuml
Alice -> Bob: Authentication Request Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request Alice <-- Bob: another Response
Expand Down

0 comments on commit 9a506ba

Please sign in to comment.