forked from napcs/docbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate
executable file
·258 lines (195 loc) · 8.18 KB
/
generate
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), "version"))
# add lchop to string class via a monkey patch
class String
# Remove the first character from the string.
def lchop
s = self.gsub(/^./, "")
end
# parses the filename from the string by assuming that the last node of the path is the filename.
def filename
self.split("/").last
end
# parses the dirname from the string by chopping off the last node of the path.
def dirname
s = self.split("/")
a = s[0..(s.length - 2)].join("/")
a
end
end
# generate a file by reading in the template and spitting out the results, substituting the rootpath in the document
def generate(file, substring, destination)
match = "<#ROOT_PATH#>"
s = get_file_as_string(file)
s.gsub!(match, substring)
put_file_from_string(destination, s)
end
# load a file into a string
def get_file_as_string(filename)
data = ''
File.open(filename, "r") { |f|
data = f.read
}
return data
end
# write the file from a string
def put_file_from_string(f, s)
File.open(f , 'w') do |file|
file.puts(s)
end
end
def show_instructions
text = %Q{Docbook Generator (C) 2008 Brian Hogan
=================================================
Creates a docbook project, chapter, or article skeleton.
generate book | chapter | article <path> (no extension)
Example: generate book my_book
- my_book/images
- my_book/images/src
- my_book/cover
- my_book/xsl
- my_book/xsl/pdf.xsl
- my_book/book.xml
- my_book/chapter01.xml
- my_book/make
or: generate chapter chapter02
- ./chapter02.xml
or: generate chapter chapter02/fun_stuff
- chapter02/fun_stuff.xml
or: generate article learning_rails
- learning_rails/images
- learning_rails/xsl
- learning_rails/xsl/pdf.xml
- learning_rails/make
- learning_rails/article.xml
You can get a sample book as well with contents
Example: generate book my_book with_sample
}
puts text
end
def article(root_path, dest_path, sample)
puts "Creating article..."
FileUtils.mkdir_p("#{dest_path}/images") rescue nil
puts " - #{dest_path}/images"
FileUtils.mkdir("#{dest_path}/images/src") rescue nil
puts " - #{dest_path}/images/src"
FileUtils.mkdir("#{dest_path}/cover") rescue nil
puts " - #{dest_path}/cover/"
FileUtils.mkdir("#{dest_path}/xsl") rescue nil
puts " - #{dest_path}/xsl"
if sample
FileUtils.cp("#{root_path}/template/article.xml", "#{dest_path}/article.xml")
else
FileUtils.cp("#{root_path}/template/article.xml", "#{dest_path}/article.xml")
end
# TODO: Deprecated?
xsl_path = PLATFORM.downcase.include?("win32") ? root_path : root_path.lchop
if File.exist?("#{dest_path}/xsl/pdf.xsl")
puts 'found old customization layer ... moving it'
FileUtils.mv "#{dest_path}/xsl/pdf.xsl","#{dest_path}/xsl/pdf.xsl.old"
FileUtils.cp "#{root_path}/template/xsl/pdf.xsl", "#{dest_path}/xsl/pdf.xsl"
FileUtils.mv "#{dest_path}/xsl/html.xsl","#{dest_path}/xsl/html.xsl.old"
FileUtils.cp "#{root_path}/template/xsl/html.xsl", "#{dest_path}/xsl/html.xsl"
else
FileUtils.cp "#{root_path}/template/xsl/pdf.xsl", "#{dest_path}/xsl/pdf.xsl"
FileUtils.cp "#{root_path}/template/xsl/html.xsl", "#{dest_path}/xsl/html.xsl"
end
puts " - #{dest_path}/xsl/pdf.xml"
puts " - #{dest_path}/xsl/html.xml"
FileUtils.cp "#{root_path}/template/w3centities-f.ent", "#{dest_path}/w3centities-f.ent"
puts " - #{dest_path}/w3centities-f.ent"
generate "#{root_path}/template/Rakefile", root_path, "#{dest_path}/Rakefile"
puts " - #{dest_path}/Rakefile"
puts " - #{dest_path}/article.xml"
version_stamp("#{dest_path}/VERSION.txt")
puts "Done"
end
def chapter(root_path, dest_path, sample)
directory = dest_path.dirname
FileUtils.mkdir_p(directory + "/images") unless directory.empty?
puts "Creating chapter..."
if sample
FileUtils.cp("#{root_path}/template/chapter01_sample.xml", "#{dest_path}.xml")
FileUtils.cp("#{root_path}/template/images/sample.png", "#{directory}/images") rescue nil
puts " - #{directory}/images/sample.png"
else
FileUtils.cp("#{root_path}/template/chapter01.xml", "#{dest_path}.xml")
end
FileUtils.cp "#{root_path}/template/w3centities-f.ent", "#{directory}/w3centities-f.ent"
puts " - #{directory}/w3centities-f.ent"
puts " - #{dest_path}.xml"
end
def book(root_path, dest_path, sample)
puts "Creating docbook project..."
FileUtils.mkdir(dest_path) rescue nil
FileUtils.mkdir("#{dest_path}/images") rescue nil
FileUtils.mkdir("#{dest_path}/xsl") rescue nil
FileUtils.mkdir("#{dest_path}/images/src") rescue nil
FileUtils.mkdir("#{dest_path}/cover") rescue nil
puts " - #{dest_path}/images"
puts " - #{dest_path}/images/src"
puts " - #{dest_path}/cover"
puts " - #{dest_path}/xsl"
if sample
FileUtils.cp("#{root_path}/template/images/sample.png", "#{dest_path}/images") rescue nil
puts " - #{dest_path}/images/sample.png"
if File.exist?("#{dest_path}/book.xml")
puts "book.xml exists - skipping"
else
FileUtils.cp("#{root_path}/template/book_sample.xml", "#{dest_path}/book.xml") rescue nil
FileUtils.cp("#{root_path}/template/chapter01_sample.xml", "#{dest_path}/chapter01.xml") rescue nil
puts " - #{dest_path}/book.xml"
puts " - #{dest_path}/chapter01.xml"
end
else
if File.exist?("#{dest_path}/book.xml")
puts "book.xml exists - skipping"
else
FileUtils.cp("#{root_path}/template/book.xml", "#{dest_path}/book.xml") rescue nil
FileUtils.cp("#{root_path}/template/chapter01.xml", "#{dest_path}/chapter01.xml") rescue nil
puts " - #{dest_path}/book.xml"
puts " - #{dest_path}/chapter01.xml"
end
end
generate "#{root_path}/template/Rakefile", root_path, "#{dest_path}/Rakefile"
puts " - #{dest_path}/Rakefile"
FileUtils.cp "#{root_path}/template/w3centities-f.ent", "#{dest_path}/w3centities-f.ent"
puts " - #{dest_path}/w3centities-f.ent"
# xsl_path is a file URL. File urls have three / (file:///) followed by the path Windows paths don't start with a slash
# but unix paths do. If it's windows, leave the path alone. If it's not windows, chop off the first character of the path
# TODO: Deprecated?
xsl_path = PLATFORM.downcase.include?("win32") ? root_path : root_path.lchop
if File.exist?("#{dest_path}/xsl/pdf.xsl")
puts 'found old customization layer ... moving it'
FileUtils.mv "#{dest_path}/xsl/pdf.xsl","#{dest_path}/xsl/pdf.xsl.old"
FileUtils.cp "#{root_path}/template/xsl/pdf.xsl", "#{dest_path}/xsl/pdf.xsl"
if File.exist?("#{dest_path}/xsl/html.xsl")
FileUtils.mv "#{dest_path}/xsl/html.xsl","#{dest_path}/xsl/html.xsl.old"
FileUtils.cp "#{root_path}/template/xsl/html.xsl", "#{dest_path}/xsl/html.xsl"
end
else
FileUtils.cp "#{root_path}/template/xsl/pdf.xsl", "#{dest_path}/xsl/pdf.xsl"
FileUtils.cp "#{root_path}/template/xsl/html.xsl", "#{dest_path}/xsl/html.xsl"
end
puts " - #{dest_path}/xsl/pdf.xml"
puts " - #{dest_path}/xsl/html.xml"
version_stamp("#{dest_path}/VERSION.txt")
puts "Done"
end
def version_stamp(path)
File.open(path, "w") do |file|
file << "Generated with #{DocbookVersion.to_s}"
end
end
if ARGV[0].nil?
show_instructions
elsif ARGV[0] == "-v" || ARGV[0] == "--version"
puts "#{DocbookVersion.to_s}"
else
require 'fileutils'
root_path = File.dirname(__FILE__)
type = ARGV[0]
output_path = ARGV[1]
sample = ARGV[2].include? "sample" rescue false
send(type, root_path, output_path, sample)
end