Module: Giter8

Defined in:
lib/giter8.rb,
lib/giter8/ast.rb,
lib/giter8/pair.rb,
lib/giter8/error.rb,
lib/giter8/fs/fs.rb,
lib/giter8/pairs.rb,
lib/giter8/literal.rb,
lib/giter8/version.rb,
lib/giter8/template.rb,
lib/giter8/conditional.rb,
lib/giter8/renderer/utils.rb,
lib/giter8/renderer/executor.rb,
lib/giter8/parsers/pairs_parser.rb,
lib/giter8/parsers/template_parser.rb

Overview

Giter8 implements a parser and renderer for Giter8 templates

Defined Under Namespace

Modules: Parsers, Renderer Classes: AST, Conditional, Error, FS, FormatterNotFoundError, Literal, Pair, Pairs, PropertyNotFoundError, Template

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.parse_props(props) ⇒ Object

Parses a given String, Hash or File into a Pairs set. When parsing from a File object, file metadata is used on error messages. The parser ignores any content beginning with a hash (#) until the end of the line. Properties are composed of a key comprised of a-z characters, numbers (0-9), unerscores and dashes. When a file is passed, the caller is responsible for closing it.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/giter8.rb', line 33

def self.parse_props(props)
  return props if props.is_a? Pairs

  if [String, Hash, File].none? { |type| props.is_a? type }
    raise Giter8::Error, "parse_props can only be used with strings, hashes, and files. Got #{props.class.name}"
  end

  opts = {}
  if props.is_a? File
    opts[:source] = props.path
    props = props.read
  end
  return Parsers::PairsParser.parse(props, opts) if props.is_a? String

  Pairs.new(props)
end

.parse_template(template) ⇒ Object

Parses a provided Giter8 template from a String or File. When a File is provided, its name will be used in error metadata. Also, when using a File object, the caller is responsible for closing it. Returns an AST object containing the file’s contents.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/giter8.rb', line 54

def self.parse_template(template)
  if [String, File, AST].none? { |type| template.is_a? type }
    raise Giter8::Error, "parse_template can only be used with strings and files. Got #{template.class.name}"
  end

  return template if template.is_a? AST

  opts = {}
  if template.is_a? File
    opts[:source] = template.path
    template = template.read
  end

  Parsers::TemplateParser.parse(template, opts)
end

.render_directory(props, input, output) ⇒ Object

Renders a provided input directory into an output directory using a provided props set.

Raises:



84
85
86
87
88
89
90
# File 'lib/giter8.rb', line 84

def self.render_directory(props, input, output)
  raise Giter8::Error, "Input directory #{input} does not exist" unless File.exist?(input)
  raise Giter8::Error, "Input path #{input} is not a directory" unless File.stat(input).directory?
  raise Giter8::Error, "Destination path #{output} already exists" if File.exist?(output)

  FS.render(props, input, output)
end

.render_template(template, props) ⇒ Object

Renders a given template using a set of props. Template may be a String or File, while props can be a Hash, String, or File. When providing a File to either parameter, the caller is responsible for closing it. Returns a string containing the rendered contents.



74
75
76
77
78
79
80
# File 'lib/giter8.rb', line 74

def self.render_template(template, props)
  template = parse_template template
  props = parse_props props

  executor = Renderer::Executor.new(props)
  executor.exec(template)
end