kwartz 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. data/COPYING +340 -0
  2. data/ChangeLog +103 -0
  3. data/README.txt +37 -0
  4. data/bin/kwartz +12 -0
  5. data/doc-api/classes/Kwartz.html +218 -0
  6. data/doc-api/classes/Kwartz/Assertion.html +140 -0
  7. data/doc-api/classes/Kwartz/AssertionError.html +148 -0
  8. data/doc-api/classes/Kwartz/AttrInfo.html +320 -0
  9. data/doc-api/classes/Kwartz/BaseError.html +206 -0
  10. data/doc-api/classes/Kwartz/BaseTranslator.html +331 -0
  11. data/doc-api/classes/Kwartz/CharacterType.html +212 -0
  12. data/doc-api/classes/Kwartz/CommandOptionError.html +154 -0
  13. data/doc-api/classes/Kwartz/CommandOptions.html +374 -0
  14. data/doc-api/classes/Kwartz/Config.html +150 -0
  15. data/doc-api/classes/Kwartz/ConvertError.html +191 -0
  16. data/doc-api/classes/Kwartz/Converter.html +252 -0
  17. data/doc-api/classes/Kwartz/CssStyleParser.html +483 -0
  18. data/doc-api/classes/Kwartz/DocumentRuleset.html +369 -0
  19. data/doc-api/classes/Kwartz/ElementExpander.html +325 -0
  20. data/doc-api/classes/Kwartz/ElementInfo.html +312 -0
  21. data/doc-api/classes/Kwartz/ElementRuleset.html +582 -0
  22. data/doc-api/classes/Kwartz/EperlHandler.html +338 -0
  23. data/doc-api/classes/Kwartz/EperlTranslator.html +167 -0
  24. data/doc-api/classes/Kwartz/ErubisHandler.html +113 -0
  25. data/doc-api/classes/Kwartz/ErubisTranslator.html +168 -0
  26. data/doc-api/classes/Kwartz/ErubyHandler.html +337 -0
  27. data/doc-api/classes/Kwartz/ErubyTranslator.html +167 -0
  28. data/doc-api/classes/Kwartz/ExpandStatement.html +227 -0
  29. data/doc-api/classes/Kwartz/Expression.html +119 -0
  30. data/doc-api/classes/Kwartz/Handler.html +558 -0
  31. data/doc-api/classes/Kwartz/JstlHandler.html +657 -0
  32. data/doc-api/classes/Kwartz/JstlTranslator.html +226 -0
  33. data/doc-api/classes/Kwartz/KwartzError.html +146 -0
  34. data/doc-api/classes/Kwartz/Main.html +384 -0
  35. data/doc-api/classes/Kwartz/NativeExpression.html +236 -0
  36. data/doc-api/classes/Kwartz/NativeStatement.html +254 -0
  37. data/doc-api/classes/Kwartz/Node.html +156 -0
  38. data/doc-api/classes/Kwartz/ParseError.html +148 -0
  39. data/doc-api/classes/Kwartz/PhpHandler.html +333 -0
  40. data/doc-api/classes/Kwartz/PhpTranslator.html +194 -0
  41. data/doc-api/classes/Kwartz/PresentationLogicParser.html +830 -0
  42. data/doc-api/classes/Kwartz/PrintStatement.html +221 -0
  43. data/doc-api/classes/Kwartz/RailsHandler.html +587 -0
  44. data/doc-api/classes/Kwartz/RailsTranslator.html +167 -0
  45. data/doc-api/classes/Kwartz/RubyStyleParser.html +558 -0
  46. data/doc-api/classes/Kwartz/Ruleset.html +117 -0
  47. data/doc-api/classes/Kwartz/Statement.html +119 -0
  48. data/doc-api/classes/Kwartz/StrutsTranslator.html +190 -0
  49. data/doc-api/classes/Kwartz/TagInfo.html +314 -0
  50. data/doc-api/classes/Kwartz/TextConverter.html +270 -0
  51. data/doc-api/classes/Kwartz/Translator.html +318 -0
  52. data/doc-api/classes/Test.html +107 -0
  53. data/doc-api/classes/Test/Unit.html +101 -0
  54. data/doc-api/created.rid +1 -0
  55. data/doc-api/files/__/README_txt.html +150 -0
  56. data/doc-api/files/kwartz/assert_rb.html +114 -0
  57. data/doc-api/files/kwartz/binding/eperl_rb.html +116 -0
  58. data/doc-api/files/kwartz/binding/erubis_rb.html +116 -0
  59. data/doc-api/files/kwartz/binding/eruby_rb.html +115 -0
  60. data/doc-api/files/kwartz/binding/jstl_rb.html +116 -0
  61. data/doc-api/files/kwartz/binding/php_rb.html +116 -0
  62. data/doc-api/files/kwartz/binding/rails_rb.html +115 -0
  63. data/doc-api/files/kwartz/binding/struts_rb.html +117 -0
  64. data/doc-api/files/kwartz/config_rb.html +107 -0
  65. data/doc-api/files/kwartz/converter_rb.html +119 -0
  66. data/doc-api/files/kwartz/error_rb.html +107 -0
  67. data/doc-api/files/kwartz/main_rb.html +124 -0
  68. data/doc-api/files/kwartz/node_rb.html +114 -0
  69. data/doc-api/files/kwartz/parser_rb.html +117 -0
  70. data/doc-api/files/kwartz/translator_rb.html +115 -0
  71. data/doc-api/files/kwartz/util/assert-text-equal_rb.html +115 -0
  72. data/doc-api/files/kwartz/util/testcase-helper_rb.html +115 -0
  73. data/doc-api/files/kwartz_rb.html +120 -0
  74. data/doc-api/fr_class_index.html +75 -0
  75. data/doc-api/fr_file_index.html +45 -0
  76. data/doc-api/fr_method_index.html +216 -0
  77. data/doc-api/index.html +24 -0
  78. data/doc-api/rdoc-style.css +208 -0
  79. data/doc/docstyle.css +188 -0
  80. data/doc/p-pattern.html +1207 -0
  81. data/doc/reference.html +3396 -0
  82. data/doc/users-guide.html +1670 -0
  83. data/examples/breadcrumbs1/Makefile +15 -0
  84. data/examples/breadcrumbs1/breadcrumbs.eruby.plogic +27 -0
  85. data/examples/breadcrumbs1/breadcrumbs.html +12 -0
  86. data/examples/breadcrumbs1/breadcrumbs.jstl.plogic +28 -0
  87. data/examples/breadcrumbs1/breadcrumbs.php.plogic +26 -0
  88. data/examples/breadcrumbs1/main.php +12 -0
  89. data/examples/breadcrumbs1/main.rb +12 -0
  90. data/examples/breadcrumbs2/Makefile +15 -0
  91. data/examples/breadcrumbs2/breadcrumbs.eruby.plogic +22 -0
  92. data/examples/breadcrumbs2/breadcrumbs.html +14 -0
  93. data/examples/breadcrumbs2/breadcrumbs.jstl.plogic +24 -0
  94. data/examples/breadcrumbs2/breadcrumbs.php.plogic +23 -0
  95. data/examples/breadcrumbs2/main.php +12 -0
  96. data/examples/breadcrumbs2/main.rb +12 -0
  97. data/examples/pagelayout/Makefile +47 -0
  98. data/examples/pagelayout/content.eruby.plogic +44 -0
  99. data/examples/pagelayout/content.jstl.plogic +36 -0
  100. data/examples/pagelayout/content.php.plogic +37 -0
  101. data/examples/pagelayout/content1.html +36 -0
  102. data/examples/pagelayout/content2.html +29 -0
  103. data/examples/pagelayout/design.css +40 -0
  104. data/examples/pagelayout/layout.html +50 -0
  105. data/examples/pagelayout/main.php +55 -0
  106. data/examples/pagelayout/main.rb +77 -0
  107. data/examples/pagelayout/menu.eruby.plogic +14 -0
  108. data/examples/pagelayout/menu.html +13 -0
  109. data/examples/pagelayout/menu.jstl.plogic +14 -0
  110. data/examples/pagelayout/menu.php.plogic +14 -0
  111. data/examples/rails1/Makefile +36 -0
  112. data/examples/rails1/README +19 -0
  113. data/examples/rails1/application_helper.rb +31 -0
  114. data/examples/rails1/edit.html +28 -0
  115. data/examples/rails1/edit.plogic +10 -0
  116. data/examples/rails1/form.html +52 -0
  117. data/examples/rails1/form.plogic +33 -0
  118. data/examples/rails1/layout.plogic +15 -0
  119. data/examples/rails1/link_to.plogic +19 -0
  120. data/examples/rails1/list.html +48 -0
  121. data/examples/rails1/list.plogic +28 -0
  122. data/examples/rails1/new.html +27 -0
  123. data/examples/rails1/new.plogic +10 -0
  124. data/examples/rails1/reader.plogic +29 -0
  125. data/examples/rails1/show.html +40 -0
  126. data/examples/rails1/show.plogic +4 -0
  127. data/examples/rails1/writer.plogic +8 -0
  128. data/examples/table1/Makefile +15 -0
  129. data/examples/table1/main.php +11 -0
  130. data/examples/table1/main.rb +11 -0
  131. data/examples/table1/table1.eruby.plogic +21 -0
  132. data/examples/table1/table1.html +16 -0
  133. data/examples/table1/table1.jstl.plogic +21 -0
  134. data/examples/table1/table1.php.plogic +22 -0
  135. data/kwartz.gemspec +55 -0
  136. data/lib/kwartz.rb +13 -0
  137. data/lib/kwartz/assert.rb +31 -0
  138. data/lib/kwartz/binding/eperl.rb +166 -0
  139. data/lib/kwartz/binding/erubis.rb +61 -0
  140. data/lib/kwartz/binding/eruby.rb +164 -0
  141. data/lib/kwartz/binding/jstl.rb +334 -0
  142. data/lib/kwartz/binding/php.rb +167 -0
  143. data/lib/kwartz/binding/rails.rb +295 -0
  144. data/lib/kwartz/binding/struts.rb +109 -0
  145. data/lib/kwartz/config.rb +28 -0
  146. data/lib/kwartz/converter.rb +920 -0
  147. data/lib/kwartz/error.rb +41 -0
  148. data/lib/kwartz/main.rb +464 -0
  149. data/lib/kwartz/node.rb +454 -0
  150. data/lib/kwartz/parser.rb +903 -0
  151. data/lib/kwartz/translator.rb +153 -0
  152. data/lib/kwartz/util/assert-text-equal.rb +44 -0
  153. data/lib/kwartz/util/testcase-helper.rb +112 -0
  154. data/setup.rb +1331 -0
  155. data/test/test-compile.rb +36 -0
  156. data/test/test-compile.yaml +178 -0
  157. data/test/test-converter.rb +34 -0
  158. data/test/test-converter.yaml +127 -0
  159. data/test/test-directives.rb +32 -0
  160. data/test/test-directives.yaml +1411 -0
  161. data/test/test-main.rb +464 -0
  162. data/test/test-parser.rb +54 -0
  163. data/test/test-parser.yaml +394 -0
  164. data/test/test-rails.rb +28 -0
  165. data/test/test-rails.yaml +301 -0
  166. data/test/test-ruleset.rb +36 -0
  167. data/test/test-ruleset.yaml +804 -0
  168. data/test/test.rb +44 -0
  169. metadata +236 -0
