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

Support custom-style attribute for docx table #10009

Merged
merged 1 commit into from
Sep 10, 2024
Merged

Conversation

Sebbones
Copy link
Contributor

This is a suggested fix to support the custom-style attribute for docx tables. I'm referring to the issues #9603, #7549, #6496, #4697. I needed this feature to support multiple table styles in one document and saw a lot of issues addressing this feature. With absolutely no prior experience in Haskell, I just gave it a shot :)

The conversion from html to docx with and without it, worked fine. With the table[custom-style="CustomStyle"] attribute, it used the CustomStyle for the table and if omitted, the fallback/default style is "Table".

I hope the code is okayish, but happy to change it if needed.

@jgm
Copy link
Owner

jgm commented Sep 10, 2024

Perfect! Thanks.

@jgm jgm merged commit 7e04673 into jgm:main Sep 10, 2024
9 of 12 checks passed
@Sebbones Sebbones deleted the issue7549 branch September 10, 2024 19:46
@tdewin
Copy link

tdewin commented Sep 19, 2024

FINALLY, thank you soo soo soo much for those 4 golden lines of code

Since I'm editing in markdown and you can't seem to add the style on a table (as far as I can tell?), I wrote a small luafilter

https://github.com/tdewin/pandoc-lua-timo-filters/blob/main/defaulttabstyle.lua

You can pass a default value for the tables with

-M tablestyle:"table"

You can override a value in text with a code block

```tablemeta{custom-style=tablestylecustom}
```

  ----------------------------------------------------------------- 
  test   test2   test\
                 dezdezdez dezd ze dez d ez d ez dez dez de z dez
  ------ ------- --------------------------------------------------
  test   test2   test3

  -----------------------------------------------------------------


  ----------------------------------------------------------------- 
  test   test2   test\
                 dezdezdez dezd ze dez d ez d ez dez dez de z dez
  ------ ------- --------------------------------------------------
  test   test2   test3

  -----------------------------------------------------------------

@R3myG
Copy link

R3myG commented Oct 31, 2024

Would it be possible to have an example of how to use this new parameter?
I'm using Quarto 1.6.32 which uses Pandoc 3.4 and the following didn't work:

::: {custom-style="TableCustom"}
| Header 1 | Header 2 |
|----------|----------|
| Val1         |    Val2      |
:::
| Header 1 | Header 2 |
|----------|----------|
| Val1         |    Val2      |
: {custom-style="TableCustom"}

@tarleb
Copy link
Collaborator

tarleb commented Oct 31, 2024

Pandoc Markdown doesn't yet have built-in syntax for table attributes. With --from=commonmark_x one would write

{custom-style="TableCustom"}
| Header 1 | Header 2 |
|----------|----------|
| Val1     |    Val2      |

For pandoc Markdown it's best to use the Div-based syntax, but that requires an additional Lua filter like the one below (untested)

function Div (div)
  -- do nothing if the div doesn't have a custom-style attribute
  -- or if the div doesn't have exactly one element
  if not div.attributes['custom-style'] or #div.content == 1 then
    return div
  end
  local tbl = div.content[1]
  if tbl.t == 'Table' then
    tbl.attributes['custom-style'] = div.attributes['custom-style']
    return tbl
  end
end

@R3myG
Copy link

R3myG commented Jan 21, 2025

@tarleb Thank you for the suggestion, I've tested it in quarto and couldn't make it work unfortunately.

I had to use the following notation because of quarto:

::: {custom-style="TableCustom"}
| Header 1 | Header 2 |
|----------|----------|
| Val1     |    Val2      |
:::

I also had to adapt the lua code to:

function Div (div)
    -- First, check if we have the custom-style attribute
    if not div.attributes['custom-style'] then
        return div
    end

    -- Check if div has content
    if not div.content or #div.content == 0 then
        return div
    end

    -- Get the first element
    local first_elem = div.content[1]
    
    -- Check if it's a table
    if first_elem and first_elem.t == 'Table' then
        -- Copy the custom-style attribute to the table
        if not first_elem.attributes then
            first_elem.attributes = {}
        end
        first_elem.attributes['custom-style'] = div.attributes['custom-style']
        return first_elem
    end

    -- If we get here, return the original div
    return div
end

Unfortunately, afterwards the tables were still using the Table style.

If you have any suggestions on how to make it work, don't hesitate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants