|
1 | 1 | module CodeRay
|
2 | 2 | module Encoders
|
3 |
| - |
4 | 3 | 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 | 4 | 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 |
| 5 | + def self.wrap_string_in string, element, css = nil |
64 | 6 | 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 | 7 | when :span
|
69 |
| - raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil |
70 |
| - wrap_in! SPAN |
| 8 | + SPAN |
| 9 | + when :div |
| 10 | + return string if string[/\A<(?:div|table)\b/] |
| 11 | + DIV |
71 | 12 | 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 |
| 13 | + string = wrap_string_in(string, :div) unless string[/\A<(?:div|table)\b/] |
| 14 | + PAGE.sub('<%CSS%>', css ? css.stylesheet : '') |
81 | 15 | 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 |
| - |
| 16 | + raise ArgumentError, 'Unknown wrap element: %p' % [element] |
| 17 | + end.sub('<%CONTENT%>', string) |
115 | 18 | end
|
116 |
| - |
117 |
| - SPAN = Template.new '<span class="CodeRay"><%CONTENT%></span>' |
118 |
| - |
119 |
| - DIV = Template.new <<-DIV |
| 19 | + |
| 20 | + SPAN = '<span class="CodeRay"><%CONTENT%></span>' |
| 21 | + |
| 22 | + DIV = <<-DIV |
120 | 23 | <div class="CodeRay">
|
121 | 24 | <div class="code"><pre><%CONTENT%></pre></div>
|
122 | 25 | </div>
|
123 | 26 | DIV
|
124 |
| - |
125 |
| - TABLE = Template.new <<-TABLE |
126 |
| -<table class="CodeRay"><tr> |
127 |
| - <td class="line-numbers"><pre><%LINE_NUMBERS%></pre></td> |
128 |
| - <td class="code"><pre><%CONTENT%></pre></td> |
129 |
| -</tr></table> |
130 |
| - TABLE |
131 |
| - |
132 |
| - PAGE = Template.new <<-PAGE |
| 27 | + |
| 28 | + PAGE = <<-PAGE |
133 | 29 | <!DOCTYPE html>
|
134 | 30 | <html>
|
135 | 31 | <head>
|
@@ -157,10 +53,7 @@ def apply target, replacement
|
157 | 53 | </body>
|
158 | 54 | </html>
|
159 | 55 | PAGE
|
160 |
| - |
161 | 56 | end
|
162 |
| - |
163 | 57 | end
|
164 |
| - |
165 | 58 | end
|
166 | 59 | end
|
0 commit comments