Skip to content

Commit

Permalink
resolves #1645 map CompositeConverter#convert (#1649)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored Sep 19, 2022
1 parent 9ec9dfc commit 6ecd6db
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/core/benchmark/chrome.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ puppeteer.launch({ args: ['--disable-gpu', '--no-sandbox', '--single-process', '
const start = new Date().getTime()
html = asciidoctor.convert(content, options)
const duration = new Date().getTime() - start
result.push({ id: i, duration: duration, html: html })
result.push({ id: i, duration, html })
}
return result
}, data)
Expand Down
8 changes: 4 additions & 4 deletions packages/core/benchmark/stats.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const log = (durations) => {
}

module.exports = {
quantile: quantile,
average: average,
standardDeviation: standardDeviation,
log: log
quantile,
average,
standardDeviation,
log
}
6 changes: 3 additions & 3 deletions packages/core/spec/browser/asciidoctor-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Asciidoctor from '../../build/asciidoctor-browser.js'
mocha.setup({
ui: 'bdd',
checkLeaks: false,
reporter: reporter
reporter
})

const expect = chai.expect
Expand All @@ -27,9 +27,9 @@ import Asciidoctor from '../../build/asciidoctor-browser.js'
}
const testOptions = {
platform: 'Browser',
baseDir: baseDir,
baseDir,
coreVersion: asciidoctorCoreSemVer,
remoteBaseUri: remoteBaseUri
remoteBaseUri
}
shareSpec(testOptions, asciidoctor, expect)
includeHttpsSpec(testOptions, asciidoctor, expect)
Expand Down
49 changes: 37 additions & 12 deletions packages/core/spec/node/asciidoctor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ intro
const text = message.text
const sourceLocation = message.source_location
return JSON.stringify({
programName: programName,
programName,
message: text,
sourceLocation: {
lineNumber: sourceLocation.getLineNumber(),
path: sourceLocation.getPath()
},
severity: severity
severity
}) + '\n'
}
}))
Expand Down Expand Up @@ -335,7 +335,7 @@ intro
it('should print timings to the MemoryLogger', () => {
const memoryLogger = asciidoctor.MemoryLogger.create()
const timings = asciidoctor.Timings.create()
const options = { timings: timings }
const options = { timings }
asciidoctor.convert('Hello *world*', options)
timings.printReport(memoryLogger, 'stdin')
const messages = memoryLogger.getMessages()
Expand Down Expand Up @@ -394,7 +394,7 @@ intro
const logs = []
const customLogger = asciidoctor.LoggerManager.newLogger('CustomLogger', {
add: function (severity, message, progname) {
logs.push({ severity: severity, message: message || progname })
logs.push({ severity, message: message || progname })
}
})
customLogger.error('hello')
Expand All @@ -406,7 +406,7 @@ intro
const logs = []
const customLogger = asciidoctor.LoggerManager.newLogger('CustomLogger', {
add: function (severity, message, progname) {
logs.push({ severity: severity, message: message, progname: progname })
logs.push({ severity, message, progname })
}
})
customLogger.add('error', 'hi', 'asciidoctor.js')
Expand Down Expand Up @@ -475,7 +475,7 @@ intro
}
})
const timings = asciidoctor.Timings.create()
const options = { timings: timings }
const options = { timings }
asciidoctor.convert('Hello *world*', options)
timings.printReport(outStream, 'stdin')
outStream.end()
Expand All @@ -487,11 +487,11 @@ intro
try {
const data = []
console.log = function () {
data.push({ method: 'log', arguments: arguments })
data.push({ method: 'log', arguments })
return defaultLog.apply(console, arguments)
}
const timings = asciidoctor.Timings.create()
const options = { timings: timings }
const options = { timings }
asciidoctor.convert('Hello *world*', options)
timings.printReport(console, 'stdin')
expect(data.length).to.equal(4)
Expand All @@ -502,7 +502,7 @@ intro
})
it('should print timings to an object with a log function', () => {
const timings = asciidoctor.Timings.create()
const options = { timings: timings }
const options = { timings }
asciidoctor.convert('Hello *world*', options)
const logger = {}
const data = []
Expand All @@ -518,10 +518,10 @@ intro
const data = []
try {
process.stdout.write = function () {
data.push({ method: 'log', arguments: arguments })
data.push({ method: 'log', arguments })
}
const timings = asciidoctor.Timings.create()
const options = { timings: timings }
const options = { timings }
asciidoctor.convert('Hello *world*', options)
timings.printReport(undefined, 'stdin')
expect(data.length).to.equal(4)
Expand Down Expand Up @@ -2230,7 +2230,7 @@ content`, options)
const markup = defs[key]
it(`should include docinfo files for html backend with attribute ${key}`, () => {
const attributes = ['linkcss', 'copycss!'].concat(key.split(' '))
const options = { safe: 'safe', standalone: true, to_file: false, attributes: attributes }
const options = { safe: 'safe', standalone: true, to_file: false, attributes }
const html = asciidoctor.convertFile('spec/fixtures/basic.adoc', options)
if (markup.head_script) {
expect(html).to.contain('<script src="modernizr.js"></script>')
Expand Down Expand Up @@ -3057,6 +3057,31 @@ And here&#8217;s a block image:</p>
}).timeout(5000)
})

describe('Using a composite converter', () => {
it('should get the converters', () => {
const options = { safe: 'safe', backend: 'html5', template_dir: 'spec/fixtures/templates/nunjucks' }
const doc = asciidoctor.load('content', options)
const compositeConverter = doc.getConverter()
expect(compositeConverter.getConverters().length).to.equal(2)
}).timeout(5000)
it('should retrieve the converter for the specified transform', () => {
const options = { safe: 'safe', backend: 'html5', template_dir: 'spec/fixtures/templates/nunjucks' }
const doc = asciidoctor.load('content', options)
const compositeConverter = doc.getConverter()
expect(compositeConverter.getConverter('paragraph').constructor.$$name).to.equal('TemplateConverter')
expect(compositeConverter.getConverter('admonition').constructor.$$name).to.equal('Html5Converter')
expect(() => compositeConverter.getConverter('invalid')).to.throw(Error, 'Could not find a converter to handle transform: invalid')
}).timeout(5000)
it('should find the converter for the specified transform', () => {
const options = { safe: 'safe', backend: 'html5', template_dir: 'spec/fixtures/templates/nunjucks' }
const doc = asciidoctor.load('content', options)
const compositeConverter = doc.getConverter()
expect(compositeConverter.findConverter('paragraph').constructor.$$name).to.equal('TemplateConverter')
expect(compositeConverter.findConverter('admonition').constructor.$$name).to.equal('Html5Converter')
expect(() => compositeConverter.findConverter('invalid')).to.throw(Error, 'Could not find a converter to handle transform: invalid')
}).timeout(5000)
})

if (isWin && process.env.APPVEYOR_BUILD_FOLDER) {
describe('Windows', () => {
it('should include file with an absolute path (base_dir is the drive letter)', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/spec/share/asciidoctor-spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ A bold statement!<sup class="footnote" id="_footnote_disclaimer">[<a id="_footno
}
const openBlock = self.createBlock(parent, 'open', [], { role: 'external-url' })
openBlock.title = attrs.title
const link = self.createInline(parent, 'anchor', text, { type: 'link', target: target })
const link = self.createInline(parent, 'anchor', text, { type: 'link', target })
openBlock.append(link)
return openBlock
})
Expand Down
4 changes: 2 additions & 2 deletions packages/core/spec/share/bin/mock-server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ portfinder.getPort((err, port) => {
if (err) {
throw err
}
server = new ServerMock({ host: 'localhost', port: port })
server = new ServerMock({ host: 'localhost', port })
server.start(() => {
console.log(`Running at http://localhost:${port}`)
process.send({ event: 'started', port: port })
process.send({ event: 'started', port })
})
})
100 changes: 100 additions & 0 deletions packages/core/src/asciidoctor-core-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3797,6 +3797,27 @@ if (TemplateConverter) {
return this['$handles?'](name)
}

