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
|
# File 'lib/webapidoc/base.rb', line 33
def self.build
@data = YAML.load_file(@@configFile).freeze
puts "building webapidoc for " + @data["title"]
puts "api url: " + @data["url"] if @data["url"]
FileUtils.remove_dir @@publicDir if File.exists?(@@publicDir)
FileUtils.mkdir_p @@publicDir
FileUtils.mkdir @@publicDir + "/css"
FileUtils.cp_r(libDir + "/js", @@publicDir + "/js")
sass_filename = libDir + "/css/webapidoc.scss"
css = Sass::Engine.for_file(sass_filename, {:style => :compressed}).render
File.open(@@publicDir + "/css/webapidoc.css", "wb") {|f| f.write(css) }
inFile = "#{@@docDir}/documentation.md"
if !File.exists?(inFile)
inFile = inFile + ".erb"
end
if !File.exists?(inFile)
puts "documentation not found: #{inFile}"
exit 0
end
contents = ERB.new(File.open(inFile, 'r').read).result(binding)
maruku = Maruku.new(contents)
html = Nokogiri::HTML(maruku.to_html)
@chapters = []
@chapters = html.xpath('//body').children.inject([]) do |chapters_hash, child|
if child.name == 'h1'
title = child.inner_text
anchor = title.downcase.gsub(/[^\d\w\s]/, "").tr(" ", "_")
chapters_hash << { :title => title, :contents => '', :file => anchor}
end
next chapters_hash if chapters_hash.empty?
chapters_hash.last[:contents] << child.to_xhtml
chapters_hash
end
@sections = []
cidx = -1
html.xpath('//body').children.inject([]) do |chapters_hash, child|
if child.name == 'h1' or child.name == 'h2' or child.name == 'h3'
title = child.inner_text
anchor = title.downcase.gsub(/[^\d\w\s]/, "").tr(" ", "_")
level = child.name[-1, 1]
cidx = cidx + 1 if level == "1"
next unless @chapters[cidx].present?
file = @chapters[cidx][:file]
@sections << {:title => title, :file => file, :anchor => anchor, :level => level}
end
end
template = "#{libDir}/template.html.erb"
@chapters.each_with_index do | chapter, idx |
outFile = "#{@@publicDir}/#{chapter[:file]}.html"
puts idx.to_s + ":" + chapter[:title] + " > " + outFile
@html = chapter[:contents]
File.open(outFile, 'w') { |file| file.write(ERB.new(File.open(template, 'r').read).result(binding)) }
end
end
|