Skip to content

Commit 2710d17

Browse files
committed
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.
1 parent d76c670 commit 2710d17

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

util/generate_man.js

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ program
4242
.parse()
4343
const debug = program.opts().debug
4444

45-
4645
const doInclude = (content, includes, f) => {
4746
const result = content.replace(includesRE, (m, m1) => {
4847
if (!m1.length) return m
@@ -91,33 +90,53 @@ const processDovecotMdPost = () => {
9190
}
9291
}
9392

93+
const DovecotMd = (md) => {
94+
const parts = md.split(',').map((x) => x.trim())
95+
switch (parts[0]) {
96+
case 'doveadm':
97+
return [{type:'strong', children:[{type:'text', value:'doveadm(1)'}]},
98+
{type:'emphasis', children:[{type:'text', value:' ' + parts.slice(1).join(' ')}]}]
99+
case 'man':
100+
return [{type:'text', value:parts[1] + '(' + (parts[3] ? parts[3] : '1') + ')'}]
101+
case 'plugin':
102+
return [{type:'text', value:parts[1] + ' plugin documentation'}]
103+
case 'rfc':
104+
return [{type:'text', value:'RFC ' + parts[1]}]
105+
case 'setting':
106+
return [{type:'strong', children:[{type:'text', value:parts[1]}]},
107+
{type:'text', value:' setting'}]
108+
case 'link':
109+
return [{type:'text', value:parts[1]}]
110+
default:
111+
throw new Error('unknown dovecot markdown command: ' + parts[0])
112+
}
113+
}
114+
94115
const processDovecotMdPre = () => {
95116
return tree => {
96117
/* Go through and replace Dovecot markdown items with man-friendly
97118
* textual equivalents. */
98-
return map(tree, (node) => {
99-
if (node.value) {
100-
node.value = node.value.replace(includesDM, (m, m1) => {
101-
if (!m1.length) return m
102-
103-
const parts = m1.split(',').map((x) => x.trim())
104-
switch (parts[0]) {
105-
case 'man':
106-
return parts[1] + '(' + (parts[3] ? parts[3] : '1') + ')'
107-
case 'plugin':
108-
return parts[1] + ' plugin documentation'
109-
case 'rfc':
110-
return 'RFC ' + parts[1]
111-
case 'setting':
112-
return '`' + parts[1] + '`'
113-
case 'link':
114-
return parts[1]
115-
default:
116-
throw new Error('unknown dovecot markdown command: ' + parts[0])
117-
}
118-
})
119-
}
120-
return node
119+
return map(tree, (para) => {
120+
visit(para, 'text', (node, index, para) => {
121+
/* if the text contains at least one tag, we need to convert
122+
* this into a bunch of children that replace the parent's children. */
123+
var result;
124+
var newChildren = []
125+
var pos = 0;
126+
while ((result = includesDM.exec(node.value))) {
127+
const pre = node.value.substr(pos, result.index - pos)
128+
pos = result.index + result[0].length
129+
if (pre != "")
130+
newChildren.push({type: 'text', value: pre})
131+
newChildren.push(...DovecotMd(result[1]))
132+
}
133+
if (newChildren.length != 0) {
134+
if (pos < node.value.length)
135+
newChildren.push({type: 'text', value: node.value.substr(pos)})
136+
para.children = newChildren
137+
}
138+
})
139+
return para
121140
})
122141
}
123142
}

0 commit comments

Comments
 (0)