summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2018-08-27 21:40:31 +0000
committerKarl Berry <karl@freefriends.org>2018-08-27 21:40:31 +0000
commitf1c2e4e8b45c0e7bbdaca6e345c187375d024f6a (patch)
tree860c73f354a94aaca03662a2cddce04ff0063f46 /Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua
parentc357ace7563241fac62d6f2a73fc3a1521e628b2 (diff)
arabluatex (27aug18)
git-svn-id: svn://tug.org/texlive/trunk@48499 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua')
-rw-r--r--Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua288
1 files changed, 278 insertions, 10 deletions
diff --git a/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua b/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua
index 823823621c1..4db23a295ed 100644
--- a/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua
+++ b/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua
@@ -49,10 +49,12 @@ local cmdstar = lpeg.Cs(spce * lpeg.P("*"))
local bsqbracketsii = lpeg.Cs(bsqbrackets^-2)
local bcbracesii = lpeg.Cs(bcbraces^-2)
local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1)
-local cmdargs = lpeg.Cs(spce * bsqbracketsii * bcbracesii * bsqbrackets^-1)
+local cmdargs = lpeg.Cs(spce^-1 * bsqbracketsii * bcbracesii * bsqbrackets^-1)
+local arbargs = lpeg.Cs(spce^-1 * bsqbrackets^-1 * bcbraces)
+local baytargs = lpeg.Cs(spce * bcbraces * bsqbrackets^-1 * bcbraces)
local function protectarb(str)
- str = string.gsub(str, "(\\arb%s?)(%[.-%])(%b{})", "\\@arb%2%3")
+ str = string.gsub(str, "(\\arb%s?)(%[.-%])(%b{})", "\\al@brk{\\arb%2%3}")
str = string.gsub(str, "(\\LR%s?)(%b{})", "\\@LR%2")
str = string.gsub(str, "(\\RL%s?)(%b{})", "\\@RL%2")
return str
@@ -639,6 +641,210 @@ local function processbuckw(str)
return str
end
+-- The following functions produce a copy of the original .tex source
+-- file in which all arabtex strings are replaced with Unicode
+-- equivalents
+local utffilesuffix = "_out"
+local export_utf = "no"
+
+function al_utffilesuffix(str)
+ utffilesuffix = str
+ return true
+end
+
+function al_doexport(str)
+ export_utf = str
+ return true
+end
+
+function al_openstream()
+ local f = io.open(tex.jobname..utffilesuffix.."_tmp.tex", "a+")
+ local preamble = io.open(tex.jobname..".tex", "r")
+ for line in preamble:lines() do
+ f:write(line, "\n")
+ if string.find(line, "^%s-\\begin%s?{document}") then
+ break
+ end
+ end
+ preamble:close()
+ f:close()
+ return true
+end
+
+local function processarbtoutf(str)
+ if export_utf ~= "arabverse" then
+ str = "\\begin{arabexport}"..str
+ else end
+ str = string.gsub(str, "(\\txtrans%s?)(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ body = string.gsub(body, "(\\abjad%s?)(%b{})", function(btag, bbody)
+ bbody = string.sub(bbody, 2, -2)
+ return string.format("%s", bbody)
+ end)
+ return string.format("%s{%s}", tag, body)
+ end)
+
+ str = string.gsub(str, "(\\txarb%s?)(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ body = string.gsub(body, "(\\abjad%s?)(%b{})", function(btag, bbody)
+ bbody = string.sub(bbody, 2, -2)
+ if tonumber(bbody) ~= nil then
+ bbody = abjadify(bbody)
+ return string.format("\\aemph{\\arb[novoc]{%s}}", bbody)
+ else
+ return string.format("%s{%s}", btag, bbody)
+ end
+ end)
+ body = string.gsub(body, "(\\arbmark%s?)(%b{})", function(btag, bbody)
+ bbody = string.sub(bbody, 2, -2)
+ return string.format("%s[rl]{%s}", btag, bbody)
+ end)
+ return string.format("%s{%s}", tag, body)
+ end)
+ str = string.gsub(str, "(\\bayt)%s?(%b{})(%b[])(%b{})", function(tag, argi, argii, argiii)
+ argi = string.sub(argi, 2, -2)
+ argii = string.sub(argii, 2, -2)
+ argiii = string.sub(argiii, 2, -2)
+ return string.format("%s*{\\arb{%s}}[\\arb{%s}]{\\arb{%s}}", tag, argi, argii, argiii)
+ end)
+ str = string.gsub(str, "(\\bayt)%s?(%b{})(%b{})", function(tag, argi, argii)
+ argi = string.sub(argi, 2, -2)
+ argii = string.sub(argii, 2, -2)
+ return string.format("%s*{\\arb{%s}}{\\arb{%s}}", tag, argi, argii)
+ end)
+ str = string.gsub(str, "(\\prname)%s?(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ if string.find(body, "\\uc%s?%b{}") then
+ return string.format("%s*{%s}", tag, body)
+ else
+ return string.format("%s{\\arb[trans]{\\uc{%s}}}", tag, body)
+ end
+ end)
+ str = string.gsub(str, "(\\begin%s?{arab})(%b[])", function(tag, body)
+ if string.find(body, "trans") then
+ return string.format("\\par\\bgroup\\setLR\\arb%s{", body)
+ else
+ return string.format("\\par\\bgroup\\setRL\\arb%s{", body)
+ end
+ end)
+ str = string.gsub(str, "(\\begin%s?{arab})", "\\par\\bgroup\\arbpardir\\arb{")
+ str = string.gsub(str, "\\end%s?{arab}", "}\\egroup\\par")
+ -- This does not work, while the following two do. Look into this later.
+ -- str = gsub(str, lpeg.Cs("\\arb") * spcenc * bsqbrackets^-1 * bcbraces, function(tag, opt, body)
+ -- body = string.sub(body, 2, -2)
+ -- return string.format("%s%s\\@al@pr@ob%s\\@al@pr@cb", tag, opt, body)
+ -- end)
+ str = string.gsub(str, "(\\arb%s?)(%b[])(%b{})", function(tag, opt, body)
+ body = string.sub(body, 2, -2)
+ return string.format("%s%s\\@al@pr@ob%s\\@al@pr@cb", tag, opt, body)
+ end)
+ str = string.gsub(str, "(\\arb)%s?(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ return string.format("%s\\@al@pr@ob%s\\@al@pr@cb", tag, body)
+ end)
+ str = string.gsub(str, "(\\arbmark)%s?(%b[])(%b{})", function(tag, opt, body)
+ body = string.sub(body, 2, -2)
+ return string.format("%s%s\\@al@pr@ob%s\\@al@pr@cb", tag, opt, body)
+ end)
+ str = string.gsub(str, "(\\arbmark)%s?(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ return string.format("%s\\@al@pr@ob%s\\@al@pr@cb", tag, body)
+ end)
+ str = string.gsub(str, "(\\[Uu]c)%s?(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ return string.format("%s\\@al@pr@ob%s\\@al@pr@cb", tag, body)
+ end)
+ str = string.gsub(str, "{", "\\@al@ob")
+ str = string.gsub(str, "} ", "\\@al@cb@sp")
+ str = string.gsub(str, "}", "\\@al@cb")
+ str = string.gsub(str, "\\@al@pr@ob", "{")
+ str = string.gsub(str, "\\@al@pr@cb", "}")
+ str = string.gsub(str, "(%b{})", function(body)
+ body = string.sub(body, 2, -2)
+ body = string.gsub(body, "(%s?)(\\@al@ob)", "%1{")
+ body = string.gsub(body, "(\\@al@cb@sp)", "} ")
+ body = string.gsub(body, "(\\@al@cb)(%s?)", "}%2")
+ return string.format("{%s}", body)
+ end)
+ if export_utf ~= "arabverse" then
+ str = str.."\\end{arabexport}"
+ else end
+ return str
+end
+
+function arbtoutf(str)
+ str = processarbtoutf(str)
+ str = "\\ArbOutFile{"..str.."}"
+ str = string.gsub(str, "(\\ArbOutFile)%s?(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ body = gsub(body, lpeg.Cs("\\arb") * arbargs, "}%1%2\\ArbOutFile{")
+ return string.format("%s{%s}", tag, body)
+ end)
+ str = string.gsub(str, "(\\ArbOutFile)%s?(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ body = string.gsub(body, "(\\[Uu]c)%s?(%b{})", "}%1%2\\ArbOutFile{")
+ return string.format("%s{%s}", tag, body)
+ end)
+ str = string.gsub(str, "(\\ArbOutFile)%s?(%b{})", function(tag, body)
+ body = string.sub(body, 2, -2)
+ body = gsub(body, lpeg.Cs("\\arbmark") * arbargs, "}%1%2\\ArbOutFile{")
+ return string.format("%s{%s}", tag, body)
+ end)
+ return str
+end
+
+function tooutfile(str, nl)
+ local f = io.open(tex.jobname..utffilesuffix.."_tmp.tex", "a+")
+ if nl == "newline" then
+ f:write(str, "\n\n")
+ else
+ f:write(str)
+ end
+ f:close()
+ return str
+end
+
+function al_closestream()
+ local f = io.open(tex.jobname..utffilesuffix.."_tmp.tex", "r")
+ local o = io.open(tex.jobname..utffilesuffix..".tex", "w")
+ local t = f:read("*a")
+ t = string.gsub(t, "\\arabicfont{}", "")
+ t = string.gsub(t, "\\par ", "\n\n")
+ t = string.gsub(t, "(\\@al@ob)", "{")
+ t = string.gsub(t, "(\\@al@cb@sp)", "} ")
+ t = string.gsub(t, "(\\@al@cb)(%s?)", "}")
+ t = gsub(t, lpeg.Cs("\\begin") * spcenc^-1 * bcbraces * cmdargs, "\n%1%2%3\n")
+ t = string.gsub(t, "(\\\\)(%s?)", "%1\n")
+ t = string.gsub(t, "(\\\\)(\n)(\\end%s?)(%b{})", "%1%3%4")
+ t = string.gsub(t, "%s-\n(\\begin%s?)(%b{})", "\n%1%2")
+ t = string.gsub(t, "(\\item)", "\n%1")
+ t = string.gsub(t, "\n\n(\\item)", "\n%1")
+ t = string.gsub(t, "(\\end%s?)(%b{})", "%1%2\n")
+ t = string.gsub(t, "([^\n]%s-)(\\end)%s?(%b{})", "%1\n%2%3")
+ t = string.gsub(t, "\n\n\n", "\n\n")
+ t = string.gsub(t, "(\\txarb%s?%{)(\\txarb%s?)(%b{})(%})", function(tagio, tagii, body, tagic)
+ body = string.sub(body, 2, -2)
+ return string.format("%s%s%s", tagio, body, tagic)
+ end)
+ t = string.gsub(t, "(\\prname%s?%*%{)(\\txtrans%s?)(%b{})(%})", function(tagio, tagii, body, tagic)
+ body = string.sub(body, 2, -2)
+ return string.format("%s%s%s", tagio, body, tagic)
+ end)
+ if string.find(t, "\\begin%s?{document}.-\\arb%s?[%[%{]") or
+ string.find(t, "\\begin%s?{document}.-\\[Uu]c%s?%b{}")
+ then
+ tex.print([[\unexpanded{\PackageWarningNoLine{arabluatex}{There are still 'arabtex' strings to be converted. Please open ]]..tex.jobname..utffilesuffix..".tex"..[[ and compile it one more time}}]])
+ else end
+ t = t.."\n\\end{document}"
+ io.write(t)
+ o:write(t)
+ f:close()
+ o:close()
+ os.remove(tex.jobname..utffilesuffix.."_tmp.tex")
+ return true
+end
+
+-- Process standard arabluatex modes:
function processvoc(str, rules, scheme)
str = takeoutarb(str)
str = processarbnull(str, scheme)
@@ -655,7 +861,16 @@ function processvoc(str, rules, scheme)
str = voc(str, rules)
else end
str = unprotectarb(str)
-return str
+ if export_utf == "yes" then
+ tofile = "\\txarb{"..str.."}"
+ tooutfile(tofile)
+ elseif export_utf == "arabverse" then
+ tofile = "\\txarb{"..str.."}"
+ tooutfile(tofile)
+ else
+ return str
+ end
+ return ""
end
function processfullvoc(str, rules, scheme)
@@ -676,7 +891,16 @@ function processfullvoc(str, rules, scheme)
str = fullvoc(str, rules)
else end
str = unprotectarb(str)
-return str
+ if export_utf == "yes" then
+ tofile = "\\txarb{"..str.."}"
+ tooutfile(tofile)
+ elseif export_utf == "arabverse" then
+ tofile = "\\txarb{"..str.."}"
+ tooutfile(tofile)
+ else
+ return str
+ end
+ return ""
end
function processnovoc(str, rules, scheme)
@@ -695,7 +919,16 @@ function processnovoc(str, rules, scheme)
str = novoc(str)
else end
str = unprotectarb(str)
-return str
+ if export_utf == "yes" then
+ tofile = "\\txarb{"..str.."}"
+ tooutfile(tofile)
+ elseif export_utf == "arabverse" then
+ tofile = "\\txarb{"..str.."}"
+ tooutfile(tofile)
+ else
+ return str
+ end
+ return ""
end
function processtrans(str, mode, rules, scheme)
@@ -717,7 +950,16 @@ function processtrans(str, mode, rules, scheme)
str = transarabica(str)
end
str = unprotectarb(str)
-return str
+ if export_utf == "yes" then
+ tofile = str
+ tooutfile(tofile)
+ elseif export_utf == "arabverse" then
+ tofile = str
+ tooutfile(tofile)
+ else
+ return str
+ end
+ return ""
end
function newarbmark(abbr, rtlmk, ltrmk)
@@ -736,11 +978,19 @@ local function isintable(table, element)
return false
end
-function processarbmarks(str)
+function processarbmarks(str, dir)
if not isintable(arbmarks, str) then
str = "\\LR{<??>}"..atletter.."\\al@wrong@mark{}"..atother
else
- if tex.textdir == "TLT" then
+ if dir == "lr" then
+ for i = 1,#arbmarks do
+ str = string.gsub(str, arbmarks[i].a, arbmarks[i].c)
+ end
+ elseif dir == "rl" then
+ for i = 1,#arbmarks do
+ str = string.gsub(str, arbmarks[i].a, arbmarks[i].b)
+ end
+ elseif tex.textdir == "TLT" then
for i = 1,#arbmarks do
str = string.gsub(str, arbmarks[i].a, arbmarks[i].c)
end
@@ -750,7 +1000,16 @@ function processarbmarks(str)
end
end
end
- return str
+ if export_utf == "yes" then
+ tofile = str
+ tooutfile(tofile)
+ elseif export_utf == "arabverse" then
+ tofile = str
+ tooutfile(tofile)
+ else
+ return str
+ end
+ return ""
end
function uc(str)
@@ -804,7 +1063,16 @@ function uc(str)
for i = 1,#lcuc do
str = string.gsub(str, "(%s[%(%<%[]?)"..lcuc[i].a, "%1"..lcuc[i].b)
end
- return "\\txtrans{"..str.."}"
+ if export_utf == "yes" then
+ tofile = "\\txtrans{"..str.."}"
+ tooutfile(tofile)
+ elseif export_utf == "arabverse" then
+ tofile = "\\txtrans{"..str.."}"
+ tooutfile(tofile)
+ else
+ return "\\txtrans{"..str.."}"
+ end
+ return ""
end
-- this function is adapted from an 'obsolete project' of Khaled