Skip to content

Commit

Permalink
Merge pull request #534 from atusy/args
Browse files Browse the repository at this point in the history
convert the table of arguments to the sections of arguments with Lua filter
  • Loading branch information
renkun-ken authored Apr 16, 2022
2 parents 699cb08 + 7eefe08 commit b7f15d3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: languageserver
Title: Language Server Protocol
Version: 0.3.12
Version: 0.3.12.9000
Date: 2021-10-18
Authors@R:
c(person(given = "Randy",
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# languageserver 0.3.12.9000 (Development version)

**Closed issues:**

- Some of the tables of arguments are too complex to be represented by GFM (#533)

**Merged pull requests.**

- Convert the table of arguments to the sections of arguments with Lua filter (#534, thanks @atusy)

# languageserver 0.3.12

**Closed issues:**
Expand Down
6 changes: 4 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,10 @@ html_to_markdown <- function(html) {
logger$info("Converting html to markdown using", html_file, md_file)
stringi::stri_write_lines(html, html_file)
result <- tryCatch({
format <- if (rmarkdown::pandoc_version() >= "2.0") "gfm" else "markdown_github"
rmarkdown::pandoc_convert(html_file, to = format, output = md_file)
pandoc2 <- rmarkdown::pandoc_version() >= "2.0"
format <- if (pandoc2) "gfm" else "markdown_github"
options <- if (pandoc2) c("--lua-filter", system.file(package = "languageserver", "lua/html-to-markdown.lua"))
rmarkdown::pandoc_convert(html_file, to = format, output = md_file, options = options)
paste0(stringi::stri_read_lines(md_file, encoding = "utf-8"), collapse = "\n")
}, error = function(e) {
logger$info("html_to_markdown failed: ", conditionMessage(e))
Expand Down
39 changes: 39 additions & 0 deletions inst/lua/html-to-markdown.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--[[
Converts the Arguments from a table to sections
Conversion happens if the table cannot be represented by the GFM-syntax.
Cells in the 1st column become headers and those in the 2nd column becomes
content of the corresponding headers.
--]]
function Table(el)
local tb = pandoc.utils.to_simple_table(el)

-- Do nothing if the table has headers or is not composed of 2 columns
if #tb.headers > 0 or #tb.rows[1] ~= 2 then return end

-- Do nothing if the 1st column has cells not comprised of single Plain block
-- or if the 2nd column can be represented by the GFM-syntax.
local function unlike_para(x)
return x.t ~= "Plain" and x.t ~= "Para"
end
local complex_table = false
for _, row in pairs(tb.rows) do
if (#row[1] ~= 1) or unlike_para(row[1][1]) then return end
complex_table = complex_table or #row[2] > 1 or unlike_para(row[2][1])
end
if not complex_table then return end

-- Conversion
local header
local blocks = {}
for _, row in pairs(tb.rows) do
header = row[1][1].content
table.insert(header, pandoc.Str(':'))
table.insert(blocks, pandoc.Header(4, header))
for _, b in pairs(row[2]) do
table.insert(blocks, b.t == "Plain" and pandoc.Para(b.content) or b)
end
end
return blocks
end

0 comments on commit b7f15d3

Please sign in to comment.