From 9cf48d26e7c95306844a2f8e640cd691de3be220 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 12 Jul 2011 20:27:56 -0400
Subject: [PATCH] Fix to build method.
---
src/com/gitblit/build/BuildSite.java | 161 ++++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 133 insertions(+), 28 deletions(-)
diff --git a/src/com/gitblit/build/BuildSite.java b/src/com/gitblit/build/BuildSite.java
index 5a9825a..97615f8 100644
--- a/src/com/gitblit/build/BuildSite.java
+++ b/src/com/gitblit/build/BuildSite.java
@@ -19,11 +19,13 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
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;
@@ -31,16 +33,35 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Vector;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import com.gitblit.Constants;
+import com.gitblit.utils.FileUtils;
import com.gitblit.utils.MarkdownUtils;
import com.gitblit.utils.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 {
+
+ private final static String CASE_SENSITIVE = "CASE-SENSITIVE";
+
+ private final static String RESTART_REQUIRED = "RESTART REQUIRED";
+
+ private final static String SINCE = "SINCE";
public static void main(String... args) {
Params params = new Params();
@@ -87,10 +108,25 @@
sb.setLength(sb.length() - 3);
sb.trimToSize();
- String htmlHeader = readContent(new File(params.pageHeader), "\n");
- String htmlFooter = readContent(new File(params.pageFooter), "\n");
- final String links = sb.toString();
- final String header = MessageFormat.format(htmlHeader, Constants.FULL_NAME, links);
+ String htmlHeader = FileUtils.readContent(new File(params.pageHeader), "\n");
+
+ String htmlAdSnippet = null;
+ if (!StringUtils.isEmpty(params.adSnippet)) {
+ File snippet = new File(params.adSnippet);
+ if (snippet.exists()) {
+ htmlAdSnippet = FileUtils.readContent(snippet, "\n");
+ }
+ }
+ String htmlFooter = FileUtils.readContent(new File(params.pageFooter), "\n");
+ String links = sb.toString();
+ String header = MessageFormat.format(htmlHeader, Constants.FULL_NAME, links);
+ if (!StringUtils.isEmpty(params.analyticsSnippet)) {
+ File snippet = new File(params.analyticsSnippet);
+ if (snippet.exists()) {
+ String htmlSnippet = FileUtils.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) {
@@ -107,9 +143,14 @@
String[] kv = token.split("=");
content = content.replace(kv[0], kv[1]);
}
+ for (String alias : params.properties) {
+ String[] kv = alias.split("=");
+ String loadedContent = generatePropertiesContent(new File(kv[1]));
+ content = content.replace(kv[0], loadedContent);
+ }
for (String alias : params.loads) {
String[] kv = alias.split("=");
- String loadedContent = readContent(new File(kv[1]), "\n");
+ String loadedContent = FileUtils.readContent(new File(kv[1]), "\n");
loadedContent = StringUtils.escapeForHtml(loadedContent, false);
loadedContent = StringUtils.breakLinesForHtml(loadedContent);
content = content.replace(kv[0], loadedContent);
@@ -117,6 +158,9 @@
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(
new File(destinationFolder, fileName)), Charset.forName("UTF-8"));
writer.write(header);
+ if (!StringUtils.isEmpty(htmlAdSnippet)) {
+ writer.write(htmlAdSnippet);
+ }
writer.write(content);
writer.write(footer);
reader.close();
@@ -129,32 +173,72 @@
}
}
- 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 getDocumentName(File file) {
String displayName = file.getName().substring(0, file.getName().lastIndexOf('.'))
.toLowerCase();
- // trim leading ##_ which is to control display order
- return displayName.substring(3);
+ int underscore = displayName.indexOf('_') + 1;
+ if (underscore > -1) {
+ // trim leading ##_ which is to control display order
+ return displayName.substring(underscore);
+ }
+ return displayName;
+ }
+
+ private static String generatePropertiesContent(File propertiesFile) throws Exception {
+ // Read the current Gitblit properties
+ BufferedReader propertiesReader = new BufferedReader(new FileReader(propertiesFile));
+
+ Vector<Setting> settings = new Vector<Setting>();
+ List<String> comments = new ArrayList<String>();
+ String line = null;
+ while ((line = propertiesReader.readLine()) != null) {
+ if (line.length() == 0) {
+ Setting s = new Setting("", "", comments);
+ settings.add(s);
+ comments.clear();
+ } else {
+ if (line.charAt(0) == '#') {
+ comments.add(line.substring(1).trim());
+ } else {
+ String[] kvp = line.split("=", 2);
+ String key = kvp[0].trim();
+ Setting s = new Setting(key, kvp[1].trim(), comments);
+ settings.add(s);
+ comments.clear();
+ }
+ }
+ }
+ propertiesReader.close();
+
+ StringBuilder sb = new StringBuilder();
+ for (Setting setting : settings) {
+ for (String comment : setting.comments) {
+ if (comment.contains(SINCE) || comment.contains(RESTART_REQUIRED)
+ || comment.contains(CASE_SENSITIVE)) {
+ sb.append(MessageFormat.format("<span style=\"color:#004000;\"># <i>{0}</i></span>", transformMarkdown(comment)));
+ } else {
+ sb.append(MessageFormat.format("<span style=\"color:#004000;\"># {0}</span>", transformMarkdown(comment)));
+ }
+ sb.append("<br/>\n");
+ }
+ if (!StringUtils.isEmpty(setting.name)) {
+ sb.append(MessageFormat.format("<span style=\"color:#000080;\">{0}</span> = <span style=\"color:#800000;\">{1}</span>", setting.name, StringUtils.escapeForHtml(setting.value, false)));
+ }
+ sb.append("<br/>\n");
+ }
+
+ return sb.toString();
+ }
+
+ private static String transformMarkdown(String comment) throws ParseException {
+ String md = MarkdownUtils.transformMarkdown(comment);
+ if (md.startsWith("<p>")) {
+ md = md.substring(3);
+ }
+ if (md.endsWith("</p>")) {
+ md = md.substring(0, md.length() - 4);
+ }
+ return md;
}
private static void usage(JCommander jc, ParameterException t) {
@@ -168,6 +252,18 @@
jc.usage();
}
System.exit(0);
+ }
+
+ private static class Setting {
+ final String name;
+ final String value;
+ final List<String> comments;
+
+ Setting(String name, String value, List<String> comments) {
+ this.name = name;
+ this.value = value;
+ this.comments = new ArrayList<String>(comments);
+ }
}
@Parameters(separators = " ")
@@ -185,6 +281,12 @@
@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>();
@@ -197,5 +299,8 @@
@Parameter(names = { "--load" }, description = "%TOKEN%=filename", required = false)
public List<String> loads = new ArrayList<String>();
+ @Parameter(names = { "--properties" }, description = "%TOKEN%=filename", required = false)
+ public List<String> properties = new ArrayList<String>();
+
}
}
--
Gitblit v1.9.1