kinbote 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,4 +10,5 @@ if File.exists?(name)
10
10
  exit 1
11
11
  end
12
12
 
13
- FileUtils.copy_entry("#{File.dirname(__FILE__)}/../site", name)
13
+ FileUtils.copy_entry("#{File.dirname(__FILE__)}/../site", name)
14
+ FileUtils.mkdir_p("#{name}/public/css")
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'kinbote'
6
- s.version = '0.0.1'
7
- s.date = '2010-07-10'
6
+ s.version = '0.0.2'
7
+ s.date = '2010-07-11'
8
8
 
9
9
  s.description = "Kinbote"
10
10
  s.summary = "Kinbote"
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  lib/kinbote.rb
24
24
  lib/kinbote/attribute.rb
25
25
  lib/kinbote/base.rb
26
+ lib/kinbote/kinfile.rb
26
27
  lib/kinbote/page.rb
27
28
  lib/kinbote/page_match.rb
28
29
  lib/kinbote/site.rb
@@ -40,6 +41,7 @@ Gem::Specification.new do |s|
40
41
  site/.kinbote/views/examples/related_any.haml
41
42
  site/.kinbote/views/examples/specific.haml
42
43
  site/.kinbote/views/layout.haml
44
+ site/.kinbote/views/messages.haml
43
45
  site/.kinbote/views/page.haml
44
46
  site/.kinbote/views/pages.haml
45
47
  site/.kinbote/views/site.haml
@@ -1,71 +1,43 @@
1
1
  module Kinbote
2
2
  class Attribute
3
3
  include Util
4
-
5
- attr_accessor :name, :values, :_varname
6
-
7
- def initialize(name, page, values, global_attribute = nil)
4
+ include Kinfile
5
+ attr_reader :name
6
+
7
+ def initialize(name, page, values, global=nil)
8
8
  @name = name.to_s
9
9
  @pages = []
10
10
  @values = []
11
- @global_attribute = global_attribute || self
11
+ @global = global || self
12
12
  add_page(page, values)
13
- @_varname = slugify(@name).gsub("-", "_")
14
- @css = File.exist?(file_name(:css))
15
13
  end
16
-
14
+
17
15
  def add_page(page, values)
18
16
  @pages.delete_if{|p| p.slug == page.slug}
19
17
  @pages << page
20
18
  values.each do |v|
21
- g_val = $site.find_value(v, @global_attribute, true)
22
- if g_val
23
- g_val.add_page(page)
24
- else
25
- g_val = Value.new(v, page, @global_attribute)
26
- $site.add_value(g_val)
27
- end
19
+ g_val = $site.find_value(v, @global, true)
20
+ g_val ? g_val.add_page(page) : g_val = Value.new(v, page, @global); $site.add_value(g_val)
28
21
  l_val = $site.find_value(v, self)
29
- if l_val
30
- l_val.add_page(page)
31
- else
32
- @values << Value.new(v, page, @global_attribute, g_val)
33
- end
22
+ l_val ? l_val.add_page(page) : @values << Value.new(v, page, @global, g_val)
34
23
  end
35
24
  end
36
-
25
+
37
26
  def remove_page(page)
38
27
  @values.map{|value| value.remove_page(page)}
39
- @global_attribute.pages.delete_if{|p| p.slug == page.slug}
28
+ @global.pages.delete_if{|p| p.slug == page.slug}
40
29
  @pages.delete_if{|p| p.slug == page.slug}
41
- @global_attribute.values.delete_if{|v| v.pages.size == 0}
30
+ @global.values.delete_if{|v| v.pages.size == 0}
42
31
  @values.delete_if{|v| v.pages.size == 0}
43
32
  end
44
-
45
- def create_css
46
- FileUtils.mkdir_p(file_name(:css, true)) if !File.exist?(file_name(:css, true))
47
- File.open(file_name(:css), "w") if !File.exist?(file_name(:css))
48
- @global_attribute.css!
33
+
34
+ def values
35
+ @values.sort{|x, y| x.value <=> y.value }
49
36
  end
50
-
51
- def file_name(type, dir = nil)
52
- if type == :css
53
- return "#{$site.kinbote_path}/views/sass/attributes/#{@name}/#{!dir ? "_all.sass" : ""}"
54
- end
55
- end
56
-
57
- def css_files
58
- files = (@global_attribute.css? ? ["/css/attributes/#{@name}/_all"] : [])
59
- @values.each do |value|
60
- files += value.css_files
61
- end
62
- files
63
- end
64
-
65
- def pages; @global_attribute == self ? @pages : @global_attribute.pages; end
37
+
38
+ def varname; varify(@name); end
39
+ def pages; @global == self ? @pages : @global.pages; end
66
40
  def to_s; @name; end
