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
|
# File 'lib/booklab/toc/format/markdown.rb', line 6
def load(raw)
items = []
space_size = 4
if raw.match(/^[\t]{1}[\s]?[-\*]/m)
space_size = 1
elsif raw.match(/^[\s]{2}[\s]?[-\*]/m)
space_size = 2
end
lines = raw.split("\n")
lines.each do |line|
item = { "title" => nil, "url" => nil, "depth" => 0 }
line = line.gsub(/^([\s]+)\*/, "\\1-")
item["depth"] = (line.match(/^([\s]+)/).to_s.length / space_size).round
m = line.match(/\[(?<title>.+?)\]\((?<url>.+?)\)/)
if m
item["title"] = m[:title]
item["url"] = m[:url].split(" ")[0]
else
item["title"] = line.gsub(/^[\s]?\-[\s]?/, "").strip
end
items << item
end
items
end
|