To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 8a / 8a4b8b250e38c9c5c65661973d49cb661d84e614.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (3.91 KB)

1
module CodeRay
2
module Encoders
3

    
4
  class HTML
5

    
6
    # This module is included in the output String of the HTML Encoder.
7
    #
8
    # It provides methods like wrap, div, page etc.
9
    #
10
    # Remember to use #clone instead of #dup to keep the modules the object was
11
    # extended with.
12
    #
13
    # TODO: Rewrite this without monkey patching.
14
    module Output
15

    
16
      attr_accessor :css
17

    
18
      class << self
19

    
20
        # Raises an exception if an object that doesn't respond to to_str is extended by Output,
21
        # to prevent users from misuse. Use Module#remove_method to disable.
22
        def extended o  # :nodoc:
23
          warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str
24
        end
25

    
26
        def make_stylesheet css, in_tag = false  # :nodoc:
27
          sheet = css.stylesheet
28
          sheet = <<-'CSS' if in_tag
29
<style type="text/css">
30
#{sheet}
31
</style>
32
          CSS
33
          sheet
34
        end
35

    
36
        def page_template_for_css css  # :nodoc:
37
          sheet = make_stylesheet css
38
          PAGE.apply 'CSS', sheet
39
        end
40

    
41
      end
42

    
43
      def wrapped_in? element
44
        wrapped_in == element
45
      end
46

    
47
      def wrapped_in
48
        @wrapped_in ||= nil
49
      end
50
      attr_writer :wrapped_in
51

    
52
      def wrap_in! template
53
        Template.wrap! self, template, 'CONTENT'
54
        self
55
      end
56
      
57
      def apply_title! title
58
        self.sub!(/(<title>)(<\/title>)/) { $1 + title + $2 }
59
        self
60
      end
61

    
62
      def wrap! element, *args
63
        return self if not element or element == wrapped_in
64
        case element
65
        when :div
66
          raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil
67
          wrap_in! DIV
68
        when :span
69
          raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil
70
          wrap_in! SPAN
71
        when :page
72
          wrap! :div if wrapped_in? nil
73
          raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? :div
74
          wrap_in! Output.page_template_for_css(@css)
75
          if args.first.is_a?(Hash) && title = args.first[:title]
76
            apply_title! title
77
          end
78
          self
79
        when nil
80
          return self
81
        else
82
          raise "Unknown value %p for :wrap" % element
83
        end
84
        @wrapped_in = element
85
        self
86
      end
87

    
88
      def stylesheet in_tag = false
89
        Output.make_stylesheet @css, in_tag
90
      end
91

    
92
#-- don't include the templates in docu
93

    
94
      class Template < String  # :nodoc:
95

    
96
        def self.wrap! str, template, target
97
          target = Regexp.new(Regexp.escape("<%#{target}%>"))
98
          if template =~ target
99
            str[0,0] = $`
100
            str << $'
101
          else
102
            raise "Template target <%%%p%%> not found" % target
103
          end
104
        end
105

    
106
        def apply target, replacement
107
          target = Regexp.new(Regexp.escape("<%#{target}%>"))
108
          if self =~ target
109
            Template.new($` + replacement + $')
110
          else
111
            raise "Template target <%%%p%%> not found" % target
112
          end
113
        end
114

    
115
      end
116

    
117
      SPAN = Template.new '<span class="CodeRay"><%CONTENT%></span>'
118

    
119
      DIV = Template.new <<-DIV
120
<div class="CodeRay">
121
  <div class="code"><pre><%CONTENT%></pre></div>
122
</div>
123
      DIV
124

    
125
      TABLE = Template.new <<-TABLE
126
<table class="CodeRay"><tr>
127
  <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
128
  <td class="code"><pre><%CONTENT%></pre></td>
129
</tr></table>
130
      TABLE
131

    
132
      PAGE = Template.new <<-PAGE
133
<!DOCTYPE html>
134
<html>
135
<head>
136
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
137
  <title></title>
138
  <style type="text/css">
139
.CodeRay .line-numbers a {
140
  text-decoration: inherit;
141
  color: inherit;
142
}
143
<%CSS%>
144
  </style>
145
</head>
146
<body style="background-color: white;">
147

    
148
<%CONTENT%>
149
</body>
150
</html>
151
      PAGE
152

    
153
    end
154

    
155
  end
156

    
157
end
158
end