Пример #1
0
        bool TryProcessSnippetLine(TextWriter writer, IndexReader reader, string line, List <MissingSnippet> missings, List <Snippet> used)
        {
            string key;

            if (!SnippetKeyReader.TryExtractKeyFromLine(line, out key))
            {
                return(false);
            }
            writer.WriteLine($"<!-- snippet: {key} -->");

            IReadOnlyList <Snippet> group;

            if (!snippets.TryGetValue(key, out group))
            {
                var missing = new MissingSnippet(
                    key: key,
                    line: reader.Index);
                missings.Add(missing);
                var message = $"** Could not find snippet '{key}' **";
                writer.WriteLine(message);
                return(true);
            }

            appendSnippetGroup(key, group, writer);
            used.AddRange(group);
            return(true);
        }
Пример #2
0
    public void MissingSpaces()
    {
        string key;

        SnippetKeyReader.TryExtractKeyFromLine("snippet:snippet", out key);
        Assert.AreEqual("snippet", key);
    }
Пример #3
0
    public void WithDashes()
    {
        string key;

        SnippetKeyReader.TryExtractKeyFromLine("snippet:my-code-snippet", out key);
        Assert.AreEqual("my-code-snippet", key);
    }
        bool TryProcessSnippetLine(TextWriter writer, IndexReader reader, string line, List <MissingSnippet> missings, List <Snippet> used)
        {
            if (!SnippetKeyReader.TryExtractKeyFromLine(line, out var key))
            {
                return(false);
            }

            writer.WriteLine($"<!-- snippet: {key} -->");

            if (TryGetFromFiles(key, out var snippetFromFiles))
            {
                appendSnippetGroup(key, snippetFromFiles, writer);
                writer.WriteLine($"<!-- endsnippet -->");
                used.AddRange(snippetFromFiles);
                return(true);
            }

            var missing = new MissingSnippet(
                key: key,
                line: reader.Index);

            missings.Add(missing);
            var message = $"** Could not find snippet '{key}' **";

            writer.WriteLine(message);
            return(true);
        }
Пример #5
0
    void AddInclude(List <Line> lines, Line line, List <Include> usedIncludes, int index, Include include)
    {
        usedIncludes.Add(include);
        var path = GetPath(include);

        if (!include.Lines.Any())
        {
            line.Current = $@"<!-- include: {include.Key}. path: {path} -->";
            return;
        }

        var firstLine = include.Lines.First();

        var linesCount = include.Lines.Count;

        if (SnippetKeyReader.IsSnippetLine(firstLine))
        {
            line.Current = $@"<!-- include: {include.Key}. path: {path} -->";
            lines.Insert(index + 1, new Line(firstLine, include.Path, 1));
            lines.Insert(index + 2, new Line($@"<!-- end include: {include.Key}. path: {path} -->", include.Path, 1));

            if (linesCount == 1)
            {
                return;
            }
        }
        else
        {
            line.Current = $@"{firstLine} <!-- include: {include.Key}. path: {path} -->";

            if (linesCount == 1)
            {
                return;
            }
        }

        for (var includeIndex = 1; includeIndex < linesCount - 1; includeIndex++)
        {
            var includeLine = include.Lines[includeIndex];

            //todo: path of include
            lines.Insert(index + includeIndex, new Line(includeLine, include.Path, includeIndex));
        }

        var lastLine = include.Lines.Last();

        if (SnippetKeyReader.IsSnippetLine(lastLine))
        {
            lines.Insert(index + linesCount - 1, new Line(lastLine, include.Path, linesCount));
            lines.Insert(index + linesCount, new Line($@"<!-- end include: {include.Key}. path: {path} -->", include.Path, linesCount));
        }
        else
        {
            lines.Insert(index + linesCount - 1, new Line($@"{lastLine} <!-- end include: {include.Key}. path: {path} -->", include.Path, linesCount));
        }
    }
Пример #6
0
    public void MissingSpaces()
    {
        var exception = Assert.Throws <MarkdownProcessingException>(() => SnippetKeyReader.TryExtractKeyFromLine("snippet:snippet", out var key));

        Assert.True(exception.Message == "Invalid syntax for the snippet 'snippet': There must be a space before the start of the key.");
    }
Пример #7
0
 public void ExtraSpace()
 {
     SnippetKeyReader.TryExtractKeyFromLine("snippet:  snippet   ", out var key);
     Assert.Equal("snippet", key);
 }
Пример #8
0
 public void WithDashes()
 {
     SnippetKeyReader.TryExtractKeyFromLine("snippet: my-code-snippet", out var key);
     Assert.Equal("my-code-snippet", key);
 }
        internal ProcessResult Apply(List <Line> lines, string newLine, string?relativePath)
        {
            var  missingSnippets  = new List <MissingSnippet>();
            var  validationErrors = new List <ValidationError>();
            var  missingIncludes  = new List <MissingInclude>();
            var  usedSnippets     = new List <Snippet>();
            var  usedIncludes     = new List <Include>();
            var  builder          = new StringBuilder();
            Line?tocLine          = null;
            var  headerLines      = new List <Line>();

            for (var index = 0; index < lines.Count; index++)
            {
                var line = lines[index];

                if (validateContent)
                {
                    var errors = ContentValidation.Verify(line.Original).ToList();
                    if (errors.Any())
                    {
                        validationErrors.AddRange(errors.Select(error => new ValidationError(error.error, line.LineNumber, error.column, line.Path)));
                        continue;
                    }
                }

                if (includeProcessor.TryProcessInclude(lines, line, usedIncludes, index, missingIncludes))
                {
                    continue;
                }

                if (line.Current.StartsWith("#"))
                {
                    if (tocLine != null)
                    {
                        headerLines.Add(line);
                    }

                    continue;
                }

                if (line.Current == "toc")
                {
                    tocLine = line;
                    continue;
                }

                if (SnippetKeyReader.TryExtractKeyFromLine(line, out var key))
                {
                    builder.Clear();

                    void AppendLine(string s)
                    {
                        builder.Append(s);
                        builder.Append(newLine);
                    }

                    ProcessSnippetLine(AppendLine, missingSnippets, usedSnippets, key, line);
                    builder.TrimEnd();
                    line.Current = builder.ToString();
                }
            }

            if (writeHeader)
            {
                lines.Insert(0, new Line(HeaderWriter.WriteHeader(relativePath !, header, newLine), "", 0));
            }

            if (tocLine != null)
            {
                tocLine.Current = TocBuilder.BuildToc(headerLines, tocLevel, tocExcludes, newLine);
            }

            return(new ProcessResult(
                       missingSnippets: missingSnippets,
                       usedSnippets: usedSnippets.Distinct().ToList(),
                       usedIncludes: usedIncludes.Distinct().ToList(),
                       missingIncludes: missingIncludes,
                       validationErrors: validationErrors));
        }
Пример #10
0
 public void Simple()
 {
     SnippetKeyReader.TryExtractKeyFromLine(new Line("snippet: snippet", "path", 1), out var key);
     Assert.Equal("snippet", key);
 }