rdiscount 2.2.0.2 → 2.2.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/ext/gethopt.c ADDED
@@ -0,0 +1,285 @@
1
+ /*
2
+ * gehopt; options processing with both single-character and whole-word
3
+ * options both introduced with -
4
+ */
5
+
6
+ #include <stdio.h>
7
+ #include <string.h>
8
+
9
+ #include "gethopt.h"
10
+
11
+
12
+ void
13
+ hoptset(ctx, argc, argv)
14
+ struct h_context *ctx;
15
+ int argc;
16
+ char **argv;
17
+ {
18
+ memset(ctx, 0, sizeof *ctx);
19
+ ctx->argc = argc;
20
+ ctx->argv = argv;
21
+ ctx->optind = 1;
22
+ }
23
+
24
+
25
+ char *
26
+ hoptarg(ctx)
27
+ struct h_context *ctx;
28
+ {
29
+ return ctx->optarg;
30
+ }
31
+
32
+ int
33
+ hoptind(ctx)
34
+ struct h_context *ctx;
35
+ {
36
+ return ctx->optind;
37
+ }
38
+
39
+ char
40
+ hoptopt(ctx)
41
+ struct h_context *ctx;
42
+ {
43
+ return ctx->optopt;
44
+ }
45
+
46
+
47
+ int
48
+ hopterr(struct h_context *ctx, int val)
49
+ {
50
+ int old = ctx->opterr;
51
+
52
+ ctx->opterr = !!val;
53
+ return old;
54
+ }
55
+
56
+
57
+ struct h_opt *
58
+ gethopt(ctx, opts, nropts)
59
+ struct h_context *ctx;
60
+ struct h_opt *opts;
61
+ int nropts;
62
+ {
63
+ int i;
64
+ int dashes;
65
+
66
+
67
+ if ( (ctx == 0) || ctx->optend || (ctx->optind >= ctx->argc) )
68
+ return 0;
69
+
70
+ ctx->optarg = 0;
71
+ ctx->optopt = 0;
72
+
73
+ if ( ctx->optchar == 0) {
74
+ /* check for leading -
75
+ */
76
+ if ( ctx->argv[ctx->optind][0] != '-' ) {
77
+ /* out of arguments */
78
+ ctx->optend = 1;
79
+ return 0;
80
+ }
81
+
82
+ if ( ctx->argv[ctx->optind][1] == 0
83
+ || strcmp(ctx->argv[ctx->optind], "--") == 0 ) {
84
+ /* option list finishes with - or -- token
85
+ */
86
+ ctx->optend = 1;
87
+ ctx->optind++;
88
+ return 0;
89
+ }
90
+
91
+ dashes = 1;
92
+ if ( ctx->argv[ctx->optind][dashes] == '-' ) {
93
+ /* support GNU-style long option double-dash prefix
94
+ * (if gethopt is passed an unknown option with a double-dash
95
+ * prefix, it won't match a word and then the second dash
96
+ * will be scanned as if it was a regular old single-character
97
+ * option.)
98
+ */
99
+ dashes = 2;
100
+ }
101
+
102
+ for ( i=0; i < nropts; i++ ) {
103
+ if ( ! opts[i].optword )
104
+ continue;
105
+
106
+ if (strcmp(opts[i].optword, dashes+(ctx->argv[ctx->optind]) ) == 0 ) {
107
+ if ( opts[i].opthasarg ) {
108
+ if ( ctx->argc > ctx->optind ) {
109
+ ctx->optarg = ctx->argv[ctx->optind+1];
110
+ ctx->optind += 2;
111
+ }
112
+ else {
113
+ /* word argument with required arg at end of
114
+ *command line
115
+ */
116
+ if ( ctx->opterr )
117
+ fprintf(stderr,
118
+ "%s: option requires an argument -- %s\n",
119
+ ctx->argv[0], opts[i].optword);
120
+ ctx->optind ++;
121
+ return HOPTERR;
122
+ }
123
+ }
124
+ else {
125
+ ctx->optind ++;
126
+ }
127
+ return &opts[i];
128
+ }
129
+ }
130
+ ctx->optchar = 1;
131
+ }
132
+
133
+ ctx->optopt = ctx->argv[ctx->optind][ctx->optchar++];
134
+
135
+ if ( !ctx->optopt ) {
136
+ /* fell off the end of this argument */
137
+ ctx->optind ++;
138
+ ctx->optchar = 0;
139
+ return gethopt(ctx, opts, nropts);
140
+ }
141
+
142
+ for ( i=0; i<nropts; i++ ) {
143
+ if ( opts[i].optchar == ctx->optopt ) {
144
+ /* found a single-char option!
145
+ */
146
+ if ( opts[i].opthasarg ) {
147
+ if ( ctx->argv[ctx->optind][ctx->optchar] ) {
148
+ /* argument immediately follows this options (-Oc)
149
+ */
150
+ ctx->optarg = &ctx->argv[ctx->optind][ctx->optchar];
151
+ ctx->optind ++;
152
+ ctx->optchar = 0;
153
+ }
154
+ else if ( ctx->optind < ctx->argc-1 ) {
155
+ /* argument is next arg (-O c)
156
+ */
157
+ ctx->optarg = &ctx->argv[ctx->optind+1][0];
158
+ ctx->optind += 2;
159
+ ctx->optchar = 0;
160
+ }
161
+ else {
162
+ /* end of arg string (-O); set optarg to null, return
163
+ * (should it opterr on me?)
164
+ */
165
+ ctx->optarg = 0;
166
+ ctx->optind ++;
167
+ ctx->optchar = 0;
168
+ if ( ctx->opterr )
169
+ fprintf(stderr,
170
+ "%s: option requires an argument -- %c\n",
171
+ ctx->argv[0], opts[i].optchar);
172
+ return HOPTERR;
173
+ }
174
+ }
175
+ else {
176
+ if ( !ctx->argv[ctx->optind][ctx->optchar] ) {
177
+ ctx->optind ++;
178
+ ctx->optchar = 0;
179
+ }
180
+ }
181
+ return &opts[i];
182
+ }
183
+ }
184
+ if ( ctx->opterr )
185
+ fprintf(stderr, "%s: illegal option -- %c\n", ctx->argv[0], ctx->optopt);
186
+ return HOPTERR;
187
+ }
188
+
189
+
190
+ void
191
+ hoptusage(char *pgm, struct h_opt opts[], int nropts, char *arguments)
192
+ {
193
+ int i;
194
+ int optcount;
195
+
196
+ fprintf(stderr, "usage: %s", pgm);
197
+
198
+ /* print out the options that don't have flags first */
199
+
200
+ for ( optcount=i=0; i < nropts; i++ ) {
201
+ if ( opts[i].optchar && !opts[i].opthasarg) {
202
+ if (optcount == 0 )
203
+ fputs(" [-", stderr);
204
+ fputc(opts[i].optchar, stderr);
205
+ optcount++;
206
+ }
207
+ }
208
+ if ( optcount )
209
+ fputc(']', stderr);
210
+
211
+ /* print out the options WITH flags */
212
+ for ( i = 0; i < nropts; i++ )
213
+ if ( opts[i].optchar && opts[i].opthasarg)
214
+ fprintf(stderr, " [-%c %s]", opts[i].optchar, opts[i].opthasarg);
215
+
216
+ /* print out the long options */
217
+ for ( i = 0; i < nropts; i++ )
218
+ if ( opts[i].optword ) {
219
+ fprintf(stderr, " [-%s", opts[i].optword);
220
+ if ( opts[i].opthasarg )
221
+ fprintf(stderr, " %s", opts[i].opthasarg);
222
+ fputc(']', stderr);
223
+ }
224
+
225
+ /* print out the arguments string, if any */
226
+
227
+ if ( arguments )
228
+ fprintf(stderr, " %s", arguments);
229
+
230
+ /* and we're done */
231
+ fputc('\n', stderr);
232
+ }
233
+
234
+
235
+ #if DEBUG
236
+ struct h_opt opts[] = {
237
+ { 0, "css", 0, 1, "css file" },
238
+ { 1, "header", 0, 1, "header file" },
239
+ { 2, 0, 'a', 0, "option a (no arg)" },
240
+ { 3, 0, 'b', 1, "option B (with arg)" },
241
+ { 4, "help", '?', 0, "help message" },
242
+ } ;
243
+
244
+ #define NROPT (sizeof opts/sizeof opts[0])
245
+
246
+
247
+ int
248
+ main(argc, argv)
249
+ char **argv;
250
+ {
251
+ struct h_opt *ret;
252
+ struct h_context ctx;
253
+ int i;
254
+
255
+
256
+ hoptset(&ctx, argc, argv);
257
+ hopterr(&ctx, 1);
258
+
259
+ while (( ret = gethopt(&ctx, opts, NROPT) )) {
260
+
261
+ if ( ret != HOPTERR ) {
262
+ if ( ret->optword )
263
+ printf("%s", ret->optword);
264
+ else
265
+ printf("%c", ret->optchar);
266
+
267
+ if ( ret->opthasarg ) {
268
+ if ( hoptarg(&ctx) )
269
+ printf(" with argument \"%s\"", hoptarg(&ctx));
270
+ else
271
+ printf(" with no argument?");
272
+ }
273
+ printf(" (%s)\n", ret->optdesc);
274
+ }
275
+ }
276
+
277
+ argc -= hoptind(&ctx);
278
+ argv += hoptind(&ctx);
279
+
280
+ for ( i=0; i < argc; i++ )
281
+ printf("%d: %s\n", i, argv[i]);
282
+ return 0;
283
+ }
284
+
285
+ #endif /*DEBUG*/
data/ext/gethopt.h ADDED
@@ -0,0 +1,43 @@
1
+ /*
2
+ * gethopt; options processing with both single-character and whole-work
3
+ * options both introduced with -
4
+ */
5
+
6
+ #ifndef __GETHOPT_D
7
+ #define __GETHOPT_D
8
+
9
+ #include <stdio.h>
10
+ #include <string.h>
11
+
12
+
13
+ struct h_opt {
14
+ int option;
15
+ char *optword;
16
+ char optchar;
17
+ char *opthasarg;
18
+ char *optdesc;
19
+ } ;
20
+
21
+ #define HOPTERR ((struct h_opt*)-1)
22
+
23
+ struct h_context {
24
+ char **argv;
25
+ int argc;
26
+ int optchar;
27
+ int optind;
28
+ char *optarg;
29
+ char optopt;
30
+ int opterr:1;
31
+ int optend:1;
32
+ } ;
33
+
34
+ extern char *hoptarg(struct h_context *);
35
+ extern int hoptind(struct h_context *);
36
+ extern char hoptopt(struct h_context *);
37
+ extern void hoptset(struct h_context *, int, char **);
38
+ extern int hopterr(struct h_context *, int);
39
+ extern struct h_opt *gethopt(struct h_context *, struct h_opt*, int);
40
+
41
+ extern void hoptusage(char *, struct h_opt*, int, char *);
42
+
43
+ #endif/*__GETHOPT_D*/
@@ -30,7 +30,7 @@ gfm_populate(getc_func getc, void* ctx, int flags)
30
30
 
