devcenter-parser 1.2.1 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ require 'sanitize'
6
6
 
7
7
  module DevcenterParser
8
8
 
9
- VERSION = '1.2.1'
9
+ VERSION = '1.3.1'
10
10
 
11
11
  AVAILABLE_FLAVOURS = [:github, :maruku]
12
12
 
@@ -90,20 +90,22 @@ module DevcenterParser
90
90
 
91
91
  def self.maruku_underscores_to_dashes_in_subheader_anchors(doc)
92
92
  doc.css("h2,h3,h4,h5,h6").each do |node|
93
- if node.attributes['id'] && node.attributes['id'].value
94
- node.attributes['id'].value = node.attributes['id'].value.gsub(/_+/,'-')
95
- end
93
+ node['id'] = subheader_id(node.content)
96
94
  end
97
95
  doc
98
96
  end
99
97
 
100
98
  def self.github_underscores_to_dashes_in_subheader_anchors(doc)
101
99
  doc.css("h2,h3,h4,h5,h6").each do |node|
102
- node['id'] = node.content.to_s.downcase.gsub(/\W/, '-')
100
+ node['id'] = subheader_id(node.content)
103
101
  end
104
102
  doc
105
103
  end
106
104
 
105
+ def self.subheader_id(content)
106
+ content.to_s.downcase.gsub(/\W+/, '-').gsub(/\A-+|-+\Z/, '')
107
+ end
108
+
107
109
  def self.github_parse_special_blocks(doc)
108
110
  doc.css('blockquote>p:first').each do |node|
109
111
  if match = node.inner_html.match(/\A\W*(callout|warning|note)\W/)
@@ -116,7 +118,7 @@ module DevcenterParser
116
118
 
117
119
  def self.convert_to_article_links_all_relative_links_with_missing_initial_slashes(doc)
118
120
  doc.css('a').each do |node|
119
- unless node['href'] =~ /\Ahttp|\A\/|\Amailto\:|\A#/
121
+ unless node['href'].nil? || node['href'] =~ /\Ahttp|\A\/|\Amailto\:|\A#/
120
122
  node['href'] = "/articles/#{node['href']}"
121
123
  end
122
124
  end
@@ -24,33 +24,40 @@ describe 'DevcenterParser' do
24
24
  it 'respects existing ids' do
25
25
  md = '<strong id="foo">clean</strong>'
26
26
  assert_maruku_result md, '<strong id="foo">clean</strong>'
27
+ assert_github_result md, '<p><strong id="foo">clean</strong></p>'
27
28
  end
28
29
 
29
30
  it 'removes script tags and their content' do
30
31
  md = '<strong>clean<script>alert("hack!")</script></strong>'
31
32
  assert_maruku_result md, '<strong>clean</strong>'
33
+ assert_github_result md, '<p><strong>clean</strong></p>'
32
34
  end
33
35
 
34
- it 'github markdown includes ids in subheaders' do
36
+ it 'includes ids in subheaders' do
35
37
  md = <<-MARKDOWN
36
38
  ## Foo Bar Header 123
37
39
 
38
40
  Foo bar content
39
41
  MARKDOWN
40
42
  assert DevcenterParser.to_html(md, :github).include?('<h2 id="foo-bar-header-123">Foo Bar Header 123</h2>')
43
+ assert DevcenterParser.to_html(md, :maruku).include?('<h2 id="foo-bar-header-123">Foo Bar Header 123</h2>')
41
44
  end
42
45
 
43
- it 'maruku markdown includes ids in subheaders' do
44
- md = <<-MARKDOWN
45
- ## Foo Bar Header 123
46
+ it 'generates ids replacing inner non-alphanum chars with dashes' do
47
+ ['Foo Bar', 'Foo-Bar', 'Foo#bar', 'Foo##Bar', 'Foo##Bar', '-$Foo##Bar$-'].each do |title|
48
+ md = <<-MARKDOWN
49
+ ## #{title}
46
50
 
47
51
  Foo bar content
48
- MARKDOWN
49
- assert DevcenterParser.to_html(md, :maruku).include?('<h2 id="foo-bar-header-123">Foo Bar Header 123</h2>')
52
+ MARKDOWN
53
+ assert DevcenterParser.to_html(md, :github).include?("<h2 id=\"foo-bar\">#{title}</h2>"), "GitHub with title #{title}: " + DevcenterParser.to_html(md, :github)
54
+ assert DevcenterParser.to_html(md, :maruku).include?("<h2 id=\"foo-bar\">#{title}</h2>"), "Maruku: " + DevcenterParser.to_html(md, :maruku)
55
+ end
50
56
  end
51
57
 
52
- it 'github markdown supports regular block quotes without callout|warning|note' do
53
- md = <<-MARKDOWN
58
+ describe 'github markdown' do
59
+ it 'github markdown supports regular block quotes without callout|warning|note' do
60
+ md = <<-MARKDOWN
54
61
  Testing
