Skip to content

Commit 65e36ec

Browse files
committed
* maintenance tasks
--- - avoid `dplyr::if_else()` in non-vectorized use - improve source Rmd structure - update min R version and use native pipe op in examples - update lintr config - update TODOs - other small changes and fixes
1 parent e91f082 commit 65e36ec

15 files changed

+212
-193
lines changed

.lintr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# deviations from lintr's defaults only! to list all linter names enabled by default, use: `names(lintr::default_linters)`
22

3-
linters: with_defaults(absolute_path_linter = absolute_path_linter,
4-
implicit_integer_linter = implicit_integer_linter,
3+
linters: with_defaults(absolute_path_linter = absolute_path_linter(),
4+
implicit_integer_linter = implicit_integer_linter(),
55
line_length_linter = line_length_linter(160),
6-
nonportable_path_linter = nonportable_path_linter,
6+
nonportable_path_linter = nonportable_path_linter(),
77
object_length_linter = lintr::object_length_linter(40),
8-
semicolon_terminator_linter = semicolon_terminator_linter,
9-
todo_comment_linter = todo_comment_linter,
8+
semicolon_terminator_linter = semicolon_terminator_linter(),
9+
todo_comment_linter = todo_comment_linter(),
1010
trailing_whitespace_linter = NULL,
11-
T_and_F_symbol_linter = T_and_F_symbol_linter,
12-
undesirable_function_linter = undesirable_function_linter,
11+
T_and_F_symbol_linter = T_and_F_symbol_linter(),
12+
undesirable_function_linter = undesirable_function_linter(),
1313
undesirable_operator_linter = undesirable_operator_linter(c(lintr:::default_undesirable_operators, `->` = NA)),
14-
unneeded_concatenation_linter = unneeded_concatenation_linter)
14+
unneeded_concatenation_linter = unneeded_concatenation_linter())
1515
exclusions: c(list.files(path = "R",
1616
recursive = TRUE,
1717
full.names = TRUE,

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ License: AGPL (>= 3)
1919
URL: https://gitlab.com/salim_b/r/pkgs/tocr
2020
BugReports: https://gitlab.com/salim_b/r/pkgs/tocr/issues
2121
Depends:
22-
R (>= 3.4.4)
22+
R (>= 4.1)
2323
Imports:
2424
checkmate (>= 1.8.5),
2525
curl (>= 4.3),
@@ -41,4 +41,4 @@ Remotes:
4141
Encoding: UTF-8
4242
LazyData: true
4343
Roxygen: list(markdown = TRUE)
44-
RoxygenNote: 7.1.1
44+
RoxygenNote: 7.1.2

R/tocr-GEN.R R/tocr.gen.R

+61-50
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# 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/>.
314

415
utils::globalVariables(names = c("."))
516

@@ -23,9 +34,9 @@ get_non_code_block_indices <- function(md_lines) {
2334
# code block ends?
2435
if (stringr::str_detect(string = md_lines[[i]],
2536
pattern = paste0("^ {0,3}",
26-
dplyr::if_else(is_tilde, "~", "`"),
37+
ifelse(is_tilde, "~", "`"),
2738
"{", nr_of_fence_chars, ",} *$"))) {
28-
is_inside_code_block %<>% not()
39+
is_inside_code_block %<>% magrittr::not()
2940
}
3041

3142
} else {
@@ -38,7 +49,7 @@ get_non_code_block_indices <- function(md_lines) {
3849
pattern = "^ {0,3}~{3,}")
3950

4051
nr_of_fence_chars <- stringr::str_count(string = md_lines[i],
41-
pattern = dplyr::if_else(is_tilde, "~", "`"))
52+
pattern = ifelse(is_tilde, "~", "`"))
4253
} else {
4354

4455
non_code_block_indices %<>% c(i)
@@ -198,19 +209,19 @@ evaluate_position <- function(position,
198209

199210
} else if (position == "above") {
200211

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))
214225

215226
} else if (position == "below") {
216227

@@ -405,9 +416,9 @@ process_md <- function(md_lines,
405416
stringr::str_replace_all(pattern = paste0("\\[[^\\]]*?\\]\\(#(",
406417
toc_id, "|",
407418
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)),
411422
")-*?\\) ?"),
412423
replacement = "") %>%
413424
# remove all anchor links where the link text is no longer than 2 chars
@@ -462,9 +473,9 @@ process_md <- function(md_lines,
462473
magrittr::subtract(min_tier - 1L)
463474

464475
# 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)
468479

469480
# update TOC data
470481
toc_data %<>% dplyr::add_row(tier = tier,
@@ -564,15 +575,16 @@ process_md <- function(md_lines,
564575
#' @export
565576
#'
566577
#' @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) |>
571583
#' cat(sep = "\n")
572584
#'
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")
576588
add_toc <- function(md,
577589
min_tier = 2L,
578590
max_tier = 6L,
@@ -582,7 +594,7 @@ add_toc <- function(md,
582594
title = "Table of contents",
583595
title_tier = min_tier,
584596
add_backlinks = add_title,
585-
backlink_strings = c("\U1F805", "\U1F807"),
597+
backlink_strings = c("\u2191", "\u2193"),
586598
backlink_position = c("before", "after"),
587599
listing_style = c("-", "*", "ordered", "indented"),
588600
toc_id = "toc",
@@ -743,15 +755,14 @@ add_toc <- function(md,
743755
filename = basename(md))
744756

745757
# 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)
755766

756767
# determine if TOC title is a header or not
757768
is_header_title <- is.numeric(title_tier)
@@ -768,18 +779,19 @@ add_toc <- function(md,
768779
)
769780

770781
# 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))
774785

775786
# generate actual TOC ID if `position != "none"` (ensure it's unique)
776787
if (new_toc_id_position > 0L) {
777788

778789
## get all header texts preceding `new_toc_id_position` (to avoid duplicates)
779790
preceding_anchor_links <-
780791
# 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)],
783795
pattern = "^ {0,3}#{1,6}\\s+\\S") %>%
784796
# remove leading hashtags
785797
stringr::str_replace(pattern = "^ *#+",
@@ -1011,11 +1023,10 @@ add_toc <- function(md,
10111023
#' @export
10121024
#'
10131025
#' @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")}
10191030
remove_toc <- function(md,
10201031
old_toc_id = "toc") {
10211032
add_toc(md = md,

0 commit comments

Comments
 (0)