Fix upstream test to adapt to changes in HighlightJS syntax highlighter.
MODIFY
CHANGELOG.adoc
CHANGELOG.adoc
@@ -19,6 +19,10 @@ Improvement::
* Upgrade to JRuby 9.2.12.0 removing the last illegal access warnings (#935)
* Upgrade to Asciidoctor EPUB3 1.5.0-alpha.16 (#939)
+Build::
+
+* Fix upstream build to adapt to changes in Ruby Highlightjs syntax highlighter (#940)
+
== 2.3.1 (2020-06-17)
Bug Fixes::

MODIFY
asciidoctorj-core/src/test/groovy/org/asciidoctor/syntaxhighlighter/HighlightJsExtension.groovy
asciidoctorj-core/src/test/groovy/org/asciidoctor/syntaxhighlighter/HighlightJsExtension.groovy
@@ -9,7 +9,7 @@ class HighlightJsExtension implements org.asciidoctor.jruby.syntaxhighlighter.sp
@Override
void register(Asciidoctor asciidoctor) {
asciidoctor.syntaxHighlighterRegistry()
- .register(HighlightJsHighlighter, NAME_HIGHLIGHTER)
+ .register(ObservableHighlightJsHighlighter, NAME_HIGHLIGHTER)
}
}

MODIFY
asciidoctorj-core/src/test/groovy/org/asciidoctor/syntaxhighlighter/HighlightJsHighlighter.groovy
asciidoctorj-core/src/test/groovy/org/asciidoctor/syntaxhighlighter/HighlightJsHighlighter.groovy
@@ -4,12 +4,10 @@ import org.asciidoctor.ast.Block
import org.asciidoctor.ast.Document
import org.asciidoctor.extension.LocationType
-class HighlightJsHighlighter implements SyntaxHighlighterAdapter, Formatter {
+abstract class AbstractHighlightJsHighlighter implements SyntaxHighlighterAdapter, Formatter {
@Override
- boolean hasDocInfo(LocationType location) {
- location == LocationType.FOOTER
- }
+ abstract boolean hasDocInfo(LocationType location)
@Override
String getDocinfo(LocationType location, Document document, Map<String, Object> options) {
@@ -24,3 +22,46 @@ class HighlightJsHighlighter implements SyntaxHighlighterAdapter, Formatter {
"""<pre class="highlightjs highlight"><code data-lang="$lang" class="language-$lang hljs">${node.content}</code></pre>"""
}
}
+
+class OldHighlightJsHighlighter extends AbstractHighlightJsHighlighter {
+
+ @Override
+ boolean hasDocInfo(LocationType location) {
+ location == LocationType.FOOTER
+ }
+
+}
+
+class NewHighlightJsHighlighter extends AbstractHighlightJsHighlighter {
+
+ @Override
+ boolean hasDocInfo(LocationType location) {
+ true
+ }
+
+ @Override
+ String getDocinfo(LocationType location, Document document, Map<String, Object> options) {
+ String baseUrl = document.getAttribute('highlightjsdir', "${options['cdn_base_url']}/highlight.js/9.15.6")
+ location == LocationType.HEADER ?
+ """<link rel="stylesheet" href="$baseUrl/styles/${document.getAttribute('highlightjs-theme', 'github')}.min.css"${options['self_closing_tag_slash']}>""" :
+ """<script src="$baseUrl/highlight.min.js"></script>
+<script>hljs.initHighlighting()</script>"""
+ }
+
+}
+
+class ObservableHighlightJsHighlighter extends AbstractHighlightJsHighlighter {
+
+ static int called = 0
+
+ @Override
+ boolean hasDocInfo(LocationType location) {
+ location == LocationType.FOOTER
+ }
+
+ @Override
+ String format(Block node, String lang, Map<String, Object> opts) {
+ called++
+ super.format(node, lang, opts)
+ }
+}

MODIFY
asciidoctorj-core/src/test/groovy/org/asciidoctor/syntaxhighlighter/WhenAHighlightjsAdapterIsImplementedInGroovy.groovy
asciidoctorj-core/src/test/groovy/org/asciidoctor/syntaxhighlighter/WhenAHighlightjsAdapterIsImplementedInGroovy.groovy
@@ -10,11 +10,15 @@ import org.junit.runner.RunWith
import spock.lang.Specification
import static junit.framework.Assert.assertEquals
+import static org.hamcrest.Matchers.either
+import static org.hamcrest.Matchers.is
+import static org.junit.Assert.assertThat
@RunWith(ArquillianSputnik)
class WhenAHighlightjsAdapterIsImplementedInGroovy extends Specification {
- public static final String NAME_SYNTAXHIGHLIGHTER = 'highlight4J'
+ public static final String NAME_SYNTAXHIGHLIGHTER_OLD = 'oldhighlight4J'
+ public static final String NAME_SYNTAXHIGHLIGHTER_NEW = 'newhighlight4J'
public static final String HIGHLIGHTJS = 'highlightjs'
@ArquillianResource
@@ -40,13 +44,18 @@ func main() {
given:
- asciidoctor.syntaxHighlighterRegistry().register(HighlightJsHighlighter, NAME_SYNTAXHIGHLIGHTER)
+ asciidoctor.syntaxHighlighterRegistry().register(OldHighlightJsHighlighter, NAME_SYNTAXHIGHLIGHTER_OLD)
+ asciidoctor.syntaxHighlighterRegistry().register(NewHighlightJsHighlighter, NAME_SYNTAXHIGHLIGHTER_NEW)
when:
- String htmlJava = asciidoctor.convert(doc, OptionsBuilder.options()
+ String htmlJavaOld = asciidoctor.convert(doc, OptionsBuilder.options()
.safe(SafeMode.UNSAFE)
.headerFooter(true)
- .attributes(AttributesBuilder.attributes().sourceHighlighter(NAME_SYNTAXHIGHLIGHTER)))
+ .attributes(AttributesBuilder.attributes().sourceHighlighter(NAME_SYNTAXHIGHLIGHTER_OLD)))
+ String htmlJavaNew = asciidoctor.convert(doc, OptionsBuilder.options()
+ .safe(SafeMode.UNSAFE)
+ .headerFooter(true)
+ .attributes(AttributesBuilder.attributes().sourceHighlighter(NAME_SYNTAXHIGHLIGHTER_NEW)))
String htmlRuby = asciidoctor.convert(doc, OptionsBuilder.options()
.safe(SafeMode.UNSAFE)
@@ -55,24 +64,20 @@ func main() {
then:
// Cannot use `htmlRuby == htmlJava` because it fails with OOM
- assertEquals(htmlRuby, htmlJava)
+ // Asciidoctor > 2.0.10 changed the location where the javascript is included,
+ // therefore test against both possibilities to pass with 2.0.10 and upstream
+ assertThat(htmlRuby, either(is(htmlJavaNew)).or(is(htmlJavaOld)))
}
def 'should autoregister from extension'() {
when:
- String htmlJava = asciidoctor.convert(doc, OptionsBuilder.options()
+ asciidoctor.convert(doc, OptionsBuilder.options()
.safe(SafeMode.UNSAFE)
.headerFooter(true)
.attributes(AttributesBuilder.attributes().sourceHighlighter(HighlightJsExtension.NAME_HIGHLIGHTER)))
- String htmlRuby = asciidoctor.convert(doc, OptionsBuilder.options()
- .safe(SafeMode.UNSAFE)
- .headerFooter(true)
- .attributes(AttributesBuilder.attributes().sourceHighlighter(HIGHLIGHTJS)))
-
then:
- // Cannot use `htmlRuby == htmlJava` because it fails with OOM
- assertEquals(htmlRuby, htmlJava)
+ assertEquals(2, ObservableHighlightJsHighlighter.called)
}
}

Fix upstream test to adapt to changes in HighlightJS syntax highlighter.