dominate 0.1.3 → 0.1.5

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: 027d7683d6936a5b2288d2385c123c521b50e762
4
- data.tar.gz: 2a714a9548917eee30ab5a64cbc1a16acb0180fa
3
+ metadata.gz: 93f5b699571918d5a7f4a599d84f7d324ff88ef1
4
+ data.tar.gz: a3bdcd9fd5ac1dd6145888d03958ebd4334166f0
5
5
  SHA512:
6
- metadata.gz: 0bdd687486a08e26a29815b5a7c519c30a1b75874b6ab8306eebbb2009ae2b2505f805e3f3448a3255061845613f9fe48d74377ab532e0c1ba6a62f3dfe979c9
7
- data.tar.gz: 10c1329d9c4a36da9c375d16cf6aa9b264210a6ec41ebab4f96446529936d10fb3983d66836f7f0e66dbb66756874d3cac08dcbe98187aae7c22f3c8a34c623a
6
+ metadata.gz: 543d051c51e00fd274bc4cc4c331737aa22dc26889963e63763fde5ac7c9ba3609d2e8bb02ead32275b187c5954d8732ede7ad2d3b42de469f1d00cde6d23a7a
7
+ data.tar.gz: c7809e76967b223dc8f1d0fb78ae2ec2b2178e3cce14ca6962606ece132feafaa4a732f4624cfa9e5a23e81b4eff77592505d29c07086378ed3000fc77df852b
data/lib/dominate.rb CHANGED
@@ -8,10 +8,10 @@ require "dominate/dom"
8
8
  module Dominate
9
9
  extend self
10
10
 
11
- attr_accessor :config, :reset_config, :load_all
11
+ attr_accessor :config, :reset_config
12
12
 
13
- def HTML html, data = {}
14
- Dom.new html, data
13
+ def HTML html, instance = false, data = {}
14
+ Dom.new html, instance, data
15
15
  end
16
16
 
17
17
  def setup
data/lib/dominate/dom.rb CHANGED
@@ -1,33 +1,31 @@
1
1
  module Dominate
2
2
  class Dom
3
- attr_accessor :raw_html, :instance, :doc
3
+ attr_accessor :raw_html, :instance, :options, :config, :doc
4
4
 
5
- PARTIAL_REGEX = /<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/
6
- PARTIAL_REGEX_WITHIN =/<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->(.*?)<!--(?:\s*|)\/partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/m
7
- VIEW_TYPES = %w(html slim haml erb md markdown mkd mab)
5
+ YIELD_REGEX = /<!--(?:\s*|)@yield(?:\s*|)-->/
6
+ PARTIAL_REGEX = /<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/
7
+ PARTIAL_REGEX_WITHIN = /<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->(.*?)<!--(?:\s*|)\/partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/m
8
+ VIEW_TYPES = %w(html slim haml erb md markdown mkd mab)
8
9
 
9
- def initialize raw_html, instance = false
10
+ def initialize raw_html, instance = false, config = {}
10
11
  @raw_html = raw_html
11
12
  @instance = instance
13
+ @config = (Dominate.config.to_h.merge config).to_deep_ostruct
12
14
 
13
- if raw_html.match(/<html.*>/)
14
- @doc = Nokogiri::HTML::Document.parse raw_html
15
- else
16
- # need to wrap it in a div to make sure it has a root
17
- @doc = Nokogiri::HTML.fragment "<div>#{raw_html}</div>"
18
- end
19
-
20
- load_partials if Dominate.config.view_path
15
+ set_doc raw_html
16
+ load_html if Dominate.config.view_path
21
17
  end
22
18
 
23
- def load_partials
19
+ def load_html
20
+ load_layout if config.layout
21
+
24
22
  updated_html = doc.inner_html.gsub(PARTIAL_REGEX_WITHIN) do |m|
25
23
  match = m.strip.match(PARTIAL_REGEX)
26
24
  partial = match[1]
27
25
  load_view "#{view_path}/#{partial}"
28
26
  end
29
27
 
30
- doc.inner_html = updated_html if updated_html
28
+ set_doc updated_html if updated_html
31
29
 
