walrus 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/bin/walrus +19 -12
  2. data/lib/walrus.rb +27 -70
  3. data/lib/walrus/additions/string.rb +21 -35
  4. data/lib/walrus/compile_error.rb +19 -16
  5. data/lib/walrus/compiler.rb +66 -55
  6. data/lib/walrus/contrib/spec/walruscloth_spec.rb +21 -17
  7. data/lib/walrus/contrib/walruscloth.rb +19 -11
  8. data/lib/walrus/document.rb +41 -33
  9. data/lib/walrus/grammar.rb +474 -162
  10. data/lib/walrus/grammar/assignment_expression.rb +33 -0
  11. data/lib/walrus/grammar/block_directive.rb +37 -0
  12. data/lib/walrus/grammar/comment.rb +33 -0
  13. data/lib/walrus/grammar/def_directive.rb +80 -0
  14. data/lib/walrus/grammar/echo_directive.rb +56 -0
  15. data/lib/walrus/grammar/escape_sequence.rb +33 -0
  16. data/lib/walrus/grammar/import_directive.rb +54 -0
  17. data/lib/walrus/grammar/include_directive.rb +36 -0
  18. data/lib/walrus/grammar/instance_variable.rb +33 -0
  19. data/lib/walrus/grammar/literal.rb +33 -0
  20. data/lib/walrus/grammar/message_expression.rb +34 -0
  21. data/lib/walrus/grammar/multiline_comment.rb +70 -0
  22. data/lib/walrus/grammar/placeholder.rb +47 -0
  23. data/lib/walrus/grammar/raw_directive.rb +50 -0
  24. data/lib/walrus/grammar/raw_text.rb +56 -0
  25. data/lib/walrus/grammar/ruby_directive.rb +41 -0
  26. data/lib/walrus/grammar/ruby_expression.rb +42 -0
  27. data/lib/walrus/grammar/set_directive.rb +34 -0
  28. data/lib/walrus/grammar/silent_directive.rb +51 -0
  29. data/lib/walrus/grammar/slurp_directive.rb +36 -0
  30. data/lib/walrus/grammar/super_directive.rb +34 -0
  31. data/lib/walrus/parser.rb +26 -408
  32. data/lib/walrus/runner.rb +37 -20
  33. data/lib/walrus/template.rb +34 -25
  34. data/lib/walrus/version.rb +24 -1
  35. metadata +57 -71
  36. data/ext/extconf.rb +0 -16
  37. data/ext/jindex.c +0 -92
  38. data/lib/walrus/diff.rb +0 -95
  39. data/lib/walrus/grammar/additions/proc.rb +0 -26
  40. data/lib/walrus/grammar/additions/regexp.rb +0 -27
  41. data/lib/walrus/grammar/additions/string.rb +0 -58
  42. data/lib/walrus/grammar/additions/symbol.rb +0 -49
  43. data/lib/walrus/grammar/and_predicate.rb +0 -46
  44. data/lib/walrus/grammar/array_result.rb +0 -25
  45. data/lib/walrus/grammar/continuation_wrapper_exception.rb +0 -34
  46. data/lib/walrus/grammar/left_recursion_exception.rb +0 -33
  47. data/lib/walrus/grammar/location_tracking.rb +0 -115
  48. data/lib/walrus/grammar/match_data_wrapper.rb +0 -71
  49. data/lib/walrus/grammar/memoizing.rb +0 -47
  50. data/lib/walrus/grammar/memoizing_cache.rb +0 -103
  51. data/lib/walrus/grammar/node.rb +0 -66
  52. data/lib/walrus/grammar/not_predicate.rb +0 -46
  53. data/lib/walrus/grammar/parse_error.rb +0 -45
  54. data/lib/walrus/grammar/parser_state.rb +0 -187
  55. data/lib/walrus/grammar/parslet.rb +0 -34
  56. data/lib/walrus/grammar/parslet_choice.rb +0 -128
  57. data/lib/walrus/grammar/parslet_combination.rb +0 -32
  58. data/lib/walrus/grammar/parslet_combining.rb +0 -160
  59. data/lib/walrus/grammar/parslet_merge.rb +0 -94
  60. data/lib/walrus/grammar/parslet_omission.rb +0 -63
  61. data/lib/walrus/grammar/parslet_repetition.rb +0 -106
  62. data/lib/walrus/grammar/parslet_repetition_default.rb +0 -64
  63. data/lib/walrus/grammar/parslet_sequence.rb +0 -214
  64. data/lib/walrus/grammar/predicate.rb +0 -63
  65. data/lib/walrus/grammar/proc_parslet.rb +0 -58
  66. data/lib/walrus/grammar/regexp_parslet.rb +0 -79
  67. data/lib/walrus/grammar/skipped_substring_exception.rb +0 -42
  68. data/lib/walrus/grammar/string_enumerator.rb +0 -53
  69. data/lib/walrus/grammar/string_parslet.rb +0 -81
  70. data/lib/walrus/grammar/string_result.rb +0 -30
  71. data/lib/walrus/grammar/symbol_parslet.rb +0 -69
  72. data/lib/walrus/no_parameter_marker.rb +0 -25
  73. data/lib/walrus/walrus_grammar/assignment_expression.rb +0 -30
  74. data/lib/walrus/walrus_grammar/block_directive.rb +0 -34
  75. data/lib/walrus/walrus_grammar/comment.rb +0 -30
  76. data/lib/walrus/walrus_grammar/def_directive.rb +0 -72
  77. data/lib/walrus/walrus_grammar/echo_directive.rb +0 -50
  78. data/lib/walrus/walrus_grammar/escape_sequence.rb +0 -30
  79. data/lib/walrus/walrus_grammar/import_directive.rb +0 -50
  80. data/lib/walrus/walrus_grammar/include_directive.rb +0 -33
  81. data/lib/walrus/walrus_grammar/instance_variable.rb +0 -30
  82. data/lib/walrus/walrus_grammar/literal.rb +0 -30
  83. data/lib/walrus/walrus_grammar/message_expression.rb +0 -31
  84. data/lib/walrus/walrus_grammar/multiline_comment.rb +0 -60
  85. data/lib/walrus/walrus_grammar/placeholder.rb +0 -46
  86. data/lib/walrus/walrus_grammar/raw_directive.rb +0 -48
  87. data/lib/walrus/walrus_grammar/raw_text.rb +0 -51
  88. data/lib/walrus/walrus_grammar/ruby_directive.rb +0 -35
  89. data/lib/walrus/walrus_grammar/ruby_expression.rb +0 -37
  90. data/lib/walrus/walrus_grammar/set_directive.rb +0 -30
  91. data/lib/walrus/walrus_grammar/silent_directive.rb +0 -50
  92. data/lib/walrus/walrus_grammar/slurp_directive.rb +0 -31
  93. data/lib/walrus/walrus_grammar/super_directive.rb +0 -33
