summaryrefslogtreecommitdiff
path: root/gmi2html.c
blob: 936a7fceef39055591b0a7e28bba40322510f5ea (plain)
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *lstrip(char *restrict s) {
	size_t i;
	for(i = 0; s[i] != '\0'; i++) {
		if(!isspace(s[i])) {
			return &s[i];
		}
	}
	return &s[i];
}

static char *rstrip(char *restrict s) {
	char *last_nonspace = s;
	for(size_t i = 0; s[i] != '\0'; i++) {
		if(!isspace(s[i])) {
			last_nonspace = &s[i];
		}
	}
	if(last_nonspace[0] != '\0') {
		last_nonspace[1] = '\0';
	}
	return s;
}

static char *strip(char *restrict s) {
	return rstrip(lstrip(s));
}

static void print_sanitized(const char *restrict s) {
	size_t i = 0;
	while(s[i] != '\0') {
		if(s[i] == '<') {
			printf("&lt;");
		}
		else if(s[i] == '>') {
			printf("&gt;");
		}
		else if(s[i] == '&') {
			printf("&amp;");
		}
		else if(s[i] == '"') {
			printf("&quot;");
		}
		else {
			printf("%c", s[i]);
		}
		i++;
	}
}

int main(int argc, char **argv) {
	int list_mode = 0, preformat_mode = 0;
	
	// Read lines
	char *line = NULL;
	size_t line_buf_len = 0;
	ssize_t line_len;
	while((line_len = getline(&line, &line_buf_len, stdin)) != -1) {
		line[line_len - 1] = '\0';
		line_len--;
		
		// Disable list mode
		if(list_mode && (line[0] != '*' || line[1] != ' ')) {
			list_mode = 0;
			printf("</ul>\n");
		}
		
		// Preformatted mode
		if(preformat_mode) {
			if(line_len == 3 && line[0] == '`' && line[1] == '`' && line[2] == '`') {
				preformat_mode = 0;
				printf("</pre>\n");
			}
			else {
				print_sanitized(line);
				printf("\n");
			}
			continue;
		}
		
		// Blank lines
		if(line[0] == '\0') {
			printf("<br />\n");
		}
		
		// Links
		else if(line_len >= 2 && line[0] == '=' && line[1] == '>') {
			
			// Discard leading whitespace
			char *url = lstrip(&line[2]);
			
			// Get label
			char *label = NULL;
			for(size_t i = 0; url[i] != '\0'; i++) {
				if(isspace(url[i])) {
					url[i] = '\0';
					label = &url[i + 1];
					break;
				}
			}
			
			// Print link
			if(label == NULL) {
				label = url;
			}
			else {
				label = strip(label);
			}
			printf("<a href=\"");
			print_sanitized(url);
			printf("\">");
			print_sanitized(label);
			printf("</a><br />\n");
		}
		
		// Headings
		else if(line[0] == '#') {
			unsigned int header_count = 1;
			if(line_len >= 2 && line[1] == '#') {
				header_count++;
				if(line_len >= 3 && line[2] == '#') {
					header_count++;
				}
			}
			printf("<h%d>", header_count);
			print_sanitized(strip(line + header_count));
			printf("</h%d>\n", header_count);
		}
		
		// Lists
		else if(line_len >= 2 && line[0] == '*' && line[1] == ' ') {
			if(!list_mode) {
				list_mode = 1;
				printf("<ul>\n");
			}
			printf("<li>");
			print_sanitized(strip(line + 2));
			printf("</li>\n");
		}
		
		// Blockquotes
		else if(line[0] == '>') {
			printf("<q>");
			print_sanitized(strip(line + 1));
			printf("</q>\n");
		}
		
		// Preformatted text
		else if(line_len >= 3 && line[0] == '`' && line[1] == '`' && line[2] == '`') {
			preformat_mode = 1;
			printf("<pre>\n");
		}
		
		// Regular text
		else {
			printf("<p>");
			print_sanitized(strip(line));
			printf("</p>\n");
		}
	}
	free(line);
	
	// Disable list mode
	if(list_mode) {
		list_mode = 0;
		printf("</ul>\n");
	}
	
	// Preformatted mode
	if(preformat_mode) {
		preformat_mode = 0;
		printf("</pre>\n");
	}
	
	// Return
	if(feof(stdin)) {
		return EXIT_SUCCESS;
	}
	else {
		return EXIT_FAILURE;
	}
}