wavy 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d62eec206e250ccd686d9577cf46e130569c320
4
- data.tar.gz: 5e1a9cf960c8ee15cd255d303383fce4bb730d2b
3
+ metadata.gz: ec8ba39833a876b1c873e30574534d17a9d3505e
4
+ data.tar.gz: 468a62d4bd1c8c8631e5f45a9d9cb57f5b406ab8
5
5
  SHA512:
6
- metadata.gz: 3bfc1bb1a744ea5bac3fe45ad2797445186d59395724e4ab660984324d5d955cc6f5279b4f73b62ca7af8c8fcb5ba480011b067d6cccb696634e944599aa2ee0
7
- data.tar.gz: 5c3d48192889cd154d05136e53dc151fc4ca0c1333b6a15245b65afb5782b395971518471955cdc178f3ffa94d44c52dd4ed0eb23d2b97dc925ea692beedf1f1
6
+ metadata.gz: 19ad15357ccb0bcd2400716a201bf65b534f539d9e53760c7e9ec92d725c9ebe85b80a9545e69b41347717f233598316a43ec7e83705aece8ac157c38d22fa9a
7
+ data.tar.gz: 7a71dc3bbe4645f9e7c4c864fba489491b591c16d38001994a307cc8ad71f3b481bd433d15c5ea37d08d79d570162e0100b47b46fafea4f06d9c70fc8203449e
data/lib/wavy/core.rb CHANGED
@@ -42,19 +42,27 @@ module Wavy
42
42
  Wavy::Parsers::Import.extract
43
43
 
44
44
  if File.directory?(@view)
45
- Dir.glob(@view + "/**/*.wavy") do |template|
46
- full_path = template.dup
47
- full_path.slice! @view
45
+ template_dir = @view
48
46
 
47
+ Dir.glob(template_dir + "/**/*.wavy") do |template|
49
48
  filename = File.basename(template)
50
49
 
51
- template = FILE_IMPORTER.load(template)
52
- render(template, full_path)
50
+ if filename[0] != "_"
51
+
52
+ file_path = File.expand_path(template)
53
+ full_path = template.dup
54
+ full_path.slice! template_dir
55
+
56
+ filename = File.basename(template)
57
+
58
+ template = FILE_IMPORTER.load(template)
59
+ render(template, full_path, file_path)
60
+ end
53
61
  end
54
62
  else
55
63
  filename = File.basename(@view)
56
64
  template = FILE_IMPORTER.load(@view)
57
- render(template, filename)
65
+ render(template, filename, @view)
58
66
  end
59
67
 
60
68
  rescue Exception => e
@@ -64,37 +72,43 @@ module Wavy
64
72
 
65
73
  # Saves parsed template.
66
74
  #
67
- # @param (String) view Content of the view
68
- def render(view, filename)
69
- output = Wavy::Models::Template.new(view).parse
75
+ # @param (String) view Content of the template
76
+ # @param (String) filename The path and filename of the template
77
+ # @param (String) path Path to where the templates should be saved
78
+ def render(view, filename, path)
79
+ output = Wavy::Models::Template.new(view, path).parse
70
80
 
71
81
  if @save != false
72
- filename = filename.gsub("#{FILE_SUFFIX}", "")
73
- path = File.expand_path(@save)
82
+ file_path = filename.gsub("#{FILE_SUFFIX}", "")
83
+ file_name = File.basename(file_path)
74
84
 
75
- if filename[0] == "/"
76
- filename[0] = ""
77
- end
85
+ if file_name[0] != "_"
86
+ path = File.expand_path(@save)
78
87
 
79
- if path[-1,1] == "/"
80
- path = path + filename
81
- else
82
- path = path + "/" + filename
83
- end
84
-
85
- begin
86
- dirname = File.dirname(path)
88
+ if file_path[0] == "/"
89
+ file_path[0] = ""
90
+ end
87
91
 
88
- unless File.directory?(dirname)
89
- FileUtils.mkdir_p(dirname)
92
+ if path[-1,1] == "/"
93
+ path = path + file_path
94
+ else
95
+ path = path + "/" + file_path
90
96
  end
