summaryrefslogtreecommitdiff
path: root/rst_helpers.lua
diff options
context:
space:
mode:
Diffstat (limited to 'rst_helpers.lua')
-rw-r--r--rst_helpers.lua136
1 files changed, 61 insertions, 75 deletions
diff --git a/rst_helpers.lua b/rst_helpers.lua
index caf3ed3..acd61ed 100644
--- a/rst_helpers.lua
+++ b/rst_helpers.lua
@@ -20,6 +20,14 @@ local helpers = {}
helpers.table = {}
helpers.cell = {}
+local function dbg_writef(...)
+ if helpers_debug == true then
+ io.write(string.format(...))
+ end
+end
+
+helpers.dbg_write = dbg_write
+
local patterns = {}
do
@@ -47,12 +55,10 @@ do
+ (p.dash - p.cellcontent)^1,
matchwidth = Cmt(C(p.celldelim) * Carg(1), function(s,i,del, layout)
- --print("~~~~~~~~~~~~~~ NEXT MATCH ~~~~~~~~~~~~~~")
local pos = 1
local lw = layout.widths
for n=1, #lw do
pos = pos + lw[n] + 1
- --print(">",pos,i-1,i-1==pos)
if (i - 1) == pos then return true end
end
return false
@@ -89,13 +95,10 @@ function helpers.cell.create(raw, n_row, n_col, parent, variant)
end
function helpers.cell.get_x_span(content, layout, init)
- --print(#content,">"..content.."<")
- --print(layout.widths[init],">"..layout.slices[init].."<")
local acc = 0
local lw = layout.widths
for n=init, #lw do
acc = acc + lw[n] + 1
- --print(n, layout.slices[n], acc)
if utf.len(content) + 1 == acc then
return n - init
end
@@ -288,8 +291,8 @@ end
--self.__init()
--return self
--end
-
+helpers_debug = true
function helpers.table.create(raw)
local self = {}
self.rows = {}
@@ -321,67 +324,50 @@ function helpers.table.create(raw)
self.has_head = true
end
- --if not this_row.sepline and not this_row.sephead then
- --print("new row" , row)
- local splitted = { p.cells:match(row, 1, selflayout) }
- local pos_layout, pos_row = 1, 1
- local make_empty = {}
- make_empty.n, make_empty.parent = 0, nil
-
- while pos_layout <= #selflayout.widths do
- local splitpos = splitted[pos_layout]
- local layoutwidth = selflayout.widths[pos_layout]
- local span = 1
- local this
-
- if make_empty.n > 0 then
- --print(make_empty.n, make_empty.parent)
- make_empty.n = make_empty.n - 1
- this = hc.create("", nr, pos_layout, make_empty.parent)
- this.parent = make_empty.parent
- p_row, p_col = self.resolve_parent(this.parent.y, this.parent.x)
- --print(p_row, p_col)
- local thisparent = self.rows[p_row][p_col]
- --print(this.parent.y, this.parent.x,p_row,p_col)
- --this.variant = "empty" --self.rows[p_row][p_col].variant
- --if self.rows[p_row][p_col].variant == "separator" then
- if this_row.sepline or this_row.sephead or
- self.rows[p_row][p_col].variant == "separator" then
- this.variant = "separator"
- else
- this.variant = "empty1"
- end
- --this.variant = self.rows[p_row][p_col].variant
+ local splitted = { p.cells:match(row, 1, selflayout) }
+ local pos_layout, pos_row = 1, 1
+ local make_empty = {}
+ make_empty.n, make_empty.parent = 0, nil
+
+ while pos_layout <= #selflayout.widths do
+ local splitpos = splitted[pos_layout]
+ local layoutwidth = selflayout.widths[pos_layout]
+ local span = 1
+ local this
+
+ if make_empty.n > 0 then
+ make_empty.n = make_empty.n - 1
+ this = hc.create("", nr, pos_layout, make_empty.parent)
+ this.parent = make_empty.parent
+ p_row, p_col = self.resolve_parent(this.parent.y, this.parent.x)
+ local thisparent = self.rows[p_row][p_col]
+ if this_row.sepline or this_row.sephead or
+ self.rows[p_row][p_col].variant == "separator" then
+ this.variant = "separator"
else
- local cellwidth = utf.len(splitpos)
- if cellwidth > layoutwidth then
- span = span + hc.get_x_span(splitpos, selflayout, pos_layout)
- end
- pos_row = pos_row + span
- this = hc.create(splitpos, nr, pos_layout, nil)
- --print("this parent: ",this.parent)
- if p.dashesonly:match(splitpos) or
- this_row.sepline or this_row.sephead then
- this.variant = "separator"
- end
- this.span.x = span
- make_empty.n = span - 1
- make_empty.parent = span > 1 and { y = nr, x = pos_layout } or nil
- --print(this.content, make_empty.parent.y, make_empty.parent.x)
+ this.variant = "empty1"
end
+ else
+ local cellwidth = utf.len(splitpos)
+ if cellwidth > layoutwidth then
+ span = span + hc.get_x_span(splitpos, selflayout, pos_layout)
+ end
+ pos_row = pos_row + span
+ this = hc.create(splitpos, nr, pos_layout, nil)
+ if p.dashesonly:match(splitpos) or
+ this_row.sepline or this_row.sephead then
+ this.variant = "separator"
+ end
+ this.span.x = span
+ make_empty.n = span - 1
+ make_empty.parent = span > 1 and { y = nr, x = pos_layout } or nil
+ end
- this_row[pos_layout] = this
- pos_layout = pos_layout + 1
- end -- while
- --end -- if
+ this_row[pos_layout] = this
+ pos_layout = pos_layout + 1
+ end -- while
end -- for loop over rows
- --for i, j in ipairs(self.rows) do
- --print(i, #j)
- --if #j > 0 and j[3].parent then
- --print(j[3].content, j[3].span.x, j[3].parent.y, j[3].parent.x, j[3].variant)
- --end
- --end
local oldrows = self.rows
--local newrows = {}
local newrows = oldrows
@@ -393,7 +379,7 @@ function helpers.table.create(raw)
for nr, row in ipairs(newrows) do
--newrows[nr] = newrows[nr] or {}
local cell = row[nc]
- io.write(string.format("nc: %s, nr:%2s | %9s | ", nc, nr,cell.variant))
+ dbg_writef("nc: %s, nr:%2s | %9s | ", nc, nr,cell.variant)
if row.sepline or row.sephead
or p.dashesonly:match(cell.content)
or cell.variant == "separator" then -- separator; skipping and beginning new row
@@ -402,11 +388,11 @@ function helpers.table.create(raw)
newrows[nr][nc] = cell
currentrow = currentrow + 1
newrow = true
- io.write(string.format("new >%24s< ", cell.stripped))
- if cell.parent then io.write("parent |") else io.write("no par |") end
+ dbg_writef("new >%24s< ", cell.stripped)
+ if cell.parent then dbg_writef("parent |") else dbg_writef("no par |") end
else
- io.write(string.format("old >%24s< ", cell.stripped))
- if cell.parent then io.write("parent |") else io.write("no par |") end
+ dbg_writef("old >%24s< ", cell.stripped)
+ if cell.parent then dbg_writef("parent |") else dbg_writef("no par |") end
if newrow then
--newrows[currentrow] = cell
newrows[nr][nc] = cell
@@ -416,17 +402,17 @@ function helpers.table.create(raw)
local par_row, par_col
local parent
if cell.parent then
- --io.write(string.format(" use %s,%2s | ", cell.parent.x, cell.parent.y))
+ --dbg_writef(" use %s,%2s | ", cell.parent.x, cell.parent.y)
par_row, par_col = self.resolve_parent(cell.parent.y, cell.parent.x, newrows)
- io.write(string.format(" use %s,%2s | ", par_col, par_row))
+ dbg_writef(" use %s,%2s | ", par_col, par_row)
else -- Using vertical predecessor.
par_row, par_col = self.resolve_parent(nr-1,nc, newrows)
- io.write(string.format(" new %s,%2s | ", par_col, par_row))
+ dbg_writef(" new %s,%2s | ", par_col, par_row)
end
parent = newrows[par_row][par_col]
if newrows[nr].seppart then
- io.write("span++")
+ dbg_writef("span++")
parent.span.y = parent.span.y + 1
end
@@ -446,7 +432,7 @@ function helpers.table.create(raw)
end
newrow = false
end
- io.write("\n")
+ dbg_writef("\n")
newrows[nr][nc] = cell
end -- for loop over rows
end -- for loop over columns
@@ -464,10 +450,10 @@ function helpers.table.create(raw)
if cell.parent then
field = field .. string.format(" %s,%2s",cell.parent.x, cell.parent.y)
end
- io.write(string.format("%12s | ", field))
- --io.write(string.format("%10s | ",(cell.parent and "true" or "false")))
+ dbg_writef("%12s | ", field)
+ --dbg_writef("%10s | ",(cell.parent and "true" or "false"))
end
- io.write("\n")
+ dbg_writef("\n")
end
end