Esempio n. 1
0
        public override bool Execute()
        {
            if (!File.Exists(ChangelogFilePath))
            {
                Log.LogError($"Could not found changelog file {ChangelogFilePath}");
                return(false);
            }

            using var inputStream   = new FileStream(ChangelogFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            using var inputMdStream = new MarkdownStream(inputStream);

            if (OutputType.Equals("PlainText", StringComparison.OrdinalIgnoreCase))
            {
                ChangelogSectionContent = GetResult(
                    new MarkdownToPlainTextConverter().ExtractVersionSectionPlainText(inputMdStream));
            }
            else if (OutputType.Equals("Markdown", StringComparison.OrdinalIgnoreCase))
            {
                ChangelogSectionContent = GetResult(
                    new MarkdownToMarkdownConverter().ExtractVersionSection(inputMdStream));
            }
            else
            {
                Log.LogError($"Couldn't parse OutputType = {OutputType}");
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        public TokenStream Read(string markdown, string path)
        {
            _Context = MarkdownReaderMode.None;
            _Stream  = new MarkdownStream(markdown);

            YamlHeader();

            if (_YamlHeaderOnly)
            {
                return(_Output);
            }

            while (!_Stream.EOF)
            {
                var processed = UnderlineHeader() ||
                                HashHeader() ||
                                FencedBlock() ||
                                Link() ||
                                LineBreak();

                if (!processed)
                {
                    Text();
                }
            }

            return(_Output);
        }
        public async Task Run(
            [Option(0, "Path to the input Markdown file")] string inputFilePath,
            [Option("o", "Output file path")] string?outputFilePath = null,
            [Option("t", "Output type, either Markdown or PlainText")] ContentType contentType = ContentType.Markdown)
        {
            Console.OutputEncoding = Encoding.UTF8;

            await using var inputStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            using var inputMdStream = new MarkdownStream(inputStream);
            var text = contentType switch
            {
                ContentType.PlainText =>
                (await _plainTextConverter.ExtractVersionSectionPlainText(inputMdStream)).Content,
                _ => (await _markdownConverter.ExtractVersionSection(inputMdStream)).Content
            };

            if (outputFilePath == null)
            {
                await Console.Out.WriteLineAsync(text);
            }
            else
            {
                await using var outputFile = new FileStream(outputFilePath, FileMode.CreateNew);
                await using var writer     = new StreamWriter(outputFile);

                await writer.WriteLineAsync(text);
            }
        }
    }
Esempio n. 4
0
        private string UnwrapStyleMarkers(MarkdownStream stream, out MarkdownTokens flag)
        {
            flag = MarkdownTokens.None;

            // Check for style
            var styleChar     = stream.Current;
            var stylePrevious = stream.Previous;
            var styleCount    = styleChar == Asterix || styleChar == Underscore?stream.Skip(styleChar, max : 0) : 0;

            var codeCount = styleChar == Backtick?stream.Skip(Backtick, max : 0) : 0;

            var text = stream.CaptureUntil(IsTextStop, ignoreEscaping: false);

            // Check for italic and bold endings
            if (styleCount > 0)
            {
                if (stream.Current == styleChar)
                {
                    var styleEnding = stream.Skip(styleChar, max: styleCount);

                    // Add back underscores within text
                    if (styleChar == Underscore && stylePrevious != Whitespace)
                    {
                        return(Pad(text, styleChar, left: styleCount, right: styleCount));
                    }

                    // Add back asterixes/underscores that are part of text
                    if (styleEnding < styleCount)
                    {
                        text = Pad(text, styleChar, left: styleCount - styleEnding);
                    }

                    if (styleEnding == 1 || styleEnding == 3)
                    {
                        flag |= MarkdownTokens.Italic;
                    }

                    if (styleEnding >= 2)
                    {
                        flag |= MarkdownTokens.Bold;
                    }
                }
                else
                {
                    // Add back asterixes/underscores that are part of text
                    text = Pad(text, styleChar, left: styleCount);
                }
            }

            if (codeCount > 0)
            {
                if (stream.Current == styleChar)
                {
                    var codeEnding = stream.Skip(styleChar, max: 1);

                    // Add back backticks that are part of text
                    if (codeEnding < codeCount)
                    {
                        text = Pad(text, styleChar, left: codeCount - codeEnding);
                    }

                    if (codeEnding == 1)
                    {
                        flag |= MarkdownTokens.Code;
                    }
                }
                else
                {
                    // Add back backticks that are part of text
                    text = Pad(text, styleChar, left: codeCount);
                }
            }
            return(text);
        }