-
Notifications
You must be signed in to change notification settings - Fork 156
/
pgm_options.c
210 lines (180 loc) · 6.67 KB
/
pgm_options.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
/* markdown: a C implementation of John Gruber's Markdown markup language.
*
* Copyright (C) 2007-2011 Jessica 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 <errno.h>
#include <string.h>
#include <stdarg.h>
#include "config.h"
#include "markdown.h"
#include "amalloc.h"
#if HAVE_LIBGEN_H
#include <libgen.h>
#endif
static void set_dlist(mkd_flag_t *flags, int enable);
static struct _special {
char *name;
void (*setter)(mkd_flag_t *, int);
} special[] = {
{ "definitionlist", set_dlist },
{ "dlist", set_dlist },
};
static struct _opt {
char *name;
char *desc;
int special;
int off;
int alias; /* this opt is a synonym; don't display in -F? */
int sayenable;
int flag; /* flag to set/clear */
int unflag; /* flag to clear/set */
} opts[] = {
{ "tabstop", "default (4-space) tabstops", 0, 0, 0, 1, MKD_TABSTOP },
{ "image", "images", 0, 1, 0, 1, MKD_NOIMAGE },
{ "links", "links", 0, 1, 0, 1, MKD_NOLINKS },
{ "strict", "conform to the markdown standard", 0, 0, 0, 1, MKD_STRICT },
{ "relax", "conform to the markdown standard", 0, 1, 1, 1, MKD_STRICT },
{ "standard", "conform to the markdown standard", 0, 0, 1, 1, MKD_STRICT },
{ "tables", "tables", 0, 1, 0, 1, MKD_NOTABLES },
{ "header", "pandoc-style headers", 0, 1, 0, 1, MKD_NOHEADER },
{ "html", "allow raw html", 0, 1, 0, 0, MKD_NOHTML },
{ "ext", "extended protocols", 0, 1, 0, 1, MKD_NO_EXT },
{ "cdata", "generate cdata", 0, 0, 0, 0, MKD_CDATA },
{ "smarty", "smartypants", 0, 1, 0, 1, MKD_NOPANTS },
{ "pants", "smartypants", 0, 1, 1, 1, MKD_NOPANTS },
{ "toc", "tables of contents", 0, 0, 0, 1, MKD_TOC },
{ "autolink", "autolinking", 0, 0, 0, 1, MKD_AUTOLINK },
{ "safelink", "safe links", 0, 0, 0, 1, MKD_SAFELINK },
{ "strikethrough", "strikethrough", 0, 1, 0, 1, MKD_NOSTRIKETHROUGH },
{ "del", "strikethrough", 0, 1, 1, 1, MKD_NOSTRIKETHROUGH },
{ "superscript", "superscript", 0, 1, 0, 1, MKD_NOSUPERSCRIPT },
{ "divquote", ">%class% blockquotes", 0, 1, 0, 1, MKD_NODIVQUOTE },
{ "alphalist", "alpha lists", 0, 1, 0, 1, MKD_NOALPHALIST },
{ "1.0", "markdown 1.0 compatibility", 0, 0, 0, 1, MKD_1_COMPAT },
{ "footnotes", "markdown extra footnotes", 0, 0, 0, 1, MKD_EXTRA_FOOTNOTE },
{ "footnote", "markdown extra footnotes", 0, 0, 1, 1, MKD_EXTRA_FOOTNOTE },
{ "style", "extract style blocks", 0, 1, 0, 1, MKD_NOSTYLE },
{ "dldiscount", "discount-style definition lists", 0, 0, 0, 1, MKD_DLDISCOUNT },
{ "dlextra", "markdown extra-style definition lists", 0, 0, 0, 1, MKD_DLEXTRA },
{ "fencedcode", "fenced code blocks", 0, 0, 0, 1, MKD_FENCEDCODE },
{ "idanchor", "id= anchors in TOC", 0, 0, 0, 1, MKD_IDANCHOR },
{ "githubtags", "- and _ in element names", 0, 0, 0, 1, MKD_GITHUBTAGS },
{ "urlencodedanchor", "html5-style anchors", 0, 0, 0, 1, MKD_URLENCODEDANCHOR },
{ "html5anchor", "html5-style anchors", 0, 0, 1, 1, MKD_URLENCODEDANCHOR },
{ "latex", "LaTeX escapes", 0, 0, 0, 1, MKD_LATEX },
{ "explicitlist", "merge adjacent numeric/bullet lists", 0, 0, 0, 0, MKD_EXPLICITLIST },
{ "github-listitem","github-style check items", 0, 1, 0, 1, MKD_NORMAL_LISTITEM } ,
{ "regular-listitem","github-style check items", 0, 0, 1, 1, MKD_NORMAL_LISTITEM } ,
{ "definitionlist","both discount & markdown extra definition lists", 1 },
{ "dlist", "both discount & markdown extra definition lists", 1, 0, 1 },
{ "alt_as_title", "use the alt text as a title if there isn't one (images)", 0, 0, 0, 1, MKD_ALT_AS_TITLE },
} ;
#define NR(x) (sizeof x / sizeof x[0])
typedef int (*stfu)(const void *, const void *);
int
sort_by_name(struct _opt *a, struct _opt *b)
{
return strcmp(a->name,b->name);
}
int
sort_by_flag(struct _opt *a, struct _opt *b)
{
return a->flag - b->flag;
}
void
show_flags(int byname, int verbose, mkd_flag_t *flags)
{
int i;
if ( byname ) {
qsort(opts, NR(opts), sizeof(opts[0]), (stfu)sort_by_name);
for (i=0; i < NR(opts); i++) {
if ( opts[i].alias && !verbose )
continue;
if ( (flags==0) || is_flag_set(flags, opts[i].flag) )
fprintf(stderr, "%16s : %s\n", opts[i].name, opts[i].desc);
}
}
else {
qsort(opts, NR(opts), sizeof(opts[0]), (stfu)sort_by_flag);
for (i=0; i < NR(opts) && i < 8*sizeof(DWORD); i++) {
if ( opts[i].special || opts[i].alias )
continue;
if ( (flags==0) || is_flag_set(flags, opts[i].flag) ) {
fprintf(stderr, "%08lx : ", 1L<<opts[i].flag);
if ( opts[i].sayenable )
fprintf(stderr, opts[i].off ? "disable " : "enable ");
fprintf(stderr, "%s\n", opts[i].desc);
}
}
}
}
static void
handle_special(mkd_flag_t *flags, char *opt, int enable)
{
int i;
for (i=0; i < NR(special); i++)
if ( strcasecmp(opt, special[i].name) == 0 ) {
(special[i].setter)(flags, enable);
return;
}
}
char *
mkd_set_flag_string(mkd_flag_t *flags, char *optionstring)
{
int i;
int enable;
char *arg;
if ( flags == 0 ) /* shouldn't happen */
return "NULL";
for ( arg = strtok(optionstring, ","); arg; arg = strtok(NULL, ",") ) {
if ( *arg == '+' || *arg == '-' )
enable = (*arg++ == '+') ? 1 : 0;
else if ( strncasecmp(arg, "no", 2) == 0 ) {
arg += 2;
enable = 0;
}
else
enable = 1;
for ( i=0; i < NR(opts); i++ )
if ( strcasecmp(arg, opts[i].name) == 0 )
break;
if ( i < NR(opts) ) {
if ( opts[i].special ) {
handle_special(flags, opts[i].name, enable);
continue;
}
if ( opts[i].off )
enable = !enable;
if ( enable ) {
set_mkd_flag(flags, opts[i].flag);
if ( opts[i].unflag )
clear_mkd_flag(flags, opts[i].unflag);
}
else {
clear_mkd_flag(flags, opts[i].flag);
if ( opts[i].unflag )
set_mkd_flag(flags, opts[i].unflag);
}
}
else
return arg;
}
return 0;
}
static void
set_dlist(mkd_flag_t *flags, int enable)
{
if ( enable ) {
set_mkd_flag(flags, MKD_DLDISCOUNT);
set_mkd_flag(flags, MKD_DLEXTRA);
}
else {
clear_mkd_flag(flags, MKD_DLDISCOUNT);
clear_mkd_flag(flags, MKD_DLEXTRA);
}
}