libquentier  0.5.0
The library for rich desktop clients of Evernote service
ENMLConverter.h
1 /*
2  * Copyright 2016-2020 Dmitry Ivanov
3  *
4  * This file is part of libquentier
5  *
6  * libquentier is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, version 3 of the License.
9  *
10  * libquentier is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libquentier. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef LIB_QUENTIER_ENML_ENML_CONVERTER_H
20 #define LIB_QUENTIER_ENML_ENML_CONVERTER_H
21 
22 #include <quentier/utility/Printable.h>
23 #include <quentier/utility/Linkage.h>
24 #include <quentier/utility/Macros.h>
25 #include <quentier/types/ErrorString.h>
26 #include <quentier/types/Note.h>
27 
28 #include <QSet>
29 #include <QString>
30 #include <QHash>
31 #include <QTextDocument>
32 
33 namespace quentier {
34 
35 QT_FORWARD_DECLARE_CLASS(Resource)
36 QT_FORWARD_DECLARE_CLASS(DecryptedTextManager)
37 QT_FORWARD_DECLARE_CLASS(ENMLConverterPrivate)
38 
39 
44 class QUENTIER_EXPORT ENMLConverter
45 {
46 public:
47  ENMLConverter();
48  virtual ~ENMLConverter();
49 
60  class QUENTIER_EXPORT SkipHtmlElementRule: public Printable
61  {
62  public:
63  enum ComparisonRule {
64  Equals = 0,
65  StartsWith,
66  EndsWith,
67  Contains
68  };
69 
70  virtual QTextStream & print(QTextStream & strm) const override;
71 
72  QString m_elementNameToSkip;
73  ComparisonRule m_elementNameComparisonRule = ComparisonRule::Equals;
74  Qt::CaseSensitivity m_elementNameCaseSensitivity = Qt::CaseSensitive;
75 
76  QString m_attributeNameToSkip;
77  ComparisonRule m_attributeNameComparisonRule = ComparisonRule::Equals;
78  Qt::CaseSensitivity m_attributeNameCaseSensitivity = Qt::CaseSensitive;
79 
80  QString m_attributeValueToSkip;
81  ComparisonRule m_attributeValueComparisonRule = ComparisonRule::Equals;
82  Qt::CaseSensitivity m_attributeValueCaseSensitivity = Qt::CaseSensitive;
83 
84  bool m_includeElementContents = false;
85  };
86 
87  bool htmlToNoteContent(
88  const QString & html, QString & noteContent,
89  DecryptedTextManager & decryptedTextManager,
90  ErrorString & errorDescription,
91  const QVector<SkipHtmlElementRule> & skipRules = {}) const;
92 
106  bool cleanupExternalHtml(
107  const QString & inputHtml, QString & cleanedUpHtml,
108  ErrorString & errorDescription) const;
109 
123  bool htmlToQTextDocument(
124  const QString & html, QTextDocument & doc,
125  ErrorString & errorDescription,
126  const QVector<SkipHtmlElementRule> & skipRules = {}) const;
127 
129  {
130  quint64 m_numEnToDoNodes = 0;
131  quint64 m_numHyperlinkNodes = 0;
132  quint64 m_numEnCryptNodes = 0;
133  quint64 m_numEnDecryptedNodes = 0;
134  };
135 
136  bool noteContentToHtml(
137  const QString & noteContent, QString & html,
138  ErrorString & errorDescription,
139  DecryptedTextManager & decryptedTextManager,
140  NoteContentToHtmlExtraData & extraData) const;
141 
142  bool validateEnml(
143  const QString & enml, ErrorString & errorDescription) const;
144 
145  bool validateAndFixupEnml(
146  QString & enml, ErrorString & errorDescription) const;
147 
148  static bool noteContentToPlainText(
149  const QString & noteContent, QString & plainText,
150  ErrorString & errorMessage);
151 
152  static bool noteContentToListOfWords(
153  const QString & noteContent, QStringList & listOfWords,
154  ErrorString & errorMessage, QString * plainText = nullptr);
155 
156  static QStringList plainTextToListOfWords(const QString & plainText);
157 
158  static QString toDoCheckboxHtml(const bool checked, const quint64 idNumber);
159 
160  static QString encryptedTextHtml(
161  const QString & encryptedText, const QString & hint,
162  const QString & cipher, const size_t keyLength,
163  const quint64 enCryptIndex);
164 
165  static QString decryptedTextHtml(
166  const QString & decryptedText, const QString & encryptedText,
167  const QString & hint, const QString & cipher,
168  const size_t keyLength, const quint64 enDecryptedIndex);
169 
170  static QString resourceHtml(
171  const Resource & resource, ErrorString & errorDescription);
172 
173  static void escapeString(QString & string, const bool simplify = true);
174 
179  enum class EnexExportTags
180  {
181  Yes = 0,
182  No
183  };
184 
211  bool exportNotesToEnex(
212  const QVector<Note> & notes,
213  const QHash<QString, QString> & tagNamesByTagLocalUids,
214  const EnexExportTags exportTagsOption,
215  QString & enex, ErrorString & errorDescription,
216  const QString & version = {}) const;
217 
237  bool importEnex(
238  const QString & enex, QVector<Note> & notes,
239  QHash<QString, QStringList> & tagNamesByNoteLocalUid,
240  ErrorString & errorDescription) const;
241 
242 private:
243  Q_DISABLE_COPY(ENMLConverter)
244 
245 private:
246  ENMLConverterPrivate * const d_ptr;
247  Q_DECLARE_PRIVATE(ENMLConverter)
248 };
249 
250 } // namespace quentier
251 
252 #endif // LIB_QUENTIER_ENML_ENML_CONVERTER_H
The ErrorString class encapsulates two (or more) strings which are meant to contain translatable (bas...
Definition: ErrorString.h:43
Definition: Resource.h:29
EnexExportTags
The EnexExportTags enum allows to specify whether export of note(s) to ENEX should include the names ...
Definition: ENMLConverter.h:179
Definition: DecryptedTextManager.h:26
The Printable class is the interface for Quentier&#39;s internal classes which should be able to write th...
Definition: Printable.h:38
Definition: DecryptedTextManager.h:30
The SkipHtmlElementRule class describes the set of rules for HTML -> ENML conversion about the HTML e...
Definition: ENMLConverter.h:60
The ENMLConverter class encapsulates a set of methods and helper data structures for performing the c...
Definition: ENMLConverter.h:44