aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arabluatex.lua87
1 files changed, 41 insertions, 46 deletions
diff --git a/arabluatex.lua b/arabluatex.lua
index 6a08a33..d77aa51 100644
--- a/arabluatex.lua
+++ b/arabluatex.lua
@@ -34,14 +34,35 @@ local function gsub(s, patt, repl)
34 return lpeg.match(patt, s) 34 return lpeg.match(patt, s)
35end 35end
36 36
37-- some basic patterns:
38local ascii = lpeg.R("az", "AZ", "@@")
39local dblbkslash = lpeg.Cs("\\")
40local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" }
41local bcbraces = lpeg.Cs{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" }
42local spce = lpeg.Cs(" ")
43local spcenc = lpeg.P(" ")
44local cmdstar = lpeg.Cs(spce * lpeg.P("*"))
45local bsqbracketsii = lpeg.Cs(bsqbrackets^-2)
46local bcbracesii = lpeg.Cs(bcbraces^-2)
47local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1)
48local cmdargs = lpeg.Cs(spce * bsqbracketsii * bcbracesii * bsqbrackets^-1)
49
37local function protectarb(str) 50local function protectarb(str)
38 str = string.gsub(str, "(\\arb%s?)(%[.-%])(%b{})", "\\@arb%2%3") 51 str = string.gsub(str, "(\\arb%s?)(%[.-%])(%b{})", "\\@arb%2%3")
39return str 52 str = string.gsub(str, "(\\LR%s?)(%b{})", "\\@LR%2")
53 str = string.gsub(str, "(\\RL%s?)(%b{})", "\\@RL%2")
54 return str
40end 55end
41 56
42local function unprotectarb(str) 57local function unprotectarb(str)
43 str = string.gsub(str, "(\\@arb)(%[.-%])(%b{})", "\\arb%2%3") 58 str = string.gsub(str, "(\\@arb)(%[.-%])(%b{})", "\\arb%2%3")
44return str 59 str = string.gsub(str, "(\\@LR)(%b{})", "\\LR%2")
60 str = string.gsub(str, "(\\@RL)(%b{})", "\\RL%2")
61 str = gsub(str, lpeg.Cs("\\al@brk") * bcbraces, function(tag, body)
62 body = string.sub(body, 2, -2)
63 return string.format("%s", body)
64 end)
65 return str
45end 66end
46 67
47-- commands the arguments of which must not be processed by 68-- commands the arguments of which must not be processed by
@@ -50,10 +71,7 @@ end
50local albrkcmds = { 71local albrkcmds = {
51 "begin", 72 "begin",
52 "end", 73 "end",
53 "RL", 74 "par",
54 "LR",
55 "RLfootnote",
56 "LRfootnote",
57 "LRmarginpar", 75 "LRmarginpar",
58 "Footnote", 76 "Footnote",
59 "Marginpar", 77 "Marginpar",
@@ -76,25 +94,12 @@ function mkarbbreak(str)
76 return brkcmds 94 return brkcmds
77end 95end
78 96
79-- some basic patterns:
80local ascii = lpeg.R("az", "AZ", "@@")
81local dblbkslash = lpeg.Cs("\\")
82local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" }
83local bcbraces = lpeg.Cs{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" }
84local spce = lpeg.Cs(" ")
85local spcenc = lpeg.P(" ")
86local cmdstar = lpeg.Cs(spce * lpeg.P("*"))
87local bsqbracketsii = lpeg.Cs(bsqbrackets^-2)
88local bcbracesii = lpeg.Cs(bcbraces^-2)
89local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1)
90local cmdargs = lpeg.Cs(spce * bsqbracketsii * bcbracesii * bsqbrackets^-1)
91
92local function breakcmd(str) 97local function breakcmd(str)
93 -- process \item[], then \item[] 98 -- process \item[], then \item[]
94 str = string.gsub(str, "\\(item.?)(%b[])", 99 str = string.gsub(str, "\\(item.?)(%b[])",
95 function(tag, body) 100 function(tag, body)
96 body = string.sub(body, 2, -2) 101 body = string.sub(body, 2, -2)
97 return string.format("}\\item[\\arb{%s}] \\arb{", body) 102 return string.format("\\al@brk{\\item[\\arb{%s}] }", body)
98 end) 103 end)
99 str = string.gsub(str, "(\\item)(%s+)", "%1{}%2") 104 str = string.gsub(str, "(\\item)(%s+)", "%1{}%2")
100 -- \textcolor 105 -- \textcolor
@@ -102,30 +107,40 @@ local function breakcmd(str)
102 function(tag, bodycolor, bodytext) 107 function(tag, bodycolor, bodytext)
103 bodycolor = string.sub(bodycolor, 2, -2) 108 bodycolor = string.sub(bodycolor, 2, -2)
104 bodytext = string.sub(bodytext, 2, -2) 109 bodytext = string.sub(bodytext, 2, -2)
105 return string.format("}\\%s{%s}{\\arb{%s}}\\arb{", tag, bodycolor, bodytext) 110 return string.format("\\al@brk{\\%s{%s}{\\arb{%s}}}", tag, bodycolor, bodytext)
106 end) 111 end)
107 -- Abjad 112 -- Abjad
108 str = string.gsub(str, "\\(abjad%s?)(%b{})", 113 str = string.gsub(str, "\\(abjad%s?)(%b{})",
114 function(tag, body)
115 body = string.sub(body, 2, -2)
116 return string.format("\\al@brk{\\%s{%s}}", tag, body)
117 end)
118 -- LR/RL footnotes
119 str = string.gsub(str, "\\(RLfootnote%s?)(%b{})",
120 function(tag, body)
121 body = string.sub(body, 2, -2)
122 return string.format("}\\%s{%s}\\arb{", tag, body)
123 end)
124 str = string.gsub(str, "\\(LRfootnote%s?)(%b{})",
109 function(tag, body) 125 function(tag, body)
110 body = string.sub(body, 2, -2) 126 body = string.sub(body, 2, -2)
111 return string.format("}\\aemph{\\%s{%s}}\\arb{", tag, body) 127 return string.format("}\\%s{%s}\\arb{", tag, body)
112 end) 128 end)
113 -- commands set by default in albrkcmds 129 -- commands set by default in albrkcmds
114 for i = 1,#albrkcmds do 130 for i = 1,#albrkcmds do
115 str = gsub(str, dblbkslash * lpeg.Cs(albrkcmds[i]) * cmdargs, "}%1%2%3\\arb{") 131 str = gsub(str, dblbkslash * lpeg.Cs(albrkcmds[i]) * cmdargs, "\\al@brk{%1%2%3}")
116 end 132 end
117 -- user commands 133 -- user commands
118 if next(brkcmds) == nil then 134 if next(brkcmds) == nil then
119 -- nothing to do 135 -- nothing to do
120 else 136 else
121 for i = 1,#brkcmds do 137 for i = 1,#brkcmds do
122 str = gsub(str, dblbkslash * lpeg.Cs(brkcmds[i]) * cmdargs, "}%1%2%3\\arb{") 138 str = gsub(str, dblbkslash * lpeg.Cs(brkcmds[i]) * cmdargs, "\\al@brk{%1%2%3}")
123 end 139 end
124 end 140 end
125 return str 141 return str
126end 142end
127 143
128---[[
129local function holdcmd(str) 144local function holdcmd(str)
130 str = gsub(str, lpeg.Cs("\\arb") * bcbraces, function(tag, body) 145 str = gsub(str, lpeg.Cs("\\arb") * bcbraces, function(tag, body)
131 body = string.sub(body, 2, -2) 146 body = string.sub(body, 2, -2)
@@ -142,26 +157,6 @@ local function holdcmd(str)
142 str = string.gsub(str, "\\arb{}", "") 157 str = string.gsub(str, "\\arb{}", "")
143 return str 158 return str
144end 159end
145--]]
146
147--[[
148local function holdcmd(str)
149 str = string.gsub(str, "\\(arb)(%b{})", function(tag, body)
150 body = string.sub(body, 2, -2)
151 body = string.gsub(body, "\\(.-)(%b{})", function(btag, bbody)
152 bbody = string.sub(bbody, 2, -2)
153 if string.find(btag, "@") then
154 return holdcmd(string.format("}\\%s{%s}\\arb{", btag, bbody))
155 else
156 return holdcmd(string.format("}\\%s{\\arb{%s}}\\arb{", btag, bbody))
157 end
158 end)
159 return string.format("\\%s{%s}", tag, body)
160 end)
161 str = string.gsub(str, "\\arb{}", "")
162return str
163end
164--]]
165 160
166local function arbnum(str) 161local function arbnum(str)
167 str = string.gsub(str, "([0-9%,%-%/]+)", function(num) 162 str = string.gsub(str, "([0-9%,%-%/]+)", function(num)
@@ -220,7 +215,6 @@ local function takeoutcapetc(str)
220 end) 215 end)
221 str = string.gsub(str, "\\linebreak", "") 216 str = string.gsub(str, "\\linebreak", "")
222 str = string.gsub(str, "\\%-", "") 217 str = string.gsub(str, "\\%-", "")
223 str = string.gsub(str, "\\(@-)par", "\\%1par{}")
224 return str 218 return str
225end 219end
226 220
@@ -686,6 +680,7 @@ return str
686end 680end
687 681
688function newarbmark(abbr, rtlmk, ltrmk) 682function newarbmark(abbr, rtlmk, ltrmk)
683 rtlmk = "\\arabicfont "..rtlmk
689 table.insert(arbmarks, {a = abbr, b = rtlmk, c = ltrmk}) 684 table.insert(arbmarks, {a = abbr, b = rtlmk, c = ltrmk})
690 return true 685 return true
691end 686end