Verified Commit c1f150cd authored by m.eik michalke's avatar m.eik michalke
Browse files

added plugin to export/convert RMarkdown files

parent 7fca30a9
// this code was generated using the rkwarddev package.
// perhaps don't make changes here, but in the rkwarddev script instead!
//
// look for a file called: $SRC/inst/rkward/rkwarddev_rk.RMarkdown_plugin_script.R
// define variables globally
var markdownVersion;
var targetFormat;
function setGlobalVars(){
markdownVersion = getString("markdownVersion");
targetFormat = getString("targetFormat");
}
function preprocess(is_preview){
setGlobalVars();
// add requirements etc. here
echo("require(knitr)\n");
if(markdownVersion == 1) {
echo("require(markdown)\n\n");
} else {
echo("require(rmarkdown)\n");
if(targetFormat != "all" && targetFormat != "first" && targetFormat != "html_vignette") {
echo("rk.check.for.pandoc(\n stop_if_missing=TRUE,\n output_format=\"" + targetFormat + "\"\n)\n\n");
} else {}
}
}
function calculate(is_preview){
// read in variables from dialog
var markdownFile = getString("markdownFile");
var targetFile = getString("targetFile");
var targetDir = getString("targetDir");
// the R code to be evaluated
var fileExtension = targetFormat;
var renderFormat = "pandoc";
if(markdownVersion == 1) {
echo("md_tempfile <- tempfile()\n");
echo("knit2html(\n");
echo(" input=\"" + markdownFile + "\",\n output=md_tempfile,\n quiet=TRUE,\n");
echo(")\n");
fileExtension = 'html';
renderFormat = 'knit2html';
} else {
if(targetFormat == "all") {
renderFormat = 'all';
} else if(targetFormat == "first") {
renderFormat = 'NULL';
} else if(targetFormat == "beamer") {
fileExtension = 'pdf';
renderFormat = 'beamer_presentation';
} else if(targetFormat == "html") {
renderFormat = 'html_document';
} else if(targetFormat == "html5") {
fileExtension = 'html';
} else if(targetFormat == "markdown") {
fileExtension = 'md';
renderFormat = 'md_document';
} else if(targetFormat == "gfm" || targetFormat == "markdown_github") {
fileExtension = 'md';
renderFormat = 'github_document';
} else if(targetFormat == "odt") {
renderFormat = 'odt_document';
} else if(targetFormat == "latex") {
fileExtension = 'pdf';
renderFormat = 'pdf_document';
} else if(targetFormat == "pptx") {
renderFormat = 'powerpoint_presentation';
} else if(targetFormat == "rtf") {
renderFormat = 'rtf_document';
} else if(targetFormat == "html_vignette") {
fileExtension = 'html';
renderFormat = 'rmarkdown::html_vignette';
} else if(targetFormat == "docx") {
renderFormat = 'word_document';
} else {}
if(renderFormat == "pandoc") {
echo("md_tempfile <- tempfile()\n");
echo("suppressMessages(\n pandoc(\n");
echo(" input=knit(\n \"" + markdownFile + "\",\n output=md_tempfile,\n quiet=TRUE\n ),\n");
echo(" format=\"" + targetFormat + "\"\n");
echo(" )\n)\n");
} else {
echo("render(\n");
echo(" input=\"" + markdownFile + "\",\n");
if(renderFormat == "NULL") {
echo(" output_format=" + renderFormat + ",\n");
} else {
echo(" output_format=\"" + renderFormat + "\",\n");
}
if(renderFormat != "all" && renderFormat != "NULL") {
echo(" output_file=\"" + targetFile + "\",\n");
} else {
echo(" output_dir=\"" + targetDir + "\",\n");
}
echo(" quiet=TRUE\n");
echo(")\n");
}
}
if(renderFormat == "pandoc" || renderFormat == "knit2html") {
echo("\nfile.copy(\n");
echo(" from=paste0(md_tempfile, \"." + fileExtension + "\"),\n");
echo(" to=\"" + targetFile + "\",\n");
echo(" overwrite=TRUE\n)\n");
echo("unlink(paste0(md_tempfile, \"." + fileExtension + "\"))\n\n");
} else {}
}
function printout(is_preview){
// printout the results
if(targetFormat == "all" || targetFormat == "first") {
new Header(i18n("Export RMarkdown file")).addFromUI("markdownFile").addFromUI("targetFormat").addFromUI("targetDir").print();
} else {
new Header(i18n("Export RMarkdown file")).addFromUI("markdownFile").addFromUI("targetFormat").addFromUI("targetFile").print();
}
}
<!DOCTYPE rkhelp >
<document>
<!-- this code was generated using the rkwarddev package.
perhaps don't make changes here, but in the rkwarddev script instead! -->
<summary>
</summary>
<usage>
</usage>
<!-- <section id="sct_EDITORDE" title="EDIT OR DELETE ME">
EDIT OR DELETE ME
</section> -->
<settings>
<caption id="tabSourceAndTarget" />
<setting id="markdownFile">
Choose the file to be exported.
</setting>
<setting id="targetFormat">
Set the target format for exported files. If you choose either "All formats defined in document" or "First format defined in document", the formats defined via output_format will be used, see the documenation on rmarkdown::render().
</setting>
<setting id="targetFile">
Choose the target file name to export to.
</setting>
<setting id="targetDir">
Choose the target directory to export to, file names are automatically generated according to the formats defined in the document.
</setting>
<caption id="tabAdvancedOptions" />
<setting id="markdownVersion">
Set the markdown version to use. Older v1 documents are rendered into HTML format only, using the markdown package. If in doubt, leave it to the newer v2; those documents are rendered using pandoc, which allows you to choose between various output formats. But be aware that some formats may require special configuration or templates to produce proper results.
</setting>
</settings>
<!-- <related>
<ul>
<li>
<link href="rkward://rhelp/..." />
</li>
</ul>
</related> -->
<technical>
</technical>
</document>
<!DOCTYPE rkplugin >
<document>
<!-- this code was generated using the rkwarddev package.
perhaps don't make changes here, but in the rkwarddev script instead!
look for a file called: $SRC/inst/rkward/rkwarddev_rk.RMarkdown_plugin_script.R -->
<code file="ExportRMarkdown.js" />
<help file="ExportRMarkdown.rkh" />
<!-- <dependencies>
</dependencies> -->
<about name="Export RMarkdown" shortinfo="RKWard GUI to export RMarkdown documents in various formats" longinfo="RKWard GUI to export RMarkdown documents in various formats" version="0.01-1" releasedate="2018-10-30" url="https://rkward.kde.org" license="GPL (&gt;= 3)">
<author given="Meik" family="Michalke" email="meik.michalke@hhu.de" role="aut, cre" />
</about>
<logic>
<convert id="logicMDVersion" sources="markdownVersion.string" mode="equals" standard="2" />
<connect governor="logicMDVersion" client="targetFormat.enabled" />
<connect governor="current_filename" client="markdownFile.selection" />
<convert id="logicMDAllFormats" sources="targetFormat.string" mode="equals" standard="all" />
<convert id="logicMDFirstFormat" sources="targetFormat.string" mode="equals" standard="first" />
<convert id="logicMDAllOrFirst" sources="logicMDAllFormats;logicMDFirstFormat" mode="or" />
<connect governor="logicMDAllOrFirst.not" client="targetFile.visible" />
<connect governor="logicMDAllOrFirst.not" client="targetFile.required" />
<connect governor="logicMDAllOrFirst" client="targetDir.visible" />
<connect governor="logicMDAllOrFirst" client="targetDir.required" />
<script>
<![CDATA[
gui.addChangeCommand("markdownFile.selection", "dataChanged()");
gui.addChangeCommand("targetFormat.string", "dataChanged()");
gui.addChangeCommand("markdownVersion.string", "dataChanged()");
// this function is called whenever a new source file
// is selected or the target format changes
dataChanged = function(){
var fromFile = gui.getString("markdownFile");
var toFile = "";
var fileExt = gui.getString("targetFormat");
if(fileExt == "all" || fileExt == "first"){
toFile = fromFile.substr(0, fromFile.lastIndexOf("/"));
gui.setValue("targetDir.selection", toFile);
} else {
var mdVers = gui.getString("markdownVersion");
if(mdVers == "1" || fileExt == "html5" || fileExt == "html_vignette"){
fileExt = "html";
} else if(fileExt == "beamer" || fileExt == "latex"){
fileExt = "pdf";
} else if(fileExt == "markdown" || fileExt == "markdown_github" || fileExt == "gfm"){
fileExt = "md";
} else {};
var fex = fromFile.lastIndexOf(".");
toFile = fromFile.substr(0, fex < 0 ? fromFile.length : fex) + "." + fileExt;
// prevent overwriting source with target
if(fromFile == toFile){
toFile = fromFile.substr(0, fex < 0 ? fromFile.length : fex) + "_new." + fileExt;
} else {}
gui.setValue("targetFile.selection", toFile);
}
}
dataChanged (); // initialize
]]>
</script>
</logic>
<dialog label="Export RMarkdown">
<tabbook id="exportTabbook" label="Export RMarkdown">
<tab label="Source and target" id="tabSourceAndTarget">
<row id="row_cRMFFTAFBD">
<column id="clm_bRMFFTAFBD">
<browser label="RMarkdown file" type="file" id="markdownFile" filter="*.rmd *.md" />
<dropdown id="targetFormat" label="Target format">
<option label="All formats defined in document" value="all" />
<option label="First format defined in document" value="first" checked="true" />
<option label="Beamer presentation" value="beamer" />
<option label="DokuWiki" value="dokuwiki" />
<option label="HTML" value="html" />
<option label="HTML 5" value="html5" />
<option label="Markdown" value="markdown" />
<option label="Markdown (GitHub, old)" value="markdown_github" />
<option label="Markdown (GitHub)" value="gfm" />
<option label="MediaWiki" value="mediawiki" />
<option label="ODT" value="odt" />
<option label="PDF" value="latex" />
<option label="PowerPoint" value="pptx" />
<option label="RTF" value="rtf" />
<option label="Vignette (HTML)" value="html_vignette" />
<option label="Word" value="docx" />
</dropdown>
<browser label="Save as" type="savefile" id="targetFile" required="false" />
<browser label="Save to" type="dir" id="targetDir" required="false" />
<stretch />
</column>
</row>
</tab>
<tab label="Advanced options" id="tabAdvancedOptions">
<row id="row_cVM1122VM1">
<column id="clm_rddmVM1122">
<radio id="markdownVersion" label="Markdown version">
<option label="v1" value="1" />
<option label="v2" value="2" checked="true" />
</radio>
<stretch />
</column>
</row>
</tab>
</tabbook>
</dialog>
</document>
......@@ -25,6 +25,7 @@
<component type="standard" id="save_skeleton" file="save/skeleton/description.xml" label="Create package skeleton" />
<component type="standard" id="save_variables" file="save/write.xml" label="Export vector or matrix data" />
<component type="standard" id="save_csv" file="save/write_csv.xml" label="Export Table / CSV files" />
<component type="standard" id="save_rmarkdown" file="save/ExportRMarkdown.xml" label="Export RMarkdown" />
<component type="standard" id="import_spss" file="import/import_spss.xml" label="Import SPSS">
<attribute id="format" value="*.sav *.por" label="SPSS data files"/>
......@@ -61,7 +62,8 @@
<menu id="export" label="Export">
<entry component="save_r" />
<entry component="save_variables" />
<entry component="save_csv" />
<entry component="save_csv" />
<entry component="save_rmarkdown" />
<entry component="save_skeleton" />
</menu>
</menu>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment