summaryrefslogtreecommitdiffstats
path: root/src/markdown.h
blob: 6ac59cbe213d0e3044de5d94f8099d6294560c71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/******************************************************************************
 *
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby
 * granted. No representations are made about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */

#ifndef MARKDOWN_H
#define MARKDOWN_H

#include <functional>

#include <qcstring.h>
#include "parserintf.h"
#include "growbuf.h"

class Entry;

/** processes string \a s and converts markdown into doxygen/html commands. */
//QCString processMarkdown(const QCString &fileName,const int lineNr,Entry *e,const QCString &s);
QCString markdownFileNameToId(const QCString &fileName);

/// Helper class to process markdown formatted text
class Markdown
{
  public:
    Markdown(const char *fileName,int lineNr,int indentLevel=0);
    QCString process(const QCString &input);
    QCString extractPageTitle(QCString &docs,QCString &id);
    void setIndentLevel(int level) { m_indentLevel = level; }

  private:
    QCString detab(const QCString &s,int &refIndent);
    QCString processQuotations(const QCString &s,int refIndent);
    QCString processBlocks(const QCString &s,int indent);
    QCString isBlockCommand(const char *data,int offset,int size);
    void findEndOfLine(const char *data,int size,int &pi,int&i,int &end);
    int processHtmlTagWrite(const char *data,int offset,int size,bool doWrite);
    int processHtmlTag(const char *data,int offset,int size);
    int processEmphasis(const char *data,int offset,int size);
    int processEmphasis1(const char *data, int size, char c);
    int processEmphasis2(const char *data, int size, char c);
    int processEmphasis3(const char *data, int size, char c);
    int processNmdash(const char *data,int off,int size);
    int processQuoted(const char *data,int,int size);
    int processCodeSpan(const char *data, int /*offset*/, int size);
    void addStrEscapeUtf8Nbsp(const char *s,int len);
    int processSpecialCommand(const char *data, int offset, int size);
    int processLink(const char *data,int,int size);
    int findEmphasisChar(const char *data, int size, char c, int c_size);
    void processInline(const char *data,int size);
    void writeMarkdownImage(const char *fmt, bool explicitTitle,
                            const QCString &title, const QCString &content,
                            const QCString &link, const FileDef *fd);
    int isHeaderline(const char *data, int size, bool allowAdjustLevel);
    int isAtxHeader(const char *data,int size,
                       QCString &header,QCString &id,bool allowAdjustLevel);
    void writeOneLineHeaderOrRuler(const char *data,int size);
    void writeFencedCodeBlock(const char *data,const char *lng,
                int blockStart,int blockEnd);
    int writeBlockQuote(const char *data,int size);
    int writeCodeBlock(const char *data,int size,int refIndent);
    int writeTableBlock(const char *data,int size);

  private:
    struct LinkRef
    {
      LinkRef(const char *l,const char *t) : link(l), title(t) {}
      QCString link;
      QCString title;
    };
    using Action_t = std::function<int(const char *,int,int)>;

    std::unordered_map<std::string,LinkRef> m_linkRefs;
    QCString       m_fileName;
    int            m_lineNr = 0;
    int            m_indentLevel=0;  // 0 is outside markdown, -1=page level
    GrowBuf        m_out;
    Markdown::Action_t m_actions[256];
};


class MarkdownOutlineParser : public OutlineParserInterface
{
  public:
    MarkdownOutlineParser();
    virtual ~MarkdownOutlineParser();
    void startTranslationUnit(const char *) {}
    void finishTranslationUnit() {}
    void parseInput(const char *fileName,
                    const char *fileBuf,
                    const std::shared_ptr<Entry> &root,
                    bool sameTranslationUnit,
                    QStrList &filesInSameTranslationUnit);
    bool needsPreprocessing(const QCString &) const { return FALSE; }
    void parsePrototype(const char *text);
  private:
    struct Private;
    std::unique_ptr<Private> p;
};


#endif