🔍 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.

141 lines
3.8KB

  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 - Shiny Module ----
  29. help_ui <- function(id) {
  30. ns <- NS(id)
  31. miniContentPanel(
  32. fillRow(
  33. flex = c(1, 4),
  34. tagList(
  35. tags$ul(
  36. id = "help-sidebar",
  37. tags$li("Character Classes", class = "header"),
  38. tags$ul(
  39. class = "subgroup",
  40. tags$li(actionLink(ns("help_cat_character_classes_regular"), "Regular")),
  41. tags$li(actionLink(ns("help_cat_character_classes_prebuilt"), "Pre-Built"))
  42. ),
  43. tags$li(actionLink(ns("help_cat_anchors"), "Anchors")),
  44. tags$li("Escaped Characters", class = "header"),
  45. tags$ul(
  46. class = "subgroup",
  47. tags$li(actionLink(ns("help_cat_escaped_general"), "General")),
  48. tags$li(actionLink(ns("help_cat_escaped_hex"), "Hex")),
  49. tags$li(actionLink(ns("help_cat_escaped_control"), "Control Characters"))
  50. ),
  51. tags$li(actionLink(ns("help_cat_groups"), "Groups")),
  52. tags$li(actionLink(ns("help_cat_quantifiers"), "Quantifiers"))
  53. )
  54. ),
  55. tags$div(
  56. style = "width: 100%; padding-left: 10px;",
  57. uiOutput(ns('help_text_selected'))
  58. )
  59. )
  60. )
  61. }
  62. #' @importFrom rlang .data
  63. help_server <- function(input, output, session) {
  64. help_text <- reactiveVal("<p>Select a category from the left sidebar.</p>")
  65. make_html_table <- function(x) {
  66. select(x, .data$regexp, .data$text) %>%
  67. knitr::kable(
  68. col.names = c("Regexp", "Text"),
  69. escape = FALSE,
  70. format = "html")
  71. }
  72. output$help_text_selected <- renderUI({
  73. HTML(help_text())
  74. })
  75. observeEvent(input$help_cat_character_classes_regular, {
  76. cheatsheet %>%
  77. filter(.data$category == "character classes", .data$group == "regular") %>%
  78. make_html_table() %>%
  79. help_text()
  80. })
  81. observeEvent(input$help_cat_character_classes_prebuilt, {
  82. cheatsheet %>%
  83. filter(.data$category == "character classes", .data$group == "pre-built") %>%
  84. make_html_table() %>%
  85. help_text()
  86. })
  87. observeEvent(input$help_cat_anchors, {
  88. cheatsheet %>%
  89. filter(.data$category == "anchors") %>%
  90. make_html_table() %>%
  91. help_text()
  92. })
  93. observeEvent(input$help_cat_escaped_general, {
  94. cheatsheet %>%
  95. filter(.data$category == "escaped characters", .data$group == "general") %>%
  96. make_html_table() %>%
  97. help_text()
  98. })
  99. observeEvent(input$help_cat_escaped_hex, {
  100. cheatsheet %>%
  101. filter(.data$category == "escaped characters", .data$group == "hex") %>%
  102. make_html_table() %>%
  103. help_text()
  104. })
  105. observeEvent(input$help_cat_escaped_control, {
  106. cheatsheet %>%
  107. filter(.data$category == "escaped characters", .data$group == "control characters") %>%
  108. make_html_table() %>%
  109. help_text()
  110. })
  111. observeEvent(input$help_cat_groups, {
  112. cheatsheet %>%
  113. filter(.data$category == "groups") %>%
  114. make_html_table() %>%
  115. help_text()
  116. })
  117. observeEvent(input$help_cat_quantifiers, {
  118. cheatsheet %>%
  119. filter(.data$category == "quantifiers") %>%
  120. make_html_table() %>%
  121. help_text()
  122. })
  123. }