From 2c32fd202bac23df456d6515f1c33443cc803f35 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 13 Sep 2011 08:10:03 -0400
Subject: [PATCH] Revised federation setting names.  Improved documentation.

---
 src/com/gitblit/build/BuildSite.java |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 165 insertions(+), 10 deletions(-)

diff --git a/src/com/gitblit/build/BuildSite.java b/src/com/gitblit/build/BuildSite.java
index a2f6431..dca5116 100644
--- a/src/com/gitblit/build/BuildSite.java
+++ b/src/com/gitblit/build/BuildSite.java
@@ -15,14 +15,15 @@
  */
 package com.gitblit.build;
 
+import java.io.BufferedReader;
 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;
@@ -30,6 +31,7 @@
 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;
@@ -52,6 +54,12 @@
  * 
  */
 public class BuildSite {
+
+	private static final String CASE_SENSITIVE = "CASE-SENSITIVE";
+
+	private static final String RESTART_REQUIRED = "RESTART REQUIRED";
+
+	private static final String SINCE = "SINCE";
 
 	public static void main(String... args) {
 		Params params = new Params();
@@ -89,6 +97,8 @@
 				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));
@@ -99,7 +109,7 @@
 		sb.trimToSize();
 
 		String htmlHeader = FileUtils.readContent(new File(params.pageHeader), "\n");
-		
+
 		String htmlAdSnippet = null;
 		if (!StringUtils.isEmpty(params.adSnippet)) {
 			File snippet = new File(params.adSnippet);
@@ -116,7 +126,7 @@
 				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) {
@@ -126,15 +136,72 @@
 					String fileName = documentName + ".html";
 					System.out.println(MessageFormat.format("  {0} => {1}", file.getName(),
 							fileName));
-					InputStreamReader reader = new InputStreamReader(new FileInputStream(file),
-							Charset.forName("UTF-8"));
-					String content = MarkdownUtils.transformMarkdown(reader);
+					String rawContent = FileUtils.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(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);
+						}
+					}
+
 					for (String token : params.substitutions) {
-						String[] kv = token.split("=");
+						String[] kv = token.split("=", 2);
 						content = content.replace(kv[0], kv[1]);
 					}
+					for (String token : params.regex) {
+						String[] kv = token.split("!!!", 2);
+						content = content.replaceAll(kv[0], kv[1]);
+					}
+					for (String alias : params.properties) {
+						String[] kv = alias.split("=", 2);
+						String loadedContent = generatePropertiesContent(new File(kv[1]));
+						content = content.replace(kv[0], loadedContent);
+					}
 					for (String alias : params.loads) {
-						String[] kv = alias.split("=");
+						String[] kv = alias.split("=", 2);
 						String loadedContent = FileUtils.readContent(new File(kv[1]), "\n");
 						loadedContent = StringUtils.escapeForHtml(loadedContent, false);
 						loadedContent = StringUtils.breakLinesForHtml(loadedContent);
@@ -148,7 +215,6 @@
 					}
 					writer.write(content);
 					writer.write(footer);
-					reader.close();
 					writer.close();
 				}
 			} catch (Throwable t) {
@@ -169,6 +235,68 @@
 		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) {
 		System.out.println(Constants.getGitBlitVersion());
 		System.out.println();
@@ -182,6 +310,24 @@
 		System.exit(0);
 	}
 
+	/**
+	 * Setting represents a setting with its comments from the properties file.
+	 */
+	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);
+		}
+	}
+
+	/**
+	 * JCommander Parameters class for BuildSite.
+	 */
 	@Parameters(separators = " ")
 	private static class Params {
 
@@ -215,5 +361,14 @@
 		@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>();
+
+		@Parameter(names = { "--nomarkdown" }, description = "%STARTTOKEN%:%ENDTOKEN%", required = false)
+		public List<String> nomarkdown = new ArrayList<String>();
+
+		@Parameter(names = { "--regex" }, description = "searchPattern!!!replacePattern", required = false)
+		public List<String> regex = new ArrayList<String>();
+
 	}
 }

--
Gitblit v1.9.1