-
Notifications
You must be signed in to change notification settings - Fork 3
/
roweb2_headers.R
57 lines (41 loc) · 1.57 KB
/
roweb2_headers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
roblog <- "C:/Users/Maelle/Documents/ropensci/roweb2/content/blog"
all_posts <- fs::dir_ls(roblog, regexp = "*.md")
all_posts <- all_posts[all_posts != "_index.md"]
library("magrittr")
homogeneize <- function(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", "2")
}
if(no_levels == 2){
headers1 <- headers[xml2::xml_attr(headers, "level") == min(levels)]
xml2::xml_set_attr(headers1, "level", "2")
headers2 <- headers[xml2::xml_attr(headers, "level") == max(levels)]
xml2::xml_set_attr(headers2, "level", "3")
}
if(no_levels == 3){
headers1 <- headers[xml2::xml_attr(headers, "level") == min(levels)]
xml2::xml_set_attr(headers1, "level", "2")
headers3 <- headers[xml2::xml_attr(headers, "level") == max(levels)]
xml2::xml_set_attr(headers3, "level", "4")
headers2 <- headers[xml2::xml_attr(headers, "level") == (min(levels)+1)]
xml2::xml_set_attr(headers2, "level", "3")
}
#missing step converting back to Markdown
post_xml
}
correct_post <- function(post_path){
message(post_path)
yaml_xml_list <- tinkr::to_xml(post_path)
yaml_xml_list$body <- homogeneize(yaml_xml_list$body)
tinkr::to_md(yaml_xml_list, path = post_path)
}
correct_post(all_posts[grepl("2013-11-21-rgbif-changes",
all_posts)])
purrr::walk(all_posts, correct_post)