From b60448c9788d37d7c0ab9cd0a3f0337861c64843 Mon Sep 17 00:00:00 2001 From: murphy Date: Sun, 13 Nov 2005 06:24:54 +0000 Subject: New Style system added - still beta. Demos adjusted coderay.rb, encoders/html.rb, encoders/div.rb adjusted. encoders/html/* files rebuild. --- lib/coderay/encoders/div.rb | 1 + lib/coderay/encoders/html.rb | 7 +- lib/coderay/encoders/html/classes.rb | 1 + lib/coderay/encoders/html/css.rb | 133 ++++++----------------------------- lib/coderay/encoders/html/output.rb | 31 ++++---- 5 files changed, 49 insertions(+), 124 deletions(-) (limited to 'lib/coderay/encoders') diff --git a/lib/coderay/encoders/div.rb b/lib/coderay/encoders/div.rb index 640df0e..d3e595d 100644 --- a/lib/coderay/encoders/div.rb +++ b/lib/coderay/encoders/div.rb @@ -11,6 +11,7 @@ module CodeRay module Encoders :css => :style, :wrap => :div, }) + end end end diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 986bf00..df4130e 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -71,6 +71,8 @@ module Encoders :level => :xhtml, :css => :class, + :style => :cYcnus, + :wrap => :page, :line_numbers => nil, @@ -84,6 +86,8 @@ module Encoders require 'coderay/encoders/html/output' require 'coderay/encoders/html/css' + attr_reader :css + def initialize(*) super @last_options = nil @@ -116,7 +120,7 @@ module Encoders @HTML_ESCAPE["\t"] = ' ' * options[:tab_width] @opened = [nil] - @css = CSS.new + @css = CSS.new options[:style] hint = options[:hint] if hint and not [:debug, :info].include? hint @@ -192,6 +196,7 @@ module Encoders @out << '' * @opened.size @out.extend Output + @out.css = @css @out.numerize! options[:line_numbers], options # if options[:line_numbers] @out.wrap! options[:wrap] # if options[:wrap] diff --git a/lib/coderay/encoders/html/classes.rb b/lib/coderay/encoders/html/classes.rb index 6a2938e..4a01920 100644 --- a/lib/coderay/encoders/html/classes.rb +++ b/lib/coderay/encoders/html/classes.rb @@ -54,6 +54,7 @@ module CodeRay module Encoders :xml_text => 'xt', :ident => :NO_HIGHLIGHT, # 'id' + #:operator => 'op', :operator => :NO_HIGHLIGHT, # 'op' :space => :NO_HIGHLIGHT, # 'sp' :plain => :NO_HIGHLIGHT, diff --git a/lib/coderay/encoders/html/css.rb b/lib/coderay/encoders/html/css.rb index 80ccab4..69d002a 100644 --- a/lib/coderay/encoders/html/css.rb +++ b/lib/coderay/encoders/html/css.rb @@ -2,19 +2,34 @@ module CodeRay module Encoders class HTML class CSS + + DEFAULT_STYLESHEET_ID = :cYcnus + + attr :stylesheet + + def CSS.load_stylesheet style + style = DEFAULT_STYLESHEET_ID if style == :default + CodeRay::Styles[style] + end - def initialize stylesheet = TOKENS + def initialize style = :default @classes = Hash.new - parse stylesheet + style = CSS.load_stylesheet style + @stylesheet = [ + style::CSS_MAIN_STYLES, + style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ') + ].join("\n") + parse style::TOKEN_COLORS end def [] *styles cl = @classes[styles.first] return '' unless cl - style = false + style = '' 1.upto(styles.size) do |offset| break if style = cl[styles[offset .. -1]] end + raise 'Style not found: %p' % [styles] if $DEBUG and style.empty? return style end @@ -24,9 +39,9 @@ module CodeRay module Encoders ( (?: # $1 = classes \s* \. [-\w]+ )+ ) - \s* \{ - ( [^\}]* ) # $2 = style - \} \s* + \s* \{ \s* + ( [^\}]+ )? # $2 = style + \s* \} \s* | ( . ) # $3 = error /mx @@ -36,114 +51,10 @@ module CodeRay module Encoders styles = classes.scan(/[-\w]+/) cl = styles.pop @classes[cl] ||= Hash.new - @classes[cl][styles] = style.strip + @classes[cl][styles] = style.to_s.strip end end - MAIN = <<-'MAIN' -.CodeRay { - background-color: #f8f8f8; - border: 1px solid silver; - font-family: 'Courier New', 'Terminal', monospace; - color: black; -} -.CodeRay pre { margin: 0px; } - -div.CodeRay { } - -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, .CodeRay .no { - background-color: #def; - color: gray; - text-align: right; -} -.CodeRay .line_numbers tt { font-weight: bold; } -.CodeRay .no { padding: 0px 4px; } -.CodeRay .code { width: 100%; } - -ol.CodeRay { font-size: 10pt; } -ol.CodeRay li { white-space: pre; } - -.CodeRay .code pre { overflow: auto; } - MAIN - - TOKENS = <<-'TOKENS' -.af { color:#00C; } -.an { color:#007; } -.av { color:#700; } -.aw { color:#C00; } -.bi { color:#509; font-weight:bold; } -.c { color:#888; } - -.ch { color:#04D; } -.ch .k { color:#04D; } -.ch .dl { color:#039; } - -.cl { color:#B06; font-weight:bold; } -.co { color:#036; font-weight:bold; } -.cr { color:#0A0; } -.cv { color:#369; } -.df { color:#099; font-weight:bold; } -.di { color:#088; font-weight:bold; } -.dl { color:black; } -.do { color:#970; } -.ds { color:#D42; font-weight:bold; } -.e { color:#666; font-weight:bold; } -.er { color:#F00; background-color:#FAA; } -.ex { color:#F00; font-weight:bold; } -.fl { color:#60E; font-weight:bold; } -.fu { color:#06B; font-weight:bold; } -.gv { color:#d70; font-weight:bold; } -.hx { color:#058; font-weight:bold; } -.i { color:#00D; font-weight:bold; } -.ic { color:#B44; font-weight:bold; } -.il { } -.in { color:#B2B; font-weight:bold; } -.iv { color:#33B; } -.la { color:#970; font-weight:bold; } -.lv { color:#963; } -.oc { color:#40E; font-weight:bold; } -.on { color:#000; font-weight:bold; } -.pc { color:#038; font-weight:bold; } -.pd { color:#369; font-weight:bold; } -.pp { color:#579; } -.pt { color:#339; font-weight:bold; } -.r { color:#080; font-weight:bold; } - -.rx { background-color:#fff0ff; } -.rx .k { color:#808; } -.rx .dl { color:#404; } -.rx .mod { color:#C2C; } -.rx .fu { color:#404; font-weight: bold; } - -.s { background-color:#fff0f0; } -.s .s { background-color:#ffe0e0; } -.s .s .s { background-color:#ffd0d0; } -.s .k { color:#D20; } -.s .dl { color:#710; } - -.sh { background-color:#f0fff0; } -.sh .k { color:#2B2; } -.sh .dl { color:#161; } - -.sy { color:#A60; } -.sy .k { color:#A60; } -.sy .dl { color:#630; } - -.ta { color:#070; } -.tf { color:#070; font-weight:bold; } -.ts { color:#D70; font-weight:bold; } -.ty { color:#339; font-weight:bold; } -.v { color:#036; } -.xt { color:#444; } - TOKENS - - DEFAULT_STYLESHEET = MAIN + TOKENS.gsub(/^(?!$)/, '.CodeRay ') - end end diff --git a/lib/coderay/encoders/html/output.rb b/lib/coderay/encoders/html/output.rb index c8985da..36018a7 100644 --- a/lib/coderay/encoders/html/output.rb +++ b/lib/coderay/encoders/html/output.rb @@ -15,7 +15,7 @@ module CodeRay require 'coderay/encoders/html/numerization.rb' - attr_accessor :wrapped_in + attr_accessor :css class << self @@ -25,9 +25,10 @@ module CodeRay # # a = Output.new 'Code' # a.wrap! :page - def new string, element = nil + def new string, css, element = nil output = string.clone.extend self output.wrapped_in = element + output.css = css output end @@ -37,19 +38,19 @@ module CodeRay warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str end - def stylesheet in_tag = false - ss = CSS::DEFAULT_STYLESHEET - ss = <<-CSS if in_tag + def make_stylesheet css, in_tag = false + sheet = css.stylesheet + sheet = <<-CSS if in_tag CSS - ss + sheet end - def page_template_for_css css = :default - css = stylesheet if css == :default - PAGE.apply 'CSS', css + def page_template_for_css css + sheet = make_stylesheet css + PAGE.apply 'CSS', sheet end # Define a new wrapper. This is meta programming. @@ -63,6 +64,7 @@ module CodeRay end end end + end wrapper :div, :span, :page @@ -71,6 +73,11 @@ module CodeRay wrapped_in == element end + def wrapped_in + @wrapped_in ||= nil + end + attr_writer :wrapped_in + def wrap_in template clone.wrap_in! template end @@ -92,7 +99,7 @@ module CodeRay when :page wrap! :div if wrapped_in? nil raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? :div - wrap_in! Output.page_template_for_css + wrap_in! Output.page_template_for_css(@css) when nil return self else @@ -107,7 +114,7 @@ module CodeRay end def stylesheet in_tag = false - Output.stylesheet in_tag + Output.make_stylesheet @css, in_tag end class Template < String -- cgit v1.2.1