Skip to content
Snippets Groups Projects
Select Git revision
  • 6fac40340bc4ef46a52fd363027936464fc7fd5e
  • master default protected
  • stable/20250917.0
  • nightly/20250917.0
  • beta/202509161514
  • beta/202509122057
  • stable/20250912.0
  • nightly/20250912.0
  • nightly/20250911.0
  • beta/202509111213
  • stable/20250902.0
  • nightly/20250902.0
  • beta/202508201613
  • stable/20250815.1
  • stable/20250815.0
  • nightly/20250815.0
  • nightly/20250806.0
  • nightly/20250805.0
  • beta/202508051403
  • beta/202508051107
  • nightly/20250722.0
  • beta/202507211539
22 results

messageparser.h

Blame
  • François-Simon Fauteux-Chapleau's avatar
    François-Simon Fauteux-Chapleau authored
    The attempted fix in commit 65d3befa was based on the incorrect
    assumption that md4c systematically fails to detect links that contain
    at least one +, - or _. This commit removes the postprocessing step
    added in 65d3befa and instead patches md4c to directly fix its link
    recognition algorithm.
    
    GitLab: #1903
    GitLab: #1964
    Change-Id: Ic1d6302e3250390af66611a16715a6397578abb5
    6fac4034
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    messageparser.h 2.83 KiB
    /*
     * Copyright (C) 2024-2025 Savoir-faire Linux Inc.
     *
     * 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 3 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.  If not, see <https://www.gnu.org/licenses/>.
     */
    
    #pragma once
    
    #include <QObject>
    #include <QColor>
    #include <QThreadPool>
    
    class PreviewEngine;
    class HtmlParser;
    
    // This class is used to parse messages and encapsulate the logic that
    // prepares a message for display in the UI. The basic steps are:
    // 1. preprocess the markdown message (e.g. handle line breaks)
    // 2. transform markdown syntax into HTML
    // 3. generate previews for the first link in the message (if any)
    // 4. add the appropriate CSS classes to the HTML (e.g. for links, code blocks, etc.)
    //
    // Step 3. is done asynchronously, so the message is displayed as soon as possible
    // and the preview is added later.
    class MessageParser final : public QObject
    {
        Q_OBJECT
        Q_DISABLE_COPY(MessageParser)
    public:
        // Create a new MessageParser instance. We take an instance of PreviewEngine.
        explicit MessageParser(PreviewEngine* previewEngine, QObject* parent = nullptr);
        ~MessageParser() = default;
    
        // Parse the message. This will emit the messageParsed signal when the
        // message is ready to be displayed.
        void parseMessage(const QString& messageId,
                          const QString& msg,
                          bool previewLinks,
                          const QColor& linkColor,
                          const QColor& backgroundColor);
    
        // Emitted when the message is ready to be displayed.
        Q_SIGNAL void messageParsed(const QString& msgId, const QString& msg);
    
        // Emitted when the message preview is ready to be displayed.
        Q_SIGNAL void linkInfoReady(const QString& msgId, const QVariantMap& info);
    
    private:
        // Preprocess the markdown message (e.g. handle line breaks).
        void preprocessMarkdown(QString& markdown);
    
        // Transform markdown syntax into HTML.
        QString markdownToHtml(const char* markdown);
    
        // Generate a preview for the given link, then emit the messageParsed signal.
        void generatePreview(const QString& msgId, const QString& link);
    
        // The PreviewEngine instance used to generate previews.
        PreviewEngine* previewEngine_;
    
        // An instance of HtmlParser used to parse HTML.
        HtmlParser* htmlParser_;
    
        // Used to queue parse operations.
        QThreadPool* threadPool_;
    };