Avoid ending Markdown fenced code blocks with gratuitous blank lines
This caused unnecessary empty <pre> blocks when converting markdown to HTML, made code blocks too large using QSyntaxHighlighter to highlight the whole block, and caused assymmetry when rewriting markdown. Pick-to: 6.3 Fixes: QTBUG-101031 Change-Id: I08016577ccb92edb4afae31d7df3259cb011d5c8 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
This commit is contained in:
parent
d75c595eac
commit
ff153d9874
@ -498,6 +498,14 @@ int QTextMarkdownImporter::cbText(int textType, const char *text, unsigned size)
|
|||||||
case MD_BLOCK_TD:
|
case MD_BLOCK_TD:
|
||||||
m_nonEmptyTableCells.append(m_tableCol);
|
m_nonEmptyTableCells.append(m_tableCol);
|
||||||
break;
|
break;
|
||||||
|
case MD_BLOCK_CODE:
|
||||||
|
if (s == Newline) {
|
||||||
|
// defer a blank line until we see something else in the code block,
|
||||||
|
// to avoid ending every code block with a gratuitous blank line
|
||||||
|
m_needsInsertBlock = true;
|
||||||
|
s = QString();
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -531,12 +539,12 @@ int QTextMarkdownImporter::cbText(int textType, const char *text, unsigned size)
|
|||||||
QString::number(bfmt.intProperty(QTextFormat::BlockQuoteLevel));
|
QString::number(bfmt.intProperty(QTextFormat::BlockQuoteLevel));
|
||||||
if (bfmt.hasProperty(QTextFormat::BlockCodeLanguage))
|
if (bfmt.hasProperty(QTextFormat::BlockCodeLanguage))
|
||||||
debugInfo += "in a code block"_L1;
|
debugInfo += "in a code block"_L1;
|
||||||
|
if (m_cursor->currentList())
|
||||||
|
debugInfo += "in a list"_L1;
|
||||||
qCDebug(lcMD) << textType << "in block" << m_blockType << s << qPrintable(debugInfo)
|
qCDebug(lcMD) << textType << "in block" << m_blockType << s << qPrintable(debugInfo)
|
||||||
<< "bindent" << bfmt.indent() << "tindent" << bfmt.textIndent()
|
<< "bindent" << bfmt.indent() << "tindent" << bfmt.textIndent()
|
||||||
<< "margins" << bfmt.leftMargin() << bfmt.topMargin() << bfmt.bottomMargin() << bfmt.rightMargin();
|
<< "margins" << bfmt.leftMargin() << bfmt.topMargin() << bfmt.bottomMargin() << bfmt.rightMargin();
|
||||||
}
|
}
|
||||||
qCDebug(lcMD) << textType << "in block" << m_blockType << s << "in list?" << m_cursor->currentList()
|
|
||||||
<< "indent" << m_cursor->blockFormat().indent();
|
|
||||||
return 0; // no error
|
return 0; // no error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,11 +156,11 @@ void QTextMarkdownWriter::writeFrame(const QTextFrame *frame)
|
|||||||
for (int col = cell.column(); col < spanEndCol; ++col)
|
for (int col = cell.column(); col < spanEndCol; ++col)
|
||||||
m_stream << "|";
|
m_stream << "|";
|
||||||
} else if (m_fencedCodeBlock && ending) {
|
} else if (m_fencedCodeBlock && ending) {
|
||||||
m_stream << m_linePrefix << QString(m_wrappedLineIndent, Space)
|
m_stream << Newline << m_linePrefix << QString(m_wrappedLineIndent, Space)
|
||||||
<< m_codeBlockFence << Newline << Newline;
|
<< m_codeBlockFence << Newline << Newline;
|
||||||
m_codeBlockFence.clear();
|
m_codeBlockFence.clear();
|
||||||
} else if (m_indentedCodeBlock && nextIsDifferent) {
|
} else if (m_indentedCodeBlock && nextIsDifferent) {
|
||||||
m_stream << Newline;
|
m_stream << Newline << Newline;
|
||||||
} else if (endingCol > 0) {
|
} else if (endingCol > 0) {
|
||||||
if (block.textList() || block.blockFormat().hasProperty(QTextFormat::BlockCodeLanguage)) {
|
if (block.textList() || block.blockFormat().hasProperty(QTextFormat::BlockCodeLanguage)) {
|
||||||
m_stream << Newline;
|
m_stream << Newline;
|
||||||
|
@ -380,7 +380,7 @@ void tst_QTextMarkdownImporter::avoidBlankLineAtBeginning_data()
|
|||||||
|
|
||||||
QTest::newRow("Text block") << QString("Markdown text") << 1;
|
QTest::newRow("Text block") << QString("Markdown text") << 1;
|
||||||
QTest::newRow("Headline") << QString("Markdown text\n============") << 1;
|
QTest::newRow("Headline") << QString("Markdown text\n============") << 1;
|
||||||
QTest::newRow("Code block") << QString(" Markdown text") << 2;
|
QTest::newRow("Code block") << QString(" Markdown text") << 1;
|
||||||
QTest::newRow("Unordered list") << QString("* Markdown text") << 1;
|
QTest::newRow("Unordered list") << QString("* Markdown text") << 1;
|
||||||
QTest::newRow("Ordered list") << QString("1. Markdown text") << 1;
|
QTest::newRow("Ordered list") << QString("1. Markdown text") << 1;
|
||||||
QTest::newRow("Blockquote") << QString("> Markdown text") << 1;
|
QTest::newRow("Blockquote") << QString("> Markdown text") << 1;
|
||||||
@ -501,18 +501,17 @@ void tst_QTextMarkdownImporter::fencedCodeBlocks_data()
|
|||||||
QTest::addColumn<QString>("expectedFenceChar");
|
QTest::addColumn<QString>("expectedFenceChar");
|
||||||
QTest::addColumn<QString>("rewrite");
|
QTest::addColumn<QString>("rewrite");
|
||||||
|
|
||||||
// TODO shouldn't add empty blocks: QTBUG-101031
|
|
||||||
QTest::newRow("backtick fence with language")
|
QTest::newRow("backtick fence with language")
|
||||||
<< "```pseudocode\nprint('hello world\\n')\n```\n"
|
<< "```pseudocode\nprint('hello world\\n')\n```\n"
|
||||||
<< 2 << 0 << "pseudocode" << "`"
|
<< 1 << 0 << "pseudocode" << "`"
|
||||||
<< "```pseudocode\nprint('hello world\\n')\n```\n\n";
|
<< "```pseudocode\nprint('hello world\\n')\n```\n\n";
|
||||||
QTest::newRow("tilde fence with language")
|
QTest::newRow("tilde fence with language")
|
||||||
<< "~~~pseudocode\nprint('hello world\\n')\n~~~\n"
|
<< "~~~pseudocode\nprint('hello world\\n')\n~~~\n"
|
||||||
<< 2 << 0 << "pseudocode" << "~"
|
<< 1 << 0 << "pseudocode" << "~"
|
||||||
<< "~~~pseudocode\nprint('hello world\\n')\n~~~\n\n";
|
<< "~~~pseudocode\nprint('hello world\\n')\n~~~\n\n";
|
||||||
QTest::newRow("embedded backticks")
|
QTest::newRow("embedded backticks")
|
||||||
<< "```\nnone `one` ``two``\n```\nplain\n```\n```three``` ````four````\n```\nplain\n"
|
<< "```\nnone `one` ``two``\n```\nplain\n```\n```three``` ````four````\n```\nplain\n"
|
||||||
<< 4 << 2 << QString() << "`"
|
<< 2 << 2 << QString() << "`"
|
||||||
<< "```\nnone `one` ``two``\n```\nplain\n\n```\n```three``` ````four````\n```\nplain\n\n";
|
<< "```\nnone `one` ``two``\n```\nplain\n\n```\n```three``` ````four````\n```\nplain\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,6 +50,7 @@ Now let's have an indented code block:
|
|||||||
}
|
}
|
||||||
|
|
||||||
and end with a fenced code block:
|
and end with a fenced code block:
|
||||||
|
|
||||||
~~~pseudocode
|
~~~pseudocode
|
||||||
#include <something.h>
|
#include <something.h>
|
||||||
#include <else.h>
|
#include <else.h>
|
||||||
|
@ -494,7 +494,7 @@ void tst_QTextMarkdownWriter::fromHtml_data()
|
|||||||
"\n\n";
|
"\n\n";
|
||||||
QTest::newRow("code") <<
|
QTest::newRow("code") <<
|
||||||
"<pre class=\"language-pseudocode\">\n#include \"foo.h\"\n\nblock {\n statement();\n}\n\n</pre>" <<
|
"<pre class=\"language-pseudocode\">\n#include \"foo.h\"\n\nblock {\n statement();\n}\n\n</pre>" <<
|
||||||
"```pseudocode\n#include \"foo.h\"\n\nblock {\n statement();\n}\n```\n\n";
|
"```pseudocode\n#include \"foo.h\"\n\nblock {\n statement();\n}\n\n```\n\n";
|
||||||
// TODO
|
// TODO
|
||||||
// QTest::newRow("escaped number and paren after double newline") <<
|
// QTest::newRow("escaped number and paren after double newline") <<
|
||||||
// "<p>(The first sentence of this paragraph is a line, the next paragraph has a number</p>13) but that's not part of an ordered list" <<
|
// "<p>(The first sentence of this paragraph is a line, the next paragraph has a number</p>13) but that's not part of an ordered list" <<
|
||||||
|
Loading…
x
Reference in New Issue
Block a user