From b2ab0ab20e2455ef112cf340e09e2f33cf641993 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 2 Mar 2014 15:07:14 +0100 Subject: distinguish between overlined and underline-only sections with the same adonment Closing #4 See http://docutils.sourceforge.net/docs/user/rst/quickstart.html#sections for the legalese. --- src/rst_context.lua | 80 ++++++++++++++++++++++++++++++----------------------- src/rst_parser.lua | 6 ++++ 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/src/rst_context.lua b/src/rst_context.lua index c7e21fe..1654832 100644 --- a/src/rst_context.lua +++ b/src/rst_context.lua @@ -15,19 +15,21 @@ --- good. -local helpers = helpers or thirddata and thirddata.rst_helpers -local rst_directives = rst_directives or thirddata and thirddata.rst_directives - -local utf = unicode.utf8 -local utflen = utf.len -local utflower = utf.lower -local utfupper = utf.upper -local iowrite = io.write -local tableconcat = table.concat - -local stringmatch = string.match -local stringgmatch = string.gmatch -local stringgsub = string.gsub +local helpers = helpers or thirddata and thirddata.rst_helpers +local rst_directives = rst_directives or thirddata and thirddata.rst_directives + +local utf = unicode.utf8 +local utflen = utf.len +local utflower = utf.lower +local utfupper = utf.upper +local iowrite = io.write +local tableconcat = table.concat + +local stringgmatch = string.gmatch +local stringgsub = string.gsub +local stringmatch = string.match +local stringsub = string.sub +local stringformat = string.format local dbg_write = helpers.dbg_writef @@ -50,7 +52,6 @@ do end end local stringstrip = string.strip -local stringformat = string.format local err = function(str) if str then @@ -681,24 +682,34 @@ local function get_line_pattern (chr) return P(chr)^1 * (-P(1)) end -function rst_context.section (...) -- TODO general cleanup; move validity - local tab = { ... } -- checking to parser. - local section, str = true, "" - local adornchar - local ulen = utflen - if #tab == 3 then -- TODO use unicode length with ConTeXt - adornchar = tab[1]:sub(1,1) - section = ulen(tab[1]) >= ulen(tab[2]) - str = stringstrip(tab[2]) - else -- no overline - adornchar = tab[2]:sub(1,1) - section = ulen(tab[1]) <= ulen(tab[2]) - str = tab[1] +--[[-- + Quoth the spec: + An underline-only adornment is distinct from an + overline-and-underline adornment using the same character. + + We store the depths by hashing the adorn character and distinguish + the overlined ones from only underlined ones by prefixing them with + ā€œOā€ and ā€œUā€, respectively. +--]]-- + +function rst_context.section (first, second, third) + local section = true + local str = "" + local adornchar = nil + local ulen = utflen + if third then --- overlined + adornchar = "O" .. stringsub(first, 1,1) + section = ulen(first) >= ulen(second) + str = stringstrip(second) + else -- underline-only + adornchar = "U" .. stringsub(second, 1,1) + section = ulen(first) <= ulen(second) + str = stringstrip(first) end if section then -- determine level local level = rst_context.last_section_level - local rca = rst_context.collected_adornments + local rca = rst_context.collected_adornments if rca[adornchar] then level = rca[adornchar] else @@ -707,14 +718,15 @@ function rst_context.section (...) -- TODO general cleanup; move validity rst_context.last_section_level = level end - ref = get_context_reference (str) + local ref = get_context_reference (str) - str = stringformat("\n\\\\%s[%s]{%s}\n", sectionlevels[level], ref, str) - else - return [[{\\bf fix your sectioning!}\\endgraf}]] + str = stringformat("\n\\\\%s[%s]{%s}\n", + sectionlevels[level], + ref, + str) + return str or "" end - - return section and str or "" + return [[{\\bf fix your sectioning!}\\endgraf}]] end -- Prime time for the fancybreak module. diff --git a/src/rst_parser.lua b/src/rst_parser.lua index 93d8a11..3928161 100644 --- a/src/rst_parser.lua +++ b/src/rst_parser.lua @@ -127,6 +127,12 @@ state.addme = {} local valid_adornment do + --[[-- + + valid_adornment -- This subpattern tests if the string consists + entirely of one repeated adornment char. + + --]]-- local first_adornment = "" local adornment_char = S[[!"#$%&'()*+,-./:;<=>?@[]^_`{|}~]] + P[[\\]] local check_first = Cmt(adornment_char, function(_,_, first) -- cgit v1.2.3