-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathconvert_listing.rb
102 lines (89 loc) · 2.82 KB
/
convert_listing.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
55
56
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
# frozen_string_literal: true
module DocbookCompat
##
# Methods code listings and their paired callout lists.
module ConvertListing
def convert_listing(node)
[
node.title ? '<p>' : nil,
node.id ? %(<a id="#{node.id}"></a>) : nil,
node.title ? convert_listing_title(node) : nil,
convert_listing_body(node),
].compact.join
end
def convert_inline_callout(node)
%(<a id="#{node.id}"></a><i class="conum" data-value="#{node.text}"></i>)
end
def convert_colist(node)
extra_classes = node.roles.empty? ? '' : " #{node.roles.join ' '}"
[
%(<div class="calloutlist#{extra_classes}">),
'<table border="0" summary="Callout list">',
node.items.each_with_index.map do |item, index|
convert_colist_item item, index
end,
'</table>',
'</div>',
].flatten.compact.join "\n"
end
private
def convert_listing_title(node)
title = '<strong>' + node.title
title += '.' unless [':', '.'].include? node.title[-1]
title += "</strong></p>\n"
title
end
def convert_listing_body(node)
if (lang = node.attr 'language')
convert_listing_body_with_language node, lang
else
%(<pre class="screen">#{node.content || ''}</pre>)
end
end
def convert_listing_body_with_language(node, lang)
extra_classes = node.roles.empty? ? '' : " #{node.roles.join ' '}"
pre_classes = "programlisting prettyprint lang-#{lang}#{extra_classes}"
c_cla = '"console_code_copy" title="Copy to clipboard" data-track="cta"'
span = '<span class="visually-hidden">Copy to clipboard</span>'
[
%(<div class="pre_wrapper lang-#{lang}#{extra_classes}">),
%(<div class=#{c_cla}>#{span}</div>),
%(<pre class="#{pre_classes}">#{node.content || ''}</pre>),
%(</div>),
].join "\n"
end
def convert_colist_item(item, index)
[
'<tr>',
convert_colist_item_head(item, index),
convert_colist_item_body(item),
'</tr>',
]
end
def convert_colist_item_head(item, index)
[
'<td align="left" valign="top" width="5%">',
"<p>#{convert_colist_item_coids item, index}</p>",
'</td>',
]
end
def convert_colist_item_body(item)
[
'<td align="left" valign="top">',
"<p>#{item.text}</p>",
item.blocks? ? item.content : nil,
'</td>',
]
end
def convert_colist_item_coids(item, index)
return '' unless (coids = item.attr 'coids')
coids = coids.split(' ')
return '' unless (first = coids.shift)
[
%(<a href="##{first}">),
%(<i class="conum" data-value="#{index + 1}"></i></a>),
coids.map { |coid| %(<a href="##{coid}"></a>) },
].compact.join
end
end
end