Skip to content

Commit

Permalink
Change API of Preprocessor to return a Reader.
Browse files Browse the repository at this point in the history
Add a Processor method to create a new Reader from a list of source lines.
  • Loading branch information
robertpanzer committed Jul 23, 2022
1 parent d761556 commit 63b04de
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
package org.asciidoctor.extension;

import org.asciidoctor.Options;
import org.asciidoctor.ast.Block;
import org.asciidoctor.ast.Cell;
import org.asciidoctor.ast.Column;
import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.DescriptionList;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.ListItem;
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.Row;
import org.asciidoctor.ast.Section;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.ast.Table;
import org.asciidoctor.ast.*;
import org.asciidoctor.log.LogRecord;

import java.util.HashMap;
Expand All @@ -23,7 +12,7 @@

public class BaseProcessor implements Processor {

private static ProcessorFactory processorFactory;
private static final ProcessorFactory processorFactory;

static {
Iterator<ProcessorFactory> iterator = ServiceLoader.load(ProcessorFactory.class).iterator();
Expand All @@ -33,7 +22,7 @@ public class BaseProcessor implements Processor {
processorFactory = iterator.next();
}

private Processor delegate;
private final Processor delegate;

public BaseProcessor() {
this(new HashMap<>());
Expand Down Expand Up @@ -154,6 +143,41 @@ public void parseContent(StructuralNode parent, List<String> lines) {
delegate.parseContent(parent, lines);
}

@Override
public Cursor newCursor(String file) {
return delegate.newCursor(file);
}

@Override
public Cursor newCursor(String file, String dir) {
return delegate.newCursor(file, dir);
}

@Override
public Cursor newCursor(String file, String dir, int lineno) {
return delegate.newCursor(file, dir, lineno);
}

@Override
public Reader newReader(List<String> source, Cursor cursor, Map<Object, Object> options) {
return delegate.newReader(source, cursor, options);
}

@Override
public Reader newReader(List<String> source, Cursor cursor) {
return delegate.newReader(source, cursor);
}

@Override
public Reader newReader(List<String> source, Map<Object, Object> options) {
return delegate.newReader(source, options);
}

@Override
public Reader newReader(List<String> source) {
return delegate.newReader(source);
}

// REMIND: Could be default method but Groovy (with compileStatic does not resolve default methods)

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public Preprocessor(Map<String, Object> config) {
super(config);
}

public abstract void process(Document document, PreprocessorReader reader);
public abstract Reader process(Document document, PreprocessorReader reader);

This comment has been minimized.

Copy link
@verhas

verhas Jun 16, 2023

This breaks the compatibility with the existing extensions, and an extension cannot implement both preprocessors to be compatible with both versions. To amend this on the preprocessor side it need some serious module build structure and run-time hacking.


}
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.Block;
import org.asciidoctor.ast.Cell;
import org.asciidoctor.ast.Column;
import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.ListItem;
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.Row;
import org.asciidoctor.ast.Section;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.ast.Table;
import org.asciidoctor.ast.*;
import org.asciidoctor.log.LogRecord;

import java.util.List;
Expand Down Expand Up @@ -144,6 +134,20 @@ public interface Processor {
*/
void parseContent(StructuralNode parent, List<String> lines);

Cursor newCursor(String file);

Cursor newCursor(String file, String dir);

Cursor newCursor(String file, String dir, int lineno);

Reader newReader(List<String> source, Cursor cursor, Map<Object, Object> options);

Reader newReader(List<String> source, Cursor cursor);

Reader newReader(List<String> source, Map<Object, Object> options);

Reader newReader(List<String> source);

Map<String, Object> getConfig();

void log(LogRecord logRecord);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
package org.asciidoctor.jruby.extension.internal;

import org.asciidoctor.Options;
import org.asciidoctor.ast.Block;
import org.asciidoctor.ast.Cell;
import org.asciidoctor.ast.Column;
import org.asciidoctor.ast.ContentModel;
import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.ListItem;
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.Row;
import org.asciidoctor.ast.Section;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.ast.Table;
import org.asciidoctor.ast.*;
import org.asciidoctor.extension.Processor;
import org.asciidoctor.extension.Reader;
import org.asciidoctor.jruby.ast.impl.ColumnImpl;
import org.asciidoctor.jruby.ast.impl.ContentNodeImpl;
import org.asciidoctor.jruby.ast.impl.DescriptionListImpl;
import org.asciidoctor.jruby.ast.impl.DocumentImpl;
import org.asciidoctor.jruby.ast.impl.ListImpl;
import org.asciidoctor.jruby.ast.impl.NodeConverter;
import org.asciidoctor.jruby.ast.impl.RowImpl;
import org.asciidoctor.jruby.ast.impl.StructuralNodeImpl;
import org.asciidoctor.jruby.internal.JRubyAsciidoctor;
import org.asciidoctor.jruby.internal.JRubyRuntimeContext;
import org.asciidoctor.jruby.internal.RubyHashUtil;
import org.asciidoctor.jruby.internal.RubyObjectWrapper;
import org.asciidoctor.jruby.internal.RubyUtils;
import org.asciidoctor.jruby.ast.impl.*;
import org.asciidoctor.jruby.internal.*;
import org.asciidoctor.log.LogRecord;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyFixnum;
import org.jruby.RubyHash;
import org.jruby.RubySymbol;
import org.jruby.*;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.ArrayList;
Expand Down Expand Up @@ -376,6 +350,60 @@ public void parseContent(StructuralNode parent, List<String> lines) {
}
}

public Cursor newCursor(String file) {
return newCursor(file, null);
}

public Cursor newCursor(String file, String dir) {
return newCursor(file, dir, 1);
}

public Cursor newCursor(String file, String dir, int lineNo) {
Ruby runtime = JRubyRuntimeContext.get(asciidoctor);
RubyClass klazz = runtime.getModule("Asciidoctor").getClass("Reader").getClass("Cursor");
IRubyObject rubyCursor = klazz.newInstance(runtime.getCurrentContext(), new IRubyObject[]{
runtime.newString(file),
dir == null ? runtime.getNil() : runtime.newString(dir),
runtime.newFixnum(lineNo),
}, org.jruby.runtime.Block.NULL_BLOCK);
return new CursorImpl(rubyCursor);
}

@Override
public Reader newReader(List<String> source, Cursor cursor) {
return newReader(source, cursor, null);
}

@Override
public Reader newReader(List<String> source, Map<Object, Object> options) {
return newReader(source, null, options);
}

@Override
public Reader newReader(List<String> source) {
return newReader(source, null, null);
}

@Override
public Reader newReader(List<String> source, Cursor cursor, Map<Object, Object> options) {
Ruby runtime = JRubyRuntimeContext.get(asciidoctor);
RubyClass klazz = runtime.getModule("Asciidoctor").getClass("Reader");

RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbolsIfNecessary(runtime, options);

RubyArray data = runtime.newArray();
for (String line : source) {
data.add(runtime.newString(line));
}

IRubyObject rubyReader = klazz.newInstance(runtime.getCurrentContext(), new IRubyObject[]{
data,
cursor == null ? runtime.getNil() : ((RubyObjectWrapper) cursor).getRubyObject(),
convertMapToRubyHashWithSymbols
}, org.jruby.runtime.Block.NULL_BLOCK);
return new ReaderImpl(rubyReader);
}

private class Parser extends RubyObjectWrapper {

private final Reader reader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.Preprocessor;
import org.asciidoctor.extension.Reader;
import org.asciidoctor.jruby.ast.impl.NodeConverter;
import org.asciidoctor.jruby.extension.internal.PreprocessorReaderImpl;
import org.asciidoctor.jruby.internal.JRubyAsciidoctor;
import org.asciidoctor.jruby.internal.RubyHashMapDecorator;
import org.asciidoctor.jruby.internal.RubyHashUtil;
import org.asciidoctor.jruby.internal.RubyObjectWrapper;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyHash;
Expand Down Expand Up @@ -91,10 +93,14 @@ public IRubyObject initialize(ThreadContext context, IRubyObject options) throws

@JRubyMethod(name = "process", required = 2)
public IRubyObject process(ThreadContext context, IRubyObject document, IRubyObject preprocessorReader) {
getProcessor().process(
PreprocessorReaderImpl readerArg = new PreprocessorReaderImpl(preprocessorReader);
Reader reader = getProcessor().process(
(Document) NodeConverter.createASTNode(document),
new PreprocessorReaderImpl(preprocessorReader));
readerArg);

return getRuntime().getNil();
if (reader == null || reader == readerArg) {
return getRuntime().getNil();
}
return ((RubyObjectWrapper) reader).getRubyObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static RubyHash convertMapToRubyHashWithSymbolsIfNecessary(Ruby rubyRunti

RubyHash rubyHash = new RubyHash(rubyRuntime);

if (options == null) {
return rubyHash;
}

Set<Entry<Object, Object>> optionsSet = options.entrySet();

for (Entry<Object, Object> entry : optionsSet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $secondLine"""

asciidoctor.javaExtensionRegistry().preprocessor(new Preprocessor() {
@Override
void process(Document doc, PreprocessorReader reader) {
Reader process(Document doc, PreprocessorReader reader) {
preprocessorCalled.set(true)

assert reader.peekLine() == firstLine
Expand All @@ -46,6 +46,7 @@ $secondLine"""
reader.advance()

assert reader.peekLine() == null
reader
}
})

Expand All @@ -63,7 +64,7 @@ $secondLine"""

asciidoctor.javaExtensionRegistry().preprocessor(new Preprocessor() {
@Override
void process(Document doc, PreprocessorReader reader) {
Reader process(Document doc, PreprocessorReader reader) {
preprocessorCalled.set(true)

assert reader.peekLines(ONE) == [firstLine]
Expand All @@ -78,6 +79,7 @@ $secondLine"""

assert reader.peekLines(ONE) == []
assert reader.peekLines(TWO) == []
reader
}
})

