/**
* Copyright 2011 Oliver Buchtala
*
* This file is part of ndogen.
*
* ndogen is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ndogen is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ndogen. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ndogen.watch;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.ndogen.converter.Markdown2Html;
import org.ndogen.watch.task.ContentProvider;
import org.ndogen.watch.task.Template;
import org.ndogen.watch.task.WriteFile;
public class Run {
enum FileFormat {
Markdown,
Html
}
/**
* @param args
*/
public static void main(String[] args) {
if(args.length == 0) {
displayHelp();
System.exit(0);
}
String _inputFile;
boolean writeOutputFile = false;
String _outputFile = "";
FileFormat inputFormat = FileFormat.Markdown;
FileFormat outputFormat = FileFormat.Html;
boolean useTemplate = false;
String templateFile = "";
for (int i = 0; i < args.length-1; i++) {
if(args[i].equals("-o")) {
i++;
_outputFile = args[i];
writeOutputFile = true;
} else if(args[i].equals("--to")) {
i++;
String toFormat = args[i];
if(toFormat.equals("html")) {
outputFormat = FileFormat.Html;
} else {
error("Unsupported output format: " + toFormat);
}
_outputFile = args[i];
writeOutputFile = true;
} else if(args[i].equals("--template")) {
i++;
useTemplate = true;
templateFile = args[i];
if(!fileExists(templateFile)) {
error("Could not find temlpate file: " + templateFile);
}
}
}
_inputFile = args[args.length-1];
File inputFile = new File(_inputFile);
if(!inputFile.exists()) {
error("File does not exist: " + inputFile);
}
// TODO: later also handle other modes
// writing to file is going to be default
if(!writeOutputFile) {
String parent = inputFile.getParent();
String name = inputFile.getName() + ".html";
_outputFile = parent + File.separator + name;
}
ContentProvider input = null;
if(inputFormat == FileFormat.Markdown && outputFormat == FileFormat.Html) {
input = new Markdown2Html(new FileContentProvider(_inputFile));
} else {
error("Sorry. Unsupported mode: " + inputFormat.toString() + " -> " + outputFormat.toString());
}
if(useTemplate) {
input = new Template(input, templateFile);
}
Runnable processor = null;
if(writeOutputFile) {
processor = new WriteFile(new File(_outputFile), input);
} else {
error("Sorry. Unsupported mode.");
}
FileWatcher watcher = new FileWatcher(_inputFile, processor);
new Thread(watcher).start();
}
public static void displayHelp() {
StringWriter sw = new StringWriter();
PrintWriter w = new PrintWriter(sw);
w.println("java -jar ndogen.guard <OPTIONS> <FILENAME>");
w.println();
w.println("Options:");
w.println(" -o: output file");
w.println(" --from: input format");
w.println(" --to: output format");
System.out.println(sw.toString());
}
public static boolean fileExists(String file) {
return new File(file).exists();
}
public static void error(String msg) {
System.err.println(msg);
displayHelp();
System.exit(0);
}
}