File: stylesheets.rb

package info (click to toggle)
ruby-compass 0.12.2~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,308 kB
  • sloc: ruby: 10,474; makefile: 42; xml: 14
file content (230 lines) | stat: -rw-r--r-- 6,200 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
require 'rdiscount'

def stylesheets_dir(framework)
  Compass::Frameworks[framework].stylesheets_directory
end

def tree_key(item)
  "tree/"+[item[:framework], item[:stylesheet]].join("/")
end

def tree(item)
  @site.cached(tree_key(item)) do
    file = File.join(stylesheets_dir(item[:framework]), item[:stylesheet])
    contents = File.read(file)
    syntax = item[:stylesheet] =~ /\.scss$/ ? :scss : :sass
    Sass::Engine.new(contents, :syntax => syntax).send :to_tree
  end
end

def imports(item)
  sass_tree = tree(item)
  imports = []
  sass_tree.children.each do |child|
    if child.is_a?(Sass::Tree::ImportNode)
      imports << child.imported_filename
    end
  end
  imports.sort
end

def reference_item(options)
  stylesheet = options[:stylesheet]
  path = stylesheet_path(stylesheet)
  if path
    @site.cached("reference/item/#{path}") do
      @items.detect do |i|
        if i.identifier =~ /^\/reference/ && i[:stylesheet]
          i[:stylesheet] == path
        end
      end
    end
  end
end

def departialize(path)
  path.gsub(%r{(\b|/)_}){|m| m.size > 1 ? "/" : ""}
end

def reference_path(options)
  if item = reference_item(options)
    rep = item.reps.find { |r| r.name == :default }
    rep.path
  end
end

def import_paths
  paths = []
  if @item[:stylesheet]
    paths << [File.join(Compass::Frameworks[@item[:framework]].stylesheets_directory,
                        File.dirname(@item[:stylesheet])),
              @item[:stylesheet]["/"] ? File.dirname(@item[:stylesheet]) : ""]
  end

  paths += Compass::Frameworks::ALL.inject([]) {|m, f| m << f.stylesheets_directory}.map!{|p|[p, '']}
  paths
end

def stylesheet_path(ss)
  possible_names = possible_filenames_for_stylesheet(ss)
  import_paths.each do |import_path|
    possible_names.each do |filename|
      full_path = File.join(import_path.first, filename)
      if File.exist?(full_path)
        return "#{import_path.last}#{"/" if import_path.last && import_path.last.length > 0}#{filename}"
      end
    end
  end
  nil
end

def possible_filenames_for_stylesheet(ss)
  ext = File.extname(ss)
  path = File.dirname(ss)
  path = path == "." ? "" : "#{path}/"
  base = File.basename(ss)[0..-(ext.size+1)]
  extensions = if ext.size > 0
    [ext]
  else
    [".scss", ".sass"]
  end
  basenames = ["_#{base}", base]
  filenames = []
  basenames.each do |basename|
    extensions.each do |extension|
      filenames << "#{path}#{basename}#{extension}"
    end
  end
  filenames
end

def mixins(item)
  sass_tree = tree(item)
  mixins = []
  comment = nil
  sass_tree.children.each do |child|
    if child.is_a?(Sass::Tree::MixinDefNode)
      child.comment = comment && Sass::Tree::CommentNode.clean(comment)
      comment = nil
      mixins << child
    elsif child.is_a?(Sass::Tree::CommentNode)
      comment ||= ""
      comment << "\n" unless comment.empty?
      comment << child.docstring
    else
      comment = nil
    end
  end
  mixins.reject{|m| m.comment =~ /@private/}
end

def functions(item)
  sass_tree = tree(item)
  functions = []
  comment = nil
  sass_tree.children.each do |child|
    if child.is_a?(Sass::Tree::FunctionNode)
      child.comment = comment && Sass::Tree::CommentNode.clean(comment)
      comment = nil
      functions << child
    elsif child.is_a?(Sass::Tree::CommentNode)
      comment ||= ""
      comment << "\n" unless comment.empty?
      comment << child.docstring
    else
      comment = nil
    end
  end
  functions.reject{|m| m.comment =~ /@private/}
end

def constants(item)
  sass_tree = tree(item)
  constants = []
  comment = nil
  sass_tree.children.each do |child|
    if child.is_a?(Sass::Tree::VariableNode)
      child.comment = comment && Sass::Tree::CommentNode.clean(comment)
      comment = nil
      child.name.tr!("_",'-')
      constants << child
    elsif child.is_a?(Sass::Tree::CommentNode)
      comment ||= ""
      comment << "\n" unless comment.empty?
      comment << child.docstring
    else
      comment = nil
    end
  end
  constants.reject{|c| c.comment =~ /@private/}
end

def all_constants
  @items.inject([]) do |variables, item|
    next variables unless item.identifier =~ %r{/reference}
    next variables unless item[:stylesheet]
    variables += constants(item).map{|v| [item, v] }
  end
end

def all_mixins
  @items.inject([]) do |all_mixins, item|
    next all_mixins unless item.identifier =~ %r{/reference}
    next all_mixins unless item[:stylesheet]
    all_mixins += mixins(item).map{|m| [item, m] }
  end
end

def all_functions
  @items.inject([]) do |all_functions, item|
    next all_functions unless item.identifier =~ %r{/reference}
    next all_functions unless item[:stylesheet]
    all_functions += functions(item).map{|f| [item, f] }
  end
end

# Sass Only Functions from 3.1.10 (Brainy Betty)
# Not as elegant, but does the trick.
def sass_functions
  [:rgb, :rgba, :hsl, :hsla, :red, :green, :blue, :hue, :saturation, :lightness, :alpha, :opacity, :opacify, :fade_in, :transparentize, :fade_out, :lighten, :darken, :saturate, :desaturate, :adjust_hue, :adjust_color, :scale_color, :change_color, :mix, :grayscale, :complement, :invert, :unquote, :quote, :type_of, :unit, :unitless, :comparable, :percentage, :round, :ceil, :floor, :abs, :length, :nth, :join, :append, :zip, :index, :if]
end

def example_items
  @site.cached("examples") do
    @items.select do |i|
      i.identifier =~ /^\/examples/ && i[:example]
    end
  end
end

def item_for_function_name(function_name)
  @items.detect do |item|
    (item.identifier =~ %r{helpers}) && item[:documented_functions] && item[:documented_functions].include?(function_name)
  end
end

def examples_for_item(item)
  @site.cached("examples/#{item.identifier}") do
    example_items.select do |i|
      i[:framework] == item[:framework] &&
      i[:stylesheet] == item[:stylesheet]
    end
  end
end

def examples(item, mixin = nil)
  examples = examples_for_item(item)
  if mixin
    examples = examples.select {|i| i[:mixin] == mixin.name }
  else
    examples = examples.reject {|i| i[:mixin] }
  end
  examples.map{|i| i.reps.find{|r| r.name == :default}}
end

def format_doc(docstring)
  if docstring
    RDiscount.new(docstring).to_html
  end
end