// Return the selected template and format given the YAML front matter
  public RmdSelectedTemplate getTemplateFormat(String yaml) {
    // This is in the editor load path, so guard against exceptions and log
    // any we find without bringing down the editor. Failing to find a
    // 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;

      // Find the template appropriate to the first output format listed.
      // If no output format is present, assume HTML document (as the
      // renderer does).
      List<String> outFormats = getOutputFormats(tree);
      String outFormat =
          outFormats == null ? RmdOutputFormat.OUTPUT_HTML_DOCUMENT : outFormats.get(0);

      RmdTemplate template = getTemplateForFormat(outFormat);
      if (template == null) return null;

      // If this format produces HTML and is marked as Shiny, treat it as
      // 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;
  }
  private RmdFrontMatterOutputOptions transferOptions(
      RmdFrontMatter frontMatter, RmdTemplate template, String format) {
    RmdFrontMatterOutputOptions result = RmdFrontMatterOutputOptions.create();

    // loop over each option applicable to the new format; if it's
    // transferable, try to find it in one of the other formats
    JsArrayString options = template.getFormat(format).getOptions();
    for (int i = 0; i < options.length(); i++) {
      String optionName = options.get(i);
      RmdTemplateFormatOption option = template.getOption(optionName);
      if (!option.isTransferable()) continue;

      // option is transferable, is it present in another front matter entry?
      JsArrayString formats = frontMatter.getFormatList();
      for (int j = 0; j < formats.length(); j++) {
        RmdFrontMatterOutputOptions outOptions = frontMatter.getOutputOption(formats.get(j));
        if (outOptions == null) continue;
        String val = outOptions.getOptionValue(optionName);
        if (val != null) result.setOptionValue(option, val);
      }
    }

    return result;
  }