fuse 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/fuse.rb CHANGED
@@ -3,7 +3,7 @@ require 'thin'
3
3
 
4
4
  module Fuse
5
5
 
6
- VERSION = '0.1.7'
6
+ VERSION = '0.1.8'
7
7
 
8
8
  LOG_COLOURS = {
9
9
  info: 6, # cyan
data/lib/fuse/document.rb CHANGED
@@ -44,27 +44,18 @@ class Fuse::Document
44
44
  link['href'] = asset.relative_path
45
45
  end
46
46
 
47
- #attach stylesheets and scripts
48
- [Fuse::Document::Asset::StyleSheet, Fuse::Document::Asset::JavaScript].each do |klass|
49
- collection = assets.of_type(klass).sort!
50
- next unless collection.length > 0
51
- if @options[:embed_assets]
52
- #todo recreate stylesheet media attributes
53
- tag = Nokogiri::XML::Node.new(klass::EMBED_WITH, document)
54
- raw = collection.map do |asset|
55
- tag['type'] = asset.type
56
- (@options[:compress_assets] ? asset.compress : asset.filtered).strip
57
- end.reject{ |x| x.length == 0 }.join(klass::JOIN_WITH)
58
- next unless raw.length > 0
59
- tag.content = raw
60
- head << tag
61
- else
62
- collection.each do |asset|
63
- data = asset.reference_with
64
- tag = Nokogiri::XML::Node.new(data[:tag_name], document)
65
- data[:attributes].each { |k, v| tag[k] = v unless v.nil? }
66
- head << tag
67
- end
47
+ #attach scripts
48
+ scripts = assets.of_type(Fuse::Document::Asset::JavaScript).sort!
49
+ head << tag_for_collection(scripts, document) unless scripts.empty?
50
+
51
+ #attach stylesheets
52
+ style_sheets = assets.of_type(Fuse::Document::Asset::StyleSheet)
53
+ style_sheets.group_by(&:conditional_signature).each_value do |conditional_collection|
54
+ conditional_collection.group_by(&:media).each do |media, media_collection|
55
+ media_collection.sort!
56
+ tag = tag_for_collection(media_collection, document)
57
+ tag['media'] = media unless media.nil? || media.empty? || Nokogiri::XML::NodeSet === tag
58
+ head << Nokogiri::HTML.fragment(media_collection.first.conditional.wrap(tag.to_html(encoding: @options[:encoding])), @options[:encoding])
68
59
  end
69
60
  end
70
61
 
@@ -94,7 +85,7 @@ class Fuse::Document
94
85
  end
95
86
  end
96
87
  unless font_css.empty?
97
- style_node = head.css('style:not([media]), style[media=all]').first || head.add_child(Nokogiri::XML::Node.new 'style', document)
88
+ style_node = head.add_child(Nokogiri::XML::Node.new 'style', document)
98
89
  style_node.content = font_css + style_node.content
99
90
  end
100
91
 
@@ -151,4 +142,26 @@ class Fuse::Document
151
142
  def assets
152
143
  @assets ||= Asset[root]
153
144
  end
145
+
146
+ def tag_for_collection(collection, document)
147
+ return if collection.empty?
148
+ klass = collection.first.class
149
+ if @options[:embed_assets]
150
+ tag = Nokogiri::XML::Node.new(klass::EMBED_WITH, document)
151
+ raw = collection.map do |asset|
152
+ tag['type'] = asset.type
153
+ (@options[:compress_assets] ? asset.compress : asset.filtered).strip
154
+ end.reject{ |x| x.length == 0 }.join(klass::JOIN_WITH)
155
+ return unless raw.length > 0
156
+ tag.content = raw
157
+ tag
158
+ else
159
+ Nokogiri::XML::NodeSet.new(document, collection.map do |asset|
160
+ data = asset.reference_with
161
+ tag = Nokogiri::XML::Node.new(data[:tag_name], document)
162
+ data[:attributes].each { |k, v| tag[k] = v unless v.nil? }
163
+ tag
164
+ end)
165
+ end
166
+ end
154
167
  end
@@ -1,9 +1,12 @@
1
1
  class Fuse::Document::Asset
2
2
  class StyleSheet < self
3
+
3
4
  EMBED_WITH = 'style'
4
5
  JOIN_WITH = ''
5
- MEDIA_PATTERN = /\(([a-z]+(?:,\s*[a-z]+)*)\)\.[a-z]+$/i
6
+ MEDIA_PATTERN = /\(([a-z]+(?:,\s*[a-z]+)*)\)(?:\.[a-z]+$|\s*\()/i
7
+
6
8
  include HasDependents
9
+
7
10
  def reference_with
8
11
  {
9
12
  tag_name: 'link',
@@ -14,16 +17,28 @@ class Fuse::Document::Asset
14
17
  }
15
18
  }
16
19
  end
20
+
17
21
  def media
18
22
  @media ||= (match = MEDIA_PATTERN.match(path)) && match[1].split(/,\s*/).sort.join(', ')
19
23
  end
24
+
25
+ def conditional
26
+ @conditional ||= Conditional.new self
27
+ end
28
+
29
+ def conditional_signature
30
+ conditional.signature
31
+ end
32
+
20
33
  def compress
21
34
  original = raw
22
35
  compressed = ::Sass.compile original, style: :compressed
23
36
  Fuse.log "SASS: Compressed #{path} from #{original.bytesize} bytes to #{compressed.bytesize} bytes", :success
24
37
  compressed
25
38
  end
39
+
26
40
  def type; 'text/css' end
41
+
27
42
  class Sass < self
28
43
  def filter
29
44
  original = raw
@@ -32,5 +47,37 @@ class Fuse::Document::Asset
32
47
  compiled
33
48
  end
34
49
  end
50
+
51
+ class Conditional
52
+
53
+ CONDITIONAL_PATTERN = /\((!|[lg]te?\s+)?ie(\s*\d+)?\)(?:\.[a-z]+$|\s*\()/i
54
+
55
+ attr_reader :comparison, :version
56
+
57
+ def initialize(style_sheet)
58
+ match = CONDITIONAL_PATTERN.match(style_sheet.path)
59
+ return unless match
60
+ @comparison = (match[1] || '').strip.downcase
61
+ @version = match[2].strip.to_i unless match[2].nil?
62
+ end
63
+
64
+ def wrap(content)
65
+ if comparison == '!'
66
+ "<!--[if !IE]> -->#{content}<!-- <![endif]-->"
67
+ elsif comparison
68
+ "<!--[if #{signature}]>#{content}<![endif]-->"
69
+ else
70
+ content
71
+ end
72
+ end
73
+
74
+ def signature
75
+ @signature ||= begin
76
+ ret = (comparison.nil? || comparison.empty?) ? 'IE' : comparison + ' IE'
77
+ ret << ' ' + version.to_s if version
78
+ end
79
+ end
80
+
81
+ end
35
82
  end
36
83
  end
@@ -30,4 +30,8 @@ class Fuse::Document::AssetCollection < Array
30
30
  self << asset
31
31
  end
32
32
 
33
+ def group_by(*args, &block)
34
+ Hash[super(*args, &block).map{ |k, v| [k, self.class.new(v)] }]
35
+ end
36
+
33
37
  end
data/lib/fuse/server.rb CHANGED
@@ -10,6 +10,8 @@ class Fuse::Server
10
10
  def call(env)
11
11
 
12
12
  request = Rack::Request.new(env)
13
+ result = nil
14
+ status = 200
13
15
 
14
16
  call_options = @options.merge Hash[request.GET.map{ |k, v| [k.to_sym, v] }]
15
17
 
@@ -34,9 +36,15 @@ class Fuse::Server
34
36
  else
35
37
  raise
36
38
  end
39
+ end if request.path == '/'
40
+
41
+ if result.nil?
42
+ log env, 'Not found', :error
43
+ status = 404
44
+ result = render_error('Not found')
37
45
  end
38
46
 
39
- [200, {'Content-Type' => 'text/html'}, [result]]
47
+ [status, {'Content-Type' => 'text/html'}, [result]]
40
48
 
41
49
  end
42
50
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fuse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-12 00:00:00.000000000 Z
12
+ date: 2013-03-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thin