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
|
if not modules then modules = { } end modules ['lang-url'] = {
version = 1.001,
comment = "companion to lang-url.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local utfcharacters, utfvalues, utfbyte, utfchar = utf.characters, utf.values, utf.byte, utf.char
local commands = commands
local context = context
local implement = interfaces.implement
local variables = interfaces.variables
local v_before = variables.before
local v_after = variables.after
local is_letter = characters.is_letter
--[[
<p>Hyphenating <l n='url'/>'s is somewhat tricky and a matter of taste. I did
consider using a dedicated hyphenation pattern or dealing with it by node
parsing, but the following solution suits as well. After all, we're mostly
dealing with <l n='ascii'/> characters.</p>
]]--
commands.hyphenatedurl = commands.hyphenatedurl or { }
local hyphenatedurl = commands.hyphenatedurl
local characters = utilities.storage.allocate {
["!"] = "before",
["\""] = "before",
["#"] = "before",
["$"] = "before",
["%"] = "before",
["&"] = "before",
["("] = "before",
["*"] = "before",
["+"] = "before",
[","] = "before",
["-"] = "before",
["."] = "before",
["/"] = "before",
[":"] = "before",
[";"] = "before",
["<"] = "before",
["="] = "before",
[">"] = "before",
["?"] = "before",
["@"] = "before",
["["] = "before",
["\\"] = "before",
["^"] = "before",
["_"] = "before",
["`"] = "before",
["{"] = "before",
["|"] = "before",
["~"] = "before",
["'"] = "after",
[")"] = "after",
["]"] = "after",
["}"] = "after",
}
local mapping = utilities.storage.allocate {
-- [utfchar(0xA0)] = "~", -- nbsp (catch)
}
hyphenatedurl.characters = characters
hyphenatedurl.mapping = mapping
hyphenatedurl.lefthyphenmin = 2
hyphenatedurl.righthyphenmin = 3
hyphenatedurl.discretionary = nil
-- more fun is to write nodes .. maybe it's nicer to do this
-- in an attribute handler anyway
-- local ctx_a = context.a
-- local ctx_b = context.b
-- local ctx_d = context.d
-- local ctx_n = context.n
-- local ctx_s = context.s
-- local function action(hyphenatedurl,str,left,right,disc)
-- local n = 0
-- local b = math.max( left or hyphenatedurl.lefthyphenmin, 2)
-- local e = math.min(#str-(right or hyphenatedurl.righthyphenmin)+2,#str)
-- local d = disc or hyphenatedurl.discretionary
-- local p = nil
-- for s in utfcharacters(str) do
-- n = n + 1
-- s = mapping[s] or s
-- if n > 1 then
-- ctx_s() -- can be option
-- end
-- if s == d then
-- ctx_d(utfbyte(s))
-- else
-- local c = characters[s]
-- if not c or n <= b or n >= e then
-- ctx_n(utfbyte(s))
-- elseif c == 1 then
-- ctx_b(utfbyte(s))
-- elseif c == 2 then
-- ctx_a(utfbyte(s))
-- end
-- end
-- p = s
-- end
-- end
local ctx_a = context.a
local ctx_b = context.b
local ctx_d = context.d
local ctx_c = context.c
local ctx_l = context.l
local ctx_C = context.C
local ctx_L = context.L
local function action(hyphenatedurl,str,left,right,disc)
local n = 0
local b = math.max( left or hyphenatedurl.lefthyphenmin, 2)
local e = math.min(#str-(right or hyphenatedurl.righthyphenmin)+2,#str)
local d = disc or hyphenatedurl.discretionary
local p = nil
for s in utfcharacters(str) do
n = n + 1
s = mapping[s] or s
if s == d then
ctx_d(utfbyte(s))
else
local c = characters[s]
if c == v_before then
p = false
ctx_b(utfbyte(s))
elseif c == v_after then
p = false
ctx_a(utfbyte(s))
else
local l = is_letter[s]
if n <= b or n >= e then
if p and l then
ctx_L(utfbyte(s))
else
ctx_C(utfbyte(s))
end
elseif p and l then
ctx_l(utfbyte(s))
else
ctx_c(utfbyte(s))
end
p = l
end
end
end
end
-- hyphenatedurl.action = function(_,...) action(...) end -- sort of obsolete
table.setmetatablecall(hyphenatedurl,action) -- watch out: a caller
-- todo, no interface in mkiv yet
function hyphenatedurl.setcharacters(str,value) -- 1, 2 == before, after
for s in utfcharacters(str) do
characters[s] = value or v_before
end
end
-- .hyphenatedurl.setcharacters("')]}",2)
implement {
name = "sethyphenatedurlcharacters",
actions = hyphenatedurl.setcharacters,
arguments = { "string", "string" }
}
implement {
name = "hyphenatedurl",
scope = "private",
actions = function(...) action(hyphenatedurl,...) end,
arguments = { "string", "integer", "integer", "string" }
}
|