/**
* Converts the {AbstractNode} using only its converted content.
*
* @param {AbstractNode} node
* @returns {string} - the converted {string} content.
* @memberof Converter/TemplateConverter
*/
TemplateConverter.prototype.getContentOnly = function (node) {
return this.$content_only(node)
}

/**
* Skips conversion of the {AbstractNode}.
*
* @param {AbstractNode} node
* @memberof Converter/TemplateConverter
*/
TemplateConverter.prototype.skip = function (node) {
this.$skip(node)
}

/**
* Retrieves the templates that this converter manages.
*
Expand Down Expand Up @@ -3867,3 +3888,82 @@ if (TemplateConverter) {
}
}
}

/**
* @namespace
* @module Converter/CompositeConverter
*/
const CompositeConverter = Opal.Asciidoctor.Converter.CompositeConverter

if (CompositeConverter) {
// Alias
Opal.Asciidoctor.CompositeConverter = CompositeConverter

/**
* Delegates to the first converter that identifies itself as the handler for the given transform.
* The optional Hash is passed as the last option to the delegate's convert method.
*
* @param node - the AbstractNode to convert
* @param [transform] - the optional {string} transform, or the name of the node if no transform is specified. (optional, default: undefined)
* @param [opts] - an optional JSON that is passed as local variables to the template. (optional, default: undefined)
* @returns The {string} result from the delegate's convert method
* @memberof Converter/CompositeConverter
*/
CompositeConverter.prototype.convert = function (node, transform, opts) {
return this.$convert(node, transform, toHash(opts))
}

/**
* Converts the {AbstractNode} using only its converted content.
*
* @param {AbstractNode} node
* @returns {string} - the converted {string} content.
* @memberof Converter/CompositeConverter
*/
CompositeConverter.prototype.getContentOnly = function (node) {
return this.$content_only(node)
}

/**
* Skips conversion of the {AbstractNode}.
*
* @param {AbstractNode} node
* @memberof Converter/CompositeConverter
*/
CompositeConverter.prototype.skip = function (node) {
this.$skip(node)
}

/**
* Get the Array of Converter objects in the chain.
* @returns {[Converter]}
* @memberof Converter/CompositeConverter
*/
CompositeConverter.prototype.getConverters = function () {
return this.converters
}

/**
* Retrieve the converter for the specified transform.
* @param transform
* @returns {Converter|undefined}
* @memberof Converter/CompositeConverter
*/
CompositeConverter.prototype.getConverter = function (transform) {
const converter = this.$converter_for(transform)
return converter === Opal.nil ? undefined : converter
}

/**
* Find the converter for the specified transform.
* Throw an exception if no converter is found.
*
* @param transform
* @returns {Converter} - the matching converter
* @throws Error if no converter is found
* @memberof Converter/CompositeConverter
*/
CompositeConverter.prototype.findConverter = function (transform) {
return this.$find_converter(transform)
}
}
42 changes: 36 additions & 6 deletions packages/core/tasks/module/graalvm.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ const graalvmJavaCompileAndRun = (specName, className, javacBin, javaBin) => {
}
}

