Module: MiniSyntax::Highlighter::CSS

Defined in:
lib/minisyntax/highlighter/css.rb

Class Method Summary collapse

Class Method Details

.highlight(code) ⇒ Object



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
# File 'lib/minisyntax/highlighter/css.rb', line 4

def self.highlight(code)
  code.gsub! %r(( *)((\$[a-z\-_]+):(.+?);|([_\*]?[a-z\-]+:)(("|[^&])+?);|@import (.+?);|(([\.\#]?[a-z0-9\-_&:]+([,\s]\s*[\.\#]?[a-z0-9\-_&:]+)*))(\s*)\{(.*?\n\1)\})|@media (.+?)\{|@include (.+?);|@extend (.+?);)im do
    intendation = $1
    if $3
      %Q(#{intendation}<var>#{$3}</var>:#{highlight_value($4)};)
    elsif $5
      %Q(#{intendation}<b>#{$5}</b>#{highlight_value($6)};)
    elsif $8
      %Q(#{intendation}@<em>import</em> <q>#{$8}</q>;)
    elsif $10
      whitespace = $12
      rules = $13
      # selector = $10.gsub(/([\.\#\b])([a-z0-9\-_]+)\b/i) do
      #   if $1 == '.'
      #     %Q(<b><i>#{$1}#{$2}</i></b>)
      #   elsif $1 == '#'
      #     %Q(<b>#{$1}#{$2}</b>)
      #   else
      #     %Q(<em>#{$2}</em>)
      #   end
      # end
      selector = %Q(<b><i>#{$10}</i></b>)
      %Q(#{intendation}#{selector}#{whitespace}{#{highlight(rules)}})
    elsif $14
      %Q(#{intendation}@<em>media</em> #{$14.gsub('and', '<em>and</em>')}{)
    elsif $15
      call = $15
      mixin = call.gsub(/^([a-z\-_]+).*$/, '<b>\\1</b>')
      parameter = call.gsub(/^.*?\((.+?)\)/) { "(#{highlight_value($1)})" }
      %Q(#{intendation}@<em>include</em> #{mixin}#{parameter};)
    elsif $16
      call = $16
      rule = call.gsub(/^([\.#%]?[a-z\-_]+).*$/, '<b>\\1</b>')
      %Q(#{intendation}@<em>extend</em> #{rule};)
    end
  end
  code.gsub! %r((<i>)?(//.*?$|/\*.*?\*/)) do
    comment = $2
    if $1 == '<i>' or comment.gsub(%r(<q>(.*?)</q>), "\\1") =~ %r(</q>)
      comment
    else
      comment.gsub! %r(</?(b|i|em|var|code)>), ""
      %Q(<i>#{comment}</i>)
    end
  end
  code
end