Last active
January 6, 2016 14:14
-
-
Save SamyPesse/cc2c132a463bc1107694 to your computer and use it in GitHub Desktop.
Summary Generation for Code-Civil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs"); | |
var path = require("path"); | |
var _ = require("lodash"); | |
var utf8 = require('utf8'); | |
require("natural-compare-lite"); | |
var IGNORE = [ | |
"node_modules", | |
".git", | |
"SUMMARY.md", | |
"README.md" | |
]; | |
function compareName(n1, n2) { | |
var n = function(_n) { | |
return _n.replace("préliminaire", "0") | |
.replace("Ier", "I"); | |
} | |
return String.naturalCompare(n(n1), n(n2)); | |
} | |
function summaryToString(articles, padding) { | |
padding = padding || 0; | |
var before = (new Array(1+padding)).join(" "); | |
return _.reduce(articles, function(out, entry) { | |
out = out + "\n"; | |
if (entry.entries) { | |
return out + before + "* "+entry.title + summaryToString(entry.entries, padding + 1); | |
} | |
return out + before + "* ["+entry.title+"]("+entry.fullPath+")"; | |
}, ""); | |
} | |
function getSummary() { | |
var articles = getArticles("./"); | |
var output = ""; | |
var group = function(_articles) { | |
return _.chain(_articles) | |
.map(function(article) { | |
var parents = article.path.split(path.sep).slice(0, -1); | |
var parent = parents.shift() || ""; | |
article.path = path.join(parents.join(path.sep), article.title); | |
article.parent = parent; | |
return article; | |
}) | |
.groupBy("parent") | |
.map(function(entries, key) { | |
var entry = { | |
title: key, | |
entries: entries | |
}; | |
var hasChild = _.find(entry.entries, function(e) { return !!e.parent; }) != null; | |
if (hasChild) entry.entries = group(entry.entries); | |
return entry; | |
}) | |
.thru(function(entries) { | |
var firstLevel = _.find(entries, { title: "" }); | |
if (firstLevel) { | |
entries = entries.concat(firstLevel.entries || []); | |
} | |
return entries; | |
}) | |
.filter(function(e) { | |
return e.title != ""; | |
}) | |
.sort(function(e1, e2) { | |
return compareName(e1.title, e2.title); | |
}) | |
.value(); | |
} | |
articles = group(articles); | |
return summaryToString(articles, 0); | |
} | |
function getArticles(folder, parents) { | |
parents = parents || []; | |
var entries = fs.readdirSync(folder); | |
entries = entries.sort(); | |
return _.chain(entries) | |
.map(function(entry) { | |
var fullPath = path.join(folder, entry); | |
var stat = fs.lstatSync(fullPath); | |
return { | |
title: path.basename(utf8.encode(entry), ".md"), | |
fullPath: fullPath, | |
name: entry, | |
isDirectory: stat.isDirectory() | |
}; | |
}) | |
.tap(function(es) { | |
es.sort(function(e1, e2) { | |
if (!e1.isDirectory && e2.isDirectory) return -1; | |
if (e1.isDirectory && !e2.isDirectory) return 1; | |
return compareName(e1.title, e2.title); | |
}); | |
}) | |
.map(function(entry) { | |
if (_.contains(IGNORE, entry.name)) { | |
return null; | |
} | |
if (entry.isDirectory) { | |
return getArticles(entry.fullPath, parents.concat([entry.title])); | |
} | |
if (path.extname(entry.name) != ".md") return null; | |
return { | |
title: entry.title, | |
path: entry.fullPath, | |
fullPath: entry.fullPath, | |
parents: parents | |
} | |
}) | |
.compact() | |
.flatten() | |
.value(); | |
} | |
var output = "# Summary\n" + getSummary() + "\n"; | |
fs.writeFileSync("SUMMARY.md", output, { | |
encoding: "utf-8" | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment