Class: Bread::Basket::Poster::CSSReader
- Inherits:
-
Object
- Object
- Bread::Basket::Poster::CSSReader
show all
- Includes:
- UnitsHelper, CssParser
- Defined in:
- lib/bread/basket/poster/css_reader.rb
Constant Summary
collapse
- COLUMNS_FAIL =
'To use a flow layout your stylesheet must include ' \
'.columns selector.'
- LAYOUT_FAIL =
'Your stylesheet must include the .layout selector.'
Constants included
from UnitsHelper
UnitsHelper::DIMENSIONS, UnitsHelper::HASH_SELECTOR_REGEX, UnitsHelper::MEASUREMENT_REGEX, UnitsHelper::NUMERIC_REGEX
Instance Attribute Summary collapse
Instance Method Summary
collapse
#convert_inner_units, #convert_units
Constructor Details
#initialize(stylesheet_path, layout) ⇒ CSSReader
Returns a new instance of CSSReader.
18
19
20
21
22
|
# File 'lib/bread/basket/poster/css_reader.rb', line 18
def initialize(stylesheet_path, layout)
@layout = layout
@parser = CssParser::Parser.new
@parser.load_file!(stylesheet_path)
end
|
Instance Attribute Details
#layout ⇒ Object
Returns the value of attribute layout.
12
13
14
|
# File 'lib/bread/basket/poster/css_reader.rb', line 12
def layout
@layout
end
|
#parser ⇒ Object
Returns the value of attribute parser.
12
13
14
|
# File 'lib/bread/basket/poster/css_reader.rb', line 12
def parser
@parser
end
|
Instance Method Details
#attributes_from_specs(specs) ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/bread/basket/poster/css_reader.rb', line 44
def attributes_from_specs(specs)
layout.width = specs.delete 'width'
layout.height = specs.delete 'height'
layout.margin = specs.delete 'margin'
specs.each do |k, v|
layout.create_attribute(k, v)
end
end
|
#create_bounding_boxes ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/bread/basket/poster/css_reader.rb', line 69
def create_bounding_boxes
parser.each_selector do |selector, declarations|
next if skip_selector? selector
method_name = to_method_name selector
specs = rules_to_specs declarations
box = create_box selector, layout, specs
layout.create_attribute(method_name, box)
end
end
|
#create_box(selector, layout, specs) ⇒ Object
85
86
87
88
89
90
91
|
# File 'lib/bread/basket/poster/css_reader.rb', line 85
def create_box(selector, layout, specs)
if specs.key?('src')
ImageBox.new selector, layout, specs
else
Box.new selector, layout, specs
end
end
|
#create_columns ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/bread/basket/poster/css_reader.rb', line 60
def create_columns
columns = parser.find_by_selector '.columns'
layout.give_up COLUMNS_FAIL if columns.empty?
specs = rules_to_specs(columns[0])
columns = Columns.new specs, layout
layout.create_attribute('columns', columns.boxes)
layout.create_attribute('column_styles', specs)
end
|
#create_layout_attributes(specs) ⇒ Object
39
40
41
42
|
# File 'lib/bread/basket/poster/css_reader.rb', line 39
def create_layout_attributes(specs)
attributes_from_specs(specs)
finish_layout_box
end
|
#create_styles ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/bread/basket/poster/css_reader.rb', line 93
def create_styles
parser.each_selector do |selector, declarations|
next unless selector =~ HASH_SELECTOR_REGEX
method_name = to_method_name selector
specs = rules_to_specs declarations
layout.create_attribute(method_name, specs)
end
end
|
#do_your_thing! ⇒ Object
24
25
26
27
28
29
|
# File 'lib/bread/basket/poster/css_reader.rb', line 24
def do_your_thing!
init_layout
create_columns if layout.flow?
create_bounding_boxes
create_styles
end
|
#finish_layout_box ⇒ Object
53
54
55
56
57
58
|
# File 'lib/bread/basket/poster/css_reader.rb', line 53
def finish_layout_box
layout.left = 0
layout.right = layout.width
layout.bottom = 0
layout.top = layout.height
end
|
#init_layout ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/bread/basket/poster/css_reader.rb', line 31
def init_layout
layout_rules = parser.find_by_selector '.layout'
layout.give_up LAYOUT_FAIL if layout_rules.empty?
specs = rules_to_specs(layout_rules[0])
create_layout_attributes(specs)
layout.handle_defaults
end
|
#rule_to_hash(rule, hash) ⇒ Object
116
117
118
119
120
121
122
|
# File 'lib/bread/basket/poster/css_reader.rb', line 116
def rule_to_hash(rule, hash)
rule_arr = rule.split(':')
key = rule_arr[0].strip
value_arr = rule_arr[1].strip.split(',')
value_arr.map!(&:strip)
hash[key] = value_arr
end
|
#rules_to_specs(css_rules) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/bread/basket/poster/css_reader.rb', line 102
def rules_to_specs(css_rules)
rules_arr = css_rules.split(';')
hash = {}
rules_arr.each { |rule| rule_to_hash(rule, hash) }
hash.each_with_object({}) do |(k, v), h|
arr = v.map { |value| convert_units(value) }
h[k] = arr.length == 1 ? arr[0] : arr
end
end
|
#skip_selector?(selector) ⇒ Boolean
79
80
81
82
83
|
# File 'lib/bread/basket/poster/css_reader.rb', line 79
def skip_selector?(selector)
old_selector = %w(.layout .columns).include? selector
hash_selector = selector =~ HASH_SELECTOR_REGEX
old_selector || hash_selector
end
|
#to_method_name(hash_selector) ⇒ Object
124
125
126
|
# File 'lib/bread/basket/poster/css_reader.rb', line 124
def to_method_name(hash_selector)
hash_selector.sub('#', '').sub('.', '').gsub('-', '_')
end
|