Skip to content

Commit 4e6b9d0

Browse files
author
james-ball-qualcomm
authored
Merge pull request #131 from Timmmm/user/timh/substitute_macros
Apply macro substitutions to inline tagged nodes
2 parents be0bd4d + e681bb1 commit 4e6b9d0

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

converters/tags.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ def convert(node, transform = node.node_name, opts = nil)
102102

103103
private
104104

105+
# Map inline quoted types to their opening and closing tags.
106+
# These are chosen so that they sort of work as asciidoc.
107+
(QUOTE_TAGS = {
108+
monospaced: ['`', '`'],
109+
emphasis: ['_', '_'],
110+
strong: ['*', '*'],
111+
double: ['"', '"'],
112+
single: ['\'', '\''],
113+
mark: ['##', '##'], # yellow highlight
114+
superscript: ['^', '^'],
115+
subscript: ['~', '~'],
116+
# There's also 'asciimath' and 'latexmath' and maybe more (hard to tell
117+
# due to Ruby's dynamic typing). The RISC-V ISA manual currently doesn't
118+
# use these though.
119+
}).default = ['', '']
120+
105121
# Return the text content of a node. Adapted from `text-converter.rb`
106122
# in the docs: https://docs.asciidoctor.org/asciidoctor/latest/convert/custom/
107123
#
@@ -153,14 +169,38 @@ def node_text_content(node)
153169
# Separate table sections by ===.
154170
end.join("\n===\n")
155171
else
156-
node.inline? ? node.text : ["\n", node.content].compact.join
172+
if node.inline? then
173+
if node.text.nil? then
174+
nil
175+
else
176+
# Awkwardly Asciidoctor does not apply substitutions (e.g. `{ge}`) to
177+
# inline nodes. Instead the substitutions get applied by the parent block,
178+
# which means ordinarily there would be no way to extract the plain text
179+
# of just the inline node with the subtitutions applied.
180+
#
181+
# Therefore here we try to apply them manually. This assumes all sorts
182+
# of things that we can't know for sure due to Ruby's lack of static
183+
# type hints - is `node.parent` always valid for inline nodes? Does
184+
# it always have `@subs`? Do all inline nodes have `apply_subs`?
185+
#
186+
# This seems to work for now at least.
187+
text = node.apply_subs(node.text, node.parent.instance_variable_get(:@subs))
188+
189+
# We also need to add opening and closing characters for inline quoted
190+
# text (monospace, bold, italic etc).
191+
open, close = QUOTE_TAGS[node.type]
192+
%(#{open}#{text}#{close})
193+
end
194+
else
195+
["\n", node.content].compact.join
196+
end
157197
end
158198

159199
content_or_nil.nil? ? "" : content_or_nil
160200
end
161201

162202
# Convert newlines to spaces.
163-
def normalize_space text
203+
def normalize_space(text)
164204
text.tr("\n", " ")
165205
end
166206
end

0 commit comments

Comments
 (0)