forked from henrik/rails-footnotes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
footnotes.rb
executable file
·233 lines (206 loc) · 6.74 KB
/
footnotes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
module Footnotes
class Filter
@@no_style = false
@@multiple_notes = false
# Edit notes
@@notes = [ :components, :controller, :view, :layout, :stylesheets, :javascripts ]
# Show notes
@@notes += [ :session, :cookies, :params, :filters, :routes, :queries, :log, :general ]
cattr_accessor :no_style, :notes, :prefix, :multiple_notes
class << self
def before(controller)
Footnotes::Filter.start!
end
def after(controller)
filter = Footnotes::Filter.new(controller)
filter.add_footnotes!
filter.close!
end
def start!
@@notes.flatten.each do |note|
klass = eval("Footnotes::Notes::#{note.to_s.camelize}Note") if note.is_a?(Symbol) || note.is_a?(String)
klass.start! if klass.respond_to?(:start!)
end
rescue Exception => e
log_error("Footnotes Exception", e)
end
end
def initialize(controller)
@controller = controller
@template = controller.instance_variable_get('@template')
@body = controller.response.body
@notes = []
end
def add_footnotes!
add_footnotes_without_validation! if valid?
rescue Exception => e
# Discard footnotes if there are any problems
log_error("Footnotes Exception", e)
end
def close!
@notes.map{|note| note.class.close!}
end
protected
def valid?
performed_render? && first_render? && valid_format? && valid_content_type? && @body.is_a?(String) && !component_request? && !xhr?
end
def add_footnotes_without_validation!
initialize_notes!
insert_styles unless @@no_style
insert_footnotes
end
def initialize_notes!
@@notes.flatten.each do |note|
begin
note = eval("Footnotes::Notes::#{note.to_s.camelize}Note").new(@controller) if note.is_a?(Symbol) || note.is_a?(String)
@notes << note if note.respond_to?(:valid?) && note.valid?
rescue Exception => e
# Discard note if it has a problem
log_error("Footnotes #{note.to_s.camelize}Note Exception", e)
next
end
end
end
def performed_render?
@controller.instance_variable_get('@performed_render')
end
def first_render?
@template.first_render
end
def valid_format?
[:html,:rhtml,:xhtml,:rxhtml].include?(@template.template_format.to_sym)
end
def valid_content_type?
c = @controller.response.headers['Content-Type']
(c.nil? || c =~ /html/)
end
def component_request?
@controller.instance_variable_get('@parent_controller')
end
def xhr?
@controller.request.xhr?
end
#
# Insertion methods
#
def insert_styles
insert_text :before, /<\/head>/i, <<-HTML
<!-- Footnotes Style -->
<style type="text/css">
#footnotes_debug {margin: 2em 0 1em 0; text-align: center; color: #444; line-height: 16px;}
#footnotes_debug a {text-decoration: none; color: #444; line-height: 18px;}
#footnotes_debug pre {overflow: scroll; margin: 0;}
#footnotes_debug table {text-align: center;}
#footnotes_debug table td {padding: 0 5px;}
#footnotes_debug tbody {text-align: left;}
#footnotes_debug legend {background-color: #FFF;}
#footnotes_debug fieldset {text-align: left; border: 1px dashed #aaa; padding: 0.5em 1em 1em 1em; margin: 1em 2em; color: #444; background-color: #FFF;}
/* Aditional Stylesheets */
#{@notes.map(&:stylesheet).compact.join("\n")}
</style>
<!-- End Footnotes Style -->
HTML
end
def insert_footnotes
footnotes_html = <<-HTML
<!-- Footnotes -->
<div style="clear:both"></div>
<div id="footnotes_debug">
#{links}
#{fieldsets}
<script type="text/javascript">
function footnotes_close(){
#{close unless @@multiple_notes}
}
function footnotes_toogle(id){
s = document.getElementById(id).style;
before = s.display;
footnotes_close();
s.display = (before != 'block') ? 'block' : 'none'
location.href = '#footnotes_debug';
}
/* Additional Javascript */
#{@notes.map(&:javascript).compact.join("\n")}
</script>
</div>
<!-- End Footnotes -->
HTML
if @body =~ %r{<div[^>]+id=['"]footnotes_holder['"][^>]*>}
# Insert inside the "footnotes_holder" div if it exists
insert_text :after, %r{<div[^>]+id=['"]footnotes_holder['"][^>]*>}, footnotes_html
else
# Otherwise, try to insert as the last part of the html body
insert_text :before, /<\/body>/i, footnotes_html
end
end
def links
links = Hash.new([])
order = []
@notes.each do |note|
order << note.row
links[note.row] += [link_helper(note)]
end
html = ''
order.uniq!
order.each do |row|
html << "#{row.is_a?(String) ? row : row.to_s.camelize}: #{links[row].join(" | \n")}<br />"
end
html
end
def fieldsets
content = ''
@notes.each do |note|
next unless note.fieldset?
content << <<-HTML
<fieldset id="#{note.to_sym}_debug_info" style="display: none">
<legend>#{note.legend}</legend>
<code>#{note.content}</code>
</fieldset>
HTML
end
content
end
def close
javascript = ''
@notes.each do |note|
next unless note.fieldset?
javascript << close_helper(note)
end
javascript
end
# Helpers
#
def close_helper(note)
"document.getElementById('#{note.to_sym}_debug_info').style.display = 'none'\n"
end
def link_helper(note)
onclick = note.onclick
unless href = note.link
href = '#'
onclick ||= "footnotes_toogle('#{note.to_sym}_debug_info');return false;" if note.fieldset?
end
"<a href=\"#{href}\" onclick=\"#{onclick}\">#{note.title}</a>"
end
# Inserts text in to the body of the document
# +pattern+ is a Regular expression which, when matched, will cause +new_text+
# to be inserted before or after the match. If no match is found, +new_text+ is appended
# to the body instead. +position+ may be either :before or :after
#
def insert_text(position, pattern, new_text)
index = case pattern
when Regexp
if match = @body.match(pattern)
match.offset(0)[position == :before ? 0 : 1]
else
@body.size
end
else
pattern
end
@body.insert index, new_text
end
def log_error(title, exception)
RAILS_DEFAULT_LOGGER.error "#{title}: #{exception}\n#{exception.backtrace.join("\n")}"
end
end
end