55
62
 
56
63
  > not a callout
@@ -58,9 +65,9 @@ Testing
58
65
  > normal
59
66
 
60
67
  And that's it.
61
- MARKDOWN
68
+ MARKDOWN
62
69
 
63
- html = <<-HTML
70
+ html = <<-HTML
64
71
  <p>Testing</p>
65
72
 
66
73
  <blockquote>
@@ -70,11 +77,11 @@ normal</p>
70
77
  </blockquote>
71
78
 
72
79
  <p>And that's it.</p>
73
- HTML
80
+ HTML
74
81
 
75
- assert_github_result(md, html)
82
+ assert_github_result(md, html)
76
83
 
77
- md = <<-MARKDOWN
84
+ md = <<-MARKDOWN
78
85
  Testing
79
86
 
80
87
  > calloutnonono
@@ -82,9 +89,9 @@ Testing
82
89
  > normal
83
90
 
84
91
  And that's it.
85
- MARKDOWN
92
+ MARKDOWN
86
93
 
87
- html = <<-HTML
94
+ html = <<-HTML
88
95
  <p>Testing</p>
89
96
 
90
97
  <blockquote>
@@ -94,14 +101,14 @@ normal</p>
94
101
  </blockquote>
95
102
 
96
103
  <p>And that's it.</p>
97
- HTML
104
+ HTML
98
105
 
99
- assert_github_result(md, html)
100
- end
106
+ assert_github_result(md, html)
107
+ end
101
108
 
102
- it 'github markdown supports "> callout" and ">callout" and parses inner markdown' do
103
- mds = []
104
- mds << <<-MARKDOWN
109
+ it 'github markdown supports "> callout" and ">callout" and parses inner markdown' do
110
+ mds = []
111
+ mds << <<-MARKDOWN
105
112
  Testing
106
113
 
107
114
  > callout
@@ -109,9 +116,9 @@ Testing
109
116
  > normal
110
117
 
111
118
  And that's it.
112
- MARKDOWN
119
+ MARKDOWN
113
120
 
114
- mds << <<-MARKDOWN
121
+ mds << <<-MARKDOWN
115
122
  Testing
116
123
 
117
124
  >callout
@@ -119,9 +126,9 @@ Testing
119
126
  >normal
120
127
 
121
128
  And that's it.
122
- MARKDOWN
129
+ MARKDOWN
123
130
 
124
- html = <<-HTML
131
+ html = <<-HTML
125
132
  <p>Testing</p>
126
133
 
127
134
  <div class="callout">
@@ -130,16 +137,16 @@ normal</p>
130
137
  </div>
131
138
 
132
139
  <p>And that's it.</p>
133
- HTML
140
+ HTML
134
141
 
135
- mds.each do |md|
136
- assert_github_result(md, html)
142
+ mds.each do |md|
143
+ assert_github_result(md, html)
144
+ end
137
145
  end
138
- end
139
146
 
140
- it 'github markdown supports "> callout" and ">callout", parses inner markdown and allows paragraphs' do
141
- mds = []
142
- mds << <<-MARKDOWN
147
+ it 'github markdown supports "> callout" and ">callout", parses inner markdown and allows paragraphs' do
148
+ mds = []
149
+ mds << <<-MARKDOWN
143
150
  Testing
144
151
 
145
152
  > callout
@@ -148,9 +155,9 @@ Testing
148
155
  > normal
149
156
 
150
157
  And that's it.
151
- MARKDOWN
158
+ MARKDOWN
152
159
 
153
- mds << <<-MARKDOWN
160
+ mds << <<-MARKDOWN
154
161
  Testing
155
162
 
156
163
  >callout
@@ -159,9 +166,9 @@ Testing
159
166
  >normal
160
167
 
161
168
  And that's it.
162
- MARKDOWN
169
+ MARKDOWN
163
170
 
164
- html = <<-HTML
171
+ html = <<-HTML
165
172
  <p>Testing</p>
166
173
 
167
174
  <div class="callout">
@@ -171,10 +178,11 @@ And that's it.
171
178
  </div>
172
179
 
173
180
  <p>And that's it.</p>
174
- HTML
181
+ HTML
175
182
 
176
- mds.each do |md|
177
- assert_github_result(md, html)
183
+ mds.each do |md|
184
+ assert_github_result(md, html)
185
+ end
178
186
  end
179
187
  end
180
188
 
@@ -202,6 +210,12 @@ And that's it.
202
210
  end
203
211
  end
204
212
 
213
+ it 'does not add href attribute to links where it does not exist' do
214
+ md = '<a name="heh"></a>'
215
+ assert_maruku_result md, md
216
+ assert_github_result md, "<p>#{md}</p>"
217
+ end
218
+
205
219
  end
206
220
 
207
221
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devcenter-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-03 00:00:00.000000000 Z
12
+ date: 2013-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: maruku