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)
4848 end
4949
5050 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 )
5158 if filename . empty?
5259 uri = "https://gist.githubusercontent.com/#{ gist_id } /raw"
5360 else
5461 uri = "https://gist.githubusercontent.com/#{ gist_id } /raw/#{ filename } "
5562 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
5868 end
59-
6069 end
6170 end
6271end
Original file line number Diff line number Diff line change 9494 end
9595
9696 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>/ )
9898 end
9999
100100 it "produces the correct noscript tag" do
101101 expect ( output ) . to match ( /<noscript><pre><test>true<\/ test><\/ pre><\/ noscript>\n / )
102102 end
103103 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+
104121 end
105122
106123
You can’t perform that action at this time.
0 commit comments