const graalvmInstallJs = (jdkInstallDir) => {
log.task('gu install nodejs')

const start = process.hrtime()

let jdkBinDir
const platform = process.platform
if (platform === 'darwin') {
jdkBinDir = path.join(jdkInstallDir, 'Contents', 'Home', 'bin')
} else {
jdkBinDir = path.join(jdkInstallDir, 'bin')
}
const guBin = path.join(jdkBinDir, 'gu')

try {
const result = childProcess.execSync(`${guBin} install nodejs`).toString('utf8')
process.stdout.write(result)
} catch (e) {
if (e.stdout) {
process.stdout.write(e.stdout.toString())
}
if (e.stderr) {
process.stderr.write(e.stderr.toString())
}
process.exit(1)
}
log.success(`Done in ${process.hrtime(start)[0]}s`)
}

const graalvmRun = (name, jdkInstallDir) => {
log.task(`run against ${name}`)

Expand All @@ -66,9 +95,9 @@ const graalvmRun = (name, jdkInstallDir) => {
const javaResult = graalvmJavaCompileAndRun(asciidoctorSpec, asciidoctorClassName, javacBin, javaBin)
graalvmCheckConvert(javaResult, `run with ${name} java`)

// Run with GraalVM node
// Run with GraalVM node.js
try {
log.info(`run ${name} node`)
log.info(`run ${name} node.js`)
const result = childProcess.execSync(`${nodeBin} spec/graalvm/run.cjs`).toString('utf8')
process.stdout.write(result)
} catch (e) {
Expand All @@ -93,7 +122,7 @@ const downloadGraalVM = async (version) => {
}
const platform = process.platform
if (platform === 'darwin' || platform === 'linux') {
await download.getContentFromURL(`https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${version}/graalvm-ce-java8-${platform}-amd64-${version}.tar.gz`, target)
await download.getContentFromURL(`https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${version}/graalvm-ce-java11-${platform}-amd64-${version}.tar.gz`, target)
if (fs.existsSync('build/graalvm')) {
log.info('build/graalvm directory already exists, skipping "untar" task')
return Promise.resolve({})
Expand All @@ -106,11 +135,12 @@ const downloadGraalVM = async (version) => {

module.exports = class GraalVM {
constructor () {
this.graalvmVersion = '20.1.0'
this.graalvmVersion = '22.2.0'
}

get () {
return downloadGraalVM(this.graalvmVersion)
async get () {
await downloadGraalVM(this.graalvmVersion)
graalvmInstallJs('./build/graalvm')
}

static run () {
Expand Down
Loading

0 comments on commit 6ecd6db

Please sign in to comment.