kinbote 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,9 @@
1
1
  module Kinbote
2
2
  class Site
3
3
  include Util
4
-
5
- attr_reader :pages, :attributes, :kinbote_path, :config
4
+ attr_reader :kinbote_path, :config
5
+
6
+ BAD_PAGE_SLUGS = ["kinbote"]
6
7
 
7
8
  def initialize
8
9
  @pages = []
@@ -10,31 +11,87 @@ module Kinbote
10
11
  @values = []
11
12
  @kinbote_path = nil
12
13
  @config = {}
14
+ @messages = []
13
15
  end
14
16
 
15
- def set_kinbote_path(path)
16
- return if @kinbote_path
17
- @kinbote_path = path
17
+ def load
18
+ load_pages
18
19
  end
19
20
 
20
- def read_config
21
- if File.exist?("#{@kinbote_path}/config/site.yml")
22
- @config = YAML::load_file("#{@kinbote_path}/config/site.yml")
23
- else
24
- @config = {"title" => "Kinbote Site"}
21
+ def reload
22
+ read_config
23
+ reload_pages
24
+ end
25
+
26
+ def find_page(slug)
27
+ @pages.each do |page|
28
+ return page if page.slug == slug
25
29
  end
26
- @config.each do |key, value|
27
- Site.send(:define_method, slugify(key).gsub("-", "_").to_sym, Proc.new{ @config[key] }) if !Site.respond_to?(slugify(key).gsub("-", "_").to_sym)
30
+ nil
31
+ end
32
+
33
+ def find_attribute(name, page = nil)
34
+ (page ? page.attributes : @attributes).each do |attribute|
35
+ return attribute if (attribute.name == name || attribute.varname == varify(name))
36
+ end
37
+ nil
38
+ end
39
+
40
+ def find_value(val, attribute, global = false)
41
+ (global ? @values : attribute.values).each do |value|
42
+ return value if (value.value == val && (attribute.name == value.attribute.name || attribute.varname == varify(value.attribute.name)))
43
+ end
44
+ nil
45
+ end
46
+
47
+ def create_page(title, file=nil, attributes=nil)
48
+ if BAD_PAGE_SLUGS.include?(slugify(title))
49
+ add_message("#{title} #{file ? "(#{file})" : ""} is an invalid page title")
50
+ return false
28
51
  end
52
+ @pages << Page.new(title, file, attributes)
29
53
  end
30
54
 
55
+ def delete_page(slug)
56
+ page = find_page(slug)
57
+ @attributes.map{|att| att.remove_page(page)}
58
+ @attributes.delete_if{|att| att.pages.size == 0}
59
+ page.delete_files([:html, :css])
60
+ @pages.delete_if{|p| p.slug == page.slug}
61
+ Page.add_metadata
62
+ end
63
+
64
+ def remove_page_attributes(page)
65
+ @attributes.map{|att| att.remove_page(page)}
66
+ @attributes.delete_if{|att| att.pages.size == 0}
67
+ end
68
+
69
+ def pages
70
+ @pages.sort{|x, y| x.title <=> y.title }
71
+ end
72
+
73
+ def attributes
74
+ sorted_attributes(@attributes)
75
+ end
76
+
77
+ def kinbote_path= (path); @kinbote_path = path; end
78
+
79
+ def add_attribute(attribute); @attributes << attribute; end
80
+ def add_value(value); @values << value; end
81
+
82
+ def add_message(message); @messages << message if !@messages.include?(message); end
83
+ def has_messages?; @messages.size > 0; end
84
+ def render_messages; m = Array.new(@messages); @messages = []; m; end
85
+
86
+ private
87
+
31
88
  def load_pages
32
89
  Dir.glob("#{@kinbote_path}/views/pages/**/*.{haml}").each do |file|
33
90
  title, attributes = file_attributes(file)
34
- @pages << Page.new(title, file, attributes)
91
+ create_page(title, file, attributes)
35
92
  end
36
93
  end
37
-
94
+
38
95
  def reload_pages
39
96
  files = Dir.glob("#{@kinbote_path}/views/pages/**/*.{haml}")
40
97
  files.each do |file|
@@ -42,17 +99,17 @@ module Kinbote
42
99
  title, attributes = file_attributes(file)
43
100
  page = find_page(slug)
44
101
  if page
45
- next if File.size(file) == page.get_filesize
102
+ next if File.size(file) == page.file_size(:html)
46
103
  page.set_attributes(attributes.merge({"title" => [title]}))
47
- page.set_filesize
104
+ page.reset_file_size(:html)
48
105
  else
49
- @pages << Page.new(title, file, attributes)
106
+ create_page(title, file, attributes)
50
107
  end
51
108
  end
52
109
  slugs = files.map{|file| slug_from_file(file)}
53
110
  @pages.map{|page| delete_page(page.slug) if !slugs.include?(page.slug)}
54
111
  end
55
-
112
+
56
113
  def file_attributes(file)
57
114
  title = slug_from_file(file)
58
115
  attributes = {}
@@ -73,102 +130,12 @@ module Kinbote
73
130
  end
74
131
  [title, attributes]
75
132
  end
76
-
77
- def find_page(slug)
78
- @pages.each do |page|
79
- return page if page.slug == slug
80
- end
81
- nil
82
- end
83
-
84
- def find_attribute(name, page = nil)
85
- (page ? page.attributes : @attributes).each do |attribute|
86
- return attribute if (attribute.name == name || attribute._varname == name)
87
- end
88
- nil
89
- end
90
-
91
- def find_value(val, attribute, global = false)
92
- (global ? @values : attribute.values).each do |value|
93
- return value if (value.value == val && attribute.name == value.attribute.name)
94
- end
95
- nil
96
- end
97
-
98
- def create_page(title)
99
- @pages << Page.new(title)
100
- end
101
-
102
- def add_attribute(attribute)
103
- @attributes << attribute
104
- end
105
-
106
- def add_value(value)
107
- @values << value
108
- end
109
133
 
110
- def remove_page_attributes(page)
111
- @attributes.map{|att| att.remove_page(page)}
112
- @attributes.delete_if{|att| att.pages.size == 0}
113
- end
114
-
115
- def delete_page(slug)
116
- page = find_page(slug)
117
- @attributes.map{|att| att.remove_page(page)}
118
- @attributes.delete_if{|att| att.pages.size == 0}
119
- page.delete_files
120
- @pages.delete_if{|p| p.slug == page.slug}
121
- add_kinbote_haml
122
- end
123
-
124
- def add_kinbote_haml(page = nil)
125
- fname = "#{@kinbote_path}/views/#{page ? page.path : "index"}"
126
- f_new = File.open("#{fname}.haml.new", "w")
127
- File.read("#{fname}.haml").to_a.each do |line|
128
- break if line.include?("-# Kinbote")
129
- f_new.puts line
130
- end
131
- if page
132
- kinbote_haml(f_new, "page_attributes_title")
133
- kinbote_haml(f_new, "page_attributes", ["Title: #{page.title}"] + page.values.map{|v| "#{v.attribute.name}: #{v.value}"})
134
- kinbote_haml(f_new, "header")
135
- kinbote_haml(f_new, "specific", page.attributes.map{|a| a._varname})
136
- kinbote_haml(f_new, "related", page.attributes.map{|a| a.name.downcase == "date" ? nil : a.name}.compact)
137
- kinbote_haml(f_new, "related2", page.attributes.map{|a| "#{a.name}\", \"#{a.values.first.value}"})
138
- kinbote_haml(f_new, "related_any")
139
- else
140
- kinbote_haml(f_new, "home", @attributes.map{|a| a._varname})
141
- end
142
- f_new.close
143
- FileUtils.mv("#{fname}.haml.new", "#{fname}.haml")
144
- page.set_filesize if page
145
- add_kinbote_haml if page
146
- end
147
-
148
- def kinbote_haml(f, example_name, replacements=nil)
149
- return if replacements && replacements.size == 0 && example_name != "page_attributes"
150
- File.read("#{@kinbote_path}/.kinbote/views/examples/#{example_name}.haml").to_a.each do |line|
151
- if replacements && line.include?("$ALL_VALS$")
152
- replacements.each do |r|
153
- f.puts(line.gsub("$ALL_VALS$", r))
154
- end
155
- elsif replacements && line.include?("$ALL_VALS_COMMENTED$")
156
- replacements.each_with_index do |r, i|
157
- if i == replacements.size - 1
158
- f.puts(line.gsub("$ALL_VALS_COMMENTED$", r))
159
- else
160
- f.puts(line.gsub("- ", "-# ").gsub("$ALL_VALS_COMMENTED$", r))
161
- end
162
- end
163
- elsif replacements && line.include?("$FIRST_VAL$")
164
- f.puts(line.gsub("$FIRST_VAL$", replacements.first))
165
- elsif replacements && line.include?("$LAST_VAL$")
166
- f.puts(line.gsub("$LAST_VAL$", replacements.last))
167
- else
168
- f.puts(line)
169
- end
134
+ def read_config
135
+ @config = (File.exist?("#{@kinbote_path}/config/site.yml") ? YAML::load_file("#{@kinbote_path}/config/site.yml") : {"title" => "Kinbote Site"})
136
+ @config.each do |key, value|
137
+ Site.send(:define_method, varify(key).to_sym, Proc.new{ @config[key] }) if !Site.respond_to?(varify(key).to_sym)
170
138
  end
171
- f.puts ""
172
139
  end
173
140
 
174
141
  end
@@ -6,11 +6,15 @@ module Kinbote
6
6
  slug = slug[0, slug.size - 1] if slug[-1, 1] == '-'
7
7
  slug
8
8
  end
9
+
10
+ def varify(str)
11
+ slugify(str).gsub('-', '_')
12
+ end
9
13
 
10
14
  def dir_from_file(file)
11
15
  file.split("/")[0...-1].join("/") + "/"
12
16
  end
13
-
17
+
14
18
  def file_without_extension(file)
15
19
  file.split(".")[0...-1].join(".")
16
20
  end
@@ -18,11 +22,11 @@ module Kinbote
18
22
  def slug_from_file(file)
19
23
  file.split("/").last.split(".").first
20
24
  end
21
-
25
+
22
26
  def type_from_file(file)
23
27
  file.split(".").last
24
28
  end
25
-
29
+
26
30
  def output_path(file)
27
31
  if type_from_file(file) == "haml"
28
32
  "#{$site.kinbote_path}/www/#{view_path(file)}".gsub(".haml", ".html")
@@ -30,11 +34,11 @@ module Kinbote
30
34
  "#{$site.kinbote_path}/www/#{view_path(file)}".gsub("/sass/", "/css/").gsub(".sass", ".css")
31
35
  end
32
36
  end
33
-
37
+
34
38
  def view_path(file)
35
39
  file[$site.kinbote_path.size + 7, file.size]
36
40
  end
37
-
41
+
38
42
  def print_state
39
43
  @attributes.each do |a|
40
44
  puts "#{a.name} : #{a.pages.map{|p| p.slug}.join(',')}"
@@ -44,5 +48,13 @@ module Kinbote
44
48
  end
45
49
  end
46
50
 
51
+ def sorted_attributes(attributes)
52
+ attributes.sort!{|x, y| x.name <=> y.name }
53
+ attributes.each_with_index do |attribute, i|
54
+ attributes.insert(0, attributes.delete_at(i)) if attribute.varname == "date"
55
+ end
56
+ attributes
57
+ end
58
+
47
59
  end
48
60
  end
@@ -1,45 +1,27 @@
1
1
  module Kinbote
2
2
  class Value
3
- attr_accessor :value, :attribute
4
-
5
- def initialize(value, page, attribute, global_value = nil)
3
+ include Kinfile
4
+ attr_reader :value, :attribute
5
+
6
+ def initialize(value, page, attribute, global=nil)
6
7
  @value = value.to_s
7
8
  @attribute = attribute
8
- @global_value = global_value || self
9
+ @global = global || self
9
10
  @pages = [page]
10
- @css = File.exist?(file_name(:css))
11
11
  end
12
-
12
+
13
13
  def add_page(page)
14
14
  remove_page(page)
15
15
  @pages << page
16
16
  end
17
-
17
+
18
18
  def remove_page(page)
19
- @global_value.pages.delete_if{|p| p.slug == page.slug}
19
+ @global.pages.delete_if{|p| p.slug == page.slug}
20
20
  @pages.delete_if{|p| p.slug == page.slug}
21
21
  end
22
-
23
- def create_css
24
- FileUtils.mkdir_p(file_name(:css, true)) if !File.exist?(file_name(:css, true))
25
- File.open(file_name(:css), "w") if !File.exist?(file_name(:css))
26
- @global_value.css!
27
- end
28
-
29
- def file_name(type, dir = nil)
30
- if type == :css
31
- return "#{$site.kinbote_path}/views/sass/attributes/#{@attribute}/#{!dir ? "#{@value}.sass" : ""}"
32
- end
33
- end
34
-
35
- def css_files
36
- @global_value.css? ? ["/css/attributes/#{@attribute}/#{@value}"] : []
37
- end
38
22
 
39
- def pages; @global_value == self ? @pages : @global_value.pages; end
23
+ def pages; @global == self ? @pages : @global.pages; end
40
24
  def to_s; @value; end
41
- def css!; @global_value == self ? @css = true : @global_value.css!; end
42
- def css?; @global_value == self ? @css : @global_value.css?; end
43
-
25
+
44
26
  end
45
27
  end
@@ -11,22 +11,21 @@ module Kinbote
11
11
 
12
12
  configure do
13
13
  $site = Site.new
14
- $site.load_pages
14
+ $site.load
15
15
  end
16
16
 
17
17
  before do
18
- $site.set_kinbote_path(options.kinbote_path)
19
- $site.read_config
20
- $site.reload_pages
18
+ $site.kinbote_path = options.kinbote_path
19
+ $site.reload
21
20
  end
22
21
 
23
22
  not_found do
24
- status 404
23
+ set_user_variables
25
24
  haml :'404'
26
25
  end
27
26
 
28
27
  error do
29
- status 500
28
+ set_user_variables
30
29
  haml :'500'
31
30
  end
32
31
 
@@ -51,17 +50,17 @@ module Kinbote
51
50
  end
52
51
 
53
52
  post '/kinbote/pages/*/css' do
54
- $site.find_page(params[:splat].first).create_css
53
+ $site.find_page(params[:splat].first).create_file(:css)
55
54
  redirect '/kinbote/pages'
56
55
  end
57
56
 
58
57
  post '/kinbote/attributes/css' do
59
- $site.find_attribute(params["attribute"]).create_css
58
+ $site.find_attribute(params["attribute"]).create_file(:css)
60
59
  redirect '/kinbote/attributes'
61
60
  end
62
61
 
63
62
  post '/kinbote/values/css' do
64
- $site.find_value(params["value"], $site.find_attribute(params["attribute"]), true).create_css
63
+ $site.find_value(params["value"], $site.find_attribute(params["attribute"]), true).create_file(:css)
65
64
  redirect '/kinbote/attributes'
66
65
  end
67
66
 
@@ -71,9 +70,8 @@ module Kinbote
71
70
  end
72
71
 
73
72
  post '/kinbote/pages/*' do
74
- p params
75
73
  @p = $site.find_page(params[:splat].first)
76
- @p.set_attributes(kv_to_attributes(params["page"]["keys"], params["page"]["values"]))
74
+ @p.set_attributes(params["page"]["keys"], params["page"]["values"])
77
75
  redirect "/kinbote/pages/#{@p.slug}"
78
76
  end
79
77
 
@@ -82,7 +80,7 @@ module Kinbote
82
80
  end
83
81
 
84
82
  post '/kinbote/publish' do
85
- set_user_globals
83
+ set_user_variables
86
84
  publish
87
85
  redirect '/kinbote'
88
86
  end
@@ -90,7 +88,7 @@ module Kinbote
90
88
  # -----------------------------------------------------------------------------------------
91
89
 
92
90
  get '/' do
93
- set_user_globals
91
+ set_user_variables
94
92
  haml :index
95
93
  end
96
94
 
@@ -106,7 +104,7 @@ module Kinbote
106
104
  end
107
105
 
108
106
  get '/*.html' do
109
- set_user_globals
107
+ set_user_variables
110
108
  @page = $site.find_page(params[:splat].first)
111
109
  haml :"#{@page ? "#{@page.path}" : "404"}"
112
110
  end
@@ -6,21 +6,11 @@ module Sinatra
6
6
  haml("snippets/#{path}".to_sym, :layout => false)
7
7
  end
8
8
 
9
- def set_user_globals
9
+ def set_user_variables
10
10
  @site = $site
11
11
  @pages = $site.pages
12
12
  @attributes = $site.attributes
13
- @attributes.each{|att| eval("@#{att._varname} = att")}
14
- end
15
-
16
- def kv_to_attributes(keys, values)
17
- attributes = {}
18
- keys.each_with_index do |key, i|
19
- next if key.size == 0
20
- attributes[key] = [] if !attributes.has_key?(key)
21
- attributes[key] << values[i] if !attributes[key].include?(values[i])
22
- end
23
- attributes
13
+ @attributes.each{|att| eval("@#{att.varname} = att if !@#{att.varname}")}
24
14
  end
25
15
 
26
16
  def publish