lib/buildmaster/file_processor.rb in BuildMaster-0.8.1 vs lib/buildmaster/file_processor.rb in BuildMaster-0.9.0
- old
+ new
@@ -16,81 +16,82 @@
result = File.join(result, name)
end
return result
end
- attr_reader :path_to_content_file, :path_to_target_file
+ attr_reader :content_file, :target_file
- def initialize(template, path_to_content_file, sitespec)
+ def initialize(template, content_file, sitespec)
@template = template
- @path_to_content_file = path_to_content_file
+ @content_file = content_file
@sitespec = sitespec
- extension = File.extname(path_to_content_file)
+ 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 = 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)
+ basename = content_file.basename
+ @path = sitespec.relative_to_root(content_file).parent.join("#{basename}.html")
+ @target_file = sitespec.output_dir.file(@path)
else
- @path_to_target_file = FileProcessor.join(sitespec.output_dir, sitespec.relative_to_root(path_to_content_file))
+ @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
- 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
+ if (request_path[0,1] == '/')
+ request_path = request_path[1, request_path.length - 1]
end
- if (not path_to_content)
- path_to_content = join(site_spec.content_dir, relative_to_root)
+ file = site_spec.content_dir.file(request_path)
+ if (file.extname == '.html')
+ file = check_source(file)
end
- return FileProcessor.new(template, path_to_content, site_spec)
+ 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 File.extname(path_to_target_file) == '.html'
+ return target_file.extname == '.html'
end
def write_to_target
document = generate_document
if (document)
- File.open(path_to_target_file, 'w') do |file|
+ target_file.write do |file|
document.write(file, 0, false, true)
end
else
- FileUtils.cp path_to_content_file, path_to_target_file
+ content_file.copy_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)
+ 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}
\ No newline at end of file