Upload files to "java/md2html"
This commit is contained in:
82
java/md2html/Md2Html.java
Normal file
82
java/md2html/Md2Html.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package md2html;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import markup.*;
|
||||
|
||||
public class Md2Html {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String text = readFile(args[0]);
|
||||
BlockCreator creator = new BlockCreator(text);
|
||||
List<String> blocks = creator.divideByBlocks();
|
||||
|
||||
try (
|
||||
BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(
|
||||
new FileOutputStream(args[1]),
|
||||
StandardCharsets.UTF_8
|
||||
)
|
||||
)
|
||||
) {
|
||||
for (int i = 0; i < blocks.size(); i++) {
|
||||
PrimePartCreator creatorPrime = new PrimePartCreator(
|
||||
blocks.get(i)
|
||||
);
|
||||
PrimePart part = creatorPrime.createPart();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
part.toHtml(sb);
|
||||
writer.write(sb.toString());
|
||||
if (i < blocks.size() - 1) {
|
||||
writer.write(System.lineSeparator());
|
||||
}
|
||||
sb.setLength(0); // освобождаем память
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("IOException: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String readFile(String nameOfFile) {
|
||||
try (
|
||||
BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
new FileInputStream(nameOfFile),
|
||||
StandardCharsets.UTF_8
|
||||
)
|
||||
)
|
||||
) {
|
||||
StringBuilder text = new StringBuilder();
|
||||
int read = reader.read();
|
||||
while (read != -1) {
|
||||
text.append((char) read);
|
||||
read = reader.read();
|
||||
}
|
||||
return text.toString();
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Input file not found: " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
System.out.println("IOException file not found: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void writeToFile(String nameOfFile, String text) {
|
||||
try (
|
||||
BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(
|
||||
new FileOutputStream(nameOfFile),
|
||||
StandardCharsets.UTF_8
|
||||
)
|
||||
)
|
||||
) {
|
||||
writer.write(text);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Output file not found: " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
System.out.println("IOException file not found: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user