`);
}
})
linkArray.push(" ");
})
return linkArray.join("\n");
}
// Extract IDS for TOC
function extractIDs(groupToExtract){
let firstLine = "";
let id = "";
let extractedIDs = [];
groupToExtract.forEach((item)=>{
firstLine = item.split("\n")[0];
try {
id = firstLine.split('id="')[1].split(`"`)[0];
} catch (e){
}
extractedIDs.push(id)
})
return extractedIDs;
}
// Helper for splitting up html
// Takes: Content to split, SearchTerm to Split by, and term to End Splitting By
// Returns: [newContent after Split, Array of extracted ]
function splitBy(content, searchTerm, endSplitTerm){
console.log("content1", content);
console.log("endSplitTerm", endSplitTerm);
let foundArray = [];
let curIndex = -1;
let afterCurSearchIndex = -1
let nextIndex = 0;
let findbyNameLength = searchTerm.length;
let curfoundArrayIndex = 0;
let curEndSplitTermIndex = -1;
do {
// Find the index of where to stop searching
curEndSplitTermIndex = content.indexOf(endSplitTerm);
console.log("curEndSplitTermIndex", curEndSplitTermIndex)
// Find the index of the the next Search term
curIndex = content.indexOf(searchTerm);
// The index of where the next search will start
afterCurSearchIndex = curIndex+findbyNameLength;
// Find the content of the next Index
nextIndex = content.indexOf(searchTerm,afterCurSearchIndex);
console.log("nextIndex", nextIndex)
// If the next index isn't found, then next index === index of the end term
if (nextIndex === -1){
nextIndex = curEndSplitTermIndex;
}
// push from the cur index to the next found || the end term
foundArray.push(content.slice(curIndex, nextIndex))
// remove that content
content = content.replace(foundArray[curfoundArrayIndex], "");
curfoundArrayIndex++;
curEndSplitTermIndex = content.indexOf(endSplitTerm);
console.log("curEndSplitTermIndex2", curEndSplitTermIndex)
nextIndex = content.indexOf(searchTerm,afterCurSearchIndex);
// console.log("nextIndex2", nextIndex)
if (nextIndex > curEndSplitTermIndex) {
curIndex = content.indexOf(searchTerm);
console.log("curIndex", curIndex)
console.log("curEndSplitTermIndex", curEndSplitTermIndex)
foundArray.push(content.slice(curIndex, curEndSplitTermIndex))
console.log("foundArray[curfoundArrayIndex],", foundArray[curfoundArrayIndex],)
content = content.replace(foundArray[curfoundArrayIndex], "");
break;
}
} while (curIndex > -1)
// console.log("content2", content);
return [content, foundArray];
}
// Split the signals and methods [Might make this more generic]
function splitMethodsSignalsAndTypeDefs(allItemToSplit){
let methodArray = [];
let signalArray = [];
let typeDefArray = [];
let description;
allItemToSplit.forEach( (content, index) => {
// console.log("content", content);
firstLine = content.split("\n")[0];
// console.log("@@@ firstLine", firstLine)
if (firstLine.indexOf("{Signal}") > -1){
// console.log("### FOUND SIGNAL", content);
signalArray.push(content);
} else if (firstLine.indexOf("span") > -1) {
// console.log("### FOUND SPAN", content);
if (content.indexOf("Available in:") > -1){
// console.log("### DESCRIPTION", content);
description = content;
} else {
// console.log("### METHOD", content);
methodArray.push(content);
}
} else {
// console.log("### TYPEDEF", content);
if(firstLine.trim() !== ""){
// console.log("### TYPEDEF", content);
typeDefArray.push(content);
} else {
// console.log("not handled", content);
}
}
})
return [methodArray, signalArray, typeDefArray, description];
}
// 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 links = [];
let unTouchedLinks = [];
let files = fs.readdirSync(dir_out);
// Create initial Group name member map to handle individual link :: TODO find better way to do this
files.forEach(function (file){
let curSource = path.join(dir_out, file);
if (path.extname(curSource) == ".html" && path.basename(curSource, '.html') !== "index") {
let loadedHtml = prepareHtml(curSource);
let splitTitle = loadedHtml("title").text().split(": ");
let groupName = splitTitle[1];
let htmlTitle = splitTitle.pop();
switch(groupName){
case "Namespace":
groupNameMemberMap["Namespaces"].push(htmlTitle);
break;
case "Class":
groupNameMemberMap["Objects"].push(htmlTitle);
break;
default:
console.log(`Case not handled for ${groupName}`);
}
}
})
files.forEach(function (file, index){
// if (index !== 3) return;
let curSource = path.join(dir_out, file);
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();
console.log("html title", htmlTitle)
let mainDiv = loadedHtml("#main")
let methodIDs = [];
let signalIDs = [];
let typeDefIDs = [];
// Basic Regex HTML edits
let currentContent = mainDiv.html()
.replace(html_reg_findByMethod, "") //Remove Method title to be remade later
.replace(html_reg_static,"") // Remove static from the file names
.replace(html_reg_title,"") // Remove title
.replace(html_reg_objectHeader,"") // Remove extra Object Header
.replace(html_reg_htmlExt,"") // Remove the .html extension from all links
.replace(html_reg_dlClassDetails, "") // Remove unneccsary dlClassDetails Tag
.replace(html_reg_allNonHTML, allLinksToLowerCase) // Turn all links into lowercase before ID tags
.replace(html_reg_allNonHTML, fixLinkGrouping) // Make sure links refer to correct grouping
// .replace(html_reg_subsectionEdit, html_reg_subsectionEdit_replace) // Make all subsection titles the same size
.replace(html_reg_propertiesHeaderEdit, html_reg_propertiesHeaderEdit_Replace) // Remove : from Properties
.replace(html_reg_typeEdit, html_reg_typeEdit_replace) // Put type on the same line
.replace(html_reg_returnSize, html_reg_returnSize_replace) // make return size h6 instead of h5
.replace(html_reg_methodSize, html_reg_methodSize_replace) // make method size into h5
.replace(html_reg_pretty, html_reg_pretty_replace)
// .replace(html_reg_brRemove, "") // Remove extra Brs
// .replace(html_reg_typeDefSize, html_reg_typeDefSize_replace) // make type def header into h5
// .replace(html_reg_findGlobalLinks, html_reg_findGlobalLinks_replace)
// .replace(html_reg_findGeneralLinks, lowerCaseGeneralLinks)
// .replace(html_reg_findClassLinks, modifyClassLinks)
// .replace(html_reg_typeReturnSize, html_reg_typeReturnSize_replace)
// .replace(html_reg_code, html_reg_code_replace)
// Fix for namespace and object links
// currentContent = contentReplace(currentContent, htmlTitle);
// Further HTML Manipulation
// Make end term either Type Definitions or by the article
let endTerm = html_reg_findByArticleClose;
if (currentContent.indexOf("Type Definitions") > -1){
console.log("Found Type Definitions")
endTerm = `
Type Definitions
`
}
// Split HTML by Each named entry
let contentSplitArray = splitBy(currentContent, html_reg_findByName, endTerm);
// Create a reference to the current content after split and the split functions
currentContent = contentSplitArray[0]
.replace(html_reg_typeDefinitonsTitle, "") // Remove Type Definitions Title to be remade later;
// Create references to the split methods and signals
let processedMethodsSignalsAndTypeDefs = splitMethodsSignalsAndTypeDefs(contentSplitArray[1]);
// let splitMethods = processedMethodsSignalsAndTypeDefs[0];
// let splitSignals = processedMethodsSignalsAndTypeDefs[1];
// let splitTypeDefintions = processedMethodsSignalsAndTypeDefs[2];
// let splitDescription = processedMethodsSignalsAndTypeDefs[3];
// let splitMethodIDS = extractIDs(splitMethods);
// let splitSignalIDS = extractIDs(splitSignals);
// let splitTypeDefinitionIDS = extractIDs(splitTypeDefintions);
let arrayToPassToClassToc = [];
// if (splitDescription) {
// currentContent = append(currentContent, html_reg_containerOverview, splitDescription);
// }
// if (splitMethods.length > 0) {
// arrayToPassToClassToc.push({type: "Methods", array: splitMethodIDS});
// // Add the Signals header to the Signals HTML
// splitMethods.unshift(html_reg_findByMethod)
// currentContent = append(currentContent, html_reg_findByArticleClose, splitMethods.join('\n'), true);
// }
// if (splitSignals.length > 0) {
// arrayToPassToClassToc.push({type: "Signals", array: splitSignalIDS});
// // 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) {
// arrayToPassToClassToc.push({type: "Type Definitions", array: splitTypeDefinitionIDS});
// // Add the Signals header to the Signals HTML
// splitTypeDefintions.unshift(html_reg_typeDefinitonsTitle)
// currentContent = append(currentContent, html_reg_findByArticleClose, splitTypeDefintions.join('\n'), true);
// }
// let classTOC = makeClassTOC(arrayToPassToClassToc);
// if (groupName === "Global"){
// currentContent = append(currentContent, html_reg_findByTitle, classTOC);
// } else {
// currentContent = append(currentContent, html_reg_firstTableClose, classTOC);
// }
// 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"));
fs.writeFileSync('Links-Untouched', unTouchedLinks.join('\n'));
// 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);
}