This repository was archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.rb
More file actions
64 lines (50 loc) · 1.57 KB
/
application.rb
File metadata and controls
64 lines (50 loc) · 1.57 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
module Tractatus
class Tree
attr_reader :lines, :root
def initialize(lines)
@lines = lines
@root = ::Tree::TreeNode.new("Tractatus Logico-Philosophicus")
build!
end
def build!
[0, root].tap do |prev_depth, prev_node|
lines.each do |line|
line = Line.new(line)
# Traverse up to appropriate parent
parent = (prev_depth - (line.depth - 1)).times.collect do
:parent
end.inject(prev_node, &:send)
# If node is nil then we are grafting onto the root node
(parent.nil? ? root : parent) << line.node
# Setup for next loop
prev_depth, prev_node = line.depth, line.node
end
end
end
end # Tree
class Line
attr_reader :line, :node
def initialize(line)
@line = line
@node = ::Tree::TreeNode.new(name, statement)
end
def match; @match ||= /^\d.\d*/.match(line); end
def name; @name ||= match.to_s; end
def depth; @depth ||= match.to_s.split(".")[1].try(:size) || 0; end
def statement; @statement ||= match.post_match.strip; end
end # Line
class Translations
PATH = "./resources/txt"
VERSIONS = {
ogden: "#{PATH}/ogden.txt"
}
end # Translations
end # Tractatus
class Application < Sinatra::Base
use Rack::Static, urls: ["/stylesheets", "/javascripts"], root: "public"
get "/" do
@lines = File.open(Tractatus::Translations::VERSIONS[:ogden]).read.split("\n")
@tractatus = Tractatus::Tree.new(@lines)
erb :index
end
end # Application