Skip to content

Commit

Permalink
implicit named attribute overrides explicit named attribute if set ea…
Browse files Browse the repository at this point in the history
…rlier in document order (PR #32)
  • Loading branch information
mojavelinux authored Feb 28, 2024
1 parent f4ae21c commit 5560314
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
19 changes: 11 additions & 8 deletions grammar/asciidoc-block.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,17 @@ function processBlockMetadata (cacheKey = offset(), posattrs, contentAttributeNa
let posIdx = 0
for (const name of posattrs) {
const posKey = `$${++posIdx}`
if (name == null) continue
if (!(posKey in attributes)) continue
// Q: should existing named attribute be allowed to take precedence? (this has never been the case)
if (name in attributes) names.splice(names.indexOf(name), 1)
names.splice(names.indexOf(posKey), 0, name)
const valueObject = attributes[name] = attributes[posKey]
// NOTE remap value as deferred function to avoid having to resolve again for positional attribute
if (valueObject.constructor === Function) attributes[posKey] = () => attributes[name]
if (name == null || !names.includes(posKey)) continue
if (names.includes(name)) {
if (names.indexOf(posKey) < names.indexOf(name)) continue
} else {
// insert synthetic named attribute ahead of positional attribute
names.splice(names.indexOf(posKey), 0, name)
}
// assign positional attribute to named attribute, overwriting explicit named attribute if it exists
attributes[name] = attributes[posKey]
// remap positional attribute to resolved value of implicit named attribute (which will be resolved first)
attributes[posKey] = () => attributes[name]
}
}
const promote = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alt=Player Status]
image::player-status.png[Player Status Window,600,800]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "document",
"type": "block",
"blocks": [
{
"name": "image",
"type": "block",
"form": "macro",
"target": "player-status.png",
"location": [{ "line": 2, "col": 1 }, { "line": 2, "col": 54 }],
"metadata": {
"attributes": {
"alt": "Player Status Window",
"$1": "Player Status Window",
"$2": "600",
"$3": "800",
"width": "600",
"height": "800"
},
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 19 }]
}
}
],
"location": [{ "line": 1, "col": 1 }, { "line": 2, "col": 54 }]
}

0 comments on commit 5560314

Please sign in to comment.