lib/buildmaster/file_processor.rb in BuildMaster-0.7.0 vs lib/buildmaster/file_processor.rb in BuildMaster-0.8.0
- old
+ new
@@ -1,48 +1,137 @@
$:.unshift File.dirname(__FILE__)
+require 'site_spec'
+require 'source_content'
+require 'pathname'
+
module BuildMaster
class FileProcessor
- def initialize(template, content_path, evaluator)
+#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
- @content_path = content_path
- @evaluator = evaluator
+ @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_textile()
- textile_content = IO.read(@content_path)
- match_result = TEXTILE_REGX.match(textile_content)
+ def process_content_with_title(full_content)
+ match_result = @@TEXTILE_REGX.match(full_content)
title = ''
- if match_result != nil
- title = match_result[2]
- textile_content = match_result.post_match
- end
- html_body = RedCloth.new(textile_content).to_html
- html_content = <<HTML
-<!DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ 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 = <<HTML
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>#{title}</title>
</head>
<body>
#{html_body}
</body>
</html>
HTML
- return process_html_content(html_content)
- end
+ 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
- def process_html()
- return process_html_content(File.open(@content_path))
- end
-
- def process_html_content(source)
- document_with_skin = @template.process(source) {|message| @evaluator.evaluate(message, @content_path)}
- return document_with_skin
- end
-
end
end
\ No newline at end of file