1818import java .io .IOException ;
1919import java .nio .file .Files ;
2020import java .nio .file .Path ;
21+ import java .util .ArrayList ;
22+ import java .util .Arrays ;
23+ import java .util .List ;
2124import java .util .regex .Matcher ;
2225import java .util .regex .Pattern ;
2326
24- public class TextDiffToTikz {
27+ public record TextDiffToTikz ( boolean collapseMultipleCodeLines , boolean ignoreEmptyLines ) {
2528 public static String [] UNICODE_PROP_SYMBOLS = new String []{"¬" , "∧" , "∨" , "⇒" , "⇔" };
2629
2730 /**
@@ -40,37 +43,49 @@ public static void main(String[] args) throws IOException, DiffParseException {
4043 return ;
4144 }
4245
46+ final Path fileToConvert = Path .of (args [0 ]);
47+ if (!Files .exists (fileToConvert )) {
48+ Logger .error ("Path {} does not exist!" , fileToConvert );
49+ return ;
50+ }
51+
4352 final GraphvizExporter .LayoutAlgorithm layout ;
4453 if (args .length < 2 ) {
4554 layout = GraphvizExporter .LayoutAlgorithm .DOT ;
4655 } else {
4756 layout = GraphvizExporter .LayoutAlgorithm .valueOf (args [1 ].toUpperCase ());
4857 }
4958
50- final Path fileToConvert = Path .of (args [0 ]);
51- if (!Files .exists (fileToConvert )) {
52- Logger .error ("Path {} does not exist!" , fileToConvert );
53- return ;
59+ // FIXME: Use a dedicated argument parser in the future.
60+ boolean collapseMultipleCodeLines = false ;
61+ boolean ignoreEmptyLines = true ;
62+ List <String > flags = new ArrayList <>(Arrays .asList (args ).subList (2 , args .length ));
63+ if (flags .contains ("-c" ) || flags .contains ("--collapse-blocks" )) {
64+ collapseMultipleCodeLines = true ;
65+ }
66+ if (flags .contains ("-e" ) || flags .contains ("--with-empty-lines" )) {
67+ ignoreEmptyLines = false ;
5468 }
5569
70+ TextDiffToTikz me = new TextDiffToTikz (collapseMultipleCodeLines , ignoreEmptyLines );
5671 if (Files .isDirectory (fileToConvert )) {
5772 Logger .info ("Processing directory " + fileToConvert );
5873 for (Path file : FileUtils .listAllFilesRecursively (fileToConvert )) {
5974 if (FileUtils .hasExtension (file , ".diff" )) {
60- textDiff2Tikz (file , layout );
75+ me . textDiff2Tikz (file , layout );
6176 }
6277 }
6378 } else {
64- textDiff2Tikz (fileToConvert , layout );
79+ me . textDiff2Tikz (fileToConvert , layout );
6580 }
6681 }
6782
68- public static void textDiff2Tikz (Path fileToConvert , GraphvizExporter .LayoutAlgorithm layout ) throws IOException , DiffParseException {
83+ public void textDiff2Tikz (Path fileToConvert , GraphvizExporter .LayoutAlgorithm layout ) throws IOException , DiffParseException {
6984 Logger .info ("Converting file " + fileToConvert );
7085 Logger .info ("Using layout " + layout .getExecutableName ());
7186 final Path targetFile = fileToConvert .resolveSibling (fileToConvert .getFileName () + ".tikz" );
7287
73- final VariationDiff <DiffLinesLabel > d = VariationDiff .fromFile (fileToConvert , new VariationDiffParseOptions (true , true ));
88+ final VariationDiff <DiffLinesLabel > d = VariationDiff .fromFile (fileToConvert , new VariationDiffParseOptions (collapseMultipleCodeLines , ignoreEmptyLines ));
7489 final String tikz = exportAsTikz (d , layout );
7590 IO .write (targetFile , tikz );
7691 Logger .info ("Wrote file " + targetFile );
@@ -79,7 +94,7 @@ public static void textDiff2Tikz(Path fileToConvert, GraphvizExporter.LayoutAlgo
7994 public static String exportAsTikz (final VariationDiff <DiffLinesLabel > variationDiff , GraphvizExporter .LayoutAlgorithm layout ) throws IOException {
8095 // Export the test case
8196 var tikzOutput = new ByteArrayOutputStream ();
82- new TikzExporter <DiffLinesLabel >(format ).exportVariationDiff (variationDiff , layout , tikzOutput );
97+ new TikzExporter <>(format ).exportVariationDiff (variationDiff , layout , tikzOutput );
8398 return tikzOutput .toString ();
8499 }
85100
0 commit comments