simple_markdown 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/simple_markdown/action_view/helpers.rb +73 -49
- data/lib/simple_markdown/version.rb +1 -1
- data/test/dummy/log/test.log +8355 -0
- data/test/simple_markdown_test.rb +21 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36fd8a6a21b73f7e8fa22c18f2602cf6d533ac3c
|
4
|
+
data.tar.gz: 5cb9cb43981dc3e857fdb3d6fd1248ea9b70fb16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1e218f7c05bdbf6c54b41db8029715ff667e97efddf903e949aa57b344d63ee218ea928ec23ff280fe665df689e8e889d5fe2d42d1e5548ca9ce42f02fe8111
|
7
|
+
data.tar.gz: 23d5d3b04b68bdf91a8ec150ca45de61ed8467d0c4a00ea858ba34903ced4fc1bd43c0c46768a6d1f23047b82afb25f7d176128371c46d133bb9a9673d1c3591
|
@@ -7,66 +7,63 @@ module SimpleMarkdown
|
|
7
7
|
@text_map
|
8
8
|
@io
|
9
9
|
@current
|
10
|
-
@continue
|
11
10
|
|
12
11
|
# Main entry
|
13
12
|
def simple_markdown(text)
|
14
13
|
text = text.gsub(/\r\n?/, "\n").split(/\n/)
|
15
14
|
@text_map = text.map
|
16
15
|
@io = StringIO.new
|
17
|
-
|
18
|
-
|
16
|
+
begin
|
17
|
+
while(true)
|
18
|
+
parse_block
|
19
|
+
end
|
20
|
+
rescue StopIteration
|
21
|
+
ensure
|
22
|
+
return @io.string.html_safe
|
23
|
+
end
|
19
24
|
end
|
20
25
|
|
21
26
|
private
|
22
27
|
|
28
|
+
def parse_block
|
29
|
+
if(@text_map.peek.match(/^$/)) # don't want empty <p></p>
|
30
|
+
@text_map.next
|
31
|
+
elsif @text_map.peek.match(/^\s*```\s*$/) # code block
|
32
|
+
@text_map.next
|
33
|
+
parse_code
|
34
|
+
elsif @text_map.peek.match(/^\s*\#/)
|
35
|
+
parse_title # title, only works if has return before (except first time)
|
36
|
+
elsif @text_map.peek.match(/^\s*\[[0-9]+flex[0-9]*\]\s*$/)
|
37
|
+
parse_flex
|
38
|
+
else # normal block
|
39
|
+
parse_p
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
23
43
|
def parse_p
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
@io << "<p>"
|
36
|
-
@io << "\n"
|
37
|
-
parse_normal
|
38
|
-
@io << "\n"
|
39
|
-
@io << "</p>"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
rescue
|
43
|
-
# do nothing
|
44
|
-
end
|
44
|
+
begin
|
45
|
+
@io << "<p>\n"
|
46
|
+
while(!@text_map.peek.match(/^\s*$/)) # end paragraph if empty line
|
47
|
+
parse_normal
|
48
|
+
end
|
49
|
+
@text_map.next;
|
50
|
+
rescue StopIteration
|
51
|
+
# do nothing
|
52
|
+
ensure
|
53
|
+
@io << "\n</p>"
|
54
|
+
end
|
45
55
|
end
|
46
56
|
|
47
57
|
def parse_normal
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
line.gsub!(/\*\*([^\*]*)\*\*/, "<strong>#{'\1'}</strong>") # bold
|
58
|
-
if(first_time)
|
59
|
-
first_time = false
|
60
|
-
else
|
61
|
-
@io << " "
|
62
|
-
end
|
63
|
-
@io << line
|
64
|
-
@io << "<br>\n" if line.match(/\s{2,}$/) # return if more than 2 spaces at the end of the line
|
65
|
-
line = @text_map.next
|
66
|
-
end
|
67
|
-
rescue StopIteration
|
68
|
-
@continue = false
|
69
|
-
end
|
58
|
+
line = @text_map.next
|
59
|
+
line.gsub!(/(^|[^!])\[([^\]]*)\]\(([^\)]*)\)/, "#{'\1'}<a href=\"#{'\3'.strip}\">#{'\2'}</a>") # link
|
60
|
+
line.gsub!(/!\[([^\]]*)\]\(([^\)]*)\)/, "<img src=\"#{'\2'}\" alt=\"#{'\1'.strip}\">") # image
|
61
|
+
line.gsub!(/^\s*\*\s(.*)/, "• #{'\1'}<br>") # list
|
62
|
+
line.gsub!(/`([^`]+)`/) { |match| "<code>#{CGI::escapeHTML(Regexp.last_match[1])}</code>"} # inline code
|
63
|
+
line.gsub!(/(^|[^\*])\*([^\*]+)\*/, "#{'\1'}<em>#{'\2'}</em>") # italic
|
64
|
+
line.gsub!(/\*\*([^\*]*)\*\*/, "<strong>#{'\1'}</strong>") # bold
|
65
|
+
@io << line.gsub(/^([^\s]*)\s+$/, '\1 ') # prints one space if on or more at then end of the line
|
66
|
+
@io << "<br>\n" if line.match(/\s{2,}$/) # return if more than 2 spaces at the end of the line
|
70
67
|
end
|
71
68
|
|
72
69
|
def parse_code
|
@@ -78,8 +75,8 @@ module SimpleMarkdown
|
|
78
75
|
if line.match(/^\s*```\s*$/)
|
79
76
|
continue = false
|
80
77
|
else
|
81
|
-
@io <<
|
82
|
-
@io << "\n"
|
78
|
+
@io << CGI::escapeHTML(line)
|
79
|
+
@io << "\n" unless @text_map.peek.match(/^\s*```\s*$/)
|
83
80
|
end
|
84
81
|
rescue StopIteration
|
85
82
|
continue = false
|
@@ -97,6 +94,33 @@ module SimpleMarkdown
|
|
97
94
|
@io << line
|
98
95
|
end
|
99
96
|
|
97
|
+
def parse_flex
|
98
|
+
begin
|
99
|
+
@io << "<div style=\"display:flex; justify-content:space-between; align-items: flex-start;\">\n"
|
100
|
+
line = @text_map.next
|
101
|
+
scan = line.scan(/[0-9]+/)
|
102
|
+
number = scan[0].to_i
|
103
|
+
space = scan[1]
|
104
|
+
1.upto(number) do |i|
|
105
|
+
if space
|
106
|
+
@io << "<div style=\"flex:#{space};\">\n"
|
107
|
+
else
|
108
|
+
@io << "<div>\n"
|
109
|
+
end
|
110
|
+
while(!@text_map.peek.match(/^\s*\[flex[0-9]*\]\s*$/))
|
111
|
+
parse_block
|
112
|
+
end
|
113
|
+
line = @text_map.next
|
114
|
+
space = line.scan(/[0-9]+/)[0]
|
115
|
+
@io << "\n</div>"
|
116
|
+
end
|
117
|
+
rescue StopIteration
|
118
|
+
# do nothing
|
119
|
+
ensure
|
120
|
+
@io << "\n</div>"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
100
124
|
end
|
101
125
|
end
|
102
|
-
end
|
126
|
+
end
|