Class: NewPage::ProcessTemplate
- Inherits:
-
Object
- Object
- NewPage::ProcessTemplate
- Defined in:
- lib/new_page/process_template.rb
Constant Summary collapse
- TEMPLATE_PATH =
Pathname.new("_template")
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#output_file ⇒ Object
readonly
Returns the value of attribute output_file.
Instance Method Summary collapse
-
#initialize(output:, template_name: nil, name: nil, dir: false, path: nil, data: {}, context: nil) ⇒ ProcessTemplate
constructor
A new instance of ProcessTemplate.
- #render(defaults) ⇒ Object
Constructor Details
#initialize(output:, template_name: nil, name: nil, dir: false, path: nil, data: {}, context: nil) ⇒ ProcessTemplate
Returns a new instance of ProcessTemplate.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/new_page/process_template.rb', line 10 def initialize(output:, template_name: nil, name: nil, dir: false, path: nil, data: {}, context: nil, **) @path = if (path.nil?) then Pathname.new(output) / TEMPLATE_PATH else Pathname.new(path) end output = Pathname.new(output) if (name.nil?) then name = output.basename output = output.dirname end if (dir) then @output = Pathname.new(output) / Pathname.new(name) FileUtils.mkdir_p(@output) unless @output.directory? name = 'index' else @output = Pathname.new(output) end @context = context if not template_name.nil? then templates = if (@path / template_name).file? then [ @path / template_name ] else glob = File.(@path) + "/#{template_name}" Dir::glob(glob) end templates.map! { |name| Pathname.new(name) } raise ArgumentError.new("Ambiguos template name #{template_name} : #{templates}") if templates.size > 1 raise ArgumentError.new("No such template #{template_name} at #{glob}") if templates.empty? || !templates[0].file? @input_file = templates[0] else if @path.file? then @input_file = @path @path = @path.dirname else @input_file = @path / template_name end end @output_file = @output / Pathname.new(name).sub_ext(@input_file.extname) raise "#{@input_file} cannot be the same as #{@output_file}" if @input_file == @output_file @template = Liquid::Template.parse(File.read(@input_file)) end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
8 9 10 |
# File 'lib/new_page/process_template.rb', line 8 def context @context end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
8 9 10 |
# File 'lib/new_page/process_template.rb', line 8 def file @file end |
#output_file ⇒ Object (readonly)
Returns the value of attribute output_file.
8 9 10 |
# File 'lib/new_page/process_template.rb', line 8 def output_file @output_file end |
Instance Method Details
#render(defaults) ⇒ Object
63 64 65 |
# File 'lib/new_page/process_template.rb', line 63 def render(defaults) @output_file.write(@template.render(@context), mode: 'w') end |