module CodeRay module Encoders class HTML # This module is included in the output String from thew HTML Encoder. # # It provides methods like wrap, div, page etc. # # Remember to use #clone instead of #dup to keep the modules the object was # extended with. # # TODO: more doc. module Output require 'coderay/encoders/html/numerization.rb' attr_accessor :css class << self # This makes Output look like a class. # # Example: # # a = Output.new 'Code' # a.wrap! :page def new string, css = CSS.new, element = nil output = string.clone.extend self output.wrapped_in = element output.css = css output end # Raises an exception if an object that doesn't respond to to_str is extended by Output, # to prevent users from misuse. Use Module#remove_method to disable. def extended o warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str end def make_stylesheet css, in_tag = false sheet = css.stylesheet sheet = <<-CSS if in_tag CSS sheet end def page_template_for_css css sheet = make_stylesheet css PAGE.apply 'CSS', sheet end # Define a new wrapper. This is meta programming. def wrapper *wrappers wrappers.each do |wrapper| define_method wrapper do |*args| wrap wrapper, *args end define_method "#{wrapper}!".to_sym do |*args| wrap! wrapper, *args end end end end wrapper :div, :span, :page def wrapped_in? element wrapped_in == element end def wrapped_in @wrapped_in ||= nil end attr_writer :wrapped_in def wrap_in template clone.wrap_in! template end def wrap_in! template Template.wrap! self, template, 'CONTENT' self end def apply_title! title self.sub!(/(
<%CONTENT%>
<%LINE_NUMBERS%> |
<%CONTENT%> |