libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-md.h
Go to the documentation of this file.
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2026 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 * Streaming markdown to structured events renderer
25 *
26 * Hostile-input-hardened, alloc-free, streaming renderer for the well-behaved
27 * markdown subset used by readme and blog content: headings, paragraphs,
28 * fenced and indented code blocks, blockquotes, lists, pipe tables, rules,
29 * inline code / emphasis / strong, links, images and autolinks.
30 *
31 * Markdown input is never passed through as markup: the driver only issues
32 * structural events from a closed vocabulary plus data events for text, urls,
33 * image alt text and fence info strings. All markup synthesis and all
34 * security decisions about data bytes belong to the sink; the stock html sink
35 * (lws_md_html_t) entity-escapes everything and applies a url scheme policy,
36 * producing strict-CSP-compatible output with no inline styles or scripts.
37 *
38 * Structure that cannot be decided without a line of lookahead (is this the
39 * table header row?) is held in a side buffer and the decision deferred until
40 * the start of the next line; giving up on the decision past the hold cap
41 * reclassifies the held line as text, it never drops content.
42 */
43
44#if !defined(LMD_LINE_MAX)
45#define LMD_LINE_MAX 8192
46#endif
47#if !defined(LMD_HOLD_MAX)
48#define LMD_HOLD_MAX LMD_LINE_MAX
49#endif
50#if !defined(LMD_INFO_MAX)
51#define LMD_INFO_MAX 32
52#endif
53#if !defined(LMD_NEST_MAX)
54#define LMD_NEST_MAX 24
55#endif
56#if !defined(LMD_TEXT_PIECE)
57#define LMD_TEXT_PIECE 256
58#endif
59
84
94
117 lws_md_el_t el, unsigned int aux,
118 const uint8_t *data, size_t len);
119
127typedef struct lws_md_ctx {
129 void *user;
130
131 /* private below */
132
133 uint32_t evseq; /* issue index within the txn in flight */
134 uint32_t evdone; /* watermark of accepted events */
135
136 uint8_t txn; /* 0 idle, 1 line, 2 hold flush, 3 finish */
137 uint8_t in_link;/* inside link text: no nested links */
138
139 uint8_t para; /* paragraph open */
140 uint8_t list; /* 0 none, else LMD_EL_UL / _OL */
141 uint8_t li; /* inside a list item */
142 uint8_t table; /* inside a table */
143 uint8_t hold; /* line held for table decision */
144 uint8_t bq; /* open blockquote depth */
145 uint8_t code_ind;/* inside an indented code block */
146
147 uint8_t fence; /* inside a fenced code block */
148 uint8_t fchr; /* the fence character */
149 uint8_t flen; /* the opening fence length */
150 uint8_t fmatch; /* fence chars matched at line start */
151 uint8_t fbol; /* at start of a fence body line */
152 uint8_t fclose; /* swallowing a closing fence line */
153
154 uint8_t over; /* past the cap, streaming a line */
155
156 uint8_t fin_n; /* finish: closers remaining */
158
159 uint32_t llen; /* staged line length */
160 uint32_t hlen; /* held line length */
161
164
168
181
201lws_md_parse(lws_md_ctx_t *ctx, const uint8_t **buf, size_t *len);
202
222
223#if !defined(LMD_URL_MAX)
224#define LMD_URL_MAX 768
225#endif
226#if !defined(LMD_HTML_BUF)
227#define LMD_HTML_BUF 4096
228#endif
229
230typedef lws_stateful_ret_t (*lws_md_write_cb)(void *user, const uint8_t *buf,
231 size_t len);
232
247typedef size_t (*lws_md_resolve_cb)(void *user, int is_image,
248 const char *url, size_t len,
249 char *dest, size_t dest_len);
250
251#if defined(LWS_WITH_HL)
252#include "lws-hl.h"
253#endif
254
270typedef struct lws_md_html {
271 /* private */
273 void *user;
276
277#if defined(LWS_WITH_HL)
278 lws_hl_ctx_t hlctx;
279 lws_hl_html_t hlhtml;
280 uint8_t hl_on; /* bridging a fenced code block */
281 uint8_t hl_fin; /* hl finish issued */
282 uint8_t hl_closed;
283 uint8_t hl_hold; /* a byte held for the tokenizer */
284 uint8_t hl_holdb;
285 size_t hl_off; /* fed offset into current TEXT */
286#endif
287
289 size_t url_len;
290 uint8_t pending; /* 0 none, 1 A, 2 IMG */
291 uint8_t a_open; /* the <a...> tag flushed */
292 uint8_t alt_open; /* inside the img alt attr */
293 uint8_t code_open; /* inside a fence */
294 uint8_t pre_done; /* the <pre><code> flushed */
297
299 size_t buflen;
301
316 lws_md_resolve_cb resolve, void *resolve_user);
317
335 unsigned int aux, const uint8_t *data, size_t len);
336
unsigned int uint32_t
#define LWS_EXTERN
unsigned char uint8_t
lws_stateful_ret_t
#define LWS_VISIBLE
struct lws_hl_html lws_hl_html_t
struct lws_hl_ctx lws_hl_ctx_t
Definition lws-hl.h:71
#define LMD_HOLD_MAX
Definition lws-md.h:48
#define LMD_NEST_MAX
Definition lws-md.h:54
uint32_t evseq
Definition lws-md.h:133
lws_md_write_cb wc
Definition lws-md.h:272
uint8_t fmatch
Definition lws-md.h:150
#define LMD_LINE_MAX
Definition lws-md.h:45
uint8_t fbol
Definition lws-md.h:151
lws_stateful_ret_t(* lws_md_ev_cb)(void *user, lws_md_ev_t ev, lws_md_el_t el, unsigned int aux, const uint8_t *data, size_t len)
Definition lws-md.h:116
uint8_t over
Definition lws-md.h:154
uint8_t flen
Definition lws-md.h:149
#define LMD_URL_MAX
Definition lws-md.h:224
lws_stateful_ret_t(* lws_md_write_cb)(void *user, const uint8_t *buf, size_t len)
Definition lws-md.h:230
uint8_t fin_n
Definition lws-md.h:156
char info[LMD_INFO_MAX]
Definition lws-md.h:163
#define LMD_INFO_MAX
Definition lws-md.h:51
uint8_t in_link
Definition lws-md.h:137
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_md_html_close(lws_md_html_t *h)
uint8_t code_ind
Definition lws-md.h:145
uint8_t table
Definition lws-md.h:142
uint8_t bq
Definition lws-md.h:144
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_md_finish(lws_md_ctx_t *ctx)
void * user
Definition lws-md.h:273
uint8_t pre_done
Definition lws-md.h:294
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_md_parse(lws_md_ctx_t *ctx, const uint8_t **buf, size_t *len)
char info[LMD_INFO_MAX]
Definition lws-md.h:295
LWS_VISIBLE LWS_EXTERN int lws_md_html_construct(lws_md_html_t *h, lws_md_write_cb wc, void *user, lws_md_resolve_cb resolve, void *resolve_user)
size_t buflen
Definition lws-md.h:299
uint8_t infolen
Definition lws-md.h:296
lws_md_el_t
Definition lws-md.h:61
@ LMD_EL_STRONG
Definition lws-md.h:79
@ LMD_EL_IMG
Definition lws-md.h:82
@ LMD_EL_NONE
Definition lws-md.h:62
@ LMD_EL_OL
Definition lws-md.h:68
@ LMD_EL_TABLE
Definition lws-md.h:70
@ LMD_EL_EM
Definition lws-md.h:78
@ LMD_EL_CELL
Definition lws-md.h:72
@ LMD_EL_TR
Definition lws-md.h:71
@ LMD_EL_BQ
Definition lws-md.h:66
@ LMD_EL_H
Definition lws-md.h:64
@ LMD_EL_CS
Definition lws-md.h:80
@ LMD_EL_HR
Definition lws-md.h:73
@ LMD_EL_LI
Definition lws-md.h:69
@ LMD_EL_A
Definition lws-md.h:81
@ LMD_EL_P
Definition lws-md.h:65
@ LMD_EL_CODE
Definition lws-md.h:74
@ LMD_EL_UL
Definition lws-md.h:67
void * user
Definition lws-md.h:129
uint8_t pending
Definition lws-md.h:290
char line[LMD_LINE_MAX]
Definition lws-md.h:165
struct lws_md_ctx lws_md_ctx_t
char buf[LMD_HTML_BUF]
Definition lws-md.h:298
size_t(* lws_md_resolve_cb)(void *user, int is_image, const char *url, size_t len, char *dest, size_t dest_len)
Definition lws-md.h:247
lws_md_ev_cb cb
Definition lws-md.h:128
lws_md_ev_t
Definition lws-md.h:86
@ LMD_EV_TEXT
Definition lws-md.h:87
@ LMD_EV_BEGIN
Definition lws-md.h:91
@ LMD_EV_INFO
Definition lws-md.h:90
@ LMD_EV_URL
Definition lws-md.h:88
@ LMD_EV_ALT
Definition lws-md.h:89
@ LMD_EV_END
Definition lws-md.h:92
uint8_t fclose
Definition lws-md.h:152
uint8_t txn
Definition lws-md.h:136
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_md_html_event(void *user, lws_md_ev_t ev, lws_md_el_t el, unsigned int aux, const uint8_t *data, size_t len)
char url[LMD_URL_MAX]
Definition lws-md.h:288
uint32_t infolen
Definition lws-md.h:162
uint8_t a_open
Definition lws-md.h:291
size_t url_len
Definition lws-md.h:289
uint8_t li
Definition lws-md.h:141
uint8_t fence
Definition lws-md.h:147
struct lws_md_html lws_md_html_t
uint8_t hold
Definition lws-md.h:143
uint8_t code_open
Definition lws-md.h:293
uint8_t para
Definition lws-md.h:139
uint8_t alt_open
Definition lws-md.h:292
uint8_t fchr
Definition lws-md.h:148
lws_md_resolve_cb resolve
Definition lws-md.h:274
void * resolve_user
Definition lws-md.h:275
uint32_t llen
Definition lws-md.h:159
uint8_t list
Definition lws-md.h:140
char holdb[LMD_HOLD_MAX]
Definition lws-md.h:166
uint32_t hlen
Definition lws-md.h:160
uint8_t fin_list[3+2 *LMD_NEST_MAX]
Definition lws-md.h:157
uint32_t evdone
Definition lws-md.h:134
LWS_VISIBLE LWS_EXTERN int lws_md_construct(lws_md_ctx_t *ctx, lws_md_ev_cb cb, void *user)
#define LMD_HTML_BUF
Definition lws-md.h:227