Esempio n. 1
0
        // AllowNull is required on the out parameters otherwise PEVerify on the resulting DLL fails with error:
        // [IL]: Error: [C:\Users\Tim\Code\Tim Murphy\Casper\tests\Casper.Data.Git.Specifications\bin\Debug\Casper.Data.Git.dll : Casper.Data.Git.Infrastructure.YamlMarkdown::Deserialize][offset 0x0000008B] Branch out of finally block.
        // 1 Error(s) Verifying Casper.Data.Git.dll
        // todo: report issue to Simon Cropp, NullGuard.Fody
        public void Deserialize(string markdownWithFrontMatter, [AllowNull] out MarkdownMetadata markdownMetadata, [AllowNull] out string markdown)
        {
            LogTo.Trace("Deserialize(markdownWithFrontMatter: {0}, markdownMetadata, markdown)", markdownWithFrontMatter);

            string metaDataText;

            SplitMarkdownWithFrontMatter(markdownWithFrontMatter, out metaDataText, out markdown);

            using (var textReader = new StringReader(metaDataText))
            {
                var deserializer = new Deserializer();
                markdownMetadata = deserializer.Deserialize <MarkdownMetadata>(textReader);
            }
        }
Esempio n. 2
0
        private static void ProcessLink(
            Link link,
            IEnumerable <SourceInformation> items,
            MarkdownMetadata metadata,
            Counts counts,
            ConcurrentBag <Result> results)
        {
            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (link.LinkType)
            {
            case LinkType.AbsolutePath:
                counts.CountAbsolutePaths++;

                // Verify all absolute wiki links point to existing pages.
                if (items.All(i => i.SourcePath != link.Ref))
                {
                    counts.CountAbsolutePathsNotFound++;
                    results.Add(new Result(metadata.SourceInformation, link, false));
                }
                else
                {
                    counts.CountAbsolutePathsFound++;
                    results.Add(new Result(metadata.SourceInformation, link, true));
                }

                break;

            case LinkType.RelativePath:
                counts.CountRelativePaths++;

                // Verify all relative wiki links point to existing pages.
                if (items.All(i => !i.SourcePath.EndsWith(link.Ref)))
                {
                    counts.CountRelativePathsNotFound++;
                    results.Add(new Result(metadata.SourceInformation, link, false));
                }
                else
                {
                    counts.CountRelativePathsFound++;
                    results.Add(new Result(metadata.SourceInformation, link, true));
                }

                break;

            case LinkType.NamedLink:
                counts.CountHeadingLinks++;
                var refToFind = link.Ref.Substring(1);     // Removes the leading #.

                // Verify all heading links point to existing headers or HtmlIds
                var foundInHeadings = metadata.Headings.Any(h => h.HeadingLinkText == refToFind);
                var foundInHtml     = metadata.HtmlIds.Any(h => h.Id == refToFind);

                if (!foundInHeadings && !foundInHtml)
                {
                    counts.CountHeadingLinksNotFound++;
                    results.Add(new Result(metadata.SourceInformation, link, false));
                }
                else
                {
                    counts.CountHeadingLinksFound++;
                    results.Add(new Result(metadata.SourceInformation, link, true));
                }

                break;

            case LinkType.Url:

                // todo Check Urls?
                break;

            default:

                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Parse the Content of a Markdown file.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="sourceInformation"></param>
        /// <returns></returns>
        public static MarkdownMetadata Parse(string content, SourceInformation sourceInformation)
        {
            var output = new MarkdownMetadata
            {
                Content           = content,
                SourceInformation = sourceInformation
            };

            var lines = content.Split(new[] { "\n" }, StringSplitOptions.None);

            var lineNumber = 0;

            foreach (var line in lines)
            {
                lineNumber++;

                var trimmedLine = line.Trim();

                // todo Find Links. There could be multiple on a single line. "[text](ref)"

                // Process header line.
                if (trimmedLine.StartsWith("#"))
                {
                    // Count number of #'s and store that in Level.
                    var level = 1;

                    while (trimmedLine.Substring(level, 1).Equals("#"))
                    {
                        level++;
                    }

                    var text = trimmedLine.Substring(level).Trim();

                    var item = new Heading(level, text);
                    output.Headings.Add(item);

                    // There can be nothing more to parse on this line.
                    continue;
                }

                // Process Html Id
                var htmlMatch = HtmlRegex.Match(trimmedLine);

                if (htmlMatch.Success && htmlMatch.Groups.Count == 2)
                {
                    var item = new HtmlId(htmlMatch.Groups["id"].Value, trimmedLine);
                    output.HtmlIds.Add(item);

                    // There can be nothing more to parse on this line.
                    continue;
                }

                // Process Links
                var match = LinkRegex.Match(trimmedLine);

                if (match.Success && match.Groups.Count == 3)
                {
                    var item = new Link(match.Groups["text"].Value, match.Groups["ref"].Value, lineNumber);
                    output.Links.Add(item);
                }
            }

            return(output);
        }
Esempio n. 4
0
        public string Serialize(MarkdownMetadata metadata, string markdown)
        {
            var yaml = YamlSerialize(metadata);

            return(string.Format("---\r\n{0}\r\n---\r\n{1}", yaml, markdown));
        }