Skip to content

Commit

Permalink
add javadoc block macro to extract and include HTML from JavaDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed May 1, 2024
1 parent c4d02f8 commit c4d4cf6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions antora-playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ asciidoc:
page-pagination: ''
extensions:
- ./lib/feed-block-macro.js
- ./lib/javadoc-block-macro.js
- ./lib/jetty-block.js
- ./lib/skip-include-processor.js
- ./lib/absolute-path-include-processor.js
Expand Down
53 changes: 53 additions & 0 deletions lib/javadoc-block-macro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

const toHash = (object) => object && !object.$$is_hash ? Opal.hash2(Object.keys(object), object) : object

const toProc = (fn) => Object.defineProperty(fn, '$$arity', { value: fn.length })

function register (registry, context = {}) {
if (!(registry && context)) return // NOTE only works as scoped extension
registry.$groups().$store('javadoc', toProc(createExtensionGroup(context)))
return registry
}

function createExtensionGroup ({ contentCatalog, file }) {
return function () {
this.blockMacro('javadoc', function () {
this.process((parent, target, attrs) => {
const doc = parent.getDocument()
const cursor = doc.getReader().$cursor_at_mark()
const ctx = (cursor.file || {}).src || file.src
const includeFile = contentCatalog.resolveResource(target, ctx, undefined, ['partial'])
if (!includeFile) return this.createParagraph(parent, `javadoc::${target}[]`, {})
const rawLines = includeFile.contents.toString().split('\n')
const lines = []
let capturing
for (let line of rawLines) {
if (capturing) {
if (line.endsWith('<!-- end::documentation[] -->')) break
line = line.replace(/^ +[*](?: +|$)/, '')
if (line === '<table>') {
line = '<table class="tableblock frame-all grid-all stretch">'
} else if (line === '<dl>') {
lines.push('<div class="dlist">')
} else if (line === '</dl>') {
lines.push(line)
line = '</div>'
} else if (line.startsWith('<p') && !lines.length) {
line = '<div class="paragraph">\n' + line + '\n</div>'
} else if (line.startsWith('<!--')) {
continue
}
lines.push(line)
} else if (line.endsWith('<!-- tag::documentation[] -->')) {
capturing = true
continue
}
}
return this.createPassBlock(parent, lines, {})
})
})
}
}

module.exports = { register, createExtensionGroup }

0 comments on commit c4d4cf6

Please sign in to comment.