-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetamark.rb
More file actions
executable file
·148 lines (126 loc) · 3.07 KB
/
metamark.rb
File metadata and controls
executable file
·148 lines (126 loc) · 3.07 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env ruby
# Metamark
# (C) Peter Orme <peterorme6@gmail.com>
# Version 0.0.1
#
# Metamark is a little script that indexes does an offline, on-demand indexing
# of a github wiki.
# The Drawer class is a simple multimap implementation.
class Drawer
attr_accessor :contents
def initialize
@contents = {}
end
# Adds a key/value pair. If the key has another value already, the value is added
# to the list for that key.
def add(key, val)
if @contents[key].nil?
@contents[key]=[val]
else
if !@contents[key].include?(val)
@contents[key].push(val)
end
end
end
# Sets the value (or array) for a given key.
def set(key, arr)
if arr.kind_of?(Array)
@contents[key]=arr
else
@contents[key]=[arr]
end
end
# removes a key and its values
def delete(key)
@contents.delete(key)
end
# removes everything
def clear
@contents.clear
end
# removes a specific key/value pair. If there are no more values for that key,
# the key is removed as well.
def remove(key, val)
if @contents[key].nil?
# already empty
else
if @contents[key].include?(val)
@contents[key].delete(val)
if @contents[key].empty?
puts "nothing left in #{key}"
@contents.delete(key)
end
end
end
end
end
# the Scanner finds .md files and looks for ~category:xxx directives.
# It collects them all in-memory, in the multimap, and then generates the
# Cattegories.md file from the map.
class Scanner
attr_accessor :verbose
def initialize
@categories = Drawer.new
@verbose = false
end
def add_cat(cat, filename)
# clean the filename
cat.strip! # This wasu unintentional, I swear.
pattern = /\.\/(.*)\.md/
f = filename.match(pattern).captures[0]
puts "Adding category #{cat}->#{f}" if @verbose
@categories.add(cat, f)
end
# scan a single file
def scanfile (filename)
pattern = /^~[ ]?category:([[:alnum:][ _]]+)/
puts "Scanning #{filename}" if @verbose
f = File.open(filename, "r")
f.each_line do |line|
# the "scan" is perhaps not right here. Optimize later.
cat = line.scan(pattern)
# puts "SCAN #{line} -> #{cat}"
if !cat.empty?
match = cat[0][0]
match.strip!
add_cat(match, filename) if !match.empty?
end
end
end
# Scan the folder for markdown files, and scan them
def scanfolder(path)
files = Dir[path]
files.each do |file|
scanfile(file)
end
end
def dump
print "DUMP: "
@categories.contents.each {|x| print x, " -- " }
print "\n"
end
def write
catfile = "Categories.md"
File.open(catfile, 'w') { |file|
file.write("# Categories\n")
keys = @categories.contents.keys
keys.sort!
keys.each {|key|
# heading:
file.write("\n\#\# #{key}\n")
pages = @categories.contents[key].sort!
pages.each {|page|
file.write("* [#{page}](#{page})\n")
}
}
now = Time.now()
file.puts("\nGenerated by metamark on #{now}")
}
end
end
### Das Script An Sich
path = "./*.md"
scanner = Scanner.new
scanner.scanfolder(path)
scanner.write
puts "Metamark regenerated the Categories.md file."