Commit 2f9fd247 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Génère un index si nécessaire.

parent 12e692d7
......@@ -4,6 +4,7 @@ const del = require("del");
const pandoc = require("./lib/pandoc");
const drawio = require("./lib/drawio");
const index = require("./lib/index");
const SRC_DIR = process.env.SRC || "src";
const DEST_DIR = process.env.OUTPUT || "public/";
......@@ -17,7 +18,8 @@ exports.clean = clean;
const assets = () => src(ASSET_GLOB).pipe(dest(DEST_DIR));
const graphs = () => src(GRAPH_GLOB).pipe(drawio()).pipe(dest(DEST_DIR));
const prez = () => src(PREZ_GLOB).pipe(pandoc()).pipe(dest(DEST_DIR));
const prez = () =>
src(PREZ_GLOB).pipe(index()).pipe(pandoc()).pipe(dest(DEST_DIR));
exports.graphs = graphs;
exports.assets = assets;
......
const { obj } = require("through2");
const { callbackify } = require("util");
const File = require("vinyl");
const PluginError = require("plugin-error");
/**
* @param {ReadableStream} stream
* @return {Promise<Buffer>}
*/
function streamToBuffer(stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(chunk));
stream.once("end", () => resolve(Buffer.concat(chunks)));
stream.once("error", reject);
});
}
/**
* @param {File} file
* @return {Promise<string>}
*/
async function vinylToString(file) {
if (file.isStream()) {
const buffer = await streamToBuffer(file.contents);
return buffer.toString();
}
if (file.isBuffer()) {
return file.contents.toString();
}
return "";
}
const pandocTitleBlockRegExp = /^%( .+(?:\n .+)*)?\n%( .+(?:\n .+)*)?\n%( .+)?\n/u;
/**
* @param {string} text
* @return {object|null}
*/
function parsePandocTitleBlock(text) {
const match = pandocTitleBlockRegExp.exec(text);
if (!match) {
return null;
}
return { title: match[1].trim() };
}
/**
* @param {string} text
* @return {object|null}
*/
function parseYamlTitleBlock(text) {
if (!text.startsWith("---\n")) {
return null;
}
throw new PluginError(
"index",
new Error("yaml title block is not supported"),
{
lineNumber: 1,
}
);
}
const renderIndex = (inputs) =>
`% Présentations
% Dev@Science
% ${new Date().toLocaleDateString("fr-FR", {
year: "numeric",
month: "2-digit",
day: "2-digit",
})}
# Présentations
<style>
.reveal section img.icon {vertical-align:top;margin:0;height:1em}
.reveal ul {display:flex;width:100%;max-height:50vh;flex-flow:column wrap;list-style-type:none;justify-content:center}
.reveal ul li {display:inline;width:auto}
</style>
${inputs
.map(
(input) =>
`- [${input.titleBlock.title}](${input.relative.replace(
".md",
".html"
)})` +
`[<img class="plain icon" src="file_pdf.png"/>](${
input.dirname.substr(input.base.length + 1) + ".pdf"
})`
)
.join("\n")}`;
module.exports = () => {
const inputs = [];
return obj(
callbackify(async function (input, _enc) {
const text = await vinylToString(input);
input.titleBlock = parsePandocTitleBlock(text) ||
parseYamlTitleBlock(text) || { title: input.basename };
inputs.push(input);
this.push(input);
}),
callbackify(async function () {
if (inputs.find((file) => file.relative === "index.md")) {
console.log("found index.md, bailing out");
return;
}
inputs.sort((a, b) =>
a.titleBlock.title.localeCompare(b.titleBlock.title)
);
const contents = renderIndex(inputs);
this.push(
new File({
base: "/",
path: "/index.md",
contents: Buffer.from(contents),
})
);
})
);
};
......@@ -1387,6 +1387,17 @@
"requires": {
"graceful-fs": "^4.1.11",
"through2": "^2.0.3"
},
"dependencies": {
"through2": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz",
"integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==",
"requires": {
"readable-stream": "~2.3.6",
"xtend": "~4.0.1"
}
}
}
},
"fs.realpath": {
......@@ -2110,48 +2121,6 @@
}
}
},
"gulp-exec": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/gulp-exec/-/gulp-exec-4.0.0.tgz",
"integrity": "sha512-A9JvTyB3P4huusd/43bTr6SDg3MqBxL9AQbLnsKSO6/91wVkHfxgeJZlgDMkqK8sMel4so8wcko4SZOeB1UCgA==",
"requires": {
"lodash.template": "^4.4.0",
"plugin-error": "^1.0.1",
"through2": "^3.0.1"
},
"dependencies": {
"lodash._reinterpolate": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz",
"integrity": "sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0="
},
"lodash.template": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.template/-/lodash.template-4.5.0.tgz",
"integrity": "sha512-84vYFxIkmidUiFxidA/KjjH9pAycqW+h980j7Fuz5qxRtO9pgB7MDFTdys1N7A5mcucRiDyEq4fusljItR1T/A==",
"requires": {
"lodash._reinterpolate": "^3.0.0",
"lodash.templatesettings": "^4.0.0"
}
},
"lodash.templatesettings": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/lodash.templatesettings/-/lodash.templatesettings-4.2.0.tgz",
"integrity": "sha512-stgLz+i3Aa9mZgnjr/O+v9ruKZsPsndy7qPZOchbqk2cnTU1ZaldKK+v7m54WoKIyxiuMZTKT2H81F8BeAc3ZQ==",
"requires": {
"lodash._reinterpolate": "^3.0.0"
}
},
"through2": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/through2/-/through2-3.0.1.tgz",
"integrity": "sha512-M96dvTalPT3YbYLaKaCuwu+j06D/8Jfib0o/PxbVt6Amhv3dUAtW6rTV1jPgJSBG83I/e04Y6xkVdVhSRhi0ww==",
"requires": {
"readable-stream": "2 || 3"
}
}
}
},
"gulp-pandoc": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/gulp-pandoc/-/gulp-pandoc-0.2.2.tgz",
......@@ -4075,6 +4044,17 @@
"remove-bom-buffer": "^3.0.0",
"safe-buffer": "^5.1.0",
"through2": "^2.0.3"
},
"dependencies": {
"through2": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz",
"integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==",
"requires": {
"readable-stream": "~2.3.6",
"xtend": "~4.0.1"
}
}
}
},
"remove-trailing-separator": {
......@@ -4638,12 +4618,11 @@
}
},
"through2": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz",
"integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==",
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/through2/-/through2-3.0.1.tgz",
"integrity": "sha512-M96dvTalPT3YbYLaKaCuwu+j06D/8Jfib0o/PxbVt6Amhv3dUAtW6rTV1jPgJSBG83I/e04Y6xkVdVhSRhi0ww==",
"requires": {
"readable-stream": "~2.3.6",
"xtend": "~4.0.1"
"readable-stream": "2 || 3"
}
},
"through2-filter": {
......@@ -4653,6 +4632,17 @@
"requires": {
"through2": "~2.0.0",
"xtend": "~4.0.0"
},
"dependencies": {
"through2": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz",
"integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==",
"requires": {
"readable-stream": "~2.3.6",
"xtend": "~4.0.1"
}
}
}
},
"time-stamp": {
......@@ -4718,6 +4708,17 @@
"integrity": "sha1-/JKtq6ByZHvAtn1rA2ZKoZUJOvY=",
"requires": {
"through2": "^2.0.3"
},
"dependencies": {
"through2": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz",
"integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==",
"requires": {
"readable-stream": "~2.3.6",
"xtend": "~4.0.1"
}
}
}
},
"trim-newlines": {
......@@ -4899,6 +4900,17 @@
"value-or-function": "^3.0.0",
"vinyl": "^2.0.0",
"vinyl-sourcemap": "^1.1.0"
},
"dependencies": {
"through2": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz",
"integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==",
"requires": {
"readable-stream": "~2.3.6",
"xtend": "~4.0.1"
}
}
}
},
"vinyl-sourcemap": {
......
......@@ -15,9 +15,11 @@
"dependencies": {
"del": "^5.1.0",
"gulp": "^4.0.2",
"gulp-exec": "^4.0.0",
"gulp-pandoc": "^0.2.2",
"gulp-server-livereload": "^1.9.2"
"gulp-server-livereload": "^1.9.2",
"plugin-error": "^1.0.1",
"through2": "^3.0.1",
"vinyl": "^2.2.0"
},
"devDependencies": {
"prettier": "^2.0.5"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment