Hi, this is my take on a highlight plugin for wireshark data. As I have no idea how to write an extra pygment lexer nor have access to the pygment installation due to using an global pygment I wrote this based on the already existing codeblock.rb in octopress + looking at the sourcecode pygment created. Most likely this could be done way nicer (for example I am abusing the css for http highlighting instead of writing my own) but for me right now this is enough.

wirshark.rb

require './plugins/pygments_code'
require './plugins/raw'

module Jekyll
    class WireShark < Liquid::Block
        include HighlightCode
        include TemplateWrapper
        CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
        Caption = /(\S[\S\s]*)/
        def initialize(tag_name, markup, tokens)
            @caption = nil
            if markup =~ CaptionUrlTitle
                @caption = "<figcaption><span>#{$1}</span><a href='#{$2}'>#{$3 || 'link'}</a></figcaption>"
            elsif markup =~ Caption
                @caption = "<figcaption><span>#{$1}</span></figcaption>\n"
            end
            super
        end

        def render(context)
            output = super
            code = super
            source = "<figure class=\"code\">"
            source += @caption if @caption
            number_list = []
            hex_list = []
            text_list = []
            line_counter = 0
            code.lstrip.rstrip.gsub(/</,'&lt;').split( /\r?\n/ ).each do|line|
                line_list = line.split("  ")
                number = line_list[0]
                text = line_list[2]
                line = line_list[1]
                line_counter += 1
                number_list.push(number)
                hex_list.push(line)
                text_list.push(text)
            end
            source += "<div class=\"highlight\"><table><tbody>\n<tr>\n<td class=\"gutter\">\n<pre class=\"line-numbers\">\n"
            number_list.each do|num|
                source += '<span class="line-number">' + num + "</span>\n"
            end
            source += "</pre></td>\n"
            source += "<td class=\"code\"><pre><code class=\"xml\">"
            hex_list.each do|hex_line|
                source += "<span class=\"line\"><span class=\"na\">" + hex_line + "</span></span>\n"
            end
            source += "</code></pre></td>\n"
            source += "<td class=\"code\"><pre><code class=\"http\">"
            text_list.each do|text|
                source += '<span class="line"><span class="err">' + text + "</span></span>\n"
            end
            source += "</code></pre></td>\n"
            source += "</tr></tbody></table></div></figure>\n"
            source = safe_wrap(source)
            source = context['pygments_prefix'] + source if context['pygments_prefix']
            source = source + context['pygments_suffix'] if context['pygments_suffix']
            source
        end
    end
end

Liquid::Template.register_tag('wireshark', Jekyll::WireShark)

Published

Category

Octopress

Tags