예제 #1
0
 public MarkupResult Markup(string src, string path)
 {
     var dependency = new HashSet<string>();
     var html = _builder.CreateDfmEngine(new DfmRenderer() { Tokens = _tokens }).Markup(src, path, dependency);
     var result = new MarkupResult
     {
         Html = html,
     };
     if (dependency.Count > 0)
     {
         result.Dependency = dependency.ToImmutableArray();
     }
     return result;
 }
예제 #2
0
        public static IList<YamlHtmlPart> SplitYamlHtml(MarkupResult origin)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(origin.Html);
            var parts = new List<YamlHtmlPart>();

            var nodes = doc.DocumentNode.SelectNodes("//yamlheader");
            if (nodes == null) return parts;

            foreach (var node in nodes)
            {
                var sourceFile = node.GetAttributeValue("sourceFile", "NotFound");
                var startLine = node.GetAttributeValue("start", -1);
                var endLine = node.GetAttributeValue("end", -1);

                parts.Add(new YamlHtmlPart
                {
                    Origin = origin,
                    SourceFile = sourceFile,
                    StartLine = startLine,
                    EndLine = endLine
                });
            }

            var startIndexes = nodes.Select(node => node.StreamPosition).Skip(1).ToList();
            startIndexes.Add(origin.Html.Length);
            var endIndexes = nodes.Select(node => node.StreamPosition + node.OuterHtml.Length - 1).ToList();

            for (var i = 0; i < parts.Count; i++)
            {
                if (i == 0)
                {
                    parts[i].Html = origin.Html.Substring(0, startIndexes[0]);
                }
                else
                {
                    parts[i].Html = origin.Html.Substring(startIndexes[i - 1], startIndexes[i] - startIndexes[i - 1]);
                }
                parts[i].Conceptual = origin.Html.Substring(endIndexes[i] + 1, startIndexes[i] - endIndexes[i] - 1);
            }

            return parts;
        }
 public MarkupResult Markup(string src, string path)
 {
     var dependency = new HashSet<string>();
     var json = _builder.CreateDfmEngine(new DfmJsonTokenTreeRender()).Markup(src, path, dependency);
     if (json.Length != 0 && json.EndsWith(","))
     {
         json = json.Remove(json.Length - 1);
     }
     var result = new MarkupResult
     {
         // TODO: rename
         Html = $"{{\"name\":\"0>0>markdown\",\"children\":[{json}]}}",
     };
     if (dependency.Count > 0)
     {
         result.Dependency = dependency.ToImmutableArray();
     }
     return result;
 }
예제 #4
0
파일: HostService.cs 프로젝트: dotnet/docfx
        private MarkupResult ParseCore(MarkupResult markupResult, FileAndType ft)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(markupResult.Html);
            var result = markupResult.Clone();

            var node = doc.DocumentNode.SelectSingleNode("//yamlheader");
            if (node != null)
            {
                using (var sr = new StringReader(StringHelper.HtmlDecode(node.InnerHtml)))
                {
                    result.YamlHeader = YamlUtility.Deserialize<Dictionary<string, object>>(sr).ToImmutableDictionary();
                }
                node.Remove();
            }

            result.FileLinkSources = GetFileLinkSource(ft, doc);
            result.LinkToFiles = result.FileLinkSources.Keys.ToImmutableArray();

            result.UidLinkSources = GetUidLinkSources(doc);
            result.LinkToUids = result.UidLinkSources.Keys.ToImmutableHashSet();

            if (result.Dependency.Length > 0)
            {
                result.Dependency =
                    (from d in result.Dependency
                     select
                        ((TypeForwardedToRelativePath)ft.File + (TypeForwardedToRelativePath)d)
                            .GetPathFromWorkingFolder()
                            .ToString()
                    ).ToImmutableArray();
            }
            using (var sw = new StringWriter())
            {
                doc.Save(sw);
                result.Html = sw.ToString();
            }
            return result;
        }
예제 #5
0
파일: HostService.cs 프로젝트: dotnet/docfx
 public MarkupResult Parse(MarkupResult markupResult, FileAndType ft)
 {
     if (markupResult == null)
     {
         throw new ArgumentNullException(nameof(markupResult));
     }
     if (ft == null)
     {
         throw new ArgumentNullException(nameof(ft));
     }
     return ParseCore(markupResult, ft);
 }
