1
0
mirror of https://github.com/twirl/The-API-Book.git synced 2025-01-23 17:53:04 +02:00
The-API-Book/build.js

142 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-11-09 00:31:31 +03:00
const fs = require('fs');
2020-12-08 15:43:01 +03:00
const path = require('path');
2020-12-20 21:10:52 +03:00
const templates = require('./src/templates');
2020-12-20 14:31:45 +03:00
const builders = require('./src/lib/builders');
const mdHtml = require('./src/lib/md-html');
const htmlProcess = require('./src/lib/html-process');
2020-12-08 15:43:01 +03:00
2020-12-06 22:16:52 +03:00
const l10n = {
2020-12-20 21:10:52 +03:00
en: require('./src/en/l10n.json'),
ru: require('./src/ru/l10n.json')
2020-12-06 22:16:52 +03:00
};
2020-12-09 00:20:54 +03:00
2020-12-20 14:31:45 +03:00
const langsToBuild = process.argv[2] &&
process.argv[2].split(',').map((s) => s.trim()) ||
['ru', 'en'];
const targets = (process.argv[3] &&
process.argv[3].split(',') ||
['html', 'pdf', 'epub']
).reduce((targets, arg) => {
targets[arg.trim()] = true;
return targets;
}, {});
2020-12-06 22:16:52 +03:00
2020-12-08 15:43:01 +03:00
buildDocs(langsToBuild, targets, l10n).then(() => {
2020-12-07 12:23:55 +03:00
console.log('Done!');
2020-12-06 22:16:52 +03:00
process.exit(0);
}, (e) => {
console.error(e);
process.exit(255);
});
2020-12-08 15:43:01 +03:00
function buildDocs (langsToBuild, targets, l10n) {
console.log(`Building in following languages: ${
langsToBuild.join(', ')
}, targets: ${
Object.keys(targets).join(', ')
}`);
2020-12-06 22:16:52 +03:00
return Promise.all(
2020-12-08 15:43:01 +03:00
langsToBuild.map((lang) => buildDoc(lang, targets, l10n[lang]))
2020-12-06 22:16:52 +03:00
);
}
2020-11-09 00:31:31 +03:00
2020-12-20 14:31:45 +03:00
async function buildDoc (lang, targets, l10n) {
2020-12-20 21:10:52 +03:00
const pageBreak = templates.pageBreak;
2020-12-20 14:31:45 +03:00
const structure = await getStructure({
2020-12-06 22:16:52 +03:00
path: `./src/${lang}/clean-copy/`,
l10n,
pageBreak
2020-12-08 15:43:01 +03:00
});
2020-12-20 21:10:52 +03:00
const tableOfContents = templates.toc(structure, l10n);
2020-12-08 15:43:01 +03:00
const htmlContent = [
structure.frontPage,
tableOfContents,
2020-12-08 15:43:01 +03:00
...structure.sections
.map((section) => section.chapters.reduce((content, chapter) => {
if (chapter.title) {
2020-12-20 21:10:52 +03:00
content.push(templates.chapterTitle(chapter));
2020-12-08 15:43:01 +03:00
}
content.push(chapter.content);
return content;
2020-12-20 21:10:52 +03:00
}, [templates.sectionTitle(section)]).join(''))
2020-12-20 14:31:45 +03:00
];
2020-11-09 00:31:31 +03:00
2020-12-08 15:43:01 +03:00
return Promise.all(['html', 'pdf', 'epub'].map((target) => {
2021-01-04 02:33:33 +03:00
if (targets[target]) {
return prepareHtml(htmlContent.join(''), l10n, target).then((html) => {
return builders[target]({
lang,
structure,
html,
l10n,
path: path.join(__dirname, 'docs', `API.${lang}.${target}`)
});
});
} else {
return Promise.resolve();
}
2020-12-08 15:43:01 +03:00
}));
2020-12-06 22:16:52 +03:00
}
2020-11-09 00:31:31 +03:00
2020-12-20 14:31:45 +03:00
async function getStructure ({ path, l10n, pageBreak}) {
2020-12-08 15:43:01 +03:00
const structure = {
frontPage: fs.readFileSync(`${path}intro.html`, 'utf-8') + pageBreak,
sections: []
};
2020-11-09 00:31:31 +03:00
let counter = 1;
2020-12-20 14:31:45 +03:00
await fs.readdirSync(path)
2020-11-09 00:31:31 +03:00
.filter((p) => fs.statSync(`${path}${p}`).isDirectory())
.sort()
2020-12-20 14:31:45 +03:00
.reduce(async (p, dir, index) => {
const structure = await p;
2020-11-09 00:31:31 +03:00
const name = dir.split('-')[1];
2020-12-08 15:43:01 +03:00
const section = {
title: name,
anchor: `section-${index + 1}`,
2020-12-08 15:43:01 +03:00
chapters: []
}
2020-11-09 00:31:31 +03:00
const subdir = `${path}${dir}/`;
2020-12-20 14:31:45 +03:00
await fs.readdirSync(subdir)
2020-11-09 00:31:31 +03:00
.filter((p) => fs.statSync(`${subdir}${p}`).isFile() && p.indexOf('.md') == p.length - 3)
.sort()
2020-12-20 14:31:45 +03:00
.reduce(async (p, file) => {
const section = await p;
2020-12-08 15:43:01 +03:00
const md = fs.readFileSync(`${subdir}${file}`, 'utf-8').trim();
2020-12-20 14:31:45 +03:00
const content = await mdHtml(md, {
counter,
l10n,
base: __dirname
});
2020-12-08 15:43:01 +03:00
section.chapters.push({
2020-12-20 14:31:45 +03:00
anchor: content.data.anchor,
title: content.data.title,
content: content.contents + pageBreak
2020-12-08 15:43:01 +03:00
});
counter++;
2020-12-20 14:31:45 +03:00
return section;
}, Promise.resolve(section));
2020-12-08 15:43:01 +03:00
structure.sections.push(section);
2020-12-20 14:31:45 +03:00
return structure;
}, Promise.resolve(structure));
2020-12-08 15:43:01 +03:00
return structure;
2021-01-04 02:33:33 +03:00
}
async function prepareHtml (content, l10n, target) {
if (target == 'epub') {
return '';
} else {
return (await htmlProcess(
templates[target == 'html' ? 'screenHtml' : 'printHtml'](content, l10n), {
base: __dirname
}
)).contents;
}
2020-12-06 22:16:52 +03:00
}