@@ -0,0 +1,41 @@
1
+ ###
2
+ ### $Rev: 112 $
3
+ ### $Release: 3.0.0 $
4
+ ### copyright(c) 2004-2006 kuwata-lab.com all rights reserved
5
+ ###
6
+
7
+ module Kwartz
8
+
9
+
10
+ class KwartzError < StandardError
11
+ def initialize(message)
12
+ super(message)
13
+ end
14
+ end
15
+
16
+
17
+ class BaseError < KwartzError
18
+
19
+ def initialize(message, filename, linenum, column=nil)
20
+ super(message)
21
+ @filename = filename || '-'
22
+ @linenum = linenum
23
+ @column = column
24
+ end
25
+
26
+ attr_accessor :filename, :linenum, :column
27
+
28
+ def to_s
29
+ return super unless @linenum
30
+ #return "line #{@linenum}, column #{@column}: " + super
31
+ if @column
32
+ return "#{@filename}:#{@linenum}:#{@column}: " + super
33
+ else
34
+ return "#{@filename}:#{@linenum}: " + super
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+
41
+ end
@@ -0,0 +1,464 @@
1
+ ###
2
+ ### $Rev: 113 $
3
+ ### $Release: 3.0.0 $
4
+ ### copyright(c) 2004-2006 kuwata-lab.com all rights reserved
5
+ ###
6
+
7
+
8
+ require 'kwartz'
9
+ require 'kwartz/binding/eruby'
10
+ require 'kwartz/binding/php'
11
+ require 'kwartz/binding/eperl'
12
+ require 'kwartz/binding/rails'
13
+ require 'kwartz/binding/jstl'
14
+ require 'kwartz/binding/struts'
15
+ require 'kwartz/binding/erubis'
16
+
17
+
18
+
19
+ module Kwartz
20
+
21
+
22
+ ##
23
+ ## command option error class
24
+ ##
25
+ class CommandOptionError < KwartzError
26
+
27
+
28
+ def initialize(message)
29
+ super(message)
30
+ end
31
+
32
+
33
+ end
34
+
35
+
36
+ ##
37
+ ## command option class
38
+ ##
39
+ ## ex.
40
+ ## option_table = [
41
+ ## [?h, :help, nil],
42
+ ## [?v, :version, nil],
43
+ ## [?f, :file, "filename"],
44
+ ## ]
45
+ ## properties = {}
46
+ ## options = CommandOption.new(option_table, properties)
47
+ ## filenames = options.parse_argv(ARGV)
48
+ ## p options.help
49
+ ## p options.version
50
+ ## p options.file
51
+ ##
52
+ class CommandOptions
53
+
54
+ def initialize(option_table, properties={})
55
+ @_option_table = option_table
56
+ @_properties = properties
57
+ buf = []
58
+ optchars = {}
59
+ option_table.each do |char, key, argname|
60
+ buf << "def #{key}; @#{key}; end; def #{key}=(val); @#{key}=val; end\n"
61
+ end
62
+ instance_eval buf.join
63
+ end
64
+
65
+ def _find_entry(key)
66
+ if key.is_a?(Fixnum)
67
+ return @_option_table.find { |row| row[0] == key }
68
+ else
69
+ key = key.to_s.intern unless key.is_a?(Symbol)
70
+ return @_option_table.find { |row| row[1] == key }
71
+ end
72
+ end
73
+ private :_find_entry
74
+
75
+ def [](key)
76
+ if key.is_a?(Fixnum)
77
+ entry = _find_entry(key) or return
78
+ key = entry[1]
79
+ end
80
+ instance_variable_get("@#{key}")
81
+ end
82
+
83
+ def []=(key, val)
84
+ instance_variable_set("@#{key}", val)
85
+ end
86
+
87
+ def key?(key)
88
+ return instance_variables.include?("@#{key}")
89
+ end
90
+
91
+ def char(key)
92
+ entry = _find_entry(key)
93
+ return entry && entry[0]
94
+ end
95
+
96
+ def chr(key)
97
+ ch = char(key)
98
+ return ch ? ch.chr : ''
99
+ end
100
+
101
+ def parse_argv(argv)
102
+ properties = @_properties
103
+ while !argv.empty? && argv[0][0] == ?-
104
+ optstr = argv.shift
105
+ optstr = optstr[1, optstr.length - 1]
106
+ if optstr[0] == ?- # properties
107
+ unless optstr =~ /\A-([-\w]+)(?:=(.*))?/
108
+ raise option_error("'-#{optstr}': invalid property pattern.")
109
+ end
110
+ pname = $1 ; pvalue = $2
111
+ case pvalue
112
+ when nil ; pvalue = true
113
+ when /\A\d+\z/ ; pvalue = pvalue.to_i
114
+ when /\A\d+\.\d+\z/ ; pvalue = pvalue.to_f
115
+ when 'true', 'yes', 'on' ; pvalue = true
116
+ when 'false', 'no', 'off' ; pvalue = false
117
+ when 'nil', 'null' ; pvalue = nil
118
+ when /\A'.*'\z/, /\A".*"\z/ ; pvalue = eval pvalue
119
+ end
120
+ properties[pname.intern] = pvalue
121
+ else # command-line options
122
+ while optstr && !optstr.empty?
123
+ optchar = optstr[0]
124
+ optstr = optstr[1, optstr.length - 1]
125
+ entry = _find_entry(optchar)
126
+ entry or raise CommandOptionError.new("-#{optchar.chr}: unknown option.")
127
+ char, key, argname = entry
128
+ case argname
129
+ when nil, false
130
+ instance_variable_set("@#{key}", true)
131
+ when String
132
+ arg = optstr
133
+ arg = argv.shift unless arg && !arg.empty?
134
+ arg or raise CommandOptionError.new("-#{optchar.chr}: #{argname} required.")
135
+ instance_variable_set("@#{key}", arg)
136
+ optstr = ''
137
+ when true
138
+ arg = optstr
139
+ arg = true unless arg && !arg.empty?
140
+ instance_variable_set("@#{key}", arg)
141
+ optstr = ''
142
+ else
143
+ raise "** internal error **"
144
+ end
145
+ end #while
146
+ end #if
147
+ end #while
148
+ filenames = argv
149
+ return filenames
150
+ end #def
151
+
152
+ end #class
153
+
154
+
155
+ ##
156
+ ## main command
157
+ ##
158
+ ## ex.
159
+ ## Kwartz::Main.add_handler('mylang', MyLangDirectiveHandler, MyLangTranslator)
160
+ ## Kwartz::Main.main(ARGV)
161
+ ##
162
+ class Main
163
+
164
+
165
+ def initialize(argv=ARGV)
166
+ @argv = argv
167
+ @command = File.basename($0)
168
+ end
169
+
170
+
171
+ def self.main(argv=ARGV)
172
+ status = 0
173
+ begin
174
+ main = Kwartz::Main.new(argv)
175
+ out = main.execute()
176
+ print out unless out == nil
177
+ rescue Kwartz::KwartzError => ex
178
+ raise ex if $DEBUG
179
+ $stderr.puts ex.to_s
180
+ status = 1
181
+ end
182
+ exit status
183
+ end
184
+
185
+
186
+ @@option_table = [
187
+ [ ?h, :help, nil ],
188
+ [ ?v, :version, nil ],
189
+ [ ?e, :escape, nil ],
190
+ [ ?D, :debug, nil ],
191
+ [ ?t, :untabify, nil ],
192
+ [ ?S, :intern, nil ],
193
+ [ ?l, :lang, 'lang name' ],
194
+ [ ?k, :kanji, 'kanji code' ],
195
+ [ ?r, :requires, 'library name' ],
196
+ [ ?p, :plogics, 'file name' ],
197
+ [ ?P, :pstyle, 'parser style' ],
198
+ [ ?x, :extract_cont, 'element id' ],
199
+ [ ?X, :extract_elem, 'element id' ],
200
+ [ ?i, :imports, 'file name' ],
201
+ [ ?L, :layout, 'file name' ],
202
+ [ ?f, :yamlfile, 'yaml file' ],
203
+ ]
204
+
205
+
206
+ def execute(argv=@argv)
207
+
208
+ ## parse command-line options
209
+ options = CommandOptions.new(@@option_table, properties = {})
210
+ pdata_filenames = options.parse_argv(argv)
211
+ options.help = true if properties[:help]
212
+
213
+ ## help
214
+ if options.help || options.version
215
+ puts version() if options.version
216
+ puts help() if options.help
217
+ return nil
218
+ end
219
+
220
+ ## check filenames
221
+ if pdata_filenames.empty?
222
+ raise option_error("filename of presentation data is required.")
223
+ end
224
+ pdata_filenames.each do |filename|
225
+ test(?f, filename) or raise option_error("#{filename}: file not found.")
226
+ end
227
+
228
+ ## options
229
+ $KCODE = options.kanji if options.kanji
230
+ $DEBUG = options.debug if options.debug
231
+
232
+ ## parse class, hander class, translator class
233
+ style = options.pstyle || 'css'
234
+ unless parser_class = PresentationLogicParser.get_class(style)
235
+ s = "-#{options.chr(:pstyle)} #{style}"
236
+ raise option_error("#{s}: unknown style name (parser class not registered).")
237
+ end
238
+ lang = options.lang || Config::PROPERTY_LANG # 'eruby'
239
+ unless handler_class = Handler.get_class(lang)
240
+ s = "-#{options.chr(:lang)} #{lang}"
241
+ raise option_error("#{s}: unknown lang name (handler class not registered).")
242
+ end
243
+ unless translator_class = Translator.get_class(lang)
244
+ s = "-#{options.chr(:lang)} #{lang}"
245
+ raise option_error("#{s}: unknown lang name (translator class not registered).")
246
+ end
247
+
248
+ ## require libraries
249
+ if options.requires
250
+ libraries = options.requires
251
+ libraries.split(/,/).each do |library|
252
+ library.strip!
253
+ require library
254
+ end
255
+ end
256
+
257
+ ## parse presentation logic file
258
+ ruleset_list = []
259
+ if options.plogics
260
+ parser = parser_class.new(properties)
261
+ options.plogics.split(/,/).each do |filename|
262
+ filename.strip!
263
+ if test(?f, filename)
264
+ # ok
265
+ elsif test(?f, filename + '.plogic')
266
+ filename += '.plogic'
267
+ else
268
+ s = "-#{options.chr(:plogics)} #{filename}[.plogic]"
269
+ raise option_error("#{s}: file not found.")
270
+ end
271
+ plogic = File.read(filename)
272
+ ruleset_list += parser.parse(plogic, filename)
273
+ end
274
+ end
275
+
276
+ ## properties
277
+ properties[:escape] = true if options.escape && !properties.key?(:escape)
278
+
279
+ ## create converter
280
+ handler = handler_class.new(ruleset_list, properties)
281
+ converter = TextConverter.new(handler, properties)
282
+
283
+ ## import-files and layout-file
284
+ import_filenames = []
285
+ if options[?i]
286
+ (import_filenames = options.imports.split(/,/)).each do |filename|
287
+ unless test(?f, filename)
288
+ s = "-#{options.chr(:imports)}"
289
+ raise option_error("#{s} #{filename}: file not found.")
290
+ end
291
+ end
292
+ end
293
+ if options.layout
294
+ unless test(?f, options.layout)
295
+ s = "-#{options.chr(:layout)}"
296
+ raise option_error("#{s} #{options.layout}: file not found.")
297
+ end
298
+ import_filenames += pdata_filenames
299
+ pdata_filenames = [options.layout]
300
+ end
301
+ import_filenames.each do |filename|
302
+ pdata = File.read(filename)
303
+ converter.convert(pdata, filename)
304
+ end
305
+
306
+ ## convert presentation data file
307
+ stmt_list = []
308
+ pdata = nil
309
+ pdata_filenames.each do |filename|
310
+ test(?f, filename) or raise option_error("#{filename}: file not found.")
311
+ pdata = File.read(filename)
312
+ #handler = handler_class.new(ruleset_list)
313
+ #converter = TextConverter.new(handler, properties)
314
+ list = converter.convert(pdata, filename)
315
+ stmt_list.concat(list)
316
+ end
317
+
318
+ ## extract element or content
319
+ elem_id = options.extract_cont || options.extract_elem
320
+ if elem_id
321
+ content_only = options.extract_cont ? true : false
322
+ stmt_list = handler.extract(elem_id, content_only)
323
+ end
324
+
325
+ ## translate statements into target code(eRuby, PHP, JSP)
326
+ if pdata[pdata.index(?\n) - 1] == ?\r
327
+ properties[:nl] ||= "\r\n"
328
+ end
329
+ translator = translator_class.new(properties)
330
+ output = translator.translate(stmt_list)
331
+
332
+ ## load YAML file and evaluate eRuby script
333
+ if options.yamlfile
334
+ eruby_script = output
335
+ if lang == 'eruby' || lang == 'rails'
336
+ require 'erb'
337
+ trim_mode = properties.key?(:trim) ? properties[:trim] : 1
338
+ src = ERB.new(eruby_script, $SAFE, trim_mode).src
339
+ mod = ERB::Util
340
+ elsif lang == 'erubis'
341
+ require 'erubis'
342
+ src = Erubis::Eruby.new(eruby_script).src
343
+ mod = Erubis::XmlHelper
344
+ else
345
+ s1 = "-#{options.chr(:yamlfile)}"
346
+ s2 = "-#{options.chr(:lang)} #{lang}"
347
+ option_error("#{s1}: not available with '#{s2}'.")
348
+ end
349
+ unless test(?f, options.yamlfile)
350
+ s = "-#{options.chr(:yamlfile)} #{options.yamlfile}"
351
+ raise option_error("#{s}: file not found.")
352
+ end
353
+ str = File.read(options.yamlfile)
354
+ str = untabify(str) if options.untabify
355
+ require 'yaml'
356
+ ydoc = YAML.load(str)
357
+ unless ydoc.is_a?(Hash)
358
+ s = "-#{options.chr(:yamlfile)} #{options.yamlfile}"
359
+ raise option_error("#{s}: not a mapping.")
360
+ end
361
+ intern_hash_keys(ydoc) if options.intern
362
+ context = Object.new
363
+ ydoc.each do |key, val|
364
+ context.instance_variable_set("@#{key}", val)
365
+ end
366
+ context.extend(mod) # ERB::Util or Erubis::XmlHelper
367
+ output = context.instance_eval(src)
368
+ end
369
+
370
+ return output
371
+
372
+ end
373
+
374
+
375
+ private
376
+
377
+
378
+ def help
379
+ sb = []
380
+ sb << "kwartz - a template system which realized 'Independence of Presentation Logic'\n"
381
+ sb << "Usage: #{@command} [..options..] [-p plogic] file.html [file2.html ...]\n"
382
+ sb << " -h : help\n"
383
+ sb << " -v : version\n"
384
+ #sb << " -D : debug mode\n"
385
+ sb << " -e : alias of '--escape=true'\n"
386
+ sb << " -l lang : eruby/php/eperl/rails/jstl (default 'eruby')\n"
387
+ sb << " -k kanji : euc/sjis/utf8 (default nil)\n"
388
+ sb << " -r library,... : require libraries\n"
389
+ sb << " -p plogic,... : presentation logic files\n"
390
+ sb << " -i pdata,... : import presentation data files\n"
391
+ sb << " -L layoutfile : layout file ('-L f1 f2' is equivalent to '-i f2 f1')\n"
392
+ sb << " -x elem-id : extract content of element marked by elem-id\n"
393
+ sb << " -X elem-id : extract element marked by elem-id\n"
394
+ sb << " -f yamlfile : YAML file for context values\n"
395
+ sb << " -t : expand tab character in YAML file\n"
396
+ sb << " -S : convert mapping key from string to symbol in YAML file\n"
397
+ #sb << " -P style : style of presentation logic (css/ruby/yaml)\n"
398
+ sb << " --dattr=str : directive attribute name\n"
399
+ sb << " --odd=value : odd value for FOREACH/LOOP directive (default \"'odd'\")\n"
400
+ sb << " --even=value : even value for FOREACH/LOOP directive (default \"'even'\")\n"
401
+ sb << " --header=str : header text\n"
402
+ sb << " --footer=str : footer text\n"
403
+ sb << " --delspan={true|false} : delete dummy span tag (default false)\n"
404
+ sb << " --escape={true|false} : escape (sanitize) (default false)\n"
405
+ sb << " --jstl={1.2|1.1} : JSTL version (default 1.2)\n"
406
+ sb << " --charset=charset : character set for JSTL (default none)\n"
407
+ return sb.join
408
+ end
409
+
410
+
411
+ def version
412
+ v = ('$Release: 3.0.0 $' =~ /[.\d]+/) && $&
413
+ return v
414
+ end
415
+
416
+
417
+ def option_error(message)
418
+ return CommandOptionError.new(message)
419
+ end
420
+
421
+
422
+ def untabify(str, width=8)
423
+ sb = ''
424
+ str.scan(/(.*?)\t/m) do |s, |
425
+ len = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length
426
+ sb << s << (" " * (width - len % width))
427
+ end
428
+ return $' ? (sb << $') : str
429
+ end
430
+
431
+
432
+ def intern_hash_keys(obj, done={})
433
+ return if done.key?(obj.__id__)
434
+ case obj
435
+ when Hash
436
+ done[obj.__id__] = obj
437
+ obj.keys.each do |key|
438
+ obj[key.intern] = obj.delete(key) if key.is_a?(String)
439
+ end
440
+ obj.values.each do |val|
441
+ intern_hash_keys(val, done) if val.is_a?(Hash) || val.is_a?(Array)
442
+ end
443
+ when Array
444
+ done[obj.__id__] = obj
445
+ obj.each do |val|
446
+ intern_hash_keys(val, done) if val.is_a?(Hash) || val.is_a?(Array)
447
+ end
448
+ end
449
+ end
450
+
451
+
452
+ end #class
453
+
454
+
455
+
456
+ end #module
457
+
458
+
459
+
460
+ if $0 == __FILE__
461
+
462
+ Kwartz::Main.main(ARGV)
463
+
464
+ end