コード例 #1
0
        public void Visit(MdParagraph paragraph)
        {
            m_Writer.RequestBlankLine();

            var lines = paragraph.Text.ToString(m_Options).Split(s_LineBreakChars, StringSplitOptions.RemoveEmptyEntries);

            // skip paragraph if it is empty
            if (lines.Length == 0)
            {
                return;
            }

            for (var i = 0; i < lines.Length; i++)
            {
                // get the current line
                var line = lines[i];

                // if the line is not the last, append 2 spaces to
                // cause a line break in the output
                // for the last line, this can be omitted, as there will
                // be a blank line after the paragraph
                if (i != lines.Length - 1)
                {
                    line += "  ";
                }

                // no maximum line length specified => write the line to the output
                if (m_Options.MaxLineLength <= 0)
                {
                    m_Writer.WriteLine(line);
                }
                // maximum line length specified
                // => format lines to max length and write all lines to the output
                else
                {
                    var formattedLines = LineFormatter.GetLines(line, m_Options.MaxLineLength - m_Writer.PrefixLength);
                    foreach (var formattedLine in formattedLines)
                    {
                        m_Writer.WriteLine(formattedLine);
                    }
                }
            }

            m_Writer.RequestBlankLine();
        }
コード例 #2
0
        public void Visit(MdHeading block)
        {
            m_Writer.RequestBlankLine();

            var anchor = "";

            if (!String.IsNullOrWhiteSpace(block.Anchor))
            {
                if (m_Options.HeadingAnchorStyle == MdHeadingAnchorStyle.Tag ||
                    (m_Options.HeadingAnchorStyle == MdHeadingAnchorStyle.Auto && !StringComparer.Ordinal.Equals(block.Anchor, block.AutoGeneratedId)))
                {
                    anchor = $"<a id=\"{block.Anchor}\"></a>";
                }
            }

            if (m_Options.HeadingStyle == MdHeadingStyle.Setext && block.Level <= 2)
            {
                var underlineChar = block.Level == 1 ? '=' : '-';
                var text          = block.Text.ToString(m_Options);

                if (!String.IsNullOrEmpty(anchor))
                {
                    m_Writer.WriteLine(anchor);
                    m_Writer.WriteLine("");
                }

                // if no maximum line length was specified, write heading into a single line
                if (m_Options.MaxLineLength <= 0)
                {
                    m_Writer.WriteLine(text);
                    m_Writer.WriteLine(new string(underlineChar, text.Length));
                }
                // if max line length was specified, split the value into multiple lines if necessary
                else
                {
                    var headingTextLines = LineFormatter.GetLines(text, m_Options.MaxLineLength - m_Writer.PrefixLength);

                    foreach (var line in headingTextLines)
                    {
                        m_Writer.WriteLine(line);
                    }
                    m_Writer.WriteLine(new string(underlineChar, headingTextLines.Max(x => x.Length)));
                }
            }
            else
            {
                var lineBuilder = new StringBuilder();

                lineBuilder.Append('#', block.Level);
                lineBuilder.Append(' ');

                if (!String.IsNullOrEmpty(anchor))
                {
                    lineBuilder
                    .Append(anchor)
                    .Append(" ");
                }

                lineBuilder.Append(block.Text.ToString(m_Options));

                m_Writer.WriteLine(lineBuilder.ToString());
            }

            m_Writer.RequestBlankLine();
        }