32
30
  doc.traverse do |e|
33
31
  if match = e.to_html.strip.match(PARTIAL_REGEX)
@@ -39,6 +37,12 @@ module Dominate
39
37
  end
40
38
  end
41
39
 
40
+ def load_layout
41
+ html = load_view config.layout
42
+ inner_html = doc.inner_html
43
+ set_doc html.gsub YIELD_REGEX, inner_html
44
+ end
45
+
42
46
  def scope name
43
47
  reset_html
44
48
  Scope.new instance, doc.search("[data-scope='#{name}']")
@@ -58,6 +62,15 @@ module Dominate
58
62
 
59
63
  private
60
64
 
65
+ def set_doc html
66
+ if html.match(/<html.*>/)
67
+ @doc = Nokogiri::HTML::Document.parse html
68
+ else
69
+ # need to wrap it in a div to make sure it has a root
70
+ @doc = Nokogiri::HTML.fragment html
71
+ end
72
+ end
73
+
61
74
  def reset_html
62
75
  @html = false
63
76
  end
@@ -101,6 +101,5 @@ module Dominate
101
101
  value.to_s
102
102
  end
103
103
  end
104
-
105
104
  end
106
105
  end
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -6,6 +6,7 @@ setup do
6
6
  Dominate.reset_config!
7
7
  Dominate.setup do |c|
8
8
  c.view_path = './test/dummy'
9
+ c.layout = "#{c.view_path}/app"
9
10
  end
10
11
 
11
12
  inline_html = File.read './test/dummy/index.html'
@@ -80,4 +81,11 @@ scope 'dominate' do
80
81
  assert a.dom.html['Test Company']
81
82
  assert a.dom.html['This should not show'] == nil
82
83
  end
84
+
85
+ test 'layout' do |a|
86
+ assert a.dom.html['<head>'] == nil
87
+ assert a.dom.html['body']
88
+ assert a.dom.html['app layout']
89
+ assert a.dom.html.scan(/body/).length == 2
90
+ end
83
91
  end
data/test/dummy/app.slim CHANGED
@@ -1,3 +1,4 @@
1
1
  html
2
2
  body
3
- /! @content
3
+ | app layout
4
+ /! @yield
@@ -1,26 +1,32 @@
1
- <!-- @partial header -->
2
- <span>This should not show</span>
3
- <!-- /partial header -->
1
+ <html>
2
+ <head>
3
+ </head>
4
+ <body>
5
+ <!-- @partial header -->
6
+ <span>This should not show</span>
7
+ <!-- /partial header -->
4
8
 
5
- <div>
6
- <h1 data-scope='admin_only'>Admin</h1>
7
- <span>current_user:
8
- <span data-instance="current_user.first_name">John</span>
9
- <span data-instance="current_user.last_name">Doe</span>
10
- </span>
11
- </div>
12
- <ul data-scope="list">
13
- <li>
14
- <a href="#" data-prop="todo">test</a>
15
- </li>
16
- <li>
17
- <a href="#" data-prop="todo">testing</a>
18
- </li>
19
- <li>
20
- <ul data-scope="list-nested">
21
- <li data-prop="todo"></li>
22
- </ul>
23
- </li>
24
- </ul>
9
+ <div>
10
+ <h1 data-scope='admin_only'>Admin</h1>
11
+ <span>current_user:
12
+ <span data-instance="current_user.first_name">John</span>
13
+ <span data-instance="current_user.last_name">Doe</span>
14
+ </span>
15
+ </div>
16
+ <ul data-scope="list">
17
+ <li>
18
+ <a href="#" data-prop="todo">test</a>
19
+ </li>
20
+ <li>
21
+ <a href="#" data-prop="todo">testing</a>
22
+ </li>
23
+ <li>
24
+ <ul data-scope="list-nested">
25
+ <li data-prop="todo"></li>
26
+ </ul>
27
+ </li>
28
+ </ul>
25
29
 
26
- <!-- @partial footer -->
30
+ <!-- @partial footer -->
31
+ </body>
32
+ </html>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dominate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj