govspeak 0.8.15 → 0.8.16

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -106,7 +106,7 @@ creates an contact box
106
106
 
107
107
  creates an address box
108
108
 
109
- <div class="address vcard"><div class="adr org fn"><p>
109
+ <div class="address"><div class="adr org fn"><p>
110
110
  Hercules House
111
111
  <br>Hercules Road
112
112
  <br>London SE1 7DU
@@ -1,11 +1,15 @@
1
1
  require 'kramdown'
2
2
  require 'govspeak/header_extractor'
3
+ require 'kramdown/parser/kramdown_with_automatic_external_links'
3
4
  require 'htmlentities'
4
5
 
5
6
  module Govspeak
6
7
 
7
8
  class Document
8
9
 
10
+ Parser = Kramdown::Parser::KramdownWithAutomaticExternalLinks
11
+ PARSER_CLASS_NAME = Parser.name.split("::").last
12
+
9
13
  @@extensions = []
10
14
 
11
15
  attr_accessor :images
@@ -16,7 +20,8 @@ module Govspeak
16
20
 
17
21
  def initialize(source, options = {})
18
22
  @source = source ? source.dup : ""
19
- @options = options.merge(entity_output: :symbolic)
23
+ Parser.document_domains = options.delete(:document_domains)
24
+ @options = {input: PARSER_CLASS_NAME, entity_output: :symbolic}.merge(options)
20
25
  @images = []
21
26
  super()
22
27
  end
@@ -1,3 +1,3 @@
1
1
  module Govspeak
2
- VERSION = "0.8.15"
2
+ VERSION = "0.8.16"
3
3
  end
@@ -0,0 +1,27 @@
1
+ require "uri"
2
+
3
+ module Kramdown
4
+ module Parser
5
+ class KramdownWithAutomaticExternalLinks < Kramdown::Parser::Kramdown
6
+ class << self
7
+ attr_writer :document_domains
8
+ def document_domains
9
+ @document_domains || %w(www.gov.uk)
10
+ end
11
+ end
12
+
13
+ def add_link(el, href, title, alt_text = nil)
14
+ begin
15
+ host = URI.parse(href).host
16
+ unless host.nil? || (self.class.document_domains.compact.include?(host))
17
+ el.attr['rel'] = 'external'
18
+ end
19
+ rescue URI::InvalidURIError, URI::InvalidComponentError
20
+ # it's safe to ignore these very *specific* exceptions
21
+ end
22
+ super
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -9,6 +9,8 @@ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
9
9
  require 'test_helper'
10
10
  require 'govspeak_test_helper'
11
11
 
12
+ require 'ostruct'
13
+
12
14
  class GovspeakTest < Test::Unit::TestCase
13
15
  include GovspeakTestHelper
14
16
 
@@ -160,13 +162,13 @@ Teston
160
162
  assert_text_output "I am very helpful"
161
163
  end
162
164
 
163
- test_given_govspeak "This is a [link](http://www.google.com) isn't it?" do
164
- assert_html_output '<p>This is a <a href="http://www.google.com">link</a> isn&rsquo;t it?</p>'
165
+ test_given_govspeak "This is a [link](http://www.gov.uk) isn't it?" do
166
+ assert_html_output '<p>This is a <a href="http://www.gov.uk">link</a> isn&rsquo;t it?</p>'
165
167
  assert_text_output "This is a link isn’t it?"
166
168
  end
167
169
 
168
- test_given_govspeak "This is a [link with an at sign in it](http://www.google.com/@dg/@this) isn't it?" do
169
- assert_html_output '<p>This is a <a href="http://www.google.com/@dg/@this">link with an at sign in it</a> isn&rsquo;t it?</p>'
170
+ test_given_govspeak "This is a [link with an at sign in it](http://www.gov.uk/@dg/@this) isn't it?" do
171
+ assert_html_output '<p>This is a <a href="http://www.gov.uk/@dg/@this">link with an at sign in it</a> isn&rsquo;t it?</p>'
170
172
  assert_text_output "This is a link with an at sign in it isn’t it?"
171
173
  end
172
174
 
@@ -187,6 +189,68 @@ Teston
187
189
  assert_html_output '<p><a href="http://x.com" rel="external">an xx link</a></p>'
188
190
  end
189
191
 
