Permalink
Browse files

Integrate numbering images and headers in the dist targets. Refactor …

…code buildfile.
  • Loading branch information...
1 parent 53d8715 commit 32108366478da08ce0e0ce46c74b09cfcd740ef3 @jhinkey jhinkey committed Apr 12, 2012
View
2 .gitignore
@@ -1,5 +1,6 @@
*~
*.bak
+*.class
*.iml
*.swp
*-tmp.html
@@ -14,6 +15,7 @@ nbbuild.xml
/build.*.properties
**/build/**
+**/classes/**
**/dist/**
**nbproject**
*#
View
55 build-common.xml
@@ -14,6 +14,17 @@ directory to convert the userGuide into complete English markdown, html, odt, an
-->
<project name="Markdown Document Conversion" default="echo-props" basedir=".">
+ <property name="project.dir" value=".." />
+
+ <path id="lib.classpath">
+ <fileset dir="${basedir}/lib" includes="*.jar" />
+ <fileset dir="${basedir}/code/liferay-doc-utils/lib" includes="*.jar" />
+ </path>
+
+ <path id="project.classpath">
+ <pathelement path="${classpath}" />
+ <path refid="lib.classpath" />
+ </path>
<property environment="env" />
@@ -31,6 +42,32 @@ directory to convert the userGuide into complete English markdown, html, odt, an
<property name="pandoc.app" location="/home/${user.name}/.cabal/bin/pandoc"/>
+ <!-- taskdefs -->
+
+ <taskdef name="numberheaders" classname="com.liferay.documentation.util.NumberHeadersTask">
+ <classpath>
+ <fileset dir="${project.dir}/lib" includes="*.jar" />
+ <!-- Use below instead of above fileset when testing changes to classes
+ <pathelement path="${project.dir}/code/liferay-doc-utils/classes"/>
+ -->
+
+ <fileset dir="${project.dir}/code/liferay-doc-utils/lib" includes="*.jar" />
+ </classpath>
+ </taskdef>
+
+ <taskdef name="numberimages" classname="com.liferay.documentation.util.NumberImagesTask">
+ <classpath>
+ <fileset dir="${project.dir}/lib" includes="*.jar" />
+ <!-- Use below instead of above fileset when testing changes to classes
+ <pathelement path="${project.dir}/code/liferay-doc-utils/classes"/>
+ -->
+
+ <fileset dir="${project.dir}/code/liferay-doc-utils/lib" includes="*.jar" />
+ </classpath>
+ </taskdef>
+
+ <!-- targets -->
+
<target name="display-pandoc-version">
<exec executable="${pandoc.app}">
<arg value="-v" />
@@ -66,7 +103,11 @@ directory to convert the userGuide into complete English markdown, html, odt, an
</copy>
</target>
- <target name="prepare-dist" description="creates dist directory copying images and overriding with language specific images">
+ <target name="prepare-dist" depends="number-images-dir" description="numbers the images in the files, creates dist directory, copies images and overriding with language specific images">
+ <!-- Replace the above the dependencies with below when liferay portal
+ supports urlTitle assignement.
+ depends="number-headers-dir, number-images-dir"
+ -->
<echo message="... creating ${dist.dir}/${lang} directory"/>
<mkdir dir="${dist.dir}/${lang}"/>
<copy todir="${dist.dir}/${lang}/images">
@@ -242,7 +283,17 @@ directory to convert the userGuide into complete English markdown, html, odt, an
<target name="markdown-to-all" depends="markdown-to-html,markdown-to-odt,markdown-to-epub" description="converts markdown file to html, odt, and epub files" />
<target name="markdown-to-all-win" depends="markdown-to-html-win,markdown-to-odt-win,markdown-to-epub-win" description="converts markdown file to html, odt, and epub files on windows" />
-
+
+ <target name="number-headers-dir" description="numbers the headers of markdown files found in ${lang}/chapters/">
+ <numberheaders lang="${lang}" docDir="${doc.dir}">
+ </numberheaders>
+ </target>
+
+ <target name="number-images-dir" description="numbers the images of markdown files found in ${lang}/chapters/">
+ <numberimages lang="${lang}" docDir="${doc.dir}">
+ </numberimages>
+ </target>
+
<target name="copy-single-chapter" depends="prepare" description="copies chapter to be converted">
<echo message="... copying ${chapter}"/>
<copy file="./en/chapters/${chapter}.markdown" todir="./${build.dir}/${lang}"/>
View
60 code/liferay-doc-utils/build.xml
@@ -1,13 +1,63 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<project default="create_run_jar" name="Create Runnable Jar for Project liferay-doc-utils">
- <target name="create_run_jar">
- <jar destfile="./dist/liferay-doc-utils.jar" filesetmanifest="mergewithoutmain">
+<project default="dist">
+ <property name="project.dir" value="." />
+
+ <property name="build.dir" value="${project.dir}/classes" />
+ <property name="dist.dir" value="${project.dir}/dist" />
+ <property name="lib.dir" value="${project.dir}/lib" />
+ <property name="src.dir" value="${project.dir}/src" />
+
+ <path id="lib.classpath">
+ <fileset dir="${lib.dir}" includes="*.jar" />
+ </path>
+
+ <path id="project.classpath">
+ <pathelement path="${classpath}" />
+ <path refid="lib.classpath" />
+ </path>
+
+ <!-- targets -->
+
+ <target name="clean" description="cleans up" >
+ <delete dir="${build.dir}" />
+ <delete dir="${dist.dir}" />
+ </target>
+
+ <target name="compile" description="compiles the code">
+ <mkdir dir="${build.dir}" />
+
+ <antcall target="compile-java">
+ <param name="javac.classpathref" value="project.classpath" />
+ <param name="javac.destdir" value="${build.dir}" />
+ <param name="javac.srcdir" value="${src.dir}" />
+ </antcall>
+ </target>
+
+ <target name="compile-java">
+ <copy todir="${javac.destdir}">
+ <fileset dir="${javac.srcdir}" excludes="**/*.java" />
+ </copy>
+
+ <javac
+ classpathref="${javac.classpathref}"
+ destdir="${javac.destdir}"
+ includeAntRuntime="false"
+ srcdir="${javac.srcdir}"
+ />
+ </target>
+
+ <target name="dist" depends="compile"
+ description="compiles the code and creates a runnable JAR file for the project">
+ <mkdir dir="${dist.dir}" />
+ <jar destfile="${dist.dir}/liferay-doc-utils.jar"
+ filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="com.liferay.documentation.util.DocUtils"/>
<attribute name="Class-Path" value="."/>
</manifest>
- <fileset dir="./bin"/>
- <zipfileset excludes="META-INF/*.SF" src="./lib/commons-io-2.0.1.jar"/>
+ <fileset dir="${build.dir}"/>
+ <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/commons-io-2.0.1.jar"/>
</jar>
</target>
+
</project>
View
BIN code/liferay-doc-utils/lib/ant-1-8-2.jar
Binary file not shown.
View
242 code/liferay-doc-utils/src/com/liferay/documentation/util/NumberHeadersTask.java
@@ -0,0 +1,242 @@
+package com.liferay.documentation.util;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.util.HashMap;
+import java.util.regex.Pattern;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+
+public class NumberHeadersTask extends Task {
+
+ @Override
+ public void execute() throws BuildException {
+ boolean foundDuplicateIds = false;
+
+ File chDir = new File("../" + docDir + "/" + lang + "/chapters");
+ System.out.println("Numbering headers for files in " + chDir.getPath() +
+ " ...");
+
+ if (!chDir.exists() || !chDir.isDirectory()) {
+ throw new BuildException("FAILURE - bad chapters directory " + chDir);
+ }
+
+ // Get listing of markdown files
+ String[] files = chDir.list(new FilenameFilter() {
+ String filePatternArg = "([0-9]+)([^\\\\\\[\\]\\|:;%<>]+).markdown";
+ Pattern fileNamePattern = Pattern.compile(filePatternArg);
+
+ public boolean accept(File file, String name) {
+ return (fileNamePattern.matcher(name).matches());
+ }
+ });
+
+ if (files == null || files.length == 0) {
+ throw new BuildException("FAILURE - no markdown files found in " + chDir);
+ }
+
+ // Process each file
+
+ for (int i = 0; i < files.length; i++) {
+ String filename = files[i];
+ try {
+ String inFile = chDir.getPath() + "\\" + filename;
+ String outFile = chDir.getPath() + "/" + filename;
+ String outFileTmp = outFile + ".tmp";
+
+ LineNumberReader in = new LineNumberReader(new FileReader(inFile));
+ BufferedWriter out = new BufferedWriter(new FileWriter(outFileTmp));
+
+ String line;
+ while ((line = in.readLine()) != null) {
+
+ if (line.startsWith("#")) {
+
+ String newHeadingLine = handleHeaderLine(line, filename, in.getLineNumber());
+ if (newHeadingLine != null) {
+ line = newHeadingLine;
+ }
+ else {
+ foundDuplicateIds = true;
+ }
+ }
+
+ out.append(line);
+ out.append("\n");
+ }
+ in.close();
+
+ out.flush();
+ out.close();
+
+ // Replace original file with modified temp file
+ FileUtils.copyFile(new File(outFileTmp), new File(chDir.getPath() + "/" + filename));
+ FileUtils.forceDelete(new File(outFileTmp));
+ } catch (IOException e) {
+ throw new BuildException(e.getLocalizedMessage());
+ }
+ }
+
+ if (foundDuplicateIds) {
+ throw new BuildException("FAILURE - Duplicate header IDs exist");
+ }
+ }
+
+ private String extractHeading(String line, int indexOfFirstHeaderChar) {
+ String heading2 = line.substring(indexOfFirstHeaderChar);
+
+ // Replace each space with a dash
+ heading2 = heading2.replace(' ', '-').toLowerCase();
+
+ // Filter out characters other than dashes, letters, and digits
+ StringBuffer headingSb = new StringBuffer();
+ for (int i = 0; i < heading2.length(); i++) {
+ char ch = heading2.charAt(i);
+
+ if (ch == '-' || Character.isLetterOrDigit(ch)) {
+ headingSb.append(ch);
+ }
+ }
+ heading2 = headingSb.toString();
+ return heading2;
+ }
+
+ private String extractChapterNumber(String fileName) {
+ // Extract chapter number from filename
+ String chapter = "";
+ for (int i = 0; i < fileName.length(); i++) {
+ if (Character.isDigit(fileName.charAt(i))) {
+ chapter = chapter + fileName.charAt(i);
+ }
+ else {
+ break;
+ }
+ }
+ return chapter;
+ }
+
+ private String handleHeaderLine(String line, String filename, int lineNum) {
+ String newHeadingLine = null;
+
+ // Check if the header contains an ID
+ if (headerIdPattern.matcher(line).matches()) {
+ // Extract the header ID
+ int idStartIndex = line.indexOf("=");
+ int idEndIndex = line.indexOf(")", idStartIndex);
+
+ String id = null;
+ if (idStartIndex > 0 && idEndIndex > (idStartIndex + 1)) {
+ id = line.substring(idStartIndex + 1, idEndIndex);
+ }
+
+ // Check if the ID is already in use
+ String filename2 = IDS.get(id);
+ if (filename2 != null) {
+ //print error
+
+ System.out.println("Dup id:" + id + " file:" +
+ filename + " line:" + lineNum + " (already used by file:" +
+ filename2 + ")");
+ return newHeadingLine;
+ }
+ else {
+ // Add the ID
+ IDS.put(id, filename);
+ newHeadingLine = line;
+ }
+ }
+ else {
+ // Generate an ID based on the header text, file chapter
+ // number, and counter
+
+ // Find the start of the header text
+ int indexOfFirstHeaderChar = -1;
+ for (int i = 0; i < line.length(); i++) {
+ char ch = line.charAt(i);
+ if (ch != '#' && ch != ' ' && ch != '\t') {
+ indexOfFirstHeaderChar = i;
+ break;
+ }
+ }
+
+ String heading = extractHeading(line, indexOfFirstHeaderChar);
+
+ String chapter = extractChapterNumber(filename);
+
+ // Note, length of heading
+ int headingLen = heading.length();
+
+ int idCount = 0;
+ String newHeading2 = null;
+ StringBuffer headingSb;
+ while (true) {
+
+ headingSb = new StringBuffer(heading);
+ headingSb.append("-");
+ headingSb.append(chapter);
+ headingSb.append("-");
+ headingSb.append(idCount);
+
+ int lenDiff = headingSb.length() - MAX_ID_LEN;
+ if (lenDiff > 0) {
+ // Trim heading
+ heading = heading.substring(0, headingLen - lenDiff);
+ }
+
+ headingSb = new StringBuffer(heading);
+ headingSb.append("-");
+ headingSb.append(chapter);
+ headingSb.append("-");
+ headingSb.append(idCount);
+
+ if (IDS.get(headingSb.toString()) == null) {
+ // Heading is unique
+
+ // Add the ID
+ IDS.put(headingSb.toString(), filename);
+
+ newHeading2 = headingSb.toString();
+ break;
+ }
+
+ idCount++;
+ }
+
+ newHeadingLine = line + " [](id=" + newHeading2 + ")";
+ }
+
+ return newHeadingLine;
+ }
+
+ public void setLang(String lang) {
+ this.lang = lang;
+ }
+ public void setDocDir(String docDir) {
+ this.docDir = docDir;
+ }
+
+ private static final int MAX_ID_LEN = 75;
+
+ private static HashMap<String, String> IDS = new HashMap<String, String>();
+
+ private static Pattern headerIdPattern;
+
+ static {
+ String patternArg = "(#)+([^\\\\\\[\\]\\|:;%<>]*)" +
+ Pattern.quote("[") + Pattern.quote("]") +
+ Pattern.quote("(") + "id" + Pattern.quote("=") +
+ "([^\\\\\\[\\]\\|:;%<>]+)" + Pattern.quote(")");
+
+ headerIdPattern = Pattern.compile(patternArg);
+ }
+
+ private String lang;
+ private String docDir;
+}
View
63 code/liferay-doc-utils/src/com/liferay/documentation/util/NumberImagesTask.java
@@ -0,0 +1,63 @@
+package com.liferay.documentation.util;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.util.regex.Pattern;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+
+public class NumberImagesTask extends Task {
+
+ @Override
+ public void execute() throws BuildException {
+ boolean foundDuplicateIds = false;
+
+ File chDir = new File("../" + docDir + "/" + lang + "/chapters");
+ System.out.println("Numbering images for files in " + chDir.getPath() +
+ " ...");
+
+ if (!chDir.exists() || !chDir.isDirectory()) {
+ throw new BuildException("FAILURE - bad chapters directory " + chDir);
+ }
+
+ // Get listing of markdown files
+ String[] files = chDir.list(new FilenameFilter() {
+ String filePatternArg = "([0-9]+)([^\\\\\\[\\]\\|:;%<>]+).markdown";
+ Pattern fileNamePattern = Pattern.compile(filePatternArg);
+
+ public boolean accept(File file, String name) {
+ return (fileNamePattern.matcher(name).matches());
+ }
+ });
+
+ if (files == null || files.length == 0) {
+ throw new BuildException("FAILURE - no markdown files found in " + chDir);
+ }
+
+ // Process each file
+
+ for (int i = 0; i < files.length; i++) {
+ String filename = files[i];
+ String filepath = "../" + docDir + "/" + lang + "/chapters/" +
+ filename;
+ try {
+ ResetImages.resetImages(filepath);
+ NumberImages.numberImages(filepath);
+ } catch (IOException e) {
+ throw new BuildException(e.getLocalizedMessage());
+ }
+ }
+ }
+
+ public void setLang(String lang) {
+ this.lang = lang;
+ }
+ public void setDocDir(String docDir) {
+ this.docDir = docDir;
+ }
+
+ private String lang;
+ private String docDir;
+}
View
BIN lib/liferay-doc-utils.jar
Binary file not shown.

0 comments on commit 3210836

Please sign in to comment.