-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtech_docs_html_renderer.rb
144 lines (126 loc) · 4.43 KB
/
tech_docs_html_renderer.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require "middleman-core/renderers/redcarpet"
require "tmpdir"
require "timeout"
module GovukTechDocs
class TechDocsHTMLRenderer < Middleman::Renderers::MiddlemanRedcarpetHTML
include Redcarpet::Render::SmartyPants
def initialize(options = {})
@local_options = options.dup
@app = @local_options[:context].app
super
end
def paragraph(text)
@app.api("<p>#{text.strip}</p>\n")
end
def header(text, level)
anchor = UniqueIdentifierGenerator.instance.create(text, level)
%(<h#{level} id="#{anchor}">#{text}</h#{level}>\n)
end
def image(link, title, alt_text)
if alt_text && !alt_text.strip.empty?
%(<a href="#{link}" rel="noopener noreferrer">#{super}</a>)
else
super
end
end
def table(header, body)
%(<div class="table-container">
<table>
#{header}#{body}
</table>
</div>)
end
def table_row(body)
# Post-processing the table_cell HTML to implement row headings.
#
# Doing this in table_row instead of table_cell is a hack.
#
# Ideally, we'd use the table_cell callback like:
#
# def table_cell(content, alignment, header)
# if header
# "<th>#{content}</th>"
# elsif content.start_with? "# "
# "<th scope="row">#{content.sub(/^# /, "")}</th>"
# else
# "<td>#{content}</td>"
# end
# end
#
# Sadly, Redcarpet's table_cell callback doesn't allow you to distinguish
# table cells and table headings until https://github.com/vmg/redcarpet/commit/27dfb2a738a23aadd286ac9e7ecd61c4545d29de
# (which is not yet released). This means we can't use the table_cell callback
# without breaking column headers, so we're having to hack it in table_row.
fragment = Nokogiri::HTML::DocumentFragment.parse(body)
fragment.children.each do |cell|
next unless cell.name == "td"
next if cell.children.empty?
first_child = cell.children.first
next unless first_child.text?
leading_text = first_child.content
next unless leading_text.start_with?("#")
cell.name = "th"
cell["scope"] = "row"
first_child.content = leading_text.sub(/# */, "")
end
tr = Nokogiri::XML::Node.new "tr", fragment.document
tr.children = fragment.children
tr.to_html
end
def block_code(text, lang)
if lang == "mermaid"
block_diagram(text)
elsif defined?(super)
# Post-processing the block_code HTML to implement tabbable code blocks.
#
# Middleman monkey patches the Middleman::Renderers::MiddlemanRedcarpetHTML
# to include Middleman::Syntax::RedcarpetCodeRenderer. This defines its own
# version of `block_code(text, lang)` which we can call with `super`.
fragment = Nokogiri::HTML::DocumentFragment.parse(super)
fragment.traverse do |element|
if element.name == "pre" && element["tabindex"].nil?
element["tabindex"] = "0"
end
end
fragment.to_html
else
# If syntax highlighting with redcarpet isn't enabled, super will not
# be `defined?`, so we can jump straight to rendering HTML ourselves.
fragment = Nokogiri::HTML::DocumentFragment.parse("")
pre = Nokogiri::XML::Node.new "pre", fragment.document
pre["tabindex"] = "0"
code = Nokogiri::XML::Node.new "code", fragment.document
code["class"] = lang
code.content = text
pre.add_child code
pre.to_html
end
end
def block_diagram(code)
mmdc = "#{__dir__}/../../node_modules/.bin/mmdc"
Dir.mktmpdir do |tmp|
input_path = "#{tmp}/input"
output_path = "#{tmp}/output.svg"
File.open(input_path, "w") { |f| f.write(code) }
ok = exec_with_timeout("#{mmdc} -i #{input_path} -o #{output_path} --theme neutral", 10)
if ok && File.exist?(output_path)
File.read(output_path)
else
%({<pre tabindex="0">#{code}</pre>)
end
end
end
def exec_with_timeout(cmd, timeout)
pid = Process.spawn(cmd, { %i[err out] => :close, :pgroup => true })
begin
Timeout.timeout(timeout) do
Process.waitpid(pid, 0)
$CHILD_STATUS.exitstatus.zero?
end
rescue Timeout::Error
Process.kill(15, -Process.getpgid(pid))
false
end
end
end
end