91
97
 
92
- file = File.open(path, "w")
93
- file.write(output)
94
- rescue IOError => e
95
- raise 'Could not save file.'
96
- ensure
97
- file.close unless file == nil
98
+ begin
99
+ dirname = File.dirname(path)
100
+
101
+ unless File.directory?(dirname)
102
+ FileUtils.mkdir_p(dirname)
103
+ end
104
+
105
+ file = File.open(path, "w")
106
+ file.write(output)
107
+ rescue IOError => e
108
+ raise 'Could not save file.'
109
+ ensure
110
+ file.close unless file == nil
111
+ end
98
112
  end
99
113
  else
100
114
  puts output
@@ -4,23 +4,41 @@ module Wavy
4
4
 
5
5
  class Mixins
6
6
 
7
- @data = {}
7
+ @dataFunctions = {}
8
+ @dataTemplates = {}
8
9
 
9
- # Add a mixin to the data model.
10
+ # Add a mixin function to the data model.
10
11
  #
11
12
  # @param (String) id Unique name of the import
12
13
  # @param (Tree::MixinNode) node Mixin node object
13
- def self.add(id, node)
14
- if !@data[id]
15
- @data[id] = node
14
+ def self.addFunction(id, node)
15
+ if !@dataFunctions[id]
16
+ @dataFunctions[id] = node
16
17
  end
17
18
  end
18
19
 
19
- # Get all of the mixin definitions.
20
+ # Add a mixin include to the data model.
20
21
  #
21
- # @return (Object) @data Object of mixins
22
- def self.get()
23
- return @data
22
+ # @param (String) id Unique name of the import
23
+ # @param (Tree::MixinNode) node Mixin node object
24
+ def self.addTemplate(id, node)
25
+ if !@dataTemplates[id]
26
+ @dataTemplates[id] = node
27
+ end
28
+ end
29
+
30
+ # Get all of the mixin function definitions.
31
+ #
32
+ # @return (Object) @dataFunctions Object of mixins
33
+ def self.getFunctions()
34
+ return @dataFunctions
35
+ end
36
+
37
+ # Get all of the mixin include definitions.
38
+ #
39
+ # @return (Object) @dataTemplates Object of mixins
40
+ def self.getTemplates()
41
+ return @dataTemplates
24
42
  end
25
43
 
26
44
  end
@@ -7,14 +7,21 @@ module Wavy
7
7
  # (String) Original template content
8
8
  attr_reader :content
9
9
 
10
+ # (String) Original template path
11
+ attr_reader :path
12
+
10
13
  # (String) Compiled template content
11
14
  attr_reader :compiled
12
15
 
13
16
  # Creates a new view template
14
17
  #
18
+ # @param (String) content Content of file
19
+ # @param (String) path Path to file
20
+ #
15
21
  # @return (String) content The view content (string/html)
16
- def initialize(content)
22
+ def initialize(content, path)
17
23
  @content = content
24
+ @path = path
18
25
  @compiled = ""
19
26
  end
20
27
 
@@ -22,7 +29,7 @@ module Wavy
22
29
  #
23
30
  # @return (String) @compiled Compiled output
24
31
  def parse
25
- @compiled = Wavy::Parsers::Mixin.parse(@content)
32
+ @compiled = Wavy::Parsers::Mixin.parse(@content, @path)
26
33
 
27
34
  return @compiled
28
35
  end
@@ -4,21 +4,24 @@ module Wavy
4
4
 
5
5
  class Mixin
6
6
 
7
- # (Array) Params of mixin
8
- attr_reader :params
9
-
10
7
  # (String) Content within mixin
11
8
  attr_reader :content
12
9
 
10
+ # (Array) Params of mixin
11
+ attr_reader :params
12
+
13
13
  # Creates a new Mixin node
14
14
  #
15
15
  # @param (String) name Name of mixin
16
- # @param (String) params Specified arguments
17
16
  # @param (String) content Content of mixin
18
- def initialize(name, params, content)
17
+ # @param (String|Boolean) params Specified arguments
18
+ def initialize(name, content, params = false)
19
19
  @name = name
