Skip to content

Instantly share code, notes, and snippets.

@s-andringa
Created March 4, 2012 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-andringa/1974746 to your computer and use it in GitHub Desktop.
Save s-andringa/1974746 to your computer and use it in GitHub Desktop.
Meta Code converts itself into a highlighted HTML or PDF document.
require "coderay"
require "pdfkit"
require File.join(File.dirname(__FILE__), "styles/tomorrow_night")
module Meta
class Code
attr_reader :creator
def initialize(options = {})
@creator = options[:creator]
end
def save(format)
raise ArgumentError, "Invalid format: #{format}" unless %w(txt html pdf).include?(format.to_s)
File.open("#{File.basename(__FILE__, ".rb")}.#{format}", "w") { |f| f.write(send(format)) }
end
def pdf
@pdf ||= PDFKit.new(html, page_width: 150, page_height: 150, margin_top: 0, margin_right: 0,
margin_bottom: 0, margin_left: 0).to_pdf
end
def html
@html ||= CodeRay.encode(txt, :ruby, :page, style: :tomorrow_night, line_numbers: false)
end
def txt
@txt ||= "#{credits}\n#{File.read(__FILE__)}"
end
protected
def credits
<<-RB.gsub(/^\s+/, "")
# By #{creator}
# Copyright #{Time.now.year}
RB
end
end
end
Meta::Code.new(creator: "Sjoerd Andringa").save(ARGV[0] || :html)
module CodeRay
module Styles
# A colorful theme using CSS 3 colors (with alpha channel).
class TomorrowNight < Style
register_for :tomorrow_night
code_background = '#1d1f21'
numbers_background = 'hsl(180,65%,90%)'
border_color = 'silver'
normal_color = '#c5c8c6'
CSS_MAIN_STYLES = <<-MAIN # :nodoc:
body {
background-color: #{code_background};
padding: 7mm;
margin: 0;
min-height: 136mm;
}
.CodeRay {
color: #{normal_color};
}
.CodeRay pre {
margin: 0px;
font-family: Monaco;
font-size: 7pt;
font-weight: normal !important;
line-height: 1.2em;
}
span.CodeRay { white-space: pre; border: 0px; padding: 2px; }
table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; }
table.CodeRay td { padding: 2px 4px; vertical-align: top; }
.CodeRay .line-numbers {
background-color: #{numbers_background};
color: gray;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.CodeRay .line-numbers a {
background-color: #{numbers_background} !important;
color: gray !important;
text-decoration: none !important;
}
.CodeRay .line-numbers a:target { color: blue !important; }
.CodeRay .line-numbers .highlighted { color: red !important; }
.CodeRay .line-numbers .highlighted a { color: red !important; }
.CodeRay span.line-numbers { padding: 0px 4px; }
.CodeRay .line { display: block; float: left; width: 100%; }
.CodeRay .code { width: 100%; }
MAIN
TOKEN_COLORS = <<-'TOKENS'
.debug { color: white !important; background: blue !important; }
.annotation { color:#007 }
.attribute-name { color:#b48 }
.attribute-value { color:#700 }
.binary { color:#509 }
.char .content { color:#b5bd68 }
.char .delimiter { color:#b5bd68 }
.char { color:#b5bd68 }
.class { color:#f0c674; }
.class-variable { color:#369 }
.color { color:#0A0 }
.comment { color:#969896 }
.comment .char { color:#969896 }
.comment .delimiter { color:#969896 }
.complex { color:#A08 }
.constant { color:#f0c674; }
.decorator { color:#B0B }
.definition { color:#099; }
.delimiter { color:b5bd68 }
.directive { color:#088; }
.doc { color:#970 }
.doc-string { color:#D42; }
.doctype { color:#34b }
.entity { color:#800; }
.error { color:#F00; background-color:#FAA }
.escape { color:#666 }
.exception { color:#C00; }
.float { color:#60E }
.function { color:#81a2be; }
.global-variable { color:#d70 }
.hex { color:#02b }
.imaginary { color:#f00 }
.include { color:#B44; }
.inline { color: #b5bd68 }
.inline-delimiter { color: #de935f }
.instance-variable { color:#cc6666 }
.integer { color:#de935f }
.key .char { color: #60f }
.key .delimiter { color: #404 }
.key { color: #606 }
.keyword { color:#b294bb; }
.label { color:#970; }
.local-variable { color:#963 }
.namespace { color:#707; }
.octal { color:#40E }
.operator { }
.predefined { color:#369; }
.predefined-constant { color:#de935f }
.predefined-type { color:#0a5; }
.preprocessor { color:#579 }
.pseudo-class { color:#00C; }
.regexp .content { color:#cc6666 }
.regexp .delimiter { color:#cc6666 }
.regexp .modifier { color:#cc6666 }
.regexp { }
.reserved { color:#080; }
.shell .content { color:#2B2 }
.shell .delimiter { color:#161 }
.shell { background-color:hsla(120,100%,50%,0.06); }
.string .char { color: #b5bd68 }
.string .content { color: #b5bd68 }
.string .delimiter { color: #b5bd68 }
.string .modifier { color: #b5bd68 }
.string { }
.symbol .content { color:#b5bd68 }
.symbol .delimiter { color:#b5bd68 }
.symbol { color:#b5bd68 }
.tag { color:#070 }
.type { color:#339; }
.value { color: #088; }
.variable { color:#037 }
.insert { background: hsla(120,100%,50%,0.12) }
.delete { background: hsla(0,100%,50%,0.12) }
.change { color: #bbf; background: #007; }
.head { color: #f8f; background: #505 }
.head .filename { color: white; }
.delete .eyecatcher { background-color: hsla(0,100%,50%,0.2); border: 1px solid hsla(0,100%,45%,0.5); margin: -1px; border-bottom: none; border-top-left-radius: 5px; border-top-right-radius: 5px; }
.insert .eyecatcher { background-color: hsla(120,100%,50%,0.2); border: 1px solid hsla(120,100%,25%,0.5); margin: -1px; border-top: none; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; }
.insert .insert { color: #0c0; background:transparent; }
.delete .delete { color: #c00; background:transparent; }
.change .change { color: #88f }
.head .head { color: #f4f }
TOKENS
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment