コード例 #1
0
        private void BuildCore(FileModel model, IHostService host)
        {
            var markdownService = (MarkdigMarkdownService)model.MarkdownFragmentsModel.Properties.MarkdigMarkdownService;
            var overwriteDocumentModelCreater = new OverwriteDocumentModelCreater(model.MarkdownFragmentsModel.OriginalFileAndType.File);
            var overwriteApplier = new OverwriteApplier(host, OverwriteModelType.MarkdownFragments);
            var schema           = model.Properties.Schema as DocumentSchema;
            List <OverwriteDocumentModel> overwriteDocumentModels;

            // 1. string => AST(MarkdownDocument)
            var ast = markdownService.Parse((string)model.MarkdownFragmentsModel.Content, model.MarkdownFragmentsModel.OriginalFileAndType.File);

            // 2 AST(MarkdownDocument) => MarkdownFragmentModel
            var fragments = new MarkdownFragmentsCreater().Create(ast).ToList();

            // 3. MarkdownFragmentModel => OverwriteDocument
            overwriteDocumentModels = fragments.Select(overwriteDocumentModelCreater.Create).ToList();
            model.MarkdownFragmentsModel.Content = overwriteDocumentModels;

            // Validate here as OverwriteDocumentModelCreater already filtered some invalid cases, e.g. duplicated H2
            ValidateWithSchema(fragments, model);

            // 4. Apply schema to OverwriteDocument, and merge with skeyleton YAML object
            foreach (var overwriteDocumentModel in overwriteDocumentModels)
            {
                var uidDefinitons = model.Uids.Where(s => s.Name == overwriteDocumentModel.Uid).ToList();
                if (uidDefinitons.Count == 0)
                {
                    Logger.LogWarning(
                        $"Unable to find UidDefinition for Uid: { overwriteDocumentModel.Uid}",
                        code: WarningCodes.Overwrite.InvalidMarkdownFragments);
                    continue;
                }
                if (uidDefinitons.Count > 1)
                {
                    Logger.LogWarning($"There are more than one UidDefinitions found for Uid {overwriteDocumentModel.Uid} in lines {string.Join(", ", uidDefinitons.Select(uid => uid.Line).ToList())}");
                }

                var ud                  = uidDefinitons[0];
                var jsonPointer         = new JsonPointer(ud.Path).GetParentPointer();
                var schemaForCurrentUid = jsonPointer.FindSchema(schema);
                var source              = jsonPointer.GetValue(model.Content);
                var overwriteObject     = overwriteApplier.BuildOverwriteWithSchema(model.MarkdownFragmentsModel, overwriteDocumentModel, schema);
                overwriteApplier.MergeContentWithOverwrite(ref source, overwriteObject, ud.Name, string.Empty, schemaForCurrentUid);
            }

            // 5. Validate schema after the merge
            using (new LoggerFileScope(model.LocalPathFromRoot))
            {
                ((SchemaDrivenDocumentProcessor)host.Processor).SchemaValidator.Validate(model.Content);
            }

            // 6. Re-export xrefspec after the merge
            overwriteApplier.UpdateXrefSpec(model, schema);

            model.LinkToUids      = model.LinkToUids.Union(model.MarkdownFragmentsModel.LinkToUids);
            model.LinkToFiles     = model.LinkToFiles.Union(model.MarkdownFragmentsModel.LinkToFiles);
            model.FileLinkSources = model.FileLinkSources.Merge(model.MarkdownFragmentsModel.FileLinkSources);
            model.UidLinkSources  = model.UidLinkSources.Merge(model.MarkdownFragmentsModel.UidLinkSources);
            model.MarkdownFragmentsModel.Content = overwriteDocumentModels;
        }
コード例 #2
0
        public static Dictionary <string, MarkdownFragment> LoadMarkdownFragment(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return(null);
            }
            if (!File.Exists(fileName))
            {
                return(new Dictionary <string, MarkdownFragment>());
            }
            var markdown = File.ReadAllText(fileName);
            var ast      = MDService.Parse(markdown, Path.GetFileName(fileName));
            var models   = new MarkdownFragmentsCreater().Create(ast).ToList();

            return(models.ToDictionary(m => m.Uid, m => m.ToMarkdownFragment(markdown)));
        }
コード例 #3
0
        public void YamlCodeBlockShouldBeNextToL1CodeHeading()
        {
            var markdown = @"# `YAML`

## `Introduction`

This is just a normal yaml fences block:
``` yaml
a: b
c: d
```
";
            var ast      = Markdown.Parse(markdown);
            var model    = new MarkdownFragmentsCreater().Create(ast).ToList();

            Assert.Null(model[0].YamlCodeBlock);
            Assert.Null(model[0].YamlCodeBlockSource);
            Assert.IsType <FencedCodeBlock>(model[0].Contents[0].PropertyValue[1]);
        }
コード例 #4
0
        public void BaseTest()
        {
            var markdown = File.ReadAllText("TestData/System.String.yml.md");
            var ast      = Markdown.Parse(markdown);
            var model    = new MarkdownFragmentsCreater().Create(ast).ToList();

            Assert.Equal(2, model.Count);
            Assert.Equal("System.String", model[0].Uid);
            Assert.NotNull(model[0].UidSource);
            Assert.Equal("author: rpetrusha\nms.author: ronpet\nmanager: wpickett", model[0].YamlCodeBlock.Replace("\r", ""));
            Assert.NotNull(model[0].YamlCodeBlockSource);
            Assert.Equal(4, model[0].Contents.Count);
            Assert.Equal("summary", model[0].Contents[0].PropertyName);
            Assert.NotNull(model[0].Contents[0].PropertyNameSource);
            Assert.Single(model[0].Contents[0].PropertyValue);
            Assert.IsType <ParagraphBlock>(model[0].Contents[0].PropertyValue[0]);
            Assert.Equal("remarks", model[0].Contents[1].PropertyName);
            Assert.Equal(6, model[0].Contents[1].PropertyValue.Count);
            Assert.Equal("System.String.#ctor(System.Char*)", model[1].Uid);
        }