Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

234: Command Line Interface #235

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ out/

# Other
*.pyc

#Eclipse
/bin/

#Gradle
.gradle
build/

10 changes: 10 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ concurrent accesses, since neither the [PegDownProcessor] nor the underlying par

See <http://sirthias.github.com/pegdown/api> for the pegdown API documentation.

Command Line Interface
----------------------

Running _pegdown_ from the command line is pretty easy as well. Download the `-fat` jar file from the link given in the Installation instructions. And run:

`> java -jar pegdown-<version>-fat.jar`

The command line options will be printed to the console.



Plugins
-------
Expand Down
104 changes: 104 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

sourceCompatibility = 1.6
version = '1.6'
group = 'org.pegdown'


jar {
manifest {
attributes 'Implementation-Title': 'PegDown',
'Implementation-Version': version
}
}

repositories {
mavenCentral()
jcenter()
maven {
url "https://repo1.maven.org/maven2"
}

}



dependencies {
compile (
[group: 'org.parboiled', name: 'parboiled-java', version: '1.1.7']
)
testCompile (
[group: 'net.sf.jtidy', name: 'jtidy', version: 'r938'],
[group: 'org.specs2', name: 'specs2-core_2.11', version: '2.4.16']
)
}



task sourceJar(type: Jar) {
classifier "sources"
from sourceSets.main.allSource
}

task docJar(type: Jar) {
dependsOn javadoc
classifier "javadoc"
from javadoc.destinationDir
}

task allJar(type: Jar){
dependsOn sourceJar, docJar, jar
classifier "all"
from sourceSets.main.allSource, javadoc.destinationDir, sourceSets.main.output
}

//create a single Jar with all dependencies
task fatJar(type: Jar) {
group = "java"
dependsOn classes
classifier "fat"
baseName = project.name + '-fat'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}


publishing {
publications {
maven(MavenPublication) {
from components.java
artifact sourceJar
artifact docJar
artifact allJar
artifact fatJar

pom.withXml{
Node n=asNode().appendNode("scm")
n.appendNode("url","[email protected]:sirthias/pegdown.git")
n.appendNode("connection","scm:git:[email protected]:sirthias/pegdown.git")
//developers
n=asNode().appendNode("developers")
n=n.appendNode("developer")
n.appendNode("id","sirthias")
n.appendNode("name","Mathias Doenitz")
//licences
Node lic=asNode().appendNode("licenses")
n=lic.appendNode("license")
n.appendNode("name","Apache License 2.0")
n.appendNode("url","http://www.apache.org/licenses/LICENSE-2.0")
n.appendNode("distribution","repo")
n.appendNode("comments","Applies to all PegDown products")
//fat jar
n=lic.appendNode("license")
n.appendNode("name","OW2 License")
n.appendNode("url","http://asm.ow2.org/license.html")
n.appendNode("distribution","repo")
n.appendNode("comments","The -fat library repacks dependent libraries. This additional license applies only to specific parts of the product.")

}
}
}
}

137 changes: 137 additions & 0 deletions src/main/java/org/pegdown/cli/PegDownCLI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package org.pegdown.cli;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.util.List;

import org.pegdown.Extensions;
import org.pegdown.PegDownProcessor;

/**
* This class is the command line interface for PegDown. It simply converts one input file to a html output file.
*
* @author Jan Ortner
*
*/
public class PegDownCLI {

/**
* The main method parses the arguments, reads the input file, transforms the content via a PegDownProcessor
* to a html string and writes this string to the output file.
* @param args the command line arguments (see {@link #printUsage()})
*/
public static void main(String[] args) {
Path input=null;
Path output=null;
int extensions=Extensions.ALL;
//parsing arguments
for(int i=0; i<args.length;i++){
System.out.println(args[i]);
if(args[i].indexOf("+E")==0){ //add extension
//System.out.println("add: "+args[i].substring(2, args[i].length())+" mask: "+Integer.toBinaryString(getExtensionMask(args[i].substring(2, args[i].length()))));
extensions|=getExtensionMask(args[i].substring(2, args[i].length()));
}else if(args[i].indexOf("-E")==0){ //remove extension
//System.out.println("remove: "+args[i].substring(2, args[i].length())+" mask: "+Integer.toBinaryString(getExtensionMask(args[i].substring(2, args[i].length()))));
extensions&=~getExtensionMask(args[i].substring(2, args[i].length()));
}else if(input==null){ //inputfile
input=Paths.get(args[i]);
}else if(output==null){ //outputfile
output=Paths.get(args[i]);
}
}
if(input==null||!input.toFile().exists()){
if(input==null)
System.err.println("*** No input file given ***");
if(!input.toFile().exists())
System.err.println("*** File does not exist: "+input+" ***");
printUsage();
System.exit(1);
}
if(output==null){
String fileName=input.getFileName().toString();
fileName=fileName.substring(0, fileName.indexOf('.'))+".html";
if(input.getParent()!=null)
output=Paths.get(input.getParent().toString(), fileName);
else
output=Paths.get(fileName);
}
System.out.println("Reading file: "+input);
System.out.println("Writing output to: "+output);
System.out.println("ExtensionMask: "+Integer.toBinaryString(extensions));
// do conversion
try {
List<String> inLines=Files.readAllLines(input);
String in="";
for(String line:inLines){
in+=line+"\n";
}
PegDownProcessor proc=new PegDownProcessor(extensions);
String out=proc.markdownToHtml(in);
try {
if(output.getParent()!=null)
Files.createDirectory(output.getParent(), (FileAttribute<?>[]) null);
Files.write(output, out.getBytes(), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE );
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Gets the mask of the given extension. It reads the static variable with the given name from
* {@link Extensions} and returns the value of the field.
* @param extension the extension
* @return the mask or 0 if the extension was not found
*/
public static int getExtensionMask(String extension){
try{
Field f=Extensions.class.getField(extension);
if((f.getModifiers()&Modifier.STATIC)!=0
&& f.getType().equals(int.class)
){
return f.getInt(null);
}

}catch(Exception e){
e.printStackTrace();
}
return 0;
}

/**
* Prints the usage to the standard output stream.
*/
public static void printUsage(){
String usage="Usage: \n";
usage+="java -jar <jar-name> org.pegdown.cli.PegDownCLI [+/-E\"extension\"] <inputfile> [ouputfile]\n";
usage+="\ninputfile:\n";
usage+="The input file needs to be in markdownformat\n";
usage+="\noutputfile:\n";
usage+="The output file will be in html format\n";
usage+="\nExtensions:\n";
usage+="+ or - will select if an extension should be added or removed.\n";
usage+="This is done in the order given by the arguments, initially ALL extensions are enabled.\n";
usage+="Example:\n";
usage+="-EALL - will remove all default extensions\n";
usage+="Built-in extensions:\n";
Field[] extensions=Extensions.class.getDeclaredFields();
for(int i=0; i<extensions.length;i++){
//basic sanity checks
if((extensions[i].getModifiers()&Modifier.STATIC)!=0
&& extensions[i].getType().equals(int.class)
){
usage+="\t"+extensions[i].getName()+"\n";
}
}
System.out.println(usage);
}

}