Skip to content

Commit

Permalink
feat: flags for TextDiffToTikz + better defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
pmbittner committed Oct 14, 2024
1 parent 060495f commit 402139a
Showing 1 changed file with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextDiffToTikz {
public record TextDiffToTikz(boolean collapseMultipleCodeLines, boolean ignoreEmptyLines) {
public static String[] UNICODE_PROP_SYMBOLS = new String[]{"¬", "∧", "∨", "⇒", "⇔"};

/**
Expand All @@ -40,37 +43,49 @@ public static void main(String[] args) throws IOException, DiffParseException {
return;
}

final Path fileToConvert = Path.of(args[0]);
if (!Files.exists(fileToConvert)) {
Logger.error("Path {} does not exist!", fileToConvert);
return;
}

final GraphvizExporter.LayoutAlgorithm layout;
if (args.length < 2) {
layout = GraphvizExporter.LayoutAlgorithm.DOT;
} else {
layout = GraphvizExporter.LayoutAlgorithm.valueOf(args[1].toUpperCase());
}

final Path fileToConvert = Path.of(args[0]);
if (!Files.exists(fileToConvert)) {
Logger.error("Path {} does not exist!", fileToConvert);
return;
// FIXME: Use a dedicated argument parser in the future.
boolean collapseMultipleCodeLines = false;
boolean ignoreEmptyLines = true;
List<String> flags = new ArrayList<>(Arrays.asList(args).subList(2, args.length));
if (flags.contains("-c") || flags.contains("--collapse-blocks")) {
collapseMultipleCodeLines = true;
}
if (flags.contains("-e") || flags.contains("--with-empty-lines")) {
ignoreEmptyLines = false;
}

TextDiffToTikz me = new TextDiffToTikz(collapseMultipleCodeLines, ignoreEmptyLines);
if (Files.isDirectory(fileToConvert)) {
Logger.info("Processing directory " + fileToConvert);
for (Path file : FileUtils.listAllFilesRecursively(fileToConvert)) {
if (FileUtils.hasExtension(file, ".diff")) {
textDiff2Tikz(file, layout);
me.textDiff2Tikz(file, layout);
}
}
} else {
textDiff2Tikz(fileToConvert, layout);
me.textDiff2Tikz(fileToConvert, layout);
}
}

public static void textDiff2Tikz(Path fileToConvert, GraphvizExporter.LayoutAlgorithm layout) throws IOException, DiffParseException {
public void textDiff2Tikz(Path fileToConvert, GraphvizExporter.LayoutAlgorithm layout) throws IOException, DiffParseException {
Logger.info("Converting file " + fileToConvert);
Logger.info("Using layout " + layout.getExecutableName());
final Path targetFile = fileToConvert.resolveSibling(fileToConvert.getFileName() + ".tikz");

final VariationDiff<DiffLinesLabel> d = VariationDiff.fromFile(fileToConvert, new VariationDiffParseOptions(true, true));
final VariationDiff<DiffLinesLabel> d = VariationDiff.fromFile(fileToConvert, new VariationDiffParseOptions(collapseMultipleCodeLines, ignoreEmptyLines));
final String tikz = exportAsTikz(d, layout);
IO.write(targetFile, tikz);
Logger.info("Wrote file " + targetFile);
Expand All @@ -79,7 +94,7 @@ public static void textDiff2Tikz(Path fileToConvert, GraphvizExporter.LayoutAlgo
public static String exportAsTikz(final VariationDiff<DiffLinesLabel> variationDiff, GraphvizExporter.LayoutAlgorithm layout) throws IOException {
// Export the test case
var tikzOutput = new ByteArrayOutputStream();
new TikzExporter<DiffLinesLabel>(format).exportVariationDiff(variationDiff, layout, tikzOutput);
new TikzExporter<>(format).exportVariationDiff(variationDiff, layout, tikzOutput);
return tikzOutput.toString();
}

Expand Down

0 comments on commit 402139a

Please sign in to comment.