Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make JSON more robust

  • Loading branch information...
commit cd5abefb5ac0e77104943cadfa0891554bee823e 1 parent bc4e1ae
@gjtorikian authored
Showing with 66 additions and 18 deletions.
  1. +40 −14 index.js
  2. +20 −0 lib/cli.js
  3. +6 −4 package.json
View
54 index.js
@@ -9,7 +9,8 @@ var conrefs = require('markdown_conrefs'),
jade = require('jade'),
wrench = require('wrench'),
funcDocs = require('functional-docs'),
- async = require('async');
+ async = require('async'),
+ cheerio = require('cheerio');
var panda_docs = exports;
var files;
@@ -76,14 +77,18 @@ panda_docs.make = exports.make = function(paths, _options, callback) {
},
function(cb) {
- var outputJson = cbReturn;
+ if (options.json) {
+ var outputJson = cbReturn;
- outputJson.baseUrl = options.baseUrl;
- outputJson.title = options.title;
+ outputJson.baseUrl = options.baseUrl;
+ outputJson.title = options.title;
- fs.writeFile(path.join(options.output, "docs.json"), JSON.stringify(outputJson, null, " "), function(err) {
- cb(err);
- });
+ fs.writeFile(path.join(options.output, slugify(options.title) + ".json"), JSON.stringify(outputJson, null, " "), function(err) {
+ cb(err);
+ });
+ }
+ else
+ cb(null);
}
], function(err, results) {
if (err) return callback(err);
@@ -115,13 +120,25 @@ function render(options, cbReturn, callback) {
fs.readFile(filepath, 'utf8', function(err, data) {
if (err) return callback(err);
- var fileObj = {};
- fileObj.filename = filename;
- fileObj.mtime = mtime;
- fileObj.pageTitle = data.split("\n")[0].substring(2);
-
Generator.render(options, jadeCompileFn, filepath, filename, data, mtime, function(err, html) {
- fileObj.contents = html;
+ var fileObj = {};
+ fileObj.filename = filename;
+ fileObj.mtime = mtime;
+ fileObj.pageTitle = data.split("\n")[0].substring(2);
+
+ if (options.contentId) {
+ var $ = cheerio.load(html);
+ fileObj.contents = $("#" + options.contentId).html();
+ }
+ else {
+ fileObj.contents = html;
+ }
+
+ if (cbReturn.toc === undefined && options.tocId) {
+ var $ = cheerio.load(html);
+ cbReturn.toc = $("#" + options.tocId).html();
+ }
+
cbReturn.files.push(fileObj);
cb(null);
});
@@ -130,4 +147,13 @@ function render(options, cbReturn, callback) {
}, function(err, results) {
callback(err);
});
-};
+};
+
+function slugify(str) {
+ str = str.toLowerCase();
+ str = str.replace(/^\s+|\s+$/g, "");
+ str = str.replace(/[\/_|\s]+/g, "-");
+ str = str.replace(/[^a-z0-9-]+/g, "");
+ str = str.replace(/[-]+/g, "-");
+ return str = str.replace(/^-+|-+$/g, "");
+}
View
20 lib/cli.js
@@ -2,6 +2,7 @@ var fs = require('fs'),
path = require('path');
var FsTools = require('fs-tools');
+var minimatch = require('minimatch');
var argparse = require('argparse');
var ArgumentParser = argparse.ArgumentParser;
@@ -122,6 +123,25 @@ cli.addArgument(['--baseUrl'], {
defaultValue: './'
});
+cli.addArgument(['--json'], {
+ help: 'Provide a JSON file alongside the HTML output',
+ dest: 'json',
+ action: 'storeTrue',
+ defaultValue: false
+});
+
+cli.addArgument(['--tocId'], {
+ help: 'The DOM ID of your TOC section (to aid in JSON creation); optional',
+ dest: 'tocId',
+ metavar: 'STRING'
+});
+
+cli.addArgument(['--contentId'], {
+ help: 'The DOM ID of your main content (to aid in JSON creation); optional',
+ dest: 'contentId',
+ metavar: 'STRING'
+});
+
cli.findFiles = function findFiles(paths, excludes, extension, callback) {
var entries = [];
View
10 package.json
@@ -1,6 +1,6 @@
{
"name": "panda-docs",
- "version": "0.4.0",
+ "version": "0.4.1",
"author": "Garen Torikian",
"keywords": ["documentation", "docs", "markdown"],
"description": "A complete documentation generation tool for Markdown files",
@@ -15,14 +15,16 @@
},
"dependencies": {
- "namp": "",
"async": "",
"wrench": "",
"jade": "",
- "argparse" : "",
+ "cheerio": "",
+
+ "namp": "",
"markdown_conrefs": "",
- "findit": "",
"functional-docs": "",
+
+ "argparse" : "",
"minimatch" : "",
"fs-tools" : ""
},
Please sign in to comment.
Something went wrong with that request. Please try again.