hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

All formatting lost with Markdown (even when supported) #83

Closed andrewheiss closed 6 years ago

andrewheiss commented 6 years ago

Markdown has limited support for formatting (i.e. no cell background colors, borders, column spans, etc.), but it does support bold and italics. Currently with print_md() and to_md(), bold and italic formatting is discarded:

ht <- huxtable(a = 1:5, b = 5:1) %>% 
  set_bold(2:4, 1:2, TRUE) %>% 
  set_italic(5, 1:2, TRUE)

# In RStudio this prints rows 2-4 in bold and row 5 in italic
print_screen(ht)

# This generates HTML that wraps cells in <span style="font-weight: bold; "> or
# <span style="font-style: italic; ">
print_html(ht)

# This discards all styles and does not generate cells wrapped in **X** or *X*
print_md(ht)

#> ---------------------------------
#>                1               5 
#> ---------------- ----------------
#>                2               4 
#>                                  
#>                3               3 
#>                                  
#>                4               2 
#>                                  
#>                5               1 
#> ---------------------------------

It would be cool if huxtable could maintain bold and italic and generate Markdown tables like this (though I don't know how difficult implementing that would be):

#> ---------------------------------
#>                1               5 
#> ---------------- ----------------
#>            **2**            **4** 
#>                                  
#>            **3**            **3** 
#>                                  
#>            **4**            **2** 
#>                                  
#>              *5*              *1* 
#> ---------------------------------

If that's not possible, issuing a warning similar to those created when using huxreg() (e.g. Markdown cannot handle cells with colspan/rowspan > 1) could be helpful to warn users that the bold and italics are gone.

hughjonesd commented 6 years ago

Could you try github master and report if it works for you?

andrewheiss commented 6 years ago

Awesome, that works! Thanks!

print_md(ht)
#> -----------------------
#>           1          5 
#> ----------- -----------
#>       **2**      **4** 
#>                        
#>       **3**      **3** 
#>                        
#>       **4**      **2** 
#>                        
#>         *5*        *1* 
#> -----------------------
hughjonesd commented 6 years ago

Actually, could you try again with the latest version? I had to rewrite it to deal with strings that break over multiple lines, and I'm not certain I've got it right (or can test it easily...)

andrewheiss commented 6 years ago

Yep, here's what I'm getting now:

library(tidyverse)
library(huxtable)

df <- tribble(
  ~a, ~b,
  1,  "This is a test.",
  2,  "This is a test with two lines.\n\nHere's the second line.",
  3,  "This is a test with a bunch of lines\n\nthat just keep going and does it break anything\n\n if it goes this long maybe I don't know yet\n\nbut I'm still typing and that's probably\n\nenough text for now."
)

ht <- df %>% 
  huxtable() %>% 
  set_bold(2:3, 2, TRUE)

print_md(ht)
#> ---------------------------------------------------
#>           1 This is a test.                        
#> ----------- ---------------------------------------
#>           2 **This is a test with two lines.**     
#>             ****                                   
#>             **Here's the second line.**            
#>                                                    
#>           3 **This is a test with a bunch of**     
#>             **lines**                              
#>             ****                                   
#>             **that just keep going and does it**   
#>             **break anything**                     
#>             ****                                   
#>             **if it goes this long maybe I don't** 
#>             **know yet**                           
#>             ****                                   
#>             **but I'm still typing and that's**    
#>             **probably**                           
#>             ****                                   
#>             **enough text for now.**               
#> ---------------------------------------------------

It's wrapping each broken line in bold, including empty lines (hence the ****). Ideally it should wrap the entire line with **s, even if broken in the middle, I think. Like this?

---------------------------------------------------
          1 This is a test.                        
----------- ---------------------------------------
          2 **This is a test with two lines.**     

            **Here's the second line.**            

          3 **This is a test with a bunch of     
            lines**                              

            **that just keep going and does it   
            break anything**                     

            **if it goes this long maybe I don't 
            know yet**                           

            **but I'm still typing and that's    
            probably**                           

            **enough text for now.**               
---------------------------------------------------
hughjonesd commented 6 years ago

So what's wrong at the moment is that even empty lines get wrapped in bold. Hmm, does that actually make it compile wrong to HTML or PDF?

andrewheiss commented 6 years ago

The ****s in empty lines trip up pandoc. This markdown file:

# Table with broken lines wrapped in `**`s

----------------------------------------------------
           1 This is a test.                        
------------ ---------------------------------------
           2 **This is a test with two lines.**     
             ****                                   
             **Here's the second line.**            

           3 **This is a test with a bunch of**     
             **lines**                              
             ****                                   
             **that just keep going and does it**   
             **break anything**                     
             ****                                   
             **if it goes this long maybe I don't** 
             **know yet**                           
             ****                                   
             **but I'm still typing and that's**    
             **probably**                           
             ****                                   
             **enough text for now.**               
----------------------------------------------------

# Table with complete lines wrapped in `**`s

---------------------------------------------------
          1 This is a test.                        
----------- ---------------------------------------
          2 **This is a test with two lines.**     

            **Here's the second line.**            

          3 **This is a test with a bunch of     
            lines**                              

            **that just keep going and does it   
            break anything**                     

            **if it goes this long maybe I don't 
            know yet**                           

            **but I'm still typing and that's    
            probably**                           

            **enough text for now.**               
---------------------------------------------------

…creates HTML that looks like this (using this pandoc command: pandoc testing.md -s -t html5 -o testing.html)

<h1 id="table-with-broken-lines-wrapped-in-s">Table with broken lines wrapped in <code>**</code>s</h1>
<table style="width:72%;">
<colgroup>
<col style="width: 18%" />
<col style="width: 54%" />
</colgroup>
<thead>
<tr class="header">
<th style="text-align: right;">1</th>
<th style="text-align: left;">This is a test.</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2</td>
<td style="text-align: left;"><strong>This is a test with two lines.</strong> **** <strong>Here’s the second line.</strong></td>
</tr>
<tr class="even">
<td style="text-align: right;">3</td>
<td style="text-align: left;"><strong>This is a test with a bunch of</strong> <strong>lines</strong> **** <strong>that just keep going and does it</strong> <strong>break anything</strong> **** <strong>if it goes this long maybe I don’t</strong> <strong>know yet</strong> **** <strong>but I’m still typing and that’s</strong> <strong>probably</strong> **** <strong>enough text for now.</strong></td>
</tr>
</tbody>
</table>
<h1 id="table-with-complete-lines-wrapped-in-s">Table with complete lines wrapped in <code>**</code>s</h1>
<table style="width:71%;">
<colgroup>
<col style="width: 16%" />
<col style="width: 54%" />
</colgroup>
<thead>
<tr class="header">
<th style="text-align: right;">1</th>
<th style="text-align: left;">This is a test.</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2</td>
<td style="text-align: left;"><strong>This is a test with two lines.</strong></td>
</tr>
<tr class="even">
<td style="text-align: right;"></td>
<td style="text-align: left;"><strong>Here’s the second line.</strong></td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: left;"><strong>This is a test with a bunch of lines</strong></td>
</tr>
<tr class="even">
<td style="text-align: right;"></td>
<td style="text-align: left;"><strong>that just keep going and does it break anything</strong></td>
</tr>
<tr class="odd">
<td style="text-align: right;"></td>
<td style="text-align: left;"><strong>if it goes this long maybe I don’t know yet</strong></td>
</tr>
<tr class="even">
<td style="text-align: right;"></td>
<td style="text-align: left;"><strong>but I’m still typing and that’s probably</strong></td>
</tr>
<tr class="odd">
<td style="text-align: right;"></td>
<td style="text-align: left;"><strong>enough text for now.</strong></td>
</tr>
</tbody>
</table>

Or

image
hughjonesd commented 6 years ago

Well, in any case, latest push should fix.

hughjonesd commented 6 years ago

Try now.

andrewheiss commented 6 years ago

Cool, that mostly fixes it! The only weirdness is that broken lines get extra </strong> <strong> breaks in the middle when converted to HTML and TeX, but it still works fine:

<strong>This is a test with a bunch of</strong> <strong>lines</strong>

and

\textbf{This is a test with a bunch of} \textbf{lines}

instead of

<strong>This is a test with a bunch of lines</strong>
\textbf{This is a test with a bunch of lines}