Class: Booky::Textile::Table

Inherits:
Object
  • Object
show all
Includes:
Precompiler
Defined in:
lib/booky/textile/table.rb

Instance Method Summary collapse

Methods included from Precompiler

included

Instance Method Details

#compile_to(options) ⇒ Object

Replace the path with the contents of the file



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
# File 'lib/booky/textile/table.rb', line 12

def compile_to options
  begin
    options = parse(options)
    
    @output = "table#{options[:header]}.\n"
    @spreadsheet = Spreadsheet.open "#{File.dirname(Booky.source)}/#{options[:file]}"
    
    header = true
    @spreadsheet.worksheet(0).each do |row|
      
      # header
      if header
        @output += "|"
        row.each{ |cell| @output += "_. #{cell} |" }
        @output += "\n"
        header = false
        next
      end
      
      # cells
      @output += "|"
      row.each{ |cell| @output += "#{cell} |" }
      @output += "\n"
    end
    
    @output
  rescue
    raise Booky::LoadError.new "#{File.dirname(Booky.source)}/#{options[:name]}".red
  end
end

#matches(line) ⇒ Object

Does the current line need to be precompiled?



6
7
8
9
# File 'lib/booky/textile/table.rb', line 6

def matches line
  return false unless line.match /^table.*\w+$/
  line.gsub(/^table/, "").strip
end

#parse(options) ⇒ Object

Parses the options for a name and the file



44
45
46
47
48
49
# File 'lib/booky/textile/table.rb', line 44

def parse options
  file = options.scan(/[\w|\.|\/]+$/).to_a.first
  header = options.scan(/{.*}/).to_a.first
  
  { :header => header, :file => file }
end