Last active
January 20, 2016 16:03
-
-
Save caseycoding/fe1ec58b742d9ab23d0a to your computer and use it in GitHub Desktop.
Adds links in declarations for Jazzy docs with javascript and nodejs
This file contains hidden or 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 docCategories = ['Classes','Enums','Protocols','Categories'] | |
var docPath = './docs/' | |
var classesPath = './docs/Classes/' | |
var enumsPath = './docs/Enums/' | |
var protocolPath = './docs/Protocols/' | |
var categoriesPath = './docs/Categories/' | |
var Classes = fs.readdirSync(classesPath) | |
var ClassesMinusHtml = removeHtmlFromArray(Classes) | |
var Enums = fs.readdirSync(enumsPath) | |
var EnumsMinusHtml = removeHtmlFromArray(Enums) | |
var Protocols = fs.readdirSync(protocolPath) | |
var ProtocolsMinusHtml = removeHtmlFromArray(Protocols) | |
var Categories = fs.readdirSync(categoriesPath) | |
var CategoriesMinusHtml = removeHtmlFromArray(Categories) | |
var openingSpan = '<span class="n">' | |
var closingSpan = '</span>' | |
var begginingAnchor = '<a href="../' | |
var html = '.html' | |
var endOfA = '">' | |
var closeOfA = '</a>' | |
function escapeRegExp(str) { | |
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); | |
} | |
function replaceAll(str, find, replace) { | |
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace); | |
} | |
function removeHtmlFromArray(array){ | |
array.forEach(function(item, index){ | |
array[index] = item.replace(/\.[^/.]+$/, "") | |
}) | |
return array | |
} | |
function filesForCategory(category){ | |
return fs.readdirSync(docPath + category +'/') | |
} | |
function linkClassNObjects(file){ | |
ProtocolsMinusHtml.forEach(function(classToReplace, index){ | |
var searchString = openingSpan + classToReplace + closingSpan | |
var replaceValue = openingSpan + begginingAnchor + "Protocols/" + classToReplace+ html+ endOfA + classToReplace + closeOfA +closingSpan | |
file = replaceAll(file, searchString, replaceValue) | |
}) | |
EnumsMinusHtml.forEach(function(classToReplace, index){ | |
var searchString = openingSpan + classToReplace + closingSpan | |
var replaceValue = openingSpan + begginingAnchor + "Enums/" + classToReplace+ html+ endOfA + classToReplace + closeOfA +closingSpan | |
file = replaceAll(file, searchString, replaceValue) | |
}) | |
ClassesMinusHtml.forEach(function(classToReplace, index){ | |
var searchString = openingSpan + classToReplace + closingSpan | |
var replaceValue = openingSpan + begginingAnchor + "Classes/" + classToReplace+ html+ endOfA + classToReplace + closeOfA +closingSpan | |
file = replaceAll(file, searchString, replaceValue) | |
}) | |
CategoriesMinusHtml.forEach(function(classToReplace, index){ | |
var searchString = openingSpan + classToReplace + closingSpan | |
var replaceValue = openingSpan + begginingAnchor + "Categories/" + classToReplace+ html+ endOfA + classToReplace + closeOfA +closingSpan | |
file = replaceAll(file, searchString, replaceValue) | |
}) | |
return file | |
} | |
docCategories.forEach(function(category, index){ | |
filesForCategory(category).forEach(function(thisFile, index){ | |
fs.readFile(docPath + category +'/'+ thisFile, 'utf8', function (err, data) { | |
// console.log('reading '+ thisFile); | |
if (err){ | |
console.error(err); | |
} | |
var file = data.toString() | |
file = linkClassNObjects(file) | |
fs.writeFile(docPath + category +'/'+ thisFile, file, 'utf8', function (err) { | |
if (err) return console.log(err); | |
// console.log('writing '+ thisFile); | |
}); | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
still need to optimize a bit - forEach addict.