-
Notifications
You must be signed in to change notification settings - Fork 1
/
downmark_it.rb
197 lines (177 loc) · 4.73 KB
/
downmark_it.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# =Overview
# DownmarkIt is a library to convert HTML to markdown, based on Hpricot[http://github.com/hpricot/hpricot/].
#
# =Motivation
# While working on our company's new CMS, I needed to parse HTML back to markdown and surprisngly there wasn't any solution that could fit our enviroment, so I decided to make my own and share it :)
#
# =Usage
# Make sure you install Hpricot[http://github.com/hpricot/hpricot/] first, then require the library in your application, if you are using the library in a rails application, just place it in your lib folder, then use this method to convert HTML into markdown.
# markdown = DownmarkIt.to_markdown(html)
#
# =Features
# This library supports variable header tags, horizontal rulers, emphasis, strong, links, images, blockquotes, code, unordered lists(nested) and ordered lists(nested)
#
# =WARNING
# Currently DownmarkIt does not support ul tags inside ol tags or vice versa, maybe in the future i will add it ;)
#
# =License
# This code is licensed under MIT License
require 'Hpricot'
module DownmarkIt
# TODO: Add nested unordered lists inside ordered list and vice versa support
def self.to_markdown(html)
raw = Hpricot(html.gsub(/(\n|\r|\t)/, ""))
# headers
(raw/"/<h\d>/").each do |header|
if(header.name.match(/^h\d$/))
header_level = header.name.match(/\d/).to_s.to_i
header.swap("#{"#" * header_level} #{header.inner_html}\r\n")
end
end
# horizontal rulers
(raw/"hr").each do |hruler|
hruler.swap("\r\n---\r\n")
end
# emphasis
(raw/"em").each do |em|
if(em.name == "em")
em.swap("_#{em.inner_html}_")
end
end
# strong
(raw/"strong").each do |strong|
if(strong.name == "strong")
strong.swap("**#{strong.inner_html}**")
end
end
# links (anchors)
(raw/"a").each do |anchor|
if(anchor.name=="a")
if(anchor.inner_html != "")
anchor.swap("[#{anchor.inner_html}](#{anchor['href']}#{" \"#{anchor['title']}\"" if anchor['title']})")
else
anchor.swap("<#{anchor['href']}>")
end
end
end
# image
(raw/"img").each do |image|
image.swap("![#{image['alt']}](#{image['src']}#{" \"#{image['title']}\"" if image['title']})")
end
# blockquote
(raw/"blockquote").each do |qoute|
if qoute.name == "blockquote"
qoute.swap("> #{nested_qoute(qoute)}")
end
end
# code
(raw/"code").each do |code|
if code.name == "code"
code.swap("``#{code.inner_html}``")
end
end
# unordered list
(raw/"ul").each do |ul|
if ul.name == "ul"
(ul/">li").each do |li|
if li.name == "li"
nli = nested_ul(li, 0)
if (nli.match(/ - /))
li_inner = (li.inner_text.match(/^\r\n/))?("#{li.inner_text.gsub(/^\r\n/, "")}\r\n"):("- #{li.inner_text}\r\n")
li.swap("#{li_inner}")
else
li.swap("- #{nli}\r\n")
end
end
end
ul.swap("#{ul.inner_html}")
end
end
# ordered list
(raw/"ol").each do |ol|
if ol.name == "ol"
level = 0
(ol/">li").each do |li|
if li.name == "li"
nli = nested_ol(li, 0)
if (nli.match(/ \d+\. /))
li_inner = (li.inner_text.match(/^\r\n/))?("#{li.inner_text.gsub(/^\r\n/, "")}\r\n"):("#{level+=1}. #{li.inner_text}\r\n")
li.swap("#{li_inner}")
else
li.swap("#{level+=1}. #{nli}\r\n")
end
end
end
ol.swap("#{ol.inner_html}")
end
end
# lines
(raw/"p").each do |p|
if p.name == "p"
p.swap("\r\n#{p.inner_text}\r\n")
end
end
return raw.to_s
end
private
def self.nested_qoute(qoute)
nqoute = qoute.at("blockquote")
unless(nqoute.nil?)
nnqoute = nested_qoute(nqoute)
"> #{nnqoute}"
else
qoute.inner_html
end
end
def self.nested_ul(li, level)
ul = li.at("ul")
unless(ul.nil?)
nested_uli(ul, level + 1)
else
li.inner_html
end
end
def self.nested_uli(li, level)
nli = li.at("li")
unless(nli.nil?)
(li/">li").each do |cnli|
nnli = nested_ul(cnli, level + 1)
if (nnli.match(/ - /))
inner_li = (cnli.inner_text.match(/^\r\n/))?(""):(cnli.inner_text)
cnli.swap "\r\n#{" " * level}- #{inner_li}" unless inner_li == ""
else
cnli.swap "\r\n#{" " * level}- #{nnli}"
end
end
li.inner_html
else
li.inner_html
end
end
def self.nested_ol(li, level)
ol = li.at("ol")
unless(ol.nil?)
nested_oli(ol, level + 1)
else
li.inner_html
end
end
def self.nested_oli(li, level)
nli = li.at("li")
unless(nli.nil?)
nlevel = 0
(li/">li").each do |cnli|
nnli = nested_ol(cnli, level + 1)
if (nnli.match(/ \d+. /))
inner_li = (cnli.inner_text.match(/^\r\n/))?(""):(cnli.inner_text)
cnli.swap "\r\n#{" " * level}#{nlevel+=1}. #{inner_li}" unless inner_li == ""
else
cnli.swap "\r\n#{" " * level}#{nlevel+=1}. #{nnli}"
end
end
li.inner_html
else
li.inner_html
end
end
end