kpumuk commented on code in PR #14:
URL: https://github.com/apache/thrift-website/pull/14#discussion_r3089244868


##########
_plugins/remote_snippets.rb:
##########
@@ -21,44 +22,84 @@ def initialize(tag_name, text, tokens)
 
   def render(context)
     title = context.registers[:site].config['title']
-    prefix = context.registers[:site].config['gitbox_url']
-    url = "#{prefix};a=blob_plain;hb=HEAD;f=#{@path}"
-    pretty_url = "#{prefix};a=blob;hb=HEAD;f=#{@path}"
-    content = ""
-    if @range == "all"
-      URI.open(url) {|f| content = f.read }
-    else
-      rangenums = @range.split(",", 2)
-      first = 0
-      second = 0
-      if rangenums.length() == 2
-        first = Integer(rangenums[0])
-        second = Integer(rangenums[1]) - first
-        URI.open(url) {|f| content = 
f.each_line.drop(first).take(second).join() }
-      else
-        first = Integer(rangenums[0])
-        URI.open(url) {|f| content = f.each_line.drop(first).join() }
-      end
-    end
+    source = snippet_source(context)
+    content = read_content(source[:read_path], source[:local])
     snippet_type = "snippet"
     if @type == "direct"
-      content = Kramdown::Document.new(content).to_html
+      content = render_markdown(content, context)
       snippet_type = "page"
     else
       content = content.force_encoding("utf-8")
       formatter = Rouge::Formatters::HTMLLegacy.new
       lexer = Rouge::Lexer.find_fancy(@type, content)
       content = formatter.format(lexer.lex(content))
     end
+    source_link = "<a 
href=\"#{source[:pretty_url]}\">#{CGI.escapeHTML(@path)}</a>"
     #content = CGI::escapeHTML(content)
     return """
 #{content}
 <p class=\"snippet_footer\">This #{snippet_type} was generated by #{title}'s 
<strong>source tree docs</strong>:
-<a href=\"#{pretty_url}\">#{@path}</a>
+#{source_link}
 </p>
       """
   end
+
+  private
+
+  def snippet_source(context)
+    prefix = context.registers[:site].config['gitbox_url']
+    checkout = local_checkout_root(context)
+    return remote_source(prefix) unless checkout
+
+    local_path = File.expand_path(@path, checkout)
+    unless local_path.start_with?("#{checkout}/") && File.file?(local_path)
+      raise ArgumentError, "remote_snippet local checkout path is not a file: 
#{local_path}"
+    end
+
+    {
+      local: true,
+      read_path: local_path,
+      pretty_url: "#{prefix};a=blob;hb=HEAD;f=#{@path}"
+    }
+  end
+
+  def read_content(path, local)
+    lines = if local
+      File.readlines(path)
+    else
+      URI.open(path) { |file| file.each_line.to_a }
+    end
+    return lines.join() if @range == "all"
+
+    rangenums = @range.split(",", 2)
+    first = Integer(rangenums[0])
+    return lines.drop(first).join() if rangenums.length() == 1
+
+    length = Integer(rangenums[1]) - first
+    lines.drop(first).take(length).join()
+  end
+
+  def local_checkout_root(context)
+    checkout = ENV["THRIFT_CHECKOUT"] || 
context.registers[:site].config["thrift_checkout"]
+    return nil if checkout.nil? || checkout.empty?
+
+    File.expand_path(checkout)
+  end
+
+  def remote_source(prefix)
+    {
+      local: false,
+      read_path: "#{prefix};a=blob_plain;hb=HEAD;f=#{@path}",
+      pretty_url: "#{prefix};a=blob;hb=HEAD;f=#{@path}"
+    }
+  end
+
+  def render_markdown(content, context)
+    context.registers[:site]
+      .find_converter_instance(Jekyll::Converters::Markdown)
+      .convert(content.to_s)
+      .gsub(/(<(?:div|pre)\s+)class="highlight"/, '\1class="codehilite"')

Review Comment:
   The reason for that is the difference between legacy formatter (pygments) 
and modern formatter (rouge). In the script for remote snippets we use 
`Rouge::Formatters::HTMLLegacy`, which generates highlighted code using 
`pygments`. In all other places Jekyll uses modern rouge, which uses different 
classes (which are obviously not styled on the Thrift website).
   
   Interestingly, the website itself does not have any highlighted code in code 
fences, so this issue never surfaced before. I will check what can be done 
about it...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to