Search in sources :

Example 1 with YamlTree

use of org.rstudio.studio.client.rmarkdown.model.YamlTree in project rstudio by rstudio.

the class TextEditingTargetRMarkdownHelper method getCustomKnit.

public String getCustomKnit(String yaml) {
    // any we find without bringing down the editor. 
    try {
        YamlTree tree = new YamlTree(yaml);
        String knit = tree.getKeyValue(RmdFrontMatter.KNIT_KEY);
        return knit;
    } catch (Exception e) {
        Debug.log("Warning: Exception thrown while parsing YAML:\n" + yaml);
    }
    return new String();
}
Also used : YamlTree(org.rstudio.studio.client.rmarkdown.model.YamlTree) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 2 with YamlTree

use of org.rstudio.studio.client.rmarkdown.model.YamlTree in project rstudio by rstudio.

the class TextEditingTargetRMarkdownHelper method getTemplateFormat.

// Return the selected template and format given the YAML front matter
public RmdSelectedTemplate getTemplateFormat(String yaml) {
    // template here just turns off the template-specific UI format editor.
    try {
        YamlTree tree = new YamlTree(yaml);
        boolean isShiny = false;
        if (tree.getKeyValue(RmdFrontMatter.KNIT_KEY).length() > 0)
            return null;
        List<String> outFormats = getOutputFormats(tree);
        // Find the template appropriate to the first output format listed.
        // If no output format is present, assume HTML document (as the 
        // renderer does).
        String outFormat = outFormats == null ? RmdOutputFormat.OUTPUT_HTML_DOCUMENT : outFormats.get(0);
        RmdTemplate template = getTemplateForFormat(outFormat);
        if (template == null)
            return null;
        // a Shiny format
        if (template.getFormat(outFormat).getExtension().equals("html") && tree.getKeyValue(RmdFrontMatter.RUNTIME_KEY).equals(RmdFrontMatter.SHINY_RUNTIME)) {
            isShiny = true;
        }
        return new RmdSelectedTemplate(template, outFormat, isShiny);
    } catch (Exception e) {
        Debug.log("Warning: Exception thrown while parsing YAML:\n" + yaml);
    }
    return null;
}
Also used : RmdTemplate(org.rstudio.studio.client.rmarkdown.model.RmdTemplate) YamlTree(org.rstudio.studio.client.rmarkdown.model.YamlTree) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 3 with YamlTree

use of org.rstudio.studio.client.rmarkdown.model.YamlTree in project rstudio by rstudio.

the class TextEditingTargetRMarkdownHelper method replaceOutputFormatOptions.

public void replaceOutputFormatOptions(final String yaml, final String format, final RmdFrontMatterOutputOptions options, final OperationWithInput<String> onCompleted) {
    server_.convertToYAML(options, new ServerRequestCallback<RmdYamlResult>() {

        @Override
        public void onResponseReceived(RmdYamlResult result) {
            boolean isDefault = options.getOptionList().length() == 0;
            YamlTree yamlTree = new YamlTree(yaml);
            YamlTree optionTree = new YamlTree(result.getYaml());
            // add the output key if needed
            if (!yamlTree.containsKey(RmdFrontMatter.OUTPUT_KEY)) {
                yamlTree.addYamlValue(null, RmdFrontMatter.OUTPUT_KEY, RmdOutputFormat.OUTPUT_HTML_DOCUMENT);
            }
            String treeFormat = yamlTree.getKeyValue(RmdFrontMatter.OUTPUT_KEY);
            if (treeFormat.equals(format)) {
                if (isDefault) {
                // 1-a: if all options are still at their defaults, leave
                // untouched
                } else {
                    // 1-b: not all options are at defaults; replace the simple
                    // format with an option list
                    yamlTree.setKeyValue(RmdFrontMatter.OUTPUT_KEY, "");
                    yamlTree.addYamlValue(RmdFrontMatter.OUTPUT_KEY, format, "");
                    yamlTree.setKeyValue(format, optionTree);
                }
            } else if (treeFormat.length() > 0) {
                // changing it
                if (isDefault) {
                    // case 2-a: change one simple format to another 
                    yamlTree.setKeyValue(RmdFrontMatter.OUTPUT_KEY, format);
                } else {
                    // case 2-b: change a simple format to a complex one
                    yamlTree.setKeyValue(RmdFrontMatter.OUTPUT_KEY, "");
                    yamlTree.addYamlValue(RmdFrontMatter.OUTPUT_KEY, format, "");
                    yamlTree.setKeyValue(format, optionTree);
                }
            } else {
                // case 3: the output format is already not simple
                treeFormat = yamlTree.getKeyValue(format);
                if (treeFormat.equals(RmdFrontMatter.DEFAULT_FORMAT)) {
                    if (isDefault) {
                    // case 3-a: still at default settings
                    } else {
                        // case 3-b: default to complex
                        yamlTree.setKeyValue(format, optionTree);
                    }
                } else {
                    if (isDefault) {
                        // case 3-c: complex to default
                        if (yamlTree.getChildKeys(RmdFrontMatter.OUTPUT_KEY).size() == 1) {
                            // case 3-c-i: only one format, and has default settings
                            yamlTree.clearChildren(RmdFrontMatter.OUTPUT_KEY);
                            yamlTree.setKeyValue(RmdFrontMatter.OUTPUT_KEY, format);
                        } else {
                            // case 3-c-i: multiple formats, this one's becoming
                            // the default
                            yamlTree.clearChildren(format);
                            yamlTree.setKeyValue(format, RmdFrontMatter.DEFAULT_FORMAT);
                        }
                    } else {
                        // case 3-d: complex to complex
                        if (!yamlTree.containsKey(format)) {
                            yamlTree.addYamlValue(RmdFrontMatter.OUTPUT_KEY, format, "");
                        }
                        yamlTree.setKeyValue(format, optionTree);
                    }
                }
            }
            yamlTree.reorder(Arrays.asList(format));
            onCompleted.execute(yamlTree.toString());
        }

        @Override
        public void onError(ServerError error) {
            // if we fail, return the unmodified YAML
            onCompleted.execute(yaml);
        }
    });
}
Also used : RmdYamlResult(org.rstudio.studio.client.rmarkdown.model.RmdYamlResult) ServerError(org.rstudio.studio.client.server.ServerError) YamlTree(org.rstudio.studio.client.rmarkdown.model.YamlTree) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 4 with YamlTree

use of org.rstudio.studio.client.rmarkdown.model.YamlTree in project rstudio by rstudio.

the class TextEditingTargetRMarkdownHelper method convertYamlToShinyDoc.

public String convertYamlToShinyDoc(String yaml) {
    YamlTree yamlTree = new YamlTree(yaml);
    yamlTree.addYamlValue(null, "runtime", "shiny");
    return yamlTree.toString();
}
Also used : YamlTree(org.rstudio.studio.client.rmarkdown.model.YamlTree)

Aggregations

YamlTree (org.rstudio.studio.client.rmarkdown.model.YamlTree)4 JsArrayString (com.google.gwt.core.client.JsArrayString)3 RmdTemplate (org.rstudio.studio.client.rmarkdown.model.RmdTemplate)1 RmdYamlResult (org.rstudio.studio.client.rmarkdown.model.RmdYamlResult)1 ServerError (org.rstudio.studio.client.server.ServerError)1