🔍 An RStudio addin slash regex utility belt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.7KB

  1. #' Regex Cheatsheet Quick Reference
  2. #'
  3. #' @import miniUI
  4. #' @import shiny
  5. #' @export
  6. regexplain_cheatsheet <- function() {
  7. stopifnot(requireNamespace("miniUI"), requireNamespace("shiny"))
  8. ui <- miniPage(
  9. shiny::includeCSS(system.file("gadget.css", package = "regexplain")),
  10. gadgetTitleBar(
  11. "Regex Cheatsheet Quick Reference",
  12. right = miniTitleBarButton("done", "OK", TRUE)
  13. ),
  14. help_ui("help")
  15. )
  16. server <- function(input, output, session) {
  17. help_text <- callModule(help_server, "help")
  18. observeEvent(input$done, {
  19. stopApp()
  20. })
  21. observeEvent(input$cancel, {
  22. stopApp()
  23. })
  24. }
  25. viewer <- shiny::paneViewer(700)
  26. runGadget(ui, server, viewer = viewer)
  27. }
  28. help_ui <- function(id) {
  29. ns <- NS(id)
  30. miniContentPanel(
  31. fillRow(
  32. flex = c(1, 4),
  33. tagList(
  34. tags$ul(
  35. id = "help-sidebar",
  36. tags$li("Character Classes", class = "header"),
  37. tags$ul(
  38. class = "subgroup",
  39. tags$li(actionLink(ns("help_cat_character_classes_regular"), "Regular")),
  40. tags$li(actionLink(ns("help_cat_character_classes_prebuilt"), "Pre-Built"))
  41. ),
  42. tags$li(actionLink(ns("help_cat_anchors"), "Anchors")),
  43. tags$li("Escaped Characters", class = "header"),
  44. tags$ul(
  45. class = "subgroup",
  46. tags$li(actionLink(ns("help_cat_escaped_general"), "General")),
  47. tags$li(actionLink(ns("help_cat_escaped_hex"), "Hex")),
  48. tags$li(actionLink(ns("help_cat_escaped_control"), "Control Characters"))
  49. ),
  50. tags$li(actionLink(ns("help_cat_groups"), "Groups")),
  51. tags$li(actionLink(ns("help_cat_quantifiers"), "Quantifiers"))
  52. )
  53. ),
  54. tags$div(
  55. style = "width: 100%; padding-left: 10px;",
  56. uiOutput(ns('help_text_selected'))
  57. )
  58. )
  59. )
  60. }
  61. #' @importFrom rlang .data
  62. help_server <- function(input, output, session) {
  63. help_text <- reactiveVal("<p>Select a category from the left sidebar.</p>")
  64. make_html_table <- function(x) {
  65. select(x, .data$regexp, .data$text) %>%
  66. knitr::kable(
  67. col.names = c("Regexp", "Text"),
  68. escape = FALSE,
  69. format = "html")
  70. }
  71. output$help_text_selected <- renderUI({
  72. HTML(help_text())
  73. })
  74. observeEvent(input$help_cat_character_classes_regular, {
  75. cheatsheet %>%
  76. filter(.data$category == "character classes", .data$group == "regular") %>%
  77. make_html_table() %>%
  78. help_text()
  79. })
  80. observeEvent(input$help_cat_character_classes_prebuilt, {
  81. cheatsheet %>%
  82. filter(.data$category == "character classes", .data$group == "pre-built") %>%
  83. make_html_table() %>%
  84. help_text()
  85. })
  86. observeEvent(input$help_cat_anchors, {
  87. cheatsheet %>%
  88. filter(.data$category == "anchors") %>%
  89. make_html_table() %>%
  90. help_text()
  91. })
  92. observeEvent(input$help_cat_escaped_general, {
  93. cheatsheet %>%
  94. filter(.data$category == "escaped characters", .data$group == "general") %>%
  95. make_html_table() %>%
  96. help_text()
  97. })
  98. observeEvent(input$help_cat_escaped_hex, {
  99. cheatsheet %>%
  100. filter(.data$category == "escaped characters", .data$group == "hex") %>%
  101. make_html_table() %>%
  102. help_text()
  103. })
  104. observeEvent(input$help_cat_escaped_control, {
  105. cheatsheet %>%
  106. filter(.data$category == "escaped characters", .data$group == "control characters") %>%
  107. make_html_table() %>%
  108. help_text()
  109. })
  110. observeEvent(input$help_cat_groups, {
  111. cheatsheet %>%
  112. filter(.data$category == "groups") %>%
  113. make_html_table() %>%
  114. help_text()
  115. })
  116. observeEvent(input$help_cat_quantifiers, {
  117. cheatsheet %>%
  118. filter(.data$category == "quantifiers") %>%
  119. make_html_table() %>%
  120. help_text()
  121. })
  122. }