20
- @params = get_params(params)
21
20
  @content = content
21
+
22
+ if params != false
23
+ @params = get_params(params)
24
+ end
22
25
  end
23
26
 
24
27
  # Converts string of mixin parameters to an array.
@@ -34,7 +34,8 @@ module Wavy
34
34
  files = Wavy::Models::Imports.get
35
35
 
36
36
  files.each do |key, value|
37
- Wavy::Parsers::Mixin.defined(value.content)
37
+ Wavy::Parsers::Mixin.definedFunctions(value.content)
38
+ #Wavy::Parsers::Mixin.definedTemplates(value.content)
38
39
  end
39
40
  end
40
41
 
@@ -7,36 +7,142 @@ module Wavy
7
7
  # Finds all user-defined mixins.
8
8
  #
9
9
  # @param (String) data Content
10
- def self.defined(data)
10
+ def self.definedFunctions(data)
11
11
  pattern = /^(@mixin)\s(.*)\((.*)\.*\)\s{((?:[^{}]|{\$([^}]*)}|{{([^}]*)}})*)}/
12
12
  matches = data.scan(pattern)
13
13
 
14
14
  if matches.length > 0
15
15
  matches.each do |match|
16
- mixin_node = Wavy::Nodes::Mixin.new(match[1], match[2], match[3])
16
+ name = "function-" + match[1]
17
+ content = match[3]
18
+ params = match[2]
17
19
 
