org.asciidoctor.extension.PreprocessorReader Java Examples

The following examples show how to use org.asciidoctor.extension.PreprocessorReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClasspathIncludeProcessor.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void process(Document document,
                    PreprocessorReader reader,
                    String target,
                    Map<String, Object> attributes) {
    List<String> content = readContent(target);
    for (int i = content.size() - 1; i >= 0; i--) {
        String line = content.get(i);
        // See also https://github.com/asciidoctor/asciidoctorj/issues/437#issuecomment-192669617
        // Seems to be a hack to avoid mangling of paragraphes
        if (line.trim().equals("")) {
            line = " ";
        }
        reader.push_include(line, target, target, 1, attributes);
    }
}
 
Example #2
Source File: LsIncludeProcessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Override
public void process(Document document,                    // <3>
                    PreprocessorReader reader,
                    String target,
                    Map<String, Object> attributes) {

    StringBuilder sb = new StringBuilder();

    for (File f: new File(".").listFiles()) {
        sb.append(f.getName()).append("\n");
    }

    reader.push_include(                                  // <4>
            sb.toString(),
            target,
            new File(".").getAbsolutePath(),
            1,
            attributes);
}
 
Example #3
Source File: CommentPreprocessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Override
public void process(Document document, PreprocessorReader reader) {

    List<String> lines = reader.readLines();          // <2>
    List<String> newLines = new ArrayList<String>();

    boolean inComment = false;

    for (String line: lines) {                        // <3>
        if (line.trim().equals("////")) {
            if (!inComment) {
               newLines.add("[NOTE]");
            }
            newLines.add("--");
            inComment = !inComment;
        } else {
            newLines.add(line);
        }
    }

    reader.restoreLines(newLines);                    // <4>
}
 
Example #4
Source File: IncludePreprocessor.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
@Override
public void process(Document doc, PreprocessorReader reader) {
    OutputType outputType = OutputType.match(doc.getOptions().get("preprocessOutputType"));
    if (outputType == null) {
        return;
    }
    List<String> processedContent = markIncludes(reader, outputType, doc.getAttributes());
    savePreincludedDocIfRequested(processedContent, doc);
}
 
Example #5
Source File: IncludeProcessorProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@JRubyMethod(name = "process", required = 4)
public IRubyObject process(ThreadContext context, IRubyObject[] args) {
    Document document = (Document) NodeConverter.createASTNode(args[0]);
    PreprocessorReader reader = new PreprocessorReaderImpl(args[1]);
    String target = RubyUtils.rubyToJava(getRuntime(), args[2], String.class);
    Map<String, Object> attributes = new RubyAttributesMapDecorator((RubyHash) args[3]);
    getProcessor().process(document, reader, target, attributes);
    return null;
}
 
Example #6
Source File: IncludePreprocessor.java    From helidon-build-tools with Apache License 2.0 4 votes vote down vote up
private List<String> markIncludes(
        PreprocessorReader reader,
        OutputType outputType,
        Map<String, Object> attributes) {

    /*
     * Use the raw input. Do not use reader.readLines() yet because that
     * would trigger {@code include::} handling which needs to happen below.
     */
    List<String> origWithBracketedIncludes = convertHybridToBracketed(reader.lines());

    /*
     * Replace the reader's content with the bracketed format.
     */
    readAndClearReader(reader);
    String origWithBracketedIncludesContent = linesToString(origWithBracketedIncludes);
    reader.push_include(origWithBracketedIncludesContent, null, null, 1, attributes);

    /*
     * Have the reader consume the bracketed-include content which will
     * insert the included text between the bracketing comments.
     */
    List<String> bracketedIncludesWithIncludedText = readAndClearReader(reader);

    /*
     * With the actual included text between the bracketing comments convert
     * that to the numbered format which is ultimate what we want.
     */
    List<String> numberedIncludesWithIncludedText
            = convertBracketedToNumbered(bracketedIncludesWithIncludedText);

    /*
     * Prepare the reader to consume the numbered format just computed.
     */
    String numberedIncludesWithIncludedTextContent = linesToString(numberedIncludesWithIncludedText);
    reader.push_include(
            numberedIncludesWithIncludedTextContent,
            null,
            null,
            1,
            attributes);

    switch (outputType) {
        case PREPROCESSED:
            return numberedIncludesWithIncludedText;

        case NATURAL:
            return convertBracketedToNatural(bracketedIncludesWithIncludedText);

        default:
            throw new IllegalArgumentException(
                    String.format("outputType %s is not one of %s",
                        outputType == null ? "null" : outputType.toString().toLowerCase(),
                        Arrays.toString(OutputType.values())));
    }
}
 
