$:.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 :content_file, :target_file def initialize(template, content_file, sitespec) @template = template @content_file = content_file @sitespec = sitespec extension = content_file.extname 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 = content_file.basename @path = sitespec.relative_to_root(content_file).parent.join("#{basename}.html") @target_file = sitespec.output_dir.file(@path) else @target_file = sitespec.output_dir.file(sitespec.relative_to_root(content_file)) end end def FileProcessor::for_request_path(request_path, site_spec) template = site_spec.load_template if (request_path[0,1] == '/') request_path = request_path[1, request_path.length - 1] end file = site_spec.content_dir.file(request_path) if (file.extname == '.html') file = check_source(file) end return FileProcessor.new(template, file, site_spec) end def FileProcessor::check_source(file) result = file basename = file.basename dir = file.parent ['textile', 'markdown', 'html'].find do |extension| candidate = dir.file("#{basename}.#{extension}") if (candidate.exists?) result = candidate true else false end end return result end def is_html? return target_file.extname == '.html' end def write_to_target document = generate_document if (document) target_file.write do |file| document.write(file, 0, false, true) end else content_file.copy_to(target_file) end end def generate_document send(@convert_method, load_content()) if (@convert_method) end def load_content return content_file.load 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