| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Alf. |
| 5 |
* |
| 6 |
* Contact: Alf <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.0 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public |
| 19 |
* License along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
*/ |
| 23 |
/* @CPPFILE ApvlvEditor.h |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef _APVLV_MARKDOWN_H_ |
| 29 |
#define _APVLV_MARKDOWN_H_ |
| 30 |
|
| 31 |
#include <cmark.h> |
| 32 |
#include <list> |
| 33 |
#include <memory> |
| 34 |
#include <queue> |
| 35 |
#include <string> |
| 36 |
|
| 37 |
namespace apvlv |
| 38 |
{ |
| 39 |
struct MarkdownNode |
| 40 |
{ |
| 41 |
cmark_node_type node_type{ CMARK_NODE_TEXT }; |
| 42 |
cmark_list_type list_type{ CMARK_NO_LIST }; |
| 43 |
int heading_level{ 1 }; |
| 44 |
std::string literal; |
| 45 |
std::string title; |
| 46 |
std::string url; |
| 47 |
std::vector<std::unique_ptr<MarkdownNode>> children; |
| 48 |
|
| 49 |
explicit MarkdownNode (cmark_node_type _type) : node_type (_type) {}; |
| 50 |
~MarkdownNode () = default; |
| 51 |
MarkdownNode (const MarkdownNode &other); |
| 52 |
MarkdownNode (MarkdownNode &&other) noexcept; |
| 53 |
MarkdownNode &operator= (const MarkdownNode &other); |
| 54 |
MarkdownNode &operator= (MarkdownNode &&other) noexcept; |
| 55 |
|
| 56 |
int childrenCount (); |
| 57 |
MarkdownNode *childAt (int index); |
| 58 |
void appendChildPtr (std::unique_ptr<MarkdownNode> ptr); |
| 59 |
void appendChild (MarkdownNode *n); |
| 60 |
void removeChild (MarkdownNode *n); |
| 61 |
|
| 62 |
std::vector<std::string> getListTexts (); |
| 63 |
void setListTexts (cmark_list_type _type, |
| 64 |
const std::vector<std::string> &texts); |
| 65 |
void setNoListTexts (const std::vector<std::string> &texts); |
| 66 |
void setBulletListTexts (const std::vector<std::string> &texts); |
| 67 |
void setOrderedListTexts (const std::vector<std::string> &texts); |
| 68 |
|
| 69 |
using HeadAndList = std::pair<std::string, std::vector<std::string>>; |
| 70 |
std::pair<int, std::string> headText (); |
| 71 |
void appendHeadText (int _level, const std::string &_text); |
| 72 |
void appendHeadAndList (int _level, cmark_list_type _type, |
| 73 |
const HeadAndList &headAndList); |
| 74 |
void appendHeadAndNoList (int _level, const HeadAndList &headAndList); |
| 75 |
void appendHeadAndBulletList (int _level, const HeadAndList &headAndList); |
| 76 |
void appendHeadAndOrderedList (int _level, const HeadAndList &headAndList); |
| 77 |
|
| 78 |
static std::unique_ptr<MarkdownNode> fromCmarkNode (cmark_node *node); |
| 79 |
static MarkdownNode *create (MarkdownNode *parent, cmark_node_type _type, |
| 80 |
std::string_view literal = ""); |
| 81 |
cmark_node *toCmarkNode (); |
| 82 |
}; |
| 83 |
|
| 84 |
class Markdown |
| 85 |
{ |
| 86 |
public: |
| 87 |
Markdown (); |
| 88 |
~Markdown () = default; |
| 89 |
Markdown (const Markdown &other); |
| 90 |
Markdown (Markdown &&other) noexcept; |
| 91 |
Markdown &operator= (const Markdown &other); |
| 92 |
Markdown &operator= (Markdown &&other) noexcept; |
| 93 |
|
| 94 |
bool loadFromFile (const std::string &filename); |
| 95 |
bool loadFromStream (std::istream &is); |
| 96 |
bool saveToFile (const std::string &filename); |
| 97 |
bool saveToStream (std::ostream &os); |
| 98 |
[[nodiscard]] MarkdownNode *root () const; |
| 99 |
static std::unique_ptr<Markdown> |
| 100 |
create () |
| 101 |
{ |
| 102 |
return std::make_unique<Markdown> (); |
| 103 |
} |
| 104 |
|
| 105 |
private: |
| 106 |
std::unique_ptr<MarkdownNode> mRoot; |
| 107 |
}; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
#endif |