-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add javadoc block macro to extract and include HTML from JavaDoc
- Loading branch information
1 parent
c4d02f8
commit c4d4cf6
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |