1 /*
2  markdown_outline_representation.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_MARKDOWN_OUTLINE_REPRESENTATION_H_
20 #define M8R_MARKDOWN_OUTLINE_REPRESENTATION_H_
21 
22 #include <string>
23 #include <cstdio>
24 
25 #include "../../debug.h"
26 
27 #include "markdown_document.h"
28 #include "markdown_ast_node.h"
29 #include "../../model/outline.h"
30 #include "../../mind/ontology/ontology.h"
31 #include "../outline_representation.h"
32 #include "../representation_interceptor.h"
33 
34 namespace m8r {
35 
36 /**
37  * @brief Markdown Outline representation.
38  */
39 /* Method:
40  *   Markdown (instance representing MD file)
41  *     FILENAME -lexer->  LINES
42  *     LINES    -lexer->  LEXEMS @ LEXER CTX
43  *     LEXEMS   -parser-> AST @ PARSER CTX
44  *     set(AST)
45  *
46  *   MarkdownOutlineRepresentation (transcoder)
47  *     from(AST) --> OUTLINE
48  *       AST.getString(LEXEM) --> name, description, line, ...
49  *
50  * Methods are virtual so that an inherited class may provide
51  * e.g. a Markdown flavor or HTML implementations.
52  */
53 class MarkdownOutlineRepresentation : public OutlineRepresentation
54 {
55 public:
56 
57     static constexpr int AVG_NOTE_SIZE = 500;
58     static constexpr int AVG_OUTLINE_SIZE = 3*AVG_NOTE_SIZE;
59 
60 private:
61 
62     // tags, outline types and note types are dynamic (not fixed)
63     Ontology& ontology;
64 
65     RepresentationInterceptor* descriptionInterceptor;
66 
67 public:
68 
69     /**
70      * @brief Markdown representation.
71      *
72      * Interceptor is the head of interceptor chain.
73      */
74     explicit MarkdownOutlineRepresentation(
75         Ontology& ontology,
76         RepresentationInterceptor* descriptionInterceptor);
77     MarkdownOutlineRepresentation(const MarkdownOutlineRepresentation&) = delete;
78     MarkdownOutlineRepresentation(const MarkdownOutlineRepresentation&&) = delete;
79     MarkdownOutlineRepresentation &operator=(const MarkdownOutlineRepresentation&) = delete;
80     MarkdownOutlineRepresentation &operator=(const MarkdownOutlineRepresentation&&) = delete;
81     virtual ~MarkdownOutlineRepresentation();
82 
83     virtual Outline* outline(const File& file) override;
84     virtual Outline* header(const std::string* md);
85     virtual Note* note(const File& file);
86     virtual Note* note(const std::string* md);
87 
88     virtual void description(const std::string* md, std::vector<std::string*>& description);
89 
90     virtual std::string* to(const Outline* outline);
91     virtual std::string* to(const Outline* outline, std::string* md);
92     virtual std::string* toPreamble(const Outline* outline, std::string* md);
93     virtual std::string* toHeader(const Outline* outline);
94     virtual std::string* to(const Note* note);
95     virtual std::string* to(const Note* note, std::string* md, bool includeMetadata=true, bool autolinking=false);
96     virtual std::string* toDescription(const Note* note, std::string* md, bool autolinking=false);
97 
98     static std::string to(const std::vector<const Tag*>* tags);
99     static std::string* toLink(const std::string& label, const std::string& link, std::string* md);
100 
101     /**
102      * @brief Generate table of contents
103      */
104     virtual std::string* toc(const Outline* outline, bool tags=true, bool links=true);
105 
getOntology()106     Ontology& getOntology() { return ontology; }
107 
108 private:
109     Outline* outline(std::vector<MarkdownAstNodeSection*>* ast);
110     Note* note(std::vector<MarkdownAstNodeSection*>* ast, const size_t astindex=0, Outline* outline=nullptr);
111     void toHeader(const Outline* outline, std::string* md);
112     std::string to(const std::vector<Link*>& links);
113 };
114 
115 } // m8r namespace
116 
117 #endif /* M8R_MARKDOWN_OUTLINE_REPRESENTATION_H_ */
118