Expand All @@ -95,12 +97,13 @@ $secondLine"""

asciidoctor.javaExtensionRegistry().preprocessor(new Preprocessor() {
@Override
void process(Document doc, PreprocessorReader reader) {
Reader process(Document doc, PreprocessorReader reader) {
preprocessorCalled.set(true)

assert reader.readLine() == firstLine
assert reader.readLine() == secondLine
assert reader.readLine() == null
reader
}
})

Expand All @@ -120,7 +123,7 @@ $secondLine"""

asciidoctor.javaExtensionRegistry().preprocessor(new Preprocessor() {
@Override
void process(Document doc, PreprocessorReader reader) {
Reader process(Document doc, PreprocessorReader reader) {
preprocessorCalled.set(true)

assert reader.peekLine() == firstLine
Expand All @@ -131,6 +134,7 @@ $secondLine"""
assert reader.readLine() == anotherLine
assert reader.peekLine() == firstLine
assert reader.readLine() == firstLine
reader
}
})

Expand All @@ -151,7 +155,7 @@ $secondLine"""

asciidoctor.javaExtensionRegistry().preprocessor(new Preprocessor() {
@Override
void process(Document doc, PreprocessorReader reader) {
Reader process(Document doc, PreprocessorReader reader) {
preprocessorCalled.set(true)

assert reader.peekLine() == firstLine
Expand All @@ -164,6 +168,7 @@ $secondLine"""
assert reader.readLine() == anotherSecondLine
assert reader.peekLine() == firstLine
assert reader.readLine() == firstLine
reader
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class WhenAPreprocessorSetsTheSourceMapOption extends Specification {

static class SourceMapOptionPreprocessor extends Preprocessor {
@Override
void process(Document document, PreprocessorReader reader) {
Reader process(Document document, PreprocessorReader reader) {
document.sourcemap = true
reader
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public ChangeAttributeValuePreprocessor(Map<String, Object> config) {
}

@Override
public void process(Document document,
public Reader process(Document document,
PreprocessorReader reader) {

document.getAttributes().put("content", "Alex");
return reader;
}

}
Loading

0 comments on commit 63b04de

Please sign in to comment.