diff options
-rw-r--r-- | arabluatex.lua | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/arabluatex.lua b/arabluatex.lua index 8105311..e83206f 100644 --- a/arabluatex.lua +++ b/arabluatex.lua | |||
@@ -27,6 +27,58 @@ require("arabluatex_fullvoc") | |||
27 | require("arabluatex_novoc") | 27 | require("arabluatex_novoc") |
28 | require("arabluatex_trans") | 28 | require("arabluatex_trans") |
29 | 29 | ||
30 | -- arabluatex now uses lpeg | ||
31 | local original_gsub = string.gsub | ||
32 | function string.gsub(s, patt, repl) | ||
33 | if lpeg.type(patt) ~= "pattern" then | ||
34 | -- If patt isn't an LPEG pattern, revert to the normal gsub | ||
35 | return original_gsub(s, patt, repl) | ||
36 | else | ||
37 | -- Standardise repl to a function which takes the whole match | ||
38 | -- as the first argument, and then subsequent matches (if | ||
39 | -- there were any). | ||
40 | local typ = type(repl) | ||
41 | if typ == "table" then | ||
42 | local t = repl | ||
43 | repl = function(all, ...) | ||
44 | if select('#', ...) == 0 then | ||
45 | return t[all] | ||
46 | else | ||
47 | return t[...] | ||
48 | end | ||
49 | end | ||
50 | elseif typ == "string" then | ||
51 | local s = repl | ||
52 | repl = function(...) | ||
53 | local matches = {...} | ||
54 | return s:gsub("%%([0-9%%])", function(c) | ||
55 | if c == "%" then | ||
56 | return "%" | ||
57 | else | ||
58 | return matches[tonumber(c) + 1] | ||
59 | end | ||
60 | end) | ||
61 | end | ||
62 | elseif typ == "function" then | ||
63 | local f = repl | ||
64 | repl = function(all, ...) | ||
65 | if select('#', ...) == 0 then | ||
66 | return f(all) | ||
67 | else | ||
68 | return f(...) | ||
69 | end | ||
70 | end | ||
71 | else | ||
72 | error("Expected table / string / function") | ||
73 | end | ||
74 | return lpeg.Cs{ | ||
75 | (lpeg.C(patt) / repl + 1) * lpeg.V(1) + true | ||
76 | }:match(s) | ||
77 | end | ||
78 | end | ||
79 | |||
80 | P = lpeg.P | ||
81 | |||
30 | local function protectarb(str) | 82 | local function protectarb(str) |
31 | str = string.gsub(str, "(\\arb.?)(%[.-%])(%b{})", "\\@arb%2%3") | 83 | str = string.gsub(str, "(\\arb.?)(%[.-%])(%b{})", "\\@arb%2%3") |
32 | str = string.gsub(str, "(\\begin.?)(%b{})(%b[])", "\\@@begin{%2%3}") | 84 | str = string.gsub(str, "(\\begin.?)(%b{})(%b[])", "\\@@begin{%2%3}") |