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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
* Copyright 2011 James Moger.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.iciql.build;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.tautua.markdownpapers.Markdown;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import com.iciql.Constants;
import com.iciql.util.StringUtils;
/**
* Builds the web site or deployment documentation from Markdown source files.
*
* All Markdown source files must have the .mkd extension.
*
* Natural string sort order of the Markdown source filenames is the order of
* page links. "##_" prefixes are used to control the sort order.
*
* @author James Moger
*
*/
public class BuildSite {
public static void main(String... args) {
Params params = new Params();
JCommander jc = new JCommander(params);
try {
jc.parse(args);
} catch (ParameterException t) {
usage(jc, t);
}
File sourceFolder = new File(params.sourceFolder);
File destinationFolder = new File(params.outputFolder);
File[] markdownFiles = sourceFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".mkd");
}
});
Arrays.sort(markdownFiles);
Map<String, String> aliasMap = new HashMap<String, String>();
for (String alias : params.aliases) {
String[] values = alias.split("=");
aliasMap.put(values[0], values[1]);
}
System.out.println(MessageFormat.format("Generating site from {0} Markdown Docs in {1} ", markdownFiles.length,
sourceFolder.getAbsolutePath()));
String linkPattern = "<a href=''{0}''>{1}</a>";
StringBuilder sb = new StringBuilder();
for (File file : markdownFiles) {
String documentName = getDocumentName(file);
if (!params.skips.contains(documentName)) {
String displayName = documentName;
if (aliasMap.containsKey(documentName)) {
displayName = aliasMap.get(documentName);
} else {
displayName = displayName.replace('_', ' ');
}
String fileName = documentName + ".html";
sb.append(MessageFormat.format(linkPattern, fileName, displayName));
sb.append(" | ");
}
}
sb.setLength(sb.length() - 3);
sb.trimToSize();
String htmlHeader = readContent(new File(params.pageHeader), "\n");
String htmlAdSnippet = null;
if (!StringUtils.isNullOrEmpty(params.adSnippet)) {
File snippet = new File(params.adSnippet);
if (snippet.exists()) {
htmlAdSnippet = readContent(snippet, "\n");
}
}
String htmlFooter = readContent(new File(params.pageFooter), "\n");
String links = sb.toString();
String header = MessageFormat.format(htmlHeader, Constants.NAME, links);
if (!StringUtils.isNullOrEmpty(params.analyticsSnippet)) {
File snippet = new File(params.analyticsSnippet);
if (snippet.exists()) {
String htmlSnippet = readContent(snippet, "\n");
header = header.replace("<!-- ANALYTICS -->", htmlSnippet);
}
}
final String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
final String footer = MessageFormat.format(htmlFooter, "generated " + date);
for (File file : markdownFiles) {
try {
String documentName = getDocumentName(file);
if (!params.skips.contains(documentName)) {
String fileName = documentName + ".html";
System.out.println(MessageFormat.format(" {0} => {1}", file.getName(), fileName));
String rawContent = readContent(file, "\n");
String markdownContent = rawContent;
Map<String, List<String>> nomarkdownMap = new HashMap<String, List<String>>();
// extract sections marked as no-markdown
int nmd = 0;
for (String token : params.nomarkdown) {
StringBuilder strippedContent = new StringBuilder();
String nomarkdownKey = "%NOMARKDOWN" + nmd + "%";
String[] kv = token.split(":", 2);
String beginToken = kv[0];
String endToken = kv[1];
// strip nomarkdown chunks from markdown and cache them
List<String> chunks = new Vector<String>();
int beginCode = 0;
int endCode = 0;
while ((beginCode = markdownContent.indexOf(beginToken, endCode)) > -1) {
if (endCode == 0) {
strippedContent.append(markdownContent.substring(0, beginCode));
} else {
strippedContent.append(markdownContent.substring(endCode, beginCode));
}
strippedContent.append(nomarkdownKey);
endCode = markdownContent.indexOf(endToken, beginCode);
chunks.add(markdownContent.substring(beginCode, endCode));
nomarkdownMap.put(nomarkdownKey, chunks);
}
// get remainder of text
if (endCode < markdownContent.length()) {
strippedContent.append(markdownContent.substring(endCode, markdownContent.length()));
}
markdownContent = strippedContent.toString();
nmd++;
}
// transform markdown to html
String content = transformMarkdown(new StringReader(markdownContent.toString()));
// reinsert nomarkdown chunks
for (Map.Entry<String, List<String>> nomarkdown : nomarkdownMap.entrySet()) {
for (String chunk : nomarkdown.getValue()) {
content = content.replaceFirst(nomarkdown.getKey(), chunk);
}
}
// perform specified substitutions
for (String token : params.substitutions) {
String[] kv = token.split("=", 2);
content = content.replace(kv[0], kv[1]);
}
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File(destinationFolder,
fileName)), Charset.forName("UTF-8"));
writer.write(header);
if (!StringUtils.isNullOrEmpty(htmlAdSnippet)) {
writer.write(htmlAdSnippet);
}
writer.write(content);
writer.write(footer);
writer.close();
}
} catch (Throwable t) {
System.err.println("Failed to transform " + file.getName());
t.printStackTrace();
}
}
}
private static String getDocumentName(File file) {
String displayName = file.getName().substring(0, file.getName().lastIndexOf('.')).toLowerCase();
int underscore = displayName.indexOf('_') + 1;
if (underscore > -1) {
// trim leading ##_ which is to control display order
return displayName.substring(underscore);
}
return displayName;
}
/**
* Returns the string content of the specified file.
*
* @param file
* @param lineEnding
* @return the string content of the file
*/
private static String readContent(File file, String lineEnding) {
StringBuilder sb = new StringBuilder();
try {
InputStreamReader is = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(is);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
if (lineEnding != null) {
sb.append(lineEnding);
}
}
reader.close();
} catch (Throwable t) {
System.err.println("Failed to read content of " + file.getAbsolutePath());
t.printStackTrace();
}
return sb.toString();
}
private static String transformMarkdown(Reader markdownReader) throws ParseException {
// Read raw markdown content and transform it to html
StringWriter writer = new StringWriter();
try {
Markdown md = new Markdown();
md.transform(markdownReader, writer);
return writer.toString().trim();
} catch (org.tautua.markdownpapers.parser.ParseException p) {
throw new java.text.ParseException(p.getMessage(), 0);
} finally {
try {
markdownReader.close();
} catch (IOException e) {
// IGNORE
}
try {
writer.close();
} catch (IOException e) {
// IGNORE
}
}
}
private static void usage(JCommander jc, ParameterException t) {
System.out.println(Constants.NAME + " v" + Constants.VERSION);
System.out.println();
if (t != null) {
System.out.println(t.getMessage());
System.out.println();
}
if (jc != null) {
jc.usage();
}
System.exit(0);
}
/**
* Command-line parameters for BuildSite utility.
*/
@Parameters(separators = " ")
private static class Params {
@Parameter(names = { "--sourceFolder" }, description = "Markdown Source Folder", required = true)
public String sourceFolder;
@Parameter(names = { "--outputFolder" }, description = "HTML Ouptut Folder", required = true)
public String outputFolder;
@Parameter(names = { "--pageHeader" }, description = "Page Header HTML Snippet", required = true)
public String pageHeader;
@Parameter(names = { "--pageFooter" }, description = "Page Footer HTML Snippet", required = true)
public String pageFooter;
@Parameter(names = { "--adSnippet" }, description = "Ad HTML Snippet", required = false)
public String adSnippet;
@Parameter(names = { "--analyticsSnippet" }, description = "Analytics HTML Snippet", required = false)
public String analyticsSnippet;
@Parameter(names = { "--skip" }, description = "Filename to skip", required = false)
public List<String> skips = new ArrayList<String>();
@Parameter(names = { "--alias" }, description = "Filename=Linkname aliases", required = false)
public List<String> aliases = new ArrayList<String>();
@Parameter(names = { "--substitute" }, description = "%TOKEN%=value", required = false)
public List<String> substitutions = new ArrayList<String>();
@Parameter(names = { "--nomarkdown" }, description = "%STARTTOKEN%:%ENDTOKEN%", required = false)
public List<String> nomarkdown = new ArrayList<String>();
}
}
|