-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathtypeset.c
229 lines (195 loc) · 4.61 KB
/
typeset.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#define MAX_FILE_SIZE (64*1024*1024)
char data[MAX_FILE_SIZE];
int data_len=0;
char token[MAX_FILE_SIZE];
char token_type[MAX_FILE_SIZE];
char word_def[MAX_FILE_SIZE];
char word_example[MAX_FILE_SIZE];
char word_syns[MAX_FILE_SIZE];
char word_type[MAX_FILE_SIZE];
int token_len=0;
int in_token=0;
FILE *o=NULL;
struct word {
char word[1024];
char body[8192];
};
#define MAX_WORDS 100000
struct word words[MAX_WORDS];
int word_count=0;
int word_started=0;
int compare_words(const void *a,const void *b)
{
const struct word *aa=a;
const struct word *bb=b;
return strcasecmp(aa->word,bb->word);
}
void word_begin(char *word)
{
if (word_started) {
word_started=0;
word_count++;
}
strcpy(words[word_count].word,word);
words[word_count].body[0]=0;
word_started=1;
}
void word_append(char *t)
{
if ((strlen(t)+strlen(words[word_count].body))<8192) {
strcpy(&words[word_count].body[strlen(words[word_count].body)],t);
}
}
void emit_definition(void)
{
char buf[8192];
snprintf(buf,8192,"$\\bullet$ \\ (%s) {%s.}\n",
word_type,word_def);
word_append(buf);
if(word_example[0]) {
snprintf(buf,8192," \\textit{``%s''}\n",word_example);
word_append(buf);
}
if (word_syns[0]) {
snprintf(buf,8192," \\textsc{Synonyms:} %s\n",word_syns);
word_append(buf);
}
}
void token_char(char c)
{
if (in_token)
token[token_len++]=c;
}
void token_start(int depth)
{
in_token=1;
token_len=0;
}
void token_end(int depth, int colonP)
{
token[token_len]=0;
if (colonP) strcpy(token_type,token);
if ((!colonP)&&(depth==3)) {
// Remember field
if (!strcmp(token_type,"def")) strcpy(word_def,token);
else if (!strcmp(token_type,"example")) strcpy(word_example,token);
else if (!strcmp(token_type,"speech_part")) strcpy(word_type,token);
else if (!strcmp(token_type,"speech\\_part")) strcpy(word_type,token);
else if (!strcmp(token_type,"synonyms")) strcpy(word_syns,token);
else {
// fprintf(stderr,"Encountered unknown token type '%s'\n",token);
// exit(-1);
}
}
// printf("'%s' (%s) @ %d, %d\n",token,token_type,depth,colonP);
if ((!colonP)&&(depth==2)&&(!strcmp(token_type,"word")))
{
word_begin(token);
char buf[8192];
snprintf(buf,8192,"\\par \\markboth{%s}{%s}\\textbf{%s}\n",
token,token,token);
word_append(buf);
}
token_len=0;
in_token=0;
}
void parse_file(FILE *f)
{
int depth=0;
word_count=0;
data_len=fread(data,1,MAX_FILE_SIZE,f);
fprintf(stderr,"%d bytes\n",data_len);
for(int i=0;i<data_len;i++) {
switch(data[i]) {
case '{':
depth++;
if (depth==3) {
word_example[0]=0;
word_type[0]=0;
word_syns[0]=0;
word_def[0]=0;
}
break;
case '}':
if (depth) depth--;
if (depth==2) {
emit_definition();
}
break;
case '$': case '_': case '%': case '^': case '@': case '#': case '&':
token_char('\\');
token_char(data[i]);
break;
case '\"':
if (data[i+1]==':') {
token_end(depth,1);
i++;
} else {
if (!token_len)
token_start(depth);
else token_end(depth,0);
}
break;
case '\\':
if (data[i+1]=='"') {
// Escaped quote
token_char(data[i+1]);
i++;
} else if (data[i+1]=='n') {
// \n
token_char('\n');
i++;
} else {
fprintf(stderr,"Illegal escape sequence \\%c\n",data[i+1]);
exit(-3);
}
break;
default:
token_char(data[i]);
break;
}
}
// Sort words and output them
qsort(words,word_count,sizeof(struct word),compare_words);
for(int i=0;i<word_count;i++) fprintf(o,"%s",words[i].body);
}
int main(int argc,char **argv)
{
DIR *d=opendir("data");
if (!d) {
fprintf(stderr,"Could not open data directory for reading.\n");
exit(-1);
}
struct dirent *de;
while((de=readdir(d))!=NULL) {
if (strlen(de->d_name)>strlen(".json")) {
char name[1024];
strcpy(name,de->d_name);
name[strlen(de->d_name)-5]=0;
char filename[1024];
snprintf(filename,1024,"data/%s",de->d_name);
FILE *f=fopen(filename,"r");
if (!f) {
fprintf(stderr,"ERROR: Could not read '%s'\n",filename);
exit(-3);
}
snprintf(filename,1024,"%s.tex",name);
o=fopen(filename,"w");
fprintf(o,
"\\section*{%s}\n"
"\\begin{multicols}{2}\n",
name);
parse_file(f);
fprintf(o,"\\end{multicols}\n");
fprintf(o,"\\clearpage\n");
fprintf(stderr,"Scanning %s\n",de->d_name);
fclose(f);
}
}
closedir(d);
}