-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_marker.rb
55 lines (45 loc) · 1.17 KB
/
list_marker.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
require './strong_marker'
class ListMarker
def mark_lists(incoming_text)
split_text = splitter(incoming_text)
tagged_text = list_differentiator(split_text)
joined_text = text_joiner(tagged_text)
joined_text
end
def splitter(incoming_text)
split_text = incoming_text.split("\n\n")
end
def list_differentiator(incoming_text)
tagged_text = incoming_text.map do |chunk|
if chunk[0] == "*"
chunk = add_list_element_tags(chunk)
chunk = add_unordered_tags(chunk)
elsif chunk [0] == "1"
chunk = add_list_element_tags(chunk)
chunk = add_ordered_tags(chunk)
else
chunk
end
end
tagged_text
end
def add_list_element_tags(chunk)
chunk = chunk.split("\n").collect do |line|
if chunk[0] == "*"
line = " <li>" + line.slice(2..-1) + "</li>"
else
line = " <li>" + line.slice(3..-1) + "</li>"
end
end
chunk = chunk.join("\n")
end
def add_unordered_tags(list)
list = "<ul>\n" + list + "\n</ul>"
end
def add_ordered_tags(list)
list = "<ol>\n" + list + "\n</ol>"
end
def text_joiner(incoming_text)
incoming_text.join("\n\n")
end
end