markdown_parser 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bbb92f0e6efe86ed6f5ab80581e508ceffc4c58c6ed58e8466bad6531711658e
4
+ data.tar.gz: 14726c3fb8783f98da7be7a864d305d81e95722fbb47ed08cba2ef9bb710ce3f
5
+ SHA512:
6
+ metadata.gz: f7194f638141c5084ed407b90c1ec8d23411de3b832197910c6dccaeadcc06191333ebc1e47242c7688d1bffd995f83de08f2142e8a86fddeba3c0087be32df9
7
+ data.tar.gz: c96991af697b6d48cbc5518dfb011afb1850ad162d95bd0ee108bf250ba639139febf5b86ac597dc177d370855f10f65904799d08df8891160852e05e5eb7f54
@@ -0,0 +1,22 @@
1
+ module MarkdownParser
2
+ require 'escape_utils'
3
+
4
+ require_relative './markdown_parser/rules'
5
+
6
+ def self.parse(plain_text)
7
+ text = EscapeUtils.escape_html(plain_text)
8
+ "<div class='markdown-parser'>#{markdown_to_html(text)}</div>"
9
+ end
10
+
11
+ def self.markdown_to_html(text)
12
+ lines = text.split("\n")
13
+ lines << ''
14
+ lines.map do |line|
15
+ apply_rules(line)
16
+ end.join
17
+ end
18
+
19
+ def self.apply_rules(line)
20
+ Rules.apply(line)
21
+ end
22
+ end
@@ -0,0 +1,114 @@
1
+ module MarkdownParser
2
+ module Rules
3
+
4
+ @state = {
5
+ code_tag_opened: false,
6
+ code_tag_hl_opened: false,
7
+ list_opened: false,
8
+ last_was_list: false,
9
+ to_append: nil
10
+ }
11
+
12
+ def self.apply(line)
13
+ apply_inline_style(apply_line(line))
14
+ end
15
+
16
+ def self.apply_line(line)
17
+ line = EscapeUtils.unescape_html(line)
18
+
19
+ 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
39
+ end
40
+
41
+ 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
+
53
+ append_queue line
54
+ end
55
+
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
+ # toggle boolean variables
85
+ def self.toggle(symbol)
86
+ @state[symbol] = !@state[symbol]
87
+ end
88
+
89
+ # define how many #'s a string has'
90
+ def self.title_depth(str)
91
+ str.match(/^#+\s*/).to_s.size - 1
92
+ end
93
+
94
+ # adds at the beginning of the line the string in the @state[:to_append]
95
+ def self.append_queue(line)
96
+ to_append = @state[:to_append]
97
+ @state[:to_append] = nil
98
+ to_append.nil? ? line : to_append + line
99
+ end
100
+
101
+ # execute the method passed as symbol and adds the param to it
102
+ def self.before_parse(symbol, param)
103
+ send(symbol, param)
104
+ end
105
+
106
+ # check if the last line added was a list and if the current line isn't, so, add a </ul>\n
107
+ def self.ensure_to_close_list(line)
108
+ if @state[:last_was_list] && !['- ', '+ ', '* '].include?(line[0,2])
109
+ @state[:last_was_list] = false
110
+ @state[:to_append] = "</ul>"
111
+ end
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ricardo Villagrana
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem that parses your markdown to html
14
+ email: 'ricardovillagranal@gmail.com '
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/markdown_parser.rb
20
+ - lib/markdown_parser/rules.rb
21
+ homepage: https://github.com/ricvillagrana/mardown_parser
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Parses markdown to html
44
+ test_files: []