diff options
Diffstat (limited to 'arabluatex.lua')
-rw-r--r-- | arabluatex.lua | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/arabluatex.lua b/arabluatex.lua new file mode 100644 index 0000000..087ee82 --- /dev/null +++ b/arabluatex.lua | |||
@@ -0,0 +1,195 @@ | |||
1 | --[[ | ||
2 | This file is part of the `arabluatex' package | ||
3 | |||
4 | copyright (C) 2016 Robert Alessi | ||
5 | |||
6 | Please send error reports and suggestions for improvements to | ||
7 | Robert Alessi <alessi@robertalessi.net> | ||
8 | |||
9 | This program is free software; you can redistribute it and/or | ||
10 | modify it under the terms of the GNU General Public License | ||
11 | as published by the Free Software Foundation; either version 2 | ||
12 | of the License, or (at your option) any later version. | ||
13 | |||
14 | This program is distributed in the hope that it will be useful, | ||
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | GNU General Public License for more details. | ||
18 | |||
19 | You should have received a copy of the GNU General Public License | ||
20 | along with this program; if not, write to the Free Software | ||
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
22 | 02111-1307, USA. | ||
23 | --]] | ||
24 | |||
25 | require("arabluatex_voc") | ||
26 | require("arabluatex_novoc") | ||
27 | |||
28 | local function protectarb(str) | ||
29 | str = string.gsub(str, "(\\arb.?)(%[.-%])(%b{})", "\\@arb%2%3") | ||
30 | str = string.gsub(str, "\\par", "\\p@r{}") | ||
31 | str = string.gsub(str, "\\@@par", "\\p@r{}") | ||
32 | return str | ||
33 | end | ||
34 | |||
35 | local function unprotectarb(str) | ||
36 | str = string.gsub(str, "(\\@arb)(%[.-%])(%b{})", "\\arb%2%3") | ||
37 | str = string.gsub(str, "\\p@r{}", "\\par") | ||
38 | return str | ||
39 | end | ||
40 | |||
41 | local function breakcmd(str) | ||
42 | -- \LR | ||
43 | str = string.gsub(str, "\\(LR.-)(%b{})", | ||
44 | function(tag, body) | ||
45 | body = string.sub(body, 2, -2) | ||
46 | return string.format("}\\%s{%s}\\arb{", tag, body) | ||
47 | end) | ||
48 | -- Footnote | ||
49 | str = string.gsub(str, "\\(Footnote.-)(%b{})", | ||
50 | function(tag, body) | ||
51 | body = string.sub(body, 2, -2) | ||
52 | return string.format("}\\%s{%s}\\arb{", tag, body) | ||
53 | end) | ||
54 | -- Marginpar | ||
55 | str = string.gsub(str, "\\(Marginpar.-)(%b{})", | ||
56 | function(tag, body) | ||
57 | body = string.sub(body, 2, -2) | ||
58 | return string.format("}\\%s{%s}\\arb{", tag, body) | ||
59 | end) | ||
60 | -- Abjad (Needs polyglossia) | ||
61 | str = string.gsub(str, "\\(abjad.-)(%b{})", | ||
62 | function(tag, body) | ||
63 | body = string.sub(body, 2, -2) | ||
64 | return string.format("}\\aemph{\\txarb{\\%s{%s}}}\\arb{", tag, body) | ||
65 | end) | ||
66 | return str | ||
67 | end | ||
68 | |||
69 | local function holdcmd(str) | ||
70 | str = string.gsub(str, "\\(arb)(%b{})", function(tag, body) | ||
71 | body = string.sub(body, 2, -2) | ||
72 | body = string.gsub(body, "\\(.-)(%b{})", function(btag, bbody) | ||
73 | bbody = string.sub(bbody, 2, -2) | ||
74 | if string.find(btag, "@") then | ||
75 | return holdcmd(string.format("}\\%s{%s}\\arb{", btag, bbody)) | ||
76 | else | ||
77 | return holdcmd(string.format("}\\%s{\\arb{%s}}\\arb{", btag, bbody)) | ||
78 | end | ||
79 | end) | ||
80 | return string.format("\\%s{%s}", tag, body) | ||
81 | end) | ||
82 | str = string.gsub(str, "\\arb{}", "") | ||
83 | return str | ||
84 | end | ||
85 | |||
86 | local function arbnum(str) | ||
87 | str = string.gsub(str, "([0-9%,%-%/]+)", function(num) | ||
88 | return string.reverse(num) | ||
89 | end) | ||
90 | return str | ||
91 | end | ||
92 | |||
93 | local function indnum(str) | ||
94 | str = string.gsub(str, "([0-9%,%-%/]+)", function(num) | ||
95 | return string.reverse(num) | ||
96 | end) | ||
97 | for i = 1,#numbers do | ||
98 | str = string.gsub(str, numbers[i].a, numbers[i].b) | ||
99 | end | ||
100 | return str | ||
101 | end | ||
102 | |||
103 | local function voc(str) | ||
104 | str = string.gsub(str, "\\arb(%b{})", function(inside) | ||
105 | inside = string.sub(inside, 2, -2) | ||
106 | for i = 1,#hamza do | ||
107 | inside = string.gsub(inside, hamza[i].a, hamza[i].b) | ||
108 | end | ||
109 | for i = 1,#tanwin do | ||
110 | inside = string.gsub(inside, tanwin[i].a, tanwin[i].b) | ||
111 | end | ||
112 | for i = 1,#trigraphs do | ||
113 | inside = string.gsub(inside, trigraphs[i].a, trigraphs[i].b) | ||
114 | end | ||
115 | for i = 1,#digraphs do | ||
116 | inside = string.gsub(inside, digraphs[i].a, digraphs[i].b) | ||
117 | end | ||
118 | for i = 1,#single do | ||
119 | inside = string.gsub(inside, single[i].a, single[i].b) | ||
120 | end | ||
121 | for i = 1,#longv do | ||
122 | inside = string.gsub(inside, longv[i].a, longv[i].b) | ||
123 | end | ||
124 | for i = 1,#shortv do | ||
125 | inside = string.gsub(inside, shortv[i].a, shortv[i].b) | ||
126 | end | ||
127 | for i = 1,#null do | ||
128 | inside = string.gsub(inside, null[i].a, null[i].b) | ||
129 | end | ||
130 | inside = indnum(inside) | ||
131 | return string.format("\\txarb{%s}", inside) | ||
132 | end) | ||
133 | return str | ||
134 | end | ||
135 | |||
136 | local function novoc(str) | ||
137 | str = string.gsub(str, "\\arb(%b{})", function(inside) | ||
138 | inside = string.sub(inside, 2, -2) | ||
139 | for i = 1,#hamza do | ||
140 | inside = string.gsub(inside, hamza[i].a, hamza[i].b) | ||
141 | end | ||
142 | for i = 1,#tanwinnovoc do | ||
143 | inside = string.gsub(inside, tanwinnovoc[i].a, tanwinnovoc[i].b) | ||
144 | end | ||
145 | for i = 1,#trigraphs do | ||
146 | inside = string.gsub(inside, trigraphs[i].a, trigraphs[i].b) | ||
147 | end | ||
148 | for i = 1,#digraphs do | ||
149 | inside = string.gsub(inside, digraphs[i].a, digraphs[i].b) | ||
150 | end | ||
151 | for i = 1,#single do | ||
152 | inside = string.gsub(inside, single[i].a, single[i].b) | ||
153 | end | ||
154 | for i = 1,#longvnovoc do | ||
155 | inside = string.gsub(inside, longvnovoc[i].a, longvnovoc[i].b) | ||
156 | end | ||
157 | for i = 1,#shortvnovoc do | ||
158 | inside = string.gsub(inside, shortvnovoc[i].a, shortvnovoc[i].b) | ||
159 | end | ||
160 | for i = 1,#null do | ||
161 | inside = string.gsub(inside, null[i].a, null[i].b) | ||
162 | end | ||
163 | inside = indnum(inside) | ||
164 | return string.format("\\txarb{%s}", inside) | ||
165 | end) | ||
166 | return str | ||
167 | end | ||
168 | |||
169 | function processvoc(str) | ||
170 | str = "\\arb{".. str.."}" | ||
171 | str = protectarb(str) | ||
172 | str = breakcmd(str) | ||
173 | str = holdcmd(str) | ||
174 | str = voc(str) | ||
175 | str = unprotectarb(str) | ||
176 | return str | ||
177 | end | ||
178 | |||
179 | function processnovoc(str) | ||
180 | str = "\\arb{".. str.."}" | ||
181 | str = protectarb(str) | ||
182 | str = breakcmd(str) | ||
183 | str = holdcmd(str) | ||
184 | str = novoc(str) | ||
185 | str = unprotectarb(str) | ||
186 | return str | ||
187 | end | ||
188 | |||
189 | --print(processvoc("'at_A .sadIquN 'il_A ju.hA ya.tlubu min-hu .himAra-hu li-yarkiba-hu fI safraTiN qa.sIraTiN wa-qAla la-hu sawfa 'u`Idu-hu 'ilay-ka fI 'l-masA'i wa-'adfa`u la-ka 'ujraTaN 'at_A .sadIquN 'il_A ju.hA ya.tlubu min-hu .himAra-hu li-yarkiba-hu fI safraTiN qa.sIraTiN wa-qAla la-hu sawfa 'u`Idu-hu 'ilay-ka fI 'l-masA'i wa-'adfa`u la-ka 'ujraTaN")) | ||
190 | --print(arbnum("bop 0123456789")) | ||
191 | --print(processvoc("daxala \\arb[novoc]{mubtasimaN}")) | ||
192 | --print(processvoc("\\emph{daxala} \\arb[novoc]{mufakkiruN}\\Footnote{\\arb{kataba \\arb[novoc]{mubtasimaN}} Je pense que c'est bien \\arb{kataba}} faqa.t")) | ||
193 | --print(processvoc("daxala \\abjad{45} mubtasimaN")) | ||
194 | --print(process("daxala\\Footnote{Et là c'est toujours \\arb{mubtasimaN}?")) | ||
195 | --print(process("\\emph{daxala}\\Footnote{texte arabe: \\arb{\\emph{kataba}}} \\textbf{\\emph{mubtasimaN}} faqa.t")) | ||