-
Notifications
You must be signed in to change notification settings - Fork 0
/
smd.c
153 lines (136 loc) · 4.59 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "read.h"
#include "block.h"
static char stack[256] = {0};
static unsigned char depth = 0;
static FILE *INPUT = NULL;
typedef struct {
char *open, *prefix, *indent, *opentags, *reopentags, *closetags;
} Container;
Container containers[] = {
{"> ", ">", " ", "<blockquote>\n", NULL, "</blockquote>\n"},
{"/// ", "", " ", "<aside>\n", NULL, "</aside>\n"},
{"* ", "", " ", "<ul>\n<li>\n", "</li>\n<li>\n", "</li>\n</ul>\n"},
{"- ", "", " ", "<ul>\n<li>\n", "</li>\n<li>\n", "</li>\n</ul>\n"},
{"+ ", "", " ", "<ul>\n<li>\n", "</li>\n<li>\n", "</li>\n</ul>\n"},
{"0. ", "", " ", "<ol>\n<li>\n", "</li>\n<li>\n", "</li>\n</ol>\n"}
};
static Container getContainer(char c) {
switch (c) {
case '>': return containers[0];
case '/': return containers[1];
case '*': return containers[2];
case '-': return containers[3];
case '+': return containers[4];
default: return isdigit(c) ? containers[5] : (Container){0};
}
}
static inline int startsWith(char* string, char* prefix) {
return string != NULL && strncmp(prefix, string, strlen(prefix)) == 0;
}
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* openBlocks(char* line, FILE* output) {
while (line) {
Container container = getContainer(line[0]);
if (!container.open || !isBlockOpener(line, container))
return line;
fputs(container.opentags, output);
stack[depth++] = container.open[0];
line += strlen(container.open);
}
return line;
}
static int getContinuationPrefixLength(char* line, Container container) {
if (!startsWith(line, container.prefix))
return -1;
int length = strlen(container.prefix);
if (line[length] == '\n' || line[length] == '\r')
return length;
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 line + strlen(container.open);
}
int length = getContinuationPrefixLength(line, container);
if (length < 0) {
closeLevel(level, output);
return 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);
}