markdown_parser 0.0.1 → 0.1.0.alpha1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bbb92f0e6efe86ed6f5ab80581e508ceffc4c58c6ed58e8466bad6531711658e
4
- data.tar.gz: 14726c3fb8783f98da7be7a864d305d81e95722fbb47ed08cba2ef9bb710ce3f
3
+ metadata.gz: 24a82112716238d60baf97812afb31b33779efa4012f2ba15500e694c3df7af6
4
+ data.tar.gz: e0e04502f37ea5da9d3be44924c51e53dc981d7ee914879c3a9ebd18f1fb7c27
5
5
  SHA512:
6
- metadata.gz: f7194f638141c5084ed407b90c1ec8d23411de3b832197910c6dccaeadcc06191333ebc1e47242c7688d1bffd995f83de08f2142e8a86fddeba3c0087be32df9
7
- data.tar.gz: c96991af697b6d48cbc5518dfb011afb1850ad162d95bd0ee108bf250ba639139febf5b86ac597dc177d370855f10f65904799d08df8891160852e05e5eb7f54
6
+ metadata.gz: c69b9ad5c2f78ab1fdac2964b413d12e51e7c6cacd9e45c0115ed8afe98d24c029b8b1d45e5e1ee79edbdbd81252c88910635e3ace3a809305b780e195b5a411
7
+ data.tar.gz: 349b12e7146226b43b987775f3a31aa01ddd41e7ce78227d208b7e389e9ff3234deba639e835120ad1f13fbd7e681f81c2916925c006d8acadfbe9971b49cbda
@@ -0,0 +1,37 @@
1
+ module MarkdownParser
2
+ module InlineStyle
3
+ def self.apply(line)
4
+ apply_bold(line)
5
+ apply_italics(line)
6
+ apply_code(line)
7
+ apply_link(line)
8
+ apply_img(line)
9
+ end
10
+
11
+ def self.apply_bold(line)
12
+ # **strong**
13
+ line.gsub!(/\*\*(?<word>[^\*]*)\*\*/, "<strong>\\k<word></strong>")
14
+ end
15
+
16
+ def self.apply_italics(line)
17
+ # _em_
18
+ line.gsub!(/\_(?<word>[^_]*)\_/, "<em>\\k<word></em>")
19
+ end
20
+
21
+ def self.apply_code(line)
22
+ # `code`
23
+ line.gsub!(/`(?<word>[^`]*)`/, "<code>\\k<word></code>")
24
+ end
25
+
26
+ def self.apply_link(line)
27
+ # [alt message](image_url)
28
+ line.gsub!(/!\[(?<alt>[^\]]*)\]\((?<link>[^\)]*)\)/, "<img src='\\k<link>' alt='\\k<alt>' />")
29
+ end
30
+
31
+ def self.apply_img(line)
32
+ # [text](limk)
33
+ line.gsub!(/\[(?<text>[^\]]*)\]\((?<link>[^\)]*)\)/, "<a href='\\k<link>'>\\k<text></a>")
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,49 @@
1
+ module MarkdownParser
2
+ module LineStyle
3
+ def self.apply(line)
4
+ return "#{line}\n" if MarkdownParser::Rules.state[:code_tag_opened] and !line.match(/^```$/)
5
+
6
+ return apply_header(line) if line.match(/^#+\s/ )
7
+ return '<hr />' if line.match(/^(---|___|\*\*\*)\s*$/)
8
+ return apply_blockquote(line) if line.match(/^>\s.+$/)
9
+ return apply_code(line) if line.match(/^```.*/)
10
+ return apply_list(line) if line.match(/^(-|\*|\+|\d+)\s.+/)
11
+
12
+ line.empty? ? line : "<p>#{line}</p>"
13
+ end
14
+
15
+ # haeder method
16
+ def self.apply_header(line)
17
+ depth = title_depth(line)
18
+ return line if depth == 0
19
+ line.gsub(eval("/^#{'#' * depth}\s/"), "<h#{depth}>").gsub(/$/, "</h#{depth}>")
20
+ end
21
+
22
+ # define how many #'s a string has'
23
+ def self.title_depth(str)
24
+ str.match(/^#+\s*/).to_s.size - 1
25
+ end
26
+
27
+ # blockquote method
28
+ def self.apply_blockquote(line)
29
+ "<p><blockquote>#{line[2..-1]}</blockquote></p>"
30
+ end
31
+
32
+ # multi-line code method
33
+ def self.apply_code(line)
34
+ line = MarkdownParser::Rules.state[:code_tag_opened] ? "</code></pre>" : "<pre><code>"
35
+ MarkdownParser::Rules.toggle(:code_tag_opened)
36
+ line
37
+ end
38
+
39
+ # list item method, also add ul tag when is the first to be added
40
+ def self.apply_list(line)
41
+ line = "<li>#{line[2..-1]}</li>"
42
+ line = "<ul>" + line unless MarkdownParser::Rules.state[:last_was_list]
43
+ MarkdownParser::Rules.set(:list_opened, true)
44
+ MarkdownParser::Rules.set(:last_was_list, true)
45
+ line
46
+ end
47
+
48
+ end
49
+ end
@@ -15,86 +15,29 @@ module MarkdownParser
15
15
 
16
16
  def self.apply_line(line)
17
17
  line = EscapeUtils.unescape_html(line)
18
-
19
18
  before_parse :ensure_to_close_list, line
20
-
21
- return "#{line}\n" if @state[:code_tag_opened] and !line.match(/^```$/)
22
-
23
- case line
24
- when /^#+\s/ # titles
25
- apply_header(line)
26
- when /^(------|======)\s*$/ # alternative titles
27
- apply_header(line)
28
- when /^(---|___|\*\*\*)\s*$/ # line separator
29
- "<hr />"
30
- when /^>\s.+$/ # blockqoutes
31
- apply_blockquote(line)
32
- when /^```.*/ # code
33
- apply_code(line)
34
- when /^(-|\*|\+|\d+)\s.+/ # list item
35
- apply_list(line)
36
- else
37
- line.empty? ? line : "#{line}"
38
- end
19
+ MarkdownParser::LineStyle.apply(line)
39
20
  end
40
21
 
41
22
  def self.apply_inline_style(line)
42
- # **strong**
43
- line.gsub!(/\*\*(?<word>[^\*]*)\*\*/, "<strong>\\k<word></strong>")
44
- # _em_
45
- line.gsub!(/\_(?<word>[^_]*)\_/, "<em>\\k<word></em>")
46
- # `code`
47
- line.gsub!(/`(?<word>[^`]*)`/, "<code>\\k<word></code>")
48
- # [alt message](image_url)
49
- line.gsub!(/!\[(?<alt>[^\]]*)\]\((?<link>[^\)]*)\)/, '<img src="\k<link>" alt="\k<alt>" />')
50
- # [text](limk)
51
- line.gsub!(/\[(?<text>[^\]]*)\]\((?<link>[^\)]*)\)/, '<a href="\k<link>">\k<text></a>')
52
-
23
+ MarkdownParser::InlineStyle.apply(line)
24
+ # this line adds the last enqueued string for closing tags, like </ul>
53
25
  append_queue line
