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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
if not modules then modules = { } end modules ['mtx-patterns'] = {
version = 1.001,
comment = "companion to mtxrun.lua",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local format, find, concat, gsub, match, gmatch = string.format, string.find, table.concat, string.gsub, string.match, string.gmatch
local byte, char = utf.byte, utf.char
local addsuffix = file.addsuffix
local lpegmatch, validutf8 = lpeg.match, lpeg.patterns.validutf8
local helpinfo = [[
--convert generate context language files (mnemonic driven, if not given then all)
--check check pattern file (or those used by context when no file given)
--path source path where hyph-foo.tex files are stored
--destination destination path
examples of usage:
mtxrun --script pattern --check hyph-*.tex
mtxrun --script pattern --check --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns
mtxrun --script pattern --convert --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns/tex --destination=e:/tmp/patterns
mtxrun --script pattern --convert --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns/txt --destination=e:/tmp/patterns
]]
local application = logs.application {
name = "mtx-patterns",
banner = "ConTeXt Pattern File Management 0.20",
helpinfo = helpinfo,
}
local report = application.report
scripts = scripts or { }
scripts.patterns = scripts.patterns or { }
local permitted_characters = table.tohash {
0x0009, -- tab
0x0027, -- apostrofe
0x02BC, -- modifier apostrofe (used in greek)
0x002D, -- hyphen
0x200C, -- zwnj
0x2019, -- quote right
0x1FBD, -- greek, but no letter: symbol modifier
0x1FBF, -- greek, but no letter: symbol modifier
}
local ignored_ancient_greek = table.tohash {
0x1FD3, -- greekiotadialytikatonos (also 0x0390)
0x1FE3, -- greekupsilondialytikatonos (also 0x03B0)
0x1FBD, -- greek, but no letter: symbol modifier
0x1FBF, -- greek, but no letter: symbol modifier
0x03F2, -- greeksigmalunate
0x02BC, -- modifier apostrofe)
}
local ignored_french = table.tohash {
0x02BC, -- modifier apostrofe
}
local replaced_whatever = {
[char(0x2019)] = char(0x0027)
}
scripts.patterns.list = {
{ "af", "hyph-af", "afrikaans" },
-- { "ar", "hyph-ar", "arabic" },
-- { "as", "hyph-as", "assamese" },
{ "bg", "hyph-bg", "bulgarian" },
-- { "bn", "hyph-bn", "bengali" },
{ "ca", "hyph-ca", "catalan" },
-- { "??", "hyph-cop", "coptic" },
{ "cs", "hyph-cs", "czech" },
{ "cy", "hyph-cy", "welsh" },
{ "da", "hyph-da", "danish" },
{ "deo", "hyph-de-1901", "german, old spelling" },
{ "de", "hyph-de-1996", "german, new spelling" },
-- { "??", "hyph-de-ch-1901", "swiss german" },
-- { "??", "hyph-el-monoton", "greek" },
-- { "gr", "hyph-el-polyton", "greek" },
{ "agr", "hyph-grc", "ancient greek", ignored_ancient_greek },
{ "gb", "hyph-en-gb", "british english" },
{ "us", "hyph-en-us", "american english" },
-- { "eo", "hyph-eo", "esperanto" },
{ "es", "hyph-es", "spanish" },
{ "et", "hyph-et", "estonian" },
{ "eu", "hyph-eu", "basque" },
-- { "fa", "hyph-fa", "farsi" },
{ "fi", "hyph-fi", "finnish" },
{ "fr", "hyph-fr", "french", ignored_french },
-- { "??", "hyph-ga", "irish" },
-- { "??", "hyph-gl", "galician" },
-- { "gu", "hyph-gu", "gujarati" },
-- { "hi", "hyph-hi", "hindi" },
{ "hr", "hyph-hr", "croatian" },
-- { "??", "hyph-hsb", "upper sorbian" },
{ "hu", "hyph-hu", "hungarian" },
-- { "hy", "hyph-hy", "armenian" },
-- { "??", "hyph-ia", "interlingua" },
-- { "??", "hyph-id", "indonesian" },
{ "is", "hyph-is", "icelandic" },
{ "it", "hyph-it", "italian" },
-- { "??", "hyph-kmr", "kurmanji" },
-- { "kn", "hyph-kn", "kannada" },
{ "la", "hyph-la", "latin" },
-- { "lo", "hyph-lo", "lao" },
{ "lt", "hyph-lt", "lithuanian" },
{ "lv", "hyph-lv", "latvian" },
-- { "ml", "hyph-ml", "..." },
{ "mn", "hyph-mn-cyrl", "mongolian, cyrillic script" },
-- { "mr", "hyph-mr", "..." },
{ "nb", "hyph-nb", "norwegian bokmål" },
{ "nl", "hyph-nl", "dutch" },
{ "nn", "hyph-nn", "norwegian nynorsk" },
-- { "or", "hyph-or", "oriya" },
-- { "pa", "hyph-pa", "panjabi" },
-- { "", "hyph-", "" },
{ "pl", "hyph-pl", "polish" },
{ "pt", "hyph-pt", "portuguese" },
{ "ro", "hyph-ro", "romanian" },
{ "ru", "hyph-ru", "russian" },
-- { "sa", "hyph-sa", "sanskrit" },
{ "sk", "hyph-sk", "slovak" },
{ "sl", "hyph-sl", "slovenian" },
{ "sr", "hyph-sr-cyrl", "serbian" },
-- { "sr", "hyph-sr-latn", "serbian" },
{ "sv", "hyph-sv", "swedish" },
-- { "ta", "hyph-ta", "tamil" },
-- { "te", "hyph-te", "telugu" },
{ "tk", "hyph-tk", "turkmen" },
{ "tr", "hyph-tr", "turkish" },
{ "uk", "hyph-uk", "ukrainian" },
{ "zh", "hyph-zh-latn", "zh-latn, chinese pinyin" },
}
-- stripped down from lpeg example:
local utf = unicode.utf8
function utf.check(str)
return lpeg.match(lpeg.patterns.validutf8,str)
end
-- *.tex
-- *.hyp.txt *.pat.txt *.lic.txt *.chr.txt
function scripts.patterns.load(path,name,mnemonic,ignored)
local fullname = file.join(path,name)
local texfile = addsuffix(fullname,"tex")
local hypfile = addsuffix(fullname,"hyp.txt")
local patfile = addsuffix(fullname,"pat.txt")
local licfile = addsuffix(fullname,"lic.txt")
-- local chrfile = addsuffix(fullname,"chr.txt")
local okay = true
local hyphenations, patterns, comment, stripset = "", "", "", ""
local splitpatternsnew, splithyphenationsnew = { }, { }
local splitpatternsold, splithyphenationsold = { }, { }
local usedpatterncharactersnew, usedhyphenationcharactersnew = { }, { }
if lfs.isfile(patfile) then
report("using txt files %s.[hyp|pat|lic].txt",name)
comment, patterns, hyphenations = io.loaddata(licfile) or "", io.loaddata(patfile) or "", io.loaddata(hypfile) or ""
hypfile, patfile, licfile = hypfile, patfile, licfile
elseif lfs.isfile(texfile) then
report("using tex file %s.txt",name)
local data = io.loaddata(texfile) or ""
if data ~= "" then
data = gsub(data,"([\n\r])\\input ([^ \n\r]+)", function(previous,subname)
local subname = addsuffix(subname,"tex")
local subfull = file.join(file.dirname(texfile),subname)
local subdata = io.loaddata(subfull) or ""
if subdata == "" then
report("no subfile %s",subname)
end
return previous .. subdata
end)
data = gsub(data,"%%.-[\n\r]","")
data = gsub(data," *[\n\r]+","\n")
patterns = match(data,"\\patterns[%s]*{[%s]*(.-)[%s]*}") or ""
hyphenations = match(data,"\\hyphenation[%s]*{[%s]*(.-)[%s]*}") or ""
comment = match(data,"^(.-)[\n\r]\\patterns") or ""
else
okay = false
end
else
okay = false
end
if okay then
-- split into lines
local how = lpeg.patterns.whitespace^1
splitpatternsnew = lpeg.split(how,patterns)
splithyphenationsnew = lpeg.split(how,hyphenations)
end
if okay then
-- remove comments
local function check(data,splitdata,name)
if find(data,"%%") then
for i=1,#splitdata do
local line = splitdata[i]
if find(line,"%%") then
splitdata[i] = gsub(line,"%%.*$","")
report("removing comment: %s",line)
end
end
end
end
check(patterns,splitpatternsnew,patfile)
check(hyphenations,splithyphenationsnew,hypfile)
end
if okay then
-- remove lines with commands
local function check(data,splitdata,name)
if find(data,"\\") then
for i=1,#splitdata do
local line = splitdata[i]
if find(line,"\\") then
splitdata[i] = ""
report("removing line with command: %s",line)
end
end
end
end
check(patterns,splitpatternsnew,patfile)
check(hyphenations,splithyphenationsnew,hypfile)
end
if okay then
-- check for valid utf
local function check(data,splitdata,name)
for i=1,#splitdata do
local line = splitdata[i]
local ok = lpegmatch(validutf8,line)
if not ok then
splitdata[i] = ""
report("removing line with invalid utf: %s",line)
end
end
-- check for commands being used in comments
end
check(patterns,splitpatternsnew,patfile)
check(hyphenations,splithyphenationsnew,hypfile)
end
if okay then
-- remove funny lines
local cd = characters.data
local stripped = { }
local function check(splitdata,special,name)
local used = { }
for i=1,#splitdata do
local line = splitdata[i]
for b in line:utfvalues() do -- could be an lpeg
if b == special then
-- not registered
elseif permitted_characters[b] then
used[char(b)] = true
else
local cdb = cd[b]
if not cdb then
report("no entry in chardata for character %s (0x%04X)",char(b),b)
else
local ct = cdb.category
if ct == "lu" or ct == "ll" then
used[char(b)] = true
elseif ct == "nd" then
-- number
else
report("removing line with suspected utf character %s (0x%04X), category %s: %s",char(b),b,ct,line)
splitdata[i] = ""
break
end
end
end
end
end
return used
end
usedpatterncharactersnew = check(splitpatternsnew,byte("."))
usedhyphenationcharactersnew = check(splithyphenationsnew,byte("-"))
for k, v in next, stripped do
report("entries that contain character %s (0x%04X) have been omitted",char(k),k)
end
end
if okay then
local function stripped(what,ignored)
-- ignored (per language)
local p = nil
if ignored then
for k, v in next, ignored do
if p then
p = p + lpeg.P(char(k))
else
p = lpeg.P(char(k))
end
end
p = lpeg.P{ p + 1 * lpeg.V(1) } -- anywhere
end
-- replaced (all languages)
local r = nil
for k, v in next, replaced_whatever do
if r then
r = r + lpeg.P(k)/v
else
r = lpeg.P(k)/v
end
end
r = lpeg.Cs((r + 1)^0)
local result = { }
for i=1,#what do
local line = what[i]
if p and lpegmatch(p,line) then
report("discarding conflicting pattern: %s",line)
else -- we can speed this up by testing for replacements in the string
local l = lpegmatch(r,line)
if l ~= line then
report("sanitizing pattern: %s -> %s (for old patterns)",line,l)
end
result[#result+1] = l
end
end
return result
end
splitpatternsold = stripped(splitpatternsnew,ignored)
splithyphenationsold = stripped(splithyphenationsnew,ignored)
end
if okay then
-- discarding duplicates
local function check(data,splitdata,name)
local used, collected = { }, { }
for i=1,#splitdata do
local line = splitdata[i]
if line == "" then
-- discard
elseif used[line] then
-- discard
report("discarding duplicate pattern: %s",line)
else
used[line] = true
collected[#collected+1] = line
end
end
return collected
end
splitpatternsnew = check(patterns,splitpatternsnew,patfile)
splithyphenationsnew = check(hyphenations,splithyphenationsnew,hypfile)
splitpatternsold = check(patterns,splitpatternsold,patfile)
splithyphenationsold = check(hyphenations,splithyphenationsold,hypfile)
end
if not okay then
report("no valid file %s.*",name)
end
local function getused(t)
local u = { }
for k, v in next, t do
if ignored and ignored[k] then
elseif replaced_whatever[k] then
else
u[k] = v
end
end
return u
end
local usedpatterncharactersold = getused(usedpatterncharactersnew)
local usedhyphenationcharactersold = getused(usedhyphenationcharactersnew)
return okay,
splitpatternsnew, splithyphenationsnew, splitpatternsold, splithyphenationsold, comment, stripset,
usedpatterncharactersnew, usedhyphenationcharactersnew, usedpatterncharactersold, usedhyphenationcharactersold
end
function scripts.patterns.save(destination,mnemonic,name,patternsnew,hyphenationsnew,patternsold,hyphenationsold,comment,stripped,
pusednew,husednew,pusedold,husedold,ignored)
local nofpatternsnew, nofhyphenationsnew = #patternsnew, #hyphenationsnew
local nofpatternsold, nofhyphenationsold = #patternsold, #hyphenationsold
report("language %s has %s old and %s new patterns and %s old and %s new exceptions",mnemonic,nofpatternsold,nofpatternsnew,nofhyphenationsold,nofhyphenationsnew)
if mnemonic ~= "??" then
local punew = concat(table.sortedkeys(pusednew), " ")
local hunew = concat(table.sortedkeys(husednew), " ")
local puold = concat(table.sortedkeys(pusedold), " ")
local huold = concat(table.sortedkeys(husedold), " ")
local rmefile = file.join(destination,"lang-"..mnemonic..".rme")
local patfile = file.join(destination,"lang-"..mnemonic..".pat")
local hypfile = file.join(destination,"lang-"..mnemonic..".hyp")
local luafile = file.join(destination,"lang-"..mnemonic..".lua") -- suffix might change to llg
local topline = "% generated by mtxrun --script pattern --convert"
local banner = "% for comment and copyright, see " .. file.basename(rmefile)
report("saving language data for %s",mnemonic)
if not comment or comment == "" then comment = "% no comment" end
if not type(destination) == "string" then destination = "." end
local lines = string.splitlines(comment)
for i=1,#lines do
if not find(lines[i],"^%%") then
lines[i] = "% " .. lines[i]
end
end
local metadata = {
-- texcomment = comment,
texcomment = concat(lines,"\n"),
source = name,
mnemonic = mnemonic,
}
local patterndata, hyphenationdata
if nofpatternsnew > 0 then
patterndata = {
n = nofpatternsnew,
data = concat(patternsnew," ") or nil,
characters = concat(table.sortedkeys(pusednew),""),
minhyphenmin = 1, -- determined by pattern author
minhyphenmax = 1, -- determined by pattern author
}
else
patterndata = {
n = 0,
}
end
if nofhyphenationsnew > 0 then
hyphenationdata = {
n = nofhyphenationsnew,
data = concat(hyphenationsnew," "),
characters = concat(table.sortedkeys(husednew),""),
}
else
hyphenationdata = {
n = 0,
}
end
local data = {
-- a prelude to language goodies, like we have font goodies and in
-- mkiv we can use this file directly
version = "1.001",
comment = topline,
metadata = metadata,
patterns = patterndata,
exceptions = hyphenationdata,
}
os.remove(rmefile)
os.remove(patfile)
os.remove(hypfile)
os.remove(luafile)
io.savedata(rmefile,format("%s\n\n%s",topline,comment))
io.savedata(patfile,format("%s\n\n%s\n\n%% used: %s\n\n\\patterns{\n%s}",topline,banner,puold,concat(patternsold,"\n")))
io.savedata(hypfile,format("%s\n\n%s\n\n%% used: %s\n\n\\hyphenation{\n%s}",topline,banner,huold,concat(hyphenationsold,"\n")))
io.savedata(luafile,table.serialize(data,true))
end
end
function scripts.patterns.prepare()
dofile(resolvers.findfile("char-def.lua"))
end
function scripts.patterns.check()
local path = environment.argument("path") or "."
local files = environment.files
local only = false
if #files > 0 then
only = table.tohash(files)
end
for k, v in next, scripts.patterns.list do
local mnemonic, name, ignored = v[1], v[2], v[4]
if not only or only[mnemonic] then
report("checking language %s, file %s", mnemonic, name)
local okay = scripts.patterns.load(path,name,mnemonic,ignored)
if not okay then
report("there are errors that need to be fixed")
end
report()
end
end
end
function scripts.patterns.convert()
local path = environment.argument("path") or "."
if path == "" then
report("provide sourcepath using --path ")
else
local destination = environment.argument("destination") or "."
if path == destination then
resport("source path and destination path should differ (use --path and/or --destination)")
else
local files = environment.files
local only = false
if #files > 0 then
only = table.tohash(files)
end
for k, v in next, scripts.patterns.list do
local mnemonic, name, ignored = v[1], v[2], v[4]
if not only or only[mnemonic] then
report("converting language %s, file %s", mnemonic, name)
local okay, patternsnew, hyphenationsnew, patternsold, hyphenationsold, comment, stripped,
pusednew, husednew, pusedold, husedold = scripts.patterns.load(path,name,mnemonic,ignored)
if okay then
scripts.patterns.save(destination,mnemonic,name,patternsnew,hyphenationsnew,patternsold,hyphenationsold,comment,stripped,
pusednew,husednew,pusedold,husedold,ignored)
else
report("convertion aborted due to error(s)")
end
report()
end
end
end
end
end
if environment.argument("check") then
scripts.patterns.prepare()
scripts.patterns.check()
elseif environment.argument("convert") then
scripts.patterns.prepare()
scripts.patterns.convert()
else
application.help()
end
-- mtxrun --script pattern --check hyph-*.tex
-- mtxrun --script pattern --check --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns
-- mtxrun --script pattern --convert --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns/tex --destination=e:/tmp/patterns
-- mtxrun --script pattern --convert --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns/txt --destination=e:/tmp/patterns
|