192
+ test_given_govspeak "[internal link](http://www.gov.uk)" do
193
+ assert_html_output '<p><a href="http://www.gov.uk">internal link</a></p>'
194
+ end
195
+
196
+ test_given_govspeak "[link with no host is assumed to be internal](/)" do
197
+ assert_html_output '<p><a href="/">link with no host is assumed to be internal</a></p>'
198
+ end
199
+
200
+ test_given_govspeak "[internal link with rel attribute keeps it](http://www.gov.uk){:rel='next'}" do
201
+ assert_html_output '<p><a href="http://www.gov.uk" rel="next">internal link with rel attribute keeps it</a></p>'
202
+ end
203
+
204
+ test_given_govspeak "[external link without x markers](http://www.google.com)" do
205
+ assert_html_output '<p><a rel="external" href="http://www.google.com">external link without x markers</a></p>'
206
+ end
207
+
208
+ test_given_govspeak "[external link with rel attribute](http://www.google.com){:rel='next'}" do
209
+ assert_html_output '<p><a rel="next" href="http://www.google.com">external link with rel attribute</a></p>'
210
+ end
211
+
212
+ test_given_govspeak "Text before [an external link](http://www.google.com)" do
213
+ assert_html_output '<p>Text before <a rel="external" href="http://www.google.com">an external link</a></p>'
214
+ end
215
+
216
+ test_given_govspeak "[An external link](http://www.google.com) with text afterwards" do
217
+ assert_html_output '<p><a rel="external" href="http://www.google.com">An external link</a> with text afterwards</p>'
218
+ end
219
+
220
+ test_given_govspeak "Text before [an external link](http://www.google.com) and text afterwards" do
221
+ assert_html_output '<p>Text before <a rel="external" href="http://www.google.com">an external link</a> and text afterwards</p>'
222
+ end
223
+
224
+ test "should be able to override default 'document_domains' option" do
225
+ html = Govspeak::Document.new("[internal link](http://www.not-external.com)", document_domains: %w(www.not-external.com)).to_html
226
+ refute html.include?('rel="external"'), "should not consider www.not-external.com as an external url"
227
+ end
228
+
229
+ test "should be able to supply multiple domains for 'document_domains' option" do
230
+ html = Govspeak::Document.new("[internal link](http://www.not-external-either.com)", document_domains: %w(www.not-external.com www.not-external-either.com)).to_html
231
+ refute html.include?('rel="external"'), "should not consider www.not-external-either.com as an external url"
232
+ end
233
+
234
+ test "should be able to override default 'input' option" do
235
+ html = Govspeak::Document.new("[external link](http://www.external.com)", input: "kramdown").to_html
236
+ refute html.include?('rel="external"'), "should not automatically add rel external attribute"
237
+ end
238
+
239
+ test "should be able to override default 'entity output' option" do
240
+ html = Govspeak::Document.new("&amp;", entity_output: :numeric).to_html
241
+ assert html.include?("&#38;")
242
+ end
243
+
244
+ test "should be assume link with invalid uri is internal" do
245
+ html = Govspeak::Document.new("[link](:invalid-uri)").to_html
246
+ refute html.include?('rel="external"')
247
+ end
248
+
249
+ test "should be assume link with invalid uri component is internal" do
250
+ html = Govspeak::Document.new("[link](mailto://www.example.com)").to_html
251
+ refute html.include?('rel="external"')
252
+ end
253
+
190
254
  # Regression test - the surrounded_by helper doesn't require the closing x
191
255
  # so 'xaa' was getting picked up by the external link helper above
192
256
  # TODO: review whether we should require closing symbols for these extensions
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: govspeak
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.8.15
5
+ version: 0.8.16
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Griffiths
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-06-18 00:00:00 Z
14
+ date: 2012-07-06 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: kramdown
@@ -71,6 +71,7 @@ extra_rdoc_files: []
71
71
 
72
72
  files:
73
73
  - lib/govspeak.rb
74
+ - lib/kramdown/parser/kramdown_with_automatic_external_links.rb
74
75
  - lib/govspeak/header_extractor.rb
75
76
  - lib/govspeak/version.rb
76
77
  - README.md
@@ -92,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
93
  requirements:
93
94
  - - ">="
94
95
  - !ruby/object:Gem::Version
95
- hash: 4415467989565136347
96
+ hash: -3802672710997913836
96
97
  segments:
97
98
  - 0
98
99
  version: "0"
@@ -101,14 +102,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
102
  requirements:
102
103
  - - ">="
103
104
  - !ruby/object:Gem::Version
104
- hash: 4415467989565136347
105
+ hash: -3802672710997913836
105
106
  segments:
106
107
  - 0
107
108
  version: "0"
108
109
  requirements: []
109
110
 
110
111
  rubyforge_project:
111
- rubygems_version: 1.8.12
112
+ rubygems_version: 1.8.24
112
113
  signing_key:
113
114
  specification_version: 3
114
115
  summary: Markup language for single domain