Example #7
Source File: IncludePreprocessor.java    From helidon-build-tools with Apache License 2.0 4 votes vote down vote up
private static List<String> readAndClearReader(PreprocessorReader reader) {
    List<String> result = reader.readLines();
    reader.restoreLines(Collections.emptyList());
    return result;
}
 
Example #8
Source File: AsciidoctorPreprocessTask.java    From aeron with Apache License 2.0 4 votes vote down vote up
@TaskAction
public void preprocess() throws Exception
{
    if (!target.exists() && !target.mkdirs())
    {
        throw new IOException("unable to create build directory");
    }

    final File[] asciidocFiles = AsciidocUtil.filterAsciidocFiles(source);

    System.out.println("Transforming from: " + source);
    System.out.println("Found files: " + Arrays.stream(asciidocFiles).map(File::getName).collect(joining(", ")));

    final Map<File, Integer> errors = new HashMap<>();

    for (final File asciidocFile : asciidocFiles)
    {
        final File outputFile = new File(target, asciidocFile.getName());

        final Asciidoctor asciidoctor = Asciidoctor.Factory.create();

        final int[] errorCount = { 0 };

        asciidoctor.registerLogHandler(
            (logRecord) ->
            {
                if (logRecord.getSeverity() == Severity.ERROR || logRecord.getSeverity() == Severity.FATAL)
                {
                    errorCount[0]++;
                }
            });

        final HashMap<String, Object> attributes = new HashMap<>();
        attributes.put("sampleBaseDir", requireNonNull(sampleBaseDir, "Must specify sampleBaseDir"));
        attributes.put("sampleSourceDir", requireNonNull(sampleSourceDir, "Must specify sampleSourceDir"));

        final HashMap<String, Object> options = new HashMap<>();
        options.put("attributes", attributes);
        options.put("safe", org.asciidoctor.SafeMode.UNSAFE.getLevel());

        try (PrintStream output = new PrintStream(outputFile))
        {
            asciidoctor.javaExtensionRegistry().preprocessor(
                new org.asciidoctor.extension.Preprocessor()
                {
                    public void process(final Document document, final PreprocessorReader reader)
                    {
                        String line;
                        while (null != (line = reader.readLine()))
                        {
                            if (line.startsWith(":aeronVersion:"))
                            {
                                output.println(":aeronVersion: " + versionText);
                            }
                            else
                            {
                                output.println(line);
                            }
                        }
                    }
                });

            asciidoctor.loadFile(asciidocFile, options);

            if (0 < errorCount[0])
            {
                errors.put(asciidocFile, errorCount[0]);
            }
        }
    }

    errors.forEach((key, value) -> System.out.println("file: " + key + ", error count: " + value));

    if (0 < errors.size())
    {
        throw new Exception("failed due to errors in parsing");
    }
}
 
Example #9
Source File: FailingPreprocessor.java    From asciidoctor-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void process(Document document, PreprocessorReader reader) {
    System.out.println("Processing "+ this.getClass().getSimpleName());
    System.out.println("Processing: blocks found: " + document.getBlocks().size());
    throw new RuntimeException("That's all folks");
}
 
Example #10
Source File: ChangeAttributeValuePreprocessor.java    From asciidoctor-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void process(Document document, PreprocessorReader reader) {
    System.out.println("Processing "+ this.getClass().getSimpleName());
    System.out.println("Processing: blocks found: " + document.getBlocks().size());
    document.getAttributes().put("author", AUTHOR_NAME);
}
 
Example #11
Source File: UriIncludeProcessor.java    From asciidoctor-maven-plugin with Apache License 2.0 3 votes vote down vote up
@Override
public void process(Document document, PreprocessorReader reader, String target,
                    Map<String, Object> attributes) {

    System.out.println("Processing "+ this.getClass().getSimpleName());

    StringBuilder content = readContent(target);
    reader.push_include(content.toString(), target, target, 1, attributes);

}