Class: Wavy::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/wavy/core.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, view, save) ⇒ Core

Creates a new Core

Parameters:

  • config (String)

    Main configuration file

  • view (String)

    Template to compile

  • Path (Boolean|String)

    to save output



28
29
30
31
32
33
# File 'lib/wavy/core.rb', line 28

def initialize(config, view, save)
  @view = view
  @config = config
  @save = save
  @config_root = File.expand_path(File.dirname(@config))
end

Instance Method Details

#compileObject

Reads configuration and view files. Begins parsing.



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
62
63
64
65
66
67
68
69
70
71
# File 'lib/wavy/core.rb', line 36

def compile()
  begin

    @config = FILE_IMPORTER.load(@config, true)

    Wavy::Parsers::Import.load(@config, @config_root)
    Wavy::Parsers::Import.extract

    if File.directory?(@view)
      template_dir = @view

      Dir.glob(template_dir + "/**/*.wavy") do |template|
        filename = File.basename(template)

        if filename[0] != "_"

          file_path = File.expand_path(template)
          full_path = template.dup
          full_path.slice! template_dir

          filename = File.basename(template)

          template = FILE_IMPORTER.load(template)
          render(template, full_path, file_path)
        end
      end
    else
      filename = File.basename(@view)
      template = FILE_IMPORTER.load(@view)
      render(template, filename, @view)
    end

  rescue Exception => e
    puts e.message
  end
end

#render(view, filename, path) ⇒ Object

Saves parsed template.

Parameters:

  • view (String)

    Content of the template

  • filename (String)

    The path and filename of the template

  • path (String)

    Path to where the templates should be saved



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
109
110
111
112
113
114
115
116
# File 'lib/wavy/core.rb', line 78

def render(view, filename, path)
  output = Wavy::Models::Template.new(view, path).parse

  if @save != false
    file_path = filename.gsub("#{FILE_SUFFIX}", "")
    file_name = File.basename(file_path)

    if file_name[0] != "_"
      path = File.expand_path(@save)

      if file_path[0] == "/"
        file_path[0] = "" 
      end

      if path[-1,1] == "/"
        path = path + file_path
      else
        path = path + "/" + file_path
      end

      begin
        dirname = File.dirname(path)

        unless File.directory?(dirname)
          FileUtils.mkdir_p(dirname)
        end

        file = File.open(path, "w")
        file.write(output) 
      rescue IOError => e
        raise 'Could not save file.'
      ensure
        file.close unless file == nil
      end
    end
  else
    puts output
  end
end