From 5c6d0893befcc5ac8102d2fe9480f4be0046de68 Mon Sep 17 00:00:00 2001 From: Robert Alessi Date: Sat, 11 Aug 2018 15:43:56 +0200 Subject: arabtex2utf: added basic functions --- arabluatex.lua | 195 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 181 insertions(+), 14 deletions(-) diff --git a/arabluatex.lua b/arabluatex.lua index 3efccd2..4128955 100644 --- a/arabluatex.lua +++ b/arabluatex.lua @@ -50,7 +50,7 @@ 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 arbargs = lpeg.Cs(spce * bsqbrackets^-1 * bcbraces) +local arbargs = lpeg.Cs(spce^-1 * bsqbrackets^-1 * bcbraces) local baytargs = lpeg.Cs(spce * bcbraces * bsqbrackets^-1 * bcbraces) local function protectarb(str) @@ -641,24 +641,163 @@ local function processbuckw(str) return str end -function processexport(str) - str = "\\tooutfile{"..str.."}" - str = gsub(str, lpeg.Cs("\\arb") * arbargs, "}%1%2\\tooutfile{") - str = gsub(str, lpeg.Cs("\\bayt") * baytargs, "}\\tof{\\bayt}%1%2\\tooutfile{") +--- *** +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) + str = string.gsub(str, "(\\prname)%s?(%b{})", function(tag, body) + body = string.sub(body, 2, -2) + return string.format("\\arb[trans]{\\uc{%s}}", body) + end) + str = string.gsub(str, "(\\begin%s?{arab})(%b[])", "\\par\\bgroup\\arbpardir\\arb%2{") + str = string.gsub(str, "(\\begin%s?{arab})", "\\par\\bgroup\\arbpardir\\arb{") + str = string.gsub(str, "\\end%s?{arab}", "}\\egroup\\par") + -- 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{})", 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, "(\\abjad)%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 ") + 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, "\\@al@ob%s?", "{") + body = string.gsub(body, "\\@al@cb%s?", "}") + return string.format("{%s}", body) + end) + return str +end + +function arbtoutf(str) + str = processarbtoutf(str) + str = "\\ToOutFile{"..str.."}" + str = string.gsub(str, "(\\ToOutFile)%s?(%b{})", function(tag, body) + body = string.sub(body, 2, -2) + body = gsub(body, lpeg.Cs("\\arb") * arbargs, "}%1%2\\ToOutFile{") + return string.format("%s{%s}", tag, body) + end) + str = string.gsub(str, "(\\ToOutFile)%s?(%b{})", function(tag, body) + body = string.sub(body, 2, -2) + body = string.gsub(body, "(\\abjad)%s?(%b{})", "}%1%2\\ToOutFile{") + return string.format("%s{%s}", tag, body) + end) + str = string.gsub(str, "(\\ToOutFile)%s?(%b{})", function(tag, body) + body = string.sub(body, 2, -2) + body = string.gsub(body, "(\\arbmark)%s?(%b{})", "}%1%2\\ToOutFile{") + return string.format("%s{%s}", tag, body) + end) + -- str = string.gsub(str, "(\\arb%s?)(%b[])(%b{})", "}%1%2%3\\ToOutFile{") + -- str = string.gsub(str, "(\\arb)%s?(%b{})", "}%1%2\\ToOutFile{") + -- str = gsub(str, lpeg.Cs("\\bayt") * baytargs, "}\\ToOutFile{\\bayt}%1%2\\ToOutFile{") +-- str = gsub(str, lpeg.Cs("\\arb") * arbargs, "}%1%2\\ToOutFile{") +-- str = string.gsub(str, "(\\abjad)%s?(%b{})", "}%1%2\\ToOutFile{") +-- str = string.gsub(str, "(\\abjad)(%s?)(%b{})", "}%1%3\\tooutfile{") +-- str = string.gsub(str, "(\\begin%s?{arab})", "}%1") +-- str = string.gsub(str, "(\\end%s?{arab})", "%1\\ToOutFile{") +-- str = string.gsub(str, "(\\begin)", "\n%1") +-- str = string.gsub(str, "(\\end)", "\n%1") +-- str = string.gsub(str, "(\\bayt)", "\n%1") +-- str = string.gsub(str, "(\\\\)", "%1\n") return str end function tooutfile(str, nl) - local f = io.open(tex.jobname.."_out.tex", "a+") - if nl == "yes" then + 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") + --[[ next lines need work! + -- t = string.gsub(t, "{\\textdir TRT\\arabicfont{}", "\\txarb{") + t = string.gsub(t, "(\\bayt%s?)\\txarb(%b{})\\txarb(%b{})\\txarb(%b{})", + function(tag, bodyi, bodyii, bodyiii) + bodyii = string.sub(bodyii, 2, -2) + return string.format("%s%s[%s]%s", + tag, bodyi, bodyii, bodyiii) + end) + t = string.gsub(t, "(\\bayt%s?)\\txarb(%b{})\\txarb(%b{})", "%1%2%3") + t = string.gsub(t, "{\\textdir TRT\\arbmetre{\\arabicfont{}", "\\arbmetre{\\txarb{") + t = string.gsub(t, "(\\bayt%s?)\\txtrans(%b{})\\txtrans(%b{})\\txtrans(%b{})", + function(tag, bodyi, bodyii, bodyiii) + bodyii = string.sub(bodyii, 2, -2) + return string.format("%s%s[%s]%s", + tag, bodyi, bodyii, bodyiii) + end) + t = string.gsub(t, "(\\bayt%s?)\\txtrans(%b{})\\txtrans(%b{})", "%1%2%3") + t = string.gsub(t, "\\txtrans{\\arbmetre{", "\\arbmetre{\\txtrans{") + t = string.gsub(t, "\\arbmetre", "\\\\\n\\hfill") + t = string.gsub(t, "(\\bayt)", "\n%1") + --]] + t = string.gsub(t, "\\arabicfont{}", "") + t = string.gsub(t, "\\par ", "\n\n") + t = string.gsub(t, "\\@al@ob%s?", "{") + t = string.gsub(t, "\\@al@cb", "}") + t = string.gsub(t, "\n\n\n", "\n\n") + if string.find(t, "\\begin%s?{document}.-\\arb") then + tex.print([[\unexpanded{\PackageWarningNoLine{arabluatex}{There are still 'arabtex' strings to be converted. 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") +-- os.rename(tex.jobname.."_out_tmp.tex", tex.jobname.."_out.tex") return true end + function processvoc(str, rules, scheme) str = takeoutarb(str) str = processarbnull(str, scheme) @@ -675,9 +814,13 @@ function processvoc(str, rules, scheme) str = voc(str, rules) else end str = unprotectarb(str) - tofile = "{\\textdir TRT"..str.."}" - tooutfile(tofile) -return str + if export_utf == "yes" then + tofile = "\\txarb{"..str.."}" + tooutfile(tofile) + else + return str + end + return "" end function processfullvoc(str, rules, scheme) @@ -698,7 +841,13 @@ 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) + else + return str + end + return "" end function processnovoc(str, rules, scheme) @@ -717,7 +866,13 @@ 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) + else + return str + end + return "" end function processtrans(str, mode, rules, scheme) @@ -739,8 +894,13 @@ function processtrans(str, mode, rules, scheme) str = transarabica(str) end str = unprotectarb(str) + if export_utf == "yes" then + tofile = str tooutfile(str) -return str + else + return str + end + return "" end function newarbmark(abbr, rtlmk, ltrmk) @@ -773,7 +933,14 @@ function processarbmarks(str) end end end - return str +-- return str + if export_utf == "yes" then + tofile = str + tooutfile(str) + else + return str + end + return "" end function uc(str) -- cgit v1.2.3