From 98ce4d0b16045a6dad0306c79f8dce6478e5d1ea Mon Sep 17 00:00:00 2001 From: Robert Alessi Date: Sat, 26 May 2018 19:51:31 +0200 Subject: breakcmd() now uses LPeg patterns. commands with up to two optional and two mandatory arguments are now processed by \MkArbBreak{} --- arabluatex.lua | 97 +++++++++++++++++++--------------------------------------- 1 file changed, 31 insertions(+), 66 deletions(-) (limited to 'arabluatex.lua') 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") require("arabluatex_novoc") require("arabluatex_trans") --- arabluatex now uses lpeg -local original_gsub = string.gsub -function string.gsub(s, patt, repl) - if lpeg.type(patt) ~= "pattern" then - -- If patt isn't an LPEG pattern, revert to the normal gsub - return original_gsub(s, patt, repl) - else - -- Standardise repl to a function which takes the whole match - -- as the first argument, and then subsequent matches (if - -- there were any). - local typ = type(repl) - if typ == "table" then - local t = repl - repl = function(all, ...) - if select('#', ...) == 0 then - return t[all] - else - return t[...] - end - end - elseif typ == "string" then - local s = repl - repl = function(...) - local matches = {...} - return s:gsub("%%([0-9%%])", function(c) - if c == "%" then - return "%" - else - return matches[tonumber(c) + 1] - end - end) - end - elseif typ == "function" then - local f = repl - repl = function(all, ...) - if select('#', ...) == 0 then - return f(all) - else - return f(...) - end - end - else - error("Expected table / string / function") - end - return lpeg.Cs{ - (lpeg.C(patt) / repl + 1) * lpeg.V(1) + true - }:match(s) - end +-- 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 -P = lpeg.P - local function protectarb(str) str = string.gsub(str, "(\\arb.?)(%[.-%])(%b{})", "\\@arb%2%3") str = string.gsub(str, "(\\begin.?)(%b{})(%b[])", "\\@@begin{%2%3}") @@ -98,7 +53,14 @@ local function unprotectarb(str) return str end -brkcmds = {} +-- 'albrkcmds' is what is set by default. 'brkcmds' is what may be +-- set in the preamble as argument of \MkArbBreak{} +local albrkcmds = { + "Footnote", + "Marginpar", + "arbmark" +} +local brkcmds = {} function mkarbbreak(str) str = str .."," @@ -112,34 +74,33 @@ function mkarbbreak(str) return brkcmds end +-- some basic patterns: +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.P(" ") +local bsqbracketsii = lpeg.Cs(bsqbrackets^-2) +local bcbracesii = lpeg.Cs(spce^-1 * bcbraces * spce^-1 * bcbraces^-1) + local function breakcmd(str) + for i = 1,#albrkcmds do + str = gsub(str, dblbkslash * lpeg.Cs(albrkcmds[i]) * spce^-1 * bsqbracketsii * bcbracesii, "}%1%2%3%4\\arb{") + end -- user commands if next(brkcmds) == nil then -- nothing to do else for i = 1,#brkcmds do - str = string.gsub(str, "\\"..brkcmds[i].."%s?(%b[])(%b{})", - function(opt, body) - opt = string.sub(opt, 2, -2) - body = string.sub(body, 2, -2) - return string.format("}\\"..brkcmds[i].."[%s]{%s}\\arb{", opt, body) - end) - end - 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) + str = gsub(str, dblbkslash * lpeg.Cs(brkcmds[i]) * spce^-1 * bsqbracketsii * bcbracesii, "}%1%2%3%4\\arb{") end end -- process \item[], then \item[] - str = string.gsub(str, "\\(item.?)(%b[])", + str = string.gsub(str, "\\(item.?)(%b[])", function(tag, body) body = string.sub(body, 2, -2) return string.format("}\\item[\\arb{%s}] \\arb{", body) end) - str = string.gsub(str, "(\\item)(%s+)", "%1{}%2") + str = string.gsub(str, "(\\item)(%s+)", "%1{}%2") -- \edtext str = string.gsub(str, "\\(edtext.-)(%b{})(%b{})", function(tag, bodylem, bodyvar) @@ -166,6 +127,7 @@ local function breakcmd(str) 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) @@ -178,18 +140,21 @@ local function breakcmd(str) 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) end) +--]] return str end -- cgit v1.2.3