module ATD # This module holds everything related to the compilation of routes. module Compilation # A module designed to hold all the precompilation methods module Precompiler extend self # Lists all filestypes that have defined precompiler methods def filetypes instance_methods(true) - [:filetypes] end end # A module designed to hold all the compilation methods module Compiler extend self # Lists all file extentions which have defined compiler methods def filetypes instance_methods(true) - [:filetypes] end end # This method is responsible for live compilation. It takes an ATD::Route as input, and returns either # the filename if Route.output is a file or the Route.output string if Route.output is a string. # It will also take the file and call the corresponding compilation method on it. def self.compile(name, contents) return contents if name.nil? contents = File.read(contents) if contents.is_a? File parse(Compiler, name, contents) end # This method is responsible for precompilation. It takes an ATD::Route as input, and returns either # the filename if Route.output is a file or the Route.output string if Route.output is a string. # It will also take the file and call the corresponding precompilation method on it. # route.output is either a full, expanded file path, a file, or a string def self.precompile(route, *opts) return nil if route.output.nil? if route.output.is_a?(File) name = route.output.is_a?(File) ? File.basename(route.output) : route.output.dup file = route.output.is_a?(File) ? route.output.dup : File.new(route.output) route.output = parse(Precompiler, name, File.read(file)) if opts[0].nil? || opts[0] return name end route.output end class << self private def parse(type, name, contents) name = name.split(".") extensions = name - [name.first] extensions.each do |extension| if type.filetypes.include? extension.to_sym contents = type.send(extension, contents) extensions -= [extension] end end contents end end end end class Object include ATD::Compilation::Compiler end