public static DocItem LoadDocItem(string fileName, List<string> errorLines) { using (StreamReader reader = new StreamReader(fileName)) { DocItem docItem = new DocItem(); bool enterContentMode = false; List<string> contentLines = new List<string>(); while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line.StartsWith("/title:")) { int index = line.IndexOf(':'); docItem.Title = line.Substring(index + 1); } else if (line.StartsWith("/uniqueId:")) { int index = line.IndexOf(':'); docItem.UniqueId = line.Substring(index + 1); } else if (line.StartsWith("/parentUniqueId:")) { int index = line.IndexOf(':'); docItem.ParentUniqueId = line.Substring(index + 1); } else if (line.StartsWith("/symbol:")) { int index = line.IndexOf(':'); docItem.Symbol = line.Substring(index + 1); } else if (line == "/content") { if (docItem.Title == null) throw new ArgumentException("Document item should has a title."); if (docItem.UniqueId == null) throw new ArgumentException("Document item should has a unique id."); if (docItem.ParentUniqueId == null) throw new ArgumentException("Document item should has a parent unique id. Use empty for root items."); enterContentMode = true; } else { if (!enterContentMode) { throw new ArgumentException("Don't know how to parse " + line); } else { line = line.Trim(); if (line != "") { contentLines.Add(line); } } } } docItem.Content = DocContentParser.Parse(contentLines, errorLines); return docItem; } }
public static DocItemValidationResult Validate(DocItem[] docItems) { var result = new DocItemValidationResult(docItems); string errorLines = ""; foreach (var item in docItems) { foreach (var entity in item.Content.Entities) { switch (entity.Type) { case DocEntityType.LinkId: { if (entity.State == DocEntityState.Open) { if (!result.UniqueIdItemMap.ContainsKey(entity.Argument)) { errorLines += string.Format("Cannot find document item by unique id {0} when validating /+linkid/s in {1}.\r\n", entity.Argument, item.Title); } } } break; case DocEntityType.LinkSymbol: { if (entity.State == DocEntityState.Open) { if (!result.MemberIdItemMap.ContainsKey(entity.Argument)) { if (entity.Argument.StartsWith("M:")) { var methodItems = result .MemberIdItemMap .Where(p => p.Key.StartsWith(entity.Argument + "(")) .Select(p => p.Value) .ToArray(); if (methodItems.Length == 1) { entity.Argument = methodItems[0].Symbol; break; } } errorLines += string.Format("Cannot find document item by symbol {0} when validating /+linksymbol/s in {1}.\r\n", entity.Argument, item.Title); } } } break; } } } if (errorLines != "") { throw new ArgumentException(errorLines); } return result; }
public DocItemValidationResult(DocItem[] docItems) { this.DocItems = docItems; this.RootItems = docItems.Where(i => i.Parent == null).ToArray(); this.UniqueIdItemMap = docItems .Where(d => !string.IsNullOrWhiteSpace(d.UniqueId)) .ToDictionary(d => d.UniqueId, d => d); this.MemberIdItemMap = docItems .Where(d => !string.IsNullOrWhiteSpace(d.Symbol)) .ToDictionary(d => d.Symbol, d => d); }
public static Dictionary<string, DocItem> MapItems(DocItem[] inputItems) { Dictionary<string, DocItem> items = new Dictionary<string, DocItem>(); string errorLines = ""; foreach (var item in inputItems) { if (items.ContainsKey(item.UniqueId)) { errorLines += string.Format("Duplicate unique id {0} in {1}.\r\n", item.UniqueId, item.Title); } else { items.Add(item.UniqueId, item); } } if (errorLines != "") throw new ArgumentException(errorLines); return items; }
static void GenerateFile(DocItem docItem, string outputFolder, DocItemValidationResult validationResult) { string fileName = outputFolder + GetKey(docItem) + ".xml"; using (StreamWriter writer = new StreamWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write), Encoding.UTF8)) { XDocument xmlMeta = new XDocument(); xmlMeta.Add(new XElement("metaDoc")); XElement xmlRoot = xmlMeta.Root; xmlRoot.Add(new XElement("title", docItem.Title)); xmlRoot.Add(new XElement("uniqueId", GetKey(docItem))); if (docItem.Parent != null) { xmlRoot.Add(new XElement("parentUniqueId", GetKey(docItem.Parent))); } xmlRoot.Add(new XElement("subItems", docItem.SubItems.Select(d => new XElement("subItem", GetKey(d))).ToArray())); GenerateContent(xmlRoot, docItem, validationResult); xmlMeta.Save(writer); } GenerateFiles(docItem.SubItems, outputFolder, validationResult); }
static void GenerateContent(XElement xmlRoot, DocItem docItem, DocItemValidationResult validationResult) { { List<DocItem> parentList = new List<DocItem>(); { DocItem currentItem = docItem; while (currentItem != null) { parentList.Insert(0, currentItem); currentItem = currentItem.Parent; } } xmlRoot.Add(new XElement("indexTree", GenerateIndexTree(validationResult.RootItems, parentList, 0, 0).ToArray())); } var writer = new StringWriter(); { StaticHtmlDocGenerator.GenerateDocumentContent(writer, docItem, validationResult, d => string.Format("$UNIQUEIDLINK({0})", GetKey(d))); } string content = writer.ToString(); xmlRoot.Add(new XElement("content", content)); }
static string GetFileName(DocItem docItem, string outputFolder = "") { return outputFolder + docItem.UniqueId.Replace(':', '_').Replace('.', '_') + ".html"; }
static void GenerateFile(DocItem docItem, string outputFolder, DocItemValidationResult validationResult) { string fileName = GetFileName(docItem, outputFolder); using (StreamWriter writer = new StreamWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write), Encoding.UTF8)) { writer.WriteLine("<html>"); writer.WriteLine("<head>"); writer.WriteLine("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\" />"); writer.WriteLine("<link rel=\"Stylesheet\" type=\"text/css\" href=\"../Content/DocStyle.css\" />"); writer.WriteLine("<link rel=\"Stylesheet\" type=\"text/css\" href=\"../Content/PageStyle.css\" />"); writer.WriteLine("<title>{0}</title>", docItem.Title); writer.WriteLine("</head>"); writer.WriteLine("<body>"); GenerateContent(writer, docItem, validationResult); writer.WriteLine("</body>"); writer.WriteLine("</html>"); } GenerateFiles(docItem.SubItems, outputFolder, validationResult); }
static void GenerateContent(TextWriter writer, DocItem docItem, DocItemValidationResult validationResult) { writer.WriteLine("<table width=\"100%\"><col width=\"10%\"/><col width=\"90%\"/>"); writer.WriteLine("<tr><td class=\"NavigateHeader\" colspan=\"2\" align=\"left\">"); writer.WriteLine("<a class=\"LogoLink\" href=\"reference_gacui.html\"><img src=\"../Content/GacLib_LogoAndTitle.gif\" alt=\"GacLib -- GPU Accelerated C++ User Interface\"/></a>"); writer.WriteLine("</td></tr>"); writer.WriteLine("<tr><td class=\"frameTable indexPanel\" valign=\"top\">"); { List<DocItem> parentList = new List<DocItem>(); { DocItem currentItem = docItem; while (currentItem != null) { parentList.Insert(0, currentItem); currentItem = currentItem.Parent; } } GenerateIndexTree(writer, validationResult.RootItems, parentList, 0, 0); } writer.WriteLine("</td><td class=\"frameTable contentPanel\" valign=\"top\">"); { GenerateDocumentContent(writer, docItem, validationResult, d => Path.GetFileName(GetFileName(d))); } writer.WriteLine("</td></tr></table>"); }
public static void GenerateDocumentContent(TextWriter writer, DocItem docItem, DocItemValidationResult validationResult, Func<DocItem, string> linkTransformer) { string currentColor = "000000"; writer.Write("<p><font color=\"{0}\">", currentColor); foreach (var docEntity in docItem.Content.Entities) { switch (docEntity.Type) { case DocEntityType.Bold: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<b>"); break; case DocEntityState.Close: writer.Write("</b>"); break; } break; case DocEntityType.Header1: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<h1>"); break; case DocEntityState.Close: writer.Write("</h1>"); break; } break; case DocEntityType.Header2: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<h2>"); break; case DocEntityState.Close: writer.Write("</h2>"); break; } break; case DocEntityType.Header3: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<h3>"); break; case DocEntityState.Close: writer.Write("</h3>"); break; } break; case DocEntityType.LinkId: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<a href=\"{0}\">", linkTransformer(validationResult.UniqueIdItemMap[docEntity.Argument])); break; case DocEntityState.Close: writer.Write("</a>"); break; } break; case DocEntityType.LinkSymbol: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<a href=\"{0}\">", linkTransformer(validationResult.MemberIdItemMap[docEntity.Argument])); break; case DocEntityState.Close: writer.Write("</a>"); break; } break; case DocEntityType.Table: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<div class=\"docTable\"><table>"); break; case DocEntityState.Close: writer.Write("</table></div>"); break; } break; case DocEntityType.RowHeader: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<thead><tr>"); break; case DocEntityState.Close: writer.Write("</tr></thead>"); break; } break; case DocEntityType.Row: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<tr>"); break; case DocEntityState.Close: writer.Write("</tr>"); break; } break; case DocEntityType.Col: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<td>"); break; case DocEntityState.Close: writer.Write("</td>"); break; } break; case DocEntityType.Code: writer.Write("<table><tr><td>"); GenerateText(writer, docEntity.Argument); writer.Write("</td></tr></table>"); break; case DocEntityType.Slash: GenerateText(writer, "/"); break; case DocEntityType.Crlf: writer.Write("<br/>"); break; case DocEntityType.Para: writer.Write("</font></p><p><font color=\"{0}\">", currentColor); break; case DocEntityType.Nop: break; case DocEntityType.Img: writer.Write("<img src=\"{0}\"/>", docEntity.Argument); break; case DocEntityType.Color: currentColor = docEntity.Argument; writer.Write("</font><font color=\"{0}\">", currentColor); break; case DocEntityType.Text: GenerateText(writer, docEntity.Argument); break; } } writer.Write("</font></p>"); }
static string GetKey(DocItem docItem) { return(docItem.UniqueId.Replace(':', '_').Replace('.', '_')); }
public static void GenerateDocumentContent(TextWriter writer, DocItem docItem, DocItemValidationResult validationResult, Func <DocItem, string> linkTransformer) { string currentColor = "000000"; writer.Write("<p><font color=\"{0}\">", currentColor); foreach (var docEntity in docItem.Content.Entities) { switch (docEntity.Type) { case DocEntityType.Bold: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<b>"); break; case DocEntityState.Close: writer.Write("</b>"); break; } break; case DocEntityType.Header1: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<h1>"); break; case DocEntityState.Close: writer.Write("</h1>"); break; } break; case DocEntityType.Header2: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<h2>"); break; case DocEntityState.Close: writer.Write("</h2>"); break; } break; case DocEntityType.Header3: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<h3>"); break; case DocEntityState.Close: writer.Write("</h3>"); break; } break; case DocEntityType.LinkId: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<a href=\"{0}\">", linkTransformer(validationResult.UniqueIdItemMap[docEntity.Argument])); break; case DocEntityState.Close: writer.Write("</a>"); break; } break; case DocEntityType.LinkSymbol: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<a href=\"{0}\">", linkTransformer(validationResult.MemberIdItemMap[docEntity.Argument])); break; case DocEntityState.Close: writer.Write("</a>"); break; } break; case DocEntityType.Table: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<div class=\"docTable\"><table>"); break; case DocEntityState.Close: writer.Write("</table></div>"); break; } break; case DocEntityType.RowHeader: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<thead><tr>"); break; case DocEntityState.Close: writer.Write("</tr></thead>"); break; } break; case DocEntityType.Row: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<tr>"); break; case DocEntityState.Close: writer.Write("</tr>"); break; } break; case DocEntityType.Col: switch (docEntity.State) { case DocEntityState.Open: writer.Write("<td>"); break; case DocEntityState.Close: writer.Write("</td>"); break; } break; case DocEntityType.Code: writer.Write("<table><tr><td>"); GenerateText(writer, docEntity.Argument); writer.Write("</td></tr></table>"); break; case DocEntityType.Slash: GenerateText(writer, "/"); break; case DocEntityType.Crlf: writer.Write("<br/>"); break; case DocEntityType.Para: writer.Write("</font></p><p><font color=\"{0}\">", currentColor); break; case DocEntityType.Nop: break; case DocEntityType.Img: writer.Write("<img src=\"{0}\"/>", docEntity.Argument); break; case DocEntityType.Color: currentColor = docEntity.Argument; writer.Write("</font><font color=\"{0}\">", currentColor); break; case DocEntityType.Text: GenerateText(writer, docEntity.Argument); break; } } writer.Write("</font></p>"); }
static string GetFileName(DocItem docItem, string outputFolder = "") { return(outputFolder + docItem.UniqueId.Replace(':', '_').Replace('.', '_') + ".html"); }
static string GetKey(DocItem docItem) { return docItem.UniqueId.Replace(':', '_').Replace('.', '_'); }
static void Main(string[] args) { if (args.Length >= 2) { string outputMethod = args[0]; string outputTarget = Path.GetFullPath(args[1]) + "\\"; string[] inputFiles = args.Skip(2) .SelectMany(d => Directory.GetFiles(d)) .Select(f => Path.GetFullPath(f).ToUpper()) .Where(f => f.EndsWith(".DOCITEM.TXT")) .Distinct() .ToArray(); DocItem[] docItems = inputFiles .Select(f => { try { List <string> errorLines = new List <string>(); DocItem result = DocItemParser.LoadDocItem(f, errorLines); if (errorLines.Count > 0) { Console.WriteLine("[{0}]:", f); foreach (var error in errorLines) { Console.WriteLine(error); } } return(result); } catch (ArgumentException ex) { Console.WriteLine("[{0}]: {1}", f, ex.Message); return(null); } } ) .Where(i => i != null) .ToArray(); try { var mappedDocItems = DocItemSorter.MapItems(docItems); var rootItems = DocItemSorter.SortAndGetRootItems(mappedDocItems); var validationResult = DocItemValidator.Validate(docItems); switch (outputMethod.ToUpper()) { case "HTML": StaticHtmlDocGenerator.GenerateStaticHtmlDocument(outputTarget, validationResult); break; case "META": StaticMetaDocGenerator.GenerateStaticHtmlDocument(outputTarget, validationResult); break; default: throw new ArgumentException(string.Format("Don't understand output method {0}.", outputMethod)); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); Console.WriteLine("Errors cause the index generatiing to stop."); } } else { Console.WriteLine("GenDocIndex.exe html <output-folder> <input-.docitem.txt-folder> ..."); } }
public static DocItem LoadDocItem(string fileName, List <string> errorLines) { using (StreamReader reader = new StreamReader(fileName)) { DocItem docItem = new DocItem(); bool enterContentMode = false; List <string> contentLines = new List <string>(); while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line.StartsWith("/title:")) { int index = line.IndexOf(':'); docItem.Title = line.Substring(index + 1); } else if (line.StartsWith("/uniqueId:")) { int index = line.IndexOf(':'); docItem.UniqueId = line.Substring(index + 1); } else if (line.StartsWith("/parentUniqueId:")) { int index = line.IndexOf(':'); docItem.ParentUniqueId = line.Substring(index + 1); } else if (line.StartsWith("/symbol:")) { int index = line.IndexOf(':'); docItem.Symbol = line.Substring(index + 1); } else if (line == "/content") { if (docItem.Title == null) { throw new ArgumentException("Document item should has a title."); } if (docItem.UniqueId == null) { throw new ArgumentException("Document item should has a unique id."); } if (docItem.ParentUniqueId == null) { throw new ArgumentException("Document item should has a parent unique id. Use empty for root items."); } enterContentMode = true; } else { if (!enterContentMode) { throw new ArgumentException("Don't know how to parse " + line); } else { line = line.Trim(); if (line != "") { contentLines.Add(line); } } } } docItem.Content = DocContentParser.Parse(contentLines, errorLines); return(docItem); } }