File: apache.rb

package info (click to toggle)
ruby-rugments 1.0.0~beta8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 820 kB
  • sloc: ruby: 10,293; makefile: 2
file content (67 lines) | stat: -rw-r--r-- 1,645 bytes parent folder | download | duplicates (3)
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
require 'yaml'

module Rugments
  module Lexers
    class Apache < RegexLexer
      title 'Apache'
      desc 'configuration files for Apache web server'
      tag 'apache'
      mimetypes 'text/x-httpd-conf', 'text/x-apache-conf'
      filenames '.htaccess', 'httpd.conf'

      class << self
        attr_reader :keywords
      end
      # Load Apache keywords from separate YML file
      @keywords = ::YAML.load(File.open(Pathname.new(__FILE__).dirname.join('apache/keywords.yml')))

      def name_for_token(token)
        if self.class.keywords[:sections].include? token
          Name::Class
        elsif self.class.keywords[:directives].include? token
          Name::Label
        elsif self.class.keywords[:values].include? token
          Literal::String::Symbol
        end
      end

      state :whitespace do
        rule /\#.*?\n/, Comment
        rule /[\s\n]+/m, Text
      end

      state :root do
        mixin :whitespace

        rule /(<\/?)(\w+)/ do |m|
          groups Punctuation, name_for_token(m[2])
          push :section
        end

        rule /\w+/ do |m|
          token name_for_token(m[0])
          push :directive
        end
      end

      state :section do
        mixin :whitespace

        # Match section arguments
        rule /([^>]+)?(>\n)/ do |_m|
          groups Literal::String::Regex, Punctuation
          pop!
        end
      end

      state :directive do
        # Match value literals and other directive arguments
        rule /(\w+)*(.*?(\n|$))/ do |m|
          token name_for_token(m[1]), m[1]
          token Text, m[2]
          pop!
        end
      end
    end
  end
end