annotate vendor/plugins/coderay-0.9.2/lib/coderay/encoders/html/output.rb @ 877:e97cef3bd5d0 bug_70

Close obsolete branch bug_70
author Chris Cannam
date Wed, 30 Mar 2011 10:48:32 +0100
parents 513646585e45
children
rev   line source
Chris@0 1 module CodeRay
Chris@0 2 module Encoders
Chris@0 3
Chris@0 4 class HTML
Chris@0 5
Chris@0 6 # This module is included in the output String from thew HTML Encoder.
Chris@0 7 #
Chris@0 8 # It provides methods like wrap, div, page etc.
Chris@0 9 #
Chris@0 10 # Remember to use #clone instead of #dup to keep the modules the object was
Chris@0 11 # extended with.
Chris@0 12 #
Chris@0 13 # TODO: more doc.
Chris@0 14 module Output
Chris@0 15
Chris@0 16 require 'coderay/encoders/html/numerization.rb'
Chris@0 17
Chris@0 18 attr_accessor :css
Chris@0 19
Chris@0 20 class << self
Chris@0 21
Chris@0 22 # This makes Output look like a class.
Chris@0 23 #
Chris@0 24 # Example:
Chris@0 25 #
Chris@0 26 # a = Output.new '<span class="co">Code</span>'
Chris@0 27 # a.wrap! :page
Chris@0 28 def new string, css = CSS.new, element = nil
Chris@0 29 output = string.clone.extend self
Chris@0 30 output.wrapped_in = element
Chris@0 31 output.css = css
Chris@0 32 output
Chris@0 33 end
Chris@0 34
Chris@0 35 # Raises an exception if an object that doesn't respond to to_str is extended by Output,
Chris@0 36 # to prevent users from misuse. Use Module#remove_method to disable.
Chris@0 37 def extended o
Chris@0 38 warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str
Chris@0 39 end
Chris@0 40
Chris@0 41 def make_stylesheet css, in_tag = false
Chris@0 42 sheet = css.stylesheet
Chris@0 43 sheet = <<-CSS if in_tag
Chris@0 44 <style type="text/css">
Chris@0 45 #{sheet}
Chris@0 46 </style>
Chris@0 47 CSS
Chris@0 48 sheet
Chris@0 49 end
Chris@0 50
Chris@0 51 def page_template_for_css css
Chris@0 52 sheet = make_stylesheet css
Chris@0 53 PAGE.apply 'CSS', sheet
Chris@0 54 end
Chris@0 55
Chris@0 56 # Define a new wrapper. This is meta programming.
Chris@0 57 def wrapper *wrappers
Chris@0 58 wrappers.each do |wrapper|
Chris@0 59 define_method wrapper do |*args|
Chris@0 60 wrap wrapper, *args
Chris@0 61 end
Chris@0 62 define_method "#{wrapper}!".to_sym do |*args|
Chris@0 63 wrap! wrapper, *args
Chris@0 64 end
Chris@0 65 end
Chris@0 66 end
Chris@0 67
Chris@0 68 end
Chris@0 69
Chris@0 70 wrapper :div, :span, :page
Chris@0 71
Chris@0 72 def wrapped_in? element
Chris@0 73 wrapped_in == element
Chris@0 74 end
Chris@0 75
Chris@0 76 def wrapped_in
Chris@0 77 @wrapped_in ||= nil
Chris@0 78 end
Chris@0 79 attr_writer :wrapped_in
Chris@0 80
Chris@0 81 def wrap_in template
Chris@0 82 clone.wrap_in! template
Chris@0 83 end
Chris@0 84
Chris@0 85 def wrap_in! template
Chris@0 86 Template.wrap! self, template, 'CONTENT'
Chris@0 87 self
Chris@0 88 end
Chris@0 89
Chris@0 90 def apply_title! title
Chris@0 91 self.sub!(/(<title>)(<\/title>)/) { $1 + title + $2 }
Chris@0 92 self
Chris@0 93 end
Chris@0 94
Chris@0 95 def wrap! element, *args
Chris@0 96 return self if not element or element == wrapped_in
Chris@0 97 case element
Chris@0 98 when :div
Chris@0 99 raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil
Chris@0 100 wrap_in! DIV
Chris@0 101 when :span
Chris@0 102 raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil
Chris@0 103 wrap_in! SPAN
Chris@0 104 when :page
Chris@0 105 wrap! :div if wrapped_in? nil
Chris@0 106 raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? :div
Chris@0 107 wrap_in! Output.page_template_for_css(@css)
Chris@0 108 if args.first.is_a?(Hash) && title = args.first[:title]
Chris@0 109 apply_title! title
Chris@0 110 end
Chris@0 111 self
Chris@0 112 when nil
Chris@0 113 return self
Chris@0 114 else
Chris@0 115 raise "Unknown value %p for :wrap" % element
Chris@0 116 end
Chris@0 117 @wrapped_in = element
Chris@0 118 self
Chris@0 119 end
Chris@0 120
Chris@0 121 def wrap *args
Chris@0 122 clone.wrap!(*args)
Chris@0 123 end
Chris@0 124
Chris@0 125 def stylesheet in_tag = false
Chris@0 126 Output.make_stylesheet @css, in_tag
Chris@0 127 end
Chris@0 128
Chris@0 129 class Template < String
Chris@0 130
Chris@0 131 def self.wrap! str, template, target
Chris@0 132 target = Regexp.new(Regexp.escape("<%#{target}%>"))
Chris@0 133 if template =~ target
Chris@0 134 str[0,0] = $`
Chris@0 135 str << $'
Chris@0 136 else
Chris@0 137 raise "Template target <%%%p%%> not found" % target
Chris@0 138 end
Chris@0 139 end
Chris@0 140
Chris@0 141 def apply target, replacement
Chris@0 142 target = Regexp.new(Regexp.escape("<%#{target}%>"))
Chris@0 143 if self =~ target
Chris@0 144 Template.new($` + replacement + $')
Chris@0 145 else
Chris@0 146 raise "Template target <%%%p%%> not found" % target
Chris@0 147 end
Chris@0 148 end
Chris@0 149
Chris@0 150 module Simple
Chris@0 151 def ` str #` <-- for stupid editors
Chris@0 152 Template.new str
Chris@0 153 end
Chris@0 154 end
Chris@0 155 end
Chris@0 156
Chris@0 157 extend Template::Simple
Chris@0 158
Chris@0 159 #-- don't include the templates in docu
Chris@0 160
Chris@0 161 SPAN = `<span class="CodeRay"><%CONTENT%></span>`
Chris@0 162
Chris@0 163 DIV = <<-`DIV`
Chris@0 164 <div class="CodeRay">
Chris@0 165 <div class="code"><pre><%CONTENT%></pre></div>
Chris@0 166 </div>
Chris@0 167 DIV
Chris@0 168
Chris@0 169 TABLE = <<-`TABLE`
Chris@0 170 <table class="CodeRay"><tr>
Chris@0 171 <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
Chris@0 172 <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
Chris@0 173 </tr></table>
Chris@0 174 TABLE
Chris@0 175 # title="double click to expand"
Chris@0 176
Chris@0 177 LIST = <<-`LIST`
Chris@0 178 <ol class="CodeRay">
Chris@0 179 <%CONTENT%>
Chris@0 180 </ol>
Chris@0 181 LIST
Chris@0 182
Chris@0 183 PAGE = <<-`PAGE`
Chris@0 184 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
Chris@0 185 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Chris@0 186 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="de">
Chris@0 187 <head>
Chris@0 188 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
Chris@0 189 <title></title>
Chris@0 190 <style type="text/css">
Chris@0 191 <%CSS%>
Chris@0 192 </style>
Chris@0 193 </head>
Chris@0 194 <body style="background-color: white;">
Chris@0 195
Chris@0 196 <%CONTENT%>
Chris@0 197 </body>
Chris@0 198 </html>
Chris@0 199 PAGE
Chris@0 200
Chris@0 201 end
Chris@0 202
Chris@0 203 end
Chris@0 204
Chris@0 205 end
Chris@0 206 end