Debian Codegraph @

src/ApvlvMarkdown.cc

raw
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 ApvlvMarkdown.cc
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #include <algorithm>
29 #include <array>
30 #include <fstream>
31
32 #include "ApvlvMarkdown.h"
33
34 #include <qassert.h>
35
36 namespace apvlv
37 {
38 using namespace std;
39
40 MarkdownNode::MarkdownNode (const MarkdownNode &other)
41 {
42 node_type = other.node_type;
43 list_type = other.list_type;
44 heading_level = other.heading_level;
45 literal = other.literal;
46 title = other.title;
47 url = other.url;
48 for (auto &child : other.children)
49 {
50 auto nchild = new MarkdownNode (*child);
51 children.push_back (unique_ptr<MarkdownNode> (nchild));
52 }
53 }
54
55 MarkdownNode::MarkdownNode (MarkdownNode &&other) noexcept
56 {
57 node_type = other.node_type;
58 list_type = other.list_type;
59 heading_level = other.heading_level;
60 literal.swap (other.literal);
61 title.swap (other.title);
62 url.swap (other.url);
63 children.swap (other.children);
64 }
65
66 MarkdownNode &
67 MarkdownNode::operator= (const MarkdownNode &other)
68 {
69 node_type = other.node_type;
70 list_type = other.list_type;
71 heading_level = other.heading_level;
72 literal = other.literal;
73 title = other.title;
74 url = other.url;
75 for (const auto &child : other.children)
76 {
77 auto nchild = new MarkdownNode (*child);
78 children.push_back (unique_ptr<MarkdownNode> (nchild));
79 }
80 return *this;
81 }
82
83 MarkdownNode &
84 MarkdownNode::operator= (MarkdownNode &&other) noexcept
85 {
86 node_type = other.node_type;
87 list_type = other.list_type;
88 heading_level = other.heading_level;
89 literal.swap (other.literal);
90 title.swap (other.title);
91 url.swap (other.url);
92 children.swap (other.children);
93 return *this;
94 }
95
96 int
97 MarkdownNode::childrenCount ()
98 {
99 return static_cast<int> (children.size ());
100 }
101
102 MarkdownNode *
103 MarkdownNode::childAt (int index)
104 {
105 return children[index].get ();
106 }
107
108 void
109 MarkdownNode::appendChildPtr (std::unique_ptr<MarkdownNode> ptr)
110 {
111 children.push_back (std::move (ptr));
112 }
113
114 void
115 MarkdownNode::appendChild (MarkdownNode *n)
116 {
117 children.push_back (unique_ptr<MarkdownNode> (n));
118 }
119
120 void
121 MarkdownNode::removeChild (MarkdownNode *n)
122 {
123 auto iter = std::find_if (children.begin (), children.end (),
124 [n] (const auto &i) { return i.get () == n; });
125 if (iter != children.end ())
126 {
127 children.erase (iter);
128 }
129 }
130
131 std::vector<std::string>
132 MarkdownNode::getListTexts ()
133 {
134 std::vector<std::string> texts;
135 for (const auto &i : children)
136 {
137 auto p = i->children.front ().get ();
138 auto t = p->children.front ().get ();
139 texts.push_back (t->literal);
140 }
141 return texts;
142 }
143
144 void
145 MarkdownNode::setListTexts (cmark_list_type _type,
146 const std::vector<std::string> &texts)
147 {
148 children.clear ();
149 node_type = CMARK_NODE_LIST;
150 list_type = _type;
151 for (const auto &text : texts)
152 {
153 auto i = create (this, CMARK_NODE_ITEM);
154 auto p = create (i, CMARK_NODE_PARAGRAPH);
155 auto t = create (p, CMARK_NODE_TEXT);
156 t->literal = text;
157 }
158 }
159
160 void
161 MarkdownNode::setNoListTexts (const std::vector<std::string> &texts)
162 {
163 setListTexts (CMARK_NO_LIST, texts);
164 }
165
166 void
167 MarkdownNode::setBulletListTexts (const std::vector<std::string> &texts)
168 {
169 setListTexts (CMARK_BULLET_LIST, texts);
170 }
171
172 void
173 MarkdownNode::setOrderedListTexts (const std::vector<std::string> &texts)
174 {
175 setListTexts (CMARK_ORDERED_LIST, texts);
176 }
177
178 std::pair<int, std::string>
179 MarkdownNode::headText ()
180 {
181 if (node_type != CMARK_NODE_HEADING || childrenCount () < 1)
182 {
183 throw std::invalid_argument ("MarkdownNode::headText");
184 }
185
186 auto n = childAt (0);
187 std::pair<int, std::string> res;
188 res.first = n->heading_level;
189 res.second = n->literal;
190 return res;
191 }
192
193 void
194 MarkdownNode::appendHeadText (int _level, const std::string &_text)
195 {
196 auto h = create (this, CMARK_NODE_HEADING);
197 h->heading_level = _level;
198 auto ht = create (h, CMARK_NODE_TEXT);
199 ht->literal = _text;
200 }
201
202 void
203 MarkdownNode::appendHeadAndList (int _level, cmark_list_type _type,
204 const HeadAndList &headAndList)
205 {
206 appendHeadText (_level, headAndList.first);
207 auto list = create (this, CMARK_NODE_LIST);
208 list->setListTexts (_type, headAndList.second);
209 }
210
211 void
212 MarkdownNode::appendHeadAndNoList (int _level, const HeadAndList &headAndList)
213 {
214 appendHeadAndList (_level, CMARK_NO_LIST, headAndList);
215 }
216
217 void
218 MarkdownNode::appendHeadAndBulletList (int _level,
219 const HeadAndList &headAndList)
220 {
221 appendHeadAndList (_level, CMARK_BULLET_LIST, headAndList);
222 }
223
224 void
225 MarkdownNode::appendHeadAndOrderedList (int _level,
226 const HeadAndList &headAndList)
227 {
228 appendHeadAndList (_level, CMARK_ORDERED_LIST, headAndList);
229 }
230
231 std::unique_ptr<MarkdownNode>
232 MarkdownNode::fromCmarkNode (cmark_node *node)
233 {
234 auto mn = make_unique<MarkdownNode> (cmark_node_get_type (node));
235 switch (mn->node_type)
236 {
237 case CMARK_NODE_HEADING:
238 mn->heading_level = cmark_node_get_heading_level (node);
239 break;
240 case CMARK_NODE_LIST:
241 mn->list_type = cmark_node_get_list_type (node);
242 break;
243 case CMARK_NODE_IMAGE:
244 case CMARK_NODE_LINK:
245 mn->title = cmark_node_get_title (node);
246 mn->url = cmark_node_get_url (node);
247 break;
248 default:
249 auto l = cmark_node_get_literal (node);
250 if (l)
251 mn->literal = l;
252 }
253 for (auto n = cmark_node_first_child (node); n != nullptr;
254 n = cmark_node_next (n))
255 {
256 auto cmn = MarkdownNode::fromCmarkNode (n);
257 mn->children.emplace_back (std::move (cmn));
258 }
259 return mn;
260 }
261
262 MarkdownNode *
263 MarkdownNode::create (MarkdownNode *parent, cmark_node_type _type,
264 string_view literal)
265 {
266 Q_ASSERT (parent != nullptr);
267 auto mn = make_unique<MarkdownNode> (_type);
268 mn->literal = literal;
269 auto node = mn.get ();
270 parent->appendChildPtr (std::move (mn));
271 return node;
272 }
273
274 cmark_node *
275 MarkdownNode::toCmarkNode ()
276 {
277 auto doc = cmark_node_new (node_type);
278 switch (node_type)
279 {
280 case CMARK_NODE_HEADING:
281 cmark_node_set_heading_level (doc, heading_level);
282 break;
283
284 case CMARK_NODE_LIST:
285 cmark_node_set_list_type (doc, list_type);
286 break;
287
288 case CMARK_NODE_IMAGE:
289 case CMARK_NODE_LINK:
290 cmark_node_set_title (doc, title.c_str ());
291 cmark_node_set_url (doc, url.c_str ());
292 break;
293
294 default:
295 break;
296 }
297 cmark_node_set_literal (doc, literal.c_str ());
298
299 for (auto const &child : children)
300 {
301 auto n = child.get ();
302 auto d = n->toCmarkNode ();
303 cmark_node_append_child (doc, d);
304 }
305
306 return doc;
307 }
308
309 Markdown::Markdown ()
310 : mRoot{ make_unique<MarkdownNode> (CMARK_NODE_DOCUMENT) }
311 {
312 mRoot->node_type = CMARK_NODE_DOCUMENT;
313 }
314
315 Markdown::Markdown (const Markdown &other)
316 {
317 if (this != &other)
318 {
319 mRoot = make_unique<MarkdownNode> (MarkdownNode (*other.mRoot));
320 }
321 }
322
323 Markdown::Markdown (Markdown &&other) noexcept
324 {
325 if (this != &other)
326 {
327 mRoot.swap (other.mRoot);
328 }
329 }
330
331 Markdown &
332 Markdown::operator= (const Markdown &other)
333 {
334 if (this != &other)
335 {
336 mRoot = make_unique<MarkdownNode> (MarkdownNode (*other.mRoot));
337 }
338
339 return *this;
340 }
341
342 Markdown &
343 Markdown::operator= (Markdown &&other) noexcept
344 {
345 if (this != &other)
346 {
347 mRoot.swap (other.mRoot);
348 }
349 return *this;
350 }
351
352 bool
353 Markdown::loadFromFile (const std::string &filename)
354 {
355 ifstream ifs (filename, ifstream::binary);
356 if (!ifs.is_open ())
357 {
358 return false;
359 }
360
361 return loadFromStream (ifs);
362 }
363
364 bool
365 Markdown::loadFromStream (std::istream &is)
366 {
367 auto parser = cmark_parser_new (CMARK_OPT_DEFAULT);
368 array<char, 16384> buffer{};
369 while (is.good ())
370 {
371 is.read (buffer.data (), buffer.size ());
372 auto got = is.gcount ();
373 if (got == 0)
374 {
375 break;
376 }
377 cmark_parser_feed (parser, buffer.data (), got);
378 }
379
380 auto doc = cmark_parser_finish (parser);
381 cmark_parser_free (parser);
382
383 if (doc)
384 {
385 mRoot = MarkdownNode::fromCmarkNode (doc);
386 cmark_node_free (doc);
387 return true;
388 }
389 else
390 {
391 return false;
392 }
393 }
394
395 bool
396 Markdown::saveToFile (const std::string &filename)
397 {
398 ofstream ofs (filename, ofstream::binary);
399 if (!ofs.is_open ())
400 {
401 return false;
402 }
403
404 return saveToStream (ofs);
405 }
406
407 bool
408 Markdown::saveToStream (std::ostream &os)
409 {
410 auto doc = mRoot->toCmarkNode ();
411 auto text = cmark_render_commonmark (doc, CMARK_OPT_DEFAULT, 78);
412 os << text;
413 free (text);
414 cmark_node_free (doc);
415 return false;
416 }
417
418 MarkdownNode *
419 Markdown::root () const
420 {
421 return mRoot.get ();
422 }
423
424 }