Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert the table of arguments to the sections of arguments with Lua filter #534

Merged
merged 4 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.13

**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)

# 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