ScummVM API documentation
markdown.h
1 /*
2  * Copyright (c) 2009, Natacha Porté
3  * Copyright (c) 2011, Vicent Marti
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef COMMON_FORMATS_MARKDOWN_H
19 #define COMMON_FORMATS_MARKDOWN_H
20 
21 #include "common/scummsys.h"
22 
23 namespace Common {
24 
25 class String;
26 
27 #define SUNDOWN_VERSION "1.16.0"
28 #define SUNDOWN_VER_MAJOR 1
29 #define SUNDOWN_VER_MINOR 16
30 #define SUNDOWN_VER_REVISION 0
31 
32 /********************
33  * TYPE DEFINITIONS *
34  ********************/
35 
36 struct SDDataBuffer;
37 
38 /* mkd_autolink - type of autolink */
39 enum MKDAutolink {
40  MKDA_NOT_AUTOLINK, /* used internally when it is not an autolink*/
41  MKDA_NORMAL, /* normal http/http/ftp/mailto/etc link */
42  MKDA_EMAIL, /* e-mail link without explit mailto: */
43 };
44 
45 enum mkd_tableflags {
46  MKD_TABLE_ALIGN_L = 1,
47  MKD_TABLE_ALIGN_R = 2,
48  MKD_TABLE_ALIGN_CENTER = 3,
49  MKD_TABLE_ALIGNMASK = 3,
50  MKD_TABLE_HEADER = 4
51 };
52 
53 enum mkd_extensions {
54  MKDEXT_NO_INTRA_EMPHASIS = (1 << 0),
55  MKDEXT_TABLES = (1 << 1),
56  MKDEXT_FENCED_CODE = (1 << 2),
57  MKDEXT_AUTOLINK = (1 << 3),
58  MKDEXT_STRIKETHROUGH = (1 << 4),
59  MKDEXT_SPACE_HEADERS = (1 << 6),
60  MKDEXT_SUPERSCRIPT = (1 << 7),
61  MKDEXT_LAX_SPACING = (1 << 8),
62 };
63 
64 struct SDStack {
65  void **item;
66  size_t size;
67  size_t asize;
68 };
69 
70 /* sd_callbacks - functions for rendering parsed data */
71 struct SDCallbacks {
72  /* block level callbacks - NULL skips the block */
73  void (*blockcode)(SDDataBuffer *ob, const SDDataBuffer *text, const SDDataBuffer *lang, void *opaque);
74  void (*blockquote)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
75  void (*blockhtml)(SDDataBuffer *ob,const SDDataBuffer *text, void *opaque);
76  void (*header)(SDDataBuffer *ob, const SDDataBuffer *text, int level, void *opaque);
77  void (*hrule)(SDDataBuffer *ob, void *opaque);
78  void (*list_start)(SDDataBuffer *ob, const SDDataBuffer *text, int flags, void *opaque);
79  void (*list)(SDDataBuffer *ob, const SDDataBuffer *text, int flags, void *opaque);
80  void (*listitem)(SDDataBuffer *ob, const SDDataBuffer *text, int flags, void *opaque);
81  void (*paragraph)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
82  void (*table)(SDDataBuffer *ob, const SDDataBuffer *header, const SDDataBuffer *body, void *opaque);
83  void (*table_row)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
84  void (*table_cell)(SDDataBuffer *ob, const SDDataBuffer *text, int flags, void *opaque);
85 
86 
87  /* span level callbacks - NULL or return 0 prints the span verbatim */
88  int (*autolink)(SDDataBuffer *ob, const SDDataBuffer *link, MKDAutolink type, void *opaque);
89  int (*codespan)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
90  int (*double_emphasis)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
91  int (*emphasis)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
92  int (*image)(SDDataBuffer *ob, const SDDataBuffer *link, const SDDataBuffer *title, const SDDataBuffer *alt, const SDDataBuffer *ext, void *opaque);
93  int (*linebreak)(SDDataBuffer *ob, void *opaque);
94  int (*link)(SDDataBuffer *ob, const SDDataBuffer *link, const SDDataBuffer *title, const SDDataBuffer *content, void *opaque);
95  int (*raw_html_tag)(SDDataBuffer *ob, const SDDataBuffer *tag, void *opaque);
96  int (*triple_emphasis)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
97  int (*strikethrough)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
98  int (*superscript)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
99 
100  /* low level callbacks - NULL copies input directly into the output */
101  void (*entity)(SDDataBuffer *ob, const SDDataBuffer *entity, void *opaque);
102  void (*normal_text)(SDDataBuffer *ob, const SDDataBuffer *text, void *opaque);
103 
104  /* header and footer */
105  void (*doc_header)(SDDataBuffer *ob, void *opaque);
106  void (*doc_footer)(SDDataBuffer *ob, void *opaque);
107 };
108 
109 /*********
110  * FLAGS *
111  *********/
112 
113 /* list/listitem flags */
114 #define MKD_LIST_ORDERED 1
115 #define MKD_LI_BLOCK 2 /* <li> containing block data */
116 
117 #define REF_TABLE_SIZE 8
118 
119 struct LinkRef;
120 
121 class SDMarkdown {
122 public:
123  SDCallbacks _cb;
124  void *_opaque;
125 
126  LinkRef *_refs[REF_TABLE_SIZE];
127  byte _active_char[256];
128  SDStack _work_bufs[2];
129  uint _ext_flags;
130  size_t _max_nesting;
131  int _in_link_body;
132 
133  SDMarkdown(uint extensions, size_t max_nesting, const SDCallbacks *callbacks, void *opaque);
134  ~SDMarkdown();
135 
136  Common::String render(const byte *document, size_t doc_size);
137 
138  void version(int *major, int *minor, int *revision);
139 };
140 
141 /* SDDataBuffer: character array buffer */
142 struct SDDataBuffer {
143  byte *data; /* actual character data */
144  size_t size; /* size of the string */
145  size_t asize; /* allocated size (0 = volatile buffer) */
146  size_t unit; /* reallocation unit size (0 = read-only buffer) */
147 };
148 
149 /* sd_bufput: appends raw data to a buffer */
150 void sd_bufput(SDDataBuffer *, const void *, size_t);
151 
152 } // end of namespace Common
153 
154 #endif // COMMON_FORMATS_MARKDOWN_H
Definition: str.h:59
Definition: markdown.h:71
Definition: markdown.h:64
Definition: markdown.h:142
Definition: algorithm.h:29
Definition: markdown.h:121