Class: Rwiki::Page

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rwiki/page.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = Rwiki.configuration.root_page_name) ⇒ Page

Returns a new instance of Page.

Raises:



14
15
16
17
# File 'lib/rwiki/page.rb', line 14

def initialize(path = Rwiki.configuration.root_page_name)
  @file_helper = FileHelper.new(path)
  raise Error.new("Cannot find the #{path} page") unless file_helper.exists?
end

Instance Attribute Details

#file_helperObject (readonly)

Returns the value of attribute file_helper.



7
8
9
# File 'lib/rwiki/page.rb', line 7

def file_helper
  @file_helper
end

#textile_helperObject (readonly)

Returns the value of attribute textile_helper.



8
9
10
# File 'lib/rwiki/page.rb', line 8

def textile_helper
  @textile_helper
end

Class Method Details

.fuzzy_finder(query) ⇒ Object

TODO cleanup and write tests



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rwiki/page.rb', line 133

def self.fuzzy_finder(query)
  @finder ||= FuzzyFileFinder.new(Rwiki.configuration.rwiki_path)
  @finder.rescan!

  matches = @finder.find(query).sort_by { |m| [-m[:score], m[:path]] }
  matches.each do |m|
    m[:path] = m[:path].gsub(Rwiki.configuration.rwiki_path, '').gsub(/\.#{Rwiki.configuration.page_file_extension}$/, '')
    m[:highlighted_path] = m[:highlighted_path].gsub(/\.#{Rwiki.configuration.page_file_extension}$/, '')
  end

  matches
end

.rootObject



10
11
12
# File 'lib/rwiki/page.rb', line 10

def self.root
  self.new
end

Instance Method Details

#add_page(name) ⇒ Object



76
77
78
# File 'lib/rwiki/page.rb', line 76

def add_page(name)
  Page.new(file_helper.add_page(name))
end

#childrenObject



48
49
50
# File 'lib/rwiki/page.rb', line 48

def children
  @children ||= fetch_children
end

#deleteObject

Raises:



107
108
109
110
# File 'lib/rwiki/page.rb', line 107

def delete
  raise Error.new("Cannot delete the #{Rwiki.configuration.root_page_name} page") if is_root?
  file_helper.delete
end

#file_contentObject



80
81
82
# File 'lib/rwiki/page.rb', line 80

def file_content
  @file_content ||= file_helper.read_file_content
end

#findgrep(pattern, matches = []) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rwiki/page.rb', line 146

def findgrep(pattern, matches = [])
  pattern = /#{Regexp.escape(pattern)}/i if pattern.kind_of?(String)

  children.each do |child|
    child.file_content.lines.grep(pattern) do |line|
      matches << { :path => child.path, :line => line.strip }
    end

    if child.has_children?
      child.findgrep(pattern, matches)
    end
  end

  matches
end

#full_file_pathObject



35
36
37
# File 'lib/rwiki/page.rb', line 35

def full_file_path
  file_helper.full_file_path
end

#full_pathObject



31
32
33
# File 'lib/rwiki/page.rb', line 31

def full_path
  file_helper.full_path
end

#has_children?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/rwiki/page.rb', line 56

def has_children?
  !leaf?
end

#html_contentObject



84
85
86
# File 'lib/rwiki/page.rb', line 84

def html_content
  @html_content ||= textile_helper.parsed_content
end

#html_tocObject



88
89
90
# File 'lib/rwiki/page.rb', line 88

def html_toc
  @html_toc ||= textile_helper.parsed_toc
end

#is_root?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rwiki/page.rb', line 23

def is_root?
  file_helper.path == Rwiki.configuration.root_page_path
end

#leaf?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/rwiki/page.rb', line 52

def leaf?
  children.size == 0
end

#move_to(node) ⇒ Object

Raises:



102
103
104
105
# File 'lib/rwiki/page.rb', line 102

def move_to(node)
  raise Error.new("Cannot move the #{Rwiki.configuration.root_page_name} page") if is_root?
  file_helper.move_to(node.path)
end

#parentObject



43
44
45
46
# File 'lib/rwiki/page.rb', line 43

def parent
  return if is_root?
  @parent ||= Page.new(file_helper.full_parent_path)
end

#pathObject



27
28
29
# File 'lib/rwiki/page.rb', line 27

def path
  file_helper.path
end

#rename_to(new_name) ⇒ Object

Raises:



97
98
99
100
# File 'lib/rwiki/page.rb', line 97

def rename_to(new_name)
  raise Error.new("Cannot rename the #{Rwiki.configuration.root_page_name} page") if is_root?
  file_helper.rename_to(new_name)
end

#titleObject



39
40
41
# File 'lib/rwiki/page.rb', line 39

def title
  file_helper.basename
end

#to_hashObject



119
120
121
122
123
124
125
126
# File 'lib/rwiki/page.rb', line 119

def to_hash
  {
    :path => path,
    :rawContent => file_content,
    :htmlContent => html_content,
    :htmlToc => html_toc
  }
end

#to_json(extra_attrs = {}) ⇒ Object



128
129
130
# File 'lib/rwiki/page.rb', line 128

def to_json(extra_attrs = {})
  to_hash.merge(extra_attrs).to_json
end

#to_tree_node_hashObject



112
113
114
115
116
117
# File 'lib/rwiki/page.rb', line 112

def to_tree_node_hash
  {
    :text => title,
    :leaf => false
  }
end

#treeObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rwiki/page.rb', line 60

def tree
  result = []

  children.each do |child|
    tree_node = child.to_tree_node_hash

    if child.has_children?
      tree_node[:children] = child.tree
    end

    result << tree_node
  end

  result
end

#update_file_content(file_content) ⇒ Object



92
93
94
95
# File 'lib/rwiki/page.rb', line 92

def update_file_content(file_content)
  file_helper.update_file_content(file_content)
  reload!
end