File indexing completed on 2024-10-27 04:39:16
0001 // SPDX-License-Identifier: GPL-2.0-or-later 0002 // SPDX-FileCopyrightText: 2023 Louis Schul <schul9louis@gmail.com> 0003 0004 #pragma once 0005 0006 #include <QMap> 0007 #include <QObject> 0008 #include <QRegularExpression> 0009 #include <QStandardPaths> 0010 0011 class HighlightHelper : public QObject 0012 { 0013 Q_OBJECT 0014 Q_PROPERTY(QStringList highlighters READ getHighlighters CONSTANT) 0015 public: 0016 explicit HighlightHelper(QObject *parent = nullptr); 0017 0018 QStringList getHighlighters() const; 0019 Q_INVOKABLE QStringList getHighlighterStyle(const QString &highlighter) const; 0020 static QString getHighlightedString(const QString &inputStr, const QString &lang); 0021 0022 private: 0023 inline static const QString m_chromaName = QStringLiteral("chroma"); 0024 inline static const QString m_pygmentizeName = QStringLiteral("pygmentize"); 0025 inline static const QString m_kSyntaxName = QStringLiteral("ksynthaxhighlighter6"); 0026 inline static const QString m_kateSyntaxName = QStringLiteral("kate-syntax-highlighter"); 0027 inline static QMap<QString, QStringList> m_highlightersCommands = { 0028 { 0029 m_chromaName, 0030 {QStringLiteral(" --list"), QStringLiteral(" --style=\"nord\" --lexer=%1 --html --html-inline-styles")}, 0031 }, 0032 { 0033 m_pygmentizeName, 0034 {QStringLiteral(" -L styles"), QStringLiteral(" -l %1 -f html -O style=nord -O noclasses=True")}, 0035 }, 0036 { 0037 m_kSyntaxName, 0038 {QStringLiteral(" --list-themes"), QStringLiteral(" --stdin -s %1 -f html -t 'Nord' -b")}, 0039 }, 0040 { 0041 m_kateSyntaxName, 0042 {QStringLiteral(" --list-themes"), QStringLiteral(" --stdin -s %1 -f html -t 'Nord' -b")}, 0043 }, 0044 }; // nord style by default, will be replace by the given style if it exists 0045 0046 inline static const QRegularExpression m_pygmentizeRegex = QRegularExpression(QStringLiteral("(\\* )(.+)(:)")); 0047 0048 QStringList getHighlighterStyleFromCmd(const QString &highlighter) const; 0049 void setAvailableHighlighters(); 0050 inline static QMap<QString, QStringList> m_availableHighlighters; 0051 };