Class: Asciidoctor::DitaTopic

Inherits:
Converter::Base
  • Object
show all
Defined in:
lib/dita-topic.rb

Constant Summary collapse

NAME =
'dita-topic'

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DitaTopic

Returns a new instance of DitaTopic.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dita-topic.rb', line 34

def initialize *args
  super
  outfilesuffix '.dita'

  # Disable the author line by default:
  @authors_allowed = false

  # Enable processing of semantic markup by default:
  @semantic_allowed = true

  # Enable callouts by default:
  @callouts_allowed = true

  # Enable floating and block titles by default:
  @titles_allowed = true

  # Enable sidebars by default:
  @sidebars_allowed = true

  # Disable propagating the content type to sections by default:
  @type_allowed = false
end

Instance Method Details

#add_block_title(content, node) ⇒ Object

Helper methods



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
# File 'lib/dita-topic.rb', line 967

def add_block_title content, node
  # NOTE: Unlike AsciiDoc, DITA does not support titles assigned to
  # certain block elements. As a workaround, I decided to use a paragraph
  # with the outputclass attribute.

  # Check if the title is defined:
  return content unless node.title

  # Issue a warning if block titles are disabled:
  unless @titles_allowed
    logger.warn format_message "Block titles not supported in DITA"
    return content
  end

  # Return the XML output:
  <<~EOF.chomp
  <p outputclass="title"#{ node.role}><b>#{node.title}</b></p>
  #{content}
  EOF
end

#compose_author(author, node) ⇒ Object



988
989
990
991
992
# File 'lib/dita-topic.rb', line 988

