Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/olivroy/reuseme
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed May 9, 2024
2 parents e6f8842 + 9d99b0a commit 9f6bcef
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 125 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export(file_outline)
export(filter_identity)
export(filter_if_any)
export(filter_if_any_identity)
export(link_issue)
export(link_gh_issue)
export(markup_href)
export(max_named)
export(min_named)
export(mutate_identity)
Expand Down
23 changes: 22 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
# reuseme (development version)

* `proj_outline()` and `dir_outline()` now excludes example files

* `file_outline()` better support for TODO in md files

* `file_outline()` should recognize and transform markdown links automatically with new `markup_href()` It is no longer needed to use `{.href}` in your outline headings to show a link.

* `link_issue()` has been renamed `link_gh_issue()` and now only takes care of changing gh issues in markdown links.
New `markup_href()` is more general and now in charge of creating cli links for all markdown URLs.

```r
str <- "rstudio/gt#120 and [md link](https://github.com)"
# before

link_issue(str)
#> "{.href [rstudio/gt#120](https://github.com/rstudio/gt/issues/120)} and [md link](https://github.com)"

# now
link_gh_issue(str) |> markup_href()
#> "{.href [rstudio/gt#120](https://github.com/rstudio/gt/issues/120)} and {.href [md link](https://github.com)}"
```

* `rename_files2()` should work better with Quarto books, avoiding to look in `_book` and `_execute`, and classifying `summary.qmd` as generic file name, hence not looking for this regexp when searching for conflicts.

* `file_outline()` now get function calls, but doesn't print them by default.

* `proj_outline()` truncates todo items to fit on a single line.
* `file_outline()` truncates todo items to fit on a single line.

* can now personalize recent file indicator with `options(reuseme.recent_indicator)`, by default, a clock.

