private void LoadTableParser() { TableSpec.TableParserConfigFile[] configurations = TryLoadConfigurationFiles <TableSpec.TableParserConfigFile>(this.SourceFolderPath); var tableParserConfig = configurations.FirstOrDefault(); if (null != tableParserConfig) { Console.WriteLine("Using table definitions from: {0}", tableParserConfig.SourcePath); this.TableParserConfig = tableParserConfig; this.TableParser = new TableSpec.TableSpecConverter(tableParserConfig.TableDefinitions); } else { Console.WriteLine("Using default table parser definitions"); this.TableParser = TableSpec.TableSpecConverter.FromDefaultConfiguration(); } }
/// <summary> /// Convert blocks of text found inside the markdown file into things we know how to work /// with (methods, resources, examples, etc). /// </summary> /// <param name="errors"></param> /// <returns></returns> protected bool ParseMarkdownBlocks(out ValidationError[] errors) { List <ValidationError> detectedErrors = new List <ValidationError>(); string methodTitle = null; string methodDescription = null; Block previousHeaderBlock = null; List <object> foundElements = new List <object>(); for (int i = 0; i < this.OriginalMarkdownBlocks.Length; i++) { var block = this.OriginalMarkdownBlocks[i]; this.ContentOutline.Add(string.Format("{0} - {1}", block.BlockType, this.PreviewOfBlockContent(block))); // Capture GitHub Flavored Markdown Bookmarks if (IsHeaderBlock(block, 6)) { this.AddBookmarkForHeader(block.Content); } // Capture h1 and/or p element to be used as the title and description for items on this page if (IsHeaderBlock(block)) { methodTitle = block.Content; methodDescription = null; // Clear this because we don't want new title + old description detectedErrors.Add(new ValidationMessage(null, "Found title: {0}", methodTitle)); } else if (block.BlockType == BlockType.p) { if (null == previousHeaderBlock) { detectedErrors.Add(new ValidationWarning(ValidationErrorCode.MissingHeaderBlock, null, "Paragraph text found before a valid header: {0}", this.DisplayName)); } else if (IsHeaderBlock(previousHeaderBlock)) { methodDescription = block.Content; detectedErrors.Add(new ValidationMessage(null, "Found description: {0}", methodDescription)); } } else if (block.BlockType == BlockType.html) { // If the next block is a codeblock we've found a metadata + codeblock pair Block nextBlock = null; if (i + 1 < this.OriginalMarkdownBlocks.Length) { nextBlock = this.OriginalMarkdownBlocks[i + 1]; } if (null != nextBlock && nextBlock.BlockType == BlockType.codeblock) { // html + codeblock = likely request or response! ItemDefinition definition = null; try { definition = this.ParseCodeBlock(block, nextBlock); } catch (Exception ex) { detectedErrors.Add(new ValidationError(ValidationErrorCode.MarkdownParserError, this.DisplayName, ex.Message)); } if (null != definition) { detectedErrors.Add(new ValidationMessage(null, "Found code block: {0} [{1}]", definition.Title, definition.GetType().Name)); definition.Title = methodTitle; definition.Description = methodDescription; if (!foundElements.Contains(definition)) { foundElements.Add(definition); } } } else if (null == this.Annotation) { // See if this is the page-level annotation var annotation = this.ParsePageAnnotation(block); if (null != annotation) { this.Annotation = annotation; if (string.IsNullOrEmpty(this.Annotation.Title)) { this.Annotation.Title = (from b in this.OriginalMarkdownBlocks where IsHeaderBlock(b, 1) select b.Content).FirstOrDefault(); } } } } else if (block.BlockType == BlockType.table_spec) { Block blockBeforeTable = (i - 1 >= 0) ? this.OriginalMarkdownBlocks[i - 1] : null; if (null == blockBeforeTable) { continue; } ValidationError[] parseErrors; var table = TableSpecConverter.ParseTableSpec(block, previousHeaderBlock, out parseErrors); if (null != parseErrors) { detectedErrors.AddRange(parseErrors); } detectedErrors.Add(new ValidationMessage(null, "Found table: {0}. Rows:\r\n{1}", table.Type, (from r in table.Rows select JsonConvert.SerializeObject(r, Formatting.Indented)).ComponentsJoinedByString(" ,\r\n"))); foundElements.Add(table); } if (block.IsHeaderBlock()) { previousHeaderBlock = block; } } ValidationError[] postProcessingErrors; this.PostProcessFoundElements(foundElements, out postProcessingErrors); detectedErrors.AddRange(postProcessingErrors); errors = detectedErrors.ToArray(); return(!detectedErrors.Any(x => x.IsError)); }