aboutsummaryrefslogtreecommitdiff
path: root/arabluatex.lua
blob: 76d61b3fe56ac03d1a50cfb9b05d014bbbfa0097 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
--[[
This file is part of the `arabluatex' package

copyright (C) 2016  Robert Alessi

Please send error reports and suggestions for improvements to
Robert Alessi <alessi@robertalessi.net>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
--]]

require("arabluatex_voc")
require("arabluatex_fullvoc")
require("arabluatex_novoc")

local function protectarb(str)
   str = string.gsub(str, "(\\arb.?)(%[.-%])(%b{})", "\\@arb%2%3")
   str = string.gsub(str, "\\par", "\\p@r{}")
   str = string.gsub(str, "\\@@par", "\\p@r{}")
return str
end

local function unprotectarb(str)
   str = string.gsub(str, "(\\@arb)(%[.-%])(%b{})", "\\arb%2%3")
   str = string.gsub(str, "\\p@r{}", "\\par")
return str
end

local function breakcmd(str)
   -- \LR
   str = string.gsub(str, "\\(LR.-)(%b{})",
		function(tag, body)
		body = string.sub(body, 2, -2)
		return string.format("}\\%s{%s}\\arb{", tag, body)
		end)
   -- Footnote
   str = string.gsub(str, "\\(Footnote.-)(%b{})",
		function(tag, body)
		body = string.sub(body, 2, -2)
		return string.format("}\\%s{%s}\\arb{", tag, body)
		end)
	-- Marginpar
   str = string.gsub(str, "\\(Marginpar.-)(%b{})",
		function(tag, body)
		body = string.sub(body, 2, -2)
		return string.format("}\\%s{%s}\\arb{", tag, body)
		end)
   -- Abjad (Needs polyglossia)
   str = string.gsub(str, "\\(abjad.-)(%b{})",
	function(tag, body)
	body = string.sub(body, 2, -2)
	return string.format("}\\aemph{\\txarb{\\%s{%s}}}\\arb{", tag, body)
	end)
   return str
end

local function holdcmd(str)
	str = string.gsub(str, "\\(arb)(%b{})", function(tag, body)
	body = string.sub(body, 2, -2)
	body = string.gsub(body, "\\(.-)(%b{})", function(btag, bbody)
	bbody = string.sub(bbody, 2, -2)
	if string.find(btag, "@") then
	return holdcmd(string.format("}\\%s{%s}\\arb{", btag, bbody))	
	else
	return holdcmd(string.format("}\\%s{\\arb{%s}}\\arb{", btag, bbody))
	end
	end)
	return string.format("\\%s{%s}", tag, body)
	end)
	str = string.gsub(str, "\\arb{}", "")
return str
end

local function arbnum(str)
   str = string.gsub(str, "([0-9%,%-%/]+)", function(num)
	return string.reverse(num)
   end)
   return str
end

local function indnum(str)
   str = string.gsub(str, "([0-9%,%-%/]+)", function(num)
	return string.reverse(num)
   end)
   for i = 1,#numbers do
      str = string.gsub(str, numbers[i].a, numbers[i].b)
   end
   return str
end

local function voc(str)
   str = string.gsub(str, "\\arb(%b{})", function(inside)
   inside = string.sub(inside, 2, -2)
    for i = 1,#hamza do
		inside = string.gsub(inside, hamza[i].a, hamza[i].b)
	end
	for i = 1,#tanwin do
		inside  = string.gsub(inside, tanwin[i].a, tanwin[i].b)
	end
	for i = 1,#trigraphs do
		inside = string.gsub(inside, trigraphs[i].a, trigraphs[i].b)
	end
	for i = 1,#digraphs do
		inside = string.gsub(inside, digraphs[i].a, digraphs[i].b)
	end
	for i = 1,#single do
		inside = string.gsub(inside, single[i].a, single[i].b)
	end
	for i = 1,#longv do
		inside = string.gsub(inside, longv[i].a, longv[i].b)
	end
	for i = 1,#shortv do
		inside = string.gsub(inside, shortv[i].a, shortv[i].b)
	end
	for i = 1,#null do
		inside = string.gsub(inside, null[i].a, null[i].b)
	end
	inside = indnum(inside)
    return string.format("\\txarb{%s}", inside)
  end)
return str
end

local function fullvoc(str)
   str = string.gsub(str, "\\arb(%b{})", function(inside)
   inside = string.sub(inside, 2, -2)
    for i = 1,#hamzafv do
		inside = string.gsub(inside, hamzafv[i].a, hamzafv[i].b)
	end
	for i = 1,#tanwin do
		inside  = string.gsub(inside, tanwin[i].a, tanwin[i].b)
	end
	for i = 1,#trigraphs do
		inside = string.gsub(inside, trigraphs[i].a, trigraphs[i].b)
	end
	for i = 1,#digraphsfv do
		inside = string.gsub(inside, digraphsfv[i].a, digraphsfv[i].b)
	end
	for i = 1,#single do
		inside = string.gsub(inside, single[i].a, single[i].b)
	end
	for i = 1,#longv do
		inside = string.gsub(inside, longv[i].a, longv[i].b)
	end
	for i = 1,#shortv do
		inside = string.gsub(inside, shortv[i].a, shortv[i].b)
	end
	for i = 1,#null do
		inside = string.gsub(inside, null[i].a, null[i].b)
	end
	inside = indnum(inside)
    return string.format("\\txarb{%s}", inside)
  end)
return str
end

local function novoc(str)
   str = string.gsub(str, "\\arb(%b{})", function(inside)
   inside = string.sub(inside, 2, -2)
    for i = 1,#hamza do
		inside = string.gsub(inside, hamza[i].a, hamza[i].b)
	end
	for i = 1,#tanwinnv do
	  inside  = string.gsub(inside, tanwinnv[i].a, tanwinnv[i].b)
	end
	for i = 1,#trigraphs do
		inside = string.gsub(inside, trigraphs[i].a, trigraphs[i].b)
	end
	for i = 1,#digraphs do
		inside = string.gsub(inside, digraphs[i].a, digraphs[i].b)
	end
	for i = 1,#single do
		inside = string.gsub(inside, single[i].a, single[i].b)
	end
	for i = 1,#longvnv do
		inside = string.gsub(inside, longvnv[i].a, longvnv[i].b)
	end
	for i = 1,#shortvnv do
	  inside = string.gsub(inside, shortvnv[i].a, shortvnv[i].b)
	end
	for i = 1,#null do
		inside = string.gsub(inside, null[i].a, null[i].b)
	end
	inside = indnum(inside)
    return string.format("\\txarb{%s}", inside)
  end)
return str
end

function processvoc(str)
   str = "\\arb{".. str.."}"
   str = protectarb(str)
   str = breakcmd(str)
   str = holdcmd(str)
   str = voc(str)
   str = unprotectarb(str)
return str
end

function processfullvoc(str)
   str = "\\arb{".. str.."}"
   str = protectarb(str)
   str = breakcmd(str)
   str = holdcmd(str)
   str = fullvoc(str)
   str = unprotectarb(str)
return str
end

function processnovoc(str)
   str = "\\arb{".. str.."}"
   str = protectarb(str)
   str = breakcmd(str)
   str = holdcmd(str)
   str = novoc(str)
   str = unprotectarb(str)
return str
end

--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"))
--print(arbnum("bop 0123456789"))
--print(processvoc("daxala \\arb[novoc]{mubtasimaN}"))
--print(processvoc("\\emph{daxala} \\arb[novoc]{mufakkiruN}\\Footnote{\\arb{kataba \\arb[novoc]{mubtasimaN}} Je pense que c'est bien \\arb{kataba}} faqa.t"))
--print(processvoc("daxala \\abjad{45} mubtasimaN"))
--print(process("daxala\\Footnote{Et là c'est toujours \\arb{mubtasimaN}?"))
--print(process("\\emph{daxala}\\Footnote{texte arabe: \\arb{\\emph{kataba}}} \\textbf{\\emph{mubtasimaN}} faqa.t"))