Class: Booky::Textile::Source

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

Constant Summary collapse

EXTENSIONS =
{
  '.txt'    => 'text',
  '.rb'     => 'ruby',
  '.java'   => 'java',
  '.html'   => 'html',
  '.htm'    => 'html',
  '.js'     => 'javascript',
  '.coffee' => 'javascript',
  '.css'    => 'css',
  '.c'      => 'c',
  '.cpp'    => 'cpp',
  '.pl'     => 'perl',
  '.py'     => 'python',
  '.php'    => 'php',
  '.sql'    => 'sql'
}

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



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

def compile_to options
  begin
    options = parse(options)
    if @lang = language("#{File.dirname(Booky.source)}/#{options[:file]}")
      @output = "bc{name: #{options[:name]};language: #{@lang}}.. "
    else
      @output = "bc{name: #{options[:name]};}.. "
    end
    @output += File.open("#{File.dirname(Booky.source)}/#{options[:file]}", 'rb:UTF-8') { |f| f.read }
    @output += "\n\np. \n"
    @output
  rescue
    raise "Couldn't find source file: #{File.dirname(Booky.source)}/#{options[:name]}".red
  end
end

#language(file) ⇒ Object

Returns the language from the file extension



59
60
61
# File 'lib/booky/textile/source.rb', line 59

def language file
  EXTENSIONS[File.extname(file)] rescue nil
end

#matches(line) ⇒ Object

Does the current line need to be precompiled?



23
24
25
26
# File 'lib/booky/textile/source.rb', line 23

def matches line
  return false unless line.match /^source/
  line.gsub(/^source/, "").strip
end

#parse(options) ⇒ Object

Parses the options for a name and the file



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/booky/textile/source.rb', line 46

def parse options
  if name_match = options.match(/{(.*?)}/)
    options = options.gsub(name_match[0], "")
    name = name_match[1]
  end
  
  file = options[1..-1].strip
  #name = File.basename("#{File.dirname(Booky.source)}/#{file}") unless name
  
  { :name => name, :file => file }
end