diff options
-rw-r--r-- | arabluatex.lua | 97 |
1 files changed, 31 insertions, 66 deletions
diff --git a/arabluatex.lua b/arabluatex.lua index e83206f..7db1cc8 100644 --- a/arabluatex.lua +++ b/arabluatex.lua | |||
@@ -27,58 +27,13 @@ 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 | 30 | -- lpeg equivalent for string.gsub() |
31 | local original_gsub = string.gsub | 31 | local function gsub(s, patt, repl) |
32 | function string.gsub(s, patt, repl) | 32 | patt = lpeg.P(patt) |
33 | if lpeg.type(patt) ~= "pattern" then | 33 | patt = lpeg.Cs((patt / repl + 1)^0) |
34 | -- If patt isn't an LPEG pattern, revert to the normal gsub | 34 | return lpeg.match(patt, s) |
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 | 35 | end |
79 | 36 | ||
80 | P = lpeg.P | ||
81 | |||
82 | local function protectarb(str) | 37 | local function protectarb(str) |
83 | str = string.gsub(str, "(\\arb.?)(%[.-%])(%b{})", "\\@arb%2%3") | 38 | str = string.gsub(str, "(\\arb.?)(%[.-%])(%b{})", "\\@arb%2%3") |
84 | str = string.gsub(str, "(\\begin.?)(%b{})(%b[])", "\\@@begin{%2%3}") | 39 | str = string.gsub(str, "(\\begin.?)(%b{})(%b[])", "\\@@begin{%2%3}") |
@@ -98,7 +53,14 @@ local function unprotectarb(str) | |||
98 | return str | 53 | return str |
99 | end | 54 | end |
100 | 55 | ||
101 | brkcmds = {} | 56 | -- 'albrkcmds' is what is set by default. 'brkcmds' is what may be |
57 | -- set in the preamble as argument of \MkArbBreak{} | ||
58 | local albrkcmds = { | ||
59 | "Footnote", | ||
60 | "Marginpar", | ||
61 | "arbmark" | ||
62 | } | ||
63 | local brkcmds = {} | ||
102 | 64 | ||
103 | function mkarbbreak(str) | 65 | function mkarbbreak(str) |
104 | str = str .."," | 66 | str = str .."," |
@@ -112,34 +74,33 @@ function mkarbbreak(str) | |||
112 | return brkcmds | 74 | return brkcmds |
113 | end | 75 | end |
114 | 76 | ||
77 | -- some basic patterns: | ||
78 | local dblbkslash = lpeg.Cs("\\") | ||
79 | local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" } | ||
80 | local bcbraces = lpeg.Cs{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" } | ||
81 | local spce = lpeg.P(" ") | ||
82 | local bsqbracketsii = lpeg.Cs(bsqbrackets^-2) | ||
83 | local bcbracesii = lpeg.Cs(spce^-1 * bcbraces * spce^-1 * bcbraces^-1) | ||
84 | |||
115 | local function breakcmd(str) | 85 | local function breakcmd(str) |
86 | for i = 1,#albrkcmds do | ||
87 | str = gsub(str, dblbkslash * lpeg.Cs(albrkcmds[i]) * spce^-1 * bsqbracketsii * bcbracesii, "}%1%2%3%4\\arb{") | ||
88 | end | ||
116 | -- user commands | 89 | -- user commands |
117 | if next(brkcmds) == nil then | 90 | if next(brkcmds) == nil then |
118 | -- nothing to do | 91 | -- nothing to do |
119 | else | 92 | else |
120 | for i = 1,#brkcmds do | 93 | for i = 1,#brkcmds do |
121 | str = string.gsub(str, "\\"..brkcmds[i].."%s?(%b[])(%b{})", | 94 | str = gsub(str, dblbkslash * lpeg.Cs(brkcmds[i]) * spce^-1 * bsqbracketsii * bcbracesii, "}%1%2%3%4\\arb{") |
122 | function(opt, body) | ||
123 | opt = string.sub(opt, 2, -2) | ||
124 | body = string.sub(body, 2, -2) | ||
125 | return string.format("}\\"..brkcmds[i].."[%s]{%s}\\arb{", opt, body) | ||
126 | end) | ||
127 | end | ||
128 | for i = 1,#brkcmds do | ||
129 | str = string.gsub(str, "\\"..brkcmds[i].."%s?(%b{})", | ||
130 | function(body) | ||
131 | body = string.sub(body, 2, -2) | ||
132 | return string.format("}\\"..brkcmds[i].."{%s}\\arb{", body) | ||
133 | end) | ||
134 | end | 95 | end |
135 | end | 96 | end |
136 | -- process \item[], then \item[] | 97 | -- process \item[], then \item[] |
137 | str = string.gsub(str, "\\(item.?)(%b[])", | 98 | str = string.gsub(str, "\\(item.?)(%b[])", |
138 | function(tag, body) | 99 | function(tag, body) |
139 | body = string.sub(body, 2, -2) | 100 | body = string.sub(body, 2, -2) |
140 | return string.format("}\\item[\\arb{%s}] \\arb{", body) | 101 | return string.format("}\\item[\\arb{%s}] \\arb{", body) |
141 | end) | 102 | end) |
142 | str = string.gsub(str, "(\\item)(%s+)", "%1{}%2") | 103 | str = string.gsub(str, "(\\item)(%s+)", "%1{}%2") |
143 | -- \edtext | 104 | -- \edtext |
144 | str = string.gsub(str, "\\(edtext.-)(%b{})(%b{})", | 105 | str = string.gsub(str, "\\(edtext.-)(%b{})(%b{})", |
145 | function(tag, bodylem, bodyvar) | 106 | function(tag, bodylem, bodyvar) |
@@ -166,6 +127,7 @@ local function breakcmd(str) | |||
166 | bodytext = string.sub(bodytext, 2, -2) | 127 | bodytext = string.sub(bodytext, 2, -2) |
167 | return string.format("}\\%s{%s}{\\arb{%s}}\\arb{", tag, bodycolor, bodytext) | 128 | return string.format("}\\%s{%s}{\\arb{%s}}\\arb{", tag, bodycolor, bodytext) |
168 | end) | 129 | end) |
130 | --[[ | ||
169 | -- Footnote | 131 | -- Footnote |
170 | str = string.gsub(str, "\\(Footnote.-)(%b{})", | 132 | str = string.gsub(str, "\\(Footnote.-)(%b{})", |
171 | function(tag, body) | 133 | function(tag, body) |
@@ -178,18 +140,21 @@ local function breakcmd(str) | |||
178 | body = string.sub(body, 2, -2) | 140 | body = string.sub(body, 2, -2) |
179 | return string.format("}\\%s{%s}\\arb{", tag, body) | 141 | return string.format("}\\%s{%s}\\arb{", tag, body) |
180 | end) | 142 | end) |
143 | --]] | ||
181 | -- Abjad | 144 | -- Abjad |
182 | str = string.gsub(str, "\\(abjad.-)(%b{})", | 145 | str = string.gsub(str, "\\(abjad.-)(%b{})", |
183 | function(tag, body) | 146 | function(tag, body) |
184 | body = string.sub(body, 2, -2) | 147 | body = string.sub(body, 2, -2) |
185 | return string.format("}\\aemph{\\%s{%s}}\\arb{", tag, body) | 148 | return string.format("}\\aemph{\\%s{%s}}\\arb{", tag, body) |
186 | end) | 149 | end) |
150 | --[[ | ||
187 | -- Arbmark | 151 | -- Arbmark |
188 | str = string.gsub(str, "\\(arbmark.-)(%b{})", | 152 | str = string.gsub(str, "\\(arbmark.-)(%b{})", |
189 | function(tag, body) | 153 | function(tag, body) |
190 | body = string.sub(body, 2, -2) | 154 | body = string.sub(body, 2, -2) |
191 | return string.format("}\\%s{%s}\\arb{", tag, body) | 155 | return string.format("}\\%s{%s}\\arb{", tag, body) |
192 | end) | 156 | end) |
157 | --]] | ||
193 | return str | 158 | return str |
194 | end | 159 | end |
195 | 160 | ||