diff options
Diffstat (limited to 'mod')
-rw-r--r-- | mod/tex/context/third/rst/rst_context.lua | 27 | ||||
-rw-r--r-- | mod/tex/context/third/rst/rst_helpers.lua | 93 | ||||
-rw-r--r-- | mod/tex/context/third/rst/rst_parser.lua | 6 |
3 files changed, 78 insertions, 48 deletions
diff --git a/mod/tex/context/third/rst/rst_context.lua b/mod/tex/context/third/rst/rst_context.lua index a5fb603..f880f35 100644 --- a/mod/tex/context/third/rst/rst_context.lua +++ b/mod/tex/context/third/rst/rst_context.lua @@ -844,7 +844,8 @@ function rst_context.deflist (list) local deflist = [[ \\startRSTdefinitionlist ]] - for nd, item in ipairs(list) do + for nd=1, #list do + local item = list[nd] local term = item[1] local nc = 2 local tmp = [[ @@ -862,7 +863,9 @@ function rst_context.deflist (list) \\RSTdeflistdefinition{% ]] - for np, par in ipairs(item[#item]) do -- definitions, multi-paragraph + local final = item[#item] + for np=1, #final do + local par = final[np] tmp = tmp .. [[ \\RSTdeflistparagraph{% ]] .. inline_parser:match(par) .. "}\n" @@ -1056,9 +1059,9 @@ function rst_context.grid_table (tab) ]] while nr <= tab.head_end do local r = tab.rows[nr] - --for i,r in ipairs(tab.rows) do local isempty = true - for n, cell in ipairs(r) do + for n=1, #r do + local cell = r[n] if cell.variant == "normal" then isempty = false break @@ -1067,7 +1070,8 @@ function rst_context.grid_table (tab) if not isempty then local row = [[\\bTR]] - for n,c in ipairs(r) do + for n=1, #r do + local c = r[n] if not (c.parent or c.variant == "separator") then local celltext = inline_parser:match(c.stripped) @@ -1105,9 +1109,9 @@ function rst_context.grid_table (tab) end while nr <= #tab.rows do local r = tab.rows[nr] - --for i,r in ipairs(tab.rows) do local isempty = true - for n, cell in ipairs(r) do + for n=1, #r do + local cell = r[n] if cell.variant == "normal" then isempty = false break @@ -1116,7 +1120,8 @@ function rst_context.grid_table (tab) if not isempty then local row = [[\\bTR]] - for n,c in ipairs(r) do + for n=1, #r do + local c = r[n] if not (c.parent or c.variant == "separator") then local celltext = inline_parser:match(c.stripped) @@ -1164,7 +1169,8 @@ function rst_context.simple_table(tab) if not row.ignore then dbg_write(">hr>" .. #row) head = head .. [[\\bTR]] - for nc,cell in ipairs(row) do + for nc=1, #row do + local cell = row[nc] dbg_write("%7s | ", cell.content) local celltext = inline_parser:match(cell.content) if cell.span then @@ -1203,7 +1209,8 @@ function rst_context.simple_table(tab) if not row.ignore then dbg_write(">tr>" .. #row) body = body .. [[\\bTR]] - for nc,cell in ipairs(row) do + for nc=1, #row do + local cell = row[nc] dbg_write("%7s | ", cell.content) local celltext = inline_parser:match(cell.content) if cell.span then diff --git a/mod/tex/context/third/rst/rst_helpers.lua b/mod/tex/context/third/rst/rst_helpers.lua index 3ff53fe..936da97 100644 --- a/mod/tex/context/third/rst/rst_helpers.lua +++ b/mod/tex/context/third/rst/rst_helpers.lua @@ -8,7 +8,7 @@ -------------------------------------------------------------------------------- -- -local P, R, S, V, match +local P, R, S, V, lpegmatch = lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match local C, Carg, Cb, Cc, Cg, @@ -58,8 +58,8 @@ do p.col_start = Cp() * p.dash_or_equals^1 p.col_stop = p.dash_or_equals^1 * Cp() - p.column_starts = p.col_start * ( p.space^1 * p.col_start)^1 - p.column_stops = p.col_stop * ( p.space^1 * p.col_stop)^1 + p.column_starts = Ct(p.col_start * ( p.space^1 * p.col_start)^1) + p.column_stops = Ct(p.col_stop * ( p.space^1 * p.col_stop)^1) p.st_headsep = p.equals^1 * (p.space^1 * p.equals^1)^1 p.st_colspan = p.dash^1 * (p.space^1 * p.dash^1)^0 * p.space^0 * p.last @@ -159,7 +159,9 @@ local function set_layout (line) layout.widths = {} layout.slices = {} - for n, elm in ipairs(slice:match(line)) do + local elms = lpegmatch(slice, line) + for n=1, #elms do + local elm = elms[n] layout.widths[n] = #elm layout.slices[n] = elm end @@ -187,7 +189,8 @@ function helpers.table.create(raw) local hc = helpers.cell local rowcount = 0 local newtablayout = newtab.layout - for nr, row in ipairs(raw) do + for nr=1, #raw do + local row = raw[nr] newtab.rows[nr] = {} local this_row = newtab.rows[nr] this_row.sepline = p.sep_line:match(row) @@ -244,11 +247,13 @@ function helpers.table.create(raw) local oldrows = newtab.rows local newrows = oldrows - for nc, width in ipairs(newtablayout.widths) do + for nc=1, #newtablayout.widths do + local width = newtablayout.widths[nc] -- this is gonna be extremely slow but at least it's readable local newrow local currentrow = 1 - for nr, row in ipairs(newrows) do + for nr=1, #newrows do + local row = newrows[nr] local cell = row[nc] dbg_write("nc: %s, nr:%2s | %9s | ", nc, nr,cell.variant) if row.sepline or row.sephead @@ -300,18 +305,22 @@ function helpers.table.create(raw) newtab.__init() - --newtab.__draw_debug = function() - --for nr, row in ipairs(newtab.rows) do - --for nc, cell in ipairs(row) do - --local field = cell.variant:sub(1,7) - --if cell.parent then - --field = field .. string.format(" %s,%2s",cell.parent.x, cell.parent.y) - --end - --dbg_write("%12s | ", field) - --end - --dbg_write("\n") - --end - --end +--[[ + newtab.__draw_debug = function() + for nr=1, #newtab.rows do + local row = newtab.rows[nr] + for nc=1, #row do + local cell = row[nc] + local field = cell.variant:sub(1,7) + if cell.parent then + field = field .. string.format(" %s,%2s",cell.parent.x, cell.parent.y) + end + dbg_write("%12s | ", field) + end + dbg_write("\n") + end + end +--]] return newtab end @@ -330,13 +339,20 @@ end -- Check the column boundaries of a simple table. function helpers.get_st_boundaries (str) - local p = helpers.patterns - local starts, stops, slices = {}, {}, {} - for n, elm in ipairs({ p.column_starts:match(str) }) do + local p_column_starts = helpers.patterns.column_starts + local p_column_stops = helpers.patterns.column_stops + local starts, stops, slices, elms = { }, { }, { }, nil + + elms = lpegmatch(p_column_starts, str) + for n=1, #elms do + local elm = elms[n] slices[n] = { start = elm } starts[elm] = true end - for n, elm in ipairs({ p.column_stops :match(str) }) do + + elms = lpegmatch(p_column_stops, str) + for n=1, #elms do + local elm = elms[n] slices[n]["stop"] = elm stops[elm] = true end @@ -349,9 +365,9 @@ function helpers.table.simple(raw) local bounds = helpers.get_st_boundaries(raw[1]) local p = helpers.patterns - for nr, row in ipairs(raw) do + for nr=1, #raw do + local row = raw[nr] local newrow = {} - local nc = 1 if not p.st_headsep:match(row) and not p.st_colspan:match(row) then local starts, stops = {}, {} @@ -361,13 +377,15 @@ function helpers.table.simple(raw) stops = p.st_span_stops :match(raw[nr+1]) check_span = true else - for colnr, slice in ipairs(bounds.slices) do - starts[colnr] = slice.start - stops [colnr] = slice.stop + for ncol=1, #bounds.slices do + local slice = bounds.slices[ncol] + starts[ncol] = slice.start + stops [ncol] = slice.stop end end - for nc, start in ipairs(starts) do + for nc=1, #starts do + local start = starts[nc] -- last column can exceed layout width local stop = nc ~= #starts and stops[nc] or #row local cell = { @@ -377,18 +395,19 @@ function helpers.table.simple(raw) cell.content = stringstrip(row:sub(start, stop)) if check_span then local start_at, stop_at - for colnr, slice in ipairs(bounds.slices) do + for ncol=1, #bounds.slices do + local slice = bounds.slices[ncol] if slice.start == start then - start_at = colnr + start_at = ncol end if start_at and - not (colnr == #bounds.slices) then + not (ncol == #bounds.slices) then if slice.stop == stop then - stop_at = colnr + stop_at = ncol break end else -- last column, width doesn't matter - stop_at = colnr + stop_at = ncol end end cell.span.x = 1 + stop_at - start_at @@ -408,10 +427,12 @@ function helpers.table.simple(raw) rows[nr] = newrow end - for nr, row in ipairs(rows) do + for nr=1, #rows do + local row = rows[nr] if not row.ignore and row[1].content == "" then row.ignore = true - for nc, cell in ipairs(row) do + for nc=1, #row do + local cell = row[nc] local par_row, par_col = helpers.table.resolve_parent(nr - 1, nc, rows) parent = rows[par_row][par_col] parent.content = parent.content .. " " .. cell.content diff --git a/mod/tex/context/third/rst/rst_parser.lua b/mod/tex/context/third/rst/rst_parser.lua index d0d8de9..27c77e2 100644 --- a/mod/tex/context/third/rst/rst_parser.lua +++ b/mod/tex/context/third/rst/rst_parser.lua @@ -45,7 +45,9 @@ do local slen = #str + 3 --str = "*["..str.."]" str = stringformat("*[%4d][%s]", ndebug, str) - for i,j in ipairs({...}) do + local arglst = { ... } + for i=1, #arglst do + local current = arglst[i] if 80 - i * 8 - slen < 0 then local indent = "" for i=1, slen do @@ -53,7 +55,7 @@ do end str = str .. "\n" .. indent end - str = str .. stringformat(" |%6s", stringstrip(tostring(j))) + str = str .. stringformat(" |%6s", stringstrip(tostring(current))) end iowrite(str .. " |\n") return 0 |