ace 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/bin/ace CHANGED
@@ -43,13 +43,11 @@ rules.rules.each do |klass, files|
43
43
  if File.binread(file).match(/^-{3,5}\s*$/) # TODO: this should be a filter or lazy-loaded
44
44
  puts "~ Read #{file} with parse"
45
45
  raw_item = Ace::RawItem.new(file).tap(&:parse)
46
- raw_item.check_metadata_created_at(file)
47
- item = klass.create(raw_item.metadata, raw_item.content)
46
+ item = klass.create(raw_item.metadata, raw_item.content, file)
48
47
  else
49
48
  puts "~ Read #{file} without parse"
50
- item = klass.create(Hash.new, File.read(file))
49
+ item = klass.create(Hash.new, File.read(file), file)
51
50
  end
52
- item.original_path = file
53
51
  end
54
52
  end
55
53
 
data/lib/ace.rb CHANGED
@@ -21,28 +21,29 @@ module Ace
21
21
  class RawItem
22
22
  attr_accessor :path, :metadata, :content
23
23
  def initialize(path)
24
+ @path = path
24
25
  @data = File.read(path)
25
26
  end
26
27
 
27
- def check_metadata_created_at(path)
28
- if self.metadata[:title]
29
- year, month, day = File.basename(path).slice(0,10).split('-')
30
- self.metadata[:created_at] ||= Date.new(year.to_i, month.to_i, day.to_i)
31
- end
32
- end
33
-
34
28
  def parse
35
29
  pieces = @data.split(/^-{3,5}\s*$/)
36
- # if pieces.size < 3
37
- # raise RuntimeError.new(
38
- # "The file '#{path}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format."
39
- # )
40
- # end
30
+ if pieces.length == 1 || @data.empty?
31
+ self.metadata = Hash.new
32
+ self.content = pieces.first
33
+ else
34
+ self.metadata = begin
35
+ YAML.load(pieces[1]).inject(Hash.new) { |metadata, pair| metadata.merge(pair[0].to_sym => pair[1]) } || Hash.new
36
+ end
37
+ self.content = pieces[2..-1].join.strip
38
+ end
39
+
40
+ set_timestamps_in_metadata
41
+ end
41
42
 
42
- # Parse
43
- self.metadata = YAML.load(pieces[1]).inject(Hash.new) { |metadata, pair| metadata.merge(pair[0].to_sym => pair[1]) } || Hash.new
44
- # TODO: check metadata[:created_at] and supply it from filename
45
- self.content = pieces[2..-1].join.strip
43
+ private
44
+ def set_timestamps_in_metadata
45
+ self.metadata[:created_at] ||= File.ctime(self.path)
46
+ self.metadata[:updated_at] ||= File.mtime(self.path)
46
47
  end
47
48
  end
48
49
 
@@ -93,16 +94,17 @@ module Ace
93
94
  self.after_filters << filter.new(*args)
94
95
  end
95
96
 
96
- def self.create(metadata, content)
97
- self.new(metadata, content).tap(&:register)
97
+ def self.create(*args)
98
+ self.new(*args).tap(&:register)
98
99
  end
99
100
 
100
101
  # Content can be anything, not just a string.
101
102
  attr_accessor :metadata, :content
102
103
  attr_accessor :original_path
103
- def initialize(metadata, content)
104
- @metadata = metadata
105
- @content = content
104
+ def initialize(metadata, content, original_path)
105
+ @metadata = metadata
106
+ @content = content
107
+ @original_path = original_path
106
108
  end
107
109
 
108
110
  def config
@@ -3,9 +3,9 @@
3
3
  require "ace/filters"
4
4
  require "nokogiri"
5
5
 
6
- # <thumbnail src="assets/img/motivation-sheet.jpg" />
7
- # <thumbnail src="assets/img/motivation-sheet.jpg" size="550" />
8
- # <thumbnail src="assets/img/motivation-sheet.jpg" size="550x20" />
6
+ # <thumbnail src="/assets/img/motivation-sheet.jpg" />
7
+ # <thumbnail src="/assets/img/motivation-sheet.jpg" size="550" />
8
+ # <thumbnail src="/assets/img/motivation-sheet.jpg" size="550x20" />
9
9
 
10
10
  # TODO:
11
11
  # class Post < Ace::Item
@@ -14,24 +14,15 @@ require "nokogiri"
14
14
 
15
15
  module Ace
16
16
  class ImageThumbnailerFilter < Filter
17
- def thumb_path(file_name)
18
- @file_name ||= file_name
19
- @thumb_path ||= file_name.gsub(/content\/(.+)\.([^\.]*)$/, 'output/\1_thumb.\2')
17
+ def to_thumb(path)
18
+ path.to_s.sub(/\.(\w+)$/, '_thumb.\1')
20
19
  end
21
20
 
22
- def thumb_server_path
23
- @thumb_path.sub("content", "")
24
- end
25
-
26
- def original_image_server_path
27
- @file_name.sub("content", "")
28
- end
29
-
30
- def thumbnail_nodeset(file_name, doc)
21
+ def thumbnail_nodeset(link, doc)
31
22
  link = Nokogiri::XML::Node.new("a", doc)
32
23
  image = Nokogiri::XML::Node.new("img", doc)
33
- link.set_attribute("href", original_image_server_path)
34
- image.set_attribute("src", thumb_server_path)
24
+ link.set_attribute("href", link)
25
+ image.set_attribute("src", to_thumb(link))
35
26
  image.parent = link
36
27
  return link
37
28
  end
@@ -40,22 +31,23 @@ module Ace
40
31
  puts "~ [THUMB] #{item.original_path}"
41
32
  doc = Nokogiri::HTML(content)
42
33
  doc.css("thumbnail").each do |thumb|
43
- original_file = "content/#{thumb[:src]}"
44
- generate_thumbnail(original_file, thumb[:size] || 550)
45
- thumb.replace(thumbnail_nodeset(original_file, doc))
34
+ original_image_path = "content" + thumb[:src]
35
+ thumbnail_path = to_thumb("output" + thumb[:src])
36
+ generate_thumbnail(original_image_path, thumbnail_path, thumb[:src], thumb[:size] || 550)
37
+ thumb.replace(thumbnail_nodeset(thumb[:src], doc))
46
38
  end
47
39
  doc.to_s
48
40
  end
49
41
 
50
42
  private
51
- def generate_thumbnail(file_name, size)
52
- unless File.exist?(thumb_path(file_name))
53
- command = "convert #{file_name} -resize #{size} #{thumb_path(file_name)}"
43
+ def generate_thumbnail(original_path, thumbnail_path, link, size)
44
+ unless File.exist?(thumbnail_path)
45
+ command = "convert #{original_path} -resize #{size} #{thumbnail_path}"
54
46
  warn "~ $ #{command}"
55
47
  system(command)
56
- raise "Error when converting image '#{file_name}'" if $?.to_i != 0
48
+ raise "Error when converting image '#{original_path}'" if $?.to_i != 0
57
49
  else
58
- warn "~ File #{thumb_path(file_name)} already exists."
50
+ warn "~ File #{thumbnail_path} already exists."
59
51
  end
60
52
  end
61
53
  end
data/lib/ace/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Ace
4
- VERSION = "0.4.4"
4
+ VERSION ||= "0.4.5"
5
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ace
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.4
5
+ version: 0.4.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-10 00:00:00 +01:00
13
+ date: 2011-08-16 00:00:00 -07:00
14
14
  default_executable: ace
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency