From 001738e26e1b841a4042b3597ba28d9c3d04aef6 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 28 Aug 2011 14:30:54 +0200 Subject: fixed solitary substitution bug (reported by Philipp A.) --- README.rst | 4 +- mod/scripts/context/lua/mtx-rst.lua | 2 +- mod/tex/context/third/rst/rst_context.lua | 57 ++++++++++++++++------------ mod/tex/context/third/rst/rst_directives.lua | 33 ++++++++-------- mod/tex/context/third/rst/rst_helpers.lua | 19 ++++++---- mod/tex/context/third/rst/rst_parser.lua | 57 +++++++++++++++++----------- mod/tex/context/third/rst/rst_setups.lua | 15 ++++---- 7 files changed, 108 insertions(+), 79 deletions(-) diff --git a/README.rst b/README.rst index 6682a85..1ab41ce 100644 --- a/README.rst +++ b/README.rst @@ -13,9 +13,11 @@ Installation ============ In order to install |rstcontext|, copy everything under the ``mod/`` -subdirectory into your local |CONTEXT| tree, i.e.: :: +subdirectory into your local |CONTEXT| tree and update the filename database, +i.e.: :: cp -r mod/* ~/context/tex/texmf-local/ + context --generate if the |CONTEXT| minimal distribution happens to be installed at ``~/context/``. diff --git a/mod/scripts/context/lua/mtx-rst.lua b/mod/scripts/context/lua/mtx-rst.lua index 2b3841a..08782a9 100644 --- a/mod/scripts/context/lua/mtx-rst.lua +++ b/mod/scripts/context/lua/mtx-rst.lua @@ -53,7 +53,7 @@ scripts.rst.output = ea("of") if scripts.rst.input and scripts.rst.output then local expandtab = ea("et") == "true" and true local shiftwidth = ea("sw") - local debug = ea("debug") == "true" and true or false + local debug = ea("debug") == "true" if expandtab then thirddata.rst.expandtab = true end if shiftwdith then thirddata.rst.shiftwidth = tonumber(shiftwidth) end if debug then thirddata.rst_helpers.rst_debug = debug end diff --git a/mod/tex/context/third/rst/rst_context.lua b/mod/tex/context/third/rst/rst_context.lua index 3ea50af..1a86da1 100644 --- a/mod/tex/context/third/rst/rst_context.lua +++ b/mod/tex/context/third/rst/rst_context.lua @@ -18,11 +18,16 @@ local helpers = helpers or thirddata and thirddata.rst_helpers local rst_directives = rst_directives or thirddata and thirddata.rst_directives -local utf = unicode.utf8 +local utf = unicode.utf8 +local utflen = utf.len +local utflower = utf.lower +local utfupper = utf.upper +local iowrite = io.write local dbg_write = helpers.dbg_writef -local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct, P, R, S, V, match = lpeg.C, lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match +local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct, P, R, S, V, match = + lpeg.C, lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match -- This one should ignore escaped spaces. do @@ -37,10 +42,12 @@ do return stripper:match(str) or "" end end +local stringstrip = string.strip +local fmt = string.format local err = function(str) if str then - io.write("\n*[rstctx] Error: " .. str .. "\n\n") + iowrite("\n*[rstctx] Error: " .. str .. "\n\n") end end @@ -145,11 +152,11 @@ rst_context.roles.sans_serif = rst_context.roles.sans rst_context.roles.ss = rst_context.roles.sans rst_context.roles.uppercase = function(str) - return utf.upper(str) + return utfupper(str) end rst_context.roles.lowercase = function(str) - return utf.lower(str) + return utflower(str) end rst_context.roles.color = function(color, str) @@ -157,9 +164,9 @@ rst_context.roles.color = function(color, str) local definition = color:match("^color_(.+)$") if definition:match("^rgb_") then -- assume rgb local rgb = p.rgbvalues:match(definition) - definition = string.format([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3]) + definition = fmt([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3]) end - return string.format([[\\colored[%s]{%s}]], definition, str) + return fmt([[\\colored[%s]{%s}]], definition, str) end --- Inofficial text roles for my private bib @@ -259,7 +266,7 @@ function rst_context.anon_reference (str) str = str:match("^`?([^`]+)`?__$") rst_context.anonymous_links[#rst_context.anonymous_links+1] = str link = "__target_anon_" .. #rst_context.anonymous_links - return string.format([[\\RSTchoosegoto{%s}{%s}]], link, str) + return fmt([[\\RSTchoosegoto{%s}{%s}]], link, str) end local whitespace = S" \n\t\v" @@ -311,7 +318,7 @@ function rst_context.substitution_reference (str, underscores) local sub = "" rst_context.addsetups("substitutions") if underscores == "_" then -- normal reference - sub = sub .. [[\\reference[__target_]] .. rst_context.whitespace_to_underscore(string.strip(str)) .. "]{}" + sub = sub .. [[\\reference[__target_]] .. rst_context.whitespace_to_underscore(stringstrip(str)) .. "]{}" elseif underscores == "__" then -- normal reference rst_context.anonymous_targets = rst_context.anonymous_targets + 1 sub = sub .. [[\\reference[__target_anon_]] .. rst_context.anonymous_targets .. "]{}" @@ -368,7 +375,7 @@ local corresponding = { local inline_parser = P{ [1] = "block", - block = Cs(V"inline_as_first"^-1 * (V"except" + V"inline_element" + 1)^1), + block = Cs(V"inline_as_first"^-1 * (V"except" + V"inline_element" + 1)^0), inline_element = V"precede_inline" * Cs(V"inline_do_elements") @@ -612,7 +619,7 @@ function rst_context.paragraph (data) else str = data end - return string.format([[ + return fmt([[ \\startparagraph %s @@ -636,12 +643,12 @@ function rst_context.section (...) -- TODO general cleanup; move validity local tab = { ... } -- checking to parser. local section, str = true, "" local adornchar - local ulen = unicode.utf8.len + local ulen = utflen if #tab == 3 then -- TODO use unicode length with ConTeXt adornchar = tab[1]:sub(1,1) section = ulen(tab[1]) >= ulen(tab[2]) --section = get_line_pattern(adornchar):match(tab[1]) ~= nil and section - str = string.strip(tab[2]) + str = stringstrip(tab[2]) else -- no overline adornchar = tab[2]:sub(1,1) section = ulen(tab[1]) <= ulen(tab[2]) @@ -662,7 +669,7 @@ function rst_context.section (...) -- TODO general cleanup; move validity ref = get_context_reference (str) - str = string.format("\n\\\\%s[%s]{%s}\n", sectionlevels[level], ref, str) + str = fmt("\n\\\\%s[%s]{%s}\n", sectionlevels[level], ref, str) else return [[{\\bf fix your sectioning!}\\endgraf}]] end @@ -690,7 +697,7 @@ do escaped = P"\\" * V"space", nospace = V"escaped" + (1 - V"space"), } - function string.strip(str) + function stringstrip(str) return stripper:match(str) or "" end end @@ -744,7 +751,7 @@ end function rst_context.startitemize(str) local setup = "" local result = "" - str = string.strip(str) + str = stringstrip(str) local listtype = enumeration_types[str] or parse_itemstring(str) @@ -784,8 +791,8 @@ function rst_context.bullet_item (tab) if helpers.list.successor(itemtype, li[current_itemdepth]) then -- just leave it alone elseif helpers.list.greater(itemtype, li[current_itemdepth]) then - local itemnum = tonumber(string.strip(itemtype)) or helpers.list.get_decimal(itemtype) - result = result .. string.format([[ + local itemnum = tonumber(stringstrip(itemtype)) or helpers.list.get_decimal(itemtype) + result = result .. fmt([[ \\setnumber[itemgroup:itemize]{%s} ]], itemnum) end @@ -815,12 +822,12 @@ function rst_context.deflist (list) local nc = 2 local tmp = [[ - \\RSTdeflistterm{]] .. string.strip(term) .. "}" + \\RSTdeflistterm{]] .. stringstrip(term) .. "}" if #item > 2 then while nc < #item do tmp = tmp .. [[ - \\RSTdeflistclassifier{]] .. string.strip(item[nc]) .. "}" + \\RSTdeflistclassifier{]] .. stringstrip(item[nc]) .. "}" nc = nc + 1 end end @@ -868,7 +875,7 @@ end function rst_context.field (tab) local name, body = tab[1], tab[2] - return string.format([[ + return fmt([[ \\RSTfieldname{%s} \\RSTfieldbody{%s} @@ -880,7 +887,7 @@ function rst_context.line_comment (str) end function rst_context.block_comment (str) - return string.format([[ + return fmt([[ \iffalse %s\fi @@ -908,7 +915,7 @@ function rst_context.option_list (str) end function rst_context.option_item (tab) - return string.format([[\\bTR\\bTC %s \\eTC\\bTC %s \\eTC\\eTR + return fmt([[\\bTR\\bTC %s \\eTC\\bTC %s \\eTC\\eTR ]], tab[1], tab[2]) end @@ -1134,7 +1141,7 @@ function rst_context.simple_table(tab) dbg_write("%7s | ", cell.content) local celltext = inline_parser:match(cell.content) if cell.span then - head = head .. string.format([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext or "") + head = head .. fmt([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext or "") else head = head .. [[\\bTH ]] .. celltext .. [[\\eTH]] end @@ -1173,7 +1180,7 @@ function rst_context.simple_table(tab) dbg_write("%7s | ", cell.content) local celltext = inline_parser:match(cell.content) if cell.span then - body = body .. string.format([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext or "") + body = body .. fmt([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext or "") else body = body .. [[\\bTC ]] .. celltext .. [[\\eTC]] end diff --git a/mod/tex/context/third/rst/rst_directives.lua b/mod/tex/context/third/rst/rst_directives.lua index 4a2b7e3..3bc4166 100644 --- a/mod/tex/context/third/rst/rst_directives.lua +++ b/mod/tex/context/third/rst/rst_directives.lua @@ -14,9 +14,12 @@ local helpers = helpers or thirddata and thirddata.rst_helpers -- Directives for use with |substitutions| -------------------------------------------------------------------------------- -local rst_directives = { } +local rst_directives = { } thirddata.rst_directives = rst_directives -local rst_context = thirddata.rst +local rst_context = thirddata.rst + +local stringstrip = string.strip +local fmt = string.format rst_directives.anonymous = 0 rst_directives.images = {} @@ -94,7 +97,7 @@ rst_directives.image = function(name, data) end properties[key] = val else - processed = processed .. (str and str ~= "" and string.strip(str)) + processed = processed .. (str and str ~= "" and stringstrip(str)) end end end @@ -105,19 +108,19 @@ rst_directives.image = function(name, data) local images_done = rd.images.done if not anon then if not images_done[name] then - img = img .. string.format([[ + img = img .. fmt([[ \useexternalfigure[%s][%s][] ]], name, data) images_done[name] = true end - img = img .. string.format([[ + img = img .. fmt([[ \def\RSTsubstitution%s{%% \placefigure[here]{%s}{\externalfigure[%s]%s} } ]], name, rst_context.escape(inline_parser:match(properties.caption)), name, properties.setup) else -- image won't be referenced but used instantly - img = img .. string.format([[ + img = img .. fmt([[ \placefigure[here]{%s}{\externalfigure[%s]%s} ]], rst_context.escape(inline_parser:match(properties.caption)), data, properties.setup) @@ -142,7 +145,7 @@ rst_directives.caution = function(raw) end end text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text))) - return string.format([[ + return fmt([[ \startRSTcaution %s \stopRSTcaution @@ -166,7 +169,7 @@ rst_directives.danger = function(raw) end end text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text))) - return string.format([[ + return fmt([[ \startRSTdanger %s \stopRSTdanger @@ -177,9 +180,9 @@ end rst_directives.DANGER = function(addendum) local result = "" for _,str in ipairs(addendum) do - result = result .. (string.strip(str)) + result = result .. (stringstrip(str)) end - return string.format([[ + return fmt([[ %% The Rabbit of Caerbannog \startlinecorrection @@ -210,12 +213,12 @@ rst_directives.DANGER = function(addendum) end rst_directives.mp = function(name, data) - local mpcode = string.format([[ + local mpcode = fmt([[ \startreusableMPgraphic{%s} %s \stopreusableMPgraphic ]], name, data) - mpcode = mpcode .. string.format([[ + mpcode = mpcode .. fmt([[ \def\RSTsubstitution%s{%% \reuseMPgraphic{%s}%% } @@ -224,7 +227,7 @@ rst_directives.mp = function(name, data) end rst_directives.ctx = function(name, data) - local ctx = string.format([[ + local ctx = fmt([[ \startbuffer[%s] %s\stopbuffer @@ -236,7 +239,7 @@ rst_directives.ctx = function(name, data) end rst_directives.lua = function(name, data) - local luacode = string.format([[ + local luacode = fmt([[ \startbuffer[%s] \startluacode @@ -251,7 +254,7 @@ rst_directives.lua = function(name, data) end rst_directives.replace = function(name, data) - return string.format([[ + return fmt([[ \def\RSTsubstitution%s{%s} ]], name, data) diff --git a/mod/tex/context/third/rst/rst_helpers.lua b/mod/tex/context/third/rst/rst_helpers.lua index c348064..bbf1d1f 100644 --- a/mod/tex/context/third/rst/rst_helpers.lua +++ b/mod/tex/context/third/rst/rst_helpers.lua @@ -8,7 +8,6 @@ -------------------------------------------------------------------------------- -- -local utf = unicode.utf8 local P, R, S, V, match = lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match @@ -20,9 +19,15 @@ helpers = thirddata.rst_helpers helpers.table = {} helpers.cell = {} +local utf = unicode.utf8 +local utflen = utf.len + +local stringstrip = string.strip +local fmt = string.format + function helpers.dbg_writef(...) if helpers.rst_debug then - io.write(string.format(...)) + io.write(fmt(...)) end end @@ -113,7 +118,7 @@ function helpers.cell.create(raw, n_row, n_col, parent, variant) local cell = {} cell.stripped = raw and p.strip:match(raw) or "" cell.content = raw - cell.width = raw and utf.len(raw) or 0 + cell.width = raw and utflen(raw) or 0 cell.bytes = raw and #raw or 0 cell.variant = "normal" -- [normal|separator|y_continue|x_continue] cell.pos = {} @@ -131,7 +136,7 @@ function helpers.cell.get_x_span(content, layout, init) local lw = layout.widths for n=init, #lw do acc = acc + lw[n] + 1 - if utf.len(content) + 1 == acc then + if utflen(content) + 1 == acc then return n - init end end @@ -215,7 +220,7 @@ function helpers.table.create(raw) this.variant = "empty1" end else - local cellwidth = utf.len(splitpos) + local cellwidth = utflen(splitpos) if cellwidth > layoutwidth then span = span + hc.get_x_span(splitpos, newtablayout, pos_layout) end @@ -367,7 +372,7 @@ function helpers.table.simple(raw) content = "", span = { x = 1, y = 1 }, } - cell.content = string.strip(row:sub(start, stop)) + cell.content = stringstrip(row:sub(start, stop)) if check_span then local start_at, stop_at for colnr, slice in ipairs(bounds.slices) do @@ -590,7 +595,7 @@ do -- This grammar inside the function is slightly faster than the same as an upvalue -- with the value of “width” repeatedly given via lpeg.Carg(). This holds -- for repeated calls as well. - local ulen = utf.len + local ulen = utflen function helpers.string.wrapat (str, width) local width = width or 65 local linelength = 0 diff --git a/mod/tex/context/third/rst/rst_parser.lua b/mod/tex/context/third/rst/rst_parser.lua index 42d8a11..ff5d648 100644 --- a/mod/tex/context/third/rst/rst_parser.lua +++ b/mod/tex/context/third/rst/rst_parser.lua @@ -28,22 +28,35 @@ rst.expandtab = false rst.shiftwidth = 4 helpers.rst_debug = false -local warn = function(str, ...) - if not helpers.rst_debug then return false end - local slen = #str + 3 - str = "*["..str.."]" - for i,j in ipairs({...}) do - if 80 - i * 8 - slen < 0 then - local indent = "" - for i=1, slen do - indent = indent .. " " +local iowrite = io.write +local fmt = string.format +local stringlen = string.len +local stringstrip = string.strip +local utf = unicode.utf8 +local utflen = utf.len + +local warn +do + local ndebug = 0 + warn = function(str, ...) + if not helpers.rst_debug then return false end + ndebug = ndebug + 1 + local slen = #str + 3 + --str = "*["..str.."]" + str = fmt("*[%4d][%s]", ndebug, str) + for i,j in ipairs({...}) do + if 80 - i * 8 - slen < 0 then + local indent = "" + for i=1, slen do + indent = indent .. " " + end + str = str .. "\n" .. indent end - str = str .. "\n" .. indent + str = str .. fmt(" |%6s", stringstrip(tostring(j))) end - str = str .. string.format(" |%6s", string.strip(tostring(j))) + iowrite(str .. " |\n") + return 0 end - iowrite(str .. " |\n") - return 0 end local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct @@ -54,8 +67,6 @@ local P, R, S, V, match local utf = unicode.utf8 -local iowrite = io.write - local eol = P"\n" state = {} @@ -544,7 +555,7 @@ local parser = P{ block_quote_attri_first = Cmt(V"space"^1 * V"attrib_dash" * V"space", function (s, i, indent) local t = state - warn("bqa-i", utf.len(indent), #t.currentindent, + warn("bqa-i", utflen(indent), #t.currentindent, indent, t.currentindent, i) local ret = indent:match(" *") == t.currentindent t.currentindent = ret and indent or t.currentindent @@ -555,9 +566,9 @@ local parser = P{ , block_quote_attri_other = Cmt(V"space"^1, function (s, i, indent) - warn("bqa-m", #indent, utf.len(state.currentindent), + warn("bqa-m", #indent, utflen(state.currentindent), indent, state.currentindent, i) - return utf.len(state.currentindent) == #indent + return utflen(state.currentindent) == #indent end) / "" * (1 - V"eol")^1 * V"eol" @@ -881,7 +892,7 @@ local parser = P{ t.bullets.max = t.bullets.max < t.depth and t.depth or t.bullets.max return true elseif t.depth > 0 and n_spaces > 1 then -- sublist (of sublist)^0 - if n_spaces >= utf.len(oldbullet) then + if n_spaces >= utflen(oldbullet) then t.lastbullets[t.depth] = t.lastbullet t.depth = t.depth + 1 t.bullets[t.depth] = bullet @@ -911,7 +922,7 @@ local parser = P{ conversion(bullet) ) - if utf.len(t.bullets[t.depth]) ~= utf.len(bullet) then + if utflen(t.bullets[t.depth]) ~= utflen(bullet) then return false elseif not conversion(bullet) and t.bullets[t.depth] == bullet then return true @@ -944,9 +955,9 @@ local parser = P{ local t = state warn("match", t.depth, - string.len(this) == utf.len(t.bullets[t.depth]), - utf.len(t.bullets[t.depth]), string.len(this) ) - return string.len(this) == utf.len(t.bullets[t.depth]) + stringlen(this) == utflen(t.bullets[t.depth]), + utflen(t.bullets[t.depth]), stringlen(this) ) + return stringlen(this) == utflen(t.bullets[t.depth]) end) , diff --git a/mod/tex/context/third/rst/rst_setups.lua b/mod/tex/context/third/rst/rst_setups.lua index 7502621..08f21c5 100644 --- a/mod/tex/context/third/rst/rst_setups.lua +++ b/mod/tex/context/third/rst/rst_setups.lua @@ -8,12 +8,13 @@ -------------------------------------------------------------------------------- -- ---local rst_directives = context and thirddata.rst_directives or require "rst_directives" local optional_setups = { } thirddata.rst_setups = optional_setups local rst_directives = thirddata.rst_directives local rst_context = thirddata.rst - --rst_directives = require "rst_directives" + +local fmt = string.format +local stringstrip = string.strip function optional_setups.footnote_symbol () local setup = [[ @@ -42,13 +43,13 @@ function optional_setups.footnotes () ]] for nf, note in next, tf.numbered do - fn = fn .. string.format(buffer, "Autonumbered footnote", "__footnote_number_"..nf, note) + fn = fn .. fmt(buffer, "Autonumbered footnote", "__footnote_number_"..nf, note) end for nf, note in next, tf.autolabel do - fn = fn .. string.format(buffer, "Labeled footnote", "__footnote_label_"..nf, note) + fn = fn .. fmt(buffer, "Labeled footnote", "__footnote_label_"..nf, note) end for nf, note in next, tf.symbol do - fn = fn .. string.format(buffer, "Symbol footnote", "__footnote_symbol_"..nf, note) + fn = fn .. fmt(buffer, "Symbol footnote", "__footnote_symbol_"..nf, note) end return fn end @@ -95,7 +96,7 @@ function optional_setups.references () if arefs[ref_text] then ref_text = rst_context.anonymous_links[tonumber(arefs[ref_text])] end - references[#references+1] = string.format([[ + references[#references+1] = fmt([[ \useURL[__target_%s] [%s] [] [%s] ]], rst_context.whitespace_to_underscore(ref), urlescape(target), ref_text) end end @@ -125,7 +126,7 @@ function optional_setups.substitutions () local rs = rst_context.substitutions for name, content in next, rs do local directive, data = content.directive, content.data - name, data = name:gsub("%s", ""), string.strip(data) + name, data = name:gsub("%s", ""), stringstrip(data) if directives[directive] then substitutions = substitutions .. directives[directive](name, data) else -- cgit v1.2.3