Skip to content

Commit

Permalink
Switchable callout rendering (TODO tests!)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bauer committed Oct 1, 2014
1 parent c622666 commit bfcf534
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public enum OptionKey {
DOTS("dots"),
FILEPATH("filepath"),
READER("reader"),
PRETTY("pretty");
PRETTY("pretty"),
CALLOUTS("callouts");


private String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Reader getReader(CitationAnchor citation) {
case JAVACODE:
return new JavacodeReader();
default:
throw new IllegalStateException("No reader availablef or address scheme of: " + citation);
throw new IllegalStateException("No reader available for address scheme of: " + citation);
}

} else if (readerOption != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

package org.fourthline.lemma.reader.content.printer;

import org.fourthline.lemma.anchor.CitationAnchor;
import org.seamless.xhtml.Option;
import org.seamless.xhtml.XHTML;
import org.seamless.xhtml.XHTMLElement;
import org.seamless.xhtml.XHTMLParser;
import org.seamless.xml.ParserException;

import javax.xml.xpath.XPath;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -39,7 +40,11 @@ abstract public class CalloutContentPrinter extends ContentPrinter {
final public static Pattern PATTERN_CALLOUT = Pattern.compile("^\\s*DOC:\\s*CALLOUT.*");

@Override
protected void append(String[] source, XHTMLElement contentElement, String... preFormattedClasses) {
protected void append(String[] source, CitationAnchor citation, XHTMLElement contentElement, String... preFormattedClasses) {

// TODO: This option should be tested!
Option calloutsOption = citation.getOption(CitationAnchor.OptionKey.CALLOUTS);
boolean calloutsEnabled = calloutsOption == null || Boolean.valueOf(calloutsOption.getFirstValue());

List<Integer> skippedLines = new ArrayList<Integer>();
Map<Integer, String> callouts = new LinkedHashMap<Integer, String>();
Expand Down Expand Up @@ -94,16 +99,6 @@ protected void append(String[] source, XHTMLElement contentElement, String... pr
}
}

XHTMLParser parser = new XHTMLParser();
XPath xpath = parser.createXPath();

XHTMLElement preFormatted =
createPreFormattedElement(contentElement, preFormattedClasses);

XHTMLElement calloutList =
contentElement.createChild(XHTML.ELEMENT.ol)
.setClasses("callouts");

StringBuilder preFormattedString = new StringBuilder();
currentLine = 0;
int currentCallout = 1;
Expand All @@ -117,7 +112,7 @@ protected void append(String[] source, XHTMLElement contentElement, String... pr
String line = source[currentLine];

if (searchNextLine || callouts.containsKey(currentLine)) {
if (isCalloutMarkerLine(line)) {
if (calloutsEnabled && isCalloutMarkerLine(line)) {
preFormattedString.append(line)
.append(wrapCalloutMarker(currentCallout++))
.append(getEndOfLine());
Expand All @@ -133,38 +128,48 @@ protected void append(String[] source, XHTMLElement contentElement, String... pr
currentLine++;
}

XHTMLParser parser = new XHTMLParser();
XPath xpath = parser.createXPath();

XHTMLElement preFormatted =
createPreFormattedElement(contentElement, preFormattedClasses);

if (preFormattedString.length() > 0) {
preFormatted.setContent(preFormattedString.toString());
} else {
preFormatted.getParent().removeChild(preFormatted);
}

// Wrap the callout comment in an XHTML <li> and append to the <ol>
for (String calloutString : callouts.values()) {
if (calloutString.length() > 0) {
try {
XHTML calloutContent = parser.parse(
XHTMLParser.wrap(
XHTML.ELEMENT.li.name(),
XHTML.NAMESPACE_URI,
calloutString
)
);
XHTMLElement calloutItem = calloutContent.getRoot(xpath);
calloutItem.setClasses("callout");
calloutList.appendChild(calloutItem, true);
} catch (ParserException ex) {
throw new RuntimeException(
"Error parsing callout comment as XHTML: " + calloutString, ex
);
if (calloutsEnabled) {
XHTMLElement calloutList =
contentElement.createChild(XHTML.ELEMENT.ol)
.setClasses("callouts");

// Wrap the callout comment in an XHTML <li> and append to the <ol>
for (String calloutString : callouts.values()) {
if (calloutString.length() > 0) {
try {
XHTML calloutContent = parser.parse(
XHTMLParser.wrap(
XHTML.ELEMENT.li.name(),
XHTML.NAMESPACE_URI,
calloutString
)
);
XHTMLElement calloutItem = calloutContent.getRoot(xpath);
calloutItem.setClasses("callout");
calloutList.appendChild(calloutItem, true);
} catch (ParserException ex) {
throw new RuntimeException(
"Error parsing callout comment as XHTML: " + calloutString, ex
);
}
}
}
}

if (calloutList.getChildren().length == 0)
calloutList.getParent().removeChild(calloutList);

if (calloutList.getChildren().length == 0)
calloutList.getParent().removeChild(calloutList);
}
}

protected boolean isCalloutMarkerLine(String line) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.fourthline.lemma.reader.content.printer;

import org.fourthline.lemma.Constants;
import org.fourthline.lemma.anchor.CitationAnchor;
import org.seamless.xhtml.XHTML;
import org.seamless.xhtml.XHTMLElement;

Expand All @@ -32,7 +33,7 @@ public abstract class ContentPrinter {

final private Logger log = Logger.getLogger(ContentPrinter.class.getName());

public void print(String[] source, XHTMLElement parentElement, String... preFormattedClasses) {
public void print(String[] source, CitationAnchor citation, XHTMLElement parentElement, String... preFormattedClasses) {
if (source == null || source.length == 0)
return;

Expand All @@ -42,7 +43,7 @@ public void print(String[] source, XHTMLElement parentElement, String... preForm
parentElement.createChild(Constants.WRAPPER_ELEMENT)
.setAttribute(XHTML.ATTR.CLASS, Constants.TYPE_CONTENT);

append(source, content, preFormattedClasses);
append(source, citation, content, preFormattedClasses);
}


Expand All @@ -57,6 +58,6 @@ protected XHTMLElement createPreFormattedElement(XHTMLElement parent, String...
return element;
}

abstract protected void append(String[] source, XHTMLElement contentElement, String... preFormattedClasses);
abstract protected void append(String[] source, CitationAnchor citation, XHTMLElement contentElement, String... preFormattedClasses);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.fourthline.lemma.reader.content.printer;

import org.fourthline.lemma.anchor.CitationAnchor;
import org.seamless.xhtml.XHTMLElement;

/**
Expand All @@ -25,7 +26,7 @@
public class PlainContentPrinter extends ContentPrinter {

@Override
protected void append(String[] source, XHTMLElement contentElement, String... preFormattedClasses) {
protected void append(String[] source, CitationAnchor citation, XHTMLElement contentElement, String... preFormattedClasses) {
XHTMLElement preFormatted = createPreFormattedElement(contentElement, preFormattedClasses);

StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void appendContent(XHTMLElement parent, File file, CitationAnchor cita
content = filter.filter(content, citation);
}

printer.print(content, parent, "prettyprint");
printer.print(content, citation, parent, "prettyprint");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected void appendContent(XHTMLElement parent, Doc doc, CitationAnchor citati
source = filter.filter(source, citation);
}

printer.print(source, parent, "prettyprint");
printer.print(source, citation, parent, "prettyprint");
}

public String[] readSource(Doc doc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ protected void appendContent(XHTMLElement parent, File file, CitationAnchor cita

Option prettyOption = citation.getOption(CitationAnchor.OptionKey.PRETTY);
if (prettyOption != null && Boolean.valueOf(prettyOption.getFirstValue())) {
printer.print(content, parent, "prettyprint");
printer.print(content, citation, parent, "prettyprint");
} else {
printer.print(content, parent);
printer.print(content, citation, parent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void appendContent(XHTMLElement parent, File file, CitationAnchor cita
content = filter.filter(content, citation);
}

printer.print(content, parent, "prettyprint");
printer.print(content, citation, parent, "prettyprint");
}

}

0 comments on commit bfcf534

Please sign in to comment.