Class: Kinbote::Page

Inherits:
Object
  • Object
show all
Includes:
Kinfile, Util
Defined in:
lib/kinbote/page.rb

Constant Summary collapse

BAD_ATTRIBUTE_VARNAMES =
["site", "pages", "attributes"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Kinfile

#create_file, #create_files, #delete_file, #delete_files, #file_size, #files, #has_file?, #init_files, #reset_file_size

Methods included from Util

#dir_from_file, #file_without_extension, #output_path, #print_state, #slug_from_file, #slugify, #sorted_attributes, #type_from_file, #varify, #view_path

Constructor Details

#initialize(title, file = nil, attributes = nil) ⇒ Page

Returns a new instance of Page.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kinbote/page.rb', line 9

def initialize(title, file=nil, attributes=nil)
  init_files
  @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 || new_page_attributes)
  create_file(:html)
  Page.(self) if !file
  set_ivars
end

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug.



5
6
7
# File 'lib/kinbote/page.rb', line 5

def slug
  @slug
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/kinbote/page.rb', line 5

def title
  @title
end

Class Method Details

.add_metadata(page = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/kinbote/page.rb', line 126

def self.(page = nil)
  fname = "#{$site.kinbote_path}/views/#{page ? page.path : "index"}"
  f_new = File.open("#{fname}.haml.new", "w")
  File.read("#{fname}.haml").to_a.each do |line|
    break if line.include?("-# Kinbote")
    f_new.puts line
  end
  if page
    kinbote_haml(f_new, "page_attributes_title")
    kinbote_haml(f_new, "page_attributes", ["Title: #{page.title}"] + page.values.map{|v| "#{v.attribute.name}: #{v.value}"})
    if $site.print_sample_code?
      kinbote_haml(f_new, "header")
      kinbote_haml(f_new, "specific", page.attributes.map{|a| a.varname})
      kinbote_haml(f_new, "related", page.attributes.map{|a| a.name.downcase == "date" ? nil : a.name}.compact)
      kinbote_haml(f_new, "related2", page.attributes.map{|a| "#{a.name}\", \"#{a.values_for(page).first.value}"})
      kinbote_haml(f_new, "related_any")
    end
  else
    kinbote_haml(f_new, "home", $site.attributes.map{|a| a.varname}) if $site.print_sample_code?
  end
  f_new.close
  FileUtils.mv("#{fname}.haml.new", "#{fname}.haml")
  page.reset_file_size(:html) if page
  Page. if page
end

.kinbote_haml(f, example_name, replacements = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/kinbote/page.rb', line 152

def self.kinbote_haml(f, example_name, replacements=nil)
  return if replacements && replacements.size == 0 && example_name != "page_attributes"
  File.read("#{$site.kinbote_path}/.kinbote/views/examples/#{example_name}.haml").to_a.each do |line|
    if replacements && line.include?("$ALL_VALS$")
      replacements.each do |r|
        kinwrite(f, line.gsub("$ALL_VALS$", r))
      end
    elsif replacements && line.include?("$ALL_VALS_COMMENTED$")
      replacements.each_with_index do |r, i|
        if i == replacements.size - 1
          kinwrite(f, line.gsub("$ALL_VALS_COMMENTED$", r))
        else
          kinwrite(f, line.gsub("- ", "-# ").gsub("$ALL_VALS_COMMENTED$", r))
        end
      end
    elsif replacements && line.include?("$FIRST_VAL$")
      kinwrite(f, line.gsub("$FIRST_VAL$", replacements.first))
    elsif replacements && line.include?("$LAST_VAL$")
      kinwrite(f, line.gsub("$LAST_VAL$", replacements.last))
    else
      kinwrite(f, line)
    end
  end
  f.puts "" 
end

.kinwrite(f, line) ⇒ Object



178
179
180
181
# File 'lib/kinbote/page.rb', line 178

def self.kinwrite(f, line)
  line.gsub!(/\t/, $site.config["haml_indentation"]) if ($site.config.has_key?("haml_indentation") && $site.config["haml_indentation"] != '\t')
  f.puts(line)
end

Instance Method Details

#add_attribute(name, values) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/kinbote/page.rb', line 42

def add_attribute(name, values)
  if BAD_ATTRIBUTE_VARNAMES.include?(varify(name))
    $site.add_message("#{name} is an invalid attribute name")
    next
  end
  att = $site.find_attribute(name) || Attribute.new(name, self, values)
  att.add_page(self, values)
  $site.add_attribute(att)
  @attributes << att if !@attributes.map{|a| a.name}.include?(name)
end

#add_attributes(attributes) ⇒ Object



36
37
38
39
40
# File 'lib/kinbote/page.rb', line 36

def add_attributes(attributes)
  attributes.each do |name, values|
    add_attribute(name, values)
  end
end

#attributesObject



96
97
98
# File 'lib/kinbote/page.rb', line 96

def attributes
  sorted_attributes(@attributes)
end

#filtered_by?(attribute = nil, value = nil) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/kinbote/page.rb', line 106

def filtered_by?(attribute=nil, value=nil)
  return false if (!attribute || attribute.strip.size == 0) && (!value || value.strip.size == 0)
  a = $site.find_attribute(attribute, self)
  return true if !a
  (!value || value.strip.size == 0 || ($site.find_value(value, a) && $site.find_value(value, a).has_page?(@slug))) ? false : true
end

#has_attribute?(att_name) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/kinbote/page.rb', line 183

def has_attribute?(att_name)
  @attributes.map{|a| a.name}.include?(att_name)
end


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kinbote/page.rb', line 72

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{ |a_name| page_matches += lrp(a_name) }
  else
    page_matches += lrp(attribute_names)
  end
  page_matches.uniq
end

#pathObject



113
114
115
116
117
118
119
120
121
# File 'lib/kinbote/page.rb', line 113

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

#rem_attribute(att_name) ⇒ Object



53
54
55
56
57
# File 'lib/kinbote/page.rb', line 53

def rem_attribute(att_name)
  @attributes.map{|att| att.rem_page(self) if att.name == att_name}
  @attributes.delete_if{|att| att.name == att_name}
  $site.attributes.delete_if{|att| att.pages.size == 0}
end

#rem_value(att_name, val) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kinbote/page.rb', line 59

def rem_value(att_name, val)
  att = $site.find_attribute(att_name, self)
  return if !att
  v = $site.find_value(val, att)
  return if !v || !v.has_page?(@slug)
  v.rem_page(self)
  att.rem_page(self) if att.values_for(self).size == 0
  att.values_for(self).delete_if{|vv| vv.pages.size == 0}
  @attributes.delete_if{|aa| aa.pages.size == 0 || aa.values_for(self).size == 0}
  att.values.delete_if{|vv| vv.pages.size == 0}
  $site.attributes.delete_if{|aa| aa.pages.size == 0 || aa.values.size == 0}
end

#set_attributes(attributes, values = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kinbote/page.rb', line 22

def set_attributes(attributes, values=nil)
  attributes = kv_to_attributes(attributes, values) if values
  new_title = rem_title(attributes)
  return if same_attributes(attributes, new_title)
  @title = new_title if new_title && new_title.size > 0
  $site.rem_page_attributes(self)
  @attributes.map{|att| att.rem_page(self)}
  @attributes.delete_if{|att| att.pages.size == 0}
  @attributes = []
  add_attributes(attributes)
  Page.(self)
  set_ivars
end


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kinbote/page.rb', line 84

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{ |a_name| page_matches += srp(a_name, values) }
  else
    page_matches += srp(attribute_names, values)
  end
  page_matches.uniq
end

#to_sObject



123
# File 'lib/kinbote/page.rb', line 123

def to_s; @title; end

#urlObject



124
# File 'lib/kinbote/page.rb', line 124

def url; "#{@slug}.html"; end

#valuesObject



100
101
102
103
104
# File 'lib/kinbote/page.rb', line 100

def values
  vals = []
  attributes.each{ |a| a.values_for(self).each{ |v| vals << v } }
  vals
end