using System; using UnityEngine; using System.Collections.Generic; public class PRPLDocCompiler { private Main main; private int cmdCount; private string currentSection; private string[] docIndexLines; private OrderedDictionaryG>> sectionDict; public string resultFile; public void MakePRPLDocs(Main main, string srcDir, string outputDir) { this.main = main; resultFile = null; cmdCount = 0; currentSection = null; sectionDict = new OrderedDictionaryG>>(); System.Text.StringBuilder sbcommands = new System.Text.StringBuilder(); System.Text.StringBuilder sbIndex = new System.Text.StringBuilder(); srcDir = srcDir.Trim(); outputDir = outputDir.Trim(); if (srcDir != "" && !srcDir.EndsWith("/") && !srcDir.EndsWith("\\")) { srcDir = srcDir + "/"; } if (outputDir != "" && !outputDir.EndsWith("/") && !outputDir.EndsWith("\\")) { outputDir = outputDir + "/"; } try { docIndexLines = System.IO.File.ReadAllLines(srcDir + "prpldocindex.txt"); foreach (string i in docIndexLines) { ProcessDocFile(srcDir, i.Trim(), sbcommands); } System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(@"
"); sb.Append(""); GenerateDocSections(sb,sbIndex); sb.Append("\n
"); sb.Append(sbcommands); sb.Append(@"

"); sb.Append(sbIndex); sb.Append(@"

"); System.IO.File.WriteAllText(outputDir + "PRPLDocs.html", sb.ToString()); resultFile = outputDir + "PRPLDocs.html"; main.LogMessage("COMPLETE: HTML file created: " + resultFile); } catch (Exception e) { main.LogMessage("ERROR: " + e.Message); main.LogMessage(e.StackTrace); } } private void ProcessDocFile(string srcDir, string sect, System.Text.StringBuilder sbcommands) { currentSection = sect; string f = srcDir + sect + ".txt"; main.LogMessage("Processing File: " + f); if (System.IO.File.Exists(f)) { string[] lines = System.IO.File.ReadAllLines(f); for (int i = 0; i < lines.Length; i++) { string l = lines[i].TrimStart(); if (l.StartsWith("=CMD")) { i = ProcessDocCMD(sbcommands, lines, i); } } } else { main.LogMessage("File Not Found: " + f); } } private void GenerateDocSections(System.Text.StringBuilder sb, System.Text.StringBuilder sbIndex) { sb.Append("
\r\n"); //bool open = false; for (int i = 0; i < docIndexLines.Length; i++) { //sb.Append("\r\n"); if (!sectionDict.ContainsKey(docIndexLines[i].Trim())) { sb.Append(" "); } else { GenerateDocSection(sb, sbIndex, docIndexLines[i].Trim()); } //sb.Append("\r\n"); //if (i % 2 == 1) { // sb.Append("\r\n"); //open = false; //} } //if (open) { //sb.Append("\r\n"); //open = false; //} /* sb.Append("\r\n"); sb.Append("\r\n"); GenerateDocSection(sb, "Vars and Functions"); sb.Append("\r\n"); sb.Append("\r\n"); GenerateDocSection(sb, "Logic"); sb.Append("\r\n"); sb.Append("\r\n"); */ sb.Append("
"); } private void GenerateDocSection(System.Text.StringBuilder sb, System.Text.StringBuilder sbIndex, string section) { int cols = 1; sb.Append("\n\t\n\t\t\n\t\t\t\r\n"); sb.Append("\t\t\n\t\n\t\n"); List> list = sectionDict[section]; int cc = 0; //sbIndex.Append("

\n"); // open = false; foreach (Tuple tup in list) { if (cc % cols == 0) { sb.Append("\t\t

\r\n"); //open = true; } string cs; if (cc == list.Count - 1) { cs = "colspan='" + (cols - (cc % cols)).ToString() + "'"; } else { cs = ""; } sb.Append("\t\t\t\r\n"); sbIndex.Append(" " + tup.Item2 + " \r\n"); if (cc % cols == cols-1) { sb.Append("\t\t\r\n"); //open = false; } cc++; } //if (open) { sb.Append("\t\r\n"); // open = false; //} //sbIndex.Append("

\n"); sb.Append("
" + section + "
\n\t\t\t\t" + tup.Item2 + "\n\t\t\t
\r\n"); } private int ProcessDocCMD(System.Text.StringBuilder sb, string[] lines, int pos) { System.Text.StringBuilder currCmdStr = new System.Text.StringBuilder (); string cmd = ""; sb.Append (@"
\n"); string startingSection = currentSection; int i; for (i = pos; i < lines.Length; i++) { string l = lines[i].TrimStart(); if (l.StartsWith("=ENDCMD")) { break; } else if (l.StartsWith("=CMDCLASS")) { ProcessDocCMDCLASS(currCmdStr, l); } else if (l.StartsWith("=COMMAND")) { cmd = ProcessDocCOMMAND(currCmdStr, l); } else if (l.StartsWith("=DESC")) { i = ProcessDocDESC(currCmdStr, lines, i); } else if (l.StartsWith("=EX")) { i = ProcessDocEX(currCmdStr, lines, i); } } currentSection = startingSection; sb.Append(cmd); sb.Append(currCmdStr); sb.Append("\t\t
\r\n"); sb.Append("\t\r\n"); return i; } private void ProcessDocCMDCLASS(System.Text.StringBuilder sb, string l) { string sect = l.Substring("=CMDCLASS".Length).Trim(); if (sect != "") { currentSection = sect; } //sb.Append("\r\n"); } private string ProcessDocCOMMAND(System.Text.StringBuilder sb, string l) { string cmd = l.Substring("=COMMAND".Length).Trim(); string[] splitcmd = cmd.Split(' ', '('); List> section = null; if (sectionDict.ContainsKey(currentSection)) { section = sectionDict[currentSection]; } else { section = new List>(); sectionDict[currentSection] = section; } section.Add(new Tuple(cmdCount, splitcmd[0])); sb.Append("\t\t\r\n"); cmdCount++; return splitcmd [0]; } private int ProcessDocDESC(System.Text.StringBuilder sb, string[] lines, int pos) { sb.Append("\t\t
\r\n"); sb.Append("\t\t\t
\r\n"); sb.Append("\t\t\t\tDescription\r\n"); sb.Append("\t\t\t\t
\r\n"); int i; for (i = pos + 1; i < lines.Length; i++) { if (lines[i].Trim() == "=ENDDESC") { break; } else { //sb.Append(lines[i].TrimStart().Substring(2) + " "); sb.Append(lines[i].TrimEnd() + " "); } } sb.Append("\n\t\t\t
\r\n"); sb.Append("\t\t
\r\n"); sb.Append("\t\t
\r\n"); sb.Append("\t\t\tExamples\r\n"); sb.Append("\t\t
\r\n"); return i; } private int ProcessDocEX(System.Text.StringBuilder sb, string[] lines, int pos) { sb.Append("\t\t\t
\r\n"); sb.Append("\t\t\t\t\n"); int i; for (i = pos + 1; i < lines.Length; i++) { if (lines[i].Trim() == "=ENDEX") { break; } else { //sb.Append(lines[i].TrimStart().Substring(2) + "\r\n"); sb.Append(lines[i].TrimEnd() + "\r\n"); } } sb.Append("\t\t\t\t