public class BuildSite {\r
\r
private static final String SPACE_DELIMITED = "SPACE-DELIMITED";\r
- \r
+\r
private static final String CASE_SENSITIVE = "CASE-SENSITIVE";\r
\r
private static final String RESTART_REQUIRED = "RESTART REQUIRED";\r
\r
System.out.println(MessageFormat.format("Generating site from {0} Markdown Docs in {1} ",\r
markdownFiles.length, sourceFolder.getAbsolutePath()));\r
- String linkPattern = "<li><a href=''{0}''>{1}</a></li>";\r
- StringBuilder sb = new StringBuilder();\r
- for (File file : markdownFiles) {\r
- String documentName = getDocumentName(file);\r
- if (!params.skips.contains(documentName)) {\r
- String displayName = documentName;\r
- if (aliasMap.containsKey(documentName)) {\r
- displayName = aliasMap.get(documentName);\r
- } else {\r
- displayName = displayName.replace('_', ' ');\r
- }\r
- String fileName = documentName + ".html";\r
- sb.append(MessageFormat.format(linkPattern, fileName, displayName));\r
- sb.append(" | ");\r
- }\r
- }\r
- sb.setLength(sb.length() - 3);\r
- sb.trimToSize();\r
\r
String htmlHeader = FileUtils.readContent(new File(params.pageHeader), "\n");\r
\r
}\r
}\r
String htmlFooter = FileUtils.readContent(new File(params.pageFooter), "\n");\r
- String links = sb.toString();\r
- String header = MessageFormat.format(htmlHeader, Constants.FULL_NAME, links);\r
- if (!StringUtils.isEmpty(params.analyticsSnippet)) {\r
- File snippet = new File(params.analyticsSnippet);\r
- if (snippet.exists()) {\r
- String htmlSnippet = FileUtils.readContent(snippet, "\n");\r
- header = header.replace("<!-- ANALYTICS -->", htmlSnippet);\r
- }\r
- }\r
final String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());\r
final String footer = MessageFormat.format(htmlFooter, "generated " + date);\r
for (File file : markdownFiles) {\r
+ String documentName = getDocumentName(file);\r
+ if (params.skips.contains(documentName)) {\r
+ continue;\r
+ }\r
try {\r
- String documentName = getDocumentName(file);\r
- if (!params.skips.contains(documentName)) {\r
- String fileName = documentName + ".html";\r
- System.out.println(MessageFormat.format(" {0} => {1}", file.getName(),\r
- fileName));\r
- String rawContent = FileUtils.readContent(file, "\n");\r
- String markdownContent = rawContent;\r
-\r
- Map<String, List<String>> nomarkdownMap = new HashMap<String, List<String>>();\r
-\r
- // extract sections marked as no-markdown\r
- int nmd = 0;\r
- for (String token : params.nomarkdown) {\r
- StringBuilder strippedContent = new StringBuilder();\r
-\r
- String nomarkdownKey = "%NOMARKDOWN" + nmd + "%";\r
- String[] kv = token.split(":", 2);\r
- String beginToken = kv[0];\r
- String endToken = kv[1];\r
-\r
- // strip nomarkdown chunks from markdown and cache them\r
- List<String> chunks = new Vector<String>();\r
- int beginCode = 0;\r
- int endCode = 0;\r
- while ((beginCode = markdownContent.indexOf(beginToken, endCode)) > -1) {\r
- if (endCode == 0) {\r
- strippedContent.append(markdownContent.substring(0, beginCode));\r
- } else {\r
- strippedContent.append(markdownContent\r
- .substring(endCode, beginCode));\r
- }\r
- strippedContent.append(nomarkdownKey);\r
- endCode = markdownContent.indexOf(endToken, beginCode);\r
- chunks.add(markdownContent.substring(beginCode, endCode));\r
- nomarkdownMap.put(nomarkdownKey, chunks);\r
- }\r
-\r
- // get remainder of text\r
- if (endCode < markdownContent.length()) {\r
- strippedContent.append(markdownContent.substring(endCode,\r
- markdownContent.length()));\r
- }\r
- markdownContent = strippedContent.toString();\r
- nmd++;\r
+ String links = createLinks(file, markdownFiles, aliasMap, params.skips);\r
+ String header = MessageFormat.format(htmlHeader, Constants.FULL_NAME, links);\r
+ if (!StringUtils.isEmpty(params.analyticsSnippet)) {\r
+ File snippet = new File(params.analyticsSnippet);\r
+ if (snippet.exists()) {\r
+ String htmlSnippet = FileUtils.readContent(snippet, "\n");\r
+ header = header.replace("<!-- ANALYTICS -->", htmlSnippet);\r
}\r
+ }\r
\r
- // transform markdown to html\r
- String content = transformMarkdown(markdownContent.toString());\r
-\r
- // reinsert nomarkdown chunks\r
- for (Map.Entry<String, List<String>> nomarkdown : nomarkdownMap.entrySet()) {\r
- for (String chunk : nomarkdown.getValue()) {\r
- content = content.replaceFirst(nomarkdown.getKey(), chunk);\r
+ String fileName = documentName + ".html";\r
+ System.out.println(MessageFormat.format(" {0} => {1}", file.getName(), fileName));\r
+ String rawContent = FileUtils.readContent(file, "\n");\r
+ String markdownContent = rawContent;\r
+\r
+ Map<String, List<String>> nomarkdownMap = new HashMap<String, List<String>>();\r
+\r
+ // extract sections marked as no-markdown\r
+ int nmd = 0;\r
+ for (String token : params.nomarkdown) {\r
+ StringBuilder strippedContent = new StringBuilder();\r
+\r
+ String nomarkdownKey = "%NOMARKDOWN" + nmd + "%";\r
+ String[] kv = token.split(":", 2);\r
+ String beginToken = kv[0];\r
+ String endToken = kv[1];\r
+\r
+ // strip nomarkdown chunks from markdown and cache them\r
+ List<String> chunks = new Vector<String>();\r
+ int beginCode = 0;\r
+ int endCode = 0;\r
+ while ((beginCode = markdownContent.indexOf(beginToken, endCode)) > -1) {\r
+ if (endCode == 0) {\r
+ strippedContent.append(markdownContent.substring(0, beginCode));\r
+ } else {\r
+ strippedContent.append(markdownContent.substring(endCode, beginCode));\r
}\r
+ strippedContent.append(nomarkdownKey);\r
+ endCode = markdownContent.indexOf(endToken, beginCode);\r
+ chunks.add(markdownContent.substring(beginCode, endCode));\r
+ nomarkdownMap.put(nomarkdownKey, chunks);\r
}\r
\r
- for (String token : params.substitutions) {\r
- String[] kv = token.split("=", 2);\r
- content = content.replace(kv[0], kv[1]);\r
- }\r
- for (String token : params.regex) {\r
- String[] kv = token.split("!!!", 2);\r
- content = content.replaceAll(kv[0], kv[1]);\r
- }\r
- for (String alias : params.properties) {\r
- String[] kv = alias.split("=", 2);\r
- String loadedContent = generatePropertiesContent(new File(kv[1]));\r
- content = content.replace(kv[0], loadedContent);\r
- }\r
- for (String alias : params.loads) {\r
- String[] kv = alias.split("=", 2);\r
- String loadedContent = FileUtils.readContent(new File(kv[1]), "\n");\r
- loadedContent = StringUtils.escapeForHtml(loadedContent, false);\r
- loadedContent = StringUtils.breakLinesForHtml(loadedContent);\r
- content = content.replace(kv[0], loadedContent);\r
+ // get remainder of text\r
+ if (endCode < markdownContent.length()) {\r
+ strippedContent.append(markdownContent.substring(endCode,\r
+ markdownContent.length()));\r
}\r
- OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(\r
- new File(destinationFolder, fileName)), Charset.forName("UTF-8"));\r
- writer.write(header);\r
- if (!StringUtils.isEmpty(htmlAdSnippet)) {\r
- writer.write(htmlAdSnippet);\r
+ markdownContent = strippedContent.toString();\r
+ nmd++;\r
+ }\r
+\r
+ // transform markdown to html\r
+ String content = transformMarkdown(markdownContent.toString());\r
+\r
+ // reinsert nomarkdown chunks\r
+ for (Map.Entry<String, List<String>> nomarkdown : nomarkdownMap.entrySet()) {\r
+ for (String chunk : nomarkdown.getValue()) {\r
+ content = content.replaceFirst(nomarkdown.getKey(), chunk);\r
}\r
- writer.write(content);\r
- writer.write(footer);\r
- writer.close();\r
}\r
+\r
+ for (String token : params.substitutions) {\r
+ String[] kv = token.split("=", 2);\r
+ content = content.replace(kv[0], kv[1]);\r
+ }\r
+ for (String token : params.regex) {\r
+ String[] kv = token.split("!!!", 2);\r
+ content = content.replaceAll(kv[0], kv[1]);\r
+ }\r
+ for (String alias : params.properties) {\r
+ String[] kv = alias.split("=", 2);\r
+ String loadedContent = generatePropertiesContent(new File(kv[1]));\r
+ content = content.replace(kv[0], loadedContent);\r
+ }\r
+ for (String alias : params.loads) {\r
+ String[] kv = alias.split("=", 2);\r
+ String loadedContent = FileUtils.readContent(new File(kv[1]), "\n");\r
+ loadedContent = StringUtils.escapeForHtml(loadedContent, false);\r
+ loadedContent = StringUtils.breakLinesForHtml(loadedContent);\r
+ content = content.replace(kv[0], loadedContent);\r
+ }\r
+ OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File(\r
+ destinationFolder, fileName)), Charset.forName("UTF-8"));\r
+ writer.write(header);\r
+ if (!StringUtils.isEmpty(htmlAdSnippet)) {\r
+ writer.write(htmlAdSnippet);\r
+ }\r
+ writer.write(content);\r
+ writer.write(footer);\r
+ writer.close();\r
} catch (Throwable t) {\r
System.err.println("Failed to transform " + file.getName());\r
t.printStackTrace();\r
return displayName;\r
}\r
\r
+ private static String createLinks(File currentFile, File[] markdownFiles,\r
+ Map<String, String> aliasMap, List<String> skips) {\r
+ String linkPattern = "<li><a href=''{0}''>{1}</a></li>";\r
+ String currentLinkPattern = "<li class=''active''><a href=''{0}''>{1}</a></li>";\r
+ StringBuilder sb = new StringBuilder();\r
+ for (File file : markdownFiles) {\r
+ String documentName = getDocumentName(file);\r
+ if (!skips.contains(documentName)) {\r
+ String displayName = documentName;\r
+ if (aliasMap.containsKey(documentName)) {\r
+ displayName = aliasMap.get(documentName);\r
+ } else {\r
+ displayName = displayName.replace('_', ' ');\r
+ }\r
+ String fileName = documentName + ".html";\r
+ if (currentFile.getName().equals(file.getName())) {\r
+ sb.append(MessageFormat.format(currentLinkPattern, fileName, displayName));\r
+ } else {\r
+ sb.append(MessageFormat.format(linkPattern, fileName, displayName));\r
+ }\r
+ }\r
+ }\r
+ sb.setLength(sb.length() - 3);\r
+ sb.trimToSize();\r
+ return sb.toString();\r
+ }\r
+\r
private static String generatePropertiesContent(File propertiesFile) throws Exception {\r
// Read the current Gitblit properties\r
BufferedReader propertiesReader = new BufferedReader(new FileReader(propertiesFile));\r