67
- def css!; @global_attribute == self ? @css = true : @global_attribute.css!; end
68
- def css?; @global_attribute == self ? @css : @global_attribute.css?; end
69
-
41
+
70
42
  end
71
43
  end
@@ -8,6 +8,7 @@ require 'net/ssh'
8
8
  require 'net/scp'
9
9
 
10
10
  require 'kinbote/util'
11
+ require 'kinbote/kinfile'
11
12
  require 'kinbote/site'
12
13
  require 'kinbote/page'
13
14
  require 'kinbote/attribute'
@@ -15,5 +16,5 @@ require 'kinbote/value'
15
16
  require 'kinbote/page_match'
16
17
 
17
18
  module Kinbote
18
- VERSION = '0.0.1'
19
+ VERSION = '0.0.2'
19
20
  end
@@ -0,0 +1,71 @@
1
+ module Kinbote
2
+ module Kinfile
3
+
4
+ def init_files
5
+ @file_sizes = {}
6
+ end
7
+
8
+ def files(type)
9
+ files = []
10
+ if self.is_a?(Attribute)
11
+ files << "/css/attributes/#{@name}/_all" if has_file?(:css)
12
+ @values.each{|v| files += v.files(:css)}
13
+ elsif self.is_a?(Value)
14
+ files << "/css/attributes/#{@attribute}/#{@value}" if has_file?(:css)
15
+ elsif self.is_a?(Page)
16
+ files << "/css/pages/#{@slug}" if has_file?(:css)
17
+ @attributes.each{|a| files += a.files(type) }
18
+ end
19
+ files
20
+ end
21
+
22
+ def create_files(types)
23
+ types.each {|type| create_file(type) }
24
+ end
25
+
26
+ def create_file(type)
27
+ FileUtils.mkdir_p(file_path(type, true)) if !File.exist?(file_path(type, true))
28
+ File.open(file_path(type), "w") if !File.exist?(file_path(type))
29
+ end
30
+
31
+ def delete_files(types)
32
+ types.each {|type| delete_file(type) }
33
+ end
34
+
35
+ def delete_file(type)
36
+ FileUtils.rm_f(file_path(type))
37
+ end
38
+
39
+ def file_size(type)
40
+ @file_sizes[type] = File.size(file_path(type)) if !@file_sizes.has_key?(type)
41
+ @file_sizes[type]
42
+ end
43
+
44
+ def reset_file_size(type)
45
+ @file_sizes[type] = File.size(file_path(type))
46
+ end
47
+
48
+ def has_file?(type)
49
+ File.exist?(file_path(type))
50
+ end
51
+
52
+ private
53
+
54
+ def file_path(type, dir = nil)
55
+ if type == :css
56
+ if self.is_a?(Attribute)
57
+ return "#{$site.kinbote_path}/views/sass/attributes/#{@name}/#{!dir ? "_all.sass" : ""}"
58
+ elsif self.is_a?(Value)
59
+ return "#{$site.kinbote_path}/views/sass/attributes/#{@attribute}/#{!dir ? "#{@value}.sass" : ""}"
60
+ elsif self.is_a?(Page)
61
+ return "#{@directory}#{!dir ? "#{@slug}.sass" : ""}"
62
+ end
63
+ elsif type == :html
64
+ return @file if @file && !dir
65
+ return @directory if @directory && dir
66
+ return "#{$site.kinbote_path}/views/pages/#{Time.now.strftime("%Y/%m")}/#{!dir ? "/#{slugify(@title)}.haml" : ""}"
67
+ end
68
+ end
69
+
70
+ end
71
+ end
@@ -1,22 +1,26 @@
1
1
  module Kinbote
2
2
  class Page
3
3
  include Util
4
-
5
- attr_accessor :title, :attributes, :slug
6
-
4
+ include Kinfile
5
+ attr_reader :title, :slug
6
+
7
+ BAD_ATTRIBUTE_VARNAMES = ["site", "pages", "attributes"]
8
+
7
9
  def initialize(title, file=nil, attributes=nil)
10
+ init_files
8
11
  @title = title
9
12
  @directory = (file ? dir_from_file(file) : file_path(:html, true))
10
13
  @file = file || file_path(:html)
11
14
  @slug = slug_from_file(@file)
12
15
  @attributes = []
13
16
  add_attributes(attributes || {"Date" => Time.now.strftime("%B %d, %Y")})
14
- create_html
15
- @css = File.exist?(file_path(:css))
17
+ create_file(:html)
18
+ Page.add_metadata(self) if !file
16
19
  set_ivars
17
20
  end
18
21
 
19
- def set_attributes(attributes)
22
+ def set_attributes(attributes, values=nil)
23
+ attributes = kv_to_attributes(attributes, values) if values
20
24
  new_title = remove_title(attributes)
