-
Notifications
You must be signed in to change notification settings - Fork 156
/
makepage.c
116 lines (97 loc) · 2.35 KB
/
makepage.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
/*
* makepage: Use mkd_xhtmlpage() to convert markdown input to a
* fully-formed xhtml page.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mkdio.h>
#include "config.h"
#include "pgm_options.h"
#include "gethopt.h"
#ifndef HAVE_BASENAME
char*
basename(char *p)
{
char *ret = strrchr(p, '/');
return ret ? (1+ret) : p;
}
#endif
char *pgm = "makepage";
struct h_opt opts[] = {
{ 0, "version", 'V', 0, "show version info" },
{ 0, 0, 'F', "bitmap", "set/show hex flags" },
{ 0, "flags", 'f', "{+-}flags", "set/show named flags" },
} ;
#define NROPTS (sizeof opts / sizeof opts[0])
int
main(int argc, char **argv)
{
MMIOT *doc;
char *q;
int version = 0;
int ret, i;
DWORD bits;
mkd_flag_t *flags = mkd_flags();
struct h_opt *opt;
struct h_context blob;
if ( !flags )
perror("mkd_flags");
if ( (q = getenv("MARKDOWN_FLAGS")) ) {
bits = strtol(q, 0, 0);
for ( i=0; i < 8*sizeof(bits); i++)
if ( bits & (1<<i) )
mkd_set_flag_num(flags, i);
}
hoptset(&blob, argc, argv);
hopterr(&blob, 1);
while ( opt = gethopt(&blob, opts, NROPTS) ) {
if ( opt == HOPTERR ) {
hoptusage(pgm, opts, NROPTS, "[file]");
exit(1);
}
switch ( opt->optchar ) {
case 'V': version++;
break;
case 'F': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
show_flags(0,0,0);
exit(0);
}
else {
bits = strtol(hoptarg(&blob), 0, 0);
for (i=0; i < 8*sizeof(bits); i++)
if ( bits & (1<<i) )
mkd_set_flag_num(flags, i);
}
break;
case 'f': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
show_flags(1,version,0);
exit(0);
}
else if ( q = mkd_set_flag_string(flags, hoptarg(&blob)) )
fprintf(stderr, "unknown option <%s>\n", q);
break;
}
}
argc -= hoptind(&blob);
argv += hoptind(&blob);
if ( version ) {
printf("%s: discount %s", pgm, markdown_version);
if ( version > 1 )
mkd_flags_are(stdout, flags, 0);
putchar('\n');
exit(0);
}
if ( (argc > 0) && !freopen(argv[0], "r", stdin) ) {
perror(argv[0]);
exit(1);
}
if ( (doc = mkd_in(stdin, flags)) == 0 ) {
perror( (argc > 1) ? argv[1] : "stdin" );
exit(1);
}
ret = mkd_xhtmlpage(doc, flags, stdout);
mkd_cleanup(doc);
mkd_free_flags(flags);
return (ret == EOF);
}