18
- Wavy::Models::Mixins.add(match[1], mixin_node)
20
+ mixin_node = Wavy::Nodes::Mixin.new(name, content, params)
21
+
22
+ Wavy::Models::Mixins.addFunction(name, mixin_node)
23
+ end
24
+ end
25
+ end
26
+
27
+ # Finds all user-defined mixins.
28
+ #
29
+ # @param (String) content Content
30
+ # @param (String|Boolean) path Template path
31
+ def self.definedTemplates(content, path = false)
32
+ pattern = /(@import)\s\"(.*)\"/
33
+ matches = content.scan(pattern)
34
+
35
+ if matches.length > 0
36
+ matches.each do |match|
37
+ file = match[1]
38
+
39
+ name = "template-" + file
40
+
41
+ if path != false
42
+ base_dir = File.expand_path(File.dirname(path))
43
+ file_base_dir = File.expand_path(path)
44
+ full_filename = base_dir + "/" + file
45
+
46
+ # Check if full filename was included
47
+ if File.file?(full_filename)
48
+
49
+ content = FILE_IMPORTER.load(base_dir + "/" + file)
50
+ content = parse(content, path)
51
+ mixin_node = Wavy::Nodes::Mixin.new(name, content, false)
52
+
53
+ Wavy::Models::Mixins.addTemplate(name, mixin_node)
54
+ else
55
+ imported_path_file = File.basename(file)
56
+
57
+ if imported_path_file[0] == "_"
58
+ imported_path_file[0] = ""
59
+ else
60
+ imported_path_file = file
61
+ end
62
+
63
+ file_pattern = /(#{imported_path_file})\..*/
64
+ file_dir = file.scan(/(.*)\//)
65
+ file_contains_paths = file.include? '/'
66
+
67
+ if file_dir[0] || file_contains_paths == false
68
+ if file_dir[0]
69
+ dir_check_path = base_dir + "/" + file_dir[0][0] + "/*"
70
+ else
71
+ dir_check_path = base_dir + "/*"
72
+ end
73
+
74
+ directory_check = Dir.glob(dir_check_path)
75
+
76
+ if directory_check.length > 0
77
+
78
+ Dir.glob(dir_check_path) do |file_path|
79
+ filename = File.basename(file_path)
80
+
81
+ if filename[0] == "_"
82
+ filename[0] = ""
83
+
84
+ check_file_path = File.dirname(file_path) + "/" + filename
85
+ else
86
+ check_file_path = File.dirname(file_path) + "/" + filename
87
+ end
88
+
89
+ file_matches = check_file_path.scan(file_pattern)
90
+
91
+ if file_matches.length > 0
92
+ file_matches.each do |file_match|
93
+ file_match = file_match[0]
94
+
95
+ content = FILE_IMPORTER.load(file_path)
96
+ content = parse(content, file_path)
97
+
98
+ mixin_node = Wavy::Nodes::Mixin.new(name, content, false)
99
+
100
+ Wavy::Models::Mixins.addTemplate(name, mixin_node)
101
+ end
102
+ end
103
+ end
104
+ else
105
+ raise "Could not find imported file A: " + file + "\n From: " + path
106
+ end
107
+ else
108
+ raise "Could not find imported file B: " + path
109
+ end
110
+ end
111
+ end
19
112
  end
20
113
  end
21
114
  end
22
115
 
23
- # Finds all included mixins.
116
+ # Parses all function and template mixins
24
117
  #
25
118
  # @param (String) data Content
119
+ # @param (String|Boolean) path Template path
26
120
  #
27
121
  # @return (String) data Parsed content
28
- def self.parse(template)
122
+ def self.parse(template, path = false)
123
+ template = parseFunctions(template)
124
+ template = parseTemplates(template, path)
125
+
126
+ return template
127
+ end
128
+
129
+ # Finds all included mixin functions.
130
+ #
131
+ # @param (String) template Content
132
+ #
133
+ # @return (String) template Parsed content
134
+ def self.parseFunctions(template)
29
135
  # Search for mixin includes
30
136
  pattern = /(@include)\s(.*)\((.*)\)/
31
137
  matches = template.scan(pattern)
32
138
 
33
- mixins = Wavy::Models::Mixins.get
139
+ mixins = Wavy::Models::Mixins.getFunctions
34
140
 
35
141
  # Go through and parse each include
36
142
  if matches.length > 0
37
143
  matches.each do |match|
38
- if(mixins[match[1]])
39
- mixin = mixins[match[1]]
144
+ if(mixins["function-"+match[1]])
145
+ mixin = mixins["function-"+match[1]]
40
146
  params = match[2].split(",");
41
147
  content = mixin.content
42
148
 
@@ -64,6 +170,43 @@ module Wavy
64
170
  return template
65
171
  end
66
172
 
173
+ # Finds all included mixin functions.
174
+ #
175
+ # @param (String) data Content
176
+ # @param (String) path Template path
177
+ #
178
+ # @return (String) data Parsed content
179
+ def self.parseTemplates(template, path)
180
+ # Search for mixin includes
181
+ pattern = /(@import)\s\"(.*)\"/
182
+ matches = template.scan(pattern)
183
+
184
+ mixins = Wavy::Models::Mixins.getTemplates
185
+
186
+ # Go through and parse each include
187
+ if matches.length > 0
188
+ matches.each do |match|
189
+ if(mixins["template-"+match[1]])
190
+
191
+ mixin = mixins["template-"+match[1]]
192
+ content = mixin.content
193
+
194
+ # Search for includes within mixins
195
+ content = parse(content, path)
196
+
197
+ # Find mixin string to replace
198
+ find = "@import \"#{match[1]}\""
199
+ template = template.gsub(find, content)
200
+ else
201
+ Wavy::Parsers::Mixin.definedTemplates(template, path)
202
+ template = parseTemplates(template, path)
203
+ end
204
+ end
205
+ end
206
+
207
+ return template
208
+ end
209
+
67
210
  end
68
211
 
69
212
  end
data/lib/wavy/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Wavy
2
2
 
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
 
5
5
  class Version
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wavy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Govaere
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-05 00:00:00.000000000 Z
11
+ date: 2014-08-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Wavy is a simple templating engine for HTML – Inspired By Sass
14
14
  email: matthew.govaere@gmail.com
@@ -57,5 +57,5 @@ rubyforge_project:
57
57
  rubygems_version: 2.4.1
58
58
  signing_key:
59
59
  specification_version: 4
60
- summary: A simple templating engine for HTML.
60
+ summary: A simple templating engine for preprocessing HTML.
61
61
  test_files: []