lang-url.lua /size: 6423 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
lang-url
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to lang-url.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
local
utfcharacters
,
utfbyte
,
utfchar
=
utf
.
characters
,
utf
.
byte
,
utf
.
char
10
local
min
,
max
=
math
.
min
,
math
.
max
11 12
local
context
=
context
13 14
local
implement
=
interfaces
.
implement
15
local
variables
=
interfaces
.
variables
16 17
local
v_before
=
variables
.
before
18
local
v_after
=
variables
.
after
19 20
local
is_letter
=
characters
.
is_letter
21 22
--[[ 23<p>Hyphenating <l n='url'/>'s is somewhat tricky and a matter of taste. I did 24consider using a dedicated hyphenation pattern or dealing with it by node 25parsing, but the following solution suits as well. After all, we're mostly 26dealing with <l n='ascii'/> characters.</p> 27]]
--
28 29
local
urls
=
{
}
30
languages
.
urls
=
urls
31 32
local
characters
=
utilities
.
storage
.
allocate
{
33
[
"
!
"
]
=
"
before
"
,
34
[
'
"
'
]
=
"
before
"
,
35
[
"
#
"
]
=
"
before
"
,
36
[
"
$
"
]
=
"
before
"
,
37
[
"
%
"
]
=
"
before
"
,
38
[
"
&
"
]
=
"
before
"
,
39
[
"
(
"
]
=
"
before
"
,
40
[
"
*
"
]
=
"
before
"
,
41
[
"
+
"
]
=
"
before
"
,
42
[
"
,
"
]
=
"
before
"
,
43
[
"
-
"
]
=
"
before
"
,
44
[
"
.
"
]
=
"
before
"
,
45
[
"
/
"
]
=
"
before
"
,
46
[
"
:
"
]
=
"
before
"
,
47
[
"
;
"
]
=
"
before
"
,
48
[
"
<
"
]
=
"
before
"
,
49
[
"
=
"
]
=
"
before
"
,
50
[
"
>
"
]
=
"
before
"
,
51
[
"
?
"
]
=
"
before
"
,
52
[
"
@
"
]
=
"
before
"
,
53
[
"
[
"
]
=
"
before
"
,
54
[
"
\\
"
]
=
"
before
"
,
55
[
"
^
"
]
=
"
before
"
,
56
[
"
_
"
]
=
"
before
"
,
57
[
"
`
"
]
=
"
before
"
,
58
[
"
{
"
]
=
"
before
"
,
59
[
"
|
"
]
=
"
before
"
,
60
[
"
~
"
]
=
"
before
"
,
61 62
[
"
'
"
]
=
"
after
"
,
63
[
"
)
"
]
=
"
after
"
,
64
[
"
]
"
]
=
"
after
"
,
65
[
"
}
"
]
=
"
after
"
,
66
}
67 68
local
mapping
=
utilities
.
storage
.
allocate
{
69
-- [utfchar(0xA0)] = "~", -- nbsp (catch)
70
}
71 72
urls
.
characters
=
characters
73
urls
.
mapping
=
mapping
74
urls
.
lefthyphenmin
=
2
75
urls
.
righthyphenmin
=
3
76
urls
.
discretionary
=
nil
77
urls
.
packslashes
=
false
78 79
directives
.
register
(
"
hyphenators.urls.packslashes
"
,
function
(
v
)
urls
.
packslashes
=
v
end
)
80 81
local
trace
=
false
trackers
.
register
(
"
hyphenators.urls
"
,
function
(
v
)
trace
=
v
end
)
82
local
report
=
logs
.
reporter
(
"
hyphenators
"
,
"
urls
"
)
83 84
-- local ctx_a = context.a
85
-- local ctx_b = context.b
86
-- local ctx_d = context.d
87
-- local ctx_c = context.c
88
-- local ctx_l = context.l
89
-- local ctx_C = context.C
90
-- local ctx_L = context.L
91 92
-- local function action(hyphenatedurl,str,left,right,disc)
93
-- --
94
-- left = max( left or urls.lefthyphenmin, 2)
95
-- right = min(#str-(right or urls.righthyphenmin)+2,#str)
96
-- disc = disc or urls.discretionary
97
-- --
98
-- local word = nil
99
-- local prev = nil
100
-- local pack = urls.packslashes
101
-- local length = 0
102
-- --
103
-- for char in utfcharacters(str) do
104
-- length = length + 1
105
-- char = mapping[char] or char
106
-- local b = utfbyte(char)
107
-- if prev == char and prev == "/" then
108
-- ctx_c(b)
109
-- elseif char == disc then
110
-- ctx_d()
111
-- else
112
-- if prev == "/" then
113
-- ctx_d()
114
-- end
115
-- local how = characters[char]
116
-- if how == v_before then
117
-- word = false
118
-- ctx_b(b)
119
-- elseif how == v_after then
120
-- word = false
121
-- ctx_a(b)
122
-- else
123
-- local letter = is_letter[char]
124
-- if length <= left or length >= right then
125
-- if word and letter then
126
-- ctx_L(b)
127
-- else
128
-- ctx_C(b)
129
-- end
130
-- elseif word and letter then
131
-- ctx_l(b)
132
-- else
133
-- ctx_c(b)
134
-- end
135
-- word = letter
136
-- end
137
-- end
138
-- if pack then
139
-- prev = char
140
-- else
141
-- prev = nil
142
-- end
143
-- end
144
-- end
145 146
local
function
action
(
hyphenatedurl
,
str
,
left
,
right
,
disc
)
147
--
148
left
=
max
(
left
or
urls
.
lefthyphenmin
,
2
)
149
right
=
min
(
#
str
-
(
right
or
urls
.
righthyphenmin
)
+
2
,
#
str
)
150
disc
=
disc
or
urls
.
discretionary
151
--
152
local
word
=
nil
153
local
pack
=
urls
.
packslashes
154
local
length
=
0
155
local
list
=
utf
.
split
(
str
)
156
local
size
=
#
list
157
local
prev
=
nil
158 159
for
i
=
1
,
size
do
160
local
what
=
nil
161
local
dodi
=
false
162
local
char
=
list
[
i
]
163
length
=
length
+
1
164
char
=
mapping
[
char
]
or
char
165
if
char
=
=
disc
then
166
dodi
=
true
167
elseif
pack
and
char
=
=
"
/
"
and
(
list
[
i
+
1
]
=
=
"
/
"
or
prev
=
=
"
/
"
)
then
168
what
=
"
c
"
169
else
170
local
how
=
characters
[
char
]
171
if
how
=
=
v_before
then
172
what
=
"
b
"
173
elseif
how
=
=
v_after
then
174
word
=
false
175
what
=
"
a
"
176
else
177
local
letter
=
is_letter
[
char
]
178
if
length
<
=
left
or
length
>
=
right
then
179
if
word
and
letter
then
180
what
=
"
L
"
181
else
182
what
=
"
C
"
183
end
184
elseif
word
and
letter
then
185
what
=
"
l
"
186
else
187
what
=
"
c
"
188
end
189
word
=
letter
190
end
191
end
192
if
dodi
then
193
list
[
i
]
=
"
\\d
"
194
else
195
list
[
i
]
=
"
\\
"
.
.
what
.
.
"
{
"
.
.
utfbyte
(
char
)
.
.
"
}
"
196
end
197
prev
=
char
198
end
199
if
trace
then
200
report
(
"
old : %s
"
,
str
)
201
report
(
"
new : %t
"
,
list
)
202
end
203
context
(
"
%t
"
,
list
)
204
end
205 206
-- urls.action = function(_,...) action(...) end -- sort of obsolete
207 208
table
.
setmetatablecall
(
hyphenatedurl
,
action
)
-- watch out: a caller
209 210
-- todo, no interface in mkiv yet
211 212
function
urls
.
setcharacters
(
str
,
value
)
-- 1, 2 == before, after
213
for
s
in
utfcharacters
(
str
)
do
214
characters
[
s
]
=
value
or
v_before
215
end
216
end
217 218
-- .urls.setcharacters("')]}",2)
219 220
implement
{
221
name
=
"
sethyphenatedurlcharacters
"
,
222
actions
=
urls
.
setcharacters
,
223
arguments
=
"
2 strings
"
,
224
}
225 226
implement
{
227
name
=
"
hyphenatedurl
"
,
228
scope
=
"
private
"
,
229
actions
=
function
(
...
)
action
(
hyphenatedurl
,
...
)
end
,
230
arguments
=
{
"
string
"
,
"
integer
"
,
"
integer
"
,
"
string
"
}
231
}
232