-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
output.c
190 lines (147 loc) · 3.54 KB
/
output.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
/*
* Copyright 2014-2019 Katherine Flavel
*
* See LICENCE for the full copyright terms.
*/
/*
* Railroad Diagram HTML5 Output
*
* Output a HTML page of inlined SVG diagrams
*/
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include "../txt.h"
#include "../ast.h"
#include "../xalloc.h"
#include "../rrd/rrd.h"
#include "../rrd/pretty.h"
#include "../rrd/node.h"
#include "../rrd/rrd.h"
#include "../rrd/list.h"
#include "../rrd/tnode.h"
#include "io.h"
/* XXX */
extern struct dim svg_dim;
void svg_render_rule(const struct tnode *node, const char *base,
const struct ast_rule *grammar);
extern const char *css_file;
WARN_UNUSED_RESULT
int
cat(const char *in, const char *indent)
{
FILE *f;
char buf[BUFSIZ];
f = fopen(in, "r");
if (f == NULL) {
perror(in);
return 0;
}
fputs(indent, stdout);
for (;;) {
buf[sizeof buf - 1] = 'x';
if (!fgets(buf, sizeof buf, f)) {
break;
}
fputs(buf, stdout);
if (buf[sizeof buf - 1] != '\0' || buf[sizeof buf - 2] == '\n') {
fputs(indent, stdout);
}
}
(void) fclose(f);
return 1;
}
WARN_UNUSED_RESULT
static int
output(const struct ast_rule *grammar, int xml)
{
const struct ast_rule *p;
printf(" <head>\n");
if (xml) {
printf(" <meta charset='UTF-8'/>\n");
}
printf(" <style>\n");
printf(" rect, line, path { stroke-width: 1.5px; stroke: black; fill: transparent; }\n");
printf(" rect, line, path { stroke-linecap: square; stroke-linejoin: rounded; }\n");
printf(" path { fill: transparent; }\n");
printf(" text.literal { font-family: monospace; }\n");
printf(" a { fill: blue; }\n");
printf(" a:hover rect { fill: aliceblue; }\n");
printf(" h2 { font-size: inherit; font-weight: inherit; }\n");
printf(" line.ellipsis { stroke-dasharray: 1 3.5; }\n");
printf(" tspan.hex { font-family: monospace; font-size: 90%%; }\n");
printf(" path.arrow { fill: black; }\n");
printf(" svg { margin-left: 30px; }\n");
if (css_file != NULL) {
if (!cat(css_file, " "))
return 0;
}
printf(" </style>\n");
printf(" </head>\n");
printf("\n");
printf(" <body>\n");
for (p = grammar; p; p = p->next) {
struct tnode *tnode;
struct node *rrd;
unsigned h, w;
if (!ast_to_rrd(p, &rrd)) {
perror("ast_to_rrd");
return 0;
}
if (prettify) {
rrd_pretty(&rrd);
}
tnode = rrd_to_tnode(rrd, &svg_dim);
node_free(rrd);
printf(" <section>\n");
printf(" <h2><a name='%s'>%s:</a></h2>\n",
p->name, p->name);
h = (tnode->a + tnode->d + 1) * 10 + 5;
w = (tnode->w + 6) * 10;
printf(" <svg");
if (xml) {
printf(" xmlns='http://www.w3.org/2000/svg'");
}
printf(" height='%u' width='%u'>\n", h, w);
svg_render_rule(tnode, "", grammar);
printf(" </svg>\n");
printf(" </section>\n");
printf("\n");
tnode_free(tnode);
}
printf(" </body>\n");
return 1;
}
WARN_UNUSED_RESULT
int
html5_output(const struct ast_rule *grammar)
{
printf("<!DOCTYPE html>\n");
printf("<html>\n");
printf("\n");
if (!output(grammar, 0))
return 0;
printf("</html>\n");
return 1;
}
WARN_UNUSED_RESULT
int
xhtml5_output(const struct ast_rule *grammar)
{
printf("<?xml version='1.0' encoding='utf-8'?>\n");
printf("<!DOCTYPE html>\n");
printf("<html xml:lang='en' lang='en'\n");
printf(" xmlns='http://www.w3.org/1999/xhtml'\n");
printf(" xmlns:xlink='http://www.w3.org/1999/xlink'>\n");
printf("\n");
if (!output(grammar, 1))
return 0;
printf("</html>\n");
return 1;
}