simple_markdown 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a02774e8615d84e2705aa777ad21f5d87c76219b
4
- data.tar.gz: db4f3e53e79aa2332788fa0be8d0afd7fcf1e703
3
+ metadata.gz: 36fd8a6a21b73f7e8fa22c18f2602cf6d533ac3c
4
+ data.tar.gz: 5cb9cb43981dc3e857fdb3d6fd1248ea9b70fb16
5
5
  SHA512:
6
- metadata.gz: b3062997ad2c0a4a367f2aee5918d58bef133c50402035d76ef9832a035177aa639095953537462382b298475abcc91e2540c46b12f62c08c9817e01098df7d8
7
- data.tar.gz: 1641869f69799f8afae72f9ecec1b62d1794de08e09882787059167d265093665ce38fc66d51b7c6e7bcfc26408111e63132628a0ef5c298af242b55eb7c2acf
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
- parse_p
18
- @io.string.html_safe
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
- @continue = true
25
- begin
26
- while(@continue)
27
- if(@text_map.peek.match(/^$/)) # don't want empty <p></p>
28
- @text_map.next
29
- elsif @text_map.peek.match(/^\s*```\s*$/) # code block
30
- @text_map.next
31
- parse_code
32
- elsif @text_map.peek.match(/^\s*\#/)
33
- parse_title # title, only works if has return before
34
- else # normal block
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
- first_time = true
49
- begin
50
- line = @text_map.next
51
- while(!line.match(/^\s*$/)) # end paragraph if empty line
52
- line.gsub!(/(^|[^!])\[([^\]]*)\]\(([^\)]*)\)/, "#{'\1'}<a href=\"#{'\3'.strip}\">#{'\2'}</a>") # link
53
- line.gsub!(/!\[([^\]]*)\]\(([^\)]*)\)/, "<img src=\"#{'\2'}\" alt=\"#{'\1'.strip}\">") # link
54
- line.gsub!(/^\s*\*\s(.*)/, "• #{'\1'}<br>") # list
55
- line.gsub!(/`([^`]+)`/) { |match| "<code>#{h(Regexp.last_match[1])}</code>"} # inline code
56
- line.gsub!(/(^|[^\*])\*([^\*]+)\*/, "#{'\1'}<em>#{'\2'}</em>") # italic
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 << h(line)
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
@@ -1,3 +1,3 @@
1
1
  module SimpleMarkdown
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end