diff options
-rw-r--r-- | rst_context.lua | 16 | ||||
-rw-r--r-- | rst_helpers.lua | 114 |
2 files changed, 62 insertions, 68 deletions
diff --git a/rst_context.lua b/rst_context.lua index 863f2da..1f49bff 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -10,6 +10,12 @@ -- CREATED: 31/08/10 19:35:15 CEST -------------------------------------------------------------------------------- -- +--- TODO +-- - Find an appropriate way to handle generic tables irrespective of the grid +-- settings. The problem is: +-- http://archive.contextgarden.net/message/20100912.112605.8a1aaf13.en.html +-- Seems we'll have to choose either the grid or split tables as default. Not +-- good. require "lpeg" @@ -724,7 +730,7 @@ end function rst_context.simple_table(tab) local head local nr = 1 - if tab.has_head then + if tab.head_end then head = [[ \\setupTABLE[c][each] [frame=on] \\setupTABLE[r][each] [frame=on] @@ -734,13 +740,13 @@ function rst_context.simple_table(tab) ]] while nr <= tab.head_end do local row = tab[nr] - if not row.separator and not row.ignore then + if not row.ignore then dbg_write(">hr>" .. #row) head = head .. [[\\bTR]] for nc,cell in ipairs(row) do dbg_write("%7s | ", cell.content) if cell.span then - head = head .. string.format([=[\\bTH[nc=%s]%s\\eTH]=], cell.span, cell.content) + head = head .. string.format([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, cell.content) else head = head .. [[\\bTH ]] .. cell.content .. [[\\eTH]] end @@ -773,13 +779,13 @@ function rst_context.simple_table(tab) local body = "" while nr <= #tab do local row = tab[nr] - if not row.separator and not row.ignore then + if not row.ignore then dbg_write(">tr>" .. #row) body = body .. [[\\bTR]] for nc,cell in ipairs(row) do dbg_write("%7s | ", cell.content) if cell.span then - body = body .. string.format([=[\\bTC[nc=%s]%s\\eTC]=], cell.span, cell.content) + body = body .. string.format([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, cell.content) else body = body .. [[\\bTC ]] .. cell.content .. [[\\eTC]] end diff --git a/rst_helpers.lua b/rst_helpers.lua index d80fdbd..7598698 100644 --- a/rst_helpers.lua +++ b/rst_helpers.lua @@ -47,7 +47,8 @@ do p.cellcontent = (1 - p.celldelim) p.cell = p.celldelim * C((1 - p.celldelim)^1) * #p.celldelim p.cell_line = p.plus * p.dash^1 * #p.plus - p.dashesonly = p.dash^1 * p.last + p.dashesonly = p.dash^1 * p.last + p.spacesonly = p.space^1 * p.last p.col_start = Cp() * p.dash_or_equals^1 p.col_stop = p.dash_or_equals^1 * Cp() @@ -59,6 +60,7 @@ do p.st_span_starts = Ct(Cp() * p.dash^1 * (p.space^1 * Cp() * p.dash^1)^0) p.st_span_stops = Ct(p.dash^1 * Cp() * (p.space^1 * p.dash^1 * Cp())^0) + p.cells = P{ [1] = "cells", cells = p.celldelim @@ -500,77 +502,63 @@ function helpers.table.simple(raw) local bounds = helpers.get_st_boundaries(raw[1]) local p = patterns - for nr=1, #raw do - local row = raw[nr] - if p.st_colspan:match(row) then - local spans = {} - spans.starts = p.st_span_starts:match(row) - spans.stops = p.st_span_stops :match(row) - local all_match = true - for n=1, #spans.starts do -- check if they match the table layout - local start = spans.starts[n] - local stop = spans.stops[n] - if not bounds.starts[start] or - not bounds.stops[stop] then - all_match = false - break + for nr, row in ipairs(raw) do + local newrow = {} + local nc = 1 + if not p.st_headsep:match(row) and + not p.st_colspan:match(row) then + local starts, stops = {}, {} + local check_span = false + if p.st_colspan:match(raw[nr+1]) then -- expect spans over several columns + starts = p.st_span_starts:match(raw[nr+1]) + 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 end end - if all_match then - spans.cols = {} - for n, start in ipairs(spans.starts) do -- find the start and end columns - local stop = spans.stops[n] - local starts_at, stops_at - for o=1, #bounds.slices do - if bounds.slices[o].start == start then - starts_at = o + + for nc, start in ipairs(starts) do + local stop = stops[nc] + local cell = { + content = "", + span = { x = 1, y = 1 }, + --ignore = false + } + cell.content = string.strip(row:sub(start, stop)) + if check_span then + local start_at, stop_at + for colnr, slice in ipairs(bounds.slices) do + print(start, slice.start, stop, slice.stop) + if slice.start == start then + start_at = colnr end - if bounds.slices[o].stop == stop then - stops_at = o + if start_at and + not (colnr == #bounds.slices) then + if slice.stop == stop then + stop_at = colnr + break + end + else -- last column, width doesn't matter + stop_at = colnr end end - spans.cols[n] = 1 + stops_at - starts_at + cell.span.x = 1 + stop_at - start_at + print(start_at, stop_at, cell.span.x) end - multispans[nr-1] = spans + newrow[nc] = cell end - end - end - - for nr, row in ipairs(raw) do - local newrow = {} - if p.st_headsep:match(row) then - newrow.separator = true - if nr > 1 and #raw > nr then -- Ends the header. - rows.has_head = true - rows.head_end = nr - end - elseif multispans[nr] then - local spans = multispans[nr] - for nc, span in ipairs(spans.cols) do - local this = {} - this.content = string.strip(row:sub(spans.starts[nc], spans.stops[nc])) - this.span = span - newrow[#newrow+1] = this - end - elseif multispans[nr-1] then -- some cells span columns + elseif p.st_colspan:match(row) then + newrow.ignore = true + elseif not rows.head_end and + nr > 1 and #raw > nr then -- ends the header + rows.head_end = nr + newrow.head_sep = true newrow.ignore = true else - local nc = 1 - repeat - local this = {} - this.content = string.strip(row:sub(bounds.slices[nc].start, bounds.slices[nc].stop - 1)) - if bounds.starts[nc+1] then - -- Only spaces allowed between columns. - local between = s:sub(bounds.stops[nc], bounds.slices[nc+1].start) - if not match(p.space^1 * -P(1), between) then - -- This is probably too hard a step. - helpers.dbg_write("[sta-h] Please revise your table boundaries.") - os.exit(1) - end - end - newrow[nc] = this - nc = nc + 1 - until nc > #bounds.slices + newrow.ignore = true end rows[nr] = newrow end |