From b5ab9445882ece8bf0c384d7a9314753b9557bd7 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 14 Sep 2010 17:18:39 +0200 Subject: output formatting. includes paragraph reflowing --- rst_context.lua | 39 ++++++++++++++++++++++----------------- rst_helpers.lua | 40 ++++++++++++++++++++++++++++++++++++++-- rst_parser.lua | 27 +++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 21 deletions(-) diff --git a/rst_context.lua b/rst_context.lua index 1c050f9..0c657df 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -92,7 +92,7 @@ function rst_context.addsetups(item) end do - local w = S" \v\t\n" + local w = S" \v\t\n" / "_" local wp = Cs((w + 1)^1) function rst_context.whitespace_to_underscore(str) return wp:match(str) @@ -119,6 +119,7 @@ end function rst_context.literal (str) str = str:gsub([[\]], [[\\]]) -- evade escaping of backslashes return [[\\type{]] .. str .. [[}]] + --return [[\\starttyping ]] .. str .. [[\\stoptyping]] end @@ -185,20 +186,12 @@ function rst_context.target (tab) local target = tab[#tab] -- Ct + C could be clearer but who cares tab[#tab] = nil - local function resolve_indirect (r) - if r and r:match(".*_$") then -- pointing elsewhere - return resolve_indirect (refs[r:match("^`?([^`]*)`?_$")]) or "need another run!" -- TODO multiple runs && data collection - end - return r - end - local function create_anonymous () rst_context.anonymous_targets = rst_context.anonymous_targets + 1 return "anon_" .. rst_context.anonymous_targets end - target = resolve_indirect (target) - print(tab[1],target) + --target = resolve_indirect (target) local insert = "" @@ -412,7 +405,7 @@ function rst_context.paragraph (data) if not data then return "" elseif type(data) == "table" then - str = inline_parser:match(table.concat(data, " ")) + str = helpers.string.wrapat(inline_parser:match(table.concat(data, " ")), 65) else str = data end @@ -996,18 +989,30 @@ function optional_setups.footnotes () end function optional_setups.references () - local refs = [[ + local refs = rst_context.collected_references + local function resolve_indirect (r) + if r and r:match(".*_$") then -- pointing elsewhere + --print(">>"..r.."<<",">>"..r:match("^`?([^`]*)`?_$").."<<", refs[r:match("^`?([^`]*)`?_$")]) + return resolve_indirect (refs[r:match("^`?([^`]*)`?_$")]) or "need another run!" -- TODO multiple runs && data collection + end + return r + end + + local refsection = [[ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % References % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ]] - for ref, target in next, rst_context.collected_references do - refs = refs .. string.format([[ -\useURL[__target_%s] [%s] [] [%s] -]], rst_context.whitespace_to_underscore(ref), target, ref) + local references = {} + for ref, target in next, refs do + target = resolve_indirect(target) + references[#references+1] = string.format([[ +\useURL[__target_%s] [%s] [] [%s] ]], rst_context.whitespace_to_underscore(ref), target, ref) end - return refs + refsection = refsection .. table.concat(references, "\n") + return refsection end return rst_context 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 diff --git a/rst_parser.lua b/rst_parser.lua index 3a87ea2..04aa01a 100644 --- a/rst_parser.lua +++ b/rst_parser.lua @@ -1204,6 +1204,7 @@ local function get_setups () %{ Setups }% %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + \setupcolors[state=start] \setupinteraction[state=start,focus=standard,color=darkgreen,contrastcolor=darkgreen] \setupbodyfontenvironment [default] [em=italic] @@ -1216,7 +1217,17 @@ local function get_setups () local f = optional_setups[item] setups = setups .. f() end - return setups .. "\\starttext" + return setups .. [[ + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~% +%{ Main }% +%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\starttext +]] end local function main() @@ -1228,7 +1239,19 @@ local function main() local setups = get_setups() - processeddata = setups .. processeddata .. "\\stoptext" + processeddata = setups .. processeddata .. [[ + +\stoptext + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~% +%{ End of Document }% +%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% vim:ft=context:tw=65:shiftwidth=2:tabstop=2:set expandtab +]] + if processeddata then save_file(arg[2], processeddata) else -- cgit v1.2.3