CDSoft / panda

Panda is a Pandoc Lua filter that works on internal Pandoc's AST. Panda is heavily inspired by [abp](http:/cdelord.fr/abp) reimplemented as a Pandoc Lua filter.
https://cdelord.fr/panda
GNU General Public License v3.0
43 stars 5 forks source link

Add 'caption="string"' and header=true/false options to include block #7

Closed bandel65 closed 9 months ago

bandel65 commented 2 years ago

When included file is a table, the caption must be a part of the included file or else the caption is not recognized. For example if table.md contains:

Col A|ColB
-----|-----
1|2

...and the following is specified in the main file:

:::{include table.md}
:::

Table: This is my Table

The caption will not render as a table caption. For that to work, table.md must contain the following:

Col A|ColB
-----|-----
1|2

Table: This is my Table

With the main file specifying only:

:::{include table.md}
:::

The problem with this is that if one is attempting to convert a pipe-delimited text file from Excel or some other input source, neither the header separator (i.e. "-----|-----") nor the Table caption will be applied. So it would be extremely useful if one could use a table.md file which looked like this:

Col A|ColB
1|2

...and state in the main file:

:::{include table.md caption="This is my Table" header=true}
:::

So that the result would render as:

Col A ColB
1 2

Table: This is my Table

CDSoft commented 2 years ago

I would prefer a more generic way to do it instead of having a specific case for tables in include. Maybe a Lua function to preprocess the content of the file before including it. I'll think about it.

As a quick workaround I would rather suggest using https://github.com/CDSoft/upp to include such tables. upp works on the file content, not on the Pandoc AST.

e.g.:

issue_7.md:

Table with a custom header and caption:

$(include_table "table.md" {header="---|---", caption="Caption"})

Same table with a default header and no caption:

$(include_table "table.md")

table.md:

a|b|c
d|e|f
g|h|i

include_table.lua:

-- this function shall be loaded by upp to include pipe separated tables
function include_table(name)
    local function render_table(conf)
        local t = {}
        for line in io.lines(name) do table.insert(t, line) end
        if conf.header then
            local header = type(conf.header) == "boolean"
                and t[1]:gsub("[^%|]+", "-") -- default header separator deduced from the first line
                or tostring(conf.header)     -- custom header
            table.insert(t, 2, header)
        end
        if conf.caption then table.insert(t, "\n: "..conf.caption) end
        return table.concat(t, "\n")
    end
    return setmetatable({}, {
        __call = function(_, conf) return render_table(conf) end,
        __tostring = function() return render_table{header=true} end,
    })
end

Then you can chain upp and panda. E.g.:

upp -l include_table.lua issue_7.md | panda -t markdown

gives:

Table with a custom header and caption:

  a   b
  --- ---
  d   e
  g   h

  : Caption

Same table with a default header and no caption:

  a   b   c
  --- --- ---
  d   e   f
  g   h   i
bandel65 commented 2 years ago

Thanks. I’ll look into that.

From: CDSoft @.> Sent: Thursday, November 4, 2021 9:33 AM To: CDSoft/panda @.> Cc: Bill Andel @.>; Author @.> Subject: [External] Re: [CDSoft/panda] Add 'caption="string"' and header=true/false options to include block (Issue #7)

I would prefer a more generic way to do it instead of having a specific case for tables in include. Maybe a Lua function to preprocess the content of the file before including it. I'll think about it.

As a quick workaround I would rather suggest using https://github.com/CDSoft/upp to include such tables. upp works on the file content, not on the Pandoc AST.

e.g.:

issue_7.md:

Table with a custom header and caption:

$(include_table "table.md" {header="---|---", caption="Caption"})

Same table with a default header and no caption:

$(include_table "table.md")

table.md:

a|b|c

d|e|f

g|h|i

include_table.lua:

-- this function shall be loaded by upp to include pipe separated tables

function include_table(name)

local function render_table(conf)

    local t = {}

    for line in io.open(name):lines() do table.insert(t, line) end

    if conf.header then

        local header = type(conf.header) == "boolean"

            and t[1]:gsub("[^%|]+", "-") -- default header separator deduced from the first line

            or tostring(conf.header)     -- custom header

        table.insert(t, 2, header)

    end

    if conf.caption then table.insert(t, "\n: "..conf.caption) end

    return table.concat(t, "\n")

end

return setmetatable({}, {

    __call = function(_, conf) return render_table(conf) end,

    __tostring = function() return render_table{header=true} end,

})

end

Then you can chain upp and panda. E.g.:

upp -l include_table.lua issue_7.md | panda -t markdown

gives:

Table with a custom header and caption:

a b


d e

g h

: Caption

Same table with a default header and no caption:

a b c


d e f

g h i

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/CDSoft/panda/issues/7#issuecomment-961214248, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AWKXBGNXICR2IOPKKG5K56DUKKYU5ANCNFSM5HKFV74A. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

bandel65 commented 2 years ago

Just a thought: what about...

:::(include=xyz.csv)

Table: This is my caption :::

Right now the actual contents of the block are being thrown away. Could they be appended to the included file instead?