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

Class Method Details

.draft(file:, template:, package: nil, create_dir: 'default', is_package: true, edit: true) ⇒ Object





55
56
57
58
59
60
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
# File 'lib/gknit/draft.rb', line 55

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) >> 0
    raise "The template '#{template}' was not found in the package '#{package}'" if
      !(R.nzchar(template_path) >> 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.expand_path("template.yaml", template_path)
  raise "No 'template.yaml' file found for template '#{template}" if
    !File.file?(template_yaml)

  template_meta = R::Yaml::yaml__load(R.read_utf8(template_yaml))
  raise "template.yaml must contain 'name' and 'description' fields" if
    (template_meta.name.is__null || template_meta.description.is__null) >> 0
  puts "Creating template:"
  puts "Template name: #{template_meta.name >> 0}"
  puts "Description: #{template_meta.description >> 0}"

  if (create_dir == 'default')
    # check if template asks for new directory
    create_dir = template_meta.create_dir.isTRUE >> 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





46
47
48
49
# File 'lib/gknit/draft.rb', line 46

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