summaryrefslogtreecommitdiff
path: root/language/spanish/hyphen-spanish/eshyph-test.tex
blob: 06dcf7b876b5675ab229c5c2d1340cd0eb4d61c1 (plain)
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
% (c) Javier Bezos 2019
% License: MIT.

% Requires luatex >= 1.10

\documentclass{article}

{\catcode`\%=12 \gdef\%{%}}

% First, the customizable part
% ==========

\def\vow{aeiouáéíóú}
\def\con{bcdfghjklmnpqrstvwxyz}

\def\filter{s$}
% \def\filter{trans[aeiou]}
% \def\filter{^ps}

\def\leftfilter{}
% \def\leftfilter{\%-.\%-}  % eg, -.-
% \def\leftfilter{^.\%-}  % eg, .-
% \def\leftfilter{\%-.\%-}  % eg, -.
% \def\leftfilter{\%-[h\vow]}  % eg, -a
% \def\leftfilter{[^-][^-][^-][^-][^-][^-]+}

% ============

% \chardef\showall=0     % solo distintos
\chardef\showall=1     % distintos y coincidentes
% ==========

\newlanguage\left
\language\left
\lefthyphenmin1
\righthyphenmin1

\input{hyph-es.tex}

\newlanguage\right
\language\right
\lefthyphenmin1
\righthyphenmin1

\input{hyphen-es-base.tex}

% ==========
% Now the code to compare and show the hyphenated words
% ==========

\directlua{

  % Perhaps the main drawback of lua (and a huge one) is it doesn't
  % understand Unicode. As I'm just comparing strings with a few accented
  % letters, here is a fast dirty trick. 195 is the prefix in utf-8,
  % and I just remove it.

  function tosingle(s)
    s = s:gsub(string.char(195), '')
    return s
  end

  function test (head)
    lang.hyphenate(head)
    texio.write_nl('')
    local right = ''
    local left = ''
    for item in node.traverse(head) do
      if item.id == node.id'glyph' and item.char == 0x2a then % *
        left = right
        right = ''
      elseif item.id == node.id'glyph' then
        right = right .. unicode.utf8.char(item.char)
      elseif item.id == node.id'glue' then
        right = right .. ' '
      elseif item.id == node.id'disc' then
        right = right .. '-'
      end
    end
    % texio.write_nl(left)
    ql = tosingle(left)
    qf = tosingle('\leftfilter')
    if ql:find(qf) then
      if left == right then
        \ifcase\showall\or
          texio.write('.. ' .. left .. '   ' .. right)
        \fi
      else
        texio.write('@@ ' .. left .. '   ' .. right)
      end
    end
    return head
  end
  luatexbase.add_to_callback('hyphenate',
                             test,
                             'Hyphen.hyphenate')
  }

\begin{document}

\def\p#1{\setbox0\vbox{\language\left#1*\language\right#1}}

\message{^^J*******************^^J}

\directlua{
  local words = io.open('spanish-words.txt')
  for w in words:lines() do
    qw = tosingle(w)
    qf = tosingle('\filter')
    if qw:find(qf) then
      tex.print('\string\\p{' .. w .. '}')
    end
  end
  words:close()}
  
\message{^^J*******************^^J}

\end{document}