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

#18: Adding Support for --config/-c Flag #19

Merged
merged 5 commits into from
Oct 8, 2021
Merged
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
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"java.project.referencedLibraries": [
"lib/**/*.jar",
"json-simple-1.1.1.jar"
]
}
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ argument option "-h" or "--help" will display the list of command options

argument option "-i" or "--i" will allow user input the single file or files by passing the directory. It supports both .txt and .md (feature: Heading, Bold)

argument option "-c" or "--config" will allow users to specify a JSON config file that contains input and optional output values, such as:

```bash
{
"input": "docs",
"output": "web"
}
```

----
## Prerequisites
Expand All @@ -27,12 +35,13 @@ java
2. run .jar file using java -jar with the argument
```bash
java -jar out\artifacts\ssgApplication_jar\ssgApplication.jar -i <txtFileName>
java -jar out\artifacts\ssgApplication_jar\ssgApplication.jar -c <configFileName>
```

----
## Output

You can find the generated outputs at \dist
You can find the generated outputs at \dist or the custom directory specified in the config file

----
## Demo
Expand All @@ -46,6 +55,7 @@ java -jar out\artifacts\ssgApplication_jar\ssgApplication.jar -h <or --help>
---------------------------------------
* -v OR --version - current version *
* -i OR --input - input file or files *
* -c OR --config - JSON config file *
---------------------------------------
```

Expand All @@ -58,6 +68,15 @@ java -jar out\artifacts\ssgApplication_jar\ssgApplication.jar -i sources\Sherloc
>The Red Headed League.html has been created in dist!
```

```bash
java -jar out\artifacts\ssgApplication_jar\ssgApplication.jar -c config.json
>Silver Blaze.html has been created in web!
>The Adventure of the Six Napoleans.html has been created in web!
>The Adventure of the Speckled Band.html has been created in web!
>The Naval Treaty.html has been created in web!
>The Red Headed League.html has been created in web!
```

----
##License
MIT
Expand Down
Binary file added json-simple-1.1.1.jar
Binary file not shown.
Binary file modified out/artifacts/ssgApplication_jar/ssgApplication.jar
Binary file not shown.
112 changes: 74 additions & 38 deletions src/com/company/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class Main {

Expand All @@ -20,6 +21,7 @@ public static void main(String[] args) {
System.out.println("---------------------------------------");
System.out.println("* -v OR --version - current version *");
System.out.println("* -i OR --input - input file or files *");
System.out.println("* -c OR --config - JSON config file *");
System.out.println("---------------------------------------");

} else if(args[0].equals("--input") || args[0].equals("-i")){
Expand All @@ -30,53 +32,87 @@ public static void main(String[] args) {
fileNameFull[i-1] = args[i];
}

String inputFile = fileNameReader(fileNameFull);
if(inputFile.endsWith(".txt")){
//if input file is only one file
File file = new File(inputFile);
String fileName = file.getName();
//create html
createHtml(file);
processInput(fileNameReader(fileNameFull), "dist");

}
else if(inputFile.endsWith(".md")){
try{
//if input file is only one file
File file = new File(inputFile);
//create html
MDUtils.createHTMLFromMd(file);
}
catch(Exception ex){
ex.printStackTrace();
} else if(args[0].equals("--config") || args[0].equals("-c")) {

//parses JSON file for input and output values
JSONParser parser = new JSONParser();

try {
Object configObject = parser.parse(new FileReader(args[1]));
JSONObject configJsonObject = (JSONObject) configObject;

String input = "";
String output = "";

if ((String) configJsonObject.get("input") == null) {
throw new Exception("Invalid input file/folder passed. Please pass a valid input");
} else {
input = (String) configJsonObject.get("input");
}

}
else {
//if user added multiple files(directory)
try {
List<File> allFiles = Files.walk(Paths.get(inputFile))
.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());
List<String> fileNames = new ArrayList<>() ;
for(File file :allFiles){
createHtml(file);
}

} catch (IOException e) {
e.printStackTrace();
if ((String) configJsonObject.get("output") == null) {
output = "dist";
} else {
output = (String) configJsonObject.get("output");
}

processInput(input, output);

} catch (FileNotFoundException ex) {
System.out.println("The config file could not be found");
} catch (Exception ex) {
System.out.println(ex.getMessage() == null ? "The config file could not be parsed" : ex.getMessage());
}



}else {
} else {
System.out.println("Invalid argument passed. Please pass a valid argument");
}


}

private static void processInput (String inputFile, String output) {
if(inputFile.endsWith(".txt")){
//if input file is only one file
File file = new File(inputFile);
String fileName = file.getName();
//create html
createHtml(file, output);

}
else if(inputFile.endsWith(".md")){
try{
//if input file is only one file
File file = new File(inputFile);
//create html
MDUtils.createHTMLFromMd(file);
}
catch(Exception ex){
ex.printStackTrace();
}

}
else {
//if user added multiple files(directory)
try {
List<File> allFiles = Files.walk(Paths.get(inputFile))
.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());
List<String> fileNames = new ArrayList<>() ;
for(File file :allFiles){
createHtml(file, output);
}

} catch (IOException e) {
e.printStackTrace();
}
}
}

private static String fileNameReader(String[] str) {
//convert string array to string with the space
Expand All @@ -87,7 +123,7 @@ private static String fileNameReader(String[] str) {
return s.substring(0,s.length() -1);
}

private static void createHtml(File fileName){
private static void createHtml(File fileName, String output){
try{

//read file
Expand All @@ -102,7 +138,7 @@ private static void createHtml(File fileName){
String[] getOnlyName = fName.split("\\\\");
String name = getOnlyName[getOnlyName.length-1];

String htmlFileName = "dist/" + name + ".html";
String htmlFileName = output + "/" + name + ".html";

//pre-formed html forms
String beginParagraph = "<p>";
Expand Down Expand Up @@ -130,7 +166,7 @@ private static void createHtml(File fileName){
+ "";

//create directory if not exist
new File("dist").mkdir();
new File(output).mkdir();

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFileName),"UTF-8"));
writer.write(htmlFormB);
Expand All @@ -153,7 +189,7 @@ private static void createHtml(File fileName){
writer.close();

//for logging
System.out.println(name + ".html has been created in dist!");
System.out.println(name + ".html has been created in " + output + "!");

}catch(IOException e) {
e.printStackTrace();
Expand Down