QTextMarkdownWriter: fix some bad cases with word wrap

If any non-breakable content (such as a link) already went past
80 columns, or if a word ended on column 80, it didn't wrap the rest of
the paragraph following.

Change-Id: I27dc0474f18892c34ee2514ea6d5070dae29424f
Reviewed-by: Gatis Paeglis <gatis.paeglis@qt.io>
This commit is contained in:
Shawn Rutledge 2019-05-20 11:50:26 +02:00
parent 7224d0e427
commit 280d679c55
4 changed files with 36 additions and 6 deletions

View File

@ -212,10 +212,19 @@ QTextMarkdownWriter::ListInfo QTextMarkdownWriter::listInfo(QTextList *list)
static int nearestWordWrapIndex(const QString &s, int before)
{
before = qMin(before, s.length());
for (int i = before - 1; i >= 0; --i) {
if (s.at(i).isSpace())
return i;
int fragBegin = qMax(before - 15, 0);
if (lcMDW().isDebugEnabled()) {
QString frag = s.mid(fragBegin, 30);
qCDebug(lcMDW) << frag << before;
qCDebug(lcMDW) << QString(before - fragBegin, Period) + QLatin1Char('<');
}
for (int i = before - 1; i >= 0; --i) {
if (s.at(i).isSpace()) {
qCDebug(lcMDW) << QString(i - fragBegin, Period) + QLatin1Char('^') << i;
return i;
}
}
qCDebug(lcMDW, "not possible");
return -1;
}
@ -251,7 +260,7 @@ static void maybeEscapeFirstChar(QString &s)
int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ignoreFormat)
{
int ColumnLimit = 80;
const int ColumnLimit = 80;
QTextBlockFormat blockFmt = block.blockFormat();
bool indentedCodeBlock = false;
if (block.textList()) { // it's a list-item
@ -419,12 +428,18 @@ int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ign
int fragLen = fragmentText.length();
bool breakingLine = false;
while (i < fragLen) {
if (col >= ColumnLimit) {
m_stream << Newline << wrapIndentString;
col = m_wrappedLineIndent;
while (fragmentText[i].isSpace())
++i;
}
int j = i + ColumnLimit - col;
if (j < fragLen) {
int wi = nearestWordWrapIndex(fragmentText, j);
if (wi < 0) {
j = fragLen;
} else {
} else if (wi >= i) {
j = wi;
breakingLine = true;
}

View File

@ -13,7 +13,8 @@ MacFarlane writes:
> > as readable as possible. The idea is that a Markdown-formatted document should
> > be publishable as-is, as plain text, without looking like it's been marked up
> > with tags or formatting instructions. (
> > [http://daringfireball.net/projects/markdown/](http://daringfireball.net/projects/markdown/))
> > [http://daringfireball.net/projects/markdown/](http://daringfireball.net/projects/markdown/)
> > )
> The point can be illustrated by comparing a sample of AsciiDoc with an
> equivalent sample of Markdown. Here is a sample of AsciiDoc from the AsciiDoc

View File

@ -0,0 +1,13 @@
[The CommonMark Specification](https://spec.commonmark.org/0.29/) is the
conservative formal specification of the Markdown format, while
[GitHub Flavored Markdown](https://guides.github.com/features/mastering-markdown/#GitHub-flavored-markdown)
adds extra features such as task lists and tables.
Qt owes thanks to the authors of the [MD4C parser](https://github.com/mity/md4c)
for making markdown import possible. The QTextMarkdownWriter class does not
have such dependencies, and also has not yet been tested as extensively, so we
do not yet guarantee that we are able to rewrite every Markdown document that
you are able to read and display with Text or QTextEdit. But you are free to
write [bugs](https://bugreports.qt.io) about any troublesome cases that you
encounter.

View File

@ -358,6 +358,7 @@ void tst_QTextMarkdownWriter::rewriteDocument_data()
QTest::newRow("block quotes") << "blockquotes.md";
QTest::newRow("example") << "example.md";
QTest::newRow("list items after headings") << "headingsAndLists.md";
QTest::newRow("word wrap") << "wordWrap.md";
}
void tst_QTextMarkdownWriter::rewriteDocument()