[education/rkward/snippets] rkward: YAML snippet started, Tables for RMardown added

Stefan Rödiger null at kde.org
Sun Jun 19 00:13:57 BST 2022


Git commit abfc1136a75c5ac5da6cc663f186231babfa968f by Stefan Rödiger.
Committed on 18/06/2022 at 23:12.
Pushed by srodiger into branch 'snippets'.

YAML snippet started, Tables for RMardown added

M  +1    -0    rkward/CMakeLists.txt
M  +31   -1    rkward/RKWardRMd.xml
A  +47   -0    rkward/RKWardYAML.xml

https://invent.kde.org/education/rkward/commit/abfc1136a75c5ac5da6cc663f186231babfa968f

diff --git a/rkward/CMakeLists.txt b/rkward/CMakeLists.txt
index 884c49e6..a5f949c9 100644
--- a/rkward/CMakeLists.txt
+++ b/rkward/CMakeLists.txt
@@ -126,3 +126,4 @@ ENDIF(SharedMimeInfo_FOUND)
 
 INSTALL(FILES RKWardRMd.xml DESTINATION ${DATA_INSTALL_DIR}/ktexteditor_snippets/data)
 INSTALL(FILES RKWardLaTeX.xml DESTINATION ${DATA_INSTALL_DIR}/ktexteditor_snippets/data)
+INSTALL(FILES RKWardYAML.xml DESTINATION ${DATA_INSTALL_DIR}/ktexteditor_snippets/data)
diff --git a/rkward/RKWardRMd.xml b/rkward/RKWardRMd.xml
index 3ae2112c..4e50221e 100644
--- a/rkward/RKWardRMd.xml
+++ b/rkward/RKWardRMd.xml
@@ -108,7 +108,7 @@ $$e^${braces("i\\pi")} + 1$$
   <match>Code (simple chunk)</match>
   <fillin>${rangeCommand("\n```"+ braces("r") +" %%1```\n", "\nx <- 1\n\n")}</fillin>
  </item>
-  <item>
+ <item>
   <match>Quote</match>
   <fillin>${rangeCommand("> %%1", "Text")}</fillin>
  </item>
@@ -116,4 +116,34 @@ $$e^${braces("i\\pi")} + 1$$
   <match>Code (chunk with defaults)</match>
   <fillin>${rangeCommand("\n```"+ braces("r, echo = FALSE, eval = FALSE, fig.show = \"hide\", include = FALSE, message = FALSE, results = \"hide\", warning = FALSE") +" %%1```\n", "\nx <- 1\n\n")}</fillin>
  </item>
+ <item>
+  <match>Table</match>
+  <fillin>
+| Right  | Left  | Default   | Center    |
+|-------:|:------|-----------|:---------:|
+| 12     | 12    | 12        | 12        |
+| 123    | 123   | 123       | 123       |
+| 1      | 1     | 1         | 1         |
+  </fillin>
+ </item>
+ <item>
+  <match>Table with caption</match>
+  <fillin>
+| Right  | Left  | Default   | Center    |
+|-------:|:------|-----------|:---------:|
+| 12     | 12    | 12        | 12        |
+| 123    | 123   | 123       | 123       |
+| 1      | 1     | 1         | 1         |
+:Caption text
+  </fillin>
+ </item>
+ <item>
+  <match>Table (kable)</match>
+  <fillin>
+```{r}
+data <- iris[1:4, ]
+knitr::kable(data, caption = "Table caption text.")
+```
+  </fillin>
+ </item>
 </snippets>
diff --git a/rkward/RKWardYAML.xml b/rkward/RKWardYAML.xml
new file mode 100644
index 00000000..c04339fa
--- /dev/null
+++ b/rkward/RKWardYAML.xml
@@ -0,0 +1,47 @@
+<snippets name="YAML R Markdown (RKWard)" filetypes="R Markdown" authors="RKWard Team" namespace="SnippetsYAML" license="BSD">
+  <script>require("range.js")
+require("cursor.js")
+require("document.js")
+require("view.js")
+
+function fileName() { return document.fileName(); }
+function encoding() { return document.encoding(); }
+function year() { return new Date().getFullYear(); }
+function upper(x) { return x.toUpperCase(); }
+function lower(x) { return x.toLowerCase(); }
+function braces(text) { return "{" + text + "}"; }
+function lineCommand(command, replaces="", label_if_empty="Heading") {
+    let c = view.cursorPosition();
+    // NOTE: snippet scripts work by 1. inserting the raw snippet as a raw "template" 2. evaluating any commands inside the template 3. replacing the template with the evaluated template. As a result, when this function gets called, it contains the call to itself...
+    let line = document.line(c.line).replace(/\$\{lineCommand\s*\([^\}]*\}/g, "");
+    line = line.replace(new RegExp(replaces), "");
+    if (line == "") line = label_if_empty;
+    else document.removeLine(c.line);
+    return command + " " + line + "\n";
+}
+
+function rangeCommand(command, def) {
+    if (view.selectedText().length > 0) {
+        return command.replace("%%1", view.selectedText());
+    } else {
+        return command.replace("%%1", def);
+    }
+}
+</script>
+ <item>
+  <match>YAML header</match>
+    <fillin>
+---
+title: "YAML header for Rmarkdown"
+author: "John Doe"
+date: "`r Sys.Date()`"
+output:
+    html_document:
+      fig_caption: yes
+      highlight: kate
+      number_sections: yes
+      toc: yes
+---
+    </fillin>
+ </item>
+</snippets>


More information about the rkward-tracker mailing list