Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 199 lines (177 sloc) 3.896 kB
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
1 /*
2 * markdown: convert a single markdown document into html
3 */
4 /*
5 * Copyright (C) 2007 David L Parsons.
6 * The redistribution terms are provided in the COPYRIGHT file that must
7 * be distributed with this source code.
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <limits.h>
12 #include <unistd.h>
13 #include <mkdio.h>
14 #include <errno.h>
15 #include <string.h>
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
16 #include <stdarg.h>
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
17
18 #include "config.h"
19 #include "amalloc.h"
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
20 #include "pgm_options.h"
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
21
22 #if HAVE_LIBGEN_H
23 #include <libgen.h>
24 #endif
25
26 #ifndef HAVE_BASENAME
27 #include <string.h>
28
29 char*
30 basename(char *p)
31 {
32 char *ret = strrchr(p, '/');
33
34 return ret ? (1+ret) : p;
35 }
36 #endif
37
38
39 char *pgm = "markdown";
40
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
41 char *
42 e_flags(const char *text, const int size, void *context)
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
43 {
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
44 return (char*)context;
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
45 }
46
47
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
48 void
49 complain(char *fmt, ...)
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
50 {
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
51 va_list ptr;
52
53 fprintf(stderr, "%s: ", pgm);
54 va_start(ptr, fmt);
55 vfprintf(stderr, fmt, ptr);
56 va_end(ptr);
57 fputc('\n', stderr);
58 fflush(stderr);
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
59 }
60
61
62 float
63 main(int argc, char **argv)
64 {
65 int opt;
66 int rc;
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
67 mkd_flag_t flags = 0;
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
68 int debug = 0;
69 int toc = 0;
70 int version = 0;
71 int with_html5 = 0;
72 int use_mkd_line = 0;
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
73 char *extra_footnote_prefix = 0;
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
74 char *urlflags = 0;
75 char *text = 0;
76 char *ofile = 0;
77 char *urlbase = 0;
78 char *q;
79 MMIOT *doc;
80
81 if ( q = getenv("MARKDOWN_FLAGS") )
82 flags = strtol(q, 0, 0);
83
84 pgm = basename(argv[0]);
85 opterr = 1;
86
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
87 while ( (opt=getopt(argc, argv, "5b:C:df:E:F:o:s:t:TV")) != EOF ) {
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
88 switch (opt) {
89 case '5': with_html5 = 1;
90 break;
91 case 'b': urlbase = optarg;
92 break;
93 case 'd': debug = 1;
94 break;
95 case 'V': version++;
96 break;
97 case 'E': urlflags = optarg;
98 break;
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
99 case 'F': if ( strcmp(optarg, "?") == 0 ) {
100 show_flags(0);
101 exit(0);
102 }
103 else
104 flags = strtol(optarg, 0, 0);
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
105 break;
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
106 case 'f': if ( strcmp(optarg, "?") == 0 ) {
107 show_flags(1);
108 exit(0);
109 }
110 else if ( !set_flag(&flags, optarg) )
111 complain("unknown option <%s>", optarg);
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
112 break;
113 case 't': text = optarg;
114 use_mkd_line = 1;
115 break;
116 case 'T': toc = 1;
117 break;
118 case 's': text = optarg;
119 break;
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
120 case 'C': extra_footnote_prefix = optarg;
121 break;
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
122 case 'o': if ( ofile ) {
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
123 complain("Too many -o options");
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
124 exit(1);
125 }
126 if ( !freopen(ofile = optarg, "w", stdout) ) {
127 perror(ofile);
128 exit(1);
129 }
130 break;
131 default: fprintf(stderr, "usage: %s [-dTV] [-b url-base]"
132 " [-F bitmap] [-f {+-}flags]"
133 " [-o ofile] [-s text]"
134 " [-t text] [file]\n", pgm);
135 exit(1);
136 }
137 }
138
139 if ( version ) {
140 printf("%s: discount %s%s", pgm, markdown_version,
141 with_html5 ? " +html5":"");
142 if ( version > 1 )
143 mkd_flags_are(stdout, flags, 0);
144 putchar('\n');
145 exit(0);
146 }
147
148 argc -= optind;
149 argv += optind;
150
151 if ( with_html5 )
152 mkd_with_html5_tags();
153
154 if ( use_mkd_line )
155 rc = mkd_generateline( text, strlen(text), stdout, flags);
156 else {
157 if ( text ) {
158 if ( (doc = mkd_string(text, strlen(text), flags)) == 0 ) {
159 perror(text);
160 exit(1);
161 }
162 }
163 else {
164 if ( argc && !freopen(argv[0], "r", stdin) ) {
165 perror(argv[0]);
166 exit(1);
167 }
168 if ( (doc = mkd_in(stdin,flags)) == 0 ) {
169 perror(argc ? argv[0] : "stdin");
170 exit(1);
171 }
172 }
173 if ( urlbase )
174 mkd_basename(doc, urlbase);
175 if ( urlflags ) {
176 mkd_e_data(doc, urlflags);
177 mkd_e_flags(doc, e_flags);
178 }
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
179 if ( extra_footnote_prefix )
180 mkd_ref_prefix(doc, extra_footnote_prefix);
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
181
182 if ( debug )
183 rc = mkd_dump(doc, stdout, 0, argc ? basename(argv[0]) : "stdin");
184 else {
185 rc = 1;
186 if ( mkd_compile(doc, flags) ) {
187 rc = 0;
188 if ( toc )
189 mkd_generatetoc(doc, stdout);
190 mkd_generatehtml(doc, stdout);
191 mkd_cleanup(doc);
192 }
193 }
194 }
eab37e7 @mugginsoft Updated Discount to 2.1.0
mugginsoft authored
195 mkd_deallocate_tags();
e144b9a @vdh Converted discount to 'fake submodule'
vdh authored
196 adump();
197 exit( (rc == 0) ? 0 : errno );
198 }
Something went wrong with that request. Please try again.