# Turns the file into a map but keeps any newlines / comments
string[int] fileToMap (string file) {
string[int] out;
int ct = 1;
matcher finder = create_matcher("[^\\n]+\\n|[^\\n]+", file_to_buffer(file));
while (find (finder)) {
out[ct] = group(finder);
ct = ct+1;
}
return out;
}
# Turns a map of strings into a buffer
buffer mapToBuffer (string[int] map) {
buffer o;
foreach k, v in map {
o = append(o, v);
}
return o;
}
# Returns the first capture group found
string regexCapture (string regex, string text) {
matcher f = create_matcher(regex, text);
string o = "";
if (find(f)) {
for i from 1 to group_count(f) {
o = group(f, i);
if (o != "")
return o;
}
}
return o;
}
//Copy Paste the wiki into text file and then call this function, Creates a list of all the functions on the wiki
string[int] createFunctionListFromWikiPage(string textFile) {
buffer file = file_to_buffer (textFile);
string[int] wiki; //Wiki is list of functions on wiki while ash is functions from ashref
string regex = "(?:Flink\\|[^\\|]+\\|)([\\w\\d]+)|(?:FunctionEmbed\\|)([\\w\\d]+)";
matcher finder = create_matcher(regex, file);
int ct = 1;
# print(file);
while (find (finder)) {
if (group(finder, 1) != "")
wiki[ct] = group(finder, 1);
else
wiki[ct] = group(finder, 2);
ct = ct + 1;
}
# foreach i, func in wiki {print(func, "blue");}
return wiki;
}
//Copy Paste the wiki into text file and then call this function, Creates a list of all the functions on the wiki
string createFunctionListFromWikiText (string test) {
string wiki; //Wiki is functions on wiki
string regex = "(?:Flink\\|[^\\|]+\\|)([\\w\\d]+)|(?:FunctionEmbed\\|)([\\w\\d]+)";
matcher finder = create_matcher(regex, test);
while (find (finder)) {
if (group(finder, 1) != "")
wiki = group(finder, 1);
else
wiki = group(finder, 2);
}
return wiki;
}
# Turn debug on and run ashref. Turn debug off and copy paste the ashrefs from the debug file.
string[int] createFunctionListFromAshRef(string textFile) {
# Creating a list of wiki acceptable functions using the flink notation
buffer file = file_to_buffer (textFile);
matcher finder = create_matcher("([\\w\\d]+)\\(", file);
int ct = 1;
string[int] ashFuncs;
while (find (finder)) {
if (group(finder, 1) != "")
ashFuncs[ct] = group(finder, 1);
else
ashFuncs[ct] = group(finder, 2);
ct = ct + 1;
}
# foreach i, func in ashFuncs {print(func, "blue");}
return ashFuncs;
}
//Use function on ashref textfile (that has had its links replaced)
string[string] createWikiFlinkFunctions (string textFile){
buffer file = file_to_buffer (textFile);
string[string] ash; #Map will be key of flinkInfo, val of functionName
string regex = "(\\[[^\\]]+\\])|([\\w\\[\\]]+)|([\\(\\)])";
matcher finder = create_matcher(regex, file);
string temp = "{{Flink";
string text, capture;
while (find (finder)) {
text = group(finder);
if(contains_text(text, "[")) #Map parameters
temp = temp + " " + text;
else if (text == ")") { #End of function
temp = temp + "}}";
capture = regexCapture("(?:Flink[^\\]]+(?=\\]))\\]\\|([\\w\\d]+)|(?:Flink\\|[^\\|]+\\|)([\\w\\d]+)", temp);
ash[temp] = capture;
temp = "{{Flink"; #Prep for next function
}
else if (text == "(") {} #Start of inputs
else
temp = temp + "|" + text; #everything else
# foreach i, func in ash {print(func, "blue");}
}
# foreach key, val in ash {print ("Key: " + key + ". Val: " + val);}
# buffer_to_file(mapToBuffer(mapBufferFile ("WikiPageTest.txt")), "WikiPageTest2.txt");
return ash;
}
buffer regexBufferReplace (string regex, string newText, buffer text) {
matcher m = create_matcher(regex, text);
buffer b;
while (m.find()) {
append_replacement(m,b , newText);
}
return append_tail(m, b);
}
void main() {
print ("Starting to generate the new wiki page from the ashref function list!", "blue");
string wikiTextFile = "WikiPage.txt";
string ashRefFile = "AshRef Function List.txt";
string newWikiFile = "NewWikiPage.txt";
buffer newWiki, ashRefs, file;
record wikiInfo {
string func;
string flink;
};
wikiInfo[int] wikiFunctions;
string temp;
string[int] wikiFuncs, wikiFile;
boolean[string] ashFuncs;
string[string] ashrefFlinkFuncs, completedFuncs;
ashRefs = file_to_buffer(ashRefFile);
if (contains_text (ashRefs, "<")) { # Contains hyper links, remove them
ashRefs = regexBufferReplace("<[^\>]+>", "", ashRefs);
buffer_to_file(ashRefs, ashRefFile);
}
ashrefFlinkFuncs = createWikiFlinkFunctions(ashRefFile);
wikiFile = fileToMap ("WikiPage.txt");
# Comb the existing wiki to keep any of the already updated functions. I.e. functions that have FunctionEmbed, opt, /, or any
foreach i, line in wikiFile {
if (contains_text(line, "FunctionEmbed") || contains_text(line, "{{opt") || contains_text(line, "/") || contains_text(line, "any")) { #Remove existing entries that have been completed
temp = createFunctionListFromWikiText(line);
foreach key, func in ashrefFlinkFuncs { #Also skipping any {{opt functions as I don't know that info so better to not mess with
if(temp == func) {
remove ashrefFlinkFuncs[key]; #Remove the current iteration
completedFuncs[line] = func; #Replace it with the embed or opt
ashFuncs[func] = true;
}
}
}
}
# Make sure we didn't skip/miss any functions that had multiple entries but with different input parameters
foreach i, line in wikiFile {
temp = createFunctionListFromWikiText(line);
if (ashFuncs[temp])
completedFuncs[line] = temp;
}
# Add the remainning new functions to the completed list
foreach key, func in ashrefFlinkFuncs {completedFuncs[key] = func;}
# Set the completed list to the new record for ease of sorting
foreach k, v in completedFuncs {
wikiFunctions[count(wikiFunctions)] = new wikiInfo(v, k);
}
# Sort the record by alphabetical
sort wikiFunctions by value.flink;
sort wikiFunctions by value.func;
# foreach k, v in wikiFunctions { print ("Key: " + v.flink + ". Val: " + v.func);}
# Copy text from wikiFile until "==="
foreach i, line in wikiFile {
if (contains_text(line, "==="))
break;
else
append(newWiki, line);
}
# Start adding our alphabetical designations
append(newWiki, "===a===");
string alpha = "a";
string wikiText;
# Now loop through our wikiFunctions, building the new wiki page
foreach k, v in wikiFunctions {
append(newWiki, "\n");
temp = substring(v.func, 0, 1);
wikiText = regexCapture("([^\\n]+)", v.flink);
if (temp == alpha)
append(newWiki, wikiText);
else {
append(newWiki, "\n");
append(newWiki, "===" + temp + "===\n");
append(newWiki, wikiText);
alpha = temp;
}
}
#Tack on the existing wiki page ending, everything after the last function
int ct = count(wikiFile);
while (!contains_text(wikiFile[ct], "{{")) {
ct = ct - 1;
}
ct = ct + 1;
for i from ct to count(wikiFile){
append(newWiki, wikiFile[i]);
}
#Replace any "null" with type of "any"
newWiki = regexBufferReplace("\\|null", "|any", newWiki);
#Write the newWiki
buffer_to_file(newWiki, newWikiFile);
print ("Ended. The new wiki page can be found in: " + newWikiFile, "green");
}