-
Notifications
You must be signed in to change notification settings - Fork 13
/
documenter.rb
201 lines (188 loc) · 4.62 KB
/
documenter.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
145
146
147
148
149
150
151
152
153
154
155
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
200
201
require 'redcarpet'
require 'net/http'
require 'uri'
require 'net/https'
ChromeIndex = []
def writeList(file)
@documentIndex.each do |name, path|
file.puts "\t<a href=\"#{path}\" target=\"_blank\">#{name}</a><br />"
end
end
def writeDocumentationIndex
# download chrome docs, all at the same time.
cd 'gen-doc'
open('chrome.urls', 'wb') do |file|
ChromeIndex.each do |url|
file.puts url
end
end
sh('wget --no-check-certificate -p -k -i chrome.urls')
#rm('chrome.urls')
ChromeIndex.each do |url|
uri = URI(url)
base = uri.host+uri.path
html = base+'.html'
#rm_f(html)
#mv(base, html)
end
cd '..'
# generate index.
open('gen-doc/index.html', 'wb') do |file|
file.puts '<html>'
file.puts '<head>'
file.puts "\t<title>Evothings Client plugin reference index</title>"
file.puts '</head>'
file.puts '<body>'
writeList(file)
file.puts '</body>'
file.puts '</html>'
end
# generate embeddable index.
open('gen-doc/index.html.embed', 'wb') do |file|
writeList(file)
end
end
class JdocDocumenter
def initialize(jsFilename)
@jsFilename = jsFilename
end
@@versionOk = false
def self.checkVersion
return if(@@versionOk)
open('|jsdoc -v') do |stream|
ver = stream.read.strip.split(' ')[1]
req = '3.2.2'
if(ver == req)
@@versionOk = true
return
end
if(!ver || ver == '')
puts "jsdoc not found. version #{req} is required."
else
puts "jsdoc version #{req} is required. installed version: #{ver}"
end
raise "jsdoc version error"
end
end
def run(name, location)
#JdocDocumenter.checkVersion
cwd = pwd
cd location
sh "jsdoc -l -c conf.json #{@jsFilename}"
cd cwd
dst = "gen-doc/#{name}"
rm_rf(dst)
mkdir_p(File.dirname(dst))
mv("#{location}/out", dst)
return name+'/index.html'
end
end
def downloadTo(srcUri, dstPath, previousLocations = [], remainingLimit = 5)
if(File.exist?(dstPath))
puts "Skipped '#{dstPath}'"
return
end
puts "Downloading #{srcUri} to '#{dstPath}'..."
if(srcUri.scheme == 'https')
http = Net::HTTP.new(srcUri.host, srcUri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = http.get(srcUri.path)
else
res = Net::HTTP.get_response(srcUri)
end
if(res.is_a?(Net::HTTPRedirection))
loc = res['location']
if(previousLocations.include?(loc) || remainingLimit <= 0)
puts "Redirect loop detected, aborting!"
raise "redirect loop"
end
puts "Redirect #{res.code} to #{loc}"
previousLocations << loc
return downloadTo(URI(loc), dstPath, previousLocations, remainingLimit - 1)
end
if(!res.is_a?(Net::HTTPSuccess))
throw "Failed to download file!"
end
open(dstPath, 'wb') do |file|
file.write res.body
end
end
class ChromeDocumenter
def initialize(docUrl)
@docUrl = docUrl
end
def run(name, location)
uri = URI(@docUrl)
dstFilename = "#{uri.host}#{uri.path}"#.html"
ChromeIndex << @docUrl
# the stuff will be downloaded later, in writeDocumentationIndex.
return dstFilename
end
end
class RenderMarkdownWithDownloadedImages < Redcarpet::Render::HTML
def self.setDstDir(dstDir)
@@dstDir = dstDir
end
def self.setSrcDir(srcDir)
@@srcDir = srcDir
end
def image(link, title, alt_text)
uri = URI(link)
filename = File.basename(uri.path)
dst = "#{@@dstDir}/#{filename}"
if(uri.scheme && uri.host)
downloadTo(uri, dst)
else
FileUtils::Verbose.cp("#{@@srcDir}/#{link}", dst)
end
# html spec demands that 'alt' be present.
alt_text = filename if(!alt_text || alt_text.length == 0)
# title is optional.
titleTag = ''
titleTag = " title=\"#{title}\"" if(title && title.length > 0)
return "<img src=\"#{filename}\"#{titleTag} alt=\"#{alt_text}\" />"
end
end
class MarkdownDocumenter
def initialize(mdFilename)
@mdFilename = mdFilename
end
def run(name, location)
dstDir = "gen-doc/#{name}"
mkdir_p(dstDir)
dst = "#{dstDir}/index.html"
RenderMarkdownWithDownloadedImages.setDstDir(dstDir)
RenderMarkdownWithDownloadedImages.setSrcDir(location)
markdown = Redcarpet::Markdown.new(RenderMarkdownWithDownloadedImages,
no_intra_emphasis: true,
tables: true,
fenced_code_blocks: true,
autolink: true,
strikethrough: true,
lax_spacing: true,
space_after_headers: true,
superscript: true,
underline: true,
highlight: true,
quote: true,
footnotes: true,
)
md = ''
open("#{location}/#{@mdFilename}") do |file|
md = file.read
end
htmlBody = markdown.render(md)
open(dst, 'wb') do |file|
file.puts '<html>'
file.puts '<head>'
file.puts "\t<title>#{name}</title>"
file.puts '</head>'
file.puts '<body>'
file.puts htmlBody
file.puts '</body>'
file.puts '</html>'
end
return name+'/index.html'
end
end