21
25
  return if same_attributes(attributes, new_title)
22
26
  @title = new_title if new_title && new_title.size > 0
@@ -25,18 +29,16 @@ module Kinbote
25
29
  @attributes.delete_if{|att| att.pages.size == 0}
26
30
  @attributes = []
27
31
  add_attributes(attributes)
28
- $site.add_kinbote_haml(self)
32
+ Page.add_metadata(self)
29
33
  set_ivars
30
34
  end
31
-
35
+
32
36
  def loosely_related_pages(attribute_names = nil)
33
37
  page_matches = []
34
38
  if !attribute_names
35
39
  page_matches += loosely_related_pages(@attributes.map{|a| a.name})
36
40
  elsif attribute_names.is_a?(Array)
37
- attribute_names.each do |a_name|
38
- page_matches += lrp(a_name)
39
- end
41
+ attribute_names.each{ |a_name| page_matches += lrp(a_name) }
40
42
  else
41
43
  page_matches += lrp(attribute_names)
42
44
  end
@@ -48,30 +50,23 @@ module Kinbote
48
50
  if !attribute_names
49
51
  page_matches += strongly_related_pages(@attributes.map{|a| a.name})
50
52
  elsif attribute_names.is_a?(Array)
51
- attribute_names.each do |a_name|
52
- page_matches += srp(a_name, values)
53
- end
53
+ attribute_names.each{ |a_name| page_matches += srp(a_name, values) }
54
54
  else
55
55
  page_matches += srp(attribute_names, values)
56
56
  end
57
57
  page_matches.uniq
58
58
  end
59
59
 
60
+ def attributes
61
+ sorted_attributes(@attributes)
62
+ end
63
+
60
64
  def values
61
65
  vals = []
62
- @attributes.each do |a|
63
- a.values.each do |v|
64
- vals << v
65
- end
66
- end
66
+ attributes.each{ |a| a.values.each{ |v| vals << v } }
67
67
  vals
68
68
  end
69
69
 
70
- def delete_files
71
- FileUtils.rm_f(file_path(:css))
72
- FileUtils.rm_f(file_path(:html))
73
- end
74
-
75
70
  def path
76
71
  path = []
77
72
  in_path = false
@@ -82,39 +77,71 @@ module Kinbote
82
77
  "#{path.join("/")}/#{@slug}"
83
78
  end
84
79
 
85
- def create_css
86
- FileUtils.mkdir_p(file_path(:css, true)) if !File.exist?(file_path(:css, true))
87
- File.open(file_path(:css), "w") if !File.exist?(file_path(:css))
88
- @css = true
80
+ def to_s; @title; end
81
+ def url; "#{@slug}.html"; end
82
+
83
+ def self.add_metadata(page = nil)
84
+ fname = "#{$site.kinbote_path}/views/#{page ? page.path : "index"}"
85
+ f_new = File.open("#{fname}.haml.new", "w")
86
+ File.read("#{fname}.haml").to_a.each do |line|
87
+ break if line.include?("-# Kinbote")
88
+ f_new.puts line
89
+ end
90
+ if page
91
+ kinbote_haml(f_new, "page_attributes_title")
92
+ kinbote_haml(f_new, "page_attributes", ["Title: #{page.title}"] + page.values.map{|v| "#{v.attribute.name}: #{v.value}"})
93
+ kinbote_haml(f_new, "header")
94
+ kinbote_haml(f_new, "specific", page.attributes.map{|a| a.varname})
95
+ kinbote_haml(f_new, "related", page.attributes.map{|a| a.name.downcase == "date" ? nil : a.name}.compact)
96
+ kinbote_haml(f_new, "related2", page.attributes.map{|a| "#{a.name}\", \"#{a.values.first.value}"})
97
+ kinbote_haml(f_new, "related_any")
98
+ else
99
+ kinbote_haml(f_new, "home", $site.attributes.map{|a| a.varname})
100
+ end
101
+ f_new.close
102
+ FileUtils.mv("#{fname}.haml.new", "#{fname}.haml")
103
+ page.reset_file_size(:html) if page
104
+ Page.add_metadata if page
89
105
  end