@@ -1,29 +1,38 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
1
+ # Copyright 2007-2010 Wincent Colaiuta. All rights reserved.
2
+ # Redistribution and use in source and binary forms, with or without
3
+ # modification, are permitted provided that the following conditions are met:
6
4
  #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
5
+ # 1. Redistributions of source code must retain the above copyright notice,
6
+ # this list of conditions and the following disclaimer.
7
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
8
+ # this list of conditions and the following disclaimer in the documentation
9
+ # and/or other materials provided with the distribution.
11
10
  #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
11
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
15
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21
+ # POSSIBILITY OF SUCH DAMAGE.
14
22
 
15
23
  require 'walrus'
24
+ require 'walrat/additions/string.rb'
16
25
 
17
26
  module Walrus
18
27
  class Template
19
-
20
28
  attr_reader :base_text
21
-
22
- # If initialized using a Pathname or File, returns the pathname. Otherwise returns nil.
29
+
30
+ # If initialized using a Pathname or File, returns the pathname.
31
+ # Otherwise returns nil.
23
32
  attr_reader :origin
24
-
33
+
25
34
  # Accepts input of class String, Pathname or File
26
- def initialize(input)
35
+ def initialize input
27
36
  raise ArgumentError if input.nil?
28
37
  if input.respond_to? :read # should work with Pathname or File
29
38
  @base_text = input.read
@@ -32,32 +41,33 @@ module Walrus
32
41
  @base_text = input.to_s.clone
33
42
  end
34
43
  end
35
-
36
- # The fill method returns a string containing the output produced when executing the compiled template.
44
+
45
+ # The fill method returns a string containing the output produced when
46
+ # executing the compiled template.
37
47
  def fill
38
48
  @filled ||= instance_eval(compiled)
39
49
  end
40
-
50
+
41
51
  def filled
42
52
  fill
43
53
  end
44
-
54
+
45
55
  # Parses template, returning compiled input (suitable for writing to disk).
46
56
  def compile
47
57
  @parser ||= Parser.new
