summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rst_context.lua11
-rw-r--r--rst_helpers.lua136
-rw-r--r--rst_parser.lua58
3 files changed, 106 insertions, 99 deletions
diff --git a/rst_context.lua b/rst_context.lua
index 033c252..64c2cd6 100644
--- a/rst_context.lua
+++ b/rst_context.lua
@@ -13,7 +13,9 @@
require "lpeg"
-require "rst_helpers"
+require help = "rst_helpers"
+
+local dbg_write = help.dbg_write
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
@@ -281,7 +283,7 @@ local inline_parser = P{
function rst_context.paragraph (tab)
local str = inline_parser:match(table.concat(tab, " "))
- print(inline_parser:match(table.concat(tab, " ")))
+ --print(inline_parser:match(table.concat(tab, " ")))
return string.format([[
\\startparagraph
@@ -405,7 +407,7 @@ function rst_context.startitemize(str)
local listtype = enumeration_types[str] or parse_itemstring(str)
if type(listtype) == "table" then
- print(type(listtype), listtype[2])
+ --print(type(listtype), listtype[2])
setup = listtype.setup
listtype = listtype.str
end
@@ -424,7 +426,7 @@ end
function rst_context.bullet_item (str)
return [[
-\\item ]] .. str .. [[
+\\item ]] .. inline_parser:match(str) .. [[
]]
end
@@ -684,7 +686,6 @@ function rst_context.table_row (tab)
local tmp = [[\\bTR]]
for n, cell in ipairs(tab) do
tmp = tmp .. [[\\bTC]] .. cell .. [[\\eTC]] .. "\n"
- print (cell)
end
tmp = tmp .. [[\\eTR
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
diff --git a/rst_parser.lua b/rst_parser.lua
index d68645f..cc4304d 100644
--- a/rst_parser.lua
+++ b/rst_parser.lua
@@ -16,7 +16,7 @@ rst = require "rst_context"
helpers = require "rst_helpers"
-local rst_debug = false
+local rst_debug = true
local warn = function(str, ...)
if not rst_debug then return false end
@@ -281,10 +281,6 @@ local parser = P{
gt_body = ((V"gt_contentrow" - V"gt_bodysep")^1 * V"gt_bodysep")^1,
- --gt_row = (V"gt_contentrow" - V"gt_bodysep")^1
- --* C(V"gt_bodysep")
- --,
-
gt_bodysep = V"gt_matchindent"
* C(Cmt(V"table_intersection"
* (V"table_hline"^1 * V"table_intersection")^1, function(s, i, separator)
@@ -524,7 +520,6 @@ local parser = P{
* V"eol",
option_desc_more = V"space"^2
- --* V"rest_of_line"
* (1 - V"eol")^1
* V"eol"
* V"indented_lines"
@@ -688,7 +683,6 @@ local parser = P{
end
return false
end)
- --* V"bullet_indent" / rst.startitemize,
* Cs(V"bullet_indent") / rst.startitemize,
bullet_indent = V"space"^0 * V"bullet_expr" * V"space"^1,
@@ -808,7 +802,7 @@ local parser = P{
return a == b
end),
- target_link = ( V"space"^0 * V"target_firstindent" -- * C((1 - V"eol")^1) * V"eol")
+ target_link = ( V"space"^0 * V"target_firstindent"
* Ct(C(1 - V"whitespace" - V"eol")^1
* (V"target_indentmatch"
* C(1 - V"whitespace" - V"eol")^1)^0)
@@ -840,7 +834,6 @@ local parser = P{
* Ct(C((1 - V"eol")^1) * V"eol"
* (V"par_matchindent" * C((1 - V"eol")^1) * V"eol")^0)
* V"blank_line"^1
- --* V"endpar"
/ rst.paragraph,
par_setindent = Cmt(V"space"^0, function (s, i, indent)
@@ -1048,15 +1041,42 @@ local parser = P{
table_header_hline = P"=",
}
-f = io.open("inlinefmt.rst", "r")
-testdata = f:read("*all")
-f:close()
+local function load_file (name)
+ f = assert(io.open(name, "r"), "Not a file!")
+ if not f then return 1 end
+ local tmp = f:read("*all")
+ f:close()
+ return tmp
+end
+
+local function save_file (name, data)
+ f = assert(io.open(name, "w"), "Could not open file "..name.." for writing! Check its permissions")
+ if not f then return 1 end
+ f:write(data)
+ f:close()
+ return 0
+end
+
+local function main()
+ local testdata = load_file(arg[1])
+ if testdata == 1 then return 1 end
-print(parser:match(testdata))
---print(">>>Last used char>: " ..tracklists.lastbullet.." <<<<")
---print(">>>Max list nestin>: "..tracklists.bullets.max .." <<<<")
+ local processeddata = parser:match(testdata)
+
+ if processeddata then
+ save_file(arg[2], processeddata)
+ else
+ return 1
+ end
+
+ --print(">>>Last used char>: " ..tracklists.lastbullet.." <<<<")
+ --print(">>>Max list nestin>: "..tracklists.bullets.max .." <<<<")
+
+ --for i,j in next, rst.collected_references do
+ --print (string.format("== %7s => %s <=", i,j))
+ --end
+ --parser:print()
+ return 0
+end
---for i,j in next, rst.collected_references do
- --print (string.format("== %7s => %s <=", i,j))
---end
---parser:print()
+return main()