private RepositoryDataType ImportDataType(MarkdownDocument document) { RepositoryDataType entry = new RepositoryDataType(); TableData fields = null; TableData references = null; Dictionary <string, CellData> attributes = null; int start = 0; while (start < document.Count) { HeadingBlock heading = document[start++] as HeadingBlock; if (heading != null) { entry.Name = heading.Inline.FirstChild.ToString(); break; } } entry.Documentation = ReadDescription(document, ref start); while (start < document.Count) { Table table = document[start++] as Table; if (table != null) { fields = ParseGenericTable(table); break; } } while (start < document.Count) { Table table = document[start++] as Table; if (table != null) { attributes = ParseAttributeTable(table); break; } } while (start < document.Count) { Table table = document[start++] as Table; if (table != null) { references = ParseGenericTable(table); break; } } if (attributes != null) { entry.IsAbstract = false; entry.BaseType = null; CellData cell = null; if (attributes.TryGetValue("BrowseName", out cell)) { entry.Name = cell.Text; } if (attributes.TryGetValue("IsAbstract", out cell)) { if (!Boolean.TryParse(cell.Text, out entry.IsAbstract)) { entry.IsAbstract = false; } } if (attributes.TryGetValue("BaseType", out cell)) { entry.BaseType = new RepositoryLink() { Name = cell.Text, Path = cell.Link }; } if (attributes.TryGetValue("Categories", out cell)) { var name = cell.Text; if (!String.IsNullOrEmpty(name)) { var categories = name.Split(','); entry.Categories = new List <string>(); foreach (var category in categories) { if (!String.IsNullOrEmpty(name) && !entry.Categories.Contains(category)) { entry.Categories.Add(category); } } } } } if (fields != null) { for (int ii = 0; ii < fields.Count; ii++) { var field = new RepositoryDataTypeField(); field.Name = null; field.DataType = null; field.ValueRank = ValueRanks.Scalar; field.Documentation = null; var cell = fields.GetCell("Name", ii); if (cell != null && !String.IsNullOrEmpty(cell.Text)) { field.Name = cell.Text.Trim(); if (field.Name.StartsWith(" ")) { field.Name = field.Name.Replace(" ", ""); field.Name = field.Name.Substring(0, 1).ToUpper() + field.Name.Substring(1); } } cell = fields.GetCell("Value", ii); if (cell != null && !String.IsNullOrEmpty(cell.Text)) { long value = 0; if (Int64.TryParse(cell.Text, out value)) { field.Value = (int)value; } } cell = fields.GetCell("DataType", ii); if (cell != null && !String.IsNullOrEmpty(cell.Text)) { var dataType = cell.Text.Trim(); if (dataType.EndsWith("[]")) { field.ValueRank = ValueRanks.OneDimension; dataType = dataType.Substring(0, dataType.Length - 2); dataType = dataType.Trim(); } field.DataType = new RepositoryLink() { Name = dataType, Path = cell.Link }; } cell = fields.GetCell("Description", ii); if (cell != null && !String.IsNullOrEmpty(cell.Text)) { field.Documentation = new List <string>(); field.Documentation.Add(cell.Text); } if (ii != 0 || field.Name != entry.Name) { entry.Fields.Add(field); } } } if (references != null) { var descriptions = ReadReferenceDescription(document, ref start); UpdateReferences(references, descriptions, entry.References); } return(entry); }
public void Export(RepositoryDataType entry, string path) { if (path == null) { throw new ArgumentNullException("path"); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var filePath = Path.Combine(path, "readme.md"); using (var writer = new StreamWriter(filePath)) { writer.WriteLine("<!-- datatype -->"); writer.WriteLine($"## {entry.Name}"); WriteParagraphs(writer, entry.Documentation, false); writer.WriteLine($"<!-- end of description -->"); writer.WriteLine($"The fields of the {entry.Name} DataType are defined in the following table: "); if (entry.DataTypeClass == DataTypeClass.Structure || entry.DataTypeClass == DataTypeClass.Union) { writer.WriteLine(); writer.WriteLine($"|Name|Type|Description|"); writer.WriteLine($"|---|---|---|"); writer.WriteLine($"|{entry.Name}|{entry.DataTypeClass}||"); foreach (var field in entry.Fields) { StringBuilder description = new StringBuilder(); if (field.Documentation != null) { foreach (var ii in field.Documentation) { if (ii != null) { if (description.Length > 0) { description.Append($" {Environment.NewLine}"); } description.Append($"{ii}"); if (!ii.EndsWith(".")) { description.Append("."); } } } } var name = field.Name.Substring(0, 1).ToLower() + field.Name.Substring(1); if (field.ValueRank != ValueRanks.Scalar) { writer.WriteLine($"| {name}|{Export(field.DataType)}[]|{description}|"); } else { writer.WriteLine($"| {name}|{Export(field.DataType)}|{description}|"); } } } else { var columnName = (entry.DataTypeClass == DataTypeClass.OptionSet) ? "Bit No." : "Value"; writer.WriteLine(); writer.WriteLine($"|Name|{columnName}| Description|"); writer.WriteLine($"|---|---|---|"); foreach (var field in entry.Fields) { StringBuilder description = new StringBuilder(); if (field.Documentation != null) { foreach (var ii in field.Documentation) { if (ii != null) { if (description.Length > 0) { description.Append($" {Environment.NewLine}"); } description.Append($"{ii}"); if (!ii.EndsWith(".")) { description.Append("."); } } } } writer.WriteLine($"|{field.Name}|{field.Value}|{description}|"); } } writer.WriteLine(); writer.WriteLine($"The representation of the {entry.Name} DataType in the address space is shown in the following table: "); writer.WriteLine(); writer.WriteLine($"|Name|Attribute|"); writer.WriteLine($"|---|---|"); writer.WriteLine($"|NodeId|{entry.NodeId}|"); writer.WriteLine($"|NamespaceUri|{entry.NamespaceUri}|"); writer.WriteLine($"|BrowseName|{entry.Name}|"); writer.WriteLine($"|IsAbstract|{entry.IsAbstract}|"); writer.WriteLine($"|BaseType|{Export(entry.BaseType)}|"); writer.WriteLine($"|Categories|{ExportCategories(entry.Categories)}|"); writer.WriteLine(); if (entry.References != null && entry.References.Count > 0) { writer.WriteLine($"The references from the {entry.Name} DataType Node are shown in the following table: "); writer.WriteLine(); writer.WriteLine($"|Reference|NodeClass|BrowseName|DataType|TypeDefinition|ModellingRule|"); writer.WriteLine($"|---|---|---|---|---|---|"); foreach (var reference in entry.References) { writer.Write($"|{Export(reference.ReferenceType)}"); writer.Write($"|{reference.NodeClass}"); writer.Write($"|{reference.BrowseName}"); writer.Write($"|{Export(reference.DataType)}"); if (reference.DataType != null) { if (reference.ValueRank != ValueRanks.Scalar) { writer.Write("[]"); } } writer.Write($"|{Export(reference.TypeDefinition)}"); writer.Write($"|{Export(reference.ModellingRule)}"); writer.WriteLine($"|"); } writer.WriteLine(); } } }