forked from Orc/discount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
215 lines (192 loc) · 4.2 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* markdown: convert a single markdown document into html
*/
/*
* Copyright (C) 2007 David L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <mkdio.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include "config.h"
#include "amalloc.h"
#include "pgm_options.h"
#include "tags.h"
#if HAVE_LIBGEN_H
#include <libgen.h>
#endif
#ifndef HAVE_BASENAME
#include <string.h>
char*
basename(char *p)
{
char *ret = strrchr(p, '/');
return ret ? (1+ret) : p;
}
#endif
char *pgm = "markdown";
char *
e_flags(const char *text, const int size, void *context)
{
return (char*)context;
}
void
complain(char *fmt, ...)
{
va_list ptr;
fprintf(stderr, "%s: ", pgm);
va_start(ptr, fmt);
vfprintf(stderr, fmt, ptr);
va_end(ptr);
fputc('\n', stderr);
fflush(stderr);
}
main(int argc, char **argv)
{
int opt;
int rc;
mkd_flag_t flags = 0;
int debug = 0;
int toc = 0;
int content = 1;
int version = 0;
int with_html5 = 0;
int styles = 0;
int use_mkd_line = 0;
int github_flavoured = 0;
char *extra_footnote_prefix = 0;
char *urlflags = 0;
char *text = 0;
char *ofile = 0;
char *urlbase = 0;
char *q;
MMIOT *doc;
if ( q = getenv("MARKDOWN_FLAGS") )
flags = strtol(q, 0, 0);
pgm = basename(argv[0]);
opterr = 1;
while ( (opt=getopt(argc, argv, "5b:C:df:E:F:Gno:s:St:TV")) != EOF ) {
switch (opt) {
case '5': with_html5 = 1;
break;
case 'b': urlbase = optarg;
break;
case 'd': debug = 1;
break;
case 'V': version++;
break;
case 'E': urlflags = optarg;
break;
case 'F': if ( strcmp(optarg, "?") == 0 ) {
show_flags(0);
exit(0);
}
else
flags = strtol(optarg, 0, 0);
break;
case 'f': if ( strcmp(optarg, "?") == 0 ) {
show_flags(1);
exit(0);
}
else if ( !set_flag(&flags, optarg) )
complain("unknown option <%s>", optarg);
break;
case 'G': github_flavoured = 1;
break;
case 'n': content = 0;
break;
case 's': text = optarg;
break;
case 'S': styles = 1;
break;
case 't': text = optarg;
use_mkd_line = 1;
break;
case 'T': toc = 1;
break;
case 'C': extra_footnote_prefix = optarg;
break;
case 'o': if ( ofile ) {
complain("Too many -o options");
exit(1);
}
if ( !freopen(ofile = optarg, "w", stdout) ) {
perror(ofile);
exit(1);
}
break;
default: fprintf(stderr, "usage: %s [-dTV] [-b url-base]"
" [-F bitmap] [-f {+-}flags]"
" [-o ofile] [-s text]"
" [-t text] [file]\n", pgm);
exit(1);
}
}
if ( version ) {
printf("%s: discount %s%s", pgm, markdown_version,
with_html5 ? " +html5":"");
if ( version > 1 )
mkd_flags_are(stdout, flags, 0);
putchar('\n');
exit(0);
}
argc -= optind;
argv += optind;
if ( with_html5 )
mkd_with_html5_tags();
if ( use_mkd_line )
rc = mkd_generateline( text, strlen(text), stdout, flags);
else {
if ( text ) {
doc = github_flavoured ? gfm_string(text, strlen(text), flags)
: mkd_string(text, strlen(text), flags) ;
if ( !doc ) {
perror(text);
exit(1);
}
}
else {
if ( argc && !freopen(argv[0], "r", stdin) ) {
perror(argv[0]);
exit(1);
}
doc = github_flavoured ? gfm_in(stdin,flags) : mkd_in(stdin,flags);
if ( !doc ) {
perror(argc ? argv[0] : "stdin");
exit(1);
}
}
if ( urlbase )
mkd_basename(doc, urlbase);
if ( urlflags ) {
mkd_e_data(doc, urlflags);
mkd_e_flags(doc, e_flags);
}
if ( extra_footnote_prefix )
mkd_ref_prefix(doc, extra_footnote_prefix);
if ( debug )
rc = mkd_dump(doc, stdout, 0, argc ? basename(argv[0]) : "stdin");
else {
rc = 1;
if ( mkd_compile(doc, flags) ) {
rc = 0;
if ( styles )
mkd_generatecss(doc, stdout);
if ( toc )
mkd_generatetoc(doc, stdout);
if ( content )
mkd_generatehtml(doc, stdout);
mkd_cleanup(doc);
}
}
}
mkd_deallocate_tags();
adump();
exit( (rc == 0) ? 0 : errno );
}