This repository has been archived by the owner on Feb 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
kilt.c
209 lines (175 loc) · 4.92 KB
/
kilt.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
/* kilt.c - upskirt for man */
/*
* Copyright (c) 2009, Baptiste Daroussin and Natacha Porté
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "markdown.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define READ_UNIT 1024
#define OUTPUT_UNIT 64
/****************************
* MARKDOWN TO MAN RENDERER *
****************************/
void
man_text_escape(struct buf *ob, char *src, size_t size) {
size_t i = 0, org;
while (i < size) {
/* copying directly unescaped characters */
org = i;
while (i < size && src[i] != '-')
i += 1;
if (i > org) bufput(ob, src + org, i - org);
/* escaping */
if (i >= size) break;
else if (src[i] == '-') BUFPUTSL(ob, "\\-");
i += 1; } }
static void
man_blockcode(struct buf *ob, struct buf *text, void *opaque) {
if (ob->size) bufputc(ob, '\n');
BUFPUTSL(ob, ".nf\n");
if (text) man_text_escape(ob, text->data, text->size);
BUFPUTSL(ob, ".fi\n"); }
static int
man_codespan(struct buf *ob, struct buf *text, void *opaque) {
if (ob->size) bufputc(ob, '\n');
BUFPUTSL(ob, ".nf\n");
if (text) man_text_escape(ob, text->data, text->size);
BUFPUTSL(ob, ".fi\n");
return 1; }
static void
man_header(struct buf *ob, struct buf *text, int level, void *opaque) {
if (ob->size) bufputc(ob, '\n');
switch(level) {
case 1:
bufprintf(ob,".TH ");
break;
case 2:
bufprintf(ob, ".SH ");
break;
case 3:
bufprintf(ob, ".SS ");
break;
}
if (text) bufput(ob, text->data, text->size);
}
static int
man_double_emphasis(struct buf *ob, struct buf *text, char c, void *opaque) {
if (!text || !text->size) return 0;
BUFPUTSL(ob, "\\fB");
bufput(ob, text->data, text->size);
BUFPUTSL(ob, "\\fP");
return 1; }
static int
man_emphasis(struct buf *ob, struct buf *text, char c, void *opaque) {
if (!text || !text->size) return 0;
BUFPUTSL(ob, "\\fI");
if (text) bufput(ob, text->data, text->size);
BUFPUTSL(ob, "\\fP");
return 1; }
static int
man_linebreak(struct buf *ob, void *opaque) {
BUFPUTSL(ob, "\n.LP\n");
return 1; }
static void
man_paragraph(struct buf *ob, struct buf *text, void *opaque) {
if (ob->size) bufputc(ob, '\n');
BUFPUTSL(ob, ".TP\n");
if (text) bufput(ob, text->data, text->size);
BUFPUTSL(ob, "\n"); }
static void
man_list(struct buf *ob, struct buf *text, int flags, void *opaque) {
if (ob->size) bufputc(ob, '\n');
if (flags & MKD_LIST_ORDERED)
BUFPUTSL(ob,"\n.nr step 0 1\n");
else
BUFPUTSL(ob,".\n");
if (text) bufput(ob, text->data, text->size);
BUFPUTSL(ob, "\n"); }
static void
man_listitem(struct buf *ob, struct buf *text, int flags, void *opaque) {
if (flags & MKD_LIST_ORDERED)
BUFPUTSL(ob, ".IP \\n+[step]\n");
else
BUFPUTSL(ob, ".IP \\[bu] 2 \n");
if (text) {
while (text->size && text->data[text->size - 1] == '\n')
text->size -= 1;
bufput(ob, text->data, text->size); }
BUFPUTSL(ob, "\n"); }
static void
man_normal_text(struct buf *ob, struct buf *text, void *opaque) {
if (text) man_text_escape(ob, text->data, text->size); }
/* renderer structure */
struct mkd_renderer to_man = {
/* block-level callbacks */
man_blockcode,
NULL,
NULL,
man_header,
NULL,
man_list,
man_listitem,
man_paragraph,
/* span-level callbacks */
NULL,
man_codespan,
man_double_emphasis,
man_emphasis,
NULL,
man_linebreak,
NULL,
NULL,
NULL,
/* low-level callbacks */
NULL,
man_normal_text,
/* renderer data */
"*_",
NULL };
/*****************
* MAIN FUNCTION *
*****************/
/* main • main function, interfacing STDIO with the parser */
int
main(int argc, char **argv) {
struct buf *ib, *ob;
size_t ret;
FILE *in = stdin;
/* opening the file if given from the command line */
if (argc > 1) {
in = fopen(argv[1], "r");
if (!in) {
fprintf(stderr,"Unable to open input file \"%s\": %s\n",
argv[1], strerror(errno));
return 1; } }
/* reading everything */
ib = bufnew(READ_UNIT);
bufgrow(ib, READ_UNIT);
while ((ret = fread(ib->data + ib->size, 1,
ib->asize - ib->size, in)) > 0) {
ib->size += ret;
bufgrow(ib, ib->size + READ_UNIT); }
if (in != stdin) fclose(in);
/* performing markdown to man */
ob = bufnew(OUTPUT_UNIT);
markdown(ob, ib, &to_man);
/* writing the result to stdout */
fwrite(ob->data, 1, ob->size, stdout);
/* cleanup */
bufrelease(ib);
bufrelease(ob);
return 0; }
/* vim: set filetype=c: */