2020-11-08 23:31:31 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
const mdHtml = new (require('showdown').Converter)();
|
2020-12-08 14:43:01 +02:00
|
|
|
const path = require('path');
|
|
|
|
|
2020-12-07 11:23:55 +02:00
|
|
|
const langsToBuild = process.argv[2] &&
|
|
|
|
process.argv[2].split(',').map((s) => s.trim()) ||
|
|
|
|
['ru', 'en'];
|
2020-12-08 14:43:01 +02:00
|
|
|
|
|
|
|
const targets = (process.argv[3] &&
|
|
|
|
process.argv[3].split(',') ||
|
|
|
|
['html', 'pdf', 'epub']
|
|
|
|
).reduce((targets, arg) => {
|
|
|
|
targets[arg.trim()] = true;
|
|
|
|
return targets;
|
|
|
|
}, {});
|
|
|
|
|
2020-12-06 21:16:52 +02:00
|
|
|
const l10n = {
|
|
|
|
en: {
|
2020-12-08 14:43:01 +02:00
|
|
|
title: 'The API',
|
2020-12-06 21:16:52 +02:00
|
|
|
author: 'Sergey Konstantinov',
|
2020-12-08 14:43:01 +02:00
|
|
|
chapter: 'Chapter',
|
|
|
|
toc: 'Table of Contents',
|
|
|
|
frontPage: 'Front Page'
|
2020-11-08 23:31:31 +02:00
|
|
|
},
|
2020-12-06 21:16:52 +02:00
|
|
|
ru: {
|
2020-12-08 14:43:01 +02:00
|
|
|
title: 'API',
|
2020-12-06 21:16:52 +02:00
|
|
|
author: 'Сергей Константинов',
|
2020-12-08 14:43:01 +02:00
|
|
|
chapter: 'Глава',
|
|
|
|
toc: 'Содержание',
|
|
|
|
frontPage: 'Титульный лист'
|
2020-12-06 21:16:52 +02:00
|
|
|
}
|
|
|
|
};
|
2020-12-08 23:20:54 +02:00
|
|
|
const css = fs.readFileSync('src/style.css', 'utf-8');
|
|
|
|
|
2020-12-08 14:43:01 +02:00
|
|
|
const builders = require('./builders');
|
2020-12-06 21:16:52 +02:00
|
|
|
|
2020-12-08 14:43:01 +02:00
|
|
|
buildDocs(langsToBuild, targets, l10n).then(() => {
|
2020-12-07 11:23:55 +02:00
|
|
|
console.log('Done!');
|
2020-12-06 21:16:52 +02:00
|
|
|
process.exit(0);
|
|
|
|
}, (e) => {
|
|
|
|
console.error(e);
|
|
|
|
process.exit(255);
|
|
|
|
});
|
|
|
|
|
2020-12-08 14:43:01 +02:00
|
|
|
function buildDocs (langsToBuild, targets, l10n) {
|
|
|
|
console.log(`Building in following languages: ${
|
|
|
|
langsToBuild.join(', ')
|
|
|
|
}, targets: ${
|
|
|
|
Object.keys(targets).join(', ')
|
|
|
|
}`);
|
|
|
|
|
2020-12-06 21:16:52 +02:00
|
|
|
return Promise.all(
|
2020-12-08 14:43:01 +02:00
|
|
|
langsToBuild.map((lang) => buildDoc(lang, targets, l10n[lang]))
|
2020-12-06 21:16:52 +02:00
|
|
|
);
|
|
|
|
}
|
2020-11-08 23:31:31 +02:00
|
|
|
|
2020-12-08 14:43:01 +02:00
|
|
|
function buildDoc (lang, targets, l10n) {
|
|
|
|
const structure = getStructure({
|
2020-12-06 21:16:52 +02:00
|
|
|
path: `./src/${lang}/clean-copy/`,
|
|
|
|
l10n,
|
|
|
|
pageBreak:'<div class="page-break"></div>'
|
2020-12-08 14:43:01 +02:00
|
|
|
});
|
|
|
|
const htmlContent = [
|
|
|
|
structure.frontPage,
|
|
|
|
...structure.sections
|
|
|
|
.map((section) => section.chapters.reduce((content, chapter) => {
|
|
|
|
if (chapter.title) {
|
|
|
|
content.push(`<h3>${chapter.title}</h3>`);
|
|
|
|
}
|
|
|
|
content.push(chapter.content);
|
|
|
|
return content;
|
|
|
|
}, [section.title ? `<h2>${section.title}</h2>` : '']).join(''))
|
|
|
|
].join('\n');
|
2020-11-08 23:31:31 +02:00
|
|
|
|
2020-12-06 21:16:52 +02:00
|
|
|
const html = `<html><head>
|
|
|
|
<meta charset="utf-8"/>
|
2020-12-08 14:43:01 +02:00
|
|
|
<title>${l10n.author}. ${l10n.title}</title>
|
2020-12-06 21:16:52 +02:00
|
|
|
<meta name="author" content="${l10n.author}"/>
|
2020-12-07 11:23:55 +02:00
|
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=PT+Serif&family=PT+Sans&family=Inconsolata"/>
|
2020-12-08 23:20:54 +02:00
|
|
|
<style>${css}</style>
|
2020-12-06 21:16:52 +02:00
|
|
|
</head><body>
|
2020-12-08 14:43:01 +02:00
|
|
|
<article>${htmlContent}</article>
|
2020-12-06 21:16:52 +02:00
|
|
|
</body></html>`;
|
|
|
|
|
2020-12-08 14:43:01 +02:00
|
|
|
return Promise.all(['html', 'pdf', 'epub'].map((target) => {
|
|
|
|
return targets[target] ? builders[target]({
|
|
|
|
lang,
|
|
|
|
structure,
|
|
|
|
html,
|
|
|
|
l10n,
|
|
|
|
path: path.join(__dirname, 'docs', `API.${lang}.${target}`)
|
|
|
|
}) : Promise.resolve();
|
|
|
|
}));
|
2020-12-06 21:16:52 +02:00
|
|
|
}
|
2020-11-08 23:31:31 +02:00
|
|
|
|
2020-12-08 14:43:01 +02:00
|
|
|
function getStructure ({ path, l10n: { chapter }, pageBreak}) {
|
|
|
|
const structure = {
|
|
|
|
frontPage: fs.readFileSync(`${path}intro.html`, 'utf-8') + pageBreak,
|
|
|
|
sections: []
|
|
|
|
};
|
2020-11-08 23:31:31 +02:00
|
|
|
let counter = 1;
|
|
|
|
fs.readdirSync(path)
|
|
|
|
.filter((p) => fs.statSync(`${path}${p}`).isDirectory())
|
|
|
|
.sort()
|
|
|
|
.forEach((dir) => {
|
|
|
|
const name = dir.split('-')[1];
|
2020-12-08 14:43:01 +02:00
|
|
|
const section = {
|
|
|
|
title: name,
|
|
|
|
chapters: []
|
|
|
|
}
|
2020-11-08 23:31:31 +02:00
|
|
|
|
|
|
|
const subdir = `${path}${dir}/`;
|
|
|
|
fs.readdirSync(subdir)
|
|
|
|
.filter((p) => fs.statSync(`${subdir}${p}`).isFile() && p.indexOf('.md') == p.length - 3)
|
|
|
|
.sort()
|
|
|
|
.forEach((file) => {
|
2020-12-08 14:43:01 +02:00
|
|
|
const md = fs.readFileSync(`${subdir}${file}`, 'utf-8').trim();
|
|
|
|
const [ title, ...paragraphs ] = md.split(/\r?\n/);
|
|
|
|
section.chapters.push({
|
|
|
|
title: title.replace(/^### /, `${chapter} ${counter++}. `),
|
|
|
|
content: mdHtml.makeHtml(paragraphs.join('\n')) + pageBreak
|
|
|
|
});
|
2020-11-08 23:31:31 +02:00
|
|
|
});
|
2020-12-08 14:43:01 +02:00
|
|
|
|
|
|
|
structure.sections.push(section);
|
2020-11-08 23:31:31 +02:00
|
|
|
});
|
|
|
|
|
2020-12-08 14:43:01 +02:00
|
|
|
return structure;
|
2020-12-06 21:16:52 +02:00
|
|
|
}
|