ropensci / tinkr

Convert (R)Markdown files to XML, edit them, write them back as (R)Markdown
https://docs.ropensci.org/tinkr
GNU General Public License v3.0
57 stars 3 forks source link

footnote bug? #92

Closed maelle closed 1 year ago

maelle commented 1 year ago
lines <- c("lala[^1]", "", "[^1]: pof")

temp_file <- withr::local_tempfile()
brio::write_lines(lines, temp_file)

tinkr::yarn$new(temp_file)$write(temp_file)

brio::read_lines(temp_file)
#> [1] "lala[^1][^1]" ""             "[^1]: pof"    ""             ""            
#> [6] ""

# second one
lines <- c("lala[^1] blabla", "", "[^1]: pof")

temp_file <- withr::local_tempfile()
brio::write_lines(lines, temp_file)

tinkr::yarn$new(temp_file)$write(temp_file)

brio::read_lines(temp_file)
#> [1] "lala[^1][^1] blabla" ""                    "[^1]: pof"          
#> [4] ""                    ""                    ""

Created on 2023-02-23 with reprex v2.0.2

zkamvar commented 1 year ago

OMG why didn't I see this!?

maelle commented 1 year ago

Maybe because you write clear text that doesn't need footnotes?!

zkamvar commented 1 year ago

Maybe because you write clear text that doesn't need footnotes?!

*wheezing*

Funny enough, the most recent update of {commonmark} has "footnotes" support, but it's a bit strange:

txt <- c("a statement[^1][^2]\n", "[^1]: this is true", "[^2]: this is false")
commonmark::markdown_xml(txt, footnotes = TRUE) |> writeLines()
#> <?xml version="1.0" encoding="UTF-8"?>
#> <!DOCTYPE document SYSTEM "CommonMark.dtd">
#> <document xmlns="http://commonmark.org/xml/1.0">
#>   <paragraph>
#>     <text xml:space="preserve">a statement</text>
#>     <<unknown> />
#>     <<unknown> />
#>   </paragraph>
#>   <<unknown>>
#>     <paragraph>
#>       <text xml:space="preserve">this is true</text>
#>     </paragraph>
#>   </<unknown>>
#>   <<unknown>>
#>     <paragraph>
#>       <text xml:space="preserve">this is false</text>
#>     </paragraph>
#>   </<unknown>>
#> </document>

Created on 2023-03-22 with reprex v2.0.2

zkamvar commented 1 year ago

OH I think I know what's going on! {tinkr} sees this footnote as a relative link:

lines <- c("[thing] lala[^1] blabla", "", "[thing]: what", "[^1]: pof")

temp_file <- withr::local_tempfile()
brio::write_lines(lines, temp_file)

tinkr::yarn$new(temp_file)$show()
#> [thing][thing] lala[^1][^1] blabla
#> 
#> [thing]: what
#> [^1]: pof

Created on 2023-03-28 with reprex v2.0.2

zkamvar commented 1 year ago

The happy thing is that we can fix this with a little XSLT!

zkamvar commented 1 year ago

For reference, the fix for this issue is in https://github.com/ropensci/tinkr/pull/94/commits/beb8277a641fe8e798cdf27b5c5f04cf59b45a54

zkamvar commented 1 year ago

this has been fixed in #94

maelle commented 1 year ago

thanks so much!