module Booky::Textile class Source include Booky::Textile::Precompiler 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' } # Does the current line need to be precompiled? def matches line return false unless line.match /^source/ line.gsub(/^source/, "").strip end # Replace the path with the contents of the file 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 # Parses the options for a name and the file 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 # Returns the language from the file extension def language file EXTENSIONS[File.extname(file)] rescue nil end end end