Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and Extensions to the IncludeProcessor and Reader classes #537

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface Reader {

/**
* Get the 1-based offset of the current line.
*
*
* @return 1-based offset.
* @deprecated Please use {@link #getLineNumber()}
*/
Expand All @@ -19,27 +19,41 @@ public interface Reader {
*/
int getLineNumber();

/**
* Get the name of the file of the current line.
*
* @return file name
*/
public String getFile();

/**
* Get the name of the directory of the current file
*
* @return directory name
*/
public String getDir();

/**
* Check whether there are any lines left to read. If a previous call to this method resulted in a value of false,
* immediately returned the cached value. Otherwise, delegate to peek_line to determine if there is a next line
* available.
*
*
* @return True if there are more lines, False if there are not.
*/
boolean hasMoreLines();

/**
* Peek at the next line and check if it's empty (i.e., whitespace only)
*
*
* This method Does not consume the line from the stack.
*
*
* @return True if the there are no more lines or if the next line is empty
*/
boolean isNextLineEmpty();

/**
* Get the remaining lines of source data joined as a String. Delegates to Reader#read_lines, then joins the result.
*
*
* @return the lines read joined as a String
*/
String read();
Expand All @@ -48,7 +62,7 @@ public interface Reader {
* Get the remaining lines of source data. This method calls Reader#read_line repeatedly until all lines are
* consumed and returns the lines as a String Array. This method differs from Reader#lines in that it processes each
* line in turn, hence triggering any preprocessors implemented in sub-classes.
*
*
* @return the lines read as a String Array
*/
List<String> readLines();
Expand Down Expand Up @@ -98,13 +112,13 @@ public interface Reader {

/**
* Advance to the next line by discarding the line at the front of the stack
*
*
* @return a Boolean indicating whether there was a line to discard.
*/
boolean advance();

/**
*
*
* Public: Advance to the end of the reader, consuming all remaining lines
*/
void terminate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.asciidoctor.extension;

import java.util.List;

import org.asciidoctor.internal.RubyObjectWrapper;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.List;

public class ReaderImpl extends RubyObjectWrapper implements Reader {

public ReaderImpl(IRubyObject rubyNode) {
Expand All @@ -16,7 +16,7 @@ public ReaderImpl(IRubyObject rubyNode) {

static ReaderImpl createReader(Ruby runtime, List<String> lines) {
RubyArray rubyLines = runtime.newArray(lines.size());
for (String line: lines) {
for (String line : lines) {
rubyLines.add(runtime.newString(line));
}

Expand All @@ -34,6 +34,16 @@ public int getLineNumber() {
return getInt("lineno");
}

public String getFile() {
IRubyObject rObj = getRubyProperty("file");
return rObj.toString();
}

public String getDir() {
IRubyObject rObj = getRubyProperty("dir");
return rObj.toString();
}

@Override
public boolean hasMoreLines() {
return getBoolean("has_more_lines?");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.asciidoctor.extension.processorproxies;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.NodeConverter;
import org.asciidoctor.extension.IncludeProcessor;
Expand All @@ -20,10 +24,6 @@
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class IncludeProcessorProxy extends AbstractProcessorProxy<IncludeProcessor> {

public IncludeProcessorProxy(Ruby runtime, RubyClass metaClass, Class<? extends IncludeProcessor> includeProcessorClass) {
Expand Down Expand Up @@ -97,7 +97,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject options) throws
return null;
}

@JRubyMethod(name = "handles", required = 1)
@JRubyMethod(name = "handles?", required = 1)
public IRubyObject handles(ThreadContext context, IRubyObject target) {
boolean b = getProcessor().handles(RubyUtils.rubyToJava(getRuntime(), target, String.class));
return JavaEmbedUtils.javaToRuby(getRuntime(), b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ private Map<String, Object> createJavaMap() {
key = ((RubyString) rubyKey).asJavaString();
} else if (rubyKey instanceof String) {
key = (String) rubyKey;
} else if (rubyKey instanceof Number) {
key = String.valueOf(rubyKey);
} else {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.asciidoctor.extension;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.asciidoctor.ast.Document;

public class PositionalAttrsIncludeProcessor extends IncludeProcessor {

public PositionalAttrsIncludeProcessor() {
}

public PositionalAttrsIncludeProcessor(Map<String, Object> config) {
super(config);
}

@Override
public boolean handles(String target) {
return true;
}

@Override
public void process(Document document, PreprocessorReader reader, String target, Map<String, Object> attributes) {
Map<String, Object> treeMap = new TreeMap<String, Object>(attributes);

String str = "";
Iterator<Map.Entry<String, Object>> it = treeMap.entrySet().iterator();
if (it.hasNext())
str += it.next().getValue();
while (it.hasNext()) {
str += "," + it.next().getValue();
}

reader.push_include(str, target, target, 1, attributes);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
Expand Down Expand Up @@ -395,6 +396,42 @@ public void a_include_instance_processor_should_be_executed_when_include_macro_i
}

@Test
public void a_include_processor_should_only_handle_its_handles() {

JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

javaExtensionRegistry.includeProcessor(UriIncludeProcessor.class);

String content = asciidoctor.renderFile(classpath.getResource("sample-with-include.ad"),
options().toFile(false).get());

org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8");

Element contentElement = doc.getElementsByAttributeValue("class", "bare").first();

assertThat(contentElement.text(), startsWith("sample-book.adoc"));

}

@Test
public void a_include_processor_can_handle_positional_attrs() {

JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

javaExtensionRegistry.includeProcessor(PositionalAttrsIncludeProcessor.class);

String content = asciidoctor.renderFile(classpath.getResource("sample-with-include-pos-attrs.ad"),
options().toFile(false).get());

org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8");

Element contentElement = doc.getElementsByAttributeValue("class", "paragraph IncludeBlock").first();

assertThat(contentElement.text(), startsWith("My,Positional,Attribute List"));

}

@Test
public void a_treeprocessor_should_be_executed_in_document() {

JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();
Expand Down Expand Up @@ -518,7 +555,7 @@ public void a_block_macro_extension_should_be_executed_when_macro_is_detected()
javaExtensionRegistry.blockMacro("gist", GistMacro.class);

String content = asciidoctor.renderFile(
classpath.getResource("sample-with-gist-macro.ad"),
classpath.getResource("sample-with-gist-macro.ad"),
options().toFile(false).get());

org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= Example of Include

Some text

[role=IncludeBlock]
include::sample-book.adoc[My,Positional,"Attribute List"]
5 changes: 5 additions & 0 deletions asciidoctorj-core/src/test/resources/sample-with-include.ad
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= Example of Include

Some text

include::sample-book.adoc[]