-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rb
executable file
·168 lines (145 loc) · 4.56 KB
/
build.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
#!/usr/bin/env ruby
require "rubygems"
require "maruku"
require "hpricot"
require "json/pure"
require "fileutils"
module TitaniumDocs
class Build
def initialize(path = File.dirname(__FILE__))
@path = path
@src_path ||= @path + "/Source/"
@build_path ||= @path + "/Build/"
@assets_path ||= @path + "/Assets/"
end
def move_assets
if not @no_assets
["css/", "scripts/", "images/"].each do |asset|
begin
FileUtils.cp_r @assets_path + asset, @build_path, {:remove_destination => true}
puts "Copying: #{asset}"
rescue
puts "Error in copying #{asset}"
end
end
end
end
def prepare_template
puts "Preparing template.."
@template_path ||= @assets_path + "templates/"
template_main = @template_path + "main.html"
template_nav = @template_path + "nav.html"
template_footer = @template_path + "footer.html"
@document = open(template_main) { |f| Hpricot(f) }
@nav = open(template_nav) { |f| Hpricot(f) }
@footer = open(template_footer) { |f| Hpricot(f) }
(@document/"#navigation").inner_html = @nav.inner_html
(@document/"#footer").inner_html = @footer.inner_html
end
def parse_nav
@manifest = JSON.load(open( @src_path + "manifest.json" ))
if @build_nav
nav_html = ""
@manifest.each_pair do |header, files|
header = header.gsub(/[0-9\s]*/, "")
nav_html += "<h2>#{header}</h2>\n<ul>\n"
files.each_pair do |name, title|
name = name.gsub(/[0-9\s]*/, "")
ptitle = title != "" && !title.respond_to?(:each_pair) ? title : name
nav_html += nav_item(header, name, ptitle)
if title.respond_to?(:each_pair)
nav_html += "<ul>\n"
title.each_pair do |sub, item|
sub = sub.gsub(/[0-9\s]*/, "")
item = item != "" ? item : sub
nav_html += nav_item(header, sub, item)
end
nav_html += "</ul>\n"
end
end
nav_html += "</ul>\n"
end
(@document/"#navlinks").inner_html += "\n#{nav_html}"
end
end
def nav_item(header, name, title)
return "<li><a href=\"/#{header}/#{name}\" >#{title}</a></li>\n"
end
def parse_doc(header, name, title)
path = "#{header.gsub(/[0-9\s]*/, "")}/#{name.gsub(/[0-9\s]*/, "")}"
doctitle = (@document/"title").inner_html;
(@document/"title").inner_html = "#{doctitle}: #{title != "" ? title : path}"
begin
test = File.read(@src_path + path + ".md")
doc = Maruku.new(test)
(@document/"#content").inner_html = doc.to_html
(@document/"#content blockquote.docstatus").remove unless @with_status
if @build_items.nil?
File.open(@build_path + path + ".html", 'w') { |fh| fh.write @document.inner_html}
puts "Generated: #{@build_path + path}.html"
else
if @build_items.include?(name.gsub(/[0-9\s]*/, ""))
File.open(@build_path + path + ".html", 'w') { |fh| fh.write @document.inner_html}
puts "Generated: #{@build_path + path}.html"
end
end
rescue
end
(@document/"title").inner_html = doctitle
end
def parse_docs
@manifest.each_pair do |header, files|
if @build_items.nil?
FileUtils.remove_dir(@build_path + header.gsub(/[0-9\s]*/, "")) unless not File.exists?(@build_path + header.gsub(/[0-9\s]*/, ""))
FileUtils.mkdir(@build_path + header.gsub(/[0-9\s]*/, ""))
end
files.each_pair do |name, title|
parse_doc(header, name, title)
if title.respond_to?(:each_pair)
title.each_pair do |sub, item|
parse_doc(header, sub, item)
end
end
end
end
end
def set_options(arg, value)
if arg == "noassets"
puts "Option: No Assets"
@no_assets = true
elsif arg == "path" && value != ""
puts "Option: Build Path = #{value}"
@build_path = value
elsif arg == "source" && value != ""
puts "Option: Build Path = #{value}"
@source_path = value
elsif arg == "withstatus"
puts "Option: With Document Status"
@with_status = true
elsif arg == "buildnav"
puts "Option: Build Navigation Bar"
@build_nav = true
elsif arg == "files" && value != ""
puts "Option: Files = #{value}"
@build_items = value.split(" ")
else
end
end
def self.build(args)
puts "Titanium Documentation Builder"
docbuild = TitaniumDocs::Build.new
args.each do |arg|
test = arg.scan(/--([a-zA-Z]*)[=]?([a-zA-Z0-9\s\\\/.:"~]*)/)
docbuild.set_options(test[0][0], test[0][1])
end
docbuild.prepare_template
docbuild.parse_nav
docbuild.parse_docs
docbuild.move_assets
puts "Done!"
end
end
end
if __FILE__ == $0
TitaniumDocs::Build.build(ARGV)
end