diff options
Diffstat (limited to 'lib/coderay/scanners/css.rb')
-rw-r--r-- | lib/coderay/scanners/css.rb | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/lib/coderay/scanners/css.rb b/lib/coderay/scanners/css.rb index 6413f8f..e5f03f5 100644 --- a/lib/coderay/scanners/css.rb +++ b/lib/coderay/scanners/css.rb @@ -2,9 +2,9 @@ module CodeRay module Scanners class CSS < Scanner - + register_for :css - + KINDS_NOT_LOC = [ :comment, :class, :pseudo_class, :type, @@ -20,28 +20,28 @@ module Scanners NMChar = /[-_a-zA-Z0-9]|#{Escape}/ NMStart = /[_a-zA-Z]|#{Escape}/ NL = /\r\n|\r|\n|\f/ - String1 = /"(?:[^\n\r\f\\"]|\\#{NL}|#{Escape})*"?/ # FIXME: buggy regexp - String2 = /'(?:[^\n\r\f\\']|\\#{NL}|#{Escape})*'?/ # FIXME: buggy regexp + String1 = /"(?:[^\n\r\f\\"]|\\#{NL}|#{Escape})*"?/ # TODO: buggy regexp + String2 = /'(?:[^\n\r\f\\']|\\#{NL}|#{Escape})*'?/ # TODO: buggy regexp String = /#{String1}|#{String2}/ - + HexColor = /#(?:#{Hex}{6}|#{Hex}{3})/ Color = /#{HexColor}/ - + Num = /-?(?:[0-9]+|[0-9]*\.[0-9]+)/ Name = /#{NMChar}+/ Ident = /-?#{NMStart}#{NMChar}*/ AtKeyword = /@#{Ident}/ Percentage = /#{Num}%/ - + reldimensions = %w[em ex px] absdimensions = %w[in cm mm pt pc] Unit = Regexp.union(*(reldimensions + absdimensions)) - + Dimension = /#{Num}#{Unit}/ - + Comment = %r! /\* (?: .*? \*/ | .* ) !mx Function = /(?:url|alpha|attr|counters?)\((?:[^)\n\r\f]|\\\))*\)?/ - + Id = /##{Name}/ Class = /\.#{Name}/ PseudoClass = /:#{Name}/ @@ -64,20 +64,26 @@ module Scanners when :initial, :media if match = scan(/(?>#{RE::Ident})(?!\()|\*/ox) encoder.text_token match, :type + next elsif match = scan(RE::Class) encoder.text_token match, :class + next elsif match = scan(RE::Id) encoder.text_token match, :constant + next elsif match = scan(RE::PseudoClass) encoder.text_token match, :pseudo_class + next elsif match = scan(RE::AttributeSelector) # TODO: Improve highlighting inside of attribute selectors. encoder.text_token match[0,1], :operator encoder.text_token match[1..-2], :attribute_name if match.size > 2 encoder.text_token match[-1,1], :operator if match[-1] == ?] + next elsif match = scan(/@media/) encoder.text_token match, :directive states.push :media_before_name + next end when :block @@ -87,18 +93,21 @@ module Scanners else encoder.text_token match, :key end + next end - + when :media_before_name if match = scan(RE::Ident) encoder.text_token match, :type states[-1] = :media_after_name + next end when :media_after_name if match = scan(/\{/) encoder.text_token match, :operator states[-1] = :media + next end else @@ -110,12 +119,12 @@ module Scanners elsif match = scan(/\/\*(?:.*?\*\/|\z)/m) encoder.text_token match, :comment - + elsif match = scan(/\{/) value_expected = false encoder.text_token match, :operator states.push :block - + elsif match = scan(/\}/) value_expected = false if states.last == :block || states.last == :media @@ -124,14 +133,14 @@ module Scanners else encoder.text_token match, :error end - + elsif match = scan(/#{RE::String}/o) encoder.begin_group :string encoder.text_token match[0, 1], :delimiter encoder.text_token match[1..-2], :content if match.size > 2 encoder.text_token match[-1, 1], :delimiter if match.size >= 2 encoder.end_group :string - + elsif match = scan(/#{RE::Function}/o) encoder.begin_group :string start = match[/^\w+\(/] @@ -143,22 +152,22 @@ module Scanners encoder.text_token match[start.size..-1], :content end encoder.end_group :string - + elsif match = scan(/(?: #{RE::Dimension} | #{RE::Percentage} | #{RE::Num} )/ox) encoder.text_token match, :float - + elsif match = scan(/#{RE::Color}/o) encoder.text_token match, :color - + elsif match = scan(/! *important/) encoder.text_token match, :important - + elsif match = scan(/(?:rgb|hsl)a?\([^()\n]*\)?/) encoder.text_token match, :color - + elsif match = scan(RE::AtKeyword) encoder.text_token match, :directive - + elsif match = scan(/ [+>:;,.=()\/] /x) if match == ':' value_expected = true @@ -166,18 +175,18 @@ module Scanners value_expected = false end encoder.text_token match, :operator - + else encoder.text_token getch, :error - + end - + end - + encoder end - + end - + end end |