Module: GKnit
- Defined in:
- lib/gknit/draft.rb
Overview
RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Class Method Summary collapse
-
.draft(file:, template:, package: nil, create_dir: 'default', is_package: true, edit: true) ⇒ Object
--------------------------------------------------------------------------------------.
-
.template_path(gem, family, template) ⇒ Object
--------------------------------------------------------------------------------------.
Class Method Details
.draft(file:, template:, package: nil, create_dir: 'default', is_package: true, edit: true) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gknit/draft.rb', line 61 def self.draft(file:, template:, package: nil, create_dir: 'default', is_package: true, edit: true) # ignore the file extension if given. It should always be an .Rmd file file_basename = File.basename(file, File.extname(file)) file = "#{file_basename}.Rmd" # resolve package file # TODO: if package is a rubygem, then look there somehow if (is_package) template_path = R.system__file("rmarkdown", "templates", template, package: package).unboxed_get(0) raise "The template '#{template}' was not found in the package '#{package}'" if !(R.nzchar(template_path).unboxed_get(0)) else full_filename = Dir.glob("gknit-templates*", base: "#{Gem.default_dir}/gems") template_path = "#{Gem.default_dir}/gems/#{full_filename[0]}/#{template}" puts template_path # raise "this is not an R package" end # read the template.yaml and confirm it has the right fields template_yaml = File.("template.yaml", template_path) raise "No 'template.yaml' file found for template '#{template}" if !File.file?(template_yaml) = R::Yaml::yaml__load(R.read_utf8(template_yaml)) raise "template.yaml must contain 'name' and 'description' fields" if (.name.is__null || .description.is__null).unboxed_get(0) puts "Creating template:" puts "Template name: #{.name.unboxed_get(0)}" puts "Description: #{.description.unboxed_get(0)}" if (create_dir == 'default') # check if template asks for new directory create_dir = .create_dir.isTRUE.unboxed_get(0) if (create_dir) raise "The directory '#{file_basename}' already exists" if Dir.exist?(file_basename) end end raise "File #{file} already exists" if File.file?("#{file_basename}/#{file}") FileUtils.cp_r("#{template_path}/skeleton", "#{file_basename}") # FileUtils.cp("#{template_path}/resources/template.tex", "#{file_basename}") File.rename("#{file_basename}/skeleton.Rmd", "#{file_basename}/#{file}") end |
.template_path(gem, family, template) ⇒ Object
30 31 32 33 |
# File 'lib/gknit/draft.rb', line 30 def self.template_path(gem, family, template) full_filename = Dir.glob("gknit-templates*", base: "#{Gem.default_dir}/gems") template_path = "#{Gem.default_dir}/gems/#{full_filename[0]}/#{family}/#{template}" end |