From 3c969f9d4622c466542116ce3185f25de2ab357c Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Mon, 4 Nov 2024 11:48:41 +0200 Subject: [PATCH 1/3] util: generate_man - Split processDovecotMd to pre and post --- util/generate_man.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/util/generate_man.js b/util/generate_man.js index dcf3ebd3b..5c0b16482 100755 --- a/util/generate_man.js +++ b/util/generate_man.js @@ -56,7 +56,7 @@ const doInclude = (content, includes, f) => { return result } -const processDovecotMd = () => { +const processDovecotMdPost = () => { return tree => { /* Convert definition lists to base mdast elements that remark-man * can handle. */ @@ -88,7 +88,11 @@ const processDovecotMd = () => { node.node.children = [ u('blockquote', node.children) ] }) + } +} +const processDovecotMdPre = () => { + return tree => { /* Go through and replace Dovecot markdown items with man-friendly * textual equivalents. */ return map(tree, (node) => { @@ -151,9 +155,10 @@ const main = async (component, outPath) => { continue await unified(). + use(processDovecotMdPre). use(remarkParse). use(remarkDeflist). - use(processDovecotMd). + use(processDovecotMdPost). use(remarkMan, { manual: 'Dovecot', version: gitHash, From d5312bc93c3d69675bf8ff2fe8e85303f33a8b05 Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Mon, 4 Nov 2024 11:44:37 +0200 Subject: [PATCH 2/3] util: generate_man - Refactor how dovecot specific md tags are processed Since we cannot use markdown directly anymore on this level, we need to convert the dovecot nodes into proper childrens to be able to use strong, emphasis etc. formatting. --- util/generate_man.js | 94 +++++++++++++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 24 deletions(-) diff --git a/util/generate_man.js b/util/generate_man.js index 5c0b16482..1db89645c 100755 --- a/util/generate_man.js +++ b/util/generate_man.js @@ -42,7 +42,6 @@ program .parse() const debug = program.opts().debug - const doInclude = (content, includes, f) => { const result = content.replace(includesRE, (m, m1) => { if (!m1.length) return m @@ -91,33 +90,80 @@ const processDovecotMdPost = () => { } } +const DovecotMd = (md) => { + const parts = md.split(',').map((x) => x.trim()) + switch (parts[0]) { + case 'doveadm': + return [ + { + type:'strong', + children: [ + { + type:'text', value:'doveadm(1)' + } + ] + }, + { + type:'emphasis', + children: [ + { + type:'text', value:' ' + parts.slice(1).join(' ') + } + ] + } + ] + case 'man': + return [{type:'text', value:parts[1] + '(' + (parts[3] ? parts[3] : '1') + ')'}] + case 'plugin': + return [{type:'text', value:parts[1] + ' plugin documentation'}] + case 'rfc': + return [{type:'text', value:'RFC ' + parts[1]}] + case 'setting': + return [ + { + type:'strong', + children: [ + { + type:'text', value:parts[1] + } + ] + }, + { + type:'text', value:' setting' + } + ] + case 'link': + return [{type:'text', value:parts[1]}] + default: + throw new Error('unknown dovecot markdown command: ' + parts[0]) + } +} + const processDovecotMdPre = () => { return tree => { /* Go through and replace Dovecot markdown items with man-friendly * textual equivalents. */ - return map(tree, (node) => { - if (node.value) { - node.value = node.value.replace(includesDM, (m, m1) => { - if (!m1.length) return m - - const parts = m1.split(',').map((x) => x.trim()) - switch (parts[0]) { - case 'man': - return parts[1] + '(' + (parts[3] ? parts[3] : '1') + ')' - case 'plugin': - return parts[1] + ' plugin documentation' - case 'rfc': - return 'RFC ' + parts[1] - case 'setting': - return '`' + parts[1] + '`' - case 'link': - return parts[1] - default: - throw new Error('unknown dovecot markdown command: ' + parts[0]) - } - }) - } - return node + return map(tree, (para) => { + visit(para, 'text', (node, index, para) => { + /* if the text contains at least one tag, we need to convert + * this into an array of children that replace the parent's children. */ + var result; + var newChildren = [] + var pos = 0; + while ((result = includesDM.exec(node.value))) { + const pre = node.value.substr(pos, result.index - pos) + pos = result.index + result[0].length + if (pre != "") + newChildren.push({type: 'text', value: pre}) + newChildren.push(...DovecotMd(result[1])) + } + if (newChildren.length != 0) { + if (pos < node.value.length) + newChildren.push({type: 'text', value: node.value.substr(pos)}) + para.children = newChildren + } + }) + return para }) } } From 419578d67ede02d07ad622cc22ee5c0098b09c81 Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Mon, 4 Nov 2024 13:51:31 +0200 Subject: [PATCH 3/3] util: generate_man - Create output directory recursively --- util/generate_man.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/generate_man.js b/util/generate_man.js index 1db89645c..978c19eff 100755 --- a/util/generate_man.js +++ b/util/generate_man.js @@ -171,7 +171,7 @@ const processDovecotMdPre = () => { const main = async (component, outPath) => { /* Create output directory, if it doesn't exist. */ if (!fs.existsSync(outPath)) { - await fs.promises.mkdir(outPath) + await fs.promises.mkdir(outPath, { recursive: true }) } /* Generate list of man files. */