Expand Down
80 changes: 57 additions & 23 deletions R/link-elements.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,74 @@
#' Create a cli link to a GitHub issue
#' Create a markdown link to a GitHub issue
#'
#' In RStudio, links to issues are automatically recognized.
#' This function creates cli ansi links to entries of the form rstudio/rstudio#1100
#' This function creates intermediate markdown links to entries of the form rstudio/rstudio#1100
#'
#' Note: doesn't support without <OWNER>/<REPO>
#' Note: doesn't (yet) support without <OWNER>/<REPO>
#'
#' Basically trransform repo/org#xx -> [repo/org#xx](https://github.com/repo/org/issues/xx).
#'
#' Afterwards, we use [markup_href()] to create a cli link
#' @param x A string, usually lines of files that contains issue numbers.
#'
#' @return A formatted linked issue to GitHub issue
#' @return A markdown link linked issue to GitHub issue
#' @export
#' @keywords internal
#' @family inline markup internal helpers
#' @examples
#' link_issue(c("We really need rstudio/gt#1469 to be fixed.")) |> cli::cli_bullets()
link_issue <- function(x) {
#' link_gh_issue(c("We really need rstudio/gt#1469 to be fixed."))
link_gh_issue <- function(x) {
# Return early if no issue pattern is detected.
if (!any(stringr::str_detect(x, "[:graph:]+/[^#\\s]+#\\d"))) {
regex_gh_issue <- "([:graph:]+/[^#\\s]+)#(\\d+)"

has_gh_issue <- stringr::str_detect(
x,
regex_gh_issue
)
if (!any(has_gh_issue)) {
return(x)
}
# what we need to do is to t

li <- stringr::str_split(x, pattern = "\\s")
x_to_change <- x[has_gh_issue]
# x_changed <-
x_changed <- x_to_change |> stringr::str_replace_all(
regex_gh_issue,
paste0("[\\1#\\2](https://github.com/\\1/issues/\\2)")
)

issue_regex <- "([:graph:]+/[^#\\s]+)#(\\d+)"
x[has_gh_issue] <- x_changed
x
}

res <- purrr::map(li, function(x) {
purrr::map_chr(x, function(y) {
if (stringr::str_detect(y, issue_regex)) {
rest <- stringr::str_extract(y, "([:graph:]+/[^#\\s]+)#(\\d+)(.*)", 3)
if (!is.na(rest)) {
y <- stringr::str_remove(y, paste0(rest, "$"))
}
rep <- stringr::str_replace_all(y, "([:graph:]+/[^#\\s]+)#(\\d+)", "https://github.com/\\1/issues/\\2")
#' Create a cli href with a markdown link
#'
#' Transforms [text](url) -> {.href [text](url)}
#' @family inline markup internal helpers
#' @param x A character vector
#' @returns A character vector with substrings changed
#' @keywords internal
#' @export
#' @examples
#' markup_href(c("[link](https://google.com)", "{.href [link](https://google.com)}"))
#'
markup_href <- function(x) {
# already excluding markuped strings
# only safe links for now
regex_md_url <- "(?<!\\{\\.href\\s)(\\[.+\\])(\\(https.+\\))(?!\\})"

has_md_url <- stringr::str_detect(
x,
regex_md_url
)
if (!any(has_md_url)) {
return(x)
}
x_to_change <- x[has_md_url]
x_changed <- x_to_change |> stringr::str_replace_all(
regex_md_url,
paste0("{.href \\1\\2}")
)

y <- paste0("{.href [", y, "](", rep, ")}", rest)
}
y
})
})
purrr::map_chr(res, \(x) paste(x, collapse = " "))
x[has_md_url] <- x_changed
x
}
16 changes: 14 additions & 2 deletions R/outline.R
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,22 @@ dir_outline <- function(regex_outline = NULL, path = ".", work_only = TRUE, dir_
glob = file_exts_regex,
recurse = recurse
)
# TODO exclude example files (see pkgdown, usethis)
file_list_to_outline <- fs::path_filter(
file_list_to_outline,
regexp = "vignette-dump|renv/",
invert = TRUE
)

if (!identical(Sys.getenv("TESTTHAT"), "true")) {
# Remove examples from outline and test example files to avoid clutter
# examples don't help understand a project.
file_list_to_outline <- fs::path_filter(
file_list_to_outline,
regexp = "testthat/_ref/|example-file",
invert = TRUE
)
}

if (any(grepl("README.Rmd", file_list_to_outline))) {
file_list_to_outline <- stringr::str_subset(file_list_to_outline, "README.md", negate = TRUE)
}
Expand Down Expand Up @@ -423,7 +433,8 @@ keep_outline_element <- function(.data) {
# Removing quotes, etc.
display_outline_element <- function(.data) {
x <- .data
x$outline_el <- purrr::map_chr(x$content, link_issue) # to add link to GitHub.
x$outline_el <- purrr::map_chr(x$content, link_gh_issue) # to add link to GitHub.
x$outline_el <- purrr::map_chr(x$outline_el, markup_href)
x <- dplyr::mutate(
x,
outline_el = dplyr::case_when(
Expand All @@ -442,6 +453,7 @@ display_outline_element <- function(.data) {
),
outline_el = dplyr::case_when(
is_tab_or_plot_title ~ stringr::str_remove_all(outline_el, "(gt\\:\\:)?tab_header\\(|\\s*(sub)?title\\s\\=\\s['\"]|['\"],?$"),
is_md & is_todo_fixme ~ stringr::str_remove(outline_el, "\\s?-{2,}\\>.*"),
.default = outline_el
),
outline_el = stringr::str_remove(outline_el, "[-\\=]{3,}") |> stringr::str_trim(), # remove trailing bars
Expand Down
1 change: 1 addition & 0 deletions R/quarto-help.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ href_name_url <- function(x) {
links <- stringr::str_glue("{{.href [{names(x)}]({x})}}")
cli::ansi_collapse(links)
}

4 changes: 2 additions & 2 deletions R/rename-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ rename_files2 <- function(old,
action <- rlang::arg_match(action)

warn_conflicts <- rlang::arg_match(warn_conflicts)
check_logical(overwrite)
check_bool(overwrite)

if (lifecycle::is_present(force)) {
lifecycle::deprecate_warn(
Expand Down Expand Up @@ -349,7 +349,7 @@ hint_acceptable_renaming <- function(old, new, overwrite) {
if (anyNA(info$change_time)) {
cli::cli_inform("One of the file doesn't exist. hint_acceptable_renaming() expects 2 existing files.", .internal = FALSE)
}
# TODO Check that old
# TODO Check that old file is more recent
if (info$change_time[1] > info$change_time[2]) {
cli::cli_inform(c(
"!" = "{.val {new}} was modified later than {.val {old}}",
Expand Down
7 changes: 7 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ With reuseme, just use the project name!

# Outline speed

```{r}
#| echo: false
#| output: false
# For example purpose, show all to spot potential files.
withr::local_envvar("TESTTHAT" = "true")
```

Due to the growing number of criteria, regex, `file_outline()` is slowing down a bit. I will address that.

```{r}
Expand Down
Loading

0 comments on commit 9f6bcef

Please sign in to comment.