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

Tables #6

Merged
merged 6 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/to_xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ to_xml <- function(path, encoding = "UTF-8"){

splitted_content$body %>%
clean_content() %>%
commonmark::markdown_xml(extensions = FALSE) %>%
commonmark::markdown_xml(extensions = TRUE) %>%
xml2::read_xml(encoding = encoding) -> body

if(stringr::str_detect(fs::path_ext(path), "[Rr]")){
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ yaml_xml_list$body

## Details/notes

* The (R)md to XML to (R)md loop on which `tinkr` is based is slightly lossy because of Markdown syntax redundancy. For instance lists can be created with either "+", "-" or "*". When using `tinkr`, the (R)md after editing will only use "-" for lists. Such losses make your (R)md different, and the git diff a bit harder to parse, but should _not_ change the documents your (R)md is rendered to. If it does, report a bug in the issue tracker!
* The (R)md to XML to (R)md loop on which `tinkr` is based is slightly lossy because of Markdown syntax redundancy. For instance
* lists can be created with either "+", "-" or "*". When using `tinkr`, the (R)md after editing will only use "-" for lists.
* Tables are not pretty anymore (only three dashes for each cell cf [spec](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#tables)). Alignment of columns is lost.

Such losses make your (R)md different, and the git diff a bit harder to parse, but should _not_ change the documents your (R)md is rendered to, apart from the tables alignment. If it does, report a bug in the issue tracker!

* At the moment the XLST stylesheet used to cast XML back to Markdown doesn't support extensions (striked through text, tables) so when converting the Markdown files to XML the package uses `extensions=FALSE`. This means tables in the XML are text, not easy to edit.
* At the moment the XLST stylesheet used to cast XML back to Markdown doesn't support striked through text.


15 changes: 15 additions & 0 deletions inst/extdata/example2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ plot(pressure)


Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

| 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
29 changes: 29 additions & 0 deletions inst/extdata/xml2md.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,33 @@ Example usage with xsltproc:
</xsl:choose>
</xsl:template>

<!-- Table -->

<xsl:template match="md:table">
<xsl:apply-templates select="." mode="indent-block"/>
<xsl:apply-templates select="md:*"/>
</xsl:template>

<xsl:template match="md:table_header">
<xsl:text>| </xsl:text>
<xsl:apply-templates select="md:*"/>
<xsl:text>&#xa; | </xsl:text>
<xsl:for-each select="md:table_cell">
<xsl:text> --- |</xsl:text>
</xsl:for-each>
<xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="md:table_cell">
<xsl:apply-templates select="md:*"/>
<xsl:text>| </xsl:text>
</xsl:template>

<xsl:template match="md:table_row">
<xsl:text>| </xsl:text>
<xsl:apply-templates select="md:*"/>
<xsl:text>&#xa;</xsl:text>
</xsl:template>


</xsl:stylesheet>
12 changes: 12 additions & 0 deletions inst/scripts/tables_experiments.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# From Markdown to XML
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
yaml_xml_list <- to_xml(path)

library("magrittr")
body <- yaml_xml_list$body
body %>%
xml2::xml_find_all(xpath = './/d1:table',
xml2::xml_ns(.))
to_md(yaml_xml_list, "inst/scripts/test.md")


51 changes: 51 additions & 0 deletions inst/scripts/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: "Untitled"
author: "M. Salmon"
date: "September 6, 2018"
output: html_document
---

```{r setup, include="FALSE", eval="TRUE"}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

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](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

You can also embed plots, for example:

```{python, fig.cap="pretty plot", echo=-c(1, 2), eval=TRUE}
plot(pressure)
```

```{python}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

| 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