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

138 lines
3.6KB

  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. help_server <- function(input, output, session) {
  62. help_text <- reactiveVal("<p>Select a category from the left sidebar.</p>")
  63. make_html_table <- function(x) {
  64. select(x, .data$regexp, .data$text) %>%
  65. knitr::kable(
  66. col.names = c("Regexp", "Text"),
  67. escape = FALSE,
  68. format = "html")
  69. }
  70. output$help_text_selected <- renderUI({
  71. HTML(help_text())
  72. })
  73. observeEvent(input$help_cat_character_classes_regular, {
  74. cheatsheet %>%
  75. filter(category == "character classes", group == "regular") %>%
  76. make_html_table %>%
  77. help_text
  78. })
  79. observeEvent(input$help_cat_character_classes_prebuilt, {
  80. cheatsheet %>%
  81. filter(category == "character classes", group == "pre-built") %>%
  82. make_html_table %>%
  83. help_text
  84. })
  85. observeEvent(input$help_cat_anchors, {
  86. cheatsheet %>%
  87. filter(category == "anchors") %>%
  88. make_html_table %>%
  89. help_text
  90. })
  91. observeEvent(input$help_cat_escaped_general, {
  92. cheatsheet %>%
  93. filter(category == "escaped characters", group == "general") %>%
  94. make_html_table %>%
  95. help_text
  96. })
  97. observeEvent(input$help_cat_escaped_hex, {
  98. cheatsheet %>%
  99. filter(category == "escaped characters", group == "hex") %>%
  100. make_html_table %>%
  101. help_text
  102. })
  103. observeEvent(input$help_cat_escaped_control, {
  104. cheatsheet %>%
  105. filter(category == "escaped characters", group == "control characters") %>%
  106. make_html_table %>%
  107. help_text
  108. })
  109. observeEvent(input$help_cat_groups, {
  110. cheatsheet %>%
  111. filter(category == "groups") %>%
  112. make_html_table %>%
  113. help_text
  114. })
  115. observeEvent(input$help_cat_quantifiers, {
  116. cheatsheet %>%
  117. filter(category == "quantifiers") %>%
  118. make_html_table %>%
  119. help_text
  120. })
  121. }