etherpad-lite/src/bin/doc/generate.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2012-08-08 20:56:56 +02:00
#!/usr/bin/env node
2021-02-01 10:47:42 +01:00
'use strict';
2012-08-08 20:56:56 +02:00
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
const fs = require('fs');
const path = require('path');
2012-08-08 20:56:56 +02:00
// parse the args.
// Don't use nopt or whatever for this. It's simple enough.
const args = process.argv.slice(2);
let format = 'json';
let template = null;
let inputFile = null;
2012-08-08 20:56:56 +02:00
args.forEach((arg) => {
2021-02-01 10:47:42 +01:00
if (!arg.match(/^--/)) {
2012-08-08 20:56:56 +02:00
inputFile = arg;
2021-02-01 10:47:42 +01:00
} else if (arg.match(/^--format=/)) {
format = arg.replace(/^--format=/, '');
} else if (arg.match(/^--template=/)) {
template = arg.replace(/^--template=/, '');
2012-08-08 20:56:56 +02:00
}
});
2012-08-08 20:56:56 +02:00
if (!inputFile) {
throw new Error('No input file specified');
}
console.error('Input file = %s', inputFile);
fs.readFile(inputFile, 'utf8', (er, input) => {
2012-08-08 20:56:56 +02:00
if (er) throw er;
// process the input for @include lines
processIncludes(inputFile, input, next);
});
2021-02-01 10:47:42 +01:00
const includeExpr = /^@include\s+([A-Za-z0-9-_/]+)(?:\.)?([a-zA-Z]*)$/gmi;
const includeData = {};
2021-02-01 10:47:42 +01:00
const processIncludes = (inputFile, input, cb) => {
const includes = input.match(includeExpr);
2021-02-01 10:47:42 +01:00
if (includes == null) return cb(null, input);
let errState = null;
2012-08-08 20:56:56 +02:00
console.error(includes);
let incCount = includes.length;
2012-08-08 20:56:56 +02:00
if (incCount === 0) cb(null, input);
includes.forEach((include) => {
let fname = include.replace(/^@include\s+/, '');
2012-08-08 20:56:56 +02:00
if (!fname.match(/\.md$/)) fname += '.md';
2021-02-01 10:47:42 +01:00
if (Object.prototype.hasOwnProperty.call(includeData, fname)) {
2012-08-08 20:56:56 +02:00
input = input.split(include).join(includeData[fname]);
incCount--;
if (incCount === 0) {
return cb(null, input);
}
}
const fullFname = path.resolve(path.dirname(inputFile), fname);
fs.readFile(fullFname, 'utf8', (er, inc) => {
2012-08-08 20:56:56 +02:00
if (errState) return;
if (er) return cb(errState = er);
processIncludes(fullFname, inc, (er, inc) => {
2012-08-08 20:56:56 +02:00
if (errState) return;
if (er) return cb(errState = er);
incCount--;
includeData[fname] = inc;
input = input.split(include).join(includeData[fname]);
if (incCount === 0) {
return cb(null, input);
}
});
});
});
2021-02-01 10:47:42 +01:00
};
2012-08-08 20:56:56 +02:00
2021-02-01 10:47:42 +01:00
const next = (er, input) => {
2012-08-08 20:56:56 +02:00
if (er) throw er;
switch (format) {
case 'json':
require('./json.js')(input, inputFile, (er, obj) => {
2012-08-08 20:56:56 +02:00
console.log(JSON.stringify(obj, null, 2));
if (er) throw er;
});
break;
case 'html':
require('./html.js')(input, inputFile, template, (er, html) => {
2012-08-08 20:56:56 +02:00
if (er) throw er;
console.log(html);
});
break;
default:
throw new Error(`Invalid format: ${format}`);
2012-08-08 20:56:56 +02:00
}
2021-02-01 10:47:42 +01:00
};