Class: SSI::SSI

Inherits:
Object
  • Object
show all
Defined in:
lib/ssi.rb

Constant Summary collapse

SSICommands =
{
  :include => 'ssi_include',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SSI

Returns a new instance of SSI.



23
24
25
# File 'lib/ssi.rb', line 23

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/ssi.rb', line 14

def options
  @options
end

Class Method Details

.debug(msg) ⇒ Object



19
# File 'lib/ssi.rb', line 19

def debug(msg); @logger.debug(msg); end

.info(msg) ⇒ Object



17
# File 'lib/ssi.rb', line 17

def info(msg); @logger.info(msg); end

.warn(msg) ⇒ Object



18
# File 'lib/ssi.rb', line 18

def warn(msg); @logger.warn(msg); end

Instance Method Details

#ssi(dir_path, content) ⇒ Object

scan for ssi commands



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ssi.rb', line 59

def ssi(dir_path, content)
  outmap = {}

  content.scan(/(<!--#(.*?)-->)/) do|m|
    outmap[m.first] = {}
    outmap[m.first][:ssi] = m.last.strip # store for processing later
  end

  outmap.each do|k,v|
    cmd, kvs = ssi_parser(v[:ssi])
    content.sub!(k,send(SSICommands[cmd.to_sym], dir_path, kvs))
  end

  content
end

#ssi_include(dir_path, kvs) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ssi.rb', line 27

def ssi_include(dir_path, kvs)
  include_content = ''
  kvs.each do |type,filelist|
    filelist.each do |fdname| 
      if (fdname[0,1] == '/')
        fdname.gsub!(/^\//,'')
        file_path = File::expand_path(fdname, @options[:root_dir])
      else
        file_path = File::expand_path(fdname, dir_path)
      end
      new_dir_path = File::dirname(file_path)
      SSI.debug("Including content from #{file_path}") if @options[:verbose]
      include_content << ssi(new_dir_path, File.read(file_path))
    end
  end
  include_content
end

#ssi_parser(ssi) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/ssi.rb', line 45

def ssi_parser(ssi)
  cmd, attr_val_pairs_str = ssi.split
  kvs = {}
  attr_val_pairs_str.split().each do |ks|
    attrib, val = ks.split('=')
    kvs[attrib.to_sym] ||= []
    kvs[attrib.to_sym] << val.gsub(/^"|"$/,'')
  end
  [cmd, kvs]
end