90
-
91
- def css_files
92
- files = (css? ? ["/css/pages/#{@slug}"] : [])
93
- @attributes.each do |attribute|
94
- files += attribute.css_files
106
+
107
+ def self.kinbote_haml(f, example_name, replacements=nil)
108
+ return if replacements && replacements.size == 0 && example_name != "page_attributes"
109
+ File.read("#{$site.kinbote_path}/.kinbote/views/examples/#{example_name}.haml").to_a.each do |line|
110
+ if replacements && line.include?("$ALL_VALS$")
111
+ replacements.each do |r|
112
+ f.puts(line.gsub("$ALL_VALS$", r))
113
+ end
114
+ elsif replacements && line.include?("$ALL_VALS_COMMENTED$")
115
+ replacements.each_with_index do |r, i|
116
+ if i == replacements.size - 1
117
+ f.puts(line.gsub("$ALL_VALS_COMMENTED$", r))
118
+ else
119
+ f.puts(line.gsub("- ", "-# ").gsub("$ALL_VALS_COMMENTED$", r))
120
+ end
121
+ end
122
+ elsif replacements && line.include?("$FIRST_VAL$")
123
+ f.puts(line.gsub("$FIRST_VAL$", replacements.first))
124
+ elsif replacements && line.include?("$LAST_VAL$")
125
+ f.puts(line.gsub("$LAST_VAL$", replacements.last))
126
+ else
127
+ f.puts(line)
128
+ end
95
129
  end
96
- files
130
+ f.puts ""
97
131
  end
98
132
 
99
- def file_path(type, dir = nil)
100
- if type == :html
101
- return @file if @file && !dir
102
- return @directory if @directory && dir
103
- return "#{$site.kinbote_path}/views/pages/#{Time.now.strftime("%Y/%m")}/#{!dir ? "/#{slugify(@title)}.haml" : ""}"
104
- elsif type == :css
105
- return "#{@directory}#{!dir ? "#{@slug}.sass" : ""}"
133
+ private
134
+
135
+ def kv_to_attributes(keys, values)
136
+ attributes = {}
137
+ keys.each_with_index do |key, i|
138
+ next if key.size == 0
139
+ attributes[key] = [] if !attributes.has_key?(key)
140
+ attributes[key] << values[i] if !attributes[key].include?(values[i])
106
141
  end
142
+ attributes
107
143
  end
108
144
 
109
- def set_filesize; @filesize = File.size(file_path(:html)); end
110
- def get_filesize; @filesize; end
111
- def to_s; @title; end
112
- def css!; @css = true; end
113
- def css?; @css; end
114
- def url; "#{@slug}.html"; end
115
-
116
- private
117
-
118
145
  def remove_title(attributes)
119
146
  new_title = nil
120
147
  title_keys = []
@@ -128,18 +155,10 @@ module Kinbote
128
155
  new_title.first
129
156
  end
130
157
 
131
- def create_html
132
- FileUtils.mkdir_p(file_path(:html, true)) if !File.exist?(file_path(:html, true))
133
- if !File.exist?(file_path(:html))
134
- File.open(file_path(:html), "w")
135
- $site.add_kinbote_haml(self)
136
- end
137
- end
138
-
139
158
  def set_ivars
140
159
  @attributes.each do |att|
141
- instance_variable_set("@#{att._varname}", att.values)
142
- Page.send(:define_method, att._varname.to_sym, Proc.new{ eval("@#{att._varname}") }) if !Page.respond_to?(att._varname)
160
+ instance_variable_set("@#{att.varname}", att.values)
161
+ Page.send(:define_method, att.varname.to_sym, Proc.new{ eval("@#{att.varname}") }) if !Page.respond_to?(att.varname)
143
162
  end
144
163
  end
145
164
 
@@ -150,19 +169,14 @@ module Kinbote
150
169
 
151
170
  def add_attributes(attributes)
152
171
  attributes.each do |name, values|
153
- g_att = $site.find_attribute(name)
154
- if g_att
155
- g_att.add_page(self, values)
156
- else
157
- g_att = Attribute.new(name, self, values)
158
- $site.add_attribute(g_att)
172
+ if BAD_ATTRIBUTE_VARNAMES.include?(varify(name))
173
+ $site.add_message("#{name} is an invalid attribute name")
174
+ next
159
175
  end
176
+ g_att = $site.find_attribute(name)
177
+ g_att ? g_att.add_page(self, values) : g_att = Attribute.new(name, self, values); $site.add_attribute(g_att)
160
178
  l_att = $site.find_attribute(name, self)
161
- if l_att
162
- l_att.add_page(self, values)
163
- else
164
- @attributes << Attribute.new(name, self, values, g_att)
165
- end
179
+ l_att ? l_att.add_page(self, values) : @attributes << Attribute.new(name, self, values, g_att)
166
180
  end
167
181
  end
168
182
 
@@ -1,8 +1,8 @@
1
1
  module Kinbote
2
2
  class PageMatch
3
- attr_accessor :page, :attribute, :value
3
+ attr_reader :page, :attribute, :value
4
4
 
5
- def initialize(page, attribute, value = nil)
5
+ def initialize(page, attribute, value=nil)
6
6
  @page = page
7
7
  @attribute = attribute
8
8
  @value = value