-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsundown.c
115 lines (82 loc) · 2.76 KB
/
sundown.c
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
112
113
114
115
/* -*- Mode: C; Character-encoding: utf-8; -*- */
/* Copyright (C) 2004-2019 beingmeta, inc.
Copyright (C) 2020-2022 beingmeta, LLC
This file is part of beingmeta's Kno platform and is copyright
and a valuable trade secret of beingmeta, inc.
*/
#ifndef _FILEINFO
#define _FILEINFO __FILE__
#endif
#include "kno/lisp.h"
#include "kno/numbers.h"
#include "kno/eval.h"
#include "kno/cprims.h"
#include <libu8/libu8io.h>
#include "sundown/markdown.h"
#include "sundown/buffer.h"
#include "sundown/autolink.h"
#include "sundown/html.h"
#define OUTPUT_BUF_UNIT 1024
#define HTML_RENDER_FLAGS (HTML_USE_XHTML|HTML_ESCAPE|HTML_SAFELINK)
KNO_EXPORT int kno_init_sundown(void) KNO_LIBINIT_FN;
static int sundown_init = 0;
DEFC_PRIM("markdown->html",markdown2html_prim,
KNO_MAX_ARGS(2)|KNO_MIN_ARGS(1),
"Converts a markdown string to HTML",
{"mdstring",kno_string_type,KNO_VOID},
{"opts",kno_any_type,KNO_VOID})
static lispval markdown2html_prim(lispval mdstring,lispval opts)
{
lispval result = KNO_VOID;
struct sd_callbacks callbacks;
struct html_renderopt options;
struct sd_markdown *markdown;
struct buf *ob;
ob = bufnew(OUTPUT_BUF_UNIT);
sdhtml_renderer(&callbacks, &options, HTML_RENDER_FLAGS);
markdown = sd_markdown_new(0, 16, &callbacks, &options);
sd_markdown_render(ob, KNO_CSTRING(mdstring), KNO_STRLEN(mdstring), markdown);
sd_markdown_free(markdown);
result = kno_make_string(NULL,ob->size,ob->data);
bufrelease(ob);
return result;
}
DEFC_PRIM("markout",markout_prim,
KNO_MAX_ARGS(2)|KNO_MIN_ARGS(1),
"Outputs HTML for a markdown string to the "
"standard output",
{"mdstring",kno_string_type,KNO_VOID},
{"opts",kno_any_type,KNO_VOID})
static lispval markout_prim(lispval mdstring,lispval opts)
{
U8_OUTPUT *out = u8_current_output;
struct sd_callbacks callbacks;
struct html_renderopt options;
struct sd_markdown *markdown;
struct buf *ob;
ob = bufnew(OUTPUT_BUF_UNIT);
sdhtml_renderer(&callbacks, &options, HTML_RENDER_FLAGS);
markdown = sd_markdown_new(0, 16, &callbacks, &options);
sd_markdown_render(ob, KNO_CSTRING(mdstring), KNO_STRLEN(mdstring), markdown);
sd_markdown_free(markdown);
u8_putn(out,ob->data,ob->size);
bufrelease(ob);
return KNO_VOID;
}
static lispval sundown_module;
KNO_EXPORT int kno_init_sundown()
{
if (sundown_init) return 0;
/* u8_register_source_file(_FILEINFO); */
sundown_init = 1;
sundown_module = kno_new_cmodule("sundown",0,kno_init_sundown);
link_local_cprims();
u8_register_source_file(_FILEINFO);
return 1;
}
static void link_local_cprims()
{
KNO_LINK_CPRIM("MARKDOWN->HTML",markdown2html_prim,2,sundown_module);
KNO_LINK_ALIAS("MD->HTML",markdown2html_prim,sundown_module);
KNO_LINK_CPRIM("MARKOUT",markout_prim,2,sundown_module);
}