docomo_css 0.4.0 → 0.4.2

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.
data/README.rdoc CHANGED
@@ -13,15 +13,17 @@ Instead, every element must use its own style attribute, for example
13
13
 
14
14
  docomo_css works around this by inlining all CSS automatically for you.
15
15
 
16
- Also, docomo does not support styling some elements such as h1, but does if you nest the contents in a span. This library automatically handles transforming
16
+ Also, docomo does not support styling some elements such as h1, but does if you nest the contents in a span or div. This library automatically handles transforming of markup such as
17
17
 
18
18
  <h1>foo</h1>
19
19
 
20
20
  to
21
21
 
22
- <h1><span>foo</span></h1>
22
+ <h1><span style="font-size:x-small">foo</span></h1>
23
23
 
24
- so you don't need to change any styling.
24
+ so you don't need to change any styling. If you were doing this manually before and don't want docomo_css to automatically update your html, use
25
+
26
+ docomo_css :inject_unsupported_style => false
25
27
 
26
28
  == Install
27
29
 
@@ -0,0 +1,41 @@
1
+ require 'nokogiri'
2
+
3
+ module DocomoCss
4
+ module EmbeddableStyle
5
+ def embed_style(style, options = {})
6
+ options = {:inject_unsupported_styles => true}.merge!(options)
7
+ style = style.dup
8
+ inject_unsupported_styles(style) if options[:inject_unsupported_styles]
9
+ merge_style style
10
+ end
11
+
12
+ def merge_style(other_style)
13
+ return if other_style.empty?
14
+ if self['style'] == nil
15
+ self['style'] = other_style.to_s
16
+ else
17
+ self['style'] += ";" unless self['style'] =~ /;\Z/
18
+ self['style'] += other_style.to_s
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def inject_unsupported_styles(style)
25
+ if /h\d/ =~ name
26
+ if (h = style.split('color', 'font-size')) && !h.empty? && !children.empty?
27
+ children.wrap('<span>')
28
+ children.first.merge_style h
29
+ end
30
+ if (h = style.split('background-color', 'background')) && !h.empty? && !children.empty?
31
+ div = Nokogiri.make("<div>")
32
+ div.merge_style(h)
33
+ replace(div)
34
+ div.add_child(self)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ Nokogiri::XML::Element.send(:include, DocomoCss::EmbeddableStyle)
@@ -1,9 +1,14 @@
1
1
  require 'docomo_css/stylesheet'
2
+ require 'docomo_css/embeddable_style'
2
3
  require 'nokogiri'
3
4
  require 'tiny_css'
4
5
 
5
6
  module DocomoCss
6
7
  class Filter
8
+ def initialize(options = {})
9
+ @options = {}
10
+ end
11
+
7
12
  def after(controller)
8
13
  return unless controller.response.content_type =~ /application\/xhtml\+xml/
9
14
  return unless controller.request.user_agent =~ /docomo/i
@@ -38,36 +43,12 @@ module DocomoCss
38
43
 
39
44
  def embed_style(doc, css)
40
45
  css.style.each do |selector, style|
41
- stringified_style = stringify_style(style)
42
46
  doc.css(selector).each do |element|
43
- # inject support for unsupported styles
44
- if /h\d/ =~ element.name
45
- # font-size needs to be changed in span
46
- element.children.wrap('<span>')
47
- element.children.first['style'] = merge_style element['style'], stringified_style
48
-
49
- # background-color should be changed in div to give 100% width
50
- div = Nokogiri.make("<div>")
51
- div['style'] = merge_style element['style'], stringified_style
52
- element.replace(div)
53
- div.add_child(element)
54
- else
55
- element['style'] = merge_style element['style'], stringified_style
56
- end
47
+ element.embed_style(style)
57
48
  end
58
49
  end
59
50
  end
60
51
 
61
- def stringify_style(style)
62
- style.map { |k, v| "#{ k }:#{ v }" }.join ';'
63
- end
64
-
65
- def merge_style(style, other_style)
66
- return other_style if style == nil
67
- style += ";" unless style =~ /;\Z/
68
- style + other_style
69
- end
70
-
71
52
  def escape_numeric_character_reference(text)
72
53
  text.gsub /&#(\d+|x[\da-fA-F]+);/, 'HTMLCSSINLINERESCAPE\1::::::::'
73
54
  end
@@ -3,8 +3,8 @@ require 'docomo_css/filter'
3
3
  class DocomoCss::Railtie < Rails::Railtie
4
4
  initializer "docomo_css.extend.action_controller" do
5
5
  ActionController::Base.class_eval do
6
- def self.docomo_filter
7
- after_filter DocomoCss::Filter.new
6
+ def self.docomo_filter(options = {} )
7
+ after_filter DocomoCss::Filter.new(options)
8
8
  end
9
9
  end
10
10
  end
@@ -1,3 +1,3 @@
1
- class DocomoCss
2
- Version = "0.4.0"
1
+ module DocomoCss
2
+ Version = "0.4.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docomo_css
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 0
10
- version: 0.4.0
9
+ - 2
10
+ version: 0.4.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - milk1000cc
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-12-16 00:00:00 +09:00
19
+ date: 2010-12-20 00:00:00 +09:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -39,12 +39,14 @@ dependencies:
39
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
- - - ">="
42
+ - - ~>
43
43
  - !ruby/object:Gem::Version
44
- hash: 3
44
+ hash: 25
45
45
  segments:
46
46
  - 0
47
- version: "0"
47
+ - 1
48
+ - 1
49
+ version: 0.1.1
48
50
  type: :runtime
49
51
  version_requirements: *id002
50
52
  - !ruby/object:Gem::Dependency
@@ -74,6 +76,7 @@ extra_rdoc_files: []
74
76
  files:
75
77
  - MIT-LICENSE
76
78
  - README.rdoc
79
+ - lib/docomo_css/embeddable_style.rb
77
80
  - lib/docomo_css/filter.rb
78
81
  - lib/docomo_css/railtie.rb
79
82
  - lib/docomo_css/stylesheet.rb