48
58
  @compiled ||= @parser.compile(@base_text, :class_name => class_name, :origin => @origin)
49
59
  end
50
-
60
+
51
61
  # Returns the compiled text of the receiver
52
62
  def compiled
53
63
  compile
54
64
  end
55
-
65
+
56
66
  # Prints output obtained by running the compiled template.
57
67
  def run
58
68
  p fill
59
69
  end
60
-
70
+
61
71
  def class_name
62
72
  if @class_name
63
73
  @class_name
@@ -69,7 +79,7 @@ module Walrus
69
79
  end
70
80
  end
71
81
  end
72
-
82
+
73
83
  def strip_extensions(path)
74
84
  extension = File.extname(path)
75
85
  if extension != "" # recurse
@@ -78,6 +88,5 @@ module Walrus
78
88
  path
79
89
  end
80
90
  end
81
-
82
91
  end # class Template
83
92
  end # module Walrus
@@ -1,3 +1,26 @@
1
+ # Copyright 2007-2010 Wincent Colaiuta. All rights reserved.
2
+ # Redistribution and use in source and binary forms, with or without
3
+ # modification, are permitted provided that the following conditions are met:
4
+ #
5
+ # 1. Redistributions of source code must retain the above copyright notice,
6
+ # this list of conditions and the following disclaimer.
7
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
8
+ # this list of conditions and the following disclaimer in the documentation
9
+ # and/or other materials provided with the distribution.
10
+ #
11
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
15
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21
+ # POSSIBILITY OF SUCH DAMAGE.
22
+
1
23
  module Walrus
2
- VERSION = '0.2'
24
+ VERSION = '0.3'
25
+ COPYRIGHT = 'Copyright 2007-2010 Wincent Colaiuta'
3
26
  end # module Walrus
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- version: "0.2"
7
+ - 3
8
+ version: "0.3"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Wincent Colaiuta
@@ -13,13 +13,25 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-08-14 00:00:00 +02:00
16
+ date: 2010-08-21 00:00:00 +02:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
- name: wopen3
20
+ name: walrat
21
21
  prerelease: false
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
28
+ version: "0"
29
+ type: :runtime
30
+ version_requirements: *id001
31
+ - !ruby/object:Gem::Dependency
32
+ name: wopen3
33
+ prerelease: false
34
+ requirement: &id002 !ruby/object:Gem::Requirement
23
35
  requirements:
24
36
  - - ">="
25
37
  - !ruby/object:Gem::Version
@@ -28,11 +40,11 @@ dependencies:
28
40
  - 1
29
41
  version: "0.1"
30
42
  type: :runtime
31
- version_requirements: *id001
43
+ version_requirements: *id002
32
44
  - !ruby/object:Gem::Dependency
33
45
  name: mkdtemp
34
46
  prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
47
+ requirement: &id003 !ruby/object:Gem::Requirement
36
48
  requirements:
37
49
  - - ">="
38
50
  - !ruby/object:Gem::Version
@@ -41,11 +53,11 @@ dependencies:
41
53
  - 0
42
54
  version: "1.0"
43
55
  type: :development
44
- version_requirements: *id002
56
+ version_requirements: *id003
45
57
  - !ruby/object:Gem::Dependency
46
58
  name: rspec
47
59
  prerelease: false
48
- requirement: &id003 !ruby/object:Gem::Requirement
60
+ requirement: &id004 !ruby/object:Gem::Requirement
49
61
  requirements:
50
62
  - - "="
51
63
  - !ruby/object:Gem::Version
@@ -55,13 +67,25 @@ dependencies:
55
67
  - 0
56
68
  version: 1.3.0
57
69
  type: :development
58
- version_requirements: *id003
59
- description: " Walrus is an object-oriented templating system inspired by and similar\n to the Cheetah Python-powered template engine. It includes a Parser\n Expression Grammar (PEG) parser generator capable of generating an\n integrated lexer, \"packrat\" parser, and Abstract Syntax Tree (AST)\n builder.\n"
70
+ version_requirements: *id004
71
+ - !ruby/object:Gem::Dependency
72
+ name: yard
73
+ prerelease: false
74
+ requirement: &id005 !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id005
83
+ description: " Walrus is an object-oriented templating system inspired by and similar\n to the Cheetah Python-powered template engine.\n"
60
84
  email: win@wincent.com
61
85
  executables:
62
86
  - walrus
63
- extensions:
64
- - ext/extconf.rb
87
+ extensions: []
88
+
65
89
  extra_rdoc_files: []