54
26
  end
55
27
 
56
- # haeder method
57
- def self.apply_header(line)
58
- depth = title_depth(line)
59
- return line if depth == 0
60
- line.gsub(eval("/^#{'#' * depth}\s/"), "<h#{depth}>").gsub(/$/, "</h#{depth}>")
61
- end
62
-
63
- # blockquote method
64
- def self.apply_blockquote(line)
65
- "<p><blockquote>#{line[2..-1]}</blockquote></p>"
66
- end
67
-
68
- # multi-line code method
69
- def self.apply_code(line)
70
- line = @state[:code_tag_opened] ? "</code></pre>" : "<pre><code>"
71
- toggle(:code_tag_opened)
72
- line
73
- end
74
-
75
- # list item method, also add ul tag when is the first to be added
76
- def self.apply_list(line)
77
- line = "<li>#{line[2..-1]}</li>"
78
- line = "<ul>" + line unless @state[:last_was_list]
79
- toggle(:list_opened)
80
- @state[:last_was_list] = true
81
- line
82
- end
83
-
84
28
  # toggle boolean variables
85
29
  def self.toggle(symbol)
86
30
  @state[symbol] = !@state[symbol]
87
31
  end
88
32
 
89
- # define how many #'s a string has'
90
- def self.title_depth(str)
91
- str.match(/^#+\s*/).to_s.size - 1
33
+ # set the new value to index of symbol in @state
34
+ def self.set(symbol, value)
35
+ @state[symbol] = value
92
36
  end
93
37
 
94
38
  # adds at the beginning of the line the string in the @state[:to_append]
95
39
  def self.append_queue(line)
96
- to_append = @state[:to_append]
97
- @state[:to_append] = nil
40
+ to_append, @state[:to_append] = @state[:to_append], nil
98
41
  to_append.nil? ? line : to_append + line
99
42
  end
100
43
 
@@ -110,5 +53,10 @@ module MarkdownParser
110
53
  @state[:to_append] = "</ul>"
111
54
  end
112
55
  end
56
+
57
+ def self.state
58
+ @state
59
+ end
60
+
113
61
  end
114
62
  end
@@ -2,6 +2,8 @@ module MarkdownParser
2
2
  require 'escape_utils'
3
3
 
4
4
  require_relative './markdown_parser/rules'
5
+ require_relative './markdown_parser/inline_style'
6
+ require_relative './markdown_parser/line_style'
5
7
 
6
8
  def self.parse(plain_text)
7
9
  text = EscapeUtils.escape_html(plain_text)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0.alpha1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Villagrana
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-18 00:00:00.000000000 Z
11
+ date: 2019-02-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A gem that parses your markdown to html
14
14
  email: 'ricardovillagranal@gmail.com '
@@ -17,6 +17,8 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/markdown_parser.rb
20
+ - lib/markdown_parser/inline_style.rb
21
+ - lib/markdown_parser/line_style.rb
20
22
  - lib/markdown_parser/rules.rb
21
23
  homepage: https://github.com/ricvillagrana/mardown_parser
22
24
  licenses:
@@ -33,9 +35,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
35
  version: '0'
34
36
  required_rubygems_version: !ruby/object:Gem::Requirement
35
37
  requirements:
36
- - - ">="
38
+ - - ">"
37
39
  - !ruby/object:Gem::Version
38
- version: '0'
40
+ version: 1.3.1
39
41
  requirements: []
40
42
  rubygems_version: 3.0.1
41
43
  signing_key: