summaryrefslogtreecommitdiff
path: root/rst_helpers.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-14 17:18:39 +0200
committerPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-14 17:18:39 +0200
commitb5ab9445882ece8bf0c384d7a9314753b9557bd7 (patch)
tree8a9615af22999ccc08dca5563d0ddbbd39558747 /rst_helpers.lua
parent77c44e11aab17795bc24cb7cb6950712c23049b5 (diff)
downloadcontext-rst-b5ab9445882ece8bf0c384d7a9314753b9557bd7.tar.gz
output formatting. includes paragraph reflowing
Diffstat (limited to 'rst_helpers.lua')
-rw-r--r--rst_helpers.lua40
1 files changed, 38 insertions, 2 deletions
diff --git a/rst_helpers.lua b/rst_helpers.lua
index b30370a..f7eb950 100644
--- a/rst_helpers.lua
+++ b/rst_helpers.lua
@@ -29,10 +29,10 @@ end
--helpers.dbg_write = dbg_write
local dbg_write = helpers.dbg_writef
-local patterns = {}
+helpers.patterns = {}
do
- local p = patterns
+ local p = helpers.patterns
p.dash = P"-"
p.equals = P"="
p.plus = P"+"
@@ -701,5 +701,41 @@ do
helpers.list.successor = suc
end
+helpers.string = {}
+
+do
+ -- This grammar inside the function is slightly faster than the same as an upvalue
+ -- with the value of “width” repeatedly given via lpeg.Carg(). This holds
+ -- for repeated calls as well.
+ local ulen = utf.len
+ function helpers.string.wrapat (str, width)
+ local linelength = 0
+ local wrap = P{
+ [1] = "wrapper",
+
+ wrapper = Cs(V"nowhitespace"^0 * (Cs(V"wrapme") + V"other")^1),
+ whitespace = S" \t\v" + P"\n" / function() linelength = 0 end,
+ nowhitespace = 1 - V"whitespace",
+ ignore = P"\\type{" * (1 - P"}")^1 * P"}",
+ other = Cmt(V"whitespace"^1 * (V"ignore" + (1 - V"whitespace")^1), function(s,i,w)
+ linelength = linelength + ulen(w)
+ return true
+ end),
+ wrapme = Cmt(V"whitespace"^1 * (1 - V"whitespace" - V"ignore")^1, function(s,i,w)
+ local lw = ulen(w)
+ if linelength + lw > width then
+ linelength = lw
+ return true
+ end
+ return false
+ --end) / function (word) return "\n" .. match(V"whitespace"^1 * C((1 - V"whitespace")^1), word) end,
+ end) / function (word) return "\n" .. word:match("[^%s]+") end,
+ }
+
+ local reflowed = wrap:match(str)
+ return reflowed
+ end
+end
+
return helpers