ruby-web 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. data/ChangeLog +474 -0
  2. data/INSTALL.txt +9 -0
  3. data/InstalledFiles +180 -0
  4. data/LICENSE.txt +74 -0
  5. data/Rakefile +529 -0
  6. data/TODO +65 -0
  7. data/doc/additional.xml +149 -0
  8. data/doc/core.xml +652 -0
  9. data/doc/credits/index.xml +52 -0
  10. data/doc/credits/php.contributors.xml +118 -0
  11. data/doc/credits/php.language-snippets.ent +622 -0
  12. data/doc/install/index.xml +136 -0
  13. data/doc/install/mac/index.xml +21 -0
  14. data/doc/install/ruby-web.install.rb.instructions.xml +7 -0
  15. data/doc/install/unix/index.xml +46 -0
  16. data/doc/install/win/apache1.xml +166 -0
  17. data/doc/install/win/apache2.xml +141 -0
  18. data/doc/install/win/iis.xml +162 -0
  19. data/doc/install/win/index.xml +24 -0
  20. data/doc/install/win/installer.xml +31 -0
  21. data/doc/install/win/manual.xml +43 -0
  22. data/doc/manual.xml +69 -0
  23. data/doc/old/apache_cgi.txt +23 -0
  24. data/doc/old/fastcgi.txt +23 -0
  25. data/doc/old/mod_ruby.txt +21 -0
  26. data/doc/old/snippets.rdoc +183 -0
  27. data/doc/old/webrick.txt +23 -0
  28. data/doc/old/windows_cgi.txt +9 -0
  29. data/doc/tutorial.xml +14 -0
  30. data/doc/xsl/manual-multi.xsl +10 -0
  31. data/doc/xsl/manual-pdf.xsl +6 -0
  32. data/doc/xsl/manual-single.xsl +6 -0
  33. data/doc/xsl/manual.css +22 -0
  34. data/install.rb +1022 -0
  35. data/lib/formatter.rb +314 -0
  36. data/lib/html-parser.rb +429 -0
  37. data/lib/htmlrepair.rb +113 -0
  38. data/lib/htmlsplit.rb +842 -0
  39. data/lib/sgml-parser.rb +332 -0
  40. data/lib/web.rb +68 -0
  41. data/lib/web/assertinclude.rb +129 -0
  42. data/lib/web/config.rb +50 -0
  43. data/lib/web/connection.rb +1070 -0
  44. data/lib/web/convenience.rb +154 -0
  45. data/lib/web/formreader.rb +318 -0
  46. data/lib/web/htmlparser/html-parser.rb +429 -0
  47. data/lib/web/htmlparser/sgml-parser.rb +332 -0
  48. data/lib/web/htmltools/element.rb +296 -0
  49. data/lib/web/htmltools/stparser.rb +276 -0
  50. data/lib/web/htmltools/tags.rb +286 -0
  51. data/lib/web/htmltools/tree.rb +139 -0
  52. data/lib/web/htmltools/xmltree.rb +160 -0
  53. data/lib/web/htmltools/xpath.rb +71 -0
  54. data/lib/web/info.rb +63 -0
  55. data/lib/web/load.rb +210 -0
  56. data/lib/web/mime.rb +87 -0
  57. data/lib/web/phprb.rb +340 -0
  58. data/lib/web/resources/test/cookie.rb +33 -0
  59. data/lib/web/resources/test/counter.rb +20 -0
  60. data/lib/web/resources/test/multipart.rb +14 -0
  61. data/lib/web/resources/test/redirect.rb +8 -0
  62. data/lib/web/resources/test/stock.rb +33 -0
  63. data/lib/web/sapi/apache.rb +129 -0
  64. data/lib/web/sapi/fastcgi.rb +22 -0
  65. data/lib/web/sapi/install/apache.rb +180 -0
  66. data/lib/web/sapi/install/iis.rb +93 -0
  67. data/lib/web/sapi/install/macosx.rb +90 -0
  68. data/lib/web/sapi/webrick.rb +86 -0
  69. data/lib/web/session.rb +83 -0
  70. data/lib/web/shim/cgi.rb +129 -0
  71. data/lib/web/shim/rails.rb +175 -0
  72. data/lib/web/stringio.rb +78 -0
  73. data/lib/web/strscanparser.rb +24 -0
  74. data/lib/web/tagparser.rb +96 -0
  75. data/lib/web/testing.rb +666 -0
  76. data/lib/web/traceoutput.rb +75 -0
  77. data/lib/web/unit.rb +56 -0
  78. data/lib/web/upload.rb +59 -0
  79. data/lib/web/validate.rb +52 -0
  80. data/lib/web/wiki.rb +557 -0
  81. data/lib/web/wiki/linker.rb +72 -0
  82. data/lib/web/wiki/page.rb +201 -0
  83. data/lib/webunit.rb +27 -0
  84. data/lib/webunit/assert.rb +152 -0
  85. data/lib/webunit/converter.rb +154 -0
  86. data/lib/webunit/cookie.rb +118 -0
  87. data/lib/webunit/domwalker.rb +185 -0
  88. data/lib/webunit/exception.rb +14 -0
  89. data/lib/webunit/form.rb +116 -0
  90. data/lib/webunit/frame.rb +37 -0
  91. data/lib/webunit/htmlelem.rb +122 -0
  92. data/lib/webunit/image.rb +26 -0
  93. data/lib/webunit/jscript.rb +31 -0
  94. data/lib/webunit/link.rb +33 -0
  95. data/lib/webunit/params.rb +321 -0
  96. data/lib/webunit/parser.rb +229 -0
  97. data/lib/webunit/response.rb +464 -0
  98. data/lib/webunit/runtest.rb +41 -0
  99. data/lib/webunit/table.rb +148 -0
  100. data/lib/webunit/testcase.rb +45 -0
  101. data/lib/webunit/ui/cui/testrunner.rb +50 -0
  102. data/lib/webunit/utils.rb +68 -0
  103. data/lib/webunit/webunit.rb +28 -0
  104. data/test/dev/action.rb +83 -0
  105. data/test/dev/forms.rb +104 -0
  106. data/test/dev/forms2.rb +104 -0
  107. data/test/dev/parser.rb +17 -0
  108. data/test/dev/scripts/dump.rb +24 -0
  109. data/test/dev/scripts/makedist.rb +62 -0
  110. data/test/dev/scripts/uri.rb +41 -0
  111. data/test/dev/scripts/uri/common.rb +432 -0
  112. data/test/dev/scripts/uri/ftp.rb +149 -0
  113. data/test/dev/scripts/uri/generic.rb +1106 -0
  114. data/test/dev/scripts/uri/http.rb +76 -0
  115. data/test/dev/scripts/uri/https.rb +26 -0
  116. data/test/dev/scripts/uri/ldap.rb +238 -0
  117. data/test/dev/scripts/uri/mailto.rb +260 -0
  118. data/test/dev/scripts/urireg.rb +174 -0
  119. data/test/dev/simpledispatcher.rb +156 -0
  120. data/test/dev/test.action.rb +146 -0
  121. data/test/dev/test.formreader.rb +463 -0
  122. data/test/dev/test.simpledispatcher.rb +186 -0
  123. data/test/dev/webunit/conv/digit-0.rb +21 -0
  124. data/test/dev/webunit/conv/digit-1.rb +17 -0
  125. data/test/dev/webunit/conv/digit.rb +23 -0
  126. data/test/dev/webunit/conv/test_digit-0.rb +16 -0
  127. data/test/dev/webunit/conv/test_digit-1.rb +19 -0
  128. data/test/dev/webunit/conv/test_digit.rb +26 -0
  129. data/test/dev/webunit/conv/test_digit_view-0.rb +76 -0
  130. data/test/dev/webunit/conv/test_digit_view-1.rb +102 -0
  131. data/test/dev/webunit/conv/test_digit_view.rb +134 -0
  132. data/test/installation/htdocs/cgi_test.rb +296 -0
  133. data/test/installation/htdocs/test_install.rb +4 -0
  134. data/test/installation/runwebtest.rb +5 -0
  135. data/test/installation/test_cookie.rb +128 -0
  136. data/test/installation/test_form.rb +47 -0
  137. data/test/installation/test_multipart.rb +51 -0
  138. data/test/installation/test_request.rb +24 -0
  139. data/test/installation/test_response.rb +35 -0
  140. data/test/unit/htdocs/cookie.rb +32 -0
  141. data/test/unit/htdocs/multipart.rb +28 -0
  142. data/test/unit/htdocs/redirect.rb +12 -0
  143. data/test/unit/htdocs/simple.rb +13 -0
  144. data/test/unit/htdocs/stock.rb +33 -0
  145. data/test/unit/test_assert.rb +162 -0
  146. data/test/unit/test_cookie.rb +114 -0
  147. data/test/unit/test_domwalker.rb +77 -0
  148. data/test/unit/test_form.rb +42 -0
  149. data/test/unit/test_frame.rb +40 -0
  150. data/test/unit/test_htmlelem.rb +74 -0
  151. data/test/unit/test_image.rb +45 -0
  152. data/test/unit/test_jscript.rb +57 -0
  153. data/test/unit/test_link.rb +85 -0
  154. data/test/unit/test_multipart.rb +51 -0
  155. data/test/unit/test_params.rb +210 -0
  156. data/test/unit/test_parser.rb +53 -0
  157. data/test/unit/test_response.rb +150 -0
  158. data/test/unit/test_table.rb +70 -0
  159. data/test/unit/test_utils.rb +106 -0
  160. data/test/unit/test_webunit.rb +28 -0
  161. data/test/web/mod_ruby_stub.rb +39 -0
  162. data/test/web/test.assertinclude.rb +109 -0
  163. data/test/web/test.buffer.rb +182 -0
  164. data/test/web/test.code.loader.rb +78 -0
  165. data/test/web/test.config.rb +31 -0
  166. data/test/web/test.error.handling.rb +91 -0
  167. data/test/web/test.formreader-2.0.rb +352 -0
  168. data/test/web/test.load.rb +125 -0
  169. data/test/web/test.mime-type.rb +23 -0
  170. data/test/web/test.narf.cgi.rb +106 -0
  171. data/test/web/test.phprb.rb +239 -0
  172. data/test/web/test.request.rb +368 -0
  173. data/test/web/test.response.rb +637 -0
  174. data/test/web/test.ruby-web.rb +10 -0
  175. data/test/web/test.session.rb +50 -0
  176. data/test/web/test.shim.cgi.rb +96 -0
  177. data/test/web/test.tagparser.rb +65 -0
  178. data/test/web/test.template2.rb +297 -0
  179. data/test/web/test.testing2.rb +318 -0
  180. data/test/web/test.upload.rb +45 -0
  181. data/test/web/test.validate.rb +46 -0
  182. data/test/web/test.web.test.rb +495 -0
  183. data/test/wiki/test.history.rb +297 -0
  184. data/test/wiki/test.illustration_page.rb +287 -0
  185. data/test/wiki/test.linker.rb +197 -0
  186. data/test/wiki/test.tarpit.rb +56 -0
  187. data/test/wiki/test.wiki.rb +300 -0
  188. data/test/wikitestroot/admin.rb +7 -0
  189. data/test/wikitestroot/wiki.rb +6 -0
  190. metadata +234 -0
