From 1b017daf5997bdfaff76796e2ef32bd4c58e9d7e Mon Sep 17 00:00:00 2001 From: hasksy Date: Sun, 20 Dec 2020 14:24:58 +0300 Subject: [PATCH] Added My Generator --- src/Generator/compileJar.sh | 10 ++ src/Generator/src/Main.java | 11 ++ src/Generator/src/ReadWriteFileUtils.java | 186 ++++++++++++++++++++++ src/Generator/src/SolutionLeetcode.java | 32 ++++ 4 files changed, 239 insertions(+) create mode 100644 src/Generator/compileJar.sh create mode 100644 src/Generator/src/Main.java create mode 100644 src/Generator/src/ReadWriteFileUtils.java create mode 100644 src/Generator/src/SolutionLeetcode.java diff --git a/src/Generator/compileJar.sh b/src/Generator/compileJar.sh new file mode 100644 index 0000000..0d734eb --- /dev/null +++ b/src/Generator/compileJar.sh @@ -0,0 +1,10 @@ +#!/bin/bash +javac -sourcepath ./src -d bin *.java +echo 'Main-Class: Generator.src.Main' > Generator/manifest.txt +jar cfm Generator/GenerateMD.jar Generator/manifest.txt Main.class SolutionLeetcode.class ReadWriteFileUtils.class +rm -rf manifest.txt Main.class SolutionLeetcode.class ReadWriteFileUtils.class +cd ..; +mv Generator/GenerateMD.jar leetcode +echo "GenerateMD.jar file was successfully compiled and moved to leetcode folder" + +#### Have to be refactored (Doesn't work now ) \ No newline at end of file diff --git a/src/Generator/src/Main.java b/src/Generator/src/Main.java new file mode 100644 index 0000000..867d2bc --- /dev/null +++ b/src/Generator/src/Main.java @@ -0,0 +1,11 @@ +package Generator.src; + +public class Main { + public static void main(String[] args) throws Exception { + if (args.length < 2) { + System.out.printf("Given not enough arguments. Required 2. Given %d\n", args.length); + return; + } + ReadWriteFileUtils.txtToMarkdown(args[0], args[1]); + } +} \ No newline at end of file diff --git a/src/Generator/src/ReadWriteFileUtils.java b/src/Generator/src/ReadWriteFileUtils.java new file mode 100644 index 0000000..1ec0834 --- /dev/null +++ b/src/Generator/src/ReadWriteFileUtils.java @@ -0,0 +1,186 @@ +package Generator.src; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ReadWriteFileUtils { + + private static boolean isNotCreated(File file) { + if (file.exists()) { + return false; + } + System.out.printf("ERROR. File: %s is not located in this directory on doesn't exists\n", file.getName()); + return true; + } + + private static boolean isReadable(File file) { + if (!file.canRead()) { + System.out.printf("ERROR. File: %s cannot be read\n", file.getName()); + return false; + } + return true; + } + + private static boolean isEmpty(File file) { return file == null || file.length() == 0; } + + private static boolean isURL(String s) { + return s.startsWith("https://"); + } + + private static String removeLastChar(String s) { + return (s == null || s.length() == 0) ? null : (s.substring(0, s.length() - 1)); + } + + private static List parseListString(List file) { + List updatedFile = new ArrayList<>(); + for (String line : file) { + if (line != null && line.length() != 0 && !line.equals("\n")) { + while (line.endsWith(" ")) { + line = removeLastChar(line); + } + updatedFile.add(line); + } + } + return updatedFile; + } + + private static String[] listSolution(String s) { + return s.split("\n"); + } + + private static ArrayList splitTasks(File file) throws IOException { + ArrayList split = new ArrayList<>(); + BufferedReader splitter = new BufferedReader(new FileReader(file)); + int symbol = splitter.read(); + int openBrackets = 0; + int closeBrackets = 0; + StringBuilder Solution = new StringBuilder(); + while (symbol != -1) { + Solution.append((char) symbol); + if (symbol == (int) '{') { + openBrackets++; + } else if (symbol == (int) '}') closeBrackets++; + if (openBrackets != 0) { + if (openBrackets == closeBrackets) { + openBrackets = 0; + closeBrackets = 0; + split.add(Solution.toString()); + Solution = new StringBuilder(); + } + } + symbol = splitter.read(); + } + splitter.close(); + return split; + + } + + private static SolutionLeetcode setToSolutionLeetcodeClass(List Solution) { + SolutionLeetcode cl = new SolutionLeetcode(); + String title = null; + String link; + StringBuilder sourceCode = new StringBuilder(); + for (String s : Solution) { + if (title == null) { + title = s; + cl.setTitle(title); + } else if (isURL(s)) { + link = s; + cl.setLink(link); + } else sourceCode.append(s).append('\n'); + + } + cl.setSourceCode(sourceCode.toString()); + return cl; + } + + private static String getEndOfLink(String link) { + return link.substring(30, link.length() - 1); + // https://leetcode.com/problems/non-overlapping-intervals/ + // "https://leetcode.com/problems/".length() = 31 + } + + private static char toUpperCase(char c) { + return (char) ((c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c); + } + + private static String setMainTitle(File file) { + String MainTitle = file.getName(); + return toUpperCase(MainTitle.charAt(0)) + MainTitle.substring(1, MainTitle.length() - 3); + } + + private static void initEmptyMd(SolutionLeetcode cl, File markdown) throws IOException { + BufferedWriter writer = new BufferedWriter(new PrintWriter(markdown)); + writer.write("# " + setMainTitle(markdown) + "\n"); + writer.write("+ [" + cl.getTitle() + "](#" + getEndOfLink(cl.getLink()) + ")\n"); //+ [Non-overlapping Intervals](#non-overlapping-intervals) + writer.write("## " + cl.getTitle() + "\n"); + writer.write(cl.getLink() + "\n"); + writer.write("```java\n"); + writer.write(cl.getSourceCode()); + writer.write("```\n"); + writer.close(); + } + + private static int getEndOfTitleLinks(File markdown) throws IOException { + List lines = Files.readAllLines(Path.of(markdown.getPath())); + String output = null; + for (String s : lines) { + if (output == null) { + if (s.startsWith("+ [")) { + output = s; + } + } else { + if (s.startsWith("+ [")) { + output = s; + } else break; + } + } + return lines.indexOf(output); + + } + + private static void appendToMd(SolutionLeetcode cl, File markdown) throws IOException { + List lines = Files.readAllLines(Path.of(markdown.getPath())); + lines.add(getEndOfTitleLinks(markdown) + 1, "+ [" + cl.getTitle() + "](#" + getEndOfLink(cl.getLink()) + ")"); + lines.add("## " + cl.getTitle()); + lines.add(cl.getLink()); + lines.add("```java"); + lines.add(cl.getSourceCode()); + lines.add("```"); + lines = parseListString(lines); + BufferedWriter writer = new BufferedWriter(new FileWriter(markdown)); + for (String line : lines) { + writer.write(line + '\n'); + } + writer.close(); + } + + public static void txtToMarkdown(String txtPath, String markdownPath) throws IOException { + File txtSolution = new File(txtPath); + ArrayList SolutionClasses = new ArrayList<>(); + if (isNotCreated(txtSolution)) return; + if (isReadable(txtSolution)) { + ArrayList splitTasks = splitTasks(txtSolution); + for (String s : splitTasks) { + List txt = Arrays.asList(listSolution(s)); + txt = parseListString(txt); + SolutionClasses.add(setToSolutionLeetcodeClass(txt)); + } + File markdown = new File(markdownPath); + if (!markdown.exists()) markdown.createNewFile(); + if (isReadable(markdown)) { + for (SolutionLeetcode cl : SolutionClasses) { + if (isEmpty(markdown)) { + initEmptyMd(cl, markdown); + } else { + appendToMd(cl, markdown); + } + } + } + } + } +} diff --git a/src/Generator/src/SolutionLeetcode.java b/src/Generator/src/SolutionLeetcode.java new file mode 100644 index 0000000..82ce8d4 --- /dev/null +++ b/src/Generator/src/SolutionLeetcode.java @@ -0,0 +1,32 @@ +package Generator.src; + +public class SolutionLeetcode { + private String title = null; + private String link = null; + private String sourceCode = null; + + + String getTitle() { + return (this.title == null || this.title.length() == 0) ? null : this.title; + } + + String getLink() { + return (this.link == null || this.link.length() == 0) ? null : this.link; + } + + String getSourceCode() { + return (this.sourceCode == null || this.sourceCode.length() == 0) ? null : this.sourceCode; + } + + final public void setTitle(String title) { + this.title = title; + } + + final public void setLink(String link) { + this.link = link; + } + + final public void setSourceCode(String sourceCode) { + this.sourceCode = sourceCode; + } +} \ No newline at end of file