forked from Orc/discount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakepage.c
102 lines (87 loc) · 2.04 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
/*
* 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(argc, argv)
int argc;
char **argv;
{
MMIOT *doc;
char *q;
int version = 0;
int ret;
mkd_flag_t flags = 0;
struct h_opt *opt;
struct h_context blob;
if ( (q = getenv("MARKDOWN_FLAGS")) )
flags = strtol(q, 0, 0);
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);
exit(0);
}
else
flags = strtol(hoptarg(&blob), 0, 0);
break;
case 'f': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
show_flags(1,version);
exit(0);
}
else if ( q = set_flag(&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);
return (ret == EOF);
}