aboutsummaryrefslogtreecommitdiff
path: root/arabluatex.lua
diff options
context:
space:
mode:
Diffstat (limited to 'arabluatex.lua')
-rw-r--r--arabluatex.lua195
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--[[
2This file is part of the `arabluatex' package
3
4copyright (C) 2016 Robert Alessi
5
6Please send error reports and suggestions for improvements to
7Robert Alessi <alessi@robertalessi.net>
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
2202111-1307, USA.
23--]]
24
25require("arabluatex_voc")
26require("arabluatex_novoc")
27
28local 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{}")
32return str
33end
34
35local function unprotectarb(str)
36 str = string.gsub(str, "(\\@arb)(%[.-%])(%b{})", "\\arb%2%3")
37 str = string.gsub(str, "\\p@r{}", "\\par")
38return str
39end
40
41local 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
67end
68
69local 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{}", "")
83return str
84end
85
86local function arbnum(str)
87 str = string.gsub(str, "([0-9%,%-%/]+)", function(num)
88 return string.reverse(num)
89 end)
90 return str
91end
92
93local 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
101end
102
103local 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)
133return str
134end
135
136local 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)
166return str
167end
168
169function 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)
176return str
177end
178
179function 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)
186return str
187end
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"))