summaryrefslogtreecommitdiff
path: root/rst_helpers.lua
diff options
context:
space:
mode:
Diffstat (limited to 'rst_helpers.lua')
-rw-r--r--rst_helpers.lua121
1 files changed, 118 insertions, 3 deletions
diff --git a/rst_helpers.lua b/rst_helpers.lua
index acd61ed..fb0dd84 100644
--- a/rst_helpers.lua
+++ b/rst_helpers.lua
@@ -20,13 +20,14 @@ local helpers = {}
helpers.table = {}
helpers.cell = {}
-local function dbg_writef(...)
+function helpers.dbg_writef(...)
if helpers_debug == true then
io.write(string.format(...))
end
end
-helpers.dbg_write = dbg_write
+--helpers.dbg_write = dbg_write
+local dbg_write = helpers.dbg_writef
local patterns = {}
@@ -38,6 +39,9 @@ do
p.bar = P"|"
p.eol = P"\n"
p.last = -P(1)
+ p.space = P" "
+
+ p.dash_or_equals = p.dash + p.equals
p.celldelim = p.bar + p.plus
p.cellcontent = (1 - p.celldelim)
@@ -45,6 +49,16 @@ do
p.cell_line = p.plus * p.dash^1 * #p.plus
p.dashesonly = p.dash^1 * p.last
+ 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.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
+ 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
@@ -457,9 +471,110 @@ function helpers.table.create(raw)
end
end
- self.__draw_debug()
+ --self.__draw_debug()
return self
+end
+
+
+
+-- Check the column boundaries of a simple table.
+function helpers.get_st_boundaries (str)
+ local p = patterns
+ local starts, stops, slices = {}, {}, {}
+ for n, elm in ipairs({ p.column_starts:match(str) }) do
+ slices[n] = { start = elm }
+ starts[elm] = true
+ end
+ for n, elm in ipairs({ p.column_stops :match(str) }) do
+ slices[n]["stop"] = elm
+ stops[elm] = true
+ end
+ return { starts = starts, stops = stops, slices = slices }
+end
+
+function helpers.table.simple(raw)
+ local rows = {}
+ local multispans = {}
+ 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
+ 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
+ end
+ if bounds.slices[o].stop == stop then
+ stops_at = o
+ end
+ end
+ spans.cols[n] = 1 + stops_at - starts_at
+ end
+ multispans[nr-1] = spans
+ 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
+ newrow.end_head = true
+ 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
+ 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
+ end
+ rows[nr] = newrow
+ end
+ return rows
end
return helpers
+