From 6e8bf1f378f0595b1f076c0e35369f6c8395beca Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Mon, 19 May 2025 09:16:24 +0900 Subject: [PATCH 1/2] format(stylua): on lua filters --- inst/lua/blocks-to-inlines.lua | 11 +++---- inst/lua/cite.lua | 10 +++--- inst/lua/inline-code.lua | 18 +++++------ inst/lua/math.lua | 56 +++++++++++++++------------------- inst/lua/smart.lua | 12 ++++---- 5 files changed, 50 insertions(+), 57 deletions(-) diff --git a/inst/lua/blocks-to-inlines.lua b/inst/lua/blocks-to-inlines.lua index 497ab3d..9c04fd5 100644 --- a/inst/lua/blocks-to-inlines.lua +++ b/inst/lua/blocks-to-inlines.lua @@ -1,12 +1,11 @@ function Meta(meta) - sep = meta.sep_blocks and ( - meta.sep_blocks.t and meta.sep_blocks or {pandoc.Str(meta.sep_blocks)} - ) or {pandoc.LineBreak(), pandoc.LineBreak()} + sep = meta.sep_blocks and (meta.sep_blocks.t and meta.sep_blocks or { pandoc.Str(meta.sep_blocks) }) + or { pandoc.LineBreak(), pandoc.LineBreak() } end function Div(div) - div.content = {pandoc.Para(pandoc.utils.blocks_to_inlines(div.content, sep))} - return div + div.content = { pandoc.Para(pandoc.utils.blocks_to_inlines(div.content, sep)) } + return div end -return {{Meta = Meta}, {Div = Div}} +return { { Meta = Meta }, { Div = Div } } diff --git a/inst/lua/cite.lua b/inst/lua/cite.lua index 82a9435..bd6a64d 100644 --- a/inst/lua/cite.lua +++ b/inst/lua/cite.lua @@ -1,12 +1,12 @@ cite = {} function Cite(elem) - table.insert(cite, elem) - table.insert(cite, pandoc.Space()) + table.insert(cite, elem) + table.insert(cite, pandoc.Space()) end function Pandoc(doc) - table.remove(cite) - doc.blocks = {pandoc.Para(cite)} - return doc + table.remove(cite) + doc.blocks = { pandoc.Para(cite) } + return doc end diff --git a/inst/lua/inline-code.lua b/inst/lua/inline-code.lua index fd60cd9..3dd967b 100644 --- a/inst/lua/inline-code.lua +++ b/inst/lua/inline-code.lua @@ -1,11 +1,11 @@ function Code(elem) - if elem.attributes['font.family'] == nil then - elem.attributes['font.family'] = 'monospace' - end - if elem.attributes['shading.color'] == nil then - elem.attributes['shading.color'] = '#f8f8f8' - end - local span = pandoc.Span(elem.text) - span.attr = elem.attr - return span + if elem.attributes["font.family"] == nil then + elem.attributes["font.family"] = "monospace" + end + if elem.attributes["shading.color"] == nil then + elem.attributes["shading.color"] = "#f8f8f8" + end + local span = pandoc.Span(elem.text) + span.attr = elem.attr + return span end diff --git a/inst/lua/math.lua b/inst/lua/math.lua index 3e541a3..091d627 100644 --- a/inst/lua/math.lua +++ b/inst/lua/math.lua @@ -16,46 +16,40 @@ https://github.com/atusy/lua-filters/blob/master/lua/math.lua is_pandoc_2_10 = (PANDOC_VERSION[1] >= 2) and (PANDOC_VERSION[2] >= 10) if pandoc.system.os ~= "mingw32" then - function math2html(text) - return pandoc.pipe(cmd, {"-t", "html", "-f", "markdown"}, text) - end + function math2html(text) + return pandoc.pipe(cmd, { "-t", "html", "-f", "markdown" }, text) + end else - if is_pandoc_2_10 then - with_temporary_directory = pandoc.system.with_temporary_directory - else - with_temporary_directory = function(ignored, callback) - return callback(temporary_directory) - end - end - - math2html = function(text) - local function callback(directory) - local path = directory .. "\\math-rendered-by-lua-filter.html" - pandoc.pipe(cmd, {"-t", "html", "-f", "markdown", "-o", path}, text) - return io.open(path):read("a") - end - - return with_temporary_directory("write_html_math", callback) - end + if is_pandoc_2_10 then + with_temporary_directory = pandoc.system.with_temporary_directory + else + with_temporary_directory = function(ignored, callback) + return callback(temporary_directory) + end + end + + math2html = function(text) + local function callback(directory) + local path = directory .. "\\math-rendered-by-lua-filter.html" + pandoc.pipe(cmd, { "-t", "html", "-f", "markdown", "-o", path }, text) + return io.open(path):read("a") + end + + return with_temporary_directory("write_html_math", callback) + end end function Meta(elem) - cmd = elem["pandoc-path"] and ( - pandoc.utils.stringify(elem["pandoc-path"]) - ) or "pandoc" + cmd = elem["pandoc-path"] and (pandoc.utils.stringify(elem["pandoc-path"])) or "pandoc" - temporary_directory = elem["temporary-directory"] and ( - pandoc.utils.stringify(elem["temporary-directory"]) - ) or "." + temporary_directory = elem["temporary-directory"] and (pandoc.utils.stringify(elem["temporary-directory"])) or "." end function Math(elem) - return pandoc.read( - math2html("$" .. elem.text:gsub("[\n\r]", "") .. "$"), "html" - ).blocks[1].content[1].content + return pandoc.read(math2html("$" .. elem.text:gsub("[\n\r]", "") .. "$"), "html").blocks[1].content[1].content end return { - {Meta = Meta}, - {Math = Math} + { Meta = Meta }, + { Math = Math }, } diff --git a/inst/lua/smart.lua b/inst/lua/smart.lua index 4f526f1..d343fd0 100644 --- a/inst/lua/smart.lua +++ b/inst/lua/smart.lua @@ -15,13 +15,13 @@ https://github.com/atusy/lua-filters/blob/master/lua/smart.lua ]] local quotes = { - SingleQuote = {pandoc.Str("‘"), pandoc.Str("’")}, - DoubleQuote = {pandoc.Str("“"), pandoc.Str("”")} + SingleQuote = { pandoc.Str("‘"), pandoc.Str("’") }, + DoubleQuote = { pandoc.Str("“"), pandoc.Str("”") }, } function Quoted(elem) - local q = quotes[elem.quotetype] - table.insert(elem.content, 1, q[1]) - table.insert(elem.content, q[2]) - return elem.content + local q = quotes[elem.quotetype] + table.insert(elem.content, 1, q[1]) + table.insert(elem.content, q[2]) + return elem.content end From 0f0b54f4971b779a4d71a98a3f68ca2066ae3127 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Mon, 19 May 2025 09:31:59 +0900 Subject: [PATCH 2/2] fix(lua): avoid unintentional global variables --- inst/lua/blocks-to-inlines.lua | 2 ++ inst/lua/cite.lua | 2 +- inst/lua/math.lua | 22 ++++++++++++---------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/inst/lua/blocks-to-inlines.lua b/inst/lua/blocks-to-inlines.lua index 9c04fd5..7f8100d 100644 --- a/inst/lua/blocks-to-inlines.lua +++ b/inst/lua/blocks-to-inlines.lua @@ -1,3 +1,5 @@ +local sep = {} + function Meta(meta) sep = meta.sep_blocks and (meta.sep_blocks.t and meta.sep_blocks or { pandoc.Str(meta.sep_blocks) }) or { pandoc.LineBreak(), pandoc.LineBreak() } diff --git a/inst/lua/cite.lua b/inst/lua/cite.lua index bd6a64d..1490e2d 100644 --- a/inst/lua/cite.lua +++ b/inst/lua/cite.lua @@ -1,4 +1,4 @@ -cite = {} +local cite = {} function Cite(elem) table.insert(cite, elem) diff --git a/inst/lua/math.lua b/inst/lua/math.lua index 091d627..14137ad 100644 --- a/inst/lua/math.lua +++ b/inst/lua/math.lua @@ -13,40 +13,42 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI https://github.com/atusy/lua-filters/blob/master/lua/math.lua ]] -is_pandoc_2_10 = (PANDOC_VERSION[1] >= 2) and (PANDOC_VERSION[2] >= 10) +local is_pandoc_2_10 = (PANDOC_VERSION[1] >= 2) and (PANDOC_VERSION[2] >= 10) + +local L = {} if pandoc.system.os ~= "mingw32" then - function math2html(text) + function L.math2html(cmd, text) return pandoc.pipe(cmd, { "-t", "html", "-f", "markdown" }, text) end else if is_pandoc_2_10 then - with_temporary_directory = pandoc.system.with_temporary_directory + L.with_temporary_directory = pandoc.system.with_temporary_directory else - with_temporary_directory = function(ignored, callback) - return callback(temporary_directory) + function L.with_temporary_directory(_, callback) + return callback(L.temporary_directory) end end - math2html = function(text) + function L.math2html(cmd, text) local function callback(directory) local path = directory .. "\\math-rendered-by-lua-filter.html" pandoc.pipe(cmd, { "-t", "html", "-f", "markdown", "-o", path }, text) return io.open(path):read("a") end - return with_temporary_directory("write_html_math", callback) + return L.with_temporary_directory("write_html_math", callback) end end function Meta(elem) - cmd = elem["pandoc-path"] and (pandoc.utils.stringify(elem["pandoc-path"])) or "pandoc" + L.cmd = elem["pandoc-path"] and (pandoc.utils.stringify(elem["pandoc-path"])) or "pandoc" - temporary_directory = elem["temporary-directory"] and (pandoc.utils.stringify(elem["temporary-directory"])) or "." + L.temporary_directory = elem["temporary-directory"] and (pandoc.utils.stringify(elem["temporary-directory"])) or "." end function Math(elem) - return pandoc.read(math2html("$" .. elem.text:gsub("[\n\r]", "") .. "$"), "html").blocks[1].content[1].content + return pandoc.read(L.math2html(L.cmd, "$" .. elem.text:gsub("[\n\r]", "") .. "$"), "html").blocks[1].content[1].content end return {