", distance: "3"}
// Returns: [newContent after Split, Array of extracted ]
function splitBy(content, searchTerm, endSplitTerm){
let foundArray = [];
let curIndex = -1;
let afterCurSearchIndex = -1
let negateTermIndex = -1;
let nextIndex = 0;
let findbyNameLength = searchTerm.length;
let curfoundArrayIndex = 0;
let curEndSplitTermIndex = -1;
do {
curEndSplitTermIndex = content.indexOf(endSplitTerm);
curIndex = content.indexOf(searchTerm);
afterCurSearchIndex = curIndex+findbyNameLength;
nextIndex = content.indexOf(searchTerm,afterCurSearchIndex);
if (nextIndex === -1){
nextIndex = curEndSplitTermIndex;
}
foundArray.push(content.slice(curIndex, nextIndex))
// remove that content
content = content.replace(foundArray[curfoundArrayIndex], "");
curfoundArrayIndex++;
} while (curIndex > -1)
return [content, foundArray];
}
// Split the signals, methods, and typeDefs [Might make this more generic]
function splitMethodsSignalsAndTypeDefs(allItemToSplit){
let methodArray = [];
let signalArray = [];
let typeDefArray = [];
console.log(allItemToSplit.length);
allItemToSplit.forEach( method => {
firstLine = method.split("\n")[0];
if (firstLine.indexOf("Signal") > -1){
console.log("Found signal")
signalArray.push(method);
} else if (firstLine.indexOf("span") > -1) {
// console.log("Found method")
methodArray.push(method);
} else {
// console.log("Found typeDef")
typeDefArray.push(method);
}
})
return [methodArray, signalArray, typeDefArray];
}
// Helper to append
// Takes content, the search term to appendTo, the content to append,
// and bool if the append is before the found area
function append(content, searchTermToAppendto, contentToAppend, appendBefore){
let contentArray = content.split("\n");
let foundIndex = findArrayTrim(contentArray, searchTermToAppendto)
foundIndex = appendBefore ? foundIndex : foundIndex +1
contentArray.splice(foundIndex,0,contentToAppend)
return contentArray.join("\n")
}
// Helper function for append
function findArrayTrim(array, searchTerm){
var index = -1;
for (var i = 0; i < array.length; i++){
index = array[i].trim().indexOf(searchTerm.trim());
if (index > -1){
return i
}
}
return index;
}
// Remove grav directory if exists to make sure old files aren't kept
if (fs.existsSync(dir_grav)){
console.log("dir_grav exists");
rimraf.sync(dir_grav);
}
// Create Grav directories in JSDOC output
dirArray.forEach(function(dir){
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
})
// Create baseMD files
baseMDDirectories.forEach( md => {
createMD(md, map_dir_md[md]);
})
// Read jsdoc output folder and process html files
let curSource = path.join(dir_out, "Controller.html");
if (path.extname(curSource) == ".html" && path.basename(curSource, '.html') !== "index") {
// Clean up the html source
let loadedHtml = prepareHtml(curSource);
// Extract the title, group name, and the main div
let splitTitle = loadedHtml("title").text().split(": ");
let groupName = splitTitle[1];
let htmlTitle = splitTitle.pop();
let mainDiv = loadedHtml("#main")
let methodIDs = [];
let signalIDs = [];
let typeDefIDs = [];
// Basic Regex HTML edits
let mainDivRegexed = mainDiv.html()
.replace(html_reg_static,"")
.replace(html_reg_title,"")
.replace(html_reg_objectHeader,"")
.replace(html_reg_htmlExt,"")
.replace(html_reg_brRemove, "")
.replace(html_reg_subsectionEdit, html_reg_subsectionEdit_replace)
.replace(html_reg_propertiesHeaderEdit, html_reg_propertiesHeaderEdit_Replace)
.replace(html_reg_typeEdit, html_reg_typeEdit_replace)
.replace(html_reg_returnSize, html_reg_returnSize_replace)
.replace(html_reg_methodSize, html_reg_methodSize_replace)
.replace(html_reg_typeDefSize, html_reg_typeDefSize_replace)
.replace(html_reg_typeDefinitonsTitle, "");
// Further HTML Manipulation
// Split HTML by Each named entry
let contentSplitArray = splitBy(mainDivRegexed, html_reg_findByName, html_reg_findByArticleClose);
// Create a reference to the current content after split and the split functions
let currentContent = contentSplitArray[0];
// Create references to the split methods and signals
let splitMethodsSignalsAndTypeDefs = splitMethodsSignalsAndTypeDefs(contentSplitArray[1]);
let splitMethods = splitMethodsSignalsAndTypeDefs[0];
let splitSignals = splitMethodsSignalsAndTypeDefs[1];
let splitTypeDefintions = splitMethodsSignalsAndTypeDefs[2];
let splitMethodIDS = extractIDs(splitMethods);
let splitSignalIDS = extractIDs(splitSignals);
let splitTypeDefinitionIDS = extractIDs(splitTypeDefintions);
let classTOC = makeClassTOC([
{type: "Methods", array: splitMethodIDS},
{type: "Signals", array: splitSignalIDS},
{type: "Type Definitions", array: splitTypeDefinitionIDS}
]);
// Append Signals and Methods to the current Content
currentContent = append(currentContent, html_reg_findByTitle, classTOC);
currentContent = append(currentContent, html_reg_findByMethod, splitMethods.join('\n'));
if (splitSignals.length > 0) {
// Add the Signals header to the Signals HTML
splitSignals.unshift(html_reg_signalTitle)
currentContent = append(currentContent, html_reg_findByArticleClose, splitSignals.join('\n'),true);
}
if (splitTypeDefintions.length > 0) {
// Add the Signals header to the Signals HTML
splitTypeDefintions.unshift(html_reg_typeDefinitonsTitle)
currentContent = append(currentContent, html_reg_findByArticleClose, splitTypeDefintions.join('\n'), true);
}
// Final Pretty Content
currentContent = htmlclean(currentContent);
currentContent = pretty(currentContent);
// Handle Unique Categories
switch(groupName){
case "Namespace":
handleNamespace(htmlTitle, currentContent);
break;
case "Class":
handleClass(htmlTitle, currentContent);
break;
case "Global":
handleGlobal(htmlTitle, currentContent);
break;
default:
console.log(`Case not handled for ${groupName}`);
}
}
// Create the base Templates after processing individual files
createTemplate("API-Reference", makeGroupTOC(["Namespaces", "Objects", "Globals"]));
createTemplate("Namespaces", makeGroupTOC("Namespaces"));
createTemplate("Objects", makeGroupTOC("Objects"));
// Copy the files to the target Directories if Local
if (copyLocal){
// Copy files to the Twig Directory
let templateFiles = fs.readdirSync(path.resolve(targetTemplateDirectory));
// Remove Existing API files
templateFiles.forEach(function(file){
let curSource = path.join(targetTemplateDirectory, file);
if(path.basename(file, '.html').indexOf("API") > -1){
fs.unlink(curSource);
}
})
copyFolderRecursiveSync(dir_template, targetTemplateDirectory);
// Copy files to the Md Directory
let baseMdRefDir = path.join(targetMDDirectory,"06.api-reference");
// Remove existing MD directory
if (fs.existsSync(baseMdRefDir)){
rimraf.sync(baseMdRefDir);
}
copyFolderRecursiveSync(dir_md, targetMDDirectory);
}