|
1 | 1 | package org.jmarkdownviewer.jmdviewer; |
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.BufferedWriter; |
3 | 5 | import java.io.File; |
| 6 | +import java.io.FileNotFoundException; |
| 7 | +import java.io.FileReader; |
| 8 | +import java.io.FileWriter; |
| 9 | +import java.io.IOException; |
4 | 10 |
|
5 | 11 | import javax.swing.SwingUtilities; |
6 | 12 |
|
|
11 | 17 | import org.apache.commons.cli.Option; |
12 | 18 | import org.apache.commons.cli.Options; |
13 | 19 | import org.apache.commons.cli.ParseException; |
| 20 | +import org.jmarkdownviewer.jmdviewer.parser.MarkdownParser; |
14 | 21 |
|
| 22 | +public class App { |
15 | 23 |
|
16 | | -public class App implements Runnable { |
17 | | - |
18 | 24 | String lastdir; |
19 | 25 | String startfile = null; |
20 | | - |
| 26 | + |
21 | 27 | private static App m_instance; |
22 | | - |
| 28 | + |
23 | 29 | private App() { |
24 | | - |
25 | | - //lastdir = System.getProperty("user.home"); |
| 30 | + // lastdir = System.getProperty("user.home"); |
26 | 31 | lastdir = System.getProperty("user.dir"); |
27 | | - |
28 | | - } |
29 | | - |
30 | | - @Override |
31 | | - public void run() { |
32 | | - MainFrame m = new MainFrame(); |
33 | | - m.pack(); |
34 | | - m.setLocationRelativeTo(null); |
35 | | - m.setVisible(true); |
36 | | - if (startfile != null) { |
37 | | - File file = new File(startfile); |
38 | | - if(file.exists()) |
39 | | - m.openfile(file); |
40 | | - } |
41 | 32 | } |
42 | 33 |
|
43 | | - |
44 | 34 | public static App getInstance() { |
45 | | - if(m_instance == null) |
| 35 | + if (m_instance == null) |
46 | 36 | m_instance = new App(); |
47 | 37 | return m_instance; |
48 | 38 | } |
49 | | - |
50 | | - |
51 | | - public static void main( String[] args ) |
52 | | - { |
53 | | - App app = getInstance(); |
54 | | - Options options = new Options(); |
55 | | - Option help = new Option("h", "help", false, "help"); |
56 | | - options.addOption(help); |
57 | | - |
58 | | - CommandLine line; |
59 | | - CommandLineParser parser = new DefaultParser(); |
60 | | - try { |
61 | | - line = parser.parse( options, args); |
62 | | - if(line.hasOption("help")) { |
63 | | - HelpFormatter formatter = new HelpFormatter(); |
64 | | - formatter.printHelp("java -jar jmdviewer.jar [filename.md]", options); |
65 | | - System.exit(0); |
66 | | - } |
67 | | - args = line.getArgs(); |
68 | | - if(args != null && args.length > 0) { |
69 | | - app.setStartfile(args[0]); |
70 | | - } |
71 | | - } |
72 | | - catch( ParseException exp ) { |
73 | | - // oops, something went wrong |
74 | | - System.err.println( "Parsing failed. Reason: " + exp.getMessage() ); |
75 | | - HelpFormatter formatter = new HelpFormatter(); |
76 | | - formatter.printHelp("java -jar jmdviewer.jar [filename.md]", options); |
77 | | - System.exit(1); |
78 | | - } |
79 | | - |
80 | | - SwingUtilities.invokeLater(app); |
81 | | - } |
| 39 | + |
| 40 | + private void run(String[] args) { |
| 41 | + parseargs(args); |
| 42 | + SwingUtilities.invokeLater(new Runnable() { |
| 43 | + @Override |
| 44 | + public void run() { |
| 45 | + MainFrame m = new MainFrame(); |
| 46 | + m.pack(); |
| 47 | + m.setLocationRelativeTo(null); |
| 48 | + m.setVisible(true); |
| 49 | + if (startfile != null) { |
| 50 | + File file = new File(startfile); |
| 51 | + if (file.exists()) |
| 52 | + m.openfile(file); |
| 53 | + } |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + private void parseargs(String[] args) { |
| 60 | + Options options = new Options(); |
| 61 | + Option help = new Option("h", "help", false, "help"); |
| 62 | + options.addOption(help); |
| 63 | + options.addOption( |
| 64 | + Option.builder().longOpt("html").desc("convert to html").build()); |
| 65 | + options.addOption( |
| 66 | + Option.builder().longOpt("text").desc("convert to txt").build()); |
| 67 | + options.addOption( |
| 68 | + Option.builder("o").longOpt("out").hasArg().argName("filename") |
| 69 | + .desc("output to file").build()); |
| 70 | + |
| 71 | + CommandLine line; |
| 72 | + CommandLineParser parser = new DefaultParser(); |
| 73 | + try { |
| 74 | + line = parser.parse(options, args); |
| 75 | + if (line.hasOption("help")) { |
| 76 | + HelpFormatter formatter = new HelpFormatter(); |
| 77 | + formatter.printHelp("java -jar jmdviewer.jar [filename.md]", options); |
| 78 | + System.exit(0); |
| 79 | + } |
| 80 | + |
| 81 | + if (line.hasOption("html")) { |
| 82 | + if (line.getArgs().length < 1) { |
| 83 | + System.err.println("filename required for html opt"); |
| 84 | + System.exit(1); |
| 85 | + } |
| 86 | + String filename = line.getArgs()[0]; |
| 87 | + String out = dotohtml(filename); |
| 88 | + if (out != null) { |
| 89 | + if(line.hasOption("out")) { |
| 90 | + String outfilename = line.getOptionValue("out"); |
| 91 | + savefile(outfilename, out); |
| 92 | + System.out.println("saved in " + outfilename); |
| 93 | + } else |
| 94 | + System.out.println(out); |
| 95 | + } |
| 96 | + System.exit(0); |
| 97 | + } |
| 98 | + |
| 99 | + if (line.hasOption("text")) { |
| 100 | + if (line.getArgs().length < 1) { |
| 101 | + System.err.println("filename required for text opt"); |
| 102 | + System.exit(1); |
| 103 | + } |
| 104 | + String filename = line.getArgs()[0]; |
| 105 | + String out = dototext(filename); |
| 106 | + if (out != null) { |
| 107 | + if(line.hasOption("out")) { |
| 108 | + String outfilename = line.getOptionValue("out"); |
| 109 | + savefile(outfilename, out); |
| 110 | + System.out.println("saved in " + outfilename); |
| 111 | + } else |
| 112 | + System.out.println(out); |
| 113 | + } |
| 114 | + System.exit(0); |
| 115 | + } |
| 116 | + |
| 117 | + args = line.getArgs(); |
| 118 | + if (args != null && args.length > 0) { |
| 119 | + setStartfile(args[0]); |
| 120 | + } |
| 121 | + |
| 122 | + } catch (ParseException exp) { |
| 123 | + // oops, something went wrong |
| 124 | + System.err.println("Parsing failed. Reason: " + exp.getMessage()); |
| 125 | + HelpFormatter formatter = new HelpFormatter(); |
| 126 | + formatter.printHelp("java -jar jmdviewer.jar [filename.md]", options); |
| 127 | + System.exit(1); |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + public static void main(String[] args) { |
| 134 | + App app = getInstance(); |
| 135 | + app.run(args); |
| 136 | + } |
| 137 | + |
| 138 | + private String dotohtml(String filename) { |
| 139 | + MarkdownParser parser = new MarkdownParser(); |
| 140 | + String rtxt = loadfile(filename); |
| 141 | + if(rtxt == null) return null; |
| 142 | + parser.parse(rtxt); |
| 143 | + return parser.getHTML(); |
| 144 | + } |
| 145 | + |
| 146 | + private String dototext(String filename) { |
| 147 | + MarkdownParser parser = new MarkdownParser(); |
| 148 | + String rtxt = loadfile(filename); |
| 149 | + if(rtxt == null) return null; |
| 150 | + parser.parse(rtxt); |
| 151 | + return parser.getText(); |
| 152 | + } |
| 153 | + |
| 154 | + public String loadfile(String filename) { |
| 155 | + StringBuilder sb = new StringBuilder(100); |
| 156 | + try { |
| 157 | + BufferedReader reader = new BufferedReader(new FileReader(filename)); |
| 158 | + String line; |
| 159 | + while ((line = reader.readLine()) != null) { |
| 160 | + sb.append(line); |
| 161 | + sb.append("\n"); |
| 162 | + } |
| 163 | + reader.close(); |
| 164 | + } catch (FileNotFoundException e) { |
| 165 | + e.printStackTrace(); |
| 166 | + return null; |
| 167 | + } catch (IOException e) { |
| 168 | + e.printStackTrace(); |
| 169 | + return null; |
| 170 | + } |
| 171 | + return sb.toString(); |
| 172 | + } |
| 173 | + |
| 174 | + public void savefile(String filename, String text) { |
| 175 | + try { |
| 176 | + BufferedWriter writer = new BufferedWriter(new FileWriter(filename)); |
| 177 | + writer.write(text); |
| 178 | + writer.flush(); |
| 179 | + writer.close(); |
| 180 | + } catch (IOException e) { |
| 181 | + e.printStackTrace(); |
| 182 | + } |
| 183 | + } |
82 | 184 |
|
83 | 185 | public String getLastdir() { |
84 | 186 | return lastdir; |
|
0 commit comments