1# Copyright 2016-2023 The Khronos Group Inc.
2#
3# SPDX-License-Identifier: Apache-2.0
4
5require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
6
7include ::Asciidoctor
8
9# This is the generated map of API interfaces in this spec build
10require 'apimap.rb'
11$apiNames = APInames.new
12
13class SpecInlineMacroBase < Extensions::InlineMacroProcessor
14    use_dsl
15    using_format :short
16end
17
18class NormativeInlineMacroBase < SpecInlineMacroBase
19    def text
20        'normative'
21    end
22
23    def process parent, target, attributes
24        create_inline parent, :quoted, '<strong class="purple">' + text + '</strong>'
25    end
26end
27
28class LinkInlineMacroBase < SpecInlineMacroBase
29    # Check if a link macro target exists - overridden by specific macros
30    # Default assumption is that it does exist
31    def exists? target
32      return true
33    end
34
35    def process parent, target, attributes
36      if not exists? target
37        # If the macro target is not in this build, but has an alias,
38        # substitute that alias as the argument.
39        # Otherwise, turn the (attempted) link into text, and complain.
40        if $apiNames.nonexistent.has_key? target
41          oldtarget = target
42          target = $apiNames.nonexistent[oldtarget]
43          msg = 'Rewriting nonexistent link macro target: ' + @name.to_s + ':' + oldtarget + ' to ' + target
44          Asciidoctor::LoggerManager.logger.info msg
45          # Fall through
46        else
47          # Suppress warnings for apiext: macros as this is such a common case
48          if @name.to_s != 'apiext'
49            msg = 'Textifying unknown link macro target: ' + @name.to_s + ':' + target
50            Asciidoctor::LoggerManager.logger.warn msg
51          end
52          return create_inline parent, :quoted, '<code>' + target + '</code>'
53        end
54      end
55
56      if parent.document.attributes['cross-file-links']
57        return Inline.new(parent, :anchor, target, :type => :link, :target => (target + '.html'))
58      else
59        return Inline.new(parent, :anchor, target, :type => :xref, :target => ('#' + target), :attributes => {'fragment' => target, 'refid' => target})
60      end
61    end
62end
63
64class CodeInlineMacroBase < SpecInlineMacroBase
65    def process parent, target, attributes
66      if $apiNames.nonexistent.has_key? target
67        oldtarget = target
68        target = $apiNames.nonexistent[oldtarget]
69        msg = 'Rewriting nonexistent name macro target: ' + @name.to_s + ':' + oldtarget + ' to ' + target
70        Asciidoctor::LoggerManager.logger.info msg
71      end
72      create_inline parent, :quoted, '<code>' + target.gsub('&#8594;', '-&gt;') + '</code>'
73    end
74end
75
76class StrongInlineMacroBase < SpecInlineMacroBase
77    def process parent, target, attributes
78        create_inline parent, :quoted, '<code>' + target.gsub('&#8594;', '-&gt;') + '</code>'
79    end
80end
81
82class ParamInlineMacroBase < SpecInlineMacroBase
83    def process parent, target, attributes
84         create_inline parent, :quoted, '<code>' + target.gsub('&#8594;', '-&gt;') + '</code>'
85    end
86end
87
88class CanInlineMacro < NormativeInlineMacroBase
89    named :can
90    match /can:(\w*)/
91
92    def text
93        'can'
94    end
95end
96
97class CannotInlineMacro < NormativeInlineMacroBase
98    named :cannot
99    match /cannot:(\w*)/
100
101    def text
102        'cannot'
103    end
104end
105
106class MayInlineMacro < NormativeInlineMacroBase
107    named :may
108    match /may:(\w*)/
109
110    def text
111        'may'
112    end
113end
114
115class MustInlineMacro < NormativeInlineMacroBase
116    named :must
117    match /must:(\w*)/
118
119    def text
120        'must'
121    end
122end
123
124class OptionalInlineMacro < NormativeInlineMacroBase
125    named :optional
126    match /optional:(\w*)/
127
128    def text
129        'optional'
130    end
131end
132
133class OptionallyInlineMacro < NormativeInlineMacroBase
134    named :optionally
135    match /optionally:(\w*)/
136
137    def text
138        'optionally'
139    end
140end
141
142class RequiredInlineMacro < NormativeInlineMacroBase
143    named :required
144    match /required:(\w*)/
145
146    def text
147        'required'
148    end
149end
150
151class ShouldInlineMacro < NormativeInlineMacroBase
152    named :should
153    match /should:(\w*)/
154
155    def text
156        'should'
157    end
158end
159
160# Generic reference page link to any entity with an anchor/refpage
161class ReflinkInlineMacro < LinkInlineMacroBase
162    named :reflink
163    match /reflink:([-\w]+)/
164end
165
166# Link to an extension appendix/refpage
167class ApiextInlineMacro < LinkInlineMacroBase
168    named :apiext
169    match /apiext:(\w+)/
170
171    def exists? target
172        $apiNames.features.has_key? target
173    end
174end
175
176class FlinkInlineMacro < LinkInlineMacroBase
177    named :flink
178    match /flink:(\w+)/
179
180    def exists? target
181        $apiNames.protos.has_key? target
182    end
183end
184
185class FnameInlineMacro < CodeInlineMacroBase
186    named :fname
187    match /fname:(\w+)/
188end
189
190class FtextInlineMacro < CodeInlineMacroBase
191    named :ftext
192    match /ftext:([\w\*]+)/
193end
194
195class SnameInlineMacro < CodeInlineMacroBase
196    named :sname
197    match /sname:(\w+)/
198end
199
200class SlinkInlineMacro < LinkInlineMacroBase
201    named :slink
202    match /slink:(\w+)/
203
204    def exists? target
205        $apiNames.structs.has_key? target or $apiNames.handles.has_key? target
206    end
207end
208
209class StextInlineMacro < CodeInlineMacroBase
210    named :stext
211    match /stext:([\w\*]+)/
212end
213
214class EnameInlineMacro < CodeInlineMacroBase
215    named :ename
216    match /ename:(\w+)/
217
218    def exists? target
219        $apiNames.consts.has_key? target
220    end
221end
222
223class ElinkInlineMacro < LinkInlineMacroBase
224    named :elink
225    match /elink:(\w+)/
226
227    def exists? target
228        $apiNames.enums.has_key? target
229    end
230end
231
232class EtextInlineMacro < CodeInlineMacroBase
233    named :etext
234    match /etext:([\w\*]+)/
235end
236
237# this does not handle any [] at the moment
238
239class PnameInlineMacro < ParamInlineMacroBase
240    named :pname
241    match /pname:(\w+((\.|&#8594;)\w+)*)/
242end
243
244class PtextInlineMacro < ParamInlineMacroBase
245    named :ptext
246    match /ptext:([\w\*]+((\.|&#8594;)[\w\*]+)*)/
247end
248
249class DnameInlineMacro < CodeInlineMacroBase
250    named :dname
251    match /dname:(\w+)/
252end
253
254class DlinkInlineMacro < LinkInlineMacroBase
255    named :dlink
256    match /dlink:(\w+)/
257
258    def exists? target
259        $apiNames.defines.has_key? target
260    end
261end
262
263class TnameInlineMacro < CodeInlineMacroBase
264    named :tname
265    match /tname:(\w+)/
266end
267
268class TlinkInlineMacro < LinkInlineMacroBase
269    named :tlink
270    match /tlink:(\w+)/
271
272    def exists? target
273        $apiNames.flags.has_key? target or
274            $apiNames.funcpointers.has_key? target or
275            $apiNames.defines.has_key? target
276    end
277end
278
279class BasetypeInlineMacro < LinkInlineMacroBase
280    named :basetype
281    match /basetype:(\w+)/
282
283    def exists? target
284        $apiNames.basetypes.has_key? target
285    end
286end
287
288# This does not include the full range of code: use
289# It allows imbedded periods (field separators) and wildcards if followed by
290# another word, and an ending wildcard.
291
292class CodeInlineMacro < CodeInlineMacroBase
293    named :code
294    match /code:(\w+([.*]\w+)*\**)/
295end
296
297# The tag: and attr: macros are only used in registry.adoc
298
299class TagInlineMacro < StrongInlineMacroBase
300    named :tag
301    match /tag:(\w+)/
302end
303
304class AttrInlineMacro < StrongInlineMacroBase
305    named :attr
306    match /attr:(\w+)/
307end
308
309# Does nothing - just markup that we have considered the use case
310class UndefinedInlineMacro < SpecInlineMacroBase
311    named :undefined
312    match /undefined:/
313
314    def process parent, target, attributes
315        create_inline parent, :quoted, 'undefined'
316    end
317end
318