Commit a776d7e0 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Add support for using discount lib as Qtextdocument::markdown was

implemented in qt5.14 and it's buggy
parent a386d857
......@@ -8,8 +8,17 @@ add_subdirectory(calendar)
add_subdirectory(gnupgwks)
add_subdirectory(pkpass)
add_subdirectory(itinerary)
if (Qt5Widgets_VERSION VERSION_LESS 5.14.0)
message(STATUS "Markdown plugin not supported before qt 5.14.0")
set(USE_DISCOUNT_LIB TRUE)
if(discount_FOUND)
MESSAGE(STATUS "build bodyparformatter mardown with discount support")
add_subdirectory(markdown)
else()
message(STATUS "Markdown plugin not supported before qt 5.14.0")
endif()
else()
set(USE_DISCOUNT_LIB FALSE)
MESSAGE(STATUS "build bodyparformatter mardown with QTextDocument support")
add_subdirectory(markdown)
endif()
set(mardown_engine_SRCS)
set(discount_LIB)
if(USE_DISCOUNT_LIB)
set(mardown_engine_SRCS mardowndiscount.cpp)
set(discount_LIB discount::Lib)
else()
set(mardown_engine_SRCS mardownqtextdocument.cpp)
endif()
add_definitions(-DTRANSLATION_DOMAIN=\"messageviewer_text_markdown_plugin\")
set(messageviewer_bodypartformatter_text_markdown_PART_SRCS textmarkdownplugin.cpp)
set(messageviewer_bodypartformatter_text_markdown_PART_SRCS textmarkdownplugin.cpp ${mardown_engine_SRCS} markdownabstract.cpp)
add_library(messageviewer_bodypartformatter_text_markdown MODULE ${messageviewer_bodypartformatter_text_markdown_PART_SRCS})
......@@ -8,6 +16,7 @@ target_link_libraries(messageviewer_bodypartformatter_text_markdown
Qt5::Core
KF5::MessageViewer
Grantlee5::Templates
${discount_LIB}
)
install(TARGETS messageviewer_bodypartformatter_text_markdown DESTINATION ${KDE_INSTALL_PLUGINDIR}/messageviewer/bodypartformatter)
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "mardowndiscount.h"
#include <QDebug>
extern "C" {
#include <mkdio.h>
}
MardownDiscount::MardownDiscount()
{
}
MardownDiscount::~MardownDiscount()
{
}
QString MardownDiscount::toHtml() const
{
if (mText.isEmpty()) {
return {};
}
const QByteArray textArray = mText.toUtf8();
qDebug( ) << "textArray" << textArray;
MMIOT *markdownHandle = mkd_string(textArray.constData(), textArray.count(), 0);
mkd_flag_t flags = MKD_FENCEDCODE | MKD_GITHUBTAGS | MKD_AUTOLINK;
if (!mkd_compile(markdownHandle, flags)) {
qWarning() << "Failed to compile the Markdown document.";
mkd_cleanup(markdownHandle);
return {};
}
char *htmlDocument;
const int size = mkd_document(markdownHandle, &htmlDocument);
const QString html = QString::fromUtf8(htmlDocument, size);
mkd_cleanup(markdownHandle);
return html;
return {};
}
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef MARDOWNDISCOUNT_H
#define MARDOWNDISCOUNT_H
#include "markdownabstract.h"
class MardownDiscount : public MarkdownAbstract
{
public:
MardownDiscount();
~MardownDiscount();
Q_REQUIRED_RESULT QString toHtml() const override;
};
#endif // MARDOWNDISCOUNT_H
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "mardownqtextdocument.h"
#include <QTextDocument>
MardownQTextDocument::MardownQTextDocument()
{
}
MardownQTextDocument::~MardownQTextDocument()
{
}
QString MardownQTextDocument::toHtml() const
{
QTextDocument textDocument;
textDocument.setMarkdown(mText, QTextDocument::MarkdownNoHTML);
const QString result = textDocument.toHtml();
return result;
}
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef MARDOWNQTEXTDOCUMENT_H
#define MARDOWNQTEXTDOCUMENT_H
#include "markdownabstract.h"
class MardownQTextDocument : public MarkdownAbstract
{
public:
MardownQTextDocument();
~MardownQTextDocument();
Q_REQUIRED_RESULT QString toHtml() const override;
};
#endif // MARDOWNQTEXTDOCUMENT_H
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "markdownabstract.h"
MarkdownAbstract::MarkdownAbstract()
{
}
MarkdownAbstract::~MarkdownAbstract()
{
}
void MarkdownAbstract::setText(const QString &str)
{
mText = str;
}
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef MARKDOWNABSTRACT_H
#define MARKDOWNABSTRACT_H
#include <QString>
class MarkdownAbstract
{
public:
MarkdownAbstract();
virtual ~MarkdownAbstract();
void setText(const QString &str);
virtual QString toHtml() const = 0;
protected:
QString mText;
};
#endif // MARKDOWNABSTRACT_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