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

create append_md and prepend_md methods #119

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

zkamvar
Copy link
Member

@zkamvar zkamvar commented Jun 21, 2024

This creates two new methods for yarn objects:

$append_md() and $prepend_md() which allows users to use XPath or nodes
to choose where to insert markdown.

library("tinkr")
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)

With prepend_md(), you can add elements you forgot such as table captions:

ex$prepend_md("Table: BIRDS, NERDS", ".//md:table")$tail(20)
#> 
#> Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
#> 
#> Table: BIRDS, NERDS
#> 
#> | scientific\_name            | common\_name         | n   | 
#> | :------------------------- | :------------------ | --: |
#> | Corvus corone              | Carrion Crow        | 288 | 
#> | Turdus merula              | Eurasian Blackbird  | 285 | 
#> | Anas platyrhynchos         | Mallard             | 273 | 
#> | Fulica atra                | Eurasian Coot       | 268 | 
#> | Parus major                | Great Tit           | 266 | 
#> | Podiceps cristatus         | Great Crested Grebe | 254 | 
#> | Ardea cinerea              | Gray Heron          | 236 | 
#> | Cygnus olor                | Mute Swan           | 234 | 
#> | Cyanistes caeruleus        | Eurasian Blue Tit   | 233 | 
#> | Chroicocephalus ridibundus | Black-headed Gull   | 223 | 
#> 
#> blabla

With append_md() you can add more context, such as adding a note after all
the headings

txt <- c("> Hello from *tinkr*!", ">", ">  :heart: R")
ex$append_md(txt, ".//md:heading")$show(10:30)
#> ```
#> 
#> ## R Markdown
#> 
#> > Hello from *tinkr*!
#> > 
#> > :heart: R
#> 
#> This is an ~~R Markdown document~~. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
#> 
#> When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#> 
#> ```{r, eval=TRUE, echo=TRUE}
#> summary(cars)
#> ```
#> 
#> ## Including Plots
#> 
#> > Hello from *tinkr*!
#> > 
#> > :heart: R

We can also make the heading text more exciting with exclamation points!

ex$append_md("!!!", ".//md:heading/*", space = FALSE)$show(10:30)
#> ```
#> 
#> ## R Markdown!!!
#> 
#> > Hello from *tinkr*!
#> > 
#> > :heart: R
#> 
#> This is an ~~R Markdown document~~. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
#> 
#> When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#> 
#> ```{r, eval=TRUE, echo=TRUE}
#> summary(cars)
#> ```
#> 
#> ## Including Plots!!!
#> 
#> > Hello from *tinkr*!
#> > 
#> > :heart: R

Created on 2024-06-21 with reprex v2.1.0

One huge benefit of this is that markdown can be inserted on a broad spectrum.

@zkamvar zkamvar mentioned this pull request Jun 22, 2024
14 tasks
@zkamvar zkamvar requested a review from maelle June 24, 2024 16:03
@zkamvar
Copy link
Member Author

zkamvar commented Sep 20, 2024

I just tried this in a real-world situation, and the $prepend_md() method has a bug where it will insert the markdown blocks in reverse order.

Copy link
Member

@maelle maelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@@ -2,6 +2,8 @@

## NEW FEATURES

* `yarn$append_md()` and `yarn$prepend_md()` methods allow you to add new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! curious to hear what your use case was?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to try to modify the installation section of hubverse packages to have "On the R Universe" and "Development" subsections, and this the previous method of needing to know the exact position of the node in the document was frustrating.


shove_nodes_in <- function(body, new, nodes, where = "after", space = TRUE) {
if (inherits(nodes, "character")) {
nodes <- xml2::xml_find_all(body, nodes, ns = md_ns())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error if none found?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the error that's right after might be too mysterious

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I'll try it out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, especially given that there is xml_missing

copy_xml(body)
}

shove_nodes_in <- function(body, new, nodes, where = "after", space = TRUE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want to start using devtag for documenting internal functions? https://github.com/moodymudskipper/devtag

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh I think I should!

class = "insert-md-body"
)
}
return(add_nodes_to_nodes(new, old = nodes, where = where, space = space))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why an explicit return()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was burned about this with JavaScript and vowed to always use the explicit return, but I know that it's against the standard R style.

R/class-yarn.R Outdated Show resolved Hide resolved
#' parameter.
#' @param space if `TRUE`, inline nodes will have a space inserted before
#' they are appended.
#' @details this is similar to the `add_md()` method except that it can do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be a @family tag in both manual pages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes!

R/class-yarn.R Outdated Show resolved Hide resolved
@maelle
Copy link
Member

maelle commented Sep 23, 2024

Not sure why it took me THREE MONTHS to have a look 🙈

@zkamvar
Copy link
Member Author

zkamvar commented Oct 16, 2024

Not sure why it took me THREE MONTHS to have a look 🙈

It's been a busy three months!

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.

2 participants