1
1
# DO NOT EDIT THIS FILE BY HAND! Instead edit the R Markdown source file `Rmd/tocr.Rmd` and run `pkgpurl::purl_rmd()`.
2
- # See `README.md#r-markdown-format` for more information on the literature programming approach used applying the R Markdown format.
2
+ # See `README.md#r-markdown-format` for more information on the literate programming approach used applying the R Markdown format.
3
+
4
+ # tocr: TOC Generation for (R) Markdown Documents
5
+ # Copyright (C) 2022 Salim Brüggemann
6
+ #
7
+ # This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free
8
+ # Software Foundation, either version 3 of the License, or any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11
+ # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
3
14
4
15
utils :: globalVariables(names = c(" ." ))
5
16
@@ -23,9 +34,9 @@ get_non_code_block_indices <- function(md_lines) {
23
34
# code block ends?
24
35
if (stringr :: str_detect(string = md_lines [[i ]],
25
36
pattern = paste0(" ^ {0,3}" ,
26
- dplyr :: if_else (is_tilde , " ~" , " `" ),
37
+ ifelse (is_tilde , " ~" , " `" ),
27
38
" {" , nr_of_fence_chars , " ,} *$" ))) {
28
- is_inside_code_block %<> % not()
39
+ is_inside_code_block %<> % magrittr :: not()
29
40
}
30
41
31
42
} else {
@@ -38,7 +49,7 @@ get_non_code_block_indices <- function(md_lines) {
38
49
pattern = " ^ {0,3}~{3,}" )
39
50
40
51
nr_of_fence_chars <- stringr :: str_count(string = md_lines [i ],
41
- pattern = dplyr :: if_else (is_tilde , " ~" , " `" ))
52
+ pattern = ifelse (is_tilde , " ~" , " `" ))
42
53
} else {
43
54
44
55
non_code_block_indices %<> % c(i )
@@ -198,19 +209,19 @@ evaluate_position <- function(position,
198
209
199
210
} else if (position == " above" ) {
200
211
201
- position <- dplyr :: if_else (min_tier == 1L ,
202
- 1L ,
203
- stringr :: str_which(string = md_lines ,
204
- pattern = paste0(" ^ {0,3}#{" , min_tier , " ,6}\\ s+\\ S" )) %> %
205
- dplyr :: first() %> %
206
- ifelse(is.na(. ), 2L , . ) %> %
207
- magrittr :: subtract(1L ) %> %
208
- seq(from = 1L ) %> %
209
- magrittr :: extract(md_lines , . ) %> %
210
- stringr :: str_which(pattern = paste0(" ^ {0,3}#{1," , min_tier , " }\\ s+\\ S" )) %> %
211
- dplyr :: last() %> %
212
- ifelse(is.na(. ), 0L , . ) %> %
213
- magrittr :: add(1L ))
212
+ position <- ifelse (min_tier == 1L ,
213
+ 1L ,
214
+ stringr :: str_which(string = md_lines ,
215
+ pattern = paste0(" ^ {0,3}#{" , min_tier , " ,6}\\ s+\\ S" )) %> %
216
+ dplyr :: first() %> %
217
+ ifelse(is.na(. ), 2L , . ) %> %
218
+ magrittr :: subtract(1L ) %> %
219
+ seq(from = 1L ) %> %
220
+ magrittr :: extract(md_lines , . ) %> %
221
+ stringr :: str_which(pattern = paste0(" ^ {0,3}#{1," , min_tier , " }\\ s+\\ S" )) %> %
222
+ dplyr :: last() %> %
223
+ ifelse(is.na(. ), 0L , . ) %> %
224
+ magrittr :: add(1L ))
214
225
215
226
} else if (position == " below" ) {
216
227
@@ -405,9 +416,9 @@ process_md <- function(md_lines,
405
416
stringr :: str_replace_all(pattern = paste0(" \\ [[^\\ ]]*?\\ ]\\ (#(" ,
406
417
toc_id , " |" ,
407
418
old_toc_id ,
408
- dplyr :: if_else (is_header_title ,
409
- " " ,
410
- paste0(" |" , new_toc_id )),
419
+ ifelse (is_header_title ,
420
+ " " ,
421
+ paste0(" |" , new_toc_id )),
411
422
" )-*?\\ ) ?" ),
412
423
replacement = " " ) %> %
413
424
# remove all anchor links where the link text is no longer than 2 chars
@@ -462,9 +473,9 @@ process_md <- function(md_lines,
462
473
magrittr :: subtract(min_tier - 1L )
463
474
464
475
# update enumeration for ordered list TOC
465
- enumeration <- dplyr :: if_else (tier == previous_tier ,
466
- enumeration + 1L ,
467
- 1L )
476
+ enumeration <- ifelse (tier == previous_tier ,
477
+ enumeration + 1L ,
478
+ 1L )
468
479
469
480
# update TOC data
470
481
toc_data %<> % dplyr :: add_row(tier = tier ,
@@ -564,15 +575,16 @@ process_md <- function(md_lines,
564
575
# ' @export
565
576
# '
566
577
# ' @examples
567
- # ' library(magrittr)
568
- # '
569
- # ' "https://raw.githubusercontent.com/ropensci/pdftools/e7248d9956c7e73968628fa3a8ed37f0a8c23b37/README.md" %>%
570
- # ' add_toc(position = 9) %>%
578
+ # ' md <- paste0("https://raw.githubusercontent.com/ropensci/pdftools/",
579
+ # ' "e7248d9956c7e73968628fa3a8ed37f0a8c23b37/README.md")
580
+ # '
581
+ # ' md |>
582
+ # ' tocr::add_toc(position = 9) |>
571
583
# ' cat(sep = "\n")
572
584
# '
573
- # ' add_toc( md = "https://raw.githubusercontent.com/ropensci/pdftools/e7248d9956c7e73968628fa3a8ed37f0a8c23b37/README.md",
574
- # ' listing_style = "ordered") %>%
575
- # ' readr::write_lines(path = "rmarkdown_README_incl_TOC.md ")
585
+ # ' md |>
586
+ # ' tocr::add_toc( listing_style = "ordered") |>
587
+ # ' cat(sep = "\n ")
576
588
add_toc <- function (md ,
577
589
min_tier = 2L ,
578
590
max_tier = 6L ,
@@ -582,7 +594,7 @@ add_toc <- function(md,
582
594
title = " Table of contents" ,
583
595
title_tier = min_tier ,
584
596
add_backlinks = add_title ,
585
- backlink_strings = c(" \U 1F805 " , " \U 1F807 " ),
597
+ backlink_strings = c(" \u 2191 " , " \u 2193 " ),
586
598
backlink_position = c(" before" , " after" ),
587
599
listing_style = c(" -" , " *" , " ordered" , " indented" ),
588
600
toc_id = " toc" ,
@@ -743,15 +755,14 @@ add_toc <- function(md,
743
755
filename = basename(md ))
744
756
745
757
# determine fallback HTML <id> attribute position for backlinks (ID of next header line above the TOC if existing)
746
- fallback_id_position <-
747
- dplyr :: if_else(position > 1L ,
748
- stringr :: str_which(string = md_lines [1L : (min(length(md_lines ), max(position - 1L , 1L )))],
749
- pattern = " ^ {0,3}#{1,6}\\ s+\\ S" ) %> %
750
- dplyr :: last() %> %
751
- # if no header lines above TOC found, set to -1
752
- dplyr :: if_else(is.na(. ), - 1L , . ),
753
- # if `position` is 1 (or "none") there can't be any header lines above TOC
754
- 1L )
758
+ fallback_id_position <- ifelse(position > 1L ,
759
+ stringr :: str_which(string = md_lines [1L : (min(length(md_lines ), max(position - 1L , 1L )))],
760
+ pattern = " ^ {0,3}#{1,6}\\ s+\\ S" ) %> %
761
+ dplyr :: last() %> %
762
+ # if no header lines above TOC found, set to -1
763
+ ifelse(is.na(. ), - 1L , . ),
764
+ # if `position` is 1 (or "none") there can't be any header lines above TOC
765
+ 1L )
755
766
756
767
# determine if TOC title is a header or not
757
768
is_header_title <- is.numeric(title_tier )
@@ -768,18 +779,19 @@ add_toc <- function(md,
768
779
)
769
780
770
781
# set position of new TOC ID
771
- new_toc_id_position <- dplyr :: if_else (use_fallback ,
772
- fallback_id_position ,
773
- as.integer(position ))
782
+ new_toc_id_position <- ifelse (use_fallback ,
783
+ fallback_id_position ,
784
+ as.integer(position ))
774
785
775
786
# generate actual TOC ID if `position != "none"` (ensure it's unique)
776
787
if (new_toc_id_position > 0L ) {
777
788
778
789
# # get all header texts preceding `new_toc_id_position` (to avoid duplicates)
779
790
preceding_anchor_links <-
780
791
# extract all header lines until `new_toc_id_position`
781
- stringr :: str_subset(string = md_lines [1L : dplyr :: case_when(use_fallback ~ new_toc_id_position ,
782
- TRUE ~ as.integer(new_toc_id_position - 1L ))],
792
+ stringr :: str_subset(string = md_lines [1 : ifelse(use_fallback ,
793
+ new_toc_id_position ,
794
+ new_toc_id_position - 1L )],
783
795
pattern = " ^ {0,3}#{1,6}\\ s+\\ S" ) %> %
784
796
# remove leading hashtags
785
797
stringr :: str_replace(pattern = " ^ *#+" ,
@@ -1011,11 +1023,10 @@ add_toc <- function(md,
1011
1023
# ' @export
1012
1024
# '
1013
1025
# ' @examples
1014
- # ' library(magrittr)
1015
- # '
1016
- # ' remove_toc(md = paste0("https://raw.githubusercontent.com/thlorenz/doctoc/",
1017
- # ' "1d386261972d35c6bcd187d0a00e666f9d893d8d/README.md")) %>%
1018
- # ' cat(sep = "\n")
1026
+ # ' \dontrun{
1027
+ # ' tocr::remove_toc(md = paste0("https://raw.githubusercontent.com/thlorenz/doctoc/",
1028
+ # ' "1d386261972d35c6bcd187d0a00e666f9d893d8d/README.md")) |>
1029
+ # ' cat(sep = "\n")}
1019
1030
remove_toc <- function (md ,
1020
1031
old_toc_id = " toc" ) {
1021
1032
add_toc(md = md ,
0 commit comments