1 /* markdown.h - generic markdown parser */
2 
3 /*
4  * Copyright (c) 2009, Natacha Porté
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef UPSKIRT_MARKDOWN_H
20 #define UPSKIRT_MARKDOWN_H
21 
22 #include "buffer.h"
23 #include "autolink.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define SUNDOWN_VERSION "1.16.0"
30 #define SUNDOWN_VER_MAJOR 1
31 #define SUNDOWN_VER_MINOR 16
32 #define SUNDOWN_VER_REVISION 0
33 
34 /********************
35  * TYPE DEFINITIONS *
36  ********************/
37 
38 /* mkd_autolink - type of autolink */
39 enum mkd_autolink {
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 /* sd_callbacks - functions for rendering parsed data */
65 struct sd_callbacks {
66 	/* block level callbacks - NULL skips the block */
67 	void (*blockcode)(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque);
68 	void (*blockquote)(struct buf *ob, const struct buf *text, void *opaque);
69 	void (*blockhtml)(struct buf *ob,const  struct buf *text, void *opaque);
70 	void (*header)(struct buf *ob, const struct buf *text, int level, void *opaque);
71 	void (*hrule)(struct buf *ob, void *opaque);
72 	void (*list)(struct buf *ob, const struct buf *text, int flags, void *opaque);
73 	void (*listitem)(struct buf *ob, const struct buf *text, int flags, void *opaque);
74 	void (*paragraph)(struct buf *ob, const struct buf *text, void *opaque);
75 	void (*table)(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque);
76 	void (*table_row)(struct buf *ob, const struct buf *text, void *opaque);
77 	void (*table_cell)(struct buf *ob, const struct buf *text, int flags, void *opaque);
78 
79 
80 	/* span level callbacks - NULL or return 0 prints the span verbatim */
81 	int (*autolink)(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque);
82 	int (*codespan)(struct buf *ob, const struct buf *text, void *opaque);
83 	int (*double_emphasis)(struct buf *ob, const struct buf *text, void *opaque);
84 	int (*emphasis)(struct buf *ob, const struct buf *text, void *opaque);
85 	int (*image)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque);
86 	int (*linebreak)(struct buf *ob, void *opaque);
87 	int (*link)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque);
88 	int (*raw_html_tag)(struct buf *ob, const struct buf *tag, void *opaque);
89 	int (*triple_emphasis)(struct buf *ob, const struct buf *text, void *opaque);
90 	int (*strikethrough)(struct buf *ob, const struct buf *text, void *opaque);
91 	int (*superscript)(struct buf *ob, const struct buf *text, void *opaque);
92 
93 	/* low level callbacks - NULL copies input directly into the output */
94 	void (*entity)(struct buf *ob, const struct buf *entity, void *opaque);
95 	void (*normal_text)(struct buf *ob, const struct buf *text, void *opaque);
96 
97 	/* header and footer */
98 	void (*doc_header)(struct buf *ob, void *opaque);
99 	void (*doc_footer)(struct buf *ob, void *opaque);
100 };
101 
102 struct sd_markdown;
103 
104 /*********
105  * FLAGS *
106  *********/
107 
108 /* list/listitem flags */
109 #define MKD_LIST_ORDERED	1
110 #define MKD_LI_BLOCK		2  /* <li> containing block data */
111 
112 /**********************
113  * EXPORTED FUNCTIONS *
114  **********************/
115 
116 extern struct sd_markdown *
117 sd_markdown_new(
118 	unsigned int extensions,
119 	size_t max_nesting,
120 	const struct sd_callbacks *callbacks,
121 	void *opaque);
122 
123 extern void
124 sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, struct sd_markdown *md);
125 
126 extern void
127 sd_markdown_free(struct sd_markdown *md);
128 
129 extern void
130 sd_version(int *major, int *minor, int *revision);
131 
132 #ifdef __cplusplus
133 }
134 #endif
135 
136 #endif
137 
138 /* vim: set filetype=c: */
139