-
Notifications
You must be signed in to change notification settings - Fork 0
/
MudLog.java
executable file
·414 lines (355 loc) · 10.1 KB
/
MudLog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/** see ../../../../../../../LICENSE for release rights */
package ws.nzen.pdistillery.md;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import com.esotericsoftware.yamlbeans.YamlConfig;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.esotericsoftware.yamlbeans.YamlWriter;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
/** Changes relevant yaml with markdown to html yaml */
public class MudLog
{
private static final String cl = "o.";
static final String mdConfigFlag = "m", yamlConfigFlag = "y",
filesToProcessFlag = "f", verboseFlag = "v",
outputFolderPath = "o", helpFlag = "h";
static final String pluginKey = "pd_left", selfAsPlugin = "mud-log";
protected boolean verbose;
protected Path markdownConfig = null;
protected Path yamlConfig = null;
protected Path outputFolder = null;
protected MutableDataSet mdSettings = null;
protected YamlConfig docConfig = null;
/** read configs, interpret markdown, copy to output folder */
public static void main( String[] args )
{
/*
180114 next:
configure markdown
refactor translateMo()
https://github.com/vsch/flexmark-java/tree/master/flexmark-java-samples/src/com/vladsch/flexmark/samples
*/
CommandLine userInput = prepCli( prepCliParser(), args );
MudLog doesStuff = prepDoer( userInput );
if ( userInput != null )
{
doesStuff.translateMarkdownOf( userInput
.getOptionValues( filesToProcessFlag ) );
}
}
/** fills options with our cli flags and text */
public static Options prepCliParser()
{
Options knowsCliDtd = new Options();
final boolean needsEmbelishment = true;
knowsCliDtd.addOption( mdConfigFlag, needsEmbelishment, "markdown config path"
+ " (ex C:\\Program Files\\apache\\tomcat.txt)" );
knowsCliDtd.addOption( yamlConfigFlag, needsEmbelishment, "yaml config path"
+ " (ex /home/theusername/tmp/ff.json)" );
knowsCliDtd.addOption( outputFolderPath, needsEmbelishment,
"path of folder to write results" );
knowsCliDtd.addOption( filesToProcessFlag, needsEmbelishment,
"paths of files to process" );
knowsCliDtd.addOption( verboseFlag, ! needsEmbelishment,
"show debug information" );
knowsCliDtd.addOption( helpFlag, "show arg flags" );
final int numberOfFilesToAcceptFromFolder
= org.apache.commons.cli.Option.UNLIMITED_VALUES;
knowsCliDtd.getOption( filesToProcessFlag )
.setArgs( numberOfFilesToAcceptFromFolder );
return knowsCliDtd;
}
/** Parses the actual input and shows help, if requested */
public static CommandLine prepCli(
Options knowsCliDtd, String[] args )
{
CommandLineParser cliRegex = new DefaultParser();
CommandLine userInput = null;
try
{
userInput = cliRegex.parse( knowsCliDtd, args );
if ( userInput.hasOption( helpFlag ) )
{
new HelpFormatter().printHelp( "PD-MudLog", knowsCliDtd );
}
}
catch ( ParseException pe )
{
System.err.println( cl +"pc just using config: couldn't parse input "+ pe );
}
return userInput;
}
public static MudLog prepDoer( CommandLine userInput )
{
MudLog doesStuff;
if ( userInput != null && userInput.hasOption( verboseFlag ) )
{
boolean wantsLogging = true;
doesStuff = new MudLog( wantsLogging );
}
else
{
doesStuff = new MudLog();
}
if ( userInput != null )
{
final String currentDir = ( File.pathSeparator.equals( ":" ) )
? "." : ""; // NOTE windows / linux current dir
if ( userInput.hasOption( mdConfigFlag ) )
{
doesStuff.setMarkdownPath( userInput
.getOptionValue( mdConfigFlag, currentDir ) );
}
if ( userInput.hasOption( yamlConfigFlag ) )
{
doesStuff.setYamlPath( userInput
.getOptionValue( yamlConfigFlag, currentDir ) );
}
doesStuff.setOutputPath( userInput
.getOptionValue( outputFolderPath, currentDir ) );
}
return doesStuff;
}
public MudLog()
{
this( false );
}
public MudLog( boolean noiseTolerance )
{
verbose = noiseTolerance;
}
// IMPROVE these are all the same, but it feels wasteful to make an enum just for this
public void setMarkdownPath( String path )
{
final String here = cl +"smp ";
try
{
Path place = Paths.get( path );
if ( place.toFile().isFile() )
{
markdownConfig = place;
}
else if ( verbose )
{
System.err.println( here +"md config must be a file" );
}
}
catch ( InvalidPathException ipe )
{
System.err.println( here +"invalid md path "+ ipe );
}
}
public void setYamlPath( String path )
{
final String here = cl +"smp ";
try
{
Path place = Paths.get( path );
if ( place.toFile().isFile() )
{
yamlConfig = place;
}
else if ( verbose )
{
System.err.println( here +"yaml config must be a file" );
}
}
catch ( InvalidPathException ipe )
{
System.err.println( here +"invalid yaml path "+ ipe );
}
}
public void setOutputPath( String path )
{
final String here = cl +"smp ";
try
{
Path place = Paths.get( path );
if ( place.toFile().isDirectory() )
{
outputFolder = place;
}
else if ( verbose )
{
System.err.println( here +"output must be a folder" );
}
}
catch ( InvalidPathException ipe )
{
System.err.println( here +"invalid out dir "+ ipe );
}
}
/** cleanup after config changes */
public void adoptConfiguration()
{
System.out.println( cl +"ad didnt reify config yet" );
if ( docConfig == null )
{
docConfig = new YamlConfig();
}
// configure yaml thing from actual config
docConfig.writeConfig.setExplicitFirstDocument( true );
docConfig.writeConfig.setExplicitEndDocument( true );
if ( mdSettings == null )
{
mdSettings = new MutableDataSet();
}
// configure md thing from actual config
}
/** translate the md of the yaml files specified if appropriate */
public void translateMarkdownOf( String[] paths )
{
final String here = cl +"tmo ";
if ( paths != null )
{
adoptConfiguration();
YamlReader loadsInfo;
YamlWriter burysInfo;
Parser knowMdInput = Parser.builder( mdSettings ).build();
HtmlRenderer knowsHmtlOutput = HtmlRenderer.builder( mdSettings ).build();
for ( String path : paths )
{
System.out.println( cl +"tmo trans "+ path );
try ( FileReader charLoader = new FileReader( path );
FileWriter charDump = new FileWriter(
outputFolder.toString() + File.separator
+ Paths.get( path ).getFileName() ) )
{
if ( verbose )
{
System.out.println( here +"new file is "
+ outputFolder.toString() + File.separator
+ Paths.get( path ).getFileName() );
}
loadsInfo = new YamlReader( charLoader );
burysInfo = new YamlWriter( charDump );
Object document;
Map docAttributes;
// assert doc 1 is a map, doc 2 is a scalar literal
document = loadsInfo.read();
if ( document == null || ! (document instanceof Map) )
{
loadsInfo.close();
copyFileWithoutChange(new FileReader( path ), charDump);
continue;
}
else
{
if ( verbose )
{
System.out.println( document );
}
docAttributes = (Map)document;
if ( docAttributes.containsKey( pluginKey ) )
{
List plugins = (List)(docAttributes.get( pluginKey ));
// IMPROVE handle class cast ex
int indOfSelf = plugins.indexOf( selfAsPlugin );
if ( indOfSelf >= 0 )
{
plugins.remove( indOfSelf );
docAttributes.put( pluginKey, plugins );
burysInfo.write( document );
}
else
{
loadsInfo.close();
copyFileWithoutChange(new FileReader( path ), charDump);
continue;
}
}
else
{
loadsInfo.close();
copyFileWithoutChange(new FileReader( path ), charDump);
continue;
}
}
document = loadsInfo.read();
if ( document == null )
{
loadsInfo.close();
burysInfo.close();
continue;
}
else if ( ! (document instanceof String) )
{
do
{
document = loadsInfo.read();
burysInfo.write( document );
}
while ( document != null );
loadsInfo.close();
burysInfo.close();
continue;
}
else
{
// this is our markdown
String content = (String)document;
content = knowsHmtlOutput.render(
knowMdInput.parse(content) );
burysInfo.write( content );
}
document = loadsInfo.read();
if ( document == null )
{
loadsInfo.close();
burysInfo.close();
continue;
}
else
{
do
{
document = loadsInfo.read();
burysInfo.write( document );
}
while ( document != null );
loadsInfo.close();
burysInfo.close();
continue;
}
}
catch ( FileNotFoundException | InvalidPathException ie )
{
System.err.println( here +"invalid path "+ ie );
}
catch ( IOException ie )
{
System.err.println( here +"file i/o problem "+ ie );
}
}
}
}
protected void copyFileWithoutChange( FileReader original,
FileWriter duplicate ) throws IOException
{
int maybeByte;
maybeByte = original.read();
while ( maybeByte != -1 )
{
duplicate.write( maybeByte );
maybeByte = original.read();
}
}
}