Update github_blob engine to support displaying stl files (#402)

Update github_blob engine to support displaying stl files (#402)

  • Change raw.github.com to raw.githubusercontent.com

  • Update github_blob support to handle stl files

  • Update github_blob to display stls at 480x360 by default

  • Cleanup rubocop complaints

  • Fix github_blob rspec test

  • Differentiate github from gitlab for stl handling.

diff --git a/lib/onebox/engine/github_blob_onebox.rb b/lib/onebox/engine/github_blob_onebox.rb
index 876b621..9b3934f 100644
--- a/lib/onebox/engine/github_blob_onebox.rb
+++ b/lib/onebox/engine/github_blob_onebox.rb
@@ -15,7 +15,7 @@ module Onebox
         /github\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+)\/blob\/(?<sha1>[^\/]+)\/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?/mi
       end
       def raw_template(m)
-        "https://raw.github.com/#{m[:user]}/#{m[:repo]}/#{m[:sha1]}/#{m[:file]}"
+        "https://raw.githubusercontent.com/#{m[:user]}/#{m[:repo]}/#{m[:sha1]}/#{m[:file]}"
       end
       def title
         Sanitize.fragment(URI.unescape(link).sub(/^https?\:\/\/github\.com\//, ''))
diff --git a/lib/onebox/mixins/git_blob_onebox.rb b/lib/onebox/mixins/git_blob_onebox.rb
index f38e90c..a6c6fbc 100644
--- a/lib/onebox/mixins/git_blob_onebox.rb
+++ b/lib/onebox/mixins/git_blob_onebox.rb
@@ -158,38 +158,46 @@ module Onebox
 
             @file = m[:file]
             @lang = Onebox::FileTypeFinder.from_file_name(m[:file])
-            contents = open(self.raw_template(m), read_timeout: timeout).read
-
-            contents_lines = contents.lines           #get contents lines
-            contents_lines_size = contents_lines.size #get number of lines
-
-            cr = calc_range(m, contents_lines_size)    #calculate the range of lines for output
-            selected_one_liner = cr[:selected_one_liner] #if url is a one-liner calc_range will return it
-            from           = cr[:from]
-            to             = cr[:to]
-            @truncated     = cr[:truncated]
-            range_provided = cr[:range_provided]
-            @cr_results = cr
-
-            if range_provided       #if a range provided (single line or more)
-              if SHOW_LINE_NUMBER
-                lines_result = line_number_helper(contents_lines[(from - 1)..(to - 1)], from, selected_one_liner)  #print code with prefix line numbers in case range provided
-                contents = lines_result[:output]
-                @selected_lines_array = lines_result[:array]
+
+            if @lang == "stl" && link.match(/^https?:\/\/(www\.)?github\.com.*\/blob\//)
+
+              @model_file = @lang.dup
+              @raw = "https://render.githubusercontent.com/view/solid?url=" + self.raw_template(m)
+
+            else
+              contents = open(self.raw_template(m), read_timeout: timeout).read
+
+              contents_lines = contents.lines           #get contents lines
+              contents_lines_size = contents_lines.size #get number of lines
+
+              cr = calc_range(m, contents_lines_size)    #calculate the range of lines for output
+              selected_one_liner = cr[:selected_one_liner] #if url is a one-liner calc_range will return it
+              from           = cr[:from]
+              to             = cr[:to]
+              @truncated     = cr[:truncated]
+              range_provided = cr[:range_provided]
+              @cr_results = cr
+
+              if range_provided       #if a range provided (single line or more)
+                if SHOW_LINE_NUMBER
+                  lines_result = line_number_helper(contents_lines[(from - 1)..(to - 1)], from, selected_one_liner)  #print code with prefix line numbers in case range provided
+                  contents = lines_result[:output]
+                  @selected_lines_array = lines_result[:array]
+                else
+                  contents = contents_lines[(from - 1)..(to - 1)].join()
+                end
+
               else
                 contents = contents_lines[(from - 1)..(to - 1)].join()
               end
 
-            else
-              contents = contents_lines[(from - 1)..(to - 1)].join()
-            end
+              if contents.length > MAX_CHARS    #truncate content chars to limits
+                contents = contents[0..MAX_CHARS]
+                @truncated = true
+              end
 
-            if contents.length > MAX_CHARS    #truncate content chars to limits
-              contents = contents[0..MAX_CHARS]
-              @truncated = true
+              @raw = contents
             end
-
-            @raw = contents
           end
         end
 
@@ -206,7 +214,10 @@ module Onebox
             has_lines: !@selected_lines_array.nil?,
             selected_one_liner: @selected_one_liner,
             cr_results: @cr_results,
-            truncated: @truncated
+            truncated: @truncated,
+            model_file: @model_file,
+            width: 480,
+            height: 360
           }
         end
       end
diff --git a/spec/lib/onebox/engine/github_blob_onebox_spec.rb b/spec/lib/onebox/engine/github_blob_onebox_spec.rb
index 252b088..07f9f45 100644
--- a/spec/lib/onebox/engine/github_blob_onebox_spec.rb
+++ b/spec/lib/onebox/engine/github_blob_onebox_spec.rb
@@ -3,7 +3,7 @@ require "spec_helper"
 describe Onebox::Engine::GithubBlobOnebox do
   before(:all) do
     @link = "https://github.com/discourse/onebox/blob/master/lib/onebox/engine/github_blob_onebox.rb"
-    fake("https://raw.github.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb", response(described_class.onebox_name))
+    fake("https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb", response(described_class.onebox_name))
   end
 
   include_context "engines"
diff --git a/templates/githubblob.mustache b/templates/githubblob.mustache
index 56efefe..7fc1cc9 100644
--- a/templates/githubblob.mustache
+++ b/templates/githubblob.mustache
@@ -1,6 +1,13 @@
 <h4><a href="{{link}}" target="_blank">{{title}}</a></h4>
 {{^has_lines}}
+{{#model_file}}
+<iframe class="render-viewer" width="{{width}}" height="{{height}}" src="{{content}}" sandbox="allow-scripts allow-same-origin allow-top-navigation ">
+Viewer requires iframe.
+</iframe>
+{{/model_file}}
+{{^model_file}}
 <pre><code class='{{lang}}'>{{content}}</code></pre>
+{{/model_file}}
 {{/has_lines}}
 {{#has_lines}}
 {{! This is a template comment  | Sample rules for this box

GitHub sha: ef061af9