-
Notifications
You must be signed in to change notification settings - Fork 0
/
smd.c
189 lines (169 loc) · 6.09 KB
/
smd.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "read.h"
#include "inline.h"
#include "block.h"
static char stack[256] = {0};
static unsigned char depth = 0;
static FILE *INPUT = NULL;
typedef struct {
// headtags are inserted after the first line of the container; if headtags
// is not NULL, the first line will be processed with processInlines
char *open, *prefix, *indent, *opentags, *headtags, *reopentags, *closetags;
} Container;
Container containers[] = {
{"0. ", "", " ", "<ol>\n<li>\n", NULL, "</li>\n<li>\n", "</li>\n</ol>\n"},
{"* ", "", " ", "<ul>\n<li>\n", NULL, "</li>\n<li>\n", "</li>\n</ul>\n"},
{"- ", "", " ", "<ul>\n<li>\n", NULL, "</li>\n<li>\n", "</li>\n</ul>\n"},
{"= ", "", " ", "<dl>\n<dt>\n", "</dt>\n<dd>\n", "</dd>\n<dt>\n",
"</dd>\n</dl>\n"},
{"> ", ">", " ", "<blockquote>\n", NULL, NULL, "</blockquote>\n"},
{"\"\"\"", "", "", "<blockquote>\n", NULL, NULL, "</blockquote>\n"},
{":::", "", "", "<aside>\n", NULL, NULL, "</aside>\n"},
{"!!!", "", "", "<aside class=\"admonition\">\n", NULL, NULL, "</aside>\n"},
{"+++", "", "", "<details>\n<summary>\n", "</summary>\n", NULL,
"</details>\n"}
};
static Container getContainer(char c) {
switch (c) {
case '*': return containers[1];
case '-': return containers[2];
case '=': return containers[3];
case '>': return containers[4];
case '"': return containers[5];
case ':': return containers[6];
case '!': return containers[7];
case '+': return containers[8];
default: return isdigit(c) ? containers[0] : (Container){0};
}
}
static inline int isLineEnd(char c) {
return c == '\n' || c == '\r' || c == 0;
}
static inline int isFence(Container container) {
return container.prefix[0] == 0 && container.indent[0] == 0;
}
static inline int isFenceClose(char* line, char* close) {
return startsWith(line, close) && isLineEnd(line[strlen(close)]);
}
static char* getLine(FILE* input, int peek) {
static char peeked = 0, flipped = 0;
static char bufferA[4096], bufferB[sizeof(bufferA)];
char* line = flipped ? bufferB : bufferA;
char* next = flipped ? bufferA : bufferB;
if (peeked) {
peeked = peek;
flipped ^= !peek;
return next + 1;
}
char* buffer = peek ? next : line;
buffer[0] = '\n';
buffer[sizeof(bufferA) - 2] = '\n';
// load buffer with offset so we can safely look back a character
char* result = fgets(buffer + 1, sizeof(bufferA) - 1, input);
if (buffer[sizeof(bufferA) - 2] != '\n') {
fputs("\nError: line too long\n", stderr);
exit(1);
}
peeked = result ? peek : 0;
return result;
}
static char* skipContinuationPrefixes(char* line) {
for (int i = 0; i < depth; i++) {
Container container = getContainer(stack[i]);
if (startsWith(line, container.prefix))
line += strlen(container.prefix);
if (startsWith(line, container.indent))
line += strlen(container.indent);
}
return line;
}
char* readLine(void) {
return skipContinuationPrefixes(getLine(INPUT, 0));
}
char* peekLine(void) {
return skipContinuationPrefixes(getLine(INPUT, 1));
}
int peek(void) {
char* line = peekLine();
return line ? line[0] : EOF;
}
static int isBlockOpener(char* line, Container container) {
if (isdigit(container.open[0]))
return isdigit(line[0]) && startsWith(line + 1, container.open + 1);
return startsWith(line, container.open);
}
static char* processHead(Container container, char* line, FILE* output) {
line += strlen(container.open);
if (isFence(container))
line = skip(line, " \t");
if (container.headtags) {
// treat colon as the end of the head line for description list
char* stop = strcmp(container.open, "= ") ? NULL : strchr(line, ':');
processInlines(skip(line, " \t"), stop, output);
fputs(stop ? "\n" : "", output);
fputs(container.headtags, output);
return stop ? skip(stop + 1, " \t") : "\n";
}
return line;
}
static char* openBlocks(char* line, FILE* output) {
while (line) {
Container container = getContainer(line[0]);
if (!container.open || !isBlockOpener(line, container))
return line;
stack[depth++] = container.open[0];
fputs(container.opentags, output);
line = processHead(container, line, output);
}
return line;
}
static int getContinuationPrefixLength(char* line, Container container) {
if (!startsWith(line, container.prefix))
return -1;
int length = strlen(container.prefix);
if (isLineEnd(line[length]))
return length; // don't require indent if line is empty after prefix
if (isFence(container) && isFenceClose(line, container.open))
return -1;
if (startsWith(line + length, container.indent))
return length + strlen(container.indent);
return -1;
}
static void closeLevel(char index, FILE* output) {
for (unsigned char i = 0; i < depth - index; i++)
fputs(getContainer(stack[depth - i - 1]).closetags, output);
depth = index;
}
static char* closeBlocks(char* line, FILE* output) {
if (line == NULL) {
closeLevel(0, output);
return NULL;
}
for (unsigned char level = 0; level < depth; level++) {
Container container = getContainer(stack[level]);
if (container.reopentags && isBlockOpener(line, container)) {
closeLevel(level + 1, output);
fputs(container.reopentags, output);
return processHead(container, line, output);
}
int length = getContinuationPrefixLength(line, container);
if (length < 0) {
closeLevel(level, output);
return isFence(container) ? "\n" : line;
}
line += length;
}
return line;
}
static char* beginBlock(char* line, FILE* output) {
return openBlocks(closeBlocks(line, output), output);
}
int main(void) {
char* line = NULL;
INPUT = stdin;
while ((line = beginBlock(getLine(INPUT, 0), stdout)))
processBlock(line, stdout);
}