File indexing completed on 2025-01-19 04:46:50

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <MessageViewer/MessagePartRenderPlugin>
0008 #include <MessageViewer/MessagePartRendererBase>
0009 #include <MessageViewer/MessagePartRendererManager>
0010 
0011 #include "markdownabstract.h"
0012 #include <MessageViewer/HtmlWriter>
0013 #include <MimeTreeParser/MessagePart>
0014 #ifdef USE_DISCOUNT_LIB
0015 #include "markdowndiscount.h"
0016 #else
0017 #include "markdownqtextdocument.h"
0018 #endif
0019 
0020 #include <KTextTemplate/Template>
0021 #include <QMimeDatabase>
0022 #include <QTextDocument>
0023 
0024 namespace
0025 {
0026 class Formatter : public MessageViewer::MessagePartRendererBase
0027 {
0028 public:
0029     bool render(const MimeTreeParser::MessagePartPtr &msgPart, MessageViewer::HtmlWriter *htmlWriter, MessageViewer::RenderContext *context) const override
0030     {
0031         Q_UNUSED(context)
0032         auto mp = msgPart.dynamicCast<MimeTreeParser::AttachmentMessagePart>();
0033         if (!mp || context->isHiddenHint(msgPart) || mp->text().isEmpty() || context->displayHint(msgPart) != MimeTreeParser::NoIcon) {
0034             return false;
0035         }
0036 
0037         QMimeDatabase db;
0038         auto mt = db.mimeTypeForName(QString::fromLatin1(msgPart->content()->contentType()->mimeType().toLower()));
0039         if (!mt.isValid()) {
0040             return false;
0041         }
0042         if (mt.name() != QLatin1StringView("text/markdown") && !mt.allAncestors().contains(QLatin1StringView("text/markdown"))) {
0043             return false;
0044         }
0045 
0046         auto c = MessageViewer::MessagePartRendererManager::self()->createContext();
0047         c.insert(QStringLiteral("block"), msgPart.data());
0048         c.insert(QStringLiteral("showOnlyOneMimePart"), context->showOnlyOneMimePart());
0049         c.insert(QStringLiteral("content"), QVariant::fromValue<MessageViewer::KTextTemplateCallback>([=](KTextTemplate::OutputStream *) {
0050                      QString result;
0051 #ifdef USE_DISCOUNT_LIB
0052                      MarkdownDiscount engine;
0053                      engine.setText(msgPart->text());
0054                      result = engine.toHtml();
0055 #else
0056                      MarkdownQTextDocument engine;
0057                      engine.setText(msgPart->text());
0058                      result = engine.toHtml();
0059 #endif
0060                      (*htmlWriter->stream()) << result;
0061                  }));
0062         auto t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral("textmessagepart.html"));
0063         KTextTemplate::OutputStream s(htmlWriter->stream());
0064         t->render(&s, &c);
0065         return true;
0066     }
0067 };
0068 
0069 class Plugin : public QObject, public MessageViewer::MessagePartRenderPlugin
0070 {
0071     Q_OBJECT
0072     Q_INTERFACES(MessageViewer::MessagePartRenderPlugin)
0073     Q_PLUGIN_METADATA(IID "com.kde.messageviewer.bodypartformatter" FILE "textmarkdownplugin.json")
0074 public:
0075     MessageViewer::MessagePartRendererBase *renderer(int index) override
0076     {
0077         if (index == 0) {
0078             return new Formatter();
0079         }
0080         return nullptr;
0081     }
0082 };
0083 }
0084 
0085 #include "textmarkdownplugin.moc"