diff options
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua')
-rw-r--r-- | Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua | 268 |
1 files changed, 162 insertions, 106 deletions
diff --git a/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua b/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua index 7a7460aeef2..823823621c1 100644 --- a/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua +++ b/Master/texmf-dist/tex/lualatex/arabluatex/arabluatex.lua @@ -27,127 +27,139 @@ require("arabluatex_fullvoc") require("arabluatex_novoc") require("arabluatex_trans") +-- lpeg equivalent for string.gsub() +local function gsub(s, patt, repl) + patt = lpeg.P(patt) + patt = lpeg.Cs((patt / repl + 1)^0) + return lpeg.match(patt, s) +end + +-- makeatletter, makeatother +local atletter = "\\makeatletter{}" +local atother = "\\makeatother{}" + +-- some basic patterns: +local ascii = lpeg.R("az", "AZ", "@@") +local dblbkslash = lpeg.Cs("\\") +local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" } +local bcbraces = lpeg.Cs{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" } +local spce = lpeg.Cs(" ") +local spcenc = lpeg.P(" ") +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 function protectarb(str) - str = string.gsub(str, "(\\arb.?)(%[.-%])(%b{})", "\\@arb%2%3") - str = string.gsub(str, "(\\begin.?)(%b{})(%b[])", "\\@begin%3%2") - str = string.gsub(str, "(\\begin.?)(%b{})", "\\@begin%2") - str = string.gsub(str, "(\\end.?)(%b{})", "\\@end%2") - str = string.gsub(str, "\\par", "\\p@r{}") - str = string.gsub(str, "\\@@par", "\\p@r{}") -return str + str = string.gsub(str, "(\\arb%s?)(%[.-%])(%b{})", "\\@arb%2%3") + str = string.gsub(str, "(\\LR%s?)(%b{})", "\\@LR%2") + str = string.gsub(str, "(\\RL%s?)(%b{})", "\\@RL%2") + return str end local function unprotectarb(str) str = string.gsub(str, "(\\@arb)(%[.-%])(%b{})", "\\arb%2%3") - str = string.gsub(str, "(\\@begin)(%b[])(%b{})", "\\begin%3%2") - str = string.gsub(str, "(\\@begin)(%b{})", "\\begin%2") - str = string.gsub(str, "(\\@end)(%b{})", "\\end%2") - str = string.gsub(str, "\\p@r{}", "\\par") -return str + str = string.gsub(str, "(\\@LR)(%b{})", "\\LR%2") + str = string.gsub(str, "(\\@RL)(%b{})", "\\RL%2") + str = gsub(str, lpeg.Cs("\\al@brk") * bcbraces, function(tag, body) + body = string.sub(body, 2, -2) + return string.format("%s", body) + end) + return str end -brkcmds = {} - -function mkarbbreak(str) +-- the following is to be taken out of \arb{} +local outofarb = { + "LRfootnote", + "RLfootnote", + "edtext", + "pstart", + "pend" +} +-- commands the arguments of which must not be processed by arabluatex +-- inside \arb{}. 'albrkcmds' is what is set by default. 'brkcmds' +-- collects the commands set in the preamble with \MkArbBreak{} +local albrkcmds = { + "begin", + "end", + "par", + "LRmarginpar", + "arbmark", + "abjad" +} +local brkcmds = {} + +function mkarbbreak(str, opt) str = str .."," str = string.gsub(str, "%s+", "") local fieldstart = 1 - repeat - local nexti = string.find(str, "%,", fieldstart) - table.insert(brkcmds, string.sub(str, fieldstart, nexti-1)) - fieldstart = nexti +1 - until fieldstart > string.len(str) - return brkcmds + if opt == "dflt" then + repeat + local nexti = string.find(str, "%,", fieldstart) + table.insert(brkcmds, string.sub(str, fieldstart, nexti-1)) + fieldstart = nexti +1 + until fieldstart > string.len(str) + return brkcmds + elseif opt == "out" then + repeat + local nexti = string.find(str, "%,", fieldstart) + table.insert(outofarb, string.sub(str, fieldstart, nexti-1)) + fieldstart = nexti +1 + until fieldstart > string.len(str) + return outofarb + end end local function breakcmd(str) - -- user commands - if next(brkcmds) == nil then - -- nothing to do - else - for i = 1,#brkcmds do - str = string.gsub(str, "\\"..brkcmds[i].."%s?(%b{})", - function(body) - body = string.sub(body, 2, -2) - return string.format("}\\"..brkcmds[i].."{%s}\\arb{", body) - end) - end - end -- process \item[], then \item[] str = string.gsub(str, "\\(item.?)(%b[])", function(tag, body) body = string.sub(body, 2, -2) - return string.format("}\\item[\\arb{%s}] \\arb{", body) + return string.format("\\al@brk{\\item[\\arb{%s}] }", body) end) str = string.gsub(str, "(\\item)(%s+)", "%1{}%2") - -- \edtext - str = string.gsub(str, "\\(edtext.-)(%b{})(%b{})", - function(tag, bodylem, bodyvar) - bodylem = string.sub(bodylem, 2, -2) - bodyvar = string.sub(bodyvar, 2, -2) - return string.format("\\LR{\\%s{%s}{%s}}", tag, bodylem, bodyvar) - end) - -- \RL - str = string.gsub(str, "\\(RL.-)(%b{})", - function(tag, body) - body = string.sub(body, 2, -2) - return string.format("}\\%s{%s}\\arb{", tag, body) - end) - -- \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) -- \textcolor - str = string.gsub(str, "\\(textcolor.?)(%b{})(%b{})", - function(tag, bodycolor, bodytext) - bodycolor = string.sub(bodycolor, 2, -2) - bodytext = string.sub(bodytext, 2, -2) - return string.format("}\\%s{%s}{\\arb{%s}}\\arb{", tag, bodycolor, bodytext) - 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 - str = string.gsub(str, "\\(abjad.-)(%b{})", - function(tag, body) - body = string.sub(body, 2, -2) - return string.format("}\\aemph{\\%s{%s}}\\arb{", tag, body) - end) - -- Arbmark - str = string.gsub(str, "\\(arbmark.-)(%b{})", - function(tag, body) - body = string.sub(body, 2, -2) - return string.format("}\\%s{%s}\\arb{", tag, body) + str = string.gsub(str, "\\(textcolor%s?)(%b{})(%b{})", + function(tag, bodycolor, bodytext) + bodycolor = string.sub(bodycolor, 2, -2) + bodytext = string.sub(bodytext, 2, -2) + return string.format("\\al@brk{\\%s{%s}{\\arb{%s}}}", tag, bodycolor, bodytext) end) + -- commands set by default in outofarb + for i = 1,#outofarb do + str = gsub(str, dblbkslash * lpeg.Cs(outofarb[i]) * cmdargs, "}%1%2%3\\arb{") + end + -- commands set by default in albrkcmds + for i = 1,#albrkcmds do + str = gsub(str, dblbkslash * lpeg.Cs(albrkcmds[i]) * cmdargs, "\\al@brk{%1%2%3}") + end + -- user commands (brkcmds) + if next(brkcmds) == nil then + -- nothing to do + else + for i = 1,#brkcmds do + str = gsub(str, dblbkslash * lpeg.Cs(brkcmds[i]) * cmdargs, "\\al@brk{%1%2%3}") + end + end return str end local function holdcmd(str) - str = string.gsub(str, "\\(arb)(%b{})", function(tag, body) + str = gsub(str, lpeg.Cs("\\arb") * bcbraces, function(tag, body) body = string.sub(body, 2, -2) - body = string.gsub(body, "\\(.-)(%b{})", function(btag, bbody) + body = gsub(body, cmd * spcenc^-1 * bsqbracketsii * spcenc^-1 * bcbraces, function(btag, bopt, bbody) bbody = string.sub(bbody, 2, -2) if string.find(btag, "@") then - return holdcmd(string.format("}\\%s{%s}\\arb{", btag, bbody)) + return holdcmd(string.format("}%s%s{%s}\\arb{", btag, bopt, bbody)) else - return holdcmd(string.format("}\\%s{\\arb{%s}}\\arb{", btag, bbody)) + return holdcmd(string.format("}%s%s{\\arb{%s}}\\arb{", btag, bopt, bbody)) end end) - return string.format("\\%s{%s}", tag, body) - end) - str = string.gsub(str, "\\arb{}", "") -return str + return string.format("%s{%s}", tag, body) + end) + str = string.gsub(str, "\\arb{}", "") + return str end local function arbnum(str) @@ -181,7 +193,7 @@ local function processarbnull(str, scheme) else str = string.gsub(str, "(\\arbnull.?)(%b{})", function(tag, body) body = string.sub(body, 2, -2) - return string.format("o%so", body) + return string.format("O%sO", body) end) end return str @@ -201,6 +213,10 @@ local function takeoutcapetc(str) body = string.gsub(body, "(\\uc%s?)(%b{})", "\\Uc%2") return string.format("%s{%s}", tag, body) end) + str = string.gsub(str, "(\\arbup.?)(%b{})", function(tag, body) + body = string.sub(body, 2, -2) + return string.format("%s", body) + end) str = string.gsub(str, "(\\uc.?)(%b{})", function(tag, body) body = string.sub(body, 2, -2) return string.format("%s", body) @@ -210,6 +226,39 @@ local function takeoutcapetc(str) return str end +local function checkwrnested(str) + for i = 1,#outofarb do + str = gsub(str, dblbkslash * lpeg.Cs(lpeg.P("LR") + lpeg.P("RL")) * cmdargs, + function(prefix, tag, body) + body = string.sub(body, 2, -2) + if string.find(body, "\\"..outofarb[i]) then + return atletter.."\\al@wrong@nesting{}"..atother + else + -- nothing to do, so proceed. + end + end) + end + return str +end + +local function takeoutarb(str) + str = checkwrnested(str) + for i = 1,#outofarb do + str = gsub(str, dblbkslash * lpeg.Cs(outofarb[i]) * cmdargs, + function(prefix, tag, body) + body = gsub(body, lpeg.P("\\arb"), "\\@rb") + return string.format("%s%s%s", prefix, tag, body) + end) + end + str = string.gsub(str, "(\\arb%s?)(%b{})", function(tag, body) + body = string.sub(body, 2, -2) + return string.format("%s", body) + end) + str = string.gsub(str, "\\@rb", "\\arb") + str = "\\arb{"..str.."}" + return str +end + local function voc(str, rules) str = string.gsub(str, "\\arb(%b{})", function(inside) inside = string.sub(inside, 2, -2) @@ -252,7 +301,7 @@ local function voc(str, rules) inside = string.gsub(inside, null[i].a, null[i].b) end inside = indnum(inside) - return string.format("\\txarb{%s}", inside) + return string.format("\\arabicfont{}%s", inside) end) return str end @@ -288,7 +337,7 @@ local function voceasy(str) inside = string.gsub(inside, null[i].a, null[i].b) end inside = indnum(inside) - return string.format("\\txarb{%s}", inside) + return string.format("\\arabicfont{}%s", inside) end) return str end @@ -341,7 +390,7 @@ local function fullvoc(str, rules) inside = string.gsub(inside, null[i].a, null[i].b) end inside = indnum(inside) - return string.format("\\txarb{%s}", inside) + return string.format("\\arabicfont{}%s", inside) end) return str end @@ -389,7 +438,7 @@ local function fullvoceasy(str, rules) inside = string.gsub(inside, null[i].a, null[i].b) end inside = indnum(inside) - return string.format("\\txarb{%s}", inside) + return string.format("\\arabicfont{}%s", inside) end) return str end @@ -425,7 +474,7 @@ local function novoc(str) inside = string.gsub(inside, null[i].a, null[i].b) end inside = indnum(inside) - return string.format("\\txarb{%s}", inside) + return string.format("\\arabicfont{}%s", inside) end) return str end @@ -461,7 +510,7 @@ local function novoceasy(str) inside = string.gsub(inside, null[i].a, null[i].b) end inside = indnum(inside) - return string.format("\\txarb{%s}", inside) + return string.format("\\arabicfont{}%s", inside) end) return str end @@ -591,7 +640,7 @@ return str end function processvoc(str, rules, scheme) - str = "\\arb{".. str.."}" + str = takeoutarb(str) str = processarbnull(str, scheme) str = takeoutcapetc(str) str = protectarb(str) @@ -610,7 +659,7 @@ return str end function processfullvoc(str, rules, scheme) - str = "\\arb{".. str.."}" + str = takeoutarb(str) str = processarbnull(str, scheme) str = takeoutcapetc(str) str = protectarb(str) @@ -631,7 +680,7 @@ return str end function processnovoc(str, rules, scheme) - str = "\\arb{".. str.."}" + str = takeoutarb(str) str = processarbnull(str, scheme) str = takeoutcapetc(str) str = protectarb(str) @@ -650,7 +699,7 @@ return str end function processtrans(str, mode, rules, scheme) - str = "\\arb{".. str.."}" + str = takeoutarb(str) str = processdiscretionary(str) str = processarbnull(str, scheme) str = takeoutabjad(str) @@ -672,7 +721,9 @@ return str end function newarbmark(abbr, rtlmk, ltrmk) + rtlmk = "\\arabicfont{}"..rtlmk table.insert(arbmarks, {a = abbr, b = rtlmk, c = ltrmk}) + table.sort(arbmarks, function(a ,b) return(#a.a > #b.a) end) return true end @@ -687,7 +738,7 @@ end function processarbmarks(str) if not isintable(arbmarks, str) then - str = "\\LR{<??>}" + str = "\\LR{<??>}"..atletter.."\\al@wrong@mark{}"..atother else if tex.textdir == "TLT" then for i = 1,#arbmarks do @@ -760,7 +811,8 @@ end -- Hosny's that dates back to 2010. Thanks to him. -- See https://github.com/khaledhosny/lualatex-arabic function abjadify(n) - local abjadnum = "" + local abjadnum = "" + n = tonumber(n) if n >= 1000 then for i=1,math.floor(n/1000) do abjadnum = abjadnum .. abjad[4][1] @@ -790,11 +842,15 @@ function abraces(str) return str end -function aemph(str) +function aemph(str, opt) if tex.textdir == "TRT" then - str = "$\\overline{\\text{"..str.."}}$" + str = "$\\overline{\\text{\\textdir TRT{}"..str.."}}$" elseif tex.textdir == "TLT" then - str = "$\\underline{\\text{"..str.."}}$" + if opt == "over" then + str = "$\\overline{\\text{"..str.."}}$" + else + str = "$\\underline{\\text{"..str.."}}$" + end end return str end |