Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 1a1c1b91 authored by Laurent Montel's avatar Laurent Montel 😁

Start to fix convert error when we convert html text to plaintext

I will send patch to grantlee soon
parent 5dff4c12
cmake_minimum_required(VERSION 3.5)
set(PIM_VERSION "5.10.40")
set(PIM_VERSION "5.10.41")
project(KPimTextEdit VERSION ${PIM_VERSION})
......
......@@ -41,6 +41,10 @@ set(kpimtextedit_texttospeech_SRCS
texttospeech/texttospeechactions.cpp
)
set(kpimtextedit_grantlee_builder_SRCS
grantleebuilder/plaintextmarkupbuilder.cpp
grantleebuilder/markupdirector.cpp
)
set(kpimtextedit_SRCS
emoticontexteditaction.cpp
......@@ -63,7 +67,7 @@ set(kpimtextedit_SRCS
ecm_qt_declare_logging_category(kpimtextedit_SRCS HEADER kpimtextedit_debug.h IDENTIFIER KPIMTEXTEDIT_LOG CATEGORY_NAME org.kde.pim.kpimtextedit)
add_library(KF5PimTextEdit ${kpimtextedit_SRCS} ${kpimtextedit_texteditor_SRCS} ${kpimtextedit_texttospeech_SRCS} ${kpimtextedit_composerng_SRCS})
add_library(KF5PimTextEdit ${kpimtextedit_SRCS} ${kpimtextedit_texteditor_SRCS} ${kpimtextedit_texttospeech_SRCS} ${kpimtextedit_composerng_SRCS} ${kpimtextedit_grantlee_builder_SRCS})
generate_export_header(KF5PimTextEdit BASE_NAME kpimtextedit)
......
......@@ -24,13 +24,14 @@
#include "richtextcomposeremailquotehighlighter.h"
#include "nestedlisthelper_p.h"
#include "richtextexternalcomposer.h"
#include "grantleebuilder/plaintextmarkupbuilder.h"
#include "grantleebuilder/markupdirector.h"
#include <QTextBlock>
#include <QTextLayout>
#include <QClipboard>
#include "richtextcomposeremailquotedecorator.h"
#include <grantlee/plaintextmarkupbuilder.h>
#include <KActionCollection>
#include <QAction>
......@@ -233,9 +234,9 @@ void RichTextComposer::forcePlainTextMarkup(bool force)
void RichTextComposer::insertPlainTextImplementation()
{
if (d->forcePlainTextMarkup) {
Grantlee::PlainTextMarkupBuilder *pb = new Grantlee::PlainTextMarkupBuilder();
KPIMTextEdit::PlainTextMarkupBuilder *pb = new KPIMTextEdit::PlainTextMarkupBuilder();
Grantlee::MarkupDirector *pmd = new Grantlee::MarkupDirector(pb);
KPIMTextEdit::MarkupDirector *pmd = new KPIMTextEdit::MarkupDirector(pb);
pmd->processDocument(document());
const QString plainText = pb->getResult();
document()->setPlainText(plainText);
......
/*
Copyright (c) 2019 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "markupdirector.h"
using namespace KPIMTextEdit;
MarkupDirector::MarkupDirector(Grantlee::AbstractMarkupBuilder *builder)
: Grantlee::MarkupDirector(builder)
{
}
MarkupDirector::~MarkupDirector()
{
}
QTextBlock::iterator
MarkupDirector::processFragment(QTextBlock::iterator it,
const QTextFragment &fragment,
QTextDocument const *doc)
{
//Same code as Grantlee + a fix !
// Q_D( MarkupDirector );
auto charFormat = fragment.charFormat();
if (charFormat.objectType() >= QTextFormat::UserObject) {
processCustomFragment(fragment, doc);
if (!it.atEnd())
return ++it;
return it;
}
auto textObject = doc->objectForFormat(charFormat);
if (textObject)
return processCharTextObject(it, fragment, textObject);
if (fragment.text().at(0).category() == QChar::Separator_Line) {
m_builder->addNewline();
if (!it.atEnd())
return ++it;
return it;
}
// The order of closing and opening tags can determine whether generated
// html
// is valid or not.
// When processing a document with formatting which appears as
// '<b><i>Some</i>
// formatted<b> text',
// the correct generated output will contain '<strong><em>Some</em>
// formatted<strong> text'.
// However, processing text which appears as '<i><b>Some</b> formatted<i>
// text' might be incorrectly rendered
// as '<strong><em>Some</strong> formatted</em> text' if tags which start at
// the same fragment are
// opened out of order. Here, tags are not nested properly, and the html
// would
// not be valid or render correctly by unforgiving parsers (like QTextEdit).
// One solution is to make the order of opening tags dynamic. In the above
// case, the em tag would
// be opened before the strong tag '<em><strong>Some</strong> formatted</em>
// text'. That would
// require knowledge of which tag is going to close first. That might be
// possible by examining
// the 'next' QTextFragment while processing one.
//
// The other option is to do pessimistic closing of tags.
// In the above case, this means that if a fragment has two or more formats
// applied (bold and italic here),
// and one of them is closed, then all tags should be closed first. They
// will
// of course be reopened
// if necessary while processing the next fragment.
// The above case would be rendered as '<strong><em>Some</em></strong><em>
// formatted</em> text'.
//
// The first option is taken here, as the redundant opening and closing tags
// in the second option
// didn't appeal.
// See testDoubleStartDifferentFinish,
// testDoubleStartDifferentFinishReverseOrder
processOpeningElements(it);
// If a sequence such as '<br /><br />' is imported into a document with
// setHtml, LineSeparator
// characters are inserted. Here I make sure to put them back.
auto sl = fragment.text().split(QChar(QChar::LineSeparator));
QStringListIterator i(sl);
auto paraClosed = false;
while (i.hasNext()) {
m_builder->appendLiteralText(i.next());
if (i.hasNext()) {
if (i.peekNext().isEmpty()) {
if (!paraClosed) {
m_builder->endParagraph();
paraClosed = true;
}
m_builder->addNewline();
} else if (paraClosed) {
m_builder->beginParagraph(/* blockAlignment */);
paraClosed = false;
} else {
//Bug fixing : missing end line here
m_builder->addNewline();
}
}
}
if (!it.atEnd())
++it;
processClosingElements(it);
return it;
}
/*
Copyright (c) 2019 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef MARKUPDIRECTOR_H
#define MARKUPDIRECTOR_H
#include <grantlee/markupdirector.h>
namespace KPIMTextEdit
{
class MarkupDirector : public Grantlee::MarkupDirector
{
public:
explicit MarkupDirector(Grantlee::AbstractMarkupBuilder *builder);
~MarkupDirector() override;
protected:
QTextBlock::iterator processFragment(QTextBlock::iterator it, const QTextFragment &fragment, const QTextDocument *doc) override;
};
}
#endif // MARKUPDIRECTOR_H
/*
Copyright (c) 2019 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "plaintextmarkupbuilder.h"
#include <QDebug>
using namespace KPIMTextEdit;
PlainTextMarkupBuilder::PlainTextMarkupBuilder()
{
}
void PlainTextMarkupBuilder::beginParagraph(Qt::Alignment a, qreal top, qreal bottom, qreal left, qreal right)
{
//TODO
//qDebug() << " left " << left << right << bottom << top << a;
}
/*
Copyright (c) 2019 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef PLAINTEXTMARKUPBUILDER_H
#define PLAINTEXTMARKUPBUILDER_H
#include "kpimtextedit_export.h"
#include <grantlee/plaintextmarkupbuilder.h>
namespace KPIMTextEdit
{
class KPIMTEXTEDIT_EXPORT PlainTextMarkupBuilder : public Grantlee::PlainTextMarkupBuilder
{
public:
PlainTextMarkupBuilder();
void beginParagraph(Qt::Alignment a, qreal top, qreal bottom, qreal left, qreal right) override;
};
}
#endif // PLAINTEXTMARKUPBUILDER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment