lang-dis.lua /size: 6054 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
lang-dis
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to lang-ini.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
concat
=
table
.
concat
10 11
local
tex
=
tex
12
local
nodes
=
nodes
13 14
local
tasks
=
nodes
.
tasks
15
local
nuts
=
nodes
.
nuts
16 17
local
enableaction
=
tasks
.
enableaction
18
local
setaction
=
tasks
.
setaction
19 20
local
setfield
=
nuts
.
setfield
21
local
getnext
=
nuts
.
getnext
22
local
getprev
=
nuts
.
getprev
23
local
getid
=
nuts
.
getid
24
local
getattr
=
nuts
.
getattr
25
local
getsubtype
=
nuts
.
getsubtype
26
local
setsubtype
=
nuts
.
setsubtype
27
local
getchar
=
nuts
.
getchar
28
local
setchar
=
nuts
.
setchar
29
local
getdisc
=
nuts
.
getdisc
30
local
setdisc
=
nuts
.
setdisc
31
local
getlang
=
nuts
.
setlang
32
local
getboth
=
nuts
.
getboth
33
local
setlist
=
nuts
.
setlist
34
local
setlink
=
nuts
.
setlink
35
local
isglyph
=
nuts
.
isglyph
36 37
local
copy_node
=
nuts
.
copy
38
local
remove_node
=
nuts
.
remove
39
local
flush_list
=
nuts
.
flush_list
40
local
flush_node
=
nuts
.
flush_node
41 42
local
nextdisc
=
nuts
.
traversers
.
disc
43 44
local
new_disc
=
nuts
.
pool
.
disc
45 46
local
nodecodes
=
nodes
.
nodecodes
47
local
disccodes
=
nodes
.
disccodes
48 49
local
disc_code
=
nodecodes
.
disc
50
local
glyph_code
=
nodecodes
.
glyph
51 52
local
explicitdisc_code
=
disccodes
.
explicit
53 54
local
a_visualize
=
attributes
.
private
(
"
visualizediscretionary
"
)
55
local
setattribute
=
tex
.
setattribute
56 57
local
getlanguagedata
=
languages
.
getdata
58
local
prehyphenchar
=
lang
.
prehyphenchar
59
local
posthyphenchar
=
lang
.
posthyphenchar
60 61
local
check_regular
=
true
62 63
local
setlistcolor
=
nodes
.
tracers
.
colors
.
setlist
64 65
function
languages
.
visualizediscretionaries
(
head
)
66
for
d
in
nextdisc
,
head
do
67
if
getattr
(
d
,
a_visualize
)
then
68
local
pre
,
post
,
replace
=
getdisc
(
d
)
69
if
pre
then
70
setlistcolor
(
pre
,
"
darkred
"
)
71
end
72
if
post
then
73
setlistcolor
(
post
,
"
darkgreen
"
)
74
end
75
if
replace
then
76
setlistcolor
(
replace
,
"
darkblue
"
)
77
end
78
end
79
end
80
return
head
81
end
82 83
local
enabled
=
false
84 85
function
languages
.
showdiscretionaries
(
v
)
86
if
v
=
=
false
then
87
setattribute
(
a_visualize
,
unsetvalue
)
88
else
-- also nil
89
if
not
enabled
then
90
enableaction
(
"
processors
"
,
"
languages.visualizediscretionaries
"
)
91
enabled
=
true
92
end
93
setattribute
(
a_visualize
,
1
)
94
end
95
end
96 97
interfaces
.
implement
{
98
name
=
"
showdiscretionaries
"
,
99
actions
=
languages
.
showdiscretionaries
100
}
101 102
do
103 104
local
toutf
=
nodes
.
listtoutf
105
local
utfchar
=
utf
.
char
106
local
f_disc
=
string
.
formatters
[
"
{%s}{%s}{%s}
"
]
107
local
replace
=
lpeg
.
replacer
(
{
108
[
utfchar
(
0x200C
)
]
=
"
|
"
,
109
[
utfchar
(
0x200D
)
]
=
"
|
"
,
110
}
,
nil
,
true
)
111 112
local
function
convert
(
list
)
113
return
list
and
replace
(
toutf
(
list
)
)
or
"
"
114
end
115 116
function
languages
.
serializediscretionary
(
d
)
-- will move to tracer
117
local
pre
,
post
,
replace
=
getdisc
(
d
)
118
return
f_disc
(
convert
(
pre
)
,
convert
(
post
)
,
convert
(
replace
)
)
119
end
120 121
end
122 123
-- --
124 125
local
wiped
=
0
126 127
local
flatten_discretionaries
=
nuts
.
flatten_discretionaries
-- todo in nodes
128 129
-- if flatten_discretionaries then
130 131
function
languages
.
flatten
(
head
)
132
local
h
,
n
=
flatten_discretionaries
(
head
)
133
wiped
=
wiped
+
n
134
return
h
,
n
>
0
135
end
136 137
-- else
138
--
139
-- local function wipe(head,delayed)
140
-- local p, n = getboth(delayed)
141
-- local _, _, h, _, _, t = getdisc(delayed,true)
142
-- if p or n then
143
-- if h then
144
-- setlink(p,h)
145
-- setlink(t,n)
146
-- setfield(delayed,"replace")
147
-- else
148
-- setlink(p,n)
149
-- end
150
-- end
151
-- if head == delayed then
152
-- head = h
153
-- end
154
-- wiped = wiped + 1
155
-- flush_node(delayed)
156
-- return head
157
-- end
158
--
159
-- function languages.flatten(head)
160
-- local delayed = nil
161
-- for d in nextdisc, head do
162
-- if delayed then
163
-- head = wipe(head,delayed)
164
-- end
165
-- delayed = d
166
-- end
167
-- if delayed then
168
-- return wipe(head,delayed), true
169
-- else
170
-- return head, false
171
-- end
172
-- end
173
--
174
-- end
175 176
function
languages
.
nofflattened
(
)
177
return
wiped
-- handy for testing
178
end
179 180
-- experiment: for now not in not in export mode!
181 182
local
flatten
=
languages
.
flatten
183 184
nodes
.
handlers
.
flattenline
=
flatten
185 186
function
nodes
.
handlers
.
flatten
(
head
,
where
)
187
if
head
and
(
where
=
=
"
box
"
or
where
=
=
"
adjusted_hbox
"
)
then
188
return
flatten
(
head
)
189
end
190
return
head
191
end
192 193
directives
.
register
(
"
hyphenator.flatten
"
,
function
(
v
)
194
-- use with care
195
setaction
(
"
processors
"
,
"
nodes.handlers.flatten
"
,
v
)
196
setaction
(
"
contributers
"
,
"
nodes.handlers.flattenline
"
,
v
)
197
end
)
198 199
-- moved here:
200 201
function
languages
.
explicithyphen
(
template
)
202
local
pre
,
post
203
local
disc
=
new_disc
(
)
204
if
template
then
205
local
langdata
=
getlanguagedata
(
getlang
(
template
)
)
206
local
instance
=
langdata
and
langdata
.
instance
207
if
instance
then
208
local
prechr
=
prehyphenchar
(
instance
)
209
local
postchr
=
posthyphenchar
(
instance
)
210
if
prechr
>
=
0
then
211
pre
=
copy_node
(
template
)
212
setchar
(
pre
,
prechr
)
213
end
214
if
postchr
>
=
0
then
215
post
=
copy_node
(
template
)
216
setchar
(
post
,
postchr
)
217
end
218
end
219
end
220
setdisc
(
disc
,
pre
,
post
,
nil
,
explicitdisc_code
,
tex
.
exhyphenpenalty
)
221
return
disc
222
end
223