Předpokládejme, že máme markdown formátovaný text soubor ìn.md
a chceme, aby se vytvořit další soubor markdown out.md
obsahující pouze kurzívu a tučné části.
Obsah souboru.md:
# Header
There is *italic* and **bold** text!
There is *another italic* and **another bold** text!
library(tidyverse)
text <- read_file("in.md")
bold_texts <- text %>%
str_extract_all("\\*\\*[^\\*]+\\*\\*") %>%
purrr::simplify() %>%
map_chr(~ .x %>% str_remove_all("\\*"))
bold_texts
#> [1] "bold" "another bold"
italic_texts <-
text %>%
str_remove_all(bold_texts %>% map_chr(~ paste0("\\*\\*", .x, "\\*\\*")) %>% paste0(collapse = "|")) %>%
str_extract_all("\\*[^\\*]+\\*") %>%
purrr::simplify() %>%
map_chr(~ .x %>% str_remove_all("\\*"))
italic_texts
#> [1] "italic" "another italic"
out_text <- c("#Bold texts:", bold_texts, "#Italic texts:", italic_texts) %>% paste0(collapse = "\n")
cat(out_text)
#> #Bold texts:
#> bold
#> another bold
#> #Italic texts:
#> italic
#> another italic
write_file(out_text, "out.md")
Vytvořen na 2021-11-23 do reprex balíček (v2.0.1)