66
90
 
67
91
  files:
@@ -71,71 +95,34 @@ files:
71
95
  - lib/walrus/compiler.rb
72
96
  - lib/walrus/contrib/spec/walruscloth_spec.rb
73
97
  - lib/walrus/contrib/walruscloth.rb
74
- - lib/walrus/diff.rb
75
98
  - lib/walrus/document.rb
76
- - lib/walrus/grammar/additions/proc.rb
77
- - lib/walrus/grammar/additions/regexp.rb
78
- - lib/walrus/grammar/additions/string.rb
79
- - lib/walrus/grammar/additions/symbol.rb
80
- - lib/walrus/grammar/and_predicate.rb
81
- - lib/walrus/grammar/array_result.rb
82
- - lib/walrus/grammar/continuation_wrapper_exception.rb
83
- - lib/walrus/grammar/left_recursion_exception.rb
84
- - lib/walrus/grammar/location_tracking.rb
85
- - lib/walrus/grammar/match_data_wrapper.rb
86
- - lib/walrus/grammar/memoizing.rb
87
- - lib/walrus/grammar/memoizing_cache.rb
88
- - lib/walrus/grammar/node.rb
89
- - lib/walrus/grammar/not_predicate.rb
90
- - lib/walrus/grammar/parse_error.rb
91
- - lib/walrus/grammar/parser_state.rb
92
- - lib/walrus/grammar/parslet.rb
93
- - lib/walrus/grammar/parslet_choice.rb
94
- - lib/walrus/grammar/parslet_combination.rb
95
- - lib/walrus/grammar/parslet_combining.rb
96
- - lib/walrus/grammar/parslet_merge.rb
97
- - lib/walrus/grammar/parslet_omission.rb
98
- - lib/walrus/grammar/parslet_repetition.rb
99
- - lib/walrus/grammar/parslet_repetition_default.rb
100
- - lib/walrus/grammar/parslet_sequence.rb
101
- - lib/walrus/grammar/predicate.rb
102
- - lib/walrus/grammar/proc_parslet.rb
103
- - lib/walrus/grammar/regexp_parslet.rb
104
- - lib/walrus/grammar/skipped_substring_exception.rb
105
- - lib/walrus/grammar/string_enumerator.rb
106
- - lib/walrus/grammar/string_parslet.rb
107
- - lib/walrus/grammar/string_result.rb
108
- - lib/walrus/grammar/symbol_parslet.rb
99
+ - lib/walrus/grammar/assignment_expression.rb
100
+ - lib/walrus/grammar/block_directive.rb
101
+ - lib/walrus/grammar/comment.rb
102
+ - lib/walrus/grammar/def_directive.rb
103
+ - lib/walrus/grammar/echo_directive.rb
104
+ - lib/walrus/grammar/escape_sequence.rb
105
+ - lib/walrus/grammar/import_directive.rb
106
+ - lib/walrus/grammar/include_directive.rb
107
+ - lib/walrus/grammar/instance_variable.rb
108
+ - lib/walrus/grammar/literal.rb
109
+ - lib/walrus/grammar/message_expression.rb
110
+ - lib/walrus/grammar/multiline_comment.rb
111
+ - lib/walrus/grammar/placeholder.rb
112
+ - lib/walrus/grammar/raw_directive.rb
113
+ - lib/walrus/grammar/raw_text.rb
114
+ - lib/walrus/grammar/ruby_directive.rb
115
+ - lib/walrus/grammar/ruby_expression.rb
116
+ - lib/walrus/grammar/set_directive.rb
117
+ - lib/walrus/grammar/silent_directive.rb
118
+ - lib/walrus/grammar/slurp_directive.rb
119
+ - lib/walrus/grammar/super_directive.rb
109
120
  - lib/walrus/grammar.rb
110
- - lib/walrus/no_parameter_marker.rb
111
121
  - lib/walrus/parser.rb
112
122
  - lib/walrus/runner.rb
113
123
  - lib/walrus/template.rb
114
124
  - lib/walrus/version.rb
