1 /*
2  note_edit_highlight.h     MindForger thinking notebook
3 
4  Copyright (C) 2016-2020 Martin Dvorak <martin.dvorak@mindforger.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef M8R_NOTE_EDIT_HIGHLIGHT_H
20 #define M8R_NOTE_EDIT_HIGHLIGHT_H
21 
22 #include <QtWidgets>
23 
24 #include "look_n_feel.h"
25 
26 namespace m8r {
27 
28 // TODO ...Highlighted
29 class NoteEditHighlight : public QSyntaxHighlighter
30 {
31     Q_OBJECT
32 
33 private:
34     enum Type {
35         Bold,
36         Bolder,
37         Italic,
38         Italicer,
39         Strikethrough,
40         Link,
41         Autolink,
42         Codeblock,
43         Mathblock,
44         UnorderedList,
45         OrderedList,
46 
47         HtmlTag,
48         HtmlAttribute,
49         HtmlEntity,
50         HtmlComment
51     };
52 
53     enum State {
54         Normal=1<<0,
55         InComment=1<<1,
56         InCode=1<<2
57     };
58 
59     bool enabled;
60 
61     LookAndFeels& lookAndFeels;
62 
63     // Markdown formats
64     QTextCharFormat boldFormat;
65     QTextCharFormat bolderFormat;
66     QTextCharFormat italicFormat;
67     QTextCharFormat italicerFormat;
68     QTextCharFormat strikethroughFormat;
69     QTextCharFormat linkFormat;
70     QTextCharFormat listFormat;
71     QTextCharFormat codeBlockFormat;
72     QTextCharFormat mathBlockFormat;
73 
74     // HTML formats
75     QTextCharFormat htmlTagFormat;
76     QTextCharFormat htmlAttrNameFormat;
77     QTextCharFormat htmlAttValueFormat;
78     QTextCharFormat htmlEntityFormat;
79     QTextCharFormat htmlCommentFormat;
80 
81     // well defined order of regexps MATTERS - hashes/maps CANNOT be used
82     std::vector<std::pair<Type,QRegExp*>*> typeAndRegex;
83 
84 public:
85     explicit NoteEditHighlight(QTextDocument* parent);
86     NoteEditHighlight(const NoteEditHighlight&) = delete;
87     NoteEditHighlight(const NoteEditHighlight&&) = delete;
88     NoteEditHighlight &operator=(const NoteEditHighlight&) = delete;
89     NoteEditHighlight &operator=(const NoteEditHighlight&&) = delete;
90     ~NoteEditHighlight();
91 
setEnabled(bool enable)92     void setEnabled(bool enable) { enabled = enable; }
isEnabled()93     bool isEnabled() const { return enabled; }
94 
95 protected:
96     // implementation of the abstract method that performs highlighting
97     virtual void highlightBlock(const QString &text) override;
98 
99 private:
100     void addRegex(Type type, const QString& pattern, bool minimal=true);
101     void highlightPatterns(const QString& text);
102     bool highlightMultilineMdCode(const QString& text);
103     void highlightMultilineHtmlComments(const QString& text);
104 };
105 
106 }
107 
108 #endif // M8R_NOTE_EDIT_HIGHLIGHT_H
109