1. Philipp Gesang
  2. context-rst

Commits

Philipp Gesang  committed 3afe206

coding style

  • Participants
  • Parent commits a4c94b9
  • Branches default

Comments (0)

Files changed (4)

File mod/tex/context/third/rst/rst_context.lua

View file
 --------------------------------------------------------------------------------
 --
 --- TODO
--- - Find an appropriate way to handle generic tables irrespective of the grid
---   settings. The problem is:
---   http://archive.contextgarden.net/message/20100912.112605.8a1aaf13.en.html
---   Seems we'll have to choose either the grid or split tables as default. Not
---   good.
+---   Find an appropriate way to handle generic tables irrespective of the grid
+---   settings. The problem is:
+---   http://archive.contextgarden.net/message/20100912.112605.8a1aaf13.en.html
+---   Seems we'll have to choose either the grid or split tables as default. Not
+---   good.
 
 
 local helpers        = helpers        or thirddata and thirddata.rst_helpers
 
 local dbg_write = helpers.dbg_writef
 
-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
+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
 
 -- This one should ignore escaped spaces.
 do
         return stripper:match(str) or ""
     end
 end
-local stringstrip = string.strip
-local fmt         = string.format
+local stringstrip  = string.strip
+local stringformat = string.format
 
 local err = function(str)
     if str then
     end
 end
 
--- So we can use crefs[n][2] to refer to the place where the reference was
--- created.
+--- So we can use crefs[n][2] to refer to the place where the reference was
+--- created.
 local function get_context_reference (str)
     local crefs = rst_context.context_references
     local srefs = rst_context.structure_references
     local definition = color:match("^color_(.+)$")
     if definition:match("^rgb_") then -- assume rgb
         local rgb = p.rgbvalues:match(definition)
-        definition = fmt([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3])
+        definition = stringformat([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3])
     end
-    return fmt([[\\colored[%s]{%s}]], definition, str)
+    return stringformat([[\\colored[%s]{%s}]], definition, str)
 end
 
 --------------------------------------------------------------------------------
 -- wrong if you can! Or go tell those digital publishers and their willing
 -- subordinates, the authors, who think they can save a few pennys,
 -- substituting the typesetter and editor by some fancy software. Keep in mind