115
- - lib/walrus/walrus_grammar/assignment_expression.rb
116
- - lib/walrus/walrus_grammar/block_directive.rb
117
- - lib/walrus/walrus_grammar/comment.rb
118
- - lib/walrus/walrus_grammar/def_directive.rb
119
- - lib/walrus/walrus_grammar/echo_directive.rb
120
- - lib/walrus/walrus_grammar/escape_sequence.rb
121
- - lib/walrus/walrus_grammar/import_directive.rb
122
- - lib/walrus/walrus_grammar/include_directive.rb
123
- - lib/walrus/walrus_grammar/instance_variable.rb
124
- - lib/walrus/walrus_grammar/literal.rb
125
- - lib/walrus/walrus_grammar/message_expression.rb
126
- - lib/walrus/walrus_grammar/multiline_comment.rb
127
- - lib/walrus/walrus_grammar/placeholder.rb
128
- - lib/walrus/walrus_grammar/raw_directive.rb
129
- - lib/walrus/walrus_grammar/raw_text.rb
130
- - lib/walrus/walrus_grammar/ruby_directive.rb
131
- - lib/walrus/walrus_grammar/ruby_expression.rb
132
- - lib/walrus/walrus_grammar/set_directive.rb
133
- - lib/walrus/walrus_grammar/silent_directive.rb
134
- - lib/walrus/walrus_grammar/slurp_directive.rb
135
- - lib/walrus/walrus_grammar/super_directive.rb
136
125
  - lib/walrus.rb
137
- - ext/jindex.c
138
- - ext/extconf.rb
139
126
  has_rdoc: true
140
127
  homepage: https://wincent.com/products/walrus
141
128
  licenses: []
@@ -145,7 +132,6 @@ rdoc_options: []
145
132
 
146
133
  require_paths:
147
134
  - lib
148
- - ext
149
135
  required_ruby_version: !ruby/object:Gem::Requirement
150
136
  requirements:
151
137
  - - ">="
