-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Add tool to extract all code snippets from a document
- Loading branch information
1 parent
e24cd57
commit 48d9d9f
Showing
8 changed files
with
242 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright 2020 Florin Iucha | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package net.signbit.samx; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import org.apache.commons.cli.*; | ||
|
||
import net.signbit.samx.visitors.EmbeddedCodeVisitor; | ||
|
||
public class ExtractCode | ||
{ | ||
public static void main(String[] args) throws IOException | ||
{ | ||
Options options = new Options(); | ||
|
||
Option input = new Option("i", "input", true, "input file path"); | ||
input.setRequired(true); | ||
options.addOption(input); | ||
|
||
Option output = new Option("o", "output", true, "output file path"); | ||
output.setRequired(true); | ||
options.addOption(output); | ||
|
||
CommandLineParser cmdLine = new DefaultParser(); | ||
HelpFormatter helpFmt = new HelpFormatter(); | ||
|
||
try | ||
{ | ||
CommandLine cmd = cmdLine.parse(options, args); | ||
|
||
Parser.Result result = Parser.parse(cmd.getOptionValue("input")); | ||
if (result.errorCount > 0) | ||
{ | ||
System.err.print("Failed to parse input file " + cmd.getOptionValue("input")); | ||
System.exit(10); | ||
} | ||
|
||
File outputDir = null; | ||
if (cmd.getOptionValue("output") != null) | ||
{ | ||
outputDir = new File(cmd.getOptionValue("output")); | ||
if (! outputDir.isDirectory()) | ||
{ | ||
System.err.println("Specified output path " + cmd.getOptionValue("output") + " is not a directory."); | ||
System.exit(5); | ||
} | ||
} | ||
else | ||
{ | ||
outputDir = new File("."); | ||
} | ||
|
||
EmbeddedCodeVisitor visitor = new EmbeddedCodeVisitor(result.tokens, outputDir); | ||
|
||
visitor.visit(result.document); | ||
|
||
System.exit(0); | ||
} | ||
catch (ParseException pe) | ||
{ | ||
System.err.println(pe.getMessage()); | ||
helpFmt.printHelp("Renderer", options); | ||
} | ||
|
||
System.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/net/signbit/samx/visitors/EmbeddedCodeVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
Copyright 2020 Florin Iucha | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package net.signbit.samx.visitors; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
import org.antlr.v4.runtime.BufferedTokenStream; | ||
|
||
import net.signbit.samx.parser.SamXParser; | ||
import net.signbit.samx.parser.SamXParserBaseVisitor; | ||
|
||
public class EmbeddedCodeVisitor extends SamXParserBaseVisitor<StringBuilder> | ||
{ | ||
private final BufferedTokenStream tokenStream; | ||
private final File parentDir; | ||
|
||
public EmbeddedCodeVisitor(BufferedTokenStream tokenStream, File parentDir) | ||
{ | ||
this.tokenStream = tokenStream; | ||
this.parentDir = parentDir; | ||
} | ||
|
||
@Override | ||
public StringBuilder visitDocument(SamXParser.DocumentContext ctx) | ||
{ | ||
for (SamXParser.BlockContext bc : ctx.block()) | ||
{ | ||
visit(bc); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public StringBuilder visitTypedBlock(SamXParser.TypedBlockContext ctx) | ||
{ | ||
for (SamXParser.BlockContext bc : ctx.block()) | ||
{ | ||
visit(bc); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public StringBuilder visitCodeBlock(SamXParser.CodeBlockContext ctx) | ||
{ | ||
AttributeVisitor attributeVisitor = new AttributeVisitor(); | ||
attributeVisitor.visit(ctx.metadata()); | ||
|
||
final String fileStem = attributeVisitor.getId(); | ||
final String fileExtension = ctx.language.getText(); | ||
|
||
final File outputFile = new File(parentDir, fileStem + "." + fileExtension); | ||
|
||
try | ||
{ | ||
System.out.println("Writing " + outputFile.getCanonicalPath()); | ||
|
||
final FileWriter writer = new FileWriter(outputFile); | ||
|
||
/* | ||
* find the leftmost code line - that will become column 0 | ||
*/ | ||
int codeBlockIndent = 65536; | ||
for (SamXParser.ExternalCodeContext ecc : ctx.externalCode()) | ||
{ | ||
final int codeLineIndent = VisitorUtils.getTokenIndent(ecc, tokenStream); | ||
if (codeBlockIndent > codeLineIndent) | ||
{ | ||
codeBlockIndent = codeLineIndent; | ||
} | ||
} | ||
|
||
for (SamXParser.ExternalCodeContext ecc : ctx.externalCode()) | ||
{ | ||
final int codeLineIndent = VisitorUtils.getTokenIndent(ecc, tokenStream); | ||
|
||
for (int ii = codeBlockIndent; ii < codeLineIndent; ++ ii) | ||
{ | ||
writer.append(' '); | ||
} | ||
|
||
writer.append(ecc.EXTCODE().getText()); | ||
writer.append('\n'); | ||
} | ||
|
||
writer.close(); | ||
} | ||
catch (IOException ioe) | ||
{ | ||
System.err.println("Cannot open file " + outputFile.getAbsolutePath() + " for writing: " + ioe.getMessage()); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters