Skip to content

Commit

Permalink
[Core] Allow specifying which code blocks to be extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8000-0000 committed Apr 26, 2020
1 parent 48d9d9f commit 7b086d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/main/java/net/signbit/samx/ExtractCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static void main(String[] args) throws IOException
output.setRequired(true);
options.addOption(output);

Option elements = new Option("e", "elements", true, "selected elements");
elements.setRequired(false);
elements.setArgs(Option.UNLIMITED_VALUES);
options.addOption(elements);

CommandLineParser cmdLine = new DefaultParser();
HelpFormatter helpFmt = new HelpFormatter();

Expand Down Expand Up @@ -66,7 +71,7 @@ public static void main(String[] args) throws IOException
outputDir = new File(".");
}

EmbeddedCodeVisitor visitor = new EmbeddedCodeVisitor(result.tokens, outputDir);
EmbeddedCodeVisitor visitor = new EmbeddedCodeVisitor(result.tokens, outputDir, cmd.getOptionValues("elements"));

visitor.visit(result.document);

Expand Down
18 changes: 17 additions & 1 deletion src/main/java/net/signbit/samx/visitors/EmbeddedCodeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;

import org.antlr.v4.runtime.BufferedTokenStream;

Expand All @@ -29,11 +31,18 @@ public class EmbeddedCodeVisitor extends SamXParserBaseVisitor<StringBuilder>
{
private final BufferedTokenStream tokenStream;
private final File parentDir;
private final HashSet<String> elements;

public EmbeddedCodeVisitor(BufferedTokenStream tokenStream, File parentDir)

public EmbeddedCodeVisitor(BufferedTokenStream tokenStream, File parentDir, String[] elements)
{
this.tokenStream = tokenStream;
this.parentDir = parentDir;
this.elements = new HashSet<>();
if (elements != null)
{
this.elements.addAll(Arrays.asList(elements));
}
}

@Override
Expand Down Expand Up @@ -65,8 +74,15 @@ public StringBuilder visitCodeBlock(SamXParser.CodeBlockContext ctx)
attributeVisitor.visit(ctx.metadata());

final String fileStem = attributeVisitor.getId();

final String fileExtension = ctx.language.getText();

if ((!elements.isEmpty()) && (!elements.contains(fileStem)))
{
System.out.println(String.format("Skipping unselected element %s (%s)", fileStem, fileExtension));
return null;
}

final File outputFile = new File(parentDir, fileStem + "." + fileExtension);

try
Expand Down

0 comments on commit 7b086d3

Please sign in to comment.