List <Snippet> FilesToSnippets(string key)
 {
     return(snippetSourceFiles
            .Where(file => file.EndsWith(key, StringComparison.OrdinalIgnoreCase))
            .Select(file =>
     {
         var allText = File.ReadAllText(file);
         return Snippet.Build(
             startLine: 1,
             endLine: allText.LineCount(),
             value: allText,
             key: key,
             language: Path.GetExtension(file).Substring(1),
             path: file);
     })
            .ToList());
 }
Exemplo n.º 2
0
        bool TryGetSupText(Snippet snippet, string anchor, [NotNullWhen(true)] out string?supText)
        {
            var linkForAnchor = $"<a href='#{anchor}' title='Start of snippet'>anchor</a>";

            if (snippet.Path == null)
            {
                // id anchors not supported on TFS
                //https://developercommunity.visualstudio.com/content/problem/63289/anchors-in-markdown-documents-not-working.html
                if (linkFormat != LinkFormat.Tfs)
                {
                    supText = linkForAnchor;
                    return(true);
                }

                supText = null;
                return(false);
            }

            var path = snippet.Path.Replace(@"\", "/");

            if (!path.StartsWith(targetDirectory))
            {
                //if file is not in the targetDirectory then the url wont work
                supText = linkForAnchor;
                return(true);
            }

            path = path.Substring(targetDirectory.Length);

            var sourceLink    = BuildLink(snippet, path);
            var linkForSource = $"<a href='{urlPrefix}{sourceLink}' title='Snippet source file'>snippet source</a>";

            if (linkFormat == LinkFormat.Tfs)
            {
                supText = linkForSource;
            }
            else
            {
                supText = $"{linkForSource} | {linkForAnchor}";
            }

            return(true);
        }
Exemplo n.º 3
0
        static IEnumerable <Snippet> GetSnippets(TextReader stringReader, string path, int maxWidth, string newLine)
        {
            var       language  = GetLanguageFromPath(path);
            LoopStack loopStack = new();
            var       index     = 0;

            while (true)
            {
                index++;
                var line = stringReader.ReadLine();
                if (line == null)
                {
                    if (loopStack.IsInSnippet)
                    {
                        var current = loopStack.Current;
                        yield return(Snippet.BuildError(
                                         error: "Snippet was not closed",
                                         path: path,
                                         lineNumberInError: current.StartLine + 1,
                                         key: current.Key));
                    }

                    break;
                }

                var trimmedLine = line.Trim();

                if (StartEndTester.IsStart(trimmedLine, path, out var key, out var endFunc))
                {
                    loopStack.Push(endFunc, key, index, maxWidth, newLine);
                    continue;
                }

                if (!loopStack.IsInSnippet)
                {
                    continue;
                }

                if (!loopStack.Current.EndFunc(trimmedLine))
                {
                    Snippet?error = null;
                    try
                    {
                        loopStack.AppendLine(line);
                    }
                    catch (LineTooLongException exception)
                    {
                        var current = loopStack.Current;
                        error = Snippet.BuildError(
                            error: "Line too long: " + exception.Line,
                            path: path,
                            lineNumberInError: current.StartLine + 1,
                            key: current.Key);
                    }

                    if (error != null)
                    {
                        yield return(error);

                        break;
                    }

                    continue;
                }

                yield return(BuildSnippet(path, loopStack, language, index));

                loopStack.Pop();
            }
        }
Exemplo n.º 4
0
 static void WriteSnippetValueAndLanguage(Action <string> appendLine, Snippet snippet)
 {
     appendLine($"```{snippet.Language}");
     appendLine(snippet.Value);
     appendLine("```");
 }
Exemplo n.º 5
0
 static void WriteSnippet(TextWriter writer, Snippet snippet)
 {
     writer.WriteLine($@"```{snippet.Language}");
     writer.WriteLine(snippet.Value);
     writer.WriteLine(@"```");
 }