From e6d515fc9641a4af7f0653da1925f2be45c18a67 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Thu, 8 Sep 2011 12:30:09 -0500 Subject: [PATCH] Version 0.2: Adds `relative-path-root` option. * `relative-path-root` option added. This facilitates situations where the current directory of the invocation context is different than the working directory of the program. This is required to use `jlp` with tools like *Nailgun*, which keeps a persistant `java` process running and proxies new invocations to the existing process. --- jlp | 2 +- project.properties | 6 ++-- src/main/com/jdblabs/jlp/JLPMain.groovy | 41 ++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/jlp b/jlp index 5b0198c..6976c71 100755 --- a/jlp +++ b/jlp @@ -1 +1 @@ -ng com.jdblabs.jlp.JLPMain "$@" +ng com.jdblabs.jlp.JLPMain --relative-path-root "`pwd`" "$@" diff --git a/project.properties b/project.properties index dbec21b..53e6b18 100644 --- a/project.properties +++ b/project.properties @@ -1,6 +1,6 @@ -#Tue, 06 Sep 2011 17:17:39 -0500 +#Thu, 08 Sep 2011 12:27:26 -0500 name=jlp -version=0.1 -build.number=2 +version=0.2 +build.number=1 lib.local=true release.dir=release diff --git a/src/main/com/jdblabs/jlp/JLPMain.groovy b/src/main/com/jdblabs/jlp/JLPMain.groovy index 8f69855..2e2626c 100644 --- a/src/main/com/jdblabs/jlp/JLPMain.groovy +++ b/src/main/com/jdblabs/jlp/JLPMain.groovy @@ -21,6 +21,8 @@ public class JLPMain { cli.h('Print this help information.', longOpt: 'help', required: false) cli.o("Output directory (defaults to 'jlp-docs').", longOpt: 'output-dir', required: false) + cli._(longOpt: 'relative-path-root', args: 1, required: false, + 'Resolve all relative paths against this root.') // parse options def opts = cli.parse(args) @@ -30,8 +32,22 @@ public class JLPMain { cli.usage() return } + // get the relative path root (or set to current directory if not given) + def pathRoot = new File(opts."relative-path-root" ?: ".") + + // fail if our root is non-existant + if (!pathRoot.exists() || !pathRoot.isDirectory()) { + System.err.println "'${pathRoot.path}' is not a valid directory." + System.exit(1) } + // get the output directory and create it if necessary def outputDir = opts.o ? new File(opts.o) : new File("jlp-docs") + + // resolve the output directory against our relative root + if (!outputDir.isAbsolute()) { + outputDir = new File(pathRoot, outputDir.path) } + + // create the output directory if it does not exist if (!outputDir.exists()) outputDir.mkdirs() // get the CSS theme to use @@ -41,24 +57,39 @@ public class JLPMain { // get files passed in def filenames = opts.getArgs() - // -------- parse input -------- // + // parse input Map parsedFiles = filenames.inject([:]) { acc, filename -> - acc[filename] = inst.parse(new File(filename)) + + // create the File object + File file = new File(filename) + + // if this is a relative path, resolve it against our root path + if (!file.isAbsolute()) { file = new File(pathRoot, filename) } + + // parse the file, store the result + acc[filename] = inst.parse(file) return acc } - // -------- generate output -------- // + // generate output Map htmlDocs = LiterateMarkdownGenerator.generateDocuments(parsedFiles) - // -------- write output files ------- // + // write output files htmlDocs.each { filename, html -> + // split the path into parts def fileParts = filename.split(/[\.\/]/) - File subDir = new File('.') + // default the subdirectory to the output directory + File subDir = outputDir + // if the input file was in a subdirectory, we want to mirror that + // structure here. if (fileParts.length > 2) { + // find the relative subdirectory of this file subDir = new File(outputDir, fileParts[0..-3].join('/')) + + // create that directory if needed if (!subDir.exists()) subDir.mkdirs() }