|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const toProc = require('./util/to-proc') |
| 4 | + |
| 5 | +const METHOD_REGEX = /(.*)\((.*)\)/ |
| 6 | + |
| 7 | +function register (registry, context) { |
| 8 | + if (!(registry && context)) return // NOTE only works as scoped extension for now |
| 9 | + registry.$groups().$store('springio/javadoc', toProc(createExtensionGroup(context))) |
| 10 | + return registry |
| 11 | +} |
| 12 | + |
| 13 | +function createExtensionGroup () { |
| 14 | + return function () { |
| 15 | + this.inlineMacro(function () { |
| 16 | + this.named('javadoc') |
| 17 | + this.process((parent, target, attrs) => { |
| 18 | + const text = process(parent.getDocument(), parseTarget(target), attrs) |
| 19 | + return this.createInline(parent, 'quoted', text, { |
| 20 | + type: 'monospaced', |
| 21 | + }) |
| 22 | + }) |
| 23 | + }) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function parseTarget (target) { |
| 28 | + target = target.replaceAll('…​', '...') |
| 29 | + const lastSlash = target.lastIndexOf('/') |
| 30 | + const location = lastSlash !== -1 ? target.substring(0, lastSlash) : undefined |
| 31 | + const reference = lastSlash !== -1 ? target.substring(lastSlash + 1, target.length) : target |
| 32 | + const lastHash = reference.lastIndexOf('#') |
| 33 | + const classReference = lastHash !== -1 ? reference.substring(0, lastHash) : reference |
| 34 | + const anchor = lastHash !== -1 ? reference.substring(lastHash + 1, reference.length) : undefined |
| 35 | + return { location, classReference, anchor } |
| 36 | +} |
| 37 | + |
| 38 | +function process (document, target, attrs) { |
| 39 | + const location = target.location || document.getAttribute('javadoc-location', 'xref:attachment$api/java') |
| 40 | + const format = attrs.format || document.getAttribute('javadoc-format', 'short') |
| 41 | + const linkLocation = link(location, target) |
| 42 | + const linkDescription = applyFormat(target, format, attrs.$positional) |
| 43 | + return `${linkLocation}['${linkDescription}',role=apiref]` |
| 44 | +} |
| 45 | + |
| 46 | +function link (location, target) { |
| 47 | + let link = location |
| 48 | + link = !link.endsWith('/') ? link + '/' : link |
| 49 | + link += target.classReference.replaceAll('.', '/').replaceAll('$', '.') + '.html' |
| 50 | + if (target.anchor) link += '#' + target.anchor |
| 51 | + return link |
| 52 | +} |
| 53 | + |
| 54 | +function applyFormat (target, format, positionalAttrs, annotationAnchor) { |
| 55 | + if (positionalAttrs?.[0]) return positionalAttrs?.[0] |
| 56 | + switch (format) { |
| 57 | + case 'full': |
| 58 | + return className(target.classReference, 'full') + anchorText(target.anchor, format, annotationAnchor) |
| 59 | + case 'annotation': |
| 60 | + return '@' + className(target.classReference, 'short') + anchorText(target.anchor, format) |
| 61 | + default: |
| 62 | + return className(target.classReference, 'short') + anchorText(target.anchor, format) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function anchorText (anchor, format, annotationAnchor) { |
| 67 | + if (!anchor) return '' |
| 68 | + if (format === 'annotation' || annotationAnchor) anchor = anchor.replaceAll('()', '') |
| 69 | + const methodMatch = METHOD_REGEX.exec(anchor) |
| 70 | + if (methodMatch) { |
| 71 | + return '.' + methodText(methodMatch[1], methodMatch[2], format) |
| 72 | + } |
| 73 | + return '.' + anchor |
| 74 | +} |
| 75 | + |
| 76 | +function methodText (name, params, format) { |
| 77 | + const varargs = params.endsWith('...') |
| 78 | + if (varargs) params = params.substring(0, params.length - 3) |
| 79 | + return ( |
| 80 | + name + |
| 81 | + '(' + |
| 82 | + params |
| 83 | + .split(',') |
| 84 | + .map((name) => className(name, format)) |
| 85 | + .join(', ') + |
| 86 | + (!varargs ? ')' : '...)') |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +function className (classReference, format) { |
| 91 | + if (format !== 'full') classReference = classReference.split('.').slice(-1)[0] |
| 92 | + return classReference.replaceAll('$', '.') |
| 93 | +} |
| 94 | + |
| 95 | +module.exports = { register, createExtensionGroup } |
0 commit comments