File tree Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -48,15 +48,24 @@ def gist_script_tag(gist_id, filename = nil)
48
48
end
49
49
50
50
def gist_noscript_tag ( gist_id , filename = nil )
51
+ code = fetch_raw_code ( gist_id , filename )
52
+ if !code . nil?
53
+ "<noscript><pre>#{ CGI . escapeHTML ( code ) } </pre></noscript>"
54
+ end
55
+ end
56
+
57
+ def fetch_raw_code ( gist_id , filename = nil )
51
58
if filename . empty?
52
59
uri = "https://gist.githubusercontent.com/#{ gist_id } /raw"
53
60
else
54
61
uri = "https://gist.githubusercontent.com/#{ gist_id } /raw/#{ filename } "
55
62
end
56
- code = open ( uri ) . read . chomp
57
- "<noscript><pre>#{ CGI . escapeHTML ( code ) } </pre></noscript>"
63
+ begin
64
+ open ( uri ) . read . chomp
65
+ rescue OpenURI ::HTTPError => e
66
+ nil
67
+ end
58
68
end
59
-
60
69
end
61
70
end
62
71
end
Original file line number Diff line number Diff line change 94
94
end
95
95
96
96
it "produces the correct script tag" do
97
- expect ( output ) . to match ( /<script src="https:\/ \/ gist.github.com\/ #{ doc . data [ 'gist_id' ] } .js\? file=#{ doc . data [ 'gist_filename' ] } ">\s <\/ script>\n \n / )
97
+ expect ( output ) . to match ( /<script src="https:\/ \/ gist.github.com\/ #{ doc . data [ 'gist_id' ] } .js\? file=#{ doc . data [ 'gist_filename' ] } ">\s <\/ script>/ )
98
98
end
99
99
100
100
it "produces the correct noscript tag" do
101
101
expect ( output ) . to match ( /<noscript><pre><test>true<\/ test><\/ pre><\/ noscript>\n / )
102
102
end
103
103
end
104
+
105
+ context "with valid gist id and invalid filename" do
106
+ before { stub_request ( :get , "https://gist.githubusercontent.com/#{ gist_id } /raw/#{ gist_filename } " ) . to_return ( status : 404 ) }
107
+ let ( :gist_id ) { "mattr-/24081a1d93d2898ecf0f" }
108
+ let ( :gist_filename ) { "myfile.ext" }
109
+ let ( :content ) { "{% gist #{ gist_id } #{ gist_filename } %}" }
110
+
111
+ it "produces the correct script tag" do
112
+ expect ( output ) . to match ( /<script src="https:\/ \/ gist.github.com\/ #{ gist_id } .js\? file=#{ gist_filename } ">\s <\/ script>/ )
113
+ end
114
+
115
+ it "doesn't produces the noscript tag" do
116
+ expect ( output ) . to_not match ( /<noscript><pre><test>true<\/ test><\/ pre><\/ noscript>\n / )
117
+ end
118
+
119
+ end
120
+
104
121
end
105
122
106
123
You can’t perform that action at this time.
0 commit comments