Skip to content

Commit

Permalink
Improve the conversion of .hlp files into Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
monadius committed Oct 19, 2024
1 parent 6981e1a commit fd6a19e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,34 @@ class HelpItem {
constructor(doc: string) {
let sectionName = '';
let sectionLines: string[] = [];
const processLine = (line: string) => {
// Rules from `doc-to-help.sed` + additional rules for Markdown
return line.replace(/\\noindent\s*\b/g, '')
.replace(/``(.*?)''/g, '“$1”')
.replace(/\{\{/g, '<<<<<<')
.replace(/\}\}/g, '>>>>>>')
.replace(/(`?)\{(.*?)\}('?)/g, (_, a, text, b) => `\`\`${text.startsWith('`') ? ' ' : ''}${text}${text.endsWith('`') ? ' ' : ''}\`\`${a ? '' : b}`)
.replace(/<<<<<</g, '{')
.replace(/>>>>>>/g, '}')
.replace(/\{\\em (.*?)\}/g, '**$1**')
.replace(/\\begin\{itemize\}|\\end\{itemize\}/g, '')
.replace(/\\item\b/g, '*');
};
const addSection = () => {
if (!sectionName) {
return;
}
while (sectionLines.length && !sectionLines.at(-1)?.trim()) {
sectionLines.pop();
}
const text = sectionLines.map(line => line.replace(/\{([^}]*)\}/g, '`$1`')).join('\n');
const text = sectionLines.map(processLine).join('\n');
this.sections[sectionName] = text;
sectionName = '';
sectionLines = [];
};

for (const line of doc.split('\n')) {
const m = line.match(/^\\(\S+)\s*(.*)/);
const m = line.match(/^\\([A-Z_\d]+)\s*(.*)/);
if (m) {
if (m[1] === 'ENDDOC') {
break;
Expand Down

0 comments on commit fd6a19e

Please sign in to comment.