ltdtemplate 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.yardopts CHANGED
@@ -1,4 +1,4 @@
1
1
  --tag copyright:Copyright
2
2
  --tag license:License
3
3
  -
4
- HISTORY.txt RESOURCES.txt TEMPLATE_MANUAL.html
4
+ CHANGELOG RESOURCES TEMPLATE_MANUAL.html
@@ -1,3 +1,13 @@
1
+ 2013-07-29 Version 0.2.2
2
+ Added string methods capcase, downcase, join, regexp,
3
+ rep/replace, rep1/replace1, split, and upcase.
4
+
5
+ Added regexp special object and methods ci/ignorecase,
6
+ ext/extended, and multi/multiline.
7
+
8
+ Renamed HISTORY.txt to CHANGELOG to conform to best practices;
9
+ also renamed RESOURCES.txt to RESOURCES.
10
+
1
11
  2013-07-28 Version 0.2.1
2
12
  Added array.{each,each_rnd,each_seq} methods (executing the code
3
13
  block supplied as the first parameter with parameters key and
@@ -15,6 +15,10 @@ calls
15
15
  iterations
16
16
  The total number of loop iterations
17
17
 
18
+ regexp (limit)
19
+ A string may only be converted to a regexp if limit regexp
20
+ is set to false before rendering
21
+
18
22
  string_length
19
23
  The combined total length of strings created
20
24
 
data/TEMPLATE_MANUAL.html CHANGED
@@ -11,7 +11,7 @@ td:first-child, th:first-child { padding-left: 0.3em }
11
11
  </head>
12
12
  <body>
13
13
  <h1 id='top'>LtdTemplate Manual<br>
14
- <span style='font-size: smaller'>Version 0.1.3; July 13, 2013</span></h1>
14
+ <span style='font-size: smaller'>Version 0.2.2; July 29, 2013</span></h1>
15
15
 
16
16
  <h2>Author(s)</h2>
17
17
 
@@ -404,6 +404,7 @@ $.*(1, 2, 3).list /* => 1, 2, 3 */</code></blockquote>
404
404
  <li><a href='#nil_value'>Nil</a></li>
405
405
  <li><a href='#number_values'>Numbers</a></li>
406
406
  <li><a href='#string_values'>Strings</a></li>
407
+ <li><a href='#regexp_values'>Regular Expressions</a></li>
407
408
  </ul>
408
409
 
409
410
  <h3 id='array_values'>Arrays</h3>
@@ -758,6 +759,13 @@ for the nil value as follows:</p>
758
759
 
759
760
  </table>
760
761
 
762
+ <p><a href='#codeblock_syntax'>Code blocks</a> may be bound as methods for
763
+ all numeric values as follows:</p>
764
+
765
+ <blockquote><code>@Number?=(')
766
+ @Number['</code><i>method_name</i><code>]=({</code>
767
+ <i>code sequence</i> <code>})</code></blockquote>
768
+
761
769
  <p>[<a href='#contents'>Contents</a>] [<a href='#values_methods'>Values And Methods</a>]</p>
762
770
 
763
771
  <h3 id='string_values'>Strings</h3>
@@ -772,11 +780,18 @@ for the nil value as follows:</p>
772
780
 
773
781
  <tr><td><code>class</code></td><td>Returns the string "String"</td></tr>
774
782
 
783
+ <tr><td><code>capcase</code></td>
784
+ <td>Returns the string with each space-separated word capitalized
785
+ (since 0.2.2)</td></tr>
786
+
787
+ <tr><td><code>downcase</code></td>
788
+ <td>Returns the string entirely in lowercase (since 0.2.2)</td></tr>
789
+
775
790
  <tr><td><code>flt</code>, <code>float</code></td>
776
791
  <td>Returns the floating-point value of the string</td></tr>
777
792
 
778
793
  <tr><td><code>html</code></td>
779
- <td>Returns the HTML encoding of the string</td></tr>
794
+ <td>Returns the HTML encoding of the string (since 0.1.4)</td></tr>
780
795
 
781
796
  <tr><td><code>idx(</code><i>target</i><code>,
782
797
  </code><i>offset</i><code>)</code>,
@@ -789,12 +804,38 @@ for the nil value as follows:</p>
789
804
  <tr><td><code>int</code></td>
790
805
  <td>Returns the integer value of the string</td></tr>
791
806
 
807
+ <tr><td><code>join(</code><i>list</i><code>)</code></td>
808
+ <td>Equivalent to
809
+ <code>$.array(<i>list</i><code>).join(</code><i>string</i><code>)</code>
810
+ (since 0.2.2)</td></tr>
811
+
792
812
  <tr><td><code>len</code>, <code>length</code></td>
793
813
  <td>Returns the length of the string</td></tr>
794
814
 
795
815
  <tr><td><code>pcte</code></td>
796
816
  <td>Returns the "percent encoding" of the string
797
- (for URI components)</td></tr>
817
+ (for URI components; since 0.1.4)</td></tr>
818
+
819
+ <tr><td><code>regexp</code></td>
820
+ <td>Returns a "<a href='#regexp_values'>regular expression</a>"
821
+ version of the string (only if enabled by the calling program;
822
+ since 0.2.2)</td></tr>
823
+
824
+ <tr><td><code>rep(</code><i>pattern</i><code>,</code>
825
+ <i>replacement</i><code>)</code>,
826
+ <code>replace(</code><i>pattern</i><code>,</code>
827
+ <i>replacement</i><code>)</code></td>
828
+ <td>Returns a copy of the string with all occurrences of <i>pattern</i>
829
+ replaced with <i>replacement</i>; the <i>pattern</i> may be a
830
+ <a href='#regexp_values'>regular expression</a> (since 0.2.2)</td></tr>
831
+
832
+ <tr><td><code>rep1, replace1</code></td>
833
+ <td>Like <code>rep</code>/<code>replace</code>, but with only the
834
+ first occurrence replaced (since 0.2.2)</td></tr>
835
+
836
+ <tr><td><code>split(</code><i>pattern</i>, <i>limit</i><code>)</code></td>
837
+ <td>Returns an array of up to <i>limit</i> (default unlimited)
838
+ substrings split at <i>pattern</i> (since 0.2.2)</td></tr>
798
839
 
799
840
  <tr><td><code>ridx(</code><i>string</i><code>,
800
841
  </code><i>offset</i><code>)</code>,
@@ -826,6 +867,9 @@ for the nil value as follows:</p>
826
867
 
827
868
  <tr><td><code>type</code></td><td>Returns the string "string"</td></tr>
828
869
 
870
+ <tr><td><code>upcase</code></td>
871
+ <td>Returns the string entirely in uppercase</td></tr>
872
+
829
873
  <tr><td><code>+(</code><i>list</i><code>)</code></td>
830
874
  <td>Returns the concatenation of the string and the items in
831
875
  <i>list</i></td></tr>
@@ -846,6 +890,48 @@ for the nil value as follows:</p>
846
890
 
847
891
  </table>
848
892
 
893
+ <p><a href='#codeblock_syntax'>Code blocks</a> may be bound as methods for
894
+ all string values as follows:</p>
895
+
896
+ <blockquote><code>@String?=(')
897
+ @String['</code><i>method_name</i><code>]=({</code>
898
+ <i>code sequence</i> <code>})</code></blockquote>
899
+
900
+ <p>[<a href='#contents'>Contents</a>] [<a href='#values_methods'>Values And Methods</a>]</p>
901
+
902
+ <h3 id='regexp_values'>Regular Expressions</h3>
903
+
904
+ <p>"<a href='http://en.wikipedia.org/wiki/Regular_expression'>Regular
905
+ expressions</a>" ("regex" or "regexp" for short) in LtdTemplate are
906
+ specially enhanced, "magic strings". They behave like regular strings
907
+ unless used in a method call that is able to use them as a regular
908
+ expression.</p>
909
+
910
+ <p>Regular expressions must be enabled by the calling program. See
911
+ the gem documentation for details.</p>
912
+
913
+ <p>Regexp-specific methods:</p>
914
+
915
+ <table>
916
+
917
+ <tr><th>Method</th><th>Description</th></tr>
918
+
919
+ <tr><td><code>ci, ignorecase</code></td>
920
+ <td>Make pattern case-insensitive</td></tr>
921
+
922
+ <tr><td><code>ext, extended</code></td>
923
+ <td>Allow white space and comments in the pattern</td></tr>
924
+
925
+ <tr><td><code>multi, multiline</code></td>
926
+ <td>Makes <code>.</code> match newlines too</td></tr>
927
+
928
+ <tr><td><code>type</code></td><td>Returns the string "regexp"</td></tr>
929
+
930
+ </table>
931
+
932
+ <blockquote><code>"Example".replace("[aeiou]".regexp.ci,'_)
933
+ /* => _x_mpl_ */</code></blockquote>
934
+
849
935
  <p>[<a href='#contents'>Contents</a>] [<a href='#values_methods'>Values And Methods</a>]</p>
850
936
 
851
937
  <h2 id='examples'>(Some More) Examples</h2>
data/lib/ltdtemplate.rb CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  class LtdTemplate
10
10
 
11
+ VERSION = '0.2.2'
12
+
11
13
  TOKEN_MAP = {
12
14
  ?. => :dot, # method separator
13
15
  '..' => :dotdot, # begin named values
@@ -21,21 +21,28 @@ class LtdTemplate::Value::String < LtdTemplate::Code
21
21
  def get_value (opts = {})
22
22
  case opts[:method]
23
23
  when nil, 'call', 'str', 'string' then self
24
+ when 'capcase' then @template.factory :string, @value.capitalize
24
25
  when 'class' then @template.factory :string, 'String'
26
+ when 'downcase' then @template.factory :string, @value.downcase
25
27
  when 'flt', 'float' then @template.factory :number, @value.to_f
26
28
  when 'html'
27
29
  require 'htmlentities'
28
30
  @template.factory :string, HTMLEntities.new(:html4).
29
31
  encode(@value, :basic, :named, :decimal)
32
+ when 'idx', 'index', 'ridx', 'rindex' then do_index opts
30
33
  when 'int' then @template.factory :number, @value.to_i
34
+ when 'join' then do_join opts
31
35
  when 'len', 'length' then @template.factory :number, @value.length
32
36
  when 'pcte' then @template.factory(:string,
33
37
  @value.gsub(/[^a-z0-9]/i) { |c| sprintf "%%%2x", c.ord })
38
+ when 'regexp' then do_regexp opts
39
+ when 'rep', 'rep1', 'replace', 'replace1' then do_replace opts
34
40
  when 'rng', 'range', 'slc', 'slice' then do_range_slice opts
41
+ when 'split' then do_split opts
35
42
  when 'type' then @template.factory :string, 'string'
43
+ when 'upcase' then @template.factory :string, @value.upcase
36
44
  when '+' then do_add opts
37
45
  when '*' then do_multiply opts
38
- when 'idx', 'index', 'ridx', 'rindex' then do_index opts
39
46
  when '<', '<=', '==', '!=', '>=', '>' then do_compare opts
40
47
  else do_method opts, 'String'
41
48
  end
@@ -108,6 +115,18 @@ class LtdTemplate::Value::String < LtdTemplate::Code
108
115
  end
109
116
  end
110
117
 
118
+ # String join
119
+ # str.join(list)
120
+ def do_join (opts)
121
+ params = params.positional if params = opts[:parameters]
122
+ if params and params.size > 0
123
+ @template.factory(:string, params.map do |val|
124
+ val.get_value.to_text end.join(@value))
125
+ else
126
+ @template.factory :string, ''
127
+ end
128
+ end
129
+
111
130
  # Range and slice:
112
131
  # str.range([begin[, end]])
113
132
  # str.slice([begin[, length]])
@@ -124,4 +143,70 @@ class LtdTemplate::Value::String < LtdTemplate::Code
124
143
  @template.factory :string, (str || '')
125
144
  end
126
145
 
146
+ # Convert to regexp string
147
+ def do_regexp (opts)
148
+ (@template.limits[:regexp] != false) ? self :
149
+ (LtdTemplate::Value::Regexp.new @template, @value)
150
+ end
151
+
152
+ # Replace and replace one
153
+ # str.replace(pattern, replacement)
154
+ # str.replace1(pattern, replacement)
155
+ def do_replace (opts)
156
+ params = params.positional if params = opts[:parameters]
157
+ if params.size > 1
158
+ pat = params[0].get_value
159
+ pat = pat.respond_to?(:to_regexp) ? pat.to_regexp : pat.to_native
160
+ repl = params[1].get_value.to_native
161
+ if opts[:method][-1] == '1'
162
+ # replace one
163
+ @template.factory :string, @value.sub(pat, repl)
164
+ else
165
+ # replace all
166
+ @template.factory :string, @value.gsub(pat, repl)
167
+ end
168
+ else
169
+ self
170
+ end
171
+ end
172
+
173
+ # Split
174
+ # str.split(pattern[, limit])
175
+ def do_split (opts)
176
+ split_opts = []
177
+ params = params.positional if params = opts[:parameters]
178
+ if params.size > 0
179
+ pattern = params[0].get_value
180
+ split_opts << ((pattern.respond_to? :to_regexp) ?
181
+ pattern.to_regexp : pattern.to_native)
182
+ split_opts << params[1].get_value.to_native if params.size > 1
183
+ end
184
+ @template.factory(:array).set_from_array @value.split(*split_opts)
185
+ end
186
+
187
+ end
188
+
189
+ class LtdTemplate::Value::Regexp < LtdTemplate::Value::String
190
+
191
+ def initialize (template, value)
192
+ super template, value
193
+ @options = 0
194
+ end
195
+
196
+ def to_regexp
197
+ @regexp ||= Regexp.new @value, @options
198
+ end
199
+
200
+ def get_value (opts = {})
201
+ case opts[:method]
202
+ when 'ci', 'ignorecase' then @options |= Regexp::IGNORECASE; self
203
+ when 'ext', 'extended' then @options |= Regexp::EXTENDED; self
204
+ when 'multi', 'multiline' then @options |= Regexp::MULTILINE; self
205
+ when 'type' then @template.factory :string, 'regexp'
206
+ else super
207
+ end
208
+ end
209
+
127
210
  end
211
+
212
+ # END
data/lib/test.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'ltdtemplate'
2
+ t = LtdTemplate.new
3
+
4
+ t.parse '<<"".regexp.type>>'
5
+ print t.render, "\n"
6
+ t.limits[:regexp] = false
7
+ print t.render, "\n"
8
+
9
+ #t.parse '<<"+".join(1,2,"three")>>'
10
+ #t.parse '<<"Now IS the time".split("[^a-z]".regexp.ci).join("/")>>'
data/ltdtemplate.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ltdtemplate"
3
- s.version = "0.2.1"
4
- s.date = "2013-07-28"
3
+ s.version = "0.2.2"
4
+ s.date = "2013-07-29"
5
5
  s.authors = ["Brian Katzung"]
6
6
  s.email = ["briank@kappacs.com"]
7
7
  s.homepage = "http://rubygems.org/gems/ltdtemplate"
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.license = "MIT"
11
11
 
12
12
  s.files = Dir.glob("lib/**/*") +
13
- %w{ltdtemplate.gemspec Gemfile .yardopts HISTORY.txt RESOURCES.txt
13
+ %w{ltdtemplate.gemspec Gemfile .yardopts CHANGELOG RESOURCES
14
14
  TEMPLATE_MANUAL.html}
15
15
  s.test_files = Dir.glob("test/**/[0-9]*.rb")
16
16
  s.require_path = 'lib'
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Katzung
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-07-28 00:00:00 -05:00
17
+ date: 2013-07-29 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -28,6 +28,7 @@ extensions: []
28
28
  extra_rdoc_files: []
29
29
 
30
30
  files:
31
+ - lib/test.rb
31
32
  - lib/ltdtemplate/value/code_block.rb
32
33
  - lib/ltdtemplate/value/array.rb
33
34
  - lib/ltdtemplate/value/nil.rb
@@ -45,8 +46,8 @@ files:
45
46
  - ltdtemplate.gemspec
46
47
  - Gemfile
47
48
  - .yardopts
48
- - HISTORY.txt
49
- - RESOURCES.txt
49
+ - CHANGELOG
50
+ - RESOURCES
50
51
  - TEMPLATE_MANUAL.html
51
52
  - test/00class.rb
52
53
  - test/04number.rb