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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <hyphen.h>
#include "config.h"
extern char *dictfile;
/* Read a tag into a character array and return its length. */
int
readtag(char *tag, FILE *in, FILE *out)
{
char ch;
int i = 0;
/* Data after a space in a tag is irrelevant. */
while ((ch = fgetc(in)) != '>' && ch != ' ') {
fputc(ch, out);
tag[i] = ch;
++i;
}
if (ch == ' ') {
/* Seek to the end of the tag. */
while (ch != '>'
&& ch != EOF) {
fputc(ch, out);
ch = fgetc(in);
}
if (ch == EOF) {
return -1;
}
fputc(ch, out);
}
else
fputc(ch, out);
tag[i] = '\0';
return i;
}
int
cmptag(char *tag2, FILE *in, FILE *out)
{
char tag1[MAXWLEN];
readtag(tag1, in, out);
return strcmp(tag1, tag2);
}
int
checktag(char *tag, int tagamt, FILE *in, FILE *out)
{
int i;
for (i = 0; i < tagamt; ++i)
if (strcmp(tag, taglist[i]) == 0)
return i;
return -1;
}
/* Check if a character should be skipped. */
int
checkskip(char ch, int skiplen)
{
int i;
for (i = 0; i < skiplen; ++i)
if (ch == skip[i])
return 1;
return 0;
}
const char *punct = "';.,\"!?:";
/* Check if a character is punctuation. */
int
checkpunct(char ch)
{
int i;
for (i = 0; punct[i] != '\0'; ++i)
if (ch == punct[i])
return 1;
return 0;
}
const char *blank = " \n\r\t";
/* Check if a character is a blank. */
int
checkblank(char ch)
{
int i;
for (i = 0; blank[i] != '\0'; ++i)
if (ch == blank[i])
return 1;
return 0;
}
/* Loop until the body is found. */
int
findbody(FILE *in, FILE *out)
{
char ch;
while ((ch = fgetc(in)) != EOF) {
fputc(ch, out);
if (ch == '<'
&& cmptag("body", in, out) == 0)
return 1;
}
return 0;
}
/* Hyphenate a word, by means of hyphen library.
This is done so as to leverage sufficient hyphenation
patterns, with the ones used here having been taken
from those developed for TeX. */
void
hypword(char *word, int len, FILE *in, FILE *out, HyphenDict *dict)
{
if (len < MINWLEN) {
fprintf(out, "%s", word);
return;
}
char *hyphens = calloc(len + 6, sizeof(char));
char *hyphword = calloc(len << 1, sizeof(char));
char **rep = NULL;
int *pos = NULL, *cut = NULL;
hnj_hyphen_hyphenate2(dict, word, len, hyphens,
hyphword, &rep, &pos, &cut);
/* fprintf(stderr, "%s\n%s\n%s\n", word, hyphens, hyphword); */
/* Process the given hyphenation. */
int i;
char oldch = ' ', oldoldch = '\0';
for (i = 0; i < strlen(hyphword); ++i) {
if (hyphword[i] == '=' && oldch != ' '
&& oldoldch != ' ')
fputs("­", out);
else if (hyphword[i] != '=')
fputc(hyphword[i], out);
oldoldch = oldch;
oldch = hyphword[i];
}
free(hyphens);
free(hyphword);
}
/* Hyphenate the words within a tag. */
void
hyptag(FILE *in, FILE *out, int skiplen, char *tag, HyphenDict *dict)
{
char ch, word[MAXWLEN], term[MAXWLEN] = "/";
int i = 0;
strcat(term, tag);
while ((ch = fgetc(in)) != EOF) {
if (i < 0) {
fputc(ch, out);
++i;
if (checkskip(ch, skiplen)) i -= 3;
continue;
}
if (checkblank(ch)) {
word[i] = '\0';
hypword(word, i, in, out, dict);
i = 0;
}
if (checkpunct(ch)) {
word[i] = '\0';
hypword(word, i, in, out, dict);
fputc(ch, out);
i = 0;
}
else if (checkskip(ch, skiplen)) {
word[i] = '\0';
fputs(word, out);
fputc(ch, out);
/* A simple way of working around
HTML character codes. Each is 5 ( epsiv )
or 6 ( hellip ) characters long, plus '&' and ';'. */
i = -3;
}
/* Check for closing tag. */
else if (ch == '<') {
word[i] = ch;
++i;
word[i] = '\0';
hypword(word, i, in, out, dict);
i = 0;
readtag(word, in, out);
if (strcmp(word, term) == 0) break;
}
else {
word[i] = ch;
++i;
}
if (i == MAXWLEN - 1) {
word[i] = '\0';
hypword(word, i, in, out, dict);
i = 0;
}
}
}
/* Hyphenate HTML input via `­'.
hyp [in] [out] */
int
main(int argc, char **argv)
{
FILE *in;
if (argc < 2)
in = stdin;
else {
in = fopen(argv[1], "r");
if (in == NULL) {
printf("%s %s\n", argv[1], "inaccessible.");
return 1;
}
}
FILE *out;
if (argc < 3)
out = stdout;
else {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("%s %s\n", argv[2], "inaccessible.");
return 2;
}
}
if (findbody(in, out) == 0) {
puts("There is no body.");
return 3;
}
HyphenDict *dict = hnj_hyphen_load(dictfile);
if (dict == NULL) {
puts("Dict not readable.");
return 4;
}
dict->utf8 = 1;
int tagamt = 0;
while (taglist[tagamt][0] != '\0')
++tagamt;
int skiplen = strlen(skip);
char ch, tag[MAXWLEN];
int len;
while ((ch = fgetc(in)) != EOF) {
fputc(ch, out);
if (ch == '<' && (len = readtag(tag, in, out)) > 0
&& checktag(tag, tagamt, in, out) != -1)
hyptag(in, out, skiplen, tag, dict);
}
return 0;
}
|