Sha256: f218541ff01fd801cefbe56d5292af39ccb69972a36ae4e0c815ef9854d461dc

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# Classes used by the GitHub server task
#
# Although we don't actually use this, without it: 'uninitialized constant GitHub'
require 'github/markup'

require 'html/pipeline'
require 'html/pipeline/rouge_filter'
require 'webrick'

module Nox
  class GitHubRenderer < WEBrick::HTTPServlet::AbstractServlet

    def initialize config, filename
      @filename = filename
      @pipeline = HTML::Pipeline.new [
        HTML::Pipeline::MarkdownFilter,
        HTML::Pipeline::RougeFilter
      ]
    end

    def do_GET req, rsp
      begin
        rsp.status = 200
        rsp['Content-Type'] = 'text/html'
        styles = []
        styles << %{<link crossorigin="anonymous" href="/style.css" media="all" rel="stylesheet"> }
        styles << %{ <style> body { padding: 25px 25px 25px 25px; } </style> }
        render = @pipeline.call File.read(@filename)
        rsp.body = <<-BODY
            <html>
              <head>#{styles.join('')}</head>
              <body>
                <article class="markdown-body">
        #{render[:output]}
                </article>
              </body>
            </html>
        BODY
      rescue
        rsp.status = 400
        rsp['Content-Type'] = 'text/plain'
        rsp.body = "HTML Pipeline failed: #{$!}"
      end
    end
  end

  class Styles < WEBrick::HTTPServlet::AbstractServlet
    def do_GET req, rsp
      begin
        rsp.status = 200
        rsp['Content-Type'] = 'text/css'
        rsp.body = ['normalize', 'github'].map do |style|
          File.read(File.absolute_path("../../#{style}.css", File.dirname(__FILE__)))
        end.join("\n")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nox-0.1.0 lib/nox/ghserver.rb