-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplewiki.rb
93 lines (90 loc) · 2.52 KB
/
simplewiki.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require_relative 'wikipage'
require_relative 'wikiconfig'
require_relative 'labelindex'
class SimpleWiki
def initialize
@config = WikiConfig.new
@label_index = LabelIndex.new
@label_index.load
end
def get_text_file_paths
text_file_paths = Dir.glob('text/*.txt')
end
def saved_page_names
text_file_paths = get_text_file_paths
text_file_paths.sort!
#puts text_file_paths
saved_pages = []
text_file_paths.each do |text_file_path|
page_name = page_name(text_file_path)
saved_pages << page_name
end
saved_pages
end
def page_saved(wikipage)
labels_text = wikipage.get_labels
if labels_text
@label_index.add_labels(labels_text, wikipage.page_name)
@label_index.save
end
end
def search_for_text(text)
#puts "--- search_for_text (#{text})"
text_file_paths = get_text_file_paths
result = Hash.new
text_file_paths.each do |text_file_path|
page_name = page_name(text_file_path)
page = WikiPage.new(page_name)
matching_lines = page.search_for_text(text)
next if matching_lines.empty?
result[page_name] = matching_lines
end
label_result = @label_index.get_pages(text)
#puts "+++++ search_for_text (#{text}) result has #{result.size} pages with matches +++++"
return label_result,result
end
def page_name(text_file_path)
File.basename(text_file_path, '.txt')
end
def last_page_name
text_file_paths = get_text_file_paths
last_mtime = nil
last_page = nil
text_file_paths.each do |text_file_path|
stat = File.stat(text_file_path)
#puts "#{text_file_path} -> mtime: #{stat.mtime}"
#puts "stat.mtime.class: #{stat.mtime.class}"
if !last_mtime or stat.mtime > last_mtime
last_mtime = stat.mtime
last_page = page_name(text_file_path)
end
end
last_page
end
def last_few_pages
last_n_pages(@config.last_num_pages)
end
def last_n_pages(n)
text_file_paths = get_text_file_paths
pages = []
text_file_paths.each do |text_file_path|
stat = File.stat(text_file_path)
page_name = page_name(text_file_path)
page = WikiPage.new(page_name, stat.mtime)
pages << page
end
now = Time.now
sorted_by_mtime = pages.sort_by do |page|
now-page.mtime
end
page_names = []
sorted_by_mtime[0,n].each do |wikipage|
#puts "wikipage.class: #{wikipage.class}"
page_names << wikipage.page_name
end
page_names
end
def favorites
favorites = @config.favorites
end
end