Ejemplo n.º 1
0
        private string PrcessCodeSnippetHeaders(string content)
        {
            var parser = new HtmlParser(content);
            var codeSnippetBlocks = parser.GetElements("code");

            if (codeSnippetBlocks == null)
            {
                return content;
            }

            string language = string.Empty;
            int currentCount = 1;
            int total = codeSnippetBlocks.Count();

            foreach (var codeBlock in codeSnippetBlocks)
            {
                // Notifying progress
                ProgressNotificationHelper.ReportProgress(currentCount / total, "Processing Code Snippets Headers");
                currentCount++;
                if (!codeBlock.Attributes.Contains("class"))
                {
                    continue;
                }

                language = codeBlock.Attributes["class"].Value;
                if (!string.IsNullOrWhiteSpace(language))
                {
                    codeBlock.ParentNode.ParentNode.InsertBefore(
                        HtmlNode.CreateNode(string.Format(CodeHeaderTemplate, language)),
                        codeBlock.ParentNode);
                }
            }

            return parser.Html;
        }
Ejemplo n.º 2
0
 public List<CodeSnippet> GetCodeSnippets(string markdownContent, string[] externalSnippets)
 {
     var parser = new HtmlParser(markdownContent);
     List<CodeSnippet> snippets = default(List<CodeSnippet>);
     snippets = this.ReadSnippets(parser);
     snippets.AddRange(this.GetExternalSnippets());
     return snippets;
 }
Ejemplo n.º 3
0
 public CodeSnippetHighlighter(string htmlText)
 {
     this.parser = new HtmlParser(htmlText);
     this.originalText = htmlText;
 }
Ejemplo n.º 4
0
        private List<CodeSnippet> ReadSnippets(HtmlParser parser)
        {
            List<CodeSnippet> codeSnippetsList = new List<CodeSnippet>();
            var codeNodes = parser.GetElements("pre/code");

            if (codeNodes == null)
            {
                return codeSnippetsList;
            }

            StepNotificationHelper.Initialize(codeNodes.Count() + 1);
            StepNotificationHelper.Step("Scanning snippets...");

            foreach (HtmlNode codeElement in codeNodes)
            {
                if (this.Cancel)
                {
                    throw new OperationCanceledException("Operation Cancelled");
                }

                StepNotificationHelper.Step();
                var previousElement = parser.FindPreviousElement(codeElement.ParentNode);
                if (previousElement != null && previousElement.Name == "span")
                {
                    previousElement = parser.FindPreviousElement(previousElement);
                }

                if (previousElement == null || (previousElement.Name != "p" && previousElement.NodeType != HtmlNodeType.Comment))
                {
                    continue;
                }

                HtmlNode commentNode = null;
                if (previousElement.NodeType == HtmlNodeType.Comment)
                {
                    commentNode = previousElement;
                    previousElement = parser.FindPreviousElement(previousElement);
                    if (previousElement.Name != "p" || !previousElement.InnerText.StartsWith("(Code Snippet"))
                    {
                        continue;
                    }
                }

                // Check that title format is "(Code Snippet - title goes here)"
                Regex codeSnippetRegex = new Regex(@"^\(Code Snippet \p{Pd} .+\)$");
                if (!codeSnippetRegex.IsMatch(previousElement.InnerText))
                {
                    continue;
                }

                // Check that the language name is not null nor an empty string
                string languageName;
                if (codeElement.Attributes["Class"] != null)
                {
                    languageName = HttpUtility.HtmlDecode(codeElement.Attributes["Class"].Value).Trim();
                    languageName = this.StandardizeCodeLanguage(languageName);

                    if (languageName == string.Empty)
                    {
                        continue;
                    }
                }
                else
                {
                    continue;
                }

                // Search for the code snippet title
                string codeSnippetTitle = HttpUtility.HtmlDecode(previousElement.InnerText);
                codeSnippetTitle = codeSnippetTitle.Substring(16, codeSnippetTitle.Length - 17);

                CodeSnippet codeSnippet = new CodeSnippet(this.SnippetTemplate, this.VSContentTemplate)
                {
                    Code = HttpUtility.HtmlDecode(codeElement.InnerText.Trim()),
                    Language = languageName,
                    Title = codeSnippetTitle,
                    Author = this.Author
                };

                var commentLine = string.Empty;
                if (commentNode != null)
                {
                    commentLine = commentNode.InnerText.Replace("<!--", string.Empty).Replace("-->", string.Empty);
                }

                try
                {
                    this.WrapUpSnippetCode(codeSnippet, commentLine);
                    codeSnippet.Code = codeSnippet.Code.Trim();
                    codeSnippetsList.Add(codeSnippet);
                }
                catch (Exception)
                {
                    continue;
                }
            }

            return codeSnippetsList;
        }