Skip to content
Snippets Groups Projects
Commit 9f47b13e authored by Guillaume Grossetie's avatar Guillaume Grossetie :headphones:
Browse files

resolves #12 replace the sample adapter

parent f01550cc
No related branches found
No related tags found
1 merge request!14resolves #12 replace the sample adapter
Pipeline #32433 passed
import asciidoctor from '@asciidoctor/core'
const Asciidoctor = asciidoctor()
function toDocumentObjectModel (doc, text) {
const blocks = doc.getBlocks().map((block) => toNode(block))
const attributes = doc.attributes_modified.$to_a().reduce((accum, attr) => {
accum[attr] = doc.getAttribute(attr)
return accum
}, {})
return {
name: 'document',
type: 'block',
...(doc.hasHeader() && {
attributes,
header: {
title: {
inlines: [{ name: 'text', type: 'string', value: doc.getDocumentTitle() }],
},
},
}),
blocks,
}
}
function toNode (block) {
const name = block.getNodeName()
const type = 'block'
if (block.$content_model() === 'simple') {
const attributes = {}
const id = block.getId()
if (id) attributes.id = id
const subs = block.subs.filter((sub) => sub !== 'replacements')
return {
name,
type,
...(Object.keys(attributes).length > 0 && { attributes }),
inlines: toInlines(block.lines.map((l) => block.applySubstitutions(l, subs)).join(' ')),
}
}
if (name === 'paragraph') {
const subs = block.subs.filter((sub) => sub !== 'replacements')
return {
name,
type,
inlines: [block.lines.map((l) => block.applySubstitutions(l, subs)).join(' ')],
}
}
if (block.getNodeName() === 'section') {
return {
name,
type,
title: {
inlines: [{ name: 'text', type: 'string', value: block.getTitle() }],
},
level: block.getLevel(),
blocks: block.getBlocks().map((block) => toNode(block)),
}
}
if (block.getNodeName() === 'listing' || block.getNodeName() === 'literal') {
const attributes = block.getAttributes()
const style = block.getStyle()
if (style === block.getNodeName()) delete attributes.style
//const title = block.getTitle()
delete attributes.$positional
return {
name,
type,
...(Object.keys(attributes).length > 0 && { attributes }),
inlines: [
{
name: 'text',
type: 'string',
value: block.lines.join('\n'),
},
],
}
}
if (block.getNodeName() === 'list_item') {
const blocks = block.getBlocks()
return {
name: 'listItem',
type,
marker: block.getMarker(),
principal: {
inlines: [{ name: 'text', type: 'string', value: block.getText() }],
},
blocks: blocks.map((block) => toNode(block)),
}
}
if (block.getNodeName() === 'ulist') {
return {
name: 'list',
type,
marker: block.getItems()[0].getMarker(),
variant: 'unordered',
items: block.getBlocks().map((block) => toNode(block)),
}
}
const attributes = {}
const id = block.getId()
if (id) attributes.id = id
return {
type,
...(Object.keys(attributes).length > 0 && { attributes }),
blocks: block.getBlocks().map((block) => toNode(block)),
}
}
function toInlines (str) {
if (str.startsWith('<strong>') && str.endsWith('</strong>')) {
return [
{ name: 'span', type: 'inline', variant: 'strong', form: 'constrained', inlines: toInlines(str.slice(8, -9)) },
]
}
return [{ name: 'text', type: 'string', value: str }]
}
export default class AsciidoctorParser {
parse (text) {
const doc = Asciidoctor.load(text, { safe: 'safe' })
// fixme: transform doc into a generic DOM
const result = toDocumentObjectModel(doc, text)
return result
}
}
#!/usr/bin/env node #!/usr/bin/env node
import AsciidoctorParser from './parser.js' import process from 'node:process'
import ospath from 'node:path'
import fsp from 'node:fs/promises'
import { once } from 'node:events' import { once } from 'node:events'
process.title = 'asciidoctor' process.title = 'Echo AsciiDoc TCK Adapter'
/**
* @returns {Promise<string>}
*/
const readFromStdin = async () => const readFromStdin = async () =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const encoding = 'utf-8' const encoding = 'utf-8'
...@@ -29,7 +34,9 @@ const readFromStdin = async () => ...@@ -29,7 +34,9 @@ const readFromStdin = async () =>
;(async () => { ;(async () => {
const content = await readFromStdin() const content = await readFromStdin()
const asciidoctorParser = new AsciidoctorParser() const payload = JSON.parse(content)
process.stdout.write(JSON.stringify(asciidoctorParser.parse(content))) const outputFile = ospath.join(payload.path.replace(/-input\.adoc$/, '-output.json'))
const output = await fsp.readFile(outputFile, 'utf8')
process.stdout.write(output)
await once(process.stdout.end(), 'close') await once(process.stdout.end(), 'close')
})() })()
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
"asciidoc-tck": "harness/bin/asciidoc-tck.js" "asciidoc-tck": "harness/bin/asciidoc-tck.js"
}, },
"devDependencies": { "devDependencies": {
"@asciidoctor/core": "2.2.6",
"c8": "~7.12", "c8": "~7.12",
"eslint": "~8.49", "eslint": "~8.49",
"eslint-config-standard": "~17.1", "eslint-config-standard": "~17.1",
...@@ -39,21 +38,6 @@ ...@@ -39,21 +38,6 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/@asciidoctor/core": {
"version": "2.2.6",
"resolved": "https://registry.npmjs.org/@asciidoctor/core/-/core-2.2.6.tgz",
"integrity": "sha512-TmB2K5UfpDpSbCNBBntXzKHcAk2EA3/P68jmWvmJvglVUdkO9V6kTAuXVe12+h6C4GK0ndwuCrHHtEVcL5t6pQ==",
"dev": true,
"dependencies": {
"asciidoctor-opal-runtime": "0.3.3",
"unxhr": "1.0.1"
},
"engines": {
"node": ">=8.11",
"npm": ">=5.0.0",
"yarn": ">=1.1.0"
}
},
"node_modules/@bcoe/v8-coverage": { "node_modules/@bcoe/v8-coverage": {
"version": "0.2.3", "version": "0.2.3",
"resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz", "resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz",
...@@ -527,19 +511,6 @@ ...@@ -527,19 +511,6 @@
"resolved": "", "resolved": "",
"link": true "link": true
}, },
"node_modules/asciidoctor-opal-runtime": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/asciidoctor-opal-runtime/-/asciidoctor-opal-runtime-0.3.3.tgz",
"integrity": "sha512-/CEVNiOia8E5BMO9FLooo+Kv18K4+4JBFRJp8vUy/N5dMRAg+fRNV4HA+o6aoSC79jVU/aT5XvUpxSxSsTS8FQ==",
"dev": true,
"dependencies": {
"glob": "7.1.3",
"unxhr": "1.0.1"
},
"engines": {
"node": ">=8.11"
}
},
"node_modules/assertion-error": { "node_modules/assertion-error": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
...@@ -3477,15 +3448,6 @@ ...@@ -3477,15 +3448,6 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/unxhr": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/unxhr/-/unxhr-1.0.1.tgz",
"integrity": "sha512-MAhukhVHyaLGDjyDYhy8gVjWJyhTECCdNsLwlMoGFoNJ3o79fpQhtQuzmAE4IxCMDwraF4cW8ZjpAV0m9CRQbg==",
"dev": true,
"engines": {
"node": ">=8.11"
}
},
"node_modules/uri-js": { "node_modules/uri-js": {
"version": "4.4.1", "version": "4.4.1",
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
......
...@@ -43,7 +43,6 @@ ...@@ -43,7 +43,6 @@
"mocha": "~10.0" "mocha": "~10.0"
}, },
"devDependencies": { "devDependencies": {
"@asciidoctor/core": "2.2.6",
"c8": "~7.12", "c8": "~7.12",
"eslint": "~8.49", "eslint": "~8.49",
"eslint-config-standard": "~17.1", "eslint-config-standard": "~17.1",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment