Skip to content

Commit

Permalink
resolves asciidoctor#1220 super.format
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jan 11, 2023
1 parent 42ad9be commit ff79bdf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
8 changes: 4 additions & 4 deletions packages/core/spec/graalvm/asciidoctor-convert.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* global Asciidoctor */
import Asciidoctor from '../../build/asciidoctor-graalvm.js'
var asciidoctor = Asciidoctor()
const asciidoctor = Asciidoctor()

var data = '= asciidoctor.js, AsciiDoc in JavaScript\n' +
const data = '= asciidoctor.js, AsciiDoc in JavaScript\n' +
'Doc Writer <docwriter@example.com>\n\n' +
'Asciidoctor and Opal come together to bring\n' +
'http://asciidoc.org[AsciiDoc] to the browser!.\n\n' +
Expand All @@ -13,6 +13,6 @@ var data = '= asciidoctor.js, AsciiDoc in JavaScript\n' +
'NOTE: That\'s all she wrote!!!\n\n' +
'include::spec/fixtures/include.adoc[]'

var options = { safe: 'server', header_footer: true, attributes: { showtitle: true } }
var html = asciidoctor.convert(data, options)
const options = { safe: 'server', header_footer: true, attributes: { showtitle: true } }
const html = asciidoctor.convert(data, options)
console.log(html)
28 changes: 28 additions & 0 deletions packages/core/spec/share/asciidoctor-spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,34 @@ America/New_York
expect(html).to.include('<pre>Europe/London\nAmerica/New_York</pre>')
expect(html).to.include('<style>pre.prism{background-color: lightgrey}</style>')
})

it('should call super.format', () => {
class BuildHighlighter {
format (node, lang, opts) {
opts = Object.assign({}, opts, {
transform: (x, code) => {
code['$[]=']('class', `language-${lang || 'none'} hljs`)
}
})
return this.super.format(node, lang, opts)
}

handlesHighlighting () { return true }

highlight (node, source, lang, opts) {
return `<span>${source}</span><span>${lang}</span>`
}
}

asciidoctor.SyntaxHighlighter.register('build', BuildHighlighter)
const source = `[source,ruby]
----
puts 'Hello, World!'
----`
const doc = asciidoctor.load(source, { attributes: { 'source-highlighter': 'build' } })
const html = doc.convert({ standalone: false })
expect(html).to.include('<pre class=" highlight"><code class="language-ruby hljs" data-lang="ruby"><span>puts \'Hello, World!\'</span><span>ruby</span></code></pre>')
})
})

describe('Table', function () {
Expand Down
18 changes: 16 additions & 2 deletions packages/core/src/asciidoctor-core-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ function initializeClass (superClass, className, functions, defaultFunctions, ar
}
}
Opal.def(scope, '$initialize', initialize)
Opal.def(scope, 'super', function (func) {
let $superFunction
Opal.def(scope, 'super', ($superFunction = function (func) {
if (typeof func === 'function') {
Opal.send(this, Opal.find_super_dispatcher(this, func.name, func))
} else {
Expand All @@ -281,7 +282,20 @@ function initializeClass (superClass, className, functions, defaultFunctions, ar
}
Opal.send(this, Opal.find_super_dispatcher(this, 'initialize', initialize), argumentsList)
}
})
}))
for (const functionName in functions) {
$superFunction[functionName] = function () {
const argumentsList = Array.from(arguments)
for (let i = 0; i < argumentsList.length; i++) {
// convert all (Opal) Hash arguments to JSON.
if (typeof argumentsList[i] === 'object' && typeof argumentsList[i].constructor === 'function' && argumentsList[i].constructor.name === 'Object') {
argumentsList[i] = toHash(argumentsList[i])
}
}
const self = scope.$$prototype
return Opal.send(self, Opal.find_super_dispatcher(self, functionName, self[`$${functionName}`]), argumentsList)
}
}
if (defaultFunctions) {
for (const defaultFunctionName in defaultFunctions) {
if (Object.prototype.hasOwnProperty.call(defaultFunctions, defaultFunctionName) && !Object.prototype.hasOwnProperty.call(defaultFunctionsOverridden, defaultFunctionName)) {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/types/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
'@typescript-eslint'
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended'
],
overrides: [
{
Expand All @@ -17,8 +17,8 @@ module.exports = {
'@typescript-eslint/no-this-alias': [
'error',
{
'allowDestructuring': false,
'allowedNames': ['self']
allowDestructuring: false,
allowedNames: ['self']
}
]
}
Expand Down

0 comments on commit ff79bdf

Please sign in to comment.