@@ -1,16 +0,0 @@
1
- # Copyright 2007-2010 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'mkmf'
16
- create_makefile('jindex')
@@ -1,92 +0,0 @@
1
- /*
2
- Copyright 2007-2010 Wincent Colaiuta
3
- This program is free software: you can redistribute it and/or modify
4
- it under the terms of the GNU General Public License as published by
5
- the Free Software Foundation, either version 3 of the License, or
6
- (at your option) any later version.
7
-
8
- This program is distributed in the hope that it will be useful,
9
- but WITHOUT ANY WARRANTY; without even the implied warranty of
10
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
- GNU General Public License for more details.
12
-
13
- You should have received a copy of the GNU General Public License
14
- along with this program. If not, see <http://www.gnu.org/licenses/>.
15
- */
16
-
17
- #include <ruby.h>
18
-
19
- /*
20
-
21
- Common code used by almost identical "jindex" and "jrindex" methods. A C
22
- extension is necessary here because with direct access to the rb_backref_get
23
- and rb_backref_set API there is no way to propagate $~ back to the caller after
24
- invoking the "index" and "rindex" methods. The code is basically equivalent to
25
- the following Ruby code:
26
-
27
- def index arg1, optional_arg
28
- index = super # plus code here for deciding whether or not to pass optional argument
29
- match = ($~ ? $~.clone : nil)
30
- index unpack('C*')[0...index].pack('C*').jlength # jlength clobbers $~ as a side effect, should consider rewriting it
31
- $~ = match # in pure Ruby setting $~ here has only a local effect (not seen by caller)
32
- index
33
- end
34
-
35
- */
36
- static VALUE walrus_str_index_common(ID func, int argc, VALUE *argv, VALUE str)
37
- {
38
- VALUE jindex = Qnil; /* default return value */
39
- VALUE index = rb_funcall2(str, func, argc, argv); /* call String#index or String#rindex*/
40
- VALUE match = rb_backref_get(); /* save $~ */
41
- if (!NIL_P(index))
42
- {
43
- VALUE packing_format = rb_str_new2("C*");
44
- VALUE unpacked = rb_funcall(str, rb_intern("unpack"), 1, packing_format); /* unpack('C*') */
45
- VALUE range = rb_funcall(rb_cRange, rb_intern("new"), 3, INT2FIX(0), index, Qtrue);
46
- VALUE slice = rb_funcall(unpacked, rb_intern("slice"), 1, range); /* [0...idx] */
47
- VALUE packed = rb_funcall(slice, rb_intern("pack"), 1, packing_format); /* pack('C*') */
48
- jindex = rb_funcall(packed, rb_intern("jlength"), 0); /* jlength */
49
- }
50
- rb_backref_set(match); /* restore $~ */
51
- return jindex;
52
- }
53
-
54
- /*
55
-
56
- call-seq:
57
- str.jindex(substring [, offset]) -> Fixnum or nil
58
- str.jindex(fixnum [, offset]) -> Fixnum or nil
59
- str.jindex(regexp [, offset]) -> Fixnum or nil
60
-
61
- Multibyte-friendly equivalent of the String#index method. If $KCODE is
62
- appropriately set will return an accurate index based on character count rather
63
- than byte counts.
64
-
65
- */
66
- static VALUE walrus_str_jindex_m(int argc, VALUE *argv, VALUE str)
67
- {
68
- return walrus_str_index_common(rb_intern("index"), argc, argv, str);
69
- }
70
-
71
- /*
72
-
73
- call-seq:
74
- str.jrindex(substring [, offset]) -> Fixnum or nil
75
- str.jrindex(fixnum [, offset]) -> Fixnum or nil
76
- str.jrindex(regexp [, offset]) -> Fixnum or nil
77
-
78
- Multibyte-friendly equivalent of the String#rindex method. If $KCODE is
79
- appropriately set will return an accurate index based on character count rather
80
- than byte counts.
81
-
82
- */
83
- static VALUE walrus_str_jrindex_m(int argc, VALUE *argv, VALUE str)
84
- {
85
- return walrus_str_index_common(rb_intern("rindex"), argc, argv, str);
86
- }
87
-
88
- void Init_jindex()
89
- {
90
- rb_define_method(rb_cString, "jindex", walrus_str_jindex_m, -1);
91
- rb_define_method(rb_cString, "jrindex", walrus_str_jrindex_m, -1);
92
- }
@@ -1,95 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
- require 'open3'
17
- require 'pathname'
18
- require 'tempfile'
19
-
20
- # Simple wrapper for diff(1).
21
- class Diff
22
-
23
- def initialize(left, right)
24
- raise ArgumentError.new('left may not be nil') unless left
25
- raise ArgumentError.new('right may not be nil') unless right
26
-
27
- # keep a copy of the parameters (used by the diff method)
28
- @left = left.clone
29
- @right = right.clone
30
-
31
- if left.kind_of? Pathname and right.kind_of? Pathname
32
- @output = self.compare_pathname_with_pathname(left, right)
33
- elsif left.kind_of? String and right.kind_of? Pathname
34
- @output = self.compare_string_with_pathname(left, right)
35
- elsif left.kind_of? Pathname and right.kind_of? String
36
- @output = self.compare_pathname_with_string(left, right)
37
- elsif left.kind_of? String and right.kind_of? String
38
- @output = self.compare_string_with_string(left, right)
39
- else
40
- raise ArgumentError.new('unsupported argument types (%s, %s)' % [left.class.to_s, right.class.to_s])
41
- end
42
-
43
- end
44
-
45
- def compare_pathname_with_pathname(left, right)
46
- self.diff(left.realpath, right.realpath)
47
- end
48
-
49
- def compare_string_with_pathname(left, right)
50
- # will pipe left in over standard input
51
- self.diff('-', right.realpath)
52
- end
53
-
54
- def compare_pathname_with_string(left, right)
55
- # will pipe right in over standard input
56
- self.diff(left.realpath, '-')
57
- end
58
-
59
- # This is the least secure comparison method because it requires the creation of a temporary file and Ruby's builtin Tempfile class is not secure.
60
- def compare_string_with_string(left, right)
61
- # incorporate a psuedo-random component to make race conditions more difficult to exploit
62
- tempfile = Tempfile.new('walrus-%s' % rand(100000).to_s)
63
- tempfile.unlink # shut the race condition vulnerability window as soon as possible
64
- tempfile.write(left)
65
- tempfile.close
66
- self.diff(tempfile.path, "-")
67
- end
68
-
69
- # Actually runs diff(1) with the supplied arguments (pathnames). One but not both of the arguments may be "-" to indicate the standard input.
70
- def self.diff(left, right)
71
- raise ArgumentError.new('left may not be ni') unless left
72
- raise ArgumentError.new('right may not be nil') unless right
73
- raise ArgumentError.new('only one parameter may be "-"') if left == '-' and right == '-'
74
-
75
- # no shell involved, so no need to escape shell metacharacters
76
- Open3.popen3('diff', '-u', left, right) do |stdin, stdout, stderr|
77
- if left == '-'
78
- stdin.write(@left)
79
- elsif right == '-'
80
- stdin.write(@right)
81
- end
82
- stdin.close_write
83
-
84
- # TODO: decide what to do with stderr output (if any)
85
- stderr.read
86
- return stdout.read
87
- end
88
-
89
- end
90
-
91
- def to_s
92
- @output
93
- end
94
-
95
- end