File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments