From 46e593762890bf882a9941668699da867297649c Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Mon, 31 Jan 2022 10:21:00 +0100 Subject: [PATCH] Custom writer: default to single quotes for strings Makes the code more consistent and makes it easier to use double quotes in strings, which is the usual quoting style used for HTML attributes. Closes: #7487 --- data/sample.lua | 108 ++++++++++++++++++++++----------------------- test/writer.custom | 60 ++++++++++++------------- 2 files changed, 84 insertions(+), 84 deletions(-) diff --git a/data/sample.lua b/data/sample.lua index ea20add2e..c0adae230 100644 --- a/data/sample.lua +++ b/data/sample.lua @@ -14,7 +14,7 @@ -- syntax errors. local pipe = pandoc.pipe -local stringify = (require "pandoc.utils").stringify +local stringify = (require 'pandoc.utils').stringify -- The global variable PANDOC_DOCUMENT contains the full AST of -- the document which is going to be written. It can be used to @@ -25,19 +25,19 @@ local meta = PANDOC_DOCUMENT.meta -- `image_format` meta value. local image_format = meta.image_format and stringify(meta.image_format) - or "png" + or 'png' local image_mime_type = ({ - jpeg = "image/jpeg", - jpg = "image/jpeg", - gif = "image/gif", - png = "image/png", - svg = "image/svg+xml", + jpeg = 'image/jpeg', + jpg = 'image/jpeg', + gif = 'image/gif', + png = 'image/png', + svg = 'image/svg+xml', })[image_format] - or error("unsupported image format `" .. image_format .. "`") + or error('unsupported image format `' .. image_format .. '`') -- Character escaping local function escape(s, in_attribute) - return s:gsub("[<>&\"']", + return s:gsub('[<>&"\']', function(x) if x == '<' then return '<' @@ -60,7 +60,7 @@ end local function attributes(attr) local attr_table = {} for x,y in pairs(attr) do - if y and y ~= "" then + if y and y ~= '' then table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"') end end @@ -72,7 +72,7 @@ local notes = {} -- Blocksep is used to separate block elements. function Blocksep() - return "\n\n" + return '\n\n' end -- This function is called once for the whole document. Parameters: @@ -106,31 +106,31 @@ function Str(s) end function Space() - return " " + return ' ' end function SoftBreak() - return "\n" + return '\n' end function LineBreak() - return "
" + return '
' end function Emph(s) - return "" .. s .. "" + return '' .. s .. '' end function Strong(s) - return "" .. s .. "" + return '' .. s .. '' end function Subscript(s) - return "" .. s .. "" + return '' .. s .. '' end function Superscript(s) - return "" .. s .. "" + return '' .. s .. '' end function SmallCaps(s) @@ -142,33 +142,33 @@ function Strikeout(s) end function Link(s, tgt, tit, attr) - return "" .. s .. "" + return '' .. s .. '' end function Image(s, src, tit, attr) - return "" + return '' end function Code(s, attr) - return "" .. escape(s) .. "" + return '' .. escape(s) .. '' end function InlineMath(s) - return "\\(" .. escape(s) .. "\\)" + return '\\(' .. escape(s) .. '\\)' end function DisplayMath(s) - return "\\[" .. escape(s) .. "\\]" + return '\\[' .. escape(s) .. '\\]' end function SingleQuoted(s) - return "‘" .. s .. "’" + return '‘' .. s .. '’' end function DoubleQuoted(s) - return "“" .. s .. "”" + return '“' .. s .. '”' end function Note(s) @@ -184,11 +184,11 @@ function Note(s) end function Span(s, attr) - return "" .. s .. "" + return '' .. s .. '' end function RawInline(format, str) - if format == "html" then + if format == 'html' then return str else return '' @@ -200,8 +200,8 @@ function Cite(s, cs) for _,cit in ipairs(cs) do table.insert(ids, cit.citationId) end - return "" .. s .. "" + return '' .. s .. '' end function Plain(s) @@ -209,16 +209,16 @@ function Plain(s) end function Para(s) - return "

" .. s .. "

" + return '

' .. s .. '

' end -- lev is an integer, the header level. function Header(lev, s, attr) - return "" .. s .. "" + return '' .. s .. '' end function BlockQuote(s) - return "
\n" .. s .. "\n
" + return '
\n' .. s .. '\n
' end function HorizontalRule() @@ -234,39 +234,39 @@ function CodeBlock(s, attr) -- If code block has class 'dot', pipe the contents through dot -- and base64, and include the base64-encoded png as a data: URL. if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then - local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s)) + local img = pipe('base64', {}, pipe('dot', {'-T' .. image_format}, s)) return '' -- otherwise treat as code (one could pipe through a highlighter) else - return "
" .. escape(s) ..
-           "
" + return '
' .. escape(s) ..
+           '
' end end function BulletList(items) local buffer = {} for _, item in pairs(items) do - table.insert(buffer, "
  • " .. item .. "
  • ") + table.insert(buffer, '
  • ' .. item .. '
  • ') end - return "
      \n" .. table.concat(buffer, "\n") .. "\n
    " + return '
      \n' .. table.concat(buffer, '\n') .. '\n
    ' end function OrderedList(items) local buffer = {} for _, item in pairs(items) do - table.insert(buffer, "
  • " .. item .. "
  • ") + table.insert(buffer, '
  • ' .. item .. '
  • ') end - return "
      \n" .. table.concat(buffer, "\n") .. "\n
    " + return '
      \n' .. table.concat(buffer, '\n') .. '\n
    ' end function DefinitionList(items) local buffer = {} for _,item in pairs(items) do local k, v = next(item) - table.insert(buffer, "
    " .. k .. "
    \n
    " .. - table.concat(v, "
    \n
    ") .. "
    ") + table.insert(buffer, '
    ' .. k .. '
    \n
    ' .. + table.concat(v, '
    \n
    ') .. '
    ') end - return "
    \n" .. table.concat(buffer, "\n") .. "\n
    " + return '
    \n' .. table.concat(buffer, '\n') .. '\n
    ' end -- Convert pandoc alignment to something HTML can use. @@ -303,13 +303,13 @@ function Table(caption, aligns, widths, headers, rows) local function add(s) table.insert(buffer, s) end - add("") - if caption ~= "" then - add("") + add('
    " .. escape(caption) .. "
    ') + if caption ~= '' then + add('') end if widths and widths[1] ~= 0 then for _, w in pairs(widths) do - add('') + add('') end end local header_row = {} @@ -317,7 +317,7 @@ function Table(caption, aligns, widths, headers, rows) for i, h in pairs(headers) do local align = html_align(aligns[i]) table.insert(header_row,'') - empty_header = empty_header and h == "" + empty_header = empty_header and h == '' end if not empty_header then add('') @@ -326,9 +326,9 @@ function Table(caption, aligns, widths, headers, rows) end add('') end - local class = "even" + local class = 'even' for _, row in pairs(rows) do - class = (class == "even" and "odd") or "even" + class = (class == 'even' and 'odd') or 'even' add('') for i,c in pairs(row) do add('') @@ -340,7 +340,7 @@ function Table(caption, aligns, widths, headers, rows) end function RawBlock(format, str) - if format == "html" then + if format == 'html' then return str else return '' @@ -348,7 +348,7 @@ function RawBlock(format, str) end function Div(s, attr) - return "\n" .. s .. "" + return '\n' .. s .. '' end -- The following code will produce runtime warnings when you haven't defined @@ -358,6 +358,6 @@ local meta = {} meta.__index = function(_, key) io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key)) - return function() return "" end + return function() return '' end end setmetatable(_G, meta) diff --git a/test/writer.custom b/test/writer.custom index ce490f426..eb53363fa 100644 --- a/test/writer.custom +++ b/test/writer.custom @@ -5,7 +5,7 @@ John Gruber’s markdown test suite.

    Headers

    - +

    Level 3 with emphasis

    @@ -525,7 +525,7 @@ Blah

    This is strong, and so is this.

    -

    An emphasized link.

    +

    An emphasized link.

    This is strong and em.

    @@ -560,7 +560,7 @@ So is ‘pine.’

    ‘He said, “I want to go.”’ Were you alive in the 70’s?

    -

    Here is some quoted ‘code’ and a “quoted link”.

    +

    Here is some quoted ‘code’ and a “quoted link”.

    Some dashes: one—two — three—four — five.

    @@ -660,70 +660,70 @@ So is ‘pine.’

    Explicit

    -

    Just a URL.

    +

    Just a URL.

    -

    URL and title.

    +

    URL and title.

    -

    URL and title.

    +

    URL and title.

    -

    URL and title.

    +

    URL and title.

    -

    URL and title

    +

    URL and title

    -

    URL and title

    +

    URL and title

    -

    with_underscore

    +

    with_underscore

    -

    Email link

    +

    Email link

    -

    Empty.

    +

    Empty.

    Reference

    -

    Foo bar.

    +

    Foo bar.

    -

    With embedded [brackets].

    +

    With embedded [brackets].

    -

    b by itself should be a link.

    +

    b by itself should be a link.

    -

    Indented once.

    +

    Indented once.

    -

    Indented twice.

    +

    Indented twice.

    -

    Indented thrice.

    +

    Indented thrice.

    This should [not][] be a link.

    [not]: /url
    -

    Foo bar.

    +

    Foo bar.

    -

    Foo biz.

    +

    Foo biz.

    With ampersands

    -

    Here’s a link with an ampersand in the URL.

    +

    Here’s a link with an ampersand in the URL.

    -

    Here’s a link with an amersand in the link text: AT&T.

    +

    Here’s a link with an amersand in the link text: AT&T.

    -

    Here’s an inline link.

    +

    Here’s an inline link.

    -

    Here’s an inline link in pointy braces.

    +

    Here’s an inline link in pointy braces.

    -

    With an ampersand: http://example.com/?foo=1&bar=2

    +

    With an ampersand: http://example.com/?foo=1&bar=2

    -

    An e-mail address:

    +

    An e-mail address:

    -

    Blockquoted: http://example.com/

    +

    Blockquoted: http://example.com/

    Auto-links should not occur here: <http://example.com/>

    @@ -740,7 +740,7 @@ So is ‘pine.’

    lalune
    lalune
    -

    Here is a movie icon.

    +

    Here is a movie icon.


    @@ -774,7 +774,7 @@ footnote (as with list items).

    lazy and just indent the first line of each block.

  • This is easier to type. Inline notes may contain -links and ] verbatim characters, +links and ] verbatim characters, as well as [bracketed text].

  • In quote.

  • In list.

  • ' .. escape(caption) .. '
    ' .. h .. '
    ' .. c .. '