@@ -0,0 +1,71 @@
1
+ # Copyright:: Copyright (C) 2002, Ned Konz <ned@bike-nomad.com>
2
+ # License:: Same as Ruby's
3
+ # CVS ID: $Id: xpath.rb,v 1.2 2002/06/04 02:52:12 ned Exp $
4
+
5
+ # This module adapts REXML's XPath functionality for use with
6
+ # <tt>HTMLTree::Parser</tt>.
7
+
8
+ require 'web/htmltools/tree'
9
+ require 'rexml/element'
10
+ require 'rexml/document'
11
+ require 'rexml/xpath'
12
+
13
+ module HTMLTree #:nodoc: all
14
+
15
+ module TreeElement
16
+ # Given the XPath path, return an Array of matching sub-elements of
17
+ # the REXML tree.
18
+ def rexml_match(path)
19
+ node = as_rexml_document
20
+ REXML::XPath.match(node, path)
21
+ end
22
+ end
23
+
24
+ class Element
25
+ # convert the given HTMLTree::Element (or HTMLTree::Document) into
26
+ # a REXML::Element or REXML::Document, ready to use REXML::XPath on.
27
+ # Note that this caches the tree; further changes to my tree will
28
+ # not be reflected in subsequent calls
29
+ def as_rexml_document(rparent = nil, context = {})
30
+ return @_rexml_tree if @_rexml_tree
31
+ node = REXML::Element.new( tag, rparent, context )
32
+ attribute_order().each { |attr|
33
+ node.add_attribute(attr, attribute(attr).to_s)
34
+ }
35
+ children().each { |child|
36
+ childNode = child.as_rexml_document(node, context)
37
+ }
38
+ @_rexml_tree = node
39
+ end
40
+ end
41
+
42
+ class Data
43
+ def as_rexml_document(rparent = nil, context = {})
44
+ rparent.add_text(@_content)
45
+ end
46
+ end
47
+
48
+ class Comment
49
+ def as_rexml_document(rparent = nil, context = {})
50
+ node = REXML::Comment.new(@_content, parent)
51
+ end
52
+ end
53
+
54
+ class Special
55
+ def as_rexml_document(rparent = nil, context = {})
56
+ node = REXML::Instruction.new(@_content,
57
+ context[:respect_whitespace] || false, rparent)
58
+ end
59
+ end
60
+
61
+ class Document
62
+ def as_rexml_document(context = {})
63
+ node = REXML::Document.new(nil, context)
64
+ # add DocType
65
+ # add <HTML> node
66
+ node.add( html_node.as_rexml_document(node, context) )
67
+ node
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,63 @@
1
+ module Web
2
+ @@message_template = nil
3
+
4
+ def Web::print_message( title, content )
5
+ @@message_template ||=
6
+ lib_file_contents('resources/message_template.html' )
7
+ puts( @@message_template.gsub( /\$title\$/,
8
+ title ).gsub( /\$content\$/,
9
+ content ) )
10
+ end
11
+
12
+ def Web::info_fileinfo( file)
13
+ lines = '<table>'
14
+ %w!original_filename read content_type!.each { |m|
15
+ lines << "<tr><td>#{m}</td><td>#{file.send(m)}</td></tr>"
16
+ }
17
+ lines << '</table>'
18
+ end
19
+
20
+
21
+
22
+ def Web::info
23
+ Web::clear()
24
+ if ( [
25
+ "multipart.html",
26
+ "multipart.rb",
27
+ "cookie.rb",
28
+ "response.html",
29
+ "redirect.rb",
30
+ "counter.rb"
31
+ ].include? Web["test"] )
32
+ Web::load( File.dirname(__FILE__) + "/resources/test/" + Web["test"] )
33
+ else
34
+
35
+
36
+ msg = ""
37
+
38
+ [:request, :get, :post, :cookie, :env].each do |namespace|
39
+ variables = Web::send(namespace)
40
+ unless variables.empty?
41
+ msg += "<h2>Web::#{namespace}</h2>"
42
+ msg += "<table onMouseover=\"changeto(event, '#F8FF80')\" onMouseout=\"changeback(event, '#eeeeff')\">"
43
+ variables.keys.sort.each do |key|
44
+ msg += "<tr><td><b>#{key}</b></td><td>"
45
+ value = variables[key]
46
+ value = [value] unless value.kind_of? Array
47
+ msg += value.collect do |e|
48
+ Web::html_encode( if e.kind_of? String
49
+ e
50
+ else
51
+ e.inspect
52
+ end )
53
+ end.join("<br />")
54
+ end
55
+ msg += "</table>"
56
+ end
57
+ end
58
+
59
+ Web::print_message( "Web::info", msg )
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,210 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'web'
4
+ require 'web/phprb'
5
+
6
+ module Web
7
+ SHEBANG_PATTERN = /^#!.*?[\r\n\f]/
8
+ APPLICATION = "application.rb"
9
+
10
+ class TemplateError < Exception
11
+ attr_accessor :original_error, :scriptname
12
+ def initialize(error, scriptname)
13
+ self.original_error = error
14
+ self.scriptname = scriptname
15
+ super(error.to_s)
16
+ end
17
+
18
+ def trimmed_original
19
+ call_index = original_error.backtrace.length
20
+ original_error.backtrace.each_with_index do |line, i|
21
+ if line =~ /web\/load.rb.*in `call'/
22
+ call_index = i + 1
23
+ break
24
+ end
25
+ end
26
+ trace = original_error.backtrace[call_index..-1]
27
+ # I want to set the script name, but I can't get it to work
28
+ #trace[-1] = trace[-1].gsub(/^\(eval\):/, scriptname + ":")
29
+ original_error.set_backtrace( trace )
30
+ original_error
31
+ end
32
+ end
33
+
34
+ Web::config::describe( :load_suffix => "hash of :ext => lambda {|scriptname|...} code handlers, used by Web::load" )
35
+ Web::config[:load_suffix] = \
36
+ { :rb => lambda do |script|
37
+ begin
38
+ Kernel::load( script )
39
+ #rescue Exception => e
40
+ # raise TemplateError.new(e, script)
41
+ end
42
+ end,
43
+ :cgi => lambda do |script|
44
+ begin
45
+ Kernel::load( script, true )
46
+ #rescue Exception => e
47
+ # raise TemplateError.new(e, script)
48
+ end
49
+ end,
50
+ :so => lambda do |script| \
51
+ begin
52
+ Kernel::load(script, true )
53
+ #rescue Exception => e
54
+ # raise TemplateError.new(e, script)
55
+ end
56
+ end, }
57
+
58
+ class Template
59
+ attr_accessor :name, :src
60
+ def initialize( name, src )
61
+ @name = name
62
+ # ruby doesn't like mac files ("\r")
63
+ @src = src.gsub( /\r\n|\n|\r/, "\n" )
64
+ end
65
+
66
+ def evaluate
67
+ begin
68
+ ### request coupling problem here
69
+ eval( self.src )
70
+ rescue Exception => e
71
+ raise TemplateError.new(e, self.name)
72
+ end
73
+ end
74
+
75
+ def include symbol
76
+ Object::instance_eval do
77
+ include( symbol )
78
+ end
79
+ end
80
+
81
+ def Template::evaluate( name, src )
82
+ self.new( name, src ).evaluate
83
+ end
84
+ end
85
+
86
+ Web::config[:load_suffix].default = lambda do |script|
87
+ rhtml = File.open(script) {|f|f.read}.sub(SHEBANG_PATTERN, '')
88
+
89
+ require 'erb'
90
+ compiler = ERB::Compiler.new nil
91
+ compiler.put_cmd = 'Web::write'
92
+ src = compiler.compile(rhtml)
93
+ Web::Template.evaluate( script, src )
94
+ end
95
+
96
+ def Web::load( scriptname )
97
+ Web::connection.templates ||= [ ]
98
+ Web::connection.templates.push scriptname
99
+
100
+ scriptname = Web::template_file( scriptname )
101
+
102
+ unless (File.exists? scriptname)
103
+ raise LoadError.new("Template not found: #{scriptname}")
104
+ end
105
+
106
+ Web::local.merge! Web::options[:local] || {}
107
+
108
+ unless Web::application_loaded
109
+ Web::application_loaded = true
110
+ Web::connection.run_caller = caller
111
+ Web::load_application(scriptname)
112
+ end
113
+
114
+ begin
115
+ Web::config[:load_suffix].each do |suffix, block|
116
+ if scriptname =~ /\.#{suffix}$/i
117
+ return block.call( scriptname )
118
+ end
119
+ end
120
+ return Web::config[:load_suffix].default.call( scriptname )
121
+ rescue TemplateError => e
122
+ raise e.trimmed_original
123
+ end
124
+ end
125
+
126
+ Web::config[:load_path] ||= ['.']
127
+ def Web::template_file( template )
128
+ template_file = template
129
+ Web::config[:load_path].reverse.each{ |dir|
130
+ full_template_path = File.join( dir, template )
131
+ if (File.exists? full_template_path)
132
+ template_file = full_template_path
133
+ end
134
+ }
135
+ template_file
136
+ end
137
+
138
+ def Web::load_application(scriptname)
139
+ search_path = File.expand_path(scriptname)
140
+ while( search_path && \
141
+ !(search_path =~ /^([a-z]:)?\/$/i ) && \
142
+ search_path != '.' )
143
+ app = File.join( File::dirname(search_path), APPLICATION )
144
+
145
+ if File.exists? app
146
+ Web::load app
147
+ search_path = nil
148
+ else
149
+ search_path = File::dirname(search_path)
150
+ end
151
+ end
152
+ end
153
+
154
+ def Web::ruby_web()
155
+ # remnants of narflets.. do we still want to use this?
156
+ #Web::docroot = ENV["DOCUMENT_ROOT"]
157
+ #$: << "#{Web::docroot}/../src"
158
+ #$: << "#{Web::docroot}/../config"
159
+
160
+ options = {}
161
+ switches = Web::argv_switches(ARGV)
162
+ switches.each do |arg|
163
+ case arg
164
+ when /^-I/
165
+ $:.unshift arg.sub( /^-I/, '' )
166
+ when /^--cli$/
167
+ options[:noheader] = true
168
+ when /^--ruby$/
169
+ options[:style] = :ruby
170
+ when /^--xml$/
171
+ options[:xml] = true
172
+ else
173
+ #Web::print_usage
174
+ #exit
175
+ end
176
+ end
177
+
178
+ script = if (File.exists? ENV['PATH_TRANSLATED'].to_s)
179
+ ENV['PATH_TRANSLATED']
180
+ elsif(File.exists?ENV['SCRIPT_FILENAME'].to_s)
181
+ ENV['SCRIPT_FILENAME']
182
+ else
183
+ ARGV[0]
184
+ end
185
+
186
+ ARGV.shift if (script == ARGV.first )
187
+
188
+ Web::open(options) { Web::load( script ) }
189
+ end
190
+
191
+ def Web::argv_switches( args )
192
+ args.shift
193
+ switches = []
194
+ found_doubledash = false
195
+ args.delete_if do |e|
196
+ unless found_doubledash
197
+ if e =~ /(^--$)/
198
+ found_doubledash = true
199
+ elsif e =~ /^[^-]/
200
+ found_doubledash = true
201
+ false
202
+ else
203
+ switches.push e
204
+ end
205
+ end
206
+ end
207
+ switches
208
+ end
209
+
210
+ end
@@ -0,0 +1,87 @@
1
+ module Web
2
+ # == Purpose
3
+ # This class returns mime types. It parses an apache-style mime.types file
4
+ # located in site_lib/web/resources/mime.types for it's database.
5
+ #
6
+ # This class can be independantly of the rest of NARF:
7
+ #
8
+ # require 'web/mime.rb'
9
+ # Web::Mime::get_mime_type( 'filename.txt' )
10
+ #
11
+ class Mime
12
+ DEFAULT_MIMETYPE = "text/html"
13
+ @@readers = []
14
+ def Mime.readers
15
+ @@readers
16
+ end
17
+
18
+ class ApacheUnixReader #:nodoc:
19
+ MIME_FILENAME = "resources/mime.types"
20
+ COMMENTED_PATTERN = /(^\s*#)|(^\s*$)/
21
+ VALUE_PATTERN = /\.*(\S+)\s*=\s*(\S+)/
22
+
23
+ def self.mime_file
24
+ Web::lib_filename( MIME_FILENAME )
25
+ end
26
+
27
+ def self.applies?
28
+ File.exists? mime_file
29
+ end
30
+
31
+ def mime_types
32
+ mime_array = File.open( self.class.mime_file, "r" ) { |f|
33
+ f.read.to_a
34
+ }
35
+ mime_array.uniq!
36
+ mime_array.delete_if { |line|
37
+ line =~ COMMENTED_PATTERN
38
+ }
39
+ mime_hash = {}
40
+ mime_hash.default = DEFAULT_MIMETYPE
41
+ mime_array.each { |line|
42
+ pieces = line.split( /\s+/ )
43
+ mime_type = pieces.shift
44
+ pieces.each { |extension|
45
+ mime_hash[extension] = mime_type
46
+ }
47
+ }
48
+ mime_hash
49
+ end
50
+
51
+ Mime.readers.push self
52
+ end
53
+
54
+ @@mime_types = nil
55
+
56
+ class << self
57
+ def clear
58
+ @@mime_types = nil
59
+ end
60
+
61
+ def mime_types
62
+ unless(@@mime_types)
63
+ readers.each{ |klass|
64
+ if klass.applies?
65
+ @@mime_types = klass.new.mime_types
66
+ break
67
+ end
68
+ }
69
+ end
70
+ @@mime_types
71
+ end
72
+
73
+ def get_mime_type( filename )
74
+ mime_types[filename.split(".").last].to_s
75
+ end
76
+ alias :get_mimetype :get_mime_type
77
+ end
78
+ end
79
+
80
+ class << self
81
+ def get_mime_type(filename)
82
+ Mime.get_mime_type(filename)
83
+ end
84
+ alias :get_mimetype :get_mime_type
85
+ end
86
+
87
+ end
@@ -0,0 +1,340 @@
1
+ # + modified by Patrick May to support php-style
2
+ # question marks ( <? ... ?> ) instead of
3
+ # the current asp-style ( <% ... %> )
4
+ # Tiny eRuby --- ERB2
5
+ # Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
6
+ # You can redistribute it and/or modify it under the same terms as Ruby.
7
+
8
+ class PHPRB #:nodoc: all
9
+ Revision = '$Date: 2003/12/10 09:07:52 $' #'
10
+
11
+ def self.version
12
+ "erb.rb [2.0.4 #{PHPRB::Revision.split[1]}]"
13
+ end
14
+ end
15
+
16
+ # PHPRB::Compiler
17
+ class PHPRB
18
+ def PHPRB::load( script )
19
+ scriptname = script
20
+ script = if (File.exists? script)
21
+ File.open(scriptname,'r'){|f|f.read} || ""
22
+ else
23
+ scriptname = nil
24
+ script
25
+ end
26
+
27
+ script.gsub!( Web::SHEBANG_PATTERN, '' )
28
+
29
+ # I'm keeping the compiler separate from the template
30
+ # class, because I plan to replace the compiler
31
+ # ~ pat
32
+ script = PHPRB.new(script, nil, Web::options[:xml] ).src
33
+
34
+ Web::Template.evaluate( scriptname, script )
35
+ end
36
+
37
+
38
+ class Compiler
39
+ class PercentLine
40
+ def initialize(str)
41
+ @value = str
42
+ end
43
+ attr_reader :value
44
+ alias :to_s :value
45
+ end
46
+
47
+ class Scanner
48
+
49
+ def initialize(src, xml_mode=nil)
50
+ @src = src
51
+ @stag = nil
52
+ @xml_mode = xml_mode
53
+ end
54
+ attr_accessor :stag
55
+
56
+
57
+ def stag_exp
58
+ open_pattern = if @xml_mode
59
+ '\?ruby'
60
+ else
61
+ '\?(ruby)?'
62
+ end
63
+ '(<\?\?)|(<' + open_pattern + '=)|(<!---)|(<' + open_pattern + ')'
64
+ end
65
+
66
+ def etag_exp
67
+ '(\?\?>)|(--->)|(\?>)'
68
+ end
69
+
70
+ begin
71
+ require 'strscan'
72
+ def scan
73
+ stag_reg = /(.*?)(#{self.stag_exp}|\n|\z)/
74
+ etag_reg = /(.*?)(#{self.etag_exp}|\n|\z)/
75
+ scanner = StringScanner.new(@src)
76
+ while ! scanner.eos?
77
+ scanner.scan(@stag ? etag_reg : stag_reg)
78
+ text = scanner[1]
79
+ elem = scanner[2]
80
+ yield(text) unless text.empty?
81
+ yield(elem) unless elem.empty?
82
+ end
83
+ end
84
+ rescue LoadError
85
+ SplitRegexp = /#{self.stag_exp}|#{self.etag_exp}|(\n)/
86
+ def scan
87
+ @src.each do |line|
88
+ line.split(SplitRegexp).each do |token|
89
+ next if token.empty?
90
+ yield(token)
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ end
97
+
98
+ class Buffer
99
+ def initialize(compiler)
100
+ @compiler = compiler
101
+ @line = []
102
+ @script = ""
103
+ @compiler.pre_cmd.each do |x|
104
+ push(x)
105
+ end
106
+ end
107
+ attr_reader :script
108
+
109
+ def push(cmd)
110
+ @line << cmd
111
+ end
112
+
113
+ def cr
114
+ @script << (@line.join('; '))
115
+ @line = []
116
+ @script << "\n"
117
+ end
118
+
119
+ def close
120
+ return unless @line
121
+ @compiler.post_cmd.each do |x|
122
+ push(x)
123
+ end
124
+ @script << (@line.join('; '))
125
+ @line = nil
126
+ end
127
+ end
128
+
129
+ def compile(s)
130
+ out = Buffer.new(self)
131
+
132
+ content = ''
133
+ scanner = make_scanner(s)
134
+ scanner.scan do |token|
135
+ if scanner.stag.nil?
136
+ case token
137
+ when PercentLine
138
+ out.push("#{@put_cmd} #{content.dump}") if content.size > 0
139
+ content = ''
140
+ out.push(token.to_s)
141
+ out.cr
142
+ when :cr
143
+ out.cr
144
+ when '<?', '<?=', '<!---', '<?ruby', '<?ruby='
145
+ scanner.stag = token
146
+ out.push("#{@put_cmd} #{content.dump}") if content.size > 0
147
+ content = ''
148
+ when "\n"
149
+ content << "\n"
150
+ out.push("#{@put_cmd} #{content.dump}")
151
+ out.cr
152
+ content = ''
153
+ when '<??'
154
+ content << '<?'
155
+ else
156
+ content << token
157
+ end
158
+ else
159
+ # comments should include everything inside themselves
160
+ if scanner.stag == '<!---' && token != '--->'
161
+ content << token
162
+ else
163
+ case token
164
+ when '--->'
165
+ scanner.stag = nil
166
+ content = ''
167
+ # out.push("# #{content.dump}")
168
+ when '?>'
169
+ case scanner.stag
170
+ when '<?', '<?ruby'
171
+ if content[-1] == ?\n
172
+ content.chop!
173
+ out.push(content)
174
+ out.cr
175
+ else
176
+ out.push(content)
177
+ end
178
+ when '<?=', '<?ruby='
179
+ out.push("#{@put_cmd}((#{content}).to_s)")
180
+ #when '<?--'
181
+ # out.push("# #{content.dump}")
182
+ end
183
+ scanner.stag = nil
184
+ content = ''
185
+ when '??>'
186
+ content << '?>'
187
+ else
188
+ content << token
189
+ end
190
+ end
191
+ end
192
+ end
193
+ out.push("#{@put_cmd} #{content.dump}") if content.size > 0
194
+ out.close
195
+ out.script
196
+ end
197
+
198
+ def prepare_trim_mode(mode)
199
+ case mode
200
+ when 1
201
+ return [false, '>']
202
+ when 2
203
+ return [false, '<>']
204
+ when 0
205
+ return [false, nil]
206
+ when String
207
+ perc = mode.include?('?')
208
+ if mode.include?('-')
209
+ return [perc, '-']
210
+ elsif mode.include?('<>')
211
+ return [perc, '<>']
212
+ elsif mode.include?('>')
213
+ return [perc, '>']
214
+ else
215
+ [perc, nil]
216
+ end
217
+ else
218
+ return [false, nil]
219
+ end
220
+ end
221
+
222
+ def make_scanner(src)
223
+ Scanner.new(src, @xml_mode)
224
+ end
225
+
226
+ def initialize(xml_mode=nil)
227
+ @xml_mode = xml_mode
228
+ @put_cmd = 'Web::print'
229
+ @pre_cmd = []
230
+ @post_cmd = []
231
+ end
232
+ attr_accessor :put_cmd, :pre_cmd, :post_cmd
233
+ end
234
+ end
235
+
236
+ # PHPRB
237
+ class PHPRB
238
+ def initialize(str, safe_level=nil, xml_mode=nil )
239
+ @safe_level = safe_level
240
+ compiler = PHPRB::Compiler.new(xml_mode)
241
+
242
+ #, eoutvar='_erbout')
243
+ #set_eoutvar(compiler, eoutvar)
244
+ #def set_eoutvar(compiler, eoutvar = '_erbout')
245
+ #compiler.put_cmd = "#{eoutvar}.concat"
246
+ #
247
+ #cmd = []
248
+ #cmd.push "#{eoutvar} = ''"
249
+ #
250
+ #compiler.pre_cmd = cmd
251
+ #
252
+ #cmd = []
253
+ #cmd.push(eoutvar)
254
+ #
255
+ #compiler.post_cmd = cmd
256
+ #end
257
+
258
+ compiler.put_cmd = "Web::write"
259
+ #cmd = []
260
+ #cmd.push "#{eoutvar} = ''"
261
+ #
262
+ #compiler.pre_cmd = cmd
263
+ #
264
+ #cmd = []
265
+ #cmd.push(eoutvar)
266
+ #
267
+ #compiler.post_cmd = cmd
268
+
269
+
270
+ @src = compiler.compile(str)
271
+ @filename = nil
272
+ end
273
+ attr_reader :src
274
+ attr_accessor :filename
275
+
276
+ def run(b=TOPLEVEL_BINDING)
277
+ print self.result(b)
278
+ end
279
+
280
+ def result(b=TOPLEVEL_BINDING)
281
+ if @safe_level
282
+ th = Thread.start {
283
+ $SAFE = @safe_level
284
+ eval(@src, b)
285
+ }
286
+ return th.value
287
+ else
288
+ return eval(@src, b, (@filename || '(erb)'), 1)
289
+ end
290
+ end
291
+
292
+ def def_method(mod, methodname, fname='(PHPRB)')
293
+ mod.module_eval("def #{methodname}\n" + self.src + "\nend\n", fname, 0)
294
+ end
295
+
296
+ def def_module(methodname='erb')
297
+ mod = Module.new
298
+ def_method(mod, methodname)
299
+ mod
300
+ end
301
+
302
+ def def_class(superklass=Object, methodname='result')
303
+ cls = Class.new(superklass)
304
+ def_method(cls, methodname)
305
+ cls
306
+ end
307
+ end
308
+
309
+ # PHPRB::Util
310
+ class PHPRB
311
+ module Util
312
+ public
313
+ def html_escape(s)
314
+ s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
315
+ end
316
+ alias h html_escape
317
+
318
+ def url_encode(s)
319
+ s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
320
+ end
321
+ alias u url_encode
322
+ end
323
+ end
324
+
325
+ # PHPRB::DefMethod
326
+ class PHPRB
327
+ module DefMethod
328
+ public
329
+ def def_erb_method(methodname, erb)
330
+ if erb.kind_of? String
331
+ fname = erb
332
+ File.open(fname) {|f| erb = PHPRB.new(f.read) }
333
+ erb.def_method(self, methodname, fname)
334
+ else
335
+ erb.def_method(self, methodname)
336
+ end
337
+ end
338
+ module_function :def_erb_method
339
+ end
340
+ end