Skip to content

Commit

Permalink
Improve listing blocks processing
Browse files Browse the repository at this point in the history
  • Loading branch information
fluorumlabs committed Dec 10, 2018
1 parent 0f69894 commit d52fa5e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,18 @@ public enum AsciidocRenderer {
}
}),
LISTING_BLOCK(x -> {
x.tagName("div").addClass("listingblock");
x.tagName("div").addClass("listingblock").removeClass("listing");
Element div = new Element("div").addClass("content");
if (getArgument(x, 0).equals("source")) {
if (getArgument(x, 0).equals("source") || x.hasClass("source")) {
String language = getArgument(x, 1);
if ( language.isEmpty() ) language=x.getVariables().optString("source-language");
Element pre = new Element("pre")
.addClass("highlight");

if ( hasOption(x,"nowrap")) {
pre.addClass("nowrap");
}

Element code = new Element("code");
if (!language.isEmpty()) {
code.addClass("language-" + language)
Expand All @@ -430,6 +436,7 @@ public enum AsciidocRenderer {

moveChildNodes(x, code);
x.appendChild(div);
x.removeClass("source");
} else {
Element listingPre = new Element("pre");
div.appendChild(listingPre);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/github/fluorumlabs/asciidocj/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,17 @@ public static String getArgument(Element x, int i) {
}
}

protected static boolean hasOption(Element x, String key) {
if (!(x instanceof AsciidocElement)) {
return false;
}
JSONObject properties = ((AsciidocElement) x).getProperties();
if (!properties.has("options")) {
return false;
} else {
return properties.getJSONObject("options").has(key);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ AdmonitionType = "NOTE"|"TIP"|"IMPORTANT"|"WARNING"|"CAUTION"
"[" {NoLineFeed}+ "'" {NoLineFeed}+ "']" {LineFeed} |
"[" {NoLineFeed}+ "]" {LineFeed}
{
PropertiesParser.parse(strip(yytext(), 1, 2), properties);
PropertiesParser.parse(strip(yytext(), 1, 2), properties, true);
promoteArgumentsToClasses();
}

Expand Down Expand Up @@ -625,7 +625,7 @@ AdmonitionType = "NOTE"|"TIP"|"IMPORTANT"|"WARNING"|"CAUTION"
imgUrl = path.concat(imgUrl);
}

PropertiesParser.parse(extractBetween(yytext(), "[", "]"), properties);
PropertiesParser.parse(extractBetween(yytext(), "[", "]"), properties, false);

String alt = properties.optString("alt", getArgument(0));
if (alt.isEmpty()) alt = extractAfterStrict(extractBeforeStrict(imgUrl, "."), "/");
Expand Down Expand Up @@ -669,7 +669,7 @@ AdmonitionType = "NOTE"|"TIP"|"IMPORTANT"|"WARNING"|"CAUTION"
{
String videoUrl = extractBetween(yytext(), "video::", "[");

PropertiesParser.parse(extractBetween(yytext(), "[", "]"), properties);
PropertiesParser.parse(extractBetween(yytext(), "[", "]"), properties, false);

if (!videoUrl.startsWith("http://") && !videoUrl.startsWith("https://")
&& !getArgument(0).equals("youtube") && !getArgument(0).equals("vimeo")) {
Expand All @@ -696,7 +696,7 @@ AdmonitionType = "NOTE"|"TIP"|"IMPORTANT"|"WARNING"|"CAUTION"
{
String audioUrl = extractBetween(yytext(), "audio::", "[");

PropertiesParser.parse(extractBetween(yytext(), "[", "]"), properties);
PropertiesParser.parse(extractBetween(yytext(), "[", "]"), properties, false);

if (!audioUrl.startsWith("http://") && !audioUrl.startsWith("https://")) {
String path = attributes.optString("imagesdir", DEFAULT_IMAGESDIR);
Expand Down Expand Up @@ -762,7 +762,7 @@ AdmonitionType = "NOTE"|"TIP"|"IMPORTANT"|"WARNING"|"CAUTION"

yypushback(1);
yybegin(BLOCK);
} else if (getArgument(0).equals("source")) {
} else if (getArgument(0).equals("source") || getArgument(0).equals("listing")) {
yypushback(1);
openElement(AsciidocRenderer.LISTING_BLOCK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Properties = "[" ("\\]"|[^\]\r\n\u2028\u2029\u000B\u000C\u0085\
String text;
id = extractBetween(yytext(), "anchor:", "[");

PropertiesParser.parse(extractBetween(yytext(), "[", "]"), anchorOptions);
PropertiesParser.parse(extractBetween(yytext(), "[", "]"), anchorOptions, false);
text = getArgument(anchorOptions, 0);
if (!text.isEmpty()) {
attributes.put("anchor:" + id, getFormatted(text).body().html());
Expand All @@ -322,7 +322,7 @@ Properties = "[" ("\\]"|[^\]\r\n\u2028\u2029\u000B\u000C\u0085\
text = extractBetween(content,"[","]");
} else {
JSONObject anchorOptions = new JSONObject();
PropertiesParser.parse(strip(yytext(), 2, 2), anchorOptions);
PropertiesParser.parse(strip(yytext(), 2, 2), anchorOptions, false);

id = getArgument(anchorOptions, 0);
text = getArgument(anchorOptions, 1);
Expand Down Expand Up @@ -354,7 +354,7 @@ Properties = "[" ("\\]"|[^\]\r\n\u2028\u2029\u000B\u000C\u0085\
yypushback(1);
}

PropertiesParser.parse(text, properties);
PropertiesParser.parse(text, properties, false);
String title = getArgument(0);
if (title.endsWith("^")) {
title = stripTail(title, 1);
Expand Down Expand Up @@ -383,7 +383,7 @@ Properties = "[" ("\\]"|[^\]\r\n\u2028\u2029\u000B\u000C\u0085\
String content = yytext();
String text = extractBetween(extractAfterStrict(content, "++"), "[", "]");
String href = extractBetween(content, "++", "++");
PropertiesParser.parse(text, properties);
PropertiesParser.parse(text, properties, false);
String title = getArgument(0);
if (title.endsWith("^")) {
title = stripTail(title, 1);
Expand Down Expand Up @@ -420,7 +420,7 @@ Properties = "[" ("\\]"|[^\]\r\n\u2028\u2029\u000B\u000C\u0085\
yypushback(1);
}

PropertiesParser.parse(text, properties);
PropertiesParser.parse(text, properties, false);
String title = getArgument(0);
if (title.endsWith("^")) {
title = stripTail(title, 1);
Expand Down Expand Up @@ -451,7 +451,7 @@ Properties = "[" ("\\]"|[^\]\r\n\u2028\u2029\u000B\u000C\u0085\
}
String href = extractBefore(content, "[");
String text = extractBetween(content, "[", "]");
PropertiesParser.parse(text, properties);
PropertiesParser.parse(text, properties, false);
String title = getArgument(0);

if (!getArgument(1).isEmpty()) {
Expand Down Expand Up @@ -487,7 +487,7 @@ Properties = "[" ("\\]"|[^\]\r\n\u2028\u2029\u000B\u000C\u0085\
imgUrl = path.concat(imgUrl);
}

PropertiesParser.parse(extractBetween(yytext(), "[", "]"), properties);
PropertiesParser.parse(extractBetween(yytext(), "[", "]"), properties, false);

String alt = getArgument(0);
if (alt.isEmpty()) alt = extractAfterStrict(extractBeforeStrict(imgUrl, "."), "/");
Expand Down Expand Up @@ -583,7 +583,7 @@ Properties = "[" ("\\]"|[^\]\r\n\u2028\u2029\u000B\u000C\u0085\
if ( fallback(Pass.ATTRIBUTES) ) break;

String text = strip(yytext(), 1, 1);
PropertiesParser.parse(text, properties);
PropertiesParser.parse(text, properties, true);
if (!properties.has("class")) {
properties.put("class", new JSONObject());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import java.io.StringReader;
private boolean isId = false;
private boolean isOption = false;
private boolean isProperty = false;
private boolean canShorthands = false;

// We don't need that :)
protected static class Yytoken {
Expand All @@ -40,12 +41,13 @@ import java.io.StringReader;
* @param p Properties JSONObject that will receive parsed output
* @return Properties JSONObject
*/
public static JSONObject parse(String text, JSONObject p) {
public static JSONObject parse(String text, JSONObject p, boolean withShorthands) {
try(StringReader reader = new StringReader(text)) {
PropertiesParser parser = new PropertiesParser(reader);
if ( p != null ) {
parser.properties = p;
}
parser.canShorthands = withShorthands;
parser.parseInput();
parser.addValue(false);
return parser.properties;
Expand Down Expand Up @@ -118,9 +120,9 @@ import java.io.StringReader;
%%

<YYINITIAL> {
"."
"." / \s* [a-zA-Z0-9]
{
if ( currentString.length() == 0 || isClass || isId || isOption) {
if (canShorthands) {
addValue(false);
isClass = true;
} else {
Expand All @@ -129,9 +131,9 @@ import java.io.StringReader;
}
}

"%"
"%" / \s* [a-zA-Z0-9]
{
if ( currentString.length() == 0 || isClass || isId || isOption) {
if (canShorthands) {
addValue(false);
isOption = true;
} else {
Expand All @@ -140,9 +142,9 @@ import java.io.StringReader;
}
}

"#"
"#" / \s* [a-zA-Z0-9]
{
if ( currentString.length() == 0 || isClass || isId || isOption) {
if (canShorthands) {
addValue(false);
isId = true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public static Collection<Object[]> data() {
{"source, ruby",
"{\"arguments\":[\"source\",\"ruby\"]}"},
{"id='wrapup'",
"{\"id\":\"wrapup\"}"}
"{\"id\":\"wrapup\"}"},
{"source%nowrap,java",
"{\"options\":{\"nowrap\":\"\"},\"arguments\":[\"source\",\"java\"]}"}
});
}

Expand All @@ -90,6 +92,6 @@ public AsciidocPropertiesTest(String input, String expected) {
@Test
public void testAsciidocProperties() {
Assert.assertEquals(fExpected,
PropertiesParser.parse(fInput, null).toString());
PropertiesParser.parse(fInput, null, true).toString());
}
}

0 comments on commit d52fa5e

Please sign in to comment.