$:.unshift File.dirname(__FILE__) require 'site_spec' require 'source_content' require 'pathname' module BuildMaster class FileProcessor #todo match only beginning of the file @@TEXTILE_REGX = /---(-)*\n(.*)\n(-)*---\n/ def FileProcessor::join(from, path) result = from path.each_filename do |name| result = File.join(result, name) end return result end attr_reader :path_to_content_file, :path_to_target_file def initialize(template, path_to_content_file, sitespec) @template = template @path_to_content_file = path_to_content_file @sitespec = sitespec extension = File.extname(path_to_content_file) if (extension == '.html') @convert_method = 'process_html' elsif (extension == '.textile') @convert_method = 'process_textile' elsif (extension == '.markdown') @convert_method = 'process_markdown' end if @convert_method basename = File.basename(path_to_content_file, extension).to_s @path = sitespec.relative_to_root(path_to_content_file).parent.join("#{basename}.html") @path_to_target_file = FileProcessor.join(sitespec.output_dir, @path) else @path_to_target_file = FileProcessor.join(sitespec.output_dir, sitespec.relative_to_root(path_to_content_file)) end end def FileProcessor::for_request_path(request_path, site_spec) template = site_spec.load_template relative_to_root = Pathname.new("#{request_path}") dir = relative_to_root.parent extension = relative_to_root.extname.to_s result = nil path_to_content = nil if (extension == '.html') basename = relative_to_root.basename(extension).to_s source_directory = join(site_spec.content_dir, dir) ['textile', 'markdown', 'html'].find do |extension_candidate| filename = "#{basename}.#{extension_candidate}" candidate = File.join(source_directory, filename) if (FileTest.file? candidate) path_to_content = candidate true else false end end end if (not path_to_content) path_to_content = join(site_spec.content_dir, relative_to_root) end return FileProcessor.new(template, path_to_content, site_spec) end def is_html? return File.extname(path_to_target_file) == '.html' end def write_to_target document = generate_document if (document) File.open(path_to_target_file, 'w') do |file| document.write(file, 0, false, true) end else FileUtils.cp path_to_content_file, path_to_target_file end end def generate_document send(@convert_method, load_content()) if (@convert_method) end def load_content return IO.read(path_to_content_file) end def process_textile(textile_content) require 'redcloth' return process_content_with_title(textile_content) {|content| RedCloth.new(content).to_html} end def process_markdown(markdown_content) require 'bluecloth' return process_content_with_title(markdown_content) {|content| BlueCloth.new(content).to_html} end def process_content_with_title(full_content) match_result = @@TEXTILE_REGX.match(full_content) title = '' body_content = full_content if match_result != nil title = match_result[2] body_content = match_result.post_match end html_body = yield body_content html_content = < #{title} #{html_body} HTML return process_html(html_content) end def process_html(html_content) source = SourceContent.new(@path, REXML::Document.new(html_content)) document_with_skin = @template.process(source) return document_with_skin end end end