31
31
  if ( !a ) return 0;
32
32
 
33
- a->tabstop = (flags & MKD_TABSTOP) ? 4 : TABSTOP;
33
+ a->tabstop = is_flag_set(flags, MKD_TABSTOP) ? 4 : TABSTOP;
34
34
 
35
35
  CREATE(line);
36
36
 
@@ -59,16 +59,17 @@ gfm_populate(getc_func getc, void* ctx, int flags)
59
59
 
60
60
  DELETE(line);
61
61
 
62
- if ( (pandoc == 3) && !(flags & (MKD_NOHEADER|MKD_STRICT)) ) {
62
+ if ( (pandoc == 3) && !(is_flag_set(flags, MKD_NOHEADER)
63
+ || is_flag_set(flags, MKD_STRICT)) ) {
63
64
  /* the first three lines started with %, so we have a header.
64
65
  * clip the first three lines out of content and hang them
65
66
  * off header.
66
67
  */
67
68
  Line *headers = T(a->content);
68
69
 
69
- a->title = headers; __mkd_header_dle(a->title);
70
- a->author= headers->next; __mkd_header_dle(a->author);
71
- a->date = headers->next->next; __mkd_header_dle(a->date);
70
+ a->title = headers; __mkd_trim_line(a->title, 1);
71
+ a->author= headers->next; __mkd_trim_line(a->author, 1);
72
+ a->date = headers->next->next; __mkd_trim_line(a->date, 1);
72
73
 
73
74
  T(a->content) = headers->next->next->next;
74
75
  }
@@ -80,7 +81,7 @@ gfm_populate(getc_func getc, void* ctx, int flags)
80
81
  /* convert a block of text into a linked list
81
82
  */
82
83
  Document *
83
- gfm_string(const char *buf, int len, DWORD flags)
84
+ gfm_string(const char *buf, int len, mkd_flag_t flags)
84
85
  {
85
86
  struct string_stream about;
86
87
 
@@ -94,7 +95,7 @@ gfm_string(const char *buf, int len, DWORD flags)
94
95
  /* convert a file into a linked list
95
96
  */
96
97
  Document *
97
- gfm_in(FILE *f, DWORD flags)
98
+ gfm_in(FILE *f, mkd_flag_t flags)
98
99
  {
99
100
  return gfm_populate((getc_func)fgetc, f, flags & INPUT_MASK);
100
101
  }
data/ext/h1title.c ADDED
@@ -0,0 +1,36 @@
1
+ #include <stdio.h>
2
+ #include "markdown.h"
3
+
4
+ static Paragraph *
5
+ mkd_h1(Paragraph *p)
6
+ {
7
+ Paragraph *found;
8
+
9
+ while ( p ) {
10
+ if ( p->typ == HDR && p->hnumber == 1 )
11
+ return p;
12
+ if ( p->down && (found = mkd_h1(p->down)) )
13
+ return found;
14
+ p = p->next;
15
+ }
16
+ return 0;
17
+ }
18
+
19
+ char *
20
+ mkd_h1_title(Document *doc, int flags)
21
+ {
22
+ Paragraph *title;
23
+
24
+ if (doc && (title = mkd_h1(doc->code)) ) {
25
+ char *generated;
26
+ int size;
27
+
28
+ /* assert that a H1 header is one line long, so that's
29
+ * the only thing needed
30
+ */
31
+ size = mkd_line(T(title->text->text),
32
+ S(title->text->text), &generated, flags|MKD_TAGTEXT);
33
+ if ( size ) return generated;
34
+ }
35
+ return 0;
36
+ }
data/ext/html5.c CHANGED
@@ -13,7 +13,6 @@ mkd_with_html5_tags()
13
13
  mkd_define_tag("ASIDE", 0);
14
14
  mkd_define_tag("FOOTER", 0);
15
15
  mkd_define_tag("HEADER", 0);
16
- mkd_define_tag("HGROUP", 0);
17
16
  mkd_define_tag("NAV", 0);
18
17
  mkd_define_tag("SECTION", 0);
19
18
  mkd_define_tag("ARTICLE", 0);