22#
33# Mplex: Extended Metaprogramming Library
44#
5- # Copyright (c) 2009 FURUHASHI Sadayuki
5+ # Copyright (c) 2009-2010 FURUHASHI Sadayuki
66#
77# Permission is hereby granted, free of charge, to any person obtaining a copy
88# of this software and associated documentation files (the "Software"), to deal
@@ -43,16 +43,28 @@ class Mplex
4343 }
4444 end
4545
46+ def self . file ( fname , context = nil )
47+ self . result ( File . read ( fname ) , context , fname )
48+ end
49+
50+ def self . write ( fname , outfname , context = nil )
51+ out = self . result ( File . read ( fname ) , context , fname )
52+ File . open ( outfname , "w" ) { |f | f . write ( out ) }
53+ out
54+ end
55+
4656 def self . script ( src )
4757 compile ( src )
4858 end
4959
5060 private
5161 def self . with_context ( context , output , &block )
5262 context ||= Object . new
63+ save = context . instance_variable_get ( :@_mplexout )
5364 context . instance_variable_set ( :@_mplexout , output )
5465 block . call ( context )
55- context . instance_eval { remove_instance_variable ( :@_mplexout ) }
66+ context . instance_variable_set ( :@_mplexout , save )
67+ output
5668 end
5769
5870 def self . compile ( src )
@@ -73,7 +85,8 @@ class Mplex
7385 c , q = t . split ( /[ \t ]*\% \> / , 2 )
7486 t = "[%:(%]#{ c +"\n " } [%:)#{ q . strip } %]" if q
7587
76- t . split ( /\[ \% (\: ?.*?)\% \] /m ) . each_with_index { |m , i |
88+ s = t . split ( /(?:\[ \% (\: ?.*?)\% \] |\{ \{ (\: ?.*?\} *)\} \} )/m )
89+ s . each_with_index { |m , i |
7790 ( o << "#{ m [ 1 ..-1 ] } ;" ; next ) if m [ 0 ] == ?: && i % 2 == 1
7891 ( o << "@_mplexout.concat((#{ m } ).to_s);" ; next ) if i % 2 == 1
7992 o << "@_mplexout.concat(#{ m . dump } );" unless m . empty?
89102#
90103# Mplex: Extended Metaprogramming Library
91104#
92- # Copyright (c) 2009 FURUHASHI Sadayuki
105+ # Copyright (c) 2009-2010 FURUHASHI Sadayuki
93106#
94107# Permission is hereby granted, free of charge, to any person obtaining a copy
95108# of this software and associated documentation files (the "Software"), to deal
@@ -141,7 +154,9 @@ begin
141154
142155 finputs = ARGV
143156
144- raise "multiple stdin" if ( finputs + [ fctx ] ) . grep ( "-" ) . size >= 2
157+ if ( finputs + [ fctx ] ) . select { |x | x == "-" } . size >= 2
158+ raise "multiple stdin"
159+ end
145160
146161rescue
147162 usage
0 commit comments