예제 #6
0
        private MarkupResult MarkupCore(string markdown, FileAndType ft)
        {
            var html = _engine.CreateDfmEngine(DocfxFlavoredMarked.Renderer).Markup(markdown, Path.Combine(ft.BaseDir, ft.File));
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            var result = new MarkupResult();

            var node = doc.DocumentNode.SelectSingleNode("//yamlheader");
            if (node != null)
            {
                using (var sr = new StringReader(StringHelper.HtmlDecode(node.InnerHtml)))
                {
                    result.YamlHeader = YamlUtility.Deserialize<Dictionary<string, object>>(sr).ToImmutableDictionary();
                }
                node.Remove();
            }
            var linkToFiles = new HashSet<string>();
            foreach (var link in from n in doc.DocumentNode.Descendants()
                                 where !string.Equals(n.Name, "xref", StringComparison.OrdinalIgnoreCase)
                                 from attr in n.Attributes
                                 where string.Equals(attr.Name, "src", StringComparison.OrdinalIgnoreCase) ||
                                       string.Equals(attr.Name, "href", StringComparison.OrdinalIgnoreCase)
                                 where !string.IsNullOrWhiteSpace(attr.Value)
                                 select attr)
            {
                string linkFile;
                string anchor = null;
                if (PathUtility.IsRelativePath(link.Value))
                {
                    var index = link.Value.IndexOf('#');
                    if (index == -1)
                    {
                        linkFile = link.Value;
                    }
                    else if (index == 0)
                    {
                        continue;
                    }
                    else
                    {
                        linkFile = link.Value.Remove(index);
                        anchor = link.Value.Substring(index);
                    }
                    var path = (RelativePath)ft.File + (RelativePath)linkFile;
                    var file = path.GetPathFromWorkingFolder();
                    link.Value = file + anchor;
                    linkToFiles.Add(HttpUtility.UrlDecode(file));
                }
            }
            result.LinkToFiles = linkToFiles.ToImmutableArray();
            result.LinkToUids = (from n in doc.DocumentNode.Descendants()
                                 where string.Equals(n.Name, "xref", StringComparison.OrdinalIgnoreCase)
                                 from attr in n.Attributes
                                 where string.Equals(attr.Name, "href", StringComparison.OrdinalIgnoreCase)
                                 where !string.IsNullOrWhiteSpace(attr.Value)
                                 select attr.Value).ToImmutableHashSet();
            using (var sw = new StringWriter())
            {
                doc.Save(sw);
                result.Html = sw.ToString();
            }
            return result;
        }
예제 #7
0
        public MarkupResult MarkupCore(string markdown, FileAndType ft)
        {
            var html = DocfxFlavoredMarked.Markup(markdown, Path.Combine(ft.BaseDir, ft.File));
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            var result = new MarkupResult();

            // TODO: how to get TITLE
            // InnerText in HtmlAgilityPack is not decoded, should be a bug
            var headerNode = doc.DocumentNode.SelectSingleNode("//h1|//h2|//h3");
            result.Title = StringHelper.HtmlDecode(headerNode?.InnerText);

            var node = doc.DocumentNode.SelectSingleNode("//yamlheader");
            if (node != null)
            {
                using (var sr = new StringReader(StringHelper.HtmlDecode(node.InnerHtml)))
                {
                    result.YamlHeader = YamlUtility.Deserialize<Dictionary<string, object>>(sr).ToImmutableDictionary();
                }
                node.Remove();
            }
            var linkToFiles = new HashSet<string>();
            foreach (var link in from n in doc.DocumentNode.Descendants()
                                 where !string.Equals(n.Name, "xref", StringComparison.OrdinalIgnoreCase)
                                 from attr in n.Attributes
                                 where string.Equals(attr.Name, "src", StringComparison.OrdinalIgnoreCase) ||
                                       string.Equals(attr.Name, "href", StringComparison.OrdinalIgnoreCase)
                                 where !string.IsNullOrWhiteSpace(attr.Value)
                                 select attr)
            {
                string linkFile;
                string anchor = null;
                if (PathUtility.IsRelativePath(link.Value))
                {
                    var index = link.Value.IndexOf('#');
                    if (index == -1)
                    {
                        linkFile = link.Value;
                    }
                    else if (index == 0)
                    {
                        continue;
                    }
                    else
                    {
                        linkFile = link.Value.Remove(index);
                        anchor = link.Value.Substring(index);
                    }
                    var path = (RelativePath)ft.File + (RelativePath)linkFile;
                    if (path.ParentDirectoryCount > 0)
                    {
                        Logger.LogError($"Cannot refer path: \"{path}\" out of project.", file: ft.File);
                        throw new DocumentException($"Cannot refer path \"{path}\" out of project in file \"{ft.File}\".");
                    }
                    var file = path.GetPathFromWorkingFolder();
                    link.Value = file + anchor;
                    linkToFiles.Add(HttpUtility.UrlDecode(file));
                }
            }
            result.LinkToFiles = linkToFiles.ToImmutableArray();
            result.LinkToUids = (from n in doc.DocumentNode.Descendants()
                                 where string.Equals(n.Name, "xref", StringComparison.OrdinalIgnoreCase)
                                 from attr in n.Attributes
                                 where string.Equals(attr.Name, "href", StringComparison.OrdinalIgnoreCase)
                                 where !string.IsNullOrWhiteSpace(attr.Value)
                                 select attr.Value).ToImmutableArray();
            using (var sw = new StringWriter())
            {
                doc.Save(sw);
                result.Html = sw.ToString();
            }
            return result;
        }