Class: Kinbote::Page
- Inherits:
-
Object
show all
- Includes:
- Util
- Defined in:
- lib/kinbote/page.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Util
#dir_from_file, #file_without_extension, #output_path, #print_state, #slug_from_file, #slugify, #type_from_file, #view_path
Constructor Details
#initialize(title, file = nil, attributes = nil) ⇒ Page
Returns a new instance of Page.
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/kinbote/page.rb', line 7
def initialize(title, file=nil, attributes=nil)
@title = title
@directory = (file ? dir_from_file(file) : file_path(:html, true))
@file = file || file_path(:html)
@slug = slug_from_file(@file)
@attributes = []
add_attributes(attributes || {"Date" => Time.now.strftime("%B %d, %Y")})
create_html
@css = File.exist?(file_path(:css))
set_ivars
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
5
6
7
|
# File 'lib/kinbote/page.rb', line 5
def attributes
@attributes
end
|
#slug ⇒ Object
Returns the value of attribute slug.
5
6
7
|
# File 'lib/kinbote/page.rb', line 5
def slug
@slug
end
|
#title ⇒ Object
Returns the value of attribute title.
5
6
7
|
# File 'lib/kinbote/page.rb', line 5
def title
@title
end
|
Instance Method Details
#create_css ⇒ Object
85
86
87
88
89
|
# File 'lib/kinbote/page.rb', line 85
def create_css
FileUtils.mkdir_p(file_path(:css, true)) if !File.exist?(file_path(:css, true))
File.open(file_path(:css), "w") if !File.exist?(file_path(:css))
@css = true
end
|
#css! ⇒ Object
112
|
# File 'lib/kinbote/page.rb', line 112
def css!; @css = true; end
|
#css? ⇒ Boolean
113
|
# File 'lib/kinbote/page.rb', line 113
def css?; @css; end
|
#css_files ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/kinbote/page.rb', line 91
def css_files
files = (css? ? ["/css/pages/#{@slug}"] : [])
@attributes.each do |attribute|
files += attribute.css_files
end
files
end
|
#delete_files ⇒ Object
70
71
72
73
|
# File 'lib/kinbote/page.rb', line 70
def delete_files
FileUtils.rm_f(file_path(:css))
FileUtils.rm_f(file_path(:html))
end
|
#file_path(type, dir = nil) ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/kinbote/page.rb', line 99
def file_path(type, dir = nil)
if type == :html
return @file if @file && !dir
return @directory if @directory && dir
return "#{$site.kinbote_path}/views/pages/#{Time.now.strftime("%Y/%m")}/#{!dir ? "/#{slugify(@title)}.haml" : ""}"
elsif type == :css
return "#{@directory}#{!dir ? "#{@slug}.sass" : ""}"
end
end
|
#get_filesize ⇒ Object
110
|
# File 'lib/kinbote/page.rb', line 110
def get_filesize; @filesize; end
|
#loosely_related_pages(attribute_names = nil) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/kinbote/page.rb', line 32
def loosely_related_pages(attribute_names = nil)
page_matches = []
if !attribute_names
page_matches += loosely_related_pages(@attributes.map{|a| a.name})
elsif attribute_names.is_a?(Array)
attribute_names.each do |a_name|
page_matches += lrp(a_name)
end
else
page_matches += lrp(attribute_names)
end
page_matches.uniq
end
|
#path ⇒ Object
75
76
77
78
79
80
81
82
83
|
# File 'lib/kinbote/page.rb', line 75
def path
path = []
in_path = false
@directory.split("/").each do |d|
in_path = true if d == "pages"
path << d if in_path
end
"#{path.join("/")}/#{@slug}"
end
|
#set_attributes(attributes) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/kinbote/page.rb', line 19
def set_attributes(attributes)
new_title = remove_title(attributes)
return if same_attributes(attributes, new_title)
@title = new_title if new_title && new_title.size > 0
$site.remove_page_attributes(self)
@attributes.map{|att| att.remove_page(self)}
@attributes.delete_if{|att| att.pages.size == 0}
@attributes = []
add_attributes(attributes)
$site.add_kinbote_haml(self)
set_ivars
end
|
#set_filesize ⇒ Object
109
|
# File 'lib/kinbote/page.rb', line 109
def set_filesize; @filesize = File.size(file_path(:html)); end
|
#strongly_related_pages(attribute_names = nil, values = nil) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/kinbote/page.rb', line 46
def strongly_related_pages(attribute_names = nil, values = nil)
page_matches = []
if !attribute_names
page_matches += strongly_related_pages(@attributes.map{|a| a.name})
elsif attribute_names.is_a?(Array)
attribute_names.each do |a_name|
page_matches += srp(a_name, values)
end
else
page_matches += srp(attribute_names, values)
end
page_matches.uniq
end
|
#to_s ⇒ Object
111
|
# File 'lib/kinbote/page.rb', line 111
def to_s; @title; end
|
#url ⇒ Object
114
|
# File 'lib/kinbote/page.rb', line 114
def url; "#{@slug}.html"; end
|
#values ⇒ Object
60
61
62
63
64
65
66
67
68
|
# File 'lib/kinbote/page.rb', line 60
def values
vals = []
@attributes.each do |a|
a.values.each do |v|
vals << v
end
end
vals
end
|