lib/booky/textile/source.rb in booky-0.0.4 vs lib/booky/textile/source.rb in booky-0.0.6

- old
+ new

@@ -1,25 +1,46 @@ 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 + line.gsub(/^source/, "").strip end # Replace the path with the contents of the file def compile_to options begin options = parse(options) - @output = "bc{name: #{options[:name]};}.. " - @output += File.open("#{File.dirname(Booky.source)}/#{options[:file]}", 'rb') { |f| f.read } + 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 Booky::LoadError.new "#{File.dirname(Booky.source)}/#{options[:name]}" + 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 @@ -27,12 +48,17 @@ 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 = 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 \ No newline at end of file