Skip to content

Commit 45d29d9

Browse files
authored
Merge pull request #47 from Timmmm/main
Add tags backend
2 parents 177f7a6 + 8a22503 commit 45d29d9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

converters/tags.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'json'
2+
3+
class TagsConverter
4+
include Asciidoctor::Converter
5+
register_for("tags")
6+
7+
@tag_map = {}
8+
@prefix = ""
9+
10+
def initialize(backend, opts = {})
11+
super
12+
outfilesuffix(".tags.json")
13+
# Pass `-a tags-prefix=qx_` to only include tags starting with `qx_`.
14+
@prefix = opts[:document].attributes.fetch("tags-prefix", "")
15+
end
16+
17+
# `node` is an `AbstractNode`.
18+
def convert(node, transform = node.node_name, opts = nil)
19+
if transform == "document" then
20+
@tag_map = {}
21+
# Calling node.content will recursively call convert() on all the nodes
22+
# and also expand blocks, creating inline nodes. We call this to convert
23+
# all nodes to text, and record their content in the tag map. Then we
24+
# throw away the text and output the tag map as JSON instead.
25+
node.content
26+
JSON.pretty_generate({
27+
"tags": @tag_map,
28+
})
29+
else
30+
# Output the text content of this node.
31+
content = if node.inline? then node.text else node.content end
32+
unless node.id.nil?
33+
if node.id.start_with?(@prefix)
34+
@tag_map[node.id] = content
35+
end
36+
end
37+
content
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)