Esempio n. 1
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;
        }
Esempio n. 2
0
        private void WrapUpSnippetCode(CodeSnippet codeSnippet, string commentLine)
        {
            // If the commentLine is empty, then all the snippet should be inserted (default: all bold)
            if (commentLine != string.Empty)
            {
                var linesToMark = new SortedSet<int>();
                var strikedLines = new SortedSet<int>();

                // These are the lines that are bolded
                linesToMark = this.GetLinesFromCommand(commentLine, "mark");

                try
                {
                    // These are the lines that are strikethrough
                    strikedLines = this.GetLinesFromCommand(commentLine, "strike");
                }
                catch (Exception)
                {
                    strikedLines = null;
                }

                if (strikedLines != null)
                {
                    // remove the strikedthrough lines from the list
                    linesToMark.ExceptWith(strikedLines.ToList());
                }

                codeSnippet.Code = this.RemoveExtraLines(linesToMark, codeSnippet.Code);
            }
        }
Esempio n. 3
0
        private CodeSnippet ReadSnippetFromFile(string snippetPath)
        {
            CodeSnippet snippet = new CodeSnippet(this.SnippetTemplate, this.VSContentTemplate);
            string snippetContent = File.ReadAllText(snippetPath);

            var matchTitle = Regex.Match(snippetContent, "<title\\b[^>]*>(.*?)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            var matchLang = Regex.Match(snippetContent, "<code language=\"(.*)\">", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            var codeStart = snippetContent.IndexOf(matchLang.Value) + matchLang.Value.Length;
            var codeEnd = snippetContent.ToLower().LastIndexOf("</code>");

            if (snippetContent.Contains("<![CDATA["))
            {
                codeStart = snippetContent.IndexOf("<![CDATA[", codeStart) + 9;
                codeEnd = snippetContent.LastIndexOf("]]>");
            }

            snippet.Title = matchTitle.Groups[1].Value;
            snippet.Language = matchLang.Groups[1].Value;
            snippet.Code = snippetContent.Substring(codeStart, codeEnd - codeStart);

            return snippet;
        }