Skip to content
This repository has been archived by the owner on Feb 2, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
cosmetics
  • Loading branch information
sr committed Nov 5, 2008
1 parent 89a6f2a commit 7c26136
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions statig.rb
@@ -1,6 +1,4 @@
%w(rubygems
thor
yaml).each { |lib| require lib }
require 'yaml'

class SerializableProc
def initialize(block)
Expand All @@ -22,11 +20,13 @@ class Statig < Thor
method_options :force => :boolean, :config => :optional
def build(directory=Dir.pwd)
directory = File.expand_path(directory)

Dir.chdir(directory) do
abort('needs to be run inside of a git working directory') unless git?

files_list.each do |source|
destination = source.sub(File.extname(source), '.html')
next if !need_update?(source, destination) && !options[:force]
next if !needs_update?(source, destination) && !options[:force]
puts "#{source} -> #{destination}"
File.open(destination, 'w') do |file|
file << content_for(source, destination)
Expand All @@ -37,8 +37,12 @@ def build(directory=Dir.pwd)
end

private
def needs_update?(source, destination)
!File.exists?(destination) || File.mtime(source) > File.mtime(destination)
end

def content_for(source, destination)
parsed = parse_email_headers(File.read(source))
parsed = parse_meta_data(File.read(source))

if template?
template(parsed)
Expand All @@ -51,20 +55,16 @@ def formatter_for(file_name)
config[File.extname(file_name)[1..-1].to_sym]
end

def need_update?(source, destination)
!File.exists?(destination) || File.mtime(source) > File.mtime(destination)
end

def files_list
@files ||= Dir[glob].reject { |file| file =~ excludes }
Dir[glob].reject { |file| file =~ excludes }
end

def glob
@glob ||= "#{'**/' * config[:deepth]}*.{#{config[:extensions].map(&:to_s).join(',')}}"
"#{'**/' * config[:deepth]}*.{#{config[:extensions].map(&:to_s).join(',')}}"
end

def excludes
@excludes ||= Regexp.union(*(config[:excludes] || []))
Regexp.union(*(config[:excludes] || []))
end

def template(variables)
Expand All @@ -76,47 +76,50 @@ def template?
end

def load_template_file
@template_content ||= File.exists?(config[:template]) ? File.read(config[:template]) : nil
@template_content ||= begin
File.read(config[:template])
rescue Errno::ENOENT
nil
end
end

def config
@config = begin
YAML.load_file(options[:config] || 'statig.yml')
rescue Errno::ENOENT
{}
abort "Couldn't load configuration file"
end
end

def git_ignore_if_needed(file)
unless git_ignored?(file)
puts "Ignoring #{file}"
File.open('.gitignore', 'a') { |f| f << "#{file}\n" }
def parse_meta_data(string)
parsed = {:meta_data => [], :content => string}
return parsed unless string =~ /((\w[\w\s]+: .*\n)+)\n/

parsed.update(:content => $')
meta_data = $1.split("\n").inject({}) do |meta_data, line|
parts = line.split(':', 2)
key = parts.first.strip.downcase.to_sym
value = parts.last.strip
meta_data.update(key => value)
end
end

def git_ignored?(file)
return false unless File.exists?('.gitignore')
File.readlines('.gitignore').map(&:chomp).include?(file)
parsed.update(:meta_data => meta_data)
end

def git?
`git status &> /dev/null`
$?.exitstatus.to_i == 1
end

def parse_email_headers(string)
parsed = {:headers => [], :content => string}
return parsed unless string =~ /((\w[\w\s]+: .*\n)+)\n/

parsed.update(:content => $')

headers = $1.split("\n").inject({}) do |headers, line|
parts = line.split(':', 2)
key = parts.first.strip.downcase.to_sym
value = parts.last.strip
headers.update(key => value)
def git_ignore_if_needed(file)
unless git_ignored?(file)
puts "Ignoring #{file}"
File.open('.gitignore', 'a') { |f| f << "#{file}\n" }
end
end

parsed.update(:headers => headers)
def git_ignored?(file)
return false unless File.exists?('.gitignore')
File.readlines('.gitignore').map(&:chomp).include?(file)
end
end

0 comments on commit 7c26136

Please sign in to comment.