--- that zapf.tex is not just random dummy text. Now, where was I?
+-- that zapf.tex is not just random dummy text. </rant>
 
 function rst_context.roles.ctsh(str) -- shorthand
     rst_context.addsetups("citator")
     str = str:match("^`?([^`]+)`?__$")
     rst_context.anonymous_links[#rst_context.anonymous_links+1] = str
     link = "__target_anon_" .. #rst_context.anonymous_links
-    return fmt([[\\RSTchoosegoto{%s}{%s}]], link, str)
+    return stringformat([[\\RSTchoosegoto{%s}{%s}]], link, str)
 end
 
 local whitespace = S" \n\t\v"
     else
         str = data
     end
-    return fmt([[
+    return stringformat([[
 
 \\startparagraph
 %s
 
         ref = get_context_reference (str)
 
-        str = fmt("\n\\\\%s[%s]{%s}\n", sectionlevels[level], ref, str)
+        str = stringformat("\n\\\\%s[%s]{%s}\n", sectionlevels[level], ref, str)
     else
         return [[{\\bf fix your sectioning!}\\endgraf}]]
     end
             -- just leave it alone
         elseif helpers.list.greater(itemtype, li[current_itemdepth]) then
             local itemnum = tonumber(stringstrip(itemtype)) or helpers.list.get_decimal(itemtype)
-            result = result .. fmt([[
+            result = result .. stringformat([[
 \\setnumber[itemgroup:itemize]{%s}
 ]], itemnum)
         end
 
 function rst_context.field (tab)
     local name, body = tab[1], tab[2]
-    return fmt([[
+    return stringformat([[
 
     \\RSTfieldname{%s}
     \\RSTfieldbody{%s}
 end
 
 function rst_context.block_comment (str)
-    return fmt([[
+    return stringformat([[
 
 \iffalse %% start block comment
 %s\fi %% stop block comment
 end
 
 function rst_context.option_item (tab)
-    return fmt([[\\bTR\\bTC %s \\eTC\\bTC %s \\eTC\\eTR
+    return stringformat([[\\bTR\\bTC %s \\eTC\\bTC %s \\eTC\\eTR
 ]], tab[1], tab[2])
 end
 
                     dbg_write("%7s | ", cell.content)
                     local celltext = inline_parser:match(cell.content)
                     if cell.span then
-                        head = head .. fmt([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext or "")
+                        head = head .. stringformat([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext or "")
                     else
                         head = head .. [[\\bTH ]] .. celltext .. [[\\eTH]]
                     end
                 dbg_write("%7s | ", cell.content)
                 local celltext = inline_parser:match(cell.content)
                 if cell.span then
-                    body = body .. fmt([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext or "")
+                    body = body .. stringformat([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext or "")
                 else
                     body = body .. [[\\bTC ]] .. celltext .. [[\\eTC]]
                 end

File mod/tex/context/third/rst/rst_directives.lua

View file
 thirddata.rst_directives = rst_directives
 local rst_context        = thirddata.rst
 
-local stringstrip = string.strip
-local fmt         = string.format
+local stringstrip  = string.strip
+local stringformat = string.format
 
 rst_directives.anonymous = 0
 rst_directives.images        = {}
     local images_done = rd.images.done
     if not anon then
         if not images_done[name] then
-            img = img .. fmt([[
+            img = img .. stringformat([[
 
 \useexternalfigure[%s][%s][]
 ]], name, data)
         images_done[name] = true
         end
-        img = img .. fmt([[
+        img = img .. stringformat([[
 \def\RSTsubstitution%s{%%
   \placefigure[here]{%s}{\externalfigure[%s]%s}
 }
 ]], name, rst_context.escape(inline_parser:match(properties.caption)), name, properties.setup)
     else -- image won't be referenced but used instantly
-        img = img .. fmt([[
+        img = img .. stringformat([[
 
 \placefigure[here]{%s}{\externalfigure[%s]%s}
 ]], rst_context.escape(inline_parser:match(properties.caption)), data, properties.setup)
         end
     end
     text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text))) 
-    return fmt([[
+    return stringformat([[
 \startRSTcaution
 %s
 \stopRSTcaution
         end
     end
     text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text))) 
-    return fmt([[
+    return stringformat([[
 \startRSTdanger
 %s
 \stopRSTdanger
     for _,str in ipairs(addendum) do
         result = result .. (stringstrip(str))
     end
-    return fmt([[
+    return stringformat([[
 
 %% The Rabbit of Caerbannog
 \startlinecorrection
 end
 
 rst_directives.mp = function(name, data)
-    local mpcode = fmt([[
+    local mpcode = stringformat([[
 \startreusableMPgraphic{%s}
 %s
 \stopreusableMPgraphic
 ]], name, data)
-    mpcode = mpcode .. fmt([[
+    mpcode = mpcode .. stringformat([[
 \def\RSTsubstitution%s{%%
   \reuseMPgraphic{%s}%%
 }
 --- implementation.
 --- http://archive.contextgarden.net/message/20111108.175913.1d994624.en.html
 rst_directives.ctx = function(name, data)
-    local ctx = fmt([[
+    local ctx = stringformat([[
 
 \startbuffer[%s]%%
 %s\stopbuffer
 end
 
 rst_directives.lua = function(name, data)
-    local luacode = fmt([[
+    local luacode = stringformat([[
 
 \startbuffer[%s]%%
 \startluacode
             i = i + 1
         end
     end
-    return fmt([[
+    return stringformat([[
 \startformula
 %s
 \stopformula
 --------------------------------------------------------------------------------
 
 rst_directives.replace = function(name, data)
-    return fmt([[
+    return stringformat([[
 
 \def\RSTsubstitution%s{%s}
 ]], name, data)

File mod/tex/context/third/rst/rst_helpers.lua

View file
 local P, R, S, V, match 
     = lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match
 
-local C, Carg, Cb, Cc, Cg, Cmt, Cp, Cs, Ct 
-    = lpeg.C, lpeg.Carg, lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct
+local C,   Carg, Cb, Cc, Cg,
+      Cmt, Cp,   Cs, Ct 
+    = lpeg.C,   lpeg.Carg, lpeg.Cb, lpeg.Cc, lpeg.Cg,
+      lpeg.Cmt, lpeg.Cp,   lpeg.Cs, lpeg.Ct
 
 local helpers
 helpers       = thirddata.rst_helpers
 local utf    = unicode.utf8
 local utflen = utf.len
 
-local stringstrip = string.strip
-local fmt         = string.format
+local stringstrip  = string.strip
+local stringformat = string.format
 
 function helpers.dbg_writef(...)
     if helpers.rst_debug then
-        io.write(fmt(...))
+        io.write(stringformat(...))
     end
 end
 
         [1] = "cells",
         cells = p.celldelim 
               * (C(V"in_cell")
-               * (V"matchwidth" * C(V"in_cell")) ^1),
+               * (V"matchwidth" * C(V"in_cell"))^1),
 
         in_cell = p.cellcontent^1
                 + (p.dash - p.cellcontent)^1,
 
         matchwidth = Cmt(C(p.celldelim) * Carg(1), function(s,i,del, layout)
                          local pos = 1
-                         local lw = layout.widths
+                         local lw  = layout.widths
                          for n=1, #lw do
                              pos = pos + lw[n] + 1
                              if (i - 1) == pos then return true end
 
 function helpers.table.create(raw)
     local newtab = {}
-    newtab.rows = {}
+    newtab.rows  = {}
     newtab.layout = set_layout(raw[1])
 
     local p = helpers.patterns
 
     newtab.resolve_parent = function(row, col, array)
         local array = array or newtab.rows
-        local cell = array[row][col]
+        local cell  = array[row][col]
         local par_row, par_col = row, col
         if cell.parent then
             par_row, par_col = newtab.resolve_parent(cell.parent.y, cell.parent.x)
 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.
+    --- 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 = utflen
     function helpers.string.wrapat (str, width)
         local width = width or 65
             typing        = P[[\\type{]]  * (1 - P"}")^0 * P"}",
             typingenv     = P[[\\starttyping]] * (1 - P[[\\stoptyping]])^0 * P[[\\stoptyping]],
             ignore        = V"typing" + V"typingenv",
-            -- the initial whitespace of the “other” pattern must not be
-            -- enforced (“^1”) as it will break the exceptions (“ignore”
-            -- pattern)! In general it is better to have the wrapper ignore some
-            -- valid breaks than to not have it matching some valid strings at
-            -- all.
+            --- the initial whitespace of the “other” pattern must not
+            --- be enforced (“^1”) as it will break the exceptions
+            --- (“ignore” pattern)! In general it is better to have the
+            --- wrapper ignore some valid breaks than to not have it
+            --- matching some valid strings at all.
             other         = Cmt(V"whitespace"^0 * (V"ignore" + (1 - V"whitespace")^1), function(s,i,w)
                                    linelength = linelength + ulen(w)
                                    return true

File mod/tex/context/third/rst/rst_parser.lua

View file
 local helpers         = thirddata.rst_helpers
 local optional_setups = thirddata.rst_setups
 
-rst.strip_BOM     = false
-rst.expandtab     = false
+rst.strip_BOM     = true
+rst.expandtab     = true
 rst.shiftwidth    = 4
 rst.crlf          = true
 helpers.rst_debug = false
 
-local iowrite     = io.write
-local fmt         = string.format
-local stringlen   = string.len
-local stringstrip = string.strip
-local utf         = unicode.utf8
-local utflen      = utf.len
+local iowrite      = io.write
+local stringformat = string.format
+local stringlen    = string.len
+local stringstrip  = string.strip
+local utf          = unicode.utf8
+local utflen       = utf.len
 
 local warn
 do
         ndebug = ndebug + 1
         local slen = #str + 3
         --str = "*["..str.."]"
-        str = fmt("*[%4d][%s]", ndebug, str)
+        str = stringformat("*[%4d][%s]", ndebug, str)
         for i,j in ipairs({...}) do
             if 80 - i * 8 - slen < 0 then
                 local indent = ""
                 end
                 str = str .. "\n" .. indent
             end
-            str = str .. fmt(" |%6s", stringstrip(tostring(j)))
+            str = str .. stringformat(" |%6s", stringstrip(tostring(j)))
         end
         iowrite(str .. " |\n")
         return 0
     end
 end
 
-local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct 
-    = lpeg.C, lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct
+local C,   Cb, Cc, Cg,
+      Cmt, Cp, Cs, Ct 
+    = lpeg.C,   lpeg.Cb, lpeg.Cc, lpeg.Cg,
+      lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct
 
 local P, R, S, V, match 
     = lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match
     local setups     = get_setups(true)
 
     local incnr    = #rst_incsetups  + 1
-    local tmp_file = tex.jobname .. fmt("–rst_inclusion-%d.tex.tmp", incnr)
+    local tmp_file = tex.jobname .. stringformat("–rst_inclusion-%d.tex.tmp", incnr)
 
     if processed then
         processed = strip_comments:match(processed)
     if rst_inclusions[iname] then
         context.input(rst_inclusions[iname])
     else
-        context(fmt("{\\bf File for inclusion “%s” not found.}\par ", iname))
+        context(stringformat("{\\bf File for inclusion “%s” not found.}\par ", iname))
     end
 end