Example #1
0
            public static void Transform(string sourceText, TextWriter sink, params MarkdownTransformer[] transformers)
            {
                var source = new MarkdownSource(sourceText);

                foreach (var transformer in transformers)
                {
                    source = transformer.Transform(source);
                }

                sink.Write(source.WholeSource);
            }
Example #2
0
            protected override MarkdownSource Transform(MarkdownSource source)
            {
                var newSource = new StringBuilder();
                var anchor    = 0;

                foreach (var obj in source.Document.Descendants())
                {
                    switch (obj)
                    {
                    case FencedCodeBlock fencedCodeBlock:
                        if (fencedCodeBlock.Info == "csharp" && fencedCodeBlock.Arguments != null)
                        {
                            var targetPath = Path.Combine(baseDir, fencedCodeBlock.Arguments);
                            if (File.Exists(targetPath))
                            {
                                if (TryParseFencedCode(fencedCodeBlock, source.WholeSource, out var result))
                                {
                                    var placeholderMatch = NamespacePlaceholderRegex.Match(result.code);
                                    if (placeholderMatch.Success)
                                    {
                                        var ns     = placeholderMatch.Groups["namespace"].Value.Trim();
                                        var csCode = File.ReadAllText(targetPath);

                                        foreach (var match2 in FinderRegex.Matches(csCode).OfType <Match>())
                                        {
                                            var ns2 = match2.Groups["namespace"].Value.Trim();
                                            if (ns == ns2)
                                            {
                                                var csCodePart = Regex.Replace(match2.Value, @"^\s*\[Test\]\s*$", "", RegexOptions.Multiline);
                                                csCodePart = Regex.Replace(csCodePart, @"\bTestContext[.]WriteLine\b", "Debug.WriteLine", RegexOptions.Multiline);

                                                newSource.Append(source.WholeSource, anchor, fencedCodeBlock.Span.Start - anchor);
                                                newSource.Append(result.fence);
                                                newSource.AppendLine("csharp");
                                                newSource.Append(csCodePart);
                                                newSource.AppendLine(result.fence);
                                                anchor = fencedCodeBlock.Span.End + 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                }

                newSource.Append(source.WholeSource, anchor, source.WholeSource.Length - anchor);

                return(new MarkdownSource(newSource.ToString()));
            }
Example #3
0
            protected override MarkdownSource Transform(MarkdownSource source)
            {
                foreach (var obj in source.Document.Descendants())
                {
                    switch (obj)
                    {
                    case LinkInline linkInline:
                        //Debug.WriteLine($"{linkInline.Url}");
                        linkInline.Url = ResolveRelativePath(linkInline.Url, sourceDir, destinationDir);
                        break;
                    }
                }

                return(source.Update());
            }
Example #4
0
 protected abstract MarkdownSource Transform(MarkdownSource source);