forked from asciidoctor/asciidoctor-extensions-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pick-inline-macro.rb
54 lines (52 loc) · 1.63 KB
/
pick-inline-macro.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
include ::Asciidoctor
# An inline macro that picks text based on the presence of document-level attributes.
#
# Usage
#
# pick:[target-web=Web,target-desktop=Desktop]
# pick:[target-web.target-mobile="Web or mobile",target-desktop=Desktop]
#
# pick2:target-web,target-mobile@target-desktop[Web or mobile,Desktop]
#
Extensions.register do
inline_macro :pick do
#named :pick
# FIXME `using_format :short` not working in 1.5.2 because of an ordering issue
#using_format :short
match resolve_regexp(@name, :short)
# FIXME allow parse_content_as :attributes
# parse_content_as :attributes
process do |parent, target|
doc = parent.document
attrs = (AttributeList.new target).parse
valid_key = attrs.keys.find {|key|
next false unless String === key
if key.include? '.'
key.split('.').find {|key_alt| doc.attr? key_alt }
else
doc.attr? key
end
}
valid_key ? attrs[valid_key] : ''
end
end
inline_macro :pick2 do
# FIXME can't set named inside block (regexp doesn't get setup right)
#named :ifdef
# FIXME allow parse_content_as :attributes
# parse_content_as :attributes
process do |parent, target, attributes|
doc = parent.document
valid_key = target.split('@').find_index {|key|
# TODO implement + to require all keys in list to be set
if key.include? ','
key.split(',').find {|key_alt| doc.attr? key_alt }
else
doc.attr? key
end
}
valid_key ? attributes[valid_key + 1] : ''
end
end
end