This repository has been archived on 2021-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
scriptcraft/src/docs/js/generateTOC.js
Tony Gravagno 7b6bed6fa1 generateTOC has preliminary disabled code for evaluation by Walter. Would like to know if it's worth pursuing.
blocks.js has many significant updates. Note "breaking fix" of a "redston" item for "redstone". Existing apps will need to correct this if they use it.
2016-12-09 16:22:39 -06:00

73 lines
2 KiB
JavaScript

args = Array.prototype.slice.call(args,1);
// wph 20140105 trim not availabe in String on Mac OS.
if (typeof String.prototype.trim == 'undefined'){
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g,'');
};
}
var template = args[0];
var BufferedReader = java.io.BufferedReader;
var FileReader = java.io.FileReader;
var contents = [], line = undefined;
var br = new BufferedReader( new FileReader(template) );
while ( (line = br.readLine()) != null){
contents.push(line);
}
br.close();
var anchors = {};
var createLink = function(text){
var result = text.replace(/_/g,'_');
result = result.replace(/[^a-zA-Z0-9 _\-]/g,'');
result = result.replace(/ /g,'-');
var result = result.toLowerCase();
if (anchors[result]){
result = result + '-' + (anchors[result]++);
}
anchors[result] = 1;
return result;
};
java.lang.System.out.println('## Table of Contents');
for (var i = 0; i < contents.length; i++){
line = contents[i];
if (line.match(/^##\s+/)){
var h2 = line.match(/^##\s+(.*)/)[1].trim();
var link = createLink(h2);
java.lang.System.out.println(' * [' + h2 + '](#' + link + ')');
}
if (line.match(/^###\s+/)){
var h3 = line.match(/^###\s+(.*)/)[1].trim();
var link = createLink(h3);
java.lang.System.out.println(' * [' + h3 + '](#' + link + ')');
}
}
/**
* // tg 20161022 Sort secondary topics, with some exceptions
* // Still work in progress. Ignore
* var sortedContents =[];
* var sortedTemp = [];
*
* for (var i = 0; i < contents.length; i++) {
* line = contents[i];
* if(line.match(/^##\s+/)) {
* if (sortedTemp.length > 0) {
* sortedContents.push(sortedTemp.sort());
* sortedContents.push(line);
* } else {
* if (line.match(/^###\s+/)) {
* sortedTemp.push(line);
* } else {
* sortedContents.push(line);
* }
* }
* }
* contents = sortedContents;
*/