diff options
author | Robert Alessi <alessi@robertalessi.net> | 2018-06-09 08:09:17 +0200 |
---|---|---|
committer | Robert Alessi <alessi@robertalessi.net> | 2018-06-09 08:38:46 +0200 |
commit | 4598ab9c7ed597b70726baaab9e30645562b5931 (patch) | |
tree | 4b32f006496fb750491c1524ba1c4a256c9b3753 | |
parent | 3dae8039c3b840733d8b8896f49a6043c6c826a3 (diff) | |
download | arabluatex-4598ab9c7ed597b70726baaab9e30645562b5931.tar.gz |
huge commit: improved LPeg patterns, (un)protectarb() now operates on \arb[]{}, LR{}/LR{} and any command found in (al)brkcmds tables, which withholds the execution of the 'breaking' mechanism until holdcmd() function operates.
improved holdcmd() with LPeg patterns: commands with up to two optional arguments are now processed. moved back \par into albrkcmds.
-rw-r--r-- | arabluatex.lua | 87 |
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) |
35 | end | 35 | end |
36 | 36 | ||
37 | -- some basic patterns: | ||
38 | local ascii = lpeg.R("az", "AZ", "@@") | ||
39 | local dblbkslash = lpeg.Cs("\\") | ||
40 | local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" } | ||
41 | local bcbraces = lpeg.Cs{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" } | ||
42 | local spce = lpeg.Cs(" ") | ||
43 | local spcenc = lpeg.P(" ") | ||
44 | local cmdstar = lpeg.Cs(spce * lpeg.P("*")) | ||
45 | local bsqbracketsii = lpeg.Cs(bsqbrackets^-2) | ||
46 | local bcbracesii = lpeg.Cs(bcbraces^-2) | ||
47 | local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1) | ||
48 | local cmdargs = lpeg.Cs(spce * bsqbracketsii * bcbracesii * bsqbrackets^-1) | ||
49 | |||
37 | local function protectarb(str) | 50 | local 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") |
39 | return str | 52 | str = string.gsub(str, "(\\LR%s?)(%b{})", "\\@LR%2") |
53 | str = string.gsub(str, "(\\RL%s?)(%b{})", "\\@RL%2") | ||
54 | return str | ||
40 | end | 55 | end |
41 | 56 | ||
42 | local function unprotectarb(str) | 57 | local function unprotectarb(str) |
43 | str = string.gsub(str, "(\\@arb)(%[.-%])(%b{})", "\\arb%2%3") | 58 | str = string.gsub(str, "(\\@arb)(%[.-%])(%b{})", "\\arb%2%3") |
44 | return 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 | ||
45 | end | 66 | end |
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 | |||
50 | local albrkcmds = { | 71 | local 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 |
77 | end | 95 | end |
78 | 96 | ||
79 | -- some basic patterns: | ||
80 | local ascii = lpeg.R("az", "AZ", "@@") | ||
81 | local dblbkslash = lpeg.Cs("\\") | ||
82 | local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" } | ||
83 | local bcbraces = lpeg.Cs{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" } | ||
84 | local spce = lpeg.Cs(" ") | ||
85 | local spcenc = lpeg.P(" ") | ||
86 | local cmdstar = lpeg.Cs(spce * lpeg.P("*")) | ||
87 | local bsqbracketsii = lpeg.Cs(bsqbrackets^-2) | ||
88 | local bcbracesii = lpeg.Cs(bcbraces^-2) | ||
89 | local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1) | ||
90 | local cmdargs = lpeg.Cs(spce * bsqbracketsii * bcbracesii * bsqbrackets^-1) | ||
91 | |||
92 | local function breakcmd(str) | 97 | local 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 |
126 | end | 142 | end |
127 | 143 | ||
128 | ---[[ | ||
129 | local function holdcmd(str) | 144 | local 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 |
144 | end | 159 | end |
145 | --]] | ||
146 | |||
147 | --[[ | ||
148 | local 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{}", "") | ||
162 | return str | ||
163 | end | ||
164 | --]] | ||
165 | 160 | ||
166 | local function arbnum(str) | 161 | local 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 |
225 | end | 219 | end |
226 | 220 | ||
@@ -686,6 +680,7 @@ return str | |||
686 | end | 680 | end |
687 | 681 | ||
688 | function newarbmark(abbr, rtlmk, ltrmk) | 682 | function 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 |
691 | end | 686 | end |