-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmarkdown-tree.rb
70 lines (57 loc) · 1.48 KB
/
markdown-tree.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
#!/usr/bin/ruby
require 'sinatra'
require 'redcarpet'
require 'yaml'
#Config
$config = YAML.load_file('config.yaml')
#Set up Sinatra Config
set :views => "#{settings.root}/#{$config['template-folder']}/",
:public_folder => "#{settings.root}/#{$config['template-folder']}/",
:static => true
#All Pages
get '/*' do
path = params[:splat].join.split("/")
#File or folder
folder = "#{Dir.getwd}/#{$config['hierarchy-folder']}/#{params[:splat].join}"
if File.directory?(folder) then
file = "index"
else
folder.gsub!(/\/[^\/]+$/,"")
file = path.pop
end
#Get children of current folder
children = getChildren(folder)
#Render Markdown
begin
content = File.read("#{folder}/#{file}.#{$config['markdown-extension']}")
rescue
content = ""
end
erb :template, :locals => {
:siteTitle => $config['site-title'],
:currentFile => file,
:path => path,
:children => children,
:content => markdown(content)
}
end
#Children Array
def getChildren(folderPath)
children = {
:directories => [],
:pages => []
}
#Loop through each child
Dir.foreach(folderPath) do |child|
# Skip if index or . or ..
next if child.match(/^(index.#{$config['markdown-extension']}|\.+)$/)
#Children folder or file
if File.directory?("#{folderPath}/#{child}") then
children[:directories].push(child)
else
#Throw away extension and push
children[:pages].push(child.gsub!(/\.[^.]+$/, ''))
end
end
return children
end