def compose_author author, node
  name = node.sub_replacements author.name
  mail = %( &lt;#{node.sub_replacements author.email}&gt;) if author.email
  return %(#{name}#{mail})
end

#compose_circled_number(number) ⇒ Object



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/dita-topic.rb', line 1016

def compose_circled_number number
  # Verify the number is in a supported range:
  if number < 1 || number > 50
    logger.warn format_message "Callout number not in range between 1 and 50"
    return number
  end

  # Compose the XML entity:
  if number < 21
    %(&##{9311 + number};)
  elsif number < 36
    %(&##{12860 + number};)
  else
    %(&##{12941 + number};)
  end
end

#compose_floating_title(title) ⇒ Object



994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
# File 'lib/dita-topic.rb', line 994

def compose_floating_title title
  # NOTE: Unlike AsciiDoc, DITA does not support floating titles or
  # titles assigned to certain block elements. As a workaround, I decided
  # to use a paragraph with the outputclass attribute.

  # Check if the title is defined:
  return '' unless title

  # Issue a warning if floating titles are disabled:
  unless @titles_allowed
    logger.warn format_message "Floating titles not supported in DITA"
    return ''
  end

  # Return the XML output:
  %(<p outputclass="title"><b>#{title}</b></p>\n)
end

#compose_horizontal_dlist(node) ⇒ Object



900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/dita-topic.rb', line 900

def compose_horizontal_dlist node
  # Open the table:
  result = [%(<table outputclass="horizontal-dlist"#{compose_id node.id}#{ node.role}>)]

  # Check if the title is specified:
  result << %(<title>#{node.title}</title>) if node.title?

  # Define the table properties and open the tgroup:
  result << %(<tgroup cols="2">)
  result << %(<colspec colwidth="#{node.attr 'labelwidth', 15}*" />)
  result << %(<colspec colwidth="#{node.attr 'itemwidth', 85}*" />)
  result << %(<tbody>)

  # Process individual list items:
  node.items.each do |terms, description|
    # Compose the metadata attributes:
     = extract_attributes terms[0].text

    # Open the table row:
    result << %(<row#{}>)

    # Check the number of terms to process:
    if terms.count == 1
      result << %(<entry><b>#{terms[0].text}</b></entry>)
    else
      # Process individual terms:
      result << %(<entry>)
      terms.each do |item|
        result << %(<p><b>#{item.text}</b></p>)
      end
      result << %(</entry>)
    end

    # Check if the term description is specified:
    if description
      # Check if the description contains multiple block elements:
      if description.blocks?
        result << %(<entry>)
        result << %(<p>#{description.text}</p>) if description.text?
        result << description.content
        result << %(</entry>)
      else
        result << %(<entry>#{description.text}</entry>) if description.text?
      end
    end

    # Close the table row:
    result << %(</row>)
  end

  # Close the table:
  result << %(</tbody>)
  result << %(</tgroup>)
  result << %(</table>)

  # Return the XML output:
  result.join LF
end

#compose_id(id) ⇒ Object



1012
1013
1014
# File 'lib/dita-topic.rb', line 1012

def compose_id id
  id ? %( id="#{id}") : ''
end

#compose_metadata(roles) ⇒ Object



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'lib/dita-topic.rb', line 1045

def  roles
  # Check if the role is defined:
  return '' unless roles

  # Set the initial value:
  result = {}

  # Define supported metadata attributes:
  valid  = ['platform', 'product', 'audience', 'otherprops']

  # Process each role:
  roles.split.each do |role|
    # Ignore roles that do not follow the attribute:value format:
    next unless role.include? ':'

    # Separate the attribute name from its value:
    attribute, value = role.split ':'

    # Ignore unsupported attribute names:
    next unless valid.include? attribute

    # Append the value to the attribute:
    if result.key? attribute
      result[attribute] << %( #{value})
    else
      result[attribute] = %(#{value})
    end
  end

  # Return the list of metadata attributes otherwise:
  if result.empty?
    return ''
  else
    return ' ' + result.map { |k, v| %(#{k}="#{v}") unless v.empty? }.join(' ')
  end
end

#compose_qanda_dlist(node) ⇒ Object



866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/dita-topic.rb', line 866

def compose_qanda_dlist node
  # Open the ordered list:
  result = [%(<ol#{compose_id node.id}#{ node.role}>)]

  # Process individual list items:
  node.items.each do |terms, description|
    # Compose the metadata attributes:
     = extract_attributes terms[0].text

    # Open the list item:
    result << %(<li#{}>)

    # Process individual terms:
    terms.each do |item|
      result << %(<p outputclass="question"><i>#{item.text}</i></p>)
    end

    # Check if the term description is specified:
    if description
      result << %(<p>#{description.text}</p>)
      result << description.content if description.blocks?
    end

    # Close the list item:
    result << %(</li>)
  end

  # Close the ordered list:
  result << %(</ol>)

  # Return the XML output:
  add_block_title (result.join LF), node
end

#compose_type_outputclass(node) ⇒ Object



1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'lib/dita-topic.rb', line 1033

def compose_type_outputclass node
  # NOTE: _module-type and _content-type are deprecated, but still
  # present in some modules:
  outputclass = ''
  outputclass = %( outputclass="#{(node.attr '_module-type').downcase}") if node.attr? '_module-type'
  outputclass = %( outputclass="#{(node.attr '_content-type').downcase}") if node.attr? '_content-type'
  outputclass = %( outputclass="#{(node.attr '_mod-docs-content-type').downcase}") if node.attr? '_mod-docs-content-type'

  # Return the outputclass attribute:
  return outputclass
end

#convert_admonition(node) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dita-topic.rb', line 127

def convert_admonition node
  # NOTE: Unlike admonitions in AsciiDoc, the <note> element in DITA
  # cannot have its own <title>. Admonition titles are therefore not
  # preserved.

  # Issue a warning if the admonition has a title:
  if node.title?
    logger.warn format_message "Admonition titles not supported in DITA"
  end

  # Return the XML output:
  <<~EOF.chomp
  <note type="#{node.attr 'name'}"#{compose_id node.id}#{ node.role}>#{node.content}</note>
  EOF
end

#convert_audio(node) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dita-topic.rb', line 143

def convert_audio node
  # Check if the audio macro has a title specified:
  if node.title?
    <<~EOF.chomp
    <object data="#{node.media_uri(node.attr 'target')}"#{compose_id node.id}#{ node.role}>
      <desc>#{node.title}</desc>
    </object>
    EOF
  else
    %(<object data="#{node.media_uri(node.attr 'target')}"#{compose_id node.id}#{ node.role} />)
  end
end

#convert_colist(node) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/dita-topic.rb', line 156

def convert_colist node
  # Issue a warning if callouts are disabled:
  unless @callouts_allowed
    logger.warn format_message "Callouts not supported in DITA"
    return ''
  end

  # Reset the counter:
  number = 0

  # Open the definition list:
  result = [%(<dl outputclass="callout-list"#{compose_id node.id}#{ node.role}>)]

  # Process individual list items:
  node.items.each do |item|
    # Increment the counter:
    number += 1

    # Open the definition entry:
    result << %(<dlentry>)

    # Compose the callout number:
    result << %(<dt>#{compose_circled_number number}</dt>)

    # Check if description contains multiple block elements:
    if item.blocks
      result << %(<dd>)
      result << item.text
      result << item.content
      result << %(</dd>)
    else
      result << %(<entry>#{item.text}</entry>)
    end

    # Close the definition entry:
    result << %(</dlentry>)
  end

  # Close the definition list:
  result << %(</dl>)

  # Return the XML output:
  result.join LF
end

#convert_dlist(node) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/dita-topic.rb', line 201

def convert_dlist node
  # Check if a different list style is set:
  return compose_horizontal_dlist node if node.style == 'horizontal'
  return compose_qanda_dlist node if node.style == 'qanda'

  # Open the definition list:
  result = [%(<dl#{compose_id node.id}#{ node.role}>)]

  # Process individual list items:
  node.items.each do |terms, description|
    # Compose the metadata attributes:
     = extract_attributes terms[0].text

    # Open the definition entry:
    result << %(<dlentry#{}>)

    # Process individual terms:
    terms.each do |item|
      result << %(<dt>#{item.text}</dt>)
    end

    # Check if the term description is specified:
    if description
      # Check if the description contains multiple block elements:
      if description.blocks?
        result << %(<dd>)
        result << %(<p>#{description.text}</p>) if description.text?
        result << description.content
        result << %(</dd>)
      else
        result << %(<dd>#{description.text}</dd>)
      end
    end

    # Close the definition entry:
    result << %(</dlentry>)
  end

  # Close the definition list:
  result << %(</dl>)

  # Return the XML output:
  add_block_title (result.join LF), node
end

#convert_document(node) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dita-topic.rb', line 57

def convert_document node
  # Check if the author line is enabled:
  @authors_allowed = true if (node.attr 'dita-topic-authors') == 'on'

  # Check if semantic markup is enabled:
  @semantic_allowed = false if (node.attr 'dita-topic-semantic') == 'off'

  # Check if callouts are enabled:
  @callouts_allowed = false if (node.attr 'dita-topic-callouts') == 'off'

  # Check if floating and block titles are enabled:
  @titles_allowed = false if (node.attr 'dita-topic-titles') == 'off'

  # Check if sidebars are enabled:
  @sidebars_allowed = false if (node.attr 'dita-topic-sidebars') == 'off'

  # Check if propagating the content type to sections is enabled:
  @type_allowed = true if (node.attr 'dita-topic-type') == 'on'

  # Check if the file name is available (it is not for standard input):
  if node.attr? 'docname'
    file_name   = node.attr 'docname'
    file_suffix = (node.attr? 'docfilesuffix') ? (node.attr 'docfilesuffix') : ''
    @file_name  = "#{file_name}#{file_suffix}"
  end

  # Check if the modular documentation content type is specified:
  outputclass = compose_type_outputclass node

  # Open the document:
  result = ["<?xml version='1.0' encoding='utf-8' ?>"]
  result << %(<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">)
  result << %(<topic#{compose_id (node.id or node.attributes['docname'] or 'topic-'+SecureRandom.uuid)}#{outputclass}>)
  result << %(<title>#{node.doctitle}</title>)

  # Check if the author line is enabled and defined:
  if @authors_allowed && !node.authors.empty?
    # Open the prolog:
    result << %(<prolog>)

    # Process individual author names:
    node.authors.each do |author|
      result << %(<author>#{compose_author author, node}</author>)
    end

    # Close the prolog:
    result << %(</prolog>)
  end

  # Open the document body:
  result << %(<body>)

  # Check if the author line defined while disabled:
  if !@authors_allowed && !node.authors.empty?
    # Issue a warning as inline content is not going to be processed:
    logger.warn format_message "Author lines not enabled for topics"

    # Process the author line as a plain paragraph:
    result << %(<p>#{node.authors.map {|author| compose_author author, node}.join('; ')}</p>)
  end

  # Close the document body:
  result << node.content
  result << %(</body>)
  result << %(</topic>)

  # Return the XML output:
  result.join LF
end

#convert_example(node) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/dita-topic.rb', line 246

def convert_example node
  # Issue a warning if the example is nested:
  if (parent = node.parent.context) != :document && parent != :preamble
    logger.warn format_message "Examples not supported within #{parent} in DITA"
  end

  # Return the XML output:
  <<~EOF.chomp
  <example#{compose_id node.id}#{ node.role}>
  #{node.title ? %(<title>#{node.title}</title>\n) : ''}#{node.content}
  </example>
  EOF
end

#convert_floating_title(node) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/dita-topic.rb', line 260

def convert_floating_title node
  # NOTE: Unlike AsciiDoc, DITA does not have a dedicated element for
  # floating titles. As a workaround, I decided to use a paragraph with
  # the outputclass attribute.

  # Issue a warning if floating titles are disabled:
  unless @titles_allowed
    logger.warn format_message "Floating titles not supported in DITA"
    return ''
  end

  # Return the XML output:
  %(<p outputclass="title sect#{node.level}"#{compose_id node.id}#{ node.role}><b>#{node.title}</b></p>)
end

#convert_image(node) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/dita-topic.rb', line 275

def convert_image node
  # Check if additional attributes are specified:
  width  = (node.attr? 'width') ? %( width="#{node.attr 'width'}") : ''
  height = (node.attr? 'height') ? %( height="#{node.attr 'height'}") : ''
  scale  = (node.attr? 'scale') ? %( scale="#{(node.attr 'scale').tr('%', '')}") : ''

  # Exclude width and height attributes with percentage values:
  width   = '' if width.include? '%'
  height  = '' if height.include? '%'

  # Check if the image has a title specified:
  if node.title?
    <<~EOF.chomp
    <fig#{compose_id node.id}#{ node.role}>
    <title>#{node.title}</title>
    <image href="#{node.image_uri(node.attr 'target')}"#{width}#{height}#{scale} placement="break">
    <alt>#{node.alt}</alt>
    </image>
    </fig>
    EOF
  else
    <<~EOF.chomp
    <image href="#{node.image_uri(node.attr 'target')}"#{width}#{height}#{scale} placement="break"#{compose_id node.id}#{ node.role}>
    <alt>#{node.alt}</alt>
    </image>
    EOF
  end
end

#convert_inline_anchor(node) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/dita-topic.rb', line 304

def convert_inline_anchor node
  # Determine the type of the anchor:
  case node.type
  when :link
    # Compose an external link:
    %(<xref href="#{node.target}" format="html" scope="external"#{compose_id node.id}#{ node.role}>#{node.text}</xref>)
  when :xref
    # NOTE: While AsciiDoc is happy to reference an ID that is not
    # defined in the same AsciiDoc file, DITA requires the topic ID as
    # part of the reference. As this script does not have direct access
    # to the topic IDs of external files and to avoid performance issues
    # I do not want to process them from this script, I choose to issue a
    # warning so that the user can resolve the problem.

    # Determine whether the cross reference links to a file path:
    if (path = node.attributes['path'])
      # Issue a warning if the cross reference includes an ID:
      logger.warn format_message "Possible invalid reference: #{node.target}" if node.target.include? '#'

      # Compose a cross reference:
      return %(<xref href="#{node.target}"#{ node.role}>#{node.text || path}</xref>)
    end

    # Determine whether the ID reference target is in this document:
    if node.document.catalog[:refs].key? (target = node.target.delete_prefix '#')
      # Determine whether the ID reference target is the document id:
      if target == node.document.id
        # Compose the unchanged cross reference:
        return node.text ? %(<xref href="##{target}"#{ node.role}>#{node.text}</xref>) : %(<xref href="##{target}" />)
      end

      # Compose the adjusted cross reference:
      return node.text ? %(<xref href="#./#{target}"#{ node.role}>#{node.text}</xref>) : %(<xref href="#./#{target}" />)
    end

    # Issue a warning as the cross reference is unlikely to work:
    logger.warn format_message "Possible invalid reference: #{node.target}"

    # Compose the cross reference:
    node.text ? %(<xref href="#{node.target}"#{ node.role}>#{node.text}</xref>) : %(<xref href="#{node.target}" />)
  when :ref
    # NOTE: DITA does not have a dedicated element for inline anchors or
    # a direct equivalent of the <span> element from HTML. The solution
    # below is the least invasive way I could find to achieve the
    # equivalent behavior.

    # Compose an inline anchor:
    %(<i id="#{node.id}" />)
  when :bibref
    # NOTE: DITA does not have a dedicated element for inline anchors or
    # a direct equivalent of the <span> element from HTML. The solution
    # below is the least invasive way I could find to achieve the
    # equivalent behavior.

    # Compose a bibliographic reference:
    %(<i id="#{node.id}" />[#{node.reftext || node.id}])
  else
    # Issue a warning if an unknown anchor type is present:
    logger.warn format_message "Unknown anchor type: #{node.type}"
    ''
  end
end

#convert_inline_break(node) ⇒ Object



367
368
369
370
371
372
373
374
375
# File 'lib/dita-topic.rb', line 367

def convert_inline_break node
  # NOTE: Unlike AsciiDoc, DITA does not support inline line breaks.

  # Issue a warning if an inline line break is present:
  logger.warn format_message "Inline breaks not supported in DITA"

  # Return the XML output:
  %(#{node.text}<!-- break -->)
end

#convert_inline_button(node) ⇒ Object



377
378
379
# File 'lib/dita-topic.rb', line 377

def convert_inline_button node
  %(<uicontrol outputclass="button">#{node.text}</uicontrol>)
end

#convert_inline_callout(node) ⇒ Object



381
382
383
384
385
386
387
388
389
390
# File 'lib/dita-topic.rb', line 381

def convert_inline_callout node
  # Issue a warning if callouts are disabled:
  unless @callouts_allowed
    logger.warn format_message "Callouts not supported in DITA"
    return ''
  end

  # Return the XML entity:
  %(<b outputclass="callout">#{compose_circled_number node.text.to_i}</b>)
end

#convert_inline_footnote(node) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/dita-topic.rb', line 392

def convert_inline_footnote node
  # Check whether the footnote is a definition or a reference:
  if node.type == :xref
    %(<xref href="#./#{node.target}" type="fn" />)
  elsif node.id
    # Define the footnote and immediately reference it to match AsdciiDoc
    # behavior:
    %(<fn#{compose_id node.id}>#{node.text}</fn><xref href="#./#{node.id}" type="fn" />)
  else
    %(<fn>#{node.text}</fn>)
  end
end

#convert_inline_image(node) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/dita-topic.rb', line 405

def convert_inline_image node
  # Check if additional attributes are specified:
  width  = (node.attr? 'width') ? %( width="#{node.attr 'width'}") : ''
  height = (node.attr? 'height') ? %( height="#{node.attr 'height'}") : ''

  # Exclude width and height attributes with percentage values:
  width   = '' if width.include? '%'
  height  = '' if height.include? '%'

  # Return the XML output:
  %(<image href="#{node.image_uri node.target}"#{width}#{height} placement="inline"#{ node.role}><alt>#{node.alt}</alt></image>)
end

#convert_inline_indexterm(node) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/dita-topic.rb', line 418

def convert_inline_indexterm node
  # Check if the index term appears in the flow of the text:
  if node.type == :visible
    return %(<indexterm>#{node.text}</indexterm>#{node.text})
  end

  # Get primary, secondary, and tertiary index terms:
  terms = node.attr 'terms'

  # Determine the number of terms:
  case terms.size
  when 1
    %(<indexterm>#{terms[0]}</indexterm>)
  when 2
    %(<indexterm>#{terms[0]}<indexterm>#{terms[1]}</indexterm></indexterm>)
  else
    %(<indexterm>#{terms[0]}<indexterm>#{terms[1]}<indexterm>#{terms[2]}</indexterm></indexterm></indexterm>)
  end
end

#convert_inline_kbd(node) ⇒ Object



438
439
440
441
442
443
444
445
# File 'lib/dita-topic.rb', line 438

def convert_inline_kbd node
  # Check if there is more than one key:
  if (keys = node.attr 'keys').size == 1
    %(<uicontrol outputclass="key">#{keys[0]}</uicontrol>)
  else
    %(<uicontrol outputclass="key">#{keys.join '</uicontrol>+<uicontrol outputclass="key">'}</uicontrol>)
  end
end

#convert_inline_menu(node) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/dita-topic.rb', line 447

def convert_inline_menu node
  # Compose the markup for the menu:
  menu = %(<uicontrol>#{node.attr 'menu'}</uicontrol>)

  # Compose the markup for possible submenus:
  submenus = (not (node.attr 'submenus').empty?) ? %(<uicontrol>#{(node.attr 'submenus').join '</uicontrol><uicontrol>'}</uicontrol>) : ''

  # Compose the markup for the menu item:
  menuitem = (node.attr 'menuitem') ? %(<uicontrol>#{node.attr 'menuitem'}</uicontrol>) : ''

  # Return the XML output:
  %(<menucascade>#{menu}#{submenus}#{menuitem}</menucascade>)
end

#convert_inline_quoted(node) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/dita-topic.rb', line 461

def convert_inline_quoted node
  # Determine the inline markup type:
  case node.type
  when :emphasis
    %(<i#{compose_id node.id}#{ node.role}>#{node.text}</i>)
  when :strong
    %(<b#{compose_id node.id}#{ node.role}>#{node.text}</b>)
  when :monospaced
    # Set the default element value:
    element = 'codeph'

    # Check if semantic markup is enabled and the role is provided:
    if @semantic_allowed and node.role
      # Define supported roles:
      semantic_markup = {
        'command'   => 'cmdname',
        'directory' => 'filepath',
        'filename'  => 'filepath',
        'option'    => 'option',
        'variable'  => 'varname'
      }

      # Process each role:
      node.role.split.each do |role|
        # Select the appropriate semantic element:
        element = semantic_markup[role] if semantic_markup.key? role
      end
    end

    # Return the result:
    %(<#{element}#{compose_id node.id}#{ node.role}>#{node.text}</#{element}>)
  when :superscript
    %(<sup#{compose_id node.id}#{ node.role}>#{node.text}</sup>)
  when :subscript
    %(<sub#{compose_id node.id}#{ node.role}>#{node.text}</sub>)
  when :double
    %(&#8220;#{node.text}&#8221;)
  when :single
    %(&#8216;#{node.text}&#8217;)
  when :asciimath
    # Issue a warning if a STEM content is present:
    logger.warn format_message "STEM support not implemented"

    # Add comments around the STEM content:
    %(<!-- asciimath start -->#{node.text}<!-- asciimath end -->)
  when :latexmath
    # Issue a warning if a STEM content is present:
    logger.warn format_message "STEM support not implemented"

    # Add comments around the STEM content:
    %(<!-- latexmath start -->#{node.text}<!-- latexmath end -->)
  else
    %(<ph#{compose_id node.id}#{ node.role}>#{node.text}</ph>)
  end
end

#convert_listing(node) ⇒ Object



517
518
519
520
521
522
523
524
525
526
# File 'lib/dita-topic.rb', line 517

def convert_listing node
  # Check whether the source language is defined:
  language = (node.attributes.key? 'language') ? %( outputclass="language-#{node.attributes['language']}") : ''

  # Compose the XML output:
  result = %(<codeblock#{language}#{compose_id node.id}#{ node.role}>#{node.content}</codeblock>)

  # Return the XML output:
  add_block_title result, node
end

#convert_literal(node) ⇒ Object



528
529
530
531
532
533
534
# File 'lib/dita-topic.rb', line 528

def convert_literal node
  # Compose the XML output:
  result = %(<pre#{compose_id node.id}#{ node.role}>#{node.content}</pre>)

  # Return the XML output:
  add_block_title result, node
end

#convert_olist(node) ⇒ Object



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/dita-topic.rb', line 536

def convert_olist node
  # Open the ordered list:
  result = [%(<ol#{compose_id node.id}#{ node.role}>)]

  # Process individual list items:
  node.items.each do |item|
    # Compose the metadata attributes:
     = extract_attributes item.text
     =  item.role if item.role

    # Check if the list item contains multiple block elements:
    if item.blocks?
      result << %(<li#{compose_id item.id}#{}>#{item.text})
      result << item.content
      result << %(</li>)
    else
      result << %(<li#{compose_id item.id}#{}>#{item.text}</li>)
    end
  end

  # Close the ordered list:
  result << '</ol>'

  # Return the XML output:
  add_block_title (result.join LF), node
end

#convert_open(node) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/dita-topic.rb', line 563

def convert_open node
  # NOTE: Although DITA provides an <abstract> element that is intended
  # for this purpose, it is placed alongside the <body> element and not
  # inside of ot. As there is no clean way to place it there, I use
  # a workaround.

  # Determine the node type:
  if node.style == 'partintro'
    node.content
  elsif node.content_model == :compound
    <<~EOF.chomp
    <div#{(node.style == 'abstract') ? ' outputclass="abstract"' : ''}#{compose_id node.id}#{ node.role}>
    #{compose_floating_title node.title}#{node.content}
    </div>
    EOF
  else
    %(#{compose_floating_title node.title}<p#{(node.style == 'abstract') ? ' outputclass="abstract"' : ''}#{compose_id node.id}#{ node.role}>#{node.content}</p>)
  end
end

#convert_page_break(node) ⇒ Object



583
584
585
586
587
588
589
590
591
# File 'lib/dita-topic.rb', line 583

def convert_page_break node
  # NOTE: Unlike AsciiDoc, DITA does not support page breaks.

  # Issue a warning if a page break is present:
  logger.warn format_message "Page breaks not supported in DITA"

  # Return the XML output:
  %(<p outputclass="page-break"></p>)
end

#convert_paragraph(node) ⇒ Object



593
594
595
596
597
598
599
# File 'lib/dita-topic.rb', line 593

def convert_paragraph node
  if (node.attr 'role') and (node.attr 'role').split.include? '_abstract'
    add_block_title %(<p outputclass="abstract"#{compose_id node.id}#{ node.role}>#{node.content}</p>), node
  else
    add_block_title %(<p#{compose_id node.id}#{ node.role}>#{node.content}</p>), node
  end
end

#convert_preamble(node) ⇒ Object



601
602
603
# File 'lib/dita-topic.rb', line 601

def convert_preamble node
  node.content
end

#convert_quote(node) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/dita-topic.rb', line 605

def convert_quote node
  # Check if the author is defined:
  author = (node.attr? 'attribution') ? %(\n<p>&#8212; #{node.attr 'attribution'}</p>) : ''

  # Check if the citation source is defined:
  source = (node.attr? 'citetitle') ? %(\n<cite>#{node.attr 'citetitle'}</cite>) : ''

  # Check if the content contains multiple block elements:
  if node.content_model == :compound
    <<~EOF.chomp
    <lq#{compose_id node.id}#{ node.role}>
    #{compose_floating_title node.title}#{node.content}#{author}#{source}
    </lq>
    EOF
  else
    <<~EOF.chomp
    <lq#{compose_id node.id}#{ node.role}>
    #{compose_floating_title node.title}<p>#{node.content}</p>#{author}#{source}
    </lq>
    EOF
  end
end

#convert_section(node) ⇒ Object



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/dita-topic.rb', line 628

def convert_section node
  # NOTE: Unlike sections in AsciiDoc, the <section> element in DITA
  # cannot be nested. Consequently, converting AsciiDoc files that do
  # contain nested subsections will result in invalid markup.
  #
  # I explored the possibility to use the <topic> element instead as it
  # can be nested, but that presents a problem with closing the <body>
  # element of the parent <topic>. As only a very small number of
  # AsciiDoc modules contain nested subsections, I chose the simple
  # markup.

  # Issue a warning if there are nested sections:
  logger.warn format_message "Nesting of sections not supported in DITA" if node.level > 1

  # Check if the modular documentation content type is specified:
  outputclass = @type_allowed ? compose_type_outputclass(node.document) : ''

  # Return the XML output:
  <<~EOF.chomp
  <section#{compose_id node.id}#{outputclass}#{ node.role}>
  <title>#{node.title}</title>
  #{node.content}
  </section>
  EOF
end

#convert_sidebar(node) ⇒ Object



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/dita-topic.rb', line 654

def convert_sidebar node
  # NOTE: Unlike AsciiDoc, DITA does not provide markup for a sidebar. As
  # a workaround, I decided to use a div with the outputclass attribute.

  # Issue a warning if sidebars are disabled:
  unless @sidebars_allowed
    logger.warn format_message "Sidebars not supported in DITA"
    return ''
  end

  # Check if the content contains multiple block elements:
  if node.content_model == :compound
    <<~EOF.chomp
    <div outputclass="sidebar"#{compose_id node.id}#{ node.role}>
    #{compose_floating_title node.title}#{node.content}
    </div>
    EOF
  else
    <<~EOF.chomp
    <div outputclass="sidebar"#{compose_id node.id}#{ node.role}>
    #{compose_floating_title node.title}<p>#{node.content}</p>
    </div>
    EOF
  end
end

#convert_stem(node) ⇒ Object



680
681
682
683
684
# File 'lib/dita-topic.rb', line 680

def convert_stem node
  # Issue a warning if a STEM content is present:
  logger.warn format_message "STEM support not implemented"
  return ''
end

#convert_table(node) ⇒ Object



686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/dita-topic.rb', line 686

def convert_table node
  # Open the table:
  result = [%(<table#{compose_id node.id}#{ node.role}>)]

  # Check if the title is specified:
  result << %(<title>#{node.title}</title>) if node.title?

  # Define the table properties and open the tgroup:
  result << %(<tgroup cols="#{node.attr 'colcount'}">)

  # Define column properties:
  node.columns.each do |column|
    result << %(<colspec colname="col_#{column.attr 'colnumber'}" colwidth="#{column.attr ((node.attr? 'width') ? 'colabswidth' : 'colpcwidth')}*"/>)
  end

  # Process each table section (header, body, and footer):
  node.rows.to_h.each do |type, rows|
    # Skip empty sections:
    next if rows.empty?

    # Issue a warning if a table footer is present:
    if type == :foot
      logger.warn format_message "Table footers not supported in DITA"
      next
    end

    # Open the section:
    result << %(<t#{type}>)

    # Process each row:
    rows.each do |row|
      # Compose the metadata attributes:
       = extract_attributes row[0].text

      # Open the row:
      result << %(<row#{}>)

      # Process each cell:
      row.each do |cell|
        # Check if the cell spans multiple columns:
        colspan = cell.colspan ? %( namest="col_#{colnum = cell.column.attr 'colnumber'}" nameend="col_#{colnum + cell.colspan - 1}") : ''

        # Check if the cell spans multiple rows:
        rowspan = cell.rowspan ? %( morerows="#{cell.rowspan - 1}") : ''

        # Compose the entry tag:
         = %(entry#{colspan}#{rowspan})

        # Determine the formatting of the entry:
        if type == :head
          result << %(<#{}>#{cell.text}</entry>)
          next
        end
        case cell.style
        when :asciidoc
          result << %(<#{}>#{cell.content}</entry>)
        when :literal
          result << %(<#{}><pre>#{cell.text}</pre></entry>)
        else
          result << %(<#{}>)
          cell.content.each do |line|
            result << %(<p>#{line}</p>)
          end
          result << %(</entry>)
        end
      end

      # Close the row:
      result << %(</row>)
    end

    # Close the section:
    result << %(</t#{type}>)
  end

  # Close the table:
  result << %(</tgroup>)
  result << %(</table>)

  # Return the XML output:
  result.join LF
end

#convert_thematic_break(node) ⇒ Object



769
770
771
772
773
774
775
776
777
# File 'lib/dita-topic.rb', line 769

def convert_thematic_break node
  # NOTE: Unlike AsciiDoc, DITA does not support thematic breaks.

  # Issue a warning if a thematic break is present:
  logger.warn format_message "Thematic breaks not supported in DITA"

  # Return the XML output:
  %(<p outputclass="thematic-break"></p>)
end

#convert_ulist(node) ⇒ Object



779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'lib/dita-topic.rb', line 779

def convert_ulist node
  # Open the unordered list:
  result = [%(<ul#{compose_id node.id}#{ node.role}>)]

  # Process individual list items:
  node.items.each do |item|
    # Compose the metadata attributes:
     = extract_attributes item.text
     =  item.role if item.role

    # Check if the list item is part of a checklist:
    unless item.attr? 'checkbox'
      check_box = ''
    else
      check_box = (item.attr? 'checked') ? '&#10003; ' : '&#10063; '
    end

    # Check if the list item contains multiple block elements:
    if item.blocks?
      result << %(<li#{compose_id item.id}#{}>#{check_box}#{item.text})
      result << item.content
      result << %(</li>)
    else
      result << %(<li#{compose_id item.id}#{}>#{check_box}#{item.text}</li>)
    end
  end

  # Close the unordered list:
  result << '</ul>'

  # Returned the XML output:
  add_block_title (result.join LF), node
end

#convert_verse(node) ⇒ Object



813
814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/dita-topic.rb', line 813

def convert_verse node
  # Check if the author is defined:
  author = (node.attr? 'attribution') ? %(\n&#8212; #{node.attr 'attribution'}) : ''

  # Check if the citation source is defined:
  source = (node.attr? 'citetitle') ? %(\n<cite>#{node.attr 'citetitle'}</cite>) : ''

  # Return the XML output:
  <<~EOF.chomp
  <lines#{compose_id node.id}#{ node.role}>
  #{node.content}#{author}#{source}
  </lines>
  EOF
end

#convert_video(node) ⇒ Object



828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/dita-topic.rb', line 828

def convert_video node
  # Check if additional attributes are specified:
  width  = (node.attr? 'width') ? %( width="#{node.attr 'width'}") : ''
  height = (node.attr? 'height') ? %( height="#{node.attr 'height'}") : ''

  # Check if the video is a Vimeo or YouTube video:
  if (provider = (node.attr 'poster')) == 'youtube'
    # Separate a playlist from the target:
    target, list = (node.attr 'target').split '/', 2

    # Check if the playlist is provided as an attribute:
    if node.attr 'list' and not list
      list = node.attr 'list'
    end

    # Compose the playlist URL fragment:
    list = list ? %(?list=#{list}) : ''

    # Compose the target URL:
    target_url = %(https://www.youtube.com/embed/#{target}#{list})
  elsif provider == 'vimeo'
    target_url = %(https://player.vimeo.com/video/#{node.attr 'target'})
  else
    target_url = node.media_uri(node.attr 'target')
  end

  # Check if the audio macro has a title specified:
  if node.title?
    <<~EOF.chomp
    <object data="#{target_url}"#{width}#{height}#{compose_id node.id}#{ node.role}>
      <desc>#{node.title}</desc>
    </object>
    EOF
  else
    %(<object data="#{target_url}"#{width}#{height}#{compose_id node.id}#{ node.role} />)
  end
end

#extract_attributes(text) ⇒ Object



1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/dita-topic.rb', line 1082

def extract_attributes text
  # Extract metadata attributes from an empty ph element:
  if /^\s*<ph(?<attributes> [^>]+)><\/ph>\s*/ =~ text
    return attributes
  else
    return ''
  end
end

#format_message(message) ⇒ Object



1091
1092
1093
1094
1095
# File 'lib/dita-topic.rb', line 1091

def format_message message
  # Compose the warning or error message:
  file_name = (defined? @file_name) ? %( #{@file_name}:) : ''
  %(#{NAME}:#{file_name} #{message})
end