Created
August 29, 2018 14:43
-
-
Save maelle/8c4316286c686519911cdce24ea9fa83 to your computer and use it in GitHub Desktop.
How to update headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` r | |
library("magrittr") | |
homogeneize <- function(path){ | |
path %>% | |
readLines(encoding = "UTF-8") %>% | |
blogdown:::split_yaml_body() %>% | |
.$body %>% | |
commonmark::markdown_xml(extensions = TRUE) %>% | |
xml2::read_xml() -> post_xml | |
headers <- post_xml %>% | |
xml2::xml_find_all(xpath = './/d1:heading', | |
xml2::xml_ns(.)) | |
levels <- xml2::xml_attr(headers, "level") %>% | |
as.character() %>% | |
as.numeric() | |
no_levels <- length(unique(levels)) | |
if(no_levels == 1){ | |
xml2::xml_set_attr(headers, "level", "1") | |
} | |
if(no_levels == 2){ | |
headers1 <- headers[xml2::xml_attr(headers, "level") == min(levels)] | |
xml2::xml_set_attr(headers1, "level", "1") | |
headers2 <- headers[xml2::xml_attr(headers, "level") == max(levels)] | |
xml2::xml_set_attr(headers2, "level", "2") | |
} | |
if(no_levels == 3){ | |
headers1 <- headers[xml2::xml_attr(headers, "level") == min(levels)] | |
xml2::xml_set_attr(headers1, "level", "1") | |
headers3 <- headers[xml2::xml_attr(headers, "level") == max(levels)] | |
xml2::xml_set_attr(headers3, "level", "3") | |
headers2 <- headers[xml2::xml_attr(headers, "level") == (min(levels)+1)] | |
xml2::xml_set_attr(headers2, "level", "2") | |
} | |
#missing step converting back to Markdown | |
post_xml | |
} | |
roblog <- "C:\\Users\\Maelle\\Documents\\ropensci\\roweb2\\content\\blog" | |
file.path(roblog, "2013-03-08-ropensci-collaboration.md") %>% | |
homogeneize() %>% | |
xml2::xml_find_all(xpath = './/d1:heading', | |
xml2::xml_ns(.)) | |
#> {xml_nodeset (5)} | |
#> [1] <heading level="1">\n <text>Install some functions for interacting ... | |
#> [2] <heading level="1">\n <text>Get all repos for an organization, here ... | |
#> [3] <heading level="1">\n <text>Get commits broken down in to additions ... | |
#> [4] <heading level="1">\n <text>Plot commits by date and repo</text>\n< ... | |
#> [5] <heading level="1">\n <text>In addition, there are quite a few peop ... | |
file.path(roblog, "2017-09-27-rrricanes.md") %>% | |
homogeneize() %>% | |
xml2::xml_find_all(xpath = './/d1:heading', | |
xml2::xml_ns(.)) %>% | |
xml2::xml_attr("level") | |
#> [1] "1" "2" "2" "2" "2" "2" "2" "1" "2" "1" "1" "2" "2" "1" "1" "2" "2" | |
#> [18] "2" "2" "2" "2" "3" "2" "2" "2" "2" "1" "1" "1" "1" | |
file.path(roblog, "2014-02-19-taxize-update.md") %>% | |
homogeneize() %>% | |
xml2::xml_find_all(xpath = './/d1:heading', | |
xml2::xml_ns(.)) | |
#> {xml_nodeset (5)} | |
#> [1] <heading level="1">\n <text>First, install and load taxize</text>\n ... | |
#> [2] <heading level="1">\n <text>New things</text>\n</heading> | |
#> [3] <heading level="2">\n <text>New functions: class2tree</text>\n</hea ... | |
#> [4] <heading level="2">\n <text>New functions: get_gbfid</text>\n</head ... | |
#> [5] <heading level="2">\n <text>New functions: rbind and cbind for clas ... | |
``` | |
Created on 2018-08-29 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment