Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIshan Bansal <ishan@seeta.in>2011-02-04 12:53:17 (GMT)
committer Ishan Bansal <ishan@seeta.in>2011-02-04 12:53:17 (GMT)
commitee852209b70c803e6ab1bb69b71c7dafcb1448e7 (patch)
treeac20673816893b398aed4c4da463b157d9e4899e
Imported Upstream version 1HEADmaster
-rw-r--r--CHANGELOG.txt29
-rw-r--r--COPYING.txt30
-rw-r--r--README.txt76
-rw-r--r--VERSION.txt1
-rw-r--r--activity/activity-hackety.svg870
-rw-r--r--activity/activity.info7
-rw-r--r--app/.gitignore4
-rw-r--r--app/LICENSE19
-rw-r--r--app/README.textile94
-rw-r--r--app/app.yaml13
-rw-r--r--app/app/boot.rb32
-rw-r--r--app/app/db/connection_pool.rb65
-rw-r--r--app/app/db/core_ext.rb9
-rw-r--r--app/app/db/database.rb119
-rw-r--r--app/app/db/dataset.rb354
-rw-r--r--app/app/db/http.rb78
-rw-r--r--app/app/db/model.rb235
-rw-r--r--app/app/db/schema.rb161
-rw-r--r--app/app/db/sequel.rb21
-rw-r--r--app/app/db/sqlite.rb112
-rw-r--r--app/app/db/table.rb231
-rw-r--r--app/app/syntax/common.rb197
-rw-r--r--app/app/syntax/convertors/abstract.rb27
-rw-r--r--app/app/syntax/convertors/html.rb51
-rw-r--r--app/app/syntax/lang/ruby.rb317
-rw-r--r--app/app/syntax/lang/xml.rb108
-rw-r--r--app/app/syntax/lang/yaml.rb105
-rw-r--r--app/app/syntax/markup.rb214
-rw-r--r--app/app/syntax/version.rb11
-rw-r--r--app/app/ui/completion.rb183
-rw-r--r--app/app/ui/editor/editor.rb448
-rw-r--r--app/app/ui/lessons.rb319
-rwxr-xr-xapp/app/ui/mainwindow.rb130
-rw-r--r--app/app/ui/tabs/editor.rb2
-rw-r--r--app/app/ui/tabs/home.rb175
-rw-r--r--app/app/ui/tabs/lessons.rb32
-rw-r--r--app/app/ui/tabs/prefs.rb68
-rw-r--r--app/app/ui/tabs/sidetabs.rb185
-rw-r--r--app/app/ui/widgets.rb296
-rwxr-xr-xapp/fonts/Animals.ttfbin0 -> 73516 bytes
-rwxr-xr-xapp/fonts/Arcade.ttfbin0 -> 57860 bytes
-rwxr-xr-xapp/fonts/Bruegheliana.ttfbin0 -> 45648 bytes
-rwxr-xr-xapp/fonts/Carr Space.ttfbin0 -> 25472 bytes
-rwxr-xr-xapp/fonts/Chess Utrecht.ttfbin0 -> 14396 bytes
-rwxr-xr-xapp/fonts/DayRoman-X.ttfbin0 -> 30088 bytes
-rwxr-xr-xapp/fonts/DayRoman.ttfbin0 -> 83160 bytes
-rwxr-xr-xapp/fonts/Delicious-Bold.otfbin0 -> 24648 bytes
-rwxr-xr-xapp/fonts/Delicious-BoldItalic.otfbin0 -> 25424 bytes
-rwxr-xr-xapp/fonts/Delicious-Heavy.otfbin0 -> 25264 bytes
-rwxr-xr-xapp/fonts/Delicious-Italic.otfbin0 -> 25036 bytes
-rwxr-xr-xapp/fonts/Delicious-Roman.otfbin0 -> 24700 bytes
-rwxr-xr-xapp/fonts/Delicious-SmallCaps.otfbin0 -> 25532 bytes
-rwxr-xr-xapp/fonts/Even More Dings JL.ttfbin0 -> 98608 bytes
-rwxr-xr-xapp/fonts/Fontalicious.ttfbin0 -> 62452 bytes
-rwxr-xr-xapp/fonts/Fontin-Bold.otfbin0 -> 30460 bytes
-rwxr-xr-xapp/fonts/Fontin-Italic.otfbin0 -> 30636 bytes
-rwxr-xr-xapp/fonts/Fontin-Regular.otfbin0 -> 30396 bytes
-rwxr-xr-xapp/fonts/Fontin-SmallCaps.otfbin0 -> 29308 bytes
-rwxr-xr-xapp/fonts/Free.ttfbin0 -> 91148 bytes
-rwxr-xr-xapp/fonts/Illustries.ttfbin0 -> 58816 bytes
-rwxr-xr-xapp/fonts/JustOldFashion.ttfbin0 -> 79480 bytes
-rw-r--r--app/fonts/Lacuna.ttfbin0 -> 56784 bytes
-rwxr-xr-xapp/fonts/LiberationMono-Bold.ttfbin0 -> 104980 bytes
-rw-r--r--app/fonts/LiberationMono-Regular.ttfbin0 -> 107920 bytes
-rwxr-xr-xapp/fonts/LiberationSans-Bold.ttfbin0 -> 133000 bytes
-rwxr-xr-xapp/fonts/LiberationSans-BoldItalic.ttfbin0 -> 128828 bytes
-rwxr-xr-xapp/fonts/LiberationSans-Italic.ttfbin0 -> 155304 bytes
-rwxr-xr-xapp/fonts/LiberationSans-Regular.ttfbin0 -> 133088 bytes
-rwxr-xr-xapp/fonts/LiberationSerif-Bold.ttfbin0 -> 141132 bytes
-rwxr-xr-xapp/fonts/LiberationSerif-BoldItalic.ttfbin0 -> 144184 bytes
-rwxr-xr-xapp/fonts/LiberationSerif-Italic.ttfbin0 -> 138328 bytes
-rwxr-xr-xapp/fonts/LiberationSerif-Regular.ttfbin0 -> 146036 bytes
-rwxr-xr-xapp/fonts/Outer Space JL.ttfbin0 -> 67752 bytes
-rwxr-xr-xapp/fonts/Oxygene1.ttfbin0 -> 23096 bytes
-rwxr-xr-xapp/fonts/Phonetica.ttfbin0 -> 20232 bytes
-rw-r--r--app/fonts/Pixelpoiiz.ttfbin0 -> 27028 bytes
-rwxr-xr-xapp/fonts/Playing Cards.ttfbin0 -> 42064 bytes
-rwxr-xr-xapp/fonts/Silhouette.ttfbin0 -> 35164 bytes
-rw-r--r--app/fonts/TakaoGothic.otfbin0 -> 3615184 bytes
-rwxr-xr-xapp/fonts/YanoneKaffeesatz-Bold.otfbin0 -> 55568 bytes
-rwxr-xr-xapp/fonts/YanoneKaffeesatz-Light.otfbin0 -> 58328 bytes
-rwxr-xr-xapp/fonts/YanoneKaffeesatz-Regular.otfbin0 -> 58528 bytes
-rwxr-xr-xapp/fonts/YanoneKaffeesatz-Thin.otfbin0 -> 58292 bytes
-rw-r--r--app/h-ety-h.rb5
-rwxr-xr-xapp/installer/HackFolder.ini49
-rwxr-xr-xapp/installer/base.nsi252
-rwxr-xr-xapp/installer/installer-1.bmpbin0 -> 154544 bytes
-rwxr-xr-xapp/installer/installer-2.bmpbin0 -> 25820 bytes
-rwxr-xr-xapp/installer/setup.icobin0 -> 10134 bytes
-rw-r--r--app/lessons/basic_programming.rb302
-rw-r--r--app/lessons/basic_ruby.rb281
-rw-r--r--app/lessons/basic_shoes.rb183
-rw-r--r--app/lessons/tour.rb180
-rw-r--r--app/lib/all.rb9
-rw-r--r--app/lib/art/turtle.rb349
-rw-r--r--app/lib/dev/errors.rb153
-rw-r--r--app/lib/dev/events.rb112
-rw-r--r--app/lib/dev/init.rb48
-rw-r--r--app/lib/dev/stdout.rb9
-rw-r--r--app/lib/dev/win32.rb29
-rw-r--r--app/lib/enhancements.rb158
-rw-r--r--app/lib/web/all.rb2
-rw-r--r--app/lib/web/hacker.rb37
-rw-r--r--app/lib/web/web.rb318
-rw-r--r--app/lib/web/yaml.rb60
-rw-r--r--app/platform/mac/App.icnsbin0 -> 43392 bytes
-rw-r--r--app/platform/mac/Cheat.icnsbin0 -> 49066 bytes
-rw-r--r--app/platform/mac/Help.icnsbin0 -> 49908 bytes
-rw-r--r--app/platform/mac/dmg_ds_storebin0 -> 12292 bytes
-rwxr-xr-xapp/platform/msw/App.icobin0 -> 302430 bytes
-rwxr-xr-xapp/platform/msw/Cheat.icobin0 -> 302430 bytes
-rwxr-xr-xapp/platform/msw/Help.icobin0 -> 302430 bytes
-rw-r--r--app/platform/nix/app.pngbin0 -> 5591 bytes
-rw-r--r--app/root/Home/.gitignore0
-rwxr-xr-xapp/root/comics.txt4
-rwxr-xr-xapp/spec/all.rb5
-rwxr-xr-xapp/spec/enhancements.rb452
-rwxr-xr-xapp/spec/events.rb217
-rwxr-xr-xapp/spec/rspec.rb30
-rwxr-xr-xapp/spec/stdout.rb40
-rw-r--r--app/static/hacketyhack-dmg.jpgbin0 -> 18298 bytes
-rw-r--r--app/static/hhabout.pngbin0 -> 92278 bytes
-rwxr-xr-xapp/static/hhcheat.pngbin0 -> 120296 bytes
-rwxr-xr-xapp/static/hhconsole.pngbin0 -> 5988 bytes
-rw-r--r--app/static/hhhello.pngbin0 -> 301160 bytes
-rw-r--r--app/static/icon-art.pngbin0 -> 450 bytes
-rw-r--r--app/static/icon-dingbat.pngbin0 -> 676 bytes
-rw-r--r--app/static/icon-email.pngbin0 -> 673 bytes
-rwxr-xr-xapp/static/icon-file.pngbin0 -> 294 bytes
-rw-r--r--app/static/icon-sound.pngbin0 -> 732 bytes
-rw-r--r--app/static/icon-table.pngbin0 -> 726 bytes
-rw-r--r--app/static/matz.jpgbin0 -> 17047 bytes
-rw-r--r--app/static/splash-hand.pngbin0 -> 78698 bytes
-rw-r--r--app/static/tab-cheat.pngbin0 -> 918 bytes
-rw-r--r--app/static/tab-email.pngbin0 -> 641 bytes
-rw-r--r--app/static/tab-hand.pngbin0 -> 959 bytes
-rw-r--r--app/static/tab-help.pngbin0 -> 978 bytes
-rw-r--r--app/static/tab-home.pngbin0 -> 806 bytes
-rw-r--r--app/static/tab-new.pngbin0 -> 342 bytes
-rw-r--r--app/static/tab-properties.pngbin0 -> 464 bytes
-rw-r--r--app/static/tab-quit.pngbin0 -> 688 bytes
-rw-r--r--app/static/tab-tour.pngbin0 -> 782 bytes
-rw-r--r--app/static/tab-try.pngbin0 -> 525 bytes
-rw-r--r--app/static/turtle.pngbin0 -> 814 bytes
-rw-r--r--fonts/Coolvetica.ttfbin0 -> 38828 bytes
-rw-r--r--fonts/Lacuna.ttfbin0 -> 56784 bytes
-rwxr-xr-xhacketyhack22
-rwxr-xr-xhacketyhack-binbin0 -> 5860 bytes
-rw-r--r--lib/shoes.rb548
-rw-r--r--lib/shoes/cache.rb54
-rw-r--r--lib/shoes/chipmunk.rb35
-rw-r--r--lib/shoes/data.rb39
-rw-r--r--lib/shoes/help.rb468
-rw-r--r--lib/shoes/image.rb25
-rw-r--r--lib/shoes/inspect.rb128
-rw-r--r--lib/shoes/log.rb48
-rw-r--r--lib/shoes/minitar.rb986
-rw-r--r--lib/shoes/override.rb38
-rw-r--r--lib/shoes/pack.rb543
-rw-r--r--lib/shoes/search.rb46
-rw-r--r--lib/shoes/setup.rb329
-rw-r--r--lib/shoes/shy.rb131
-rw-r--r--lib/shoes/shybuilder.rb44
-rw-r--r--libcurl.so.4bin0 -> 321284 bytes
-rw-r--r--libgif.so.4bin0 -> 33732 bytes
-rw-r--r--libjpeg.so.8bin0 -> 137572 bytes
-rw-r--r--libportaudio.so.2bin0 -> 171060 bytes
-rw-r--r--libruby.sobin0 -> 1721625 bytes
-rw-r--r--libruby.so.1.9bin0 -> 1721625 bytes
-rwxr-xr-xlibshoes.sobin0 -> 400138 bytes
-rw-r--r--libsqlite3.so.0bin0 -> 568620 bytes
-rw-r--r--libungif.so.4bin0 -> 33732 bytes
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/fast_xs.sobin0 -> 12550 bytes
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot.rb26
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/blankslate.rb63
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/builder.rb216
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/elements.rb510
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/htmlinfo.rb691
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/inspect.rb103
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/modules.rb40
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/parse.rb38
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/tag.rb202
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/tags.rb164
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/traverse.rb838
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/xchar.rb94
-rw-r--r--ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot_scan.sobin0 -> 231697 bytes
-rw-r--r--ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json.rb8
-rw-r--r--ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/core.rb135
-rw-r--r--ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb58
-rw-r--r--ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/common.rb354
-rw-r--r--ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext.rb13
-rw-r--r--ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext/generator.sobin0 -> 49918 bytes
-rw-r--r--ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext/parser.sobin0 -> 40444 bytes
-rw-r--r--ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/version.rb9
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3.rb11
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/constants.rb49
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/database.rb568
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/errors.rb44
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/pragmas.rb280
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/resultset.rb126
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/statement.rb146
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/translator.rb114
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/value.rb57
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/version.rb16
-rw-r--r--ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3_native.sobin0 -> 68276 bytes
-rw-r--r--ruby/gems/1.9.1/specifications/hpricot-0.8.1.gemspec32
-rw-r--r--ruby/gems/1.9.1/specifications/json-shoes-1.1.3.gemspec34
-rw-r--r--ruby/gems/1.9.1/specifications/sqlite3-ruby-1.3.0.gemspec48
-rw-r--r--ruby/lib/English.rb155
-rw-r--r--ruby/lib/abbrev.rb103
-rw-r--r--ruby/lib/base64.rb91
-rw-r--r--ruby/lib/benchmark.rb573
-rw-r--r--ruby/lib/bigdecimal/jacobian.rb85
-rw-r--r--ruby/lib/bigdecimal/ludcmp.rb86
-rw-r--r--ruby/lib/bigdecimal/math.rb237
-rw-r--r--ruby/lib/bigdecimal/newton.rb77
-rw-r--r--ruby/lib/bigdecimal/util.rb53
-rw-r--r--ruby/lib/cgi.rb274
-rw-r--r--ruby/lib/cgi/cookie.rb144
-rw-r--r--ruby/lib/cgi/core.rb786
-rw-r--r--ruby/lib/cgi/html.rb1021
-rw-r--r--ruby/lib/cgi/session.rb537
-rw-r--r--ruby/lib/cgi/session/pstore.rb111
-rw-r--r--ruby/lib/cgi/util.rb181
-rw-r--r--ruby/lib/cmath.rb233
-rw-r--r--ruby/lib/complex.rb24
-rw-r--r--ruby/lib/csv.rb2320
-rw-r--r--ruby/lib/date.rb1834
-rw-r--r--ruby/lib/date/format.rb1313
-rw-r--r--ruby/lib/debug.rb907
-rw-r--r--ruby/lib/delegate.rb311
-rw-r--r--ruby/lib/digest.rb50
-rw-r--r--ruby/lib/digest/hmac.rb274
-rw-r--r--ruby/lib/digest/sha2.rb74
-rw-r--r--ruby/lib/dl/callback.rb69
-rw-r--r--ruby/lib/dl/cparser.rb109
-rw-r--r--ruby/lib/dl/func.rb153
-rw-r--r--ruby/lib/dl/import.rb215
-rw-r--r--ruby/lib/dl/pack.rb173
-rw-r--r--ruby/lib/dl/stack.rb146
-rw-r--r--ruby/lib/dl/struct.rb213
-rw-r--r--ruby/lib/dl/types.rb40
-rw-r--r--ruby/lib/dl/value.rb112
-rw-r--r--ruby/lib/drb.rb2
-rw-r--r--ruby/lib/drb/acl.rb146
-rw-r--r--ruby/lib/drb/drb.rb1778
-rw-r--r--ruby/lib/drb/eq.rb16
-rw-r--r--ruby/lib/drb/extserv.rb71
-rw-r--r--ruby/lib/drb/extservm.rb85
-rw-r--r--ruby/lib/drb/gw.rb122
-rw-r--r--ruby/lib/drb/invokemethod.rb34
-rw-r--r--ruby/lib/drb/observer.rb22
-rw-r--r--ruby/lib/drb/ssl.rb190
-rw-r--r--ruby/lib/drb/timeridconv.rb91
-rw-r--r--ruby/lib/drb/unix.rb108
-rw-r--r--ruby/lib/e2mmap.rb172
-rw-r--r--ruby/lib/erb.rb902
-rw-r--r--ruby/lib/expect.rb36
-rw-r--r--ruby/lib/fileutils.rb1592
-rw-r--r--ruby/lib/find.rb81
-rw-r--r--ruby/lib/forwardable.rb270
-rw-r--r--ruby/lib/ftsearch/analysis/analyzer.rb16
-rw-r--r--ruby/lib/ftsearch/analysis/simple_identifier_analyzer.rb23
-rw-r--r--ruby/lib/ftsearch/analysis/whitespace_analyzer.rb22
-rw-r--r--ruby/lib/ftsearch/document_map_reader.rb106
-rw-r--r--ruby/lib/ftsearch/document_map_writer.rb46
-rw-r--r--ruby/lib/ftsearch/field_infos.rb46
-rw-r--r--ruby/lib/ftsearch/fragment_writer.rb114
-rw-r--r--ruby/lib/ftsearch/fulltext_reader.rb52
-rw-r--r--ruby/lib/ftsearch/fulltext_writer.rb75
-rw-r--r--ruby/lib/ftsearch/suffix_array_reader.rb277
-rw-r--r--ruby/lib/ftsearch/suffix_array_writer.rb99
-rw-r--r--ruby/lib/ftsearch/util.rb21
-rw-r--r--ruby/lib/getoptlong.rb610
-rw-r--r--ruby/lib/gserver.rb253
-rw-r--r--ruby/lib/i686-linux/bigdecimal.sobin0 -> 105292 bytes
-rw-r--r--ruby/lib/i686-linux/binject.sobin0 -> 388773 bytes
-rw-r--r--ruby/lib/i686-linux/bloops.sobin0 -> 84586 bytes
-rw-r--r--ruby/lib/i686-linux/chipmunk.sobin0 -> 295410 bytes
-rw-r--r--ruby/lib/i686-linux/continuation.sobin0 -> 5401 bytes
-rw-r--r--ruby/lib/i686-linux/coverage.sobin0 -> 10145 bytes
-rw-r--r--ruby/lib/i686-linux/digest.sobin0 -> 29350 bytes
-rw-r--r--ruby/lib/i686-linux/digest/bubblebabble.sobin0 -> 11866 bytes
-rw-r--r--ruby/lib/i686-linux/digest/md5.sobin0 -> 16991 bytes
-rw-r--r--ruby/lib/i686-linux/digest/rmd160.sobin0 -> 21968 bytes
-rw-r--r--ruby/lib/i686-linux/digest/sha1.sobin0 -> 21691 bytes
-rw-r--r--ruby/lib/i686-linux/digest/sha2.sobin0 -> 31341 bytes
-rw-r--r--ruby/lib/i686-linux/dl.sobin0 -> 3981364 bytes
-rw-r--r--ruby/lib/i686-linux/enc/big5.sobin0 -> 13671 bytes
-rw-r--r--ruby/lib/i686-linux/enc/cp949.sobin0 -> 13695 bytes
-rw-r--r--ruby/lib/i686-linux/enc/emacs_mule.sobin0 -> 17456 bytes
-rw-r--r--ruby/lib/i686-linux/enc/encdb.sobin0 -> 10177 bytes
-rw-r--r--ruby/lib/i686-linux/enc/euc_jp.sobin0 -> 19551 bytes
-rw-r--r--ruby/lib/i686-linux/enc/euc_kr.sobin0 -> 13190 bytes
-rw-r--r--ruby/lib/i686-linux/enc/euc_tw.sobin0 -> 13850 bytes
-rw-r--r--ruby/lib/i686-linux/enc/gb18030.sobin0 -> 15927 bytes
-rw-r--r--ruby/lib/i686-linux/enc/gb2312.sobin0 -> 7289 bytes
-rw-r--r--ruby/lib/i686-linux/enc/gbk.sobin0 -> 13643 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_1.sobin0 -> 12403 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_10.sobin0 -> 12040 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_11.sobin0 -> 9757 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_13.sobin0 -> 12040 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_14.sobin0 -> 12040 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_15.sobin0 -> 12040 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_16.sobin0 -> 12040 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_2.sobin0 -> 12027 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_3.sobin0 -> 12027 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_4.sobin0 -> 12027 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_5.sobin0 -> 11875 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_6.sobin0 -> 9745 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_7.sobin0 -> 11871 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_8.sobin0 -> 9745 bytes
-rw-r--r--ruby/lib/i686-linux/enc/iso_8859_9.sobin0 -> 12027 bytes
-rw-r--r--ruby/lib/i686-linux/enc/koi8_r.sobin0 -> 11463 bytes
-rw-r--r--ruby/lib/i686-linux/enc/koi8_u.sobin0 -> 11883 bytes
-rw-r--r--ruby/lib/i686-linux/enc/shift_jis.sobin0 -> 19426 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/big5.sobin0 -> 153348 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/chinese.sobin0 -> 186350 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/escape.sobin0 -> 11837 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/gb18030.sobin0 -> 601372 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/gbk.sobin0 -> 198134 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/iso2022.sobin0 -> 14635 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/japanese.sobin0 -> 11476 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/japanese_euc.sobin0 -> 243110 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/japanese_sjis.sobin0 -> 153412 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/korean.sobin0 -> 246746 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/single_byte.sobin0 -> 103808 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/transdb.sobin0 -> 10065 bytes
-rw-r--r--ruby/lib/i686-linux/enc/trans/utf_16_32.sobin0 -> 17968 bytes
-rw-r--r--ruby/lib/i686-linux/enc/utf_16be.sobin0 -> 13215 bytes
-rw-r--r--ruby/lib/i686-linux/enc/utf_16le.sobin0 -> 13127 bytes
-rw-r--r--ruby/lib/i686-linux/enc/utf_32be.sobin0 -> 11131 bytes
-rw-r--r--ruby/lib/i686-linux/enc/utf_32le.sobin0 -> 11187 bytes
-rw-r--r--ruby/lib/i686-linux/enc/windows_1251.sobin0 -> 11925 bytes
-rw-r--r--ruby/lib/i686-linux/etc.sobin0 -> 20092 bytes
-rw-r--r--ruby/lib/i686-linux/fcntl.sobin0 -> 8464 bytes
-rw-r--r--ruby/lib/i686-linux/fiber.sobin0 -> 5340 bytes
-rw-r--r--ruby/lib/i686-linux/ftsearchrt.sobin0 -> 43202 bytes
-rw-r--r--ruby/lib/i686-linux/iconv.sobin0 -> 46212 bytes
-rw-r--r--ruby/lib/i686-linux/io/wait.sobin0 -> 13828 bytes
-rw-r--r--ruby/lib/i686-linux/json/ext/generator.sobin0 -> 50600 bytes
-rw-r--r--ruby/lib/i686-linux/json/ext/parser.sobin0 -> 40625 bytes
-rw-r--r--ruby/lib/i686-linux/mathn/complex.sobin0 -> 5371 bytes
-rw-r--r--ruby/lib/i686-linux/mathn/rational.sobin0 -> 5376 bytes
-rw-r--r--ruby/lib/i686-linux/nkf.sobin0 -> 310133 bytes
-rw-r--r--ruby/lib/i686-linux/pty.sobin0 -> 23381 bytes
-rw-r--r--ruby/lib/i686-linux/racc/cparse.sobin0 -> 34277 bytes
-rw-r--r--ruby/lib/i686-linux/rbconfig.rb196
-rw-r--r--ruby/lib/i686-linux/ripper.sobin0 -> 272966 bytes
-rw-r--r--ruby/lib/i686-linux/sdbm.sobin0 -> 58250 bytes
-rw-r--r--ruby/lib/i686-linux/socket.sobin0 -> 104698 bytes
-rw-r--r--ruby/lib/i686-linux/stringio.sobin0 -> 52911 bytes
-rw-r--r--ruby/lib/i686-linux/strscan.sobin0 -> 46252 bytes
-rw-r--r--ruby/lib/i686-linux/syck.sobin0 -> 284552 bytes
-rw-r--r--ruby/lib/i686-linux/syslog.sobin0 -> 23332 bytes
-rw-r--r--ruby/lib/i686-linux/zlib.sobin0 -> 103571 bytes
-rw-r--r--ruby/lib/io/nonblock.rb23
-rw-r--r--ruby/lib/ipaddr.rb813
-rw-r--r--ruby/lib/irb.rb354
-rw-r--r--ruby/lib/irb/cmd/chws.rb32
-rw-r--r--ruby/lib/irb/cmd/fork.rb38
-rw-r--r--ruby/lib/irb/cmd/help.rb36
-rw-r--r--ruby/lib/irb/cmd/load.rb66
-rw-r--r--ruby/lib/irb/cmd/nop.rb38
-rw-r--r--ruby/lib/irb/cmd/pushws.rb38
-rw-r--r--ruby/lib/irb/cmd/subirb.rb42
-rw-r--r--ruby/lib/irb/completion.rb207
-rw-r--r--ruby/lib/irb/context.rb255
-rw-r--r--ruby/lib/irb/ext/change-ws.rb61
-rw-r--r--ruby/lib/irb/ext/history.rb109
-rw-r--r--ruby/lib/irb/ext/loader.rb119
-rw-r--r--ruby/lib/irb/ext/math-mode.rb36
-rw-r--r--ruby/lib/irb/ext/multi-irb.rb240
-rw-r--r--ruby/lib/irb/ext/save-history.rb100
-rw-r--r--ruby/lib/irb/ext/tracer.rb60
-rw-r--r--ruby/lib/irb/ext/use-loader.rb64
-rw-r--r--ruby/lib/irb/ext/workspaces.rb55
-rw-r--r--ruby/lib/irb/extend-command.rb272
-rw-r--r--ruby/lib/irb/frame.rb66
-rw-r--r--ruby/lib/irb/help.rb35
-rw-r--r--ruby/lib/irb/init.rb288
-rw-r--r--ruby/lib/irb/input-method.rb142
-rw-r--r--ruby/lib/irb/lc/error.rb29
-rw-r--r--ruby/lib/irb/lc/help-message38
-rw-r--r--ruby/lib/irb/lc/ja/encoding_aliases.rb8
-rw-r--r--ruby/lib/irb/lc/ja/error.rb27
-rw-r--r--ruby/lib/irb/lc/ja/help-message39
-rw-r--r--ruby/lib/irb/locale.rb195
-rw-r--r--ruby/lib/irb/magic-file.rb36
-rw-r--r--ruby/lib/irb/notifier.rb144
-rw-r--r--ruby/lib/irb/output-method.rb69
-rw-r--r--ruby/lib/irb/ruby-lex.rb1188
-rw-r--r--ruby/lib/irb/ruby-token.rb270
-rw-r--r--ruby/lib/irb/slex.rb282
-rw-r--r--ruby/lib/irb/src_encoding.rb4
-rw-r--r--ruby/lib/irb/version.rb15
-rw-r--r--ruby/lib/irb/workspace.rb108
-rw-r--r--ruby/lib/irb/ws-for-case-2.rb14
-rw-r--r--ruby/lib/irb/xmp.rb97
-rw-r--r--ruby/lib/json.rb297
-rw-r--r--ruby/lib/json/add/core.rb135
-rw-r--r--ruby/lib/json/add/rails.rb58
-rw-r--r--ruby/lib/json/common.rb354
-rw-r--r--ruby/lib/json/editor.rb1371
-rw-r--r--ruby/lib/json/ext.rb15
-rw-r--r--ruby/lib/json/version.rb9
-rw-r--r--ruby/lib/kconv.rb282
-rw-r--r--ruby/lib/logger.rb732
-rw-r--r--ruby/lib/mathn.rb206
-rw-r--r--ruby/lib/matrix.rb1381
-rw-r--r--ruby/lib/minitest/autorun.rb9
-rw-r--r--ruby/lib/minitest/mock.rb37
-rw-r--r--ruby/lib/minitest/spec.rb89
-rw-r--r--ruby/lib/minitest/unit.rb497
-rw-r--r--ruby/lib/mkmf.rb1958
-rw-r--r--ruby/lib/monitor.rb265
-rw-r--r--ruby/lib/mutex_m.rb91
-rw-r--r--ruby/lib/net/ftp.rb981
-rw-r--r--ruby/lib/net/http.rb2399
-rw-r--r--ruby/lib/net/https.rb136
-rw-r--r--ruby/lib/net/imap.rb3500
-rw-r--r--ruby/lib/net/pop.rb1000
-rw-r--r--ruby/lib/net/protocol.rb382
-rw-r--r--ruby/lib/net/smtp.rb1014
-rw-r--r--ruby/lib/net/telnet.rb759
-rw-r--r--ruby/lib/observer.rb193
-rw-r--r--ruby/lib/open-uri.rb832
-rw-r--r--ruby/lib/open3.rb98
-rw-r--r--ruby/lib/optparse.rb1810
-rw-r--r--ruby/lib/optparse/date.rb17
-rw-r--r--ruby/lib/optparse/shellwords.rb6
-rw-r--r--ruby/lib/optparse/time.rb10
-rw-r--r--ruby/lib/optparse/uri.rb6
-rw-r--r--ruby/lib/optparse/version.rb70
-rw-r--r--ruby/lib/ostruct.rb145
-rw-r--r--ruby/lib/pathname.rb1099
-rw-r--r--ruby/lib/pp.rb532
-rw-r--r--ruby/lib/prettyprint.rb896
-rw-r--r--ruby/lib/prime.rb471
-rw-r--r--ruby/lib/profile.rb10
-rw-r--r--ruby/lib/profiler.rb59
-rw-r--r--ruby/lib/pstore.rb543
-rw-r--r--ruby/lib/racc/parser.rb441
-rw-r--r--ruby/lib/rake.rb2465
-rw-r--r--ruby/lib/rake/classic_namespace.rb8
-rw-r--r--ruby/lib/rake/clean.rb33
-rw-r--r--ruby/lib/rake/gempackagetask.rb97
-rw-r--r--ruby/lib/rake/loaders/makefile.rb35
-rw-r--r--ruby/lib/rake/packagetask.rb185
-rw-r--r--ruby/lib/rake/rake_test_loader.rb5
-rw-r--r--ruby/lib/rake/rdoctask.rb147
-rw-r--r--ruby/lib/rake/runtest.rb23
-rw-r--r--ruby/lib/rake/tasklib.rb23
-rw-r--r--ruby/lib/rake/testtask.rb161
-rw-r--r--ruby/lib/rake/win32.rb34
-rw-r--r--ruby/lib/rational.rb19
-rw-r--r--ruby/lib/rbconfig/datadir.rb24
-rw-r--r--ruby/lib/rdoc.rb395
-rw-r--r--ruby/lib/rdoc/code_objects.rb1061
-rw-r--r--ruby/lib/rdoc/diagram.rb340
-rw-r--r--ruby/lib/rdoc/dot.rb249
-rw-r--r--ruby/lib/rdoc/generator.rb1082
-rw-r--r--ruby/lib/rdoc/generator/chm.rb113
-rw-r--r--ruby/lib/rdoc/generator/chm/chm.rb100
-rw-r--r--ruby/lib/rdoc/generator/html.rb445
-rw-r--r--ruby/lib/rdoc/generator/html/common.rb24
-rw-r--r--ruby/lib/rdoc/generator/html/frameless.rb92
-rw-r--r--ruby/lib/rdoc/generator/html/hefss.rb150
-rw-r--r--ruby/lib/rdoc/generator/html/html.rb769
-rw-r--r--ruby/lib/rdoc/generator/html/kilmer.rb151
-rw-r--r--ruby/lib/rdoc/generator/html/kilmerfactory.rb427
-rw-r--r--ruby/lib/rdoc/generator/html/one_page_html.rb122
-rw-r--r--ruby/lib/rdoc/generator/ri.rb226
-rw-r--r--ruby/lib/rdoc/generator/texinfo.rb81
-rw-r--r--ruby/lib/rdoc/generator/texinfo/class.texinfo.erb44
-rw-r--r--ruby/lib/rdoc/generator/texinfo/file.texinfo.erb6
-rw-r--r--ruby/lib/rdoc/generator/texinfo/method.texinfo.erb6
-rw-r--r--ruby/lib/rdoc/generator/texinfo/texinfo.erb28
-rw-r--r--ruby/lib/rdoc/generator/xml.rb117
-rw-r--r--ruby/lib/rdoc/generator/xml/rdf.rb113
-rw-r--r--ruby/lib/rdoc/generator/xml/xml.rb123
-rw-r--r--ruby/lib/rdoc/known_classes.rb68
-rw-r--r--ruby/lib/rdoc/markup.rb378
-rw-r--r--ruby/lib/rdoc/markup/attribute_manager.rb265
-rw-r--r--ruby/lib/rdoc/markup/formatter.rb14
-rw-r--r--ruby/lib/rdoc/markup/fragments.rb337
-rw-r--r--ruby/lib/rdoc/markup/inline.rb101
-rw-r--r--ruby/lib/rdoc/markup/lines.rb152
-rw-r--r--ruby/lib/rdoc/markup/preprocess.rb75
-rw-r--r--ruby/lib/rdoc/markup/to_flow.rb185
-rw-r--r--ruby/lib/rdoc/markup/to_html.rb403
-rw-r--r--ruby/lib/rdoc/markup/to_html_crossref.rb148
-rw-r--r--ruby/lib/rdoc/markup/to_latex.rb328
-rw-r--r--ruby/lib/rdoc/markup/to_test.rb50
-rw-r--r--ruby/lib/rdoc/markup/to_texinfo.rb69
-rw-r--r--ruby/lib/rdoc/options.rb638
-rw-r--r--ruby/lib/rdoc/parser.rb142
-rw-r--r--ruby/lib/rdoc/parser/c.rb661
-rw-r--r--ruby/lib/rdoc/parser/f95.rb1835
-rw-r--r--ruby/lib/rdoc/parser/perl.rb165
-rw-r--r--ruby/lib/rdoc/parser/ruby.rb2829
-rw-r--r--ruby/lib/rdoc/parser/simple.rb38
-rw-r--r--ruby/lib/rdoc/rdoc.rb293
-rw-r--r--ruby/lib/rdoc/ri.rb8
-rw-r--r--ruby/lib/rdoc/ri/cache.rb187
-rw-r--r--ruby/lib/rdoc/ri/descriptions.rb156
-rw-r--r--ruby/lib/rdoc/ri/display.rb392
-rw-r--r--ruby/lib/rdoc/ri/driver.rb669
-rw-r--r--ruby/lib/rdoc/ri/formatter.rb616
-rw-r--r--ruby/lib/rdoc/ri/paths.rb99
-rw-r--r--ruby/lib/rdoc/ri/reader.rb106
-rw-r--r--ruby/lib/rdoc/ri/util.rb79
-rw-r--r--ruby/lib/rdoc/ri/writer.rb68
-rw-r--r--ruby/lib/rdoc/stats.rb115
-rw-r--r--ruby/lib/rdoc/template.rb64
-rw-r--r--ruby/lib/rdoc/tokenstream.rb33
-rw-r--r--ruby/lib/resolv-replace.rb63
-rw-r--r--ruby/lib/resolv.rb2262
-rw-r--r--ruby/lib/rexml/attlistdecl.rb62
-rw-r--r--ruby/lib/rexml/attribute.rb188
-rw-r--r--ruby/lib/rexml/cdata.rb67
-rw-r--r--ruby/lib/rexml/child.rb96
-rw-r--r--ruby/lib/rexml/comment.rb80
-rw-r--r--ruby/lib/rexml/doctype.rb270
-rw-r--r--ruby/lib/rexml/document.rb231
-rw-r--r--ruby/lib/rexml/dtd/attlistdecl.rb10
-rw-r--r--ruby/lib/rexml/dtd/dtd.rb51
-rw-r--r--ruby/lib/rexml/dtd/elementdecl.rb17
-rw-r--r--ruby/lib/rexml/dtd/entitydecl.rb56
-rw-r--r--ruby/lib/rexml/dtd/notationdecl.rb39
-rw-r--r--ruby/lib/rexml/element.rb1246
-rw-r--r--ruby/lib/rexml/encoding.rb71
-rw-r--r--ruby/lib/rexml/encodings/CP-1252.rb103
-rw-r--r--ruby/lib/rexml/encodings/EUC-JP.rb35
-rw-r--r--ruby/lib/rexml/encodings/ICONV.rb22
-rw-r--r--ruby/lib/rexml/encodings/ISO-8859-1.rb7
-rw-r--r--ruby/lib/rexml/encodings/ISO-8859-15.rb72
-rw-r--r--ruby/lib/rexml/encodings/SHIFT-JIS.rb37
-rw-r--r--ruby/lib/rexml/encodings/SHIFT_JIS.rb1
-rw-r--r--ruby/lib/rexml/encodings/UNILE.rb34
-rw-r--r--ruby/lib/rexml/encodings/US-ASCII.rb30
-rw-r--r--ruby/lib/rexml/encodings/UTF-16.rb35
-rw-r--r--ruby/lib/rexml/encodings/UTF-8.rb18
-rw-r--r--ruby/lib/rexml/entity.rb166
-rw-r--r--ruby/lib/rexml/formatters/default.rb109
-rw-r--r--ruby/lib/rexml/formatters/pretty.rb139
-rw-r--r--ruby/lib/rexml/formatters/transitive.rb58
-rw-r--r--ruby/lib/rexml/functions.rb388
-rw-r--r--ruby/lib/rexml/instruction.rb70
-rw-r--r--ruby/lib/rexml/light/node.rb196
-rw-r--r--ruby/lib/rexml/namespace.rb47
-rw-r--r--ruby/lib/rexml/node.rb75
-rw-r--r--ruby/lib/rexml/output.rb24
-rw-r--r--ruby/lib/rexml/parent.rb166
-rw-r--r--ruby/lib/rexml/parseexception.rb51
-rw-r--r--ruby/lib/rexml/parsers/baseparser.rb530
-rw-r--r--ruby/lib/rexml/parsers/lightparser.rb58
-rw-r--r--ruby/lib/rexml/parsers/pullparser.rb196
-rw-r--r--ruby/lib/rexml/parsers/sax2parser.rb247
-rw-r--r--ruby/lib/rexml/parsers/streamparser.rb46
-rw-r--r--ruby/lib/rexml/parsers/treeparser.rb100
-rw-r--r--ruby/lib/rexml/parsers/ultralightparser.rb56
-rw-r--r--ruby/lib/rexml/parsers/xpathparser.rb698
-rw-r--r--ruby/lib/rexml/quickpath.rb263
-rw-r--r--ruby/lib/rexml/rexml.rb31
-rw-r--r--ruby/lib/rexml/sax2listener.rb97
-rw-r--r--ruby/lib/rexml/source.rb258
-rw-r--r--ruby/lib/rexml/streamlistener.rb92
-rw-r--r--ruby/lib/rexml/syncenumerator.rb32
-rw-r--r--ruby/lib/rexml/text.rb404
-rw-r--r--ruby/lib/rexml/undefinednamespaceexception.rb8
-rw-r--r--ruby/lib/rexml/validation/relaxng.rb559
-rw-r--r--ruby/lib/rexml/validation/validation.rb155
-rw-r--r--ruby/lib/rexml/validation/validationexception.rb9
-rw-r--r--ruby/lib/rexml/xmldecl.rb119
-rw-r--r--ruby/lib/rexml/xmltokens.rb18
-rw-r--r--ruby/lib/rexml/xpath.rb77
-rw-r--r--ruby/lib/rexml/xpath_parser.rb792
-rw-r--r--ruby/lib/rinda/rinda.rb283
-rw-r--r--ruby/lib/rinda/ring.rb271
-rw-r--r--ruby/lib/rinda/tuplespace.rb642
-rw-r--r--ruby/lib/ripper.rb4
-rw-r--r--ruby/lib/ripper/core.rb70
-rw-r--r--ruby/lib/ripper/filter.rb70
-rw-r--r--ruby/lib/ripper/lexer.rb179
-rw-r--r--ruby/lib/ripper/sexp.rb99
-rw-r--r--ruby/lib/rss.rb19
-rw-r--r--ruby/lib/rss/0.9.rb427
-rw-r--r--ruby/lib/rss/1.0.rb452
-rw-r--r--ruby/lib/rss/2.0.rb111
-rw-r--r--ruby/lib/rss/atom.rb748
-rw-r--r--ruby/lib/rss/content.rb31
-rw-r--r--ruby/lib/rss/content/1.0.rb10
-rw-r--r--ruby/lib/rss/content/2.0.rb12
-rw-r--r--ruby/lib/rss/converter.rb170
-rw-r--r--ruby/lib/rss/dublincore.rb161
-rw-r--r--ruby/lib/rss/dublincore/1.0.rb13
-rw-r--r--ruby/lib/rss/dublincore/2.0.rb13
-rw-r--r--ruby/lib/rss/dublincore/atom.rb17
-rw-r--r--ruby/lib/rss/image.rb193
-rw-r--r--ruby/lib/rss/itunes.rb410
-rw-r--r--ruby/lib/rss/maker.rb44
-rw-r--r--ruby/lib/rss/maker/0.9.rb467
-rw-r--r--ruby/lib/rss/maker/1.0.rb434
-rw-r--r--ruby/lib/rss/maker/2.0.rb223
-rw-r--r--ruby/lib/rss/maker/atom.rb172
-rw-r--r--ruby/lib/rss/maker/base.rb880
-rw-r--r--ruby/lib/rss/maker/content.rb21
-rw-r--r--ruby/lib/rss/maker/dublincore.rb124
-rw-r--r--ruby/lib/rss/maker/entry.rb163
-rw-r--r--ruby/lib/rss/maker/feed.rb430
-rw-r--r--ruby/lib/rss/maker/image.rb111
-rw-r--r--ruby/lib/rss/maker/itunes.rb242
-rw-r--r--ruby/lib/rss/maker/slash.rb33
-rw-r--r--ruby/lib/rss/maker/syndication.rb18
-rw-r--r--ruby/lib/rss/maker/taxonomy.rb118
-rw-r--r--ruby/lib/rss/maker/trackback.rb61
-rw-r--r--ruby/lib/rss/parser.rb568
-rw-r--r--ruby/lib/rss/rexmlparser.rb54
-rw-r--r--ruby/lib/rss/rss.rb1313
-rw-r--r--ruby/lib/rss/slash.rb49
-rw-r--r--ruby/lib/rss/syndication.rb67
-rw-r--r--ruby/lib/rss/taxonomy.rb145
-rw-r--r--ruby/lib/rss/trackback.rb288
-rw-r--r--ruby/lib/rss/utils.rb111
-rw-r--r--ruby/lib/rss/xml-stylesheet.rb105
-rw-r--r--ruby/lib/rss/xml.rb71
-rw-r--r--ruby/lib/rss/xmlparser.rb93
-rw-r--r--ruby/lib/rss/xmlscanner.rb121
-rw-r--r--ruby/lib/rubygems.rb889
-rw-r--r--ruby/lib/rubygems/builder.rb88
-rw-r--r--ruby/lib/rubygems/command.rb406
-rw-r--r--ruby/lib/rubygems/command_manager.rb146
-rw-r--r--ruby/lib/rubygems/commands/build_command.rb53
-rw-r--r--ruby/lib/rubygems/commands/cert_command.rb86
-rw-r--r--ruby/lib/rubygems/commands/check_command.rb75
-rw-r--r--ruby/lib/rubygems/commands/cleanup_command.rb91
-rw-r--r--ruby/lib/rubygems/commands/contents_command.rb74
-rw-r--r--ruby/lib/rubygems/commands/dependency_command.rb188
-rw-r--r--ruby/lib/rubygems/commands/environment_command.rb128
-rw-r--r--ruby/lib/rubygems/commands/fetch_command.rb62
-rw-r--r--ruby/lib/rubygems/commands/generate_index_command.rb57
-rw-r--r--ruby/lib/rubygems/commands/help_command.rb172
-rw-r--r--ruby/lib/rubygems/commands/install_command.rb148
-rw-r--r--ruby/lib/rubygems/commands/list_command.rb35
-rw-r--r--ruby/lib/rubygems/commands/lock_command.rb110
-rw-r--r--ruby/lib/rubygems/commands/mirror_command.rb111
-rw-r--r--ruby/lib/rubygems/commands/outdated_command.rb33
-rw-r--r--ruby/lib/rubygems/commands/pristine_command.rb93
-rw-r--r--ruby/lib/rubygems/commands/query_command.rb233
-rw-r--r--ruby/lib/rubygems/commands/rdoc_command.rb82
-rw-r--r--ruby/lib/rubygems/commands/search_command.rb37
-rw-r--r--ruby/lib/rubygems/commands/server_command.rb48
-rw-r--r--ruby/lib/rubygems/commands/sources_command.rb152
-rw-r--r--ruby/lib/rubygems/commands/specification_command.rb77
-rw-r--r--ruby/lib/rubygems/commands/stale_command.rb27
-rw-r--r--ruby/lib/rubygems/commands/uninstall_command.rb73
-rw-r--r--ruby/lib/rubygems/commands/unpack_command.rb95
-rw-r--r--ruby/lib/rubygems/commands/update_command.rb181
-rw-r--r--ruby/lib/rubygems/commands/which_command.rb87
-rw-r--r--ruby/lib/rubygems/config_file.rb266
-rw-r--r--ruby/lib/rubygems/custom_require.rb46
-rw-r--r--ruby/lib/rubygems/defaults.rb88
-rw-r--r--ruby/lib/rubygems/dependency.rb119
-rw-r--r--ruby/lib/rubygems/dependency_installer.rb258
-rw-r--r--ruby/lib/rubygems/dependency_list.rb165
-rw-r--r--ruby/lib/rubygems/digest/digest_adapter.rb40
-rw-r--r--ruby/lib/rubygems/digest/md5.rb23
-rw-r--r--ruby/lib/rubygems/digest/sha1.rb17
-rw-r--r--ruby/lib/rubygems/digest/sha2.rb17
-rw-r--r--ruby/lib/rubygems/doc_manager.rb214
-rw-r--r--ruby/lib/rubygems/exceptions.rb84
-rw-r--r--ruby/lib/rubygems/ext.rb18
-rw-r--r--ruby/lib/rubygems/ext/builder.rb56
-rw-r--r--ruby/lib/rubygems/ext/configure_builder.rb24
-rw-r--r--ruby/lib/rubygems/ext/ext_conf_builder.rb23
-rw-r--r--ruby/lib/rubygems/ext/rake_builder.rb27
-rw-r--r--ruby/lib/rubygems/format.rb87
-rw-r--r--ruby/lib/rubygems/gem_openssl.rb83
-rw-r--r--ruby/lib/rubygems/gem_path_searcher.rb100
-rw-r--r--ruby/lib/rubygems/gem_runner.rb58
-rw-r--r--ruby/lib/rubygems/indexer.rb370
-rw-r--r--ruby/lib/rubygems/indexer/abstract_index_builder.rb88
-rw-r--r--ruby/lib/rubygems/indexer/latest_index_builder.rb35
-rw-r--r--ruby/lib/rubygems/indexer/marshal_index_builder.rb17
-rw-r--r--ruby/lib/rubygems/indexer/master_index_builder.rb54
-rw-r--r--ruby/lib/rubygems/indexer/quick_index_builder.rb50
-rw-r--r--ruby/lib/rubygems/install_update_options.rb113
-rw-r--r--ruby/lib/rubygems/installer.rb575
-rw-r--r--ruby/lib/rubygems/local_remote_options.rb134
-rw-r--r--ruby/lib/rubygems/old_format.rb148
-rw-r--r--ruby/lib/rubygems/package.rb95
-rw-r--r--ruby/lib/rubygems/package/f_sync_dir.rb24
-rw-r--r--ruby/lib/rubygems/package/tar_header.rb245
-rw-r--r--ruby/lib/rubygems/package/tar_input.rb219
-rw-r--r--ruby/lib/rubygems/package/tar_output.rb143
-rw-r--r--ruby/lib/rubygems/package/tar_reader.rb86
-rw-r--r--ruby/lib/rubygems/package/tar_reader/entry.rb99
-rw-r--r--ruby/lib/rubygems/package/tar_writer.rb180
-rw-r--r--ruby/lib/rubygems/platform.rb178
-rw-r--r--ruby/lib/rubygems/remote_fetcher.rb344
-rw-r--r--ruby/lib/rubygems/require_paths_builder.rb15
-rw-r--r--ruby/lib/rubygems/requirement.rb163
-rw-r--r--ruby/lib/rubygems/rubygems_version.rb6
-rw-r--r--ruby/lib/rubygems/security.rb786
-rw-r--r--ruby/lib/rubygems/server.rb629
-rw-r--r--ruby/lib/rubygems/source_index.rb559
-rw-r--r--ruby/lib/rubygems/source_info_cache.rb393
-rw-r--r--ruby/lib/rubygems/source_info_cache_entry.rb56
-rw-r--r--ruby/lib/rubygems/spec_fetcher.rb249
-rw-r--r--ruby/lib/rubygems/specification.rb1264
-rw-r--r--ruby/lib/rubygems/test_utilities.rb131
-rw-r--r--ruby/lib/rubygems/timer.rb25
-rw-r--r--ruby/lib/rubygems/uninstaller.rb242
-rw-r--r--ruby/lib/rubygems/user_interaction.rb360
-rw-r--r--ruby/lib/rubygems/validator.rb209
-rw-r--r--ruby/lib/rubygems/version.rb167
-rw-r--r--ruby/lib/rubygems/version_option.rb48
-rw-r--r--ruby/lib/scanf.rb703
-rw-r--r--ruby/lib/securerandom.rb182
-rw-r--r--ruby/lib/set.rb1274
-rw-r--r--ruby/lib/shell.rb300
-rw-r--r--ruby/lib/shell/builtin-command.rb160
-rw-r--r--ruby/lib/shell/command-processor.rb593
-rw-r--r--ruby/lib/shell/error.rb25
-rw-r--r--ruby/lib/shell/filter.rb109
-rw-r--r--ruby/lib/shell/process-controller.rb319
-rw-r--r--ruby/lib/shell/system-command.rb159
-rw-r--r--ruby/lib/shell/version.rb15
-rw-r--r--ruby/lib/shellwords.rb156
-rw-r--r--ruby/lib/singleton.rb313
-rw-r--r--ruby/lib/sync.rb307
-rw-r--r--ruby/lib/tempfile.rb218
-rw-r--r--ruby/lib/test/unit.rb66
-rw-r--r--ruby/lib/test/unit/assertions.rb122
-rw-r--r--ruby/lib/test/unit/testcase.rb12
-rw-r--r--ruby/lib/thread.rb367
-rw-r--r--ruby/lib/thwait.rb168
-rw-r--r--ruby/lib/time.rb869
-rw-r--r--ruby/lib/timeout.rb108
-rw-r--r--ruby/lib/tmpdir.rb138
-rw-r--r--ruby/lib/tracer.rb166
-rw-r--r--ruby/lib/tsort.rb290
-rw-r--r--ruby/lib/ubygems.rb10
-rw-r--r--ruby/lib/un.rb304
-rw-r--r--ruby/lib/uri.rb29
-rw-r--r--ruby/lib/uri/common.rb727
-rw-r--r--ruby/lib/uri/ftp.rb198
-rw-r--r--ruby/lib/uri/generic.rb1128
-rw-r--r--ruby/lib/uri/http.rb100
-rw-r--r--ruby/lib/uri/https.rb20
-rw-r--r--ruby/lib/uri/ldap.rb190
-rw-r--r--ruby/lib/uri/ldaps.rb12
-rw-r--r--ruby/lib/uri/mailto.rb266
-rw-r--r--ruby/lib/weakref.rb80
-rw-r--r--ruby/lib/webrick.rb29
-rw-r--r--ruby/lib/webrick/accesslog.rb75
-rw-r--r--ruby/lib/webrick/cgi.rb260
-rw-r--r--ruby/lib/webrick/compat.rb15
-rw-r--r--ruby/lib/webrick/config.rb100
-rw-r--r--ruby/lib/webrick/cookie.rb110
-rw-r--r--ruby/lib/webrick/htmlutils.rb25
-rw-r--r--ruby/lib/webrick/httpauth.rb45
-rw-r--r--ruby/lib/webrick/httpauth/authenticator.rb79
-rw-r--r--ruby/lib/webrick/httpauth/basicauth.rb65
-rw-r--r--ruby/lib/webrick/httpauth/digestauth.rb344
-rw-r--r--ruby/lib/webrick/httpauth/htdigest.rb91
-rw-r--r--ruby/lib/webrick/httpauth/htgroup.rb61
-rw-r--r--ruby/lib/webrick/httpauth/htpasswd.rb83
-rw-r--r--ruby/lib/webrick/httpauth/userdb.rb29
-rw-r--r--ruby/lib/webrick/httpproxy.rb288
-rw-r--r--ruby/lib/webrick/httprequest.rb402
-rw-r--r--ruby/lib/webrick/httpresponse.rb326
-rw-r--r--ruby/lib/webrick/https.rb63
-rw-r--r--ruby/lib/webrick/httpserver.rb217
-rw-r--r--ruby/lib/webrick/httpservlet.rb22
-rw-r--r--ruby/lib/webrick/httpservlet/abstract.rb70
-rw-r--r--ruby/lib/webrick/httpservlet/cgi_runner.rb47
-rw-r--r--ruby/lib/webrick/httpservlet/cgihandler.rb110
-rw-r--r--ruby/lib/webrick/httpservlet/erbhandler.rb54
-rw-r--r--ruby/lib/webrick/httpservlet/filehandler.rb435
-rw-r--r--ruby/lib/webrick/httpservlet/prochandler.rb33
-rw-r--r--ruby/lib/webrick/httpstatus.rb132
-rw-r--r--ruby/lib/webrick/httputils.rb392
-rw-r--r--ruby/lib/webrick/httpversion.rb49
-rw-r--r--ruby/lib/webrick/log.rb88
-rw-r--r--ruby/lib/webrick/server.rb210
-rw-r--r--ruby/lib/webrick/ssl.rb126
-rw-r--r--ruby/lib/webrick/utils.rb175
-rw-r--r--ruby/lib/webrick/version.rb13
-rw-r--r--ruby/lib/xmlrpc/base64.rb81
-rw-r--r--ruby/lib/xmlrpc/client.rb625
-rw-r--r--ruby/lib/xmlrpc/config.rb40
-rw-r--r--ruby/lib/xmlrpc/create.rb290
-rw-r--r--ruby/lib/xmlrpc/datetime.rb142
-rw-r--r--ruby/lib/xmlrpc/httpserver.rb178
-rw-r--r--ruby/lib/xmlrpc/marshal.rb76
-rw-r--r--ruby/lib/xmlrpc/parser.rb813
-rw-r--r--ruby/lib/xmlrpc/server.rb778
-rw-r--r--ruby/lib/xmlrpc/utils.rb165
-rw-r--r--ruby/lib/yaml.rb440
-rw-r--r--ruby/lib/yaml/baseemitter.rb242
-rw-r--r--ruby/lib/yaml/basenode.rb216
-rw-r--r--ruby/lib/yaml/constants.rb45
-rw-r--r--ruby/lib/yaml/dbm.rb111
-rw-r--r--ruby/lib/yaml/encoding.rb33
-rw-r--r--ruby/lib/yaml/error.rb34
-rw-r--r--ruby/lib/yaml/loader.rb14
-rw-r--r--ruby/lib/yaml/rubytypes.rb446
-rw-r--r--ruby/lib/yaml/store.rb43
-rw-r--r--ruby/lib/yaml/stream.rb40
-rw-r--r--ruby/lib/yaml/stringio.rb83
-rw-r--r--ruby/lib/yaml/syck.rb19
-rw-r--r--ruby/lib/yaml/tag.rb91
-rw-r--r--ruby/lib/yaml/types.rb192
-rw-r--r--ruby/lib/yaml/yamlnode.rb54
-rw-r--r--ruby/lib/yaml/ypath.rb52
-rw-r--r--samples/class-book.rb43
-rw-r--r--samples/class-book.yaml387
-rw-r--r--samples/expert-definr.rb23
-rw-r--r--samples/expert-funnies.rb51
-rw-r--r--samples/expert-irb.rb112
-rw-r--r--samples/expert-minesweeper.rb267
-rw-r--r--samples/expert-othello.rb319
-rw-r--r--samples/expert-pong.rb62
-rw-r--r--samples/expert-tankspank.rb385
-rw-r--r--samples/good-arc.rb37
-rw-r--r--samples/good-clock.rb51
-rw-r--r--samples/good-follow.rb26
-rw-r--r--samples/good-reminder.rb174
-rw-r--r--samples/good-vjot.rb56
-rw-r--r--samples/simple-accordion.rb75
-rw-r--r--samples/simple-anim-shapes.rb17
-rw-r--r--samples/simple-anim-text.rb13
-rw-r--r--samples/simple-arc.rb23
-rw-r--r--samples/simple-bounce.rb24
-rw-r--r--samples/simple-calc.rb70
-rw-r--r--samples/simple-chipmunk.rb26
-rw-r--r--samples/simple-control-sizes.rb24
-rw-r--r--samples/simple-curve.rb26
-rw-r--r--samples/simple-dialogs.rb29
-rw-r--r--samples/simple-downloader.rb27
-rw-r--r--samples/simple-draw.rb13
-rw-r--r--samples/simple-editor.rb28
-rw-r--r--samples/simple-form.rb28
-rw-r--r--samples/simple-form.shybin0 -> 2186 bytes
-rw-r--r--samples/simple-mask.rb21
-rw-r--r--samples/simple-menu.rb31
-rw-r--r--samples/simple-menu1.rb35
-rw-r--r--samples/simple-rubygems.rb29
-rw-r--r--samples/simple-slide.rb45
-rw-r--r--samples/simple-sphere.rb28
-rw-r--r--samples/simple-sqlite3.rb13
-rw-r--r--samples/simple-timer.rb13
-rw-r--r--samples/simple-video.rb13
-rw-r--r--static/Shoes.icnsbin0 -> 34271 bytes
-rw-r--r--static/app-icon.pngbin0 -> 5591 bytes
-rw-r--r--static/avatar.pngbin0 -> 1374 bytes
-rw-r--r--static/code_highlighter.js188
-rw-r--r--static/code_highlighter_ruby.js26
-rw-r--r--static/icon-debug.pngbin0 -> 525 bytes
-rw-r--r--static/icon-error.pngbin0 -> 701 bytes
-rw-r--r--static/icon-info.pngbin0 -> 778 bytes
-rw-r--r--static/icon-warn.pngbin0 -> 666 bytes
-rw-r--r--static/listbox_button1.pngbin0 -> 392 bytes
-rw-r--r--static/listbox_button2.pngbin0 -> 420 bytes
-rw-r--r--static/man-app.pngbin0 -> 10775 bytes
-rw-r--r--static/man-builds.pngbin0 -> 21491 bytes
-rw-r--r--static/man-builds1.pngbin0 -> 18226 bytes
-rw-r--r--static/man-editor-notepad.pngbin0 -> 6580 bytes
-rw-r--r--static/man-editor-osx.pngbin0 -> 11400 bytes
-rw-r--r--static/man-ele-background.pngbin0 -> 4902 bytes
-rw-r--r--static/man-ele-border.pngbin0 -> 6296 bytes
-rw-r--r--static/man-ele-button.pngbin0 -> 5125 bytes
-rw-r--r--static/man-ele-check.pngbin0 -> 6042 bytes
-rw-r--r--static/man-ele-editbox.pngbin0 -> 5308 bytes
-rw-r--r--static/man-ele-editline.pngbin0 -> 4979 bytes
-rw-r--r--static/man-ele-image.pngbin0 -> 81127 bytes
-rw-r--r--static/man-ele-listbox.pngbin0 -> 8100 bytes
-rw-r--r--static/man-ele-progress.pngbin0 -> 4405 bytes
-rw-r--r--static/man-ele-radio.pngbin0 -> 6049 bytes
-rw-r--r--static/man-ele-shape.pngbin0 -> 7225 bytes
-rw-r--r--static/man-ele-textblock.pngbin0 -> 7672 bytes
-rw-r--r--static/man-ele-video.pngbin0 -> 157853 bytes
-rw-r--r--static/man-intro-dmg.pngbin0 -> 23639 bytes
-rw-r--r--static/man-intro-exe.pngbin0 -> 16531 bytes
-rw-r--r--static/man-look-tiger.pngbin0 -> 101078 bytes
-rw-r--r--static/man-look-ubuntu.pngbin0 -> 12692 bytes
-rw-r--r--static/man-look-vista.pngbin0 -> 6629 bytes
-rw-r--r--static/man-run-osx.pngbin0 -> 18150 bytes
-rw-r--r--static/man-run-vista.pngbin0 -> 6724 bytes
-rw-r--r--static/man-run-xp.pngbin0 -> 4348 bytes
-rw-r--r--static/man-shot1.pngbin0 -> 14208 bytes
-rw-r--r--static/manual-en.txt3531
-rw-r--r--static/manual-ja.txt2825
-rw-r--r--static/manual.css167
-rw-r--r--static/menu-corner1.pngbin0 -> 462 bytes
-rw-r--r--static/menu-corner2.pngbin0 -> 563 bytes
-rw-r--r--static/menu-gray.pngbin0 -> 148 bytes
-rw-r--r--static/menu-left.pngbin0 -> 534 bytes
-rw-r--r--static/menu-right.pngbin0 -> 533 bytes
-rw-r--r--static/menu-top.pngbin0 -> 335 bytes
-rw-r--r--static/shoes-dmg.jpgbin0 -> 35616 bytes
-rw-r--r--static/shoes-icon-blue.pngbin0 -> 8035 bytes
-rw-r--r--static/shoes-icon.pngbin0 -> 6771 bytes
-rw-r--r--static/shoes-manual-apps.gifbin0 -> 27695 bytes
-rw-r--r--static/shoes_main_window.pngbin0 -> 54095 bytes
-rw-r--r--static/stripe.pngbin0 -> 328 bytes
-rw-r--r--static/stubs/blank.exebin0 -> 81920 bytes
-rw-r--r--static/stubs/blank.hfzbin0 -> 842 bytes
-rw-r--r--static/stubs/blank.run375
-rw-r--r--static/stubs/cocoa-installbin0 -> 64408 bytes
-rw-r--r--static/stubs/sh-install49
-rw-r--r--static/stubs/shoes-stub-inject.exebin0 -> 80384 bytes
-rw-r--r--static/stubs/shoes-stub.exebin0 -> 99840 bytes
-rw-r--r--static/tutor-back.pngbin0 -> 132 bytes
916 files changed, 157608 insertions, 0 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
new file mode 100644
index 0000000..8491748
--- /dev/null
+++ b/CHANGELOG.txt
@@ -0,0 +1,29 @@
+= Policeman
+== 17th August, 2010
+* Move to Ruby 1.9.1
+* General stability improvements
+* Updated all dependancies
+* Ported OSX to Cocoa
+* Now works with Windows Vista and Windows 7
+
+= Raisins (0.r1134)
+== 5th December, 2008
+<http://shoooes.net/about/raisins>
+* Built-in Shoes manual. (Alt-?)
+* Error console. (Alt-/)
+* In-memory and on-disk image cache.
+* Asynchronous download method.
+* External font support.
+* Packager for building EXE, DMG, RUN and SHY.
+* Effects (blurs, shadows, glows).
+* Arbitrary shape method.
+* Image blocks.
+* Switched from Carbon to Cocoa.
+* RubyGems integration.
+
+= Curious (0.r396)
+== 8th January, 2008
+* First official release of Shoes.
+* Support for OS X, Win32 and GTK+.
+* Scrolling stacks newly added.
+* VLC embedded for Video.
diff --git a/COPYING.txt b/COPYING.txt
new file mode 100644
index 0000000..012b02c
--- /dev/null
+++ b/COPYING.txt
@@ -0,0 +1,30 @@
+Copyright (c) 2008 why the lucky stiff
+Except:
+ fonts/Coolvetica.ttf (c) 1999 Ray Larabie
+ fonts/Lacuna.ttf (c) 2003 Glashaus, designed by Peter Hoffman
+ samples/expert-minesweeper.rb (c) 2008 que
+ samples/expert-othello.rb (c) 2008 Tieg Zaharia
+ samples/expert-tankspank.rb (c) 2008 Kevin C.
+ samples/good-clock.rb (c) 2008 Thomas Bell
+ samples/good-reminder.rb (c) 2008 Oliver Smith
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of the Software,
+and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
+OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..66d2fe4
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,76 @@
+<pre>
+ ((( |||_| ///\ [[[_ (((
+ ))) || | \\/ [[_ )))
+ an artsy any-platform app kit
+ http://shoesrb.com
+</pre>
+
+# About Shoes
+
+Shoes is the best little DSL for cross-platform GUI programming there is. It feels like real Ruby, rather than just another C++ library wrapper. If Gtk or wxWidgets is Rails, Shoes is Sinatra.
+
+# Let me tell you a story about Shoes
+
+ Way way back in the day, there was a guy named \_why. He created a project known as [Hackety Hack](http://hackety-hack.com) to teach programming to everyone. In order to reach all corners of the earth, \_why decided to make Hackety Hack work on Windows, Mac OS X, and Linux. This was a lot of work, and so \_why decided to share his toolkit with the world. Thus, Shoes was born.
+
+Everybody loved Shoes. Many apps were made, and put into [The Shoebox](http://the-shoebox.org/). But, one day, \_why left. In his memory, Team Shoes assembled, and carried on making Shoes. They released Shoes 3 in late summer 2010.
+
+# So what do these Shoes look like?
+
+Here's a little Shoes app. It's a stopwatch!
+
+ Shoes.app :height => 150, :width => 250 do
+ background rgb(240, 250, 208)
+ stack :margin => 10 do
+ button "Start" do
+ @time = Time.now
+ @label.replace "Stop watch started at #@time"
+ end
+ button "Stop" do
+ @label.replace "Stopped, ", strong("#{Time.now - @time}"), " seconds elapsed."
+ end
+ @label = para "Press ", strong("start"), " to begin timing."
+ end
+ end
+
+Here's what it looks like:
+
+![shoes timer](https://github.com/shoes/shoes/raw/develop/manual-snapshots/simple-timer.png)
+
+Pretty simple! For more samples, the manual, and a free book, check out [the Shoes website](http://shoesrb.com/).
+
+# Using Shoes
+
+If you'd like to use Shoes to develop some apps... awesome! It's super easy: Just go to the [downloads page on the Shoes website](http://shoesrb.com/downloads) and download a copy of Shoes for your platform. Mac OSX, Windows, and Linux supported!
+
+After you install Shoes, run it! You'll get a window like this:
+
+![shoes main window](https://github.com/shoes/shoes/raw/develop/static/shoes_main_window.png)
+
+You can then open any .rb file with Shoes code inside by choosing "Open an App." It'll open it up and run it, right away.
+
+Once you're happy with your app, you can choose "Package an App" to wrap up your app as a .exe, .app, or a .run. Then you can share it with someone without a pair of Shoes to call their own.
+
+# Making your own Shoes
+
+You can make your own pair of Shoes with a little bit of elbow grease. Since there are different instructions on each platform, we've got a page up on the [Shoes development wiki](http://github.com/shoes/shoes/wiki) about it. It's [right here](https://github.com/shoes/shoes/wiki/BuildingShoes).
+
+# Shoes Around the Web
+
+If you want to keep up to date with what's going on with Shoes, you can find us in various places:
+
+* [Official Shoes Site](http://shoesrb.com/)
+* [Source Code @ GitHub](http://github.com/shoes/shoes)
+* [Issue tracker @ GitHub](http://github.com/shoes/shoes/issues)
+* [Mailing List](http://librelist.com/browser/shoes/) (send an email to shoes@librelist.com to join)
+* [Twitter account](http://twitter.com/shoooesrb)
+* [Facebook page](http://www.facebook.com/pages/Shoes/132605040125019)
+* IRC room on Freenode, #shoes
+
+# Helping out with Shoes
+
+So you'd like to lend a helping hand, eh? Great! We'd love to have you. To submit a patch to Shoes, just fork us, and send a pull request.
+
+If you don't have any ideas yourself, take a look at the [Issue tracker](http://github.com/shoes/shoes/issues) and see if anything strikes your fancy. If you need help working on something, don't be afraid to post to the mailing list about it!
+
+If you're not a programmer, you can help Shoes by talking about it! Blog posts, tweets, tell your neighbors, call your grandma, whatever! Share Shoes with everyone!
diff --git a/VERSION.txt b/VERSION.txt
new file mode 100644
index 0000000..49b15bc
--- /dev/null
+++ b/VERSION.txt
@@ -0,0 +1 @@
+shoes material (0.r1544) [i686-linux Ruby1.9.1]
diff --git a/activity/activity-hackety.svg b/activity/activity-hackety.svg
new file mode 100644
index 0000000..5212fa7
--- /dev/null
+++ b/activity/activity-hackety.svg
@@ -0,0 +1,870 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
+ xmlns:cc="http://creativecommons.org/ns#"
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:svg="http://www.w3.org/2000/svg"
+ xmlns="http://www.w3.org/2000/svg"
+ xmlns:xlink="http://www.w3.org/1999/xlink"
+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+ id="svg2"
+ version="1.1"
+ inkscape:version="0.48.0 r9654"
+ width="256"
+ height="256"
+ sodipodi:docname="logo.png">
+ <metadata
+ id="metadata8">
+ <rdf:RDF>
+ <cc:Work
+ rdf:about="">
+ <dc:format>image/svg+xml</dc:format>
+ <dc:type
+ rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+ <dc:title></dc:title>
+ </cc:Work>
+ </rdf:RDF>
+ </metadata>
+ <defs
+ id="defs6" />
+ <sodipodi:namedview
+ pagecolor="#ffffff"
+ bordercolor="#666666"
+ borderopacity="1"
+ objecttolerance="10"
+ gridtolerance="10"
+ guidetolerance="10"
+ inkscape:pageopacity="0"
+ inkscape:pageshadow="2"
+ inkscape:window-width="640"
+ inkscape:window-height="480"
+ id="namedview4"
+ showgrid="false"
+ inkscape:zoom="1.25"
+ inkscape:cx="128"
+ inkscape:cy="138.8"
+ inkscape:window-x="0"
+ inkscape:window-y="27"
+ inkscape:window-maximized="0"
+ inkscape:current-layer="svg2" />
+ <image
+ width="256"
+ height="256"
+ xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAABHNCSVQICAgIfAhkiAAAIABJREFU
+eJzsvXm4ZVV5Jv6uYU/n3KkKioJiEBQQQQGNyCwiIoOoyCSzSsQYE0knMUN3OsmT7n7S+XW000Fi
+RBAEFCLOJt2J6Z9zBkc0GHBghgKrCiiq6t57zt57rfV9/cdaa+99TpV4Sgqoou73QN179tnDWuvu
+b3q/YQlmxhIt0RLtnCSf7QEs0RIt0bNHSwJgiZZoJ6YlAbBES7QT05IAWKIl2olpSQAs0RLtxLQk
+AJZoiXZiWhIAS7REOzEtCYAlWqKdmJYEwBIt0U5MSwJgiZZoJ6YlAbBES7QT05IAWKIl2olpSQAs
+0RLtxLQkAJZoiXZiWhIAS7REOzEtCYAlWqKdmPQz8ZCbPnw1nLVgIugkgVQKAGBNDWsMlNZI0hxS
+Kpi6hKkrKKWg0xxJmsGaGqYuIaVGkqYQQsKaGs4ZpFkPOklgTA0wA8ww1gDM0EkKpRMIAMwEZgYR
+QUoJqZQfUziPiWBtjSTNIJWGNTWEEEiSDEwO1lo4ZyGlgk4SMDOcNf6nswAzkjRDkmZwzvnviMAA
+hJCQSoKJ4chChB4sDEAAgBDNWgkpIYSAQHsMQoDJwVkLIQWk0pBSNdfGpi5+jR0YDCGEvy8DTNR+
+BocHC0gpw5o4KKUAIUHOhUdKATA7ZyGFjNfmzFw656B1kmudlM6ZaWaeZ2IQuSkGFphomsjOS6kB
+AWidQCeZFAAxk5+1AOqyRF0OIIQAA9BpCqUSMBOEkAAYUipI6fUUMWEwvzGsDTdrA44r2azo2O/x
+a8bb3/3bT/Ftfm7RMyIApJAQSQJrjWcs0tBaQ6eZZ/qqRE0D6CRDkuVQWqMuhzBVCSKHNMuhVB9V
+OURdldBJGgSJRFUO4FyCLCtATLDGeCY0NUxdgYmgtD9XhHefiMBEkFoDzLCmhlReCFlTQTqCTlOQ
+c6irEkmaesFjJZw14VgGpRM4Z6FFAiJ/rnMOWZZDJCmcNf5ZTCAHSCmglYZzDkzkX14BMHOHmQkC
+EgwGhIQUIjC0hE7SsIYGShGkUpAI5zBDaw3nBIgcmAlgeGEigyBgAoBGuBBTIqUySkqQc7sJyeuk
+VMuI3B7EtE4CBympMutcCfCLldLrmKgH5r1NXT1grdlbSVU5Z4cA9hJCPMDO7g6pnmCiBWKTSJ3e
+y8zPI2e+wYx9pZQ/IHJ9KVVl6nK1ENgd4IeVTpYrpdczOSWVchzGLqVE07OKR1g6HOPOl11hEI5h
+VOgt0Sg9IwKAmSGkgNYpnLAgZ2GNg9YppNJI8x7qukRdDUHkkKQZ8t4UqnIIW5VgIqRZgSIcM3UF
+0hpJkiIv+qirIYaDBWRFH0mSwpgaOkkhlYOtaxC5RthIqcDwjOashVReoztr4cghSXM4a1GXw8Dk
+GnVdIUlSaJ1ASm99GFNDKw2tE8/kAJT2zDccLCDL+8F6MSByICI4B/88reGIwM75tRECrQaToKix
+mcBSeG0o2GvJIHRcuBZKN4xCzJBKQQjRPJOZlFLaMTOYaKWQYgDmWSL3Eqn00Jp6BYDDhRBDU1bL
+AfySEOIhcnYVM71ISHUvOfdiANIK/JSIDhBCriFn54SQuZXiEXJulVT6MSaaEVKmsOYRCLmKnPsJ
+U7kfKf1IzTQUENNCiNsB7C+V/ichxL4M8W0wekKqB521i0qptUz0MITYyIwHmGhGKLWGiTJn66p9
+q7ag6UeYX6BrKLTfL1GXxDPRE/AjH3w/pE69mckMZoIN5qrSCZROAADWVEG76dYUtzXqcggpFdK8
+8FowuARKaeikdQmsNcjyAkp7ayOavtbUICYkiTcxhRRgosAgHFwCDecs2Hlh0XUJlEpgTQ0IgSRY
+BlFgSCmhdesSAAxy5IVJkiHNOi4Bt2arkhIUXAKw1/BeGADNSywAKQSA4BaI9nUnotYlkNpbA94S
+yIWUJRFNWVMfCAFH1u3J4COEENpZuwJMx3mDgIiZDgVjIVgHU4AgZpJBKIXfvTne6FPualuvqbmZ
+g7ds4hijmyGC9g3HiIik0hrkXHB75ONg2gVC/jvAB0ip/w7glUKqr0splZDyS3U5qMnRl4XASgix
+FswKgGsWC1tyBYCuFFhyAUbpGbEAnHMgLgFOoZSGkApaC1gnvJkcmFMnGYSQ3nSvCCpJkSQZZF+j
+Hi4Gc98iTXOoYgpVuYiqKpEkSRAkCnVVQjmLNCs8EzvjTXtrvOWgPIMLISGlaISANbUXREHDS6X8
+dXUFpxzSNAuWQdclsHDOwASXQCepxwnCvE1dgsiPJUkzGGMAdiBmuPDia+2tDyIHKT020nUJiBkC
+wW+GaBhJSgWRSMFMTM6CwS9gIfeAwLSr6v2dNSdBCGlNvQxMx0U/nZmnpRQgR15Jeqadin44mKVn
+aA8TIFgTQkqA2Y9HCLAj71qEv69SCuSsZ2YGXMAVrHXQWoGJQUyQUsFaK6PVJYQXxsy0i5AS5MyL
+lVIwpjpbSQUy9XFSKgchLmWiBwB+BJB3gPleAP8MwEKIe0dcgZ/B/Eu0OT0jAkApBedcMN2DTy4l
+EpmCpIQNfrUHi1JI5RnZ1mUADlMU/Wlv/lcliAhp5t2Euiph6hpKE7ROkGYF6rpEOVhAlveQBItB
+KQUpcxhTg6phAB09GOhfQK+lVXAJrPEWhE4zOGdRDgfI8iKMrUKSJN4lEBLWepdAKgWlNFhIMCyU
+ECDnUA4XkWYF0jTzlonz4COIACGD7y47AF4QIQ1Y2L7EQkowQ4H5BUKInBkHWmuOgjUvYWbprDkE
+4D3AbJxziccAJJho2guQ1iWL9xZCBoDTWxrWWmitPXYR1p/JQSjtmdoYJEUW7kVQKvXgJgSkVHDk
+kAeLKs290HLWQEgFZ2voLIMzpgForbNQWsHWBjrRsMa7ZhGAJHJKCLE7M3YPa/MaAAmAfwPwYzC+
+BIgbRhat+TnuByxRl54RAaCTDJAGzhg4U/uXRqeQUkIpDYQXxBpvDWidIMsLGFPDhvM5SZEGBjRV
+iSpgBWlWwCkvQJgZSmlkWQHnDMrBAtK8CH69B+SyzGti7+N7v15I2bgFjggiRAacs7Cmgk48flCV
+AyRphiz3gsRRsFykf6HJOYAZMmADUcMROdTlAJSkSLPcM4SzUfPBUQC74nGmDi4ACKDw3hrtag0d
+DuBYIrebs+b51tpXCQBMVDJTDjQmeRLRc6agtYmbqIFH8jUEBMhZ70pJCQEgyXsN8p4kaQDPBJIk
+aaMoWoOsv46ZQc5CJQmcdVBaIWAOEFKBrIXSGtaaxoJxxkAlCUxdNVafnPLWV5L7CJEfu/OCMv4u
+JZh4WioFZjpSCHkogDcCvC+EeC9YLI45/mgtgiVBME7PEAhIHW1pQEEDap1AKO1fqMRrR7IOJmid
+JAB3zlQeeSdqjvmIwBDkHNKsQF70UVXD5sWRUiPJFaqqhLUGedGHcA7WGs/cwsLUNSgwMYT0JjgF
+E93UkDoBAr4gpUKaBgvCWSRJBkcUIgIJkjT1oUJr4FzlXQKtQeQ808F5oeFca5kEnIKIQIIbl4CY
+vNkNXgmI5xHR7kTmFUTuVCZeReQScnZXHygIoKEUeTTdAQTBQ4GRPS4RsRalFbIi8fOVPtSplGpC
+oh5/JIgAKHYpChIGoJMUEN7CixZFknohoZQChWuTzFtRaZY1rofSGmBA9z1+kvd6HaEhYWsDKQXq
+ugIgYKoSDMBUw/C9FxzkXCGVLAD8Brzn9OcAFsNoG4snfl6iUXpGBAARQcIj4EkwmclaGKKOSyA8
+Y0oLsga2rqC019Aq5gdUJRwR0jTzEYFqCGsqcIjBF0U/uA4VpE6gtLcGTF1isLAJeW8KaZqhDnkG
+UinvlrhhAzoKqSCJwKCOS+DdCIYXFs5ZlOUQWZZDJqnPW9DJiJAzdeXHrhJAWO/FBw1fDuaRNu6J
+AcO/pDIEBaWQBzsyxzNwiLP2Bc6aw5hpzwisIdwrhrWEjLkA3tx1jhqAFMweSwmCU0iJNMsawE40
++QT+b+VBUQUSBCEArbzm1lpDKR8RKYocMZ6fZSnqukaidePqKa1gatMIBnIOUvVQ1wZJEgBf6yCl
+wHBYIUsTDMuysTBccPGICEUQEEW/H/I2ZkJkhVAOFyGlhCkHIOZlAnwegE8DuD3MpgP8LzH/luiZ
+EQDOAtAexhKe0Sn4g876MF2SpBBSecYMTOSZ2wuICORZa1AFpD7LClilYaohyFlQSBxSWqMqh8E/
+1d5NCIyXpDmyLA+JQ/5Fs8a0+QXRJWABYoJzDrLjEpi6CuayQlUNobU3601dg5zzAKH01oC1JkQD
+UkihPCMFAK7quATSyTlHTjhrj3XWHM3MryNnVzhne0w8F5UwAw3C7hk+ggTsGVl6H1zpxLsaIeyZ
+ZpkXLdT6/t5a8L8rKWGtRZKkSBLtMZYkgdYelFRaI9UK1jkoNQMlPTiZRPOfCFL4EKZUCiACdcxt
+GYRNABnB7HMiyBFmpvoAGNPTfQghYKxFojXKsgKEwGBxiDzPsHHTJgjpcx6i9ZAXPdQhVwR1BWLe
+T0CsGH37xt2BJerSMyMAgqkrA/jnk1qSxvdn52C4akKCQsoQmxcBIfcZhDrNIKSCrUuYugQHHECp
+vk8cqj1AmAQLoSwXA4bAPttQFt6KcA550fPx9PDiO2lbYRSiBEoqEELiEPvwJKS3YKTSjUvghhZp
+loODS+DdlxRSCFhnR5KXfKadA6SANeZ5RLSbUvrYqhq+3Nn6DHY0y0yOiRVC6K9ryjI5iBgtIILO
+cqTBepFKe6skoO4Nso+Q/CPhwVftgbasyJEmOrhoGkVRNGCgVj5fQkkBIoZUEmkT9pNNiFVICY4Y
+Svxea4iY8RdGIIVux8Q+o0cGt8O7KwwpBVKXQCmJLPPWy1Qvh3MEgLF+/ROQWje4iqlrlIMFUMB3
+ALEAwcvbNy/gKCELcMkK2JyeEQEQQ0lsAQ4xd4bPXJNSenDQmRFmjZosms3RzFZKQ4XEIVtXICak
+aY6smEJdDWFN2SYTFVOwdQVr6xYgzAuYuvIuQeGTdUxdxbAaTF01SUCeqZT34QN+4FOBQ6ahc41L
+UJVDpFneZOspIqjECzNrDJytAZ1AKSWI6WAJdQjBHlOVg0OZ3KvIkRAxE1AIFTW1J4YxFkqFNZGq
+wUjSLPfou5KAkD65UHhmZ2ak0Q1QEmmaIEkTKCkhpUSWJg1zM7F3d4LWj/F7ANBagmJmXgz/B8DQ
+/3nb5JuYdi0Qsxy9EIhan5uzmlcDCIKmEQodsUVEGFYVrPNhYSIHawyq4QDVcAHkXHAxFADcBuD7
+myUJjaQKL1GXnpkoQJr7ZBznEP8IAiqYrrJBoLsugQ7WgM8bkCBr4UK+f9S+zhmYukYVQMM0L+Cs
+Rl0NUZWuCRdK5c31WIsQ3YnhYB5p0KDWWYAIWd6DDaCjThIo7RONhBINOMfwfjU5C2Nal6CuK2il
+GoCPQgZhmuewxiTWVC92Qp5O5F5qTf1CW1cHA97194wQQ4Bdf97H07OiF6yI1JvqWeYXt6PYvNUi
+4KxDv1cgy1Io5UN7eZZ1rImYSyAQ0/ylEp7J/RkNpiAAUOCbkMzfHG/ZiQHuMHUbvWgYXgg09+9m
+5zaRDtHeixlw1qE2BgsLA5R1DWd95mM9HKIcLsDWNZw1PgFK6QrAnQD+EgJ3BV+jfdBmKcJLFOmZ
+yQPQnomdqb1Jz9abiCrxSTPBrxNCBgFAcHbUGhCJgHCydQl0YM5QS2CqEkzeV8+KKdTDAUw19G5C
+lqMoplCWg+DDe4BQBcHgXYI+mByMtT4hSZoQOmRvzksJheAShKw/pb2FYI23DGLOgXNDZFkBBqOu
+ql6SJIc5a97knDvU1ouHknMrmTlMXTTvp39vI8jnEf4i7yEr+h2ryGvobo6AVhpZkiBJvEWVphp5
+lnm8QEgfRpMSSskWREQUAv7Z1DCI6OT8daLpTXKi/57RjtsfQ+dzDDe27wAxh0zFkMkY2T+mNzZj
+YDjrsGlhEWVZoayqJptzcWETTDUM2aIKUmkHYAOAfwHwCYC/At5CRmC3mGCJRugZAgEdhBBI0hxC
+BlPfOf/SK+XBKsAX7Ig0pM7WIKrGXAJv5pKtYUzrEqRFr8UF2EEnqU8SCpWFRIQky5EXfZi69GE9
+IiiVIMt6MKbC4sImFKGWIIb9ZCph6xqmGkInGVR0CRw1pqgM5rizBs56EJCIUJaDPM3yw5yzp1fV
+4Ayybl9n6zkA0jN9q42cI4B9cY8IZn6S5shyX+nYuAONJhahtkIhzzL0e4X/Y+oQthMSSnYsiVB9
+2dYYbJ4vJ5tMQIRoGo9o8JFIWtD23fBadLMj0MvNgOPvMasx3qudE4Obsc0vLGKwOMSwrLxcIMZg
+cROqcgBT+hCgUjouxt0APgrgEwB+3M5mPB14yQX4WSS6edpL9NTpY9ddk6gkOcyY+jxbVycw0T6m
+rnYTQsjIGLF6z6fmkk8cShKoJEUaIxnJqGxmYiSJRpqmkBLI8xxFnvlKS+l9f58X4NUwdzztrhbn
+yIQAHlq9Gm++8FIsDgZPOqd+r4e3//LbcOGbz0FRFJvxU1f7j2bg/WxW9L8ziLzWr6saC4MhBsMS
+zlEAVIcYLM6HgrBgyvv/Kmb+jBDi5suveM/fbu3faIla4b0kALYRfez6a6VS6hBr7bmmrl4JppfU
+VTknPJrW5NIDaDLxkjSFSjKPEwQAMZa/ypDIAwFkaYo8T5EmCZIkQZokiHiBVKM9XcbqdFotL7oa
+Gnhw9cM474JLfi7zd+ncs8/C7/7WFciyfPTmXUGALetb0SDx/kJiBphhTI1hWWNxcQjrHFywrIaD
+BZhy2PRliBaCTrNvOVP/LYC/uvyK9zwx8eCXaIQagPdZHsdzgj72kQ+/AETnlMPFY5n5SFOVuwgh
+VEynhZDexw0a3wOWPWR54V0LHcuUuYO0M/q9AkUvRx4wCM/sPm1YBlAtMvWIcTtm6XYFAgCsWbsO
+7/y1K7aK+QHgE5/6NIQAfvs/XBGSgYJSHhMuXXcgDoe5Y/nANyEphyXKqkZZ1ahD7UVdlhgOFmDr
+snGNICSUTh5QOvlulhfXG6W/UZeDDVs1+CXaIi0JgKdAH/3wh1ZJrV9rqvIYcvY0U9e7A6wbLD0w
+hm/Q4QHKNMuhQ/hOhqQnb9YGxoZAr58jSzP0im7Voo+Tx3Rf6iDdI2g8gFGUPujjBnln/Nf//j/w
+0OqHf6E53/rJT2Oq38evvuOXQwhyRLE3YN+WXG4Kbo9zDoPhEAuLQ1SVT922xtduVOVi0z0KQkAl
+yYKQ6g6dZJ8E+P8ncne+5R3vqn+hwT8D9EyU1z8V2iy1e8kF2Hr62Ec+PMNEx1Tl4Hgp1RtNVR4I
+cAIEVD+auOAmASrJPKiXZj4lNyb5COF7AyilUOQZ0jRFkcfahLH4OjphNHS1f9fjj587AF44CgDf
++NZ38CvvevdTXoNPffxjOOAFL2jDe2O2fxNTiEIwJDGVlcHiYID5+QVvGYFRDhZRDhZ91CYwkJIK
+kHJNkmZfElJeJ6X87sWXvWO71/o7igBYcgF+Abrlxo/kSqll5WDxdGK6mJ072liTtfH10GMvlNEq
+pZH0CuR5H0mWhdx42ZjOWilopdCf6iHRGlma+JwD2ekJ2A0RRtYO2j/G18GjGr4LwAExfu8//fXV
+H9oma+FC78BuGT43+QCt1eErBQmWHIZlhY0b52FDLwRb1RgOF1AuzocIAWKmKEml70iz/EoI8cWL
+L7v8vm0y6CXajJYEwAR0y43XywsufRuVg/kT0zQ/09TDc5x1y0XIy49alkPtQEzcSbNeAPd0W+MP
+INEKSaKR5xmKzGt9hs/J7/rrDIboJNi0bgU3rkDLeNHHbq0F2WhfLwy++OWv4Pv/9oNttzCNmd+O
+x7cfFC3mQYS6rjG/OEBZVrAhc8/UNRY3bWjyPbwwE1ZIOUzS7Eoi9w8X//I7/mnbDXaJtkRLAmAC
+ssa86GPXX/vqqhxcOFjYeFRkdilVYDBqat99IlI/AHxtB2QASLRGojXSVKPIM4+mBzcgJtZ2fXug
+TcBpzP2Oxh1xEETL6BFMjBl84STccNPHtum6tECff1ZM9PHz8GsyKCssLAxQ1TWcI5A1GA4WUQ0X
+4YzxYw1pxUrrRZ1m/0lK9S9vuexd37/1Yzep8y66xG3TQf9itB+Awzuft9RO/2EA3wVgnpERbSNa
+wgCehD5+0w3TVTk43Dn3ZmZ6qzN1SsSJlC30HX0pKSWSgOwnad5k7HlUX0BrjX4vR5amSLO0Od41
+lSPFbLsuQ42b9tH3bnLzx1N7xpD/r3zt6/iN3/qdbbY2t958Iw564YF+bE2RUBBiRCBH2LSwgOGw
+8kCfAExVY3F+g6/PMGZkjZgZUiUbhcDtOs3+VevkUSHVF01d2kvf/s4f3HzDdcmFb7ns2WCu4wF8
+GYD6eScCuALAB5l5uxUC4xjAkgDYAl33gSuVECiUTi5miHPrcvHFYN7Nfxt0bCemn+YFsryHrOj5
+UlXAa+ggJGbnppFnGbKQjx/77G8OGI0y8ZZSazdL7kFr7jd5+p1QXEy2ufitb8cdd965zdbo1ptv
+xAsPPKDBKhqgkYG6rrE4GGLDxvkwbsLChid8Nl9o7OubpdDYu+dnI4QYCKAnlPq+1OkTUsqva538
+g5Tq8Qve8raffPyjN4o3X3zpM4G2HQ/ga1t5zRXM/P6nYzDbgpZAwAko7009f7i46QxXle8AcNDI
+xhoc49q+o1GS5ehNzUCnqb84ZOM5azE3N4M0TTA11ffVdsHHp8DUo8zcgnoR9GsjaaLl+BGKOP9Y
+kU4nMQcAvvyVr25T5m+pdVM41PmXwyEWBkMMh76DD5kaC5s2NIU7SZpBp5lvExYEG1mD2HlJKQ1r
+bU8pCarqw5VzhplPtEqfrdPs32768NW3Syk/fOvNH+XzLrz48adhUpEOwdYzPwBcKYRYy8y3busB
+PR20ZAGM0UeuvuooIdUfWFOf6KzpN3520NgUWlbpJEV/ei4022hRe2bC1NQUslRjdmYGse04sKUM
+OW78d39tJ5MuHG6LdNoU3uavFS2Ezs8ICsb7VlWNt779ndtcAGxmATDDOov16zdgYXHYbEZiQhVo
+7IWgQj2/7x0YdmCSEiYU/ZTDBQgh/Y5BUsHWdShXJggpjVQqkUp/o+hNfYqIHrrobW//+DadmKeV
+8NWFy3/eiU9CxzLzv2yj8WwzWrIAtkA3ffhDQkqx0hpzgqnKN0DgDKbQ+K7j1wICSink/Rnv62dZ
+438zE/IsR5oqzM5MhWansa9/S92YfIwidE33LtjX6PQY/B9L/olbjDXHWTTMH7//+j/989Ok/TEq
+kIKwmV8ceIEXvvCtvfxAszwHOQKDkecFqqpErz8FYwyyPPf9Ens9nyxkZ1CVQ1DuUJdDCA3UVZUI
+IWDM8ChnzEuVTh664Zq/fnmSZJ+AwF0XvuWybZUa/Gd4aswPAEfAVylu17QkAABYW6dK6ncS0xuY
+6RD2L2y3QYdP2AlVhr49uF+6GLvOswxT031kaeLj/UKOaPsRT7cD7Y8weUeLt5aH/xkBP8/ko5aC
+Pz8ChO33ZV3h1k9++mlYMWCX5ctHcg9EEI5aKbjQjFSFpiJFL0eaes2vlUaS+P0ChJiGjea/NbDG
+N2StawOV+K3jBODbxpdDJJlBNVgIloPNiGh/Z+t3WFO9Sev00x/+q7+4lZz74eVXvGfxKUztCABv
+3SaLtAPQTi8Abrz26heYqjyDYH6TiWbirjdNeSy8uZQVfc/8nWo4Ae/vz83OIE008jyG9cZN/min
++6McAMLGvw/au5sD0G0I1I7DfxrRvDEPoE0dan5+85vfxje+9e1tvGKe5ubmOm6Hf2KaaMzOTgPs
+OwUTE7IkQZomzZyJCVpppGnoAFxkwboqIABUxkAKYGEwhBQSg8EQOtHNhqxFfxrDxXmoxIat5GiG
+azND1v4OgFcwcPU1V77vXy6/4rcf+AWn9mfbZoV2DNppBcANH/qAVFof6Zx7NzEdD6KZmNACeKaP
+9f1JXnigL0katctM6Pd6SFONqX6BuD/fuFbsgnjRk/f39+MQHa3efEbrAjTgIDDG5q2AGQ0XeqrK
+Ep/89Ge3+boBwCknn9TgGl1wUkqF2elpcCjgibv+qJALwWBIqGZOMtT1S6WbHYeykBqdhO3WZqb6
+WBgMQMRYCO5Fkqa+T4PpoxwuwpTDKBxPEFI+H8Cnr7nyvZ+7/Ir3fHkrp3Y+gFc/1fXZkWinFQDW
+VHsTuf/MRMcwuTk0WpmB8DImWYre1ByyomgTeoTXzkWRY2522jfhkKopgOkmx4wwdaOpuzQW2mN0
+rhyHC1uroGmsgY5h0X0WgNu+93189etPTyLdiw46KKTsoomKALHnn4BvduTn4Hdgpk7MI4yzIxx9
+XoP/XgXBIsM+kgAwPTUF5yymegUWBgM4SxiGHgp50cdgYZPv7GQMyJq9pVJXAOKwa65838sBfOLy
+K377/gmmNQvgT5/ayux4tNMJgBuv/WDqrH25NdVlTPXpRGFDzlCc4/1/AZ3m6PWnkIT22pESrbFs
+2SwSKZGkSVPNF+TGWGOMTqccgSatd9yH76YDxJh/cyJEIzgo+PojVkC4WWNxMKMqK9z66c88LesH
+AAe/6CCMuyJREHXdghgu7QKc0QYan3dL3AqLAKIKFhBCQwqBWT0NayyKIoexFpvmFzCzbDnK4RCm
+qlCVi6iHAyGVehUD+wvglddc+d73Xn7Fe776c6b1bviMv52KtpTS+JylGz70gRlAXASBPwXz+bE9
+dQT7Yr+8rOhhamYOWdH3/faF92kTrTHd76FX5EjzbMTk98QdLT5Wwdee0TBL9OGjWPBoeqvSu8De
+SHFQuFdMJOQgKGJ14U/uuhtf+vLPe99/cVq1ahWAtlkoh3k3m5pyK9w6mQwI+f6A6DYgbdekdcE2
+h04jrqKkglQSRZ5hZqqPXXdZhiJLkRcF8l4f03O7oDczF0MpezHzSYCyEx0CAAAgAElEQVT4k2uu
+fN9Ln2RKBwL4o22wNDsc7VQCgJw9zVnza66uj2Hmvjer4w7BgFQaeX8avelZv0U4/HvkrMN0v4fl
+y2YxOzuD6C40pjva17wb9etq+tanR+eXQIFxu/hB93xu7jWaPRgZLv5PDFhj8cnPfG6r1mXfffbB
+bitW/PwTA83MTHkfPsxViohuxIah4xq+FYwjgiJOv8lybH/GvCsK1ZW+ypJBoZfksKpCQxMBYywE
++56SWieYnluO2eW7I8lyKJ0UzHwCwH94zZXvXfUzpvSb8JuN7nS0U7gA1171FymTO9Q59y5m+0vg
+NgU1vpBSShT96dB+O2lMWa0UijzF9HTfdyeOKD8DFO4/AtzFFGG04b2ujx+fK8YEgtf+m4+9jeyj
+DRV27t/FAQQDP/rJT/DZz0/eJm/vvfbCn1/5fnz2k7fiYx+drFgoS1O0pb+tBTJSvzAGerbzaf/t
+zj/WNDTAJwMuVBM655otw4Zlhbo2zXP9Nm+6aSWmlAJZglQS07O7YH7jem/ZMb8JAn8K4JGx6RwD
+4J0TL9hzjJ7zAuCmD1/dJ6KjTTX8dSb3sobLRNgtFwSlNfLeFPozs8GX969holMsXzYDJSW00oHR
+RLy8UXHMnb7+Yy98LHVtP/vzRtk4fhmHJpqS35H6eg4WSwdriJof8Dn/t/zNJ7Zqfc4+5xysWLES
+3/7mNyc6/5KLLkCWZkB3HeLQu0INW/Dzx0DD0cSnNm7inNf4xhhUdY2yMkDQ+jEvwxm/9VrcLSlu
+BccQcKaCTnM4Y0JGIgHAwmbSCMgB/MetWa+toMeepvtuU3pOC4AbrvmAIOdOJ6K3M/Nr/dHITN7v
+lDpB3ptG0ev7ryNHMWNmpo8sS33n3SZuP97aevStas5pOKMF8eIDugU040h6zKsfNwe6zx4RHNwe
+f2j1Q/jb//P3E6/PypW74ZwLLsb6xx/HT+66e6Jr9ly1h0/N7UypLTsWnbl0xtiApKO+T+MyhDkw
+AtMbh8FggKryDUGNtc0OQOQcyuGi31IO8Hs3WOubriJsRKsUqsECAEBKycwshJBfgC/Z7dKJAM6Y
+cLm2lm5/mu67Tek5KwCu++srEyZ6FTPe7Wx9THxj4z50TASdpuhNzSLNiuYFEkIgLwr0C99vX0rV
++Lrj7N7AVR2mbRDwDmNGP2C8mq/xeZsbebO6za9vz4+mc4wExGBB9JkhgM9+bus6ZL/+9W/Afvsf
+iLt+PDlmsN9++46UKI+Cne0aNLweLKUWCoyH2Zv4TCDy3YGtIywuDjEsy0Y6mqqEqf0+EC7sGu2c
+C2FI0TRgEUIMmZFrre8l4nukVC+E3ypslZRikZn/6+VXvKdr/i8H8CdbtWBbR/c/jffeZvScFQBg
+fgkRXU7WHkVEqkmpDUi/ShL0pudazQ/PxP2iwGyI72utG9M0nBF+bsFZj8d47JgYOxaZNgoV7n4v
+Ri+PLNYpBR65XUTbAfz0p2vwka1o+LF82TK8/k1nI0kS/PhHP5z4un322msUg+h4M+24WpdFCIA6
+nn/sXhQ3AynLGsZaDAbDZhcgdoSqKlEOFmBNBUCAnIU1Fsr/XQYAcgjM5/n018G8BkKwAH+JmX9C
+VZkDYi0EFsDYjxk/2EJ68Ovg036fLtpuewJ06TknAK658n2Swbs4Zy8UQp7CzD59T0jEBB+pFIqp
+WeRF0fItMRKt0OvlSNOkY7J2X/eoyUez/EYaYnQ4OJroUbl3tflIrkA40AX1/HXREtjMqB65lhn4
++Cc+1Qi3Seiss87CS19+JKqyxPe+M3m68NT0FFpvPcyb209dAdWNiAjEUKGPuNTGoKr93n912PPR
+Gb+T8nAw73eU5jZ6ADDSLF0rVXK30notAz+RUv1dlhf3MPPU+Ze85W4AuOFDH9Dv+I3fsZ0hr93C
+NPYA8P9NPOmtpz/GkgB4toh3ExDngnEmM814nvUpqWBAZxmyou81f0DzpZJIEoWpqR6KIg9pvbGz
+LzCCWjfgXDwi2s9jzN/N0R/V7P7aUbbZ3KqI/nO3zx+PX8vAhg0bcPPHJwf/+r0eXv3aUzE9PYPH
+H3sUP/3pTye67rhjjoZWurPHn2hCdd0QhRiJDvjxEnuNX5V+V+XFwRBVWYHBsFXVaHxn6iAERcw2
+ZCHVQ0mafUZKdbuQ8iHn7I/fevmvPrilMb7lHe+yWzo+Rm+FFwJPF32bmbeHVmY/l55TAuDa979v
+D2Y+BeBfA/ACACOmqUo08mIKea+HuJW2lArT/R6yLEGe+f3/Ig7YDRW2DI0WrR8z2XmMqVuwr2Xb
+UXO/NZXb82JMLzAQ88i5XigIdIHC//P3/4iyLCdep9NOOw0ve8XRgBB44on1WL9+/UTXHXLIwej3
+ek3UYTzi0cUDGsuKffekujYoy8ozflV7wK/y+y4OBwtwzgN9UkporY2AWBBK3a+Uvg5CfltpdZdg
+2Isuu3zTxBPdMh2Ipz/l956n+f7bjJ4TAuDGaz6ohMDe1tpTnKnOY+b9uxqVyYU4/8xIXj8TIctz
+9PsFEq2glEbc+ba5tsPzrQvQPjua892quHGUPybwdMGxxivuYAJR03efO1JQFEUMt08p6xrXXn/D
+xGslhcDr3ngmVuy2EgLA97/7LaxZu26ia/d//n7t2EeEY2e94rzZNwYF+/Ddxo0LKCsP7pm6Rl0N
+US7O+3LgsABplq0WQlZS6+9kee9vhJDfOf+St6y+6boP6Yvfdvkkmn0S+s2tOXm/fffFr77rXfjc
+Zz+Lf/6Xicv757d+WM8OPScEgHNmLynVuWA6GxCHM1h1jHZfZdabQtGfCnv0+e+01pjq5UiSxBeh
+BCbugvIjmanoMGbjp3dadAObof3xmjbLLRwDj2IG41YA2vsJHju/A7p9/m//Do9PqMEB4JxzzsFR
+x72qSbl94J57YO1kvDUzO9vMnTqCrB0xw0dAQhzfOiwsLGJxMISxDkyEqhxguLAJtvab+8SOwEKq
+DTrN/03r5DM6SW9888WXNj70JZe9Y1sx/8uwlUk/l1z6Flx42a/gG//6r1tz2cJWjepZpB1eAHzk
+6qv6zDjRmvp8ZnoRMWWjiTeMJO+hNzXThPoYgJYK01M99Pv9qKYbs7pjpW8BCAxarmFmMarVg+QY
+BQhbFLz5hUXHPelaHfEZrTvQIu6jwqaqa1x9zXUTr5VSCq859XTstnL35v7rN0wuPPbYfSWaKaIV
+grIDiTgiH6uv/CYgxnnGr4dDLC5sbMJ4IXQX78QAZ9bUhwkhv17X8zff/JHrblZaz6R5/mln7a7n
+nH/hfZ+45WPq3Asueiq+9VYBf4cfdhjOveStuO/un+CHP5y4q9J/B7B1my4+i7TD1wIwcCQzncpE
+LyLnii5KzgwkWYGpmTko7VO9fVcaidmZKUxPT/ldeIJm3TyLr3tMbOnHSMFLC9KJkeu6+EEE7lrl
+P7qRt+jeo/Os7vZfUdv+3f/+ezz62OQJZ6efdiqOOu54xO3LhmWJRyc0/wFg112Wj7ks7bxjqm5V
+Vnj8iQ3YsHEBtbVgZgwWFrBpw2M+j58YaZaBKexd4ByYSVhjCrJm98H8hj8z5eCchY2P37q4acN/
+eWLdmq+Ww8Hv3PThD/0eE73olhs/cgAA3PrRG7f23T0DwGu25oKLLrkEe+6xCvfdezfuu+/+SS97
+cEcBAIEd2AK4/oNXCQHsw0xvZKLjiCkXoaTXF/cwdJKg6E1B66RhJq0UZqb76BVF06hCNP+gydEf
+EwVow3VjoS6MgoEjQbrAJKPJQ6J1L8KRkQiB6IQJuesSdPEHRlUbfOozkzf80FrjhFe/Bqv23AcE
+ghIKqx96AA8/vHqi608+6URkWdaOe2zCzjqUVY2Nm+ZhAuNHAZFmGZJ0dxA5SCG83w+/vZgLv9u6
+AqTUvjxbwlqrgWof59w+tq4OF1JWdTn45STLv/bR6655yDl36803XPfEhW+5bM2ES/DnE54HADjt
+1FPxujeeDak05jfNo6yqSS+9b2ue82zTDisAAOxC4FPZ8QlMbs/ITJH5kzRD3ptGWhSNqZlojdnZ
+GfSLHFLJlhkDo8aGIF30vluK2+bnxyGMigrR5X6MgoVbCvNtbgGMugUjpcEd5hcQ+MY3vok77pw8
+geeVxx+HV73mtZBShh18GQ8/cD/uuffeia7fe++9oLVuo31hPF5QUdj3bxNYiKYXYJIkqOsaxfQ0
+6tpAKgkignYErVXYHER7PEBImNonAlXDgf9ZLkInKeqqFBIir6vyAGvqA5TWBhCXZEX/bz563TVr
+Ab764sve8WQ7Br8bwEGTrlWSJDj3ggux+557wTiLdWvG64eelJYEwNNN13/wqoTBvySYzyV2B7Q5
+5l7rKCmR96aQ9/pt6yoAU1NT6BfZyLHoKrS+fGt3N/vyddD7rhCIKbkNKDcC/rXFPGM43wgW0CTN
+cVeAjAkFMJg7nwXw6c9+fuL1StMUJ518CvZ9/v7tnCGxuLiIweJk7uqB+++PKKAAdHIkPEkpseuu
+u0BJn7ufaI3aWmilwOwLfLSSGJQVkkSjqmokyQyGgyFkUaAsS2R5BmsM0iwHM6E3PQNb1yAiDIeL
+SDNGNRyAnEvI0QuI3B8opdZJqY786PXXfFwK+WNrzT2Xvv2dXRN8Jbay1v+CCy7ACa85FUIILMxv
+wo/uvGNrLt9hIgDADioAABwsIF7vnD2KiXre3PbJPlIqpEUPaV5AqgD6kW/Z3e/laGzsMR8dGEP0
+uT2tfddbNwDoaOpuxV689RjIN5r004JmAEYqBkeAyDE3IWrdb3/3Nnzla1+feLEOPfQlOOmU06FD
+2awQApYI69aumdi03WPVHs0cY8izNVAker3CCy7puwNLKf0uScxe8zvfGzANG6j0QxZmkWdQUqKs
+cmitMRgOIYTAcFBBKuXrAsDIigLkHPLeNKpyAGdqmLqCY97NsT3bOXeWTpLPA+L6a6/6i+++/dd/
+M4Ijvw5g10nXas9Vq/D6s87G3LJlEBDY9MQG3Hf//ZNe/gXsQBEAYAcUAB+5+qoZZj6NiE5loh4Q
+fVLPbTpNkBV9KK1H0m3nZqegtW66/XZZttXMXfM+fB/N7vC2c5dzm+vQmusjAqPzHUYtiia0F5ib
+wxxalH9zoQH2wuzmWybfdEYphVef9Brsf+BBTS6CFBKlqfCjO++AmzAEuHxuDt08iBgKjEJSQjTR
+lIitSNEKV6mUtzukbABRQEBrf16R55BSeIsBjJnpKdS1wcx0D4uDIZRU2LBxI4peH3VRoK4qkHMY
+DjbBGZM7U4OcfbNU6iAmuuWa97/vc5e/+7cJW1nue9bZZ+PYV54IAd/voaorPLx6vIjwZ9IdzLxk
+ATxddP0Hr+oz41VMfDo79/ygepuccZ1oZEUfaZ6P+OpzczPIO1hAF8wbkQAYBe8azTvim7c2QxfZ
+b/L4O/hAU7w7JkRGv0VnTPH76Pij+Rzvf/u/34EvfvkrE6/Ziw46CCefdkbYi88FOeVN6YcefHAz
+U/5n0ezMzCio2RGucX5NM5AoJDa7S7c02s/eYxLwdfuIwsP/PdMkAQSQZSnIOfSKDFVtUJsMw2EJ
+Rw5Z0cNgYR7kDMrBIsi6w4QU+zDzwbssX7b88fVPTLKpJwDg0Je8GGe86VwURR+OHCQY6x9/FJvm
+J+bp70964vZCO5QAALAfM11A5F5M5ER3SzMpBdK8hyzvdVJ4Cb1egempHpRs21W1GXuttu6CbN7M
+FWNCYFRWNJH+cN5oxiBa5F+gydwbBQfF2D2A8TtEhLKLK/zt/5683h8Ajjv+OLz0iKNAYCgJuGBu
+lGWJTZsmy6o95eSTfBEQt/MSQXpGa6ALqEbhwGgPNCveBVHF+PrHKwSkbJugMnzSlpQeWHTOYnam
+j03zg2aXYWsM0ryHxfkNMNVw2d33PXjp4+u3bqOgM17/BvzSEa9AFNbMhO99+5sYDoeT3mJiU2F7
+oR1GAFz/wav2BvAGJjqRieZ8k0huQDmd5siLPlTip8TMSLTG3OxMkwPQetTAaMovo5vG29T2j+mw
+7p57TcZgBAO7PNt5RleAROtCoPX7Y0OMFpYQIwKi6wbc/8CD+MSnJt/pZ++99sKbzrsASkoQ22D+
++54Cjz66DoPBZBvoHHTQQX6rs2YFR92cOO8GEwnisp1z1xLqWEwda6fJluzctrvWgC/aAhMg/N9z
+2dw0yrLCsmWzeHz9BgylhNK7Yn7DevzfL31l4nUCgGOOPhpnnncBtE5gnW0yRlc/+ODWVFluqfJw
+u6YdQgB85OqrNDNOJHJnErk5/0a0WlMqjaI/PdLLjx1hdvkyZGnaaKtI8aVsD4x9Fxlz5CX258Ve
+eKM5AdwIhHiPLnVdinhCm9TTxQDGzeaulQB87vN/N/GaSSlx+hln4KWvOAbEBMlxzv4Jd//oTqx+
+eDKFtevy5Y046tg2rRPTCLU4amBkMVpjphFucU2iQNoy+XVt+gWy359RCt/+jJmQJH6b8UQLrF7/
+BL5722346te+vlX7Ifb7PZx7/gXY/8CD4dg1g2cmbNi4cVIBcA+AydMqtxPa7gXA9VdflTHj5QC/
+gckdCubMM7l/s6QKqH+WQQjZ6J/Z2Sn0+wUgRMvsXQbbHAFs/Pf2fWw91qbvH0et3ZrCgiM41u0T
+gBY4REebde7cbfTBQOdF64COgc3WPrYOH7tl8o1wV+62AmeddwESpUDsoGVXSwMPPXA/NmzYONG9
+dt99ZcdH2oJVxG3Tj66Qi59a74ZHzhl3fdo8yDGhwAzrCFL6DkBggnWEwaDE/Q88gG9869v4hy/8
+Ix54cIsVwj+XjjryKJz+xrMAMKz1M9FS47H1j+PRdRMr9e8CmKyuejui7V4AKKVXkLNnOueOIKJM
+oAHVIYRAmhXIi37YSQYAE9Isw/T0VNPFdwSQiozaYU4AY9q39UW58zz/TcsEI4VCY2Y7MGomM7oC
+pgXPhABWP/wI/uf/ej82bNiA79z2vc3W4GUvPRwvO/wwVPWT5bqM0imnnIrDf+kIEBNUzJDk1q8e
+lqVnpglotxUrOozcujV+xu3vsSaA2f8eAUav5UebiHAjGKPFEH5jLwhjUhaRH2NdG9S1gSNCWVb4
+zm234Qtf+L/4p8kr9LZIs7MzOP+SS7Fy5R6w5MCWAOVLxR+87x488MDEQmWHY35gOxcA133gLxU5
+t5yBV5B1m/V0l0ojK3pIEh9bZjCU0pie6iNN0hFwqmHbyKiNn9/xScN9R31/bst0wz+NMtyCxTCq
+1eL90EiXcczg4YcfwdsufyfWPklO/m3f+z5u+97kAPPM9DRe/6az/V6GAOQI6wrU1mDThKbtCw88
+ACt2WxE08hh6IjqTgncJol/fTVyKcx4pmQ5L0AgG9n0aY8cgRwRrDYiBhYUhjPGtw779ne/is5/7
+PO784eRZkE9Gp5xyCl5/1nlgAM5Y31RUevxnzSOr8djktRb3bZMBPcO0XQsAa+pdJdERzLwnM+uW
+ibjR/jpJW+YiwvTsNIoib0z/0Zh6q7U3R60BjDFKpM2Ar44lO+r3jpr08WSv/cdy/hl4bP16vPXt
+78TadZMX5ExCJ510El56xFEAvCZu/PcwtsfXrcVDDzwAHpdUW6A991yFfq8Y8fsBYPxvEdOCRwVD
+tATaNezKDM/8aDS9dQ5l6TP/rCOUZYmqruGsxVe++jV85nOfxyMTdi+ahFbtsQfOv/hSZGkKYy2s
+83UIMYFssDjYGqtrhwsBAtuxALjmyvdKQBzMzp0NIfZqQbuA+icp0rzw6HQAuKampjDV80U+ciRn
+v2Pcd7S22OxzMN3HBMG459viAuOiYpx+xvcCeOKJJ/Ar73r3Nmd+AHjdG8/E7NwyCDAUEBDtuP2W
+wLo1D+Ouu+6a6F577L47oskvg2QbxUA2F5TdBeuGVuPvznkT3zrnm3069v3/ywrWOkAIWFOjKks8
++ug6/PWHrsWdP/rxU1yVzelNZ52FE046BY4AdgRyBCklpBQwzmLt2jWoJi8CWnIBti2JHsDHQ4hX
+A5w15id7Mz8rppDlefOiZVmK6ekpJEna5gGg1fjjjTsaSyBiBB213eYAxHuIkRe5veeYFdwByVrA
+EI2l0GWQRx9bj7vu3vado0569atxzPGvghACMuhrwV6kOfasvLCwgMXFyTJW995rL9zS6Tfo/XN0
+LB2G1gmWzc0hy/za77ZiV79F18w0ZmamfZsvKcM6C9TW+lbf1sFah8FgGJKUBKwxqIaLMKbG9773
+b/ira6+fGKvYGnrePvvgzZe8BVJIWHIwhkAswBCQQmJhYRPu+MHtMGai3p73YQcMAQLbtQDAQQDe
+AHCCThyZGVA68X39gu+YJAmmpnrIUp85JoX07agANAbwmG/euAaxD/8YIDiOdkdqE4ba+7RKsNvS
+qw0idpt6jgON25recNZZWLHb7mCQj5QIb7ZTyOBj+NLdSWPbf/bn79tmYzv+uGMwPTWD2dkZrFix
+AjMzM5ibmUGSKAgAqZYAOTjn8IUvfgWf//t/3GbPHqeLL70UB7/4MFgimNqByIcWk5CaPL9pIx64
+/4FJb/cDAJOFVLYz2i4FwLVX/cUqJvoPgHhZVKPEPu6rkhR5f6pB/QUEUq2RZ5nPPQ/au02y6SDu
+aAE/AA1DbA4EdgHC1tQHMGJBRDO4iwO0bD3ayHOzVtpP4jj8onTsMUfjxJNP9UU4cGFDk24Bk4Aj
+wurVD2LT/DNfs/L1f/r5iP2LX3QQpJLV7f9+Z/Z0jeOIl78cZ59/EaRUqIyFNQQXgAoVmsnVxmyN
++T95X/XtjLZLASClPJ+AE5moE1T2jJPlBbK8AADfzYeBJFHQSvrPGNWtXfBvtKXWlnvsdfV0y7St
+/m6FQpfhg5DpCBegHcfIeMS2Z/xIb77wYqzac28ADNX1bYIAk8L7to88+AAWFrbPorV//+GPAOBp
+Y34AOP+ii7H38/YDkYMLzO+B2hiyJDz80IOYnzBVGsBk+6pth7RdCYDrP3iVEAJHkqNLAOzRgkee
+hdIsR577lt5gz9BplmIq9vUb0+Ddz016bQTuOn7+qMkvRth9pHili/o33N8F+rpJQEA3wxBoQ38Q
+o0JhW9Dxxx2LI4893qPYgqFi7JEJ3MTcfBee1atXPy1+9Y5ArzvtNJzyujdAKY3aWFDYiJRIgAVA
+7N+NH99xO9Y++uikt90+pekEtF31BFRKLQfE2UxufyYnWr702jor+lBNzB8gZ9ErMiitmnxzT6Lz
+LxB74MW8gK5JPK6RR7V6PL819UeQbnQjB11QMFwbx94ZS7Qe5DZe+ZNOfi32ff4BAfyLkxFgIZpn
+An7NntiKLsLPJRIAzjr/Aqzaa+/Qv9DAWJ93QOEdk9IrhocefBDzk1cBTgwWbG+03QiAG675ayGE
+PJjJvZbYTTXM1in2STPf5EMIgIlQFDl6hd/Ac1T7Y/S3qOiZG6U9roGbIp2Oxo8/xjP64lmMcWER
+hUTruTQCZuw+2xIDeOnhh+GEk05GkiQQoM5mptyMiTgKH0I53GGa1m5TuvTSS/HKE18DAYG6trCW
+QRBhk1KGUgJa+/doOPlGK7dhB9oIZJy2GwHgjEmsrY92zr3Qd4wFoskupESW96B03NCDoZUKYb+k
+feGjhm/u2hUGXbivo5Eb1yCeKVqN3TBtq9Fb26Jl+vhM7j4vhhG3AASMpChsAzrxpNfgoEMO89V+
+neMcTBIh/MiIfQjQTNgE5LlEq/bYHW885zysWLESjgjGhPqFGCJ1XlgqKfH4ujVY+9OJ+wDejR2o
+Dfg4bTcYgDXVXkLKk5lYiQ6ajxD2S7O86euvddzHzzf5aDPRRvftG++o0/jl3bh8RzOPigiEDj6j
+2r17n9YCCMfj87pFQJ08AdHcCFBy4j4VT0r7v+D5OOHVJyPNciCE/gS8PwtqO/PG8T3y0INYv5V1
+8s8FOuvss3HkMcfBMWFYWRhjvWVE1IZHQ2biw6tX45FHJs7rmbwv+3ZI24UFcN0H/nJGKn0+E79c
+CKGi5udQVZLlfeg0ac7Psgz9Xs+H/aRoUkq7mlyItptP83PM9I9hwtaLb7+P14/aFIHluSMkmvM3
+dzkiHNBsC4ZWiMzMTj+VJWvo2OOOx0tf/goIMCQ4FOT43INOgKMjgbpNyncOOuTgF+F1bzwL/f40
+rCXUpQOxZ3hC6CRN3DSLXbPmETzyyMQWwH1P28CfAdouLACp9LEM+ybJNEfkG0syecbJ8gJpnrfo
+OfttvBOtoVSUX6IF2QNzRu09iuaPx+2jFu/a6cEB6Jj+Lc7f1rGP1PGDMdJQpLlbJxrRzSUAsGx2
+Dhdd8OatKvEdp91XrsRJp5yCqZlZgAnoWB9ShMQfBpQQIMnB5B11Vbq0bG4Oe+65J6anp0K6LkbG
+DXjBaK3BQw89BGstBoPh1vTMf1bojNe/AUccfSyICVVlYYl8PwG/dSGcA5SWKDKfrbiwaX5r2oDd
+9jQO/WmnZ10A3HTt1Sucs68D06FRU8b3WErlC3500sXkkCQazWvc2O9txl+3FXdk/m6Lr1afd1/u
+LcFynWiC8HHiBlDEKBLQBQe7Vkd7rkDnwYCUOOXk1zwlAXDMscfghFefEkQMQcX1ELKxOgCPcDM8
+dpL3ekiS1pp61QmvxOveeCb6/Sks32UX7LFqL0xNT/uuOCNoird8hBSwtcEjj6yGNQbWWGzcuAHO
+ORhjIAD89KePYN2aNX7r8TVrsG7tOjz22GNbA6xtMzr6qCNx5rnnQ6sEw9qgrAjOtVZczABUkFBK
+wZLF+vWPYTiYuA3YDhsBAJ5lAXDdB/5SOGd3Z3IvIed8fC+wFREjy3Nf6hv8dCaHfr+HNE3CDr+t
+Ru6a+d28/obJub03ow0JRhBwy0LB/x6NgfFkom52X5swNH6P9uLu5iOAwEEHHoCjXnEEvvGtrU8k
+WzY3h5NPOQ0zs3M+Q7JFKBu3hrgt4AF7y+r5B7wQLz/iFbj9B5GkCx0AACAASURBVD/AWW86C7/7
+R3+C5z3/gMYXHBVso9R1Jw465NDmfD8930MPQsBYg3I4QDkYYDgcoCpL2JBT/+i6tXjw/vtwz10/
+xk8ffgRr163FwsIiFhbmMT8/j7XrHt2aFlxPSr2iwNnnnY+DDnkJrCMMBwbOeXOfwjrFlu4RMB0O
+FnHXj34EYyeqAViLHRwDeFYFwGXv+g2+9qr/eSiYnxcRs26yjU5SqDRtikiyPEe/VyDRCQTac0e1
+a7d+v8OEPPpid3vxd1m1y9Ddb9oKuNbZ724Q0gUQG2tDdAWP/9kF5bI8x3nnnPULCYBzzzsPZ775
+Il+/LmjzmTSltv6YlhLGWeRZjt/74/+GC9/6y9hn3/0wO7cczjmPjQqEzr6j+MaInRSjCtxxcKRn
+fg7rqpXG1NQMpqdnO+vq7/PCYHczOVhj4MgFD0rg61/8Av7z7//eFvvw93sFmH2KrnNuZB1/Fh17
+7DE49fVvhBQCi7VBVTsQ+Xbf5NrxAgJJIqClwMKmeaxZMzEAeA920BqASM+aALjhQx8Qzpr9iNwZ
+zFiJ8AISeTtbaY286I8k+PT7hUf+w1bezSvQfVPRSfTpWAEjmX9oj7fmO0YESpBHzdvfWBfd6ztg
+X4vwi45GbO/RXtu6BXfffQ9+63e3qm19Z+z+XrH+AeCw5ZeE8GktLdcJP8tEAhaM2dk5LDv8ZXBE
+QGgXpuAthJFJhlvEph1oUU2wFBAhRBtBWBKx9r9d2bimMQ/BCwwAUgUrDlBCYsOGJ3DHD27frN5/
+emoKv/prv4Yjjz4OxlQYDoZYu/anuPeuu3Dvvffg0UcfhXMO69evx5q16xrrYW52FuddeDH22ut5
+MNZhWBqQi5rf/3HZ+TRgISXyTEEJYDgYYMMTE0dJbv+F/njbET1rAkBpnUDIk9hUr2K2ud9XvmXC
+rOiFDr8B4GNCohMoJf3mEqFBJIBOL/6Wxn30UU2MRiCMV/d1bP0WS+AOk6Pr6Xcsis552BJ4Fv8J
+rsPGTZvwu//pD3/B1QM+fsstOPSww3HRZb/igT4poODz2IWQoa02NfPkICg0M0h6zMB3vuEmlBo1
+sYRnFBnXHh1jKvyR/HxCn4EweQXfzjtaQnFxpJQgMIhFc33Mv5dC4t57foL3/el/wc23/M3IHHdf
+uRt+/w/+EG++5G0o8gLR6bLOwdQVFuY3effCWdx3912460d3YvWDD+Kxxx7D8/bdF6e+/kwwGItD
+A1NzUxHpXMj+I/+/Cq+HY8aGDU9gcXGybskAJt+fbTulZ00AMLMmcq9kopUx289rCoYK4B9Em/WX
+JAl0SPmNxS3jvr7Y7FgrBLrVfiOewRgwiA4uMKL0mm8xcn4XIOzKmM265MQBhav+x3v/18Qbc26J
+Ns3P4+abbsSRxx6PA1/4IhgipDIwM7ixZCJ1TXsluBG23axFBIFI4UvvHofVi5ZRs6gtyOiFTcdd
+ChaQkH6u3k2R3nUQ8Mwf0rN/8P3v4o9+/3fw1a99bWR+L3vZS/H7f/jHePXJp0Mp775EEFJJAV0U
+KIre/+PuvOPlqOr+/z4zW+7ekh5IgUAIgYQWkhB6gNAFBZSmgjwgYqMJSFXgESyPCo9dkCaK1EAI
+SQgl9F6FCALSQ01vt+/unPP749TZuzfZDT4X/B1e4e7Ozpw5c+Z8v9/Ptx4zFMWojcew2977GRVQ
+7w+JELR1lGhvL1MuJ259aZSgDaVSKUTArN9+83U+qG0noH8CD9b31j577VOJA7jmst+K7o72TZJS
+92YqSYTdWMMayeJcnkzWb+kdRzEtLU3kbM1/25EK/4jUPazUUuGRgKDTZyqj41f20vNmyrET5YFF
+wCD8s/To3XV+483TmTO3vg0+QG9a+bn993ffn3zqKW649mq6iyUEkZOqtqKu37BEOJ09nJRUkTI7
+YcLaOZRDK3rogctV6Y1Y9LMrlPTmTxFwu/CdSkNsZandkYKIxx+6nzNO+W4P4t9j99349R/+xN77
+fV5X5zFJO0kpoVws09VdpqOzSGdnka5iibLZERppqxXqsXV0FGlt04VHpLIp5RrZJYkysQCaqWQi
+AUrywXvvsaS2OoB/B/rerfFvbp8KA2jpPzibyTdMBbGJc90F+mU+H5T6QtDQoGP+NSKwpi0DRaOw
+kJfqEexjmxVmypKwk5Ce+1eG6Nq+3BiDcz1SwDOWKnf2zEl3/sxzz61TkY2DDzqIi3/5Kw478stk
+Mj6K8M45s3ny0Qc14duy6BaFGBdASJRu3pxOQkDs+rMWnnae/ew6DhmYCpRhGgYoOA0qTKKySVFl
+FSHNkrv95r9y8rdP4LnnnnfPIoTgmGO+xm8uu4pttp2MQlEuJ8gk0fq7VCSJIilLysWEYjGhq7NM
+W1uR1au7Wb26m1WrulixqpslyzpZ1VqkXJbmXdliowrjBdR2CanIZWMaCxmkVKxeXbNN7z+2BkDY
+PhUGkM3nso1NLTtGUTwICMJ3IZPLk2tocHA6ScoIoQz8xyww4cKCrUEnMpAyCmxfXrdPE6YjVoEz
+MnqXoM/ec6Ru7QVpq4JHGaryaOU9dZ8fLVzIqaefVfd8bT9lO84473wGDx7KPgcezFe+/BX32xtv
+vsX1115j9rCPkNI/dxjdBgTErL85o6gI0FFI3MEYInRmoQgpPVSFBO6dgLG0G4OclFAyHpRyOeGq
+P/yaU086KVXHP5fLcsbpp3PRz3/NqNGbmDJdZWRZoqRAJlIjiEQiE4N0Ekm5nFAqS0rlhO6ypLuo
+g31KxYSkbDYQMUE/UmoPhJSSREptC8AIESHo6Ohgee2ZkgtqPfGz3D4VBqCk3BDFJiCjsNgnQCaT
+xW77hdK6az6fAzSRB0p9utmcAKelVxBwhRZvJXiIGCpJ1xUHVWG1oBTeqGAyYepvujepJBf95Ge0
+d9SXN1IoNHDiqaex5dYTSJSkubmFo4/7BqM23NCdM2PGDObdfacOaRUCiU2iEsarosdiCVILZGsv
+EIb5eX3KuTDN6DWEl1a5x7tIfXlwZSbToggBzkNQTBRCxHS0t/GHS37KheefT1tgaBvQvx8X//in
+nHn+RfQfMIBESmQpQSSKRNpdgDQjQdmS4RIpIiPFdTKPlJjS4hhpn6AklKUp+iH1tdL8rhGFdCrM
+wo8/4IP3aqbr1+t6kZ/R1ucMYOZt0zMgdi2Vi2Pt1tROPwVd6dfm9wtBS0sLjQVd/FNanTYIerHN
+ErIIdVnhEYGV3hZspCRX0I+FvmmmYBmICs4Jf1eekMx/HkXo///x8it54smn656v759xJnvv/wVt
+GzF9T95+R07+3mnunERKrv7T5bz0wnNIBWVHiPZ/WvfVaMBLfY2SbJxg8EzGSh8FmCESEVGgYtgr
+dB9Gz7e3M0ggkYqSEkRRzLKli7jovDO5+OKLUxGBozfemN/+8XKO/+4p5HJ5kiQhKZVJEqntBVJp
+aS2VJ1wFSkQkidHtTSy/J35j8VfCEb5SgrJUJMbyb+0lZioAwaKPP+L99z+o9dW8W/F9a+BlIcTK
+Kv+WCSGOqrXjvmyfBgLIKqV2SUqlIamIL6XI5hvI5kw1KAVSJkSxIJPJeH0y6Mh/9rK5R3yIRQVW
+cpkTnJRXntztYg6hbXhDRw4q/WOa8EVANhpLz73rHq64+s+1zo9rRx99NEd/41s0NjW5Kj8CRZTJ
+sP/nD2b//fZ15744fz633nQ9HW1tgNC7AGPVF4uCwG3YYSZL2d/tsxtG6pOK9PNpN570zM8yXGc/
+MXEBQkvhREGitLFv4cKP+NG5Z3LFlVemYjG22GI8v73scg784hFEUaxr85fKmpiNDmdxlUIYZmDR
+gInos4wBK/WVQwPa7aet/tL0ZftIpKKUKDKZiAH98gih6GjvYEXtMQChAfAIdEzAlkD/Kv8GAX/7
+LDKBPmUAt996i1CKjZWSmydJOY6cHm/hfy4tXYWgIZdDGONfyARCK7sj1wpU0KMJoX3kFUa7nhGB
+PtfP2gUCS1/qGo/5q5sBX331X5x7/oVrGVjPttvUXTn1zHMYut4wkGVioYkyYx5++IYbcczxJzBk
+8CB3zeWXX84Tjz6kmacSTmLa53HDtJZ758/HKf46kco+uyY9GyUoRITAehlC46LJqHOzJygrPdcf
+vL+A/z7n+1z3t+tTz7f7blP53WVXsOse+yDQth5r7FMmjTkxsSFKWlXAMBarFiBAiRRRWwSgdxqC
+JNGQP3HSX+v/0txDoGMopFKsWrWKFStX1vJ6whLgvwZqTej4mxDiiBrP7ZPWpwxAEzETZJKMTsql
+VMy3rvqTM9V9QESCfv1ayOZy7hwP/wNzXAjpVRqeu2vwtOs0deE1WS/U0hI8GDkWRodmiHQQkZGH
+wpsoli9fwXkX/KjueRqzyWhOPv37jBk7Tkt8j7WJBK7e/9Rp+/CVr3qhUi6Xueqy37N40UcIEVFW
+drpEQPDKMUI9P8ohAWyoMwqltO8eEWmGIC0iMGMw10sUETgGLZWiZNSHd956nQvPPoObb7kl9XwH
+HnAAl/zuMiZvv7PeEahcJilJlwGqidwQsrXaO4agnHtTE3XiiF2jAm3oKxujoVS+4k9iGIhNAEqM
+jUFEEcWuLt5+8/Va9wF4EVgfeBI4tc7XO8nO1afxr7L1KQM45NDDFbB1uVweGPrQFBBncuRyDcYK
+rZDlBNCVf+yxyEosK2tU4Ls3a9cRaUpae4IVwTFEFKgI2uCIPa9irqzBzKoMjuG4v4b4jVQpl8tc
+fuXVdQf7tDQ3892TT2HaPgfo+wjl0p7tnvU62k7R3NzCYV85mh22395dP++++5l+/V8pl4pG79Xz
+ZKMnbfELZfZN0IxAaH1fBPq9MZwqU1Q0qI1i4L+F6aBMKKZUigSBEDHvLXiLn1zwA26fOTP1fCec
+cAKX/uEKxm42nrJMtK5fLGvDXiKRCMrGyp9IKEtL7Fq1sGm81oVnNzzVCCSinHi0UE6kJ/YKdUFa
+G4BZNx3tbXzw/vu1vqb3gIXAjnW93M9g61MGMPO26SOVUuOEIK+MPpkkZQ3DMhlHnZZT5U3aqi/K
+qTw1gy8D7h32XnaHOrA95PRWKhE9TpkIdNwQHVid15/rdXzfv/ebz5l7FzdPv62u+RFCcNRRR/HV
+//qG2d4MYvt8zsqgI9didPDLVhMmctgRR6ZSfP98zdW8+vI/nGFOGa+KG51Kz6kQuN9dVSXTokig
+pHTM0RpsLUPV8TNpG0ySlJlx843cNmNGj2dc+PHHvPjc03QVuxFCEEURIjKYRkRBmK7UKoGF+0bF
+sMk8yur40ur4IJMEq7bZWghSKRKlvBFR6jiCxHCClqYc+UxEe0cny2qvAnxCrSd+1lufMgClGCsQ
+22ayOZoHDCbX0Ei+0IRSikwuTxxnnA7X2NhIQ6EBsIQYQhiV+uNP8j97ZG4WpxApBJBiBuEY0Ysn
+ZAROh3bIIG0jEIEdQAHPv/AiF170k7rn5wufP5BTzjqPQqERkMTmLpHAJOr4QWiC1DD9C4cewYEH
+HOD6eeedd/nr1X+iq6vTWb9l4O90qovwn3WlNGXmRrnfHbOzbsOKd+DQggrsDApW9qJLz54zh2OO
++jK/vOh8Vq1YThRFxBmNbGTiGYlB8ihlowhxKoLzDJhkHmmMfonU8fzlwCYgtcvAqQHWVWqlfz4f
+k4mgq6uTthq3S/uErbEvblJr62MbAJsj6J/JZGlobGLA4KE09xvIgCHDyOcLXiorjc7jKDZlmisl
+uyVOZRZxoHibG1lftCv5FVC8XaiVMD91uf0ghMmw88Yle3Z4uT28cOFCvndG/cE+W24xntPPOZ/h
+I0aiVEKMNJl+OIs4kDJgZoVmReutP5z/+sa3GD9uc9ff9dffwJ0zpxsEFJEoUxvAoSw3fW7+LPkJ
+4y508xdFXm2w1wTPbJmAVLrQZqIUXWso/lEslrj00kv5w6U/p1wuEccxmVwMaEIuG8ivw3U9dNfB
+RUbqJ97gZ+MeZADzrcSXChMqbNx/KvAEJNKNf8G7b1dNQ/7/vfUZA5h52/R+KDYXQrQIuxKN4S/f
+UNCZf4FUaiw0pIitanOUWtk8jBWR1Xk9F/Fw1nSD/xsubN+b7icSOhPRhrlaGWi77i52c/FPf86q
+2neUAXQgzCmnf59tJk7WyVDCGP4c0rCoxKZLEzAy/aw7Td2DHXfe1fXZ1d3NH3/7G55+XMfZJ8oY
+BU1GnkK/fB1b4wOE7FNFQmBttFYFsCHb+r0YI6DSXoNEKqSJHHj0/ru56845a33uv/zlL9wzdzaR
+EMRCjyUxEN2pGkp5qJ944lZYAhfOwOeud2qEJXRjR0h0NaAkkc5rkRgbyUfvv19PDMD/N63vEICi
+EcEYFDmw2WkePvt8AN3yuZyGvcZIV0mU4HXjajg+VQ3X9m+atGoBHlA4GN/D+Bf0iYXOmoiicMgC
+rv7zX3jsiSdrnxPTTjrlVL54xFEoFJlIua3NNWMxlndMrn2gmuin1ym5s269kQfuS2+m+cKL8zn7
+9FN57KF5REKjgLKBwNj+wDNRS+DuHM+ArMHVhs6CTazRUY6J8SC0d7RzzRWX814NxLR02TJuuPZq
+2tpayWQyZLKxgfuegLVbUARuPuMOlDoUWCba2JdYl58jbOm8CErqkGGNDqQLKMtlIlqasjoHYNW/
+p67H4bvvwoB+/f4tffVF6zMGoFDDgBFeovlNK5xKbwi34Lb9Fj2kNCnCNhQrbSe+s5TKUIUhVGca
+eIgbaBm2G5G6XlvPMXEFDzz4EH+68pr6JgX48pFHcvx3TiGT0Ua/yI5DCPTuvpG5r3D7H1jYmxgH
+/dxZM7j4Rxey4L2eVux/vPQS1/zpclatWEok4pR9w+4XIIKHVWikYz0HocYvhGVInomCDvhRhsnO
+mzubJ+pggk88+SQPzLuLSEA+FxPHES6Cz9zHufqUopxou4Ay0j8J1AMM5LeBQNojIA3KMbv/oFWV
+ciJpLOTo15ihu1ikq/uTJfa1NDdz1dFHclp2mLNdrWOzgUOV//5PbAd9aAMQmyulNtZ6XhXsDeDt
+1MRx7IjQ2qK8TSrFMQIx7UA86YvNV+sDCrqoag2s3kXqEucuA95d8D7fP+eH9UwGoHfzPePc8+k/
+YKAL8tGJKdZ2EWm/tsAZ8fRjaOIXIuLvzz7JL3724zXC1/ffe48lSxa7aDgd+x7kMZgPntnZmoI+
+1t9NlQrOs98RQER3sZvHH36wLhVo9epWnn78MUrlMnGkA75coI8J9lFSGwgTI82l1f1thJ8M9H9p
+g4B8ARCXoi21R0Aa6GPVjFWrVvLGa6/V/uIq2oYjRnDF5z7Hvvd00TAgz9Jla0woWlOgwaHASmBZ
+lX/twJfXeZC9tD5hADNvnZ4FhoAYAmkaTtGdgZ2ZTOx07eBXvCNLOCoUKeIn+GxFv72ZwItQ8yfU
+K5w6UsE4Kj9XjL+9o4NTTj+Tcp277WywwUhOO/s8Nhm7OW47rzDhxvreCfR/s2ClmYklSxZz7RWX
+MX/+mitTtXd0sGzJYjP4NLQXhqsoREqlscxNGWODdXk6p6pjBnqeYyF4983XeeLxx+uaB6UUjz7y
+MG+/+TqZOCKTsa5dYxNIPCFjmE0ipasr4HMElCv3re0Rumm1AJ9PYPoTCDIZnd+wfOkSXnrppbrG
+bdtBu+/KDRN3Yeq/FNHIHA/KtrVtvNpbDfVDgVvXcrsb+Tczgb6pCKRX0mDjlo4c/QanhNKoUCiE
+1zn3ncIb3hA2Ah+PT3vclEBch1hefw9dWsqoEqluAinv+EKFLeJnv7h0nazHp51xJrvvua82xglM
+HL1J1jE6uDA6v3WzRZEO7JFElEslbr3+L/ytIsS2WlNKmkXpid+W+3JTYoxqHtl4t56IRIX0l9o9
+JyVKaB++BD768H2W1lZMI9WWL19O66pVWi/PxiCELksuorTEV0b3txJd6Ug/KX2Gn1JW/9d9251/
+pPUEGNtCNhsxZECeSECx2M3SpTXHALj2/S99gWM/yDKwXSBGNFBe0M6LHZ1pwdKzVTM2jGHtxG/b
+jaaP+ivKVGl9ggCUoiwEGyvPmFPGM6fnC8hkMrpWnbCx6P7sCjshodXOE26FPUBU4vlKRqH7cURu
+JJqTkIFIVM5qptvN02/ljtlrt3ZXthNPPJFDv3w0cZwhUolO8hGaUJXCGemsKmAr7iaJDodFKR55
+4B5+dekva7pfuVymq7vbLczQvag8VYOwBkdh5kT/tYk3Vl1yYbqYXZlERLlc5rmnnqo1lj7VVre2
+8dI/XnSSXhorfpLYpCLl0nhtUc9ESlckxAX4JD5HQEnlIgt11mCgWijN6BsaYkDS1trGqtU1bwTC
+8PXX57IjvsSJr2QZmM8jBuWgJCkNyLBk7enelSc0AA/UNWGwe53n99r6qiZgf2Ck0BmtOrdcKuei
+C4tWRFHUQwo7OaytYwFMtbLKSFBj4ImE1UtTnehzreTvEQlnmEnABFJuRqtVmD8vvfwyP/mf2ggw
+bJ/bfz++fcoZ9B8wEKUSMrEJRTbFPLWrT0dJ2r8G/5MYXXvF8sXceN1fWLJ0WU33XLVqNYs+/ABr
+Y5FK4kyLUaQRh31cyzARmiEkoX3Auzw9/NctKZd59ZV/1q0KAXR0dPDuW29iw5Od399a/QN1IzFE
+jfJFRzwakI74E2MYdkFEWIMiBlWY4KFEsmDB23TXuLvRXjtuzzlDRzHuOUm0cQMi1vdTy4s8v4nk
+wdlrLRT0VPC5ES3RR9U5ZVPQQaJr1DVqaX3CAIS2YrZgqVeCiCNkkmCTUsBKI8hmTAiwJXaLRSvl
+v3MlitAskCL+gG7NNWGHgRrgdBLh+k7ZDwJj48JFC9epou+EbbbmjHMvYOSGowBJRpjoOhNv74p5
+Vtg2rEFLEoFKuO/uOcyYcXvN901kouvvm+cXKoj7tzaHAGvpxzZVl4WOvbfErzDX2OSgSBfXLJWK
+LF9eG0PqMb4kMZGDPlfBSXbzT9poRjMfyggSm/evz9PXJtbYGTApKZM0o0VHU3Z1dvLqSy+tDbYD
+cOohB3LssgaGfgBi00aEMVRi7vVRY5buYnFt3VhjQwvwOLqOQL1tTzTzqB229NL6CgEUUDQ5nT7S
+Cy6KtI6npPYtR1FEHOsCEiHhRjYM1jQf064P6s/pJSwrmIGjc2m/OK5C6oRQbQhUDMu72ts7uOgn
+P+fDD2vePBKA9ddbj5NP+z7bTtnBGNuUq9TrH83eVzkfvR2WVJopLF++jNlVYuzX3HRePlgQpUBF
+dvcAj4KUMozIzJuTngY5oAPxrU7ubQQRCz/+kFWr6guACptCue0Nw0g+UK7MmasMpKyE9zq9woc7
+K5Po45mI/5uY5xnYL08hH9HeWmLRokVrGBmM3WQ0Z02ZxJ7PlGkYmUUMiVGJ9GtLQXuj4sb5/1zb
+Yy5CS+0NgIfQuv+n2vqEASjIC0GzR5bVCA9X/tsvOYUzhQf0GcJzawq0NGuVhEhY9cD8TQn4sMRX
+MEpH8B4hVKKPv91wE489/kRdzx/HMcce93UOOvRIGz9nohGVC3VW4aMaP7wWWBryaiGseOKRB3no
+4Yfrun9ra6sxclk0I1CRQhgpToCkhNH5hdHvrRS12X9WUCZSmnFqQ9K7b72xToY034RDZzasV2CI
+PfES3Rr4QiJXSgc4CXBJPjYJyLoSvVqh79G/OU8+l2F5qcTKFb277aZsOZ6fj96SsS8p4jHNPivS
+2mkQqPYy74zK8PQta/UkXIvOILwXrft/6q1vVABBCzDIErGbP7OghHHPKTSxSKU3a/TNEKLR0f0m
+ncIzFMMYLHTXtoCwqm1qRI4xeL5SKfWVG7z9/ODDj/L7y/5U9/MffNBBfOd739fMTZVNwKzVVw0s
+N7vyhPn5wtgrpHnWzo4O7r/nrrrrCiZJQnd3l/e0CFx9ABuRqXVkQCU4q7mdm1ASI5AyQRcgVZjN
+ffj4ww/r2VG3Z1MeSkuTxgu+dFdoFBTKpgNLzwSM68+qCt4ugEMH0hgvBd4guHTJIj7+eGGvwzpr
+4MaMfR/ikQ0WnNkp1PeIBMniIncOrWkz0aXAI2s9qw9b3yAARQFU1kpdL/SFweTg3jIqcNebsFjH
+ObQXMSXOVSCtjXEI4d1cyvWL4Rf+XGUIzd5NhNeE6oGC9z/8gNPPOqfuZ588aRJnXfAjBlijX+RV
+FQ2f/STZDTkImIAKzn3l5fnce++9Ve+zthYHhtbQBWqJ3zJZrXp4YnTjcu9KJxfZyDw7z22rW+nq
++mTbhHt1x8dsOcI1ahFG2luGpRyDSJcGs0lErj+rRigfDiKABe+8xXvvv9djLLatn28gbmpwxlA7
+LxatiK6EpZvnuO/VmnYJq99q/H/c+ioSMALR6eg4gJWIlBagmwj1+Wq6OQETML8Z5qEcWjCXWFTg
+7AGhiy/EByK4VgXHdZLP6WedW7eFe8SI4fz3T37G5uO2JEkRv5dEzsilcPUNdFFNo7UbvTVR8Ppr
+r7Jw4Zr11TU1Ny2h2qW8HQAwsDulBKFM2S2bdWe5RhRFzj5TLBfXFgCz9vFZFIDwsfyWqB0i8J4C
++7oqK/0GQwy8CZoplCXkchFNTVrVXLZ4ca+2i0wmQz6TQSW+yKvzEJmWrCrz5ICE19586xM9e53t
+KXq6E9ep9REDUBlhZXYg/X1Un/4TRZGXTOb/whC3pkv/0iu5hqFbs5ht9Ld/UaGdO1DpnSIg8OqC
+VxH0aZf86ne89q/6qkDHccyZZ5/LTrvtiVKSrNClvPyARMUIlSdGE5Ck0Om1AJ3trdx/99yarNWV
+zboSrVHRzoX1AGgCsXqyUUVsiS3l2KqTsg5mG709UZJSKV3ird6Wz+fd/NtS5i7OX2r3nVUH7PFE
+aV+/I3Y8atAQH1czwP7LRjBiaAvrDchRLEs61qBOjRkxnEIcO7hv59JmggoFnQMjrpj/8jo/d7U2
+auSItZ3Swb/BBQh95gUQ3QoajF3OW+Qdx/fSJjLSzyMAL0E4/gAAIABJREFUvN5vN6N0MNl2b5UL
+Q872BZnfvJrgxuM5hjnHawmh3UAw445Z3DS91iAt34479lgO/fJRJrMv0ZIyNCAp5ZXZ0O2X1jzM
+NlqCjrY2Xpw/v+5xAM69qpkMKG3PB3TQjVOHsDvoCKNfW8kZlNBSVtcGJSSR0K6vZUuWrBNzAv3O
++/Xrp/cRkIkrD2bHZbMALSJwZcCxmaJh3QAD+6VyjERHUUYMbs6y0cj+DB/agIgikmKRZWuIXJy4
+xTgac1lUd+IYtmsC5NISD22pmP/wuucRVLZvHrAPTVHMr9bsZepdZ6mz9ZURcDU4qkIZWO4sz07F
+t66cQFEL7QTgCSSMATDfHVMQAR1JCcZXLQKGYglRmGs8MBFmjIJXX3uNC9ahss9uU6fy3VPPoKWl
+PyDJxD7yTkt46e6jjX+WOVg84tUcHX2Y4bVXXmb5mpNMem2VcRJC2QIa0kcbogzRRzpmQAWKkPKG
+NfCRgHZHnbbVK1m+bN1iAEBL//VHjACE2V3dVDSWfkNPwJf6NoU9MOPW45GBnq+ZmFZRoLkxz8ih
+TWw4rImmQgxKUJKSh+67m3vumtvruDYZMpB4ZQVTs5NYlLQPFNz49jvr/NyV7TsH7sdpbxU4e/ha
+3/O6LYQqrW8QgGbWLgsqTYgqdaJzfznp7j7oM5QP8zF2GPOlgrgD6e478xJBX6tVEJ9khCPClStX
+cuKpZ9T9qJuOGcN//+RnbLzp5sboZ3R96QCMM1JKlNta29G8GYKu/yncI7zw7NN1W/9TLbCyW8kO
+CqlMWRATRKOr6voiKrY0lx2ikt6aHhmGtnrlChYvXnfbRFNTExtstLGJC7FZf/qtJKYKkS8LJn0F
+oACZOMOhUkBEPhcxZECBUSOaaWnK0pjPEAkolcu8+PxzXHvV5Txw3/189PHHvY5rg0KBzOJyWt20
+KuqKEs9NyPLgdc+t83PbFscxP/zi5znqVUFDU5YPl681nPrfVrusr5KBEqXoENDfEygu3BVwtG8t
+tz0NgwSSOugkNA4GUs4uYktgaYefSt1AhEzCBOecd8GPat0l1rXGxgLn/+gitpm0PSCJRRBpGAWD
+U/ggHKyp0zIEr/9rL4UO2l24cOEnMrJp9Uo4IvYG1gAqS1uC20+pd6nhCC3sU6Foa13NihX15wDY
+1pDPM2L4Bi5Aysbx23LmLvQ3MfX8LOQ3YyqbjL9ICLKZiIH9Gthg/WZGDG0gm9FLvK29jZdeeJ4b
+r/sLs2fPrmkPwCFKENvlqYI1IxWrBgp+8dzf1/mZbVt/6FDO320XDnolJh6Q4Z/DFG8+uNbqxPVn
+XPXS+ioScKUQrACGAynDfmgJ01lnAUOwH0RI2T6ox2n2Tnoq/91a+M113mkQEn8aXVh0cMVVf+bR
+OoN9AE4//Qz2PfBgokgQC4Xdw1cBwlbZsVBA6r8RYVKNGYvyteqsD7ujo2OddWwhBBlTcNU+p90q
+20parF3AQBRtANTIRTk1wejkJu4in4mIhKBYLFIsrrsLsFAokMtnkQpWtZUolhKE8FJeMynr8zcb
+fSh0VKKxoTQ1ZijkYjZYr5lhQwu0FLIUE8ny5ct5+olHmDvrDu644w5W1VH5Z0TRRzv6wB9IlhZ5
+eKuI+Q//a52fGWD7bbbmB2M2Y+IzkszYPHJZkQ8aMyyvfXeiT9z6SgVYji5o0MMWp5m7iYkncigg
+stQTEL+F7q4SUEAQ7kVZC741FhrpITC4Wip/gR1D0M9DDz/G79Yh2OewQw/lK//1dRoaCqASbfEP
+3W3GOGkxvtbLVco+qcGrjq/3ng/3COvcmpuaGDhooCMq7UoLav6ZQVjobbmvNAY/H3tvrO+JJI4E
+caSvLZfKnwidjB49moGDBlFOJK3tJUq2bDehTm9DhL2OL5WikM8wZECBDYc30lzI0NSQBQQfL1rM
+ow/N45475zB37l201hCk1JDPM3mrLZi8yUbsP2gQoxb4uH4rcFRXwpINY37/7PO99rO2JoTg2P32
+4pttzYx6P0aML+hkr4zgX901BRS9uc43r2h9pQK0o6ua6BaoAO6QAiGU8bV7eO+t8wFUd7Dedxgy
+hXB34cC6Z6RuyFCMimAk77sLFnDODy+o+/G2mzyJk08/k5EbbIRUUlf2cUZK/XB2Y46QA+qvytQh
+0Mc1/UnPLPzo17lls1nyDQXXj430cxAfHd0X0L5BJX4jjsTMrau2Y+ZbCJ27n5TXjQHEcczEyZNZ
+b9hIEhNs4117ejBJCpVAJo7JxoIBLQ2MHN7M0P5ZsrEud7a6rZUZN/6Nu+6czX33P1DTTj+NjQVO
+PPhA9mpqYeTqMgNWSKIV3Y5Rh8xXrixx6/CEV95YNxrcdOONOHPHKez5UkKhKYsYlEMlEkqKtkER
+j7zxTi3d/Nt2Ju4TBnDIoYd3zbxtut8iJ1Th0RImivWCLyfSLEKrt4cGOpwOrfsRaSJ3Orb3GmgG
+IgJ47a8LbQGrVq3ivAsvqtvQttGoUXz/nB+w9cTtDPHbrbyULqdtxmSr0FiEr70fyo/HzEkkhCua
+YCskfdLWr18LQ4cN0+OwpnJsvX1rP4lASadXJ0mi7RISpxpIFxsgXNRiIiULF35Ma9u62aUyccyw
+ESPJxBHdRS39ta1BeaOkiNz9WgpZ1h/cyLChBfK5mEI2RqJYuXo1s269hZuu/ytPP/NsTYSfzWY5
+9bCDOLShhWGLy+SXFFOz7WonYpjiqhLPj4Vfz7lnnZ71qH2m8fVoIJu/rIjWK0A2Ml4eAeWE5UMj
+npy31ojCd+i5M/E6t76yAYDi5WpizElrAAFJOSFJEuJMnFL0faSatfYHxG+xcoW6oE+z0WuBl6Ci
+lctlLvnN7/jHS/UFdDTk83z7uyey1/6fRwiIkKlAER9lp8enBD4RSvjUX4Vyx02wszO+CcO01lX/
+B2hsbGTwoCF+ISur0/t+lZLYEttWJZBmPFYFUEZJSaQ0m5Xq87s6O2tJg63aRowYwdjNNgcFK1Z3
+s7qt6Cz7Wl0BIST5fJZhgwoMX6+Jwf1yLmPwvfcW8NjDD3Ddn6/miSefWuv9AIYMHsyx++7B4bkW
+RiwsE5VLzh3qJL4Fmna9lCSdWcXVyxbS0VkTTHdt6naTOG7sGHZ+tUxLJgPDc3qOA4uqKiqey9Q0
+h22sOQhoEJDt5bcOTAqxZW59xgAUvI6iKCAXHnSoXtnEF2mqwOoTLOxPbWxoqEovU5sNGFYQ8uqC
+O2ZWvy8IYg4quOuee7n9jtl1P9MxxxzDsd86kTiOUTLRdgsFWnP2VvZI2IAeU2jTIpDATqHNcMYG
+gDcfVBYwWZfW0tLCkKHrmftLbLadMtxAKSgrG0hjbSvCMQvr9rOSGXxlHqUUixd+THt7+zqNbfTo
+jZm0/U5IpeguSrqLZcf84ljQVMjRrzHH+kMLrD8wTxzFdBZLvPPmv3j8kYe46frreObZ2lxxo0eN
+4tCdp/DlXAvDPyrh63OalWQDwipkDEqhlhd5YJuY2dfXXvNww5EjOHrKJI7obGLo/DJiaAOiIU7l
+FWBuV1KKeR/27pIM2o1VjsXAFugty45BF+Cp1p4CvgG4vOU+YwACPlLwoYLR+ognBO+X14uqVErQ
+lcGDCj4E57n/B98C4vfWs7SdwdkNAnfcy6+8wrkXXFT380zbY3e+e9qZNDQUkDIhK8x4wrFGVs/3
+RT9iF5QjUvE/7rN5HqmENiSa4h2NDeuePdqvX3+yuaxxuxqdPpDq2n/uPTBa6mtUYIN+vJFQ196P
+bVKWkrS3rbUQZq9ts83H0VhooK2zxNIVXTrbD0FzU44RQxsZNriBbDaiIZuhu1jmxb8/w6OPPMT0
+G6+vGbGN3WQ0/7XbTuyrsoz8sEyqMG/g3lPu/8IbXpVCtZV5e3TMTx+ujfjXGzKEw3eawpeSRjZ9
+EzIDBcJkE6qycmjDBZ11lHl7A8ELT9WUT/Bg8Hk0cBBwFLpK0NrajsB9wBcxlYn6TgWAD4TgNaUY
+HerjWgXwxjEhBN3FIi04vuw6SO/XZwkpCA3WJ7nPCi1lHVOvUBuWr1zBaWedW/eDbDpmDGeedwEb
+jR6DUpJs5It7RJEwHj4btgzW66+J3UpXHQQkBS4j0LoqJeaYNHsBCMHgIUPIZDLrVHJrzKabMnDQ
+EC3BDUG7AB+E32jT6PyarqXOp1c2TyAxEl/o4iTOlqLo6Fg36d9YKPC5z38BEBTLimIpoakhw/Ah
+Taw/pEBLY8ZJ5X/Mf5Hbbr6BO2fP4l+vv1FT/5tvOoaTpu3C1E7B0PeNxA8FQgC1nEwO11IkoKjo
+LiVcWW5lwQcfrvF+I4cP58idp3BA3Mimb5R18td6uQqbV4A2QNumOxJeaIr4eC2FSUz7ADgZXUV4
+91ouqGjDgNnoYiStfcYADjns8NaZt03/u4D9UEQ+J89Ieh3JAQiK3UWSpOxKg1Ua+vR7MwZC+0I9
+ZnYv0RrdKqPt7DkX//QXfFxndl1zcxNn/+CH7Ljr7s7i73c5skzAcnic+uJujS10gqVyd61FLm4/
+RDN2qRTbbjeFQqFQkzsrbC0tLYzbciuyuQYtpVU6bVbhAVNioLdMnBnSlyMzPFozAuF8MKVSidV1
+boVm24QJE9h8/FZIqSiVJWNGDaClKaYhFzuj4IJ33uLGv/6ZObNm8fobtRH+ZmM24bS9prLbahjw
+jmGYgUEP+8zhmiCAY+Z8ISFZXWLGOLjupt7TsMdsNIoDJ2zJYbQw+kNdh4IBOUTGGIHtbax9ikBB
+LEpaB0Xc/PIrtTzaA0DNe5ivoQ0BpgKP9SUCQCkeE4JOoMmyPwGoyKIBvfBL5TLlUpmMieKyQT2+
+fh1pjprisN4QGNw5YBD6z9XXXse8B0I0VVv7zndP5ODDv4pSehuv2KAZt8sOCimFg/+a+K2lws2D
+N/6ZMTuXID4xyf9VTJg4hcGDBtbNAPr1a2G99YeRiQXdRR3sY8Nm3SYayihHymfi2W20ECYxyDIB
+c05k828dtqm/7bXPPgwZNgwlYHD/nN4YBOjo7OSlV/7JrBnTuf7661i0aPFa+xJCsO2WW3D8TpPZ
+bxk0vhVI+1CNDL778meBbdhJZkGyqJMXxsX8+K6eVv9sNsvUSduy90YbsHNrxCbvQNwEoinrqwY5
+ZmKWaCouxPztTpi/ieCZB2tSZ/as5aQa2/HAqj5lAMBzSqkFaIMFYJ19AinLKFMrUClFsVwmb+hW
+Kr03XoqIDeEpZSzozjDoqce50QLpihA8/Ojj/Op3f6h78EcecQQnnHgqURwjkC69V6GIbJEMJ2kE
+ylYkUoHxTwDomAAdG2CRqGZc0pYLMxwkQi+cpn4t7LjjTry7oL5EsHGbj2PbydshFZTLCUnZGwDt
+v3B3Xe3nx9XXk8Y1KAPmoJQkl4/J5WI62kMsV3sbOGAAO+4ylUjE2C3QOtrbmf/8s9w9dw4333gD
+ixavnfABpkzYhq9sN4GDlgsK7xhbREBsKV9+hRQOx556jrYSK4bF/Pj1V1PRg2M33ogDJ27NnoV+
+bLIsof+bkigfwdAYYuEMq7o/z2C8h8GjDiUVnUnCbYvXPZHqE7TtgWl9ygC+eNjhS2feNv0BFFsE
+KqR2ndmKNeZYZ2cXLc1Nmj04V4EJow3Qmi/qYTis07sD3u7UAsGHH37EWefVH+yz1VZbcvIZZzFk
+6PoolIb+UYTdPssWN3E77jqvhpfy1ggnhHLX6oEDQjPDOIqQSiKUrgSg4+GhsbGJPfbZl5tvuaVm
+l2AURWw7cSLDho+kXDaZdS6NVsfbW5epC/BxOr+x+sug1r5SriBHJhMRR4JSOTElwuprBx18ENtO
+nkIcCVpb23j0wXk8cN887pg5k8U1Ev60nbbnyK3HM22ZonlB7WOwaNIyXqcG2VaUtBfgfxtaeWb+
+Swxbbz0mbzqaw0aNYqtVguELBSJKIBPBwLgHuqy0Q2Hv4/Q6M4gVJV7YNOL22x6teez/xjYQGN/X
+CAClmCEEJwjIA554AJVIRBwjhKCzs4tyqUQ2l3co3xGzBdSGxh20CokNd6pr3cVuzj7/wrqDfdYb
+OoTzf3Qx47eagEKRFSZ6XioTuGOcfEqhIm24k0obIPWwDDMwdf6t9d2zKC2Rte6fTlsWSpl9DiK2
+mTCR8ePG8cqrr9Y07hHDh7PfgV/A5tSXy9q74BKwhJX8erISG/9vy21b9581Ega2AMeEamRGYWvI
+5znksCPJNxSYO/t2ZtxyM/PmzTOlwdfe9thxCt+asDWTlpdpfrtcYWPrGb3niBBcmbWUdyhUGaWi
+VEq4b6Rk1aIO/vSVwxm7PGHMh4LoDYgaI0QhMmjVg9HwHpj7hPaGHrNUlnRkJDNXr/7ElZTWscVA
+rs8ZgICnUDyGYC9wNlFdoCFJnFKglKK9o4v+2ayBbJawTDYf4JSt0ANgWmrCzcv45a9+y4vz69sD
+TgjBmWefy7R9DkAIdIafvZWywT06mMnFHmAiD1WwIAyBW7Ulsp6AYHwakvsFbBGAMAa60WPHsede
+e9XMAHbeaSe2mTRFG9mKZS29DeRH2UQgPAJQUZB0Y6WkHmcigwg95afd+c7raHvuOY3ly5Zx0vHH
+cN/997OshloCG224AafuO409ihGNHZKWt3zykXepeXhvfuh1TfhL7Ao0nyNB3JxlaqvkgO71iV9V
+RM05GB6n3mfaGK2bCPoNrSKhfcGOVy4qcv+Wihumz6thxv5PmgIW9zkDOOSwwztn3jb9WqXYyxES
+oExuvicgQXt7B/1amrV3wGsIrjmk5Viwh9ShXo0Q3Hr7Hdw0/ba6x3vCCSdw6FeOIZvNIGyBLjtm
+Q5y2tG8qZiEo6yWE0Pn/hnnpnCRXbsM9gFJh4RBligIolyabz+U48OAvMXvWHVW3Ag9b/34tfPmY
+Y8nl8lr3T4IimUbiO3hviNgSt/vr4L/JyhP4JB07/6RorKZ2z73zuHfefTW5NDcetSHf2Xs3vtAW
+0X+BBJWkiB16MiG3HCqIM9Us8TtDoO8vkjC4XaByMRS0YdJWUXaXW9WpSr+Q7jv8jgBay3wwSnD5
+P9e6j8D/ZRPAv/pwe/BUmws8C2nh7aL5zPvt6i6asEtrtDJZg8K9Yt1UwIlTdgG9al+c/xIX/vhn
+dQ9y2h67c/x3TqL/gIGgJLEQeh8/87tjYMFCdHfVPzg4b3P9rVSNTAfCGA9B6FqgdvnaBWakdoR+
+/snb78g3v3PiWsd+1FFHs9PUPVBo/3qprOF9OVFOJXC1/UxwkPX92+PSBAyVDRpIknRtADfaOhFA
+kiRrJf4tN9+Mnxz7VebsNI2j34P+y30OQyUxeyZkJWxaUlc2a6ez6yRUu9wb1LHOVZmJb8JNRCUy
+8IwlIH5AdEvKHQm3NJeZ/8q/r5QYwMQtx9PcWKj1dAW88akwgEMOPXy5gD9VvslIl8FBmVJVURTR
+2qaDTKSJpLMt3FLMQ2a7AIy/SsGSZUs5/6If1z3GTceM4fSzz2PzcVsihDSE7+G9XVfS7+aBRTAW
+1luDpMIQsEE2LgHEqhDCL1xnmDIMT3vbJEi9PUg2l+crxxzHFw85pNexT9tjD75x4ik0FgqUignd
+xcTtiScMEtGFf4zkl9Jtn41hDHZbbpsIpBmGNAzB1+13j/5valuPH8elx3+NmybuyLEfCAYuS3qo
+eD1IukIBd8jEfLFE7gjfnOsuC2xIynTk34O/mwr/2Y5E8FtvKoc9lkhKi7uZuXXEb2bMqnNmqrch
+gwZx5D57ctmhh/DHprFsMXrjWi9dAizocxXANcEM4Fgh2NVCdsAUBVFO3+rs7KK9vYOmxsa05VZ5
+nTt8ya57EdHV3cWlv/od77y7oK6h9e/fj++d8X122WNvpDKJL8Y2Id2eefohXCqTG5vR91GuoCdK
+IoWNFdAxAlLpSEClZPA8tg6AcmWvnd3TMEZFwoCBg/na14/n8SceZ/Hi9G48kydP4qeX/IpNxmxG
+Z1eRjs4yMtHoIzHz6jbdtHxS+VRbmUjjxUh7AhIdo6uDhAyFCfPcyTpEJ1a2cZtuyql77sJurTBg
+genPPH/Kh+/gl9f/LMFX2gBSfx1hKqNups8VqXMCNSLo140HjMfJH9aXV7E/GLuCUFB6u5O7pkSc
+OX3mJ6qgHMcxO0+cwN6jRzGllGGT9yUN84o8eXiBF26vOU35buDjT40BHHLo4Stm3jb9t0qxgxBk
+HRQTEUolWq+OIiBi5apWmgoFHWIbR44inP7vrtUWbDv3N958K3Puri91M45jvvWtb3PY0cci0EOI
+tAUvuIePMdDxO8q5IJUQWJdeFJldfUSEN+vhxUgAE/0uvXZRi8ASr33xAnTueEaw+177c+Wf/8pv
+Lv0Fzz37HLlcjh122IGzfngB47fchu5Sia6uhCQhVVwjMbG+usCmvrktuJGY8ls2CEigt9ZWNlNR
+KqI4orGQJYoE3WVJc78WNhi10Tqvg41HbchZ++3JtJWSfm+XvehWdqIqiMpRoH0XAa0FkhwIqDOA
+4BVSndTt0kjDfg5YhxuPQxGh3cf2ofVZfVEkEGVF6YV2bt9DctYdd1KsIVW56lxtuAEHbbctOzQ0
+svUyGPCm1HtqNmRZsUfMH996g1K5pr4T4Go+hUCgVFNKzRZC3ILiy0Ds6+Eao45UiEiXnFq5upX+
+/fs5KauDf0TwzuwL1ltsP/H0M1zym9/XPaYvffGLfPt7Z5LPZrHBPiISzqgHmEq+nmE5IBLU/VNK
+MwZb/FOfZEpdY9dwWCPAuwjNARwBKOEWl1QSURZEGcFu0/Zhyk678sa/XiGOYsZsPo58Lk+xVKKj
+o2z0fmUgvFZBrKTX3ZnKQApT7DO9I285VZVXUWjIMrBfnoEDchRyGd2fEkyYNJkRw4evscBmZdtm
+/DiO23kKn1sJLe8ErjwnYe08BBeldA3PiC0/TRGfWxj6f56IbVfpYCCLAELU4HV486UXeB8yjzBx
+jShCtZdo7yjxlz0SfnHnPTXVKQhbJpPhi3tMZYdBA9i1O8+IjyVRt0Q0xjAwB3GEXNrFo2Mljz70
+Yq3d3gS8CCQi5GCfRpt52/TJwA1KsRkEk263s45ihNBbW603ZBD5hjyREKbevqra5/sffMhhXz2m
+bn//1ltvxe8uu5JtJk1BIcmiiI0UF8JLZtB6c7hqhfA6oF9sliEAShkbh9IMQeFUGBWc63VVzQS0
+ZAaQxoev+4vimCgTEQmIhY1JlJQTRVtbiWIxcYs3VVdfK/VIJSgnJu3a2BrKiX7GxOj4iblXIZ+l
+pSnLoAENNBdiIhHR2V3k9Vdf4sF593Dn7Fk88+xzPSRrtTZxqy35yvYTObg1onmFecduvoKWkuIq
+TfyhUY2AQHtp4U+e+YYfSKExx69DGF/Fo5DS+cNzbWtLWNpP8YtoOdffc98aZqVn22XStuy8wXCm
+RU1suljQ2AGiMYJ8BBYFRwI6ElY2wfFLXuWpF2reN2IspqzYp84AAGbeNv04pfi1EPSzx5TCGQNt
+lGBDPs96QwfpbcSF179DWCeV4oTvnsLTNeaI29ZYKHDFNX/mwEMOQylFjHG/BWhOSeV0PwfvsUU/
+TBIPFi14iKrDgnGLQ6MFPXhb1FITofexQ2CNNzn60iAjt69AJIgj4TZXRUFnd0J3V+L8+dbIpyW5
+hrFK6rp+YGv8m910jP4vpWZ8TYUsDYUMLU1ZmgsZspmYcinhHy8+z9zZM5l9xx28+lptluxJW23J
+sTtOYq+2iAFL0zaDqgzAtkpYXo0oK2C+O9bbsg40Cd1l7ypDKNkr1YRKxhLaCkRJsiKbcFG8lJvv
+faCXgfgWxzFbbTaW3TcdzY65RsYvVAxpjRD5CNEce6IPbB4CSBZ3c/1WgnNumL7We5h2PXC0/fKp
+qgC2KcUtQrAV8E2lVLN29Vn4pXSEYBTR1dXFqtWt9GtuJspmcEk2RrpFUcTPL/nfuokf4IcXXsj+
+Bx2KlIpMpF1+GIORh4gRwtgmXLUfwEJMO97I6aMC7QDELzolEJHf6BIwyTb2s2VkFopb5ib8ltgK
+vT+9gJK9Dr3Lj95Cyxb9lA5dJIZpyMT7+RMH+Y17T2hjVWNDzMABDQzslyOX8RuazX/xBW76yzXc
+e+89vFHjXnjbjB/HqVN3ZKdVkv7vJkCF8Suwn6TCZVMtrZPbv6Ha0IPa/ctJGeoquwv7tKqHU0WD
+saRUB8vIK7pyTCKCZEWRBzZLuPm23olfJzCNZ7cxGzMpbmDrZYLBCwQxIPpnEevFqcSiSkEtV5f4
+18bwuwfrCiW+NvzymWAAXzzs8PaZt07/LYINhRCHYnJ/IrNVFBj9WwlaW9sRIqJfcxOZTGy22tZQ
+ddadc7n+ppo5oWvf/OY3OeaE7yKEIBbSFe2wVTqEtRybTUos8lBgDIDBuqRSktnFJZwurfe60GqA
+jQvQar9FCGZpKb+nvfUpaKIW3lLvJHmwLTbCMRWlcHn9NmRfJnZXAuEy/qSSNGQztLTkGNg/R4MJ
+Eu3s6uKdN19n+g1/5aYbb+Ljhb1vpR228ZuN5dTdd2backnL26UUQXmoHRK7z8JLT191MZ6a56pM
+w019YLztRZOwdhvbrx2n0+fBooJwjCLoO7wOIMrHrKcUcRy7UN84jhkxbBjjNhjBThtvyNhEMH5V
+xJD3FBkg6pdFDRXGE+bBiJ2HlEpSVnSWE67pXMGHa9jevKL9HV0QxDGxz4QKYNvtt07fXMCPFRym
+id4GwxjCi7UqIBQMGNCPfi3NxLFWB9546y0OOeKouu+5/3778vNf/Z4NNh5DJCRZoVLGJZQiEjp7
+T4jIynNjExKOgIWyRAUYw6E3FFpp4bPFlCU+rcMEiTj6kHW3SaWQiZfQFrq7wh1oOC8IJTqmb+HD
+f5W1/IcbbiqycURDIUtTIaZQyNJSyACC9vY2Xp7/AnfNmck1V13F6tbain5O2GI8x+60HQcug6a2
+Nbi6PFquOF6Bz8PjvenblevXK/hpdaFCZIeqR8hsJQVPAAAgAElEQVSUKnMJqp5X8SxpfcAw8u6E
+eRtIXujuIp8kbF1oZNOumOaVCQNXgchFiKxA5DN61/vg2cL7eO3RMiYov9/FXVsknDhrTj1FYvbh
+s8wAQDMBUJcIIT4POBuaNC6yOKM3DIiEYNDAATQ3N9LZ3sGXjzlureGxlW399YbyxyuvYdre+4PQ
+yV0xKl2VNzDqKIybUtpyX9XmLu1ntshFF/4I4KN5oWG9PZTdmUevKBuKa4nWI4bIIYHElNC235WZ
+K7vjL+g0YA3/TV1/43FoyMf075enpTFDQz6DQLCqtZVnnniUeXfN4dZbb2Xp0tpSVadM2IYjt9uG
+z68UtKySAbX0InYriSY1hWnuUI1RWAKpSpD1tIDg9NfeYIL9bv4XoIDUdSEDkaA6yiYzTIeRRZFA
+5GPIpJlMVcNm5YPa1pXwXrbItxf+k/mv1rw5yV3AAf52+gafCRUgbALeRIgLUOSAaUKQrTQGgp7T
+latWIwT86Mc/rZv4BXDe+Reyx977gYBYSKf7CaOro0hxYfvyI2Hi6fHFPI3FIFBHdRCQrvxtdW6d
+6+922pWBYQ/dkS3NhTHKacCg71N2un1iIhD174kL4jG72CpcJp+NAbA19xF6M43mlhwDmrPkshFR
+FNPa3sGj99/N3XNmc8cdd7Cyxh10dpg4geOmbMvUVYoBCxI7SZ5oe9Pne1GZNL8Ubh6Vm/Y0Idpj
+qrL/KowlJFKvwxvmHOj4FtWl7hEaGoWl8YBQg+vs+Q6ACBDNGVf9N+UmTo25p9R3AgS9frwqCcVF
+3dyzrWL+g3XtTHRetYOfOQRg28xbp++s4DfAdk4QK1/ww4YNz5w1i2v/el3d/Z973nmcetZ55HIN
+RKpMHEeOnH0FHz/pdkFay79WC6w0rwwQSsNGXVzDQ37zKN74ZrPywG2Lba3+fptrX8jT5uzbHXH1
+b17nd0k87l56XI2NWZqbszRkIxpyeiONUqnEvLmzmXHLTTz00EMsq2HPPIAJW2zBabvuwE4rEpqX
+V2ygaVuFtLTfKyF21es81QbqGNVRBf7cquqDuywI2qmy7teGJHpFCZAm3mD81RhctT5CG0KojqRa
+LJALu5m/ieK4Rx9iSe07Ms8CDg4PfGZVgLDNvHX6bgguBbbDwNwojox+DC++OJ+LfvLTuvs94ogj
++NH/XML6w0YQCUkm0kYzuymH47sVaNXriLof/T0I8cUf1zH/lpg1F5fGco+y8fhav/F73WP0fVuA
+w8bp6xEkChMOTLBJR0D0AVPRaoB2V+byMf375WhuzBIbFLVy5Qqef+Yprr78DzzyyKM1l/Xeevw4
+vjd1R3ZfklBole6Bq+rzbpJwdFkJ20XFaVCFmCp1gZS+3fPeVfXn3lBBtTEEg+ztudLrIR2t2IPB
+VYP31RhccG4PxhGBak9YXO7i3Owi7n7sySqj6rVtCqTcNv8RDABg5q3Td1XwP0Kwiz2mpGLR4kWc
++8MLWFHnRopTttuOX/7m92wzcTtAEqPI2FJO+FRkYUS1TEl7BeazVQMsjLQ72NhX6Vx3ClSw/ba2
+wOvffLot+rvykjuRpuiIkfi2TqcKbAKpPfssAzDqdxwJoljQryVHcyEmn80gFaxYuYInHnmAW2+8
+gTlz5lCusRjFDhMn8LXtJrD/YkWhXaalVKVxrlqrIEJ/vflfbwiiCuOoJI6e16UlvdErUuCgGuFX
+ZTwVFv9K+0BlRKE/N/gSXJ9ydVoiDPqgl/MEgrYP2vn1mHb+OLeuGgI/Bs7vOUX63p85G0BlU/CM
+EPwQOFYpDhGClcVScaPLr7iqvGLFirrGv/56Q/nWd0/UxC+0Oy+2OpvVvbHQ2bh3lK/4o639lsA1
+VxYqgJNmqy/l/vmKv9rynqBMPX1L5BbeW6hurfhgN8I0VntbviuI1/dw32wpYhZ7U2OG/s1Z4jii
+Ma8Jf9HiRdx/z1zunTuHe+65t+bdbXbbfjuOnLAl+yyTNL2TOMpJBc/YFrrD1iJCU/nxdumHkNf+
+pnCbOtlf65HIjhFU9GFRWeglSBGhCC8yUjnwDFnFMMUElX+XKcbh7isqxhQwgXBsdk5tH1FE8nEX
+j2wBV8xae1BR0JYDa9z04jOPAABuv/WWWAgxDMWYUrk08Sc/+/kXX5w/f/d6+shms5xzzrmcevYP
+ycS64GYmCiSB5fhRlFqIoUvQViry70e4kGBhF4SyBElKB/fZd3qxSGWj8Wx8gJbediPMREoXAZiY
+zMAwwEerAEFBCqVoKGRpbsrQWMjqmvQiYtnypdx+843cNWcWDz30cM0Sf+qUyRw3cWt2XpZoq/7a
+Wiitq0j1lMSvQALh4UoYHl7Xq5phr+0FTle7V/h7Nf27h0QPr0H09DxW69N8TyUgBWgplPLhfVP9
+CVArS7xV6Obkxa/xj9fqMvztD1TNhvuPUQEqWyaT+Wq5XL6+3uu+ftzXufjS39BUaARZ1iG0KCPN
+COagojKPqFhHQhOqL08mzH4GvnaeZQRhhJ/1vbvPBiboz8J81vf2qoAvyKntByYaUHl9P4oiMpmI
+5qYsjYUM2Vgzl7b2NqZfdy0zZ9zG4088UXP66Q4TJ3DSlElsv6xM88qkJzR31OEJoQdRVjNwmWtT
+5wdEsUZ4H1DGmmB61VaFyKuNs5KB9IDj4XjDMVcQdjVmEt6vMrgolUlYMSYEiKJk5aouzh+whNse
+eqz35+zZUm6/yvafygC2Bta6fWpl223qVP5w9V8YucEoBAkZ97jChO3ifPqeawfxfiJy2XoeOgIB
+owD0tlnm/ckg6Ub/DSS2slZ7n51nYWKS2Bx8c0xKhwyU9e2bPIOGQpaWpgz5bITNjFi48GMemncX
+11x5Bc///YWaCX/Ktttw0g6TmfpRmXxHRdkt9zcg4FA/FT2hcM9jhFRVnWirneNgA2sm3oquqjIS
+c12qclPQnUcwlTaEnvUmwnnpgT4qdXjS/VbaB8KPjjCNClR+v4srN23n4jl3V3uaNbVhQK+73vzH
+2ACCNoTqGyOusY0cMYLTzj6HkRtuhFKJTiKy69HqmG4DD6uSej+0VN66L0Bv5eXEkenAMBBbLcht
+oGkWb6jny8RXpvHW/8CoRyDlA2mP0v1FEeTzGZobsxQaMs7bsGDBWzzy4P3c9Le/1rxLbhRF7LLd
+JI6ZuBV7f5iQe6toCMfrqimpmZL2fg4qj1Xq4Mr85IiyB5PAqtT+eHDPkFKqQXOnkgdn9soQepHw
+lte4c0LJrkhXbnKdht975g+EzEHXiQhGFzAG25SZKCGAWJC828HTW0VcMvdh6mzfYQ3EH7b/JAbw
+S2DLei7IZbOcde657LbnfkglyUaCSGnJS2TDeP17sXEamvABaU0CymziEZb78sU7pNHNrYvO6/6a
+qKUW2gY9YIx3OoBIJpZZaOJPEunr8Nsy3QiyWUEu1v77xkKGXBxRTiTvvP0G9945i1l33M5TTz+T
+llJraPtO3ZkjttiMPRYlNL5Vcos6tN8psYYOKlduJRGRJlZ9Wpo4QkOg1yqqEFLwPdWf/V8gxisD
+gzyTSEt1t4FLBRpIEbd5WWHNAdd7D5hvazuIVP+VKoBlBG5cqWfVd1ACWFni7Q0VF//zJTrr2478
+WeDKWk/+T2EApwLH1nvRSSefzFeOOR6BIhY64QKbO2+oVBjJY1RyzceNb99CeYEND9JNJQlEkSF8
+67NXTq+Tyn9XyjITv8OOPuZdd5qJYJCAcuOQRMRZoYm+MUM2jnQoKYLXX3+VmbfczLx77+bZ556v
+mfCn7bQDx03Ykp2WlPT2WVQsUlH5tycX6AGvqxFRNWK08NdRe1pnthcLQfraasaYyu+9fA4JrNoM
+hVGBPSC7QR8hrKfyczAHTo2oVAXWYORzAgXzzALokiwudXFJ95J6jX4Ax6Er/tTU/hMYwO7Ar+u9
+6JCDD+Zbp5xBJpszGX6eU7uWQmRW/EeapI20tuGbziJgJDUO1htJ5gx8JkLPFu8w1znitym91rin
+YYKzFYR5BIWGiObGLNlsTCS0Z+D9BQu47qo/MXvOLF6rY3HsOmUyJ0/ZlslLSjS+W/R71PdYlZ6M
+vb4awGE80aRgfSWR9SKJ0wfTFXjWRKg1txDS2T4rx5AaW2DRDwjcEW1I1HZqAvSSQgSmvx5zYJBm
+Sv0InzVkXkVJsa3E5eu3MfvuJ+p9+guBumqNf9aNgKPQRr/+9Vy09VZb8Ycrr2GrCZNBlclE7r0h
+hN6Sy27nbYN+g2VvWACuAGhkxIEyUkIYqO6rAykXimuhs99hh7TPHv0bSqVCe6UxQkaxIJOJyOdi
+GhuyCCBRCW/96zXumn0HV13xJz74cM3bVNsmhGCHidtyyk6T2HlxmVynhERhBmGqA1VI4V6gd7Xv
+VYm6UhJbLlF5TkB4odrhqM4x5IrrK3TzgAP54/Q8FjKqHrB/DWPuOfTeQ5mrSvfgWMhQHNIJr5eK
+0uJubhovOXv67VXusMb2EjAZqKnm2H+CEbAB+DN1En9DPs9pZ57NFttMRKHDfG3BHOu3DxNCpKnK
+65GvlsaRgEiYMuX2NTkpX7GnnrGOKQPcldTEr8y1FiBoY57uw+Xv28i9OKKhIUNDQ0wuExGJiGKp
+xEvzX+CBe+Zy04038GaNRTgA9t51J46esAW7LUvIf1RCJB6NCNAIIITJ1YRAqFY4mF5xXkir4aq2
+HLdHn7hzvKTEw+VwLBVE6VSDHhSmKojc/tbTxx8a+Jzht5KgA+IPb2WnoxqUTz9jBZJRHi2GPNYH
+TOlzkqVF5o1XnD/zzmq9rq0dQ43EH7bPMgP4b9ZhO+SzzzmXzx30JQSQwUB/ZeW8XhLWqCeldOW0
+tN6tEQIol+Fn35gtoGn1emXy/6Wp12fVA6v/o5SrsWchv0/isdV5BVEckc1FFBpicnFEHMe0d3Ty
+z3+8wN2zZzLz9hm89fY7NT//AdN245AtxjKtTdGwuExkg49ME0YNqUqc9nnX9jm1wH0EpSdcglVO
+mqDC36r1F0h4LzEDRhX2D4QeA/+UIYmGt3F4PnV+ykAYjCsEGCr1AKTHGsxPOsxXOEboGJE5Vslw
+1OoST46Fs+Y9QLFU7DH2tbSL0EU+626fVQZwKHB2vRd984QTOOaEb5PL54jQde28oU9H/+my42iJ
+E0UuXdcE+vaEiUHJbLckDBOQKMMIolQFHgXIxH7WB3wBD40a8tmYOBuRy2XIZgSRiCiXE5575jFm
+Tr+ZWbPu4IMPaoP6APvutgtfm7AF23VImpZLWwBQMztzjjL7A6Aq4KhtVeim6nlVGUPlhSlRl5aI
+VQxolcTkpG+oGlSOzBJSBUMJIX4quMfq8eExe5/gb5U7Bf0obPwHFX0oM2DNJ3oGH/XIA7Bjbi3x
+2qiIH/z9aZbXuEFq0J4Ffl7vRbZ9FhnAtsCt9V609957ccJJ32Pw4KHobbyM9BVGBiuNBKwAsPX8
+Q27tbANC2AreHrY7I51yUsll7dkCHOAQgEQH7+gyXp7zR1FEPh+Tz0XEkU5BLsuEl+Y/x7VXXMa9
+995bV3nt3Xfcnu/uMJGtOyWNKxOEyTAUFtFYvmWLGAvzHAAh04PqxF/leKql9Gd7UVpdWFN2nL9Z
++nsKyofYvpqdIlQVeujn6e8alPjzlfClv8JHDe8fohCnvlSoEfaqqvYBpYJjvh8RCeTqIu8OVpz3
+xiu88c67lVfW0o4B6it/HbTPGgMYDvyt3os2GT2ab590KmPGbo7CEH8AO30kX09hopTO4rOcWZnK
+OkQEUtQwDemZipX4NlnHrntFGPYrbBdEEeSyMQ35DLHbAr2DV1/+B7fd9DduuvEmlteR2bjbjtvz
+rZ0mMaktIb+qrAkfPwjtyQggiVmCIswf6MWYVZ2Qqp1HGvYH/YYE26OfCoLrIcUdAwnvE9wkZRuw
+96tgDuFN7fNUnlONmUCK+FODTIGdCshvxuFuq3r6/FPPK0CuKrJkAPys7WOe+cfLlbNUS/sS8Ik2
+GPwsMYAGNJSpK9inUGjgOyeexJ77fg4AoXTNAGEks650a6dfp+uGRkDL4lNLUnnrvFUREgnC+vfB
+GfaUAiV9KS8L822uf5yJyeVi4kiQz2rCb21r4+/PPsW8uXP48zXX0FZjLr4AvnbI5/nSJhswflVC
+tLrsjXtSGalvnsd8tvLewdg1RAZ7eqsS3gopYqm6qFPneGJLwe1KHd8er2KT6AGhU/epkL49iDn8
+LNL3qEAPjgHZ8QjR01ZRwVN6zloFvxLCz1+AjJxwWV3mo2yJH3cu5s5HHq/a81ra/wJ1uwoq22eJ
+AZwAfK3ei4752jF87fhvaymLDvXFEL5C1w60f6U0tfu1IoiN7POCzJfSBv2DT9yJkNhiHF7qJ6bC
+rk0CsqpAnI3JZSNy2Zg41jp+a2srzz75KHfeMZMZM2bULPG3Hj+eiw/Yg83aNcQXrYnNP/JCKRKa
+GRhG4KCHst/1v5Q+bz5bQnMWdrvYe1jHPZGlDNgQEEuFFLbnGoL0ejieKHoQv5WudpxVkISoeJbK
+HkLCThGjIfRA9RPB84baRkrHN/2kVIRgtOEj+cvT6EgITfxqZYnFjZJfRMuZ9cA6Ef9TwDnrcmFl
++6zEAUwFHqn3ooO+8Hku+f3lDF1vOCDJYKz6JhFHRL5sl2sGEciAO4cS0hK9XTw+Oy8spR3E9zvd
+H8dgsrmYbFZLfRB0F7t57MH7uH36TcydO5eVK2urtzd0yBB+9rXD2KmkobuQikgBiSIyhB1JhUoU
+sQIhJULizhGJvoYEVFl/VpUIILRWB4u8VwNhBYyuLv17XieCU3pC+yothPeVxwNEoA+FsL/yhqRv
+XoEUeh1/wBzS90j333MMntH0SPcVoFaVWJwrc0nDCm6478FeHn6NrYSu8PPeulxs22cpDmA4MKPe
+i7abPJnv/+AChgwdBkpn+Am3CAAhDJHjIL0QegNOaSWM7cwa8wJeIc1W2rbunt0G3HoFULY2n+5J
+CEEunyFnjHuW8F/6+7Nc8Yffce+999RcWlsIwbePPJSjN1if/kWJqfCpR23XqMSpNlYlVQpv9Evx
+PdXD0G6fu9J6rkFDBSGnFnEaRqeJr0rfwhOZHWeKj1Seb+mrh+EwePgQEfTQ/dPE6ZZDxdgrpbaV
+2H75iPQ09mYvsAK0Yn5EOE8aQKJWlVjRpPh1fjU33L1OxA/wBT4h8Yft02YADcBf0Zl+Nbf+/6+9
+M4+So7rv/efe6p5FI7QjWVhCkhFIrAbMjo8DAp5ZHPs4BDuJ/eJnMMSEyHbsE5tw/JLgBOzYjl8I
+mM3GEDAgCYGQQEJCRgsYCySwyQFsbFaZTZoZNBqNpFm6q+77495bdau6eqa7ZxPq+p5T0nQtt251
+1+/7W+7v/u64cfzNFVdw1DHHI4TCA0LpQJjlt83YvxnztjMA9RmRWaiULe2lQgGx8/yt5WyhlJO8
+oxRKSKQn9HBeThfgEELS1bWL32x5mnvvvJ2lS5dWXIQD4OzTT+XvTz2OGX0K2ReAH4RZi0qZ0gO+
+eRItrXqYT2F8fuPaBJHpH64zGn1FGqnBL1cLmnPLWQXJNhzt6QpTZMnHTfm4SR8XsCSfhCo07Z5J
+39/GcRwCKmlqgH2pUf7k8yWCi6F7keyXMfu3NRf5vmhnyeqqjV2LKyhT4KNWjDYB/CNwdrUXXbFw
+IZ++6K/0B6UQUtnwHuD4+Dg/rNBBQSFtSS67RdlZNsBnJ+Zg95m0Xj+I/FnheeTzkpynNwV0dnay
+6fH1rH1kJXfedVc1CzYwb+6HuOzUEzlr8hREwdgoCb/VDmfGDO9Q45tnVqHMhvvtPIdyyjqGmEXg
+Ci/lNSvEpcz1nXEaiAlu2r64qJa7PtwfWhlpboeIfYw+RNO2k8QUD/qVWkAlSJ6bdIkA4QmCbT28
+3VzgWtpYvq7q/H6Lu4Aba724HEaTAD4D/FPVF110EZf93dcQngcE2vRHV/PV83bc/H1QplBn6JPZ
+IJ8tuGGExgpIEAR6UfBQkehrikogpUkPFjpfP5/ThTg6Onbw+Pq1rFq+jBUPPUR3d0/Fz+N5Hpec
+cyb/Z/5hjNFlhhBCaH/eCrZyDByT2ScTUiyU3ZwgoFm7wBIeKpqUEiJNsMsE3WIBQufykvhArC3z
+j+tbJwTHva8bvFPu8YRVktwXI7akrLr9Spj2+t6lQm8fR1k2de/r3qCMtWNJKHi3h9en+vx7bxsP
+P16z8K8BLqn14v4wWkHAI4GqBz5PPeVkbvjpHcz50KHYyj7C+WFtma2IifXEHjtELoSdkefU7FOR
+uWdn9vm+D0LqSrwm5VdIgZeT5MKViSS7u3bxyEMPsGrFCtaufZQ9e6uat82Zx32Yb5x2Egc1NZlh
+O4VUgQ7cmaCfQAf6hEJ/9rXwW0G3gb7wHAUUA6RvHrCoiUD5yrgOAleYB4RjNpS1HlwCIGE2E90u
++XdSeMK2Er5+SV9K7lvmWZLkk+xT4tnCS9z72bMdkix9ZPd7jb6DoLWXVz+ouPq9razbXP2CtQav
+AscwiGSfNIxmEHAMNVT2OXz+fL79nWuYNWcutqCnjeLr39Axl5X5Scwc3sjEt8JvZgCqaM5+OGnH
+TtM10X0htYnv5bzwJeneu5dfbniMn91yE+s3bKBQqG4OxtyDZ3Ll2Wdw7JRJCFv7CzTZGEIK10AK
+zKi80hF+bc6ba3wVf++UFnaJc06AjoMkzGKXB1yU7HO0X0lwMIQVABF+coUyZlVYoXJN9xJBdoQ/
+YTHE+hd2pQwDGOuwhLTC+IAIO+tmB+pbpUwUctyHeIzBeW6pP/lbu9l8iM+/v/Mam//n+ZTvrCJs
+Bz7OEAu/i9EggOvRtf0qhpSSiy+9jI+cfDpCgGfMMvuuYIXC/qlM9J9ofB4gnLmHiq3Go4iKeth3
+T0oZan1PeigUnTt3sumJDdx75x08vHJl9DJXiInjx/P1887h3FkztJa35jpoATemu0QgVGAiGoYQ
+TBwiYWlrUrAypQQyYjRipo9SceFJEeaQSER03zCY4C6BnGgjNVkoqTETUfE4IUQkU9JmQuhTLYkk
+kaTBsWBiFq9ImPTOfUoSmFyCS8YYFDrBpxjQt7WXjUcH/POzz/BGFfM5UnA6iQU9hhojTQB/C1xc
+7UWXXHIJF/3l52nI5xFmim+Ygmtr8WOTffRqOEGgwqCZFXW3PqbVtGE2n2MHep4W/pznAYL32lvZ
+8Is1PLZmNYuXLKm40KZFLpfj4nMW8NfHHEGDwBQgBVtBNBRa+0ontZ8lC0Fk5ps2DJ2FQUMr6GEz
+oC0FI8P6QHQ85v87Jm+oqKPTI5PYcbvAEq6ICUlkeembhH+HnY5r9bBbVuAd98ClWdufkn1p8Qf3
+nEjZl5BEeL7zfK5x4dYDxD03RogK9voUthe4/8NFvvf4k9Us3ZWGMxhm4YeRJYA/AX5c7UUXnHcu
+C7/+TSZMnIxSPjlr1kOkndDaz87td1/IwH2pHbPfXW9PZ2gJENJk7Qk8Kdm27V3Wr13Nww8+wOrV
+a6oazrM4+6QT+OrpJzG1qREbVdLEpCceESg9OoF9LjOWYX18Zcc2CF0BzGeUHpKUKtLwmHZC8zck
+q6S0W/PVEahklJ5IC6dZDK4FlH59tK/scUplMqntSR6LEY2hvzLWSUkbDhHFvA7i5n5JvULKWB2g
+3529Prv7itxzrM+/PfCwWcOhZpwLbBxMA5VipAjgEGpI9jn6qKP42reuYsasOeiKvoBSuj6f0HX7
+7Kw9QGf+mXn2iED7vtgpnMbfD6IXJlAgPQlS4Hna65ZCsHPnTlY9uJRlS5ewYePjVQ3nWcybPZtv
+nnsWh08ajzQWhsQRcGu2YF/GeC6/mXGE9WItGWhtarR/YD3caJovoC0G32hR+/wJIQ4t8UofKM33
+HyB4XD5mEJ7gaHwcfyHR35DQUjR/iuDHK/Cm3zPsn23TCr9VHvb+5lhkJZT2U73Vw9YDi9wysYs7
+lz7W73dSAT7LEI/194eRIIAx6IkLk6q5aOrUA/nqN77BcSedCnpgDk9Gc/kh4ZcJ408rK/DOC64I
+l8lWSGy2v5QCL+eZ31SwZ08Xj61ZyW0331zVYhouJk2YwN9/4uMsmD3TRPEdc9/V4HaJaWXJSYUW
+AKanYeeVjVKIUNCVG/V3pELYF9S1AOxj2FiAo+nSzOWK4apFkdhXCWLWAZFwOi6GeyxZKDRtAlGq
+4KcEEoGS9qzgh4/guiAOWYe38qH4TjfPzQ34Uesf2bDx1xU+eFl8EVgy2EaqwUgQwELgk9VckM/n
++ZsvX84nL/xLpNBDeZ4wfr0AXdcvqskHVk/qcl1WM0SBvigApoS2FKSQZmIQtLdu41dPrGfxz+9i
+9ZpHa3rIxoYGLjv3HD714cNpMrMGpRF8FQqqTT4wbov17dHHPcMP9jNEAi7Ct1CFIwM6vyFxjtmU
+71gErkwko9plTOfoM5FwOm3EkJS5pCBDKeE4yj15fkjwzvWxflvtnBDKtPuE74KKD3cnuSsm+E47
+YVvCeVAB7C3SEwSsOU7wvSe3sHVwwT7Q4/x3DLaRalEpAUwDJlZ47m708EUBLfjfq7ZTF5x/Hp+/
++DLy+TxYv98g+oETEy4UTpQfOyIWWglK6B9ZShmm1ra3bmftIw+zZtVKVjz0UNVRfdCjBZecdDLf
+HDMLMbaF1mJAocEjX7SzDbXJjtXeIRMp3OCdEHr5L2lfbDuSoQjdHWIpvSr8X7j7VNo5pGrl1OIV
+jsmdOBAF8hI+eMyysIJSxvx3fWtX+JL3SsYIUvtkrRzTD+X0KSwT5pBLxGVuwhG4AWCXKOy5KvQ/
+zHvWUaBtguKGhg5uWzxokx/gMuBnQ9FQtRiIAPJoDf4fVbb7MHALsLzaDp1+2ql8++prmTZtOr7y
+aRAKTFFuIbUQKWPEi3DZbmH3YBN37EKarj8opcQTktbWbTy+7lEeXHofj6xeoxN/akBLczO3nX4W
+H21tRjRLxLvdTAsCdhw8jp7GPF5gTPrA+Ot+2m8AABOkSURBVOhEmhtlLXV7TAuknYporQDhCnUi
+IKi/C+IkYNs2gmyHNi1cDRxb+MMVsJjFEP1fooHt3+Z/13UvGSYz/UxemzSvI/5IEEhJDMBxH5Ix
+g9h1InaF7lq8BkDy2pA/k99BnyLoKvL0bJ9btr3J2vVbGAJcRhULeQw1+ssEzKMFf+FIdWbmzBlc
+f9OtnHHWx/H9Ig2eERYhUcrXAg+hEOhEPZvYAygVzpPRQT6lS/EAnvTY1bmTR1Ys4/4li1i/YUNN
+wb0kFpx0At+afDBHviWRUxtRfQGFyXk6Zo9n79gGZKCQQaAz9YhcAWv+e+YllMZXF0pn9el9EQlI
+31wfKD1ByDfkEOipwPiEmYLKlv4Os//MS5/UomW0dKrJnkSa8JRpI7qGaG+K2R6LT5SxMmzroftg
+mxSlf8dM/DLPpptXpaTg9FGvtlqkYyw8MKmXGzY+SWtbe9q3Ui0+ywj7/BaVZAKOqPALIfjbv1vI
+R888Ry/j5YX2rzHRtCBbswwwv64My3iFC2qaWnvS06b+3j17WLtqBbfe+GOe3lz58lmVYN3mZ8if
+Irhy+kzmtvXhTW4g31FkSm8Huw5uYffEZpTJ8bcFSGyKb/jsNhoulA4OCsISX5GFoAVboDP9pEQL
+PYQJP+53Y/1meyMV7ox96f0+W1I4w2sSGr5kzTzShCx+z1Lz2rkuodFLyoU7rl54VfiAEQOUaHJr
+Mbn9TVOALvH0Kvxdfbw0W/Cjtj+yeunmku+pRvwZQ1DRZ7AoZwH8K/DtkezI5Zd/mW//2/dpbG5G
+osgL54VDZ8cFjuDqH89Mk8UspCmk9vMNWXTsaOeJDb/g/kWLeOjhh4e1/2d85DiumjKbw9+WeAc2
+6j4Kwd4PNrF7agt9eYkXmEIegTKZfgKCwHyOyEEqwhz/kDACvWaR8OPzAkRgj2ttL0ycICwA4hOX
+wjIaOzVgaGSpRJsnzX9jWsS0ciWwAg4JUo5u6oYkYhqflGcog7Sz+rVqpIBioKfwHhCwbkqR//zV
+07yzraL1NivBn6Ld5FFDqChSCGAh8F8j2Zn/dfbZfP+6G5g5+xCkVGaGHyaSLmMJPtEDRAU5FYJA
+CHLG3H/vvXZ+uf4XrFy+jPuW3j9iz7HgpBNYOH0mJ7yZQ47PaW/fVxSnNLB7ajPd43QykAwCU9FH
+ha6AnuBjXQB0SrCyQm1zCBxiCJSe8GMmDlnSsBN/RGCPJbSq+wWmmO32UL+uQtKUtxckiaZSlOlb
+sgupsYxkO6ZPbqwj7Tr7fsWX/DKuUleBoKB4ZqbPT1rfZtWTla22XAEKwMfQJb1GFeUI4ERgyGyc
+SnDo3EO46ae385GTTgOzkIddpTec+GIX6wii8XD9bguTFCTxhOC99nY2rl/L8qVLWLnqkSHx8avF
+vA/N5spjjuVjb3g0NeWgxYMCqGZJz9RG9k5qopiXmgD8wGT8GU1uCcGU9YqsBRWW+gqHDi0xGP/f
+lgYTisj3N7HNVF88KZ9p+ypBJcLe3zklZIOVTiJLoDR3wZ4KkTA744qhYIdGintusg2l9EvX6+O3
+Fdg2OeD+yX3csWkz21vbBvoGKsV2dHrvoKr4DhXSCOAA4CF0yu6IYOKE8fznj2/igk/9uU7KESCx
+i3c4a+8pwGh7beFaH07P++/p6eXRlQ9y93/fwYaNG6uenTfUOOgD07j05BO4sK2JSb0eYkIODBcV
+x3r0HthIz/g8gYn6e4YEJCI05cMpv1bIg0jz21qA+NHfwgT9VFHvC/MAglIhSCLSlnFBCy8qIYtI
+svoNGIaqtfT8mNCWaTutf/2RTUnMYoA8BITWLcoH1dZLV6Ni8yy47fXXePzZ35S9Tw1YB3weqHzB
+h2FGGgFcDNw2kp341pVX8tV/uIrGpmY8oSf5KIhW6xFmUQ3zUvoYVjdvZHfPXp7Z9Ct+ctP1rFr1
+SE2Ze8MFz/P44jln8rncBOa+K5Bjc5ATCJOpXByXo2dyA33NOZCEc/u1lg+QShcFISAaQbCCXgxC
+gpABqGIQZQX6xgXwo0InQDyoZvpY1g8uY46XaNyEcJdtK6ZyEyRTZjQhHjxMb6fUTXBVvoq36XQ3
+HEbyFXT7FDoK/HqeYll3J/euqy31ux8sAr4MVFYJdoQQCxaja/JtoMqa/IPBZz9zEf/yvf9g6gcO
+QphkH0/o0tvCjPvbKHHRlOGW0gMBu7u6eG7LJhb9/C7uubfq0gIjiuMPn8eX5s3n7LdztCgPDsib
+VcoUSChOyNM7IU+xwUMZIvAcPz/0792iIAGxCsC2HLgKFBTRJBMu/53wc10kbeR+LPmSQF0sAJiy
+Ly1mUOG9qokhxIki7GipJeP2MQhQu4sEe3zenBLwwPhe7nnqGd7d3lrRPavAPwA/HOpGhwJJAvgL
+aijSUStOP+00fnDdj5l/5NHoop6Bnn+vojn6QaDwlSJAZ+5JIdm9u4tntzzF/ffezYMPLqu4yu5o
+o6mpiS9/fAHn9zVxeGsO2SyhQU9CEkVFIAXFcR7+2BzFJg/lCW3a+0Fk4qtI6HH8f6FAFHVcgCJR
+4M+6AKkh8BRNmYYU33tANyL5OWmKJyyKuEvg9HcgwhpofzIgqLS5H+ws4BcU2yb6rBzXy7Lfv8zz
+L/2hzFPVDB89xj9yEegqkSSAB4BPV3qxsGOzoXZRFY+tz5k9ix9edwNnnnMeoMhLsFMurLnvBzbA
+B3npUSj08cuN63hgySIeXbN6KAMzabgKuAE9FPrVoWz41GOP4YJZMzl/VzNTdoDX4iGaczojr6DA
+A3+sR7HZQzVKgrwMI/9a0weRJeBHowW2PoAqqDAByJRJiJAqUMQkMD2ynjjmskF4TqmbEBskcC5x
+Tf+onkDiAgdJooipepHWLukksruI31nk1RkBT7YUuOeF3/HbKpZbrwLPoYV/yFllKJEkgG50ie6K
+4NbhcxsciARaxozh6muu4QuXXkHekwgCU/pK/1LavbVsrrffPv8cP73xelYsX05H9SunVoPvmq3L
+fM4DN1NDAZP+4HkeZ514PJ+bPoOTd+Rp6QaZE9Dk6UVNfABF0CT1lhOoRomtay6dBT+kLRdmo/62
+/l8QSX9qYG4g7a9SpDdsTMXJ3z1U5pn7O5Z6z5Rj/dUZLGlHagtKFRUUfFRnkVemK54YV+Tu557n
+pVdfH6g3teJG4Gvo4b59Gi4BjCV66QeENGPtKvkiKBu0Sw/ECSG49Etf4p+++0NaWsZiF/NAGHMf
+gc2UD4KAP/zuBW6/9UaWLF5M566Ku1ctetDa/kekR2jHoxcr/cRQ33jK5MmcOm8uX5g+k9k7FFN3
+mwVFWjwt7Eazq5zQJJAX2m3wtOaTNgHICn/CCuhX6JLWQJlAXHishjH9VJcgGTh0BLsiknDOSz1f
+gPBB9RQJAiiogJcnFlnXXGD9K6+z+fkXK3+O6jGqOf3VwiWAT6CH/waEFf5kIy7KuQQLzjyDH/zX
+jRwydx6B8smZGXJFRSj4vu+z9bVXWL50MT+59Ra2bR+yzKskCsA/o2dgDXST8WgXacFwdGTa1Kkc
+cfAHuXjuocxt9zlohyQnBYz1IC81CZiqRcpkAylPIHICPBMFNxpfWSvArf4Lpdo17bMjkJBitqeR
+RjIy72CgGEC/Gt9to8QFca4XjhtQVKiuIkUBew6AF8YXWbqznRfffpcX//By2XsMAZ5AC/8+Mb5f
+KVwCWE6F8/WT2j/5t/vZJYGJEydy171L+OifLMA3lX1MgDtctPO1V37PQ/cvYe2jj/LU008P4aOW
+4CrgbqpbXulg4HaGiQQAGhoa+OgxR/KFeYcxvbPIrHZBy24Q43LQrEc/bA1B9+UXxiJQ4KwF4CB1
+aK1M0Cxl3HxAQS03bm9Zo1zqcYVtYZop8eutxbmniNpRYNdEQdtkwYvNPne//AovbX2THcPrMgL8
+X3SUv/KFIPYRuATwDnp9vgERK8BRRujT3IHm5mau/f4P+MKXLkevrad/RU9Ktr39Jsvuu5f777uP
+Z3896Ioq/eE76IrEtU7jOhgdEzhvyHqUAiklMz4wlS+edgqHyTxTOgrMb8vhIRAtHqJZuwjKnQCU
+aMOVleR+VUZzl8YKnBBAP25B2nVuByppw73O1fThu+SZQpB2dKMQoLqK+Ht8Xp0Db0/O8cudHaz6
+nxd4t7WtptqNVeIldAGPmlf6GG24BLAXaB7oAmlW2u1vEZGk2e+SwIePOZpbb7+LeUccha8UhUIf
+yxbdw113/IxNTz1V8ShCDbgZ+BcGNvUrwST0PInPDUFbFeGsE4/n0/MPY9LeAgfvDJjV5unwaIun
+YwJ5WXJNpIHTdvYzaSdhXpcQRjK0n4zCp2n95PVlNHySKAToNfWKRuB9E9/o9HlvsuLJKX20j2ng
+0Zde5sna6+7Xgu+ilcn7Tuu7cAmgohEA1/xPQ5IY4hN3FI2NjXznmmu55PKv8JvNm7jh//2Q5StW
+1Nj9inAjOrg31GM9k4BvAt8a4nb7RS6X4/xTTuRjs2cyZk8vH+hWHN7ZwAGdCpoloslDNEhUzh2i
+JVoOvJwpnxRUd1/y7+Q1zt8xH929jyvcDlKDhPY2fQEUFaovQDUIelEUcooN43p4rtDN2909rHqi
+tpqNg8AW4CvsAxN5hgIuAXQC4wa6oJz/78I1/13YH2rBmWdw3PEfYfHiRbw1+Bpq5XAjcB3DOw7r
+AX+FXtl4VHDUoYdw/vHHMMPLM2ZPH9P7BId0eIx5q4gc50FeQJNENOq/hWeyAV2BTMYULJLauh+T
+P2YllNmvZ2frK5U7QqF0+FcVAlSPD90BSsB7E6EvD4UxksdUF1va36NbSJ598Xd0dI5KRu1X0Gny
+w7ZCz0jDJYDfAocPdEHS/w8bSMQCkvuAkWLq69A+/rAvpuBgAdolGLEU6jR4nsdx8w7ljCPnM6Op
+CdW1h/EFwbQ+yYw9HhPe9JENAhokssXTFdakQDR5eoQhJ1BeP0E6Rb+BvJKYg12RSDmfrXIomuhv
+b4DqC/A92D4FWltAjm2kKy9Z9PLLPP/WO+zp7aO1vb3mkm1DgJ8BV1NdwPh9AZcAbkCvOz4g0jIA
+k8dt48m/h5EE7kBHY98arhsMgGnAtVSRMCSELk46nC/2uLFjOWLOLI6bM4ujpk7B29uD1+czts+n
+0YcJfYIJ3bqI5+R2kF2+zjHwBHJMRBJIEFKYasoAeh/WcPCE9tNtAg5O/MFmJBYVqjfAb4Q3Jgf8
+sdGno0Ehm5vo8SQbX3uDTb/7PR2du/aVCV3PoxN6NhJOqt6/4BLAx6hyFZI0d6C/myT/HiL8Au2H
+D+vQQRX4CzQRzBnoRCkljY2N9Pb2jvgLP2XiBBobGpg3cwYzJ07AE3DKQdMZJwQeAk9B0LUXL9B+
+Tk4JvAByCHIBeAo8M0MxrySiEOB5Eqn0JKbORsWrDQV6lOKNYje9QLcnaGgeQ0EIXtj6Ji++9jrb
+2we1bNZwYTvwDfQw8X4NlwDGUcNURWsNJBt0j9cyV6ACrAGuQQ/B7GvsPA0dIPz6QCemWVCjCWuV
+CKGHZ3P5PDnPw/M8cp6n6zWYlZKl1J9znkdOSnINDTQ1NdGYz7NnbzdtO3ZQKBaHY3bdcOFVtCV5
+PfvYtN3hgvvuCWAXhBPxqt6EEEpKGdvcfYNp29keR2ctesP0nQwlTkMnWA3Vs2fb8GxtaLIen/4z
+7r9IKuUfMURfqhBCCQhJYAjafAEdbMsPz1cxrPg08CSj/6JnW3x7B12kY0B3bX9FkgCOZPR/lOS2
+B/jfw/L0I4sD0Muir2b0v9N6334PXIh21eoaIQE4LHAho/8DKXRZi6vRgrM/oQn4DLAYnXw12t9z
+PW2PAadQ5QK1+zPSCADgUkb3h7oVvZT4/o7T0GsmbmL0hWN/3baik3fmUEWti3pBOQIAvSDIaPxg
+Fw7Po+7TmISeV/BzYBujLzTv920b2tX6DJnQ94v+CMBDa6eR/OFuH6bnfD9hPnpRljuB1xh9YXq/
+bLvMd/aPZL59xeiPAGBkSWA1MGY4HvJ9jPno4anFwJuMvpDta9s2dNLVQuDoGr/juoYlgP5WBwad
+3XYzwzdOugU4hzpJvqgRhwFno1/0A9HkMKpzD0YJG9E+fSfwIiM752O/Q9m1AVMy+qajg4ML0esH
+DBW2AOcrpYZkneVa0F8a8z6MacAFwLFocpiEJujDRrNTw4At6Oy8F9Gz8F4Fdoxmh/Yn9Lc4aDlM
+QUevz0OvcTZ/EPd/BPhraq/OkyGOJjQhnAgcgSaDYStfNky4GbgPeMV87iSzDIcNtRCAi/HoElkz
+0MMs89FTitN8eTdfP0AHbBaxH82t3geRR8dxJFEG5ZHorMSRwq+JV2EqoAV6K1qrP00k4D76fdjn
+y2nvLxgsAWR4/8EjIuhGdB3IA4gThWf+bzT/N5n/3a3JOSeJd9FTaduA3cDrw/MoGQaLGAFkyJCh
+PlFaUTJDhgx1g4wAMmSoY2QEkCFDHSMjgAwZ6hgZAWTIUMfICCBDhjpGRgAZMtQxMgLIkKGOkRFA
+hgx1jIwAMmSoY2QEkCFDHSMjgAwZ6hgZAWTIUMfICCBDhjpGRgAZMtQxMgLIkKGOkRFAhgx1jIwA
+MmSoY2QEkCFDHSMjgAwZ6hgZAWTIUMfICCBDhjpGRgAZMtQxMgLIkKGOkRFAhgx1jIwAMmSoY2QE
+kCFDHSMjgAwZ6hgZAWTIUMfICCBDhjpGRgAZMtQxMgLIkKGOkRFAhgx1jP8PT31pT2946Q8AAAAA
+SUVORK5CYII=
+"
+ id="image10"
+ x="0"
+ y="0" />
+</svg>
diff --git a/activity/activity.info b/activity/activity.info
new file mode 100644
index 0000000..3903422
--- /dev/null
+++ b/activity/activity.info
@@ -0,0 +1,7 @@
+[Activity]
+name = HacketyHack
+service_name = org.hackety.hack
+icon = activity-hackety
+activity_version = 1
+exec = ./hacketyhack
+license = BSD
diff --git a/app/.gitignore b/app/.gitignore
new file mode 100644
index 0000000..d8e4748
--- /dev/null
+++ b/app/.gitignore
@@ -0,0 +1,4 @@
+*~
+root/Home/*
+test*
+.*
diff --git a/app/LICENSE b/app/LICENSE
new file mode 100644
index 0000000..43f3180
--- /dev/null
+++ b/app/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Steve Klabnik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE. \ No newline at end of file
diff --git a/app/README.textile b/app/README.textile
new file mode 100644
index 0000000..3e4583c
--- /dev/null
+++ b/app/README.textile
@@ -0,0 +1,94 @@
+h1. Hackety Hack (for Mac OS X, Windows, and Linux)
+
+ Hackety Hack is a programming starter kit. It's an editor
+ with helpful coding tools and built-in messaging (so you can
+ pass scripts to friends easily.)
+
+h2. This is 1.0!
+
+All the major pieces are in place. Hooray! There are still some kinks to work out, though. Nobody's perfect! Please "file an Issue":http://github.com/hacketyhack/hacketyhack/issues if you find something.
+
+h2. Building Hackety Hack
+
+h3. Shoooes
+
+H-ety H is built on "Shoes":http://github.com/shoes/shoes. So, you gotta get Shoes first.
+
+*IMPORTANT NOTE*
+
+bq. Hackety Hack depends on features that are only in the latest version of Shoes, "Policeman." This is the third version of Shoes, there's also "Rasins," which was version 2. If you download Shoes 2, it won't work!
+
+Now back to your regularly scheduled instructions.
+
+There are two options to getting Shoes: download a pre-built version, or build it yourself!
+
+h4. Pre-assembled Shoes
+
+You can try downloading the latest version of Shoes from the "Recent Builds Page":http://wiki.github.com/shoes/shoes/recentbuilds on the Shoes Wiki.
+
+h4. Some-assembly-required Shoes
+
+If you like living on the bleeding edge, or there isn't a Shoes made for your platform, you can check out the "Building Shoes":http://wiki.github.com/shoes/shoes/buildingshoes page to find out how to build Shoes on your platform.
+
+h3. 'Got Shoes Strapped on my Feet
+
+Once you've got yourself a pair of Shoes, you'll want to fork me, then clone your repo:
+
+bq. $ git clone git@github.com:YOURUSER/hacketyhack.git
+
+If you've got your 'shoes' environment variable set, you can just run Hackety directly:
+
+bq. $ cd hacketyhack
+ $ ./h-ety-h.rb
+
+Otherwise, pick 'h-ety-h.rb' from the "Open an App." menu in Shoes.
+
+You can also run 'shoes h-ety-h.rb' or if you're on a Mac, something like ' /Users/steveklabnik/Documents/src/shoes/Shoes.app/Contents/MacOS/shoes h-ety-h.rb' from the terminal.
+
+h2. Building an installer
+
+If you want to build Hackety Hack as a standalone app with the installer for your platform, you need to have your own Shoes built. Then, get your directories lined up...
+
+bq. $ ls
+ shoes hacketyhack
+
+And rebuild shoes, while pointing the APP flag at your Hackety directory:
+
+bq. $ cd shoes
+ $ rake APP=../hacketyhack
+ $ rake APP=../hacketyhack installer
+
+That's it!
+
+h2. Acknowledgements
+
+ Beneath my wings are many winds.
+
+* _why, who was quite the lucky stiff. Without his work and
+ vision, Hackety Hack would have never been born. Hopefully
+ he'll be proud of how his child lives out its life...
+
+* Yukihiro Matsumoto, whose Ruby language
+ is the heart of Hackety Hack. I adore
+ this language. Ruby's shared lib and stdlib
+ are included under the terms of the Ruby
+ license.
+
+* Sharon Rosner for the Sequel lib
+ (http://sequel.rubyforge.org)
+ I use a fork from an old version.
+
+* Jamis Buck for the Syntax lib.
+ (http://syntax.rubyforge.org)
+ Live syntax highlighting.
+
+* Alex Brem for help on bloopsaphone.
+ He just started hacking away. I like that!
+
+* Numerous font authors whose free
+ offerings are included.
+
+* Fela Winkelmolen, for devoting an entire summer to get Hackety to v1.0!
+
+* Everybody who's been putting hard work into Shoes. You guys are awesome.
+
diff --git a/app/app.yaml b/app/app.yaml
new file mode 100644
index 0000000..5c74a30
--- /dev/null
+++ b/app/app.yaml
@@ -0,0 +1,13 @@
+name: Hackety Hack
+version: 1
+release: Material
+icons:
+ win32: platform/msw/App.ico
+ osx: platform/mac/App.icns
+ gtk: platform/nix/app.png
+dmg:
+ ds_store: platform/mac/dmg_ds_store
+ background: static/hacketyhack-dmg.jpg
+run: app/h-ety-h.rb
+clone: git checkout-index --prefix=dist/app/ -a
+ignore: [samples]
diff --git a/app/app/boot.rb b/app/app/boot.rb
new file mode 100644
index 0000000..40b98d4
--- /dev/null
+++ b/app/app/boot.rb
@@ -0,0 +1,32 @@
+# requires and initializations needed for /h-ety-h.rb
+# more initializations are in h-ety-h/init.rb
+
+require 'hpricot'
+
+module ::HH end
+
+def HH.anonymous_binding
+ bind = ::TOPLEVEL_BINDING
+ obj = eval("self", bind)
+ obj.instance_variable_set("@binding", bind)
+ bind
+end
+
+require 'lib/all'
+require 'app/syntax/markup'
+
+require 'app/db/sequel'
+
+require 'app/ui/lessons'
+require 'app/ui/widgets'
+require 'app/ui/completion'
+require 'app/ui/tabs/sidetabs'
+
+#let's give them a simple program to start off with!
+if HH::PREFS['first_run'].nil?
+ File.open(File.join(HH::USER, "Hello World.rb"), "w") do |f|
+ f << 'alert "Hello, world!"'
+ end
+
+ #the first_run pref will get set by the tour notice in app/ui/mainwindow
+end
diff --git a/app/app/db/connection_pool.rb b/app/app/db/connection_pool.rb
new file mode 100644
index 0000000..72e47e1
--- /dev/null
+++ b/app/app/db/connection_pool.rb
@@ -0,0 +1,65 @@
+require 'thread'
+
+module HH::Sequel
+ class ConnectionPool
+ attr_reader :max_size, :mutex, :conn_maker
+ attr_reader :available_connections, :allocated, :created_count
+
+ def initialize(max_size = 4, &block)
+ @max_size = max_size
+ @mutex = Mutex.new
+ @conn_maker = block
+
+ @available_connections = []
+ @allocated = {}
+ @created_count = 0
+ end
+
+ def size
+ @created_count
+ end
+
+ def hold
+ t = Thread.current
+ if (conn = owned_connection(t))
+ return yield(conn)
+ end
+ while !(conn = acquire(t))
+ sleep 0.001
+ end
+ begin
+ yield conn
+ ensure
+ release(t)
+ end
+ end
+
+ def owned_connection(thread)
+ @mutex.synchronize {@allocated[thread]}
+ end
+
+ def acquire(thread)
+ @mutex.synchronize do
+ @allocated[thread] ||= available
+ end
+ end
+
+ def available
+ @available_connections.pop || make_new
+ end
+
+ def make_new
+ if @created_count < @max_size
+ @created_count += 1
+ @conn_maker.call
+ end
+ end
+
+ def release(thread)
+ @mutex.synchronize do
+ @available_connections << @allocated[thread]
+ @allocated.delete(thread)
+ end
+ end
+ end
+end
diff --git a/app/app/db/core_ext.rb b/app/app/db/core_ext.rb
new file mode 100644
index 0000000..81046a0
--- /dev/null
+++ b/app/app/db/core_ext.rb
@@ -0,0 +1,9 @@
+# Time extensions.
+class Time
+ SQL_FORMAT = "TIMESTAMP '%Y-%m-%d %H:%M:%S'".freeze
+
+ # Formats the Time object as an SQL TIMESTAMP.
+ def to_sql_timestamp
+ strftime(SQL_FORMAT)
+ end
+end
diff --git a/app/app/db/database.rb b/app/app/db/database.rb
new file mode 100644
index 0000000..ce5679d
--- /dev/null
+++ b/app/app/db/database.rb
@@ -0,0 +1,119 @@
+require 'uri'
+
+require 'app/db/schema'
+
+module HH::Sequel
+ # A Database object represents a virtual connection to a database.
+ # The Database class is meant to be subclassed by database adapters in order
+ # to provide the functionality needed for executing queries.
+ class Database
+ # Constructs a new instance of a database connection with the specified
+ # options hash.
+ #
+ # Sequel::Database is an abstract class that is not useful by itself.
+ def initialize(opts = {})
+ @opts = opts
+ end
+
+ # Returns a new dataset with the from method invoked.
+ def from(*args); dataset.from(*args); end
+
+ # Returns a new dataset with the select method invoked.
+ def select(*args); dataset.select(*args); end
+
+ # Returns a new dataset with the from parameter set. For example,
+ # db[:posts].each {|p| alert p[:title]}
+ def [](table)
+ dataset.from(table)
+ end
+
+ # call-seq:
+ # db.execute(sql)
+ # db << sql
+ #
+ # Executes an sql query.
+ def <<(sql)
+ execute(sql)
+ end
+
+ # Returns a literal SQL representation of a value. This method is usually
+ # overriden in database adapters.
+ def literal(v)
+ case v
+ when String then "'%s'" % v
+ else v.to_s
+ end
+ end
+
+ # Creates a table. The easiest way to use this method is to provide a
+ # block:
+ # DB.create_table :posts do
+ # primary_key :id, :serial
+ # column :title, :text
+ # column :content, :text
+ # index :title
+ # end
+ def create_table(name, columns = nil, indexes = nil, &block)
+ if block
+ schema = Schema.new
+ schema.create_table(name, &block)
+ schema.create(self)
+ else
+ execute Schema.create_table_sql(name, columns, indexes)
+ end
+ end
+
+ # Drops a table.
+ def drop_table(name)
+ execute Schema.drop_table_sql(name)
+ end
+
+ # Performs a brute-force check for the existance of a table. This method is
+ # usually overriden in descendants.
+ def table_exists?(name)
+ from(name).first && true
+ rescue
+ false
+ end
+
+ @@adapters = Hash.new
+
+ # Sets the adapter scheme for the database class. Call this method in
+ # descendnants of Database to allow connection using a URL. For example:
+ # class DB2::Database < Sequel::Database
+ # set_adapter_scheme :db2
+ # ...
+ # end
+ def self.set_adapter_scheme(scheme)
+ @@adapters[scheme.to_sym] = self
+ end
+
+ # Converts a uri to an options hash. These options are then passed
+ # to a newly created database object.
+ def self.uri_to_options(uri)
+ {
+ :user => uri.user,
+ :password => uri.password,
+ :host => uri.host,
+ :port => uri.port,
+ :database => (uri.path =~ /\/(.*)/) && ($1)
+ }
+ end
+
+ # call-seq:
+ # Sequel::Database.connect(conn_string)
+ # Sequel.connect(conn_string)
+ #
+ # Creates a new database object based on the supplied connection string.
+ # The specified scheme determines the database class used, and the rest
+ # of the string specifies the connection options. For example:
+ # DB = Sequel.connect('sqlite:///blog.db')
+ def self.connect(conn_string)
+ uri = URI.parse(conn_string)
+ c = @@adapters[uri.scheme.to_sym]
+ raise "Invalid database scheme" unless c
+ c.new(c.uri_to_options(uri))
+ end
+ end
+end
+
diff --git a/app/app/db/dataset.rb b/app/app/db/dataset.rb
new file mode 100644
index 0000000..ca653ab
--- /dev/null
+++ b/app/app/db/dataset.rb
@@ -0,0 +1,354 @@
+module HH::Sequel
+ # A Dataset represents a view of a the data in a database, constrained by
+ # specific parameters such as filtering conditions, order, etc. Datasets
+ # can be used to create, retrieve, update and delete records.
+ #
+ # Query results are always retrieved on demand, so a dataset can be kept
+ # around and reused indefinitely:
+ # my_posts = DB[:posts].filter(:author => 'david') # no records are retrieved
+ # p my_posts.all # records are now retrieved
+ # ...
+ # p my_posts.all # records are retrieved again
+ #
+ # In order to provide this functionality, dataset methods such as where,
+ # select, order, etc. return modified copies of the dataset, so you can
+ # use different datasets to access data:
+ # posts = DB[:posts]
+ # davids_posts = posts.filter(:author => 'david')
+ # old_posts = posts.filter('stamp < ?', 1.week.ago)
+ #
+ # Datasets are Enumerable objects, so they can be manipulated using any
+ # of the Enumerable methods, such as map, inject, etc.
+ class Dataset
+ include Enumerable
+
+ attr_reader :db
+ attr_accessor :record_class
+
+ # Constructs a new instance of a dataset with a database instance, initial
+ # options and an optional record class. Datasets are usually constructed by
+ # invoking Database methods:
+ # DB[:posts]
+ # Or:
+ # DB.dataset # the returned dataset is blank
+ #
+ # Sequel::Dataset is an abstract class that is not useful by itself. Each
+ # database adaptor should provide a descendant class of Sequel::Dataset.
+ def initialize(db, opts = {}, record_class = nil)
+ @db = db
+ @opts = opts || {}
+ @record_class = record_class
+ end
+
+ # Returns a new instance of the dataset with its options
+ def dup_merge(opts)
+ self.class.new(@db, @opts.merge(opts), @record_class)
+ end
+
+ AS_REGEXP = /(.*)___(.*)/.freeze
+ AS_FORMAT = "%s AS %s".freeze
+ DOUBLE_UNDERSCORE = '__'.freeze
+ PERIOD = '.'.freeze
+
+ # Returns a valid SQL fieldname as a string. Field names specified as
+ # symbols can include double underscores to denote a dot separator, e.g.
+ # :posts__id will be converted into posts.id.
+ def field_name(field)
+ field.is_a?(Symbol) ? field.to_field_name : field
+ end
+
+ QUALIFIED_REGEXP = /(.*)\.(.*)/.freeze
+ QUALIFIED_FORMAT = "%s.%s".freeze
+
+ # Returns a qualified field name (including a table name) if the field
+ # name isn't already qualified.
+ def qualified_field_name(field, table)
+ fn = field_name(field)
+ fn = QUALIFIED_FORMAT % [table, fn] unless fn =~ QUALIFIED_REGEXP
+ end
+
+ WILDCARD = '*'.freeze
+ COMMA_SEPARATOR = ", ".freeze
+
+ # Converts a field list into a comma seperated string of field names.
+ def field_list(fields)
+ case fields
+ when Array then
+ if fields.empty?
+ WILDCARD
+ else
+ fields.map {|i| field_name(i)}.join(COMMA_SEPARATOR)
+ end
+ when Symbol then
+ fields.to_field_name
+ else
+ fields
+ end
+ end
+
+ # Converts an array of sources into a comma separated list.
+ def source_list(source)
+ case source
+ when Array then source.join(COMMA_SEPARATOR)
+ else source
+ end
+ end
+
+ # Returns a literal representation of a value to be used as part
+ # of an SQL expression. This method is overriden in descendants.
+ def literal(v)
+ "'%s'" % SQLite3::Database.quote(v.to_s)
+ end
+
+ AND_SEPARATOR = " AND ".freeze
+ EQUAL_COND = "(%s = %s)".freeze
+
+ # Formats an equality condition SQL expression.
+ def where_equal_condition(left, right)
+ EQUAL_COND % [field_name(left), literal(right)]
+ end
+
+ # Formats a where clause.
+ def where_list(where)
+ case where
+ when Hash then
+ where.map {|kv| where_equal_condition(kv[0], kv[1])}.join(AND_SEPARATOR)
+ when Array then
+ fmt = where.shift
+ fmt.gsub('?') {|i| literal(where.shift)}
+ else
+ where
+ end
+ end
+
+ # Formats a join condition.
+ def join_cond_list(cond, join_table)
+ cond.map do |kv|
+ EQUAL_COND % [
+ qualified_field_name(kv[0], join_table),
+ qualified_field_name(kv[1], @opts[:from])]
+ end.join(AND_SEPARATOR)
+ end
+
+ # Returns a copy of the dataset with the source changed.
+ def from(source)
+ dup_merge(:from => source)
+ end
+
+ # Returns a copy of the dataset with the selected fields changed.
+ def select(*fields)
+ fields = fields.first if fields.size == 1
+ dup_merge(:select => fields)
+ end
+
+ # Returns a copy of the dataset with the order changed.
+ def order(*order)
+ dup_merge(:order => order)
+ end
+
+ DESC_ORDER_REGEXP = /(.*)\sDESC/.freeze
+
+ def reverse_order(order)
+ order.map do |f|
+ if f.to_s =~ DESC_ORDER_REGEXP
+ $1
+ else
+ f.DESC
+ end
+ end
+ end
+
+ # Returns a copy of the dataset with the where conditions changed.
+ def where(*where)
+ if where.size == 1
+ where = where.first
+ if @opts[:where] && @opts[:where].is_a?(Hash) && where.is_a?(Hash)
+ where = @opts[:where].merge(where)
+ end
+ end
+ dup_merge(:where => where)
+ end
+
+ LEFT_OUTER_JOIN = 'LEFT OUTER JOIN'.freeze
+ INNER_JOIN = 'INNER JOIN'.freeze
+ RIGHT_OUTER_JOIN = 'RIGHT OUTER JOIN'.freeze
+ FULL_OUTER_JOIN = 'FULL OUTER JOIN'.freeze
+
+ def join(table, cond)
+ dup_merge(:join_type => LEFT_OUTER_JOIN, :join_table => table,
+ :join_cond => cond)
+ end
+
+ alias_method :filter, :where
+ alias_method :all, :to_a
+ alias_method :enum_map, :map
+
+ #
+ def map(field_name = nil, &block)
+ if block
+ enum_map(&block)
+ elsif field_name
+ enum_map {|r| r[field_name]}
+ else
+ []
+ end
+ end
+
+ def hash_column(key_column, value_column)
+ inject({}) do |m, r|
+ m[r[key_column]] = r[value_column]
+ m
+ end
+ end
+
+ def <<(values)
+ insert(values)
+ end
+
+ def insert_multiple(array, &block)
+ if block
+ array.each {|i| insert(block[i])}
+ else
+ array.each {|i| insert(i)}
+ end
+ end
+
+ SELECT = "SELECT %s FROM %s".freeze
+ LIMIT = " LIMIT %s".freeze
+ ORDER = " ORDER BY %s".freeze
+ WHERE = " WHERE %s".freeze
+ JOIN_CLAUSE = " %s %s ON %s".freeze
+
+ EMPTY = ''.freeze
+
+ SPACE = ' '.freeze
+
+ def select_sql(opts = nil)
+ opts = opts ? @opts.merge(opts) : @opts
+
+ fields = opts[:select]
+ select_fields = fields ? field_list(fields) : WILDCARD
+ select_source = source_list(opts[:from])
+ sql = SELECT % [select_fields, select_source]
+
+ if join_type = opts[:join_type]
+ join_table = opts[:join_table]
+ join_cond = join_cond_list(opts[:join_cond], join_table)
+ sql << (JOIN_CLAUSE % [join_type, join_table, join_cond])
+ end
+
+ if where = opts[:where]
+ sql << (WHERE % where_list(where))
+ end
+
+ if order = opts[:order]
+ sql << (ORDER % order.join(COMMA_SEPARATOR))
+ end
+
+ if limit = opts[:limit]
+ sql << (LIMIT % limit)
+ end
+
+ sql
+ end
+
+ INSERT = "INSERT INTO %s (%s) VALUES (%s)".freeze
+ INSERT_EMPTY = "INSERT INTO %s DEFAULT VALUES".freeze
+
+ def insert_sql(values, opts = nil)
+ opts = opts ? @opts.merge(opts) : @opts
+
+ if values.nil? || values.empty?
+ INSERT_EMPTY % opts[:from]
+ else
+ field_list = []
+ value_list = []
+ values.each do |k, v|
+ field_list << k
+ value_list << literal(v)
+ end
+
+ INSERT % [
+ opts[:from],
+ field_list.join(COMMA_SEPARATOR),
+ value_list.join(COMMA_SEPARATOR)]
+ end
+ end
+
+ UPDATE = "UPDATE %s SET %s".freeze
+ SET_FORMAT = "%s = %s".freeze
+
+ def update_sql(values, opts = nil)
+ opts = opts ? @opts.merge(opts) : @opts
+
+ set_list = values.map {|kv| SET_FORMAT % [kv[0], literal(kv[1])]}.
+ join(COMMA_SEPARATOR)
+ update_clause = UPDATE % [opts[:from], set_list]
+
+ where = opts[:where]
+ where_clause = where ? WHERE % where_list(where) : EMPTY
+
+ [update_clause, where_clause].join(SPACE)
+ end
+
+ DELETE = "DELETE FROM %s".freeze
+
+ def delete_sql(opts = nil)
+ opts = opts ? @opts.merge(opts) : @opts
+
+ delete_source = opts[:from]
+
+ where = opts[:where]
+ where_clause = where ? WHERE % where_list(where) : EMPTY
+
+ [DELETE % delete_source, where_clause].join(SPACE)
+ end
+
+ COUNT = "COUNT(*)".freeze
+ SELECT_COUNT = {:select => COUNT, :order => nil}.freeze
+
+ def count_sql(opts = nil)
+ select_sql(opts ? opts.merge(SELECT_COUNT) : SELECT_COUNT)
+ end
+
+ # aggregates
+ def min(field)
+ select(field.MIN).first[:min]
+ end
+
+ def max(field)
+ select(field.MAX).first[:max]
+ end
+ end
+end
+
+class Symbol
+ def DESC
+ "#{to_s} DESC"
+ end
+
+ def AS(target)
+ "#{field_name} AS #{target}"
+ end
+
+ def MIN; "MIN(#{to_field_name})"; end
+ def MAX; "MAX(#{to_field_name})"; end
+
+ AS_REGEXP = /(.*)___(.*)/.freeze
+ AS_FORMAT = "%s AS %s".freeze
+ DOUBLE_UNDERSCORE = '__'.freeze
+ PERIOD = '.'.freeze
+
+ def to_field_name
+ s = to_s
+ if s =~ AS_REGEXP
+ s = AS_FORMAT % [$1, $2]
+ end
+ s.split(DOUBLE_UNDERSCORE).join(PERIOD)
+ end
+
+ def ALL
+ "#{to_s}.*"
+ end
+end
+
diff --git a/app/app/db/http.rb b/app/app/db/http.rb
new file mode 100644
index 0000000..96bdf97
--- /dev/null
+++ b/app/app/db/http.rb
@@ -0,0 +1,78 @@
+require 'lib/web/yaml'
+
+module HH::Sequel
+ module HTTP
+ class Database < HH::Sequel::Database
+ set_adapter_scheme :http
+ attr_reader :url
+
+ include HH::YAML
+
+ def initialize(opts = {})
+ super
+ end
+
+ def dataset(opts = nil)
+ Dataset.new(self, opts)
+ end
+
+ def tables
+ fetch_uri(:Get)
+ end
+
+ def drop_table(name)
+ fetch_uri(:Delete, name)
+ end
+ end
+
+ class Dataset < HH::Sequel::Dataset
+ def each(opts = nil, &block)
+ res = @db.fetch_uri(:Get, @opts[:from], opts)
+ res.each(&block)
+ self
+ end
+
+ LIMIT_1 = {:limit => 1}.freeze
+
+ def first(opts = nil)
+ opts = opts ? opts.merge(LIMIT_1) : LIMIT_1
+ @db.fetch_uri(:Get, @opts[:from], opts).first
+ end
+
+ def last(opts = nil)
+ raise RuntimeError, 'No order specified' unless
+ @opts[:order] || (opts && opts[:order])
+
+ opts = {:order => reverse_order(@opts[:order])}.
+ merge(opts ? opts.merge(LIMIT_1) : LIMIT_1)
+ @db.fetch_uri(:Get, @opts[:from], opts).first
+ end
+
+ def count(opts = nil)
+ @db.fetch_uri(:Get, "#{@opts[:from]}/count", opts)
+ end
+
+ def insert(values = nil, opts = nil)
+ @db.fetch_uri(:Post, @opts[:from], values)
+ end
+
+ def save(values = nil, opts = nil)
+ @db.fetch_uri(:Post, @opts[:from], values)
+ end
+
+ def bulk_insert(values = nil, opts = nil)
+ @db.fetch_uri(:Put, "#{@opts[:from]}/new", YAML.dump(values))
+ end
+
+ def update(values, opts = nil)
+ @db.fetch_uri(:Post, @opts[:from], values)
+ self
+ end
+
+ def delete(opts = nil)
+ @db.fetch_uri(:Delete, @opts[:from])
+ self
+ end
+ end
+ end
+end
diff --git a/app/app/db/model.rb b/app/app/db/model.rb
new file mode 100644
index 0000000..db1b2c9
--- /dev/null
+++ b/app/app/db/model.rb
@@ -0,0 +1,235 @@
+
+module HH::Sequel
+ class Model
+ @@db = nil
+
+ def self.db; @@db; end
+ def self.db=(db); @@db = db; end
+
+ def self.table_name; @table_name; end
+ def self.set_table_name(t); @table_name = t; end
+
+ def self.dataset
+ return @dataset if @dataset
+ if !table_name
+ raise RuntimeError, "Table name not specified for class #{self}."
+ elsif !db
+ raise RuntimeError, "No database connected."
+ end
+ @dataset = db[table_name]
+ @dataset.record_class = self
+ @dataset
+ end
+ def self.set_dataset(ds); @dataset = ds; @dataset.record_class = self; end
+
+ def self.cache_by(column, expiration)
+ @cache_column = column
+
+ prefix = "#{name}.#{column}."
+ define_method(:cache_key) do
+ prefix + @values[column].to_s
+ end
+
+ define_method("find_by_#{column}".to_sym) do |arg|
+ key = cache_key
+ rec = CACHE[key]
+ if !rec
+ rec = find(column => arg)
+ CACHE.set(key, rec, expiration)
+ end
+ rec
+ end
+
+ alias_method :delete, :delete_and_invalidate_cache
+ alias_method :set, :set_and_update_cache
+ end
+
+ def self.cache_column
+ @cache_column
+ end
+
+ def self.primary_key; @primary_key ||= :id; end
+ def self.set_primary_key(k); @primary_key = k; end
+
+ def self.schema(name = nil, &block)
+ name ||= table_name
+ @schema = Schema::Generator.new(name, &block)
+ set_table_name name
+ if @schema.primary_key_name
+ set_primary_key @schema.primary_key_name
+ end
+ end
+
+ def self.table_exists?
+ db.table_exists?(table_name)
+ end
+
+ def self.create_table
+ db.execute get_schema.create_sql
+ end
+
+ def self.drop_table
+ db.execute get_schema.drop_sql
+ end
+
+ def self.recreate_table
+ drop_table if table_exists?
+ create_table
+ end
+
+ def self.get_schema
+ @schema
+ end
+
+ ONE_TO_ONE_PROC = "proc {i = @values[:%s]; %s[i] if i}".freeze
+ ID_POSTFIX = "_id".freeze
+ FROM_DATASET = "db[%s]".freeze
+
+ def self.one_to_one(name, opts)
+ klass = opts[:class] ? opts[:class] : (FROM_DATASET % name.inspect)
+ key = opts[:key] || (name.to_s + ID_POSTFIX)
+ define_method name, &eval(ONE_TO_ONE_PROC % [key, klass])
+ end
+
+ ONE_TO_MANY_PROC = "proc {%s.filter(:%s => @pkey)}".freeze
+ ONE_TO_MANY_ORDER_PROC = "proc {%s.filter(:%s => @pkey).order(%s)}".freeze
+ def self.one_to_many(name, opts)
+ klass = opts[:class] ? opts[:class] :
+ (FROM_DATASET % (opts[:table] || name.inspect))
+ key = opts[:on]
+ order = opts[:order]
+ define_method name, &eval(
+ (order ? ONE_TO_MANY_ORDER_PROC : ONE_TO_MANY_PROC) %
+ [klass, key, order.inspect]
+ )
+ end
+
+ def self.get_hooks(key)
+ @hooks ||= {}
+ @hooks[key] ||= []
+ end
+
+ def self.has_hooks?(key)
+ !get_hooks(key).empty?
+ end
+
+ def run_hooks(key)
+ self.class.get_hooks(key).each {|h| instance_eval(&h)}
+ end
+
+ def self.before_delete(&block)
+ get_hooks(:before_delete).unshift(block)
+ end
+
+ def self.after_create(&block)
+ get_hooks(:after_create) << block
+ end
+
+ ############################################################################
+
+ attr_reader :values, :pkey
+
+ def model
+ self.class
+ end
+
+ def primary_key
+ model.primary_key
+ end
+
+ def initialize(values)
+ @values = values
+ @pkey = values[self.class.primary_key]
+ end
+
+ def exists?
+ model.filter(primary_key => @pkey).count == 1
+ end
+
+ def refresh
+ record = self.class.find(primary_key => @pkey)
+ record ? (@values = record.values) :
+ (raise RuntimeError, "Record not found")
+ self
+ end
+
+ def self.find(cond)
+ dataset.filter(cond).first # || (raise RuntimeError, "Record not found.")
+ end
+
+ def self.each(&block); dataset.each(&block); end
+ def self.all; dataset.all; end
+ def self.filter(*arg); dataset.filter(*arg); end
+ def self.first; dataset.first; end
+ def self.count; dataset.count; end
+ def self.map(column); dataset.map(column); end
+ def self.hash_column(column); dataset.hash_column(primary_key, column); end
+ def self.join(*args); dataset.join(*args); end
+ def self.lock(mode, &block); dataset.lock(mode, &block); end
+ def self.delete_all
+ if has_hooks?(:before_delete)
+ db.transaction {dataset.all.each {|r| r.delete}}
+ else
+ dataset.delete
+ end
+ end
+
+ def self.[](key)
+ find key.is_a?(Hash) ? key : {primary_key => key}
+ end
+
+ def self.create(values = nil)
+ db.transaction do
+ obj = find(primary_key => dataset.insert(values))
+ obj.run_hooks(:after_create)
+ obj
+ end
+ end
+
+ def delete
+ db.transaction do
+ run_hooks(:before_delete)
+ model.dataset.filter(primary_key => @pkey).delete
+ end
+ end
+
+ FIND_BY_REGEXP = /^find_by_(.*)/.freeze
+ FILTER_BY_REGEXP = /^filter_by_(.*)/.freeze
+
+ def self.method_missing(m, *args)
+ Thread.exclusive do
+ method_name = m.to_s
+ if method_name =~ FIND_BY_REGEXP
+ c = $1
+ meta_def(method_name) {|arg| find(c => arg)}
+ send(m, *args) if respond_to?(m)
+ elsif method_name =~ FILTER_BY_REGEXP
+ c = $1
+ meta_def(method_name) {|arg| filter(c => arg)}
+ send(m, *args) if respond_to?(m)
+ else
+ super
+ end
+ end
+ end
+
+ def db; @@db; end
+
+ def [](field); @values[field]; end
+
+ def ==(obj)
+ (obj.class == model) && (obj.pkey == @pkey)
+ end
+
+ def set(values)
+ model.dataset.filter(primary_key => @pkey).update(values)
+ @values.merge!(values)
+ end
+ end
+
+ def self.Model(table_name)
+ Class.new(Sequel::Model) do
+ meta_def(:inherited) {|c| c.set_table_name(table_name)}
+ end
+ end
+end
diff --git a/app/app/db/schema.rb b/app/app/db/schema.rb
new file mode 100644
index 0000000..3315385
--- /dev/null
+++ b/app/app/db/schema.rb
@@ -0,0 +1,161 @@
+
+module HH::Sequel
+ class Schema
+ COMMA_SEPARATOR = ', '.freeze
+ COLUMN_DEF = '%s %s'.freeze
+ UNIQUE = ' UNIQUE'.freeze
+ NOT_NULL = ' NOT NULL'.freeze
+ DEFAULT = ' DEFAULT %s'.freeze
+ PRIMARY_KEY = ' PRIMARY KEY'.freeze
+ REFERENCES = ' REFERENCES %s'.freeze
+ ON_DELETE = ' ON DELETE %s'.freeze
+
+ RESTRICT = 'RESTRICT'.freeze
+ CASCADE = 'CASCADE'.freeze
+ NO_ACTION = 'NO ACTION'.freeze
+ SET_NULL = 'SET NULL'.freeze
+ SET_DEFAULT = 'SET DEFAULT'.freeze
+
+ TYPES = Hash.new {|h, k| k}
+ TYPES[:double] = 'double precision'
+
+ def self.on_delete_action(action)
+ case action
+ when restrict then RESTRICT
+ when cascade then CASCADE
+ when set_null then SET_NULL
+ when set_default then SET_DEFAULT
+ else NO_ACTION
+ end
+ end
+
+ def self.column_definition(column)
+ c = COLUMN_DEF % [column[:name], TYPES[column[:type]]]
+ c << UNIQUE if column[:unique]
+ c << NOT_NULL if column[:null] == false
+ c << DEFAULT % SQLite::Database.quote(column[:default]) if column.include?(:default)
+ c << PRIMARY_KEY if column[:primary_key]
+ c << REFERENCES % column[:table] if column[:table]
+ c << ON_DELETE % on_delete_action(column[:on_delete]) if
+ column[:on_delete]
+ c
+ end
+
+ def self.create_table_column_list(columns)
+ columns.map {|c| column_definition(c)}.join(COMMA_SEPARATOR)
+ end
+
+ CREATE_INDEX = 'CREATE INDEX %s ON %s (%s);'.freeze
+ CREATE_UNIQUE_INDEX = 'CREATE UNIQUE INDEX %s ON %s (%s);'.freeze
+ INDEX_NAME = '%s_%s_index'.freeze
+ UNDERSCORE = '_'.freeze
+
+ def self.index_definition(table_name, index)
+ fields = index[:columns].join(COMMA_SEPARATOR)
+ index_name = index[:name] || INDEX_NAME %
+ [table_name, index[:columns].join(UNDERSCORE)]
+ (index[:unique] ? CREATE_UNIQUE_INDEX : CREATE_INDEX) %
+ [index_name, table_name, fields]
+ end
+
+ def self.create_indexes_sql(table_name, indexes)
+ indexes.map {|i| index_definition(table_name, i)}.join
+ end
+
+ CREATE_TABLE = "CREATE TABLE %s (%s);".freeze
+
+ def self.create_table_sql(name, columns, indexes = nil)
+ sql = CREATE_TABLE % [name, create_table_column_list(columns)]
+ sql << create_indexes_sql(name, indexes) if indexes && !indexes.empty?
+ sql
+ end
+
+ DROP_TABLE = "DROP TABLE %s CASCADE;".freeze
+
+ def self.drop_table_sql(name)
+ DROP_TABLE % name
+ end
+
+ class Generator
+ attr_reader :table_name
+
+ def initialize(table_name, &block)
+ @table_name = table_name
+ @primary_key = {:name => :id, :type => :serial, :primary_key => true}
+ @columns = []
+ @indexes = []
+ instance_eval(&block)
+ end
+
+ def primary_key(name, type = nil, opts = nil)
+ @primary_key = {
+ :name => name,
+ :type => type || :serial,
+ :primary_key => true
+ }.merge(opts || {})
+ end
+
+ def primary_key_name
+ @primary_key && @primary_key[:name]
+ end
+
+ def column(name, type, opts = nil)
+ @columns << {:name => name, :type => type}.merge(opts || {})
+ end
+
+ def foreign_key(name, opts)
+ @columns << {:name => name, :type => :integer}.merge(opts || {})
+ end
+
+ def has_column?(name)
+ @columns.each {|c| return true if c[:name] == name}
+ false
+ end
+
+ def index(columns, opts = nil)
+ columns = [columns] unless columns.is_a?(Array)
+ @indexes << {:columns => columns}.merge(opts || {})
+ end
+
+ def create_sql
+ if @primary_key && !has_column?(@primary_key[:name])
+ @columns.unshift(@primary_key)
+ end
+ Schema.create_table_sql(@table_name, @columns, @indexes)
+ end
+
+ def drop_sql
+ Schema.drop_table_sql(@table_name)
+ end
+ end
+
+ attr_reader :instructions
+
+ def initialize(&block)
+ @instructions = []
+ instance_eval(&block) if block
+ end
+
+ def create_table(table_name, &block)
+ @instructions << Generator.new(table_name, &block)
+ end
+
+ def create(db)
+ @instructions.each do |s|
+ db.execute(s.create_sql)
+ end
+ end
+
+ def drop(db)
+ @instructions.reverse_each do |s|
+ db.execute(s.drop_sql) if db.table_exists?(s.table_name)
+ end
+ end
+
+ def recreate(db)
+ drop(db)
+ create(db)
+ end
+ end
+end
+
diff --git a/app/app/db/sequel.rb b/app/app/db/sequel.rb
new file mode 100644
index 0000000..ec23298
--- /dev/null
+++ b/app/app/db/sequel.rb
@@ -0,0 +1,21 @@
+require 'app/db/core_ext'
+require 'app/db/database'
+require 'app/db/connection_pool'
+require 'app/db/schema'
+require 'app/db/dataset'
+require 'app/db/model'
+require 'app/db/sqlite'
+require 'app/db/http'
+
+module HH::Sequel #:nodoc:
+ def self.connect(url)
+ Database.connect(url)
+ end
+end
+
+require 'app/db/table'
+
+# some constant initialization
+HH::DB = HH::Sequel::SQLite::Database.new(:database => File.join(HH::USER, "+TABLES"))
+HH::DB.extend HH::DbMixin
+HH::DB.init \ No newline at end of file
diff --git a/app/app/db/sqlite.rb b/app/app/db/sqlite.rb
new file mode 100644
index 0000000..2267c2f
--- /dev/null
+++ b/app/app/db/sqlite.rb
@@ -0,0 +1,112 @@
+require 'sqlite3'
+
+module HH::Sequel
+ module SQLite
+ class Database < HH::Sequel::Database
+ set_adapter_scheme :sqlite
+ attr_reader :pool
+
+ def initialize(opts = {})
+ super
+ @pool = ConnectionPool.new(@opts[:max_connections] || 4) do
+ db = SQLite3::Database.new(@opts[:database])
+ db.type_translation = true
+ db
+ end
+ end
+
+ def dataset(opts = nil)
+ SQLite::Dataset.new(self, opts)
+ end
+
+ def tables
+ execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").map { |name,| name }
+ end
+
+ def execute(sql)
+ @pool.hold {|conn| conn.execute(sql)}
+ end
+
+ def execute_insert(sql)
+ @pool.hold {|conn| conn.execute(sql); conn.last_insert_row_id}
+ end
+
+ def single_value(sql)
+ @pool.hold {|conn| conn.get_first_value(sql)}
+ end
+
+ def result_set(sql, record_class, &block)
+ @pool.hold do |conn|
+ conn.query(sql) do |result|
+ columns = result.columns
+ column_count = columns.size
+ result.each do |values|
+ row = {}
+ column_count.times {|i| row[columns[i].to_sym] = values[i]}
+ block.call(record_class ? record_class.new(row) : row)
+ end
+ end
+ end
+ end
+
+ def synchronize(&block)
+ @pool.hold(&block)
+ end
+
+ def transaction(&block)
+ @pool.hold {|conn| conn.transaction(&block)}
+ end
+
+ def table_exists?(name)
+ execute("PRAGMA table_info('%s')" % SQLite3::Database.quote(name.to_s)).any?
+ end
+ end
+
+ class Dataset < HH::Sequel::Dataset
+ def each(opts = nil, &block)
+ @db.result_set(select_sql(opts), @record_class, &block)
+ self
+ end
+
+ LIMIT_1 = {:limit => 1}.freeze
+
+ def first(opts = nil)
+ opts = opts ? opts.merge(LIMIT_1) : LIMIT_1
+ @db.result_set(select_sql(opts), @record_class) {|r| return r}
+ end
+
+ def last(opts = nil)
+ raise RuntimeError, 'No order specified' unless
+ @opts[:order] || (opts && opts[:order])
+
+ opts = {:order => reverse_order(@opts[:order])}.
+ merge(opts ? opts.merge(LIMIT_1) : LIMIT_1)
+ @db.result_set(select_sql(opts), @record_class) {|r| return r}
+ end
+
+ def count(opts = nil)
+ @db.single_value(count_sql(opts)).to_i
+ end
+
+ def insert(values = nil, opts = nil)
+ @db.synchronize do
+ @db.execute_insert insert_sql(values, opts)
+ end
+ end
+
+ def update(values, opts = nil)
+ @db.synchronize do
+ @db.execute update_sql(values, opts)
+ end
+ self
+ end
+
+ def delete(opts = nil)
+ @db.synchronize do
+ @db.execute delete_sql(opts)
+ end
+ self
+ end
+ end
+ end
+end
diff --git a/app/app/db/table.rb b/app/app/db/table.rb
new file mode 100644
index 0000000..25563a3
--- /dev/null
+++ b/app/app/db/table.rb
@@ -0,0 +1,231 @@
+class HH::Sequel::SQLite::Dataset
+ def table_name
+ @opts[:from]
+ end
+ def widget(slot)
+ slot.stack(:margin => 18).tap do |s|
+ s.title "The #{table_name} Table"
+ set.each do |item|
+ s.para s.link(item[:title], :size => 18, :stroke => "#777"),
+ " Table::Item", :stroke => "#999"
+ s.para "at #{item[:created]}"
+ s.para item[:editbox]
+ end
+ end
+ end
+end
+
+class HH::Sequel::Dataset
+ def only(id)
+ first(:where => ['id = ?', id])
+ end
+ def limit(num)
+ dup_merge(:limit => num)
+ end
+ def recent(num)
+ order("created DESC").limit(num)
+ end
+ def save(data)
+ @db.save(@opts[:from], data)
+ end
+end
+
+module HH::DbMixin
+ SPECIAL_FIELDS = ['id', 'created', 'updated']
+ def tables
+ execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").
+ map { |name,| name if name !~ /^HETYH_/ }.compact
+ end
+ def save(table, obj)
+ table = table.to_s
+ fields = get_fields(table)
+ if fields.empty?
+ startup(table, obj.keys)
+ else
+ missing = obj.keys - fields
+ unless missing.empty?
+ missing.each do |name|
+ add_column(table, name)
+ end
+ end
+ end
+ if obj['id']
+ from(table).only(obj['id']).update(obj.merge(:updated => Time.now))
+ else
+ from(table).insert(obj.merge(:created => Time.now, :updated => Time.now))
+ end
+ end
+ def init
+ unless table_exists? "HETYH_PREFS"
+ create_table "HETYH_PREFS" do
+ primary_key :id, :integer
+ column :name, :text
+ column :value, :text
+ index :name
+ end
+ end
+ HH.load_prefs
+ unless table_exists? "HETYH_SHARES"
+ create_table "HETYH_SHARES" do
+ primary_key :id, :integer
+ column :title, :text
+ column :klass, :text
+ column :active, :integer
+ index :title
+ end
+ end
+ HH.load_shares
+ end
+ def startup(table, fields)
+ SPECIAL_FIELDS.each do |x|
+ fields.each do |y|
+ raise ArgumentError, "Can't have a field called #{y}!" if y.downcase == x
+ end
+ end
+ create_table table do
+ primary_key :id, :integer
+ column :created, :datetime
+ column :updated, :datetime
+ fields.each do |name|
+ column name, :text
+ if [:title, :name].include? name
+ index name
+ end
+ end
+ end
+ true
+ rescue SQLite3::SQLException
+ false
+ end
+ def drop_table(table)
+ raise ArgumentError, "Table name must be letters, numbers, underscores only." if table !~ /^\w+$/
+ execute("DROP TABLE #{table}")
+ end
+ def get_fields(table)
+ raise ArgumentError, "Table name must be letters, numbers, underscores only." if table !~ /^\w+$/
+ execute("PRAGMA table_info(#{table})").map { |id, name,| name }
+ end
+ def add_column(table, column)
+ raise ArgumentError, "Table name must be letters, numbers, underscores only." if table !~ /^\w+$/
+ execute("ALTER TABLE #{table} ADD COLUMN #{HH::Sequel::Schema.column_definition(:name => column, :type => :text)}")
+ end
+end
+
+def Table(t)
+ raise ArgumentError, "Table name must be letters, numbers, underscores only. No spaces!" if t !~ /^\w+$/
+ if HH.check_share(t, 'Table')
+ Web.table(t)
+ else
+ HH::DB[t]
+ end
+end
+
+module HH
+ PREFS = {}
+ SHARES = {}
+
+ class << self
+ def tutor_on?
+ PREFS['tutor'] == 'on'
+ end
+
+ def tutor=(state)
+ PREFS['tutor'] = state
+ save_prefs
+ end
+
+ def tutor_lesson
+ (PREFS['tut_lesson'] || 0).to_i
+ end
+
+ def tutor_lesson=(n)
+ PREFS['tut_lesson']=n
+ save_prefs
+ end
+
+ def tutor_page
+ PREFS['tut_page'] || '/start'
+ end
+
+ def tutor_page=(p)
+ PREFS['tut_page']=p
+ save_prefs
+ end
+
+ def save_prefs
+ preft = HH::DB["HETYH_PREFS"]
+ preft.delete
+ PREFS.each do |k, v|
+ preft.insert(:name => k, :value => v)
+ end
+ nil
+ end
+
+ def load_prefs
+ HH::DB["HETYH_PREFS"].each do |row|
+ PREFS[row[:name]] = row[:value] unless row[:value].strip.empty?
+ end
+ PREFS['tutor'] = 'off'
+ end
+
+ def load_shares
+ SHARES.clear
+ HH::DB["HETYH_SHARES"].each do |row|
+ SHARES["#{row[:title]}:#{row[:klass]}"] = row[:active]
+ end
+ end
+
+ def add_share(title, klass)
+ share = {:title => title, :klass => klass, :active => 1}
+ HH::DB["HETYH_SHARES"].insert(share)
+ SHARES["#{title}:#{klass}"] = 1
+ end
+
+ def check_share(title, klass)
+ SHARES["#{title}:#{klass}"]
+ end
+
+ def script_exists?(name)
+ File.exists?(HH::USER + "/" + name + ".rb")
+ end
+
+ def save_script(name, code)
+ APP.emit :save, :name => name, :code => code
+ File.open(HH::USER + "/" + name + ".rb", "w") do |f|
+ f << code
+ end
+ return if PREFS['username'].blank?
+ end
+
+ def get_script(path)
+ app = {:name => File.basename(path, '.rb'), :script => File.read(path)}
+ m, = *app[:script].match(/\A(([ \t]*#.+)(\r?\n|$))+/)
+ app[:mtime] = File.mtime(path)
+ app[:desc] = m.gsub(/^[ \t]*#+[ \t]*/, '').strip.gsub(/\n+/, ' ') if m
+ app
+ end
+
+ def scripts
+ Dir["#{HH::USER}/*.rb"].map { |path| get_script(path) }.
+ sort_by { |script| Time.now - script[:mtime] }
+ end
+
+ def samples
+ Dir["#{HH::HOME}/samples/*.rb"].map do |path|
+ s = get_script(path)
+ # set the creation time to nil
+ s[:mtime] = nil
+ s[:sample] = true
+ s
+ end. sort_by { |script| script[:name] }
+ end
+
+ def user
+ return if PREFS['username'].blank?
+ unless @user and @user.name == PREFS['username']
+ @user = Hacker(PREFS)
+ end
+ @user
+ end
+ end
+end
diff --git a/app/app/syntax/common.rb b/app/app/syntax/common.rb
new file mode 100644
index 0000000..be961c1
--- /dev/null
+++ b/app/app/syntax/common.rb
@@ -0,0 +1,197 @@
+require 'strscan'
+
+module HH::Syntax
+
+ # A single token extracted by a tokenizer. It is simply the lexeme
+ # itself, decorated with a 'group' attribute to identify the type of the
+ # lexeme.
+ class Token < String
+
+ # the type of the lexeme that was extracted.
+ attr_reader :group
+
+ # the instruction associated with this token (:none, :region_open, or
+ # :region_close)
+ attr_reader :instruction
+
+ # Create a new Token representing the given text, and belonging to the
+ # given group.
+ def initialize( text, group, instruction = :none )
+ super text
+ @group = group
+ @instruction = instruction
+ end
+
+ end
+
+ # The base class of all tokenizers. It sets up the scanner and manages the
+ # looping until all tokens have been extracted. It also provides convenience
+ # methods to make sure adjacent tokens of identical groups are returned as
+ # a single token.
+ class Tokenizer
+
+ # The current group being processed by the tokenizer
+ attr_reader :group
+
+ # The current chunk of text being accumulated
+ attr_reader :chunk
+
+ # Start tokenizing. This sets up the state in preparation for tokenization,
+ # such as creating a new scanner for the text and saving the callback block.
+ # The block will be invoked for each token extracted.
+ def start( text, &block )
+ @chunk = ""
+ @group = :normal
+ @callback = block
+ @text = StringScanner.new( text )
+ setup
+ end
+
+ # Subclasses may override this method to provide implementation-specific
+ # setup logic.
+ def setup
+ end
+
+ # Finish tokenizing. This flushes the buffer, yielding any remaining text
+ # to the client.
+ def finish
+ start_group nil
+ teardown
+ end
+
+ # Subclasses may override this method to provide implementation-specific
+ # teardown logic.
+ def teardown
+ end
+
+ # Subclasses must implement this method, which is called for each iteration
+ # of the tokenization process. This method may extract multiple tokens.
+ def step
+ raise NotImplementedError, "subclasses must implement #step"
+ end
+
+ # Begins tokenizing the given text, calling #step until the text has been
+ # exhausted.
+ def tokenize( text, &block )
+ start text, &block
+ step until @text.eos?
+ finish
+ end
+
+ # Specify a set of tokenizer-specific options. Each tokenizer may (or may
+ # not) publish any options, but if a tokenizer does those options may be
+ # used to specify optional behavior.
+ def set( opts={} )
+ ( @options ||= Hash.new ).update opts
+ end
+
+ # Get the value of the specified option.
+ def option(opt)
+ @options ? @options[opt] : nil
+ end
+
+ private
+
+ EOL = /(?=\r\n?|\n|$)/
+
+ # A convenience for delegating method calls to the scanner.
+ def self.delegate( sym )
+ define_method( sym ) { |*a| @text.__send__( sym, *a ) }
+ end
+
+ delegate :bol?
+ delegate :eos?
+ delegate :scan
+ delegate :scan_until
+ delegate :check
+ delegate :check_until
+ delegate :getch
+ delegate :matched
+ delegate :pre_match
+ delegate :peek
+ delegate :pos
+
+ # Access the n-th subgroup from the most recent match.
+ def subgroup(n)
+ @text[n]
+ end
+
+ # Append the given data to the currently active chunk.
+ def append( data )
+ @chunk << data
+ end
+
+ # Request that a new group be started. If the current group is the same
+ # as the group being requested, a new group will not be created. If a new
+ # group is created and the current chunk is not empty, the chunk's
+ # contents will be yielded to the client as a token, and then cleared.
+ #
+ # After the new group is started, if +data+ is non-nil it will be appended
+ # to the chunk.
+ def start_group( gr, data=nil )
+ flush_chunk if gr != @group
+ @group = gr
+ @chunk << data if data
+ end
+
+ def start_region( gr, data=nil )
+ flush_chunk
+ @group = gr
+ @callback.call( Token.new( data||"", @group, :region_open ) )
+ end
+
+ def end_region( gr, data=nil )
+ flush_chunk
+ @group = gr
+ @callback.call( Token.new( data||"", @group, :region_close ) )
+ end
+
+ def flush_chunk
+ @callback.call( Token.new( @chunk, @group ) ) unless @chunk.empty?
+ @chunk = ""
+ end
+
+ def subtokenize( syntax, text )
+ tokenizer = Syntax.load( syntax )
+ tokenizer.set @options if @options
+ flush_chunk
+ tokenizer.tokenize( text, &@callback )
+ end
+ end
+
+
+ # A default tokenizer for handling syntaxes that are not explicitly handled
+ # elsewhere. It simply yields the given text as a single token.
+ class Default
+
+ # Yield the given text as a single token.
+ def tokenize( text )
+ yield Token.new( text, :normal )
+ end
+
+ end
+
+ # A hash for registering syntax implementations.
+ SYNTAX = Hash.new( Default )
+
+ # Load the implementation of the requested syntax. If the syntax cannot be
+ # found, or if it cannot be loaded for whatever reason, the Default syntax
+ # handler will be returned.
+ def load( syntax )
+ begin
+ require "app/syntax/lang/#{syntax}"
+ rescue LoadError
+ end
+ SYNTAX[ syntax ].new
+ end
+ module_function :load
+
+ # Return an array of the names of supported syntaxes.
+ def all
+ lang_dir = File.join(File.dirname(__FILE__), "syntax", "lang")
+ Dir["#{lang_dir}/*.rb"].map { |path| File.basename(path, ".rb") }
+ end
+ module_function :all
+
+
+end
diff --git a/app/app/syntax/convertors/abstract.rb b/app/app/syntax/convertors/abstract.rb
new file mode 100644
index 0000000..461e2db
--- /dev/null
+++ b/app/app/syntax/convertors/abstract.rb
@@ -0,0 +1,27 @@
+require 'app/syntax/common'
+
+module HH::Syntax
+ module Convertors
+
+ # The abstract ancestor class for all convertors. It implements a few
+ # convenience methods to provide a common interface for all convertors.
+ class Abstract
+
+ # A reference to the tokenizer used by this convertor.
+ attr_reader :tokenizer
+
+ # A convenience method for instantiating a new convertor for a
+ # specific syntax.
+ def self.for_syntax( syntax )
+ new( Syntax.load( syntax ) )
+ end
+
+ # Creates a new convertor that uses the given tokenizer.
+ def initialize( tokenizer )
+ @tokenizer = tokenizer
+ end
+
+ end
+
+ end
+end
diff --git a/app/app/syntax/convertors/html.rb b/app/app/syntax/convertors/html.rb
new file mode 100644
index 0000000..bbea262
--- /dev/null
+++ b/app/app/syntax/convertors/html.rb
@@ -0,0 +1,51 @@
+require 'app/syntax/convertors/abstract'
+
+module HH::Syntax
+ module Convertors
+
+ # A simple class for converting a text into HTML.
+ class HTML < Abstract
+
+ # Converts the given text to HTML, using spans to represent token groups
+ # of any type but <tt>:normal</tt> (which is always unhighlighted). If
+ # +pre+ is +true+, the html is automatically wrapped in pre tags.
+ def convert( text, pre=true )
+ html = ""
+ html << "<pre>" if pre
+ regions = []
+ @tokenizer.tokenize( text ) do |tok|
+ value = html_escape(tok)
+ case tok.instruction
+ when :region_close then
+ regions.pop
+ html << "</span>"
+ when :region_open then
+ regions.push tok.group
+ html << "<span class=\"#{tok.group}\">#{value}"
+ else
+ if tok.group == ( regions.last || :normal )
+ html << value
+ else
+ html << "<span class=\"#{tok.group}\">#{value}</span>"
+ end
+ end
+ end
+ html << "</span>" while regions.pop
+ html << "</pre>" if pre
+ html
+ end
+
+ private
+
+ # Replaces some characters with their corresponding HTML entities.
+ def html_escape( string )
+ string.gsub( /&/, "&amp;" ).
+ gsub( /</, "&lt;" ).
+ gsub( />/, "&gt;" ).
+ gsub( /"/, "&quot;" )
+ end
+
+ end
+
+ end
+end
diff --git a/app/app/syntax/lang/ruby.rb b/app/app/syntax/lang/ruby.rb
new file mode 100644
index 0000000..407db20
--- /dev/null
+++ b/app/app/syntax/lang/ruby.rb
@@ -0,0 +1,317 @@
+require 'app/syntax/common'
+
+module HH::Syntax
+
+ # A tokenizer for the Ruby language. It recognizes all common syntax
+ # (and some less common syntax) but because it is not a true lexer, it
+ # will make mistakes on some ambiguous cases.
+ class Ruby < Tokenizer
+
+ # The list of all identifiers recognized as keywords.
+ KEYWORDS =
+ %w{if then elsif else end begin do rescue ensure while for
+ class module def yield raise until unless and or not when
+ case super undef break next redo retry in return alias
+ defined?}
+
+ # Perform ruby-specific setup
+ def setup
+ @selector = false
+ @allow_operator = false
+ @heredocs = []
+ end
+
+ # Step through a single iteration of the tokenization process.
+ def step
+ case
+ when bol? && check( /=begin/ )
+ start_group( :comment, scan_until( /^=end#{EOL}/ ) )
+ when bol? && check( /__END__#{EOL}/ )
+ start_group( :comment, scan_until( /\Z/ ) )
+ else
+ case
+ when check( /def\s+/ )
+ start_group :keyword, scan( /def\s+/ )
+ start_group :method, scan_until( /(?=[;(\s]|#{EOL})/ )
+ when check( /class\s+/ )
+ start_group :keyword, scan( /class\s+/ )
+ start_group :class, scan_until( /(?=[;\s<]|#{EOL})/ )
+ when check( /module\s+/ )
+ start_group :keyword, scan( /module\s+/ )
+ start_group :module, scan_until( /(?=[;\s]|#{EOL})/ )
+ when check( /::/ )
+ start_group :punct, scan(/::/)
+ when check( /:"/ )
+ start_group :symbol, scan(/:/)
+ scan_delimited_region :symbol, :symbol, "", true
+ @allow_operator = true
+ when check( /:'/ )
+ start_group :symbol, scan(/:/)
+ scan_delimited_region :symbol, :symbol, "", false
+ @allow_operator = true
+ when scan( /:[_a-zA-Z@$][$@\w]*[=!?]?/ )
+ start_group :symbol, matched
+ @allow_operator = true
+ when scan( /\?(\\[^\n\r]|[^\\\n\r\s])/ )
+ start_group :char, matched
+ @allow_operator = true
+ when check( /(__FILE__|__LINE__|true|false|nil|self)[?!]?/ )
+ if @selector || matched[-1] == ?? || matched[-1] == ?!
+ start_group :ident,
+ scan(/(__FILE__|__LINE__|true|false|nil|self)[?!]?/)
+ else
+ start_group :constant,
+ scan(/(__FILE__|__LINE__|true|false|nil|self)/)
+ end
+ @selector = false
+ @allow_operator = true
+ when scan(/0([bB][01]+|[oO][0-7]+|[dD][0-9]+|[xX][0-9a-fA-F]+)/)
+ start_group :number, matched
+ @allow_operator = true
+ else
+ case peek(2)
+ when "%r"
+ scan_delimited_region :punct, :regex, scan( /../ ), true
+ @allow_operator = true
+ when "%w", "%q"
+ scan_delimited_region :punct, :string, scan( /../ ), false
+ @allow_operator = true
+ when "%s"
+ scan_delimited_region :punct, :symbol, scan( /../ ), false
+ @allow_operator = true
+ when "%W", "%Q", "%x"
+ scan_delimited_region :punct, :string, scan( /../ ), true
+ @allow_operator = true
+ when /%[^\sa-zA-Z0-9]/
+ scan_delimited_region :punct, :string, scan( /./ ), true
+ @allow_operator = true
+ when "<<"
+ saw_word = ( chunk[-1,1] =~ /[\w!?]/ )
+ start_group :punct, scan( /<</ )
+ if saw_word
+ @allow_operator = false
+ return
+ end
+
+ float_right = scan( /-/ )
+ append "-" if float_right
+ if ( type = scan( /['"]/ ) )
+ append type
+ delim = scan_until( /(?=#{type})/ )
+ if delim.nil?
+ append scan_until( /\Z/ )
+ return
+ end
+ else
+ delim = scan( /\w+/ ) or return
+ end
+ start_group :constant, delim
+ start_group :punct, scan( /#{type}/ ) if type
+ @heredocs << [ float_right, type, delim ]
+ @allow_operator = true
+ else
+ case peek(1)
+ when /[\n\r]/
+ unless @heredocs.empty?
+ scan_heredoc(*@heredocs.shift)
+ else
+ start_group :normal, scan( /\s+/ )
+ end
+ @allow_operator = false
+ when /\s/
+ start_group :normal, scan( /\s+/ )
+ when "#"
+ start_group :comment, scan( /#[^\n\r]*/ )
+ when /[A-Z]/
+ start_group @selector ? :ident : :constant, scan( /\w+/ )
+ @allow_operator = true
+ when /[a-z_]/
+ word = scan( /\w+[?!]?/ )
+ if !@selector && KEYWORDS.include?( word )
+ start_group :keyword, word
+ @allow_operator = false
+ elsif
+ start_group :ident, word
+ @allow_operator = true
+ end
+ @selector = false
+ when /\d/
+ start_group :number,
+ scan( /[\d_]+(\.[\d_]+)?([eE][\d_]+)?/ )
+ @allow_operator = true
+ when '"'
+ scan_delimited_region :punct, :string, "", true
+ @allow_operator = true
+ when '/'
+ if @allow_operator
+ start_group :punct, scan(%r{/})
+ @allow_operator = false
+ else
+ scan_delimited_region :punct, :regex, "", true
+ @allow_operator = true
+ end
+ when "'"
+ scan_delimited_region :punct, :string, "", false
+ @allow_operator = true
+ when "."
+ dots = scan( /\.{1,3}/ )
+ start_group :punct, dots
+ @selector = ( dots.length == 1 )
+ when /[@]/
+ start_group :attribute, scan( /@{1,2}\w*/ )
+ @allow_operator = true
+ when /[$]/
+ start_group :global, scan(/\$/)
+ start_group :global, scan( /\w+|./ ) if check(/./)
+ @allow_operator = true
+ when /[-!?*\/+=<>(\[\{}:;,&|%]/
+ start_group :punct, scan(/./)
+ @allow_operator = false
+ when /[)\]]/
+ start_group :punct, scan(/./)
+ @allow_operator = true
+ else
+ # all else just falls through this, to prevent
+ # infinite loops...
+ append getch
+ end
+ end
+ end
+ end
+ end
+
+ private
+
+ # Scan a delimited region of text. This handles the simple cases (strings
+ # delimited with quotes) as well as the more complex cases of %-strings
+ # and here-documents.
+ #
+ # * +delim_group+ is the group to use to classify the delimiters of the
+ # region
+ # * +inner_group+ is the group to use to classify the contents of the
+ # region
+ # * +starter+ is the text to use as the starting delimiter
+ # * +exprs+ is a boolean flag indicating whether the region is an
+ # interpolated string or not
+ # * +delim+ is the text to use as the delimiter of the region. If +nil+,
+ # the next character will be treated as the delimiter.
+ # * +heredoc+ is either +false+, meaning the region is not a heredoc, or
+ # <tt>:flush</tt> (meaning the delimiter must be flushed left), or
+ # <tt>:float</tt> (meaning the delimiter doens't have to be flush left).
+ def scan_delimited_region( delim_group, inner_group, starter, exprs,
+ delim=nil, heredoc=false )
+ # begin
+ if !delim
+ start_group delim_group, starter
+ delim = scan( /./ )
+ append delim
+
+ delim = case delim
+ when '{' then '}'
+ when '(' then ')'
+ when '[' then ']'
+ when '<' then '>'
+ else delim
+ end
+ end
+
+ start_region inner_group
+
+ items = "\\\\|"
+ if heredoc
+ items << "(^"
+ items << '\s*' if heredoc == :float
+ items << "#{Regexp.escape(delim)}\s*?)#{EOL}"
+ else
+ items << "#{Regexp.escape(delim)}"
+ end
+ items << "|#(\\$|@@?|\\{)" if exprs
+ items = Regexp.new( items )
+
+ loop do
+ p = pos
+ match = scan_until( items )
+ if match.nil?
+ start_group inner_group, scan_until( /\Z/ )
+ break
+ else
+ text = pre_match[p..-1]
+ start_group inner_group, text if text.length > 0
+ case matched.strip
+ when "\\"
+ unless exprs
+ case peek(1)
+ when "'"
+ scan(/./)
+ start_group :escape, "\\'"
+ when "\\"
+ scan(/./)
+ start_group :escape, "\\\\"
+ else
+ start_group inner_group, "\\"
+ end
+ else
+ start_group :escape, "\\"
+ c = getch
+ append c
+ case c
+ when 'x'
+ append scan( /[a-fA-F0-9]{1,2}/ )
+ when /[0-7]/
+ append scan( /[0-7]{0,2}/ )
+ end
+ end
+ when delim
+ end_region inner_group
+ start_group delim_group, matched
+ break
+ when /^#/
+ do_highlight = (option(:expressions) == :highlight)
+ start_region :expr if do_highlight
+ start_group :expr, matched
+ case matched[1]
+ when ?{
+ depth = 1
+ content = ""
+ while depth > 0
+ p = pos
+ c = scan_until( /[\{}]/ )
+ if c.nil?
+ content << scan_until( /\Z/ )
+ break
+ else
+ depth += ( matched == "{" ? 1 : -1 )
+ content << pre_match[p..-1]
+ content << matched if depth > 0
+ end
+ end
+ if do_highlight
+ subtokenize "ruby", content
+ start_group :expr, "}"
+ else
+ append content + "}"
+ end
+ when ?$, ?@
+ append scan( /\w+/ )
+ end
+ end_region :expr if do_highlight
+ else raise "unexpected match on #{matched}"
+ end
+ end
+ end
+ end
+
+ # Scan a heredoc beginning at the current position.
+ #
+ # * +float+ indicates whether the delimiter may be floated to the right
+ # * +type+ is +nil+, a single quote, or a double quote
+ # * +delim+ is the delimiter to look for
+ def scan_heredoc(float, type, delim)
+ scan_delimited_region( :constant, :string, "", type != "'",
+ delim, float ? :float : :flush )
+ end
+ end
+
+ SYNTAX["ruby"] = Ruby
+
+end
diff --git a/app/app/syntax/lang/xml.rb b/app/app/syntax/lang/xml.rb
new file mode 100644
index 0000000..02ba798
--- /dev/null
+++ b/app/app/syntax/lang/xml.rb
@@ -0,0 +1,108 @@
+require 'app/syntax/common'
+
+module HH::Syntax
+
+ # A simple implementation of an XML lexer. It handles most cases. It is
+ # not a validating lexer, meaning it will happily process invalid XML without
+ # complaining.
+ class XML < Tokenizer
+
+ # Initialize the lexer.
+ def setup
+ @in_tag = false
+ end
+
+ # Step through a single iteration of the tokenization process. This will
+ # yield (potentially) many tokens, and possibly zero tokens.
+ def step
+ start_group :normal, matched if scan( /\s+/ )
+ if @in_tag
+ case
+ when scan( /([-\w]+):([-\w]+)/ )
+ start_group :namespace, subgroup(1)
+ start_group :punct, ":"
+ start_group :attribute, subgroup(2)
+ when scan( /\d+/ )
+ start_group :number, matched
+ when scan( /[-\w]+/ )
+ start_group :attribute, matched
+ when scan( %r{[/?]?>} )
+ @in_tag = false
+ start_group :punct, matched
+ when scan( /=/ )
+ start_group :punct, matched
+ when scan( /["']/ )
+ scan_string matched
+ else
+ append getch
+ end
+ elsif ( text = scan_until( /(?=[<&])/ ) )
+ start_group :normal, text unless text.empty?
+ if scan(/<!--.*?(-->|\Z)/m)
+ start_group :comment, matched
+ else
+ case peek(1)
+ when "<"
+ start_group :punct, getch
+ case peek(1)
+ when "?"
+ append getch
+ when "/"
+ append getch
+ when "!"
+ append getch
+ end
+ start_group :normal, matched if scan( /\s+/ )
+ if scan( /([-\w]+):([-\w]+)/ )
+ start_group :namespace, subgroup(1)
+ start_group :punct, ":"
+ start_group :tag, subgroup(2)
+ elsif scan( /[-\w]+/ )
+ start_group :tag, matched
+ end
+ @in_tag = true
+ when "&"
+ if scan( /&\S{1,10};/ )
+ start_group :entity, matched
+ else
+ start_group :normal, scan( /&/ )
+ end
+ end
+ end
+ else
+ append scan_until( /\Z/ )
+ end
+ end
+
+ private
+
+ # Scan the string starting at the current position, with the given
+ # delimiter character.
+ def scan_string( delim )
+ start_group :punct, delim
+ match = /(?=[&\\]|#{delim})/
+ loop do
+ break unless ( text = scan_until( match ) )
+ start_group :string, text unless text.empty?
+ case peek(1)
+ when "&"
+ if scan( /&\S{1,10};/ )
+ start_group :entity, matched
+ else
+ start_group :string, getch
+ end
+ when "\\"
+ start_group :string, getch
+ append getch || ""
+ when delim
+ start_group :punct, getch
+ break
+ end
+ end
+ end
+
+ end
+
+ SYNTAX["xml"] = XML
+
+end
diff --git a/app/app/syntax/lang/yaml.rb b/app/app/syntax/lang/yaml.rb
new file mode 100644
index 0000000..f6d44ad
--- /dev/null
+++ b/app/app/syntax/lang/yaml.rb
@@ -0,0 +1,105 @@
+require 'app/syntax/common'
+
+module HH::Syntax
+
+ # A simple implementation of an YAML lexer. It handles most cases. It is
+ # not a validating lexer.
+ class YAML < Tokenizer
+
+ # Step through a single iteration of the tokenization process. This will
+ # yield (potentially) many tokens, and possibly zero tokens.
+ def step
+ if bol?
+ case
+ when scan(/---(\s*.+)?$/)
+ start_group :document, matched
+ when scan(/(\s*)([a-zA-Z][-\w]*)(\s*):/)
+ start_group :normal, subgroup(1)
+ start_group :key, subgroup(2)
+ start_group :normal, subgroup(3)
+ start_group :punct, ":"
+ when scan(/(\s*)-/)
+ start_group :normal, subgroup(1)
+ start_group :punct, "-"
+ when scan(/\s*$/)
+ start_group :normal, matched
+ when scan(/#.*$/)
+ start_group :comment, matched
+ else
+ append getch
+ end
+ else
+ case
+ when scan(/[\n\r]+/)
+ start_group :normal, matched
+ when scan(/[ \t]+/)
+ start_group :normal, matched
+ when scan(/!+(.*?^)?\S+/)
+ start_group :type, matched
+ when scan(/&\S+/)
+ start_group :anchor, matched
+ when scan(/\*\S+/)
+ start_group :ref, matched
+ when scan(/\d\d:\d\d:\d\d/)
+ start_group :time, matched
+ when scan(/\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d(\.\d+)? [-+]\d\d:\d\d/)
+ start_group :date, matched
+ when scan(/['"]/)
+ start_group :punct, matched
+ scan_string matched
+ when scan(/:\w+/)
+ start_group :symbol, matched
+ when scan(/[:]/)
+ start_group :punct, matched
+ when scan(/#.*$/)
+ start_group :comment, matched
+ when scan(/>-?/)
+ start_group :punct, matched
+ start_group :normal, scan(/.*$/)
+ append getch until eos? || bol?
+ return if eos?
+ indent = check(/ */)
+ start_group :string
+ loop do
+ line = check_until(/[\n\r]|\Z/)
+ break if line.nil?
+ if line.chomp.length > 0
+ this_indent = line.chomp.match( /^\s*/ )[0]
+ break if this_indent.length < indent.length
+ end
+ append scan_until(/[\n\r]|\Z/)
+ end
+ else
+ start_group :normal, scan_until(/(?=$|#)/)
+ end
+ end
+ end
+
+ private
+
+ def scan_string( delim )
+ regex = /(?=[#{delim=="'" ? "" : "\\\\"}#{delim}])/
+ loop do
+ text = scan_until( regex )
+ if text.nil?
+ start_group :string, scan_until( /\Z/ )
+ break
+ else
+ start_group :string, text unless text.empty?
+ end
+
+ case peek(1)
+ when "\\"
+ start_group :expr, scan(/../)
+ else
+ start_group :punct, getch
+ break
+ end
+ end
+ end
+
+ end
+
+ SYNTAX["yaml"] = YAML
+
+end
diff --git a/app/app/syntax/markup.rb b/app/app/syntax/markup.rb
new file mode 100644
index 0000000..f0a5207
--- /dev/null
+++ b/app/app/syntax/markup.rb
@@ -0,0 +1,214 @@
+# syntax highlighting
+
+require 'app/syntax/common'
+
+module HH::Markup
+
+ TOKENIZER = HH::Syntax.load "ruby"
+ COLORS = {
+ :comment => {:stroke => "#887"},
+ :keyword => {:stroke => "#111"},
+ :method => {:stroke => "#C09", :weight => "bold"},
+ # :class => {:stroke => "#0c4", :weight => "bold"},
+ # :module => {:stroke => "#050"},
+ # :punct => {:stroke => "#668", :weight => "bold"},
+ :symbol => {:stroke => "#C30"},
+ :string => {:stroke => "#C90"},
+ :number => {:stroke => "#396" },
+ :regex => {:stroke => "#000", :fill => "#FFC" },
+ # :char => {:stroke => "#f07"},
+ :attribute => {:stroke => "#369" },
+ # :global => {:stroke => "#7FB" },
+ :expr => {:stroke => "#722" },
+ # :escape => {:stroke => "#277" }
+ :ident => {:stroke => "#A79"},
+ :constant => {:stroke => "#630", :weight => "bold"},
+ :class => {:stroke => "#630", :weight => "bold"},
+ :matching => {:stroke => "#ff0", :weight => "bold"},
+ }
+
+
+ def highlight str, pos=nil, colors=COLORS
+ tokens = []
+ TOKENIZER.tokenize(str) do |t|
+ if t.group == :punct
+ # split punctuation into single characters tokens
+ # TODO: to it in the parser
+ tokens += t.split('').map{|s| HH::Syntax::Token.new(s, :punct)}
+ else
+ # add token as is
+ tokens << t
+ end
+ end
+
+ res = []
+ tokens.each do |token|
+ res <<
+ if colors[token.group]
+ span(token, colors[token.group])
+ elsif colors[:any]
+ span(token, colors[:any])
+ else
+ token
+ end
+ end
+
+ if pos.nil? or pos < 0
+ return res
+ end
+
+ token_index, matching_index = matching_token(tokens, pos)
+
+ if token_index
+ res[token_index] = span(tokens[token_index], colors[:matching])
+ if matching_index
+ res[matching_index] = span(tokens[matching_index], colors[:matching])
+ end
+ end
+
+ res
+ end
+
+private
+ def matching_token(tokens, pos)
+ curr_pos = 0
+ token_index = nil
+ tokens.each_with_index do |t, i|
+ curr_pos += t.size
+ if token_index.nil? and curr_pos >= pos
+ token_index = i
+ break
+ end
+ end
+ if token_index.nil? then return nil end
+
+ match = matching_token_at_index(tokens, token_index);
+ if match.nil? and curr_pos == pos and token_index < tokens.size-1
+ # try the token before the cursor, instead of the one after
+ token_index += 1
+ match = matching_token_at_index(tokens, token_index)
+ end
+
+ if match
+ [token_index, match]
+ else
+ nil
+ end
+ end
+
+
+ def matching_token_at_index(tokens, index)
+ starts, ends, direction = matching_tokens(tokens, index)
+ if starts.nil?
+ return nil
+ end
+
+ stack_level = 1
+ index += direction
+ while index >= 0 and index < tokens.size
+ # TODO separate space in the tokenizer
+ t = tokens[index].gsub(/\s/, '')
+ if ends.include?(t) and not as_modifier?(tokens, index)
+ stack_level -= 1
+ return index if stack_level == 0
+ elsif starts.include?(t) and not as_modifier?(tokens, index)
+ stack_level += 1
+ end
+ index += direction
+ end
+ # no matching token found
+ return nil
+ end
+
+ # returns an array of tokens matching and the direction
+ def matching_tokens(tokens, index)
+ # TODO separate space in the tokenizer
+ token = tokens[index].gsub(/\s/, '')
+ starts = [token]
+ if OPEN_BRACKETS[token]
+ direction = 1
+ ends = [OPEN_BRACKETS[token]]
+ elsif CLOSE_BRACKETS[token]
+ direction = -1
+ ends = [CLOSE_BRACKETS[token]]
+ elsif OPEN_BLOCK.include?(token)
+ if as_modifier?(tokens, index)
+ return nil
+ end
+ direction = 1
+ ends = ['end']
+ starts = OPEN_BLOCK
+ elsif token == 'end'
+ direction = -1
+ ends = OPEN_BLOCK
+ else
+ return nil
+ end
+
+ [starts, ends, direction]
+ end
+
+ def as_modifier?(tokens, index)
+
+ if not MODIFIERS.include? tokens[index].gsub(/\s/, '')
+ return false
+ end
+
+ index -= 1
+ # find last index before the token that is no space
+ index -= 1 while index >= 0 and tokens[index] =~ /\A[ \t]*\z/
+
+ if index < 0
+ # first character of the string
+ false
+ elsif tokens[index] =~ /\n[ \t]*\Z/
+ # first token of the line
+ false
+ elsif tokens[index].group == :punct
+ # preceded by a punctuation token on the same line
+ i = tokens[index].rindex(/\S/)
+ punc = tokens[index][i, 1]
+ # true if the preceeding statement was terminating
+ not NON_TERMINATING.include?(punc)
+ else
+ # preceded by a non punctuation token on the same line
+ true
+ end
+ end
+
+
+ OPEN_BRACKETS = {
+ '{' => '}',
+ '(' => ')',
+ '[' => ']',
+ }
+
+ #close_bracket = {}
+ #OPEN_BRACKETS.each{|open, close| opens_bracket[close] = open}
+ #CLOSE_BRACKETS = opens_bracket
+ # the following is more readable :)
+ CLOSE_BRACKETS = {
+ '}' => '{',
+ ')' => '(',
+ ']' => '[',
+ }
+
+ BRACKETS = CLOSE_BRACKETS.keys + OPEN_BRACKETS.keys
+
+ OPEN_BLOCK = [
+ 'def',
+ 'class',
+ 'module',
+ 'do',
+ 'if',
+ 'unless',
+ 'while',
+ 'until',
+ 'begin',
+ 'for'
+ ]
+
+ MODIFIERS = %w[if unless while until]
+
+ NON_TERMINATING = %w{+ - * / , . = ~ < > ( [}
+end
diff --git a/app/app/syntax/version.rb b/app/app/syntax/version.rb
new file mode 100644
index 0000000..72d93a6
--- /dev/null
+++ b/app/app/syntax/version.rb
@@ -0,0 +1,11 @@
+# not used anywhere
+
+#module HH::Syntax
+# module Version
+# MAJOR=1
+# MINOR=0
+# TINY=0
+#
+# STRING=[MAJOR,MINOR,TINY].join('.')
+# end
+#end
diff --git a/app/app/ui/completion.rb b/app/app/ui/completion.rb
new file mode 100644
index 0000000..91bacf9
--- /dev/null
+++ b/app/app/ui/completion.rb
@@ -0,0 +1,183 @@
+# from the irb source code
+
+module HH::InputCompletor
+
+ ReservedWords = [
+ "BEGIN", "END",
+ "alias", "and",
+ "begin", "break",
+ "case", "class",
+ "def", "defined", "do",
+ "else", "elsif", "end", "ensure",
+ "false", "for",
+ "if", "in",
+ "module",
+ "next", "nil", "not",
+ "or",
+ "redo", "rescue", "retry", "return",
+ "self", "super",
+ "then", "true",
+ "undef", "unless", "until",
+ "when", "while",
+ "yield",
+ ]
+
+ def self.complete input, bind
+ case input
+ when /^(\/[^\/]*\/)\.([^.]*)$/
+ # Regexp
+ receiver = $1
+ message = Regexp.quote($2)
+
+ candidates = Regexp.instance_methods(true)
+ select_message(receiver, message, candidates)
+
+ when /^([^\]]*\])\.([^.]*)$/
+ # Array
+ receiver = $1
+ message = Regexp.quote($2)
+
+ candidates = Array.instance_methods(true)
+ select_message(receiver, message, candidates)
+
+ when /^([^\}]*\})\.([^.]*)$/
+ # Proc or Hash
+ receiver = $1
+ message = Regexp.quote($2)
+
+ candidates = Proc.instance_methods(true) | Hash.instance_methods(true)
+ select_message(receiver, message, candidates)
+
+ when /^(:[^:.]*)$/
+ # Symbol
+ if Symbol.respond_to?(:all_symbols)
+ sym = $1
+ candidates = Symbol.all_symbols.collect{|s| ":" + s.id2name}
+ candidates.grep(/^#{sym}/)
+ else
+ []
+ end
+
+ when /^::([A-Z][^:\.\(]*)$/
+ # Absolute Constant or class methods
+ receiver = $1
+ candidates = Object.constants
+ candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
+
+ when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
+ # Constant or class methods
+ receiver = $1
+ message = Regexp.quote($4)
+ begin
+ candidates = eval("#{receiver}.constants | #{receiver}.methods", bind)
+ rescue Exception
+ candidates = []
+ end
+ candidates.grep(/^#{message}/).collect{|e| receiver + "::" + e}
+
+ when /^(:[^:.]+)\.([^.]*)$/
+ # Symbol
+ receiver = $1
+ message = Regexp.quote($2)
+
+ candidates = Symbol.instance_methods(true)
+ select_message(receiver, message, candidates)
+
+ when /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)\.([^.]*)$/
+ # Numeric
+ receiver = $1
+ message = Regexp.quote($5)
+
+ begin
+ candidates = eval(receiver, bind).methods
+ rescue Exception
+ candidates = []
+ end
+ select_message(receiver, message, candidates)
+
+ when /^(-?0x[0-9a-fA-F_]+)\.([^.]*)$/
+ # Numeric(0xFFFF)
+ receiver = $1
+ message = Regexp.quote($2)
+
+ begin
+ candidates = eval(receiver, bind).methods
+ rescue Exception
+ candidates = []
+ end
+ select_message(receiver, message, candidates)
+
+ when /^(\$[^.]*)$/
+ candidates = global_variables.grep(Regexp.new(Regexp.quote($1)))
+
+ # when /^(\$?(\.?[^.]+)+)\.([^.]*)$/
+ when /^((\.?[^.]+)+)\.([^.]*)$/
+ # variable
+ receiver = $1
+ message = Regexp.quote($3)
+
+ gv = eval("global_variables", bind)
+ lv = eval("local_variables", bind)
+ cv = eval("self.class.constants", bind)
+
+ if (gv | lv | cv).include?(receiver)
+ # foo.func and foo is local var.
+ candidates = eval("#{receiver}.methods", bind)
+ elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
+ # Foo::Bar.func
+ begin
+ candidates = eval("#{receiver}.methods", bind)
+ rescue Exception
+ candidates = []
+ end
+ else
+ # func1.func2
+ candidates = []
+ ObjectSpace.each_object(Module){|m|
+ begin
+ name = m.name
+ rescue Exception
+ name = ""
+ end
+ next if name != "IRB::Context" and
+ /^(IRB|SLex|RubyLex|RubyToken)/ =~ name
+ candidates.concat m.instance_methods(false)
+ }
+ candidates.sort!
+ candidates.uniq!
+ end
+ select_message(receiver, message, candidates).select{|x| x}
+
+ when /^\.([^.]*)$/
+ # unknown(maybe String)
+
+ receiver = ""
+ message = Regexp.quote($1)
+
+ candidates = String.instance_methods(true)
+ select_message(receiver, message, candidates)
+
+ else
+ candidates = eval("methods | private_methods | local_variables | self.class.constants", bind)
+
+ (candidates|ReservedWords).grep(/^#{Regexp.quote(input)}/)
+ end
+ end
+
+ Operators = ["%", "&", "*", "**", "+", "-", "/",
+ "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
+ "[]", "[]=", "^",]
+
+ def self.select_message(receiver, message, candidates)
+ candidates.grep(/^#{message}/).collect do |e|
+ case e
+ when /^[a-zA-Z_]/
+ receiver + "." + e
+ when /^[0-9]/
+ when *Operators
+ #receiver + " " + e
+ end
+ end
+ end
+
+end
diff --git a/app/app/ui/editor/editor.rb b/app/app/ui/editor/editor.rb
new file mode 100644
index 0000000..450b027
--- /dev/null
+++ b/app/app/ui/editor/editor.rb
@@ -0,0 +1,448 @@
+# the code editor tab contents
+
+class HH::SideTabs::Editor < HH::SideTab
+ # common code between InsertionAction and DeletionAction
+ # on_insert_text and on_delete_text should be called before any subclass
+ # can be used
+ class InsertionDeletionCommand
+
+ def self.on_insert_text &block
+ @@insert_text = block
+ end
+ def self.on_delete_text &block
+ @@delete_text = block
+ end
+
+ # action to insert/delete str to text at position pos
+ def initialize pos, str
+ @position, @string = pos, str
+ end
+ def insert
+ @@insert_text.call(@position, @string)
+ end
+ def delete
+ @@delete_text.call(@position, @string.size)
+ end
+
+ protected
+ attr_accessor :position, :string
+ end
+
+ class InsertionCommand < InsertionDeletionCommand
+ alias execute insert
+ alias unexecute delete
+
+ # returns nil if not mergeble
+ def merge_with second
+ if second.class != self.class
+ nil
+ elsif second.position != self.position + self.string.size
+ nil
+ elsif second.string == "\n"
+ nil # newlines always start a new command
+ else
+ self.string += second.string
+ self
+ end
+ end
+ end
+
+ class DeletionCommand < InsertionDeletionCommand
+ alias execute delete
+ alias unexecute insert
+
+ def merge_with second
+ if second.class != self.class
+ nil
+ elsif second.string == "\n"
+ nil
+ elsif second.position == self.position
+ # probably the delete key
+ self.string += second.string
+ self
+ elsif self.position == second.position + second.string.size
+ # probably the backspace key
+ self.position = second.position
+ self.string = second.string + self.string
+ self
+ else
+ nil
+ end
+ end
+ end
+
+ module UndoRedo
+
+ def reset_undo_redo
+ @command_stack = [] # array of actions
+ @stack_position = 0;
+ @last_position = nil
+ end
+
+ # _act was added for consistency with redo_act
+ def undo_command
+ return if @stack_position == 0
+ @stack_position -= 1;
+ @command_stack[@stack_position].unexecute;
+ end
+
+ # _act was added because redo is a keyword
+ def redo_command
+ return if @stack_position == @command_stack.size
+ @command_stack[@stack_position].execute
+ @stack_position += 1;
+ end
+
+ def add_command cmd
+ # all redos get removed
+ @command_stack[@stack_position..-1] = nil
+ last = @command_stack.last
+ if last.nil? or not last.merge_with(cmd)
+ # create new command
+ @command_stack[@stack_position] = cmd
+ @stack_position += 1
+ end
+ end
+ end
+end # module HH::Editor
+
+class HH::SideTabs::Editor
+ include HH::Markup
+ include UndoRedo
+
+ def content
+ draw_content
+ end
+
+ def load script
+ if not @save_button.hidden
+ # current script is unsaved
+ name = @script[:name] || "An unnamed program"
+ if not confirm("#{name} has not been saved, if you continue \n" +
+ " all unsaved modifications will be lost")
+ return false
+ end
+ end
+ clear {draw_content script}
+ true
+ end
+
+ # asks confirmation and then saves (or not if save is)
+ def save_if_confirmed
+ if not @save_button.hidden
+ name = @script[:name] || "unnamed program"
+ question = "Do you want to save modifications to \"#{name}\"?\n" +
+ "Warning: by pressing \"Cancel\" you will lose any \n" +
+ "unsaved modifications."
+ if confirm(question)
+ save @script[:name]
+ true
+ else
+ false
+ end
+ end
+ end
+
+ def draw_content(script = {})
+ @str = script[:script] || ""
+ name = script[:name] || "A New Program"
+ @script = script
+
+ reset_undo_redo
+ InsertionDeletionCommand.on_insert_text {|pos, str| insert_text(pos, str)}
+ InsertionDeletionCommand.on_delete_text {|pos, len| delete_text(pos, len)}
+ @editor = stack :margin_left => 10, :margin_top => 10, :width => 1.0, :height => 92 do
+ @sname = subtitle name, :font => "Lacuna Regular", :size => 22,
+ :margin => 0, :wrap => "trim"
+ @stale = para(script[:mtime] ? "Last saved #{script[:mtime].since} ago." :
+ "Not yet saved.", :margin => 0, :stroke => "#39C")
+ glossb "New Program", :top => 0, :right => 0, :width => 160 do
+ load({})
+ end
+ end
+ stack :margin_left => 0, :width => 1.0, :height => -92 do
+ background white(0.4), :width => 38
+ @scroll =
+ flow :width => 1.0, :height => 1.0, :margin => 2, :scroll => true do
+ stack :width => 37, :margin_right => 6 do
+ @ln = para "1", :font => "Liberation Mono", :size => 10, :stroke => "#777", :align => "right"
+ end
+ stack :width => -37, :margin_left => 6, :margin_bottom => 60 do
+ @t = para "", :font => "Liberation Mono", :size => 10, :stroke => "#662",
+ :wrap => "trim", :margin_right => 28
+ @t.cursor = 0
+ def @t.hit_sloppy(x, y)
+ x -= 6
+ c = hit(x, y)
+ if c
+ c + 1
+ elsif x <= 48
+ hit(48, y)
+ end
+ end
+ end
+ motion do |x, y|
+ c = @t.hit_sloppy(x, y)
+ if c
+ if self.cursor == :arrow
+ self.cursor = :text
+ end
+ if self.mouse[0] == 1 and @clicked
+ if @t.marker.nil?
+ @t.marker = c
+ else
+ @t.cursor = c
+ end
+ end
+ elsif self.cursor == :text
+ self.cursor = :arrow
+ end
+ end
+ release do
+ @clicked = false
+ end
+ click do |_, x, y|
+ c = @t.hit_sloppy(x, y)
+ if c
+ @clicked = true
+ @t.marker = nil
+ @t.cursor = c
+ end
+ update_text
+ end
+ leave { self.cursor = :arrow }
+ end
+ end
+
+ stack :height => 40, :width => 182, :bottom => -3, :right => 0 do
+
+ @copy_button =
+ glossb "Copy", :width => 60, :top => 2, :left => 70 do
+ save(nil)
+ end
+ @save_button =
+ glossb "Save", :width => 60, :top => 2, :left => 70, :hidden => true do
+ if save(script[:name])
+ timer 0.1 do
+ @save_button.hide
+ @copy_button.show
+ @save_to_cloud_button.show
+ end
+ end
+ end
+ @save_to_cloud_button =
+ glossb "Upload", :width => 70, :top => 2, :left => 0 do
+ hacker = Hacker.new :username => HH::PREFS['username'], :password => HH::PREFS['password']
+ hacker.save_program_to_the_cloud script[:name].to_slug, @str
+ alert("Uploaded!")
+ end
+ glossb "Run", :width => 52, :top => 2, :left => 130 do
+ eval(@str, HH.anonymous_binding)
+ end
+ end
+
+ every 20 do
+ if script[:mtime]
+ @stale.text = "Last saved #{script[:mtime].since} ago."
+ end
+ end
+
+ def onkey(k)
+ case k when :shift_home, :shift_end, :shift_up, :shift_left, :shift_down, :shift_right
+ @t.marker = @t.cursor unless @t.marker
+ when :home, :end, :up, :left, :down, :right
+ @t.marker = nil
+ end
+
+ case k
+ when String
+ if k == "\n"
+ # handle indentation
+ ind = indentation_size
+ handle_text_insertion(k)
+ handle_text_insertion(" " * ind) if ind > 0
+ else
+ # usual case
+ handle_text_insertion(k)
+ end
+ when :backspace, :shift_backspace, :control_backspace
+ if @t.cursor > 0 and @t.marker.nil?
+ @t.marker = @t.cursor - 1 # make highlight length at least 1
+ end
+ sel = @t.highlight
+ if sel[0] > 0 or sel[1] > 0
+ handle_text_deletion(*sel)
+ end
+ when :delete
+ sel = @t.highlight
+ sel[1] = 1 if sel[1] == 0
+ handle_text_deletion(*sel)
+ when :tab
+ handle_text_insertion(" ")
+# when :alt_q
+# @action.clear { home }
+ when :control_a, :alt_a
+ @t.marker = 0
+ @t.cursor = @str.length
+ when :control_x, :alt_x
+ if @t.marker
+ sel = @t.highlight
+ self.clipboard = @str[*sel]
+ if sel[1] == 0
+ sel[1] = 1
+ raise "why did this happen??"
+ end
+ handle_text_deletion(*sel)
+ end
+ when :control_c, :alt_c, :control_insertadd_characte
+ if @t.marker
+ self.clipboard = @str[*@t.highlight]
+ end
+ when :control_v, :alt_v, :shift_insert
+ handle_text_insertion(self.clipboard) if self.clipboard
+ when :control_z
+ debug("undo!")
+ undo_command
+ when :control_y, :alt_Z, :shift_alt_z
+ redo_command
+ when :shift_home, :home
+ nl = @str.rindex("\n", @t.cursor - 1) || -1
+ @t.cursor = nl + 1
+ when :shift_end, :end
+ nl = @str.index("\n", @t.cursor) || @str.length
+ @t.cursor = nl
+ when :shift_up, :up
+ if @t.cursor > 0
+ nl = @str.rindex("\n", @t.cursor - 1)
+ if nl
+ horz = @t.cursor - nl
+ upnl = @str.rindex("\n", nl - 1) || -1
+ @t.cursor = upnl + horz
+ @t.cursor = nl if @t.cursor > nl
+ end
+ end
+ when :shift_down, :down
+ nl = @str.index("\n", @t.cursor)
+ if nl
+ if @t.cursor > 0
+ horz = @t.cursor - (@str.rindex("\n", @t.cursor - 1) || -1)
+ else
+ horz = 1
+ end
+ dnl = @str.index("\n", nl + 1) || @str.length
+ @t.cursor = nl + horz
+ @t.cursor = dnl if @t.cursor > dnl
+ end
+ when :shift_right, :right
+ @t.cursor += 1 if @t.cursor < @str.length
+ when :shift_left, :left
+ @t.cursor -= 1 if @t.cursor > 0
+ end
+ if k
+ text_changed
+ end
+
+ update_text
+ end
+
+ spaces = [?\t, ?\s, ?\n]
+
+ keypress do |k|
+ onkey(k)
+ if @t.cursor_top < @scroll.scroll_top
+ @scroll.scroll_top = @t.cursor_top
+ elsif @t.cursor_top + 92 > @scroll.scroll_top + @scroll.height
+ @scroll.scroll_top = (@t.cursor_top + 92) - @scroll.height
+ end
+
+ end
+
+ # for samples do not allow to upload to cloud when just opened
+ @save_to_cloud_button.hide if script[:sample]
+ update_text
+ end
+
+ # saves the file, asks for a new name if a nil argument is passed
+ def save name
+ if name.nil?
+ msg = ""
+ while true
+ name = ask(msg + "Give your program a name.")
+ break if name.nil? or not HH.script_exists?(name)
+ msg = "You already have a program named '" + name + "'.\n"
+ end
+ end
+ if name
+ @script[:name] = name
+ HH.save_script(@script[:name], @str)
+ @script[:mtime] = Time.now
+ @sname.text = @script[:name]
+ @stale.text = "Last saved #{@script[:mtime].since} ago."
+ true
+ else
+ false
+ end
+ end
+
+ def update_text
+ @t.replace *highlight(@str, @t.cursor)
+ @ln.replace [*1..(@str.count("\n")+1)].join("\n")
+ end
+
+ def text_changed
+ if @save_button.hidden
+ @copy_button.hide
+ @save_button.show
+ @save_to_cloud_button.hide
+ end
+ end
+
+ # find the indentation level at the current cursor or marker
+ # whatever occurs first
+ # the result is the number of spaces
+ def indentation_size
+ # TODO marker
+ pos = @str.rindex("\n", @t.cursor-1)
+ return 0 if pos.nil?
+
+ pos += 1
+
+ ind_size = 0
+ while @str[pos, 1] == ' '
+ ind_size += 1
+ pos += 1
+ end
+ ind_size
+ end
+
+ # called when the user wants to insert text
+ def handle_text_insertion str
+ pos, len = @t.highlight;
+ handle_text_deletion(pos, len) if len > 0
+
+ add_command InsertionCommand.new(pos, str)
+ insert_text(pos, str)
+ end
+
+ # called when the user wants to delete text
+ def handle_text_deletion pos, len
+ str = @str[pos, len]
+ return if str.empty? # happens if len == 0 or pos to big
+ add_command DeletionCommand.new(pos, str)
+ delete_text(pos, len)
+ end
+
+ def insert_text pos, text
+ @str.insert(pos, text)
+ @t.cursor = pos + text.size
+ @t.cursor = :marker # XXX ???
+ #update_text
+ end
+
+ def delete_text pos, len
+ @str[pos, len] = "" # TODO use slice?
+ @t.cursor = pos
+ @t.cursor = :marker
+ #update_text
+ end
+end
diff --git a/app/app/ui/lessons.rb b/app/app/ui/lessons.rb
new file mode 100644
index 0000000..14f7081
--- /dev/null
+++ b/app/app/ui/lessons.rb
@@ -0,0 +1,319 @@
+
+# redefines methods, like title and para, for use in the lessons
+# included in HH::LessonContainer
+module HH::LessonContainerText
+ TITLES = {:font => "Lacuna Regular", :stroke => "#e06", :margin => 4}
+ PARAS = {:stroke => "#eec", :size => 11, :margin_bottom => 6}
+ LIST = {:margin_left => 20, :margin => 4, :size => 10}
+ CODE_STYLE = {:size => 9, :margin => 8, :font => "Liberation Mono",
+ :stroke => "#000"}
+ COLORS = {
+ :comment => {:stroke => "#bba"},
+ :keyword => {:stroke => "#FCF90F"},
+ :method => {:stroke => "#C09", :weight => "bold"},
+ :symbol => {:stroke => "#9DF3C6"},
+ :string => {:stroke => "#C9F5A5"},
+ :number => {:stroke => "#C9F5A5"},
+ :regex => {:stroke => "#000", :fill => "#FFC" },
+ :attribute => {:stroke => "#C9F5A5"},
+ :expr => {:stroke => "#f33" },
+ :ident => {:stroke => "#6e7"},
+ :any => {:stroke => "#FFF"},
+ :constant => {:stroke => "#55f", :weight => "bold"},
+ :class => {:stroke => "#55f", :weight => "bold"},
+ :matching => {:stroke => "#f00", :weight => "bold"},
+ }
+
+ # merges options +opts+ with those of +args+ if any
+ def merge_opts(args, opts)
+ res = args.dup
+ if res.last.is_a? Hash
+ # there are already options
+ # keep them
+ orig_opts = res.last
+ orig_opts.replace(opts.merge(orig_opts))
+ else
+ res << opts
+ end
+ res
+ end
+
+ def title *args
+ super *merge_opts( args, TITLES.merge(:size => 22) )
+ end
+ def subtitle *args
+ super *merge_opts( args, TITLES.merge(:size => 14) )
+ end
+ def item *args
+ para *merge_opts( args, LIST )
+ end
+ def para *args
+ super *merge_opts( args, PARAS )
+ end
+ # FileUtils.link gets precedence else, i don't quite understand why that
+ # module is included at all but it is...
+ def link *a, &b; app.link *a, &b end
+ def code *args
+ super *merge_opts( args, {:stroke => "#9de", :fill => "#237"} )
+ end
+ def prompt *args
+ code *merge_opts( args, {:stroke => "#EEE", :fill => "#602"} )
+ end
+
+ include HH::Markup
+ def embed_code str, opts={}
+ stack :margin_bottom => 12 do
+ background "#602", :curve => 4
+ para highlight(str, nil, COLORS), CODE_STYLE
+ if opts[:run_button]
+ stack :top => 0, :right => 2, :width => 70 do
+ stack do
+ background "#8A7", :margin => [0, 2, 0, 2], :curve => 4
+ l = link("Run this", :stroke => "#eee", :underline => "none") do
+ eval(str, TOPLEVEL_BINDING)
+ end
+ para l, :margin => 4, :align => 'center',
+ :weight => 'bold', :size => 9
+ end
+ end
+ end
+ end
+ end
+end
+
+
+
+# the code in the +page+ blocks in the lessons is executed with +self+
+# being a LessonEnvironment, methods of the main app (and thus of shoes):
+# method_missing propagates all calls
+class HH::LessonContainer
+ include HH::LessonContainerText
+
+ # the Shoes slot that contains the lesson
+ attr_accessor :slot
+
+ def initialize lesson_set
+ @lesson_set = lesson_set
+ @event_connections = []
+ end
+
+ # convenience method the access the main shoes application
+ def app
+ @slot.app
+ end
+
+ def method_missing(symbol, *args, &blk)
+ app.send symbol, *args, &blk
+ end
+
+ # part of the lesson DSL, executes the page block
+ def set_content &blk
+ delete_event_connections
+ @slot.clear { instance_eval &blk }
+ end
+
+ def on_event *args, &blk
+ conn = app.on_event(*args, &blk)
+ @event_connections << conn
+ conn
+ end
+
+ # on the event specified in +args+ goes to the next page
+ # if a block is specified it is used as additional condition
+ # the event arguments are passed to the block
+ def next_when *args, &blk
+ if blk
+ unless args.size == 1
+ raise ArgumentError, "if a block is passed there should be no arguments"
+ end
+ cond = HH::EventCondition.new &blk
+ on_event(args[0], cond) {next_page}
+ else
+ on_event(*args) {next_page}
+ end
+ end
+
+ def delete_event_connections
+ @event_connections.each do |ec|
+ app.delete_event_connection ec
+ end
+ @event_connections = []
+ end
+
+ def next_page
+ @lesson_set.next_page
+ end
+end
+
+
+# class to load and execute the level sets
+class HH::LessonSet
+ include HH::Observable
+
+ def initialize name, blk
+ # content of @lessons:
+ # name, pages = @lessons[lesson_n]
+ # title, block = pages[page_n]
+ @lessons = []
+ @name = name
+ @container = HH::LessonContainer.new self
+ instance_eval &blk
+ end
+
+ def init &blk
+ @container.instance_eval &blk
+ end
+
+ # returns only when close gets called
+ def execute_in slot
+ # loads saved lesson and page, of 0, 0, by default
+ # differently from what is displayed in the UI,
+ # internally @lesson and @page start at 0
+ @lesson = (HH::PREFS["tut_lesson_#@name"] || "0").to_i
+ @page = (HH::PREFS["tut_page_#@name"] || "0").to_i
+ @container.slot = slot
+ slot.extend HH::Tooltip
+
+ execute_page
+ @@open_lesson = self
+ end
+
+ # finalization in case of an open lesson
+ def self.close_open_lesson
+ if (defined? @@open_lesson) && @@open_lesson
+ @@open_lesson.save_lesson
+ end
+ end
+
+ def show_menu
+ name, lessons = @name, @lessons
+ lesson_set = self
+ @container.set_content do
+ background gray(0.1)
+ stack :margin => 10, :height => -32, :scroll => true do
+ title name
+
+ lesson_i = 0
+ lessons.each do |name, pages|
+ lesson = lesson_i
+ lesson_i += 1
+
+ subtitle "#{lesson_i} #{name}"#, :stroke => gray(0.9)
+ page_i = 0
+ pages.each do |title, _proc|
+ page = page_i
+ page_i += 1
+ open_page = proc do lesson_set.instance_eval do
+ @lesson, @page = lesson, page
+ execute_page
+ end end
+ para link("#{title}", :stroke => gray(0.9), :click => open_page),
+ :margin_left => 10
+ end
+ end
+ end
+ flow :height => 32, :bottom => 0, :right => 0 do
+ icon_button :x, :right => 10 do
+ lesson_set.close_lesson
+ end
+ end
+ end
+ end
+
+ # displays the page @page of lesson @lesson
+ def execute_page
+ lessons = @lessons
+ lesson, page = @lesson, @page
+ lesson_set = self
+
+ @container.set_content do
+ background gray(0.1)
+
+ lesson_name, pages = lessons[lesson]
+ page_title, page_block = pages[page]
+
+ stack :margin => 10, :height => -32, :scroll => true do
+ # if first page of a lesson display the lesson name
+ if page == 0
+ title "#{lesson+1}. #{lesson_name}"
+ end
+
+ # if first page of a lesson do not display page number
+ page_num = page == 0 ? "" : "#{lesson+1}.#{page+1} "
+ subtitle "#{page_num}#{page_title}"
+
+ instance_eval &page_block
+ end
+
+ flow :height => 32, :bottom => 0, :right => 0 do
+ icon_button :arrow_left, "Previous", :left => 10 do
+ lesson_set.previous_page
+ end
+ icon_button :arrow_right, "Next", :left => 100 do
+ lesson_set.next_page
+ end
+ icon_button :menu, "Index", :left => 55 do
+ lesson_set.show_menu
+ end
+ icon_button :x, "Close", :right => 10 do
+ lesson_set.close_lesson
+ end
+ end
+ end
+ end
+
+ def next_page
+ @page += 1
+ _name, pages = @lessons[@lesson]
+ if @page >= pages.size
+ @page = 0
+ @lesson += 1
+ if @lesson >= @lessons.size
+ @lesson = 0
+ end
+ end
+ execute_page
+ end
+
+ def previous_page
+ @page -= 1
+ if @page < 0
+ @lesson -= 1
+ if @lesson < 0
+ @lesson = @lessons.size-1
+ end
+ _name, pages = @lessons[@lesson]
+ @page = pages.size-1
+ end
+ execute_page
+ end
+
+ # calls finalization
+ def close_lesson
+ save_lesson
+ @container.delete_event_connections
+ @@open_lesson = nil
+ emit :close
+ end
+
+ # called on close to save the current lesson and page
+ def save_lesson
+ HH::PREFS["tut_lesson_#@name"] = @lesson
+ HH::PREFS["tut_page_#@name"] = @page
+ HH.save_prefs
+ end
+
+ # lesson DSL method
+ def lesson name
+ @lessons << [name, []]
+ end
+
+ # lesson DSL method
+ def page title, &blk
+ if @lessons.empty?
+ lesson << "Lesson"
+ end
+ _name, pages = @lessons.last
+ pages << [title, blk]
+ end
+end
diff --git a/app/app/ui/mainwindow.rb b/app/app/ui/mainwindow.rb
new file mode 100755
index 0000000..d06cf84
--- /dev/null
+++ b/app/app/ui/mainwindow.rb
@@ -0,0 +1,130 @@
+require 'app/boot'
+
+
+# methods for the main app
+module HH::App
+ # starts a lesson
+ # returns only once the lesson gets closed
+ include HH::Markup
+ def start_lessons name, blk
+ @main_content.style(:width => -400)
+ @lesson_stack.show
+ l = HH::LessonSet.new(name, blk).execute_in @lesson_stack
+ l.on_event :close do hide_lesson end
+ end
+
+ def hide_lesson
+ @lesson_stack.hide
+ @main_content.style(:width => 1.0)
+ end
+
+ def load_file name={}
+ if gettab(:Editor).load(name)
+ opentab :Editor
+ end
+ end
+
+ # replaces the "Running..." message of the currently running program
+ def say arg
+ # FIXME TODO: DECOMMENT TO REPRODUCE A SEGMENTATION FAULT: para (para "abc")
+ if @program_running
+ txt = case arg
+ when String
+ arg
+ else
+ highlight(txt.inspect)
+ end
+ @program_running.clear{para txt}
+ end
+ end
+
+ def finalization
+ # this method gets called on close
+ HH::LessonSet.close_lesson
+ gettab(:Editor).save_if_confirmed
+
+ HH::PREFS['width'] = width
+ HH::PREFS['height'] = height
+ HH::save_prefs
+ end
+end
+
+w = (HH::PREFS['width'] || '790').to_i
+h = (HH::PREFS['height'] || '550').to_i
+window :title => "Hackety Hack", :width => w, :height => h do
+ HH::APP = self
+ extend HH::App, HH::Widgets, HH::Observable
+ style(Shoes::LinkHover, :fill => nil, :stroke => "#C66")
+ style(Shoes::Link, :stroke => "#377")
+
+ @main_content = flow :width => 1.0, :height => -1 do
+ background "#e9efe0"
+ background "#e9efe0".."#c1c5d0", :height => 150, :bottom => 150
+ end
+ @lesson_stack = stack :hidden => true, :width => 400
+ @lesson_stack.finish do
+ finalization
+ end
+
+ extend HH::HasSideTabs
+ init_tabs @main_content
+
+ addtab :Home, :icon => "tab-home.png"
+ addtab :Editor, :icon => "tab-new.png"
+ addtab :Lessons, :icon => "tab-tour.png"
+ addtab :Help, :icon => "tab-help.png" do
+ Shoes.show_manual
+ end
+ addtab :Cheat, :icon => "tab-cheat.png" do
+ dialog :title => "Hackety Hack - Cheat Sheet", :width => 496 do
+ image "#{HH::STATIC}/hhcheat.png"
+ end
+ end
+ addtab :About, :icon => "tab-hand.png" do
+ about = app.slot.stack :top => 0, :left => 0,
+ :width => 1.0, :height => 1.0 do
+ background white
+ image("#{HH::STATIC}/hhabout.png", :top => 30, :left => 100).
+ click { about.remove }
+ glossb "OK", :top => 500, :left => 0.45, :width => 70, :color => "dark" do
+ about.remove
+ end
+ click { about.remove }
+ end
+ end
+ addtab :Quit, :icon => "tab-quit.png", :position => :bottom do
+ close
+ end
+ addtab :Prefs, :hover => "Preferences", :icon => "tab-properties.png",
+ :position => :bottom
+ opentab :Home
+
+ @tour_notice =
+ stack :top => 46, :left => 22, :width => 250, :height => 54, :hidden => true do
+ fill black(0.6)
+ nostroke
+ shape 0, 20 do
+ line_to 23.6, 0
+ line_to 23.6, 10
+ line_to 0, 0
+ end
+ background black(0.6), :curve => 6, :left => 24, :width => 215
+ para "Check out the Hackety Hack Tour to get started!",
+ :stroke => "#FFF", :margin => 6, :size => 11, :margin_left => 22,
+ :align => "center"
+ end
+
+
+ # splash screen
+ stack :top => 0, :left => 0, :width => 1.0, :height => 1.0 do
+ splash
+ if HH::PREFS['first_run'].nil?
+ @tour_notice.toggle
+ @tour_notice.click { @tour_notice.hide }
+ HH::PREFS['first_run'] = true
+ HH::save_prefs
+ end
+ end
+
+
+end
diff --git a/app/app/ui/tabs/editor.rb b/app/app/ui/tabs/editor.rb
new file mode 100644
index 0000000..f7f35ff
--- /dev/null
+++ b/app/app/ui/tabs/editor.rb
@@ -0,0 +1,2 @@
+# just a redirect
+require 'app/ui/editor/editor'
diff --git a/app/app/ui/tabs/home.rb b/app/app/ui/tabs/home.rb
new file mode 100644
index 0000000..441e276
--- /dev/null
+++ b/app/app/ui/tabs/home.rb
@@ -0,0 +1,175 @@
+# the home tab content
+# partly unfinished: some features have just started being implemented
+
+class HH::SideTabs::Home < HH::SideTab
+# unfinished method that asks if the user wants to upgrade
+# def home_bulletin
+# stack do
+# background "#FF9".."#FFF"
+# subtitle "Upgrade to 0.7?", :font => "Phonetica", :align => "center", :margin => 8
+# para "A New Hackety Hack is Here!", :align => "center", :margin_bottom => 50
+# glossb "Upgrade", :top => 90, :left => 0.42, :width => 100, :color => "red" do
+# alert("No upgrades yet.")
+# end
+# end
+# stack do
+# background black(0.4)..black(0.0)
+# image 1, 10
+# end
+# end
+ def initialize *args, &blk
+ super *args, &blk
+ # never changes so is most efficient to load here
+ @samples = HH.samples
+ end
+
+ # auxiliary method to displays the arrows, for example in case
+ # more than 5 programs have to be listed
+ def home_arrows meth, start, total
+ stack :top => 0, :right => 10 do
+ nex = total > start + 5
+ if start > 0
+ glossb "<<", :top => 0, :right => 10 + (nex ? 100 : 0), :width => 50 do
+ @homepane.clear { send(meth, start - 5) }
+ end
+ end
+ if nex
+ glossb "Next 5 >>", :top => 0, :right => 10, :width => 100 do
+ @homepane.clear { send(meth, start + 5) }
+ end
+ end
+ end
+ end
+
+
+ def home_scripts start=0
+ display_scripts @scripts, start
+ end
+
+ def sample_scripts start=0
+ display_scripts @samples, start, true
+ end
+
+ # auxiliary function used to both display the user programs (scripts)
+ # and the samples
+ def display_scripts scripts, start, samples = false
+ if scripts.empty?
+ para "You have no programs.", :margin_left => 12, :font => "Lacuna Regular"
+ else
+ scripts[start,5].each do |script|
+ stack :margin_left => 8, :margin_top => 4 do
+ flow do
+ britelink "icon-file.png", script[:name], script[:mtime] do
+ load_file script
+ end
+ unless script[:sample]
+ # if it is not a sample file
+ para (link "x" do
+ if confirm("Do you really want to delete \"#{script[:name]}\"?")
+ delete script
+ end
+ end)
+ end
+ end
+ if script[:desc]
+ para script[:desc], :stroke => "#777", :size => 9,
+ :font => "Lacuna Regular", :margin => 0, :margin_left => 18,
+ :margin_right => 140
+ end
+ end
+ end
+ # FIXME: sometimes :sample_scripts
+ m = samples ? :sample_scripts : :home_scripts
+ home_arrows m, start, scripts.length
+ end
+ end
+
+ def delete script
+ File.delete "#{HH::USER}/#{script[:name]}.rb"
+ reset
+ end
+
+ # I think this was meant to show all tables currently in the database
+# def home_tables start = 0
+# if @tables.empty?
+# para "You have no tables.", :margin_left => 12, :font => "Lacuna Regular"
+# else
+# @tables[start,5].each do |name|
+# stack :margin_left => 8, :margin_top => 4 do
+# britelink "icon-table.png", name do
+# alert("No tables page yet.")
+# end
+# end
+# end
+# home_arrows :home_tables, start, @tables.length
+# end
+# end
+
+ def home_lessons
+ para "You have no lessons.", :margin_left => 12, :font => "Lacuna Regular"
+ end
+
+ # add a tab at the top of the homepane, for now there is only one tab:
+ # (Programs)
+ def hometab name, bg, starts = false, &blk
+ tab =
+ stack :margin_top => (starts ? 6 : 10), :margin_left => 14, :width => 120 do
+ off = background bg, :curve => 6, :hidden => starts
+ on = background rgb(233, 239, 224), :curve => (starts ? 6 : 0), :top => (starts ? 0 : 28)
+ title = link(name, :stroke => (starts ? black : white), :underline => "none") do
+ @tabs.each do |t|
+ next unless t.contents[0].hidden
+ t.margin_top = 10
+ t.contents[2].contents[0].stroke = white
+ t.contents[2].size = 11
+ t.contents[1].style(:curve => 0, :top => 28)
+ t.contents[0].show
+ end
+ tab.margin_top = 6
+ title.stroke = black
+ title.parent.size = 13
+ on.style(:curve => 6, :top => 0)
+ off.hide
+ blk[]
+ end
+ para title, :size => (starts ? 13 : 11), :align => "center",
+ :margin => 6, :margin_bottom => 12, :font => "Lacuna Regular"
+ end
+ @tabs << tab
+ end
+
+ def on_click
+ reset
+ end
+
+ # creates the content of the home tab
+ def content
+ image "#{HH::STATIC}/hhhello.png", :bottom => -120, :right => 0
+
+ @tabs, @tables = [], HH::DB.tables
+ @scripts = HH.scripts
+ stack :margin => 0, :margin_left => 0 do
+ stack do
+ background "#CDC", :height => 35
+ background black(0.05)..black(0.2), :height => 38
+ flow do
+ hometab "Programs", "#555", true do
+ @homepane.clear { home_scripts }
+ end
+ hometab "Samples", "#555", false do
+ @homepane.clear { sample_scripts }
+ end
+ end
+ end
+ stack do
+ @homepane = stack do
+ home_scripts
+ end
+ end
+ stack :margin_left => 12 do
+ background rgb(233, 239, 224, 0.85)..rgb(233, 239, 224, 0.0)
+ image 10, 70
+ end
+ end
+ end
+end
diff --git a/app/app/ui/tabs/lessons.rb b/app/app/ui/tabs/lessons.rb
new file mode 100644
index 0000000..9a14af3
--- /dev/null
+++ b/app/app/ui/tabs/lessons.rb
@@ -0,0 +1,32 @@
+module Kernel
+ # topmost instruction for the lessons DSL
+ # starts a lesson set
+ def lesson_set name, &blk
+ HH::SideTabs::Lessons.load_lesson name, blk
+ end
+end
+
+class HH::SideTabs::Lessons < HH::SideTab
+ # auxiliary function used by Kernel#lesson_set
+ # stores the code of the DSL used to write the lessons
+ def self.load_lesson name, blk
+ @@lessons << [name, blk]
+ end
+
+ # draws the lessons tab
+ def content
+ stack :margin => 10 do
+ title "Lessons"
+ @@lessons = []
+ Dir["#{HH::LESSONS}/*.rb"].each { |f| load f }
+ @@lessons.sort{|a, b| a[0] <=> b[0]}.each do |name, blk|
+ stack do
+ britelink "icon-file.png", name do
+ HH::APP.start_lessons name, blk
+ end
+ end
+ end
+ end
+ end
+end
+
diff --git a/app/app/ui/tabs/prefs.rb b/app/app/ui/tabs/prefs.rb
new file mode 100644
index 0000000..d01bcfb
--- /dev/null
+++ b/app/app/ui/tabs/prefs.rb
@@ -0,0 +1,68 @@
+# the messages tab content
+
+class HH::SideTabs::Prefs < HH::SideTab
+ def loading_stack *a
+ n = nil
+ s = stack(*a) do
+ background black(0.4), :curve => 12
+ stroke white(0.6)
+ fill white(0.6)
+ para "\nLoading.\n\n", :align => "center", :size => 17,
+ :font => "Lacuna Regular", :stroke => white
+ o1 = oval -400, 0, 16
+ o2 = oval -400, 0, 16
+ n = animate do |i|
+ v1 = Math.sin(i * 0.2) * 5
+ v2 = Math.cos(i * 0.2) * 5
+ o1.move(((s.width - 50) * 0.5) + v1, 85 + v2)
+ o2.move(((s.width - 50) * 0.5) - v1, 85 - v2)
+ end
+ end
+ s.instance_variable_set("@n", n)
+ def s.replace &blk
+ @n.stop; @n = nil
+ clear &blk
+ end
+ s
+ end
+
+ def clover_whoosh
+ background "#efefa0"
+ background "#efefa0".."#c1d5c0", :height => 150, :bottom => 150
+ end
+
+ def content
+ user = HH::PREFS['username']
+ clover_whoosh
+ stack :margin => [10, 20, 0, 20], :width => 1.0, :height => 1.0 do
+ subtitle "Your Preferences", :font => "Lacuna Regular", :margin => 0, :size => 22,
+ :stroke => "#377"
+ if user
+ para "Hello, #{user}! ",
+ else
+ para "Let's set up Hackety Hack to use on the Internet okay? ",
+ "Be sure you have an account from ",
+ link("hackety-hack.com", :click => "http://hackety-hack.com"), "."
+ end
+
+ @prefpane =
+ stack do
+ stack :margin => 20, :width => 400 do
+ para "Your username", :size => 10, :margin => 2, :stroke => "#352"
+ @user = edit_line user, :width => 1.0
+
+ para "Your password", :size => 10, :margin => 2, :stroke => "#352"
+ @pass = edit_line HH::PREFS['password'], :width => 1.0, :secret => true
+
+ button "Save", :margin_top => 10 do
+ HH::PREFS['username'] = @user.text
+ HH::PREFS['password'] = @pass.text
+ HH.save_prefs
+ alert("Saved, thanks!")
+ end
+ end
+ end
+
+ end
+ end
+end
diff --git a/app/app/ui/tabs/sidetabs.rb b/app/app/ui/tabs/sidetabs.rb
new file mode 100644
index 0000000..355b9e3
--- /dev/null
+++ b/app/app/ui/tabs/sidetabs.rb
@@ -0,0 +1,185 @@
+class HH::SideTabs
+ include HH::Observable
+ ICON_SIZE = 16
+ HOVER_WIDTH = 140
+ def initialize slot, dir
+ @slot, @directory = slot, dir
+ @n_tabs = {:top => 0, :bottom => 1}
+ # tabs whose file has been loaded
+ @loaded_tabs = {}
+ sidetabs = self
+ width = HOVER_WIDTH;
+ append_to @slot do
+ tip = nil
+ right = stack :margin_left => 38, :height => 1.0
+ left = stack :top => 0, :left => 0, :width => 38, :height => 1.0 do
+ tip = stack :top => 0, :left => 0, :width => width, :margin => 4,
+ :hidden => true do
+ background "#F7A", :curve => 6
+ para "HOME", :margin => 3, :margin_left => 40, :stroke => white
+ end
+ # colored background
+ background "#cdc", :width => 38
+ background "#dfa", :width => 36
+ background "#fda", :width => 30
+ background "#daf", :width => 24
+ background "#aaf", :width => 18
+ background "#7aa", :width => 12
+ background "#77a", :width => 6
+ end
+ sidetabs.instance_eval{@left, @right, @tip = left, right, tip}
+ end
+ end
+
+ # +opts+ is an hash
+ # if a block is given no file gets loaded
+ def addtab symbol, opts={}, &blk
+ # default options
+ if not symbol.is_a?(Symbol)
+ raise ArgumentError
+ end
+ tab = opts
+ tab[:symbol] = symbol
+ tab[:icon] ||= "icon-file.png"
+ tab[:position] ||= :top
+ tab[:hover] ||= symbol.to_s
+
+ pos = tab[:position]
+ pixelpos = @n_tabs[pos] * (ICON_SIZE + 10)
+ @n_tabs[pos] += 1
+ hover = tab[:hover]
+ icon_path = HH::STATIC + "/" + tab[:icon]
+ tip = @tip
+ onclick = proc do
+ opentab symbol
+ end
+ width = HOVER_WIDTH+22;
+ append_to @left do
+ stack pos => pixelpos, :left => 0, :width => 38, :margin => 4 do
+ bg = background "#DFA", :height => 26, :curve => 6, :hidden => true
+ image(icon_path, :margin => 4).
+ hover do
+ bg.show
+ tip.parent.width = width
+ tip.top = nil
+ tip.bottom = nil
+ tip.send("#{pos}=", pixelpos)
+ tip.contents[1].text = hover
+ tip.show
+ end.leave do
+ bg.hide
+ tip.hide
+ tip.parent.width = 40
+ end.click &onclick
+ end
+ end
+
+ if blk
+ @loaded_tabs[symbol] = HH::NoContentSideTab.new blk
+ end
+ end
+
+
+ def opentab symbol
+ tab = gettab symbol
+ if tab.has_content?
+ @current_tab.close if @current_tab
+ @current_tab = tab
+ end
+ tab.open
+ emit :tab_opened, symbol
+ end
+
+ def gettab symbol
+ if @loaded_tabs.include? symbol
+ return @loaded_tabs[symbol]
+ else
+ require "app/ui/tabs/#{symbol.downcase}.rb"
+ @loaded_tabs[symbol] = self.class.const_get(symbol).new(@right)
+ end
+ end
+
+private
+ def append_to slot, &blk
+ slot.app do
+ slot.append {self.instance_eval &blk}
+ end
+ end
+end
+
+module HH::HasSideTabs
+ def init_tabs slot, dir="app/ui/tabs"
+ @__side_tab_class = HH::SideTabs.new slot, dir
+ # effectively redirects event to HH::APP
+ @__side_tab_class.on_event :tab_opened, :any do |newtab|
+ emit :tab_opened, newtab
+ end
+ end
+
+ # returns the created tab
+ def addtab *args, &blk
+ @__side_tab_class.addtab *args, &blk
+ end
+
+ def opentab symbol
+ @__side_tab_class.opentab symbol
+ end
+
+ def gettab symbol
+ @__side_tab_class.gettab symbol
+ end
+end
+
+class HH::SideTab
+ def initialize slot
+ @slot = slot
+ slot.append do
+ @content = flow :hidden => true, :left => 0, :top => 0,
+ :width => 1.0, :height => 1.0 do content end
+ end
+ end
+
+ def open
+ on_click
+ if has_content?
+ @content.show
+ end
+ end
+
+ def close
+ if has_content?
+ @content.hide
+ end
+ end
+
+ def clear &blk
+ @content.clear &blk
+ end
+
+ def reset
+ clear {content}
+ end
+
+ def has_content?
+ self.class.method_defined?(:content)
+ end
+
+ def method_missing symbol, *args, &blk
+ #slot = @slot
+ @slot.app.send symbol, *args, &blk
+ end
+
+ def on_click
+ # by default does nothing
+ end
+end
+
+class HH::NoContentSideTab < HH::SideTab
+ def initialize blk
+ @blk = blk
+ end
+ def on_click
+ @blk.call
+ end
+end
+
diff --git a/app/app/ui/widgets.rb b/app/app/ui/widgets.rb
new file mode 100644
index 0000000..624a87e
--- /dev/null
+++ b/app/app/ui/widgets.rb
@@ -0,0 +1,296 @@
+# some extensions to shoes (subclasses of Shoes::Widget)
+# and auxiliary methods for the hh app (the HH::Widgets mixin)
+
+#def scroll_box(opts = {}, &blk)
+# opts = {:width => 1.0, :height => 300, :scroll => true}.merge(opts)
+# stack opts, &blk
+#end
+
+# a glossy button
+class Glossb < Shoes::Widget
+ def initialize(name, opts={}, &blk)
+ fg, bgfill = "#777", "#DDD"
+ case opts[:color]
+ when "dark"; fg, bgfill = "#CCC", "#000"
+ when "yellow"; fg, bgfill = "#FFF", "#7AA"
+ when "red"; fg, bgfill = "#FF5", "#F30"
+ end
+
+ txt = link(name, :underline => 'none', :stroke => fg) {}
+ stack :margin => 4 do
+ background bgfill, :curve => 5
+ @txt = para txt, :align => 'center', :margin => 4, :size => 11
+ hover { @over.show }
+ leave { @over.hide }
+ end
+
+ @over = stack :top => 0, :left => 0, :margin => 2, :hidden => true do
+ background bgfill, :curve => 5
+ @txt_over = para txt, :align => 'center', :margin => 4, :size => 14, :weight => "bold"
+ end
+ @fg = fg
+ click &blk
+ end
+
+ def text= txt
+ new_link = link(txt, :underline => 'none', :stroke => @fg) {}
+ @txt.replace(new_link)
+ @txt_over.replace(new_link)
+ end
+end
+
+class IconButton < Shoes::Widget
+# BSIZE = 16
+# MARGIN = 8
+# SIZE = BSIZE + MARGIN * 2
+ def initialize (type, tooltip, opts={}, &blk)
+ strokewidth 1
+ nofill
+
+ @tooltip_text = tooltip
+
+ stack do
+ stack :margin => 8, :width => 32, :height => 32 do
+ stroke white
+ send type
+ end
+
+ hover do
+ @over.show
+ if @tooltip_text
+ create_tooltip
+ end
+ end
+ leave do
+ @over.hide
+ if @tooltip
+ @tooltip.hide
+ @tooltip.remove
+ @tooltip = nil
+ end
+ end
+ end
+
+ style(:width => 32)
+
+ stack :margin => 8, :top => 0, :left => 0 do
+ @over = stack :width => 16, :height => 16, :hidden => true do
+ background gray(0.8)
+ stroke black
+ send type
+ end
+ end
+
+ click &blk
+ end
+
+ def create_tooltip
+ slot = parent
+ x, y = left, top
+ while not slot.respond_to? :tooltip
+ x += slot.left
+ y += slot.top
+ slot = slot.parent
+ end
+
+ @tooltip = slot.tooltip(@tooltip_text, x, y-20,
+ :fill => red, :stroke => white)
+ end
+
+ def arrow_right
+ line(1, 8, 14, 8)
+ line(14, 8, 10, 1+3)
+ line(14, 8, 10, 15-3)
+ end
+
+ def arrow_left
+ line(1, 8, 14, 8)
+ line(1, 8, 6, 1+3)
+ line(1, 8, 6, 15-3)
+ end
+
+ def x
+ line(2, 2, 13, 13)
+ line(2, 13, 13, 2)
+ end
+
+ def menu
+ rect 2, 2, 11, 11
+ line 4, 6, 11, 6
+ line 4, 8, 11, 8
+ line 4, 10, 11, 10
+ end
+end
+
+module HH::Tooltip
+ def tooltip str, x, y, opts={}
+ f = nil
+ #opts[:wrap] = "trim"
+ slot = self
+ app do
+ slot.append do
+ f = flow :left => x, :top => y do
+ para str, opts
+ end
+ end
+ end
+ f
+ end
+end
+
+#class Lightboard < Shoes::Widget
+# def initialize(width, height, actual = width * height)
+# @opts = []
+# nostroke
+# resize(width, height, actual)
+# end
+# def coords(x, y, opts)
+# at((y * @width) + x, opts)
+# end
+# def at(i, opts)
+# return if i > @actual
+# r, p = self.contents[i * 2], self.contents[(i * 2) + 1]
+# @opts[i] = (@opts[i] || {}).update(opts)
+# if opts[:text]
+# p.text = opts[:text]
+# end
+# if opts[:fill]
+# r.fill = opts[:fill]
+# end
+# end
+# def fits(length)
+# r = Math.sqrt(length)
+# resize(r.round, r.ceil, length)
+# end
+# def resize(width, height, actual = width * height)
+# @width, @height, @actual = width, height, actual
+# build
+# end
+# def build
+# height = parent.height / @height
+# width = parent.width / @width
+# clear do
+# @actual.times do |i|
+# y = i / @width
+# x = i % @width
+# top = y * height
+# left = x * width
+# opts = @opts[i] || {}
+# rect left + 4, top + 4, width - 8, height - 8,
+# :curve => 12, :fill => opts[:fill] || white
+# para opts[:text] || "", :top => top + (height * 0.5) - 32,
+# :left => left, :width => width, :align => "center",
+# :size => 31, :font => "Lacuna Regular"
+# end
+# end
+# end
+#end
+
+# splash screen and a link with a similar appearance as the gloss buttons
+# included by the hh app
+module HH::Widgets
+ def splash
+ nostroke
+ background black
+ color_names = Shoes::COLORS.keys
+
+ @c = :gray
+ @r = star :top => 410, :left => 310, :outer => 80, :inner => 100
+ @s =
+ stack :top => -400, :left => 100, :width => 370, :height => 370 do
+ @mask = mask do
+ star 210, 210, 130, 500, 90
+ end
+ image "#{HH::STATIC}/splash-hand.png", :top => 84, :left => 84
+ end
+ @t = title span(" Welcome to\n", :size => 15), strong("Hackety Hack"),
+ :stroke => black, :top => 180, :left => 80, :font => "Lacuna Regular"
+ @b = glossb "Ready", :top => 280, :left => 80, :width => 80, :hidden => true do
+ @an.stop
+ @ban = @s.parent.animate(30) do |i|
+ @s.parent.move(-(i*40), 0)
+ if i == 30
+ @s.parent.remove
+ end
+ end
+ end
+ # @v = video "music.wav",
+ # :autoplay => true, :width => 0, :height => 0, :top => -100, :left => -100
+ @an = animate(30) do |i|
+ @mask.clear do
+ rotate 1
+ star 210, 210, 130, 500, 90
+ end
+ if i == 60
+ @b.show
+ end
+ if @s.top < 198
+ dist = (208 - @s.top) / 6
+ dist = 10 if dist > 10
+ @s.top += dist
+ if @s.top > -40
+ @t.stroke = gray(@s.top + 55)
+ end
+ else
+ if @v
+ @v.remove
+ @v = nil
+ end
+ o = @r.style[:outer]
+ if o > 500
+ @r.style :inner => 100, :outer => 80, :points => (10..80).rand
+ @c = color_names[(0..color_names.length).rand]
+ elsif o > 200
+ @r.style :fill => send(@c, (500 - o).abs * 0.01), :outer => o + 10
+ else
+ @r.style :fill => send(@c, (0.1..0.6).rand), :outer => o + 1
+ end
+ end
+ end
+ end
+
+ # creates a link having a similar appearance as the gloss buttons
+ def britelink icon, name, time = nil, bg = "#8c9", &blk
+ bg = background bg, :curve => 6, :height => 29, :hidden => true
+ flow :margin => 4, :width => 300 do
+ image HH::STATIC + "/" + icon, :margin_right => 6, :margin => 3
+ p1 = link(name, :stroke => black, :underline => "none", &blk)
+ para p1, :size => 13, :font => "Lacuna Regular", :margin => 0,
+ :wrap => "trim", :width => 280
+ if time
+ p2 = para time.short, :stroke => "#396", :font => "Lacuna Regular",
+ :size => 9, :margin => 4, :margin_bottom => 0
+ end
+ ele = image 1, 1
+ p1.hover do
+ p1.parent.stroke = p1.stroke = white
+ p2.stroke = "#FF5" if time
+ bg.width = ele.left + 10
+ bg.show
+ end
+ p1.leave do
+ p1.parent.stroke = p1.stroke = black
+ p2.stroke = "#396" if time
+ bg.hide
+ end
+ end
+ end
+
+ # method to create a side tab (actually is just a stack with an image in it)
+ # +icon_path+:: the icon displayed in the tab
+ # +top+:: if > 0 indicates the distance from the top, else the distance from
+ # the bottom
+ # +name+:: text displayed on icon hover
+ # +blk+:: the block passed is executed on click
+ def sidetab(icon_path, top, name, &blk)
+ v = top < 0 ? :bottom : :top
+ stack v => top.abs, :left => 0, :width => 38, :margin => 4 do
+ bg = background "#DFA", :height => 26, :curve => 6, :hidden => true
+ image(icon_path, :margin => 4).
+ hover { bg.show; @tip.parent.width = 122; @tip.top = nil; @tip.bottom = nil
+ @tip.send("#{v}=", top.abs); @tip.contents[1].text = name; @tip.show }.
+ leave { bg.hide; @tip.hide; @tip.parent.width = 40 }.
+ click &blk
+ end
+ end
+end
diff --git a/app/fonts/Animals.ttf b/app/fonts/Animals.ttf
new file mode 100755
index 0000000..4f3781e
--- /dev/null
+++ b/app/fonts/Animals.ttf
Binary files differ
diff --git a/app/fonts/Arcade.ttf b/app/fonts/Arcade.ttf
new file mode 100755
index 0000000..2bde6a4
--- /dev/null
+++ b/app/fonts/Arcade.ttf
Binary files differ
diff --git a/app/fonts/Bruegheliana.ttf b/app/fonts/Bruegheliana.ttf
new file mode 100755
index 0000000..a2b673d
--- /dev/null
+++ b/app/fonts/Bruegheliana.ttf
Binary files differ
diff --git a/app/fonts/Carr Space.ttf b/app/fonts/Carr Space.ttf
new file mode 100755
index 0000000..f0c1165
--- /dev/null
+++ b/app/fonts/Carr Space.ttf
Binary files differ
diff --git a/app/fonts/Chess Utrecht.ttf b/app/fonts/Chess Utrecht.ttf
new file mode 100755
index 0000000..3023e2e
--- /dev/null
+++ b/app/fonts/Chess Utrecht.ttf
Binary files differ
diff --git a/app/fonts/DayRoman-X.ttf b/app/fonts/DayRoman-X.ttf
new file mode 100755
index 0000000..d12122c
--- /dev/null
+++ b/app/fonts/DayRoman-X.ttf
Binary files differ
diff --git a/app/fonts/DayRoman.ttf b/app/fonts/DayRoman.ttf
new file mode 100755
index 0000000..e2e813a
--- /dev/null
+++ b/app/fonts/DayRoman.ttf
Binary files differ
diff --git a/app/fonts/Delicious-Bold.otf b/app/fonts/Delicious-Bold.otf
new file mode 100755
index 0000000..e5b1e25
--- /dev/null
+++ b/app/fonts/Delicious-Bold.otf
Binary files differ
diff --git a/app/fonts/Delicious-BoldItalic.otf b/app/fonts/Delicious-BoldItalic.otf
new file mode 100755
index 0000000..81bf13b
--- /dev/null
+++ b/app/fonts/Delicious-BoldItalic.otf
Binary files differ
diff --git a/app/fonts/Delicious-Heavy.otf b/app/fonts/Delicious-Heavy.otf
new file mode 100755
index 0000000..c6faccd
--- /dev/null
+++ b/app/fonts/Delicious-Heavy.otf
Binary files differ
diff --git a/app/fonts/Delicious-Italic.otf b/app/fonts/Delicious-Italic.otf
new file mode 100755
index 0000000..d57df3b
--- /dev/null
+++ b/app/fonts/Delicious-Italic.otf
Binary files differ
diff --git a/app/fonts/Delicious-Roman.otf b/app/fonts/Delicious-Roman.otf
new file mode 100755
index 0000000..31ec11a
--- /dev/null
+++ b/app/fonts/Delicious-Roman.otf
Binary files differ
diff --git a/app/fonts/Delicious-SmallCaps.otf b/app/fonts/Delicious-SmallCaps.otf
new file mode 100755
index 0000000..05d1305
--- /dev/null
+++ b/app/fonts/Delicious-SmallCaps.otf
Binary files differ
diff --git a/app/fonts/Even More Dings JL.ttf b/app/fonts/Even More Dings JL.ttf
new file mode 100755
index 0000000..f13cafd
--- /dev/null
+++ b/app/fonts/Even More Dings JL.ttf
Binary files differ
diff --git a/app/fonts/Fontalicious.ttf b/app/fonts/Fontalicious.ttf
new file mode 100755
index 0000000..7e71830
--- /dev/null
+++ b/app/fonts/Fontalicious.ttf
Binary files differ
diff --git a/app/fonts/Fontin-Bold.otf b/app/fonts/Fontin-Bold.otf
new file mode 100755
index 0000000..678a7aa
--- /dev/null
+++ b/app/fonts/Fontin-Bold.otf
Binary files differ
diff --git a/app/fonts/Fontin-Italic.otf b/app/fonts/Fontin-Italic.otf
new file mode 100755
index 0000000..0d5d92e
--- /dev/null
+++ b/app/fonts/Fontin-Italic.otf
Binary files differ
diff --git a/app/fonts/Fontin-Regular.otf b/app/fonts/Fontin-Regular.otf
new file mode 100755
index 0000000..37b668e
--- /dev/null
+++ b/app/fonts/Fontin-Regular.otf
Binary files differ
diff --git a/app/fonts/Fontin-SmallCaps.otf b/app/fonts/Fontin-SmallCaps.otf
new file mode 100755
index 0000000..705654c
--- /dev/null
+++ b/app/fonts/Fontin-SmallCaps.otf
Binary files differ
diff --git a/app/fonts/Free.ttf b/app/fonts/Free.ttf
new file mode 100755
index 0000000..9aa527c
--- /dev/null
+++ b/app/fonts/Free.ttf
Binary files differ
diff --git a/app/fonts/Illustries.ttf b/app/fonts/Illustries.ttf
new file mode 100755
index 0000000..6c7465d
--- /dev/null
+++ b/app/fonts/Illustries.ttf
Binary files differ
diff --git a/app/fonts/JustOldFashion.ttf b/app/fonts/JustOldFashion.ttf
new file mode 100755
index 0000000..2c08ce5
--- /dev/null
+++ b/app/fonts/JustOldFashion.ttf
Binary files differ
diff --git a/app/fonts/Lacuna.ttf b/app/fonts/Lacuna.ttf
new file mode 100644
index 0000000..d4e5003
--- /dev/null
+++ b/app/fonts/Lacuna.ttf
Binary files differ
diff --git a/app/fonts/LiberationMono-Bold.ttf b/app/fonts/LiberationMono-Bold.ttf
new file mode 100755
index 0000000..95de753
--- /dev/null
+++ b/app/fonts/LiberationMono-Bold.ttf
Binary files differ
diff --git a/app/fonts/LiberationMono-Regular.ttf b/app/fonts/LiberationMono-Regular.ttf
new file mode 100644
index 0000000..e3024a0
--- /dev/null
+++ b/app/fonts/LiberationMono-Regular.ttf
Binary files differ
diff --git a/app/fonts/LiberationSans-Bold.ttf b/app/fonts/LiberationSans-Bold.ttf
new file mode 100755
index 0000000..53200d9
--- /dev/null
+++ b/app/fonts/LiberationSans-Bold.ttf
Binary files differ
diff --git a/app/fonts/LiberationSans-BoldItalic.ttf b/app/fonts/LiberationSans-BoldItalic.ttf
new file mode 100755
index 0000000..d06deca
--- /dev/null
+++ b/app/fonts/LiberationSans-BoldItalic.ttf
Binary files differ
diff --git a/app/fonts/LiberationSans-Italic.ttf b/app/fonts/LiberationSans-Italic.ttf
new file mode 100755
index 0000000..07275ad
--- /dev/null
+++ b/app/fonts/LiberationSans-Italic.ttf
Binary files differ
diff --git a/app/fonts/LiberationSans-Regular.ttf b/app/fonts/LiberationSans-Regular.ttf
new file mode 100755
index 0000000..09fac2f
--- /dev/null
+++ b/app/fonts/LiberationSans-Regular.ttf
Binary files differ
diff --git a/app/fonts/LiberationSerif-Bold.ttf b/app/fonts/LiberationSerif-Bold.ttf
new file mode 100755
index 0000000..3a4ab92
--- /dev/null
+++ b/app/fonts/LiberationSerif-Bold.ttf
Binary files differ
diff --git a/app/fonts/LiberationSerif-BoldItalic.ttf b/app/fonts/LiberationSerif-BoldItalic.ttf
new file mode 100755
index 0000000..dc75de8
--- /dev/null
+++ b/app/fonts/LiberationSerif-BoldItalic.ttf
Binary files differ
diff --git a/app/fonts/LiberationSerif-Italic.ttf b/app/fonts/LiberationSerif-Italic.ttf
new file mode 100755
index 0000000..d92b5e3
--- /dev/null
+++ b/app/fonts/LiberationSerif-Italic.ttf
Binary files differ
diff --git a/app/fonts/LiberationSerif-Regular.ttf b/app/fonts/LiberationSerif-Regular.ttf
new file mode 100755
index 0000000..d100691
--- /dev/null
+++ b/app/fonts/LiberationSerif-Regular.ttf
Binary files differ
diff --git a/app/fonts/Outer Space JL.ttf b/app/fonts/Outer Space JL.ttf
new file mode 100755
index 0000000..b3e48f6
--- /dev/null
+++ b/app/fonts/Outer Space JL.ttf
Binary files differ
diff --git a/app/fonts/Oxygene1.ttf b/app/fonts/Oxygene1.ttf
new file mode 100755
index 0000000..c98e9db
--- /dev/null
+++ b/app/fonts/Oxygene1.ttf
Binary files differ
diff --git a/app/fonts/Phonetica.ttf b/app/fonts/Phonetica.ttf
new file mode 100755
index 0000000..357826f
--- /dev/null
+++ b/app/fonts/Phonetica.ttf
Binary files differ
diff --git a/app/fonts/Pixelpoiiz.ttf b/app/fonts/Pixelpoiiz.ttf
new file mode 100644
index 0000000..b0c719d
--- /dev/null
+++ b/app/fonts/Pixelpoiiz.ttf
Binary files differ
diff --git a/app/fonts/Playing Cards.ttf b/app/fonts/Playing Cards.ttf
new file mode 100755
index 0000000..dcf631b
--- /dev/null
+++ b/app/fonts/Playing Cards.ttf
Binary files differ
diff --git a/app/fonts/Silhouette.ttf b/app/fonts/Silhouette.ttf
new file mode 100755
index 0000000..03cfa1d
--- /dev/null
+++ b/app/fonts/Silhouette.ttf
Binary files differ
diff --git a/app/fonts/TakaoGothic.otf b/app/fonts/TakaoGothic.otf
new file mode 100644
index 0000000..c2e6074
--- /dev/null
+++ b/app/fonts/TakaoGothic.otf
Binary files differ
diff --git a/app/fonts/YanoneKaffeesatz-Bold.otf b/app/fonts/YanoneKaffeesatz-Bold.otf
new file mode 100755
index 0000000..4ea5919
--- /dev/null
+++ b/app/fonts/YanoneKaffeesatz-Bold.otf
Binary files differ
diff --git a/app/fonts/YanoneKaffeesatz-Light.otf b/app/fonts/YanoneKaffeesatz-Light.otf
new file mode 100755
index 0000000..8a3e2bf
--- /dev/null
+++ b/app/fonts/YanoneKaffeesatz-Light.otf
Binary files differ
diff --git a/app/fonts/YanoneKaffeesatz-Regular.otf b/app/fonts/YanoneKaffeesatz-Regular.otf
new file mode 100755
index 0000000..bee3b05
--- /dev/null
+++ b/app/fonts/YanoneKaffeesatz-Regular.otf
Binary files differ
diff --git a/app/fonts/YanoneKaffeesatz-Thin.otf b/app/fonts/YanoneKaffeesatz-Thin.otf
new file mode 100755
index 0000000..764d902
--- /dev/null
+++ b/app/fonts/YanoneKaffeesatz-Thin.otf
Binary files differ
diff --git a/app/h-ety-h.rb b/app/h-ety-h.rb
new file mode 100644
index 0000000..210dfc2
--- /dev/null
+++ b/app/h-ety-h.rb
@@ -0,0 +1,5 @@
+#!/usr/bin/env shoes
+
+# the main application executable
+
+load 'app/ui/mainwindow.rb'
diff --git a/app/installer/HackFolder.ini b/app/installer/HackFolder.ini
new file mode 100755
index 0000000..2659b86
--- /dev/null
+++ b/app/installer/HackFolder.ini
@@ -0,0 +1,49 @@
+[Settings]
+NumFields=5
+
+[Field 1]
+Type=label
+Text=Where would you like your programs to be saved?
+Left=0
+Right=-1
+Top=0
+Bottom=10
+
+[Field 2]
+Type=RadioButton
+Text=A "My Hacks" folder in My Documents
+Left=8
+Right=-1
+Top=30
+Bottom=40
+State=1
+Flags=NOTIFY
+
+[Field 3]
+Type=RadioButton
+Text=A "Hackety Hack" folder on the desktop
+Left=8
+Right=-1
+Top=50
+Bottom=60
+State=0
+Flags=NOTIFY
+
+[Field 4]
+Type=RadioButton
+Text=Let me pick my own folder
+Left=8
+Right=-1
+Top=70
+Bottom=80
+State=0
+Flags=NOTIFY
+
+[Field 5]
+Type=DirRequest
+Text=Let me pick my own folder
+Left=18
+Right=-1
+Top=85
+Bottom=97
+Flags=DISABLED
diff --git a/app/installer/base.nsi b/app/installer/base.nsi
new file mode 100755
index 0000000..f4db8a9
--- /dev/null
+++ b/app/installer/base.nsi
@@ -0,0 +1,252 @@
+;--------------------------------
+;Definitions
+!define AppName "Hackety Hack"
+!define AppVersion "0.Y"
+!define AppMainEXE "hacketyhack.exe"
+!define ShortName "HacketyHack"
+!define InstallKey "Software\Hackety.org\${AppName}"
+
+;--------------------------------
+;Include Modern UI
+
+ !include "MUI.nsh"
+ !include LogicLib.nsh
+
+;--------------------------------
+;General
+
+ ;Name and file
+ Name "${AppName}"
+ OutFile "${ShortName}-${AppVersion}.exe"
+
+ ;Default installation folder
+ InstallDir "$PROGRAMFILES\${AppName}"
+
+ ;Get installation folder from registry if available
+ InstallDirRegKey HKCU "${InstallKey}" ""
+
+ ;Vista redirects $SMPROGRAMS to all users without this
+ RequestExecutionLevel admin
+
+;--------------------------------
+;Variables
+
+ Var MUI_TEMP
+ Var STARTMENU_FOLDER
+
+;--------------------------------
+;Interface Configuration
+
+ !define MUI_ABORTWARNING
+ !define MUI_ICON setup.ico
+ !define MUI_UNICON setup.ico
+ !define MUI_WELCOMEPAGE_TITLE_3LINES
+ !define MUI_WELCOMEFINISHPAGE_BITMAP installer-1.bmp
+ !define MUI_HEADERIMAGE
+ !define MUI_HEADERIMAGE_RIGHT
+ !define MUI_HEADERIMAGE_BITMAP installer-2.bmp
+ !define MUI_COMPONENTSPAGE_NODESC
+
+;--------------------------------
+;Pages
+
+ !insertmacro MUI_PAGE_WELCOME
+ !insertmacro MUI_PAGE_DIRECTORY
+ Page custom HackFolderPage HackFolderHook
+
+ ;Start Menu Folder Page Configuration
+ !define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKCU"
+ !define MUI_STARTMENUPAGE_REGISTRY_KEY "${InstallKey}"
+ !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
+
+ !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
+ !insertmacro MUI_PAGE_INSTFILES
+
+ ; Finish Page
+ !define MUI_FINISHPAGE_NOREBOOTSUPPORT
+ !define MUI_FINISHPAGE_TITLE_3LINES
+ !define MUI_FINISHPAGE_RUN
+ !define MUI_FINISHPAGE_RUN_FUNCTION LaunchApp
+ !define MUI_FINISHPAGE_RUN_TEXT $(LAUNCH_TEXT)
+ !define MUI_PAGE_CUSTOMFUNCTION_PRE preFinish
+ !insertmacro MUI_PAGE_FINISH
+
+ !insertmacro MUI_UNPAGE_CONFIRM
+ !insertmacro MUI_UNPAGE_INSTFILES
+
+;--------------------------------
+;Languages
+
+ !insertmacro MUI_LANGUAGE "English"
+ LangString LAUNCH_TEXT ${LANG_ENGLISH} "Run Hackety Hack"
+ LangString TEXT_IO_TITLE ${LANG_ENGLISH} "Your Hackety Hack Settings"
+ LangString TEXT_IO_SUBTITLE ${LANG_ENGLISH} "Customize the way you use Hackety Hack"
+
+;--------------------------------
+;Reserve Files
+
+ ;If you are using solid compression, files that are required before
+ ;the actual installation should be stored first in the data block,
+ ;because this will make your installer start faster.
+
+ ReserveFile "HackFolder.ini"
+ !insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
+
+;--------------------------------
+;Installer Section
+!macro MakeFileAssoc LangName LangExt
+ WriteRegStr HKCR ".hack-${LangExt}" "" "${ShortName}.${LangName}Program"
+ WriteRegStr HKCR "${ShortName}.${LangName}Program" "" "${AppName} ${LangName} Program"
+ WriteRegStr HKCR "${ShortName}.${LangName}Program\shell\open\command" "" '"$INSTDIR\${AppMainEXE}" "%1"'
+!macroend
+
+!macro UnmakeFileAssoc LangName LangExt
+ DeleteRegKey /ifempty HKCU ".hack-${LangExt}"
+ DeleteRegKey /ifempty HKCU "${ShortName}.${LangName}Program"
+!macroend
+
+Section "App Section" SecApp
+
+ SetOutPath "$INSTDIR"
+
+ File /r /x components\compreg.dat /x components\xpti.dat /x installer ..\*.*
+
+ ;Store installation folder
+ WriteRegStr HKCU "${InstallKey}" "" $INSTDIR
+
+ ;Store hacks folder
+ !insertmacro MUI_INSTALLOPTIONS_READ $0 "HackFolder.ini" "Field 3" "State"
+ ${If} $0 == "1"
+ WriteRegStr HKCU "${InstallKey}" "HackFolder" "%DESKTOP%/Hackety Hack"
+ ${Else}
+ !insertmacro MUI_INSTALLOPTIONS_READ $0 "HackFolder.ini" "Field 4" "State"
+ ${If} $0 == "1"
+ !insertmacro MUI_INSTALLOPTIONS_READ $0 "HackFolder.ini" "Field 5" "State"
+ WriteRegStr HKCU "${InstallKey}" "HackFolder" $0
+ ${Else}
+ WriteRegStr HKCU "${InstallKey}" "HackFolder" "%MYDOCUMENTS%/My Hacks"
+ ${EndIf}
+ ${EndIf}
+
+ ;Make associations
+ !insertmacro MakeFileAssoc "Ruby" "rb"
+
+ ;Create uninstaller
+ WriteUninstaller "$INSTDIR\Uninstall.exe"
+
+ !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
+
+ ;Create shortcuts
+ CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
+ CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Hackety Hack.lnk" "$INSTDIR\${AppMainEXE}"
+ CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Get Help.lnk" "$INSTDIR\${AppMainEXE}" "--manual"
+ CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
+
+ !insertmacro MUI_STARTMENU_WRITE_END
+
+SectionEnd
+
+
+Function .onInit
+
+ ;Extract InstallOptions INI files
+ !insertmacro MUI_INSTALLOPTIONS_EXTRACT "HackFolder.ini"
+
+FunctionEnd
+
+Function HackFolderPage
+
+ !insertmacro MUI_HEADER_TEXT "$(TEXT_IO_TITLE)" "$(TEXT_IO_SUBTITLE)"
+ !insertmacro MUI_INSTALLOPTIONS_DISPLAY "HackFolder.ini"
+
+FunctionEnd
+
+Function HackFolderHook
+
+ !insertmacro MUI_INSTALLOPTIONS_READ $0 "HackFolder.ini" "Settings" "State"
+ StrCmp $0 0 validate
+ StrCmp $0 2 nofolder
+ StrCmp $0 3 nofolder
+ StrCmp $0 4 otherfolder ; Select your own folder
+ Abort
+
+nofolder:
+ !insertmacro MUI_INSTALLOPTIONS_READ $1 "HackFolder.ini" "Field 5" "HWND"
+ EnableWindow $1 0
+ !insertmacro MUI_INSTALLOPTIONS_READ $1 "HackFolder.ini" "Field 5" "HWND2"
+ EnableWindow $1 0
+ !insertmacro MUI_INSTALLOPTIONS_WRITE "HackFolder.ini" "Field 5" "Flags" "DISABLED"
+ Abort
+
+otherfolder:
+ !insertmacro MUI_INSTALLOPTIONS_READ $1 "HackFolder.ini" "Field 5" "HWND"
+ EnableWindow $1 1
+ !insertmacro MUI_INSTALLOPTIONS_READ $1 "HackFolder.ini" "Field 5" "HWND2"
+ EnableWindow $1 1
+ !insertmacro MUI_INSTALLOPTIONS_WRITE "HackFolder.ini" "Field 5" "Flags" ""
+ Abort
+
+validate:
+ !insertmacro MUI_INSTALLOPTIONS_READ $0 "HackFolder.ini" "Field 4" "State"
+ ${If} $0 == "1"
+ !insertmacro MUI_INSTALLOPTIONS_READ $0 "HackFolder.ini" "Field 5" "State"
+ ${If} $0 == ""
+ MessageBox MB_ICONEXCLAMATION|MB_OK "You must select a folder for your programs."
+ Abort
+ ${EndIf}
+ ${EndIf}
+FunctionEnd
+
+; When we add an optional action to the finish page the cancel button is
+; enabled. This disables it and leaves the finish button as the only choice.
+Function preFinish
+ !insertmacro MUI_INSTALLOPTIONS_WRITE "ioSpecial.ini" "settings" "cancelenabled" "0"
+FunctionEnd
+
+Function LaunchApp
+ ; ${CloseApp} "true" $(WARN_APP_RUNNING_INSTALL)
+ Exec "$INSTDIR\${AppMainEXE}"
+FunctionEnd
+
+;--------------------------------
+;Descriptions
+
+ ;Language strings
+ LangString DESC_SecApp ${LANG_ENGLISH} "A test section."
+
+ ;Assign language strings to sections
+ !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
+ !insertmacro MUI_DESCRIPTION_TEXT ${SecApp} $(DESC_SecApp)
+ !insertmacro MUI_FUNCTION_DESCRIPTION_END
+
+;--------------------------------
+;Uninstaller Section
+
+Section "Uninstall"
+
+ RMDir /r "$INSTDIR"
+
+ !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
+
+ Delete "$SMPROGRAMS\$MUI_TEMP\Hackety Hack.lnk"
+ Delete "$SMPROGRAMS\$MUI_TEMP\Uninstall.lnk"
+
+ ;Delete empty start menu parent diretories
+ StrCpy $MUI_TEMP "$SMPROGRAMS\$MUI_TEMP"
+
+ startMenuDeleteLoop:
+ ClearErrors
+ RMDir $MUI_TEMP
+ GetFullPathName $MUI_TEMP "$MUI_TEMP\.."
+
+ IfErrors startMenuDeleteLoopDone
+
+ StrCmp $MUI_TEMP $SMPROGRAMS startMenuDeleteLoopDone startMenuDeleteLoop
+ startMenuDeleteLoopDone:
+
+ ;Unmake associations
+ !insertmacro UnmakeFileAssoc "Ruby" "rb"
+
+ DeleteRegKey /ifempty HKCU "${InstallKey}"
+
+SectionEnd
diff --git a/app/installer/installer-1.bmp b/app/installer/installer-1.bmp
new file mode 100755
index 0000000..ec13107
--- /dev/null
+++ b/app/installer/installer-1.bmp
Binary files differ
diff --git a/app/installer/installer-2.bmp b/app/installer/installer-2.bmp
new file mode 100755
index 0000000..9fcfed0
--- /dev/null
+++ b/app/installer/installer-2.bmp
Binary files differ
diff --git a/app/installer/setup.ico b/app/installer/setup.ico
new file mode 100755
index 0000000..1ec9242
--- /dev/null
+++ b/app/installer/setup.ico
Binary files differ
diff --git a/app/lessons/basic_programming.rb b/app/lessons/basic_programming.rb
new file mode 100644
index 0000000..abd0ca6
--- /dev/null
+++ b/app/lessons/basic_programming.rb
@@ -0,0 +1,302 @@
+lesson_set "2: Basic Programming" do
+
+ lesson "Hello there!"
+ page "Round One" do
+ para "So, you'd like to learn how to hack code with the best of 'em, eh? Well, ",
+ "you've come to the right place. This is the very first lesson I have to ",
+ "share with you. It all starts here."
+ para "I want to get you started off on the best possible foot with making ",
+ "programs, so we'll start off by talking a little bit about what ",
+ "programming is, and then we'll write some basic programs to draw fun ",
+ "things on the screen. Sound good? Off we go!"
+ flow do
+ para "(click the little "
+ icon_button :arrow_right, nil do
+ alert "Not this one! The one below!"
+ end
+ para " on the bottom of the screen to get started)"
+ end
+ end
+
+ page "Lesson Controls" do
+ para "Before we move on, Here's a refresher on the controls you can use ",
+ "to move around in the Lesson."
+ flow do
+ icon_button :arrow_left, nil
+ para strong("back"), ": goes back one page"
+ end
+ flow do
+ icon_button :arrow_right, nil
+ para strong("continue"), ": goes to the next page"
+ end
+ flow do
+ icon_button :menu, nil
+ para strong("menu"), ": makes it easy to jump around to any lesson"
+ end
+ flow do
+ icon_button :x, nil
+ para strong("close"), ": closes the tutor"
+ end
+ para "Don't forget! Press "
+ icon_button :arrow_right, nil
+ para "to move to the next part. Have at it!"
+ end
+
+ lesson "Let's talk about programming"
+ page "It's all about instructions" do
+ para "When you get down to it, programming is all about ", strong("algorithms"),
+ ". That's a big fancy word for 'a list of instructions.' Every program ",
+ "is simply a big to-do list of instructions for the computer to follow."
+ para "You can turn almost anything into a list of instructions if you really ",
+ "think about it. Most of the time, you've got so much practice at doing ",
+ "things that you don't even think about these individual steps. You just ",
+ "do them. It's very natural."
+ end
+
+ page "The computer is simple" do
+ para "Unfortunately, computers are actually quite simple. This may be contrary ",
+ "to everything you've ever heard, but it's the truth. Even though we ",
+ "compare computers to things like our brains, it's a really poor analogy. ",
+ "What computers are actually really good at is performing simple, boring ",
+ "things over and over again very accurately. They can't think for ",
+ "themselves!"
+ para "This is why computers appear to be complex. They blindly follow whatever ",
+ "orders they're given, without any thought about how those instructions ",
+ "are good or bad. It's hard to think in such simple terms!"
+ end
+
+ page "Explain yourself well" do
+ para "It's important to remember that you have to fully explain yourself to the ",
+ "computer when you're programming. It can't figure out what you're trying ",
+ "to say, so you have to say what you mean!"
+ para "This takes some practice, so we're going to start off with some exercises ",
+ "in explaining ourselves in very basic terms. It's almost like trying to ",
+ "explain math to a young child: you have to go slowly, triple check your ",
+ "work, and have some patience when it just doesn't quite get it."
+ end
+
+ lesson "Lists of Instructions"
+ page "A to-do list, not a shoping list" do
+ para "When I say that computers follow lists of instructions, I mean a to-do ",
+ "list, not a shopping list. What I'm trying to say is that these lists have ",
+ "an ", strong("order"), " to them that the computer follows. It does each ",
+ "step in turn as quickly as it possibly can."
+ para "A shopping list is a different kind of list entirely. You can go to ",
+ "whichever aisle you choose, and as long as you get everything before you ",
+ "leave, you're A-OK. This isn't what the computer does at all."
+ end
+
+ page "How would you tell a person to do it?" do
+ para "Let's try an example: if you had to tell someone in words how to draw a ",
+ "square on a piece of paper, how would you do it?"
+ para "You're not allowed to say \"like this\" or \"this way,\" that's cheating! ",
+ "You have to spell out every detail."
+ end
+
+ page "Once again: computers are simple" do
+ para "How'd you do? I can't see what you said, but here's an example of how ",
+ "simple computers are compared to people. Did you forget to mention how long ",
+ "each side of the square is? If you didn't good job!"
+ para "Here's how I'd do it, by the way. This isn't the only right answer, it's ",
+ "just an example:"
+ para "1. Put your pen down on the paper."
+ para "2. Draw right one inch."
+ para "3. Draw down one inch."
+ para "4. Draw left one inch."
+ para "5. Draw up one inch."
+ para "6. Take your pen off of the paper."
+ para "7. You're done!"
+ end
+
+ lesson "Turtles, all the way down."
+ page "Drawing... with turtles?" do
+ para "Okay! Enough of these thinking experiments. Let's actually make something. ",
+ "I bet you've been wondering when that was going to start, right? It's ",
+ "really important to get the basics down first."
+ para "We're going to tell the computer how to draw shapes... with turtles. Yes, ",
+ "you heard me right. You're going to have to give these instructions to a ",
+ "turtle."
+ para "This particular turtle is carrying a pen. You have a piece of paper. The ",
+ "turtle will follow your every word. But the turtle is a bit slow, and ",
+ "needs careful instruction. Are you up to it?"
+ end
+
+ page "The Turtle and its commands" do
+ para "We have to tell Hackety Hack that we want to tell the Turtle what to do. ",
+ "To do that, we have a ", code("Turtle"), " command. We can tell the ",
+ code("Turtle"), " two things: "
+ para code("draw"), ": the turtle will follow our instructions at lightning speed, "
+ "drawing our entire image in the blink of an eye."
+ para code("start"), ": an interactive window will appear, which lets you see ",
+ "the turtle move at each step in the program. You can move at your own ",
+ "pace. This is useful if the turtle isn't doing what you expect!"
+ para ""
+ flow do
+ para "Click on the editor tab ("
+ image "#{HH::STATIC}/tab-new.png", :margin => 6 do
+ alert("Not this one, silly! the one on the left!")
+ end
+ para ") to get started."
+ end
+
+ next_when :tab_opened, :Editor
+ end
+
+ page "Type it in!" do
+ para "Cool. Now type this: "
+ embed_code "Turtle.draw"
+ para "The period in between the ", code("Turtle"), " and the ", code("draw"),
+ " connects them together. Programming languages have rules, just like ",
+ "English has rules! You can think of ", code("Turtle"), " like a subject, ",
+ "and ", code("draw"), " as a verb. Together, they make a sentence: hey ",
+ "turtle, draw me something!"
+ para "Once you've typed that in, go ahead and click the 'Run' button. You'll see ",
+ "the turtle flash on the screen for just a brief moment, then dissapear."
+ end
+
+ page "Do... what I tell you to" do
+ para "Awesome! We've got the turtle to appear, at least. Now we need to tell ",
+ "it what we want to draw!"
+ para "Remember when we said that all programs are lists of instructions? In this ",
+ "case, our program only has one instruction: ", code("Turtle"), ", draw ",
+ "something! But we need to be able to give the ", code("Turtle"), " its ",
+ "own list of instructions."
+ para "To do this, we'll use two words: ", code("do"), " and ", code("end"), ". ",
+ "These two words together make up a ", em("sublist"), " of things, just for ",
+ "the ", code("Turtle"), "!"
+ end
+
+ page "Changing the background" do
+ para "Let's try this: we can tell the ", code("Turtle"), " that we want to use ",
+ "a different background color by using the ", code('background'), " command. ",
+ "Check it out:"
+ embed_code "Turtle.draw do
+ background maroon
+end"
+ para "Type this in and click 'Run'!"
+ end
+
+ page "The Turtle gets its orders" do
+ para "Cool stuff! The background is now maroon. You can find a full list of ",
+ "colors that are supported on the ", link("Shoes website", :click => 'http://shoesrb.com/manual/Colors.html'), "."
+ para "This is also how you make lists of instructions for the ", code("Turtle"),
+ " to follow. To make it a little easier to see, programmers will often ",
+ "put two spaces before sublists. Get in the habit now, you'll thank me later!"
+ end
+
+ page "The pen" do
+ para "Now that we've got a snazzy background color, how do we draw some lines? ",
+ "Well, the first thing you need to know about is the pen. The ",
+ code("Turtle"), " carries a pen along, and drags it along the ground behind ",
+ "itself. You can change the color of line the pen draws with the ",
+ code("pencolor"), " command."
+ end
+
+ lesson "Drawing lines"
+ page "Sally forth!" do
+ para "Okay, enough dilly-dallying. Let's tell the turtle to draw a line! Here's ",
+ "my line. Give this one a shot, then try your own colors and numbers!"
+ embed_code "Turtle.draw do
+ background lightslategray
+ pencolor honeydew
+ forward 50
+end"
+ para "50 is the number of pixels to move foward, by the way."
+ end
+
+ page "You spin me right round, baby" do
+ para "Great! So you've got a line. But what if you don't want the ",
+ code("Turtle"), " to move forward? Well, you can tell it to turn by using a ",
+ code("turnleft"), " or ", code("turnright"), " command, like this:"
+ embed_code "Turtle.draw do
+ background lightslategray
+ pencolor honeydew
+ forward 50
+ turnright 90
+ forward 50
+end"
+ para "Give that a shot, then play with it a bit!"
+ para "If you're wondering what 90 means, it's the number of degrees that it'll turn."
+ end
+
+ page "I like to move it, move it" do
+ para "Okay, now we're cooking! Let's break this down again:"
+ para code("Turtle.draw"), " tells the ", code("Turtle"), " we want it to draw ",
+ "some things. The period connects the two."
+ para code("do ... end"), " is a sublist of things. This is what we want the ",
+ code("Turtle"), " to be drawing. Not for the rest of our program."
+ para code("pencolor"), " sets the color of the pen the ", code("Turtle"), " is ",
+ "dragging along behind him, and ", code("background"), " sets the color of ",
+ "the background."
+ para code("turnright"), " (or its buddy ", code("turnleft"), ") tells the ",
+ code("Turtle"), " to turn to the right or left."
+ para code("forward"), " (or its friend ", code("backward"), ") tells the ",
+ code("Turtle"), " to move."
+ end
+
+ page "Let's try drawing that square" do
+ para "Go ahead. Give it a shot. Try to get the ", code("Turtle"), " to draw a ",
+ "square."
+ para "I'll wait. :)"
+ end
+
+ page "Here's my version" do
+ para "Here's how I did it:"
+ embed_code "Turtle.draw do
+ background lightslategray
+ pencolor honeydew
+ forward 50
+ turnright 90
+ forward 50
+ turnright 90
+ forward 50
+ turnright 90
+ forward 50
+end"
+ end
+
+ lesson "Repeating ourselves"
+ page "Pete and Repeat..." do
+ para "Man, that was a ton of reptition! My fingers almost fell off typing ",
+ code("forward"), " and ", code("turnright"), " there!"
+ para "I have good news, though: I mentioned something earlier about computers. ",
+ "It turns out that doing boring, repetitive things is something they're ",
+ "really good at! They'll do the same thing over and over again, forever even ",
+ "as long as you ask nicely."
+ end
+
+ page "Repeating repeating ourselves ourselves" do
+ para "Check it out: our ", code("Turtle"), " actually knows numbers. For ",
+ "exaple:"
+ embed_code "Turtle.draw do
+ background lightslategray
+ pencolor honeydew
+ 4.times do
+ forward 50
+ turnright 90
+ end
+end"
+ para "Try running this example. It also draws a square! Wow!"
+ end
+
+ page "4.times" do
+ para "It's pretty easy: ", code("4"), " can take instructions too, just like ",
+ "our ", code("Turtle"), ". This command repeats a list of instructions ",
+ "that many times. Fun! Four times. And the ", code("do"), " and ",
+ code("end"), " show which list of instructions go with the ", code("4"),
+ " rather than with the ", code("Turtle"), "."
+ end
+
+ page "Try it out!" do
+ para "Have a blast: make some fun shapes of your own!"
+ end
+
+ lesson "Summary"
+ page "Congradulations!" do
+ para "Wow, you're awesome. Pat yourself on the back. High five someone. You've ",
+ "got these basics down!"
+ para "Check out the ", em("Basic Ruby"), " lesson to pick up some totally ",
+ "different and exciting things!"
+ end
+
+end
diff --git a/app/lessons/basic_ruby.rb b/app/lessons/basic_ruby.rb
new file mode 100644
index 0000000..894e4bf
--- /dev/null
+++ b/app/lessons/basic_ruby.rb
@@ -0,0 +1,281 @@
+# encoding: UTF-8
+
+lesson_set "3: Basic Ruby" do
+
+ lesson "Hello there!"
+ page "Let's get started" do
+ para "Welcome to your first lesson in Ruby! You're going to have a blast."
+ para "Ruby is a great programming language that you can use to make all kinds of ",
+ "things with. Let's get going!"
+ flow do
+ para "(click the little "
+ icon_button :arrow_right, nil do
+ alert "Not this one! The one below!"
+ end
+ para " on the bottom of the screen to get started)"
+ end
+ end
+
+ page "Lesson Controls" do
+ para "Before we move on, Here's a refresher on the controls you can use ",
+ "to move around in the Lesson."
+ flow do
+ icon_button :arrow_left, nil
+ para strong("back"), ": goes back one page"
+ end
+ flow do
+ icon_button :arrow_right, nil
+ para strong("continue"), ": goes to the next page"
+ end
+ flow do
+ icon_button :menu, nil
+ para strong("menu"), ": makes it easy to jump around to any lesson"
+ end
+ flow do
+ icon_button :x, nil
+ para strong("close"), ": closes the tutor"
+ end
+ para "Don't forget! Press "
+ icon_button :arrow_right, nil
+ para "to move to the next part. Have at it!"
+ end
+
+ lesson "A bit more about Ruby"
+ page "Konnichiwa, Ruby!" do # can't do 日本語 without a bunch of work...
+ flow do # due to multiple fonts...
+ para em("Ruby"), " was created by "
+ para "ã¾ã¤ã‚‚㨠ゆãã²ã‚", :font => "TakaoGothic"
+ para " (you can just call him Matz) in 1995. If you couldn't guess, Matz is ",
+ "from Japan. Here he is:"
+ end
+ image "#{HH::STATIC}/matz.jpg"
+ end
+
+ page "Ruby is enjoyable" do
+ para "Matz has this to say about Ruby:\n"
+ para em("I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language.\n")
+ para "One more thing about Ruby: Rubyists (that's what people who like Ruby call ",
+ "themselves) have a saying: ", strong("MINSWAN"), ". This stands for ",
+ strong("M"), "atz ", strong("I"), "s ", strong("N"), "ice ",
+ strong("S"), "o ", strong("W"), "e ", strong("A"), "re ", strong("N"), "ice. ",
+ "Which is a pretty nice saying, itself. Be nice to everyone, and give them ",
+ "a hand when they need it!"
+
+ end
+
+ lesson "Displaying Things"
+ page "Let's do this!" do
+ para "Okay! The very first thing that you need to know is how to show something ",
+ "on the screen. Otherwise, you won't know what's going on!"
+ flow do
+ para "In order to start coding, we need to bring up the Editor. Click the ("
+ image "#{HH::STATIC}/tab-new.png", :margin => 6 do
+ alert("Not this one, silly! the one on the left!")
+ end
+ para ") to open it up."
+ end
+ next_when :tab_opened, :Editor
+ end
+
+ page "Hello, World!" do
+ para "There are two ways of doing this. Here's the first: alert"
+ embed_code 'alert "Hello, world!"'
+ para "Type this in and press the 'Run' button."
+ end
+
+ page "alert" do
+ para "Okay, let's break this down: There's two main parts to this little program: ",
+ "you have an ", code("alert"), ", and a ", code('"Hello, world!"'), ". These ",
+ "two parts work just like an English sentence: The ", code("alert"), " is a ",
+ 'verb and the stuff in the ""s is an object. In Ruby, we call verbs ',
+ strong("methods"), ". The ", code("alert"), " verb says 'Put an alert box ",
+ "on the screen, and the content of the box is whatever thing you give me.'"
+ para "We'll talk about the ", code('"Hello, world!"'), " in just a second. Here's ",
+ "the other way of making this happen: "
+ embed_code 'puts "Hello, world!"'
+ para "But if you try that here, it won't work! The ", code("puts"), " method ",
+ "doesn't display a dialog box, it puts text out to a command-line prompt. ",
+ "Since Hackety Hack is all graphical, this doesn't work here. So we'll ",
+ "be using ", code("alert"), "s throughout these tutorials, but if you look ",
+ "at other Ruby tutorials, you may see ", code("puts"), "."
+ end
+
+ lesson "Letters, words, and sentences"
+ page "Strings" do
+ para "Okay! Now that you've got that verb bit down, it's time to learn about ",
+ em("String"), "s. Strings are what we call a bunch of words between a pair ",
+ "of \" characters. The \"s are used to tell the computer what words you ",
+ "actually want to say. Let's think about our example:"
+ embed_code 'alert "Hello, world!"'
+ para "If you didn't have the \"s, the computer wouldn't know which words were ",
+ "methods and which ones were part of the string! And consider this:"
+ embed_code 'alert "I am on high alert!"', :run_button => true
+ para "Without making all of those words a string, how would Ruby know that the ",
+ "second alert was some text you wanted to say, rather than another alert box?"
+ end
+
+ page "Adding Strings" do
+ para "Now, if you want to put two bits of strings together, you can use the ",
+ code("+"), " character to do it. Try typing this:"
+ embed_code 'alert "Hello, " + "world!"'
+ para "Same thing! The ", code("+"), " sticks the two strings together. This ",
+ "will end up being super useful later!"
+ end
+
+ lesson "Numbers and Math"
+ page "Numbers" do
+ para "You can just use numbers, and Ruby understands them:"
+ embed_code "alert 2"
+ para "You can even use numbers that have a decimal point in them:"
+ embed_code "alert 1.5"
+ end
+
+ page "Basic Math" do
+ para "You can also do math with numbers, and it'll work out pretty well:"
+ embed_code "alert 1 + 2"
+ para ""
+ embed_code "alert 5 - 3"
+ para ""
+ embed_code "alert 2 * 3"
+ para ""
+ embed_code "alert 4 / 2"
+ para ""
+ para "But if you try this, nothing happens:"
+ embed_code 'alert "hey" + 2'
+ para "This is kind of fun and silly, though:"
+ embed_code 'alert "hey" * 2'
+ end
+
+ page "Errors" do
+ para "You know how nothing happened when you hit the Run button earlier? That ",
+ "was because there was an error. You can see any errors that run by hitting ",
+ "either Control-/ or Command-/, depending on what kind of computer you're using."
+ para "The error that results from ", code('alert "hey" + 2'), " is "
+ embed_code "can't convert Fixnum into String"
+ para "What is that?"
+ end
+
+ lesson "A few words about types"
+ page "Why's it do that?" do
+ para "Each part of a Ruby program is an ", code("Object"), ". Right now, all you ",
+ "need to know about ", code("Object"), "s is that it's sort of like saying ",
+ '"a thing." Your program is made up of a bunch of ', code("Object"), "s ",
+ "working together."
+ para "We'll learn more about ", code("Object"), "s in a future lesson, but there ",
+ "is one thing I'll tell you: ", code("Object"), "s have a 'type.' This lets ",
+ "Ruby know what kind of ", code("Object"), " it is."
+ end
+
+ page "Adding numbers to words" do
+ para "That's why"
+ embed_code 'puts "hey" + 2'
+ para 'doesn\'t really work: "hey" is a ', code("String"), " object, and 2 is a ",
+ code("Fixnum"), " object. And adding ", code("String"), "s and ",
+ code("Fixnum"), "s doesn't make any sense. We can make this code work, though!"
+ para "All we need to do is turn the ", code("Fixnum"), " into a ", code("String"),
+ ". We can do this by using the ", code("to_s"), " method."
+ embed_code 'puts "hey" + 2.to_s'
+ end
+
+ page "Let's look at that again" do
+ embed_code 'puts "hey" + 2.to_s'
+ para "Okay, this isn't bad. We have our ", code("puts"), " method. We're giving it ",
+ code('"hey" + 2.to_s'), ". The ", code("2.to_s"), " turns a ",
+ code("Fixnum"), " 2, which is like the mathematical idea of a 2, into the ",
+ code("String"), " 2, which is like when you write a 2 down on a piece of ",
+ "paper."
+ end
+
+ lesson "Variables"
+ page "They're like boxes" do
+ para "What happens if we want to keep something around? Most programs are not ",
+ "one line, I assure you. You can use a ", em("variable"), " to hold a ",
+ "value and use it later. It's like a box that you put things in."
+ para "Let's try one out:"
+ embed_code 'message = "Hello, world!"
+alert message'
+ para "Give that a run."
+ end
+ page "Assignment" do
+ para "Cool stuff! We used an ", code("="), " to ", em("assign"), " the ", code("String"), '"Hello, world!" into the variable ', code("message"), ". We then passed that ",
+ code("message"), " to the ", code("alert"), " method."
+ para "As you can see, we can use variables in place of another value. Try this:"
+ embed_code 'number = 5
+number = number * 2
+number = number - 8
+number = number + 1
+alert number'
+ para "Make a guess before you run this program."
+ end
+
+ lesson "User Input"
+ page "ask-ing for it." do
+ para "We can ask the user of our program for some input, and then put their answer ",
+ "into a variable. It's easy! Check this program out:"
+ embed_code 'name = ask "What is your name?"
+alert "Hello, " + name'
+ para "The ", code("ask"), " method brings up a box and lets our users type ",
+ "something in. Fun! We put their answer into the ", code("name"), " variable ",
+ "and then showed it with ", code("alert"), ". Sweet!"
+ end
+
+ lesson "Basic flow control"
+ page "if..." do
+ para "Remember back to that Beginning Programming lesson... we talked about how ",
+ "programs are one big list, that the computer follows in order."
+ para "Well, guess what? We can actually change this order by using certain bits ",
+ "of code. Compare these two programs:"
+ embed_code 'number = 2
+if number == 2
+ alert "Yes!"
+else
+ alert "No!"
+end'
+ embed_code 'number = 1
+if number == 2
+ alert "Yes!"
+else
+ alert "No!"
+end'
+ para "There are a few new things here."
+ end
+
+ page "==" do
+ para "Here it is again:"
+ embed_code 'number = 2
+if number == 2
+ alert "Yes!"
+else
+ alert "No!"
+end'
+ para "The == command is just a bit different than the = command. == tests the ",
+ code("Object"), " on its right against the ", code("Object"), " on its left. ",
+ "If the two are equal, then the code after the ", code("if"), " will run. ",
+ "If they're not equal, you get the code after the ", code("else"), ". The ",
+ code("end"), " lets us know we're done with our ", code("if"), "."
+ end
+
+ lesson "Example: a guessing game"
+ page "Guess!" do
+ para "Let's put this all together:"
+ embed_code 'secret_number = 42
+guess = ask "I have a secret number. Take a guess, see if you can figure it out!"
+if guess == secret_number
+ alert "Yes! You guessed right!"
+else
+ alert "Sorry, you\'ll have to try again."
+end'
+ end
+
+ lesson "Summary"
+ page "Good job!" do
+ para "Congrats! You've picked up all of the basics of Ruby. There's a lot more ",
+ "you still have to learn, though!"
+ para "Here's what you've learned so far:"
+ para "* ", code("alert"), " and ", code("ask")
+ para "* ", code("="), ", variables, and ", code("==")
+ para "* ", code("if"), " and ", code("else")
+ para "Awesome! You'll want to check out Basic Shoes next!"
+ end
+
+end
diff --git a/app/lessons/basic_shoes.rb b/app/lessons/basic_shoes.rb
new file mode 100644
index 0000000..4227b2e
--- /dev/null
+++ b/app/lessons/basic_shoes.rb
@@ -0,0 +1,183 @@
+# encoding: UTF-8
+
+lesson_set "4: Basic Shoes" do
+
+ lesson "Hello there!"
+ page "Let's get started" do
+ para "Welcome to your first lesson about Shoes! I'm going to introduce you to the ",
+ "basics that Shoes brings to everyone who programs."
+ para "If you didn't know, Shoes is a Ruby toolkit that lets you build GUI programs ",
+ "really easy and fun!"
+ flow do
+ para "(click the little "
+ icon_button :arrow_right, nil do
+ alert "Not this one! The one below!"
+ end
+ para " on the bottom of the screen to get started)"
+ end
+ end
+
+ page "Lesson Controls" do
+ para "Before we move on, Here's a refresher on the controls you can use ",
+ "to move around in the Lesson."
+ flow do
+ icon_button :arrow_left, nil
+ para strong("back"), ": goes back one page"
+ end
+ flow do
+ icon_button :arrow_right, nil
+ para strong("continue"), ": goes to the next page"
+ end
+ flow do
+ icon_button :menu, nil
+ para strong("menu"), ": makes it easy to jump around to any lesson"
+ end
+ flow do
+ icon_button :x, nil
+ para strong("close"), ": closes the tutor"
+ end
+ para "Don't forget! Press "
+ icon_button :arrow_right, nil
+ para "to move to the next part. Have at it!"
+ end
+
+ lesson "Apps"
+ page "Shoes.app" do
+ para "Okay! Shoes is tons of fun. It's really easy to get started. Here's the ",
+ "simplest Shoes app ever:"
+ embed_code "Shoes.app do
+end"
+ para "Give that a spin!"
+ end
+
+ page "It's just a block" do
+ para "You didn't say that you wanted anything in the app, so it just gives you ",
+ "a blank window. You can pass options in, too: "
+ embed_code "Shoes.app :height => 200, :width => 200 do
+end"
+ para "This'll give you whatever sized app you want! We'll be putting all of the ",
+ "fun stuff inside of the ", code("do...end"), "."
+ end
+
+ lesson "para"
+ page "The basics" do
+ para "Blank windows are pretty boring, so let's spice it up with some text!"
+ embed_code 'Shoes.app do
+ para "Hello, world"
+end'
+ para "You know what to do by now. ", code("para"), " is short for 'paragraph.' It ",
+ "lets you place text in your apps."
+ para code("para"), " and other Shoes widgets take bunches of options, too. Check ",
+ "it:"
+ embed_code 'Shoes.app do
+ para "Hello there, world", :font => "TakaoGothic"
+end'
+ end
+
+ lesson "stacks"
+ page "They're default!" do
+ para "If you're looking to lay out your Shoes widgets, there are two options. The ",
+ "first is a ", code("stack"), ". A Stack is the default layout a Shoes app ",
+ "has. So this won't look much differently than one without the stack:"
+ embed_code 'Shoes.app do
+ stack do
+ para "Hello!"
+ para "Hello!"
+ para "Hello!"
+ end
+end'
+ para "As you can see, the ", code("para"), "s are stacked on top of each other. ",
+ "By itself, kinda boring, since they already do this. But..."
+ end
+
+ lesson "flows"
+ page "The counterpart of stacks" do
+ para code("flow"), "s are kind of like stacks, but they go sideways rather than ",
+ "up and down. Try this as an example:"
+ embed_code 'Shoes.app do
+ flow do
+ para "Hello!"
+ para "Hello!"
+ para "Hello!"
+ end
+end'
+ para "Just a little bit different, eh?"
+ end
+
+ lesson "stacks + flows"
+ page "With their powers combined..." do
+ para "You can combine the ", code("stack"), " with the ", code("flow"), "s ",
+ "to make whatever kind of layout you want. For example: "
+ embed_code 'Shoes.app do
+ flow do
+ stack :width => "50" do
+ para "Hello!"
+ para "Hello!"
+ para "Hello!"
+ end
+ stack :width => "50" do
+ para "Goodbye!"
+ para "Goodbye!"
+ para "Goodbye!"
+ end
+ end
+end'
+ para "The ", code(":width"), " attribute sets how wide the stack is. Pretty simple."
+ end
+
+ lesson "button"
+ page "Push it real good" do
+ para "Buttons are also super simple in Shoes. Just give them a title and a ",
+ "bunch of code to run when they get pushed:"
+ embed_code 'Shoes.app do
+ button "Push me" do
+ alert "Good job."
+ end
+end'
+ para "I bet you're starting to see a pattern. Shoes loves to use blocks of code ",
+ "to make things super simple."
+ end
+
+ lesson "image"
+ page "Pics or it didn't happen" do
+ para "There are two ways that you can show an image in a Shoes app. Either you ",
+ "have the file on your computer:"
+ embed_code 'Shoes.app do
+ image "#{HH::STATIC}/matz.jpg"
+end'
+ para "(Can you figure out what this does? Don't feel bad if you can't.)"
+ para "You can also specify an image on the web:"
+ embed_code 'Shoes.app do
+ image "http://shoesrb.com/images/shoes-icon.png"
+end'
+ para "Either one is fine. Shoes cares not."
+ end
+
+ lesson "edit_line"
+ page "Getting some input" do
+ para "If you'd like to let someone type something in a box, well, ",
+ code("edit_line"), " is right up your alley!"
+ embed_code 'Shoes.app do
+ edit_line
+end'
+ para "This is sort of boring though... why not get the information from the box?"
+ embed_code 'Shoes.app do
+ line = edit_line
+ button "Push me!" do
+ alert line.text
+ end
+end'
+
+ end
+
+ lesson "Summary"
+ page "Great job!" do
+ para "There's a ton more things that you can do with Shoes, but you've got the ",
+ "basics down!"
+ para "If you'd like to learn more, you can visit the ",
+ link("Shoes website",
+ :click => "http://shoesrb.com/"),
+ " or press Control-M (or Command-M) to bring up the Shoes Manual."
+ end
+
+end
diff --git a/app/lessons/tour.rb b/app/lessons/tour.rb
new file mode 100644
index 0000000..abb4fcf
--- /dev/null
+++ b/app/lessons/tour.rb
@@ -0,0 +1,180 @@
+lesson_set "1: A Tour of Hackety Hack" do
+ lesson "Welcome!"
+ page "Why hello there!" do
+ para "Welcome to the Hackety Hack tour!"
+ flow do
+ para "This whole side of the screen is the ", em("Hackety Hack Tutor"),
+ ". You can move forward through the lessons by clicking the ",
+ em("Next"), " button("
+ icon_button :arrow_right, nil do
+ alert "You should click on the actual button, below! =)"
+ end
+ para "). Give it a shot!"
+ end end
+
+ page "Good Job!" do
+
+ para "See? Super easy. Let's explore the rest of Hackety Hack."
+ para "You can access the different functions of Hackety through the buttons ",
+ "on the left side of the screen. For example, you got here by clicking ",
+ "on 'Lessons.' There are 8 of those buttons, ",
+ "but since you're already on Lessons, let's talk about them first."
+ para "Before we move on, just take a minute to look at the controls in the bar below."
+ flow do
+ icon_button :arrow_left, nil
+ para strong("back"), ": goes back one page"
+ end
+ flow do
+ icon_button :arrow_right, nil
+ para strong("continue"), ": goes to the next page"
+ end
+ flow do
+ icon_button :menu, nil
+ para strong("menu"), ": makes it easy to jump around to any lesson"
+ end
+ flow do
+ icon_button :x, nil
+ para strong("close"), ": closes the tutor"
+ end
+ para "Don't forget! Press "
+ icon_button :arrow_right, nil
+ para "to move to the next part. Have at it!"
+ end
+
+ lesson "Lessons"
+
+ page "A Lesson lesson." do
+ para "When you click on the Lesson button, it'll bring you to a list of all of ",
+ "the lessons that come with Hackety. For now, there's two: This Tour, and ",
+ "a basic introduction to Ruby. More Lessons will be added, and eventually, ",
+ "you'll be able to write and share your own Lessons with other Hackety ",
+ "Hackers."
+ para "Lessons are just simple Ruby files. They're fun to make! You can even make ",
+ "lessons advance automatically based on certain events. For example, click ",
+ " on the Home button to move on."
+ para "The home button looks like this:"
+ image "#{HH::STATIC}/tab-home.png", :margin => 6 do
+ alert("Not this one, silly! the one on the left!")
+ end
+ next_when :tab_opened, :Home
+ end
+
+ lesson "Home"
+ page "Welcome Home" do
+ para "This is the home screen, which shows you two very important things: your ",
+ "own programs, and the sample programs. Everyone starts off with one simple ",
+ "program: Hello, world! I won't even ask you to open it, check it out:\n"
+ embed_code 'alert "Hello, world!"', :run_button => true
+ para "This is an actual Ruby program, click the button to try it out! You'll ",
+ "learn more about Ruby itself in the Beginning Ruby Lesson."
+ end
+
+ page "Samples" do flow do
+ para "If you click on the 'Samples' tab, you can see a bunch of sample programs ",
+ "that we've included for some inspiration. There's a few interesting ",
+ "animations, some games, and a few other things."
+ para "That's all there really is to say about the homepage. Try opening the ",
+ "Editor. Here's its icon:"
+ image "#{HH::STATIC}/tab-new.png", :margin => 6 do
+ alert("Not this one, silly! the one on the left!")
+ end
+ next_when :tab_opened, :Editor
+ end end
+
+ lesson "Editor"
+ page "Using the Editor" do
+ para "This is where the magic happens: all of your programs will be created in ",
+ "the editor. Give it a shot: try typing this program in.\n"
+ embed_code 'name = ask "What is your name?"
+alert "Hello, " + name + "."'
+ para "\nAfter doing so, you can try running the program by pressing the ",
+ "'Run' button in the lower right corner."
+ end
+
+ page "Saving and Uploading Programs" do
+ para "To save your program, simply click the 'Save' button. It'll prompt you for ",
+ "a title, and then the program will appear on your Home screen."
+ para "Once you've saved your program, two new buttons appear: 'Copy' and 'Upload.",
+ "' Copy will duplicate your program, and then ask you for a new name. This ",
+ " is really useful if you'd like to modify one of the example programs. ",
+ "Upload will send a copy of your program to the Hackety Hack website, ",
+ "where you can show it off to other Hackety Hackers. :) More about this ",
+ "when we talk about Preferences."
+ end
+
+ lesson "Help"
+ page "Getting Help" do flow do
+ para "The next tab is the Help tab. It looks like this: "
+ image "#{HH::STATIC}/tab-help.png", :margin => 6 do
+ alert("Not this one, silly! the one on the left!")
+ end
+ para " Click it, and it'll open up a new window. Browse around and come back, ",
+ "I'll be here."
+ next_when :tab_opened, :Help
+ end end
+
+ page "Okay, well... Shoes." do
+ para "That's a lot of help! Hackety Hack is built with Shoes, which is a ",
+ "toolkit for creating GUI programs in Ruby. All of the programs that ",
+ "you make in Hackety Hack are built with Shoes. That manual contains ",
+ "the entire Shoes reference, and there's a lot! Luckily, there's also ",
+ "a much shorter cheat sheet too. Go ahead click it:"
+ image "#{HH::STATIC}/tab-cheat.png", :margin => 6 do
+ alert("Not this one, silly! the one on the left!")
+ end
+ next_when :tab_opened, :Cheat
+ end
+
+ lesson "Cheat"
+ page "Short and sweet." do flow do
+ para "The Cheat Sheet is much simpler. It just contains some helpful bits ",
+ "that you should find useful. A quick reference of often used bits. ",
+ "And a short sheet deserves a short explanation. Check out the About ("
+ image "#{HH::STATIC}/tab-hand.png", :margin => 6 do
+ alert("Not this one, silly! the one on the left!")
+ end
+ para ") tab next."
+ next_when :tab_opened, :About
+ end end
+
+ lesson "About"
+ page "About Hackety" do
+ para "The classic About box. These have been around basically since the ",
+ "beginning of time. It's just a fun little image that tells you what ",
+ "version of Hackety Hack you're using. It'll change with every release."
+ para "Time for the last one: open up the Preferences tab."
+ next_when :tab_opened, :Prefs
+ end
+
+ lesson "Preferences"
+ page "I do prefer..." do
+ para "This lets you adjust your preferences for Hackety Hack. Right now, there's ",
+ "only one preference: linking Hackety with your account on ",
+ link("hackety-hack.com", :click => "http://hackety-hack.com"), ". You ",
+ strong("do"), " have one of those, right?"
+ para "If you link your account, you can upload your programs to the website ",
+ "and easily share them with others! More interesting features will be ",
+ "developed along these lines, so sign up, stick your info in, and prepare ",
+ "for all kinds of awesome."
+ para "I won't make you click the button to advance this time... instead, just ",
+ "click the arrow to advance."
+ end
+
+ lesson "Quit"
+ page "Self-explanatory" do
+ para "If you did click the quit button, well, you wouldn't be here anymore. ",
+ "And that'd be unfortunate. So, don't click it until you're good and ready. ",
+ "When it's your time to go, it'll be there waiting for you. Come back soon!"
+ end
+
+ lesson "... and beyond!"
+ page "What now?" do
+ para "This concludes the Hackety Hack tour. Good job! Now you know everything ",
+ "that Hackety Hack can do. It's pretty simple!"
+ para "This isn't the only lesson that we have for you, though. Give the ",
+ "'Beginning Programming' lesson a shot to actually start learning how to ",
+ "make programs of your own."
+ para "What are you waiting for? Get going!"
+ end
+
+end
diff --git a/app/lib/all.rb b/app/lib/all.rb
new file mode 100644
index 0000000..7b8f9bd
--- /dev/null
+++ b/app/lib/all.rb
@@ -0,0 +1,9 @@
+require 'lib/web/all'
+require 'lib/dev/init'
+
+require 'lib/art/turtle'
+require 'lib/enhancements'
+require 'lib/dev/errors'
+require 'lib/dev/events'
+require 'lib/dev/stdout'
+
diff --git a/app/lib/art/turtle.rb b/app/lib/art/turtle.rb
new file mode 100644
index 0000000..f4a7690
--- /dev/null
+++ b/app/lib/art/turtle.rb
@@ -0,0 +1,349 @@
+# a turtle graphics library
+
+require 'thread'
+
+class Shoes::TurtleCanvas < Shoes::Widget
+ # default values
+ WIDTH = 500
+ HEIGHT = 500
+ SPEED = 4 # power of two
+
+ include Math
+ DEG = PI / 180.0
+
+ # para with the next command written on it
+ attr_writer :next_command, :pen_info
+ attr_accessor :speed # powers of two
+ attr_reader :width, :height
+
+ def initialize
+ @width = WIDTH
+ @height = WIDTH
+ style width => @width, :height => @height
+ @queue = Queue.new
+ @image = image "#{HH::STATIC}/turtle.png"
+ @image.transform :center
+ @speed = SPEED
+ @paused = true
+ reset
+ move_turtle_to_top
+ end
+
+ def start_draw
+ @paused = false
+ @speed = nil
+ @image.hide
+ end
+
+
+ ### user commands ###
+
+ def reset
+ clear_orig
+ @pendown = true
+ @heading = 180*DEG # internal heading is rotated by 180 w.r.t user heading
+ @pendown = true
+ @turtle_angle = 180
+ @bg_color = white
+ @fg_color = black
+ @pen_size = 1
+ background_orig @bg_color
+ stroke @fg_color
+ strokewidth @pen_size
+ update_position(@width/2, @height/2)
+ update_turtle_heading
+ end
+
+ def forward len=100
+ is_step
+ x = len*sin(@heading) + @x
+ y = len*cos(@heading) + @y
+ if @pendown
+ l = [@x, @y, x, y]
+ line(*l)
+ end
+ update_position(x, y)
+ end
+ def backward len=100
+ forward(-len)
+ end
+ def turnleft angle=90
+ is_step
+ @heading += angle*DEG
+ @heading %= 2*PI
+ update_turtle_heading
+ end
+ def turnright angle=90
+ turnleft(-angle)
+ end
+ def setheading direction=180
+ is_step
+ direction += 180
+ direction %= 360
+ @heading = direction*DEG
+ update_turtle_heading
+ end
+ def penup
+ @pendown = false
+ end
+ def pendown
+ is_step
+ @pendown = true
+ end
+ def pendown?
+ return @pendown
+ end
+ def goto x, y
+ is_step
+ update_position(x, y)
+ end
+ def center
+ go(width/2, height/2)
+ end
+ def setx x
+ is_step
+ update_position(x, @y)
+ end
+ def sety y
+ is_step
+ update_position(@x, y)
+ end
+ def getx
+ @x
+ end
+ def gety
+ @y
+ end
+ def getposition
+ [@x, @y]
+ end
+ def getheading
+ degs = @heading/DEG
+ degs += 180
+ degs % 360
+ end
+
+ ### user commands already in shoes (the first two with another name ###
+
+ def pencolor args
+ is_step
+ stroke args
+ @fg_color = args
+ update_pen_info
+ end
+
+ def pensize args
+ is_step
+ strokewidth args
+ @pen_size = args
+ update_pen_info
+ end
+
+ alias clear_orig clear
+ alias background_orig background
+
+ def clear *args
+ in_step
+ clear_orig *args
+ end
+ def background args
+ is_step
+ background_orig args
+ move_turtle_to_top
+ @bg_color = args
+ update_pen_info
+ end
+
+
+ ## UI commands: should not be used by the user ##
+
+ def step
+ @queue.enq nil
+ end
+
+ def toggle_pause
+ @paused = !@paused
+ if !@paused
+ @speed = SPEED if @speed.nil?
+ step
+ end
+ @paused # return value
+ end
+
+ def save filename
+ _snapshot :filename => filename, :format => :pdf
+ end
+
+private
+ def update_position x, y
+ @x, @y = x, y
+ @image.move(x.round - 16, y.round - 16) unless drawing?
+ end
+
+
+ def update_turtle_heading
+ # update turtle image
+ angle_in_degrees = @heading/DEG
+ diff = (angle_in_degrees - @turtle_angle).round
+ @turtle_angle += diff
+ @image.rotate(diff) unless drawing?
+ end
+
+ def move_turtle_to_top
+ return if drawing?
+ s = @image.style
+ @image = image "#{HH::STATIC}/turtle.png"
+ @image.style s
+ end
+
+ def is_step
+ return if drawing?
+ display
+ if @paused
+ # wait for step
+ @queue.deq
+ else
+ sleep 1.0/@speed
+ if @paused # if it got paused in the meantime
+ @queue.deq
+ end
+ end
+ end
+
+ def display
+ method = nil
+ bt = caller
+ 1.upto 4 do |i|
+ m = bt[i][/`([^']*)'/, 1]
+ if m.nil? || m =~ /^block /
+ break
+ else
+ method = m
+ end
+ end
+ @next_command.replace(method)
+ end
+
+ # false if drawing directly the final result
+ def drawing?
+ @speed.nil? and not @paused
+ end
+
+ def update_pen_info
+ @pen_info.append do
+ background_orig @bg_color
+ line 5, 10, 35, 10, :stroke => @fg_color, :strokewidth => @pen_size
+ end if @pen_info
+ end
+end
+
+module Turtle
+ def self.draw opts={}, &blk
+ opts[:draw] = true
+ start opts, &blk
+ end
+
+ def self.start opts={}, &blk
+ w = opts[:width] || Shoes::TurtleCanvas::WIDTH
+ h = opts[:height] || Shoes::TurtleCanvas::HEIGHT
+ opts[:width] = w + 20
+ opts[:height] = h + ( opts[:draw]? 60 : 130)
+
+ Shoes.app opts do
+ extend Turtle # add methods back (after self changed)
+ @block = blk
+
+ unless opts[:draw]
+ para "pen: "
+ @pen_info = stack :top => 5, :width => 40, :height => 20 do
+ background white
+ line 5, 10, 35, 10
+ end
+ end
+
+ glossb "save...", :color => 'dark', :right => '-0px', :width => 100 do
+ filename = ask_save_file
+ unless filename.nil?
+ filename += '.pdf' unless filename =~ /\.pdf$/
+ @canvas.save filename
+ end
+ end
+
+ stack :height => h + 20 do
+ background gray
+ stack :top => 10, :left => 10, :width => w, :height => h do
+ shape do
+ background white
+ @canvas = turtle_canvas
+ end
+ end
+ end
+
+ if opts[:draw]
+ draw_all
+ else
+ draw_controls
+ @interactive_thread = Thread.new do
+ sleep 0.1 # HACK
+ @canvas.instance_eval &blk
+ @next_command.replace("(END)")
+ end
+ end
+ end
+ end
+
+private
+ def execute_canvas_code blk
+ @canvas.instance_eval do
+ shape do
+ self.instance_eval &blk
+ end
+ end
+ end
+
+ def draw_controls
+ flow do
+ stack do
+ flow do
+ para "next command: "
+ @next_command = para 'start', :font => 'Liberation Mono'
+ @canvas.next_command = @next_command
+ end
+ end
+ glossb "execute", :color => 'dark', :width => 100, :right => '-0px' do
+ @canvas.step
+ end
+ end
+
+ flow do
+ glossb "slower", :color => 'dark', :width => 100 do
+ @canvas.speed /= 2 if @canvas.speed > 2
+ end
+ @toggle_pause = glossb "play", :color => 'dark', :width => 100 do
+ paused = @canvas.toggle_pause
+ if paused
+ @toggle_pause.text = 'play'
+ else
+ @toggle_pause.text = 'pause'
+ end
+ end
+ glossb "faster", :color => 'dark', :width => 100 do
+ @canvas.speed *= 2
+ end
+ glossb "draw all", :color => 'dark', :right => '-0px', :width => 100 do
+ @interactive_thread.kill
+ @canvas.reset
+ @next_command.replace("(draw all)")
+ draw_all
+ end
+ end
+ @canvas.pen_info = @pen_info
+ end
+
+ def draw_all
+ timer 0.1 do
+ @canvas.start_draw
+ execute_canvas_code @block
+ end
+ end
+end
diff --git a/app/lib/dev/errors.rb b/app/lib/dev/errors.rb
new file mode 100644
index 0000000..bdae3e9
--- /dev/null
+++ b/app/lib/dev/errors.rb
@@ -0,0 +1,153 @@
+#
+# = Exceptions and Errors =
+#
+# attempting to bring friendlier
+# language to the whole thing.
+#
+class Exception
+ TRACE_RE = %r!(.+?):(.+?):in `(.+?)':\s*(.*)!
+ EXCLAMATIONS = ['Oops!', 'Holy cats!', 'By jove,', 'Pardon:',
+ 'Whoops!', 'Great coats!', 'Hot pickles!', 'Hot snacks!',
+ 'Actually:', 'Crikey,', 'Yipes,']
+ def file
+ message[TRACE_RE, 1]
+ end
+ def line
+ message[TRACE_RE, 2].to_i
+ end
+ def where
+ message[TRACE_RE, 3]
+ end
+ def says
+ message[TRACE_RE, 4] or message
+ end
+ def exclamation
+ EXCLAMATIONS[rand(EXCLAMATIONS.length)]
+ end
+ def token_name t
+ case t.downcase
+ when "'['"
+ "an opening bracket `[`"
+ when "']'"
+ "a closing bracket `]`"
+ when "'('"
+ "an opening parentheses `(`"
+ when "')'"
+ "a closing parentheses `)`"
+ when "'{'"
+ "an opening curly brace `{`"
+ when "'}'"
+ "a closing curly brace `}`"
+ when /'(.+?)'/
+ "a `#$1`"
+ when "tstar"
+ "an asterisk"
+ when /\$end/
+ "the end of the program"
+ when /t(.+)/
+ word = $1
+ if word =~ /^[aeiou]/
+ "an #{word}"
+ else
+ "a #{word}"
+ end
+ when /k(.+)/
+ word = $1
+ if word =~ /^[aeiou]/
+ "an `#{word}`"
+ else
+ "a `#{word}`"
+ end
+ else
+ t
+ end
+ end
+ def friendly
+ s = self.says
+ msg, xtra =
+ case self
+ when LocalJumpError
+ case s
+ when "no block given"
+ ["A block is missing."]
+ else [s]
+ end
+ when NoMethodError
+ case s
+ when /undefined method `([^']+)' for (.+?):(.+)/
+ ["No `#$1` method found.", "You tried to use the `#$1` method on a #$3 object. (The object was: #{$2})"]
+ when /private method `(.+?)' called for (.+?):(.+)/
+ ["The `#$1` method is private on #$3.", "Check the help page for #$3 objects."]
+ else [s]
+ end
+ when NameError
+ case s
+ when /uninitialized constant (.+)/
+ "There is nothing called `#$1`."
+ when /undefined local variable or method `(.+?)'/
+ "There is nothing called `#$1`."
+ else [s]
+ end
+ when ArgumentError
+ case s
+ when /wrong number of arguments \((\d+) for (\d+)\)/
+ given, needed = $1.to_i, $2.to_i
+ if given < needed
+ ["The `#{where}` method needs a bit more.", "You sent it #{given} arguments, while it needs #{needed}."]
+ else
+ ["The `#{where}` method was given too much.", "You gave it #{given} arguments, but it only needs #{needed}."]
+ end
+ else [s]
+ end
+ when SyntaxError
+ case s
+ when /unterminated string meets end of file/
+ ['You are missing an end quote.']
+ when /unexpected (.+?), expecting (.+)/
+ t1, t2 = $1, $2
+ ["#{token_name(t2).capitalize} went missing.", "#{token_name(t1).capitalize} was found where #{token_name(t2)} should be."]
+ when /unexpected (.+)/
+ if $1 == "\$end"
+ ["This program isn't finished. A block or method was left open."]
+ else
+ ["#{token_name($1).capitalize} doesn't match up."]
+ end
+ else [s]
+ end
+ when SocketError
+ case s
+ when /no address associated with hostname/
+ ['No such hostname (in `#{where}`.)', 'Is your internet connection okay? Check with a browser.']
+ else [s]
+ end
+ else
+ [self.class.name, self.says]
+ end
+ msg = "= #{exclamation} #{msg} ="
+ msg += "\n#{xtra}" if xtra
+ msg
+ rescue => e
+ "#{e.class}: #{e.says}"
+ end
+ EVAL_TRACE_RE = /^\(eval\):(\d+)/
+ METH_TRACE_RE = /in `([^']+)'/
+ def hint
+ line =
+ if l = message[TRACE_RE, 2]
+ l.to_i
+ elsif backtrace.first
+ if backtrace.first =~ /in `eval'|^\(eval\):/
+ if eval_line = backtrace.grep(EVAL_TRACE_RE).first
+ eval_line[EVAL_TRACE_RE, 1].to_i
+ end
+ end
+ end
+ if line
+ "Check line #{line} of your program."
+ elsif line = backtrace.grep(EVAL_TRACE_RE).first
+ line = line[EVAL_TRACE_RE, 1]
+ meth = backtrace.grep(METH_TRACE_RE).last[METH_TRACE_RE, 1]
+ "The problem appears to be inside the '''#{meth}''' method used on '''line #{line}''' of your program.\n\nThis problem could be Hackety Hack's fault. It'd be good to ask about this problem in the online [[http://talkety.hacketyhack.net forums]]."
+ end
+ end
+end
diff --git a/app/lib/dev/events.rb b/app/lib/dev/events.rb
new file mode 100644
index 0000000..c891ef5
--- /dev/null
+++ b/app/lib/dev/events.rb
@@ -0,0 +1,112 @@
+module HH; end
+
+class HH::EventConnection
+# module Array
+# def ===(other)
+# return false unless other.is_a? ::Array
+# (0...size).each do |i|
+# cond = self[i]
+# return false unless cond == :any || cond === other[i]
+# end
+# return false
+# end
+# end
+
+ attr_reader :event
+
+ def initialize event, args_cond, &blk
+ @event, @args_cond, @blk = event, args_cond, blk
+ end
+
+ # executes the connection if the arguments match
+ def try args
+ # the argument conditions matched
+ @blk.call *args if match? args
+ end
+
+private
+ # checks if the arguments +args+ match the conditions +@args_cond+
+ def match? args
+ # match size
+ return false if @args_cond.size != args.size
+
+ if @args_cond.size == 1 && @args_cond[0].is_a?(Hash)
+ return self.class.match_hash?(@args_cond[0], args[0])
+ end
+
+ # else match each element
+ (0...args.size).each do |i|
+ cond = @args_cond[i]
+ return false unless cond == :any || cond === args[i]
+ end
+ return true
+ end
+
+ def self.match_hash?(cond, hash)
+ cond.is_a?(Hash) or raise ArgumentError
+ return false unless hash.is_a?(Hash)
+ cond.each do |key, cond|
+ return false unless cond === hash[key]
+ end
+ return true
+ end
+
+# def observer
+# @blk.binding.eval("self")
+# end
+
+public
+ def to_s
+ "#<EventConnection :#{@event} #{@args_cond.inspect}] >"
+ end
+
+ alias inspect to_s
+end
+
+
+class HH::EventCondition
+ def initialize &blk
+ @blk = blk
+ end
+
+ def === args
+ if not args.is_a? Hash
+ raise ArgumentError, "for now EventCondition only works on hash events"
+ end
+ @blk.call args# && match_hash?(args, {})
+ end
+
+#private
+# def simple_condition_match? args
+# HH::EventConnection.match_hash?(@simple_condition, hash)
+# end
+end
+
+
+require 'set'
+
+module HH::Observable
+ def emit event, *args
+ return unless @event_connections
+ connections = @event_connections[event]
+ connections.each {|c| c.try(args)}
+ end
+
+ # :any is a condition that always matches
+ # returns the new connection (that can be useful later to delete it)
+ def on_event event, *args_cond, &blk
+ # in first call initialize @event_connections
+ @event_connections = Hash.new(Set.new) if @event_connections.nil?
+ new_conn = HH::EventConnection.new event, args_cond, &blk
+ @event_connections[event] += [new_conn]
+ #debug "#{new_conn} added"
+ #emit :new_event_connection, new_conn
+ new_conn
+ end
+
+ def delete_event_connection c
+ #debug "#{c} deleted!"
+ @event_connections[c.event].delete c
+ end
+end
+
diff --git a/app/lib/dev/init.rb b/app/lib/dev/init.rb
new file mode 100644
index 0000000..37d534b
--- /dev/null
+++ b/app/lib/dev/init.rb
@@ -0,0 +1,48 @@
+# sets constant in the HH module and environment variables
+# the current directory in set to HH::USER (~/.hacketyhack on unix systems)
+# (HH::APP is initialized in h-ety-h.rb instead)
+
+HH::NET = "hackety-hack.com"
+HH::REST = "http://hackety-hack.com"
+#for easy switching when developing
+#HH::NET = "localhost:3000"
+#HH::REST = "http://localhost:3000"
+HH::HOME = Dir.pwd
+HH::STATIC = HH::HOME + "/static"
+HH::FONTS = HH::HOME + "/fonts"
+HH::LESSONS = HH::HOME + "/lessons"
+$LOAD_PATH << HH::HOME
+
+# platform-specific directories
+case RUBY_PLATFORM when /win32/, /i386-mingw32/
+ require 'lib/dev/win32'
+ HOME = ENV['USERPROFILE'].gsub(/\\/, '/')
+ ENV['MYDOCUMENTS'] = HH.read_shell_folder('Personal')
+ ENV['APPDATA'] = HH.read_shell_folder('AppData')
+ ENV['DESKTOP'] = HH.read_shell_folder('Desktop')
+ HH::USER =
+ begin
+ HH.win_path(Win32::Registry::HKEY_CURRENT_USER.
+ open('Software\Hackety.org\Hackety Hack').
+ read_s('HackFolder'))
+ rescue
+ HH.win_path('%APPDATA%/Hackety Hack')
+ end
+else
+ ENV['DESKTOP'] = File.join(ENV['HOME'], "Desktop")
+ ENV['APPDATA'] = ENV['HOME']
+ ENV['MYDOCUMENTS'] = ENV['HOME']
+ HH::USER = File.join(ENV['HOME'], ".hacketyhack")
+end
+
+HH::DOWNLOADS = File.join(HH::USER, 'Downloads')
+FileUtils.makedirs(HH::DOWNLOADS)
+
+Dir.chdir(HH::USER)
+
+font "#{HH::FONTS}/Lacuna.ttf"
+font "#{HH::FONTS}/LiberationMono-Regular.ttf"
+font "#{HH::FONTS}/LiberationMono-Bold.ttf"
+font "#{HH::FONTS}/Pixelpoiiz.ttf"
+font "#{HH::FONTS}/Phonetica.ttf"
+font "#{HH::FONTS}/TakaoGothic.otf"
diff --git a/app/lib/dev/stdout.rb b/app/lib/dev/stdout.rb
new file mode 100644
index 0000000..e07e14c
--- /dev/null
+++ b/app/lib/dev/stdout.rb
@@ -0,0 +1,9 @@
+# allow to track standard output
+require 'lib/dev/events'
+
+STDOUT.extend HH::Observable
+
+def STDOUT.write str
+ emit :output, str if str
+ super # default behaviour
+end
diff --git a/app/lib/dev/win32.rb b/app/lib/dev/win32.rb
new file mode 100644
index 0000000..ef8687b
--- /dev/null
+++ b/app/lib/dev/win32.rb
@@ -0,0 +1,29 @@
+require 'Win32API'
+require 'win32/registry'
+
+module HH
+ SHGetFolderPath = Win32API.new "shell32.dll", "SHGetFolderPath", %w[P I P I P], "I"
+ class << self
+ def read_shell_folder(name)
+ x =
+ case name
+ when "Personal"; 0x05
+ when "AppData"; 0x1A
+ when "Desktop"; 0x00
+ end
+ path = " " * 256
+ SHGetFolderPath.call(0, x, 0, 0, path)
+ path.strip.gsub("\0", "").gsub(/\\/, '/')
+ end
+ def win_vars(str)
+ str.gsub(/%DESKTOP%/, ENV['DESKTOP']).
+ gsub(/%USERNAME%/) { HH::PREFS['hh_username'] }.
+ gsub(/%APPDATA%/, ENV['APPDATA']).
+ gsub(/%MYDOCUMENTS%/, ENV['MYDOCUMENTS']).
+ gsub(/%HACKETY_USER%/) { HH::USER }
+ end
+ def win_path(str)
+ win_vars(str.gsub(/\\/, '/'))
+ end
+ end
+end
diff --git a/app/lib/enhancements.rb b/app/lib/enhancements.rb
new file mode 100644
index 0000000..5ce6e1e
--- /dev/null
+++ b/app/lib/enhancements.rb
@@ -0,0 +1,158 @@
+# Extensions to existing classes
+
+module Kernel
+ def say arg
+ HH::APP.say arg
+ end
+end
+
+
+class Object
+ # rails-like blank? method
+ def blank?
+ if respond_to? :empty?
+ empty?
+ elsif respond_to? :zero?
+ zero?
+ else
+ !self
+ end
+ end
+
+ def try(method, *args)
+ respond_to?(method) ? send(method, *args) : self
+ end
+
+ # FIXME fixes the link inherited from FileUtils, I don't know why or where
+ # FileUtils is extended...
+ undef link if defined? link
+end
+
+class String
+ # checks if the string starts with the string +beginning+
+ def starts?( beginning )
+ self[0, beginning.length] == beginning
+ end
+ # checks if the string ends with the string +ending+
+ def ends?( ending )
+ self[-ending.length, ending.length] == ending
+ end
+ def remove( phrase )
+ r = dup
+ r[phrase] = ""
+ r
+ end
+ def to_a
+ self.split "\n"
+ end
+
+ # used to convert strings into slugs, just like the website uses.
+ def to_slug
+ self.gsub(/\s/, "_").gsub(/\W/, "").downcase
+ end
+
+ # rot 13 encoding
+ def rot13
+ tr("A-Za-z", "N-ZA-Mn-za-m")
+ end
+
+ # rot 13 encoding
+ def rot13!
+ tr!("A-Za-z", "N-ZA-Mn-za-m")
+ end
+end
+
+
+#
+# = Numbers
+#
+# Enhancements to the basic number classes.
+#
+class Fixnum
+ def ordinalize
+ case self
+ when 1; "1st"
+ when 2; "2nd"
+ when 3; "3rd"
+ else "#{self}th"
+ end
+ end
+ def weeks; self * 7*24*60*60; end
+end
+
+
+
+#
+# = Time =
+#
+# Enhancements to the clock.
+#
+class Time
+ def calendar
+ "#{strftime('%B')} #{day.ordinalize}, #{year}"
+ end
+ def calendar_with_time
+ "#{strftime('%B')} #{day.ordinalize}, #{year} at #{time_only}"
+ end
+ def time_only
+ h = hour % 12
+ h = 12 if h.zero?
+ "#{h}:#{strftime('%M %p')}"
+ end
+ def quick
+ "#{strftime('%b')} #{day}, #{year} at #{time_only.downcase.gsub(' ', '')}"
+ end
+ def short
+ "#{strftime('%b')} #{day}"
+ end
+ def full
+ strftime("%Y-%m-%d %H:%M:%S")
+ end
+ def since(new_time = Time.now, include_seconds = false)
+ time_span = new_time.to_i - self.to_i
+ distance_in_minutes = (((time_span).abs)/60.0).round
+ distance_in_seconds = ((time_span).abs).round
+
+ case distance_in_minutes
+ when 0..1
+ if not include_seconds
+ return (distance_in_seconds < 55) ? 'less than a minute' : '1 minute'
+ end
+ # else:
+ case distance_in_seconds
+ when 0..4 then 'less than 5 seconds'
+ when 5..9 then 'less than 10 seconds'
+ when 10..19 then 'less than 20 seconds'
+ when 20..39 then 'half a minute'
+ when 40..59 then 'less than a minute'
+ else '1 minute'
+ end
+ when 2..45 then "#{distance_in_minutes} minutes"
+ when 46..90 then 'about 1 hour'
+ when 91..1440 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
+ when 1441..2879 then '1 day'
+ else
+ days = (distance_in_minutes / 1440)
+ if (days / 365) > 0
+ "#{days / 365} years"
+ else
+ "#{days % 365} days"
+ end
+ end
+ end
+end
+
+require 'thread'
+class Thread
+ alias initialize_orig initialize
+ def initialize *args, &blk
+ initialize_orig *args do
+ begin
+ blk.call
+ rescue => ex
+ error ex
+ end
+ end
+ end
+end
+
diff --git a/app/lib/web/all.rb b/app/lib/web/all.rb
new file mode 100644
index 0000000..535e8b7
--- /dev/null
+++ b/app/lib/web/all.rb
@@ -0,0 +1,2 @@
+require 'lib/web/hacker'
+require 'lib/web/web'
diff --git a/app/lib/web/hacker.rb b/app/lib/web/hacker.rb
new file mode 100644
index 0000000..f74350f
--- /dev/null
+++ b/app/lib/web/hacker.rb
@@ -0,0 +1,37 @@
+# website integration
+
+require 'lib/web/yaml'
+
+def Hacker name
+ Hacker.new name
+end
+
+class Hacker
+ include HH::YAML
+
+ attr :name
+ attr :password
+
+ def initialize(who)
+ @name = who[:username]
+ @password = who[:password]
+ end
+
+ def inspect
+ "(Hacker #{@name})"
+ end
+
+ def channel(title)
+ Channel.new(@name, title)
+ end
+
+ def program_list &blk
+ http('GET', "/programs/#{@name}.json", :username => @name, :password => @password, &blk)
+ end
+
+ def save_program_to_the_cloud name, code
+ url = "/programs/#{@name}/#{name}.json"
+ http('PUT', url, {:creator_username => @name, :title => name, :code => code, :username => @name, :password => @password}) {|u| true }
+ end
+
+end
diff --git a/app/lib/web/web.rb b/app/lib/web/web.rb
new file mode 100644
index 0000000..38c148a
--- /dev/null
+++ b/app/lib/web/web.rb
@@ -0,0 +1,318 @@
+#
+# = Web =
+#
+# feeds and searches.
+#
+module Web
+ JSON_MIME_TYPES = ["application/x-javascript", "application/x-json", "application/json"]
+ XML_MIME_TYPES = ["application/rdf+xml", "application/rss+xml", "application/atom+xml", "application/xml", "text/xml"]
+ [JSON_MIME_TYPES, XML_MIME_TYPES].each do |ary|
+ ary.map! { |str| /^#{Regexp::quote(str)}/ }
+ end
+end
+
+module Hpricot
+ class Doc
+ def widget(slot)
+ ary = [:para, nil, []]
+ children.each { |c| c.build_list(ary) }
+ 0.step(ary.length - 1, 3) do |i|
+ case ary[i]
+ when :image
+ slot.send(ary[i], *ary[i+1])
+ else
+ unless ary[i+2].find_all { |x| !x.is_a?(String) or (x.gsub!(/^[\n\t]+|[\n\t]+$/, ''); x =~ /\S/) }.empty?
+ slot.send(ary[i], ary[i+2], ary[i+1])
+ end
+ end
+ end
+ end
+ def to_s
+ (self/"//*/text()").join(" ").gsub(/\n+^Z/, '')
+ end
+ def length
+ to_s.length
+ end
+ end
+ module Traverse
+ def build_list(ary, top = ary, inside = false) end
+ end
+ class Elem
+ def build_list(ary, top = ary, inside = false)
+ key, opts, text, block = nil, nil, nil, nil
+ case name
+ when "a"; key, opts = :link, {:click => self['href']}
+ when "b", "strong"; key = :strong
+ when "i", "em"; key = :em
+ when "sup"; key = :sup
+ when "sub"; key = :sub
+ when "br"; text = "\n"
+ when "img"; block, opts = :image, [self['src']]
+ when "p"; block = :para
+ when "blockquote"; block, opts = :para, {:margin => 20}
+ when "li"; block, opts = :para, {:margin => 10}
+ end
+
+ if key
+ ary2 = [key, opts, []]
+ children.each { |c| c.build_list(ary2, top, true) }
+ unless ary2.last.empty?
+ unless ary2.last.find_all { |x| !x.is_a?(String) or (x.gsub!(/^\n+|\n+$/, ''); x =~ /\S/) }.empty?
+ ele = HH::APP.send(ary2[0], ary2[2], ary2[1])
+ ary.last << ele
+ end
+ end
+ elsif text
+ ary.last.last << text if ary.last.last.is_a? String
+ elsif block == :image
+ if ary[0] == :link
+ opts << {:click => ary[1]}
+ end
+ top[-3,0] = [:image, opts, nil]
+ elsif block
+ if !inside
+ ary << block << opts << []
+ end
+ children.each { |c| c.build_list(ary, top, true) }
+ else
+ children.each { |c| c.build_list(ary, top, inside) }
+ end
+ ary
+ end
+ end
+ class Text
+ def build_list(ary, top = nil, inside = false)
+ ary = ary.last
+ txt = self.inner_text
+ txt.gsub!(/\r\n/, "\n")
+ txt.gsub!(/\n+/, "\n")
+ if ary.last.is_a? String
+ ary.last << txt
+ elsif txt =~ /\S/
+ ary << txt
+ end
+ ary
+ end
+ end
+end
+
+class Feed
+ attr_accessor :title, :link, :description, :items
+ def initialize(t, l, d, i)
+ @title, @link, @description, @items = t, l, d, i
+ end
+ def widget(slot)
+ slot.stack(:margin => 18).tap do |s|
+ s.inscription "Feed from #{self.link}"
+ s.title self.title
+ s.para self.description if self.description
+ items.each do |item|
+ item.widget(s)
+ end
+ end
+ end
+ def to_s
+ res = "Feed from #{link}\n"
+ res << "== #{title} ==\n"
+ res << "#{description}" if description
+ res << " (#{items.size} items)\n"
+ end
+ def each(&blk)
+ items.each(&blk)
+ end
+ def self.parse(data)
+ doc = Hpricot.XML(data)
+ if doc.at("/rss, /feed, rdf:rdf, rdf:RDF")
+ Feed.load(doc)
+ elsif link = doc.at("link[@type='application/atom+xml'], link[@type='application/rss+xml']")
+ URI(link['href'])
+ else
+ doc
+ end
+ end
+ def self.load(doc)
+ title = (doc/"feed/title, channel/title").inner_text
+ link = doc.at("feed/link, channel/link")
+ link = link['href'] || link.inner_text
+ description = (doc/"feed/tagline, channel/description").inner_text
+ items = []
+ (doc/"feed/entry, item").each do |item|
+ ilink = item.at("/link")
+ desc = item.at("content:encoded, content, description")
+ if desc.to_s =~ /<\w+( |>)/n
+ desc = Hpricot(desc.inner_text)
+ end
+ items << Feed::Item.new((item/"/title").inner_text,
+ ilink['href'] || ilink.inner_text,
+ desc)
+ end
+ self.new(title, link, description, items)
+ end
+end
+
+class Feed::Item
+ attr_accessor :title, :link, :description
+ def to_s; "(Feed::Item)" end
+ def initialize(t, l, d)
+ @title, @link, @description = t, l, d
+ end
+ def widget(slot)
+ slot.stack.tap do |s|
+ s.para s.link(self.title, :click => self.link, :size => 18, :stroke => "#777"),
+ " Feed::Item", :stroke => "#999"
+ if self.description.respond_to? :widget
+ self.description.widget(s)
+ else
+ s.para self.description
+ end
+ end
+ end
+ def to_s
+ res = "== #{title} ==\n"
+ res << "#{description}\n"
+ end
+end
+
+# downloads the file at URI to filename showing the progress in a window
+# if filename is a relative path, the file will be saved to the Downloads
+# directory of HH, by default the basename of the URI is used
+def Web.download uri, filename=nil, &blk
+ filename ||= File.basename(uri)
+ filename = File.expand_path(filename, "#{HH::USER}/Downloads/")
+ opts = {:save => filename }
+
+ Web.dowload_dialog uri, opts, &blk
+end
+
+def Web.dowload_dialog uri, opts = {}, &blk
+ window :width => 450, :height => 100, :margin => 10, :title => "Download" do
+ # method to close the window
+ def self.finished
+ timer 1 do
+ close
+ end
+ end
+
+ status = para "Downloading #{uri}"
+ p = progress :width => 1.0
+
+ opts[:start] = proc{|dl| status.text = 'Connecting'}
+ opts[:progress] = proc do |dl|
+ status.text = "Transferred #{dl.transferred} of #{dl.length} bytes (#{dl.percent}%)"
+ p.fraction = dl.percent * 0.01
+ end
+
+ opts[:finish] = proc do |dl|
+ status.text = 'Download finished'
+ finished
+ blk.call(dl) if blk
+ end
+ opts[:error] = proc{|dl, err| status.text = "Error: #{err}"; finished}
+ HH::APP.download uri, opts
+ end
+end
+
+def Web.fetch(uri, opts = {}, &blk)
+ Web.dowload_dialog uri, opts do |dl|
+ data = dl.response.body
+ unless opts[:as]
+ opts[:as] =
+ case dl.response.headers['Content-Type']
+ when *Web::JSON_MIME_TYPES; JSON
+ when *Web::XML_MIME_TYPES; Feed
+ end
+ end
+ if opts[:as]
+ if opts[:as].respond_to? :parse
+ obj = opts[:as].parse(data)
+ if obj.is_a? URI
+ Web.fetch(obj, opts, &blk)
+ else
+ blk[obj]
+ end
+ elsif opts[:as] == String
+ blk[data]
+ else
+ raise ArgumentError, "Web.fetch can't load into the #{opts[:as]} class"
+ end
+ else
+ blk[data]
+ end
+ end
+end
+
+def Web.delicious(search, opts = {})
+ search = search.try(:join, " ")
+ opts[:limit] ||= 10
+ url = "setcount=#{opts[:limit]}"
+ search = "\"#{search}\"" if opts[:exact]
+ if opts[:page].to_i > 1
+ url += "&page=#{opts[:page]}"
+ end
+
+ HH::APP.download("http://del.icio.us/search?p=#{URI.escape(search)}&#{url}") do |doc|
+ dls = Hpricot(doc.response.body)
+ list = (dls/"div.data").map do |ele|
+ link, meta = ele.at("h4 a"), ele.at(".delNavCount")
+ Feed::Item.new(link.inner_text, link['href'], "saved by #{meta.inner_text} people")
+ end
+ yield Feed.new("del.icio.us", "http://del.icio.us/",
+ "Delicious search for #{search}", list)
+ end
+end
+
+def Web.flickr(search, opts = {})
+ search = search.try(:join, " ").split(/\s+/).join(",")
+ HH::APP.download("http://api.flickr.com/services/feeds/photos_public.gne?tags=#{URI.escape(search)}") do |doc|
+ yield Feed.load(Hpricot(doc.response.body))
+ end
+end
+
+def Web.google(search, opts = {})
+ search = search.try(:join, " ")
+ opts[:limit] ||= 10
+ url = "num=#{opts[:limit]+3}"
+ search = "\"#{search}\"" if opts[:exact]
+ if opts[:page].to_i > 1
+ url += "&start=#{opts[:limit] * (opts[:page].to_i - 1)}"
+ end
+ if opts[:site]
+ if opts[:site].respond_to?(:join)
+ search += "( site:#{opts[:site].join(' | site:')} )"
+ else
+ search += " site:#{opts[:site]}"
+ end
+ end
+
+ HH::APP.download("http://www.google.com/search?q=#{URI.escape(search)}&#{url}") do |doc|
+ ggl = Hpricot(doc.response.body)
+ list = (ggl/".g")[0,opts[:limit]].map do |ele|
+ link = ele.at("a")
+ Feed::Item.new(link.inner_text, link['href'], (ele/(".s:first".."br")).inner_text)
+ end
+ yield Feed.new("Google", "http://google.com/",
+ "Google search for: #{search}", list)
+ end
+end
+
+def Web.yahoo(search, opts = {})
+ search = search.try(:join, " ")
+ opts[:limit] ||= 10
+ url = "n=#{opts[:limit]}"
+ search = "\"#{search}\"" if opts[:exact]
+ if opts[:page].to_i > 1
+ url += "&b=#{opts[:limit] * (opts[:page].to_i - 1)}"
+ end
+ url += "&vs=#{URI.escape([*opts[:site]].join(" | "))}" if opts[:site]
+
+ HH::APP.download("http://search.yahoo.com/search?p=#{URI.escape(search)}&#{url}") do |doc|
+ yahoo = Hpricot(doc.response.body)
+ list = (yahoo/"div#web li").map do |ele|
+ link = ele.at(".yschttl")
+ next unless link
+ Feed::Item.new(link.inner_text, link['href'], (ele/".abstr, .sm-abs").inner_text)
+ end.compact
+ yield Feed.new("Yahoo!", "http://yahoo.com/",
+ "Yahoo! search for: #{search}", list)
+ end
+end
diff --git a/app/lib/web/yaml.rb b/app/lib/web/yaml.rb
new file mode 100644
index 0000000..d389447
--- /dev/null
+++ b/app/lib/web/yaml.rb
@@ -0,0 +1,60 @@
+require 'yaml'
+
+class FetchError < StandardError; end
+class SharedAlreadyError < StandardError; end
+
+module HH::YAML
+ def http(meth, path, params = nil, &blk)
+ url = HH::REST + path.to_s
+ body, headers = nil, {'Accept' => 'text/yaml'}
+ case params
+ when String
+ body = params
+ when Hash
+ if params[:who]
+ headers['X-Who'] = params.delete(:who)
+ end
+
+ if params[:post]
+ body = params[:post]
+ else
+ x = qs(params)
+ if meth == 'GET'
+ url += "?" + x
+ else
+ body = x
+ headers['Content-Type'] = 'application/x-www-form-urlencoded'
+ end
+ end
+ end
+
+ # if HH::PREFS['username']
+ # req.basic_auth HH::PREFS['username'], HH::PREFS['pass']
+ # end
+ headers['Authorization'] = 'Basic ' + ["#{HH::PREFS['username']}:#{HH::PREFS['password']}"].pack("m").strip
+ HH::APP.download url, :method => meth, :body => body, :headers => headers do |dl|
+ blk[YAML.load(dl.response.body)] if blk
+ end
+ end
+
+ def escape(string)
+ string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
+ '%' + $1.unpack('H2' * $1.size).join('%').upcase
+ end.tr(' ', '+')
+ end
+
+ def qs(hsh, prefix = [])
+ hsh.map do |k, v|
+ ary = prefix + [k]
+ case v
+ when Hash
+ qs(v, ary)
+ else
+ ok = escape(ary.first) +
+ ary[1..-1].map { |x| "[#{escape(x)}]" }.join
+ "#{ok}=#{escape(v)}"
+ end
+ end.join("&")
+ end
+end
+
diff --git a/app/platform/mac/App.icns b/app/platform/mac/App.icns
new file mode 100644
index 0000000..83c98ec
--- /dev/null
+++ b/app/platform/mac/App.icns
Binary files differ
diff --git a/app/platform/mac/Cheat.icns b/app/platform/mac/Cheat.icns
new file mode 100644
index 0000000..0b3319b
--- /dev/null
+++ b/app/platform/mac/Cheat.icns
Binary files differ
diff --git a/app/platform/mac/Help.icns b/app/platform/mac/Help.icns
new file mode 100644
index 0000000..2c1e3dc
--- /dev/null
+++ b/app/platform/mac/Help.icns
Binary files differ
diff --git a/app/platform/mac/dmg_ds_store b/app/platform/mac/dmg_ds_store
new file mode 100644
index 0000000..36e1c56
--- /dev/null
+++ b/app/platform/mac/dmg_ds_store
Binary files differ
diff --git a/app/platform/msw/App.ico b/app/platform/msw/App.ico
new file mode 100755
index 0000000..a4d43f6
--- /dev/null
+++ b/app/platform/msw/App.ico
Binary files differ
diff --git a/app/platform/msw/Cheat.ico b/app/platform/msw/Cheat.ico
new file mode 100755
index 0000000..8375ac7
--- /dev/null
+++ b/app/platform/msw/Cheat.ico
Binary files differ
diff --git a/app/platform/msw/Help.ico b/app/platform/msw/Help.ico
new file mode 100755
index 0000000..009d2ef
--- /dev/null
+++ b/app/platform/msw/Help.ico
Binary files differ
diff --git a/app/platform/nix/app.png b/app/platform/nix/app.png
new file mode 100644
index 0000000..617aa3b
--- /dev/null
+++ b/app/platform/nix/app.png
Binary files differ
diff --git a/app/root/Home/.gitignore b/app/root/Home/.gitignore
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/root/Home/.gitignore
diff --git a/app/root/comics.txt b/app/root/comics.txt
new file mode 100755
index 0000000..36d31b8
--- /dev/null
+++ b/app/root/comics.txt
@@ -0,0 +1,4 @@
+Achewood: http://achewood.com/
+Dinosaur Comics: http://qwantz.com/
+Perry Bible Fellowship: http://cheston.com/pbf/archive.html
+Get Your War On: http://mnftiu.cc/
diff --git a/app/spec/all.rb b/app/spec/all.rb
new file mode 100755
index 0000000..f08b73b
--- /dev/null
+++ b/app/spec/all.rb
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+require 'spec/enhancements'
+require 'spec/events'
+require 'spec/stdout' \ No newline at end of file
diff --git a/app/spec/enhancements.rb b/app/spec/enhancements.rb
new file mode 100755
index 0000000..8d7b533
--- /dev/null
+++ b/app/spec/enhancements.rb
@@ -0,0 +1,452 @@
+#!/usr/bin/env ruby
+
+require 'lib/enhancements'
+
+require 'spec/autorun'
+
+describe Object, "blank?" do
+ it "should return true for empty strings" do
+ "".blank?.should == true
+ end
+
+ it "should return false for non empty strings" do
+ "a".blank?.should == false
+ ".".blank?.should == false
+ "dfasfa".blank?.should == false
+ "[]".blank?.should == false
+ "0".blank?.should == false
+ end
+
+ it "should return false for strings composed of just spaces" do
+ " ".blank?.should == false
+ " ".blank?.should == false
+ "\t".blank?.should == false
+ "\n".blank?.should == false
+ end
+
+ it "should return true for empty arrays" do
+ [].blank?.should == true
+ end
+
+ it "should return false for non empty arrays" do
+ [1, 2, 3].blank?.should == false
+ [""].blank?.should == false
+ [0].blank?.should == false
+ [nil].blank?.should == false
+ end
+
+ it "should return true for zero" do
+ 0.blank?.should == true
+ 0.0.blank?.should == true
+ Rational(0).blank?.should == true
+ end
+
+ it "should return false for non zero numbers" do
+ 1.blank?.should == false
+ 1.2.blank?.should == false
+ 0.0001.blank?.should == false
+ (Rational(1)/100).blank?.should == false
+ nan = 0.0/0.0
+ nan.blank?.should == false
+ inf = 1.0/0.0
+ inf.blank?.should == false
+ end
+
+ it "should return true for nil and false" do
+ nil.blank?.should == true
+ false.blank?.should == true
+ end
+
+ it "should return true for an object with empty? returning true" do
+ a = Object.new
+ a.blank?.should == false
+ def a.empty?; true end
+ a.blank?.should == true
+ end
+
+ it "should return true for an object with no empty? " +
+ "and zero? returning true" do
+ a = Object.new
+ a.blank?.should == false
+ def a.zero?; true end
+ a.blank?.should == true
+ end
+
+ # I didn't add the use case with empty? returning false and zero returning
+ # true, as I'm not sure the current implementation is the best way to go
+ # I will leave it undefined
+end
+
+
+
+
+describe Object, "#tap" do
+ it "should return self" do
+ obj = Object.new
+ obj.tap{}.should == obj
+ end
+
+ it "should yield self" do
+ obj = Object.new
+ obj.tap do |x|
+ x.should == obj
+ end
+ end
+end
+
+describe Object, "#try" do
+ it "should do nothing and return self on an inexistend method" do
+ obj = Object.new
+ obj.try(:inexistend, 1, 2).should == obj
+ end
+
+ context "with no arguments" do it "should call the method if it exists" do
+ obj = "123"
+ obj.try :reverse!
+ obj.should == "321"
+ end end
+
+ context "with more arguments" do it "should call the method if it exists" do
+ obj = "123"
+ obj.try :delete!, "2"
+ obj.should == "13"
+ end end
+
+ it "should return the result ot the method if it exists" do
+ obj = "123"
+ obj.freeze
+ obj.try(:delete, "2").should == "13"
+ obj.should == "123"
+ end
+end
+
+
+
+
+describe String, "#ends?" do
+ it "should return true if it starts with the given string" do
+ "hello".starts?("he").should == true
+ "hellohello".starts?("hello").should == true
+ end
+
+ it "should always return true if the given string is empty" do
+ "hello".starts?("").should == true
+ "".starts?("").should == true
+ end
+
+ it "should return false if it does not start with the given string" do
+ "hello".starts?("ello").should == false
+ " hello hello".starts?("hello").should == false
+ "hello".starts?("e").should == false
+ "hello".starts?("o").should == false
+ "".starts?("hello").should == false
+ "".starts?(" ").should == false
+ end
+end
+
+describe String, "#ends?" do
+ it "should return true if it starts with the given string" do
+ "hello".ends?("lo").should == true
+ "hellohello".ends?("hello").should == true
+ end
+
+ it "should always return true if the given string is empty" do
+ "hello".ends?("").should == true
+ "".ends?("").should == true
+ end
+
+ it "should return false if it does not start with the given string" do
+ "hello".ends?("hell").should == false
+ "hello hello ".ends?("hello").should == false
+ "hello".ends?("e").should == false
+ "hello".ends?("l").should == false
+ "".ends?("hello").should == false
+ "".ends?(" ").should == false
+ end
+end
+
+describe String, "#remove" do
+ it "should remove the substring equal to the argument" do
+ "hello".remove("el").should == "hlo"
+ "hellohello".remove("ell").should == "hohello"
+ "hello".remove("l").should == "helo"
+ end
+
+ it "should rase an exception if it doesn't contain the substring" do
+ lambda{"hello".remove("ello ")}.should raise_error
+ end
+
+ it "should not change the string" do
+ str = "hello"
+ str.remove "l"
+ str.should == "hello"
+ str.remove("he")
+ str.should == "hello"
+ end
+end
+
+describe String, "#to_slug" do
+ it "should contain no characters other than lowercase alphanumeric and _" do
+ # create a random string containing also a a lot of noise
+ random_string = ""
+ 1000.times do
+ random_string << rand(256).chr
+ end
+ random_string.to_slug.should =~ /^[a-z0-9_]*$/
+ end
+
+ it "should only return lowercase characters" do
+ "heLlO".to_slug.should == "hello"
+ end
+
+ it "should only return alphanumeric characters" do
+ "hello,world1!!!".to_slug.should == "helloworld1"
+ end
+
+ it "should transform whitespace to _" do
+ "a a a \n".to_slug.should == "a_a__a__"
+ end
+
+ it "should combine all the above correctly" do
+ "Hello, World1!!!".to_slug.should == "hello_world1"
+ end
+end
+
+
+describe String, "#rot13" do
+ it "should translitarate all alphabeti ascii characters correctly" do
+ str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ str.freeze
+ str.rot13.should == "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
+ end
+
+ it "should not change non alphabetic characters" do
+ str = "1 + 2i"
+ str.freeze
+ str.rot13.should == "1 + 2v"
+ end
+end
+
+describe String, "#rot13!" do
+ it "should translitarate all alphabeti ascii characters correctly" do
+ str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ str.rot13!
+ str.should == "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
+ end
+
+ it "should not change non alphabetic characters" do
+ str = "1 + 2i"
+ str.rot13!
+ str.should == "1 + 2v"
+ end
+
+ it "should return the transliterated string" do
+ str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ str.rot13!.should == "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
+ end
+end
+
+
+
+
+
+describe Fixnum, "#ordinalize" do
+ it "should return custom order for 1, 2, 3" do
+ 1.ordinalize.should == "1st"
+ 2.ordinalize.should == "2nd"
+ 3.ordinalize.should == "3rd"
+ 4.ordinalize.should == "4th"
+ 102.ordinalize.should == "102th"
+ end
+
+ it "should return the correct order for numbers > 3" do
+ 4.ordinalize.should == "4th"
+ 102.ordinalize.should == "102th"
+ end
+end
+
+describe Fixnum, "#weeks" do
+ it "should return the number of second in a self week" do
+ 1.weeks.should == 604800
+ 0.weeks.should == 0
+ 100.weeks.should == 60480000
+ end
+end
+
+
+require 'time'
+describe Time, "#calendar" do
+ it "should return an easy readable string of the date" do
+ Time.local(2010, 8, 27).calendar.should == "August 27th, 2010"
+ Time.local(2001, 1, 1).calendar.should == "January 1st, 2001"
+ end
+end
+
+describe Time, "#calendar_with_time" do
+ it "should return an easy readable string of the date and time" do
+ Time.local(2010, 8, 27, 15, 28).calendar_with_time.
+ should == "August 27th, 2010 at 3:28 PM"
+ Time.local(2001, 1, 1, 0, 1).calendar_with_time.
+ should == "January 1st, 2001 at 12:01 AM"
+ end
+end
+
+describe Time, "#time_only" do
+ it "should return an easy readable string of the time" do
+ Time.local(2010, 1, 1, 0, 0).time_only.should == "12:00 AM"
+ Time.local(2010, 1, 1, 23, 59).time_only.should == "11:59 PM"
+ Time.local(2010, 1, 1, 13, 1).time_only.should == "1:01 PM"
+ end
+end
+
+describe Time, "#quick" do
+ it "should return a readable short string of the date and time" do
+ Time.local(2010, 8, 27, 15, 28).quick.should == "Aug 27, 2010 at 3:28pm"
+ Time.local(2001, 1, 1, 0, 1).quick.should == "Jan 1, 2001 at 12:01am"
+ end
+end
+
+describe Time, "#short" do
+ it "should return a short string of the date and time" do
+ Time.local(2010, 8, 27, 15, 28).short.should == "Aug 27"
+ Time.local(2001, 1, 1, 0, 1).short.should == "Jan 1"
+ end
+end
+
+describe Time, "#full" do
+ it "should return a complete string of the date and time" do
+ Time.local(2010, 8, 27, 15, 28).full.should == "2010-08-27 15:28:00"
+ Time.local(2001, 1, 1, 0, 1, 1).full.should == "2001-01-01 00:01:01"
+ end
+end
+
+describe Time, "#since" do
+ def now
+ Time.local(2010, 8, 27, 15, 28, 18)
+ end
+
+ it "should return friendly formatting for times less than a minute ago" do
+ # zero seconds
+ Time.local(2010, 8, 27, 15, 28, 18).since(now).should == "less than a minute"
+ # one second
+ Time.local(2010, 8, 27, 15, 28, 17).since(now).should == "less than a minute"
+ # 40 seconds
+ Time.local(2010, 8, 27, 15, 27, 38).since(now).should == "less than a minute"
+ # 58 seconds
+ Time.local(2010, 8, 27, 15, 27, 20).since(now).should == "1 minute"
+ end
+
+ context "with include_seconds" do
+ it "should return friendly formatting for times less than a minute ago" do
+ # zero seconds
+ Time.local(2010, 8, 27, 15, 28, 18).since(now, true).should == "less than 5 seconds"
+ # 4 second
+ Time.local(2010, 8, 27, 15, 28, 14).since(now, true).should == "less than 5 seconds"
+ # 5 second
+ Time.local(2010, 8, 27, 15, 28, 13).since(now, true).should == "less than 10 seconds"
+ # 9 second
+ Time.local(2010, 8, 27, 15, 28, 9).since(now, true).should == "less than 10 seconds"
+ # 10 second
+ Time.local(2010, 8, 27, 15, 28, 8).since(now, true).should == "less than 20 seconds"
+ # 19 second
+ Time.local(2010, 8, 27, 15, 27, 59).since(now, true).should == "less than 20 seconds"
+ # 20 second
+ Time.local(2010, 8, 27, 15, 27, 58).since(now, true).should == "half a minute"
+ # 39 seconds
+ Time.local(2010, 8, 27, 15, 27, 39).since(now, true).should == "half a minute"
+ # 40 seconds
+ Time.local(2010, 8, 27, 15, 27, 38).since(now, true).should == "less than a minute"
+ # 58 seconds
+ Time.local(2010, 8, 27, 15, 27, 20).since(now, true).should == "less than a minute"
+ # one minute
+ Time.local(2010, 8, 27, 15, 27, 18).since(now, true).should == "1 minute"
+ end
+ end
+
+ it "should return friendly formatting for times less than an hour ago" do
+ # one minute
+ Time.local(2010, 8, 27, 15, 27, 18).since(now).should == "1 minute"
+ # one minute and one second
+ Time.local(2010, 8, 27, 15, 27, 17).since(now).should == "1 minute"
+ # about 3 minutes
+ Time.local(2010, 8, 27, 15, 25, 22).since(now).should == "3 minutes"
+ Time.local(2010, 8, 27, 15, 25, 16).since(now).should == "3 minutes"
+ # about 45 minutes
+ Time.local(2010, 8, 27, 14, 43, 22).since(now).should == "45 minutes"
+ Time.local(2010, 8, 27, 14, 43, 16).since(now).should == "45 minutes"
+ end
+
+ it "should return friendly formatting for times less than a day ago" do
+ # about 46 minutes
+ Time.local(2010, 8, 27, 14, 42, 22).since(now).should == "about 1 hour"
+ Time.local(2010, 8, 27, 14, 42, 16).since(now).should == "about 1 hour"
+ # about 90 minutes
+ Time.local(2010, 8, 27, 13, 58, 22).since(now).should == "about 1 hour"
+ Time.local(2010, 8, 27, 13, 58, 16).since(now).should == "about 1 hour"
+ # about 91 minutes
+ Time.local(2010, 8, 27, 13, 57, 22).since(now).should == "about 2 hours"
+ # 24 hours
+ Time.local(2010, 8, 26, 15, 28, 18).since(now).should == "about 24 hours"
+ end
+
+ it "should return friendly formatting for times less than a year ago" do
+ # about 24 hours and one minute
+ Time.local(2010, 8, 26, 15, 27, 18).since(now).should == "1 day"
+ # about 1 day and 12 hours
+ Time.local(2010, 8, 26, 3, 28, 18).since(now).should == "1 day"
+ # almost 2 days
+ Time.local(2010, 8, 25, 15, 29, 18).since(now).should == "1 day"
+ # 2 days
+ Time.local(2010, 8, 25, 15, 28, 18).since(now).should == "2 days"
+ # 26 days
+ Time.local(2010, 8, 1, 15, 28, 18).since(now).should == "26 days"
+ # 1 day less than a day
+ Time.local(2009, 8, 28, 15, 28, 18).since(now).should == "364 days"
+ end
+
+ it "should return friendly formatting for times more than a year" do
+ Time.local(2009, 8, 27, 15, 28, 18).since(now).should == "1 years"
+ # one day less then 2 years
+ Time.local(2008, 8, 28, 15, 28, 18).since(now).should == "1 years"
+ # two years
+ Time.local(2008, 8, 27, 15, 28, 18).since(now).should == "2 years"
+ end
+
+ it "should use Time.now by default" do
+ now_time = Time.now
+ Time.local(2010, 8, 25, 15, 29, 18).since(now_time).should ==
+ Time.local(2010, 8, 25, 15, 29, 18).since
+ end
+end
+
+
+describe Thread, "#new" do
+ it "should execute the block argument" do
+ block_called = false
+ t = Thread.new do
+ block_called = true
+ end
+ t.join
+ block_called.should == true
+ end
+
+ it "should start a new thread" do
+ another_thread = false
+ topmost = Thread.current
+ t = Thread.new do
+ another_thread = true if Thread.current != topmost
+ end
+ t.join
+ another_thread.should == true
+ end
+
+ it "should pass the arguments to the block" do
+ Thread.new :arg1, 123 do |arg1, arg2|
+ arg1.should == :arg1
+ arg2.should == 123
+ end
+ end
+
+ # TODO: needs shoes
+ #it "should call error on exception"
+end
diff --git a/app/spec/events.rb b/app/spec/events.rb
new file mode 100755
index 0000000..89420a1
--- /dev/null
+++ b/app/spec/events.rb
@@ -0,0 +1,217 @@
+#!/usr/bin/env ruby
+
+require 'lib/dev/events'
+
+require 'spec/autorun'
+
+
+describe HH::EventConnection, "#event" do
+ it "should return the correct value" do
+ ec = HH::EventConnection.new(:my_event, :any)
+ ec.event.should == :my_event
+ end
+end
+
+describe HH::EventConnection, "#try" do
+ # auxiliary methods calls #try with arguments +args+ on a connection
+ # with condition +conds+
+ # it returns :successful on :unsuccessful
+ def try(conds, args)
+ result = :unsuccessful
+ conn = HH::EventConnection.new(:my_event, conds) do
+ result = :successful
+ end
+ conn.try args
+ result
+ end
+
+ it "should not succeed with condition of wrong size" do
+ try([], [1]).should == :unsuccessful
+ try([1], []).should == :unsuccessful
+ try([1, 1], [1]).should == :unsuccessful
+ try([1], [1, 1]).should == :unsuccessful
+ end
+
+ it "should succeed with no conditions" do
+ try([], []).should == :successful
+ end
+
+ it "should succeed with one correct condition" do
+ try([String], ["str"]).should == :successful
+ try([nil], [nil]).should == :successful
+ try([/^\d$/], ["4"]).should == :successful
+ end
+
+ it "should succeed with the :any condition" do
+ try([:any], [[1,2,3]]).should == :successful
+ end
+
+ it "should not succeed with one wrong condition" do
+ try([String], [4]).should == :unsuccessful
+ try([nil], [false]).should == :unsuccessful
+ try([/^\d$/], ["44"]).should == :unsuccessful
+ try([[1,2,3]], [:any]).should == :unsuccessful
+ end
+
+ it "should succeed with multiple correct conditions" do
+ cond = [String, nil, /^\d$/, :any]
+ args = ["str", nil, "4", [1, 2, 3]]
+ try(cond, args).should == :successful
+ end
+
+ it "should not succeed if at least one condition is wrong" do
+ cond = [Numeric, nil, /^\d$/, :any]
+ args = ["str", nil, "4", [1, 2, 3]]
+ try(cond, args).should == :unsuccessful
+ cond = [String, nil, /^\d$/, :wrong]
+ try(cond, args).should == :unsuccessful
+ end
+
+ context "using hash arguments" do
+ def try cond, args
+ super [cond], [args]
+ end
+
+ it "should succeed with no conditions" do
+ try({}, {}).should == :successful
+ try({}, {:a => 1, :b => 2}).should == :successful
+ end
+
+ it "should not succeed if the argument isn't an hash" do
+ no_hash = []
+ cond = {:a => nil, :something => 1234}
+ try(cond, no_hash).should == :unsuccessful
+ try({}, no_hash).should == :unsuccessful
+ end
+
+ it "should succeed with one correct condition" do
+ try({:first => String}, {:first => "str"}).should == :successful
+ try({:a => nil}, {:a => nil}).should == :successful
+ try({:str => /^\d$/}, {:str => "4"}).should == :successful
+ end
+
+ it "should not succeed with one wrong condition" do
+ try({:first => String}, {:first => 5}).should == :unsuccessful
+ try({:a => nil}, {:a => ""}).should == :unsuccessful
+ try({:str => /^\d$/}, {:str => "44"}).should == :unsuccessful
+ end
+
+ it "should succeed with multiple correct conditions" do
+ cond = {:a => String, :b => nil, :c => /^\d$/}
+ args = {:a => "str", :b => nil, :c => "4", :d => [1, 2, 3]}
+ try(cond, args).should == :successful
+ end
+
+ it "should not succeed with at least one wrong condition" do
+ cond = {:a => Numeric, :b => nil, :c => /^\d$/}
+ args = {:a => "str", :b => nil, :c => "4", :d => [1, 2, 3]}
+ try(cond, args).should == :unsuccessful
+ cond = {:a => String, :b => nil, :c => /^\d$/, :wrong => Array}
+ try(cond, args).should == :unsuccessful
+ end
+ end
+end
+
+
+
+
+describe HH::Observable, "#emit and #on_event" do
+ it "should work when there are no connections for an event" do
+ obj = Object.new
+ obj.extend HH::Observable
+ obj.emit :my_event, "arg1", :arg2
+ obj.emit :another_event, {:arg1 => "str", :arg2 => :sym}
+ end
+
+ it "should call try on all and only the correct connections" do
+ obj = Object.new
+ obj.extend HH::Observable
+
+ conn1_called = conn2_called = conn3_called = 0
+ obj.on_event :event1 do
+ conn1_called += 1
+ end
+ obj.on_event :event1, String do
+ conn2_called += 1
+ end
+ obj.on_event :event2 do
+ conne3_called += 1
+ end
+ obj.emit :event1 # conn1
+ obj.emit :event1 # conn1
+ obj.emit :event1 # conn1
+ obj.emit :event1, 123 # no connection
+ obj.emit :event1, 123 # no connection
+ obj.emit :event1, "str" # conn 2
+ conn1_called.should == 3
+ conn2_called.should == 1
+ conn3_called.should == 0
+ end
+end
+
+describe HH::Observable, "#delete_event_connection" do
+ it "should delete the event connection" do
+ obj = Object.new
+ obj.extend HH::Observable
+
+ conn1_called = conn2_called = conn3_called = 0
+ obj.on_event :event1 do
+ conn1_called += 1
+ end
+ conn2 = obj.on_event :event1, String do
+ conn2_called += 1
+ end
+ obj.on_event :event2 do
+ conne3_called += 1
+ end
+ obj.delete_event_connection conn2
+ obj.emit :event1 # conn1
+ obj.emit :event1 # conn1
+ obj.emit :event1 # conn1
+ obj.emit :event1, 123 # no connection
+ obj.emit :event1, 123 # no connection
+ obj.emit :event1, "str" # conn 2
+ conn1_called.should == 3
+ conn2_called.should == 0 # never called because deleted
+ conn3_called.should == 0
+ end
+end
+
+###### the following tests test the implementation so may be changed #####
+#describe HH::EventConnection, "::match_hash?" do
+# def call cond, hash
+# HH::EventConnection.match_hash? cond, hash
+# end
+#
+# it "should return true with an empty condition" do
+# empty_cond = {}
+# call(empty_cond, {}).should == true
+# call(empty_cond, {:something => 123, [] => nil}).should == true
+# end
+#
+# it "should return true with one correct condition" do
+# call({:first => String}, {:first => "str"}).should == true
+# call({:a => nil}, {:a => nil}).should == true
+# call({:str => /^\d$/}, {:str => "4"}).should == true
+# end
+#
+# it "should return false with one wrong condition" do
+# call({:first => String}, {:first => 5}).should == false
+# call({:a => nil}, {:a => ""}).should == false
+# call({:str => /^\d$/}, {:str => "44"}).should == false
+# end
+#
+# it "should return false if the argument isn't a hash" do
+# no_hash = []
+# cond = {nil => nil, :something => 1234}
+# call(cond, no_hash).should == false
+# call({}, no_hash).should == false
+# end
+#
+# it "should raise an ArgumentError if the condition isn't a hash" do
+# cond = [] # no hash
+# lambda {call(cond, [])}.should raise_error(ArgumentError)
+# lambda {call(cond, :something)}.should raise_error(ArgumentError)
+# end
+#end
+
diff --git a/app/spec/rspec.rb b/app/spec/rspec.rb
new file mode 100755
index 0000000..0f4e9d0
--- /dev/null
+++ b/app/spec/rspec.rb
@@ -0,0 +1,30 @@
+# a test trying to get rspec to run with Shoes
+
+Shoes.setup do
+ gem 'rspec'
+end
+
+require 'spec/autorun'
+
+describe String, "#reverse" do
+ it "should reverse the string" do
+ "abcd".reverse.should == "dcba"
+ end
+end
+
+# test to look if rspec with shoes is working
+#describe Shoes::App, "#style" do
+# it "should have correct default values" do
+# Shoes.app do
+# style[:cap].should == nil
+# style[:strokewidth].should == 1.0
+# end
+# end
+#end
+
+# exit loop
+Shoes.app do
+ timer(0.01) do
+ close
+ end
+end
diff --git a/app/spec/stdout.rb b/app/spec/stdout.rb
new file mode 100755
index 0000000..bffa90b
--- /dev/null
+++ b/app/spec/stdout.rb
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+
+require 'lib/dev/stdout'
+
+require 'spec/autorun'
+
+# XXX: dots will be doubled because they are written also as part of the test
+# I chose to display dots so that the spec execution output looks good
+describe STDOUT, "#write" do
+ it "should emit the :output signal" do
+ event_called = false
+ conn = STDOUT.on_event :output, :any do
+ event_called = true
+ end
+ STDOUT.write "."
+ STDOUT.delete_event_connection conn
+ event_called.should == true
+ end
+
+ it "should emit a signal with the correct argument" do
+ event_called = false
+ conn = STDOUT.on_event :output, :any do |arg|
+ event_called = true
+ arg.should == "."
+ end
+ STDOUT.write "."
+ STDOUT.delete_event_connection conn
+ event_called.should == true
+ end
+
+ it "should be called when using print" do
+ event_called = false
+ conn = STDOUT.on_event :output, :any do
+ event_called = true
+ end
+ print "."
+ STDOUT.delete_event_connection conn
+ event_called.should == true
+ end
+end
diff --git a/app/static/hacketyhack-dmg.jpg b/app/static/hacketyhack-dmg.jpg
new file mode 100644
index 0000000..32458eb
--- /dev/null
+++ b/app/static/hacketyhack-dmg.jpg
Binary files differ
diff --git a/app/static/hhabout.png b/app/static/hhabout.png
new file mode 100644
index 0000000..2a199c6
--- /dev/null
+++ b/app/static/hhabout.png
Binary files differ
diff --git a/app/static/hhcheat.png b/app/static/hhcheat.png
new file mode 100755
index 0000000..d87dcfd
--- /dev/null
+++ b/app/static/hhcheat.png
Binary files differ
diff --git a/app/static/hhconsole.png b/app/static/hhconsole.png
new file mode 100755
index 0000000..ed02d47
--- /dev/null
+++ b/app/static/hhconsole.png
Binary files differ
diff --git a/app/static/hhhello.png b/app/static/hhhello.png
new file mode 100644
index 0000000..1c0dec7
--- /dev/null
+++ b/app/static/hhhello.png
Binary files differ
diff --git a/app/static/icon-art.png b/app/static/icon-art.png
new file mode 100644
index 0000000..0bfecd5
--- /dev/null
+++ b/app/static/icon-art.png
Binary files differ
diff --git a/app/static/icon-dingbat.png b/app/static/icon-dingbat.png
new file mode 100644
index 0000000..4ef151a
--- /dev/null
+++ b/app/static/icon-dingbat.png
Binary files differ
diff --git a/app/static/icon-email.png b/app/static/icon-email.png
new file mode 100644
index 0000000..f233bc7
--- /dev/null
+++ b/app/static/icon-email.png
Binary files differ
diff --git a/app/static/icon-file.png b/app/static/icon-file.png
new file mode 100755
index 0000000..8b8b1ca
--- /dev/null
+++ b/app/static/icon-file.png
Binary files differ
diff --git a/app/static/icon-sound.png b/app/static/icon-sound.png
new file mode 100644
index 0000000..0ca9074
--- /dev/null
+++ b/app/static/icon-sound.png
Binary files differ
diff --git a/app/static/icon-table.png b/app/static/icon-table.png
new file mode 100644
index 0000000..693709c
--- /dev/null
+++ b/app/static/icon-table.png
Binary files differ
diff --git a/app/static/matz.jpg b/app/static/matz.jpg
new file mode 100644
index 0000000..ec1539d
--- /dev/null
+++ b/app/static/matz.jpg
Binary files differ
diff --git a/app/static/splash-hand.png b/app/static/splash-hand.png
new file mode 100644
index 0000000..35f8c39
--- /dev/null
+++ b/app/static/splash-hand.png
Binary files differ
diff --git a/app/static/tab-cheat.png b/app/static/tab-cheat.png
new file mode 100644
index 0000000..a09c62d
--- /dev/null
+++ b/app/static/tab-cheat.png
Binary files differ
diff --git a/app/static/tab-email.png b/app/static/tab-email.png
new file mode 100644
index 0000000..7348aed
--- /dev/null
+++ b/app/static/tab-email.png
Binary files differ
diff --git a/app/static/tab-hand.png b/app/static/tab-hand.png
new file mode 100644
index 0000000..5a9ee7a
--- /dev/null
+++ b/app/static/tab-hand.png
Binary files differ
diff --git a/app/static/tab-help.png b/app/static/tab-help.png
new file mode 100644
index 0000000..b7cbbff
--- /dev/null
+++ b/app/static/tab-help.png
Binary files differ
diff --git a/app/static/tab-home.png b/app/static/tab-home.png
new file mode 100644
index 0000000..fed6221
--- /dev/null
+++ b/app/static/tab-home.png
Binary files differ
diff --git a/app/static/tab-new.png b/app/static/tab-new.png
new file mode 100644
index 0000000..813f712
--- /dev/null
+++ b/app/static/tab-new.png
Binary files differ
diff --git a/app/static/tab-properties.png b/app/static/tab-properties.png
new file mode 100644
index 0000000..ab0e8ea
--- /dev/null
+++ b/app/static/tab-properties.png
Binary files differ
diff --git a/app/static/tab-quit.png b/app/static/tab-quit.png
new file mode 100644
index 0000000..2541d2b
--- /dev/null
+++ b/app/static/tab-quit.png
Binary files differ
diff --git a/app/static/tab-tour.png b/app/static/tab-tour.png
new file mode 100644
index 0000000..d22fde8
--- /dev/null
+++ b/app/static/tab-tour.png
Binary files differ
diff --git a/app/static/tab-try.png b/app/static/tab-try.png
new file mode 100644
index 0000000..b3d8ce0
--- /dev/null
+++ b/app/static/tab-try.png
Binary files differ
diff --git a/app/static/turtle.png b/app/static/turtle.png
new file mode 100644
index 0000000..3ad4844
--- /dev/null
+++ b/app/static/turtle.png
Binary files differ
diff --git a/fonts/Coolvetica.ttf b/fonts/Coolvetica.ttf
new file mode 100644
index 0000000..56a4b17
--- /dev/null
+++ b/fonts/Coolvetica.ttf
Binary files differ
diff --git a/fonts/Lacuna.ttf b/fonts/Lacuna.ttf
new file mode 100644
index 0000000..d4e5003
--- /dev/null
+++ b/fonts/Lacuna.ttf
Binary files differ
diff --git a/hacketyhack b/hacketyhack
new file mode 100755
index 0000000..8390d67
--- /dev/null
+++ b/hacketyhack
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+REALPATH=`readlink -f $0`
+
+APPPATH="${REALPATH%/*}"
+if [ "$APPPATH" = "shoes" ]; then
+ APPPATH="."
+fi
+if [ "$APPPATH" = "." ]; then
+ APPPATH=`pwd`
+fi
+
+# makeself changes the directory to /tmp/selfgzNNNNN. if we're in
+# an extracted archive, change back to the directory where we launched.
+if [ `pwd | awk '{ sub(/[0-9]+/, ""); print }'` = "/tmp/selfgz" ]; then
+ # fix the symlink, this is actually a bug in the minitar lib
+ rm -f libruby.so.1.8
+ ln -s libruby.so libruby.so.1.8
+ cd "$OLDPWD"
+fi
+cd "$OLDPWD"
+LD_LIBRARY_PATH=$APPPATH $APPPATH/hacketyhack-bin "$@"
diff --git a/hacketyhack-bin b/hacketyhack-bin
new file mode 100755
index 0000000..a349199
--- /dev/null
+++ b/hacketyhack-bin
Binary files differ
diff --git a/lib/shoes.rb b/lib/shoes.rb
new file mode 100644
index 0000000..1df9589
--- /dev/null
+++ b/lib/shoes.rb
@@ -0,0 +1,548 @@
+# -*- encoding: utf-8 -*-
+#
+# lib/shoes.rb
+# The Shoes base app, both a demonstration and the learning tool for
+# using Shoes.
+#
+ARGV.delete_if { |x| x =~ /-psn_/ }
+
+require 'open-uri'
+require 'optparse'
+require 'resolv-replace' if RUBY_PLATFORM =~ /win/
+require 'shoes/inspect'
+require 'shoes/cache'
+if Object.const_defined? :Shoes
+ require 'shoes/image'
+end
+require 'shoes/shybuilder'
+
+def Shoes.hook; end
+
+class Encoding
+ %w[ASCII_8BIT UTF_16BE UTF_16LE UTF_32BE UTF_32LE US_ASCII].each do |ec|
+ eval "#{ec} = '#{ec.sub '_', '-'}'"
+ end unless RUBY_PLATFORM =~ /linux/
+end
+
+class Range
+ def rand
+ conv = (Integer === self.end && Integer === self.begin ? :to_i : :to_f)
+ ((Kernel.rand * (self.end - self.begin)) + self.begin).send(conv)
+ end
+end
+
+unless Time.respond_to? :today
+ def Time.today
+ t = Time.now
+ t - (t.to_i % 86400)
+ end
+end
+
+class Shoes
+ RELEASES = %w[Curious Raisins Policeman]
+
+ NotFound = proc do
+ para "404 NOT FOUND, GUYS!"
+ end
+
+ class << self; attr_accessor :locale, :language end
+ @locale = ENV["SHOES_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C"
+ @language = @locale[/^(\w{2})_/, 1] || "en"
+
+ @mounts = []
+
+ OPTS = OptionParser.new do |opts|
+ opts.banner = "Usage: shoes [options] (app.rb or app.shy)"
+
+ opts.on("-m", "--manual",
+ "Open the built-in manual.") do
+ show_manual
+ end
+
+ opts.on("-p", "--package",
+ "Package a Shoes app for Windows, OS X and Linux.") do |s|
+ make_pack
+ end
+
+ opts.on("-g", "--gem",
+ "Passes commands to RubyGems.") do
+ require 'shoes/setup'
+ require 'rubygems/gem_runner'
+ Gem::GemRunner.new.run(ARGV)
+ raise SystemExit, ""
+ end
+
+ opts.on("--manual-html DIRECTORY", "Saves the manual to a directory as HTML.") do |dir|
+ manual_as :html, dir
+ raise SystemExit, "HTML manual in: #{dir}"
+ end
+
+ opts.on("--install MODE SRC DEST", "Installs a file.") do |mode|
+ src, dest = ARGV
+ FileUtils.install src, dest, :mode => mode.to_i(8), :preserve => true
+ raise SystemExit, ""
+ end
+
+ opts.on("--nolayered", "No WS_EX_LAYERED style option.") do
+ $NOLAYERED = 1
+ Shoes.args!
+ end
+
+ opts.on_tail("-v", "--version", "Display the version info.") do
+ raise SystemExit, File.read("#{DIR}/VERSION.txt").strip
+ end
+
+ opts.on_tail("-h", "--help", "Show this message") do
+ raise SystemExit, opts.to_s
+ end
+ end
+
+ class SettingUp < StandardError; end
+
+ @setups = {}
+
+ def self.setup &blk
+ require 'shoes/setup'
+ line = caller[0]
+ return if @setups[line]
+ script = line[/^(.+?):/, 1]
+ set = Shoes::Setup.new(script, &blk)
+ @setups[line] = true
+ unless set.no_steps?
+ raise SettingUp
+ end
+ end
+
+ def self.show_selector
+ fname = ask_open_file
+ Shoes.visit(fname) if fname
+ end
+
+ def self.package_app
+ fname = ask_open_file
+ return false unless fname
+ start_shy_builder fname
+ end
+
+ def self.splash
+ font "#{DIR}/fonts/Lacuna.ttf"
+ Shoes.app :width => 400, :height => 300, :resizable => false do
+ style(Para, :align => "center", :weight => "bold", :font => "Lacuna Regular", :size => 13)
+ style(Link, :stroke => yellow, :underline => nil)
+ style(LinkHover, :stroke => yellow, :fill => nil)
+
+ x1 = 77; y1 = 122
+ x2 = 148; y2 = -122
+ x3 = 245; y3 = 0
+
+ nofill
+ strokewidth 40.0
+
+ @waves = stack :top => 0, :left => 0
+
+ require 'shoes/search'
+ require 'shoes/help'
+
+ stack :margin => 18 do
+ para "Welcome to", :stroke => "#DFA", :margin => 0
+ para "SHOES", :size => 48, :stroke => "#DFA", :margin_top => 0
+ stack do
+ background black(0.2), :curve => 8
+ para link("Open an App.") { Shoes.show_selector and close }, :margin => 10, :margin_bottom => 4
+ #para link("Package an App.") { Shoes.package_app and close }, :margin => 10, :margin_bottom => 4
+ para link("Package an App.") { Shoes.make_pack and close }, :margin => 10, :margin_bottom => 4
+ para link("Read the Manual.") { Shoes.show_manual and close }, :margin => 10
+ end
+ inscription "Alt-Slash opens the console.", :stroke => "#DFA", :align => "center"
+ end
+
+ animate(10) do |ani|
+ a = Math.sin(ani * 0.02) * 20
+ @waves.clear do
+ background white
+ y = -30
+ 16.times do |i|
+ shape do
+ move_to x = (-300 - (i*(a*0.8))), y
+ c = (a + 14) * 0.01
+ stroke rgb(i * 0.06, c + 0.1, 0.1, 1.0 - (ani * 0.0003))
+ 4.times do
+ curve_to x1 + x, (y1-(i*a)) + y, x2 + x, (y2+(i*a)) + y, x3 + x, y3 + y
+ x += x3
+ end
+ end
+ y += 30
+ end
+ end
+ end
+ end
+ end
+
+ def self.make_pack
+ require 'shoes/pack'
+ Shoes.app(:width => 500, :height => 480, :resizable => true, &PackMake)
+ end
+
+ def self.manual_p(str, path)
+ str.gsub(/\n+\s*/, " ").
+ gsub(/&/, '&amp;').gsub(/>/, '&gt;').gsub(/>/, '&lt;').gsub(/"/, '&quot;').
+ gsub(/`(.+?)`/m, '<code>\1</code>').gsub(/\[\[BR\]\]/i, "<br />\n").
+ gsub(/\^(.+?)\^/m, '\1').
+ gsub(/'''(.+?)'''/m, '<strong>\1</strong>').gsub(/''(.+?)''/m, '<em>\1</em>').
+ gsub(/\[\[(http:\/\/\S+?)\]\]/m, '<a href="\1" target="_new">\1</a>').
+ gsub(/\[\[(http:\/\/\S+?) (.+?)\]\]/m, '<a href="\1" target="_new">\2</a>').
+ gsub(/\[\[(\S+?)\]\]/m) do
+ ms, mn = $1.split(".", 2)
+ if mn
+ '<a href="' + ms + '.html#' + mn + '">' + mn + '</a>'
+ else
+ '<a href="' + ms + '.html">' + ms + '</a>'
+ end
+ end.
+ gsub(/\[\[(\S+?) (.+?)\]\]/m, '<a href="\1.html">\2</a>').
+ gsub(/\!(\{[^}\n]+\})?([^!\n]+\.\w+)\!/) do
+ x = "static/#$2"
+ FileUtils.cp("#{DIR}/#{x}", "#{path}/#{x}") if File.exists? "#{DIR}/#{x}"
+ '<img src="' + x + '" />'
+ end
+ end
+
+ def self.manual_link(sect)
+ end
+
+ TITLES = {:title => :h1, :subtitle => :h2, :tagline => :h3, :caption => :h4}
+
+ def self.manual_as format, *args
+ require 'shoes/search'
+ require 'shoes/help'
+
+ case format
+ when :shoes
+ Shoes.app(:width => 720, :height => 640, &Shoes::Help)
+ else
+ extend Shoes::Manual
+ man = self
+ dir, = args
+ FileUtils.mkdir_p File.join(dir, 'static')
+ FileUtils.cp "#{DIR}/static/shoes-icon.png", "#{dir}/static"
+ %w[manual.css code_highlighter.js code_highlighter_ruby.js].
+ each { |x| FileUtils.cp "#{DIR}/static/#{x}", "#{dir}/static" }
+ html_bits = proc do
+ proc do |sym, text|
+ case sym when :intro
+ div.intro { p { self << man.manual_p(text, dir) } }
+ when :code
+ pre { code.rb text.gsub(/^\s*?\n/, '') }
+ when :colors
+ color_names = (Shoes::COLORS.keys*"\n").split("\n").sort
+ color_names.each do |color|
+ c = Shoes::COLORS[color.intern]
+ f = c.dark? ? "white" : "black"
+ div.color(:style => "background: #{c}; color: #{f}") { h3 color; p c }
+ end
+ when :index
+ tree = man.class_tree
+ shown = []
+ i = 0
+ index_p = proc do |k, subs|
+ unless shown.include? k
+ i += 1
+ p "â–¸ #{k}", :style => "margin-left: #{20*i}px"
+ subs.uniq.sort.each do |s|
+ index_p[s, tree[s]]
+ end if subs
+ i -= 1
+ shown << k
+ end
+ end
+ tree.sort.each &index_p
+ # index_page
+ when :list
+ ul { text.each { |x| li { self << man.manual_p(x, dir) } } }
+ when :samples
+ folder = File.join DIR, 'samples'
+ h = {}
+ Dir.glob(File.join folder, '*').each do |file|
+ if File.extname(file) == '.rb'
+ key = File.basename(file).split('-')[0]
+ h[key] ? h[key].push(file) : h[key] = [file]
+ end
+ end
+ h.each do |k, v|
+ p "<h4>#{k}</h4>"
+ samples = []
+ v.each do |file|
+ sample = File.basename(file).split('-')[1..-1].join('-')[0..-4]
+ samples << "<a href=\"http://github.com/shoes/shoes/raw/master/manual-snapshots/#{k}-#{sample}.png\">#{sample}</a>"
+ end
+ p samples.join ' '
+ end
+ else
+ send(TITLES[sym] || :p) { self << man.manual_p(text, dir) }
+ end
+ end
+ end
+
+ docs = load_docs(Shoes::Manual::path)
+ sections = docs.map { |x,| x }
+
+ docn = 1
+ docs.each do |title1, opt1|
+ subsect = opt1['sections'].map { |x,| x }
+ menu = sections.map do |x|
+ [x, (subsect if x == title1)]
+ end
+
+ path1 = File.join(dir, title1.gsub(/\W/, ''))
+ make_html("#{path1}.html", title1, menu) do
+ h2 "The Shoes Manual"
+ h1 title1
+ man.wiki_tokens opt1['description'], true, &instance_eval(&html_bits)
+ p.next { text "Next: "
+ a opt1['sections'].first[1]['title'], :href => "#{opt1['sections'].first[0]}.html" }
+ end
+
+ optn = 1
+ opt1['sections'].each do |title2, opt2|
+ path2 = File.join(dir, title2)
+ make_html("#{path2}.html", opt2['title'], menu) do
+ h2 "The Shoes Manual"
+ h1 opt2['title']
+ man.wiki_tokens opt2['description'], true, &instance_eval(&html_bits)
+ opt2['methods'].each do |title3, desc3|
+ sig, val = title3.split(/\s+»\s+/, 2)
+ aname = sig[/^[^(=]+=?/].gsub(/\s/, '').downcase
+ a :name => aname
+ div.method do
+ a sig, :href => "##{aname}"
+ text " » #{val}" if val
+ end
+ div.sample do
+ man.wiki_tokens desc3, &instance_eval(&html_bits)
+ end
+ end
+ if opt1['sections'][optn]
+ p.next { text "Next: "
+ a opt1['sections'][optn][1]['title'], :href => "#{opt1['sections'][optn][0]}.html" }
+ elsif docs[docn]
+ p.next { text "Next: "
+ a docs[docn][0], :href => "#{docs[docn][0].gsub(/\W/, '')}.html" }
+ end
+ optn += 1
+ end
+ end
+
+ docn += 1
+ end
+ end
+ end
+
+ def self.show_manual
+ manual_as :shoes
+ end
+
+ def self.show_log
+ require 'shoes/log'
+ return if @log_app and Shoes.APPS.include? @log_app
+ @log_app =
+ Shoes.app do
+ extend Shoes::LogWindow
+ setup
+ end
+ end
+
+ def self.mount(path, meth, &blk)
+ @mounts << [path, meth || blk]
+ end
+
+ SHOES_URL_RE = %r!^@([^/]+)(.*)$!
+
+ def self.run(path)
+ uri = URI(path)
+ @mounts.each do |mpath, rout|
+ m, *args = *path.match(/^#{mpath}$/)
+ if m
+ unless rout.is_a? Proc
+ rout = rout[0].instance_method(rout[1])
+ end
+ return [rout, args]
+ end
+ end
+ case uri.path when "/"
+ [nil]
+ when SHOES_URL_RE
+ [proc { eval(URI("http://#$1:53045#$2").read) }]
+ else
+ [NotFound]
+ end
+ end
+
+ def self.args!
+ if RUBY_PLATFORM !~ /darwin/ and ARGV.empty?
+ Shoes.splash
+ end
+ OPTS.parse! ARGV
+ ARGV[0] or true
+ end
+
+ def self.uri(str)
+ if str =~ SHOES_URL_RE
+ URI("http://#$1:53045#$2")
+ else
+ URI(str) rescue nil
+ end
+ end
+
+ def self.visit(path)
+ uri = Shoes.uri(path)
+
+ case uri
+ when URI::HTTP
+ str = uri.read
+ if str !~ /Shoes\.app/
+ Shoes.app do
+ eval(uri.read)
+ end
+ else
+ eval(uri.read)
+ end
+ else
+ path = File.expand_path(path.gsub(/\\/, "/"))
+ if path =~ /\.shy$/
+ @shy = true
+ require 'shoes/shy'
+ base = File.basename(path, ".shy")
+ tmpdir = "%s/shoes-%s.%d" % [Dir.tmpdir, base, $$]
+ shy = Shy.x(path, tmpdir)
+ Dir.chdir(tmpdir)
+ Shoes.debug "Loaded SHY: #{shy.name} #{shy.version} by #{shy.creator}"
+ path = shy.launch
+ else
+ @shy = false
+ Dir.chdir(File.dirname(path))
+ path = File.basename(path)
+ end
+
+ $0.replace path
+
+ code = read_file(path)
+ eval(code, TOPLEVEL_BINDING, path)
+ end
+ rescue SettingUp
+ rescue Object => e
+ error(e)
+ show_log
+ end
+
+ def self.read_file path
+ if RUBY_VERSION =~ /^1\.9/ and !@shy
+ #File.open(path, 'r:utf-8') { |f| f.read }
+ IO.read(path).force_encoding("UTF-8")
+ else
+ File.read(path)
+ end
+ end
+
+ def self.url(path, meth)
+ Shoes.mount(path, [self, meth])
+ end
+
+ module Basic
+ def tween opts, &blk
+ opts = opts.dup
+
+ if opts[:upward]
+ opts[:top] = self.top - opts.delete(:upward)
+ elsif opts[:downward]
+ opts[:top] = self.top + opts.delete(:downward)
+ end
+
+ if opts[:sideways]
+ opts[:left] = self.left + opts.delete(:sideways)
+ end
+
+ @TWEEN.remove if @TWEEN
+ @TWEEN = parent.animate(opts[:speed] || 20) do
+
+ # figure out a coordinate halfway between here and there
+ cont = opts.select do |k, v|
+ if self.respond_to? k
+ n, o = v, self.send(k)
+ if n != o
+ n = o + ((n - o) / 2)
+ n = v if o == n
+ self.send("#{k}=", n)
+ end
+ self.style[k] != v
+ end
+ end
+
+ # if we're there, get rid of the animation
+ if cont.empty?
+ @TWEEN.remove
+ @TWEEN = nil
+ blk.call if blk
+ end
+ end
+ end
+ end
+
+ # complete list of styles
+ BASIC_S = [:left, :top, :right, :bottom, :width, :height, :attach, :hidden,
+ :displace_left, :displace_top, :margin, :margin_left, :margin_top,
+ :margin_right, :margin_bottom]
+ TEXT_S = [:strikecolor, :undercolor, :font, :size, :family, :weight,
+ :rise, :kerning, :emphasis, :strikethrough, :stretch, :underline,
+ :variant]
+ MOUSE_S = [:click, :motion, :release, :hover, :leave]
+ KEY_S = [:keydown, :keypress, :keyup]
+ COLOR_S = [:stroke, :fill]
+
+ {Background => [:angle, :radius, :curve, *BASIC_S],
+ Border => [:angle, :radius, :curve, :strokewidth, *BASIC_S],
+ Canvas => [:scroll, :start, :finish, *(KEY_S|MOUSE_S|BASIC_S)],
+ Check => [:click, :checked, *BASIC_S],
+ Radio => [:click, :checked, :group, *BASIC_S],
+ EditLine => [:change, :secret, :text, *BASIC_S],
+ EditBox => [:change, :text, *BASIC_S],
+ Effect => [:radius, :distance, :inner, *(COLOR_S|BASIC_S)],
+ Image => MOUSE_S|BASIC_S,
+ ListBox => [:change, :items, :choose, *BASIC_S],
+ # Pattern => [:angle, :radius, *BASIC_S],
+ Progress => BASIC_S,
+ Shape => COLOR_S|MOUSE_S|BASIC_S,
+ TextBlock => [:justify, :align, :leading, *(COLOR_S|MOUSE_S|TEXT_S|BASIC_S)],
+ Text => COLOR_S|MOUSE_S|TEXT_S|BASIC_S}.
+ each do |klass, styles|
+ klass.class_eval do
+ include Basic
+ styles.each do |m|
+ case m when *MOUSE_S
+ else
+ define_method(m) { style[m] } unless klass.method_defined? m
+ define_method("#{m}=") { |v| style(m => v) } unless klass.method_defined? "#{m}="
+ end
+ end
+ end
+ end
+
+ class Types::Widget
+ @types = {}
+ def self.inherited subc
+ methc = subc.to_s[/(^|::)(\w+)$/, 2].
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
+ @types[methc] = subc
+ Shoes.class_eval %{
+ def #{methc}(*a, &b)
+ a.unshift Widget.instance_variable_get("@types")[#{methc.dump}]
+ widget(*a, &b)
+ end
+ }
+ end
+ end
+end
+
+def window(*a, &b)
+ Shoes.app(*a, &b)
+end
diff --git a/lib/shoes/cache.rb b/lib/shoes/cache.rb
new file mode 100644
index 0000000..b3a5583
--- /dev/null
+++ b/lib/shoes/cache.rb
@@ -0,0 +1,54 @@
+require 'fileutils'
+include FileUtils
+
+# locate ~/.shoes
+require 'tmpdir'
+
+lib_dir = nil
+homes = []
+homes << [ENV['HOME'], File.join( ENV['HOME'], '.shoes' )] if ENV['HOME']
+homes << [ENV['APPDATA'], File.join( ENV['APPDATA'], 'Shoes' )] if ENV['APPDATA']
+homes.each do |home_top, home_dir|
+ next unless home_top
+ if File.exists? home_top
+ lib_dir = home_dir
+ break
+ end
+end
+LIB_DIR = lib_dir || File.join(Dir::tmpdir, "shoes")
+LIB_DIR.gsub! /\\/, '/'
+SITE_LIB_DIR = File.join(LIB_DIR, '+lib')
+GEM_DIR = File.join(LIB_DIR, '+gem')
+CACHE_DIR = File.join(LIB_DIR, '+cache')
+
+mkdir_p(CACHE_DIR)
+$:.unshift SITE_LIB_DIR
+$:.unshift GEM_DIR
+ENV['GEM_HOME'] = GEM_DIR
+
+require 'rbconfig'
+config = {
+ 'ruby_install_name' => "shoes --ruby",
+ 'RUBY_INSTALL_NAME' => "shoes --ruby",
+ 'prefix' => "#{DIR}",
+ 'bindir' => "#{DIR}",
+ 'rubylibdir' => "#{DIR}/ruby/lib",
+ 'datarootdir' => "#{DIR}/share",
+ 'dvidir' => "#{DIR}/doc/${PACKAGE}",
+ 'psdir' => "#{DIR}/doc/${PACKAGE}",
+ 'htmldir' => "#{DIR}/doc/${PACKAGE}",
+ 'docdir' => "#{DIR}/doc/${PACKAGE}",
+ 'archdir' => "#{DIR}/ruby/lib/#{RUBY_PLATFORM}",
+ 'sitedir' => SITE_LIB_DIR,
+ 'sitelibdir' => SITE_LIB_DIR,
+ 'sitearchdir' => "#{SITE_LIB_DIR}/#{RUBY_PLATFORM}",
+ 'LIBRUBYARG_STATIC' => "",
+ 'libdir' => "#{DIR}",
+ 'LDFLAGS' => "-L. -L#{DIR}"
+}
+Config::CONFIG.merge! config
+Config::MAKEFILE_CONFIG.merge! config
+GEM_CENTRAL_DIR = File.join(DIR, 'ruby/gems/' + Config::CONFIG['ruby_version'])
+Dir[GEM_CENTRAL_DIR + "/gems/*"].each do |gdir|
+ $: << "#{gdir}/lib"
+end
diff --git a/lib/shoes/chipmunk.rb b/lib/shoes/chipmunk.rb
new file mode 100644
index 0000000..f56e583
--- /dev/null
+++ b/lib/shoes/chipmunk.rb
@@ -0,0 +1,35 @@
+require 'chipmunk'
+
+module ChipMunk
+ def cp_space
+ @space = CP::Space.new
+ @space.damping = 0.8
+ @space.gravity = vec2 0, 25
+ @space
+ end
+
+ def cp_oval l, t, r, opts = {}
+ b = CP::Body.new 1,1
+ b.p = vec2 l, t
+ @space.add_body b
+ @space.add_shape CP::Shape::Circle.new(b, r, vec2(0, 0))
+
+ img = image left: l-r-1, top: t-r-1, width: 2*r+2, height: 2*r+2, body: b, inflate: r-2 do
+ oval 1, 1, 2*r, opts
+ end
+ end
+
+ def cp_line x0, y0, x1, y1, opts = {}
+ opts[:strokewidth] = 5 unless opts[:strokewidth]
+ sb = CP::Body.new 1.0/0.0, 1.0/0.0
+ seg = CP::Shape::Segment.new sb, vec2(x0, y0), vec2(x1, y1), opts[:strokewidth]
+ @space.add_shape seg
+ line x0, y0, x1, y1, opts
+ end
+end
+
+Shoes::Image.class_eval do
+ def cp_move
+ move style[:body].p.x.to_i - style[:inflate], style[:body].p.y.to_i - style[:inflate]
+ end
+end
diff --git a/lib/shoes/data.rb b/lib/shoes/data.rb
new file mode 100644
index 0000000..1f05001
--- /dev/null
+++ b/lib/shoes/data.rb
@@ -0,0 +1,39 @@
+require 'sqlite3'
+
+data_file = File.join(LIB_DIR, "+data")
+data_init = !File.exists?(data_file)
+
+DATABASE = SQLite3::Database.new data_file
+DATABASE.type_translation = true
+
+class << DATABASE
+ DATABASE_VERSION = 1
+ def setup
+ DATABASE.execute_batch %{
+ CREATE TABLE cache (
+ url text primary key,
+ etag text,
+ hash varchar(40),
+ saved datetime
+ );
+ CREATE TABLE upgrades (
+ version int primary key
+ );
+ INSERT INTO upgrades VALUES (?);
+ }, DATABASE_VERSION
+ end
+ def check_cache_for url
+ etag, hash, saved =
+ DATABASE.get_first_row("SELECT etag, hash, saved FROM cache WHERE url = ?", url)
+ {:etag => etag, :hash => hash, :saved => saved.nil? ? 0 : Time.parse(saved.to_s).to_i}
+ end
+ def notify_cache_of url, etag, hash
+ DATABASE.query %{
+ REPLACE INTO cache (url, etag, hash, saved)
+ VALUES (?, ?, ?, datetime("now", "localtime"));
+ }, url, etag, hash
+ nil
+ end
+end
+
+DATABASE.setup if data_init
diff --git a/lib/shoes/help.rb b/lib/shoes/help.rb
new file mode 100644
index 0000000..836bbd7
--- /dev/null
+++ b/lib/shoes/help.rb
@@ -0,0 +1,468 @@
+# -*- encoding: utf-8 -*-
+module Shoes::Manual
+ PARA_RE = /\s*?(\{{3}(?:.+?)\}{3})|\n\n/m
+ CODE_RE = /\{{3}(?:\s*\#![^\n]+)?(.+?)\}{3}/m
+ IMAGE_RE = /\!(\{([^}\n]+)\})?([^!\n]+\.\w+)\!/
+ CODE_STYLE = {:size => 9, :margin => 12}
+ INTRO_STYLE = {:size => 16, :margin_bottom => 20, :stroke => "#000"}
+ SUB_STYLE = {:stroke => "#CCC", :margin_top => 10}
+ IMAGE_STYLE = {:margin => 8, :margin_left => 100}
+ COLON = ": "
+
+ [INTRO_STYLE, SUB_STYLE].each do |h|
+ h[:font] = "MS UI Gothic"
+ end if Shoes.language == 'ja'
+
+ def self.path
+ path = "#{DIR}/static/manual-#{Shoes.language}.txt"
+ unless File.exists? path
+ path = "#{DIR}/static/manual-en.txt"
+ end
+ path
+ end
+
+ def dewikify_hi(str, terms, intro = false)
+ if terms
+ code = []
+ str = str.
+ gsub(CODE_RE) { |x| code << x; "CODE#[#{code.length-1}]" }.
+ gsub(/#{Regexp::quote(terms)}/i, '@\0@').
+ gsub(/CODE#\[(\d+)\]/) { code[$1.to_i] }
+ end
+ dewikify(str, intro)
+ end
+
+ def dewikify_p(ele, str, *args)
+ str = str.gsub(/\n+\s*/, " ").dump.
+ gsub(/`(.+?)`/m, '", code("\1"), "').gsub(/\[\[BR\]\]/i, "\n").
+ gsub(/\^(.+?)\^/m, '\1').
+ gsub(/@(.+?)@/m, '", strong("\1", :fill => yellow), "').
+ gsub(/'''(.+?)'''/m, '", strong("\1"), "').gsub(/''(.+?)''/m, '", em("\1"), "').
+ gsub(/\[\[(\S+?)\]\]/m, '", link("\1".split(".", 2).last) { open_link("\1") }, "').
+ gsub(/\[\[(\S+?) (.+?)\]\]/m, '", link("\2") { open_link("\1") }, "').
+ gsub(IMAGE_RE, '", *args); stack(IMAGE_STYLE.merge({\2})) { image("#{DIR}/static/\3") }; #{ele}("')
+ #debug str if str =~ /The list of special keys/
+ a = str.split(', ", ", ')
+ if a.size == 1
+ eval("#{ele}(#{str}, *args)")
+ else
+ flow do
+ a[0...-1].each{|s| eval("#{ele}(#{s}, ',', *args)")}
+ eval("#{ele}(#{a[-1]}, *args)")
+ end
+ end
+ end
+
+ def dewikify_code(str)
+ str = str.gsub(/\A\n+/, '').chomp
+ stack :margin_bottom => 12 do
+ background rgb(210, 210, 210), :curve => 4
+ para code(str), CODE_STYLE
+ stack :top => 0, :right => 2, :width => 70 do
+ stack do
+ background "#8A7", :margin => [0, 2, 0, 2], :curve => 4
+ para link("Run this", :stroke => "#eee", :underline => "none") { run_code(str) },
+ :margin => 4, :align => 'center', :weight => 'bold', :size => 9
+ end
+ end
+ end
+ end
+
+ def wiki_tokens(str, intro = false)
+ paras = str.split(PARA_RE).reject { |x| x.empty? }
+ if intro
+ yield :intro, paras.shift
+ end
+ paras.map do |ps|
+ if ps =~ CODE_RE
+ yield :code, $1
+ else
+ case ps
+ when /\A\{COLORS\}/
+ yield :colors, nil
+ when /\A\{INDEX\}/
+ yield :index, nil
+ when /\A\{SAMPLES\}/
+ yield :samples, nil
+ when /\A \* (.+)/m
+ yield :list, $1.split(/^ \* /)
+ when /\A==== (.+) ====/
+ yield :caption, $1
+ when /\A=== (.+) ===/
+ yield :tagline, $1
+ when /\A== (.+) ==/
+ yield :subtitle, $1
+ when /\A= (.+) =/
+ yield :title, $1
+ else
+ yield :para, ps
+ end
+ end
+ end
+ end
+
+ def dewikify(str, intro = false)
+ proc do
+ wiki_tokens(str, intro) do |sym, text|
+ case sym when :intro
+ dewikify_p :para, text, INTRO_STYLE
+ when :code
+ dewikify_code(text)
+ when :colors
+ color_page
+ when :index
+ index_page
+ when :samples
+ sample_page
+ when :list
+ text.each { |t| stack(:margin_left => 30) {
+ fill black; oval -10, 7, 6; dewikify_p :para, t } }
+ else
+ dewikify_p sym, text
+ end
+ end
+ end
+ end
+
+ def sample_page
+ folder = File.join DIR, 'samples'
+ h = {}
+ Dir.glob(File.join folder, '*').each do |file|
+ if File.extname(file) == '.rb'
+ key = File.basename(file).split('-')[0]
+ h[key] ? h[key].push(file) : h[key] = [file]
+ end
+ end
+ stack do
+ h.each do |k, v|
+ subtitle k
+ flow do
+ v.each do |file|
+ para link(File.basename(file).split('-')[1..-1].join('-')[0..-4]){
+ Dir.chdir(folder){eval IO.read(file).force_encoding("UTF-8"), TOPLEVEL_BINDING}
+ }
+ end
+ end
+ end
+ end
+ end
+
+ def color_page
+ color_names = (Shoes::COLORS.keys*"\n").split("\n").sort
+ flow do
+ color_names.each do |color|
+ flow :width => 0.33 do
+ c = send(color)
+ background c
+ para strong(color), "\n", c, :stroke => (c.dark? ? white : black),
+ :margin => 4, :align => 'center'
+ end
+ end
+ end
+ end
+
+ def class_tree
+ tree = {}
+ Shoes.constants.each do |c|
+ k = Shoes.const_get(c)
+ next unless k.respond_to? :superclass
+
+ c = "Shoes::#{c}"
+ if k.superclass == Object
+ tree[c] ||= []
+ else
+ k.ancestors[1..-1].each do |sk|
+ break if [Object, Kernel].include? sk
+ next unless sk.is_a? Class #don't show mixins
+ (tree[sk.name] ||= []) << c
+ c = sk.name
+ end
+ end
+ end
+ tree
+ end
+
+ def index_page
+ tree = class_tree
+ shown = []
+ index_p = proc do |k, subs|
+ unless shown.include? k
+ stack :margin_left => 20 do
+ flow do
+ para "â–¸ ", :font => case RUBY_PLATFORM
+ when /mingw/; "MS UI Gothic"
+ when /darwin/; "AppleGothic, Arial"
+ else "Arial"
+ end
+ para k
+ end
+ subs.uniq.sort.each do |s|
+ index_p[s, tree[s]]
+ end if subs
+ end
+ shown << k
+ end
+ end
+ tree.sort.each &index_p
+ end
+
+ def run_code str
+ eval(str, TOPLEVEL_BINDING)
+ end
+
+ def load_docs path
+ return @docs if @docs
+ str = Shoes.read_file(path)
+ @search = Shoes::Search.new
+ @sections, @methods, @mindex = {}, {}, {}
+ @docs =
+ (str.split(/^= (.+?) =/)[1..-1]/2).map do |k,v|
+ sparts = v.split(/^== (.+?) ==/)
+
+ sections = (sparts[1..-1]/2).map do |k2,v2|
+ meth = v2.split(/^=== (.+?) ===/)
+ k2t = k2[/^(?:The )?([\-\w]+)/, 1]
+ meth_plain = meth[0].gsub(IMAGE_RE, '')
+ @search.add_document :uri => "T #{k2t}", :body => "#{k2}\n#{meth_plain}".downcase
+
+ hsh = {'title' => k2, 'section' => k,
+ 'description' => meth[0],
+ 'methods' => (meth[1..-1]/2).map { |_k,_v|
+ @search.add_document :uri => "M #{k}#{COLON}#{k2t}#{COLON}#{_k}", :body => "#{_k}\n#{_v}".downcase
+ @mindex["#{k2t}.#{_k[/[\w\.]+/]}"] = [k2t, _k]
+ [_k, _v]
+ }}
+ @methods[k2t] = hsh
+ [k2t, hsh]
+ end
+
+ @search.add_document :uri => "S #{k}", :body => "#{k}\n#{sparts[0]}".downcase
+ hsh = {'description' => sparts[0], 'sections' => sections,
+ 'class' => "toc" + k.downcase.gsub(/\W+/, '')}
+ @sections[k] = hsh
+ [k, hsh]
+ end
+ @search.finish!
+ @docs
+ end
+
+ def show_search
+ @toc.each { |k,v| v.hide }
+ @title.replace "Search"
+ @doc.clear do
+ dewikify_p :para, "Try method names (like `button` or `arrow`) or topics (like `slots`)", :align => 'center'
+ flow :margin_left => 60 do
+ edit_line :width => -60 do |terms|
+ @results.clear do
+ termd = terms.text.downcase
+ #found = termd.empty? ? [] : manual_search(termd)
+ found = (termd.empty? or termd[0] == 'z' or termd[0] == 'y') ? [] : manual_search(termd)
+ para "#{found.length} matches", :align => "center", :margin_bottom => 0
+ found.each do |typ, head|
+ flow :margin => 4 do
+ case typ
+ when "S"
+ background "#333", :curve => 4
+ caption strong(link(head, :stroke => white) { open_section(head, terms.text) })
+ para "Section header", Shoes::Manual::SUB_STYLE
+ when "T"
+ background "#777", :curve => 4
+ caption strong(link(head, :stroke => "#EEE") { open_methods(head, terms.text) })
+ hsh = @methods[head]
+ para "Sub-section under #{hsh['section']} (#{hsh['methods'].length} methods)", Shoes::Manual::SUB_STYLE
+ when "M"
+ background "#CCC", :curve => 4
+ sect, subhead, head = head.split(Shoes::Manual::COLON, 3)
+ para strong(sect, Shoes::Manual::COLON, subhead, Shoes::Manual::COLON, link(head) { open_methods(subhead, terms.text, head) })
+ end
+ end
+ end
+ end
+ end
+ end
+ @results = stack
+ end
+ app.slot.scroll_top = 0
+ end
+
+ def open_link(head)
+ if head == "Search"
+ show_search
+ elsif @sections.has_key? head
+ open_section(head)
+ elsif @methods.has_key? head
+ open_methods(head)
+ elsif @mindex.has_key? head
+ head, sub = @mindex[head]
+ open_methods(head, nil, sub)
+ elsif head =~ /^http:\/\//
+ debug head
+ visit head
+ end
+ end
+
+ def add_next_link(docn, optn)
+ opt1, optn = @docs[docn][1], optn + 1
+ if opt1['sections'][optn]
+ @doc.para "Next: ",
+ link(opt1['sections'][optn][1]['title']) { open_methods(opt1['sections'][optn][0]) },
+ :align => "right"
+ elsif @docs[docn + 1]
+ @doc.para "Next: ",
+ link(@docs[docn + 1][0]) { open_section(@docs[docn + 1][0].gsub(/\W/, '')) },
+ :align => "right"
+ end
+ end
+
+ def open_section(sect_s, terms = nil)
+ sect_h = @sections[sect_s]
+ sect_cls = sect_h['class']
+ @toc.each { |k,v| v.send(k == sect_cls ? :show : :hide) }
+ @title.replace sect_s
+ @doc.clear(&dewikify_hi(sect_h['description'], terms, true))
+ add_next_link(@docs.index { |x,| x == sect_s }, -1) rescue nil
+ app.slot.scroll_top = 0
+ end
+
+ def open_methods(meth_s, terms = nil, meth_a = nil)
+ meth_h = @methods[meth_s]
+ @title.replace meth_h['title']
+ @doc.clear do
+ unless meth_a
+ instance_eval &dewikify_hi(meth_h['description'], terms, true)
+ end
+ meth_h['methods'].each do |mname, expl|
+ if meth_a.nil? or meth_a == mname
+ sig, val = mname.split("»", 2)
+ stack(:margin_top => 8, :margin_bottom => 8) {
+ background "#333".."#666", :curve => 3, :angle => 90
+ tagline sig, (span("»", val, :stroke => "#BBB") if val), :margin => 4 }
+ instance_eval &dewikify_hi(expl, terms)
+ end
+ end
+ end
+ optn = nil
+ docn = @docs.index { |_,h| optn = h['sections'].index { |x,| x == meth_s } } rescue nil
+ add_next_link(docn, optn) if docn
+ app.slot.scroll_top = 0
+ end
+
+ def manual_search(terms)
+ terms += " " if terms.length == 1
+ @search.find_all(terms).map do |title, count|
+ title.split(" ", 2)
+ end
+ end
+
+ def make_html(path, title, menu, &blk)
+ require 'hpricot'
+ File.open(path, 'w') do |f|
+ f << Hpricot do
+ xhtml_transitional do
+ head do
+ meta :"http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8"
+ title "The Shoes Manual // #{title}"
+ script :type => "text/javascript", :src => "static/code_highlighter.js"
+ script :type => "text/javascript", :src => "static/code_highlighter_ruby.js"
+ style :type => "text/css" do
+ text "@import 'static/manual.css';"
+ end
+ end
+ body do
+ div.main! do
+ div.manual! &blk
+ div.sidebar do
+ img :src => "static/shoes-icon.png"
+ ul do
+ li { a.prime "HELP", :href => "./" }
+ menu.each do |m, sm|
+ li do
+ a m, :href => "#{m[/^\w+/]}.html"
+ if sm
+ ul.sub do
+ sm.each { |smm| li { a smm, :href => "#{smm}.html" } }
+ end
+ end
+ end
+ end
+
+ end
+ end
+ end
+ end
+ end
+ end.to_html
+ end
+ end
+end
+
+def Shoes.make_help_page
+ font "#{DIR}/fonts/Coolvetica.ttf" unless Shoes::FONTS.include? "Coolvetica"
+ proc do
+ extend Shoes::Manual
+ docs = load_docs Shoes::Manual.path
+
+ style(Shoes::Image, :margin => 8, :margin_left => 100)
+ style(Shoes::Code, :stroke => "#C30")
+ style(Shoes::LinkHover, :stroke => green, :fill => nil)
+ style(Shoes::Para, :size => 12, :stroke => "#332")
+ style(Shoes::Tagline, :size => 12, :weight => "bold", :stroke => "#eee", :margin => 6)
+ style(Shoes::Caption, :size => 24)
+ background "#ddd".."#fff", :angle => 90
+
+ [Shoes::LinkHover, Shoes::Para, Shoes::Tagline, Shoes::Caption].each do |type|
+ style(type, :font => "MS UI Gothic")
+ end if Shoes.language == 'ja'
+
+ stack do
+ background black
+ stack :margin_left => 118 do
+ para "The Shoes Manual", :stroke => "#eee", :margin_top => 8, :margin_left => 17,
+ :margin_bottom => 0
+ @title = title docs[0][0], :stroke => white, :margin => 4, :margin_left => 14,
+ :margin_top => 0, :font => "Coolvetica"
+ end
+ background "rgb(66, 66, 66, 180)".."rgb(0, 0, 0, 0)", :height => 0.7
+ background "rgb(66, 66, 66, 100)".."rgb(255, 255, 255, 0)", :height => 20, :bottom => 0
+ end
+ @doc =
+ stack :margin_left => 130, :margin_top => 20, :margin_bottom => 50, :margin_right => 50 + gutter,
+ &dewikify(docs[0][-1]['description'], true)
+ add_next_link(0, -1)
+ stack :top => 80, :left => 0, :attach => Shoes::Window do
+ @toc = {}
+ stack :margin => 12, :width => 130, :margin_top => 20 do
+ docs.each do |sect_s, sect_h|
+ sect_cls = sect_h['class']
+ para strong(link(sect_s, :stroke => black) { open_section(sect_s) }),
+ :size => 11, :margin => 4, :margin_top => 0
+ @toc[sect_cls] =
+ stack :hidden => @toc.empty? ? false : true do
+ links = sect_h['sections'].map do |meth_s, meth_h|
+ [link(meth_s) { open_methods(meth_s) }, "\n"]
+ end.flatten
+ links[-1] = {:size => 9, :margin => 4, :margin_left => 10}
+ para *links
+ end
+ end
+ end
+ stack :margin => 12, :width => 118, :margin_top => 6 do
+ background "#330", :curve => 4
+ para "Not finding it? Try ", strong(link("Search", :stroke => white) { show_search }), "!", :stroke => "#ddd", :size => 9, :align => "center", :margin => 6
+ end
+ stack :margin => 12, :width => 118 do
+ inscription "Shoes #{Shoes::RELEASE_NAME}\nRevision: #{Shoes::REVISION}",
+ :size => 7, :align => "center", :stroke => "#999"
+ end
+ end
+ image :width => 120, :height => 120, :top => -18, :left => 6 do
+ image "#{DIR}/static/shoes-icon.png", :width => 100, :height => 100, :top => 10, :left => 10
+ glow 2
+ end
+ end
+rescue => e
+ p e.message
+ p e.class
+end
+
+Shoes::Help = Shoes.make_help_page
diff --git a/lib/shoes/image.rb b/lib/shoes/image.rb
new file mode 100644
index 0000000..c7dc3ef
--- /dev/null
+++ b/lib/shoes/image.rb
@@ -0,0 +1,25 @@
+require 'digest/sha1'
+
+class Shoes
+ def self.image_temp_path uri, uext
+ File.join(Dir::tmpdir, "#{uri.host}-#{Time.now.usec}" + uext)
+ end
+ def self.image_cache_path hash, ext
+ dir = File.join(CACHE_DIR, hash[0,2])
+ Dir.mkdir(dir) unless File.exists?(dir)
+ File.join(dir, hash[2..-1]) + ext.downcase
+ end
+ def snapshot(options = {}, &block)
+ options[:format] ||= :svg
+
+ options[:filename] ||= ( tf_path = ( require 'tempfile'
+ tf = Tempfile.new(File.basename(__FILE__)).path ))
+
+ _snapshot options do
+ block.call
+ end
+ return File.read(options[:filename])
+ ensure
+ File.unlink(tf_path) if tf_path
+ end
+end
diff --git a/lib/shoes/inspect.rb b/lib/shoes/inspect.rb
new file mode 100644
index 0000000..3a2c637
--- /dev/null
+++ b/lib/shoes/inspect.rb
@@ -0,0 +1,128 @@
+module Kernel
+ def inspect(hits = {})
+ return "(#{self.class} ...)" if hits[self]
+ hits[self] = true
+ if instance_variables.empty?
+ "(#{self.class})"
+ else
+ "(#{self.class} " +
+ instance_variables.map do |x|
+ v = instance_variable_get(x)
+ "#{x}=" + (v.method(:inspect).arity == 0 ? v.inspect : v.inspect(hits))
+ end.join(' ') +
+ ")"
+ end
+ end
+ #def to_html
+ # obj = self
+ # Web.Bit {
+ # h1 "A #{obj.class}"
+ # }
+ #end
+ def to_s; inspect end
+end
+
+class Array
+ def inspect(hits = {})
+ return "[...]" if hits[self]
+ hits[self] = true
+ "[" + map { |x| x.method(:inspect).arity == 0 ? x.inspect : x.inspect(hits) }.join(', ') + "]"
+ end
+ def to_html
+ ary = self
+ Web.Bit {
+ h5 "A List of Things"
+ h1 "An Array"
+ unless ary.empty?
+ ol {
+ ary.map { |x| li { self << HTML(x) } }
+ }
+ end
+ }
+ end
+ def / len
+ a = []
+ each_with_index do |x, i|
+ a << [] if i % len == 0
+ a.last << x
+ end
+ a
+ end
+end
+
+class Hash
+ def inspect(hits = {})
+ return "{...}" if hits[self]
+ hits[self] = true
+ mappings = map do |k,v|
+ key = k.method(:inspect).arity == 0 ? k.inspect : k.inspect(hits)
+ val = v.method(:inspect).arity == 0 ? v.inspect : v.inspect(hits)
+ "#{key} => #{val}"
+ end
+ "{ #{mappings.join(', ')} }"
+ end
+ def to_html
+ h = self
+ Web.Bit {
+ h5 "Pairs of Things"
+ h1 "A Hash"
+ unless h.empty?
+ ul {
+ h.each { |k, v| li { self << "<div class='hashkey'>#{HTML(k)}</div><div class='hashvalue'>#{HTML(v)}</div>" } }
+ }
+ end
+ }
+ end
+end
+
+class File
+ def inspect(hits = nil)
+ "(#{self.class} #{path})"
+ end
+end
+
+class Proc
+ def inspect(hits = nil)
+ v = "a"
+ pvars = []
+ (arity < 0 ? -(arity+1) : arity).times do |i|
+ pvars << v
+ v = v.succ
+ end
+ pvars << "*#{v}" if arity < 0
+ "(Proc |#{pvars.join(',')}|)"
+ end
+end
+
+class Class
+ def make_inspect m = :inspect
+ alias_method :the_original_inspect, m
+ class_eval %{
+ def inspect(hits = nil)
+ the_original_inspect
+ end
+ }
+ #def to_html(hits = nil)
+ # #{m} + " <div class='classname'>" + self.class.name + "</div>"
+ #end
+ end
+end
+
+class Module; make_inspect :name end
+class Regexp; make_inspect end
+class String; make_inspect end
+class Symbol; make_inspect end
+class Time; make_inspect end
+class Numeric; make_inspect :to_s end
+class Bignum; make_inspect :to_s end
+class Fixnum; make_inspect :to_s end
+class Float; make_inspect :to_s end
+class TrueClass; make_inspect :to_s end
+class FalseClass; make_inspect :to_s end
+class NilClass; make_inspect end
+
+class Shoes::Types::App
+ def inspect(hits = nil)
+ "(#{self.class} #{name.dump})"
+ end
+end
diff --git a/lib/shoes/log.rb b/lib/shoes/log.rb
new file mode 100644
index 0000000..602e688
--- /dev/null
+++ b/lib/shoes/log.rb
@@ -0,0 +1,48 @@
+module Shoes::LogWindow
+ def setup
+ stack do
+ flow do
+ background black
+ stack :width => -100 do
+ tagline "Shoes Console", :stroke => white
+ end
+ button "Clear", :margin => 6, :width => 80, :height => 40 do
+ Shoes.log.clear
+ end
+ end
+ @log, @hash = stack, nil
+ update
+ every(0.2) do
+ update
+ end
+ end
+ end
+ def update
+ if @hash != Shoes.log.hash
+ @hash = Shoes.log.hash
+ @log.clear do
+ i = 0
+ Shoes.log.each do |typ, msg, at, mid, rbf, rbl|
+ stack do
+ background "#f1f5e1" if i % 2 == 0
+ inscription strong(typ.to_s.capitalize, :stroke => "#05C"), " in ",
+ span(rbf, " line ", rbl, :stroke => "#335"), " | ",
+ span(at, :stroke => "#777"),
+ :stroke => "#059", :margin => 4, :margin_bottom => 0
+ flow do
+ stack :margin => 6, :width => 20 do
+ image "#{DIR}/static/icon-#{typ}.png"
+ end
+ stack :margin => 4, :width => -20 do
+ s = msg.to_s.force_encoding "UTF-8"
+ s << "\n#{msg.backtrace.join("\n")}" if msg.kind_of?(Exception)
+ para s, :margin => 4, :margin_top => 0
+ end
+ end
+ end
+ i += 1
+ end
+ end
+ end
+ end
+end
diff --git a/lib/shoes/minitar.rb b/lib/shoes/minitar.rb
new file mode 100644
index 0000000..87617f9
--- /dev/null
+++ b/lib/shoes/minitar.rb
@@ -0,0 +1,986 @@
+#!/usr/bin/env ruby
+#--
+# Archive::Tar::Minitar 0.5.1
+# Copyright © 2004 Mauricio Julio Fernández Pradier and Austin Ziegler
+#
+# This program is based on and incorporates parts of RPA::Package from
+# rpa-base (lib/rpa/package.rb and lib/rpa/util.rb) by Mauricio and has been
+# adapted to be more generic by Austin.
+#
+# It is licensed under the GNU General Public Licence or Ruby's licence.
+#
+# $Id: minitar.rb,v 1.3 2004/09/22 17:47:43 austin Exp $
+#++
+
+module Archive; end
+module Archive::Tar; end
+
+ # = Archive::Tar::PosixHeader
+ # Implements the POSIX tar header as a Ruby class. The structure of
+ # the POSIX tar header is:
+ #
+ # struct tarfile_entry_posix
+ # { // pack/unpack
+ # char name[100]; // ASCII (+ Z unless filled) a100/Z100
+ # char mode[8]; // 0 padded, octal, null a8 /A8
+ # char uid[8]; // ditto a8 /A8
+ # char gid[8]; // ditto a8 /A8
+ # char size[12]; // 0 padded, octal, null a12 /A12
+ # char mtime[12]; // 0 padded, octal, null a12 /A12
+ # char checksum[8]; // 0 padded, octal, null, space a8 /A8
+ # char typeflag[1]; // see below a /a
+ # char linkname[100]; // ASCII + (Z unless filled) a100/Z100
+ # char magic[6]; // "ustar\0" a6 /A6
+ # char version[2]; // "00" a2 /A2
+ # char uname[32]; // ASCIIZ a32 /Z32
+ # char gname[32]; // ASCIIZ a32 /Z32
+ # char devmajor[8]; // 0 padded, octal, null a8 /A8
+ # char devminor[8]; // 0 padded, octal, null a8 /A8
+ # char prefix[155]; // ASCII (+ Z unless filled) a155/Z155
+ # };
+ #
+ # The +typeflag+ may be one of the following known values:
+ #
+ # <tt>"0"</tt>:: Regular file. NULL should be treated as a synonym, for
+ # compatibility purposes.
+ # <tt>"1"</tt>:: Hard link.
+ # <tt>"2"</tt>:: Symbolic link.
+ # <tt>"3"</tt>:: Character device node.
+ # <tt>"4"</tt>:: Block device node.
+ # <tt>"5"</tt>:: Directory.
+ # <tt>"6"</tt>:: FIFO node.
+ # <tt>"7"</tt>:: Reserved.
+ #
+ # POSIX indicates that "A POSIX-compliant implementation must treat any
+ # unrecognized typeflag value as a regular file."
+class Archive::Tar::PosixHeader
+ FIELDS = %w(name mode uid gid size mtime checksum typeflag linkname) +
+ %w(magic version uname gname devmajor devminor prefix)
+
+ FIELDS.each { |field| attr_reader field.intern }
+
+ HEADER_PACK_FORMAT = "a100a8a8a8a12a12a7aaa100a6a2a32a32a8a8a155"
+ HEADER_UNPACK_FORMAT = "Z100A8A8A8A12A12A8aZ100A6A2Z32Z32A8A8Z155"
+
+ # Creates a new PosixHeader from a data stream.
+ def self.new_from_stream(stream)
+ data = stream.read(512)
+ fields = data.unpack(HEADER_UNPACK_FORMAT)
+ name = fields.shift
+ mode = fields.shift.oct
+ uid = fields.shift.oct
+ gid = fields.shift.oct
+ size = fields.shift.oct
+ mtime = fields.shift.oct
+ checksum = fields.shift.oct
+ typeflag = fields.shift
+ linkname = fields.shift
+ magic = fields.shift
+ version = fields.shift.oct
+ uname = fields.shift
+ gname = fields.shift
+ devmajor = fields.shift.oct
+ devminor = fields.shift.oct
+ prefix = fields.shift
+
+ empty = (data == "\0" * 512)
+
+ new(:name => name, :mode => mode, :uid => uid, :gid => gid,
+ :size => size, :mtime => mtime, :checksum => checksum,
+ :typeflag => typeflag, :magic => magic, :version => version,
+ :uname => uname, :gname => gname, :devmajor => devmajor,
+ :devminor => devminor, :prefix => prefix, :empty => empty)
+ end
+
+ # Creates a new PosixHeader. A PosixHeader cannot be created unless the
+ # #name, #size, #prefix, and #mode are provided.
+ def initialize(vals)
+ unless vals[:name] && vals[:size] && vals[:prefix] && vals[:mode]
+ raise ArgumentError
+ end
+
+ vals[:mtime] ||= 0
+ vals[:checksum] ||= ""
+ vals[:typeflag] ||= "0"
+ vals[:magic] ||= "ustar"
+ vals[:version] ||= "00"
+
+ FIELDS.each do |field|
+ instance_variable_set("@#{field}", vals[field.intern])
+ end
+ @empty = vals[:empty]
+ end
+
+ def empty?
+ @empty
+ end
+
+ def to_s
+ update_checksum
+ header(@checksum)
+ end
+
+ # Update the checksum field.
+ def update_checksum
+ hh = header(" " * 8)
+ @checksum = oct(calculate_checksum(hh), 6)
+ end
+
+ private
+ def oct(num, len)
+ if num.nil?
+ "\0" * (len + 1)
+ else
+ "%0#{len}o" % num
+ end
+ end
+
+ def calculate_checksum(hdr)
+ hdr.unpack("C*").inject { |aa, bb| aa + bb }
+ end
+
+ def header(chksum)
+ arr = [name, oct(mode, 7), oct(uid, 7), oct(gid, 7), oct(size, 11),
+ oct(mtime, 11), chksum, " ", typeflag, linkname, magic, version,
+ uname, gname, oct(devmajor, 7), oct(devminor, 7), prefix]
+ str = arr.pack(HEADER_PACK_FORMAT)
+ str + "\0" * ((512 - str.size) % 512)
+ end
+end
+
+require 'fileutils'
+require 'find'
+
+ # = Archive::Tar::Minitar 0.5.1
+ # Archive::Tar::Minitar is a pure-Ruby library and command-line
+ # utility that provides the ability to deal with POSIX tar(1) archive
+ # files. The implementation is based heavily on Mauricio Fernández's
+ # implementation in rpa-base, but has been reorganised to promote
+ # reuse in other projects.
+ #
+ # This tar class performs a subset of all tar (POSIX tape archive)
+ # operations. We can only deal with typeflags 0, 1, 2, and 5 (see
+ # Archive::Tar::PosixHeader). All other typeflags will be treated as
+ # normal files.
+ #
+ # NOTE::: support for typeflags 1 and 2 is not yet implemented in this
+ # version.
+ #
+ # This release is version 0.5.1. The library can only handle files and
+ # directories at this point. A future version will be expanded to
+ # handle symbolic links and hard links in a portable manner. The
+ # command line utility, minitar, can only create archives, extract
+ # from archives, and list archive contents.
+ #
+ # == Synopsis
+ # Using this library is easy. The simplest case is:
+ #
+ # require 'zlib'
+ # require 'archive/tar/minitar'
+ # include Archive::Tar
+ #
+ # # Packs everything that matches Find.find('tests')
+ # File.open('test.tar', 'wb') { |tar| Minitar.pack('tests', tar) }
+ # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
+ # Minitar.unpack('test.tar', 'x')
+ #
+ # A gzipped tar can be written with:
+ #
+ # tgz = Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
+ # # Warning: tgz will be closed!
+ # Minitar.pack('tests', tgz)
+ #
+ # tgz = Zlib::GzipReader.new(File.open('test.tgz', 'rb'))
+ # # Warning: tgz will be closed!
+ # Minitar.unpack(tgz, 'x')
+ #
+ # As the case above shows, one need not write to a file. However, it
+ # will sometimes require that one dive a little deeper into the API,
+ # as in the case of StringIO objects. Note that I'm not providing a
+ # block with Minitar::Output, as Minitar::Output#close automatically
+ # closes both the Output object and the wrapped data stream object.
+ #
+ # begin
+ # sgz = Zlib::GzipWriter.new(StringIO.new(""))
+ # tar = Output.new(sgz)
+ # Find.find('tests') do |entry|
+ # Minitar.pack_file(entry, tar)
+ # end
+ # ensure
+ # # Closes both tar and sgz.
+ # tar.close
+ # end
+ #
+ # == Copyright
+ # Copyright 2004 Mauricio Julio Fernández Pradier and Austin Ziegler
+ #
+ # This program is based on and incorporates parts of RPA::Package from
+ # rpa-base (lib/rpa/package.rb and lib/rpa/util.rb) by Mauricio and
+ # has been adapted to be more generic by Austin.
+ #
+ # 'minitar' contains an adaptation of Ruby/ProgressBar by Satoru
+ # Takabayashi <satoru@namazu.org>, copyright © 2001 - 2004.
+ #
+ # This program is free software. It may be redistributed and/or
+ # modified under the terms of the GPL version 2 (or later) or Ruby's
+ # licence.
+module Archive::Tar::Minitar
+ VERSION = "0.5.1"
+
+ # The exception raised when a wrapped data stream class is expected to
+ # respond to #rewind or #pos but does not.
+ class NonSeekableStream < StandardError; end
+ # The exception raised when a block is required for proper operation of
+ # the method.
+ class BlockRequired < ArgumentError; end
+ # The exception raised when operations are performed on a stream that has
+ # previously been closed.
+ class ClosedStream < StandardError; end
+ # The exception raised when a filename exceeds 256 bytes in length,
+ # the maximum supported by the standard Tar format.
+ class FileNameTooLong < StandardError; end
+ # The exception raised when a data stream ends before the amount of data
+ # expected in the archive's PosixHeader.
+ class UnexpectedEOF < StandardError; end
+
+ # The class that writes a tar format archive to a data stream.
+ class Writer
+ # A stream wrapper that can only be written to. Any attempt to read
+ # from this restricted stream will result in a NameError being thrown.
+ class RestrictedStream
+ def initialize(anIO)
+ @io = anIO
+ end
+
+ def write(data)
+ @io.write(data)
+ end
+ end
+
+ # A RestrictedStream that also has a size limit.
+ class BoundedStream < Archive::Tar::Minitar::Writer::RestrictedStream
+ # The exception raised when the user attempts to write more data to
+ # a BoundedStream than has been allocated.
+ class FileOverflow < RuntimeError; end
+
+ # The maximum number of bytes that may be written to this data
+ # stream.
+ attr_reader :limit
+ # The current total number of bytes written to this data stream.
+ attr_reader :written
+
+ def initialize(io, limit)
+ @io = io
+ @limit = limit
+ @written = 0
+ end
+
+ def write(data)
+ raise FileOverflow if (data.size + @written) > @limit
+ @io.write(data)
+ @written += data.size
+ data.size
+ end
+ end
+
+ # With no associated block, +Writer::open+ is a synonym for
+ # +Writer::new+. If the optional code block is given, it will be
+ # passed the new _writer_ as an argument and the Writer object will
+ # automatically be closed when the block terminates. In this instance,
+ # +Writer::open+ returns the value of the block.
+ def self.open(anIO)
+ writer = Writer.new(anIO)
+
+ return writer unless block_given?
+
+ begin
+ res = yield writer
+ ensure
+ writer.close
+ end
+
+ res
+ end
+
+ # Creates and returns a new Writer object.
+ def initialize(anIO)
+ @io = anIO
+ @closed = false
+ end
+
+ # Adds a file to the archive as +name+. +opts+ must contain the
+ # following values:
+ #
+ # <tt>:mode</tt>:: The Unix file permissions mode value.
+ # <tt>:size</tt>:: The size, in bytes.
+ #
+ # +opts+ may contain the following values:
+ #
+ # <tt>:uid</tt>: The Unix file owner user ID number.
+ # <tt>:gid</tt>: The Unix file owner group ID number.
+ # <tt>:mtime</tt>:: The *integer* modification time value.
+ #
+ # It will not be possible to add more than <tt>opts[:size]</tt> bytes
+ # to the file.
+ def add_file_simple(name, opts = {}) # :yields BoundedStream:
+ raise Archive::Tar::Minitar::BlockRequired unless block_given?
+ raise Archive::Tar::ClosedStream if @closed
+
+ name, prefix = split_name(name)
+
+ header = { :name => name, :mode => opts[:mode], :mtime => opts[:mtime],
+ :size => opts[:size], :gid => opts[:gid], :uid => opts[:uid],
+ :prefix => prefix }
+ header = Archive::Tar::PosixHeader.new(header).to_s
+ @io.write(header)
+
+ os = BoundedStream.new(@io, opts[:size])
+ yield os
+ # FIXME: what if an exception is raised in the block?
+
+ min_padding = opts[:size] - os.written
+ @io.write("\0" * min_padding)
+ remainder = (512 - (opts[:size] % 512)) % 512
+ @io.write("\0" * remainder)
+ end
+
+ # Adds a file to the archive as +name+. +opts+ must contain the
+ # following value:
+ #
+ # <tt>:mode</tt>:: The Unix file permissions mode value.
+ #
+ # +opts+ may contain the following values:
+ #
+ # <tt>:uid</tt>: The Unix file owner user ID number.
+ # <tt>:gid</tt>: The Unix file owner group ID number.
+ # <tt>:mtime</tt>:: The *integer* modification time value.
+ #
+ # The file's size will be determined from the amount of data written
+ # to the stream.
+ #
+ # For #add_file to be used, the Archive::Tar::Minitar::Writer must be
+ # wrapping a stream object that is seekable (e.g., it responds to
+ # #pos=). Otherwise, #add_file_simple must be used.
+ #
+ # +opts+ may be modified during the writing to the stream.
+ def add_file(name, opts = {}) # :yields RestrictedStream, +opts+:
+ raise Archive::Tar::Minitar::BlockRequired unless block_given?
+ raise Archive::Tar::Minitar::ClosedStream if @closed
+ raise Archive::Tar::Minitar::NonSeekableStream unless @io.respond_to?(:pos=)
+
+ name, prefix = split_name(name)
+ init_pos = @io.pos
+ @io.write("\0" * 512) # placeholder for the header
+
+ yield RestrictedStream.new(@io), opts
+ # FIXME: what if an exception is raised in the block?
+
+ size = @io.pos - (init_pos + 512)
+ remainder = (512 - (size % 512)) % 512
+ @io.write("\0" * remainder)
+
+ final_pos = @io.pos
+ @io.pos = init_pos
+
+ header = { :name => name, :mode => opts[:mode], :mtime => opts[:mtime],
+ :size => size, :gid => opts[:gid], :uid => opts[:uid],
+ :prefix => prefix }
+ header = Archive::Tar::PosixHeader.new(header).to_s
+ @io.write(header)
+ @io.pos = final_pos
+ end
+
+ # Creates a directory in the tar.
+ def mkdir(name, opts = {})
+ raise ClosedStream if @closed
+ name, prefix = split_name(name)
+ header = { :name => name, :mode => opts[:mode], :typeflag => "5",
+ :size => 0, :gid => opts[:gid], :uid => opts[:uid],
+ :mtime => opts[:mtime], :prefix => prefix }
+ header = Archive::Tar::PosixHeader.new(header).to_s
+ @io.write(header)
+ nil
+ end
+
+ # Passes the #flush method to the wrapped stream, used for buffered
+ # streams.
+ def flush
+ raise ClosedStream if @closed
+ @io.flush if @io.respond_to?(:flush)
+ end
+
+ # Closes the Writer.
+ def close
+ return if @closed
+ @io.write("\0" * 1024)
+ @closed = true
+ end
+
+ private
+ def split_name(name)
+ raise FileNameTooLong if name.size > 256
+ if name.size <= 100
+ prefix = ""
+ else
+ parts = name.split(/\//)
+ newname = parts.pop
+
+ nxt = ""
+
+ loop do
+ nxt = parts.pop
+ break if newname.size + 1 + nxt.size > 100
+ newname = "#{nxt}/#{newname}"
+ end
+
+ prefix = (parts + [nxt]).join("/")
+
+ name = newname
+
+ raise FileNameTooLong if name.size > 100 || prefix.size > 155
+ end
+ return name, prefix
+ end
+ end
+
+ # The class that reads a tar format archive from a data stream. The data
+ # stream may be sequential or random access, but certain features only work
+ # with random access data streams.
+ class Reader
+ # This marks the EntryStream closed for reading without closing the
+ # actual data stream.
+ module InvalidEntryStream
+ def read(len = nil); raise ClosedStream; end
+ def getc; raise ClosedStream; end
+ def rewind; raise ClosedStream; end
+ end
+
+ # EntryStreams are pseudo-streams on top of the main data stream.
+ class EntryStream
+ Archive::Tar::PosixHeader::FIELDS.each do |field|
+ attr_reader field.intern
+ end
+
+ def initialize(header, anIO)
+ @io = anIO
+ @name = header.name
+ @mode = header.mode
+ @uid = header.uid
+ @gid = header.gid
+ @size = header.size
+ @mtime = header.mtime
+ @checksum = header.checksum
+ @typeflag = header.typeflag
+ @linkname = header.linkname
+ @magic = header.magic
+ @version = header.version
+ @uname = header.uname
+ @gname = header.gname
+ @devmajor = header.devmajor
+ @devminor = header.devminor
+ @prefix = header.prefix
+ @read = 0
+ @orig_pos = @io.pos
+ end
+
+ # Reads +len+ bytes (or all remaining data) from the entry. Returns
+ # +nil+ if there is no more data to read.
+ def read(len = nil)
+ return nil if @read >= @size
+ len ||= @size - @read
+ max_read = [len, @size - @read].min
+ ret = @io.read(max_read)
+ @read += ret.size
+ ret
+ end
+
+ # Reads one byte from the entry. Returns +nil+ if there is no more data
+ # to read.
+ def getc
+ return nil if @read >= @size
+ ret = @io.getc
+ @read += 1 if ret
+ ret
+ end
+
+ # Returns +true+ if the entry represents a directory.
+ def directory?
+ @typeflag == "5"
+ end
+ alias_method :directory, :directory?
+
+ # Returns +true+ if the entry represents a plain file.
+ def file?
+ @typeflag == "0"
+ end
+ alias_method :file, :file?
+
+ # Returns +true+ if the current read pointer is at the end of the
+ # EntryStream data.
+ def eof?
+ @read >= @size
+ end
+
+ # Returns the current read pointer in the EntryStream.
+ def pos
+ @read
+ end
+
+ # Sets the current read pointer to the beginning of the EntryStream.
+ def rewind
+ raise NonSeekableStream unless @io.respond_to?(:pos=)
+ @io.pos = @orig_pos
+ @read = 0
+ end
+
+ def orig_pos
+ @orig_pos
+ end
+
+ def bytes_read
+ @read
+ end
+
+ # Returns the full and proper name of the entry.
+ def full_name
+ if @prefix != ""
+ File.join(@prefix, @name)
+ else
+ @name
+ end
+ end
+
+ # Closes the entry.
+ def close
+ invalidate
+ end
+
+ private
+ def invalidate
+ extend InvalidEntryStream
+ end
+ end
+
+ # With no associated block, +Reader::open+ is a synonym for
+ # +Reader::new+. If the optional code block is given, it will be passed
+ # the new _writer_ as an argument and the Reader object will
+ # automatically be closed when the block terminates. In this instance,
+ # +Reader::open+ returns the value of the block.
+ def self.open(anIO)
+ reader = Reader.new(anIO)
+
+ return reader unless block_given?
+
+ begin
+ res = yield reader
+ ensure
+ reader.close
+ end
+
+ res
+ end
+
+ # Creates and returns a new Reader object.
+ def initialize(anIO)
+ @io = anIO
+ @init_pos = anIO.pos
+ end
+
+ # Iterates through each entry in the data stream.
+ def each(&block)
+ each_entry(&block)
+ end
+
+ # Resets the read pointer to the beginning of data stream. Do not call
+ # this during a #each or #each_entry iteration. This only works with
+ # random access data streams that respond to #rewind and #pos.
+ def rewind
+ if @init_pos == 0
+ raise NonSeekableStream unless @io.respond_to?(:rewind)
+ @io.rewind
+ else
+ raise NonSeekableStream unless @io.respond_to?(:pos=)
+ @io.pos = @init_pos
+ end
+ end
+
+ # Iterates through each entry in the data stream.
+ def each_entry
+ loop do
+ return if @io.eof?
+
+ header = Archive::Tar::PosixHeader.new_from_stream(@io)
+ return if header.empty?
+
+ entry = EntryStream.new(header, @io)
+ size = entry.size
+
+ yield entry
+
+ skip = (512 - (size % 512)) % 512
+
+ if @io.respond_to?(:seek)
+ # avoid reading...
+ @io.seek(size - entry.bytes_read, IO::SEEK_CUR)
+ else
+ pending = size - entry.bytes_read
+ while pending > 0
+ bread = @io.read([pending, 4096].min).size
+ raise UnexpectedEOF if @io.eof?
+ pending -= bread
+ end
+ end
+ @io.read(skip) # discard trailing zeros
+ # make sure nobody can use #read, #getc or #rewind anymore
+ entry.close
+ end
+ end
+
+ def close
+ end
+ end
+
+ # Wraps a Archive::Tar::Minitar::Reader with convenience methods and
+ # wrapped stream management; Input only works with random access data
+ # streams. See Input::new for details.
+ class Input
+ include Enumerable
+
+ # With no associated block, +Input::open+ is a synonym for
+ # +Input::new+. If the optional code block is given, it will be passed
+ # the new _writer_ as an argument and the Input object will
+ # automatically be closed when the block terminates. In this instance,
+ # +Input::open+ returns the value of the block.
+ def self.open(input)
+ stream = Input.new(input)
+ return stream unless block_given?
+
+ begin
+ res = yield stream
+ ensure
+ stream.close
+ end
+
+ res
+ end
+
+ # Creates a new Input object. If +input+ is a stream object that responds
+ # to #read), then it will simply be wrapped. Otherwise, one will be
+ # created and opened using Kernel#open. When Input#close is called, the
+ # stream object wrapped will be closed.
+ def initialize(input)
+ if input.respond_to?(:read)
+ @io = input
+ else
+ @io = open(input, "rb")
+ end
+ @tarreader = Archive::Tar::Minitar::Reader.new(@io)
+ end
+
+ # Iterates through each entry and rewinds to the beginning of the stream
+ # when finished.
+ def each(&block)
+ @tarreader.each { |entry| yield entry }
+ ensure
+ @tarreader.rewind
+ end
+
+ # Extracts the current +entry+ to +destdir+. If a block is provided, it
+ # yields an +action+ Symbol, the full name of the file being extracted
+ # (+name+), and a Hash of statistical information (+stats+).
+ #
+ # The +action+ will be one of:
+ # <tt>:dir</tt>:: The +entry+ is a directory.
+ # <tt>:file_start</tt>:: The +entry+ is a file; the extract of the
+ # file is just beginning.
+ # <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract
+ # of the +entry+.
+ # <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
+ #
+ # The +stats+ hash contains the following keys:
+ # <tt>:current</tt>:: The current total number of bytes read in the
+ # +entry+.
+ # <tt>:currinc</tt>:: The current number of bytes read in this read
+ # cycle.
+ # <tt>:entry</tt>:: The entry being extracted; this is a
+ # Reader::EntryStream, with all methods thereof.
+ def extract_entry(destdir, entry) # :yields action, name, stats:
+ stats = {
+ :current => 0,
+ :currinc => 0,
+ :entry => entry
+ }
+
+ if entry.directory?
+ dest = File.join(destdir, entry.full_name)
+
+ yield :dir, entry.full_name, stats if block_given?
+
+ if Archive::Tar::Minitar.dir?(dest)
+ begin
+ FileUtils.chmod(entry.mode, dest)
+ rescue Exception
+ nil
+ end
+ else
+ FileUtils.mkdir_p(dest, :mode => entry.mode)
+ FileUtils.chmod(entry.mode, dest)
+ end
+
+ fsync_dir(dest)
+ fsync_dir(File.join(dest, ".."))
+ return
+ else # it's a file
+ destdir = File.join(destdir, File.dirname(entry.full_name))
+ FileUtils.mkdir_p(destdir, :mode => 0755)
+
+ destfile = File.join(destdir, File.basename(entry.full_name))
+ FileUtils.chmod(0600, destfile) rescue nil # Errno::ENOENT
+
+ yield :file_start, entry.full_name, stats if block_given?
+
+ File.open(destfile, "wb", entry.mode) do |os|
+ loop do
+ data = entry.read(4096)
+ break unless data
+
+ stats[:currinc] = os.write(data)
+ stats[:current] += stats[:currinc]
+
+ yield :file_progress, entry.full_name, stats if block_given?
+ end
+ os.fsync
+ end
+
+ FileUtils.chmod(entry.mode, destfile)
+ fsync_dir(File.dirname(destfile))
+ fsync_dir(File.join(File.dirname(destfile), ".."))
+
+ yield :file_done, entry.full_name, stats if block_given?
+ end
+ end
+
+ # Returns the Reader object for direct access.
+ def tar
+ @tarreader
+ end
+
+ # Closes the Reader object and the wrapped data stream.
+ def close
+ @io.close
+ @tarreader.close
+ end
+
+ private
+ def fsync_dir(dirname)
+ # make sure this hits the disc
+ dir = open(dirname, 'rb')
+ dir.fsync
+ rescue # ignore IOError if it's an unpatched (old) Ruby
+ nil
+ ensure
+ dir.close if dir rescue nil
+ end
+ end
+
+ # Wraps a Archive::Tar::Minitar::Writer with convenience methods and
+ # wrapped stream management; Output only works with random access data
+ # streams. See Output::new for details.
+ class Output
+ # With no associated block, +Output::open+ is a synonym for
+ # +Output::new+. If the optional code block is given, it will be passed
+ # the new _writer_ as an argument and the Output object will
+ # automatically be closed when the block terminates. In this instance,
+ # +Output::open+ returns the value of the block.
+ def self.open(output)
+ stream = Output.new(output)
+ return stream unless block_given?
+
+ begin
+ res = yield stream
+ ensure
+ stream.close
+ end
+
+ res
+ end
+
+ # Creates a new Output object. If +output+ is a stream object that
+ # responds to #read), then it will simply be wrapped. Otherwise, one will
+ # be created and opened using Kernel#open. When Output#close is called,
+ # the stream object wrapped will be closed.
+ def initialize(output)
+ if output.respond_to?(:write)
+ @io = output
+ else
+ @io = ::File.open(output, "wb")
+ end
+ @tarwriter = Archive::Tar::Minitar::Writer.new(@io)
+ end
+
+ # Returns the Writer object for direct access.
+ def tar
+ @tarwriter
+ end
+
+ # Closes the Writer object and the wrapped data stream.
+ def close
+ @tarwriter.close
+ @io.close
+ end
+ end
+
+ class << self
+ # Tests if +path+ refers to a directory. Fixes an apparently
+ # corrupted <tt>stat()</tt> call on Windows.
+ def dir?(path)
+ File.directory?((path[-1] == ?/) ? path : "#{path}/")
+ end
+
+ # A convenience method for wrapping Archive::Tar::Minitar::Input.open
+ # (mode +r+) and Archive::Tar::Minitar::Output.open (mode +w+). No other
+ # modes are currently supported.
+ def open(dest, mode = "r", &block)
+ case mode
+ when "r"
+ Input.open(dest, &block)
+ when "w"
+ Output.open(dest, &block)
+ else
+ raise "Unknown open mode for Archive::Tar::Minitar.open."
+ end
+ end
+
+ # A convenience method to packs the file provided. +entry+ may either be
+ # a filename (in which case various values for the file (see below) will
+ # be obtained from <tt>File#stat(entry)</tt> or a Hash with the fields:
+ #
+ # <tt>:name</tt>:: The filename to be packed into the tarchive.
+ # *REQUIRED*.
+ # <tt>:mode</tt>:: The mode to be applied.
+ # <tt>:uid</tt>:: The user owner of the file. (Ignored on Windows.)
+ # <tt>:gid</tt>:: The group owner of the file. (Ignored on Windows.)
+ # <tt>:mtime</tt>:: The modification Time of the file.
+ #
+ # During packing, if a block is provided, #pack_file yields an +action+
+ # Symol, the full name of the file being packed, and a Hash of
+ # statistical information, just as with
+ # Archive::Tar::Minitar::Input#extract_entry.
+ #
+ # The +action+ will be one of:
+ # <tt>:dir</tt>:: The +entry+ is a directory.
+ # <tt>:file_start</tt>:: The +entry+ is a file; the extract of the
+ # file is just beginning.
+ # <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract
+ # of the +entry+.
+ # <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
+ #
+ # The +stats+ hash contains the following keys:
+ # <tt>:current</tt>:: The current total number of bytes read in the
+ # +entry+.
+ # <tt>:currinc</tt>:: The current number of bytes read in this read
+ # cycle.
+ # <tt>:name</tt>:: The filename to be packed into the tarchive.
+ # *REQUIRED*.
+ # <tt>:mode</tt>:: The mode to be applied.
+ # <tt>:uid</tt>:: The user owner of the file. (+nil+ on Windows.)
+ # <tt>:gid</tt>:: The group owner of the file. (+nil+ on Windows.)
+ # <tt>:mtime</tt>:: The modification Time of the file.
+ def pack_file(entry, outputter) #:yields action, name, stats:
+ outputter = outputter.tar if outputter.kind_of?(Archive::Tar::Minitar::Output)
+
+ stats = {}
+
+ if entry.kind_of?(Hash)
+ name = entry[:name]
+
+ entry.each { |kk, vv| stats[kk] = vv unless vv.nil? }
+ else
+ name = entry
+ end
+
+ name = name.sub(%r{\./}, '')
+ stat = File.stat(name)
+ stats[:mode] ||= stat.mode
+ stats[:mtime] ||= stat.mtime
+ stats[:size] = stat.size
+ if name == "sh-install" # an ugly shoes-specific hack, to
+ stats[:mode] = 0755 # get the file to be 0755 on windows
+ end
+
+ if RUBY_PLATFORM =~ /win32/
+ stats[:uid] = nil
+ stats[:gid] = nil
+ else
+ stats[:uid] ||= stat.uid
+ stats[:gid] ||= stat.gid
+ end
+
+ case
+ when File.file?(name)
+ outputter.add_file_simple(name, stats) do |os|
+ stats[:current] = 0
+ yield :file_start, name, stats if block_given?
+ File.open(name, "rb") do |ff|
+ until ff.eof?
+ stats[:currinc] = os.write(ff.read(4096))
+ stats[:current] += stats[:currinc]
+ yield :file_progress, name, stats if block_given?
+ end
+ end
+ yield :file_done, name, stats if block_given?
+ end
+ when dir?(name)
+ yield :dir, name, stats if block_given?
+ outputter.mkdir(name, stats)
+ else
+ raise "Don't yet know how to pack this type of file."
+ end
+ end
+
+ # A convenience method to pack files specified by +src+ into +dest+. If
+ # +src+ is an Array, then each file detailed therein will be packed into
+ # the resulting Archive::Tar::Minitar::Output stream; if +recurse_dirs+
+ # is true, then directories will be recursed.
+ #
+ # If +src+ is an Array, it will be treated as the argument to Find.find;
+ # all files matching will be packed.
+ def pack(src, dest, recurse_dirs = true, &block)
+ Output.open(dest) do |outp|
+ if src.kind_of?(Array)
+ src.each do |entry|
+ pack_file(entry, outp, &block)
+ if dir?(entry) and recurse_dirs
+ Dir["#{entry}/**/**"].each do |ee|
+ pack_file(ee, outp, &block)
+ end
+ end
+ end
+ else
+ Find.find(src) do |entry|
+ pack_file(entry, outp, &block)
+ end
+ end
+ end
+ end
+
+ # A convenience method to unpack files from +src+ into the directory
+ # specified by +dest+. Only those files named explicitly in +files+
+ # will be extracted.
+ def unpack(src, dest, files = [], &block)
+ Input.open(src) do |inp|
+ if File.exist?(dest) and (not dir?(dest))
+ raise "Can't unpack to a non-directory."
+ elsif not File.exist?(dest)
+ FileUtils.mkdir_p(dest)
+ end
+
+ inp.each do |entry|
+ if files.empty? or files.include?(entry.full_name)
+ inp.extract_entry(dest, entry, &block)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/shoes/override.rb b/lib/shoes/override.rb
new file mode 100644
index 0000000..8fab47f
--- /dev/null
+++ b/lib/shoes/override.rb
@@ -0,0 +1,38 @@
+module Override
+ def self.extended mod
+ def mod.list_box args = {}
+ l, t, w, h = args[:left], args[:top], args[:width], 20
+ w ||= 200
+ bcolor = rgb(123, 158, 189)
+ selected, fimg, bimg = [], nil, nil
+
+ f = flow :left => l, :top => t, :width => w, :height => h do
+ border bcolor
+ selected[0] = inscription
+ fimg = image "#{DIR}/static/listbox_button1.png", :left => w-17, :top => 2
+ bimg = image("#{DIR}/static/listbox_button2.png", :left => w-17, :top => 2).hide
+ fimg.hover{bimg.show}
+ bimg.leave{bimg.hide}
+ bimg.click{bimg.show}
+ end
+
+ rects, inscs = [], []
+ args[:items].length.times do |i|
+ x, y = l, t+(i+1)*h
+ r = rect(x, y, w-1, h, :stroke => bcolor, :fill => white).hide
+ s = inscription(args[:items][i], :left => x, :top => y).hide
+ r.hover{r.style :fill => blue}
+ r.leave{r.style :fill => white}
+ r.click{selected[0].text = s.text; selected[1] = r}
+ rects << r
+ inscs << s
+ end
+
+ f.click do
+ rects.each{|r| r.toggle; r.style(:fill => blue) if r == selected[1]}
+ inscs.each{|i| i.toggle}
+ end
+ end
+ end
+end
+
diff --git a/lib/shoes/pack.rb b/lib/shoes/pack.rb
new file mode 100644
index 0000000..bb1b4d0
--- /dev/null
+++ b/lib/shoes/pack.rb
@@ -0,0 +1,543 @@
+#
+# lib/shoes/pack.rb
+# Packing apps into Windows, OS X and Linux binaries
+#
+require 'shoes/shy'
+require 'binject'
+require 'open-uri'
+
+class Shoes
+ module Pack
+ def self.rewrite a, before, hsh
+ File.open(before) do |b|
+ b.each do |line|
+ a << line.gsub(/\#\{(\w+)\}/) { hsh[$1] }
+ end
+ end
+ end
+
+ def self.pkg(platform, opt)
+ $stderr.puts "#{platform} #{opt}"
+ extension = case platform
+ when "win32" then
+ "exe"
+ when "linux" then
+ "run"
+ when "osx" then
+ "dmg"
+ else
+ raise "Unknown platform"
+ end
+
+ case opt
+ when Shoes::I_YES then
+ url = "http://shoes.heroku.com/pkg/#{Shoes::RELEASE_NAME.downcase}/#{platform}/shoes"
+ local_file_path = File.join(LIB_DIR, Shoes::RELEASE_NAME.downcase, platform, "latest_shoes.#{extension}")
+ when Shoes::I_NOV then
+ url = "http://shoes.heroku.com/pkg/#{Shoes::RELEASE_NAME.downcase}/#{platform}/shoes-novideo"
+ local_file_path = File.join(LIB_DIR, Shoes::RELEASE_NAME.downcase, platform, "latest_shoes-novideo.#{extension}")
+ when I_NET then
+ url = false
+ else
+ raise "missing download option #{opt}"
+ end
+
+ FileUtils.makedirs File.join(LIB_DIR, Shoes::RELEASE_NAME.downcase, platform)
+
+ if url then
+ begin
+ url = open(url).read.strip
+ debug url
+ internet_ok = true
+ rescue Exception => e
+ error e
+ internet_ok = false
+ end
+
+ if File.exists? local_file_path
+ return open(local_file_path)
+ elsif internet_ok then
+ begin
+ debug "Downloading #{url}..."
+ downloaded = open(url)
+ debug "Download of #{url} finished"
+ rescue Exception => e
+ error "Could not download from the internet at #{url}\n" + e
+ internet_ok = false
+ end
+ if internet_ok then
+ begin
+ File.open(local_file_path, "wb") do |f|
+ f.write(downloaded.read)
+ end
+ return open(local_file_path)
+ rescue Exception => e
+ raise "The download failed from\n#{url}\nor could not write to local files" + e
+ end
+ end
+ else
+ noHopeMsg = "Failed to find an existing Shoes at:\n#{local_file_path}\nor download from\n#{url} to include with your script."
+ raise noHopeMsg
+ end
+ end
+ end
+
+ def self.exe(script, opt, &blk)
+ size = File.size(script)
+ f = File.open(script, 'rb')
+ exe = Binject::EXE.new(File.join(DIR, "static", "stubs", "blank.exe"))
+ size += script.length
+ exe.inject("SHOES_FILENAME", File.basename(script))
+ size += File.size(script)
+ exe.inject("SHOES_PAYLOAD", f)
+ f2 = pkg("win32", opt)
+ if f2
+ size += File.size(f2.path)
+ f3 = File.open(f2.path, 'rb')
+ exe.inject("SHOES_SETUP", f3)
+
+ count, last = 0, 0.0
+ exe.save(script.gsub(/\.\w+$/, '') + ".exe") do |len|
+ count += len
+ prg = count.to_f / size.to_f
+ blk[last = prg] if blk and prg - last > 0.02 and prg < 1.0
+ end
+
+ f.close
+ f2.close
+ f3.close
+ else
+ Dir.chdir DIR + '/static/stubs' do
+ `.\\shoes-stub-inject.exe #{script.gsub('/', "\\")}`
+ end
+ end
+ blk[1.0] if blk
+ end
+
+ def self.dmg(script, opt, &blk)
+ name = File.basename(script).gsub(/\.\w+$/, '')
+ app_name = name.capitalize.gsub(/[-_](\w)/) { $1.capitalize }
+ vol_name = name.capitalize.gsub(/[-_](\w)/) { " " + $1.capitalize }
+ app_app = "#{app_name}.app"
+ vers = [1, 0]
+
+ tmp_dir = File.join(LIB_DIR, "+dmg")
+ FileUtils.rm_rf(tmp_dir)
+ FileUtils.mkdir_p(tmp_dir)
+ FileUtils.cp(File.join(DIR, "static", "stubs", "blank.hfz"),
+ File.join(tmp_dir, "blank.hfz"))
+ app_dir = File.join(tmp_dir, app_app)
+ res_dir = File.join(tmp_dir, app_app, "Contents", "Resources")
+ mac_dir = File.join(tmp_dir, app_app, "Contents", "MacOS")
+ [res_dir, mac_dir].map { |x| FileUtils.mkdir_p(x) }
+ FileUtils.cp(File.join(DIR, "static", "Shoes.icns"), app_dir)
+ FileUtils.cp(File.join(DIR, "static", "Shoes.icns"), res_dir)
+ File.open(File.join(app_dir, "Contents", "PkgInfo"), 'w') do |f|
+ f << "APPL????"
+ end
+ File.open(File.join(app_dir, "Contents", "Info.plist"), 'w') do |f|
+ f << <<END
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+<key>CFBundleGetInfoString</key>
+<string>#{app_name} #{vers.join(".")}</string>
+<key>CFBundleExecutable</key>
+<string>#{name}-launch</string>
+<key>CFBundleIdentifier</key>
+<string>org.hackety.#{name}</string>
+<key>CFBundleName</key>
+<string>#{app_name}</string>
+<key>CFBundleIconFile</key>
+<string>Shoes.icns</string>
+<key>CFBundleShortVersionString</key>
+<string>#{vers.join(".")}</string>
+<key>CFBundleInfoDictionaryVersion</key>
+<string>6.0</string>
+<key>CFBundlePackageType</key>
+<string>APPL</string>
+<key>IFMajorVersion</key>
+<integer>#{vers[0]}</integer>
+<key>IFMinorVersion</key>
+<integer>#{vers[1]}</integer>
+</dict>
+</plist>
+END
+ end
+ File.open(File.join(app_dir, "Contents", "version.plist"), 'w') do |f|
+ f << <<END
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+<key>BuildVersion</key>
+<string>1</string>
+<key>CFBundleVersion</key>
+<string>#{vers.join(".")}</string>
+<key>ProjectName</key>
+<string>#{app_name}</string>
+<key>SourceVersion</key>
+<string>#{Time.now.strftime("%Y%m%d")}</string>
+</dict>
+</plist>
+END
+ end
+ File.open(File.join(mac_dir, "#{name}-launch"), 'w') do |f|
+ f << <<END
+#!/bin/bash
+SHOESPATH=/Applications/Shoes.app/Contents/MacOS
+APPPATH="${0%/*}"
+unset DYLD_LIBRARY_PATH
+cd "$APPPATH"
+echo "[Pango]" > /tmp/pangorc
+echo "ModuleFiles=$SHOESPATH/pango.modules" >> /tmp/pangorc
+if [ ! -d /Applications/Shoes.app ]
+ then ./cocoa-install
+ fi
+ open -a /Applications/Shoes.app "#{File.basename(script)}"
+ # DYLD_LIBRARY_PATH=$SHOESPATH PANGO_RC_FILE="$APPPATH/pangorc" $SHOESPATH/shoes-bin "#{File.basename(script)}"
+END
+ end
+ FileUtils.cp(script, File.join(mac_dir, File.basename(script)))
+ FileUtils.cp(File.join(DIR, "static", "stubs", "cocoa-install"),
+ File.join(mac_dir, "cocoa-install"))
+
+ dmg = Binject::DMG.new(File.join(tmp_dir, "blank.hfz"), vol_name)
+ f2 = pkg("osx", opt)
+ if f2
+ dmg.grow(10)
+ dmg.inject_file("setup.dmg", f2.path)
+ end
+ dmg.inject_dir(app_app, app_dir)
+ dmg.chmod_file(0755, "#{app_app}/Contents/MacOS/#{name}-launch")
+ dmg.chmod_file(0755, "#{app_app}/Contents/MacOS/cocoa-install")
+ dmg.save(script.gsub(/\.\w+$/, '') + ".dmg") do |perc|
+ blk[perc * 0.01] if blk
+ end
+ FileUtils.rm_rf(tmp_dir)
+ blk[1.0] if blk
+ end
+
+ def self.linux(script, opt, &blk)
+ name = File.basename(script).gsub(/\.\w+$/, '')
+ app_name = name.capitalize.gsub(/[-_](\w)/) { $1.capitalize }
+ run_path = script.gsub(/\.\w+$/, '') + ".run"
+ tgz_path = script.gsub(/\.\w+$/, '') + ".tgz"
+ tmp_dir = File.join(LIB_DIR, "+run")
+ FileUtils.mkdir_p(tmp_dir)
+ pkgf = pkg("linux", opt)
+ prog = 1.0
+ if pkgf
+ size = Shy.hrun(pkgf)
+ pblk = Shy.progress(size) do |name, perc, left|
+ blk[perc * 0.5]
+ end if blk
+ Shy.xzf(pkgf, tmp_dir, &pblk)
+ prog -= 0.5
+ end
+
+ FileUtils.cp(script, File.join(tmp_dir, File.basename(script)))
+ File.open(File.join(tmp_dir, "sh-install"), 'wb') do |a|
+ rewrite a, File.join(DIR, "static", "stubs", "sh-install"),
+ 'SCRIPT' => "./#{File.basename(script)}"
+ end
+ FileUtils.chmod 0755, File.join(tmp_dir, "sh-install")
+
+ raw = Shy.du(tmp_dir)
+ File.open(tgz_path, 'wb') do |f|
+ pblk = Shy.progress(raw) do |name, perc, left|
+ blk[prog + (perc * prog)]
+ end if blk
+ Shy.czf(f, tmp_dir, &pblk)
+ end
+
+ md5, fsize = Shy.md5sum(tgz_path), File.size(tgz_path)
+ File.open(run_path, 'wb') do |f|
+ rewrite f, File.join(DIR, "static", "stubs", "blank.run"),
+ 'CRC' => '0000000000', 'MD5' => md5, 'LABEL' => app_name, 'NAME' => name,
+ 'SIZE' => fsize, 'RAWSIZE' => (raw / 1024) + 1, 'TIME' => Time.now, 'FULLSIZE' => raw
+ File.open(tgz_path, 'rb') do |f2|
+ f.write f2.read(8192) until f2.eof
+ end
+ end
+ FileUtils.chmod 0755, run_path
+ FileUtils.rm_rf(tgz_path)
+ FileUtils.rm_rf(tmp_dir)
+ blk[1.0] if blk
+ end
+ end
+
+ Shoes::I_NET = "No, download Shoes if it's absent."
+ Shoes::I_YES = "Yes, I want Shoes included."
+ Shoes::I_NOV = "Yes, include Shoes, but without video support."
+ PackMake = proc do
+ background "#DDD"
+
+ @page1 = stack do
+ stack do
+ background white
+ background "#FFF".."#EEE", :height => 50, :bottom => 50
+ border "#CCC", :height => 2, :bottom => 0
+ stack :margin => 20 do
+ selt = proc { @sel1.toggle; @sel2.toggle }
+ @path = ""
+ @shy_path = nil
+ @sel1 =
+ flow do
+ para "File to package:"
+ inscription " (or a ", link("directory", &selt), ")"
+ edit1 = edit_line :width => -120
+ @bb = button "Browse...", :width => 100 do
+ @path = edit1.text = ask_open_file
+ #est_recount
+ end
+ end
+ @sel2 =
+ flow :hidden => true do
+ para "Directory:"
+ inscription " (or a ", link("single file", &selt), ")"
+ edit2 = edit_line :width => -120
+ @bf = button "Folder...", :width => 100 do
+ @path = edit2.text = ask_open_folder
+ #est_recount
+ end
+ end
+
+ para "Packaging options"
+ para "Should Shoes be included with your script or should the script ",
+ "download Shoes when the user runs it? Not all options are available on all ",
+ "systems. The defaults work."
+ flow :margin_left => 20 do
+ @shy = check
+ para "Shoes (.shy) for users who have Shoes already", :margin_right => 20
+ end
+ items = [Shoes::I_NET, Shoes::I_YES, Shoes::I_NOV]
+ items.shift unless ::RUBY_PLATFORM =~ /mswin|mingw/
+ flow :margin_left => 20 do
+ flow :width => 0.25 do
+ @exe = check
+ para "Windows"
+ end
+ @incWin = list_box :items => items, :width => 0.6, :height => 30, do
+ @downOpt = @incWin.text
+ est_recount
+ end
+ @incWin.choose items[0]
+ end
+ flow :margin_left => 20 do
+ flow :width => 0.25 do
+ @dmg = check
+ para "OS X", :margin_right => 47
+ end
+ osxop = [Shoes::I_NET, Shoes::I_NOV]
+ @incOSX = list_box :items => osxop, :width => 0.6, :height => 30 do
+ @downOpt = @incOSX.text
+ est_recount
+ end
+ @incOSX.choose(Shoes::I_NOV)
+ end
+ flow :margin_left => 20 do
+ flow :width => 0.25 do
+ @run = check
+ para "Linux", :margin_right => 49
+ end
+ @incLinux = list_box :items => [Shoes::I_NET], :width => 0.6,
+ :height => 30 do
+ est_recount
+ end
+ @incLinux.choose(Shoes::I_NET)
+ end
+ end
+ end
+
+ stack :margin => 20 do
+ @est = para "Estimated size of your choice: ", strong("0k"), :margin => 0, :margin_bottom => 4
+ def est_recount
+ base =
+ case @downOpt
+ when Shoes::I_NET; 98
+ when Shoes::I_YES; 11600
+ when Shoes::I_NOV; 7000
+ end
+ base += ((File.directory?(@path) ? Shy.du(@path) : File.size(@path)) rescue 0) / 1024
+ @est.replace "Estimated size of each app: ", strong(base > 1024 ?
+ "%0.1fM" % [base / 1024.0] : "#{base}K")
+ end
+ def build_thread
+ @shy_path = nil
+ if File.directory? @path
+ @shy_path = @path.gsub(%r![\\/]+$!, '') + ".shy"
+ elsif @shy.style[:checked]
+ @shy_path = @path.gsub(/\.\w+$/, '') + ".shy"
+ end
+ if @shy_path and not @shy_meta
+ @page_shy.show
+ @shy_para.text = File.basename(@shy_path)
+ @shy_launch.items = Shy.launchable(@path)
+ return
+ end
+ @page2.show
+ @path2.replace File.basename(@path)
+ #inc_text = @inc.text
+ inc_win_text, inc_osx_text, inc_linux_text = @incWin.text, @incOSX.text, @incLinux.text
+ Thread.start do
+ begin
+ sofar, stage = 0.0, 1.0 / [@shy.style[:checked], @exe.style[:checked], @dmg.style[:checked], @run.style[:checked]].
+ select { |x| x }.size
+ blk = proc do |frac|
+ @prog.style(:width => sofar + (frac * stage))
+ end
+
+ if @shy_path
+ @status.replace "Compressing the script's folder."
+ pblk = Shy.progress(Shy.du(@path)) do |name, perc, left|
+ blk[perc]
+ end
+ Shy.c(@shy_path, @shy_meta, @path, &pblk)
+ @path = @shy_path
+ @prog.style(:width => sofar += stage)
+ end
+ if @exe.style[:checked]
+ @status.replace "Working on an .exe for Windows."
+ Shoes::Pack.exe(@path, inc_win_text, &blk)
+ @prog.style(:width => sofar += stage)
+ end
+ if @dmg.style[:checked]
+ @status.replace "Working on a .dmg for Mac OS X."
+ Shoes::Pack.dmg(@path, inc_osx_text, &blk)
+ @prog.style(:width => sofar += stage)
+ end
+ if @run.style[:checked]
+ @status.replace "Working on a .run for Linux."
+ Shoes::Pack.linux(@path, inc_linux_text, &blk)
+ @prog.style(:width => sofar += stage)
+ end
+ if @shy_path and not @shy.style[:checked]
+ FileUtils.rm_rf(@shy_path)
+ end
+
+ every do
+ if @prog.style[:width] == 1.0
+ @page2.hide
+ @page3.show
+ @path3.replace File.basename(@path)
+ end
+ end
+ rescue => e
+ @packErrMsg = e
+ # weirdness begins
+ @page2.hide
+ @path3.style :font => 'italic', :size => 12
+ @page3.show
+ @path3.replace @packErrMsg
+ end
+ end
+ end
+
+ inscription "Using the latest Shoes build (0.r#{Shoes::REVISION})", :margin => 0
+ flow :margin_top => 10, :margin_left => 310 do
+ button "OK", :margin_right => 4 do
+ @page1.hide; @bb.hide; @bf.hide
+ @packErrMsg = nil
+ build_thread
+ end
+ button "Cancel" do
+ close
+ end
+ end
+ end
+ end
+
+ @page_shy = stack :hidden => true do
+ stack do
+ background white
+ border "#DDD", :height => 2, :bottom => 0
+ stack :margin => 20 do
+ para "Details for:", :margin => 4
+ @shy_para = para "", :size => 20, :margin => 4
+ flow do
+ stack :margin => 10, :width => 0.4 do
+ para "Name of app:"
+ @shy_name = edit_line :width => 1.0
+ end
+ stack :margin => 10, :width => 0.4 do
+ para "Version:"
+ @shy_version = edit_line :width => 120
+ end
+ stack :margin => 10, :width => 0.4 do
+ para "Creator"
+ @shy_creator = edit_line :width => 1.0
+ end
+ stack :margin => 10, :width => 0.5 do
+ para "Launch"
+ @shy_launch = list_box :height => 30
+ end
+ end
+ end
+ end
+
+ stack :margin => 20 do
+ flow :margin_top => 10, :margin_left => 310 do
+ button "OK", :margin_right => 4 do
+ @shy_meta = Shy.new
+ @shy_meta.name = @shy_name.text
+ @shy_meta.creator = @shy_creator.text
+ @shy_meta.version = @shy_version.text
+ @shy_meta.launch = @shy_launch.text
+ @page_shy.hide
+ build_thread
+ end
+ button "Cancel" do
+ close
+ end
+ end
+ end
+ end
+
+ @page2 = stack :hidden => true do
+ stack do
+ background white
+ border "#DDD", :height => 2, :bottom => 0
+ stack :margin => 20 do
+ para "Packaging:", :margin => 4
+ @path2 = para "", :size => 20, :margin => 4
+ @status = para "", :margin => 4
+ end
+ end
+
+ stack :margin => 20 do
+ stack :width => -20, :height => 24 do
+ @prog = background "#{DIR}/static/stripe.png", :curve => 7
+ background "rgb(0, 0, 0, 100)".."rgb(120, 120, 120, 0)", :curve => 6, :height => 16
+ background "rgb(120, 120, 120, 0)".."rgb(0, 0, 0, 100)", :curve => 6,
+ :height => 16, :top => 8
+ border "rgb(60, 60, 60, 80)", :curve => 7, :strokewidth => 2
+ end
+ end
+ end
+
+ @page3 = stack :hidden => true do
+ stack do
+ background white
+ border "#DDD", :height => 2, :bottom => 0
+ stack :margin => 20 do
+ para "Completed:", :margin => 4
+ @path3 = para "", :size => 20, :margin => 4
+ para "Your files are done, you may close this window.", :margin => 4
+ button "Quit" do
+ exit
+ end
+ end
+ end
+ end
+
+ start do
+ @exe.checked = false
+ @dmg.checked = false
+ @run.checked = false
+ @shy.checked = true
+ #@inc.choose( ::RUBY_PLATFORM =~ /mswin|mingw/ ? Shoes::I_NET : Shoes::I_NOV )
+ end
+ end
+end
diff --git a/lib/shoes/search.rb b/lib/shoes/search.rb
new file mode 100644
index 0000000..98c480c
--- /dev/null
+++ b/lib/shoes/search.rb
@@ -0,0 +1,46 @@
+require 'ftsearch/fragment_writer'
+require 'ftsearch/analysis/simple_identifier_analyzer'
+#require 'ftsearchrt'
+
+class Shoes::Search
+ include FTSearch
+ attr_reader :index
+ def initialize fields = [:uri, :body]
+ field_infos = FTSearch::FieldInfos.new
+ fields.each do |name|
+ field_infos.add_field :name => name,
+ :analyzer => FTSearch::Analysis::SimpleIdentifierAnalyzer.new
+ end
+ @index = FTSearch::FragmentWriter.new :path => nil, :field_infos => field_infos
+ end
+ def add_document hsh
+ @index.add_document hsh
+ end
+ def finish!
+ @index.finish!
+
+ @ft = FulltextReader.new :io => StringIO.new(@index.fulltext_writer.data)
+ @sa = SuffixArrayReader.new @ft, nil, :io => StringIO.new(@index.suffix_array_writer.data)
+ @dm = DocumentMapReader.new :io => StringIO.new(@index.doc_map_writer.data)
+ end
+ def find_all terms, show = 20, prob_sort = false
+ h = Hash.new{|h,k| h[k] = 0}
+ weights = Hash.new(1.0)
+ weights[0] = 10000000 # :uri
+ weights[1] = 10000000 # :body
+ hits = @sa.find_all terms
+ size = hits.size
+ if prob_sort && size > 10000
+ iterations = 50 * Math.sqrt(size)
+ offsets = @sa.lazyhits_to_offsets(hits)
+ weight_arr = weights.sort_by{|id,w| id}.map{|_,v| v}
+ sorted = @dm.rank_offsets_probabilistic(offsets, weight_arr, iterations)
+ else
+ offsets = @sa.lazyhits_to_offsets(hits)
+ sorted = @dm.rank_offsets(offsets, weights.sort_by{|id,w| id}.map{|_,v| v})
+ end
+ sorted[0..show].map do |doc_id, count|
+ [@dm.document_id_to_uri(doc_id), count]
+ end
+ end
+end
diff --git a/lib/shoes/setup.rb b/lib/shoes/setup.rb
new file mode 100644
index 0000000..834847a
--- /dev/null
+++ b/lib/shoes/setup.rb
@@ -0,0 +1,329 @@
+require 'rubygems'
+require 'rubygems/dependency_installer'
+module Gem
+ @ruby = (File.join(Config::CONFIG['bindir'], 'shoes') + Config::CONFIG['EXEEXT']).
+ sub(/.*\s.*/m, '"\&"') + " --ruby"
+end
+class << Gem::Ext::ExtConfBuilder
+ alias_method :make__, :make
+ def make(dest_path, results)
+ raise unless File.exist?('Makefile')
+ mf = File.read('Makefile')
+ mf = mf.gsub(/^INSTALL\s*=\s*.*$/, "INSTALL = $(RUBY) -run -e install -- -vp")
+ mf = mf.gsub(/^INSTALL_PROG\s*=\s*.*$/, "INSTALL_PROG = $(INSTALL) -m 0755")
+ mf = mf.gsub(/^INSTALL_DATA\s*=\s*.*$/, "INSTALL_DATA = $(INSTALL) -m 0644")
+ File.open('Makefile', 'wb') {|f| f.print mf}
+ make__(dest_path, results)
+ end
+end
+
+# STDIN.reopen("/dev/tty") if STDIN.eof?
+class NotSupportedByShoes < Exception; end
+
+class Shoes::Setup
+
+ def self.init
+ gem_reset
+ install_sources if Gem.source_index.find_name('sources').empty?
+ end
+
+ def self.gem_reset
+ Gem.use_paths(GEM_DIR, [GEM_DIR, GEM_CENTRAL_DIR])
+ Gem.source_index.refresh!
+ end
+
+ def self.setup_app(setup)
+ appt = "Setting up for #{setup.script}"
+ Shoes.app :width => 370, :height => 158, :resizable => false, :title => appt do
+ background "#EEE".."#9AA"
+ image :top => 0, :left => 0 do
+ stroke "#FFF"; strokewidth 0.1
+ (0..158).step(3) { |i| line 0, i, 370, i }
+ end
+ @pulse = stack :top => 0, :left => 0
+ @logo = image "#{DIR}/static/shoes-icon-blue.png", :top => -20, :right => -20
+ stack :margin => 18 do
+ title "Shoes Setup", :size => 12, :weight => "bold", :margin => 0
+ para "Preparing #{setup.script}", :size => 8, :margin => 0, :margin_top => 8, :width => 220
+ @pr = progress :width => 1.0, :top => 70, :height => 20
+ button "Cancel", :top => 98, :left => 0.4 do
+ self.close
+ end
+
+ start do
+ @th =
+ Thread.start(self.app) do |app|
+ begin
+ setup.start(app)
+ rescue => e
+ puts e.message
+ end
+ end
+ end
+ end
+
+ animate 10 do |i|
+ i %= 10
+ @pulse.clear do
+ fill black(0.2 - (i * 0.02))
+ strokewidth(3.0 - (i * 0.2))
+ stroke rgb(0.7, 0.7, 0.9, 1.0 - (i * 0.1))
+ oval(@logo.left - i, @logo.top - i, @logo.width + (i * 2))
+ end
+ @pr.fraction = $fraction
+ if @script
+ Shoes.visit(@script)
+ close
+ end
+ end
+ end
+ end
+
+ attr_accessor :steps, :script
+
+ def initialize(script, &blk)
+ @steps = []
+ @script = script
+ instance_eval &blk
+ unless no_steps?
+ app = self.class.setup_app(self)
+ end
+ end
+
+ def no_steps?
+ (@steps.map { |s| s[0] }.uniq - [:source]).empty?
+ end
+
+ def gem name, version = nil
+ arg = "#{name} #{version}".strip
+ name, version = arg.split(/\s+/, 2)
+ if Gem.source_index.find_name(name, version).empty?
+ @steps << [:gem, arg]
+ else
+ activate_gem(name, version)
+ end
+ end
+
+ def source uri
+ @steps << [:source, uri]
+ end
+
+ def activate_gem(name, version)
+ gem = Gem.source_index.find_name(name, version).first
+ Gem.activate(gem.name, "= #{gem.version}")
+ end
+
+ def start(app)
+ old_ui = Gem::DefaultUserInteraction.ui
+ ui = Gem::DefaultUserInteraction.ui = Gem::ShoesFace.new(app)
+ count, total = 0.5, @steps.length
+ ui.progress count, total
+
+ steps.each do |act, arg|
+ case act
+ when :gem
+ name, version = arg.split(/\s+/, 2)
+ count += 1
+ ui.say "Looking for #{name}"
+ if Gem.source_index.find_name(name, version).empty?
+ ui.title "Installing #{name}"
+ installer = Gem::DependencyInstaller.new
+ begin
+ installer.install(name, version || Gem::Requirement.default)
+ self.class.gem_reset
+ activate_gem(name, version)
+ ui.say "Finished installing #{name}"
+ rescue Object => e
+ ui.error "while installing #{name}", e
+ raise e
+ end
+ end
+ when :source
+ ui.title "Switching Gem servers"
+ ui.say "Pulling from #{arg}"
+ Gem.sources.clear << arg
+ self.class.gem_reset
+ end
+ ui.progress count, total
+ end
+ Gem::DefaultUserInteraction.ui = old_ui
+ app.instance_variable_set("@script", @script)
+ end
+
+ def svn(dir, save_as = nil, &blk)
+ dir.gsub! /(.)\/*$/, '\1/'
+ if save_as.nil? or save_as.empty?
+ save_as = File.join(GEM_DIR, 'svn', '1')
+ save_as.succ! while File.exists? save_as
+ elsif save_as.index(GEM_DIR) != 0
+ save_as = File.join(GEM_DIR, 'svn', save_as)
+ end
+ mkdir_p(save_as)
+ puts "** Pulling down #{dir}..."
+ svnuri = URI.parse(dir)
+ case svnuri.scheme
+ when "http", "https"
+ REXML::Document.new(svnuri.open { |f| f.read }).
+ each_element("/svn/index/*") do |ele|
+ fname, href = ele.attributes['name'], ele.attributes['href']
+ case ele.name
+ when "file"
+ puts "- #{dir}#{href}"
+ URI.parse("#{dir}#{href}").open do |f|
+ File.open(File.join(save_as, fname), 'wb') do |f2|
+ f2 << f.read(16384) until f.eof?
+ end
+ end
+ when "dir"
+ svn("#{dir}#{href}", File.join(save_as, fname))
+ end
+ end
+ else
+ raise NotSupportedByShoes, "Only HTTP addresses are supported by Shoes's Subversion module."
+ end
+ if blk
+ Dir.chdir(save_as, &blk)
+ end
+ end
+
+ def self.install_sources
+ require 'base64'
+ sources_gem = File.join(LIB_DIR, "sources-0.0.1.gem")
+ File.open(sources_gem, "wb") do |f|
+ f << Base64.decode64( <<-GEM.gsub(/^ +/, '') )
+ ZGF0YS50YXIuZ3oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAADAwMDA2NDQAMDAwMDAwMAAwMDAwMDAwADAwMDAwMDAwMjYw
+ ADAwMDAwMDAwMDAwADAxMzMxNQAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB1c3RhcgAwMHdoZWVs
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAd2hlZWwAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAwMDAwMDAwADAwMDAwMDAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAfiwgAAKBzRAADyslM0i/OLy1KTi3WK0pioAkw
+ AAIzMxMwDQTotIGhCYINFgcKGBsxKBjQxjmooLS4JLEIaH15RmpqDh51hOTR
+ PTdEQG5+SmlOqoJ7ai6XgoIDNCUo2CpEK2WUlBRY6eunp+YCU0ZpUmVaflF6
+ qh6QUIoFKk1JTVMoTs1J04NqAQoh9AM5qXkpXCA80P4bBaNgFIyCUYAdAAAA
+ AP//AwBOIUx0AAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAG1ldGFkYXRhLmd6
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAw
+ MDAwNjQ0ADAwMDAwMDAAMDAwMDAwMAAwMDAwMDAwMDYzMAAwMDAwMDAwMDAw
+ MAAwMTM0MDAAIDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAdXN0YXIAMDB3aGVlbAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAHdoZWVsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAMDAwMDAwMAAwMDAwMDAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAH4sIAACgc0QAA4SRyW7cMAyG73wKNndPNCkaFDrkmntb9FIEgizR
+ thItLiVneftKHk88QYHWMqCF5E/yY9d1+ImX/u069Y9kirynIOX3mYwbnNHF
+ pYjQ7COFrJ6Jc32RKA5fD8cj5Eu/3XqEqANJzGlhQxneDX9n+nkyISBeiIvD
+ EawuVeJGiNtOfOluPqMQcv2xE7d1g7yEoPlN4o/JZZy1edIj4czp2VnKaNNL
+ 9EnbcxU4JEamkAphbQZdzEV7v5YOTL8Xx6RmXaYsETr0rgcK2vl6m1KguYrL
+ E4oqNFZXTmsbCDWbYTeXtXjQS0mb3E7A0qAXXxS9klmK7n3T6l20jiXWHSad
+ FdtkJA7aZzoXZFVLqP4PUMpvp4hAsTSavF9bQ4hdXVd3V/XUzv+cRPs+TENc
+ jgfmCq0yCBKbCGQ3RhdH9UR1FmCIizKTdhuLKXHN/+sBYHCe3tleb2QO3EOh
+ XNRmbY6Ng0orz+2FXgvrlc+l3w5zd6OY97CPDNqLpZmipWjcOeYPAAAA//8D
+ ADLcAkIBAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAA
+ GEM
+ end
+ Gem::Installer.new(sources_gem).install()
+ end
+end
+
+class Gem::ShoesFace
+ class ProgressReporter
+ attr_reader :count
+
+ def initialize(prog, status, size, initial_message,
+ terminal_message = "complete")
+ @prog = prog
+ (@status = status).replace initial_message
+ @total = size
+ @count = 0.0
+ end
+
+ def updated(message)
+ @count += 1.0
+ @prog.fraction = (@count / @total.to_f) * 0.5
+ end
+
+ def done
+ end
+ end
+
+ def initialize app
+ @title, @status, @prog, = app.slot.contents[-1].contents
+ end
+ def title msg
+ @title.replace msg
+ end
+ def progress count, total
+ #@prog.fraction = count.to_f / total.to_f
+ $fraction = count.to_f / total.to_f
+ end
+ def ask_yes_no msg
+ Kernel.confirm(msg)
+ end
+ def ask msg
+ Kernel.ask(msg)
+ end
+ def error msg, e
+ stat = @status
+ stat.app do
+ error(e)
+ stat.replace link("Error") { Shoes.show_log }, " ", msg
+ end
+ end
+ def say msg
+ @status.replace msg
+ end
+ def alert msg, quiz=nil
+ say(msg)
+ ask(quiz) if quiz
+ end
+ def progress_reporter(*args)
+ ProgressReporter.new(@prog, @status, *args)
+ end
+ def method_missing(*args)
+ p args
+ nil
+ end
+end
+
+Shoes::Setup.init
diff --git a/lib/shoes/shy.rb b/lib/shoes/shy.rb
new file mode 100644
index 0000000..96f6de3
--- /dev/null
+++ b/lib/shoes/shy.rb
@@ -0,0 +1,131 @@
+#
+# lib/shoes/shy.rb
+# Shy, the Shoes YAML archive format
+#
+require 'digest/md5'
+require 'zlib'
+require 'shoes/minitar'
+require 'find'
+require 'tmpdir'
+require 'yaml'
+
+class Shy
+ VERSION = 0x0001
+ MAGIC = "_shy".freeze
+ LAYOUT = "A4vV".freeze #Force to Little Endian for all platforms
+
+ yaml_as 'tag:hackety.org,2007:shy'
+ attr_accessor :name, :creator, :version, :launch
+
+ def self.launchable(d)
+ if File.directory? d
+ Dir["#{d}/**/*.rb"].map do |path|
+ path.gsub(%r!#{Regexp::quote(d)}/!, '')
+ end
+ else
+ [File.basename(d)]
+ end
+ end
+
+ def self.__hdr__(f)
+ hdr = f.read(10).unpack(LAYOUT)
+ raise IOError, "Invalid header" if hdr[0] != MAGIC and hdr[1] > VERSION
+ YAML.load(f.read(hdr[2]))
+ end
+
+
+ def self.du(root)
+ size = 0
+ Find.find(root) do |path|
+ if FileTest.directory?(path)
+ if File.basename(path)[0] == ?.
+ Find.prune
+ else
+ next
+ end
+ else
+ size += FileTest.size(path)
+ end
+ end
+ size
+ end
+
+ def self.meta(path, d = ".")
+ File.open(path, 'rb') do |f|
+ shy = __hdr__(f)
+ end
+ end
+
+ def self.x(path, d = ".")
+ File.open(path, 'rb') do |f|
+ shy = __hdr__(f)
+ inp = Zlib::GzipReader.new(f)
+ Archive::Tar::Minitar.unpack(inp, d)
+ shy
+ end
+ end
+
+ def self.c(path, shy, d, &blk)
+ path = File.expand_path(path)
+ meta = shy.to_yaml
+ File.open(path, "wb") do |f|
+ f << [MAGIC, VERSION, meta.length].pack(LAYOUT)
+ f << meta
+ self.czf(f, d, &blk)
+ end
+ end
+
+ def self.progress(total, &blk)
+ if blk
+ last, left = 0.0, total
+ proc do |action, name, stats|
+ if action == :file_progress
+ left -= stats[:currinc]
+ prg = 1.0 - (left.to_f / total.to_f)
+ blk[name, (last = prg), left] if prg - last > 0.02
+ end
+ end
+ end
+ end
+
+ def self.czf(f, d, &blk)
+ total = du(d)
+ out = Zlib::GzipWriter.new(f)
+ files = ["."]
+ unless File.directory? d
+ files = [File.basename(d)]
+ d = File.dirname(d)
+ end
+ Dir.chdir(d) do
+ Archive::Tar::Minitar.pack(files, out, &blk)
+ end
+ end
+
+ def self.xzf(f, d, &blk)
+ gz = Zlib::GzipReader.new(f)
+ Archive::Tar::Minitar.unpack(gz, d, &blk)
+ end
+
+ def self.md5sum(path)
+ digest = Digest::MD5.new
+ File.open(path, "rb") do |f|
+ digest.update f.read(8192) until f.eof
+ end
+ digest.hexdigest
+ end
+
+ def self.hrun(f)
+ b, i = 0, 1
+ last = 65535
+ while i < last
+ case f.readline
+ when /OLDSKIP=(\d+)/
+ last = $1.to_i
+ when /FULLSIZE=(\d+)/
+ b = $1.to_i
+ end
+ i += 1
+ end
+ b
+ end
+end
diff --git a/lib/shoes/shybuilder.rb b/lib/shoes/shybuilder.rb
new file mode 100644
index 0000000..f0f09c6
--- /dev/null
+++ b/lib/shoes/shybuilder.rb
@@ -0,0 +1,44 @@
+# -*- encoding: utf-8 -*-
+# crude shy-building UI; inspired by Jesse's "Shy Makey Thing"
+
+require 'shoes/shy'
+
+class Shoes
+ def self.start_shy_builder(launch_script)
+ launch_script = File.expand_path(launch_script)
+ top_dir = File.dirname(launch_script)
+ launch_script = File.basename(launch_script)
+ shy_name = "#{top_dir}.shy"
+ Shoes.app do
+ background white
+ stack do
+ para "Almost ready to make #{shy_name}"
+ fields = {}
+ for label, name in [["Project Name", "name"],
+ ["Version", "version"],
+ ["Your Name", "creator"]]
+ flow :width => 1.0 do
+ para "#{label}: "
+ fields[name] = edit_line ''
+ end
+ end
+ button "Build .shy" do
+ shy_desc = Shy.new
+ for name in fields.keys
+ shy_desc.send("#{name}=".intern, fields[name].text)
+ end
+ shy_desc.launch = launch_script
+ Shy.c(shy_name, shy_desc, top_dir)
+ clear
+ background white
+ stack do
+ para "Built #{shy_name}"
+ button "Ok" do
+ close
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/libcurl.so.4 b/libcurl.so.4
new file mode 100644
index 0000000..f921bc1
--- /dev/null
+++ b/libcurl.so.4
Binary files differ
diff --git a/libgif.so.4 b/libgif.so.4
new file mode 100644
index 0000000..e4f8461
--- /dev/null
+++ b/libgif.so.4
Binary files differ
diff --git a/libjpeg.so.8 b/libjpeg.so.8
new file mode 100644
index 0000000..f513262
--- /dev/null
+++ b/libjpeg.so.8
Binary files differ
diff --git a/libportaudio.so.2 b/libportaudio.so.2
new file mode 100644
index 0000000..1bc18d1
--- /dev/null
+++ b/libportaudio.so.2
Binary files differ
diff --git a/libruby.so b/libruby.so
new file mode 100644
index 0000000..858242c
--- /dev/null
+++ b/libruby.so
Binary files differ
diff --git a/libruby.so.1.9 b/libruby.so.1.9
new file mode 100644
index 0000000..858242c
--- /dev/null
+++ b/libruby.so.1.9
Binary files differ
diff --git a/libshoes.so b/libshoes.so
new file mode 100755
index 0000000..7fe1ba2
--- /dev/null
+++ b/libshoes.so
Binary files differ
diff --git a/libsqlite3.so.0 b/libsqlite3.so.0
new file mode 100644
index 0000000..4f7cf9c
--- /dev/null
+++ b/libsqlite3.so.0
Binary files differ
diff --git a/libungif.so.4 b/libungif.so.4
new file mode 100644
index 0000000..e4f8461
--- /dev/null
+++ b/libungif.so.4
Binary files differ
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/fast_xs.so b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/fast_xs.so
new file mode 100644
index 0000000..02936e1
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/fast_xs.so
Binary files differ
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot.rb
new file mode 100644
index 0000000..3032f63
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot.rb
@@ -0,0 +1,26 @@
+# == About hpricot.rb
+#
+# All of Hpricot's various part are loaded when you use <tt>require 'hpricot'</tt>.
+#
+# * hpricot_scan: the scanner (a C extension for Ruby) which turns an HTML stream into tokens.
+# * hpricot/parse.rb: uses the scanner to sort through tokens and give you back a complete document object.
+# * hpricot/tag.rb: sets up objects for the various types of elements in an HTML document.
+# * hpricot/modules.rb: categorizes the various elements using mixins.
+# * hpricot/traverse.rb: methods for searching documents.
+# * hpricot/elements.rb: methods for dealing with a group of elements as an Hpricot::Elements list.
+# * hpricot/inspect.rb: methods for displaying documents in a readable form.
+
+# If available, Nikolai's UTF-8 library will ease use of utf-8 documents.
+# See http://git.bitwi.se/ruby-character-encodings.git/.
+begin
+ require 'encoding/character/utf-8'
+rescue LoadError
+end
+
+require 'hpricot_scan'
+require 'hpricot/tag'
+require 'hpricot/modules'
+require 'hpricot/traverse'
+require 'hpricot/inspect'
+require 'hpricot/parse'
+require 'hpricot/builder'
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/blankslate.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/blankslate.rb
new file mode 100644
index 0000000..6cc20f4
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/blankslate.rb
@@ -0,0 +1,63 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2004 by Jim Weirich (jim@weirichhouse.org).
+# All rights reserved.
+
+# Permission is granted for use, copying, modification, distribution,
+# and distribution of modified versions of this work as long as the
+# above copyright notice is included.
+#++
+
+module Hpricot
+
+ # BlankSlate provides an abstract base class with no predefined
+ # methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
+ # BlankSlate is useful as a base class when writing classes that
+ # depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
+ class BlankSlate
+ class << self
+
+ # Hide the method named +name+ in the BlankSlate class. Don't
+ # hide +instance_eval+ or any method beginning with "__".
+ def hide(name)
+ undef_method name if
+ instance_methods.include?(name) and
+ name !~ /^(__|instance_eval)/
+ end
+ end
+
+ instance_methods.each { |m| hide(m) }
+ end
+end
+
+# Since Ruby is very dynamic, methods added to the ancestors of
+# BlankSlate <em>after BlankSlate is defined</em> will show up in the
+# list of available BlankSlate methods. We handle this by defining a
+# hook in the Object and Kernel classes that will hide any defined
+module Kernel
+ class << self
+ alias_method :hpricot_slate_method_added, :method_added
+
+ # Detect method additions to Kernel and remove them in the
+ # BlankSlate class.
+ def method_added(name)
+ hpricot_slate_method_added(name)
+ return if self != Kernel
+ Hpricot::BlankSlate.hide(name)
+ end
+ end
+end
+
+class Object
+ class << self
+ alias_method :hpricot_slate_method_added, :method_added
+
+ # Detect method additions to Object and remove them in the
+ # BlankSlate class.
+ def method_added(name)
+ hpricot_slate_method_added(name)
+ return if self != Object
+ Hpricot::BlankSlate.hide(name)
+ end
+ end
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/builder.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/builder.rb
new file mode 100644
index 0000000..c1ab829
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/builder.rb
@@ -0,0 +1,216 @@
+require 'hpricot/tags'
+#require 'fast_xs'
+require 'hpricot/blankslate'
+require 'hpricot/htmlinfo'
+
+module Hpricot
+ # XML unescape
+ def self.uxs(str)
+ str.to_s.
+ gsub(/\&(\w+);/) { [NamedCharacters[$1] || ??].pack("U*") }.
+ gsub(/\&\#(\d+);/) { [$1.to_i].pack("U*") }
+ end
+
+ def self.build(ele = Doc.new, assigns = {}, &blk)
+ ele.extend Builder
+ assigns.each do |k, v|
+ ele.instance_variable_set("@#{k}", v)
+ end
+ ele.instance_eval(&blk)
+ ele
+ end
+
+ module Builder
+
+ @@default = {
+ :indent => 0,
+ :output_helpers => true,
+ :output_xml_instruction => true,
+ :output_meta_tag => true,
+ :auto_validation => true,
+ :tagset => Hpricot::XHTMLTransitional,
+ :root_attributes => {
+ :xmlns => 'http://www.w3.org/1999/xhtml', :'xml:lang' => 'en', :lang => 'en'
+ }
+ }
+
+ def self.set(option, value)
+ @@default[option] = value
+ end
+
+ def add_child ele
+ ele.parent = self
+ self.children ||= []
+ self.children << ele
+ ele
+ end
+
+ # Write a +string+ to the HTML stream, making sure to escape it.
+ def text!(string)
+ add_child Text.new(string)
+ end
+
+ # Write a +string+ to the HTML stream without escaping it.
+ def text(string)
+ add_child Text.new(string)
+ nil
+ end
+ alias_method :<<, :text
+ alias_method :concat, :text
+
+ # Create a tag named +tag+. Other than the first argument which is the tag name,
+ # the arguments are the same as the tags implemented via method_missing.
+ def tag!(tag, *args, &block)
+ ele_id = nil
+ if @auto_validation and @tagset
+ if !@tagset.tagset.has_key?(tag)
+ raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
+ elsif args.last.respond_to?(:to_hash)
+ attrs = args.last.to_hash
+
+ if @tagset.forms.include?(tag) and attrs[:id]
+ attrs[:name] ||= attrs[:id]
+ end
+
+ attrs.each do |k, v|
+ atname = k.to_s.downcase.intern
+ unless k =~ /:/ or @tagset.tagset[tag].include? atname
+ raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
+ end
+ if atname == :id
+ ele_id = v.to_s
+ if @elements.has_key? ele_id
+ raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
+ end
+ end
+ end
+ end
+ end
+
+ # turn arguments into children or attributes
+ childs = []
+ attrs = args.grep(Hash)
+ childs.concat((args - attrs).flatten.map do |x|
+ if x.respond_to? :to_html
+ Hpricot.make(x.to_html)
+ elsif x
+ Text.new(x)
+ end
+ end.flatten)
+ attrs = attrs.inject({}) do |hsh, ath|
+ ath.each do |k, v|
+ hsh[k] = v.to_s if v
+ end
+ hsh
+ end
+
+ # create the element itself
+ tag = tag.to_s
+ f = Elem.new(tag, attrs, childs, ETag.new(tag))
+
+ # build children from the block
+ if block
+ build(f, &block)
+ end
+
+ add_child f
+ f
+ end
+
+ def build(*a, &b)
+ Hpricot.build(*a, &b)
+ end
+
+ # Every HTML tag method goes through an html_tag call. So, calling <tt>div</tt> is equivalent
+ # to calling <tt>html_tag(:div)</tt>. All HTML tags in Hpricot's list are given generated wrappers
+ # for this method.
+ #
+ # If the @auto_validation setting is on, this method will check for many common mistakes which
+ # could lead to invalid XHTML.
+ def html_tag(sym, *args, &block)
+ if @auto_validation and @tagset.self_closing.include?(sym) and block
+ raise InvalidXhtmlError, "the `#{sym}' element is self-closing, please remove the block"
+ elsif args.empty? and block.nil?
+ CssProxy.new(self, sym)
+ else
+ tag!(sym, *args, &block)
+ end
+ end
+
+ XHTMLTransitional.tags.each do |k|
+ class_eval %{
+ def #{k}(*args, &block)
+ html_tag(#{k.inspect}, *args, &block)
+ end
+ }
+ end
+
+ def doctype(target, pub, sys)
+ add_child DocType.new(target, pub, sys)
+ end
+
+ remove_method :head
+
+ # Builds a head tag. Adds a <tt>meta</tt> tag inside with Content-Type
+ # set to <tt>text/html; charset=utf-8</tt>.
+ def head(*args, &block)
+ tag!(:head, *args) do
+ tag!(:meta, "http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8") if @output_meta_tag
+ instance_eval(&block)
+ end
+ end
+
+ # Builds an html tag. An XML 1.0 instruction and an XHTML 1.0 Transitional doctype
+ # are prepended. Also assumes <tt>:xmlns => "http://www.w3.org/1999/xhtml",
+ # :lang => "en"</tt>.
+ def xhtml_transitional(attrs = {}, &block)
+ # self.tagset = Hpricot::XHTMLTransitional
+ xhtml_html(attrs, &block)
+ end
+
+ # Builds an html tag with XHTML 1.0 Strict doctype instead.
+ def xhtml_strict(attrs = {}, &block)
+ # self.tagset = Hpricot::XHTMLStrict
+ xhtml_html(attrs, &block)
+ end
+
+ private
+
+ def xhtml_html(attrs = {}, &block)
+ instruct! if @output_xml_instruction
+ doctype(:html, *@@default[:tagset].doctype)
+ tag!(:html, @@default[:root_attributes].merge(attrs), &block)
+ end
+
+ end
+
+ # Class used by Markaby::Builder to store element options. Methods called
+ # against the CssProxy object are added as element classes or IDs.
+ #
+ # See the README for examples.
+ class CssProxy < BlankSlate
+
+ # Creates a CssProxy object.
+ def initialize(builder, sym)
+ @builder, @sym, @attrs = builder, sym, {}
+ end
+
+ # Adds attributes to an element. Bang methods set the :id attribute.
+ # Other methods add to the :class attribute.
+ def method_missing(id_or_class, *args, &block)
+ if (idc = id_or_class.to_s) =~ /!$/
+ @attrs[:id] = $`
+ else
+ @attrs[:class] = @attrs[:class].nil? ? idc : "#{@attrs[:class]} #{idc}".strip
+ end
+
+ if block or args.any?
+ args.push(@attrs)
+ return @builder.tag!(@sym, *args, &block)
+ end
+
+ return self
+ end
+
+ end
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/elements.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/elements.rb
new file mode 100644
index 0000000..0854b8a
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/elements.rb
@@ -0,0 +1,510 @@
+module Hpricot
+# Once you've matched a list of elements, you will often need to handle them as
+# a group. Or you may want to perform the same action on each of them.
+# Hpricot::Elements is an extension of Ruby's array class, with some methods
+# added for altering elements contained in the array.
+#
+# If you need to create an element array from regular elements:
+#
+# Hpricot::Elements[ele1, ele2, ele3]
+#
+# Assuming that ele1, ele2 and ele3 contain element objects (Hpricot::Elem,
+# Hpricot::Doc, etc.)
+#
+# == Continuing Searches
+#
+# Usually the Hpricot::Elements you're working on comes from a search you've
+# done. Well, you can continue searching the list by using the same <tt>at</tt>
+# and <tt>search</tt> methods you can use on plain elements.
+#
+# elements = doc.search("/div/p")
+# elements = elements.search("/a[@href='http://hoodwink.d/']")
+# elements = elements.at("img")
+#
+# == Altering Elements
+#
+# When you're altering elements in the list, your changes will be reflected in
+# the document you started searching from.
+#
+# doc = Hpricot("That's my <b>spoon</b>, Tyler.")
+# doc.at("b").swap("<i>fork</i>")
+# doc.to_html
+# #=> "That's my <i>fork</i>, Tyler."
+#
+# == Getting More Detailed
+#
+# If you can't find a method here that does what you need, you may need to
+# loop through the elements and find a method in Hpricot::Container::Trav
+# which can do what you need.
+#
+# For example, you may want to search for all the H3 header tags in a document
+# and grab all the tags underneath the header, but not inside the header.
+# A good method for this is <tt>next_sibling</tt>:
+#
+# doc.search("h3").each do |h3|
+# while ele = h3.next_sibling
+# ary << ele # stuff away all the elements under the h3
+# end
+# end
+#
+# Most of the useful element methods are in the mixins Hpricot::Traverse
+# and Hpricot::Container::Trav.
+ class Elements < Array
+
+ # Searches this list for any elements (or children of these elements) matching
+ # the CSS or XPath expression +expr+. Root is assumed to be the element scanned.
+ #
+ # See Hpricot::Container::Trav.search for more.
+ def search(*expr,&blk)
+ Elements[*map { |x| x.search(*expr,&blk) }.flatten.uniq]
+ end
+ alias_method :/, :search
+
+ # Searches this list for the first element (or child of these elements) matching
+ # the CSS or XPath expression +expr+. Root is assumed to be the element scanned.
+ #
+ # See Hpricot::Container::Trav.at for more.
+ def at(expr, &blk)
+ search(expr, &blk).first
+ end
+ alias_method :%, :at
+
+ # Convert this group of elements into a complete HTML fragment, returned as a
+ # string.
+ def to_html
+ map { |x| x.output("") }.join
+ end
+ alias_method :to_s, :to_html
+
+ # Returns an HTML fragment built of the contents of each element in this list.
+ #
+ # If a HTML +string+ is supplied, this method acts like inner_html=.
+ def inner_html(*string)
+ if string.empty?
+ map { |x| x.inner_html }.join
+ else
+ x = self.inner_html = string.pop || x
+ end
+ end
+ alias_method :html, :inner_html
+ alias_method :innerHTML, :inner_html
+
+ # Replaces the contents of each element in this list. Supply an HTML +string+,
+ # which is loaded into Hpricot objects and inserted into every element in this
+ # list.
+ def inner_html=(string)
+ each { |x| x.inner_html = string }
+ end
+ alias_method :html=, :inner_html=
+ alias_method :innerHTML=, :inner_html=
+
+ # Returns an string containing the text contents of each element in this list.
+ # All HTML tags are removed.
+ def inner_text
+ map { |x| x.inner_text }.join
+ end
+ alias_method :text, :inner_text
+
+ # Remove all elements in this list from the document which contains them.
+ #
+ # doc = Hpricot("<html>Remove this: <b>here</b></html>")
+ # doc.search("b").remove
+ # doc.to_html
+ # => "<html>Remove this: </html>"
+ #
+ def remove
+ each { |x| x.parent.children.delete(x) }
+ end
+
+ # Empty the elements in this list, by removing their insides.
+ #
+ # doc = Hpricot("<p> We have <i>so much</i> to say.</p>")
+ # doc.search("i").empty
+ # doc.to_html
+ # => "<p> We have <i></i> to say.</p>"
+ #
+ def empty
+ each { |x| x.inner_html = nil }
+ end
+
+ # Add to the end of the contents inside each element in this list.
+ # Pass in an HTML +str+, which is turned into Hpricot elements.
+ def append(str = nil, &blk)
+ each { |x| x.html(x.children + x.make(str, &blk)) }
+ end
+
+ # Add to the start of the contents inside each element in this list.
+ # Pass in an HTML +str+, which is turned into Hpricot elements.
+ def prepend(str = nil, &blk)
+ each { |x| x.html(x.make(str, &blk) + x.children) }
+ end
+
+ # Add some HTML just previous to each element in this list.
+ # Pass in an HTML +str+, which is turned into Hpricot elements.
+ def before(str = nil, &blk)
+ each { |x| x.parent.insert_before x.make(str, &blk), x }
+ end
+
+ # Just after each element in this list, add some HTML.
+ # Pass in an HTML +str+, which is turned into Hpricot elements.
+ def after(str = nil, &blk)
+ each { |x| x.parent.insert_after x.make(str, &blk), x }
+ end
+
+ # Wraps each element in the list inside the element created by HTML +str+.
+ # If more than one element is found in the string, Hpricot locates the
+ # deepest spot inside the first element.
+ #
+ # doc.search("a[@href]").
+ # wrap(%{<div class="link"><div class="link_inner"></div></div>})
+ #
+ # This code wraps every link on the page inside a +div.link+ and a +div.link_inner+ nest.
+ def wrap(str = nil, &blk)
+ each do |x|
+ wrap = x.make(str, &blk)
+ nest = wrap.detect { |w| w.respond_to? :children }
+ unless nest
+ raise "No wrapping element found."
+ end
+ x.parent.replace_child(x, wrap)
+ nest = nest.children.first until nest.empty?
+ nest.html([x])
+ end
+ end
+
+ # Gets and sets attributes on all matched elements.
+ #
+ # Pass in a +key+ on its own and this method will return the string value
+ # assigned to that attribute for the first elements. Or +nil+ if the
+ # attribute isn't found.
+ #
+ # doc.search("a").attr("href")
+ # #=> "http://hacketyhack.net/"
+ #
+ # Or, pass in a +key+ and +value+. This will set an attribute for all
+ # matched elements.
+ #
+ # doc.search("p").attr("class", "basic")
+ #
+ # You may also use a Hash to set a series of attributes:
+ #
+ # (doc/"a").attr(:class => "basic", :href => "http://hackety.org/")
+ #
+ # Lastly, a block can be used to rewrite an attribute based on the element
+ # it belongs to. The block will pass in an element. Return from the block
+ # the new value of the attribute.
+ #
+ # records.attr("href") { |e| e['href'] + "#top" }
+ #
+ # This example adds a <tt>#top</tt> anchor to each link.
+ #
+ def attr key, value = nil, &blk
+ if value or blk
+ each do |el|
+ el.set_attribute(key, value || blk[el])
+ end
+ return self
+ end
+ if key.is_a? Hash
+ key.each { |k,v| self.attr(k,v) }
+ return self
+ else
+ return self[0].get_attribute(key)
+ end
+ end
+ alias_method :set, :attr
+
+ # Adds the class to all matched elements.
+ #
+ # (doc/"p").add_class("bacon")
+ #
+ # Now all paragraphs will have class="bacon".
+ def add_class class_name
+ each do |el|
+ next unless el.respond_to? :get_attribute
+ classes = el.get_attribute('class').to_s.split(" ")
+ el.set_attribute('class', classes.push(class_name).uniq.join(" "))
+ end
+ self
+ end
+
+ # Remove an attribute from each of the matched elements.
+ #
+ # (doc/"input").remove_attr("disabled")
+ #
+ def remove_attr name
+ each do |el|
+ next unless el.respond_to? :remove_attribute
+ el.remove_attribute(name)
+ end
+ self
+ end
+
+ # Removes a class from all matched elements.
+ #
+ # (doc/"span").remove_class("lightgrey")
+ #
+ # Or, to remove all classes:
+ #
+ # (doc/"span").remove_class
+ #
+ def remove_class name = nil
+ each do |el|
+ next unless el.respond_to? :get_attribute
+ if name
+ classes = el.get_attribute('class').to_s.split(" ")
+ el.set_attribute('class', (classes - [name]).uniq.join(" "))
+ else
+ el.remove_attribute("class")
+ end
+ end
+ self
+ end
+
+ ATTR_RE = %r!\[ *(?:(@)([\w\(\)-]+)|([\w\(\)-]+\(\))) *([~\!\|\*$\^=]*) *'?"?([^'"]*)'?"? *\]!i
+ BRACK_RE = %r!(\[) *([^\]]*) *\]+!i
+ FUNC_RE = %r!(:)?([a-zA-Z0-9\*_-]*)\( *[\"']?([^ \)]*?)['\"]? *\)!
+ CUST_RE = %r!(:)([a-zA-Z0-9\*_-]*)()!
+ CATCH_RE = %r!([:\.#]*)([a-zA-Z0-9\*_-]+)!
+
+ def self.filter(nodes, expr, truth = true)
+ until expr.empty?
+ _, *m = *expr.match(/^(?:#{ATTR_RE}|#{BRACK_RE}|#{FUNC_RE}|#{CUST_RE}|#{CATCH_RE})/)
+ break unless _
+
+ expr = $'
+ m.compact!
+ if m[0] == '@'
+ m[0] = "@#{m.slice!(2,1).join}"
+ end
+
+ if m[0] == '[' && m[1] =~ /^\d+$/
+ m = [":", "nth", m[1].to_i-1]
+ end
+
+ if m[0] == ":" && m[1] == "not"
+ nodes, = Elements.filter(nodes, m[2], false)
+ elsif "#{m[0]}#{m[1]}" =~ /^(:even|:odd)$/
+ new_nodes = []
+ nodes.each_with_index {|n,i| new_nodes.push(n) if (i % 2 == (m[1] == "even" ? 0 : 1)) }
+ nodes = new_nodes
+ elsif "#{m[0]}#{m[1]}" =~ /^(:first|:last)$/
+ nodes = [nodes.send(m[1])]
+ else
+ meth = "filter[#{m[0]}#{m[1]}]" unless m[0].empty?
+ if meth and Traverse.method_defined? meth
+ args = m[2..-1]
+ else
+ meth = "filter[#{m[0]}]"
+ if Traverse.method_defined? meth
+ args = m[1..-1]
+ end
+ end
+ args << -1
+ nodes = Elements[*nodes.find_all do |x|
+ args[-1] += 1
+ x.send(meth, *args) ? truth : !truth
+ end]
+ end
+ end
+ [nodes, expr]
+ end
+
+ # Given two elements, attempt to gather an Elements array of everything between
+ # (and including) those two elements.
+ def self.expand(ele1, ele2, excl=false)
+ ary = []
+ offset = excl ? -1 : 0
+
+ if ele1 and ele2
+ # let's quickly take care of siblings
+ if ele1.parent == ele2.parent
+ ary = ele1.parent.children[ele1.node_position..(ele2.node_position+offset)]
+ else
+ # find common parent
+ p, ele1_p = ele1, [ele1]
+ ele1_p.unshift p while p.respond_to?(:parent) and p = p.parent
+ p, ele2_p = ele2, [ele2]
+ ele2_p.unshift p while p.respond_to?(:parent) and p = p.parent
+ common_parent = ele1_p.zip(ele2_p).select { |p1, p2| p1 == p2 }.flatten.last
+
+ child = nil
+ if ele1 == common_parent
+ child = ele2
+ elsif ele2 == common_parent
+ child = ele1
+ end
+
+ if child
+ ary = common_parent.children[0..(child.node_position+offset)]
+ end
+ end
+ end
+
+ return Elements[*ary]
+ end
+
+ def filter(expr)
+ nodes, = Elements.filter(self, expr)
+ nodes
+ end
+
+ def not(expr)
+ if expr.is_a? Traverse
+ nodes = self - [expr]
+ else
+ nodes, = Elements.filter(self, expr, false)
+ end
+ nodes
+ end
+
+ private
+ def copy_node(node, l)
+ l.instance_variables.each do |iv|
+ node.instance_variable_set(iv, l.instance_variable_get(iv))
+ end
+ end
+
+ end
+
+ module Traverse
+ def self.filter(tok, &blk)
+ define_method("filter[#{tok.is_a?(String) ? tok : tok.inspect}]", &blk)
+ end
+
+ filter '' do |name,i|
+ name == '*' || (self.respond_to?(:name) && self.name.downcase == name.downcase)
+ end
+
+ filter '#' do |id,i|
+ self.elem? and get_attribute('id').to_s == id
+ end
+
+ filter '.' do |name,i|
+ self.elem? and classes.include? name
+ end
+
+ filter :lt do |num,i|
+ self.position < num.to_i
+ end
+
+ filter :gt do |num,i|
+ self.position > num.to_i
+ end
+
+ nth = proc { |num,i| self.position == num.to_i }
+ nth_first = proc { |*a| self.position == 0 }
+ nth_last = proc { |*a| self == parent.children_of_type(self.name).last }
+
+ filter :nth, &nth
+ filter :eq, &nth
+ filter ":nth-of-type", &nth
+
+ filter :first, &nth_first
+ filter ":first-of-type", &nth_first
+
+ filter :last, &nth_last
+ filter ":last-of-type", &nth_last
+
+ filter :even do |num,i|
+ self.position % 2 == 0
+ end
+
+ filter :odd do |num,i|
+ self.position % 2 == 1
+ end
+
+ filter ':first-child' do |i|
+ self == parent.containers.first
+ end
+
+ filter ':nth-child' do |arg,i|
+ case arg
+ when 'even'; (parent.containers.index(self) + 1) % 2 == 0
+ when 'odd'; (parent.containers.index(self) + 1) % 2 == 1
+ else self == (parent.containers[arg.to_i - 1])
+ end
+ end
+
+ filter ":last-child" do |i|
+ self == parent.containers.last
+ end
+
+ filter ":nth-last-child" do |arg,i|
+ self == parent.containers[-1-arg.to_i]
+ end
+
+ filter ":nth-last-of-type" do |arg,i|
+ self == parent.children_of_type(self.name)[-1-arg.to_i]
+ end
+
+ filter ":only-of-type" do |arg,i|
+ parent.children_of_type(self.name).length == 1
+ end
+
+ filter ":only-child" do |arg,i|
+ parent.containers.length == 1
+ end
+
+ filter :parent do |*a|
+ containers.length > 0
+ end
+
+ filter :empty do |*a|
+ containers.length == 0
+ end
+
+ filter :root do |*a|
+ self.is_a? Hpricot::Doc
+ end
+
+ filter 'text' do |*a|
+ self.text?
+ end
+
+ filter 'comment' do |*a|
+ self.comment?
+ end
+
+ filter :contains do |arg, ignore|
+ html.include? arg
+ end
+
+
+
+ pred_procs =
+ {'text()' => proc { |ele, *_| ele.inner_text.strip },
+ '@' => proc { |ele, attr, *_| ele.get_attribute(attr).to_s if ele.elem? }}
+
+ oper_procs =
+ {'=' => proc { |a,b| a == b },
+ '!=' => proc { |a,b| a != b },
+ '~=' => proc { |a,b| a.split(/\s+/).include?(b) },
+ '|=' => proc { |a,b| a =~ /^#{Regexp::quote b}(-|$)/ },
+ '^=' => proc { |a,b| a.index(b) == 0 },
+ '$=' => proc { |a,b| a =~ /#{Regexp::quote b}$/ },
+ '*=' => proc { |a,b| idx = a.index(b) }}
+
+ pred_procs.each do |pred_n, pred_f|
+ oper_procs.each do |oper_n, oper_f|
+ filter "#{pred_n}#{oper_n}" do |*a|
+ qual = pred_f[self, *a]
+ oper_f[qual, a[-2]] if qual
+ end
+ end
+ end
+
+ filter 'text()' do |val,i|
+ self.children.grep(Hpricot::Text).detect { |x| x.content =~ /\S/ } if self.children
+ end
+
+ filter '@' do |attr,val,i|
+ self.elem? and has_attribute? attr
+ end
+
+ filter '[' do |val,i|
+ self.elem? and search(val).length > 0
+ end
+
+ end
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/htmlinfo.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/htmlinfo.rb
new file mode 100644
index 0000000..892340b
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/htmlinfo.rb
@@ -0,0 +1,691 @@
+module Hpricot
+# The code below is auto-generated. Don't edit manually.
+ # :stopdoc:
+ NamedCharacters =
+{"AElig"=>198, "Aacute"=>193, "Acirc"=>194, "Agrave"=>192, "Alpha"=>913,
+ "Aring"=>197, "Atilde"=>195, "Auml"=>196, "Beta"=>914, "Ccedil"=>199,
+ "Chi"=>935, "Dagger"=>8225, "Delta"=>916, "ETH"=>208, "Eacute"=>201,
+ "Ecirc"=>202, "Egrave"=>200, "Epsilon"=>917, "Eta"=>919, "Euml"=>203,
+ "Gamma"=>915, "Iacute"=>205, "Icirc"=>206, "Igrave"=>204, "Iota"=>921,
+ "Iuml"=>207, "Kappa"=>922, "Lambda"=>923, "Mu"=>924, "Ntilde"=>209, "Nu"=>925,
+ "OElig"=>338, "Oacute"=>211, "Ocirc"=>212, "Ograve"=>210, "Omega"=>937,
+ "Omicron"=>927, "Oslash"=>216, "Otilde"=>213, "Ouml"=>214, "Phi"=>934,
+ "Pi"=>928, "Prime"=>8243, "Psi"=>936, "Rho"=>929, "Scaron"=>352, "Sigma"=>931,
+ "THORN"=>222, "Tau"=>932, "Theta"=>920, "Uacute"=>218, "Ucirc"=>219,
+ "Ugrave"=>217, "Upsilon"=>933, "Uuml"=>220, "Xi"=>926, "Yacute"=>221,
+ "Yuml"=>376, "Zeta"=>918, "aacute"=>225, "acirc"=>226, "acute"=>180,
+ "aelig"=>230, "agrave"=>224, "alefsym"=>8501, "alpha"=>945, "amp"=>38,
+ "and"=>8743, "ang"=>8736, "apos"=>39, "aring"=>229, "asymp"=>8776,
+ "atilde"=>227, "auml"=>228, "bdquo"=>8222, "beta"=>946, "brvbar"=>166,
+ "bull"=>8226, "cap"=>8745, "ccedil"=>231, "cedil"=>184, "cent"=>162,
+ "chi"=>967, "circ"=>710, "clubs"=>9827, "cong"=>8773, "copy"=>169,
+ "crarr"=>8629, "cup"=>8746, "curren"=>164, "dArr"=>8659, "dagger"=>8224,
+ "darr"=>8595, "deg"=>176, "delta"=>948, "diams"=>9830, "divide"=>247,
+ "eacute"=>233, "ecirc"=>234, "egrave"=>232, "empty"=>8709, "emsp"=>8195,
+ "ensp"=>8194, "epsilon"=>949, "equiv"=>8801, "eta"=>951, "eth"=>240,
+ "euml"=>235, "euro"=>8364, "exist"=>8707, "fnof"=>402, "forall"=>8704,
+ "frac12"=>189, "frac14"=>188, "frac34"=>190, "frasl"=>8260, "gamma"=>947,
+ "ge"=>8805, "gt"=>62, "hArr"=>8660, "harr"=>8596, "hearts"=>9829,
+ "hellip"=>8230, "iacute"=>237, "icirc"=>238, "iexcl"=>161, "igrave"=>236,
+ "image"=>8465, "infin"=>8734, "int"=>8747, "iota"=>953, "iquest"=>191,
+ "isin"=>8712, "iuml"=>239, "kappa"=>954, "lArr"=>8656, "lambda"=>955,
+ "lang"=>9001, "laquo"=>171, "larr"=>8592, "lceil"=>8968, "ldquo"=>8220,
+ "le"=>8804, "lfloor"=>8970, "lowast"=>8727, "loz"=>9674, "lrm"=>8206,
+ "lsaquo"=>8249, "lsquo"=>8216, "lt"=>60, "macr"=>175, "mdash"=>8212,
+ "micro"=>181, "middot"=>183, "minus"=>8722, "mu"=>956, "nabla"=>8711,
+ "nbsp"=>160, "ndash"=>8211, "ne"=>8800, "ni"=>8715, "not"=>172, "notin"=>8713,
+ "nsub"=>8836, "ntilde"=>241, "nu"=>957, "oacute"=>243, "ocirc"=>244,
+ "oelig"=>339, "ograve"=>242, "oline"=>8254, "omega"=>969, "omicron"=>959,
+ "oplus"=>8853, "or"=>8744, "ordf"=>170, "ordm"=>186, "oslash"=>248,
+ "otilde"=>245, "otimes"=>8855, "ouml"=>246, "para"=>182, "part"=>8706,
+ "permil"=>8240, "perp"=>8869, "phi"=>966, "pi"=>960, "piv"=>982,
+ "plusmn"=>177, "pound"=>163, "prime"=>8242, "prod"=>8719, "prop"=>8733,
+ "psi"=>968, "quot"=>34, "rArr"=>8658, "radic"=>8730, "rang"=>9002,
+ "raquo"=>187, "rarr"=>8594, "rceil"=>8969, "rdquo"=>8221, "real"=>8476,
+ "reg"=>174, "rfloor"=>8971, "rho"=>961, "rlm"=>8207, "rsaquo"=>8250,
+ "rsquo"=>8217, "sbquo"=>8218, "scaron"=>353, "sdot"=>8901, "sect"=>167,
+ "shy"=>173, "sigma"=>963, "sigmaf"=>962, "sim"=>8764, "spades"=>9824,
+ "sub"=>8834, "sube"=>8838, "sum"=>8721, "sup"=>8835, "sup1"=>185, "sup2"=>178,
+ "sup3"=>179, "supe"=>8839, "szlig"=>223, "tau"=>964, "there4"=>8756,
+ "theta"=>952, "thetasym"=>977, "thinsp"=>8201, "thorn"=>254, "tilde"=>732,
+ "times"=>215, "trade"=>8482, "uArr"=>8657, "uacute"=>250, "uarr"=>8593,
+ "ucirc"=>251, "ugrave"=>249, "uml"=>168, "upsih"=>978, "upsilon"=>965,
+ "uuml"=>252, "weierp"=>8472, "xi"=>958, "yacute"=>253, "yen"=>165,
+ "yuml"=>255, "zeta"=>950, "zwj"=>8205, "zwnj"=>8204}
+
+
+ NamedCharactersPattern = /\A(?-mix:AElig|Aacute|Acirc|Agrave|Alpha|Aring|Atilde|Auml|Beta|Ccedil|Chi|Dagger|Delta|ETH|Eacute|Ecirc|Egrave|Epsilon|Eta|Euml|Gamma|Iacute|Icirc|Igrave|Iota|Iuml|Kappa|Lambda|Mu|Ntilde|Nu|OElig|Oacute|Ocirc|Ograve|Omega|Omicron|Oslash|Otilde|Ouml|Phi|Pi|Prime|Psi|Rho|Scaron|Sigma|THORN|Tau|Theta|Uacute|Ucirc|Ugrave|Upsilon|Uuml|Xi|Yacute|Yuml|Zeta|aacute|acirc|acute|aelig|agrave|alefsym|alpha|amp|and|ang|apos|aring|asymp|atilde|auml|bdquo|beta|brvbar|bull|cap|ccedil|cedil|cent|chi|circ|clubs|cong|copy|crarr|cup|curren|dArr|dagger|darr|deg|delta|diams|divide|eacute|ecirc|egrave|empty|emsp|ensp|epsilon|equiv|eta|eth|euml|euro|exist|fnof|forall|frac12|frac14|frac34|frasl|gamma|ge|gt|hArr|harr|hearts|hellip|iacute|icirc|iexcl|igrave|image|infin|int|iota|iquest|isin|iuml|kappa|lArr|lambda|lang|laquo|larr|lceil|ldquo|le|lfloor|lowast|loz|lrm|lsaquo|lsquo|lt|macr|mdash|micro|middot|minus|mu|nabla|nbsp|ndash|ne|ni|not|notin|nsub|ntilde|nu|oacute|ocirc|oelig|ograve|oline|omega|omicron|oplus|or|ordf|ordm|oslash|otilde|otimes|ouml|para|part|permil|perp|phi|pi|piv|plusmn|pound|prime|prod|prop|psi|quot|rArr|radic|rang|raquo|rarr|rceil|rdquo|real|reg|rfloor|rho|rlm|rsaquo|rsquo|sbquo|scaron|sdot|sect|shy|sigma|sigmaf|sim|spades|sub|sube|sum|sup|sup1|sup2|sup3|supe|szlig|tau|there4|theta|thetasym|thinsp|thorn|tilde|times|trade|uArr|uacute|uarr|ucirc|ugrave|uml|upsih|upsilon|uuml|weierp|xi|yacute|yen|yuml|zeta|zwj|zwnj)\z/
+
+ ElementContent =
+{"h6"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "object"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "param", "pre", "q",
+ "s", "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "dl"=>["dd", "dt"],
+ "p"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "acronym"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "code"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "ul"=>["li"],
+ "tt"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "label"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "form"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "q"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "thead"=>["tr"],
+ "area"=>:EMPTY,
+ "td"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "title"=>[],
+ "dir"=>["li"],
+ "s"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "ol"=>["li"],
+ "hr"=>:EMPTY,
+ "applet"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "param", "pre", "q",
+ "s", "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "table"=>["caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr"],
+ "legend"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "cite"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "a"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "html"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "base", "basefont", "bdo",
+ "big", "blockquote", "body", "br", "button", "center", "cite", "code",
+ "dfn", "dir", "div", "dl", "em", "fieldset", "font", "form", "h1", "h2",
+ "h3", "h4", "h5", "h6", "head", "hr", "i", "iframe", "img", "input",
+ "isindex", "kbd", "label", "map", "menu", "noframes", "noscript", "object",
+ "ol", "p", "pre", "q", "s", "samp", "script", "select", "small", "span",
+ "strike", "strong", "sub", "sup", "table", "textarea", "title", "tt", "u",
+ "ul", "var"],
+ "u"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "blockquote"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "center"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "b"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "base"=>:EMPTY,
+ "th"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "link"=>:EMPTY,
+ "var"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "samp"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "div"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "textarea"=>[],
+ "pre"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "head"=>["base", "isindex", "title"],
+ "span"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "br"=>:EMPTY,
+ "script"=>:CDATA,
+ "noframes"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "style"=>:CDATA,
+ "meta"=>:EMPTY,
+ "dt"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "option"=>[],
+ "kbd"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "big"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "tfoot"=>["tr"],
+ "sup"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "bdo"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "isindex"=>:EMPTY,
+ "dfn"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "fieldset"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "legend",
+ "map", "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "em"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "font"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "tbody"=>["tr"],
+ "noscript"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "li"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "col"=>:EMPTY,
+ "small"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "dd"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "i"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "menu"=>["li"],
+ "strong"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "basefont"=>:EMPTY,
+ "img"=>:EMPTY,
+ "optgroup"=>["option"],
+ "map"=>
+ ["address", "area", "blockquote", "center", "dir", "div", "dl", "fieldset",
+ "form", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "isindex", "menu",
+ "noframes", "noscript", "ol", "p", "pre", "table", "ul"],
+ "h1"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "address"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "p", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "sub"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "param"=>:EMPTY,
+ "input"=>:EMPTY,
+ "h2"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "abbr"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "h3"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "strike"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "body"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "ins"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "button"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "h4"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "select"=>["optgroup", "option"],
+ "caption"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "colgroup"=>["col"],
+ "tr"=>["td", "th"],
+ "del"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"],
+ "h5"=>
+ ["a", "abbr", "acronym", "applet", "b", "basefont", "bdo", "big", "br",
+ "button", "cite", "code", "dfn", "em", "font", "i", "iframe", "img",
+ "input", "kbd", "label", "map", "object", "q", "s", "samp", "script",
+ "select", "small", "span", "strike", "strong", "sub", "sup", "textarea",
+ "tt", "u", "var"],
+ "iframe"=>
+ ["a", "abbr", "acronym", "address", "applet", "b", "basefont", "bdo", "big",
+ "blockquote", "br", "button", "center", "cite", "code", "dfn", "dir", "div",
+ "dl", "em", "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "i", "iframe", "img", "input", "isindex", "kbd", "label", "map",
+ "menu", "noframes", "noscript", "object", "ol", "p", "pre", "q", "s",
+ "samp", "script", "select", "small", "span", "strike", "strong", "sub",
+ "sup", "table", "textarea", "tt", "u", "ul", "var"]}
+ ElementContent.keys.each do |k|
+ v = ElementContent[k]
+ if v.is_a? Array
+ ElementContent[k] = v.inject({}) do |h, name|
+ h[name.hash] = true
+ h
+ end
+ end
+ end
+
+ ElementInclusions =
+{"head"=>["link", "meta", "object", "script", "style"], "body"=>["del", "ins"]}
+ ElementInclusions.each do |k, v|
+ v.each do |name|
+ ElementContent[k][name.hash] = :allow
+ end
+ end
+
+ ElementExclusions =
+{"button"=>
+ ["a", "button", "fieldset", "form", "iframe", "input", "isindex", "label",
+ "select", "textarea"],
+ "a"=>["a"],
+ "dir"=>
+ ["address", "blockquote", "center", "dir", "div", "dl", "fieldset", "form",
+ "h1", "h2", "h3", "h4", "h5", "h6", "hr", "isindex", "menu", "noframes",
+ "noscript", "ol", "p", "pre", "table", "ul"],
+ "title"=>["link", "meta", "object", "script", "style"],
+ "pre"=>
+ ["applet", "basefont", "big", "font", "img", "object", "small", "sub",
+ "sup"],
+ "form"=>["form"],
+ "menu"=>
+ ["address", "blockquote", "center", "dir", "div", "dl", "fieldset", "form",
+ "h1", "h2", "h3", "h4", "h5", "h6", "hr", "isindex", "menu", "noframes",
+ "noscript", "ol", "p", "pre", "table", "ul"],
+ "label"=>["label"]}
+ ElementExclusions.each do |k, v|
+ v.each do |name|
+ ElementContent[k][name.hash] = :deny
+ end
+ end
+
+ OmittedAttrName =
+{"h6"=>
+ {"center"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "right"=>"align", "rtl"=>"dir"},
+ "object"=>
+ {"bottom"=>"align", "declare"=>"declare", "left"=>"align", "ltr"=>"dir",
+ "middle"=>"align", "right"=>"align", "rtl"=>"dir", "top"=>"align"},
+ "dl"=>{"compact"=>"compact", "ltr"=>"dir", "rtl"=>"dir"},
+ "p"=>
+ {"center"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "right"=>"align", "rtl"=>"dir"},
+ "acronym"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "code"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "ul"=>
+ {"circle"=>"type", "compact"=>"compact", "disc"=>"type", "ltr"=>"dir",
+ "rtl"=>"dir", "square"=>"type"},
+ "tt"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "label"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "form"=>{"get"=>"method", "ltr"=>"dir", "post"=>"method", "rtl"=>"dir"},
+ "q"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "thead"=>
+ {"baseline"=>"valign", "bottom"=>"valign", "center"=>"align",
+ "char"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "middle"=>"valign", "right"=>"align", "rtl"=>"dir", "top"=>"valign"},
+ "area"=>
+ {"circle"=>"shape", "default"=>"shape", "ltr"=>"dir", "nohref"=>"nohref",
+ "poly"=>"shape", "rect"=>"shape", "rtl"=>"dir"},
+ "td"=>
+ {"baseline"=>"valign", "bottom"=>"valign", "center"=>"align",
+ "char"=>"align", "col"=>"scope", "colgroup"=>"scope", "justify"=>"align",
+ "left"=>"align", "ltr"=>"dir", "middle"=>"valign", "nowrap"=>"nowrap",
+ "right"=>"align", "row"=>"scope", "rowgroup"=>"scope", "rtl"=>"dir",
+ "top"=>"valign"},
+ "title"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "dir"=>{"compact"=>"compact", "ltr"=>"dir", "rtl"=>"dir"},
+ "s"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "ol"=>{"compact"=>"compact", "ltr"=>"dir", "rtl"=>"dir"},
+ "hr"=>
+ {"center"=>"align", "left"=>"align", "ltr"=>"dir", "noshade"=>"noshade",
+ "right"=>"align", "rtl"=>"dir"},
+ "applet"=>
+ {"bottom"=>"align", "left"=>"align", "middle"=>"align", "right"=>"align",
+ "top"=>"align"},
+ "table"=>
+ {"above"=>"frame", "all"=>"rules", "below"=>"frame", "border"=>"frame",
+ "box"=>"frame", "center"=>"align", "cols"=>"rules", "groups"=>"rules",
+ "hsides"=>"frame", "left"=>"align", "lhs"=>"frame", "ltr"=>"dir",
+ "none"=>"rules", "rhs"=>"frame", "right"=>"align", "rows"=>"rules",
+ "rtl"=>"dir", "void"=>"frame", "vsides"=>"frame"},
+ "legend"=>
+ {"bottom"=>"align", "left"=>"align", "ltr"=>"dir", "right"=>"align",
+ "rtl"=>"dir", "top"=>"align"},
+ "cite"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "a"=>
+ {"circle"=>"shape", "default"=>"shape", "ltr"=>"dir", "poly"=>"shape",
+ "rect"=>"shape", "rtl"=>"dir"},
+ "html"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "u"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "blockquote"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "center"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "b"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "th"=>
+ {"baseline"=>"valign", "bottom"=>"valign", "center"=>"align",
+ "char"=>"align", "col"=>"scope", "colgroup"=>"scope", "justify"=>"align",
+ "left"=>"align", "ltr"=>"dir", "middle"=>"valign", "nowrap"=>"nowrap",
+ "right"=>"align", "row"=>"scope", "rowgroup"=>"scope", "rtl"=>"dir",
+ "top"=>"valign"},
+ "link"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "var"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "samp"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "div"=>
+ {"center"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "right"=>"align", "rtl"=>"dir"},
+ "textarea"=>
+ {"disabled"=>"disabled", "ltr"=>"dir", "readonly"=>"readonly", "rtl"=>"dir"},
+ "pre"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "head"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "span"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "br"=>{"all"=>"clear", "left"=>"clear", "none"=>"clear", "right"=>"clear"},
+ "script"=>{"defer"=>"defer"},
+ "noframes"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "style"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "meta"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "dt"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "option"=>
+ {"disabled"=>"disabled", "ltr"=>"dir", "rtl"=>"dir", "selected"=>"selected"},
+ "kbd"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "big"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "tfoot"=>
+ {"baseline"=>"valign", "bottom"=>"valign", "center"=>"align",
+ "char"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "middle"=>"valign", "right"=>"align", "rtl"=>"dir", "top"=>"valign"},
+ "sup"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "bdo"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "isindex"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "dfn"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "fieldset"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "em"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "font"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "tbody"=>
+ {"baseline"=>"valign", "bottom"=>"valign", "center"=>"align",
+ "char"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "middle"=>"valign", "right"=>"align", "rtl"=>"dir", "top"=>"valign"},
+ "noscript"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "li"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "col"=>
+ {"baseline"=>"valign", "bottom"=>"valign", "center"=>"align",
+ "char"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "middle"=>"valign", "right"=>"align", "rtl"=>"dir", "top"=>"valign"},
+ "small"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "dd"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "i"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "menu"=>{"compact"=>"compact", "ltr"=>"dir", "rtl"=>"dir"},
+ "strong"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "img"=>
+ {"bottom"=>"align", "ismap"=>"ismap", "left"=>"align", "ltr"=>"dir",
+ "middle"=>"align", "right"=>"align", "rtl"=>"dir", "top"=>"align"},
+ "optgroup"=>{"disabled"=>"disabled", "ltr"=>"dir", "rtl"=>"dir"},
+ "map"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "address"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "h1"=>
+ {"center"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "right"=>"align", "rtl"=>"dir"},
+ "sub"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "param"=>{"data"=>"valuetype", "object"=>"valuetype", "ref"=>"valuetype"},
+ "input"=>
+ {"bottom"=>"align", "button"=>"type", "checkbox"=>"type",
+ "checked"=>"checked", "disabled"=>"disabled", "file"=>"type",
+ "hidden"=>"type", "image"=>"type", "ismap"=>"ismap", "left"=>"align",
+ "ltr"=>"dir", "middle"=>"align", "password"=>"type", "radio"=>"type",
+ "readonly"=>"readonly", "reset"=>"type", "right"=>"align", "rtl"=>"dir",
+ "submit"=>"type", "text"=>"type", "top"=>"align"},
+ "h2"=>
+ {"center"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "right"=>"align", "rtl"=>"dir"},
+ "abbr"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "h3"=>
+ {"center"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "right"=>"align", "rtl"=>"dir"},
+ "strike"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "body"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "ins"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "button"=>
+ {"button"=>"type", "disabled"=>"disabled", "ltr"=>"dir", "reset"=>"type",
+ "rtl"=>"dir", "submit"=>"type"},
+ "h4"=>
+ {"center"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "right"=>"align", "rtl"=>"dir"},
+ "select"=>
+ {"disabled"=>"disabled", "ltr"=>"dir", "multiple"=>"multiple", "rtl"=>"dir"},
+ "caption"=>
+ {"bottom"=>"align", "left"=>"align", "ltr"=>"dir", "right"=>"align",
+ "rtl"=>"dir", "top"=>"align"},
+ "colgroup"=>
+ {"baseline"=>"valign", "bottom"=>"valign", "center"=>"align",
+ "char"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "middle"=>"valign", "right"=>"align", "rtl"=>"dir", "top"=>"valign"},
+ "tr"=>
+ {"baseline"=>"valign", "bottom"=>"valign", "center"=>"align",
+ "char"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "middle"=>"valign", "right"=>"align", "rtl"=>"dir", "top"=>"valign"},
+ "del"=>{"ltr"=>"dir", "rtl"=>"dir"},
+ "h5"=>
+ {"center"=>"align", "justify"=>"align", "left"=>"align", "ltr"=>"dir",
+ "right"=>"align", "rtl"=>"dir"},
+ "iframe"=>
+ {"0"=>"frameborder", "1"=>"frameborder", "auto"=>"scrolling",
+ "bottom"=>"align", "left"=>"align", "middle"=>"align", "no"=>"scrolling",
+ "right"=>"align", "top"=>"align", "yes"=>"scrolling"}}
+
+ # :startdoc:
+# The code above is auto-generated. Don't edit manually.
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/inspect.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/inspect.rb
new file mode 100644
index 0000000..e46433e
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/inspect.rb
@@ -0,0 +1,103 @@
+require 'pp'
+
+module Hpricot
+ # :stopdoc:
+ class Elements
+ def pretty_print(q)
+ q.object_group(self) { super }
+ end
+ alias inspect pretty_print_inspect
+ end
+
+ class Doc
+ def pretty_print(q)
+ q.object_group(self) { children.each {|elt| q.breakable; q.pp elt } if children }
+ end
+ alias inspect pretty_print_inspect
+ end
+
+ module Leaf
+ def pretty_print(q)
+ q.group(1, '{', '}') {
+ q.text self.class.name.sub(/.*::/,'').downcase
+ if rs = raw_string
+ rs.scan(/[^\r\n]*(?:\r\n?|\n|[^\r\n]\z)/) {|line|
+ q.breakable
+ q.pp line
+ }
+ elsif self.respond_to? :to_s
+ q.breakable
+ q.text self.to_s
+ end
+ }
+ end
+ alias inspect pretty_print_inspect
+ end
+
+ class Elem
+ def pretty_print(q)
+ if empty?
+ q.group(1, '{emptyelem', '}') {
+ q.breakable; pretty_print_stag q
+ }
+ else
+ q.group(1, "{elem", "}") {
+ q.breakable; pretty_print_stag q
+ if children
+ children.each {|elt| q.breakable; q.pp elt }
+ end
+ if etag
+ q.breakable; q.text etag
+ end
+ }
+ end
+ end
+ def pretty_print_stag(q)
+ q.group(1, '<', '>') {
+ q.text name
+
+ if raw_attributes
+ raw_attributes.each {|n, t|
+ q.breakable
+ if t
+ q.text "#{n}=\"#{Hpricot.uxs(t)}\""
+ else
+ q.text n
+ end
+ }
+ end
+ }
+ end
+ alias inspect pretty_print_inspect
+ end
+
+ class ETag
+ def pretty_print(q)
+ q.group(1, '</', '>') {
+ q.text name
+ }
+ end
+ alias inspect pretty_print_inspect
+ end
+
+ class Text
+ def pretty_print(q)
+ q.text content.dump
+ end
+ end
+
+ class BogusETag
+ def pretty_print(q)
+ q.group(1, '{', '}') {
+ q.text self.class.name.sub(/.*::/,'').downcase
+ if rs = raw_string
+ q.breakable
+ q.text rs
+ else
+ q.text "</#{name}>"
+ end
+ }
+ end
+ end
+ # :startdoc:
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/modules.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/modules.rb
new file mode 100644
index 0000000..46b2444
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/modules.rb
@@ -0,0 +1,40 @@
+module Hpricot
+ class Name; include Hpricot end
+ class Context; include Hpricot end
+
+ # :stopdoc:
+ module Tag; include Hpricot end
+ class ETag; include Tag end
+ # :startdoc:
+
+ module Node; include Hpricot end
+ class ETag; include Node end
+ module Container; include Node end
+ class Doc; include Container end
+ class Elem; include Container end
+
+ module Leaf; include Node end
+ class CData; include Leaf end
+ class Text; include Leaf end
+ class XMLDecl; include Leaf end
+ class DocType; include Leaf end
+ class ProcIns; include Leaf end
+ class Comment; include Leaf end
+ class BogusETag; include Leaf end
+
+ module Traverse end
+ module Container::Trav; include Traverse end
+ module Leaf::Trav; include Traverse end
+ class Doc; module Trav; include Container::Trav end; include Trav end
+ class Elem; module Trav; include Container::Trav end; include Trav end
+ class CData; module Trav; include Leaf::Trav end; include Trav end
+ class Text; module Trav; include Leaf::Trav end; include Trav end
+ class XMLDecl; module Trav; include Leaf::Trav end; include Trav end
+ class DocType; module Trav; include Leaf::Trav end; include Trav end
+ class ProcIns; module Trav; include Leaf::Trav end; include Trav end
+ class Comment; module Trav; include Leaf::Trav end; include Trav end
+ class BogusETag; module Trav; include Leaf::Trav end; include Trav end
+
+ class Error < StandardError; end
+end
+
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/parse.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/parse.rb
new file mode 100644
index 0000000..4c9711e
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/parse.rb
@@ -0,0 +1,38 @@
+require 'hpricot/htmlinfo'
+
+def Hpricot(input = nil, opts = {}, &blk)
+ Hpricot.make(input, opts, &blk)
+end
+
+module Hpricot
+ # Exception class used for any errors related to deficiencies in the system when
+ # handling the character encodings of a document.
+ class EncodingError < StandardError; end
+
+ # Hpricot.parse parses <i>input</i> and return a document tree.
+ # represented by Hpricot::Doc.
+ def Hpricot.parse(input = nil, opts = {}, &blk)
+ make(input, opts, &blk)
+ end
+
+ # Hpricot::XML parses <i>input</i>, disregarding all the HTML rules
+ # and returning a document tree.
+ def Hpricot.XML(input = nil, opts = {}, &blk)
+ opts.merge! :xml => true
+ make(input, opts, &blk)
+ end
+
+ # :stopdoc:
+
+ def Hpricot.make(input = nil, opts = {}, &blk)
+ if blk
+ doc = Hpricot.build(&blk)
+ doc.instance_variable_set("@options", opts)
+ doc
+ else
+ Hpricot.scan(input, opts)
+ end
+ end
+
+ # :startdoc:
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/tag.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/tag.rb
new file mode 100644
index 0000000..ea5868a
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/tag.rb
@@ -0,0 +1,202 @@
+module Hpricot
+ # :stopdoc:
+
+ class Doc
+ def output(out, opts = {})
+ children.each do |n|
+ n.output(out, opts)
+ end if children
+ out
+ end
+ def make(input = nil, &blk)
+ Hpricot.make(input, @options, &blk).children
+ end
+ def altered!; end
+ def inspect_tree
+ children.map { |x| x.inspect_tree }.join if children
+ end
+ end
+
+ module Node
+ def html_quote(str)
+ "\"" + str.gsub('"', '\\"') + "\""
+ end
+ def clear_raw; end
+ def if_output(opts)
+ if opts[:preserve] and not raw_string.nil?
+ raw_string
+ else
+ yield opts
+ end
+ end
+ def pathname; self.name end
+ def altered!
+ clear_raw
+ end
+ def inspect_tree(depth = 0)
+ %{#{" " * depth}} + self.class.name.split(/::/).last.downcase + "\n"
+ end
+ end
+
+ class Elem
+ TITLES = {:title => :h1, :subtitle => :h2, :tagline => :h3, :caption => :h4}
+ def initialize tag, attrs = nil, children = nil, etag = nil
+ self.name, self.raw_attributes, self.children, self.etag =
+ tag, attrs, children, etag
+ end
+ def empty?; children.nil? or children.empty? end
+ def attributes
+ if raw_attributes
+ raw_attributes.inject({}) do |hsh, (k, v)|
+ hsh[k] = Hpricot.uxs(v)
+ hsh
+ end
+ else
+ {}
+ end
+ end
+ def to_plain_text
+ if self.name == 'br'
+ "\n"
+ elsif self.name == 'p'
+ "\n\n" + super + "\n\n"
+ elsif self.name == 'a' and self.has_attribute?('href')
+ "#{super} [#{self['href']}]"
+ elsif self.name == 'img' and self.has_attribute?('src')
+ "[img:#{self['src']}]"
+ else
+ super
+ end
+ end
+ def pathname; self.name end
+ def output(out, opts = {})
+ out <<
+ if_output(opts) do
+ "<#{name}#{attributes_as_html}" +
+ ((empty? and not etag) ? " /" : "") +
+ ">"
+ end
+ if children
+ children.each { |n| n.output(out, opts) }
+ end
+ if opts[:preserve]
+ out << etag if etag
+ elsif etag or !empty?
+ out << "</#{name}>"
+ end
+ out
+ end
+ def attributes_as_html
+ if raw_attributes
+ raw_attributes.map do |aname, aval|
+ " #{aname}" +
+ (aval ? "=#{html_quote aval}" : "")
+ end.join
+ end
+ end
+ def inspect_tree(depth = 0)
+ %{#{" " * depth}} + name + "\n" +
+ (children ? children.map { |x| x.inspect_tree(depth + 1) }.join : "")
+ end
+ end
+
+ class BogusETag
+ def initialize name; self.name = name end
+ def output(out, opts = {})
+ out <<
+ if_output(opts) do
+ "</#{name}>"
+ end
+ end
+ end
+
+ class ETag < BogusETag
+ def output(out, opts = {}); out << if_output(opts) { '' }; end
+ end
+
+ class Text
+ def initialize content; self.content = content end
+ def pathname; "text()" end
+ def to_s
+ Hpricot.uxs(content)
+ end
+ alias_method :inner_text, :to_s
+ alias_method :to_plain_text, :to_s
+ def << str; self.content << str end
+ def output(out, opts = {})
+ out <<
+ if_output(opts) do
+ content.to_s
+ end
+ end
+ end
+
+ class CData
+ def initialize content; self.content = content end
+ alias_method :to_s, :content
+ alias_method :to_plain_text, :content
+ alias_method :inner_text, :content
+ def raw_string; "<![CDATA[#{content}]]>" end
+ def output(out, opts = {})
+ out <<
+ if_output(opts) do
+ "<![CDATA[#{content}]]>"
+ end
+ end
+ end
+
+ class XMLDecl
+ def pathname; "xmldecl()" end
+ def output(out, opts = {})
+ out <<
+ if_output(opts) do
+ "<?xml version=\"#{version}\"" +
+ (encoding ? " encoding=\"#{encoding}\"" : "") +
+ (standalone != nil ? " standalone=\"#{standalone ? 'yes' : 'no'}\"" : "") +
+ "?>"
+ end
+ end
+ end
+
+ class DocType
+ attr_accessor :target, :public_id, :system_id
+ def initialize target, pub, sys
+ self.target, self.public_id, self.system_id = target, pub, sys
+ end
+ def pathname; "doctype()" end
+ def output(out, opts = {})
+ out <<
+ if_output(opts) do
+ "<!DOCTYPE #{target} " +
+ (public_id ? "PUBLIC \"#{public_id}\"" : "SYSTEM") +
+ (system_id ? " #{html_quote(system_id)}" : "") + ">"
+ end
+ end
+ end
+
+ class ProcIns
+ def pathname; "procins()" end
+ def raw_string; output("") end
+ def output(out, opts = {})
+ out <<
+ if_output(opts) do
+ "<?#{target}" +
+ (content ? " #{content}" : "") +
+ "?>"
+ end
+ end
+ end
+
+ class Comment
+ def pathname; "comment()" end
+ def raw_string; "<!--#{content}-->" end
+ def output(out, opts = {})
+ out <<
+ if_output(opts) do
+ "<!--#{content}-->"
+ end
+ end
+ end
+
+ # :startdoc:
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/tags.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/tags.rb
new file mode 100644
index 0000000..6c2db5f
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/tags.rb
@@ -0,0 +1,164 @@
+module Hpricot
+
+ FORM_TAGS = [ :form, :input, :select, :textarea ]
+ SELF_CLOSING_TAGS = [ :base, :meta, :link, :hr, :br, :param, :img, :area, :input, :col ]
+
+ # Common sets of attributes.
+ AttrCore = [:id, :class, :style, :title]
+ AttrI18n = [:lang, 'xml:lang'.intern, :dir]
+ AttrEvents = [:onclick, :ondblclick, :onmousedown, :onmouseup, :onmouseover, :onmousemove,
+ :onmouseout, :onkeypress, :onkeydown, :onkeyup]
+ AttrFocus = [:accesskey, :tabindex, :onfocus, :onblur]
+ AttrHAlign = [:align, :char, :charoff]
+ AttrVAlign = [:valign]
+ Attrs = AttrCore + AttrI18n + AttrEvents
+
+ # All the tags and attributes from XHTML 1.0 Strict
+ class XHTMLStrict
+ class << self
+ attr_accessor :tags, :tagset, :forms, :self_closing, :doctype
+ end
+ @doctype = ["-//W3C//DTD XHTML 1.0 Strict//EN", "DTD/xhtml1-strict.dtd"]
+ @tagset = {
+ :html => AttrI18n + [:id, :xmlns],
+ :head => AttrI18n + [:id, :profile],
+ :title => AttrI18n + [:id],
+ :base => [:href, :id],
+ :meta => AttrI18n + [:id, :http, :name, :content, :scheme, 'http-equiv'.intern],
+ :link => Attrs + [:charset, :href, :hreflang, :type, :rel, :rev, :media],
+ :style => AttrI18n + [:id, :type, :media, :title, 'xml:space'.intern],
+ :script => [:id, :charset, :type, :src, :defer, 'xml:space'.intern],
+ :noscript => Attrs,
+ :body => Attrs + [:onload, :onunload],
+ :div => Attrs,
+ :p => Attrs,
+ :ul => Attrs,
+ :ol => Attrs,
+ :li => Attrs,
+ :dl => Attrs,
+ :dt => Attrs,
+ :dd => Attrs,
+ :address => Attrs,
+ :hr => Attrs,
+ :pre => Attrs + ['xml:space'.intern],
+ :blockquote => Attrs + [:cite],
+ :ins => Attrs + [:cite, :datetime],
+ :del => Attrs + [:cite, :datetime],
+ :a => Attrs + AttrFocus + [:charset, :type, :name, :href, :hreflang, :rel, :rev, :shape, :coords],
+ :span => Attrs,
+ :bdo => AttrCore + AttrEvents + [:lang, 'xml:lang'.intern, :dir],
+ :br => AttrCore,
+ :em => Attrs,
+ :strong => Attrs,
+ :dfn => Attrs,
+ :code => Attrs,
+ :samp => Attrs,
+ :kbd => Attrs,
+ :var => Attrs,
+ :cite => Attrs,
+ :abbr => Attrs,
+ :acronym => Attrs,
+ :q => Attrs + [:cite],
+ :sub => Attrs,
+ :sup => Attrs,
+ :tt => Attrs,
+ :i => Attrs,
+ :b => Attrs,
+ :big => Attrs,
+ :small => Attrs,
+ :object => Attrs + [:declare, :classid, :codebase, :data, :type, :codetype, :archive, :standby, :height, :width, :usemap, :name, :tabindex],
+ :param => [:id, :name, :value, :valuetype, :type],
+ :img => Attrs + [:src, :alt, :longdesc, :height, :width, :usemap, :ismap],
+ :map => AttrI18n + AttrEvents + [:id, :class, :style, :title, :name],
+ :area => Attrs + AttrFocus + [:shape, :coords, :href, :nohref, :alt],
+ :form => Attrs + [:action, :method, :enctype, :onsubmit, :onreset, :accept, :accept],
+ :label => Attrs + [:for, :accesskey, :onfocus, :onblur],
+ :input => Attrs + AttrFocus + [:type, :name, :value, :checked, :disabled, :readonly, :size, :maxlength, :src, :alt, :usemap, :onselect, :onchange, :accept],
+ :select => Attrs + [:name, :size, :multiple, :disabled, :tabindex, :onfocus, :onblur, :onchange],
+ :optgroup => Attrs + [:disabled, :label],
+ :option => Attrs + [:selected, :disabled, :label, :value],
+ :textarea => Attrs + AttrFocus + [:name, :rows, :cols, :disabled, :readonly, :onselect, :onchange],
+ :fieldset => Attrs,
+ :legend => Attrs + [:accesskey],
+ :button => Attrs + AttrFocus + [:name, :value, :type, :disabled],
+ :table => Attrs + [:summary, :width, :border, :frame, :rules, :cellspacing, :cellpadding],
+ :caption => Attrs,
+ :colgroup => Attrs + AttrHAlign + AttrVAlign + [:span, :width],
+ :col => Attrs + AttrHAlign + AttrVAlign + [:span, :width],
+ :thead => Attrs + AttrHAlign + AttrVAlign,
+ :tfoot => Attrs + AttrHAlign + AttrVAlign,
+ :tbody => Attrs + AttrHAlign + AttrVAlign,
+ :tr => Attrs + AttrHAlign + AttrVAlign,
+ :th => Attrs + AttrHAlign + AttrVAlign + [:abbr, :axis, :headers, :scope, :rowspan, :colspan],
+ :td => Attrs + AttrHAlign + AttrVAlign + [:abbr, :axis, :headers, :scope, :rowspan, :colspan],
+ :h1 => Attrs,
+ :h2 => Attrs,
+ :h3 => Attrs,
+ :h4 => Attrs,
+ :h5 => Attrs,
+ :h6 => Attrs
+ }
+
+ @tags = @tagset.keys
+ @forms = @tags & FORM_TAGS
+ @self_closing = @tags & SELF_CLOSING_TAGS
+ end
+
+ # Additional tags found in XHTML 1.0 Transitional
+ class XHTMLTransitional
+ class << self
+ attr_accessor :tags, :tagset, :forms, :self_closing, :doctype
+ end
+ @doctype = ["-//W3C//DTD XHTML 1.0 Transitional//EN", "DTD/xhtml1-transitional.dtd"]
+ @tagset = XHTMLStrict.tagset.merge \
+ :strike => Attrs,
+ :center => Attrs,
+ :dir => Attrs + [:compact],
+ :noframes => Attrs,
+ :basefont => [:id, :size, :color, :face],
+ :u => Attrs,
+ :menu => Attrs + [:compact],
+ :iframe => AttrCore + [:longdesc, :name, :src, :frameborder, :marginwidth, :marginheight, :scrolling, :align, :height, :width],
+ :font => AttrCore + AttrI18n + [:size, :color, :face],
+ :s => Attrs,
+ :applet => AttrCore + [:codebase, :archive, :code, :object, :alt, :name, :width, :height, :align, :hspace, :vspace],
+ :isindex => AttrCore + AttrI18n + [:prompt]
+
+ # Additional attributes found in XHTML 1.0 Transitional
+ { :script => [:language],
+ :a => [:target],
+ :td => [:bgcolor, :nowrap, :width, :height],
+ :p => [:align],
+ :h5 => [:align],
+ :h3 => [:align],
+ :li => [:type, :value],
+ :div => [:align],
+ :pre => [:width],
+ :body => [:background, :bgcolor, :text, :link, :vlink, :alink],
+ :ol => [:type, :compact, :start],
+ :h4 => [:align],
+ :h2 => [:align],
+ :object => [:align, :border, :hspace, :vspace],
+ :img => [:name, :align, :border, :hspace, :vspace],
+ :link => [:target],
+ :legend => [:align],
+ :dl => [:compact],
+ :input => [:align],
+ :h6 => [:align],
+ :hr => [:align, :noshade, :size, :width],
+ :base => [:target],
+ :ul => [:type, :compact],
+ :br => [:clear],
+ :form => [:name, :target],
+ :area => [:target],
+ :h1 => [:align]
+ }.each do |k, v|
+ @tagset[k] += v
+ end
+
+ @tags = @tagset.keys
+ @forms = @tags & FORM_TAGS
+ @self_closing = @tags & SELF_CLOSING_TAGS
+ end
+
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/traverse.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/traverse.rb
new file mode 100644
index 0000000..15541d6
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/traverse.rb
@@ -0,0 +1,838 @@
+require 'hpricot/elements'
+require 'uri'
+
+module Hpricot
+ module Traverse
+ # Is this object the enclosing HTML or XML document?
+ def doc?() Doc::Trav === self end
+ # Is this object an HTML or XML element?
+ def elem?() Elem::Trav === self end
+ # Is this object an HTML text node?
+ def text?() Text::Trav === self end
+ # Is this object an XML declaration?
+ def xmldecl?() XMLDecl::Trav === self end
+ # Is this object a doctype tag?
+ def doctype?() DocType::Trav === self end
+ # Is this object an XML processing instruction?
+ def procins?() ProcIns::Trav === self end
+ # Is this object a comment?
+ def comment?() Comment::Trav === self end
+ # Is this object a stranded end tag?
+ def bogusetag?() BogusETag::Trav === self end
+
+ # Parses an HTML string, making an HTML fragment based on
+ # the options used to create the container document.
+ def make(input = nil, &blk)
+ if parent and parent.respond_to? :make
+ parent.make(input, &blk)
+ else
+ Hpricot.make(input, &blk).children
+ end
+ end
+
+ # Builds an HTML string from this node and its contents.
+ # If you need to write to a stream, try calling <tt>output(io)</tt>
+ # as a method on this object.
+ def to_html
+ output("")
+ end
+ alias_method :to_s, :to_html
+
+ # Attempts to preserve the original HTML of the document, only
+ # outputing new tags for elements which have changed.
+ def to_original_html
+ output("", :preserve => true)
+ end
+
+ def index(name)
+ i = 0
+ return i if name == "*"
+ children.each do |x|
+ return i if (x.respond_to?(:name) and name == x.name) or
+ (x.text? and name == "text()")
+ i += 1
+ end if children
+ -1
+ end
+
+ # Puts together an array of neighboring nodes based on their proximity
+ # to this node. So, for example, to get the next node, you could use
+ # <tt>nodes_at(1). Or, to get the previous node, use <tt>nodes_at(1)</tt>.
+ #
+ # This method also accepts ranges and sets of numbers.
+ #
+ # ele.nodes_at(-3..-1, 1..3) # gets three nodes before and three after
+ # ele.nodes_at(1, 5, 7) # gets three nodes at offsets below the current node
+ # ele.nodes_at(0, 5..6) # the current node and two others
+ def nodes_at(*pos)
+ sib = parent.children
+ i, si = 0, sib.index(self)
+ pos.map! do |r|
+ if r.is_a?(Range) and r.begin.is_a?(String)
+ r = Range.new(parent.index(r.begin)-si, parent.index(r.end)-si, r.exclude_end?)
+ end
+ r
+ end
+ p pos
+ Elements[*
+ sib.select do |x|
+ sel =
+ case i - si when *pos
+ true
+ end
+ i += 1
+ sel
+ end
+ ]
+ end
+
+ # Returns the node neighboring this node to the south: just below it.
+ # This method includes text nodes and comments and such.
+ def next
+ sib = parent.children
+ sib[sib.index(self) + 1] if parent
+ end
+ alias_method :next_node, :next
+
+ # Returns to node neighboring this node to the north: just above it.
+ # This method includes text nodes and comments and such.
+ def previous
+ sib = parent.children
+ x = sib.index(self) - 1
+ sib[x] if sib and x >= 0
+ end
+ alias_method :previous_node, :previous
+
+ # Find all preceding nodes.
+ def preceding
+ sibs = parent.children
+ si = sibs.index(self)
+ return Elements[*sibs[0...si]]
+ end
+
+ # Find all nodes which follow the current one.
+ def following
+ sibs = parent.children
+ si = sibs.index(self) + 1
+ return Elements[*sibs[si...sibs.length]]
+ end
+
+ # Adds elements immediately after this element, contained in the +html+ string.
+ def after(html = nil, &blk)
+ parent.insert_after(make(html, &blk), self)
+ end
+
+ # Adds elements immediately before this element, contained in the +html+ string.
+ def before(html = nil, &blk)
+ parent.insert_before(make(html, &blk), self)
+ end
+
+
+ # Replace this element and its contents with the nodes contained
+ # in the +html+ string.
+ def swap(html = nil, &blk)
+ parent.altered!
+ parent.replace_child(self, make(html, &blk))
+ end
+
+ def get_subnode(*indexes)
+ n = self
+ indexes.each {|index|
+ n = n.get_subnode_internal(index)
+ }
+ n
+ end
+
+ # Builds a string from the text contained in this node. All
+ # HTML elements are removed.
+ def to_plain_text
+ if respond_to?(:children) and children
+ children.map { |x| x.to_plain_text }.join.strip.gsub(/\n{2,}/, "\n\n")
+ else
+ ""
+ end
+ end
+
+ # Builds a string from the text contained in this node. All
+ # HTML elements are removed.
+ def inner_text
+ if respond_to?(:children) and children
+ children.map { |x| x.inner_text }.join
+ else
+ ""
+ end
+ end
+ alias_method :innerText, :inner_text
+
+ # Builds an HTML string from the contents of this node.
+ def html(inner = nil, &blk)
+ if inner or blk
+ altered!
+ case inner
+ when Array
+ self.children = inner
+ else
+ self.children = make(inner, &blk)
+ end
+ reparent self.children
+ else
+ if respond_to?(:children) and children
+ children.map { |x| x.output("") }.join
+ else
+ ""
+ end
+ end
+ end
+ alias_method :inner_html, :html
+ alias_method :innerHTML, :inner_html
+
+ # Inserts new contents into the current node, based on
+ # the HTML contained in string +inner+.
+ def inner_html=(inner)
+ html(inner || [])
+ end
+ alias_method :innerHTML=, :inner_html=
+
+ def reparent(nodes)
+ altered!
+ [*nodes].each { |e| e.parent = self }
+ end
+ private :reparent
+
+ def clean_path(path)
+ path.gsub(/^\s+|\s+$/, '')
+ end
+
+ # Builds a unique XPath string for this node, from the
+ # root of the document containing it.
+ def xpath
+ if elem? and has_attribute? 'id'
+ "//#{self.name}[@id='#{get_attribute('id')}']"
+ else
+ sim, id = 0, 0, 0
+ parent.children.each do |e|
+ id = sim if e == self
+ sim += 1 if e.pathname == self.pathname
+ end if parent.children
+ p = File.join(parent.xpath, self.pathname)
+ p += "[#{id+1}]" if sim >= 2
+ p
+ end
+ end
+
+ # Builds a unique CSS string for this node, from the
+ # root of the document containing it.
+ def css_path
+ if elem? and has_attribute? 'id'
+ "##{get_attribute('id')}"
+ else
+ sim, i, id = 0, 0, 0
+ parent.children.each do |e|
+ id = sim if e == self
+ sim += 1 if e.pathname == self.pathname
+ end if parent.children
+ p = parent.css_path
+ p = p ? "#{p} > #{self.pathname}" : self.pathname
+ p += ":nth(#{id})" if sim >= 2
+ p
+ end
+ end
+
+ def node_position
+ parent.children.index(self)
+ end
+
+ def position
+ parent.children_of_type(self.pathname).index(self)
+ end
+
+ # Searches this node for all elements matching
+ # the CSS or XPath +expr+. Returns an Elements array
+ # containing the matching nodes. If +blk+ is given, it
+ # is used to iterate through the matching set.
+ def search(expr, &blk)
+ if Range === expr
+ return Elements.expand(at(expr.begin), at(expr.end), expr.exclude_end?)
+ end
+ last = nil
+ nodes = [self]
+ done = []
+ expr = expr.to_s
+ hist = []
+ until expr.empty?
+ expr = clean_path(expr)
+ expr.gsub!(%r!^//!, '')
+
+ case expr
+ when %r!^/?\.\.!
+ last = expr = $'
+ nodes.map! { |node| node.parent }
+ when %r!^[>/]\s*!
+ last = expr = $'
+ nodes = Elements[*nodes.map { |node| node.children if node.respond_to? :children }.flatten.compact]
+ when %r!^\+!
+ last = expr = $'
+ nodes.map! do |node|
+ siblings = node.parent.children
+ siblings[siblings.index(node)+1]
+ end
+ nodes.compact!
+ when %r!^~!
+ last = expr = $'
+ nodes.map! do |node|
+ siblings = node.parent.children
+ siblings[(siblings.index(node)+1)..-1]
+ end
+ nodes.flatten!
+ when %r!^[|,]!
+ last = expr = " #$'"
+ nodes.shift if nodes.first == self
+ done += nodes
+ nodes = [self]
+ else
+ m = expr.match(%r!^([#.]?)([a-z0-9\\*_-]*)!i).to_a
+ after = $'
+ mt = after[%r!:[a-z0-9\\*_-]+!i, 0]
+ oop = false
+ if mt and not (mt == ":not" or Traverse.method_defined? "filter[#{mt}]")
+ after = $'
+ m[2] += mt
+ expr = after
+ end
+ if m[1] == '#'
+ oid = get_element_by_id(m[2])
+ nodes = oid ? [oid] : []
+ expr = after
+ else
+ m[2] = "*" if after =~ /^\(\)/ || m[2] == "" || m[1] == "."
+ ret = []
+ nodes.each do |node|
+ case m[2]
+ when '*'
+ node.traverse_element { |n| ret << n }
+ else
+ if node.respond_to? :get_elements_by_tag_name
+ ret += [*node.get_elements_by_tag_name(m[2])] - [*(node unless last)]
+ end
+ end
+ end
+ nodes = ret
+ end
+ last = nil
+ end
+
+ hist << expr
+ break if hist[-1] == hist[-2]
+ nodes, expr = Elements.filter(nodes, expr)
+ end
+ nodes = done + nodes.flatten.uniq
+ if blk
+ nodes.each(&blk)
+ self
+ else
+ Elements[*nodes]
+ end
+ end
+ alias_method :/, :search
+
+ # Find the first matching node for the CSS or XPath
+ # +expr+ string.
+ def at(expr)
+ search(expr).first
+ end
+ alias_method :%, :at
+
+ # +traverse_element+ traverses elements in the tree.
+ # It yields elements in depth first order.
+ #
+ # If _names_ are empty, it yields all elements.
+ # If non-empty _names_ are given, it should be list of universal names.
+ #
+ # A nested element is yielded in depth first order as follows.
+ #
+ # t = Hpricot('<a id=0><b><a id=1 /></b><c id=2 /></a>')
+ # t.traverse_element("a", "c") {|e| p e}
+ # # =>
+ # {elem <a id="0"> {elem <b> {emptyelem <a id="1">} </b>} {emptyelem <c id="2">} </a>}
+ # {emptyelem <a id="1">}
+ # {emptyelem <c id="2">}
+ #
+ # Universal names are specified as follows.
+ #
+ # t = Hpricot(<<'End')
+ # <html>
+ # <meta name="robots" content="index,nofollow">
+ # <meta name="author" content="Who am I?">
+ # </html>
+ # End
+ # t.traverse_element("{http://www.w3.org/1999/xhtml}meta") {|e| p e}
+ # # =>
+ # {emptyelem <{http://www.w3.org/1999/xhtml}meta name="robots" content="index,nofollow">}
+ # {emptyelem <{http://www.w3.org/1999/xhtml}meta name="author" content="Who am I?">}
+ #
+ def traverse_element(*names, &block) # :yields: element
+ if names.empty?
+ traverse_all_element(&block)
+ else
+ name_set = {}
+ names.each {|n| name_set[n] = true }
+ traverse_some_element(name_set, &block)
+ end
+ nil
+ end
+
+ # Find children of a given +tag_name+.
+ #
+ # ele.children_of_type('p')
+ # #=> [...array of paragraphs...]
+ #
+ def children_of_type(tag_name)
+ if respond_to? :children
+ children.find_all do |x|
+ x.respond_to?(:pathname) && x.pathname == tag_name
+ end
+ end
+ end
+
+ end
+
+ module Container::Trav
+ # Return all children of this node which can contain other
+ # nodes. This is a good way to get all HTML elements which
+ # aren't text, comment, doctype or processing instruction nodes.
+ def containers
+ children.grep(Container::Trav)
+ end
+
+ # Returns the container node neighboring this node to the south: just below it.
+ # By "container" node, I mean: this method does not find text nodes or comments or cdata or any of that.
+ # See Hpricot::Traverse#next_node if you need to hunt out all kinds of nodes.
+ def next_sibling
+ sib = parent.containers
+ sib[sib.index(self) + 1] if parent
+ end
+
+ # Returns the container node neighboring this node to the north: just above it.
+ # By "container" node, I mean: this method does not find text nodes or comments or cdata or any of that.
+ # See Hpricot::Traverse#previous_node if you need to hunt out all kinds of nodes.
+ def previous_sibling
+ sib = parent.containers
+ x = sib.index(self) - 1
+ sib[x] if sib and x >= 0
+ end
+
+ # Find all preceding sibling elements. Like the other "sibling" methods, this weeds
+ # out text and comment nodes.
+ def preceding_siblings()
+ sibs = parent.containers
+ si = sibs.index(self)
+ return Elements[*sibs[0...si]]
+ end
+
+ # Find sibling elements which follow the current one. Like the other "sibling" methods, this weeds
+ # out text and comment nodes.
+ def following_siblings()
+ sibs = parent.containers
+ si = sibs.index(self) + 1
+ return Elements[*sibs[si...sibs.length]]
+ end
+
+ # Puts together an array of neighboring sibling elements based on their proximity
+ # to this element.
+ #
+ # This method accepts ranges and sets of numbers.
+ #
+ # ele.siblings_at(-3..-1, 1..3) # gets three elements before and three after
+ # ele.siblings_at(1, 5, 7) # gets three elements at offsets below the current element
+ # ele.siblings_at(0, 5..6) # the current element and two others
+ #
+ # Like the other "sibling" methods, this doesn't find text and comment nodes.
+ # Use nodes_at to include those nodes.
+ def siblings_at(*pos)
+ sib = parent.containers
+ i, si = 0, sib.index(self)
+ Elements[*
+ sib.select do |x|
+ sel = case i - si when *pos
+ true
+ end
+ i += 1
+ sel
+ end
+ ]
+ end
+
+ # Replace +old+, a child of the current node, with +new+ node.
+ def replace_child(old, new)
+ reparent new
+ children[children.index(old), 1] = [*new]
+ end
+
+ # Insert +nodes+, an array of HTML elements or a single element,
+ # before the node +ele+, a child of the current node.
+ def insert_before(nodes, ele)
+ case nodes
+ when Array
+ nodes.each { |n| insert_before(n, ele) }
+ else
+ reparent nodes
+ children[children.index(ele) || 0, 0] = nodes
+ end
+ end
+
+ # Insert +nodes+, an array of HTML elements or a single element,
+ # after the node +ele+, a child of the current node.
+ def insert_after(nodes, ele)
+ case nodes
+ when Array
+ nodes.reverse_each { |n| insert_after(n, ele) }
+ else
+ reparent nodes
+ idx = children.index(ele)
+ children[idx ? idx + 1 : children.length, 0] = nodes
+ end
+ end
+
+ # +each_child+ iterates over each child.
+ def each_child(&block) # :yields: child_node
+ children.each(&block) if children
+ nil
+ end
+
+ # +each_child_with_index+ iterates over each child.
+ def each_child_with_index(&block) # :yields: child_node, index
+ children.each_with_index(&block) if children
+ nil
+ end
+
+ # +find_element+ searches an element which universal name is specified by
+ # the arguments.
+ # It returns nil if not found.
+ def find_element(*names)
+ traverse_element(*names) {|e| return e }
+ nil
+ end
+
+ # Returns a list of CSS classes to which this element belongs.
+ def classes
+ get_attribute('class').to_s.strip.split(/\s+/)
+ end
+
+ def get_element_by_id(id)
+ traverse_all_element do |ele|
+ if ele.elem? and eid = ele.get_attribute('id')
+ return ele if eid.to_s == id
+ end
+ end
+ nil
+ end
+
+ def get_elements_by_tag_name(*a)
+ list = Elements[]
+ a.delete("*")
+ traverse_element(*a.map { |tag| [tag, "{http://www.w3.org/1999/xhtml}#{tag}"] }.flatten) do |e|
+ list << e if e.elem?
+ end
+ list
+ end
+
+ def each_hyperlink_attribute
+ traverse_element(
+ '{http://www.w3.org/1999/xhtml}a',
+ '{http://www.w3.org/1999/xhtml}area',
+ '{http://www.w3.org/1999/xhtml}link',
+ '{http://www.w3.org/1999/xhtml}img',
+ '{http://www.w3.org/1999/xhtml}object',
+ '{http://www.w3.org/1999/xhtml}q',
+ '{http://www.w3.org/1999/xhtml}blockquote',
+ '{http://www.w3.org/1999/xhtml}ins',
+ '{http://www.w3.org/1999/xhtml}del',
+ '{http://www.w3.org/1999/xhtml}form',
+ '{http://www.w3.org/1999/xhtml}input',
+ '{http://www.w3.org/1999/xhtml}head',
+ '{http://www.w3.org/1999/xhtml}base',
+ '{http://www.w3.org/1999/xhtml}script') {|elem|
+ case elem.name
+ when %r{\{http://www.w3.org/1999/xhtml\}(?:base|a|area|link)\z}i
+ attrs = ['href']
+ when %r{\{http://www.w3.org/1999/xhtml\}(?:img)\z}i
+ attrs = ['src', 'longdesc', 'usemap']
+ when %r{\{http://www.w3.org/1999/xhtml\}(?:object)\z}i
+ attrs = ['classid', 'codebase', 'data', 'usemap']
+ when %r{\{http://www.w3.org/1999/xhtml\}(?:q|blockquote|ins|del)\z}i
+ attrs = ['cite']
+ when %r{\{http://www.w3.org/1999/xhtml\}(?:form)\z}i
+ attrs = ['action']
+ when %r{\{http://www.w3.org/1999/xhtml\}(?:input)\z}i
+ attrs = ['src', 'usemap']
+ when %r{\{http://www.w3.org/1999/xhtml\}(?:head)\z}i
+ attrs = ['profile']
+ when %r{\{http://www.w3.org/1999/xhtml\}(?:script)\z}i
+ attrs = ['src', 'for']
+ end
+ attrs.each {|attr|
+ if hyperlink = elem.get_attribute(attr)
+ yield elem, attr, hyperlink
+ end
+ }
+ }
+ end
+ private :each_hyperlink_attribute
+
+ # +each_hyperlink_uri+ traverses hyperlinks such as HTML href attribute
+ # of A element.
+ #
+ # It yields Hpricot::Text and URI for each hyperlink.
+ #
+ # The URI objects are created with a base URI which is given by
+ # HTML BASE element or the argument ((|base_uri|)).
+ # +each_hyperlink_uri+ doesn't yields href of the BASE element.
+ def each_hyperlink_uri(base_uri=nil) # :yields: hyperlink, uri
+ base_uri = URI.parse(base_uri) if String === base_uri
+ links = []
+ each_hyperlink_attribute {|elem, attr, hyperlink|
+ if %r{\{http://www.w3.org/1999/xhtml\}(?:base)\z}i =~ elem.name
+ base_uri = URI.parse(hyperlink.to_s)
+ else
+ links << hyperlink
+ end
+ }
+ if base_uri
+ links.each {|hyperlink| yield hyperlink, base_uri + hyperlink.to_s }
+ else
+ links.each {|hyperlink| yield hyperlink, URI.parse(hyperlink.to_s) }
+ end
+ end
+
+ # +each_hyperlink+ traverses hyperlinks such as HTML href attribute
+ # of A element.
+ #
+ # It yields Hpricot::Text.
+ #
+ # Note that +each_hyperlink+ yields HTML href attribute of BASE element.
+ def each_hyperlink # :yields: text
+ links = []
+ each_hyperlink_attribute {|elem, attr, hyperlink|
+ yield hyperlink
+ }
+ end
+
+ # +each_uri+ traverses hyperlinks such as HTML href attribute
+ # of A element.
+ #
+ # It yields URI for each hyperlink.
+ #
+ # The URI objects are created with a base URI which is given by
+ # HTML BASE element or the argument ((|base_uri|)).
+ def each_uri(base_uri=nil) # :yields: URI
+ each_hyperlink_uri(base_uri) {|hyperlink, uri| yield uri }
+ end
+ end
+
+ # :stopdoc:
+ module Doc::Trav
+ def traverse_all_element(&block)
+ children.each {|c| c.traverse_all_element(&block) } if children
+ end
+ def xpath
+ "/"
+ end
+ def css_path
+ nil
+ end
+ end
+
+ module Elem::Trav
+ def traverse_all_element(&block)
+ yield self
+ children.each {|c| c.traverse_all_element(&block) } if children
+ end
+ end
+
+ module Leaf::Trav
+ def traverse_all_element
+ yield self
+ end
+ end
+
+ module Doc::Trav
+ def traverse_some_element(name_set, &block)
+ children.each {|c| c.traverse_some_element(name_set, &block) } if children
+ end
+ end
+
+ module Elem::Trav
+ def traverse_some_element(name_set, &block)
+ yield self if name_set.include? self.name
+ children.each {|c| c.traverse_some_element(name_set, &block) } if children
+ end
+ end
+
+ module Leaf::Trav
+ def traverse_some_element(name_set)
+ end
+ end
+ # :startdoc:
+
+ module Traverse
+ # +traverse_text+ traverses texts in the tree
+ def traverse_text(&block) # :yields: text
+ traverse_text_internal(&block)
+ nil
+ end
+ end
+
+ # :stopdoc:
+ module Container::Trav
+ def traverse_text_internal(&block)
+ each_child {|c| c.traverse_text_internal(&block) }
+ end
+ end
+
+ module Leaf::Trav
+ def traverse_text_internal
+ end
+ end
+
+ module Text::Trav
+ def traverse_text_internal
+ yield self
+ end
+ end
+ # :startdoc:
+
+ module Container::Trav
+ # +filter+ rebuilds the tree without some components.
+ #
+ # node.filter {|descendant_node| predicate } -> node
+ # loc.filter {|descendant_loc| predicate } -> node
+ #
+ # +filter+ yields each node except top node.
+ # If given block returns false, corresponding node is dropped.
+ # If given block returns true, corresponding node is retained and
+ # inner nodes are examined.
+ #
+ # +filter+ returns an node.
+ # It doesn't return location object even if self is location object.
+ #
+ def filter(&block)
+ subst = {}
+ each_child_with_index {|descendant, i|
+ if yield descendant
+ if descendant.elem?
+ subst[i] = descendant.filter(&block)
+ else
+ subst[i] = descendant
+ end
+ else
+ subst[i] = nil
+ end
+ }
+ to_node.subst_subnode(subst)
+ end
+ end
+
+ module Doc::Trav
+ # +title+ searches title and return it as a text.
+ # It returns nil if not found.
+ #
+ # +title+ searchs following information.
+ #
+ # - <title>...</title> in HTML
+ # - <title>...</title> in RSS
+ def title
+ e = find_element('title',
+ '{http://www.w3.org/1999/xhtml}title',
+ '{http://purl.org/rss/1.0/}title',
+ '{http://my.netscape.com/rdf/simple/0.9/}title')
+ e && e.extract_text
+ end
+
+ # +author+ searches author and return it as a text.
+ # It returns nil if not found.
+ #
+ # +author+ searchs following information.
+ #
+ # - <meta name="author" content="author-name"> in HTML
+ # - <link rev="made" title="author-name"> in HTML
+ # - <dc:creator>author-name</dc:creator> in RSS
+ # - <dc:publisher>author-name</dc:publisher> in RSS
+ def author
+ traverse_element('meta',
+ '{http://www.w3.org/1999/xhtml}meta') {|e|
+ begin
+ next unless e.fetch_attr('name').downcase == 'author'
+ author = e.fetch_attribute('content').strip
+ return author if !author.empty?
+ rescue IndexError
+ end
+ }
+
+ traverse_element('link',
+ '{http://www.w3.org/1999/xhtml}link') {|e|
+ begin
+ next unless e.fetch_attr('rev').downcase == 'made'
+ author = e.fetch_attribute('title').strip
+ return author if !author.empty?
+ rescue IndexError
+ end
+ }
+
+ if channel = find_element('{http://purl.org/rss/1.0/}channel')
+ channel.traverse_element('{http://purl.org/dc/elements/1.1/}creator') {|e|
+ begin
+ author = e.extract_text.strip
+ return author if !author.empty?
+ rescue IndexError
+ end
+ }
+ channel.traverse_element('{http://purl.org/dc/elements/1.1/}publisher') {|e|
+ begin
+ author = e.extract_text.strip
+ return author if !author.empty?
+ rescue IndexError
+ end
+ }
+ end
+
+ nil
+ end
+
+ end
+
+ module Doc::Trav
+ def root
+ es = []
+ children.each {|c| es << c if c.elem? } if children
+ raise Hpricot::Error, "no element" if es.empty?
+ raise Hpricot::Error, "multiple top elements" if 1 < es.length
+ es[0]
+ end
+ end
+
+ module Elem::Trav
+ def has_attribute?(name)
+ self.raw_attributes && self.raw_attributes.has_key?(name.to_s)
+ end
+ def get_attribute(name)
+ a = self.raw_attributes && self.raw_attributes[name.to_s]
+ a = Hpricot.uxs(a) if a
+ a
+ end
+ alias_method :[], :get_attribute
+ def set_attribute(name, val)
+ altered!
+ self.raw_attributes ||= {}
+ self.raw_attributes[name.to_s] = val.fast_xs
+ end
+ alias_method :[]=, :set_attribute
+ def remove_attribute(name)
+ name = name.to_s
+ if has_attribute? name
+ altered!
+ self.raw_attributes.delete(name)
+ end
+ end
+ end
+
+end
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/xchar.rb b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/xchar.rb
new file mode 100644
index 0000000..6a5aa4b
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot/xchar.rb
@@ -0,0 +1,94 @@
+#!/usr/bin/env ruby
+
+# The XChar library is provided courtesy of Sam Ruby (See
+# http://intertwingly.net/stories/2005/09/28/xchar.rb)
+
+# --------------------------------------------------------------------
+
+######################################################################
+module Hpricot
+
+ ####################################################################
+ # XML Character converter, from Sam Ruby:
+ # (see http://intertwingly.net/stories/2005/09/28/xchar.rb).
+ #
+ module XChar # :nodoc:
+
+ # See
+ # http://intertwingly.net/stories/2004/04/14/i18n.html#CleaningWindows
+ # for details.
+ CP1252 = { # :nodoc:
+ 128 => 8364, # euro sign
+ 130 => 8218, # single low-9 quotation mark
+ 131 => 402, # latin small letter f with hook
+ 132 => 8222, # double low-9 quotation mark
+ 133 => 8230, # horizontal ellipsis
+ 134 => 8224, # dagger
+ 135 => 8225, # double dagger
+ 136 => 710, # modifier letter circumflex accent
+ 137 => 8240, # per mille sign
+ 138 => 352, # latin capital letter s with caron
+ 139 => 8249, # single left-pointing angle quotation mark
+ 140 => 338, # latin capital ligature oe
+ 142 => 381, # latin capital letter z with caron
+ 145 => 8216, # left single quotation mark
+ 146 => 8217, # right single quotation mark
+ 147 => 8220, # left double quotation mark
+ 148 => 8221, # right double quotation mark
+ 149 => 8226, # bullet
+ 150 => 8211, # en dash
+ 151 => 8212, # em dash
+ 152 => 732, # small tilde
+ 153 => 8482, # trade mark sign
+ 154 => 353, # latin small letter s with caron
+ 155 => 8250, # single right-pointing angle quotation mark
+ 156 => 339, # latin small ligature oe
+ 158 => 382, # latin small letter z with caron
+ 159 => 376, # latin capital letter y with diaeresis
+ }
+
+ # See http://www.w3.org/TR/REC-xml/#dt-chardata for details.
+ PREDEFINED = {
+ 34 => '&quot;', # quotation mark
+ 38 => '&amp;', # ampersand
+ 60 => '&lt;', # left angle bracket
+ 62 => '&gt;' # right angle bracket
+ }
+ PREDEFINED_U = PREDEFINED.inject({}) { |hsh, (k, v)| hsh[v] = k; hsh }
+
+ # See http://www.w3.org/TR/REC-xml/#charsets for details.
+ VALID = [
+ 0x9, 0xA, 0xD,
+ (0x20..0xD7FF),
+ (0xE000..0xFFFD),
+ (0x10000..0x10FFFF)
+ ]
+ end
+
+ class << self
+ # XML escaped version of chr
+ def xchr(str)
+ n = XChar::CP1252[str] || str
+ case n when *XChar::VALID
+ XChar::PREDEFINED[n] or (n<128 ? n.chr : "&##{n};")
+ else
+ '*'
+ end
+ end
+
+ # XML escaped version of to_s
+ def xs(str)
+ str.to_s.unpack('U*').map {|n| xchr(n)}.join # ASCII, UTF-8
+ rescue
+ str.to_s.unpack('C*').map {|n| xchr(n)}.join # ISO-8859-1, WIN-1252
+ end
+
+ # XML unescape
+ def uxs(str)
+ str.to_s.
+ gsub(/\&\w+;/) { |x| (XChar::PREDEFINED_U[x] || ??).chr }.
+ gsub(/\&\#(\d+);/) { [$1.to_i].pack("U*") }
+ end
+ end
+end
+
diff --git a/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot_scan.so b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot_scan.so
new file mode 100644
index 0000000..821f7c1
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/hpricot-0.8.1/lib/hpricot_scan.so
Binary files differ
diff --git a/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json.rb b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json.rb
new file mode 100644
index 0000000..c72ad42
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json.rb
@@ -0,0 +1,8 @@
+# stub to enforce use of the C extension.
+require 'json/common'
+
+module JSON
+ require 'json/version'
+ require 'json/ext'
+ JSON_LOADED = true
+end
diff --git a/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/core.rb b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/core.rb
new file mode 100644
index 0000000..5a56ed7
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/core.rb
@@ -0,0 +1,135 @@
+# This file contains implementations of ruby core's custom objects for
+# serialisation/deserialisation.
+
+unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
+ ::JSON::JSON_LOADED
+ require 'json'
+end
+require 'date'
+
+class Time
+ def self.json_create(object)
+ if usec = object.delete('u') # used to be tv_usec -> tv_nsec
+ object['n'] = usec * 1000
+ end
+ if respond_to?(:tv_nsec)
+ at(*object.values_at('s', 'n'))
+ else
+ at(object['s'], object['n'] / 1000)
+ end
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 's' => tv_sec,
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
+ }.to_json(*args)
+ end
+end
+
+class Date
+ def self.json_create(object)
+ civil(*object.values_at('y', 'm', 'd', 'sg'))
+ end
+
+ alias start sg unless method_defined?(:start)
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'y' => year,
+ 'm' => month,
+ 'd' => day,
+ 'sg' => start,
+ }.to_json(*args)
+ end
+end
+
+class DateTime
+ def self.json_create(object)
+ args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
+ of_a, of_b = object['of'].split('/')
+ if of_b and of_b != '0'
+ args << Rational(of_a.to_i, of_b.to_i)
+ else
+ args << of_a
+ end
+ args << object['sg']
+ civil(*args)
+ end
+
+ alias start sg unless method_defined?(:start)
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'y' => year,
+ 'm' => month,
+ 'd' => day,
+ 'H' => hour,
+ 'M' => min,
+ 'S' => sec,
+ 'of' => offset.to_s,
+ 'sg' => start,
+ }.to_json(*args)
+ end
+end
+
+class Range
+ def self.json_create(object)
+ new(*object['a'])
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'a' => [ first, last, exclude_end? ]
+ }.to_json(*args)
+ end
+end
+
+class Struct
+ def self.json_create(object)
+ new(*object['v'])
+ end
+
+ def to_json(*args)
+ klass = self.class.name
+ klass.empty? and raise JSON::JSONError, "Only named structs are supported!"
+ {
+ 'json_class' => klass,
+ 'v' => values,
+ }.to_json(*args)
+ end
+end
+
+class Exception
+ def self.json_create(object)
+ result = new(object['m'])
+ result.set_backtrace object['b']
+ result
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'm' => message,
+ 'b' => backtrace,
+ }.to_json(*args)
+ end
+end
+
+class Regexp
+ def self.json_create(object)
+ new(object['s'], object['o'])
+ end
+
+ def to_json(*)
+ {
+ 'json_class' => self.class.name,
+ 'o' => options,
+ 's' => source,
+ }.to_json
+ end
+end
diff --git a/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb
new file mode 100644
index 0000000..e86ed1a
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb
@@ -0,0 +1,58 @@
+# This file contains implementations of rails custom objects for
+# serialisation/deserialisation.
+
+unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
+ ::JSON::JSON_LOADED
+ require 'json'
+end
+
+class Object
+ def self.json_create(object)
+ obj = new
+ for key, value in object
+ next if key == 'json_class'
+ instance_variable_set "@#{key}", value
+ end
+ obj
+ end
+
+ def to_json(*a)
+ result = {
+ 'json_class' => self.class.name
+ }
+ instance_variables.inject(result) do |r, name|
+ r[name[1..-1]] = instance_variable_get name
+ r
+ end
+ result.to_json(*a)
+ end
+end
+
+class Symbol
+ def to_json(*a)
+ to_s.to_json(*a)
+ end
+end
+
+module Enumerable
+ def to_json(*a)
+ to_a.to_json(*a)
+ end
+end
+
+# class Regexp
+# def to_json(*)
+# inspect
+# end
+# end
+#
+# The above rails definition has some problems:
+#
+# 1. { 'foo' => /bar/ }.to_json # => "{foo: /bar/}"
+# This isn't valid JSON, because the regular expression syntax is not
+# defined in RFC 4627. (And unquoted strings are disallowed there, too.)
+# Though it is valid Javascript.
+#
+# 2. { 'foo' => /bar/mix }.to_json # => "{foo: /bar/mix}"
+# This isn't even valid Javascript.
+
diff --git a/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/common.rb b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/common.rb
new file mode 100644
index 0000000..499fcc0
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/common.rb
@@ -0,0 +1,354 @@
+require 'json/version'
+
+module JSON
+ class << self
+ # If _object_ is string-like parse the string and return the parsed result
+ # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
+ # data structure object and return it.
+ #
+ # The _opts_ argument is passed through to generate/parse respectively, see
+ # generate and parse for their documentation.
+ def [](object, opts = {})
+ if object.respond_to? :to_str
+ JSON.parse(object.to_str, opts => {})
+ else
+ JSON.generate(object, opts => {})
+ end
+ end
+
+ # Returns the JSON parser class, that is used by JSON. This might be either
+ # JSON::Ext::Parser or JSON::Pure::Parser.
+ attr_reader :parser
+
+ # Set the JSON parser class _parser_ to be used by JSON.
+ def parser=(parser) # :nodoc:
+ @parser = parser
+ remove_const :Parser if const_defined? :Parser
+ const_set :Parser, parser
+ end
+
+ # Return the constant located at _path_. The format of _path_ has to be
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
+ # level (absolute namespace path?). If there doesn't exist a constant at
+ # the given path, an ArgumentError is raised.
+ def deep_const_get(path) # :nodoc:
+ path = path.to_s
+ path.split(/::/).inject(Object) do |p, c|
+ case
+ when c.empty? then p
+ when p.const_defined?(c) then p.const_get(c)
+ else raise ArgumentError, "can't find const #{path}"
+ end
+ end
+ end
+
+ # Set the module _generator_ to be used by JSON.
+ def generator=(generator) # :nodoc:
+ @generator = generator
+ generator_methods = generator::GeneratorMethods
+ for const in generator_methods.constants
+ klass = deep_const_get(const)
+ modul = generator_methods.const_get(const)
+ klass.class_eval do
+ instance_methods(false).each do |m|
+ m.to_s == 'to_json' and remove_method m
+ end
+ include modul
+ end
+ end
+ self.state = generator::State
+ const_set :State, self.state
+ end
+
+ # Returns the JSON generator modul, that is used by JSON. This might be
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
+ attr_reader :generator
+
+ # Returns the JSON generator state class, that is used by JSON. This might
+ # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
+ attr_accessor :state
+
+ # This is create identifier, that is used to decide, if the _json_create_
+ # hook of a class should be called. It defaults to 'json_class'.
+ attr_accessor :create_id
+ end
+ self.create_id = 'json_class'
+
+ NaN = (-1.0) ** 0.5
+
+ Infinity = 1.0/0
+
+ MinusInfinity = -Infinity
+
+ # The base exception for JSON errors.
+ class JSONError < StandardError; end
+
+ # This exception is raised, if a parser error occurs.
+ class ParserError < JSONError; end
+
+ # This exception is raised, if the nesting of parsed datastructures is too
+ # deep.
+ class NestingError < ParserError; end
+
+ # This exception is raised, if a generator or unparser error occurs.
+ class GeneratorError < JSONError; end
+ # For backwards compatibility
+ UnparserError = GeneratorError
+
+ # If a circular data structure is encountered while unparsing
+ # this exception is raised.
+ class CircularDatastructure < GeneratorError; end
+
+ # This exception is raised, if the required unicode support is missing on the
+ # system. Usually this means, that the iconv library is not installed.
+ class MissingUnicodeSupport < JSONError; end
+
+ module_function
+
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
+ #
+ # _opts_ can have the following
+ # keys:
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
+ # structures. Disable depth checking with :max_nesting => false, it defaults
+ # to 19.
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
+ # to false.
+ # * *create_additions*: If set to false, the Parser doesn't create
+ # additions even if a matchin class and create_id was found. This option
+ # defaults to true.
+ def parse(source, opts = {})
+ JSON.parser.new(source, opts).parse
+ end
+
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
+ # The bang version of the parse method, defaults to the more dangerous values
+ # for the _opts_ hash, so be sure only to parse trusted _source_ strings.
+ #
+ # _opts_ can have the following keys:
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
+ # methods defaults to not doing max depth checking: This can be dangerous,
+ # if someone wants to fill up your stack.
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
+ # to true.
+ # * *create_additions*: If set to false, the Parser doesn't create
+ # additions even if a matchin class and create_id was found. This option
+ # defaults to true.
+ def parse!(source, opts = {})
+ opts = {
+ :max_nesting => false,
+ :allow_nan => true
+ }.update(opts)
+ JSON.parser.new(source, opts).parse
+ end
+
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
+ # return it. _state_ is
+ # * a JSON::State object,
+ # * or a Hash like object (responding to to_hash),
+ # * an object convertible into a hash by a to_h method,
+ # that is used as or to configure a State object.
+ #
+ # It defaults to a state object, that creates the shortest possible JSON text
+ # in one line, checks for circular data structures and doesn't allow NaN,
+ # Infinity, and -Infinity.
+ #
+ # A _state_ hash can have the following keys:
+ # * *indent*: a string used to indent levels (default: ''),
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
+ # * *check_circular*: true if checking for circular data structures
+ # should be done (the default), false otherwise.
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
+ # generated, otherwise an exception is thrown, if these values are
+ # encountered. This options defaults to false.
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
+ # structures from which JSON is to be generated. Disable depth checking
+ # with :max_nesting => false, it defaults to 19.
+ #
+ # See also the fast_generate for the fastest creation method with the least
+ # amount of sanity checks, and the pretty_generate method for some
+ # defaults for a pretty output.
+ def generate(obj, state = nil)
+ if state
+ state = State.from_state(state)
+ else
+ state = State.new
+ end
+ obj.to_json(state)
+ end
+
+ # :stopdoc:
+ # I want to deprecate these later, so I'll first be silent about them, and
+ # later delete them.
+ alias unparse generate
+ module_function :unparse
+ # :startdoc:
+
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
+ # return it. This method disables the checks for circles in Ruby objects, and
+ # also generates NaN, Infinity, and, -Infinity float values.
+ #
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
+ def fast_generate(obj)
+ obj.to_json(nil)
+ end
+
+ # :stopdoc:
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
+ alias fast_unparse fast_generate
+ module_function :fast_unparse
+ # :startdoc:
+
+ # Unparse the Ruby data structure _obj_ into a JSON string and return it. The
+ # returned string is a prettier form of the string returned by #unparse.
+ #
+ # The _opts_ argument can be used to configure the generator, see the
+ # generate method for a more detailed explanation.
+ def pretty_generate(obj, opts = nil)
+ state = JSON.state.new(
+ :indent => ' ',
+ :space => ' ',
+ :object_nl => "\n",
+ :array_nl => "\n",
+ :check_circular => true
+ )
+ if opts
+ if opts.respond_to? :to_hash
+ opts = opts.to_hash
+ elsif opts.respond_to? :to_h
+ opts = opts.to_h
+ else
+ raise TypeError, "can't convert #{opts.class} into Hash"
+ end
+ state.configure(opts)
+ end
+ obj.to_json(state)
+ end
+
+ # :stopdoc:
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
+ alias pretty_unparse pretty_generate
+ module_function :pretty_unparse
+ # :startdoc:
+
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
+ # either be a string-like object, an IO like object, or an object responding
+ # to the read method. If _proc_ was given, it will be called with any nested
+ # Ruby object as an argument recursively in depth first order.
+ #
+ # This method is part of the implementation of the load/dump interface of
+ # Marshal and YAML.
+ def load(source, proc = nil)
+ if source.respond_to? :to_str
+ source = source.to_str
+ elsif source.respond_to? :to_io
+ source = source.to_io.read
+ else
+ source = source.read
+ end
+ result = parse(source, :max_nesting => false, :allow_nan => true)
+ recurse_proc(result, &proc) if proc
+ result
+ end
+
+ def recurse_proc(result, &proc)
+ case result
+ when Array
+ result.each { |x| recurse_proc x, &proc }
+ proc.call result
+ when Hash
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
+ proc.call result
+ else
+ proc.call result
+ end
+ end
+ private :recurse_proc
+ module_function :recurse_proc
+
+ alias restore load
+ module_function :restore
+
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
+ # the result.
+ #
+ # If anIO (an IO like object or an object that responds to the write method)
+ # was given, the resulting JSON is written to it.
+ #
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
+ # exception is raised. This argument is similar (but not exactly the
+ # same!) to the _limit_ argument in Marshal.dump.
+ #
+ # This method is part of the implementation of the load/dump interface of
+ # Marshal and YAML.
+ def dump(obj, anIO = nil, limit = nil)
+ if anIO and limit.nil?
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
+ unless anIO.respond_to?(:write)
+ limit = anIO
+ anIO = nil
+ end
+ end
+ limit ||= 0
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
+ if anIO
+ anIO.write result
+ anIO
+ else
+ result
+ end
+ rescue JSON::NestingError
+ raise ArgumentError, "exceed depth limit"
+ end
+end
+
+module ::Kernel
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
+ # one line.
+ def j(*objs)
+ objs.each do |obj|
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
+ end
+ nil
+ end
+
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
+ # indentation and over many lines.
+ def jj(*objs)
+ objs.each do |obj|
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
+ end
+ nil
+ end
+
+ # If _object_ is string-like parse the string and return the parsed result as
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
+ # structure object and return it.
+ #
+ # The _opts_ argument is passed through to generate/parse respectively, see
+ # generate and parse for their documentation.
+ def JSON(object, opts = {})
+ if object.respond_to? :to_str
+ JSON.parse(object.to_str, opts)
+ else
+ JSON.generate(object, opts)
+ end
+ end
+end
+
+class ::Class
+ # Returns true, if this class can be used to create an instance
+ # from a serialised JSON string. The class has to implement a class
+ # method _json_create_ that expects a hash as first parameter, which includes
+ # the required data.
+ def json_creatable?
+ respond_to?(:json_create)
+ end
+end
diff --git a/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext.rb b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext.rb
new file mode 100644
index 0000000..ff4fa42
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext.rb
@@ -0,0 +1,13 @@
+require 'json/common'
+
+module JSON
+ # This module holds all the modules/classes that implement JSON's
+ # functionality as C extensions.
+ module Ext
+ require 'json/ext/parser'
+ require 'json/ext/generator'
+ $DEBUG and warn "Using c extension for JSON."
+ JSON.parser = Parser
+ JSON.generator = Generator
+ end
+end
diff --git a/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext/generator.so b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext/generator.so
new file mode 100644
index 0000000..27fb981
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext/generator.so
Binary files differ
diff --git a/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext/parser.so b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext/parser.so
new file mode 100644
index 0000000..ae052a0
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/ext/parser.so
Binary files differ
diff --git a/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/version.rb b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/version.rb
new file mode 100644
index 0000000..acf8217
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/version.rb
@@ -0,0 +1,9 @@
+module JSON
+ # JSON version
+ VERSION = '1.1.3'
+ VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
+ VARIANT_BINARY = false
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3.rb
new file mode 100644
index 0000000..43d62f0
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3.rb
@@ -0,0 +1,11 @@
+# support multiple ruby version (fat binaries under windows)
+begin
+ RUBY_VERSION =~ /(\d+.\d+)/
+ require "sqlite3/#{$1}/sqlite3_native"
+rescue LoadError
+ #require 'sqlite3/sqlite3_native'
+ require 'sqlite3_native'
+end
+
+require 'sqlite3/database'
+require 'sqlite3/version'
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/constants.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/constants.rb
new file mode 100644
index 0000000..b316c2d
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/constants.rb
@@ -0,0 +1,49 @@
+module SQLite3 ; module Constants
+
+ module TextRep
+ UTF8 = 1
+ UTF16LE = 2
+ UTF16BE = 3
+ UTF16 = 4
+ ANY = 5
+ end
+
+ module ColumnType
+ INTEGER = 1
+ FLOAT = 2
+ TEXT = 3
+ BLOB = 4
+ NULL = 5
+ end
+
+ module ErrorCode
+ OK = 0 # Successful result
+ ERROR = 1 # SQL error or missing database
+ INTERNAL = 2 # An internal logic error in SQLite
+ PERM = 3 # Access permission denied
+ ABORT = 4 # Callback routine requested an abort
+ BUSY = 5 # The database file is locked
+ LOCKED = 6 # A table in the database is locked
+ NOMEM = 7 # A malloc() failed
+ READONLY = 8 # Attempt to write a readonly database
+ INTERRUPT = 9 # Operation terminated by sqlite_interrupt()
+ IOERR = 10 # Some kind of disk I/O error occurred
+ CORRUPT = 11 # The database disk image is malformed
+ NOTFOUND = 12 # (Internal Only) Table or record not found
+ FULL = 13 # Insertion failed because database is full
+ CANTOPEN = 14 # Unable to open the database file
+ PROTOCOL = 15 # Database lock protocol error
+ EMPTY = 16 # (Internal Only) Database table is empty
+ SCHEMA = 17 # The database schema changed
+ TOOBIG = 18 # Too much data for one row of a table
+ CONSTRAINT = 19 # Abort due to contraint violation
+ MISMATCH = 20 # Data type mismatch
+ MISUSE = 21 # Library used incorrectly
+ NOLFS = 22 # Uses OS features not supported on host
+ AUTH = 23 # Authorization denied
+
+ ROW = 100 # sqlite_step() has another row ready
+ DONE = 101 # sqlite_step() has finished executing
+ end
+
+end ; end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/database.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/database.rb
new file mode 100644
index 0000000..4d3522f
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/database.rb
@@ -0,0 +1,568 @@
+require 'sqlite3/constants'
+require 'sqlite3/errors'
+require 'sqlite3/pragmas'
+require 'sqlite3/statement'
+require 'sqlite3/translator'
+require 'sqlite3/value'
+
+module SQLite3
+
+ # The Database class encapsulates a single connection to a SQLite3 database.
+ # Its usage is very straightforward:
+ #
+ # require 'sqlite3'
+ #
+ # SQLite3::Database.new( "data.db" ) do |db|
+ # db.execute( "select * from table" ) do |row|
+ # p row
+ # end
+ # end
+ #
+ # It wraps the lower-level methods provides by the selected driver, and
+ # includes the Pragmas module for access to various pragma convenience
+ # methods.
+ #
+ # The Database class provides type translation services as well, by which
+ # the SQLite3 data types (which are all represented as strings) may be
+ # converted into their corresponding types (as defined in the schemas
+ # for their tables). This translation only occurs when querying data from
+ # the database--insertions and updates are all still typeless.
+ #
+ # Furthermore, the Database class has been designed to work well with the
+ # ArrayFields module from Ara Howard. If you require the ArrayFields
+ # module before performing a query, and if you have not enabled results as
+ # hashes, then the results will all be indexible by field name.
+ class Database
+ include Pragmas
+
+ class << self
+
+ alias :open :new
+
+ # Quotes the given string, making it safe to use in an SQL statement.
+ # It replaces all instances of the single-quote character with two
+ # single-quote characters. The modified string is returned.
+ def quote( string )
+ string.gsub( /'/, "''" )
+ end
+
+ end
+
+ # A boolean that indicates whether rows in result sets should be returned
+ # as hashes or not. By default, rows are returned as arrays.
+ attr_accessor :results_as_hash
+
+ # A boolean indicating whether or not type translation is enabled for this
+ # database.
+ attr_accessor :type_translation
+
+ # Return the type translator employed by this database instance. Each
+ # database instance has its own type translator; this allows for different
+ # type handlers to be installed in each instance without affecting other
+ # instances. Furthermore, the translators are instantiated lazily, so that
+ # if a database does not use type translation, it will not be burdened by
+ # the overhead of a useless type translator. (See the Translator class.)
+ def translator
+ @translator ||= Translator.new
+ end
+
+ # Installs (or removes) a block that will be invoked for every access
+ # to the database. If the block returns 0 (or +nil+), the statement
+ # is allowed to proceed. Returning 1 causes an authorization error to
+ # occur, and returning 2 causes the access to be silently denied.
+ def authorizer( &block )
+ self.authorizer = block
+ end
+
+ # Returns a Statement object representing the given SQL. This does not
+ # execute the statement; it merely prepares the statement for execution.
+ #
+ # The Statement can then be executed using Statement#execute.
+ #
+ def prepare sql
+ stmt = SQLite3::Statement.new( self, sql )
+ return stmt unless block_given?
+
+ begin
+ yield stmt
+ ensure
+ stmt.close
+ end
+ end
+
+ # Executes the given SQL statement. If additional parameters are given,
+ # they are treated as bind variables, and are bound to the placeholders in
+ # the query.
+ #
+ # Note that if any of the values passed to this are hashes, then the
+ # key/value pairs are each bound separately, with the key being used as
+ # the name of the placeholder to bind the value to.
+ #
+ # The block is optional. If given, it will be invoked for each row returned
+ # by the query. Otherwise, any results are accumulated into an array and
+ # returned wholesale.
+ #
+ # See also #execute2, #query, and #execute_batch for additional ways of
+ # executing statements.
+ def execute sql, bind_vars = [], *args, &block
+ # FIXME: This is a terrible hack and should be removed but is required
+ # for older versions of rails
+ hack = Object.const_defined?(:ActiveRecord) && sql =~ /^PRAGMA index_list/
+
+ if bind_vars.nil? || !args.empty?
+ if args.empty?
+ bind_vars = []
+ else
+ bind_vars = [nil] + args
+ end
+
+ warn(<<-eowarn) if $VERBOSE
+#{caller[0]} is calling SQLite3::Database#execute with nil or multiple bind params
+without using an array. Please switch to passing bind parameters as an array.
+ eowarn
+ end
+
+ prepare( sql ) do |stmt|
+ stmt.bind_params(bind_vars)
+ if type_translation
+ stmt = ResultSet.new(self, stmt).to_a
+ end
+
+ if block_given?
+ stmt.each do |row|
+ if @results_as_hash
+ h = Hash[*stmt.columns.zip(row).flatten]
+ row.each_with_index { |r, i| h[i] = r }
+
+ yield h
+ else
+ yield row
+ end
+ end
+ else
+ if @results_as_hash
+ stmt.map { |row|
+ h = Hash[*stmt.columns.zip(row).flatten]
+ row.each_with_index { |r, i| h[i] = r }
+
+ # FIXME UGH TERRIBLE HACK!
+ h['unique'] = h['unique'].to_s if hack
+
+ h
+ }
+ else
+ stmt.to_a
+ end
+ end
+ end
+ end
+
+ # Executes the given SQL statement, exactly as with #execute. However, the
+ # first row returned (either via the block, or in the returned array) is
+ # always the names of the columns. Subsequent rows correspond to the data
+ # from the result set.
+ #
+ # Thus, even if the query itself returns no rows, this method will always
+ # return at least one row--the names of the columns.
+ #
+ # See also #execute, #query, and #execute_batch for additional ways of
+ # executing statements.
+ def execute2( sql, *bind_vars )
+ prepare( sql ) do |stmt|
+ result = stmt.execute( *bind_vars )
+ if block_given?
+ yield stmt.columns
+ result.each { |row| yield row }
+ else
+ return result.inject( [ stmt.columns ] ) { |arr,row|
+ arr << row; arr }
+ end
+ end
+ end
+
+ # Executes all SQL statements in the given string. By contrast, the other
+ # means of executing queries will only execute the first statement in the
+ # string, ignoring all subsequent statements. This will execute each one
+ # in turn. The same bind parameters, if given, will be applied to each
+ # statement.
+ #
+ # This always returns +nil+, making it unsuitable for queries that return
+ # rows.
+ def execute_batch( sql, bind_vars = [], *args )
+ # FIXME: remove this stuff later
+ unless [Array, Hash].include?(bind_vars.class)
+ bind_vars = [bind_vars]
+ warn(<<-eowarn) if $VERBOSE
+#{caller[0]} is calling SQLite3::Database#execute_batch with bind parameters
+that are not a list of a hash. Please switch to passing bind parameters as an
+array or hash.
+ eowarn
+ end
+
+ # FIXME: remove this stuff later
+ if bind_vars.nil? || !args.empty?
+ if args.empty?
+ bind_vars = []
+ else
+ bind_vars = [nil] + args
+ end
+
+ warn(<<-eowarn) if $VERBOSE
+#{caller[0]} is calling SQLite3::Database#execute_batch with nil or multiple bind params
+without using an array. Please switch to passing bind parameters as an array.
+ eowarn
+ end
+
+ sql = sql.strip
+ until sql.empty? do
+ prepare( sql ) do |stmt|
+ # FIXME: this should probably use sqlite3's api for batch execution
+ # This implementation requires stepping over the results.
+ if bind_vars.length == stmt.bind_parameter_count
+ stmt.bind_params(bind_vars)
+ end
+ stmt.step
+ sql = stmt.remainder.strip
+ end
+ end
+ nil
+ end
+
+ # This is a convenience method for creating a statement, binding
+ # paramters to it, and calling execute:
+ #
+ # result = db.query( "select * from foo where a=?", 5 )
+ # # is the same as
+ # result = db.prepare( "select * from foo where a=?" ).execute( 5 )
+ #
+ # You must be sure to call +close+ on the ResultSet instance that is
+ # returned, or you could have problems with locks on the table. If called
+ # with a block, +close+ will be invoked implicitly when the block
+ # terminates.
+ def query( sql, bind_vars = [], *args )
+
+ if bind_vars.nil? || !args.empty?
+ if args.empty?
+ bind_vars = []
+ else
+ bind_vars = [nil] + args
+ end
+
+ warn(<<-eowarn) if $VERBOSE
+#{caller[0]} is calling SQLite3::Database#query with nil or multiple bind params
+without using an array. Please switch to passing bind parameters as an array.
+ eowarn
+ end
+
+ result = prepare( sql ).execute( bind_vars )
+ if block_given?
+ begin
+ yield result
+ ensure
+ result.close
+ end
+ else
+ return result
+ end
+ end
+
+ # A convenience method for obtaining the first row of a result set, and
+ # discarding all others. It is otherwise identical to #execute.
+ #
+ # See also #get_first_value.
+ def get_first_row( sql, *bind_vars )
+ execute( sql, *bind_vars ) { |row| return row }
+ nil
+ end
+
+ # A convenience method for obtaining the first value of the first row of a
+ # result set, and discarding all other values and rows. It is otherwise
+ # identical to #execute.
+ #
+ # See also #get_first_row.
+ def get_first_value( sql, *bind_vars )
+ execute( sql, *bind_vars ) { |row| return row[0] }
+ nil
+ end
+
+ alias :busy_timeout :busy_timeout=
+
+ # Creates a new function for use in SQL statements. It will be added as
+ # +name+, with the given +arity+. (For variable arity functions, use
+ # -1 for the arity.)
+ #
+ # The block should accept at least one parameter--the FunctionProxy
+ # instance that wraps this function invocation--and any other
+ # arguments it needs (up to its arity).
+ #
+ # The block does not return a value directly. Instead, it will invoke
+ # the FunctionProxy#set_result method on the +func+ parameter and
+ # indicate the return value that way.
+ #
+ # Example:
+ #
+ # db.create_function( "maim", 1 ) do |func, value|
+ # if value.nil?
+ # func.result = nil
+ # else
+ # func.result = value.split(//).sort.join
+ # end
+ # end
+ #
+ # puts db.get_first_value( "select maim(name) from table" )
+ def create_function name, arity, text_rep=Constants::TextRep::ANY, &block
+ define_function(name) do |*args|
+ fp = FunctionProxy.new
+ block.call(fp, *args)
+ fp.result
+ end
+ self
+ end
+
+ # Creates a new aggregate function for use in SQL statements. Aggregate
+ # functions are functions that apply over every row in the result set,
+ # instead of over just a single row. (A very common aggregate function
+ # is the "count" function, for determining the number of rows that match
+ # a query.)
+ #
+ # The new function will be added as +name+, with the given +arity+. (For
+ # variable arity functions, use -1 for the arity.)
+ #
+ # The +step+ parameter must be a proc object that accepts as its first
+ # parameter a FunctionProxy instance (representing the function
+ # invocation), with any subsequent parameters (up to the function's arity).
+ # The +step+ callback will be invoked once for each row of the result set.
+ #
+ # The +finalize+ parameter must be a +proc+ object that accepts only a
+ # single parameter, the FunctionProxy instance representing the current
+ # function invocation. It should invoke FunctionProxy#set_result to
+ # store the result of the function.
+ #
+ # Example:
+ #
+ # db.create_aggregate( "lengths", 1 ) do
+ # step do |func, value|
+ # func[ :total ] ||= 0
+ # func[ :total ] += ( value ? value.length : 0 )
+ # end
+ #
+ # finalize do |func|
+ # func.set_result( func[ :total ] || 0 )
+ # end
+ # end
+ #
+ # puts db.get_first_value( "select lengths(name) from table" )
+ #
+ # See also #create_aggregate_handler for a more object-oriented approach to
+ # aggregate functions.
+ def create_aggregate( name, arity, step=nil, finalize=nil,
+ text_rep=Constants::TextRep::ANY, &block )
+
+ factory = Class.new do
+ def self.step( &block )
+ define_method(:step, &block)
+ end
+
+ def self.finalize( &block )
+ define_method(:finalize, &block)
+ end
+ end
+
+ if block_given?
+ factory.instance_eval(&block)
+ else
+ factory.class_eval do
+ define_method(:step, step)
+ define_method(:finalize, finalize)
+ end
+ end
+
+ proxy = factory.new
+ proxy.extend(Module.new {
+ attr_accessor :ctx
+
+ def step( *args )
+ super(@ctx, *args)
+ end
+
+ def finalize
+ super(@ctx)
+ end
+ })
+ proxy.ctx = FunctionProxy.new
+ define_aggregator(name, proxy)
+ end
+
+ # This is another approach to creating an aggregate function (see
+ # #create_aggregate). Instead of explicitly specifying the name,
+ # callbacks, arity, and type, you specify a factory object
+ # (the "handler") that knows how to obtain all of that information. The
+ # handler should respond to the following messages:
+ #
+ # +arity+:: corresponds to the +arity+ parameter of #create_aggregate. This
+ # message is optional, and if the handler does not respond to it,
+ # the function will have an arity of -1.
+ # +name+:: this is the name of the function. The handler _must_ implement
+ # this message.
+ # +new+:: this must be implemented by the handler. It should return a new
+ # instance of the object that will handle a specific invocation of
+ # the function.
+ #
+ # The handler instance (the object returned by the +new+ message, described
+ # above), must respond to the following messages:
+ #
+ # +step+:: this is the method that will be called for each step of the
+ # aggregate function's evaluation. It should implement the same
+ # signature as the +step+ callback for #create_aggregate.
+ # +finalize+:: this is the method that will be called to finalize the
+ # aggregate function's evaluation. It should implement the
+ # same signature as the +finalize+ callback for
+ # #create_aggregate.
+ #
+ # Example:
+ #
+ # class LengthsAggregateHandler
+ # def self.arity; 1; end
+ #
+ # def initialize
+ # @total = 0
+ # end
+ #
+ # def step( ctx, name )
+ # @total += ( name ? name.length : 0 )
+ # end
+ #
+ # def finalize( ctx )
+ # ctx.set_result( @total )
+ # end
+ # end
+ #
+ # db.create_aggregate_handler( LengthsAggregateHandler )
+ # puts db.get_first_value( "select lengths(name) from A" )
+ def create_aggregate_handler( handler )
+ proxy = Class.new do
+ def initialize handler
+ @handler = handler
+ @fp = FunctionProxy.new
+ end
+
+ def step( *args )
+ @handler.step(@fp, *args)
+ end
+
+ def finalize
+ @handler.finalize @fp
+ @fp.result
+ end
+ end
+ define_aggregator(handler.name, proxy.new(handler.new))
+ self
+ end
+
+ # Begins a new transaction. Note that nested transactions are not allowed
+ # by SQLite, so attempting to nest a transaction will result in a runtime
+ # exception.
+ #
+ # The +mode+ parameter may be either <tt>:deferred</tt> (the default),
+ # <tt>:immediate</tt>, or <tt>:exclusive</tt>.
+ #
+ # If a block is given, the database instance is yielded to it, and the
+ # transaction is committed when the block terminates. If the block
+ # raises an exception, a rollback will be performed instead. Note that if
+ # a block is given, #commit and #rollback should never be called
+ # explicitly or you'll get an error when the block terminates.
+ #
+ # If a block is not given, it is the caller's responsibility to end the
+ # transaction explicitly, either by calling #commit, or by calling
+ # #rollback.
+ def transaction( mode = :deferred )
+ execute "begin #{mode.to_s} transaction"
+ @transaction_active = true
+
+ if block_given?
+ abort = false
+ begin
+ yield self
+ rescue ::Object
+ abort = true
+ raise
+ ensure
+ abort and rollback or commit
+ end
+ end
+
+ true
+ end
+
+ # Commits the current transaction. If there is no current transaction,
+ # this will cause an error to be raised. This returns +true+, in order
+ # to allow it to be used in idioms like
+ # <tt>abort? and rollback or commit</tt>.
+ def commit
+ execute "commit transaction"
+ @transaction_active = false
+ true
+ end
+
+ # Rolls the current transaction back. If there is no current transaction,
+ # this will cause an error to be raised. This returns +true+, in order
+ # to allow it to be used in idioms like
+ # <tt>abort? and rollback or commit</tt>.
+ def rollback
+ execute "rollback transaction"
+ @transaction_active = false
+ true
+ end
+
+ # Returns +true+ if there is a transaction active, and +false+ otherwise.
+ def transaction_active?
+ @transaction_active
+ end
+
+ # A helper class for dealing with custom functions (see #create_function,
+ # #create_aggregate, and #create_aggregate_handler). It encapsulates the
+ # opaque function object that represents the current invocation. It also
+ # provides more convenient access to the API functions that operate on
+ # the function object.
+ #
+ # This class will almost _always_ be instantiated indirectly, by working
+ # with the create methods mentioned above.
+ class FunctionProxy
+ attr_accessor :result
+
+ # Create a new FunctionProxy that encapsulates the given +func+ object.
+ # If context is non-nil, the functions context will be set to that. If
+ # it is non-nil, it must quack like a Hash. If it is nil, then none of
+ # the context functions will be available.
+ def initialize
+ @result = nil
+ @context = {}
+ end
+
+ # Set the result of the function to the given error message.
+ # The function will then return that error.
+ def set_error( error )
+ @driver.result_error( @func, error.to_s, -1 )
+ end
+
+ # (Only available to aggregate functions.) Returns the number of rows
+ # that the aggregate has processed so far. This will include the current
+ # row, and so will always return at least 1.
+ def count
+ @driver.aggregate_count( @func )
+ end
+
+ # Returns the value with the given key from the context. This is only
+ # available to aggregate functions.
+ def []( key )
+ @context[ key ]
+ end
+
+ # Sets the value with the given key in the context. This is only
+ # available to aggregate functions.
+ def []=( key, value )
+ @context[ key ] = value
+ end
+ end
+ end
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/errors.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/errors.rb
new file mode 100644
index 0000000..3936a59
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/errors.rb
@@ -0,0 +1,44 @@
+require 'sqlite3/constants'
+
+module SQLite3
+ class Exception < ::StandardError
+ @code = 0
+
+ # The numeric error code that this exception represents.
+ def self.code
+ @code
+ end
+
+ # A convenience for accessing the error code for this exception.
+ def code
+ self.class.code
+ end
+ end
+
+ class SQLException < Exception; end
+ class InternalException < Exception; end
+ class PermissionException < Exception; end
+ class AbortException < Exception; end
+ class BusyException < Exception; end
+ class LockedException < Exception; end
+ class MemoryException < Exception; end
+ class ReadOnlyException < Exception; end
+ class InterruptException < Exception; end
+ class IOException < Exception; end
+ class CorruptException < Exception; end
+ class NotFoundException < Exception; end
+ class FullException < Exception; end
+ class CantOpenException < Exception; end
+ class ProtocolException < Exception; end
+ class EmptyException < Exception; end
+ class SchemaChangedException < Exception; end
+ class TooBigException < Exception; end
+ class ConstraintException < Exception; end
+ class MismatchException < Exception; end
+ class MisuseException < Exception; end
+ class UnsupportedException < Exception; end
+ class AuthorizationException < Exception; end
+ class FormatException < Exception; end
+ class RangeException < Exception; end
+ class NotADatabaseException < Exception; end
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/pragmas.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/pragmas.rb
new file mode 100644
index 0000000..2e31938
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/pragmas.rb
@@ -0,0 +1,280 @@
+require 'sqlite3/errors'
+
+module SQLite3
+
+ # This module is intended for inclusion solely by the Database class. It
+ # defines convenience methods for the various pragmas supported by SQLite3.
+ #
+ # For a detailed description of these pragmas, see the SQLite3 documentation
+ # at http://sqlite.org/pragma.html.
+ module Pragmas
+
+ # Returns +true+ or +false+ depending on the value of the named pragma.
+ def get_boolean_pragma( name )
+ get_first_value( "PRAGMA #{name}" ) != "0"
+ end
+ private :get_boolean_pragma
+
+ # Sets the given pragma to the given boolean value. The value itself
+ # may be +true+ or +false+, or any other commonly used string or
+ # integer that represents truth.
+ def set_boolean_pragma( name, mode )
+ case mode
+ when String
+ case mode.downcase
+ when "on", "yes", "true", "y", "t"; mode = "'ON'"
+ when "off", "no", "false", "n", "f"; mode = "'OFF'"
+ else
+ raise Exception,
+ "unrecognized pragma parameter #{mode.inspect}"
+ end
+ when true, 1
+ mode = "ON"
+ when false, 0, nil
+ mode = "OFF"
+ else
+ raise Exception,
+ "unrecognized pragma parameter #{mode.inspect}"
+ end
+
+ execute( "PRAGMA #{name}=#{mode}" )
+ end
+ private :set_boolean_pragma
+
+ # Requests the given pragma (and parameters), and if the block is given,
+ # each row of the result set will be yielded to it. Otherwise, the results
+ # are returned as an array.
+ def get_query_pragma( name, *parms, &block ) # :yields: row
+ if parms.empty?
+ execute( "PRAGMA #{name}", &block )
+ else
+ args = "'" + parms.join("','") + "'"
+ execute( "PRAGMA #{name}( #{args} )", &block )
+ end
+ end
+ private :get_query_pragma
+
+ # Return the value of the given pragma.
+ def get_enum_pragma( name )
+ get_first_value( "PRAGMA #{name}" )
+ end
+ private :get_enum_pragma
+
+ # Set the value of the given pragma to +mode+. The +mode+ parameter must
+ # conform to one of the values in the given +enum+ array. Each entry in
+ # the array is another array comprised of elements in the enumeration that
+ # have duplicate values. See #synchronous, #default_synchronous,
+ # #temp_store, and #default_temp_store for usage examples.
+ def set_enum_pragma( name, mode, enums )
+ match = enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } }
+ raise Exception,
+ "unrecognized #{name} #{mode.inspect}" unless match
+ execute( "PRAGMA #{name}='#{match.first.upcase}'" )
+ end
+ private :set_enum_pragma
+
+ # Returns the value of the given pragma as an integer.
+ def get_int_pragma( name )
+ get_first_value( "PRAGMA #{name}" ).to_i
+ end
+ private :get_int_pragma
+
+ # Set the value of the given pragma to the integer value of the +value+
+ # parameter.
+ def set_int_pragma( name, value )
+ execute( "PRAGMA #{name}=#{value.to_i}" )
+ end
+ private :set_int_pragma
+
+ # The enumeration of valid synchronous modes.
+ SYNCHRONOUS_MODES = [ [ 'full', 2 ], [ 'normal', 1 ], [ 'off', 0 ] ]
+
+ # The enumeration of valid temp store modes.
+ TEMP_STORE_MODES = [ [ 'default', 0 ], [ 'file', 1 ], [ 'memory', 2 ] ]
+
+ # Does an integrity check on the database. If the check fails, a
+ # SQLite3::Exception will be raised. Otherwise it
+ # returns silently.
+ def integrity_check
+ execute( "PRAGMA integrity_check" ) do |row|
+ raise Exception, row[0] if row[0] != "ok"
+ end
+ end
+
+ def auto_vacuum
+ get_boolean_pragma "auto_vacuum"
+ end
+
+ def auto_vacuum=( mode )
+ set_boolean_pragma "auto_vacuum", mode
+ end
+
+ def schema_cookie
+ get_int_pragma "schema_cookie"
+ end
+
+ def schema_cookie=( cookie )
+ set_int_pragma "schema_cookie", cookie
+ end
+
+ def user_cookie
+ get_int_pragma "user_cookie"
+ end
+
+ def user_cookie=( cookie )
+ set_int_pragma "user_cookie", cookie
+ end
+
+ def cache_size
+ get_int_pragma "cache_size"
+ end
+
+ def cache_size=( size )
+ set_int_pragma "cache_size", size
+ end
+
+ def default_cache_size
+ get_int_pragma "default_cache_size"
+ end
+
+ def default_cache_size=( size )
+ set_int_pragma "default_cache_size", size
+ end
+
+ def default_synchronous
+ get_enum_pragma "default_synchronous"
+ end
+
+ def default_synchronous=( mode )
+ set_enum_pragma "default_synchronous", mode, SYNCHRONOUS_MODES
+ end
+
+ def synchronous
+ get_enum_pragma "synchronous"
+ end
+
+ def synchronous=( mode )
+ set_enum_pragma "synchronous", mode, SYNCHRONOUS_MODES
+ end
+
+ def default_temp_store
+ get_enum_pragma "default_temp_store"
+ end
+
+ def default_temp_store=( mode )
+ set_enum_pragma "default_temp_store", mode, TEMP_STORE_MODES
+ end
+
+ def temp_store
+ get_enum_pragma "temp_store"
+ end
+
+ def temp_store=( mode )
+ set_enum_pragma "temp_store", mode, TEMP_STORE_MODES
+ end
+
+ def full_column_names
+ get_boolean_pragma "full_column_names"
+ end
+
+ def full_column_names=( mode )
+ set_boolean_pragma "full_column_names", mode
+ end
+
+ def parser_trace
+ get_boolean_pragma "parser_trace"
+ end
+
+ def parser_trace=( mode )
+ set_boolean_pragma "parser_trace", mode
+ end
+
+ def vdbe_trace
+ get_boolean_pragma "vdbe_trace"
+ end
+
+ def vdbe_trace=( mode )
+ set_boolean_pragma "vdbe_trace", mode
+ end
+
+ def database_list( &block ) # :yields: row
+ get_query_pragma "database_list", &block
+ end
+
+ def foreign_key_list( table, &block ) # :yields: row
+ get_query_pragma "foreign_key_list", table, &block
+ end
+
+ def index_info( index, &block ) # :yields: row
+ get_query_pragma "index_info", index, &block
+ end
+
+ def index_list( table, &block ) # :yields: row
+ get_query_pragma "index_list", table, &block
+ end
+
+ ###
+ # Returns information about +table+. Yields each row of table information
+ # if a block is provided.
+ def table_info table
+ stmt = prepare "PRAGMA table_info(#{table})"
+ columns = stmt.columns
+
+ needs_tweak_default =
+ version_compare(SQLite3.libversion.to_s, "3.3.7") > 0
+
+ result = [] unless block_given?
+ stmt.each do |row|
+ new_row = Hash[*columns.zip(row).flatten]
+
+ # FIXME: This should be removed but is required for older versions
+ # of rails
+ if(Object.const_defined?(:ActiveRecord))
+ new_row['notnull'] = new_row['notnull'].to_s
+ end
+
+ tweak_default(new_row) if needs_tweak_default
+
+ if block_given?
+ yield new_row
+ else
+ result << new_row
+ end
+ end
+ stmt.close
+
+ result
+ end
+
+ private
+
+ # Compares two version strings
+ def version_compare(v1, v2)
+ v1 = v1.split(".").map { |i| i.to_i }
+ v2 = v2.split(".").map { |i| i.to_i }
+ parts = [v1.length, v2.length].max
+ v1.push 0 while v1.length < parts
+ v2.push 0 while v2.length < parts
+ v1.zip(v2).each do |a,b|
+ return -1 if a < b
+ return 1 if a > b
+ end
+ return 0
+ end
+
+ # Since SQLite 3.3.8, the table_info pragma has returned the default
+ # value of the row as a quoted SQL value. This method essentially
+ # unquotes those values.
+ def tweak_default(hash)
+ case hash["dflt_value"]
+ when /^null$/i
+ hash["dflt_value"] = nil
+ when /^'(.*)'$/
+ hash["dflt_value"] = $1.gsub(/''/, "'")
+ when /^"(.*)"$/
+ hash["dflt_value"] = $1.gsub(/""/, '"')
+ end
+ end
+ end
+
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/resultset.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/resultset.rb
new file mode 100644
index 0000000..bc331b0
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/resultset.rb
@@ -0,0 +1,126 @@
+require 'sqlite3/constants'
+require 'sqlite3/errors'
+
+module SQLite3
+
+ # The ResultSet object encapsulates the enumerability of a query's output.
+ # It is a simple cursor over the data that the query returns. It will
+ # very rarely (if ever) be instantiated directly. Instead, client's should
+ # obtain a ResultSet instance via Statement#execute.
+ class ResultSet
+ include Enumerable
+
+ # The class of which we return an object in case we want an Array as
+ # result. (ArrayFields is installed.)
+ class ArrayWithTypes < Array
+ attr_accessor :types
+ end
+
+ # The class of which we return an object in case we want an Array as
+ # result. (ArrayFields is not installed.)
+ class ArrayWithTypesAndFields < Array
+ attr_accessor :types
+ attr_accessor :fields
+ end
+
+ # The class of which we return an object in case we want a Hash as
+ # result.
+ class HashWithTypes < Hash
+ attr_accessor :types
+ end
+
+ # Create a new ResultSet attached to the given database, using the
+ # given sql text.
+ def initialize db, stmt
+ @db = db
+ @stmt = stmt
+ end
+
+ # Reset the cursor, so that a result set which has reached end-of-file
+ # can be rewound and reiterated.
+ def reset( *bind_params )
+ @stmt.reset!
+ @stmt.bind_params( *bind_params )
+ @eof = false
+ end
+
+ # Query whether the cursor has reached the end of the result set or not.
+ def eof?
+ @stmt.done?
+ end
+
+ # Obtain the next row from the cursor. If there are no more rows to be
+ # had, this will return +nil+. If type translation is active on the
+ # corresponding database, the values in the row will be translated
+ # according to their types.
+ #
+ # The returned value will be an array, unless Database#results_as_hash has
+ # been set to +true+, in which case the returned value will be a hash.
+ #
+ # For arrays, the column names are accessible via the +fields+ property,
+ # and the column types are accessible via the +types+ property.
+ #
+ # For hashes, the column names are the keys of the hash, and the column
+ # types are accessible via the +types+ property.
+ def next
+ row = @stmt.step
+ return nil if @stmt.done?
+
+ if @db.type_translation
+ row = @stmt.types.zip(row).map do |type, value|
+ @db.translator.translate( type, value )
+ end
+ end
+
+ if @db.results_as_hash
+ new_row = HashWithTypes[*@stmt.columns.zip(row).flatten]
+ row.each_with_index { |value,idx|
+ new_row[idx] = value
+ }
+ row = new_row
+ else
+ if row.respond_to?(:fields)
+ row = ArrayWithTypes.new(row)
+ else
+ row = ArrayWithTypesAndFields.new(row)
+ end
+ row.fields = @stmt.columns
+ end
+
+ row.types = @stmt.types
+ row
+ end
+
+ # Required by the Enumerable mixin. Provides an internal iterator over the
+ # rows of the result set.
+ def each( &block )
+ while node = self.next
+ yield node
+ end
+ end
+
+ # Closes the statement that spawned this result set.
+ # <em>Use with caution!</em> Closing a result set will automatically
+ # close any other result sets that were spawned from the same statement.
+ def close
+ @stmt.close
+ end
+
+ # Queries whether the underlying statement has been closed or not.
+ def closed?
+ @stmt.closed?
+ end
+
+ # Returns the types of the columns returned by this result set.
+ def types
+ @stmt.types
+ end
+
+ # Returns the names of the columns returned by this result set.
+ def columns
+ @stmt.columns
+ end
+
+ end
+
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/statement.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/statement.rb
new file mode 100644
index 0000000..ade99ef
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/statement.rb
@@ -0,0 +1,146 @@
+require 'sqlite3/errors'
+require 'sqlite3/resultset'
+
+class String
+ def to_blob
+ SQLite3::Blob.new( self )
+ end
+end
+
+module SQLite3
+ # A statement represents a prepared-but-unexecuted SQL query. It will rarely
+ # (if ever) be instantiated directly by a client, and is most often obtained
+ # via the Database#prepare method.
+ class Statement
+ include Enumerable
+
+ # This is any text that followed the first valid SQL statement in the text
+ # with which the statement was initialized. If there was no trailing text,
+ # this will be the empty string.
+ attr_reader :remainder
+
+ # Binds the given variables to the corresponding placeholders in the SQL
+ # text.
+ #
+ # See Database#execute for a description of the valid placeholder
+ # syntaxes.
+ #
+ # Example:
+ #
+ # stmt = db.prepare( "select * from table where a=? and b=?" )
+ # stmt.bind_params( 15, "hello" )
+ #
+ # See also #execute, #bind_param, Statement#bind_param, and
+ # Statement#bind_params.
+ def bind_params( *bind_vars )
+ index = 1
+ bind_vars.flatten.each do |var|
+ if Hash === var
+ var.each { |key, val| bind_param key, val }
+ else
+ bind_param index, var
+ index += 1
+ end
+ end
+ end
+
+ # Execute the statement. This creates a new ResultSet object for the
+ # statement's virtual machine. If a block was given, the new ResultSet will
+ # be yielded to it; otherwise, the ResultSet will be returned.
+ #
+ # Any parameters will be bound to the statement using #bind_params.
+ #
+ # Example:
+ #
+ # stmt = db.prepare( "select * from table" )
+ # stmt.execute do |result|
+ # ...
+ # end
+ #
+ # See also #bind_params, #execute!.
+ def execute( *bind_vars )
+ reset! if active? || done?
+
+ bind_params(*bind_vars) unless bind_vars.empty?
+ @results = ResultSet.new(@connection, self)
+
+ yield @results if block_given?
+ @results
+ end
+
+ # Execute the statement. If no block was given, this returns an array of
+ # rows returned by executing the statement. Otherwise, each row will be
+ # yielded to the block.
+ #
+ # Any parameters will be bound to the statement using #bind_params.
+ #
+ # Example:
+ #
+ # stmt = db.prepare( "select * from table" )
+ # stmt.execute! do |row|
+ # ...
+ # end
+ #
+ # See also #bind_params, #execute.
+ def execute!( *bind_vars, &block )
+ execute(*bind_vars)
+ block_given? ? each(&block) : to_a
+ end
+
+ # Returns true if the statement is currently active, meaning it has an
+ # open result set.
+ def active?
+ !done?
+ end
+
+ # Return an array of the column names for this statement. Note that this
+ # may execute the statement in order to obtain the metadata; this makes it
+ # a (potentially) expensive operation.
+ def columns
+ get_metadata unless @columns
+ return @columns
+ end
+
+ def each
+ loop do
+ val = step
+ break self if done?
+ yield val
+ end
+ end
+
+ # Return an array of the data types for each column in this statement. Note
+ # that this may execute the statement in order to obtain the metadata; this
+ # makes it a (potentially) expensive operation.
+ def types
+ must_be_open!
+ get_metadata unless @types
+ @types
+ end
+
+ # A convenience method for obtaining the metadata about the query. Note
+ # that this will actually execute the SQL, which means it can be a
+ # (potentially) expensive operation.
+ def get_metadata
+ @columns = []
+ @types = []
+
+ column_count.times do |column|
+ @columns << column_name(column)
+ @types << column_decltype(column)
+ end
+
+ @columns.freeze
+ @types.freeze
+ end
+ private :get_metadata
+
+ # Performs a sanity check to ensure that the statement is not
+ # closed. If it is, an exception is raised.
+ def must_be_open! # :nodoc:
+ if closed?
+ raise SQLite3::Exception, "cannot use a closed statement"
+ end
+ end
+ end
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/translator.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/translator.rb
new file mode 100644
index 0000000..cae3694
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/translator.rb
@@ -0,0 +1,114 @@
+require 'time'
+require 'date'
+
+module SQLite3
+
+ # The Translator class encapsulates the logic and callbacks necessary for
+ # converting string data to a value of some specified type. Every Database
+ # instance may have a Translator instance, in order to assist in type
+ # translation (Database#type_translation).
+ #
+ # Further, applications may define their own custom type translation logic
+ # by registering translator blocks with the corresponding database's
+ # translator instance (Database#translator).
+ class Translator
+
+ # Create a new Translator instance. It will be preinitialized with default
+ # translators for most SQL data types.
+ def initialize
+ @translators = Hash.new( proc { |type,value| value } )
+ @type_name_cache = {}
+ register_default_translators
+ end
+
+ # Add a new translator block, which will be invoked to process type
+ # translations to the given type. The type should be an SQL datatype, and
+ # may include parentheses (i.e., "VARCHAR(30)"). However, any parenthetical
+ # information is stripped off and discarded, so type translation decisions
+ # are made solely on the "base" type name.
+ #
+ # The translator block itself should accept two parameters, "type" and
+ # "value". In this case, the "type" is the full type name (including
+ # parentheses), so the block itself may include logic for changing how a
+ # type is translated based on the additional data. The "value" parameter
+ # is the (string) data to convert.
+ #
+ # The block should return the translated value.
+ def add_translator( type, &block ) # :yields: type, value
+ @translators[ type_name( type ) ] = block
+ end
+
+ # Translate the given string value to a value of the given type. In the
+ # absense of an installed translator block for the given type, the value
+ # itself is always returned. Further, +nil+ values are never translated,
+ # and are always passed straight through regardless of the type parameter.
+ def translate( type, value )
+ unless value.nil?
+ # FIXME: this is a hack to support Sequel
+ if type && %w{ datetime timestamp }.include?(type.downcase)
+ @translators[ type_name( type ) ].call( type, value.to_s )
+ else
+ @translators[ type_name( type ) ].call( type, value )
+ end
+ end
+ end
+
+ # A convenience method for working with type names. This returns the "base"
+ # type name, without any parenthetical data.
+ def type_name( type )
+ @type_name_cache[type] ||= begin
+ type = "" if type.nil?
+ type = $1 if type =~ /^(.*?)\(/
+ type.upcase
+ end
+ end
+ private :type_name
+
+ # Register the default translators for the current Translator instance.
+ # This includes translators for most major SQL data types.
+ def register_default_translators
+ [ "time",
+ "timestamp" ].each { |type| add_translator( type ) { |t, v| Time.parse( v ) } }
+
+ add_translator( "date" ) { |t,v| Date.parse(v) }
+ add_translator( "datetime" ) { |t,v| DateTime.parse(v) }
+
+ [ "decimal",
+ "float",
+ "numeric",
+ "double",
+ "real",
+ "dec",
+ "fixed" ].each { |type| add_translator( type ) { |t,v| v.to_f } }
+
+ [ "integer",
+ "smallint",
+ "mediumint",
+ "int",
+ "bigint" ].each { |type| add_translator( type ) { |t,v| v.to_i } }
+
+ [ "bit",
+ "bool",
+ "boolean" ].each do |type|
+ add_translator( type ) do |t,v|
+ !( v.strip.gsub(/00+/,"0") == "0" ||
+ v.downcase == "false" ||
+ v.downcase == "f" ||
+ v.downcase == "no" ||
+ v.downcase == "n" )
+ end
+ end
+
+ add_translator( "tinyint" ) do |type, value|
+ if type =~ /\(\s*1\s*\)/
+ value.to_i == 1
+ else
+ value.to_i
+ end
+ end
+ end
+ private :register_default_translators
+
+ end
+
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/value.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/value.rb
new file mode 100644
index 0000000..e5e5bf2
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/value.rb
@@ -0,0 +1,57 @@
+require 'sqlite3/constants'
+
+module SQLite3
+
+ class Value
+ attr_reader :handle
+
+ def initialize( db, handle )
+ @driver = db.driver
+ @handle = handle
+ end
+
+ def null?
+ type == :null
+ end
+
+ def to_blob
+ @driver.value_blob( @handle )
+ end
+
+ def length( utf16=false )
+ if utf16
+ @driver.value_bytes16( @handle )
+ else
+ @driver.value_bytes( @handle )
+ end
+ end
+
+ def to_f
+ @driver.value_double( @handle )
+ end
+
+ def to_i
+ @driver.value_int( @handle )
+ end
+
+ def to_int64
+ @driver.value_int64( @handle )
+ end
+
+ def to_s( utf16=false )
+ @driver.value_text( @handle, utf16 )
+ end
+
+ def type
+ case @driver.value_type( @handle )
+ when Constants::ColumnType::INTEGER then :int
+ when Constants::ColumnType::FLOAT then :float
+ when Constants::ColumnType::TEXT then :text
+ when Constants::ColumnType::BLOB then :blob
+ when Constants::ColumnType::NULL then :null
+ end
+ end
+
+ end
+
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/version.rb b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/version.rb
new file mode 100644
index 0000000..7157562
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3/version.rb
@@ -0,0 +1,16 @@
+module SQLite3
+
+ module Version
+
+ MAJOR = 1
+ MINOR = 3
+ TINY = 0
+ BUILD = nil
+
+ STRING = [ MAJOR, MINOR, TINY, BUILD ].compact.join( "." )
+ #:beta-tag:
+
+ VERSION = '1.3.0'
+ end
+
+end
diff --git a/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3_native.so b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3_native.so
new file mode 100644
index 0000000..7fe2d1e
--- /dev/null
+++ b/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.0/lib/sqlite3_native.so
Binary files differ
diff --git a/ruby/gems/1.9.1/specifications/hpricot-0.8.1.gemspec b/ruby/gems/1.9.1/specifications/hpricot-0.8.1.gemspec
new file mode 100644
index 0000000..51a6c60
--- /dev/null
+++ b/ruby/gems/1.9.1/specifications/hpricot-0.8.1.gemspec
@@ -0,0 +1,32 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{hpricot}
+ s.version = "0.8.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["why the lucky stiff"]
+ s.date = %q{2009-04-03}
+ s.description = %q{a swift, liberal HTML parser with a fantastic library}
+ s.email = %q{why@ruby-lang.org}
+ s.extensions = ["ext/fast_xs/extconf.rb", "ext/hpricot_scan/extconf.rb"]
+ s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
+ s.files = ["CHANGELOG", "COPYING", "README", "Rakefile", "test/test_paths.rb", "test/test_preserved.rb", "test/test_parser.rb", "test/files", "test/files/cy0.html", "test/files/pace_application.html", "test/files/basic.xhtml", "test/files/utf8.html", "test/files/boingboing.html", "test/files/week9.html", "test/files/tenderlove.html", "test/files/immob.html", "test/files/why.xml", "test/files/uswebgen.html", "test/load_files.rb", "test/nokogiri-bench.rb", "test/test_builder.rb", "test/test_xml.rb", "test/test_alter.rb", "lib/hpricot", "lib/hpricot/tags.rb", "lib/hpricot/builder.rb", "lib/hpricot/traverse.rb", "lib/hpricot/elements.rb", "lib/hpricot/modules.rb", "lib/hpricot/inspect.rb", "lib/hpricot/tag.rb", "lib/hpricot/blankslate.rb", "lib/hpricot/xchar.rb", "lib/hpricot/htmlinfo.rb", "lib/hpricot/parse.rb", "lib/hpricot.rb", "extras/mingw-rbconfig.rb", "ext/hpricot_scan/hpricot_scan.h", "ext/hpricot_scan/HpricotScanService.java", "ext/fast_xs/FastXsService.java", "ext/hpricot_scan/hpricot_scan.c", "ext/hpricot_scan/hpricot_css.c", "ext/fast_xs/fast_xs.c", "ext/hpricot_scan/test.rb", "ext/hpricot_scan/extconf.rb", "ext/fast_xs/extconf.rb", "ext/hpricot_scan/hpricot_scan.rl", "ext/hpricot_scan/hpricot_scan.java.rl", "ext/hpricot_scan/hpricot_css.rl", "ext/hpricot_scan/hpricot_common.rl"]
+ s.has_rdoc = true
+ s.homepage = %q{http://code.whytheluckystiff.net/hpricot/}
+ s.rdoc_options = ["--quiet", "--title", "The Hpricot Reference", "--main", "README", "--inline-source"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{hobix}
+ s.rubygems_version = %q{1.3.0}
+ s.summary = %q{a swift, liberal HTML parser with a fantastic library}
+
+ if s.respond_to? :specification_version= then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
diff --git a/ruby/gems/1.9.1/specifications/json-shoes-1.1.3.gemspec b/ruby/gems/1.9.1/specifications/json-shoes-1.1.3.gemspec
new file mode 100644
index 0000000..eb30b77
--- /dev/null
+++ b/ruby/gems/1.9.1/specifications/json-shoes-1.1.3.gemspec
@@ -0,0 +1,34 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{json-shoes}
+ s.version = "1.1.3"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Florian Frank"]
+ s.date = %q{2008-07-10}
+ s.default_executable = %q{edit_json.rb}
+ s.description = %q{}
+ s.email = %q{flori@ping.de}
+ s.executables = ["edit_json.rb"]
+ s.extensions = ["ext/json/ext/parser/extconf.rb", "ext/json/ext/generator/extconf.rb"]
+ s.files = ["install.rb", "lib", "lib/json.rb", "lib/json", "lib/json/Array.xpm", "lib/json/FalseClass.xpm", "lib/json/json.xpm", "lib/json/editor.rb", "lib/json/Hash.xpm", "lib/json/Key.xpm", "lib/json/common.rb", "lib/json/String.xpm", "lib/json/pure", "lib/json/pure/generator.rb", "lib/json/pure/parser.rb", "lib/json/Numeric.xpm", "lib/json/ext.rb", "lib/json/pure.rb", "lib/json/NilClass.xpm", "lib/json/add", "lib/json/add/rails.rb", "lib/json/add/core.rb", "lib/json/TrueClass.xpm", "lib/json/version.rb", "ext", "ext/json", "ext/json/ext", "ext/json/ext/parser", "ext/json/ext/parser/unicode.h", "ext/json/ext/parser/parser.c", "ext/json/ext/parser/extconf.rb", "ext/json/ext/parser/unicode.c", "ext/json/ext/parser/parser.rl", "ext/json/ext/generator", "ext/json/ext/generator/unicode.h", "ext/json/ext/generator/extconf.rb", "ext/json/ext/generator/generator.c", "ext/json/ext/generator/unicode.c", "README", "diagrams", "CHANGES", "RUBY", "TODO", "VERSION", "tests", "tests/test_json.rb", "tests/test_json_addition.rb", "tests/fixtures", "tests/fixtures/fail11.json", "tests/fixtures/fail5.json", "tests/fixtures/fail10.json", "tests/fixtures/fail3.json", "tests/fixtures/pass15.json", "tests/fixtures/fail9.json", "tests/fixtures/fail22.json", "tests/fixtures/fail6.json", "tests/fixtures/pass2.json", "tests/fixtures/fail20.json", "tests/fixtures/fail19.json", "tests/fixtures/fail12.json", "tests/fixtures/fail7.json", "tests/fixtures/fail4.json", "tests/fixtures/fail1.json", "tests/fixtures/fail24.json", "tests/fixtures/fail21.json", "tests/fixtures/pass1.json", "tests/fixtures/fail2.json", "tests/fixtures/fail25.json", "tests/fixtures/pass16.json", "tests/fixtures/pass3.json", "tests/fixtures/fail18.json", "tests/fixtures/fail28.json", "tests/fixtures/fail13.json", "tests/fixtures/fail27.json", "tests/fixtures/pass17.json", "tests/fixtures/pass26.json", "tests/fixtures/fail23.json", "tests/fixtures/fail14.json", "tests/fixtures/fail8.json", "tests/runner.rb", "tests/test_json_generate.rb", "tests/test_json_rails.rb", "tests/test_json_unicode.rb", "tests/test_json_fixtures.rb", "benchmarks", "benchmarks/benchmark_parser.rb", "benchmarks/benchmark_generator.rb", "benchmarks/benchmark_rails.rb", "benchmarks/benchmark.txt", "Rakefile", "GPL", "data", "data/example.json", "data/index.html", "data/prototype.js", "bin", "bin/edit_json.rb", "bin/prettify_json.rb", "tools", "tools/fuzz.rb", "tools/server.rb"]
+ s.has_rdoc = true
+ s.homepage = %q{http://json.rubyforge.org}
+ s.rdoc_options = ["--title", "JSON -- A JSON implemention", "--main", "JSON", "--line-numbers"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{json}
+ s.rubygems_version = %q{1.3.0}
+ s.summary = %q{A JSON implementation as a Ruby extension}
+ s.test_files = ["tests/runner.rb"]
+
+ if s.respond_to? :specification_version= then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
diff --git a/ruby/gems/1.9.1/specifications/sqlite3-ruby-1.3.0.gemspec b/ruby/gems/1.9.1/specifications/sqlite3-ruby-1.3.0.gemspec
new file mode 100644
index 0000000..36ff34d
--- /dev/null
+++ b/ruby/gems/1.9.1/specifications/sqlite3-ruby-1.3.0.gemspec
@@ -0,0 +1,48 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{sqlite3-ruby}
+ s.version = "1.3.0"
+ #s.platform = %q{x86-mswin32-60}
+ s.platform = ""
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.3.5") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jamis Buck", "Luis Lavena", "Aaron Patterson"]
+ s.date = %q{2010-06-06}
+ s.description = %q{This module allows Ruby programs to interface with the SQLite3
+database engine (http://www.sqlite.org). You must have the
+SQLite engine installed in order to build this module.
+
+Note that this module is NOT compatible with SQLite 2.x.}
+ s.email = ["jamis@37signals.com", "luislavena@gmail.com", "aaron@tenderlovemaking.com"]
+ s.extra_rdoc_files = ["Manifest.txt", "API_CHANGES.rdoc", "README.rdoc", "CHANGELOG.rdoc", "ext/sqlite3/database.c", "ext/sqlite3/statement.c", "ext/sqlite3/sqlite3.c", "ext/sqlite3/exception.c"]
+ s.files = ["API_CHANGES.rdoc", "CHANGELOG.rdoc", "ChangeLog.cvs", "LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "ext/sqlite3/database.c", "ext/sqlite3/database.h", "ext/sqlite3/exception.c", "ext/sqlite3/exception.h", "ext/sqlite3/extconf.rb", "ext/sqlite3/sqlite3.c", "ext/sqlite3/sqlite3_ruby.h", "ext/sqlite3/statement.c", "ext/sqlite3/statement.h", "faq/faq.rb", "faq/faq.yml", "lib/sqlite3.rb", "lib/sqlite3/constants.rb", "lib/sqlite3/database.rb", "lib/sqlite3/errors.rb", "lib/sqlite3/pragmas.rb", "lib/sqlite3/resultset.rb", "lib/sqlite3/statement.rb", "lib/sqlite3/translator.rb", "lib/sqlite3/value.rb", "lib/sqlite3/version.rb", "setup.rb", "tasks/faq.rake", "tasks/gem.rake", "tasks/native.rake", "tasks/vendor_sqlite3.rake", "test/helper.rb", "test/test_database.rb", "test/test_deprecated.rb", "test/test_encoding.rb", "test/test_integration.rb", "test/test_integration_open_close.rb", "test/test_integration_pending.rb", "test/test_integration_resultset.rb", "test/test_integration_statement.rb", "test/test_sqlite3.rb", "test/test_statement.rb", "lib/sqlite3/1.8/sqlite3_native.so", "lib/sqlite3/1.9/sqlite3_native.so"]
+ s.homepage = %q{http://github.com/luislavena/sqlite3-ruby}
+ s.rdoc_options = ["--main", "README.rdoc"]
+ s.require_paths = ["lib"]
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
+ s.rubyforge_project = %q{sqlite3-ruby}
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{This module allows Ruby programs to interface with the SQLite3 database engine (http://www.sqlite.org)}
+ s.test_files = ["test/test_sqlite3.rb", "test/test_integration_open_close.rb", "test/test_database.rb", "test/test_integration.rb", "test/test_statement.rb", "test/test_integration_pending.rb", "test/test_deprecated.rb", "test/test_integration_statement.rb", "test/test_encoding.rb", "test/test_integration_resultset.rb"]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ #if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<rubyforge>, [">= 2.0.4"])
+ s.add_development_dependency(%q<rake-compiler>, ["~> 0.7.0"])
+ s.add_development_dependency(%q<hoe>, [">= 2.6.0"])
+ else
+ s.add_dependency(%q<rubyforge>, [">= 2.0.4"])
+ s.add_dependency(%q<rake-compiler>, ["~> 0.7.0"])
+ s.add_dependency(%q<hoe>, [">= 2.6.0"])
+ end
+ else
+ s.add_dependency(%q<rubyforge>, [">= 2.0.4"])
+ s.add_dependency(%q<rake-compiler>, ["~> 0.7.0"])
+ s.add_dependency(%q<hoe>, [">= 2.6.0"])
+ end
+end
diff --git a/ruby/lib/English.rb b/ruby/lib/English.rb
new file mode 100644
index 0000000..1a0e11d
--- /dev/null
+++ b/ruby/lib/English.rb
@@ -0,0 +1,155 @@
+# Include the English library file in a Ruby script, and you can
+# reference the global variables such as \VAR{\$\_} using less
+# cryptic names, listed in the following table.% \vref{tab:english}.
+#
+# Without 'English':
+#
+# $\ = ' -- '
+# "waterbuffalo" =~ /buff/
+# print $", $', $$, "\n"
+#
+# With English:
+#
+# require "English"
+#
+# $OUTPUT_FIELD_SEPARATOR = ' -- '
+# "waterbuffalo" =~ /buff/
+# print $LOADED_FEATURES, $POSTMATCH, $PID, "\n"
+
+
+# The exception object passed to +raise+.
+alias $ERROR_INFO $!
+
+# The stack backtrace generated by the last
+# exception. <tt>See Kernel.caller</tt> for details. Thread local.
+alias $ERROR_POSITION $@
+
+# The default separator pattern used by <tt>String.split</tt>. May be
+# set from the command line using the <tt>-F</tt> flag.
+alias $FS $;
+
+# The default separator pattern used by <tt>String.split</tt>. May be
+# set from the command line using the <tt>-F</tt> flag.
+alias $FIELD_SEPARATOR $;
+
+# The separator string output between the parameters to methods such
+# as <tt>Kernel.print</tt> and <tt>Array.join</tt>. Defaults to +nil+,
+# which adds no text.
+alias $OFS $,
+
+# The separator string output between the parameters to methods such
+# as <tt>Kernel.print</tt> and <tt>Array.join</tt>. Defaults to +nil+,
+# which adds no text.
+alias $OUTPUT_FIELD_SEPARATOR $,
+
+# The input record separator (newline by default). This is the value
+# that routines such as <tt>Kernel.gets</tt> use to determine record
+# boundaries. If set to +nil+, +gets+ will read the entire file.
+alias $RS $/
+
+# The input record separator (newline by default). This is the value
+# that routines such as <tt>Kernel.gets</tt> use to determine record
+# boundaries. If set to +nil+, +gets+ will read the entire file.
+alias $INPUT_RECORD_SEPARATOR $/
+
+# The string appended to the output of every call to methods such as
+# <tt>Kernel.print</tt> and <tt>IO.write</tt>. The default value is
+# +nil+.
+alias $ORS $\
+
+# The string appended to the output of every call to methods such as
+# <tt>Kernel.print</tt> and <tt>IO.write</tt>. The default value is
+# +nil+.
+alias $OUTPUT_RECORD_SEPARATOR $\
+
+# The number of the last line read from the current input file.
+alias $INPUT_LINE_NUMBER $.
+
+# The number of the last line read from the current input file.
+alias $NR $.
+
+# The last line read by <tt>Kernel.gets</tt> or
+# <tt>Kernel.readline</tt>. Many string-related functions in the
+# +Kernel+ module operate on <tt>$_</tt> by default. The variable is
+# local to the current scope. Thread local.
+alias $LAST_READ_LINE $_
+
+# The destination of output for <tt>Kernel.print</tt>
+# and <tt>Kernel.printf</tt>. The default value is
+# <tt>$stdout</tt>.
+alias $DEFAULT_OUTPUT $>
+
+# An object that provides access to the concatenation
+# of the contents of all the files
+# given as command-line arguments, or <tt>$stdin</tt>
+# (in the case where there are no
+# arguments). <tt>$<</tt> supports methods similar to a
+# +File+ object:
+# +inmode+, +close+,
+# <tt>closed?</tt>, +each+,
+# <tt>each_byte</tt>, <tt>each_line</tt>,
+# +eof+, <tt>eof?</tt>, +file+,
+# +filename+, +fileno+,
+# +getc+, +gets+, +lineno+,
+# <tt>lineno=</tt>, +path+,
+# +pos+, <tt>pos=</tt>,
+# +read+, +readchar+,
+# +readline+, +readlines+,
+# +rewind+, +seek+, +skip+,
+# +tell+, <tt>to_a</tt>, <tt>to_i</tt>,
+# <tt>to_io</tt>, <tt>to_s</tt>, along with the
+# methods in +Enumerable+. The method +file+
+# returns a +File+ object for the file currently
+# being read. This may change as <tt>$<</tt> reads
+# through the files on the command line. Read only.
+alias $DEFAULT_INPUT $<
+
+# The process number of the program being executed. Read only.
+alias $PID $$
+
+# The process number of the program being executed. Read only.
+alias $PROCESS_ID $$
+
+# The exit status of the last child process to terminate. Read
+# only. Thread local.
+alias $CHILD_STATUS $?
+
+# A +MatchData+ object that encapsulates the results of a successful
+# pattern match. The variables <tt>$&</tt>, <tt>$`</tt>, <tt>$'</tt>,
+# and <tt>$1</tt> to <tt>$9</tt> are all derived from
+# <tt>$~</tt>. Assigning to <tt>$~</tt> changes the values of these
+# derived variables. This variable is local to the current
+# scope. Thread local.
+alias $LAST_MATCH_INFO $~
+
+# If set to any value apart from +nil+ or +false+, all pattern matches
+# will be case insensitive, string comparisons will ignore case, and
+# string hash values will be case insensitive. Deprecated
+alias $IGNORECASE $=
+
+# An array of strings containing the command-line
+# options from the invocation of the program. Options
+# used by the Ruby interpreter will have been
+# removed. Read only. Also known simply as +ARGV+.
+alias $ARGV $*
+
+# The string matched by the last successful pattern
+# match. This variable is local to the current
+# scope. Read only. Thread local.
+alias $MATCH $&
+
+# The string preceding the match in the last
+# successful pattern match. This variable is local to
+# the current scope. Read only. Thread local.
+alias $PREMATCH $`
+
+# The string following the match in the last
+# successful pattern match. This variable is local to
+# the current scope. Read only. Thread local.
+alias $POSTMATCH $'
+
+# The contents of the highest-numbered group matched in the last
+# successful pattern match. Thus, in <tt>"cat" =~ /(c|a)(t|z)/</tt>,
+# <tt>$+</tt> will be set to "t". This variable is local to the
+# current scope. Read only. Thread local.
+alias $LAST_PAREN_MATCH $+
diff --git a/ruby/lib/abbrev.rb b/ruby/lib/abbrev.rb
new file mode 100644
index 0000000..338b89f
--- /dev/null
+++ b/ruby/lib/abbrev.rb
@@ -0,0 +1,103 @@
+#!/usr/bin/env ruby
+=begin
+#
+# Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
+#
+# All rights reserved. You can redistribute and/or modify it under
+# the same terms as Ruby.
+#
+# $Idaemons: /home/cvs/rb/abbrev.rb,v 1.2 2001/05/30 09:37:45 knu Exp $
+# $RoughId: abbrev.rb,v 1.4 2003/10/14 19:45:42 knu Exp $
+# $Id: abbrev.rb 11708 2007-02-12 23:01:19Z shyouhei $
+=end
+
+# Calculate the set of unique abbreviations for a given set of strings.
+#
+# require 'abbrev'
+# require 'pp'
+#
+# pp Abbrev::abbrev(['ruby', 'rules']).sort
+#
+# <i>Generates:</i>
+#
+# [["rub", "ruby"],
+# ["ruby", "ruby"],
+# ["rul", "rules"],
+# ["rule", "rules"],
+# ["rules", "rules"]]
+#
+# Also adds an +abbrev+ method to class +Array+.
+
+module Abbrev
+
+ # Given a set of strings, calculate the set of unambiguous
+ # abbreviations for those strings, and return a hash where the keys
+ # are all the possible abbreviations and the values are the full
+ # strings. Thus, given input of "car" and "cone", the keys pointing
+ # to "car" would be "ca" and "car", while those pointing to "cone"
+ # would be "co", "con", and "cone".
+ #
+ # The optional +pattern+ parameter is a pattern or a string. Only
+ # those input strings matching the pattern, or begging the string,
+ # are considered for inclusion in the output hash
+
+ def abbrev(words, pattern = nil)
+ table = {}
+ seen = Hash.new(0)
+
+ if pattern.is_a?(String)
+ pattern = /^#{Regexp.quote(pattern)}/ # regard as a prefix
+ end
+
+ words.each do |word|
+ next if (abbrev = word).empty?
+ while (len = abbrev.rindex(/[\w\W]\z/)) > 0
+ abbrev = word[0,len]
+
+ next if pattern && pattern !~ abbrev
+
+ case seen[abbrev] += 1
+ when 1
+ table[abbrev] = word
+ when 2
+ table.delete(abbrev)
+ else
+ break
+ end
+ end
+ end
+
+ words.each do |word|
+ next if pattern && pattern !~ word
+
+ table[word] = word
+ end
+
+ table
+ end
+
+ module_function :abbrev
+end
+
+class Array
+ # Calculates the set of unambiguous abbreviations for the strings in
+ # +self+. If passed a pattern or a string, only the strings matching
+ # the pattern or starting with the string are considered.
+ #
+ # %w{ car cone }.abbrev #=> { "ca" => "car", "car" => "car",
+ # "co" => "cone", "con" => cone",
+ # "cone" => "cone" }
+ def abbrev(pattern = nil)
+ Abbrev::abbrev(self, pattern)
+ end
+end
+
+if $0 == __FILE__
+ while line = gets
+ hash = line.split.abbrev
+
+ hash.sort.each do |k, v|
+ puts "#{k} => #{v}"
+ end
+ end
+end
diff --git a/ruby/lib/base64.rb b/ruby/lib/base64.rb
new file mode 100644
index 0000000..ebd796e
--- /dev/null
+++ b/ruby/lib/base64.rb
@@ -0,0 +1,91 @@
+#
+# = base64.rb: methods for base64-encoding and -decoding stings
+#
+
+# The Base64 module provides for the encoding (#encode64, #strict_encode64,
+# #urlsafe_encode64) and decoding (#decode64, #strict_decode64,
+# #urlsafe_decode64) of binary data using a Base64 representation.
+#
+# == Example
+#
+# A simple encoding and decoding.
+#
+# require "base64"
+#
+# enc = Base64.encode64('Send reinforcements')
+# # -> "U2VuZCByZWluZm9yY2VtZW50cw==\n"
+# plain = Base64.decode64(enc)
+# # -> "Send reinforcements"
+#
+# The purpose of using base64 to encode data is that it translates any
+# binary data into purely printable characters.
+
+module Base64
+ module_function
+
+ # Returns the Base64-encoded version of +bin+.
+ # This method complies with RFC 2045.
+ # Line feeds are added to every 60 encoded charactors.
+ #
+ # require 'base64'
+ # Base64.encode64("Now is the time for all good coders\nto learn Ruby")
+ #
+ # <i>Generates:</i>
+ #
+ # Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g
+ # UnVieQ==
+ def encode64(bin)
+ [bin].pack("m")
+ end
+
+ # Returns the Base64-decoded version of +str+.
+ # This method complies with RFC 2045.
+ # Characters outside the base alphabet are ignored.
+ #
+ # require 'base64'
+ # str = 'VGhpcyBpcyBsaW5lIG9uZQpUaGlzIG' +
+ # 'lzIGxpbmUgdHdvClRoaXMgaXMgbGlu' +
+ # 'ZSB0aHJlZQpBbmQgc28gb24uLi4K'
+ # puts Base64.decode64(str)
+ #
+ # <i>Generates:</i>
+ #
+ # This is line one
+ # This is line two
+ # This is line three
+ # And so on...
+ def decode64(str)
+ str.unpack("m").first
+ end
+
+ # Returns the Base64-encoded version of +bin+.
+ # This method complies with RFC 4648.
+ # No line feeds are added.
+ def strict_encode64(bin)
+ [bin].pack("m0")
+ end
+
+ # Returns the Base64-decoded version of +str+.
+ # This method complies with RFC 4648.
+ # ArgumentError is raised if +str+ is incorrectly padded or contains
+ # non-alphabet characters. Note that CR or LF are also rejected.
+ def strict_decode64(str)
+ str.unpack("m0").first
+ end
+
+ # Returns the Base64-encoded version of +bin+.
+ # This method complies with ``Base 64 Encoding with URL and Filename Safe
+ # Alphabet'' in RFC 4648.
+ # The alphabet uses '-' instead of '+' and '_' instead of '/'.
+ def urlsafe_encode64(bin)
+ strict_encode64(bin).tr("+/", "-_")
+ end
+
+ # Returns the Base64-decoded version of +str+.
+ # This method complies with ``Base 64 Encoding with URL and Filename Safe
+ # Alphabet'' in RFC 4648.
+ # The alphabet uses '-' instead of '+' and '_' instead of '/'.
+ def urlsafe_decode64(str)
+ strict_decode64(str.tr("-_", "+/"))
+ end
+end
diff --git a/ruby/lib/benchmark.rb b/ruby/lib/benchmark.rb
new file mode 100644
index 0000000..236c0e1
--- /dev/null
+++ b/ruby/lib/benchmark.rb
@@ -0,0 +1,573 @@
+=begin
+#
+# benchmark.rb - a performance benchmarking library
+#
+# $Id: benchmark.rb 22530 2009-02-22 12:49:00Z yugui $
+#
+# Created by Gotoken (gotoken@notwork.org).
+#
+# Documentation by Gotoken (original RD), Lyle Johnson (RDoc conversion), and
+# Gavin Sinclair (editing).
+#
+=end
+
+# == Overview
+#
+# The Benchmark module provides methods for benchmarking Ruby code, giving
+# detailed reports on the time taken for each task.
+#
+
+# The Benchmark module provides methods to measure and report the time
+# used to execute Ruby code.
+#
+# * Measure the time to construct the string given by the expression
+# <tt>"a"*1_000_000</tt>:
+#
+# require 'benchmark'
+#
+# puts Benchmark.measure { "a"*1_000_000 }
+#
+# On my machine (FreeBSD 3.2 on P5, 100MHz) this generates:
+#
+# 1.166667 0.050000 1.216667 ( 0.571355)
+#
+# This report shows the user CPU time, system CPU time, the sum of
+# the user and system CPU times, and the elapsed real time. The unit
+# of time is seconds.
+#
+# * Do some experiments sequentially using the #bm method:
+#
+# require 'benchmark'
+#
+# n = 50000
+# Benchmark.bm do |x|
+# x.report { for i in 1..n; a = "1"; end }
+# x.report { n.times do ; a = "1"; end }
+# x.report { 1.upto(n) do ; a = "1"; end }
+# end
+#
+# The result:
+#
+# user system total real
+# 1.033333 0.016667 1.016667 ( 0.492106)
+# 1.483333 0.000000 1.483333 ( 0.694605)
+# 1.516667 0.000000 1.516667 ( 0.711077)
+#
+# * Continuing the previous example, put a label in each report:
+#
+# require 'benchmark'
+#
+# n = 50000
+# Benchmark.bm(7) do |x|
+# x.report("for:") { for i in 1..n; a = "1"; end }
+# x.report("times:") { n.times do ; a = "1"; end }
+# x.report("upto:") { 1.upto(n) do ; a = "1"; end }
+# end
+#
+# The result:
+#
+# user system total real
+# for: 1.050000 0.000000 1.050000 ( 0.503462)
+# times: 1.533333 0.016667 1.550000 ( 0.735473)
+# upto: 1.500000 0.016667 1.516667 ( 0.711239)
+#
+#
+# * The times for some benchmarks depend on the order in which items
+# are run. These differences are due to the cost of memory
+# allocation and garbage collection. To avoid these discrepancies,
+# the #bmbm method is provided. For example, to compare ways to
+# sort an array of floats:
+#
+# require 'benchmark'
+#
+# array = (1..1000000).map { rand }
+#
+# Benchmark.bmbm do |x|
+# x.report("sort!") { array.dup.sort! }
+# x.report("sort") { array.dup.sort }
+# end
+#
+# The result:
+#
+# Rehearsal -----------------------------------------
+# sort! 11.928000 0.010000 11.938000 ( 12.756000)
+# sort 13.048000 0.020000 13.068000 ( 13.857000)
+# ------------------------------- total: 25.006000sec
+#
+# user system total real
+# sort! 12.959000 0.010000 12.969000 ( 13.793000)
+# sort 12.007000 0.000000 12.007000 ( 12.791000)
+#
+#
+# * Report statistics of sequential experiments with unique labels,
+# using the #benchmark method:
+#
+# require 'benchmark'
+# include Benchmark # we need the CAPTION and FMTSTR constants
+#
+# n = 50000
+# Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:", ">avg:") do |x|
+# tf = x.report("for:") { for i in 1..n; a = "1"; end }
+# tt = x.report("times:") { n.times do ; a = "1"; end }
+# tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
+# [tf+tt+tu, (tf+tt+tu)/3]
+# end
+#
+# The result:
+#
+# user system total real
+# for: 1.016667 0.016667 1.033333 ( 0.485749)
+# times: 1.450000 0.016667 1.466667 ( 0.681367)
+# upto: 1.533333 0.000000 1.533333 ( 0.722166)
+# >total: 4.000000 0.033333 4.033333 ( 1.889282)
+# >avg: 1.333333 0.011111 1.344444 ( 0.629761)
+
+module Benchmark
+
+ BENCHMARK_VERSION = "2002-04-25" #:nodoc"
+
+ def Benchmark::times() # :nodoc:
+ Process::times()
+ end
+
+
+ # Invokes the block with a <tt>Benchmark::Report</tt> object, which
+ # may be used to collect and report on the results of individual
+ # benchmark tests. Reserves <i>label_width</i> leading spaces for
+ # labels on each line. Prints _caption_ at the top of the
+ # report, and uses _fmt_ to format each line.
+ # If the block returns an array of
+ # <tt>Benchmark::Tms</tt> objects, these will be used to format
+ # additional lines of output. If _label_ parameters are
+ # given, these are used to label these extra lines.
+ #
+ # _Note_: Other methods provide a simpler interface to this one, and are
+ # suitable for nearly all benchmarking requirements. See the examples in
+ # Benchmark, and the #bm and #bmbm methods.
+ #
+ # Example:
+ #
+ # require 'benchmark'
+ # include Benchmark # we need the CAPTION and FMTSTR constants
+ #
+ # n = 50000
+ # Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:", ">avg:") do |x|
+ # tf = x.report("for:") { for i in 1..n; a = "1"; end }
+ # tt = x.report("times:") { n.times do ; a = "1"; end }
+ # tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
+ # [tf+tt+tu, (tf+tt+tu)/3]
+ # end
+ #
+ # <i>Generates:</i>
+ #
+ # user system total real
+ # for: 1.016667 0.016667 1.033333 ( 0.485749)
+ # times: 1.450000 0.016667 1.466667 ( 0.681367)
+ # upto: 1.533333 0.000000 1.533333 ( 0.722166)
+ # >total: 4.000000 0.033333 4.033333 ( 1.889282)
+ # >avg: 1.333333 0.011111 1.344444 ( 0.629761)
+ #
+
+ def benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) # :yield: report
+ sync = STDOUT.sync
+ STDOUT.sync = true
+ label_width ||= 0
+ fmtstr ||= FMTSTR
+ raise ArgumentError, "no block" unless iterator?
+ print caption
+ results = yield(Report.new(label_width, fmtstr))
+ Array === results and results.grep(Tms).each {|t|
+ print((labels.shift || t.label || "").ljust(label_width),
+ t.format(fmtstr))
+ }
+ STDOUT.sync = sync
+ end
+
+
+ # A simple interface to the #benchmark method, #bm is generates sequential reports
+ # with labels. The parameters have the same meaning as for #benchmark.
+ #
+ # require 'benchmark'
+ #
+ # n = 50000
+ # Benchmark.bm(7) do |x|
+ # x.report("for:") { for i in 1..n; a = "1"; end }
+ # x.report("times:") { n.times do ; a = "1"; end }
+ # x.report("upto:") { 1.upto(n) do ; a = "1"; end }
+ # end
+ #
+ # <i>Generates:</i>
+ #
+ # user system total real
+ # for: 1.050000 0.000000 1.050000 ( 0.503462)
+ # times: 1.533333 0.016667 1.550000 ( 0.735473)
+ # upto: 1.500000 0.016667 1.516667 ( 0.711239)
+ #
+
+ def bm(label_width = 0, *labels, &blk) # :yield: report
+ benchmark(" "*label_width + CAPTION, label_width, FMTSTR, *labels, &blk)
+ end
+
+
+ # Sometimes benchmark results are skewed because code executed
+ # earlier encounters different garbage collection overheads than
+ # that run later. #bmbm attempts to minimize this effect by running
+ # the tests twice, the first time as a rehearsal in order to get the
+ # runtime environment stable, the second time for
+ # real. <tt>GC.start</tt> is executed before the start of each of
+ # the real timings; the cost of this is not included in the
+ # timings. In reality, though, there's only so much that #bmbm can
+ # do, and the results are not guaranteed to be isolated from garbage
+ # collection and other effects.
+ #
+ # Because #bmbm takes two passes through the tests, it can
+ # calculate the required label width.
+ #
+ # require 'benchmark'
+ #
+ # array = (1..1000000).map { rand }
+ #
+ # Benchmark.bmbm do |x|
+ # x.report("sort!") { array.dup.sort! }
+ # x.report("sort") { array.dup.sort }
+ # end
+ #
+ # <i>Generates:</i>
+ #
+ # Rehearsal -----------------------------------------
+ # sort! 11.928000 0.010000 11.938000 ( 12.756000)
+ # sort 13.048000 0.020000 13.068000 ( 13.857000)
+ # ------------------------------- total: 25.006000sec
+ #
+ # user system total real
+ # sort! 12.959000 0.010000 12.969000 ( 13.793000)
+ # sort 12.007000 0.000000 12.007000 ( 12.791000)
+ #
+ # #bmbm yields a Benchmark::Job object and returns an array of
+ # Benchmark::Tms objects.
+ #
+ def bmbm(width = 0, &blk) # :yield: job
+ job = Job.new(width)
+ yield(job)
+ width = job.width
+ sync = STDOUT.sync
+ STDOUT.sync = true
+
+ # rehearsal
+ print "Rehearsal "
+ puts '-'*(width+CAPTION.length - "Rehearsal ".length)
+ list = []
+ job.list.each{|label,item|
+ print(label.ljust(width))
+ res = Benchmark::measure(&item)
+ print res.format()
+ list.push res
+ }
+ sum = Tms.new; list.each{|i| sum += i}
+ ets = sum.format("total: %tsec")
+ printf("%s %s\n\n",
+ "-"*(width+CAPTION.length-ets.length-1), ets)
+
+ # take
+ print ' '*width, CAPTION
+ list = []
+ ary = []
+ job.list.each{|label,item|
+ GC::start
+ print label.ljust(width)
+ res = Benchmark::measure(&item)
+ print res.format()
+ ary.push res
+ list.push [label, res]
+ }
+
+ STDOUT.sync = sync
+ ary
+ end
+
+ #
+ # Returns the time used to execute the given block as a
+ # Benchmark::Tms object.
+ #
+ def measure(label = "") # :yield:
+ t0, r0 = Benchmark.times, Time.now
+ yield
+ t1, r1 = Benchmark.times, Time.now
+ Benchmark::Tms.new(t1.utime - t0.utime,
+ t1.stime - t0.stime,
+ t1.cutime - t0.cutime,
+ t1.cstime - t0.cstime,
+ r1.to_f - r0.to_f,
+ label)
+ end
+
+ #
+ # Returns the elapsed real time used to execute the given block.
+ #
+ def realtime(&blk) # :yield:
+ r0 = Time.now
+ yield
+ r1 = Time.now
+ r1.to_f - r0.to_f
+ end
+
+
+
+ #
+ # A Job is a sequence of labelled blocks to be processed by the
+ # Benchmark.bmbm method. It is of little direct interest to the user.
+ #
+ class Job # :nodoc:
+ #
+ # Returns an initialized Job instance.
+ # Usually, one doesn't call this method directly, as new
+ # Job objects are created by the #bmbm method.
+ # _width_ is a initial value for the label offset used in formatting;
+ # the #bmbm method passes its _width_ argument to this constructor.
+ #
+ def initialize(width)
+ @width = width
+ @list = []
+ end
+
+ #
+ # Registers the given label and block pair in the job list.
+ #
+ def item(label = "", &blk) # :yield:
+ raise ArgumentError, "no block" unless block_given?
+ label += ' '
+ w = label.length
+ @width = w if @width < w
+ @list.push [label, blk]
+ self
+ end
+
+ alias report item
+
+ # An array of 2-element arrays, consisting of label and block pairs.
+ attr_reader :list
+
+ # Length of the widest label in the #list, plus one.
+ attr_reader :width
+ end
+
+ module_function :benchmark, :measure, :realtime, :bm, :bmbm
+
+
+
+ #
+ # This class is used by the Benchmark.benchmark and Benchmark.bm methods.
+ # It is of little direct interest to the user.
+ #
+ class Report # :nodoc:
+ #
+ # Returns an initialized Report instance.
+ # Usually, one doesn't call this method directly, as new
+ # Report objects are created by the #benchmark and #bm methods.
+ # _width_ and _fmtstr_ are the label offset and
+ # format string used by Tms#format.
+ #
+ def initialize(width = 0, fmtstr = nil)
+ @width, @fmtstr = width, fmtstr
+ end
+
+ #
+ # Prints the _label_ and measured time for the block,
+ # formatted by _fmt_. See Tms#format for the
+ # formatting rules.
+ #
+ def item(label = "", *fmt, &blk) # :yield:
+ print label.ljust(@width)
+ res = Benchmark::measure(&blk)
+ print res.format(@fmtstr, *fmt)
+ res
+ end
+
+ alias report item
+ end
+
+
+
+ #
+ # A data object, representing the times associated with a benchmark
+ # measurement.
+ #
+ class Tms
+ CAPTION = " user system total real\n"
+ FMTSTR = "%10.6u %10.6y %10.6t %10.6r\n"
+
+ # User CPU time
+ attr_reader :utime
+
+ # System CPU time
+ attr_reader :stime
+
+ # User CPU time of children
+ attr_reader :cutime
+
+ # System CPU time of children
+ attr_reader :cstime
+
+ # Elapsed real time
+ attr_reader :real
+
+ # Total time, that is _utime_ + _stime_ + _cutime_ + _cstime_
+ attr_reader :total
+
+ # Label
+ attr_reader :label
+
+ #
+ # Returns an initialized Tms object which has
+ # _u_ as the user CPU time, _s_ as the system CPU time,
+ # _cu_ as the children's user CPU time, _cs_ as the children's
+ # system CPU time, _real_ as the elapsed real time and _l_
+ # as the label.
+ #
+ def initialize(u = 0.0, s = 0.0, cu = 0.0, cs = 0.0, real = 0.0, l = nil)
+ @utime, @stime, @cutime, @cstime, @real, @label = u, s, cu, cs, real, l
+ @total = @utime + @stime + @cutime + @cstime
+ end
+
+ #
+ # Returns a new Tms object whose times are the sum of the times for this
+ # Tms object, plus the time required to execute the code block (_blk_).
+ #
+ def add(&blk) # :yield:
+ self + Benchmark::measure(&blk)
+ end
+
+ #
+ # An in-place version of #add.
+ #
+ def add!
+ t = Benchmark::measure(&blk)
+ @utime = utime + t.utime
+ @stime = stime + t.stime
+ @cutime = cutime + t.cutime
+ @cstime = cstime + t.cstime
+ @real = real + t.real
+ self
+ end
+
+ #
+ # Returns a new Tms object obtained by memberwise summation
+ # of the individual times for this Tms object with those of the other
+ # Tms object.
+ # This method and #/() are useful for taking statistics.
+ #
+ def +(other); memberwise(:+, other) end
+
+ #
+ # Returns a new Tms object obtained by memberwise subtraction
+ # of the individual times for the other Tms object from those of this
+ # Tms object.
+ #
+ def -(other); memberwise(:-, other) end
+
+ #
+ # Returns a new Tms object obtained by memberwise multiplication
+ # of the individual times for this Tms object by _x_.
+ #
+ def *(x); memberwise(:*, x) end
+
+ #
+ # Returns a new Tms object obtained by memberwise division
+ # of the individual times for this Tms object by _x_.
+ # This method and #+() are useful for taking statistics.
+ #
+ def /(x); memberwise(:/, x) end
+
+ #
+ # Returns the contents of this Tms object as
+ # a formatted string, according to a format string
+ # like that passed to Kernel.format. In addition, #format
+ # accepts the following extensions:
+ #
+ # <tt>%u</tt>:: Replaced by the user CPU time, as reported by Tms#utime.
+ # <tt>%y</tt>:: Replaced by the system CPU time, as reported by #stime (Mnemonic: y of "s*y*stem")
+ # <tt>%U</tt>:: Replaced by the children's user CPU time, as reported by Tms#cutime
+ # <tt>%Y</tt>:: Replaced by the children's system CPU time, as reported by Tms#cstime
+ # <tt>%t</tt>:: Replaced by the total CPU time, as reported by Tms#total
+ # <tt>%r</tt>:: Replaced by the elapsed real time, as reported by Tms#real
+ # <tt>%n</tt>:: Replaced by the label string, as reported by Tms#label (Mnemonic: n of "*n*ame")
+ #
+ # If _fmtstr_ is not given, FMTSTR is used as default value, detailing the
+ # user, system and real elapsed time.
+ #
+ def format(arg0 = nil, *args)
+ fmtstr = (arg0 || FMTSTR).dup
+ fmtstr.gsub!(/(%[-+\.\d]*)n/){"#{$1}s" % label}
+ fmtstr.gsub!(/(%[-+\.\d]*)u/){"#{$1}f" % utime}
+ fmtstr.gsub!(/(%[-+\.\d]*)y/){"#{$1}f" % stime}
+ fmtstr.gsub!(/(%[-+\.\d]*)U/){"#{$1}f" % cutime}
+ fmtstr.gsub!(/(%[-+\.\d]*)Y/){"#{$1}f" % cstime}
+ fmtstr.gsub!(/(%[-+\.\d]*)t/){"#{$1}f" % total}
+ fmtstr.gsub!(/(%[-+\.\d]*)r/){"(#{$1}f)" % real}
+ arg0 ? Kernel::format(fmtstr, *args) : fmtstr
+ end
+
+ #
+ # Same as #format.
+ #
+ def to_s
+ format
+ end
+
+ #
+ # Returns a new 6-element array, consisting of the
+ # label, user CPU time, system CPU time, children's
+ # user CPU time, children's system CPU time and elapsed
+ # real time.
+ #
+ def to_a
+ [@label, @utime, @stime, @cutime, @cstime, @real]
+ end
+
+ protected
+ def memberwise(op, x)
+ case x
+ when Benchmark::Tms
+ Benchmark::Tms.new(utime.__send__(op, x.utime),
+ stime.__send__(op, x.stime),
+ cutime.__send__(op, x.cutime),
+ cstime.__send__(op, x.cstime),
+ real.__send__(op, x.real)
+ )
+ else
+ Benchmark::Tms.new(utime.__send__(op, x),
+ stime.__send__(op, x),
+ cutime.__send__(op, x),
+ cstime.__send__(op, x),
+ real.__send__(op, x)
+ )
+ end
+ end
+ end
+
+ # The default caption string (heading above the output times).
+ CAPTION = Benchmark::Tms::CAPTION
+
+ # The default format string used to display times. See also Benchmark::Tms#format.
+ FMTSTR = Benchmark::Tms::FMTSTR
+end
+
+if __FILE__ == $0
+ include Benchmark
+
+ n = ARGV[0].to_i.nonzero? || 50000
+ puts %Q([#{n} times iterations of `a = "1"'])
+ benchmark(" " + CAPTION, 7, FMTSTR) do |x|
+ x.report("for:") {for i in 1..n; a = "1"; end} # Benchmark::measure
+ x.report("times:") {n.times do ; a = "1"; end}
+ x.report("upto:") {1.upto(n) do ; a = "1"; end}
+ end
+
+ benchmark do
+ [
+ measure{for i in 1..n; a = "1"; end}, # Benchmark::measure
+ measure{n.times do ; a = "1"; end},
+ measure{1.upto(n) do ; a = "1"; end}
+ ]
+ end
+end
diff --git a/ruby/lib/bigdecimal/jacobian.rb b/ruby/lib/bigdecimal/jacobian.rb
new file mode 100644
index 0000000..8c36ad1
--- /dev/null
+++ b/ruby/lib/bigdecimal/jacobian.rb
@@ -0,0 +1,85 @@
+#
+# require 'bigdecimal/jacobian'
+#
+# Provides methods to compute the Jacobian matrix of a set of equations at a
+# point x. In the methods below:
+#
+# f is an Object which is used to compute the Jacobian matrix of the equations.
+# It must provide the following methods:
+#
+# f.values(x):: returns the values of all functions at x
+#
+# f.zero:: returns 0.0
+# f.one:: returns 1.0
+# f.two:: returns 1.0
+# f.ten:: returns 10.0
+#
+# f.eps:: returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
+#
+# x is the point at which to compute the Jacobian.
+#
+# fx is f.values(x).
+#
+module Jacobian
+ #--
+ def isEqual(a,b,zero=0.0,e=1.0e-8)
+ aa = a.abs
+ bb = b.abs
+ if aa == zero && bb == zero then
+ true
+ else
+ if ((a-b)/(aa+bb)).abs < e then
+ true
+ else
+ false
+ end
+ end
+ end
+ #++
+
+ # Computes the derivative of f[i] at x[i].
+ # fx is the value of f at x.
+ def dfdxi(f,fx,x,i)
+ nRetry = 0
+ n = x.size
+ xSave = x[i]
+ ok = 0
+ ratio = f.ten*f.ten*f.ten
+ dx = x[i].abs/ratio
+ dx = fx[i].abs/ratio if isEqual(dx,f.zero,f.zero,f.eps)
+ dx = f.one/f.ten if isEqual(dx,f.zero,f.zero,f.eps)
+ until ok>0 do
+ s = f.zero
+ deriv = []
+ if(nRetry>100) then
+ raise "Singular Jacobian matrix. No change at x[" + i.to_s + "]"
+ end
+ dx = dx*f.two
+ x[i] += dx
+ fxNew = f.values(x)
+ for j in 0...n do
+ if !isEqual(fxNew[j],fx[j],f.zero,f.eps) then
+ ok += 1
+ deriv <<= (fxNew[j]-fx[j])/dx
+ else
+ deriv <<= f.zero
+ end
+ end
+ x[i] = xSave
+ end
+ deriv
+ end
+
+ # Computes the Jacobian of f at x. fx is the value of f at x.
+ def jacobian(f,fx,x)
+ n = x.size
+ dfdx = Array::new(n*n)
+ for i in 0...n do
+ df = dfdxi(f,fx,x,i)
+ for j in 0...n do
+ dfdx[j*n+i] = df[j]
+ end
+ end
+ dfdx
+ end
+end
diff --git a/ruby/lib/bigdecimal/ludcmp.rb b/ruby/lib/bigdecimal/ludcmp.rb
new file mode 100644
index 0000000..a18d017
--- /dev/null
+++ b/ruby/lib/bigdecimal/ludcmp.rb
@@ -0,0 +1,86 @@
+require 'bigdecimal'
+
+#
+# Solves a*x = b for x, using LU decomposition.
+#
+module LUSolve
+ # Performs LU decomposition of the n by n matrix a.
+ def ludecomp(a,n,zero=0,one=1)
+ prec = BigDecimal.limit(nil)
+ ps = []
+ scales = []
+ for i in 0...n do # pick up largest(abs. val.) element in each row.
+ ps <<= i
+ nrmrow = zero
+ ixn = i*n
+ for j in 0...n do
+ biggst = a[ixn+j].abs
+ nrmrow = biggst if biggst>nrmrow
+ end
+ if nrmrow>zero then
+ scales <<= one.div(nrmrow,prec)
+ else
+ raise "Singular matrix"
+ end
+ end
+ n1 = n - 1
+ for k in 0...n1 do # Gaussian elimination with partial pivoting.
+ biggst = zero;
+ for i in k...n do
+ size = a[ps[i]*n+k].abs*scales[ps[i]]
+ if size>biggst then
+ biggst = size
+ pividx = i
+ end
+ end
+ raise "Singular matrix" if biggst<=zero
+ if pividx!=k then
+ j = ps[k]
+ ps[k] = ps[pividx]
+ ps[pividx] = j
+ end
+ pivot = a[ps[k]*n+k]
+ for i in (k+1)...n do
+ psin = ps[i]*n
+ a[psin+k] = mult = a[psin+k].div(pivot,prec)
+ if mult!=zero then
+ pskn = ps[k]*n
+ for j in (k+1)...n do
+ a[psin+j] -= mult.mult(a[pskn+j],prec)
+ end
+ end
+ end
+ end
+ raise "Singular matrix" if a[ps[n1]*n+n1] == zero
+ ps
+ end
+
+ # Solves a*x = b for x, using LU decomposition.
+ #
+ # a is a matrix, b is a constant vector, x is the solution vector.
+ #
+ # ps is the pivot, a vector which indicates the permutation of rows performed
+ # during LU decomposition.
+ def lusolve(a,b,ps,zero=0.0)
+ prec = BigDecimal.limit(nil)
+ n = ps.size
+ x = []
+ for i in 0...n do
+ dot = zero
+ psin = ps[i]*n
+ for j in 0...i do
+ dot = a[psin+j].mult(x[j],prec) + dot
+ end
+ x <<= b[ps[i]] - dot
+ end
+ (n-1).downto(0) do |i|
+ dot = zero
+ psin = ps[i]*n
+ for j in (i+1)...n do
+ dot = a[psin+j].mult(x[j],prec) + dot
+ end
+ x[i] = (x[i]-dot).div(a[psin+i],prec)
+ end
+ x
+ end
+end
diff --git a/ruby/lib/bigdecimal/math.rb b/ruby/lib/bigdecimal/math.rb
new file mode 100644
index 0000000..58908d7
--- /dev/null
+++ b/ruby/lib/bigdecimal/math.rb
@@ -0,0 +1,237 @@
+require 'bigdecimal'
+
+#
+#--
+# Contents:
+# sqrt(x, prec)
+# sin (x, prec)
+# cos (x, prec)
+# atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
+# exp (x, prec)
+# log (x, prec)
+# PI (prec)
+# E (prec) == exp(1.0,prec)
+#
+# where:
+# x ... BigDecimal number to be computed.
+# |x| must be small enough to get convergence.
+# prec ... Number of digits to be obtained.
+#++
+#
+# Provides mathematical functions.
+#
+# Example:
+#
+# require "bigdecimal"
+# require "bigdecimal/math"
+#
+# include BigMath
+#
+# a = BigDecimal((PI(100)/2).to_s)
+# puts sin(a,100) # -> 0.10000000000000000000......E1
+#
+module BigMath
+
+ # Computes the square root of x to the specified number of digits of
+ # precision.
+ #
+ # BigDecimal.new('2').sqrt(16).to_s
+ # -> "0.14142135623730950488016887242096975E1"
+ #
+ def sqrt(x,prec)
+ x.sqrt(prec)
+ end
+
+ # Computes the sine of x to the specified number of digits of precision.
+ #
+ # If x is infinite or NaN, returns NaN.
+ def sin(x, prec)
+ raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
+ return BigDecimal("NaN") if x.infinite? || x.nan?
+ n = prec + BigDecimal.double_fig
+ one = BigDecimal("1")
+ two = BigDecimal("2")
+ x1 = x
+ x2 = x.mult(x,n)
+ sign = 1
+ y = x
+ d = y
+ i = one
+ z = one
+ while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
+ m = BigDecimal.double_fig if m < BigDecimal.double_fig
+ sign = -sign
+ x1 = x2.mult(x1,n)
+ i += two
+ z *= (i-one) * i
+ d = sign * x1.div(z,m)
+ y += d
+ end
+ y
+ end
+
+ # Computes the cosine of x to the specified number of digits of precision.
+ #
+ # If x is infinite or NaN, returns NaN.
+ def cos(x, prec)
+ raise ArgumentError, "Zero or negative precision for cos" if prec <= 0
+ return BigDecimal("NaN") if x.infinite? || x.nan?
+ n = prec + BigDecimal.double_fig
+ one = BigDecimal("1")
+ two = BigDecimal("2")
+ x1 = one
+ x2 = x.mult(x,n)
+ sign = 1
+ y = one
+ d = y
+ i = BigDecimal("0")
+ z = one
+ while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
+ m = BigDecimal.double_fig if m < BigDecimal.double_fig
+ sign = -sign
+ x1 = x2.mult(x1,n)
+ i += two
+ z *= (i-one) * i
+ d = sign * x1.div(z,m)
+ y += d
+ end
+ y
+ end
+
+ # Computes the arctangent of x to the specified number of digits of precision.
+ #
+ # If x is infinite or NaN, returns NaN.
+ # Raises an argument error if x > 1.
+ def atan(x, prec)
+ raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
+ return BigDecimal("NaN") if x.infinite? || x.nan?
+ raise ArgumentError, "x.abs must be less than 1.0" if x.abs>=1
+ n = prec + BigDecimal.double_fig
+ y = x
+ d = y
+ t = x
+ r = BigDecimal("3")
+ x2 = x.mult(x,n)
+ while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
+ m = BigDecimal.double_fig if m < BigDecimal.double_fig
+ t = -t.mult(x2,n)
+ d = t.div(r,m)
+ y += d
+ r += 2
+ end
+ y
+ end
+
+ # Computes the value of e (the base of natural logarithms) raised to the
+ # power of x, to the specified number of digits of precision.
+ #
+ # If x is infinite or NaN, returns NaN.
+ #
+ # BigMath::exp(BigDecimal.new('1'), 10).to_s
+ # -> "0.271828182845904523536028752390026306410273E1"
+ def exp(x, prec)
+ raise ArgumentError, "Zero or negative precision for exp" if prec <= 0
+ return BigDecimal("NaN") if x.infinite? || x.nan?
+ n = prec + BigDecimal.double_fig
+ one = BigDecimal("1")
+ x1 = one
+ y = one
+ d = y
+ z = one
+ i = 0
+ while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
+ m = BigDecimal.double_fig if m < BigDecimal.double_fig
+ x1 = x1.mult(x,n)
+ i += 1
+ z *= i
+ d = x1.div(z,m)
+ y += d
+ end
+ y
+ end
+
+ # Computes the natural logarithm of x to the specified number of digits
+ # of precision.
+ #
+ # Returns x if x is infinite or NaN.
+ #
+ def log(x, prec)
+ raise ArgumentError, "Zero or negative argument for log" if x <= 0 || prec <= 0
+ return x if x.infinite? || x.nan?
+ one = BigDecimal("1")
+ two = BigDecimal("2")
+ n = prec + BigDecimal.double_fig
+ x = (x - one).div(x + one,n)
+ x2 = x.mult(x,n)
+ y = x
+ d = y
+ i = one
+ while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
+ m = BigDecimal.double_fig if m < BigDecimal.double_fig
+ x = x2.mult(x,n)
+ i += two
+ d = x.div(i,m)
+ y += d
+ end
+ y*two
+ end
+
+ # Computes the value of pi to the specified number of digits of precision.
+ def PI(prec)
+ raise ArgumentError, "Zero or negative argument for PI" if prec <= 0
+ n = prec + BigDecimal.double_fig
+ zero = BigDecimal("0")
+ one = BigDecimal("1")
+ two = BigDecimal("2")
+
+ m25 = BigDecimal("-0.04")
+ m57121 = BigDecimal("-57121")
+
+ pi = zero
+
+ d = one
+ k = one
+ w = one
+ t = BigDecimal("-80")
+ while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
+ m = BigDecimal.double_fig if m < BigDecimal.double_fig
+ t = t*m25
+ d = t.div(k,m)
+ k = k+two
+ pi = pi + d
+ end
+
+ d = one
+ k = one
+ w = one
+ t = BigDecimal("956")
+ while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
+ m = BigDecimal.double_fig if m < BigDecimal.double_fig
+ t = t.div(m57121,n)
+ d = t.div(k,m)
+ pi = pi + d
+ k = k+two
+ end
+ pi
+ end
+
+ # Computes e (the base of natural logarithms) to the specified number of
+ # digits of precision.
+ def E(prec)
+ raise ArgumentError, "Zero or negative precision for E" if prec <= 0
+ n = prec + BigDecimal.double_fig
+ one = BigDecimal("1")
+ y = one
+ d = y
+ z = one
+ i = 0
+ while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
+ m = BigDecimal.double_fig if m < BigDecimal.double_fig
+ i += 1
+ z *= i
+ d = one.div(z,m)
+ y += d
+ end
+ y
+ end
+end
diff --git a/ruby/lib/bigdecimal/newton.rb b/ruby/lib/bigdecimal/newton.rb
new file mode 100644
index 0000000..59ac0f7
--- /dev/null
+++ b/ruby/lib/bigdecimal/newton.rb
@@ -0,0 +1,77 @@
+#
+# newton.rb
+#
+# Solves the nonlinear algebraic equation system f = 0 by Newton's method.
+# This program is not dependent on BigDecimal.
+#
+# To call:
+# n = nlsolve(f,x)
+# where n is the number of iterations required,
+# x is the initial value vector
+# f is an Object which is used to compute the values of the equations to be solved.
+# It must provide the following methods:
+#
+# f.values(x):: returns the values of all functions at x
+#
+# f.zero:: returns 0.0
+# f.one:: returns 1.0
+# f.two:: returns 1.0
+# f.ten:: returns 10.0
+#
+# f.eps:: returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
+#
+# On exit, x is the solution vector.
+#
+require "bigdecimal/ludcmp"
+require "bigdecimal/jacobian"
+
+module Newton
+ include LUSolve
+ include Jacobian
+
+ def norm(fv,zero=0.0)
+ s = zero
+ n = fv.size
+ for i in 0...n do
+ s += fv[i]*fv[i]
+ end
+ s
+ end
+
+ def nlsolve(f,x)
+ nRetry = 0
+ n = x.size
+
+ f0 = f.values(x)
+ zero = f.zero
+ one = f.one
+ two = f.two
+ p5 = one/two
+ d = norm(f0,zero)
+ minfact = f.ten*f.ten*f.ten
+ minfact = one/minfact
+ e = f.eps
+ while d >= e do
+ nRetry += 1
+ # Not yet converged. => Compute Jacobian matrix
+ dfdx = jacobian(f,f0,x)
+ # Solve dfdx*dx = -f0 to estimate dx
+ dx = lusolve(dfdx,f0,ludecomp(dfdx,n,zero,one),zero)
+ fact = two
+ xs = x.dup
+ begin
+ fact *= p5
+ if fact < minfact then
+ raise "Failed to reduce function values."
+ end
+ for i in 0...n do
+ x[i] = xs[i] - dx[i]*fact
+ end
+ f0 = f.values(x)
+ dn = norm(f0,zero)
+ end while(dn>=d)
+ d = dn
+ end
+ nRetry
+ end
+end
diff --git a/ruby/lib/bigdecimal/util.rb b/ruby/lib/bigdecimal/util.rb
new file mode 100644
index 0000000..257781f
--- /dev/null
+++ b/ruby/lib/bigdecimal/util.rb
@@ -0,0 +1,53 @@
+#
+# BigDecimal utility library.
+#
+# To use these functions, require 'bigdecimal/util'
+#
+# The following methods are provided to convert other types to BigDecimals:
+#
+# String#to_d -> BigDecimal
+# Float#to_d -> BigDecimal
+# Rational#to_d -> BigDecimal
+#
+# The following method is provided to convert BigDecimals to other types:
+#
+# BigDecimal#to_r -> Rational
+#
+# ----------------------------------------------------------------------
+#
+class Float < Numeric
+ def to_d
+ BigDecimal(self.to_s)
+ end
+end
+
+class String
+ def to_d
+ BigDecimal(self)
+ end
+end
+
+class BigDecimal < Numeric
+ # Converts a BigDecimal to a String of the form "nnnnnn.mmm".
+ # This method is deprecated; use BigDecimal#to_s("F") instead.
+ def to_digits
+ if self.nan? || self.infinite? || self.zero?
+ self.to_s
+ else
+ i = self.to_i.to_s
+ s,f,y,z = self.frac.split
+ i + "." + ("0"*(-z)) + f
+ end
+ end
+end
+
+class Rational < Numeric
+ # Converts a Rational to a BigDecimal
+ def to_d(nFig=0)
+ num = self.numerator.to_s
+ if nFig<=0
+ nFig = BigDecimal.double_fig*2+1
+ end
+ BigDecimal.new(num).div(self.denominator,nFig)
+ end
+end
diff --git a/ruby/lib/cgi.rb b/ruby/lib/cgi.rb
new file mode 100644
index 0000000..6acf05b
--- /dev/null
+++ b/ruby/lib/cgi.rb
@@ -0,0 +1,274 @@
+#
+# cgi.rb - cgi support library
+#
+# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
+#
+# Copyright (C) 2000 Information-technology Promotion Agency, Japan
+#
+# Author: Wakou Aoyama <wakou@ruby-lang.org>
+#
+# Documentation: Wakou Aoyama (RDoc'd and embellished by William Webber)
+#
+# == Overview
+#
+# The Common Gateway Interface (CGI) is a simple protocol
+# for passing an HTTP request from a web server to a
+# standalone program, and returning the output to the web
+# browser. Basically, a CGI program is called with the
+# parameters of the request passed in either in the
+# environment (GET) or via $stdin (POST), and everything
+# it prints to $stdout is returned to the client.
+#
+# This file holds the +CGI+ class. This class provides
+# functionality for retrieving HTTP request parameters,
+# managing cookies, and generating HTML output. See the
+# class documentation for more details and examples of use.
+#
+# The file cgi/session.rb provides session management
+# functionality; see that file for more details.
+#
+# See http://www.w3.org/CGI/ for more information on the CGI
+# protocol.
+
+raise "Please, use ruby 1.9.0 or later." if RUBY_VERSION < "1.9.0"
+
+# CGI class. See documentation for the file cgi.rb for an overview
+# of the CGI protocol.
+#
+# == Introduction
+#
+# CGI is a large class, providing several categories of methods, many of which
+# are mixed in from other modules. Some of the documentation is in this class,
+# some in the modules CGI::QueryExtension and CGI::HtmlExtension. See
+# CGI::Cookie for specific information on handling cookies, and cgi/session.rb
+# (CGI::Session) for information on sessions.
+#
+# For queries, CGI provides methods to get at environmental variables,
+# parameters, cookies, and multipart request data. For responses, CGI provides
+# methods for writing output and generating HTML.
+#
+# Read on for more details. Examples are provided at the bottom.
+#
+# == Queries
+#
+# The CGI class dynamically mixes in parameter and cookie-parsing
+# functionality, environmental variable access, and support for
+# parsing multipart requests (including uploaded files) from the
+# CGI::QueryExtension module.
+#
+# === Environmental Variables
+#
+# The standard CGI environmental variables are available as read-only
+# attributes of a CGI object. The following is a list of these variables:
+#
+#
+# AUTH_TYPE HTTP_HOST REMOTE_IDENT
+# CONTENT_LENGTH HTTP_NEGOTIATE REMOTE_USER
+# CONTENT_TYPE HTTP_PRAGMA REQUEST_METHOD
+# GATEWAY_INTERFACE HTTP_REFERER SCRIPT_NAME
+# HTTP_ACCEPT HTTP_USER_AGENT SERVER_NAME
+# HTTP_ACCEPT_CHARSET PATH_INFO SERVER_PORT
+# HTTP_ACCEPT_ENCODING PATH_TRANSLATED SERVER_PROTOCOL
+# HTTP_ACCEPT_LANGUAGE QUERY_STRING SERVER_SOFTWARE
+# HTTP_CACHE_CONTROL REMOTE_ADDR
+# HTTP_FROM REMOTE_HOST
+#
+#
+# For each of these variables, there is a corresponding attribute with the
+# same name, except all lower case and without a preceding HTTP_.
+# +content_length+ and +server_port+ are integers; the rest are strings.
+#
+# === Parameters
+#
+# The method #params() returns a hash of all parameters in the request as
+# name/value-list pairs, where the value-list is an Array of one or more
+# values. The CGI object itself also behaves as a hash of parameter names
+# to values, but only returns a single value (as a String) for each
+# parameter name.
+#
+# For instance, suppose the request contains the parameter
+# "favourite_colours" with the multiple values "blue" and "green". The
+# following behaviour would occur:
+#
+# cgi.params["favourite_colours"] # => ["blue", "green"]
+# cgi["favourite_colours"] # => "blue"
+#
+# If a parameter does not exist, the former method will return an empty
+# array, the latter an empty string. The simplest way to test for existence
+# of a parameter is by the #has_key? method.
+#
+# === Cookies
+#
+# HTTP Cookies are automatically parsed from the request. They are available
+# from the #cookies() accessor, which returns a hash from cookie name to
+# CGI::Cookie object.
+#
+# === Multipart requests
+#
+# If a request's method is POST and its content type is multipart/form-data,
+# then it may contain uploaded files. These are stored by the QueryExtension
+# module in the parameters of the request. The parameter name is the name
+# attribute of the file input field, as usual. However, the value is not
+# a string, but an IO object, either an IOString for small files, or a
+# Tempfile for larger ones. This object also has the additional singleton
+# methods:
+#
+# #local_path():: the path of the uploaded file on the local filesystem
+# #original_filename():: the name of the file on the client computer
+# #content_type():: the content type of the file
+#
+# == Responses
+#
+# The CGI class provides methods for sending header and content output to
+# the HTTP client, and mixes in methods for programmatic HTML generation
+# from CGI::HtmlExtension and CGI::TagMaker modules. The precise version of HTML
+# to use for HTML generation is specified at object creation time.
+#
+# === Writing output
+#
+# The simplest way to send output to the HTTP client is using the #out() method.
+# This takes the HTTP headers as a hash parameter, and the body content
+# via a block. The headers can be generated as a string using the #header()
+# method. The output stream can be written directly to using the #print()
+# method.
+#
+# === Generating HTML
+#
+# Each HTML element has a corresponding method for generating that
+# element as a String. The name of this method is the same as that
+# of the element, all lowercase. The attributes of the element are
+# passed in as a hash, and the body as a no-argument block that evaluates
+# to a String. The HTML generation module knows which elements are
+# always empty, and silently drops any passed-in body. It also knows
+# which elements require matching closing tags and which don't. However,
+# it does not know what attributes are legal for which elements.
+#
+# There are also some additional HTML generation methods mixed in from
+# the CGI::HtmlExtension module. These include individual methods for the
+# different types of form inputs, and methods for elements that commonly
+# take particular attributes where the attributes can be directly specified
+# as arguments, rather than via a hash.
+#
+# == Examples of use
+#
+# === Get form values
+#
+# require "cgi"
+# cgi = CGI.new
+# value = cgi['field_name'] # <== value string for 'field_name'
+# # if not 'field_name' included, then return "".
+# fields = cgi.keys # <== array of field names
+#
+# # returns true if form has 'field_name'
+# cgi.has_key?('field_name')
+# cgi.has_key?('field_name')
+# cgi.include?('field_name')
+#
+# CAUTION! cgi['field_name'] returned an Array with the old
+# cgi.rb(included in ruby 1.6)
+#
+# === Get form values as hash
+#
+# require "cgi"
+# cgi = CGI.new
+# params = cgi.params
+#
+# cgi.params is a hash.
+#
+# cgi.params['new_field_name'] = ["value"] # add new param
+# cgi.params['field_name'] = ["new_value"] # change value
+# cgi.params.delete('field_name') # delete param
+# cgi.params.clear # delete all params
+#
+#
+# === Save form values to file
+#
+# require "pstore"
+# db = PStore.new("query.db")
+# db.transaction do
+# db["params"] = cgi.params
+# end
+#
+#
+# === Restore form values from file
+#
+# require "pstore"
+# db = PStore.new("query.db")
+# db.transaction do
+# cgi.params = db["params"]
+# end
+#
+#
+# === Get multipart form values
+#
+# require "cgi"
+# cgi = CGI.new
+# value = cgi['field_name'] # <== value string for 'field_name'
+# value.read # <== body of value
+# value.local_path # <== path to local file of value
+# value.original_filename # <== original filename of value
+# value.content_type # <== content_type of value
+#
+# and value has StringIO or Tempfile class methods.
+#
+# === Get cookie values
+#
+# require "cgi"
+# cgi = CGI.new
+# values = cgi.cookies['name'] # <== array of 'name'
+# # if not 'name' included, then return [].
+# names = cgi.cookies.keys # <== array of cookie names
+#
+# and cgi.cookies is a hash.
+#
+# === Get cookie objects
+#
+# require "cgi"
+# cgi = CGI.new
+# for name, cookie in cgi.cookies
+# cookie.expires = Time.now + 30
+# end
+# cgi.out("cookie" => cgi.cookies) {"string"}
+#
+# cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... }
+#
+# require "cgi"
+# cgi = CGI.new
+# cgi.cookies['name'].expires = Time.now + 30
+# cgi.out("cookie" => cgi.cookies['name']) {"string"}
+#
+# === Print http header and html string to $DEFAULT_OUTPUT ($>)
+#
+# require "cgi"
+# cgi = CGI.new("html3") # add HTML generation methods
+# cgi.out() do
+# cgi.html() do
+# cgi.head{ cgi.title{"TITLE"} } +
+# cgi.body() do
+# cgi.form() do
+# cgi.textarea("get_text") +
+# cgi.br +
+# cgi.submit
+# end +
+# cgi.pre() do
+# CGI::escapeHTML(
+# "params: " + cgi.params.inspect + "\n" +
+# "cookies: " + cgi.cookies.inspect + "\n" +
+# ENV.collect() do |key, value|
+# key + " --> " + value + "\n"
+# end.join("")
+# )
+# end
+# end
+# end
+# end
+#
+# # add HTML generation methods
+# CGI.new("html3") # html3.2
+# CGI.new("html4") # html4.01 (Strict)
+# CGI.new("html4Tr") # html4.01 Transitional
+# CGI.new("html4Fr") # html4.01 Frameset
+#
+require 'cgi/core'
+require 'cgi/cookie'
+require 'cgi/util'
diff --git a/ruby/lib/cgi/cookie.rb b/ruby/lib/cgi/cookie.rb
new file mode 100644
index 0000000..4e8d3e2
--- /dev/null
+++ b/ruby/lib/cgi/cookie.rb
@@ -0,0 +1,144 @@
+# Class representing an HTTP cookie.
+#
+# In addition to its specific fields and methods, a Cookie instance
+# is a delegator to the array of its values.
+#
+# See RFC 2965.
+#
+# == Examples of use
+# cookie1 = CGI::Cookie::new("name", "value1", "value2", ...)
+# cookie1 = CGI::Cookie::new("name" => "name", "value" => "value")
+# cookie1 = CGI::Cookie::new('name' => 'name',
+# 'value' => ['value1', 'value2', ...],
+# 'path' => 'path', # optional
+# 'domain' => 'domain', # optional
+# 'expires' => Time.now, # optional
+# 'secure' => true # optional
+# )
+#
+# cgi.out("cookie" => [cookie1, cookie2]) { "string" }
+#
+# name = cookie1.name
+# values = cookie1.value
+# path = cookie1.path
+# domain = cookie1.domain
+# expires = cookie1.expires
+# secure = cookie1.secure
+#
+# cookie1.name = 'name'
+# cookie1.value = ['value1', 'value2', ...]
+# cookie1.path = 'path'
+# cookie1.domain = 'domain'
+# cookie1.expires = Time.now + 30
+# cookie1.secure = true
+class CGI
+ class Cookie < Array
+
+ # Create a new CGI::Cookie object.
+ #
+ # The contents of the cookie can be specified as a +name+ and one
+ # or more +value+ arguments. Alternatively, the contents can
+ # be specified as a single hash argument. The possible keywords of
+ # this hash are as follows:
+ #
+ # name:: the name of the cookie. Required.
+ # value:: the cookie's value or list of values.
+ # path:: the path for which this cookie applies. Defaults to the
+ # base directory of the CGI script.
+ # domain:: the domain for which this cookie applies.
+ # expires:: the time at which this cookie expires, as a +Time+ object.
+ # secure:: whether this cookie is a secure cookie or not (default to
+ # false). Secure cookies are only transmitted to HTTPS
+ # servers.
+ #
+ # These keywords correspond to attributes of the cookie object.
+ def initialize(name = "", *value)
+ if name.kind_of?(String)
+ @name = name
+ %r|^(.*/)|.match(ENV["SCRIPT_NAME"])
+ @path = ($1 or "")
+ @secure = false
+ return super(value)
+ end
+
+ options = name
+ unless options.has_key?("name")
+ raise ArgumentError, "`name' required"
+ end
+
+ @name = options["name"]
+ value = Array(options["value"])
+ # simple support for IE
+ if options["path"]
+ @path = options["path"]
+ else
+ %r|^(.*/)|.match(ENV["SCRIPT_NAME"])
+ @path = ($1 or "")
+ end
+ @domain = options["domain"]
+ @expires = options["expires"]
+ @secure = options["secure"] == true ? true : false
+
+ super(value)
+ end
+
+ attr_accessor("name", "path", "domain", "expires")
+ attr_reader("secure")
+
+ def value
+ self
+ end
+
+ def value=(val)
+ replace(Array(val))
+ end
+
+ # Set whether the Cookie is a secure cookie or not.
+ #
+ # +val+ must be a boolean.
+ def secure=(val)
+ @secure = val if val == true or val == false
+ @secure
+ end
+
+ # Convert the Cookie to its string representation.
+ def to_s
+ val = collect{|v| CGI::escape(v) }.join("&")
+ buf = "#{@name}=#{val}"
+ buf << "; domain=#{@domain}" if @domain
+ buf << "; path=#{@path}" if @path
+ buf << "; expires=#{CGI::rfc1123_date(@expires)}" if @expires
+ buf << "; secure" if @secure == true
+ buf
+ end
+
+ end # class Cookie
+
+
+ # Parse a raw cookie string into a hash of cookie-name=>Cookie
+ # pairs.
+ #
+ # cookies = CGI::Cookie::parse("raw_cookie_string")
+ # # { "name1" => cookie1, "name2" => cookie2, ... }
+ #
+ def Cookie::parse(raw_cookie)
+ cookies = Hash.new([])
+ return cookies unless raw_cookie
+
+ raw_cookie.split(/[;,]\s?/).each do |pairs|
+ name, values = pairs.split('=',2)
+ next unless name and values
+ name = CGI::unescape(name)
+ values ||= ""
+ values = values.split('&').collect{|v| CGI::unescape(v) }
+ if cookies.has_key?(name)
+ values = cookies[name].value + values
+ end
+ cookies[name] = Cookie::new(name, *values)
+ end
+
+ cookies
+ end
+end
+
+
diff --git a/ruby/lib/cgi/core.rb b/ruby/lib/cgi/core.rb
new file mode 100644
index 0000000..4521d87
--- /dev/null
+++ b/ruby/lib/cgi/core.rb
@@ -0,0 +1,786 @@
+class CGI
+
+ $CGI_ENV = ENV # for FCGI support
+
+ # String for carriage return
+ CR = "\015"
+
+ # String for linefeed
+ LF = "\012"
+
+ # Standard internet newline sequence
+ EOL = CR + LF
+
+ REVISION = '$Id: core.rb 23760 2009-06-20 09:06:49Z yugui $' #:nodoc:
+
+ NEEDS_BINMODE = true if /WIN/i.match(RUBY_PLATFORM)
+
+ # Path separators in different environments.
+ PATH_SEPARATOR = {'UNIX'=>'/', 'WINDOWS'=>'\\', 'MACINTOSH'=>':'}
+
+ # HTTP status codes.
+ HTTP_STATUS = {
+ "OK" => "200 OK",
+ "PARTIAL_CONTENT" => "206 Partial Content",
+ "MULTIPLE_CHOICES" => "300 Multiple Choices",
+ "MOVED" => "301 Moved Permanently",
+ "REDIRECT" => "302 Found",
+ "NOT_MODIFIED" => "304 Not Modified",
+ "BAD_REQUEST" => "400 Bad Request",
+ "AUTH_REQUIRED" => "401 Authorization Required",
+ "FORBIDDEN" => "403 Forbidden",
+ "NOT_FOUND" => "404 Not Found",
+ "METHOD_NOT_ALLOWED" => "405 Method Not Allowed",
+ "NOT_ACCEPTABLE" => "406 Not Acceptable",
+ "LENGTH_REQUIRED" => "411 Length Required",
+ "PRECONDITION_FAILED" => "412 Precondition Failed",
+ "SERVER_ERROR" => "500 Internal Server Error",
+ "NOT_IMPLEMENTED" => "501 Method Not Implemented",
+ "BAD_GATEWAY" => "502 Bad Gateway",
+ "VARIANT_ALSO_VARIES" => "506 Variant Also Negotiates"
+ }
+
+ # Abbreviated day-of-week names specified by RFC 822
+ RFC822_DAYS = %w[ Sun Mon Tue Wed Thu Fri Sat ]
+
+ # Abbreviated month names specified by RFC 822
+ RFC822_MONTHS = %w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]
+
+ # :startdoc:
+
+ def env_table
+ ENV
+ end
+
+ def stdinput
+ $stdin
+ end
+
+ def stdoutput
+ $stdout
+ end
+
+ private :env_table, :stdinput, :stdoutput
+
+
+ # Create an HTTP header block as a string.
+ #
+ # Includes the empty line that ends the header block.
+ #
+ # +options+ can be a string specifying the Content-Type (defaults
+ # to text/html), or a hash of header key/value pairs. The following
+ # header keys are recognized:
+ #
+ # type:: the Content-Type header. Defaults to "text/html"
+ # charset:: the charset of the body, appended to the Content-Type header.
+ # nph:: a boolean value. If true, prepend protocol string and status code, and
+ # date; and sets default values for "server" and "connection" if not
+ # explicitly set.
+ # status:: the HTTP status code, returned as the Status header. See the
+ # list of available status codes below.
+ # server:: the server software, returned as the Server header.
+ # connection:: the connection type, returned as the Connection header (for
+ # instance, "close".
+ # length:: the length of the content that will be sent, returned as the
+ # Content-Length header.
+ # language:: the language of the content, returned as the Content-Language
+ # header.
+ # expires:: the time on which the current content expires, as a +Time+
+ # object, returned as the Expires header.
+ # cookie:: a cookie or cookies, returned as one or more Set-Cookie headers.
+ # The value can be the literal string of the cookie; a CGI::Cookie
+ # object; an Array of literal cookie strings or Cookie objects; or a
+ # hash all of whose values are literal cookie strings or Cookie objects.
+ # These cookies are in addition to the cookies held in the
+ # @output_cookies field.
+ #
+ # Other header lines can also be set; they are appended as key: value.
+ #
+ # header
+ # # Content-Type: text/html
+ #
+ # header("text/plain")
+ # # Content-Type: text/plain
+ #
+ # header("nph" => true,
+ # "status" => "OK", # == "200 OK"
+ # # "status" => "200 GOOD",
+ # "server" => ENV['SERVER_SOFTWARE'],
+ # "connection" => "close",
+ # "type" => "text/html",
+ # "charset" => "iso-2022-jp",
+ # # Content-Type: text/html; charset=iso-2022-jp
+ # "length" => 103,
+ # "language" => "ja",
+ # "expires" => Time.now + 30,
+ # "cookie" => [cookie1, cookie2],
+ # "my_header1" => "my_value"
+ # "my_header2" => "my_value")
+ #
+ # The status codes are:
+ #
+ # "OK" --> "200 OK"
+ # "PARTIAL_CONTENT" --> "206 Partial Content"
+ # "MULTIPLE_CHOICES" --> "300 Multiple Choices"
+ # "MOVED" --> "301 Moved Permanently"
+ # "REDIRECT" --> "302 Found"
+ # "NOT_MODIFIED" --> "304 Not Modified"
+ # "BAD_REQUEST" --> "400 Bad Request"
+ # "AUTH_REQUIRED" --> "401 Authorization Required"
+ # "FORBIDDEN" --> "403 Forbidden"
+ # "NOT_FOUND" --> "404 Not Found"
+ # "METHOD_NOT_ALLOWED" --> "405 Method Not Allowed"
+ # "NOT_ACCEPTABLE" --> "406 Not Acceptable"
+ # "LENGTH_REQUIRED" --> "411 Length Required"
+ # "PRECONDITION_FAILED" --> "412 Precondition Failed"
+ # "SERVER_ERROR" --> "500 Internal Server Error"
+ # "NOT_IMPLEMENTED" --> "501 Method Not Implemented"
+ # "BAD_GATEWAY" --> "502 Bad Gateway"
+ # "VARIANT_ALSO_VARIES" --> "506 Variant Also Negotiates"
+ #
+ # This method does not perform charset conversion.
+ def header(options='text/html')
+ if options.is_a?(String)
+ content_type = options
+ buf = _header_for_string(content_type)
+ elsif options.is_a?(Hash)
+ if options.size == 1 && options.has_key?('type')
+ content_type = options['type']
+ buf = _header_for_string(content_type)
+ else
+ buf = _header_for_hash(options.dup)
+ end
+ else
+ raise ArgumentError.new("expected String or Hash but got #{options.class}")
+ end
+ if defined?(MOD_RUBY)
+ _header_for_modruby(buf)
+ return ''
+ else
+ buf << EOL # empty line of separator
+ return buf
+ end
+ end # header()
+
+ def _header_for_string(content_type) #:nodoc:
+ buf = ''
+ if nph?()
+ buf << "#{$CGI_ENV['SERVER_PROTOCOL'] || 'HTTP/1.0'} 200 OK#{EOL}"
+ buf << "Date: #{CGI.rfc1123_date(Time.now)}#{EOL}"
+ buf << "Server: #{$CGI_ENV['SERVER_SOFTWARE']}#{EOL}"
+ buf << "Connection: close#{EOL}"
+ end
+ buf << "Content-Type: #{content_type}#{EOL}"
+ if @output_cookies
+ @output_cookies.each {|cookie| buf << "Set-Cookie: #{cookie}#{EOL}" }
+ end
+ return buf
+ end # _header_for_string
+ private :_header_for_string
+
+ def _header_for_hash(options) #:nodoc:
+ buf = ''
+ ## add charset to option['type']
+ options['type'] ||= 'text/html'
+ charset = options.delete('charset')
+ options['type'] += "; charset=#{charset}" if charset
+ ## NPH
+ options.delete('nph') if defined?(MOD_RUBY)
+ if options.delete('nph') || nph?()
+ protocol = $CGI_ENV['SERVER_PROTOCOL'] || 'HTTP/1.0'
+ status = options.delete('status')
+ status = HTTP_STATUS[status] || status || '200 OK'
+ buf << "#{protocol} #{status}#{EOL}"
+ buf << "Date: #{CGI.rfc1123_date(Time.now)}#{EOL}"
+ options['server'] ||= $CGI_ENV['SERVER_SOFTWARE'] || ''
+ options['connection'] ||= 'close'
+ end
+ ## common headers
+ status = options.delete('status')
+ buf << "Status: #{HTTP_STATUS[status] || status}#{EOL}" if status
+ server = options.delete('server')
+ buf << "Server: #{server}#{EOL}" if server
+ connection = options.delete('connection')
+ buf << "Connection: #{connection}#{EOL}" if connection
+ type = options.delete('type')
+ buf << "Content-Type: #{type}#{EOL}" #if type
+ length = options.delete('length')
+ buf << "Content-Length: #{length}#{EOL}" if length
+ language = options.delete('language')
+ buf << "Content-Language: #{language}#{EOL}" if language
+ expires = options.delete('expires')
+ buf << "Expires: #{CGI.rfc1123_date(expires)}#{EOL}" if expires
+ ## cookie
+ if cookie = options.delete('cookie')
+ case cookie
+ when String, Cookie
+ buf << "Set-Cookie: #{cookie}#{EOL}"
+ when Array
+ arr = cookie
+ arr.each {|c| buf << "Set-Cookie: #{c}#{EOL}" }
+ when Hash
+ hash = cookie
+ hash.each {|name, c| buf << "Set-Cookie: #{c}#{EOL}" }
+ end
+ end
+ if @output_cookies
+ @output_cookies.each {|c| buf << "Set-Cookie: #{c}#{EOL}" }
+ end
+ ## other headers
+ options.each do |key, value|
+ buf << "#{key}: #{value}#{EOL}"
+ end
+ return buf
+ end # _header_for_hash
+ private :_header_for_hash
+
+ def nph? #:nodoc:
+ return /IIS\/(\d+)/.match($CGI_ENV['SERVER_SOFTWARE']) && $1.to_i < 5
+ end
+
+ def _header_for_modruby(buf) #:nodoc:
+ request = Apache::request
+ buf.scan(/([^:]+): (.+)#{EOL}/o) do |name, value|
+ warn sprintf("name:%s value:%s\n", name, value) if $DEBUG
+ case name
+ when 'Set-Cookie'
+ request.headers_out.add(name, value)
+ when /^status$/i
+ request.status_line = value
+ request.status = value.to_i
+ when /^content-type$/i
+ request.content_type = value
+ when /^content-encoding$/i
+ request.content_encoding = value
+ when /^location$/i
+ request.status = 302 if request.status == 200
+ request.headers_out[name] = value
+ else
+ request.headers_out[name] = value
+ end
+ end
+ request.send_http_header
+ return ''
+ end
+ private :_header_for_modruby
+ #
+
+ # Print an HTTP header and body to $DEFAULT_OUTPUT ($>)
+ #
+ # The header is provided by +options+, as for #header().
+ # The body of the document is that returned by the passed-
+ # in block. This block takes no arguments. It is required.
+ #
+ # cgi = CGI.new
+ # cgi.out{ "string" }
+ # # Content-Type: text/html
+ # # Content-Length: 6
+ # #
+ # # string
+ #
+ # cgi.out("text/plain") { "string" }
+ # # Content-Type: text/plain
+ # # Content-Length: 6
+ # #
+ # # string
+ #
+ # cgi.out("nph" => true,
+ # "status" => "OK", # == "200 OK"
+ # "server" => ENV['SERVER_SOFTWARE'],
+ # "connection" => "close",
+ # "type" => "text/html",
+ # "charset" => "iso-2022-jp",
+ # # Content-Type: text/html; charset=iso-2022-jp
+ # "language" => "ja",
+ # "expires" => Time.now + (3600 * 24 * 30),
+ # "cookie" => [cookie1, cookie2],
+ # "my_header1" => "my_value",
+ # "my_header2" => "my_value") { "string" }
+ #
+ # Content-Length is automatically calculated from the size of
+ # the String returned by the content block.
+ #
+ # If ENV['REQUEST_METHOD'] == "HEAD", then only the header
+ # is outputted (the content block is still required, but it
+ # is ignored).
+ #
+ # If the charset is "iso-2022-jp" or "euc-jp" or "shift_jis" then
+ # the content is converted to this charset, and the language is set
+ # to "ja".
+ def out(options = "text/html") # :yield:
+
+ options = { "type" => options } if options.kind_of?(String)
+ content = yield
+ options["length"] = content.bytesize.to_s
+ output = stdoutput
+ output.binmode if defined? output.binmode
+ output.print header(options)
+ output.print content unless "HEAD" == env_table['REQUEST_METHOD']
+ end
+
+
+ # Print an argument or list of arguments to the default output stream
+ #
+ # cgi = CGI.new
+ # cgi.print # default: cgi.print == $DEFAULT_OUTPUT.print
+ def print(*options)
+ stdoutput.print(*options)
+ end
+
+ # Parse an HTTP query string into a hash of key=>value pairs.
+ #
+ # params = CGI::parse("query_string")
+ # # {"name1" => ["value1", "value2", ...],
+ # # "name2" => ["value1", "value2", ...], ... }
+ #
+ def CGI::parse(query)
+ params = {}
+ query.split(/[&;]/).each do |pairs|
+ key, value = pairs.split('=',2).collect{|v| CGI::unescape(v) }
+ if key && value
+ params.has_key?(key) ? params[key].push(value) : params[key] = [value]
+ elsif key
+ params[key]=[]
+ end
+ end
+ params.default=[].freeze
+ params
+ end
+
+ # Maximum content length of post data
+ ##MAX_CONTENT_LENGTH = 2 * 1024 * 1024
+
+ # Maximum content length of multipart data
+ MAX_MULTIPART_LENGTH = 128 * 1024 * 1024
+
+ # Maximum number of request parameters when multipart
+ MAX_MULTIPART_COUNT = 128
+
+ # Mixin module. It provides the follow functionality groups:
+ #
+ # 1. Access to CGI environment variables as methods. See
+ # documentation to the CGI class for a list of these variables.
+ #
+ # 2. Access to cookies, including the cookies attribute.
+ #
+ # 3. Access to parameters, including the params attribute, and overloading
+ # [] to perform parameter value lookup by key.
+ #
+ # 4. The initialize_query method, for initialising the above
+ # mechanisms, handling multipart forms, and allowing the
+ # class to be used in "offline" mode.
+ #
+ module QueryExtension
+
+ %w[ CONTENT_LENGTH SERVER_PORT ].each do |env|
+ define_method(env.sub(/^HTTP_/, '').downcase) do
+ (val = env_table[env]) && Integer(val)
+ end
+ end
+
+ %w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
+ PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST
+ REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME
+ SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE
+
+ HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
+ HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST
+ HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT ].each do |env|
+ define_method(env.sub(/^HTTP_/, '').downcase) do
+ env_table[env]
+ end
+ end
+
+ # Get the raw cookies as a string.
+ def raw_cookie
+ env_table["HTTP_COOKIE"]
+ end
+
+ # Get the raw RFC2965 cookies as a string.
+ def raw_cookie2
+ env_table["HTTP_COOKIE2"]
+ end
+
+ # Get the cookies as a hash of cookie-name=>Cookie pairs.
+ attr_accessor :cookies
+
+ # Get the parameters as a hash of name=>values pairs, where
+ # values is an Array.
+ attr_reader :params
+
+ # Get the uploaed files as a hash of name=>values pairs
+ attr_reader :files
+
+ # Set all the parameters.
+ def params=(hash)
+ @params.clear
+ @params.update(hash)
+ end
+
+ def read_multipart(boundary, content_length)
+ ## read first boundary
+ stdin = $stdin
+ first_line = "--#{boundary}#{EOL}"
+ content_length -= first_line.bytesize
+ status = stdin.read(first_line.bytesize)
+ raise EOFError.new("no content body") unless status
+ raise EOFError.new("bad content body") unless first_line == status
+ ## parse and set params
+ params = {}
+ @files = {}
+ boundary_rexp = /--#{Regexp.quote(boundary)}(#{EOL}|--)/
+ boundary_size = "#{EOL}--#{boundary}#{EOL}".bytesize
+ boundary_end = nil
+ buf = ''
+ bufsize = 10 * 1024
+ max_count = MAX_MULTIPART_COUNT
+ n = 0
+ while true
+ (n += 1) < max_count or raise StandardError.new("too many parameters.")
+ ## create body (StringIO or Tempfile)
+ body = create_body(bufsize < content_length)
+ class << body
+ alias local_path path
+ attr_reader :original_filename, :content_type
+ end
+ ## find head and boundary
+ head = nil
+ separator = EOL * 2
+ until head && matched = boundary_rexp.match(buf)
+ if !head && pos = buf.index(separator)
+ len = pos + EOL.bytesize
+ head = buf[0, len]
+ buf = buf[(pos+separator.bytesize)..-1]
+ else
+ if head && buf.size > boundary_size
+ len = buf.size - boundary_size
+ body.print(buf[0, len])
+ buf[0, len] = ''
+ end
+ c = stdin.read(bufsize < content_length ? bufsize : content_length)
+ raise EOFError.new("bad content body") if c.nil? || c.empty?
+ buf << c
+ content_length -= c.bytesize
+ end
+ end
+ ## read to end of boundary
+ m = matched
+ len = m.begin(0)
+ s = buf[0, len]
+ if s =~ /(\r?\n)\z/
+ s = buf[0, len - $1.bytesize]
+ end
+ body.print(s)
+ buf = buf[m.end(0)..-1]
+ boundary_end = m[1]
+ content_length = -1 if boundary_end == '--'
+ ## reset file cursor position
+ body.rewind
+ ## original filename
+ /Content-Disposition:.* filename=(?:"(.*?)"|([^;\r\n]*))/i.match(head)
+ filename = $1 || $2 || ''
+ filename = CGI.unescape(filename) if unescape_filename?()
+ body.instance_variable_set('@original_filename', filename.taint)
+ ## content type
+ /Content-Type: (.*)/i.match(head)
+ (content_type = $1 || '').chomp!
+ body.instance_variable_set('@content_type', content_type.taint)
+ ## query parameter name
+ /Content-Disposition:.* name=(?:"(.*?)"|([^;\r\n]*))/i.match(head)
+ name = $1 || $2 || ''
+ if body.original_filename.empty?
+ value=body.read.dup.force_encoding(@accept_charset)
+ (params[name] ||= []) << value
+ unless value.valid_encoding?
+ if @accept_charset_error_block
+ @accept_charset_error_block.call(name,value)
+ else
+ raise InvalidEncoding,"Accept-Charset encoding error"
+ end
+ end
+ class << params[name].last;self;end.class_eval do
+ define_method(:read){self}
+ define_method(:original_filename){""}
+ define_method(:content_type){""}
+ end
+ else
+ (params[name] ||= []) << body
+ @files[name]=body
+ end
+ ## break loop
+ break if buf.size == 0
+ break if content_length == -1
+ end
+ raise EOFError, "bad boundary end of body part" unless boundary_end =~ /--/
+ params.default = []
+ params
+ end # read_multipart
+ private :read_multipart
+ def create_body(is_large) #:nodoc:
+ if is_large
+ require 'tempfile'
+ body = Tempfile.new('CGI', encoding: "ascii-8bit")
+ else
+ begin
+ require 'stringio'
+ body = StringIO.new("".force_encoding("ascii-8bit"))
+ rescue LoadError
+ require 'tempfile'
+ body = Tempfile.new('CGI', encoding: "ascii-8bit")
+ end
+ end
+ body.binmode if defined? body.binmode
+ return body
+ end
+ def unescape_filename? #:nodoc:
+ user_agent = $CGI_ENV['HTTP_USER_AGENT']
+ return /Mac/i.match(user_agent) && /Mozilla/i.match(user_agent) && !/MSIE/i.match(user_agent)
+ end
+
+ # offline mode. read name=value pairs on standard input.
+ def read_from_cmdline
+ require "shellwords"
+
+ string = unless ARGV.empty?
+ ARGV.join(' ')
+ else
+ if STDIN.tty?
+ STDERR.print(
+ %|(offline mode: enter name=value pairs on standard input)\n|
+ )
+ end
+ readlines.join(' ').gsub(/\n/, '')
+ end.gsub(/\\=/, '%3D').gsub(/\\&/, '%26')
+
+ words = Shellwords.shellwords(string)
+
+ if words.find{|x| /=/.match(x) }
+ words.join('&')
+ else
+ words.join('+')
+ end
+ end
+ private :read_from_cmdline
+
+ # A wrapper class to use a StringIO object as the body and switch
+ # to a TempFile when the passed threshold is passed.
+ # Initialize the data from the query.
+ #
+ # Handles multipart forms (in particular, forms that involve file uploads).
+ # Reads query parameters in the @params field, and cookies into @cookies.
+ def initialize_query()
+ if ("POST" == env_table['REQUEST_METHOD']) and
+ %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|.match(env_table['CONTENT_TYPE'])
+ raise StandardError.new("too large multipart data.") if env_table['CONTENT_LENGTH'].to_i > MAX_MULTIPART_LENGTH
+ boundary = $1.dup
+ @multipart = true
+ @params = read_multipart(boundary, Integer(env_table['CONTENT_LENGTH']))
+ else
+ @multipart = false
+ @params = CGI::parse(
+ case env_table['REQUEST_METHOD']
+ when "GET", "HEAD"
+ if defined?(MOD_RUBY)
+ Apache::request.args or ""
+ else
+ env_table['QUERY_STRING'] or ""
+ end
+ when "POST"
+ stdinput.binmode if defined? stdinput.binmode
+ stdinput.read(Integer(env_table['CONTENT_LENGTH'])) or ''
+ else
+ read_from_cmdline
+ end.dup.force_encoding(@accept_charset)
+ )
+ unless Encoding.find(@accept_charset) == Encoding::ASCII_8BIT
+ @params.each do |key,values|
+ values.each do |value|
+ unless value.valid_encoding?
+ if @accept_charset_error_block
+ @accept_charset_error_block.call(key,value)
+ else
+ raise InvalidEncoding,"Accept-Charset encoding error"
+ end
+ end
+ end
+ end
+ end
+ end
+
+ @cookies = CGI::Cookie::parse((env_table['HTTP_COOKIE'] or env_table['COOKIE']))
+ end
+ private :initialize_query
+
+ def multipart?
+ @multipart
+ end
+
+ # Get the value for the parameter with a given key.
+ #
+ # If the parameter has multiple values, only the first will be
+ # retrieved; use #params() to get the array of values.
+ def [](key)
+ params = @params[key]
+ return '' unless params
+ value = params[0]
+ if @multipart
+ if value
+ return value
+ elsif defined? StringIO
+ StringIO.new("".force_encoding("ascii-8bit"))
+ else
+ Tempfile.new("CGI",encoding:"ascii-8bit")
+ end
+ else
+ str = if value then value.dup else "" end
+ str
+ end
+ end
+
+ # Return all parameter keys as an array.
+ def keys(*args)
+ @params.keys(*args)
+ end
+
+ # Returns true if a given parameter key exists in the query.
+ def has_key?(*args)
+ @params.has_key?(*args)
+ end
+ alias key? has_key?
+ alias include? has_key?
+
+ end # QueryExtension
+
+ # InvalidEncoding Exception class
+ class InvalidEncoding < Exception; end
+
+ # @@accept_charset is default accept character set.
+ # This default value default is "UTF-8"
+ # If you want to change the default accept character set
+ # when create a new CGI instance, set this:
+ #
+ # CGI.accept_charset = "EUC-JP"
+ #
+
+ @@accept_charset="UTF-8"
+
+ def self.accept_charset
+ @@accept_charset
+ end
+
+ def self.accept_charset=(accept_charset)
+ @@accept_charset=accept_charset
+ end
+
+ # Create a new CGI instance.
+ #
+ # CGI accept constructor parameters either in a hash, string as a block.
+ # But string is as same as using :tag_maker of hash.
+ #
+ # CGI.new("html3") #=> CGI.new(:tag_maker=>"html3")
+ #
+ # And, if you specify string, @accept_charset cannot be changed.
+ # Instead, please use hash parameter.
+ #
+ # == accept_charset
+ #
+ # :accept_charset specifies encoding of received query string.
+ # ( Default value is @@accept_charset. )
+ # If not valid, raise CGI::InvalidEncoding
+ #
+ # Example. Suppose @@accept_charset # => "UTF-8"
+ #
+ # when not specified:
+ #
+ # cgi=CGI.new # @accept_charset # => "UTF-8"
+ #
+ # when specified "EUC-JP":
+ #
+ # cgi=CGI.new(:accept_charset => "EUC-JP") # => "EUC-JP"
+ #
+ # == block
+ #
+ # When you use a block, you can write a process
+ # that query encoding is invalid. Example:
+ #
+ # encoding_error={}
+ # cgi=CGI.new(:accept_charset=>"EUC-JP") do |name,value|
+ # encoding_error[key] = value
+ # end
+ #
+ # == tag_maker
+ #
+ # :tag_maker specifies which version of HTML to load the HTML generation
+ # methods for. The following versions of HTML are supported:
+ #
+ # html3:: HTML 3.x
+ # html4:: HTML 4.0
+ # html4Tr:: HTML 4.0 Transitional
+ # html4Fr:: HTML 4.0 with Framesets
+ #
+ # If not specified, no HTML generation methods will be loaded.
+ #
+ # If the CGI object is not created in a standard CGI call environment
+ # (that is, it can't locate REQUEST_METHOD in its environment), then
+ # it will run in "offline" mode. In this mode, it reads its parameters
+ # from the command line or (failing that) from standard input. Otherwise,
+ # cookies and other parameters are parsed automatically from the standard
+ # CGI locations, which varies according to the REQUEST_METHOD. It works this:
+ #
+ # CGI.new(:tag_maker=>"html3")
+ #
+ # This will be obsolete:
+ #
+ # CGI.new("html3")
+ #
+ attr_reader :accept_charset
+ def initialize(options = {},&block)
+ @accept_charset_error_block=block if block_given?
+ @options={:accept_charset=>@@accept_charset}
+ case options
+ when Hash
+ @options.merge!(options)
+ when String
+ @options[:tag_maker]=options
+ end
+ @accept_charset=@options[:accept_charset]
+ if defined?(MOD_RUBY) && !ENV.key?("GATEWAY_INTERFACE")
+ Apache.request.setup_cgi_env
+ end
+
+ extend QueryExtension
+ @multipart = false
+
+ initialize_query() # set @params, @cookies
+ @output_cookies = nil
+ @output_hidden = nil
+
+ case @options[:tag_maker]
+ when "html3"
+ require 'cgi/html'
+ extend Html3
+ element_init()
+ extend HtmlExtension
+ when "html4"
+ require 'cgi/html'
+ extend Html4
+ element_init()
+ extend HtmlExtension
+ when "html4Tr"
+ require 'cgi/html'
+ extend Html4Tr
+ element_init()
+ extend HtmlExtension
+ when "html4Fr"
+ require 'cgi/html'
+ extend Html4Tr
+ element_init()
+ extend Html4Fr
+ element_init()
+ extend HtmlExtension
+ end
+ end
+
+end # class CGI
+
+
diff --git a/ruby/lib/cgi/html.rb b/ruby/lib/cgi/html.rb
new file mode 100644
index 0000000..62f1fc1
--- /dev/null
+++ b/ruby/lib/cgi/html.rb
@@ -0,0 +1,1021 @@
+ # Base module for HTML-generation mixins.
+ #
+ # Provides methods for code generation for tags following
+ # the various DTD element types.
+class CGI
+ module TagMaker # :nodoc:
+
+ # Generate code for an element with required start and end tags.
+ #
+ # - -
+ def nn_element_def(element)
+ nOE_element_def(element, <<-END)
+ if block_given?
+ yield.to_s
+ else
+ ""
+ end +
+ "</#{element.upcase}>"
+ END
+ end
+
+ # Generate code for an empty element.
+ #
+ # - O EMPTY
+ def nOE_element_def(element, append = nil)
+ s = <<-END
+ attributes={attributes=>nil} if attributes.kind_of?(String)
+ "<#{element.upcase}" + attributes.collect{|name, value|
+ next unless value
+ " " + CGI::escapeHTML(name.to_s) +
+ if true == value
+ ""
+ else
+ '="' + CGI::escapeHTML(value.to_s) + '"'
+ end
+ }.join + ">"
+ END
+ s.sub!(/\Z/, " +") << append if append
+ s
+ end
+
+ # Generate code for an element for which the end (and possibly the
+ # start) tag is optional.
+ #
+ # O O or - O
+ def nO_element_def(element)
+ nOE_element_def(element, <<-END)
+ if block_given?
+ yield.to_s + "</#{element.upcase}>"
+ else
+ ""
+ end
+ END
+ end
+
+ end # TagMaker
+
+
+ #
+ # Mixin module providing HTML generation methods.
+ #
+ # For example,
+ # cgi.a("http://www.example.com") { "Example" }
+ # # => "<A HREF=\"http://www.example.com\">Example</A>"
+ #
+ # Modules Http3, Http4, etc., contain more basic HTML-generation methods
+ # (:title, :center, etc.).
+ #
+ # See class CGI for a detailed example.
+ #
+ module HtmlExtension
+
+
+ # Generate an Anchor element as a string.
+ #
+ # +href+ can either be a string, giving the URL
+ # for the HREF attribute, or it can be a hash of
+ # the element's attributes.
+ #
+ # The body of the element is the string returned by the no-argument
+ # block passed in.
+ #
+ # a("http://www.example.com") { "Example" }
+ # # => "<A HREF=\"http://www.example.com\">Example</A>"
+ #
+ # a("HREF" => "http://www.example.com", "TARGET" => "_top") { "Example" }
+ # # => "<A HREF=\"http://www.example.com\" TARGET=\"_top\">Example</A>"
+ #
+ def a(href = "") # :yield:
+ attributes = if href.kind_of?(String)
+ { "HREF" => href }
+ else
+ href
+ end
+ if block_given?
+ super(attributes){ yield }
+ else
+ super(attributes)
+ end
+ end
+
+ # Generate a Document Base URI element as a String.
+ #
+ # +href+ can either by a string, giving the base URL for the HREF
+ # attribute, or it can be a has of the element's attributes.
+ #
+ # The passed-in no-argument block is ignored.
+ #
+ # base("http://www.example.com/cgi")
+ # # => "<BASE HREF=\"http://www.example.com/cgi\">"
+ def base(href = "") # :yield:
+ attributes = if href.kind_of?(String)
+ { "HREF" => href }
+ else
+ href
+ end
+ if block_given?
+ super(attributes){ yield }
+ else
+ super(attributes)
+ end
+ end
+
+ # Generate a BlockQuote element as a string.
+ #
+ # +cite+ can either be a string, give the URI for the source of
+ # the quoted text, or a hash, giving all attributes of the element,
+ # or it can be omitted, in which case the element has no attributes.
+ #
+ # The body is provided by the passed-in no-argument block
+ #
+ # blockquote("http://www.example.com/quotes/foo.html") { "Foo!" }
+ # #=> "<BLOCKQUOTE CITE=\"http://www.example.com/quotes/foo.html\">Foo!</BLOCKQUOTE>
+ def blockquote(cite = {}) # :yield:
+ attributes = if cite.kind_of?(String)
+ { "CITE" => cite }
+ else
+ cite
+ end
+ if block_given?
+ super(attributes){ yield }
+ else
+ super(attributes)
+ end
+ end
+
+
+ # Generate a Table Caption element as a string.
+ #
+ # +align+ can be a string, giving the alignment of the caption
+ # (one of top, bottom, left, or right). It can be a hash of
+ # all the attributes of the element. Or it can be omitted.
+ #
+ # The body of the element is provided by the passed-in no-argument block.
+ #
+ # caption("left") { "Capital Cities" }
+ # # => <CAPTION ALIGN=\"left\">Capital Cities</CAPTION>
+ def caption(align = {}) # :yield:
+ attributes = if align.kind_of?(String)
+ { "ALIGN" => align }
+ else
+ align
+ end
+ if block_given?
+ super(attributes){ yield }
+ else
+ super(attributes)
+ end
+ end
+
+
+ # Generate a Checkbox Input element as a string.
+ #
+ # The attributes of the element can be specified as three arguments,
+ # +name+, +value+, and +checked+. +checked+ is a boolean value;
+ # if true, the CHECKED attribute will be included in the element.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # checkbox("name")
+ # # = checkbox("NAME" => "name")
+ #
+ # checkbox("name", "value")
+ # # = checkbox("NAME" => "name", "VALUE" => "value")
+ #
+ # checkbox("name", "value", true)
+ # # = checkbox("NAME" => "name", "VALUE" => "value", "CHECKED" => true)
+ def checkbox(name = "", value = nil, checked = nil)
+ attributes = if name.kind_of?(String)
+ { "TYPE" => "checkbox", "NAME" => name,
+ "VALUE" => value, "CHECKED" => checked }
+ else
+ name["TYPE"] = "checkbox"
+ name
+ end
+ input(attributes)
+ end
+
+ # Generate a sequence of checkbox elements, as a String.
+ #
+ # The checkboxes will all have the same +name+ attribute.
+ # Each checkbox is followed by a label.
+ # There will be one checkbox for each value. Each value
+ # can be specified as a String, which will be used both
+ # as the value of the VALUE attribute and as the label
+ # for that checkbox. A single-element array has the
+ # same effect.
+ #
+ # Each value can also be specified as a three-element array.
+ # The first element is the VALUE attribute; the second is the
+ # label; and the third is a boolean specifying whether this
+ # checkbox is CHECKED.
+ #
+ # Each value can also be specified as a two-element
+ # array, by omitting either the value element (defaults
+ # to the same as the label), or the boolean checked element
+ # (defaults to false).
+ #
+ # checkbox_group("name", "foo", "bar", "baz")
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="bar">bar
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
+ #
+ # checkbox_group("name", ["foo"], ["bar", true], "baz")
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
+ # # <INPUT TYPE="checkbox" CHECKED NAME="name" VALUE="bar">bar
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
+ #
+ # checkbox_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="1">Foo
+ # # <INPUT TYPE="checkbox" CHECKED NAME="name" VALUE="2">Bar
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="Baz">Baz
+ #
+ # checkbox_group("NAME" => "name",
+ # "VALUES" => ["foo", "bar", "baz"])
+ #
+ # checkbox_group("NAME" => "name",
+ # "VALUES" => [["foo"], ["bar", true], "baz"])
+ #
+ # checkbox_group("NAME" => "name",
+ # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
+ def checkbox_group(name = "", *values)
+ if name.kind_of?(Hash)
+ values = name["VALUES"]
+ name = name["NAME"]
+ end
+ values.collect{|value|
+ if value.kind_of?(String)
+ checkbox(name, value) + value
+ else
+ if value[-1] == true || value[-1] == false
+ checkbox(name, value[0], value[-1]) +
+ value[-2]
+ else
+ checkbox(name, value[0]) +
+ value[-1]
+ end
+ end
+ }.join
+ end
+
+
+ # Generate an File Upload Input element as a string.
+ #
+ # The attributes of the element can be specified as three arguments,
+ # +name+, +size+, and +maxlength+. +maxlength+ is the maximum length
+ # of the file's _name_, not of the file's _contents_.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # See #multipart_form() for forms that include file uploads.
+ #
+ # file_field("name")
+ # # <INPUT TYPE="file" NAME="name" SIZE="20">
+ #
+ # file_field("name", 40)
+ # # <INPUT TYPE="file" NAME="name" SIZE="40">
+ #
+ # file_field("name", 40, 100)
+ # # <INPUT TYPE="file" NAME="name" SIZE="40" MAXLENGTH="100">
+ #
+ # file_field("NAME" => "name", "SIZE" => 40)
+ # # <INPUT TYPE="file" NAME="name" SIZE="40">
+ def file_field(name = "", size = 20, maxlength = nil)
+ attributes = if name.kind_of?(String)
+ { "TYPE" => "file", "NAME" => name,
+ "SIZE" => size.to_s }
+ else
+ name["TYPE"] = "file"
+ name
+ end
+ attributes["MAXLENGTH"] = maxlength.to_s if maxlength
+ input(attributes)
+ end
+
+
+ # Generate a Form element as a string.
+ #
+ # +method+ should be either "get" or "post", and defaults to the latter.
+ # +action+ defaults to the current CGI script name. +enctype+
+ # defaults to "application/x-www-form-urlencoded".
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # See also #multipart_form() for forms that include file uploads.
+ #
+ # form{ "string" }
+ # # <FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
+ #
+ # form("get") { "string" }
+ # # <FORM METHOD="get" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
+ #
+ # form("get", "url") { "string" }
+ # # <FORM METHOD="get" ACTION="url" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
+ #
+ # form("METHOD" => "post", "ENCTYPE" => "enctype") { "string" }
+ # # <FORM METHOD="post" ENCTYPE="enctype">string</FORM>
+ def form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded")
+ attributes = if method.kind_of?(String)
+ { "METHOD" => method, "ACTION" => action,
+ "ENCTYPE" => enctype }
+ else
+ unless method.has_key?("METHOD")
+ method["METHOD"] = "post"
+ end
+ unless method.has_key?("ENCTYPE")
+ method["ENCTYPE"] = enctype
+ end
+ method
+ end
+ if block_given?
+ body = yield
+ else
+ body = ""
+ end
+ if @output_hidden
+ body += @output_hidden.collect{|k,v|
+ "<INPUT TYPE=\"HIDDEN\" NAME=\"#{k}\" VALUE=\"#{v}\">"
+ }.join
+ end
+ super(attributes){body}
+ end
+
+ # Generate a Hidden Input element as a string.
+ #
+ # The attributes of the element can be specified as two arguments,
+ # +name+ and +value+.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # hidden("name")
+ # # <INPUT TYPE="hidden" NAME="name">
+ #
+ # hidden("name", "value")
+ # # <INPUT TYPE="hidden" NAME="name" VALUE="value">
+ #
+ # hidden("NAME" => "name", "VALUE" => "reset", "ID" => "foo")
+ # # <INPUT TYPE="hidden" NAME="name" VALUE="value" ID="foo">
+ def hidden(name = "", value = nil)
+ attributes = if name.kind_of?(String)
+ { "TYPE" => "hidden", "NAME" => name, "VALUE" => value }
+ else
+ name["TYPE"] = "hidden"
+ name
+ end
+ input(attributes)
+ end
+
+ # Generate a top-level HTML element as a string.
+ #
+ # The attributes of the element are specified as a hash. The
+ # pseudo-attribute "PRETTY" can be used to specify that the generated
+ # HTML string should be indented. "PRETTY" can also be specified as
+ # a string as the sole argument to this method. The pseudo-attribute
+ # "DOCTYPE", if given, is used as the leading DOCTYPE SGML tag; it
+ # should include the entire text of this tag, including angle brackets.
+ #
+ # The body of the html element is supplied as a block.
+ #
+ # html{ "string" }
+ # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>string</HTML>
+ #
+ # html("LANG" => "ja") { "string" }
+ # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML LANG="ja">string</HTML>
+ #
+ # html("DOCTYPE" => false) { "string" }
+ # # <HTML>string</HTML>
+ #
+ # html("DOCTYPE" => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">') { "string" }
+ # # <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML>string</HTML>
+ #
+ # html("PRETTY" => " ") { "<BODY></BODY>" }
+ # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+ # # <HTML>
+ # # <BODY>
+ # # </BODY>
+ # # </HTML>
+ #
+ # html("PRETTY" => "\t") { "<BODY></BODY>" }
+ # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+ # # <HTML>
+ # # <BODY>
+ # # </BODY>
+ # # </HTML>
+ #
+ # html("PRETTY") { "<BODY></BODY>" }
+ # # = html("PRETTY" => " ") { "<BODY></BODY>" }
+ #
+ # html(if $VERBOSE then "PRETTY" end) { "HTML string" }
+ #
+ def html(attributes = {}) # :yield:
+ if nil == attributes
+ attributes = {}
+ elsif "PRETTY" == attributes
+ attributes = { "PRETTY" => true }
+ end
+ pretty = attributes.delete("PRETTY")
+ pretty = " " if true == pretty
+ buf = ""
+
+ if attributes.has_key?("DOCTYPE")
+ if attributes["DOCTYPE"]
+ buf += attributes.delete("DOCTYPE")
+ else
+ attributes.delete("DOCTYPE")
+ end
+ else
+ buf += doctype
+ end
+
+ if block_given?
+ buf += super(attributes){ yield }
+ else
+ buf += super(attributes)
+ end
+
+ if pretty
+ CGI::pretty(buf, pretty)
+ else
+ buf
+ end
+
+ end
+
+ # Generate an Image Button Input element as a string.
+ #
+ # +src+ is the URL of the image to use for the button. +name+
+ # is the input name. +alt+ is the alternative text for the image.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # image_button("url")
+ # # <INPUT TYPE="image" SRC="url">
+ #
+ # image_button("url", "name", "string")
+ # # <INPUT TYPE="image" SRC="url" NAME="name" ALT="string">
+ #
+ # image_button("SRC" => "url", "ATL" => "strng")
+ # # <INPUT TYPE="image" SRC="url" ALT="string">
+ def image_button(src = "", name = nil, alt = nil)
+ attributes = if src.kind_of?(String)
+ { "TYPE" => "image", "SRC" => src, "NAME" => name,
+ "ALT" => alt }
+ else
+ src["TYPE"] = "image"
+ src["SRC"] ||= ""
+ src
+ end
+ input(attributes)
+ end
+
+
+ # Generate an Image element as a string.
+ #
+ # +src+ is the URL of the image. +alt+ is the alternative text for
+ # the image. +width+ is the width of the image, and +height+ is
+ # its height.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # img("src", "alt", 100, 50)
+ # # <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
+ #
+ # img("SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50)
+ # # <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
+ def img(src = "", alt = "", width = nil, height = nil)
+ attributes = if src.kind_of?(String)
+ { "SRC" => src, "ALT" => alt }
+ else
+ src
+ end
+ attributes["WIDTH"] = width.to_s if width
+ attributes["HEIGHT"] = height.to_s if height
+ super(attributes)
+ end
+
+
+ # Generate a Form element with multipart encoding as a String.
+ #
+ # Multipart encoding is used for forms that include file uploads.
+ #
+ # +action+ is the action to perform. +enctype+ is the encoding
+ # type, which defaults to "multipart/form-data".
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # multipart_form{ "string" }
+ # # <FORM METHOD="post" ENCTYPE="multipart/form-data">string</FORM>
+ #
+ # multipart_form("url") { "string" }
+ # # <FORM METHOD="post" ACTION="url" ENCTYPE="multipart/form-data">string</FORM>
+ def multipart_form(action = nil, enctype = "multipart/form-data")
+ attributes = if action == nil
+ { "METHOD" => "post", "ENCTYPE" => enctype }
+ elsif action.kind_of?(String)
+ { "METHOD" => "post", "ACTION" => action,
+ "ENCTYPE" => enctype }
+ else
+ unless action.has_key?("METHOD")
+ action["METHOD"] = "post"
+ end
+ unless action.has_key?("ENCTYPE")
+ action["ENCTYPE"] = enctype
+ end
+ action
+ end
+ if block_given?
+ form(attributes){ yield }
+ else
+ form(attributes)
+ end
+ end
+
+
+ # Generate a Password Input element as a string.
+ #
+ # +name+ is the name of the input field. +value+ is its default
+ # value. +size+ is the size of the input field display. +maxlength+
+ # is the maximum length of the inputted password.
+ #
+ # Alternatively, attributes can be specified as a hash.
+ #
+ # password_field("name")
+ # # <INPUT TYPE="password" NAME="name" SIZE="40">
+ #
+ # password_field("name", "value")
+ # # <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="40">
+ #
+ # password_field("password", "value", 80, 200)
+ # # <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
+ #
+ # password_field("NAME" => "name", "VALUE" => "value")
+ # # <INPUT TYPE="password" NAME="name" VALUE="value">
+ def password_field(name = "", value = nil, size = 40, maxlength = nil)
+ attributes = if name.kind_of?(String)
+ { "TYPE" => "password", "NAME" => name,
+ "VALUE" => value, "SIZE" => size.to_s }
+ else
+ name["TYPE"] = "password"
+ name
+ end
+ attributes["MAXLENGTH"] = maxlength.to_s if maxlength
+ input(attributes)
+ end
+
+ # Generate a Select element as a string.
+ #
+ # +name+ is the name of the element. The +values+ are the options that
+ # can be selected from the Select menu. Each value can be a String or
+ # a one, two, or three-element Array. If a String or a one-element
+ # Array, this is both the value of that option and the text displayed for
+ # it. If a three-element Array, the elements are the option value, displayed
+ # text, and a boolean value specifying whether this option starts as selected.
+ # The two-element version omits either the option value (defaults to the same
+ # as the display text) or the boolean selected specifier (defaults to false).
+ #
+ # The attributes and options can also be specified as a hash. In this
+ # case, options are specified as an array of values as described above,
+ # with the hash key of "VALUES".
+ #
+ # popup_menu("name", "foo", "bar", "baz")
+ # # <SELECT NAME="name">
+ # # <OPTION VALUE="foo">foo</OPTION>
+ # # <OPTION VALUE="bar">bar</OPTION>
+ # # <OPTION VALUE="baz">baz</OPTION>
+ # # </SELECT>
+ #
+ # popup_menu("name", ["foo"], ["bar", true], "baz")
+ # # <SELECT NAME="name">
+ # # <OPTION VALUE="foo">foo</OPTION>
+ # # <OPTION VALUE="bar" SELECTED>bar</OPTION>
+ # # <OPTION VALUE="baz">baz</OPTION>
+ # # </SELECT>
+ #
+ # popup_menu("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
+ # # <SELECT NAME="name">
+ # # <OPTION VALUE="1">Foo</OPTION>
+ # # <OPTION SELECTED VALUE="2">Bar</OPTION>
+ # # <OPTION VALUE="Baz">Baz</OPTION>
+ # # </SELECT>
+ #
+ # popup_menu("NAME" => "name", "SIZE" => 2, "MULTIPLE" => true,
+ # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
+ # # <SELECT NAME="name" MULTIPLE SIZE="2">
+ # # <OPTION VALUE="1">Foo</OPTION>
+ # # <OPTION SELECTED VALUE="2">Bar</OPTION>
+ # # <OPTION VALUE="Baz">Baz</OPTION>
+ # # </SELECT>
+ def popup_menu(name = "", *values)
+
+ if name.kind_of?(Hash)
+ values = name["VALUES"]
+ size = name["SIZE"].to_s if name["SIZE"]
+ multiple = name["MULTIPLE"]
+ name = name["NAME"]
+ else
+ size = nil
+ multiple = nil
+ end
+
+ select({ "NAME" => name, "SIZE" => size,
+ "MULTIPLE" => multiple }){
+ values.collect{|value|
+ if value.kind_of?(String)
+ option({ "VALUE" => value }){ value }
+ else
+ if value[value.size - 1] == true
+ option({ "VALUE" => value[0], "SELECTED" => true }){
+ value[value.size - 2]
+ }
+ else
+ option({ "VALUE" => value[0] }){
+ value[value.size - 1]
+ }
+ end
+ end
+ }.join
+ }
+
+ end
+
+ # Generates a radio-button Input element.
+ #
+ # +name+ is the name of the input field. +value+ is the value of
+ # the field if checked. +checked+ specifies whether the field
+ # starts off checked.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # radio_button("name", "value")
+ # # <INPUT TYPE="radio" NAME="name" VALUE="value">
+ #
+ # radio_button("name", "value", true)
+ # # <INPUT TYPE="radio" NAME="name" VALUE="value" CHECKED>
+ #
+ # radio_button("NAME" => "name", "VALUE" => "value", "ID" => "foo")
+ # # <INPUT TYPE="radio" NAME="name" VALUE="value" ID="foo">
+ def radio_button(name = "", value = nil, checked = nil)
+ attributes = if name.kind_of?(String)
+ { "TYPE" => "radio", "NAME" => name,
+ "VALUE" => value, "CHECKED" => checked }
+ else
+ name["TYPE"] = "radio"
+ name
+ end
+ input(attributes)
+ end
+
+ # Generate a sequence of radio button Input elements, as a String.
+ #
+ # This works the same as #checkbox_group(). However, it is not valid
+ # to have more than one radiobutton in a group checked.
+ #
+ # radio_group("name", "foo", "bar", "baz")
+ # # <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
+ # # <INPUT TYPE="radio" NAME="name" VALUE="bar">bar
+ # # <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
+ #
+ # radio_group("name", ["foo"], ["bar", true], "baz")
+ # # <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
+ # # <INPUT TYPE="radio" CHECKED NAME="name" VALUE="bar">bar
+ # # <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
+ #
+ # radio_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
+ # # <INPUT TYPE="radio" NAME="name" VALUE="1">Foo
+ # # <INPUT TYPE="radio" CHECKED NAME="name" VALUE="2">Bar
+ # # <INPUT TYPE="radio" NAME="name" VALUE="Baz">Baz
+ #
+ # radio_group("NAME" => "name",
+ # "VALUES" => ["foo", "bar", "baz"])
+ #
+ # radio_group("NAME" => "name",
+ # "VALUES" => [["foo"], ["bar", true], "baz"])
+ #
+ # radio_group("NAME" => "name",
+ # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
+ def radio_group(name = "", *values)
+ if name.kind_of?(Hash)
+ values = name["VALUES"]
+ name = name["NAME"]
+ end
+ values.collect{|value|
+ if value.kind_of?(String)
+ radio_button(name, value) + value
+ else
+ if value[-1] == true || value[-1] == false
+ radio_button(name, value[0], value[-1]) +
+ value[-2]
+ else
+ radio_button(name, value[0]) +
+ value[-1]
+ end
+ end
+ }.join
+ end
+
+ # Generate a reset button Input element, as a String.
+ #
+ # This resets the values on a form to their initial values. +value+
+ # is the text displayed on the button. +name+ is the name of this button.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # reset
+ # # <INPUT TYPE="reset">
+ #
+ # reset("reset")
+ # # <INPUT TYPE="reset" VALUE="reset">
+ #
+ # reset("VALUE" => "reset", "ID" => "foo")
+ # # <INPUT TYPE="reset" VALUE="reset" ID="foo">
+ def reset(value = nil, name = nil)
+ attributes = if (not value) or value.kind_of?(String)
+ { "TYPE" => "reset", "VALUE" => value, "NAME" => name }
+ else
+ value["TYPE"] = "reset"
+ value
+ end
+ input(attributes)
+ end
+
+ alias scrolling_list popup_menu
+
+ # Generate a submit button Input element, as a String.
+ #
+ # +value+ is the text to display on the button. +name+ is the name
+ # of the input.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # submit
+ # # <INPUT TYPE="submit">
+ #
+ # submit("ok")
+ # # <INPUT TYPE="submit" VALUE="ok">
+ #
+ # submit("ok", "button1")
+ # # <INPUT TYPE="submit" VALUE="ok" NAME="button1">
+ #
+ # submit("VALUE" => "ok", "NAME" => "button1", "ID" => "foo")
+ # # <INPUT TYPE="submit" VALUE="ok" NAME="button1" ID="foo">
+ def submit(value = nil, name = nil)
+ attributes = if (not value) or value.kind_of?(String)
+ { "TYPE" => "submit", "VALUE" => value, "NAME" => name }
+ else
+ value["TYPE"] = "submit"
+ value
+ end
+ input(attributes)
+ end
+
+ # Generate a text field Input element, as a String.
+ #
+ # +name+ is the name of the input field. +value+ is its initial
+ # value. +size+ is the size of the input area. +maxlength+
+ # is the maximum length of input accepted.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # text_field("name")
+ # # <INPUT TYPE="text" NAME="name" SIZE="40">
+ #
+ # text_field("name", "value")
+ # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="40">
+ #
+ # text_field("name", "value", 80)
+ # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80">
+ #
+ # text_field("name", "value", 80, 200)
+ # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
+ #
+ # text_field("NAME" => "name", "VALUE" => "value")
+ # # <INPUT TYPE="text" NAME="name" VALUE="value">
+ def text_field(name = "", value = nil, size = 40, maxlength = nil)
+ attributes = if name.kind_of?(String)
+ { "TYPE" => "text", "NAME" => name, "VALUE" => value,
+ "SIZE" => size.to_s }
+ else
+ name["TYPE"] = "text"
+ name
+ end
+ attributes["MAXLENGTH"] = maxlength.to_s if maxlength
+ input(attributes)
+ end
+
+ # Generate a TextArea element, as a String.
+ #
+ # +name+ is the name of the textarea. +cols+ is the number of
+ # columns and +rows+ is the number of rows in the display.
+ #
+ # Alternatively, the attributes can be specified as a hash.
+ #
+ # The body is provided by the passed-in no-argument block
+ #
+ # textarea("name")
+ # # = textarea("NAME" => "name", "COLS" => 70, "ROWS" => 10)
+ #
+ # textarea("name", 40, 5)
+ # # = textarea("NAME" => "name", "COLS" => 40, "ROWS" => 5)
+ def textarea(name = "", cols = 70, rows = 10) # :yield:
+ attributes = if name.kind_of?(String)
+ { "NAME" => name, "COLS" => cols.to_s,
+ "ROWS" => rows.to_s }
+ else
+ name
+ end
+ if block_given?
+ super(attributes){ yield }
+ else
+ super(attributes)
+ end
+ end
+
+ end # HtmlExtension
+
+
+ # Mixin module for HTML version 3 generation methods.
+ module Html3 # :nodoc:
+
+ # The DOCTYPE declaration for this version of HTML
+ def doctype
+ %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">|
+ end
+
+ # Initialise the HTML generation methods for this version.
+ def element_init
+ extend TagMaker
+ methods = ""
+ # - -
+ for element in %w[ A TT I B U STRIKE BIG SMALL SUB SUP EM STRONG
+ DFN CODE SAMP KBD VAR CITE FONT ADDRESS DIV center MAP
+ APPLET PRE XMP LISTING DL OL UL DIR MENU SELECT table TITLE
+ STYLE SCRIPT H1 H2 H3 H4 H5 H6 TEXTAREA FORM BLOCKQUOTE
+ CAPTION ]
+ methods += <<-BEGIN + nn_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+
+ # - O EMPTY
+ for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
+ ISINDEX META ]
+ methods += <<-BEGIN + nOE_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+
+ # O O or - O
+ for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD LI OPTION tr
+ th td ]
+ methods += <<-BEGIN + nO_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+ eval(methods)
+ end
+
+ end # Html3
+
+
+ # Mixin module for HTML version 4 generation methods.
+ module Html4 # :nodoc:
+
+ # The DOCTYPE declaration for this version of HTML
+ def doctype
+ %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">|
+ end
+
+ # Initialise the HTML generation methods for this version.
+ def element_init
+ extend TagMaker
+ methods = ""
+ # - -
+ for element in %w[ TT I B BIG SMALL EM STRONG DFN CODE SAMP KBD
+ VAR CITE ABBR ACRONYM SUB SUP SPAN BDO ADDRESS DIV MAP OBJECT
+ H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT OPTGROUP
+ FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT
+ TEXTAREA FORM A BLOCKQUOTE CAPTION ]
+ methods += <<-BEGIN + nn_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+
+ # - O EMPTY
+ for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META ]
+ methods += <<-BEGIN + nOE_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+
+ # O O or - O
+ for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
+ COLGROUP TR TH TD HEAD]
+ methods += <<-BEGIN + nO_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+ eval(methods)
+ end
+
+ end # Html4
+
+
+ # Mixin module for HTML version 4 transitional generation methods.
+ module Html4Tr # :nodoc:
+
+ # The DOCTYPE declaration for this version of HTML
+ def doctype
+ %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">|
+ end
+
+ # Initialise the HTML generation methods for this version.
+ def element_init
+ extend TagMaker
+ methods = ""
+ # - -
+ for element in %w[ TT I B U S STRIKE BIG SMALL EM STRONG DFN
+ CODE SAMP KBD VAR CITE ABBR ACRONYM FONT SUB SUP SPAN BDO
+ ADDRESS DIV CENTER MAP OBJECT APPLET H1 H2 H3 H4 H5 H6 PRE Q
+ INS DEL DL OL UL DIR MENU LABEL SELECT OPTGROUP FIELDSET
+ LEGEND BUTTON TABLE IFRAME NOFRAMES TITLE STYLE SCRIPT
+ NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ]
+ methods += <<-BEGIN + nn_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+
+ # - O EMPTY
+ for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
+ COL ISINDEX META ]
+ methods += <<-BEGIN + nOE_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+
+ # O O or - O
+ for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
+ COLGROUP TR TH TD HEAD ]
+ methods += <<-BEGIN + nO_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+ eval(methods)
+ end
+
+ end # Html4Tr
+
+
+ # Mixin module for generating HTML version 4 with framesets.
+ module Html4Fr # :nodoc:
+
+ # The DOCTYPE declaration for this version of HTML
+ def doctype
+ %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">|
+ end
+
+ # Initialise the HTML generation methods for this version.
+ def element_init
+ methods = ""
+ # - -
+ for element in %w[ FRAMESET ]
+ methods += <<-BEGIN + nn_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+
+ # - O EMPTY
+ for element in %w[ FRAME ]
+ methods += <<-BEGIN + nOE_element_def(element) + <<-END
+ def #{element.downcase}(attributes = {})
+ BEGIN
+ end
+ END
+ end
+ eval(methods)
+ end
+
+ end # Html4Fr
+end
+
+
diff --git a/ruby/lib/cgi/session.rb b/ruby/lib/cgi/session.rb
new file mode 100644
index 0000000..2b5aa84
--- /dev/null
+++ b/ruby/lib/cgi/session.rb
@@ -0,0 +1,537 @@
+#
+# cgi/session.rb - session support for cgi scripts
+#
+# Copyright (C) 2001 Yukihiro "Matz" Matsumoto
+# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
+# Copyright (C) 2000 Information-technology Promotion Agency, Japan
+#
+# Author: Yukihiro "Matz" Matsumoto
+#
+# Documentation: William Webber (william@williamwebber.com)
+#
+# == Overview
+#
+# This file provides the +CGI::Session+ class, which provides session
+# support for CGI scripts. A session is a sequence of HTTP requests
+# and responses linked together and associated with a single client.
+# Information associated with the session is stored
+# on the server between requests. A session id is passed between client
+# and server with every request and response, transparently
+# to the user. This adds state information to the otherwise stateless
+# HTTP request/response protocol.
+#
+# See the documentation to the +CGI::Session+ class for more details
+# and examples of usage. See cgi.rb for the +CGI+ class itself.
+
+require 'cgi'
+require 'tmpdir'
+
+class CGI
+
+ # Class representing an HTTP session. See documentation for the file
+ # cgi/session.rb for an introduction to HTTP sessions.
+ #
+ # == Lifecycle
+ #
+ # A CGI::Session instance is created from a CGI object. By default,
+ # this CGI::Session instance will start a new session if none currently
+ # exists, or continue the current session for this client if one does
+ # exist. The +new_session+ option can be used to either always or
+ # never create a new session. See #new() for more details.
+ #
+ # #delete() deletes a session from session storage. It
+ # does not however remove the session id from the client. If the client
+ # makes another request with the same id, the effect will be to start
+ # a new session with the old session's id.
+ #
+ # == Setting and retrieving session data.
+ #
+ # The Session class associates data with a session as key-value pairs.
+ # This data can be set and retrieved by indexing the Session instance
+ # using '[]', much the same as hashes (although other hash methods
+ # are not supported).
+ #
+ # When session processing has been completed for a request, the
+ # session should be closed using the close() method. This will
+ # store the session's state to persistent storage. If you want
+ # to store the session's state to persistent storage without
+ # finishing session processing for this request, call the update()
+ # method.
+ #
+ # == Storing session state
+ #
+ # The caller can specify what form of storage to use for the session's
+ # data with the +database_manager+ option to CGI::Session::new. The
+ # following storage classes are provided as part of the standard library:
+ #
+ # CGI::Session::FileStore:: stores data as plain text in a flat file. Only
+ # works with String data. This is the default
+ # storage type.
+ # CGI::Session::MemoryStore:: stores data in an in-memory hash. The data
+ # only persists for as long as the current ruby
+ # interpreter instance does.
+ # CGI::Session::PStore:: stores data in Marshalled format. Provided by
+ # cgi/session/pstore.rb. Supports data of any type,
+ # and provides file-locking and transaction support.
+ #
+ # Custom storage types can also be created by defining a class with
+ # the following methods:
+ #
+ # new(session, options)
+ # restore # returns hash of session data.
+ # update
+ # close
+ # delete
+ #
+ # Changing storage type mid-session does not work. Note in particular
+ # that by default the FileStore and PStore session data files have the
+ # same name. If your application switches from one to the other without
+ # making sure that filenames will be different
+ # and clients still have old sessions lying around in cookies, then
+ # things will break nastily!
+ #
+ # == Maintaining the session id.
+ #
+ # Most session state is maintained on the server. However, a session
+ # id must be passed backwards and forwards between client and server
+ # to maintain a reference to this session state.
+ #
+ # The simplest way to do this is via cookies. The CGI::Session class
+ # provides transparent support for session id communication via cookies
+ # if the client has cookies enabled.
+ #
+ # If the client has cookies disabled, the session id must be included
+ # as a parameter of all requests sent by the client to the server. The
+ # CGI::Session class in conjunction with the CGI class will transparently
+ # add the session id as a hidden input field to all forms generated
+ # using the CGI#form() HTML generation method. No built-in support is
+ # provided for other mechanisms, such as URL re-writing. The caller is
+ # responsible for extracting the session id from the session_id
+ # attribute and manually encoding it in URLs and adding it as a hidden
+ # input to HTML forms created by other mechanisms. Also, session expiry
+ # is not automatically handled.
+ #
+ # == Examples of use
+ #
+ # === Setting the user's name
+ #
+ # require 'cgi'
+ # require 'cgi/session'
+ # require 'cgi/session/pstore' # provides CGI::Session::PStore
+ #
+ # cgi = CGI.new("html4")
+ #
+ # session = CGI::Session.new(cgi,
+ # 'database_manager' => CGI::Session::PStore, # use PStore
+ # 'session_key' => '_rb_sess_id', # custom session key
+ # 'session_expires' => Time.now + 30 * 60, # 30 minute timeout
+ # 'prefix' => 'pstore_sid_') # PStore option
+ # if cgi.has_key?('user_name') and cgi['user_name'] != ''
+ # # coerce to String: cgi[] returns the
+ # # string-like CGI::QueryExtension::Value
+ # session['user_name'] = cgi['user_name'].to_s
+ # elsif !session['user_name']
+ # session['user_name'] = "guest"
+ # end
+ # session.close
+ #
+ # === Creating a new session safely
+ #
+ # require 'cgi'
+ # require 'cgi/session'
+ #
+ # cgi = CGI.new("html4")
+ #
+ # # We make sure to delete an old session if one exists,
+ # # not just to free resources, but to prevent the session
+ # # from being maliciously hijacked later on.
+ # begin
+ # session = CGI::Session.new(cgi, 'new_session' => false)
+ # session.delete
+ # rescue ArgumentError # if no old session
+ # end
+ # session = CGI::Session.new(cgi, 'new_session' => true)
+ # session.close
+ #
+ class Session
+
+ class NoSession < RuntimeError #:nodoc:
+ end
+
+ # The id of this session.
+ attr_reader :session_id, :new_session
+
+ def Session::callback(dbman) #:nodoc:
+ Proc.new{
+ dbman[0].close unless dbman.empty?
+ }
+ end
+
+ # Create a new session id.
+ #
+ # The session id is an MD5 hash based upon the time,
+ # a random number, and a constant string. This routine
+ # is used internally for automatically generated
+ # session ids.
+ def create_new_id
+ require 'securerandom'
+ begin
+ session_id = SecureRandom.hex(16)
+ rescue NotImplementedError
+ require 'digest/md5'
+ md5 = Digest::MD5::new
+ now = Time::now
+ md5.update(now.to_s)
+ md5.update(String(now.usec))
+ md5.update(String(rand(0)))
+ md5.update(String($$))
+ md5.update('foobar')
+ session_id = md5.hexdigest
+ end
+ session_id
+ end
+ private :create_new_id
+
+ # Create a new CGI::Session object for +request+.
+ #
+ # +request+ is an instance of the +CGI+ class (see cgi.rb).
+ # +option+ is a hash of options for initialising this
+ # CGI::Session instance. The following options are
+ # recognised:
+ #
+ # session_key:: the parameter name used for the session id.
+ # Defaults to '_session_id'.
+ # session_id:: the session id to use. If not provided, then
+ # it is retrieved from the +session_key+ parameter
+ # of the request, or automatically generated for
+ # a new session.
+ # new_session:: if true, force creation of a new session. If not set,
+ # a new session is only created if none currently
+ # exists. If false, a new session is never created,
+ # and if none currently exists and the +session_id+
+ # option is not set, an ArgumentError is raised.
+ # database_manager:: the name of the class providing storage facilities
+ # for session state persistence. Built-in support
+ # is provided for +FileStore+ (the default),
+ # +MemoryStore+, and +PStore+ (from
+ # cgi/session/pstore.rb). See the documentation for
+ # these classes for more details.
+ #
+ # The following options are also recognised, but only apply if the
+ # session id is stored in a cookie.
+ #
+ # session_expires:: the time the current session expires, as a
+ # +Time+ object. If not set, the session will terminate
+ # when the user's browser is closed.
+ # session_domain:: the hostname domain for which this session is valid.
+ # If not set, defaults to the hostname of the server.
+ # session_secure:: if +true+, this session will only work over HTTPS.
+ # session_path:: the path for which this session applies. Defaults
+ # to the directory of the CGI script.
+ #
+ # +option+ is also passed on to the session storage class initializer; see
+ # the documentation for each session storage class for the options
+ # they support.
+ #
+ # The retrieved or created session is automatically added to +request+
+ # as a cookie, and also to its +output_hidden+ table, which is used
+ # to add hidden input elements to forms.
+ #
+ # *WARNING* the +output_hidden+
+ # fields are surrounded by a <fieldset> tag in HTML 4 generation, which
+ # is _not_ invisible on many browsers; you may wish to disable the
+ # use of fieldsets with code similar to the following
+ # (see http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/37805)
+ #
+ # cgi = CGI.new("html4")
+ # class << cgi
+ # undef_method :fieldset
+ # end
+ #
+ def initialize(request, option={})
+ @new_session = false
+ session_key = option['session_key'] || '_session_id'
+ session_id = option['session_id']
+ unless session_id
+ if option['new_session']
+ session_id = create_new_id
+ @new_session = true
+ end
+ end
+ unless session_id
+ if request.key?(session_key)
+ session_id = request[session_key]
+ session_id = session_id.read if session_id.respond_to?(:read)
+ end
+ unless session_id
+ session_id, = request.cookies[session_key]
+ end
+ unless session_id
+ unless option.fetch('new_session', true)
+ raise ArgumentError, "session_key `%s' should be supplied"%session_key
+ end
+ session_id = create_new_id
+ @new_session = true
+ end
+ end
+ @session_id = session_id
+ dbman = option['database_manager'] || FileStore
+ begin
+ @dbman = dbman::new(self, option)
+ rescue NoSession
+ unless option.fetch('new_session', true)
+ raise ArgumentError, "invalid session_id `%s'"%session_id
+ end
+ session_id = @session_id = create_new_id unless session_id
+ @new_session=true
+ retry
+ end
+ request.instance_eval do
+ @output_hidden = {session_key => session_id} unless option['no_hidden']
+ @output_cookies = [
+ Cookie::new("name" => session_key,
+ "value" => session_id,
+ "expires" => option['session_expires'],
+ "domain" => option['session_domain'],
+ "secure" => option['session_secure'],
+ "path" =>
+ if option['session_path']
+ option['session_path']
+ elsif ENV["SCRIPT_NAME"]
+ File::dirname(ENV["SCRIPT_NAME"])
+ else
+ ""
+ end)
+ ] unless option['no_cookies']
+ end
+ @dbprot = [@dbman]
+ ObjectSpace::define_finalizer(self, Session::callback(@dbprot))
+ end
+
+ # Retrieve the session data for key +key+.
+ def [](key)
+ @data ||= @dbman.restore
+ @data[key]
+ end
+
+ # Set the session date for key +key+.
+ def []=(key, val)
+ @write_lock ||= true
+ @data ||= @dbman.restore
+ @data[key] = val
+ end
+
+ # Store session data on the server. For some session storage types,
+ # this is a no-op.
+ def update
+ @dbman.update
+ end
+
+ # Store session data on the server and close the session storage.
+ # For some session storage types, this is a no-op.
+ def close
+ @dbman.close
+ @dbprot.clear
+ end
+
+ # Delete the session from storage. Also closes the storage.
+ #
+ # Note that the session's data is _not_ automatically deleted
+ # upon the session expiring.
+ def delete
+ @dbman.delete
+ @dbprot.clear
+ end
+
+ # File-based session storage class.
+ #
+ # Implements session storage as a flat file of 'key=value' values.
+ # This storage type only works directly with String values; the
+ # user is responsible for converting other types to Strings when
+ # storing and from Strings when retrieving.
+ class FileStore
+ # Create a new FileStore instance.
+ #
+ # This constructor is used internally by CGI::Session. The
+ # user does not generally need to call it directly.
+ #
+ # +session+ is the session for which this instance is being
+ # created. The session id must only contain alphanumeric
+ # characters; automatically generated session ids observe
+ # this requirement.
+ #
+ # +option+ is a hash of options for the initializer. The
+ # following options are recognised:
+ #
+ # tmpdir:: the directory to use for storing the FileStore
+ # file. Defaults to Dir::tmpdir (generally "/tmp"
+ # on Unix systems).
+ # prefix:: the prefix to add to the session id when generating
+ # the filename for this session's FileStore file.
+ # Defaults to "cgi_sid_".
+ # suffix:: the prefix to add to the session id when generating
+ # the filename for this session's FileStore file.
+ # Defaults to the empty string.
+ #
+ # This session's FileStore file will be created if it does
+ # not exist, or opened if it does.
+ def initialize(session, option={})
+ dir = option['tmpdir'] || Dir::tmpdir
+ prefix = option['prefix'] || 'cgi_sid_'
+ suffix = option['suffix'] || ''
+ id = session.session_id
+ require 'digest/md5'
+ md5 = Digest::MD5.hexdigest(id)[0,16]
+ @path = dir+"/"+prefix+md5+suffix
+ if File::exist? @path
+ @hash = nil
+ else
+ unless session.new_session
+ raise CGI::Session::NoSession, "uninitialized session"
+ end
+ @hash = {}
+ end
+ end
+
+ # Restore session state from the session's FileStore file.
+ #
+ # Returns the session state as a hash.
+ def restore
+ unless @hash
+ @hash = {}
+ begin
+ lockf = File.open(@path+".lock", "r")
+ lockf.flock File::LOCK_SH
+ f = File.open(@path, 'r')
+ for line in f
+ line.chomp!
+ k, v = line.split('=',2)
+ @hash[CGI::unescape(k)] = Marshal.restore(CGI::unescape(v))
+ end
+ ensure
+ f.close unless f.nil?
+ lockf.close if lockf
+ end
+ end
+ @hash
+ end
+
+ # Save session state to the session's FileStore file.
+ def update
+ return unless @hash
+ begin
+ lockf = File.open(@path+".lock", File::CREAT|File::RDWR, 0600)
+ lockf.flock File::LOCK_EX
+ f = File.open(@path+".new", File::CREAT|File::TRUNC|File::WRONLY, 0600)
+ for k,v in @hash
+ f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(Marshal.dump(v)))
+ end
+ f.close
+ File.rename @path+".new", @path
+ ensure
+ f.close if f and !f.closed?
+ lockf.close if lockf
+ end
+ end
+
+ # Update and close the session's FileStore file.
+ def close
+ update
+ end
+
+ # Close and delete the session's FileStore file.
+ def delete
+ File::unlink @path+".lock" rescue nil
+ File::unlink @path+".new" rescue nil
+ File::unlink @path rescue Errno::ENOENT
+ end
+ end
+
+ # In-memory session storage class.
+ #
+ # Implements session storage as a global in-memory hash. Session
+ # data will only persist for as long as the ruby interpreter
+ # instance does.
+ class MemoryStore
+ GLOBAL_HASH_TABLE = {} #:nodoc:
+
+ # Create a new MemoryStore instance.
+ #
+ # +session+ is the session this instance is associated with.
+ # +option+ is a list of initialisation options. None are
+ # currently recognised.
+ def initialize(session, option=nil)
+ @session_id = session.session_id
+ unless GLOBAL_HASH_TABLE.key?(@session_id)
+ unless session.new_session
+ raise CGI::Session::NoSession, "uninitialized session"
+ end
+ GLOBAL_HASH_TABLE[@session_id] = {}
+ end
+ end
+
+ # Restore session state.
+ #
+ # Returns session data as a hash.
+ def restore
+ GLOBAL_HASH_TABLE[@session_id]
+ end
+
+ # Update session state.
+ #
+ # A no-op.
+ def update
+ # don't need to update; hash is shared
+ end
+
+ # Close session storage.
+ #
+ # A no-op.
+ def close
+ # don't need to close
+ end
+
+ # Delete the session state.
+ def delete
+ GLOBAL_HASH_TABLE.delete(@session_id)
+ end
+ end
+
+ # Dummy session storage class.
+ #
+ # Implements session storage place holder. No actual storage
+ # will be done.
+ class NullStore
+ # Create a new NullStore instance.
+ #
+ # +session+ is the session this instance is associated with.
+ # +option+ is a list of initialisation options. None are
+ # currently recognised.
+ def initialize(session, option=nil)
+ end
+
+ # Restore (empty) session state.
+ def restore
+ {}
+ end
+
+ # Update session state.
+ #
+ # A no-op.
+ def update
+ end
+
+ # Close session storage.
+ #
+ # A no-op.
+ def close
+ end
+
+ # Delete the session state.
+ #
+ # A no-op.
+ def delete
+ end
+ end
+ end
+end
diff --git a/ruby/lib/cgi/session/pstore.rb b/ruby/lib/cgi/session/pstore.rb
new file mode 100644
index 0000000..3cd3e46
--- /dev/null
+++ b/ruby/lib/cgi/session/pstore.rb
@@ -0,0 +1,111 @@
+#
+# cgi/session/pstore.rb - persistent storage of marshalled session data
+#
+# Documentation: William Webber (william@williamwebber.com)
+#
+# == Overview
+#
+# This file provides the CGI::Session::PStore class, which builds
+# persistent of session data on top of the pstore library. See
+# cgi/session.rb for more details on session storage managers.
+
+require 'cgi/session'
+require 'pstore'
+
+class CGI
+ class Session
+ # PStore-based session storage class.
+ #
+ # This builds upon the top-level PStore class provided by the
+ # library file pstore.rb. Session data is marshalled and stored
+ # in a file. File locking and transaction services are provided.
+ class PStore
+ # Create a new CGI::Session::PStore instance
+ #
+ # This constructor is used internally by CGI::Session. The
+ # user does not generally need to call it directly.
+ #
+ # +session+ is the session for which this instance is being
+ # created. The session id must only contain alphanumeric
+ # characters; automatically generated session ids observe
+ # this requirement.
+ #
+ # +option+ is a hash of options for the initializer. The
+ # following options are recognised:
+ #
+ # tmpdir:: the directory to use for storing the PStore
+ # file. Defaults to Dir::tmpdir (generally "/tmp"
+ # on Unix systems).
+ # prefix:: the prefix to add to the session id when generating
+ # the filename for this session's PStore file.
+ # Defaults to the empty string.
+ #
+ # This session's PStore file will be created if it does
+ # not exist, or opened if it does.
+ def initialize(session, option={})
+ dir = option['tmpdir'] || Dir::tmpdir
+ prefix = option['prefix'] || ''
+ id = session.session_id
+ require 'digest/md5'
+ md5 = Digest::MD5.hexdigest(id)[0,16]
+ path = dir+"/"+prefix+md5
+ path.untaint
+ if File::exist?(path)
+ @hash = nil
+ else
+ unless session.new_session
+ raise CGI::Session::NoSession, "uninitialized session"
+ end
+ @hash = {}
+ end
+ @p = ::PStore.new(path)
+ @p.transaction do |p|
+ File.chmod(0600, p.path)
+ end
+ end
+
+ # Restore session state from the session's PStore file.
+ #
+ # Returns the session state as a hash.
+ def restore
+ unless @hash
+ @p.transaction do
+ @hash = @p['hash'] || {}
+ end
+ end
+ @hash
+ end
+
+ # Save session state to the session's PStore file.
+ def update
+ @p.transaction do
+ @p['hash'] = @hash
+ end
+ end
+
+ # Update and close the session's PStore file.
+ def close
+ update
+ end
+
+ # Close and delete the session's PStore file.
+ def delete
+ path = @p.path
+ File::unlink path
+ end
+
+ end
+ end
+end
+
+if $0 == __FILE__
+ # :enddoc:
+ STDIN.reopen("/dev/null")
+ cgi = CGI.new
+ session = CGI::Session.new(cgi, 'database_manager' => CGI::Session::PStore)
+ session['key'] = {'k' => 'v'}
+ puts session['key'].class
+ fail unless Hash === session['key']
+ puts session['key'].inspect
+ fail unless session['key'].inspect == '{"k"=>"v"}'
+end
diff --git a/ruby/lib/cgi/util.rb b/ruby/lib/cgi/util.rb
new file mode 100644
index 0000000..991b68c
--- /dev/null
+++ b/ruby/lib/cgi/util.rb
@@ -0,0 +1,181 @@
+class CGI
+ # URL-encode a string.
+ # url_encoded_string = CGI::escape("'Stop!' said Fred")
+ # # => "%27Stop%21%27+said+Fred"
+ def CGI::escape(string)
+ string.gsub(/([^ a-zA-Z0-9_.-]+)/) do
+ '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
+ end.tr(' ', '+')
+ end
+
+
+ # URL-decode a string.
+ # string = CGI::unescape("%27Stop%21%27+said+Fred")
+ # # => "'Stop!' said Fred"
+ def CGI::unescape(string)
+ enc = string.encoding
+ string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) do
+ [$1.delete('%')].pack('H*').force_encoding(enc)
+ end
+ end
+
+ TABLE_FOR_ESCAPE_HTML__ = {
+ '&' => '&amp;',
+ '"' => '&quot;',
+ '<' => '&lt;',
+ '>' => '&gt;',
+ }
+
+ # Escape special characters in HTML, namely &\"<>
+ # CGI::escapeHTML('Usage: foo "bar" <baz>')
+ # # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
+ def CGI::escapeHTML(string)
+ string.gsub(/[&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
+ end
+
+
+ # Unescape a string that has been HTML-escaped
+ # CGI::unescapeHTML("Usage: foo &quot;bar&quot; &lt;baz&gt;")
+ # # => "Usage: foo \"bar\" <baz>"
+ def CGI::unescapeHTML(string)
+ enc = string.encoding
+ if [Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].include?(enc)
+ return string.gsub(Regexp.new('&(amp|quot|gt|lt|#[0-9]+|#x[0-9A-Fa-f]+);'.encode(enc))) do
+ case $1.encode("US-ASCII")
+ when 'amp' then '&'.encode(enc)
+ when 'quot' then '"'.encode(enc)
+ when 'gt' then '>'.encode(enc)
+ when 'lt' then '<'.encode(enc)
+ when /\A#0*(\d+)\z/ then $1.to_i.chr(enc)
+ when /\A#x([0-9a-f]+)\z/i then $1.hex.chr(enc)
+ end
+ end
+ end
+ asciicompat = Encoding.compatible?(string, "a")
+ string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/) do
+ match = $1.dup
+ case match
+ when 'amp' then '&'
+ when 'quot' then '"'
+ when 'gt' then '>'
+ when 'lt' then '<'
+ when /\A#0*(\d+)\z/
+ n = $1.to_i
+ if enc == Encoding::UTF_8 or
+ enc == Encoding::ISO_8859_1 && n < 256 or
+ asciicompat && n < 128
+ n.chr(enc)
+ else
+ "&##{$1};"
+ end
+ when /\A#x([0-9a-f]+)\z/i
+ n = $1.hex
+ if enc == Encoding::UTF_8 or
+ enc == Encoding::ISO_8859_1 && n < 256 or
+ asciicompat && n < 128
+ n.chr(enc)
+ else
+ "&#x#{$1};"
+ end
+ else
+ "&#{match};"
+ end
+ end
+ end
+ def CGI::escape_html(str)
+ escapeHTML(str)
+ end
+ def CGI::unescape_html(str)
+ unescapeHTML(str)
+ end
+
+ # Escape only the tags of certain HTML elements in +string+.
+ #
+ # Takes an element or elements or array of elements. Each element
+ # is specified by the name of the element, without angle brackets.
+ # This matches both the start and the end tag of that element.
+ # The attribute list of the open tag will also be escaped (for
+ # instance, the double-quotes surrounding attribute values).
+ #
+ # print CGI::escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
+ # # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
+ #
+ # print CGI::escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
+ # # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
+ def CGI::escapeElement(string, *elements)
+ elements = elements[0] if elements[0].kind_of?(Array)
+ unless elements.empty?
+ string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/i) do
+ CGI::escapeHTML($&)
+ end
+ else
+ string
+ end
+ end
+
+
+ # Undo escaping such as that done by CGI::escapeElement()
+ #
+ # print CGI::unescapeElement(
+ # CGI::escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
+ # # "&lt;BR&gt;<A HREF="url"></A>"
+ #
+ # print CGI::unescapeElement(
+ # CGI::escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
+ # # "&lt;BR&gt;<A HREF="url"></A>"
+ def CGI::unescapeElement(string, *elements)
+ elements = elements[0] if elements[0].kind_of?(Array)
+ unless elements.empty?
+ string.gsub(/&lt;\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?&gt;/i) do
+ CGI::unescapeHTML($&)
+ end
+ else
+ string
+ end
+ end
+ def CGI::escape_element(str)
+ escapeElement(str)
+ end
+ def CGI::unescape_element(str)
+ unescapeElement(str)
+ end
+
+ # Format a +Time+ object as a String using the format specified by RFC 1123.
+ #
+ # CGI::rfc1123_date(Time.now)
+ # # Sat, 01 Jan 2000 00:00:00 GMT
+ def CGI::rfc1123_date(time)
+ t = time.clone.gmtime
+ return format("%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
+ RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
+ t.hour, t.min, t.sec)
+ end
+
+ # Prettify (indent) an HTML string.
+ #
+ # +string+ is the HTML string to indent. +shift+ is the indentation
+ # unit to use; it defaults to two spaces.
+ #
+ # print CGI::pretty("<HTML><BODY></BODY></HTML>")
+ # # <HTML>
+ # # <BODY>
+ # # </BODY>
+ # # </HTML>
+ #
+ # print CGI::pretty("<HTML><BODY></BODY></HTML>", "\t")
+ # # <HTML>
+ # # <BODY>
+ # # </BODY>
+ # # </HTML>
+ #
+ def CGI::pretty(string, shift = " ")
+ lines = string.gsub(/(?!\A)<(?:.|\n)*?>/, "\n\\0").gsub(/<(?:.|\n)*?>(?!\n)/, "\\0\n")
+ end_pos = 0
+ while end_pos = lines.index(/^<\/(\w+)/, end_pos)
+ element = $1.dup
+ start_pos = lines.rindex(/^\s*<#{element}/i, end_pos)
+ lines[start_pos ... end_pos] = "__" + lines[start_pos ... end_pos].gsub(/\n(?!\z)/, "\n" + shift) + "__"
+ end
+ lines.gsub(/^((?:#{Regexp::quote(shift)})*)__(?=<\/?\w)/, '\1')
+ end
+end
diff --git a/ruby/lib/cmath.rb b/ruby/lib/cmath.rb
new file mode 100644
index 0000000..1b0c65c
--- /dev/null
+++ b/ruby/lib/cmath.rb
@@ -0,0 +1,233 @@
+module CMath
+
+ include Math
+
+ alias exp! exp
+ alias log! log
+ alias log10! log10
+ alias sqrt! sqrt
+
+ alias sin! sin
+ alias cos! cos
+ alias tan! tan
+
+ alias sinh! sinh
+ alias cosh! cosh
+ alias tanh! tanh
+
+ alias asin! asin
+ alias acos! acos
+ alias atan! atan
+ alias atan2! atan2
+
+ alias asinh! asinh
+ alias acosh! acosh
+ alias atanh! atanh
+
+ def exp(z)
+ if z.real?
+ exp!(z)
+ else
+ Complex(exp!(z.real) * cos!(z.imag),
+ exp!(z.real) * sin!(z.imag))
+ end
+ end
+
+ def log(*args)
+ z, b = args
+ if z.real? and z >= 0 and (b.nil? or b >= 0)
+ log!(*args)
+ else
+ r, theta = z.polar
+ a = Complex(log!(r.abs), theta)
+ if b
+ a /= log(b)
+ end
+ a
+ end
+ end
+
+ def log10(z)
+ if z.real? and z >= 0
+ log10!(z)
+ else
+ log(z) / log!(10)
+ end
+ end
+
+ def sqrt(z)
+ if z.real?
+ if z < 0
+ Complex(0, sqrt!(-z))
+ else
+ sqrt!(z)
+ end
+ else
+ if z.imag < 0
+ sqrt(z.conjugate).conjugate
+ else
+ r = z.abs
+ x = z.real
+ Complex(sqrt!((r + x) / 2), sqrt!((r - x) / 2))
+ end
+ end
+ end
+
+ def sin(z)
+ if z.real?
+ sin!(z)
+ else
+ Complex(sin!(z.real) * cosh!(z.imag),
+ cos!(z.real) * sinh!(z.imag))
+ end
+ end
+
+ def cos(z)
+ if z.real?
+ cos!(z)
+ else
+ Complex(cos!(z.real) * cosh!(z.imag),
+ -sin!(z.real) * sinh!(z.imag))
+ end
+ end
+
+ def tan(z)
+ if z.real?
+ tan!(z)
+ else
+ sin(z)/cos(z)
+ end
+ end
+
+ def sinh(z)
+ if z.real?
+ sinh!(z)
+ else
+ Complex(sinh!(z.real) * cos!(z.imag),
+ cosh!(z.real) * sin!(z.imag))
+ end
+ end
+
+ def cosh(z)
+ if z.real?
+ cosh!(z)
+ else
+ Complex(cosh!(z.real) * cos!(z.imag),
+ sinh!(z.real) * sin!(z.imag))
+ end
+ end
+
+ def tanh(z)
+ if z.real?
+ tanh!(z)
+ else
+ sinh(z) / cosh(z)
+ end
+ end
+
+ def asin(z)
+ if z.real? and z >= -1 and z <= 1
+ asin!(z)
+ else
+ Complex(0, -1.0) * log(Complex(0, 1.0) * z + sqrt(1.0 - z * z))
+ end
+ end
+
+ def acos(z)
+ if z.real? and z >= -1 and z <= 1
+ acos!(z)
+ else
+ Complex(0, -1.0) * log(z + Complex(0, 1.0) * sqrt(1.0 - z * z))
+ end
+ end
+
+ def atan(z)
+ if z.real?
+ atan!(z)
+ else
+ Complex(0, 1.0) * log((Complex(0, 1.0) + z) / (Complex(0, 1.0) - z)) / 2.0
+ end
+ end
+
+ def atan2(y,x)
+ if y.real? and x.real?
+ atan2!(y,x)
+ else
+ Complex(0, -1.0) * log((x + Complex(0, 1.0) * y) / sqrt(x * x + y * y))
+ end
+ end
+
+ def acosh(z)
+ if z.real? and z >= 1
+ acosh!(z)
+ else
+ log(z + sqrt(z * z - 1.0))
+ end
+ end
+
+ def asinh(z)
+ if z.real?
+ asinh!(z)
+ else
+ log(z + sqrt(1.0 + z * z))
+ end
+ end
+
+ def atanh(z)
+ if z.real? and z >= -1 and z <= 1
+ atanh!(z)
+ else
+ log((1.0 + z) / (1.0 - z)) / 2.0
+ end
+ end
+
+ module_function :exp!
+ module_function :exp
+ module_function :log!
+ module_function :log
+ module_function :log10!
+ module_function :log10
+ module_function :sqrt!
+ module_function :sqrt
+
+ module_function :sin!
+ module_function :sin
+ module_function :cos!
+ module_function :cos
+ module_function :tan!
+ module_function :tan
+
+ module_function :sinh!
+ module_function :sinh
+ module_function :cosh!
+ module_function :cosh
+ module_function :tanh!
+ module_function :tanh
+
+ module_function :asin!
+ module_function :asin
+ module_function :acos!
+ module_function :acos
+ module_function :atan!
+ module_function :atan
+ module_function :atan2!
+ module_function :atan2
+
+ module_function :asinh!
+ module_function :asinh
+ module_function :acosh!
+ module_function :acosh
+ module_function :atanh!
+ module_function :atanh
+
+ module_function :log2
+ module_function :cbrt
+ module_function :frexp
+ module_function :ldexp
+ module_function :hypot
+ module_function :erf
+ module_function :erfc
+ module_function :gamma
+ module_function :lgamma
+
+end
diff --git a/ruby/lib/complex.rb b/ruby/lib/complex.rb
new file mode 100644
index 0000000..3018791
--- /dev/null
+++ b/ruby/lib/complex.rb
@@ -0,0 +1,24 @@
+require 'cmath'
+
+unless defined?(Math.exp!)
+ Object.instance_eval{remove_const :Math}
+ Math = CMath
+end
+
+def Complex.generic? (other)
+ other.kind_of?(Integer) ||
+ other.kind_of?(Float) ||
+ other.kind_of?(Rational)
+end
+
+class Complex
+
+ alias image imag
+
+end
+
+class Numeric
+
+ def im() Complex(0, self) end
+
+end
diff --git a/ruby/lib/csv.rb b/ruby/lib/csv.rb
new file mode 100644
index 0000000..71ebee8
--- /dev/null
+++ b/ruby/lib/csv.rb
@@ -0,0 +1,2320 @@
+# encoding: US-ASCII
+# = csv.rb -- CSV Reading and Writing
+#
+# Created by James Edward Gray II on 2005-10-31.
+# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
+# under the terms of Ruby's license.
+#
+# See CSV for documentation.
+#
+# == Description
+#
+# Welcome to the new and improved CSV.
+#
+# This version of the CSV library began its life as FasterCSV. FasterCSV was
+# intended as a replacement to Ruby's then standard CSV library. It was
+# designed to address concerns users of that library had and it had three
+# primary goals:
+#
+# 1. Be significantly faster than CSV while remaining a pure Ruby library.
+# 2. Use a smaller and easier to maintain code base. (FasterCSV eventually
+# grew larger, was also but considerably richer in features. The parsing
+# core remains quite small.)
+# 3. Improve on the CSV interface.
+#
+# Obviously, the last one is subjective. I did try to defer to the original
+# interface whenever I didn't have a compelling reason to change it though, so
+# hopefully this won't be too radically different.
+#
+# We must have met our goals because FasterCSV was renamed to CSV and replaced
+# the original library.
+#
+# == What's Different From the Old CSV?
+#
+# I'm sure I'll miss something, but I'll try to mention most of the major
+# differences I am aware of, to help others quickly get up to speed:
+#
+# === CSV Parsing
+#
+# * This parser is m17n aware. See CSV for full details.
+# * This library has a stricter parser and will throw MalformedCSVErrors on
+# problematic data.
+# * This library has a less liberal idea of a line ending than CSV. What you
+# set as the <tt>:row_sep</tt> is law. It can auto-detect your line endings
+# though.
+# * The old library returned empty lines as <tt>[nil]</tt>. This library calls
+# them <tt>[]</tt>.
+# * This library has a much faster parser.
+#
+# === Interface
+#
+# * CSV now uses Hash-style parameters to set options.
+# * CSV no longer has generate_row() or parse_row().
+# * The old CSV's Reader and Writer classes have been dropped.
+# * CSV::open() is now more like Ruby's open().
+# * CSV objects now support most standard IO methods.
+# * CSV now has a new() method used to wrap objects like String and IO for
+# reading and writing.
+# * CSV::generate() is different from the old method.
+# * CSV no longer supports partial reads. It works line-by-line.
+# * CSV no longer allows the instance methods to override the separators for
+# performance reasons. They must be set in the constructor.
+#
+# If you use this library and find yourself missing any functionality I have
+# trimmed, please {let me know}[mailto:james@grayproductions.net].
+#
+# == Documentation
+#
+# See CSV for documentation.
+#
+# == What is CSV, really?
+#
+# CSV maintains a pretty strict definition of CSV taken directly from
+# {the RFC}[http://www.ietf.org/rfc/rfc4180.txt]. I relax the rules in only one
+# place and that is to make using this library easier. CSV will parse all valid
+# CSV.
+#
+# What you don't want to do is feed CSV invalid data. Because of the way the
+# CSV format works, it's common for a parser to need to read until the end of
+# the file to be sure a field is invalid. This eats a lot of time and memory.
+#
+# Luckily, when working with invalid CSV, Ruby's built-in methods will almost
+# always be superior in every way. For example, parsing non-quoted fields is as
+# easy as:
+#
+# data.split(",")
+#
+# == Questions and/or Comments
+#
+# Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net]
+# with any questions.
+
+require "forwardable"
+require "English"
+require "date"
+require "stringio"
+
+#
+# This class provides a complete interface to CSV files and data. It offers
+# tools to enable you to read and write to and from Strings or IO objects, as
+# needed.
+#
+# == Reading
+#
+# === From a File
+#
+# ==== A Line at a Time
+#
+# CSV.foreach("path/to/file.csv") do |row|
+# # use row here...
+# end
+#
+# ==== All at Once
+#
+# arr_of_arrs = CSV.read("path/to/file.csv")
+#
+# === From a String
+#
+# ==== A Line at a Time
+#
+# CSV.parse("CSV,data,String") do |row|
+# # use row here...
+# end
+#
+# ==== All at Once
+#
+# arr_of_arrs = CSV.parse("CSV,data,String")
+#
+# == Writing
+#
+# === To a File
+#
+# CSV.open("path/to/file.csv", "wb") do |csv|
+# csv << ["row", "of", "CSV", "data"]
+# csv << ["another", "row"]
+# # ...
+# end
+#
+# === To a String
+#
+# csv_string = CSV.generate do |csv|
+# csv << ["row", "of", "CSV", "data"]
+# csv << ["another", "row"]
+# # ...
+# end
+#
+# == Convert a Single Line
+#
+# csv_string = ["CSV", "data"].to_csv # to CSV
+# csv_array = "CSV,String".parse_csv # from CSV
+#
+# == Shortcut Interface
+#
+# CSV { |csv_out| csv_out << %w{my data here} } # to $stdout
+# CSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String
+# CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr
+#
+# == CSV and Character Encodings (M17n or Multilingualization)
+#
+# This new CSV parser is m17n savvy. The parser works in the Encoding of the IO
+# or String object being read from or written to. Your data is never transcoded
+# (unless you ask Ruby to transcode it for you) and will literally be parsed in
+# the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the
+# Encoding of your data. This is accomplished by transcoding the parser itself
+# into your Encoding.
+#
+# Some transcoding must take place, of course, to accomplish this multiencoding
+# support. For example, <tt>:col_sep</tt>, <tt>:row_sep</tt>, and
+# <tt>:quote_char</tt> must be transcoded to match your data. Hopefully this
+# makes the entire process feel transparent, since CSV's defaults should just
+# magically work for you data. However, you can set these values manually in
+# the target Encoding to avoid the translation.
+#
+# It's also important to note that while all of CSV's core parser is now
+# Encoding agnostic, some features are not. For example, the built-in
+# converters will try to transcode data to UTF-8 before making conversions.
+# Again, you can provide custom converters that are aware of your Encodings to
+# avoid this translation. It's just too hard for me to support native
+# conversions in all of Ruby's Encodings.
+#
+# Anyway, the practical side of this is simple: make sure IO and String objects
+# passed into CSV have the proper Encoding set and everything should just work.
+# CSV methods that allow you to open IO objects (CSV::foreach(), CSV::open(),
+# CSV::read(), and CSV::readlines()) do allow you to specify the Encoding.
+#
+# One minor exception comes when generating CSV into a String with an Encoding
+# that is not ASCII compatible. There's no existing data for CSV to use to
+# prepare itself and thus you will probably need to manually specify the desired
+# Encoding for most of those cases. It will try to guess using the fields in a
+# row of output though, when using CSV::generate_line() or Array#to_csv().
+#
+# I try to point out any other Encoding issues in the documentation of methods
+# as they come up.
+#
+# This has been tested to the best of my ability with all non-"dummy" Encodings
+# Ruby ships with. However, it is brave new code and may have some bugs.
+# Please feel free to {report}[mailto:james@grayproductions.net] any issues you
+# find with it.
+#
+class CSV
+ # The version of the installed library.
+ VERSION = "2.4.5".freeze
+
+ #
+ # A CSV::Row is part Array and part Hash. It retains an order for the fields
+ # and allows duplicates just as an Array would, but also allows you to access
+ # fields by name just as you could if they were in a Hash.
+ #
+ # All rows returned by CSV will be constructed from this class, if header row
+ # processing is activated.
+ #
+ class Row
+ #
+ # Construct a new CSV::Row from +headers+ and +fields+, which are expected
+ # to be Arrays. If one Array is shorter than the other, it will be padded
+ # with +nil+ objects.
+ #
+ # The optional +header_row+ parameter can be set to +true+ to indicate, via
+ # CSV::Row.header_row?() and CSV::Row.field_row?(), that this is a header
+ # row. Otherwise, the row is assumes to be a field row.
+ #
+ # A CSV::Row object supports the following Array methods through delegation:
+ #
+ # * empty?()
+ # * length()
+ # * size()
+ #
+ def initialize(headers, fields, header_row = false)
+ @header_row = header_row
+
+ # handle extra headers or fields
+ @row = if headers.size > fields.size
+ headers.zip(fields)
+ else
+ fields.zip(headers).map { |pair| pair.reverse }
+ end
+ end
+
+ # Internal data format used to compare equality.
+ attr_reader :row
+ protected :row
+
+ ### Array Delegation ###
+
+ extend Forwardable
+ def_delegators :@row, :empty?, :length, :size
+
+ # Returns +true+ if this is a header row.
+ def header_row?
+ @header_row
+ end
+
+ # Returns +true+ if this is a field row.
+ def field_row?
+ not header_row?
+ end
+
+ # Returns the headers of this row.
+ def headers
+ @row.map { |pair| pair.first }
+ end
+
+ #
+ # :call-seq:
+ # field( header )
+ # field( header, offset )
+ # field( index )
+ #
+ # This method will fetch the field value by +header+ or +index+. If a field
+ # is not found, +nil+ is returned.
+ #
+ # When provided, +offset+ ensures that a header match occurrs on or later
+ # than the +offset+ index. You can use this to find duplicate headers,
+ # without resorting to hard-coding exact indices.
+ #
+ def field(header_or_index, minimum_index = 0)
+ # locate the pair
+ finder = header_or_index.is_a?(Integer) ? :[] : :assoc
+ pair = @row[minimum_index..-1].send(finder, header_or_index)
+
+ # return the field if we have a pair
+ pair.nil? ? nil : pair.last
+ end
+ alias_method :[], :field
+
+ #
+ # :call-seq:
+ # []=( header, value )
+ # []=( header, offset, value )
+ # []=( index, value )
+ #
+ # Looks up the field by the semantics described in CSV::Row.field() and
+ # assigns the +value+.
+ #
+ # Assigning past the end of the row with an index will set all pairs between
+ # to <tt>[nil, nil]</tt>. Assigning to an unused header appends the new
+ # pair.
+ #
+ def []=(*args)
+ value = args.pop
+
+ if args.first.is_a? Integer
+ if @row[args.first].nil? # extending past the end with index
+ @row[args.first] = [nil, value]
+ @row.map! { |pair| pair.nil? ? [nil, nil] : pair }
+ else # normal index assignment
+ @row[args.first][1] = value
+ end
+ else
+ index = index(*args)
+ if index.nil? # appending a field
+ self << [args.first, value]
+ else # normal header assignment
+ @row[index][1] = value
+ end
+ end
+ end
+
+ #
+ # :call-seq:
+ # <<( field )
+ # <<( header_and_field_array )
+ # <<( header_and_field_hash )
+ #
+ # If a two-element Array is provided, it is assumed to be a header and field
+ # and the pair is appended. A Hash works the same way with the key being
+ # the header and the value being the field. Anything else is assumed to be
+ # a lone field which is appended with a +nil+ header.
+ #
+ # This method returns the row for chaining.
+ #
+ def <<(arg)
+ if arg.is_a?(Array) and arg.size == 2 # appending a header and name
+ @row << arg
+ elsif arg.is_a?(Hash) # append header and name pairs
+ arg.each { |pair| @row << pair }
+ else # append field value
+ @row << [nil, arg]
+ end
+
+ self # for chaining
+ end
+
+ #
+ # A shortcut for appending multiple fields. Equivalent to:
+ #
+ # args.each { |arg| csv_row << arg }
+ #
+ # This method returns the row for chaining.
+ #
+ def push(*args)
+ args.each { |arg| self << arg }
+
+ self # for chaining
+ end
+
+ #
+ # :call-seq:
+ # delete( header )
+ # delete( header, offset )
+ # delete( index )
+ #
+ # Used to remove a pair from the row by +header+ or +index+. The pair is
+ # located as described in CSV::Row.field(). The deleted pair is returned,
+ # or +nil+ if a pair could not be found.
+ #
+ def delete(header_or_index, minimum_index = 0)
+ if header_or_index.is_a? Integer # by index
+ @row.delete_at(header_or_index)
+ else # by header
+ @row.delete_at(index(header_or_index, minimum_index))
+ end
+ end
+
+ #
+ # The provided +block+ is passed a header and field for each pair in the row
+ # and expected to return +true+ or +false+, depending on whether the pair
+ # should be deleted.
+ #
+ # This method returns the row for chaining.
+ #
+ def delete_if(&block)
+ @row.delete_if(&block)
+
+ self # for chaining
+ end
+
+ #
+ # This method accepts any number of arguments which can be headers, indices,
+ # Ranges of either, or two-element Arrays containing a header and offset.
+ # Each argument will be replaced with a field lookup as described in
+ # CSV::Row.field().
+ #
+ # If called with no arguments, all fields are returned.
+ #
+ def fields(*headers_and_or_indices)
+ if headers_and_or_indices.empty? # return all fields--no arguments
+ @row.map { |pair| pair.last }
+ else # or work like values_at()
+ headers_and_or_indices.inject(Array.new) do |all, h_or_i|
+ all + if h_or_i.is_a? Range
+ index_begin = h_or_i.begin.is_a?(Integer) ? h_or_i.begin :
+ index(h_or_i.begin)
+ index_end = h_or_i.end.is_a?(Integer) ? h_or_i.end :
+ index(h_or_i.end)
+ new_range = h_or_i.exclude_end? ? (index_begin...index_end) :
+ (index_begin..index_end)
+ fields.values_at(new_range)
+ else
+ [field(*Array(h_or_i))]
+ end
+ end
+ end
+ end
+ alias_method :values_at, :fields
+
+ #
+ # :call-seq:
+ # index( header )
+ # index( header, offset )
+ #
+ # This method will return the index of a field with the provided +header+.
+ # The +offset+ can be used to locate duplicate header names, as described in
+ # CSV::Row.field().
+ #
+ def index(header, minimum_index = 0)
+ # find the pair
+ index = headers[minimum_index..-1].index(header)
+ # return the index at the right offset, if we found one
+ index.nil? ? nil : index + minimum_index
+ end
+
+ # Returns +true+ if +name+ is a header for this row, and +false+ otherwise.
+ def header?(name)
+ headers.include? name
+ end
+ alias_method :include?, :header?
+
+ #
+ # Returns +true+ if +data+ matches a field in this row, and +false+
+ # otherwise.
+ #
+ def field?(data)
+ fields.include? data
+ end
+
+ include Enumerable
+
+ #
+ # Yields each pair of the row as header and field tuples (much like
+ # iterating over a Hash).
+ #
+ # Support for Enumerable.
+ #
+ # This method returns the row for chaining.
+ #
+ def each(&block)
+ @row.each(&block)
+
+ self # for chaining
+ end
+
+ #
+ # Returns +true+ if this row contains the same headers and fields in the
+ # same order as +other+.
+ #
+ def ==(other)
+ @row == other.row
+ end
+
+ #
+ # Collapses the row into a simple Hash. Be warning that this discards field
+ # order and clobbers duplicate fields.
+ #
+ def to_hash
+ # flatten just one level of the internal Array
+ Hash[*@row.inject(Array.new) { |ary, pair| ary.push(*pair) }]
+ end
+
+ #
+ # Returns the row as a CSV String. Headers are not used. Equivalent to:
+ #
+ # csv_row.fields.to_csv( options )
+ #
+ def to_csv(options = Hash.new)
+ fields.to_csv(options)
+ end
+ alias_method :to_s, :to_csv
+
+ # A summary of fields, by header, in an ASCII compatible String.
+ def inspect
+ str = ["#<", self.class.to_s]
+ each do |header, field|
+ str << " " << (header.is_a?(Symbol) ? header.to_s : header.inspect) <<
+ ":" << field.inspect
+ end
+ str << ">"
+ begin
+ str.join
+ rescue # any encoding error
+ str.map do |s|
+ e = Encoding::Converter.asciicompat_encoding(s.encoding)
+ e ? s.encode(e) : s.force_encoding("ASCII-8BIT")
+ end.join
+ end
+ end
+ end
+
+ #
+ # A CSV::Table is a two-dimensional data structure for representing CSV
+ # documents. Tables allow you to work with the data by row or column,
+ # manipulate the data, and even convert the results back to CSV, if needed.
+ #
+ # All tables returned by CSV will be constructed from this class, if header
+ # row processing is activated.
+ #
+ class Table
+ #
+ # Construct a new CSV::Table from +array_of_rows+, which are expected
+ # to be CSV::Row objects. All rows are assumed to have the same headers.
+ #
+ # A CSV::Table object supports the following Array methods through
+ # delegation:
+ #
+ # * empty?()
+ # * length()
+ # * size()
+ #
+ def initialize(array_of_rows)
+ @table = array_of_rows
+ @mode = :col_or_row
+ end
+
+ # The current access mode for indexing and iteration.
+ attr_reader :mode
+
+ # Internal data format used to compare equality.
+ attr_reader :table
+ protected :table
+
+ ### Array Delegation ###
+
+ extend Forwardable
+ def_delegators :@table, :empty?, :length, :size
+
+ #
+ # Returns a duplicate table object, in column mode. This is handy for
+ # chaining in a single call without changing the table mode, but be aware
+ # that this method can consume a fair amount of memory for bigger data sets.
+ #
+ # This method returns the duplicate table for chaining. Don't chain
+ # destructive methods (like []=()) this way though, since you are working
+ # with a duplicate.
+ #
+ def by_col
+ self.class.new(@table.dup).by_col!
+ end
+
+ #
+ # Switches the mode of this table to column mode. All calls to indexing and
+ # iteration methods will work with columns until the mode is changed again.
+ #
+ # This method returns the table and is safe to chain.
+ #
+ def by_col!
+ @mode = :col
+
+ self
+ end
+
+ #
+ # Returns a duplicate table object, in mixed mode. This is handy for
+ # chaining in a single call without changing the table mode, but be aware
+ # that this method can consume a fair amount of memory for bigger data sets.
+ #
+ # This method returns the duplicate table for chaining. Don't chain
+ # destructive methods (like []=()) this way though, since you are working
+ # with a duplicate.
+ #
+ def by_col_or_row
+ self.class.new(@table.dup).by_col_or_row!
+ end
+
+ #
+ # Switches the mode of this table to mixed mode. All calls to indexing and
+ # iteration methods will use the default intelligent indexing system until
+ # the mode is changed again. In mixed mode an index is assumed to be a row
+ # reference while anything else is assumed to be column access by headers.
+ #
+ # This method returns the table and is safe to chain.
+ #
+ def by_col_or_row!
+ @mode = :col_or_row
+
+ self
+ end
+
+ #
+ # Returns a duplicate table object, in row mode. This is handy for chaining
+ # in a single call without changing the table mode, but be aware that this
+ # method can consume a fair amount of memory for bigger data sets.
+ #
+ # This method returns the duplicate table for chaining. Don't chain
+ # destructive methods (like []=()) this way though, since you are working
+ # with a duplicate.
+ #
+ def by_row
+ self.class.new(@table.dup).by_row!
+ end
+
+ #
+ # Switches the mode of this table to row mode. All calls to indexing and
+ # iteration methods will work with rows until the mode is changed again.
+ #
+ # This method returns the table and is safe to chain.
+ #
+ def by_row!
+ @mode = :row
+
+ self
+ end
+
+ #
+ # Returns the headers for the first row of this table (assumed to match all
+ # other rows). An empty Array is returned for empty tables.
+ #
+ def headers
+ if @table.empty?
+ Array.new
+ else
+ @table.first.headers
+ end
+ end
+
+ #
+ # In the default mixed mode, this method returns rows for index access and
+ # columns for header access. You can force the index association by first
+ # calling by_col!() or by_row!().
+ #
+ # Columns are returned as an Array of values. Altering that Array has no
+ # effect on the table.
+ #
+ def [](index_or_header)
+ if @mode == :row or # by index
+ (@mode == :col_or_row and index_or_header.is_a? Integer)
+ @table[index_or_header]
+ else # by header
+ @table.map { |row| row[index_or_header] }
+ end
+ end
+
+ #
+ # In the default mixed mode, this method assigns rows for index access and
+ # columns for header access. You can force the index association by first
+ # calling by_col!() or by_row!().
+ #
+ # Rows may be set to an Array of values (which will inherit the table's
+ # headers()) or a CSV::Row.
+ #
+ # Columns may be set to a single value, which is copied to each row of the
+ # column, or an Array of values. Arrays of values are assigned to rows top
+ # to bottom in row major order. Excess values are ignored and if the Array
+ # does not have a value for each row the extra rows will receive a +nil+.
+ #
+ # Assigning to an existing column or row clobbers the data. Assigning to
+ # new columns creates them at the right end of the table.
+ #
+ def []=(index_or_header, value)
+ if @mode == :row or # by index
+ (@mode == :col_or_row and index_or_header.is_a? Integer)
+ if value.is_a? Array
+ @table[index_or_header] = Row.new(headers, value)
+ else
+ @table[index_or_header] = value
+ end
+ else # set column
+ if value.is_a? Array # multiple values
+ @table.each_with_index do |row, i|
+ if row.header_row?
+ row[index_or_header] = index_or_header
+ else
+ row[index_or_header] = value[i]
+ end
+ end
+ else # repeated value
+ @table.each do |row|
+ if row.header_row?
+ row[index_or_header] = index_or_header
+ else
+ row[index_or_header] = value
+ end
+ end
+ end
+ end
+ end
+
+ #
+ # The mixed mode default is to treat a list of indices as row access,
+ # returning the rows indicated. Anything else is considered columnar
+ # access. For columnar access, the return set has an Array for each row
+ # with the values indicated by the headers in each Array. You can force
+ # column or row mode using by_col!() or by_row!().
+ #
+ # You cannot mix column and row access.
+ #
+ def values_at(*indices_or_headers)
+ if @mode == :row or # by indices
+ ( @mode == :col_or_row and indices_or_headers.all? do |index|
+ index.is_a?(Integer) or
+ ( index.is_a?(Range) and
+ index.first.is_a?(Integer) and
+ index.last.is_a?(Integer) )
+ end )
+ @table.values_at(*indices_or_headers)
+ else # by headers
+ @table.map { |row| row.values_at(*indices_or_headers) }
+ end
+ end
+
+ #
+ # Adds a new row to the bottom end of this table. You can provide an Array,
+ # which will be converted to a CSV::Row (inheriting the table's headers()),
+ # or a CSV::Row.
+ #
+ # This method returns the table for chaining.
+ #
+ def <<(row_or_array)
+ if row_or_array.is_a? Array # append Array
+ @table << Row.new(headers, row_or_array)
+ else # append Row
+ @table << row_or_array
+ end
+
+ self # for chaining
+ end
+
+ #
+ # A shortcut for appending multiple rows. Equivalent to:
+ #
+ # rows.each { |row| self << row }
+ #
+ # This method returns the table for chaining.
+ #
+ def push(*rows)
+ rows.each { |row| self << row }
+
+ self # for chaining
+ end
+
+ #
+ # Removes and returns the indicated column or row. In the default mixed
+ # mode indices refer to rows and everything else is assumed to be a column
+ # header. Use by_col!() or by_row!() to force the lookup.
+ #
+ def delete(index_or_header)
+ if @mode == :row or # by index
+ (@mode == :col_or_row and index_or_header.is_a? Integer)
+ @table.delete_at(index_or_header)
+ else # by header
+ @table.map { |row| row.delete(index_or_header).last }
+ end
+ end
+
+ #
+ # Removes any column or row for which the block returns +true+. In the
+ # default mixed mode or row mode, iteration is the standard row major
+ # walking of rows. In column mode, interation will +yield+ two element
+ # tuples containing the column name and an Array of values for that column.
+ #
+ # This method returns the table for chaining.
+ #
+ def delete_if(&block)
+ if @mode == :row or @mode == :col_or_row # by index
+ @table.delete_if(&block)
+ else # by header
+ to_delete = Array.new
+ headers.each_with_index do |header, i|
+ to_delete << header if block[[header, self[header]]]
+ end
+ to_delete.map { |header| delete(header) }
+ end
+
+ self # for chaining
+ end
+
+ include Enumerable
+
+ #
+ # In the default mixed mode or row mode, iteration is the standard row major
+ # walking of rows. In column mode, interation will +yield+ two element
+ # tuples containing the column name and an Array of values for that column.
+ #
+ # This method returns the table for chaining.
+ #
+ def each(&block)
+ if @mode == :col
+ headers.each { |header| block[[header, self[header]]] }
+ else
+ @table.each(&block)
+ end
+
+ self # for chaining
+ end
+
+ # Returns +true+ if all rows of this table ==() +other+'s rows.
+ def ==(other)
+ @table == other.table
+ end
+
+ #
+ # Returns the table as an Array of Arrays. Headers will be the first row,
+ # then all of the field rows will follow.
+ #
+ def to_a
+ @table.inject([headers]) do |array, row|
+ if row.header_row?
+ array
+ else
+ array + [row.fields]
+ end
+ end
+ end
+
+ #
+ # Returns the table as a complete CSV String. Headers will be listed first,
+ # then all of the field rows.
+ #
+ def to_csv(options = Hash.new)
+ @table.inject([headers.to_csv(options)]) do |rows, row|
+ if row.header_row?
+ rows
+ else
+ rows + [row.fields.to_csv(options)]
+ end
+ end.join
+ end
+ alias_method :to_s, :to_csv
+
+ # Shows the mode and size of this table in a US-ASCII String.
+ def inspect
+ "#<#{self.class} mode:#{@mode} row_count:#{to_a.size}>".encode("US-ASCII")
+ end
+ end
+
+ # The error thrown when the parser encounters illegal CSV formatting.
+ class MalformedCSVError < RuntimeError; end
+
+ #
+ # A FieldInfo Struct contains details about a field's position in the data
+ # source it was read from. CSV will pass this Struct to some blocks that make
+ # decisions based on field structure. See CSV.convert_fields() for an
+ # example.
+ #
+ # <b><tt>index</tt></b>:: The zero-based index of the field in its row.
+ # <b><tt>line</tt></b>:: The line of the data source this row is from.
+ # <b><tt>header</tt></b>:: The header for the column, when available.
+ #
+ FieldInfo = Struct.new(:index, :line, :header)
+
+ # A Regexp used to find and convert some common Date formats.
+ DateMatcher = / \A(?: (\w+,?\s+)?\w+\s+\d{1,2},?\s+\d{2,4} |
+ \d{4}-\d{2}-\d{2} )\z /x
+ # A Regexp used to find and convert some common DateTime formats.
+ DateTimeMatcher =
+ / \A(?: (\w+,?\s+)?\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2},?\s+\d{2,4} |
+ \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2} )\z /x
+
+ # The encoding used by all converters.
+ ConverterEncoding = Encoding.find("UTF-8")
+
+ #
+ # This Hash holds the built-in converters of CSV that can be accessed by name.
+ # You can select Converters with CSV.convert() or through the +options+ Hash
+ # passed to CSV::new().
+ #
+ # <b><tt>:integer</tt></b>:: Converts any field Integer() accepts.
+ # <b><tt>:float</tt></b>:: Converts any field Float() accepts.
+ # <b><tt>:numeric</tt></b>:: A combination of <tt>:integer</tt>
+ # and <tt>:float</tt>.
+ # <b><tt>:date</tt></b>:: Converts any field Date::parse() accepts.
+ # <b><tt>:date_time</tt></b>:: Converts any field DateTime::parse() accepts.
+ # <b><tt>:all</tt></b>:: All built-in converters. A combination of
+ # <tt>:date_time</tt> and <tt>:numeric</tt>.
+ #
+ # All built-in converters transcode field data to UTF-8 before attempting a
+ # conversion. If your data cannot be transcoded to UTF-8 the conversion will
+ # fail and the field will remain unchanged.
+ #
+ # This Hash is intentionally left unfrozen and users should feel free to add
+ # values to it that can be accessed by all CSV objects.
+ #
+ # To add a combo field, the value should be an Array of names. Combo fields
+ # can be nested with other combo fields.
+ #
+ Converters = { integer: lambda { |f|
+ Integer(f.encode(ConverterEncoding)) rescue f
+ },
+ float: lambda { |f|
+ Float(f.encode(ConverterEncoding)) rescue f
+ },
+ numeric: [:integer, :float],
+ date: lambda { |f|
+ begin
+ e = f.encode(ConverterEncoding)
+ e =~ DateMatcher ? Date.parse(e) : f
+ rescue # encoding conversion or date parse errors
+ f
+ end
+ },
+ date_time: lambda { |f|
+ begin
+ e = f.encode(ConverterEncoding)
+ e =~ DateTimeMatcher ? DateTime.parse(e) : f
+ rescue # encoding conversion or date parse errors
+ f
+ end
+ },
+ all: [:date_time, :numeric] }
+
+ #
+ # This Hash holds the built-in header converters of CSV that can be accessed
+ # by name. You can select HeaderConverters with CSV.header_convert() or
+ # through the +options+ Hash passed to CSV::new().
+ #
+ # <b><tt>:downcase</tt></b>:: Calls downcase() on the header String.
+ # <b><tt>:symbol</tt></b>:: The header String is downcased, spaces are
+ # replaced with underscores, non-word characters
+ # are dropped, and finally to_sym() is called.
+ #
+ # All built-in header converters transcode header data to UTF-8 before
+ # attempting a conversion. If your data cannot be transcoded to UTF-8 the
+ # conversion will fail and the header will remain unchanged.
+ #
+ # This Hash is intetionally left unfrozen and users should feel free to add
+ # values to it that can be accessed by all CSV objects.
+ #
+ # To add a combo field, the value should be an Array of names. Combo fields
+ # can be nested with other combo fields.
+ #
+ HeaderConverters = {
+ downcase: lambda { |h| h.encode(ConverterEncoding).downcase },
+ symbol: lambda { |h|
+ h.encode(ConverterEncoding).downcase.gsub(/\s+/, "_").
+ gsub(/\W+/, "").to_sym
+ }
+ }
+
+ #
+ # The options used when no overrides are given by calling code. They are:
+ #
+ # <b><tt>:col_sep</tt></b>:: <tt>","</tt>
+ # <b><tt>:row_sep</tt></b>:: <tt>:auto</tt>
+ # <b><tt>:quote_char</tt></b>:: <tt>'"'</tt>
+ # <b><tt>:field_size_limit</tt></b>:: +nil+
+ # <b><tt>:converters</tt></b>:: +nil+
+ # <b><tt>:unconverted_fields</tt></b>:: +nil+
+ # <b><tt>:headers</tt></b>:: +false+
+ # <b><tt>:return_headers</tt></b>:: +false+
+ # <b><tt>:header_converters</tt></b>:: +nil+
+ # <b><tt>:skip_blanks</tt></b>:: +false+
+ # <b><tt>:force_quotes</tt></b>:: +false+
+ #
+ DEFAULT_OPTIONS = { col_sep: ",",
+ row_sep: :auto,
+ quote_char: '"',
+ field_size_limit: nil,
+ converters: nil,
+ unconverted_fields: nil,
+ headers: false,
+ return_headers: false,
+ header_converters: nil,
+ skip_blanks: false,
+ force_quotes: false }.freeze
+
+ #
+ # This method will return a CSV instance, just like CSV::new(), but the
+ # instance will be cached and returned for all future calls to this method for
+ # the same +data+ object (tested by Object#object_id()) with the same
+ # +options+.
+ #
+ # If a block is given, the instance is passed to the block and the return
+ # value becomes the return value of the block.
+ #
+ def self.instance(data = $stdout, options = Hash.new)
+ # create a _signature_ for this method call, data object and options
+ sig = [data.object_id] +
+ options.values_at(*DEFAULT_OPTIONS.keys.sort_by { |sym| sym.to_s })
+
+ # fetch or create the instance for this signature
+ @@instances ||= Hash.new
+ instance = (@@instances[sig] ||= new(data, options))
+
+ if block_given?
+ yield instance # run block, if given, returning result
+ else
+ instance # or return the instance
+ end
+ end
+
+ #
+ # This method allows you to serialize an Array of Ruby objects to a String or
+ # File of CSV data. This is not as powerful as Marshal or YAML, but perhaps
+ # useful for spreadsheet and database interaction.
+ #
+ # Out of the box, this method is intended to work with simple data objects or
+ # Structs. It will serialize a list of instance variables and/or
+ # Struct.members().
+ #
+ # If you need need more complicated serialization, you can control the process
+ # by adding methods to the class to be serialized.
+ #
+ # A class method csv_meta() is responsible for returning the first row of the
+ # document (as an Array). This row is considered to be a Hash of the form
+ # key_1,value_1,key_2,value_2,... CSV::load() expects to find a class key
+ # with a value of the stringified class name and CSV::dump() will create this,
+ # if you do not define this method. This method is only called on the first
+ # object of the Array.
+ #
+ # The next method you can provide is an instance method called csv_headers().
+ # This method is expected to return the second line of the document (again as
+ # an Array), which is to be used to give each column a header. By default,
+ # CSV::load() will set an instance variable if the field header starts with an
+ # @ character or call send() passing the header as the method name and
+ # the field value as an argument. This method is only called on the first
+ # object of the Array.
+ #
+ # Finally, you can provide an instance method called csv_dump(), which will
+ # be passed the headers. This should return an Array of fields that can be
+ # serialized for this object. This method is called once for every object in
+ # the Array.
+ #
+ # The +io+ parameter can be used to serialize to a File, and +options+ can be
+ # anything CSV::new() accepts.
+ #
+ def self.dump(ary_of_objs, io = "", options = Hash.new)
+ obj_template = ary_of_objs.first
+
+ csv = new(io, options)
+
+ # write meta information
+ begin
+ csv << obj_template.class.csv_meta
+ rescue NoMethodError
+ csv << [:class, obj_template.class]
+ end
+
+ # write headers
+ begin
+ headers = obj_template.csv_headers
+ rescue NoMethodError
+ headers = obj_template.instance_variables.sort
+ if obj_template.class.ancestors.find { |cls| cls.to_s =~ /\AStruct\b/ }
+ headers += obj_template.members.map { |mem| "#{mem}=" }.sort
+ end
+ end
+ csv << headers
+
+ # serialize each object
+ ary_of_objs.each do |obj|
+ begin
+ csv << obj.csv_dump(headers)
+ rescue NoMethodError
+ csv << headers.map do |var|
+ if var[0] == ?@
+ obj.instance_variable_get(var)
+ else
+ obj[var[0..-2]]
+ end
+ end
+ end
+ end
+
+ if io.is_a? String
+ csv.string
+ else
+ csv.close
+ end
+ end
+
+ #
+ # This method is the reading counterpart to CSV::dump(). See that method for
+ # a detailed description of the process.
+ #
+ # You can customize loading by adding a class method called csv_load() which
+ # will be passed a Hash of meta information, an Array of headers, and an Array
+ # of fields for the object the method is expected to return.
+ #
+ # Remember that all fields will be Strings after this load. If you need
+ # something else, use +options+ to setup converters or provide a custom
+ # csv_load() implementation.
+ #
+ def self.load(io_or_str, options = Hash.new)
+ csv = new(io_or_str, options)
+
+ # load meta information
+ meta = Hash[*csv.shift]
+ cls = meta["class".encode(csv.encoding)].split("::".encode(csv.encoding)).
+ inject(Object) do |c, const|
+ c.const_get(const)
+ end
+
+ # load headers
+ headers = csv.shift
+
+ # unserialize each object stored in the file
+ results = csv.inject(Array.new) do |all, row|
+ begin
+ obj = cls.csv_load(meta, headers, row)
+ rescue NoMethodError
+ obj = cls.allocate
+ headers.zip(row) do |name, value|
+ if name[0] == ?@
+ obj.instance_variable_set(name, value)
+ else
+ obj.send(name, value)
+ end
+ end
+ end
+ all << obj
+ end
+
+ csv.close unless io_or_str.is_a? String
+
+ results
+ end
+
+ #
+ # :call-seq:
+ # filter( options = Hash.new ) { |row| ... }
+ # filter( input, options = Hash.new ) { |row| ... }
+ # filter( input, output, options = Hash.new ) { |row| ... }
+ #
+ # This method is a convenience for building Unix-like filters for CSV data.
+ # Each row is yielded to the provided block which can alter it as needed.
+ # After the block returns, the row is appended to +output+ altered or not.
+ #
+ # The +input+ and +output+ arguments can be anything CSV::new() accepts
+ # (generally String or IO objects). If not given, they default to
+ # <tt>ARGF</tt> and <tt>$stdout</tt>.
+ #
+ # The +options+ parameter is also filtered down to CSV::new() after some
+ # clever key parsing. Any key beginning with <tt>:in_</tt> or
+ # <tt>:input_</tt> will have that leading identifier stripped and will only
+ # be used in the +options+ Hash for the +input+ object. Keys starting with
+ # <tt>:out_</tt> or <tt>:output_</tt> affect only +output+. All other keys
+ # are assigned to both objects.
+ #
+ # The <tt>:output_row_sep</tt> +option+ defaults to
+ # <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>).
+ #
+ def self.filter(*args)
+ # parse options for input, output, or both
+ in_options, out_options = Hash.new, {row_sep: $INPUT_RECORD_SEPARATOR}
+ if args.last.is_a? Hash
+ args.pop.each do |key, value|
+ case key.to_s
+ when /\Ain(?:put)?_(.+)\Z/
+ in_options[$1.to_sym] = value
+ when /\Aout(?:put)?_(.+)\Z/
+ out_options[$1.to_sym] = value
+ else
+ in_options[key] = value
+ out_options[key] = value
+ end
+ end
+ end
+ # build input and output wrappers
+ input = new(args.shift || ARGF, in_options)
+ output = new(args.shift || $stdout, out_options)
+
+ # read, yield, write
+ input.each do |row|
+ yield row
+ output << row
+ end
+ end
+
+ #
+ # This method is intended as the primary interface for reading CSV files. You
+ # pass a +path+ and any +options+ you wish to set for the read. Each row of
+ # file will be passed to the provided +block+ in turn.
+ #
+ # The +options+ parameter can be anything CSV::new() understands. This method
+ # also understands an additional <tt>:encoding</tt> parameter that you can use
+ # to specify the Encoding of the data in the file to be read. You must provide
+ # this unless your data is in Encoding::default_external(). CSV will use this
+ # to deterime how to parse the data. You may provide a second Encoding to
+ # have the data transcoded as it is read. For example,
+ # <tt>encoding: "UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the file
+ # but transcode it to UTF-8 before CSV parses it.
+ #
+ def self.foreach(path, options = Hash.new, &block)
+ encoding = options.delete(:encoding)
+ mode = "rb"
+ mode << ":#{encoding}" if encoding
+ open(path, mode, options) do |csv|
+ csv.each(&block)
+ end
+ end
+
+ #
+ # :call-seq:
+ # generate( str, options = Hash.new ) { |csv| ... }
+ # generate( options = Hash.new ) { |csv| ... }
+ #
+ # This method wraps a String you provide, or an empty default String, in a
+ # CSV object which is passed to the provided block. You can use the block to
+ # append CSV rows to the String and when the block exits, the final String
+ # will be returned.
+ #
+ # Note that a passed String *is* modfied by this method. Call dup() before
+ # passing if you need a new String.
+ #
+ # The +options+ parameter can be anthing CSV::new() understands. This method
+ # understands an additional <tt>:encoding</tt> parameter when not passed a
+ # String to set the base Encoding for the output. CSV needs this hint if you
+ # plan to output non-ASCII compatible data.
+ #
+ def self.generate(*args)
+ # add a default empty String, if none was given
+ if args.first.is_a? String
+ io = StringIO.new(args.shift)
+ io.seek(0, IO::SEEK_END)
+ args.unshift(io)
+ else
+ encoding = args.last.is_a?(Hash) ? args.last.delete(:encoding) : nil
+ str = ""
+ str.encode!(encoding) if encoding
+ args.unshift(str)
+ end
+ csv = new(*args) # wrap
+ yield csv # yield for appending
+ csv.string # return final String
+ end
+
+ #
+ # This method is a shortcut for converting a single row (Array) into a CSV
+ # String.
+ #
+ # The +options+ parameter can be anthing CSV::new() understands. This method
+ # understands an additional <tt>:encoding</tt> parameter to set the base
+ # Encoding for the output. This method will try to guess your Encoding from
+ # the first non-+nil+ field in +row+, if possible, but you may need to use
+ # this parameter as a backup plan.
+ #
+ # The <tt>:row_sep</tt> +option+ defaults to <tt>$INPUT_RECORD_SEPARATOR</tt>
+ # (<tt>$/</tt>) when calling this method.
+ #
+ def self.generate_line(row, options = Hash.new)
+ options = {row_sep: $INPUT_RECORD_SEPARATOR}.merge(options)
+ encoding = options.delete(:encoding)
+ str = ""
+ if encoding
+ str.force_encoding(encoding)
+ elsif field = row.find { |f| not f.nil? }
+ str.force_encoding(String(field).encoding)
+ end
+ (new(str, options) << row).string
+ end
+
+ #
+ # :call-seq:
+ # open( filename, mode = "rb", options = Hash.new ) { |faster_csv| ... }
+ # open( filename, options = Hash.new ) { |faster_csv| ... }
+ # open( filename, mode = "rb", options = Hash.new )
+ # open( filename, options = Hash.new )
+ #
+ # This method opens an IO object, and wraps that with CSV. This is intended
+ # as the primary interface for writing a CSV file.
+ #
+ # You must pass a +filename+ and may optionally add a +mode+ for Ruby's
+ # open(). You may also pass an optional Hash containing any +options+
+ # CSV::new() understands as the final argument.
+ #
+ # This method works like Ruby's open() call, in that it will pass a CSV object
+ # to a provided block and close it when the block terminates, or it will
+ # return the CSV object when no block is provided. (*Note*: This is different
+ # from the Ruby 1.8 CSV library which passed rows to the block. Use
+ # CSV::foreach() for that behavior.)
+ #
+ # You must provide a +mode+ with an embedded Encoding designator unless your
+ # data is in Encoding::default_external(). CSV will check the Encoding of the
+ # underlying IO object (set by the +mode+ you pass) to deterime how to parse
+ # the data. You may provide a second Encoding to have the data transcoded as
+ # it is read just as you can with a normal call to IO::open(). For example,
+ # <tt>"rb:UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the file but
+ # transcode it to UTF-8 before CSV parses it.
+ #
+ # An opened CSV object will delegate to many IO methods for convenience. You
+ # may call:
+ #
+ # * binmode()
+ # * binmode?()
+ # * close()
+ # * close_read()
+ # * close_write()
+ # * closed?()
+ # * eof()
+ # * eof?()
+ # * external_encoding()
+ # * fcntl()
+ # * fileno()
+ # * flock()
+ # * flush()
+ # * fsync()
+ # * internal_encoding()
+ # * ioctl()
+ # * isatty()
+ # * path()
+ # * pid()
+ # * pos()
+ # * pos=()
+ # * reopen()
+ # * seek()
+ # * stat()
+ # * sync()
+ # * sync=()
+ # * tell()
+ # * to_i()
+ # * to_io()
+ # * truncate()
+ # * tty?()
+ #
+ def self.open(*args)
+ # find the +options+ Hash
+ options = if args.last.is_a? Hash then args.pop else Hash.new end
+ # default to a binary open mode
+ args << "rb" if args.size == 1
+ # wrap a File opened with the remaining +args+
+ csv = new(File.open(*args), options)
+
+ # handle blocks like Ruby's open(), not like the CSV library
+ if block_given?
+ begin
+ yield csv
+ ensure
+ csv.close
+ end
+ else
+ csv
+ end
+ end
+
+ #
+ # :call-seq:
+ # parse( str, options = Hash.new ) { |row| ... }
+ # parse( str, options = Hash.new )
+ #
+ # This method can be used to easily parse CSV out of a String. You may either
+ # provide a +block+ which will be called with each row of the String in turn,
+ # or just use the returned Array of Arrays (when no +block+ is given).
+ #
+ # You pass your +str+ to read from, and an optional +options+ Hash containing
+ # anything CSV::new() understands.
+ #
+ def self.parse(*args, &block)
+ csv = new(*args)
+ if block.nil? # slurp contents, if no block is given
+ begin
+ csv.read
+ ensure
+ csv.close
+ end
+ else # or pass each row to a provided block
+ csv.each(&block)
+ end
+ end
+
+ #
+ # This method is a shortcut for converting a single line of a CSV String into
+ # a into an Array. Note that if +line+ contains multiple rows, anything
+ # beyond the first row is ignored.
+ #
+ # The +options+ parameter can be anthing CSV::new() understands.
+ #
+ def self.parse_line(line, options = Hash.new)
+ new(line, options).shift
+ end
+
+ #
+ # Use to slurp a CSV file into an Array of Arrays. Pass the +path+ to the
+ # file and any +options+ CSV::new() understands. This method also understands
+ # an additional <tt>:encoding</tt> parameter that you can use to specify the
+ # Encoding of the data in the file to be read. You must provide this unless
+ # your data is in Encoding::default_external(). CSV will use this to deterime
+ # how to parse the data. You may provide a second Encoding to have the data
+ # transcoded as it is read. For example,
+ # <tt>encoding: "UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the file
+ # but transcode it to UTF-8 before CSV parses it.
+ #
+ def self.read(path, options = Hash.new)
+ encoding = options.delete(:encoding)
+ mode = "rb"
+ mode << ":#{encoding}" if encoding
+ open(path, mode, options) { |csv| csv.read }
+ end
+
+ # Alias for CSV::read().
+ def self.readlines(*args)
+ read(*args)
+ end
+
+ #
+ # A shortcut for:
+ #
+ # CSV.read( path, { headers: true,
+ # converters: :numeric,
+ # header_converters: :symbol }.merge(options) )
+ #
+ def self.table(path, options = Hash.new)
+ read( path, { headers: true,
+ converters: :numeric,
+ header_converters: :symbol }.merge(options) )
+ end
+
+ #
+ # This constructor will wrap either a String or IO object passed in +data+ for
+ # reading and/or writing. In addition to the CSV instance methods, several IO
+ # methods are delegated. (See CSV::open() for a complete list.) If you pass
+ # a String for +data+, you can later retrieve it (after writing to it, for
+ # example) with CSV.string().
+ #
+ # Note that a wrapped String will be positioned at at the beginning (for
+ # reading). If you want it at the end (for writing), use CSV::generate().
+ # If you want any other positioning, pass a preset StringIO object instead.
+ #
+ # You may set any reading and/or writing preferences in the +options+ Hash.
+ # Available options are:
+ #
+ # <b><tt>:col_sep</tt></b>:: The String placed between each field.
+ # This String will be transcoded into
+ # the data's Encoding before parsing.
+ # <b><tt>:row_sep</tt></b>:: The String appended to the end of each
+ # row. This can be set to the special
+ # <tt>:auto</tt> setting, which requests
+ # that CSV automatically discover this
+ # from the data. Auto-discovery reads
+ # ahead in the data looking for the next
+ # <tt>"\r\n"</tt>, <tt>"\n"</tt>, or
+ # <tt>"\r"</tt> sequence. A sequence
+ # will be selected even if it occurs in
+ # a quoted field, assuming that you
+ # would have the same line endings
+ # there. If none of those sequences is
+ # found, +data+ is <tt>ARGF</tt>,
+ # <tt>STDIN</tt>, <tt>STDOUT</tt>, or
+ # <tt>STDERR</tt>, or the stream is only
+ # available for output, the default
+ # <tt>$INPUT_RECORD_SEPARATOR</tt>
+ # (<tt>$/</tt>) is used. Obviously,
+ # discovery takes a little time. Set
+ # manually if speed is important. Also
+ # note that IO objects should be opened
+ # in binary mode on Windows if this
+ # feature will be used as the
+ # line-ending translation can cause
+ # problems with resetting the document
+ # position to where it was before the
+ # read ahead. This String will be
+ # transcoded into the data's Encoding
+ # before parsing.
+ # <b><tt>:quote_char</tt></b>:: The character used to quote fields.
+ # This has to be a single character
+ # String. This is useful for
+ # application that incorrectly use
+ # <tt>'</tt> as the quote character
+ # instead of the correct <tt>"</tt>.
+ # CSV will always consider a double
+ # sequence this character to be an
+ # escaped quote. This String will be
+ # transcoded into the data's Encoding
+ # before parsing.
+ # <b><tt>:field_size_limit</tt></b>:: This is a maximum size CSV will read
+ # ahead looking for the closing quote
+ # for a field. (In truth, it reads to
+ # the first line ending beyond this
+ # size.) If a quote cannot be found
+ # within the limit CSV will raise a
+ # MalformedCSVError, assuming the data
+ # is faulty. You can use this limit to
+ # prevent what are effectively DoS
+ # attacks on the parser. However, this
+ # limit can cause a legitimate parse to
+ # fail and thus is set to +nil+, or off,
+ # by default.
+ # <b><tt>:converters</tt></b>:: An Array of names from the Converters
+ # Hash and/or lambdas that handle custom
+ # conversion. A single converter
+ # doesn't have to be in an Array. All
+ # built-in converters try to transcode
+ # fields to UTF-8 before converting.
+ # The conversion will fail if the data
+ # cannot be transcoded, leaving the
+ # field unchanged.
+ # <b><tt>:unconverted_fields</tt></b>:: If set to +true+, an
+ # unconverted_fields() method will be
+ # added to all returned rows (Array or
+ # CSV::Row) that will return the fields
+ # as they were before conversion. Note
+ # that <tt>:headers</tt> supplied by
+ # Array or String were not fields of the
+ # document and thus will have an empty
+ # Array attached.
+ # <b><tt>:headers</tt></b>:: If set to <tt>:first_row</tt> or
+ # +true+, the initial row of the CSV
+ # file will be treated as a row of
+ # headers. If set to an Array, the
+ # contents will be used as the headers.
+ # If set to a String, the String is run
+ # through a call of CSV::parse_line()
+ # with the same <tt>:col_sep</tt>,
+ # <tt>:row_sep</tt>, and
+ # <tt>:quote_char</tt> as this instance
+ # to produce an Array of headers. This
+ # setting causes CSV#shift() to return
+ # rows as CSV::Row objects instead of
+ # Arrays and CSV#read() to return
+ # CSV::Table objects instead of an Array
+ # of Arrays.
+ # <b><tt>:return_headers</tt></b>:: When +false+, header rows are silently
+ # swallowed. If set to +true+, header
+ # rows are returned in a CSV::Row object
+ # with identical headers and
+ # fields (save that the fields do not go
+ # through the converters).
+ # <b><tt>:write_headers</tt></b>:: When +true+ and <tt>:headers</tt> is
+ # set, a header row will be added to the
+ # output.
+ # <b><tt>:header_converters</tt></b>:: Identical in functionality to
+ # <tt>:converters</tt> save that the
+ # conversions are only made to header
+ # rows. All built-in converters try to
+ # transcode headers to UTF-8 before
+ # converting. The conversion will fail
+ # if the data cannot be transcoded,
+ # leaving the header unchanged.
+ # <b><tt>:skip_blanks</tt></b>:: When set to a +true+ value, CSV will
+ # skip over any rows with no content.
+ # <b><tt>:force_quotes</tt></b>:: When set to a +true+ value, CSV will
+ # quote all CSV fields it creates.
+ #
+ # See CSV::DEFAULT_OPTIONS for the default settings.
+ #
+ # Options cannot be overriden in the instance methods for performance reasons,
+ # so be sure to set what you want here.
+ #
+ def initialize(data, options = Hash.new)
+ # build the options for this read/write
+ options = DEFAULT_OPTIONS.merge(options)
+
+ # create the IO object we will read from
+ @io = if data.is_a? String then StringIO.new(data) else data end
+ # honor the IO encoding if we can, otherwise default to ASCII-8BIT
+ @encoding = raw_encoding || Encoding.default_internal || Encoding.default_external
+ #
+ # prepare for building safe regular expressions in the target encoding,
+ # if we can transcode the needed characters
+ #
+ @re_esc = "\\".encode(@encoding) rescue ""
+ @re_chars = %w[ \\ . [ ] - ^ $ ?
+ * + { } ( ) | #
+ \ \r \n \t \f \v ].
+ map { |s| s.encode(@encoding) rescue nil }.compact
+
+ init_separators(options)
+ init_parsers(options)
+ init_converters(options)
+ init_headers(options)
+
+ unless options.empty?
+ raise ArgumentError, "Unknown options: #{options.keys.join(', ')}."
+ end
+
+ # track our own lineno since IO gets confused about line-ends is CSV fields
+ @lineno = 0
+ end
+
+ #
+ # The encoded <tt>:col_sep</tt> used in parsing and writing. See CSV::new
+ # for details.
+ #
+ attr_reader :col_sep
+ #
+ # The encoded <tt>:row_sep</tt> used in parsing and writing. See CSV::new
+ # for details.
+ #
+ attr_reader :row_sep
+ #
+ # The encoded <tt>:quote_char</tt> used in parsing and writing. See CSV::new
+ # for details.
+ #
+ attr_reader :quote_char
+ # The limit for field size, if any. See CSV::new for details.
+ attr_reader :field_size_limit
+ #
+ # Returns the current list of converters in effect. See CSV::new for details.
+ # Built-in converters will be returned by name, while others will be returned
+ # as is.
+ #
+ def converters
+ @converters.map do |converter|
+ name = Converters.rassoc(converter)
+ name ? name.first : converter
+ end
+ end
+ #
+ # Returns +true+ if unconverted_fields() to parsed results. See CSV::new
+ # for details.
+ #
+ def unconverted_fields?() @unconverted_fields end
+ #
+ # Returns +nil+ if headers will not be used, +true+ if they will but have not
+ # yet been read, or the actual headers after they have been read. See
+ # CSV::new for details.
+ #
+ def headers
+ @headers || true if @use_headers
+ end
+ #
+ # Returns +true+ if headers will be returned as a row of results.
+ # See CSV::new for details.
+ #
+ def return_headers?() @return_headers end
+ # Returns +true+ if headers are written in output. See CSV::new for details.
+ def write_headers?() @write_headers end
+ #
+ # Returns the current list of converters in effect for headers. See CSV::new
+ # for details. Built-in converters will be returned by name, while others
+ # will be returned as is.
+ #
+ def header_converters
+ @header_converters.map do |converter|
+ name = HeaderConverters.rassoc(converter)
+ name ? name.first : converter
+ end
+ end
+ #
+ # Returns +true+ blank lines are skipped by the parser. See CSV::new
+ # for details.
+ #
+ def skip_blanks?() @skip_blanks end
+ # Returns +true+ if all output fields are quoted. See CSV::new for details.
+ def force_quotes?() @force_quotes end
+
+ #
+ # The Encoding CSV is parsing or writing in. This will be the Encoding you
+ # receive parsed data in and/or the Encoding data will be written in.
+ #
+ attr_reader :encoding
+
+ #
+ # The line number of the last row read from this file. Fields with nested
+ # line-end characters will not affect this count.
+ #
+ attr_reader :lineno
+
+ ### IO and StringIO Delegation ###
+
+ extend Forwardable
+ def_delegators :@io, :binmode, :binmode?, :close, :close_read, :close_write,
+ :closed?, :eof, :eof?, :external_encoding, :fcntl,
+ :fileno, :flock, :flush, :fsync, :internal_encoding,
+ :ioctl, :isatty, :path, :pid, :pos, :pos=, :reopen,
+ :seek, :stat, :string, :sync, :sync=, :tell, :to_i,
+ :to_io, :truncate, :tty?
+
+ # Rewinds the underlying IO object and resets CSV's lineno() counter.
+ def rewind
+ @headers = nil
+ @lineno = 0
+
+ @io.rewind
+ end
+
+ ### End Delegation ###
+
+ #
+ # The primary write method for wrapped Strings and IOs, +row+ (an Array or
+ # CSV::Row) is converted to CSV and appended to the data source. When a
+ # CSV::Row is passed, only the row's fields() are appended to the output.
+ #
+ # The data source must be open for writing.
+ #
+ def <<(row)
+ # make sure headers have been assigned
+ if header_row? and [Array, String].include? @use_headers.class
+ parse_headers # won't read data for Array or String
+ self << @headers if @write_headers
+ end
+
+ # handle CSV::Row objects and Hashes
+ row = case row
+ when self.class::Row then row.fields
+ when Hash then @headers.map { |header| row[header] }
+ else row
+ end
+
+ @headers = row if header_row?
+ @lineno += 1
+
+ @io << row.map(&@quote).join(@col_sep) + @row_sep # quote and separate
+
+ self # for chaining
+ end
+ alias_method :add_row, :<<
+ alias_method :puts, :<<
+
+ #
+ # :call-seq:
+ # convert( name )
+ # convert { |field| ... }
+ # convert { |field, field_info| ... }
+ #
+ # You can use this method to install a CSV::Converters built-in, or provide a
+ # block that handles a custom conversion.
+ #
+ # If you provide a block that takes one argument, it will be passed the field
+ # and is expected to return the converted value or the field itself. If your
+ # block takes two arguments, it will also be passed a CSV::FieldInfo Struct,
+ # containing details about the field. Again, the block should return a
+ # converted field or the field itself.
+ #
+ def convert(name = nil, &converter)
+ add_converter(:converters, self.class::Converters, name, &converter)
+ end
+
+ #
+ # :call-seq:
+ # header_convert( name )
+ # header_convert { |field| ... }
+ # header_convert { |field, field_info| ... }
+ #
+ # Identical to CSV#convert(), but for header rows.
+ #
+ # Note that this method must be called before header rows are read to have any
+ # effect.
+ #
+ def header_convert(name = nil, &converter)
+ add_converter( :header_converters,
+ self.class::HeaderConverters,
+ name,
+ &converter )
+ end
+
+ include Enumerable
+
+ #
+ # Yields each row of the data source in turn.
+ #
+ # Support for Enumerable.
+ #
+ # The data source must be open for reading.
+ #
+ def each
+ while row = shift
+ yield row
+ end
+ end
+
+ #
+ # Slurps the remaining rows and returns an Array of Arrays.
+ #
+ # The data source must be open for reading.
+ #
+ def read
+ rows = to_a
+ if @use_headers
+ Table.new(rows)
+ else
+ rows
+ end
+ end
+ alias_method :readlines, :read
+
+ # Returns +true+ if the next row read will be a header row.
+ def header_row?
+ @use_headers and @headers.nil?
+ end
+
+ #
+ # The primary read method for wrapped Strings and IOs, a single row is pulled
+ # from the data source, parsed and returned as an Array of fields (if header
+ # rows are not used) or a CSV::Row (when header rows are used).
+ #
+ # The data source must be open for reading.
+ #
+ def shift
+ #########################################################################
+ ### This method is purposefully kept a bit long as simple conditional ###
+ ### checks are faster than numerous (expensive) method calls. ###
+ #########################################################################
+
+ # handle headers not based on document content
+ if header_row? and @return_headers and
+ [Array, String].include? @use_headers.class
+ if @unconverted_fields
+ return add_unconverted_fields(parse_headers, Array.new)
+ else
+ return parse_headers
+ end
+ end
+
+ # begin with a blank line, so we can always add to it
+ line = ""
+
+ #
+ # it can take multiple calls to <tt>@io.gets()</tt> to get a full line,
+ # because of \r and/or \n characters embedded in quoted fields
+ #
+ loop do
+ # add another read to the line
+ (line += @io.gets(@row_sep)) rescue return nil
+ # copy the line so we can chop it up in parsing
+ parse = line.dup
+ parse.sub!(@parsers[:line_end], "")
+
+ #
+ # I believe a blank line should be an <tt>Array.new</tt>, not Ruby 1.8
+ # CSV's <tt>[nil]</tt>
+ #
+ if parse.empty?
+ @lineno += 1
+ if @skip_blanks
+ line = ""
+ next
+ elsif @unconverted_fields
+ return add_unconverted_fields(Array.new, Array.new)
+ elsif @use_headers
+ return self.class::Row.new(Array.new, Array.new)
+ else
+ return Array.new
+ end
+ end
+
+ #
+ # shave leading empty fields if needed, because the main parser chokes
+ # on these
+ #
+ csv = if parse.sub!(@parsers[:leading_fields], "")
+ [nil] * ($&.length / @col_sep.length)
+ else
+ Array.new
+ end
+ #
+ # then parse the main fields with a hyper-tuned Regexp from
+ # Mastering Regular Expressions, Second Edition
+ #
+ parse.gsub!(@parsers[:csv_row]) do
+ csv << if $1.nil? # we found an unquoted field
+ if $2.empty? # switch empty unquoted fields to +nil+...
+ nil # for Ruby 1.8 CSV compatibility
+ else
+ # I decided to take a strict approach to CSV parsing...
+ if $2.count(@parsers[:return_newline]).zero? # verify correctness
+ $2
+ else
+ # or throw an Exception
+ raise MalformedCSVError, "Unquoted fields do not allow " +
+ "\\r or \\n (line #{lineno + 1})."
+ end
+ end
+ else # we found a quoted field...
+ $1.gsub(@quote_char * 2, @quote_char) # unescape contents
+ end
+ "" # gsub!'s replacement, clear the field
+ end
+
+ # if parse is empty?(), we found all the fields on the line...
+ if parse.empty?
+ @lineno += 1
+
+ # save fields unconverted fields, if needed...
+ unconverted = csv.dup if @unconverted_fields
+
+ # convert fields, if needed...
+ csv = convert_fields(csv) unless @use_headers or @converters.empty?
+ # parse out header rows and handle CSV::Row conversions...
+ csv = parse_headers(csv) if @use_headers
+
+ # inject unconverted fields and accessor, if requested...
+ if @unconverted_fields and not csv.respond_to? :unconverted_fields
+ add_unconverted_fields(csv, unconverted)
+ end
+
+ # return the results
+ break csv
+ end
+ # if we're not empty?() but at eof?(), a quoted field wasn't closed...
+ if @io.eof?
+ raise MalformedCSVError, "Unclosed quoted field on line #{lineno + 1}."
+ elsif parse =~ @parsers[:bad_field]
+ raise MalformedCSVError, "Illegal quoting on line #{lineno + 1}."
+ elsif @field_size_limit and parse.length >= @field_size_limit
+ raise MalformedCSVError, "Field size exceeded on line #{lineno + 1}."
+ end
+ # otherwise, we need to loop and pull some more data to complete the row
+ end
+ end
+ alias_method :gets, :shift
+ alias_method :readline, :shift
+
+ #
+ # Returns a simplified description of the key FasterCSV attributes in an
+ # ASCII compatible String.
+ #
+ def inspect
+ str = ["<#", self.class.to_s, " io_type:"]
+ # show type of wrapped IO
+ if @io == $stdout then str << "$stdout"
+ elsif @io == $stdin then str << "$stdin"
+ elsif @io == $stderr then str << "$stderr"
+ else str << @io.class.to_s
+ end
+ # show IO.path(), if available
+ if @io.respond_to?(:path) and (p = @io.path)
+ str << " io_path:" << p.inspect
+ end
+ # show encoding
+ str << " encoding:" << @encoding.name
+ # show other attributes
+ %w[ lineno col_sep row_sep
+ quote_char skip_blanks ].each do |attr_name|
+ if a = instance_variable_get("@#{attr_name}")
+ str << " " << attr_name << ":" << a.inspect
+ end
+ end
+ if @use_headers
+ str << " headers:" << headers.inspect
+ end
+ str << ">"
+ begin
+ str.join
+ rescue # any encoding error
+ str.map do |s|
+ e = Encoding::Converter.asciicompat_encoding(s.encoding)
+ e ? s.encode(e) : s.force_encoding("ASCII-8BIT")
+ end.join
+ end
+ end
+
+ private
+
+ #
+ # Stores the indicated separators for later use.
+ #
+ # If auto-discovery was requested for <tt>@row_sep</tt>, this method will read
+ # ahead in the <tt>@io</tt> and try to find one. +ARGF+, +STDIN+, +STDOUT+,
+ # +STDERR+ and any stream open for output only with a default
+ # <tt>@row_sep</tt> of <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>).
+ #
+ # This method also establishes the quoting rules used for CSV output.
+ #
+ def init_separators(options)
+ # store the selected separators
+ @col_sep = options.delete(:col_sep).to_s.encode(@encoding)
+ @row_sep = options.delete(:row_sep) # encode after resolving :auto
+ @quote_char = options.delete(:quote_char).to_s.encode(@encoding)
+
+ if @quote_char.length != 1
+ raise ArgumentError, ":quote_char has to be a single character String"
+ end
+
+ #
+ # automatically discover row separator when requested
+ # (not fully encoding safe)
+ #
+ if @row_sep == :auto
+ if [ARGF, STDIN, STDOUT, STDERR].include?(@io) or
+ (defined?(Zlib) and @io.class == Zlib::GzipWriter)
+ @row_sep = $INPUT_RECORD_SEPARATOR
+ else
+ begin
+ saved_pos = @io.pos # remember where we were
+ while @row_sep == :auto
+ #
+ # if we run out of data, it's probably a single line
+ # (use a sensible default)
+ #
+ if @io.eof?
+ @row_sep = $INPUT_RECORD_SEPARATOR
+ break
+ end
+
+ # read ahead a bit
+ sample = read_to_char(1024)
+ sample += read_to_char(1) if sample[-1..-1] == encode_str("\r") and
+ not @io.eof?
+ # try to find a standard separator
+ if sample =~ encode_re("\r\n?|\n")
+ @row_sep = $&
+ break
+ end
+ end
+ # tricky seek() clone to work around GzipReader's lack of seek()
+ @io.rewind
+ # reset back to the remembered position
+ while saved_pos > 1024 # avoid loading a lot of data into memory
+ @io.read(1024)
+ saved_pos -= 1024
+ end
+ @io.read(saved_pos) if saved_pos.nonzero?
+ rescue IOError # stream not opened for reading
+ @row_sep = $INPUT_RECORD_SEPARATOR
+ end
+ end
+ end
+ @row_sep = @row_sep.to_s.encode(@encoding)
+
+ # establish quoting rules
+ @force_quotes = options.delete(:force_quotes)
+ do_quote = lambda do |field|
+ @quote_char +
+ String(field).gsub(@quote_char, @quote_char * 2) +
+ @quote_char
+ end
+ quotable_chars = encode_str("\r\n", @col_sep, @quote_char)
+ @quote = if @force_quotes
+ do_quote
+ else
+ lambda do |field|
+ if field.nil? # represent +nil+ fields as empty unquoted fields
+ ""
+ else
+ field = String(field) # Stringify fields
+ # represent empty fields as empty quoted fields
+ if field.empty? or
+ field.count(quotable_chars).nonzero?
+ do_quote.call(field)
+ else
+ field # unquoted field
+ end
+ end
+ end
+ end
+ end
+
+ # Pre-compiles parsers and stores them by name for access during reads.
+ def init_parsers(options)
+ # store the parser behaviors
+ @skip_blanks = options.delete(:skip_blanks)
+ @field_size_limit = options.delete(:field_size_limit)
+
+ # prebuild Regexps for faster parsing
+ esc_col_sep = escape_re(@col_sep)
+ esc_row_sep = escape_re(@row_sep)
+ esc_quote = escape_re(@quote_char)
+ @parsers = {
+ # for empty leading fields
+ leading_fields: encode_re("\\A(?:", esc_col_sep, ")+"),
+ # The Primary Parser
+ csv_row: encode_re(
+ "\\G(?:\\A|", esc_col_sep, ")", # anchor the match
+ "(?:", esc_quote, # find quoted fields
+ "((?>[^", esc_quote, "]*)", # "unrolling the loop"
+ "(?>", esc_quote * 2, # double for escaping
+ "[^", esc_quote, "]*)*)",
+ esc_quote,
+ "|", # ... or ...
+ "([^", esc_quote, esc_col_sep, "]*))", # unquoted fields
+ "(?=", esc_col_sep, "|\\z)" # ensure field is ended
+ ),
+ # a test for unescaped quotes
+ bad_field: encode_re(
+ "\\A", esc_col_sep, "?", # an optional comma
+ "(?:", esc_quote, # a quoted field
+ "(?>[^", esc_quote, "]*)", # "unrolling the loop"
+ "(?>", esc_quote * 2, # double for escaping
+ "[^", esc_quote, "]*)*",
+ esc_quote, # the closing quote
+ "[^", esc_quote, "]", # an extra character
+ "|", # ... or ...
+ "[^", esc_quote, esc_col_sep, "]+", # an unquoted field
+ esc_quote, ")" # an extra quote
+ ),
+ # safer than chomp!()
+ line_end: encode_re(esc_row_sep, "\\z"),
+ # illegal unquoted characters
+ return_newline: encode_str("\r\n")
+ }
+ end
+
+ #
+ # Loads any converters requested during construction.
+ #
+ # If +field_name+ is set <tt>:converters</tt> (the default) field converters
+ # are set. When +field_name+ is <tt>:header_converters</tt> header converters
+ # are added instead.
+ #
+ # The <tt>:unconverted_fields</tt> option is also actived for
+ # <tt>:converters</tt> calls, if requested.
+ #
+ def init_converters(options, field_name = :converters)
+ if field_name == :converters
+ @unconverted_fields = options.delete(:unconverted_fields)
+ end
+
+ instance_variable_set("@#{field_name}", Array.new)
+
+ # find the correct method to add the converters
+ convert = method(field_name.to_s.sub(/ers\Z/, ""))
+
+ # load converters
+ unless options[field_name].nil?
+ # allow a single converter not wrapped in an Array
+ unless options[field_name].is_a? Array
+ options[field_name] = [options[field_name]]
+ end
+ # load each converter...
+ options[field_name].each do |converter|
+ if converter.is_a? Proc # custom code block
+ convert.call(&converter)
+ else # by name
+ convert.call(converter)
+ end
+ end
+ end
+
+ options.delete(field_name)
+ end
+
+ # Stores header row settings and loads header converters, if needed.
+ def init_headers(options)
+ @use_headers = options.delete(:headers)
+ @return_headers = options.delete(:return_headers)
+ @write_headers = options.delete(:write_headers)
+
+ # headers must be delayed until shift(), in case they need a row of content
+ @headers = nil
+
+ init_converters(options, :header_converters)
+ end
+
+ #
+ # The actual work method for adding converters, used by both CSV.convert() and
+ # CSV.header_convert().
+ #
+ # This method requires the +var_name+ of the instance variable to place the
+ # converters in, the +const+ Hash to lookup named converters in, and the
+ # normal parameters of the CSV.convert() and CSV.header_convert() methods.
+ #
+ def add_converter(var_name, const, name = nil, &converter)
+ if name.nil? # custom converter
+ instance_variable_get("@#{var_name}") << converter
+ else # named converter
+ combo = const[name]
+ case combo
+ when Array # combo converter
+ combo.each do |converter_name|
+ add_converter(var_name, const, converter_name)
+ end
+ else # individual named converter
+ instance_variable_get("@#{var_name}") << combo
+ end
+ end
+ end
+
+ #
+ # Processes +fields+ with <tt>@converters</tt>, or <tt>@header_converters</tt>
+ # if +headers+ is passed as +true+, returning the converted field set. Any
+ # converter that changes the field into something other than a String halts
+ # the pipeline of conversion for that field. This is primarily an efficiency
+ # shortcut.
+ #
+ def convert_fields(fields, headers = false)
+ # see if we are converting headers or fields
+ converters = headers ? @header_converters : @converters
+
+ fields.map.with_index do |field, index|
+ converters.each do |converter|
+ field = if converter.arity == 1 # straight field converter
+ converter[field]
+ else # FieldInfo converter
+ header = @use_headers && !headers ? @headers[index] : nil
+ converter[field, FieldInfo.new(index, lineno, header)]
+ end
+ break unless field.is_a? String # short-curcuit pipeline for speed
+ end
+ field # final state of each field, converted or original
+ end
+ end
+
+ #
+ # This methods is used to turn a finished +row+ into a CSV::Row. Header rows
+ # are also dealt with here, either by returning a CSV::Row with identical
+ # headers and fields (save that the fields do not go through the converters)
+ # or by reading past them to return a field row. Headers are also saved in
+ # <tt>@headers</tt> for use in future rows.
+ #
+ # When +nil+, +row+ is assumed to be a header row not based on an actual row
+ # of the stream.
+ #
+ def parse_headers(row = nil)
+ if @headers.nil? # header row
+ @headers = case @use_headers # save headers
+ # Array of headers
+ when Array then @use_headers
+ # CSV header String
+ when String
+ self.class.parse_line( @use_headers,
+ col_sep: @col_sep,
+ row_sep: @row_sep,
+ quote_char: @quote_char )
+ # first row is headers
+ else row
+ end
+
+ # prepare converted and unconverted copies
+ row = @headers if row.nil?
+ @headers = convert_fields(@headers, true)
+
+ if @return_headers # return headers
+ return self.class::Row.new(@headers, row, true)
+ elsif not [Array, String].include? @use_headers.class # skip to field row
+ return shift
+ end
+ end
+
+ self.class::Row.new(@headers, convert_fields(row)) # field row
+ end
+
+ #
+ # Thiw methods injects an instance variable <tt>unconverted_fields</tt> into
+ # +row+ and an accessor method for it called unconverted_fields(). The
+ # variable is set to the contents of +fields+.
+ #
+ def add_unconverted_fields(row, fields)
+ class << row
+ attr_reader :unconverted_fields
+ end
+ row.instance_eval { @unconverted_fields = fields }
+ row
+ end
+
+ #
+ # This method is an encoding safe version of Regexp::escape(). It will escape
+ # any characters that would change the meaning of a regular expression in the
+ # encoding of +str+. Regular expression characters that cannot be transcoded
+ # to the target encoding will be skipped and no escaping will be performed if
+ # a backslash cannot be transcoded.
+ #
+ def escape_re(str)
+ str.chars.map { |c| @re_chars.include?(c) ? @re_esc + c : c }.join
+ end
+
+ #
+ # Builds a regular expression in <tt>@encoding</tt>. All +chunks+ will be
+ # transcoded to that encoding.
+ #
+ def encode_re(*chunks)
+ Regexp.new(encode_str(*chunks))
+ end
+
+ #
+ # Builds a String in <tt>@encoding</tt>. All +chunks+ will be transcoded to
+ # that encoding.
+ #
+ def encode_str(*chunks)
+ chunks.map { |chunk| chunk.encode(@encoding.name) }.join
+ end
+
+ #
+ # Reads at least +bytes+ from <tt>@io</tt>, but will read up 10 bytes ahead if
+ # needed to ensure the data read is valid in the ecoding of that data. This
+ # should ensure that it is safe to use regular expressions on the read data,
+ # unless it is actually a broken encoding. The read data will be returned in
+ # <tt>@encoding</tt>.
+ #
+ def read_to_char(bytes)
+ return "" if @io.eof?
+ data = read_io(bytes)
+ begin
+ raise unless data.valid_encoding?
+ encoded = encode_str(data)
+ raise unless encoded.valid_encoding?
+ return encoded
+ rescue # encoding error or my invalid data raise
+ if @io.eof? or data.size >= bytes + 10
+ return data
+ else
+ data += read_io(1)
+ retry
+ end
+ end
+ end
+
+ private
+ def raw_encoding
+ if @io.respond_to? :internal_encoding
+ @io.internal_encoding || @io.external_encoding
+ elsif @io.is_a? StringIO
+ @io.string.encoding
+ elsif @io.respond_to? :encoding
+ @io.encoding
+ else
+ Encoding::ASCII_8BIT
+ end
+ end
+
+ def read_io(bytes)
+ @io.read(bytes).force_encoding(raw_encoding)
+ end
+end
+
+# Another name for CSV::instance().
+def CSV(*args, &block)
+ CSV.instance(*args, &block)
+end
+
+class Array
+ # Equivalent to <tt>CSV::generate_line(self, options)</tt>.
+ def to_csv(options = Hash.new)
+ CSV.generate_line(self, options)
+ end
+end
+
+class String
+ # Equivalent to <tt>CSV::parse_line(self, options)</tt>.
+ def parse_csv(options = Hash.new)
+ CSV.parse_line(self, options)
+ end
+end
diff --git a/ruby/lib/date.rb b/ruby/lib/date.rb
new file mode 100644
index 0000000..2c97925
--- /dev/null
+++ b/ruby/lib/date.rb
@@ -0,0 +1,1834 @@
+#
+# date.rb - date and time library
+#
+# Author: Tadayoshi Funaba 1998-2008
+#
+# Documentation: William Webber <william@williamwebber.com>
+#
+#--
+# $Id: date.rb,v 2.37 2008-01-17 20:16:31+09 tadf Exp $
+#++
+#
+# == Overview
+#
+# This file provides two classes for working with
+# dates and times.
+#
+# The first class, Date, represents dates.
+# It works with years, months, weeks, and days.
+# See the Date class documentation for more details.
+#
+# The second, DateTime, extends Date to include hours,
+# minutes, seconds, and fractions of a second. It
+# provides basic support for time zones. See the
+# DateTime class documentation for more details.
+#
+# === Ways of calculating the date.
+#
+# In common usage, the date is reckoned in years since or
+# before the Common Era (CE/BCE, also known as AD/BC), then
+# as a month and day-of-the-month within the current year.
+# This is known as the *Civil* *Date*, and abbreviated
+# as +civil+ in the Date class.
+#
+# Instead of year, month-of-the-year, and day-of-the-month,
+# the date can also be reckoned in terms of year and
+# day-of-the-year. This is known as the *Ordinal* *Date*,
+# and is abbreviated as +ordinal+ in the Date class. (Note
+# that referring to this as the Julian date is incorrect.)
+#
+# The date can also be reckoned in terms of year, week-of-the-year,
+# and day-of-the-week. This is known as the *Commercial*
+# *Date*, and is abbreviated as +commercial+ in the
+# Date class. The commercial week runs Monday (day-of-the-week
+# 1) to Sunday (day-of-the-week 7), in contrast to the civil
+# week which runs Sunday (day-of-the-week 0) to Saturday
+# (day-of-the-week 6). The first week of the commercial year
+# starts on the Monday on or before January 1, and the commercial
+# year itself starts on this Monday, not January 1.
+#
+# For scientific purposes, it is convenient to refer to a date
+# simply as a day count, counting from an arbitrary initial
+# day. The date first chosen for this was January 1, 4713 BCE.
+# A count of days from this date is the *Julian* *Day* *Number*
+# or *Julian* *Date*, which is abbreviated as +jd+ in the
+# Date class. This is in local time, and counts from midnight
+# on the initial day. The stricter usage is in UTC, and counts
+# from midday on the initial day. This is referred to in the
+# Date class as the *Astronomical* *Julian* *Day* *Number*, and
+# abbreviated as +ajd+. In the Date class, the Astronomical
+# Julian Day Number includes fractional days.
+#
+# Another absolute day count is the *Modified* *Julian* *Day*
+# *Number*, which takes November 17, 1858 as its initial day.
+# This is abbreviated as +mjd+ in the Date class. There
+# is also an *Astronomical* *Modified* *Julian* *Day* *Number*,
+# which is in UTC and includes fractional days. This is
+# abbreviated as +amjd+ in the Date class. Like the Modified
+# Julian Day Number (and unlike the Astronomical Julian
+# Day Number), it counts from midnight.
+#
+# Alternative calendars such as the Chinese Lunar Calendar,
+# the Islamic Calendar, or the French Revolutionary Calendar
+# are not supported by the Date class; nor are calendars that
+# are based on an Era different from the Common Era, such as
+# the Japanese Imperial Calendar or the Republic of China
+# Calendar.
+#
+# === Calendar Reform
+#
+# The standard civil year is 365 days long. However, the
+# solar year is fractionally longer than this. To account
+# for this, a *leap* *year* is occasionally inserted. This
+# is a year with 366 days, the extra day falling on February 29.
+# In the early days of the civil calendar, every fourth
+# year without exception was a leap year. This way of
+# reckoning leap years is the *Julian* *Calendar*.
+#
+# However, the solar year is marginally shorter than 365 1/4
+# days, and so the *Julian* *Calendar* gradually ran slow
+# over the centuries. To correct this, every 100th year
+# (but not every 400th year) was excluded as a leap year.
+# This way of reckoning leap years, which we use today, is
+# the *Gregorian* *Calendar*.
+#
+# The Gregorian Calendar was introduced at different times
+# in different regions. The day on which it was introduced
+# for a particular region is the *Day* *of* *Calendar*
+# *Reform* for that region. This is abbreviated as +sg+
+# (for Start of Gregorian calendar) in the Date class.
+#
+# Two such days are of particular
+# significance. The first is October 15, 1582, which was
+# the Day of Calendar Reform for Italy and most Catholic
+# countries. The second is September 14, 1752, which was
+# the Day of Calendar Reform for England and its colonies
+# (including what is now the United States). These two
+# dates are available as the constants Date::ITALY and
+# Date::ENGLAND, respectively. (By comparison, Germany and
+# Holland, less Catholic than Italy but less stubborn than
+# England, changed over in 1698; Sweden in 1753; Russia not
+# till 1918, after the Revolution; and Greece in 1923. Many
+# Orthodox churches still use the Julian Calendar. A complete
+# list of Days of Calendar Reform can be found at
+# http://www.polysyllabic.com/GregConv.html.)
+#
+# Switching from the Julian to the Gregorian calendar
+# involved skipping a number of days to make up for the
+# accumulated lag, and the later the switch was (or is)
+# done, the more days need to be skipped. So in 1582 in Italy,
+# 4th October was followed by 15th October, skipping 10 days; in 1752
+# in England, 2nd September was followed by 14th September, skipping
+# 11 days; and if I decided to switch from Julian to Gregorian
+# Calendar this midnight, I would go from 27th July 2003 (Julian)
+# today to 10th August 2003 (Gregorian) tomorrow, skipping
+# 13 days. The Date class is aware of this gap, and a supposed
+# date that would fall in the middle of it is regarded as invalid.
+#
+# The Day of Calendar Reform is relevant to all date representations
+# involving years. It is not relevant to the Julian Day Numbers,
+# except for converting between them and year-based representations.
+#
+# In the Date and DateTime classes, the Day of Calendar Reform or
+# +sg+ can be specified a number of ways. First, it can be as
+# the Julian Day Number of the Day of Calendar Reform. Second,
+# it can be using the constants Date::ITALY or Date::ENGLAND; these
+# are in fact the Julian Day Numbers of the Day of Calendar Reform
+# of the respective regions. Third, it can be as the constant
+# Date::JULIAN, which means to always use the Julian Calendar.
+# Finally, it can be as the constant Date::GREGORIAN, which means
+# to always use the Gregorian Calendar.
+#
+# Note: in the Julian Calendar, New Years Day was March 25. The
+# Date class does not follow this convention.
+#
+# === Time Zones
+#
+# DateTime objects support a simple representation
+# of time zones. Time zones are represented as an offset
+# from UTC, as a fraction of a day. This offset is the
+# how much local time is later (or earlier) than UTC.
+# UTC offset 0 is centred on England (also known as GMT).
+# As you travel east, the offset increases until you
+# reach the dateline in the middle of the Pacific Ocean;
+# as you travel west, the offset decreases. This offset
+# is abbreviated as +of+ in the Date class.
+#
+# This simple representation of time zones does not take
+# into account the common practice of Daylight Savings
+# Time or Summer Time.
+#
+# Most DateTime methods return the date and the
+# time in local time. The two exceptions are
+# #ajd() and #amjd(), which return the date and time
+# in UTC time, including fractional days.
+#
+# The Date class does not support time zone offsets, in that
+# there is no way to create a Date object with a time zone.
+# However, methods of the Date class when used by a
+# DateTime instance will use the time zone offset of this
+# instance.
+#
+# == Examples of use
+#
+# === Print out the date of every Sunday between two dates.
+#
+# def print_sundays(d1, d2)
+# d1 +=1 while (d1.wday != 0)
+# d1.step(d2, 7) do |date|
+# puts "#{Date::MONTHNAMES[date.mon]} #{date.day}"
+# end
+# end
+#
+# print_sundays(Date::civil(2003, 4, 8), Date::civil(2003, 5, 23))
+#
+# === Calculate how many seconds to go till midnight on New Year's Day.
+#
+# def secs_to_new_year(now = DateTime::now())
+# new_year = DateTime.new(now.year + 1, 1, 1)
+# dif = new_year - now
+# hours, mins, secs, ignore_fractions = Date::day_fraction_to_time(dif)
+# return hours * 60 * 60 + mins * 60 + secs
+# end
+#
+# puts secs_to_new_year()
+
+require 'date/format'
+
+# Class representing a date.
+#
+# See the documentation to the file date.rb for an overview.
+#
+# Internally, the date is represented as an Astronomical
+# Julian Day Number, +ajd+. The Day of Calendar Reform, +sg+, is
+# also stored, for conversions to other date formats. (There
+# is also an +of+ field for a time zone offset, but this
+# is only for the use of the DateTime subclass.)
+#
+# A new Date object is created using one of the object creation
+# class methods named after the corresponding date format, and the
+# arguments appropriate to that date format; for instance,
+# Date::civil() (aliased to Date::new()) with year, month,
+# and day-of-month, or Date::ordinal() with year and day-of-year.
+# All of these object creation class methods also take the
+# Day of Calendar Reform as an optional argument.
+#
+# Date objects are immutable once created.
+#
+# Once a Date has been created, date values
+# can be retrieved for the different date formats supported
+# using instance methods. For instance, #mon() gives the
+# Civil month, #cwday() gives the Commercial day of the week,
+# and #yday() gives the Ordinal day of the year. Date values
+# can be retrieved in any format, regardless of what format
+# was used to create the Date instance.
+#
+# The Date class includes the Comparable module, allowing
+# date objects to be compared and sorted, ranges of dates
+# to be created, and so forth.
+class Date
+
+ include Comparable
+
+ # Full month names, in English. Months count from 1 to 12; a
+ # month's numerical representation indexed into this array
+ # gives the name of that month (hence the first element is nil).
+ MONTHNAMES = [nil] + %w(January February March April May June July
+ August September October November December)
+
+ # Full names of days of the week, in English. Days of the week
+ # count from 0 to 6 (except in the commercial week); a day's numerical
+ # representation indexed into this array gives the name of that day.
+ DAYNAMES = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
+
+ # Abbreviated month names, in English.
+ ABBR_MONTHNAMES = [nil] + %w(Jan Feb Mar Apr May Jun
+ Jul Aug Sep Oct Nov Dec)
+
+ # Abbreviated day names, in English.
+ ABBR_DAYNAMES = %w(Sun Mon Tue Wed Thu Fri Sat)
+
+ [MONTHNAMES, DAYNAMES, ABBR_MONTHNAMES, ABBR_DAYNAMES].each do |xs|
+ xs.each{|x| x.freeze unless x.nil?}.freeze
+ end
+
+ class Infinity < Numeric # :nodoc:
+
+ include Comparable
+
+ def initialize(d=1) @d = d <=> 0 end
+
+ def d() @d end
+
+ protected :d
+
+ def zero? () false end
+ def finite? () false end
+ def infinite? () d.nonzero? end
+ def nan? () d.zero? end
+
+ def abs() self.class.new end
+
+ def -@ () self.class.new(-d) end
+ def +@ () self.class.new(+d) end
+
+ def <=> (other)
+ case other
+ when Infinity; return d <=> other.d
+ when Numeric; return d
+ else
+ begin
+ l, r = other.coerce(self)
+ return l <=> r
+ rescue NoMethodError
+ end
+ end
+ nil
+ end
+
+ def coerce(other)
+ case other
+ when Numeric; return -d, d
+ else
+ super
+ end
+ end
+
+ end
+
+ # The Julian Day Number of the Day of Calendar Reform for Italy
+ # and the Catholic countries.
+ ITALY = 2299161 # 1582-10-15
+
+ # The Julian Day Number of the Day of Calendar Reform for England
+ # and her Colonies.
+ ENGLAND = 2361222 # 1752-09-14
+
+ # A constant used to indicate that a Date should always use the
+ # Julian calendar.
+ JULIAN = Infinity.new
+
+ # A constant used to indicate that a Date should always use the
+ # Gregorian calendar.
+ GREGORIAN = -Infinity.new
+
+ HALF_DAYS_IN_DAY = Rational(1, 2) # :nodoc:
+ HOURS_IN_DAY = Rational(1, 24) # :nodoc:
+ MINUTES_IN_DAY = Rational(1, 1440) # :nodoc:
+ SECONDS_IN_DAY = Rational(1, 86400) # :nodoc:
+ MILLISECONDS_IN_DAY = Rational(1, 86400*10**3) # :nodoc:
+ NANOSECONDS_IN_DAY = Rational(1, 86400*10**9) # :nodoc:
+ MILLISECONDS_IN_SECOND = Rational(1, 10**3) # :nodoc:
+ NANOSECONDS_IN_SECOND = Rational(1, 10**9) # :nodoc:
+
+ MJD_EPOCH_IN_AJD = Rational(4800001, 2) # 1858-11-17 # :nodoc:
+ UNIX_EPOCH_IN_AJD = Rational(4881175, 2) # 1970-01-01 # :nodoc:
+ MJD_EPOCH_IN_CJD = 2400001 # :nodoc:
+ UNIX_EPOCH_IN_CJD = 2440588 # :nodoc:
+ LD_EPOCH_IN_CJD = 2299160 # :nodoc:
+
+ t = Module.new do
+
+ private
+
+ def find_fdoy(y, sg) # :nodoc:
+ j = nil
+ 1.upto(31) do |d|
+ break if j = _valid_civil?(y, 1, d, sg)
+ end
+ j
+ end
+
+ def find_ldoy(y, sg) # :nodoc:
+ j = nil
+ 31.downto(1) do |d|
+ break if j = _valid_civil?(y, 12, d, sg)
+ end
+ j
+ end
+
+ def find_fdom(y, m, sg) # :nodoc:
+ j = nil
+ 1.upto(31) do |d|
+ break if j = _valid_civil?(y, m, d, sg)
+ end
+ j
+ end
+
+ def find_ldom(y, m, sg) # :nodoc:
+ j = nil
+ 31.downto(1) do |d|
+ break if j = _valid_civil?(y, m, d, sg)
+ end
+ j
+ end
+
+ # Convert an Ordinal Date to a Julian Day Number.
+ #
+ # +y+ and +d+ are the year and day-of-year to convert.
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # Returns the corresponding Julian Day Number.
+ def ordinal_to_jd(y, d, sg=GREGORIAN) # :nodoc:
+ find_fdoy(y, sg) + d - 1
+ end
+
+ # Convert a Julian Day Number to an Ordinal Date.
+ #
+ # +jd+ is the Julian Day Number to convert.
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # Returns the corresponding Ordinal Date as
+ # [year, day_of_year]
+ def jd_to_ordinal(jd, sg=GREGORIAN) # :nodoc:
+ y = jd_to_civil(jd, sg)[0]
+ j = find_fdoy(y, sg)
+ doy = jd - j + 1
+ return y, doy
+ end
+
+ # Convert a Civil Date to a Julian Day Number.
+ # +y+, +m+, and +d+ are the year, month, and day of the
+ # month. +sg+ specifies the Day of Calendar Reform.
+ #
+ # Returns the corresponding Julian Day Number.
+ def civil_to_jd(y, m, d, sg=GREGORIAN) # :nodoc:
+ if m <= 2
+ y -= 1
+ m += 12
+ end
+ a = (y / 100.0).floor
+ b = 2 - a + (a / 4.0).floor
+ jd = (365.25 * (y + 4716)).floor +
+ (30.6001 * (m + 1)).floor +
+ d + b - 1524
+ if jd < sg
+ jd -= b
+ end
+ jd
+ end
+
+ # Convert a Julian Day Number to a Civil Date. +jd+ is
+ # the Julian Day Number. +sg+ specifies the Day of
+ # Calendar Reform.
+ #
+ # Returns the corresponding [year, month, day_of_month]
+ # as a three-element array.
+ def jd_to_civil(jd, sg=GREGORIAN) # :nodoc:
+ if jd < sg
+ a = jd
+ else
+ x = ((jd - 1867216.25) / 36524.25).floor
+ a = jd + 1 + x - (x / 4.0).floor
+ end
+ b = a + 1524
+ c = ((b - 122.1) / 365.25).floor
+ d = (365.25 * c).floor
+ e = ((b - d) / 30.6001).floor
+ dom = b - d - (30.6001 * e).floor
+ if e <= 13
+ m = e - 1
+ y = c - 4716
+ else
+ m = e - 13
+ y = c - 4715
+ end
+ return y, m, dom
+ end
+
+ # Convert a Commercial Date to a Julian Day Number.
+ #
+ # +y+, +w+, and +d+ are the (commercial) year, week of the year,
+ # and day of the week of the Commercial Date to convert.
+ # +sg+ specifies the Day of Calendar Reform.
+ def commercial_to_jd(y, w, d, sg=GREGORIAN) # :nodoc:
+ j = find_fdoy(y, sg) + 3
+ (j - (((j - 1) + 1) % 7)) +
+ 7 * (w - 1) +
+ (d - 1)
+ end
+
+ # Convert a Julian Day Number to a Commercial Date
+ #
+ # +jd+ is the Julian Day Number to convert.
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # Returns the corresponding Commercial Date as
+ # [commercial_year, week_of_year, day_of_week]
+ def jd_to_commercial(jd, sg=GREGORIAN) # :nodoc:
+ a = jd_to_civil(jd - 3, sg)[0]
+ y = if jd >= commercial_to_jd(a + 1, 1, 1, sg) then a + 1 else a end
+ w = 1 + ((jd - commercial_to_jd(y, 1, 1, sg)) / 7).floor
+ d = (jd + 1) % 7
+ d = 7 if d == 0
+ return y, w, d
+ end
+
+ def weeknum_to_jd(y, w, d, f=0, sg=GREGORIAN) # :nodoc:
+ a = find_fdoy(y, sg) + 6
+ (a - ((a - f) + 1) % 7 - 7) + 7 * w + d
+ end
+
+ def jd_to_weeknum(jd, f=0, sg=GREGORIAN) # :nodoc:
+ y, m, d = jd_to_civil(jd, sg)
+ a = find_fdoy(y, sg) + 6
+ w, d = (jd - (a - ((a - f) + 1) % 7) + 7).divmod(7)
+ return y, w, d
+ end
+
+ def nth_kday_to_jd(y, m, n, k, sg=GREGORIAN) # :nodoc:
+ j = if n > 0
+ find_fdom(y, m, sg) - 1
+ else
+ find_ldom(y, m, sg) + 7
+ end
+ (j - (((j - k) + 1) % 7)) + 7 * n
+ end
+
+ def jd_to_nth_kday(jd, sg=GREGORIAN) # :nodoc:
+ y, m, d = jd_to_civil(jd, sg)
+ j = find_fdom(y, m, sg)
+ return y, m, ((jd - j) / 7).floor + 1, jd_to_wday(jd)
+ end
+
+ # Convert an Astronomical Julian Day Number to a (civil) Julian
+ # Day Number.
+ #
+ # +ajd+ is the Astronomical Julian Day Number to convert.
+ # +of+ is the offset from UTC as a fraction of a day (defaults to 0).
+ #
+ # Returns the (civil) Julian Day Number as [day_number,
+ # fraction] where +fraction+ is always 1/2.
+ def ajd_to_jd(ajd, of=0) (ajd + of + HALF_DAYS_IN_DAY).divmod(1) end # :nodoc:
+
+ # Convert a (civil) Julian Day Number to an Astronomical Julian
+ # Day Number.
+ #
+ # +jd+ is the Julian Day Number to convert, and +fr+ is a
+ # fractional day.
+ # +of+ is the offset from UTC as a fraction of a day (defaults to 0).
+ #
+ # Returns the Astronomical Julian Day Number as a single
+ # numeric value.
+ def jd_to_ajd(jd, fr, of=0) jd + fr - of - HALF_DAYS_IN_DAY end # :nodoc:
+
+ # Convert a fractional day +fr+ to [hours, minutes, seconds,
+ # fraction_of_a_second]
+ def day_fraction_to_time(fr) # :nodoc:
+ ss, fr = fr.divmod(SECONDS_IN_DAY) # 4p
+ h, ss = ss.divmod(3600)
+ min, s = ss.divmod(60)
+ return h, min, s, fr * 86400
+ end
+
+ # Convert an +h+ hour, +min+ minutes, +s+ seconds period
+ # to a fractional day.
+ begin
+ Rational(Rational(1, 2), 2) # a challenge
+
+ def time_to_day_fraction(h, min, s)
+ Rational(h * 3600 + min * 60 + s, 86400) # 4p
+ end
+ rescue
+ def time_to_day_fraction(h, min, s)
+ if Integer === h && Integer === min && Integer === s
+ Rational(h * 3600 + min * 60 + s, 86400) # 4p
+ else
+ (h * 3600 + min * 60 + s).to_r/86400 # 4p
+ end
+ end
+ end
+
+ # Convert an Astronomical Modified Julian Day Number to an
+ # Astronomical Julian Day Number.
+ def amjd_to_ajd(amjd) amjd + MJD_EPOCH_IN_AJD end # :nodoc:
+
+ # Convert an Astronomical Julian Day Number to an
+ # Astronomical Modified Julian Day Number.
+ def ajd_to_amjd(ajd) ajd - MJD_EPOCH_IN_AJD end # :nodoc:
+
+ # Convert a Modified Julian Day Number to a Julian
+ # Day Number.
+ def mjd_to_jd(mjd) mjd + MJD_EPOCH_IN_CJD end # :nodoc:
+
+ # Convert a Julian Day Number to a Modified Julian Day
+ # Number.
+ def jd_to_mjd(jd) jd - MJD_EPOCH_IN_CJD end # :nodoc:
+
+ # Convert a count of the number of days since the adoption
+ # of the Gregorian Calendar (in Italy) to a Julian Day Number.
+ def ld_to_jd(ld) ld + LD_EPOCH_IN_CJD end # :nodoc:
+
+ # Convert a Julian Day Number to the number of days since
+ # the adoption of the Gregorian Calendar (in Italy).
+ def jd_to_ld(jd) jd - LD_EPOCH_IN_CJD end # :nodoc:
+
+ # Convert a Julian Day Number to the day of the week.
+ #
+ # Sunday is day-of-week 0; Saturday is day-of-week 6.
+ def jd_to_wday(jd) (jd + 1) % 7 end # :nodoc:
+
+ # Is +jd+ a valid Julian Day Number?
+ #
+ # If it is, returns it. In fact, any value is treated as a valid
+ # Julian Day Number.
+ def _valid_jd? (jd, sg=GREGORIAN) jd end # :nodoc:
+
+ # Do the year +y+ and day-of-year +d+ make a valid Ordinal Date?
+ # Returns the corresponding Julian Day Number if they do, or
+ # nil if they don't.
+ #
+ # +d+ can be a negative number, in which case it counts backwards
+ # from the end of the year (-1 being the last day of the year).
+ # No year wraparound is performed, however, so valid values of
+ # +d+ are -365 .. -1, 1 .. 365 on a non-leap-year,
+ # -366 .. -1, 1 .. 366 on a leap year.
+ # A date falling in the period skipped in the Day of Calendar Reform
+ # adjustment is not valid.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def _valid_ordinal? (y, d, sg=GREGORIAN) # :nodoc:
+ if d < 0
+ j = find_ldoy(y, sg)
+ ny, nd = jd_to_ordinal(j + d + 1, sg)
+ return unless ny == y
+ d = nd
+ end
+ jd = ordinal_to_jd(y, d, sg)
+ return unless [y, d] == jd_to_ordinal(jd, sg)
+ jd
+ end
+
+ # Do year +y+, month +m+, and day-of-month +d+ make a
+ # valid Civil Date? Returns the corresponding Julian
+ # Day Number if they do, nil if they don't.
+ #
+ # +m+ and +d+ can be negative, in which case they count
+ # backwards from the end of the year and the end of the
+ # month respectively. No wraparound is performed, however,
+ # and invalid values cause an ArgumentError to be raised.
+ # A date falling in the period skipped in the Day of Calendar
+ # Reform adjustment is not valid.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def _valid_civil? (y, m, d, sg=GREGORIAN) # :nodoc:
+ if m < 0
+ m += 13
+ end
+ if d < 0
+ j = find_ldom(y, m, sg)
+ ny, nm, nd = jd_to_civil(j + d + 1, sg)
+ return unless [ny, nm] == [y, m]
+ d = nd
+ end
+ jd = civil_to_jd(y, m, d, sg)
+ return unless [y, m, d] == jd_to_civil(jd, sg)
+ jd
+ end
+
+ # Do year +y+, week-of-year +w+, and day-of-week +d+ make a
+ # valid Commercial Date? Returns the corresponding Julian
+ # Day Number if they do, nil if they don't.
+ #
+ # Monday is day-of-week 1; Sunday is day-of-week 7.
+ #
+ # +w+ and +d+ can be negative, in which case they count
+ # backwards from the end of the year and the end of the
+ # week respectively. No wraparound is performed, however,
+ # and invalid values cause an ArgumentError to be raised.
+ # A date falling in the period skipped in the Day of Calendar
+ # Reform adjustment is not valid.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def _valid_commercial? (y, w, d, sg=GREGORIAN) # :nodoc:
+ if d < 0
+ d += 8
+ end
+ if w < 0
+ ny, nw, nd =
+ jd_to_commercial(commercial_to_jd(y + 1, 1, 1, sg) + w * 7, sg)
+ return unless ny == y
+ w = nw
+ end
+ jd = commercial_to_jd(y, w, d, sg)
+ return unless [y, w, d] == jd_to_commercial(jd, sg)
+ jd
+ end
+
+ def _valid_weeknum? (y, w, d, f, sg=GREGORIAN) # :nodoc:
+ if d < 0
+ d += 7
+ end
+ if w < 0
+ ny, nw, nd, nf =
+ jd_to_weeknum(weeknum_to_jd(y + 1, 1, f, f, sg) + w * 7, f, sg)
+ return unless ny == y
+ w = nw
+ end
+ jd = weeknum_to_jd(y, w, d, f, sg)
+ return unless [y, w, d] == jd_to_weeknum(jd, f, sg)
+ jd
+ end
+
+ def _valid_nth_kday? (y, m, n, k, sg=GREGORIAN) # :nodoc:
+ if k < 0
+ k += 7
+ end
+ if n < 0
+ ny, nm = (y * 12 + m).divmod(12)
+ nm, = (nm + 1) .divmod(1)
+ ny, nm, nn, nk =
+ jd_to_nth_kday(nth_kday_to_jd(ny, nm, 1, k, sg) + n * 7, sg)
+ return unless [ny, nm] == [y, m]
+ n = nn
+ end
+ jd = nth_kday_to_jd(y, m, n, k, sg)
+ return unless [y, m, n, k] == jd_to_nth_kday(jd, sg)
+ jd
+ end
+
+ # Do hour +h+, minute +min+, and second +s+ constitute a valid time?
+ #
+ # If they do, returns their value as a fraction of a day. If not,
+ # returns nil.
+ #
+ # The 24-hour clock is used. Negative values of +h+, +min+, and
+ # +sec+ are treating as counting backwards from the end of the
+ # next larger unit (e.g. a +min+ of -2 is treated as 58). No
+ # wraparound is performed.
+ def _valid_time? (h, min, s) # :nodoc:
+ h += 24 if h < 0
+ min += 60 if min < 0
+ s += 60 if s < 0
+ return unless ((0...24) === h &&
+ (0...60) === min &&
+ (0...60) === s) ||
+ (24 == h &&
+ 0 == min &&
+ 0 == s)
+ time_to_day_fraction(h, min, s)
+ end
+
+ end
+
+ extend t
+ include t
+
+ # Is a year a leap year in the Julian calendar?
+ #
+ # All years divisible by 4 are leap years in the Julian calendar.
+ def self.julian_leap? (y) y % 4 == 0 end
+
+ # Is a year a leap year in the Gregorian calendar?
+ #
+ # All years divisible by 4 are leap years in the Gregorian calendar,
+ # except for years divisible by 100 and not by 400.
+ def self.gregorian_leap? (y) y % 4 == 0 && y % 100 != 0 || y % 400 == 0 end
+
+ class << self; alias_method :leap?, :gregorian_leap? end
+ class << self; alias_method :new!, :new end
+
+ def self.valid_jd? (jd, sg=ITALY)
+ !!_valid_jd?(jd, sg)
+ end
+
+ def self.valid_ordinal? (y, d, sg=ITALY)
+ !!_valid_ordinal?(y, d, sg)
+ end
+
+ def self.valid_civil? (y, m, d, sg=ITALY)
+ !!_valid_civil?(y, m, d, sg)
+ end
+
+ class << self; alias_method :valid_date?, :valid_civil? end
+
+ def self.valid_commercial? (y, w, d, sg=ITALY)
+ !!_valid_commercial?(y, w, d, sg)
+ end
+
+ def self.valid_weeknum? (y, w, d, f, sg=ITALY) # :nodoc:
+ !!_valid_weeknum?(y, w, d, f, sg)
+ end
+
+ private_class_method :valid_weeknum?
+
+ def self.valid_nth_kday? (y, m, n, k, sg=ITALY) # :nodoc:
+ !!_valid_nth_kday?(y, m, n, k, sg)
+ end
+
+ private_class_method :valid_nth_kday?
+
+ def self.valid_time? (h, min, s) # :nodoc:
+ !!_valid_time?(h, min, s)
+ end
+
+ private_class_method :valid_time?
+
+ # Create a new Date object from a Julian Day Number.
+ #
+ # +jd+ is the Julian Day Number; if not specified, it defaults to
+ # 0.
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.jd(jd=0, sg=ITALY)
+ jd = _valid_jd?(jd, sg)
+ new!(jd_to_ajd(jd, 0, 0), 0, sg)
+ end
+
+ # Create a new Date object from an Ordinal Date, specified
+ # by year +y+ and day-of-year +d+. +d+ can be negative,
+ # in which it counts backwards from the end of the year.
+ # No year wraparound is performed, however. An invalid
+ # value for +d+ results in an ArgumentError being raised.
+ #
+ # +y+ defaults to -4712, and +d+ to 1; this is Julian Day
+ # Number day 0.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.ordinal(y=-4712, d=1, sg=ITALY)
+ unless jd = _valid_ordinal?(y, d, sg)
+ raise ArgumentError, 'invalid date'
+ end
+ new!(jd_to_ajd(jd, 0, 0), 0, sg)
+ end
+
+ # Create a new Date object for the Civil Date specified by
+ # year +y+, month +m+, and day-of-month +d+.
+ #
+ # +m+ and +d+ can be negative, in which case they count
+ # backwards from the end of the year and the end of the
+ # month respectively. No wraparound is performed, however,
+ # and invalid values cause an ArgumentError to be raised.
+ # can be negative
+ #
+ # +y+ defaults to -4712, +m+ to 1, and +d+ to 1; this is
+ # Julian Day Number day 0.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.civil(y=-4712, m=1, d=1, sg=ITALY)
+ unless jd = _valid_civil?(y, m, d, sg)
+ raise ArgumentError, 'invalid date'
+ end
+ new!(jd_to_ajd(jd, 0, 0), 0, sg)
+ end
+
+ class << self; alias_method :new, :civil end
+
+ # Create a new Date object for the Commercial Date specified by
+ # year +y+, week-of-year +w+, and day-of-week +d+.
+ #
+ # Monday is day-of-week 1; Sunday is day-of-week 7.
+ #
+ # +w+ and +d+ can be negative, in which case they count
+ # backwards from the end of the year and the end of the
+ # week respectively. No wraparound is performed, however,
+ # and invalid values cause an ArgumentError to be raised.
+ #
+ # +y+ defaults to -4712, +w+ to 1, and +d+ to 1; this is
+ # Julian Day Number day 0.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.commercial(y=-4712, w=1, d=1, sg=ITALY)
+ unless jd = _valid_commercial?(y, w, d, sg)
+ raise ArgumentError, 'invalid date'
+ end
+ new!(jd_to_ajd(jd, 0, 0), 0, sg)
+ end
+
+ def self.weeknum(y=-4712, w=0, d=1, f=0, sg=ITALY)
+ unless jd = _valid_weeknum?(y, w, d, f, sg)
+ raise ArgumentError, 'invalid date'
+ end
+ new!(jd_to_ajd(jd, 0, 0), 0, sg)
+ end
+
+ private_class_method :weeknum
+
+ def self.nth_kday(y=-4712, m=1, n=1, k=1, sg=ITALY)
+ unless jd = _valid_nth_kday?(y, m, n, k, sg)
+ raise ArgumentError, 'invalid date'
+ end
+ new!(jd_to_ajd(jd, 0, 0), 0, sg)
+ end
+
+ private_class_method :nth_kday
+
+ def self.rewrite_frags(elem) # :nodoc:
+ elem ||= {}
+ if seconds = elem[:seconds]
+ d, fr = seconds.divmod(86400)
+ h, fr = fr.divmod(3600)
+ min, fr = fr.divmod(60)
+ s, fr = fr.divmod(1)
+ elem[:jd] = UNIX_EPOCH_IN_CJD + d
+ elem[:hour] = h
+ elem[:min] = min
+ elem[:sec] = s
+ elem[:sec_fraction] = fr
+ elem.delete(:seconds)
+ elem.delete(:offset)
+ end
+ elem
+ end
+
+ private_class_method :rewrite_frags
+
+ def self.complete_frags(elem) # :nodoc:
+ i = 0
+ g = [[:time, [:hour, :min, :sec]],
+ [nil, [:jd]],
+ [:ordinal, [:year, :yday, :hour, :min, :sec]],
+ [:civil, [:year, :mon, :mday, :hour, :min, :sec]],
+ [:commercial, [:cwyear, :cweek, :cwday, :hour, :min, :sec]],
+ [:wday, [:wday, :hour, :min, :sec]],
+ [:wnum0, [:year, :wnum0, :wday, :hour, :min, :sec]],
+ [:wnum1, [:year, :wnum1, :wday, :hour, :min, :sec]],
+ [nil, [:cwyear, :cweek, :wday, :hour, :min, :sec]],
+ [nil, [:year, :wnum0, :cwday, :hour, :min, :sec]],
+ [nil, [:year, :wnum1, :cwday, :hour, :min, :sec]]].
+ collect{|k, a| e = elem.values_at(*a).compact; [k, a, e]}.
+ select{|k, a, e| e.size > 0}.
+ sort_by{|k, a, e| [e.size, i -= 1]}.last
+
+ d = nil
+
+ if g && g[0] && (g[1].size - g[2].size) != 0
+ d ||= Date.today
+
+ case g[0]
+ when :ordinal
+ elem[:year] ||= d.year
+ elem[:yday] ||= 1
+ when :civil
+ g[1].each do |e|
+ break if elem[e]
+ elem[e] = d.__send__(e)
+ end
+ elem[:mon] ||= 1
+ elem[:mday] ||= 1
+ when :commercial
+ g[1].each do |e|
+ break if elem[e]
+ elem[e] = d.__send__(e)
+ end
+ elem[:cweek] ||= 1
+ elem[:cwday] ||= 1
+ when :wday
+ elem[:jd] ||= (d - d.wday + elem[:wday]).jd
+ when :wnum0
+ g[1].each do |e|
+ break if elem[e]
+ elem[e] = d.__send__(e)
+ end
+ elem[:wnum0] ||= 0
+ elem[:wday] ||= 0
+ when :wnum1
+ g[1].each do |e|
+ break if elem[e]
+ elem[e] = d.__send__(e)
+ end
+ elem[:wnum1] ||= 0
+ elem[:wday] ||= 0
+ end
+ end
+
+ if g && g[0] == :time
+ if self <= DateTime
+ d ||= Date.today
+ elem[:jd] ||= d.jd
+ end
+ end
+
+ elem[:hour] ||= 0
+ elem[:min] ||= 0
+ elem[:sec] ||= 0
+ elem[:sec] = [elem[:sec], 59].min
+
+ elem
+ end
+
+ private_class_method :complete_frags
+
+ def self.valid_date_frags?(elem, sg) # :nodoc:
+ catch :jd do
+ a = elem.values_at(:jd)
+ if a.all?
+ if jd = _valid_jd?(*(a << sg))
+ throw :jd, jd
+ end
+ end
+
+ a = elem.values_at(:year, :yday)
+ if a.all?
+ if jd = _valid_ordinal?(*(a << sg))
+ throw :jd, jd
+ end
+ end
+
+ a = elem.values_at(:year, :mon, :mday)
+ if a.all?
+ if jd = _valid_civil?(*(a << sg))
+ throw :jd, jd
+ end
+ end
+
+ a = elem.values_at(:cwyear, :cweek, :cwday)
+ if a[2].nil? && elem[:wday]
+ a[2] = elem[:wday].nonzero? || 7
+ end
+ if a.all?
+ if jd = _valid_commercial?(*(a << sg))
+ throw :jd, jd
+ end
+ end
+
+ a = elem.values_at(:year, :wnum0, :wday)
+ if a[2].nil? && elem[:cwday]
+ a[2] = elem[:cwday] % 7
+ end
+ if a.all?
+ if jd = _valid_weeknum?(*(a << 0 << sg))
+ throw :jd, jd
+ end
+ end
+
+ a = elem.values_at(:year, :wnum1, :wday)
+ if a[2]
+ a[2] = (a[2] - 1) % 7
+ end
+ if a[2].nil? && elem[:cwday]
+ a[2] = (elem[:cwday] - 1) % 7
+ end
+ if a.all?
+ if jd = _valid_weeknum?(*(a << 1 << sg))
+ throw :jd, jd
+ end
+ end
+ end
+ end
+
+ private_class_method :valid_date_frags?
+
+ def self.valid_time_frags? (elem) # :nodoc:
+ h, min, s = elem.values_at(:hour, :min, :sec)
+ _valid_time?(h, min, s)
+ end
+
+ private_class_method :valid_time_frags?
+
+ def self.new_by_frags(elem, sg) # :nodoc:
+ elem = rewrite_frags(elem)
+ elem = complete_frags(elem)
+ unless jd = valid_date_frags?(elem, sg)
+ raise ArgumentError, 'invalid date'
+ end
+ new!(jd_to_ajd(jd, 0, 0), 0, sg)
+ end
+
+ private_class_method :new_by_frags
+
+ # Create a new Date object by parsing from a String
+ # according to a specified format.
+ #
+ # +str+ is a String holding a date representation.
+ # +fmt+ is the format that the date is in. See
+ # date/format.rb for details on supported formats.
+ #
+ # The default +str+ is '-4712-01-01', and the default
+ # +fmt+ is '%F', which means Year-Month-Day_of_Month.
+ # This gives Julian Day Number day 0.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # An ArgumentError will be raised if +str+ cannot be
+ # parsed.
+ def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
+ elem = _strptime(str, fmt)
+ new_by_frags(elem, sg)
+ end
+
+ # Create a new Date object by parsing from a String,
+ # without specifying the format.
+ #
+ # +str+ is a String holding a date representation.
+ # +comp+ specifies whether to interpret 2-digit years
+ # as 19XX (>= 69) or 20XX (< 69); the default is not to.
+ # The method will attempt to parse a date from the String
+ # using various heuristics; see #_parse in date/format.rb
+ # for more details. If parsing fails, an ArgumentError
+ # will be raised.
+ #
+ # The default +str+ is '-4712-01-01'; this is Julian
+ # Day Number day 0.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.parse(str='-4712-01-01', comp=true, sg=ITALY)
+ elem = _parse(str, comp)
+ new_by_frags(elem, sg)
+ end
+
+ def self.iso8601(str='-4712-01-01', sg=ITALY) # :nodoc:
+ elem = _iso8601(str)
+ new_by_frags(elem, sg)
+ end
+
+ def self.rfc3339(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
+ elem = _rfc3339(str)
+ new_by_frags(elem, sg)
+ end
+
+ def self.xmlschema(str='-4712-01-01', sg=ITALY) # :nodoc:
+ elem = _xmlschema(str)
+ new_by_frags(elem, sg)
+ end
+
+ def self.rfc2822(str='Mon, 1 Jan -4712 00:00:00 +0000', sg=ITALY) # :nodoc:
+ elem = _rfc2822(str)
+ new_by_frags(elem, sg)
+ end
+
+ class << self; alias_method :rfc822, :rfc2822 end
+
+ def self.httpdate(str='Mon, 01 Jan -4712 00:00:00 GMT', sg=ITALY) # :nodoc:
+ elem = _httpdate(str)
+ new_by_frags(elem, sg)
+ end
+
+ def self.jisx0301(str='-4712-01-01', sg=ITALY) # :nodoc:
+ elem = _jisx0301(str)
+ new_by_frags(elem, sg)
+ end
+
+ class << self
+
+ def once(*ids) # :nodoc: -- restricted
+ for id in ids
+ module_eval <<-"end;"
+ alias_method :__#{id.object_id}__, :#{id.to_s}
+ private :__#{id.object_id}__
+ def #{id.to_s}(*args)
+ @__ca__[#{id.object_id}] ||= __#{id.object_id}__(*args)
+ end
+ end;
+ end
+ end
+
+ private :once
+
+ end
+
+ # *NOTE* this is the documentation for the method new!(). If
+ # you are reading this as the documentation for new(), that is
+ # because rdoc doesn't fully support the aliasing of the
+ # initialize() method.
+ # new() is in
+ # fact an alias for #civil(): read the documentation for that
+ # method instead.
+ #
+ # Create a new Date object.
+ #
+ # +ajd+ is the Astronomical Julian Day Number.
+ # +of+ is the offset from UTC as a fraction of a day.
+ # Both default to 0.
+ #
+ # +sg+ specifies the Day of Calendar Reform to use for this
+ # Date object.
+ #
+ # Using one of the factory methods such as Date::civil is
+ # generally easier and safer.
+ def initialize(ajd=0, of=0, sg=ITALY)
+ @ajd, @of, @sg = ajd, of, sg
+ @__ca__ = {}
+ end
+
+ # Get the date as an Astronomical Julian Day Number.
+ def ajd() @ajd end
+
+ # Get the date as an Astronomical Modified Julian Day Number.
+ def amjd() ajd_to_amjd(@ajd) end
+
+ once :amjd
+
+ # Get the date as a Julian Day Number.
+ def jd() ajd_to_jd(@ajd, @of)[0] end
+
+ # Get any fractional day part of the date.
+ def day_fraction() ajd_to_jd(@ajd, @of)[1] end
+
+ # Get the date as a Modified Julian Day Number.
+ def mjd() jd_to_mjd(jd) end
+
+ # Get the date as the number of days since the Day of Calendar
+ # Reform (in Italy and the Catholic countries).
+ def ld() jd_to_ld(jd) end
+
+ once :jd, :day_fraction, :mjd, :ld
+
+ # Get the date as a Civil Date, [year, month, day_of_month]
+ def civil() jd_to_civil(jd, @sg) end # :nodoc:
+
+ # Get the date as an Ordinal Date, [year, day_of_year]
+ def ordinal() jd_to_ordinal(jd, @sg) end # :nodoc:
+
+ # Get the date as a Commercial Date, [year, week_of_year, day_of_week]
+ def commercial() jd_to_commercial(jd, @sg) end # :nodoc:
+
+ def weeknum0() jd_to_weeknum(jd, 0, @sg) end # :nodoc:
+ def weeknum1() jd_to_weeknum(jd, 1, @sg) end # :nodoc:
+
+ once :civil, :ordinal, :commercial, :weeknum0, :weeknum1
+ private :civil, :ordinal, :commercial, :weeknum0, :weeknum1
+
+ # Get the year of this date.
+ def year() civil[0] end
+
+ # Get the day-of-the-year of this date.
+ #
+ # January 1 is day-of-the-year 1
+ def yday() ordinal[1] end
+
+ # Get the month of this date.
+ #
+ # January is month 1.
+ def mon() civil[1] end
+
+ # Get the day-of-the-month of this date.
+ def mday() civil[2] end
+
+ alias_method :month, :mon
+ alias_method :day, :mday
+
+ def wnum0() weeknum0[1] end # :nodoc:
+ def wnum1() weeknum1[1] end # :nodoc:
+
+ private :wnum0, :wnum1
+
+ # Get the time of this date as [hours, minutes, seconds,
+ # fraction_of_a_second]
+ def time() day_fraction_to_time(day_fraction) end # :nodoc:
+
+ once :time
+ private :time
+
+ # Get the hour of this date.
+ def hour() time[0] end
+
+ # Get the minute of this date.
+ def min() time[1] end
+
+ # Get the second of this date.
+ def sec() time[2] end
+
+ # Get the fraction-of-a-second of this date.
+ def sec_fraction() time[3] end
+
+ alias_method :minute, :min
+ alias_method :second, :sec
+ alias_method :second_fraction, :sec_fraction
+
+ private :hour, :min, :sec, :sec_fraction,
+ :minute, :second, :second_fraction
+
+ def zone() strftime('%:z') end
+
+ private :zone
+
+ # Get the commercial year of this date. See *Commercial* *Date*
+ # in the introduction for how this differs from the normal year.
+ def cwyear() commercial[0] end
+
+ # Get the commercial week of the year of this date.
+ def cweek() commercial[1] end
+
+ # Get the commercial day of the week of this date. Monday is
+ # commercial day-of-week 1; Sunday is commercial day-of-week 7.
+ def cwday() commercial[2] end
+
+ # Get the week day of this date. Sunday is day-of-week 0;
+ # Saturday is day-of-week 6.
+ def wday() jd_to_wday(jd) end
+
+ once :wday
+
+=begin
+ MONTHNAMES.each_with_index do |n, i|
+ if n
+ define_method(n.downcase + '?'){mon == i}
+ end
+ end
+=end
+
+ DAYNAMES.each_with_index do |n, i|
+ define_method(n.downcase + '?'){wday == i}
+ end
+
+ def nth_kday? (n, k)
+ k == wday && jd === nth_kday_to_jd(year, mon, n, k, start)
+ end
+
+ private :nth_kday?
+
+ # Is the current date old-style (Julian Calendar)?
+ def julian? () jd < @sg end
+
+ # Is the current date new-style (Gregorian Calendar)?
+ def gregorian? () !julian? end
+
+ once :julian?, :gregorian?
+
+ def fix_style # :nodoc:
+ if julian?
+ then self.class::JULIAN
+ else self.class::GREGORIAN end
+ end
+
+ private :fix_style
+
+ # Is this a leap year?
+ def leap?
+ jd_to_civil(civil_to_jd(year, 3, 1, fix_style) - 1,
+ fix_style)[-1] == 29
+ end
+
+ once :leap?
+
+ # When is the Day of Calendar Reform for this Date object?
+ def start() @sg end
+
+ # Create a copy of this Date object using a new Day of Calendar Reform.
+ def new_start(sg=self.class::ITALY) self.class.new!(@ajd, @of, sg) end
+
+ # Create a copy of this Date object that uses the Italian/Catholic
+ # Day of Calendar Reform.
+ def italy() new_start(self.class::ITALY) end
+
+ # Create a copy of this Date object that uses the English/Colonial
+ # Day of Calendar Reform.
+ def england() new_start(self.class::ENGLAND) end
+
+ # Create a copy of this Date object that always uses the Julian
+ # Calendar.
+ def julian() new_start(self.class::JULIAN) end
+
+ # Create a copy of this Date object that always uses the Gregorian
+ # Calendar.
+ def gregorian() new_start(self.class::GREGORIAN) end
+
+ def offset() @of end
+
+ def new_offset(of=0)
+ if String === of
+ of = Rational(zone_to_diff(of) || 0, 86400)
+ end
+ self.class.new!(@ajd, of, @sg)
+ end
+
+ private :offset, :new_offset
+
+ # Return a new Date object that is +n+ days later than the
+ # current one.
+ #
+ # +n+ may be a negative value, in which case the new Date
+ # is earlier than the current one; however, #-() might be
+ # more intuitive.
+ #
+ # If +n+ is not a Numeric, a TypeError will be thrown. In
+ # particular, two Dates cannot be added to each other.
+ def + (n)
+ case n
+ when Numeric; return self.class.new!(@ajd + n, @of, @sg)
+ end
+ raise TypeError, 'expected numeric'
+ end
+
+ # If +x+ is a Numeric value, create a new Date object that is
+ # +x+ days earlier than the current one.
+ #
+ # If +x+ is a Date, return the number of days between the
+ # two dates; or, more precisely, how many days later the current
+ # date is than +x+.
+ #
+ # If +x+ is neither Numeric nor a Date, a TypeError is raised.
+ def - (x)
+ case x
+ when Numeric; return self.class.new!(@ajd - x, @of, @sg)
+ when Date; return @ajd - x.ajd
+ end
+ raise TypeError, 'expected numeric or date'
+ end
+
+ # Compare this date with another date.
+ #
+ # +other+ can also be a Numeric value, in which case it is
+ # interpreted as an Astronomical Julian Day Number.
+ #
+ # Comparison is by Astronomical Julian Day Number, including
+ # fractional days. This means that both the time and the
+ # timezone offset are taken into account when comparing
+ # two DateTime instances. When comparing a DateTime instance
+ # with a Date instance, the time of the latter will be
+ # considered as falling on midnight UTC.
+ def <=> (other)
+ case other
+ when Numeric; return @ajd <=> other
+ when Date; return @ajd <=> other.ajd
+ end
+ nil
+ end
+
+ # The relationship operator for Date.
+ #
+ # Compares dates by Julian Day Number. When comparing
+ # two DateTime instances, or a DateTime with a Date,
+ # the instances will be regarded as equivalent if they
+ # fall on the same date in local time.
+ def === (other)
+ case other
+ when Numeric; return jd == other
+ when Date; return jd == other.jd
+ end
+ false
+ end
+
+ def next_day(n=1) self + n end
+ def prev_day(n=1) self - n end
+
+ # Return a new Date one day after this one.
+ def next() next_day end
+
+ alias_method :succ, :next
+
+ # Return a new Date object that is +n+ months later than
+ # the current one.
+ #
+ # If the day-of-the-month of the current Date is greater
+ # than the last day of the target month, the day-of-the-month
+ # of the returned Date will be the last day of the target month.
+ def >> (n)
+ y, m = (year * 12 + (mon - 1) + n).divmod(12)
+ m, = (m + 1) .divmod(1)
+ d = mday
+ d -= 1 until jd2 = _valid_civil?(y, m, d, @sg)
+ self + (jd2 - jd)
+ end
+
+ # Return a new Date object that is +n+ months earlier than
+ # the current one.
+ #
+ # If the day-of-the-month of the current Date is greater
+ # than the last day of the target month, the day-of-the-month
+ # of the returned Date will be the last day of the target month.
+ def << (n) self >> -n end
+
+ def next_month(n=1) self >> n end
+ def prev_month(n=1) self << n end
+
+ def next_year(n=1) self >> n * 12 end
+ def prev_year(n=1) self << n * 12 end
+
+ require 'enumerator'
+
+ # Step the current date forward +step+ days at a
+ # time (or backward, if +step+ is negative) until
+ # we reach +limit+ (inclusive), yielding the resultant
+ # date at each step.
+ def step(limit, step=1) # :yield: date
+=begin
+ if step.zero?
+ raise ArgumentError, "step can't be 0"
+ end
+=end
+ unless block_given?
+ return to_enum(:step, limit, step)
+ end
+ da = self
+ op = %w(- <= >=)[step <=> 0]
+ while da.__send__(op, limit)
+ yield da
+ da += step
+ end
+ self
+ end
+
+ # Step forward one day at a time until we reach +max+
+ # (inclusive), yielding each date as we go.
+ def upto(max, &block) # :yield: date
+ step(max, +1, &block)
+ end
+
+ # Step backward one day at a time until we reach +min+
+ # (inclusive), yielding each date as we go.
+ def downto(min, &block) # :yield: date
+ step(min, -1, &block)
+ end
+
+ # Is this Date equal to +other+?
+ #
+ # +other+ must both be a Date object, and represent the same date.
+ def eql? (other) Date === other && self == other end
+
+ # Calculate a hash value for this date.
+ def hash() @ajd.hash end
+
+ # Return internal object state as a programmer-readable string.
+ def inspect
+ format('#<%s: %s (%s,%s,%s)>', self.class, to_s, @ajd, @of, @sg)
+ end
+
+ # Return the date as a human-readable string.
+ #
+ # The format used is YYYY-MM-DD.
+ def to_s() format('%.4d-%02d-%02d', year, mon, mday) end # 4p
+
+ # Dump to Marshal format.
+ def marshal_dump() [@ajd, @of, @sg] end
+
+ # Load from Marshal format.
+ def marshal_load(a)
+ @ajd, @of, @sg, = a
+ @__ca__ = {}
+ end
+
+end
+
+# Class representing a date and time.
+#
+# See the documentation to the file date.rb for an overview.
+#
+# DateTime objects are immutable once created.
+#
+# == Other methods.
+#
+# The following methods are defined in Date, but declared private
+# there. They are made public in DateTime. They are documented
+# here.
+#
+# === hour()
+#
+# Get the hour-of-the-day of the time. This is given
+# using the 24-hour clock, counting from midnight. The first
+# hour after midnight is hour 0; the last hour of the day is
+# hour 23.
+#
+# === min()
+#
+# Get the minute-of-the-hour of the time.
+#
+# === sec()
+#
+# Get the second-of-the-minute of the time.
+#
+# === sec_fraction()
+#
+# Get the fraction of a second of the time. This is returned as
+# a +Rational+.
+#
+# === zone()
+#
+# Get the time zone as a String. This is representation of the
+# time offset such as "+1000", not the true time-zone name.
+#
+# === offset()
+#
+# Get the time zone offset as a fraction of a day. This is returned
+# as a +Rational+.
+#
+# === new_offset(of=0)
+#
+# Create a new DateTime object, identical to the current one, except
+# with a new time zone offset of +of+. +of+ is the new offset from
+# UTC as a fraction of a day.
+#
+class DateTime < Date
+
+ # Create a new DateTime object corresponding to the specified
+ # Julian Day Number +jd+ and hour +h+, minute +min+, second +s+.
+ #
+ # The 24-hour clock is used. Negative values of +h+, +min+, and
+ # +sec+ are treating as counting backwards from the end of the
+ # next larger unit (e.g. a +min+ of -2 is treated as 58). No
+ # wraparound is performed. If an invalid time portion is specified,
+ # an ArgumentError is raised.
+ #
+ # +of+ is the offset from UTC as a fraction of a day (defaults to 0).
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # All day/time values default to 0.
+ def self.jd(jd=0, h=0, min=0, s=0, of=0, sg=ITALY)
+ unless (jd = _valid_jd?(jd, sg)) &&
+ (fr = _valid_time?(h, min, s))
+ raise ArgumentError, 'invalid date'
+ end
+ if String === of
+ of = Rational(zone_to_diff(of) || 0, 86400)
+ end
+ new!(jd_to_ajd(jd, fr, of), of, sg)
+ end
+
+ # Create a new DateTime object corresponding to the specified
+ # Ordinal Date and hour +h+, minute +min+, second +s+.
+ #
+ # The 24-hour clock is used. Negative values of +h+, +min+, and
+ # +sec+ are treating as counting backwards from the end of the
+ # next larger unit (e.g. a +min+ of -2 is treated as 58). No
+ # wraparound is performed. If an invalid time portion is specified,
+ # an ArgumentError is raised.
+ #
+ # +of+ is the offset from UTC as a fraction of a day (defaults to 0).
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # +y+ defaults to -4712, and +d+ to 1; this is Julian Day Number
+ # day 0. The time values default to 0.
+ def self.ordinal(y=-4712, d=1, h=0, min=0, s=0, of=0, sg=ITALY)
+ unless (jd = _valid_ordinal?(y, d, sg)) &&
+ (fr = _valid_time?(h, min, s))
+ raise ArgumentError, 'invalid date'
+ end
+ if String === of
+ of = Rational(zone_to_diff(of) || 0, 86400)
+ end
+ new!(jd_to_ajd(jd, fr, of), of, sg)
+ end
+
+ # Create a new DateTime object corresponding to the specified
+ # Civil Date and hour +h+, minute +min+, second +s+.
+ #
+ # The 24-hour clock is used. Negative values of +h+, +min+, and
+ # +sec+ are treating as counting backwards from the end of the
+ # next larger unit (e.g. a +min+ of -2 is treated as 58). No
+ # wraparound is performed. If an invalid time portion is specified,
+ # an ArgumentError is raised.
+ #
+ # +of+ is the offset from UTC as a fraction of a day (defaults to 0).
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # +y+ defaults to -4712, +m+ to 1, and +d+ to 1; this is Julian Day
+ # Number day 0. The time values default to 0.
+ def self.civil(y=-4712, m=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY)
+ unless (jd = _valid_civil?(y, m, d, sg)) &&
+ (fr = _valid_time?(h, min, s))
+ raise ArgumentError, 'invalid date'
+ end
+ if String === of
+ of = Rational(zone_to_diff(of) || 0, 86400)
+ end
+ new!(jd_to_ajd(jd, fr, of), of, sg)
+ end
+
+ class << self; alias_method :new, :civil end
+
+ # Create a new DateTime object corresponding to the specified
+ # Commercial Date and hour +h+, minute +min+, second +s+.
+ #
+ # The 24-hour clock is used. Negative values of +h+, +min+, and
+ # +sec+ are treating as counting backwards from the end of the
+ # next larger unit (e.g. a +min+ of -2 is treated as 58). No
+ # wraparound is performed. If an invalid time portion is specified,
+ # an ArgumentError is raised.
+ #
+ # +of+ is the offset from UTC as a fraction of a day (defaults to 0).
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # +y+ defaults to -4712, +w+ to 1, and +d+ to 1; this is
+ # Julian Day Number day 0.
+ # The time values default to 0.
+ def self.commercial(y=-4712, w=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY)
+ unless (jd = _valid_commercial?(y, w, d, sg)) &&
+ (fr = _valid_time?(h, min, s))
+ raise ArgumentError, 'invalid date'
+ end
+ if String === of
+ of = Rational(zone_to_diff(of) || 0, 86400)
+ end
+ new!(jd_to_ajd(jd, fr, of), of, sg)
+ end
+
+ def self.weeknum(y=-4712, w=0, d=1, f=0, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
+ unless (jd = _valid_weeknum?(y, w, d, f, sg)) &&
+ (fr = _valid_time?(h, min, s))
+ raise ArgumentError, 'invalid date'
+ end
+ if String === of
+ of = Rational(zone_to_diff(of) || 0, 86400)
+ end
+ new!(jd_to_ajd(jd, fr, of), of, sg)
+ end
+
+ private_class_method :weeknum
+
+ def self.nth_kday(y=-4712, m=1, n=1, k=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
+ unless (jd = _valid_nth_kday?(y, m, n, k, sg)) &&
+ (fr = _valid_time?(h, min, s))
+ raise ArgumentError, 'invalid date'
+ end
+ if String === of
+ of = Rational(zone_to_diff(of) || 0, 86400)
+ end
+ new!(jd_to_ajd(jd, fr, of), of, sg)
+ end
+
+ private_class_method :nth_kday
+
+ def self.new_by_frags(elem, sg) # :nodoc:
+ elem = rewrite_frags(elem)
+ elem = complete_frags(elem)
+ unless (jd = valid_date_frags?(elem, sg)) &&
+ (fr = valid_time_frags?(elem))
+ raise ArgumentError, 'invalid date'
+ end
+ fr += (elem[:sec_fraction] || 0) / 86400
+ of = Rational(elem[:offset] || 0, 86400)
+ new!(jd_to_ajd(jd, fr, of), of, sg)
+ end
+
+ private_class_method :new_by_frags
+
+ # Create a new DateTime object by parsing from a String
+ # according to a specified format.
+ #
+ # +str+ is a String holding a date-time representation.
+ # +fmt+ is the format that the date-time is in. See
+ # date/format.rb for details on supported formats.
+ #
+ # The default +str+ is '-4712-01-01T00:00:00+00:00', and the default
+ # +fmt+ is '%FT%T%z'. This gives midnight on Julian Day Number day 0.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ #
+ # An ArgumentError will be raised if +str+ cannot be
+ # parsed.
+ def self.strptime(str='-4712-01-01T00:00:00+00:00', fmt='%FT%T%z', sg=ITALY)
+ elem = _strptime(str, fmt)
+ new_by_frags(elem, sg)
+ end
+
+ # Create a new DateTime object by parsing from a String,
+ # without specifying the format.
+ #
+ # +str+ is a String holding a date-time representation.
+ # +comp+ specifies whether to interpret 2-digit years
+ # as 19XX (>= 69) or 20XX (< 69); the default is not to.
+ # The method will attempt to parse a date-time from the String
+ # using various heuristics; see #_parse in date/format.rb
+ # for more details. If parsing fails, an ArgumentError
+ # will be raised.
+ #
+ # The default +str+ is '-4712-01-01T00:00:00+00:00'; this is Julian
+ # Day Number day 0.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.parse(str='-4712-01-01T00:00:00+00:00', comp=true, sg=ITALY)
+ elem = _parse(str, comp)
+ new_by_frags(elem, sg)
+ end
+
+ def self.iso8601(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
+ elem = _iso8601(str)
+ new_by_frags(elem, sg)
+ end
+
+ def self.rfc3339(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
+ elem = _rfc3339(str)
+ new_by_frags(elem, sg)
+ end
+
+ def self.xmlschema(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
+ elem = _xmlschema(str)
+ new_by_frags(elem, sg)
+ end
+
+ def self.rfc2822(str='Mon, 1 Jan -4712 00:00:00 +0000', sg=ITALY) # :nodoc:
+ elem = _rfc2822(str)
+ new_by_frags(elem, sg)
+ end
+
+ class << self; alias_method :rfc822, :rfc2822 end
+
+ def self.httpdate(str='Mon, 01 Jan -4712 00:00:00 GMT', sg=ITALY) # :nodoc:
+ elem = _httpdate(str)
+ new_by_frags(elem, sg)
+ end
+
+ def self.jisx0301(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
+ elem = _jisx0301(str)
+ new_by_frags(elem, sg)
+ end
+
+ public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset,
+ :minute, :second, :second_fraction
+
+ def to_s # 4p
+ format('%.4d-%02d-%02dT%02d:%02d:%02d%s',
+ year, mon, mday, hour, min, sec, zone)
+ end
+
+end
+
+class Time
+
+ def to_time() getlocal end
+
+ def to_date
+ jd = Date.__send__(:civil_to_jd, year, mon, mday, Date::ITALY)
+ Date.new!(Date.__send__(:jd_to_ajd, jd, 0, 0), 0, Date::ITALY)
+ end
+
+ def to_datetime
+ jd = DateTime.__send__(:civil_to_jd, year, mon, mday, DateTime::ITALY)
+ fr = DateTime.__send__(:time_to_day_fraction, hour, min, [sec, 59].min) +
+ Rational(nsec, 86400_000_000_000)
+ of = Rational(utc_offset, 86400)
+ DateTime.new!(DateTime.__send__(:jd_to_ajd, jd, fr, of),
+ of, DateTime::ITALY)
+ end
+
+end
+
+class Date
+
+ def to_time() Time.local(year, mon, mday) end
+ def to_date() self end
+ def to_datetime() DateTime.new!(jd_to_ajd(jd, 0, 0), @of, @sg) end
+
+ # Create a new Date object representing today.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.today(sg=ITALY)
+ t = Time.now
+ jd = civil_to_jd(t.year, t.mon, t.mday, sg)
+ new!(jd_to_ajd(jd, 0, 0), 0, sg)
+ end
+
+ # Create a new DateTime object representing the current time.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.now(sg=ITALY)
+ t = Time.now
+ jd = civil_to_jd(t.year, t.mon, t.mday, sg)
+ fr = time_to_day_fraction(t.hour, t.min, [t.sec, 59].min) +
+ Rational(t.nsec, 86400_000_000_000)
+ of = Rational(t.utc_offset, 86400)
+ new!(jd_to_ajd(jd, fr, of), of, sg)
+ end
+
+ private_class_method :now
+
+end
+
+class DateTime < Date
+
+ def to_time
+ d = new_offset(0)
+ d.instance_eval do
+ Time.utc(year, mon, mday, hour, min, sec +
+ sec_fraction)
+ end.
+ getlocal
+ end
+
+ def to_date() Date.new!(jd_to_ajd(jd, 0, 0), 0, @sg) end
+ def to_datetime() self end
+
+ private_class_method :today
+ public_class_method :now
+
+end
diff --git a/ruby/lib/date/format.rb b/ruby/lib/date/format.rb
new file mode 100644
index 0000000..a83b298
--- /dev/null
+++ b/ruby/lib/date/format.rb
@@ -0,0 +1,1313 @@
+# format.rb: Written by Tadayoshi Funaba 1999-2008
+# $Id: format.rb,v 2.43 2008-01-17 20:16:31+09 tadf Exp $
+
+class Date
+
+ module Format # :nodoc:
+
+ MONTHS = {
+ 'january' => 1, 'february' => 2, 'march' => 3, 'april' => 4,
+ 'may' => 5, 'june' => 6, 'july' => 7, 'august' => 8,
+ 'september'=> 9, 'october' =>10, 'november' =>11, 'december' =>12
+ }
+
+ DAYS = {
+ 'sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday'=> 3,
+ 'thursday' => 4, 'friday' => 5, 'saturday' => 6
+ }
+
+ ABBR_MONTHS = {
+ 'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
+ 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
+ 'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12
+ }
+
+ ABBR_DAYS = {
+ 'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3,
+ 'thu' => 4, 'fri' => 5, 'sat' => 6
+ }
+
+ ZONES = {
+ 'ut' => 0*3600, 'gmt' => 0*3600, 'est' => -5*3600, 'edt' => -4*3600,
+ 'cst' => -6*3600, 'cdt' => -5*3600, 'mst' => -7*3600, 'mdt' => -6*3600,
+ 'pst' => -8*3600, 'pdt' => -7*3600,
+ 'a' => 1*3600, 'b' => 2*3600, 'c' => 3*3600, 'd' => 4*3600,
+ 'e' => 5*3600, 'f' => 6*3600, 'g' => 7*3600, 'h' => 8*3600,
+ 'i' => 9*3600, 'k' => 10*3600, 'l' => 11*3600, 'm' => 12*3600,
+ 'n' => -1*3600, 'o' => -2*3600, 'p' => -3*3600, 'q' => -4*3600,
+ 'r' => -5*3600, 's' => -6*3600, 't' => -7*3600, 'u' => -8*3600,
+ 'v' => -9*3600, 'w' =>-10*3600, 'x' =>-11*3600, 'y' =>-12*3600,
+ 'z' => 0*3600,
+
+ 'utc' => 0*3600, 'wet' => 0*3600,
+ 'at' => -2*3600, 'brst'=> -2*3600, 'ndt' => -(2*3600+1800),
+ 'art' => -3*3600, 'adt' => -3*3600, 'brt' => -3*3600, 'clst'=> -3*3600,
+ 'nst' => -(3*3600+1800),
+ 'ast' => -4*3600, 'clt' => -4*3600,
+ 'akdt'=> -8*3600, 'ydt' => -8*3600,
+ 'akst'=> -9*3600, 'hadt'=> -9*3600, 'hdt' => -9*3600, 'yst' => -9*3600,
+ 'ahst'=>-10*3600, 'cat' =>-10*3600, 'hast'=>-10*3600, 'hst' =>-10*3600,
+ 'nt' =>-11*3600,
+ 'idlw'=>-12*3600,
+ 'bst' => 1*3600, 'cet' => 1*3600, 'fwt' => 1*3600, 'met' => 1*3600,
+ 'mewt'=> 1*3600, 'mez' => 1*3600, 'swt' => 1*3600, 'wat' => 1*3600,
+ 'west'=> 1*3600,
+ 'cest'=> 2*3600, 'eet' => 2*3600, 'fst' => 2*3600, 'mest'=> 2*3600,
+ 'mesz'=> 2*3600, 'sast'=> 2*3600, 'sst' => 2*3600,
+ 'bt' => 3*3600, 'eat' => 3*3600, 'eest'=> 3*3600, 'msk' => 3*3600,
+ 'msd' => 4*3600, 'zp4' => 4*3600,
+ 'zp5' => 5*3600, 'ist' => (5*3600+1800),
+ 'zp6' => 6*3600,
+ 'wast'=> 7*3600,
+ 'cct' => 8*3600, 'sgt' => 8*3600, 'wadt'=> 8*3600,
+ 'jst' => 9*3600, 'kst' => 9*3600,
+ 'east'=> 10*3600, 'gst' => 10*3600,
+ 'eadt'=> 11*3600,
+ 'idle'=> 12*3600, 'nzst'=> 12*3600, 'nzt' => 12*3600,
+ 'nzdt'=> 13*3600,
+
+ 'afghanistan' => 16200, 'alaskan' => -32400,
+ 'arab' => 10800, 'arabian' => 14400,
+ 'arabic' => 10800, 'atlantic' => -14400,
+ 'aus central' => 34200, 'aus eastern' => 36000,
+ 'azores' => -3600, 'canada central' => -21600,
+ 'cape verde' => -3600, 'caucasus' => 14400,
+ 'cen. australia' => 34200, 'central america' => -21600,
+ 'central asia' => 21600, 'central europe' => 3600,
+ 'central european' => 3600, 'central pacific' => 39600,
+ 'central' => -21600, 'china' => 28800,
+ 'dateline' => -43200, 'e. africa' => 10800,
+ 'e. australia' => 36000, 'e. europe' => 7200,
+ 'e. south america' => -10800, 'eastern' => -18000,
+ 'egypt' => 7200, 'ekaterinburg' => 18000,
+ 'fiji' => 43200, 'fle' => 7200,
+ 'greenland' => -10800, 'greenwich' => 0,
+ 'gtb' => 7200, 'hawaiian' => -36000,
+ 'india' => 19800, 'iran' => 12600,
+ 'jerusalem' => 7200, 'korea' => 32400,
+ 'mexico' => -21600, 'mid-atlantic' => -7200,
+ 'mountain' => -25200, 'myanmar' => 23400,
+ 'n. central asia' => 21600, 'nepal' => 20700,
+ 'new zealand' => 43200, 'newfoundland' => -12600,
+ 'north asia east' => 28800, 'north asia' => 25200,
+ 'pacific sa' => -14400, 'pacific' => -28800,
+ 'romance' => 3600, 'russian' => 10800,
+ 'sa eastern' => -10800, 'sa pacific' => -18000,
+ 'sa western' => -14400, 'samoa' => -39600,
+ 'se asia' => 25200, 'malay peninsula' => 28800,
+ 'south africa' => 7200, 'sri lanka' => 21600,
+ 'taipei' => 28800, 'tasmania' => 36000,
+ 'tokyo' => 32400, 'tonga' => 46800,
+ 'us eastern' => -18000, 'us mountain' => -25200,
+ 'vladivostok' => 36000, 'w. australia' => 28800,
+ 'w. central africa' => 3600, 'w. europe' => 3600,
+ 'west asia' => 18000, 'west pacific' => 36000,
+ 'yakutsk' => 32400
+ }
+
+ [MONTHS, DAYS, ABBR_MONTHS, ABBR_DAYS, ZONES].each do |x|
+ x.freeze
+ end
+
+ class Bag # :nodoc:
+
+ def initialize
+ @elem = {}
+ end
+
+ def method_missing(t, *args, &block)
+ t = t.to_s
+ set = t.chomp!('=')
+ t = t.intern
+ if set
+ @elem[t] = args[0]
+ else
+ @elem[t]
+ end
+ end
+
+ def to_hash
+ @elem.reject{|k, v| /\A_/ =~ k.to_s || v.nil?}
+ end
+
+ end
+
+ end
+
+ def emit(e, f) # :nodoc:
+ case e
+ when Numeric
+ sign = %w(+ + -)[e <=> 0]
+ e = e.abs
+ end
+
+ s = e.to_s
+
+ if f[:s] && f[:p] == '0'
+ f[:w] -= 1
+ end
+
+ if f[:s] && f[:p] == "\s"
+ s[0,0] = sign
+ end
+
+ if f[:p] != '-'
+ s = s.rjust(f[:w], f[:p])
+ end
+
+ if f[:s] && f[:p] != "\s"
+ s[0,0] = sign
+ end
+
+ s = s.upcase if f[:u]
+ s = s.downcase if f[:d]
+ s
+ end
+
+ def emit_w(e, w, f) # :nodoc:
+ f[:w] = [f[:w], w].compact.max
+ emit(e, f)
+ end
+
+ def emit_n(e, w, f) # :nodoc:
+ f[:p] ||= '0'
+ emit_w(e, w, f)
+ end
+
+ def emit_sn(e, w, f) # :nodoc:
+ if e < 0
+ w += 1
+ f[:s] = true
+ end
+ emit_n(e, w, f)
+ end
+
+ def emit_z(e, w, f) # :nodoc:
+ w += 1
+ f[:s] = true
+ emit_n(e, w, f)
+ end
+
+ def emit_a(e, w, f) # :nodoc:
+ f[:p] ||= "\s"
+ emit_w(e, w, f)
+ end
+
+ def emit_ad(e, w, f) # :nodoc:
+ if f[:x]
+ f[:u] = true
+ f[:d] = false
+ end
+ emit_a(e, w, f)
+ end
+
+ def emit_au(e, w, f) # :nodoc:
+ if f[:x]
+ f[:u] = false
+ f[:d] = true
+ end
+ emit_a(e, w, f)
+ end
+
+ private :emit, :emit_w, :emit_n, :emit_sn, :emit_z,
+ :emit_a, :emit_ad, :emit_au
+
+ def strftime(fmt='%F')
+ fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do
+ f = {}
+ m = $&
+ s, w, c = $1, $2, $3
+ if s
+ s.scan(/./) do |k|
+ case k
+ when '-'; f[:p] = '-'
+ when '_'; f[:p] = "\s"
+ when '0'; f[:p] = '0'
+ when '^'; f[:u] = true
+ when '#'; f[:x] = true
+ end
+ end
+ end
+ if w
+ f[:w] = w.to_i
+ end
+ case c
+ when 'A'; emit_ad(DAYNAMES[wday], 0, f)
+ when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
+ when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
+ when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
+ when 'C', 'EC'; emit_sn((year / 100).floor, 2, f)
+ when 'c', 'Ec'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
+ when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
+ when 'd', 'Od'; emit_n(mday, 2, f)
+ when 'e', 'Oe'; emit_a(mday, 2, f)
+ when 'F'
+ if m == '%F'
+ format('%.4d-%02d-%02d', year, mon, mday) # 4p
+ else
+ emit_a(strftime('%Y-%m-%d'), 0, f)
+ end
+ when 'G'; emit_sn(cwyear, 4, f)
+ when 'g'; emit_n(cwyear % 100, 2, f)
+ when 'H', 'OH'; emit_n(hour, 2, f)
+ when 'h'; emit_ad(strftime('%b'), 0, f)
+ when 'I', 'OI'; emit_n((hour % 12).nonzero? || 12, 2, f)
+ when 'j'; emit_n(yday, 3, f)
+ when 'k'; emit_a(hour, 2, f)
+ when 'L'
+ f[:p] = nil
+ w = f[:w] || 3
+ u = 10**w
+ emit_n((sec_fraction * u).floor, w, f)
+ when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
+ when 'M', 'OM'; emit_n(min, 2, f)
+ when 'm', 'Om'; emit_n(mon, 2, f)
+ when 'N'
+ f[:p] = nil
+ w = f[:w] || 9
+ u = 10**w
+ emit_n((sec_fraction * u).floor, w, f)
+ when 'n'; emit_a("\n", 0, f)
+ when 'P'; emit_ad(strftime('%p').downcase, 0, f)
+ when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
+ when 'Q'
+ s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round
+ emit_sn(s, 1, f)
+ when 'R'; emit_a(strftime('%H:%M'), 0, f)
+ when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
+ when 'S', 'OS'; emit_n(sec, 2, f)
+ when 's'
+ s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round
+ emit_sn(s, 1, f)
+ when 'T'
+ if m == '%T'
+ format('%02d:%02d:%02d', hour, min, sec) # 4p
+ else
+ emit_a(strftime('%H:%M:%S'), 0, f)
+ end
+ when 't'; emit_a("\t", 0, f)
+ when 'U', 'W', 'OU', 'OW'
+ emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f)
+ when 'u', 'Ou'; emit_n(cwday, 1, f)
+ when 'V', 'OV'; emit_n(cweek, 2, f)
+ when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
+ when 'w', 'Ow'; emit_n(wday, 1, f)
+ when 'X', 'EX'; emit_a(strftime('%H:%M:%S'), 0, f)
+ when 'x', 'Ex'; emit_a(strftime('%m/%d/%y'), 0, f)
+ when 'Y', 'EY'; emit_sn(year, 4, f)
+ when 'y', 'Ey', 'Oy'; emit_n(year % 100, 2, f)
+ when 'Z'; emit_au(strftime('%:z'), 0, f)
+ when /\A(:{0,3})z/
+ t = $1.size
+ sign = if offset < 0 then -1 else +1 end
+ fr = offset.abs
+ ss = fr.div(SECONDS_IN_DAY) # 4p
+ hh, ss = ss.divmod(3600)
+ mm, ss = ss.divmod(60)
+ if t == 3
+ if ss.nonzero? then t = 2
+ elsif mm.nonzero? then t = 1
+ else t = -1
+ end
+ end
+ case t
+ when -1
+ tail = []
+ sep = ''
+ when 0
+ f[:w] -= 2 if f[:w]
+ tail = ['%02d' % mm]
+ sep = ''
+ when 1
+ f[:w] -= 3 if f[:w]
+ tail = ['%02d' % mm]
+ sep = ':'
+ when 2
+ f[:w] -= 6 if f[:w]
+ tail = ['%02d' % mm, '%02d' % ss]
+ sep = ':'
+ end
+ ([emit_z(sign * hh, 2, f)] + tail).join(sep)
+ when '%'; emit_a('%', 0, f)
+ when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
+ else
+ m
+ end
+ end
+ end
+
+# alias_method :format, :strftime
+
+ def asctime() strftime('%c') end
+
+ alias_method :ctime, :asctime
+
+ def iso8601() strftime('%F') end
+
+ def rfc3339() iso8601 end
+
+ def xmlschema() iso8601 end # :nodoc:
+
+ def rfc2822() strftime('%a, %-d %b %Y %T %z') end
+
+ alias_method :rfc822, :rfc2822
+
+ def httpdate() new_offset(0).strftime('%a, %d %b %Y %T GMT') end # :nodoc:
+
+ def jisx0301
+ if jd < 2405160
+ iso8601
+ else
+ case jd
+ when 2405160...2419614
+ g = 'M%02d' % (year - 1867)
+ when 2419614...2424875
+ g = 'T%02d' % (year - 1911)
+ when 2424875...2447535
+ g = 'S%02d' % (year - 1925)
+ else
+ g = 'H%02d' % (year - 1988)
+ end
+ g + strftime('.%m.%d')
+ end
+ end
+
+=begin
+ def beat(n=0)
+ i, f = (new_offset(HOURS_IN_DAY).day_fraction * 1000).divmod(1)
+ ('@%03d' % i) +
+ if n < 1
+ ''
+ else
+ '.%0*d' % [n, (f / Rational(1, 10**n)).round]
+ end
+ end
+=end
+
+ def self.num_pattern? (s) # :nodoc:
+ /\A%[EO]?[CDdeFGgHIjkLlMmNQRrSsTUuVvWwXxYy\d]/ =~ s || /\A\d/ =~ s
+ end
+
+ private_class_method :num_pattern?
+
+ def self._strptime_i(str, fmt, e) # :nodoc:
+ fmt.scan(/%([EO]?(?::{1,3}z|.))|(.)/m) do |s, c|
+ a = $&
+ if s
+ case s
+ when 'A', 'a'
+ return unless str.sub!(/\A(#{Format::DAYS.keys.join('|')})/io, '') ||
+ str.sub!(/\A(#{Format::ABBR_DAYS.keys.join('|')})/io, '')
+ val = Format::DAYS[$1.downcase] || Format::ABBR_DAYS[$1.downcase]
+ return unless val
+ e.wday = val
+ when 'B', 'b', 'h'
+ return unless str.sub!(/\A(#{Format::MONTHS.keys.join('|')})/io, '') ||
+ str.sub!(/\A(#{Format::ABBR_MONTHS.keys.join('|')})/io, '')
+ val = Format::MONTHS[$1.downcase] || Format::ABBR_MONTHS[$1.downcase]
+ return unless val
+ e.mon = val
+ when 'C', 'EC'
+ return unless str.sub!(if num_pattern?($')
+ then /\A([-+]?\d{1,2})/
+ else /\A([-+]?\d{1,})/
+ end, '')
+ val = $1.to_i
+ e._cent = val
+ when 'c', 'Ec'
+ return unless _strptime_i(str, '%a %b %e %H:%M:%S %Y', e)
+ when 'D'
+ return unless _strptime_i(str, '%m/%d/%y', e)
+ when 'd', 'e', 'Od', 'Oe'
+ return unless str.sub!(/\A( \d|\d{1,2})/, '')
+ val = $1.to_i
+ return unless (1..31) === val
+ e.mday = val
+ when 'F'
+ return unless _strptime_i(str, '%Y-%m-%d', e)
+ when 'G'
+ return unless str.sub!(if num_pattern?($')
+ then /\A([-+]?\d{1,4})/
+ else /\A([-+]?\d{1,})/
+ end, '')
+ val = $1.to_i
+ e.cwyear = val
+ when 'g'
+ return unless str.sub!(/\A(\d{1,2})/, '')
+ val = $1.to_i
+ return unless (0..99) === val
+ e.cwyear = val
+ e._cent ||= if val >= 69 then 19 else 20 end
+ when 'H', 'k', 'OH'
+ return unless str.sub!(/\A( \d|\d{1,2})/, '')
+ val = $1.to_i
+ return unless (0..24) === val
+ e.hour = val
+ when 'I', 'l', 'OI'
+ return unless str.sub!(/\A( \d|\d{1,2})/, '')
+ val = $1.to_i
+ return unless (1..12) === val
+ e.hour = val
+ when 'j'
+ return unless str.sub!(/\A(\d{1,3})/, '')
+ val = $1.to_i
+ return unless (1..366) === val
+ e.yday = val
+ when 'L'
+ return unless str.sub!(if num_pattern?($')
+ then /\A([-+]?\d{1,3})/
+ else /\A([-+]?\d{1,})/
+ end, '')
+# val = Rational($1.to_i, 10**3)
+ val = Rational($1.to_i, 10**$1.size)
+ e.sec_fraction = val
+ when 'M', 'OM'
+ return unless str.sub!(/\A(\d{1,2})/, '')
+ val = $1.to_i
+ return unless (0..59) === val
+ e.min = val
+ when 'm', 'Om'
+ return unless str.sub!(/\A(\d{1,2})/, '')
+ val = $1.to_i
+ return unless (1..12) === val
+ e.mon = val
+ when 'N'
+ return unless str.sub!(if num_pattern?($')
+ then /\A([-+]?\d{1,9})/
+ else /\A([-+]?\d{1,})/
+ end, '')
+# val = Rational($1.to_i, 10**9)
+ val = Rational($1.to_i, 10**$1.size)
+ e.sec_fraction = val
+ when 'n', 't'
+ return unless _strptime_i(str, "\s", e)
+ when 'P', 'p'
+ return unless str.sub!(/\A([ap])(?:m\b|\.m\.)/i, '')
+ e._merid = if $1.downcase == 'a' then 0 else 12 end
+ when 'Q'
+ return unless str.sub!(/\A(-?\d{1,})/, '')
+ val = Rational($1.to_i, 10**3)
+ e.seconds = val
+ when 'R'
+ return unless _strptime_i(str, '%H:%M', e)
+ when 'r'
+ return unless _strptime_i(str, '%I:%M:%S %p', e)
+ when 'S', 'OS'
+ return unless str.sub!(/\A(\d{1,2})/, '')
+ val = $1.to_i
+ return unless (0..60) === val
+ e.sec = val
+ when 's'
+ return unless str.sub!(/\A(-?\d{1,})/, '')
+ val = $1.to_i
+ e.seconds = val
+ when 'T'
+ return unless _strptime_i(str, '%H:%M:%S', e)
+ when 'U', 'W', 'OU', 'OW'
+ return unless str.sub!(/\A(\d{1,2})/, '')
+ val = $1.to_i
+ return unless (0..53) === val
+ e.__send__(if s[-1,1] == 'U' then :wnum0= else :wnum1= end, val)
+ when 'u', 'Ou'
+ return unless str.sub!(/\A(\d{1})/, '')
+ val = $1.to_i
+ return unless (1..7) === val
+ e.cwday = val
+ when 'V', 'OV'
+ return unless str.sub!(/\A(\d{1,2})/, '')
+ val = $1.to_i
+ return unless (1..53) === val
+ e.cweek = val
+ when 'v'
+ return unless _strptime_i(str, '%e-%b-%Y', e)
+ when 'w'
+ return unless str.sub!(/\A(\d{1})/, '')
+ val = $1.to_i
+ return unless (0..6) === val
+ e.wday = val
+ when 'X', 'EX'
+ return unless _strptime_i(str, '%H:%M:%S', e)
+ when 'x', 'Ex'
+ return unless _strptime_i(str, '%m/%d/%y', e)
+ when 'Y', 'EY'
+ return unless str.sub!(if num_pattern?($')
+ then /\A([-+]?\d{1,4})/
+ else /\A([-+]?\d{1,})/
+ end, '')
+ val = $1.to_i
+ e.year = val
+ when 'y', 'Ey', 'Oy'
+ return unless str.sub!(/\A(\d{1,2})/, '')
+ val = $1.to_i
+ return unless (0..99) === val
+ e.year = val
+ e._cent ||= if val >= 69 then 19 else 20 end
+ when 'Z', /\A:{0,3}z/
+ return unless str.sub!(/\A((?:gmt|utc?)?[-+]\d+(?:[,.:]\d+(?::\d+)?)?
+ |[[:alpha:].\s]+(?:standard|daylight)\s+time\b
+ |[[:alpha:]]+(?:\s+dst)?\b
+ )/ix, '')
+ val = $1
+ e.zone = val
+ offset = zone_to_diff(val)
+ e.offset = offset
+ when '%'
+ return unless str.sub!(/\A%/, '')
+ when '+'
+ return unless _strptime_i(str, '%a %b %e %H:%M:%S %Z %Y', e)
+ else
+ return unless str.sub!(Regexp.new('\\A' + Regexp.quote(a)), '')
+ end
+ else
+ case c
+ when /\A[\s\v]/
+ str.sub!(/\A[\s\v]+/, '')
+ else
+ return unless str.sub!(Regexp.new('\\A' + Regexp.quote(a)), '')
+ end
+ end
+ end
+ end
+
+ private_class_method :_strptime_i
+
+ def self._strptime(str, fmt='%F')
+ str = str.dup
+ e = Format::Bag.new
+ return unless _strptime_i(str, fmt, e)
+
+ if e._cent
+ if e.cwyear
+ e.cwyear += e._cent * 100
+ end
+ if e.year
+ e. year += e._cent * 100
+ end
+ end
+
+ if e._merid
+ if e.hour
+ e.hour %= 12
+ e.hour += e._merid
+ end
+ end
+
+ unless str.empty?
+ e.leftover = str
+ end
+
+ e.to_hash
+ end
+
+ def self.s3e(e, y, m, d, bc=false)
+ unless String === m
+ m = m.to_s
+ end
+
+ if y && m && !d
+ y, m, d = d, y, m
+ end
+
+ if y == nil
+ if d && d.size > 2
+ y = d
+ d = nil
+ end
+ if d && d[0,1] == "'"
+ y = d
+ d = nil
+ end
+ end
+
+ if y
+ y.scan(/(\d+)(.+)?/)
+ if $2
+ y, d = d, $1
+ end
+ end
+
+ if m
+ if m[0,1] == "'" || m.size > 2
+ y, m, d = m, d, y # us -> be
+ end
+ end
+
+ if d
+ if d[0,1] == "'" || d.size > 2
+ y, d = d, y
+ end
+ end
+
+ if y
+ y =~ /([-+])?(\d+)/
+ if $1 || $2.size > 2
+ c = false
+ end
+ iy = $&.to_i
+ if bc
+ iy = -iy + 1
+ end
+ e.year = iy
+ end
+
+ if m
+ m =~ /\d+/
+ e.mon = $&.to_i
+ end
+
+ if d
+ d =~ /\d+/
+ e.mday = $&.to_i
+ end
+
+ if c != nil
+ e._comp = c
+ end
+
+ end
+
+ private_class_method :s3e
+
+ def self._parse_day(str, e) # :nodoc:
+ if str.sub!(/\b(#{Format::ABBR_DAYS.keys.join('|')})[^-\d\s]*/io, ' ')
+ e.wday = Format::ABBR_DAYS[$1.downcase]
+ true
+=begin
+ elsif str.sub!(/\b(?!\dth)(su|mo|tu|we|th|fr|sa)\b/i, ' ')
+ e.wday = %w(su mo tu we th fr sa).index($1.downcase)
+ true
+=end
+ end
+ end
+
+ def self._parse_time(str, e) # :nodoc:
+ if str.sub!(
+ /(
+ (?:
+ \d+\s*:\s*\d+
+ (?:
+ \s*:\s*\d+(?:[,.]\d*)?
+ )?
+ |
+ \d+\s*h(?:\s*\d+m?(?:\s*\d+s?)?)?
+ )
+ (?:
+ \s*
+ [ap](?:m\b|\.m\.)
+ )?
+ |
+ \d+\s*[ap](?:m\b|\.m\.)
+ )
+ (?:
+ \s*
+ (
+ (?:gmt|utc?)?[-+]\d+(?:[,.:]\d+(?::\d+)?)?
+ |
+ [[:alpha:].\s]+(?:standard|daylight)\stime\b
+ |
+ [[:alpha:]]+(?:\sdst)?\b
+ )
+ )?
+ /ix,
+ ' ')
+
+ t = $1
+ e.zone = $2 if $2
+
+ t =~ /\A(\d+)h?
+ (?:\s*:?\s*(\d+)m?
+ (?:
+ \s*:?\s*(\d+)(?:[,.](\d+))?s?
+ )?
+ )?
+ (?:\s*([ap])(?:m\b|\.m\.))?/ix
+
+ e.hour = $1.to_i
+ e.min = $2.to_i if $2
+ e.sec = $3.to_i if $3
+ e.sec_fraction = Rational($4.to_i, 10**$4.size) if $4
+
+ if $5
+ e.hour %= 12
+ if $5.downcase == 'p'
+ e.hour += 12
+ end
+ end
+ true
+ end
+ end
+
+=begin
+ def self._parse_beat(str, e) # :nodoc:
+ if str.sub!(/@\s*(\d+)(?:[,.](\d*))?/, ' ')
+ beat = Rational($1.to_i)
+ beat += Rational($2.to_i, 10**$2.size) if $2
+ secs = Rational(beat, 1000)
+ h, min, s, fr = self.day_fraction_to_time(secs)
+ e.hour = h
+ e.min = min
+ e.sec = s
+ e.sec_fraction = fr * 86400
+ e.zone = '+01:00'
+ true
+ end
+ end
+=end
+
+ def self._parse_eu(str, e) # :nodoc:
+ if str.sub!(
+ /'?(\d+)[^-\d\s]*
+ \s*
+ (#{Format::ABBR_MONTHS.keys.join('|')})[^-\d\s']*
+ (?:
+ \s*
+ (c(?:e|\.e\.)|b(?:ce|\.c\.e\.)|a(?:d|\.d\.)|b(?:c|\.c\.))?
+ \s*
+ ('?-?\d+(?:(?:st|nd|rd|th)\b)?)
+ )?
+ /iox,
+ ' ') # '
+ s3e(e, $4, Format::ABBR_MONTHS[$2.downcase], $1,
+ $3 && $3[0,1].downcase == 'b')
+ true
+ end
+ end
+
+ def self._parse_us(str, e) # :nodoc:
+ if str.sub!(
+ /\b(#{Format::ABBR_MONTHS.keys.join('|')})[^-\d\s']*
+ \s*
+ ('?\d+)[^-\d\s']*
+ (?:
+ \s*
+ (c(?:e|\.e\.)|b(?:ce|\.c\.e\.)|a(?:d|\.d\.)|b(?:c|\.c\.))?
+ \s*
+ ('?-?\d+)
+ )?
+ /iox,
+ ' ') # '
+ s3e(e, $4, Format::ABBR_MONTHS[$1.downcase], $2,
+ $3 && $3[0,1].downcase == 'b')
+ true
+ end
+ end
+
+ def self._parse_iso(str, e) # :nodoc:
+ if str.sub!(/('?[-+]?\d+)-(\d+)-('?-?\d+)/, ' ')
+ s3e(e, $1, $2, $3)
+ true
+ end
+ end
+
+ def self._parse_iso2(str, e) # :nodoc:
+ if str.sub!(/\b(\d{2}|\d{4})?-?w(\d{2})(?:-?(\d))?\b/i, ' ')
+ e.cwyear = $1.to_i if $1
+ e.cweek = $2.to_i
+ e.cwday = $3.to_i if $3
+ true
+ elsif str.sub!(/-w-(\d)\b/i, ' ')
+ e.cwday = $1.to_i
+ true
+ elsif str.sub!(/--(\d{2})?-(\d{2})\b/, ' ')
+ e.mon = $1.to_i if $1
+ e.mday = $2.to_i
+ true
+ elsif str.sub!(/--(\d{2})(\d{2})?\b/, ' ')
+ e.mon = $1.to_i
+ e.mday = $2.to_i if $2
+ true
+ elsif /[,.](\d{2}|\d{4})-\d{3}\b/ !~ str &&
+ str.sub!(/\b(\d{2}|\d{4})-(\d{3})\b/, ' ')
+ e.year = $1.to_i
+ e.yday = $2.to_i
+ true
+ elsif /\d-\d{3}\b/ !~ str &&
+ str.sub!(/\b-(\d{3})\b/, ' ')
+ e.yday = $1.to_i
+ true
+ end
+ end
+
+ def self._parse_jis(str, e) # :nodoc:
+ if str.sub!(/\b([mtsh])(\d+)\.(\d+)\.(\d+)/i, ' ')
+ era = { 'm'=>1867,
+ 't'=>1911,
+ 's'=>1925,
+ 'h'=>1988
+ }[$1.downcase]
+ e.year = $2.to_i + era
+ e.mon = $3.to_i
+ e.mday = $4.to_i
+ true
+ end
+ end
+
+ def self._parse_vms(str, e) # :nodoc:
+ if str.sub!(/('?-?\d+)-(#{Format::ABBR_MONTHS.keys.join('|')})[^-]*
+ -('?-?\d+)/iox, ' ')
+ s3e(e, $3, Format::ABBR_MONTHS[$2.downcase], $1)
+ true
+ elsif str.sub!(/\b(#{Format::ABBR_MONTHS.keys.join('|')})[^-]*
+ -('?-?\d+)(?:-('?-?\d+))?/iox, ' ')
+ s3e(e, $3, Format::ABBR_MONTHS[$1.downcase], $2)
+ true
+ end
+ end
+
+ def self._parse_sla(str, e) # :nodoc:
+ if str.sub!(%r|('?-?\d+)/\s*('?\d+)(?:\D\s*('?-?\d+))?|, ' ') # '
+ s3e(e, $1, $2, $3)
+ true
+ end
+ end
+
+ def self._parse_dot(str, e) # :nodoc:
+ if str.sub!(%r|('?-?\d+)\.\s*('?\d+)\.\s*('?-?\d+)|, ' ') # '
+ s3e(e, $1, $2, $3)
+ true
+ end
+ end
+
+ def self._parse_year(str, e) # :nodoc:
+ if str.sub!(/'(\d+)\b/, ' ')
+ e.year = $1.to_i
+ true
+ end
+ end
+
+ def self._parse_mon(str, e) # :nodoc:
+ if str.sub!(/\b(#{Format::ABBR_MONTHS.keys.join('|')})\S*/io, ' ')
+ e.mon = Format::ABBR_MONTHS[$1.downcase]
+ true
+ end
+ end
+
+ def self._parse_mday(str, e) # :nodoc:
+ if str.sub!(/(\d+)(st|nd|rd|th)\b/i, ' ')
+ e.mday = $1.to_i
+ true
+ end
+ end
+
+ def self._parse_ddd(str, e) # :nodoc:
+ if str.sub!(
+ /([-+]?)(\d{2,14})
+ (?:
+ \s*
+ t?
+ \s*
+ (\d{2,6})?(?:[,.](\d*))?
+ )?
+ (?:
+ \s*
+ (
+ z\b
+ |
+ [-+]\d{1,4}\b
+ |
+ \[[-+]?\d[^\]]*\]
+ )
+ )?
+ /ix,
+ ' ')
+ case $2.size
+ when 2
+ if $3.nil? && $4
+ e.sec = $2[-2, 2].to_i
+ else
+ e.mday = $2[ 0, 2].to_i
+ end
+ when 4
+ if $3.nil? && $4
+ e.sec = $2[-2, 2].to_i
+ e.min = $2[-4, 2].to_i
+ else
+ e.mon = $2[ 0, 2].to_i
+ e.mday = $2[ 2, 2].to_i
+ end
+ when 6
+ if $3.nil? && $4
+ e.sec = $2[-2, 2].to_i
+ e.min = $2[-4, 2].to_i
+ e.hour = $2[-6, 2].to_i
+ else
+ e.year = ($1 + $2[ 0, 2]).to_i
+ e.mon = $2[ 2, 2].to_i
+ e.mday = $2[ 4, 2].to_i
+ end
+ when 8, 10, 12, 14
+ if $3.nil? && $4
+ e.sec = $2[-2, 2].to_i
+ e.min = $2[-4, 2].to_i
+ e.hour = $2[-6, 2].to_i
+ e.mday = $2[-8, 2].to_i
+ if $2.size >= 10
+ e.mon = $2[-10, 2].to_i
+ end
+ if $2.size == 12
+ e.year = ($1 + $2[-12, 2]).to_i
+ end
+ if $2.size == 14
+ e.year = ($1 + $2[-14, 4]).to_i
+ e._comp = false
+ end
+ else
+ e.year = ($1 + $2[ 0, 4]).to_i
+ e.mon = $2[ 4, 2].to_i
+ e.mday = $2[ 6, 2].to_i
+ e.hour = $2[ 8, 2].to_i if $2.size >= 10
+ e.min = $2[10, 2].to_i if $2.size >= 12
+ e.sec = $2[12, 2].to_i if $2.size >= 14
+ e._comp = false
+ end
+ when 3
+ if $3.nil? && $4
+ e.sec = $2[-2, 2].to_i
+ e.min = $2[-3, 1].to_i
+ else
+ e.yday = $2[ 0, 3].to_i
+ end
+ when 5
+ if $3.nil? && $4
+ e.sec = $2[-2, 2].to_i
+ e.min = $2[-4, 2].to_i
+ e.hour = $2[-5, 1].to_i
+ else
+ e.year = ($1 + $2[ 0, 2]).to_i
+ e.yday = $2[ 2, 3].to_i
+ end
+ when 7
+ if $3.nil? && $4
+ e.sec = $2[-2, 2].to_i
+ e.min = $2[-4, 2].to_i
+ e.hour = $2[-6, 2].to_i
+ e.mday = $2[-7, 1].to_i
+ else
+ e.year = ($1 + $2[ 0, 4]).to_i
+ e.yday = $2[ 4, 3].to_i
+ end
+ end
+ if $3
+ if $4
+ case $3.size
+ when 2, 4, 6
+ e.sec = $3[-2, 2].to_i
+ e.min = $3[-4, 2].to_i if $3.size >= 4
+ e.hour = $3[-6, 2].to_i if $3.size >= 6
+ end
+ else
+ case $3.size
+ when 2, 4, 6
+ e.hour = $3[ 0, 2].to_i
+ e.min = $3[ 2, 2].to_i if $3.size >= 4
+ e.sec = $3[ 4, 2].to_i if $3.size >= 6
+ end
+ end
+ end
+ if $4
+ e.sec_fraction = Rational($4.to_i, 10**$4.size)
+ end
+ if $5
+ e.zone = $5
+ if e.zone[0,1] == '['
+ o, n, = e.zone[1..-2].split(':')
+ e.zone = n || o
+ if /\A\d/ =~ o
+ o = format('+%s', o)
+ end
+ e.offset = zone_to_diff(o)
+ end
+ end
+ true
+ end
+ end
+
+ private_class_method :_parse_day, :_parse_time, # :_parse_beat,
+ :_parse_eu, :_parse_us, :_parse_iso, :_parse_iso2,
+ :_parse_jis, :_parse_vms, :_parse_sla, :_parse_dot,
+ :_parse_year, :_parse_mon, :_parse_mday, :_parse_ddd
+
+ def self._parse(str, comp=true)
+ str = str.dup
+
+ e = Format::Bag.new
+
+ e._comp = comp
+
+ str.gsub!(/[^-+',.\/:@[:alnum:]\[\]]+/, ' ')
+
+ _parse_time(str, e) # || _parse_beat(str, e)
+ _parse_day(str, e)
+
+ _parse_eu(str, e) ||
+ _parse_us(str, e) ||
+ _parse_iso(str, e) ||
+ _parse_jis(str, e) ||
+ _parse_vms(str, e) ||
+ _parse_sla(str, e) ||
+ _parse_dot(str, e) ||
+ _parse_iso2(str, e) ||
+ _parse_year(str, e) ||
+ _parse_mon(str, e) ||
+ _parse_mday(str, e) ||
+ _parse_ddd(str, e)
+
+ if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/i, ' ')
+ if e.year
+ e.year = -e.year + 1
+ end
+ end
+
+ if str.sub!(/\A\s*(\d{1,2})\s*\z/, ' ')
+ if e.hour && !e.mday
+ v = $1.to_i
+ if (1..31) === v
+ e.mday = v
+ end
+ end
+ if e.mday && !e.hour
+ v = $1.to_i
+ if (0..24) === v
+ e.hour = v
+ end
+ end
+ end
+
+ if e._comp
+ if e.cwyear
+ if e.cwyear >= 0 && e.cwyear <= 99
+ e.cwyear += if e.cwyear >= 69
+ then 1900 else 2000 end
+ end
+ end
+ if e.year
+ if e.year >= 0 && e.year <= 99
+ e.year += if e.year >= 69
+ then 1900 else 2000 end
+ end
+ end
+ end
+
+ e.offset ||= zone_to_diff(e.zone) if e.zone
+
+ e.to_hash
+ end
+
+ def self._iso8601(str) # :nodoc:
+ if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}|
+ ([-+]?\d{2,})?-\d{3}|
+ (\d{2}|\d{4})?-w\d{2}-\d|
+ -w-\d)
+ (t
+ \d{2}:\d{2}(:\d{2}([,.]\d+)?)?
+ (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
+ _parse(str)
+ elsif /\A\s*(([-+]?(\d{2}|\d{4})|--)\d{2}\d{2}|
+ ([-+]?(\d{2}|\d{4}))?\d{3}|-\d{3}|
+ (\d{2}|\d{4})?w\d{2}\d)
+ (t?
+ \d{2}\d{2}(\d{2}([,.]\d+)?)?
+ (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
+ _parse(str)
+ elsif /\A\s*(\d{2}:\d{2}(:\d{2}([,.]\d+)?)?
+ (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
+ _parse(str)
+ elsif /\A\s*(\d{2}\d{2}(\d{2}([,.]\d+)?)?
+ (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
+ _parse(str)
+ end
+ end
+
+ def self._rfc3339(str) # :nodoc:
+ if /\A\s*-?\d{4}-\d{2}-\d{2} # allow minus, anyway
+ (t|\s)
+ \d{2}:\d{2}:\d{2}(\.\d+)?
+ (z|[-+]\d{2}:\d{2})\s*\z/ix =~ str
+ _parse(str)
+ end
+ end
+
+ def self._xmlschema(str) # :nodoc:
+ if /\A\s*(-?\d{4,})(?:-(\d{2})(?:-(\d{2}))?)?
+ (?:t
+ (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?)?
+ (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
+ e = Format::Bag.new
+ e.year = $1.to_i
+ e.mon = $2.to_i if $2
+ e.mday = $3.to_i if $3
+ e.hour = $4.to_i if $4
+ e.min = $5.to_i if $5
+ e.sec = $6.to_i if $6
+ e.sec_fraction = Rational($7.to_i, 10**$7.size) if $7
+ if $8
+ e.zone = $8
+ e.offset = zone_to_diff($8)
+ end
+ e.to_hash
+ elsif /\A\s*(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?
+ (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
+ e = Format::Bag.new
+ e.hour = $1.to_i if $1
+ e.min = $2.to_i if $2
+ e.sec = $3.to_i if $3
+ e.sec_fraction = Rational($4.to_i, 10**$4.size) if $4
+ if $5
+ e.zone = $5
+ e.offset = zone_to_diff($5)
+ end
+ e.to_hash
+ elsif /\A\s*(?:--(\d{2})(?:-(\d{2}))?|---(\d{2}))
+ (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
+ e = Format::Bag.new
+ e.mon = $1.to_i if $1
+ e.mday = $2.to_i if $2
+ e.mday = $3.to_i if $3
+ if $4
+ e.zone = $4
+ e.offset = zone_to_diff($4)
+ end
+ e.to_hash
+ end
+ end
+
+ def self._rfc2822(str) # :nodoc:
+ if /\A\s*(?:(?:#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)?
+ \d{1,2}\s+
+ (?:#{Format::ABBR_MONTHS.keys.join('|')})\s+
+ -?(\d{2,})\s+ # allow minus, anyway
+ \d{2}:\d{2}(:\d{2})?\s*
+ (?:[-+]\d{4}|ut|gmt|e[sd]t|c[sd]t|m[sd]t|p[sd]t|[a-ik-z])\s*\z/iox =~ str
+ e = _parse(str, false)
+ if $1.size < 4
+ if e[:year] < 50
+ e[:year] += 2000
+ elsif e[:year] < 1000
+ e[:year] += 1900
+ end
+ end
+ e
+ end
+ end
+
+ class << self; alias_method :_rfc822, :_rfc2822 end
+
+ def self._httpdate(str) # :nodoc:
+ if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+
+ \d{2}\s+
+ (#{Format::ABBR_MONTHS.keys.join('|')})\s+
+ -?\d{4}\s+ # allow minus, anyway
+ \d{2}:\d{2}:\d{2}\s+
+ gmt\s*\z/iox =~ str
+ _rfc2822(str)
+ elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+
+ \d{2}\s*-\s*
+ (#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s*
+ \d{2}\s+
+ \d{2}:\d{2}:\d{2}\s+
+ gmt\s*\z/iox =~ str
+ _parse(str)
+ elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+
+ (#{Format::ABBR_MONTHS.keys.join('|')})\s+
+ \d{1,2}\s+
+ \d{2}:\d{2}:\d{2}\s+
+ \d{4}\s*\z/iox =~ str
+ _parse(str)
+ end
+ end
+
+ def self._jisx0301(str) # :nodoc:
+ if /\A\s*[mtsh]?\d{2}\.\d{2}\.\d{2}
+ (t
+ (\d{2}:\d{2}(:\d{2}([,.]\d*)?)?
+ (z|[-+]\d{2}(:?\d{2})?)?)?)?\s*\z/ix =~ str
+ if /\A\s*\d/ =~ str
+ _parse(str.sub(/\A\s*(\d)/, 'h\1'))
+ else
+ _parse(str)
+ end
+ else
+ _iso8601(str)
+ end
+ end
+
+ t = Module.new do
+
+ private
+
+ def zone_to_diff(zone) # :nodoc:
+ zone = zone.downcase
+ if zone.sub!(/\s+(standard|daylight)\s+time\z/, '')
+ dst = $1 == 'daylight'
+ else
+ dst = zone.sub!(/\s+dst\z/, '')
+ end
+ if Format::ZONES.include?(zone)
+ offset = Format::ZONES[zone]
+ offset += 3600 if dst
+ elsif zone.sub!(/\A(?:gmt|utc?)?([-+])/, '')
+ sign = $1
+ if zone.include?(':')
+ hour, min, sec, = zone.split(':')
+ elsif zone.include?(',') || zone.include?('.')
+ hour, fr, = zone.split(/[,.]/)
+ min = Rational(fr.to_i, 10**fr.size) * 60
+ else
+ case zone.size
+ when 3
+ hour = zone[0,1]
+ min = zone[1,2]
+ else
+ hour = zone[0,2]
+ min = zone[2,2]
+ sec = zone[4,2]
+ end
+ end
+ offset = hour.to_i * 3600 + min.to_i * 60 + sec.to_i
+ offset *= -1 if sign == '-'
+ end
+ offset
+ end
+
+ end
+
+ extend t
+ include t
+
+end
+
+class DateTime < Date
+
+ def strftime(fmt='%FT%T%:z')
+ super(fmt)
+ end
+
+ def self._strptime(str, fmt='%FT%T%z')
+ super(str, fmt)
+ end
+
+ def iso8601_timediv(n) # :nodoc:
+ strftime('T%T' +
+ if n < 1
+ ''
+ else
+ '.%0*d' % [n, (sec_fraction / Rational(1, 10**n)).round]
+ end +
+ '%:z')
+ end
+
+ private :iso8601_timediv
+
+ def iso8601(n=0)
+ super() + iso8601_timediv(n)
+ end
+
+ def rfc3339(n=0) iso8601(n) end
+
+ def xmlschema(n=0) iso8601(n) end # :nodoc:
+
+ def jisx0301(n=0)
+ super() + iso8601_timediv(n)
+ end
+
+end
diff --git a/ruby/lib/debug.rb b/ruby/lib/debug.rb
new file mode 100644
index 0000000..7bb1450
--- /dev/null
+++ b/ruby/lib/debug.rb
@@ -0,0 +1,907 @@
+# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
+# Copyright (C) 2000 Information-technology Promotion Agency, Japan
+# Copyright (C) 2000-2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
+
+require 'continuation'
+
+if $SAFE > 0
+ STDERR.print "-r debug.rb is not available in safe mode\n"
+ exit 1
+end
+
+require 'tracer'
+require 'pp'
+
+class Tracer
+ def Tracer.trace_func(*vars)
+ Single.trace_func(*vars)
+ end
+end
+
+SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
+
+class DEBUGGER__
+MUTEX = Mutex.new
+
+class Context
+ DEBUG_LAST_CMD = []
+
+ begin
+ require 'readline'
+ def readline(prompt, hist)
+ Readline::readline(prompt, hist)
+ end
+ rescue LoadError
+ def readline(prompt, hist)
+ STDOUT.print prompt
+ STDOUT.flush
+ line = STDIN.gets
+ exit unless line
+ line.chomp!
+ line
+ end
+ USE_READLINE = false
+ end
+
+ def initialize
+ if Thread.current == Thread.main
+ @stop_next = 1
+ else
+ @stop_next = 0
+ end
+ @last_file = nil
+ @file = nil
+ @line = nil
+ @no_step = nil
+ @frames = []
+ @finish_pos = 0
+ @trace = false
+ @catch = "StandardError"
+ @suspend_next = false
+ end
+
+ def stop_next(n=1)
+ @stop_next = n
+ end
+
+ def set_suspend
+ @suspend_next = true
+ end
+
+ def clear_suspend
+ @suspend_next = false
+ end
+
+ def suspend_all
+ DEBUGGER__.suspend
+ end
+
+ def resume_all
+ DEBUGGER__.resume
+ end
+
+ def check_suspend
+ while MUTEX.synchronize {
+ if @suspend_next
+ DEBUGGER__.waiting.push Thread.current
+ @suspend_next = false
+ true
+ end
+ }
+ end
+ end
+
+ def trace?
+ @trace
+ end
+
+ def set_trace(arg)
+ @trace = arg
+ end
+
+ def stdout
+ DEBUGGER__.stdout
+ end
+
+ def break_points
+ DEBUGGER__.break_points
+ end
+
+ def display
+ DEBUGGER__.display
+ end
+
+ def context(th)
+ DEBUGGER__.context(th)
+ end
+
+ def set_trace_all(arg)
+ DEBUGGER__.set_trace(arg)
+ end
+
+ def set_last_thread(th)
+ DEBUGGER__.set_last_thread(th)
+ end
+
+ def debug_eval(str, binding)
+ begin
+ val = eval(str, binding)
+ rescue StandardError, ScriptError => e
+ at = eval("caller(1)", binding)
+ stdout.printf "%s:%s\n", at.shift, e.to_s.sub(/\(eval\):1:(in `.*?':)?/, '')
+ for i in at
+ stdout.printf "\tfrom %s\n", i
+ end
+ throw :debug_error
+ end
+ end
+
+ def debug_silent_eval(str, binding)
+ begin
+ eval(str, binding)
+ rescue StandardError, ScriptError
+ nil
+ end
+ end
+
+ def var_list(ary, binding)
+ ary.sort!
+ for v in ary
+ stdout.printf " %s => %s\n", v, eval(v, binding).inspect
+ end
+ end
+
+ def debug_variable_info(input, binding)
+ case input
+ when /^\s*g(?:lobal)?\s*$/
+ var_list(global_variables, binding)
+
+ when /^\s*l(?:ocal)?\s*$/
+ var_list(eval("local_variables", binding), binding)
+
+ when /^\s*i(?:nstance)?\s+/
+ obj = debug_eval($', binding)
+ var_list(obj.instance_variables, obj.instance_eval{binding()})
+
+ when /^\s*c(?:onst(?:ant)?)?\s+/
+ obj = debug_eval($', binding)
+ unless obj.kind_of? Module
+ stdout.print "Should be Class/Module: ", $', "\n"
+ else
+ var_list(obj.constants, obj.module_eval{binding()})
+ end
+ end
+ end
+
+ def debug_method_info(input, binding)
+ case input
+ when /^i(:?nstance)?\s+/
+ obj = debug_eval($', binding)
+
+ len = 0
+ for v in obj.methods.sort
+ len += v.size + 1
+ if len > 70
+ len = v.size + 1
+ stdout.print "\n"
+ end
+ stdout.print v, " "
+ end
+ stdout.print "\n"
+
+ else
+ obj = debug_eval(input, binding)
+ unless obj.kind_of? Module
+ stdout.print "Should be Class/Module: ", input, "\n"
+ else
+ len = 0
+ for v in obj.instance_methods(false).sort
+ len += v.size + 1
+ if len > 70
+ len = v.size + 1
+ stdout.print "\n"
+ end
+ stdout.print v, " "
+ end
+ stdout.print "\n"
+ end
+ end
+ end
+
+ def thnum
+ num = DEBUGGER__.instance_eval{@thread_list[Thread.current]}
+ unless num
+ DEBUGGER__.make_thread_list
+ num = DEBUGGER__.instance_eval{@thread_list[Thread.current]}
+ end
+ num
+ end
+
+ def debug_command(file, line, id, binding)
+ MUTEX.lock
+ unless defined?($debugger_restart) and $debugger_restart
+ callcc{|c| $debugger_restart = c}
+ end
+ set_last_thread(Thread.current)
+ frame_pos = 0
+ binding_file = file
+ binding_line = line
+ previous_line = nil
+ if ENV['EMACS']
+ stdout.printf "\032\032%s:%d:\n", binding_file, binding_line
+ else
+ stdout.printf "%s:%d:%s", binding_file, binding_line,
+ line_at(binding_file, binding_line)
+ end
+ @frames[0] = [binding, file, line, id]
+ display_expressions(binding)
+ prompt = true
+ while prompt and input = readline("(rdb:%d) "%thnum(), true)
+ catch(:debug_error) do
+ if input == ""
+ next unless DEBUG_LAST_CMD[0]
+ input = DEBUG_LAST_CMD[0]
+ stdout.print input, "\n"
+ else
+ DEBUG_LAST_CMD[0] = input
+ end
+
+ case input
+ when /^\s*tr(?:ace)?(?:\s+(on|off))?(?:\s+(all))?$/
+ if defined?( $2 )
+ if $1 == 'on'
+ set_trace_all true
+ else
+ set_trace_all false
+ end
+ elsif defined?( $1 )
+ if $1 == 'on'
+ set_trace true
+ else
+ set_trace false
+ end
+ end
+ if trace?
+ stdout.print "Trace on.\n"
+ else
+ stdout.print "Trace off.\n"
+ end
+
+ when /^\s*b(?:reak)?\s+(?:(.+):)?([^.:]+)$/
+ pos = $2
+ if $1
+ klass = debug_silent_eval($1, binding)
+ file = $1
+ end
+ if pos =~ /^\d+$/
+ pname = pos
+ pos = pos.to_i
+ else
+ pname = pos = pos.intern.id2name
+ end
+ break_points.push [true, 0, klass || file, pos]
+ stdout.printf "Set breakpoint %d at %s:%s\n", break_points.size, klass || file, pname
+
+ when /^\s*b(?:reak)?\s+(.+)[#.]([^.:]+)$/
+ pos = $2.intern.id2name
+ klass = debug_eval($1, binding)
+ break_points.push [true, 0, klass, pos]
+ stdout.printf "Set breakpoint %d at %s.%s\n", break_points.size, klass, pos
+
+ when /^\s*wat(?:ch)?\s+(.+)$/
+ exp = $1
+ break_points.push [true, 1, exp]
+ stdout.printf "Set watchpoint %d:%s\n", break_points.size, exp
+
+ when /^\s*b(?:reak)?$/
+ if break_points.find{|b| b[1] == 0}
+ n = 1
+ stdout.print "Breakpoints:\n"
+ break_points.each do |b|
+ if b[0] and b[1] == 0
+ stdout.printf " %d %s:%s\n", n, b[2], b[3]
+ end
+ n += 1
+ end
+ end
+ if break_points.find{|b| b[1] == 1}
+ n = 1
+ stdout.print "\n"
+ stdout.print "Watchpoints:\n"
+ for b in break_points
+ if b[0] and b[1] == 1
+ stdout.printf " %d %s\n", n, b[2]
+ end
+ n += 1
+ end
+ end
+ if break_points.size == 0
+ stdout.print "No breakpoints\n"
+ else
+ stdout.print "\n"
+ end
+
+ when /^\s*del(?:ete)?(?:\s+(\d+))?$/
+ pos = $1
+ unless pos
+ input = readline("Clear all breakpoints? (y/n) ", false)
+ if input == "y"
+ for b in break_points
+ b[0] = false
+ end
+ end
+ else
+ pos = pos.to_i
+ if break_points[pos-1]
+ break_points[pos-1][0] = false
+ else
+ stdout.printf "Breakpoint %d is not defined\n", pos
+ end
+ end
+
+ when /^\s*disp(?:lay)?\s+(.+)$/
+ exp = $1
+ display.push [true, exp]
+ stdout.printf "%d: ", display.size
+ display_expression(exp, binding)
+
+ when /^\s*disp(?:lay)?$/
+ display_expressions(binding)
+
+ when /^\s*undisp(?:lay)?(?:\s+(\d+))?$/
+ pos = $1
+ unless pos
+ input = readline("Clear all expressions? (y/n) ", false)
+ if input == "y"
+ for d in display
+ d[0] = false
+ end
+ end
+ else
+ pos = pos.to_i
+ if display[pos-1]
+ display[pos-1][0] = false
+ else
+ stdout.printf "Display expression %d is not defined\n", pos
+ end
+ end
+
+ when /^\s*c(?:ont)?$/
+ prompt = false
+
+ when /^\s*s(?:tep)?(?:\s+(\d+))?$/
+ if $1
+ lev = $1.to_i
+ else
+ lev = 1
+ end
+ @stop_next = lev
+ prompt = false
+
+ when /^\s*n(?:ext)?(?:\s+(\d+))?$/
+ if $1
+ lev = $1.to_i
+ else
+ lev = 1
+ end
+ @stop_next = lev
+ @no_step = @frames.size - frame_pos
+ prompt = false
+
+ when /^\s*w(?:here)?$/, /^\s*f(?:rame)?$/
+ display_frames(frame_pos)
+
+ when /^\s*l(?:ist)?(?:\s+(.+))?$/
+ if not $1
+ b = previous_line ? previous_line + 10 : binding_line - 5
+ e = b + 9
+ elsif $1 == '-'
+ b = previous_line ? previous_line - 10 : binding_line - 5
+ e = b + 9
+ else
+ b, e = $1.split(/[-,]/)
+ if e
+ b = b.to_i
+ e = e.to_i
+ else
+ b = b.to_i - 5
+ e = b + 9
+ end
+ end
+ previous_line = b
+ display_list(b, e, binding_file, binding_line)
+
+ when /^\s*up(?:\s+(\d+))?$/
+ previous_line = nil
+ if $1
+ lev = $1.to_i
+ else
+ lev = 1
+ end
+ frame_pos += lev
+ if frame_pos >= @frames.size
+ frame_pos = @frames.size - 1
+ stdout.print "At toplevel\n"
+ end
+ binding, binding_file, binding_line = @frames[frame_pos]
+ stdout.print format_frame(frame_pos)
+
+ when /^\s*down(?:\s+(\d+))?$/
+ previous_line = nil
+ if $1
+ lev = $1.to_i
+ else
+ lev = 1
+ end
+ frame_pos -= lev
+ if frame_pos < 0
+ frame_pos = 0
+ stdout.print "At stack bottom\n"
+ end
+ binding, binding_file, binding_line = @frames[frame_pos]
+ stdout.print format_frame(frame_pos)
+
+ when /^\s*fin(?:ish)?$/
+ if frame_pos == @frames.size
+ stdout.print "\"finish\" not meaningful in the outermost frame.\n"
+ else
+ @finish_pos = @frames.size - frame_pos
+ frame_pos = 0
+ prompt = false
+ end
+
+ when /^\s*cat(?:ch)?(?:\s+(.+))?$/
+ if $1
+ excn = $1
+ if excn == 'off'
+ @catch = nil
+ stdout.print "Clear catchpoint.\n"
+ else
+ @catch = excn
+ stdout.printf "Set catchpoint %s.\n", @catch
+ end
+ else
+ if @catch
+ stdout.printf "Catchpoint %s.\n", @catch
+ else
+ stdout.print "No catchpoint.\n"
+ end
+ end
+
+ when /^\s*q(?:uit)?$/
+ input = readline("Really quit? (y/n) ", false)
+ if input == "y"
+ exit! # exit -> exit!: No graceful way to stop threads...
+ end
+
+ when /^\s*v(?:ar)?\s+/
+ debug_variable_info($', binding)
+
+ when /^\s*m(?:ethod)?\s+/
+ debug_method_info($', binding)
+
+ when /^\s*th(?:read)?\s+/
+ if DEBUGGER__.debug_thread_info($', binding) == :cont
+ prompt = false
+ end
+
+ when /^\s*pp\s+/
+ PP.pp(debug_eval($', binding), stdout)
+
+ when /^\s*p\s+/
+ stdout.printf "%s\n", debug_eval($', binding).inspect
+
+ when /^\s*r(?:estart)?$/
+ $debugger_restart.call
+
+ when /^\s*h(?:elp)?$/
+ debug_print_help()
+
+ else
+ v = debug_eval(input, binding)
+ stdout.printf "%s\n", v.inspect
+ end
+ end
+ end
+ MUTEX.unlock
+ resume_all
+ end
+
+ def debug_print_help
+ stdout.print <<EOHELP
+Debugger help v.-0.002b
+Commands
+ b[reak] [file:|class:]<line|method>
+ b[reak] [class.]<line|method>
+ set breakpoint to some position
+ wat[ch] <expression> set watchpoint to some expression
+ cat[ch] (<exception>|off) set catchpoint to an exception
+ b[reak] list breakpoints
+ cat[ch] show catchpoint
+ del[ete][ nnn] delete some or all breakpoints
+ disp[lay] <expression> add expression into display expression list
+ undisp[lay][ nnn] delete one particular or all display expressions
+ c[ont] run until program ends or hit breakpoint
+ s[tep][ nnn] step (into methods) one line or till line nnn
+ n[ext][ nnn] go over one line or till line nnn
+ w[here] display frames
+ f[rame] alias for where
+ l[ist][ (-|nn-mm)] list program, - lists backwards
+ nn-mm lists given lines
+ up[ nn] move to higher frame
+ down[ nn] move to lower frame
+ fin[ish] return to outer frame
+ tr[ace] (on|off) set trace mode of current thread
+ tr[ace] (on|off) all set trace mode of all threads
+ q[uit] exit from debugger
+ v[ar] g[lobal] show global variables
+ v[ar] l[ocal] show local variables
+ v[ar] i[nstance] <object> show instance variables of object
+ v[ar] c[onst] <object> show constants of object
+ m[ethod] i[nstance] <obj> show methods of object
+ m[ethod] <class|module> show instance methods of class or module
+ th[read] l[ist] list all threads
+ th[read] c[ur[rent]] show current thread
+ th[read] [sw[itch]] <nnn> switch thread context to nnn
+ th[read] stop <nnn> stop thread nnn
+ th[read] resume <nnn> resume thread nnn
+ p expression evaluate expression and print its value
+ h[elp] print this help
+ <everything else> evaluate
+EOHELP
+ end
+
+ def display_expressions(binding)
+ n = 1
+ for d in display
+ if d[0]
+ stdout.printf "%d: ", n
+ display_expression(d[1], binding)
+ end
+ n += 1
+ end
+ end
+
+ def display_expression(exp, binding)
+ stdout.printf "%s = %s\n", exp, debug_silent_eval(exp, binding).to_s
+ end
+
+ def frame_set_pos(file, line)
+ if @frames[0]
+ @frames[0][1] = file
+ @frames[0][2] = line
+ end
+ end
+
+ def display_frames(pos)
+ 0.upto(@frames.size - 1) do |n|
+ if n == pos
+ stdout.print "--> "
+ else
+ stdout.print " "
+ end
+ stdout.print format_frame(n)
+ end
+ end
+
+ def format_frame(pos)
+ bind, file, line, id = @frames[pos]
+ sprintf "#%d %s:%s%s\n", pos + 1, file, line,
+ (id ? ":in `#{id.id2name}'" : "")
+ end
+
+ def display_list(b, e, file, line)
+ stdout.printf "[%d, %d] in %s\n", b, e, file
+ if lines = SCRIPT_LINES__[file] and lines != true
+ b.upto(e) do |n|
+ if n > 0 && lines[n-1]
+ if n == line
+ stdout.printf "=> %d %s\n", n, lines[n-1].chomp
+ else
+ stdout.printf " %d %s\n", n, lines[n-1].chomp
+ end
+ end
+ end
+ else
+ stdout.printf "No sourcefile available for %s\n", file
+ end
+ end
+
+ def line_at(file, line)
+ lines = SCRIPT_LINES__[file]
+ if lines
+ return "\n" if lines == true
+ line = lines[line-1]
+ return "\n" unless line
+ return line
+ end
+ return "\n"
+ end
+
+ def debug_funcname(id)
+ if id.nil?
+ "toplevel"
+ else
+ id.id2name
+ end
+ end
+
+ def check_break_points(file, klass, pos, binding, id)
+ return false if break_points.empty?
+ n = 1
+ for b in break_points
+ if b[0] # valid
+ if b[1] == 0 # breakpoint
+ if (b[2] == file and b[3] == pos) or
+ (klass and b[2] == klass and b[3] == pos)
+ stdout.printf "Breakpoint %d, %s at %s:%s\n", n, debug_funcname(id), file, pos
+ return true
+ end
+ elsif b[1] == 1 # watchpoint
+ if debug_silent_eval(b[2], binding)
+ stdout.printf "Watchpoint %d, %s at %s:%s\n", n, debug_funcname(id), file, pos
+ return true
+ end
+ end
+ end
+ n += 1
+ end
+ return false
+ end
+
+ def excn_handle(file, line, id, binding)
+ if $!.class <= SystemExit
+ set_trace_func nil
+ exit
+ end
+
+ if @catch and ($!.class.ancestors.find { |e| e.to_s == @catch })
+ stdout.printf "%s:%d: `%s' (%s)\n", file, line, $!, $!.class
+ fs = @frames.size
+ tb = caller(0)[-fs..-1]
+ if tb
+ for i in tb
+ stdout.printf "\tfrom %s\n", i
+ end
+ end
+ suspend_all
+ debug_command(file, line, id, binding)
+ end
+ end
+
+ def trace_func(event, file, line, id, binding, klass)
+ Tracer.trace_func(event, file, line, id, binding, klass) if trace?
+ context(Thread.current).check_suspend
+ @file = file
+ @line = line
+ case event
+ when 'line'
+ frame_set_pos(file, line)
+ if !@no_step or @frames.size == @no_step
+ @stop_next -= 1
+ @stop_next = -1 if @stop_next < 0
+ elsif @frames.size < @no_step
+ @stop_next = 0 # break here before leaving...
+ else
+ # nothing to do. skipped.
+ end
+ if @stop_next == 0 or check_break_points(file, nil, line, binding, id)
+ @no_step = nil
+ suspend_all
+ debug_command(file, line, id, binding)
+ end
+
+ when 'call'
+ @frames.unshift [binding, file, line, id]
+ if check_break_points(file, klass, id.id2name, binding, id)
+ suspend_all
+ debug_command(file, line, id, binding)
+ end
+
+ when 'c-call'
+ frame_set_pos(file, line)
+
+ when 'class'
+ @frames.unshift [binding, file, line, id]
+
+ when 'return', 'end'
+ if @frames.size == @finish_pos
+ @stop_next = 1
+ @finish_pos = 0
+ end
+ @frames.shift
+
+ when 'raise'
+ excn_handle(file, line, id, binding)
+
+ end
+ @last_file = file
+ end
+end
+
+trap("INT") { DEBUGGER__.interrupt }
+@last_thread = Thread::main
+@max_thread = 1
+@thread_list = {Thread::main => 1}
+@break_points = []
+@display = []
+@waiting = []
+@stdout = STDOUT
+
+class << DEBUGGER__
+ def stdout
+ @stdout
+ end
+
+ def stdout=(s)
+ @stdout = s
+ end
+
+ def display
+ @display
+ end
+
+ def break_points
+ @break_points
+ end
+
+ def waiting
+ @waiting
+ end
+
+ def set_trace( arg )
+ MUTEX.synchronize do
+ make_thread_list
+ for th, in @thread_list
+ context(th).set_trace arg
+ end
+ end
+ arg
+ end
+
+ def set_last_thread(th)
+ @last_thread = th
+ end
+
+ def suspend
+ MUTEX.synchronize do
+ make_thread_list
+ for th, in @thread_list
+ next if th == Thread.current
+ context(th).set_suspend
+ end
+ end
+ # Schedule other threads to suspend as soon as possible.
+ Thread.pass
+ end
+
+ def resume
+ MUTEX.synchronize do
+ make_thread_list
+ @thread_list.each do |th,|
+ next if th == Thread.current
+ context(th).clear_suspend
+ end
+ waiting.each do |th|
+ th.run
+ end
+ waiting.clear
+ end
+ # Schedule other threads to restart as soon as possible.
+ Thread.pass
+ end
+
+ def context(thread=Thread.current)
+ c = thread[:__debugger_data__]
+ unless c
+ thread[:__debugger_data__] = c = Context.new
+ end
+ c
+ end
+
+ def interrupt
+ context(@last_thread).stop_next
+ end
+
+ def get_thread(num)
+ th = @thread_list.key(num)
+ unless th
+ @stdout.print "No thread ##{num}\n"
+ throw :debug_error
+ end
+ th
+ end
+
+ def thread_list(num)
+ th = get_thread(num)
+ if th == Thread.current
+ @stdout.print "+"
+ else
+ @stdout.print " "
+ end
+ @stdout.printf "%d ", num
+ @stdout.print th.inspect, "\t"
+ file = context(th).instance_eval{@file}
+ if file
+ @stdout.print file,":",context(th).instance_eval{@line}
+ end
+ @stdout.print "\n"
+ end
+
+ def thread_list_all
+ for th in @thread_list.values.sort
+ thread_list(th)
+ end
+ end
+
+ def make_thread_list
+ hash = {}
+ for th in Thread::list
+ if @thread_list.key? th
+ hash[th] = @thread_list[th]
+ else
+ @max_thread += 1
+ hash[th] = @max_thread
+ end
+ end
+ @thread_list = hash
+ end
+
+ def debug_thread_info(input, binding)
+ case input
+ when /^l(?:ist)?/
+ make_thread_list
+ thread_list_all
+
+ when /^c(?:ur(?:rent)?)?$/
+ make_thread_list
+ thread_list(@thread_list[Thread.current])
+
+ when /^(?:sw(?:itch)?\s+)?(\d+)/
+ make_thread_list
+ th = get_thread($1.to_i)
+ if th == Thread.current
+ @stdout.print "It's the current thread.\n"
+ else
+ thread_list(@thread_list[th])
+ context(th).stop_next
+ th.run
+ return :cont
+ end
+
+ when /^stop\s+(\d+)/
+ make_thread_list
+ th = get_thread($1.to_i)
+ if th == Thread.current
+ @stdout.print "It's the current thread.\n"
+ elsif th.stop?
+ @stdout.print "Already stopped.\n"
+ else
+ thread_list(@thread_list[th])
+ context(th).suspend
+ end
+
+ when /^resume\s+(\d+)/
+ make_thread_list
+ th = get_thread($1.to_i)
+ if th == Thread.current
+ @stdout.print "It's the current thread.\n"
+ elsif !th.stop?
+ @stdout.print "Already running."
+ else
+ thread_list(@thread_list[th])
+ th.run
+ end
+ end
+ end
+end
+
+stdout.printf "Debug.rb\n"
+stdout.printf "Emacs support available.\n\n"
+RubyVM::InstructionSequence.compile_option = {
+ trace_instruction: true
+}
+set_trace_func proc { |event, file, line, id, binding, klass, *rest|
+ DEBUGGER__.context.trace_func event, file, line, id, binding, klass
+}
+end
diff --git a/ruby/lib/delegate.rb b/ruby/lib/delegate.rb
new file mode 100644
index 0000000..cb16adb
--- /dev/null
+++ b/ruby/lib/delegate.rb
@@ -0,0 +1,311 @@
+# = delegate -- Support for the Delegation Pattern
+#
+# Documentation by James Edward Gray II and Gavin Sinclair
+#
+# == Introduction
+#
+# This library provides three different ways to delegate method calls to an
+# object. The easiest to use is SimpleDelegator. Pass an object to the
+# constructor and all methods supported by the object will be delegated. This
+# object can be changed later.
+#
+# Going a step further, the top level DelegateClass method allows you to easily
+# setup delegation through class inheritance. This is considerably more
+# flexible and thus probably the most common use for this library.
+#
+# Finally, if you need full control over the delegation scheme, you can inherit
+# from the abstract class Delegator and customize as needed. (If you find
+# yourself needing this control, have a look at _forwardable_, also in the
+# standard library. It may suit your needs better.)
+#
+# == Notes
+#
+# Be advised, RDoc will not detect delegated methods.
+#
+# <b>delegate.rb provides full-class delegation via the
+# DelegateClass() method. For single-method delegation via
+# def_delegator(), see forwardable.rb.</b>
+#
+# == Examples
+#
+# === SimpleDelegator
+#
+# Here's a simple example that takes advantage of the fact that
+# SimpleDelegator's delegation object can be changed at any time.
+#
+# class Stats
+# def initialize
+# @source = SimpleDelegator.new([])
+# end
+#
+# def stats( records )
+# @source.__setobj__(records)
+#
+# "Elements: #{@source.size}\n" +
+# " Non-Nil: #{@source.compact.size}\n" +
+# " Unique: #{@source.uniq.size}\n"
+# end
+# end
+#
+# s = Stats.new
+# puts s.stats(%w{James Edward Gray II})
+# puts
+# puts s.stats([1, 2, 3, nil, 4, 5, 1, 2])
+#
+# <i>Prints:</i>
+#
+# Elements: 4
+# Non-Nil: 4
+# Unique: 4
+#
+# Elements: 8
+# Non-Nil: 7
+# Unique: 6
+#
+# === DelegateClass()
+#
+# Here's a sample of use from <i>tempfile.rb</i>.
+#
+# A _Tempfile_ object is really just a _File_ object with a few special rules
+# about storage location and/or when the File should be deleted. That makes for
+# an almost textbook perfect example of how to use delegation.
+#
+# class Tempfile < DelegateClass(File)
+# # constant and class member data initialization...
+#
+# def initialize(basename, tmpdir=Dir::tmpdir)
+# # build up file path/name in var tmpname...
+#
+# @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)
+#
+# # ...
+#
+# super(@tmpfile)
+#
+# # below this point, all methods of File are supported...
+# end
+#
+# # ...
+# end
+#
+# === Delegator
+#
+# SimpleDelegator's implementation serves as a nice example here.
+#
+# class SimpleDelegator < Delegator
+# def initialize(obj)
+# super # pass obj to Delegator constructor, required
+# @delegate_sd_obj = obj # store obj for future use
+# end
+#
+# def __getobj__
+# @delegate_sd_obj # return object we are delegating to, required
+# end
+#
+# def __setobj__(obj)
+# @delegate_sd_obj = obj # change delegation object, a feature we're providing
+# end
+#
+# # ...
+# end
+
+#
+# Delegator is an abstract class used to build delegator pattern objects from
+# subclasses. Subclasses should redefine \_\_getobj\_\_. For a concrete
+# implementation, see SimpleDelegator.
+#
+class Delegator
+ [:to_s,:inspect,:=~,:!~,:===].each do |m|
+ undef_method m
+ end
+
+ #
+ # Pass in the _obj_ to delegate method calls to. All methods supported by
+ # _obj_ will be delegated to.
+ #
+ def initialize(obj)
+ __setobj__(obj)
+ end
+
+ # Handles the magic of delegation through \_\_getobj\_\_.
+ def method_missing(m, *args, &block)
+ begin
+ target = self.__getobj__
+ unless target.respond_to?(m)
+ super(m, *args, &block)
+ else
+ target.__send__(m, *args, &block)
+ end
+ rescue Exception
+ $@.delete_if{|s| %r"\A#{Regexp.quote(__FILE__)}:\d+:in `method_missing'\z"o =~ s}
+ ::Kernel::raise
+ end
+ end
+
+ #
+ # Checks for a method provided by this the delegate object by fowarding the
+ # call through \_\_getobj\_\_.
+ #
+ def respond_to?(m, include_private = false)
+ return true if super
+ return self.__getobj__.respond_to?(m, include_private)
+ end
+
+ #
+ # Returns true if two objects are considered same.
+ #
+ def ==(obj)
+ return true if obj.equal?(self)
+ self.__getobj__ == obj
+ end
+
+ #
+ # This method must be overridden by subclasses and should return the object
+ # method calls are being delegated to.
+ #
+ def __getobj__
+ raise NotImplementedError, "need to define `__getobj__'"
+ end
+
+ #
+ # This method must be overridden by subclasses and change the object delegate
+ # to _obj_.
+ #
+ def __setobj__(obj)
+ raise NotImplementedError, "need to define `__setobj__'"
+ end
+
+ # Serialization support for the object returned by \_\_getobj\_\_.
+ def marshal_dump
+ __getobj__
+ end
+ # Reinitializes delegation from a serialized object.
+ def marshal_load(obj)
+ __setobj__(obj)
+ end
+
+ # Clone support for the object returned by \_\_getobj\_\_.
+ def clone
+ new = super
+ new.__setobj__(__getobj__.clone)
+ new
+ end
+ # Duplication support for the object returned by \_\_getobj\_\_.
+ def dup
+ new = super
+ new.__setobj__(__getobj__.dup)
+ new
+ end
+end
+
+#
+# A concrete implementation of Delegator, this class provides the means to
+# delegate all supported method calls to the object passed into the constructor
+# and even to change the object being delegated to at a later time with
+# \_\_setobj\_\_ .
+#
+class SimpleDelegator<Delegator
+ # Returns the current object method calls are being delegated to.
+ def __getobj__
+ @delegate_sd_obj
+ end
+
+ #
+ # Changes the delegate object to _obj_.
+ #
+ # It's important to note that this does *not* cause SimpleDelegator's methods
+ # to change. Because of this, you probably only want to change delegation
+ # to objects of the same type as the original delegate.
+ #
+ # Here's an example of changing the delegation object.
+ #
+ # names = SimpleDelegator.new(%w{James Edward Gray II})
+ # puts names[1] # => Edward
+ # names.__setobj__(%w{Gavin Sinclair})
+ # puts names[1] # => Sinclair
+ #
+ def __setobj__(obj)
+ raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
+ @delegate_sd_obj = obj
+ end
+end
+
+# :stopdoc:
+def Delegator.delegating_block(mid)
+ lambda do |*args, &block|
+ begin
+ __getobj__.__send__(mid, *args, &block)
+ rescue
+ re = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/o
+ $!.backtrace.delete_if {|t| re =~ t}
+ raise
+ end
+ end
+end
+# :startdoc:
+
+#
+# The primary interface to this library. Use to setup delegation when defining
+# your class.
+#
+# class MyClass < DelegateClass( ClassToDelegateTo ) # Step 1
+# def initialize
+# super(obj_of_ClassToDelegateTo) # Step 2
+# end
+# end
+#
+def DelegateClass(superclass)
+ klass = Class.new(Delegator)
+ methods = superclass.public_instance_methods(true)
+ methods -= ::Delegator.public_instance_methods
+ methods -= [:to_s,:inspect,:=~,:!~,:===]
+ klass.module_eval {
+ def __getobj__ # :nodoc:
+ @delegate_dc_obj
+ end
+ def __setobj__(obj) # :nodoc:
+ raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
+ @delegate_dc_obj = obj
+ end
+ }
+ klass.module_eval do
+ methods.each do |method|
+ define_method(method, Delegator.delegating_block(method))
+ end
+ end
+ return klass
+end
+
+# :enddoc:
+
+if __FILE__ == $0
+ class ExtArray<DelegateClass(Array)
+ def initialize()
+ super([])
+ end
+ end
+
+ ary = ExtArray.new
+ p ary.class
+ ary.push 25
+ p ary
+ ary.push 42
+ ary.each {|x| p x}
+
+ foo = Object.new
+ def foo.test
+ 25
+ end
+ def foo.iter
+ yield self
+ end
+ def foo.error
+ raise 'this is OK'
+ end
+ foo2 = SimpleDelegator.new(foo)
+ p foo2
+ foo2.instance_eval{print "foo\n"}
+ p foo.test == foo2.test # => true
+ p foo2.iter{[55,true]} # => true
+ foo2.error # raise error!
+end
diff --git a/ruby/lib/digest.rb b/ruby/lib/digest.rb
new file mode 100644
index 0000000..0c4ee3c
--- /dev/null
+++ b/ruby/lib/digest.rb
@@ -0,0 +1,50 @@
+require 'digest.so'
+
+module Digest
+ def self.const_missing(name)
+ case name
+ when :SHA256, :SHA384, :SHA512
+ lib = 'digest/sha2.so'
+ else
+ lib = File.join('digest', name.to_s.downcase)
+ end
+
+ begin
+ require lib
+ rescue LoadError => e
+ raise LoadError, "library not found for class Digest::#{name} -- #{lib}", caller(1)
+ end
+ unless Digest.const_defined?(name)
+ raise NameError, "uninitialized constant Digest::#{name}", caller(1)
+ end
+ Digest.const_get(name)
+ end
+
+ class ::Digest::Class
+ # creates a digest object and reads a given file, _name_.
+ #
+ # p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
+ # # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
+ def self.file(name)
+ new.file(name)
+ end
+ end
+
+ module Instance
+ # updates the digest with the contents of a given file _name_ and
+ # returns self.
+ def file(name)
+ File.open(name, "rb") {|f|
+ buf = ""
+ while f.read(16384, buf)
+ update buf
+ end
+ }
+ self
+ end
+ end
+end
+
+def Digest(name)
+ Digest.const_get(name)
+end
diff --git a/ruby/lib/digest/hmac.rb b/ruby/lib/digest/hmac.rb
new file mode 100644
index 0000000..e88a335
--- /dev/null
+++ b/ruby/lib/digest/hmac.rb
@@ -0,0 +1,274 @@
+# = digest/hmac.rb
+#
+# An implementation of HMAC keyed-hashing algorithm
+#
+# == Overview
+#
+# This library adds a method named hmac() to Digest classes, which
+# creates a Digest class for calculating HMAC digests.
+#
+# == Examples
+#
+# require 'digest/hmac'
+#
+# # one-liner example
+# puts Digest::HMAC.hexdigest("data", "hash key", Digest::SHA1)
+#
+# # rather longer one
+# hmac = Digest::HMAC.new("foo", Digest::RMD160)
+#
+# buf = ""
+# while stream.read(16384, buf)
+# hmac.update(buf)
+# end
+#
+# puts hmac.bubblebabble
+#
+# == License
+#
+# Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
+#
+# Documentation by Akinori MUSHA
+#
+# All rights reserved. You can redistribute and/or modify it under
+# the same terms as Ruby.
+#
+# $Id: hmac.rb 14881 2008-01-04 07:26:14Z akr $
+#
+
+require 'digest'
+
+module Digest
+ class HMAC < Digest::Class
+ def initialize(key, digester)
+ @md = digester.new
+
+ block_len = @md.block_length
+
+ if key.bytesize > block_len
+ key = @md.digest(key)
+ end
+
+ ipad = Array.new(block_len).fill(0x36)
+ opad = Array.new(block_len).fill(0x5c)
+
+ key.bytes.each_with_index { |c, i|
+ ipad[i] ^= c
+ opad[i] ^= c
+ }
+
+ @key = key.freeze
+ @ipad = ipad.inject('') { |s, c| s << c.chr }.freeze
+ @opad = opad.inject('') { |s, c| s << c.chr }.freeze
+ @md.update(@ipad)
+ end
+
+ def initialize_copy(other)
+ @md = other.instance_eval { @md.clone }
+ end
+
+ def update(text)
+ @md.update(text)
+ self
+ end
+ alias << update
+
+ def reset
+ @md.reset
+ @md.update(@ipad)
+ self
+ end
+
+ def finish
+ d = @md.digest!
+ @md.update(@opad)
+ @md.update(d)
+ @md.digest!
+ end
+ private :finish
+
+ def digest_length
+ @md.digest_length
+ end
+
+ def block_length
+ @md.block_length
+ end
+
+ def inspect
+ sprintf('#<%s: key=%s, digest=%s>', self.class.name, @key.inspect, @md.inspect.sub(/^\#<(.*)>$/) { $1 });
+ end
+ end
+end
+
+if $0 == __FILE__
+ eval DATA.read, nil, $0, __LINE__+4
+end
+
+__END__
+
+require 'test/unit'
+
+module TM_HMAC
+ def test_s_hexdigest
+ cases.each { |h|
+ digesters.each { |d|
+ assert_equal(h[:hexdigest], Digest::HMAC.hexdigest(h[:data], h[:key], d))
+ }
+ }
+ end
+
+ def test_hexdigest
+ cases.each { |h|
+ digesters.each { |d|
+ hmac = Digest::HMAC.new(h[:key], d)
+
+ hmac.update(h[:data])
+
+ assert_equal(h[:hexdigest], hmac.hexdigest)
+ }
+ }
+ end
+
+ def test_reset
+ cases.each { |h|
+ digesters.each { |d|
+ hmac = Digest::HMAC.new(h[:key], d)
+ hmac.update("test")
+ hmac.reset
+ hmac.update(h[:data])
+
+ assert_equal(h[:hexdigest], hmac.hexdigest)
+ }
+ }
+ end
+end
+
+class TC_HMAC_MD5 < Test::Unit::TestCase
+ include TM_HMAC
+
+ def digesters
+ [Digest::MD5, Digest::MD5.new]
+ end
+
+ # Taken from RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
+ def cases
+ [
+ {
+ :key => "\x0b" * 16,
+ :data => "Hi There",
+ :hexdigest => "9294727a3638bb1c13f48ef8158bfc9d",
+ }, {
+ :key => "Jefe",
+ :data => "what do ya want for nothing?",
+ :hexdigest => "750c783e6ab0b503eaa86e310a5db738",
+ }, {
+ :key => "\xaa" * 16,
+ :data => "\xdd" * 50,
+ :hexdigest => "56be34521d144c88dbb8c733f0e8b3f6",
+ }, {
+ :key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
+ :data => "\xcd" * 50,
+ :hexdigest => "697eaf0aca3a3aea3a75164746ffaa79",
+ }, {
+ :key => "\x0c" * 16,
+ :data => "Test With Truncation",
+ :hexdigest => "56461ef2342edc00f9bab995690efd4c",
+ }, {
+ :key => "\xaa" * 80,
+ :data => "Test Using Larger Than Block-Size Key - Hash Key First",
+ :hexdigest => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
+ }, {
+ :key => "\xaa" * 80,
+ :data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
+ :hexdigest => "6f630fad67cda0ee1fb1f562db3aa53e",
+ }
+ ]
+ end
+end
+
+class TC_HMAC_SHA1 < Test::Unit::TestCase
+ include TM_HMAC
+
+ def digesters
+ [Digest::SHA1, Digest::SHA1.new]
+ end
+
+ # Taken from RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
+ def cases
+ [
+ {
+ :key => "\x0b" * 20,
+ :data => "Hi There",
+ :hexdigest => "b617318655057264e28bc0b6fb378c8ef146be00",
+ }, {
+ :key => "Jefe",
+ :data => "what do ya want for nothing?",
+ :hexdigest => "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
+ }, {
+ :key => "\xaa" * 20,
+ :data => "\xdd" * 50,
+ :hexdigest => "125d7342b9ac11cd91a39af48aa17b4f63f175d3",
+ }, {
+ :key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
+ :data => "\xcd" * 50,
+ :hexdigest => "4c9007f4026250c6bc8414f9bf50c86c2d7235da",
+ }, {
+ :key => "\x0c" * 20,
+ :data => "Test With Truncation",
+ :hexdigest => "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
+ }, {
+ :key => "\xaa" * 80,
+ :data => "Test Using Larger Than Block-Size Key - Hash Key First",
+ :hexdigest => "aa4ae5e15272d00e95705637ce8a3b55ed402112",
+ }, {
+ :key => "\xaa" * 80,
+ :data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
+ :hexdigest => "e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
+ }
+ ]
+ end
+end
+
+class TC_HMAC_RMD160 < Test::Unit::TestCase
+ include TM_HMAC
+
+ def digesters
+ [Digest::RMD160, Digest::RMD160.new]
+ end
+
+ # Taken from RFC 2286: Test Cases for HMAC-RIPEMD160 and HMAC-RIPEMD128
+ def cases
+ [
+ {
+ :key => "\x0b" * 20,
+ :data => "Hi There",
+ :hexdigest => "24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668",
+ }, {
+ :key => "Jefe",
+ :data => "what do ya want for nothing?",
+ :hexdigest => "dda6c0213a485a9e24f4742064a7f033b43c4069",
+ }, {
+ :key => "\xaa" * 20,
+ :data => "\xdd" * 50,
+ :hexdigest => "b0b105360de759960ab4f35298e116e295d8e7c1",
+ }, {
+ :key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
+ :data => "\xcd" * 50,
+ :hexdigest => "d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4",
+ }, {
+ :key => "\x0c" * 20,
+ :data => "Test With Truncation",
+ :hexdigest => "7619693978f91d90539ae786500ff3d8e0518e39",
+ }, {
+ :key => "\xaa" * 80,
+ :data => "Test Using Larger Than Block-Size Key - Hash Key First",
+ :hexdigest => "6466ca07ac5eac29e1bd523e5ada7605b791fd8b",
+ }, {
+ :key => "\xaa" * 80,
+ :data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
+ :hexdigest => "69ea60798d71616cce5fd0871e23754cd75d5a0a",
+ }
+ ]
+ end
+end
diff --git a/ruby/lib/digest/sha2.rb b/ruby/lib/digest/sha2.rb
new file mode 100644
index 0000000..89e8c68
--- /dev/null
+++ b/ruby/lib/digest/sha2.rb
@@ -0,0 +1,74 @@
+#--
+# sha2.rb - defines Digest::SHA2 class which wraps up the SHA256,
+# SHA384, and SHA512 classes.
+#++
+# Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
+#
+# All rights reserved. You can redistribute and/or modify it under the same
+# terms as Ruby.
+#
+# $Id: sha2.rb 24475 2009-08-08 15:35:43Z yugui $
+
+require 'digest'
+require 'digest/sha2.so'
+
+module Digest
+ #
+ # A meta digest provider class for SHA256, SHA384 and SHA512.
+ #
+ class SHA2 < Digest::Class
+ # call-seq:
+ # Digest::SHA2.new(bitlen = 256) -> digest_obj
+ #
+ # Creates a new SHA2 hash object with a given bit length.
+ def initialize(bitlen = 256)
+ case bitlen
+ when 256
+ @sha2 = Digest::SHA256.new
+ when 384
+ @sha2 = Digest::SHA384.new
+ when 512
+ @sha2 = Digest::SHA512.new
+ else
+ raise ArgumentError, "unsupported bit length: %s" % bitlen.inspect
+ end
+ @bitlen = bitlen
+ end
+
+ # :nodoc:
+ def reset
+ @sha2.reset
+ self
+ end
+
+ # :nodoc:
+ def update(str)
+ @sha2.update(str)
+ self
+ end
+ alias << update
+
+ def finish
+ @sha2.digest!
+ end
+ private :finish
+
+ def block_length
+ @sha2.block_length
+ end
+
+ def digest_length
+ @sha2.digest_length
+ end
+
+ # :nodoc:
+ def initialize_copy(other)
+ @sha2 = other.instance_eval { @sha2.clone }
+ end
+
+ # :nodoc:
+ def inspect
+ "#<%s:%d %s>" % [self.class.name, @bitlen, hexdigest]
+ end
+ end
+end
diff --git a/ruby/lib/dl/callback.rb b/ruby/lib/dl/callback.rb
new file mode 100644
index 0000000..c8daaf6
--- /dev/null
+++ b/ruby/lib/dl/callback.rb
@@ -0,0 +1,69 @@
+require 'dl'
+require 'thread'
+
+module DL
+ SEM = Mutex.new
+
+ def set_callback_internal(proc_entry, addr_entry, argc, ty, &cbp)
+ if( argc < 0 )
+ raise(ArgumentError, "arity should not be less than 0.")
+ end
+ addr = nil
+ SEM.synchronize{
+ ary = proc_entry[ty]
+ (0...MAX_CALLBACK).each{|n|
+ idx = (n * DLSTACK_SIZE) + argc
+ if( ary[idx].nil? )
+ ary[idx] = cbp
+ addr = addr_entry[ty][idx]
+ break
+ end
+ }
+ }
+ addr
+ end
+
+ def set_cdecl_callback(ty, argc, &cbp)
+ set_callback_internal(CdeclCallbackProcs, CdeclCallbackAddrs, argc, ty, &cbp)
+ end
+
+ def set_stdcall_callback(ty, argc, &cbp)
+ set_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, argc, ty, &cbp)
+ end
+
+ def remove_callback_internal(proc_entry, addr_entry, addr, ctype = nil)
+ index = nil
+ if( ctype )
+ addr_entry[ctype].each_with_index{|xaddr, idx|
+ if( xaddr == addr )
+ index = idx
+ end
+ }
+ else
+ addr_entry.each{|ty,entry|
+ entry.each_with_index{|xaddr, idx|
+ if( xaddr == addr )
+ index = idx
+ end
+ }
+ }
+ end
+ if( index and proc_entry[ctype][index] )
+ proc_entry[ctype][index] = nil
+ return true
+ else
+ return false
+ end
+ end
+
+ def remove_cdecl_callback(addr, ctype = nil)
+ remove_callback_internal(CdeclCallbackProcs, CdeclCallbackAddrs, addr, ctype)
+ end
+
+ def remove_stdcall_callback(addr, ctype = nil)
+ remove_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, addr, ctype)
+ end
+
+ alias set_callback set_cdecl_callback
+ alias remove_callback remove_cdecl_callback
+end
diff --git a/ruby/lib/dl/cparser.rb b/ruby/lib/dl/cparser.rb
new file mode 100644
index 0000000..a9bdb2e
--- /dev/null
+++ b/ruby/lib/dl/cparser.rb
@@ -0,0 +1,109 @@
+module DL
+ module CParser
+ def parse_struct_signature(signature, tymap=nil)
+ if( signature.is_a?(String) )
+ signature = signature.split(/\s*,\s*/)
+ end
+ mems = []
+ tys = []
+ signature.each{|msig|
+ tks = msig.split(/\s+(\*)?/)
+ ty = tks[0..-2].join(" ")
+ member = tks[-1]
+
+ case ty
+ when /\[(\d+)\]/
+ n = $1.to_i
+ ty.gsub!(/\s*\[\d+\]/,"")
+ ty = [ty, n]
+ when /\[\]/
+ ty.gsub!(/\s*\[\]/, "*")
+ end
+
+ case member
+ when /\[(\d+)\]/
+ ty = [ty, $1.to_i]
+ member.gsub!(/\s*\[\d+\]/,"")
+ when /\[\]/
+ ty = ty + "*"
+ member.gsub!(/\s*\[\]/, "")
+ end
+
+ mems.push(member)
+ tys.push(parse_ctype(ty,tymap))
+ }
+ return tys, mems
+ end
+
+ def parse_signature(signature, tymap=nil)
+ tymap ||= {}
+ signature = signature.gsub(/\s+/, " ").strip
+ case signature
+ when /^([\d\w@\*_\s]+)\(([\d\w\*_\s\,\[\]]*)\)$/
+ ret = $1
+ (args = $2).strip!
+ ret = ret.split(/\s+/)
+ args = args.split(/\s*,\s*/)
+ func = ret.pop
+ if( func =~ /^\*/ )
+ func.gsub!(/^\*+/,"")
+ ret.push("*")
+ end
+ ret = ret.join(" ")
+ return [func, parse_ctype(ret, tymap), args.collect{|arg| parse_ctype(arg, tymap)}]
+ else
+ raise(RuntimeError,"can't parse the function prototype: #{proto}")
+ end
+ end
+
+ def parse_ctype(ty, tymap=nil)
+ tymap ||= {}
+ case ty
+ when Array
+ return [parse_ctype(ty[0], tymap), ty[1]]
+ when "void"
+ return TYPE_VOID
+ when "char"
+ return TYPE_CHAR
+ when "unsigned char"
+ return -TYPE_CHAR
+ when "short"
+ return TYPE_SHORT
+ when "unsigned short"
+ return -TYPE_SHORT
+ when "int"
+ return TYPE_INT
+ when "unsigned int"
+ return -TYPE_INT
+ when "long"
+ return TYPE_LONG
+ when "unsigned long"
+ return -TYPE_LONG
+ when "long long"
+ if( defined?(TYPE_LONG_LONG) )
+ return TYPE_LONG_LONG
+ else
+ raise(RuntimeError, "unsupported type: #{ty}")
+ end
+ when "unsigned long long"
+ if( defined?(TYPE_LONG_LONG) )
+ return -TYPE_LONG_LONG
+ else
+ raise(RuntimeError, "unsupported type: #{ty}")
+ end
+ when "float"
+ return TYPE_FLOAT
+ when "double"
+ return TYPE_DOUBLE
+ when /\*/, /\[\s*\]/
+ return TYPE_VOIDP
+ else
+ if( tymap[ty] )
+ return parse_ctype(tymap[ty], tymap)
+ else
+ raise(DLError, "unknown type: #{ty}")
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/dl/func.rb b/ruby/lib/dl/func.rb
new file mode 100644
index 0000000..f0e1ca7
--- /dev/null
+++ b/ruby/lib/dl/func.rb
@@ -0,0 +1,153 @@
+require 'dl'
+require 'dl/callback'
+require 'dl/stack'
+require 'dl/value'
+require 'thread'
+
+module DL
+ class Function
+ include DL
+ include ValueUtil
+
+ def initialize(cfunc, argtypes, &proc)
+ @cfunc = cfunc
+ @stack = Stack.new(argtypes.collect{|ty| ty.abs})
+ if( @cfunc.ctype < 0 )
+ @cfunc.ctype = @cfunc.ctype.abs
+ @unsigned = true
+ end
+ if( proc )
+ bind(&proc)
+ end
+ end
+
+ def to_i()
+ @cfunc.to_i
+ end
+
+ def check_safe_obj(val)
+ if $SAFE > 0 and val.tainted?
+ raise SecurityError, 'Insecure operation'
+ end
+ end
+
+ def call(*args, &block)
+ funcs = []
+ args.each{|e| check_safe_obj(e) }
+ check_safe_obj(block)
+ args = wrap_args(args, @stack.types, funcs, &block)
+ r = @cfunc.call(@stack.pack(args))
+ funcs.each{|f| f.unbind_at_call()}
+ return wrap_result(r)
+ end
+
+ def wrap_result(r)
+ case @cfunc.ctype
+ when TYPE_VOIDP
+ r = CPtr.new(r)
+ else
+ if( @unsigned )
+ r = unsigned_value(r, @cfunc.ctype)
+ end
+ end
+ r
+ end
+
+ def bind(&block)
+ if( !block )
+ raise(RuntimeError, "block must be given.")
+ end
+ if( @cfunc.ptr == 0 )
+ cb = Proc.new{|*args|
+ ary = @stack.unpack(args)
+ @stack.types.each_with_index{|ty, idx|
+ case ty
+ when TYPE_VOIDP
+ ary[idx] = CPtr.new(ary[idx])
+ end
+ }
+ r = block.call(*ary)
+ wrap_arg(r, @cfunc.ctype, [])
+ }
+ case @cfunc.calltype
+ when :cdecl
+ @cfunc.ptr = set_cdecl_callback(@cfunc.ctype, @stack.size, &cb)
+ when :stdcall
+ @cfunc.ptr = set_stdcall_callback(@cfunc.ctype, @stack.size, &cb)
+ else
+ raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}")
+ end
+ if( @cfunc.ptr == 0 )
+ raise(RuntimeException, "can't bind C function.")
+ end
+ end
+ end
+
+ def unbind()
+ if( @cfunc.ptr != 0 )
+ case @cfunc.calltype
+ when :cdecl
+ remove_cdecl_callback(@cfunc.ptr, @cfunc.ctype)
+ when :stdcall
+ remove_stdcall_callback(@cfunc.ptr, @cfunc.ctype)
+ else
+ raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}")
+ end
+ @cfunc.ptr = 0
+ end
+ end
+
+ def bound?()
+ @cfunc.ptr != 0
+ end
+
+ def bind_at_call(&block)
+ bind(&block)
+ end
+
+ def unbind_at_call()
+ end
+ end
+
+ class TempFunction < Function
+ def bind_at_call(&block)
+ bind(&block)
+ end
+
+ def unbind_at_call()
+ unbind()
+ end
+ end
+
+ class CarriedFunction < Function
+ def initialize(cfunc, argtypes, n)
+ super(cfunc, argtypes)
+ @carrier = []
+ @index = n
+ @mutex = Mutex.new
+ end
+
+ def create_carrier(data)
+ ary = []
+ userdata = [ary, data]
+ @mutex.lock()
+ @carrier.push(userdata)
+ return dlwrap(userdata)
+ end
+
+ def bind_at_call(&block)
+ userdata = @carrier[-1]
+ userdata[0].push(block)
+ bind{|*args|
+ ptr = args[@index]
+ if( !ptr )
+ raise(RuntimeError, "The index of userdata should be lower than #{args.size}.")
+ end
+ userdata = dlunwrap(Integer(ptr))
+ args[@index] = userdata[1]
+ userdata[0][0].call(*args)
+ }
+ @mutex.unlock()
+ end
+ end
+end
diff --git a/ruby/lib/dl/import.rb b/ruby/lib/dl/import.rb
new file mode 100644
index 0000000..4f93468
--- /dev/null
+++ b/ruby/lib/dl/import.rb
@@ -0,0 +1,215 @@
+require 'dl'
+require 'dl/func.rb'
+require 'dl/struct.rb'
+require 'dl/cparser.rb'
+
+module DL
+ class CompositeHandler
+ def initialize(handlers)
+ @handlers = handlers
+ end
+
+ def handlers()
+ @handlers
+ end
+
+ def sym(symbol)
+ @handlers.each{|handle|
+ if( handle )
+ begin
+ addr = handle.sym(symbol)
+ return addr
+ rescue DLError
+ end
+ end
+ }
+ return nil
+ end
+
+ def [](symbol)
+ sym(symbol)
+ end
+ end
+
+ module Importer
+ include DL
+ include CParser
+ extend Importer
+
+ def dlload(*libs)
+ handles = libs.collect{|lib|
+ case lib
+ when nil
+ nil
+ when Handle
+ lib
+ when Importer
+ lib.handlers
+ else
+ begin
+ DL.dlopen(lib)
+ rescue DLError
+ raise(DLError, "can't load #{lib}")
+ end
+ end
+ }.flatten()
+ @handler = CompositeHandler.new(handles)
+ @func_map = {}
+ @type_alias = {}
+ end
+
+ def typealias(alias_type, orig_type)
+ @type_alias[alias_type] = orig_type
+ end
+
+ def sizeof(ty)
+ case ty
+ when String
+ ty = parse_ctype(ty, @type_alias).abs()
+ case ty
+ when TYPE_CHAR
+ return SIZEOF_CHAR
+ when TYPE_SHORT
+ return SIZEOF_SHORT
+ when TYPE_INT
+ return SIZEOF_INT
+ when TYPE_LONG
+ return SIZEOF_LONG
+ when TYPE_LONG_LONG
+ return SIZEOF_LONG_LON
+ when TYPE_FLOAT
+ return SIZEOF_FLOAT
+ when TYPE_DOUBLE
+ return SIZEOF_DOUBLE
+ when TYPE_VOIDP
+ return SIZEOF_VOIDP
+ else
+ raise(DLError, "unknown type: #{ty}")
+ end
+ when Class
+ if( ty.instance_methods().include?(:to_ptr) )
+ return ty.size()
+ end
+ end
+ return CPtr[ty].size()
+ end
+
+ def parse_bind_options(opts)
+ h = {}
+ prekey = nil
+ while( opt = opts.shift() )
+ case opt
+ when :stdcall, :cdecl
+ h[:call_type] = opt
+ when :carried, :temp, :temporal, :bind
+ h[:callback_type] = opt
+ h[:carrier] = opts.shift()
+ else
+ h[opt] = true
+ end
+ end
+ h
+ end
+ private :parse_bind_options
+
+ def extern(signature, *opts)
+ symname, ctype, argtype = parse_signature(signature, @type_alias)
+ opt = parse_bind_options(opts)
+ f = import_function(symname, ctype, argtype, opt[:call_type])
+ name = symname.gsub(/@.+/,'')
+ @func_map[name] = f
+ # define_method(name){|*args,&block| f.call(*args,&block)}
+ module_eval(<<-EOS)
+ def #{name}(*args, &block)
+ @func_map['#{name}'].call(*args,&block)
+ end
+ EOS
+ module_function(name)
+ f
+ end
+
+ def bind(signature, *opts, &blk)
+ name, ctype, argtype = parse_signature(signature, @type_alias)
+ h = parse_bind_options(opts)
+ case h[:callback_type]
+ when :bind, nil
+ f = bind_function(name, ctype, argtype, h[:call_type], &blk)
+ when :temp, :temporal
+ f = create_temp_function(name, ctype, argtype, h[:call_type])
+ when :carried
+ f = create_carried_function(name, ctype, argtype, h[:call_type], h[:carrier])
+ else
+ raise(RuntimeError, "unknown callback type: #{h[:callback_type]}")
+ end
+ @func_map[name] = f
+ #define_method(name){|*args,&block| f.call(*args,&block)}
+ module_eval(<<-EOS)
+ def #{name}(*args,&block)
+ @func_map['#{name}'].call(*args,&block)
+ end
+ EOS
+ module_function(name)
+ f
+ end
+
+ def struct(signature)
+ tys, mems = parse_struct_signature(signature, @type_alias)
+ DL::CStructBuilder.create(CStruct, tys, mems)
+ end
+
+ def union(signature)
+ tys, mems = parse_struct_signature(signature, @type_alias)
+ DL::CStructBuilder.create(CUnion, tys, mems)
+ end
+
+ def [](name)
+ @func_map[name]
+ end
+
+ def create_value(ty, val=nil)
+ s = struct([ty + " value"])
+ ptr = s.malloc()
+ if( val )
+ ptr.value = val
+ end
+ return ptr
+ end
+ alias value create_value
+
+ def import_value(ty, addr)
+ s = struct([ty + " value"])
+ ptr = s.new(addr)
+ return ptr
+ end
+
+ def import_symbol(name)
+ addr = @handler.sym(name)
+ if( !addr )
+ raise(DLError, "cannot find the symbol: #{name}")
+ end
+ CPtr.new(addr)
+ end
+
+ def import_function(name, ctype, argtype, call_type = nil)
+ addr = @handler.sym(name)
+ if( !addr )
+ raise(DLError, "cannot find the function: #{name}()")
+ end
+ Function.new(CFunc.new(addr, ctype, name, call_type || :cdecl), argtype)
+ end
+
+ def bind_function(name, ctype, argtype, call_type = nil, &block)
+ f = Function.new(CFunc.new(0, ctype, name, call_type || :cdecl), argtype)
+ f.bind(&block)
+ f
+ end
+
+ def create_temp_function(name, ctype, argtype, call_type = nil)
+ TempFunction.new(CFunc.new(0, ctype, name, call_type || :cdecl), argtype)
+ end
+
+ def create_carried_function(name, ctype, argtype, call_type = nil, n = 0)
+ CarriedFunction.new(CFunc.new(0, ctype, name, call_type || :cdecl), argtype, n)
+ end
+ end
+end
diff --git a/ruby/lib/dl/pack.rb b/ruby/lib/dl/pack.rb
new file mode 100644
index 0000000..ad91833
--- /dev/null
+++ b/ruby/lib/dl/pack.rb
@@ -0,0 +1,173 @@
+require 'dl'
+
+module DL
+ module PackInfo
+ if( defined?(TYPE_LONG_LONG) )
+ ALIGN_MAP = {
+ TYPE_VOIDP => ALIGN_VOIDP,
+ TYPE_CHAR => ALIGN_CHAR,
+ TYPE_SHORT => ALIGN_SHORT,
+ TYPE_INT => ALIGN_INT,
+ TYPE_LONG => ALIGN_LONG,
+ TYPE_LONG_LONG => ALIGN_LONG_LONG,
+ TYPE_FLOAT => ALIGN_FLOAT,
+ TYPE_DOUBLE => ALIGN_DOUBLE,
+ -TYPE_CHAR => ALIGN_CHAR,
+ -TYPE_SHORT => ALIGN_SHORT,
+ -TYPE_INT => ALIGN_INT,
+ -TYPE_LONG => ALIGN_LONG,
+ -TYPE_LONG_LONG => ALIGN_LONG_LONG,
+ }
+
+ PACK_MAP = {
+ TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG) ? "q" : "l!"),
+ TYPE_CHAR => "c",
+ TYPE_SHORT => "s!",
+ TYPE_INT => "i!",
+ TYPE_LONG => "l!",
+ TYPE_LONG_LONG => "q",
+ TYPE_FLOAT => "f",
+ TYPE_DOUBLE => "d",
+ -TYPE_CHAR => "c",
+ -TYPE_SHORT => "s!",
+ -TYPE_INT => "i!",
+ -TYPE_LONG => "l!",
+ -TYPE_LONG_LONG => "q",
+ }
+
+ SIZE_MAP = {
+ TYPE_VOIDP => SIZEOF_VOIDP,
+ TYPE_CHAR => SIZEOF_CHAR,
+ TYPE_SHORT => SIZEOF_SHORT,
+ TYPE_INT => SIZEOF_INT,
+ TYPE_LONG => SIZEOF_LONG,
+ TYPE_LONG_LONG => SIZEOF_LONG_LONG,
+ TYPE_FLOAT => SIZEOF_FLOAT,
+ TYPE_DOUBLE => SIZEOF_DOUBLE,
+ -TYPE_CHAR => SIZEOF_CHAR,
+ -TYPE_SHORT => SIZEOF_SHORT,
+ -TYPE_INT => SIZEOF_INT,
+ -TYPE_LONG => SIZEOF_LONG,
+ -TYPE_LONG_LONG => SIZEOF_LONG_LONG,
+ }
+ else
+ ALIGN_MAP = {
+ TYPE_VOIDP => ALIGN_VOIDP,
+ TYPE_CHAR => ALIGN_CHAR,
+ TYPE_SHORT => ALIGN_SHORT,
+ TYPE_INT => ALIGN_INT,
+ TYPE_LONG => ALIGN_LONG,
+ TYPE_FLOAT => ALIGN_FLOAT,
+ TYPE_DOUBLE => ALIGN_DOUBLE,
+ -TYPE_CHAR => ALIGN_CHAR,
+ -TYPE_SHORT => ALIGN_SHORT,
+ -TYPE_INT => ALIGN_INT,
+ -TYPE_LONG => ALIGN_LONG,
+ }
+
+ PACK_MAP = {
+ TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG) ? "q" : "l!"),
+ TYPE_CHAR => "c",
+ TYPE_SHORT => "s!",
+ TYPE_INT => "i!",
+ TYPE_LONG => "l!",
+ TYPE_FLOAT => "f",
+ TYPE_DOUBLE => "d",
+ -TYPE_CHAR => "c",
+ -TYPE_SHORT => "s!",
+ -TYPE_INT => "i!",
+ -TYPE_LONG => "l!",
+ }
+
+ SIZE_MAP = {
+ TYPE_VOIDP => SIZEOF_VOIDP,
+ TYPE_CHAR => SIZEOF_CHAR,
+ TYPE_SHORT => SIZEOF_SHORT,
+ TYPE_INT => SIZEOF_INT,
+ TYPE_LONG => SIZEOF_LONG,
+ TYPE_FLOAT => SIZEOF_FLOAT,
+ TYPE_DOUBLE => SIZEOF_DOUBLE,
+ -TYPE_CHAR => SIZEOF_CHAR,
+ -TYPE_SHORT => SIZEOF_SHORT,
+ -TYPE_INT => SIZEOF_INT,
+ -TYPE_LONG => SIZEOF_LONG,
+ }
+ end
+
+ def align(addr, align)
+ d = addr % align
+ if( d == 0 )
+ addr
+ else
+ addr + (align - d)
+ end
+ end
+ module_function :align
+ end
+
+ class Packer
+ include PackInfo
+
+ def Packer.[](*types)
+ Packer.new(types)
+ end
+
+ def initialize(types)
+ parse_types(types)
+ end
+
+ def size()
+ @size
+ end
+
+ def pack(ary)
+ case SIZEOF_VOIDP
+ when SIZEOF_LONG
+ ary.pack(@template)
+ when SIZEOF_LONG
+ ary.pack(@template)
+ else
+ raise(RuntimeError, "sizeof(void*)?")
+ end
+ end
+
+ def unpack(ary)
+ case SIZEOF_VOIDP
+ when SIZEOF_LONG
+ ary.join().unpack(@template)
+ when SIZEOF_LONG_LONG
+ ary.join().unpack(@template)
+ else
+ raise(RuntimeError, "sizeof(void*)?")
+ end
+ end
+
+ private
+
+ def parse_types(types)
+ @template = ""
+ addr = 0
+ types.each{|t|
+ orig_addr = addr
+ if( t.is_a?(Array) )
+ addr = align(orig_addr, ALIGN_MAP[TYPE_VOIDP])
+ else
+ addr = align(orig_addr, ALIGN_MAP[t])
+ end
+ d = addr - orig_addr
+ if( d > 0 )
+ @template << "x#{d}"
+ end
+ if( t.is_a?(Array) )
+ @template << (PACK_MAP[t[0]] * t[1])
+ addr += (SIZE_MAP[t[0]] * t[1])
+ else
+ @template << PACK_MAP[t]
+ addr += SIZE_MAP[t]
+ end
+ }
+ addr = align(addr, ALIGN_MAP[TYPE_VOIDP])
+ @size = addr
+ end
+ end
+end
diff --git a/ruby/lib/dl/stack.rb b/ruby/lib/dl/stack.rb
new file mode 100644
index 0000000..99a24bc
--- /dev/null
+++ b/ruby/lib/dl/stack.rb
@@ -0,0 +1,146 @@
+require 'dl'
+
+module DL
+ class Stack
+ def Stack.[](*types)
+ Stack.new(types)
+ end
+
+ def initialize(types)
+ parse_types(types)
+ end
+
+ def size()
+ @size
+ end
+
+ def types()
+ @types
+ end
+
+ def pack(ary)
+ case SIZEOF_VOIDP
+ when SIZEOF_LONG
+ ary.pack(@template).unpack('l!*')
+ when SIZEOF_LONG_LONG
+ ary.pack(@template).unpack('q*')
+ else
+ raise(RuntimeError, "sizeof(void*)?")
+ end
+ end
+
+ def unpack(ary)
+ case SIZEOF_VOIDP
+ when SIZEOF_LONG
+ ary.pack('l!*').unpack(@template)
+ when SIZEOF_LONG_LONG
+ ary.pack('q*').unpack(@template)
+ else
+ raise(RuntimeError, "sizeof(void*)?")
+ end
+ end
+
+ private
+
+ def align(addr, align)
+ d = addr % align
+ if( d == 0 )
+ addr
+ else
+ addr + (align - d)
+ end
+ end
+
+if( defined?(TYPE_LONG_LONG) )
+ ALIGN_MAP = {
+ TYPE_VOIDP => ALIGN_VOIDP,
+ TYPE_CHAR => ALIGN_VOIDP,
+ TYPE_SHORT => ALIGN_VOIDP,
+ TYPE_INT => ALIGN_VOIDP,
+ TYPE_LONG => ALIGN_VOIDP,
+ TYPE_LONG_LONG => ALIGN_LONG_LONG,
+ TYPE_FLOAT => ALIGN_FLOAT,
+ TYPE_DOUBLE => ALIGN_DOUBLE,
+ }
+
+ PACK_MAP = {
+ TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG)? "q" : "l!"),
+ TYPE_CHAR => "c",
+ TYPE_SHORT => "s!",
+ TYPE_INT => "i!",
+ TYPE_LONG => "l!",
+ TYPE_LONG_LONG => "q",
+ TYPE_FLOAT => "f",
+ TYPE_DOUBLE => "d",
+ }
+
+ SIZE_MAP = {
+ TYPE_VOIDP => SIZEOF_VOIDP,
+ TYPE_CHAR => SIZEOF_CHAR,
+ TYPE_SHORT => SIZEOF_SHORT,
+ TYPE_INT => SIZEOF_INT,
+ TYPE_LONG => SIZEOF_LONG,
+ TYPE_LONG_LONG => SIZEOF_LONG_LONG,
+ TYPE_FLOAT => SIZEOF_FLOAT,
+ TYPE_DOUBLE => SIZEOF_DOUBLE,
+ }
+else
+ ALIGN_MAP = {
+ TYPE_VOIDP => ALIGN_VOIDP,
+ TYPE_CHAR => ALIGN_VOIDP,
+ TYPE_SHORT => ALIGN_VOIDP,
+ TYPE_INT => ALIGN_VOIDP,
+ TYPE_LONG => ALIGN_VOIDP,
+ TYPE_FLOAT => ALIGN_FLOAT,
+ TYPE_DOUBLE => ALIGN_DOUBLE,
+ }
+
+ PACK_MAP = {
+ TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG)? "q" : "l!"),
+ TYPE_CHAR => "c",
+ TYPE_SHORT => "s!",
+ TYPE_INT => "i!",
+ TYPE_LONG => "l!",
+ TYPE_FLOAT => "f",
+ TYPE_DOUBLE => "d",
+ }
+
+ SIZE_MAP = {
+ TYPE_VOIDP => SIZEOF_VOIDP,
+ TYPE_CHAR => SIZEOF_CHAR,
+ TYPE_SHORT => SIZEOF_SHORT,
+ TYPE_INT => SIZEOF_INT,
+ TYPE_LONG => SIZEOF_LONG,
+ TYPE_FLOAT => SIZEOF_FLOAT,
+ TYPE_DOUBLE => SIZEOF_DOUBLE,
+ }
+end
+
+ def parse_types(types)
+ @types = types
+ @template = ""
+ addr = 0
+ types.each{|t|
+ addr = add_padding(addr, ALIGN_MAP[t])
+ @template << PACK_MAP[t]
+ addr += SIZE_MAP[t]
+ }
+ addr = add_padding(addr, ALIGN_MAP[SIZEOF_VOIDP])
+ if( addr % SIZEOF_VOIDP == 0 )
+ @size = addr / SIZEOF_VOIDP
+ else
+ @size = (addr / SIZEOF_VOIDP) + 1
+ end
+ end
+
+ def add_padding(addr, align)
+ orig_addr = addr
+ addr = align(orig_addr, align)
+ d = addr - orig_addr
+ if( d > 0 )
+ @template << "x#{d}"
+ end
+ addr
+ end
+ end
+end
diff --git a/ruby/lib/dl/struct.rb b/ruby/lib/dl/struct.rb
new file mode 100644
index 0000000..4272b39
--- /dev/null
+++ b/ruby/lib/dl/struct.rb
@@ -0,0 +1,213 @@
+require 'dl'
+require 'dl/pack.rb'
+
+module DL
+ class CStruct
+ def CStruct.entity_class()
+ CStructEntity
+ end
+ end
+
+ class CUnion
+ def CUnion.entity_class()
+ CUnionEntity
+ end
+ end
+
+ module CStructBuilder
+ def create(klass, types, members)
+ new_class = Class.new(klass){
+ define_method(:initialize){|addr|
+ @entity = klass.entity_class.new(addr, types)
+ @entity.assign_names(members)
+ }
+ define_method(:to_ptr){ @entity }
+ define_method(:to_i){ @entity.to_i }
+ members.each{|name|
+ define_method(name){ @entity[name] }
+ define_method(name + "="){|val| @entity[name] = val }
+ }
+ }
+ size = klass.entity_class.size(types)
+ new_class.module_eval(<<-EOS)
+ def new_class.size()
+ #{size}
+ end
+ def new_class.malloc()
+ addr = DL.malloc(#{size})
+ new(addr)
+ end
+ EOS
+ return new_class
+ end
+ module_function :create
+ end
+
+ class CStructEntity < CPtr
+ include PackInfo
+ include ValueUtil
+
+ def CStructEntity.malloc(types, func = nil)
+ addr = DL.malloc(CStructEntity.size(types))
+ CStructEntity.new(addr, types, func)
+ end
+
+ def CStructEntity.size(types)
+ offset = 0
+ max_align = 0
+ types.each_with_index{|t,i|
+ orig_offset = offset
+ if( t.is_a?(Array) )
+ align = PackInfo::ALIGN_MAP[t[0]]
+ offset = PackInfo.align(orig_offset, align)
+ size = offset - orig_offset
+ offset += (PackInfo::SIZE_MAP[t[0]] * t[1])
+ else
+ align = PackInfo::ALIGN_MAP[t]
+ offset = PackInfo.align(orig_offset, align)
+ size = offset - orig_offset
+ offset += PackInfo::SIZE_MAP[t]
+ end
+ if (max_align < align)
+ max_align = align
+ end
+ }
+ offset = PackInfo.align(offset, max_align)
+ offset
+ end
+
+ def initialize(addr, types, func = nil)
+ set_ctypes(types)
+ super(addr, @size, func)
+ end
+
+ def assign_names(members)
+ @members = members
+ end
+
+ def set_ctypes(types)
+ @ctypes = types
+ @offset = []
+ offset = 0
+ max_align = 0
+ types.each_with_index{|t,i|
+ orig_offset = offset
+ if( t.is_a?(Array) )
+ align = ALIGN_MAP[t[0]]
+ else
+ align = ALIGN_MAP[t]
+ end
+ offset = PackInfo.align(orig_offset, align)
+ size = offset - orig_offset
+ @offset[i] = offset
+ if( t.is_a?(Array) )
+ offset += (SIZE_MAP[t[0]] * t[1])
+ else
+ offset += SIZE_MAP[t]
+ end
+ if (max_align < align)
+ max_align = align
+ end
+ }
+ offset = PackInfo.align(offset, max_align)
+ @size = offset
+ end
+
+ def [](name)
+ idx = @members.index(name)
+ if( idx.nil? )
+ raise(ArgumentError, "no such member: #{name}")
+ end
+ ty = @ctypes[idx]
+ if( ty.is_a?(Array) )
+ r = super(@offset[idx], SIZE_MAP[ty[0]] * ty[1])
+ else
+ r = super(@offset[idx], SIZE_MAP[ty.abs])
+ end
+ packer = Packer.new([ty])
+ val = packer.unpack([r])
+ case ty
+ when Array
+ case ty[0]
+ when TYPE_VOIDP
+ val = val.collect{|v| CPtr.new(v)}
+ end
+ when TYPE_VOIDP
+ val = CPtr.new(val[0])
+ else
+ val = val[0]
+ end
+ if( ty.is_a?(Integer) && (ty < 0) )
+ return unsigned_value(val, ty)
+ elsif( ty.is_a?(Array) && (ty[0] < 0) )
+ return val.collect{|v| unsigned_value(v,ty[0])}
+ else
+ return val
+ end
+ end
+
+ def []=(name, val)
+ idx = @members.index(name)
+ if( idx.nil? )
+ raise(ArgumentError, "no such member: #{name}")
+ end
+ ty = @ctypes[idx]
+ packer = Packer.new([ty])
+ val = wrap_arg(val, ty, [])
+ buff = packer.pack([val].flatten())
+ super(@offset[idx], buff.size, buff)
+ if( ty.is_a?(Integer) && (ty < 0) )
+ return unsigned_value(val, ty)
+ elsif( ty.is_a?(Array) && (ty[0] < 0) )
+ return val.collect{|v| unsigned_value(v,ty[0])}
+ else
+ return val
+ end
+ end
+
+ def to_s()
+ super(@size)
+ end
+ end
+
+ class CUnionEntity < CStructEntity
+ include PackInfo
+
+ def CUnionEntity.malloc(types, func=nil)
+ addr = DL.malloc(CUnionEntity.size(types))
+ CUnionEntity.new(addr, types, func)
+ end
+
+ def CUnionEntity.size(types)
+ size = 0
+ types.each_with_index{|t,i|
+ if( t.is_a?(Array) )
+ tsize = PackInfo::SIZE_MAP[t[0]] * t[1]
+ else
+ tsize = PackInfo::SIZE_MAP[t]
+ end
+ if( tsize > size )
+ size = tsize
+ end
+ }
+ end
+
+ def set_ctypes(types)
+ @ctypes = types
+ @offset = []
+ @size = 0
+ types.each_with_index{|t,i|
+ @offset[i] = 0
+ if( t.is_a?(Array) )
+ size = SIZE_MAP[t[0]] * t[1]
+ else
+ size = SIZE_MAP[t]
+ end
+ if( size > @size )
+ @size = size
+ end
+ }
+ end
+ end
+end
+
diff --git a/ruby/lib/dl/types.rb b/ruby/lib/dl/types.rb
new file mode 100644
index 0000000..b85ac89
--- /dev/null
+++ b/ruby/lib/dl/types.rb
@@ -0,0 +1,40 @@
+module DL
+ module Win32Types
+ def included(m)
+ m.module_eval{
+ typealias "DWORD", "unsigned long"
+ typealias "PDWORD", "unsigned long *"
+ typealias "WORD", "unsigned short"
+ typealias "PWORD", "unsigned short *"
+ typealias "BOOL", "int"
+ typealias "ATOM", "int"
+ typealias "BYTE", "unsigned char"
+ typealias "PBYTE", "unsigned char *"
+ typealias "UINT", "unsigned int"
+ typealias "ULONG", "unsigned long"
+ typealias "UCHAR", "unsigned char"
+ typealias "HANDLE", "unsigned long"
+ typealias "PHANDLE", "void*"
+ typealias "PVOID", "void*"
+ typealias "LPCSTR", "char*"
+ typealias "LPSTR", "char*"
+ typealias "HINSTANCE", "unsigned int"
+ typealias "HDC", "unsigned int"
+ typealias "HWND", "unsigned int"
+ }
+ end
+ module_function :included
+ end
+
+ module BasicTypes
+ def included(m)
+ m.module_eval{
+ typealias "uint", "unsigned int"
+ typealias "u_int", "unsigned int"
+ typealias "ulong", "unsigned long"
+ typealias "u_long", "unsigned long"
+ }
+ end
+ module_function :included
+ end
+end
diff --git a/ruby/lib/dl/value.rb b/ruby/lib/dl/value.rb
new file mode 100644
index 0000000..56dfcef
--- /dev/null
+++ b/ruby/lib/dl/value.rb
@@ -0,0 +1,112 @@
+require 'dl'
+
+module DL
+ module ValueUtil
+ def unsigned_value(val, ty)
+ case ty.abs
+ when TYPE_CHAR
+ [val].pack("c").unpack("C")[0]
+ when TYPE_SHORT
+ [val].pack("s!").unpack("S!")[0]
+ when TYPE_INT
+ [val].pack("i!").unpack("I!")[0]
+ when TYPE_LONG
+ [val].pack("l!").unpack("L!")[0]
+ when TYPE_LONG_LONG
+ [val].pack("q!").unpack("Q!")[0]
+ else
+ val
+ end
+ end
+
+ def signed_value(val, ty)
+ case ty.abs
+ when TYPE_CHAR
+ [val].pack("C").unpack("c")[0]
+ when TYPE_SHORT
+ [val].pack("S!").unpack("s!")[0]
+ when TYPE_INT
+ [val].pack("I!").unpack("i!")[0]
+ when TYPE_LONG
+ [val].pack("L!").unpack("l!")[0]
+ when TYPE_LONG_LONG
+ [val].pack("Q!").unpack("q!")[0]
+ else
+ val
+ end
+ end
+
+ def wrap_args(args, tys, funcs, &block)
+ result = []
+ tys ||= []
+ args.each_with_index{|arg, idx|
+ result.push(wrap_arg(arg, tys[idx], funcs, &block))
+ }
+ result
+ end
+
+ def wrap_arg(arg, ty, funcs, &block)
+ funcs ||= []
+ case arg
+ when nil
+ return 0
+ when CPtr
+ return arg.to_i
+ when IO
+ case ty
+ when TYPE_VOIDP
+ return CPtr[arg].to_i
+ else
+ return arg.to_i
+ end
+ when Function
+ if( block )
+ arg.bind_at_call(&block)
+ funcs.push(arg)
+ elsif !arg.bound?
+ raise(RuntimeError, "block must be given.")
+ end
+ return arg.to_i
+ when String
+ if( ty.is_a?(Array) )
+ return arg.unpack('C*')
+ else
+ case SIZEOF_VOIDP
+ when SIZEOF_LONG
+ return [arg].pack("p").unpack("l!")[0]
+ when SIZEOF_LONG_LONG
+ return [arg].pack("p").unpack("q")[0]
+ else
+ raise(RuntimeError, "sizeof(void*)?")
+ end
+ end
+ when Float, Integer
+ return arg
+ when Array
+ if( ty.is_a?(Array) ) # used only by struct
+ case ty[0]
+ when TYPE_VOIDP
+ return arg.collect{|v| Integer(v)}
+ when TYPE_CHAR
+ if( arg.is_a?(String) )
+ return val.unpack('C*')
+ end
+ end
+ return arg
+ else
+ return arg
+ end
+ else
+ if( arg.respond_to?(:to_ptr) )
+ return arg.to_ptr.to_i
+ else
+ begin
+ return Integer(arg)
+ rescue
+ raise(ArgumentError, "unknown argument type: #{arg.class}")
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/drb.rb b/ruby/lib/drb.rb
new file mode 100644
index 0000000..93cc811
--- /dev/null
+++ b/ruby/lib/drb.rb
@@ -0,0 +1,2 @@
+require 'drb/drb'
+
diff --git a/ruby/lib/drb/acl.rb b/ruby/lib/drb/acl.rb
new file mode 100644
index 0000000..861c8a5
--- /dev/null
+++ b/ruby/lib/drb/acl.rb
@@ -0,0 +1,146 @@
+# acl-2.0 - simple Access Control List
+#
+# Copyright (c) 2000,2002,2003 Masatoshi SEKI
+#
+# acl.rb is copyrighted free software by Masatoshi SEKI.
+# You can redistribute it and/or modify it under the same terms as Ruby.
+
+require 'ipaddr'
+
+class ACL
+ VERSION=["2.0.0"]
+ class ACLEntry
+ def initialize(str)
+ if str == '*' or str == 'all'
+ @pat = [:all]
+ elsif str.include?('*')
+ @pat = [:name, dot_pat(str)]
+ else
+ begin
+ @pat = [:ip, IPAddr.new(str)]
+ rescue ArgumentError
+ @pat = [:name, dot_pat(str)]
+ end
+ end
+ end
+
+ private
+ def dot_pat_str(str)
+ list = str.split('.').collect { |s|
+ (s == '*') ? '.+' : s
+ }
+ list.join("\\.")
+ end
+
+ private
+ def dot_pat(str)
+ exp = "^" + dot_pat_str(str) + "$"
+ Regexp.new(exp)
+ end
+
+ public
+ def match(addr)
+ case @pat[0]
+ when :all
+ true
+ when :ip
+ begin
+ ipaddr = IPAddr.new(addr[3])
+ ipaddr = ipaddr.ipv4_mapped if @pat[1].ipv6? && ipaddr.ipv4?
+ rescue ArgumentError
+ return false
+ end
+ (@pat[1].include?(ipaddr)) ? true : false
+ when :name
+ (@pat[1] =~ addr[2]) ? true : false
+ else
+ false
+ end
+ end
+ end
+
+ class ACLList
+ def initialize
+ @list = []
+ end
+
+ public
+ def match(addr)
+ @list.each do |e|
+ return true if e.match(addr)
+ end
+ false
+ end
+
+ public
+ def add(str)
+ @list.push(ACLEntry.new(str))
+ end
+ end
+
+ DENY_ALLOW = 0
+ ALLOW_DENY = 1
+
+ def initialize(list=nil, order = DENY_ALLOW)
+ @order = order
+ @deny = ACLList.new
+ @allow = ACLList.new
+ install_list(list) if list
+ end
+
+ public
+ def allow_socket?(soc)
+ allow_addr?(soc.peeraddr)
+ end
+
+ public
+ def allow_addr?(addr)
+ case @order
+ when DENY_ALLOW
+ return true if @allow.match(addr)
+ return false if @deny.match(addr)
+ return true
+ when ALLOW_DENY
+ return false if @deny.match(addr)
+ return true if @allow.match(addr)
+ return false
+ else
+ false
+ end
+ end
+
+ public
+ def install_list(list)
+ i = 0
+ while i < list.size
+ permission, domain = list.slice(i,2)
+ case permission.downcase
+ when 'allow'
+ @allow.add(domain)
+ when 'deny'
+ @deny.add(domain)
+ else
+ raise "Invalid ACL entry #{list.to_s}"
+ end
+ i += 2
+ end
+ end
+end
+
+if __FILE__ == $0
+ # example
+ list = %w(deny all
+ allow 192.168.1.1
+ allow ::ffff:192.168.1.2
+ allow 192.168.1.3
+ )
+
+ addr = ["AF_INET", 10, "lc630", "192.168.1.3"]
+
+ acl = ACL.new
+ p acl.allow_addr?(addr)
+
+ acl = ACL.new(list, ACL::DENY_ALLOW)
+ p acl.allow_addr?(addr)
+end
+
diff --git a/ruby/lib/drb/drb.rb b/ruby/lib/drb/drb.rb
new file mode 100644
index 0000000..b6b7650
--- /dev/null
+++ b/ruby/lib/drb/drb.rb
@@ -0,0 +1,1778 @@
+#
+# = drb/drb.rb
+#
+# Distributed Ruby: _dRuby_ version 2.0.4
+#
+# Copyright (c) 1999-2003 Masatoshi SEKI. You can redistribute it and/or
+# modify it under the same terms as Ruby.
+#
+# Author:: Masatoshi SEKI
+#
+# Documentation:: William Webber (william@williamwebber.com)
+#
+# == Overview
+#
+# dRuby is a distributed object system for Ruby. It allows an object in one
+# Ruby process to invoke methods on an object in another Ruby process on the
+# same or a different machine.
+#
+# The Ruby standard library contains the core classes of the dRuby package.
+# However, the full package also includes access control lists and the
+# Rinda tuple-space distributed task management system, as well as a
+# large number of samples. The full dRuby package can be downloaded from
+# the dRuby home page (see *References*).
+#
+# For an introduction and examples of usage see the documentation to the
+# DRb module.
+#
+# == References
+#
+# [http://www2a.biglobe.ne.jp/~seki/ruby/druby.html]
+# The dRuby home page, in Japanese. Contains the full dRuby package
+# and links to other Japanese-language sources.
+#
+# [http://www2a.biglobe.ne.jp/~seki/ruby/druby.en.html]
+# The English version of the dRuby home page.
+#
+# [http://www.chadfowler.com/ruby/drb.html]
+# A quick tutorial introduction to using dRuby by Chad Fowler.
+#
+# [http://www.linux-mag.com/2002-09/ruby_05.html]
+# A tutorial introduction to dRuby in Linux Magazine by Dave Thomas.
+# Includes a discussion of Rinda.
+#
+# [http://www.eng.cse.dmu.ac.uk/~hgs/ruby/dRuby/]
+# Links to English-language Ruby material collected by Hugh Sasse.
+#
+# [http://www.rubycentral.com/book/ospace.html]
+# The chapter from *Programming* *Ruby* by Dave Thomas and Andy Hunt
+# which discusses dRuby.
+#
+# [http://www.clio.ne.jp/home/web-i31s/Flotuard/Ruby/PRC2K_seki/dRuby.en.html]
+# Translation of presentation on Ruby by Masatoshi Seki.
+
+require 'socket'
+require 'thread'
+require 'fcntl'
+require 'drb/eq'
+
+#
+# == Overview
+#
+# dRuby is a distributed object system for Ruby. It is written in
+# pure Ruby and uses its own protocol. No add-in services are needed
+# beyond those provided by the Ruby runtime, such as TCP sockets. It
+# does not rely on or interoperate with other distributed object
+# systems such as CORBA, RMI, or .NET.
+#
+# dRuby allows methods to be called in one Ruby process upon a Ruby
+# object located in another Ruby process, even on another machine.
+# References to objects can be passed between processes. Method
+# arguments and return values are dumped and loaded in marshalled
+# format. All of this is done transparently to both the caller of the
+# remote method and the object that it is called upon.
+#
+# An object in a remote process is locally represented by a
+# DRb::DRbObject instance. This acts as a sort of proxy for the
+# remote object. Methods called upon this DRbObject instance are
+# forwarded to its remote object. This is arranged dynamically at run
+# time. There are no statically declared interfaces for remote
+# objects, such as CORBA's IDL.
+#
+# dRuby calls made into a process are handled by a DRb::DRbServer
+# instance within that process. This reconstitutes the method call,
+# invokes it upon the specified local object, and returns the value to
+# the remote caller. Any object can receive calls over dRuby. There
+# is no need to implement a special interface, or mixin special
+# functionality. Nor, in the general case, does an object need to
+# explicitly register itself with a DRbServer in order to receive
+# dRuby calls.
+#
+# One process wishing to make dRuby calls upon another process must
+# somehow obtain an initial reference to an object in the remote
+# process by some means other than as the return value of a remote
+# method call, as there is initially no remote object reference it can
+# invoke a method upon. This is done by attaching to the server by
+# URI. Each DRbServer binds itself to a URI such as
+# 'druby://example.com:8787'. A DRbServer can have an object attached
+# to it that acts as the server's *front* *object*. A DRbObject can
+# be explicitly created from the server's URI. This DRbObject's
+# remote object will be the server's front object. This front object
+# can then return references to other Ruby objects in the DRbServer's
+# process.
+#
+# Method calls made over dRuby behave largely the same as normal Ruby
+# method calls made within a process. Method calls with blocks are
+# supported, as are raising exceptions. In addition to a method's
+# standard errors, a dRuby call may also raise one of the
+# dRuby-specific errors, all of which are subclasses of DRb::DRbError.
+#
+# Any type of object can be passed as an argument to a dRuby call or
+# returned as its return value. By default, such objects are dumped
+# or marshalled at the local end, then loaded or unmarshalled at the
+# remote end. The remote end therefore receives a copy of the local
+# object, not a distributed reference to it; methods invoked upon this
+# copy are executed entirely in the remote process, not passed on to
+# the local original. This has semantics similar to pass-by-value.
+#
+# However, if an object cannot be marshalled, a dRuby reference to it
+# is passed or returned instead. This will turn up at the remote end
+# as a DRbObject instance. All methods invoked upon this remote proxy
+# are forwarded to the local object, as described in the discussion of
+# DRbObjects. This has semantics similar to the normal Ruby
+# pass-by-reference.
+#
+# The easiest way to signal that we want an otherwise marshallable
+# object to be passed or returned as a DRbObject reference, rather
+# than marshalled and sent as a copy, is to include the
+# DRb::DRbUndumped mixin module.
+#
+# dRuby supports calling remote methods with blocks. As blocks (or
+# rather the Proc objects that represent them) are not marshallable,
+# the block executes in the local, not the remote, context. Each
+# value yielded to the block is passed from the remote object to the
+# local block, then the value returned by each block invocation is
+# passed back to the remote execution context to be collected, before
+# the collected values are finally returned to the local context as
+# the return value of the method invocation.
+#
+# == Examples of usage
+#
+# For more dRuby samples, see the +samples+ directory in the full
+# dRuby distribution.
+#
+# === dRuby in client/server mode
+#
+# This illustrates setting up a simple client-server drb
+# system. Run the server and client code in different terminals,
+# starting the server code first.
+#
+# ==== Server code
+#
+# require 'drb/drb'
+#
+# # The URI for the server to connect to
+# URI="druby://localhost:8787"
+#
+# class TimeServer
+#
+# def get_current_time
+# return Time.now
+# end
+#
+# end
+#
+# # The object that handles requests on the server
+# FRONT_OBJECT=TimeServer.new
+#
+# $SAFE = 1 # disable eval() and friends
+#
+# DRb.start_service(URI, FRONT_OBJECT)
+# # Wait for the drb server thread to finish before exiting.
+# DRb.thread.join
+#
+# ==== Client code
+#
+# require 'drb/drb'
+#
+# # The URI to connect to
+# SERVER_URI="druby://localhost:8787"
+#
+# # Start a local DRbServer to handle callbacks.
+# #
+# # Not necessary for this small example, but will be required
+# # as soon as we pass a non-marshallable object as an argument
+# # to a dRuby call.
+# DRb.start_service
+#
+# timeserver = DRbObject.new_with_uri(SERVER_URI)
+# puts timeserver.get_current_time
+#
+# === Remote objects under dRuby
+#
+# This example illustrates returning a reference to an object
+# from a dRuby call. The Logger instances live in the server
+# process. References to them are returned to the client process,
+# where methods can be invoked upon them. These methods are
+# executed in the server process.
+#
+# ==== Server code
+#
+# require 'drb/drb'
+#
+# URI="druby://localhost:8787"
+#
+# class Logger
+#
+# # Make dRuby send Logger instances as dRuby references,
+# # not copies.
+# include DRb::DRbUndumped
+#
+# def initialize(n, fname)
+# @name = n
+# @filename = fname
+# end
+#
+# def log(message)
+# File.open(@filename, "a") do |f|
+# f.puts("#{Time.now}: #{@name}: #{message}")
+# end
+# end
+#
+# end
+#
+# # We have a central object for creating and retrieving loggers.
+# # This retains a local reference to all loggers created. This
+# # is so an existing logger can be looked up by name, but also
+# # to prevent loggers from being garbage collected. A dRuby
+# # reference to an object is not sufficient to prevent it being
+# # garbage collected!
+# class LoggerFactory
+#
+# def initialize(bdir)
+# @basedir = bdir
+# @loggers = {}
+# end
+#
+# def get_logger(name)
+# if !@loggers.has_key? name
+# # make the filename safe, then declare it to be so
+# fname = name.gsub(/[.\/]/, "_").untaint
+# @loggers[name] = Logger.new(name, @basedir + "/" + fname)
+# end
+# return @loggers[name]
+# end
+#
+# end
+#
+# FRONT_OBJECT=LoggerFactory.new("/tmp/dlog")
+#
+# $SAFE = 1 # disable eval() and friends
+#
+# DRb.start_service(URI, FRONT_OBJECT)
+# DRb.thread.join
+#
+# ==== Client code
+#
+# require 'drb/drb'
+#
+# SERVER_URI="druby://localhost:8787"
+#
+# DRb.start_service
+#
+# log_service=DRbObject.new_with_uri(SERVER_URI)
+#
+# ["loga", "logb", "logc"].each do |logname|
+#
+# logger=log_service.get_logger(logname)
+#
+# logger.log("Hello, world!")
+# logger.log("Goodbye, world!")
+# logger.log("=== EOT ===")
+#
+# end
+#
+# == Security
+#
+# As with all network services, security needs to be considered when
+# using dRuby. By allowing external access to a Ruby object, you are
+# not only allowing outside clients to call the methods you have
+# defined for that object, but by default to execute arbitrary Ruby
+# code on your server. Consider the following:
+#
+# # !!! UNSAFE CODE !!!
+# ro = DRbObject::new_with_uri("druby://your.server.com:8989")
+# class << ro
+# undef :instance_eval # force call to be passed to remote object
+# end
+# ro.instance_eval("`rm -rf *`")
+#
+# The dangers posed by instance_eval and friends are such that a
+# DRbServer should generally be run with $SAFE set to at least
+# level 1. This will disable eval() and related calls on strings
+# passed across the wire. The sample usage code given above follows
+# this practice.
+#
+# A DRbServer can be configured with an access control list to
+# selectively allow or deny access from specified IP addresses. The
+# main druby distribution provides the ACL class for this purpose. In
+# general, this mechanism should only be used alongside, rather than
+# as a replacement for, a good firewall.
+#
+# == dRuby internals
+#
+# dRuby is implemented using three main components: a remote method
+# call marshaller/unmarshaller; a transport protocol; and an
+# ID-to-object mapper. The latter two can be directly, and the first
+# indirectly, replaced, in order to provide different behaviour and
+# capabilities.
+#
+# Marshalling and unmarshalling of remote method calls is performed by
+# a DRb::DRbMessage instance. This uses the Marshal module to dump
+# the method call before sending it over the transport layer, then
+# reconstitute it at the other end. There is normally no need to
+# replace this component, and no direct way is provided to do so.
+# However, it is possible to implement an alternative marshalling
+# scheme as part of an implementation of the transport layer.
+#
+# The transport layer is responsible for opening client and server
+# network connections and forwarding dRuby request across them.
+# Normally, it uses DRb::DRbMessage internally to manage marshalling
+# and unmarshalling. The transport layer is managed by
+# DRb::DRbProtocol. Multiple protocols can be installed in
+# DRbProtocol at the one time; selection between them is determined by
+# the scheme of a dRuby URI. The default transport protocol is
+# selected by the scheme 'druby:', and implemented by
+# DRb::DRbTCPSocket. This uses plain TCP/IP sockets for
+# communication. An alternative protocol, using UNIX domain sockets,
+# is implemented by DRb::DRbUNIXSocket in the file drb/unix.rb, and
+# selected by the scheme 'drbunix:'. A sample implementation over
+# HTTP can be found in the samples accompanying the main dRuby
+# distribution.
+#
+# The ID-to-object mapping component maps dRuby object ids to the
+# objects they refer to, and vice versa. The implementation to use
+# can be specified as part of a DRb::DRbServer's configuration. The
+# default implementation is provided by DRb::DRbIdConv. It uses an
+# object's ObjectSpace id as its dRuby id. This means that the dRuby
+# reference to that object only remains meaningful for the lifetime of
+# the object's process and the lifetime of the object within that
+# process. A modified implementation is provided by DRb::TimerIdConv
+# in the file drb/timeridconv.rb. This implementation retains a local
+# reference to all objects exported over dRuby for a configurable
+# period of time (defaulting to ten minutes), to prevent them being
+# garbage-collected within this time. Another sample implementation
+# is provided in sample/name.rb in the main dRuby distribution. This
+# allows objects to specify their own id or "name". A dRuby reference
+# can be made persistent across processes by having each process
+# register an object using the same dRuby name.
+#
+module DRb
+
+ # Superclass of all errors raised in the DRb module.
+ class DRbError < RuntimeError; end
+
+ # Error raised when an error occurs on the underlying communication
+ # protocol.
+ class DRbConnError < DRbError; end
+
+ # Class responsible for converting between an object and its id.
+ #
+ # This, the default implementation, uses an object's local ObjectSpace
+ # __id__ as its id. This means that an object's identification over
+ # drb remains valid only while that object instance remains alive
+ # within the server runtime.
+ #
+ # For alternative mechanisms, see DRb::TimerIdConv in rdb/timeridconv.rb
+ # and DRbNameIdConv in sample/name.rb in the full drb distribution.
+ class DRbIdConv
+
+ # Convert an object reference id to an object.
+ #
+ # This implementation looks up the reference id in the local object
+ # space and returns the object it refers to.
+ def to_obj(ref)
+ ObjectSpace._id2ref(ref)
+ end
+
+ # Convert an object into a reference id.
+ #
+ # This implementation returns the object's __id__ in the local
+ # object space.
+ def to_id(obj)
+ obj.nil? ? nil : obj.__id__
+ end
+ end
+
+ # Mixin module making an object undumpable or unmarshallable.
+ #
+ # If an object which includes this module is returned by method
+ # called over drb, then the object remains in the server space
+ # and a reference to the object is returned, rather than the
+ # object being marshalled and moved into the client space.
+ module DRbUndumped
+ def _dump(dummy) # :nodoc:
+ raise TypeError, 'can\'t dump'
+ end
+ end
+
+ # Error raised by the DRb module when an attempt is made to refer to
+ # the context's current drb server but the context does not have one.
+ # See #current_server.
+ class DRbServerNotFound < DRbError; end
+
+ # Error raised by the DRbProtocol module when it cannot find any
+ # protocol implementation support the scheme specified in a URI.
+ class DRbBadURI < DRbError; end
+
+ # Error raised by a dRuby protocol when it doesn't support the
+ # scheme specified in a URI. See DRb::DRbProtocol.
+ class DRbBadScheme < DRbError; end
+
+ # An exception wrapping a DRb::DRbUnknown object
+ class DRbUnknownError < DRbError
+
+ # Create a new DRbUnknownError for the DRb::DRbUnknown object +unknown+
+ def initialize(unknown)
+ @unknown = unknown
+ super(unknown.name)
+ end
+
+ # Get the wrapped DRb::DRbUnknown object.
+ attr_reader :unknown
+
+ def self._load(s) # :nodoc:
+ Marshal::load(s)
+ end
+
+ def _dump(lv) # :nodoc:
+ Marshal::dump(@unknown)
+ end
+ end
+
+ # An exception wrapping an error object
+ class DRbRemoteError < DRbError
+ def initialize(error)
+ @reason = error.class.to_s
+ super("#{error.message} (#{error.class})")
+ set_backtrace(error.backtrace)
+ end
+
+ # the class of the error, as a string.
+ attr_reader :reason
+ end
+
+ # Class wrapping a marshalled object whose type is unknown locally.
+ #
+ # If an object is returned by a method invoked over drb, but the
+ # class of the object is unknown in the client namespace, or
+ # the object is a constant unknown in the client namespace, then
+ # the still-marshalled object is returned wrapped in a DRbUnknown instance.
+ #
+ # If this object is passed as an argument to a method invoked over
+ # drb, then the wrapped object is passed instead.
+ #
+ # The class or constant name of the object can be read from the
+ # +name+ attribute. The marshalled object is held in the +buf+
+ # attribute.
+ class DRbUnknown
+
+ # Create a new DRbUnknown object.
+ #
+ # +buf+ is a string containing a marshalled object that could not
+ # be unmarshalled. +err+ is the error message that was raised
+ # when the unmarshalling failed. It is used to determine the
+ # name of the unmarshalled object.
+ def initialize(err, buf)
+ case err.to_s
+ when /uninitialized constant (\S+)/
+ @name = $1
+ when /undefined class\/module (\S+)/
+ @name = $1
+ else
+ @name = nil
+ end
+ @buf = buf
+ end
+
+ # The name of the unknown thing.
+ #
+ # Class name for unknown objects; variable name for unknown
+ # constants.
+ attr_reader :name
+
+ # Buffer contained the marshalled, unknown object.
+ attr_reader :buf
+
+ def self._load(s) # :nodoc:
+ begin
+ Marshal::load(s)
+ rescue NameError, ArgumentError
+ DRbUnknown.new($!, s)
+ end
+ end
+
+ def _dump(lv) # :nodoc:
+ @buf
+ end
+
+ # Attempt to load the wrapped marshalled object again.
+ #
+ # If the class of the object is now known locally, the object
+ # will be unmarshalled and returned. Otherwise, a new
+ # but identical DRbUnknown object will be returned.
+ def reload
+ self.class._load(@buf)
+ end
+
+ # Create a DRbUnknownError exception containing this object.
+ def exception
+ DRbUnknownError.new(self)
+ end
+ end
+
+ class DRbArray
+ def initialize(ary)
+ @ary = ary.collect { |obj|
+ if obj.kind_of? DRbUndumped
+ DRbObject.new(obj)
+ else
+ begin
+ Marshal.dump(obj)
+ obj
+ rescue
+ DRbObject.new(obj)
+ end
+ end
+ }
+ end
+
+ def self._load(s)
+ Marshal::load(s)
+ end
+
+ def _dump(lv)
+ Marshal.dump(@ary)
+ end
+ end
+
+ # Handler for sending and receiving drb messages.
+ #
+ # This takes care of the low-level marshalling and unmarshalling
+ # of drb requests and responses sent over the wire between server
+ # and client. This relieves the implementor of a new drb
+ # protocol layer with having to deal with these details.
+ #
+ # The user does not have to directly deal with this object in
+ # normal use.
+ class DRbMessage
+ def initialize(config) # :nodoc:
+ @load_limit = config[:load_limit]
+ @argc_limit = config[:argc_limit]
+ end
+
+ def dump(obj, error=false) # :nodoc:
+ obj = make_proxy(obj, error) if obj.kind_of? DRbUndumped
+ begin
+ str = Marshal::dump(obj)
+ rescue
+ str = Marshal::dump(make_proxy(obj, error))
+ end
+ [str.size].pack('N') + str
+ end
+
+ def load(soc) # :nodoc:
+ begin
+ sz = soc.read(4) # sizeof (N)
+ rescue
+ raise(DRbConnError, $!.message, $!.backtrace)
+ end
+ raise(DRbConnError, 'connection closed') if sz.nil?
+ raise(DRbConnError, 'premature header') if sz.size < 4
+ sz = sz.unpack('N')[0]
+ raise(DRbConnError, "too large packet #{sz}") if @load_limit < sz
+ begin
+ str = soc.read(sz)
+ rescue
+ raise(DRbConnError, $!.message, $!.backtrace)
+ end
+ raise(DRbConnError, 'connection closed') if str.nil?
+ raise(DRbConnError, 'premature marshal format(can\'t read)') if str.size < sz
+ DRb.mutex.synchronize do
+ begin
+ save = Thread.current[:drb_untaint]
+ Thread.current[:drb_untaint] = []
+ Marshal::load(str)
+ rescue NameError, ArgumentError
+ DRbUnknown.new($!, str)
+ ensure
+ Thread.current[:drb_untaint].each do |x|
+ x.untaint
+ end
+ Thread.current[:drb_untaint] = save
+ end
+ end
+ end
+
+ def send_request(stream, ref, msg_id, arg, b) # :nodoc:
+ ary = []
+ ary.push(dump(ref.__drbref))
+ ary.push(dump(msg_id.id2name))
+ ary.push(dump(arg.length))
+ arg.each do |e|
+ ary.push(dump(e))
+ end
+ ary.push(dump(b))
+ stream.write(ary.join(''))
+ rescue
+ raise(DRbConnError, $!.message, $!.backtrace)
+ end
+
+ def recv_request(stream) # :nodoc:
+ ref = load(stream)
+ ro = DRb.to_obj(ref)
+ msg = load(stream)
+ argc = load(stream)
+ raise ArgumentError, 'too many arguments' if @argc_limit < argc
+ argv = Array.new(argc, nil)
+ argc.times do |n|
+ argv[n] = load(stream)
+ end
+ block = load(stream)
+ return ro, msg, argv, block
+ end
+
+ def send_reply(stream, succ, result) # :nodoc:
+ stream.write(dump(succ) + dump(result, !succ))
+ rescue
+ raise(DRbConnError, $!.message, $!.backtrace)
+ end
+
+ def recv_reply(stream) # :nodoc:
+ succ = load(stream)
+ result = load(stream)
+ [succ, result]
+ end
+
+ private
+ def make_proxy(obj, error=false)
+ if error
+ DRbRemoteError.new(obj)
+ else
+ DRbObject.new(obj)
+ end
+ end
+ end
+
+ # Module managing the underlying network protocol(s) used by drb.
+ #
+ # By default, drb uses the DRbTCPSocket protocol. Other protocols
+ # can be defined. A protocol must define the following class methods:
+ #
+ # [open(uri, config)] Open a client connection to the server at +uri+,
+ # using configuration +config+. Return a protocol
+ # instance for this connection.
+ # [open_server(uri, config)] Open a server listening at +uri+,
+ # using configuration +config+. Return a
+ # protocol instance for this listener.
+ # [uri_option(uri, config)] Take a URI, possibly containing an option
+ # component (e.g. a trailing '?param=val'),
+ # and return a [uri, option] tuple.
+ #
+ # All of these methods should raise a DRbBadScheme error if the URI
+ # does not identify the protocol they support (e.g. "druby:" for
+ # the standard Ruby protocol). This is how the DRbProtocol module,
+ # given a URI, determines which protocol implementation serves that
+ # protocol.
+ #
+ # The protocol instance returned by #open_server must have the
+ # following methods:
+ #
+ # [accept] Accept a new connection to the server. Returns a protocol
+ # instance capable of communicating with the client.
+ # [close] Close the server connection.
+ # [uri] Get the URI for this server.
+ #
+ # The protocol instance returned by #open must have the following methods:
+ #
+ # [send_request (ref, msg_id, arg, b)]
+ # Send a request to +ref+ with the given message id and arguments.
+ # This is most easily implemented by calling DRbMessage.send_request,
+ # providing a stream that sits on top of the current protocol.
+ # [recv_reply]
+ # Receive a reply from the server and return it as a [success-boolean,
+ # reply-value] pair. This is most easily implemented by calling
+ # DRb.recv_reply, providing a stream that sits on top of the
+ # current protocol.
+ # [alive?]
+ # Is this connection still alive?
+ # [close]
+ # Close this connection.
+ #
+ # The protocol instance returned by #open_server().accept() must have
+ # the following methods:
+ #
+ # [recv_request]
+ # Receive a request from the client and return a [object, message,
+ # args, block] tuple. This is most easily implemented by calling
+ # DRbMessage.recv_request, providing a stream that sits on top of
+ # the current protocol.
+ # [send_reply(succ, result)]
+ # Send a reply to the client. This is most easily implemented
+ # by calling DRbMessage.send_reply, providing a stream that sits
+ # on top of the current protocol.
+ # [close]
+ # Close this connection.
+ #
+ # A new protocol is registered with the DRbProtocol module using
+ # the add_protocol method.
+ #
+ # For examples of other protocols, see DRbUNIXSocket in drb/unix.rb,
+ # and HTTP0 in sample/http0.rb and sample/http0serv.rb in the full
+ # drb distribution.
+ module DRbProtocol
+
+ # Add a new protocol to the DRbProtocol module.
+ def add_protocol(prot)
+ @protocol.push(prot)
+ end
+ module_function :add_protocol
+
+ # Open a client connection to +uri+ with the configuration +config+.
+ #
+ # The DRbProtocol module asks each registered protocol in turn to
+ # try to open the URI. Each protocol signals that it does not handle that
+ # URI by raising a DRbBadScheme error. If no protocol recognises the
+ # URI, then a DRbBadURI error is raised. If a protocol accepts the
+ # URI, but an error occurs in opening it, a DRbConnError is raised.
+ def open(uri, config, first=true)
+ @protocol.each do |prot|
+ begin
+ return prot.open(uri, config)
+ rescue DRbBadScheme
+ rescue DRbConnError
+ raise($!)
+ rescue
+ raise(DRbConnError, "#{uri} - #{$!.inspect}")
+ end
+ end
+ if first && (config[:auto_load] != false)
+ auto_load(uri, config)
+ return open(uri, config, false)
+ end
+ raise DRbBadURI, 'can\'t parse uri:' + uri
+ end
+ module_function :open
+
+ # Open a server listening for connections at +uri+ with
+ # configuration +config+.
+ #
+ # The DRbProtocol module asks each registered protocol in turn to
+ # try to open a server at the URI. Each protocol signals that it does
+ # not handle that URI by raising a DRbBadScheme error. If no protocol
+ # recognises the URI, then a DRbBadURI error is raised. If a protocol
+ # accepts the URI, but an error occurs in opening it, the underlying
+ # error is passed on to the caller.
+ def open_server(uri, config, first=true)
+ @protocol.each do |prot|
+ begin
+ return prot.open_server(uri, config)
+ rescue DRbBadScheme
+ end
+ end
+ if first && (config[:auto_load] != false)
+ auto_load(uri, config)
+ return open_server(uri, config, false)
+ end
+ raise DRbBadURI, 'can\'t parse uri:' + uri
+ end
+ module_function :open_server
+
+ # Parse +uri+ into a [uri, option] pair.
+ #
+ # The DRbProtocol module asks each registered protocol in turn to
+ # try to parse the URI. Each protocol signals that it does not handle that
+ # URI by raising a DRbBadScheme error. If no protocol recognises the
+ # URI, then a DRbBadURI error is raised.
+ def uri_option(uri, config, first=true)
+ @protocol.each do |prot|
+ begin
+ uri, opt = prot.uri_option(uri, config)
+ # opt = nil if opt == ''
+ return uri, opt
+ rescue DRbBadScheme
+ end
+ end
+ if first && (config[:auto_load] != false)
+ auto_load(uri, config)
+ return uri_option(uri, config, false)
+ end
+ raise DRbBadURI, 'can\'t parse uri:' + uri
+ end
+ module_function :uri_option
+
+ def auto_load(uri, config) # :nodoc:
+ if uri =~ /^drb([a-z0-9]+):/
+ require("drb/#{$1}") rescue nil
+ end
+ end
+ module_function :auto_load
+ end
+
+ # The default drb protocol.
+ #
+ # Communicates over a TCP socket.
+ class DRbTCPSocket
+ private
+ def self.parse_uri(uri)
+ if uri =~ /^druby:\/\/(.*?):(\d+)(\?(.*))?$/
+ host = $1
+ port = $2.to_i
+ option = $4
+ [host, port, option]
+ else
+ raise(DRbBadScheme, uri) unless uri =~ /^druby:/
+ raise(DRbBadURI, 'can\'t parse uri:' + uri)
+ end
+ end
+
+ public
+
+ # Open a client connection to +uri+ using configuration +config+.
+ def self.open(uri, config)
+ host, port, option = parse_uri(uri)
+ host.untaint
+ port.untaint
+ soc = TCPSocket.open(host, port)
+ self.new(uri, soc, config)
+ end
+
+ def self.getservername
+ host = Socket::gethostname
+ begin
+ Socket::gethostbyname(host)[0]
+ rescue
+ 'localhost'
+ end
+ end
+
+ def self.open_server_inaddr_any(host, port)
+ infos = Socket::getaddrinfo(host, nil,
+ Socket::AF_UNSPEC,
+ Socket::SOCK_STREAM,
+ 0,
+ Socket::AI_PASSIVE)
+ families = Hash[*infos.collect { |af, *_| af }.uniq.zip([]).flatten]
+ return TCPServer.open('0.0.0.0', port) if families.has_key?('AF_INET')
+ return TCPServer.open('::', port) if families.has_key?('AF_INET6')
+ return TCPServer.open(port)
+ end
+
+ # Open a server listening for connections at +uri+ using
+ # configuration +config+.
+ def self.open_server(uri, config)
+ uri = 'druby://:0' unless uri
+ host, port, opt = parse_uri(uri)
+ config = {:tcp_original_host => host}.update(config)
+ if host.size == 0
+ host = getservername
+ soc = open_server_inaddr_any(host, port)
+ else
+ soc = TCPServer.open(host, port)
+ end
+ port = soc.addr[1] if port == 0
+ config[:tcp_port] = port
+ uri = "druby://#{host}:#{port}"
+ self.new(uri, soc, config)
+ end
+
+ # Parse +uri+ into a [uri, option] pair.
+ def self.uri_option(uri, config)
+ host, port, option = parse_uri(uri)
+ return "druby://#{host}:#{port}", option
+ end
+
+ # Create a new DRbTCPSocket instance.
+ #
+ # +uri+ is the URI we are connected to.
+ # +soc+ is the tcp socket we are bound to. +config+ is our
+ # configuration.
+ def initialize(uri, soc, config={})
+ @uri = uri
+ @socket = soc
+ @config = config
+ @acl = config[:tcp_acl]
+ @msg = DRbMessage.new(config)
+ set_sockopt(@socket)
+ end
+
+ # Get the URI that we are connected to.
+ attr_reader :uri
+
+ # Get the address of our TCP peer (the other end of the socket
+ # we are bound to.
+ def peeraddr
+ @socket.peeraddr
+ end
+
+ # Get the socket.
+ def stream; @socket; end
+
+ # On the client side, send a request to the server.
+ def send_request(ref, msg_id, arg, b)
+ @msg.send_request(stream, ref, msg_id, arg, b)
+ end
+
+ # On the server side, receive a request from the client.
+ def recv_request
+ @msg.recv_request(stream)
+ end
+
+ # On the server side, send a reply to the client.
+ def send_reply(succ, result)
+ @msg.send_reply(stream, succ, result)
+ end
+
+ # On the client side, receive a reply from the server.
+ def recv_reply
+ @msg.recv_reply(stream)
+ end
+
+ public
+
+ # Close the connection.
+ #
+ # If this is an instance returned by #open_server, then this stops
+ # listening for new connections altogether. If this is an instance
+ # returned by #open or by #accept, then it closes this particular
+ # client-server session.
+ def close
+ if @socket
+ @socket.close
+ @socket = nil
+ end
+ end
+
+ # On the server side, for an instance returned by #open_server,
+ # accept a client connection and return a new instance to handle
+ # the server's side of this client-server session.
+ def accept
+ while true
+ s = @socket.accept
+ break if (@acl ? @acl.allow_socket?(s) : true)
+ s.close
+ end
+ if @config[:tcp_original_host].to_s.size == 0
+ uri = "druby://#{s.addr[3]}:#{@config[:tcp_port]}"
+ else
+ uri = @uri
+ end
+ self.class.new(uri, s, @config)
+ end
+
+ # Check to see if this connection is alive.
+ def alive?
+ return false unless @socket
+ if IO.select([@socket], nil, nil, 0)
+ close
+ return false
+ end
+ true
+ end
+
+ def set_sockopt(soc) # :nodoc:
+ soc.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
+ soc.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::FD_CLOEXEC
+ end
+ end
+
+ module DRbProtocol
+ @protocol = [DRbTCPSocket] # default
+ end
+
+ class DRbURIOption # :nodoc: I don't understand the purpose of this class...
+ def initialize(option)
+ @option = option.to_s
+ end
+ attr :option
+ def to_s; @option; end
+
+ def ==(other)
+ return false unless DRbURIOption === other
+ @option == other.option
+ end
+
+ def hash
+ @option.hash
+ end
+
+ alias eql? ==
+ end
+
+ # Object wrapping a reference to a remote drb object.
+ #
+ # Method calls on this object are relayed to the remote
+ # object that this object is a stub for.
+ class DRbObject
+
+ # Unmarshall a marshalled DRbObject.
+ #
+ # If the referenced object is located within the local server, then
+ # the object itself is returned. Otherwise, a new DRbObject is
+ # created to act as a stub for the remote referenced object.
+ def self._load(s)
+ uri, ref = Marshal.load(s)
+
+ if DRb.here?(uri)
+ obj = DRb.to_obj(ref)
+ if ((! obj.tainted?) && Thread.current[:drb_untaint])
+ Thread.current[:drb_untaint].push(obj)
+ end
+ return obj
+ end
+
+ self.new_with(uri, ref)
+ end
+
+ def self.new_with(uri, ref)
+ it = self.allocate
+ it.instance_variable_set('@uri', uri)
+ it.instance_variable_set('@ref', ref)
+ it
+ end
+
+ # Create a new DRbObject from a URI alone.
+ def self.new_with_uri(uri)
+ self.new(nil, uri)
+ end
+
+ # Marshall this object.
+ #
+ # The URI and ref of the object are marshalled.
+ def _dump(lv)
+ Marshal.dump([@uri, @ref])
+ end
+
+ # Create a new remote object stub.
+ #
+ # +obj+ is the (local) object we want to create a stub for. Normally
+ # this is +nil+. +uri+ is the URI of the remote object that this
+ # will be a stub for.
+ def initialize(obj, uri=nil)
+ @uri = nil
+ @ref = nil
+ if obj.nil?
+ return if uri.nil?
+ @uri, option = DRbProtocol.uri_option(uri, DRb.config)
+ @ref = DRbURIOption.new(option) unless option.nil?
+ else
+ @uri = uri ? uri : (DRb.uri rescue nil)
+ @ref = obj ? DRb.to_id(obj) : nil
+ end
+ end
+
+ # Get the URI of the remote object.
+ def __drburi
+ @uri
+ end
+
+ # Get the reference of the object, if local.
+ def __drbref
+ @ref
+ end
+
+ undef :to_s
+ undef :to_a if respond_to?(:to_a)
+
+ def respond_to?(msg_id, priv=false)
+ case msg_id
+ when :_dump
+ true
+ when :marshal_dump
+ false
+ else
+ method_missing(:respond_to?, msg_id, priv)
+ end
+ end
+
+ # Routes method calls to the referenced object.
+ def method_missing(msg_id, *a, &b)
+ if DRb.here?(@uri)
+ obj = DRb.to_obj(@ref)
+ DRb.current_server.check_insecure_method(obj, msg_id)
+ return obj.__send__(msg_id, *a, &b)
+ end
+
+ succ, result = self.class.with_friend(@uri) do
+ DRbConn.open(@uri) do |conn|
+ conn.send_message(self, msg_id, a, b)
+ end
+ end
+
+ if succ
+ return result
+ elsif DRbUnknown === result
+ raise result
+ else
+ bt = self.class.prepare_backtrace(@uri, result)
+ result.set_backtrace(bt + caller)
+ raise result
+ end
+ end
+
+ def self.with_friend(uri)
+ friend = DRb.fetch_server(uri)
+ return yield() unless friend
+
+ save = Thread.current['DRb']
+ Thread.current['DRb'] = { 'server' => friend }
+ return yield
+ ensure
+ Thread.current['DRb'] = save if friend
+ end
+
+ def self.prepare_backtrace(uri, result)
+ prefix = "(#{uri}) "
+ bt = []
+ result.backtrace.each do |x|
+ break if /`__send__'$/ =~ x
+ if /^\(druby:\/\// =~ x
+ bt.push(x)
+ else
+ bt.push(prefix + x)
+ end
+ end
+ bt
+ end
+
+ def pretty_print(q) # :nodoc:
+ q.pp_object(self)
+ end
+
+ def pretty_print_cycle(q) # :nodoc:
+ q.object_address_group(self) {
+ q.breakable
+ q.text '...'
+ }
+ end
+ end
+
+ # Class handling the connection between a DRbObject and the
+ # server the real object lives on.
+ #
+ # This class maintains a pool of connections, to reduce the
+ # overhead of starting and closing down connections for each
+ # method call.
+ #
+ # This class is used internally by DRbObject. The user does
+ # not normally need to deal with it directly.
+ class DRbConn
+ POOL_SIZE = 16 # :nodoc:
+ @mutex = Mutex.new
+ @pool = []
+
+ def self.open(remote_uri) # :nodoc:
+ begin
+ conn = nil
+
+ @mutex.synchronize do
+ #FIXME
+ new_pool = []
+ @pool.each do |c|
+ if conn.nil? and c.uri == remote_uri
+ conn = c if c.alive?
+ else
+ new_pool.push c
+ end
+ end
+ @pool = new_pool
+ end
+
+ conn = self.new(remote_uri) unless conn
+ succ, result = yield(conn)
+ return succ, result
+
+ ensure
+ if conn
+ if succ
+ @mutex.synchronize do
+ @pool.unshift(conn)
+ @pool.pop.close while @pool.size > POOL_SIZE
+ end
+ else
+ conn.close
+ end
+ end
+ end
+ end
+
+ def initialize(remote_uri) # :nodoc:
+ @uri = remote_uri
+ @protocol = DRbProtocol.open(remote_uri, DRb.config)
+ end
+ attr_reader :uri # :nodoc:
+
+ def send_message(ref, msg_id, arg, block) # :nodoc:
+ @protocol.send_request(ref, msg_id, arg, block)
+ @protocol.recv_reply
+ end
+
+ def close # :nodoc:
+ @protocol.close
+ @protocol = nil
+ end
+
+ def alive? # :nodoc:
+ return false unless @protocol
+ @protocol.alive?
+ end
+ end
+
+ # Class representing a drb server instance.
+ #
+ # A DRbServer must be running in the local process before any incoming
+ # dRuby calls can be accepted, or any local objects can be passed as
+ # dRuby references to remote processes, even if those local objects are
+ # never actually called remotely. You do not need to start a DRbServer
+ # in the local process if you are only making outgoing dRuby calls
+ # passing marshalled parameters.
+ #
+ # Unless multiple servers are being used, the local DRbServer is normally
+ # started by calling DRb.start_service.
+ class DRbServer
+ @@acl = nil
+ @@idconv = DRbIdConv.new
+ @@secondary_server = nil
+ @@argc_limit = 256
+ @@load_limit = 256 * 102400
+ @@verbose = false
+ @@safe_level = 0
+
+ # Set the default value for the :argc_limit option.
+ #
+ # See #new(). The initial default value is 256.
+ def self.default_argc_limit(argc)
+ @@argc_limit = argc
+ end
+
+ # Set the default value for the :load_limit option.
+ #
+ # See #new(). The initial default value is 25 MB.
+ def self.default_load_limit(sz)
+ @@load_limit = sz
+ end
+
+ # Set the default value for the :acl option.
+ #
+ # See #new(). The initial default value is nil.
+ def self.default_acl(acl)
+ @@acl = acl
+ end
+
+ # Set the default value for the :id_conv option.
+ #
+ # See #new(). The initial default value is a DRbIdConv instance.
+ def self.default_id_conv(idconv)
+ @@idconv = idconv
+ end
+
+ def self.default_safe_level(level)
+ @@safe_level = level
+ end
+
+ # Set the default value of the :verbose option.
+ #
+ # See #new(). The initial default value is false.
+ def self.verbose=(on)
+ @@verbose = on
+ end
+
+ # Get the default value of the :verbose option.
+ def self.verbose
+ @@verbose
+ end
+
+ def self.make_config(hash={}) # :nodoc:
+ default_config = {
+ :idconv => @@idconv,
+ :verbose => @@verbose,
+ :tcp_acl => @@acl,
+ :load_limit => @@load_limit,
+ :argc_limit => @@argc_limit,
+ :safe_level => @@safe_level
+ }
+ default_config.update(hash)
+ end
+
+ # Create a new DRbServer instance.
+ #
+ # +uri+ is the URI to bind to. This is normally of the form
+ # 'druby://<hostname>:<port>' where <hostname> is a hostname of
+ # the local machine. If nil, then the system's default hostname
+ # will be bound to, on a port selected by the system; these value
+ # can be retrieved from the +uri+ attribute. 'druby:' specifies
+ # the default dRuby transport protocol: another protocol, such
+ # as 'drbunix:', can be specified instead.
+ #
+ # +front+ is the front object for the server, that is, the object
+ # to which remote method calls on the server will be passed. If
+ # nil, then the server will not accept remote method calls.
+ #
+ # If +config_or_acl+ is a hash, it is the configuration to
+ # use for this server. The following options are recognised:
+ #
+ # :idconv :: an id-to-object conversion object. This defaults
+ # to an instance of the class DRb::DRbIdConv.
+ # :verbose :: if true, all unsuccessful remote calls on objects
+ # in the server will be logged to $stdout. false
+ # by default.
+ # :tcp_acl :: the access control list for this server. See
+ # the ACL class from the main dRuby distribution.
+ # :load_limit :: the maximum message size in bytes accepted by
+ # the server. Defaults to 25 MB (26214400).
+ # :argc_limit :: the maximum number of arguments to a remote
+ # method accepted by the server. Defaults to
+ # 256.
+ #
+ # The default values of these options can be modified on
+ # a class-wide basis by the class methods #default_argc_limit,
+ # #default_load_limit, #default_acl, #default_id_conv,
+ # and #verbose=
+ #
+ # If +config_or_acl+ is not a hash, but is not nil, it is
+ # assumed to be the access control list for this server.
+ # See the :tcp_acl option for more details.
+ #
+ # If no other server is currently set as the primary server,
+ # this will become the primary server.
+ #
+ # The server will immediately start running in its own thread.
+ def initialize(uri=nil, front=nil, config_or_acl=nil)
+ if Hash === config_or_acl
+ config = config_or_acl.dup
+ else
+ acl = config_or_acl || @@acl
+ config = {
+ :tcp_acl => acl
+ }
+ end
+
+ @config = self.class.make_config(config)
+
+ @protocol = DRbProtocol.open_server(uri, @config)
+ @uri = @protocol.uri
+
+ @front = front
+ @idconv = @config[:idconv]
+ @safe_level = @config[:safe_level]
+
+ @grp = ThreadGroup.new
+ @thread = run
+
+ DRb.regist_server(self)
+ end
+
+ # The URI of this DRbServer.
+ attr_reader :uri
+
+ # The main thread of this DRbServer.
+ #
+ # This is the thread that listens for and accepts connections
+ # from clients, not that handles each client's request-response
+ # session.
+ attr_reader :thread
+
+ # The front object of the DRbServer.
+ #
+ # This object receives remote method calls made on the server's
+ # URI alone, with an object id.
+ attr_reader :front
+
+ # The configuration of this DRbServer
+ attr_reader :config
+
+ attr_reader :safe_level
+
+ # Set whether to operate in verbose mode.
+ #
+ # In verbose mode, failed calls are logged to stdout.
+ def verbose=(v); @config[:verbose]=v; end
+
+ # Get whether the server is in verbose mode.
+ #
+ # In verbose mode, failed calls are logged to stdout.
+ def verbose; @config[:verbose]; end
+
+ # Is this server alive?
+ def alive?
+ @thread.alive?
+ end
+
+ # Stop this server.
+ def stop_service
+ DRb.remove_server(self)
+ if Thread.current['DRb'] && Thread.current['DRb']['server'] == self
+ Thread.current['DRb']['stop_service'] = true
+ else
+ @thread.kill
+ end
+ end
+
+ # Convert a dRuby reference to the local object it refers to.
+ def to_obj(ref)
+ return front if ref.nil?
+ return front[ref.to_s] if DRbURIOption === ref
+ @idconv.to_obj(ref)
+ end
+
+ # Convert a local object to a dRuby reference.
+ def to_id(obj)
+ return nil if obj.__id__ == front.__id__
+ @idconv.to_id(obj)
+ end
+
+ private
+ def kill_sub_thread
+ Thread.new do
+ grp = ThreadGroup.new
+ grp.add(Thread.current)
+ list = @grp.list
+ while list.size > 0
+ list.each do |th|
+ th.kill if th.alive?
+ end
+ list = @grp.list
+ end
+ end
+ end
+
+ def run
+ Thread.start do
+ begin
+ while true
+ main_loop
+ end
+ ensure
+ @protocol.close if @protocol
+ kill_sub_thread
+ end
+ end
+ end
+
+ # List of insecure methods.
+ #
+ # These methods are not callable via dRuby.
+ INSECURE_METHOD = [
+ :__send__
+ ]
+
+ # Has a method been included in the list of insecure methods?
+ def insecure_method?(msg_id)
+ INSECURE_METHOD.include?(msg_id)
+ end
+
+ # Coerce an object to a string, providing our own representation if
+ # to_s is not defined for the object.
+ def any_to_s(obj)
+ obj.to_s + ":#{obj.class}"
+ rescue
+ sprintf("#<%s:0x%lx>", obj.class, obj.__id__)
+ end
+
+ # Check that a method is callable via dRuby.
+ #
+ # +obj+ is the object we want to invoke the method on. +msg_id+ is the
+ # method name, as a Symbol.
+ #
+ # If the method is an insecure method (see #insecure_method?) a
+ # SecurityError is thrown. If the method is private or undefined,
+ # a NameError is thrown.
+ def check_insecure_method(obj, msg_id)
+ return true if Proc === obj && msg_id == :__drb_yield
+ raise(ArgumentError, "#{any_to_s(msg_id)} is not a symbol") unless Symbol == msg_id.class
+ raise(SecurityError, "insecure method `#{msg_id}'") if insecure_method?(msg_id)
+
+ if obj.private_methods.include?(msg_id)
+ desc = any_to_s(obj)
+ raise NoMethodError, "private method `#{msg_id}' called for #{desc}"
+ elsif obj.protected_methods.include?(msg_id)
+ desc = any_to_s(obj)
+ raise NoMethodError, "protected method `#{msg_id}' called for #{desc}"
+ else
+ true
+ end
+ end
+ public :check_insecure_method
+
+ class InvokeMethod # :nodoc:
+ def initialize(drb_server, client)
+ @drb_server = drb_server
+ @safe_level = drb_server.safe_level
+ @client = client
+ end
+
+ def perform
+ @result = nil
+ @succ = false
+ setup_message
+
+ if $SAFE < @safe_level
+ info = Thread.current['DRb']
+ if @block
+ @result = Thread.new {
+ Thread.current['DRb'] = info
+ $SAFE = @safe_level
+ perform_with_block
+ }.value
+ else
+ @result = Thread.new {
+ Thread.current['DRb'] = info
+ $SAFE = @safe_level
+ perform_without_block
+ }.value
+ end
+ else
+ if @block
+ @result = perform_with_block
+ else
+ @result = perform_without_block
+ end
+ end
+ @succ = true
+ if @msg_id == :to_ary && @result.class == Array
+ @result = DRbArray.new(@result)
+ end
+ return @succ, @result
+ rescue StandardError, ScriptError, Interrupt
+ @result = $!
+ return @succ, @result
+ end
+
+ private
+ def init_with_client
+ obj, msg, argv, block = @client.recv_request
+ @obj = obj
+ @msg_id = msg.intern
+ @argv = argv
+ @block = block
+ end
+
+ def check_insecure_method
+ @drb_server.check_insecure_method(@obj, @msg_id)
+ end
+
+ def setup_message
+ init_with_client
+ check_insecure_method
+ end
+
+ def perform_without_block
+ if Proc === @obj && @msg_id == :__drb_yield
+ if @argv.size == 1
+ ary = @argv
+ else
+ ary = [@argv]
+ end
+ ary.collect(&@obj)[0]
+ else
+ @obj.__send__(@msg_id, *@argv)
+ end
+ end
+
+ end
+
+ if RUBY_VERSION >= '1.8'
+ require 'drb/invokemethod'
+ class InvokeMethod
+ include InvokeMethod18Mixin
+ end
+ else
+ require 'drb/invokemethod16'
+ class InvokeMethod
+ include InvokeMethod16Mixin
+ end
+ end
+
+ # The main loop performed by a DRbServer's internal thread.
+ #
+ # Accepts a connection from a client, and starts up its own
+ # thread to handle it. This thread loops, receiving requests
+ # from the client, invoking them on a local object, and
+ # returning responses, until the client closes the connection
+ # or a local method call fails.
+ def main_loop
+ Thread.start(@protocol.accept) do |client|
+ @grp.add Thread.current
+ Thread.current['DRb'] = { 'client' => client ,
+ 'server' => self }
+ loop do
+ begin
+ succ = false
+ invoke_method = InvokeMethod.new(self, client)
+ succ, result = invoke_method.perform
+ if !succ && verbose
+ p result
+ result.backtrace.each do |x|
+ puts x
+ end
+ end
+ client.send_reply(succ, result) rescue nil
+ ensure
+ client.close unless succ
+ if Thread.current['DRb']['stop_service']
+ Thread.new { stop_service }
+ end
+ break unless succ
+ end
+ end
+ end
+ end
+ end
+
+ @primary_server = nil
+
+ # Start a dRuby server locally.
+ #
+ # The new dRuby server will become the primary server, even
+ # if another server is currently the primary server.
+ #
+ # +uri+ is the URI for the server to bind to. If nil,
+ # the server will bind to random port on the default local host
+ # name and use the default dRuby protocol.
+ #
+ # +front+ is the server's front object. This may be nil.
+ #
+ # +config+ is the configuration for the new server. This may
+ # be nil.
+ #
+ # See DRbServer::new.
+ def start_service(uri=nil, front=nil, config=nil)
+ @primary_server = DRbServer.new(uri, front, config)
+ end
+ module_function :start_service
+
+ # The primary local dRuby server.
+ #
+ # This is the server created by the #start_service call.
+ attr_accessor :primary_server
+ module_function :primary_server=, :primary_server
+
+ # Get the 'current' server.
+ #
+ # In the context of execution taking place within the main
+ # thread of a dRuby server (typically, as a result of a remote
+ # call on the server or one of its objects), the current
+ # server is that server. Otherwise, the current server is
+ # the primary server.
+ #
+ # If the above rule fails to find a server, a DRbServerNotFound
+ # error is raised.
+ def current_server
+ drb = Thread.current['DRb']
+ server = (drb && drb['server']) ? drb['server'] : @primary_server
+ raise DRbServerNotFound unless server
+ return server
+ end
+ module_function :current_server
+
+ # Stop the local dRuby server.
+ #
+ # This operates on the primary server. If there is no primary
+ # server currently running, it is a noop.
+ def stop_service
+ @primary_server.stop_service if @primary_server
+ @primary_server = nil
+ end
+ module_function :stop_service
+
+ # Get the URI defining the local dRuby space.
+ #
+ # This is the URI of the current server. See #current_server.
+ def uri
+ drb = Thread.current['DRb']
+ client = (drb && drb['client'])
+ if client
+ uri = client.uri
+ return uri if uri
+ end
+ current_server.uri
+ end
+ module_function :uri
+
+ # Is +uri+ the URI for the current local server?
+ def here?(uri)
+ (current_server.uri rescue nil) == uri
+ end
+ module_function :here?
+
+ # Get the configuration of the current server.
+ #
+ # If there is no current server, this returns the default configuration.
+ # See #current_server and DRbServer::make_config.
+ def config
+ current_server.config
+ rescue
+ DRbServer.make_config
+ end
+ module_function :config
+
+ # Get the front object of the current server.
+ #
+ # This raises a DRbServerNotFound error if there is no current server.
+ # See #current_server.
+ def front
+ current_server.front
+ end
+ module_function :front
+
+ # Convert a reference into an object using the current server.
+ #
+ # This raises a DRbServerNotFound error if there is no current server.
+ # See #current_server.
+ def to_obj(ref)
+ current_server.to_obj(ref)
+ end
+
+ # Get a reference id for an object using the current server.
+ #
+ # This raises a DRbServerNotFound error if there is no current server.
+ # See #current_server.
+ def to_id(obj)
+ current_server.to_id(obj)
+ end
+ module_function :to_id
+ module_function :to_obj
+
+ # Get the thread of the primary server.
+ #
+ # This returns nil if there is no primary server. See #primary_server.
+ def thread
+ @primary_server ? @primary_server.thread : nil
+ end
+ module_function :thread
+
+ # Set the default id conv object.
+ #
+ # See DRbServer#default_id_conv.
+ def install_id_conv(idconv)
+ DRbServer.default_id_conv(idconv)
+ end
+ module_function :install_id_conv
+
+ # Set the default acl.
+ #
+ # See DRb::DRbServer.default_acl.
+ def install_acl(acl)
+ DRbServer.default_acl(acl)
+ end
+ module_function :install_acl
+
+ @mutex = Mutex.new
+ def mutex
+ @mutex
+ end
+ module_function :mutex
+
+ @server = {}
+ def regist_server(server)
+ @server[server.uri] = server
+ mutex.synchronize do
+ @primary_server = server unless @primary_server
+ end
+ end
+ module_function :regist_server
+
+ def remove_server(server)
+ @server.delete(server.uri)
+ end
+ module_function :remove_server
+
+ def fetch_server(uri)
+ @server[uri]
+ end
+ module_function :fetch_server
+end
+
+DRbObject = DRb::DRbObject
+DRbUndumped = DRb::DRbUndumped
+DRbIdConv = DRb::DRbIdConv
diff --git a/ruby/lib/drb/eq.rb b/ruby/lib/drb/eq.rb
new file mode 100644
index 0000000..e24512d
--- /dev/null
+++ b/ruby/lib/drb/eq.rb
@@ -0,0 +1,16 @@
+require 'drb/drb'
+
+module DRb
+ class DRbObject
+ def ==(other)
+ return false unless DRbObject === other
+ (@ref == other.__drbref) && (@uri == other.__drburi)
+ end
+
+ def hash
+ [@uri, @ref].hash
+ end
+
+ alias eql? ==
+ end
+end
diff --git a/ruby/lib/drb/extserv.rb b/ruby/lib/drb/extserv.rb
new file mode 100644
index 0000000..af52250
--- /dev/null
+++ b/ruby/lib/drb/extserv.rb
@@ -0,0 +1,71 @@
+=begin
+ external service
+ Copyright (c) 2000,2002 Masatoshi SEKI
+=end
+
+require 'drb/drb'
+require 'monitor'
+
+module DRb
+ class ExtServ
+ include MonitorMixin
+ include DRbUndumped
+
+ def initialize(there, name, server=nil)
+ super()
+ @server = server || DRb::primary_server
+ @name = name
+ ro = DRbObject.new(nil, there)
+ synchronize do
+ @invoker = ro.regist(name, DRbObject.new(self, @server.uri))
+ end
+ end
+ attr_reader :server
+
+ def front
+ DRbObject.new(nil, @server.uri)
+ end
+
+ def stop_service
+ synchronize do
+ @invoker.unregist(@name)
+ server = @server
+ @server = nil
+ server.stop_service
+ true
+ end
+ end
+
+ def alive?
+ @server ? @server.alive? : false
+ end
+ end
+end
+
+if __FILE__ == $0
+ class Foo
+ include DRbUndumped
+
+ def initialize(str)
+ @str = str
+ end
+
+ def hello(it)
+ "#{it}: #{self}"
+ end
+
+ def to_s
+ @str
+ end
+ end
+
+ cmd = ARGV.shift
+ case cmd
+ when 'itest1', 'itest2'
+ front = Foo.new(cmd)
+ manager = DRb::DRbServer.new(nil, front)
+ es = DRb::ExtServ.new(ARGV.shift, ARGV.shift, manager)
+ es.server.thread.join
+ end
+end
+
diff --git a/ruby/lib/drb/extservm.rb b/ruby/lib/drb/extservm.rb
new file mode 100644
index 0000000..7af644a
--- /dev/null
+++ b/ruby/lib/drb/extservm.rb
@@ -0,0 +1,85 @@
+=begin
+ external service manager
+ Copyright (c) 2000 Masatoshi SEKI
+=end
+
+require 'drb/drb'
+require 'thread'
+require 'monitor'
+
+module DRb
+ class ExtServManager
+ include DRbUndumped
+ include MonitorMixin
+
+ @@command = {}
+
+ def self.command
+ @@command
+ end
+
+ def self.command=(cmd)
+ @@command = cmd
+ end
+
+ def initialize
+ super()
+ @cond = new_cond
+ @servers = {}
+ @waiting = []
+ @queue = Queue.new
+ @thread = invoke_thread
+ @uri = nil
+ end
+ attr_accessor :uri
+
+ def service(name)
+ synchronize do
+ while true
+ server = @servers[name]
+ return server if server && server.alive?
+ invoke_service(name)
+ @cond.wait
+ end
+ end
+ end
+
+ def regist(name, ro)
+ synchronize do
+ @servers[name] = ro
+ @cond.signal
+ end
+ self
+ end
+
+ def unregist(name)
+ synchronize do
+ @servers.delete(name)
+ end
+ end
+
+ private
+ def invoke_thread
+ Thread.new do
+ while true
+ name = @queue.pop
+ invoke_service_command(name, @@command[name])
+ end
+ end
+ end
+
+ def invoke_service(name)
+ @queue.push(name)
+ end
+
+ def invoke_service_command(name, command)
+ raise "invalid command. name: #{name}" unless command
+ synchronize do
+ return if @servers.include?(name)
+ @servers[name] = false
+ end
+ uri = @uri || DRb.uri
+ spawn("#{command} #{uri} #{name}")
+ end
+ end
+end
diff --git a/ruby/lib/drb/gw.rb b/ruby/lib/drb/gw.rb
new file mode 100644
index 0000000..b7a5f53
--- /dev/null
+++ b/ruby/lib/drb/gw.rb
@@ -0,0 +1,122 @@
+require 'drb/drb'
+require 'monitor'
+
+module DRb
+ class GWIdConv < DRbIdConv
+ def to_obj(ref)
+ if Array === ref && ref[0] == :DRbObject
+ return DRbObject.new_with(ref[1], ref[2])
+ end
+ super(ref)
+ end
+ end
+
+ class GW
+ include MonitorMixin
+ def initialize
+ super()
+ @hash = {}
+ end
+
+ def [](key)
+ synchronize do
+ @hash[key]
+ end
+ end
+
+ def []=(key, v)
+ synchronize do
+ @hash[key] = v
+ end
+ end
+ end
+
+ class DRbObject
+ def self._load(s)
+ uri, ref = Marshal.load(s)
+ if DRb.uri == uri
+ return ref ? DRb.to_obj(ref) : DRb.front
+ end
+
+ self.new_with(DRb.uri, [:DRbObject, uri, ref])
+ end
+
+ def _dump(lv)
+ if DRb.uri == @uri
+ if Array === @ref && @ref[0] == :DRbObject
+ Marshal.dump([@ref[1], @ref[2]])
+ else
+ Marshal.dump([@uri, @ref]) # ??
+ end
+ else
+ Marshal.dump([DRb.uri, [:DRbObject, @uri, @ref]])
+ end
+ end
+ end
+end
+
+=begin
+DRb.install_id_conv(DRb::GWIdConv.new)
+
+front = DRb::GW.new
+
+s1 = DRb::DRbServer.new('drbunix:/tmp/gw_b_a', front)
+s2 = DRb::DRbServer.new('drbunix:/tmp/gw_b_c', front)
+
+s1.thread.join
+s2.thread.join
+=end
+
+=begin
+# foo.rb
+
+require 'drb/drb'
+
+class Foo
+ include DRbUndumped
+ def initialize(name, peer=nil)
+ @name = name
+ @peer = peer
+ end
+
+ def ping(obj)
+ puts "#{@name}: ping: #{obj.inspect}"
+ @peer.ping(self) if @peer
+ end
+end
+=end
+
+=begin
+# gw_a.rb
+require 'drb/unix'
+require 'foo'
+
+obj = Foo.new('a')
+DRb.start_service("drbunix:/tmp/gw_a", obj)
+
+robj = DRbObject.new_with_uri('drbunix:/tmp/gw_b_a')
+robj[:a] = obj
+
+DRb.thread.join
+=end
+
+=begin
+# gw_c.rb
+require 'drb/unix'
+require 'foo'
+
+foo = Foo.new('c', nil)
+
+DRb.start_service("drbunix:/tmp/gw_c", nil)
+
+robj = DRbObject.new_with_uri("drbunix:/tmp/gw_b_c")
+
+puts "c->b"
+a = robj[:a]
+sleep 2
+
+a.ping(foo)
+
+DRb.thread.join
+=end
+
diff --git a/ruby/lib/drb/invokemethod.rb b/ruby/lib/drb/invokemethod.rb
new file mode 100644
index 0000000..7da8ace
--- /dev/null
+++ b/ruby/lib/drb/invokemethod.rb
@@ -0,0 +1,34 @@
+# for ruby-1.8.0
+
+module DRb
+ class DRbServer
+ module InvokeMethod18Mixin
+ def block_yield(x)
+ if x.size == 1 && x[0].class == Array
+ x[0] = DRbArray.new(x[0])
+ end
+ block_value = @block.call(*x)
+ end
+
+ def perform_with_block
+ @obj.__send__(@msg_id, *@argv) do |*x|
+ jump_error = nil
+ begin
+ block_value = block_yield(x)
+ rescue LocalJumpError
+ jump_error = $!
+ end
+ if jump_error
+ case jump_error.reason
+ when :break
+ break(jump_error.exit_value)
+ else
+ raise jump_error
+ end
+ end
+ block_value
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/drb/observer.rb b/ruby/lib/drb/observer.rb
new file mode 100644
index 0000000..149426d
--- /dev/null
+++ b/ruby/lib/drb/observer.rb
@@ -0,0 +1,22 @@
+require 'observer'
+
+module DRb
+ module DRbObservable
+ include Observable
+
+ def notify_observers(*arg)
+ if defined? @observer_state and @observer_state
+ if defined? @observer_peers
+ @observer_peers.each do |observer, method|
+ begin
+ observer.send(method, *arg)
+ rescue
+ delete_observer(observer)
+ end
+ end
+ end
+ @observer_state = false
+ end
+ end
+ end
+end
diff --git a/ruby/lib/drb/ssl.rb b/ruby/lib/drb/ssl.rb
new file mode 100644
index 0000000..58d6b7d
--- /dev/null
+++ b/ruby/lib/drb/ssl.rb
@@ -0,0 +1,190 @@
+require 'socket'
+require 'openssl'
+require 'drb/drb'
+require 'singleton'
+
+module DRb
+
+ class DRbSSLSocket < DRbTCPSocket
+
+ class SSLConfig
+
+ DEFAULT = {
+ :SSLCertificate => nil,
+ :SSLPrivateKey => nil,
+ :SSLClientCA => nil,
+ :SSLCACertificatePath => nil,
+ :SSLCACertificateFile => nil,
+ :SSLVerifyMode => ::OpenSSL::SSL::VERIFY_NONE,
+ :SSLVerifyDepth => nil,
+ :SSLVerifyCallback => nil, # custom verification
+ :SSLCertificateStore => nil,
+ # Must specify if you use auto generated certificate.
+ :SSLCertName => nil, # e.g. [["CN","fqdn.example.com"]]
+ :SSLCertComment => "Generated by Ruby/OpenSSL"
+ }
+
+ def initialize(config)
+ @config = config
+ @cert = config[:SSLCertificate]
+ @pkey = config[:SSLPrivateKey]
+ @ssl_ctx = nil
+ end
+
+ def [](key);
+ @config[key] || DEFAULT[key]
+ end
+
+ def connect(tcp)
+ ssl = ::OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
+ ssl.sync = true
+ ssl.connect
+ ssl
+ end
+
+ def accept(tcp)
+ ssl = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
+ ssl.sync = true
+ ssl.accept
+ ssl
+ end
+
+ def setup_certificate
+ if @cert && @pkey
+ return
+ end
+
+ rsa = OpenSSL::PKey::RSA.new(512){|p, n|
+ next unless self[:verbose]
+ case p
+ when 0; $stderr.putc "." # BN_generate_prime
+ when 1; $stderr.putc "+" # BN_generate_prime
+ when 2; $stderr.putc "*" # searching good prime,
+ # n = #of try,
+ # but also data from BN_generate_prime
+ when 3; $stderr.putc "\n" # found good prime, n==0 - p, n==1 - q,
+ # but also data from BN_generate_prime
+ else; $stderr.putc "*" # BN_generate_prime
+ end
+ }
+
+ cert = OpenSSL::X509::Certificate.new
+ cert.version = 3
+ cert.serial = 0
+ name = OpenSSL::X509::Name.new(self[:SSLCertName])
+ cert.subject = name
+ cert.issuer = name
+ cert.not_before = Time.now
+ cert.not_after = Time.now + (365*24*60*60)
+ cert.public_key = rsa.public_key
+
+ ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
+ cert.extensions = [
+ ef.create_extension("basicConstraints","CA:FALSE"),
+ ef.create_extension("subjectKeyIdentifier", "hash") ]
+ ef.issuer_certificate = cert
+ cert.add_extension(ef.create_extension("authorityKeyIdentifier",
+ "keyid:always,issuer:always"))
+ if comment = self[:SSLCertComment]
+ cert.add_extension(ef.create_extension("nsComment", comment))
+ end
+ cert.sign(rsa, OpenSSL::Digest::SHA1.new)
+
+ @cert = cert
+ @pkey = rsa
+ end
+
+ def setup_ssl_context
+ ctx = ::OpenSSL::SSL::SSLContext.new
+ ctx.cert = @cert
+ ctx.key = @pkey
+ ctx.client_ca = self[:SSLClientCA]
+ ctx.ca_path = self[:SSLCACertificatePath]
+ ctx.ca_file = self[:SSLCACertificateFile]
+ ctx.verify_mode = self[:SSLVerifyMode]
+ ctx.verify_depth = self[:SSLVerifyDepth]
+ ctx.verify_callback = self[:SSLVerifyCallback]
+ ctx.cert_store = self[:SSLCertificateStore]
+ @ssl_ctx = ctx
+ end
+ end
+
+ def self.parse_uri(uri)
+ if uri =~ /^drbssl:\/\/(.*?):(\d+)(\?(.*))?$/
+ host = $1
+ port = $2.to_i
+ option = $4
+ [host, port, option]
+ else
+ raise(DRbBadScheme, uri) unless uri =~ /^drbssl:/
+ raise(DRbBadURI, 'can\'t parse uri:' + uri)
+ end
+ end
+
+ def self.open(uri, config)
+ host, port, option = parse_uri(uri)
+ host.untaint
+ port.untaint
+ soc = TCPSocket.open(host, port)
+ ssl_conf = SSLConfig::new(config)
+ ssl_conf.setup_ssl_context
+ ssl = ssl_conf.connect(soc)
+ self.new(uri, ssl, ssl_conf, true)
+ end
+
+ def self.open_server(uri, config)
+ uri = 'drbssl://:0' unless uri
+ host, port, opt = parse_uri(uri)
+ if host.size == 0
+ host = getservername
+ soc = open_server_inaddr_any(host, port)
+ else
+ soc = TCPServer.open(host, port)
+ end
+ port = soc.addr[1] if port == 0
+ @uri = "drbssl://#{host}:#{port}"
+
+ ssl_conf = SSLConfig.new(config)
+ ssl_conf.setup_certificate
+ ssl_conf.setup_ssl_context
+ self.new(@uri, soc, ssl_conf, false)
+ end
+
+ def self.uri_option(uri, config)
+ host, port, option = parse_uri(uri)
+ return "drbssl://#{host}:#{port}", option
+ end
+
+ def initialize(uri, soc, config, is_established)
+ @ssl = is_established ? soc : nil
+ super(uri, soc.to_io, config)
+ end
+
+ def stream; @ssl; end
+
+ def close
+ if @ssl
+ @ssl.close
+ @ssl = nil
+ end
+ super
+ end
+
+ def accept
+ begin
+ while true
+ soc = @socket.accept
+ break if (@acl ? @acl.allow_socket?(soc) : true)
+ soc.close
+ end
+ ssl = @config.accept(soc)
+ self.class.new(uri, ssl, @config, true)
+ rescue OpenSSL::SSL::SSLError
+ warn("#{__FILE__}:#{__LINE__}: warning: #{$!.message} (#{$!.class})") if @config[:verbose]
+ retry
+ end
+ end
+ end
+
+ DRbProtocol.add_protocol(DRbSSLSocket)
+end
diff --git a/ruby/lib/drb/timeridconv.rb b/ruby/lib/drb/timeridconv.rb
new file mode 100644
index 0000000..bb2c48d
--- /dev/null
+++ b/ruby/lib/drb/timeridconv.rb
@@ -0,0 +1,91 @@
+require 'drb/drb'
+require 'monitor'
+
+module DRb
+ class TimerIdConv < DRbIdConv
+ class TimerHolder2
+ include MonitorMixin
+
+ class InvalidIndexError < RuntimeError; end
+
+ def initialize(timeout=600)
+ super()
+ @sentinel = Object.new
+ @gc = {}
+ @curr = {}
+ @renew = {}
+ @timeout = timeout
+ @keeper = keeper
+ end
+
+ def add(obj)
+ synchronize do
+ key = obj.__id__
+ @curr[key] = obj
+ return key
+ end
+ end
+
+ def fetch(key, dv=@sentinel)
+ synchronize do
+ obj = peek(key)
+ if obj == @sentinel
+ return dv unless dv == @sentinel
+ raise InvalidIndexError
+ end
+ @renew[key] = obj # KeepIt
+ return obj
+ end
+ end
+
+ def include?(key)
+ synchronize do
+ obj = peek(key)
+ return false if obj == @sentinel
+ true
+ end
+ end
+
+ def peek(key)
+ synchronize do
+ return @curr.fetch(key, @renew.fetch(key, @gc.fetch(key, @sentinel)))
+ end
+ end
+
+ private
+ def alternate
+ synchronize do
+ @gc = @curr # GCed
+ @curr = @renew
+ @renew = {}
+ end
+ end
+
+ def keeper
+ Thread.new do
+ loop do
+ size = alternate
+ sleep(@timeout)
+ end
+ end
+ end
+ end
+
+ def initialize(timeout=600)
+ @holder = TimerHolder2.new(timeout)
+ end
+
+ def to_obj(ref)
+ return super if ref.nil?
+ @holder.fetch(ref)
+ rescue TimerHolder2::InvalidIndexError
+ raise "invalid reference"
+ end
+
+ def to_id(obj)
+ return @holder.add(obj)
+ end
+ end
+end
+
+# DRb.install_id_conv(TimerIdConv.new)
diff --git a/ruby/lib/drb/unix.rb b/ruby/lib/drb/unix.rb
new file mode 100644
index 0000000..57feed8
--- /dev/null
+++ b/ruby/lib/drb/unix.rb
@@ -0,0 +1,108 @@
+require 'socket'
+require 'drb/drb'
+require 'tmpdir'
+
+raise(LoadError, "UNIXServer is required") unless defined?(UNIXServer)
+
+module DRb
+
+ class DRbUNIXSocket < DRbTCPSocket
+ def self.parse_uri(uri)
+ if /^drbunix:(.*?)(\?(.*))?$/ =~ uri
+ filename = $1
+ option = $3
+ [filename, option]
+ else
+ raise(DRbBadScheme, uri) unless uri =~ /^drbunix:/
+ raise(DRbBadURI, 'can\'t parse uri:' + uri)
+ end
+ end
+
+ def self.open(uri, config)
+ filename, option = parse_uri(uri)
+ filename.untaint
+ soc = UNIXSocket.open(filename)
+ self.new(uri, soc, config)
+ end
+
+ def self.open_server(uri, config)
+ filename, option = parse_uri(uri)
+ if filename.size == 0
+ soc = temp_server
+ filename = soc.path
+ uri = 'drbunix:' + soc.path
+ else
+ soc = UNIXServer.open(filename)
+ end
+ owner = config[:UNIXFileOwner]
+ group = config[:UNIXFileGroup]
+ if owner || group
+ require 'etc'
+ owner = Etc.getpwnam( owner ).uid if owner
+ group = Etc.getgrnam( group ).gid if group
+ File.chown owner, group, filename
+ end
+ mode = config[:UNIXFileMode]
+ File.chmod(mode, filename) if mode
+
+ self.new(uri, soc, config, true)
+ end
+
+ def self.uri_option(uri, config)
+ filename, option = parse_uri(uri)
+ return "drbunix:#{filename}", option
+ end
+
+ def initialize(uri, soc, config={}, server_mode = false)
+ super(uri, soc, config)
+ set_sockopt(@socket)
+ @server_mode = server_mode
+ @acl = nil
+ end
+
+ # import from tempfile.rb
+ Max_try = 10
+ private
+ def self.temp_server
+ tmpdir = Dir::tmpdir
+ n = 0
+ while true
+ begin
+ tmpname = sprintf('%s/druby%d.%d', tmpdir, $$, n)
+ lock = tmpname + '.lock'
+ unless File.exist?(tmpname) or File.exist?(lock)
+ Dir.mkdir(lock)
+ break
+ end
+ rescue
+ raise "cannot generate tempfile `%s'" % tmpname if n >= Max_try
+ #sleep(1)
+ end
+ n += 1
+ end
+ soc = UNIXServer.new(tmpname)
+ Dir.rmdir(lock)
+ soc
+ end
+
+ public
+ def close
+ return unless @socket
+ path = @socket.path if @server_mode
+ @socket.close
+ File.unlink(path) if @server_mode
+ @socket = nil
+ end
+
+ def accept
+ s = @socket.accept
+ self.class.new(nil, s, @config)
+ end
+
+ def set_sockopt(soc)
+ soc.fcntl(Fcntl::F_SETFL, Fcntl::FD_CLOEXEC) if defined? Fcntl::FD_CLOEXEC
+ end
+ end
+
+ DRbProtocol.add_protocol(DRbUNIXSocket)
+end
diff --git a/ruby/lib/e2mmap.rb b/ruby/lib/e2mmap.rb
new file mode 100644
index 0000000..b8d1d44
--- /dev/null
+++ b/ruby/lib/e2mmap.rb
@@ -0,0 +1,172 @@
+#
+# e2mmap.rb - for ruby 1.1
+# $Release Version: 2.0$
+# $Revision: 1.10 $
+# by Keiju ISHITSUKA
+#
+# --
+# Usage:
+#
+# U1)
+# class Foo
+# extend Exception2MessageMapper
+# def_e2message ExistingExceptionClass, "message..."
+# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+#
+# U2)
+# module Error
+# extend Exception2MessageMapper
+# def_e2meggage ExistingExceptionClass, "message..."
+# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+# class Foo
+# include Error
+# ...
+# end
+#
+# foo = Foo.new
+# foo.Fail ....
+#
+# U3)
+# module Error
+# extend Exception2MessageMapper
+# def_e2message ExistingExceptionClass, "message..."
+# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+# class Foo
+# extend Exception2MessageMapper
+# include Error
+# ...
+# end
+#
+# Foo.Fail NewExceptionClass, arg...
+# Foo.Fail ExistingExceptionClass, arg...
+#
+#
+module Exception2MessageMapper
+ @RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
+
+ E2MM = Exception2MessageMapper
+
+ def E2MM.extend_object(cl)
+ super
+ cl.bind(self) unless cl < E2MM
+ end
+
+ def bind(cl)
+ self.module_eval %[
+ def Raise(err = nil, *rest)
+ Exception2MessageMapper.Raise(self.class, err, *rest)
+ end
+ alias Fail Raise
+
+ def self.included(mod)
+ mod.extend Exception2MessageMapper
+ end
+ ]
+ end
+
+ # Fail(err, *rest)
+ # err: exception
+ # rest: message arguments
+ #
+ def Raise(err = nil, *rest)
+ E2MM.Raise(self, err, *rest)
+ end
+ alias Fail Raise
+ alias fail Raise
+
+ # def_e2message(c, m)
+ # c: exception
+ # m: message_form
+ # define exception c with message m.
+ #
+ def def_e2message(c, m)
+ E2MM.def_e2message(self, c, m)
+ end
+
+ # def_exception(n, m, s)
+ # n: exception_name
+ # m: message_form
+ # s: superclass(default: StandardError)
+ # define exception named ``c'' with message m.
+ #
+ def def_exception(n, m, s = StandardError)
+ E2MM.def_exception(self, n, m, s)
+ end
+
+ #
+ # Private definitions.
+ #
+ # {[class, exp] => message, ...}
+ @MessageMap = {}
+
+ # E2MM.def_e2message(k, e, m)
+ # k: class to define exception under.
+ # e: exception
+ # m: message_form
+ # define exception c with message m.
+ #
+ def E2MM.def_e2message(k, c, m)
+ E2MM.instance_eval{@MessageMap[[k, c]] = m}
+ c
+ end
+
+ # E2MM.def_exception(k, n, m, s)
+ # k: class to define exception under.
+ # n: exception_name
+ # m: message_form
+ # s: superclass(default: StandardError)
+ # define exception named ``c'' with message m.
+ #
+ def E2MM.def_exception(k, n, m, s = StandardError)
+ n = n.id2name if n.kind_of?(Fixnum)
+ e = Class.new(s)
+ E2MM.instance_eval{@MessageMap[[k, e]] = m}
+ k.const_set(n, e)
+ end
+
+ # Fail(klass, err, *rest)
+ # klass: class to define exception under.
+ # err: exception
+ # rest: message arguments
+ #
+ def E2MM.Raise(klass = E2MM, err = nil, *rest)
+ if form = e2mm_message(klass, err)
+ b = $@.nil? ? caller(1) : $@
+ #p $@
+ #p __FILE__
+ b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/
+ raise err, sprintf(form, *rest), b
+ else
+ E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
+ end
+ end
+ class <<E2MM
+ alias Fail Raise
+ end
+
+ def E2MM.e2mm_message(klass, exp)
+ for c in klass.ancestors
+ if mes = @MessageMap[[c,exp]]
+ #p mes
+ m = klass.instance_eval('"' + mes + '"')
+ return m
+ end
+ end
+ nil
+ end
+ class <<self
+ alias message e2mm_message
+ end
+
+ E2MM.def_exception(E2MM,
+ :ErrNotRegisteredException,
+ "not registerd exception(%s)")
+end
+
+
diff --git a/ruby/lib/erb.rb b/ruby/lib/erb.rb
new file mode 100644
index 0000000..76b2f2c
--- /dev/null
+++ b/ruby/lib/erb.rb
@@ -0,0 +1,902 @@
+# = ERB -- Ruby Templating
+#
+# Author:: Masatoshi SEKI
+# Documentation:: James Edward Gray II and Gavin Sinclair
+#
+# See ERB for primary documentation and ERB::Util for a couple of utility
+# routines.
+#
+# Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
+#
+# You can redistribute it and/or modify it under the same terms as Ruby.
+
+=begin rdoc
+= ERB -- Ruby Templating
+
+== Introduction
+
+ERB provides an easy to use but powerful templating system for Ruby. Using
+ERB, actual Ruby code can be added to any plain text document for the
+purposes of generating document information details and/or flow control.
+
+A very simple example is this:
+
+ require 'erb'
+
+ x = 42
+ template = ERB.new <<-EOF
+ The value of x is: <%= x %>
+ EOF
+ puts template.result(binding)
+
+<em>Prints:</em> The value of x is: 42
+
+More complex examples are given below.
+
+
+== Recognized Tags
+
+ERB recognizes certain tags in the provided template and converts them based
+on the rules below:
+
+ <% Ruby code -- inline with output %>
+ <%= Ruby expression -- replace with result %>
+ <%# comment -- ignored -- useful in testing %>
+ % a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
+ %% replaced with % if first thing on a line and % processing is used
+ <%% or %%> -- replace with <% or %> respectively
+
+All other text is passed through ERB filtering unchanged.
+
+
+== Options
+
+There are several settings you can change when you use ERB:
+* the nature of the tags that are recognized;
+* the value of <tt>$SAFE</tt> under which the template is run;
+* the binding used to resolve local variables in the template.
+
+See the ERB.new and ERB#result methods for more detail.
+
+== Character encodings
+
+ERB (or ruby code generated by ERB) returns a string in the same
+character encoding as the input string. When the input string has
+a magic comment, however, it returns a string in the encoding specified
+by the magic comment.
+
+ # -*- coding: UTF-8 -*-
+ require 'erb'
+
+ template = ERB.new <<EOF
+ <%#-*- coding: Big5 -*-%>
+ \_\_ENCODING\_\_ is <%= \_\_ENCODING\_\_ %>.
+ EOF
+ puts template.result
+
+<em>Prints:</em> \_\_ENCODING\_\_ is Big5.
+
+
+== Examples
+
+=== Plain Text
+
+ERB is useful for any generic templating situation. Note that in this example, we use the
+convenient "% at start of line" tag, and we quote the template literally with
+<tt>%q{...}</tt> to avoid trouble with the backslash.
+
+ require "erb"
+
+ # Create template.
+ template = %q{
+ From: James Edward Gray II <james@grayproductions.net>
+ To: <%= to %>
+ Subject: Addressing Needs
+
+ <%= to[/\w+/] %>:
+
+ Just wanted to send a quick note assuring that your needs are being
+ addressed.
+
+ I want you to know that my team will keep working on the issues,
+ especially:
+
+ <%# ignore numerous minor requests -- focus on priorities %>
+ % priorities.each do |priority|
+ * <%= priority %>
+ % end
+
+ Thanks for your patience.
+
+ James Edward Gray II
+ }.gsub(/^ /, '')
+
+ message = ERB.new(template, 0, "%<>")
+
+ # Set up template data.
+ to = "Community Spokesman <spokesman@ruby_community.org>"
+ priorities = [ "Run Ruby Quiz",
+ "Document Modules",
+ "Answer Questions on Ruby Talk" ]
+
+ # Produce result.
+ email = message.result
+ puts email
+
+<i>Generates:</i>
+
+ From: James Edward Gray II <james@grayproductions.net>
+ To: Community Spokesman <spokesman@ruby_community.org>
+ Subject: Addressing Needs
+
+ Community:
+
+ Just wanted to send a quick note assuring that your needs are being addressed.
+
+ I want you to know that my team will keep working on the issues, especially:
+
+ * Run Ruby Quiz
+ * Document Modules
+ * Answer Questions on Ruby Talk
+
+ Thanks for your patience.
+
+ James Edward Gray II
+
+=== Ruby in HTML
+
+ERB is often used in <tt>.rhtml</tt> files (HTML with embedded Ruby). Notice the need in
+this example to provide a special binding when the template is run, so that the instance
+variables in the Product object can be resolved.
+
+ require "erb"
+
+ # Build template data class.
+ class Product
+ def initialize( code, name, desc, cost )
+ @code = code
+ @name = name
+ @desc = desc
+ @cost = cost
+
+ @features = [ ]
+ end
+
+ def add_feature( feature )
+ @features << feature
+ end
+
+ # Support templating of member data.
+ def get_binding
+ binding
+ end
+
+ # ...
+ end
+
+ # Create template.
+ template = %{
+ <html>
+ <head><title>Ruby Toys -- <%= @name %></title></head>
+ <body>
+
+ <h1><%= @name %> (<%= @code %>)</h1>
+ <p><%= @desc %></p>
+
+ <ul>
+ <% @features.each do |f| %>
+ <li><b><%= f %></b></li>
+ <% end %>
+ </ul>
+
+ <p>
+ <% if @cost < 10 %>
+ <b>Only <%= @cost %>!!!</b>
+ <% else %>
+ Call for a price, today!
+ <% end %>
+ </p>
+
+ </body>
+ </html>
+ }.gsub(/^ /, '')
+
+ rhtml = ERB.new(template)
+
+ # Set up template data.
+ toy = Product.new( "TZ-1002",
+ "Rubysapien",
+ "Geek's Best Friend! Responds to Ruby commands...",
+ 999.95 )
+ toy.add_feature("Listens for verbal commands in the Ruby language!")
+ toy.add_feature("Ignores Perl, Java, and all C variants.")
+ toy.add_feature("Karate-Chop Action!!!")
+ toy.add_feature("Matz signature on left leg.")
+ toy.add_feature("Gem studded eyes... Rubies, of course!")
+
+ # Produce result.
+ rhtml.run(toy.get_binding)
+
+<i>Generates (some blank lines removed):</i>
+
+ <html>
+ <head><title>Ruby Toys -- Rubysapien</title></head>
+ <body>
+
+ <h1>Rubysapien (TZ-1002)</h1>
+ <p>Geek's Best Friend! Responds to Ruby commands...</p>
+
+ <ul>
+ <li><b>Listens for verbal commands in the Ruby language!</b></li>
+ <li><b>Ignores Perl, Java, and all C variants.</b></li>
+ <li><b>Karate-Chop Action!!!</b></li>
+ <li><b>Matz signature on left leg.</b></li>
+ <li><b>Gem studded eyes... Rubies, of course!</b></li>
+ </ul>
+
+ <p>
+ Call for a price, today!
+ </p>
+
+ </body>
+ </html>
+
+
+== Notes
+
+There are a variety of templating solutions available in various Ruby projects:
+* ERB's big brother, eRuby, works the same but is written in C for speed;
+* Amrita (smart at producing HTML/XML);
+* cs/Template (written in C for speed);
+* RDoc, distributed with Ruby, uses its own template engine, which can be reused elsewhere;
+* and others; search the RAA.
+
+Rails, the web application framework, uses ERB to create views.
+=end
+class ERB
+ Revision = '$Date:: 2009-01-17 21:20:08 +0900#$' #'
+
+ # Returns revision information for the erb.rb module.
+ def self.version
+ "erb.rb [2.1.0 #{ERB::Revision.split[1]}]"
+ end
+end
+
+#--
+# ERB::Compiler
+class ERB
+ class Compiler # :nodoc:
+ class PercentLine # :nodoc:
+ def initialize(str)
+ @value = str
+ end
+ attr_reader :value
+ alias :to_s :value
+
+ def empty?
+ @value.empty?
+ end
+ end
+
+ class Scanner # :nodoc:
+ @scanner_map = {}
+ def self.regist_scanner(klass, trim_mode, percent)
+ @scanner_map[[trim_mode, percent]] = klass
+ end
+
+ def self.default_scanner=(klass)
+ @default_scanner = klass
+ end
+
+ def self.make_scanner(src, trim_mode, percent)
+ klass = @scanner_map.fetch([trim_mode, percent], @default_scanner)
+ klass.new(src, trim_mode, percent)
+ end
+
+ def initialize(src, trim_mode, percent)
+ @src = src
+ @stag = nil
+ end
+ attr_accessor :stag
+
+ def scan; end
+ end
+
+ class TrimScanner < Scanner # :nodoc:
+ def initialize(src, trim_mode, percent)
+ super
+ @trim_mode = trim_mode
+ @percent = percent
+ if @trim_mode == '>'
+ @scan_line = self.method(:trim_line1)
+ elsif @trim_mode == '<>'
+ @scan_line = self.method(:trim_line2)
+ elsif @trim_mode == '-'
+ @scan_line = self.method(:explicit_trim_line)
+ else
+ @scan_line = self.method(:scan_line)
+ end
+ end
+ attr_accessor :stag
+
+ def scan(&block)
+ @stag = nil
+ if @percent
+ @src.each_line do |line|
+ percent_line(line, &block)
+ end
+ else
+ @scan_line.call(@src, &block)
+ end
+ nil
+ end
+
+ def percent_line(line, &block)
+ if @stag || line[0] != ?%
+ return @scan_line.call(line, &block)
+ end
+
+ line[0] = ''
+ if line[0] == ?%
+ @scan_line.call(line, &block)
+ else
+ yield(PercentLine.new(line.chomp))
+ end
+ end
+
+ def scan_line(line)
+ line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
+ tokens.each do |token|
+ next if token.empty?
+ yield(token)
+ end
+ end
+ end
+
+ def trim_line1(line)
+ line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
+ tokens.each do |token|
+ next if token.empty?
+ if token == "%>\n"
+ yield('%>')
+ yield(:cr)
+ else
+ yield(token)
+ end
+ end
+ end
+ end
+
+ def trim_line2(line)
+ head = nil
+ line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
+ tokens.each do |token|
+ next if token.empty?
+ head = token unless head
+ if token == "%>\n"
+ yield('%>')
+ if is_erb_stag?(head)
+ yield(:cr)
+ else
+ yield("\n")
+ end
+ head = nil
+ else
+ yield(token)
+ head = nil if token == "\n"
+ end
+ end
+ end
+ end
+
+ def explicit_trim_line(line)
+ line.scan(/(.*?)(^[ \t]*<%\-|<%\-|<%%|%%>|<%=|<%#|<%|-%>\n|-%>|%>|\z)/m) do |tokens|
+ tokens.each do |token|
+ next if token.empty?
+ if @stag.nil? && /[ \t]*<%-/ =~ token
+ yield('<%')
+ elsif @stag && token == "-%>\n"
+ yield('%>')
+ yield(:cr)
+ elsif @stag && token == '-%>'
+ yield('%>')
+ else
+ yield(token)
+ end
+ end
+ end
+ end
+
+ ERB_STAG = %w(<%= <%# <%)
+ def is_erb_stag?(s)
+ ERB_STAG.member?(s)
+ end
+ end
+
+ Scanner.default_scanner = TrimScanner
+
+ class SimpleScanner < Scanner # :nodoc:
+ def scan
+ @src.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
+ tokens.each do |token|
+ next if token.empty?
+ yield(token)
+ end
+ end
+ end
+ end
+
+ Scanner.regist_scanner(SimpleScanner, nil, false)
+
+ begin
+ require 'strscan'
+ class SimpleScanner2 < Scanner # :nodoc:
+ def scan
+ stag_reg = /(.*?)(<%%|<%=|<%#|<%|\z)/m
+ etag_reg = /(.*?)(%%>|%>|\z)/m
+ scanner = StringScanner.new(@src)
+ while ! scanner.eos?
+ scanner.scan(@stag ? etag_reg : stag_reg)
+ yield(scanner[1])
+ yield(scanner[2])
+ end
+ end
+ end
+ Scanner.regist_scanner(SimpleScanner2, nil, false)
+
+ class ExplicitScanner < Scanner # :nodoc:
+ def scan
+ stag_reg = /(.*?)(^[ \t]*<%-|<%%|<%=|<%#|<%-|<%|\z)/m
+ etag_reg = /(.*?)(%%>|-%>|%>|\z)/m
+ scanner = StringScanner.new(@src)
+ while ! scanner.eos?
+ scanner.scan(@stag ? etag_reg : stag_reg)
+ yield(scanner[1])
+
+ elem = scanner[2]
+ if /[ \t]*<%-/ =~ elem
+ yield('<%')
+ elsif elem == '-%>'
+ yield('%>')
+ yield(:cr) if scanner.scan(/(\n|\z)/)
+ else
+ yield(elem)
+ end
+ end
+ end
+ end
+ Scanner.regist_scanner(ExplicitScanner, '-', false)
+
+ rescue LoadError
+ end
+
+ class Buffer # :nodoc:
+ def initialize(compiler, enc=nil)
+ @compiler = compiler
+ @line = []
+ @script = enc ? "#coding:#{enc.to_s}\n" : ""
+ @compiler.pre_cmd.each do |x|
+ push(x)
+ end
+ end
+ attr_reader :script
+
+ def push(cmd)
+ @line << cmd
+ end
+
+ def cr
+ @script << (@line.join('; '))
+ @line = []
+ @script << "\n"
+ end
+
+ def close
+ return unless @line
+ @compiler.post_cmd.each do |x|
+ push(x)
+ end
+ @script << (@line.join('; '))
+ @line = nil
+ end
+ end
+
+ def content_dump(s)
+ n = s.count("\n")
+ if n > 0
+ s.dump + "\n" * n
+ else
+ s.dump
+ end
+ end
+
+ def compile(s)
+ enc = s.encoding
+ raise ArgumentError, "#{enc} is not ASCII compatible" if enc.dummy?
+ s = s.dup.force_encoding("ASCII-8BIT") # don't use constant Enoding::ASCII_8BIT for miniruby
+ enc = detect_magic_comment(s) || enc
+ out = Buffer.new(self, enc)
+
+ content = ''
+ scanner = make_scanner(s)
+ scanner.scan do |token|
+ next if token.nil?
+ next if token == ''
+ if scanner.stag.nil?
+ case token
+ when PercentLine
+ out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
+ content = ''
+ out.push(token.to_s)
+ out.cr
+ when :cr
+ out.cr
+ when '<%', '<%=', '<%#'
+ scanner.stag = token
+ out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
+ content = ''
+ when "\n"
+ content << "\n"
+ out.push("#{@put_cmd} #{content_dump(content)}")
+ content = ''
+ when '<%%'
+ content << '<%'
+ else
+ content << token
+ end
+ else
+ case token
+ when '%>'
+ case scanner.stag
+ when '<%'
+ if content[-1] == ?\n
+ content.chop!
+ out.push(content)
+ out.cr
+ else
+ out.push(content)
+ end
+ when '<%='
+ out.push("#{@insert_cmd}((#{content}).to_s)")
+ when '<%#'
+ # out.push("# #{content_dump(content)}")
+ end
+ scanner.stag = nil
+ content = ''
+ when '%%>'
+ content << '%>'
+ else
+ content << token
+ end
+ end
+ end
+ out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
+ out.close
+ return out.script, enc
+ end
+
+ def prepare_trim_mode(mode)
+ case mode
+ when 1
+ return [false, '>']
+ when 2
+ return [false, '<>']
+ when 0
+ return [false, nil]
+ when String
+ perc = mode.include?('%')
+ if mode.include?('-')
+ return [perc, '-']
+ elsif mode.include?('<>')
+ return [perc, '<>']
+ elsif mode.include?('>')
+ return [perc, '>']
+ else
+ [perc, nil]
+ end
+ else
+ return [false, nil]
+ end
+ end
+
+ def make_scanner(src)
+ Scanner.make_scanner(src, @trim_mode, @percent)
+ end
+
+ def initialize(trim_mode)
+ @percent, @trim_mode = prepare_trim_mode(trim_mode)
+ @put_cmd = 'print'
+ @insert_cmd = @put_cmd
+ @pre_cmd = []
+ @post_cmd = []
+ end
+ attr_reader :percent, :trim_mode
+ attr_accessor :put_cmd, :insert_cmd, :pre_cmd, :post_cmd
+
+ private
+ def detect_magic_comment(s)
+ if /\A<%#(.*)%>/ =~ s or (@percent and /\A%#(.*)/ =~ s)
+ comment = $1
+ comment = $1 if comment[/-\*-\s*(.*?)\s*-*-$/]
+ if %r"coding\s*[=:]\s*([[:alnum:]\-_]+)" =~ comment
+ enc = $1.sub(/-(?:mac|dos|unix)/i, '')
+ enc = Encoding.find(enc)
+ end
+ end
+ end
+ end
+end
+
+#--
+# ERB
+class ERB
+ #
+ # Constructs a new ERB object with the template specified in _str_.
+ #
+ # An ERB object works by building a chunk of Ruby code that will output
+ # the completed template when run. If _safe_level_ is set to a non-nil value,
+ # ERB code will be run in a separate thread with <b>$SAFE</b> set to the
+ # provided level.
+ #
+ # If _trim_mode_ is passed a String containing one or more of the following
+ # modifiers, ERB will adjust its code generation as listed:
+ #
+ # % enables Ruby code processing for lines beginning with %
+ # <> omit newline for lines starting with <% and ending in %>
+ # > omit newline for lines ending in %>
+ #
+ # _eoutvar_ can be used to set the name of the variable ERB will build up
+ # its output in. This is useful when you need to run multiple ERB
+ # templates through the same binding and/or when you want to control where
+ # output ends up. Pass the name of the variable to be used inside a String.
+ #
+ # === Example
+ #
+ # require "erb"
+ #
+ # # build data class
+ # class Listings
+ # PRODUCT = { :name => "Chicken Fried Steak",
+ # :desc => "A well messages pattie, breaded and fried.",
+ # :cost => 9.95 }
+ #
+ # attr_reader :product, :price
+ #
+ # def initialize( product = "", price = "" )
+ # @product = product
+ # @price = price
+ # end
+ #
+ # def build
+ # b = binding
+ # # create and run templates, filling member data variables
+ # ERB.new(<<-'END_PRODUCT'.gsub(/^\s+/, ""), 0, "", "@product").result b
+ # <%= PRODUCT[:name] %>
+ # <%= PRODUCT[:desc] %>
+ # END_PRODUCT
+ # ERB.new(<<-'END_PRICE'.gsub(/^\s+/, ""), 0, "", "@price").result b
+ # <%= PRODUCT[:name] %> -- <%= PRODUCT[:cost] %>
+ # <%= PRODUCT[:desc] %>
+ # END_PRICE
+ # end
+ # end
+ #
+ # # setup template data
+ # listings = Listings.new
+ # listings.build
+ #
+ # puts listings.product + "\n" + listings.price
+ #
+ # _Generates_
+ #
+ # Chicken Fried Steak
+ # A well messages pattie, breaded and fried.
+ #
+ # Chicken Fried Steak -- 9.95
+ # A well messages pattie, breaded and fried.
+ #
+ def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
+ @safe_level = safe_level
+ compiler = ERB::Compiler.new(trim_mode)
+ set_eoutvar(compiler, eoutvar)
+ @src, @enc = *compiler.compile(str)
+ @filename = nil
+ end
+
+ # The Ruby code generated by ERB
+ attr_reader :src
+
+ # The optional _filename_ argument passed to Kernel#eval when the ERB code
+ # is run
+ attr_accessor :filename
+
+ #
+ # Can be used to set _eoutvar_ as described in ERB#new. It's probably easier
+ # to just use the constructor though, since calling this method requires the
+ # setup of an ERB _compiler_ object.
+ #
+ def set_eoutvar(compiler, eoutvar = '_erbout')
+ compiler.put_cmd = "#{eoutvar}.concat"
+ compiler.insert_cmd = "#{eoutvar}.concat"
+
+ cmd = []
+ cmd.push "#{eoutvar} = ''"
+
+ compiler.pre_cmd = cmd
+
+ cmd = []
+ cmd.push("#{eoutvar}.force_encoding(__ENCODING__)")
+
+ compiler.post_cmd = cmd
+ end
+
+ # Generate results and print them. (see ERB#result)
+ def run(b=TOPLEVEL_BINDING)
+ print self.result(b)
+ end
+
+ #
+ # Executes the generated ERB code to produce a completed template, returning
+ # the results of that code. (See ERB#new for details on how this process can
+ # be affected by _safe_level_.)
+ #
+ # _b_ accepts a Binding or Proc object which is used to set the context of
+ # code evaluation.
+ #
+ def result(b=TOPLEVEL_BINDING)
+ if @safe_level
+ proc {
+ $SAFE = @safe_level
+ eval(@src, b, (@filename || '(erb)'), 0)
+ }.call
+ else
+ eval(@src, b, (@filename || '(erb)'), 0)
+ end
+ end
+
+ # Define _methodname_ as instance method of _mod_ from compiled ruby source.
+ #
+ # example:
+ # filename = 'example.rhtml' # 'arg1' and 'arg2' are used in example.rhtml
+ # erb = ERB.new(File.read(filename))
+ # erb.def_method(MyClass, 'render(arg1, arg2)', filename)
+ # print MyClass.new.render('foo', 123)
+ def def_method(mod, methodname, fname='(ERB)')
+ src = self.src
+ magic_comment = "#coding:#{@enc}\n"
+ mod.module_eval do
+ eval(magic_comment + "def #{methodname}\n" + src + "\nend\n", binding, fname, -2)
+ end
+ end
+
+ # Create unnamed module, define _methodname_ as instance method of it, and return it.
+ #
+ # example:
+ # filename = 'example.rhtml' # 'arg1' and 'arg2' are used in example.rhtml
+ # erb = ERB.new(File.read(filename))
+ # erb.filename = filename
+ # MyModule = erb.def_module('render(arg1, arg2)')
+ # class MyClass
+ # include MyModule
+ # end
+ def def_module(methodname='erb')
+ mod = Module.new
+ def_method(mod, methodname, @filename || '(ERB)')
+ mod
+ end
+
+ # Define unnamed class which has _methodname_ as instance method, and return it.
+ #
+ # example:
+ # class MyClass_
+ # def initialize(arg1, arg2)
+ # @arg1 = arg1; @arg2 = arg2
+ # end
+ # end
+ # filename = 'example.rhtml' # @arg1 and @arg2 are used in example.rhtml
+ # erb = ERB.new(File.read(filename))
+ # erb.filename = filename
+ # MyClass = erb.def_class(MyClass_, 'render()')
+ # print MyClass.new('foo', 123).render()
+ def def_class(superklass=Object, methodname='result')
+ cls = Class.new(superklass)
+ def_method(cls, methodname, @filename || '(ERB)')
+ cls
+ end
+end
+
+#--
+# ERB::Util
+class ERB
+ # A utility module for conversion routines, often handy in HTML generation.
+ module Util
+ public
+ #
+ # A utility method for escaping HTML tag characters in _s_.
+ #
+ # require "erb"
+ # include ERB::Util
+ #
+ # puts html_escape("is a > 0 & a < 10?")
+ #
+ # _Generates_
+ #
+ # is a &gt; 0 &amp; a &lt; 10?
+ #
+ def html_escape(s)
+ s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
+ end
+ alias h html_escape
+ module_function :h
+ module_function :html_escape
+
+ #
+ # A utility method for encoding the String _s_ as a URL.
+ #
+ # require "erb"
+ # include ERB::Util
+ #
+ # puts url_encode("Programming Ruby: The Pragmatic Programmer's Guide")
+ #
+ # _Generates_
+ #
+ # Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide
+ #
+ def url_encode(s)
+ s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[^a-zA-Z0-9_\-.]/n) {
+ sprintf("%%%02X", $&.unpack("C")[0])
+ }
+ end
+ alias u url_encode
+ module_function :u
+ module_function :url_encode
+ end
+end
+
+#--
+# ERB::DefMethod
+class ERB
+ # Utility module to define eRuby script as instance method.
+ #
+ # === Example
+ #
+ # example.rhtml:
+ # <% for item in @items %>
+ # <b><%= item %></b>
+ # <% end %>
+ #
+ # example.rb:
+ # require 'erb'
+ # class MyClass
+ # extend ERB::DefMethod
+ # def_erb_method('render()', 'example.rhtml')
+ # def initialize(items)
+ # @items = items
+ # end
+ # end
+ # print MyClass.new([10,20,30]).render()
+ #
+ # result:
+ #
+ # <b>10</b>
+ #
+ # <b>20</b>
+ #
+ # <b>30</b>
+ #
+ module DefMethod
+ public
+ # define _methodname_ as instance method of current module, using ERB object or eRuby file
+ def def_erb_method(methodname, erb_or_fname)
+ if erb_or_fname.kind_of? String
+ fname = erb_or_fname
+ erb = ERB.new(File.read(fname))
+ erb.def_method(self, methodname, fname)
+ else
+ erb = erb_or_fname
+ erb.def_method(self, methodname, erb.filename || '(ERB)')
+ end
+ end
+ module_function :def_erb_method
+ end
+end
diff --git a/ruby/lib/expect.rb b/ruby/lib/expect.rb
new file mode 100644
index 0000000..08191b0
--- /dev/null
+++ b/ruby/lib/expect.rb
@@ -0,0 +1,36 @@
+$expect_verbose = false
+
+class IO
+ def expect(pat,timeout=9999999)
+ buf = ''
+ case pat
+ when String
+ e_pat = Regexp.new(Regexp.quote(pat))
+ when Regexp
+ e_pat = pat
+ end
+ while true
+ if !IO.select([self],nil,nil,timeout) or eof? then
+ result = nil
+ break
+ end
+ c = getc.chr
+ buf << c
+ if $expect_verbose
+ STDOUT.print c
+ STDOUT.flush
+ end
+ if mat=e_pat.match(buf) then
+ result = [buf,*mat.to_a[1..-1]]
+ break
+ end
+ end
+ if block_given? then
+ yield result
+ else
+ return result
+ end
+ nil
+ end
+end
+
diff --git a/ruby/lib/fileutils.rb b/ruby/lib/fileutils.rb
new file mode 100644
index 0000000..0a3fdc4
--- /dev/null
+++ b/ruby/lib/fileutils.rb
@@ -0,0 +1,1592 @@
+#
+# = fileutils.rb
+#
+# Copyright (c) 2000-2007 Minero Aoki
+#
+# This program is free software.
+# You can distribute/modify this program under the same terms of ruby.
+#
+# == module FileUtils
+#
+# Namespace for several file utility methods for copying, moving, removing, etc.
+#
+# === Module Functions
+#
+# cd(dir, options)
+# cd(dir, options) {|dir| .... }
+# pwd()
+# mkdir(dir, options)
+# mkdir(list, options)
+# mkdir_p(dir, options)
+# mkdir_p(list, options)
+# rmdir(dir, options)
+# rmdir(list, options)
+# ln(old, new, options)
+# ln(list, destdir, options)
+# ln_s(old, new, options)
+# ln_s(list, destdir, options)
+# ln_sf(src, dest, options)
+# cp(src, dest, options)
+# cp(list, dir, options)
+# cp_r(src, dest, options)
+# cp_r(list, dir, options)
+# mv(src, dest, options)
+# mv(list, dir, options)
+# rm(list, options)
+# rm_r(list, options)
+# rm_rf(list, options)
+# install(src, dest, mode = <src's>, options)
+# chmod(mode, list, options)
+# chmod_R(mode, list, options)
+# chown(user, group, list, options)
+# chown_R(user, group, list, options)
+# touch(list, options)
+#
+# The <tt>options</tt> parameter is a hash of options, taken from the list
+# <tt>:force</tt>, <tt>:noop</tt>, <tt>:preserve</tt>, and <tt>:verbose</tt>.
+# <tt>:noop</tt> means that no changes are made. The other two are obvious.
+# Each method documents the options that it honours.
+#
+# All methods that have the concept of a "source" file or directory can take
+# either one file or a list of files in that argument. See the method
+# documentation for examples.
+#
+# There are some `low level' methods, which do not accept any option:
+#
+# copy_entry(src, dest, preserve = false, dereference = false)
+# copy_file(src, dest, preserve = false, dereference = true)
+# copy_stream(srcstream, deststream)
+# remove_entry(path, force = false)
+# remove_entry_secure(path, force = false)
+# remove_file(path, force = false)
+# compare_file(path_a, path_b)
+# compare_stream(stream_a, stream_b)
+# uptodate?(file, cmp_list)
+#
+# == module FileUtils::Verbose
+#
+# This module has all methods of FileUtils module, but it outputs messages
+# before acting. This equates to passing the <tt>:verbose</tt> flag to methods
+# in FileUtils.
+#
+# == module FileUtils::NoWrite
+#
+# This module has all methods of FileUtils module, but never changes
+# files/directories. This equates to passing the <tt>:noop</tt> flag to methods
+# in FileUtils.
+#
+# == module FileUtils::DryRun
+#
+# This module has all methods of FileUtils module, but never changes
+# files/directories. This equates to passing the <tt>:noop</tt> and
+# <tt>:verbose</tt> flags to methods in FileUtils.
+#
+
+module FileUtils
+
+ def self.private_module_function(name) #:nodoc:
+ module_function name
+ private_class_method name
+ end
+
+ # This hash table holds command options.
+ OPT_TABLE = {} #:nodoc: internal use only
+
+ #
+ # Options: (none)
+ #
+ # Returns the name of the current directory.
+ #
+ def pwd
+ Dir.pwd
+ end
+ module_function :pwd
+
+ alias getwd pwd
+ module_function :getwd
+
+ #
+ # Options: verbose
+ #
+ # Changes the current directory to the directory +dir+.
+ #
+ # If this method is called with block, resumes to the old
+ # working directory after the block execution finished.
+ #
+ # FileUtils.cd('/', :verbose => true) # chdir and report it
+ #
+ def cd(dir, options = {}, &block) # :yield: dir
+ fu_check_options options, OPT_TABLE['cd']
+ fu_output_message "cd #{dir}" if options[:verbose]
+ Dir.chdir(dir, &block)
+ fu_output_message 'cd -' if options[:verbose] and block
+ end
+ module_function :cd
+
+ alias chdir cd
+ module_function :chdir
+
+ OPT_TABLE['cd'] =
+ OPT_TABLE['chdir'] = [:verbose]
+
+ #
+ # Options: (none)
+ #
+ # Returns true if +newer+ is newer than all +old_list+.
+ # Non-existent files are older than any file.
+ #
+ # FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \
+ # system 'make hello.o'
+ #
+ def uptodate?(new, old_list, options = nil)
+ raise ArgumentError, 'uptodate? does not accept any option' if options
+
+ return false unless File.exist?(new)
+ new_time = File.mtime(new)
+ old_list.each do |old|
+ if File.exist?(old)
+ return false unless new_time > File.mtime(old)
+ end
+ end
+ true
+ end
+ module_function :uptodate?
+
+ #
+ # Options: mode noop verbose
+ #
+ # Creates one or more directories.
+ #
+ # FileUtils.mkdir 'test'
+ # FileUtils.mkdir %w( tmp data )
+ # FileUtils.mkdir 'notexist', :noop => true # Does not really create.
+ # FileUtils.mkdir 'tmp', :mode => 0700
+ #
+ def mkdir(list, options = {})
+ fu_check_options options, OPT_TABLE['mkdir']
+ list = fu_list(list)
+ fu_output_message "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
+ return if options[:noop]
+
+ list.each do |dir|
+ fu_mkdir dir, options[:mode]
+ end
+ end
+ module_function :mkdir
+
+ OPT_TABLE['mkdir'] = [:mode, :noop, :verbose]
+
+ #
+ # Options: mode noop verbose
+ #
+ # Creates a directory and all its parent directories.
+ # For example,
+ #
+ # FileUtils.mkdir_p '/usr/local/lib/ruby'
+ #
+ # causes to make following directories, if it does not exist.
+ # * /usr
+ # * /usr/local
+ # * /usr/local/lib
+ # * /usr/local/lib/ruby
+ #
+ # You can pass several directories at a time in a list.
+ #
+ def mkdir_p(list, options = {})
+ fu_check_options options, OPT_TABLE['mkdir_p']
+ list = fu_list(list)
+ fu_output_message "mkdir -p #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
+ return *list if options[:noop]
+
+ list.map {|path| path.sub(%r</\z>, '') }.each do |path|
+ # optimize for the most common case
+ begin
+ fu_mkdir path, options[:mode]
+ next
+ rescue SystemCallError
+ next if File.directory?(path)
+ end
+
+ stack = []
+ until path == stack.last # dirname("/")=="/", dirname("C:/")=="C:/"
+ stack.push path
+ path = File.dirname(path)
+ end
+ stack.reverse_each do |dir|
+ begin
+ fu_mkdir dir, options[:mode]
+ rescue SystemCallError => err
+ raise unless File.directory?(dir)
+ end
+ end
+ end
+
+ return *list
+ end
+ module_function :mkdir_p
+
+ alias mkpath mkdir_p
+ alias makedirs mkdir_p
+ module_function :mkpath
+ module_function :makedirs
+
+ OPT_TABLE['mkdir_p'] =
+ OPT_TABLE['mkpath'] =
+ OPT_TABLE['makedirs'] = [:mode, :noop, :verbose]
+
+ def fu_mkdir(path, mode) #:nodoc:
+ path = path.sub(%r</\z>, '')
+ if mode
+ Dir.mkdir path, mode
+ File.chmod mode, path
+ else
+ Dir.mkdir path
+ end
+ end
+ private_module_function :fu_mkdir
+
+ #
+ # Options: noop, verbose
+ #
+ # Removes one or more directories.
+ #
+ # FileUtils.rmdir 'somedir'
+ # FileUtils.rmdir %w(somedir anydir otherdir)
+ # # Does not really remove directory; outputs message.
+ # FileUtils.rmdir 'somedir', :verbose => true, :noop => true
+ #
+ def rmdir(list, options = {})
+ fu_check_options options, OPT_TABLE['rmdir']
+ list = fu_list(list)
+ parents = options[:parents]
+ fu_output_message "rmdir #{parents ? '-p ' : ''}#{list.join ' '}" if options[:verbose]
+ return if options[:noop]
+ list.each do |dir|
+ begin
+ Dir.rmdir(dir = dir.sub(%r</\z>, ''))
+ if parents
+ until (parent = File.dirname(dir)) == '.' or parent == dir
+ Dir.rmdir(dir)
+ end
+ end
+ rescue Errno::ENOTEMPTY, Errno::ENOENT
+ end
+ end
+ end
+ module_function :rmdir
+
+ OPT_TABLE['rmdir'] = [:parents, :noop, :verbose]
+
+ #
+ # Options: force noop verbose
+ #
+ # <b><tt>ln(old, new, options = {})</tt></b>
+ #
+ # Creates a hard link +new+ which points to +old+.
+ # If +new+ already exists and it is a directory, creates a link +new/old+.
+ # If +new+ already exists and it is not a directory, raises Errno::EEXIST.
+ # But if :force option is set, overwrite +new+.
+ #
+ # FileUtils.ln 'gcc', 'cc', :verbose => true
+ # FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
+ #
+ # <b><tt>ln(list, destdir, options = {})</tt></b>
+ #
+ # Creates several hard links in a directory, with each one pointing to the
+ # item in +list+. If +destdir+ is not a directory, raises Errno::ENOTDIR.
+ #
+ # include FileUtils
+ # cd '/sbin'
+ # FileUtils.ln %w(cp mv mkdir), '/bin' # Now /sbin/cp and /bin/cp are linked.
+ #
+ def ln(src, dest, options = {})
+ fu_check_options options, OPT_TABLE['ln']
+ fu_output_message "ln#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
+ return if options[:noop]
+ fu_each_src_dest0(src, dest) do |s,d|
+ remove_file d, true if options[:force]
+ File.link s, d
+ end
+ end
+ module_function :ln
+
+ alias link ln
+ module_function :link
+
+ OPT_TABLE['ln'] =
+ OPT_TABLE['link'] = [:force, :noop, :verbose]
+
+ #
+ # Options: force noop verbose
+ #
+ # <b><tt>ln_s(old, new, options = {})</tt></b>
+ #
+ # Creates a symbolic link +new+ which points to +old+. If +new+ already
+ # exists and it is a directory, creates a symbolic link +new/old+. If +new+
+ # already exists and it is not a directory, raises Errno::EEXIST. But if
+ # :force option is set, overwrite +new+.
+ #
+ # FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
+ # FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force => true
+ #
+ # <b><tt>ln_s(list, destdir, options = {})</tt></b>
+ #
+ # Creates several symbolic links in a directory, with each one pointing to the
+ # item in +list+. If +destdir+ is not a directory, raises Errno::ENOTDIR.
+ #
+ # If +destdir+ is not a directory, raises Errno::ENOTDIR.
+ #
+ # FileUtils.ln_s Dir.glob('bin/*.rb'), '/home/aamine/bin'
+ #
+ def ln_s(src, dest, options = {})
+ fu_check_options options, OPT_TABLE['ln_s']
+ fu_output_message "ln -s#{options[:force] ? 'f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
+ return if options[:noop]
+ fu_each_src_dest0(src, dest) do |s,d|
+ remove_file d, true if options[:force]
+ File.symlink s, d
+ end
+ end
+ module_function :ln_s
+
+ alias symlink ln_s
+ module_function :symlink
+
+ OPT_TABLE['ln_s'] =
+ OPT_TABLE['symlink'] = [:force, :noop, :verbose]
+
+ #
+ # Options: noop verbose
+ #
+ # Same as
+ # #ln_s(src, dest, :force)
+ #
+ def ln_sf(src, dest, options = {})
+ fu_check_options options, OPT_TABLE['ln_sf']
+ options = options.dup
+ options[:force] = true
+ ln_s src, dest, options
+ end
+ module_function :ln_sf
+
+ OPT_TABLE['ln_sf'] = [:noop, :verbose]
+
+ #
+ # Options: preserve noop verbose
+ #
+ # Copies a file content +src+ to +dest+. If +dest+ is a directory,
+ # copies +src+ to +dest/src+.
+ #
+ # If +src+ is a list of files, then +dest+ must be a directory.
+ #
+ # FileUtils.cp 'eval.c', 'eval.c.org'
+ # FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6'
+ # FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose => true
+ # FileUtils.cp 'symlink', 'dest' # copy content, "dest" is not a symlink
+ #
+ def cp(src, dest, options = {})
+ fu_check_options options, OPT_TABLE['cp']
+ fu_output_message "cp#{options[:preserve] ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
+ return if options[:noop]
+ fu_each_src_dest(src, dest) do |s, d|
+ copy_file s, d, options[:preserve]
+ end
+ end
+ module_function :cp
+
+ alias copy cp
+ module_function :copy
+
+ OPT_TABLE['cp'] =
+ OPT_TABLE['copy'] = [:preserve, :noop, :verbose]
+
+ #
+ # Options: preserve noop verbose dereference_root remove_destination
+ #
+ # Copies +src+ to +dest+. If +src+ is a directory, this method copies
+ # all its contents recursively. If +dest+ is a directory, copies
+ # +src+ to +dest/src+.
+ #
+ # +src+ can be a list of files.
+ #
+ # # Installing ruby library "mylib" under the site_ruby
+ # FileUtils.rm_r site_ruby + '/mylib', :force
+ # FileUtils.cp_r 'lib/', site_ruby + '/mylib'
+ #
+ # # Examples of copying several files to target directory.
+ # FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
+ # FileUtils.cp_r Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop => true, :verbose => true
+ #
+ # # If you want to copy all contents of a directory instead of the
+ # # directory itself, c.f. src/x -> dest/x, src/y -> dest/y,
+ # # use following code.
+ # FileUtils.cp_r 'src/.', 'dest' # cp_r('src', 'dest') makes src/dest,
+ # # but this doesn't.
+ #
+ def cp_r(src, dest, options = {})
+ fu_check_options options, OPT_TABLE['cp_r']
+ fu_output_message "cp -r#{options[:preserve] ? 'p' : ''}#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
+ return if options[:noop]
+ fu_each_src_dest(src, dest) do |s, d|
+ copy_entry s, d, options[:preserve], options[:dereference_root], options[:remove_destination]
+ end
+ end
+ module_function :cp_r
+
+ OPT_TABLE['cp_r'] = [:preserve, :noop, :verbose,
+ :dereference_root, :remove_destination]
+
+ #
+ # Copies a file system entry +src+ to +dest+.
+ # If +src+ is a directory, this method copies its contents recursively.
+ # This method preserves file types, c.f. symlink, directory...
+ # (FIFO, device files and etc. are not supported yet)
+ #
+ # Both of +src+ and +dest+ must be a path name.
+ # +src+ must exist, +dest+ must not exist.
+ #
+ # If +preserve+ is true, this method preserves owner, group, permissions
+ # and modified time.
+ #
+ # If +dereference_root+ is true, this method dereference tree root.
+ #
+ # If +remove_destination+ is true, this method removes each destination file before copy.
+ #
+ def copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)
+ Entry_.new(src, nil, dereference_root).traverse do |ent|
+ destent = Entry_.new(dest, ent.rel, false)
+ File.unlink destent.path if remove_destination && File.file?(destent.path)
+ ent.copy destent.path
+ ent.copy_metadata destent.path if preserve
+ end
+ end
+ module_function :copy_entry
+
+ #
+ # Copies file contents of +src+ to +dest+.
+ # Both of +src+ and +dest+ must be a path name.
+ #
+ def copy_file(src, dest, preserve = false, dereference = true)
+ ent = Entry_.new(src, nil, dereference)
+ ent.copy_file dest
+ ent.copy_metadata dest if preserve
+ end
+ module_function :copy_file
+
+ #
+ # Copies stream +src+ to +dest+.
+ # +src+ must respond to #read(n) and
+ # +dest+ must respond to #write(str).
+ #
+ def copy_stream(src, dest)
+ IO.copy_stream(src, dest)
+ end
+ module_function :copy_stream
+
+ #
+ # Options: force noop verbose
+ #
+ # Moves file(s) +src+ to +dest+. If +file+ and +dest+ exist on the different
+ # disk partition, the file is copied then the original file is removed.
+ #
+ # FileUtils.mv 'badname.rb', 'goodname.rb'
+ # FileUtils.mv 'stuff.rb', '/notexist/lib/ruby', :force => true # no error
+ #
+ # FileUtils.mv %w(junk.txt dust.txt), '/home/aamine/.trash/'
+ # FileUtils.mv Dir.glob('test*.rb'), 'test', :noop => true, :verbose => true
+ #
+ def mv(src, dest, options = {})
+ fu_check_options options, OPT_TABLE['mv']
+ fu_output_message "mv#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
+ return if options[:noop]
+ fu_each_src_dest(src, dest) do |s, d|
+ destent = Entry_.new(d, nil, true)
+ begin
+ if destent.exist?
+ if destent.directory?
+ raise Errno::EEXIST, dest
+ else
+ destent.remove_file if rename_cannot_overwrite_file?
+ end
+ end
+ begin
+ File.rename s, d
+ rescue Errno::EXDEV
+ copy_entry s, d, true
+ if options[:secure]
+ remove_entry_secure s, options[:force]
+ else
+ remove_entry s, options[:force]
+ end
+ end
+ rescue SystemCallError
+ raise unless options[:force]
+ end
+ end
+ end
+ module_function :mv
+
+ alias move mv
+ module_function :move
+
+ OPT_TABLE['mv'] =
+ OPT_TABLE['move'] = [:force, :noop, :verbose, :secure]
+
+ def rename_cannot_overwrite_file? #:nodoc:
+ /cygwin|mswin|mingw|bccwin|emx/ =~ RUBY_PLATFORM
+ end
+ private_module_function :rename_cannot_overwrite_file?
+
+ #
+ # Options: force noop verbose
+ #
+ # Remove file(s) specified in +list+. This method cannot remove directories.
+ # All StandardErrors are ignored when the :force option is set.
+ #
+ # FileUtils.rm %w( junk.txt dust.txt )
+ # FileUtils.rm Dir.glob('*.so')
+ # FileUtils.rm 'NotExistFile', :force => true # never raises exception
+ #
+ def rm(list, options = {})
+ fu_check_options options, OPT_TABLE['rm']
+ list = fu_list(list)
+ fu_output_message "rm#{options[:force] ? ' -f' : ''} #{list.join ' '}" if options[:verbose]
+ return if options[:noop]
+
+ list.each do |path|
+ remove_file path, options[:force]
+ end
+ end
+ module_function :rm
+
+ alias remove rm
+ module_function :remove
+
+ OPT_TABLE['rm'] =
+ OPT_TABLE['remove'] = [:force, :noop, :verbose]
+
+ #
+ # Options: noop verbose
+ #
+ # Equivalent to
+ #
+ # #rm(list, :force => true)
+ #
+ def rm_f(list, options = {})
+ fu_check_options options, OPT_TABLE['rm_f']
+ options = options.dup
+ options[:force] = true
+ rm list, options
+ end
+ module_function :rm_f
+
+ alias safe_unlink rm_f
+ module_function :safe_unlink
+
+ OPT_TABLE['rm_f'] =
+ OPT_TABLE['safe_unlink'] = [:noop, :verbose]
+
+ #
+ # Options: force noop verbose secure
+ #
+ # remove files +list+[0] +list+[1]... If +list+[n] is a directory,
+ # removes its all contents recursively. This method ignores
+ # StandardError when :force option is set.
+ #
+ # FileUtils.rm_r Dir.glob('/tmp/*')
+ # FileUtils.rm_r '/', :force => true # :-)
+ #
+ # WARNING: This method causes local vulnerability
+ # if one of parent directories or removing directory tree are world
+ # writable (including /tmp, whose permission is 1777), and the current
+ # process has strong privilege such as Unix super user (root), and the
+ # system has symbolic link. For secure removing, read the documentation
+ # of #remove_entry_secure carefully, and set :secure option to true.
+ # Default is :secure=>false.
+ #
+ # NOTE: This method calls #remove_entry_secure if :secure option is set.
+ # See also #remove_entry_secure.
+ #
+ def rm_r(list, options = {})
+ fu_check_options options, OPT_TABLE['rm_r']
+ # options[:secure] = true unless options.key?(:secure)
+ list = fu_list(list)
+ fu_output_message "rm -r#{options[:force] ? 'f' : ''} #{list.join ' '}" if options[:verbose]
+ return if options[:noop]
+ list.each do |path|
+ if options[:secure]
+ remove_entry_secure path, options[:force]
+ else
+ remove_entry path, options[:force]
+ end
+ end
+ end
+ module_function :rm_r
+
+ OPT_TABLE['rm_r'] = [:force, :noop, :verbose, :secure]
+
+ #
+ # Options: noop verbose secure
+ #
+ # Equivalent to
+ #
+ # #rm_r(list, :force => true)
+ #
+ # WARNING: This method causes local vulnerability.
+ # Read the documentation of #rm_r first.
+ #
+ def rm_rf(list, options = {})
+ fu_check_options options, OPT_TABLE['rm_rf']
+ options = options.dup
+ options[:force] = true
+ rm_r list, options
+ end
+ module_function :rm_rf
+
+ alias rmtree rm_rf
+ module_function :rmtree
+
+ OPT_TABLE['rm_rf'] =
+ OPT_TABLE['rmtree'] = [:noop, :verbose, :secure]
+
+ #
+ # This method removes a file system entry +path+. +path+ shall be a
+ # regular file, a directory, or something. If +path+ is a directory,
+ # remove it recursively. This method is required to avoid TOCTTOU
+ # (time-of-check-to-time-of-use) local security vulnerability of #rm_r.
+ # #rm_r causes security hole when:
+ #
+ # * Parent directory is world writable (including /tmp).
+ # * Removing directory tree includes world writable directory.
+ # * The system has symbolic link.
+ #
+ # To avoid this security hole, this method applies special preprocess.
+ # If +path+ is a directory, this method chown(2) and chmod(2) all
+ # removing directories. This requires the current process is the
+ # owner of the removing whole directory tree, or is the super user (root).
+ #
+ # WARNING: You must ensure that *ALL* parent directories are not
+ # world writable. Otherwise this method does not work.
+ # Only exception is temporary directory like /tmp and /var/tmp,
+ # whose permission is 1777.
+ #
+ # WARNING: Only the owner of the removing directory tree, or Unix super
+ # user (root) should invoke this method. Otherwise this method does not
+ # work.
+ #
+ # For details of this security vulnerability, see Perl's case:
+ #
+ # http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CAN-2005-0448
+ # http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CAN-2004-0452
+ #
+ # For fileutils.rb, this vulnerability is reported in [ruby-dev:26100].
+ #
+ def remove_entry_secure(path, force = false)
+ unless fu_have_symlink?
+ remove_entry path, force
+ return
+ end
+ fullpath = File.expand_path(path)
+ st = File.lstat(fullpath)
+ unless st.directory?
+ File.unlink fullpath
+ return
+ end
+ # is a directory.
+ parent_st = File.stat(File.dirname(fullpath))
+ unless parent_st.world_writable?
+ remove_entry path, force
+ return
+ end
+ unless parent_st.sticky?
+ raise ArgumentError, "parent directory is world writable, FileUtils#remove_entry_secure does not work; abort: #{path.inspect} (parent directory mode #{'%o' % parent_st.mode})"
+ end
+ # freeze tree root
+ euid = Process.euid
+ File.open(fullpath + '/.') {|f|
+ unless fu_stat_identical_entry?(st, f.stat)
+ # symlink (TOC-to-TOU attack?)
+ File.unlink fullpath
+ return
+ end
+ f.chown euid, -1
+ f.chmod 0700
+ }
+ # ---- tree root is frozen ----
+ root = Entry_.new(path)
+ root.preorder_traverse do |ent|
+ if ent.directory?
+ ent.chown euid, -1
+ ent.chmod 0700
+ end
+ end
+ root.postorder_traverse do |ent|
+ begin
+ ent.remove
+ rescue
+ raise unless force
+ end
+ end
+ rescue
+ raise unless force
+ end
+ module_function :remove_entry_secure
+
+ def fu_have_symlink? #:nodoc
+ File.symlink nil, nil
+ rescue NotImplementedError
+ return false
+ rescue
+ return true
+ end
+ private_module_function :fu_have_symlink?
+
+ def fu_stat_identical_entry?(a, b) #:nodoc:
+ a.dev == b.dev and a.ino == b.ino
+ end
+ private_module_function :fu_stat_identical_entry?
+
+ #
+ # This method removes a file system entry +path+.
+ # +path+ might be a regular file, a directory, or something.
+ # If +path+ is a directory, remove it recursively.
+ #
+ # See also #remove_entry_secure.
+ #
+ def remove_entry(path, force = false)
+ Entry_.new(path).postorder_traverse do |ent|
+ begin
+ ent.remove
+ rescue
+ raise unless force
+ end
+ end
+ rescue
+ raise unless force
+ end
+ module_function :remove_entry
+
+ #
+ # Removes a file +path+.
+ # This method ignores StandardError if +force+ is true.
+ #
+ def remove_file(path, force = false)
+ Entry_.new(path).remove_file
+ rescue
+ raise unless force
+ end
+ module_function :remove_file
+
+ #
+ # Removes a directory +dir+ and its contents recursively.
+ # This method ignores StandardError if +force+ is true.
+ #
+ def remove_dir(path, force = false)
+ remove_entry path, force # FIXME?? check if it is a directory
+ end
+ module_function :remove_dir
+
+ #
+ # Returns true if the contents of a file A and a file B are identical.
+ #
+ # FileUtils.compare_file('somefile', 'somefile') #=> true
+ # FileUtils.compare_file('/bin/cp', '/bin/mv') #=> maybe false
+ #
+ def compare_file(a, b)
+ return false unless File.size(a) == File.size(b)
+ File.open(a, 'rb') {|fa|
+ File.open(b, 'rb') {|fb|
+ return compare_stream(fa, fb)
+ }
+ }
+ end
+ module_function :compare_file
+
+ alias identical? compare_file
+ alias cmp compare_file
+ module_function :identical?
+ module_function :cmp
+
+ #
+ # Returns true if the contents of a stream +a+ and +b+ are identical.
+ #
+ def compare_stream(a, b)
+ bsize = fu_stream_blksize(a, b)
+ sa = sb = nil
+ while sa == sb
+ sa = a.read(bsize)
+ sb = b.read(bsize)
+ unless sa and sb
+ if sa.nil? and sb.nil?
+ return true
+ end
+ end
+ end
+ false
+ end
+ module_function :compare_stream
+
+ #
+ # Options: mode preserve noop verbose
+ #
+ # If +src+ is not same as +dest+, copies it and changes the permission
+ # mode to +mode+. If +dest+ is a directory, destination is +dest+/+src+.
+ # This method removes destination before copy.
+ #
+ # FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
+ # FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
+ #
+ def install(src, dest, options = {})
+ fu_check_options options, OPT_TABLE['install']
+ fu_output_message "install -c#{options[:preserve] && ' -p'}#{options[:mode] ? (' -m 0%o' % options[:mode]) : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
+ return if options[:noop]
+ fu_each_src_dest(src, dest) do |s, d|
+ unless File.exist?(d) and compare_file(s, d)
+ remove_file d, true
+ st = File.stat(s) if options[:preserve]
+ copy_file s, d
+ File.utime st.atime, st.mtime, d if options[:preserve]
+ File.chmod options[:mode], d if options[:mode]
+ end
+ end
+ end
+ module_function :install
+
+ OPT_TABLE['install'] = [:mode, :preserve, :noop, :verbose]
+
+ #
+ # Options: noop verbose
+ #
+ # Changes permission bits on the named files (in +list+) to the bit pattern
+ # represented by +mode+.
+ #
+ # FileUtils.chmod 0755, 'somecommand'
+ # FileUtils.chmod 0644, %w(my.rb your.rb his.rb her.rb)
+ # FileUtils.chmod 0755, '/usr/bin/ruby', :verbose => true
+ #
+ def chmod(mode, list, options = {})
+ fu_check_options options, OPT_TABLE['chmod']
+ list = fu_list(list)
+ fu_output_message sprintf('chmod %o %s', mode, list.join(' ')) if options[:verbose]
+ return if options[:noop]
+ list.each do |path|
+ Entry_.new(path).chmod mode
+ end
+ end
+ module_function :chmod
+
+ OPT_TABLE['chmod'] = [:noop, :verbose]
+
+ #
+ # Options: noop verbose force
+ #
+ # Changes permission bits on the named files (in +list+)
+ # to the bit pattern represented by +mode+.
+ #
+ # FileUtils.chmod_R 0700, "/tmp/app.#{$$}"
+ #
+ def chmod_R(mode, list, options = {})
+ fu_check_options options, OPT_TABLE['chmod_R']
+ list = fu_list(list)
+ fu_output_message sprintf('chmod -R%s %o %s',
+ (options[:force] ? 'f' : ''),
+ mode, list.join(' ')) if options[:verbose]
+ return if options[:noop]
+ list.each do |root|
+ Entry_.new(root).traverse do |ent|
+ begin
+ ent.chmod mode
+ rescue
+ raise unless options[:force]
+ end
+ end
+ end
+ end
+ module_function :chmod_R
+
+ OPT_TABLE['chmod_R'] = [:noop, :verbose, :force]
+
+ #
+ # Options: noop verbose
+ #
+ # Changes owner and group on the named files (in +list+)
+ # to the user +user+ and the group +group+. +user+ and +group+
+ # may be an ID (Integer/String) or a name (String).
+ # If +user+ or +group+ is nil, this method does not change
+ # the attribute.
+ #
+ # FileUtils.chown 'root', 'staff', '/usr/local/bin/ruby'
+ # FileUtils.chown nil, 'bin', Dir.glob('/usr/bin/*'), :verbose => true
+ #
+ def chown(user, group, list, options = {})
+ fu_check_options options, OPT_TABLE['chown']
+ list = fu_list(list)
+ fu_output_message sprintf('chown %s%s',
+ [user,group].compact.join(':') + ' ',
+ list.join(' ')) if options[:verbose]
+ return if options[:noop]
+ uid = fu_get_uid(user)
+ gid = fu_get_gid(group)
+ list.each do |path|
+ Entry_.new(path).chown uid, gid
+ end
+ end
+ module_function :chown
+
+ OPT_TABLE['chown'] = [:noop, :verbose]
+
+ #
+ # Options: noop verbose force
+ #
+ # Changes owner and group on the named files (in +list+)
+ # to the user +user+ and the group +group+ recursively.
+ # +user+ and +group+ may be an ID (Integer/String) or
+ # a name (String). If +user+ or +group+ is nil, this
+ # method does not change the attribute.
+ #
+ # FileUtils.chown_R 'www', 'www', '/var/www/htdocs'
+ # FileUtils.chown_R 'cvs', 'cvs', '/var/cvs', :verbose => true
+ #
+ def chown_R(user, group, list, options = {})
+ fu_check_options options, OPT_TABLE['chown_R']
+ list = fu_list(list)
+ fu_output_message sprintf('chown -R%s %s%s',
+ (options[:force] ? 'f' : ''),
+ [user,group].compact.join(':') + ' ',
+ list.join(' ')) if options[:verbose]
+ return if options[:noop]
+ uid = fu_get_uid(user)
+ gid = fu_get_gid(group)
+ return unless uid or gid
+ list.each do |root|
+ Entry_.new(root).traverse do |ent|
+ begin
+ ent.chown uid, gid
+ rescue
+ raise unless options[:force]
+ end
+ end
+ end
+ end
+ module_function :chown_R
+
+ OPT_TABLE['chown_R'] = [:noop, :verbose, :force]
+
+ begin
+ require 'etc'
+
+ def fu_get_uid(user) #:nodoc:
+ return nil unless user
+ user = user.to_s
+ if /\A\d+\z/ =~ user
+ then user.to_i
+ else Etc.getpwnam(user).uid
+ end
+ end
+ private_module_function :fu_get_uid
+
+ def fu_get_gid(group) #:nodoc:
+ return nil unless group
+ group = group.to_s
+ if /\A\d+\z/ =~ group
+ then group.to_i
+ else Etc.getgrnam(group).gid
+ end
+ end
+ private_module_function :fu_get_gid
+
+ rescue LoadError
+ # need Win32 support???
+
+ def fu_get_uid(user) #:nodoc:
+ user # FIXME
+ end
+ private_module_function :fu_get_uid
+
+ def fu_get_gid(group) #:nodoc:
+ group # FIXME
+ end
+ private_module_function :fu_get_gid
+ end
+
+ #
+ # Options: noop verbose
+ #
+ # Updates modification time (mtime) and access time (atime) of file(s) in
+ # +list+. Files are created if they don't exist.
+ #
+ # FileUtils.touch 'timestamp'
+ # FileUtils.touch Dir.glob('*.c'); system 'make'
+ #
+ def touch(list, options = {})
+ fu_check_options options, OPT_TABLE['touch']
+ list = fu_list(list)
+ created = nocreate = options[:nocreate]
+ t = options[:mtime]
+ if options[:verbose]
+ fu_output_message "touch #{nocreate ? ' -c' : ''}#{t ? t.strftime(' -t %Y%m%d%H%M.%S') : ''}#{list.join ' '}"
+ end
+ return if options[:noop]
+ list.each do |path|
+ created = nocreate
+ begin
+ File.utime(t, t, path)
+ rescue Errno::ENOENT
+ raise if created
+ File.open(path, 'a') {
+ ;
+ }
+ created = true
+ retry if t
+ end
+ end
+ end
+ module_function :touch
+
+ OPT_TABLE['touch'] = [:noop, :verbose, :mtime, :nocreate]
+
+ private
+
+ module StreamUtils_
+ private
+
+ def fu_windows?
+ /mswin|mingw|bccwin|emx/ =~ RUBY_PLATFORM
+ end
+
+ def fu_copy_stream0(src, dest, blksize = nil) #:nodoc:
+ IO.copy_stream(src, dest)
+ end
+
+ def fu_stream_blksize(*streams)
+ streams.each do |s|
+ next unless s.respond_to?(:stat)
+ size = fu_blksize(s.stat)
+ return size if size
+ end
+ fu_default_blksize()
+ end
+
+ def fu_blksize(st)
+ s = st.blksize
+ return nil unless s
+ return nil if s == 0
+ s
+ end
+
+ def fu_default_blksize
+ 1024
+ end
+ end
+
+ include StreamUtils_
+ extend StreamUtils_
+
+ class Entry_ #:nodoc: internal use only
+ include StreamUtils_
+
+ def initialize(a, b = nil, deref = false)
+ @prefix = @rel = @path = nil
+ if b
+ @prefix = a
+ @rel = b
+ else
+ @path = a
+ end
+ @deref = deref
+ @stat = nil
+ @lstat = nil
+ end
+
+ def inspect
+ "\#<#{self.class} #{path()}>"
+ end
+
+ def path
+ if @path
+ File.path(@path)
+ else
+ join(@prefix, @rel)
+ end
+ end
+
+ def prefix
+ @prefix || @path
+ end
+
+ def rel
+ @rel
+ end
+
+ def dereference?
+ @deref
+ end
+
+ def exist?
+ lstat! ? true : false
+ end
+
+ def file?
+ s = lstat!
+ s and s.file?
+ end
+
+ def directory?
+ s = lstat!
+ s and s.directory?
+ end
+
+ def symlink?
+ s = lstat!
+ s and s.symlink?
+ end
+
+ def chardev?
+ s = lstat!
+ s and s.chardev?
+ end
+
+ def blockdev?
+ s = lstat!
+ s and s.blockdev?
+ end
+
+ def socket?
+ s = lstat!
+ s and s.socket?
+ end
+
+ def pipe?
+ s = lstat!
+ s and s.pipe?
+ end
+
+ S_IF_DOOR = 0xD000
+
+ def door?
+ s = lstat!
+ s and (s.mode & 0xF000 == S_IF_DOOR)
+ end
+
+ def entries
+ Dir.entries(path())\
+ .reject {|n| n == '.' or n == '..' }\
+ .map {|n| Entry_.new(prefix(), join(rel(), n.untaint)) }
+ end
+
+ def stat
+ return @stat if @stat
+ if lstat() and lstat().symlink?
+ @stat = File.stat(path())
+ else
+ @stat = lstat()
+ end
+ @stat
+ end
+
+ def stat!
+ return @stat if @stat
+ if lstat! and lstat!.symlink?
+ @stat = File.stat(path())
+ else
+ @stat = lstat!
+ end
+ @stat
+ rescue SystemCallError
+ nil
+ end
+
+ def lstat
+ if dereference?
+ @lstat ||= File.stat(path())
+ else
+ @lstat ||= File.lstat(path())
+ end
+ end
+
+ def lstat!
+ lstat()
+ rescue SystemCallError
+ nil
+ end
+
+ def chmod(mode)
+ if symlink?
+ File.lchmod mode, path() if have_lchmod?
+ else
+ File.chmod mode, path()
+ end
+ end
+
+ def chown(uid, gid)
+ if symlink?
+ File.lchown uid, gid, path() if have_lchown?
+ else
+ File.chown uid, gid, path()
+ end
+ end
+
+ def copy(dest)
+ case
+ when file?
+ copy_file dest
+ when directory?
+ if !File.exist?(dest) and /^#{Regexp.quote(path)}/ =~ File.dirname(dest)
+ raise ArgumentError, "cannot copy directory %s to itself %s" % [path, dest]
+ end
+ begin
+ Dir.mkdir dest
+ rescue
+ raise unless File.directory?(dest)
+ end
+ when symlink?
+ File.symlink File.readlink(path()), dest
+ when chardev?
+ raise "cannot handle device file" unless File.respond_to?(:mknod)
+ mknod dest, ?c, 0666, lstat().rdev
+ when blockdev?
+ raise "cannot handle device file" unless File.respond_to?(:mknod)
+ mknod dest, ?b, 0666, lstat().rdev
+ when socket?
+ raise "cannot handle socket" unless File.respond_to?(:mknod)
+ mknod dest, nil, lstat().mode, 0
+ when pipe?
+ raise "cannot handle FIFO" unless File.respond_to?(:mkfifo)
+ mkfifo dest, 0666
+ when door?
+ raise "cannot handle door: #{path()}"
+ else
+ raise "unknown file type: #{path()}"
+ end
+ end
+
+ def copy_file(dest)
+ File.open(dest, 'wb') do |f|
+ IO.copy_stream(path(), f)
+ end
+ end
+
+ def copy_metadata(path)
+ st = lstat()
+ File.utime st.atime, st.mtime, path
+ begin
+ File.chown st.uid, st.gid, path
+ rescue Errno::EPERM
+ # clear setuid/setgid
+ File.chmod st.mode & 01777, path
+ else
+ File.chmod st.mode, path
+ end
+ end
+
+ def remove
+ if directory?
+ remove_dir1
+ else
+ remove_file
+ end
+ end
+
+ def remove_dir1
+ platform_support {
+ Dir.rmdir path().sub(%r</\z>, '')
+ }
+ end
+
+ def remove_file
+ platform_support {
+ File.unlink path
+ }
+ end
+
+ def platform_support
+ return yield unless fu_windows?
+ first_time_p = true
+ begin
+ yield
+ rescue Errno::ENOENT
+ raise
+ rescue => err
+ if first_time_p
+ first_time_p = false
+ begin
+ File.chmod 0700, path() # Windows does not have symlink
+ retry
+ rescue SystemCallError
+ end
+ end
+ raise err
+ end
+ end
+
+ def preorder_traverse
+ stack = [self]
+ while ent = stack.pop
+ yield ent
+ stack.concat ent.entries.reverse if ent.directory?
+ end
+ end
+
+ alias traverse preorder_traverse
+
+ def postorder_traverse
+ if directory?
+ entries().each do |ent|
+ ent.postorder_traverse do |e|
+ yield e
+ end
+ end
+ end
+ yield self
+ end
+
+ private
+
+ $fileutils_rb_have_lchmod = nil
+
+ def have_lchmod?
+ # This is not MT-safe, but it does not matter.
+ if $fileutils_rb_have_lchmod == nil
+ $fileutils_rb_have_lchmod = check_have_lchmod?
+ end
+ $fileutils_rb_have_lchmod
+ end
+
+ def check_have_lchmod?
+ return false unless File.respond_to?(:lchmod)
+ File.lchmod 0
+ return true
+ rescue NotImplementedError
+ return false
+ end
+
+ $fileutils_rb_have_lchown = nil
+
+ def have_lchown?
+ # This is not MT-safe, but it does not matter.
+ if $fileutils_rb_have_lchown == nil
+ $fileutils_rb_have_lchown = check_have_lchown?
+ end
+ $fileutils_rb_have_lchown
+ end
+
+ def check_have_lchown?
+ return false unless File.respond_to?(:lchown)
+ File.lchown nil, nil
+ return true
+ rescue NotImplementedError
+ return false
+ end
+
+ def join(dir, base)
+ return File.path(dir) if not base or base == '.'
+ return File.path(base) if not dir or dir == '.'
+ File.join(dir, base)
+ end
+ end # class Entry_
+
+ def fu_list(arg) #:nodoc:
+ [arg].flatten.map {|path| File.path(path) }
+ end
+ private_module_function :fu_list
+
+ def fu_each_src_dest(src, dest) #:nodoc:
+ fu_each_src_dest0(src, dest) do |s, d|
+ raise ArgumentError, "same file: #{s} and #{d}" if fu_same?(s, d)
+ yield s, d
+ end
+ end
+ private_module_function :fu_each_src_dest
+
+ def fu_each_src_dest0(src, dest) #:nodoc:
+ if tmp = Array.try_convert(src)
+ tmp.each do |s|
+ s = File.path(s)
+ yield s, File.join(dest, File.basename(s))
+ end
+ else
+ src = File.path(src)
+ if File.directory?(dest)
+ yield src, File.join(dest, File.basename(src))
+ else
+ yield src, File.path(dest)
+ end
+ end
+ end
+ private_module_function :fu_each_src_dest0
+
+ def fu_same?(a, b) #:nodoc:
+ if fu_have_st_ino?
+ st1 = File.stat(a)
+ st2 = File.stat(b)
+ st1.dev == st2.dev and st1.ino == st2.ino
+ else
+ File.expand_path(a) == File.expand_path(b)
+ end
+ rescue Errno::ENOENT
+ return false
+ end
+ private_module_function :fu_same?
+
+ def fu_have_st_ino? #:nodoc:
+ not fu_windows?
+ end
+ private_module_function :fu_have_st_ino?
+
+ def fu_check_options(options, optdecl) #:nodoc:
+ h = options.dup
+ optdecl.each do |opt|
+ h.delete opt
+ end
+ raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty?
+ end
+ private_module_function :fu_check_options
+
+ def fu_update_option(args, new) #:nodoc:
+ if tmp = Hash.try_convert(args.last)
+ args[-1] = tmp.dup.update(new)
+ else
+ args.push new
+ end
+ args
+ end
+ private_module_function :fu_update_option
+
+ @fileutils_output = $stderr
+ @fileutils_label = ''
+
+ def fu_output_message(msg) #:nodoc:
+ @fileutils_output ||= $stderr
+ @fileutils_label ||= ''
+ @fileutils_output.puts @fileutils_label + msg
+ end
+ private_module_function :fu_output_message
+
+ #
+ # Returns an Array of method names which have any options.
+ #
+ # p FileUtils.commands #=> ["chmod", "cp", "cp_r", "install", ...]
+ #
+ def FileUtils.commands
+ OPT_TABLE.keys
+ end
+
+ #
+ # Returns an Array of option names.
+ #
+ # p FileUtils.options #=> ["noop", "force", "verbose", "preserve", "mode"]
+ #
+ def FileUtils.options
+ OPT_TABLE.values.flatten.uniq.map {|sym| sym.to_s }
+ end
+
+ #
+ # Returns true if the method +mid+ have an option +opt+.
+ #
+ # p FileUtils.have_option?(:cp, :noop) #=> true
+ # p FileUtils.have_option?(:rm, :force) #=> true
+ # p FileUtils.have_option?(:rm, :perserve) #=> false
+ #
+ def FileUtils.have_option?(mid, opt)
+ li = OPT_TABLE[mid.to_s] or raise ArgumentError, "no such method: #{mid}"
+ li.include?(opt)
+ end
+
+ #
+ # Returns an Array of option names of the method +mid+.
+ #
+ # p FileUtils.options(:rm) #=> ["noop", "verbose", "force"]
+ #
+ def FileUtils.options_of(mid)
+ OPT_TABLE[mid.to_s].map {|sym| sym.to_s }
+ end
+
+ #
+ # Returns an Array of method names which have the option +opt+.
+ #
+ # p FileUtils.collect_method(:preserve) #=> ["cp", "cp_r", "copy", "install"]
+ #
+ def FileUtils.collect_method(opt)
+ OPT_TABLE.keys.select {|m| OPT_TABLE[m].include?(opt) }
+ end
+
+ METHODS = singleton_methods() - [:private_module_function,
+ :commands, :options, :have_option?, :options_of, :collect_method]
+
+ #
+ # This module has all methods of FileUtils module, but it outputs messages
+ # before acting. This equates to passing the <tt>:verbose</tt> flag to
+ # methods in FileUtils.
+ #
+ module Verbose
+ include FileUtils
+ @fileutils_output = $stderr
+ @fileutils_label = ''
+ ::FileUtils.collect_method(:verbose).each do |name|
+ module_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{name}(*args)
+ super(*fu_update_option(args, :verbose => true))
+ end
+ private :#{name}
+ EOS
+ end
+ extend self
+ class << self
+ ::FileUtils::METHODS.each do |m|
+ public m
+ end
+ end
+ end
+
+ #
+ # This module has all methods of FileUtils module, but never changes
+ # files/directories. This equates to passing the <tt>:noop</tt> flag
+ # to methods in FileUtils.
+ #
+ module NoWrite
+ include FileUtils
+ @fileutils_output = $stderr
+ @fileutils_label = ''
+ ::FileUtils.collect_method(:noop).each do |name|
+ module_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{name}(*args)
+ super(*fu_update_option(args, :noop => true))
+ end
+ private :#{name}
+ EOS
+ end
+ extend self
+ class << self
+ ::FileUtils::METHODS.each do |m|
+ public m
+ end
+ end
+ end
+
+ #
+ # This module has all methods of FileUtils module, but never changes
+ # files/directories, with printing message before acting.
+ # This equates to passing the <tt>:noop</tt> and <tt>:verbose</tt> flag
+ # to methods in FileUtils.
+ #
+ module DryRun
+ include FileUtils
+ @fileutils_output = $stderr
+ @fileutils_label = ''
+ ::FileUtils.collect_method(:noop).each do |name|
+ module_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{name}(*args)
+ super(*fu_update_option(args, :noop => true, :verbose => true))
+ end
+ private :#{name}
+ EOS
+ end
+ extend self
+ class << self
+ ::FileUtils::METHODS.each do |m|
+ public m
+ end
+ end
+ end
+
+end
diff --git a/ruby/lib/find.rb b/ruby/lib/find.rb
new file mode 100644
index 0000000..79ff7c1
--- /dev/null
+++ b/ruby/lib/find.rb
@@ -0,0 +1,81 @@
+#
+# find.rb: the Find module for processing all files under a given directory.
+#
+
+#
+# The +Find+ module supports the top-down traversal of a set of file paths.
+#
+# For example, to total the size of all files under your home directory,
+# ignoring anything in a "dot" directory (e.g. $HOME/.ssh):
+#
+# require 'find'
+#
+# total_size = 0
+#
+# Find.find(ENV["HOME"]) do |path|
+# if FileTest.directory?(path)
+# if File.basename(path)[0] == ?.
+# Find.prune # Don't look any further into this directory.
+# else
+# next
+# end
+# else
+# total_size += FileTest.size(path)
+# end
+# end
+#
+module Find
+
+ #
+ # Calls the associated block with the name of every file and directory listed
+ # as arguments, then recursively on their subdirectories, and so on.
+ #
+ # See the +Find+ module documentation for an example.
+ #
+ def find(*paths) # :yield: path
+ block_given? or return enum_for(__method__, *paths)
+
+ paths.collect!{|d| raise Errno::ENOENT unless File.exist?(d); d.dup}
+ while file = paths.shift
+ catch(:prune) do
+ yield file.dup.taint
+ next unless File.exist? file
+ begin
+ if File.lstat(file).directory? then
+ d = Dir.open(file)
+ begin
+ for f in d
+ next if f == "." or f == ".."
+ if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
+ f = file + f
+ elsif file == "/" then
+ f = "/" + f
+ else
+ f = File.join(file, f)
+ end
+ paths.unshift f.untaint
+ end
+ ensure
+ d.close
+ end
+ end
+ rescue Errno::ENOENT, Errno::EACCES
+ end
+ end
+ end
+ end
+
+ #
+ # Skips the current file or directory, restarting the loop with the next
+ # entry. If the current file is a directory, that directory will not be
+ # recursively entered. Meaningful only within the block associated with
+ # Find::find.
+ #
+ # See the +Find+ module documentation for an example.
+ #
+ def prune
+ throw :prune
+ end
+
+ module_function :find, :prune
+end
diff --git a/ruby/lib/forwardable.rb b/ruby/lib/forwardable.rb
new file mode 100644
index 0000000..7b6b1dd
--- /dev/null
+++ b/ruby/lib/forwardable.rb
@@ -0,0 +1,270 @@
+#
+# forwardable.rb -
+# $Release Version: 1.1$
+# $Revision: 24174 $
+# by Keiju ISHITSUKA(keiju@ishitsuka.com)
+# original definition by delegator.rb
+# Revised by Daniel J. Berger with suggestions from Florian Gross.
+#
+# Documentation by James Edward Gray II and Gavin Sinclair
+#
+# == Introduction
+#
+# This library allows you delegate method calls to an object, on a method by
+# method basis.
+#
+# == Notes
+#
+# Be advised, RDoc will not detect delegated methods.
+#
+# <b>forwardable.rb provides single-method delegation via the
+# def_delegator() and def_delegators() methods. For full-class
+# delegation via DelegateClass(), see delegate.rb.</b>
+#
+# == Examples
+#
+# === Forwardable
+#
+# Forwardable makes building a new class based on existing work, with a proper
+# interface, almost trivial. We want to rely on what has come before obviously,
+# but with delegation we can take just the methods we need and even rename them
+# as appropriate. In many cases this is preferable to inheritance, which gives
+# us the entire old interface, even if much of it isn't needed.
+#
+# class Queue
+# extend Forwardable
+#
+# def initialize
+# @q = [ ] # prepare delegate object
+# end
+#
+# # setup preferred interface, enq() and deq()...
+# def_delegator :@q, :push, :enq
+# def_delegator :@q, :shift, :deq
+#
+# # support some general Array methods that fit Queues well
+# def_delegators :@q, :clear, :first, :push, :shift, :size
+# end
+#
+# q = Queue.new
+# q.enq 1, 2, 3, 4, 5
+# q.push 6
+#
+# q.shift # => 1
+# while q.size > 0
+# puts q.deq
+# end
+#
+# q.enq "Ruby", "Perl", "Python"
+# puts q.first
+# q.clear
+# puts q.first
+#
+# <i>Prints:</i>
+#
+# 2
+# 3
+# 4
+# 5
+# 6
+# Ruby
+# nil
+#
+# SingleForwardable can be used to setup delegation at the object level as well.
+#
+# printer = String.new
+# printer.extend SingleForwardable # prepare object for delegation
+# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
+# printer.puts "Howdy!"
+#
+# Also, SingleForwardable can be use to Class or Module.
+#
+# module Facade
+# extend SingleForwardable
+# def_delegator :Implementation, :service
+#
+# class Implementation
+# def service...
+# end
+# end
+#
+# If you want to use both Forwardable and SingleForwardable, you can
+# use methods def_instance_delegator and def_single_delegator, etc.
+#
+# If the object isn't a Module and Class, You can too extend
+# Forwardable module.
+# printer = String.new
+# printer.extend Forwardable # prepare object for delegation
+# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
+# printer.puts "Howdy!"
+#
+# <i>Prints:</i>
+#
+# Howdy!
+
+#
+# The Forwardable module provides delegation of specified
+# methods to a designated object, using the methods #def_delegator
+# and #def_delegators.
+#
+# For example, say you have a class RecordCollection which
+# contains an array <tt>@records</tt>. You could provide the lookup method
+# #record_number(), which simply calls #[] on the <tt>@records</tt>
+# array, like this:
+#
+# class RecordCollection
+# extend Forwardable
+# def_delegator :@records, :[], :record_number
+# end
+#
+# Further, if you wish to provide the methods #size, #<<, and #map,
+# all of which delegate to @records, this is how you can do it:
+#
+# class RecordCollection
+# # extend Forwardable, but we did that above
+# def_delegators :@records, :size, :<<, :map
+# end
+# f = Foo.new
+# f.printf ...
+# f.gets
+# f.content_at(1)
+#
+# Also see the example at forwardable.rb.
+
+module Forwardable
+ FORWARDABLE_VERSION = "1.1.0"
+
+ @debug = nil
+ class<<self
+ attr_accessor :debug
+ end
+
+ # Takes a hash as its argument. The key is a symbol or an array of
+ # symbols. These symbols correspond to method names. The value is
+ # the accessor to which the methods will be delegated.
+ #
+ # :call-seq:
+ # delegate method => accessor
+ # delegate [method, method, ...] => accessor
+ #
+ def instance_delegate(hash)
+ hash.each{ |methods, accessor|
+ methods = methods.to_s unless methods.respond_to?(:each)
+ methods.each{ |method|
+ def_instance_delegator(accessor, method)
+ }
+ }
+ end
+
+ #
+ # Shortcut for defining multiple delegator methods, but with no
+ # provision for using a different name. The following two code
+ # samples have the same effect:
+ #
+ # def_delegators :@records, :size, :<<, :map
+ #
+ # def_delegator :@records, :size
+ # def_delegator :@records, :<<
+ # def_delegator :@records, :map
+ #
+ def def_instance_delegators(accessor, *methods)
+ methods.delete("__send__")
+ methods.delete("__id__")
+ for method in methods
+ def_instance_delegator(accessor, method)
+ end
+ end
+
+ def def_instance_delegator(accessor, method, ali = method)
+ line_no = __LINE__; str = %{
+ def #{ali}(*args, &block)
+ begin
+ #{accessor}.__send__(:#{method}, *args, &block)
+ rescue Exception
+ $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
+ ::Kernel::raise
+ end
+ end
+ }
+ # If it's not a class or module, it's an instance
+ begin
+ module_eval(str, __FILE__, line_no)
+ rescue
+ instance_eval(str, __FILE__, line_no)
+ end
+
+ end
+
+ alias delegate instance_delegate
+ alias def_delegators def_instance_delegators
+ alias def_delegator def_instance_delegator
+end
+
+#
+# Usage of The SingleForwardable is like Fowadable module.
+#
+module SingleForwardable
+ # Takes a hash as its argument. The key is a symbol or an array of
+ # symbols. These symbols correspond to method names. The value is
+ # the accessor to which the methods will be delegated.
+ #
+ # :call-seq:
+ # delegate method => accessor
+ # delegate [method, method, ...] => accessor
+ #
+ def single_delegate(hash)
+ hash.each{ |methods, accessor|
+ methods = methods.to_s unless methods.respond_to?(:each)
+ methods.each{ |method|
+ def_single_delegator(accessor, method)
+ }
+ }
+ end
+
+ #
+ # Shortcut for defining multiple delegator methods, but with no
+ # provision for using a different name. The following two code
+ # samples have the same effect:
+ #
+ # def_delegators :@records, :size, :<<, :map
+ #
+ # def_delegator :@records, :size
+ # def_delegator :@records, :<<
+ # def_delegator :@records, :map
+ #
+ def def_single_delegators(accessor, *methods)
+ methods.delete("__send__")
+ methods.delete("__id__")
+ for method in methods
+ def_single_delegator(accessor, method)
+ end
+ end
+
+ #
+ # Defines a method _method_ which delegates to _obj_ (i.e. it calls
+ # the method of the same name in _obj_). If _new_name_ is
+ # provided, it is used as the name for the delegate method.
+ #
+ def def_single_delegator(accessor, method, ali = method)
+ line_no = __LINE__; str = %{
+ def #{ali}(*args, &block)
+ begin
+ #{accessor}.__send__(:#{method}, *args, &block)
+ rescue Exception
+ $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
+ ::Kernel::raise
+ end
+ end
+ }
+
+ instance_eval(str, __FILE__, __LINE__)
+ end
+
+ alias delegate single_delegate
+ alias def_delegators def_single_delegators
+ alias def_delegator def_single_delegator
+end
+
+
+
+
diff --git a/ruby/lib/ftsearch/analysis/analyzer.rb b/ruby/lib/ftsearch/analysis/analyzer.rb
new file mode 100644
index 0000000..0e08760
--- /dev/null
+++ b/ruby/lib/ftsearch/analysis/analyzer.rb
@@ -0,0 +1,16 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+module FTSearch
+module Analysis
+
+class Analyzer
+ def find_suffixes(text)
+ ret = []
+ append_suffixes(ret, text, 0)
+ ret
+ end
+end
+
+end # Analysis
+end # FTSearch
diff --git a/ruby/lib/ftsearch/analysis/simple_identifier_analyzer.rb b/ruby/lib/ftsearch/analysis/simple_identifier_analyzer.rb
new file mode 100644
index 0000000..b939861
--- /dev/null
+++ b/ruby/lib/ftsearch/analysis/simple_identifier_analyzer.rb
@@ -0,0 +1,23 @@
+
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+require 'strscan'
+require 'ftsearch/analysis/analyzer'
+
+module FTSearch
+module Analysis
+
+class SimpleIdentifierAnalyzer < Analyzer
+ def append_suffixes(array, text, offset)
+ sc = StringScanner.new(text)
+ sc.skip(/[^A-Za-z_]+/)
+ until sc.eos?
+ array << (sc.pos + offset)
+ break unless sc.skip(/[A-Za-z_][A-Za-z0-9_]*[^A-Za-z_]*/)
+ end
+ end
+end
+
+end # Analyzer
+end # FTSearch
diff --git a/ruby/lib/ftsearch/analysis/whitespace_analyzer.rb b/ruby/lib/ftsearch/analysis/whitespace_analyzer.rb
new file mode 100644
index 0000000..6cb5191
--- /dev/null
+++ b/ruby/lib/ftsearch/analysis/whitespace_analyzer.rb
@@ -0,0 +1,22 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+require 'strscan'
+require 'ftsearch/analysis/analyzer'
+
+module FTSearch
+module Analysis
+ class WhiteSpaceAnalyzer < Analyzer
+ def append_suffixes(array, text, offset)
+ sc = StringScanner.new(text)
+ sc.skip(/(\s|\n)*/)
+ until sc.eos?
+ array << (sc.pos + offset)
+ break unless sc.skip(/\S+\s*/)
+ end
+
+ array
+ end
+ end
+end # Analyzer
+end # FTSearch
diff --git a/ruby/lib/ftsearch/document_map_reader.rb b/ruby/lib/ftsearch/document_map_reader.rb
new file mode 100644
index 0000000..5d842c0
--- /dev/null
+++ b/ruby/lib/ftsearch/document_map_reader.rb
@@ -0,0 +1,106 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+module FTSearch
+
+class DocumentMapReader
+ DEFAULT_OPTIONS = {
+ :path => nil,
+ :io => nil,
+ }
+ def initialize(options = {})
+ options = DEFAULT_OPTIONS.merge(options)
+ unless options[:path] || options[:io]
+ raise ArgumentError, "Need either the path to the suffix array file or an IO."
+ end
+ if options[:path]
+ io = File.open(options[:path], "rb")
+ else
+ io = options[:io]
+ end
+ @uri_tbl, @field_arr = Marshal.load(io)
+ end
+
+ def document_id(suffix_idx, offset)
+ idx = binary_search(@field_arr, offset, 0, @field_arr.size)
+ @field_arr[idx][1]
+ end
+
+ def field_id(suffix_idx, offset)
+ idx = binary_search(@field_arr, offset, 0, @field_arr.size)
+ @field_arr[idx][2]
+ end
+
+ def field_size(suffix_idx, offset)
+ idx = binary_search(@field_arr, offset, 0, @field_arr.size)
+ @field_arr[idx][3]
+ end
+
+ def field_info(suffix_idx, offset)
+ idx = binary_search(@field_arr, offset, 0, @field_arr.size)
+ @field_arr[idx]
+ end
+
+ def document_uri(suffix_idx, offset)
+ doc_id = document_id(suffix_idx, offset)
+ @uri_tbl[doc_id]
+ end
+
+ def document_id_to_uri(doc_id)
+ @uri_tbl[doc_id]
+ end
+
+ def offsets_to_field_infos(offsets)
+ offsets.map{|off| @field_arr[binary_search(@field_arr, off, 0, @field_arr.size)]}
+ end
+
+ def rank_offsets(offsets, weights)
+ h = Hash.new{|h,k| h[k] = 0.0}
+ offsets_to_field_infos(offsets).each do |offset, doc_id, field_id, field_size|
+ h[doc_id] += weights[field_id] / field_size
+ end
+ sort_score_hash(h)
+ end
+
+ def rank_offsets_probabilistic(offsets, weights, iterations)
+ h = Hash.new{|h,k| h[k] = 0.0}
+ infos = offsets_to_field_infos(offsets)
+ max = infos.size
+ while iterations > 0
+ offset, doc_id, field_id, field_size = infos[rand(max)]
+ h[doc_id] += weights[field_id] / field_size
+ iterations -= 1
+ end
+ sort_score_hash(h)
+ end
+
+ def dump_data
+ [@uri_tbl, @field_arr]
+ end
+
+ def num_documents
+ @uri_tbl.size
+ end
+
+ private
+ def sort_score_hash(h)
+ h.sort_by{|_,score| -score}
+ end
+
+ def binary_search(arr, offset, from, to)
+ middle = 0
+ while to - from > 1
+ middle = (from + to) / 2
+ pivot, = arr[middle]
+ if offset < pivot
+ to = middle
+ else
+ from = middle
+ end
+ end
+
+ from
+ end
+
+end
+end # FTSearch
diff --git a/ruby/lib/ftsearch/document_map_writer.rb b/ruby/lib/ftsearch/document_map_writer.rb
new file mode 100644
index 0000000..7bf20bc
--- /dev/null
+++ b/ruby/lib/ftsearch/document_map_writer.rb
@@ -0,0 +1,46 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+require 'ftsearch/util'
+
+module FTSearch
+class DocumentMapWriter
+ include InMemoryWriter
+
+ DEFAULT_OPTIONS = {
+ :path => "docmap-#{Process.pid}-#{rand(100000)}",
+ }
+ def initialize(options = {})
+ options = DEFAULT_OPTIONS.merge(options)
+ @path = options[:path]
+ @field_arr = []
+ @uri_tbl = []
+ @data = [@uri_tbl, @field_arr]
+ initialize_in_memory_buffer
+ end
+
+ def merge(doc_map_reader)
+ # TODO: general merge?
+ @uri_tbl, @field_arr = doc_map_reader.dump_data
+ @data = [@uri_tbl, @field_arr]
+ end
+
+ def add_document(doc_id, uri)
+ @uri_tbl[doc_id] = uri
+ end
+
+ def add_field(offset, doc_id, field_id, size)
+ @field_arr << [offset, doc_id, field_id, size]
+ end
+
+ def finish!
+ if @path
+ File.open(@path, "wb") do |f|
+ Marshal.dump(@data, f)
+ end
+ else
+ Marshal.dump(@data, @memory_io)
+ end
+ end
+end
+end # FTSearch
diff --git a/ruby/lib/ftsearch/field_infos.rb b/ruby/lib/ftsearch/field_infos.rb
new file mode 100644
index 0000000..91749b2
--- /dev/null
+++ b/ruby/lib/ftsearch/field_infos.rb
@@ -0,0 +1,46 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+require 'ftsearch/analysis/whitespace_analyzer'
+
+module FTSearch
+class FieldInfos
+ DEFAULT_OPTIONS = {
+ :default_analyzer => FTSearch::Analysis::WhiteSpaceAnalyzer.new,
+ :stored => true,
+ }
+ def initialize(options = {})
+ options = DEFAULT_OPTIONS.merge(options)
+ @fields = {}
+ @default_options = options
+ end
+
+ def add_field(options = {})
+ options = @default_options.merge(options)
+ raise "Need a name" unless options[:name]
+ store_field_info(options)
+ end
+
+ def [](field_name)
+ if field_info = @fields[field_name]
+ field_info
+ else
+ store_field_info(:name => field_name)
+ end
+ end
+
+ private
+ def store_field_info(options)
+ options = @default_options.merge(options)
+ unless options[:analyzer]
+ if klass = options[:analyzer_class]
+ options[:analyzer] = klass.new
+ else
+ options[:analyzer] = @default_options[:default_analyzer]
+ end
+ end
+ @fields[options[:name]] = options
+ end
+end
+end # FTSearch
+
diff --git a/ruby/lib/ftsearch/fragment_writer.rb b/ruby/lib/ftsearch/fragment_writer.rb
new file mode 100644
index 0000000..79b783f
--- /dev/null
+++ b/ruby/lib/ftsearch/fragment_writer.rb
@@ -0,0 +1,114 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+require 'fileutils'
+require 'ftsearch/suffix_array_writer'
+require 'ftsearch/document_map_writer'
+require 'ftsearch/fulltext_writer'
+require 'ftsearch/field_infos'
+
+require 'ftsearch/fulltext_reader'
+require 'ftsearch/document_map_reader'
+require 'ftsearch/suffix_array_reader'
+
+module FTSearch
+
+class FragmentWriter
+ DEFAULT_OPTIONS = {
+ :path => "ftsearch-#{Process.pid}-#{rand(100000)}",
+ :default_analyzer_class => FTSearch::Analysis::WhiteSpaceAnalyzer,
+ :field_infos_class => FieldInfos,
+ :fulltext_writer_class => FulltextWriter,
+ :suffix_array_writer_class => SuffixArrayWriter,
+ :doc_map_writer_class => DocumentMapWriter,
+ :field_infos => nil,
+ :fulltext_writer => nil,
+ :suffix_array_writer => nil,
+ :doc_map_writer_nil => nil,
+ }
+
+ attr_reader :fulltext_writer, :suffix_array_writer, :doc_map_writer
+ def initialize(options = {})
+ options = DEFAULT_OPTIONS.merge(options)
+ create = lambda do |name, *args|
+ options[name] || options[(name.to_s + "_class").to_sym].new(*args)
+ end
+ build_path = lambda do |suffix|
+ if @path
+ File.join(@tmpdir, suffix)
+ else
+ nil
+ end
+ end
+ @path = options[:path]
+ if @path
+ @path = File.expand_path(@path)
+ @tmpdir = @path + "#{Process.pid}-#{rand(100000)}"
+ FileUtils.mkdir_p(@tmpdir)
+ end
+
+ @fulltext_writer = create.call(:fulltext_writer, :path => build_path["fulltext"])
+ @suffix_array_writer = create.call(:suffix_array_writer, :path => build_path["suffixes"])
+ @doc_map_writer = create.call(:doc_map_writer, :path => build_path["docmap"])
+
+ default_analyzer = (klass = options[:default_analyzer_class]) ? klass.new : nil
+ @field_infos = create.call(:field_infos, :default_analyzer => default_analyzer)
+ @num_documents = 0
+ @field_map = Hash.new{|h,k| h[k.to_sym] = h.size}
+ @field_map[:uri] # init
+ end
+
+ def add_document(doc_hash)
+ uri = doc_hash[:uri] || @num_documents.to_s
+ @fulltext_writer.add_document(@num_documents, doc_hash.merge(:uri => uri),
+ @field_map, @field_infos, @suffix_array_writer, @doc_map_writer)
+ @num_documents += 1
+ end
+
+ def merge(fragment_directory)
+ raise "Cannot import old data unless the destination Fragment is empty." unless @num_documents == 0
+ # TODO: use a FragmentReader to access old data
+ fulltext_reader = FulltextReader.new(:path => "#{fragment_directory}/fulltext")
+ suffix_array_reader = SuffixArrayReader.new(fulltext_reader, nil,
+ :path => "#{fragment_directory}/suffixes")
+ doc_map_reader = DocumentMapReader.new(:path => "#{fragment_directory}/docmap")
+ @fulltext_writer.merge(fulltext_reader)
+ @suffix_array_writer.merge(suffix_array_reader)
+ @doc_map_writer.merge(doc_map_reader)
+ #FIXME: .num_documents will be wrong if some URIs were repeated
+ @num_documents = doc_map_reader.num_documents
+ File.open(File.join(fragment_directory, "fieldmap"), "rb") do |f|
+ i = 0
+ f.each_line{|l| @field_map[l.chomp.to_sym] = i; i+= 1}
+ end
+ end
+
+ def fields
+ @field_map.sort_by{|field, fid| fid}.map{|field, fid| field}
+ end
+
+ def documents
+ @num_documents
+ end
+
+ def field_id(field)
+ @field_map.has_key?(field) && @field_map[field]
+ end
+
+ def finish!
+ @fulltext_writer.finish!
+ fulltext = @fulltext_writer.data
+ @suffix_array_writer.finish!(fulltext)
+ @doc_map_writer.finish!
+
+ if @path
+ File.open(File.join(@tmpdir, "fieldmap"), "wb") do |f|
+ @field_map.sort_by{|field_name, field_id| field_id}.each do |field_name, field_id|
+ f.puts field_name
+ end
+ File.rename(@tmpdir, @path)
+ end
+ end
+ end
+end
+end # FTSearch
diff --git a/ruby/lib/ftsearch/fulltext_reader.rb b/ruby/lib/ftsearch/fulltext_reader.rb
new file mode 100644
index 0000000..05da57b
--- /dev/null
+++ b/ruby/lib/ftsearch/fulltext_reader.rb
@@ -0,0 +1,52 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+module FTSearch
+
+class FulltextReader
+ DEFAULT_OPTIONS = {
+ :path => nil,
+ :io => nil,
+ }
+ def initialize(options = {})
+ options = DEFAULT_OPTIONS.merge(options)
+ unless options[:path] || options[:io]
+ raise ArgumentError, "Need either the path to the suffix array file or an IO."
+ end
+ init_internal_structures(options)
+ end
+
+ def get_data(offset, size)
+ @io.pos = offset
+ @io.read(size)
+ end
+
+ def dump_data(&block)
+ blocksize = 32768
+ @io.pos = 0
+ begin
+ size = @io.stat.size - 1
+ rescue NoMethodError # try with StringIO's interface
+ size = @io.string.size - 1
+ end
+ read = 0
+ #buffer = " " * blocksize
+ while read < size
+ #@io.read([size - read, blocksize].min, buffer)
+ #yield buffer
+ data = @io.read([size - read, blocksize].min)
+ read += data.size
+ yield data
+ end
+ end
+
+ private
+ def init_internal_structures(options)
+ if options[:path]
+ @io = File.open(options[:path], "rb")
+ else
+ @io = options[:io]
+ end
+ end
+end
+end # FTSearch
diff --git a/ruby/lib/ftsearch/fulltext_writer.rb b/ruby/lib/ftsearch/fulltext_writer.rb
new file mode 100644
index 0000000..ad92c0b
--- /dev/null
+++ b/ruby/lib/ftsearch/fulltext_writer.rb
@@ -0,0 +1,75 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+require 'ftsearch/util'
+
+module FTSearch
+class FulltextWriter
+ include InMemoryWriter
+
+ DEFAULT_OPTIONS = {
+ :path => "fulltext-#{Process.pid}-#{rand(100000)}",
+ }
+
+ attr_reader :path
+
+ def initialize(options = {})
+ options = DEFAULT_OPTIONS.merge(options)
+ @path = options[:path]
+ initialize_in_memory_buffer
+ if @path
+ @io = File.open(@path, "wb")
+ else
+ @io = @memory_io
+ end
+ end
+
+ def merge(fulltext_reader)
+ fulltext_reader.dump_data do |data|
+ @io.write data
+ end
+ end
+
+ def add_document(doc_id, doc_hash, field_mapping, field_infos, suffix_array_writer, doc_map_writer)
+ write_document_header(doc_id, doc_hash, field_mapping, field_infos)
+ doc_map_writer.add_document(doc_id, doc_hash[:uri])
+ doc_hash.each_pair do |field_name, data|
+ if field_id = field_mapping[field_name]
+ field_info = field_infos[field_name]
+ if field_info[:stored]
+ suffix_offset, segment_offset = store_field(doc_id, field_name, field_id, data)
+ if analyzer = field_info[:analyzer]
+ suffix_array_writer.add_suffixes(analyzer, data, suffix_offset)
+ end
+ doc_map_writer.add_field(segment_offset, doc_id, field_id, data.size)
+ end
+ end
+ end
+ end
+
+ def finish!
+ @io.write "\0"
+ @io.fsync
+ @io.close
+ end
+
+ private
+ def write_document_header(doc_id, doc_hash, field_mapping, field_infos)
+ stored_fields = doc_hash.select do |field_name, data|
+ field_infos[field_name][:stored]
+ end
+ total_size = stored_fields.inject(0){|s,(_,data)| s + data.size} + stored_fields.size * 9
+ # 9 = field ids plus field size plus trailing \0
+ @io.write [total_size].pack("V")
+ end
+
+ def store_field(doc_id, field_name, field_id, data)
+ @io.write [field_id, data.size].pack("V2")
+ offset = @io.pos
+ @io.write data
+ @io.write "\0"
+
+ [offset, offset - 8]
+ end
+end
+end # FTSearch
diff --git a/ruby/lib/ftsearch/suffix_array_reader.rb b/ruby/lib/ftsearch/suffix_array_reader.rb
new file mode 100644
index 0000000..830bf61
--- /dev/null
+++ b/ruby/lib/ftsearch/suffix_array_reader.rb
@@ -0,0 +1,277 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+module FTSearch
+
+class SuffixArrayReader
+ class Hit < Struct.new(:term, :suffix_number, :offset, :fulltext_reader)
+ def context(size)
+ strip_markers(self.fulltext_reader.get_data(offset - size, 2 * size), size)
+ end
+
+ def text(size)
+ strip_markers(self.fulltext_reader.get_data(offset, size), 0)
+ end
+
+ private
+ def strip_markers(str, size)
+ first = (str.rindex("\0", -size) || -1) + 1
+ last = str.index("\0", size) || str.size
+ str[first...last]
+ end
+ end
+
+ class LazyHits < Struct.new(:term, :suffix_array_reader, :fulltext_reader,
+ :from_index, :to_index)
+ include Enumerable
+ def each
+ sa_reader = self.suffix_array_reader
+ ft_reader = self.fulltext_reader
+ term = self.term
+ self.from_index.upto(self.to_index - 1) do |idx|
+ yield Hit.new(term, idx, sa_reader.suffix_index_to_offset(idx),
+ ft_reader)
+ end
+ end
+
+ def [](i)
+ i += to_index - from_index if i < 0
+ sa_reader = self.suffix_array_reader
+ if (idx = from_index + i) < to_index && idx >= from_index
+ Hit.new(self.term, idx, sa_reader.suffix_index_to_offset(idx),
+ self.fulltext_reader)
+ else
+ nil
+ end
+ end
+
+ def size
+ to_index - from_index
+ end
+ end
+
+ DEFAULT_OPTIONS = {
+ :path => nil,
+ :io => nil,
+ }
+ def initialize(fulltext_reader, doc_map, options = {})
+ options = DEFAULT_OPTIONS.merge(options)
+ @fulltext_reader = fulltext_reader
+ @doc_map = doc_map
+ unless options[:path] || options[:io]
+ raise ArgumentError, "Need either the path to the suffix array file or an IO."
+ end
+ init_internal_structures(options)
+ end
+
+ def count_hits(term)
+ from = binary_search(term, 0, @suffixes.size)
+ offset = @suffixes[from]
+ if @fulltext_reader.get_data(offset, term.size) == term
+ to = binary_search_upper(term, 0, @suffixes.size)
+ to - from
+ else
+ 0
+ end
+ end
+
+ def find_all(term)
+ from = binary_search(term, 0, @suffixes.size)
+ offset = @suffixes[from]
+ if @fulltext_reader.get_data(offset, term.size) == term
+ to = binary_search_upper(term, 0, @suffixes.size)
+ LazyHits.new(term, self, @fulltext_reader, from, to)
+ else
+ LazyHits.new(term, self, @fulltext_reader, 0, 0)
+ end
+ end
+
+ def find_first(term)
+ suffix_index = binary_search(term, 0, @suffixes.size)
+ offset = @suffixes[suffix_index]
+ if @fulltext_reader.get_data(offset, term.size) == term
+ Hit.new(term, suffix_index, offset, @fulltext_reader)
+ else
+ nil
+ end
+ end
+
+ def find_next(hit)
+ end
+
+ def suffix_index_to_offset(suffix_index)
+ @suffixes[suffix_index]
+ end
+
+ def lazyhits_to_offsets(lazyhits)
+ from = lazyhits.from_index
+ to = lazyhits.to_index
+ @io.pos = @base + 4 * from
+ @io.read((to - from) * 4).unpack("V*")
+ end
+
+ def dump_data
+ @io.pos = @base
+ while data = @io.read(32768)
+ yield data.unpack("V*")
+ end
+ end
+
+ private
+ def init_internal_structures(options)
+ if options[:path]
+ @io = File.open(options[:path], "rb")
+ else
+ @io = options[:io]
+ end
+ @total_suffixes, @block_size, @inline_suffix_size = @io.read(12).unpack("VVV")
+ @inline_suffixes = []
+ if @block_size != 0
+ 0.step(@total_suffixes, @block_size){ @inline_suffixes << @io.read(@inline_suffix_size)}
+ end
+
+ # skip padding
+ if (mod = @io.pos & 0xf) != 0
+ @io.read(16 - mod)
+ end
+
+ @base = @io.pos
+ #@suffixes = io.read.unpack("V*")
+ @suffixes = Object.new
+ nsuffixes = @total_suffixes
+ io = @io
+ base = @base
+ class << @suffixes; self end.module_eval do
+ define_method(:[]) do |i|
+ io.pos = base + i * 4
+ io.read(4).unpack("V")[0]
+ end
+ define_method(:size){ nsuffixes }
+ end
+ end
+
+ def binary_search(term, from, to)
+ from, to = binary_search_inline_suffixes(term, from, to)
+
+ tsize = term.size
+ while from < to
+ middle = (from + to) / 2
+ pivot = @fulltext_reader.get_data(@suffixes[middle], tsize)
+ if term <= pivot
+ to = middle
+ else
+ from = middle + 1
+ end
+ end
+
+ from
+ end
+
+ def binary_search_upper(term, from, to)
+ from, to = binary_search_inline_suffixes_upper(term, from, to)
+
+ tsize = term.size
+
+ #puts "#{from} -- #{to}"
+ #from.upto(to+5) do |idx|
+ # puts "#{idx} #{@fulltext_reader.get_data(@suffixes[idx], tsize + 10).inspect}"
+ #end
+ while from < to
+ middle = (from + to) / 2
+ pivot = @fulltext_reader.get_data(@suffixes[middle], tsize)
+ if term < pivot
+ to = middle
+ else
+ from = middle + 1
+ end
+ end
+
+ #puts "RET: #{from}"
+ from
+ end
+
+
+ def binary_search_inline_suffixes(term, from, to)
+ return [from, to] if @block_size == 0
+
+ tsize = term.size
+ while to - from > @block_size
+ middle = (from + to) / 2
+ #puts "from: #{from} to #{to} middle: #{middle}" if $DEBUG
+ quotient, mod = middle.divmod(@block_size)
+ middle = middle - mod
+ pivot = @inline_suffixes[quotient]
+ #puts "NOW: #{middle} pivot: #{pivot.inspect}" if $DEBUG
+ if tsize <= @inline_suffix_size
+ if term <= pivot
+ to = middle
+ else
+ from = middle + 1
+ end
+ elsif term[0, @inline_suffix_size] < pivot
+ to = middle
+ else
+ # FIXME: handle pivot[-1] = 255?
+ pivot = pivot.clone
+ #pivot[-1] += 1
+ pivot.next!
+ #puts "TESTING AGAINST new pivot: #{pivot.inspect}" if $DEBUG
+ if term > pivot
+ from = middle + 1
+ else # term[0, @inline_suffix_size] == pivot, disambiguate
+ pivot = @fulltext_reader.get_data(@suffixes[middle], term.size)
+ if term <= pivot
+ to = middle
+ else
+ from = middle + 1
+ end
+ end
+ end
+ end
+
+ [from, to]
+ end
+
+ def binary_search_inline_suffixes_upper(term, from, to)
+ return [from, to] if @block_size == 0
+
+ tsize = term.size
+ while to - from > @block_size
+ middle = (from + to) / 2
+ #puts "from: #{from} to #{to} middle: #{middle}" if $DEBUG
+ quotient, mod = middle.divmod(@block_size)
+ middle = middle - mod
+ pivot = @inline_suffixes[quotient]
+ #puts "NOW: #{middle} pivot: #{pivot.inspect}" if $DEBUG
+ if tsize <= @inline_suffix_size
+ if term < pivot[0, tsize]
+ to = middle
+ else
+ from = middle + 1
+ end
+ elsif term[0, @inline_suffix_size] < pivot
+ to = middle
+ else
+ # FIXME: handle pivot[-1] = 255?
+ pivot = pivot.clone
+ #pivot[-1] += 1
+ pivot.next!
+ #puts "TESTING AGAINST new pivot: #{pivot.inspect}" if $DEBUG
+ if term > pivot
+ from = middle + 1
+ else # term[0, @inline_suffix_size] == pivot, disambiguate
+ pivot = @fulltext_reader.get_data(@suffixes[middle], term.size)
+ if term < pivot
+ to = middle
+ else
+ from = middle + 1
+ end
+ end
+ end
+ end
+
+ [from, to]
+ end
+end
+
+end # FTSearch
diff --git a/ruby/lib/ftsearch/suffix_array_writer.rb b/ruby/lib/ftsearch/suffix_array_writer.rb
new file mode 100644
index 0000000..694dd3b
--- /dev/null
+++ b/ruby/lib/ftsearch/suffix_array_writer.rb
@@ -0,0 +1,99 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+require 'enumerator'
+require 'ftsearch/util'
+
+module FTSearch
+
+class SuffixArrayWriter
+ include InMemoryWriter
+
+ DEFAULT_OPTIONS = {
+ :path => "suffixes-#{Process.pid}-#{rand(100000)}",
+ :block_size => 32,
+ :inline_suffix_size => 8,
+ :default_analyzer => nil,
+ }
+ def initialize(options = {})
+ options = DEFAULT_OPTIONS.merge(options)
+ @path = options[:path]
+ @suffixes = []
+ @block_size = options[:block_size]
+ @inline_suffix_size = options[:inline_suffix_size]
+ @finished = false
+ initialize_in_memory_buffer
+ end
+
+ def merge(suffix_array_reader)
+ unless @suffixes.empty?
+ raise "Cannot merge if the destination SuffixArrayWriter isn't empty!"
+ end
+ suffix_array_reader.dump_data do |partial_sarray|
+ @suffixes.concat partial_sarray
+ end
+ end
+
+ def add_suffixes(analyzer, data, offset)
+ #analyzer.new.find_suffixes(data).each{|x| @suffixes << offset + x}
+ analyzer.append_suffixes(@suffixes, data, offset)
+ end
+
+ def finish!(fulltext)
+ return if @finished
+ if $DEBUG
+ puts "Suffixes: #{@suffixes.size}"
+ t0 = Time.new
+ end
+ sort!(fulltext)
+ if $DEBUG
+ puts "Sorted in #{Time.new - t0}"
+ end
+ if $DEBUG
+ puts "Dumping suffixes"
+ t0 = Time.new
+ end
+ dump_suffixes(fulltext)
+ if $DEBUG
+ puts "Dumped in #{Time.new - t0}"
+ end
+ @finished = true
+ end
+
+ private
+ def dump_suffixes(fulltext)
+ io = @path ? File.open(@path, "wb") : @memory_io
+ io.write([@suffixes.size, @block_size || 0, @inline_suffix_size].pack("VVV"))
+ if @block_size
+ dump_inline_suffixes(io, fulltext)
+ end
+ add_padding(io)
+ dump_suffix_array(io)
+ ensure
+ io.close if @path
+ end
+
+ def dump_inline_suffixes(io, fulltext)
+ 0.step(@suffixes.size, @block_size) do |suffix_idx|
+ io.write([fulltext[@suffixes[suffix_idx], @inline_suffix_size]].pack("a#{@inline_suffix_size}"))
+ end
+ end
+
+ def dump_suffix_array(io)
+ @suffixes.each_slice(1024){|suffixes| io.write(suffixes.pack("V*")) }
+ end
+
+ def add_padding(io)
+ # padding to 16-byte
+ if (mod = io.pos & 0xf) != 0
+ io.write("\0" * (16 - mod))
+ end
+ end
+
+ def sort!(fulltext)
+ tsize = fulltext.size
+ @suffixes = @suffixes.sort_by{|offset| fulltext[offset, tsize - offset]}
+ end
+end
+
+end # FTSearch
diff --git a/ruby/lib/ftsearch/util.rb b/ruby/lib/ftsearch/util.rb
new file mode 100644
index 0000000..2944a7b
--- /dev/null
+++ b/ruby/lib/ftsearch/util.rb
@@ -0,0 +1,21 @@
+# Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
+#
+
+require 'stringio'
+module FTSearch
+
+module InMemoryWriter
+ def initialize_in_memory_buffer
+ @memory_io = StringIO.new("")
+ end
+
+ def data
+ if @path
+ File.open(@path, "rb"){|f| f.read} rescue nil
+ else
+ @memory_io.string.clone
+ end
+ end
+end
+
+end # FTSearch
diff --git a/ruby/lib/getoptlong.rb b/ruby/lib/getoptlong.rb
new file mode 100644
index 0000000..4cfb5fb
--- /dev/null
+++ b/ruby/lib/getoptlong.rb
@@ -0,0 +1,610 @@
+#
+# GetoptLong for Ruby
+#
+# Copyright (C) 1998, 1999, 2000 Motoyuki Kasahara.
+#
+# You may redistribute and/or modify this library under the same license
+# terms as Ruby.
+#
+# See GetoptLong for documentation.
+#
+# Additional documents and the latest version of `getoptlong.rb' can be
+# found at http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/
+
+# The GetoptLong class allows you to parse command line options similarly to
+# the GNU getopt_long() C library call. Note, however, that GetoptLong is a
+# pure Ruby implementation.
+#
+# GetoptLong allows for POSIX-style options like <tt>--file</tt> as well
+# as single letter options like <tt>-f</tt>
+#
+# The empty option <tt>--</tt> (two minus symbols) is used to end option
+# processing. This can be particularly important if options have optional
+# arguments.
+#
+# Here is a simple example of usage:
+#
+# require 'getoptlong'
+# require 'rdoc/usage'
+#
+# opts = GetoptLong.new(
+# [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
+# [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
+# [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
+# )
+#
+# dir = nil
+# name = nil
+# repetitions = 1
+# opts.each do |opt, arg|
+# case opt
+# when '--help'
+# puts <<-EOF
+# hello [OPTION] ... DIR
+#
+# -h, --help:
+# show help
+#
+# --repeat x, -n x:
+# repeat x times
+#
+# --name [name]:
+# greet user by name, if name not supplied default is John
+#
+# DIR: The directory in which to issue the greeting.
+# EOF
+# when '--repeat'
+# repetitions = arg.to_i
+# when '--name'
+# if arg == ''
+# name = 'John'
+# else
+# name = arg
+# end
+# end
+# end
+#
+# if ARGV.length != 1
+# puts "Missing dir argument (try --help)"
+# exit 0
+# end
+#
+# dir = ARGV.shift
+#
+# Dir.chdir(dir)
+# for i in (1..repetitions)
+# print "Hello"
+# if name
+# print ", #{name}"
+# end
+# puts
+# end
+#
+# Example command line:
+#
+# hello -n 6 --name -- /tmp
+#
+class GetoptLong
+ #
+ # Orderings.
+ #
+ ORDERINGS = [REQUIRE_ORDER = 0, PERMUTE = 1, RETURN_IN_ORDER = 2]
+
+ #
+ # Argument flags.
+ #
+ ARGUMENT_FLAGS = [NO_ARGUMENT = 0, REQUIRED_ARGUMENT = 1,
+ OPTIONAL_ARGUMENT = 2]
+
+ #
+ # Status codes.
+ #
+ STATUS_YET, STATUS_STARTED, STATUS_TERMINATED = 0, 1, 2
+
+ #
+ # Error types.
+ #
+ class Error < StandardError; end
+ class AmbiguousOption < Error; end
+ class NeedlessArgument < Error; end
+ class MissingArgument < Error; end
+ class InvalidOption < Error; end
+
+ #
+ # Set up option processing.
+ #
+ # The options to support are passed to new() as an array of arrays.
+ # Each sub-array contains any number of String option names which carry
+ # the same meaning, and one of the following flags:
+ #
+ # GetoptLong::NO_ARGUMENT :: Option does not take an argument.
+ #
+ # GetoptLong::REQUIRED_ARGUMENT :: Option always takes an argument.
+ #
+ # GetoptLong::OPTIONAL_ARGUMENT :: Option may or may not take an argument.
+ #
+ # The first option name is considered to be the preferred (canonical) name.
+ # Other than that, the elements of each sub-array can be in any order.
+ #
+ def initialize(*arguments)
+ #
+ # Current ordering.
+ #
+ if ENV.include?('POSIXLY_CORRECT')
+ @ordering = REQUIRE_ORDER
+ else
+ @ordering = PERMUTE
+ end
+
+ #
+ # Hash table of option names.
+ # Keys of the table are option names, and their values are canonical
+ # names of the options.
+ #
+ @canonical_names = Hash.new
+
+ #
+ # Hash table of argument flags.
+ # Keys of the table are option names, and their values are argument
+ # flags of the options.
+ #
+ @argument_flags = Hash.new
+
+ #
+ # Whether error messages are output to $stderr.
+ #
+ @quiet = FALSE
+
+ #
+ # Status code.
+ #
+ @status = STATUS_YET
+
+ #
+ # Error code.
+ #
+ @error = nil
+
+ #
+ # Error message.
+ #
+ @error_message = nil
+
+ #
+ # Rest of catenated short options.
+ #
+ @rest_singles = ''
+
+ #
+ # List of non-option-arguments.
+ # Append them to ARGV when option processing is terminated.
+ #
+ @non_option_arguments = Array.new
+
+ if 0 < arguments.length
+ set_options(*arguments)
+ end
+ end
+
+ #
+ # Set the handling of the ordering of options and arguments.
+ # A RuntimeError is raised if option processing has already started.
+ #
+ # The supplied value must be a member of GetoptLong::ORDERINGS. It alters
+ # the processing of options as follows:
+ #
+ # <b>REQUIRE_ORDER</b> :
+ #
+ # Options are required to occur before non-options.
+ #
+ # Processing of options ends as soon as a word is encountered that has not
+ # been preceded by an appropriate option flag.
+ #
+ # For example, if -a and -b are options which do not take arguments,
+ # parsing command line arguments of '-a one -b two' would result in
+ # 'one', '-b', 'two' being left in ARGV, and only ('-a', '') being
+ # processed as an option/arg pair.
+ #
+ # This is the default ordering, if the environment variable
+ # POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
+ #
+ # <b>PERMUTE</b> :
+ #
+ # Options can occur anywhere in the command line parsed. This is the
+ # default behavior.
+ #
+ # Every sequence of words which can be interpreted as an option (with or
+ # without argument) is treated as an option; non-option words are skipped.
+ #
+ # For example, if -a does not require an argument and -b optionally takes
+ # an argument, parsing '-a one -b two three' would result in ('-a','') and
+ # ('-b', 'two') being processed as option/arg pairs, and 'one','three'
+ # being left in ARGV.
+ #
+ # If the ordering is set to PERMUTE but the environment variable
+ # POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for
+ # compatibility with GNU getopt_long.
+ #
+ # <b>RETURN_IN_ORDER</b> :
+ #
+ # All words on the command line are processed as options. Words not
+ # preceded by a short or long option flag are passed as arguments
+ # with an option of '' (empty string).
+ #
+ # For example, if -a requires an argument but -b does not, a command line
+ # of '-a one -b two three' would result in option/arg pairs of ('-a', 'one')
+ # ('-b', ''), ('', 'two'), ('', 'three') being processed.
+ #
+ def ordering=(ordering)
+ #
+ # The method is failed if option processing has already started.
+ #
+ if @status != STATUS_YET
+ set_error(ArgumentError, "argument error")
+ raise RuntimeError,
+ "invoke ordering=, but option processing has already started"
+ end
+
+ #
+ # Check ordering.
+ #
+ if !ORDERINGS.include?(ordering)
+ raise ArgumentError, "invalid ordering `#{ordering}'"
+ end
+ if ordering == PERMUTE && ENV.include?('POSIXLY_CORRECT')
+ @ordering = REQUIRE_ORDER
+ else
+ @ordering = ordering
+ end
+ end
+
+ #
+ # Return ordering.
+ #
+ attr_reader :ordering
+
+ #
+ # Set options. Takes the same argument as GetoptLong.new.
+ #
+ # Raises a RuntimeError if option processing has already started.
+ #
+ def set_options(*arguments)
+ #
+ # The method is failed if option processing has already started.
+ #
+ if @status != STATUS_YET
+ raise RuntimeError,
+ "invoke set_options, but option processing has already started"
+ end
+
+ #
+ # Clear tables of option names and argument flags.
+ #
+ @canonical_names.clear
+ @argument_flags.clear
+
+ arguments.each do |*arg|
+ arg = arg.first # TODO: YARV Hack
+ #
+ # Find an argument flag and it set to `argument_flag'.
+ #
+ argument_flag = nil
+ arg.each do |i|
+ if ARGUMENT_FLAGS.include?(i)
+ if argument_flag != nil
+ raise ArgumentError, "too many argument-flags"
+ end
+ argument_flag = i
+ end
+ end
+
+ raise ArgumentError, "no argument-flag" if argument_flag == nil
+
+ canonical_name = nil
+ arg.each do |i|
+ #
+ # Check an option name.
+ #
+ next if i == argument_flag
+ begin
+ if !i.is_a?(String) || i !~ /^-([^-]|-.+)$/
+ raise ArgumentError, "an invalid option `#{i}'"
+ end
+ if (@canonical_names.include?(i))
+ raise ArgumentError, "option redefined `#{i}'"
+ end
+ rescue
+ @canonical_names.clear
+ @argument_flags.clear
+ raise
+ end
+
+ #
+ # Register the option (`i') to the `@canonical_names' and
+ # `@canonical_names' Hashes.
+ #
+ if canonical_name == nil
+ canonical_name = i
+ end
+ @canonical_names[i] = canonical_name
+ @argument_flags[i] = argument_flag
+ end
+ raise ArgumentError, "no option name" if canonical_name == nil
+ end
+ return self
+ end
+
+ #
+ # Set/Unset `quiet' mode.
+ #
+ attr_writer :quiet
+
+ #
+ # Return the flag of `quiet' mode.
+ #
+ attr_reader :quiet
+
+ #
+ # `quiet?' is an alias of `quiet'.
+ #
+ alias quiet? quiet
+
+ #
+ # Explicitly terminate option processing.
+ #
+ def terminate
+ return nil if @status == STATUS_TERMINATED
+ raise RuntimeError, "an error has occured" if @error != nil
+
+ @status = STATUS_TERMINATED
+ @non_option_arguments.reverse_each do |argument|
+ ARGV.unshift(argument)
+ end
+
+ @canonical_names = nil
+ @argument_flags = nil
+ @rest_singles = nil
+ @non_option_arguments = nil
+
+ return self
+ end
+
+ #
+ # Returns true if option processing has terminated, false otherwise.
+ #
+ def terminated?
+ return @status == STATUS_TERMINATED
+ end
+
+ #
+ # Set an error (a protected method).
+ #
+ def set_error(type, message)
+ $stderr.print("#{$0}: #{message}\n") if !@quiet
+
+ @error = type
+ @error_message = message
+ @canonical_names = nil
+ @argument_flags = nil
+ @rest_singles = nil
+ @non_option_arguments = nil
+
+ raise type, message
+ end
+ protected :set_error
+
+ #
+ # Examine whether an option processing is failed.
+ #
+ attr_reader :error
+
+ #
+ # `error?' is an alias of `error'.
+ #
+ alias error? error
+
+ # Return the appropriate error message in POSIX-defined format.
+ # If no error has occurred, returns nil.
+ #
+ def error_message
+ return @error_message
+ end
+
+ #
+ # Get next option name and its argument, as an Array of two elements.
+ #
+ # The option name is always converted to the first (preferred)
+ # name given in the original options to GetoptLong.new.
+ #
+ # Example: ['--option', 'value']
+ #
+ # Returns nil if the processing is complete (as determined by
+ # STATUS_TERMINATED).
+ #
+ def get
+ option_name, option_argument = nil, ''
+
+ #
+ # Check status.
+ #
+ return nil if @error != nil
+ case @status
+ when STATUS_YET
+ @status = STATUS_STARTED
+ when STATUS_TERMINATED
+ return nil
+ end
+
+ #
+ # Get next option argument.
+ #
+ if 0 < @rest_singles.length
+ argument = '-' + @rest_singles
+ elsif (ARGV.length == 0)
+ terminate
+ return nil
+ elsif @ordering == PERMUTE
+ while 0 < ARGV.length && ARGV[0] !~ /^-./
+ @non_option_arguments.push(ARGV.shift)
+ end
+ if ARGV.length == 0
+ terminate
+ return nil
+ end
+ argument = ARGV.shift
+ elsif @ordering == REQUIRE_ORDER
+ if (ARGV[0] !~ /^-./)
+ terminate
+ return nil
+ end
+ argument = ARGV.shift
+ else
+ argument = ARGV.shift
+ end
+
+ #
+ # Check the special argument `--'.
+ # `--' indicates the end of the option list.
+ #
+ if argument == '--' && @rest_singles.length == 0
+ terminate
+ return nil
+ end
+
+ #
+ # Check for long and short options.
+ #
+ if argument =~ /^(--[^=]+)/ && @rest_singles.length == 0
+ #
+ # This is a long style option, which start with `--'.
+ #
+ pattern = $1
+ if @canonical_names.include?(pattern)
+ option_name = pattern
+ else
+ #
+ # The option `option_name' is not registered in `@canonical_names'.
+ # It may be an abbreviated.
+ #
+ matches = []
+ @canonical_names.each_key do |key|
+ if key.index(pattern) == 0
+ option_name = key
+ matches << key
+ end
+ end
+ if 2 <= matches.length
+ set_error(AmbiguousOption, "option `#{argument}' is ambiguous between #{matches.join(', ')}")
+ elsif matches.length == 0
+ set_error(InvalidOption, "unrecognized option `#{argument}'")
+ end
+ end
+
+ #
+ # Check an argument to the option.
+ #
+ if @argument_flags[option_name] == REQUIRED_ARGUMENT
+ if argument =~ /=(.*)$/
+ option_argument = $1
+ elsif 0 < ARGV.length
+ option_argument = ARGV.shift
+ else
+ set_error(MissingArgument,
+ "option `#{argument}' requires an argument")
+ end
+ elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
+ if argument =~ /=(.*)$/
+ option_argument = $1
+ elsif 0 < ARGV.length && ARGV[0] !~ /^-./
+ option_argument = ARGV.shift
+ else
+ option_argument = ''
+ end
+ elsif argument =~ /=(.*)$/
+ set_error(NeedlessArgument,
+ "option `#{option_name}' doesn't allow an argument")
+ end
+
+ elsif argument =~ /^(-(.))(.*)/
+ #
+ # This is a short style option, which start with `-' (not `--').
+ # Short options may be catenated (e.g. `-l -g' is equivalent to
+ # `-lg').
+ #
+ option_name, ch, @rest_singles = $1, $2, $3
+
+ if @canonical_names.include?(option_name)
+ #
+ # The option `option_name' is found in `@canonical_names'.
+ # Check its argument.
+ #
+ if @argument_flags[option_name] == REQUIRED_ARGUMENT
+ if 0 < @rest_singles.length
+ option_argument = @rest_singles
+ @rest_singles = ''
+ elsif 0 < ARGV.length
+ option_argument = ARGV.shift
+ else
+ # 1003.2 specifies the format of this message.
+ set_error(MissingArgument, "option requires an argument -- #{ch}")
+ end
+ elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
+ if 0 < @rest_singles.length
+ option_argument = @rest_singles
+ @rest_singles = ''
+ elsif 0 < ARGV.length && ARGV[0] !~ /^-./
+ option_argument = ARGV.shift
+ else
+ option_argument = ''
+ end
+ end
+ else
+ #
+ # This is an invalid option.
+ # 1003.2 specifies the format of this message.
+ #
+ if ENV.include?('POSIXLY_CORRECT')
+ set_error(InvalidOption, "invalid option -- #{ch}")
+ else
+ set_error(InvalidOption, "invalid option -- #{ch}")
+ end
+ end
+ else
+ #
+ # This is a non-option argument.
+ # Only RETURN_IN_ORDER falled into here.
+ #
+ return '', argument
+ end
+
+ return @canonical_names[option_name], option_argument
+ end
+
+ #
+ # `get_option' is an alias of `get'.
+ #
+ alias get_option get
+
+ # Iterator version of `get'.
+ #
+ # The block is called repeatedly with two arguments:
+ # The first is the option name.
+ # The second is the argument which followed it (if any).
+ # Example: ('--opt', 'value')
+ #
+ # The option name is always converted to the first (preferred)
+ # name given in the original options to GetoptLong.new.
+ #
+ def each
+ loop do
+ option_name, option_argument = get_option
+ break if option_name == nil
+ yield option_name, option_argument
+ end
+ end
+
+ #
+ # `each_option' is an alias of `each'.
+ #
+ alias each_option each
+end
diff --git a/ruby/lib/gserver.rb b/ruby/lib/gserver.rb
new file mode 100644
index 0000000..592e866
--- /dev/null
+++ b/ruby/lib/gserver.rb
@@ -0,0 +1,253 @@
+#
+# Copyright (C) 2001 John W. Small All Rights Reserved
+#
+# Author:: John W. Small
+# Documentation:: Gavin Sinclair
+# Licence:: Freeware.
+#
+# See the class GServer for documentation.
+#
+
+require "socket"
+require "thread"
+
+#
+# GServer implements a generic server, featuring thread pool management,
+# simple logging, and multi-server management. See HttpServer in
+# <tt>xmlrpc/httpserver.rb</tt> in the Ruby standard library for an example of
+# GServer in action.
+#
+# Any kind of application-level server can be implemented using this class.
+# It accepts multiple simultaneous connections from clients, up to an optional
+# maximum number. Several _services_ (i.e. one service per TCP port) can be
+# run simultaneously, and stopped at any time through the class method
+# <tt>GServer.stop(port)</tt>. All the threading issues are handled, saving
+# you the effort. All events are optionally logged, but you can provide your
+# own event handlers if you wish.
+#
+# === Example
+#
+# Using GServer is simple. Below we implement a simple time server, run it,
+# query it, and shut it down. Try this code in +irb+:
+#
+# require 'gserver'
+#
+# #
+# # A server that returns the time in seconds since 1970.
+# #
+# class TimeServer < GServer
+# def initialize(port=10001, *args)
+# super(port, *args)
+# end
+# def serve(io)
+# io.puts(Time.now.to_s)
+# end
+# end
+#
+# # Run the server with logging enabled (it's a separate thread).
+# server = TimeServer.new
+# server.audit = true # Turn logging on.
+# server.start
+#
+# # *** Now point your browser to http://localhost:10001 to see it working ***
+#
+# # See if it's still running.
+# GServer.in_service?(10001) # -> true
+# server.stopped? # -> false
+#
+# # Shut the server down gracefully.
+# server.shutdown
+#
+# # Alternatively, stop it immediately.
+# GServer.stop(10001)
+# # or, of course, "server.stop".
+#
+# All the business of accepting connections and exception handling is taken
+# care of. All we have to do is implement the method that actually serves the
+# client.
+#
+# === Advanced
+#
+# As the example above shows, the way to use GServer is to subclass it to
+# create a specific server, overriding the +serve+ method. You can override
+# other methods as well if you wish, perhaps to collect statistics, or emit
+# more detailed logging.
+#
+# connecting
+# disconnecting
+# starting
+# stopping
+#
+# The above methods are only called if auditing is enabled.
+#
+# You can also override +log+ and +error+ if, for example, you wish to use a
+# more sophisticated logging system.
+#
+class GServer
+
+ DEFAULT_HOST = "127.0.0.1"
+
+ def serve(io)
+ end
+
+ @@services = {} # Hash of opened ports, i.e. services
+ @@servicesMutex = Mutex.new
+
+ def GServer.stop(port, host = DEFAULT_HOST)
+ @@servicesMutex.synchronize {
+ @@services[host][port].stop
+ }
+ end
+
+ def GServer.in_service?(port, host = DEFAULT_HOST)
+ @@services.has_key?(host) and
+ @@services[host].has_key?(port)
+ end
+
+ def stop
+ @connectionsMutex.synchronize {
+ if @tcpServerThread
+ @tcpServerThread.raise "stop"
+ end
+ }
+ end
+
+ def stopped?
+ @tcpServerThread == nil
+ end
+
+ def shutdown
+ @shutdown = true
+ end
+
+ def connections
+ @connections.size
+ end
+
+ def join
+ @tcpServerThread.join if @tcpServerThread
+ end
+
+ attr_reader :port, :host, :maxConnections
+ attr_accessor :stdlog, :audit, :debug
+
+ def connecting(client)
+ addr = client.peeraddr
+ log("#{self.class.to_s} #{@host}:#{@port} client:#{addr[1]} " +
+ "#{addr[2]}<#{addr[3]}> connect")
+ true
+ end
+
+ def disconnecting(clientPort)
+ log("#{self.class.to_s} #{@host}:#{@port} " +
+ "client:#{clientPort} disconnect")
+ end
+
+ protected :connecting, :disconnecting
+
+ def starting()
+ log("#{self.class.to_s} #{@host}:#{@port} start")
+ end
+
+ def stopping()
+ log("#{self.class.to_s} #{@host}:#{@port} stop")
+ end
+
+ protected :starting, :stopping
+
+ def error(detail)
+ log(detail.backtrace.join("\n"))
+ end
+
+ def log(msg)
+ if @stdlog
+ @stdlog.puts("[#{Time.new.ctime}] %s" % msg)
+ @stdlog.flush
+ end
+ end
+
+ protected :error, :log
+
+ def initialize(port, host = DEFAULT_HOST, maxConnections = 4,
+ stdlog = $stderr, audit = false, debug = false)
+ @tcpServerThread = nil
+ @port = port
+ @host = host
+ @maxConnections = maxConnections
+ @connections = []
+ @connectionsMutex = Mutex.new
+ @connectionsCV = ConditionVariable.new
+ @stdlog = stdlog
+ @audit = audit
+ @debug = debug
+ end
+
+ def start(maxConnections = -1)
+ raise "running" if !stopped?
+ @shutdown = false
+ @maxConnections = maxConnections if maxConnections > 0
+ @@servicesMutex.synchronize {
+ if GServer.in_service?(@port,@host)
+ raise "Port already in use: #{host}:#{@port}!"
+ end
+ @tcpServer = TCPServer.new(@host,@port)
+ @port = @tcpServer.addr[1]
+ @@services[@host] = {} unless @@services.has_key?(@host)
+ @@services[@host][@port] = self;
+ }
+ @tcpServerThread = Thread.new {
+ begin
+ starting if @audit
+ while !@shutdown
+ @connectionsMutex.synchronize {
+ while @connections.size >= @maxConnections
+ @connectionsCV.wait(@connectionsMutex)
+ end
+ }
+ client = @tcpServer.accept
+ @connections << Thread.new(client) { |myClient|
+ begin
+ myPort = myClient.peeraddr[1]
+ serve(myClient) if !@audit or connecting(myClient)
+ rescue => detail
+ error(detail) if @debug
+ ensure
+ begin
+ myClient.close
+ rescue
+ end
+ @connectionsMutex.synchronize {
+ @connections.delete(Thread.current)
+ @connectionsCV.signal
+ }
+ disconnecting(myPort) if @audit
+ end
+ }
+ end
+ rescue => detail
+ error(detail) if @debug
+ ensure
+ begin
+ @tcpServer.close
+ rescue
+ end
+ if @shutdown
+ @connectionsMutex.synchronize {
+ while @connections.size > 0
+ @connectionsCV.wait(@connectionsMutex)
+ end
+ }
+ else
+ @connections.each { |c| c.raise "stop" }
+ end
+ @tcpServerThread = nil
+ @@servicesMutex.synchronize {
+ @@services[@host].delete(@port)
+ }
+ stopping if @audit
+ end
+ }
+ self
+ end
+
+end
diff --git a/ruby/lib/i686-linux/bigdecimal.so b/ruby/lib/i686-linux/bigdecimal.so
new file mode 100644
index 0000000..5ad2d76
--- /dev/null
+++ b/ruby/lib/i686-linux/bigdecimal.so
Binary files differ
diff --git a/ruby/lib/i686-linux/binject.so b/ruby/lib/i686-linux/binject.so
new file mode 100644
index 0000000..7380d8c
--- /dev/null
+++ b/ruby/lib/i686-linux/binject.so
Binary files differ
diff --git a/ruby/lib/i686-linux/bloops.so b/ruby/lib/i686-linux/bloops.so
new file mode 100644
index 0000000..1b42ade
--- /dev/null
+++ b/ruby/lib/i686-linux/bloops.so
Binary files differ
diff --git a/ruby/lib/i686-linux/chipmunk.so b/ruby/lib/i686-linux/chipmunk.so
new file mode 100644
index 0000000..e073e90
--- /dev/null
+++ b/ruby/lib/i686-linux/chipmunk.so
Binary files differ
diff --git a/ruby/lib/i686-linux/continuation.so b/ruby/lib/i686-linux/continuation.so
new file mode 100644
index 0000000..c401f0a
--- /dev/null
+++ b/ruby/lib/i686-linux/continuation.so
Binary files differ
diff --git a/ruby/lib/i686-linux/coverage.so b/ruby/lib/i686-linux/coverage.so
new file mode 100644
index 0000000..3c4bb9e
--- /dev/null
+++ b/ruby/lib/i686-linux/coverage.so
Binary files differ
diff --git a/ruby/lib/i686-linux/digest.so b/ruby/lib/i686-linux/digest.so
new file mode 100644
index 0000000..f3e3a56
--- /dev/null
+++ b/ruby/lib/i686-linux/digest.so
Binary files differ
diff --git a/ruby/lib/i686-linux/digest/bubblebabble.so b/ruby/lib/i686-linux/digest/bubblebabble.so
new file mode 100644
index 0000000..758697d
--- /dev/null
+++ b/ruby/lib/i686-linux/digest/bubblebabble.so
Binary files differ
diff --git a/ruby/lib/i686-linux/digest/md5.so b/ruby/lib/i686-linux/digest/md5.so
new file mode 100644
index 0000000..819e3cc
--- /dev/null
+++ b/ruby/lib/i686-linux/digest/md5.so
Binary files differ
diff --git a/ruby/lib/i686-linux/digest/rmd160.so b/ruby/lib/i686-linux/digest/rmd160.so
new file mode 100644
index 0000000..6f52bf6
--- /dev/null
+++ b/ruby/lib/i686-linux/digest/rmd160.so
Binary files differ
diff --git a/ruby/lib/i686-linux/digest/sha1.so b/ruby/lib/i686-linux/digest/sha1.so
new file mode 100644
index 0000000..14cae22
--- /dev/null
+++ b/ruby/lib/i686-linux/digest/sha1.so
Binary files differ
diff --git a/ruby/lib/i686-linux/digest/sha2.so b/ruby/lib/i686-linux/digest/sha2.so
new file mode 100644
index 0000000..ccd1d8b
--- /dev/null
+++ b/ruby/lib/i686-linux/digest/sha2.so
Binary files differ
diff --git a/ruby/lib/i686-linux/dl.so b/ruby/lib/i686-linux/dl.so
new file mode 100644
index 0000000..fc96314
--- /dev/null
+++ b/ruby/lib/i686-linux/dl.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/big5.so b/ruby/lib/i686-linux/enc/big5.so
new file mode 100644
index 0000000..76a85da
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/big5.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/cp949.so b/ruby/lib/i686-linux/enc/cp949.so
new file mode 100644
index 0000000..911e7bb
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/cp949.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/emacs_mule.so b/ruby/lib/i686-linux/enc/emacs_mule.so
new file mode 100644
index 0000000..a8e5438
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/emacs_mule.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/encdb.so b/ruby/lib/i686-linux/enc/encdb.so
new file mode 100644
index 0000000..04addb2
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/encdb.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/euc_jp.so b/ruby/lib/i686-linux/enc/euc_jp.so
new file mode 100644
index 0000000..78c4601
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/euc_jp.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/euc_kr.so b/ruby/lib/i686-linux/enc/euc_kr.so
new file mode 100644
index 0000000..a8c47d5
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/euc_kr.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/euc_tw.so b/ruby/lib/i686-linux/enc/euc_tw.so
new file mode 100644
index 0000000..cbdbadb
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/euc_tw.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/gb18030.so b/ruby/lib/i686-linux/enc/gb18030.so
new file mode 100644
index 0000000..101293c
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/gb18030.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/gb2312.so b/ruby/lib/i686-linux/enc/gb2312.so
new file mode 100644
index 0000000..aeabeb9
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/gb2312.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/gbk.so b/ruby/lib/i686-linux/enc/gbk.so
new file mode 100644
index 0000000..5e8aeaf
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/gbk.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_1.so b/ruby/lib/i686-linux/enc/iso_8859_1.so
new file mode 100644
index 0000000..89e2363
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_1.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_10.so b/ruby/lib/i686-linux/enc/iso_8859_10.so
new file mode 100644
index 0000000..950f16e
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_10.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_11.so b/ruby/lib/i686-linux/enc/iso_8859_11.so
new file mode 100644
index 0000000..e6b8fec
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_11.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_13.so b/ruby/lib/i686-linux/enc/iso_8859_13.so
new file mode 100644
index 0000000..338c4ca
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_13.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_14.so b/ruby/lib/i686-linux/enc/iso_8859_14.so
new file mode 100644
index 0000000..ad556fe
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_14.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_15.so b/ruby/lib/i686-linux/enc/iso_8859_15.so
new file mode 100644
index 0000000..1ea0755
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_15.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_16.so b/ruby/lib/i686-linux/enc/iso_8859_16.so
new file mode 100644
index 0000000..2934199
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_16.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_2.so b/ruby/lib/i686-linux/enc/iso_8859_2.so
new file mode 100644
index 0000000..d2ec64a
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_2.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_3.so b/ruby/lib/i686-linux/enc/iso_8859_3.so
new file mode 100644
index 0000000..7af3f9e
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_3.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_4.so b/ruby/lib/i686-linux/enc/iso_8859_4.so
new file mode 100644
index 0000000..4921399
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_4.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_5.so b/ruby/lib/i686-linux/enc/iso_8859_5.so
new file mode 100644
index 0000000..65b792b
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_5.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_6.so b/ruby/lib/i686-linux/enc/iso_8859_6.so
new file mode 100644
index 0000000..05ba45b
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_6.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_7.so b/ruby/lib/i686-linux/enc/iso_8859_7.so
new file mode 100644
index 0000000..68a1aa9
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_7.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_8.so b/ruby/lib/i686-linux/enc/iso_8859_8.so
new file mode 100644
index 0000000..4d740c5
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_8.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/iso_8859_9.so b/ruby/lib/i686-linux/enc/iso_8859_9.so
new file mode 100644
index 0000000..f2c17bd
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/iso_8859_9.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/koi8_r.so b/ruby/lib/i686-linux/enc/koi8_r.so
new file mode 100644
index 0000000..54124d6
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/koi8_r.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/koi8_u.so b/ruby/lib/i686-linux/enc/koi8_u.so
new file mode 100644
index 0000000..9771f61
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/koi8_u.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/shift_jis.so b/ruby/lib/i686-linux/enc/shift_jis.so
new file mode 100644
index 0000000..d727e2b
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/shift_jis.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/big5.so b/ruby/lib/i686-linux/enc/trans/big5.so
new file mode 100644
index 0000000..e121575
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/big5.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/chinese.so b/ruby/lib/i686-linux/enc/trans/chinese.so
new file mode 100644
index 0000000..065c9a1
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/chinese.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/escape.so b/ruby/lib/i686-linux/enc/trans/escape.so
new file mode 100644
index 0000000..edd0335
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/escape.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/gb18030.so b/ruby/lib/i686-linux/enc/trans/gb18030.so
new file mode 100644
index 0000000..6c1d40e
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/gb18030.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/gbk.so b/ruby/lib/i686-linux/enc/trans/gbk.so
new file mode 100644
index 0000000..7cacace
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/gbk.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/iso2022.so b/ruby/lib/i686-linux/enc/trans/iso2022.so
new file mode 100644
index 0000000..aa812e9
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/iso2022.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/japanese.so b/ruby/lib/i686-linux/enc/trans/japanese.so
new file mode 100644
index 0000000..af469cd
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/japanese.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/japanese_euc.so b/ruby/lib/i686-linux/enc/trans/japanese_euc.so
new file mode 100644
index 0000000..ea61b48
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/japanese_euc.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/japanese_sjis.so b/ruby/lib/i686-linux/enc/trans/japanese_sjis.so
new file mode 100644
index 0000000..a962634
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/japanese_sjis.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/korean.so b/ruby/lib/i686-linux/enc/trans/korean.so
new file mode 100644
index 0000000..2e35c21
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/korean.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/single_byte.so b/ruby/lib/i686-linux/enc/trans/single_byte.so
new file mode 100644
index 0000000..925c445
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/single_byte.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/transdb.so b/ruby/lib/i686-linux/enc/trans/transdb.so
new file mode 100644
index 0000000..ba8b6ff
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/transdb.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/trans/utf_16_32.so b/ruby/lib/i686-linux/enc/trans/utf_16_32.so
new file mode 100644
index 0000000..86e065a
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/trans/utf_16_32.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/utf_16be.so b/ruby/lib/i686-linux/enc/utf_16be.so
new file mode 100644
index 0000000..face3cf
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/utf_16be.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/utf_16le.so b/ruby/lib/i686-linux/enc/utf_16le.so
new file mode 100644
index 0000000..90d4b28
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/utf_16le.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/utf_32be.so b/ruby/lib/i686-linux/enc/utf_32be.so
new file mode 100644
index 0000000..527d178
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/utf_32be.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/utf_32le.so b/ruby/lib/i686-linux/enc/utf_32le.so
new file mode 100644
index 0000000..0237ae8
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/utf_32le.so
Binary files differ
diff --git a/ruby/lib/i686-linux/enc/windows_1251.so b/ruby/lib/i686-linux/enc/windows_1251.so
new file mode 100644
index 0000000..9bfb011
--- /dev/null
+++ b/ruby/lib/i686-linux/enc/windows_1251.so
Binary files differ
diff --git a/ruby/lib/i686-linux/etc.so b/ruby/lib/i686-linux/etc.so
new file mode 100644
index 0000000..a715848
--- /dev/null
+++ b/ruby/lib/i686-linux/etc.so
Binary files differ
diff --git a/ruby/lib/i686-linux/fcntl.so b/ruby/lib/i686-linux/fcntl.so
new file mode 100644
index 0000000..1a5ae4a
--- /dev/null
+++ b/ruby/lib/i686-linux/fcntl.so
Binary files differ
diff --git a/ruby/lib/i686-linux/fiber.so b/ruby/lib/i686-linux/fiber.so
new file mode 100644
index 0000000..ffd73a4
--- /dev/null
+++ b/ruby/lib/i686-linux/fiber.so
Binary files differ
diff --git a/ruby/lib/i686-linux/ftsearchrt.so b/ruby/lib/i686-linux/ftsearchrt.so
new file mode 100644
index 0000000..1484042
--- /dev/null
+++ b/ruby/lib/i686-linux/ftsearchrt.so
Binary files differ
diff --git a/ruby/lib/i686-linux/iconv.so b/ruby/lib/i686-linux/iconv.so
new file mode 100644
index 0000000..695ac56
--- /dev/null
+++ b/ruby/lib/i686-linux/iconv.so
Binary files differ
diff --git a/ruby/lib/i686-linux/io/wait.so b/ruby/lib/i686-linux/io/wait.so
new file mode 100644
index 0000000..66f9d21
--- /dev/null
+++ b/ruby/lib/i686-linux/io/wait.so
Binary files differ
diff --git a/ruby/lib/i686-linux/json/ext/generator.so b/ruby/lib/i686-linux/json/ext/generator.so
new file mode 100644
index 0000000..4d0b28f
--- /dev/null
+++ b/ruby/lib/i686-linux/json/ext/generator.so
Binary files differ
diff --git a/ruby/lib/i686-linux/json/ext/parser.so b/ruby/lib/i686-linux/json/ext/parser.so
new file mode 100644
index 0000000..806141a
--- /dev/null
+++ b/ruby/lib/i686-linux/json/ext/parser.so
Binary files differ
diff --git a/ruby/lib/i686-linux/mathn/complex.so b/ruby/lib/i686-linux/mathn/complex.so
new file mode 100644
index 0000000..742ce0e
--- /dev/null
+++ b/ruby/lib/i686-linux/mathn/complex.so
Binary files differ
diff --git a/ruby/lib/i686-linux/mathn/rational.so b/ruby/lib/i686-linux/mathn/rational.so
new file mode 100644
index 0000000..b180f30
--- /dev/null
+++ b/ruby/lib/i686-linux/mathn/rational.so
Binary files differ
diff --git a/ruby/lib/i686-linux/nkf.so b/ruby/lib/i686-linux/nkf.so
new file mode 100644
index 0000000..10081e7
--- /dev/null
+++ b/ruby/lib/i686-linux/nkf.so
Binary files differ
diff --git a/ruby/lib/i686-linux/pty.so b/ruby/lib/i686-linux/pty.so
new file mode 100644
index 0000000..5c73bd7
--- /dev/null
+++ b/ruby/lib/i686-linux/pty.so
Binary files differ
diff --git a/ruby/lib/i686-linux/racc/cparse.so b/ruby/lib/i686-linux/racc/cparse.so
new file mode 100644
index 0000000..5691d54
--- /dev/null
+++ b/ruby/lib/i686-linux/racc/cparse.so
Binary files differ
diff --git a/ruby/lib/i686-linux/rbconfig.rb b/ruby/lib/i686-linux/rbconfig.rb
new file mode 100644
index 0000000..46b9095
--- /dev/null
+++ b/ruby/lib/i686-linux/rbconfig.rb
@@ -0,0 +1,196 @@
+
+# This file was created by mkconfig.rb when ruby was built. Any
+# changes made to this file will be lost the next time ruby is built.
+
+module RbConfig
+ RUBY_VERSION == "1.9.1" or
+ raise "ruby lib version (1.9.1) doesn't match executable version (#{RUBY_VERSION})"
+
+ TOPDIR = File.dirname(__FILE__).chomp!("/lib/ruby/1.9.1/i686-linux")
+ DESTDIR = '' unless defined? DESTDIR
+ CONFIG = {}
+ CONFIG["DESTDIR"] = DESTDIR
+ CONFIG["INSTALL"] = '/usr/bin/install -c'
+ CONFIG["EXEEXT"] = ""
+ CONFIG["prefix"] = (TOPDIR || DESTDIR + "/home/cjb/.rvm/rubies/ruby-1.9.1-p378")
+ CONFIG["ruby_install_name"] = "ruby"
+ CONFIG["RUBY_INSTALL_NAME"] = "ruby"
+ CONFIG["RUBY_SO_NAME"] = "ruby"
+ CONFIG["BUILTIN_TRANSSRCS"] = " newline.c"
+ CONFIG["MANTYPE"] = "doc"
+ CONFIG["NROFF"] = "/usr/bin/nroff"
+ CONFIG["vendorhdrdir"] = "$(rubyhdrdir)/vendor_ruby"
+ CONFIG["sitehdrdir"] = "$(rubyhdrdir)/site_ruby"
+ CONFIG["rubyhdrdir"] = "$(includedir)/$(RUBY_INSTALL_NAME)-$(ruby_version)"
+ CONFIG["configure_args"] = " '--prefix=/home/cjb/.rvm/rubies/ruby-1.9.1-p378' '--enable-shared'"
+ CONFIG["vendordir"] = "$(libdir)/$(RUBY_INSTALL_NAME)/vendor_ruby"
+ CONFIG["sitedir"] = "$(libdir)/$(RUBY_INSTALL_NAME)/site_ruby"
+ CONFIG["ruby_version"] = "1.9.1"
+ CONFIG["sitearch"] = "i686-linux"
+ CONFIG["arch"] = "i686-linux"
+ CONFIG["MAKEFILES"] = "Makefile"
+ CONFIG["THREAD_MODEL"] = "pthread"
+ CONFIG["EXPORT_PREFIX"] = ""
+ CONFIG["COMMON_HEADERS"] = ""
+ CONFIG["COMMON_MACROS"] = ""
+ CONFIG["COMMON_LIBS"] = ""
+ CONFIG["MAINLIBS"] = ""
+ CONFIG["ENABLE_SHARED"] = "yes"
+ CONFIG["DLDLIBS"] = " -lc"
+ CONFIG["SOLIBS"] = "$(LIBS)"
+ CONFIG["LIBRUBYARG_SHARED"] = "-Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)"
+ CONFIG["LIBRUBYARG_STATIC"] = "-Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static"
+ CONFIG["LIBRUBYARG"] = "$(LIBRUBYARG_SHARED)"
+ CONFIG["LIBRUBY"] = "$(LIBRUBY_SO)"
+ CONFIG["LIBRUBY_ALIASES"] = "lib$(RUBY_SO_NAME).so.$(MAJOR).$(MINOR) lib$(RUBY_SO_NAME).so"
+ CONFIG["LIBRUBY_SO"] = "lib$(RUBY_SO_NAME).so.$(MAJOR).$(MINOR).$(TEENY)"
+ CONFIG["LIBRUBY_A"] = "lib$(RUBY_SO_NAME)-static.a"
+ CONFIG["RUBYW_INSTALL_NAME"] = ""
+ CONFIG["rubyw_install_name"] = ""
+ CONFIG["LIBRUBY_DLDFLAGS"] = "-Wl,-soname,lib$(RUBY_SO_NAME).so.$(MAJOR).$(MINOR)"
+ CONFIG["LIBRUBY_LDSHARED"] = "$(CC) -shared"
+ CONFIG["warnflags"] = "-Wall -Wno-parentheses"
+ CONFIG["debugflags"] = "-g"
+ CONFIG["optflags"] = "-O2"
+ CONFIG["cflags"] = "$(optflags) $(debugflags) $(warnflags)"
+ CONFIG["cppflags"] = ""
+ CONFIG["RDOCTARGET"] = "install-doc"
+ CONFIG["ARCHFILE"] = ""
+ CONFIG["EXTOUT"] = ".ext"
+ CONFIG["PREP"] = "miniruby$(EXEEXT)"
+ CONFIG["setup"] = "Setup"
+ CONFIG["EXTSTATIC"] = ""
+ CONFIG["STRIP"] = "strip -S -x"
+ CONFIG["TRY_LINK"] = ""
+ CONFIG["LIBPATHENV"] = "LD_LIBRARY_PATH"
+ CONFIG["RPATHFLAG"] = " -Wl,-R%1$-s"
+ CONFIG["LIBPATHFLAG"] = " -L%1$-s"
+ CONFIG["LINK_SO"] = ""
+ CONFIG["LIBEXT"] = "a"
+ CONFIG["DLEXT2"] = ""
+ CONFIG["DLEXT"] = "so"
+ CONFIG["LDSHAREDXX"] = "$(CXX) -shared"
+ CONFIG["LDSHARED"] = "$(CC) -shared"
+ CONFIG["CCDLFLAGS"] = " -fPIC"
+ CONFIG["STATIC"] = ""
+ CONFIG["ARCH_FLAG"] = ""
+ CONFIG["DLDFLAGS"] = ""
+ CONFIG["ALLOCA"] = ""
+ CONFIG["RMALL"] = "rm -fr"
+ CONFIG["RMDIRS"] = "$(top_srcdir)/tool/rmdirs"
+ CONFIG["MAKEDIRS"] = "mkdir -p"
+ CONFIG["CP"] = "cp"
+ CONFIG["RM"] = "rm -f"
+ CONFIG["INSTALL_DATA"] = "$(INSTALL) -m 644"
+ CONFIG["INSTALL_SCRIPT"] = "$(INSTALL)"
+ CONFIG["INSTALL_PROGRAM"] = "$(INSTALL)"
+ CONFIG["SET_MAKE"] = ""
+ CONFIG["LN_S"] = "ln -s"
+ CONFIG["DLLWRAP"] = ""
+ CONFIG["WINDRES"] = ""
+ CONFIG["NM"] = ""
+ CONFIG["OBJCOPY"] = "objcopy"
+ CONFIG["OBJDUMP"] = "objdump"
+ CONFIG["ASFLAGS"] = ""
+ CONFIG["AS"] = "as"
+ CONFIG["AR"] = "ar"
+ CONFIG["RANLIB"] = "ranlib"
+ CONFIG["COUTFLAG"] = "-o "
+ CONFIG["OUTFLAG"] = "-o "
+ CONFIG["CPPOUTFILE"] = "-o conftest.i"
+ CONFIG["GNU_LD"] = "yes"
+ CONFIG["EGREP"] = "/bin/grep -E"
+ CONFIG["GREP"] = "/bin/grep"
+ CONFIG["CPP"] = "gcc -E"
+ CONFIG["CXXFLAGS"] = " $(cxxflags)"
+ CONFIG["CXX"] = "g++"
+ CONFIG["OBJEXT"] = "o"
+ CONFIG["CPPFLAGS"] = " $(DEFS) $(cppflags)"
+ CONFIG["LDFLAGS"] = "-L. -rdynamic -Wl,-export-dynamic"
+ CONFIG["CFLAGS"] = " $(cflags) -fPIC"
+ CONFIG["CC"] = "gcc"
+ CONFIG["target_os"] = "linux"
+ CONFIG["target_vendor"] = "pc"
+ CONFIG["target_cpu"] = "i686"
+ CONFIG["target"] = "i686-pc-linux-gnu"
+ CONFIG["host_os"] = "linux-gnu"
+ CONFIG["host_vendor"] = "pc"
+ CONFIG["host_cpu"] = "i686"
+ CONFIG["host"] = "i686-pc-linux-gnu"
+ CONFIG["build_os"] = "linux-gnu"
+ CONFIG["build_vendor"] = "pc"
+ CONFIG["build_cpu"] = "i686"
+ CONFIG["build"] = "i686-pc-linux-gnu"
+ CONFIG["TEENY"] = "1"
+ CONFIG["MINOR"] = "9"
+ CONFIG["MAJOR"] = "1"
+ CONFIG["BASERUBY"] = "echo executable host ruby is required. use --with-baseruby option.; false"
+ CONFIG["target_alias"] = ""
+ CONFIG["host_alias"] = ""
+ CONFIG["build_alias"] = ""
+ CONFIG["LIBS"] = "-lpthread -lrt -ldl -lcrypt -lm "
+ CONFIG["ECHO_T"] = ""
+ CONFIG["ECHO_N"] = "-n"
+ CONFIG["ECHO_C"] = ""
+ CONFIG["DEFS"] = "-D_FILE_OFFSET_BITS=64"
+ CONFIG["mandir"] = "$(datarootdir)/man"
+ CONFIG["localedir"] = "$(datarootdir)/locale"
+ CONFIG["libdir"] = "$(exec_prefix)/lib"
+ CONFIG["psdir"] = "$(docdir)"
+ CONFIG["pdfdir"] = "$(docdir)"
+ CONFIG["dvidir"] = "$(docdir)"
+ CONFIG["htmldir"] = "$(docdir)"
+ CONFIG["infodir"] = "$(datarootdir)/info"
+ CONFIG["docdir"] = "$(datarootdir)/doc/$(PACKAGE)"
+ CONFIG["oldincludedir"] = "/usr/include"
+ CONFIG["includedir"] = "$(prefix)/include"
+ CONFIG["localstatedir"] = "$(prefix)/var"
+ CONFIG["sharedstatedir"] = "$(prefix)/com"
+ CONFIG["sysconfdir"] = "$(prefix)/etc"
+ CONFIG["datadir"] = "$(datarootdir)"
+ CONFIG["datarootdir"] = "$(prefix)/share"
+ CONFIG["libexecdir"] = "$(exec_prefix)/libexec"
+ CONFIG["sbindir"] = "$(exec_prefix)/sbin"
+ CONFIG["bindir"] = "$(exec_prefix)/bin"
+ CONFIG["exec_prefix"] = "$(prefix)"
+ CONFIG["PACKAGE_URL"] = ""
+ CONFIG["PACKAGE_BUGREPORT"] = ""
+ CONFIG["PACKAGE_STRING"] = ""
+ CONFIG["PACKAGE_VERSION"] = ""
+ CONFIG["PACKAGE_TARNAME"] = ""
+ CONFIG["PACKAGE_NAME"] = ""
+ CONFIG["PATH_SEPARATOR"] = ":"
+ CONFIG["SHELL"] = "/bin/sh"
+ CONFIG["rubylibdir"] = "$(libdir)/$(ruby_install_name)/$(ruby_version)"
+ CONFIG["archdir"] = "$(rubylibdir)/$(arch)"
+ CONFIG["sitelibdir"] = "$(sitedir)/$(ruby_version)"
+ CONFIG["sitearchdir"] = "$(sitelibdir)/$(sitearch)"
+ CONFIG["vendorlibdir"] = "$(vendordir)/$(ruby_version)"
+ CONFIG["vendorarchdir"] = "$(vendorlibdir)/$(sitearch)"
+ CONFIG["topdir"] = File.dirname(__FILE__)
+ MAKEFILE_CONFIG = {}
+ CONFIG.each{|k,v| MAKEFILE_CONFIG[k] = v.dup}
+ def RbConfig::expand(val, config = CONFIG)
+ val.gsub!(/\$\$|\$\(([^()]+)\)|\$\{([^{}]+)\}/) do
+ var = $&
+ if !(v = $1 || $2)
+ '$'
+ elsif key = config[v = v[/\A[^:]+(?=(?::(.*?)=(.*))?\z)/]]
+ pat, sub = $1, $2
+ config[v] = false
+ RbConfig::expand(key, config)
+ config[v] = key
+ key = key.gsub(/#{Regexp.quote(pat)}(?=\s|\z)/n) {sub} if pat
+ key
+ else
+ var
+ end
+ end
+ val
+ end
+ CONFIG.each_value do |val|
+ RbConfig::expand(val)
+ end
+end
+Config = RbConfig # compatibility for ruby-1.8.4 and older.
+CROSS_COMPILING = nil unless defined? CROSS_COMPILING
diff --git a/ruby/lib/i686-linux/ripper.so b/ruby/lib/i686-linux/ripper.so
new file mode 100644
index 0000000..3c0d1e8
--- /dev/null
+++ b/ruby/lib/i686-linux/ripper.so
Binary files differ
diff --git a/ruby/lib/i686-linux/sdbm.so b/ruby/lib/i686-linux/sdbm.so
new file mode 100644
index 0000000..5e134f5
--- /dev/null
+++ b/ruby/lib/i686-linux/sdbm.so
Binary files differ
diff --git a/ruby/lib/i686-linux/socket.so b/ruby/lib/i686-linux/socket.so
new file mode 100644
index 0000000..f7eb3c6
--- /dev/null
+++ b/ruby/lib/i686-linux/socket.so
Binary files differ
diff --git a/ruby/lib/i686-linux/stringio.so b/ruby/lib/i686-linux/stringio.so
new file mode 100644
index 0000000..9afd9a7
--- /dev/null
+++ b/ruby/lib/i686-linux/stringio.so
Binary files differ
diff --git a/ruby/lib/i686-linux/strscan.so b/ruby/lib/i686-linux/strscan.so
new file mode 100644
index 0000000..0a5838f
--- /dev/null
+++ b/ruby/lib/i686-linux/strscan.so
Binary files differ
diff --git a/ruby/lib/i686-linux/syck.so b/ruby/lib/i686-linux/syck.so
new file mode 100644
index 0000000..9fac9c0
--- /dev/null
+++ b/ruby/lib/i686-linux/syck.so
Binary files differ
diff --git a/ruby/lib/i686-linux/syslog.so b/ruby/lib/i686-linux/syslog.so
new file mode 100644
index 0000000..15358b7
--- /dev/null
+++ b/ruby/lib/i686-linux/syslog.so
Binary files differ
diff --git a/ruby/lib/i686-linux/zlib.so b/ruby/lib/i686-linux/zlib.so
new file mode 100644
index 0000000..dc0954c
--- /dev/null
+++ b/ruby/lib/i686-linux/zlib.so
Binary files differ
diff --git a/ruby/lib/io/nonblock.rb b/ruby/lib/io/nonblock.rb
new file mode 100644
index 0000000..2103fdf
--- /dev/null
+++ b/ruby/lib/io/nonblock.rb
@@ -0,0 +1,23 @@
+require "fcntl"
+class IO
+ def nonblock?
+ (fcntl(Fcntl::F_GETFL) & File::NONBLOCK) != 0
+ end
+
+ def nonblock=(nb)
+ f = fcntl(Fcntl::F_GETFL)
+ if nb
+ f |= File::NONBLOCK
+ else
+ f &= ~File::NONBLOCK
+ end
+ fcntl(Fcntl::F_SETFL, f)
+ end
+
+ def nonblock(nb = true)
+ nb, self.nonblock = nonblock?, nb
+ yield
+ ensure
+ self.nonblock = nb
+ end
+end if defined?(Fcntl::F_GETFL)
diff --git a/ruby/lib/ipaddr.rb b/ruby/lib/ipaddr.rb
new file mode 100644
index 0000000..a62cc46
--- /dev/null
+++ b/ruby/lib/ipaddr.rb
@@ -0,0 +1,813 @@
+#
+# ipaddr.rb - A class to manipulate an IP address
+#
+# Copyright (c) 2002 Hajimu UMEMOTO <ume@mahoroba.org>.
+# Copyright (c) 2007 Akinori MUSHA <knu@iDaemons.org>.
+# All rights reserved.
+#
+# You can redistribute and/or modify it under the same terms as Ruby.
+#
+# $Id: ipaddr.rb 19504 2008-09-23 21:39:21Z ryan $
+#
+# Contact:
+# - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
+#
+# TODO:
+# - scope_id support
+#
+require 'socket'
+
+unless Socket.const_defined? "AF_INET6"
+ class Socket
+ AF_INET6 = Object.new
+ end
+
+ class << IPSocket
+ def valid_v4?(addr)
+ if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr
+ return $~.captures.all? {|i| i.to_i < 256}
+ end
+ return false
+ end
+
+ def valid_v6?(addr)
+ # IPv6 (normal)
+ return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr
+ return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
+ return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
+ # IPv6 (IPv4 compat)
+ return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_v4?($')
+ return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')
+ return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')
+
+ false
+ end
+
+ def valid?(addr)
+ valid_v4?(addr) || valid_v6?(addr)
+ end
+
+ alias getaddress_orig getaddress
+ def getaddress(s)
+ if valid?(s)
+ s
+ elsif /\A[-A-Za-z\d.]+\Z/ =~ s
+ getaddress_orig(s)
+ else
+ raise ArgumentError, "invalid address"
+ end
+ end
+ end
+end
+
+# IPAddr provides a set of methods to manipulate an IP address. Both IPv4 and
+# IPv6 are supported.
+#
+# == Example
+#
+# require 'ipaddr'
+#
+# ipaddr1 = IPAddr.new "3ffe:505:2::1"
+#
+# p ipaddr1 #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
+#
+# p ipaddr1.to_s #=> "3ffe:505:2::1"
+#
+# ipaddr2 = ipaddr1.mask(48) #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>
+#
+# p ipaddr2.to_s #=> "3ffe:505:2::"
+#
+# ipaddr3 = IPAddr.new "192.168.2.0/24"
+#
+# p ipaddr3 #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
+
+class IPAddr
+
+ IN4MASK = 0xffffffff
+ IN6MASK = 0xffffffffffffffffffffffffffffffff
+ IN6FORMAT = (["%.4x"] * 8).join(':')
+
+ # Returns the address family of this IP address.
+ attr_reader :family
+
+ # Creates a new ipaddr containing the given network byte ordered
+ # string form of an IP address.
+ def IPAddr::new_ntoh(addr)
+ return IPAddr.new(IPAddr::ntop(addr))
+ end
+
+ # Convert a network byte ordered string form of an IP address into
+ # human readable form.
+ def IPAddr::ntop(addr)
+ case addr.size
+ when 4
+ s = addr.unpack('C4').join('.')
+ when 16
+ s = IN6FORMAT % addr.unpack('n8')
+ else
+ raise ArgumentError, "unsupported address family"
+ end
+ return s
+ end
+
+ # Returns a new ipaddr built by bitwise AND.
+ def &(other)
+ return self.clone.set(@addr & coerce_other(other).to_i)
+ end
+
+ # Returns a new ipaddr built by bitwise OR.
+ def |(other)
+ return self.clone.set(@addr | coerce_other(other).to_i)
+ end
+
+ # Returns a new ipaddr built by bitwise right-shift.
+ def >>(num)
+ return self.clone.set(@addr >> num)
+ end
+
+ # Returns a new ipaddr built by bitwise left shift.
+ def <<(num)
+ return self.clone.set(addr_mask(@addr << num))
+ end
+
+ # Returns a new ipaddr built by bitwise negation.
+ def ~
+ return self.clone.set(addr_mask(~@addr))
+ end
+
+ # Returns true if two ipaddrs are equal.
+ def ==(other)
+ other = coerce_other(other)
+ return @family == other.family && @addr == other.to_i
+ end
+
+ # Returns a new ipaddr built by masking IP address with the given
+ # prefixlen/netmask. (e.g. 8, 64, "255.255.255.0", etc.)
+ def mask(prefixlen)
+ return self.clone.mask!(prefixlen)
+ end
+
+ # Returns true if the given ipaddr is in the range.
+ #
+ # e.g.:
+ # require 'ipaddr'
+ # net1 = IPAddr.new("192.168.2.0/24")
+ # net2 = IPAddr.new("192.168.2.100")
+ # net3 = IPAddr.new("192.168.3.0")
+ # p net1.include?(net2) #=> true
+ # p net1.include?(net3) #=> false
+ def include?(other)
+ other = coerce_other(other)
+ if ipv4_mapped?
+ if (@mask_addr >> 32) != 0xffffffffffffffffffffffff
+ return false
+ end
+ mask_addr = (@mask_addr & IN4MASK)
+ addr = (@addr & IN4MASK)
+ family = Socket::AF_INET
+ else
+ mask_addr = @mask_addr
+ addr = @addr
+ family = @family
+ end
+ if other.ipv4_mapped?
+ other_addr = (other.to_i & IN4MASK)
+ other_family = Socket::AF_INET
+ else
+ other_addr = other.to_i
+ other_family = other.family
+ end
+
+ if family != other_family
+ return false
+ end
+ return ((addr & mask_addr) == (other_addr & mask_addr))
+ end
+ alias === include?
+
+ # Returns the integer representation of the ipaddr.
+ def to_i
+ return @addr
+ end
+
+ # Returns a string containing the IP address representation.
+ def to_s
+ str = to_string
+ return str if ipv4?
+
+ str.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1')
+ loop do
+ break if str.sub!(/\A0:0:0:0:0:0:0:0\Z/, '::')
+ break if str.sub!(/\b0:0:0:0:0:0:0\b/, ':')
+ break if str.sub!(/\b0:0:0:0:0:0\b/, ':')
+ break if str.sub!(/\b0:0:0:0:0\b/, ':')
+ break if str.sub!(/\b0:0:0:0\b/, ':')
+ break if str.sub!(/\b0:0:0\b/, ':')
+ break if str.sub!(/\b0:0\b/, ':')
+ break
+ end
+ str.sub!(/:{3,}/, '::')
+
+ if /\A::(ffff:)?([\da-f]{1,4}):([\da-f]{1,4})\Z/i =~ str
+ str = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256)
+ end
+
+ str
+ end
+
+ # Returns a string containing the IP address representation in
+ # canonical form.
+ def to_string
+ return _to_string(@addr)
+ end
+
+ # Returns a network byte ordered string form of the IP address.
+ def hton
+ case @family
+ when Socket::AF_INET
+ return [@addr].pack('N')
+ when Socket::AF_INET6
+ return (0..7).map { |i|
+ (@addr >> (112 - 16 * i)) & 0xffff
+ }.pack('n8')
+ else
+ raise "unsupported address family"
+ end
+ end
+
+ # Returns true if the ipaddr is an IPv4 address.
+ def ipv4?
+ return @family == Socket::AF_INET
+ end
+
+ # Returns true if the ipaddr is an IPv6 address.
+ def ipv6?
+ return @family == Socket::AF_INET6
+ end
+
+ # Returns true if the ipaddr is an IPv4-mapped IPv6 address.
+ def ipv4_mapped?
+ return ipv6? && (@addr >> 32) == 0xffff
+ end
+
+ # Returns true if the ipaddr is an IPv4-compatible IPv6 address.
+ def ipv4_compat?
+ if !ipv6? || (@addr >> 32) != 0
+ return false
+ end
+ a = (@addr & IN4MASK)
+ return a != 0 && a != 1
+ end
+
+ # Returns a new ipaddr built by converting the native IPv4 address
+ # into an IPv4-mapped IPv6 address.
+ def ipv4_mapped
+ if !ipv4?
+ raise ArgumentError, "not an IPv4 address"
+ end
+ return self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6)
+ end
+
+ # Returns a new ipaddr built by converting the native IPv4 address
+ # into an IPv4-compatible IPv6 address.
+ def ipv4_compat
+ if !ipv4?
+ raise ArgumentError, "not an IPv4 address"
+ end
+ return self.clone.set(@addr, Socket::AF_INET6)
+ end
+
+ # Returns a new ipaddr built by converting the IPv6 address into a
+ # native IPv4 address. If the IP address is not an IPv4-mapped or
+ # IPv4-compatible IPv6 address, returns self.
+ def native
+ if !ipv4_mapped? && !ipv4_compat?
+ return self
+ end
+ return self.clone.set(@addr & IN4MASK, Socket::AF_INET)
+ end
+
+ # Returns a string for DNS reverse lookup. It returns a string in
+ # RFC3172 form for an IPv6 address.
+ def reverse
+ case @family
+ when Socket::AF_INET
+ return _reverse + ".in-addr.arpa"
+ when Socket::AF_INET6
+ return ip6_arpa
+ else
+ raise "unsupported address family"
+ end
+ end
+
+ # Returns a string for DNS reverse lookup compatible with RFC3172.
+ def ip6_arpa
+ if !ipv6?
+ raise ArgumentError, "not an IPv6 address"
+ end
+ return _reverse + ".ip6.arpa"
+ end
+
+ # Returns a string for DNS reverse lookup compatible with RFC1886.
+ def ip6_int
+ if !ipv6?
+ raise ArgumentError, "not an IPv6 address"
+ end
+ return _reverse + ".ip6.int"
+ end
+
+ # Returns the successor to the ipaddr.
+ def succ
+ return self.clone.set(@addr + 1, @family)
+ end
+
+ # Compares the ipaddr with another.
+ def <=>(other)
+ other = coerce_other(other)
+
+ return nil if other.family != @family
+
+ return @addr <=> other.to_i
+ end
+ include Comparable
+
+ # Creates a Range object for the network address.
+ def to_range
+ begin_addr = (@addr & @mask_addr)
+
+ case @family
+ when Socket::AF_INET
+ end_addr = (@addr | (IN4MASK ^ @mask_addr))
+ when Socket::AF_INET6
+ end_addr = (@addr | (IN6MASK ^ @mask_addr))
+ else
+ raise "unsupported address family"
+ end
+
+ return clone.set(begin_addr, @family)..clone.set(end_addr, @family)
+ end
+
+ # Returns a string containing a human-readable representation of the
+ # ipaddr. ("#<IPAddr: family:address/mask>")
+ def inspect
+ case @family
+ when Socket::AF_INET
+ af = "IPv4"
+ when Socket::AF_INET6
+ af = "IPv6"
+ else
+ raise "unsupported address family"
+ end
+ return sprintf("#<%s: %s:%s/%s>", self.class.name,
+ af, _to_string(@addr), _to_string(@mask_addr))
+ end
+
+ protected
+
+ def set(addr, *family)
+ case family[0] ? family[0] : @family
+ when Socket::AF_INET
+ if addr < 0 || addr > IN4MASK
+ raise ArgumentError, "invalid address"
+ end
+ when Socket::AF_INET6
+ if addr < 0 || addr > IN6MASK
+ raise ArgumentError, "invalid address"
+ end
+ else
+ raise ArgumentError, "unsupported address family"
+ end
+ @addr = addr
+ if family[0]
+ @family = family[0]
+ end
+ return self
+ end
+
+ def mask!(mask)
+ if mask.kind_of?(String)
+ if mask =~ /^\d+$/
+ prefixlen = mask.to_i
+ else
+ m = IPAddr.new(mask)
+ if m.family != @family
+ raise ArgumentError, "address family is not same"
+ end
+ @mask_addr = m.to_i
+ @addr &= @mask_addr
+ return self
+ end
+ else
+ prefixlen = mask
+ end
+ case @family
+ when Socket::AF_INET
+ if prefixlen < 0 || prefixlen > 32
+ raise ArgumentError, "invalid length"
+ end
+ masklen = 32 - prefixlen
+ @mask_addr = ((IN4MASK >> masklen) << masklen)
+ when Socket::AF_INET6
+ if prefixlen < 0 || prefixlen > 128
+ raise ArgumentError, "invalid length"
+ end
+ masklen = 128 - prefixlen
+ @mask_addr = ((IN6MASK >> masklen) << masklen)
+ else
+ raise "unsupported address family"
+ end
+ @addr = ((@addr >> masklen) << masklen)
+ return self
+ end
+
+ private
+
+ # Creates a new ipaddr object either from a human readable IP
+ # address representation in string, or from a packed in_addr value
+ # followed by an address family.
+ #
+ # In the former case, the following are the valid formats that will
+ # be recognized: "address", "address/prefixlen" and "address/mask",
+ # where IPv6 address may be enclosed in square brackets (`[' and
+ # `]'). If a prefixlen or a mask is specified, it returns a masked
+ # IP address. Although the address family is determined
+ # automatically from a specified string, you can specify one
+ # explicitly by the optional second argument.
+ #
+ # Otherwise an IP addess is generated from a packed in_addr value
+ # and an address family.
+ #
+ # The IPAddr class defines many methods and operators, and some of
+ # those, such as &, |, include? and ==, accept a string, or a packed
+ # in_addr value instead of an IPAddr object.
+ def initialize(addr = '::', family = Socket::AF_UNSPEC)
+ if !addr.kind_of?(String)
+ case family
+ when Socket::AF_INET, Socket::AF_INET6
+ set(addr.to_i, family)
+ @mask_addr = (family == Socket::AF_INET) ? IN4MASK : IN6MASK
+ return
+ when Socket::AF_UNSPEC
+ raise ArgumentError, "address family must be specified"
+ else
+ raise ArgumentError, "unsupported address family: #{family}"
+ end
+ end
+ prefix, prefixlen = addr.split('/')
+ if prefix =~ /^\[(.*)\]$/i
+ prefix = $1
+ family = Socket::AF_INET6
+ end
+ # It seems AI_NUMERICHOST doesn't do the job.
+ #Socket.getaddrinfo(left, nil, Socket::AF_INET6, Socket::SOCK_STREAM, nil,
+ # Socket::AI_NUMERICHOST)
+ begin
+ IPSocket.getaddress(prefix) # test if address is vaild
+ rescue
+ raise ArgumentError, "invalid address"
+ end
+ @addr = @family = nil
+ if family == Socket::AF_UNSPEC || family == Socket::AF_INET
+ @addr = in_addr(prefix)
+ if @addr
+ @family = Socket::AF_INET
+ end
+ end
+ if !@addr && (family == Socket::AF_UNSPEC || family == Socket::AF_INET6)
+ @addr = in6_addr(prefix)
+ @family = Socket::AF_INET6
+ end
+ if family != Socket::AF_UNSPEC && @family != family
+ raise ArgumentError, "address family mismatch"
+ end
+ if prefixlen
+ mask!(prefixlen)
+ else
+ @mask_addr = (@family == Socket::AF_INET) ? IN4MASK : IN6MASK
+ end
+ end
+
+ def coerce_other(other)
+ case other
+ when IPAddr
+ other
+ when String
+ self.class.new(other)
+ else
+ self.class.new(other, @family)
+ end
+ end
+
+ def in_addr(addr)
+ if addr =~ /^\d+\.\d+\.\d+\.\d+$/
+ return addr.split('.').inject(0) { |i, s|
+ i << 8 | s.to_i
+ }
+ end
+ return nil
+ end
+
+ def in6_addr(left)
+ case left
+ when /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i
+ return in_addr($1) + 0xffff00000000
+ when /^::(\d+\.\d+\.\d+\.\d+)$/i
+ return in_addr($1)
+ when /[^0-9a-f:]/i
+ raise ArgumentError, "invalid address"
+ when /^(.*)::(.*)$/
+ left, right = $1, $2
+ else
+ right = ''
+ end
+ l = left.split(':')
+ r = right.split(':')
+ rest = 8 - l.size - r.size
+ if rest < 0
+ return nil
+ end
+ return (l + Array.new(rest, '0') + r).inject(0) { |i, s|
+ i << 16 | s.hex
+ }
+ end
+
+ def addr_mask(addr)
+ case @family
+ when Socket::AF_INET
+ return addr & IN4MASK
+ when Socket::AF_INET6
+ return addr & IN6MASK
+ else
+ raise "unsupported address family"
+ end
+ end
+
+ def _reverse
+ case @family
+ when Socket::AF_INET
+ return (0..3).map { |i|
+ (@addr >> (8 * i)) & 0xff
+ }.join('.')
+ when Socket::AF_INET6
+ return ("%.32x" % @addr).reverse!.gsub!(/.(?!$)/, '\&.')
+ else
+ raise "unsupported address family"
+ end
+ end
+
+ def _to_string(addr)
+ case @family
+ when Socket::AF_INET
+ return (0..3).map { |i|
+ (addr >> (24 - 8 * i)) & 0xff
+ }.join('.')
+ when Socket::AF_INET6
+ return (("%.32x" % addr).gsub!(/.{4}(?!$)/, '\&:'))
+ else
+ raise "unsupported address family"
+ end
+ end
+
+end
+
+if $0 == __FILE__
+ eval DATA.read, nil, $0, __LINE__+4
+end
+
+__END__
+
+require 'test/unit'
+
+class TC_IPAddr < Test::Unit::TestCase
+ def test_s_new
+ assert_nothing_raised {
+ IPAddr.new("3FFE:505:ffff::/48")
+ IPAddr.new("0:0:0:1::")
+ IPAddr.new("2001:200:300::/48")
+ }
+
+ a = IPAddr.new
+ assert_equal("::", a.to_s)
+ assert_equal("0000:0000:0000:0000:0000:0000:0000:0000", a.to_string)
+ assert_equal(Socket::AF_INET6, a.family)
+
+ a = IPAddr.new("0123:4567:89ab:cdef:0ABC:DEF0:1234:5678")
+ assert_equal("123:4567:89ab:cdef:abc:def0:1234:5678", a.to_s)
+ assert_equal("0123:4567:89ab:cdef:0abc:def0:1234:5678", a.to_string)
+ assert_equal(Socket::AF_INET6, a.family)
+
+ a = IPAddr.new("3ffe:505:2::/48")
+ assert_equal("3ffe:505:2::", a.to_s)
+ assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0000", a.to_string)
+ assert_equal(Socket::AF_INET6, a.family)
+ assert_equal(false, a.ipv4?)
+ assert_equal(true, a.ipv6?)
+ assert_equal("#<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>", a.inspect)
+
+ a = IPAddr.new("3ffe:505:2::/ffff:ffff:ffff::")
+ assert_equal("3ffe:505:2::", a.to_s)
+ assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0000", a.to_string)
+ assert_equal(Socket::AF_INET6, a.family)
+
+ a = IPAddr.new("0.0.0.0")
+ assert_equal("0.0.0.0", a.to_s)
+ assert_equal("0.0.0.0", a.to_string)
+ assert_equal(Socket::AF_INET, a.family)
+
+ a = IPAddr.new("192.168.1.2")
+ assert_equal("192.168.1.2", a.to_s)
+ assert_equal("192.168.1.2", a.to_string)
+ assert_equal(Socket::AF_INET, a.family)
+ assert_equal(true, a.ipv4?)
+ assert_equal(false, a.ipv6?)
+
+ a = IPAddr.new("192.168.1.2/24")
+ assert_equal("192.168.1.0", a.to_s)
+ assert_equal("192.168.1.0", a.to_string)
+ assert_equal(Socket::AF_INET, a.family)
+ assert_equal("#<IPAddr: IPv4:192.168.1.0/255.255.255.0>", a.inspect)
+
+ a = IPAddr.new("192.168.1.2/255.255.255.0")
+ assert_equal("192.168.1.0", a.to_s)
+ assert_equal("192.168.1.0", a.to_string)
+ assert_equal(Socket::AF_INET, a.family)
+
+ assert_equal("0:0:0:1::", IPAddr.new("0:0:0:1::").to_s)
+ assert_equal("2001:200:300::", IPAddr.new("2001:200:300::/48").to_s)
+
+ assert_equal("2001:200:300::", IPAddr.new("[2001:200:300::]/48").to_s)
+
+ [
+ ["fe80::1%fxp0"],
+ ["::1/255.255.255.0"],
+ ["::1:192.168.1.2/120"],
+ [IPAddr.new("::1").to_i],
+ ["::ffff:192.168.1.2/120", Socket::AF_INET],
+ ["[192.168.1.2]/120"],
+ ].each { |args|
+ assert_raises(ArgumentError) {
+ IPAddr.new(*args)
+ }
+ }
+ end
+
+ def test_s_new_ntoh
+ addr = ''
+ IPAddr.new("1234:5678:9abc:def0:1234:5678:9abc:def0").hton.each_byte { |c|
+ addr += sprintf("%02x", c)
+ }
+ assert_equal("123456789abcdef0123456789abcdef0", addr)
+ addr = ''
+ IPAddr.new("123.45.67.89").hton.each_byte { |c|
+ addr += sprintf("%02x", c)
+ }
+ assert_equal(sprintf("%02x%02x%02x%02x", 123, 45, 67, 89), addr)
+ a = IPAddr.new("3ffe:505:2::")
+ assert_equal("3ffe:505:2::", IPAddr.new_ntoh(a.hton).to_s)
+ a = IPAddr.new("192.168.2.1")
+ assert_equal("192.168.2.1", IPAddr.new_ntoh(a.hton).to_s)
+ end
+
+ def test_ipv4_compat
+ a = IPAddr.new("::192.168.1.2")
+ assert_equal("::192.168.1.2", a.to_s)
+ assert_equal("0000:0000:0000:0000:0000:0000:c0a8:0102", a.to_string)
+ assert_equal(Socket::AF_INET6, a.family)
+ assert_equal(true, a.ipv4_compat?)
+ b = a.native
+ assert_equal("192.168.1.2", b.to_s)
+ assert_equal(Socket::AF_INET, b.family)
+ assert_equal(false, b.ipv4_compat?)
+
+ a = IPAddr.new("192.168.1.2")
+ b = a.ipv4_compat
+ assert_equal("::192.168.1.2", b.to_s)
+ assert_equal(Socket::AF_INET6, b.family)
+ end
+
+ def test_ipv4_mapped
+ a = IPAddr.new("::ffff:192.168.1.2")
+ assert_equal("::ffff:192.168.1.2", a.to_s)
+ assert_equal("0000:0000:0000:0000:0000:ffff:c0a8:0102", a.to_string)
+ assert_equal(Socket::AF_INET6, a.family)
+ assert_equal(true, a.ipv4_mapped?)
+ b = a.native
+ assert_equal("192.168.1.2", b.to_s)
+ assert_equal(Socket::AF_INET, b.family)
+ assert_equal(false, b.ipv4_mapped?)
+
+ a = IPAddr.new("192.168.1.2")
+ b = a.ipv4_mapped
+ assert_equal("::ffff:192.168.1.2", b.to_s)
+ assert_equal(Socket::AF_INET6, b.family)
+ end
+
+ def test_reverse
+ assert_equal("f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa", IPAddr.new("3ffe:505:2::f").reverse)
+ assert_equal("1.2.168.192.in-addr.arpa", IPAddr.new("192.168.2.1").reverse)
+ end
+
+ def test_ip6_arpa
+ assert_equal("f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa", IPAddr.new("3ffe:505:2::f").ip6_arpa)
+ assert_raises(ArgumentError) {
+ IPAddr.new("192.168.2.1").ip6_arpa
+ }
+ end
+
+ def test_ip6_int
+ assert_equal("f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.int", IPAddr.new("3ffe:505:2::f").ip6_int)
+ assert_raises(ArgumentError) {
+ IPAddr.new("192.168.2.1").ip6_int
+ }
+ end
+
+ def test_to_s
+ assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0001", IPAddr.new("3ffe:505:2::1").to_string)
+ assert_equal("3ffe:505:2::1", IPAddr.new("3ffe:505:2::1").to_s)
+ end
+end
+
+class TC_Operator < Test::Unit::TestCase
+
+ IN6MASK32 = "ffff:ffff::"
+ IN6MASK128 = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
+
+ def setup
+ @in6_addr_any = IPAddr.new()
+ @a = IPAddr.new("3ffe:505:2::/48")
+ @b = IPAddr.new("0:0:0:1::")
+ @c = IPAddr.new(IN6MASK32)
+ end
+ alias set_up setup
+
+ def test_or
+ assert_equal("3ffe:505:2:1::", (@a | @b).to_s)
+ a = @a
+ a |= @b
+ assert_equal("3ffe:505:2:1::", a.to_s)
+ assert_equal("3ffe:505:2::", @a.to_s)
+ assert_equal("3ffe:505:2:1::",
+ (@a | 0x00000000000000010000000000000000).to_s)
+ end
+
+ def test_and
+ assert_equal("3ffe:505::", (@a & @c).to_s)
+ a = @a
+ a &= @c
+ assert_equal("3ffe:505::", a.to_s)
+ assert_equal("3ffe:505:2::", @a.to_s)
+ assert_equal("3ffe:505::", (@a & 0xffffffff000000000000000000000000).to_s)
+ end
+
+ def test_shift_right
+ assert_equal("0:3ffe:505:2::", (@a >> 16).to_s)
+ a = @a
+ a >>= 16
+ assert_equal("0:3ffe:505:2::", a.to_s)
+ assert_equal("3ffe:505:2::", @a.to_s)
+ end
+
+ def test_shift_left
+ assert_equal("505:2::", (@a << 16).to_s)
+ a = @a
+ a <<= 16
+ assert_equal("505:2::", a.to_s)
+ assert_equal("3ffe:505:2::", @a.to_s)
+ end
+
+ def test_carrot
+ a = ~@in6_addr_any
+ assert_equal(IN6MASK128, a.to_s)
+ assert_equal("::", @in6_addr_any.to_s)
+ end
+
+ def test_equal
+ assert_equal(true, @a == IPAddr.new("3ffe:505:2::"))
+ assert_equal(false, @a == IPAddr.new("3ffe:505:3::"))
+ assert_equal(true, @a != IPAddr.new("3ffe:505:3::"))
+ assert_equal(false, @a != IPAddr.new("3ffe:505:2::"))
+ end
+
+ def test_mask
+ a = @a.mask(32)
+ assert_equal("3ffe:505::", a.to_s)
+ assert_equal("3ffe:505:2::", @a.to_s)
+ end
+
+ def test_include?
+ assert_equal(true, @a.include?(IPAddr.new("3ffe:505:2::")))
+ assert_equal(true, @a.include?(IPAddr.new("3ffe:505:2::1")))
+ assert_equal(false, @a.include?(IPAddr.new("3ffe:505:3::")))
+ net1 = IPAddr.new("192.168.2.0/24")
+ assert_equal(true, net1.include?(IPAddr.new("192.168.2.0")))
+ assert_equal(true, net1.include?(IPAddr.new("192.168.2.255")))
+ assert_equal(false, net1.include?(IPAddr.new("192.168.3.0")))
+ # test with integer parameter
+ int = (192 << 24) + (168 << 16) + (2 << 8) + 13
+
+ assert_equal(true, net1.include?(int))
+ assert_equal(false, net1.include?(int+255))
+
+ end
+
+end
diff --git a/ruby/lib/irb.rb b/ruby/lib/irb.rb
new file mode 100644
index 0000000..3ca5ff8
--- /dev/null
+++ b/ruby/lib/irb.rb
@@ -0,0 +1,354 @@
+#
+# irb.rb - irb main module
+# $Release Version: 0.9.5 $
+# $Revision: 24294 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+require "e2mmap"
+
+require "irb/init"
+require "irb/context"
+require "irb/extend-command"
+#require "irb/workspace"
+
+require "irb/ruby-lex"
+require "irb/input-method"
+require "irb/locale"
+
+STDOUT.sync = true
+
+module IRB
+ @RCS_ID='-$Id: irb.rb 24294 2009-07-26 15:33:29Z yugui $-'
+
+ class Abort < Exception;end
+
+ #
+ @CONF = {}
+
+ def IRB.conf
+ @CONF
+ end
+
+ # IRB version method
+ def IRB.version
+ if v = @CONF[:VERSION] then return v end
+
+ require "irb/version"
+ rv = @RELEASE_VERSION.sub(/\.0/, "")
+ @CONF[:VERSION] = format("irb %s(%s)", rv, @LAST_UPDATE_DATE)
+ end
+
+ def IRB.CurrentContext
+ IRB.conf[:MAIN_CONTEXT]
+ end
+
+ # initialize IRB and start TOP_LEVEL irb
+ def IRB.start(ap_path = nil)
+ $0 = File::basename(ap_path, ".rb") if ap_path
+
+ IRB.setup(ap_path)
+
+ if @CONF[:SCRIPT]
+ irb = Irb.new(nil, @CONF[:SCRIPT])
+ else
+ irb = Irb.new
+ end
+
+ @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
+ @CONF[:MAIN_CONTEXT] = irb.context
+
+ trap("SIGINT") do
+ irb.signal_handle
+ end
+
+ begin
+ catch(:IRB_EXIT) do
+ irb.eval_input
+ end
+ ensure
+ irb_at_exit
+ end
+# print "\n"
+ end
+
+ def IRB.irb_at_exit
+ @CONF[:AT_EXIT].each{|hook| hook.call}
+ end
+
+ def IRB.irb_exit(irb, ret)
+ throw :IRB_EXIT, ret
+ end
+
+ def IRB.irb_abort(irb, exception = Abort)
+ if defined? Thread
+ irb.context.thread.raise exception, "abort then interrupt!!"
+ else
+ raise exception, "abort then interrupt!!"
+ end
+ end
+
+ #
+ # irb interpreter main routine
+ #
+ class Irb
+ def initialize(workspace = nil, input_method = nil, output_method = nil)
+ @context = Context.new(self, workspace, input_method, output_method)
+ @context.main.extend ExtendCommandBundle
+ @signal_status = :IN_IRB
+
+ @scanner = RubyLex.new
+ @scanner.exception_on_syntax_error = false
+ end
+ attr_reader :context
+ attr_accessor :scanner
+
+ def eval_input
+ @scanner.set_prompt do
+ |ltype, indent, continue, line_no|
+ if ltype
+ f = @context.prompt_s
+ elsif continue
+ f = @context.prompt_c
+ elsif indent > 0
+ f = @context.prompt_n
+ else
+ f = @context.prompt_i
+ end
+ f = "" unless f
+ if @context.prompting?
+ @context.io.prompt = p = prompt(f, ltype, indent, line_no)
+ else
+ @context.io.prompt = p = ""
+ end
+ if @context.auto_indent_mode
+ unless ltype
+ ind = prompt(@context.prompt_i, ltype, indent, line_no)[/.*\z/].size +
+ indent * 2 - p.size
+ ind += 2 if continue
+ @context.io.prompt = p + " " * ind if ind > 0
+ end
+ end
+ end
+
+ @scanner.set_input(@context.io) do
+ signal_status(:IN_INPUT) do
+ if l = @context.io.gets
+ print l if @context.verbose?
+ else
+ if @context.ignore_eof? and @context.io.readable_atfer_eof?
+ l = "\n"
+ if @context.verbose?
+ printf "Use \"exit\" to leave %s\n", @context.ap_name
+ end
+ end
+ end
+ l
+ end
+ end
+
+ @scanner.each_top_level_statement do |line, line_no|
+ signal_status(:IN_EVAL) do
+ begin
+ line.untaint
+ @context.evaluate(line, line_no)
+ output_value if @context.echo?
+ exc = nil
+ rescue Interrupt => exc
+ rescue SystemExit, SignalException
+ raise
+ rescue Exception => exc
+ end
+ if exc
+ print exc.class, ": ", exc, "\n"
+ if exc.backtrace[0] =~ /irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/ &&
+ !(SyntaxError === exc)
+ irb_bug = true
+ else
+ irb_bug = false
+ end
+
+ messages = []
+ lasts = []
+ levels = 0
+ for m in exc.backtrace
+ m = @context.workspace.filter_backtrace(m) unless irb_bug
+ if m
+ if messages.size < @context.back_trace_limit
+ messages.push "\tfrom "+m
+ else
+ lasts.push "\tfrom "+m
+ if lasts.size > @context.back_trace_limit
+ lasts.shift
+ levels += 1
+ end
+ end
+ end
+ end
+ print messages.join("\n"), "\n"
+ unless lasts.empty?
+ printf "... %d levels...\n", levels if levels > 0
+ print lasts.join("\n")
+ end
+ print "Maybe IRB bug!!\n" if irb_bug
+ end
+ if $SAFE > 2
+ abort "Error: irb does not work for $SAFE level higher than 2"
+ end
+ end
+ end
+ end
+
+ def suspend_name(path = nil, name = nil)
+ @context.irb_path, back_path = path, @context.irb_path if path
+ @context.irb_name, back_name = name, @context.irb_name if name
+ begin
+ yield back_path, back_name
+ ensure
+ @context.irb_path = back_path if path
+ @context.irb_name = back_name if name
+ end
+ end
+
+ def suspend_workspace(workspace)
+ @context.workspace, back_workspace = workspace, @context.workspace
+ begin
+ yield back_workspace
+ ensure
+ @context.workspace = back_workspace
+ end
+ end
+
+ def suspend_input_method(input_method)
+ back_io = @context.io
+ @context.instance_eval{@io = input_method}
+ begin
+ yield back_io
+ ensure
+ @context.instance_eval{@io = back_io}
+ end
+ end
+
+ def suspend_context(context)
+ @context, back_context = context, @context
+ begin
+ yield back_context
+ ensure
+ @context = back_context
+ end
+ end
+
+ def signal_handle
+ unless @context.ignore_sigint?
+ print "\nabort!!\n" if @context.verbose?
+ exit
+ end
+
+ case @signal_status
+ when :IN_INPUT
+ print "^C\n"
+ raise RubyLex::TerminateLineInput
+ when :IN_EVAL
+ IRB.irb_abort(self)
+ when :IN_LOAD
+ IRB.irb_abort(self, LoadAbort)
+ when :IN_IRB
+ # ignore
+ else
+ # ignore other cases as well
+ end
+ end
+
+ def signal_status(status)
+ return yield if @signal_status == :IN_LOAD
+
+ signal_status_back = @signal_status
+ @signal_status = status
+ begin
+ yield
+ ensure
+ @signal_status = signal_status_back
+ end
+ end
+
+ def prompt(prompt, ltype, indent, line_no)
+ p = prompt.dup
+ p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
+ case $2
+ when "N"
+ @context.irb_name
+ when "m"
+ @context.main.to_s
+ when "M"
+ @context.main.inspect
+ when "l"
+ ltype
+ when "i"
+ if $1
+ format("%" + $1 + "d", indent)
+ else
+ indent.to_s
+ end
+ when "n"
+ if $1
+ format("%" + $1 + "d", line_no)
+ else
+ line_no.to_s
+ end
+ when "%"
+ "%"
+ end
+ end
+ p
+ end
+
+ def output_value
+ if @context.inspect?
+ printf @context.return_format, @context.last_value.inspect
+ else
+ printf @context.return_format, @context.last_value
+ end
+ end
+
+ def inspect
+ ary = []
+ for iv in instance_variables
+ case (iv = iv.to_s)
+ when "@signal_status"
+ ary.push format("%s=:%s", iv, @signal_status.id2name)
+ when "@context"
+ ary.push format("%s=%s", iv, eval(iv).__to_s__)
+ else
+ ary.push format("%s=%s", iv, eval(iv))
+ end
+ end
+ format("#<%s: %s>", self.class, ary.join(", "))
+ end
+ end
+
+ # Singleton method
+ def @CONF.inspect
+ IRB.version unless self[:VERSION]
+
+ array = []
+ for k, v in sort{|a1, a2| a1[0].id2name <=> a2[0].id2name}
+ case k
+ when :MAIN_CONTEXT, :__TMP__EHV__
+ array.push format("CONF[:%s]=...myself...", k.id2name)
+ when :PROMPT
+ s = v.collect{
+ |kk, vv|
+ ss = vv.collect{|kkk, vvv| ":#{kkk.id2name}=>#{vvv.inspect}"}
+ format(":%s=>{%s}", kk.id2name, ss.join(", "))
+ }
+ array.push format("CONF[:%s]={%s}", k.id2name, s.join(", "))
+ else
+ array.push format("CONF[:%s]=%s", k.id2name, v.inspect)
+ end
+ end
+ array.join("\n")
+ end
+end
diff --git a/ruby/lib/irb/cmd/chws.rb b/ruby/lib/irb/cmd/chws.rb
new file mode 100644
index 0000000..19fea49
--- /dev/null
+++ b/ruby/lib/irb/cmd/chws.rb
@@ -0,0 +1,32 @@
+#
+# change-ws.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "irb/cmd/nop.rb"
+require "irb/ext/change-ws.rb"
+
+module IRB
+ module ExtendCommand
+
+ class CurrentWorkingWorkspace<Nop
+ def execute(*obj)
+ irb_context.main
+ end
+ end
+
+ class ChangeWorkspace<Nop
+ def execute(*obj)
+ irb_context.change_workspace(*obj)
+ irb_context.main
+ end
+ end
+ end
+end
+
diff --git a/ruby/lib/irb/cmd/fork.rb b/ruby/lib/irb/cmd/fork.rb
new file mode 100644
index 0000000..bf4acee
--- /dev/null
+++ b/ruby/lib/irb/cmd/fork.rb
@@ -0,0 +1,38 @@
+#
+# fork.rb -
+# $Release Version: 0.9.5 $
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+@RCS_ID='-$Id: fork.rb 14912 2008-01-06 15:49:38Z akr $-'
+
+
+module IRB
+ module ExtendCommand
+ class Fork<Nop
+ def execute(&block)
+ pid = send ExtendCommand.irb_original_method_name("fork")
+ unless pid
+ class<<self
+ alias_method :exit, ExtendCommand.irb_original_method_name('exit')
+ end
+ if iterator?
+ begin
+ yield
+ ensure
+ exit
+ end
+ end
+ end
+ pid
+ end
+ end
+ end
+end
+
+
diff --git a/ruby/lib/irb/cmd/help.rb b/ruby/lib/irb/cmd/help.rb
new file mode 100644
index 0000000..25ca463
--- /dev/null
+++ b/ruby/lib/irb/cmd/help.rb
@@ -0,0 +1,36 @@
+#
+# help.rb - helper using ri
+# $Release Version: 0.9.5$
+# $Revision: 26019 $
+#
+# --
+#
+#
+#
+
+require 'rdoc/ri/driver'
+require 'rdoc/ri/util'
+
+require "irb/cmd/nop.rb"
+
+module IRB
+ module ExtendCommand
+ class Help<Nop
+ begin
+ Ri = RDoc::RI::Driver.new
+ rescue SystemExit
+ else
+ def execute(*names)
+ names.each do |name|
+ begin
+ Ri.get_info_for(name.to_s)
+ rescue RDoc::RI::Error
+ puts $!.message
+ end
+ end
+ nil
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/irb/cmd/load.rb b/ruby/lib/irb/cmd/load.rb
new file mode 100644
index 0000000..802a321
--- /dev/null
+++ b/ruby/lib/irb/cmd/load.rb
@@ -0,0 +1,66 @@
+#
+# load.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "irb/cmd/nop.rb"
+require "irb/ext/loader"
+
+module IRB
+ module ExtendCommand
+ class Load<Nop
+ include IrbLoader
+
+ def execute(file_name, priv = nil)
+# return ruby_load(file_name) unless IRB.conf[:USE_LOADER]
+ return irb_load(file_name, priv)
+ end
+ end
+
+ class Require<Nop
+ include IrbLoader
+
+ def execute(file_name)
+# return ruby_require(file_name) unless IRB.conf[:USE_LOADER]
+
+ rex = Regexp.new("#{Regexp.quote(file_name)}(\.o|\.rb)?")
+ return false if $".find{|f| f =~ rex}
+
+ case file_name
+ when /\.rb$/
+ begin
+ if irb_load(file_name)
+ $".push file_name
+ return true
+ end
+ rescue LoadError
+ end
+ when /\.(so|o|sl)$/
+ return ruby_require(file_name)
+ end
+
+ begin
+ irb_load(f = file_name + ".rb")
+ $".push f
+ return true
+ rescue LoadError
+ return ruby_require(file_name)
+ end
+ end
+ end
+
+ class Source<Nop
+ include IrbLoader
+ def execute(file_name)
+ source_file(file_name)
+ end
+ end
+ end
+
+end
diff --git a/ruby/lib/irb/cmd/nop.rb b/ruby/lib/irb/cmd/nop.rb
new file mode 100644
index 0000000..b0e3f8c
--- /dev/null
+++ b/ruby/lib/irb/cmd/nop.rb
@@ -0,0 +1,38 @@
+#
+# nop.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+module IRB
+ module ExtendCommand
+ class Nop
+
+ @RCS_ID='-$Id: nop.rb 14912 2008-01-06 15:49:38Z akr $-'
+
+ def self.execute(conf, *opts)
+ command = new(conf)
+ command.execute(*opts)
+ end
+
+ def initialize(conf)
+ @irb_context = conf
+ end
+
+ attr_reader :irb_context
+
+ def irb
+ @irb_context.irb
+ end
+
+ def execute(*opts)
+ #nop
+ end
+ end
+ end
+end
+
diff --git a/ruby/lib/irb/cmd/pushws.rb b/ruby/lib/irb/cmd/pushws.rb
new file mode 100644
index 0000000..9cab961
--- /dev/null
+++ b/ruby/lib/irb/cmd/pushws.rb
@@ -0,0 +1,38 @@
+#
+# change-ws.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "irb/cmd/nop.rb"
+require "irb/ext/workspaces.rb"
+
+module IRB
+ module ExtendCommand
+ class Workspaces<Nop
+ def execute(*obj)
+ irb_context.workspaces.collect{|ws| ws.main}
+ end
+ end
+
+ class PushWorkspace<Workspaces
+ def execute(*obj)
+ irb_context.push_workspace(*obj)
+ super
+ end
+ end
+
+ class PopWorkspace<Workspaces
+ def execute(*obj)
+ irb_context.pop_workspace(*obj)
+ super
+ end
+ end
+ end
+end
+
diff --git a/ruby/lib/irb/cmd/subirb.rb b/ruby/lib/irb/cmd/subirb.rb
new file mode 100644
index 0000000..af3d2e5
--- /dev/null
+++ b/ruby/lib/irb/cmd/subirb.rb
@@ -0,0 +1,42 @@
+#!/usr/local/bin/ruby
+#
+# multi.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "irb/cmd/nop.rb"
+require "irb/ext/multi-irb"
+
+module IRB
+ module ExtendCommand
+ class IrbCommand<Nop
+ def execute(*obj)
+ IRB.irb(nil, *obj)
+ end
+ end
+
+ class Jobs<Nop
+ def execute
+ IRB.JobManager
+ end
+ end
+
+ class Foreground<Nop
+ def execute(key)
+ IRB.JobManager.switch(key)
+ end
+ end
+
+ class Kill<Nop
+ def execute(*keys)
+ IRB.JobManager.kill(*keys)
+ end
+ end
+ end
+end
diff --git a/ruby/lib/irb/completion.rb b/ruby/lib/irb/completion.rb
new file mode 100644
index 0000000..e74a45b
--- /dev/null
+++ b/ruby/lib/irb/completion.rb
@@ -0,0 +1,207 @@
+#
+# irb/completor.rb -
+# $Release Version: 0.9$
+# $Revision: 23233 $
+# by Keiju ISHITSUKA(keiju@ishitsuka.com)
+# From Original Idea of shugo@ruby-lang.org
+#
+
+require "readline"
+
+module IRB
+ module InputCompletor
+
+ @RCS_ID='-$Id: completion.rb 23233 2009-04-19 13:35:47Z yugui $-'
+
+ ReservedWords = [
+ "BEGIN", "END",
+ "alias", "and",
+ "begin", "break",
+ "case", "class",
+ "def", "defined", "do",
+ "else", "elsif", "end", "ensure",
+ "false", "for",
+ "if", "in",
+ "module",
+ "next", "nil", "not",
+ "or",
+ "redo", "rescue", "retry", "return",
+ "self", "super",
+ "then", "true",
+ "undef", "unless", "until",
+ "when", "while",
+ "yield",
+ ]
+
+ CompletionProc = proc { |input|
+ bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
+
+# puts "input: #{input}"
+
+ case input
+ when /^(\/[^\/]*\/)\.([^.]*)$/
+ # Regexp
+ receiver = $1
+ message = Regexp.quote($2)
+
+ candidates = Regexp.instance_methods.collect{|m| m.to_s}
+ select_message(receiver, message, candidates)
+
+ when /^([^\]]*\])\.([^.]*)$/
+ # Array
+ receiver = $1
+ message = Regexp.quote($2)
+
+ candidates = Array.instance_methods.collect{|m| m.to_s}
+ select_message(receiver, message, candidates)
+
+ when /^([^\}]*\})\.([^.]*)$/
+ # Proc or Hash
+ receiver = $1
+ message = Regexp.quote($2)
+
+ candidates = Proc.instance_methods.collect{|m| m.to_s}
+ candidates |= Hash.instance_methods.collect{|m| m.to_s}
+ select_message(receiver, message, candidates)
+
+ when /^(:[^:.]*)$/
+ # Symbol
+ if Symbol.respond_to?(:all_symbols)
+ sym = $1
+ candidates = Symbol.all_symbols.collect{|s| ":" + s.id2name}
+ candidates.grep(/^#{sym}/)
+ else
+ []
+ end
+
+ when /^::([A-Z][^:\.\(]*)$/
+ # Absolute Constant or class methods
+ receiver = $1
+ candidates = Object.constants.collect{|m| m.to_s}
+ candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
+
+ when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
+ # Constant or class methods
+ receiver = $1
+ message = Regexp.quote($4)
+ begin
+ candidates = eval("#{receiver}.constants.collect{|m| m.to_s}", bind)
+ candidates |= eval("#{receiver}.methods.collect{|m| m.to_s}", bind)
+ rescue Exception
+ candidates = []
+ end
+ candidates.grep(/^#{message}/).collect{|e| receiver + "::" + e}
+
+ when /^(:[^:.]+)\.([^.]*)$/
+ # Symbol
+ receiver = $1
+ message = Regexp.quote($2)
+
+ candidates = Symbol.instance_methods.collect{|m| m.to_s}
+ select_message(receiver, message, candidates)
+
+ when /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)\.([^.]*)$/
+ # Numeric
+ receiver = $1
+ message = Regexp.quote($5)
+
+ begin
+ candidates = eval(receiver, bind).methods.collect{|m| m.to_s}
+ rescue Exception
+ candidates = []
+ end
+ select_message(receiver, message, candidates)
+
+ when /^(-?0x[0-9a-fA-F_]+)\.([^.]*)$/
+ # Numeric(0xFFFF)
+ receiver = $1
+ message = Regexp.quote($2)
+
+ begin
+ candidates = eval(receiver, bind).methods.collect{|m| m.to_s}
+ rescue Exception
+ candidates = []
+ end
+ select_message(receiver, message, candidates)
+
+ when /^(\$[^.]*)$/
+ regmessage = Regexp.new(Regexp.quote($1))
+ candidates = global_variables.collect{|m| m.to_s}.grep(regmessage)
+
+# when /^(\$?(\.?[^.]+)+)\.([^.]*)$/
+ when /^((\.?[^.]+)+)\.([^.]*)$/
+ # variable
+ receiver = $1
+ message = Regexp.quote($3)
+
+ gv = eval("global_variables", bind).collect{|m| m.to_s}
+ lv = eval("local_variables", bind).collect{|m| m.to_s}
+ cv = eval("self.class.constants", bind).collect{|m| m.to_s}
+
+ if (gv | lv | cv).include?(receiver)
+ # foo.func and foo is local var.
+ candidates = eval("#{receiver}.methods", bind).collect{|m| m.to_s}
+ elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
+ # Foo::Bar.func
+ begin
+ candidates = eval("#{receiver}.methods", bind).collect{|m| m.to_s}
+ rescue Exception
+ candidates = []
+ end
+ else
+ # func1.func2
+ candidates = []
+ ObjectSpace.each_object(Module){|m|
+ begin
+ name = m.name
+ rescue Exception
+ name = ""
+ end
+ next if name != "IRB::Context" and
+ /^(IRB|SLex|RubyLex|RubyToken)/ =~ name
+ candidates.concat m.instance_methods(false).collect{|x| x.to_s}
+ }
+ candidates.sort!
+ candidates.uniq!
+ end
+ select_message(receiver, message, candidates)
+
+ when /^\.([^.]*)$/
+ # unknown(maybe String)
+
+ receiver = ""
+ message = Regexp.quote($1)
+
+ candidates = String.instance_methods(true).collect{|m| m.to_s}
+ select_message(receiver, message, candidates)
+
+ else
+ candidates = eval("methods | private_methods | local_variables | self.class.constants", bind).collect{|m| m.to_s}
+
+ (candidates|ReservedWords).grep(/^#{Regexp.quote(input)}/)
+ end
+ }
+
+ Operators = ["%", "&", "*", "**", "+", "-", "/",
+ "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
+ "[]", "[]=", "^", "!", "!=", "!~"]
+
+ def self.select_message(receiver, message, candidates)
+ candidates.grep(/^#{message}/).collect do |e|
+ case e
+ when /^[a-zA-Z_]/
+ receiver + "." + e
+ when /^[0-9]/
+ when *Operators
+ #receiver + " " + e
+ end
+ end
+ end
+ end
+end
+
+if Readline.respond_to?("basic_word_break_characters=")
+ Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
+end
+Readline.completion_append_character = nil
+Readline.completion_proc = IRB::InputCompletor::CompletionProc
diff --git a/ruby/lib/irb/context.rb b/ruby/lib/irb/context.rb
new file mode 100644
index 0000000..d96e8af
--- /dev/null
+++ b/ruby/lib/irb/context.rb
@@ -0,0 +1,255 @@
+#
+# irb/context.rb - irb context
+# $Release Version: 0.9.5$
+# $Revision: 19670 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+require "irb/workspace"
+
+module IRB
+ class Context
+ #
+ # Arguments:
+ # input_method: nil -- stdin or readline
+ # String -- File
+ # other -- using this as InputMethod
+ #
+ def initialize(irb, workspace = nil, input_method = nil, output_method = nil)
+ @irb = irb
+ if workspace
+ @workspace = workspace
+ else
+ @workspace = WorkSpace.new
+ end
+ @thread = Thread.current if defined? Thread
+# @irb_level = 0
+
+ # copy of default configuration
+ @ap_name = IRB.conf[:AP_NAME]
+ @rc = IRB.conf[:RC]
+ @load_modules = IRB.conf[:LOAD_MODULES]
+
+ @use_readline = IRB.conf[:USE_READLINE]
+ @inspect_mode = IRB.conf[:INSPECT_MODE]
+
+ self.math_mode = IRB.conf[:MATH_MODE] if IRB.conf[:MATH_MODE]
+ self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRACER]
+ self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
+ self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVAL_HISTORY]
+
+ @ignore_sigint = IRB.conf[:IGNORE_SIGINT]
+ @ignore_eof = IRB.conf[:IGNORE_EOF]
+
+ @back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT]
+
+ self.prompt_mode = IRB.conf[:PROMPT_MODE]
+
+ if IRB.conf[:SINGLE_IRB] or !defined?(JobManager)
+ @irb_name = IRB.conf[:IRB_NAME]
+ else
+ @irb_name = "irb#"+IRB.JobManager.n_jobs.to_s
+ end
+ @irb_path = "(" + @irb_name + ")"
+
+ case input_method
+ when nil
+ case use_readline?
+ when nil
+ if (defined?(ReadlineInputMethod) && STDIN.tty? &&
+ IRB.conf[:PROMPT_MODE] != :INF_RUBY)
+ @io = ReadlineInputMethod.new
+ else
+ @io = StdioInputMethod.new
+ end
+ when false
+ @io = StdioInputMethod.new
+ when true
+ if defined?(ReadlineInputMethod)
+ @io = ReadlineInputMethod.new
+ else
+ @io = StdioInputMethod.new
+ end
+ end
+
+ when String
+ @io = FileInputMethod.new(input_method)
+ @irb_name = File.basename(input_method)
+ @irb_path = input_method
+ else
+ @io = input_method
+ end
+ self.save_history = IRB.conf[:SAVE_HISTORY] if IRB.conf[:SAVE_HISTORY]
+
+ if output_method
+ @output_method = output_method
+ else
+ @output_method = StdioOutputMethod.new
+ end
+
+ @verbose = IRB.conf[:VERBOSE]
+ @echo = IRB.conf[:ECHO]
+ if @echo.nil?
+ @echo = true
+ end
+ @debug_level = IRB.conf[:DEBUG_LEVEL]
+ end
+
+ def main
+ @workspace.main
+ end
+
+ attr_reader :workspace_home
+ attr_accessor :workspace
+ attr_reader :thread
+ attr_accessor :io
+
+ attr_accessor :irb
+ attr_accessor :ap_name
+ attr_accessor :rc
+ attr_accessor :load_modules
+ attr_accessor :irb_name
+ attr_accessor :irb_path
+
+ attr_reader :use_readline
+ attr_reader :inspect_mode
+
+ attr_reader :prompt_mode
+ attr_accessor :prompt_i
+ attr_accessor :prompt_s
+ attr_accessor :prompt_c
+ attr_accessor :prompt_n
+ attr_accessor :auto_indent_mode
+ attr_accessor :return_format
+
+ attr_accessor :ignore_sigint
+ attr_accessor :ignore_eof
+ attr_accessor :echo
+ attr_accessor :verbose
+ attr_reader :debug_level
+
+ attr_accessor :back_trace_limit
+
+ alias use_readline? use_readline
+ alias rc? rc
+ alias ignore_sigint? ignore_sigint
+ alias ignore_eof? ignore_eof
+ alias echo? echo
+
+ def verbose?
+ if @verbose.nil?
+ if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)
+ false
+ elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
+ true
+ else
+ false
+ end
+ end
+ end
+
+ def prompting?
+ verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
+ (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
+ end
+
+ attr_reader :last_value
+
+ def set_last_value(value)
+ @last_value = value
+ @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
+ end
+
+ attr_reader :irb_name
+
+ def prompt_mode=(mode)
+ @prompt_mode = mode
+ pconf = IRB.conf[:PROMPT][mode]
+ @prompt_i = pconf[:PROMPT_I]
+ @prompt_s = pconf[:PROMPT_S]
+ @prompt_c = pconf[:PROMPT_C]
+ @prompt_n = pconf[:PROMPT_N]
+ @return_format = pconf[:RETURN]
+ if ai = pconf.include?(:AUTO_INDENT)
+ @auto_indent_mode = ai
+ else
+ @auto_indent_mode = IRB.conf[:AUTO_INDENT]
+ end
+ end
+
+ def inspect?
+ @inspect_mode.nil? or @inspect_mode
+ end
+
+ def file_input?
+ @io.class == FileInputMethod
+ end
+
+ def inspect_mode=(opt)
+ if opt
+ @inspect_mode = opt
+ else
+ @inspect_mode = !@inspect_mode
+ end
+ print "Switch to#{unless @inspect_mode; ' non';end} inspect mode.\n" if verbose?
+ @inspect_mode
+ end
+
+ def use_readline=(opt)
+ @use_readline = opt
+ print "use readline module\n" if @use_readline
+ end
+
+ def debug_level=(value)
+ @debug_level = value
+ RubyLex.debug_level = value
+ SLex.debug_level = value
+ end
+
+ def debug?
+ @debug_level > 0
+ end
+
+ def evaluate(line, line_no)
+ @line_no = line_no
+ set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
+# @workspace.evaluate("_ = IRB.conf[:MAIN_CONTEXT]._")
+# @_ = @workspace.evaluate(line, irb_path, line_no)
+ end
+
+ alias __exit__ exit
+ def exit(ret = 0)
+ IRB.irb_exit(@irb, ret)
+ end
+
+ NOPRINTING_IVARS = ["@last_value"]
+ NO_INSPECTING_IVARS = ["@irb", "@io"]
+ IDNAME_IVARS = ["@prompt_mode"]
+
+ alias __inspect__ inspect
+ def inspect
+ array = []
+ for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
+ ivar = ivar.to_s
+ name = ivar.sub(/^@(.*)$/, '\1')
+ val = instance_eval(ivar)
+ case ivar
+ when *NOPRINTING_IVARS
+ array.push format("conf.%s=%s", name, "...")
+ when *NO_INSPECTING_IVARS
+ array.push format("conf.%s=%s", name, val.to_s)
+ when *IDNAME_IVARS
+ array.push format("conf.%s=:%s", name, val.id2name)
+ else
+ array.push format("conf.%s=%s", name, val.inspect)
+ end
+ end
+ array.join("\n")
+ end
+ alias __to_s__ to_s
+ alias to_s inspect
+ end
+end
diff --git a/ruby/lib/irb/ext/change-ws.rb b/ruby/lib/irb/ext/change-ws.rb
new file mode 100644
index 0000000..2648579
--- /dev/null
+++ b/ruby/lib/irb/ext/change-ws.rb
@@ -0,0 +1,61 @@
+#
+# irb/ext/cb.rb -
+# $Release Version: 0.9.5$
+# $Revision: 20880 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+module IRB
+ class Context
+
+ def home_workspace
+ if defined? @home_workspace
+ @home_workspace
+ else
+ @home_workspace = @workspace
+ end
+ end
+
+ def change_workspace(*_main)
+ if _main.empty?
+ @workspace = home_workspace
+ return main
+ end
+
+ @workspace = WorkSpace.new(_main[0])
+
+ if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
+ main.extend ExtendCommandBundle
+ end
+ end
+
+# def change_binding(*_main)
+# back = @workspace
+# @workspace = WorkSpace.new(*_main)
+# unless _main.empty?
+# begin
+# main.extend ExtendCommandBundle
+# rescue
+# print "can't change binding to: ", main.inspect, "\n"
+# @workspace = back
+# return nil
+# end
+# end
+# @irb_level += 1
+# begin
+# catch(:SU_EXIT) do
+# @irb.eval_input
+# end
+# ensure
+# @irb_level -= 1
+# @workspace = back
+# end
+# end
+# alias change_workspace change_binding
+ end
+end
+
diff --git a/ruby/lib/irb/ext/history.rb b/ruby/lib/irb/ext/history.rb
new file mode 100644
index 0000000..953bbcf
--- /dev/null
+++ b/ruby/lib/irb/ext/history.rb
@@ -0,0 +1,109 @@
+#
+# history.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+module IRB
+
+ class Context
+
+ NOPRINTING_IVARS.push "@eval_history_values"
+
+ alias _set_last_value set_last_value
+
+ def set_last_value(value)
+ _set_last_value(value)
+
+# @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
+ if @eval_history #and !@eval_history_values.equal?(llv)
+ @eval_history_values.push @line_no, @last_value
+ @workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}"
+ end
+
+ @last_value
+ end
+
+ attr_reader :eval_history
+ def eval_history=(no)
+ if no
+ if defined?(@eval_history) && @eval_history
+ @eval_history_values.size(no)
+ else
+ @eval_history_values = History.new(no)
+ IRB.conf[:__TMP__EHV__] = @eval_history_values
+ @workspace.evaluate(self, "__ = IRB.conf[:__TMP__EHV__]")
+ IRB.conf.delete(:__TMP_EHV__)
+ end
+ else
+ @eval_history_values = nil
+ end
+ @eval_history = no
+ end
+ end
+
+ class History
+ @RCS_ID='-$Id: history.rb 14912 2008-01-06 15:49:38Z akr $-'
+
+ def initialize(size = 16)
+ @size = size
+ @contents = []
+ end
+
+ def size(size)
+ if size != 0 && size < @size
+ @contents = @contents[@size - size .. @size]
+ end
+ @size = size
+ end
+
+ def [](idx)
+ begin
+ if idx >= 0
+ @contents.find{|no, val| no == idx}[1]
+ else
+ @contents[idx][1]
+ end
+ rescue NameError
+ nil
+ end
+ end
+
+ def push(no, val)
+ @contents.push [no, val]
+ @contents.shift if @size != 0 && @contents.size > @size
+ end
+
+ alias real_inspect inspect
+
+ def inspect
+ if @contents.empty?
+ return real_inspect
+ end
+
+ unless (last = @contents.pop)[1].equal?(self)
+ @contents.push last
+ last = nil
+ end
+ str = @contents.collect{|no, val|
+ if val.equal?(self)
+ "#{no} ...self-history..."
+ else
+ "#{no} #{val.inspect}"
+ end
+ }.join("\n")
+ if str == ""
+ str = "Empty."
+ end
+ @contents.push last if last
+ str
+ end
+ end
+end
+
+
diff --git a/ruby/lib/irb/ext/loader.rb b/ruby/lib/irb/ext/loader.rb
new file mode 100644
index 0000000..6ea1877
--- /dev/null
+++ b/ruby/lib/irb/ext/loader.rb
@@ -0,0 +1,119 @@
+#
+# loader.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+
+module IRB
+ class LoadAbort < Exception;end
+
+ module IrbLoader
+ @RCS_ID='-$Id: loader.rb 14912 2008-01-06 15:49:38Z akr $-'
+
+ alias ruby_load load
+ alias ruby_require require
+
+ def irb_load(fn, priv = nil)
+ path = search_file_from_ruby_path(fn)
+ raise LoadError, "No such file to load -- #{fn}" unless path
+
+ load_file(path, priv)
+ end
+
+ def search_file_from_ruby_path(fn)
+ if /^#{Regexp.quote(File::Separator)}/ =~ fn
+ return fn if File.exist?(fn)
+ return nil
+ end
+
+ for path in $:
+ if File.exist?(f = File.join(path, fn))
+ return f
+ end
+ end
+ return nil
+ end
+
+ def source_file(path)
+ irb.suspend_name(path, File.basename(path)) do
+ irb.suspend_input_method(FileInputMethod.new(path)) do
+ |back_io|
+ irb.signal_status(:IN_LOAD) do
+ if back_io.kind_of?(FileInputMethod)
+ irb.eval_input
+ else
+ begin
+ irb.eval_input
+ rescue LoadAbort
+ print "load abort!!\n"
+ end
+ end
+ end
+ end
+ end
+ end
+
+ def load_file(path, priv = nil)
+ irb.suspend_name(path, File.basename(path)) do
+
+ if priv
+ ws = WorkSpace.new(Module.new)
+ else
+ ws = WorkSpace.new
+ end
+ irb.suspend_workspace(ws) do
+ irb.suspend_input_method(FileInputMethod.new(path)) do
+ |back_io|
+ irb.signal_status(:IN_LOAD) do
+# p irb.conf
+ if back_io.kind_of?(FileInputMethod)
+ irb.eval_input
+ else
+ begin
+ irb.eval_input
+ rescue LoadAbort
+ print "load abort!!\n"
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+
+ def old
+ back_io = @io
+ back_path = @irb_path
+ back_name = @irb_name
+ back_scanner = @irb.scanner
+ begin
+ @io = FileInputMethod.new(path)
+ @irb_name = File.basename(path)
+ @irb_path = path
+ @irb.signal_status(:IN_LOAD) do
+ if back_io.kind_of?(FileInputMethod)
+ @irb.eval_input
+ else
+ begin
+ @irb.eval_input
+ rescue LoadAbort
+ print "load abort!!\n"
+ end
+ end
+ end
+ ensure
+ @io = back_io
+ @irb_name = back_name
+ @irb_path = back_path
+ @irb.scanner = back_scanner
+ end
+ end
+ end
+end
+
diff --git a/ruby/lib/irb/ext/math-mode.rb b/ruby/lib/irb/ext/math-mode.rb
new file mode 100644
index 0000000..d2a2e92
--- /dev/null
+++ b/ruby/lib/irb/ext/math-mode.rb
@@ -0,0 +1,36 @@
+#
+# math-mode.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+require "mathn"
+
+module IRB
+ class Context
+ attr_reader :math_mode
+ alias math? math_mode
+
+ def math_mode=(opt)
+ if @math_mode == true && opt == false
+ IRB.fail CantReturnToNormalMode
+ return
+ end
+
+ @math_mode = opt
+ if math_mode
+ main.extend Math
+ print "start math mode\n" if verbose?
+ end
+ end
+
+ def inspect?
+ @inspect_mode.nil? && !@math_mode or @inspect_mode
+ end
+ end
+end
+
diff --git a/ruby/lib/irb/ext/multi-irb.rb b/ruby/lib/irb/ext/multi-irb.rb
new file mode 100644
index 0000000..01c8873
--- /dev/null
+++ b/ruby/lib/irb/ext/multi-irb.rb
@@ -0,0 +1,240 @@
+#
+# irb/multi-irb.rb - multiple irb module
+# $Release Version: 0.9.5$
+# $Revision: 18837 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+IRB.fail CantShiftToMultiIrbMode unless defined?(Thread)
+require "thread"
+
+module IRB
+ # job management class
+ class JobManager
+ @RCS_ID='-$Id: multi-irb.rb 18837 2008-08-25 13:41:11Z mame $-'
+
+ def initialize
+ # @jobs = [[thread, irb],...]
+ @jobs = []
+ @current_job = nil
+ end
+
+ attr_accessor :current_job
+
+ def n_jobs
+ @jobs.size
+ end
+
+ def thread(key)
+ th, irb = search(key)
+ th
+ end
+
+ def irb(key)
+ th, irb = search(key)
+ irb
+ end
+
+ def main_thread
+ @jobs[0][0]
+ end
+
+ def main_irb
+ @jobs[0][1]
+ end
+
+ def insert(irb)
+ @jobs.push [Thread.current, irb]
+ end
+
+ def switch(key)
+ th, irb = search(key)
+ IRB.fail IrbAlreadyDead unless th.alive?
+ IRB.fail IrbSwitchedToCurrentThread if th == Thread.current
+ @current_job = irb
+ th.run
+ Thread.stop
+ @current_job = irb(Thread.current)
+ end
+
+ def kill(*keys)
+ for key in keys
+ th, irb = search(key)
+ IRB.fail IrbAlreadyDead unless th.alive?
+ th.exit
+ end
+ end
+
+ def search(key)
+ job = case key
+ when Integer
+ @jobs[key]
+ when Irb
+ @jobs.find{|k, v| v.equal?(key)}
+ when Thread
+ @jobs.assoc(key)
+ else
+ @jobs.find{|k, v| v.context.main.equal?(key)}
+ end
+ IRB.fail NoSuchJob, key if job.nil?
+ job
+ end
+
+ def delete(key)
+ case key
+ when Integer
+ IRB.fail NoSuchJob, key unless @jobs[key]
+ @jobs[key] = nil
+ else
+ catch(:EXISTS) do
+ @jobs.each_index do
+ |i|
+ if @jobs[i] and (@jobs[i][0] == key ||
+ @jobs[i][1] == key ||
+ @jobs[i][1].context.main.equal?(key))
+ @jobs[i] = nil
+ throw :EXISTS
+ end
+ end
+ IRB.fail NoSuchJob, key
+ end
+ end
+ until assoc = @jobs.pop; end unless @jobs.empty?
+ @jobs.push assoc
+ end
+
+ def inspect
+ ary = []
+ @jobs.each_index do
+ |i|
+ th, irb = @jobs[i]
+ next if th.nil?
+
+ if th.alive?
+ if th.stop?
+ t_status = "stop"
+ else
+ t_status = "running"
+ end
+ else
+ t_status = "exited"
+ end
+ ary.push format("#%d->%s on %s (%s: %s)",
+ i,
+ irb.context.irb_name,
+ irb.context.main,
+ th,
+ t_status)
+ end
+ ary.join("\n")
+ end
+ end
+
+ @JobManager = JobManager.new
+
+ def IRB.JobManager
+ @JobManager
+ end
+
+ def IRB.CurrentContext
+ IRB.JobManager.irb(Thread.current).context
+ end
+
+ # invoke multi-irb
+ def IRB.irb(file = nil, *main)
+ workspace = WorkSpace.new(*main)
+ parent_thread = Thread.current
+ Thread.start do
+ begin
+ irb = Irb.new(workspace, file)
+ rescue
+ print "Subirb can't start with context(self): ", workspace.main.inspect, "\n"
+ print "return to main irb\n"
+ Thread.pass
+ Thread.main.wakeup
+ Thread.exit
+ end
+ @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
+ @JobManager.insert(irb)
+ @JobManager.current_job = irb
+ begin
+ system_exit = false
+ catch(:IRB_EXIT) do
+ irb.eval_input
+ end
+ rescue SystemExit
+ system_exit = true
+ raise
+ #fail
+ ensure
+ unless system_exit
+ @JobManager.delete(irb)
+ if parent_thread.alive?
+ @JobManager.current_job = @JobManager.irb(parent_thread)
+ parent_thread.run
+ else
+ @JobManager.current_job = @JobManager.main_irb
+ @JobManager.main_thread.run
+ end
+ end
+ end
+ end
+ Thread.stop
+ @JobManager.current_job = @JobManager.irb(Thread.current)
+ end
+
+# class Context
+# def set_last_value(value)
+# @last_value = value
+# @workspace.evaluate "_ = IRB.JobManager.irb(Thread.current).context.last_value"
+# if @eval_history #and !@__.equal?(@last_value)
+# @eval_history_values.push @line_no, @last_value
+# @workspace.evaluate "__ = IRB.JobManager.irb(Thread.current).context.instance_eval{@eval_history_values}"
+# end
+# @last_value
+# end
+# end
+
+# module ExtendCommand
+# def irb_context
+# IRB.JobManager.irb(Thread.current).context
+# end
+# # alias conf irb_context
+# end
+
+ @CONF[:SINGLE_IRB_MODE] = false
+ @JobManager.insert(@CONF[:MAIN_CONTEXT].irb)
+ @JobManager.current_job = @CONF[:MAIN_CONTEXT].irb
+
+ class Irb
+ def signal_handle
+ unless @context.ignore_sigint?
+ print "\nabort!!\n" if @context.verbose?
+ exit
+ end
+
+ case @signal_status
+ when :IN_INPUT
+ print "^C\n"
+ IRB.JobManager.thread(self).raise RubyLex::TerminateLineInput
+ when :IN_EVAL
+ IRB.irb_abort(self)
+ when :IN_LOAD
+ IRB.irb_abort(self, LoadAbort)
+ when :IN_IRB
+ # ignore
+ else
+ # ignore other cases as well
+ end
+ end
+ end
+
+ trap("SIGINT") do
+ @JobManager.current_job.signal_handle
+ Thread.stop
+ end
+
+end
diff --git a/ruby/lib/irb/ext/save-history.rb b/ruby/lib/irb/ext/save-history.rb
new file mode 100644
index 0000000..9e48c43
--- /dev/null
+++ b/ruby/lib/irb/ext/save-history.rb
@@ -0,0 +1,100 @@
+#!/usr/local/bin/ruby
+#
+# save-history.rb -
+# $Release Version: 0.9.5$
+# $Revision: 24294 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "readline"
+
+module IRB
+ module HistorySavingAbility
+ @RCS_ID='-$Id: save-history.rb 24294 2009-07-26 15:33:29Z yugui $-'
+ end
+
+ class Context
+ def init_save_history
+ unless (class<<@io;self;end).include?(HistorySavingAbility)
+ @io.extend(HistorySavingAbility)
+ end
+ end
+
+ def save_history
+ IRB.conf[:SAVE_HISTORY]
+ end
+
+ def save_history=(val)
+ IRB.conf[:SAVE_HISTORY] = val
+ if val
+ main_context = IRB.conf[:MAIN_CONTEXT]
+ main_context = self unless main_context
+ main_context.init_save_history
+ end
+ end
+
+ def history_file
+ IRB.conf[:HISTORY_FILE]
+ end
+
+ def history_file=(hist)
+ IRB.conf[:HISTORY_FILE] = hist
+ end
+ end
+
+ module HistorySavingAbility
+ include Readline
+
+# def HistorySavingAbility.create_finalizer
+# proc do
+# if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
+# if hf = IRB.conf[:HISTORY_FILE]
+# file = File.expand_path(hf)
+# end
+# file = IRB.rc_file("_history") unless file
+# open(file, 'w' ) do |f|
+# hist = HISTORY.to_a
+# f.puts(hist[-num..-1] || hist)
+# end
+# end
+# end
+# end
+
+ def HistorySavingAbility.extended(obj)
+# ObjectSpace.define_finalizer(obj, HistorySavingAbility.create_finalizer)
+ IRB.conf[:AT_EXIT].push proc{obj.save_history}
+ obj.load_history
+ obj
+ end
+
+ def load_history
+ if history_file = IRB.conf[:HISTORY_FILE]
+ history_file = File.expand_path(history_file)
+ end
+ history_file = IRB.rc_file("_history") unless history_file
+ if File.exist?(history_file)
+ open(history_file) do |f|
+ f.each {|l| HISTORY << l.chomp}
+ end
+ end
+ end
+
+ def save_history
+ if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
+ if history_file = IRB.conf[:HISTORY_FILE]
+ history_file = File.expand_path(history_file)
+ end
+ history_file = IRB.rc_file("_history") unless history_file
+ open(history_file, 'w' ) do |f|
+ hist = HISTORY.to_a
+ f.puts(hist[-num..-1] || hist)
+ end
+ end
+ end
+ end
+end
+
diff --git a/ruby/lib/irb/ext/tracer.rb b/ruby/lib/irb/ext/tracer.rb
new file mode 100644
index 0000000..e9a8d5c
--- /dev/null
+++ b/ruby/lib/irb/ext/tracer.rb
@@ -0,0 +1,60 @@
+#
+# irb/lib/tracer.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+require "tracer"
+
+module IRB
+
+ # initialize tracing function
+ def IRB.initialize_tracer
+ Tracer.verbose = false
+ Tracer.add_filter {
+ |event, file, line, id, binding, *rests|
+ /^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and
+ File::basename(file) != "irb.rb"
+ }
+ end
+
+ class Context
+ attr_reader :use_tracer
+ alias use_tracer? use_tracer
+
+ def use_tracer=(opt)
+ if opt
+ Tracer.set_get_line_procs(@irb_path) {
+ |line_no, *rests|
+ @io.line(line_no)
+ }
+ elsif !opt && @use_tracer
+ Tracer.off
+ end
+ @use_tracer=opt
+ end
+ end
+
+ class WorkSpace
+ alias __evaluate__ evaluate
+ def evaluate(context, statements, file = nil, line = nil)
+ if context.use_tracer? && file != nil && line != nil
+ Tracer.on
+ begin
+ __evaluate__(context, statements, file, line)
+ ensure
+ Tracer.off
+ end
+ else
+ __evaluate__(context, statements, file || __FILE__, line || __LINE__)
+ end
+ end
+ end
+
+ IRB.initialize_tracer
+end
+
diff --git a/ruby/lib/irb/ext/use-loader.rb b/ruby/lib/irb/ext/use-loader.rb
new file mode 100644
index 0000000..cea727b
--- /dev/null
+++ b/ruby/lib/irb/ext/use-loader.rb
@@ -0,0 +1,64 @@
+#
+# use-loader.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "irb/cmd/load"
+require "irb/ext/loader"
+
+class Object
+ alias __original__load__IRB_use_loader__ load
+ alias __original__require__IRB_use_loader__ require
+end
+
+module IRB
+ module ExtendCommandBundle
+ def irb_load(*opts, &b)
+ ExtendCommand::Load.execute(irb_context, *opts, &b)
+ end
+ def irb_require(*opts, &b)
+ ExtendCommand::Require.execute(irb_context, *opts, &b)
+ end
+ end
+
+ class Context
+
+ IRB.conf[:USE_LOADER] = false
+
+ def use_loader
+ IRB.conf[:USE_LOADER]
+ end
+
+ alias use_loader? use_loader
+
+ def use_loader=(opt)
+
+ if IRB.conf[:USE_LOADER] != opt
+ IRB.conf[:USE_LOADER] = opt
+ if opt
+ if !$".include?("irb/cmd/load")
+ end
+ (class<<@workspace.main;self;end).instance_eval {
+ alias_method :load, :irb_load
+ alias_method :require, :irb_require
+ }
+ else
+ (class<<@workspace.main;self;end).instance_eval {
+ alias_method :load, :__original__load__IRB_use_loader__
+ alias_method :require, :__original__require__IRB_use_loader__
+ }
+ end
+ end
+ print "Switch to load/require#{unless use_loader; ' non';end} trace mode.\n" if verbose?
+ opt
+ end
+ end
+end
+
+
diff --git a/ruby/lib/irb/ext/workspaces.rb b/ruby/lib/irb/ext/workspaces.rb
new file mode 100644
index 0000000..a68e7d5
--- /dev/null
+++ b/ruby/lib/irb/ext/workspaces.rb
@@ -0,0 +1,55 @@
+#
+# push-ws.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+module IRB
+ class Context
+
+ def irb_level
+ workspace_stack.size
+ end
+
+ def workspaces
+ if defined? @workspaces
+ @workspaces
+ else
+ @workspaces = []
+ end
+ end
+
+ def push_workspace(*_main)
+ if _main.empty?
+ if workspaces.empty?
+ print "No other workspace\n"
+ return nil
+ end
+ ws = workspaces.pop
+ workspaces.push @workspace
+ @workspace = ws
+ return workspaces
+ end
+
+ workspaces.push @workspace
+ @workspace = WorkSpace.new(@workspace.binding, _main[0])
+ if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
+ main.extend ExtendCommandBundle
+ end
+ end
+
+ def pop_workspace
+ if workspaces.empty?
+ print "workspace stack empty\n"
+ return
+ end
+ @workspace = workspaces.pop
+ end
+ end
+end
+
diff --git a/ruby/lib/irb/extend-command.rb b/ruby/lib/irb/extend-command.rb
new file mode 100644
index 0000000..2d91f45
--- /dev/null
+++ b/ruby/lib/irb/extend-command.rb
@@ -0,0 +1,272 @@
+#
+# irb/extend-command.rb - irb extend command
+# $Release Version: 0.9.5$
+# $Revision: 26021 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+module IRB
+ #
+ # IRB extended command
+ #
+ module ExtendCommandBundle
+ EXCB = ExtendCommandBundle
+
+ NO_OVERRIDE = 0
+ OVERRIDE_PRIVATE_ONLY = 0x01
+ OVERRIDE_ALL = 0x02
+
+ def irb_exit(ret = 0)
+ irb_context.exit(ret)
+ end
+
+ def irb_context
+ IRB.CurrentContext
+ end
+
+ @ALIASES = [
+ [:context, :irb_context, NO_OVERRIDE],
+ [:conf, :irb_context, NO_OVERRIDE],
+ [:irb_quit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
+ [:exit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
+ [:quit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
+ ]
+
+ @EXTEND_COMMANDS = [
+ [:irb_current_working_workspace, :CurrentWorkingWorkspace, "irb/cmd/chws",
+ [:irb_print_working_workspace, OVERRIDE_ALL],
+ [:irb_cwws, OVERRIDE_ALL],
+ [:irb_pwws, OVERRIDE_ALL],
+# [:irb_cww, OVERRIDE_ALL],
+# [:irb_pww, OVERRIDE_ALL],
+ [:cwws, NO_OVERRIDE],
+ [:pwws, NO_OVERRIDE],
+# [:cww, NO_OVERRIDE],
+# [:pww, NO_OVERRIDE],
+ [:irb_current_working_binding, OVERRIDE_ALL],
+ [:irb_print_working_binding, OVERRIDE_ALL],
+ [:irb_cwb, OVERRIDE_ALL],
+ [:irb_pwb, OVERRIDE_ALL],
+# [:cwb, NO_OVERRIDE],
+# [:pwb, NO_OVERRIDE]
+ ],
+ [:irb_change_workspace, :ChangeWorkspace, "irb/cmd/chws",
+ [:irb_chws, OVERRIDE_ALL],
+# [:irb_chw, OVERRIDE_ALL],
+ [:irb_cws, OVERRIDE_ALL],
+# [:irb_cw, OVERRIDE_ALL],
+ [:chws, NO_OVERRIDE],
+# [:chw, NO_OVERRIDE],
+ [:cws, NO_OVERRIDE],
+# [:cw, NO_OVERRIDE],
+ [:irb_change_binding, OVERRIDE_ALL],
+ [:irb_cb, OVERRIDE_ALL],
+ [:cb, NO_OVERRIDE]],
+
+ [:irb_workspaces, :Workspaces, "irb/cmd/pushws",
+ [:workspaces, NO_OVERRIDE],
+ [:irb_bindings, OVERRIDE_ALL],
+ [:bindings, NO_OVERRIDE]],
+ [:irb_push_workspace, :PushWorkspace, "irb/cmd/pushws",
+ [:irb_pushws, OVERRIDE_ALL],
+# [:irb_pushw, OVERRIDE_ALL],
+ [:pushws, NO_OVERRIDE],
+# [:pushw, NO_OVERRIDE],
+ [:irb_push_binding, OVERRIDE_ALL],
+ [:irb_pushb, OVERRIDE_ALL],
+ [:pushb, NO_OVERRIDE]],
+ [:irb_pop_workspace, :PopWorkspace, "irb/cmd/pushws",
+ [:irb_popws, OVERRIDE_ALL],
+# [:irb_popw, OVERRIDE_ALL],
+ [:popws, NO_OVERRIDE],
+# [:popw, NO_OVERRIDE],
+ [:irb_pop_binding, OVERRIDE_ALL],
+ [:irb_popb, OVERRIDE_ALL],
+ [:popb, NO_OVERRIDE]],
+
+ [:irb_load, :Load, "irb/cmd/load"],
+ [:irb_require, :Require, "irb/cmd/load"],
+ [:irb_source, :Source, "irb/cmd/load",
+ [:source, NO_OVERRIDE]],
+
+ [:irb, :IrbCommand, "irb/cmd/subirb"],
+ [:irb_jobs, :Jobs, "irb/cmd/subirb",
+ [:jobs, NO_OVERRIDE]],
+ [:irb_fg, :Foreground, "irb/cmd/subirb",
+ [:fg, NO_OVERRIDE]],
+ [:irb_kill, :Kill, "irb/cmd/subirb",
+ [:kill, OVERRIDE_PRIVATE_ONLY]],
+
+ [:irb_help, :Help, "irb/cmd/help",
+ [:help, NO_OVERRIDE]],
+
+ ]
+
+ def self.install_extend_commands
+ for args in @EXTEND_COMMANDS
+ def_extend_command(*args)
+ end
+ end
+
+ # aliases = [commands_alias, flag], ...
+ def self.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases)
+ case cmd_class
+ when Symbol
+ cmd_class = cmd_class.id2name
+ when String
+ when Class
+ cmd_class = cmd_class.name
+ end
+
+ if load_file
+ eval <<-"EOS", binding, __FILE__, __LINE__+1
+ def #{cmd_name}(*opts, &b)
+ require "#{load_file}"
+ arity = ExtendCommand::#{cmd_class}.instance_method(:execute).arity
+ args = []
+ if arity < 0
+ args << "*opts"
+ arity = -arity - 1
+ end
+ args.unshift *(1..arity).map {|i| "arg" + i.to_s }
+ args << "&block"
+ args = args.join(", ")
+ eval <<-"EOS2", binding, __FILE__, __LINE__+1
+ def #{cmd_name}(\#{args})
+ ExtendCommand::#{cmd_class}.execute(irb_context, \#{args})
+ end
+ EOS2
+ send :#{cmd_name}, *opts, &b
+ end
+ EOS
+ else
+ eval <<-"EOS", binding, __FILE__, __LINE__+1
+ def #{cmd_name}(*opts, &b)
+ ExtendCommand::#{cmd_class}.execute(irb_context, *opts, &b)
+ end
+ EOS
+ end
+
+ for ali, flag in aliases
+ @ALIASES.push [ali, cmd_name, flag]
+ end
+ end
+
+ # override = {NO_OVERRIDE, OVERRIDE_PRIVATE_ONLY, OVERRIDE_ALL}
+ def install_alias_method(to, from, override = NO_OVERRIDE)
+ to = to.id2name unless to.kind_of?(String)
+ from = from.id2name unless from.kind_of?(String)
+
+ if override == OVERRIDE_ALL or
+ (override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or
+ (override == NO_OVERRIDE) && !respond_to?(to, true)
+ target = self
+ (class<<self;self;end).instance_eval{
+ if target.respond_to?(to, true) &&
+ !target.respond_to?(EXCB.irb_original_method_name(to), true)
+ alias_method(EXCB.irb_original_method_name(to), to)
+ end
+ alias_method to, from
+ }
+ else
+ print "irb: warn: can't alias #{to} from #{from}.\n"
+ end
+ end
+
+ def self.irb_original_method_name(method_name)
+ "irb_" + method_name + "_org"
+ end
+
+ def self.extend_object(obj)
+ unless (class<<obj;ancestors;end).include?(EXCB)
+ super
+ for ali, com, flg in @ALIASES
+ obj.install_alias_method(ali, com, flg)
+ end
+ end
+ end
+
+ install_extend_commands
+ end
+
+ # extension support for Context
+ module ContextExtender
+ CE = ContextExtender
+
+ @EXTEND_COMMANDS = [
+ [:eval_history=, "irb/ext/history.rb"],
+ [:use_tracer=, "irb/ext/tracer.rb"],
+ [:math_mode=, "irb/ext/math-mode.rb"],
+ [:use_loader=, "irb/ext/use-loader.rb"],
+ [:save_history=, "irb/ext/save-history.rb"],
+ ]
+
+ def self.install_extend_commands
+ for args in @EXTEND_COMMANDS
+ def_extend_command(*args)
+ end
+ end
+
+ def self.def_extend_command(cmd_name, load_file, *aliases)
+ Context.module_eval <<-"EOS", __FILE__, __LINE__+1
+ def #{cmd_name}(*opts, &b)
+ Context.module_eval {remove_method(:#{cmd_name})}
+ require "#{load_file}"
+ send :#{cmd_name}, *opts, &b
+ end
+ for ali in aliases
+ alias_method ali, cmd_name
+ end
+ EOS
+ end
+
+ CE.install_extend_commands
+ end
+
+ module MethodExtender
+ def def_pre_proc(base_method, extend_method)
+ base_method = base_method.to_s
+ extend_method = extend_method.to_s
+
+ alias_name = new_alias_name(base_method)
+ module_eval <<-"EOS", __FILE__, __LINE__+1
+ alias_method alias_name, base_method
+ def #{base_method}(*opts)
+ send :#{extend_method}, *opts
+ send :#{alias_name}, *opts
+ end
+ EOS
+ end
+
+ def def_post_proc(base_method, extend_method)
+ base_method = base_method.to_s
+ extend_method = extend_method.to_s
+
+ alias_name = new_alias_name(base_method)
+ module_eval <<-"EOS", __FLIE__, __LINE__+1
+ alias_method alias_name, base_method
+ def #{base_method}(*opts)
+ send :#{alias_name}, *opts
+ send :#{extend_method}, *opts
+ end
+ EOS
+ end
+
+ # return #{prefix}#{name}#{postfix}<num>
+ def new_alias_name(name, prefix = "__alias_of__", postfix = "__")
+ base_name = "#{prefix}#{name}#{postfix}"
+ all_methods = instance_methods(true) + private_instance_methods(true)
+ same_methods = all_methods.grep(/^#{Regexp.quote(base_name)}[0-9]*$/)
+ return base_name if same_methods.empty?
+ no = same_methods.size
+ while !same_methods.include?(alias_name = base_name + no)
+ no += 1
+ end
+ alias_name
+ end
+ end
+end
+
diff --git a/ruby/lib/irb/frame.rb b/ruby/lib/irb/frame.rb
new file mode 100644
index 0000000..fed0521
--- /dev/null
+++ b/ruby/lib/irb/frame.rb
@@ -0,0 +1,66 @@
+#
+# frame.rb -
+# $Release Version: 0.9$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+
+module IRB
+ class Frame
+ extend Exception2MessageMapper
+ def_exception :FrameOverflow, "frame overflow"
+ def_exception :FrameUnderflow, "frame underflow"
+
+ INIT_STACK_TIMES = 3
+ CALL_STACK_OFFSET = 3
+
+ def initialize
+ @frames = [TOPLEVEL_BINDING] * INIT_STACK_TIMES
+ end
+
+ def trace_func(event, file, line, id, binding)
+ case event
+ when 'call', 'class'
+ @frames.push binding
+ when 'return', 'end'
+ @frames.pop
+ end
+ end
+
+ def top(n = 0)
+ bind = @frames[-(n + CALL_STACK_OFFSET)]
+ Fail FrameUnderflow unless bind
+ bind
+ end
+
+ def bottom(n = 0)
+ bind = @frames[n]
+ Fail FrameOverflow unless bind
+ bind
+ end
+
+ # singleton functions
+ def Frame.bottom(n = 0)
+ @backtrace.bottom(n)
+ end
+
+ def Frame.top(n = 0)
+ @backtrace.top(n)
+ end
+
+ def Frame.sender
+ eval "self", @backtrace.top
+ end
+
+ @backtrace = Frame.new
+ set_trace_func proc{|event, file, line, id, binding, klass|
+ @backtrace.trace_func(event, file, line, id, binding)
+ }
+ end
+end
diff --git a/ruby/lib/irb/help.rb b/ruby/lib/irb/help.rb
new file mode 100644
index 0000000..a893b9f
--- /dev/null
+++ b/ruby/lib/irb/help.rb
@@ -0,0 +1,35 @@
+#
+# irb/help.rb - print usage module
+# $Release Version: 0.9.5$
+# $Revision: 20882 $
+# by Keiju ISHITSUKA(keiju@ishitsuka.com)
+#
+# --
+#
+#
+#
+
+require 'irb/magic-file'
+
+module IRB
+ def IRB.print_usage
+ lc = IRB.conf[:LC_MESSAGES]
+ path = lc.find("irb/help-message")
+ space_line = false
+ IRB::MagicFile.open(path){|f|
+ f.each_line do |l|
+ if /^\s*$/ =~ l
+ lc.puts l unless space_line
+ space_line = true
+ next
+ end
+ space_line = false
+
+ l.sub!(/#.*$/, "")
+ next if /^\s*$/ =~ l
+ lc.puts l
+ end
+ }
+ end
+end
+
diff --git a/ruby/lib/irb/init.rb b/ruby/lib/irb/init.rb
new file mode 100644
index 0000000..c0659ac
--- /dev/null
+++ b/ruby/lib/irb/init.rb
@@ -0,0 +1,288 @@
+#
+# irb/init.rb - irb initialize module
+# $Release Version: 0.9.5$
+# $Revision: 24294 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+module IRB
+
+ # initialize config
+ def IRB.setup(ap_path)
+ IRB.init_config(ap_path)
+ IRB.init_error
+ IRB.parse_opts
+ IRB.run_config
+ IRB.load_modules
+
+ unless @CONF[:PROMPT][@CONF[:PROMPT_MODE]]
+ IRB.fail(UndefinedPromptMode, @CONF[:PROMPT_MODE])
+ end
+ end
+
+ # @CONF default setting
+ def IRB.init_config(ap_path)
+ # class instance variables
+ @TRACER_INITIALIZED = false
+
+ # default configurations
+ unless ap_path and @CONF[:AP_NAME]
+ ap_path = File.join(File.dirname(File.dirname(__FILE__)), "irb.rb")
+ end
+ @CONF[:AP_NAME] = File::basename(ap_path, ".rb")
+
+ @CONF[:IRB_NAME] = "irb"
+ @CONF[:IRB_LIB_PATH] = File.dirname(__FILE__)
+
+ @CONF[:RC] = true
+ @CONF[:LOAD_MODULES] = []
+ @CONF[:IRB_RC] = nil
+
+ @CONF[:MATH_MODE] = false
+ @CONF[:USE_READLINE] = false unless defined?(ReadlineInputMethod)
+ @CONF[:INSPECT_MODE] = nil
+ @CONF[:USE_TRACER] = false
+ @CONF[:USE_LOADER] = false
+ @CONF[:IGNORE_SIGINT] = true
+ @CONF[:IGNORE_EOF] = false
+ @CONF[:ECHO] = nil
+ @CONF[:VERBOSE] = nil
+
+ @CONF[:EVAL_HISTORY] = nil
+ @CONF[:SAVE_HISTORY] = nil
+
+ @CONF[:BACK_TRACE_LIMIT] = 16
+
+ @CONF[:PROMPT] = {
+ :NULL => {
+ :PROMPT_I => nil,
+ :PROMPT_N => nil,
+ :PROMPT_S => nil,
+ :PROMPT_C => nil,
+ :RETURN => "%s\n"
+ },
+ :DEFAULT => {
+ :PROMPT_I => "%N(%m):%03n:%i> ",
+ :PROMPT_N => "%N(%m):%03n:%i> ",
+ :PROMPT_S => "%N(%m):%03n:%i%l ",
+ :PROMPT_C => "%N(%m):%03n:%i* ",
+ :RETURN => "=> %s\n"
+ },
+ :CLASSIC => {
+ :PROMPT_I => "%N(%m):%03n:%i> ",
+ :PROMPT_N => "%N(%m):%03n:%i> ",
+ :PROMPT_S => "%N(%m):%03n:%i%l ",
+ :PROMPT_C => "%N(%m):%03n:%i* ",
+ :RETURN => "%s\n"
+ },
+ :SIMPLE => {
+ :PROMPT_I => ">> ",
+ :PROMPT_N => ">> ",
+ :PROMPT_S => nil,
+ :PROMPT_C => "?> ",
+ :RETURN => "=> %s\n"
+ },
+ :INF_RUBY => {
+ :PROMPT_I => "%N(%m):%03n:%i> ",
+# :PROMPT_N => "%N(%m):%03n:%i> ",
+ :PROMPT_N => nil,
+ :PROMPT_S => nil,
+ :PROMPT_C => nil,
+ :RETURN => "%s\n",
+ :AUTO_INDENT => true
+ },
+ :XMP => {
+ :PROMPT_I => nil,
+ :PROMPT_N => nil,
+ :PROMPT_S => nil,
+ :PROMPT_C => nil,
+ :RETURN => " ==>%s\n"
+ }
+ }
+
+ @CONF[:PROMPT_MODE] = (STDIN.tty? ? :DEFAULT : :NULL)
+ @CONF[:AUTO_INDENT] = false
+
+ @CONF[:CONTEXT_MODE] = 3 # use binding in function on TOPLEVEL_BINDING
+ @CONF[:SINGLE_IRB] = false
+
+# @CONF[:LC_MESSAGES] = "en"
+ @CONF[:LC_MESSAGES] = Locale.new
+
+ @CONF[:AT_EXIT] = []
+
+ @CONF[:DEBUG_LEVEL] = 1
+ end
+
+ def IRB.init_error
+ @CONF[:LC_MESSAGES].load("irb/error.rb")
+ end
+
+ FEATURE_IOPT_CHANGE_VERSION = "1.9.0"
+
+ # option analyzing
+ def IRB.parse_opts
+ load_path = []
+ while opt = ARGV.shift
+ case opt
+ when "-f"
+ @CONF[:RC] = false
+ when "-m"
+ @CONF[:MATH_MODE] = true
+ when "-d"
+ $DEBUG = true
+ when /^-r(.+)?/
+ opt = $1 || ARGV.shift
+ @CONF[:LOAD_MODULES].push opt if opt
+ when /^-I(.+)?/
+ opt = $1 || ARGV.shift
+ load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt
+ when '-U'
+ set_encoding("UTF-8", "UTF-8")
+ when /^-E(.+)?/, /^--encoding(?:=(.+))?/
+ opt = $1 || ARGV.shift
+ set_encoding(*opt.split(':', 2))
+ when "--inspect"
+ @CONF[:INSPECT_MODE] = true
+ when "--noinspect"
+ @CONF[:INSPECT_MODE] = false
+ when "--readline"
+ @CONF[:USE_READLINE] = true
+ when "--noreadline"
+ @CONF[:USE_READLINE] = false
+ when "--echo"
+ @CONF[:ECHO] = true
+ when "--noecho"
+ @CONF[:ECHO] = false
+ when "--verbose"
+ @CONF[:VERBOSE] = true
+ when "--noverbose"
+ @CONF[:VERBOSE] = false
+ when /^--prompt-mode(?:=(.+))?/, /^--prompt(?:=(.+))?/
+ opt = $1 || ARGV.shift
+ prompt_mode = opt.upcase.tr("-", "_").intern
+ @CONF[:PROMPT_MODE] = prompt_mode
+ when "--noprompt"
+ @CONF[:PROMPT_MODE] = :NULL
+ when "--inf-ruby-mode"
+ @CONF[:PROMPT_MODE] = :INF_RUBY
+ when "--sample-book-mode", "--simple-prompt"
+ @CONF[:PROMPT_MODE] = :SIMPLE
+ when "--tracer"
+ @CONF[:USE_TRACER] = true
+ when /^--back-trace-limit(?:=(.+))?/
+ @CONF[:BACK_TRACE_LIMIT] = ($1 || ARGV.shift).to_i
+ when /^--context-mode(?:=(.+))?/
+ @CONF[:CONTEXT_MODE] = ($1 || ARGV.shift).to_i
+ when "--single-irb"
+ @CONF[:SINGLE_IRB] = true
+ when /^--irb_debug=(?:=(.+))?/
+ @CONF[:DEBUG_LEVEL] = ($1 || ARGV.shift).to_i
+ when "-v", "--version"
+ print IRB.version, "\n"
+ exit 0
+ when "-h", "--help"
+ require "irb/help"
+ IRB.print_usage
+ exit 0
+ when "--"
+ if opt = ARGV.shfit
+ @CONF[:SCRIPT] = opt
+ $0 = opt
+ end
+ break
+ when /^-/
+ IRB.fail UnrecognizedSwitch, opt
+ else
+ @CONF[:SCRIPT] = opt
+ $0 = opt
+ break
+ end
+ end
+ if RUBY_VERSION >= FEATURE_IOPT_CHANGE_VERSION
+ load_path.collect! do |path|
+ /\A\.\// =~ path ? path : File.expand_path(path)
+ end
+ end
+ $LOAD_PATH.unshift(*load_path)
+
+ end
+
+ # running config
+ def IRB.run_config
+ if @CONF[:RC]
+ begin
+ load rc_file
+ rescue LoadError, Errno::ENOENT
+ rescue # StandardError, ScriptError
+ print "load error: #{rc_file}\n"
+ print $!.class, ": ", $!, "\n"
+ for err in $@[0, $@.size - 2]
+ print "\t", err, "\n"
+ end
+ end
+ end
+ end
+
+ IRBRC_EXT = "rc"
+ def IRB.rc_file(ext = IRBRC_EXT)
+ if !@CONF[:RC_NAME_GENERATOR]
+ rc_file_generators do |rcgen|
+ @CONF[:RC_NAME_GENERATOR] ||= rcgen
+ if File.exist?(rcgen.call(IRBRC_EXT))
+ @CONF[:RC_NAME_GENERATOR] = rcgen
+ break
+ end
+ end
+ end
+ @CONF[:RC_NAME_GENERATOR].call ext
+ end
+
+ # enumerate possible rc-file base name generators
+ def IRB.rc_file_generators
+ if irbrc = ENV["IRBRC"]
+ yield proc{|rc| rc == "rc" ? irbrc : irbrc+rc}
+ end
+ if home = ENV["HOME"]
+ yield proc{|rc| home+"/.irb#{rc}"}
+ end
+ home = Dir.pwd
+ yield proc{|rc| home+"/.irb#{rc}"}
+ yield proc{|rc| home+"/irb#{rc.sub(/\A_?/, '.')}"}
+ yield proc{|rc| home+"/_irb#{rc}"}
+ yield proc{|rc| home+"/$irb#{rc}"}
+ end
+
+ # loading modules
+ def IRB.load_modules
+ for m in @CONF[:LOAD_MODULES]
+ begin
+ require m
+ rescue LoadError => err
+ warn err.backtrace[0] << ":#{err.class}: #{err}"
+ end
+ end
+ end
+
+
+ DefaultEncodings = Struct.new(:external, :internal)
+ class << IRB
+ private
+ def set_encoding(extern, intern = nil)
+ verbose, $VERBOSE = $VERBOSE, nil
+ Encoding.default_external = extern unless extern.nil? || extern.empty?
+ Encoding.default_internal = intern unless intern.nil? || intern.empty?
+ @CONF[:ENCODINGS] = IRB::DefaultEncodings.new(extern, intern)
+ [$stdin, $stdout, $stderr].each do |io|
+ io.set_encoding(extern, intern)
+ end
+ @CONF[:LC_MESSAGES].instance_variable_set(:@encoding, extern)
+ ensure
+ $VERBOSE = verbose
+ end
+ end
+end
diff --git a/ruby/lib/irb/input-method.rb b/ruby/lib/irb/input-method.rb
new file mode 100644
index 0000000..cc022ca
--- /dev/null
+++ b/ruby/lib/irb/input-method.rb
@@ -0,0 +1,142 @@
+#
+# irb/input-method.rb - input methods used irb
+# $Release Version: 0.9.5$
+# $Revision: 21546 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+require 'irb/src_encoding'
+require 'irb/magic-file'
+
+module IRB
+ #
+ # InputMethod
+ # StdioInputMethod
+ # FileInputMethod
+ # (ReadlineInputMethod)
+ #
+ STDIN_FILE_NAME = "(line)"
+ class InputMethod
+ @RCS_ID='-$Id: input-method.rb 21546 2009-01-15 15:36:57Z yugui $-'
+
+ def initialize(file = STDIN_FILE_NAME)
+ @file_name = file
+ end
+ attr_reader :file_name
+
+ attr_accessor :prompt
+
+ def gets
+ IRB.fail NotImplementedError, "gets"
+ end
+ public :gets
+
+ def readable_atfer_eof?
+ false
+ end
+ end
+
+ class StdioInputMethod < InputMethod
+ def initialize
+ super
+ @line_no = 0
+ @line = []
+ @stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
+ @stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
+ end
+
+ def gets
+ print @prompt
+ line = @stdin.gets
+ @line[@line_no += 1] = line
+ end
+
+ def eof?
+ @stdin.eof?
+ end
+
+ def readable_atfer_eof?
+ true
+ end
+
+ def line(line_no)
+ @line[line_no]
+ end
+
+ def encoding
+ @stdin.external_encoding
+ end
+ end
+
+ class FileInputMethod < InputMethod
+ def initialize(file)
+ super
+ @io = IRB::MagicFile.open(file)
+ end
+ attr_reader :file_name
+
+ def eof?
+ @io.eof?
+ end
+
+ def gets
+ print @prompt
+ l = @io.gets
+# print @prompt, l
+ l
+ end
+
+ def encoding
+ @io.external_encoding
+ end
+ end
+
+ begin
+ require "readline"
+ class ReadlineInputMethod < InputMethod
+ include Readline
+ def initialize
+ super
+
+ @line_no = 0
+ @line = []
+ @eof = false
+
+ @stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
+ @stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
+ end
+
+ def gets
+ Readline.input = @stdin
+ Readline.output = @stdout
+ if l = readline(@prompt, false)
+ HISTORY.push(l) if !l.empty?
+ @line[@line_no += 1] = l + "\n"
+ else
+ @eof = true
+ l
+ end
+ end
+
+ def eof?
+ @eof
+ end
+
+ def readable_atfer_eof?
+ true
+ end
+
+ def line(line_no)
+ @line[line_no]
+ end
+
+ def encoding
+ @stdin.external_encoding
+ end
+ end
+ rescue LoadError
+ end
+end
diff --git a/ruby/lib/irb/lc/error.rb b/ruby/lib/irb/lc/error.rb
new file mode 100644
index 0000000..b76bb9d
--- /dev/null
+++ b/ruby/lib/irb/lc/error.rb
@@ -0,0 +1,29 @@
+#
+# irb/lc/error.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+require "e2mmap"
+
+module IRB
+
+ # exceptions
+ extend Exception2MessageMapper
+ def_exception :UnrecognizedSwitch, "Unrecognized switch: %s"
+ def_exception :NotImplementedError, "Need to define `%s'"
+ def_exception :CantReturnToNormalMode, "Can't return to normal mode."
+ def_exception :IllegalParameter, "Invalid parameter(%s)."
+ def_exception :IrbAlreadyDead, "Irb is already dead."
+ def_exception :IrbSwitchedToCurrentThread, "Switched to current thread."
+ def_exception :NoSuchJob, "No such job(%s)."
+ def_exception :CantShiftToMultiIrbMode, "Can't shift to multi irb mode."
+ def_exception :CantChangeBinding, "Can't change binding to (%s)."
+ def_exception :UndefinedPromptMode, "Undefined prompt mode(%s)."
+
+end
+
diff --git a/ruby/lib/irb/lc/help-message b/ruby/lib/irb/lc/help-message
new file mode 100644
index 0000000..312833c
--- /dev/null
+++ b/ruby/lib/irb/lc/help-message
@@ -0,0 +1,38 @@
+# -*- coding: US-ASCII -*-
+#
+# irb/lc/help-message.rb -
+# $Release Version: 0.9.5$
+# $Revision: 20882 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+Usage: irb.rb [options] [programfile] [arguments]
+ -f Suppress read of ~/.irbrc
+ -m Bc mode (load mathn, fraction or matrix are available)
+ -d Set $DEBUG to true (same as `ruby -d')
+ -r load-module Same as `ruby -r'
+ -I path Specify $LOAD_PATH directory
+ -U Same as `ruby -U`
+ -E enc Same as `ruby -E`
+ --inspect Use `inspect' for output (default except for bc mode)
+ --noinspect Don't use inspect for output
+ --readline Use Readline extension module
+ --noreadline Don't use Readline extension module
+ --prompt prompt-mode
+ --prompt-mode prompt-mode
+ Switch prompt mode. Pre-defined prompt modes are
+ `default', `simple', `xmp' and `inf-ruby'
+ --inf-ruby-mode Use prompt appropriate for inf-ruby-mode on emacs.
+ Suppresses --readline.
+ --simple-prompt Simple prompt mode
+ --noprompt No prompt mode
+ --tracer Display trace for each execution of commands.
+ --back-trace-limit n
+ Display backtrace top n and tail n. The default
+ value is 16.
+ --irb_debug n Set internal debug level to n (not for popular use)
+ -v, --version Print the version of irb
+# vim:fileencoding=us-ascii
diff --git a/ruby/lib/irb/lc/ja/encoding_aliases.rb b/ruby/lib/irb/lc/ja/encoding_aliases.rb
new file mode 100644
index 0000000..a713dff
--- /dev/null
+++ b/ruby/lib/irb/lc/ja/encoding_aliases.rb
@@ -0,0 +1,8 @@
+module IRB
+ class Locale
+ @@legacy_encoding_alias_map = {
+ 'ujis' => Encoding::EUC_JP,
+ 'euc' => Encoding::EUC_JP
+ }.freeze
+ end
+end
diff --git a/ruby/lib/irb/lc/ja/error.rb b/ruby/lib/irb/lc/ja/error.rb
new file mode 100644
index 0000000..d1d7d54
--- /dev/null
+++ b/ruby/lib/irb/lc/ja/error.rb
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+# irb/lc/ja/error.rb -
+# $Release Version: 0.9.5$
+# $Revision: 20882 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+require "e2mmap"
+
+module IRB
+ # exceptions
+ extend Exception2MessageMapper
+ def_exception :UnrecognizedSwitch, 'スイッãƒ(%s)ãŒåˆ†ã‚Šã¾ã›ã‚“'
+ def_exception :NotImplementedError, '`%s\'ã®å®šç¾©ãŒå¿…è¦ã§ã™'
+ def_exception :CantReturnToNormalMode, 'Normalモードã«æˆ»ã‚Œã¾ã›ã‚“.'
+ def_exception :IllegalParameter, 'パラメータ(%s)ãŒé–“é•ã£ã¦ã„ã¾ã™.'
+ def_exception :IrbAlreadyDead, 'Irbã¯æ—¢ã«æ­»ã‚“ã§ã„ã¾ã™.'
+ def_exception :IrbSwitchedToCurrentThread, 'カレントスレッドã«åˆ‡ã‚Šæ›¿ã‚ã‚Šã¾ã—ãŸ.'
+ def_exception :NoSuchJob, 'ãã®ã‚ˆã†ãªã‚¸ãƒ§ãƒ–(%s)ã¯ã‚ã‚Šã¾ã›ã‚“.'
+ def_exception :CantShiftToMultiIrbMode, 'multi-irb modeã«ç§»ã‚Œã¾ã›ã‚“.'
+ def_exception :CantChangeBinding, 'ãƒã‚¤ãƒ³ãƒ‡ã‚£ãƒ³ã‚°(%s)ã«å¤‰æ›´ã§ãã¾ã›ã‚“.'
+ def_exception :UndefinedPromptMode, 'プロンプトモード(%s)ã¯å®šç¾©ã•ã‚Œã¦ã„ã¾ã›ã‚“.'
+end
+# vim:fileencoding=utf-8
diff --git a/ruby/lib/irb/lc/ja/help-message b/ruby/lib/irb/lc/ja/help-message
new file mode 100644
index 0000000..832bc5f
--- /dev/null
+++ b/ruby/lib/irb/lc/ja/help-message
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+# irb/lc/ja/help-message.rb -
+# $Release Version: 0.9.5$
+# $Revision: 20882 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+Usage: irb.rb [options] [programfile] [arguments]
+ -f ~/.irbrc を読ã¿è¾¼ã¾ãªã„.
+ -m bcモード(分数, 行列ã®è¨ˆç®—ãŒã§ãã‚‹)
+ -d $DEBUG ã‚’trueã«ã™ã‚‹(ruby -d ã¨åŒã˜)
+ -r load-module ruby -r ã¨åŒã˜.
+ -I path $LOAD_PATH ã« path を追加ã™ã‚‹.
+ -U ruby -U ã¨åŒã˜.
+ -E enc ruby -E ã¨åŒã˜.
+ --inspect çµæžœå‡ºåŠ›ã«inspectを用ã„ã‚‹(bcモード以外ã¯ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆ).
+ --noinspect çµæžœå‡ºåŠ›ã«inspectを用ã„ãªã„.
+ --readline readlineライブラリを利用ã™ã‚‹.
+ --noreadline readlineライブラリを利用ã—ãªã„.
+ --prompt prompt-mode/--prompt-mode prompt-mode
+ プロンプトモードを切替ãˆã¾ã™. ç¾åœ¨å®šç¾©ã•ã‚Œã¦ã„るプ
+ ロンプトモードã¯, default, simple, xmp, inf-rubyãŒ
+ 用æ„ã•ã‚Œã¦ã„ã¾ã™.
+ --inf-ruby-mode emacsã®inf-ruby-mode用ã®ãƒ—ロンプト表示を行ãªã†. 特
+ ã«æŒ‡å®šãŒãªã„é™ã‚Š, readlineライブラリã¯ä½¿ã‚ãªããªã‚‹.
+ --simple-prompt éžå¸¸ã«ã‚·ãƒ³ãƒ—ルãªãƒ—ロンプトを用ã„るモードã§ã™.
+ --noprompt プロンプト表示を行ãªã‚ãªã„.
+ --tracer コマンド実行時ã«ãƒˆãƒ¬ãƒ¼ã‚¹ã‚’è¡Œãªã†.
+ --back-trace-limit n
+ ãƒãƒƒã‚¯ãƒˆãƒ¬ãƒ¼ã‚¹è¡¨ç¤ºã‚’ãƒãƒƒã‚¯ãƒˆãƒ¬ãƒ¼ã‚¹ã®é ­ã‹ã‚‰ n, 後ã‚
+ ã‹ã‚‰nã ã‘è¡Œãªã†. デフォルトã¯16
+ --irb_debug n irbã®ãƒ‡ãƒãƒƒã‚°ãƒ‡ãƒãƒƒã‚°ãƒ¬ãƒ™ãƒ«ã‚’nã«è¨­å®šã™ã‚‹(利用ã—ãª
+ ã„æ–¹ãŒç„¡é›£ã§ã—ょã†).
+ -v, --version irbã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³ã‚’表示ã™ã‚‹
+
+# vim:fileencoding=utf-8
diff --git a/ruby/lib/irb/locale.rb b/ruby/lib/irb/locale.rb
new file mode 100644
index 0000000..c8e58b2
--- /dev/null
+++ b/ruby/lib/irb/locale.rb
@@ -0,0 +1,195 @@
+#
+# irb/locale.rb - internationalization module
+# $Release Version: 0.9.5$
+# $Revision: 20889 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+module IRB
+ class Locale
+ @RCS_ID='-$Id: locale.rb 20889 2008-12-20 02:02:48Z yugui $-'
+
+ LOCALE_NAME_RE = %r[
+ (?<language>[[:alpha:]]{2})
+ (?:_
+ (?<territory>[[:alpha:]]{2,3})
+ (?:\.
+ (?<codeset>[^@]+)
+ )?
+ )?
+ (?:@
+ (?<modifier>.*)
+ )?
+ ]x
+ LOCALE_DIR = "/lc/"
+
+ @@legacy_encoding_alias_map = {}.freeze
+
+ def initialize(locale = nil)
+ @lang = @territory = @encoding_name = @modifier = nil
+ @locale = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C"
+ if m = LOCALE_NAME_RE.match(@locale)
+ @lang, @territory, @encoding_name, @modifier = m[:language], m[:territory], m[:codeset], m[:modifier]
+
+ if @encoding_name
+ begin load 'irb/encoding_aliases.rb'; rescue LoadError; end
+ if @encoding = @@legacy_encoding_alias_map[@encoding_name]
+ warn "%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"]
+ end
+ @encoding = Encoding.find(@encoding_name) rescue nil
+ end
+ end
+ @encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT)
+ end
+
+ attr_reader :lang, :territory, :encoding, :modifieer
+
+ def String(mes)
+ mes = super(mes)
+ if @encoding
+ mes.encode(@encoding)
+ else
+ mes
+ end
+ end
+
+ def format(*opts)
+ String(super(*opts))
+ end
+
+ def gets(*rs)
+ String(super(*rs))
+ end
+
+ def readline(*rs)
+ String(super(*rs))
+ end
+
+ def print(*opts)
+ ary = opts.collect{|opt| String(opt)}
+ super(*ary)
+ end
+
+ def printf(*opts)
+ s = format(*opts)
+ print s
+ end
+
+ def puts(*opts)
+ ary = opts.collect{|opt| String(opt)}
+ super(*ary)
+ end
+
+ def require(file, priv = nil)
+ rex = Regexp.new("lc/#{Regexp.quote(file)}\.(so|o|sl|rb)?")
+ return false if $".find{|f| f =~ rex}
+
+ case file
+ when /\.rb$/
+ begin
+ load(file, priv)
+ $".push file
+ return true
+ rescue LoadError
+ end
+ when /\.(so|o|sl)$/
+ return super
+ end
+
+ begin
+ load(f = file + ".rb")
+ $".push f #"
+ return true
+ rescue LoadError
+ return ruby_require(file)
+ end
+ end
+
+ alias toplevel_load load
+
+ def load(file, priv=nil)
+ dir = File.dirname(file)
+ dir = "" if dir == "."
+ base = File.basename(file)
+
+ if dir[0] == ?/ #/
+ lc_path = search_file(dir, base)
+ return real_load(lc_path, priv) if lc_path
+ end
+
+ for path in $:
+ lc_path = search_file(path + "/" + dir, base)
+ return real_load(lc_path, priv) if lc_path
+ end
+ raise LoadError, "No such file to load -- #{file}"
+ end
+
+ def real_load(path, priv)
+ src = MagicFile.open(path){|f| f.read}
+ if priv
+ eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
+ else
+ eval(src, TOPLEVEL_BINDING, path)
+ end
+ end
+ private :real_load
+
+ def find(file , paths = $:)
+ dir = File.dirname(file)
+ dir = "" if dir == "."
+ base = File.basename(file)
+ if dir[0] == ?/ #/
+ return lc_path = search_file(dir, base)
+ else
+ for path in $:
+ if lc_path = search_file(path + "/" + dir, base)
+ return lc_path
+ end
+ end
+ end
+ nil
+ end
+
+ def search_file(path, file)
+ each_sublocale do |lc|
+ full_path = path + lc_path(file, lc)
+ return full_path if File.exist?(full_path)
+ end
+ nil
+ end
+ private :search_file
+
+ def lc_path(file = "", lc = @locale)
+ if lc.nil?
+ LOCALE_DIR + file
+ else
+ LOCALE_DIR + @lang + "/" + file
+ end
+ end
+ private :lc_path
+
+ def each_sublocale
+ if @lang
+ if @territory
+ if @encoding_name
+ yield "#{@lang}_#{@territory}.#{@encoding_name}@#{@modifier}" if @modifier
+ yield "#{@lang}_#{@territory}.#{@encoding_name}"
+ end
+ yield "#{@lang}_#{@territory}@#{@modifier}" if @modifier
+ yield "#{@lang}_#{@territory}"
+ end
+ yield "#{@lang}@#{@modifier}" if @modifier
+ yield "#{@lang}"
+ end
+ yield nil
+ end
+ private :each_sublocale
+ end
+end
+
+
+
+
diff --git a/ruby/lib/irb/magic-file.rb b/ruby/lib/irb/magic-file.rb
new file mode 100644
index 0000000..8612620
--- /dev/null
+++ b/ruby/lib/irb/magic-file.rb
@@ -0,0 +1,36 @@
+module IRB
+ class << (MagicFile = Object.new)
+ # see parser_magic_comment in parse.y
+ ENCODING_SPEC_RE = %r"coding\s*[=:]\s*([[:alnum:]\-_]+)"
+
+ def open(path)
+ io = File.open(path, 'rb')
+ line = io.gets
+ line = io.gets if line[0,2] == "#!"
+ encoding = detect_encoding(line)
+ encoding ||= default_src_encoding
+ io.rewind
+ io.set_encoding(encoding, nil)
+
+ if block_given?
+ begin
+ return (yield io)
+ ensure
+ io.close
+ end
+ else
+ return io
+ end
+ end
+
+ private
+ def detect_encoding(line)
+ return unless line[0] == ?#
+ line = line[1..-1]
+ line = $1 if line[/-\*-\s*(.*?)\s*-*-$/]
+ return nil unless ENCODING_SPEC_RE =~ line
+ encoding = $1
+ return encoding.sub(/-(?:mac|dos|unix)/i, '')
+ end
+ end
+end
diff --git a/ruby/lib/irb/notifier.rb b/ruby/lib/irb/notifier.rb
new file mode 100644
index 0000000..694c1be
--- /dev/null
+++ b/ruby/lib/irb/notifier.rb
@@ -0,0 +1,144 @@
+#
+# notifier.rb - output methods used by irb
+# $Release Version: 0.9.5$
+# $Revision: 16810 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+require "irb/output-method"
+
+module IRB
+ module Notifier
+ extend Exception2MessageMapper
+ def_exception :ErrUndefinedNotifier,
+ "undefined notifier level: %d is specified"
+ def_exception :ErrUnrecognizedLevel,
+ "unrecognized notifier level: %s is specified"
+
+ def def_notifier(prefix = "", output_method = StdioOutputMethod.new)
+ CompositeNotifier.new(prefix, output_method)
+ end
+ module_function :def_notifier
+
+ class AbstructNotifier
+ def initialize(prefix, base_notifier)
+ @prefix = prefix
+ @base_notifier = base_notifier
+ end
+
+ attr_reader :prefix
+
+ def notify?
+ true
+ end
+
+ def print(*opts)
+ @base_notifier.print prefix, *opts if notify?
+ end
+
+ def printn(*opts)
+ @base_notifier.printn prefix, *opts if notify?
+ end
+
+ def printf(format, *opts)
+ @base_notifier.printf(prefix + format, *opts) if notify?
+ end
+
+ def puts(*objs)
+ if notify?
+ @base_notifier.puts(*objs.collect{|obj| prefix + obj.to_s})
+ end
+ end
+
+ def pp(*objs)
+ if notify?
+ @base_notifier.ppx @prefix, *objs
+ end
+ end
+
+ def ppx(prefix, *objs)
+ if notify?
+ @base_notifier.ppx @prefix+prefix, *objs
+ end
+ end
+
+ def exec_if
+ yield(@base_notifier) if notify?
+ end
+ end
+
+ class CompositeNotifier<AbstructNotifier
+ def initialize(prefix, base_notifier)
+ super
+
+ @notifiers = [D_NOMSG]
+ @level_notifier = D_NOMSG
+ end
+
+ attr_reader :notifiers
+
+ def def_notifier(level, prefix = "")
+ notifier = LeveledNotifier.new(self, level, prefix)
+ @notifiers[level] = notifier
+ notifier
+ end
+
+ attr_reader :level_notifier
+ alias level level_notifier
+
+ def level_notifier=(value)
+ case value
+ when AbstructNotifier
+ @level_notifier = value
+ when Integer
+ l = @notifiers[value]
+ Notifier.Raise ErrUndefinedNotifer, value unless l
+ @level_notifier = l
+ else
+ Notifier.Raise ErrUnrecognizedLevel, value unless l
+ end
+ end
+
+ alias level= level_notifier=
+ end
+
+ class LeveledNotifier<AbstructNotifier
+ include Comparable
+
+ def initialize(base, level, prefix)
+ super(prefix, base)
+
+ @level = level
+ end
+
+ attr_reader :level
+
+ def <=>(other)
+ @level <=> other.level
+ end
+
+ def notify?
+ @base_notifier.level >= self
+ end
+ end
+
+ class NoMsgNotifier<LeveledNotifier
+ def initialize
+ @base_notifier = nil
+ @level = 0
+ @prefix = ""
+ end
+
+ def notify?
+ false
+ end
+ end
+
+ D_NOMSG = NoMsgNotifier.new
+ end
+end
diff --git a/ruby/lib/irb/output-method.rb b/ruby/lib/irb/output-method.rb
new file mode 100644
index 0000000..6e94b6a
--- /dev/null
+++ b/ruby/lib/irb/output-method.rb
@@ -0,0 +1,69 @@
+#
+# output-method.rb - optput methods used by irb
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+
+module IRB
+ # OutputMethod
+ # StdioOutputMethod
+
+ class OutputMethod
+ @RCS_ID='-$Id: output-method.rb 14912 2008-01-06 15:49:38Z akr $-'
+
+ def print(*opts)
+ IRB.fail NotImplementError, "print"
+ end
+
+ def printn(*opts)
+ print opts.join(" "), "\n"
+ end
+
+ # extend printf
+ def printf(format, *opts)
+ if /(%*)%I/ =~ format
+ format, opts = parse_printf_format(format, opts)
+ end
+ print sprintf(format, *opts)
+ end
+
+ # %
+ # <flag> [#0- +]
+ # <minimum field width> (\*|\*[1-9][0-9]*\$|[1-9][0-9]*)
+ # <precision>.(\*|\*[1-9][0-9]*\$|[1-9][0-9]*|)?
+ # #<length modifier>(hh|h|l|ll|L|q|j|z|t)
+ # <conversion specifier>[diouxXeEfgGcsb%]
+ def parse_printf_format(format, opts)
+ return format, opts if $1.size % 2 == 1
+ end
+
+ def puts(*objs)
+ for obj in objs
+ print(*obj)
+ print "\n"
+ end
+ end
+
+ def pp(*objs)
+ puts(*objs.collect{|obj| obj.inspect})
+ end
+
+ def ppx(prefix, *objs)
+ puts(*objs.collect{|obj| prefix+obj.inspect})
+ end
+
+ end
+
+ class StdioOutputMethod<OutputMethod
+ def print(*opts)
+ STDOUT.print(*opts)
+ end
+ end
+end
diff --git a/ruby/lib/irb/ruby-lex.rb b/ruby/lib/irb/ruby-lex.rb
new file mode 100644
index 0000000..bee9a8b
--- /dev/null
+++ b/ruby/lib/irb/ruby-lex.rb
@@ -0,0 +1,1188 @@
+#
+# irb/ruby-lex.rb - ruby lexcal analyzer
+# $Release Version: 0.9.5$
+# $Revision: 24448 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+require "irb/slex"
+require "irb/ruby-token"
+
+class RubyLex
+ @RCS_ID='-$Id: ruby-lex.rb 24448 2009-08-08 10:55:48Z yugui $-'
+
+ extend Exception2MessageMapper
+ def_exception(:AlreadyDefinedToken, "Already defined token(%s)")
+ def_exception(:TkReading2TokenNoKey, "key nothing(key='%s')")
+ def_exception(:TkSymbol2TokenNoKey, "key nothing(key='%s')")
+ def_exception(:TkReading2TokenDuplicateError,
+ "key duplicate(token_n='%s', key='%s')")
+ def_exception(:SyntaxError, "%s")
+
+ def_exception(:TerminateLineInput, "Terminate Line Input")
+
+ include RubyToken
+
+ class << self
+ attr_accessor :debug_level
+ def debug?
+ @debug_level > 0
+ end
+ end
+ @debug_level = 0
+
+ def initialize
+ lex_init
+ set_input(STDIN)
+
+ @seek = 0
+ @exp_line_no = @line_no = 1
+ @base_char_no = 0
+ @char_no = 0
+ @rests = []
+ @readed = []
+ @here_readed = []
+
+ @indent = 0
+ @indent_stack = []
+ @lex_state = EXPR_BEG
+ @space_seen = false
+ @here_header = false
+
+ @continue = false
+ @line = ""
+
+ @skip_space = false
+ @readed_auto_clean_up = false
+ @exception_on_syntax_error = true
+
+ @prompt = nil
+ end
+
+ attr_accessor :skip_space
+ attr_accessor :readed_auto_clean_up
+ attr_accessor :exception_on_syntax_error
+
+ attr_reader :seek
+ attr_reader :char_no
+ attr_reader :line_no
+ attr_reader :indent
+
+ # io functions
+ def set_input(io, p = nil, &block)
+ @io = io
+ if p.respond_to?(:call)
+ @input = p
+ elsif block_given?
+ @input = block
+ else
+ @input = Proc.new{@io.gets}
+ end
+ end
+
+ def get_readed
+ if idx = @readed.reverse.index("\n")
+ @base_char_no = idx
+ else
+ @base_char_no += @readed.size
+ end
+
+ readed = @readed.join("")
+ @readed = []
+ readed
+ end
+
+ def getc
+ while @rests.empty?
+# return nil unless buf_input
+ @rests.push nil unless buf_input
+ end
+ c = @rests.shift
+ return if c == nil
+ if @here_header
+ @here_readed.push c
+ else
+ @readed.push c
+ end
+ @seek += 1
+ if c == "\n"
+ @line_no += 1
+ @char_no = 0
+ else
+ @char_no += 1
+ end
+ c
+ end
+
+ def gets
+ l = ""
+ while c = getc
+ l.concat(c)
+ break if c == "\n"
+ end
+ return nil if l == "" and c.nil?
+ l
+ end
+
+ def eof?
+ @io.eof?
+ end
+
+ def getc_of_rests
+ if @rests.empty?
+ nil
+ else
+ getc
+ end
+ end
+
+ def ungetc(c = nil)
+ if @here_readed.empty?
+ c2 = @readed.pop
+ else
+ c2 = @here_readed.pop
+ end
+ c = c2 unless c
+ @rests.unshift c #c =
+ @seek -= 1
+ if c == "\n"
+ @line_no -= 1
+ if idx = @readed.reverse.index("\n")
+ @char_no = @readed.size - idx
+ else
+ @char_no = @base_char_no + @readed.size
+ end
+ else
+ @char_no -= 1
+ end
+ end
+
+ def peek_equal?(str)
+ chrs = str.split(//)
+ until @rests.size >= chrs.size
+ return false unless buf_input
+ end
+ @rests[0, chrs.size] == chrs
+ end
+
+ def peek_match?(regexp)
+ while @rests.empty?
+ return false unless buf_input
+ end
+ regexp =~ @rests.join("")
+ end
+
+ def peek(i = 0)
+ while @rests.size <= i
+ return nil unless buf_input
+ end
+ @rests[i]
+ end
+
+ def buf_input
+ prompt
+ line = @input.call
+ return nil unless line
+ @rests.concat line.chars.to_a
+ true
+ end
+ private :buf_input
+
+ def set_prompt(p = nil, &block)
+ p = block if block_given?
+ if p.respond_to?(:call)
+ @prompt = p
+ else
+ @prompt = Proc.new{print p}
+ end
+ end
+
+ def prompt
+ if @prompt
+ @prompt.call(@ltype, @indent, @continue, @line_no)
+ end
+ end
+
+ def initialize_input
+ @ltype = nil
+ @quoted = nil
+ @indent = 0
+ @indent_stack = []
+ @lex_state = EXPR_BEG
+ @space_seen = false
+ @here_header = false
+
+ @continue = false
+ prompt
+
+ @line = ""
+ @exp_line_no = @line_no
+ end
+
+ def each_top_level_statement
+ initialize_input
+ catch(:TERM_INPUT) do
+ loop do
+ begin
+ @continue = false
+ prompt
+ unless l = lex
+ throw :TERM_INPUT if @line == ''
+ else
+ @line.concat l
+ if @ltype or @continue or @indent > 0
+ next
+ end
+ end
+ if @line != "\n"
+ @line.force_encoding(@io.encoding)
+ yield @line, @exp_line_no
+ end
+ break unless l
+ @line = ''
+ @exp_line_no = @line_no
+
+ @indent = 0
+ @indent_stack = []
+ prompt
+ rescue TerminateLineInput
+ initialize_input
+ prompt
+ get_readed
+ end
+ end
+ end
+ end
+
+ def lex
+ until (((tk = token).kind_of?(TkNL) || tk.kind_of?(TkEND_OF_SCRIPT)) &&
+ !@continue or
+ tk.nil?)
+ #p tk
+ #p @lex_state
+ #p self
+ end
+ line = get_readed
+ # print self.inspect
+ if line == "" and tk.kind_of?(TkEND_OF_SCRIPT) || tk.nil?
+ nil
+ else
+ line
+ end
+ end
+
+ def token
+ # require "tracer"
+ # Tracer.on
+ @prev_seek = @seek
+ @prev_line_no = @line_no
+ @prev_char_no = @char_no
+ begin
+ begin
+ tk = @OP.match(self)
+ @space_seen = tk.kind_of?(TkSPACE)
+ rescue SyntaxError
+ raise if @exception_on_syntax_error
+ tk = TkError.new(@seek, @line_no, @char_no)
+ end
+ end while @skip_space and tk.kind_of?(TkSPACE)
+ if @readed_auto_clean_up
+ get_readed
+ end
+ # Tracer.off
+ tk
+ end
+
+ ENINDENT_CLAUSE = [
+ "case", "class", "def", "do", "for", "if",
+ "module", "unless", "until", "while", "begin" #, "when"
+ ]
+ DEINDENT_CLAUSE = ["end" #, "when"
+ ]
+
+ PERCENT_LTYPE = {
+ "q" => "\'",
+ "Q" => "\"",
+ "x" => "\`",
+ "r" => "/",
+ "w" => "]",
+ "W" => "]",
+ "s" => ":"
+ }
+
+ PERCENT_PAREN = {
+ "{" => "}",
+ "[" => "]",
+ "<" => ">",
+ "(" => ")"
+ }
+
+ Ltype2Token = {
+ "\'" => TkSTRING,
+ "\"" => TkSTRING,
+ "\`" => TkXSTRING,
+ "/" => TkREGEXP,
+ "]" => TkDSTRING,
+ ":" => TkSYMBOL
+ }
+ DLtype2Token = {
+ "\"" => TkDSTRING,
+ "\`" => TkDXSTRING,
+ "/" => TkDREGEXP,
+ }
+
+ def lex_init()
+ @OP = IRB::SLex.new
+ @OP.def_rules("\0", "\004", "\032") do |op, io|
+ Token(TkEND_OF_SCRIPT)
+ end
+
+ @OP.def_rules(" ", "\t", "\f", "\r", "\13") do |op, io|
+ @space_seen = true
+ while getc =~ /[ \t\f\r\13]/; end
+ ungetc
+ Token(TkSPACE)
+ end
+
+ @OP.def_rule("#") do |op, io|
+ identify_comment
+ end
+
+ @OP.def_rule("=begin",
+ proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
+ |op, io|
+ @ltype = "="
+ until getc == "\n"; end
+ until peek_equal?("=end") && peek(4) =~ /\s/
+ until getc == "\n"; end
+ end
+ gets
+ @ltype = nil
+ Token(TkRD_COMMENT)
+ end
+
+ @OP.def_rule("\n") do |op, io|
+ print "\\n\n" if RubyLex.debug?
+ case @lex_state
+ when EXPR_BEG, EXPR_FNAME, EXPR_DOT
+ @continue = true
+ else
+ @continue = false
+ @lex_state = EXPR_BEG
+ until (@indent_stack.empty? ||
+ [TkLPAREN, TkLBRACK, TkLBRACE,
+ TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
+ @indent_stack.pop
+ end
+ end
+ @here_header = false
+ @here_readed = []
+ Token(TkNL)
+ end
+
+ @OP.def_rules("*", "**",
+ "=", "==", "===",
+ "=~", "<=>",
+ "<", "<=",
+ ">", ">=", ">>",
+ "!", "!=", "!~") do
+ |op, io|
+ case @lex_state
+ when EXPR_FNAME, EXPR_DOT
+ @lex_state = EXPR_ARG
+ else
+ @lex_state = EXPR_BEG
+ end
+ Token(op)
+ end
+
+ @OP.def_rules("<<") do
+ |op, io|
+ tk = nil
+ if @lex_state != EXPR_END && @lex_state != EXPR_CLASS &&
+ (@lex_state != EXPR_ARG || @space_seen)
+ c = peek(0)
+ if /\S/ =~ c && (/["'`]/ =~ c || /[\w_]/ =~ c || c == "-")
+ tk = identify_here_document
+ end
+ end
+ unless tk
+ tk = Token(op)
+ case @lex_state
+ when EXPR_FNAME, EXPR_DOT
+ @lex_state = EXPR_ARG
+ else
+ @lex_state = EXPR_BEG
+ end
+ end
+ tk
+ end
+
+ @OP.def_rules("'", '"') do
+ |op, io|
+ identify_string(op)
+ end
+
+ @OP.def_rules("`") do
+ |op, io|
+ if @lex_state == EXPR_FNAME
+ @lex_state = EXPR_END
+ Token(op)
+ else
+ identify_string(op)
+ end
+ end
+
+ @OP.def_rules('?') do
+ |op, io|
+ if @lex_state == EXPR_END
+ @lex_state = EXPR_BEG
+ Token(TkQUESTION)
+ else
+ ch = getc
+ if @lex_state == EXPR_ARG && ch =~ /\s/
+ ungetc
+ @lex_state = EXPR_BEG;
+ Token(TkQUESTION)
+ else
+ if (ch == '\\')
+ read_escape
+ end
+ @lex_state = EXPR_END
+ Token(TkINTEGER)
+ end
+ end
+ end
+
+ @OP.def_rules("&", "&&", "|", "||") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token(op)
+ end
+
+ @OP.def_rules("+=", "-=", "*=", "**=",
+ "&=", "|=", "^=", "<<=", ">>=", "||=", "&&=") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ op =~ /^(.*)=$/
+ Token(TkOPASGN, $1)
+ end
+
+ @OP.def_rule("+@", proc{|op, io| @lex_state == EXPR_FNAME}) do
+ |op, io|
+ @lex_state = EXPR_ARG
+ Token(op)
+ end
+
+ @OP.def_rule("-@", proc{|op, io| @lex_state == EXPR_FNAME}) do
+ |op, io|
+ @lex_state = EXPR_ARG
+ Token(op)
+ end
+
+ @OP.def_rules("+", "-") do
+ |op, io|
+ catch(:RET) do
+ if @lex_state == EXPR_ARG
+ if @space_seen and peek(0) =~ /[0-9]/
+ throw :RET, identify_number
+ else
+ @lex_state = EXPR_BEG
+ end
+ elsif @lex_state != EXPR_END and peek(0) =~ /[0-9]/
+ throw :RET, identify_number
+ else
+ @lex_state = EXPR_BEG
+ end
+ Token(op)
+ end
+ end
+
+ @OP.def_rule(".") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ if peek(0) =~ /[0-9]/
+ ungetc
+ identify_number
+ else
+ # for "obj.if" etc.
+ @lex_state = EXPR_DOT
+ Token(TkDOT)
+ end
+ end
+
+ @OP.def_rules("..", "...") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token(op)
+ end
+
+ lex_int2
+ end
+
+ def lex_int2
+ @OP.def_rules("]", "}", ")") do
+ |op, io|
+ @lex_state = EXPR_END
+ @indent -= 1
+ @indent_stack.pop
+ Token(op)
+ end
+
+ @OP.def_rule(":") do
+ |op, io|
+ if @lex_state == EXPR_END || peek(0) =~ /\s/
+ @lex_state = EXPR_BEG
+ Token(TkCOLON)
+ else
+ @lex_state = EXPR_FNAME;
+ Token(TkSYMBEG)
+ end
+ end
+
+ @OP.def_rule("::") do
+ |op, io|
+# p @lex_state.id2name, @space_seen
+ if @lex_state == EXPR_BEG or @lex_state == EXPR_ARG && @space_seen
+ @lex_state = EXPR_BEG
+ Token(TkCOLON3)
+ else
+ @lex_state = EXPR_DOT
+ Token(TkCOLON2)
+ end
+ end
+
+ @OP.def_rule("/") do
+ |op, io|
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ identify_string(op)
+ elsif peek(0) == '='
+ getc
+ @lex_state = EXPR_BEG
+ Token(TkOPASGN, "/") #/)
+ elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
+ identify_string(op)
+ else
+ @lex_state = EXPR_BEG
+ Token("/") #/)
+ end
+ end
+
+ @OP.def_rules("^") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token("^")
+ end
+
+ # @OP.def_rules("^=") do
+ # @lex_state = EXPR_BEG
+ # Token(OP_ASGN, :^)
+ # end
+
+ @OP.def_rules(",") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token(op)
+ end
+
+ @OP.def_rules(";") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ until (@indent_stack.empty? ||
+ [TkLPAREN, TkLBRACK, TkLBRACE,
+ TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
+ @indent_stack.pop
+ end
+ Token(op)
+ end
+
+ @OP.def_rule("~") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token("~")
+ end
+
+ @OP.def_rule("~@", proc{|op, io| @lex_state == EXPR_FNAME}) do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token("~")
+ end
+
+ @OP.def_rule("(") do
+ |op, io|
+ @indent += 1
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ @lex_state = EXPR_BEG
+ tk_c = TkfLPAREN
+ else
+ @lex_state = EXPR_BEG
+ tk_c = TkLPAREN
+ end
+ @indent_stack.push tk_c
+ tk = Token(tk_c)
+ end
+
+ @OP.def_rule("[]", proc{|op, io| @lex_state == EXPR_FNAME}) do
+ |op, io|
+ @lex_state = EXPR_ARG
+ Token("[]")
+ end
+
+ @OP.def_rule("[]=", proc{|op, io| @lex_state == EXPR_FNAME}) do
+ |op, io|
+ @lex_state = EXPR_ARG
+ Token("[]=")
+ end
+
+ @OP.def_rule("[") do
+ |op, io|
+ @indent += 1
+ if @lex_state == EXPR_FNAME
+ tk_c = TkfLBRACK
+ else
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ tk_c = TkLBRACK
+ elsif @lex_state == EXPR_ARG && @space_seen
+ tk_c = TkLBRACK
+ else
+ tk_c = TkfLBRACK
+ end
+ @lex_state = EXPR_BEG
+ end
+ @indent_stack.push tk_c
+ Token(tk_c)
+ end
+
+ @OP.def_rule("{") do
+ |op, io|
+ @indent += 1
+ if @lex_state != EXPR_END && @lex_state != EXPR_ARG
+ tk_c = TkLBRACE
+ else
+ tk_c = TkfLBRACE
+ end
+ @lex_state = EXPR_BEG
+ @indent_stack.push tk_c
+ Token(tk_c)
+ end
+
+ @OP.def_rule('\\') do
+ |op, io|
+ if getc == "\n"
+ @space_seen = true
+ @continue = true
+ Token(TkSPACE)
+ else
+ ungetc
+ Token("\\")
+ end
+ end
+
+ @OP.def_rule('%') do
+ |op, io|
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ identify_quotation
+ elsif peek(0) == '='
+ getc
+ Token(TkOPASGN, :%)
+ elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
+ identify_quotation
+ else
+ @lex_state = EXPR_BEG
+ Token("%") #))
+ end
+ end
+
+ @OP.def_rule('$') do
+ |op, io|
+ identify_gvar
+ end
+
+ @OP.def_rule('@') do
+ |op, io|
+ if peek(0) =~ /[\w_@]/
+ ungetc
+ identify_identifier
+ else
+ Token("@")
+ end
+ end
+
+ # @OP.def_rule("def", proc{|op, io| /\s/ =~ io.peek(0)}) do
+ # |op, io|
+ # @indent += 1
+ # @lex_state = EXPR_FNAME
+ # # @lex_state = EXPR_END
+ # # until @rests[0] == "\n" or @rests[0] == ";"
+ # # rests.shift
+ # # end
+ # end
+
+ @OP.def_rule("") do
+ |op, io|
+ printf "MATCH: start %s: %s\n", op, io.inspect if RubyLex.debug?
+ if peek(0) =~ /[0-9]/
+ t = identify_number
+ elsif peek(0) =~ /[\w_]/
+ t = identify_identifier
+ end
+ printf "MATCH: end %s: %s\n", op, io.inspect if RubyLex.debug?
+ t
+ end
+
+ p @OP if RubyLex.debug?
+ end
+
+ def identify_gvar
+ @lex_state = EXPR_END
+
+ case ch = getc
+ when /[~_*$?!@\/\\;,=:<>".]/ #"
+ Token(TkGVAR, "$" + ch)
+ when "-"
+ Token(TkGVAR, "$-" + getc)
+ when "&", "`", "'", "+"
+ Token(TkBACK_REF, "$"+ch)
+ when /[1-9]/
+ while getc =~ /[0-9]/; end
+ ungetc
+ Token(TkNTH_REF)
+ when /\w/
+ ungetc
+ ungetc
+ identify_identifier
+ else
+ ungetc
+ Token("$")
+ end
+ end
+
+ def identify_identifier
+ token = ""
+ if peek(0) =~ /[$@]/
+ token.concat(c = getc)
+ if c == "@" and peek(0) == "@"
+ token.concat getc
+ end
+ end
+
+ while (ch = getc) =~ /\w|_/
+ print ":", ch, ":" if RubyLex.debug?
+ token.concat ch
+ end
+ ungetc
+
+ if (ch == "!" || ch == "?") && token[0,1] =~ /\w/ && peek(0) != "="
+ token.concat getc
+ end
+
+ # almost fix token
+
+ case token
+ when /^\$/
+ return Token(TkGVAR, token)
+ when /^\@\@/
+ @lex_state = EXPR_END
+ # p Token(TkCVAR, token)
+ return Token(TkCVAR, token)
+ when /^\@/
+ @lex_state = EXPR_END
+ return Token(TkIVAR, token)
+ end
+
+ if @lex_state != EXPR_DOT
+ print token, "\n" if RubyLex.debug?
+
+ token_c, *trans = TkReading2Token[token]
+ if token_c
+ # reserved word?
+
+ if (@lex_state != EXPR_BEG &&
+ @lex_state != EXPR_FNAME &&
+ trans[1])
+ # modifiers
+ token_c = TkSymbol2Token[trans[1]]
+ @lex_state = trans[0]
+ else
+ if @lex_state != EXPR_FNAME
+ if ENINDENT_CLAUSE.include?(token)
+ # check for ``class = val'' etc.
+ valid = true
+ case token
+ when "class"
+ valid = false unless peek_match?(/^\s*(<<|\w|::)/)
+ when "def"
+ valid = false if peek_match?(/^\s*(([+\-\/*&\|^]|<<|>>|\|\||\&\&)=|\&\&|\|\|)/)
+ when "do"
+ valid = false if peek_match?(/^\s*([+\-\/*]?=|\*|<|>|\&)/)
+ when *ENINDENT_CLAUSE
+ valid = false if peek_match?(/^\s*([+\-\/*]?=|\*|<|>|\&|\|)/)
+ else
+ # no nothing
+ end
+ if valid
+ if token == "do"
+ if ![TkFOR, TkWHILE, TkUNTIL].include?(@indent_stack.last)
+ @indent += 1
+ @indent_stack.push token_c
+ end
+ else
+ @indent += 1
+ @indent_stack.push token_c
+ end
+# p @indent_stack
+ end
+
+ elsif DEINDENT_CLAUSE.include?(token)
+ @indent -= 1
+ @indent_stack.pop
+ end
+ @lex_state = trans[0]
+ else
+ @lex_state = EXPR_END
+ end
+ end
+ return Token(token_c, token)
+ end
+ end
+
+ if @lex_state == EXPR_FNAME
+ @lex_state = EXPR_END
+ if peek(0) == '='
+ token.concat getc
+ end
+ elsif @lex_state == EXPR_BEG || @lex_state == EXPR_DOT
+ @lex_state = EXPR_ARG
+ else
+ @lex_state = EXPR_END
+ end
+
+ if token[0, 1] =~ /[A-Z]/
+ return Token(TkCONSTANT, token)
+ elsif token[token.size - 1, 1] =~ /[!?]/
+ return Token(TkFID, token)
+ else
+ return Token(TkIDENTIFIER, token)
+ end
+ end
+
+ def identify_here_document
+ ch = getc
+# if lt = PERCENT_LTYPE[ch]
+ if ch == "-"
+ ch = getc
+ indent = true
+ end
+ if /['"`]/ =~ ch
+ lt = ch
+ quoted = ""
+ while (c = getc) && c != lt
+ quoted.concat c
+ end
+ else
+ lt = '"'
+ quoted = ch.dup
+ while (c = getc) && c =~ /\w/
+ quoted.concat c
+ end
+ ungetc
+ end
+
+ ltback, @ltype = @ltype, lt
+ reserve = []
+ while ch = getc
+ reserve.push ch
+ if ch == "\\"
+ reserve.push ch = getc
+ elsif ch == "\n"
+ break
+ end
+ end
+
+ @here_header = false
+ while l = gets
+ l = l.sub(/(:?\r)?\n\z/, '')
+ if (indent ? l.strip : l) == quoted
+ break
+ end
+ end
+
+ @here_header = true
+ @here_readed.concat reserve
+ while ch = reserve.pop
+ ungetc ch
+ end
+
+ @ltype = ltback
+ @lex_state = EXPR_END
+ Token(Ltype2Token[lt])
+ end
+
+ def identify_quotation
+ ch = getc
+ if lt = PERCENT_LTYPE[ch]
+ ch = getc
+ elsif ch =~ /\W/
+ lt = "\""
+ else
+ RubyLex.fail SyntaxError, "unknown type of %string"
+ end
+# if ch !~ /\W/
+# ungetc
+# next
+# end
+ #@ltype = lt
+ @quoted = ch unless @quoted = PERCENT_PAREN[ch]
+ identify_string(lt, @quoted)
+ end
+
+ def identify_number
+ @lex_state = EXPR_END
+
+ if peek(0) == "0" && peek(1) !~ /[.eE]/
+ getc
+ case peek(0)
+ when /[xX]/
+ ch = getc
+ match = /[0-9a-fA-F_]/
+ when /[bB]/
+ ch = getc
+ match = /[01_]/
+ when /[oO]/
+ ch = getc
+ match = /[0-7_]/
+ when /[dD]/
+ ch = getc
+ match = /[0-9_]/
+ when /[0-7]/
+ match = /[0-7_]/
+ when /[89]/
+ RubyLex.fail SyntaxError, "Invalid octal digit"
+ else
+ return Token(TkINTEGER)
+ end
+
+ len0 = true
+ non_digit = false
+ while ch = getc
+ if match =~ ch
+ if ch == "_"
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{ch}' in number"
+ else
+ non_digit = ch
+ end
+ else
+ non_digit = false
+ len0 = false
+ end
+ else
+ ungetc
+ if len0
+ RubyLex.fail SyntaxError, "numeric literal without digits"
+ end
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
+ break
+ end
+ end
+ return Token(TkINTEGER)
+ end
+
+ type = TkINTEGER
+ allow_point = true
+ allow_e = true
+ non_digit = false
+ while ch = getc
+ case ch
+ when /[0-9]/
+ non_digit = false
+ when "_"
+ non_digit = ch
+ when allow_point && "."
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
+ type = TkFLOAT
+ if peek(0) !~ /[0-9]/
+ type = TkINTEGER
+ ungetc
+ break
+ end
+ allow_point = false
+ when allow_e && "e", allow_e && "E"
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
+ type = TkFLOAT
+ if peek(0) =~ /[+-]/
+ getc
+ end
+ allow_e = false
+ allow_point = false
+ non_digit = ch
+ else
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
+ ungetc
+ break
+ end
+ end
+ Token(type)
+ end
+
+ def identify_string(ltype, quoted = ltype)
+ @ltype = ltype
+ @quoted = quoted
+ subtype = nil
+ begin
+ nest = 0
+ while ch = getc
+ if @quoted == ch and nest == 0
+ break
+ elsif ch == "#" and peek(0) == "{"
+ identify_string_dvar
+ elsif @ltype != "'" && @ltype != "]" && @ltype != ":" and ch == "#"
+ subtype = true
+ elsif ch == '\\' and @ltype == "'" #'
+ case ch = getc
+ when "\\", "\n", "'"
+ else
+ ungetc
+ end
+ elsif ch == '\\' #'
+ read_escape
+ end
+ if PERCENT_PAREN.values.include?(@quoted)
+ if PERCENT_PAREN[ch] == @quoted
+ nest += 1
+ elsif ch == @quoted
+ nest -= 1
+ end
+ end
+ end
+ if @ltype == "/"
+ if peek(0) =~ /i|m|x|o|e|s|u|n/
+ getc
+ end
+ end
+ if subtype
+ Token(DLtype2Token[ltype])
+ else
+ Token(Ltype2Token[ltype])
+ end
+ ensure
+ @ltype = nil
+ @quoted = nil
+ @lex_state = EXPR_END
+ end
+ end
+
+ def identify_string_dvar
+ begin
+ getc
+
+ reserve_continue = @continue
+ reserve_ltype = @ltype
+ reserve_indent = @indent
+ reserve_indent_stack = @indent_stack
+ reserve_state = @lex_state
+ reserve_quoted = @quoted
+
+ @ltype = nil
+ @quoted = nil
+ @indent = 0
+ @indent_stack = []
+ @lex_state = EXPR_BEG
+
+ loop do
+ @continue = false
+ prompt
+ tk = token
+ if @ltype or @continue or @indent > 0
+ next
+ end
+ break if tk.kind_of?(TkRBRACE)
+ end
+ ensure
+ @continue = reserve_continue
+ @ltype = reserve_ltype
+ @indent = reserve_indent
+ @indent_stack = reserve_indent_stack
+ @lex_state = reserve_state
+ @quoted = reserve_quoted
+ end
+ end
+
+ def identify_comment
+ @ltype = "#"
+
+ while ch = getc
+# if ch == "\\" #"
+# read_escape
+# end
+ if ch == "\n"
+ @ltype = nil
+ ungetc
+ break
+ end
+ end
+ return Token(TkCOMMENT)
+ end
+
+ def read_escape
+ case ch = getc
+ when "\n", "\r", "\f"
+ when "\\", "n", "t", "r", "f", "v", "a", "e", "b", "s" #"
+ when /[0-7]/
+ ungetc ch
+ 3.times do
+ case ch = getc
+ when /[0-7]/
+ when nil
+ break
+ else
+ ungetc
+ break
+ end
+ end
+
+ when "x"
+ 2.times do
+ case ch = getc
+ when /[0-9a-fA-F]/
+ when nil
+ break
+ else
+ ungetc
+ break
+ end
+ end
+
+ when "M"
+ if (ch = getc) != '-'
+ ungetc
+ else
+ if (ch = getc) == "\\" #"
+ read_escape
+ end
+ end
+
+ when "C", "c" #, "^"
+ if ch == "C" and (ch = getc) != "-"
+ ungetc
+ elsif (ch = getc) == "\\" #"
+ read_escape
+ end
+ else
+ # other characters
+ end
+ end
+end
diff --git a/ruby/lib/irb/ruby-token.rb b/ruby/lib/irb/ruby-token.rb
new file mode 100644
index 0000000..1e8fe4d
--- /dev/null
+++ b/ruby/lib/irb/ruby-token.rb
@@ -0,0 +1,270 @@
+#
+# irb/ruby-token.rb - ruby tokens
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+module RubyToken
+ EXPR_BEG = :EXPR_BEG
+ EXPR_MID = :EXPR_MID
+ EXPR_END = :EXPR_END
+ EXPR_ARG = :EXPR_ARG
+ EXPR_FNAME = :EXPR_FNAME
+ EXPR_DOT = :EXPR_DOT
+ EXPR_CLASS = :EXPR_CLASS
+
+ # for ruby 1.4X
+ if !defined?(Symbol)
+ Symbol = Integer
+ end
+
+ class Token
+ def initialize(seek, line_no, char_no)
+ @seek = seek
+ @line_no = line_no
+ @char_no = char_no
+ end
+ attr :seek, :line_no, :char_no
+ end
+
+ class TkNode < Token
+ def initialize(seek, line_no, char_no)
+ super
+ end
+ attr :node
+ end
+
+ class TkId < Token
+ def initialize(seek, line_no, char_no, name)
+ super(seek, line_no, char_no)
+ @name = name
+ end
+ attr :name
+ end
+
+ class TkVal < Token
+ def initialize(seek, line_no, char_no, value = nil)
+ super(seek, line_no, char_no)
+ @value = value
+ end
+ attr :value
+ end
+
+ class TkOp < Token
+ attr_accessor :name
+ end
+
+ class TkOPASGN < TkOp
+ def initialize(seek, line_no, char_no, op)
+ super(seek, line_no, char_no)
+ op = TkReading2Token[op][0] unless op.kind_of?(Symbol)
+ @op = op
+ end
+ attr :op
+ end
+
+ class TkUnknownChar < Token
+ def initialize(seek, line_no, char_no, id)
+ super(seek, line_no, char_no)
+ @name = name
+ end
+ attr :name
+ end
+
+ class TkError < Token
+ end
+
+ def Token(token, value = nil)
+ case token
+ when String
+ if (tk = TkReading2Token[token]).nil?
+ IRB.fail TkReading2TokenNoKey, token
+ end
+ tk = Token(tk[0], value)
+ if tk.kind_of?(TkOp)
+ tk.name = token
+ end
+ return tk
+ when Symbol
+ if (tk = TkSymbol2Token[token]).nil?
+ IRB.fail TkSymbol2TokenNoKey, token
+ end
+ return Token(tk[0], value)
+ else
+ if (token.ancestors & [TkId, TkVal, TkOPASGN, TkUnknownChar]).empty?
+ token.new(@prev_seek, @prev_line_no, @prev_char_no)
+ else
+ token.new(@prev_seek, @prev_line_no, @prev_char_no, value)
+ end
+ end
+ end
+
+ TokenDefinitions = [
+ [:TkCLASS, TkId, "class", EXPR_CLASS],
+ [:TkMODULE, TkId, "module", EXPR_BEG],
+ [:TkDEF, TkId, "def", EXPR_FNAME],
+ [:TkUNDEF, TkId, "undef", EXPR_FNAME],
+ [:TkBEGIN, TkId, "begin", EXPR_BEG],
+ [:TkRESCUE, TkId, "rescue", EXPR_MID],
+ [:TkENSURE, TkId, "ensure", EXPR_BEG],
+ [:TkEND, TkId, "end", EXPR_END],
+ [:TkIF, TkId, "if", EXPR_BEG, :TkIF_MOD],
+ [:TkUNLESS, TkId, "unless", EXPR_BEG, :TkUNLESS_MOD],
+ [:TkTHEN, TkId, "then", EXPR_BEG],
+ [:TkELSIF, TkId, "elsif", EXPR_BEG],
+ [:TkELSE, TkId, "else", EXPR_BEG],
+ [:TkCASE, TkId, "case", EXPR_BEG],
+ [:TkWHEN, TkId, "when", EXPR_BEG],
+ [:TkWHILE, TkId, "while", EXPR_BEG, :TkWHILE_MOD],
+ [:TkUNTIL, TkId, "until", EXPR_BEG, :TkUNTIL_MOD],
+ [:TkFOR, TkId, "for", EXPR_BEG],
+ [:TkBREAK, TkId, "break", EXPR_END],
+ [:TkNEXT, TkId, "next", EXPR_END],
+ [:TkREDO, TkId, "redo", EXPR_END],
+ [:TkRETRY, TkId, "retry", EXPR_END],
+ [:TkIN, TkId, "in", EXPR_BEG],
+ [:TkDO, TkId, "do", EXPR_BEG],
+ [:TkRETURN, TkId, "return", EXPR_MID],
+ [:TkYIELD, TkId, "yield", EXPR_END],
+ [:TkSUPER, TkId, "super", EXPR_END],
+ [:TkSELF, TkId, "self", EXPR_END],
+ [:TkNIL, TkId, "nil", EXPR_END],
+ [:TkTRUE, TkId, "true", EXPR_END],
+ [:TkFALSE, TkId, "false", EXPR_END],
+ [:TkAND, TkId, "and", EXPR_BEG],
+ [:TkOR, TkId, "or", EXPR_BEG],
+ [:TkNOT, TkId, "not", EXPR_BEG],
+ [:TkIF_MOD, TkId],
+ [:TkUNLESS_MOD, TkId],
+ [:TkWHILE_MOD, TkId],
+ [:TkUNTIL_MOD, TkId],
+ [:TkALIAS, TkId, "alias", EXPR_FNAME],
+ [:TkDEFINED, TkId, "defined?", EXPR_END],
+ [:TklBEGIN, TkId, "BEGIN", EXPR_END],
+ [:TklEND, TkId, "END", EXPR_END],
+ [:Tk__LINE__, TkId, "__LINE__", EXPR_END],
+ [:Tk__FILE__, TkId, "__FILE__", EXPR_END],
+
+ [:TkIDENTIFIER, TkId],
+ [:TkFID, TkId],
+ [:TkGVAR, TkId],
+ [:TkCVAR, TkId],
+ [:TkIVAR, TkId],
+ [:TkCONSTANT, TkId],
+
+ [:TkINTEGER, TkVal],
+ [:TkFLOAT, TkVal],
+ [:TkSTRING, TkVal],
+ [:TkXSTRING, TkVal],
+ [:TkREGEXP, TkVal],
+ [:TkSYMBOL, TkVal],
+
+ [:TkDSTRING, TkNode],
+ [:TkDXSTRING, TkNode],
+ [:TkDREGEXP, TkNode],
+ [:TkNTH_REF, TkNode],
+ [:TkBACK_REF, TkNode],
+
+ [:TkUPLUS, TkOp, "+@"],
+ [:TkUMINUS, TkOp, "-@"],
+ [:TkPOW, TkOp, "**"],
+ [:TkCMP, TkOp, "<=>"],
+ [:TkEQ, TkOp, "=="],
+ [:TkEQQ, TkOp, "==="],
+ [:TkNEQ, TkOp, "!="],
+ [:TkGEQ, TkOp, ">="],
+ [:TkLEQ, TkOp, "<="],
+ [:TkANDOP, TkOp, "&&"],
+ [:TkOROP, TkOp, "||"],
+ [:TkMATCH, TkOp, "=~"],
+ [:TkNMATCH, TkOp, "!~"],
+ [:TkDOT2, TkOp, ".."],
+ [:TkDOT3, TkOp, "..."],
+ [:TkAREF, TkOp, "[]"],
+ [:TkASET, TkOp, "[]="],
+ [:TkLSHFT, TkOp, "<<"],
+ [:TkRSHFT, TkOp, ">>"],
+ [:TkCOLON2, TkOp],
+ [:TkCOLON3, TkOp],
+# [:OPASGN, TkOp], # +=, -= etc. #
+ [:TkASSOC, TkOp, "=>"],
+ [:TkQUESTION, TkOp, "?"], #?
+ [:TkCOLON, TkOp, ":"], #:
+
+ [:TkfLPAREN], # func( #
+ [:TkfLBRACK], # func[ #
+ [:TkfLBRACE], # func{ #
+ [:TkSTAR], # *arg
+ [:TkAMPER], # &arg #
+ [:TkSYMBEG], # :SYMBOL
+
+ [:TkGT, TkOp, ">"],
+ [:TkLT, TkOp, "<"],
+ [:TkPLUS, TkOp, "+"],
+ [:TkMINUS, TkOp, "-"],
+ [:TkMULT, TkOp, "*"],
+ [:TkDIV, TkOp, "/"],
+ [:TkMOD, TkOp, "%"],
+ [:TkBITOR, TkOp, "|"],
+ [:TkBITXOR, TkOp, "^"],
+ [:TkBITAND, TkOp, "&"],
+ [:TkBITNOT, TkOp, "~"],
+ [:TkNOTOP, TkOp, "!"],
+
+ [:TkBACKQUOTE, TkOp, "`"],
+
+ [:TkASSIGN, Token, "="],
+ [:TkDOT, Token, "."],
+ [:TkLPAREN, Token, "("], #(exp)
+ [:TkLBRACK, Token, "["], #[arry]
+ [:TkLBRACE, Token, "{"], #{hash}
+ [:TkRPAREN, Token, ")"],
+ [:TkRBRACK, Token, "]"],
+ [:TkRBRACE, Token, "}"],
+ [:TkCOMMA, Token, ","],
+ [:TkSEMICOLON, Token, ";"],
+
+ [:TkCOMMENT],
+ [:TkRD_COMMENT],
+ [:TkSPACE],
+ [:TkNL],
+ [:TkEND_OF_SCRIPT],
+
+ [:TkBACKSLASH, TkUnknownChar, "\\"],
+ [:TkAT, TkUnknownChar, "@"],
+ [:TkDOLLAR, TkUnknownChar, "$"],
+ ]
+
+ # {reading => token_class}
+ # {reading => [token_class, *opt]}
+ TkReading2Token = {}
+ TkSymbol2Token = {}
+
+ def RubyToken.def_token(token_n, super_token = Token, reading = nil, *opts)
+ token_n = token_n.id2name if token_n.kind_of?(Symbol)
+ if RubyToken.const_defined?(token_n)
+ IRB.fail AlreadyDefinedToken, token_n
+ end
+ token_c = eval("class #{token_n} < #{super_token}; end; #{token_n}")
+
+ if reading
+ if TkReading2Token[reading]
+ IRB.fail TkReading2TokenDuplicateError, token_n, reading
+ end
+ if opts.empty?
+ TkReading2Token[reading] = [token_c]
+ else
+ TkReading2Token[reading] = [token_c].concat(opts)
+ end
+ end
+ TkSymbol2Token[token_n.intern] = token_c
+ end
+
+ for defs in TokenDefinitions
+ def_token(*defs)
+ end
+end
diff --git a/ruby/lib/irb/slex.rb b/ruby/lib/irb/slex.rb
new file mode 100644
index 0000000..4210f27
--- /dev/null
+++ b/ruby/lib/irb/slex.rb
@@ -0,0 +1,282 @@
+#
+# irb/slex.rb - simple lex analyzer
+# $Release Version: 0.9.5$
+# $Revision: 16810 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+require "irb/notifier"
+
+module IRB
+ class SLex
+ @RCS_ID='-$Id: slex.rb 16810 2008-06-04 09:37:38Z matz $-'
+
+ extend Exception2MessageMapper
+ def_exception :ErrNodeNothing, "node nothing"
+ def_exception :ErrNodeAlreadyExists, "node already exists"
+
+ DOUT = Notifier::def_notifier("SLex::")
+ D_WARN = DOUT::def_notifier(1, "Warn: ")
+ D_DEBUG = DOUT::def_notifier(2, "Debug: ")
+ D_DETAIL = DOUT::def_notifier(4, "Detail: ")
+
+ DOUT.level = Notifier::D_NOMSG
+
+ def initialize
+ @head = Node.new("")
+ end
+
+ def def_rule(token, preproc = nil, postproc = nil, &block)
+ D_DETAIL.pp token
+
+ postproc = block if block_given?
+ node = create(token, preproc, postproc)
+ end
+
+ def def_rules(*tokens, &block)
+ if block_given?
+ p = block
+ end
+ for token in tokens
+ def_rule(token, nil, p)
+ end
+ end
+
+ def preproc(token, proc)
+ node = search(token)
+ node.preproc=proc
+ end
+
+ #$BMW%A%'%C%/(B?
+ def postproc(token)
+ node = search(token, proc)
+ node.postproc=proc
+ end
+
+ def search(token)
+ @head.search(token.split(//))
+ end
+
+ def create(token, preproc = nil, postproc = nil)
+ @head.create_subnode(token.split(//), preproc, postproc)
+ end
+
+ def match(token)
+ case token
+ when Array
+ when String
+ return match(token.split(//))
+ else
+ return @head.match_io(token)
+ end
+ ret = @head.match(token)
+ D_DETAIL.exec_if{D_DEATIL.printf "match end: %s:%s\n", ret, token.inspect}
+ ret
+ end
+
+ def inspect
+ format("<SLex: @head = %s>", @head.inspect)
+ end
+
+ #----------------------------------------------------------------------
+ #
+ # class Node -
+ #
+ #----------------------------------------------------------------------
+ class Node
+ # if postproc is nil, this node is an abstract node.
+ # if postproc is non-nil, this node is a real node.
+ def initialize(preproc = nil, postproc = nil)
+ @Tree = {}
+ @preproc = preproc
+ @postproc = postproc
+ end
+
+ attr_accessor :preproc
+ attr_accessor :postproc
+
+ def search(chrs, opt = nil)
+ return self if chrs.empty?
+ ch = chrs.shift
+ if node = @Tree[ch]
+ node.search(chrs, opt)
+ else
+ if opt
+ chrs.unshift ch
+ self.create_subnode(chrs)
+ else
+ SLex.fail ErrNodeNothing
+ end
+ end
+ end
+
+ def create_subnode(chrs, preproc = nil, postproc = nil)
+ if chrs.empty?
+ if @postproc
+ D_DETAIL.pp node
+ SLex.fail ErrNodeAlreadyExists
+ else
+ D_DEBUG.puts "change abstract node to real node."
+ @preproc = preproc
+ @postproc = postproc
+ end
+ return self
+ end
+
+ ch = chrs.shift
+ if node = @Tree[ch]
+ if chrs.empty?
+ if node.postproc
+ DebugLogger.pp node
+ DebugLogger.pp self
+ DebugLogger.pp ch
+ DebugLogger.pp chrs
+ SLex.fail ErrNodeAlreadyExists
+ else
+ D_WARN.puts "change abstract node to real node"
+ node.preproc = preproc
+ node.postproc = postproc
+ end
+ else
+ node.create_subnode(chrs, preproc, postproc)
+ end
+ else
+ if chrs.empty?
+ node = Node.new(preproc, postproc)
+ else
+ node = Node.new
+ node.create_subnode(chrs, preproc, postproc)
+ end
+ @Tree[ch] = node
+ end
+ node
+ end
+
+ #
+ # chrs: String
+ # character array
+ # io must have getc()/ungetc(); and ungetc() must be
+ # able to be called arbitrary number of times.
+ #
+ def match(chrs, op = "")
+ D_DETAIL.print "match>: ", chrs, "op:", op, "\n"
+ if chrs.empty?
+ if @preproc.nil? || @preproc.call(op, chrs)
+ DOUT.printf(D_DETAIL, "op1: %s\n", op)
+ @postproc.call(op, chrs)
+ else
+ nil
+ end
+ else
+ ch = chrs.shift
+ if node = @Tree[ch]
+ if ret = node.match(chrs, op+ch)
+ return ret
+ else
+ chrs.unshift ch
+ if @postproc and @preproc.nil? || @preproc.call(op, chrs)
+ DOUT.printf(D_DETAIL, "op2: %s\n", op.inspect)
+ ret = @postproc.call(op, chrs)
+ return ret
+ else
+ return nil
+ end
+ end
+ else
+ chrs.unshift ch
+ if @postproc and @preproc.nil? || @preproc.call(op, chrs)
+ DOUT.printf(D_DETAIL, "op3: %s\n", op)
+ @postproc.call(op, chrs)
+ return ""
+ else
+ return nil
+ end
+ end
+ end
+ end
+
+ def match_io(io, op = "")
+ if op == ""
+ ch = io.getc
+ if ch == nil
+ return nil
+ end
+ else
+ ch = io.getc_of_rests
+ end
+ if ch.nil?
+ if @preproc.nil? || @preproc.call(op, io)
+ D_DETAIL.printf("op1: %s\n", op)
+ @postproc.call(op, io)
+ else
+ nil
+ end
+ else
+ if node = @Tree[ch]
+ if ret = node.match_io(io, op+ch)
+ ret
+ else
+ io.ungetc ch
+ if @postproc and @preproc.nil? || @preproc.call(op, io)
+ DOUT.exec_if{D_DETAIL.printf "op2: %s\n", op.inspect}
+ @postproc.call(op, io)
+ else
+ nil
+ end
+ end
+ else
+ io.ungetc ch
+ if @postproc and @preproc.nil? || @preproc.call(op, io)
+ D_DETAIL.printf("op3: %s\n", op)
+ @postproc.call(op, io)
+ else
+ nil
+ end
+ end
+ end
+ end
+ end
+ end
+end
+
+if $0 == __FILE__
+ # Tracer.on
+ case $1
+ when "1"
+ tr = SLex.new
+ print "0: ", tr.inspect, "\n"
+ tr.def_rule("=") {print "=\n"}
+ print "1: ", tr.inspect, "\n"
+ tr.def_rule("==") {print "==\n"}
+ print "2: ", tr.inspect, "\n"
+
+ print "case 1:\n"
+ print tr.match("="), "\n"
+ print "case 2:\n"
+ print tr.match("=="), "\n"
+ print "case 3:\n"
+ print tr.match("=>"), "\n"
+
+ when "2"
+ tr = SLex.new
+ print "0: ", tr.inspect, "\n"
+ tr.def_rule("=") {print "=\n"}
+ print "1: ", tr.inspect, "\n"
+ tr.def_rule("==", proc{false}) {print "==\n"}
+ print "2: ", tr.inspect, "\n"
+
+ print "case 1:\n"
+ print tr.match("="), "\n"
+ print "case 2:\n"
+ print tr.match("=="), "\n"
+ print "case 3:\n"
+ print tr.match("=>"), "\n"
+ end
+ exit
+end
+
diff --git a/ruby/lib/irb/src_encoding.rb b/ruby/lib/irb/src_encoding.rb
new file mode 100644
index 0000000..958cef1
--- /dev/null
+++ b/ruby/lib/irb/src_encoding.rb
@@ -0,0 +1,4 @@
+# DO NOT WRITE ANY MAGIC COMMENT HERE.
+def default_src_encoding
+ return __ENCODING__
+end
diff --git a/ruby/lib/irb/version.rb b/ruby/lib/irb/version.rb
new file mode 100644
index 0000000..60123d0
--- /dev/null
+++ b/ruby/lib/irb/version.rb
@@ -0,0 +1,15 @@
+#
+# irb/version.rb - irb version definition file
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ishitsuka.com)
+#
+# --
+#
+#
+#
+
+module IRB
+ @RELEASE_VERSION = "0.9.5"
+ @LAST_UPDATE_DATE = "05/04/13"
+end
diff --git a/ruby/lib/irb/workspace.rb b/ruby/lib/irb/workspace.rb
new file mode 100644
index 0000000..a2b8b5a
--- /dev/null
+++ b/ruby/lib/irb/workspace.rb
@@ -0,0 +1,108 @@
+#
+# irb/workspace-binding.rb -
+# $Release Version: 0.9.5$
+# $Revision: 15689 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+module IRB
+ class WorkSpace
+ # create new workspace. set self to main if specified, otherwise
+ # inherit main from TOPLEVEL_BINDING.
+ def initialize(*main)
+ if main[0].kind_of?(Binding)
+ @binding = main.shift
+ elsif IRB.conf[:SINGLE_IRB]
+ @binding = TOPLEVEL_BINDING
+ else
+ case IRB.conf[:CONTEXT_MODE]
+ when 0 # binding in proc on TOPLEVEL_BINDING
+ @binding = eval("proc{binding}.call",
+ TOPLEVEL_BINDING,
+ __FILE__,
+ __LINE__)
+ when 1 # binding in loaded file
+ require "tempfile"
+ f = Tempfile.open("irb-binding")
+ f.print <<EOF
+ $binding = binding
+EOF
+ f.close
+ load f.path
+ @binding = $binding
+
+ when 2 # binding in loaded file(thread use)
+ unless defined? BINDING_QUEUE
+ require "thread"
+
+ IRB.const_set("BINDING_QUEUE", SizedQueue.new(1))
+ Thread.abort_on_exception = true
+ Thread.start do
+ eval "require \"irb/ws-for-case-2\"", TOPLEVEL_BINDING, __FILE__, __LINE__
+ end
+ Thread.pass
+ end
+ @binding = BINDING_QUEUE.pop
+
+ when 3 # binging in function on TOPLEVEL_BINDING(default)
+ @binding = eval("def irb_binding; binding; end; irb_binding",
+ TOPLEVEL_BINDING,
+ __FILE__,
+ __LINE__ - 3)
+ end
+ end
+ if main.empty?
+ @main = eval("self", @binding)
+ else
+ @main = main[0]
+ IRB.conf[:__MAIN__] = @main
+ case @main
+ when Module
+ @binding = eval("IRB.conf[:__MAIN__].module_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
+ else
+ begin
+ @binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
+ rescue TypeError
+ IRB.fail CantChangeBinding, @main.inspect
+ end
+ end
+ end
+ eval("_=nil", @binding)
+ end
+
+ attr_reader :binding
+ attr_reader :main
+
+ def evaluate(context, statements, file = __FILE__, line = __LINE__)
+ eval(statements, @binding, file, line)
+ end
+
+ # error message manipulator
+ def filter_backtrace(bt)
+ case IRB.conf[:CONTEXT_MODE]
+ when 0
+ return nil if bt =~ /\(irb_local_binding\)/
+ when 1
+ if(bt =~ %r!/tmp/irb-binding! or
+ bt =~ %r!irb/.*\.rb! or
+ bt =~ /irb\.rb/)
+ return nil
+ end
+ when 2
+ return nil if bt =~ /irb\/.*\.rb/
+ return nil if bt =~ /irb\.rb/
+ when 3
+ return nil if bt =~ /irb\/.*\.rb/
+ return nil if bt =~ /irb\.rb/
+ bt.sub!(/:\s*in `irb_binding'/, '')
+ end
+ bt
+ end
+
+ def IRB.delete_caller
+ end
+ end
+end
diff --git a/ruby/lib/irb/ws-for-case-2.rb b/ruby/lib/irb/ws-for-case-2.rb
new file mode 100644
index 0000000..bf54c97
--- /dev/null
+++ b/ruby/lib/irb/ws-for-case-2.rb
@@ -0,0 +1,14 @@
+#
+# irb/ws-for-case-2.rb -
+# $Release Version: 0.9.5$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+while true
+ IRB::BINDING_QUEUE.push b = binding
+end
diff --git a/ruby/lib/irb/xmp.rb b/ruby/lib/irb/xmp.rb
new file mode 100644
index 0000000..f46484f
--- /dev/null
+++ b/ruby/lib/irb/xmp.rb
@@ -0,0 +1,97 @@
+#
+# xmp.rb - irb version of gotoken xmp
+# $Release Version: 0.9$
+# $Revision: 21633 $
+# by Keiju ISHITSUKA(Nippon Rational Inc.)
+#
+# --
+#
+#
+#
+
+require "irb"
+require "irb/frame"
+
+class XMP
+ @RCS_ID='-$Id: xmp.rb 21633 2009-01-17 12:19:53Z yugui $-'
+
+ def initialize(bind = nil)
+ IRB.init_config(nil)
+ #IRB.parse_opts
+ #IRB.load_modules
+
+ IRB.conf[:PROMPT_MODE] = :XMP
+
+ bind = IRB::Frame.top(1) unless bind
+ ws = IRB::WorkSpace.new(bind)
+ @io = StringInputMethod.new
+ @irb = IRB::Irb.new(ws, @io)
+ @irb.context.ignore_sigint = false
+
+# IRB.conf[:IRB_RC].call(@irb.context) if IRB.conf[:IRB_RC]
+ IRB.conf[:MAIN_CONTEXT] = @irb.context
+ end
+
+ def puts(exps)
+ @io.puts exps
+
+ if @irb.context.ignore_sigint
+ begin
+ trap_proc_b = trap("SIGINT"){@irb.signal_handle}
+ catch(:IRB_EXIT) do
+ @irb.eval_input
+ end
+ ensure
+ trap("SIGINT", trap_proc_b)
+ end
+ else
+ catch(:IRB_EXIT) do
+ @irb.eval_input
+ end
+ end
+ end
+
+ class StringInputMethod < IRB::InputMethod
+ def initialize
+ super
+ @exps = []
+ end
+
+ def eof?
+ @exps.empty?
+ end
+
+ def gets
+ while l = @exps.shift
+ next if /^\s+$/ =~ l
+ l.concat "\n"
+ print @prompt, l
+ break
+ end
+ l
+ end
+
+ def puts(exps)
+ if @encoding and exps.encoding != @encoding
+ enc = Encoding.compatible?(@exps.join("\n"), exps)
+ if enc.nil?
+ raise Encoding::CompatibilityError, "Encoding in which the passed exression is encoded is not compatible to the preceding's one"
+ else
+ @encoding = enc
+ end
+ else
+ @encoding = exps.encoding
+ end
+ @exps.concat exps.split(/\n/)
+ end
+
+ attr_reader :encoding
+ end
+end
+
+def xmp(exps, bind = nil)
+ bind = IRB::Frame.top(1) unless bind
+ xmp = XMP.new(bind)
+ xmp.puts exps
+ xmp
+end
diff --git a/ruby/lib/json.rb b/ruby/lib/json.rb
new file mode 100644
index 0000000..9e5c3f7
--- /dev/null
+++ b/ruby/lib/json.rb
@@ -0,0 +1,297 @@
+require 'json/common'
+# = json - JSON for Ruby
+#
+# == Description
+#
+# This is a implementation of the JSON specification according to RFC 4627
+# (http://www.ietf.org/rfc/rfc4627.txt). Starting from version 1.0.0 on there
+# will be two variants available:
+#
+# * A pure ruby variant, that relies on the iconv and the stringscan
+# extensions, which are both part of the ruby standard library.
+# * The quite a bit faster C extension variant, which is in parts implemented
+# in C and comes with its own unicode conversion functions and a parser
+# generated by the ragel state machine compiler
+# (http://www.cs.queensu.ca/~thurston/ragel).
+#
+# Both variants of the JSON generator escape all non-ASCII an control
+# characters with \uXXXX escape sequences, and support UTF-16 surrogate pairs
+# in order to be able to generate the whole range of unicode code points. This
+# means that generated JSON text is encoded as UTF-8 (because ASCII is a subset
+# of UTF-8) and at the same time avoids decoding problems for receiving
+# endpoints, that don't expect UTF-8 encoded texts. On the negative side this
+# may lead to a bit longer strings than necessarry.
+#
+# All strings, that are to be encoded as JSON strings, should be UTF-8 byte
+# sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8
+# encoded, please use the to_json_raw_object method of String (which produces
+# an object, that contains a byte array) and decode the result on the receiving
+# endpoint.
+#
+# == Author
+#
+# Florian Frank <mailto:flori@ping.de>
+#
+# == License
+#
+# This software is distributed under the same license as Ruby itself, see
+# http://www.ruby-lang.org/en/LICENSE.txt.
+#
+# == Download
+#
+# The latest version of this library can be downloaded at
+#
+# * http://rubyforge.org/frs?group_id=953
+#
+# Online Documentation should be located at
+#
+# * http://json.rubyforge.org
+#
+# == Usage
+#
+# To use JSON you can
+# require 'json'
+# to load the installed variant (either the extension 'json' or the pure
+# variant 'json_pure'). If you have installed the extension variant, you can
+# pick either the extension variant or the pure variant by typing
+# require 'json/ext'
+# or
+# require 'json/pure'
+#
+# You can choose to load a set of common additions to ruby core's objects if
+# you
+# require 'json/add/core'
+#
+# After requiring this you can, e. g., serialise/deserialise Ruby ranges:
+#
+# JSON JSON(1..10) # => 1..10
+#
+# To find out how to add JSON support to other or your own classes, read the
+# Examples section below.
+#
+# To get the best compatibility to rails' JSON implementation, you can
+# require 'json/add/rails'
+#
+# Both of the additions attempt to require 'json' (like above) first, if it has
+# not been required yet.
+#
+# == Speed Comparisons
+#
+# I have created some benchmark results (see the benchmarks/data-p4-3Ghz
+# subdir of the package) for the JSON-parser to estimate the speed up in the C
+# extension:
+#
+# Comparing times (call_time_mean):
+# 1 ParserBenchmarkExt#parser 900 repeats:
+# 553.922304770 ( real) -> 21.500x
+# 0.001805307
+# 2 ParserBenchmarkYAML#parser 1000 repeats:
+# 224.513358139 ( real) -> 8.714x
+# 0.004454078
+# 3 ParserBenchmarkPure#parser 1000 repeats:
+# 26.755020642 ( real) -> 1.038x
+# 0.037376163
+# 4 ParserBenchmarkRails#parser 1000 repeats:
+# 25.763381731 ( real) -> 1.000x
+# 0.038814780
+# calls/sec ( time) -> speed covers
+# secs/call
+#
+# In the table above 1 is JSON::Ext::Parser, 2 is YAML.load with YAML
+# compatbile JSON document, 3 is is JSON::Pure::Parser, and 4 is
+# ActiveSupport::JSON.decode. The ActiveSupport JSON-decoder converts the
+# input first to YAML and then uses the YAML-parser, the conversion seems to
+# slow it down so much that it is only as fast as the JSON::Pure::Parser!
+#
+# If you look at the benchmark data you can see that this is mostly caused by
+# the frequent high outliers - the median of the Rails-parser runs is still
+# overall smaller than the median of the JSON::Pure::Parser runs:
+#
+# Comparing times (call_time_median):
+# 1 ParserBenchmarkExt#parser 900 repeats:
+# 800.592479481 ( real) -> 26.936x
+# 0.001249075
+# 2 ParserBenchmarkYAML#parser 1000 repeats:
+# 271.002390644 ( real) -> 9.118x
+# 0.003690004
+# 3 ParserBenchmarkRails#parser 1000 repeats:
+# 30.227910865 ( real) -> 1.017x
+# 0.033082008
+# 4 ParserBenchmarkPure#parser 1000 repeats:
+# 29.722384421 ( real) -> 1.000x
+# 0.033644676
+# calls/sec ( time) -> speed covers
+# secs/call
+#
+# I have benchmarked the JSON-Generator as well. This generated a few more
+# values, because there are different modes that also influence the achieved
+# speed:
+#
+# Comparing times (call_time_mean):
+# 1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
+# 547.354332608 ( real) -> 15.090x
+# 0.001826970
+# 2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
+# 443.968212317 ( real) -> 12.240x
+# 0.002252414
+# 3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
+# 375.104545883 ( real) -> 10.341x
+# 0.002665923
+# 4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
+# 49.978706968 ( real) -> 1.378x
+# 0.020008521
+# 5 GeneratorBenchmarkRails#generator 1000 repeats:
+# 38.531868759 ( real) -> 1.062x
+# 0.025952543
+# 6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
+# 36.927649925 ( real) -> 1.018x 7 (>=3859)
+# 0.027079979
+# 7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
+# 36.272134441 ( real) -> 1.000x 6 (>=3859)
+# 0.027569373
+# calls/sec ( time) -> speed covers
+# secs/call
+#
+# In the table above 1-3 are JSON::Ext::Generator methods. 4, 6, and 7 are
+# JSON::Pure::Generator methods and 5 is the Rails JSON generator. It is now a
+# bit faster than the generator_safe and generator_pretty methods of the pure
+# variant but slower than the others.
+#
+# To achieve the fastest JSON text output, you can use the fast_generate
+# method. Beware, that this will disable the checking for circular Ruby data
+# structures, which may cause JSON to go into an infinite loop.
+#
+# Here are the median comparisons for completeness' sake:
+#
+# Comparing times (call_time_median):
+# 1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
+# 708.258020939 ( real) -> 16.547x
+# 0.001411915
+# 2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
+# 569.105020353 ( real) -> 13.296x
+# 0.001757145
+# 3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
+# 482.825371244 ( real) -> 11.280x
+# 0.002071142
+# 4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
+# 62.717626652 ( real) -> 1.465x
+# 0.015944481
+# 5 GeneratorBenchmarkRails#generator 1000 repeats:
+# 43.965681162 ( real) -> 1.027x
+# 0.022745013
+# 6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
+# 43.929073409 ( real) -> 1.026x 7 (>=3859)
+# 0.022763968
+# 7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
+# 42.802514491 ( real) -> 1.000x 6 (>=3859)
+# 0.023363113
+# calls/sec ( time) -> speed covers
+# secs/call
+#
+# == Examples
+#
+# To create a JSON text from a ruby data structure, you can call JSON.generate
+# like that:
+#
+# json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
+# # => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
+#
+# To create a valid JSON text you have to make sure, that the output is
+# embedded in either a JSON array [] or a JSON object {}. The easiest way to do
+# this, is by putting your values in a Ruby Array or Hash instance.
+#
+# To get back a ruby data structure from a JSON text, you have to call
+# JSON.parse on it:
+#
+# JSON.parse json
+# # => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
+#
+# Note, that the range from the original data structure is a simple
+# string now. The reason for this is, that JSON doesn't support ranges
+# or arbitrary classes. In this case the json library falls back to call
+# Object#to_json, which is the same as #to_s.to_json.
+#
+# It's possible to add JSON support serialization to arbitrary classes by
+# simply implementing a more specialized version of the #to_json method, that
+# should return a JSON object (a hash converted to JSON with #to_json) like
+# this (don't forget the *a for all the arguments):
+#
+# class Range
+# def to_json(*a)
+# {
+# 'json_class' => self.class.name, # = 'Range'
+# 'data' => [ first, last, exclude_end? ]
+# }.to_json(*a)
+# end
+# end
+#
+# The hash key 'json_class' is the class, that will be asked to deserialise the
+# JSON representation later. In this case it's 'Range', but any namespace of
+# the form 'A::B' or '::A::B' will do. All other keys are arbitrary and can be
+# used to store the necessary data to configure the object to be deserialised.
+#
+# If a the key 'json_class' is found in a JSON object, the JSON parser checks
+# if the given class responds to the json_create class method. If so, it is
+# called with the JSON object converted to a Ruby hash. So a range can
+# be deserialised by implementing Range.json_create like this:
+#
+# class Range
+# def self.json_create(o)
+# new(*o['data'])
+# end
+# end
+#
+# Now it possible to serialise/deserialise ranges as well:
+#
+# json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
+# # => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
+# JSON.parse json
+# # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
+#
+# JSON.generate always creates the shortest possible string representation of a
+# ruby data structure in one line. This good for data storage or network
+# protocols, but not so good for humans to read. Fortunately there's also
+# JSON.pretty_generate (or JSON.pretty_generate) that creates a more
+# readable output:
+#
+# puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])
+# [
+# 1,
+# 2,
+# {
+# "a": 3.141
+# },
+# false,
+# true,
+# null,
+# {
+# "json_class": "Range",
+# "data": [
+# 4,
+# 10,
+# false
+# ]
+# }
+# ]
+#
+# There are also the methods Kernel#j for generate, and Kernel#jj for
+# pretty_generate output to the console, that work analogous to Core Ruby's p
+# and the pp library's pp methods.
+#
+# The script tools/server.rb contains a small example if you want to test, how
+# receiving a JSON object from a webrick server in your browser with the
+# javasript prototype library (http://www.prototypejs.org) works.
+#
+module JSON
+ require 'json/version'
+
+ if VARIANT_BINARY
+ require 'json/ext'
+ else
+ begin
+ require 'json/ext'
+ rescue LoadError
+ require 'json/pure'
+ end
+ end
+end
diff --git a/ruby/lib/json/add/core.rb b/ruby/lib/json/add/core.rb
new file mode 100644
index 0000000..4423e7a
--- /dev/null
+++ b/ruby/lib/json/add/core.rb
@@ -0,0 +1,135 @@
+# This file contains implementations of ruby core's custom objects for
+# serialisation/deserialisation.
+
+unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
+ ::JSON::JSON_LOADED
+ require 'json'
+end
+require 'date'
+
+class Time
+ def self.json_create(object)
+ if usec = object.delete('u') # used to be tv_usec -> tv_nsec
+ object['n'] = usec * 1000
+ end
+ if respond_to?(:tv_nsec)
+ at(*object.values_at('s', 'n'))
+ else
+ at(object['s'], object['n'] / 1000)
+ end
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 's' => tv_sec,
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
+ }.to_json(*args)
+ end
+end
+
+class Date
+ def self.json_create(object)
+ civil(*object.values_at('y', 'm', 'd', 'sg'))
+ end
+
+ alias start sg unless method_defined?(:start)
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'y' => year,
+ 'm' => month,
+ 'd' => day,
+ 'sg' => start,
+ }.to_json(*args)
+ end
+end
+
+class DateTime
+ def self.json_create(object)
+ args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
+ of_a, of_b = object['of'].split('/')
+ if of_b and of_b != '0'
+ args << Rational(of_a.to_i, of_b.to_i)
+ else
+ args << of_a
+ end
+ args << object['sg']
+ civil(*args)
+ end
+
+ alias start sg unless method_defined?(:start)
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'y' => year,
+ 'm' => month,
+ 'd' => day,
+ 'H' => hour,
+ 'M' => min,
+ 'S' => sec,
+ 'of' => offset.to_s,
+ 'sg' => start,
+ }.to_json(*args)
+ end
+end
+
+class Range
+ def self.json_create(object)
+ new(*object['a'])
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'a' => [ first, last, exclude_end? ]
+ }.to_json(*args)
+ end
+end
+
+class Struct
+ def self.json_create(object)
+ new(*object['v'])
+ end
+
+ def to_json(*args)
+ klass = self.class.name
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
+ {
+ 'json_class' => klass,
+ 'v' => values,
+ }.to_json(*args)
+ end
+end
+
+class Exception
+ def self.json_create(object)
+ result = new(object['m'])
+ result.set_backtrace object['b']
+ result
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'm' => message,
+ 'b' => backtrace,
+ }.to_json(*args)
+ end
+end
+
+class Regexp
+ def self.json_create(object)
+ new(object['s'], object['o'])
+ end
+
+ def to_json(*)
+ {
+ 'json_class' => self.class.name,
+ 'o' => options,
+ 's' => source,
+ }.to_json
+ end
+end
diff --git a/ruby/lib/json/add/rails.rb b/ruby/lib/json/add/rails.rb
new file mode 100644
index 0000000..e86ed1a
--- /dev/null
+++ b/ruby/lib/json/add/rails.rb
@@ -0,0 +1,58 @@
+# This file contains implementations of rails custom objects for
+# serialisation/deserialisation.
+
+unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
+ ::JSON::JSON_LOADED
+ require 'json'
+end
+
+class Object
+ def self.json_create(object)
+ obj = new
+ for key, value in object
+ next if key == 'json_class'
+ instance_variable_set "@#{key}", value
+ end
+ obj
+ end
+
+ def to_json(*a)
+ result = {
+ 'json_class' => self.class.name
+ }
+ instance_variables.inject(result) do |r, name|
+ r[name[1..-1]] = instance_variable_get name
+ r
+ end
+ result.to_json(*a)
+ end
+end
+
+class Symbol
+ def to_json(*a)
+ to_s.to_json(*a)
+ end
+end
+
+module Enumerable
+ def to_json(*a)
+ to_a.to_json(*a)
+ end
+end
+
+# class Regexp
+# def to_json(*)
+# inspect
+# end
+# end
+#
+# The above rails definition has some problems:
+#
+# 1. { 'foo' => /bar/ }.to_json # => "{foo: /bar/}"
+# This isn't valid JSON, because the regular expression syntax is not
+# defined in RFC 4627. (And unquoted strings are disallowed there, too.)
+# Though it is valid Javascript.
+#
+# 2. { 'foo' => /bar/mix }.to_json # => "{foo: /bar/mix}"
+# This isn't even valid Javascript.
+
diff --git a/ruby/lib/json/common.rb b/ruby/lib/json/common.rb
new file mode 100644
index 0000000..499fcc0
--- /dev/null
+++ b/ruby/lib/json/common.rb
@@ -0,0 +1,354 @@
+require 'json/version'
+
+module JSON
+ class << self
+ # If _object_ is string-like parse the string and return the parsed result
+ # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
+ # data structure object and return it.
+ #
+ # The _opts_ argument is passed through to generate/parse respectively, see
+ # generate and parse for their documentation.
+ def [](object, opts = {})
+ if object.respond_to? :to_str
+ JSON.parse(object.to_str, opts => {})
+ else
+ JSON.generate(object, opts => {})
+ end
+ end
+
+ # Returns the JSON parser class, that is used by JSON. This might be either
+ # JSON::Ext::Parser or JSON::Pure::Parser.
+ attr_reader :parser
+
+ # Set the JSON parser class _parser_ to be used by JSON.
+ def parser=(parser) # :nodoc:
+ @parser = parser
+ remove_const :Parser if const_defined? :Parser
+ const_set :Parser, parser
+ end
+
+ # Return the constant located at _path_. The format of _path_ has to be
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
+ # level (absolute namespace path?). If there doesn't exist a constant at
+ # the given path, an ArgumentError is raised.
+ def deep_const_get(path) # :nodoc:
+ path = path.to_s
+ path.split(/::/).inject(Object) do |p, c|
+ case
+ when c.empty? then p
+ when p.const_defined?(c) then p.const_get(c)
+ else raise ArgumentError, "can't find const #{path}"
+ end
+ end
+ end
+
+ # Set the module _generator_ to be used by JSON.
+ def generator=(generator) # :nodoc:
+ @generator = generator
+ generator_methods = generator::GeneratorMethods
+ for const in generator_methods.constants
+ klass = deep_const_get(const)
+ modul = generator_methods.const_get(const)
+ klass.class_eval do
+ instance_methods(false).each do |m|
+ m.to_s == 'to_json' and remove_method m
+ end
+ include modul
+ end
+ end
+ self.state = generator::State
+ const_set :State, self.state
+ end
+
+ # Returns the JSON generator modul, that is used by JSON. This might be
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
+ attr_reader :generator
+
+ # Returns the JSON generator state class, that is used by JSON. This might
+ # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
+ attr_accessor :state
+
+ # This is create identifier, that is used to decide, if the _json_create_
+ # hook of a class should be called. It defaults to 'json_class'.
+ attr_accessor :create_id
+ end
+ self.create_id = 'json_class'
+
+ NaN = (-1.0) ** 0.5
+
+ Infinity = 1.0/0
+
+ MinusInfinity = -Infinity
+
+ # The base exception for JSON errors.
+ class JSONError < StandardError; end
+
+ # This exception is raised, if a parser error occurs.
+ class ParserError < JSONError; end
+
+ # This exception is raised, if the nesting of parsed datastructures is too
+ # deep.
+ class NestingError < ParserError; end
+
+ # This exception is raised, if a generator or unparser error occurs.
+ class GeneratorError < JSONError; end
+ # For backwards compatibility
+ UnparserError = GeneratorError
+
+ # If a circular data structure is encountered while unparsing
+ # this exception is raised.
+ class CircularDatastructure < GeneratorError; end
+
+ # This exception is raised, if the required unicode support is missing on the
+ # system. Usually this means, that the iconv library is not installed.
+ class MissingUnicodeSupport < JSONError; end
+
+ module_function
+
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
+ #
+ # _opts_ can have the following
+ # keys:
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
+ # structures. Disable depth checking with :max_nesting => false, it defaults
+ # to 19.
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
+ # to false.
+ # * *create_additions*: If set to false, the Parser doesn't create
+ # additions even if a matchin class and create_id was found. This option
+ # defaults to true.
+ def parse(source, opts = {})
+ JSON.parser.new(source, opts).parse
+ end
+
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
+ # The bang version of the parse method, defaults to the more dangerous values
+ # for the _opts_ hash, so be sure only to parse trusted _source_ strings.
+ #
+ # _opts_ can have the following keys:
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
+ # methods defaults to not doing max depth checking: This can be dangerous,
+ # if someone wants to fill up your stack.
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
+ # to true.
+ # * *create_additions*: If set to false, the Parser doesn't create
+ # additions even if a matchin class and create_id was found. This option
+ # defaults to true.
+ def parse!(source, opts = {})
+ opts = {
+ :max_nesting => false,
+ :allow_nan => true
+ }.update(opts)
+ JSON.parser.new(source, opts).parse
+ end
+
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
+ # return it. _state_ is
+ # * a JSON::State object,
+ # * or a Hash like object (responding to to_hash),
+ # * an object convertible into a hash by a to_h method,
+ # that is used as or to configure a State object.
+ #
+ # It defaults to a state object, that creates the shortest possible JSON text
+ # in one line, checks for circular data structures and doesn't allow NaN,
+ # Infinity, and -Infinity.
+ #
+ # A _state_ hash can have the following keys:
+ # * *indent*: a string used to indent levels (default: ''),
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
+ # * *check_circular*: true if checking for circular data structures
+ # should be done (the default), false otherwise.
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
+ # generated, otherwise an exception is thrown, if these values are
+ # encountered. This options defaults to false.
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
+ # structures from which JSON is to be generated. Disable depth checking
+ # with :max_nesting => false, it defaults to 19.
+ #
+ # See also the fast_generate for the fastest creation method with the least
+ # amount of sanity checks, and the pretty_generate method for some
+ # defaults for a pretty output.
+ def generate(obj, state = nil)
+ if state
+ state = State.from_state(state)
+ else
+ state = State.new
+ end
+ obj.to_json(state)
+ end
+
+ # :stopdoc:
+ # I want to deprecate these later, so I'll first be silent about them, and
+ # later delete them.
+ alias unparse generate
+ module_function :unparse
+ # :startdoc:
+
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
+ # return it. This method disables the checks for circles in Ruby objects, and
+ # also generates NaN, Infinity, and, -Infinity float values.
+ #
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
+ def fast_generate(obj)
+ obj.to_json(nil)
+ end
+
+ # :stopdoc:
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
+ alias fast_unparse fast_generate
+ module_function :fast_unparse
+ # :startdoc:
+
+ # Unparse the Ruby data structure _obj_ into a JSON string and return it. The
+ # returned string is a prettier form of the string returned by #unparse.
+ #
+ # The _opts_ argument can be used to configure the generator, see the
+ # generate method for a more detailed explanation.
+ def pretty_generate(obj, opts = nil)
+ state = JSON.state.new(
+ :indent => ' ',
+ :space => ' ',
+ :object_nl => "\n",
+ :array_nl => "\n",
+ :check_circular => true
+ )
+ if opts
+ if opts.respond_to? :to_hash
+ opts = opts.to_hash
+ elsif opts.respond_to? :to_h
+ opts = opts.to_h
+ else
+ raise TypeError, "can't convert #{opts.class} into Hash"
+ end
+ state.configure(opts)
+ end
+ obj.to_json(state)
+ end
+
+ # :stopdoc:
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
+ alias pretty_unparse pretty_generate
+ module_function :pretty_unparse
+ # :startdoc:
+
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
+ # either be a string-like object, an IO like object, or an object responding
+ # to the read method. If _proc_ was given, it will be called with any nested
+ # Ruby object as an argument recursively in depth first order.
+ #
+ # This method is part of the implementation of the load/dump interface of
+ # Marshal and YAML.
+ def load(source, proc = nil)
+ if source.respond_to? :to_str
+ source = source.to_str
+ elsif source.respond_to? :to_io
+ source = source.to_io.read
+ else
+ source = source.read
+ end
+ result = parse(source, :max_nesting => false, :allow_nan => true)
+ recurse_proc(result, &proc) if proc
+ result
+ end
+
+ def recurse_proc(result, &proc)
+ case result
+ when Array
+ result.each { |x| recurse_proc x, &proc }
+ proc.call result
+ when Hash
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
+ proc.call result
+ else
+ proc.call result
+ end
+ end
+ private :recurse_proc
+ module_function :recurse_proc
+
+ alias restore load
+ module_function :restore
+
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
+ # the result.
+ #
+ # If anIO (an IO like object or an object that responds to the write method)
+ # was given, the resulting JSON is written to it.
+ #
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
+ # exception is raised. This argument is similar (but not exactly the
+ # same!) to the _limit_ argument in Marshal.dump.
+ #
+ # This method is part of the implementation of the load/dump interface of
+ # Marshal and YAML.
+ def dump(obj, anIO = nil, limit = nil)
+ if anIO and limit.nil?
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
+ unless anIO.respond_to?(:write)
+ limit = anIO
+ anIO = nil
+ end
+ end
+ limit ||= 0
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
+ if anIO
+ anIO.write result
+ anIO
+ else
+ result
+ end
+ rescue JSON::NestingError
+ raise ArgumentError, "exceed depth limit"
+ end
+end
+
+module ::Kernel
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
+ # one line.
+ def j(*objs)
+ objs.each do |obj|
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
+ end
+ nil
+ end
+
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
+ # indentation and over many lines.
+ def jj(*objs)
+ objs.each do |obj|
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
+ end
+ nil
+ end
+
+ # If _object_ is string-like parse the string and return the parsed result as
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
+ # structure object and return it.
+ #
+ # The _opts_ argument is passed through to generate/parse respectively, see
+ # generate and parse for their documentation.
+ def JSON(object, opts = {})
+ if object.respond_to? :to_str
+ JSON.parse(object.to_str, opts)
+ else
+ JSON.generate(object, opts)
+ end
+ end
+end
+
+class ::Class
+ # Returns true, if this class can be used to create an instance
+ # from a serialised JSON string. The class has to implement a class
+ # method _json_create_ that expects a hash as first parameter, which includes
+ # the required data.
+ def json_creatable?
+ respond_to?(:json_create)
+ end
+end
diff --git a/ruby/lib/json/editor.rb b/ruby/lib/json/editor.rb
new file mode 100644
index 0000000..1e13f33
--- /dev/null
+++ b/ruby/lib/json/editor.rb
@@ -0,0 +1,1371 @@
+# To use the GUI JSON editor, start the edit_json.rb executable script. It
+# requires ruby-gtk to be installed.
+
+require 'gtk2'
+require 'iconv'
+require 'json'
+require 'rbconfig'
+require 'open-uri'
+
+module JSON
+ module Editor
+ include Gtk
+
+ # Beginning of the editor window title
+ TITLE = 'JSON Editor'.freeze
+
+ # Columns constants
+ ICON_COL, TYPE_COL, CONTENT_COL = 0, 1, 2
+
+ # JSON primitive types (Containers)
+ CONTAINER_TYPES = %w[Array Hash].sort
+ # All JSON primitive types
+ ALL_TYPES = (%w[TrueClass FalseClass Numeric String NilClass] +
+ CONTAINER_TYPES).sort
+
+ # The Nodes necessary for the tree representation of a JSON document
+ ALL_NODES = (ALL_TYPES + %w[Key]).sort
+
+ DEFAULT_DIALOG_KEY_PRESS_HANDLER = lambda do |dialog, event|
+ case event.keyval
+ when Gdk::Keyval::GDK_Return
+ dialog.response Dialog::RESPONSE_ACCEPT
+ when Gdk::Keyval::GDK_Escape
+ dialog.response Dialog::RESPONSE_REJECT
+ end
+ end
+
+ # Returns the Gdk::Pixbuf of the icon named _name_ from the icon cache.
+ def Editor.fetch_icon(name)
+ @icon_cache ||= {}
+ unless @icon_cache.key?(name)
+ path = File.dirname(__FILE__)
+ @icon_cache[name] = Gdk::Pixbuf.new(File.join(path, name + '.xpm'))
+ end
+ @icon_cache[name]
+ end
+
+ # Opens an error dialog on top of _window_ showing the error message
+ # _text_.
+ def Editor.error_dialog(window, text)
+ dialog = MessageDialog.new(window, Dialog::MODAL,
+ MessageDialog::ERROR,
+ MessageDialog::BUTTONS_CLOSE, text)
+ dialog.show_all
+ dialog.run
+ rescue TypeError
+ dialog = MessageDialog.new(Editor.window, Dialog::MODAL,
+ MessageDialog::ERROR,
+ MessageDialog::BUTTONS_CLOSE, text)
+ dialog.show_all
+ dialog.run
+ ensure
+ dialog.destroy if dialog
+ end
+
+ # Opens a yes/no question dialog on top of _window_ showing the error
+ # message _text_. If yes was answered _true_ is returned, otherwise
+ # _false_.
+ def Editor.question_dialog(window, text)
+ dialog = MessageDialog.new(window, Dialog::MODAL,
+ MessageDialog::QUESTION,
+ MessageDialog::BUTTONS_YES_NO, text)
+ dialog.show_all
+ dialog.run do |response|
+ return Gtk::Dialog::RESPONSE_YES === response
+ end
+ ensure
+ dialog.destroy if dialog
+ end
+
+ # Convert the tree model starting from Gtk::TreeIter _iter_ into a Ruby
+ # data structure and return it.
+ def Editor.model2data(iter)
+ return nil if iter.nil?
+ case iter.type
+ when 'Hash'
+ hash = {}
+ iter.each { |c| hash[c.content] = Editor.model2data(c.first_child) }
+ hash
+ when 'Array'
+ array = Array.new(iter.n_children)
+ iter.each_with_index { |c, i| array[i] = Editor.model2data(c) }
+ array
+ when 'Key'
+ iter.content
+ when 'String'
+ iter.content
+ when 'Numeric'
+ content = iter.content
+ if /\./.match(content)
+ content.to_f
+ else
+ content.to_i
+ end
+ when 'TrueClass'
+ true
+ when 'FalseClass'
+ false
+ when 'NilClass'
+ nil
+ else
+ fail "Unknown type found in model: #{iter.type}"
+ end
+ end
+
+ # Convert the Ruby data structure _data_ into tree model data for Gtk and
+ # returns the whole model. If the parameter _model_ wasn't given a new
+ # Gtk::TreeStore is created as the model. The _parent_ parameter specifies
+ # the parent node (iter, Gtk:TreeIter instance) to which the data is
+ # appended, alternativeley the result of the yielded block is used as iter.
+ def Editor.data2model(data, model = nil, parent = nil)
+ model ||= TreeStore.new(Gdk::Pixbuf, String, String)
+ iter = if block_given?
+ yield model
+ else
+ model.append(parent)
+ end
+ case data
+ when Hash
+ iter.type = 'Hash'
+ data.sort.each do |key, value|
+ pair_iter = model.append(iter)
+ pair_iter.type = 'Key'
+ pair_iter.content = key.to_s
+ Editor.data2model(value, model, pair_iter)
+ end
+ when Array
+ iter.type = 'Array'
+ data.each do |value|
+ Editor.data2model(value, model, iter)
+ end
+ when Numeric
+ iter.type = 'Numeric'
+ iter.content = data.to_s
+ when String, true, false, nil
+ iter.type = data.class.name
+ iter.content = data.nil? ? 'null' : data.to_s
+ else
+ iter.type = 'String'
+ iter.content = data.to_s
+ end
+ model
+ end
+
+ # The Gtk::TreeIter class is reopened and some auxiliary methods are added.
+ class Gtk::TreeIter
+ include Enumerable
+
+ # Traverse each of this Gtk::TreeIter instance's children
+ # and yield to them.
+ def each
+ n_children.times { |i| yield nth_child(i) }
+ end
+
+ # Recursively traverse all nodes of this Gtk::TreeIter's subtree
+ # (including self) and yield to them.
+ def recursive_each(&block)
+ yield self
+ each do |i|
+ i.recursive_each(&block)
+ end
+ end
+
+ # Remove the subtree of this Gtk::TreeIter instance from the
+ # model _model_.
+ def remove_subtree(model)
+ while current = first_child
+ model.remove(current)
+ end
+ end
+
+ # Returns the type of this node.
+ def type
+ self[TYPE_COL]
+ end
+
+ # Sets the type of this node to _value_. This implies setting
+ # the respective icon accordingly.
+ def type=(value)
+ self[TYPE_COL] = value
+ self[ICON_COL] = Editor.fetch_icon(value)
+ end
+
+ # Returns the content of this node.
+ def content
+ self[CONTENT_COL]
+ end
+
+ # Sets the content of this node to _value_.
+ def content=(value)
+ self[CONTENT_COL] = value
+ end
+ end
+
+ # This module bundles some method, that can be used to create a menu. It
+ # should be included into the class in question.
+ module MenuExtension
+ include Gtk
+
+ # Creates a Menu, that includes MenuExtension. _treeview_ is the
+ # Gtk::TreeView, on which it operates.
+ def initialize(treeview)
+ @treeview = treeview
+ @menu = Menu.new
+ end
+
+ # Returns the Gtk::TreeView of this menu.
+ attr_reader :treeview
+
+ # Returns the menu.
+ attr_reader :menu
+
+ # Adds a Gtk::SeparatorMenuItem to this instance's #menu.
+ def add_separator
+ menu.append SeparatorMenuItem.new
+ end
+
+ # Adds a Gtk::MenuItem to this instance's #menu. _label_ is the label
+ # string, _klass_ is the item type, and _callback_ is the procedure, that
+ # is called if the _item_ is activated.
+ def add_item(label, keyval = nil, klass = MenuItem, &callback)
+ label = "#{label} (C-#{keyval.chr})" if keyval
+ item = klass.new(label)
+ item.signal_connect(:activate, &callback)
+ if keyval
+ self.signal_connect(:'key-press-event') do |item, event|
+ if event.state & Gdk::Window::ModifierType::CONTROL_MASK != 0 and
+ event.keyval == keyval
+ callback.call item
+ end
+ end
+ end
+ menu.append item
+ item
+ end
+
+ # This method should be implemented in subclasses to create the #menu of
+ # this instance. It has to be called after an instance of this class is
+ # created, to build the menu.
+ def create
+ raise NotImplementedError
+ end
+
+ def method_missing(*a, &b)
+ treeview.__send__(*a, &b)
+ end
+ end
+
+ # This class creates the popup menu, that opens when clicking onto the
+ # treeview.
+ class PopUpMenu
+ include MenuExtension
+
+ # Change the type or content of the selected node.
+ def change_node(item)
+ if current = selection.selected
+ parent = current.parent
+ old_type, old_content = current.type, current.content
+ if ALL_TYPES.include?(old_type)
+ @clipboard_data = Editor.model2data(current)
+ type, content = ask_for_element(parent, current.type,
+ current.content)
+ if type
+ current.type, current.content = type, content
+ current.remove_subtree(model)
+ toplevel.display_status("Changed a node in tree.")
+ window.change
+ end
+ else
+ toplevel.display_status(
+ "Cannot change node of type #{old_type} in tree!")
+ end
+ end
+ end
+
+ # Cut the selected node and its subtree, and save it into the
+ # clipboard.
+ def cut_node(item)
+ if current = selection.selected
+ if current and current.type == 'Key'
+ @clipboard_data = {
+ current.content => Editor.model2data(current.first_child)
+ }
+ else
+ @clipboard_data = Editor.model2data(current)
+ end
+ model.remove(current)
+ window.change
+ toplevel.display_status("Cut a node from tree.")
+ end
+ end
+
+ # Copy the selected node and its subtree, and save it into the
+ # clipboard.
+ def copy_node(item)
+ if current = selection.selected
+ if current and current.type == 'Key'
+ @clipboard_data = {
+ current.content => Editor.model2data(current.first_child)
+ }
+ else
+ @clipboard_data = Editor.model2data(current)
+ end
+ window.change
+ toplevel.display_status("Copied a node from tree.")
+ end
+ end
+
+ # Paste the data in the clipboard into the selected Array or Hash by
+ # appending it.
+ def paste_node_appending(item)
+ if current = selection.selected
+ if @clipboard_data
+ case current.type
+ when 'Array'
+ Editor.data2model(@clipboard_data, model, current)
+ expand_collapse(current)
+ when 'Hash'
+ if @clipboard_data.is_a? Hash
+ parent = current.parent
+ hash = Editor.model2data(current)
+ model.remove(current)
+ hash.update(@clipboard_data)
+ Editor.data2model(hash, model, parent)
+ if parent
+ expand_collapse(parent)
+ elsif @expanded
+ expand_all
+ end
+ window.change
+ else
+ toplevel.display_status(
+ "Cannot paste non-#{current.type} data into '#{current.type}'!")
+ end
+ else
+ toplevel.display_status(
+ "Cannot paste node below '#{current.type}'!")
+ end
+ else
+ toplevel.display_status("Nothing to paste in clipboard!")
+ end
+ else
+ toplevel.display_status("Append a node into the root first!")
+ end
+ end
+
+ # Paste the data in the clipboard into the selected Array inserting it
+ # before the selected element.
+ def paste_node_inserting_before(item)
+ if current = selection.selected
+ if @clipboard_data
+ parent = current.parent or return
+ parent_type = parent.type
+ if parent_type == 'Array'
+ selected_index = parent.each_with_index do |c, i|
+ break i if c == current
+ end
+ Editor.data2model(@clipboard_data, model, parent) do |m|
+ m.insert_before(parent, current)
+ end
+ expand_collapse(current)
+ toplevel.display_status("Inserted an element to " +
+ "'#{parent_type}' before index #{selected_index}.")
+ window.change
+ else
+ toplevel.display_status(
+ "Cannot insert node below '#{parent_type}'!")
+ end
+ else
+ toplevel.display_status("Nothing to paste in clipboard!")
+ end
+ else
+ toplevel.display_status("Append a node into the root first!")
+ end
+ end
+
+ # Append a new node to the selected Hash or Array.
+ def append_new_node(item)
+ if parent = selection.selected
+ parent_type = parent.type
+ case parent_type
+ when 'Hash'
+ key, type, content = ask_for_hash_pair(parent)
+ key or return
+ iter = create_node(parent, 'Key', key)
+ iter = create_node(iter, type, content)
+ toplevel.display_status(
+ "Added a (key, value)-pair to '#{parent_type}'.")
+ window.change
+ when 'Array'
+ type, content = ask_for_element(parent)
+ type or return
+ iter = create_node(parent, type, content)
+ window.change
+ toplevel.display_status("Appendend an element to '#{parent_type}'.")
+ else
+ toplevel.display_status("Cannot append to '#{parent_type}'!")
+ end
+ else
+ type, content = ask_for_element
+ type or return
+ iter = create_node(nil, type, content)
+ window.change
+ end
+ end
+
+ # Insert a new node into an Array before the selected element.
+ def insert_new_node(item)
+ if current = selection.selected
+ parent = current.parent or return
+ parent_parent = parent.parent
+ parent_type = parent.type
+ if parent_type == 'Array'
+ selected_index = parent.each_with_index do |c, i|
+ break i if c == current
+ end
+ type, content = ask_for_element(parent)
+ type or return
+ iter = model.insert_before(parent, current)
+ iter.type, iter.content = type, content
+ toplevel.display_status("Inserted an element to " +
+ "'#{parent_type}' before index #{selected_index}.")
+ window.change
+ else
+ toplevel.display_status(
+ "Cannot insert node below '#{parent_type}'!")
+ end
+ else
+ toplevel.display_status("Append a node into the root first!")
+ end
+ end
+
+ # Recursively collapse/expand a subtree starting from the selected node.
+ def collapse_expand(item)
+ if current = selection.selected
+ if row_expanded?(current.path)
+ collapse_row(current.path)
+ else
+ expand_row(current.path, true)
+ end
+ else
+ toplevel.display_status("Append a node into the root first!")
+ end
+ end
+
+ # Create the menu.
+ def create
+ add_item("Change node", ?n, &method(:change_node))
+ add_separator
+ add_item("Cut node", ?X, &method(:cut_node))
+ add_item("Copy node", ?C, &method(:copy_node))
+ add_item("Paste node (appending)", ?A, &method(:paste_node_appending))
+ add_item("Paste node (inserting before)", ?I,
+ &method(:paste_node_inserting_before))
+ add_separator
+ add_item("Append new node", ?a, &method(:append_new_node))
+ add_item("Insert new node before", ?i, &method(:insert_new_node))
+ add_separator
+ add_item("Collapse/Expand node (recursively)", ?e,
+ &method(:collapse_expand))
+
+ menu.show_all
+ signal_connect(:button_press_event) do |widget, event|
+ if event.kind_of? Gdk::EventButton and event.button == 3
+ menu.popup(nil, nil, event.button, event.time)
+ end
+ end
+ signal_connect(:popup_menu) do
+ menu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME)
+ end
+ end
+ end
+
+ # This class creates the File pulldown menu.
+ class FileMenu
+ include MenuExtension
+
+ # Clear the model and filename, but ask to save the JSON document, if
+ # unsaved changes have occured.
+ def new(item)
+ window.clear
+ end
+
+ # Open a file and load it into the editor. Ask to save the JSON document
+ # first, if unsaved changes have occured.
+ def open(item)
+ window.file_open
+ end
+
+ def open_location(item)
+ window.location_open
+ end
+
+ # Revert the current JSON document in the editor to the saved version.
+ def revert(item)
+ window.instance_eval do
+ @filename and file_open(@filename)
+ end
+ end
+
+ # Save the current JSON document.
+ def save(item)
+ window.file_save
+ end
+
+ # Save the current JSON document under the given filename.
+ def save_as(item)
+ window.file_save_as
+ end
+
+ # Quit the editor, after asking to save any unsaved changes first.
+ def quit(item)
+ window.quit
+ end
+
+ # Create the menu.
+ def create
+ title = MenuItem.new('File')
+ title.submenu = menu
+ add_item('New', &method(:new))
+ add_item('Open', ?o, &method(:open))
+ add_item('Open location', ?l, &method(:open_location))
+ add_item('Revert', &method(:revert))
+ add_separator
+ add_item('Save', ?s, &method(:save))
+ add_item('Save As', ?S, &method(:save_as))
+ add_separator
+ add_item('Quit', ?q, &method(:quit))
+ title
+ end
+ end
+
+ # This class creates the Edit pulldown menu.
+ class EditMenu
+ include MenuExtension
+
+ # Copy data from model into primary clipboard.
+ def copy(item)
+ data = Editor.model2data(model.iter_first)
+ json = JSON.pretty_generate(data, :max_nesting => false)
+ c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY)
+ c.text = json
+ end
+
+ # Copy json text from primary clipboard into model.
+ def paste(item)
+ c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY)
+ if json = c.wait_for_text
+ window.ask_save if @changed
+ begin
+ window.edit json
+ rescue JSON::ParserError
+ window.clear
+ end
+ end
+ end
+
+ # Find a string in all nodes' contents and select the found node in the
+ # treeview.
+ def find(item)
+ @search = ask_for_find_term(@search) or return
+ iter = model.get_iter('0') or return
+ iter.recursive_each do |i|
+ if @iter
+ if @iter != i
+ next
+ else
+ @iter = nil
+ next
+ end
+ elsif @search.match(i[CONTENT_COL])
+ set_cursor(i.path, nil, false)
+ @iter = i
+ break
+ end
+ end
+ end
+
+ # Repeat the last search given by #find.
+ def find_again(item)
+ @search or return
+ iter = model.get_iter('0')
+ iter.recursive_each do |i|
+ if @iter
+ if @iter != i
+ next
+ else
+ @iter = nil
+ next
+ end
+ elsif @search.match(i[CONTENT_COL])
+ set_cursor(i.path, nil, false)
+ @iter = i
+ break
+ end
+ end
+ end
+
+ # Sort (Reverse sort) all elements of the selected array by the given
+ # expression. _x_ is the element in question.
+ def sort(item)
+ if current = selection.selected
+ if current.type == 'Array'
+ parent = current.parent
+ ary = Editor.model2data(current)
+ order, reverse = ask_for_order
+ order or return
+ begin
+ block = eval "lambda { |x| #{order} }"
+ if reverse
+ ary.sort! { |a,b| block[b] <=> block[a] }
+ else
+ ary.sort! { |a,b| block[a] <=> block[b] }
+ end
+ rescue => e
+ Editor.error_dialog(self, "Failed to sort Array with #{order}: #{e}!")
+ else
+ Editor.data2model(ary, model, parent) do |m|
+ m.insert_before(parent, current)
+ end
+ model.remove(current)
+ expand_collapse(parent)
+ window.change
+ toplevel.display_status("Array has been sorted.")
+ end
+ else
+ toplevel.display_status("Only Array nodes can be sorted!")
+ end
+ else
+ toplevel.display_status("Select an Array to sort first!")
+ end
+ end
+
+ # Create the menu.
+ def create
+ title = MenuItem.new('Edit')
+ title.submenu = menu
+ add_item('Copy', ?c, &method(:copy))
+ add_item('Paste', ?v, &method(:paste))
+ add_separator
+ add_item('Find', ?f, &method(:find))
+ add_item('Find Again', ?g, &method(:find_again))
+ add_separator
+ add_item('Sort', ?S, &method(:sort))
+ title
+ end
+ end
+
+ class OptionsMenu
+ include MenuExtension
+
+ # Collapse/Expand all nodes by default.
+ def collapsed_nodes(item)
+ if expanded
+ self.expanded = false
+ collapse_all
+ else
+ self.expanded = true
+ expand_all
+ end
+ end
+
+ # Toggle pretty saving mode on/off.
+ def pretty_saving(item)
+ @pretty_item.toggled
+ window.change
+ end
+
+ attr_reader :pretty_item
+
+ # Create the menu.
+ def create
+ title = MenuItem.new('Options')
+ title.submenu = menu
+ add_item('Collapsed nodes', nil, CheckMenuItem, &method(:collapsed_nodes))
+ @pretty_item = add_item('Pretty saving', nil, CheckMenuItem,
+ &method(:pretty_saving))
+ @pretty_item.active = true
+ window.unchange
+ title
+ end
+ end
+
+ # This class inherits from Gtk::TreeView, to configure it and to add a lot
+ # of behaviour to it.
+ class JSONTreeView < Gtk::TreeView
+ include Gtk
+
+ # Creates a JSONTreeView instance, the parameter _window_ is
+ # a MainWindow instance and used for self delegation.
+ def initialize(window)
+ @window = window
+ super(TreeStore.new(Gdk::Pixbuf, String, String))
+ self.selection.mode = SELECTION_BROWSE
+
+ @expanded = false
+ self.headers_visible = false
+ add_columns
+ add_popup_menu
+ end
+
+ # Returns the MainWindow instance of this JSONTreeView.
+ attr_reader :window
+
+ # Returns true, if nodes are autoexpanding, false otherwise.
+ attr_accessor :expanded
+
+ private
+
+ def add_columns
+ cell = CellRendererPixbuf.new
+ column = TreeViewColumn.new('Icon', cell,
+ 'pixbuf' => ICON_COL
+ )
+ append_column(column)
+
+ cell = CellRendererText.new
+ column = TreeViewColumn.new('Type', cell,
+ 'text' => TYPE_COL
+ )
+ append_column(column)
+
+ cell = CellRendererText.new
+ cell.editable = true
+ column = TreeViewColumn.new('Content', cell,
+ 'text' => CONTENT_COL
+ )
+ cell.signal_connect(:edited, &method(:cell_edited))
+ append_column(column)
+ end
+
+ def unify_key(iter, key)
+ return unless iter.type == 'Key'
+ parent = iter.parent
+ if parent.any? { |c| c != iter and c.content == key }
+ old_key = key
+ i = 0
+ begin
+ key = sprintf("%s.%d", old_key, i += 1)
+ end while parent.any? { |c| c != iter and c.content == key }
+ end
+ iter.content = key
+ end
+
+ def cell_edited(cell, path, value)
+ iter = model.get_iter(path)
+ case iter.type
+ when 'Key'
+ unify_key(iter, value)
+ toplevel.display_status('Key has been changed.')
+ when 'FalseClass'
+ value.downcase!
+ if value == 'true'
+ iter.type, iter.content = 'TrueClass', 'true'
+ end
+ when 'TrueClass'
+ value.downcase!
+ if value == 'false'
+ iter.type, iter.content = 'FalseClass', 'false'
+ end
+ when 'Numeric'
+ iter.content =
+ if value == 'Infinity'
+ value
+ else
+ (Integer(value) rescue Float(value) rescue 0).to_s
+ end
+ when 'String'
+ iter.content = value
+ when 'Hash', 'Array'
+ return
+ else
+ fail "Unknown type found in model: #{iter.type}"
+ end
+ window.change
+ end
+
+ def configure_value(value, type)
+ value.editable = false
+ case type
+ when 'Array', 'Hash'
+ value.text = ''
+ when 'TrueClass'
+ value.text = 'true'
+ when 'FalseClass'
+ value.text = 'false'
+ when 'NilClass'
+ value.text = 'null'
+ when 'Numeric', 'String'
+ value.text ||= ''
+ value.editable = true
+ else
+ raise ArgumentError, "unknown type '#{type}' encountered"
+ end
+ end
+
+ def add_popup_menu
+ menu = PopUpMenu.new(self)
+ menu.create
+ end
+
+ public
+
+ # Create a _type_ node with content _content_, and add it to _parent_
+ # in the model. If _parent_ is nil, create a new model and put it into
+ # the editor treeview.
+ def create_node(parent, type, content)
+ iter = if parent
+ model.append(parent)
+ else
+ new_model = Editor.data2model(nil)
+ toplevel.view_new_model(new_model)
+ new_model.iter_first
+ end
+ iter.type, iter.content = type, content
+ expand_collapse(parent) if parent
+ iter
+ end
+
+ # Ask for a hash key, value pair to be added to the Hash node _parent_.
+ def ask_for_hash_pair(parent)
+ key_input = type_input = value_input = nil
+
+ dialog = Dialog.new("New (key, value) pair for Hash", nil, nil,
+ [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
+ [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
+ )
+ dialog.width_request = 640
+
+ hbox = HBox.new(false, 5)
+ hbox.pack_start(Label.new("Key:"), false)
+ hbox.pack_start(key_input = Entry.new)
+ key_input.text = @key || ''
+ dialog.vbox.pack_start(hbox, false)
+ key_input.signal_connect(:activate) do
+ if parent.any? { |c| c.content == key_input.text }
+ toplevel.display_status('Key already exists in Hash!')
+ key_input.text = ''
+ else
+ toplevel.display_status('Key has been changed.')
+ end
+ end
+
+ hbox = HBox.new(false, 5)
+ hbox.pack_start(Label.new("Type:"), false)
+ hbox.pack_start(type_input = ComboBox.new(true))
+ ALL_TYPES.each { |t| type_input.append_text(t) }
+ type_input.active = @type || 0
+ dialog.vbox.pack_start(hbox, false)
+
+ type_input.signal_connect(:changed) do
+ value_input.editable = false
+ case ALL_TYPES[type_input.active]
+ when 'Array', 'Hash'
+ value_input.text = ''
+ when 'TrueClass'
+ value_input.text = 'true'
+ when 'FalseClass'
+ value_input.text = 'false'
+ when 'NilClass'
+ value_input.text = 'null'
+ else
+ value_input.text = ''
+ value_input.editable = true
+ end
+ end
+
+ hbox = HBox.new(false, 5)
+ hbox.pack_start(Label.new("Value:"), false)
+ hbox.pack_start(value_input = Entry.new)
+ value_input.width_chars = 60
+ value_input.text = @value || ''
+ dialog.vbox.pack_start(hbox, false)
+
+ dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
+ dialog.show_all
+ self.focus = dialog
+ dialog.run do |response|
+ if response == Dialog::RESPONSE_ACCEPT
+ @key = key_input.text
+ type = ALL_TYPES[@type = type_input.active]
+ content = value_input.text
+ return @key, type, content
+ end
+ end
+ return
+ ensure
+ dialog.destroy
+ end
+
+ # Ask for an element to be appended _parent_.
+ def ask_for_element(parent = nil, default_type = nil, value_text = @content)
+ type_input = value_input = nil
+
+ dialog = Dialog.new(
+ "New element into #{parent ? parent.type : 'root'}",
+ nil, nil,
+ [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
+ [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
+ )
+ hbox = HBox.new(false, 5)
+ hbox.pack_start(Label.new("Type:"), false)
+ hbox.pack_start(type_input = ComboBox.new(true))
+ default_active = 0
+ types = parent ? ALL_TYPES : CONTAINER_TYPES
+ types.each_with_index do |t, i|
+ type_input.append_text(t)
+ if t == default_type
+ default_active = i
+ end
+ end
+ type_input.active = default_active
+ dialog.vbox.pack_start(hbox, false)
+ type_input.signal_connect(:changed) do
+ configure_value(value_input, types[type_input.active])
+ end
+
+ hbox = HBox.new(false, 5)
+ hbox.pack_start(Label.new("Value:"), false)
+ hbox.pack_start(value_input = Entry.new)
+ value_input.width_chars = 60
+ value_input.text = value_text if value_text
+ configure_value(value_input, types[type_input.active])
+
+ dialog.vbox.pack_start(hbox, false)
+
+ dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
+ dialog.show_all
+ self.focus = dialog
+ dialog.run do |response|
+ if response == Dialog::RESPONSE_ACCEPT
+ type = types[type_input.active]
+ @content = case type
+ when 'Numeric'
+ if (t = value_input.text) == 'Infinity'
+ 1 / 0.0
+ else
+ Integer(t) rescue Float(t) rescue 0
+ end
+ else
+ value_input.text
+ end.to_s
+ return type, @content
+ end
+ end
+ return
+ ensure
+ dialog.destroy if dialog
+ end
+
+ # Ask for an order criteria for sorting, using _x_ for the element in
+ # question. Returns the order criterium, and true/false for reverse
+ # sorting.
+ def ask_for_order
+ dialog = Dialog.new(
+ "Give an order criterium for 'x'.",
+ nil, nil,
+ [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
+ [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
+ )
+ hbox = HBox.new(false, 5)
+
+ hbox.pack_start(Label.new("Order:"), false)
+ hbox.pack_start(order_input = Entry.new)
+ order_input.text = @order || 'x'
+ order_input.width_chars = 60
+
+ hbox.pack_start(reverse_checkbox = CheckButton.new('Reverse'), false)
+
+ dialog.vbox.pack_start(hbox, false)
+
+ dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
+ dialog.show_all
+ self.focus = dialog
+ dialog.run do |response|
+ if response == Dialog::RESPONSE_ACCEPT
+ return @order = order_input.text, reverse_checkbox.active?
+ end
+ end
+ return
+ ensure
+ dialog.destroy if dialog
+ end
+
+ # Ask for a find term to search for in the tree. Returns the term as a
+ # string.
+ def ask_for_find_term(search = nil)
+ dialog = Dialog.new(
+ "Find a node matching regex in tree.",
+ nil, nil,
+ [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
+ [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
+ )
+ hbox = HBox.new(false, 5)
+
+ hbox.pack_start(Label.new("Regex:"), false)
+ hbox.pack_start(regex_input = Entry.new)
+ hbox.pack_start(icase_checkbox = CheckButton.new('Icase'), false)
+ regex_input.width_chars = 60
+ if search
+ regex_input.text = search.source
+ icase_checkbox.active = search.casefold?
+ end
+
+ dialog.vbox.pack_start(hbox, false)
+
+ dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
+ dialog.show_all
+ self.focus = dialog
+ dialog.run do |response|
+ if response == Dialog::RESPONSE_ACCEPT
+ begin
+ return Regexp.new(regex_input.text, icase_checkbox.active? ? Regexp::IGNORECASE : 0)
+ rescue => e
+ Editor.error_dialog(self, "Evaluation of regex /#{regex_input.text}/ failed: #{e}!")
+ return
+ end
+ end
+ end
+ return
+ ensure
+ dialog.destroy if dialog
+ end
+
+ # Expand or collapse row pointed to by _iter_ according
+ # to the #expanded attribute.
+ def expand_collapse(iter)
+ if expanded
+ expand_row(iter.path, true)
+ else
+ collapse_row(iter.path)
+ end
+ end
+ end
+
+ # The editor main window
+ class MainWindow < Gtk::Window
+ include Gtk
+
+ def initialize(encoding)
+ @changed = false
+ @encoding = encoding
+ super(TOPLEVEL)
+ display_title
+ set_default_size(800, 600)
+ signal_connect(:delete_event) { quit }
+
+ vbox = VBox.new(false, 0)
+ add(vbox)
+ #vbox.border_width = 0
+
+ @treeview = JSONTreeView.new(self)
+ @treeview.signal_connect(:'cursor-changed') do
+ display_status('')
+ end
+
+ menu_bar = create_menu_bar
+ vbox.pack_start(menu_bar, false, false, 0)
+
+ sw = ScrolledWindow.new(nil, nil)
+ sw.shadow_type = SHADOW_ETCHED_IN
+ sw.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
+ vbox.pack_start(sw, true, true, 0)
+ sw.add(@treeview)
+
+ @status_bar = Statusbar.new
+ vbox.pack_start(@status_bar, false, false, 0)
+
+ @filename ||= nil
+ if @filename
+ data = read_data(@filename)
+ view_new_model Editor.data2model(data)
+ end
+
+ signal_connect(:button_release_event) do |_,event|
+ if event.button == 2
+ c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY)
+ if url = c.wait_for_text
+ location_open url
+ end
+ false
+ else
+ true
+ end
+ end
+ end
+
+ # Creates the menu bar with the pulldown menus and returns it.
+ def create_menu_bar
+ menu_bar = MenuBar.new
+ @file_menu = FileMenu.new(@treeview)
+ menu_bar.append @file_menu.create
+ @edit_menu = EditMenu.new(@treeview)
+ menu_bar.append @edit_menu.create
+ @options_menu = OptionsMenu.new(@treeview)
+ menu_bar.append @options_menu.create
+ menu_bar
+ end
+
+ # Sets editor status to changed, to indicate that the edited data
+ # containts unsaved changes.
+ def change
+ @changed = true
+ display_title
+ end
+
+ # Sets editor status to unchanged, to indicate that the edited data
+ # doesn't containt unsaved changes.
+ def unchange
+ @changed = false
+ display_title
+ end
+
+ # Puts a new model _model_ into the Gtk::TreeView to be edited.
+ def view_new_model(model)
+ @treeview.model = model
+ @treeview.expanded = true
+ @treeview.expand_all
+ unchange
+ end
+
+ # Displays _text_ in the status bar.
+ def display_status(text)
+ @cid ||= nil
+ @status_bar.pop(@cid) if @cid
+ @cid = @status_bar.get_context_id('dummy')
+ @status_bar.push(@cid, text)
+ end
+
+ # Opens a dialog, asking, if changes should be saved to a file.
+ def ask_save
+ if Editor.question_dialog(self,
+ "Unsaved changes to JSON model. Save?")
+ if @filename
+ file_save
+ else
+ file_save_as
+ end
+ end
+ end
+
+ # Quit this editor, that is, leave this editor's main loop.
+ def quit
+ ask_save if @changed
+ if Gtk.main_level > 0
+ destroy
+ Gtk.main_quit
+ end
+ nil
+ end
+
+ # Display the new title according to the editor's current state.
+ def display_title
+ title = TITLE.dup
+ title << ": #@filename" if @filename
+ title << " *" if @changed
+ self.title = title
+ end
+
+ # Clear the current model, after asking to save all unsaved changes.
+ def clear
+ ask_save if @changed
+ @filename = nil
+ self.view_new_model nil
+ end
+
+ def check_pretty_printed(json)
+ pretty = !!((nl_index = json.index("\n")) && nl_index != json.size - 1)
+ @options_menu.pretty_item.active = pretty
+ end
+ private :check_pretty_printed
+
+ # Open the data at the location _uri_, if given. Otherwise open a dialog
+ # to ask for the _uri_.
+ def location_open(uri = nil)
+ uri = ask_for_location unless uri
+ uri or return
+ ask_save if @changed
+ data = load_location(uri) or return
+ view_new_model Editor.data2model(data)
+ end
+
+ # Open the file _filename_ or call the #select_file method to ask for a
+ # filename.
+ def file_open(filename = nil)
+ filename = select_file('Open as a JSON file') unless filename
+ data = load_file(filename) or return
+ view_new_model Editor.data2model(data)
+ end
+
+ # Edit the string _json_ in the editor.
+ def edit(json)
+ if json.respond_to? :read
+ json = json.read
+ end
+ data = parse_json json
+ view_new_model Editor.data2model(data)
+ end
+
+ # Save the current file.
+ def file_save
+ if @filename
+ store_file(@filename)
+ else
+ file_save_as
+ end
+ end
+
+ # Save the current file as the filename
+ def file_save_as
+ filename = select_file('Save as a JSON file')
+ store_file(filename)
+ end
+
+ # Store the current JSON document to _path_.
+ def store_file(path)
+ if path
+ data = Editor.model2data(@treeview.model.iter_first)
+ File.open(path + '.tmp', 'wb') do |output|
+ data or break
+ if @options_menu.pretty_item.active?
+ output.puts JSON.pretty_generate(data, :max_nesting => false)
+ else
+ output.write JSON.generate(data, :max_nesting => false)
+ end
+ end
+ File.rename path + '.tmp', path
+ @filename = path
+ toplevel.display_status("Saved data to '#@filename'.")
+ unchange
+ end
+ rescue SystemCallError => e
+ Editor.error_dialog(self, "Failed to store JSON file: #{e}!")
+ end
+
+ # Load the file named _filename_ into the editor as a JSON document.
+ def load_file(filename)
+ if filename
+ if File.directory?(filename)
+ Editor.error_dialog(self, "Try to select a JSON file!")
+ nil
+ else
+ @filename = filename
+ if data = read_data(filename)
+ toplevel.display_status("Loaded data from '#@filename'.")
+ end
+ display_title
+ data
+ end
+ end
+ end
+
+ # Load the data at location _uri_ into the editor as a JSON document.
+ def load_location(uri)
+ data = read_data(uri) or return
+ @filename = nil
+ toplevel.display_status("Loaded data from '#{uri}'.")
+ display_title
+ data
+ end
+
+ def parse_json(json)
+ check_pretty_printed(json)
+ if @encoding && !/^utf8$/i.match(@encoding)
+ iconverter = Iconv.new('utf8', @encoding)
+ json = iconverter.iconv(json)
+ end
+ JSON::parse(json, :max_nesting => false, :create_additions => false)
+ end
+ private :parse_json
+
+ # Read a JSON document from the file named _filename_, parse it into a
+ # ruby data structure, and return the data.
+ def read_data(filename)
+ open(filename) do |f|
+ json = f.read
+ return parse_json(json)
+ end
+ rescue => e
+ Editor.error_dialog(self, "Failed to parse JSON file: #{e}!")
+ return
+ end
+
+ # Open a file selecton dialog, displaying _message_, and return the
+ # selected filename or nil, if no file was selected.
+ def select_file(message)
+ filename = nil
+ fs = FileSelection.new(message)
+ fs.set_modal(true)
+ @default_dir = File.join(Dir.pwd, '') unless @default_dir
+ fs.set_filename(@default_dir)
+ fs.set_transient_for(self)
+ fs.signal_connect(:destroy) { Gtk.main_quit }
+ fs.ok_button.signal_connect(:clicked) do
+ filename = fs.filename
+ @default_dir = File.join(File.dirname(filename), '')
+ fs.destroy
+ Gtk.main_quit
+ end
+ fs.cancel_button.signal_connect(:clicked) do
+ fs.destroy
+ Gtk.main_quit
+ end
+ fs.show_all
+ Gtk.main
+ filename
+ end
+
+ # Ask for location URI a to load data from. Returns the URI as a string.
+ def ask_for_location
+ dialog = Dialog.new(
+ "Load data from location...",
+ nil, nil,
+ [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
+ [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
+ )
+ hbox = HBox.new(false, 5)
+
+ hbox.pack_start(Label.new("Location:"), false)
+ hbox.pack_start(location_input = Entry.new)
+ location_input.width_chars = 60
+ location_input.text = @location || ''
+
+ dialog.vbox.pack_start(hbox, false)
+
+ dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
+ dialog.show_all
+ dialog.run do |response|
+ if response == Dialog::RESPONSE_ACCEPT
+ return @location = location_input.text
+ end
+ end
+ return
+ ensure
+ dialog.destroy if dialog
+ end
+ end
+
+ class << self
+ # Starts a JSON Editor. If a block was given, it yields
+ # to the JSON::Editor::MainWindow instance.
+ def start(encoding = 'utf8') # :yield: window
+ Gtk.init
+ @window = Editor::MainWindow.new(encoding)
+ @window.icon_list = [ Editor.fetch_icon('json') ]
+ yield @window if block_given?
+ @window.show_all
+ Gtk.main
+ end
+
+ # Edit the string _json_ with encoding _encoding_ in the editor.
+ def edit(json, encoding = 'utf8')
+ start(encoding) do |window|
+ window.edit json
+ end
+ end
+
+ attr_reader :window
+ end
+ end
+end
diff --git a/ruby/lib/json/ext.rb b/ruby/lib/json/ext.rb
new file mode 100644
index 0000000..719e560
--- /dev/null
+++ b/ruby/lib/json/ext.rb
@@ -0,0 +1,15 @@
+require 'json/common'
+
+module JSON
+ # This module holds all the modules/classes that implement JSON's
+ # functionality as C extensions.
+ module Ext
+ require 'json/ext/parser'
+ require 'json/ext/generator'
+ $DEBUG and warn "Using c extension for JSON."
+ JSON.parser = Parser
+ JSON.generator = Generator
+ end
+
+ JSON_LOADED = true
+end
diff --git a/ruby/lib/json/version.rb b/ruby/lib/json/version.rb
new file mode 100644
index 0000000..e2764b0
--- /dev/null
+++ b/ruby/lib/json/version.rb
@@ -0,0 +1,9 @@
+module JSON
+ # JSON version
+ VERSION = '1.1.4'
+ VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
+ VARIANT_BINARY = false
+end
diff --git a/ruby/lib/kconv.rb b/ruby/lib/kconv.rb
new file mode 100644
index 0000000..f2c9489
--- /dev/null
+++ b/ruby/lib/kconv.rb
@@ -0,0 +1,282 @@
+#
+# kconv.rb - Kanji Converter.
+#
+# $Id: kconv.rb 15050 2008-01-14 14:37:29Z naruse $
+#
+# ----
+#
+# kconv.rb implements the Kconv class for Kanji Converter. Additionally,
+# some methods in String classes are added to allow easy conversion.
+#
+
+require 'nkf'
+
+#
+# Kanji Converter for Ruby.
+#
+module Kconv
+ #
+ # Public Constants
+ #
+
+ #Constant of Encoding
+
+ # Auto-Detect
+ AUTO = NKF::AUTO
+ # ISO-2022-JP
+ JIS = NKF::JIS
+ # EUC-JP
+ EUC = NKF::EUC
+ # Shift_JIS
+ SJIS = NKF::SJIS
+ # BINARY
+ BINARY = NKF::BINARY
+ # NOCONV
+ NOCONV = NKF::NOCONV
+ # ASCII
+ ASCII = NKF::ASCII
+ # UTF-8
+ UTF8 = NKF::UTF8
+ # UTF-16
+ UTF16 = NKF::UTF16
+ # UTF-32
+ UTF32 = NKF::UTF32
+ # UNKNOWN
+ UNKNOWN = NKF::UNKNOWN
+
+ #
+ # Public Methods
+ #
+
+ # call-seq:
+ # Kconv.kconv(str, to_enc, from_enc=nil)
+ #
+ # Convert <code>str</code> to out_code.
+ # <code>out_code</code> and <code>in_code</code> are given as constants of Kconv.
+ def kconv(str, to_enc, from_enc=nil)
+ opt = ''
+ opt += ' --ic=' + from_enc.to_s if from_enc
+ opt += ' --oc=' + to_enc.to_s if to_enc
+
+ ::NKF::nkf(opt, str)
+ end
+ module_function :kconv
+
+ #
+ # Encode to
+ #
+
+ # call-seq:
+ # Kconv.tojis(str) => string
+ #
+ # Convert <code>str</code> to ISO-2022-JP
+ def tojis(str)
+ kconv(str, JIS)
+ end
+ module_function :tojis
+
+ # call-seq:
+ # Kconv.toeuc(str) => string
+ #
+ # Convert <code>str</code> to EUC-JP
+ def toeuc(str)
+ kconv(str, EUC)
+ end
+ module_function :toeuc
+
+ # call-seq:
+ # Kconv.tosjis(str) => string
+ #
+ # Convert <code>str</code> to Shift_JIS
+ def tosjis(str)
+ kconv(str, SJIS)
+ end
+ module_function :tosjis
+
+ # call-seq:
+ # Kconv.toutf8(str) => string
+ #
+ # Convert <code>str</code> to UTF-8
+ def toutf8(str)
+ kconv(str, UTF8)
+ end
+ module_function :toutf8
+
+ # call-seq:
+ # Kconv.toutf16(str) => string
+ #
+ # Convert <code>str</code> to UTF-16
+ def toutf16(str)
+ kconv(str, UTF16)
+ end
+ module_function :toutf16
+
+ # call-seq:
+ # Kconv.toutf32(str) => string
+ #
+ # Convert <code>str</code> to UTF-32
+ def toutf32(str)
+ kconv(str, UTF32)
+ end
+ module_function :toutf32
+
+ # call-seq:
+ # Kconv.tolocale => string
+ #
+ # Convert <code>self</code> to locale encoding
+ def tolocale(str)
+ kconv(str, Encoding.locale_charmap)
+ end
+ module_function :tolocale
+
+ #
+ # guess
+ #
+
+ # call-seq:
+ # Kconv.guess(str) => encoding
+ #
+ # Guess input encoding by NKF.guess
+ def guess(str)
+ ::NKF::guess(str)
+ end
+ module_function :guess
+
+ #
+ # isEncoding
+ #
+
+ # call-seq:
+ # Kconv.iseuc(str) => true or false
+ #
+ # Returns whether input encoding is EUC-JP or not.
+ #
+ # *Note* don't expect this return value is MatchData.
+ def iseuc(str)
+ str.dup.force_encoding(EUC).valid_encoding?
+ end
+ module_function :iseuc
+
+ # call-seq:
+ # Kconv.issjis(str) => true or false
+ #
+ # Returns whether input encoding is Shift_JIS or not.
+ def issjis(str)
+ str.dup.force_encoding(SJIS).valid_encoding?
+ end
+ module_function :issjis
+
+ # call-seq:
+ # Kconv.isjis(str) => true or false
+ #
+ # Returns whether input encoding is ISO-2022-JP or not.
+ def isjis(str)
+ /\A [\t\n\r\x20-\x7E]*
+ (?:
+ (?:\x1b \x28 I [\x21-\x7E]*
+ |\x1b \x28 J [\x21-\x7E]*
+ |\x1b \x24 @ (?:[\x21-\x7E]{2})*
+ |\x1b \x24 B (?:[\x21-\x7E]{2})*
+ |\x1b \x24 \x28 D (?:[\x21-\x7E]{2})*
+ )*
+ \x1b \x28 B [\t\n\r\x20-\x7E]*
+ )*
+ \z/nox =~ str.dup.force_encoding('BINARY') ? true : false
+ end
+ module_function :isjis
+
+ # call-seq:
+ # Kconv.isutf8(str) => true or false
+ #
+ # Returns whether input encoding is UTF-8 or not.
+ def isutf8(str)
+ str.dup.force_encoding(UTF8).valid_encoding?
+ end
+ module_function :isutf8
+end
+
+class String
+ # call-seq:
+ # String#kconv(to_enc, from_enc)
+ #
+ # Convert <code>self</code> to out_code.
+ # <code>out_code</code> and <code>in_code</code> are given as constants of Kconv.
+ def kconv(to_enc, from_enc=nil)
+ form_enc = self.encoding if !from_enc && self.encoding != Encoding.list[0]
+ Kconv::kconv(self, to_enc, from_enc)
+ end
+
+ #
+ # to Encoding
+ #
+
+ # call-seq:
+ # String#tojis => string
+ #
+ # Convert <code>self</code> to ISO-2022-JP
+ def tojis; Kconv.tojis(self) end
+
+ # call-seq:
+ # String#toeuc => string
+ #
+ # Convert <code>self</code> to EUC-JP
+ def toeuc; Kconv.toeuc(self) end
+
+ # call-seq:
+ # String#tosjis => string
+ #
+ # Convert <code>self</code> to Shift_JIS
+ def tosjis; Kconv.tosjis(self) end
+
+ # call-seq:
+ # String#toutf8 => string
+ #
+ # Convert <code>self</code> to UTF-8
+ def toutf8; Kconv.toutf8(self) end
+
+ # call-seq:
+ # String#toutf16 => string
+ #
+ # Convert <code>self</code> to UTF-16
+ def toutf16; Kconv.toutf16(self) end
+
+ # call-seq:
+ # String#toutf32 => string
+ #
+ # Convert <code>self</code> to UTF-32
+ def toutf32; Kconv.toutf32(self) end
+
+ # call-seq:
+ # String#tolocale => string
+ #
+ # Convert <code>self</code> to locale encoding
+ def tolocale; Kconv.tolocale(self) end
+
+ #
+ # is Encoding
+ #
+
+ # call-seq:
+ # String#iseuc => true or false
+ #
+ # Returns whether <code>self</code>'s encoding is EUC-JP or not.
+ def iseuc; Kconv.iseuc(self) end
+
+ # call-seq:
+ # String#issjis => true or false
+ #
+ # Returns whether <code>self</code>'s encoding is Shift_JIS or not.
+ def issjis; Kconv.issjis(self) end
+
+ # call-seq:
+ # String#isjis => true or false
+ #
+ # Returns whether <code>self</code>'s encoding is ISO-2022-JP or not.
+ def isjis; Kconv.isjis(self) end
+
+ # call-seq:
+ # String#isutf8 => true or false
+ #
+ # Returns whether <code>self</code>'s encoding is UTF-8 or not.
+ def isutf8; Kconv.isutf8(self) end
+end
diff --git a/ruby/lib/logger.rb b/ruby/lib/logger.rb
new file mode 100644
index 0000000..c8d9cfc
--- /dev/null
+++ b/ruby/lib/logger.rb
@@ -0,0 +1,732 @@
+# logger.rb - simple logging utility
+# Copyright (C) 2000-2003, 2005 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>.
+
+require 'monitor'
+
+# = logger.rb
+#
+# Simple logging utility.
+#
+# Author:: NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
+# Documentation:: NAKAMURA, Hiroshi and Gavin Sinclair
+# License::
+# You can redistribute it and/or modify it under the same terms of Ruby's
+# license; either the dual license version in 2003, or any later version.
+# Revision:: $Id: logger.rb 20321 2008-11-22 14:52:06Z yugui $
+#
+# See Logger for documentation.
+#
+
+
+#
+# == Description
+#
+# The Logger class provides a simple but sophisticated logging utility that
+# anyone can use because it's included in the Ruby 1.8.x standard library.
+#
+# The HOWTOs below give a code-based overview of Logger's usage, but the basic
+# concept is as follows. You create a Logger object (output to a file or
+# elsewhere), and use it to log messages. The messages will have varying
+# levels (+info+, +error+, etc), reflecting their varying importance. The
+# levels, and their meanings, are:
+#
+# +FATAL+:: an unhandleable error that results in a program crash
+# +ERROR+:: a handleable error condition
+# +WARN+:: a warning
+# +INFO+:: generic (useful) information about system operation
+# +DEBUG+:: low-level information for developers
+#
+# So each message has a level, and the Logger itself has a level, which acts
+# as a filter, so you can control the amount of information emitted from the
+# logger without having to remove actual messages.
+#
+# For instance, in a production system, you may have your logger(s) set to
+# +INFO+ (or +WARN+ if you don't want the log files growing large with
+# repetitive information). When you are developing it, though, you probably
+# want to know about the program's internal state, and would set them to
+# +DEBUG+.
+#
+# === Example
+#
+# A simple example demonstrates the above explanation:
+#
+# log = Logger.new(STDOUT)
+# log.level = Logger::WARN
+#
+# log.debug("Created logger")
+# log.info("Program started")
+# log.warn("Nothing to do!")
+#
+# begin
+# File.each_line(path) do |line|
+# unless line =~ /^(\w+) = (.*)$/
+# log.error("Line in wrong format: #{line}")
+# end
+# end
+# rescue => err
+# log.fatal("Caught exception; exiting")
+# log.fatal(err)
+# end
+#
+# Because the Logger's level is set to +WARN+, only the warning, error, and
+# fatal messages are recorded. The debug and info messages are silently
+# discarded.
+#
+# === Features
+#
+# There are several interesting features that Logger provides, like
+# auto-rolling of log files, setting the format of log messages, and
+# specifying a program name in conjunction with the message. The next section
+# shows you how to achieve these things.
+#
+#
+# == HOWTOs
+#
+# === How to create a logger
+#
+# The options below give you various choices, in more or less increasing
+# complexity.
+#
+# 1. Create a logger which logs messages to STDERR/STDOUT.
+#
+# logger = Logger.new(STDERR)
+# logger = Logger.new(STDOUT)
+#
+# 2. Create a logger for the file which has the specified name.
+#
+# logger = Logger.new('logfile.log')
+#
+# 3. Create a logger for the specified file.
+#
+# file = File.open('foo.log', File::WRONLY | File::APPEND)
+# # To create new (and to remove old) logfile, add File::CREAT like;
+# # file = open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
+# logger = Logger.new(file)
+#
+# 4. Create a logger which ages logfile once it reaches a certain size. Leave
+# 10 "old log files" and each file is about 1,024,000 bytes.
+#
+# logger = Logger.new('foo.log', 10, 1024000)
+#
+# 5. Create a logger which ages logfile daily/weekly/monthly.
+#
+# logger = Logger.new('foo.log', 'daily')
+# logger = Logger.new('foo.log', 'weekly')
+# logger = Logger.new('foo.log', 'monthly')
+#
+# === How to log a message
+#
+# Notice the different methods (+fatal+, +error+, +info+) being used to log
+# messages of various levels. Other methods in this family are +warn+ and
+# +debug+. +add+ is used below to log a message of an arbitrary (perhaps
+# dynamic) level.
+#
+# 1. Message in block.
+#
+# logger.fatal { "Argument 'foo' not given." }
+#
+# 2. Message as a string.
+#
+# logger.error "Argument #{ @foo } mismatch."
+#
+# 3. With progname.
+#
+# logger.info('initialize') { "Initializing..." }
+#
+# 4. With severity.
+#
+# logger.add(Logger::FATAL) { 'Fatal error!' }
+#
+# === How to close a logger
+#
+# logger.close
+#
+# === Setting severity threshold
+#
+# 1. Original interface.
+#
+# logger.sev_threshold = Logger::WARN
+#
+# 2. Log4r (somewhat) compatible interface.
+#
+# logger.level = Logger::INFO
+#
+# DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
+#
+#
+# == Format
+#
+# Log messages are rendered in the output stream in a certain format by
+# default. The default format and a sample are shown below:
+#
+# Log format:
+# SeverityID, [Date Time mSec #pid] SeverityLabel -- ProgName: message
+#
+# Log sample:
+# I, [Wed Mar 03 02:34:24 JST 1999 895701 #19074] INFO -- Main: info.
+#
+# You may change the date and time format in this manner:
+#
+# logger.datetime_format = "%Y-%m-%d %H:%M:%S"
+# # e.g. "2004-01-03 00:54:26"
+#
+# You may change the overall format with Logger#formatter= method.
+#
+# logger.formatter = proc { |severity, datetime, progname, msg|
+# "#{datetime}: #{msg}\n"
+# }
+# # e.g. "Thu Sep 22 08:51:08 GMT+9:00 2005: hello world"
+#
+
+
+class Logger
+ VERSION = "1.2.6"
+ id, name, rev = %w$Id: logger.rb 20321 2008-11-22 14:52:06Z yugui $
+ if name
+ name = name.chomp(",v")
+ else
+ name = File.basename(__FILE__)
+ end
+ rev ||= "v#{VERSION}"
+ ProgName = "#{name}/#{rev}"
+
+ class Error < RuntimeError; end
+ class ShiftingError < Error; end
+
+ # Logging severity.
+ module Severity
+ DEBUG = 0
+ INFO = 1
+ WARN = 2
+ ERROR = 3
+ FATAL = 4
+ UNKNOWN = 5
+ end
+ include Severity
+
+ # Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
+ attr_accessor :level
+
+ # Logging program name.
+ attr_accessor :progname
+
+ # Logging date-time format (string passed to +strftime+).
+ def datetime_format=(datetime_format)
+ @default_formatter.datetime_format = datetime_format
+ end
+
+ def datetime_format
+ @default_formatter.datetime_format
+ end
+
+ # Logging formatter. formatter#call is invoked with 4 arguments; severity,
+ # time, progname and msg for each log. Bear in mind that time is a Time and
+ # msg is an Object that user passed and it could not be a String. It is
+ # expected to return a logdev#write-able Object. Default formatter is used
+ # when no formatter is set.
+ attr_accessor :formatter
+
+ alias sev_threshold level
+ alias sev_threshold= level=
+
+ # Returns +true+ iff the current severity level allows for the printing of
+ # +DEBUG+ messages.
+ def debug?; @level <= DEBUG; end
+
+ # Returns +true+ iff the current severity level allows for the printing of
+ # +INFO+ messages.
+ def info?; @level <= INFO; end
+
+ # Returns +true+ iff the current severity level allows for the printing of
+ # +WARN+ messages.
+ def warn?; @level <= WARN; end
+
+ # Returns +true+ iff the current severity level allows for the printing of
+ # +ERROR+ messages.
+ def error?; @level <= ERROR; end
+
+ # Returns +true+ iff the current severity level allows for the printing of
+ # +FATAL+ messages.
+ def fatal?; @level <= FATAL; end
+
+ #
+ # === Synopsis
+ #
+ # Logger.new(name, shift_age = 7, shift_size = 1048576)
+ # Logger.new(name, shift_age = 'weekly')
+ #
+ # === Args
+ #
+ # +logdev+::
+ # The log device. This is a filename (String) or IO object (typically
+ # +STDOUT+, +STDERR+, or an open file).
+ # +shift_age+::
+ # Number of old log files to keep, *or* frequency of rotation (+daily+,
+ # +weekly+ or +monthly+).
+ # +shift_size+::
+ # Maximum logfile size (only applies when +shift_age+ is a number).
+ #
+ # === Description
+ #
+ # Create an instance.
+ #
+ def initialize(logdev, shift_age = 0, shift_size = 1048576)
+ @progname = nil
+ @level = DEBUG
+ @default_formatter = Formatter.new
+ @formatter = nil
+ @logdev = nil
+ if logdev
+ @logdev = LogDevice.new(logdev, :shift_age => shift_age,
+ :shift_size => shift_size)
+ end
+ end
+
+ #
+ # === Synopsis
+ #
+ # Logger#add(severity, message = nil, progname = nil) { ... }
+ #
+ # === Args
+ #
+ # +severity+::
+ # Severity. Constants are defined in Logger namespace: +DEBUG+, +INFO+,
+ # +WARN+, +ERROR+, +FATAL+, or +UNKNOWN+.
+ # +message+::
+ # The log message. A String or Exception.
+ # +progname+::
+ # Program name string. Can be omitted. Treated as a message if no +message+ and
+ # +block+ are given.
+ # +block+::
+ # Can be omitted. Called to get a message string if +message+ is nil.
+ #
+ # === Return
+ #
+ # +true+ if successful, +false+ otherwise.
+ #
+ # When the given severity is not high enough (for this particular logger), log
+ # no message, and return +true+.
+ #
+ # === Description
+ #
+ # Log a message if the given severity is high enough. This is the generic
+ # logging method. Users will be more inclined to use #debug, #info, #warn,
+ # #error, and #fatal.
+ #
+ # <b>Message format</b>: +message+ can be any object, but it has to be
+ # converted to a String in order to log it. Generally, +inspect+ is used
+ # if the given object is not a String.
+ # A special case is an +Exception+ object, which will be printed in detail,
+ # including message, class, and backtrace. See #msg2str for the
+ # implementation if required.
+ #
+ # === Bugs
+ #
+ # * Logfile is not locked.
+ # * Append open does not need to lock file.
+ # * But on the OS which supports multi I/O, records possibly be mixed.
+ #
+ def add(severity, message = nil, progname = nil, &block)
+ severity ||= UNKNOWN
+ if @logdev.nil? or severity < @level
+ return true
+ end
+ progname ||= @progname
+ if message.nil?
+ if block_given?
+ message = yield
+ else
+ message = progname
+ progname = @progname
+ end
+ end
+ @logdev.write(
+ format_message(format_severity(severity), Time.now, progname, message))
+ true
+ end
+ alias log add
+
+ #
+ # Dump given message to the log device without any formatting. If no log
+ # device exists, return +nil+.
+ #
+ def <<(msg)
+ unless @logdev.nil?
+ @logdev.write(msg)
+ end
+ end
+
+ #
+ # Log a +DEBUG+ message.
+ #
+ # See #info for more information.
+ #
+ def debug(progname = nil, &block)
+ add(DEBUG, nil, progname, &block)
+ end
+
+ #
+ # Log an +INFO+ message.
+ #
+ # The message can come either from the +progname+ argument or the +block+. If
+ # both are provided, then the +block+ is used as the message, and +progname+
+ # is used as the program name.
+ #
+ # === Examples
+ #
+ # logger.info("MainApp") { "Received connection from #{ip}" }
+ # # ...
+ # logger.info "Waiting for input from user"
+ # # ...
+ # logger.info { "User typed #{input}" }
+ #
+ # You'll probably stick to the second form above, unless you want to provide a
+ # program name (which you can do with <tt>Logger#progname=</tt> as well).
+ #
+ # === Return
+ #
+ # See #add.
+ #
+ def info(progname = nil, &block)
+ add(INFO, nil, progname, &block)
+ end
+
+ #
+ # Log a +WARN+ message.
+ #
+ # See #info for more information.
+ #
+ def warn(progname = nil, &block)
+ add(WARN, nil, progname, &block)
+ end
+
+ #
+ # Log an +ERROR+ message.
+ #
+ # See #info for more information.
+ #
+ def error(progname = nil, &block)
+ add(ERROR, nil, progname, &block)
+ end
+
+ #
+ # Log a +FATAL+ message.
+ #
+ # See #info for more information.
+ #
+ def fatal(progname = nil, &block)
+ add(FATAL, nil, progname, &block)
+ end
+
+ #
+ # Log an +UNKNOWN+ message. This will be printed no matter what the logger
+ # level.
+ #
+ # See #info for more information.
+ #
+ def unknown(progname = nil, &block)
+ add(UNKNOWN, nil, progname, &block)
+ end
+
+ #
+ # Close the logging device.
+ #
+ def close
+ @logdev.close if @logdev
+ end
+
+private
+
+ # Severity label for logging. (max 5 char)
+ SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
+
+ def format_severity(severity)
+ SEV_LABEL[severity] || 'ANY'
+ end
+
+ def format_message(severity, datetime, progname, msg)
+ (@formatter || @default_formatter).call(severity, datetime, progname, msg)
+ end
+
+
+ class Formatter
+ Format = "%s, [%s#%d] %5s -- %s: %s\n"
+
+ attr_accessor :datetime_format
+
+ def initialize
+ @datetime_format = nil
+ end
+
+ def call(severity, time, progname, msg)
+ Format % [severity[0..0], format_datetime(time), $$, severity, progname,
+ msg2str(msg)]
+ end
+
+ private
+
+ def format_datetime(time)
+ if @datetime_format.nil?
+ time.strftime("%Y-%m-%dT%H:%M:%S.") << "%06d " % time.usec
+ else
+ time.strftime(@datetime_format)
+ end
+ end
+
+ def msg2str(msg)
+ case msg
+ when ::String
+ msg
+ when ::Exception
+ "#{ msg.message } (#{ msg.class })\n" <<
+ (msg.backtrace || []).join("\n")
+ else
+ msg.inspect
+ end
+ end
+ end
+
+
+ class LogDevice
+ attr_reader :dev
+ attr_reader :filename
+
+ class LogDeviceMutex
+ include MonitorMixin
+ end
+
+ def initialize(log = nil, opt = {})
+ @dev = @filename = @shift_age = @shift_size = nil
+ @mutex = LogDeviceMutex.new
+ if log.respond_to?(:write) and log.respond_to?(:close)
+ @dev = log
+ else
+ @dev = open_logfile(log)
+ @dev.sync = true
+ @filename = log
+ @shift_age = opt[:shift_age] || 7
+ @shift_size = opt[:shift_size] || 1048576
+ end
+ end
+
+ def write(message)
+ @mutex.synchronize do
+ if @shift_age and @dev.respond_to?(:stat)
+ begin
+ check_shift_log
+ rescue
+ raise Logger::ShiftingError.new("Shifting failed. #{$!}")
+ end
+ end
+ @dev.write(message)
+ end
+ end
+
+ def close
+ @mutex.synchronize do
+ @dev.close
+ end
+ end
+
+ private
+
+ def open_logfile(filename)
+ if (FileTest.exist?(filename))
+ open(filename, (File::WRONLY | File::APPEND))
+ else
+ create_logfile(filename)
+ end
+ end
+
+ def create_logfile(filename)
+ logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
+ logdev.sync = true
+ add_log_header(logdev)
+ logdev
+ end
+
+ def add_log_header(file)
+ file.write(
+ "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
+ )
+ end
+
+ SiD = 24 * 60 * 60
+
+ def check_shift_log
+ if @shift_age.is_a?(Integer)
+ # Note: always returns false if '0'.
+ if @filename && (@shift_age > 0) && (@dev.stat.size > @shift_size)
+ shift_log_age
+ end
+ else
+ now = Time.now
+ if @dev.stat.mtime <= previous_period_end(now)
+ shift_log_period(now)
+ end
+ end
+ end
+
+ def shift_log_age
+ (@shift_age-3).downto(0) do |i|
+ if FileTest.exist?("#{@filename}.#{i}")
+ File.rename("#{@filename}.#{i}", "#{@filename}.#{i+1}")
+ end
+ end
+ @dev.close
+ File.rename("#{@filename}", "#{@filename}.0")
+ @dev = create_logfile(@filename)
+ return true
+ end
+
+ def shift_log_period(now)
+ postfix = previous_period_end(now).strftime("%Y%m%d") # YYYYMMDD
+ age_file = "#{@filename}.#{postfix}"
+ if FileTest.exist?(age_file)
+ raise RuntimeError.new("'#{ age_file }' already exists.")
+ end
+ @dev.close
+ File.rename("#{@filename}", age_file)
+ @dev = create_logfile(@filename)
+ return true
+ end
+
+ def previous_period_end(now)
+ case @shift_age
+ when /^daily$/
+ eod(now - 1 * SiD)
+ when /^weekly$/
+ eod(now - ((now.wday + 1) * SiD))
+ when /^monthly$/
+ eod(now - now.mday * SiD)
+ else
+ now
+ end
+ end
+
+ def eod(t)
+ Time.mktime(t.year, t.month, t.mday, 23, 59, 59)
+ end
+ end
+
+
+ #
+ # == Description
+ #
+ # Application -- Add logging support to your application.
+ #
+ # == Usage
+ #
+ # 1. Define your application class as a sub-class of this class.
+ # 2. Override 'run' method in your class to do many things.
+ # 3. Instantiate it and invoke 'start'.
+ #
+ # == Example
+ #
+ # class FooApp < Application
+ # def initialize(foo_app, application_specific, arguments)
+ # super('FooApp') # Name of the application.
+ # end
+ #
+ # def run
+ # ...
+ # log(WARN, 'warning', 'my_method1')
+ # ...
+ # @log.error('my_method2') { 'Error!' }
+ # ...
+ # end
+ # end
+ #
+ # status = FooApp.new(....).start
+ #
+ class Application
+ include Logger::Severity
+
+ # Name of the application given at initialize.
+ attr_reader :appname
+
+ #
+ # == Synopsis
+ #
+ # Application.new(appname = '')
+ #
+ # == Args
+ #
+ # +appname+:: Name of the application.
+ #
+ # == Description
+ #
+ # Create an instance. Log device is +STDERR+ by default. This can be
+ # changed with #set_log.
+ #
+ def initialize(appname = nil)
+ @appname = appname
+ @log = Logger.new(STDERR)
+ @log.progname = @appname
+ @level = @log.level
+ end
+
+ #
+ # Start the application. Return the status code.
+ #
+ def start
+ status = -1
+ begin
+ log(INFO, "Start of #{ @appname }.")
+ status = run
+ rescue
+ log(FATAL, "Detected an exception. Stopping ... #{$!} (#{$!.class})\n" << $@.join("\n"))
+ ensure
+ log(INFO, "End of #{ @appname }. (status: #{ status.to_s })")
+ end
+ status
+ end
+
+ # Logger for this application. See the class Logger for an explanation.
+ def logger
+ @log
+ end
+
+ #
+ # Sets the logger for this application. See the class Logger for an explanation.
+ #
+ def logger=(logger)
+ @log = logger
+ end
+
+ #
+ # Sets the log device for this application. See <tt>Logger.new</tt> for an explanation
+ # of the arguments.
+ #
+ def set_log(logdev, shift_age = 0, shift_size = 1024000)
+ @log = Logger.new(logdev, shift_age, shift_size)
+ @log.progname = @appname
+ @log.level = @level
+ end
+
+ def log=(logdev)
+ set_log(logdev)
+ end
+
+ #
+ # Set the logging threshold, just like <tt>Logger#level=</tt>.
+ #
+ def level=(level)
+ @level = level
+ @log.level = @level
+ end
+
+ #
+ # See Logger#add. This application's +appname+ is used.
+ #
+ def log(severity, message = nil, &block)
+ @log.add(severity, message, @appname, &block) if @log
+ end
+
+ private
+
+ def run
+ raise RuntimeError.new('Method run must be defined in the derived class.')
+ end
+ end
+end
diff --git a/ruby/lib/mathn.rb b/ruby/lib/mathn.rb
new file mode 100644
index 0000000..780ecd6
--- /dev/null
+++ b/ruby/lib/mathn.rb
@@ -0,0 +1,206 @@
+#
+# mathn.rb -
+# $Release Version: 0.5 $
+# $Revision: 1.1.1.1.4.1 $
+# by Keiju ISHITSUKA(SHL Japan Inc.)
+#
+# --
+#
+#
+#
+
+require "cmath.rb"
+require "matrix.rb"
+require "prime.rb"
+
+require "mathn/rational"
+require "mathn/complex"
+
+unless defined?(Math.exp!)
+ Object.instance_eval{remove_const :Math}
+ Math = CMath
+end
+
+class Fixnum
+ remove_method :/
+ alias / quo
+
+ alias power! ** unless method_defined? :power!
+
+ def ** (other)
+ if self < 0 && other.round != other
+ Complex(self, 0.0) ** other
+ else
+ power!(other)
+ end
+ end
+
+end
+
+class Bignum
+ remove_method :/
+ alias / quo
+
+ alias power! ** unless method_defined? :power!
+
+ def ** (other)
+ if self < 0 && other.round != other
+ Complex(self, 0.0) ** other
+ else
+ power!(other)
+ end
+ end
+
+end
+
+class Rational
+ def ** (other)
+ if other.kind_of?(Rational)
+ other2 = other
+ if self < 0
+ return Complex(self, 0.0) ** other
+ elsif other == 0
+ return Rational(1,1)
+ elsif self == 0
+ return Rational(0,1)
+ elsif self == 1
+ return Rational(1,1)
+ end
+
+ npd = numerator.prime_division
+ dpd = denominator.prime_division
+ if other < 0
+ other = -other
+ npd, dpd = dpd, npd
+ end
+
+ for elm in npd
+ elm[1] = elm[1] * other
+ if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
+ return Float(self) ** other2
+ end
+ elm[1] = elm[1].to_i
+ end
+
+ for elm in dpd
+ elm[1] = elm[1] * other
+ if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
+ return Float(self) ** other2
+ end
+ elm[1] = elm[1].to_i
+ end
+
+ num = Integer.from_prime_division(npd)
+ den = Integer.from_prime_division(dpd)
+
+ Rational(num,den)
+
+ elsif other.kind_of?(Integer)
+ if other > 0
+ num = numerator ** other
+ den = denominator ** other
+ elsif other < 0
+ num = denominator ** -other
+ den = numerator ** -other
+ elsif other == 0
+ num = 1
+ den = 1
+ end
+ Rational(num, den)
+ elsif other.kind_of?(Float)
+ Float(self) ** other
+ else
+ x , y = other.coerce(self)
+ x ** y
+ end
+ end
+end
+
+module Math
+ remove_method(:sqrt)
+ def sqrt(a)
+ if a.kind_of?(Complex)
+ abs = sqrt(a.real*a.real + a.imag*a.imag)
+# if not abs.kind_of?(Rational)
+# return a**Rational(1,2)
+# end
+ x = sqrt((a.real + abs)/Rational(2))
+ y = sqrt((-a.real + abs)/Rational(2))
+# if !(x.kind_of?(Rational) and y.kind_of?(Rational))
+# return a**Rational(1,2)
+# end
+ if a.imag >= 0
+ Complex(x, y)
+ else
+ Complex(x, -y)
+ end
+ elsif a.respond_to?(:nan?) and a.nan?
+ a
+ elsif a >= 0
+ rsqrt(a)
+ else
+ Complex(0,rsqrt(-a))
+ end
+ end
+
+ def rsqrt(a)
+ if a.kind_of?(Float)
+ sqrt!(a)
+ elsif a.kind_of?(Rational)
+ rsqrt(a.numerator)/rsqrt(a.denominator)
+ else
+ src = a
+ max = 2 ** 32
+ byte_a = [src & 0xffffffff]
+ # ruby's bug
+ while (src >= max) and (src >>= 32)
+ byte_a.unshift src & 0xffffffff
+ end
+
+ answer = 0
+ main = 0
+ side = 0
+ for elm in byte_a
+ main = (main << 32) + elm
+ side <<= 16
+ if answer != 0
+ if main * 4 < side * side
+ applo = main.div(side)
+ else
+ applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
+ end
+ else
+ applo = sqrt!(main).to_i + 1
+ end
+
+ while (x = (side + applo) * applo) > main
+ applo -= 1
+ end
+ main -= x
+ answer = (answer << 16) + applo
+ side += applo * 2
+ end
+ if main == 0
+ answer
+ else
+ sqrt!(a)
+ end
+ end
+ end
+
+ module_function :sqrt
+ module_function :rsqrt
+end
+
+class Float
+ alias power! **
+
+ def ** (other)
+ if self < 0 && other.round != other
+ Complex(self, 0.0) ** other
+ else
+ power!(other)
+ end
+ end
+
+end
diff --git a/ruby/lib/matrix.rb b/ruby/lib/matrix.rb
new file mode 100644
index 0000000..04616d0
--- /dev/null
+++ b/ruby/lib/matrix.rb
@@ -0,0 +1,1381 @@
+#!/usr/local/bin/ruby
+#--
+# matrix.rb -
+# $Release Version: 1.0$
+# $Revision: 1.13 $
+# Original Version from Smalltalk-80 version
+# on July 23, 1985 at 8:37:17 am
+# by Keiju ISHITSUKA
+#++
+#
+# = matrix.rb
+#
+# An implementation of Matrix and Vector classes.
+#
+# Author:: Keiju ISHITSUKA
+# Documentation:: Gavin Sinclair (sourced from <i>Ruby in a Nutshell</i> (Matsumoto, O'Reilly))
+#
+# See classes Matrix and Vector for documentation.
+#
+
+require "e2mmap.rb"
+
+module ExceptionForMatrix # :nodoc:
+ extend Exception2MessageMapper
+ def_e2message(TypeError, "wrong argument type %s (expected %s)")
+ def_e2message(ArgumentError, "Wrong # of arguments(%d for %d)")
+
+ def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch")
+ def_exception("ErrNotRegular", "Not Regular Matrix")
+ def_exception("ErrOperationNotDefined", "This operation(%s) can\\'t defined")
+end
+
+#
+# The +Matrix+ class represents a mathematical matrix, and provides methods for creating
+# special-case matrices (zero, identity, diagonal, singular, vector), operating on them
+# arithmetically and algebraically, and determining their mathematical properties (trace, rank,
+# inverse, determinant).
+#
+# Note that although matrices should theoretically be rectangular, this is not
+# enforced by the class.
+#
+# Also note that the determinant of integer matrices may be incorrectly calculated unless you
+# also <tt>require 'mathn'</tt>. This may be fixed in the future.
+#
+# == Method Catalogue
+#
+# To create a matrix:
+# * <tt> Matrix[*rows] </tt>
+# * <tt> Matrix.[](*rows) </tt>
+# * <tt> Matrix.rows(rows, copy = true) </tt>
+# * <tt> Matrix.columns(columns) </tt>
+# * <tt> Matrix.diagonal(*values) </tt>
+# * <tt> Matrix.scalar(n, value) </tt>
+# * <tt> Matrix.scalar(n, value) </tt>
+# * <tt> Matrix.identity(n) </tt>
+# * <tt> Matrix.unit(n) </tt>
+# * <tt> Matrix.I(n) </tt>
+# * <tt> Matrix.zero(n) </tt>
+# * <tt> Matrix.row_vector(row) </tt>
+# * <tt> Matrix.column_vector(column) </tt>
+#
+# To access Matrix elements/columns/rows/submatrices/properties:
+# * <tt> [](i, j) </tt>
+# * <tt> #row_size </tt>
+# * <tt> #column_size </tt>
+# * <tt> #row(i) </tt>
+# * <tt> #column(j) </tt>
+# * <tt> #collect </tt>
+# * <tt> #map </tt>
+# * <tt> #minor(*param) </tt>
+#
+# Properties of a matrix:
+# * <tt> #regular? </tt>
+# * <tt> #singular? </tt>
+# * <tt> #square? </tt>
+#
+# Matrix arithmetic:
+# * <tt> *(m) </tt>
+# * <tt> +(m) </tt>
+# * <tt> -(m) </tt>
+# * <tt> #/(m) </tt>
+# * <tt> #inverse </tt>
+# * <tt> #inv </tt>
+# * <tt> ** </tt>
+#
+# Matrix functions:
+# * <tt> #determinant </tt>
+# * <tt> #det </tt>
+# * <tt> #rank </tt>
+# * <tt> #trace </tt>
+# * <tt> #tr </tt>
+# * <tt> #transpose </tt>
+# * <tt> #t </tt>
+#
+# Conversion to other data types:
+# * <tt> #coerce(other) </tt>
+# * <tt> #row_vectors </tt>
+# * <tt> #column_vectors </tt>
+# * <tt> #to_a </tt>
+#
+# String representations:
+# * <tt> #to_s </tt>
+# * <tt> #inspect </tt>
+#
+class Matrix
+ @RCS_ID='-$Id: matrix.rb,v 1.13 2001/12/09 14:22:23 keiju Exp keiju $-'
+
+# extend Exception2MessageMapper
+ include ExceptionForMatrix
+
+ # instance creations
+ private_class_method :new
+
+ #
+ # Creates a matrix where each argument is a row.
+ # Matrix[ [25, 93], [-1, 66] ]
+ # => 25 93
+ # -1 66
+ #
+ def Matrix.[](*rows)
+ new(:init_rows, rows, false)
+ end
+
+ #
+ # Creates a matrix where +rows+ is an array of arrays, each of which is a row
+ # to the matrix. If the optional argument +copy+ is false, use the given
+ # arrays as the internal structure of the matrix without copying.
+ # Matrix.rows([[25, 93], [-1, 66]])
+ # => 25 93
+ # -1 66
+ def Matrix.rows(rows, copy = true)
+ new(:init_rows, rows, copy)
+ end
+
+ #
+ # Creates a matrix using +columns+ as an array of column vectors.
+ # Matrix.columns([[25, 93], [-1, 66]])
+ # => 25 -1
+ # 93 66
+ #
+ #
+ def Matrix.columns(columns)
+ rows = (0 .. columns[0].size - 1).collect {|i|
+ (0 .. columns.size - 1).collect {|j|
+ columns[j][i]
+ }
+ }
+ Matrix.rows(rows, false)
+ end
+
+ #
+ # Creates a matrix where the diagonal elements are composed of +values+.
+ # Matrix.diagonal(9, 5, -3)
+ # => 9 0 0
+ # 0 5 0
+ # 0 0 -3
+ #
+ def Matrix.diagonal(*values)
+ size = values.size
+ rows = (0 .. size - 1).collect {|j|
+ row = Array.new(size).fill(0, 0, size)
+ row[j] = values[j]
+ row
+ }
+ rows(rows, false)
+ end
+
+ #
+ # Creates an +n+ by +n+ diagonal matrix where each diagonal element is
+ # +value+.
+ # Matrix.scalar(2, 5)
+ # => 5 0
+ # 0 5
+ #
+ def Matrix.scalar(n, value)
+ Matrix.diagonal(*Array.new(n).fill(value, 0, n))
+ end
+
+ #
+ # Creates an +n+ by +n+ identity matrix.
+ # Matrix.identity(2)
+ # => 1 0
+ # 0 1
+ #
+ def Matrix.identity(n)
+ Matrix.scalar(n, 1)
+ end
+ class << Matrix
+ alias unit identity
+ alias I identity
+ end
+
+ #
+ # Creates an +n+ by +n+ zero matrix.
+ # Matrix.zero(2)
+ # => 0 0
+ # 0 0
+ #
+ def Matrix.zero(n)
+ Matrix.scalar(n, 0)
+ end
+
+ #
+ # Creates a single-row matrix where the values of that row are as given in
+ # +row+.
+ # Matrix.row_vector([4,5,6])
+ # => 4 5 6
+ #
+ def Matrix.row_vector(row)
+ case row
+ when Vector
+ Matrix.rows([row.to_a], false)
+ when Array
+ Matrix.rows([row.dup], false)
+ else
+ Matrix.rows([[row]], false)
+ end
+ end
+
+ #
+ # Creates a single-column matrix where the values of that column are as given
+ # in +column+.
+ # Matrix.column_vector([4,5,6])
+ # => 4
+ # 5
+ # 6
+ #
+ def Matrix.column_vector(column)
+ case column
+ when Vector
+ Matrix.columns([column.to_a])
+ when Array
+ Matrix.columns([column])
+ else
+ Matrix.columns([[column]])
+ end
+ end
+
+ #
+ # This method is used by the other methods that create matrices, and is of no
+ # use to general users.
+ #
+ def initialize(init_method, *argv)
+ self.send(init_method, *argv)
+ end
+
+ def init_rows(rows, copy)
+ if copy
+ @rows = rows.collect{|row| row.dup}
+ else
+ @rows = rows
+ end
+ self
+ end
+ private :init_rows
+
+ #
+ # Returns element (+i+,+j+) of the matrix. That is: row +i+, column +j+.
+ #
+ def [](i, j)
+ @rows[i][j]
+ end
+ alias element []
+ alias component []
+
+ def []=(i, j, v)
+ @rows[i][j] = v
+ end
+ alias set_element []=
+ alias set_component []=
+ private :[]=, :set_element, :set_component
+
+ #
+ # Returns the number of rows.
+ #
+ def row_size
+ @rows.size
+ end
+
+ #
+ # Returns the number of columns. Note that it is possible to construct a
+ # matrix with uneven columns (e.g. Matrix[ [1,2,3], [4,5] ]), but this is
+ # mathematically unsound. This method uses the first row to determine the
+ # result.
+ #
+ def column_size
+ @rows[0].size
+ end
+
+ #
+ # Returns row vector number +i+ of the matrix as a Vector (starting at 0 like
+ # an array). When a block is given, the elements of that vector are iterated.
+ #
+ def row(i) # :yield: e
+ if block_given?
+ for e in @rows[i]
+ yield e
+ end
+ else
+ Vector.elements(@rows[i])
+ end
+ end
+
+ #
+ # Returns column vector number +j+ of the matrix as a Vector (starting at 0
+ # like an array). When a block is given, the elements of that vector are
+ # iterated.
+ #
+ def column(j) # :yield: e
+ if block_given?
+ 0.upto(row_size - 1) do |i|
+ yield @rows[i][j]
+ end
+ else
+ col = (0 .. row_size - 1).collect {|i|
+ @rows[i][j]
+ }
+ Vector.elements(col, false)
+ end
+ end
+
+ #
+ # Returns a matrix that is the result of iteration of the given block over all
+ # elements of the matrix.
+ # Matrix[ [1,2], [3,4] ].collect { |e| e**2 }
+ # => 1 4
+ # 9 16
+ #
+ def collect # :yield: e
+ rows = @rows.collect{|row| row.collect{|e| yield e}}
+ Matrix.rows(rows, false)
+ end
+ alias map collect
+
+ #
+ # Returns a section of the matrix. The parameters are either:
+ # * start_row, nrows, start_col, ncols; OR
+ # * col_range, row_range
+ #
+ # Matrix.diagonal(9, 5, -3).minor(0..1, 0..2)
+ # => 9 0 0
+ # 0 5 0
+ #
+ def minor(*param)
+ case param.size
+ when 2
+ from_row = param[0].first
+ size_row = param[0].end - from_row
+ size_row += 1 unless param[0].exclude_end?
+ from_col = param[1].first
+ size_col = param[1].end - from_col
+ size_col += 1 unless param[1].exclude_end?
+ when 4
+ from_row = param[0]
+ size_row = param[1]
+ from_col = param[2]
+ size_col = param[3]
+ else
+ Matrix.Raise ArgumentError, param.inspect
+ end
+
+ rows = @rows[from_row, size_row].collect{|row|
+ row[from_col, size_col]
+ }
+ Matrix.rows(rows, false)
+ end
+
+ #--
+ # TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Returns +true+ if this is a regular matrix.
+ #
+ def regular?
+ square? and rank == column_size
+ end
+
+ #
+ # Returns +true+ is this is a singular (i.e. non-regular) matrix.
+ #
+ def singular?
+ not regular?
+ end
+
+ #
+ # Returns +true+ is this is a square matrix. See note in column_size about this
+ # being unreliable, though.
+ #
+ def square?
+ column_size == row_size
+ end
+
+ #--
+ # OBJECT METHODS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Returns +true+ if and only if the two matrices contain equal elements.
+ #
+ def ==(other)
+ return false unless Matrix === other
+
+ other.compare_by_row_vectors(@rows)
+ end
+ def eql?(other)
+ return false unless Matrix === other
+
+ other.compare_by_row_vectors(@rows, :eql?)
+ end
+
+ #
+ # Not really intended for general consumption.
+ #
+ def compare_by_row_vectors(rows, comparison = :==)
+ return false unless @rows.size == rows.size
+
+ 0.upto(@rows.size - 1) do |i|
+ return false unless @rows[i].send(comparison, rows[i])
+ end
+ true
+ end
+
+ #
+ # Returns a clone of the matrix, so that the contents of each do not reference
+ # identical objects.
+ #
+ def clone
+ Matrix.rows(@rows)
+ end
+
+ #
+ # Returns a hash-code for the matrix.
+ #
+ def hash
+ value = 0
+ for row in @rows
+ for e in row
+ value ^= e.hash
+ end
+ end
+ return value
+ end
+
+ #--
+ # ARITHMETIC -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Matrix multiplication.
+ # Matrix[[2,4], [6,8]] * Matrix.identity(2)
+ # => 2 4
+ # 6 8
+ #
+ def *(m) # m is matrix or vector or number
+ case(m)
+ when Numeric
+ rows = @rows.collect {|row|
+ row.collect {|e|
+ e * m
+ }
+ }
+ return Matrix.rows(rows, false)
+ when Vector
+ m = Matrix.column_vector(m)
+ r = self * m
+ return r.column(0)
+ when Matrix
+ Matrix.Raise ErrDimensionMismatch if column_size != m.row_size
+
+ rows = (0 .. row_size - 1).collect {|i|
+ (0 .. m.column_size - 1).collect {|j|
+ vij = 0
+ 0.upto(column_size - 1) do |k|
+ vij += self[i, k] * m[k, j]
+ end
+ vij
+ }
+ }
+ return Matrix.rows(rows, false)
+ else
+ x, y = m.coerce(self)
+ return x * y
+ end
+ end
+
+ #
+ # Matrix addition.
+ # Matrix.scalar(2,5) + Matrix[[1,0], [-4,7]]
+ # => 6 0
+ # -4 12
+ #
+ def +(m)
+ case m
+ when Numeric
+ Matrix.Raise ErrOperationNotDefined, "+"
+ when Vector
+ m = Matrix.column_vector(m)
+ when Matrix
+ else
+ x, y = m.coerce(self)
+ return x + y
+ end
+
+ Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
+
+ rows = (0 .. row_size - 1).collect {|i|
+ (0 .. column_size - 1).collect {|j|
+ self[i, j] + m[i, j]
+ }
+ }
+ Matrix.rows(rows, false)
+ end
+
+ #
+ # Matrix subtraction.
+ # Matrix[[1,5], [4,2]] - Matrix[[9,3], [-4,1]]
+ # => -8 2
+ # 8 1
+ #
+ def -(m)
+ case m
+ when Numeric
+ Matrix.Raise ErrOperationNotDefined, "-"
+ when Vector
+ m = Matrix.column_vector(m)
+ when Matrix
+ else
+ x, y = m.coerce(self)
+ return x - y
+ end
+
+ Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
+
+ rows = (0 .. row_size - 1).collect {|i|
+ (0 .. column_size - 1).collect {|j|
+ self[i, j] - m[i, j]
+ }
+ }
+ Matrix.rows(rows, false)
+ end
+
+ #
+ # Matrix division (multiplication by the inverse).
+ # Matrix[[7,6], [3,9]] / Matrix[[2,9], [3,1]]
+ # => -7 1
+ # -3 -6
+ #
+ def /(other)
+ case other
+ when Numeric
+ rows = @rows.collect {|row|
+ row.collect {|e|
+ e / other
+ }
+ }
+ return Matrix.rows(rows, false)
+ when Matrix
+ return self * other.inverse
+ else
+ x, y = other.coerce(self)
+ return x / y
+ end
+ end
+
+ #
+ # Returns the inverse of the matrix.
+ # Matrix[[1, 2], [2, 1]].inverse
+ # => -1 1
+ # 0 -1
+ #
+ def inverse
+ Matrix.Raise ErrDimensionMismatch unless square?
+ Matrix.I(row_size).inverse_from(self)
+ end
+ alias inv inverse
+
+ #
+ # Not for public consumption?
+ #
+ def inverse_from(src)
+ size = row_size - 1
+ a = src.to_a
+
+ for k in 0..size
+ i = k
+ akk = a[k][k].abs
+ ((k+1)..size).each do |j|
+ v = a[j][k].abs
+ if v > akk
+ i = j
+ akk = v
+ end
+ end
+ Matrix.Raise ErrNotRegular if akk == 0
+ if i != k
+ a[i], a[k] = a[k], a[i]
+ @rows[i], @rows[k] = @rows[k], @rows[i]
+ end
+ akk = a[k][k]
+
+ for i in 0 .. size
+ next if i == k
+ q = a[i][k].quo(akk)
+ a[i][k] = 0
+
+ for j in (k + 1).. size
+ a[i][j] -= a[k][j] * q
+ end
+ for j in 0..size
+ @rows[i][j] -= @rows[k][j] * q
+ end
+ end
+
+ for j in (k + 1).. size
+ a[k][j] = a[k][j].quo(akk)
+ end
+ for j in 0..size
+ @rows[k][j] = @rows[k][j].quo(akk)
+ end
+ end
+ self
+ end
+ #alias reciprocal inverse
+
+ #
+ # Matrix exponentiation. Defined for integer powers only. Equivalent to
+ # multiplying the matrix by itself N times.
+ # Matrix[[7,6], [3,9]] ** 2
+ # => 67 96
+ # 48 99
+ #
+ def ** (other)
+ if other.kind_of?(Integer)
+ x = self
+ if other <= 0
+ x = self.inverse
+ return Matrix.identity(self.column_size) if other == 0
+ other = -other
+ end
+ z = x
+ n = other - 1
+ while n != 0
+ while (div, mod = n.divmod(2)
+ mod == 0)
+ x = x * x
+ n = div
+ end
+ z *= x
+ n -= 1
+ end
+ z
+ elsif other.kind_of?(Float) || defined?(Rational) && other.kind_of?(Rational)
+ Matrix.Raise ErrOperationNotDefined, "**"
+ else
+ Matrix.Raise ErrOperationNotDefined, "**"
+ end
+ end
+
+ #--
+ # MATRIX FUNCTIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Returns the determinant of the matrix. If the matrix is not square, the
+ # result is 0. This method's algorism is Gaussian elimination method
+ # and using Numeric#quo(). Beware that using Float values, with their
+ # usual lack of precision, can affect the value returned by this method. Use
+ # Rational values or Matrix#det_e instead if this is important to you.
+ #
+ # Matrix[[7,6], [3,9]].determinant
+ # => 63.0
+ #
+ def determinant
+ return 0 unless square?
+
+ size = row_size - 1
+ a = to_a
+
+ det = 1
+ k = 0
+ loop do
+ if (akk = a[k][k]) == 0
+ i = k
+ loop do
+ return 0 if (i += 1) > size
+ break unless a[i][k] == 0
+ end
+ a[i], a[k] = a[k], a[i]
+ akk = a[k][k]
+ det *= -1
+ end
+
+ for i in k + 1 .. size
+ q = a[i][k].quo(akk)
+ (k + 1).upto(size) do |j|
+ a[i][j] -= a[k][j] * q
+ end
+ end
+ det *= akk
+ break unless (k += 1) <= size
+ end
+ det
+ end
+ alias det determinant
+
+ #
+ # Returns the determinant of the matrix. If the matrix is not square, the
+ # result is 0. This method's algorism is Gaussian elimination method.
+ # This method uses Euclidean algorism. If all elements are integer,
+ # really exact value. But, if an element is a float, can't return
+ # exact value.
+ #
+ # Matrix[[7,6], [3,9]].determinant
+ # => 63
+ #
+ def determinant_e
+ return 0 unless square?
+
+ size = row_size - 1
+ a = to_a
+
+ det = 1
+ k = 0
+ loop do
+ if a[k][k].zero?
+ i = k
+ loop do
+ return 0 if (i += 1) > size
+ break unless a[i][k].zero?
+ end
+ a[i], a[k] = a[k], a[i]
+ det *= -1
+ end
+
+ for i in (k + 1)..size
+ q = a[i][k].quo(a[k][k])
+ k.upto(size) do |j|
+ a[i][j] -= a[k][j] * q
+ end
+ unless a[i][k].zero?
+ a[i], a[k] = a[k], a[i]
+ det *= -1
+ redo
+ end
+ end
+ det *= a[k][k]
+ break unless (k += 1) <= size
+ end
+ det
+ end
+ alias det_e determinant_e
+
+ #
+ # Returns the rank of the matrix. Beware that using Float values,
+ # probably return faild value. Use Rational values or Matrix#rank_e
+ # for getting exact result.
+ #
+ # Matrix[[7,6], [3,9]].rank
+ # => 2
+ #
+ def rank
+ if column_size > row_size
+ a = transpose.to_a
+ a_column_size = row_size
+ a_row_size = column_size
+ else
+ a = to_a
+ a_column_size = column_size
+ a_row_size = row_size
+ end
+ rank = 0
+ k = 0
+ begin
+ if (akk = a[k][k]) == 0
+ i = k
+ exists = true
+ loop do
+ if (i += 1) > a_row_size - 1
+ exists = false
+ break
+ end
+ break unless a[i][k] == 0
+ end
+ if exists
+ a[i], a[k] = a[k], a[i]
+ akk = a[k][k]
+ else
+ i = k
+ exists = true
+ loop do
+ if (i += 1) > a_column_size - 1
+ exists = false
+ break
+ end
+ break unless a[k][i] == 0
+ end
+ if exists
+ k.upto(a_row_size - 1) do |j|
+ a[j][k], a[j][i] = a[j][i], a[j][k]
+ end
+ akk = a[k][k]
+ else
+ next
+ end
+ end
+ end
+
+ for i in (k + 1)..(a_row_size - 1)
+ q = a[i][k].quo(akk)
+ for j in (k + 1)..(a_column_size - 1)
+ a[i][j] -= a[k][j] * q
+ end
+ end
+ rank += 1
+ end while (k += 1) <= a_column_size - 1
+ return rank
+ end
+
+ #
+ # Returns the rank of the matrix. This method uses Euclidean
+ # algorism. If all elements are integer, really exact value. But, if
+ # an element is a float, can't return exact value.
+ #
+ # Matrix[[7,6], [3,9]].rank
+ # => 2
+ #
+ def rank_e
+ a = to_a
+ a_column_size = column_size
+ a_row_size = row_size
+ pi = 0
+ (0 ... a_column_size).each do |j|
+ if i = (pi ... a_row_size).find{|i0| !a[i0][j].zero?}
+ if i != pi
+ a[pi], a[i] = a[i], a[pi]
+ end
+ (pi + 1 ... a_row_size).each do |k|
+ q = a[k][j].quo(a[pi][j])
+ (pi ... a_column_size).each do |j0|
+ a[k][j0] -= q * a[pi][j0]
+ end
+ if k > pi && !a[k][j].zero?
+ a[k], a[pi] = a[pi], a[k]
+ redo
+ end
+ end
+ pi += 1
+ end
+ end
+ pi
+ end
+
+
+ #
+ # Returns the trace (sum of diagonal elements) of the matrix.
+ # Matrix[[7,6], [3,9]].trace
+ # => 16
+ #
+ def trace
+ tr = 0
+ 0.upto(column_size - 1) do |i|
+ tr += @rows[i][i]
+ end
+ tr
+ end
+ alias tr trace
+
+ #
+ # Returns the transpose of the matrix.
+ # Matrix[[1,2], [3,4], [5,6]]
+ # => 1 2
+ # 3 4
+ # 5 6
+ # Matrix[[1,2], [3,4], [5,6]].transpose
+ # => 1 3 5
+ # 2 4 6
+ #
+ def transpose
+ Matrix.columns(@rows)
+ end
+ alias t transpose
+
+ #--
+ # CONVERTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # FIXME: describe #coerce.
+ #
+ def coerce(other)
+ case other
+ when Numeric
+ return Scalar.new(other), self
+ else
+ raise TypeError, "#{self.class} can't be coerced into #{other.class}"
+ end
+ end
+
+ #
+ # Returns an array of the row vectors of the matrix. See Vector.
+ #
+ def row_vectors
+ rows = (0 .. row_size - 1).collect {|i|
+ row(i)
+ }
+ rows
+ end
+
+ #
+ # Returns an array of the column vectors of the matrix. See Vector.
+ #
+ def column_vectors
+ columns = (0 .. column_size - 1).collect {|i|
+ column(i)
+ }
+ columns
+ end
+
+ #
+ # Returns an array of arrays that describe the rows of the matrix.
+ #
+ def to_a
+ @rows.collect{|row| row.collect{|e| e}}
+ end
+
+ def elements_to_f
+ collect{|e| e.to_f}
+ end
+
+ def elements_to_i
+ collect{|e| e.to_i}
+ end
+
+ def elements_to_r
+ collect{|e| e.to_r}
+ end
+
+ #--
+ # PRINTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Overrides Object#to_s
+ #
+ def to_s
+ "Matrix[" + @rows.collect{|row|
+ "[" + row.collect{|e| e.to_s}.join(", ") + "]"
+ }.join(", ")+"]"
+ end
+
+ #
+ # Overrides Object#inspect
+ #
+ def inspect
+ "Matrix"+@rows.inspect
+ end
+
+ # Private CLASS
+
+ class Scalar < Numeric # :nodoc:
+ include ExceptionForMatrix
+
+ def initialize(value)
+ @value = value
+ end
+
+ # ARITHMETIC
+ def +(other)
+ case other
+ when Numeric
+ Scalar.new(@value + other)
+ when Vector, Matrix
+ Scalar.Raise WrongArgType, other.class, "Numeric or Scalar"
+ when Scalar
+ Scalar.new(@value + other.value)
+ else
+ x, y = other.coerce(self)
+ x + y
+ end
+ end
+
+ def -(other)
+ case other
+ when Numeric
+ Scalar.new(@value - other)
+ when Vector, Matrix
+ Scalar.Raise WrongArgType, other.class, "Numeric or Scalar"
+ when Scalar
+ Scalar.new(@value - other.value)
+ else
+ x, y = other.coerce(self)
+ x - y
+ end
+ end
+
+ def *(other)
+ case other
+ when Numeric
+ Scalar.new(@value * other)
+ when Vector, Matrix
+ other.collect{|e| @value * e}
+ else
+ x, y = other.coerce(self)
+ x * y
+ end
+ end
+
+ def / (other)
+ case other
+ when Numeric
+ Scalar.new(@value / other)
+ when Vector
+ Scalar.Raise WrongArgType, other.class, "Numeric or Scalar or Matrix"
+ when Matrix
+ self * other.inverse
+ else
+ x, y = other.coerce(self)
+ x.quo(y)
+ end
+ end
+
+ def ** (other)
+ case other
+ when Numeric
+ Scalar.new(@value ** other)
+ when Vector
+ Scalar.Raise WrongArgType, other.class, "Numeric or Scalar or Matrix"
+ when Matrix
+ other.powered_by(self)
+ else
+ x, y = other.coerce(self)
+ x ** y
+ end
+ end
+ end
+end
+
+
+#
+# The +Vector+ class represents a mathematical vector, which is useful in its own right, and
+# also constitutes a row or column of a Matrix.
+#
+# == Method Catalogue
+#
+# To create a Vector:
+# * <tt> Vector.[](*array) </tt>
+# * <tt> Vector.elements(array, copy = true) </tt>
+#
+# To access elements:
+# * <tt> [](i) </tt>
+#
+# To enumerate the elements:
+# * <tt> #each2(v) </tt>
+# * <tt> #collect2(v) </tt>
+#
+# Vector arithmetic:
+# * <tt> *(x) "is matrix or number" </tt>
+# * <tt> +(v) </tt>
+# * <tt> -(v) </tt>
+#
+# Vector functions:
+# * <tt> #inner_product(v) </tt>
+# * <tt> #collect </tt>
+# * <tt> #map </tt>
+# * <tt> #map2(v) </tt>
+# * <tt> #r </tt>
+# * <tt> #size </tt>
+#
+# Conversion to other data types:
+# * <tt> #covector </tt>
+# * <tt> #to_a </tt>
+# * <tt> #coerce(other) </tt>
+#
+# String representations:
+# * <tt> #to_s </tt>
+# * <tt> #inspect </tt>
+#
+class Vector
+ include ExceptionForMatrix
+
+ #INSTANCE CREATION
+
+ private_class_method :new
+
+ #
+ # Creates a Vector from a list of elements.
+ # Vector[7, 4, ...]
+ #
+ def Vector.[](*array)
+ new(:init_elements, array, copy = false)
+ end
+
+ #
+ # Creates a vector from an Array. The optional second argument specifies
+ # whether the array itself or a copy is used internally.
+ #
+ def Vector.elements(array, copy = true)
+ new(:init_elements, array, copy)
+ end
+
+ #
+ # For internal use.
+ #
+ def initialize(method, array, copy)
+ self.send(method, array, copy)
+ end
+
+ #
+ # For internal use.
+ #
+ def init_elements(array, copy)
+ if copy
+ @elements = array.dup
+ else
+ @elements = array
+ end
+ end
+
+ # ACCESSING
+
+ #
+ # Returns element number +i+ (starting at zero) of the vector.
+ #
+ def [](i)
+ @elements[i]
+ end
+ alias element []
+ alias component []
+
+ def []=(i, v)
+ @elements[i]= v
+ end
+ alias set_element []=
+ alias set_component []=
+ private :[]=, :set_element, :set_component
+
+ #
+ # Returns the number of elements in the vector.
+ #
+ def size
+ @elements.size
+ end
+
+ #--
+ # ENUMERATIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Iterate over the elements of this vector and +v+ in conjunction.
+ #
+ def each2(v) # :yield: e1, e2
+ Vector.Raise ErrDimensionMismatch if size != v.size
+ 0.upto(size - 1) do |i|
+ yield @elements[i], v[i]
+ end
+ end
+
+ #
+ # Collects (as in Enumerable#collect) over the elements of this vector and +v+
+ # in conjunction.
+ #
+ def collect2(v) # :yield: e1, e2
+ Vector.Raise ErrDimensionMismatch if size != v.size
+ (0 .. size - 1).collect do |i|
+ yield @elements[i], v[i]
+ end
+ end
+
+ #--
+ # COMPARING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Returns +true+ iff the two vectors have the same elements in the same order.
+ #
+ def ==(other)
+ return false unless Vector === other
+
+ other.compare_by(@elements)
+ end
+ def eql?(other)
+ return false unless Vector === other
+
+ other.compare_by(@elements, :eql?)
+ end
+
+ #
+ # For internal use.
+ #
+ def compare_by(elements, comparison = :==)
+ @elements.send(comparison, elements)
+ end
+
+ #
+ # Return a copy of the vector.
+ #
+ def clone
+ Vector.elements(@elements)
+ end
+
+ #
+ # Return a hash-code for the vector.
+ #
+ def hash
+ @elements.hash
+ end
+
+ #--
+ # ARITHMETIC -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Multiplies the vector by +x+, where +x+ is a number or another vector.
+ #
+ def *(x)
+ case x
+ when Numeric
+ els = @elements.collect{|e| e * x}
+ Vector.elements(els, false)
+ when Matrix
+ Matrix.column_vector(self) * x
+ else
+ s, x = x.coerce(self)
+ s * x
+ end
+ end
+
+ #
+ # Vector addition.
+ #
+ def +(v)
+ case v
+ when Vector
+ Vector.Raise ErrDimensionMismatch if size != v.size
+ els = collect2(v) {|v1, v2|
+ v1 + v2
+ }
+ Vector.elements(els, false)
+ when Matrix
+ Matrix.column_vector(self) + v
+ else
+ s, x = v.coerce(self)
+ s + x
+ end
+ end
+
+ #
+ # Vector subtraction.
+ #
+ def -(v)
+ case v
+ when Vector
+ Vector.Raise ErrDimensionMismatch if size != v.size
+ els = collect2(v) {|v1, v2|
+ v1 - v2
+ }
+ Vector.elements(els, false)
+ when Matrix
+ Matrix.column_vector(self) - v
+ else
+ s, x = v.coerce(self)
+ s - x
+ end
+ end
+
+ #--
+ # VECTOR FUNCTIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Returns the inner product of this vector with the other.
+ # Vector[4,7].inner_product Vector[10,1] => 47
+ #
+ def inner_product(v)
+ Vector.Raise ErrDimensionMismatch if size != v.size
+
+ p = 0
+ each2(v) {|v1, v2|
+ p += v1 * v2
+ }
+ p
+ end
+
+ #
+ # Like Array#collect.
+ #
+ def collect # :yield: e
+ els = @elements.collect {|v|
+ yield v
+ }
+ Vector.elements(els, false)
+ end
+ alias map collect
+
+ #
+ # Like Vector#collect2, but returns a Vector instead of an Array.
+ #
+ def map2(v) # :yield: e1, e2
+ els = collect2(v) {|v1, v2|
+ yield v1, v2
+ }
+ Vector.elements(els, false)
+ end
+
+ #
+ # Returns the modulus (Pythagorean distance) of the vector.
+ # Vector[5,8,2].r => 9.643650761
+ #
+ def r
+ v = 0
+ for e in @elements
+ v += e*e
+ end
+ return Math.sqrt(v)
+ end
+
+ #--
+ # CONVERTING
+ #++
+
+ #
+ # Creates a single-row matrix from this vector.
+ #
+ def covector
+ Matrix.row_vector(self)
+ end
+
+ #
+ # Returns the elements of the vector in an array.
+ #
+ def to_a
+ @elements.dup
+ end
+
+ def elements_to_f
+ collect{|e| e.to_f}
+ end
+
+ def elements_to_i
+ collect{|e| e.to_i}
+ end
+
+ def elements_to_r
+ collect{|e| e.to_r}
+ end
+
+ #
+ # FIXME: describe Vector#coerce.
+ #
+ def coerce(other)
+ case other
+ when Numeric
+ return Matrix::Scalar.new(other), self
+ else
+ raise TypeError, "#{self.class} can't be coerced into #{other.class}"
+ end
+ end
+
+ #--
+ # PRINTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ #++
+
+ #
+ # Overrides Object#to_s
+ #
+ def to_s
+ "Vector[" + @elements.join(", ") + "]"
+ end
+
+ #
+ # Overrides Object#inspect
+ #
+ def inspect
+ str = "Vector"+@elements.inspect
+ end
+end
+
+
+# Documentation comments:
+# - Matrix#coerce and Vector#coerce need to be documented
diff --git a/ruby/lib/minitest/autorun.rb b/ruby/lib/minitest/autorun.rb
new file mode 100644
index 0000000..a9f9c67
--- /dev/null
+++ b/ruby/lib/minitest/autorun.rb
@@ -0,0 +1,9 @@
+############################################################
+# This file is imported from a different project.
+# DO NOT make modifications in this repo.
+# File a patch instead and assign it to Ryan Davis
+############################################################
+
+require 'minitest/unit'
+
+MiniTest::Unit.autorun
diff --git a/ruby/lib/minitest/mock.rb b/ruby/lib/minitest/mock.rb
new file mode 100644
index 0000000..54af28c
--- /dev/null
+++ b/ruby/lib/minitest/mock.rb
@@ -0,0 +1,37 @@
+############################################################
+# This file is imported from a different project.
+# DO NOT make modifications in this repo.
+# File a patch instead and assign it to Ryan Davis
+############################################################
+
+class MockExpectationError < StandardError; end
+
+module MiniTest
+ class Mock
+ def initialize
+ @expected_calls = {}
+ @actual_calls = Hash.new {|h,k| h[k] = [] }
+ end
+
+ def expect(name, retval, args=[])
+ n, r, a = name, retval, args # for the closure below
+ @expected_calls[name] = { :retval => retval, :args => args }
+ self.class.__send__(:define_method, name) { |*x|
+ raise ArgumentError unless @expected_calls[n][:args].size == x.size
+ @actual_calls[n] << { :retval => r, :args => x }
+ retval
+ }
+ self
+ end
+
+ def verify
+ @expected_calls.each_key do |name|
+ expected = @expected_calls[name]
+ msg = "expected #{name}, #{expected.inspect}"
+ raise MockExpectationError, msg unless
+ @actual_calls.has_key? name and @actual_calls[name].include?(expected)
+ end
+ true
+ end
+ end
+end
diff --git a/ruby/lib/minitest/spec.rb b/ruby/lib/minitest/spec.rb
new file mode 100644
index 0000000..2158ec0
--- /dev/null
+++ b/ruby/lib/minitest/spec.rb
@@ -0,0 +1,89 @@
+############################################################
+# This file is imported from a different project.
+# DO NOT make modifications in this repo.
+# File a patch instead and assign it to Ryan Davis
+############################################################
+
+#!/usr/bin/ruby -w
+
+require 'minitest/unit'
+
+class Module
+ def infect_with_assertions pos_prefix, neg_prefix, skip_re, map = {}
+ MiniTest::Assertions.public_instance_methods(false).each do |meth|
+ meth = meth.to_s
+
+ new_name = case meth
+ when /^assert/ then
+ meth.sub(/^assert/, pos_prefix.to_s)
+ when /^refute/ then
+ meth.sub(/^refute/, neg_prefix.to_s)
+ end
+ next unless new_name
+ next if new_name =~ skip_re
+
+ regexp, replacement = map.find { |re, _| new_name =~ re }
+ new_name.sub! regexp, replacement if replacement
+
+ # warn "%-22p -> %p %p" % [meth, new_name, regexp]
+ self.class_eval <<-EOM
+ def #{new_name} *args, &block
+ return MiniTest::Spec.current.#{meth}(*args, &self) if Proc === self
+ return MiniTest::Spec.current.#{meth}(args.first, self) if args.size == 1
+ return MiniTest::Spec.current.#{meth}(self, *args)
+ end
+ EOM
+ end
+ end
+end
+
+Object.infect_with_assertions(:must, :wont,
+ /^(must|wont)$|wont_(throw)|
+ must_(block|not?_|nothing|raise$)/x,
+ /(must_throw)s/ => '\1',
+ /(?!not)_same/ => '_be_same_as',
+ /_in_/ => '_be_within_',
+ /_operator/ => '_be',
+ /_includes/ => '_include',
+ /(must|wont)_(.*_of|nil|empty)/ => '\1_be_\2',
+ /must_raises/ => 'must_raise')
+
+class Object
+ alias :must_be_close_to :must_be_within_delta
+ alias :wont_be_close_to :wont_be_within_delta
+end
+
+module Kernel
+ def describe desc, &block
+ cls = Class.new(MiniTest::Spec)
+ Object.const_set desc.to_s.split(/\W+/).map { |s| s.capitalize }.join, cls
+
+ cls.class_eval(&block)
+ end
+ private :describe
+end
+
+class MiniTest::Spec < MiniTest::Unit::TestCase
+ def self.current
+ @@current_spec
+ end
+
+ def initialize name
+ super
+ @@current_spec = self
+ end
+
+ def self.before(type = :each, &block)
+ raise "unsupported before type: #{type}" unless type == :each
+ define_method :setup, &block
+ end
+
+ def self.after(type = :each, &block)
+ raise "unsupported after type: #{type}" unless type == :each
+ define_method :teardown, &block
+ end
+
+ def self.it desc, &block
+ define_method "test_#{desc.gsub(/\W+/, '_').downcase}", &block
+ end
+end
diff --git a/ruby/lib/minitest/unit.rb b/ruby/lib/minitest/unit.rb
new file mode 100644
index 0000000..0f71126
--- /dev/null
+++ b/ruby/lib/minitest/unit.rb
@@ -0,0 +1,497 @@
+############################################################
+# This file is imported from a different project.
+# DO NOT make modifications in this repo.
+# File a patch instead and assign it to Ryan Davis
+############################################################
+
+##
+#
+# Totally minimal drop-in replacement for test-unit
+#
+# TODO: refute -> debunk, prove/rebut, show/deny... lots of possibilities
+
+module MiniTest
+ class Assertion < Exception; end
+ class Skip < Assertion; end
+
+ file = if RUBY_VERSION =~ /^1\.9/ then # bt's expanded, but __FILE__ isn't :(
+ File.expand_path __FILE__
+ elsif __FILE__ =~ /^[^\.]/ then # assume both relative
+ require 'pathname'
+ pwd = Pathname.new Dir.pwd
+ pn = Pathname.new File.expand_path(__FILE__)
+ pn = File.join(".", pn.relative_path_from(pwd)) unless pn.relative?
+ pn.to_s
+ else # assume both are expanded
+ __FILE__
+ end
+
+ # './lib' in project dir, or '/usr/local/blahblah' if installed
+ MINI_DIR = File.dirname(File.dirname(file))
+
+ def self.filter_backtrace bt
+ return ["No backtrace"] unless bt
+
+ new_bt = []
+ bt.each do |line|
+ break if line.rindex(MINI_DIR, 0)
+ new_bt << line
+ end
+
+ new_bt = bt.reject { |line| line.rindex(MINI_DIR, 0) } if new_bt.empty?
+ new_bt = bt.dup if new_bt.empty?
+ new_bt
+ end
+
+ module Assertions
+ def mu_pp(obj)
+ s = obj.inspect
+ s = s.force_encoding(Encoding.default_external) if defined? Encoding
+ s
+ end
+
+ def _assertions= n
+ @_assertions = n
+ end
+
+ def _assertions
+ @_assertions ||= 0
+ end
+
+ def assert test, msg = nil
+ msg ||= "Failed assertion, no message given."
+ self._assertions += 1
+ unless test then
+ msg = msg.call if Proc === msg
+ raise MiniTest::Assertion, msg
+ end
+ true
+ end
+
+ def assert_block msg = nil
+ msg = message(msg) { "Expected block to return true value" }
+ assert yield, msg
+ end
+
+ def assert_empty obj, msg = nil
+ msg = message(msg) { "Expected #{obj.inspect} to be empty" }
+ assert_respond_to obj, :empty?
+ assert obj.empty?, msg
+ end
+
+ def assert_equal exp, act, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(exp)}, not #{mu_pp(act)}" }
+ assert(exp == act, msg)
+ end
+
+ def assert_in_delta exp, act, delta = 0.001, msg = nil
+ n = (exp - act).abs
+ msg = message(msg) { "Expected #{exp} - #{act} (#{n}) to be < #{delta}" }
+ assert delta >= n, msg
+ end
+
+ def assert_in_epsilon a, b, epsilon = 0.001, msg = nil
+ assert_in_delta a, b, [a, b].min * epsilon, msg
+ end
+
+ def assert_includes collection, obj, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}" }
+ assert_respond_to collection, :include?
+ assert collection.include?(obj), msg
+ end
+
+ def assert_instance_of cls, obj, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(obj)} to be an instance of #{cls}, not #{obj.class}" }
+ flip = (Module === obj) && ! (Module === cls) # HACK for specs
+ obj, cls = cls, obj if flip
+ assert obj.instance_of?(cls), msg
+ end
+
+ def assert_kind_of cls, obj, msg = nil # TODO: merge with instance_of
+ msg = message(msg) {
+ "Expected #{mu_pp(obj)} to be a kind of #{cls}, not #{obj.class}" }
+ flip = (Module === obj) && ! (Module === cls) # HACK for specs
+ obj, cls = cls, obj if flip
+ assert obj.kind_of?(cls), msg
+ end
+
+ def assert_match exp, act, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(exp)} to match #{mu_pp(act)}" }
+ assert_respond_to act, :"=~"
+ exp = /#{Regexp.escape(exp)}/ if String === exp && String === act
+ assert exp =~ act, msg
+ end
+
+ def assert_nil obj, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(obj)} to be nil" }
+ assert obj.nil?, msg
+ end
+
+ def assert_operator o1, op, o2, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op} #{mu_pp(o2)}" }
+ assert o1.__send__(op, o2), msg
+ end
+
+ def assert_raises *exp
+ msg = String === exp.last ? exp.pop : nil
+ should_raise = false
+ begin
+ yield
+ should_raise = true
+ rescue Exception => e
+ assert(exp.any? { |ex|
+ ex.instance_of?(Module) ? e.kind_of?(ex) : ex == e.class
+ }, exception_details(e, "#{mu_pp(exp)} exception expected, not"))
+
+ return e
+ end
+
+ exp = exp.first if exp.size == 1
+ flunk "#{mu_pp(exp)} expected but nothing was raised." if should_raise
+ end
+
+ def assert_respond_to obj, meth, msg = nil
+ msg = message(msg) {
+ "Expected #{mu_pp(obj)} (#{obj.class}) to respond to ##{meth}"
+ }
+ flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs
+ obj, meth = meth, obj if flip
+ assert obj.respond_to?(meth), msg
+ end
+
+ def assert_same exp, act, msg = nil
+ msg = message(msg) {
+ data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]
+ "Expected %s (0x%x) to be the same as %s (0x%x)" % data
+ }
+ assert exp.equal?(act), msg
+ end
+
+ def assert_send send_ary, m = nil
+ recv, msg, *args = send_ary
+ m = message(m) {
+ "Expected #{mu_pp(recv)}.#{msg}(*#{mu_pp(args)}) to return true" }
+ assert recv.__send__(msg, *args), m
+ end
+
+ def assert_throws sym, msg = nil
+ default = "Expected #{mu_pp(sym)} to have been thrown"
+ caught = true
+ catch(sym) do
+ begin
+ yield
+ rescue ArgumentError => e # 1.9 exception
+ default += ", not #{e.message.split(/ /).last}"
+ rescue NameError => e # 1.8 exception
+ default += ", not #{e.name.inspect}"
+ end
+ caught = false
+ end
+
+ assert caught, message(msg) { default }
+ end
+
+ def capture_io
+ require 'stringio'
+
+ orig_stdout, orig_stderr = $stdout, $stderr
+ captured_stdout, captured_stderr = StringIO.new, StringIO.new
+ $stdout, $stderr = captured_stdout, captured_stderr
+
+ yield
+
+ return captured_stdout.string, captured_stderr.string
+ ensure
+ $stdout = orig_stdout
+ $stderr = orig_stderr
+ end
+
+ def exception_details e, msg
+ "#{msg}\nClass: <#{e.class}>\nMessage: <#{e.message.inspect}>\n---Backtrace---\n#{MiniTest::filter_backtrace(e.backtrace).join("\n")}\n---------------"
+ end
+
+ def flunk msg = nil
+ msg ||= "Epic Fail!"
+ assert false, msg
+ end
+
+ def message msg = nil, &default
+ proc {
+ if msg then
+ msg = msg.to_s unless String === msg
+ msg += '.' unless msg.empty?
+ msg += "\n#{default.call}."
+ msg.strip
+ else
+ "#{default.call}."
+ end
+ }
+ end
+
+ # used for counting assertions
+ def pass msg = nil
+ assert true
+ end
+
+ def refute test, msg = nil
+ msg ||= "Failed refutation, no message given"
+ not assert(! test, msg)
+ end
+
+ def refute_empty obj, msg = nil
+ msg = message(msg) { "Expected #{obj.inspect} to not be empty" }
+ assert_respond_to obj, :empty?
+ refute obj.empty?, msg
+ end
+
+ def refute_equal exp, act, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}" }
+ refute exp == act, msg
+ end
+
+ def refute_in_delta exp, act, delta = 0.001, msg = nil
+ n = (exp - act).abs
+ msg = message(msg) { "Expected #{exp} - #{act} (#{n}) to not be < #{delta}" }
+ refute delta > n, msg
+ end
+
+ def refute_in_epsilon a, b, epsilon = 0.001, msg = nil
+ refute_in_delta a, b, a * epsilon, msg
+ end
+
+ def refute_includes collection, obj, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}" }
+ assert_respond_to collection, :include?
+ refute collection.include?(obj), msg
+ end
+
+ def refute_instance_of cls, obj, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not be an instance of #{cls}" }
+ flip = (Module === obj) && ! (Module === cls) # HACK for specs
+ obj, cls = cls, obj if flip
+ refute obj.instance_of?(cls), msg
+ end
+
+ def refute_kind_of cls, obj, msg = nil # TODO: merge with instance_of
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not be a kind of #{cls}" }
+ flip = (Module === obj) && ! (Module === cls) # HACK for specs
+ obj, cls = cls, obj if flip
+ refute obj.kind_of?(cls), msg
+ end
+
+ def refute_match exp, act, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(exp)} to not match #{mu_pp(act)}" }
+ assert_respond_to act, :"=~"
+ exp = /#{Regexp.escape(exp)}/ if String === exp && String === act
+ refute exp =~ act, msg
+ end
+
+ def refute_nil obj, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not be nil" }
+ refute obj.nil?, msg
+ end
+
+ def refute_operator o1, op, o2, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}" }
+ refute o1.__send__(op, o2), msg
+ end
+
+ def refute_respond_to obj, meth, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not respond to #{meth}" }
+ flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs
+ obj, meth = meth, obj if flip
+ refute obj.respond_to?(meth), msg
+ end
+
+ def refute_same exp, act, msg = nil
+ msg = message(msg) { "Expected #{mu_pp(act)} to not be the same as #{mu_pp(exp)}" }
+ refute exp.equal?(act), msg
+ end
+
+ def skip msg = nil, bt = caller
+ msg ||= "Skipped, no message given"
+ raise MiniTest::Skip, msg, bt
+ end
+ end
+
+ class Unit
+ VERSION = "1.3.1"
+
+ attr_accessor :report, :failures, :errors, :skips
+ attr_accessor :test_count, :assertion_count
+
+ @@installed_at_exit ||= false
+ @@out = $stdout
+
+ def self.autorun
+ at_exit {
+ next if $! # don't run if there was an exception
+ exit_code = MiniTest::Unit.new.run(ARGV)
+ exit false if exit_code && exit_code != 0
+ } unless @@installed_at_exit
+ @@installed_at_exit = true
+ end
+
+ def self.output= stream
+ @@out = stream
+ end
+
+ def location e
+ last_before_assertion = ""
+ e.backtrace.reverse_each do |s|
+ break if s =~ /in .(assert|refute|flunk|pass|fail|raise)/
+ last_before_assertion = s
+ end
+ last_before_assertion.sub(/:in .*$/, '')
+ end
+
+ def puke klass, meth, e
+ e = case e
+ when MiniTest::Skip then
+ @skips += 1
+ "Skipped:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
+ when MiniTest::Assertion then
+ @failures += 1
+ "Failure:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
+ else
+ @errors += 1
+ bt = MiniTest::filter_backtrace(e.backtrace).join("\n ")
+ "Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.message}\n #{bt}\n"
+ end
+ @report << e
+ e[0, 1]
+ end
+
+ def initialize
+ @report = []
+ @errors = @failures = @skips = 0
+ @verbose = false
+ end
+
+ ##
+ # Top level driver, controls all output and filtering.
+
+ def run args = []
+ @verbose = args.delete('-v')
+
+ filter = if args.first =~ /^(-n|--name)$/ then
+ args.shift
+ arg = args.shift
+ arg =~ /\/(.*)\// ? Regexp.new($1) : arg
+ else
+ /./ # anything - ^test_ already filtered by #tests
+ end
+
+ @@out.puts "Loaded suite #{$0.sub(/\.rb$/, '')}\nStarted"
+
+ start = Time.now
+ run_test_suites filter
+
+ @@out.puts
+ @@out.puts "Finished in #{'%.6f' % (Time.now - start)} seconds."
+
+ @report.each_with_index do |msg, i|
+ @@out.puts "\n%3d) %s" % [i + 1, msg]
+ end
+
+ @@out.puts
+
+ format = "%d tests, %d assertions, %d failures, %d errors, %d skips"
+ @@out.puts format % [test_count, assertion_count, failures, errors, skips]
+
+ return failures + errors if @test_count > 0 # or return nil...
+ end
+
+ def run_test_suites filter = /./
+ @test_count, @assertion_count = 0, 0
+ old_sync, @@out.sync = @@out.sync, true if @@out.respond_to? :sync=
+ TestCase.test_suites.each do |suite|
+ suite.test_methods.grep(filter).each do |test|
+ inst = suite.new test
+ inst._assertions = 0
+ @@out.print "#{suite}##{test}: " if @verbose
+
+ t = Time.now if @verbose
+ result = inst.run(self)
+
+ @@out.print "%.2f s: " % (Time.now - t) if @verbose
+ @@out.print result
+ @@out.puts if @verbose
+ @test_count += 1
+ @assertion_count += inst._assertions
+ end
+ end
+ @@out.sync = old_sync if @@out.respond_to? :sync=
+ [@test_count, @assertion_count]
+ end
+
+ class TestCase
+ attr_reader :name
+
+ def run runner
+ result = '.'
+ begin
+ @passed = nil
+ self.setup
+ self.__send__ self.name
+ @passed = true
+ rescue Exception => e
+ @passed = false
+ result = runner.puke(self.class, self.name, e)
+ ensure
+ begin
+ self.teardown
+ rescue Exception => e
+ result = runner.puke(self.class, self.name, e)
+ end
+ end
+ result
+ end
+
+ def initialize name
+ @name = name
+ @passed = nil
+ end
+
+ def self.reset
+ @@test_suites = {}
+ end
+
+ reset
+
+ def self.inherited klass
+ @@test_suites[klass] = true
+ end
+
+ def self.test_order
+ :random
+ end
+
+ def self.test_suites
+ @@test_suites.keys.sort_by { |ts| ts.name }
+ end
+
+ def self.test_methods
+ methods = public_instance_methods(true).grep(/^test/).map { |m|
+ m.to_s
+ }.sort
+
+ if self.test_order == :random then
+ max = methods.size
+ methods = methods.sort_by { rand(max) }
+ end
+
+ methods
+ end
+
+ def setup; end
+ def teardown; end
+
+ def passed?
+ @passed
+ end
+
+ include MiniTest::Assertions
+ end # class TestCase
+ end # class Test
+end # module Mini
diff --git a/ruby/lib/mkmf.rb b/ruby/lib/mkmf.rb
new file mode 100644
index 0000000..06aeca9
--- /dev/null
+++ b/ruby/lib/mkmf.rb
@@ -0,0 +1,1958 @@
+# module to create Makefile for extension modules
+# invoke like: ruby -r mkmf extconf.rb
+
+require 'rbconfig'
+require 'fileutils'
+require 'shellwords'
+
+CONFIG = RbConfig::MAKEFILE_CONFIG
+ORIG_LIBPATH = ENV['LIB']
+
+CXX_EXT = %w[cc cxx cpp]
+if /mswin|bccwin|mingw|os2/ !~ CONFIG['build_os']
+ CXX_EXT.concat(%w[C])
+end
+SRC_EXT = %w[c m] << CXX_EXT
+$static = nil
+$config_h = '$(arch_hdrdir)/ruby/config.h'
+$default_static = $static
+
+unless defined? $configure_args
+ $configure_args = {}
+ args = CONFIG["configure_args"]
+ if ENV["CONFIGURE_ARGS"]
+ args << " " << ENV["CONFIGURE_ARGS"]
+ end
+ for arg in Shellwords::shellwords(args)
+ arg, val = arg.split('=', 2)
+ next unless arg
+ arg.tr!('_', '-')
+ if arg.sub!(/^(?!--)/, '--')
+ val or next
+ arg.downcase!
+ end
+ next if /^--(?:top|topsrc|src|cur)dir$/ =~ arg
+ $configure_args[arg] = val || true
+ end
+ for arg in ARGV
+ arg, val = arg.split('=', 2)
+ next unless arg
+ arg.tr!('_', '-')
+ if arg.sub!(/^(?!--)/, '--')
+ val or next
+ arg.downcase!
+ end
+ $configure_args[arg] = val || true
+ end
+end
+
+$libdir = CONFIG["libdir"]
+$rubylibdir = CONFIG["rubylibdir"]
+$archdir = CONFIG["archdir"]
+$sitedir = CONFIG["sitedir"]
+$sitelibdir = CONFIG["sitelibdir"]
+$sitearchdir = CONFIG["sitearchdir"]
+$vendordir = CONFIG["vendordir"]
+$vendorlibdir = CONFIG["vendorlibdir"]
+$vendorarchdir = CONFIG["vendorarchdir"]
+
+$mswin = /mswin/ =~ RUBY_PLATFORM
+$bccwin = /bccwin/ =~ RUBY_PLATFORM
+$mingw = /mingw/ =~ RUBY_PLATFORM
+$cygwin = /cygwin/ =~ RUBY_PLATFORM
+$netbsd = /netbsd/ =~ RUBY_PLATFORM
+$os2 = /os2/ =~ RUBY_PLATFORM
+$beos = /beos/ =~ RUBY_PLATFORM
+$haiku = /haiku/ =~ RUBY_PLATFORM
+$solaris = /solaris/ =~ RUBY_PLATFORM
+$dest_prefix_pattern = (File::PATH_SEPARATOR == ';' ? /\A([[:alpha:]]:)?/ : /\A/)
+
+# :stopdoc:
+
+def config_string(key, config = CONFIG)
+ s = config[key] and !s.empty? and block_given? ? yield(s) : s
+end
+
+def dir_re(dir)
+ Regexp.new('\$(?:\('+dir+'\)|\{'+dir+'\})(?:\$(?:\(target_prefix\)|\{target_prefix\}))?')
+end
+
+def relative_from(path, base)
+ dir = File.join(path, "")
+ if File.expand_path(dir) == File.expand_path(dir, base)
+ path
+ else
+ File.join(base, path)
+ end
+end
+
+INSTALL_DIRS = [
+ [dir_re('commondir'), "$(RUBYCOMMONDIR)"],
+ [dir_re('sitedir'), "$(RUBYCOMMONDIR)"],
+ [dir_re('vendordir'), "$(RUBYCOMMONDIR)"],
+ [dir_re('rubylibdir'), "$(RUBYLIBDIR)"],
+ [dir_re('archdir'), "$(RUBYARCHDIR)"],
+ [dir_re('sitelibdir'), "$(RUBYLIBDIR)"],
+ [dir_re('vendorlibdir'), "$(RUBYLIBDIR)"],
+ [dir_re('sitearchdir'), "$(RUBYARCHDIR)"],
+ [dir_re('vendorarchdir'), "$(RUBYARCHDIR)"],
+ [dir_re('rubyhdrdir'), "$(RUBYHDRDIR)"],
+ [dir_re('sitehdrdir'), "$(SITEHDRDIR)"],
+ [dir_re('vendorhdrdir'), "$(VENDORHDRDIR)"],
+ [dir_re('bindir'), "$(BINDIR)"],
+]
+
+def install_dirs(target_prefix = nil)
+ if $extout
+ dirs = [
+ ['BINDIR', '$(extout)/bin'],
+ ['RUBYCOMMONDIR', '$(extout)/common'],
+ ['RUBYLIBDIR', '$(RUBYCOMMONDIR)$(target_prefix)'],
+ ['RUBYARCHDIR', '$(extout)/$(arch)$(target_prefix)'],
+ ['HDRDIR', '$(extout)/include/ruby$(target_prefix)'],
+ ['ARCHHDRDIR', '$(extout)/include/$(arch)/ruby$(target_prefix)'],
+ ['extout', "#$extout"],
+ ['extout_prefix', "#$extout_prefix"],
+ ]
+ elsif $extmk
+ dirs = [
+ ['BINDIR', '$(bindir)'],
+ ['RUBYCOMMONDIR', '$(rubylibdir)'],
+ ['RUBYLIBDIR', '$(rubylibdir)$(target_prefix)'],
+ ['RUBYARCHDIR', '$(archdir)$(target_prefix)'],
+ ['HDRDIR', '$(rubyhdrdir)/ruby$(target_prefix)'],
+ ['ARCHHDRDIR', '$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
+ ]
+ elsif $configure_args.has_key?('--vendor')
+ dirs = [
+ ['BINDIR', '$(bindir)'],
+ ['RUBYCOMMONDIR', '$(vendordir)$(target_prefix)'],
+ ['RUBYLIBDIR', '$(vendorlibdir)$(target_prefix)'],
+ ['RUBYARCHDIR', '$(vendorarchdir)$(target_prefix)'],
+ ['HDRDIR', '$(rubyhdrdir)/ruby$(target_prefix)'],
+ ['ARCHHDRDIR', '$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
+ ]
+ else
+ dirs = [
+ ['BINDIR', '$(bindir)'],
+ ['RUBYCOMMONDIR', '$(sitedir)$(target_prefix)'],
+ ['RUBYLIBDIR', '$(sitelibdir)$(target_prefix)'],
+ ['RUBYARCHDIR', '$(sitearchdir)$(target_prefix)'],
+ ['HDRDIR', '$(rubyhdrdir)/ruby$(target_prefix)'],
+ ['ARCHHDRDIR', '$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
+ ]
+ end
+ dirs << ['target_prefix', (target_prefix ? "/#{target_prefix}" : "")]
+ dirs
+end
+
+def map_dir(dir, map = nil)
+ map ||= INSTALL_DIRS
+ map.inject(dir) {|d, (orig, new)| d.gsub(orig, new)}
+end
+
+topdir = File.dirname(libdir = File.dirname(__FILE__))
+extdir = File.expand_path("ext", topdir)
+path = File.expand_path($0)
+$extmk = path[0, topdir.size+1] == topdir+"/" && %r"\A(ext|enc|tool)\z" =~ File.dirname(path[topdir.size+1..-1])
+if not $extmk and File.exist?(($hdrdir = RbConfig::CONFIG["rubyhdrdir"]) + "/ruby/ruby.h")
+ $topdir = $hdrdir
+ $top_srcdir = $hdrdir
+ $arch_hdrdir = $hdrdir + "/$(arch)"
+elsif File.exist?(($hdrdir = ($top_srcdir ||= topdir) + "/include") + "/ruby.h")
+ $topdir ||= RbConfig::CONFIG["topdir"]
+ $arch_hdrdir = "$(extout)/include/$(arch)"
+else
+ abort "mkmf.rb can't find header files for ruby at #{$hdrdir}/ruby.h"
+end
+
+OUTFLAG = CONFIG['OUTFLAG']
+COUTFLAG = CONFIG['COUTFLAG']
+CPPOUTFILE = CONFIG['CPPOUTFILE']
+
+CONFTEST_C = "conftest.c".freeze
+
+class String
+ # Wraps a string in escaped quotes if it contains whitespace.
+ def quote
+ /\s/ =~ self ? "\"#{self}\"" : "#{self}"
+ end
+
+ # Generates a string used as cpp macro name.
+ def tr_cpp
+ strip.upcase.tr_s("^A-Z0-9_", "_")
+ end
+end
+class Array
+ # Wraps all strings in escaped quotes if they contain whitespace.
+ def quote
+ map {|s| s.quote}
+ end
+end
+
+def rm_f(*files)
+ opt = ([files.pop] if Hash === files.last)
+ FileUtils.rm_f(Dir[*files.flatten(1)], *opt)
+end
+
+def rm_rf(*files)
+ opt = ([files.pop] if Hash === files.last)
+ FileUtils.rm_rf(Dir[*files.flatten(1)], *opt)
+end
+
+# Returns time stamp of the +target+ file if it exists and is newer
+# than or equal to all of +times+.
+def modified?(target, times)
+ (t = File.mtime(target)) rescue return nil
+ Array === times or times = [times]
+ t if times.all? {|n| n <= t}
+end
+
+def merge_libs(*libs)
+ libs.inject([]) do |x, y|
+ xy = x & y
+ xn = yn = 0
+ y = y.inject([]) {|ary, e| ary.last == e ? ary : ary << e}
+ y.each_with_index do |v, yi|
+ if xy.include?(v)
+ xi = [x.index(v), xn].max()
+ x[xi, 1] = y[yn..yi]
+ xn, yn = xi + (yi - yn + 1), yi + 1
+ end
+ end
+ x.concat(y[yn..-1] || [])
+ end
+end
+
+# This is a custom logging module. It generates an mkmf.log file when you
+# run your extconf.rb script. This can be useful for debugging unexpected
+# failures.
+#
+# This module and its associated methods are meant for internal use only.
+#
+module Logging
+ @log = nil
+ @logfile = 'mkmf.log'
+ @orgerr = $stderr.dup
+ @orgout = $stdout.dup
+ @postpone = 0
+ @quiet = $extmk
+
+ def self::log_open
+ @log ||= File::open(@logfile, 'wb')
+ @log.sync = true
+ end
+
+ def self::open
+ log_open
+ $stderr.reopen(@log)
+ $stdout.reopen(@log)
+ yield
+ ensure
+ $stderr.reopen(@orgerr)
+ $stdout.reopen(@orgout)
+ end
+
+ def self::message(*s)
+ log_open
+ @log.printf(*s)
+ end
+
+ def self::logfile file
+ @logfile = file
+ if @log and not @log.closed?
+ @log.flush
+ @log.close
+ @log = nil
+ end
+ end
+
+ def self::postpone
+ tmplog = "mkmftmp#{@postpone += 1}.log"
+ open do
+ log, *save = @log, @logfile, @orgout, @orgerr
+ @log, @logfile, @orgout, @orgerr = nil, tmplog, log, log
+ begin
+ log.print(open {yield})
+ @log.close
+ File::open(tmplog) {|t| FileUtils.copy_stream(t, log)}
+ ensure
+ @log, @logfile, @orgout, @orgerr = log, *save
+ @postpone -= 1
+ rm_f tmplog
+ end
+ end
+ end
+
+ class << self
+ attr_accessor :quiet
+ end
+end
+
+def xsystem command
+ varpat = /\$\((\w+)\)|\$\{(\w+)\}/
+ if varpat =~ command
+ vars = Hash.new {|h, k| h[k] = ''; ENV[k]}
+ command = command.dup
+ nil while command.gsub!(varpat) {vars[$1||$2]}
+ end
+ Logging::open do
+ puts command.quote
+ system(command)
+ end
+end
+
+def xpopen command, *mode, &block
+ Logging::open do
+ case mode[0]
+ when nil, /^r/
+ puts "#{command} |"
+ else
+ puts "| #{command}"
+ end
+ IO.popen(command, *mode, &block)
+ end
+end
+
+def log_src(src)
+ src = src.split(/^/)
+ fmt = "%#{src.size.to_s.size}d: %s"
+ Logging::message <<"EOM"
+checked program was:
+/* begin */
+EOM
+ src.each_with_index {|line, no| Logging::message fmt, no+1, line}
+ Logging::message <<"EOM"
+/* end */
+
+EOM
+end
+
+def create_tmpsrc(src)
+ src = "#{COMMON_HEADERS}\n#{src}"
+ src = yield(src) if block_given?
+ src.gsub!(/[ \t]+$/, '')
+ src.gsub!(/\A\n+|^\n+$/, '')
+ src.sub!(/[^\n]\z/, "\\&\n")
+ count = 0
+ begin
+ open(CONFTEST_C, "wb") do |cfile|
+ cfile.print src
+ end
+ rescue Errno::EACCES
+ if (count += 1) < 5
+ sleep 0.2
+ retry
+ end
+ end
+ src
+end
+
+def have_devel?
+ unless defined? $have_devel
+ $have_devel = true
+ $have_devel = try_link(MAIN_DOES_NOTHING)
+ end
+ $have_devel
+end
+
+def try_do(src, command, &b)
+ unless have_devel?
+ raise <<MSG
+The complier failed to generate an executable file.
+You have to install development tools first.
+MSG
+ end
+ src = create_tmpsrc(src, &b)
+ xsystem(command)
+ensure
+ log_src(src)
+ rm_rf 'conftest.dSYM'
+end
+
+def link_command(ldflags, opt="", libpath=$DEFLIBPATH|$LIBPATH)
+ conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote,
+ 'src' => CONFTEST_C,
+ 'arch_hdrdir' => "#$arch_hdrdir",
+ 'top_srcdir' => $top_srcdir.quote,
+ 'INCFLAGS' => "#$INCFLAGS",
+ 'CPPFLAGS' => "#$CPPFLAGS",
+ 'CFLAGS' => "#$CFLAGS",
+ 'ARCH_FLAG' => "#$ARCH_FLAG",
+ 'LDFLAGS' => "#$LDFLAGS #{ldflags}",
+ 'LIBPATH' => libpathflag(libpath),
+ 'LOCAL_LIBS' => "#$LOCAL_LIBS #$libs",
+ 'LIBS' => "#$LIBRUBYARG_STATIC #{opt} #$LIBS")
+ RbConfig::expand(TRY_LINK.dup, conf)
+end
+
+def cc_command(opt="")
+ conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote,
+ 'arch_hdrdir' => "#$arch_hdrdir",
+ 'top_srcdir' => $top_srcdir.quote)
+ RbConfig::expand("$(CC) #$INCFLAGS #$CPPFLAGS #$CFLAGS #$ARCH_FLAG #{opt} -c #{CONFTEST_C}",
+ conf)
+end
+
+def cpp_command(outfile, opt="")
+ conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote,
+ 'arch_hdrdir' => "#$arch_hdrdir",
+ 'top_srcdir' => $top_srcdir.quote)
+ RbConfig::expand("$(CPP) #$INCFLAGS #$CPPFLAGS #$CFLAGS #{opt} #{CONFTEST_C} #{outfile}",
+ conf)
+end
+
+def libpathflag(libpath=$DEFLIBPATH|$LIBPATH)
+ libpath.map{|x|
+ case x
+ when "$(topdir)", /\A\./
+ LIBPATHFLAG
+ else
+ LIBPATHFLAG+RPATHFLAG
+ end % x.quote
+ }.join
+end
+
+def try_link0(src, opt="", &b)
+ try_do(src, link_command("", opt), &b)
+end
+
+def try_link(src, opt="", &b)
+ try_link0(src, opt, &b)
+ensure
+ rm_f "conftest*", "c0x32*"
+end
+
+def try_compile(src, opt="", &b)
+ try_do(src, cc_command(opt), &b)
+ensure
+ rm_f "conftest*"
+end
+
+def try_cpp(src, opt="", &b)
+ try_do(src, cpp_command(CPPOUTFILE, opt), &b)
+ensure
+ rm_f "conftest*"
+end
+
+def cpp_include(header)
+ if header
+ header = [header] unless header.kind_of? Array
+ header.map {|h| "#include <#{h}>\n"}.join
+ else
+ ""
+ end
+end
+
+def with_cppflags(flags)
+ cppflags = $CPPFLAGS
+ $CPPFLAGS = flags
+ ret = yield
+ensure
+ $CPPFLAGS = cppflags unless ret
+end
+
+def with_cflags(flags)
+ cflags = $CFLAGS
+ $CFLAGS = flags
+ ret = yield
+ensure
+ $CFLAGS = cflags unless ret
+end
+
+def with_ldflags(flags)
+ ldflags = $LDFLAGS
+ $LDFLAGS = flags
+ ret = yield
+ensure
+ $LDFLAGS = ldflags unless ret
+end
+
+def try_static_assert(expr, headers = nil, opt = "", &b)
+ headers = cpp_include(headers)
+ try_compile(<<SRC, opt, &b)
+#{headers}
+/*top*/
+int conftest_const[(#{expr}) ? 1 : -1];
+SRC
+end
+
+def try_constant(const, headers = nil, opt = "", &b)
+ includes = cpp_include(headers)
+ if CROSS_COMPILING
+ if try_static_assert("#{const} > 0", headers, opt)
+ # positive constant
+ elsif try_static_assert("#{const} < 0", headers, opt)
+ neg = true
+ const = "-(#{const})"
+ elsif try_static_assert("#{const} == 0", headers, opt)
+ return 0
+ else
+ # not a constant
+ return nil
+ end
+ upper = 1
+ lower = 0
+ until try_static_assert("#{const} <= #{upper}", headers, opt)
+ lower = upper
+ upper <<= 1
+ end
+ return nil unless lower
+ while upper > lower + 1
+ mid = (upper + lower) / 2
+ if try_static_assert("#{const} > #{mid}", headers, opt)
+ lower = mid
+ else
+ upper = mid
+ end
+ end
+ upper = -upper if neg
+ return upper
+ else
+ src = %{#{includes}
+#include <stdio.h>
+/*top*/
+int conftest_const = (int)(#{const});
+int main() {printf("%d\\n", conftest_const); return 0;}
+}
+ if try_link0(src, opt, &b)
+ xpopen("./conftest") do |f|
+ return Integer(f.gets)
+ end
+ end
+ end
+ nil
+end
+
+def try_func(func, libs, headers = nil, &b)
+ headers = cpp_include(headers)
+ try_link(<<"SRC", libs, &b) or
+#{headers}
+/*top*/
+#{MAIN_DOES_NOTHING}
+int t() { void ((*volatile p)()); p = (void ((*)()))#{func}; return 0; }
+SRC
+ try_link(<<"SRC", libs, &b)
+#{headers}
+/*top*/
+#{MAIN_DOES_NOTHING}
+int t() { #{func}(); return 0; }
+SRC
+end
+
+def try_var(var, headers = nil, &b)
+ headers = cpp_include(headers)
+ try_compile(<<"SRC", &b)
+#{headers}
+/*top*/
+#{MAIN_DOES_NOTHING}
+int t() { const volatile void *volatile p; p = &(&#{var})[0]; return 0; }
+SRC
+end
+
+def egrep_cpp(pat, src, opt = "", &b)
+ src = create_tmpsrc(src, &b)
+ xpopen(cpp_command('', opt)) do |f|
+ if Regexp === pat
+ puts(" ruby -ne 'print if #{pat.inspect}'")
+ f.grep(pat) {|l|
+ puts "#{f.lineno}: #{l}"
+ return true
+ }
+ false
+ else
+ puts(" egrep '#{pat}'")
+ begin
+ stdin = $stdin.dup
+ $stdin.reopen(f)
+ system("egrep", pat)
+ ensure
+ $stdin.reopen(stdin)
+ end
+ end
+ end
+ensure
+ rm_f "conftest*"
+ log_src(src)
+end
+
+# This is used internally by the have_macro? method.
+def macro_defined?(macro, src, opt = "", &b)
+ src = src.sub(/[^\n]\z/, "\\&\n")
+ try_compile(src + <<"SRC", opt, &b)
+/*top*/
+#ifndef #{macro}
+# error
+>>>>>> #{macro} undefined <<<<<<
+#endif
+SRC
+end
+
+def try_run(src, opt = "", &b)
+ if try_link0(src, opt, &b)
+ xsystem("./conftest")
+ else
+ nil
+ end
+ensure
+ rm_f "conftest*"
+end
+
+def install_files(mfile, ifiles, map = nil, srcprefix = nil)
+ ifiles or return
+ ifiles.empty? and return
+ srcprefix ||= '$(srcdir)'
+ RbConfig::expand(srcdir = srcprefix.dup)
+ dirs = []
+ path = Hash.new {|h, i| h[i] = dirs.push([i])[-1]}
+ ifiles.each do |files, dir, prefix|
+ dir = map_dir(dir, map)
+ prefix &&= %r|\A#{Regexp.quote(prefix)}/?|
+ if /\A\.\// =~ files
+ # install files which are in current working directory.
+ files = files[2..-1]
+ len = nil
+ else
+ # install files which are under the $(srcdir).
+ files = File.join(srcdir, files)
+ len = srcdir.size
+ end
+ f = nil
+ Dir.glob(files) do |fx|
+ f = fx
+ f[0..len] = "" if len
+ case File.basename(f)
+ when *$NONINSTALLFILES
+ next
+ end
+ d = File.dirname(f)
+ d.sub!(prefix, "") if prefix
+ d = (d.empty? || d == ".") ? dir : File.join(dir, d)
+ f = File.join(srcprefix, f) if len
+ path[d] << f
+ end
+ unless len or f
+ d = File.dirname(files)
+ d.sub!(prefix, "") if prefix
+ d = (d.empty? || d == ".") ? dir : File.join(dir, d)
+ path[d] << files
+ end
+ end
+ dirs
+end
+
+def install_rb(mfile, dest, srcdir = nil)
+ install_files(mfile, [["lib/**/*.rb", dest, "lib"]], nil, srcdir)
+end
+
+def append_library(libs, lib) # :no-doc:
+ format(LIBARG, lib) + " " + libs
+end
+
+def message(*s)
+ unless Logging.quiet and not $VERBOSE
+ printf(*s)
+ $stdout.flush
+ end
+end
+
+# This emits a string to stdout that allows users to see the results of the
+# various have* and find* methods as they are tested.
+#
+# Internal use only.
+#
+def checking_for(m, fmt = nil)
+ f = caller[0][/in `(.*)'$/, 1] and f << ": " #` for vim #'
+ m = "checking #{/\Acheck/ =~ f ? '' : 'for '}#{m}... "
+ message "%s", m
+ a = r = nil
+ Logging::postpone do
+ r = yield
+ a = (fmt ? fmt % r : r ? "yes" : "no") << "\n"
+ "#{f}#{m}-------------------- #{a}\n"
+ end
+ message(a)
+ Logging::message "--------------------\n\n"
+ r
+end
+
+def checking_message(target, place = nil, opt = nil)
+ [["in", place], ["with", opt]].inject("#{target}") do |msg, (pre, noun)|
+ if noun
+ [[:to_str], [:join, ","], [:to_s]].each do |meth, *args|
+ if noun.respond_to?(meth)
+ break noun = noun.send(meth, *args)
+ end
+ end
+ msg << " #{pre} #{noun}" unless noun.empty?
+ end
+ msg
+ end
+end
+
+# :startdoc:
+
+# Returns whether or not +macro+ is defined either in the common header
+# files or within any +headers+ you provide.
+#
+# Any options you pass to +opt+ are passed along to the compiler.
+#
+def have_macro(macro, headers = nil, opt = "", &b)
+ checking_for checking_message(macro, headers, opt) do
+ macro_defined?(macro, cpp_include(headers), opt, &b)
+ end
+end
+
+# Returns whether or not the given entry point +func+ can be found within
+# +lib+. If +func+ is nil, the 'main()' entry point is used by default.
+# If found, it adds the library to list of libraries to be used when linking
+# your extension.
+#
+# If +headers+ are provided, it will include those header files as the
+# header files it looks in when searching for +func+.
+#
+# The real name of the library to be linked can be altered by
+# '--with-FOOlib' configuration option.
+#
+def have_library(lib, func = nil, headers = nil, &b)
+ func = "main" if !func or func.empty?
+ lib = with_config(lib+'lib', lib)
+ checking_for checking_message("#{func}()", LIBARG%lib) do
+ if COMMON_LIBS.include?(lib)
+ true
+ else
+ libs = append_library($libs, lib)
+ if try_func(func, libs, headers, &b)
+ $libs = libs
+ true
+ else
+ false
+ end
+ end
+ end
+end
+
+# Returns whether or not the entry point +func+ can be found within the library
+# +lib+ in one of the +paths+ specified, where +paths+ is an array of strings.
+# If +func+ is nil , then the main() function is used as the entry point.
+#
+# If +lib+ is found, then the path it was found on is added to the list of
+# library paths searched and linked against.
+#
+def find_library(lib, func, *paths, &b)
+ func = "main" if !func or func.empty?
+ lib = with_config(lib+'lib', lib)
+ paths = paths.collect {|path| path.split(File::PATH_SEPARATOR)}.flatten
+ checking_for "#{func}() in #{LIBARG%lib}" do
+ libpath = $LIBPATH
+ libs = append_library($libs, lib)
+ begin
+ until r = try_func(func, libs, &b) or paths.empty?
+ $LIBPATH = libpath | [paths.shift]
+ end
+ if r
+ $libs = libs
+ libpath = nil
+ end
+ ensure
+ $LIBPATH = libpath if libpath
+ end
+ r
+ end
+end
+
+# Returns whether or not the function +func+ can be found in the common
+# header files, or within any +headers+ that you provide. If found, a
+# macro is passed as a preprocessor constant to the compiler using the
+# function name, in uppercase, prepended with 'HAVE_'.
+#
+# For example, if have_func('foo') returned true, then the HAVE_FOO
+# preprocessor macro would be passed to the compiler.
+#
+def have_func(func, headers = nil, &b)
+ checking_for checking_message("#{func}()", headers) do
+ if try_func(func, $libs, headers, &b)
+ $defs.push(format("-DHAVE_%s", func.tr_cpp))
+ true
+ else
+ false
+ end
+ end
+end
+
+# Returns whether or not the variable +var+ can be found in the common
+# header files, or within any +headers+ that you provide. If found, a
+# macro is passed as a preprocessor constant to the compiler using the
+# variable name, in uppercase, prepended with 'HAVE_'.
+#
+# For example, if have_var('foo') returned true, then the HAVE_FOO
+# preprocessor macro would be passed to the compiler.
+#
+def have_var(var, headers = nil, &b)
+ checking_for checking_message(var, headers) do
+ if try_var(var, headers, &b)
+ $defs.push(format("-DHAVE_%s", var.tr_cpp))
+ true
+ else
+ false
+ end
+ end
+end
+
+# Returns whether or not the given +header+ file can be found on your system.
+# If found, a macro is passed as a preprocessor constant to the compiler using
+# the header file name, in uppercase, prepended with 'HAVE_'.
+#
+# For example, if have_header('foo.h') returned true, then the HAVE_FOO_H
+# preprocessor macro would be passed to the compiler.
+#
+def have_header(header, &b)
+ checking_for header do
+ if try_cpp(cpp_include(header), &b)
+ $defs.push(format("-DHAVE_%s", header.tr("a-z./\055", "A-Z___")))
+ true
+ else
+ false
+ end
+ end
+end
+
+# Instructs mkmf to search for the given +header+ in any of the +paths+
+# provided, and returns whether or not it was found in those paths.
+#
+# If the header is found then the path it was found on is added to the list
+# of included directories that are sent to the compiler (via the -I switch).
+#
+def find_header(header, *paths)
+ message = checking_message(header, paths)
+ header = cpp_include(header)
+ checking_for message do
+ if try_cpp(header)
+ true
+ else
+ found = false
+ paths.each do |dir|
+ opt = "-I#{dir}".quote
+ if try_cpp(header, opt)
+ $INCFLAGS << " " << opt
+ found = true
+ break
+ end
+ end
+ found
+ end
+ end
+end
+
+# Returns whether or not the struct of type +type+ contains +member+. If
+# it does not, or the struct type can't be found, then false is returned. You
+# may optionally specify additional +headers+ in which to look for the struct
+# (in addition to the common header files).
+#
+# If found, a macro is passed as a preprocessor constant to the compiler using
+# the type name and the member name, in uppercase, prepended with 'HAVE_'.
+#
+# For example, if have_struct_member('struct foo', 'bar') returned true, then the
+# HAVE_STRUCT_FOO_BAR preprocessor macro would be passed to the compiler.
+#
+# HAVE_ST_BAR is also defined for backward compatibility.
+#
+def have_struct_member(type, member, headers = nil, &b)
+ checking_for checking_message("#{type}.#{member}", headers) do
+ if try_compile(<<"SRC", &b)
+#{cpp_include(headers)}
+/*top*/
+#{MAIN_DOES_NOTHING}
+int s = (char *)&((#{type}*)0)->#{member} - (char *)0;
+SRC
+ $defs.push(format("-DHAVE_%s_%s", type.tr_cpp, member.tr_cpp))
+ $defs.push(format("-DHAVE_ST_%s", member.tr_cpp)) # backward compatibility
+ true
+ else
+ false
+ end
+ end
+end
+
+def try_type(type, headers = nil, opt = "", &b)
+ if try_compile(<<"SRC", opt, &b)
+#{cpp_include(headers)}
+/*top*/
+typedef #{type} conftest_type;
+int conftestval[sizeof(conftest_type)?1:-1];
+SRC
+ $defs.push(format("-DHAVE_TYPE_%s", type.tr_cpp))
+ true
+ else
+ false
+ end
+end
+
+# Returns whether or not the static type +type+ is defined. You may
+# optionally pass additional +headers+ to check against in addition to the
+# common header files.
+#
+# You may also pass additional flags to +opt+ which are then passed along to
+# the compiler.
+#
+# If found, a macro is passed as a preprocessor constant to the compiler using
+# the type name, in uppercase, prepended with 'HAVE_TYPE_'.
+#
+# For example, if have_type('foo') returned true, then the HAVE_TYPE_FOO
+# preprocessor macro would be passed to the compiler.
+#
+def have_type(type, headers = nil, opt = "", &b)
+ checking_for checking_message(type, headers, opt) do
+ try_type(type, headers, opt, &b)
+ end
+end
+
+# Returns where the static type +type+ is defined.
+#
+# You may also pass additional flags to +opt+ which are then passed along to
+# the compiler.
+#
+# See also +have_type+.
+#
+def find_type(type, opt, *headers, &b)
+ opt ||= ""
+ fmt = "not found"
+ def fmt.%(x)
+ x ? x.respond_to?(:join) ? x.join(",") : x : self
+ end
+ checking_for checking_message(type, nil, opt), fmt do
+ headers.find do |h|
+ try_type(type, h, opt, &b)
+ end
+ end
+end
+
+def try_const(const, headers = nil, opt = "", &b)
+ const, type = *const
+ if try_compile(<<"SRC", opt, &b)
+#{cpp_include(headers)}
+/*top*/
+typedef #{type || 'int'} conftest_type;
+conftest_type conftestval = #{type ? '' : '(int)'}#{const};
+SRC
+ $defs.push(format("-DHAVE_CONST_%s", const.tr_cpp))
+ true
+ else
+ false
+ end
+end
+
+# Returns whether or not the constant +const+ is defined. You may
+# optionally pass the +type+ of +const+ as <code>[const, type]</code>,
+# like as:
+#
+# have_const(%w[PTHREAD_MUTEX_INITIALIZER pthread_mutex_t], "pthread.h")
+#
+# You may also pass additional +headers+ to check against in addition
+# to the common header files, and additional flags to +opt+ which are
+# then passed along to the compiler.
+#
+# If found, a macro is passed as a preprocessor constant to the compiler using
+# the type name, in uppercase, prepended with 'HAVE_CONST_'.
+#
+# For example, if have_const('foo') returned true, then the HAVE_CONST_FOO
+# preprocessor macro would be passed to the compiler.
+#
+def have_const(const, headers = nil, opt = "", &b)
+ checking_for checking_message([*const].compact.join(' '), headers, opt) do
+ try_const(const, headers, opt, &b)
+ end
+end
+
+# Returns the size of the given +type+. You may optionally specify additional
+# +headers+ to search in for the +type+.
+#
+# If found, a macro is passed as a preprocessor constant to the compiler using
+# the type name, in uppercase, prepended with 'SIZEOF_', followed by the type
+# name, followed by '=X' where 'X' is the actual size.
+#
+# For example, if check_sizeof('mystruct') returned 12, then the
+# SIZEOF_MYSTRUCT=12 preprocessor macro would be passed to the compiler.
+#
+def check_sizeof(type, headers = nil, &b)
+ expr = "sizeof(#{type})"
+ fmt = "%d"
+ def fmt.%(x)
+ x ? super : "failed"
+ end
+ checking_for checking_message("size of #{type}", headers), fmt do
+ if size = try_constant(expr, headers, &b)
+ $defs.push(format("-DSIZEOF_%s=%d", type.tr_cpp, size))
+ size
+ end
+ end
+end
+
+# :stopdoc:
+
+# Used internally by the what_type? method to determine if +type+ is a scalar
+# pointer.
+def scalar_ptr_type?(type, member = nil, headers = nil, &b)
+ try_compile(<<"SRC", &b) # pointer
+#{cpp_include(headers)}
+/*top*/
+volatile #{type} conftestval;
+#{MAIN_DOES_NOTHING}
+int t() {return (int)(1-*(conftestval#{member ? ".#{member}" : ""}));}
+SRC
+end
+
+# Used internally by the what_type? method to determine if +type+ is a scalar
+# pointer.
+def scalar_type?(type, member = nil, headers = nil, &b)
+ try_compile(<<"SRC", &b) # pointer
+#{cpp_include(headers)}
+/*top*/
+volatile #{type} conftestval;
+#{MAIN_DOES_NOTHING}
+int t() {return (int)(1-(conftestval#{member ? ".#{member}" : ""}));}
+SRC
+end
+
+def what_type?(type, member = nil, headers = nil, &b)
+ m = "#{type}"
+ name = type
+ if member
+ m << "." << member
+ name = "(((#{type} *)0)->#{member})"
+ end
+ fmt = "seems %s"
+ def fmt.%(x)
+ x ? super : "unknown"
+ end
+ checking_for checking_message(m, headers), fmt do
+ if scalar_ptr_type?(type, member, headers, &b)
+ if try_static_assert("sizeof(*#{name}) == 1", headers)
+ "string"
+ end
+ elsif scalar_type?(type, member, headers, &b)
+ if try_static_assert("sizeof(#{name}) > sizeof(long)", headers)
+ "long long"
+ elsif try_static_assert("sizeof(#{name}) > sizeof(int)", headers)
+ "long"
+ elsif try_static_assert("sizeof(#{name}) > sizeof(short)", headers)
+ "int"
+ elsif try_static_assert("sizeof(#{name}) > 1", headers)
+ "short"
+ else
+ "char"
+ end
+ end
+ end
+end
+
+# This method is used internally by the find_executable method.
+#
+# Internal use only.
+#
+def find_executable0(bin, path = nil)
+ ext = config_string('EXEEXT')
+ if File.expand_path(bin) == bin
+ return bin if File.executable?(bin)
+ ext and File.executable?(file = bin + ext) and return file
+ return nil
+ end
+ if path ||= ENV['PATH']
+ path = path.split(File::PATH_SEPARATOR)
+ else
+ path = %w[/usr/local/bin /usr/ucb /usr/bin /bin]
+ end
+ file = nil
+ path.each do |dir|
+ return file if File.executable?(file = File.join(dir, bin))
+ return file if ext and File.executable?(file << ext)
+ end
+ nil
+end
+
+# :startdoc:
+
+# Searches for the executable +bin+ on +path+. The default path is your
+# PATH environment variable. If that isn't defined, it will resort to
+# searching /usr/local/bin, /usr/ucb, /usr/bin and /bin.
+#
+# If found, it will return the full path, including the executable name,
+# of where it was found.
+#
+# Note that this method does not actually affect the generated Makefile.
+#
+def find_executable(bin, path = nil)
+ checking_for checking_message(bin, path) do
+ find_executable0(bin, path)
+ end
+end
+
+# :stopdoc:
+
+def arg_config(config, default=nil, &block)
+ $arg_config << [config, default]
+ defaults = []
+ if default
+ defaults << default
+ elsif !block
+ defaults << nil
+ end
+ $configure_args.fetch(config.tr('_', '-'), *defaults, &block)
+end
+
+# :startdoc:
+
+# Tests for the presence of a --with-<tt>config</tt> or --without-<tt>config</tt>
+# option. Returns true if the with option is given, false if the without
+# option is given, and the default value otherwise.
+#
+# This can be useful for adding custom definitions, such as debug information.
+#
+# Example:
+#
+# if with_config("debug")
+# $defs.push("-DOSSL_DEBUG") unless $defs.include? "-DOSSL_DEBUG"
+# end
+#
+def with_config(config, default=nil)
+ config = config.sub(/^--with[-_]/, '')
+ val = arg_config("--with-"+config) do
+ if arg_config("--without-"+config)
+ false
+ elsif block_given?
+ yield(config, default)
+ else
+ break default
+ end
+ end
+ case val
+ when "yes"
+ true
+ when "no"
+ false
+ else
+ val
+ end
+end
+
+# Tests for the presence of an --enable-<tt>config</tt> or
+# --disable-<tt>config</tt> option. Returns true if the enable option is given,
+# false if the disable option is given, and the default value otherwise.
+#
+# This can be useful for adding custom definitions, such as debug information.
+#
+# Example:
+#
+# if enable_config("debug")
+# $defs.push("-DOSSL_DEBUG") unless $defs.include? "-DOSSL_DEBUG"
+# end
+#
+def enable_config(config, default=nil)
+ if arg_config("--enable-"+config)
+ true
+ elsif arg_config("--disable-"+config)
+ false
+ elsif block_given?
+ yield(config, default)
+ else
+ return default
+ end
+end
+
+# Generates a header file consisting of the various macro definitions generated
+# by other methods such as have_func and have_header. These are then wrapped in
+# a custom #ifndef based on the +header+ file name, which defaults to
+# 'extconf.h'.
+#
+# For example:
+#
+# # extconf.rb
+# require 'mkmf'
+# have_func('realpath')
+# have_header('sys/utime.h')
+# create_header
+# create_makefile('foo')
+#
+# The above script would generate the following extconf.h file:
+#
+# #ifndef EXTCONF_H
+# #define EXTCONF_H
+# #define HAVE_REALPATH 1
+# #define HAVE_SYS_UTIME_H 1
+# #endif
+#
+# Given that the create_header method generates a file based on definitions
+# set earlier in your extconf.rb file, you will probably want to make this
+# one of the last methods you call in your script.
+#
+def create_header(header = "extconf.h")
+ message "creating %s\n", header
+ sym = header.tr("a-z./\055", "A-Z___")
+ hdr = ["#ifndef #{sym}\n#define #{sym}\n"]
+ for line in $defs
+ case line
+ when /^-D([^=]+)(?:=(.*))?/
+ hdr << "#define #$1 #{$2 ? Shellwords.shellwords($2)[0] : 1}\n"
+ when /^-U(.*)/
+ hdr << "#undef #$1\n"
+ end
+ end
+ hdr << "#endif\n"
+ hdr = hdr.join
+ unless (IO.read(header) == hdr rescue false)
+ open(header, "w") do |hfile|
+ hfile.write(hdr)
+ end
+ end
+ $extconf_h = header
+end
+
+# Sets a +target+ name that the user can then use to configure various 'with'
+# options with on the command line by using that name. For example, if the
+# target is set to "foo", then the user could use the --with-foo-dir command
+# line option.
+#
+# You may pass along additional 'include' or 'lib' defaults via the +idefault+
+# and +ldefault+ parameters, respectively.
+#
+# Note that dir_config only adds to the list of places to search for libraries
+# and include files. It does not link the libraries into your application.
+#
+def dir_config(target, idefault=nil, ldefault=nil)
+ if dir = with_config(target + "-dir", (idefault unless ldefault))
+ defaults = Array === dir ? dir : dir.split(File::PATH_SEPARATOR)
+ idefault = ldefault = nil
+ end
+
+ idir = with_config(target + "-include", idefault)
+ $arg_config.last[1] ||= "${#{target}-dir}/include"
+ ldir = with_config(target + "-lib", ldefault)
+ $arg_config.last[1] ||= "${#{target}-dir}/lib"
+
+ idirs = idir ? Array === idir ? idir : idir.split(File::PATH_SEPARATOR) : []
+ if defaults
+ idirs.concat(defaults.collect {|d| d + "/include"})
+ idir = ([idir] + idirs).compact.join(File::PATH_SEPARATOR)
+ end
+ unless idirs.empty?
+ idirs.collect! {|d| "-I" + d}
+ idirs -= Shellwords.shellwords($CPPFLAGS)
+ unless idirs.empty?
+ $CPPFLAGS = (idirs.quote << $CPPFLAGS).join(" ")
+ end
+ end
+
+ ldirs = ldir ? Array === ldir ? ldir : ldir.split(File::PATH_SEPARATOR) : []
+ if defaults
+ ldirs.concat(defaults.collect {|d| d + "/lib"})
+ ldir = ([ldir] + ldirs).compact.join(File::PATH_SEPARATOR)
+ end
+ $LIBPATH = ldirs | $LIBPATH
+
+ [idir, ldir]
+end
+
+# :stopdoc:
+
+# Handles meta information about installed libraries. Uses your platform's
+# pkg-config program if it has one.
+def pkg_config(pkg)
+ if pkgconfig = with_config("#{pkg}-config") and find_executable0(pkgconfig)
+ # iff package specific config command is given
+ get = proc {|opt| `#{pkgconfig} --#{opt}`.chomp}
+ elsif ($PKGCONFIG ||=
+ (pkgconfig = with_config("pkg-config", ("pkg-config" unless CROSS_COMPILING))) &&
+ find_executable0(pkgconfig) && pkgconfig) and
+ system("#{$PKGCONFIG} --exists #{pkg}")
+ # default to pkg-config command
+ get = proc {|opt| `#{$PKGCONFIG} --#{opt} #{pkg}`.chomp}
+ elsif find_executable0(pkgconfig = "#{pkg}-config")
+ # default to package specific config command, as a last resort.
+ get = proc {|opt| `#{pkgconfig} --#{opt}`.chomp}
+ end
+ if get
+ cflags = get['cflags']
+ ldflags = get['libs']
+ libs = get['libs-only-l']
+ ldflags = (Shellwords.shellwords(ldflags) - Shellwords.shellwords(libs)).quote.join(" ")
+ $CFLAGS += " " << cflags
+ $LDFLAGS += " " << ldflags
+ $libs += " " << libs
+ Logging::message "package configuration for %s\n", pkg
+ Logging::message "cflags: %s\nldflags: %s\nlibs: %s\n\n",
+ cflags, ldflags, libs
+ [cflags, ldflags, libs]
+ else
+ Logging::message "package configuration for %s is not found\n", pkg
+ nil
+ end
+end
+
+def with_destdir(dir)
+ dir = dir.sub($dest_prefix_pattern, '')
+ /\A\$[\(\{]/ =~ dir ? dir : "$(DESTDIR)"+dir
+end
+
+# Converts forward slashes to backslashes. Aimed at MS Windows.
+#
+# Internal use only.
+#
+def winsep(s)
+ s.tr('/', '\\')
+end
+
+# Converts native path to format acceptable in Makefile
+#
+# Internal use only.
+#
+if !CROSS_COMPILING
+ case CONFIG['build_os']
+ when 'mingw32'
+ def mkintpath(path)
+ # mingw uses make from msys and it needs special care
+ # converts from C:\some\path to /C/some/path
+ path = path.dup
+ path.tr!('\\', '/')
+ path.sub!(/\A([A-Za-z]):(?=\/)/, '/\1')
+ path
+ end
+ end
+end
+unless defined?(mkintpath)
+ def mkintpath(path)
+ path
+ end
+end
+
+def configuration(srcdir)
+ mk = []
+ vpath = $VPATH.dup
+ if !CROSS_COMPILING
+ case CONFIG['build_os']
+ when 'cygwin'
+ if CONFIG['target_os'] != 'cygwin'
+ vpath = vpath.map {|p| p.sub(/.*/, '$(shell cygpath -u \&)')}
+ end
+ end
+ end
+ CONFIG["hdrdir"] ||= $hdrdir
+ mk << %{
+SHELL = /bin/sh
+
+#### Start of system configuration section. ####
+#{"top_srcdir = " + $top_srcdir.sub(%r"\A#{Regexp.quote($topdir)}/", "$(topdir)/") if $extmk}
+srcdir = #{srcdir.gsub(/\$\((srcdir)\)|\$\{(srcdir)\}/) {mkintpath(CONFIG[$1||$2])}.quote}
+topdir = #{mkintpath($extmk ? CONFIG["topdir"] : $topdir).quote}
+hdrdir = #{mkintpath(CONFIG["hdrdir"]).quote}
+arch_hdrdir = #{$arch_hdrdir}
+VPATH = #{vpath.join(CONFIG['PATH_SEPARATOR'])}
+}
+ if $extmk
+ mk << "RUBYLIB = -\nRUBYOPT = -r$(top_srcdir)/ext/purelib.rb\n"
+ end
+ if destdir = CONFIG["prefix"][$dest_prefix_pattern, 1]
+ mk << "\nDESTDIR = #{destdir}\n"
+ end
+ CONFIG.each do |key, var|
+ next unless /prefix$/ =~ key
+ mk << "#{key} = #{with_destdir(var)}\n"
+ end
+ CONFIG.each do |key, var|
+ next if /^abs_/ =~ key
+ next if /^(?:src|top|hdr)dir$/ =~ key
+ next unless /dir$/ =~ key
+ mk << "#{key} = #{with_destdir(var)}\n"
+ end
+ if !$extmk and !$configure_args.has_key?('--ruby') and
+ sep = config_string('BUILD_FILE_SEPARATOR')
+ sep = ":/=#{sep}"
+ else
+ sep = ""
+ end
+ possible_command = (proc {|s| s if /top_srcdir/ !~ s} unless $extmk)
+ extconf_h = $extconf_h ? "-DRUBY_EXTCONF_H=\\\"$(RUBY_EXTCONF_H)\\\" " : $defs.join(" ") << " "
+ mk << %{
+CC = #{CONFIG['CC']}
+CXX = #{CONFIG['CXX']}
+LIBRUBY = #{CONFIG['LIBRUBY']}
+LIBRUBY_A = #{CONFIG['LIBRUBY_A']}
+LIBRUBYARG_SHARED = #$LIBRUBYARG_SHARED
+LIBRUBYARG_STATIC = #$LIBRUBYARG_STATIC
+OUTFLAG = #{OUTFLAG}
+COUTFLAG = #{COUTFLAG}
+
+RUBY_EXTCONF_H = #{$extconf_h}
+cflags = #{CONFIG['cflags']}
+optflags = #{CONFIG['optflags']}
+debugflags = #{CONFIG['debugflags']}
+warnflags = #{CONFIG['warnflags']}
+CFLAGS = #{$static ? '' : CONFIG['CCDLFLAGS']} #$CFLAGS #$ARCH_FLAG
+INCFLAGS = -I. #$INCFLAGS
+DEFS = #{CONFIG['DEFS']}
+CPPFLAGS = #{extconf_h}#{$CPPFLAGS}
+CXXFLAGS = $(CFLAGS) #{CONFIG['CXXFLAGS']}
+ldflags = #{$LDFLAGS}
+dldflags = #{$DLDFLAGS}
+archflag = #{$ARCH_FLAG}
+DLDFLAGS = $(ldflags) $(dldflags) $(archflag)
+LDSHARED = #{CONFIG['LDSHARED']}
+LDSHAREDXX = #{config_string('LDSHAREDXX') || '$(LDSHARED)'}
+AR = #{CONFIG['AR']}
+EXEEXT = #{CONFIG['EXEEXT']}
+
+RUBY_INSTALL_NAME = #{CONFIG['RUBY_INSTALL_NAME']}
+RUBY_SO_NAME = #{CONFIG['RUBY_SO_NAME']}
+arch = #{CONFIG['arch']}
+sitearch = #{CONFIG['sitearch']}
+ruby_version = #{RbConfig::CONFIG['ruby_version']}
+ruby = #{$ruby}
+RUBY = $(ruby#{sep})
+RM = #{config_string('RM', &possible_command) || '$(RUBY) -run -e rm -- -f'}
+RM_RF = #{'$(RUBY) -run -e rm -- -rf'}
+RMDIRS = #{config_string('RMDIRS', &possible_command) || '$(RUBY) -run -e rmdir -- -p'}
+MAKEDIRS = #{config_string('MAKEDIRS', &possible_command) || '@$(RUBY) -run -e mkdir -- -p'}
+INSTALL = #{config_string('INSTALL', &possible_command) || '@$(RUBY) -run -e install -- -vp'}
+INSTALL_PROG = #{config_string('INSTALL_PROG') || '$(INSTALL) -m 0755'}
+INSTALL_DATA = #{config_string('INSTALL_DATA') || '$(INSTALL) -m 0644'}
+COPY = #{config_string('CP', &possible_command) || '@$(RUBY) -run -e cp -- -v'}
+
+#### End of system configuration section. ####
+
+preload = #{defined?($preload) && $preload ? $preload.join(' ') : ''}
+}
+ if $nmake == ?b
+ mk.each do |x|
+ x.gsub!(/^(MAKEDIRS|INSTALL_(?:PROG|DATA))+\s*=.*\n/) do
+ "!ifndef " + $1 + "\n" +
+ $& +
+ "!endif\n"
+ end
+ end
+ end
+ mk
+end
+# :startdoc:
+
+def dummy_makefile(srcdir)
+ configuration(srcdir) << <<RULES << CLEANINGS
+CLEANFILES = #{$cleanfiles.join(' ')}
+DISTCLEANFILES = #{$distcleanfiles.join(' ')}
+
+all install static install-so install-rb: Makefile
+
+RULES
+end
+
+def depend_rules(depend)
+ suffixes = []
+ depout = []
+ cont = implicit = nil
+ impconv = proc do
+ COMPILE_RULES.each {|rule| depout << (rule % implicit[0]) << implicit[1]}
+ implicit = nil
+ end
+ ruleconv = proc do |line|
+ if implicit
+ if /\A\t/ =~ line
+ implicit[1] << line
+ next
+ else
+ impconv[]
+ end
+ end
+ if m = /\A\.(\w+)\.(\w+)(?:\s*:)/.match(line)
+ suffixes << m[1] << m[2]
+ implicit = [[m[1], m[2]], [m.post_match]]
+ next
+ elsif RULE_SUBST and /\A(?!\s*\w+\s*=)[$\w][^#]*:/ =~ line
+ line.gsub!(%r"(\s)(?!\.)([^$(){}+=:\s\/\\,]+)(?=\s|\z)") {$1 + RULE_SUBST % $2}
+ end
+ depout << line
+ end
+ depend.each_line do |line|
+ line.gsub!(/\.o\b/, ".#{$OBJEXT}")
+ line.gsub!(/\$\((?:hdr|top)dir\)\/config.h/, $config_h)
+ line.gsub!(%r"\$\(hdrdir\)/(?!ruby(?![^:;/\s]))(?=[-\w]+\.h)", '\&ruby/')
+ if $nmake && /\A\s*\$\(RM|COPY\)/ =~ line
+ line.gsub!(%r"[-\w\./]{2,}"){$&.tr("/", "\\")}
+ line.gsub!(/(\$\((?!RM|COPY)[^:)]+)(?=\))/, '\1:/=\\')
+ end
+ if /(?:^|[^\\])(?:\\\\)*\\$/ =~ line
+ (cont ||= []) << line
+ next
+ elsif cont
+ line = (cont << line).join
+ cont = nil
+ end
+ ruleconv.call(line)
+ end
+ if cont
+ ruleconv.call(cont.join)
+ elsif implicit
+ impconv.call
+ end
+ unless suffixes.empty?
+ depout.unshift(".SUFFIXES: ." + suffixes.uniq.join(" .") + "\n\n")
+ end
+ depout.unshift("$(OBJS): $(RUBY_EXTCONF_H)\n\n") if $extconf_h
+ depout.flatten!
+ depout
+end
+
+# Generates the Makefile for your extension, passing along any options and
+# preprocessor constants that you may have generated through other methods.
+#
+# The +target+ name should correspond the name of the global function name
+# defined within your C extension, minus the 'Init_'. For example, if your
+# C extension is defined as 'Init_foo', then your target would simply be 'foo'.
+#
+# If any '/' characters are present in the target name, only the last name
+# is interpreted as the target name, and the rest are considered toplevel
+# directory names, and the generated Makefile will be altered accordingly to
+# follow that directory structure.
+#
+# For example, if you pass 'test/foo' as a target name, your extension will
+# be installed under the 'test' directory. This means that in order to
+# load the file within a Ruby program later, that directory structure will
+# have to be followed, e.g. "require 'test/foo'".
+#
+# The +srcprefix+ should be used when your source files are not in the same
+# directory as your build script. This will not only eliminate the need for
+# you to manually copy the source files into the same directory as your build
+# script, but it also sets the proper +target_prefix+ in the generated
+# Makefile.
+#
+# Setting the +target_prefix+ will, in turn, install the generated binary in
+# a directory under your Config::CONFIG['sitearchdir'] that mimics your local
+# filesystem when you run 'make install'.
+#
+# For example, given the following file tree:
+#
+# ext/
+# extconf.rb
+# test/
+# foo.c
+#
+# And given the following code:
+#
+# create_makefile('test/foo', 'test')
+#
+# That will set the +target_prefix+ in the generated Makefile to 'test'. That,
+# in turn, will create the following file tree when installed via the
+# 'make install' command:
+#
+# /path/to/ruby/sitearchdir/test/foo.so
+#
+# It is recommended that you use this approach to generate your makefiles,
+# instead of copying files around manually, because some third party
+# libraries may depend on the +target_prefix+ being set properly.
+#
+# The +srcprefix+ argument can be used to override the default source
+# directory, i.e. the current directory . It is included as part of the VPATH
+# and added to the list of INCFLAGS.
+#
+def create_makefile(target, srcprefix = nil)
+ $target = target
+ libpath = $DEFLIBPATH|$LIBPATH
+ message "creating Makefile\n"
+ rm_f "conftest*"
+ if CONFIG["DLEXT"] == $OBJEXT
+ for lib in libs = $libs.split
+ lib.sub!(/-l(.*)/, %%"lib\\1.#{$LIBEXT}"%)
+ end
+ $defs.push(format("-DEXTLIB='%s'", libs.join(",")))
+ end
+
+ if target.include?('/')
+ target_prefix, target = File.split(target)
+ target_prefix[0,0] = '/'
+ else
+ target_prefix = ""
+ end
+
+ srcprefix ||= '$(srcdir)'
+ RbConfig::expand(srcdir = srcprefix.dup)
+
+ if not $objs
+ $objs = []
+ srcs = Dir[File.join(srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
+ for f in srcs
+ obj = File.basename(f, ".*") << ".o"
+ $objs.push(obj) unless $objs.index(obj)
+ end
+ elsif !(srcs = $srcs)
+ srcs = $objs.collect {|o| o.sub(/\.o\z/, '.c')}
+ end
+ $srcs = srcs
+ for i in $objs
+ i.sub!(/\.o\z/, ".#{$OBJEXT}")
+ end
+ $objs = $objs.join(" ")
+
+ target = nil if $objs == ""
+
+ if target and EXPORT_PREFIX
+ if File.exist?(File.join(srcdir, target + '.def'))
+ deffile = "$(srcdir)/$(TARGET).def"
+ unless EXPORT_PREFIX.empty?
+ makedef = %{-pe "$_.sub!(/^(?=\\w)/,'#{EXPORT_PREFIX}') unless 1../^EXPORTS$/i"}
+ end
+ else
+ makedef = %{-e "puts 'EXPORTS', '#{EXPORT_PREFIX}Init_$(TARGET)'"}
+ end
+ if makedef
+ $distcleanfiles << '$(DEFFILE)'
+ origdef = deffile
+ deffile = "$(TARGET)-$(arch).def"
+ end
+ end
+ origdef ||= ''
+
+ if $extout and $INSTALLFILES
+ $cleanfiles.concat($INSTALLFILES.collect {|files, dir|File.join(dir, files.sub(/\A\.\//, ''))})
+ $distcleandirs.concat($INSTALLFILES.collect {|files, dir| dir})
+ end
+
+ if $extmk and not $extconf_h
+ create_header
+ end
+
+ libpath = libpathflag(libpath)
+
+ dllib = target ? "$(TARGET).#{CONFIG['DLEXT']}" : ""
+ staticlib = target ? "$(TARGET).#$LIBEXT" : ""
+ mfile = open("Makefile", "wb")
+ mfile.print(*configuration(srcprefix))
+ mfile.print "
+libpath = #{($DEFLIBPATH|$LIBPATH).join(" ")}
+LIBPATH = #{libpath}
+DEFFILE = #{deffile}
+
+CLEANFILES = #{$cleanfiles.join(' ')}
+DISTCLEANFILES = #{$distcleanfiles.join(' ')}
+DISTCLEANDIRS = #{$distcleandirs.join(' ')}
+
+extout = #{$extout}
+extout_prefix = #{$extout_prefix}
+target_prefix = #{target_prefix}
+LOCAL_LIBS = #{$LOCAL_LIBS}
+LIBS = #{$LIBRUBYARG} #{$libs} #{$LIBS}
+SRCS = #{srcs.collect(&File.method(:basename)).join(' ')}
+OBJS = #{$objs}
+TARGET = #{target}
+DLLIB = #{dllib}
+EXTSTATIC = #{$static || ""}
+STATIC_LIB = #{staticlib unless $static.nil?}
+#{!$extout && defined?($installed_list) ? "INSTALLED_LIST = #{$installed_list}\n" : ""}
+" #"
+ # TODO: fixme
+ install_dirs.each {|d| mfile.print("%-14s= %s\n" % d) if /^[[:upper:]]/ =~ d[0]}
+ n = ($extout ? '$(RUBYARCHDIR)/' : '') + '$(TARGET)'
+ mfile.print "
+TARGET_SO = #{($extout ? '$(RUBYARCHDIR)/' : '')}$(DLLIB)
+CLEANLIBS = #{n}.#{CONFIG['DLEXT']} #{config_string('cleanlibs') {|t| t.gsub(/\$\*/) {n}}}
+CLEANOBJS = *.#{$OBJEXT} #{config_string('cleanobjs') {|t| t.gsub(/\$\*/, '$(TARGET)')}} *.bak
+
+all: #{$extout ? "install" : target ? "$(DLLIB)" : "Makefile"}
+static: $(STATIC_LIB)#{$extout ? " install-rb" : ""}
+"
+ mfile.print CLEANINGS
+ fsep = config_string('BUILD_FILE_SEPARATOR') {|s| s unless s == "/"}
+ if fsep
+ sep = ":/=#{fsep}"
+ fseprepl = proc {|s|
+ s = s.gsub("/", fsep)
+ s = s.gsub(/(\$\(\w+)(\))/) {$1+sep+$2}
+ s = s.gsub(/(\$\{\w+)(\})/) {$1+sep+$2}
+ }
+ else
+ fseprepl = proc {|s| s}
+ sep = ""
+ end
+ dirs = []
+ mfile.print "install: install-so install-rb\n\n"
+ sodir = (dir = "$(RUBYARCHDIR)").dup
+ mfile.print("install-so: ")
+ if target
+ f = "$(DLLIB)"
+ dest = "#{dir}/#{f}"
+ mfile.puts dir, "install-so: #{dest}"
+ if $extout
+ mfile.print "clean-so::\n"
+ mfile.print "\t@-$(RM) #{fseprepl[dest]}\n"
+ mfile.print "\t@-$(RMDIRS) #{fseprepl[dir]}\n"
+ else
+ mfile.print "#{dest}: #{f}\n"
+ mfile.print "\t$(INSTALL_PROG) #{fseprepl[f]} #{fseprepl[dir]}\n"
+ if defined?($installed_list)
+ mfile.print "\t@echo #{dir}/#{File.basename(f)}>>$(INSTALLED_LIST)\n"
+ end
+ end
+ else
+ mfile.puts "Makefile"
+ end
+ mfile.print("install-rb: pre-install-rb install-rb-default\n")
+ mfile.print("install-rb-default: pre-install-rb-default\n")
+ mfile.print("pre-install-rb: Makefile\n")
+ mfile.print("pre-install-rb-default: Makefile\n")
+ for sfx, i in [["-default", [["lib/**/*.rb", "$(RUBYLIBDIR)", "lib"]]], ["", $INSTALLFILES]]
+ files = install_files(mfile, i, nil, srcprefix) or next
+ for dir, *files in files
+ unless dirs.include?(dir)
+ dirs << dir
+ mfile.print "pre-install-rb#{sfx}: #{dir}\n"
+ end
+ for f in files
+ dest = "#{dir}/#{File.basename(f)}"
+ mfile.print("install-rb#{sfx}: #{dest}\n")
+ mfile.print("#{dest}: #{f}\n")
+ mfile.print("\t$(#{$extout ? 'COPY' : 'INSTALL_DATA'}) ")
+ mfile.print("#{fseprepl[f]} $(@D#{sep})\n")
+ if defined?($installed_list) and !$extout
+ mfile.print("\t@echo #{dest}>>$(INSTALLED_LIST)\n")
+ end
+ if $extout
+ mfile.print("clean-rb#{sfx}::\n")
+ mfile.print("\t@-$(RM) #{fseprepl[dest]}\n")
+ end
+ end
+ end
+ if $extout
+ dirs.uniq!
+ dirs.reverse!
+ unless dirs.empty?
+ mfile.print("clean-rb#{sfx}::\n")
+ for dir in dirs
+ mfile.print("\t@-$(RMDIRS) #{fseprepl[dir]}\n")
+ end
+ end
+ end
+ end
+ dirs.unshift(sodir) if target and !dirs.include?(sodir)
+ dirs.each {|d| mfile.print "#{d}:\n\t$(MAKEDIRS) $@\n"}
+
+ mfile.print <<-SITEINSTALL
+
+site-install: site-install-so site-install-rb
+site-install-so: install-so
+site-install-rb: install-rb
+
+ SITEINSTALL
+
+ return unless target
+
+ mfile.puts SRC_EXT.collect {|ext| ".path.#{ext} = $(VPATH)"} if $nmake == ?b
+ mfile.print ".SUFFIXES: .#{SRC_EXT.join(' .')} .#{$OBJEXT}\n"
+ mfile.print "\n"
+
+ CXX_EXT.each do |ext|
+ COMPILE_RULES.each do |rule|
+ mfile.printf(rule, ext, $OBJEXT)
+ mfile.printf("\n\t%s\n\n", COMPILE_CXX)
+ end
+ end
+ %w[c].each do |ext|
+ COMPILE_RULES.each do |rule|
+ mfile.printf(rule, ext, $OBJEXT)
+ mfile.printf("\n\t%s\n\n", COMPILE_C)
+ end
+ end
+
+ mfile.print "$(RUBYARCHDIR)/" if $extout
+ mfile.print "$(DLLIB): "
+ mfile.print "$(DEFFILE) " if makedef
+ mfile.print "$(OBJS) Makefile\n"
+ mfile.print "\t@-$(RM) $(@#{sep})\n"
+ mfile.print "\t@-$(MAKEDIRS) $(@D)\n" if $extout
+ link_so = LINK_SO.gsub(/^/, "\t")
+ if srcs.any?(&%r"\.(?:#{CXX_EXT.join('|')})\z".method(:===))
+ link_so = link_so.sub(/\bLDSHARED\b/, '\&XX')
+ end
+ mfile.print link_so, "\n\n"
+ unless $static.nil?
+ mfile.print "$(STATIC_LIB): $(OBJS)\n\t@-$(RM) $(@#{sep})\n\t"
+ mfile.print "$(AR) #{config_string('ARFLAGS') || 'cru '}$@ $(OBJS)"
+ config_string('RANLIB') do |ranlib|
+ mfile.print "\n\t@-#{ranlib} $(DLLIB) 2> /dev/null || true"
+ end
+ end
+ mfile.print "\n\n"
+ if makedef
+ mfile.print "$(DEFFILE): #{origdef}\n"
+ mfile.print "\t$(RUBY) #{makedef} #{origdef} > $@\n\n"
+ end
+
+ depend = File.join(srcdir, "depend")
+ if File.exist?(depend)
+ mfile.print("###\n", *depend_rules(File.read(depend)))
+ else
+ headers = %w[$(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h]
+ if RULE_SUBST
+ headers.each {|h| h.sub!(/.*/, &RULE_SUBST.method(:%))}
+ end
+ headers << $config_h
+ headers << '$(RUBY_EXTCONF_H)' if $extconf_h
+ mfile.print "$(OBJS): ", headers.join(' '), "\n"
+ end
+
+ $makefile_created = true
+ensure
+ mfile.close if mfile
+end
+
+# :stopdoc:
+
+def init_mkmf(config = CONFIG)
+ $makefile_created = false
+ $arg_config = []
+ $enable_shared = config['ENABLE_SHARED'] == 'yes'
+ $defs = []
+ $extconf_h = nil
+ $CFLAGS = with_config("cflags", arg_config("CFLAGS", config["CFLAGS"])).dup
+ $ARCH_FLAG = with_config("arch_flag", arg_config("ARCH_FLAG", config["ARCH_FLAG"])).dup
+ $CPPFLAGS = with_config("cppflags", arg_config("CPPFLAGS", config["CPPFLAGS"])).dup
+ $LDFLAGS = with_config("ldflags", arg_config("LDFLAGS", config["LDFLAGS"])).dup
+ $INCFLAGS = "-I$(arch_hdrdir)"
+ $INCFLAGS << " -I$(hdrdir)/ruby/backward" unless $extmk
+ $INCFLAGS << " -I$(hdrdir) -I$(srcdir)"
+ $DLDFLAGS = with_config("dldflags", arg_config("DLDFLAGS", config["DLDFLAGS"])).dup
+ $LIBEXT = config['LIBEXT'].dup
+ $OBJEXT = config["OBJEXT"].dup
+ $LIBS = "#{config['LIBS']} #{config['DLDLIBS']}"
+ $LIBRUBYARG = ""
+ $LIBRUBYARG_STATIC = config['LIBRUBYARG_STATIC']
+ $LIBRUBYARG_SHARED = config['LIBRUBYARG_SHARED']
+ $DEFLIBPATH = $extmk ? ["$(topdir)"] : CROSS_COMPILING ? [] : ["$(libdir)"]
+ $DEFLIBPATH.unshift(".")
+ $LIBPATH = []
+ $INSTALLFILES = []
+ $NONINSTALLFILES = [/~\z/, /\A#.*#\z/, /\A\.#/, /\.bak\z/i, /\.orig\z/, /\.rej\z/, /\.l[ao]\z/, /\.o\z/]
+ $VPATH = %w[$(srcdir) $(arch_hdrdir)/ruby $(hdrdir)/ruby]
+
+ $objs = nil
+ $srcs = nil
+ $libs = ""
+ if $enable_shared or RbConfig.expand(config["LIBRUBY"].dup) != RbConfig.expand(config["LIBRUBY_A"].dup)
+ $LIBRUBYARG = config['LIBRUBYARG']
+ end
+
+ $LOCAL_LIBS = ""
+
+ $cleanfiles = config_string('CLEANFILES') {|s| Shellwords.shellwords(s)} || []
+ $cleanfiles << "mkmf.log"
+ $distcleanfiles = config_string('DISTCLEANFILES') {|s| Shellwords.shellwords(s)} || []
+ $distcleandirs = config_string('DISTCLEANDIRS') {|s| Shellwords.shellwords(s)} || []
+
+ $extout ||= nil
+ $extout_prefix ||= nil
+
+ $arg_config.clear
+ dir_config("opt")
+end
+
+FailedMessage = <<MESSAGE
+Could not create Makefile due to some reason, probably lack of
+necessary libraries and/or headers. Check the mkmf.log file for more
+details. You may need configuration options.
+
+Provided configuration options:
+MESSAGE
+
+# Returns whether or not the Makefile was successfully generated. If not,
+# the script will abort with an error message.
+#
+# Internal use only.
+#
+def mkmf_failed(path)
+ unless $makefile_created or File.exist?("Makefile")
+ opts = $arg_config.collect {|t, n| "\t#{t}#{n ? "=#{n}" : ""}\n"}
+ abort "*** #{path} failed ***\n" + FailedMessage + opts.join
+ end
+end
+
+# :startdoc:
+
+init_mkmf
+
+$make = with_config("make-prog", ENV["MAKE"] || "make")
+make, = Shellwords.shellwords($make)
+$nmake = nil
+case
+when $mswin
+ $nmake = ?m if /nmake/i =~ make
+when $bccwin
+ $nmake = ?b if /Borland/i =~ `#{make} -h`
+end
+
+RbConfig::CONFIG["srcdir"] = CONFIG["srcdir"] =
+ $srcdir = arg_config("--srcdir", File.dirname($0))
+$configure_args["--topsrcdir"] ||= $srcdir
+if $curdir = arg_config("--curdir")
+ RbConfig.expand(curdir = $curdir.dup)
+else
+ curdir = $curdir = "."
+end
+unless File.expand_path(RbConfig::CONFIG["topdir"]) == File.expand_path(curdir)
+ CONFIG["topdir"] = $curdir
+ RbConfig::CONFIG["topdir"] = curdir
+end
+$configure_args["--topdir"] ||= $curdir
+$ruby = arg_config("--ruby", File.join(RbConfig::CONFIG["bindir"], CONFIG["ruby_install_name"]))
+
+split = Shellwords.method(:shellwords).to_proc
+
+EXPORT_PREFIX = config_string('EXPORT_PREFIX') {|s| s.strip}
+
+hdr = ['#include "ruby.h"' "\n"]
+config_string('COMMON_MACROS') do |s|
+ Shellwords.shellwords(s).each do |w|
+ hdr << "#define " + w.split(/=/, 2).join(" ")
+ end
+end
+config_string('COMMON_HEADERS') do |s|
+ Shellwords.shellwords(s).each {|w| hdr << "#include <#{w}>"}
+end
+COMMON_HEADERS = hdr.join("\n")
+COMMON_LIBS = config_string('COMMON_LIBS', &split) || []
+
+COMPILE_RULES = config_string('COMPILE_RULES', &split) || %w[.%s.%s:]
+RULE_SUBST = config_string('RULE_SUBST')
+COMPILE_C = config_string('COMPILE_C') || '$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<'
+COMPILE_CXX = config_string('COMPILE_CXX') || '$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<'
+TRY_LINK = config_string('TRY_LINK') ||
+ "$(CC) #{OUTFLAG}conftest $(INCFLAGS) $(CPPFLAGS) " \
+ "$(CFLAGS) $(src) $(LIBPATH) $(LDFLAGS) $(ARCH_FLAG) $(LOCAL_LIBS) $(LIBS)"
+LINK_SO = config_string('LINK_SO') ||
+ if CONFIG["DLEXT"] == $OBJEXT
+ "ld $(DLDFLAGS) -r -o $@ $(OBJS)\n"
+ else
+ "$(LDSHARED) #{OUTFLAG}$@ $(OBJS) " \
+ "$(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)"
+ end
+LIBPATHFLAG = config_string('LIBPATHFLAG') || ' -L"%s"'
+RPATHFLAG = config_string('RPATHFLAG') || ''
+LIBARG = config_string('LIBARG') || '-l%s'
+MAIN_DOES_NOTHING = config_string('MAIN_DOES_NOTHING') || 'int main() {return 0;}'
+
+sep = config_string('BUILD_FILE_SEPARATOR') {|s| ":/=#{s}" if sep != "/"} || ""
+CLEANINGS = "
+clean-rb-default::
+clean-rb::
+clean-so::
+clean: clean-so clean-rb-default clean-rb
+\t\t@-$(RM) $(CLEANLIBS#{sep}) $(CLEANOBJS#{sep}) $(CLEANFILES#{sep})
+
+distclean-rb-default::
+distclean-rb::
+distclean-so::
+distclean: clean distclean-so distclean-rb-default distclean-rb
+\t\t@-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
+\t\t@-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES#{sep})
+\t\t@-$(RMDIRS) $(DISTCLEANDIRS#{sep})
+
+realclean: distclean
+"
+
+if not $extmk and /\A(extconf|makefile).rb\z/ =~ File.basename($0)
+ END {mkmf_failed($0)}
+end
diff --git a/ruby/lib/monitor.rb b/ruby/lib/monitor.rb
new file mode 100644
index 0000000..2f2bb16
--- /dev/null
+++ b/ruby/lib/monitor.rb
@@ -0,0 +1,265 @@
+=begin
+
+= monitor.rb
+
+Copyright (C) 2001 Shugo Maeda <shugo@ruby-lang.org>
+
+This library is distributed under the terms of the Ruby license.
+You can freely distribute/modify this library.
+
+== example
+
+This is a simple example.
+
+ require 'monitor.rb'
+
+ buf = []
+ buf.extend(MonitorMixin)
+ empty_cond = buf.new_cond
+
+ # consumer
+ Thread.start do
+ loop do
+ buf.synchronize do
+ empty_cond.wait_while { buf.empty? }
+ print buf.shift
+ end
+ end
+ end
+
+ # producer
+ while line = ARGF.gets
+ buf.synchronize do
+ buf.push(line)
+ empty_cond.signal
+ end
+ end
+
+The consumer thread waits for the producer thread to push a line
+to buf while buf.empty?, and the producer thread (main thread)
+reads a line from ARGF and push it to buf, then call
+empty_cond.signal.
+
+=end
+
+require 'thread'
+
+#
+# Adds monitor functionality to an arbitrary object by mixing the module with
+# +include+. For example:
+#
+# require 'monitor'
+#
+# buf = []
+# buf.extend(MonitorMixin)
+# empty_cond = buf.new_cond
+#
+# # consumer
+# Thread.start do
+# loop do
+# buf.synchronize do
+# empty_cond.wait_while { buf.empty? }
+# print buf.shift
+# end
+# end
+# end
+#
+# # producer
+# while line = ARGF.gets
+# buf.synchronize do
+# buf.push(line)
+# empty_cond.signal
+# end
+# end
+#
+# The consumer thread waits for the producer thread to push a line
+# to buf while buf.empty?, and the producer thread (main thread)
+# reads a line from ARGF and push it to buf, then call
+# empty_cond.signal.
+#
+module MonitorMixin
+ #
+ # FIXME: This isn't documented in Nutshell.
+ #
+ # Since MonitorMixin.new_cond returns a ConditionVariable, and the example
+ # above calls while_wait and signal, this class should be documented.
+ #
+ class ConditionVariable
+ class Timeout < Exception; end
+
+ def wait(timeout = nil)
+ if timeout
+ raise NotImplementedError, "timeout is not implemented yet"
+ end
+ @monitor.__send__(:mon_check_owner)
+ count = @monitor.__send__(:mon_exit_for_cond)
+ begin
+ @cond.wait(@monitor.instance_variable_get("@mon_mutex"))
+ return true
+ ensure
+ @monitor.__send__(:mon_enter_for_cond, count)
+ end
+ end
+
+ def wait_while
+ while yield
+ wait
+ end
+ end
+
+ def wait_until
+ until yield
+ wait
+ end
+ end
+
+ def signal
+ @monitor.__send__(:mon_check_owner)
+ @cond.signal
+ end
+
+ def broadcast
+ @monitor.__send__(:mon_check_owner)
+ @cond.broadcast
+ end
+
+ def count_waiters
+ raise NotImplementedError
+ end
+
+ private
+
+ def initialize(monitor)
+ @monitor = monitor
+ @cond = ::ConditionVariable.new
+ end
+ end
+
+ def self.extend_object(obj)
+ super(obj)
+ obj.__send__(:mon_initialize)
+ end
+
+ #
+ # Attempts to enter exclusive section. Returns +false+ if lock fails.
+ #
+ def mon_try_enter
+ if @mon_owner != Thread.current
+ unless @mon_mutex.try_lock
+ return false
+ end
+ @mon_owner = Thread.current
+ end
+ @mon_count += 1
+ return true
+ end
+ # For backward compatibility
+ alias try_mon_enter mon_try_enter
+
+ #
+ # Enters exclusive section.
+ #
+ def mon_enter
+ if @mon_owner != Thread.current
+ @mon_mutex.lock
+ @mon_owner = Thread.current
+ end
+ @mon_count += 1
+ end
+
+ #
+ # Leaves exclusive section.
+ #
+ def mon_exit
+ mon_check_owner
+ @mon_count -=1
+ if @mon_count == 0
+ @mon_owner = nil
+ @mon_mutex.unlock
+ end
+ end
+
+ #
+ # Enters exclusive section and executes the block. Leaves the exclusive
+ # section automatically when the block exits. See example under
+ # +MonitorMixin+.
+ #
+ def mon_synchronize
+ mon_enter
+ begin
+ yield
+ ensure
+ mon_exit
+ end
+ end
+ alias synchronize mon_synchronize
+
+ #
+ # FIXME: This isn't documented in Nutshell.
+ #
+ def new_cond
+ return ConditionVariable.new(self)
+ end
+
+ private
+
+ def initialize(*args)
+ super
+ mon_initialize
+ end
+
+ def mon_initialize
+ @mon_owner = nil
+ @mon_count = 0
+ @mon_mutex = Mutex.new
+ end
+
+ def mon_check_owner
+ if @mon_owner != Thread.current
+ raise ThreadError, "current thread not owner"
+ end
+ end
+
+ def mon_enter_for_cond(count)
+ @mon_owner = Thread.current
+ @mon_count = count
+ end
+
+ def mon_exit_for_cond
+ count = @mon_count
+ @mon_owner = nil
+ @mon_count = 0
+ return count
+ end
+end
+
+class Monitor
+ include MonitorMixin
+ alias try_enter try_mon_enter
+ alias enter mon_enter
+ alias exit mon_exit
+end
+
+
+# Documentation comments:
+# - All documentation comes from Nutshell.
+# - MonitorMixin.new_cond appears in the example, but is not documented in
+# Nutshell.
+# - All the internals (internal modules Accessible and Initializable, class
+# ConditionVariable) appear in RDoc. It might be good to hide them, by
+# making them private, or marking them :nodoc:, etc.
+# - The entire example from the RD section at the top is replicated in the RDoc
+# comment for MonitorMixin. Does the RD section need to remain?
+# - RDoc doesn't recognise aliases, so we have mon_synchronize documented, but
+# not synchronize.
+# - mon_owner is in Nutshell, but appears as an accessor in a separate module
+# here, so is hard/impossible to RDoc. Some other useful accessors
+# (mon_count and some queue stuff) are also in this module, and don't appear
+# directly in the RDoc output.
+# - in short, it may be worth changing the code layout in this file to make the
+# documentation easier
+
+# Local variables:
+# mode: Ruby
+# tab-width: 8
+# End:
diff --git a/ruby/lib/mutex_m.rb b/ruby/lib/mutex_m.rb
new file mode 100644
index 0000000..f46f866
--- /dev/null
+++ b/ruby/lib/mutex_m.rb
@@ -0,0 +1,91 @@
+#
+# mutex_m.rb -
+# $Release Version: 3.0$
+# $Revision: 1.7 $
+# Original from mutex.rb
+# by Keiju ISHITSUKA(keiju@ishitsuka.com)
+# modified by matz
+# patched by akira yamada
+#
+# --
+# Usage:
+# require "mutex_m.rb"
+# obj = Object.new
+# obj.extend Mutex_m
+# ...
+# extended object can be handled like Mutex
+# or
+# class Foo
+# include Mutex_m
+# ...
+# end
+# obj = Foo.new
+# this obj can be handled like Mutex
+#
+
+require 'thread'
+
+module Mutex_m
+ def Mutex_m.define_aliases(cl)
+ cl.module_eval %q{
+ alias locked? mu_locked?
+ alias lock mu_lock
+ alias unlock mu_unlock
+ alias try_lock mu_try_lock
+ alias synchronize mu_synchronize
+ }
+ end
+
+ def Mutex_m.append_features(cl)
+ super
+ define_aliases(cl) unless cl.instance_of?(Module)
+ end
+
+ def Mutex_m.extend_object(obj)
+ super
+ obj.mu_extended
+ end
+
+ def mu_extended
+ unless (defined? locked? and
+ defined? lock and
+ defined? unlock and
+ defined? try_lock and
+ defined? synchronize)
+ Mutex_m.define_aliases(class<<self;self;end)
+ end
+ mu_initialize
+ end
+
+ # locking
+ def mu_synchronize(&block)
+ @_mutex.synchronize(&block)
+ end
+
+ def mu_locked?
+ @_mutex.locked?
+ end
+
+ def mu_try_lock
+ @_mutex.try_lock
+ end
+
+ def mu_lock
+ @_mutex.lock
+ end
+
+ def mu_unlock
+ @_mutex.unlock
+ end
+
+ private
+
+ def mu_initialize
+ @_mutex = Mutex.new
+ end
+
+ def initialize(*args)
+ mu_initialize
+ super
+ end
+end
diff --git a/ruby/lib/net/ftp.rb b/ruby/lib/net/ftp.rb
new file mode 100644
index 0000000..06cc3ea
--- /dev/null
+++ b/ruby/lib/net/ftp.rb
@@ -0,0 +1,981 @@
+#
+# = net/ftp.rb - FTP Client Library
+#
+# Written by Shugo Maeda <shugo@ruby-lang.org>.
+#
+# Documentation by Gavin Sinclair, sourced from "Programming Ruby" (Hunt/Thomas)
+# and "Ruby In a Nutshell" (Matsumoto), used with permission.
+#
+# This library is distributed under the terms of the Ruby license.
+# You can freely distribute/modify this library.
+#
+# It is included in the Ruby standard library.
+#
+# See the Net::FTP class for an overview.
+#
+
+require "socket"
+require "monitor"
+
+module Net
+
+ # :stopdoc:
+ class FTPError < StandardError; end
+ class FTPReplyError < FTPError; end
+ class FTPTempError < FTPError; end
+ class FTPPermError < FTPError; end
+ class FTPProtoError < FTPError; end
+ # :startdoc:
+
+ #
+ # This class implements the File Transfer Protocol. If you have used a
+ # command-line FTP program, and are familiar with the commands, you will be
+ # able to use this class easily. Some extra features are included to take
+ # advantage of Ruby's style and strengths.
+ #
+ # == Example
+ #
+ # require 'net/ftp'
+ #
+ # === Example 1
+ #
+ # ftp = Net::FTP.new('ftp.netlab.co.jp')
+ # ftp.login
+ # files = ftp.chdir('pub/lang/ruby/contrib')
+ # files = ftp.list('n*')
+ # ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
+ # ftp.close
+ #
+ # === Example 2
+ #
+ # Net::FTP.open('ftp.netlab.co.jp') do |ftp|
+ # ftp.login
+ # files = ftp.chdir('pub/lang/ruby/contrib')
+ # files = ftp.list('n*')
+ # ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
+ # end
+ #
+ # == Major Methods
+ #
+ # The following are the methods most likely to be useful to users:
+ # - FTP.open
+ # - #getbinaryfile
+ # - #gettextfile
+ # - #putbinaryfile
+ # - #puttextfile
+ # - #chdir
+ # - #nlst
+ # - #size
+ # - #rename
+ # - #delete
+ #
+ class FTP
+ include MonitorMixin
+
+ # :stopdoc:
+ FTP_PORT = 21
+ CRLF = "\r\n"
+ DEFAULT_BLOCKSIZE = 4096
+ # :startdoc:
+
+ # When +true+, transfers are performed in binary mode. Default: +true+.
+ attr_reader :binary
+
+ # When +true+, the connection is in passive mode. Default: +false+.
+ attr_accessor :passive
+
+ # When +true+, all traffic to and from the server is written
+ # to +$stdout+. Default: +false+.
+ attr_accessor :debug_mode
+
+ # Sets or retrieves the +resume+ status, which decides whether incomplete
+ # transfers are resumed or restarted. Default: +false+.
+ attr_accessor :resume
+
+ # The server's welcome message.
+ attr_reader :welcome
+
+ # The server's last response code.
+ attr_reader :last_response_code
+ alias lastresp last_response_code
+
+ # The server's last response.
+ attr_reader :last_response
+
+ #
+ # A synonym for <tt>FTP.new</tt>, but with a mandatory host parameter.
+ #
+ # If a block is given, it is passed the +FTP+ object, which will be closed
+ # when the block finishes, or when an exception is raised.
+ #
+ def FTP.open(host, user = nil, passwd = nil, acct = nil)
+ if block_given?
+ ftp = new(host, user, passwd, acct)
+ begin
+ yield ftp
+ ensure
+ ftp.close
+ end
+ else
+ new(host, user, passwd, acct)
+ end
+ end
+
+ #
+ # Creates and returns a new +FTP+ object. If a +host+ is given, a connection
+ # is made. Additionally, if the +user+ is given, the given user name,
+ # password, and (optionally) account are used to log in. See #login.
+ #
+ def initialize(host = nil, user = nil, passwd = nil, acct = nil)
+ super()
+ @binary = false
+ @passive = false
+ @debug_mode = false
+ @resume = false
+ if host
+ connect(host)
+ if user
+ login(user, passwd, acct)
+ end
+ end
+ end
+
+ def binary=(newmode)
+ if newmode != @binary
+ @binary = newmode
+ @binary ? voidcmd("TYPE I") : voidcmd("TYPE A")
+ end
+ end
+
+ def with_binary(newmode)
+ oldmode = binary
+ self.binary = newmode
+ begin
+ yield
+ ensure
+ self.binary = oldmode
+ end
+ end
+ private :with_binary
+
+ # Obsolete
+ def return_code
+ $stderr.puts("warning: Net::FTP#return_code is obsolete and do nothing")
+ return "\n"
+ end
+
+ # Obsolete
+ def return_code=(s)
+ $stderr.puts("warning: Net::FTP#return_code= is obsolete and do nothing")
+ end
+
+ def open_socket(host, port)
+ if defined? SOCKSSocket and ENV["SOCKS_SERVER"]
+ @passive = true
+ return SOCKSSocket.open(host, port)
+ else
+ return TCPSocket.open(host, port)
+ end
+ end
+ private :open_socket
+
+ #
+ # Establishes an FTP connection to host, optionally overriding the default
+ # port. If the environment variable +SOCKS_SERVER+ is set, sets up the
+ # connection through a SOCKS proxy. Raises an exception (typically
+ # <tt>Errno::ECONNREFUSED</tt>) if the connection cannot be established.
+ #
+ def connect(host, port = FTP_PORT)
+ if @debug_mode
+ print "connect: ", host, ", ", port, "\n"
+ end
+ synchronize do
+ @sock = open_socket(host, port)
+ voidresp
+ end
+ end
+
+ #
+ # WRITEME or make private
+ #
+ def set_socket(sock, get_greeting = true)
+ synchronize do
+ @sock = sock
+ if get_greeting
+ voidresp
+ end
+ end
+ end
+
+ def sanitize(s)
+ if s =~ /^PASS /i
+ return s[0, 5] + "*" * (s.length - 5)
+ else
+ return s
+ end
+ end
+ private :sanitize
+
+ def putline(line)
+ if @debug_mode
+ print "put: ", sanitize(line), "\n"
+ end
+ line = line + CRLF
+ @sock.write(line)
+ end
+ private :putline
+
+ def getline
+ line = @sock.readline # if get EOF, raise EOFError
+ line.sub!(/(\r\n|\n|\r)\z/n, "")
+ if @debug_mode
+ print "get: ", sanitize(line), "\n"
+ end
+ return line
+ end
+ private :getline
+
+ def getmultiline
+ line = getline
+ buff = line
+ if line[3] == ?-
+ code = line[0, 3]
+ begin
+ line = getline
+ buff << "\n" << line
+ end until line[0, 3] == code and line[3] != ?-
+ end
+ return buff << "\n"
+ end
+ private :getmultiline
+
+ def getresp
+ @last_response = getmultiline
+ @last_response_code = @last_response[0, 3]
+ case @last_response_code
+ when /\A[123]/
+ return @last_response
+ when /\A4/
+ raise FTPTempError, @last_response
+ when /\A5/
+ raise FTPPermError, @last_response
+ else
+ raise FTPProtoError, @last_response
+ end
+ end
+ private :getresp
+
+ def voidresp
+ resp = getresp
+ if resp[0] != ?2
+ raise FTPReplyError, resp
+ end
+ end
+ private :voidresp
+
+ #
+ # Sends a command and returns the response.
+ #
+ def sendcmd(cmd)
+ synchronize do
+ putline(cmd)
+ return getresp
+ end
+ end
+
+ #
+ # Sends a command and expect a response beginning with '2'.
+ #
+ def voidcmd(cmd)
+ synchronize do
+ putline(cmd)
+ voidresp
+ end
+ end
+
+ def sendport(host, port)
+ af = (@sock.peeraddr)[0]
+ if af == "AF_INET"
+ cmd = "PORT " + (host.split(".") + port.divmod(256)).join(",")
+ elsif af == "AF_INET6"
+ cmd = sprintf("EPRT |2|%s|%d|", host, port)
+ else
+ raise FTPProtoError, host
+ end
+ voidcmd(cmd)
+ end
+ private :sendport
+
+ def makeport
+ sock = TCPServer.open(@sock.addr[3], 0)
+ port = sock.addr[1]
+ host = sock.addr[3]
+ resp = sendport(host, port)
+ return sock
+ end
+ private :makeport
+
+ def makepasv
+ if @sock.peeraddr[0] == "AF_INET"
+ host, port = parse227(sendcmd("PASV"))
+ else
+ host, port = parse229(sendcmd("EPSV"))
+ # host, port = parse228(sendcmd("LPSV"))
+ end
+ return host, port
+ end
+ private :makepasv
+
+ def transfercmd(cmd, rest_offset = nil)
+ if @passive
+ host, port = makepasv
+ conn = open_socket(host, port)
+ if @resume and rest_offset
+ resp = sendcmd("REST " + rest_offset.to_s)
+ if resp[0] != ?3
+ raise FTPReplyError, resp
+ end
+ end
+ resp = sendcmd(cmd)
+ # skip 2XX for some ftp servers
+ resp = getresp if resp[0] == ?2
+ if resp[0] != ?1
+ raise FTPReplyError, resp
+ end
+ else
+ sock = makeport
+ if @resume and rest_offset
+ resp = sendcmd("REST " + rest_offset.to_s)
+ if resp[0] != ?3
+ raise FTPReplyError, resp
+ end
+ end
+ resp = sendcmd(cmd)
+ # skip 2XX for some ftp servers
+ resp = getresp if resp[0] == ?2
+ if resp[0] != ?1
+ raise FTPReplyError, resp
+ end
+ conn = sock.accept
+ sock.close
+ end
+ return conn
+ end
+ private :transfercmd
+
+ def getaddress
+ thishost = Socket.gethostname
+ if not thishost.index(".")
+ thishost = Socket.gethostbyname(thishost)[0]
+ end
+ if ENV.has_key?("LOGNAME")
+ realuser = ENV["LOGNAME"]
+ elsif ENV.has_key?("USER")
+ realuser = ENV["USER"]
+ else
+ realuser = "anonymous"
+ end
+ return realuser + "@" + thishost
+ end
+ private :getaddress
+
+ #
+ # Logs in to the remote host. The session must have been previously
+ # connected. If +user+ is the string "anonymous" and the +password+ is
+ # +nil+, a password of <tt>user@host</tt> is synthesized. If the +acct+
+ # parameter is not +nil+, an FTP ACCT command is sent following the
+ # successful login. Raises an exception on error (typically
+ # <tt>Net::FTPPermError</tt>).
+ #
+ def login(user = "anonymous", passwd = nil, acct = nil)
+ if user == "anonymous" and passwd == nil
+ passwd = getaddress
+ end
+
+ resp = ""
+ synchronize do
+ resp = sendcmd('USER ' + user)
+ if resp[0] == ?3
+ raise FTPReplyError, resp if passwd.nil?
+ resp = sendcmd('PASS ' + passwd)
+ end
+ if resp[0] == ?3
+ raise FTPReplyError, resp if acct.nil?
+ resp = sendcmd('ACCT ' + acct)
+ end
+ end
+ if resp[0] != ?2
+ raise FTPReplyError, resp
+ end
+ @welcome = resp
+ self.binary = true
+ end
+
+ #
+ # Puts the connection into binary (image) mode, issues the given command,
+ # and fetches the data returned, passing it to the associated block in
+ # chunks of +blocksize+ characters. Note that +cmd+ is a server command
+ # (such as "RETR myfile").
+ #
+ def retrbinary(cmd, blocksize, rest_offset = nil) # :yield: data
+ synchronize do
+ with_binary(true) do
+ conn = transfercmd(cmd, rest_offset)
+ loop do
+ data = conn.read(blocksize)
+ break if data == nil
+ yield(data)
+ end
+ conn.close
+ voidresp
+ end
+ end
+ end
+
+ #
+ # Puts the connection into ASCII (text) mode, issues the given command, and
+ # passes the resulting data, one line at a time, to the associated block. If
+ # no block is given, prints the lines. Note that +cmd+ is a server command
+ # (such as "RETR myfile").
+ #
+ def retrlines(cmd) # :yield: line
+ synchronize do
+ with_binary(false) do
+ conn = transfercmd(cmd)
+ loop do
+ line = conn.gets
+ break if line == nil
+ if line[-2, 2] == CRLF
+ line = line[0 .. -3]
+ elsif line[-1] == ?\n
+ line = line[0 .. -2]
+ end
+ yield(line)
+ end
+ conn.close
+ voidresp
+ end
+ end
+ end
+
+ #
+ # Puts the connection into binary (image) mode, issues the given server-side
+ # command (such as "STOR myfile"), and sends the contents of the file named
+ # +file+ to the server. If the optional block is given, it also passes it
+ # the data, in chunks of +blocksize+ characters.
+ #
+ def storbinary(cmd, file, blocksize, rest_offset = nil, &block) # :yield: data
+ if rest_offset
+ file.seek(rest_offset, IO::SEEK_SET)
+ end
+ synchronize do
+ with_binary(true) do
+ conn = transfercmd(cmd, rest_offset)
+ loop do
+ buf = file.read(blocksize)
+ break if buf == nil
+ conn.write(buf)
+ yield(buf) if block
+ end
+ conn.close
+ voidresp
+ end
+ end
+ rescue Errno::EPIPE
+ # EPIPE, in this case, means that the data connection was unexpectedly
+ # terminated. Rather than just raising EPIPE to the caller, check the
+ # response on the control connection. If getresp doesn't raise a more
+ # appropriate exception, re-raise the original exception.
+ getresp
+ raise
+ end
+
+ #
+ # Puts the connection into ASCII (text) mode, issues the given server-side
+ # command (such as "STOR myfile"), and sends the contents of the file
+ # named +file+ to the server, one line at a time. If the optional block is
+ # given, it also passes it the lines.
+ #
+ def storlines(cmd, file, &block) # :yield: line
+ synchronize do
+ with_binary(false) do
+ conn = transfercmd(cmd)
+ loop do
+ buf = file.gets
+ break if buf == nil
+ if buf[-2, 2] != CRLF
+ buf = buf.chomp + CRLF
+ end
+ conn.write(buf)
+ yield(buf) if block
+ end
+ conn.close
+ voidresp
+ end
+ end
+ rescue Errno::EPIPE
+ # EPIPE, in this case, means that the data connection was unexpectedly
+ # terminated. Rather than just raising EPIPE to the caller, check the
+ # response on the control connection. If getresp doesn't raise a more
+ # appropriate exception, re-raise the original exception.
+ getresp
+ raise
+ end
+
+ #
+ # Retrieves +remotefile+ in binary mode, storing the result in +localfile+.
+ # If +localfile+ is nil, returns retrieved data.
+ # If a block is supplied, it is passed the retrieved data in +blocksize+
+ # chunks.
+ #
+ def getbinaryfile(remotefile, localfile = File.basename(remotefile),
+ blocksize = DEFAULT_BLOCKSIZE) # :yield: data
+ result = nil
+ if localfile
+ if @resume
+ rest_offset = File.size?(localfile)
+ f = open(localfile, "a")
+ else
+ rest_offset = nil
+ f = open(localfile, "w")
+ end
+ elsif !block_given?
+ result = ""
+ end
+ begin
+ f.binmode if localfile
+ retrbinary("RETR " + remotefile, blocksize, rest_offset) do |data|
+ f.write(data) if localfile
+ yield(data) if block_given?
+ result.concat(data) if result
+ end
+ return result
+ ensure
+ f.close if localfile
+ end
+ end
+
+ #
+ # Retrieves +remotefile+ in ASCII (text) mode, storing the result in
+ # +localfile+.
+ # If +localfile+ is nil, returns retrieved data.
+ # If a block is supplied, it is passed the retrieved data one
+ # line at a time.
+ #
+ def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: line
+ result = nil
+ if localfile
+ f = open(localfile, "w")
+ elsif !block_given?
+ result = ""
+ end
+ begin
+ retrlines("RETR " + remotefile) do |line|
+ f.puts(line) if localfile
+ yield(line) if block_given?
+ result.concat(line + "\n") if result
+ end
+ return result
+ ensure
+ f.close if localfile
+ end
+ end
+
+ #
+ # Retrieves +remotefile+ in whatever mode the session is set (text or
+ # binary). See #gettextfile and #getbinaryfile.
+ #
+ def get(remotefile, localfile = File.basename(remotefile),
+ blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data
+ if @binary
+ getbinaryfile(remotefile, localfile, blocksize, &block)
+ else
+ gettextfile(remotefile, localfile, &block)
+ end
+ end
+
+ #
+ # Transfers +localfile+ to the server in binary mode, storing the result in
+ # +remotefile+. If a block is supplied, calls it, passing in the transmitted
+ # data in +blocksize+ chunks.
+ #
+ def putbinaryfile(localfile, remotefile = File.basename(localfile),
+ blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data
+ if @resume
+ begin
+ rest_offset = size(remotefile)
+ rescue Net::FTPPermError
+ rest_offset = nil
+ end
+ else
+ rest_offset = nil
+ end
+ f = open(localfile)
+ begin
+ f.binmode
+ storbinary("STOR " + remotefile, f, blocksize, rest_offset, &block)
+ ensure
+ f.close
+ end
+ end
+
+ #
+ # Transfers +localfile+ to the server in ASCII (text) mode, storing the result
+ # in +remotefile+. If callback or an associated block is supplied, calls it,
+ # passing in the transmitted data one line at a time.
+ #
+ def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line
+ f = open(localfile)
+ begin
+ storlines("STOR " + remotefile, f, &block)
+ ensure
+ f.close
+ end
+ end
+
+ #
+ # Transfers +localfile+ to the server in whatever mode the session is set
+ # (text or binary). See #puttextfile and #putbinaryfile.
+ #
+ def put(localfile, remotefile = File.basename(localfile),
+ blocksize = DEFAULT_BLOCKSIZE, &block)
+ if @binary
+ putbinaryfile(localfile, remotefile, blocksize, &block)
+ else
+ puttextfile(localfile, remotefile, &block)
+ end
+ end
+
+ #
+ # Sends the ACCT command. TODO: more info.
+ #
+ def acct(account)
+ cmd = "ACCT " + account
+ voidcmd(cmd)
+ end
+
+ #
+ # Returns an array of filenames in the remote directory.
+ #
+ def nlst(dir = nil)
+ cmd = "NLST"
+ if dir
+ cmd = cmd + " " + dir
+ end
+ files = []
+ retrlines(cmd) do |line|
+ files.push(line)
+ end
+ return files
+ end
+
+ #
+ # Returns an array of file information in the directory (the output is like
+ # `ls -l`). If a block is given, it iterates through the listing.
+ #
+ def list(*args, &block) # :yield: line
+ cmd = "LIST"
+ args.each do |arg|
+ cmd = cmd + " " + arg
+ end
+ if block
+ retrlines(cmd, &block)
+ else
+ lines = []
+ retrlines(cmd) do |line|
+ lines << line
+ end
+ return lines
+ end
+ end
+ alias ls list
+ alias dir list
+
+ #
+ # Renames a file on the server.
+ #
+ def rename(fromname, toname)
+ resp = sendcmd("RNFR " + fromname)
+ if resp[0] != ?3
+ raise FTPReplyError, resp
+ end
+ voidcmd("RNTO " + toname)
+ end
+
+ #
+ # Deletes a file on the server.
+ #
+ def delete(filename)
+ resp = sendcmd("DELE " + filename)
+ if resp[0, 3] == "250"
+ return
+ elsif resp[0] == ?5
+ raise FTPPermError, resp
+ else
+ raise FTPReplyError, resp
+ end
+ end
+
+ #
+ # Changes the (remote) directory.
+ #
+ def chdir(dirname)
+ if dirname == ".."
+ begin
+ voidcmd("CDUP")
+ return
+ rescue FTPPermError => e
+ if e.message[0, 3] != "500"
+ raise e
+ end
+ end
+ end
+ cmd = "CWD " + dirname
+ voidcmd(cmd)
+ end
+
+ #
+ # Returns the size of the given (remote) filename.
+ #
+ def size(filename)
+ with_binary(true) do
+ resp = sendcmd("SIZE " + filename)
+ if resp[0, 3] != "213"
+ raise FTPReplyError, resp
+ end
+ return resp[3..-1].strip.to_i
+ end
+ end
+
+ MDTM_REGEXP = /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/ # :nodoc:
+
+ #
+ # Returns the last modification time of the (remote) file. If +local+ is
+ # +true+, it is returned as a local time, otherwise it's a UTC time.
+ #
+ def mtime(filename, local = false)
+ str = mdtm(filename)
+ ary = str.scan(MDTM_REGEXP)[0].collect {|i| i.to_i}
+ return local ? Time.local(*ary) : Time.gm(*ary)
+ end
+
+ #
+ # Creates a remote directory.
+ #
+ def mkdir(dirname)
+ resp = sendcmd("MKD " + dirname)
+ return parse257(resp)
+ end
+
+ #
+ # Removes a remote directory.
+ #
+ def rmdir(dirname)
+ voidcmd("RMD " + dirname)
+ end
+
+ #
+ # Returns the current remote directory.
+ #
+ def pwd
+ resp = sendcmd("PWD")
+ return parse257(resp)
+ end
+ alias getdir pwd
+
+ #
+ # Returns system information.
+ #
+ def system
+ resp = sendcmd("SYST")
+ if resp[0, 3] != "215"
+ raise FTPReplyError, resp
+ end
+ return resp[4 .. -1]
+ end
+
+ #
+ # Aborts the previous command (ABOR command).
+ #
+ def abort
+ line = "ABOR" + CRLF
+ print "put: ABOR\n" if @debug_mode
+ @sock.send(line, Socket::MSG_OOB)
+ resp = getmultiline
+ unless ["426", "226", "225"].include?(resp[0, 3])
+ raise FTPProtoError, resp
+ end
+ return resp
+ end
+
+ #
+ # Returns the status (STAT command).
+ #
+ def status
+ line = "STAT" + CRLF
+ print "put: STAT\n" if @debug_mode
+ @sock.send(line, Socket::MSG_OOB)
+ return getresp
+ end
+
+ #
+ # Issues the MDTM command. TODO: more info.
+ #
+ def mdtm(filename)
+ resp = sendcmd("MDTM " + filename)
+ if resp[0, 3] == "213"
+ return resp[3 .. -1].strip
+ end
+ end
+
+ #
+ # Issues the HELP command.
+ #
+ def help(arg = nil)
+ cmd = "HELP"
+ if arg
+ cmd = cmd + " " + arg
+ end
+ sendcmd(cmd)
+ end
+
+ #
+ # Exits the FTP session.
+ #
+ def quit
+ voidcmd("QUIT")
+ end
+
+ #
+ # Issues a NOOP command.
+ #
+ def noop
+ voidcmd("NOOP")
+ end
+
+ #
+ # Issues a SITE command.
+ #
+ def site(arg)
+ cmd = "SITE " + arg
+ voidcmd(cmd)
+ end
+
+ #
+ # Closes the connection. Further operations are impossible until you open
+ # a new connection with #connect.
+ #
+ def close
+ @sock.close if @sock and not @sock.closed?
+ end
+
+ #
+ # Returns +true+ iff the connection is closed.
+ #
+ def closed?
+ @sock == nil or @sock.closed?
+ end
+
+ def parse227(resp)
+ if resp[0, 3] != "227"
+ raise FTPReplyError, resp
+ end
+ left = resp.index("(")
+ right = resp.index(")")
+ if left == nil or right == nil
+ raise FTPProtoError, resp
+ end
+ numbers = resp[left + 1 .. right - 1].split(",")
+ if numbers.length != 6
+ raise FTPProtoError, resp
+ end
+ host = numbers[0, 4].join(".")
+ port = (numbers[4].to_i << 8) + numbers[5].to_i
+ return host, port
+ end
+ private :parse227
+
+ def parse228(resp)
+ if resp[0, 3] != "228"
+ raise FTPReplyError, resp
+ end
+ left = resp.index("(")
+ right = resp.index(")")
+ if left == nil or right == nil
+ raise FTPProtoError, resp
+ end
+ numbers = resp[left + 1 .. right - 1].split(",")
+ if numbers[0] == "4"
+ if numbers.length != 9 || numbers[1] != "4" || numbers[2 + 4] != "2"
+ raise FTPProtoError, resp
+ end
+ host = numbers[2, 4].join(".")
+ port = (numbers[7].to_i << 8) + numbers[8].to_i
+ elsif numbers[0] == "6"
+ if numbers.length != 21 || numbers[1] != "16" || numbers[2 + 16] != "2"
+ raise FTPProtoError, resp
+ end
+ v6 = ["", "", "", "", "", "", "", ""]
+ for i in 0 .. 7
+ v6[i] = sprintf("%02x%02x", numbers[(i * 2) + 2].to_i,
+ numbers[(i * 2) + 3].to_i)
+ end
+ host = v6[0, 8].join(":")
+ port = (numbers[19].to_i << 8) + numbers[20].to_i
+ end
+ return host, port
+ end
+ private :parse228
+
+ def parse229(resp)
+ if resp[0, 3] != "229"
+ raise FTPReplyError, resp
+ end
+ left = resp.index("(")
+ right = resp.index(")")
+ if left == nil or right == nil
+ raise FTPProtoError, resp
+ end
+ numbers = resp[left + 1 .. right - 1].split(resp[left + 1, 1])
+ if numbers.length != 4
+ raise FTPProtoError, resp
+ end
+ port = numbers[3].to_i
+ host = (@sock.peeraddr())[3]
+ return host, port
+ end
+ private :parse229
+
+ def parse257(resp)
+ if resp[0, 3] != "257"
+ raise FTPReplyError, resp
+ end
+ if resp[3, 2] != ' "'
+ return ""
+ end
+ dirname = ""
+ i = 5
+ n = resp.length
+ while i < n
+ c = resp[i, 1]
+ i = i + 1
+ if c == '"'
+ if i > n or resp[i, 1] != '"'
+ break
+ end
+ i = i + 1
+ end
+ dirname = dirname + c
+ end
+ return dirname
+ end
+ private :parse257
+ end
+
+end
+
+
+# Documentation comments:
+# - sourced from pickaxe and nutshell, with improvements (hopefully)
+# - three methods should be private (search WRITEME)
+# - two methods need more information (search TODO)
diff --git a/ruby/lib/net/http.rb b/ruby/lib/net/http.rb
new file mode 100644
index 0000000..7e35290
--- /dev/null
+++ b/ruby/lib/net/http.rb
@@ -0,0 +1,2399 @@
+#
+# = net/http.rb
+#
+# Copyright (c) 1999-2007 Yukihiro Matsumoto
+# Copyright (c) 1999-2007 Minero Aoki
+# Copyright (c) 2001 GOTOU Yuuzou
+#
+# Written and maintained by Minero Aoki <aamine@loveruby.net>.
+# HTTPS support added by GOTOU Yuuzou <gotoyuzo@notwork.org>.
+#
+# This file is derived from "http-access.rb".
+#
+# Documented by Minero Aoki; converted to RDoc by William Webber.
+#
+# This program is free software. You can re-distribute and/or
+# modify this program under the same terms of ruby itself ---
+# Ruby Distribution License or GNU General Public License.
+#
+# See Net::HTTP for an overview and examples.
+#
+# NOTE: You can find Japanese version of this document here:
+# http://www.ruby-lang.org/ja/man/html/net_http.html
+#
+#--
+# $Id: http.rb 25620 2009-11-01 15:48:31Z yugui $
+#++
+
+require 'net/protocol'
+require 'uri'
+
+module Net #:nodoc:
+
+ # :stopdoc:
+ class HTTPBadResponse < StandardError; end
+ class HTTPHeaderSyntaxError < StandardError; end
+ # :startdoc:
+
+ # == What Is This Library?
+ #
+ # This library provides your program functions to access WWW
+ # documents via HTTP, Hyper Text Transfer Protocol version 1.1.
+ # For details of HTTP, refer [RFC2616]
+ # (http://www.ietf.org/rfc/rfc2616.txt).
+ #
+ # == Examples
+ #
+ # === Getting Document From WWW Server
+ #
+ # Example #1: Simple GET+print
+ #
+ # require 'net/http'
+ # Net::HTTP.get_print 'www.example.com', '/index.html'
+ #
+ # Example #2: Simple GET+print by URL
+ #
+ # require 'net/http'
+ # require 'uri'
+ # Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
+ #
+ # Example #3: More generic GET+print
+ #
+ # require 'net/http'
+ # require 'uri'
+ #
+ # url = URI.parse('http://www.example.com/index.html')
+ # res = Net::HTTP.start(url.host, url.port) {|http|
+ # http.get('/index.html')
+ # }
+ # puts res.body
+ #
+ # Example #4: More generic GET+print
+ #
+ # require 'net/http'
+ #
+ # url = URI.parse('http://www.example.com/index.html')
+ # req = Net::HTTP::Get.new(url.path)
+ # res = Net::HTTP.start(url.host, url.port) {|http|
+ # http.request(req)
+ # }
+ # puts res.body
+ #
+ # === Posting Form Data
+ #
+ # require 'net/http'
+ # require 'uri'
+ #
+ # #1: Simple POST
+ # res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
+ # {'q' => 'ruby', 'max' => '50'})
+ # puts res.body
+ #
+ # #2: POST with basic authentication
+ # res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'),
+ # {'from' => '2005-01-01',
+ # 'to' => '2005-03-31'})
+ # puts res.body
+ #
+ # #3: Detailed control
+ # url = URI.parse('http://www.example.com/todo.cgi')
+ # req = Net::HTTP::Post.new(url.path)
+ # req.basic_auth 'jack', 'pass'
+ # req.set_form_data({'from' => '2005-01-01', 'to' => '2005-03-31'}, ';')
+ # res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
+ # case res
+ # when Net::HTTPSuccess, Net::HTTPRedirection
+ # # OK
+ # else
+ # res.error!
+ # end
+ #
+ # #4: Multiple values
+ # res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
+ # {'q' => ['ruby', 'perl'], 'max' => '50'})
+ # puts res.body
+ #
+ # === Accessing via Proxy
+ #
+ # Net::HTTP.Proxy creates http proxy class. It has same
+ # methods of Net::HTTP but its instances always connect to
+ # proxy, instead of given host.
+ #
+ # require 'net/http'
+ #
+ # proxy_addr = 'your.proxy.host'
+ # proxy_port = 8080
+ # :
+ # Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') {|http|
+ # # always connect to your.proxy.addr:8080
+ # :
+ # }
+ #
+ # Since Net::HTTP.Proxy returns Net::HTTP itself when proxy_addr is nil,
+ # there's no need to change code if there's proxy or not.
+ #
+ # There are two additional parameters in Net::HTTP.Proxy which allow to
+ # specify proxy user name and password:
+ #
+ # Net::HTTP::Proxy(proxy_addr, proxy_port, proxy_user = nil, proxy_pass = nil)
+ #
+ # You may use them to work with authorization-enabled proxies:
+ #
+ # require 'net/http'
+ # require 'uri'
+ #
+ # proxy_host = 'your.proxy.host'
+ # proxy_port = 8080
+ # uri = URI.parse(ENV['http_proxy'])
+ # proxy_user, proxy_pass = uri.userinfo.split(/:/) if uri.userinfo
+ # Net::HTTP::Proxy(proxy_host, proxy_port,
+ # proxy_user, proxy_pass).start('www.example.com') {|http|
+ # # always connect to your.proxy.addr:8080 using specified username and password
+ # :
+ # }
+ #
+ # Note that net/http never rely on HTTP_PROXY environment variable.
+ # If you want to use proxy, set it explicitly.
+ #
+ # === Following Redirection
+ #
+ # require 'net/http'
+ # require 'uri'
+ #
+ # def fetch(uri_str, limit = 10)
+ # # You should choose better exception.
+ # raise ArgumentError, 'HTTP redirect too deep' if limit == 0
+ #
+ # response = Net::HTTP.get_response(URI.parse(uri_str))
+ # case response
+ # when Net::HTTPSuccess then response
+ # when Net::HTTPRedirection then fetch(response['location'], limit - 1)
+ # else
+ # response.error!
+ # end
+ # end
+ #
+ # print fetch('http://www.ruby-lang.org')
+ #
+ # Net::HTTPSuccess and Net::HTTPRedirection is a HTTPResponse class.
+ # All HTTPResponse objects belong to its own response class which
+ # indicate HTTP result status. For details of response classes,
+ # see section "HTTP Response Classes".
+ #
+ # === Basic Authentication
+ #
+ # require 'net/http'
+ #
+ # Net::HTTP.start('www.example.com') {|http|
+ # req = Net::HTTP::Get.new('/secret-page.html')
+ # req.basic_auth 'account', 'password'
+ # response = http.request(req)
+ # print response.body
+ # }
+ #
+ # === HTTP Request Classes
+ #
+ # Here is HTTP request class hierarchy.
+ #
+ # Net::HTTPRequest
+ # Net::HTTP::Get
+ # Net::HTTP::Head
+ # Net::HTTP::Post
+ # Net::HTTP::Put
+ # Net::HTTP::Proppatch
+ # Net::HTTP::Lock
+ # Net::HTTP::Unlock
+ # Net::HTTP::Options
+ # Net::HTTP::Propfind
+ # Net::HTTP::Delete
+ # Net::HTTP::Move
+ # Net::HTTP::Copy
+ # Net::HTTP::Mkcol
+ # Net::HTTP::Trace
+ #
+ # === HTTP Response Classes
+ #
+ # Here is HTTP response class hierarchy.
+ # All classes are defined in Net module.
+ #
+ # HTTPResponse
+ # HTTPUnknownResponse
+ # HTTPInformation # 1xx
+ # HTTPContinue # 100
+ # HTTPSwitchProtocl # 101
+ # HTTPSuccess # 2xx
+ # HTTPOK # 200
+ # HTTPCreated # 201
+ # HTTPAccepted # 202
+ # HTTPNonAuthoritativeInformation # 203
+ # HTTPNoContent # 204
+ # HTTPResetContent # 205
+ # HTTPPartialContent # 206
+ # HTTPRedirection # 3xx
+ # HTTPMultipleChoice # 300
+ # HTTPMovedPermanently # 301
+ # HTTPFound # 302
+ # HTTPSeeOther # 303
+ # HTTPNotModified # 304
+ # HTTPUseProxy # 305
+ # HTTPTemporaryRedirect # 307
+ # HTTPClientError # 4xx
+ # HTTPBadRequest # 400
+ # HTTPUnauthorized # 401
+ # HTTPPaymentRequired # 402
+ # HTTPForbidden # 403
+ # HTTPNotFound # 404
+ # HTTPMethodNotAllowed # 405
+ # HTTPNotAcceptable # 406
+ # HTTPProxyAuthenticationRequired # 407
+ # HTTPRequestTimeOut # 408
+ # HTTPConflict # 409
+ # HTTPGone # 410
+ # HTTPLengthRequired # 411
+ # HTTPPreconditionFailed # 412
+ # HTTPRequestEntityTooLarge # 413
+ # HTTPRequestURITooLong # 414
+ # HTTPUnsupportedMediaType # 415
+ # HTTPRequestedRangeNotSatisfiable # 416
+ # HTTPExpectationFailed # 417
+ # HTTPServerError # 5xx
+ # HTTPInternalServerError # 500
+ # HTTPNotImplemented # 501
+ # HTTPBadGateway # 502
+ # HTTPServiceUnavailable # 503
+ # HTTPGatewayTimeOut # 504
+ # HTTPVersionNotSupported # 505
+ #
+ # == Switching Net::HTTP versions
+ #
+ # You can use net/http.rb 1.1 features (bundled with Ruby 1.6)
+ # by calling HTTP.version_1_1. Calling Net::HTTP.version_1_2
+ # allows you to use 1.2 features again.
+ #
+ # # example
+ # Net::HTTP.start {|http1| ...(http1 has 1.2 features)... }
+ #
+ # Net::HTTP.version_1_1
+ # Net::HTTP.start {|http2| ...(http2 has 1.1 features)... }
+ #
+ # Net::HTTP.version_1_2
+ # Net::HTTP.start {|http3| ...(http3 has 1.2 features)... }
+ #
+ # This function is NOT thread-safe.
+ #
+ class HTTP < Protocol
+
+ # :stopdoc:
+ Revision = %q$Revision: 25620 $.split[1]
+ HTTPVersion = '1.1'
+ @newimpl = true
+ begin
+ require 'zlib'
+ require 'stringio' #for our purposes (unpacking gzip) lump these together
+ HAVE_ZLIB=true
+ rescue LoadError
+ HAVE_ZLIB=false
+ end
+ # :startdoc:
+
+ # Turns on net/http 1.2 (ruby 1.8) features.
+ # Defaults to ON in ruby 1.8.
+ #
+ # I strongly recommend to call this method always.
+ #
+ # require 'net/http'
+ # Net::HTTP.version_1_2
+ #
+ def HTTP.version_1_2
+ @newimpl = true
+ end
+
+ # Turns on net/http 1.1 (ruby 1.6) features.
+ # Defaults to OFF in ruby 1.8.
+ def HTTP.version_1_1
+ @newimpl = false
+ end
+
+ # true if net/http is in version 1.2 mode.
+ # Defaults to true.
+ def HTTP.version_1_2?
+ @newimpl
+ end
+
+ # true if net/http is in version 1.1 compatible mode.
+ # Defaults to true.
+ def HTTP.version_1_1?
+ not @newimpl
+ end
+
+ class << HTTP
+ alias is_version_1_1? version_1_1? #:nodoc:
+ alias is_version_1_2? version_1_2? #:nodoc:
+ end
+
+ #
+ # short cut methods
+ #
+
+ #
+ # Get body from target and output it to +$stdout+. The
+ # target can either be specified as (+uri+), or as
+ # (+host+, +path+, +port+ = 80); so:
+ #
+ # Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
+ #
+ # or:
+ #
+ # Net::HTTP.get_print 'www.example.com', '/index.html'
+ #
+ def HTTP.get_print(uri_or_host, path = nil, port = nil)
+ get_response(uri_or_host, path, port) {|res|
+ res.read_body do |chunk|
+ $stdout.print chunk
+ end
+ }
+ nil
+ end
+
+ # Send a GET request to the target and return the response
+ # as a string. The target can either be specified as
+ # (+uri+), or as (+host+, +path+, +port+ = 80); so:
+ #
+ # print Net::HTTP.get(URI.parse('http://www.example.com/index.html'))
+ #
+ # or:
+ #
+ # print Net::HTTP.get('www.example.com', '/index.html')
+ #
+ def HTTP.get(uri_or_host, path = nil, port = nil)
+ get_response(uri_or_host, path, port).body
+ end
+
+ # Send a GET request to the target and return the response
+ # as a Net::HTTPResponse object. The target can either be specified as
+ # (+uri+), or as (+host+, +path+, +port+ = 80); so:
+ #
+ # res = Net::HTTP.get_response(URI.parse('http://www.example.com/index.html'))
+ # print res.body
+ #
+ # or:
+ #
+ # res = Net::HTTP.get_response('www.example.com', '/index.html')
+ # print res.body
+ #
+ def HTTP.get_response(uri_or_host, path = nil, port = nil, &block)
+ if path
+ host = uri_or_host
+ new(host, port || HTTP.default_port).start {|http|
+ return http.request_get(path, &block)
+ }
+ else
+ uri = uri_or_host
+ new(uri.host, uri.port).start {|http|
+ return http.request_get(uri.request_uri, &block)
+ }
+ end
+ end
+
+ # Posts HTML form data to the +URL+.
+ # Form data must be represented as a Hash of String to String, e.g:
+ #
+ # { "cmd" => "search", "q" => "ruby", "max" => "50" }
+ #
+ # This method also does Basic Authentication iff +URL+.user exists.
+ #
+ # Example:
+ #
+ # require 'net/http'
+ # require 'uri'
+ #
+ # HTTP.post_form URI.parse('http://www.example.com/search.cgi'),
+ # { "q" => "ruby", "max" => "50" }
+ #
+ def HTTP.post_form(url, params)
+ req = Post.new(url.path)
+ req.form_data = params
+ req.basic_auth url.user, url.password if url.user
+ new(url.host, url.port).start {|http|
+ http.request(req)
+ }
+ end
+
+ #
+ # HTTP session management
+ #
+
+ # The default port to use for HTTP requests; defaults to 80.
+ def HTTP.default_port
+ http_default_port()
+ end
+
+ # The default port to use for HTTP requests; defaults to 80.
+ def HTTP.http_default_port
+ 80
+ end
+
+ # The default port to use for HTTPS requests; defaults to 443.
+ def HTTP.https_default_port
+ 443
+ end
+
+ def HTTP.socket_type #:nodoc: obsolete
+ BufferedIO
+ end
+
+ # creates a new Net::HTTP object and opens its TCP connection and
+ # HTTP session. If the optional block is given, the newly
+ # created Net::HTTP object is passed to it and closed when the
+ # block finishes. In this case, the return value of this method
+ # is the return value of the block. If no block is given, the
+ # return value of this method is the newly created Net::HTTP object
+ # itself, and the caller is responsible for closing it upon completion.
+ def HTTP.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) # :yield: +http+
+ new(address, port, p_addr, p_port, p_user, p_pass).start(&block)
+ end
+
+ class << HTTP
+ alias newobj new
+ end
+
+ # Creates a new Net::HTTP object.
+ # If +proxy_addr+ is given, creates an Net::HTTP object with proxy support.
+ # This method does not open the TCP connection.
+ def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)
+ h = Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port)
+ h.instance_eval {
+ @newimpl = ::Net::HTTP.version_1_2?
+ }
+ h
+ end
+
+ # Creates a new Net::HTTP object for the specified +address+.
+ # This method does not open the TCP connection.
+ def initialize(address, port = nil)
+ @address = address
+ @port = (port || HTTP.default_port)
+ @curr_http_version = HTTPVersion
+ @no_keepalive_server = false
+ @close_on_empty_response = false
+ @socket = nil
+ @started = false
+ @open_timeout = nil
+ @read_timeout = 60
+ @debug_output = nil
+ @use_ssl = false
+ @ssl_context = nil
+ @enable_post_connection_check = true
+ @compression = nil
+ @sspi_enabled = false
+ if defined?(SSL_ATTRIBUTES)
+ SSL_ATTRIBUTES.each do |name|
+ instance_variable_set "@#{name}", nil
+ end
+ end
+ end
+
+ def inspect
+ "#<#{self.class} #{@address}:#{@port} open=#{started?}>"
+ end
+
+ # *WARNING* This method causes serious security hole.
+ # Never use this method in production code.
+ #
+ # Set an output stream for debugging.
+ #
+ # http = Net::HTTP.new
+ # http.set_debug_output $stderr
+ # http.start { .... }
+ #
+ def set_debug_output(output)
+ warn 'Net::HTTP#set_debug_output called after HTTP started' if started?
+ @debug_output = output
+ end
+
+ # The host name to connect to.
+ attr_reader :address
+
+ # The port number to connect to.
+ attr_reader :port
+
+ # Seconds to wait until connection is opened.
+ # If the HTTP object cannot open a connection in this many seconds,
+ # it raises a TimeoutError exception.
+ attr_accessor :open_timeout
+
+ # Seconds to wait until reading one block (by one read(2) call).
+ # If the HTTP object cannot open a connection in this many seconds,
+ # it raises a TimeoutError exception.
+ attr_reader :read_timeout
+
+ # Setter for the read_timeout attribute.
+ def read_timeout=(sec)
+ @socket.read_timeout = sec if @socket
+ @read_timeout = sec
+ end
+
+ # returns true if the HTTP session is started.
+ def started?
+ @started
+ end
+
+ alias active? started? #:nodoc: obsolete
+
+ attr_accessor :close_on_empty_response
+
+ # returns true if use SSL/TLS with HTTP.
+ def use_ssl?
+ false # redefined in net/https
+ end
+
+ # Opens TCP connection and HTTP session.
+ #
+ # When this method is called with block, gives a HTTP object
+ # to the block and closes the TCP connection / HTTP session
+ # after the block executed.
+ #
+ # When called with a block, returns the return value of the
+ # block; otherwise, returns self.
+ #
+ def start # :yield: http
+ raise IOError, 'HTTP session already opened' if @started
+ if block_given?
+ begin
+ do_start
+ return yield(self)
+ ensure
+ do_finish
+ end
+ end
+ do_start
+ self
+ end
+
+ def do_start
+ connect
+ @started = true
+ end
+ private :do_start
+
+ def connect
+ D "opening connection to #{conn_address()}..."
+ s = timeout(@open_timeout) { TCPSocket.open(conn_address(), conn_port()) }
+ D "opened"
+ if use_ssl?
+ ssl_parameters = Hash.new
+ SSL_ATTRIBUTES.each do |name|
+ if value = instance_variable_get("@#{name}")
+ ssl_parameters[name] = value
+ end
+ end
+ @ssl_context = OpenSSL::SSL::SSLContext.new
+ @ssl_context.set_params(ssl_parameters)
+ s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
+ s.sync_close = true
+ end
+ @socket = BufferedIO.new(s)
+ @socket.read_timeout = @read_timeout
+ @socket.debug_output = @debug_output
+ if use_ssl?
+ if proxy?
+ @socket.writeline sprintf('CONNECT %s:%s HTTP/%s',
+ @address, @port, HTTPVersion)
+ @socket.writeline "Host: #{@address}:#{@port}"
+ if proxy_user
+ credential = ["#{proxy_user}:#{proxy_pass}"].pack('m')
+ credential.delete!("\r\n")
+ @socket.writeline "Proxy-Authorization: Basic #{credential}"
+ end
+ @socket.writeline ''
+ HTTPResponse.read_new(@socket).value
+ end
+ s.connect
+ if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
+ s.post_connection_check(@address)
+ end
+ end
+ on_connect
+ end
+ private :connect
+
+ def on_connect
+ end
+ private :on_connect
+
+ # Finishes HTTP session and closes TCP connection.
+ # Raises IOError if not started.
+ def finish
+ raise IOError, 'HTTP session not yet started' unless started?
+ do_finish
+ end
+
+ def do_finish
+ @started = false
+ @socket.close if @socket and not @socket.closed?
+ @socket = nil
+ end
+ private :do_finish
+
+ #
+ # proxy
+ #
+
+ public
+
+ # no proxy
+ @is_proxy_class = false
+ @proxy_addr = nil
+ @proxy_port = nil
+ @proxy_user = nil
+ @proxy_pass = nil
+
+ # Creates an HTTP proxy class.
+ # Arguments are address/port of proxy host and username/password
+ # if authorization on proxy server is required.
+ # You can replace the HTTP class with created proxy class.
+ #
+ # If ADDRESS is nil, this method returns self (Net::HTTP).
+ #
+ # # Example
+ # proxy_class = Net::HTTP::Proxy('proxy.example.com', 8080)
+ # :
+ # proxy_class.start('www.ruby-lang.org') {|http|
+ # # connecting proxy.foo.org:8080
+ # :
+ # }
+ #
+ def HTTP.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
+ return self unless p_addr
+ delta = ProxyDelta
+ proxyclass = Class.new(self)
+ proxyclass.module_eval {
+ include delta
+ # with proxy
+ @is_proxy_class = true
+ @proxy_address = p_addr
+ @proxy_port = p_port || default_port()
+ @proxy_user = p_user
+ @proxy_pass = p_pass
+ }
+ proxyclass
+ end
+
+ class << HTTP
+ # returns true if self is a class which was created by HTTP::Proxy.
+ def proxy_class?
+ @is_proxy_class
+ end
+
+ attr_reader :proxy_address
+ attr_reader :proxy_port
+ attr_reader :proxy_user
+ attr_reader :proxy_pass
+ end
+
+ # True if self is a HTTP proxy class.
+ def proxy?
+ self.class.proxy_class?
+ end
+
+ # Address of proxy host. If self does not use a proxy, nil.
+ def proxy_address
+ self.class.proxy_address
+ end
+
+ # Port number of proxy host. If self does not use a proxy, nil.
+ def proxy_port
+ self.class.proxy_port
+ end
+
+ # User name for accessing proxy. If self does not use a proxy, nil.
+ def proxy_user
+ self.class.proxy_user
+ end
+
+ # User password for accessing proxy. If self does not use a proxy, nil.
+ def proxy_pass
+ self.class.proxy_pass
+ end
+
+ alias proxyaddr proxy_address #:nodoc: obsolete
+ alias proxyport proxy_port #:nodoc: obsolete
+
+ private
+
+ # without proxy
+
+ def conn_address
+ address()
+ end
+
+ def conn_port
+ port()
+ end
+
+ def edit_path(path)
+ path
+ end
+
+ module ProxyDelta #:nodoc: internal use only
+ private
+
+ def conn_address
+ proxy_address()
+ end
+
+ def conn_port
+ proxy_port()
+ end
+
+ def edit_path(path)
+ use_ssl? ? path : "http://#{addr_port()}#{path}"
+ end
+ end
+
+ #
+ # HTTP operations
+ #
+
+ public
+
+ # Gets data from +path+ on the connected-to host.
+ # +initheader+ must be a Hash like { 'Accept' => '*/*', ... },
+ # and it defaults to an empty hash.
+ # If +initheader+ doesn't have the key 'accept-encoding', then
+ # a value of "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" is used,
+ # so that gzip compression is used in preference to deflate
+ # compression, which is used in preference to no compression.
+ # Ruby doesn't have libraries to support the compress (Lempel-Ziv)
+ # compression, so that is not supported. The intent of this is
+ # to reduce bandwidth by default. If this routine sets up
+ # compression, then it does the decompression also, removing
+ # the header as well to prevent confusion. Otherwise
+ # it leaves the body as it found it.
+ #
+ # In version 1.1 (ruby 1.6), this method returns a pair of objects,
+ # a Net::HTTPResponse object and the entity body string.
+ # In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse
+ # object.
+ #
+ # If called with a block, yields each fragment of the
+ # entity body in turn as a string as it is read from
+ # the socket. Note that in this case, the returned response
+ # object will *not* contain a (meaningful) body.
+ #
+ # +dest+ argument is obsolete.
+ # It still works but you must not use it.
+ #
+ # In version 1.1, this method might raise an exception for
+ # 3xx (redirect). In this case you can get a HTTPResponse object
+ # by "anException.response".
+ #
+ # In version 1.2, this method never raises exception.
+ #
+ # # version 1.1 (bundled with Ruby 1.6)
+ # response, body = http.get('/index.html')
+ #
+ # # version 1.2 (bundled with Ruby 1.8 or later)
+ # response = http.get('/index.html')
+ #
+ # # using block
+ # File.open('result.txt', 'w') {|f|
+ # http.get('/~foo/') do |str|
+ # f.write str
+ # end
+ # }
+ #
+ def get(path, initheader = {}, dest = nil, &block) # :yield: +body_segment+
+ res = nil
+ if HAVE_ZLIB
+ unless initheader.keys.any?{|k| k.downcase == "accept-encoding"}
+ initheader["accept-encoding"] = "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
+ @compression = true
+ end
+ end
+ request(Get.new(path, initheader)) {|r|
+ if r.key?("content-encoding") and @compression
+ @compression = nil # Clear it till next set.
+ the_body = r.read_body dest, &block
+ case r["content-encoding"]
+ when "gzip"
+ r.body= Zlib::GzipReader.new(StringIO.new(the_body)).read
+ r.delete("content-encoding")
+ when "deflate"
+ r.body= Zlib::Inflate.inflate(the_body);
+ r.delete("content-encoding")
+ when "identity"
+ ; # nothing needed
+ else
+ ; # Don't do anything dramatic, unless we need to later
+ end
+ else
+ r.read_body dest, &block
+ end
+ res = r
+ }
+ unless @newimpl
+ res.value
+ return res, res.body
+ end
+
+ res
+ end
+
+ # Gets only the header from +path+ on the connected-to host.
+ # +header+ is a Hash like { 'Accept' => '*/*', ... }.
+ #
+ # This method returns a Net::HTTPResponse object.
+ #
+ # In version 1.1, this method might raise an exception for
+ # 3xx (redirect). On the case you can get a HTTPResponse object
+ # by "anException.response".
+ # In version 1.2, this method never raises an exception.
+ #
+ # response = nil
+ # Net::HTTP.start('some.www.server', 80) {|http|
+ # response = http.head('/index.html')
+ # }
+ # p response['content-type']
+ #
+ def head(path, initheader = nil)
+ res = request(Head.new(path, initheader))
+ res.value unless @newimpl
+ res
+ end
+
+ # Posts +data+ (must be a String) to +path+. +header+ must be a Hash
+ # like { 'Accept' => '*/*', ... }.
+ #
+ # In version 1.1 (ruby 1.6), this method returns a pair of objects, a
+ # Net::HTTPResponse object and an entity body string.
+ # In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse object.
+ #
+ # If called with a block, yields each fragment of the
+ # entity body in turn as a string as it are read from
+ # the socket. Note that in this case, the returned response
+ # object will *not* contain a (meaningful) body.
+ #
+ # +dest+ argument is obsolete.
+ # It still works but you must not use it.
+ #
+ # In version 1.1, this method might raise an exception for
+ # 3xx (redirect). In this case you can get an HTTPResponse object
+ # by "anException.response".
+ # In version 1.2, this method never raises exception.
+ #
+ # # version 1.1
+ # response, body = http.post('/cgi-bin/search.rb', 'query=foo')
+ #
+ # # version 1.2
+ # response = http.post('/cgi-bin/search.rb', 'query=foo')
+ #
+ # # using block
+ # File.open('result.txt', 'w') {|f|
+ # http.post('/cgi-bin/search.rb', 'query=foo') do |str|
+ # f.write str
+ # end
+ # }
+ #
+ # You should set Content-Type: header field for POST.
+ # If no Content-Type: field given, this method uses
+ # "application/x-www-form-urlencoded" by default.
+ #
+ def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
+ res = nil
+ request(Post.new(path, initheader), data) {|r|
+ r.read_body dest, &block
+ res = r
+ }
+ unless @newimpl
+ res.value
+ return res, res.body
+ end
+ res
+ end
+
+ def put(path, data, initheader = nil) #:nodoc:
+ res = request(Put.new(path, initheader), data)
+ res.value unless @newimpl
+ res
+ end
+
+ # Sends a PROPPATCH request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def proppatch(path, body, initheader = nil)
+ request(Proppatch.new(path, initheader), body)
+ end
+
+ # Sends a LOCK request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def lock(path, body, initheader = nil)
+ request(Lock.new(path, initheader), body)
+ end
+
+ # Sends a UNLOCK request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def unlock(path, body, initheader = nil)
+ request(Unlock.new(path, initheader), body)
+ end
+
+ # Sends a OPTIONS request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def options(path, initheader = nil)
+ request(Options.new(path, initheader))
+ end
+
+ # Sends a PROPFIND request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def propfind(path, body = nil, initheader = {'Depth' => '0'})
+ request(Propfind.new(path, initheader), body)
+ end
+
+ # Sends a DELETE request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def delete(path, initheader = {'Depth' => 'Infinity'})
+ request(Delete.new(path, initheader))
+ end
+
+ # Sends a MOVE request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def move(path, initheader = nil)
+ request(Move.new(path, initheader))
+ end
+
+ # Sends a COPY request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def copy(path, initheader = nil)
+ request(Copy.new(path, initheader))
+ end
+
+ # Sends a MKCOL request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def mkcol(path, body = nil, initheader = nil)
+ request(Mkcol.new(path, initheader), body)
+ end
+
+ # Sends a TRACE request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ def trace(path, initheader = nil)
+ request(Trace.new(path, initheader))
+ end
+
+ # Sends a GET request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ #
+ # When called with a block, yields an HTTPResponse object.
+ # The body of this response will not have been read yet;
+ # the caller can process it using HTTPResponse#read_body,
+ # if desired.
+ #
+ # Returns the response.
+ #
+ # This method never raises Net::* exceptions.
+ #
+ # response = http.request_get('/index.html')
+ # # The entity body is already read here.
+ # p response['content-type']
+ # puts response.body
+ #
+ # # using block
+ # http.request_get('/index.html') {|response|
+ # p response['content-type']
+ # response.read_body do |str| # read body now
+ # print str
+ # end
+ # }
+ #
+ def request_get(path, initheader = nil, &block) # :yield: +response+
+ request(Get.new(path, initheader), &block)
+ end
+
+ # Sends a HEAD request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ #
+ # Returns the response.
+ #
+ # This method never raises Net::* exceptions.
+ #
+ # response = http.request_head('/index.html')
+ # p response['content-type']
+ #
+ def request_head(path, initheader = nil, &block)
+ request(Head.new(path, initheader), &block)
+ end
+
+ # Sends a POST request to the +path+ and gets a response,
+ # as an HTTPResponse object.
+ #
+ # When called with a block, yields an HTTPResponse object.
+ # The body of this response will not have been read yet;
+ # the caller can process it using HTTPResponse#read_body,
+ # if desired.
+ #
+ # Returns the response.
+ #
+ # This method never raises Net::* exceptions.
+ #
+ # # example
+ # response = http.request_post('/cgi-bin/nice.rb', 'datadatadata...')
+ # p response.status
+ # puts response.body # body is already read
+ #
+ # # using block
+ # http.request_post('/cgi-bin/nice.rb', 'datadatadata...') {|response|
+ # p response.status
+ # p response['content-type']
+ # response.read_body do |str| # read body now
+ # print str
+ # end
+ # }
+ #
+ def request_post(path, data, initheader = nil, &block) # :yield: +response+
+ request Post.new(path, initheader), data, &block
+ end
+
+ def request_put(path, data, initheader = nil, &block) #:nodoc:
+ request Put.new(path, initheader), data, &block
+ end
+
+ alias get2 request_get #:nodoc: obsolete
+ alias head2 request_head #:nodoc: obsolete
+ alias post2 request_post #:nodoc: obsolete
+ alias put2 request_put #:nodoc: obsolete
+
+
+ # Sends an HTTP request to the HTTP server.
+ # This method also sends DATA string if DATA is given.
+ #
+ # Returns a HTTPResponse object.
+ #
+ # This method never raises Net::* exceptions.
+ #
+ # response = http.send_request('GET', '/index.html')
+ # puts response.body
+ #
+ def send_request(name, path, data = nil, header = nil)
+ r = HTTPGenericRequest.new(name,(data ? true : false),true,path,header)
+ request r, data
+ end
+
+ # Sends an HTTPRequest object REQUEST to the HTTP server.
+ # This method also sends DATA string if REQUEST is a post/put request.
+ # Giving DATA for get/head request causes ArgumentError.
+ #
+ # When called with a block, yields an HTTPResponse object.
+ # The body of this response will not have been read yet;
+ # the caller can process it using HTTPResponse#read_body,
+ # if desired.
+ #
+ # Returns a HTTPResponse object.
+ #
+ # This method never raises Net::* exceptions.
+ #
+ def request(req, body = nil, &block) # :yield: +response+
+ unless started?
+ start {
+ req['connection'] ||= 'close'
+ return request(req, body, &block)
+ }
+ end
+ if proxy_user()
+ req.proxy_basic_auth proxy_user(), proxy_pass() unless use_ssl?
+ end
+ req.set_body_internal body
+ res = transport_request(req, &block)
+ if sspi_auth?(res)
+ sspi_auth(req)
+ res = transport_request(req, &block)
+ end
+ res
+ end
+
+ private
+
+ def transport_request(req)
+ begin_transport req
+ req.exec @socket, @curr_http_version, edit_path(req.path)
+ begin
+ res = HTTPResponse.read_new(@socket)
+ end while res.kind_of?(HTTPContinue)
+ res.reading_body(@socket, req.response_body_permitted?) {
+ yield res if block_given?
+ }
+ end_transport req, res
+ res
+ end
+
+ def begin_transport(req)
+ connect if @socket.closed?
+ if not req.response_body_permitted? and @close_on_empty_response
+ req['connection'] ||= 'close'
+ end
+ req['host'] ||= addr_port()
+ end
+
+ def end_transport(req, res)
+ @curr_http_version = res.http_version
+ if @socket.closed?
+ D 'Conn socket closed'
+ elsif not res.body and @close_on_empty_response
+ D 'Conn close'
+ @socket.close
+ elsif keep_alive?(req, res)
+ D 'Conn keep-alive'
+ else
+ D 'Conn close'
+ @socket.close
+ end
+ end
+
+ def keep_alive?(req, res)
+ return false if req.connection_close?
+ if @curr_http_version <= '1.0'
+ res.connection_keep_alive?
+ else # HTTP/1.1 or later
+ not res.connection_close?
+ end
+ end
+
+ def sspi_auth?(res)
+ return false unless @sspi_enabled
+ if res.kind_of?(HTTPProxyAuthenticationRequired) and
+ proxy? and res["Proxy-Authenticate"].include?("Negotiate")
+ begin
+ require 'win32/sspi'
+ true
+ rescue LoadError
+ false
+ end
+ else
+ false
+ end
+ end
+
+ def sspi_auth(req)
+ n = Win32::SSPI::NegotiateAuth.new
+ req["Proxy-Authorization"] = "Negotiate #{n.get_initial_token}"
+ # Some versions of ISA will close the connection if this isn't present.
+ req["Connection"] = "Keep-Alive"
+ req["Proxy-Connection"] = "Keep-Alive"
+ res = transport_request(req)
+ authphrase = res["Proxy-Authenticate"] or return res
+ req["Proxy-Authorization"] = "Negotiate #{n.complete_authentication(authphrase)}"
+ rescue => err
+ raise HTTPAuthenticationError.new('HTTP authentication failed', err)
+ end
+
+ #
+ # utils
+ #
+
+ private
+
+ def addr_port
+ if use_ssl?
+ address() + (port == HTTP.https_default_port ? '' : ":#{port()}")
+ else
+ address() + (port == HTTP.http_default_port ? '' : ":#{port()}")
+ end
+ end
+
+ def D(msg)
+ return unless @debug_output
+ @debug_output << msg
+ @debug_output << "\n"
+ end
+
+ end
+
+ HTTPSession = HTTP
+
+
+ #
+ # Header module.
+ #
+ # Provides access to @header in the mixed-into class as a hash-like
+ # object, except with case-insensitive keys. Also provides
+ # methods for accessing commonly-used header values in a more
+ # convenient format.
+ #
+ module HTTPHeader
+
+ def initialize_http_header(initheader)
+ @header = {}
+ return unless initheader
+ initheader.each do |key, value|
+ warn "net/http: warning: duplicated HTTP header: #{key}" if key?(key) and $VERBOSE
+ @header[key.downcase] = [value.strip]
+ end
+ end
+
+ def size #:nodoc: obsolete
+ @header.size
+ end
+
+ alias length size #:nodoc: obsolete
+
+ # Returns the header field corresponding to the case-insensitive key.
+ # For example, a key of "Content-Type" might return "text/html"
+ def [](key)
+ a = @header[key.downcase] or return nil
+ a.join(', ')
+ end
+
+ # Sets the header field corresponding to the case-insensitive key.
+ def []=(key, val)
+ unless val
+ @header.delete key.downcase
+ return val
+ end
+ @header[key.downcase] = [val]
+ end
+
+ # [Ruby 1.8.3]
+ # Adds header field instead of replace.
+ # Second argument +val+ must be a String.
+ # See also #[]=, #[] and #get_fields.
+ #
+ # request.add_field 'X-My-Header', 'a'
+ # p request['X-My-Header'] #=> "a"
+ # p request.get_fields('X-My-Header') #=> ["a"]
+ # request.add_field 'X-My-Header', 'b'
+ # p request['X-My-Header'] #=> "a, b"
+ # p request.get_fields('X-My-Header') #=> ["a", "b"]
+ # request.add_field 'X-My-Header', 'c'
+ # p request['X-My-Header'] #=> "a, b, c"
+ # p request.get_fields('X-My-Header') #=> ["a", "b", "c"]
+ #
+ def add_field(key, val)
+ if @header.key?(key.downcase)
+ @header[key.downcase].push val
+ else
+ @header[key.downcase] = [val]
+ end
+ end
+
+ # [Ruby 1.8.3]
+ # Returns an array of header field strings corresponding to the
+ # case-insensitive +key+. This method allows you to get duplicated
+ # header fields without any processing. See also #[].
+ #
+ # p response.get_fields('Set-Cookie')
+ # #=> ["session=al98axx; expires=Fri, 31-Dec-1999 23:58:23",
+ # "query=rubyscript; expires=Fri, 31-Dec-1999 23:58:23"]
+ # p response['Set-Cookie']
+ # #=> "session=al98axx; expires=Fri, 31-Dec-1999 23:58:23, query=rubyscript; expires=Fri, 31-Dec-1999 23:58:23"
+ #
+ def get_fields(key)
+ return nil unless @header[key.downcase]
+ @header[key.downcase].dup
+ end
+
+ # Returns the header field corresponding to the case-insensitive key.
+ # Returns the default value +args+, or the result of the block, or
+ # raises an IndexErrror if there's no header field named +key+
+ # See Hash#fetch
+ def fetch(key, *args, &block) #:yield: +key+
+ a = @header.fetch(key.downcase, *args, &block)
+ a.kind_of?(Array) ? a.join(', ') : a
+ end
+
+ # Iterates for each header names and values.
+ def each_header #:yield: +key+, +value+
+ @header.each do |k,va|
+ yield k, va.join(', ')
+ end
+ end
+
+ alias each each_header
+
+ # Iterates for each header names.
+ def each_name(&block) #:yield: +key+
+ @header.each_key(&block)
+ end
+
+ alias each_key each_name
+
+ # Iterates for each capitalized header names.
+ def each_capitalized_name(&block) #:yield: +key+
+ @header.each_key do |k|
+ yield capitalize(k)
+ end
+ end
+
+ # Iterates for each header values.
+ def each_value #:yield: +value+
+ @header.each_value do |va|
+ yield va.join(', ')
+ end
+ end
+
+ # Removes a header field.
+ def delete(key)
+ @header.delete(key.downcase)
+ end
+
+ # true if +key+ header exists.
+ def key?(key)
+ @header.key?(key.downcase)
+ end
+
+ # Returns a Hash consist of header names and values.
+ def to_hash
+ @header.dup
+ end
+
+ # As for #each_header, except the keys are provided in capitalized form.
+ def each_capitalized
+ @header.each do |k,v|
+ yield capitalize(k), v.join(', ')
+ end
+ end
+
+ alias canonical_each each_capitalized
+
+ def capitalize(name)
+ name.split(/-/).map {|s| s.capitalize }.join('-')
+ end
+ private :capitalize
+
+ # Returns an Array of Range objects which represents Range: header field,
+ # or +nil+ if there is no such header.
+ def range
+ return nil unless @header['range']
+ self['Range'].split(/,/).map {|spec|
+ m = /bytes\s*=\s*(\d+)?\s*-\s*(\d+)?/i.match(spec) or
+ raise HTTPHeaderSyntaxError, "wrong Range: #{spec}"
+ d1 = m[1].to_i
+ d2 = m[2].to_i
+ if m[1] and m[2] then d1..d2
+ elsif m[1] then d1..-1
+ elsif m[2] then -d2..-1
+ else
+ raise HTTPHeaderSyntaxError, 'range is not specified'
+ end
+ }
+ end
+
+ # Set Range: header from Range (arg r) or beginning index and
+ # length from it (arg idx&len).
+ #
+ # req.range = (0..1023)
+ # req.set_range 0, 1023
+ #
+ def set_range(r, e = nil)
+ unless r
+ @header.delete 'range'
+ return r
+ end
+ r = (r...r+e) if e
+ case r
+ when Numeric
+ n = r.to_i
+ rangestr = (n > 0 ? "0-#{n-1}" : "-#{-n}")
+ when Range
+ first = r.first
+ last = r.last
+ last -= 1 if r.exclude_end?
+ if last == -1
+ rangestr = (first > 0 ? "#{first}-" : "-#{-first}")
+ else
+ raise HTTPHeaderSyntaxError, 'range.first is negative' if first < 0
+ raise HTTPHeaderSyntaxError, 'range.last is negative' if last < 0
+ raise HTTPHeaderSyntaxError, 'must be .first < .last' if first > last
+ rangestr = "#{first}-#{last}"
+ end
+ else
+ raise TypeError, 'Range/Integer is required'
+ end
+ @header['range'] = ["bytes=#{rangestr}"]
+ r
+ end
+
+ alias range= set_range
+
+ # Returns an Integer object which represents the Content-Length: header field
+ # or +nil+ if that field is not provided.
+ def content_length
+ return nil unless key?('Content-Length')
+ len = self['Content-Length'].slice(/\d+/) or
+ raise HTTPHeaderSyntaxError, 'wrong Content-Length format'
+ len.to_i
+ end
+
+ def content_length=(len)
+ unless len
+ @header.delete 'content-length'
+ return nil
+ end
+ @header['content-length'] = [len.to_i.to_s]
+ end
+
+ # Returns "true" if the "transfer-encoding" header is present and
+ # set to "chunked". This is an HTTP/1.1 feature, allowing the
+ # the content to be sent in "chunks" without at the outset
+ # stating the entire content length.
+ def chunked?
+ return false unless @header['transfer-encoding']
+ field = self['Transfer-Encoding']
+ (/(?:\A|[^\-\w])chunked(?![\-\w])/i =~ field) ? true : false
+ end
+
+ # Returns a Range object which represents Content-Range: header field.
+ # This indicates, for a partial entity body, where this fragment
+ # fits inside the full entity body, as range of byte offsets.
+ def content_range
+ return nil unless @header['content-range']
+ m = %r<bytes\s+(\d+)-(\d+)/(\d+|\*)>i.match(self['Content-Range']) or
+ raise HTTPHeaderSyntaxError, 'wrong Content-Range format'
+ m[1].to_i .. m[2].to_i + 1
+ end
+
+ # The length of the range represented in Content-Range: header.
+ def range_length
+ r = content_range() or return nil
+ r.end - r.begin
+ end
+
+ # Returns a content type string such as "text/html".
+ # This method returns nil if Content-Type: header field does not exist.
+ def content_type
+ return nil unless main_type()
+ if sub_type()
+ then "#{main_type()}/#{sub_type()}"
+ else main_type()
+ end
+ end
+
+ # Returns a content type string such as "text".
+ # This method returns nil if Content-Type: header field does not exist.
+ def main_type
+ return nil unless @header['content-type']
+ self['Content-Type'].split(';').first.to_s.split('/')[0].to_s.strip
+ end
+
+ # Returns a content type string such as "html".
+ # This method returns nil if Content-Type: header field does not exist
+ # or sub-type is not given (e.g. "Content-Type: text").
+ def sub_type
+ return nil unless @header['content-type']
+ main, sub = *self['Content-Type'].split(';').first.to_s.split('/')
+ return nil unless sub
+ sub.strip
+ end
+
+ # Returns content type parameters as a Hash as like
+ # {"charset" => "iso-2022-jp"}.
+ def type_params
+ result = {}
+ list = self['Content-Type'].to_s.split(';')
+ list.shift
+ list.each do |param|
+ k, v = *param.split('=', 2)
+ result[k.strip] = v.strip
+ end
+ result
+ end
+
+ # Set Content-Type: header field by +type+ and +params+.
+ # +type+ must be a String, +params+ must be a Hash.
+ def set_content_type(type, params = {})
+ @header['content-type'] = [type + params.map{|k,v|"; #{k}=#{v}"}.join('')]
+ end
+
+ alias content_type= set_content_type
+
+ # Set header fields and a body from HTML form data.
+ # +params+ should be a Hash containing HTML form data.
+ # Optional argument +sep+ means data record separator.
+ #
+ # This method also set Content-Type: header field to
+ # application/x-www-form-urlencoded.
+ #
+ # Example:
+ # http.form_data = {"q" => "ruby", "lang" => "en"}
+ # http.form_data = {"q" => ["ruby", "perl"], "lang" => "en"}
+ # http.set_form_data({"q" => "ruby", "lang" => "en"}, ';')
+ #
+ def set_form_data(params, sep = '&')
+ self.body = params.map {|k, v| encode_kvpair(k, v) }.flatten.join(sep)
+ self.content_type = 'application/x-www-form-urlencoded'
+ end
+
+ alias form_data= set_form_data
+
+ def encode_kvpair(k, vs)
+ Array(vs).map {|v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }
+ end
+ private :encode_kvpair
+
+ def urlencode(str)
+ str.dup.force_encoding('ASCII-8BIT').gsub(/[^a-zA-Z0-9_\.\-]/){'%%%02x' % $&.ord}
+ end
+ private :urlencode
+
+ # Set the Authorization: header for "Basic" authorization.
+ def basic_auth(account, password)
+ @header['authorization'] = [basic_encode(account, password)]
+ end
+
+ # Set Proxy-Authorization: header for "Basic" authorization.
+ def proxy_basic_auth(account, password)
+ @header['proxy-authorization'] = [basic_encode(account, password)]
+ end
+
+ def basic_encode(account, password)
+ 'Basic ' + ["#{account}:#{password}"].pack('m').delete("\r\n")
+ end
+ private :basic_encode
+
+ def connection_close?
+ tokens(@header['connection']).include?('close') or
+ tokens(@header['proxy-connection']).include?('close')
+ end
+
+ def connection_keep_alive?
+ tokens(@header['connection']).include?('keep-alive') or
+ tokens(@header['proxy-connection']).include?('keep-alive')
+ end
+
+ def tokens(vals)
+ return [] unless vals
+ vals.map {|v| v.split(',') }.flatten\
+ .reject {|str| str.strip.empty? }\
+ .map {|tok| tok.strip.downcase }
+ end
+ private :tokens
+
+ end
+
+
+ #
+ # Parent of HTTPRequest class. Do not use this directly; use
+ # a subclass of HTTPRequest.
+ #
+ # Mixes in the HTTPHeader module.
+ #
+ class HTTPGenericRequest
+
+ include HTTPHeader
+
+ def initialize(m, reqbody, resbody, path, initheader = nil)
+ @method = m
+ @request_has_body = reqbody
+ @response_has_body = resbody
+ raise ArgumentError, "no HTTP request path given" unless path
+ raise ArgumentError, "HTTP request path is empty" if path.empty?
+ @path = path
+ initialize_http_header initheader
+ self['Accept'] ||= '*/*'
+ self['User-Agent'] ||= 'Ruby'
+ @body = nil
+ @body_stream = nil
+ end
+
+ attr_reader :method
+ attr_reader :path
+
+ def inspect
+ "\#<#{self.class} #{@method}>"
+ end
+
+ def request_body_permitted?
+ @request_has_body
+ end
+
+ def response_body_permitted?
+ @response_has_body
+ end
+
+ def body_exist?
+ warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?" if $VERBOSE
+ response_body_permitted?
+ end
+
+ attr_reader :body
+
+ def body=(str)
+ @body = str
+ @body_stream = nil
+ str
+ end
+
+ attr_reader :body_stream
+
+ def body_stream=(input)
+ @body = nil
+ @body_stream = input
+ input
+ end
+
+ def set_body_internal(str) #:nodoc: internal use only
+ raise ArgumentError, "both of body argument and HTTPRequest#body set" if str and (@body or @body_stream)
+ self.body = str if str
+ end
+
+ #
+ # write
+ #
+
+ def exec(sock, ver, path) #:nodoc: internal use only
+ if @body
+ send_request_with_body sock, ver, path, @body
+ elsif @body_stream
+ send_request_with_body_stream sock, ver, path, @body_stream
+ else
+ write_header sock, ver, path
+ end
+ end
+
+ private
+
+ def send_request_with_body(sock, ver, path, body)
+ self.content_length = body.bytesize
+ delete 'Transfer-Encoding'
+ supply_default_content_type
+ write_header sock, ver, path
+ sock.write body
+ end
+
+ def send_request_with_body_stream(sock, ver, path, f)
+ unless content_length() or chunked?
+ raise ArgumentError,
+ "Content-Length not given and Transfer-Encoding is not `chunked'"
+ end
+ supply_default_content_type
+ write_header sock, ver, path
+ if chunked?
+ while s = f.read(1024)
+ sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
+ end
+ sock.write "0\r\n\r\n"
+ else
+ while s = f.read(1024)
+ sock.write s
+ end
+ end
+ end
+
+ def supply_default_content_type
+ return if content_type()
+ warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
+ set_content_type 'application/x-www-form-urlencoded'
+ end
+
+ def write_header(sock, ver, path)
+ buf = "#{@method} #{path} HTTP/#{ver}\r\n"
+ each_capitalized do |k,v|
+ buf << "#{k}: #{v}\r\n"
+ end
+ buf << "\r\n"
+ sock.write buf
+ end
+
+ end
+
+
+ #
+ # HTTP request class. This class wraps request header and entity path.
+ # You *must* use its subclass, Net::HTTP::Get, Post, Head.
+ #
+ class HTTPRequest < HTTPGenericRequest
+
+ # Creates HTTP request object.
+ def initialize(path, initheader = nil)
+ super self.class::METHOD,
+ self.class::REQUEST_HAS_BODY,
+ self.class::RESPONSE_HAS_BODY,
+ path, initheader
+ end
+ end
+
+
+ class HTTP # reopen
+ #
+ # HTTP 1.1 methods --- RFC2616
+ #
+
+ class Get < HTTPRequest
+ METHOD = 'GET'
+ REQUEST_HAS_BODY = false
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Head < HTTPRequest
+ METHOD = 'HEAD'
+ REQUEST_HAS_BODY = false
+ RESPONSE_HAS_BODY = false
+ end
+
+ class Post < HTTPRequest
+ METHOD = 'POST'
+ REQUEST_HAS_BODY = true
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Put < HTTPRequest
+ METHOD = 'PUT'
+ REQUEST_HAS_BODY = true
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Delete < HTTPRequest
+ METHOD = 'DELETE'
+ REQUEST_HAS_BODY = false
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Options < HTTPRequest
+ METHOD = 'OPTIONS'
+ REQUEST_HAS_BODY = false
+ RESPONSE_HAS_BODY = false
+ end
+
+ class Trace < HTTPRequest
+ METHOD = 'TRACE'
+ REQUEST_HAS_BODY = false
+ RESPONSE_HAS_BODY = true
+ end
+
+ #
+ # WebDAV methods --- RFC2518
+ #
+
+ class Propfind < HTTPRequest
+ METHOD = 'PROPFIND'
+ REQUEST_HAS_BODY = true
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Proppatch < HTTPRequest
+ METHOD = 'PROPPATCH'
+ REQUEST_HAS_BODY = true
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Mkcol < HTTPRequest
+ METHOD = 'MKCOL'
+ REQUEST_HAS_BODY = true
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Copy < HTTPRequest
+ METHOD = 'COPY'
+ REQUEST_HAS_BODY = false
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Move < HTTPRequest
+ METHOD = 'MOVE'
+ REQUEST_HAS_BODY = false
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Lock < HTTPRequest
+ METHOD = 'LOCK'
+ REQUEST_HAS_BODY = true
+ RESPONSE_HAS_BODY = true
+ end
+
+ class Unlock < HTTPRequest
+ METHOD = 'UNLOCK'
+ REQUEST_HAS_BODY = true
+ RESPONSE_HAS_BODY = true
+ end
+ end
+
+
+ ###
+ ### Response
+ ###
+
+ # HTTP exception class.
+ # You must use its subclasses.
+ module HTTPExceptions
+ def initialize(msg, res) #:nodoc:
+ super msg
+ @response = res
+ end
+ attr_reader :response
+ alias data response #:nodoc: obsolete
+ end
+ class HTTPError < ProtocolError
+ include HTTPExceptions
+ end
+ class HTTPRetriableError < ProtoRetriableError
+ include HTTPExceptions
+ end
+ class HTTPServerException < ProtoServerError
+ # We cannot use the name "HTTPServerError", it is the name of the response.
+ include HTTPExceptions
+ end
+ class HTTPFatalError < ProtoFatalError
+ include HTTPExceptions
+ end
+
+
+ # HTTP response class. This class wraps response header and entity.
+ # Mixes in the HTTPHeader module, which provides access to response
+ # header values both via hash-like methods and individual readers.
+ # Note that each possible HTTP response code defines its own
+ # HTTPResponse subclass. These are listed below.
+ # All classes are
+ # defined under the Net module. Indentation indicates inheritance.
+ #
+ # xxx HTTPResponse
+ #
+ # 1xx HTTPInformation
+ # 100 HTTPContinue
+ # 101 HTTPSwitchProtocol
+ #
+ # 2xx HTTPSuccess
+ # 200 HTTPOK
+ # 201 HTTPCreated
+ # 202 HTTPAccepted
+ # 203 HTTPNonAuthoritativeInformation
+ # 204 HTTPNoContent
+ # 205 HTTPResetContent
+ # 206 HTTPPartialContent
+ #
+ # 3xx HTTPRedirection
+ # 300 HTTPMultipleChoice
+ # 301 HTTPMovedPermanently
+ # 302 HTTPFound
+ # 303 HTTPSeeOther
+ # 304 HTTPNotModified
+ # 305 HTTPUseProxy
+ # 307 HTTPTemporaryRedirect
+ #
+ # 4xx HTTPClientError
+ # 400 HTTPBadRequest
+ # 401 HTTPUnauthorized
+ # 402 HTTPPaymentRequired
+ # 403 HTTPForbidden
+ # 404 HTTPNotFound
+ # 405 HTTPMethodNotAllowed
+ # 406 HTTPNotAcceptable
+ # 407 HTTPProxyAuthenticationRequired
+ # 408 HTTPRequestTimeOut
+ # 409 HTTPConflict
+ # 410 HTTPGone
+ # 411 HTTPLengthRequired
+ # 412 HTTPPreconditionFailed
+ # 413 HTTPRequestEntityTooLarge
+ # 414 HTTPRequestURITooLong
+ # 415 HTTPUnsupportedMediaType
+ # 416 HTTPRequestedRangeNotSatisfiable
+ # 417 HTTPExpectationFailed
+ #
+ # 5xx HTTPServerError
+ # 500 HTTPInternalServerError
+ # 501 HTTPNotImplemented
+ # 502 HTTPBadGateway
+ # 503 HTTPServiceUnavailable
+ # 504 HTTPGatewayTimeOut
+ # 505 HTTPVersionNotSupported
+ #
+ # xxx HTTPUnknownResponse
+ #
+ class HTTPResponse
+ # true if the response has body.
+ def HTTPResponse.body_permitted?
+ self::HAS_BODY
+ end
+
+ def HTTPResponse.exception_type # :nodoc: internal use only
+ self::EXCEPTION_TYPE
+ end
+ end # reopened after
+
+ # :stopdoc:
+
+ class HTTPUnknownResponse < HTTPResponse
+ HAS_BODY = true
+ EXCEPTION_TYPE = HTTPError
+ end
+ class HTTPInformation < HTTPResponse # 1xx
+ HAS_BODY = false
+ EXCEPTION_TYPE = HTTPError
+ end
+ class HTTPSuccess < HTTPResponse # 2xx
+ HAS_BODY = true
+ EXCEPTION_TYPE = HTTPError
+ end
+ class HTTPRedirection < HTTPResponse # 3xx
+ HAS_BODY = true
+ EXCEPTION_TYPE = HTTPRetriableError
+ end
+ class HTTPClientError < HTTPResponse # 4xx
+ HAS_BODY = true
+ EXCEPTION_TYPE = HTTPServerException # for backward compatibility
+ end
+ class HTTPServerError < HTTPResponse # 5xx
+ HAS_BODY = true
+ EXCEPTION_TYPE = HTTPFatalError # for backward compatibility
+ end
+
+ class HTTPContinue < HTTPInformation # 100
+ HAS_BODY = false
+ end
+ class HTTPSwitchProtocol < HTTPInformation # 101
+ HAS_BODY = false
+ end
+
+ class HTTPOK < HTTPSuccess # 200
+ HAS_BODY = true
+ end
+ class HTTPCreated < HTTPSuccess # 201
+ HAS_BODY = true
+ end
+ class HTTPAccepted < HTTPSuccess # 202
+ HAS_BODY = true
+ end
+ class HTTPNonAuthoritativeInformation < HTTPSuccess # 203
+ HAS_BODY = true
+ end
+ class HTTPNoContent < HTTPSuccess # 204
+ HAS_BODY = false
+ end
+ class HTTPResetContent < HTTPSuccess # 205
+ HAS_BODY = false
+ end
+ class HTTPPartialContent < HTTPSuccess # 206
+ HAS_BODY = true
+ end
+
+ class HTTPMultipleChoice < HTTPRedirection # 300
+ HAS_BODY = true
+ end
+ class HTTPMovedPermanently < HTTPRedirection # 301
+ HAS_BODY = true
+ end
+ class HTTPFound < HTTPRedirection # 302
+ HAS_BODY = true
+ end
+ HTTPMovedTemporarily = HTTPFound
+ class HTTPSeeOther < HTTPRedirection # 303
+ HAS_BODY = true
+ end
+ class HTTPNotModified < HTTPRedirection # 304
+ HAS_BODY = false
+ end
+ class HTTPUseProxy < HTTPRedirection # 305
+ HAS_BODY = false
+ end
+ # 306 unused
+ class HTTPTemporaryRedirect < HTTPRedirection # 307
+ HAS_BODY = true
+ end
+
+ class HTTPBadRequest < HTTPClientError # 400
+ HAS_BODY = true
+ end
+ class HTTPUnauthorized < HTTPClientError # 401
+ HAS_BODY = true
+ end
+ class HTTPPaymentRequired < HTTPClientError # 402
+ HAS_BODY = true
+ end
+ class HTTPForbidden < HTTPClientError # 403
+ HAS_BODY = true
+ end
+ class HTTPNotFound < HTTPClientError # 404
+ HAS_BODY = true
+ end
+ class HTTPMethodNotAllowed < HTTPClientError # 405
+ HAS_BODY = true
+ end
+ class HTTPNotAcceptable < HTTPClientError # 406
+ HAS_BODY = true
+ end
+ class HTTPProxyAuthenticationRequired < HTTPClientError # 407
+ HAS_BODY = true
+ end
+ class HTTPRequestTimeOut < HTTPClientError # 408
+ HAS_BODY = true
+ end
+ class HTTPConflict < HTTPClientError # 409
+ HAS_BODY = true
+ end
+ class HTTPGone < HTTPClientError # 410
+ HAS_BODY = true
+ end
+ class HTTPLengthRequired < HTTPClientError # 411
+ HAS_BODY = true
+ end
+ class HTTPPreconditionFailed < HTTPClientError # 412
+ HAS_BODY = true
+ end
+ class HTTPRequestEntityTooLarge < HTTPClientError # 413
+ HAS_BODY = true
+ end
+ class HTTPRequestURITooLong < HTTPClientError # 414
+ HAS_BODY = true
+ end
+ HTTPRequestURITooLarge = HTTPRequestURITooLong
+ class HTTPUnsupportedMediaType < HTTPClientError # 415
+ HAS_BODY = true
+ end
+ class HTTPRequestedRangeNotSatisfiable < HTTPClientError # 416
+ HAS_BODY = true
+ end
+ class HTTPExpectationFailed < HTTPClientError # 417
+ HAS_BODY = true
+ end
+
+ class HTTPInternalServerError < HTTPServerError # 500
+ HAS_BODY = true
+ end
+ class HTTPNotImplemented < HTTPServerError # 501
+ HAS_BODY = true
+ end
+ class HTTPBadGateway < HTTPServerError # 502
+ HAS_BODY = true
+ end
+ class HTTPServiceUnavailable < HTTPServerError # 503
+ HAS_BODY = true
+ end
+ class HTTPGatewayTimeOut < HTTPServerError # 504
+ HAS_BODY = true
+ end
+ class HTTPVersionNotSupported < HTTPServerError # 505
+ HAS_BODY = true
+ end
+
+ # :startdoc:
+
+
+ class HTTPResponse # reopen
+
+ CODE_CLASS_TO_OBJ = {
+ '1' => HTTPInformation,
+ '2' => HTTPSuccess,
+ '3' => HTTPRedirection,
+ '4' => HTTPClientError,
+ '5' => HTTPServerError
+ }
+ CODE_TO_OBJ = {
+ '100' => HTTPContinue,
+ '101' => HTTPSwitchProtocol,
+
+ '200' => HTTPOK,
+ '201' => HTTPCreated,
+ '202' => HTTPAccepted,
+ '203' => HTTPNonAuthoritativeInformation,
+ '204' => HTTPNoContent,
+ '205' => HTTPResetContent,
+ '206' => HTTPPartialContent,
+
+ '300' => HTTPMultipleChoice,
+ '301' => HTTPMovedPermanently,
+ '302' => HTTPFound,
+ '303' => HTTPSeeOther,
+ '304' => HTTPNotModified,
+ '305' => HTTPUseProxy,
+ '307' => HTTPTemporaryRedirect,
+
+ '400' => HTTPBadRequest,
+ '401' => HTTPUnauthorized,
+ '402' => HTTPPaymentRequired,
+ '403' => HTTPForbidden,
+ '404' => HTTPNotFound,
+ '405' => HTTPMethodNotAllowed,
+ '406' => HTTPNotAcceptable,
+ '407' => HTTPProxyAuthenticationRequired,
+ '408' => HTTPRequestTimeOut,
+ '409' => HTTPConflict,
+ '410' => HTTPGone,
+ '411' => HTTPLengthRequired,
+ '412' => HTTPPreconditionFailed,
+ '413' => HTTPRequestEntityTooLarge,
+ '414' => HTTPRequestURITooLong,
+ '415' => HTTPUnsupportedMediaType,
+ '416' => HTTPRequestedRangeNotSatisfiable,
+ '417' => HTTPExpectationFailed,
+
+ '500' => HTTPInternalServerError,
+ '501' => HTTPNotImplemented,
+ '502' => HTTPBadGateway,
+ '503' => HTTPServiceUnavailable,
+ '504' => HTTPGatewayTimeOut,
+ '505' => HTTPVersionNotSupported
+ }
+
+ class << HTTPResponse
+ def read_new(sock) #:nodoc: internal use only
+ httpv, code, msg = read_status_line(sock)
+ res = response_class(code).new(httpv, code, msg)
+ each_response_header(sock) do |k,v|
+ res.add_field k, v
+ end
+ res
+ end
+
+ private
+
+ def read_status_line(sock)
+ str = sock.readline
+ m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or
+ raise HTTPBadResponse, "wrong status line: #{str.dump}"
+ m.captures
+ end
+
+ def response_class(code)
+ CODE_TO_OBJ[code] or
+ CODE_CLASS_TO_OBJ[code[0,1]] or
+ HTTPUnknownResponse
+ end
+
+ def each_response_header(sock)
+ while true
+ line = sock.readuntil("\n", true).sub(/\s+\z/, '')
+ break if line.empty?
+ m = /\A([^:]+):\s*/.match(line) or
+ raise HTTPBadResponse, 'wrong header line format'
+ yield m[1], m.post_match
+ end
+ end
+ end
+
+ # next is to fix bug in RDoc, where the private inside class << self
+ # spills out.
+ public
+
+ include HTTPHeader
+
+ def initialize(httpv, code, msg) #:nodoc: internal use only
+ @http_version = httpv
+ @code = code
+ @message = msg
+ initialize_http_header nil
+ @body = nil
+ @read = false
+ end
+
+ # The HTTP version supported by the server.
+ attr_reader :http_version
+
+ # HTTP result code string. For example, '302'. You can also
+ # determine the response type by which response subclass the
+ # response object is an instance of.
+ attr_reader :code
+
+ # HTTP result message. For example, 'Not Found'.
+ attr_reader :message
+ alias msg message # :nodoc: obsolete
+
+ def inspect
+ "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
+ end
+
+ # For backward compatibility.
+ # To allow Net::HTTP 1.1 style assignment
+ # e.g.
+ # response, body = Net::HTTP.get(....)
+ #
+ def to_ary
+ warn "net/http.rb: warning: Net::HTTP v1.1 style assignment found at #{caller(1)[0]}; use `response = http.get(...)' instead." if $VERBOSE
+ res = self.dup
+ class << res
+ undef to_ary
+ end
+ [res, res.body]
+ end
+
+ #
+ # response <-> exception relationship
+ #
+
+ def code_type #:nodoc:
+ self.class
+ end
+
+ def error! #:nodoc:
+ raise error_type().new(@code + ' ' + @message.dump, self)
+ end
+
+ def error_type #:nodoc:
+ self.class::EXCEPTION_TYPE
+ end
+
+ # Raises HTTP error if the response is not 2xx.
+ def value
+ error! unless self.kind_of?(HTTPSuccess)
+ end
+
+ #
+ # header (for backward compatibility only; DO NOT USE)
+ #
+
+ def response #:nodoc:
+ warn "#{caller(1)[0]}: warning: HTTPResponse#response is obsolete" if $VERBOSE
+ self
+ end
+
+ def header #:nodoc:
+ warn "#{caller(1)[0]}: warning: HTTPResponse#header is obsolete" if $VERBOSE
+ self
+ end
+
+ def read_header #:nodoc:
+ warn "#{caller(1)[0]}: warning: HTTPResponse#read_header is obsolete" if $VERBOSE
+ self
+ end
+
+ #
+ # body
+ #
+
+ def reading_body(sock, reqmethodallowbody) #:nodoc: internal use only
+ @socket = sock
+ @body_exist = reqmethodallowbody && self.class.body_permitted?
+ begin
+ yield
+ self.body # ensure to read body
+ ensure
+ @socket = nil
+ end
+ end
+
+ # Gets entity body. If the block given, yields it to +block+.
+ # The body is provided in fragments, as it is read in from the socket.
+ #
+ # Calling this method a second or subsequent time will return the
+ # already read string.
+ #
+ # http.request_get('/index.html') {|res|
+ # puts res.read_body
+ # }
+ #
+ # http.request_get('/index.html') {|res|
+ # p res.read_body.object_id # 538149362
+ # p res.read_body.object_id # 538149362
+ # }
+ #
+ # # using iterator
+ # http.request_get('/index.html') {|res|
+ # res.read_body do |segment|
+ # print segment
+ # end
+ # }
+ #
+ def read_body(dest = nil, &block)
+ if @read
+ raise IOError, "#{self.class}\#read_body called twice" if dest or block
+ return @body
+ end
+ to = procdest(dest, block)
+ stream_check
+ if @body_exist
+ read_body_0 to
+ @body = to
+ else
+ @body = nil
+ end
+ @read = true
+
+ @body
+ end
+
+ # Returns the entity body.
+ #
+ # Calling this method a second or subsequent time will return the
+ # already read string.
+ #
+ # http.request_get('/index.html') {|res|
+ # puts res.body
+ # }
+ #
+ # http.request_get('/index.html') {|res|
+ # p res.body.object_id # 538149362
+ # p res.body.object_id # 538149362
+ # }
+ #
+ def body
+ read_body()
+ end
+
+ # Because it may be necessary to modify the body, Eg, decompression
+ # this method facilitates that.
+ def body=(value)
+ @body = value
+ end
+
+ alias entity body #:nodoc: obsolete
+
+ private
+
+ def read_body_0(dest)
+ if chunked?
+ read_chunked dest
+ return
+ end
+ clen = content_length()
+ if clen
+ @socket.read clen, dest, true # ignore EOF
+ return
+ end
+ clen = range_length()
+ if clen
+ @socket.read clen, dest
+ return
+ end
+ @socket.read_all dest
+ end
+
+ def read_chunked(dest)
+ len = nil
+ total = 0
+ while true
+ line = @socket.readline
+ hexlen = line.slice(/[0-9a-fA-F]+/) or
+ raise HTTPBadResponse, "wrong chunk size line: #{line}"
+ len = hexlen.hex
+ break if len == 0
+ @socket.read len, dest; total += len
+ @socket.read 2 # \r\n
+ end
+ until @socket.readline.empty?
+ # none
+ end
+ end
+
+ def stream_check
+ raise IOError, 'attempt to read body out of block' if @socket.closed?
+ end
+
+ def procdest(dest, block)
+ raise ArgumentError, 'both arg and block given for HTTP method' \
+ if dest and block
+ if block
+ ReadAdapter.new(block)
+ else
+ dest || ''
+ end
+ end
+
+ end
+
+
+ # :enddoc:
+
+ #--
+ # for backward compatibility
+ class HTTP
+ ProxyMod = ProxyDelta
+ end
+ module NetPrivate
+ HTTPRequest = ::Net::HTTPRequest
+ end
+
+ HTTPInformationCode = HTTPInformation
+ HTTPSuccessCode = HTTPSuccess
+ HTTPRedirectionCode = HTTPRedirection
+ HTTPRetriableCode = HTTPRedirection
+ HTTPClientErrorCode = HTTPClientError
+ HTTPFatalErrorCode = HTTPClientError
+ HTTPServerErrorCode = HTTPServerError
+ HTTPResponceReceiver = HTTPResponse
+
+end # module Net
diff --git a/ruby/lib/net/https.rb b/ruby/lib/net/https.rb
new file mode 100644
index 0000000..0b29315
--- /dev/null
+++ b/ruby/lib/net/https.rb
@@ -0,0 +1,136 @@
+=begin
+
+= $RCSfile$ -- SSL/TLS enhancement for Net::HTTP.
+
+== Info
+ 'OpenSSL for Ruby 2' project
+ Copyright (C) 2001 GOTOU Yuuzou <gotoyuzo@notwork.org>
+ All rights reserved.
+
+== Licence
+ This program is licenced under the same licence as Ruby.
+ (See the file 'LICENCE'.)
+
+== Requirements
+ This program requires Net 1.2.0 or higher version.
+ You can get it from RAA or Ruby's CVS repository.
+
+== Version
+ $Id: https.rb 18512 2008-08-12 05:20:09Z aamine $
+
+ 2001-11-06: Contiributed to Ruby/OpenSSL project.
+ 2004-03-06: Some code is merged in to net/http.
+
+== Example
+
+Here is a simple HTTP client:
+
+ require 'net/http'
+ require 'uri'
+
+ uri = URI.parse(ARGV[0] || 'http://localhost/')
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.start {
+ http.request_get(uri.path) {|res|
+ print res.body
+ }
+ }
+
+It can be replaced by the following code:
+
+ require 'net/https'
+ require 'uri'
+
+ uri = URI.parse(ARGV[0] || 'https://localhost/')
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS
+ http.start {
+ http.request_get(uri.path) {|res|
+ print res.body
+ }
+ }
+
+== class Net::HTTP
+
+=== Instance Methods
+
+: use_ssl?
+ returns true if use SSL/TLS with HTTP.
+
+: use_ssl=((|true_or_false|))
+ sets use_ssl.
+
+: peer_cert
+ return the X.509 certificates the server presented.
+
+: key, key=((|key|))
+ Sets an OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
+ (This method is appeared in Michal Rokos's OpenSSL extension.)
+
+: cert, cert=((|cert|))
+ Sets an OpenSSL::X509::Certificate object as client certificate
+ (This method is appeared in Michal Rokos's OpenSSL extension).
+
+: ca_file, ca_file=((|path|))
+ Sets path of a CA certification file in PEM format.
+ The file can contrain several CA certificats.
+
+: ca_path, ca_path=((|path|))
+ Sets path of a CA certification directory containing certifications
+ in PEM format.
+
+: verify_mode, verify_mode=((|mode|))
+ Sets the flags for server the certification verification at
+ begining of SSL/TLS session.
+ OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER is acceptable.
+
+: verify_callback, verify_callback=((|proc|))
+ Sets the verify callback for the server certification verification.
+
+: verify_depth, verify_depth=((|num|))
+ Sets the maximum depth for the certificate chain verification.
+
+: cert_store, cert_store=((|store|))
+ Sets the X509::Store to verify peer certificate.
+
+: ssl_timeout, ssl_timeout=((|sec|))
+ Sets the SSL timeout seconds.
+
+=end
+
+require 'net/http'
+require 'openssl'
+
+module Net
+ class HTTP
+ remove_method :use_ssl?
+ def use_ssl?
+ @use_ssl
+ end
+
+ # Turn on/off SSL.
+ # This flag must be set before starting session.
+ # If you change use_ssl value after session started,
+ # a Net::HTTP object raises IOError.
+ def use_ssl=(flag)
+ flag = (flag ? true : false)
+ if started? and @use_ssl != flag
+ raise IOError, "use_ssl value changed, but session already started"
+ end
+ @use_ssl = flag
+ end
+
+ SSL_ATTRIBUTES = %w(
+ ssl_version key cert ca_file ca_path cert_store ciphers
+ verify_mode verify_callback verify_depth ssl_timeout
+ )
+ attr_accessor(*SSL_ATTRIBUTES)
+
+ def peer_cert
+ if not use_ssl? or not @socket
+ return nil
+ end
+ @socket.io.peer_cert
+ end
+ end
+end
diff --git a/ruby/lib/net/imap.rb b/ruby/lib/net/imap.rb
new file mode 100644
index 0000000..ea2b598
--- /dev/null
+++ b/ruby/lib/net/imap.rb
@@ -0,0 +1,3500 @@
+#
+# = net/imap.rb
+#
+# Copyright (C) 2000 Shugo Maeda <shugo@ruby-lang.org>
+#
+# This library is distributed under the terms of the Ruby license.
+# You can freely distribute/modify this library.
+#
+# Documentation: Shugo Maeda, with RDoc conversion and overview by William
+# Webber.
+#
+# See Net::IMAP for documentation.
+#
+
+
+require "socket"
+require "monitor"
+require "digest/md5"
+require "strscan"
+begin
+ require "openssl/ssl"
+rescue LoadError
+end
+
+module Net
+
+ #
+ # Net::IMAP implements Internet Message Access Protocol (IMAP) client
+ # functionality. The protocol is described in [IMAP].
+ #
+ # == IMAP Overview
+ #
+ # An IMAP client connects to a server, and then authenticates
+ # itself using either #authenticate() or #login(). Having
+ # authenticated itself, there is a range of commands
+ # available to it. Most work with mailboxes, which may be
+ # arranged in an hierarchical namespace, and each of which
+ # contains zero or more messages. How this is implemented on
+ # the server is implementation-dependent; on a UNIX server, it
+ # will frequently be implemented as a files in mailbox format
+ # within a hierarchy of directories.
+ #
+ # To work on the messages within a mailbox, the client must
+ # first select that mailbox, using either #select() or (for
+ # read-only access) #examine(). Once the client has successfully
+ # selected a mailbox, they enter _selected_ state, and that
+ # mailbox becomes the _current_ mailbox, on which mail-item
+ # related commands implicitly operate.
+ #
+ # Messages have two sorts of identifiers: message sequence
+ # numbers, and UIDs.
+ #
+ # Message sequence numbers number messages within a mail box
+ # from 1 up to the number of items in the mail box. If new
+ # message arrives during a session, it receives a sequence
+ # number equal to the new size of the mail box. If messages
+ # are expunged from the mailbox, remaining messages have their
+ # sequence numbers "shuffled down" to fill the gaps.
+ #
+ # UIDs, on the other hand, are permanently guaranteed not to
+ # identify another message within the same mailbox, even if
+ # the existing message is deleted. UIDs are required to
+ # be assigned in ascending (but not necessarily sequential)
+ # order within a mailbox; this means that if a non-IMAP client
+ # rearranges the order of mailitems within a mailbox, the
+ # UIDs have to be reassigned. An IMAP client cannot thus
+ # rearrange message orders.
+ #
+ # == Examples of Usage
+ #
+ # === List sender and subject of all recent messages in the default mailbox
+ #
+ # imap = Net::IMAP.new('mail.example.com')
+ # imap.authenticate('LOGIN', 'joe_user', 'joes_password')
+ # imap.examine('INBOX')
+ # imap.search(["RECENT"]).each do |message_id|
+ # envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
+ # puts "#{envelope.from[0].name}: \t#{envelope.subject}"
+ # end
+ #
+ # === Move all messages from April 2003 from "Mail/sent-mail" to "Mail/sent-apr03"
+ #
+ # imap = Net::IMAP.new('mail.example.com')
+ # imap.authenticate('LOGIN', 'joe_user', 'joes_password')
+ # imap.select('Mail/sent-mail')
+ # if not imap.list('Mail/', 'sent-apr03')
+ # imap.create('Mail/sent-apr03')
+ # end
+ # imap.search(["BEFORE", "30-Apr-2003", "SINCE", "1-Apr-2003"]).each do |message_id|
+ # imap.copy(message_id, "Mail/sent-apr03")
+ # imap.store(message_id, "+FLAGS", [:Deleted])
+ # end
+ # imap.expunge
+ #
+ # == Thread Safety
+ #
+ # Net::IMAP supports concurrent threads. For example,
+ #
+ # imap = Net::IMAP.new("imap.foo.net", "imap2")
+ # imap.authenticate("cram-md5", "bar", "password")
+ # imap.select("inbox")
+ # fetch_thread = Thread.start { imap.fetch(1..-1, "UID") }
+ # search_result = imap.search(["BODY", "hello"])
+ # fetch_result = fetch_thread.value
+ # imap.disconnect
+ #
+ # This script invokes the FETCH command and the SEARCH command concurrently.
+ #
+ # == Errors
+ #
+ # An IMAP server can send three different types of responses to indicate
+ # failure:
+ #
+ # NO:: the attempted command could not be successfully completed. For
+ # instance, the username/password used for logging in are incorrect;
+ # the selected mailbox does not exists; etc.
+ #
+ # BAD:: the request from the client does not follow the server's
+ # understanding of the IMAP protocol. This includes attempting
+ # commands from the wrong client state; for instance, attempting
+ # to perform a SEARCH command without having SELECTed a current
+ # mailbox. It can also signal an internal server
+ # failure (such as a disk crash) has occurred.
+ #
+ # BYE:: the server is saying goodbye. This can be part of a normal
+ # logout sequence, and can be used as part of a login sequence
+ # to indicate that the server is (for some reason) unwilling
+ # to accept our connection. As a response to any other command,
+ # it indicates either that the server is shutting down, or that
+ # the server is timing out the client connection due to inactivity.
+ #
+ # These three error response are represented by the errors
+ # Net::IMAP::NoResponseError, Net::IMAP::BadResponseError, and
+ # Net::IMAP::ByeResponseError, all of which are subclasses of
+ # Net::IMAP::ResponseError. Essentially, all methods that involve
+ # sending a request to the server can generate one of these errors.
+ # Only the most pertinent instances have been documented below.
+ #
+ # Because the IMAP class uses Sockets for communication, its methods
+ # are also susceptible to the various errors that can occur when
+ # working with sockets. These are generally represented as
+ # Errno errors. For instance, any method that involves sending a
+ # request to the server and/or receiving a response from it could
+ # raise an Errno::EPIPE error if the network connection unexpectedly
+ # goes down. See the socket(7), ip(7), tcp(7), socket(2), connect(2),
+ # and associated man pages.
+ #
+ # Finally, a Net::IMAP::DataFormatError is thrown if low-level data
+ # is found to be in an incorrect format (for instance, when converting
+ # between UTF-8 and UTF-16), and Net::IMAP::ResponseParseError is
+ # thrown if a server response is non-parseable.
+ #
+ #
+ # == References
+ #
+ # [[IMAP]]
+ # M. Crispin, "INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1",
+ # RFC 2060, December 1996. (Note: since obsoleted by RFC 3501)
+ #
+ # [[LANGUAGE-TAGS]]
+ # Alvestrand, H., "Tags for the Identification of
+ # Languages", RFC 1766, March 1995.
+ #
+ # [[MD5]]
+ # Myers, J., and M. Rose, "The Content-MD5 Header Field", RFC
+ # 1864, October 1995.
+ #
+ # [[MIME-IMB]]
+ # Freed, N., and N. Borenstein, "MIME (Multipurpose Internet
+ # Mail Extensions) Part One: Format of Internet Message Bodies", RFC
+ # 2045, November 1996.
+ #
+ # [[RFC-822]]
+ # Crocker, D., "Standard for the Format of ARPA Internet Text
+ # Messages", STD 11, RFC 822, University of Delaware, August 1982.
+ #
+ # [[RFC-2087]]
+ # Myers, J., "IMAP4 QUOTA extension", RFC 2087, January 1997.
+ #
+ # [[RFC-2086]]
+ # Myers, J., "IMAP4 ACL extension", RFC 2086, January 1997.
+ #
+ # [[RFC-2195]]
+ # Klensin, J., Catoe, R., and Krumviede, P., "IMAP/POP AUTHorize Extension
+ # for Simple Challenge/Response", RFC 2195, September 1997.
+ #
+ # [[SORT-THREAD-EXT]]
+ # Crispin, M., "INTERNET MESSAGE ACCESS PROTOCOL - SORT and THREAD
+ # Extensions", draft-ietf-imapext-sort, May 2003.
+ #
+ # [[OSSL]]
+ # http://www.openssl.org
+ #
+ # [[RSSL]]
+ # http://savannah.gnu.org/projects/rubypki
+ #
+ # [[UTF7]]
+ # Goldsmith, D. and Davis, M., "UTF-7: A Mail-Safe Transformation Format of
+ # Unicode", RFC 2152, May 1997.
+ #
+ class IMAP
+ include MonitorMixin
+ if defined?(OpenSSL)
+ include OpenSSL
+ include SSL
+ end
+
+ # Returns an initial greeting response from the server.
+ attr_reader :greeting
+
+ # Returns recorded untagged responses. For example:
+ #
+ # imap.select("inbox")
+ # p imap.responses["EXISTS"][-1]
+ # #=> 2
+ # p imap.responses["UIDVALIDITY"][-1]
+ # #=> 968263756
+ attr_reader :responses
+
+ # Returns all response handlers.
+ attr_reader :response_handlers
+
+ # The thread to receive exceptions.
+ attr_accessor :client_thread
+
+ # Flag indicating a message has been seen
+ SEEN = :Seen
+
+ # Flag indicating a message has been answered
+ ANSWERED = :Answered
+
+ # Flag indicating a message has been flagged for special or urgent
+ # attention
+ FLAGGED = :Flagged
+
+ # Flag indicating a message has been marked for deletion. This
+ # will occur when the mailbox is closed or expunged.
+ DELETED = :Deleted
+
+ # Flag indicating a message is only a draft or work-in-progress version.
+ DRAFT = :Draft
+
+ # Flag indicating that the message is "recent", meaning that this
+ # session is the first session in which the client has been notified
+ # of this message.
+ RECENT = :Recent
+
+ # Flag indicating that a mailbox context name cannot contain
+ # children.
+ NOINFERIORS = :Noinferiors
+
+ # Flag indicating that a mailbox is not selected.
+ NOSELECT = :Noselect
+
+ # Flag indicating that a mailbox has been marked "interesting" by
+ # the server; this commonly indicates that the mailbox contains
+ # new messages.
+ MARKED = :Marked
+
+ # Flag indicating that the mailbox does not contains new messages.
+ UNMARKED = :Unmarked
+
+ # Returns the debug mode.
+ def self.debug
+ return @@debug
+ end
+
+ # Sets the debug mode.
+ def self.debug=(val)
+ return @@debug = val
+ end
+
+ # Adds an authenticator for Net::IMAP#authenticate. +auth_type+
+ # is the type of authentication this authenticator supports
+ # (for instance, "LOGIN"). The +authenticator+ is an object
+ # which defines a process() method to handle authentication with
+ # the server. See Net::IMAP::LoginAuthenticator,
+ # Net::IMAP::CramMD5Authenticator, and Net::IMAP::DigestMD5Authenticator
+ # for examples.
+ #
+ #
+ # If +auth_type+ refers to an existing authenticator, it will be
+ # replaced by the new one.
+ def self.add_authenticator(auth_type, authenticator)
+ @@authenticators[auth_type] = authenticator
+ end
+
+ # Disconnects from the server.
+ def disconnect
+ begin
+ begin
+ # try to call SSL::SSLSocket#io.
+ @sock.io.shutdown
+ rescue NoMethodError
+ # @sock is not an SSL::SSLSocket.
+ @sock.shutdown
+ end
+ rescue Errno::ENOTCONN
+ # ignore `Errno::ENOTCONN: Socket is not connected' on some platforms.
+ end
+ @receiver_thread.join
+ @sock.close
+ end
+
+ # Returns true if disconnected from the server.
+ def disconnected?
+ return @sock.closed?
+ end
+
+ # Sends a CAPABILITY command, and returns an array of
+ # capabilities that the server supports. Each capability
+ # is a string. See [IMAP] for a list of possible
+ # capabilities.
+ #
+ # Note that the Net::IMAP class does not modify its
+ # behaviour according to the capabilities of the server;
+ # it is up to the user of the class to ensure that
+ # a certain capability is supported by a server before
+ # using it.
+ def capability
+ synchronize do
+ send_command("CAPABILITY")
+ return @responses.delete("CAPABILITY")[-1]
+ end
+ end
+
+ # Sends a NOOP command to the server. It does nothing.
+ def noop
+ send_command("NOOP")
+ end
+
+ # Sends a LOGOUT command to inform the server that the client is
+ # done with the connection.
+ def logout
+ send_command("LOGOUT")
+ end
+
+ # Sends a STARTTLS command to start TLS session.
+ def starttls(options = {}, verify = true)
+ send_command("STARTTLS") do |resp|
+ if resp.kind_of?(TaggedResponse) && resp.name == "OK"
+ begin
+ # for backward compatibility
+ certs = options.to_str
+ options = create_ssl_params(certs, verify)
+ rescue NoMethodError
+ end
+ start_tls_session(options)
+ end
+ end
+ end
+
+ # Sends an AUTHENTICATE command to authenticate the client.
+ # The +auth_type+ parameter is a string that represents
+ # the authentication mechanism to be used. Currently Net::IMAP
+ # supports authentication mechanisms:
+ #
+ # LOGIN:: login using cleartext user and password.
+ # CRAM-MD5:: login with cleartext user and encrypted password
+ # (see [RFC-2195] for a full description). This
+ # mechanism requires that the server have the user's
+ # password stored in clear-text password.
+ #
+ # For both these mechanisms, there should be two +args+: username
+ # and (cleartext) password. A server may not support one or other
+ # of these mechanisms; check #capability() for a capability of
+ # the form "AUTH=LOGIN" or "AUTH=CRAM-MD5".
+ #
+ # Authentication is done using the appropriate authenticator object:
+ # see @@authenticators for more information on plugging in your own
+ # authenticator.
+ #
+ # For example:
+ #
+ # imap.authenticate('LOGIN', user, password)
+ #
+ # A Net::IMAP::NoResponseError is raised if authentication fails.
+ def authenticate(auth_type, *args)
+ auth_type = auth_type.upcase
+ unless @@authenticators.has_key?(auth_type)
+ raise ArgumentError,
+ format('unknown auth type - "%s"', auth_type)
+ end
+ authenticator = @@authenticators[auth_type].new(*args)
+ send_command("AUTHENTICATE", auth_type) do |resp|
+ if resp.instance_of?(ContinuationRequest)
+ data = authenticator.process(resp.data.text.unpack("m")[0])
+ s = [data].pack("m").gsub(/\n/, "")
+ send_string_data(s)
+ put_string(CRLF)
+ end
+ end
+ end
+
+ # Sends a LOGIN command to identify the client and carries
+ # the plaintext +password+ authenticating this +user+. Note
+ # that, unlike calling #authenticate() with an +auth_type+
+ # of "LOGIN", #login() does *not* use the login authenticator.
+ #
+ # A Net::IMAP::NoResponseError is raised if authentication fails.
+ def login(user, password)
+ send_command("LOGIN", user, password)
+ end
+
+ # Sends a SELECT command to select a +mailbox+ so that messages
+ # in the +mailbox+ can be accessed.
+ #
+ # After you have selected a mailbox, you may retrieve the
+ # number of items in that mailbox from @responses["EXISTS"][-1],
+ # and the number of recent messages from @responses["RECENT"][-1].
+ # Note that these values can change if new messages arrive
+ # during a session; see #add_response_handler() for a way of
+ # detecting this event.
+ #
+ # A Net::IMAP::NoResponseError is raised if the mailbox does not
+ # exist or is for some reason non-selectable.
+ def select(mailbox)
+ synchronize do
+ @responses.clear
+ send_command("SELECT", mailbox)
+ end
+ end
+
+ # Sends a EXAMINE command to select a +mailbox+ so that messages
+ # in the +mailbox+ can be accessed. Behaves the same as #select(),
+ # except that the selected +mailbox+ is identified as read-only.
+ #
+ # A Net::IMAP::NoResponseError is raised if the mailbox does not
+ # exist or is for some reason non-examinable.
+ def examine(mailbox)
+ synchronize do
+ @responses.clear
+ send_command("EXAMINE", mailbox)
+ end
+ end
+
+ # Sends a CREATE command to create a new +mailbox+.
+ #
+ # A Net::IMAP::NoResponseError is raised if a mailbox with that name
+ # cannot be created.
+ def create(mailbox)
+ send_command("CREATE", mailbox)
+ end
+
+ # Sends a DELETE command to remove the +mailbox+.
+ #
+ # A Net::IMAP::NoResponseError is raised if a mailbox with that name
+ # cannot be deleted, either because it does not exist or because the
+ # client does not have permission to delete it.
+ def delete(mailbox)
+ send_command("DELETE", mailbox)
+ end
+
+ # Sends a RENAME command to change the name of the +mailbox+ to
+ # +newname+.
+ #
+ # A Net::IMAP::NoResponseError is raised if a mailbox with the
+ # name +mailbox+ cannot be renamed to +newname+ for whatever
+ # reason; for instance, because +mailbox+ does not exist, or
+ # because there is already a mailbox with the name +newname+.
+ def rename(mailbox, newname)
+ send_command("RENAME", mailbox, newname)
+ end
+
+ # Sends a SUBSCRIBE command to add the specified +mailbox+ name to
+ # the server's set of "active" or "subscribed" mailboxes as returned
+ # by #lsub().
+ #
+ # A Net::IMAP::NoResponseError is raised if +mailbox+ cannot be
+ # subscribed to, for instance because it does not exist.
+ def subscribe(mailbox)
+ send_command("SUBSCRIBE", mailbox)
+ end
+
+ # Sends a UNSUBSCRIBE command to remove the specified +mailbox+ name
+ # from the server's set of "active" or "subscribed" mailboxes.
+ #
+ # A Net::IMAP::NoResponseError is raised if +mailbox+ cannot be
+ # unsubscribed from, for instance because the client is not currently
+ # subscribed to it.
+ def unsubscribe(mailbox)
+ send_command("UNSUBSCRIBE", mailbox)
+ end
+
+ # Sends a LIST command, and returns a subset of names from
+ # the complete set of all names available to the client.
+ # +refname+ provides a context (for instance, a base directory
+ # in a directory-based mailbox hierarchy). +mailbox+ specifies
+ # a mailbox or (via wildcards) mailboxes under that context.
+ # Two wildcards may be used in +mailbox+: '*', which matches
+ # all characters *including* the hierarchy delimiter (for instance,
+ # '/' on a UNIX-hosted directory-based mailbox hierarchy); and '%',
+ # which matches all characters *except* the hierarchy delimiter.
+ #
+ # If +refname+ is empty, +mailbox+ is used directly to determine
+ # which mailboxes to match. If +mailbox+ is empty, the root
+ # name of +refname+ and the hierarchy delimiter are returned.
+ #
+ # The return value is an array of +Net::IMAP::MailboxList+. For example:
+ #
+ # imap.create("foo/bar")
+ # imap.create("foo/baz")
+ # p imap.list("", "foo/%")
+ # #=> [#<Net::IMAP::MailboxList attr=[:Noselect], delim="/", name="foo/">, \\
+ # #<Net::IMAP::MailboxList attr=[:Noinferiors, :Marked], delim="/", name="foo/bar">, \\
+ # #<Net::IMAP::MailboxList attr=[:Noinferiors], delim="/", name="foo/baz">]
+ def list(refname, mailbox)
+ synchronize do
+ send_command("LIST", refname, mailbox)
+ return @responses.delete("LIST")
+ end
+ end
+
+ # Sends the GETQUOTAROOT command along with specified +mailbox+.
+ # This command is generally available to both admin and user.
+ # If mailbox exists, returns an array containing objects of
+ # Net::IMAP::MailboxQuotaRoot and Net::IMAP::MailboxQuota.
+ def getquotaroot(mailbox)
+ synchronize do
+ send_command("GETQUOTAROOT", mailbox)
+ result = []
+ result.concat(@responses.delete("QUOTAROOT"))
+ result.concat(@responses.delete("QUOTA"))
+ return result
+ end
+ end
+
+ # Sends the GETQUOTA command along with specified +mailbox+.
+ # If this mailbox exists, then an array containing a
+ # Net::IMAP::MailboxQuota object is returned. This
+ # command generally is only available to server admin.
+ def getquota(mailbox)
+ synchronize do
+ send_command("GETQUOTA", mailbox)
+ return @responses.delete("QUOTA")
+ end
+ end
+
+ # Sends a SETQUOTA command along with the specified +mailbox+ and
+ # +quota+. If +quota+ is nil, then quota will be unset for that
+ # mailbox. Typically one needs to be logged in as server admin
+ # for this to work. The IMAP quota commands are described in
+ # [RFC-2087].
+ def setquota(mailbox, quota)
+ if quota.nil?
+ data = '()'
+ else
+ data = '(STORAGE ' + quota.to_s + ')'
+ end
+ send_command("SETQUOTA", mailbox, RawData.new(data))
+ end
+
+ # Sends the SETACL command along with +mailbox+, +user+ and the
+ # +rights+ that user is to have on that mailbox. If +rights+ is nil,
+ # then that user will be stripped of any rights to that mailbox.
+ # The IMAP ACL commands are described in [RFC-2086].
+ def setacl(mailbox, user, rights)
+ if rights.nil?
+ send_command("SETACL", mailbox, user, "")
+ else
+ send_command("SETACL", mailbox, user, rights)
+ end
+ end
+
+ # Send the GETACL command along with specified +mailbox+.
+ # If this mailbox exists, an array containing objects of
+ # Net::IMAP::MailboxACLItem will be returned.
+ def getacl(mailbox)
+ synchronize do
+ send_command("GETACL", mailbox)
+ return @responses.delete("ACL")[-1]
+ end
+ end
+
+ # Sends a LSUB command, and returns a subset of names from the set
+ # of names that the user has declared as being "active" or
+ # "subscribed". +refname+ and +mailbox+ are interpreted as
+ # for #list().
+ # The return value is an array of +Net::IMAP::MailboxList+.
+ def lsub(refname, mailbox)
+ synchronize do
+ send_command("LSUB", refname, mailbox)
+ return @responses.delete("LSUB")
+ end
+ end
+
+ # Sends a STATUS command, and returns the status of the indicated
+ # +mailbox+. +attr+ is a list of one or more attributes that
+ # we are request the status of. Supported attributes include:
+ #
+ # MESSAGES:: the number of messages in the mailbox.
+ # RECENT:: the number of recent messages in the mailbox.
+ # UNSEEN:: the number of unseen messages in the mailbox.
+ #
+ # The return value is a hash of attributes. For example:
+ #
+ # p imap.status("inbox", ["MESSAGES", "RECENT"])
+ # #=> {"RECENT"=>0, "MESSAGES"=>44}
+ #
+ # A Net::IMAP::NoResponseError is raised if status values
+ # for +mailbox+ cannot be returned, for instance because it
+ # does not exist.
+ def status(mailbox, attr)
+ synchronize do
+ send_command("STATUS", mailbox, attr)
+ return @responses.delete("STATUS")[-1].attr
+ end
+ end
+
+ # Sends a APPEND command to append the +message+ to the end of
+ # the +mailbox+. The optional +flags+ argument is an array of
+ # flags to initially passing to the new message. The optional
+ # +date_time+ argument specifies the creation time to assign to the
+ # new message; it defaults to the current time.
+ # For example:
+ #
+ # imap.append("inbox", <<EOF.gsub(/\n/, "\r\n"), [:Seen], Time.now)
+ # Subject: hello
+ # From: shugo@ruby-lang.org
+ # To: shugo@ruby-lang.org
+ #
+ # hello world
+ # EOF
+ #
+ # A Net::IMAP::NoResponseError is raised if the mailbox does
+ # not exist (it is not created automatically), or if the flags,
+ # date_time, or message arguments contain errors.
+ def append(mailbox, message, flags = nil, date_time = nil)
+ args = []
+ if flags
+ args.push(flags)
+ end
+ args.push(date_time) if date_time
+ args.push(Literal.new(message))
+ send_command("APPEND", mailbox, *args)
+ end
+
+ # Sends a CHECK command to request a checkpoint of the currently
+ # selected mailbox. This performs implementation-specific
+ # housekeeping, for instance, reconciling the mailbox's
+ # in-memory and on-disk state.
+ def check
+ send_command("CHECK")
+ end
+
+ # Sends a CLOSE command to close the currently selected mailbox.
+ # The CLOSE command permanently removes from the mailbox all
+ # messages that have the \Deleted flag set.
+ def close
+ send_command("CLOSE")
+ end
+
+ # Sends a EXPUNGE command to permanently remove from the currently
+ # selected mailbox all messages that have the \Deleted flag set.
+ def expunge
+ synchronize do
+ send_command("EXPUNGE")
+ return @responses.delete("EXPUNGE")
+ end
+ end
+
+ # Sends a SEARCH command to search the mailbox for messages that
+ # match the given searching criteria, and returns message sequence
+ # numbers. +keys+ can either be a string holding the entire
+ # search string, or a single-dimension array of search keywords and
+ # arguments. The following are some common search criteria;
+ # see [IMAP] section 6.4.4 for a full list.
+ #
+ # <message set>:: a set of message sequence numbers. ',' indicates
+ # an interval, ':' indicates a range. For instance,
+ # '2,10:12,15' means "2,10,11,12,15".
+ #
+ # BEFORE <date>:: messages with an internal date strictly before
+ # <date>. The date argument has a format similar
+ # to 8-Aug-2002.
+ #
+ # BODY <string>:: messages that contain <string> within their body.
+ #
+ # CC <string>:: messages containing <string> in their CC field.
+ #
+ # FROM <string>:: messages that contain <string> in their FROM field.
+ #
+ # NEW:: messages with the \Recent, but not the \Seen, flag set.
+ #
+ # NOT <search-key>:: negate the following search key.
+ #
+ # OR <search-key> <search-key>:: "or" two search keys together.
+ #
+ # ON <date>:: messages with an internal date exactly equal to <date>,
+ # which has a format similar to 8-Aug-2002.
+ #
+ # SINCE <date>:: messages with an internal date on or after <date>.
+ #
+ # SUBJECT <string>:: messages with <string> in their subject.
+ #
+ # TO <string>:: messages with <string> in their TO field.
+ #
+ # For example:
+ #
+ # p imap.search(["SUBJECT", "hello", "NOT", "NEW"])
+ # #=> [1, 6, 7, 8]
+ def search(keys, charset = nil)
+ return search_internal("SEARCH", keys, charset)
+ end
+
+ # As for #search(), but returns unique identifiers.
+ def uid_search(keys, charset = nil)
+ return search_internal("UID SEARCH", keys, charset)
+ end
+
+ # Sends a FETCH command to retrieve data associated with a message
+ # in the mailbox. The +set+ parameter is a number or an array of
+ # numbers or a Range object. The number is a message sequence
+ # number. +attr+ is a list of attributes to fetch; see the
+ # documentation for Net::IMAP::FetchData for a list of valid
+ # attributes.
+ # The return value is an array of Net::IMAP::FetchData. For example:
+ #
+ # p imap.fetch(6..8, "UID")
+ # #=> [#<Net::IMAP::FetchData seqno=6, attr={"UID"=>98}>, \\
+ # #<Net::IMAP::FetchData seqno=7, attr={"UID"=>99}>, \\
+ # #<Net::IMAP::FetchData seqno=8, attr={"UID"=>100}>]
+ # p imap.fetch(6, "BODY[HEADER.FIELDS (SUBJECT)]")
+ # #=> [#<Net::IMAP::FetchData seqno=6, attr={"BODY[HEADER.FIELDS (SUBJECT)]"=>"Subject: test\r\n\r\n"}>]
+ # data = imap.uid_fetch(98, ["RFC822.SIZE", "INTERNALDATE"])[0]
+ # p data.seqno
+ # #=> 6
+ # p data.attr["RFC822.SIZE"]
+ # #=> 611
+ # p data.attr["INTERNALDATE"]
+ # #=> "12-Oct-2000 22:40:59 +0900"
+ # p data.attr["UID"]
+ # #=> 98
+ def fetch(set, attr)
+ return fetch_internal("FETCH", set, attr)
+ end
+
+ # As for #fetch(), but +set+ contains unique identifiers.
+ def uid_fetch(set, attr)
+ return fetch_internal("UID FETCH", set, attr)
+ end
+
+ # Sends a STORE command to alter data associated with messages
+ # in the mailbox, in particular their flags. The +set+ parameter
+ # is a number or an array of numbers or a Range object. Each number
+ # is a message sequence number. +attr+ is the name of a data item
+ # to store: 'FLAGS' means to replace the message's flag list
+ # with the provided one; '+FLAGS' means to add the provided flags;
+ # and '-FLAGS' means to remove them. +flags+ is a list of flags.
+ #
+ # The return value is an array of Net::IMAP::FetchData. For example:
+ #
+ # p imap.store(6..8, "+FLAGS", [:Deleted])
+ # #=> [#<Net::IMAP::FetchData seqno=6, attr={"FLAGS"=>[:Seen, :Deleted]}>, \\
+ # #<Net::IMAP::FetchData seqno=7, attr={"FLAGS"=>[:Seen, :Deleted]}>, \\
+ # #<Net::IMAP::FetchData seqno=8, attr={"FLAGS"=>[:Seen, :Deleted]}>]
+ def store(set, attr, flags)
+ return store_internal("STORE", set, attr, flags)
+ end
+
+ # As for #store(), but +set+ contains unique identifiers.
+ def uid_store(set, attr, flags)
+ return store_internal("UID STORE", set, attr, flags)
+ end
+
+ # Sends a COPY command to copy the specified message(s) to the end
+ # of the specified destination +mailbox+. The +set+ parameter is
+ # a number or an array of numbers or a Range object. The number is
+ # a message sequence number.
+ def copy(set, mailbox)
+ copy_internal("COPY", set, mailbox)
+ end
+
+ # As for #copy(), but +set+ contains unique identifiers.
+ def uid_copy(set, mailbox)
+ copy_internal("UID COPY", set, mailbox)
+ end
+
+ # Sends a SORT command to sort messages in the mailbox.
+ # Returns an array of message sequence numbers. For example:
+ #
+ # p imap.sort(["FROM"], ["ALL"], "US-ASCII")
+ # #=> [1, 2, 3, 5, 6, 7, 8, 4, 9]
+ # p imap.sort(["DATE"], ["SUBJECT", "hello"], "US-ASCII")
+ # #=> [6, 7, 8, 1]
+ #
+ # See [SORT-THREAD-EXT] for more details.
+ def sort(sort_keys, search_keys, charset)
+ return sort_internal("SORT", sort_keys, search_keys, charset)
+ end
+
+ # As for #sort(), but returns an array of unique identifiers.
+ def uid_sort(sort_keys, search_keys, charset)
+ return sort_internal("UID SORT", sort_keys, search_keys, charset)
+ end
+
+ # Adds a response handler. For example, to detect when
+ # the server sends us a new EXISTS response (which normally
+ # indicates new messages being added to the mail box),
+ # you could add the following handler after selecting the
+ # mailbox.
+ #
+ # imap.add_response_handler { |resp|
+ # if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
+ # puts "Mailbox now has #{resp.data} messages"
+ # end
+ # }
+ #
+ def add_response_handler(handler = Proc.new)
+ @response_handlers.push(handler)
+ end
+
+ # Removes the response handler.
+ def remove_response_handler(handler)
+ @response_handlers.delete(handler)
+ end
+
+ # As for #search(), but returns message sequence numbers in threaded
+ # format, as a Net::IMAP::ThreadMember tree. The supported algorithms
+ # are:
+ #
+ # ORDEREDSUBJECT:: split into single-level threads according to subject,
+ # ordered by date.
+ # REFERENCES:: split into threads by parent/child relationships determined
+ # by which message is a reply to which.
+ #
+ # Unlike #search(), +charset+ is a required argument. US-ASCII
+ # and UTF-8 are sample values.
+ #
+ # See [SORT-THREAD-EXT] for more details.
+ def thread(algorithm, search_keys, charset)
+ return thread_internal("THREAD", algorithm, search_keys, charset)
+ end
+
+ # As for #thread(), but returns unique identifiers instead of
+ # message sequence numbers.
+ def uid_thread(algorithm, search_keys, charset)
+ return thread_internal("UID THREAD", algorithm, search_keys, charset)
+ end
+
+ # Decode a string from modified UTF-7 format to UTF-8.
+ #
+ # UTF-7 is a 7-bit encoding of Unicode [UTF7]. IMAP uses a
+ # slightly modified version of this to encode mailbox names
+ # containing non-ASCII characters; see [IMAP] section 5.1.3.
+ #
+ # Net::IMAP does _not_ automatically encode and decode
+ # mailbox names to and from utf7.
+ def self.decode_utf7(s)
+ return s.gsub(/&(.*?)-/n) {
+ if $1.empty?
+ "&"
+ else
+ base64 = $1.tr(",", "/")
+ x = base64.length % 4
+ if x > 0
+ base64.concat("=" * (4 - x))
+ end
+ base64.unpack("m")[0].unpack("n*").pack("U*")
+ end
+ }.force_encoding("UTF-8")
+ end
+
+ # Encode a string from UTF-8 format to modified UTF-7.
+ def self.encode_utf7(s)
+ return s.gsub(/(&)|([^\x20-\x25\x27-\x7e]+)/u) {
+ if $1
+ "&-"
+ else
+ base64 = [$&.unpack("U*").pack("n*")].pack("m")
+ "&" + base64.delete("=\n").tr("/", ",") + "-"
+ end
+ }.force_encoding("ASCII-8BIT")
+ end
+
+ private
+
+ CRLF = "\r\n" # :nodoc:
+ PORT = 143 # :nodoc:
+ SSL_PORT = 993 # :nodoc:
+
+ @@debug = false
+ @@authenticators = {}
+
+ # call-seq:
+ # Net::IMAP.new(host, options = {})
+ #
+ # Creates a new Net::IMAP object and connects it to the specified
+ # +host+.
+ #
+ # +options+ is an option hash, each key of which is a symbol.
+ #
+ # The available options are:
+ #
+ # port:: port number (default value is 143 for imap, or 993 for imaps)
+ # ssl:: if options[:ssl] is true, then an attempt will be made
+ # to use SSL (now TLS) to connect to the server. For this to work
+ # OpenSSL [OSSL] and the Ruby OpenSSL [RSSL] extensions need to
+ # be installed.
+ # if options[:ssl] is a hash, it's passed to
+ # OpenSSL::SSL::SSLContext#set_params as parameters.
+ #
+ # The most common errors are:
+ #
+ # Errno::ECONNREFUSED:: connection refused by +host+ or an intervening
+ # firewall.
+ # Errno::ETIMEDOUT:: connection timed out (possibly due to packets
+ # being dropped by an intervening firewall).
+ # Errno::ENETUNREACH:: there is no route to that network.
+ # SocketError:: hostname not known or other socket error.
+ # Net::IMAP::ByeResponseError:: we connected to the host, but they
+ # immediately said goodbye to us.
+ def initialize(host, port_or_options = {},
+ usessl = false, certs = nil, verify = true)
+ super()
+ @host = host
+ begin
+ options = port_or_options.to_hash
+ rescue NoMethodError
+ # for backward compatibility
+ options = {}
+ options[:port] = port_or_options
+ if usessl
+ options[:ssl] = create_ssl_params(certs, verify)
+ end
+ end
+ @port = options[:port] || (options[:ssl] ? SSL_PORT : PORT)
+ @tag_prefix = "RUBY"
+ @tagno = 0
+ @parser = ResponseParser.new
+ @sock = TCPSocket.open(@host, @port)
+ if options[:ssl]
+ start_tls_session(options[:ssl])
+ @usessl = true
+ else
+ @usessl = false
+ end
+ @responses = Hash.new([].freeze)
+ @tagged_responses = {}
+ @response_handlers = []
+ @tagged_response_arrival = new_cond
+ @continuation_request_arrival = new_cond
+ @logout_command_tag = nil
+ @debug_output_bol = true
+ @exception = nil
+
+ @greeting = get_response
+ if @greeting.name == "BYE"
+ @sock.close
+ raise ByeResponseError, @greeting.raw_data
+ end
+
+ @client_thread = Thread.current
+ @receiver_thread = Thread.start {
+ receive_responses
+ }
+ end
+
+ def receive_responses
+ while true
+ synchronize do
+ @exception = nil
+ end
+ begin
+ resp = get_response
+ rescue Exception => e
+ synchronize do
+ @sock.close
+ @exception = e
+ end
+ break
+ end
+ unless resp
+ synchronize do
+ @exception = EOFError.new("end of file reached")
+ end
+ break
+ end
+ begin
+ synchronize do
+ case resp
+ when TaggedResponse
+ @tagged_responses[resp.tag] = resp
+ @tagged_response_arrival.broadcast
+ if resp.tag == @logout_command_tag
+ return
+ end
+ when UntaggedResponse
+ record_response(resp.name, resp.data)
+ if resp.data.instance_of?(ResponseText) &&
+ (code = resp.data.code)
+ record_response(code.name, code.data)
+ end
+ if resp.name == "BYE" && @logout_command_tag.nil?
+ @sock.close
+ @exception = ByeResponseError.new(resp.raw_data)
+ break
+ end
+ when ContinuationRequest
+ @continuation_request_arrival.signal
+ end
+ @response_handlers.each do |handler|
+ handler.call(resp)
+ end
+ end
+ rescue Exception => e
+ @exception = e
+ synchronize do
+ @tagged_response_arrival.broadcast
+ @continuation_request_arrival.broadcast
+ end
+ end
+ end
+ synchronize do
+ @tagged_response_arrival.broadcast
+ @continuation_request_arrival.broadcast
+ end
+ end
+
+ def get_tagged_response(tag, cmd)
+ until @tagged_responses.key?(tag)
+ raise @exception if @exception
+ @tagged_response_arrival.wait
+ end
+ resp = @tagged_responses.delete(tag)
+ case resp.name
+ when /\A(?:NO)\z/ni
+ raise NoResponseError, resp.data.text
+ when /\A(?:BAD)\z/ni
+ raise BadResponseError, resp.data.text
+ else
+ return resp
+ end
+ end
+
+ def get_response
+ buff = ""
+ while true
+ s = @sock.gets(CRLF)
+ break unless s
+ buff.concat(s)
+ if /\{(\d+)\}\r\n/n =~ s
+ s = @sock.read($1.to_i)
+ buff.concat(s)
+ else
+ break
+ end
+ end
+ return nil if buff.length == 0
+ if @@debug
+ $stderr.print(buff.gsub(/^/n, "S: "))
+ end
+ return @parser.parse(buff)
+ end
+
+ def record_response(name, data)
+ unless @responses.has_key?(name)
+ @responses[name] = []
+ end
+ @responses[name].push(data)
+ end
+
+ def send_command(cmd, *args, &block)
+ synchronize do
+ args.each do |i|
+ validate_data(i)
+ end
+ tag = generate_tag
+ put_string(tag + " " + cmd)
+ args.each do |i|
+ put_string(" ")
+ send_data(i)
+ end
+ put_string(CRLF)
+ if cmd == "LOGOUT"
+ @logout_command_tag = tag
+ end
+ if block
+ add_response_handler(block)
+ end
+ begin
+ return get_tagged_response(tag, cmd)
+ ensure
+ if block
+ remove_response_handler(block)
+ end
+ end
+ end
+ end
+
+ def generate_tag
+ @tagno += 1
+ return format("%s%04d", @tag_prefix, @tagno)
+ end
+
+ def put_string(str)
+ @sock.print(str)
+ if @@debug
+ if @debug_output_bol
+ $stderr.print("C: ")
+ end
+ $stderr.print(str.gsub(/\n(?!\z)/n, "\nC: "))
+ if /\r\n\z/n.match(str)
+ @debug_output_bol = true
+ else
+ @debug_output_bol = false
+ end
+ end
+ end
+
+ def validate_data(data)
+ case data
+ when nil
+ when String
+ when Integer
+ if data < 0 || data >= 4294967296
+ raise DataFormatError, num.to_s
+ end
+ when Array
+ data.each do |i|
+ validate_data(i)
+ end
+ when Time
+ when Symbol
+ else
+ data.validate
+ end
+ end
+
+ def send_data(data)
+ case data
+ when nil
+ put_string("NIL")
+ when String
+ send_string_data(data)
+ when Integer
+ send_number_data(data)
+ when Array
+ send_list_data(data)
+ when Time
+ send_time_data(data)
+ when Symbol
+ send_symbol_data(data)
+ else
+ data.send_data(self)
+ end
+ end
+
+ def send_string_data(str)
+ case str
+ when ""
+ put_string('""')
+ when /[\x80-\xff\r\n]/n
+ # literal
+ send_literal(str)
+ when /[(){ \x00-\x1f\x7f%*"\\]/n
+ # quoted string
+ send_quoted_string(str)
+ else
+ put_string(str)
+ end
+ end
+
+ def send_quoted_string(str)
+ put_string('"' + str.gsub(/["\\]/n, "\\\\\\&") + '"')
+ end
+
+ def send_literal(str)
+ put_string("{" + str.length.to_s + "}" + CRLF)
+ @continuation_request_arrival.wait
+ raise @exception if @exception
+ put_string(str)
+ end
+
+ def send_number_data(num)
+ put_string(num.to_s)
+ end
+
+ def send_list_data(list)
+ put_string("(")
+ first = true
+ list.each do |i|
+ if first
+ first = false
+ else
+ put_string(" ")
+ end
+ send_data(i)
+ end
+ put_string(")")
+ end
+
+ DATE_MONTH = %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
+
+ def send_time_data(time)
+ t = time.dup.gmtime
+ s = format('"%2d-%3s-%4d %02d:%02d:%02d +0000"',
+ t.day, DATE_MONTH[t.month - 1], t.year,
+ t.hour, t.min, t.sec)
+ put_string(s)
+ end
+
+ def send_symbol_data(symbol)
+ put_string("\\" + symbol.to_s)
+ end
+
+ def search_internal(cmd, keys, charset)
+ if keys.instance_of?(String)
+ keys = [RawData.new(keys)]
+ else
+ normalize_searching_criteria(keys)
+ end
+ synchronize do
+ if charset
+ send_command(cmd, "CHARSET", charset, *keys)
+ else
+ send_command(cmd, *keys)
+ end
+ return @responses.delete("SEARCH")[-1]
+ end
+ end
+
+ def fetch_internal(cmd, set, attr)
+ if attr.instance_of?(String)
+ attr = RawData.new(attr)
+ end
+ synchronize do
+ @responses.delete("FETCH")
+ send_command(cmd, MessageSet.new(set), attr)
+ return @responses.delete("FETCH")
+ end
+ end
+
+ def store_internal(cmd, set, attr, flags)
+ if attr.instance_of?(String)
+ attr = RawData.new(attr)
+ end
+ synchronize do
+ @responses.delete("FETCH")
+ send_command(cmd, MessageSet.new(set), attr, flags)
+ return @responses.delete("FETCH")
+ end
+ end
+
+ def copy_internal(cmd, set, mailbox)
+ send_command(cmd, MessageSet.new(set), mailbox)
+ end
+
+ def sort_internal(cmd, sort_keys, search_keys, charset)
+ if search_keys.instance_of?(String)
+ search_keys = [RawData.new(search_keys)]
+ else
+ normalize_searching_criteria(search_keys)
+ end
+ normalize_searching_criteria(search_keys)
+ synchronize do
+ send_command(cmd, sort_keys, charset, *search_keys)
+ return @responses.delete("SORT")[-1]
+ end
+ end
+
+ def thread_internal(cmd, algorithm, search_keys, charset)
+ if search_keys.instance_of?(String)
+ search_keys = [RawData.new(search_keys)]
+ else
+ normalize_searching_criteria(search_keys)
+ end
+ normalize_searching_criteria(search_keys)
+ send_command(cmd, algorithm, charset, *search_keys)
+ return @responses.delete("THREAD")[-1]
+ end
+
+ def normalize_searching_criteria(keys)
+ keys.collect! do |i|
+ case i
+ when -1, Range, Array
+ MessageSet.new(i)
+ else
+ i
+ end
+ end
+ end
+
+ def create_ssl_params(certs = nil, verify = true)
+ params = {}
+ if certs
+ if File.file?(certs)
+ params[:ca_file] = certs
+ elsif File.directory?(certs)
+ params[:ca_path] = certs
+ end
+ end
+ if verify
+ params[:verify_mode] = VERIFY_PEER
+ else
+ params[:verify_mode] = VERIFY_NONE
+ end
+ return params
+ end
+
+ def start_tls_session(params = {})
+ unless defined?(OpenSSL)
+ raise "SSL extension not installed"
+ end
+ if @sock.kind_of?(OpenSSL::SSL::SSLSocket)
+ raise RuntimeError, "already using SSL"
+ end
+ begin
+ params = params.to_hash
+ rescue NoMethodError
+ params = {}
+ end
+ context = SSLContext.new
+ context.set_params(params)
+ if defined?(VerifyCallbackProc)
+ context.verify_callback = VerifyCallbackProc
+ end
+ @sock = SSLSocket.new(@sock, context)
+ @sock.sync_close = true
+ @sock.connect
+ if context.verify_mode != VERIFY_NONE
+ @sock.post_connection_check(@host)
+ end
+ end
+
+ class RawData # :nodoc:
+ def send_data(imap)
+ imap.send(:put_string, @data)
+ end
+
+ def validate
+ end
+
+ private
+
+ def initialize(data)
+ @data = data
+ end
+ end
+
+ class Atom # :nodoc:
+ def send_data(imap)
+ imap.send(:put_string, @data)
+ end
+
+ def validate
+ end
+
+ private
+
+ def initialize(data)
+ @data = data
+ end
+ end
+
+ class QuotedString # :nodoc:
+ def send_data(imap)
+ imap.send(:send_quoted_string, @data)
+ end
+
+ def validate
+ end
+
+ private
+
+ def initialize(data)
+ @data = data
+ end
+ end
+
+ class Literal # :nodoc:
+ def send_data(imap)
+ imap.send(:send_literal, @data)
+ end
+
+ def validate
+ end
+
+ private
+
+ def initialize(data)
+ @data = data
+ end
+ end
+
+ class MessageSet # :nodoc:
+ def send_data(imap)
+ imap.send(:put_string, format_internal(@data))
+ end
+
+ def validate
+ validate_internal(@data)
+ end
+
+ private
+
+ def initialize(data)
+ @data = data
+ end
+
+ def format_internal(data)
+ case data
+ when "*"
+ return data
+ when Integer
+ if data == -1
+ return "*"
+ else
+ return data.to_s
+ end
+ when Range
+ return format_internal(data.first) +
+ ":" + format_internal(data.last)
+ when Array
+ return data.collect {|i| format_internal(i)}.join(",")
+ when ThreadMember
+ return data.seqno.to_s +
+ ":" + data.children.collect {|i| format_internal(i).join(",")}
+ end
+ end
+
+ def validate_internal(data)
+ case data
+ when "*"
+ when Integer
+ ensure_nz_number(data)
+ when Range
+ when Array
+ data.each do |i|
+ validate_internal(i)
+ end
+ when ThreadMember
+ data.children.each do |i|
+ validate_internal(i)
+ end
+ else
+ raise DataFormatError, data.inspect
+ end
+ end
+
+ def ensure_nz_number(num)
+ if num < -1 || num == 0 || num >= 4294967296
+ msg = "nz_number must be non-zero unsigned 32-bit integer: " +
+ num.inspect
+ raise DataFormatError, msg
+ end
+ end
+ end
+
+ # Net::IMAP::ContinuationRequest represents command continuation requests.
+ #
+ # The command continuation request response is indicated by a "+" token
+ # instead of a tag. This form of response indicates that the server is
+ # ready to accept the continuation of a command from the client. The
+ # remainder of this response is a line of text.
+ #
+ # continue_req ::= "+" SPACE (resp_text / base64)
+ #
+ # ==== Fields:
+ #
+ # data:: Returns the data (Net::IMAP::ResponseText).
+ #
+ # raw_data:: Returns the raw data string.
+ ContinuationRequest = Struct.new(:data, :raw_data)
+
+ # Net::IMAP::UntaggedResponse represents untagged responses.
+ #
+ # Data transmitted by the server to the client and status responses
+ # that do not indicate command completion are prefixed with the token
+ # "*", and are called untagged responses.
+ #
+ # response_data ::= "*" SPACE (resp_cond_state / resp_cond_bye /
+ # mailbox_data / message_data / capability_data)
+ #
+ # ==== Fields:
+ #
+ # name:: Returns the name such as "FLAGS", "LIST", "FETCH"....
+ #
+ # data:: Returns the data such as an array of flag symbols,
+ # a ((<Net::IMAP::MailboxList>)) object....
+ #
+ # raw_data:: Returns the raw data string.
+ UntaggedResponse = Struct.new(:name, :data, :raw_data)
+
+ # Net::IMAP::TaggedResponse represents tagged responses.
+ #
+ # The server completion result response indicates the success or
+ # failure of the operation. It is tagged with the same tag as the
+ # client command which began the operation.
+ #
+ # response_tagged ::= tag SPACE resp_cond_state CRLF
+ #
+ # tag ::= 1*<any ATOM_CHAR except "+">
+ #
+ # resp_cond_state ::= ("OK" / "NO" / "BAD") SPACE resp_text
+ #
+ # ==== Fields:
+ #
+ # tag:: Returns the tag.
+ #
+ # name:: Returns the name. the name is one of "OK", "NO", "BAD".
+ #
+ # data:: Returns the data. See ((<Net::IMAP::ResponseText>)).
+ #
+ # raw_data:: Returns the raw data string.
+ #
+ TaggedResponse = Struct.new(:tag, :name, :data, :raw_data)
+
+ # Net::IMAP::ResponseText represents texts of responses.
+ # The text may be prefixed by the response code.
+ #
+ # resp_text ::= ["[" resp_text_code "]" SPACE] (text_mime2 / text)
+ # ;; text SHOULD NOT begin with "[" or "="
+ #
+ # ==== Fields:
+ #
+ # code:: Returns the response code. See ((<Net::IMAP::ResponseCode>)).
+ #
+ # text:: Returns the text.
+ #
+ ResponseText = Struct.new(:code, :text)
+
+ #
+ # Net::IMAP::ResponseCode represents response codes.
+ #
+ # resp_text_code ::= "ALERT" / "PARSE" /
+ # "PERMANENTFLAGS" SPACE "(" #(flag / "\*") ")" /
+ # "READ-ONLY" / "READ-WRITE" / "TRYCREATE" /
+ # "UIDVALIDITY" SPACE nz_number /
+ # "UNSEEN" SPACE nz_number /
+ # atom [SPACE 1*<any TEXT_CHAR except "]">]
+ #
+ # ==== Fields:
+ #
+ # name:: Returns the name such as "ALERT", "PERMANENTFLAGS", "UIDVALIDITY"....
+ #
+ # data:: Returns the data if it exists.
+ #
+ ResponseCode = Struct.new(:name, :data)
+
+ # Net::IMAP::MailboxList represents contents of the LIST response.
+ #
+ # mailbox_list ::= "(" #("\Marked" / "\Noinferiors" /
+ # "\Noselect" / "\Unmarked" / flag_extension) ")"
+ # SPACE (<"> QUOTED_CHAR <"> / nil) SPACE mailbox
+ #
+ # ==== Fields:
+ #
+ # attr:: Returns the name attributes. Each name attribute is a symbol
+ # capitalized by String#capitalize, such as :Noselect (not :NoSelect).
+ #
+ # delim:: Returns the hierarchy delimiter
+ #
+ # name:: Returns the mailbox name.
+ #
+ MailboxList = Struct.new(:attr, :delim, :name)
+
+ # Net::IMAP::MailboxQuota represents contents of GETQUOTA response.
+ # This object can also be a response to GETQUOTAROOT. In the syntax
+ # specification below, the delimiter used with the "#" construct is a
+ # single space (SPACE).
+ #
+ # quota_list ::= "(" #quota_resource ")"
+ #
+ # quota_resource ::= atom SPACE number SPACE number
+ #
+ # quota_response ::= "QUOTA" SPACE astring SPACE quota_list
+ #
+ # ==== Fields:
+ #
+ # mailbox:: The mailbox with the associated quota.
+ #
+ # usage:: Current storage usage of mailbox.
+ #
+ # quota:: Quota limit imposed on mailbox.
+ #
+ MailboxQuota = Struct.new(:mailbox, :usage, :quota)
+
+ # Net::IMAP::MailboxQuotaRoot represents part of the GETQUOTAROOT
+ # response. (GETQUOTAROOT can also return Net::IMAP::MailboxQuota.)
+ #
+ # quotaroot_response ::= "QUOTAROOT" SPACE astring *(SPACE astring)
+ #
+ # ==== Fields:
+ #
+ # mailbox:: The mailbox with the associated quota.
+ #
+ # quotaroots:: Zero or more quotaroots that effect the quota on the
+ # specified mailbox.
+ #
+ MailboxQuotaRoot = Struct.new(:mailbox, :quotaroots)
+
+ # Net::IMAP::MailboxACLItem represents response from GETACL.
+ #
+ # acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE rights)
+ #
+ # identifier ::= astring
+ #
+ # rights ::= astring
+ #
+ # ==== Fields:
+ #
+ # user:: Login name that has certain rights to the mailbox
+ # that was specified with the getacl command.
+ #
+ # rights:: The access rights the indicated user has to the
+ # mailbox.
+ #
+ MailboxACLItem = Struct.new(:user, :rights)
+
+ # Net::IMAP::StatusData represents contents of the STATUS response.
+ #
+ # ==== Fields:
+ #
+ # mailbox:: Returns the mailbox name.
+ #
+ # attr:: Returns a hash. Each key is one of "MESSAGES", "RECENT", "UIDNEXT",
+ # "UIDVALIDITY", "UNSEEN". Each value is a number.
+ #
+ StatusData = Struct.new(:mailbox, :attr)
+
+ # Net::IMAP::FetchData represents contents of the FETCH response.
+ #
+ # ==== Fields:
+ #
+ # seqno:: Returns the message sequence number.
+ # (Note: not the unique identifier, even for the UID command response.)
+ #
+ # attr:: Returns a hash. Each key is a data item name, and each value is
+ # its value.
+ #
+ # The current data items are:
+ #
+ # [BODY]
+ # A form of BODYSTRUCTURE without extension data.
+ # [BODY[<section>]<<origin_octet>>]
+ # A string expressing the body contents of the specified section.
+ # [BODYSTRUCTURE]
+ # An object that describes the [MIME-IMB] body structure of a message.
+ # See Net::IMAP::BodyTypeBasic, Net::IMAP::BodyTypeText,
+ # Net::IMAP::BodyTypeMessage, Net::IMAP::BodyTypeMultipart.
+ # [ENVELOPE]
+ # A Net::IMAP::Envelope object that describes the envelope
+ # structure of a message.
+ # [FLAGS]
+ # A array of flag symbols that are set for this message. flag symbols
+ # are capitalized by String#capitalize.
+ # [INTERNALDATE]
+ # A string representing the internal date of the message.
+ # [RFC822]
+ # Equivalent to BODY[].
+ # [RFC822.HEADER]
+ # Equivalent to BODY.PEEK[HEADER].
+ # [RFC822.SIZE]
+ # A number expressing the [RFC-822] size of the message.
+ # [RFC822.TEXT]
+ # Equivalent to BODY[TEXT].
+ # [UID]
+ # A number expressing the unique identifier of the message.
+ #
+ FetchData = Struct.new(:seqno, :attr)
+
+ # Net::IMAP::Envelope represents envelope structures of messages.
+ #
+ # ==== Fields:
+ #
+ # date:: Returns a string that represents the date.
+ #
+ # subject:: Returns a string that represents the subject.
+ #
+ # from:: Returns an array of Net::IMAP::Address that represents the from.
+ #
+ # sender:: Returns an array of Net::IMAP::Address that represents the sender.
+ #
+ # reply_to:: Returns an array of Net::IMAP::Address that represents the reply-to.
+ #
+ # to:: Returns an array of Net::IMAP::Address that represents the to.
+ #
+ # cc:: Returns an array of Net::IMAP::Address that represents the cc.
+ #
+ # bcc:: Returns an array of Net::IMAP::Address that represents the bcc.
+ #
+ # in_reply_to:: Returns a string that represents the in-reply-to.
+ #
+ # message_id:: Returns a string that represents the message-id.
+ #
+ Envelope = Struct.new(:date, :subject, :from, :sender, :reply_to,
+ :to, :cc, :bcc, :in_reply_to, :message_id)
+
+ #
+ # Net::IMAP::Address represents electronic mail addresses.
+ #
+ # ==== Fields:
+ #
+ # name:: Returns the phrase from [RFC-822] mailbox.
+ #
+ # route:: Returns the route from [RFC-822] route-addr.
+ #
+ # mailbox:: nil indicates end of [RFC-822] group.
+ # If non-nil and host is nil, returns [RFC-822] group name.
+ # Otherwise, returns [RFC-822] local-part
+ #
+ # host:: nil indicates [RFC-822] group syntax.
+ # Otherwise, returns [RFC-822] domain name.
+ #
+ Address = Struct.new(:name, :route, :mailbox, :host)
+
+ #
+ # Net::IMAP::ContentDisposition represents Content-Disposition fields.
+ #
+ # ==== Fields:
+ #
+ # dsp_type:: Returns the disposition type.
+ #
+ # param:: Returns a hash that represents parameters of the Content-Disposition
+ # field.
+ #
+ ContentDisposition = Struct.new(:dsp_type, :param)
+
+ # Net::IMAP::ThreadMember represents a thread-node returned
+ # by Net::IMAP#thread
+ #
+ # ==== Fields:
+ #
+ # seqno:: The sequence number of this message.
+ #
+ # children:: an array of Net::IMAP::ThreadMember objects for mail
+ # items that are children of this in the thread.
+ #
+ ThreadMember = Struct.new(:seqno, :children)
+
+ # Net::IMAP::BodyTypeBasic represents basic body structures of messages.
+ #
+ # ==== Fields:
+ #
+ # media_type:: Returns the content media type name as defined in [MIME-IMB].
+ #
+ # subtype:: Returns the content subtype name as defined in [MIME-IMB].
+ #
+ # param:: Returns a hash that represents parameters as defined in [MIME-IMB].
+ #
+ # content_id:: Returns a string giving the content id as defined in [MIME-IMB].
+ #
+ # description:: Returns a string giving the content description as defined in
+ # [MIME-IMB].
+ #
+ # encoding:: Returns a string giving the content transfer encoding as defined in
+ # [MIME-IMB].
+ #
+ # size:: Returns a number giving the size of the body in octets.
+ #
+ # md5:: Returns a string giving the body MD5 value as defined in [MD5].
+ #
+ # disposition:: Returns a Net::IMAP::ContentDisposition object giving
+ # the content disposition.
+ #
+ # language:: Returns a string or an array of strings giving the body
+ # language value as defined in [LANGUAGE-TAGS].
+ #
+ # extension:: Returns extension data.
+ #
+ # multipart?:: Returns false.
+ #
+ class BodyTypeBasic < Struct.new(:media_type, :subtype,
+ :param, :content_id,
+ :description, :encoding, :size,
+ :md5, :disposition, :language,
+ :extension)
+ def multipart?
+ return false
+ end
+
+ # Obsolete: use +subtype+ instead. Calling this will
+ # generate a warning message to +stderr+, then return
+ # the value of +subtype+.
+ def media_subtype
+ $stderr.printf("warning: media_subtype is obsolete.\n")
+ $stderr.printf(" use subtype instead.\n")
+ return subtype
+ end
+ end
+
+ # Net::IMAP::BodyTypeText represents TEXT body structures of messages.
+ #
+ # ==== Fields:
+ #
+ # lines:: Returns the size of the body in text lines.
+ #
+ # And Net::IMAP::BodyTypeText has all fields of Net::IMAP::BodyTypeBasic.
+ #
+ class BodyTypeText < Struct.new(:media_type, :subtype,
+ :param, :content_id,
+ :description, :encoding, :size,
+ :lines,
+ :md5, :disposition, :language,
+ :extension)
+ def multipart?
+ return false
+ end
+
+ # Obsolete: use +subtype+ instead. Calling this will
+ # generate a warning message to +stderr+, then return
+ # the value of +subtype+.
+ def media_subtype
+ $stderr.printf("warning: media_subtype is obsolete.\n")
+ $stderr.printf(" use subtype instead.\n")
+ return subtype
+ end
+ end
+
+ # Net::IMAP::BodyTypeMessage represents MESSAGE/RFC822 body structures of messages.
+ #
+ # ==== Fields:
+ #
+ # envelope:: Returns a Net::IMAP::Envelope giving the envelope structure.
+ #
+ # body:: Returns an object giving the body structure.
+ #
+ # And Net::IMAP::BodyTypeMessage has all methods of Net::IMAP::BodyTypeText.
+ #
+ class BodyTypeMessage < Struct.new(:media_type, :subtype,
+ :param, :content_id,
+ :description, :encoding, :size,
+ :envelope, :body, :lines,
+ :md5, :disposition, :language,
+ :extension)
+ def multipart?
+ return false
+ end
+
+ # Obsolete: use +subtype+ instead. Calling this will
+ # generate a warning message to +stderr+, then return
+ # the value of +subtype+.
+ def media_subtype
+ $stderr.printf("warning: media_subtype is obsolete.\n")
+ $stderr.printf(" use subtype instead.\n")
+ return subtype
+ end
+ end
+
+ # Net::IMAP::BodyTypeMultipart represents multipart body structures
+ # of messages.
+ #
+ # ==== Fields:
+ #
+ # media_type:: Returns the content media type name as defined in [MIME-IMB].
+ #
+ # subtype:: Returns the content subtype name as defined in [MIME-IMB].
+ #
+ # parts:: Returns multiple parts.
+ #
+ # param:: Returns a hash that represents parameters as defined in [MIME-IMB].
+ #
+ # disposition:: Returns a Net::IMAP::ContentDisposition object giving
+ # the content disposition.
+ #
+ # language:: Returns a string or an array of strings giving the body
+ # language value as defined in [LANGUAGE-TAGS].
+ #
+ # extension:: Returns extension data.
+ #
+ # multipart?:: Returns true.
+ #
+ class BodyTypeMultipart < Struct.new(:media_type, :subtype,
+ :parts,
+ :param, :disposition, :language,
+ :extension)
+ def multipart?
+ return true
+ end
+
+ # Obsolete: use +subtype+ instead. Calling this will
+ # generate a warning message to +stderr+, then return
+ # the value of +subtype+.
+ def media_subtype
+ $stderr.printf("warning: media_subtype is obsolete.\n")
+ $stderr.printf(" use subtype instead.\n")
+ return subtype
+ end
+ end
+
+ class ResponseParser # :nodoc:
+ def parse(str)
+ @str = str
+ @pos = 0
+ @lex_state = EXPR_BEG
+ @token = nil
+ return response
+ end
+
+ private
+
+ EXPR_BEG = :EXPR_BEG
+ EXPR_DATA = :EXPR_DATA
+ EXPR_TEXT = :EXPR_TEXT
+ EXPR_RTEXT = :EXPR_RTEXT
+ EXPR_CTEXT = :EXPR_CTEXT
+
+ T_SPACE = :SPACE
+ T_NIL = :NIL
+ T_NUMBER = :NUMBER
+ T_ATOM = :ATOM
+ T_QUOTED = :QUOTED
+ T_LPAR = :LPAR
+ T_RPAR = :RPAR
+ T_BSLASH = :BSLASH
+ T_STAR = :STAR
+ T_LBRA = :LBRA
+ T_RBRA = :RBRA
+ T_LITERAL = :LITERAL
+ T_PLUS = :PLUS
+ T_PERCENT = :PERCENT
+ T_CRLF = :CRLF
+ T_EOF = :EOF
+ T_TEXT = :TEXT
+
+ BEG_REGEXP = /\G(?:\
+(?# 1: SPACE )( +)|\
+(?# 2: NIL )(NIL)(?=[\x80-\xff(){ \x00-\x1f\x7f%*"\\\[\]+])|\
+(?# 3: NUMBER )(\d+)(?=[\x80-\xff(){ \x00-\x1f\x7f%*"\\\[\]+])|\
+(?# 4: ATOM )([^\x80-\xff(){ \x00-\x1f\x7f%*"\\\[\]+]+)|\
+(?# 5: QUOTED )"((?:[^\x00\r\n"\\]|\\["\\])*)"|\
+(?# 6: LPAR )(\()|\
+(?# 7: RPAR )(\))|\
+(?# 8: BSLASH )(\\)|\
+(?# 9: STAR )(\*)|\
+(?# 10: LBRA )(\[)|\
+(?# 11: RBRA )(\])|\
+(?# 12: LITERAL )\{(\d+)\}\r\n|\
+(?# 13: PLUS )(\+)|\
+(?# 14: PERCENT )(%)|\
+(?# 15: CRLF )(\r\n)|\
+(?# 16: EOF )(\z))/ni
+
+ DATA_REGEXP = /\G(?:\
+(?# 1: SPACE )( )|\
+(?# 2: NIL )(NIL)|\
+(?# 3: NUMBER )(\d+)|\
+(?# 4: QUOTED )"((?:[^\x00\r\n"\\]|\\["\\])*)"|\
+(?# 5: LITERAL )\{(\d+)\}\r\n|\
+(?# 6: LPAR )(\()|\
+(?# 7: RPAR )(\)))/ni
+
+ TEXT_REGEXP = /\G(?:\
+(?# 1: TEXT )([^\x00\r\n]*))/ni
+
+ RTEXT_REGEXP = /\G(?:\
+(?# 1: LBRA )(\[)|\
+(?# 2: TEXT )([^\x00\r\n]*))/ni
+
+ CTEXT_REGEXP = /\G(?:\
+(?# 1: TEXT )([^\x00\r\n\]]*))/ni
+
+ Token = Struct.new(:symbol, :value)
+
+ def response
+ token = lookahead
+ case token.symbol
+ when T_PLUS
+ result = continue_req
+ when T_STAR
+ result = response_untagged
+ else
+ result = response_tagged
+ end
+ match(T_CRLF)
+ match(T_EOF)
+ return result
+ end
+
+ def continue_req
+ match(T_PLUS)
+ match(T_SPACE)
+ return ContinuationRequest.new(resp_text, @str)
+ end
+
+ def response_untagged
+ match(T_STAR)
+ match(T_SPACE)
+ token = lookahead
+ if token.symbol == T_NUMBER
+ return numeric_response
+ elsif token.symbol == T_ATOM
+ case token.value
+ when /\A(?:OK|NO|BAD|BYE|PREAUTH)\z/ni
+ return response_cond
+ when /\A(?:FLAGS)\z/ni
+ return flags_response
+ when /\A(?:LIST|LSUB)\z/ni
+ return list_response
+ when /\A(?:QUOTA)\z/ni
+ return getquota_response
+ when /\A(?:QUOTAROOT)\z/ni
+ return getquotaroot_response
+ when /\A(?:ACL)\z/ni
+ return getacl_response
+ when /\A(?:SEARCH|SORT)\z/ni
+ return search_response
+ when /\A(?:THREAD)\z/ni
+ return thread_response
+ when /\A(?:STATUS)\z/ni
+ return status_response
+ when /\A(?:CAPABILITY)\z/ni
+ return capability_response
+ else
+ return text_response
+ end
+ else
+ parse_error("unexpected token %s", token.symbol)
+ end
+ end
+
+ def response_tagged
+ tag = atom
+ match(T_SPACE)
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return TaggedResponse.new(tag, name, resp_text, @str)
+ end
+
+ def response_cond
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return UntaggedResponse.new(name, resp_text, @str)
+ end
+
+ def numeric_response
+ n = number
+ match(T_SPACE)
+ token = match(T_ATOM)
+ name = token.value.upcase
+ case name
+ when "EXISTS", "RECENT", "EXPUNGE"
+ return UntaggedResponse.new(name, n, @str)
+ when "FETCH"
+ shift_token
+ match(T_SPACE)
+ data = FetchData.new(n, msg_att)
+ return UntaggedResponse.new(name, data, @str)
+ end
+ end
+
+ def msg_att
+ match(T_LPAR)
+ attr = {}
+ while true
+ token = lookahead
+ case token.symbol
+ when T_RPAR
+ shift_token
+ break
+ when T_SPACE
+ shift_token
+ token = lookahead
+ end
+ case token.value
+ when /\A(?:ENVELOPE)\z/ni
+ name, val = envelope_data
+ when /\A(?:FLAGS)\z/ni
+ name, val = flags_data
+ when /\A(?:INTERNALDATE)\z/ni
+ name, val = internaldate_data
+ when /\A(?:RFC822(?:\.HEADER|\.TEXT)?)\z/ni
+ name, val = rfc822_text
+ when /\A(?:RFC822\.SIZE)\z/ni
+ name, val = rfc822_size
+ when /\A(?:BODY(?:STRUCTURE)?)\z/ni
+ name, val = body_data
+ when /\A(?:UID)\z/ni
+ name, val = uid_data
+ else
+ parse_error("unknown attribute `%s'", token.value)
+ end
+ attr[name] = val
+ end
+ return attr
+ end
+
+ def envelope_data
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return name, envelope
+ end
+
+ def envelope
+ @lex_state = EXPR_DATA
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ result = nil
+ else
+ match(T_LPAR)
+ date = nstring
+ match(T_SPACE)
+ subject = nstring
+ match(T_SPACE)
+ from = address_list
+ match(T_SPACE)
+ sender = address_list
+ match(T_SPACE)
+ reply_to = address_list
+ match(T_SPACE)
+ to = address_list
+ match(T_SPACE)
+ cc = address_list
+ match(T_SPACE)
+ bcc = address_list
+ match(T_SPACE)
+ in_reply_to = nstring
+ match(T_SPACE)
+ message_id = nstring
+ match(T_RPAR)
+ result = Envelope.new(date, subject, from, sender, reply_to,
+ to, cc, bcc, in_reply_to, message_id)
+ end
+ @lex_state = EXPR_BEG
+ return result
+ end
+
+ def flags_data
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return name, flag_list
+ end
+
+ def internaldate_data
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ token = match(T_QUOTED)
+ return name, token.value
+ end
+
+ def rfc822_text
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return name, nstring
+ end
+
+ def rfc822_size
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return name, number
+ end
+
+ def body_data
+ token = match(T_ATOM)
+ name = token.value.upcase
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ return name, body
+ end
+ name.concat(section)
+ token = lookahead
+ if token.symbol == T_ATOM
+ name.concat(token.value)
+ shift_token
+ end
+ match(T_SPACE)
+ data = nstring
+ return name, data
+ end
+
+ def body
+ @lex_state = EXPR_DATA
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ result = nil
+ else
+ match(T_LPAR)
+ token = lookahead
+ if token.symbol == T_LPAR
+ result = body_type_mpart
+ else
+ result = body_type_1part
+ end
+ match(T_RPAR)
+ end
+ @lex_state = EXPR_BEG
+ return result
+ end
+
+ def body_type_1part
+ token = lookahead
+ case token.value
+ when /\A(?:TEXT)\z/ni
+ return body_type_text
+ when /\A(?:MESSAGE)\z/ni
+ return body_type_msg
+ else
+ return body_type_basic
+ end
+ end
+
+ def body_type_basic
+ mtype, msubtype = media_type
+ token = lookahead
+ if token.symbol == T_RPAR
+ return BodyTypeBasic.new(mtype, msubtype)
+ end
+ match(T_SPACE)
+ param, content_id, desc, enc, size = body_fields
+ md5, disposition, language, extension = body_ext_1part
+ return BodyTypeBasic.new(mtype, msubtype,
+ param, content_id,
+ desc, enc, size,
+ md5, disposition, language, extension)
+ end
+
+ def body_type_text
+ mtype, msubtype = media_type
+ match(T_SPACE)
+ param, content_id, desc, enc, size = body_fields
+ match(T_SPACE)
+ lines = number
+ md5, disposition, language, extension = body_ext_1part
+ return BodyTypeText.new(mtype, msubtype,
+ param, content_id,
+ desc, enc, size,
+ lines,
+ md5, disposition, language, extension)
+ end
+
+ def body_type_msg
+ mtype, msubtype = media_type
+ match(T_SPACE)
+ param, content_id, desc, enc, size = body_fields
+ match(T_SPACE)
+ env = envelope
+ match(T_SPACE)
+ b = body
+ match(T_SPACE)
+ lines = number
+ md5, disposition, language, extension = body_ext_1part
+ return BodyTypeMessage.new(mtype, msubtype,
+ param, content_id,
+ desc, enc, size,
+ env, b, lines,
+ md5, disposition, language, extension)
+ end
+
+ def body_type_mpart
+ parts = []
+ while true
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ break
+ end
+ parts.push(body)
+ end
+ mtype = "MULTIPART"
+ msubtype = case_insensitive_string
+ param, disposition, language, extension = body_ext_mpart
+ return BodyTypeMultipart.new(mtype, msubtype, parts,
+ param, disposition, language,
+ extension)
+ end
+
+ def media_type
+ mtype = case_insensitive_string
+ match(T_SPACE)
+ msubtype = case_insensitive_string
+ return mtype, msubtype
+ end
+
+ def body_fields
+ param = body_fld_param
+ match(T_SPACE)
+ content_id = nstring
+ match(T_SPACE)
+ desc = nstring
+ match(T_SPACE)
+ enc = case_insensitive_string
+ match(T_SPACE)
+ size = number
+ return param, content_id, desc, enc, size
+ end
+
+ def body_fld_param
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ return nil
+ end
+ match(T_LPAR)
+ param = {}
+ while true
+ token = lookahead
+ case token.symbol
+ when T_RPAR
+ shift_token
+ break
+ when T_SPACE
+ shift_token
+ end
+ name = case_insensitive_string
+ match(T_SPACE)
+ val = string
+ param[name] = val
+ end
+ return param
+ end
+
+ def body_ext_1part
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ else
+ return nil
+ end
+ md5 = nstring
+
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ else
+ return md5
+ end
+ disposition = body_fld_dsp
+
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ else
+ return md5, disposition
+ end
+ language = body_fld_lang
+
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ else
+ return md5, disposition, language
+ end
+
+ extension = body_extensions
+ return md5, disposition, language, extension
+ end
+
+ def body_ext_mpart
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ else
+ return nil
+ end
+ param = body_fld_param
+
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ else
+ return param
+ end
+ disposition = body_fld_dsp
+ match(T_SPACE)
+ language = body_fld_lang
+
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ else
+ return param, disposition, language
+ end
+
+ extension = body_extensions
+ return param, disposition, language, extension
+ end
+
+ def body_fld_dsp
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ return nil
+ end
+ match(T_LPAR)
+ dsp_type = case_insensitive_string
+ match(T_SPACE)
+ param = body_fld_param
+ match(T_RPAR)
+ return ContentDisposition.new(dsp_type, param)
+ end
+
+ def body_fld_lang
+ token = lookahead
+ if token.symbol == T_LPAR
+ shift_token
+ result = []
+ while true
+ token = lookahead
+ case token.symbol
+ when T_RPAR
+ shift_token
+ return result
+ when T_SPACE
+ shift_token
+ end
+ result.push(case_insensitive_string)
+ end
+ else
+ lang = nstring
+ if lang
+ return lang.upcase
+ else
+ return lang
+ end
+ end
+ end
+
+ def body_extensions
+ result = []
+ while true
+ token = lookahead
+ case token.symbol
+ when T_RPAR
+ return result
+ when T_SPACE
+ shift_token
+ end
+ result.push(body_extension)
+ end
+ end
+
+ def body_extension
+ token = lookahead
+ case token.symbol
+ when T_LPAR
+ shift_token
+ result = body_extensions
+ match(T_RPAR)
+ return result
+ when T_NUMBER
+ return number
+ else
+ return nstring
+ end
+ end
+
+ def section
+ str = ""
+ token = match(T_LBRA)
+ str.concat(token.value)
+ token = match(T_ATOM, T_NUMBER, T_RBRA)
+ if token.symbol == T_RBRA
+ str.concat(token.value)
+ return str
+ end
+ str.concat(token.value)
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ str.concat(token.value)
+ token = match(T_LPAR)
+ str.concat(token.value)
+ while true
+ token = lookahead
+ case token.symbol
+ when T_RPAR
+ str.concat(token.value)
+ shift_token
+ break
+ when T_SPACE
+ shift_token
+ str.concat(token.value)
+ end
+ str.concat(format_string(astring))
+ end
+ end
+ token = match(T_RBRA)
+ str.concat(token.value)
+ return str
+ end
+
+ def format_string(str)
+ case str
+ when ""
+ return '""'
+ when /[\x80-\xff\r\n]/n
+ # literal
+ return "{" + str.length.to_s + "}" + CRLF + str
+ when /[(){ \x00-\x1f\x7f%*"\\]/n
+ # quoted string
+ return '"' + str.gsub(/["\\]/n, "\\\\\\&") + '"'
+ else
+ # atom
+ return str
+ end
+ end
+
+ def uid_data
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return name, number
+ end
+
+ def text_response
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ @lex_state = EXPR_TEXT
+ token = match(T_TEXT)
+ @lex_state = EXPR_BEG
+ return UntaggedResponse.new(name, token.value)
+ end
+
+ def flags_response
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return UntaggedResponse.new(name, flag_list, @str)
+ end
+
+ def list_response
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ return UntaggedResponse.new(name, mailbox_list, @str)
+ end
+
+ def mailbox_list
+ attr = flag_list
+ match(T_SPACE)
+ token = match(T_QUOTED, T_NIL)
+ if token.symbol == T_NIL
+ delim = nil
+ else
+ delim = token.value
+ end
+ match(T_SPACE)
+ name = astring
+ return MailboxList.new(attr, delim, name)
+ end
+
+ def getquota_response
+ # If quota never established, get back
+ # `NO Quota root does not exist'.
+ # If quota removed, get `()' after the
+ # folder spec with no mention of `STORAGE'.
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ mailbox = astring
+ match(T_SPACE)
+ match(T_LPAR)
+ token = lookahead
+ case token.symbol
+ when T_RPAR
+ shift_token
+ data = MailboxQuota.new(mailbox, nil, nil)
+ return UntaggedResponse.new(name, data, @str)
+ when T_ATOM
+ shift_token
+ match(T_SPACE)
+ token = match(T_NUMBER)
+ usage = token.value
+ match(T_SPACE)
+ token = match(T_NUMBER)
+ quota = token.value
+ match(T_RPAR)
+ data = MailboxQuota.new(mailbox, usage, quota)
+ return UntaggedResponse.new(name, data, @str)
+ else
+ parse_error("unexpected token %s", token.symbol)
+ end
+ end
+
+ def getquotaroot_response
+ # Similar to getquota, but only admin can use getquota.
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ mailbox = astring
+ quotaroots = []
+ while true
+ token = lookahead
+ break unless token.symbol == T_SPACE
+ shift_token
+ quotaroots.push(astring)
+ end
+ data = MailboxQuotaRoot.new(mailbox, quotaroots)
+ return UntaggedResponse.new(name, data, @str)
+ end
+
+ def getacl_response
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ mailbox = astring
+ data = []
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ while true
+ token = lookahead
+ case token.symbol
+ when T_CRLF
+ break
+ when T_SPACE
+ shift_token
+ end
+ user = astring
+ match(T_SPACE)
+ rights = astring
+ ##XXX data.push([user, rights])
+ data.push(MailboxACLItem.new(user, rights))
+ end
+ end
+ return UntaggedResponse.new(name, data, @str)
+ end
+
+ def search_response
+ token = match(T_ATOM)
+ name = token.value.upcase
+ token = lookahead
+ if token.symbol == T_SPACE
+ shift_token
+ data = []
+ while true
+ token = lookahead
+ case token.symbol
+ when T_CRLF
+ break
+ when T_SPACE
+ shift_token
+ end
+ data.push(number)
+ end
+ else
+ data = []
+ end
+ return UntaggedResponse.new(name, data, @str)
+ end
+
+ def thread_response
+ token = match(T_ATOM)
+ name = token.value.upcase
+ token = lookahead
+
+ if token.symbol == T_SPACE
+ threads = []
+
+ while true
+ shift_token
+ token = lookahead
+
+ case token.symbol
+ when T_LPAR
+ threads << thread_branch(token)
+ when T_CRLF
+ break
+ end
+ end
+ else
+ # no member
+ threads = []
+ end
+
+ return UntaggedResponse.new(name, threads, @str)
+ end
+
+ def thread_branch(token)
+ rootmember = nil
+ lastmember = nil
+
+ while true
+ shift_token # ignore first T_LPAR
+ token = lookahead
+
+ case token.symbol
+ when T_NUMBER
+ # new member
+ newmember = ThreadMember.new(number, [])
+ if rootmember.nil?
+ rootmember = newmember
+ else
+ lastmember.children << newmember
+ end
+ lastmember = newmember
+ when T_SPACE
+ # do nothing
+ when T_LPAR
+ if rootmember.nil?
+ # dummy member
+ lastmember = rootmember = ThreadMember.new(nil, [])
+ end
+
+ lastmember.children << thread_branch(token)
+ when T_RPAR
+ break
+ end
+ end
+
+ return rootmember
+ end
+
+ def status_response
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ mailbox = astring
+ match(T_SPACE)
+ match(T_LPAR)
+ attr = {}
+ while true
+ token = lookahead
+ case token.symbol
+ when T_RPAR
+ shift_token
+ break
+ when T_SPACE
+ shift_token
+ end
+ token = match(T_ATOM)
+ key = token.value.upcase
+ match(T_SPACE)
+ val = number
+ attr[key] = val
+ end
+ data = StatusData.new(mailbox, attr)
+ return UntaggedResponse.new(name, data, @str)
+ end
+
+ def capability_response
+ token = match(T_ATOM)
+ name = token.value.upcase
+ match(T_SPACE)
+ data = []
+ while true
+ token = lookahead
+ case token.symbol
+ when T_CRLF
+ break
+ when T_SPACE
+ shift_token
+ end
+ data.push(atom.upcase)
+ end
+ return UntaggedResponse.new(name, data, @str)
+ end
+
+ def resp_text
+ @lex_state = EXPR_RTEXT
+ token = lookahead
+ if token.symbol == T_LBRA
+ code = resp_text_code
+ else
+ code = nil
+ end
+ token = match(T_TEXT)
+ @lex_state = EXPR_BEG
+ return ResponseText.new(code, token.value)
+ end
+
+ def resp_text_code
+ @lex_state = EXPR_BEG
+ match(T_LBRA)
+ token = match(T_ATOM)
+ name = token.value.upcase
+ case name
+ when /\A(?:ALERT|PARSE|READ-ONLY|READ-WRITE|TRYCREATE|NOMODSEQ)\z/n
+ result = ResponseCode.new(name, nil)
+ when /\A(?:PERMANENTFLAGS)\z/n
+ match(T_SPACE)
+ result = ResponseCode.new(name, flag_list)
+ when /\A(?:UIDVALIDITY|UIDNEXT|UNSEEN)\z/n
+ match(T_SPACE)
+ result = ResponseCode.new(name, number)
+ else
+ match(T_SPACE)
+ @lex_state = EXPR_CTEXT
+ token = match(T_TEXT)
+ @lex_state = EXPR_BEG
+ result = ResponseCode.new(name, token.value)
+ end
+ match(T_RBRA)
+ @lex_state = EXPR_RTEXT
+ return result
+ end
+
+ def address_list
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ return nil
+ else
+ result = []
+ match(T_LPAR)
+ while true
+ token = lookahead
+ case token.symbol
+ when T_RPAR
+ shift_token
+ break
+ when T_SPACE
+ shift_token
+ end
+ result.push(address)
+ end
+ return result
+ end
+ end
+
+ ADDRESS_REGEXP = /\G\
+(?# 1: NAME )(?:NIL|"((?:[^\x80-\xff\x00\r\n"\\]|\\["\\])*)") \
+(?# 2: ROUTE )(?:NIL|"((?:[^\x80-\xff\x00\r\n"\\]|\\["\\])*)") \
+(?# 3: MAILBOX )(?:NIL|"((?:[^\x80-\xff\x00\r\n"\\]|\\["\\])*)") \
+(?# 4: HOST )(?:NIL|"((?:[^\x80-\xff\x00\r\n"\\]|\\["\\])*)")\
+\)/ni
+
+ def address
+ match(T_LPAR)
+ if @str.index(ADDRESS_REGEXP, @pos)
+ # address does not include literal.
+ @pos = $~.end(0)
+ name = $1
+ route = $2
+ mailbox = $3
+ host = $4
+ for s in [name, route, mailbox, host]
+ if s
+ s.gsub!(/\\(["\\])/n, "\\1")
+ end
+ end
+ else
+ name = nstring
+ match(T_SPACE)
+ route = nstring
+ match(T_SPACE)
+ mailbox = nstring
+ match(T_SPACE)
+ host = nstring
+ match(T_RPAR)
+ end
+ return Address.new(name, route, mailbox, host)
+ end
+
+# def flag_list
+# result = []
+# match(T_LPAR)
+# while true
+# token = lookahead
+# case token.symbol
+# when T_RPAR
+# shift_token
+# break
+# when T_SPACE
+# shift_token
+# end
+# result.push(flag)
+# end
+# return result
+# end
+
+# def flag
+# token = lookahead
+# if token.symbol == T_BSLASH
+# shift_token
+# token = lookahead
+# if token.symbol == T_STAR
+# shift_token
+# return token.value.intern
+# else
+# return atom.intern
+# end
+# else
+# return atom
+# end
+# end
+
+ FLAG_REGEXP = /\
+(?# FLAG )\\([^\x80-\xff(){ \x00-\x1f\x7f%"\\]+)|\
+(?# ATOM )([^\x80-\xff(){ \x00-\x1f\x7f%*"\\]+)/n
+
+ def flag_list
+ if @str.index(/\(([^)]*)\)/ni, @pos)
+ @pos = $~.end(0)
+ return $1.scan(FLAG_REGEXP).collect { |flag, atom|
+ atom || flag.capitalize.intern
+ }
+ else
+ parse_error("invalid flag list")
+ end
+ end
+
+ def nstring
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ return nil
+ else
+ return string
+ end
+ end
+
+ def astring
+ token = lookahead
+ if string_token?(token)
+ return string
+ else
+ return atom
+ end
+ end
+
+ def string
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ return nil
+ end
+ token = match(T_QUOTED, T_LITERAL)
+ return token.value
+ end
+
+ STRING_TOKENS = [T_QUOTED, T_LITERAL, T_NIL]
+
+ def string_token?(token)
+ return STRING_TOKENS.include?(token.symbol)
+ end
+
+ def case_insensitive_string
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ return nil
+ end
+ token = match(T_QUOTED, T_LITERAL)
+ return token.value.upcase
+ end
+
+ def atom
+ result = ""
+ while true
+ token = lookahead
+ if atom_token?(token)
+ result.concat(token.value)
+ shift_token
+ else
+ if result.empty?
+ parse_error("unexpected token %s", token.symbol)
+ else
+ return result
+ end
+ end
+ end
+ end
+
+ ATOM_TOKENS = [
+ T_ATOM,
+ T_NUMBER,
+ T_NIL,
+ T_LBRA,
+ T_RBRA,
+ T_PLUS
+ ]
+
+ def atom_token?(token)
+ return ATOM_TOKENS.include?(token.symbol)
+ end
+
+ def number
+ token = lookahead
+ if token.symbol == T_NIL
+ shift_token
+ return nil
+ end
+ token = match(T_NUMBER)
+ return token.value.to_i
+ end
+
+ def nil_atom
+ match(T_NIL)
+ return nil
+ end
+
+ def match(*args)
+ token = lookahead
+ unless args.include?(token.symbol)
+ parse_error('unexpected token %s (expected %s)',
+ token.symbol.id2name,
+ args.collect {|i| i.id2name}.join(" or "))
+ end
+ shift_token
+ return token
+ end
+
+ def lookahead
+ unless @token
+ @token = next_token
+ end
+ return @token
+ end
+
+ def shift_token
+ @token = nil
+ end
+
+ def next_token
+ case @lex_state
+ when EXPR_BEG
+ if @str.index(BEG_REGEXP, @pos)
+ @pos = $~.end(0)
+ if $1
+ return Token.new(T_SPACE, $+)
+ elsif $2
+ return Token.new(T_NIL, $+)
+ elsif $3
+ return Token.new(T_NUMBER, $+)
+ elsif $4
+ return Token.new(T_ATOM, $+)
+ elsif $5
+ return Token.new(T_QUOTED,
+ $+.gsub(/\\(["\\])/n, "\\1"))
+ elsif $6
+ return Token.new(T_LPAR, $+)
+ elsif $7
+ return Token.new(T_RPAR, $+)
+ elsif $8
+ return Token.new(T_BSLASH, $+)
+ elsif $9
+ return Token.new(T_STAR, $+)
+ elsif $10
+ return Token.new(T_LBRA, $+)
+ elsif $11
+ return Token.new(T_RBRA, $+)
+ elsif $12
+ len = $+.to_i
+ val = @str[@pos, len]
+ @pos += len
+ return Token.new(T_LITERAL, val)
+ elsif $13
+ return Token.new(T_PLUS, $+)
+ elsif $14
+ return Token.new(T_PERCENT, $+)
+ elsif $15
+ return Token.new(T_CRLF, $+)
+ elsif $16
+ return Token.new(T_EOF, $+)
+ else
+ parse_error("[Net::IMAP BUG] BEG_REGEXP is invalid")
+ end
+ else
+ @str.index(/\S*/n, @pos)
+ parse_error("unknown token - %s", $&.dump)
+ end
+ when EXPR_DATA
+ if @str.index(DATA_REGEXP, @pos)
+ @pos = $~.end(0)
+ if $1
+ return Token.new(T_SPACE, $+)
+ elsif $2
+ return Token.new(T_NIL, $+)
+ elsif $3
+ return Token.new(T_NUMBER, $+)
+ elsif $4
+ return Token.new(T_QUOTED,
+ $+.gsub(/\\(["\\])/n, "\\1"))
+ elsif $5
+ len = $+.to_i
+ val = @str[@pos, len]
+ @pos += len
+ return Token.new(T_LITERAL, val)
+ elsif $6
+ return Token.new(T_LPAR, $+)
+ elsif $7
+ return Token.new(T_RPAR, $+)
+ else
+ parse_error("[Net::IMAP BUG] DATA_REGEXP is invalid")
+ end
+ else
+ @str.index(/\S*/n, @pos)
+ parse_error("unknown token - %s", $&.dump)
+ end
+ when EXPR_TEXT
+ if @str.index(TEXT_REGEXP, @pos)
+ @pos = $~.end(0)
+ if $1
+ return Token.new(T_TEXT, $+)
+ else
+ parse_error("[Net::IMAP BUG] TEXT_REGEXP is invalid")
+ end
+ else
+ @str.index(/\S*/n, @pos)
+ parse_error("unknown token - %s", $&.dump)
+ end
+ when EXPR_RTEXT
+ if @str.index(RTEXT_REGEXP, @pos)
+ @pos = $~.end(0)
+ if $1
+ return Token.new(T_LBRA, $+)
+ elsif $2
+ return Token.new(T_TEXT, $+)
+ else
+ parse_error("[Net::IMAP BUG] RTEXT_REGEXP is invalid")
+ end
+ else
+ @str.index(/\S*/n, @pos)
+ parse_error("unknown token - %s", $&.dump)
+ end
+ when EXPR_CTEXT
+ if @str.index(CTEXT_REGEXP, @pos)
+ @pos = $~.end(0)
+ if $1
+ return Token.new(T_TEXT, $+)
+ else
+ parse_error("[Net::IMAP BUG] CTEXT_REGEXP is invalid")
+ end
+ else
+ @str.index(/\S*/n, @pos) #/
+ parse_error("unknown token - %s", $&.dump)
+ end
+ else
+ parse_error("invalid @lex_state - %s", @lex_state.inspect)
+ end
+ end
+
+ def parse_error(fmt, *args)
+ if IMAP.debug
+ $stderr.printf("@str: %s\n", @str.dump)
+ $stderr.printf("@pos: %d\n", @pos)
+ $stderr.printf("@lex_state: %s\n", @lex_state)
+ if @token
+ $stderr.printf("@token.symbol: %s\n", @token.symbol)
+ $stderr.printf("@token.value: %s\n", @token.value.inspect)
+ end
+ end
+ raise ResponseParseError, format(fmt, *args)
+ end
+ end
+
+ # Authenticator for the "LOGIN" authentication type. See
+ # #authenticate().
+ class LoginAuthenticator
+ def process(data)
+ case @state
+ when STATE_USER
+ @state = STATE_PASSWORD
+ return @user
+ when STATE_PASSWORD
+ return @password
+ end
+ end
+
+ private
+
+ STATE_USER = :USER
+ STATE_PASSWORD = :PASSWORD
+
+ def initialize(user, password)
+ @user = user
+ @password = password
+ @state = STATE_USER
+ end
+ end
+ add_authenticator "LOGIN", LoginAuthenticator
+
+ # Authenticator for the "PLAIN" authentication type. See
+ # #authenticate().
+ class PlainAuthenticator
+ def process(data)
+ return "\0#{@user}\0#{@password}"
+ end
+
+ private
+
+ def initialize(user, password)
+ @user = user
+ @password = password
+ end
+ end
+ add_authenticator "PLAIN", PlainAuthenticator
+
+ # Authenticator for the "CRAM-MD5" authentication type. See
+ # #authenticate().
+ class CramMD5Authenticator
+ def process(challenge)
+ digest = hmac_md5(challenge, @password)
+ return @user + " " + digest
+ end
+
+ private
+
+ def initialize(user, password)
+ @user = user
+ @password = password
+ end
+
+ def hmac_md5(text, key)
+ if key.length > 64
+ key = Digest::MD5.digest(key)
+ end
+
+ k_ipad = key + "\0" * (64 - key.length)
+ k_opad = key + "\0" * (64 - key.length)
+ for i in 0..63
+ k_ipad[i] = (k_ipad[i].ord ^ 0x36).chr
+ k_opad[i] = (k_opad[i].ord ^ 0x5c).chr
+ end
+
+ digest = Digest::MD5.digest(k_ipad + text)
+
+ return Digest::MD5.hexdigest(k_opad + digest)
+ end
+ end
+ add_authenticator "CRAM-MD5", CramMD5Authenticator
+
+ # Authenticator for the "DIGEST-MD5" authentication type. See
+ # #authenticate().
+ class DigestMD5Authenticator
+ def process(challenge)
+ case @stage
+ when STAGE_ONE
+ @stage = STAGE_TWO
+ sparams = {}
+ c = StringScanner.new(challenge)
+ while c.scan(/(?:\s*,)?\s*(\w+)=("(?:[^\\"]+|\\.)*"|[^,]+)\s*/)
+ k, v = c[1], c[2]
+ if v =~ /^"(.*)"$/
+ v = $1
+ if v =~ /,/
+ v = v.split(',')
+ end
+ end
+ sparams[k] = v
+ end
+
+ raise DataFormatError, "Bad Challenge: '#{challenge}'" unless c.rest.size == 0
+ raise Error, "Server does not support auth (qop = #{sparams['qop'].join(',')})" unless sparams['qop'].include?("auth")
+
+ response = {
+ :nonce => sparams['nonce'],
+ :username => @user,
+ :realm => sparams['realm'],
+ :cnonce => Digest::MD5.hexdigest("%.15f:%.15f:%d" % [Time.now.to_f, rand, Process.pid.to_s]),
+ :'digest-uri' => 'imap/' + sparams['realm'],
+ :qop => 'auth',
+ :maxbuf => 65535,
+ :nc => "%08d" % nc(sparams['nonce']),
+ :charset => sparams['charset'],
+ }
+
+ response[:authzid] = @authname unless @authname.nil?
+
+ # now, the real thing
+ a0 = Digest::MD5.digest( [ response.values_at(:username, :realm), @password ].join(':') )
+
+ a1 = [ a0, response.values_at(:nonce,:cnonce) ].join(':')
+ a1 << ':' + response[:authzid] unless response[:authzid].nil?
+
+ a2 = "AUTHENTICATE:" + response[:'digest-uri']
+ a2 << ":00000000000000000000000000000000" if response[:qop] and response[:qop] =~ /^auth-(?:conf|int)$/
+
+ response[:response] = Digest::MD5.hexdigest(
+ [
+ Digest::MD5.hexdigest(a1),
+ response.values_at(:nonce, :nc, :cnonce, :qop),
+ Digest::MD5.hexdigest(a2)
+ ].join(':')
+ )
+
+ return response.keys.map {|key| qdval(key.to_s, response[key]) }.join(',')
+ when STAGE_TWO
+ @stage = nil
+ # if at the second stage, return an empty string
+ if challenge =~ /rspauth=/
+ return ''
+ else
+ raise ResponseParseError, challenge
+ end
+ else
+ raise ResponseParseError, challenge
+ end
+ end
+
+ def initialize(user, password, authname = nil)
+ @user, @password, @authname = user, password, authname
+ @nc, @stage = {}, STAGE_ONE
+ end
+
+ private
+
+ STAGE_ONE = :stage_one
+ STAGE_TWO = :stage_two
+
+ def nc(nonce)
+ if @nc.has_key? nonce
+ @nc[nonce] = @nc[nonce] + 1
+ else
+ @nc[nonce] = 1
+ end
+ return @nc[nonce]
+ end
+
+ # some responses need quoting
+ def qdval(k, v)
+ return if k.nil? or v.nil?
+ if %w"username authzid realm nonce cnonce digest-uri qop".include? k
+ v.gsub!(/([\\"])/, "\\\1")
+ return '%s="%s"' % [k, v]
+ else
+ return '%s=%s' % [k, v]
+ end
+ end
+ end
+ add_authenticator "DIGEST-MD5", DigestMD5Authenticator
+
+ # Superclass of IMAP errors.
+ class Error < StandardError
+ end
+
+ # Error raised when data is in the incorrect format.
+ class DataFormatError < Error
+ end
+
+ # Error raised when a response from the server is non-parseable.
+ class ResponseParseError < Error
+ end
+
+ # Superclass of all errors used to encapsulate "fail" responses
+ # from the server.
+ class ResponseError < Error
+ end
+
+ # Error raised upon a "NO" response from the server, indicating
+ # that the client command could not be completed successfully.
+ class NoResponseError < ResponseError
+ end
+
+ # Error raised upon a "BAD" response from the server, indicating
+ # that the client command violated the IMAP protocol, or an internal
+ # server failure has occurred.
+ class BadResponseError < ResponseError
+ end
+
+ # Error raised upon a "BYE" response from the server, indicating
+ # that the client is not being allowed to login, or has been timed
+ # out due to inactivity.
+ class ByeResponseError < ResponseError
+ end
+ end
+end
+
+if __FILE__ == $0
+ # :enddoc:
+ require "getoptlong"
+
+ $stdout.sync = true
+ $port = nil
+ $user = ENV["USER"] || ENV["LOGNAME"]
+ $auth = "login"
+ $ssl = false
+
+ def usage
+ $stderr.print <<EOF
+usage: #{$0} [options] <host>
+
+ --help print this message
+ --port=PORT specifies port
+ --user=USER specifies user
+ --auth=AUTH specifies auth type
+ --ssl use ssl
+EOF
+ end
+
+ def get_password
+ print "password: "
+ system("stty", "-echo")
+ begin
+ return gets.chop
+ ensure
+ system("stty", "echo")
+ print "\n"
+ end
+ end
+
+ def get_command
+ printf("%s@%s> ", $user, $host)
+ if line = gets
+ return line.strip.split(/\s+/)
+ else
+ return nil
+ end
+ end
+
+ parser = GetoptLong.new
+ parser.set_options(['--debug', GetoptLong::NO_ARGUMENT],
+ ['--help', GetoptLong::NO_ARGUMENT],
+ ['--port', GetoptLong::REQUIRED_ARGUMENT],
+ ['--user', GetoptLong::REQUIRED_ARGUMENT],
+ ['--auth', GetoptLong::REQUIRED_ARGUMENT],
+ ['--ssl', GetoptLong::NO_ARGUMENT])
+ begin
+ parser.each_option do |name, arg|
+ case name
+ when "--port"
+ $port = arg
+ when "--user"
+ $user = arg
+ when "--auth"
+ $auth = arg
+ when "--ssl"
+ $ssl = true
+ when "--debug"
+ Net::IMAP.debug = true
+ when "--help"
+ usage
+ exit(1)
+ end
+ end
+ rescue
+ usage
+ exit(1)
+ end
+
+ $host = ARGV.shift
+ unless $host
+ usage
+ exit(1)
+ end
+
+ imap = Net::IMAP.new($host, :port => $port, :ssl => $ssl)
+ begin
+ password = get_password
+ imap.authenticate($auth, $user, password)
+ while true
+ cmd, *args = get_command
+ break unless cmd
+ begin
+ case cmd
+ when "list"
+ for mbox in imap.list("", args[0] || "*")
+ if mbox.attr.include?(Net::IMAP::NOSELECT)
+ prefix = "!"
+ elsif mbox.attr.include?(Net::IMAP::MARKED)
+ prefix = "*"
+ else
+ prefix = " "
+ end
+ print prefix, mbox.name, "\n"
+ end
+ when "select"
+ imap.select(args[0] || "inbox")
+ print "ok\n"
+ when "close"
+ imap.close
+ print "ok\n"
+ when "summary"
+ unless messages = imap.responses["EXISTS"][-1]
+ puts "not selected"
+ next
+ end
+ if messages > 0
+ for data in imap.fetch(1..-1, ["ENVELOPE"])
+ print data.seqno, ": ", data.attr["ENVELOPE"].subject, "\n"
+ end
+ else
+ puts "no message"
+ end
+ when "fetch"
+ if args[0]
+ data = imap.fetch(args[0].to_i, ["RFC822.HEADER", "RFC822.TEXT"])[0]
+ puts data.attr["RFC822.HEADER"]
+ puts data.attr["RFC822.TEXT"]
+ else
+ puts "missing argument"
+ end
+ when "logout", "exit", "quit"
+ break
+ when "help", "?"
+ print <<EOF
+list [pattern] list mailboxes
+select [mailbox] select mailbox
+close close mailbox
+summary display summary
+fetch [msgno] display message
+logout logout
+help, ? display help message
+EOF
+ else
+ print "unknown command: ", cmd, "\n"
+ end
+ rescue Net::IMAP::Error
+ puts $!
+ end
+ end
+ ensure
+ imap.logout
+ imap.disconnect
+ end
+end
+
diff --git a/ruby/lib/net/pop.rb b/ruby/lib/net/pop.rb
new file mode 100644
index 0000000..d3505e7
--- /dev/null
+++ b/ruby/lib/net/pop.rb
@@ -0,0 +1,1000 @@
+# = net/pop.rb
+#
+# Copyright (c) 1999-2007 Yukihiro Matsumoto.
+#
+# Copyright (c) 1999-2007 Minero Aoki.
+#
+# Written & maintained by Minero Aoki <aamine@loveruby.net>.
+#
+# Documented by William Webber and Minero Aoki.
+#
+# This program is free software. You can re-distribute and/or
+# modify this program under the same terms as Ruby itself,
+# Ruby Distribute License.
+#
+# NOTE: You can find Japanese version of this document at:
+# http://www.ruby-lang.org/ja/man/html/net_pop.html
+#
+# $Id: pop.rb 19776 2008-10-14 02:22:46Z kazu $
+#
+# See Net::POP3 for documentation.
+#
+
+require 'net/protocol'
+require 'digest/md5'
+require 'timeout'
+
+begin
+ require "openssl/ssl"
+rescue LoadError
+end
+
+module Net
+
+ # Non-authentication POP3 protocol error
+ # (reply code "-ERR", except authentication).
+ class POPError < ProtocolError; end
+
+ # POP3 authentication error.
+ class POPAuthenticationError < ProtoAuthError; end
+
+ # Unexpected response from the server.
+ class POPBadResponse < POPError; end
+
+ #
+ # = Net::POP3
+ #
+ # == What is This Library?
+ #
+ # This library provides functionality for retrieving
+ # email via POP3, the Post Office Protocol version 3. For details
+ # of POP3, see [RFC1939] (http://www.ietf.org/rfc/rfc1939.txt).
+ #
+ # == Examples
+ #
+ # === Retrieving Messages
+ #
+ # This example retrieves messages from the server and deletes them
+ # on the server.
+ #
+ # Messages are written to files named 'inbox/1', 'inbox/2', ....
+ # Replace 'pop.example.com' with your POP3 server address, and
+ # 'YourAccount' and 'YourPassword' with the appropriate account
+ # details.
+ #
+ # require 'net/pop'
+ #
+ # pop = Net::POP3.new('pop.example.com')
+ # pop.start('YourAccount', 'YourPassword') # (1)
+ # if pop.mails.empty?
+ # puts 'No mail.'
+ # else
+ # i = 0
+ # pop.each_mail do |m| # or "pop.mails.each ..." # (2)
+ # File.open("inbox/#{i}", 'w') do |f|
+ # f.write m.pop
+ # end
+ # m.delete
+ # i += 1
+ # end
+ # puts "#{pop.mails.size} mails popped."
+ # end
+ # pop.finish # (3)
+ #
+ # 1. Call Net::POP3#start and start POP session.
+ # 2. Access messages by using POP3#each_mail and/or POP3#mails.
+ # 3. Close POP session by calling POP3#finish or use the block form of #start.
+ #
+ # === Shortened Code
+ #
+ # The example above is very verbose. You can shorten the code by using
+ # some utility methods. First, the block form of Net::POP3.start can
+ # be used instead of POP3.new, POP3#start and POP3#finish.
+ #
+ # require 'net/pop'
+ #
+ # Net::POP3.start('pop.example.com', 110,
+ # 'YourAccount', 'YourPassword') do |pop|
+ # if pop.mails.empty?
+ # puts 'No mail.'
+ # else
+ # i = 0
+ # pop.each_mail do |m| # or "pop.mails.each ..."
+ # File.open("inbox/#{i}", 'w') do |f|
+ # f.write m.pop
+ # end
+ # m.delete
+ # i += 1
+ # end
+ # puts "#{pop.mails.size} mails popped."
+ # end
+ # end
+ #
+ # POP3#delete_all is an alternative for #each_mail and #delete.
+ #
+ # require 'net/pop'
+ #
+ # Net::POP3.start('pop.example.com', 110,
+ # 'YourAccount', 'YourPassword') do |pop|
+ # if pop.mails.empty?
+ # puts 'No mail.'
+ # else
+ # i = 1
+ # pop.delete_all do |m|
+ # File.open("inbox/#{i}", 'w') do |f|
+ # f.write m.pop
+ # end
+ # i += 1
+ # end
+ # end
+ # end
+ #
+ # And here is an even shorter example.
+ #
+ # require 'net/pop'
+ #
+ # i = 0
+ # Net::POP3.delete_all('pop.example.com', 110,
+ # 'YourAccount', 'YourPassword') do |m|
+ # File.open("inbox/#{i}", 'w') do |f|
+ # f.write m.pop
+ # end
+ # i += 1
+ # end
+ #
+ # === Memory Space Issues
+ #
+ # All the examples above get each message as one big string.
+ # This example avoids this.
+ #
+ # require 'net/pop'
+ #
+ # i = 1
+ # Net::POP3.delete_all('pop.example.com', 110,
+ # 'YourAccount', 'YourPassword') do |m|
+ # File.open("inbox/#{i}", 'w') do |f|
+ # m.pop do |chunk| # get a message little by little.
+ # f.write chunk
+ # end
+ # i += 1
+ # end
+ # end
+ #
+ # === Using APOP
+ #
+ # The net/pop library supports APOP authentication.
+ # To use APOP, use the Net::APOP class instead of the Net::POP3 class.
+ # You can use the utility method, Net::POP3.APOP(). For example:
+ #
+ # require 'net/pop'
+ #
+ # # Use APOP authentication if $isapop == true
+ # pop = Net::POP3.APOP($is_apop).new('apop.example.com', 110)
+ # pop.start(YourAccount', 'YourPassword') do |pop|
+ # # Rest of the code is the same.
+ # end
+ #
+ # === Fetch Only Selected Mail Using 'UIDL' POP Command
+ #
+ # If your POP server provides UIDL functionality,
+ # you can grab only selected mails from the POP server.
+ # e.g.
+ #
+ # def need_pop?( id )
+ # # determine if we need pop this mail...
+ # end
+ #
+ # Net::POP3.start('pop.example.com', 110,
+ # 'Your account', 'Your password') do |pop|
+ # pop.mails.select { |m| need_pop?(m.unique_id) }.each do |m|
+ # do_something(m.pop)
+ # end
+ # end
+ #
+ # The POPMail#unique_id() method returns the unique-id of the message as a
+ # String. Normally the unique-id is a hash of the message.
+ #
+ class POP3 < Protocol
+
+ Revision = %q$Revision: 19776 $.split[1]
+
+ #
+ # Class Parameters
+ #
+
+ def POP3.default_port
+ default_pop3_port()
+ end
+
+ # The default port for POP3 connections, port 110
+ def POP3.default_pop3_port
+ 110
+ end
+
+ # The default port for POP3S connections, port 995
+ def POP3.default_pop3s_port
+ 995
+ end
+
+ def POP3.socket_type #:nodoc: obsolete
+ Net::InternetMessageIO
+ end
+
+ #
+ # Utilities
+ #
+
+ # Returns the APOP class if +isapop+ is true; otherwise, returns
+ # the POP class. For example:
+ #
+ # # Example 1
+ # pop = Net::POP3::APOP($is_apop).new(addr, port)
+ #
+ # # Example 2
+ # Net::POP3::APOP($is_apop).start(addr, port) do |pop|
+ # ....
+ # end
+ #
+ def POP3.APOP(isapop)
+ isapop ? APOP : POP3
+ end
+
+ # Starts a POP3 session and iterates over each POPMail object,
+ # yielding it to the +block+.
+ # This method is equivalent to:
+ #
+ # Net::POP3.start(address, port, account, password) do |pop|
+ # pop.each_mail do |m|
+ # yield m
+ # end
+ # end
+ #
+ # This method raises a POPAuthenticationError if authentication fails.
+ #
+ # === Example
+ #
+ # Net::POP3.foreach('pop.example.com', 110,
+ # 'YourAccount', 'YourPassword') do |m|
+ # file.write m.pop
+ # m.delete if $DELETE
+ # end
+ #
+ def POP3.foreach(address, port = nil,
+ account = nil, password = nil,
+ isapop = false, &block) # :yields: message
+ start(address, port, account, password, isapop) {|pop|
+ pop.each_mail(&block)
+ }
+ end
+
+ # Starts a POP3 session and deletes all messages on the server.
+ # If a block is given, each POPMail object is yielded to it before
+ # being deleted.
+ #
+ # This method raises a POPAuthenticationError if authentication fails.
+ #
+ # === Example
+ #
+ # Net::POP3.delete_all('pop.example.com', 110,
+ # 'YourAccount', 'YourPassword') do |m|
+ # file.write m.pop
+ # end
+ #
+ def POP3.delete_all(address, port = nil,
+ account = nil, password = nil,
+ isapop = false, &block)
+ start(address, port, account, password, isapop) {|pop|
+ pop.delete_all(&block)
+ }
+ end
+
+ # Opens a POP3 session, attempts authentication, and quits.
+ #
+ # This method raises POPAuthenticationError if authentication fails.
+ #
+ # === Example: normal POP3
+ #
+ # Net::POP3.auth_only('pop.example.com', 110,
+ # 'YourAccount', 'YourPassword')
+ #
+ # === Example: APOP
+ #
+ # Net::POP3.auth_only('pop.example.com', 110,
+ # 'YourAccount', 'YourPassword', true)
+ #
+ def POP3.auth_only(address, port = nil,
+ account = nil, password = nil,
+ isapop = false)
+ new(address, port, isapop).auth_only account, password
+ end
+
+ # Starts a pop3 session, attempts authentication, and quits.
+ # This method must not be called while POP3 session is opened.
+ # This method raises POPAuthenticationError if authentication fails.
+ def auth_only(account, password)
+ raise IOError, 'opening previously opened POP session' if started?
+ start(account, password) {
+ ;
+ }
+ end
+
+ #
+ # SSL
+ #
+
+ @ssl_params = nil
+
+ # call-seq:
+ # Net::POP.enable_ssl(params = {})
+ #
+ # Enable SSL for all new instances.
+ # +params+ is passed to OpenSSL::SSLContext#set_params.
+ def POP3.enable_ssl(*args)
+ @ssl_params = create_ssl_params(*args)
+ end
+
+ def POP3.create_ssl_params(verify_or_params = {}, certs = nil)
+ begin
+ params = verify_or_params.to_hash
+ rescue NoMethodError
+ params = {}
+ params[:verify_mode] = verify_or_params
+ if certs
+ if File.file?(certs)
+ params[:ca_file] = certs
+ elsif File.directory?(certs)
+ params[:ca_path] = certs
+ end
+ end
+ end
+ return params
+ end
+
+ # Disable SSL for all new instances.
+ def POP3.disable_ssl
+ @ssl_params = nil
+ end
+
+ def POP3.ssl_params
+ return @ssl_params
+ end
+
+ def POP3.use_ssl?
+ return !@ssl_params.nil?
+ end
+
+ def POP3.verify
+ return @ssl_params[:verify_mode]
+ end
+
+ def POP3.certs
+ return @ssl_params[:ca_file] || @ssl_params[:ca_path]
+ end
+
+ #
+ # Session management
+ #
+
+ # Creates a new POP3 object and open the connection. Equivalent to
+ #
+ # Net::POP3.new(address, port, isapop).start(account, password)
+ #
+ # If +block+ is provided, yields the newly-opened POP3 object to it,
+ # and automatically closes it at the end of the session.
+ #
+ # === Example
+ #
+ # Net::POP3.start(addr, port, account, password) do |pop|
+ # pop.each_mail do |m|
+ # file.write m.pop
+ # m.delete
+ # end
+ # end
+ #
+ def POP3.start(address, port = nil,
+ account = nil, password = nil,
+ isapop = false, &block) # :yield: pop
+ new(address, port, isapop).start(account, password, &block)
+ end
+
+ # Creates a new POP3 object.
+ #
+ # +address+ is the hostname or ip address of your POP3 server.
+ #
+ # The optional +port+ is the port to connect to.
+ #
+ # The optional +isapop+ specifies whether this connection is going
+ # to use APOP authentication; it defaults to +false+.
+ #
+ # This method does *not* open the TCP connection.
+ def initialize(addr, port = nil, isapop = false)
+ @address = addr
+ @ssl_params = POP3.ssl_params
+ @port = port
+ @apop = isapop
+
+ @command = nil
+ @socket = nil
+ @started = false
+ @open_timeout = 30
+ @read_timeout = 60
+ @debug_output = nil
+
+ @mails = nil
+ @n_mails = nil
+ @n_bytes = nil
+ end
+
+ # Does this instance use APOP authentication?
+ def apop?
+ @apop
+ end
+
+ # does this instance use SSL?
+ def use_ssl?
+ return !@ssl_params.nil?
+ end
+
+ # call-seq:
+ # Net::POP#enable_ssl(params = {})
+ #
+ # Enables SSL for this instance. Must be called before the connection is
+ # established to have any effect.
+ # +params[:port]+ is port to establish the SSL connection on; Defaults to 995.
+ # +params+ (except :port) is passed to OpenSSL::SSLContext#set_params.
+ def enable_ssl(verify_or_params = {}, certs = nil, port = nil)
+ begin
+ @ssl_params = verify_or_params.to_hash.dup
+ @port = @ssl_params.delete(:port) || @port
+ rescue NoMethodError
+ @ssl_params = POP3.create_ssl_params(verify_or_params, certs)
+ @port = port || @port
+ end
+ end
+
+ def disable_ssl
+ @ssl_params = nil
+ end
+
+ # Provide human-readable stringification of class state.
+ def inspect
+ "#<#{self.class} #{@address}:#{@port} open=#{@started}>"
+ end
+
+ # *WARNING*: This method causes a serious security hole.
+ # Use this method only for debugging.
+ #
+ # Set an output stream for debugging.
+ #
+ # === Example
+ #
+ # pop = Net::POP.new(addr, port)
+ # pop.set_debug_output $stderr
+ # pop.start(account, passwd) do |pop|
+ # ....
+ # end
+ #
+ def set_debug_output(arg)
+ @debug_output = arg
+ end
+
+ # The address to connect to.
+ attr_reader :address
+
+ # The port number to connect to.
+ def port
+ return @port || (use_ssl? ? POP3.default_pop3s_port : POP3.default_pop3_port)
+ end
+
+ # Seconds to wait until a connection is opened.
+ # If the POP3 object cannot open a connection within this time,
+ # it raises a TimeoutError exception.
+ attr_accessor :open_timeout
+
+ # Seconds to wait until reading one block (by one read(1) call).
+ # If the POP3 object cannot complete a read() within this time,
+ # it raises a TimeoutError exception.
+ attr_reader :read_timeout
+
+ # Set the read timeout.
+ def read_timeout=(sec)
+ @command.socket.read_timeout = sec if @command
+ @read_timeout = sec
+ end
+
+ # +true+ if the POP3 session has started.
+ def started?
+ @started
+ end
+
+ alias active? started? #:nodoc: obsolete
+
+ # Starts a POP3 session.
+ #
+ # When called with block, gives a POP3 object to the block and
+ # closes the session after block call finishes.
+ #
+ # This method raises a POPAuthenticationError if authentication fails.
+ def start(account, password) # :yield: pop
+ raise IOError, 'POP session already started' if @started
+ if block_given?
+ begin
+ do_start account, password
+ return yield(self)
+ ensure
+ do_finish
+ end
+ else
+ do_start account, password
+ return self
+ end
+ end
+
+ def do_start(account, password)
+ s = timeout(@open_timeout) { TCPSocket.open(@address, port) }
+ if use_ssl?
+ raise 'openssl library not installed' unless defined?(OpenSSL)
+ context = OpenSSL::SSL::SSLContext.new
+ context.set_params(@ssl_params)
+ s = OpenSSL::SSL::SSLSocket.new(s, context)
+ s.sync_close = true
+ s.connect
+ if context.verify_mode != OpenSSL::SSL::VERIFY_NONE
+ s.post_connection_check(@address)
+ end
+ end
+ @socket = InternetMessageIO.new(s)
+ logging "POP session started: #{@address}:#{@port} (#{@apop ? 'APOP' : 'POP'})"
+ @socket.read_timeout = @read_timeout
+ @socket.debug_output = @debug_output
+ on_connect
+ @command = POP3Command.new(@socket)
+ if apop?
+ @command.apop account, password
+ else
+ @command.auth account, password
+ end
+ @started = true
+ ensure
+ # Authentication failed, clean up connection.
+ unless @started
+ s.close if s and not s.closed?
+ @socket = nil
+ @command = nil
+ end
+ end
+ private :do_start
+
+ def on_connect
+ end
+ private :on_connect
+
+ # Finishes a POP3 session and closes TCP connection.
+ def finish
+ raise IOError, 'POP session not yet started' unless started?
+ do_finish
+ end
+
+ def do_finish
+ @mails = nil
+ @n_mails = nil
+ @n_bytes = nil
+ @command.quit if @command
+ ensure
+ @started = false
+ @command = nil
+ @socket.close if @socket and not @socket.closed?
+ @socket = nil
+ end
+ private :do_finish
+
+ def command
+ raise IOError, 'POP session not opened yet' \
+ if not @socket or @socket.closed?
+ @command
+ end
+ private :command
+
+ #
+ # POP protocol wrapper
+ #
+
+ # Returns the number of messages on the POP server.
+ def n_mails
+ return @n_mails if @n_mails
+ @n_mails, @n_bytes = command().stat
+ @n_mails
+ end
+
+ # Returns the total size in bytes of all the messages on the POP server.
+ def n_bytes
+ return @n_bytes if @n_bytes
+ @n_mails, @n_bytes = command().stat
+ @n_bytes
+ end
+
+ # Returns an array of Net::POPMail objects, representing all the
+ # messages on the server. This array is renewed when the session
+ # restarts; otherwise, it is fetched from the server the first time
+ # this method is called (directly or indirectly) and cached.
+ #
+ # This method raises a POPError if an error occurs.
+ def mails
+ return @mails.dup if @mails
+ if n_mails() == 0
+ # some popd raises error for LIST on the empty mailbox.
+ @mails = []
+ return []
+ end
+
+ @mails = command().list.map {|num, size|
+ POPMail.new(num, size, self, command())
+ }
+ @mails.dup
+ end
+
+ # Yields each message to the passed-in block in turn.
+ # Equivalent to:
+ #
+ # pop3.mails.each do |popmail|
+ # ....
+ # end
+ #
+ # This method raises a POPError if an error occurs.
+ def each_mail(&block) # :yield: message
+ mails().each(&block)
+ end
+
+ alias each each_mail
+
+ # Deletes all messages on the server.
+ #
+ # If called with a block, yields each message in turn before deleting it.
+ #
+ # === Example
+ #
+ # n = 1
+ # pop.delete_all do |m|
+ # File.open("inbox/#{n}") do |f|
+ # f.write m.pop
+ # end
+ # n += 1
+ # end
+ #
+ # This method raises a POPError if an error occurs.
+ #
+ def delete_all # :yield: message
+ mails().each do |m|
+ yield m if block_given?
+ m.delete unless m.deleted?
+ end
+ end
+
+ # Resets the session. This clears all "deleted" marks from messages.
+ #
+ # This method raises a POPError if an error occurs.
+ def reset
+ command().rset
+ mails().each do |m|
+ m.instance_eval {
+ @deleted = false
+ }
+ end
+ end
+
+ def set_all_uids #:nodoc: internal use only (called from POPMail#uidl)
+ uidl = command().uidl
+ @mails.each {|m| m.uid = uidl[m.number] }
+ end
+
+ def logging(msg)
+ @debug_output << msg + "\n" if @debug_output
+ end
+
+ end # class POP3
+
+ # class aliases
+ POP = POP3
+ POPSession = POP3
+ POP3Session = POP3
+
+ #
+ # This class is equivalent to POP3, except that it uses APOP authentication.
+ #
+ class APOP < POP3
+ # Always returns true.
+ def apop?
+ true
+ end
+ end
+
+ # class aliases
+ APOPSession = APOP
+
+ #
+ # This class represents a message which exists on the POP server.
+ # Instances of this class are created by the POP3 class; they should
+ # not be directly created by the user.
+ #
+ class POPMail
+
+ def initialize(num, len, pop, cmd) #:nodoc:
+ @number = num
+ @length = len
+ @pop = pop
+ @command = cmd
+ @deleted = false
+ @uid = nil
+ end
+
+ # The sequence number of the message on the server.
+ attr_reader :number
+
+ # The length of the message in octets.
+ attr_reader :length
+ alias size length
+
+ # Provide human-readable stringification of class state.
+ def inspect
+ "#<#{self.class} #{@number}#{@deleted ? ' deleted' : ''}>"
+ end
+
+ #
+ # This method fetches the message. If called with a block, the
+ # message is yielded to the block one chunk at a time. If called
+ # without a block, the message is returned as a String. The optional
+ # +dest+ argument will be prepended to the returned String; this
+ # argument is essentially obsolete.
+ #
+ # === Example without block
+ #
+ # POP3.start('pop.example.com', 110,
+ # 'YourAccount, 'YourPassword') do |pop|
+ # n = 1
+ # pop.mails.each do |popmail|
+ # File.open("inbox/#{n}", 'w') do |f|
+ # f.write popmail.pop
+ # end
+ # popmail.delete
+ # n += 1
+ # end
+ # end
+ #
+ # === Example with block
+ #
+ # POP3.start('pop.example.com', 110,
+ # 'YourAccount, 'YourPassword') do |pop|
+ # n = 1
+ # pop.mails.each do |popmail|
+ # File.open("inbox/#{n}", 'w') do |f|
+ # popmail.pop do |chunk| ####
+ # f.write chunk
+ # end
+ # end
+ # n += 1
+ # end
+ # end
+ #
+ # This method raises a POPError if an error occurs.
+ #
+ def pop( dest = '', &block ) # :yield: message_chunk
+ if block_given?
+ @command.retr(@number, &block)
+ nil
+ else
+ @command.retr(@number) do |chunk|
+ dest << chunk
+ end
+ dest
+ end
+ end
+
+ alias all pop #:nodoc: obsolete
+ alias mail pop #:nodoc: obsolete
+
+ # Fetches the message header and +lines+ lines of body.
+ #
+ # The optional +dest+ argument is obsolete.
+ #
+ # This method raises a POPError if an error occurs.
+ def top(lines, dest = '')
+ @command.top(@number, lines) do |chunk|
+ dest << chunk
+ end
+ dest
+ end
+
+ # Fetches the message header.
+ #
+ # The optional +dest+ argument is obsolete.
+ #
+ # This method raises a POPError if an error occurs.
+ def header(dest = '')
+ top(0, dest)
+ end
+
+ # Marks a message for deletion on the server. Deletion does not
+ # actually occur until the end of the session; deletion may be
+ # cancelled for _all_ marked messages by calling POP3#reset().
+ #
+ # This method raises a POPError if an error occurs.
+ #
+ # === Example
+ #
+ # POP3.start('pop.example.com', 110,
+ # 'YourAccount, 'YourPassword') do |pop|
+ # n = 1
+ # pop.mails.each do |popmail|
+ # File.open("inbox/#{n}", 'w') do |f|
+ # f.write popmail.pop
+ # end
+ # popmail.delete ####
+ # n += 1
+ # end
+ # end
+ #
+ def delete
+ @command.dele @number
+ @deleted = true
+ end
+
+ alias delete! delete #:nodoc: obsolete
+
+ # True if the mail has been deleted.
+ def deleted?
+ @deleted
+ end
+
+ # Returns the unique-id of the message.
+ # Normally the unique-id is a hash string of the message.
+ #
+ # This method raises a POPError if an error occurs.
+ def unique_id
+ return @uid if @uid
+ @pop.set_all_uids
+ @uid
+ end
+
+ alias uidl unique_id
+
+ def uid=(uid) #:nodoc: internal use only
+ @uid = uid
+ end
+
+ end # class POPMail
+
+
+ class POP3Command #:nodoc: internal use only
+
+ def initialize(sock)
+ @socket = sock
+ @error_occured = false
+ res = check_response(critical { recv_response() })
+ @apop_stamp = res.slice(/<[!-~]+@[!-~]+>/)
+ end
+
+ attr_reader :socket
+
+ def inspect
+ "#<#{self.class} socket=#{@socket}>"
+ end
+
+ def auth(account, password)
+ check_response_auth(critical {
+ check_response_auth(get_response('USER %s', account))
+ get_response('PASS %s', password)
+ })
+ end
+
+ def apop(account, password)
+ raise POPAuthenticationError, 'not APOP server; cannot login' \
+ unless @apop_stamp
+ check_response_auth(critical {
+ get_response('APOP %s %s',
+ account,
+ Digest::MD5.hexdigest(@apop_stamp + password))
+ })
+ end
+
+ def list
+ critical {
+ getok 'LIST'
+ list = []
+ @socket.each_list_item do |line|
+ m = /\A(\d+)[ \t]+(\d+)/.match(line) or
+ raise POPBadResponse, "bad response: #{line}"
+ list.push [m[1].to_i, m[2].to_i]
+ end
+ return list
+ }
+ end
+
+ def stat
+ res = check_response(critical { get_response('STAT') })
+ m = /\A\+OK\s+(\d+)\s+(\d+)/.match(res) or
+ raise POPBadResponse, "wrong response format: #{res}"
+ [m[1].to_i, m[2].to_i]
+ end
+
+ def rset
+ check_response(critical { get_response('RSET') })
+ end
+
+ def top(num, lines = 0, &block)
+ critical {
+ getok('TOP %d %d', num, lines)
+ @socket.each_message_chunk(&block)
+ }
+ end
+
+ def retr(num, &block)
+ critical {
+ getok('RETR %d', num)
+ @socket.each_message_chunk(&block)
+ }
+ end
+
+ def dele(num)
+ check_response(critical { get_response('DELE %d', num) })
+ end
+
+ def uidl(num = nil)
+ if num
+ res = check_response(critical { get_response('UIDL %d', num) })
+ return res.split(/ /)[1]
+ else
+ critical {
+ getok('UIDL')
+ table = {}
+ @socket.each_list_item do |line|
+ num, uid = line.split
+ table[num.to_i] = uid
+ end
+ return table
+ }
+ end
+ end
+
+ def quit
+ check_response(critical { get_response('QUIT') })
+ end
+
+ private
+
+ def getok(fmt, *fargs)
+ @socket.writeline sprintf(fmt, *fargs)
+ check_response(recv_response())
+ end
+
+ def get_response(fmt, *fargs)
+ @socket.writeline sprintf(fmt, *fargs)
+ recv_response()
+ end
+
+ def recv_response
+ @socket.readline
+ end
+
+ def check_response(res)
+ raise POPError, res unless /\A\+OK/i =~ res
+ res
+ end
+
+ def check_response_auth(res)
+ raise POPAuthenticationError, res unless /\A\+OK/i =~ res
+ res
+ end
+
+ def critical
+ return '+OK dummy ok response' if @error_occured
+ begin
+ return yield()
+ rescue Exception
+ @error_occured = true
+ raise
+ end
+ end
+
+ end # class POP3Command
+
+end # module Net
diff --git a/ruby/lib/net/protocol.rb b/ruby/lib/net/protocol.rb
new file mode 100644
index 0000000..c1fd94b
--- /dev/null
+++ b/ruby/lib/net/protocol.rb
@@ -0,0 +1,382 @@
+#
+# = net/protocol.rb
+#
+#--
+# Copyright (c) 1999-2004 Yukihiro Matsumoto
+# Copyright (c) 1999-2004 Minero Aoki
+#
+# written and maintained by Minero Aoki <aamine@loveruby.net>
+#
+# This program is free software. You can re-distribute and/or
+# modify this program under the same terms as Ruby itself,
+# Ruby Distribute License or GNU General Public License.
+#
+# $Id: protocol.rb 12091 2007-03-19 02:27:08Z aamine $
+#++
+#
+# WARNING: This file is going to remove.
+# Do not rely on the implementation written in this file.
+#
+
+require 'socket'
+require 'timeout'
+
+module Net # :nodoc:
+
+ class Protocol #:nodoc: internal use only
+ private
+ def Protocol.protocol_param(name, val)
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def #{name}
+ #{val}
+ end
+ End
+ end
+ end
+
+
+ class ProtocolError < StandardError; end
+ class ProtoSyntaxError < ProtocolError; end
+ class ProtoFatalError < ProtocolError; end
+ class ProtoUnknownError < ProtocolError; end
+ class ProtoServerError < ProtocolError; end
+ class ProtoAuthError < ProtocolError; end
+ class ProtoCommandError < ProtocolError; end
+ class ProtoRetriableError < ProtocolError; end
+ ProtocRetryError = ProtoRetriableError
+
+
+ class BufferedIO #:nodoc: internal use only
+ def initialize(io)
+ @io = io
+ @read_timeout = 60
+ @debug_output = nil
+ @rbuf = ''
+ end
+
+ attr_reader :io
+ attr_accessor :read_timeout
+ attr_accessor :debug_output
+
+ def inspect
+ "#<#{self.class} io=#{@io}>"
+ end
+
+ def closed?
+ @io.closed?
+ end
+
+ def close
+ @io.close
+ end
+
+ #
+ # Read
+ #
+
+ public
+
+ def read(len, dest = '', ignore_eof = false)
+ LOG "reading #{len} bytes..."
+ read_bytes = 0
+ begin
+ while read_bytes + @rbuf.size < len
+ dest << (s = rbuf_consume(@rbuf.size))
+ read_bytes += s.size
+ rbuf_fill
+ end
+ dest << (s = rbuf_consume(len - read_bytes))
+ read_bytes += s.size
+ rescue EOFError
+ raise unless ignore_eof
+ end
+ LOG "read #{read_bytes} bytes"
+ dest
+ end
+
+ def read_all(dest = '')
+ LOG 'reading all...'
+ read_bytes = 0
+ begin
+ while true
+ dest << (s = rbuf_consume(@rbuf.size))
+ read_bytes += s.size
+ rbuf_fill
+ end
+ rescue EOFError
+ ;
+ end
+ LOG "read #{read_bytes} bytes"
+ dest
+ end
+
+ def readuntil(terminator, ignore_eof = false)
+ begin
+ until idx = @rbuf.index(terminator)
+ rbuf_fill
+ end
+ return rbuf_consume(idx + terminator.size)
+ rescue EOFError
+ raise unless ignore_eof
+ return rbuf_consume(@rbuf.size)
+ end
+ end
+
+ def readline
+ readuntil("\n").chop
+ end
+
+ private
+
+ BUFSIZE = 1024 * 16
+
+ def rbuf_fill
+ timeout(@read_timeout) {
+ @rbuf << @io.sysread(BUFSIZE)
+ }
+ end
+
+ def rbuf_consume(len)
+ s = @rbuf.slice!(0, len)
+ @debug_output << %Q[-> #{s.dump}\n] if @debug_output
+ s
+ end
+
+ #
+ # Write
+ #
+
+ public
+
+ def write(str)
+ writing {
+ write0 str
+ }
+ end
+
+ def writeline(str)
+ writing {
+ write0 str + "\r\n"
+ }
+ end
+
+ private
+
+ def writing
+ @written_bytes = 0
+ @debug_output << '<- ' if @debug_output
+ yield
+ @debug_output << "\n" if @debug_output
+ bytes = @written_bytes
+ @written_bytes = nil
+ bytes
+ end
+
+ def write0(str)
+ @debug_output << str.dump if @debug_output
+ len = @io.write(str)
+ @written_bytes += len
+ len
+ end
+
+ #
+ # Logging
+ #
+
+ private
+
+ def LOG_off
+ @save_debug_out = @debug_output
+ @debug_output = nil
+ end
+
+ def LOG_on
+ @debug_output = @save_debug_out
+ end
+
+ def LOG(msg)
+ return unless @debug_output
+ @debug_output << msg + "\n"
+ end
+ end
+
+
+ class InternetMessageIO < BufferedIO #:nodoc: internal use only
+ def initialize(io)
+ super
+ @wbuf = nil
+ end
+
+ #
+ # Read
+ #
+
+ def each_message_chunk
+ LOG 'reading message...'
+ LOG_off()
+ read_bytes = 0
+ while (line = readuntil("\r\n")) != ".\r\n"
+ read_bytes += line.size
+ yield line.sub(/\A\./, '')
+ end
+ LOG_on()
+ LOG "read message (#{read_bytes} bytes)"
+ end
+
+ # *library private* (cannot handle 'break')
+ def each_list_item
+ while (str = readuntil("\r\n")) != ".\r\n"
+ yield str.chop
+ end
+ end
+
+ def write_message_0(src)
+ prev = @written_bytes
+ each_crlf_line(src) do |line|
+ write0 line.sub(/\A\./, '..')
+ end
+ @written_bytes - prev
+ end
+
+ #
+ # Write
+ #
+
+ def write_message(src)
+ LOG "writing message from #{src.class}"
+ LOG_off()
+ len = writing {
+ using_each_crlf_line {
+ write_message_0 src
+ }
+ }
+ LOG_on()
+ LOG "wrote #{len} bytes"
+ len
+ end
+
+ def write_message_by_block(&block)
+ LOG 'writing message from block'
+ LOG_off()
+ len = writing {
+ using_each_crlf_line {
+ begin
+ block.call(WriteAdapter.new(self, :write_message_0))
+ rescue LocalJumpError
+ # allow `break' from writer block
+ end
+ }
+ }
+ LOG_on()
+ LOG "wrote #{len} bytes"
+ len
+ end
+
+ private
+
+ def using_each_crlf_line
+ @wbuf = ''
+ yield
+ if not @wbuf.empty? # unterminated last line
+ write0 @wbuf.chomp + "\r\n"
+ elsif @written_bytes == 0 # empty src
+ write0 "\r\n"
+ end
+ write0 ".\r\n"
+ @wbuf = nil
+ end
+
+ def each_crlf_line(src)
+ buffer_filling(@wbuf, src) do
+ while line = @wbuf.slice!(/\A.*(?:\n|\r\n|\r(?!\z))/n)
+ yield line.chomp("\n") + "\r\n"
+ end
+ end
+ end
+
+ def buffer_filling(buf, src)
+ case src
+ when String # for speeding up.
+ 0.step(src.size - 1, 1024) do |i|
+ buf << src[i, 1024]
+ yield
+ end
+ when File # for speeding up.
+ while s = src.read(1024)
+ buf << s
+ yield
+ end
+ else # generic reader
+ src.each do |str|
+ buf << str
+ yield if buf.size > 1024
+ end
+ yield unless buf.empty?
+ end
+ end
+ end
+
+
+ #
+ # The writer adapter class
+ #
+ class WriteAdapter
+ def initialize(socket, method)
+ @socket = socket
+ @method_id = method
+ end
+
+ def inspect
+ "#<#{self.class} socket=#{@socket.inspect}>"
+ end
+
+ def write(str)
+ @socket.__send__(@method_id, str)
+ end
+
+ alias print write
+
+ def <<(str)
+ write str
+ self
+ end
+
+ def puts(str = '')
+ write str.chomp("\n") + "\n"
+ end
+
+ def printf(*args)
+ write sprintf(*args)
+ end
+ end
+
+
+ class ReadAdapter #:nodoc: internal use only
+ def initialize(block)
+ @block = block
+ end
+
+ def inspect
+ "#<#{self.class}>"
+ end
+
+ def <<(str)
+ call_block(str, &@block) if @block
+ end
+
+ private
+
+ # This method is needed because @block must be called by yield,
+ # not Proc#call. You can see difference when using `break' in
+ # the block.
+ def call_block(str)
+ yield str
+ end
+ end
+
+
+ module NetPrivate #:nodoc: obsolete
+ Socket = ::Net::InternetMessageIO
+ end
+
+end # module Net
diff --git a/ruby/lib/net/smtp.rb b/ruby/lib/net/smtp.rb
new file mode 100644
index 0000000..4aef8d7
--- /dev/null
+++ b/ruby/lib/net/smtp.rb
@@ -0,0 +1,1014 @@
+# = net/smtp.rb
+#
+# Copyright (c) 1999-2007 Yukihiro Matsumoto.
+#
+# Copyright (c) 1999-2007 Minero Aoki.
+#
+# Written & maintained by Minero Aoki <aamine@loveruby.net>.
+#
+# Documented by William Webber and Minero Aoki.
+#
+# This program is free software. You can re-distribute and/or
+# modify this program under the same terms as Ruby itself.
+#
+# NOTE: You can find Japanese version of this document at:
+# http://www.ruby-lang.org/ja/man/html/net_smtp.html
+#
+# $Id: smtp.rb 18351 2008-08-04 05:46:53Z shyouhei $
+#
+# See Net::SMTP for documentation.
+#
+
+require 'net/protocol'
+require 'digest/md5'
+require 'timeout'
+begin
+ require 'openssl'
+rescue LoadError
+end
+
+module Net
+
+ # Module mixed in to all SMTP error classes
+ module SMTPError
+ # This *class* is a module for backward compatibility.
+ # In later release, this module becomes a class.
+ end
+
+ # Represents an SMTP authentication error.
+ class SMTPAuthenticationError < ProtoAuthError
+ include SMTPError
+ end
+
+ # Represents SMTP error code 420 or 450, a temporary error.
+ class SMTPServerBusy < ProtoServerError
+ include SMTPError
+ end
+
+ # Represents an SMTP command syntax error (error code 500)
+ class SMTPSyntaxError < ProtoSyntaxError
+ include SMTPError
+ end
+
+ # Represents a fatal SMTP error (error code 5xx, except for 500)
+ class SMTPFatalError < ProtoFatalError
+ include SMTPError
+ end
+
+ # Unexpected reply code returned from server.
+ class SMTPUnknownError < ProtoUnknownError
+ include SMTPError
+ end
+
+ # Command is not supported on server.
+ class SMTPUnsupportedCommand < ProtocolError
+ include SMTPError
+ end
+
+ #
+ # = Net::SMTP
+ #
+ # == What is This Library?
+ #
+ # This library provides functionality to send internet
+ # mail via SMTP, the Simple Mail Transfer Protocol. For details of
+ # SMTP itself, see [RFC2821] (http://www.ietf.org/rfc/rfc2821.txt).
+ #
+ # == What is This Library NOT?
+ #
+ # This library does NOT provide functions to compose internet mails.
+ # You must create them by yourself. If you want better mail support,
+ # try RubyMail or TMail. You can get both libraries from RAA.
+ # (http://www.ruby-lang.org/en/raa.html)
+ #
+ # FYI: the official documentation on internet mail is: [RFC2822] (http://www.ietf.org/rfc/rfc2822.txt).
+ #
+ # == Examples
+ #
+ # === Sending Messages
+ #
+ # You must open a connection to an SMTP server before sending messages.
+ # The first argument is the address of your SMTP server, and the second
+ # argument is the port number. Using SMTP.start with a block is the simplest
+ # way to do this. This way, the SMTP connection is closed automatically
+ # after the block is executed.
+ #
+ # require 'net/smtp'
+ # Net::SMTP.start('your.smtp.server', 25) do |smtp|
+ # # Use the SMTP object smtp only in this block.
+ # end
+ #
+ # Replace 'your.smtp.server' with your SMTP server. Normally
+ # your system manager or internet provider supplies a server
+ # for you.
+ #
+ # Then you can send messages.
+ #
+ # msgstr = <<END_OF_MESSAGE
+ # From: Your Name <your@mail.address>
+ # To: Destination Address <someone@example.com>
+ # Subject: test message
+ # Date: Sat, 23 Jun 2001 16:26:43 +0900
+ # Message-Id: <unique.message.id.string@example.com>
+ #
+ # This is a test message.
+ # END_OF_MESSAGE
+ #
+ # require 'net/smtp'
+ # Net::SMTP.start('your.smtp.server', 25) do |smtp|
+ # smtp.send_message msgstr,
+ # 'your@mail.address',
+ # 'his_addess@example.com'
+ # end
+ #
+ # === Closing the Session
+ #
+ # You MUST close the SMTP session after sending messages, by calling
+ # the #finish method:
+ #
+ # # using SMTP#finish
+ # smtp = Net::SMTP.start('your.smtp.server', 25)
+ # smtp.send_message msgstr, 'from@address', 'to@address'
+ # smtp.finish
+ #
+ # You can also use the block form of SMTP.start/SMTP#start. This closes
+ # the SMTP session automatically:
+ #
+ # # using block form of SMTP.start
+ # Net::SMTP.start('your.smtp.server', 25) do |smtp|
+ # smtp.send_message msgstr, 'from@address', 'to@address'
+ # end
+ #
+ # I strongly recommend this scheme. This form is simpler and more robust.
+ #
+ # === HELO domain
+ #
+ # In almost all situations, you must provide a third argument
+ # to SMTP.start/SMTP#start. This is the domain name which you are on
+ # (the host to send mail from). It is called the "HELO domain".
+ # The SMTP server will judge whether it should send or reject
+ # the SMTP session by inspecting the HELO domain.
+ #
+ # Net::SMTP.start('your.smtp.server', 25,
+ # 'mail.from.domain') { |smtp| ... }
+ #
+ # === SMTP Authentication
+ #
+ # The Net::SMTP class supports three authentication schemes;
+ # PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554])
+ # To use SMTP authentication, pass extra arguments to
+ # SMTP.start/SMTP#start.
+ #
+ # # PLAIN
+ # Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
+ # 'Your Account', 'Your Password', :plain)
+ # # LOGIN
+ # Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
+ # 'Your Account', 'Your Password', :login)
+ #
+ # # CRAM MD5
+ # Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
+ # 'Your Account', 'Your Password', :cram_md5)
+ #
+ class SMTP
+
+ Revision = %q$Revision: 18351 $.split[1]
+
+ # The default SMTP port number, 25.
+ def SMTP.default_port
+ 25
+ end
+
+ # The default mail submission port number, 587.
+ def SMTP.default_submission_port
+ 587
+ end
+
+ # The default SMTPS port number, 465.
+ def SMTP.default_tls_port
+ 465
+ end
+
+ class << self
+ alias default_ssl_port default_tls_port
+ end
+
+ def SMTP.default_ssl_context
+ OpenSSL::SSL::SSLContext.new
+ end
+
+ #
+ # Creates a new Net::SMTP object.
+ #
+ # +address+ is the hostname or ip address of your SMTP
+ # server. +port+ is the port to connect to; it defaults to
+ # port 25.
+ #
+ # This method does not open the TCP connection. You can use
+ # SMTP.start instead of SMTP.new if you want to do everything
+ # at once. Otherwise, follow SMTP.new with SMTP#start.
+ #
+ def initialize(address, port = nil)
+ @address = address
+ @port = (port || SMTP.default_port)
+ @esmtp = true
+ @capabilities = nil
+ @socket = nil
+ @started = false
+ @open_timeout = 30
+ @read_timeout = 60
+ @error_occured = false
+ @debug_output = nil
+ @tls = false
+ @starttls = false
+ @ssl_context = nil
+ end
+
+ # Provide human-readable stringification of class state.
+ def inspect
+ "#<#{self.class} #{@address}:#{@port} started=#{@started}>"
+ end
+
+ # +true+ if the SMTP object uses ESMTP (which it does by default).
+ def esmtp?
+ @esmtp
+ end
+
+ #
+ # Set whether to use ESMTP or not. This should be done before
+ # calling #start. Note that if #start is called in ESMTP mode,
+ # and the connection fails due to a ProtocolError, the SMTP
+ # object will automatically switch to plain SMTP mode and
+ # retry (but not vice versa).
+ #
+ def esmtp=(bool)
+ @esmtp = bool
+ end
+
+ alias esmtp esmtp?
+
+ # true if server advertises STARTTLS.
+ # You cannot get valid value before opening SMTP session.
+ def capable_starttls?
+ capable?('STARTTLS')
+ end
+
+ def capable?(key)
+ return nil unless @capabilities
+ @capabilities[key] ? true : false
+ end
+ private :capable?
+
+ # true if server advertises AUTH PLAIN.
+ # You cannot get valid value before opening SMTP session.
+ def capable_plain_auth?
+ auth_capable?('PLAIN')
+ end
+
+ # true if server advertises AUTH LOGIN.
+ # You cannot get valid value before opening SMTP session.
+ def capable_login_auth?
+ auth_capable?('LOGIN')
+ end
+
+ # true if server advertises AUTH CRAM-MD5.
+ # You cannot get valid value before opening SMTP session.
+ def capable_cram_md5_auth?
+ auth_capable?('CRAM-MD5')
+ end
+
+ def auth_capable?(type)
+ return nil unless @capabilities
+ return false unless @capabilities['AUTH']
+ @capabilities['AUTH'].include?(type)
+ end
+ private :auth_capable?
+
+ # Returns supported authentication methods on this server.
+ # You cannot get valid value before opening SMTP session.
+ def capable_auth_types
+ return [] unless @capabilities
+ return [] unless @capabilities['AUTH']
+ @capabilities['AUTH']
+ end
+
+ # true if this object uses SMTP/TLS (SMTPS).
+ def tls?
+ @tls
+ end
+
+ alias ssl? tls?
+
+ # Enables SMTP/TLS (SMTPS: SMTP over direct TLS connection) for
+ # this object. Must be called before the connection is established
+ # to have any effect. +context+ is a OpenSSL::SSL::SSLContext object.
+ def enable_tls(context = SMTP.default_ssl_context)
+ raise 'openssl library not installed' unless defined?(OpenSSL)
+ raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @starttls
+ @tls = true
+ @ssl_context = context
+ end
+
+ alias enable_ssl enable_tls
+
+ # Disables SMTP/TLS for this object. Must be called before the
+ # connection is established to have any effect.
+ def disable_tls
+ @tls = false
+ @ssl_context = nil
+ end
+
+ alias disable_ssl disable_tls
+
+ # Returns truth value if this object uses STARTTLS.
+ # If this object always uses STARTTLS, returns :always.
+ # If this object uses STARTTLS when the server support TLS, returns :auto.
+ def starttls?
+ @starttls
+ end
+
+ # true if this object uses STARTTLS.
+ def starttls_always?
+ @starttls == :always
+ end
+
+ # true if this object uses STARTTLS when server advertises STARTTLS.
+ def starttls_auto?
+ @starttls == :auto
+ end
+
+ # Enables SMTP/TLS (STARTTLS) for this object.
+ # +context+ is a OpenSSL::SSL::SSLContext object.
+ def enable_starttls(context = SMTP.default_ssl_context)
+ raise 'openssl library not installed' unless defined?(OpenSSL)
+ raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @tls
+ @starttls = :always
+ @ssl_context = context
+ end
+
+ # Enables SMTP/TLS (STARTTLS) for this object if server accepts.
+ # +context+ is a OpenSSL::SSL::SSLContext object.
+ def enable_starttls_auto(context = SMTP.default_ssl_context)
+ raise 'openssl library not installed' unless defined?(OpenSSL)
+ raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @tls
+ @starttls = :auto
+ @ssl_context = context
+ end
+
+ # Disables SMTP/TLS (STARTTLS) for this object. Must be called
+ # before the connection is established to have any effect.
+ def disable_starttls
+ @starttls = false
+ @ssl_context = nil
+ end
+
+ # The address of the SMTP server to connect to.
+ attr_reader :address
+
+ # The port number of the SMTP server to connect to.
+ attr_reader :port
+
+ # Seconds to wait while attempting to open a connection.
+ # If the connection cannot be opened within this time, a
+ # TimeoutError is raised.
+ attr_accessor :open_timeout
+
+ # Seconds to wait while reading one block (by one read(2) call).
+ # If the read(2) call does not complete within this time, a
+ # TimeoutError is raised.
+ attr_reader :read_timeout
+
+ # Set the number of seconds to wait until timing-out a read(2)
+ # call.
+ def read_timeout=(sec)
+ @socket.read_timeout = sec if @socket
+ @read_timeout = sec
+ end
+
+ #
+ # WARNING: This method causes serious security holes.
+ # Use this method for only debugging.
+ #
+ # Set an output stream for debug logging.
+ # You must call this before #start.
+ #
+ # # example
+ # smtp = Net::SMTP.new(addr, port)
+ # smtp.set_debug_output $stderr
+ # smtp.start do |smtp|
+ # ....
+ # end
+ #
+ def debug_output=(arg)
+ @debug_output = arg
+ end
+
+ alias set_debug_output debug_output=
+
+ #
+ # SMTP session control
+ #
+
+ #
+ # Creates a new Net::SMTP object and connects to the server.
+ #
+ # This method is equivalent to:
+ #
+ # Net::SMTP.new(address, port).start(helo_domain, account, password, authtype)
+ #
+ # === Example
+ #
+ # Net::SMTP.start('your.smtp.server') do |smtp|
+ # smtp.send_message msgstr, 'from@example.com', ['dest@example.com']
+ # end
+ #
+ # === Block Usage
+ #
+ # If called with a block, the newly-opened Net::SMTP object is yielded
+ # to the block, and automatically closed when the block finishes. If called
+ # without a block, the newly-opened Net::SMTP object is returned to
+ # the caller, and it is the caller's responsibility to close it when
+ # finished.
+ #
+ # === Parameters
+ #
+ # +address+ is the hostname or ip address of your smtp server.
+ #
+ # +port+ is the port to connect to; it defaults to port 25.
+ #
+ # +helo+ is the _HELO_ _domain_ provided by the client to the
+ # server (see overview comments); it defaults to 'localhost'.
+ #
+ # The remaining arguments are used for SMTP authentication, if required
+ # or desired. +user+ is the account name; +secret+ is your password
+ # or other authentication token; and +authtype+ is the authentication
+ # type, one of :plain, :login, or :cram_md5. See the discussion of
+ # SMTP Authentication in the overview notes.
+ #
+ # === Errors
+ #
+ # This method may raise:
+ #
+ # * Net::SMTPAuthenticationError
+ # * Net::SMTPServerBusy
+ # * Net::SMTPSyntaxError
+ # * Net::SMTPFatalError
+ # * Net::SMTPUnknownError
+ # * IOError
+ # * TimeoutError
+ #
+ def SMTP.start(address, port = nil, helo = 'localhost',
+ user = nil, secret = nil, authtype = nil,
+ &block) # :yield: smtp
+ new(address, port).start(helo, user, secret, authtype, &block)
+ end
+
+ # +true+ if the SMTP session has been started.
+ def started?
+ @started
+ end
+
+ #
+ # Opens a TCP connection and starts the SMTP session.
+ #
+ # === Parameters
+ #
+ # +helo+ is the _HELO_ _domain_ that you'll dispatch mails from; see
+ # the discussion in the overview notes.
+ #
+ # If both of +user+ and +secret+ are given, SMTP authentication
+ # will be attempted using the AUTH command. +authtype+ specifies
+ # the type of authentication to attempt; it must be one of
+ # :login, :plain, and :cram_md5. See the notes on SMTP Authentication
+ # in the overview.
+ #
+ # === Block Usage
+ #
+ # When this methods is called with a block, the newly-started SMTP
+ # object is yielded to the block, and automatically closed after
+ # the block call finishes. Otherwise, it is the caller's
+ # responsibility to close the session when finished.
+ #
+ # === Example
+ #
+ # This is very similar to the class method SMTP.start.
+ #
+ # require 'net/smtp'
+ # smtp = Net::SMTP.new('smtp.mail.server', 25)
+ # smtp.start(helo_domain, account, password, authtype) do |smtp|
+ # smtp.send_message msgstr, 'from@example.com', ['dest@example.com']
+ # end
+ #
+ # The primary use of this method (as opposed to SMTP.start)
+ # is probably to set debugging (#set_debug_output) or ESMTP
+ # (#esmtp=), which must be done before the session is
+ # started.
+ #
+ # === Errors
+ #
+ # If session has already been started, an IOError will be raised.
+ #
+ # This method may raise:
+ #
+ # * Net::SMTPAuthenticationError
+ # * Net::SMTPServerBusy
+ # * Net::SMTPSyntaxError
+ # * Net::SMTPFatalError
+ # * Net::SMTPUnknownError
+ # * IOError
+ # * TimeoutError
+ #
+ def start(helo = 'localhost',
+ user = nil, secret = nil, authtype = nil) # :yield: smtp
+ if block_given?
+ begin
+ do_start helo, user, secret, authtype
+ return yield(self)
+ ensure
+ do_finish
+ end
+ else
+ do_start helo, user, secret, authtype
+ return self
+ end
+ end
+
+ # Finishes the SMTP session and closes TCP connection.
+ # Raises IOError if not started.
+ def finish
+ raise IOError, 'not yet started' unless started?
+ do_finish
+ end
+
+ private
+
+ def do_start(helo_domain, user, secret, authtype)
+ raise IOError, 'SMTP session already started' if @started
+ if user or secret
+ check_auth_method(authtype || DEFAULT_AUTH_TYPE)
+ check_auth_args user, secret
+ end
+ s = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
+ logging "Connection opened: #{@address}:#{@port}"
+ @socket = new_internet_message_io(tls? ? tlsconnect(s) : s)
+ check_response critical { recv_response() }
+ do_helo helo_domain
+ if starttls_always? or (capable_starttls? and starttls_auto?)
+ unless capable_starttls?
+ raise SMTPUnsupportedCommand,
+ "STARTTLS is not supported on this server"
+ end
+ starttls
+ @socket = new_internet_message_io(tlsconnect(s))
+ # helo response may be different after STARTTLS
+ do_helo helo_domain
+ end
+ authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
+ @started = true
+ ensure
+ unless @started
+ # authentication failed, cancel connection.
+ s.close if s and not s.closed?
+ @socket = nil
+ end
+ end
+
+ def tlsconnect(s)
+ s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
+ logging "TLS connection started"
+ s.sync_close = true
+ s.connect
+ if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
+ s.post_connection_check(@address)
+ end
+ s
+ end
+
+ def new_internet_message_io(s)
+ io = InternetMessageIO.new(s)
+ io.read_timeout = @read_timeout
+ io.debug_output = @debug_output
+ io
+ end
+
+ def do_helo(helo_domain)
+ res = @esmtp ? ehlo(helo_domain) : helo(helo_domain)
+ @capabilities = res.capabilities
+ rescue SMTPError
+ if @esmtp
+ @esmtp = false
+ @error_occured = false
+ retry
+ end
+ raise
+ end
+
+ def do_finish
+ quit if @socket and not @socket.closed? and not @error_occured
+ ensure
+ @started = false
+ @error_occured = false
+ @socket.close if @socket and not @socket.closed?
+ @socket = nil
+ end
+
+ #
+ # Message Sending
+ #
+
+ public
+
+ #
+ # Sends +msgstr+ as a message. Single CR ("\r") and LF ("\n") found
+ # in the +msgstr+, are converted into the CR LF pair. You cannot send a
+ # binary message with this method. +msgstr+ should include both
+ # the message headers and body.
+ #
+ # +from_addr+ is a String representing the source mail address.
+ #
+ # +to_addr+ is a String or Strings or Array of Strings, representing
+ # the destination mail address or addresses.
+ #
+ # === Example
+ #
+ # Net::SMTP.start('smtp.example.com') do |smtp|
+ # smtp.send_message msgstr,
+ # 'from@example.com',
+ # ['dest@example.com', 'dest2@example.com']
+ # end
+ #
+ # === Errors
+ #
+ # This method may raise:
+ #
+ # * Net::SMTPServerBusy
+ # * Net::SMTPSyntaxError
+ # * Net::SMTPFatalError
+ # * Net::SMTPUnknownError
+ # * IOError
+ # * TimeoutError
+ #
+ def send_message(msgstr, from_addr, *to_addrs)
+ raise IOError, 'closed session' unless @socket
+ mailfrom from_addr
+ rcptto_list to_addrs
+ data msgstr
+ end
+
+ alias send_mail send_message
+ alias sendmail send_message # obsolete
+
+ #
+ # Opens a message writer stream and gives it to the block.
+ # The stream is valid only in the block, and has these methods:
+ #
+ # puts(str = ''):: outputs STR and CR LF.
+ # print(str):: outputs STR.
+ # printf(fmt, *args):: outputs sprintf(fmt,*args).
+ # write(str):: outputs STR and returns the length of written bytes.
+ # <<(str):: outputs STR and returns self.
+ #
+ # If a single CR ("\r") or LF ("\n") is found in the message,
+ # it is converted to the CR LF pair. You cannot send a binary
+ # message with this method.
+ #
+ # === Parameters
+ #
+ # +from_addr+ is a String representing the source mail address.
+ #
+ # +to_addr+ is a String or Strings or Array of Strings, representing
+ # the destination mail address or addresses.
+ #
+ # === Example
+ #
+ # Net::SMTP.start('smtp.example.com', 25) do |smtp|
+ # smtp.open_message_stream('from@example.com', ['dest@example.com']) do |f|
+ # f.puts 'From: from@example.com'
+ # f.puts 'To: dest@example.com'
+ # f.puts 'Subject: test message'
+ # f.puts
+ # f.puts 'This is a test message.'
+ # end
+ # end
+ #
+ # === Errors
+ #
+ # This method may raise:
+ #
+ # * Net::SMTPServerBusy
+ # * Net::SMTPSyntaxError
+ # * Net::SMTPFatalError
+ # * Net::SMTPUnknownError
+ # * IOError
+ # * TimeoutError
+ #
+ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
+ raise IOError, 'closed session' unless @socket
+ mailfrom from_addr
+ rcptto_list to_addrs
+ data(&block)
+ end
+
+ alias ready open_message_stream # obsolete
+
+ #
+ # Authentication
+ #
+
+ public
+
+ DEFAULT_AUTH_TYPE = :plain
+
+ def authenticate(user, secret, authtype = DEFAULT_AUTH_TYPE)
+ check_auth_method authtype
+ check_auth_args user, secret
+ send auth_method(authtype), user, secret
+ end
+
+ def auth_plain(user, secret)
+ check_auth_args user, secret
+ res = critical {
+ get_response('AUTH PLAIN ' + base64_encode("\0#{user}\0#{secret}"))
+ }
+ check_auth_response res
+ res
+ end
+
+ def auth_login(user, secret)
+ check_auth_args user, secret
+ res = critical {
+ check_auth_continue get_response('AUTH LOGIN')
+ check_auth_continue get_response(base64_encode(user))
+ get_response(base64_encode(secret))
+ }
+ check_auth_response res
+ res
+ end
+
+ def auth_cram_md5(user, secret)
+ check_auth_args user, secret
+ res = critical {
+ res0 = get_response('AUTH CRAM-MD5')
+ check_auth_continue res0
+ crammed = cram_md5_response(secret, res0.cram_md5_challenge)
+ get_response(base64_encode("#{user} #{crammed}"))
+ }
+ check_auth_response res
+ res
+ end
+
+ private
+
+ def check_auth_method(type)
+ unless respond_to?(auth_method(type), true)
+ raise ArgumentError, "wrong authentication type #{type}"
+ end
+ end
+
+ def auth_method(type)
+ "auth_#{type.to_s.downcase}".intern
+ end
+
+ def check_auth_args(user, secret)
+ unless user
+ raise ArgumentError, 'SMTP-AUTH requested but missing user name'
+ end
+ unless secret
+ raise ArgumentError, 'SMTP-AUTH requested but missing secret phrase'
+ end
+ end
+
+ def base64_encode(str)
+ # expects "str" may not become too long
+ [str].pack('m').gsub(/\s+/, '')
+ end
+
+ IMASK = 0x36
+ OMASK = 0x5c
+
+ # CRAM-MD5: [RFC2195]
+ def cram_md5_response(secret, challenge)
+ tmp = Digest::MD5.digest(cram_secret(secret, IMASK) + challenge)
+ Digest::MD5.hexdigest(cram_secret(secret, OMASK) + tmp)
+ end
+
+ CRAM_BUFSIZE = 64
+
+ def cram_secret(secret, mask)
+ secret = Digest::MD5.digest(secret) if secret.size > CRAM_BUFSIZE
+ buf = secret.ljust(CRAM_BUFSIZE, "\0")
+ 0.upto(buf.size - 1) do |i|
+ buf[i] = (buf[i].ord ^ mask).chr
+ end
+ buf
+ end
+
+ #
+ # SMTP command dispatcher
+ #
+
+ public
+
+ def starttls
+ getok('STARTTLS')
+ end
+
+ def helo(domain)
+ getok("HELO #{domain}")
+ end
+
+ def ehlo(domain)
+ getok("EHLO #{domain}")
+ end
+
+ def mailfrom(from_addr)
+ if $SAFE > 0
+ raise SecurityError, 'tainted from_addr' if from_addr.tainted?
+ end
+ getok("MAIL FROM:<#{from_addr}>")
+ end
+
+ def rcptto_list(to_addrs)
+ raise ArgumentError, 'mail destination not given' if to_addrs.empty?
+ to_addrs.flatten.each do |addr|
+ rcptto addr
+ end
+ end
+
+ def rcptto(to_addr)
+ if $SAFE > 0
+ raise SecurityError, 'tainted to_addr' if to_addr.tainted?
+ end
+ getok("RCPT TO:<#{to_addr}>")
+ end
+
+ # This method sends a message.
+ # If +msgstr+ is given, sends it as a message.
+ # If block is given, yield a message writer stream.
+ # You must write message before the block is closed.
+ #
+ # # Example 1 (by string)
+ # smtp.data(<<EndMessage)
+ # From: john@example.com
+ # To: betty@example.com
+ # Subject: I found a bug
+ #
+ # Check vm.c:58879.
+ # EndMessage
+ #
+ # # Example 2 (by block)
+ # smtp.data {|f|
+ # f.puts "From: john@example.com"
+ # f.puts "To: betty@example.com"
+ # f.puts "Subject: I found a bug"
+ # f.puts ""
+ # f.puts "Check vm.c:58879."
+ # }
+ #
+ def data(msgstr = nil, &block) #:yield: stream
+ if msgstr and block
+ raise ArgumentError, "message and block are exclusive"
+ end
+ unless msgstr or block
+ raise ArgumentError, "message or block is required"
+ end
+ res = critical {
+ check_continue get_response('DATA')
+ if msgstr
+ @socket.write_message msgstr
+ else
+ @socket.write_message_by_block(&block)
+ end
+ recv_response()
+ }
+ check_response res
+ res
+ end
+
+ def quit
+ getok('QUIT')
+ end
+
+ private
+
+ def getok(reqline)
+ res = critical {
+ @socket.writeline reqline
+ recv_response()
+ }
+ check_response res
+ res
+ end
+
+ def get_response(reqline)
+ @socket.writeline reqline
+ recv_response()
+ end
+
+ def recv_response
+ buf = ''
+ while true
+ line = @socket.readline
+ buf << line << "\n"
+ break unless line[3,1] == '-' # "210-PIPELINING"
+ end
+ Response.parse(buf)
+ end
+
+ def critical(&block)
+ return '200 dummy reply code' if @error_occured
+ begin
+ return yield()
+ rescue Exception
+ @error_occured = true
+ raise
+ end
+ end
+
+ def check_response(res)
+ unless res.success?
+ raise res.exception_class, res.message
+ end
+ end
+
+ def check_continue(res)
+ unless res.continue?
+ raise SMTPUnknownError, "could not get 3xx (#{res.status})"
+ end
+ end
+
+ def check_auth_response(res)
+ unless res.success?
+ raise SMTPAuthenticationError, res.message
+ end
+ end
+
+ def check_auth_continue(res)
+ unless res.continue?
+ raise res.exception_class, res.message
+ end
+ end
+
+ class Response
+ def Response.parse(str)
+ new(str[0,3], str)
+ end
+
+ def initialize(status, string)
+ @status = status
+ @string = string
+ end
+
+ attr_reader :status
+ attr_reader :string
+
+ def status_type_char
+ @status[0, 1]
+ end
+
+ def success?
+ status_type_char() == '2'
+ end
+
+ def continue?
+ status_type_char() == '3'
+ end
+
+ def message
+ @string.lines.first
+ end
+
+ def cram_md5_challenge
+ @string.split(/ /)[1].unpack('m')[0]
+ end
+
+ def capabilities
+ return {} unless @string[3, 1] == '-'
+ h = {}
+ @string.lines.drop(1).each do |line|
+ k, *v = line[4..-1].chomp.split(nil)
+ h[k] = v
+ end
+ h
+ end
+
+ def exception_class
+ case @status
+ when /\A4/ then SMTPServerBusy
+ when /\A50/ then SMTPSyntaxError
+ when /\A53/ then SMTPAuthenticationError
+ when /\A5/ then SMTPFatalError
+ else SMTPUnknownError
+ end
+ end
+ end
+
+ def logging(msg)
+ @debug_output << msg + "\n" if @debug_output
+ end
+
+ end # class SMTP
+
+ SMTPSession = SMTP
+
+end
diff --git a/ruby/lib/net/telnet.rb b/ruby/lib/net/telnet.rb
new file mode 100644
index 0000000..d560c93
--- /dev/null
+++ b/ruby/lib/net/telnet.rb
@@ -0,0 +1,759 @@
+# = net/telnet.rb - Simple Telnet Client Library
+#
+# Author:: Wakou Aoyama <wakou@ruby-lang.org>
+# Documentation:: William Webber and Wakou Aoyama
+#
+# This file holds the class Net::Telnet, which provides client-side
+# telnet functionality.
+#
+# For documentation, see Net::Telnet.
+#
+
+require "socket"
+require "delegate"
+require "timeout"
+require "English"
+
+module Net
+
+ #
+ # == Net::Telnet
+ #
+ # Provides telnet client functionality.
+ #
+ # This class also has, through delegation, all the methods of a
+ # socket object (by default, a +TCPSocket+, but can be set by the
+ # +Proxy+ option to <tt>new()</tt>). This provides methods such as
+ # <tt>close()</tt> to end the session and <tt>sysread()</tt> to read
+ # data directly from the host, instead of via the <tt>waitfor()</tt>
+ # mechanism. Note that if you do use <tt>sysread()</tt> directly
+ # when in telnet mode, you should probably pass the output through
+ # <tt>preprocess()</tt> to extract telnet command sequences.
+ #
+ # == Overview
+ #
+ # The telnet protocol allows a client to login remotely to a user
+ # account on a server and execute commands via a shell. The equivalent
+ # is done by creating a Net::Telnet class with the +Host+ option
+ # set to your host, calling #login() with your user and password,
+ # issuing one or more #cmd() calls, and then calling #close()
+ # to end the session. The #waitfor(), #print(), #puts(), and
+ # #write() methods, which #cmd() is implemented on top of, are
+ # only needed if you are doing something more complicated.
+ #
+ # A Net::Telnet object can also be used to connect to non-telnet
+ # services, such as SMTP or HTTP. In this case, you normally
+ # want to provide the +Port+ option to specify the port to
+ # connect to, and set the +Telnetmode+ option to false to prevent
+ # the client from attempting to interpret telnet command sequences.
+ # Generally, #login() will not work with other protocols, and you
+ # have to handle authentication yourself.
+ #
+ # For some protocols, it will be possible to specify the +Prompt+
+ # option once when you create the Telnet object and use #cmd() calls;
+ # for others, you will have to specify the response sequence to
+ # look for as the Match option to every #cmd() call, or call
+ # #puts() and #waitfor() directly; for yet others, you will have
+ # to use #sysread() instead of #waitfor() and parse server
+ # responses yourself.
+ #
+ # It is worth noting that when you create a new Net::Telnet object,
+ # you can supply a proxy IO channel via the Proxy option. This
+ # can be used to attach the Telnet object to other Telnet objects,
+ # to already open sockets, or to any read-write IO object. This
+ # can be useful, for instance, for setting up a test fixture for
+ # unit testing.
+ #
+ # == Examples
+ #
+ # === Log in and send a command, echoing all output to stdout
+ #
+ # localhost = Net::Telnet::new("Host" => "localhost",
+ # "Timeout" => 10,
+ # "Prompt" => /[$%#>] \z/n)
+ # localhost.login("username", "password") { |c| print c }
+ # localhost.cmd("command") { |c| print c }
+ # localhost.close
+ #
+ #
+ # === Check a POP server to see if you have mail
+ #
+ # pop = Net::Telnet::new("Host" => "your_destination_host_here",
+ # "Port" => 110,
+ # "Telnetmode" => false,
+ # "Prompt" => /^\+OK/n)
+ # pop.cmd("user " + "your_username_here") { |c| print c }
+ # pop.cmd("pass " + "your_password_here") { |c| print c }
+ # pop.cmd("list") { |c| print c }
+ #
+ # == References
+ #
+ # There are a large number of RFCs relevant to the Telnet protocol.
+ # RFCs 854-861 define the base protocol. For a complete listing
+ # of relevant RFCs, see
+ # http://www.omnifarious.org/~hopper/technical/telnet-rfc.html
+ #
+ class Telnet < SimpleDelegator
+
+ # :stopdoc:
+ IAC = 255.chr # "\377" # "\xff" # interpret as command
+ DONT = 254.chr # "\376" # "\xfe" # you are not to use option
+ DO = 253.chr # "\375" # "\xfd" # please, you use option
+ WONT = 252.chr # "\374" # "\xfc" # I won't use option
+ WILL = 251.chr # "\373" # "\xfb" # I will use option
+ SB = 250.chr # "\372" # "\xfa" # interpret as subnegotiation
+ GA = 249.chr # "\371" # "\xf9" # you may reverse the line
+ EL = 248.chr # "\370" # "\xf8" # erase the current line
+ EC = 247.chr # "\367" # "\xf7" # erase the current character
+ AYT = 246.chr # "\366" # "\xf6" # are you there
+ AO = 245.chr # "\365" # "\xf5" # abort output--but let prog finish
+ IP = 244.chr # "\364" # "\xf4" # interrupt process--permanently
+ BREAK = 243.chr # "\363" # "\xf3" # break
+ DM = 242.chr # "\362" # "\xf2" # data mark--for connect. cleaning
+ NOP = 241.chr # "\361" # "\xf1" # nop
+ SE = 240.chr # "\360" # "\xf0" # end sub negotiation
+ EOR = 239.chr # "\357" # "\xef" # end of record (transparent mode)
+ ABORT = 238.chr # "\356" # "\xee" # Abort process
+ SUSP = 237.chr # "\355" # "\xed" # Suspend process
+ EOF = 236.chr # "\354" # "\xec" # End of file
+ SYNCH = 242.chr # "\362" # "\xf2" # for telfunc calls
+
+ OPT_BINARY = 0.chr # "\000" # "\x00" # Binary Transmission
+ OPT_ECHO = 1.chr # "\001" # "\x01" # Echo
+ OPT_RCP = 2.chr # "\002" # "\x02" # Reconnection
+ OPT_SGA = 3.chr # "\003" # "\x03" # Suppress Go Ahead
+ OPT_NAMS = 4.chr # "\004" # "\x04" # Approx Message Size Negotiation
+ OPT_STATUS = 5.chr # "\005" # "\x05" # Status
+ OPT_TM = 6.chr # "\006" # "\x06" # Timing Mark
+ OPT_RCTE = 7.chr # "\a" # "\x07" # Remote Controlled Trans and Echo
+ OPT_NAOL = 8.chr # "\010" # "\x08" # Output Line Width
+ OPT_NAOP = 9.chr # "\t" # "\x09" # Output Page Size
+ OPT_NAOCRD = 10.chr # "\n" # "\x0a" # Output Carriage-Return Disposition
+ OPT_NAOHTS = 11.chr # "\v" # "\x0b" # Output Horizontal Tab Stops
+ OPT_NAOHTD = 12.chr # "\f" # "\x0c" # Output Horizontal Tab Disposition
+ OPT_NAOFFD = 13.chr # "\r" # "\x0d" # Output Formfeed Disposition
+ OPT_NAOVTS = 14.chr # "\016" # "\x0e" # Output Vertical Tabstops
+ OPT_NAOVTD = 15.chr # "\017" # "\x0f" # Output Vertical Tab Disposition
+ OPT_NAOLFD = 16.chr # "\020" # "\x10" # Output Linefeed Disposition
+ OPT_XASCII = 17.chr # "\021" # "\x11" # Extended ASCII
+ OPT_LOGOUT = 18.chr # "\022" # "\x12" # Logout
+ OPT_BM = 19.chr # "\023" # "\x13" # Byte Macro
+ OPT_DET = 20.chr # "\024" # "\x14" # Data Entry Terminal
+ OPT_SUPDUP = 21.chr # "\025" # "\x15" # SUPDUP
+ OPT_SUPDUPOUTPUT = 22.chr # "\026" # "\x16" # SUPDUP Output
+ OPT_SNDLOC = 23.chr # "\027" # "\x17" # Send Location
+ OPT_TTYPE = 24.chr # "\030" # "\x18" # Terminal Type
+ OPT_EOR = 25.chr # "\031" # "\x19" # End of Record
+ OPT_TUID = 26.chr # "\032" # "\x1a" # TACACS User Identification
+ OPT_OUTMRK = 27.chr # "\e" # "\x1b" # Output Marking
+ OPT_TTYLOC = 28.chr # "\034" # "\x1c" # Terminal Location Number
+ OPT_3270REGIME = 29.chr # "\035" # "\x1d" # Telnet 3270 Regime
+ OPT_X3PAD = 30.chr # "\036" # "\x1e" # X.3 PAD
+ OPT_NAWS = 31.chr # "\037" # "\x1f" # Negotiate About Window Size
+ OPT_TSPEED = 32.chr # " " # "\x20" # Terminal Speed
+ OPT_LFLOW = 33.chr # "!" # "\x21" # Remote Flow Control
+ OPT_LINEMODE = 34.chr # "\"" # "\x22" # Linemode
+ OPT_XDISPLOC = 35.chr # "#" # "\x23" # X Display Location
+ OPT_OLD_ENVIRON = 36.chr # "$" # "\x24" # Environment Option
+ OPT_AUTHENTICATION = 37.chr # "%" # "\x25" # Authentication Option
+ OPT_ENCRYPT = 38.chr # "&" # "\x26" # Encryption Option
+ OPT_NEW_ENVIRON = 39.chr # "'" # "\x27" # New Environment Option
+ OPT_EXOPL = 255.chr # "\377" # "\xff" # Extended-Options-List
+
+ NULL = "\000"
+ CR = "\015"
+ LF = "\012"
+ EOL = CR + LF
+ REVISION = '$Id: telnet.rb 17387 2008-06-17 14:04:48Z jeg2 $'
+ # :startdoc:
+
+ #
+ # Creates a new Net::Telnet object.
+ #
+ # Attempts to connect to the host (unless the Proxy option is
+ # provided: see below). If a block is provided, it is yielded
+ # status messages on the attempt to connect to the server, of
+ # the form:
+ #
+ # Trying localhost...
+ # Connected to localhost.
+ #
+ # +options+ is a hash of options. The following example lists
+ # all options and their default values.
+ #
+ # host = Net::Telnet::new(
+ # "Host" => "localhost", # default: "localhost"
+ # "Port" => 23, # default: 23
+ # "Binmode" => false, # default: false
+ # "Output_log" => "output_log", # default: nil (no output)
+ # "Dump_log" => "dump_log", # default: nil (no output)
+ # "Prompt" => /[$%#>] \z/n, # default: /[$%#>] \z/n
+ # "Telnetmode" => true, # default: true
+ # "Timeout" => 10, # default: 10
+ # # if ignore timeout then set "Timeout" to false.
+ # "Waittime" => 0, # default: 0
+ # "Proxy" => proxy # default: nil
+ # # proxy is Net::Telnet or IO object
+ # )
+ #
+ # The options have the following meanings:
+ #
+ # Host:: the hostname or IP address of the host to connect to, as a String.
+ # Defaults to "localhost".
+ #
+ # Port:: the port to connect to. Defaults to 23.
+ #
+ # Binmode:: if false (the default), newline substitution is performed.
+ # Outgoing LF is
+ # converted to CRLF, and incoming CRLF is converted to LF. If
+ # true, this substitution is not performed. This value can
+ # also be set with the #binmode() method. The
+ # outgoing conversion only applies to the #puts() and #print()
+ # methods, not the #write() method. The precise nature of
+ # the newline conversion is also affected by the telnet options
+ # SGA and BIN.
+ #
+ # Output_log:: the name of the file to write connection status messages
+ # and all received traffic to. In the case of a proper
+ # Telnet session, this will include the client input as
+ # echoed by the host; otherwise, it only includes server
+ # responses. Output is appended verbatim to this file.
+ # By default, no output log is kept.
+ #
+ # Dump_log:: as for Output_log, except that output is written in hexdump
+ # format (16 bytes per line as hex pairs, followed by their
+ # printable equivalent), with connection status messages
+ # preceded by '#', sent traffic preceded by '>', and
+ # received traffic preceded by '<'. By default, not dump log
+ # is kept.
+ #
+ # Prompt:: a regular expression matching the host's command-line prompt
+ # sequence. This is needed by the Telnet class to determine
+ # when the output from a command has finished and the host is
+ # ready to receive a new command. By default, this regular
+ # expression is /[$%#>] \z/n.
+ #
+ # Telnetmode:: a boolean value, true by default. In telnet mode,
+ # traffic received from the host is parsed for special
+ # command sequences, and these sequences are escaped
+ # in outgoing traffic sent using #puts() or #print()
+ # (but not #write()). If you are using the Net::Telnet
+ # object to connect to a non-telnet service (such as
+ # SMTP or POP), this should be set to "false" to prevent
+ # undesired data corruption. This value can also be set
+ # by the #telnetmode() method.
+ #
+ # Timeout:: the number of seconds to wait before timing out both the
+ # initial attempt to connect to host (in this constructor),
+ # and all attempts to read data from the host (in #waitfor(),
+ # #cmd(), and #login()). Exceeding this timeout causes a
+ # TimeoutError to be raised. The default value is 10 seconds.
+ # You can disable the timeout by setting this value to false.
+ # In this case, the connect attempt will eventually timeout
+ # on the underlying connect(2) socket call with an
+ # Errno::ETIMEDOUT error (but generally only after a few
+ # minutes), but other attempts to read data from the host
+ # will hand indefinitely if no data is forthcoming.
+ #
+ # Waittime:: the amount of time to wait after seeing what looks like a
+ # prompt (that is, received data that matches the Prompt
+ # option regular expression) to see if more data arrives.
+ # If more data does arrive in this time, Net::Telnet assumes
+ # that what it saw was not really a prompt. This is to try to
+ # avoid false matches, but it can also lead to missing real
+ # prompts (if, for instance, a background process writes to
+ # the terminal soon after the prompt is displayed). By
+ # default, set to 0, meaning not to wait for more data.
+ #
+ # Proxy:: a proxy object to used instead of opening a direct connection
+ # to the host. Must be either another Net::Telnet object or
+ # an IO object. If it is another Net::Telnet object, this
+ # instance will use that one's socket for communication. If an
+ # IO object, it is used directly for communication. Any other
+ # kind of object will cause an error to be raised.
+ #
+ def initialize(options) # :yield: mesg
+ @options = options
+ @options["Host"] = "localhost" unless @options.has_key?("Host")
+ @options["Port"] = 23 unless @options.has_key?("Port")
+ @options["Prompt"] = /[$%#>] \z/n unless @options.has_key?("Prompt")
+ @options["Timeout"] = 10 unless @options.has_key?("Timeout")
+ @options["Waittime"] = 0 unless @options.has_key?("Waittime")
+ unless @options.has_key?("Binmode")
+ @options["Binmode"] = false
+ else
+ unless (true == @options["Binmode"] or false == @options["Binmode"])
+ raise ArgumentError, "Binmode option must be true or false"
+ end
+ end
+
+ unless @options.has_key?("Telnetmode")
+ @options["Telnetmode"] = true
+ else
+ unless (true == @options["Telnetmode"] or false == @options["Telnetmode"])
+ raise ArgumentError, "Telnetmode option must be true or false"
+ end
+ end
+
+ @telnet_option = { "SGA" => false, "BINARY" => false }
+
+ if @options.has_key?("Output_log")
+ @log = File.open(@options["Output_log"], 'a+')
+ @log.sync = true
+ @log.binmode
+ end
+
+ if @options.has_key?("Dump_log")
+ @dumplog = File.open(@options["Dump_log"], 'a+')
+ @dumplog.sync = true
+ @dumplog.binmode
+ def @dumplog.log_dump(dir, x) # :nodoc:
+ len = x.length
+ addr = 0
+ offset = 0
+ while 0 < len
+ if len < 16
+ line = x[offset, len]
+ else
+ line = x[offset, 16]
+ end
+ hexvals = line.unpack('H*')[0]
+ hexvals += ' ' * (32 - hexvals.length)
+ hexvals = format("%s %s %s %s " * 4, *hexvals.unpack('a2' * 16))
+ line = line.gsub(/[\000-\037\177-\377]/n, '.')
+ printf "%s 0x%5.5x: %s%s\n", dir, addr, hexvals, line
+ addr += 16
+ offset += 16
+ len -= 16
+ end
+ print "\n"
+ end
+ end
+
+ if @options.has_key?("Proxy")
+ if @options["Proxy"].kind_of?(Net::Telnet)
+ @sock = @options["Proxy"].sock
+ elsif @options["Proxy"].kind_of?(IO)
+ @sock = @options["Proxy"]
+ else
+ raise "Error: Proxy must be an instance of Net::Telnet or IO."
+ end
+ else
+ message = "Trying " + @options["Host"] + "...\n"
+ yield(message) if block_given?
+ @log.write(message) if @options.has_key?("Output_log")
+ @dumplog.log_dump('#', message) if @options.has_key?("Dump_log")
+
+ begin
+ if @options["Timeout"] == false
+ @sock = TCPSocket.open(@options["Host"], @options["Port"])
+ else
+ timeout(@options["Timeout"]) do
+ @sock = TCPSocket.open(@options["Host"], @options["Port"])
+ end
+ end
+ rescue TimeoutError
+ raise TimeoutError, "timed out while opening a connection to the host"
+ rescue
+ @log.write($ERROR_INFO.to_s + "\n") if @options.has_key?("Output_log")
+ @dumplog.log_dump('#', $ERROR_INFO.to_s + "\n") if @options.has_key?("Dump_log")
+ raise
+ end
+ @sock.sync = true
+ @sock.binmode
+
+ message = "Connected to " + @options["Host"] + ".\n"
+ yield(message) if block_given?
+ @log.write(message) if @options.has_key?("Output_log")
+ @dumplog.log_dump('#', message) if @options.has_key?("Dump_log")
+ end
+
+ super(@sock)
+ end # initialize
+
+ # The socket the Telnet object is using. Note that this object becomes
+ # a delegate of the Telnet object, so normally you invoke its methods
+ # directly on the Telnet object.
+ attr :sock
+
+ # Set telnet command interpretation on (+mode+ == true) or off
+ # (+mode+ == false), or return the current value (+mode+ not
+ # provided). It should be on for true telnet sessions, off if
+ # using Net::Telnet to connect to a non-telnet service such
+ # as SMTP.
+ def telnetmode(mode = nil)
+ case mode
+ when nil
+ @options["Telnetmode"]
+ when true, false
+ @options["Telnetmode"] = mode
+ else
+ raise ArgumentError, "argument must be true or false, or missing"
+ end
+ end
+
+ # Turn telnet command interpretation on (true) or off (false). It
+ # should be on for true telnet sessions, off if using Net::Telnet
+ # to connect to a non-telnet service such as SMTP.
+ def telnetmode=(mode)
+ if (true == mode or false == mode)
+ @options["Telnetmode"] = mode
+ else
+ raise ArgumentError, "argument must be true or false"
+ end
+ end
+
+ # Turn newline conversion on (+mode+ == false) or off (+mode+ == true),
+ # or return the current value (+mode+ is not specified).
+ def binmode(mode = nil)
+ case mode
+ when nil
+ @options["Binmode"]
+ when true, false
+ @options["Binmode"] = mode
+ else
+ raise ArgumentError, "argument must be true or false"
+ end
+ end
+
+ # Turn newline conversion on (false) or off (true).
+ def binmode=(mode)
+ if (true == mode or false == mode)
+ @options["Binmode"] = mode
+ else
+ raise ArgumentError, "argument must be true or false"
+ end
+ end
+
+ # Preprocess received data from the host.
+ #
+ # Performs newline conversion and detects telnet command sequences.
+ # Called automatically by #waitfor(). You should only use this
+ # method yourself if you have read input directly using sysread()
+ # or similar, and even then only if in telnet mode.
+ def preprocess(string)
+ # combine CR+NULL into CR
+ string = string.gsub(/#{CR}#{NULL}/no, CR) if @options["Telnetmode"]
+
+ # combine EOL into "\n"
+ string = string.gsub(/#{EOL}/no, "\n") unless @options["Binmode"]
+
+ # remove NULL
+ string = string.gsub(/#{NULL}/no, '') unless @options["Binmode"]
+
+ string.gsub(/#{IAC}(
+ [#{IAC}#{AO}#{AYT}#{DM}#{IP}#{NOP}]|
+ [#{DO}#{DONT}#{WILL}#{WONT}]
+ [#{OPT_BINARY}-#{OPT_NEW_ENVIRON}#{OPT_EXOPL}]|
+ #{SB}[^#{IAC}]*#{IAC}#{SE}
+ )/xno) do
+ if IAC == $1 # handle escaped IAC characters
+ IAC
+ elsif AYT == $1 # respond to "IAC AYT" (are you there)
+ self.write("nobody here but us pigeons" + EOL)
+ ''
+ elsif DO[0] == $1[0] # respond to "IAC DO x"
+ if OPT_BINARY[0] == $1[1]
+ @telnet_option["BINARY"] = true
+ self.write(IAC + WILL + OPT_BINARY)
+ else
+ self.write(IAC + WONT + $1[1..1])
+ end
+ ''
+ elsif DONT[0] == $1[0] # respond to "IAC DON'T x" with "IAC WON'T x"
+ self.write(IAC + WONT + $1[1..1])
+ ''
+ elsif WILL[0] == $1[0] # respond to "IAC WILL x"
+ if OPT_BINARY[0] == $1[1]
+ self.write(IAC + DO + OPT_BINARY)
+ elsif OPT_ECHO[0] == $1[1]
+ self.write(IAC + DO + OPT_ECHO)
+ elsif OPT_SGA[0] == $1[1]
+ @telnet_option["SGA"] = true
+ self.write(IAC + DO + OPT_SGA)
+ else
+ self.write(IAC + DONT + $1[1..1])
+ end
+ ''
+ elsif WONT[0] == $1[0] # respond to "IAC WON'T x"
+ if OPT_ECHO[0] == $1[1]
+ self.write(IAC + DONT + OPT_ECHO)
+ elsif OPT_SGA[0] == $1[1]
+ @telnet_option["SGA"] = false
+ self.write(IAC + DONT + OPT_SGA)
+ else
+ self.write(IAC + DONT + $1[1..1])
+ end
+ ''
+ else
+ ''
+ end
+ end
+ end # preprocess
+
+ # Read data from the host until a certain sequence is matched.
+ #
+ # If a block is given, the received data will be yielded as it
+ # is read in (not necessarily all in one go), or nil if EOF
+ # occurs before any data is received. Whether a block is given
+ # or not, all data read will be returned in a single string, or again
+ # nil if EOF occurs before any data is received. Note that
+ # received data includes the matched sequence we were looking for.
+ #
+ # +options+ can be either a regular expression or a hash of options.
+ # If a regular expression, this specifies the data to wait for.
+ # If a hash, this can specify the following options:
+ #
+ # Match:: a regular expression, specifying the data to wait for.
+ # Prompt:: as for Match; used only if Match is not specified.
+ # String:: as for Match, except a string that will be converted
+ # into a regular expression. Used only if Match and
+ # Prompt are not specified.
+ # Timeout:: the number of seconds to wait for data from the host
+ # before raising a TimeoutError. If set to false,
+ # no timeout will occur. If not specified, the
+ # Timeout option value specified when this instance
+ # was created will be used, or, failing that, the
+ # default value of 10 seconds.
+ # Waittime:: the number of seconds to wait after matching against
+ # the input data to see if more data arrives. If more
+ # data arrives within this time, we will judge ourselves
+ # not to have matched successfully, and will continue
+ # trying to match. If not specified, the Waittime option
+ # value specified when this instance was created will be
+ # used, or, failing that, the default value of 0 seconds,
+ # which means not to wait for more input.
+ # FailEOF:: if true, when the remote end closes the connection then an
+ # EOFError will be raised. Otherwise, defaults to the old
+ # behaviour that the function will return whatever data
+ # has been received already, or nil if nothing was received.
+ #
+ def waitfor(options) # :yield: recvdata
+ time_out = @options["Timeout"]
+ waittime = @options["Waittime"]
+ fail_eof = @options["FailEOF"]
+
+ if options.kind_of?(Hash)
+ prompt = if options.has_key?("Match")
+ options["Match"]
+ elsif options.has_key?("Prompt")
+ options["Prompt"]
+ elsif options.has_key?("String")
+ Regexp.new( Regexp.quote(options["String"]) )
+ end
+ time_out = options["Timeout"] if options.has_key?("Timeout")
+ waittime = options["Waittime"] if options.has_key?("Waittime")
+ fail_eof = options["FailEOF"] if options.has_key?("FailEOF")
+ else
+ prompt = options
+ end
+
+ if time_out == false
+ time_out = nil
+ end
+
+ line = ''
+ buf = ''
+ rest = ''
+ until(prompt === line and not IO::select([@sock], nil, nil, waittime))
+ unless IO::select([@sock], nil, nil, time_out)
+ raise TimeoutError, "timed out while waiting for more data"
+ end
+ begin
+ c = @sock.readpartial(1024 * 1024)
+ @dumplog.log_dump('<', c) if @options.has_key?("Dump_log")
+ if @options["Telnetmode"]
+ c = rest + c
+ if Integer(c.rindex(/#{IAC}#{SE}/no) || 0) <
+ Integer(c.rindex(/#{IAC}#{SB}/no) || 0)
+ buf = preprocess(c[0 ... c.rindex(/#{IAC}#{SB}/no)])
+ rest = c[c.rindex(/#{IAC}#{SB}/no) .. -1]
+ elsif pt = c.rindex(/#{IAC}[^#{IAC}#{AO}#{AYT}#{DM}#{IP}#{NOP}]?\z/no) ||
+ c.rindex(/\r\z/no)
+ buf = preprocess(c[0 ... pt])
+ rest = c[pt .. -1]
+ else
+ buf = preprocess(c)
+ rest = ''
+ end
+ else
+ # Not Telnetmode.
+ #
+ # We cannot use preprocess() on this data, because that
+ # method makes some Telnetmode-specific assumptions.
+ buf = rest + c
+ rest = ''
+ unless @options["Binmode"]
+ if pt = buf.rindex(/\r\z/no)
+ buf = buf[0 ... pt]
+ rest = buf[pt .. -1]
+ end
+ buf.gsub!(/#{EOL}/no, "\n")
+ end
+ end
+ @log.print(buf) if @options.has_key?("Output_log")
+ line += buf
+ yield buf if block_given?
+ rescue EOFError # End of file reached
+ raise if fail_eof
+ if line == ''
+ line = nil
+ yield nil if block_given?
+ end
+ break
+ end
+ end
+ line
+ end
+
+ # Write +string+ to the host.
+ #
+ # Does not perform any conversions on +string+. Will log +string+ to the
+ # dumplog, if the Dump_log option is set.
+ def write(string)
+ length = string.length
+ while 0 < length
+ IO::select(nil, [@sock])
+ @dumplog.log_dump('>', string[-length..-1]) if @options.has_key?("Dump_log")
+ length -= @sock.syswrite(string[-length..-1])
+ end
+ end
+
+ # Sends a string to the host.
+ #
+ # This does _not_ automatically append a newline to the string. Embedded
+ # newlines may be converted and telnet command sequences escaped
+ # depending upon the values of telnetmode, binmode, and telnet options
+ # set by the host.
+ def print(string)
+ string = string.gsub(/#{IAC}/no, IAC + IAC) if @options["Telnetmode"]
+
+ if @options["Binmode"]
+ self.write(string)
+ else
+ if @telnet_option["BINARY"] and @telnet_option["SGA"]
+ # IAC WILL SGA IAC DO BIN send EOL --> CR
+ self.write(string.gsub(/\n/n, CR))
+ elsif @telnet_option["SGA"]
+ # IAC WILL SGA send EOL --> CR+NULL
+ self.write(string.gsub(/\n/n, CR + NULL))
+ else
+ # NONE send EOL --> CR+LF
+ self.write(string.gsub(/\n/n, EOL))
+ end
+ end
+ end
+
+ # Sends a string to the host.
+ #
+ # Same as #print(), but appends a newline to the string.
+ def puts(string)
+ self.print(string + "\n")
+ end
+
+ # Send a command to the host.
+ #
+ # More exactly, sends a string to the host, and reads in all received
+ # data until is sees the prompt or other matched sequence.
+ #
+ # If a block is given, the received data will be yielded to it as
+ # it is read in. Whether a block is given or not, the received data
+ # will be return as a string. Note that the received data includes
+ # the prompt and in most cases the host's echo of our command.
+ #
+ # +options+ is either a String, specified the string or command to
+ # send to the host; or it is a hash of options. If a hash, the
+ # following options can be specified:
+ #
+ # String:: the command or other string to send to the host.
+ # Match:: a regular expression, the sequence to look for in
+ # the received data before returning. If not specified,
+ # the Prompt option value specified when this instance
+ # was created will be used, or, failing that, the default
+ # prompt of /[$%#>] \z/n.
+ # Timeout:: the seconds to wait for data from the host before raising
+ # a Timeout error. If not specified, the Timeout option
+ # value specified when this instance was created will be
+ # used, or, failing that, the default value of 10 seconds.
+ #
+ # The command or other string will have the newline sequence appended
+ # to it.
+ def cmd(options) # :yield: recvdata
+ match = @options["Prompt"]
+ time_out = @options["Timeout"]
+
+ if options.kind_of?(Hash)
+ string = options["String"]
+ match = options["Match"] if options.has_key?("Match")
+ time_out = options["Timeout"] if options.has_key?("Timeout")
+ else
+ string = options
+ end
+
+ self.puts(string)
+ if block_given?
+ waitfor({"Prompt" => match, "Timeout" => time_out}){|c| yield c }
+ else
+ waitfor({"Prompt" => match, "Timeout" => time_out})
+ end
+ end
+
+ # Login to the host with a given username and password.
+ #
+ # The username and password can either be provided as two string
+ # arguments in that order, or as a hash with keys "Name" and
+ # "Password".
+ #
+ # This method looks for the strings "login" and "Password" from the
+ # host to determine when to send the username and password. If the
+ # login sequence does not follow this pattern (for instance, you
+ # are connecting to a service other than telnet), you will need
+ # to handle login yourself.
+ #
+ # The password can be omitted, either by only
+ # provided one String argument, which will be used as the username,
+ # or by providing a has that has no "Password" key. In this case,
+ # the method will not look for the "Password:" prompt; if it is
+ # sent, it will have to be dealt with by later calls.
+ #
+ # The method returns all data received during the login process from
+ # the host, including the echoed username but not the password (which
+ # the host should not echo). If a block is passed in, this received
+ # data is also yielded to the block as it is received.
+ def login(options, password = nil) # :yield: recvdata
+ login_prompt = /[Ll]ogin[: ]*\z/n
+ password_prompt = /[Pp]ass(?:word|phrase)[: ]*\z/n
+ if options.kind_of?(Hash)
+ username = options["Name"]
+ password = options["Password"]
+ login_prompt = options["LoginPrompt"] if options["LoginPrompt"]
+ password_prompt = options["PasswordPrompt"] if options["PasswordPrompt"]
+ else
+ username = options
+ end
+
+ if block_given?
+ line = waitfor(login_prompt){|c| yield c }
+ if password
+ line += cmd({"String" => username,
+ "Match" => password_prompt}){|c| yield c }
+ line += cmd(password){|c| yield c }
+ else
+ line += cmd(username){|c| yield c }
+ end
+ else
+ line = waitfor(login_prompt)
+ if password
+ line += cmd({"String" => username,
+ "Match" => password_prompt})
+ line += cmd(password)
+ else
+ line += cmd(username)
+ end
+ end
+ line
+ end
+
+ end # class Telnet
+end # module Net
+
diff --git a/ruby/lib/observer.rb b/ruby/lib/observer.rb
new file mode 100644
index 0000000..472a154
--- /dev/null
+++ b/ruby/lib/observer.rb
@@ -0,0 +1,193 @@
+#
+# observer.rb implements the _Observer_ object-oriented design pattern. The
+# following documentation is copied, with modifications, from "Programming
+# Ruby", by Hunt and Thomas; http://www.rubycentral.com/book/lib_patterns.html.
+#
+# == About
+#
+# The Observer pattern, also known as Publish/Subscribe, provides a simple
+# mechanism for one object to inform a set of interested third-party objects
+# when its state changes.
+#
+# == Mechanism
+#
+# In the Ruby implementation, the notifying class mixes in the +Observable+
+# module, which provides the methods for managing the associated observer
+# objects.
+#
+# The observers must implement the +update+ method to receive notifications.
+#
+# The observable object must:
+# * assert that it has +changed+
+# * call +notify_observers+
+#
+# == Example
+#
+# The following example demonstrates this nicely. A +Ticker+, when run,
+# continually receives the stock +Price+ for its +@symbol+. A +Warner+ is a
+# general observer of the price, and two warners are demonstrated, a +WarnLow+
+# and a +WarnHigh+, which print a warning if the price is below or above their
+# set limits, respectively.
+#
+# The +update+ callback allows the warners to run without being explicitly
+# called. The system is set up with the +Ticker+ and several observers, and the
+# observers do their duty without the top-level code having to interfere.
+#
+# Note that the contract between publisher and subscriber (observable and
+# observer) is not declared or enforced. The +Ticker+ publishes a time and a
+# price, and the warners receive that. But if you don't ensure that your
+# contracts are correct, nothing else can warn you.
+#
+# require "observer"
+#
+# class Ticker ### Periodically fetch a stock price.
+# include Observable
+#
+# def initialize(symbol)
+# @symbol = symbol
+# end
+#
+# def run
+# lastPrice = nil
+# loop do
+# price = Price.fetch(@symbol)
+# print "Current price: #{price}\n"
+# if price != lastPrice
+# changed # notify observers
+# lastPrice = price
+# notify_observers(Time.now, price)
+# end
+# sleep 1
+# end
+# end
+# end
+#
+# class Price ### A mock class to fetch a stock price (60 - 140).
+# def Price.fetch(symbol)
+# 60 + rand(80)
+# end
+# end
+#
+# class Warner ### An abstract observer of Ticker objects.
+# def initialize(ticker, limit)
+# @limit = limit
+# ticker.add_observer(self)
+# end
+# end
+#
+# class WarnLow < Warner
+# def update(time, price) # callback for observer
+# if price < @limit
+# print "--- #{time.to_s}: Price below #@limit: #{price}\n"
+# end
+# end
+# end
+#
+# class WarnHigh < Warner
+# def update(time, price) # callback for observer
+# if price > @limit
+# print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
+# end
+# end
+# end
+#
+# ticker = Ticker.new("MSFT")
+# WarnLow.new(ticker, 80)
+# WarnHigh.new(ticker, 120)
+# ticker.run
+#
+# Produces:
+#
+# Current price: 83
+# Current price: 75
+# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75
+# Current price: 90
+# Current price: 134
+# +++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134
+# Current price: 134
+# Current price: 112
+# Current price: 79
+# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
+
+
+#
+# Implements the Observable design pattern as a mixin so that other objects can
+# be notified of changes in state. See observer.rb for details and an example.
+#
+module Observable
+
+ #
+ # Add +observer+ as an observer on this object. +observer+ will now receive
+ # notifications. The second optional argument specifies a method to notify
+ # updates, of which default value is +update+.
+ #
+ def add_observer(observer, func=:update)
+ @observer_peers = {} unless defined? @observer_peers
+ unless observer.respond_to? func
+ raise NoMethodError, "observer does not respond to `#{func.to_s}'"
+ end
+ @observer_peers[observer] = func
+ end
+
+ #
+ # Delete +observer+ as an observer on this object. It will no longer receive
+ # notifications.
+ #
+ def delete_observer(observer)
+ @observer_peers.delete observer if defined? @observer_peers
+ end
+
+ #
+ # Delete all observers associated with this object.
+ #
+ def delete_observers
+ @observer_peers.clear if defined? @observer_peers
+ end
+
+ #
+ # Return the number of observers associated with this object.
+ #
+ def count_observers
+ if defined? @observer_peers
+ @observer_peers.size
+ else
+ 0
+ end
+ end
+
+ #
+ # Set the changed state of this object. Notifications will be sent only if
+ # the changed +state+ is +true+.
+ #
+ def changed(state=true)
+ @observer_state = state
+ end
+
+ #
+ # Query the changed state of this object.
+ #
+ def changed?
+ if defined? @observer_state and @observer_state
+ true
+ else
+ false
+ end
+ end
+
+ #
+ # If this object's changed state is +true+, invoke the update method in each
+ # currently associated observer in turn, passing it the given arguments. The
+ # changed state is then set to +false+.
+ #
+ def notify_observers(*arg)
+ if defined? @observer_state and @observer_state
+ if defined? @observer_peers
+ @observer_peers.each { |k, v|
+ k.send v, *arg
+ }
+ end
+ @observer_state = false
+ end
+ end
+
+end
diff --git a/ruby/lib/open-uri.rb b/ruby/lib/open-uri.rb
new file mode 100644
index 0000000..b426455
--- /dev/null
+++ b/ruby/lib/open-uri.rb
@@ -0,0 +1,832 @@
+require 'uri'
+require 'stringio'
+require 'time'
+
+module Kernel
+ private
+ alias open_uri_original_open open # :nodoc:
+ class << self
+ alias open_uri_original_open open # :nodoc:
+ end
+
+ # makes possible to open various resources including URIs.
+ # If the first argument respond to `open' method,
+ # the method is called with the rest arguments.
+ #
+ # If the first argument is a string which begins with xxx://,
+ # it is parsed by URI.parse. If the parsed object respond to `open' method,
+ # the method is called with the rest arguments.
+ #
+ # Otherwise original open is called.
+ #
+ # Since open-uri.rb provides URI::HTTP#open, URI::HTTPS#open and
+ # URI::FTP#open,
+ # Kernel[#.]open can accepts such URIs and strings which begins with
+ # http://, https:// and ftp://.
+ # In these case, the opened file object is extended by OpenURI::Meta.
+ def open(name, *rest, &block) # :doc:
+ if name.respond_to?(:open)
+ name.open(*rest, &block)
+ elsif name.respond_to?(:to_str) &&
+ %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
+ (uri = URI.parse(name)).respond_to?(:open)
+ uri.open(*rest, &block)
+ else
+ open_uri_original_open(name, *rest, &block)
+ end
+ end
+ module_function :open
+end
+
+# OpenURI is an easy-to-use wrapper for net/http, net/https and net/ftp.
+#
+#== Example
+#
+# It is possible to open http/https/ftp URL as usual like opening a file:
+#
+# open("http://www.ruby-lang.org/") {|f|
+# f.each_line {|line| p line}
+# }
+#
+# The opened file has several methods for meta information as follows since
+# it is extended by OpenURI::Meta.
+#
+# open("http://www.ruby-lang.org/en") {|f|
+# f.each_line {|line| p line}
+# p f.base_uri # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
+# p f.content_type # "text/html"
+# p f.charset # "iso-8859-1"
+# p f.content_encoding # []
+# p f.last_modified # Thu Dec 05 02:45:02 UTC 2002
+# }
+#
+# Additional header fields can be specified by an optional hash argument.
+#
+# open("http://www.ruby-lang.org/en/",
+# "User-Agent" => "Ruby/#{RUBY_VERSION}",
+# "From" => "foo@bar.invalid",
+# "Referer" => "http://www.ruby-lang.org/") {|f|
+# # ...
+# }
+#
+# The environment variables such as http_proxy, https_proxy and ftp_proxy
+# are in effect by default. :proxy => nil disables proxy.
+#
+# open("http://www.ruby-lang.org/en/raa.html", :proxy => nil) {|f|
+# # ...
+# }
+#
+# URI objects can be opened in a similar way.
+#
+# uri = URI.parse("http://www.ruby-lang.org/en/")
+# uri.open {|f|
+# # ...
+# }
+#
+# URI objects can be read directly. The returned string is also extended by
+# OpenURI::Meta.
+#
+# str = uri.read
+# p str.base_uri
+#
+# Author:: Tanaka Akira <akr@m17n.org>
+
+module OpenURI
+ Options = {
+ :proxy => true,
+ :proxy_http_basic_authentication => true,
+ :progress_proc => true,
+ :content_length_proc => true,
+ :http_basic_authentication => true,
+ :read_timeout => true,
+ :ssl_ca_cert => nil,
+ :ssl_verify_mode => nil,
+ :ftp_active_mode => false,
+ :redirect => true,
+ }
+
+ def OpenURI.check_options(options) # :nodoc:
+ options.each {|k, v|
+ next unless Symbol === k
+ unless Options.include? k
+ raise ArgumentError, "unrecognized option: #{k}"
+ end
+ }
+ end
+
+ def OpenURI.scan_open_optional_arguments(*rest) # :nodoc:
+ if !rest.empty? && (String === rest.first || Integer === rest.first)
+ mode = rest.shift
+ if !rest.empty? && Integer === rest.first
+ perm = rest.shift
+ end
+ end
+ return mode, perm, rest
+ end
+
+ def OpenURI.open_uri(name, *rest) # :nodoc:
+ uri = URI::Generic === name ? name : URI.parse(name)
+ mode, perm, rest = OpenURI.scan_open_optional_arguments(*rest)
+ options = rest.shift if !rest.empty? && Hash === rest.first
+ raise ArgumentError.new("extra arguments") if !rest.empty?
+ options ||= {}
+ OpenURI.check_options(options)
+
+ if /\Arb?(?:\Z|:([^:]+))/ =~ mode
+ encoding, = $1,Encoding.find($1) if $1
+ mode = nil
+ end
+
+ unless mode == nil ||
+ mode == 'r' || mode == 'rb' ||
+ mode == File::RDONLY
+ raise ArgumentError.new("invalid access mode #{mode} (#{uri.class} resource is read only.)")
+ end
+
+ io = open_loop(uri, options)
+ io.set_encoding(encoding) if encoding
+ if block_given?
+ begin
+ yield io
+ ensure
+ io.close
+ end
+ else
+ io
+ end
+ end
+
+ def OpenURI.open_loop(uri, options) # :nodoc:
+ proxy_opts = []
+ proxy_opts << :proxy_http_basic_authentication if options.include? :proxy_http_basic_authentication
+ proxy_opts << :proxy if options.include? :proxy
+ proxy_opts.compact!
+ if 1 < proxy_opts.length
+ raise ArgumentError, "multiple proxy options specified"
+ end
+ case proxy_opts.first
+ when :proxy_http_basic_authentication
+ opt_proxy, proxy_user, proxy_pass = options.fetch(:proxy_http_basic_authentication)
+ proxy_user = proxy_user.to_str
+ proxy_pass = proxy_pass.to_str
+ if opt_proxy == true
+ raise ArgumentError.new("Invalid authenticated proxy option: #{options[:proxy_http_basic_authentication].inspect}")
+ end
+ when :proxy
+ opt_proxy = options.fetch(:proxy)
+ proxy_user = nil
+ proxy_pass = nil
+ when nil
+ opt_proxy = true
+ proxy_user = nil
+ proxy_pass = nil
+ end
+ case opt_proxy
+ when true
+ find_proxy = lambda {|u| pxy = u.find_proxy; pxy ? [pxy, nil, nil] : nil}
+ when nil, false
+ find_proxy = lambda {|u| nil}
+ when String
+ opt_proxy = URI.parse(opt_proxy)
+ find_proxy = lambda {|u| [opt_proxy, proxy_user, proxy_pass]}
+ when URI::Generic
+ find_proxy = lambda {|u| [opt_proxy, proxy_user, proxy_pass]}
+ else
+ raise ArgumentError.new("Invalid proxy option: #{opt_proxy}")
+ end
+
+ uri_set = {}
+ buf = nil
+ while true
+ redirect = catch(:open_uri_redirect) {
+ buf = Buffer.new
+ uri.buffer_open(buf, find_proxy.call(uri), options)
+ nil
+ }
+ if redirect
+ if redirect.relative?
+ # Although it violates RFC2616, Location: field may have relative
+ # URI. It is converted to absolute URI using uri as a base URI.
+ redirect = uri + redirect
+ end
+ if !options.fetch(:redirect, true)
+ raise HTTPRedirect.new(buf.io.status.join(' '), buf.io, redirect)
+ end
+ unless OpenURI.redirectable?(uri, redirect)
+ raise "redirection forbidden: #{uri} -> #{redirect}"
+ end
+ if options.include? :http_basic_authentication
+ # send authentication only for the URI directly specified.
+ options = options.dup
+ options.delete :http_basic_authentication
+ end
+ uri = redirect
+ raise "HTTP redirection loop: #{uri}" if uri_set.include? uri.to_s
+ uri_set[uri.to_s] = true
+ else
+ break
+ end
+ end
+ io = buf.io
+ io.base_uri = uri
+ io
+ end
+
+ def OpenURI.redirectable?(uri1, uri2) # :nodoc:
+ # This test is intended to forbid a redirection from http://... to
+ # file:///etc/passwd.
+ # https to http redirect is also forbidden intentionally.
+ # It avoids sending secure cookie or referer by non-secure HTTP protocol.
+ # (RFC 2109 4.3.1, RFC 2965 3.3, RFC 2616 15.1.3)
+ # However this is ad hoc. It should be extensible/configurable.
+ uri1.scheme.downcase == uri2.scheme.downcase ||
+ (/\A(?:http|ftp)\z/i =~ uri1.scheme && /\A(?:http|ftp)\z/i =~ uri2.scheme)
+ end
+
+ def OpenURI.open_http(buf, target, proxy, options) # :nodoc:
+ if proxy
+ proxy_uri, proxy_user, proxy_pass = proxy
+ raise "Non-HTTP proxy URI: #{proxy_uri}" if proxy_uri.class != URI::HTTP
+ end
+
+ if target.userinfo && "1.9.0" <= RUBY_VERSION
+ # don't raise for 1.8 because compatibility.
+ raise ArgumentError, "userinfo not supported. [RFC3986]"
+ end
+
+ header = {}
+ options.each {|k, v| header[k] = v if String === k }
+
+ require 'net/http'
+ klass = Net::HTTP
+ if URI::HTTP === target
+ # HTTP or HTTPS
+ if proxy
+ if proxy_user && proxy_pass
+ klass = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_user, proxy_pass)
+ else
+ klass = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port)
+ end
+ end
+ target_host = target.host
+ target_port = target.port
+ request_uri = target.request_uri
+ else
+ # FTP over HTTP proxy
+ target_host = proxy_uri.host
+ target_port = proxy_uri.port
+ request_uri = target.to_s
+ if proxy_user && proxy_pass
+ header["Proxy-Authorization"] = 'Basic ' + ["#{proxy_user}:#{proxy_pass}"].pack('m').delete("\r\n")
+ end
+ end
+
+ http = klass.new(target_host, target_port)
+ if target.class == URI::HTTPS
+ require 'net/https'
+ http.use_ssl = true
+ http.verify_mode = options[:ssl_verify_mode] || OpenSSL::SSL::VERIFY_PEER
+ store = OpenSSL::X509::Store.new
+ if options[:ssl_ca_cert]
+ if File.directory? options[:ssl_ca_cert]
+ store.add_path options[:ssl_ca_cert]
+ else
+ store.add_file options[:ssl_ca_cert]
+ end
+ else
+ store.set_default_paths
+ end
+ http.cert_store = store
+ end
+ if options.include? :read_timeout
+ http.read_timeout = options[:read_timeout]
+ end
+
+ resp = nil
+ http.start {
+ req = Net::HTTP::Get.new(request_uri, header)
+ if options.include? :http_basic_authentication
+ user, pass = options[:http_basic_authentication]
+ req.basic_auth user, pass
+ end
+ http.request(req) {|response|
+ resp = response
+ if options[:content_length_proc] && Net::HTTPSuccess === resp
+ if resp.key?('Content-Length')
+ options[:content_length_proc].call(resp['Content-Length'].to_i)
+ else
+ options[:content_length_proc].call(nil)
+ end
+ end
+ resp.read_body {|str|
+ buf << str
+ if options[:progress_proc] && Net::HTTPSuccess === resp
+ options[:progress_proc].call(buf.size)
+ end
+ }
+ }
+ }
+ io = buf.io
+ io.rewind
+ io.status = [resp.code, resp.message]
+ resp.each {|name,value| buf.io.meta_add_field name, value }
+ case resp
+ when Net::HTTPSuccess
+ when Net::HTTPMovedPermanently, # 301
+ Net::HTTPFound, # 302
+ Net::HTTPSeeOther, # 303
+ Net::HTTPTemporaryRedirect # 307
+ begin
+ loc_uri = URI.parse(resp['location'])
+ rescue URI::InvalidURIError
+ raise OpenURI::HTTPError.new(io.status.join(' ') + ' (Invalid Location URI)', io)
+ end
+ throw :open_uri_redirect, loc_uri
+ else
+ raise OpenURI::HTTPError.new(io.status.join(' '), io)
+ end
+ end
+
+ class HTTPError < StandardError
+ def initialize(message, io)
+ super(message)
+ @io = io
+ end
+ attr_reader :io
+ end
+
+ class HTTPRedirect < HTTPError
+ def initialize(message, io, uri)
+ super(message, io)
+ @uri = uri
+ end
+ attr_reader :uri
+ end
+
+ class Buffer # :nodoc:
+ def initialize
+ @io = StringIO.new
+ @size = 0
+ end
+ attr_reader :size
+
+ StringMax = 10240
+ def <<(str)
+ @io << str
+ @size += str.length
+ if StringIO === @io && StringMax < @size
+ require 'tempfile'
+ io = Tempfile.new('open-uri')
+ io.binmode
+ Meta.init io, @io if Meta === @io
+ io << @io.string
+ @io = io
+ end
+ end
+
+ def io
+ Meta.init @io unless Meta === @io
+ @io
+ end
+ end
+
+ # Mixin for holding meta-information.
+ module Meta
+ def Meta.init(obj, src=nil) # :nodoc:
+ obj.extend Meta
+ obj.instance_eval {
+ @base_uri = nil
+ @meta = {}
+ }
+ if src
+ obj.status = src.status
+ obj.base_uri = src.base_uri
+ src.meta.each {|name, value|
+ obj.meta_add_field(name, value)
+ }
+ end
+ end
+
+ # returns an Array which consists status code and message.
+ attr_accessor :status
+
+ # returns a URI which is base of relative URIs in the data.
+ # It may differ from the URI supplied by a user because redirection.
+ attr_accessor :base_uri
+
+ # returns a Hash which represents header fields.
+ # The Hash keys are downcased for canonicalization.
+ attr_reader :meta
+
+ def meta_setup_encoding # :nodoc:
+ charset = self.charset
+ enc = nil
+ if charset
+ begin
+ enc = Encoding.find(charset)
+ rescue ArgumentError
+ end
+ end
+ enc = Encoding::ASCII_8BIT unless enc
+ if self.respond_to? :force_encoding
+ self.force_encoding(enc)
+ elsif self.respond_to? :string
+ self.string.force_encoding(enc)
+ else # Tempfile
+ self.set_encoding enc
+ end
+ end
+
+ def meta_add_field(name, value) # :nodoc:
+ name = name.downcase
+ @meta[name] = value
+ meta_setup_encoding if name == 'content-type'
+ end
+
+ # returns a Time which represents Last-Modified field.
+ def last_modified
+ if v = @meta['last-modified']
+ Time.httpdate(v)
+ else
+ nil
+ end
+ end
+
+ RE_LWS = /[\r\n\t ]+/n
+ RE_TOKEN = %r{[^\x00- ()<>@,;:\\"/\[\]?={}\x7f]+}n
+ RE_QUOTED_STRING = %r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])*"}n
+ RE_PARAMETERS = %r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
+
+ def content_type_parse # :nodoc:
+ v = @meta['content-type']
+ # The last (?:;#{RE_LWS}?)? matches extra ";" which violates RFC2045.
+ if v && %r{\A#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?/(#{RE_TOKEN})#{RE_LWS}?(#{RE_PARAMETERS})(?:;#{RE_LWS}?)?\z}no =~ v
+ type = $1.downcase
+ subtype = $2.downcase
+ parameters = []
+ $3.scan(/;#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?=#{RE_LWS}?(?:(#{RE_TOKEN})|(#{RE_QUOTED_STRING}))/no) {|att, val, qval|
+ val = qval.gsub(/[\r\n\t !#-\[\]-~\x80-\xff]+|(\\[\x00-\x7f])/n) { $1 ? $1[1,1] : $& } if qval
+ parameters << [att.downcase, val]
+ }
+ ["#{type}/#{subtype}", *parameters]
+ else
+ nil
+ end
+ end
+
+ # returns "type/subtype" which is MIME Content-Type.
+ # It is downcased for canonicalization.
+ # Content-Type parameters are stripped.
+ def content_type
+ type, *parameters = content_type_parse
+ type || 'application/octet-stream'
+ end
+
+ # returns a charset parameter in Content-Type field.
+ # It is downcased for canonicalization.
+ #
+ # If charset parameter is not given but a block is given,
+ # the block is called and its result is returned.
+ # It can be used to guess charset.
+ #
+ # If charset parameter and block is not given,
+ # nil is returned except text type in HTTP.
+ # In that case, "iso-8859-1" is returned as defined by RFC2616 3.7.1.
+ def charset
+ type, *parameters = content_type_parse
+ if pair = parameters.assoc('charset')
+ pair.last.downcase
+ elsif block_given?
+ yield
+ elsif type && %r{\Atext/} =~ type &&
+ @base_uri && /\Ahttp\z/i =~ @base_uri.scheme
+ "iso-8859-1" # RFC2616 3.7.1
+ else
+ nil
+ end
+ end
+
+ # returns a list of encodings in Content-Encoding field
+ # as an Array of String.
+ # The encodings are downcased for canonicalization.
+ def content_encoding
+ v = @meta['content-encoding']
+ if v && %r{\A#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?(?:,#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?)*}o =~ v
+ v.scan(RE_TOKEN).map {|content_coding| content_coding.downcase}
+ else
+ []
+ end
+ end
+ end
+
+ # Mixin for HTTP and FTP URIs.
+ module OpenRead
+ # OpenURI::OpenRead#open provides `open' for URI::HTTP and URI::FTP.
+ #
+ # OpenURI::OpenRead#open takes optional 3 arguments as:
+ # OpenURI::OpenRead#open([mode [, perm]] [, options]) [{|io| ... }]
+ #
+ # `mode', `perm' is same as Kernel#open.
+ #
+ # However, `mode' must be read mode because OpenURI::OpenRead#open doesn't
+ # support write mode (yet).
+ # Also `perm' is just ignored because it is meaningful only for file
+ # creation.
+ #
+ # `options' must be a hash.
+ #
+ # Each pairs which key is a string in the hash specify a extra header
+ # field for HTTP.
+ # I.e. it is ignored for FTP without HTTP proxy.
+ #
+ # The hash may include other options which key is a symbol:
+ #
+ # [:proxy]
+ # Synopsis:
+ # :proxy => "http://proxy.foo.com:8000/"
+ # :proxy => URI.parse("http://proxy.foo.com:8000/")
+ # :proxy => true
+ # :proxy => false
+ # :proxy => nil
+ #
+ # If :proxy option is specified, the value should be String, URI,
+ # boolean or nil.
+ # When String or URI is given, it is treated as proxy URI.
+ # When true is given or the option itself is not specified,
+ # environment variable `scheme_proxy' is examined.
+ # `scheme' is replaced by `http', `https' or `ftp'.
+ # When false or nil is given, the environment variables are ignored and
+ # connection will be made to a server directly.
+ #
+ # [:proxy_http_basic_authentication]
+ # Synopsis:
+ # :proxy_http_basic_authentication => ["http://proxy.foo.com:8000/", "proxy-user", "proxy-password"]
+ # :proxy_http_basic_authentication => [URI.parse("http://proxy.foo.com:8000/"), "proxy-user", "proxy-password"]
+ #
+ # If :proxy option is specified, the value should be an Array with 3 elements.
+ # It should contain a proxy URI, a proxy user name and a proxy password.
+ # The proxy URI should be a String, an URI or nil.
+ # The proxy user name and password should be a String.
+ #
+ # If nil is given for the proxy URI, this option is just ignored.
+ #
+ # If :proxy and :proxy_http_basic_authentication is specified,
+ # ArgumentError is raised.
+ #
+ # [:http_basic_authentication]
+ # Synopsis:
+ # :http_basic_authentication=>[user, password]
+ #
+ # If :http_basic_authentication is specified,
+ # the value should be an array which contains 2 strings:
+ # username and password.
+ # It is used for HTTP Basic authentication defined by RFC 2617.
+ #
+ # [:content_length_proc]
+ # Synopsis:
+ # :content_length_proc => lambda {|content_length| ... }
+ #
+ # If :content_length_proc option is specified, the option value procedure
+ # is called before actual transfer is started.
+ # It takes one argument which is expected content length in bytes.
+ #
+ # If two or more transfer is done by HTTP redirection, the procedure
+ # is called only one for a last transfer.
+ #
+ # When expected content length is unknown, the procedure is called with
+ # nil.
+ # It is happen when HTTP response has no Content-Length header.
+ #
+ # [:progress_proc]
+ # Synopsis:
+ # :progress_proc => lambda {|size| ...}
+ #
+ # If :progress_proc option is specified, the proc is called with one
+ # argument each time when `open' gets content fragment from network.
+ # The argument `size' `size' is a accumulated transfered size in bytes.
+ #
+ # If two or more transfer is done by HTTP redirection, the procedure
+ # is called only one for a last transfer.
+ #
+ # :progress_proc and :content_length_proc are intended to be used for
+ # progress bar.
+ # For example, it can be implemented as follows using Ruby/ProgressBar.
+ #
+ # pbar = nil
+ # open("http://...",
+ # :content_length_proc => lambda {|t|
+ # if t && 0 < t
+ # pbar = ProgressBar.new("...", t)
+ # pbar.file_transfer_mode
+ # end
+ # },
+ # :progress_proc => lambda {|s|
+ # pbar.set s if pbar
+ # }) {|f| ... }
+ #
+ # [:read_timeout]
+ # Synopsis:
+ # :read_timeout=>nil (no timeout)
+ # :read_timeout=>10 (10 second)
+ #
+ # :read_timeout option specifies a timeout of read for http connections.
+ #
+ # [:ssl_ca_cert]
+ # Synopsis:
+ # :ssl_ca_cert=>filename
+ #
+ # :ssl_ca_cert is used to specify CA certificate for SSL.
+ # If it is given, default certificates are not used.
+ #
+ # [:ssl_verify_mode]
+ # Synopsis:
+ # :ssl_verify_mode=>mode
+ #
+ # :ssl_verify_mode is used to specify openssl verify mode.
+ #
+ # OpenURI::OpenRead#open returns an IO like object if block is not given.
+ # Otherwise it yields the IO object and return the value of the block.
+ # The IO object is extended with OpenURI::Meta.
+ #
+ # [:ftp_active_mode]
+ # Synopsis:
+ # :ftp_active_mode=>bool
+ #
+ # :ftp_active_mode=>true is used to make ftp active mode.
+ # Note that the active mode is default in Ruby 1.8 or prior.
+ # Ruby 1.9 uses passive mode by default.
+ #
+ # [:redirect]
+ # Synopsis:
+ # :redirect=>bool
+ #
+ # :redirect=>false is used to disable HTTP redirects at all.
+ # OpenURI::HTTPRedirect exception raised on redirection.
+ # It is true by default.
+ # The true means redirections between http and ftp is permitted.
+ #
+ def open(*rest, &block)
+ OpenURI.open_uri(self, *rest, &block)
+ end
+
+ # OpenURI::OpenRead#read([options]) reads a content referenced by self and
+ # returns the content as string.
+ # The string is extended with OpenURI::Meta.
+ # The argument `options' is same as OpenURI::OpenRead#open.
+ def read(options={})
+ self.open(options) {|f|
+ str = f.read
+ Meta.init str, f
+ str
+ }
+ end
+ end
+end
+
+module URI
+ class Generic
+ # returns a proxy URI.
+ # The proxy URI is obtained from environment variables such as http_proxy,
+ # ftp_proxy, no_proxy, etc.
+ # If there is no proper proxy, nil is returned.
+ #
+ # Note that capitalized variables (HTTP_PROXY, FTP_PROXY, NO_PROXY, etc.)
+ # are examined too.
+ #
+ # But http_proxy and HTTP_PROXY is treated specially under CGI environment.
+ # It's because HTTP_PROXY may be set by Proxy: header.
+ # So HTTP_PROXY is not used.
+ # http_proxy is not used too if the variable is case insensitive.
+ # CGI_HTTP_PROXY can be used instead.
+ def find_proxy
+ name = self.scheme.downcase + '_proxy'
+ proxy_uri = nil
+ if name == 'http_proxy' && ENV.include?('REQUEST_METHOD') # CGI?
+ # HTTP_PROXY conflicts with *_proxy for proxy settings and
+ # HTTP_* for header information in CGI.
+ # So it should be careful to use it.
+ pairs = ENV.reject {|k, v| /\Ahttp_proxy\z/i !~ k }
+ case pairs.length
+ when 0 # no proxy setting anyway.
+ proxy_uri = nil
+ when 1
+ k, v = pairs.shift
+ if k == 'http_proxy' && ENV[k.upcase] == nil
+ # http_proxy is safe to use because ENV is case sensitive.
+ proxy_uri = ENV[name]
+ else
+ proxy_uri = nil
+ end
+ else # http_proxy is safe to use because ENV is case sensitive.
+ proxy_uri = ENV.to_hash[name]
+ end
+ if !proxy_uri
+ # Use CGI_HTTP_PROXY. cf. libwww-perl.
+ proxy_uri = ENV["CGI_#{name.upcase}"]
+ end
+ elsif name == 'http_proxy'
+ unless proxy_uri = ENV[name]
+ if proxy_uri = ENV[name.upcase]
+ warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.'
+ end
+ end
+ else
+ proxy_uri = ENV[name] || ENV[name.upcase]
+ end
+
+ if proxy_uri && self.host
+ require 'socket'
+ begin
+ addr = IPSocket.getaddress(self.host)
+ proxy_uri = nil if /\A127\.|\A::1\z/ =~ addr
+ rescue SocketError
+ end
+ end
+
+ if proxy_uri
+ proxy_uri = URI.parse(proxy_uri)
+ name = 'no_proxy'
+ if no_proxy = ENV[name] || ENV[name.upcase]
+ no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
+ if /(\A|\.)#{Regexp.quote host}\z/i =~ self.host &&
+ (!port || self.port == port.to_i)
+ proxy_uri = nil
+ break
+ end
+ }
+ end
+ proxy_uri
+ else
+ nil
+ end
+ end
+ end
+
+ class HTTP
+ def buffer_open(buf, proxy, options) # :nodoc:
+ OpenURI.open_http(buf, self, proxy, options)
+ end
+
+ include OpenURI::OpenRead
+ end
+
+ class FTP
+ def buffer_open(buf, proxy, options) # :nodoc:
+ if proxy
+ OpenURI.open_http(buf, self, proxy, options)
+ return
+ end
+ require 'net/ftp'
+
+ path = self.path
+ path = path.sub(%r{\A/}, '%2F') # re-encode the beginning slash because uri library decodes it.
+ directories = path.split(%r{/}, -1)
+ directories.each {|d|
+ d.gsub!(/%([0-9A-Fa-f][0-9A-Fa-f])/) { [$1].pack("H2") }
+ }
+ unless filename = directories.pop
+ raise ArgumentError, "no filename: #{self.inspect}"
+ end
+ directories.each {|d|
+ if /[\r\n]/ =~ d
+ raise ArgumentError, "invalid directory: #{d.inspect}"
+ end
+ }
+ if /[\r\n]/ =~ filename
+ raise ArgumentError, "invalid filename: #{filename.inspect}"
+ end
+ typecode = self.typecode
+ if typecode && /\A[aid]\z/ !~ typecode
+ raise ArgumentError, "invalid typecode: #{typecode.inspect}"
+ end
+
+ # The access sequence is defined by RFC 1738
+ ftp = Net::FTP.new
+ ftp.connect(self.host, self.port)
+ ftp.passive = true if !options[:ftp_active_mode]
+ # todo: extract user/passwd from .netrc.
+ user = 'anonymous'
+ passwd = nil
+ user, passwd = self.userinfo.split(/:/) if self.userinfo
+ ftp.login(user, passwd)
+ directories.each {|cwd|
+ ftp.voidcmd("CWD #{cwd}")
+ }
+ if typecode
+ # xxx: typecode D is not handled.
+ ftp.voidcmd("TYPE #{typecode.upcase}")
+ end
+ if options[:content_length_proc]
+ options[:content_length_proc].call(ftp.size(filename))
+ end
+ ftp.retrbinary("RETR #{filename}", 4096) { |str|
+ buf << str
+ options[:progress_proc].call(buf.size) if options[:progress_proc]
+ }
+ ftp.close
+ buf.io.rewind
+ end
+
+ include OpenURI::OpenRead
+ end
+end
diff --git a/ruby/lib/open3.rb b/ruby/lib/open3.rb
new file mode 100644
index 0000000..d776de7
--- /dev/null
+++ b/ruby/lib/open3.rb
@@ -0,0 +1,98 @@
+#
+# = open3.rb: Popen, but with stderr, too
+#
+# Author:: Yukihiro Matsumoto
+# Documentation:: Konrad Meyer
+#
+# Open3 gives you access to stdin, stdout, and stderr when running other
+# programs.
+#
+
+#
+# Open3 grants you access to stdin, stdout, stderr and a thread to wait the
+# child process when running another program.
+#
+# Example:
+#
+# require "open3"
+# include Open3
+#
+# stdin, stdout, stderr, wait_thr = popen3('nroff -man')
+#
+# Open3.popen3 can also take a block which will receive stdin, stdout,
+# stderr and wait_thr as parameters.
+# This ensures stdin, stdout and stderr are closed and
+# the process is terminated once the block exits.
+#
+# Example:
+#
+# require "open3"
+#
+# Open3.popen3('nroff -man') { |stdin, stdout, stderr, wait_thr| ... }
+#
+
+module Open3
+ #
+ # Open stdin, stdout, and stderr streams and start external executable.
+ # In addition, a thread for waiting the started process is noticed.
+ # The thread has a thread variable :pid which is the pid of the started
+ # process.
+ #
+ # Non-block form:
+ #
+ # stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
+ # pid = wait_thr[:pid] # pid of the started process.
+ # ...
+ # stdin.close # stdin, stdout and stderr should be closed in this form.
+ # stdout.close
+ # stderr.close
+ # exit_status = wait_thr.value # Process::Status object returned.
+ #
+ # Block form:
+ #
+ # Open3.popen3(cmd) { |stdin, stdout, stderr, wait_thr| ... }
+ #
+ # The parameter +cmd+ is passed directly to Kernel#spawn.
+ #
+ # wait_thr.value waits the termination of the process.
+ # The block form also waits the process when it returns.
+ #
+ # Closing stdin, stdout and stderr does not wait the process.
+ #
+ def popen3(*cmd)
+ pw = IO::pipe # pipe[0] for read, pipe[1] for write
+ pr = IO::pipe
+ pe = IO::pipe
+
+ pid = spawn(*cmd, STDIN=>pw[0], STDOUT=>pr[1], STDERR=>pe[1])
+ wait_thr = Process.detach(pid)
+ pw[0].close
+ pr[1].close
+ pe[1].close
+ pi = [pw[1], pr[0], pe[0], wait_thr]
+ pw[1].sync = true
+ if defined? yield
+ begin
+ return yield(*pi)
+ ensure
+ [pw[1], pr[0], pe[0]].each{|p| p.close unless p.closed?}
+ wait_thr.join
+ end
+ end
+ pi
+ end
+ module_function :popen3
+end
+
+if $0 == __FILE__
+ a = Open3.popen3("nroff -man")
+ Thread.start do
+ while line = gets
+ a[0].print line
+ end
+ a[0].close
+ end
+ while line = a[1].gets
+ print ":", line
+ end
+end
diff --git a/ruby/lib/optparse.rb b/ruby/lib/optparse.rb
new file mode 100644
index 0000000..2fe3c3b
--- /dev/null
+++ b/ruby/lib/optparse.rb
@@ -0,0 +1,1810 @@
+#
+# optparse.rb - command-line option analysis with the OptionParser class.
+#
+# Author:: Nobu Nakada
+# Documentation:: Nobu Nakada and Gavin Sinclair.
+#
+# See OptionParser for documentation.
+#
+
+
+# == Developer Documentation (not for RDoc output)
+#
+# === Class tree
+#
+# - OptionParser:: front end
+# - OptionParser::Switch:: each switches
+# - OptionParser::List:: options list
+# - OptionParser::ParseError:: errors on parsing
+# - OptionParser::AmbiguousOption
+# - OptionParser::NeedlessArgument
+# - OptionParser::MissingArgument
+# - OptionParser::InvalidOption
+# - OptionParser::InvalidArgument
+# - OptionParser::AmbiguousArgument
+#
+# === Object relationship diagram
+#
+# +--------------+
+# | OptionParser |<>-----+
+# +--------------+ | +--------+
+# | ,-| Switch |
+# on_head -------->+---------------+ / +--------+
+# accept/reject -->| List |<|>-
+# | |<|>- +----------+
+# on ------------->+---------------+ `-| argument |
+# : : | class |
+# +---------------+ |==========|
+# on_tail -------->| | |pattern |
+# +---------------+ |----------|
+# OptionParser.accept ->| DefaultList | |converter |
+# reject |(shared between| +----------+
+# | all instances)|
+# +---------------+
+#
+# == OptionParser
+#
+# === Introduction
+#
+# OptionParser is a class for command-line option analysis. It is much more
+# advanced, yet also easier to use, than GetoptLong, and is a more Ruby-oriented
+# solution.
+#
+# === Features
+#
+# 1. The argument specification and the code to handle it are written in the
+# same place.
+# 2. It can output an option summary; you don't need to maintain this string
+# separately.
+# 3. Optional and mandatory arguments are specified very gracefully.
+# 4. Arguments can be automatically converted to a specified class.
+# 5. Arguments can be restricted to a certain set.
+#
+# All of these features are demonstrated in the examples below.
+#
+# === Minimal example
+#
+# require 'optparse'
+#
+# options = {}
+# OptionParser.new do |opts|
+# opts.banner = "Usage: example.rb [options]"
+#
+# opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
+# options[:verbose] = v
+# end
+# end.parse!
+#
+# p options
+# p ARGV
+#
+# === Complete example
+#
+# The following example is a complete Ruby program. You can run it and see the
+# effect of specifying various options. This is probably the best way to learn
+# the features of +optparse+.
+#
+# require 'optparse'
+# require 'optparse/time'
+# require 'ostruct'
+# require 'pp'
+#
+# class OptparseExample
+#
+# CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
+# CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
+#
+# #
+# # Return a structure describing the options.
+# #
+# def self.parse(args)
+# # The options specified on the command line will be collected in *options*.
+# # We set default values here.
+# options = OpenStruct.new
+# options.library = []
+# options.inplace = false
+# options.encoding = "utf8"
+# options.transfer_type = :auto
+# options.verbose = false
+#
+# opts = OptionParser.new do |opts|
+# opts.banner = "Usage: example.rb [options]"
+#
+# opts.separator ""
+# opts.separator "Specific options:"
+#
+# # Mandatory argument.
+# opts.on("-r", "--require LIBRARY",
+# "Require the LIBRARY before executing your script") do |lib|
+# options.library << lib
+# end
+#
+# # Optional argument; multi-line description.
+# opts.on("-i", "--inplace [EXTENSION]",
+# "Edit ARGV files in place",
+# " (make backup if EXTENSION supplied)") do |ext|
+# options.inplace = true
+# options.extension = ext || ''
+# options.extension.sub!(/\A\.?(?=.)/, ".") # Ensure extension begins with dot.
+# end
+#
+# # Cast 'delay' argument to a Float.
+# opts.on("--delay N", Float, "Delay N seconds before executing") do |n|
+# options.delay = n
+# end
+#
+# # Cast 'time' argument to a Time object.
+# opts.on("-t", "--time [TIME]", Time, "Begin execution at given time") do |time|
+# options.time = time
+# end
+#
+# # Cast to octal integer.
+# opts.on("-F", "--irs [OCTAL]", OptionParser::OctalInteger,
+# "Specify record separator (default \\0)") do |rs|
+# options.record_separator = rs
+# end
+#
+# # List of arguments.
+# opts.on("--list x,y,z", Array, "Example 'list' of arguments") do |list|
+# options.list = list
+# end
+#
+# # Keyword completion. We are specifying a specific set of arguments (CODES
+# # and CODE_ALIASES - notice the latter is a Hash), and the user may provide
+# # the shortest unambiguous text.
+# code_list = (CODE_ALIASES.keys + CODES).join(',')
+# opts.on("--code CODE", CODES, CODE_ALIASES, "Select encoding",
+# " (#{code_list})") do |encoding|
+# options.encoding = encoding
+# end
+#
+# # Optional argument with keyword completion.
+# opts.on("--type [TYPE]", [:text, :binary, :auto],
+# "Select transfer type (text, binary, auto)") do |t|
+# options.transfer_type = t
+# end
+#
+# # Boolean switch.
+# opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
+# options.verbose = v
+# end
+#
+# opts.separator ""
+# opts.separator "Common options:"
+#
+# # No argument, shows at tail. This will print an options summary.
+# # Try it and see!
+# opts.on_tail("-h", "--help", "Show this message") do
+# puts opts
+# exit
+# end
+#
+# # Another typical switch to print the version.
+# opts.on_tail("--version", "Show version") do
+# puts OptionParser::Version.join('.')
+# exit
+# end
+# end
+#
+# opts.parse!(args)
+# options
+# end # parse()
+#
+# end # class OptparseExample
+#
+# options = OptparseExample.parse(ARGV)
+# pp options
+#
+# === Further documentation
+#
+# The above examples should be enough to learn how to use this class. If you
+# have any questions, email me (gsinclair@soyabean.com.au) and I will update
+# this document.
+#
+class OptionParser
+ # :stopdoc:
+ RCSID = %w$Id: optparse.rb 23396 2009-05-11 15:05:43Z yugui $[1..-1].each {|s| s.freeze}.freeze
+ Version = (RCSID[1].split('.').collect {|s| s.to_i}.extend(Comparable).freeze if RCSID[1])
+ LastModified = (Time.gm(*RCSID[2, 2].join('-').scan(/\d+/).collect {|s| s.to_i}) if RCSID[2])
+ Release = RCSID[2]
+
+ NoArgument = [NO_ARGUMENT = :NONE, nil].freeze
+ RequiredArgument = [REQUIRED_ARGUMENT = :REQUIRED, true].freeze
+ OptionalArgument = [OPTIONAL_ARGUMENT = :OPTIONAL, false].freeze
+ # :startdoc:
+
+ #
+ # Keyword completion module. This allows partial arguments to be specified
+ # and resolved against a list of acceptable values.
+ #
+ module Completion
+ def complete(key, icase = false, pat = nil)
+ pat ||= Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'),
+ icase)
+ canon, sw, cn = nil
+ candidates = []
+ each do |k, *v|
+ (if Regexp === k
+ kn = nil
+ k === key
+ else
+ kn = defined?(k.id2name) ? k.id2name : k
+ pat === kn
+ end) or next
+ v << k if v.empty?
+ candidates << [k, v, kn]
+ end
+ candidates = candidates.sort_by {|k, v, kn| kn.size}
+ if candidates.size == 1
+ canon, sw, * = candidates[0]
+ elsif candidates.size > 1
+ canon, sw, cn = candidates.shift
+ candidates.each do |k, v, kn|
+ next if sw == v
+ if String === cn and String === kn
+ if cn.rindex(kn, 0)
+ canon, sw, cn = k, v, kn
+ next
+ elsif kn.rindex(cn, 0)
+ next
+ end
+ end
+ throw :ambiguous, key
+ end
+ end
+ if canon
+ block_given? or return key, *sw
+ yield(key, *sw)
+ end
+ end
+
+ def convert(opt = nil, val = nil, *)
+ val
+ end
+ end
+
+
+ #
+ # Map from option/keyword string to object with completion.
+ #
+ class OptionMap < Hash
+ include Completion
+ end
+
+
+ #
+ # Individual switch class. Not important to the user.
+ #
+ # Defined within Switch are several Switch-derived classes: NoArgument,
+ # RequiredArgument, etc.
+ #
+ class Switch
+ attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block
+
+ #
+ # Guesses argument style from +arg+. Returns corresponding
+ # OptionParser::Switch class (OptionalArgument, etc.).
+ #
+ def self.guess(arg)
+ case arg
+ when ""
+ t = self
+ when /\A=?\[/
+ t = Switch::OptionalArgument
+ when /\A\s+\[/
+ t = Switch::PlacedArgument
+ else
+ t = Switch::RequiredArgument
+ end
+ self >= t or incompatible_argument_styles(arg, t)
+ t
+ end
+
+ def self.incompatible_argument_styles(arg, t)
+ raise(ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}",
+ ParseError.filter_backtrace(caller(2)))
+ end
+
+ def self.pattern
+ NilClass
+ end
+
+ def initialize(pattern = nil, conv = nil,
+ short = nil, long = nil, arg = nil,
+ desc = ([] if short or long), block = Proc.new)
+ raise if Array === pattern
+ @pattern, @conv, @short, @long, @arg, @desc, @block =
+ pattern, conv, short, long, arg, desc, block
+ end
+
+ #
+ # Parses +arg+ and returns rest of +arg+ and matched portion to the
+ # argument pattern. Yields when the pattern doesn't match substring.
+ #
+ def parse_arg(arg)
+ pattern or return nil, [arg]
+ unless m = pattern.match(arg)
+ yield(InvalidArgument, arg)
+ return arg, []
+ end
+ if String === m
+ m = [s = m]
+ else
+ m = m.to_a
+ s = m[0]
+ return nil, m unless String === s
+ end
+ raise InvalidArgument, arg unless arg.rindex(s, 0)
+ return nil, m if s.length == arg.length
+ yield(InvalidArgument, arg) # didn't match whole arg
+ return arg[s.length..-1], m
+ end
+ private :parse_arg
+
+ #
+ # Parses argument, converts and returns +arg+, +block+ and result of
+ # conversion. Yields at semi-error condition instead of raising an
+ # exception.
+ #
+ def conv_arg(arg, val = [])
+ if conv
+ val = conv.call(*val)
+ else
+ val = proc {|v| v}.call(*val)
+ end
+ return arg, block, val
+ end
+ private :conv_arg
+
+ #
+ # Produces the summary text. Each line of the summary is yielded to the
+ # block (without newline).
+ #
+ # +sdone+:: Already summarized short style options keyed hash.
+ # +ldone+:: Already summarized long style options keyed hash.
+ # +width+:: Width of left side (option part). In other words, the right
+ # side (description part) starts after +width+ columns.
+ # +max+:: Maximum width of left side -> the options are filled within
+ # +max+ columns.
+ # +indent+:: Prefix string indents all summarized lines.
+ #
+ def summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "")
+ sopts, lopts = [], [], nil
+ @short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short
+ @long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long
+ return if sopts.empty? and lopts.empty? # completely hidden
+
+ left = [sopts.join(', ')]
+ right = desc.dup
+
+ while s = lopts.shift
+ l = left[-1].length + s.length
+ l += arg.length if left.size == 1 && arg
+ l < max or sopts.empty? or left << ''
+ left[-1] << if left[-1].empty? then ' ' * 4 else ', ' end << s
+ end
+
+ left[0] << arg if arg
+ mlen = left.collect {|ss| ss.length}.max.to_i
+ while mlen > width and l = left.shift
+ mlen = left.collect {|ss| ss.length}.max.to_i if l.length == mlen
+ yield(indent + l)
+ end
+
+ while begin l = left.shift; r = right.shift; l or r end
+ l = l.to_s.ljust(width) + ' ' + r if r and !r.empty?
+ yield(indent + l)
+ end
+
+ self
+ end
+
+ def add_banner(to) # :nodoc:
+ unless @short or @long
+ s = desc.join
+ to << " [" + s + "]..." unless s.empty?
+ end
+ to
+ end
+
+ def match_nonswitch?(str) # :nodoc:
+ @pattern =~ str unless @short or @long
+ end
+
+ #
+ # Main name of the switch.
+ #
+ def switch_name
+ (long.first || short.first).sub(/\A-+(?:\[no-\])?/, '')
+ end
+
+ #
+ # Switch that takes no arguments.
+ #
+ class NoArgument < self
+
+ #
+ # Raises an exception if any arguments given.
+ #
+ def parse(arg, argv)
+ yield(NeedlessArgument, arg) if arg
+ conv_arg(arg)
+ end
+
+ def self.incompatible_argument_styles(*)
+ end
+
+ def self.pattern
+ Object
+ end
+ end
+
+ #
+ # Switch that takes an argument.
+ #
+ class RequiredArgument < self
+
+ #
+ # Raises an exception if argument is not present.
+ #
+ def parse(arg, argv)
+ unless arg
+ raise MissingArgument if argv.empty?
+ arg = argv.shift
+ end
+ conv_arg(*parse_arg(arg, &method(:raise)))
+ end
+ end
+
+ #
+ # Switch that can omit argument.
+ #
+ class OptionalArgument < self
+
+ #
+ # Parses argument if given, or uses default value.
+ #
+ def parse(arg, argv, &error)
+ if arg
+ conv_arg(*parse_arg(arg, &error))
+ else
+ conv_arg(arg)
+ end
+ end
+ end
+
+ #
+ # Switch that takes an argument, which does not begin with '-'.
+ #
+ class PlacedArgument < self
+
+ #
+ # Returns nil if argument is not present or begins with '-'.
+ #
+ def parse(arg, argv, &error)
+ if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
+ return nil, block, nil
+ end
+ opt = (val = parse_arg(val, &error))[1]
+ val = conv_arg(*val)
+ if opt and !arg
+ argv.shift
+ else
+ val[0] = nil
+ end
+ val
+ end
+ end
+ end
+
+ #
+ # Simple option list providing mapping from short and/or long option
+ # string to OptionParser::Switch and mapping from acceptable argument to
+ # matching pattern and converter pair. Also provides summary feature.
+ #
+ class List
+ # Map from acceptable argument types to pattern and converter pairs.
+ attr_reader :atype
+
+ # Map from short style option switches to actual switch objects.
+ attr_reader :short
+
+ # Map from long style option switches to actual switch objects.
+ attr_reader :long
+
+ # List of all switches and summary string.
+ attr_reader :list
+
+ #
+ # Just initializes all instance variables.
+ #
+ def initialize
+ @atype = {}
+ @short = OptionMap.new
+ @long = OptionMap.new
+ @list = []
+ end
+
+ #
+ # See OptionParser.accept.
+ #
+ def accept(t, pat = /.*/nm, &block)
+ if pat
+ pat.respond_to?(:match) or
+ raise TypeError, "has no `match'", ParseError.filter_backtrace(caller(2))
+ else
+ pat = t if t.respond_to?(:match)
+ end
+ unless block
+ block = pat.method(:convert).to_proc if pat.respond_to?(:convert)
+ end
+ @atype[t] = [pat, block]
+ end
+
+ #
+ # See OptionParser.reject.
+ #
+ def reject(t)
+ @atype.delete(t)
+ end
+
+ #
+ # Adds +sw+ according to +sopts+, +lopts+ and +nlopts+.
+ #
+ # +sw+:: OptionParser::Switch instance to be added.
+ # +sopts+:: Short style option list.
+ # +lopts+:: Long style option list.
+ # +nlopts+:: Negated long style options list.
+ #
+ def update(sw, sopts, lopts, nsw = nil, nlopts = nil)
+ sopts.each {|o| @short[o] = sw} if sopts
+ lopts.each {|o| @long[o] = sw} if lopts
+ nlopts.each {|o| @long[o] = nsw} if nsw and nlopts
+ used = @short.invert.update(@long.invert)
+ @list.delete_if {|o| Switch === o and !used[o]}
+ end
+ private :update
+
+ #
+ # Inserts +switch+ at the head of the list, and associates short, long
+ # and negated long options. Arguments are:
+ #
+ # +switch+:: OptionParser::Switch instance to be inserted.
+ # +short_opts+:: List of short style options.
+ # +long_opts+:: List of long style options.
+ # +nolong_opts+:: List of long style options with "no-" prefix.
+ #
+ # prepend(switch, short_opts, long_opts, nolong_opts)
+ #
+ def prepend(*args)
+ update(*args)
+ @list.unshift(args[0])
+ end
+
+ #
+ # Appends +switch+ at the tail of the list, and associates short, long
+ # and negated long options. Arguments are:
+ #
+ # +switch+:: OptionParser::Switch instance to be inserted.
+ # +short_opts+:: List of short style options.
+ # +long_opts+:: List of long style options.
+ # +nolong_opts+:: List of long style options with "no-" prefix.
+ #
+ # append(switch, short_opts, long_opts, nolong_opts)
+ #
+ def append(*args)
+ update(*args)
+ @list.push(args[0])
+ end
+
+ #
+ # Searches +key+ in +id+ list. The result is returned or yielded if a
+ # block is given. If it isn't found, nil is returned.
+ #
+ def search(id, key)
+ if list = __send__(id)
+ val = list.fetch(key) {return nil}
+ block_given? ? yield(val) : val
+ end
+ end
+
+ #
+ # Searches list +id+ for +opt+ and the optional patterns for completion
+ # +pat+. If +icase+ is true, the search is case insensitive. The result
+ # is returned or yielded if a block is given. If it isn't found, nil is
+ # returned.
+ #
+ def complete(id, opt, icase = false, *pat, &block)
+ __send__(id).complete(opt, icase, *pat, &block)
+ end
+
+ #
+ # Iterates over each option, passing the option to the +block+.
+ #
+ def each_option(&block)
+ list.each(&block)
+ end
+
+ #
+ # Creates the summary table, passing each line to the +block+ (without
+ # newline). The arguments +args+ are passed along to the summarize
+ # method which is called on every option.
+ #
+ def summarize(*args, &block)
+ sum = []
+ list.reverse_each do |opt|
+ if opt.respond_to?(:summarize) # perhaps OptionParser::Switch
+ s = []
+ opt.summarize(*args) {|l| s << l}
+ sum.concat(s.reverse)
+ elsif !opt or opt.empty?
+ sum << ""
+ elsif opt.respond_to?(:each_line)
+ sum.concat([*opt.each_line].reverse)
+ else
+ sum.concat([*opt.each].reverse)
+ end
+ end
+ sum.reverse_each(&block)
+ end
+
+ def add_banner(to) # :nodoc:
+ list.each do |opt|
+ if opt.respond_to?(:add_banner)
+ opt.add_banner(to)
+ end
+ end
+ to
+ end
+ end
+
+ #
+ # Hash with completion search feature. See OptionParser::Completion.
+ #
+ class CompletingHash < Hash
+ include Completion
+
+ #
+ # Completion for hash key.
+ #
+ def match(key)
+ *values = fetch(key) {
+ raise AmbiguousArgument, catch(:ambiguous) {return complete(key)}
+ }
+ return key, *values
+ end
+ end
+
+ # :stopdoc:
+
+ #
+ # Enumeration of acceptable argument styles. Possible values are:
+ #
+ # NO_ARGUMENT:: The switch takes no arguments. (:NONE)
+ # REQUIRED_ARGUMENT:: The switch requires an argument. (:REQUIRED)
+ # OPTIONAL_ARGUMENT:: The switch requires an optional argument. (:OPTIONAL)
+ #
+ # Use like --switch=argument (long style) or -Xargument (short style). For
+ # short style, only portion matched to argument pattern is dealed as
+ # argument.
+ #
+ ArgumentStyle = {}
+ NoArgument.each {|el| ArgumentStyle[el] = Switch::NoArgument}
+ RequiredArgument.each {|el| ArgumentStyle[el] = Switch::RequiredArgument}
+ OptionalArgument.each {|el| ArgumentStyle[el] = Switch::OptionalArgument}
+ ArgumentStyle.freeze
+
+ #
+ # Switches common used such as '--', and also provides default
+ # argument classes
+ #
+ DefaultList = List.new
+ DefaultList.short['-'] = Switch::NoArgument.new {}
+ DefaultList.long[''] = Switch::NoArgument.new {throw :terminate}
+
+ #
+ # Default options for ARGV, which never appear in option summary.
+ #
+ Officious = {}
+
+ #
+ # --help
+ # Shows option summary.
+ #
+ Officious['help'] = proc do |parser|
+ Switch::NoArgument.new do
+ puts parser.help
+ exit
+ end
+ end
+
+ #
+ # --version
+ # Shows version string if Version is defined.
+ #
+ Officious['version'] = proc do |parser|
+ Switch::OptionalArgument.new do |pkg|
+ if pkg
+ begin
+ require 'optparse/version'
+ rescue LoadError
+ else
+ show_version(*pkg.split(/,/)) or
+ abort("#{parser.program_name}: no version found in package #{pkg}")
+ exit
+ end
+ end
+ v = parser.ver or abort("#{parser.program_name}: version unknown")
+ puts v
+ exit
+ end
+ end
+
+ # :startdoc:
+
+ #
+ # Class methods
+ #
+
+ #
+ # Initializes a new instance and evaluates the optional block in context
+ # of the instance. Arguments +args+ are passed to #new, see there for
+ # description of parameters.
+ #
+ # This method is *deprecated*, its behavior corresponds to the older #new
+ # method.
+ #
+ def self.with(*args, &block)
+ opts = new(*args)
+ opts.instance_eval(&block)
+ opts
+ end
+
+ #
+ # Returns an incremented value of +default+ according to +arg+.
+ #
+ def self.inc(arg, default = nil)
+ case arg
+ when Integer
+ arg.nonzero?
+ when nil
+ default.to_i + 1
+ end
+ end
+ def inc(*args)
+ self.class.inc(*args)
+ end
+
+ #
+ # Initializes the instance and yields itself if called with a block.
+ #
+ # +banner+:: Banner message.
+ # +width+:: Summary width.
+ # +indent+:: Summary indent.
+ #
+ def initialize(banner = nil, width = 32, indent = ' ' * 4)
+ @stack = [DefaultList, List.new, List.new]
+ @program_name = nil
+ @banner = banner
+ @summary_width = width
+ @summary_indent = indent
+ @default_argv = ARGV
+ add_officious
+ yield self if block_given?
+ end
+
+ def add_officious # :nodoc:
+ list = base()
+ Officious.each do |opt, block|
+ list.long[opt] ||= block.call(self)
+ end
+ end
+
+ #
+ # Terminates option parsing. Optional parameter +arg+ is a string pushed
+ # back to be the first non-option argument.
+ #
+ def terminate(arg = nil)
+ self.class.terminate(arg)
+ end
+ def self.terminate(arg = nil)
+ throw :terminate, arg
+ end
+
+ @stack = [DefaultList]
+ def self.top() DefaultList end
+
+ #
+ # Directs to accept specified class +t+. The argument string is passed to
+ # the block in which it should be converted to the desired class.
+ #
+ # +t+:: Argument class specifier, any object including Class.
+ # +pat+:: Pattern for argument, defaults to +t+ if it responds to match.
+ #
+ # accept(t, pat, &block)
+ #
+ def accept(*args, &blk) top.accept(*args, &blk) end
+ #
+ # See #accept.
+ #
+ def self.accept(*args, &blk) top.accept(*args, &blk) end
+
+ #
+ # Directs to reject specified class argument.
+ #
+ # +t+:: Argument class specifier, any object including Class.
+ #
+ # reject(t)
+ #
+ def reject(*args, &blk) top.reject(*args, &blk) end
+ #
+ # See #reject.
+ #
+ def self.reject(*args, &blk) top.reject(*args, &blk) end
+
+ #
+ # Instance methods
+ #
+
+ # Heading banner preceding summary.
+ attr_writer :banner
+
+ # Program name to be emitted in error message and default banner,
+ # defaults to $0.
+ attr_writer :program_name
+
+ # Width for option list portion of summary. Must be Numeric.
+ attr_accessor :summary_width
+
+ # Indentation for summary. Must be String (or have + String method).
+ attr_accessor :summary_indent
+
+ # Strings to be parsed in default.
+ attr_accessor :default_argv
+
+ #
+ # Heading banner preceding summary.
+ #
+ def banner
+ unless @banner
+ @banner = "Usage: #{program_name} [options]"
+ visit(:add_banner, @banner)
+ end
+ @banner
+ end
+
+ #
+ # Program name to be emitted in error message and default banner, defaults
+ # to $0.
+ #
+ def program_name
+ @program_name || File.basename($0, '.*')
+ end
+
+ # for experimental cascading :-)
+ alias set_banner banner=
+ alias set_program_name program_name=
+ alias set_summary_width summary_width=
+ alias set_summary_indent summary_indent=
+
+ # Version
+ attr_writer :version
+ # Release code
+ attr_writer :release
+
+ #
+ # Version
+ #
+ def version
+ @version || (defined?(::Version) && ::Version)
+ end
+
+ #
+ # Release code
+ #
+ def release
+ @release || (defined?(::Release) && ::Release) || (defined?(::RELEASE) && ::RELEASE)
+ end
+
+ #
+ # Returns version string from program_name, version and release.
+ #
+ def ver
+ if v = version
+ str = "#{program_name} #{[v].join('.')}"
+ str << " (#{v})" if v = release
+ str
+ end
+ end
+
+ def warn(mesg = $!)
+ super("#{program_name}: #{mesg}")
+ end
+
+ def abort(mesg = $!)
+ super("#{program_name}: #{mesg}")
+ end
+
+ #
+ # Subject of #on / #on_head, #accept / #reject
+ #
+ def top
+ @stack[-1]
+ end
+
+ #
+ # Subject of #on_tail.
+ #
+ def base
+ @stack[1]
+ end
+
+ #
+ # Pushes a new List.
+ #
+ def new
+ @stack.push(List.new)
+ if block_given?
+ yield self
+ else
+ self
+ end
+ end
+
+ #
+ # Removes the last List.
+ #
+ def remove
+ @stack.pop
+ end
+
+ #
+ # Puts option summary into +to+ and returns +to+. Yields each line if
+ # a block is given.
+ #
+ # +to+:: Output destination, which must have method <<. Defaults to [].
+ # +width+:: Width of left side, defaults to @summary_width.
+ # +max+:: Maximum length allowed for left side, defaults to +width+ - 1.
+ # +indent+:: Indentation, defaults to @summary_indent.
+ #
+ def summarize(to = [], width = @summary_width, max = width - 1, indent = @summary_indent, &blk)
+ blk ||= proc {|l| to << (l.index($/, -1) ? l : l + $/)}
+ visit(:summarize, {}, {}, width, max, indent, &blk)
+ to
+ end
+
+ #
+ # Returns option summary string.
+ #
+ def help; summarize(banner.to_s.sub(/\n?\z/, "\n")) end
+ alias to_s help
+
+ #
+ # Returns option summary list.
+ #
+ def to_a; summarize(banner.to_a.dup) end
+
+ #
+ # Checks if an argument is given twice, in which case an ArgumentError is
+ # raised. Called from OptionParser#switch only.
+ #
+ # +obj+:: New argument.
+ # +prv+:: Previously specified argument.
+ # +msg+:: Exception message.
+ #
+ def notwice(obj, prv, msg)
+ unless !prv or prv == obj
+ raise(ArgumentError, "argument #{msg} given twice: #{obj}",
+ ParseError.filter_backtrace(caller(2)))
+ end
+ obj
+ end
+ private :notwice
+
+ SPLAT_PROC = proc {|*a| a.length <= 1 ? a.first : a}
+ #
+ # Creates an OptionParser::Switch from the parameters. The parsed argument
+ # value is passed to the given block, where it can be processed.
+ #
+ # See at the beginning of OptionParser for some full examples.
+ #
+ # +opts+ can include the following elements:
+ #
+ # [Argument style:]
+ # One of the following:
+ # :NONE, :REQUIRED, :OPTIONAL
+ #
+ # [Argument pattern:]
+ # Acceptable option argument format, must be pre-defined with
+ # OptionParser.accept or OptionParser#accept, or Regexp. This can appear
+ # once or assigned as String if not present, otherwise causes an
+ # ArgumentError. Examples:
+ # Float, Time, Array
+ #
+ # [Possible argument values:]
+ # Hash or Array.
+ # [:text, :binary, :auto]
+ # %w[iso-2022-jp shift_jis euc-jp utf8 binary]
+ # { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
+ #
+ # [Long style switch:]
+ # Specifies a long style switch which takes a mandatory, optional or no
+ # argument. It's a string of the following form:
+ # "--switch=MANDATORY" or "--switch MANDATORY"
+ # "--switch[=OPTIONAL]"
+ # "--switch"
+ #
+ # [Short style switch:]
+ # Specifies short style switch which takes a mandatory, optional or no
+ # argument. It's a string of the following form:
+ # "-xMANDATORY"
+ # "-x[OPTIONAL]"
+ # "-x"
+ # There is also a special form which matches character range (not full
+ # set of regular expression):
+ # "-[a-z]MANDATORY"
+ # "-[a-z][OPTIONAL]"
+ # "-[a-z]"
+ #
+ # [Argument style and description:]
+ # Instead of specifying mandatory or optional arguments directly in the
+ # switch parameter, this separate parameter can be used.
+ # "=MANDATORY"
+ # "=[OPTIONAL]"
+ #
+ # [Description:]
+ # Description string for the option.
+ # "Run verbosely"
+ #
+ # [Handler:]
+ # Handler for the parsed argument value. Either give a block or pass a
+ # Proc or Method as an argument.
+ #
+ def make_switch(opts, block = nil)
+ short, long, nolong, style, pattern, conv, not_pattern, not_conv, not_style = [], [], []
+ ldesc, sdesc, desc, arg = [], [], []
+ default_style = Switch::NoArgument
+ default_pattern = nil
+ klass = nil
+ n, q, a = nil
+
+ opts.each do |o|
+ # argument class
+ next if search(:atype, o) do |pat, c|
+ klass = notwice(o, klass, 'type')
+ if not_style and not_style != Switch::NoArgument
+ not_pattern, not_conv = pat, c
+ else
+ default_pattern, conv = pat, c
+ end
+ end
+
+ # directly specified pattern(any object possible to match)
+ if (!(String === o || Symbol === o)) and o.respond_to?(:match)
+ pattern = notwice(o, pattern, 'pattern')
+ if pattern.respond_to?(:convert)
+ conv = pattern.method(:convert).to_proc
+ else
+ conv = SPLAT_PROC
+ end
+ next
+ end
+
+ # anything others
+ case o
+ when Proc, Method
+ block = notwice(o, block, 'block')
+ when Array, Hash
+ case pattern
+ when CompletingHash
+ when nil
+ pattern = CompletingHash.new
+ conv = pattern.method(:convert).to_proc if pattern.respond_to?(:convert)
+ else
+ raise ArgumentError, "argument pattern given twice"
+ end
+ o.each {|pat, *v| pattern[pat] = v.fetch(0) {pat}}
+ when Module
+ raise ArgumentError, "unsupported argument type: #{o}", ParseError.filter_backtrace(caller(4))
+ when *ArgumentStyle.keys
+ style = notwice(ArgumentStyle[o], style, 'style')
+ when /^--no-([^\[\]=\s]*)(.+)?/
+ q, a = $1, $2
+ o = notwice(a ? Object : TrueClass, klass, 'type')
+ not_pattern, not_conv = search(:atype, o) unless not_style
+ not_style = (not_style || default_style).guess(arg = a) if a
+ default_style = Switch::NoArgument
+ default_pattern, conv = search(:atype, FalseClass) unless default_pattern
+ ldesc << "--no-#{q}"
+ long << 'no-' + (q = q.downcase)
+ nolong << q
+ when /^--\[no-\]([^\[\]=\s]*)(.+)?/
+ q, a = $1, $2
+ o = notwice(a ? Object : TrueClass, klass, 'type')
+ if a
+ default_style = default_style.guess(arg = a)
+ default_pattern, conv = search(:atype, o) unless default_pattern
+ end
+ ldesc << "--[no-]#{q}"
+ long << (o = q.downcase)
+ not_pattern, not_conv = search(:atype, FalseClass) unless not_style
+ not_style = Switch::NoArgument
+ nolong << 'no-' + o
+ when /^--([^\[\]=\s]*)(.+)?/
+ q, a = $1, $2
+ if a
+ o = notwice(NilClass, klass, 'type')
+ default_style = default_style.guess(arg = a)
+ default_pattern, conv = search(:atype, o) unless default_pattern
+ end
+ ldesc << "--#{q}"
+ long << (o = q.downcase)
+ when /^-(\[\^?\]?(?:[^\\\]]|\\.)*\])(.+)?/
+ q, a = $1, $2
+ o = notwice(Object, klass, 'type')
+ if a
+ default_style = default_style.guess(arg = a)
+ default_pattern, conv = search(:atype, o) unless default_pattern
+ end
+ sdesc << "-#{q}"
+ short << Regexp.new(q)
+ when /^-(.)(.+)?/
+ q, a = $1, $2
+ if a
+ o = notwice(NilClass, klass, 'type')
+ default_style = default_style.guess(arg = a)
+ default_pattern, conv = search(:atype, o) unless default_pattern
+ end
+ sdesc << "-#{q}"
+ short << q
+ when /^=/
+ style = notwice(default_style.guess(arg = o), style, 'style')
+ default_pattern, conv = search(:atype, Object) unless default_pattern
+ else
+ desc.push(o)
+ end
+ end
+
+ default_pattern, conv = search(:atype, default_style.pattern) unless default_pattern
+ if !(short.empty? and long.empty?)
+ s = (style || default_style).new(pattern || default_pattern,
+ conv, sdesc, ldesc, arg, desc, block)
+ elsif !block
+ if style or pattern
+ raise ArgumentError, "no switch given", ParseError.filter_backtrace(caller)
+ end
+ s = desc
+ else
+ short << pattern
+ s = (style || default_style).new(pattern,
+ conv, nil, nil, arg, desc, block)
+ end
+ return s, short, long,
+ (not_style.new(not_pattern, not_conv, sdesc, ldesc, nil, desc, block) if not_style),
+ nolong
+ end
+
+ def define(*opts, &block)
+ top.append(*(sw = make_switch(opts, block)))
+ sw[0]
+ end
+
+ #
+ # Add option switch and handler. See #make_switch for an explanation of
+ # parameters.
+ #
+ def on(*opts, &block)
+ define(*opts, &block)
+ self
+ end
+ alias def_option define
+
+ def define_head(*opts, &block)
+ top.prepend(*(sw = make_switch(opts, block)))
+ sw[0]
+ end
+
+ #
+ # Add option switch like with #on, but at head of summary.
+ #
+ def on_head(*opts, &block)
+ define_head(*opts, &block)
+ self
+ end
+ alias def_head_option define_head
+
+ def define_tail(*opts, &block)
+ base.append(*(sw = make_switch(opts, block)))
+ sw[0]
+ end
+
+ #
+ # Add option switch like with #on, but at tail of summary.
+ #
+ def on_tail(*opts, &block)
+ define_tail(*opts, &block)
+ self
+ end
+ alias def_tail_option define_tail
+
+ #
+ # Add separator in summary.
+ #
+ def separator(string)
+ top.append(string, nil, nil)
+ end
+
+ #
+ # Parses command line arguments +argv+ in order. When a block is given,
+ # each non-option argument is yielded.
+ #
+ # Returns the rest of +argv+ left unparsed.
+ #
+ def order(*argv, &block)
+ argv = argv[0].dup if argv.size == 1 and Array === argv[0]
+ order!(argv, &block)
+ end
+
+ #
+ # Same as #order, but removes switches destructively.
+ #
+ def order!(argv = default_argv, &nonopt)
+ parse_in_order(argv, &nonopt)
+ end
+
+ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
+ opt, arg, val, rest = nil
+ nonopt ||= proc {|a| throw :terminate, a}
+ argv.unshift(arg) if arg = catch(:terminate) {
+ while arg = argv.shift
+ case arg
+ # long option
+ when /\A--([^=]*)(?:=(.*))?/nm
+ opt, rest = $1, $2
+ begin
+ sw, = complete(:long, opt, true)
+ rescue ParseError
+ raise $!.set_option(arg, true)
+ end
+ begin
+ opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
+ val = cb.call(val) if cb
+ setter.call(sw.switch_name, val) if setter
+ rescue ParseError
+ raise $!.set_option(arg, rest)
+ end
+
+ # short option
+ when /\A-(.)((=).*|.+)?/nm
+ opt, has_arg, eq, val, rest = $1, $3, $3, $2, $2
+ begin
+ sw, = search(:short, opt)
+ unless sw
+ begin
+ sw, = complete(:short, opt)
+ # short option matched.
+ val = arg.sub(/\A-/, '')
+ has_arg = true
+ rescue InvalidOption
+ # if no short options match, try completion with long
+ # options.
+ sw, = complete(:long, opt)
+ eq ||= !rest
+ end
+ end
+ rescue ParseError
+ raise $!.set_option(arg, true)
+ end
+ begin
+ opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
+ raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
+ argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
+ val = cb.call(val) if cb
+ setter.call(sw.switch_name, val) if setter
+ rescue ParseError
+ raise $!.set_option(arg, arg.length > 2)
+ end
+
+ # non-option argument
+ else
+ catch(:prune) do
+ visit(:each_option) do |sw0|
+ sw = sw0
+ sw.block.call(arg) if Switch === sw and sw.match_nonswitch?(arg)
+ end
+ nonopt.call(arg)
+ end
+ end
+ end
+
+ nil
+ }
+
+ visit(:search, :short, nil) {|sw| sw.block.call(*argv) if !sw.pattern}
+
+ argv
+ end
+ private :parse_in_order
+
+ #
+ # Parses command line arguments +argv+ in permutation mode and returns
+ # list of non-option arguments.
+ #
+ def permute(*argv)
+ argv = argv[0].dup if argv.size == 1 and Array === argv[0]
+ permute!(argv)
+ end
+
+ #
+ # Same as #permute, but removes switches destructively.
+ #
+ def permute!(argv = default_argv)
+ nonopts = []
+ order!(argv, &nonopts.method(:<<))
+ argv[0, 0] = nonopts
+ argv
+ end
+
+ #
+ # Parses command line arguments +argv+ in order when environment variable
+ # POSIXLY_CORRECT is set, and in permutation mode otherwise.
+ #
+ def parse(*argv)
+ argv = argv[0].dup if argv.size == 1 and Array === argv[0]
+ parse!(argv)
+ end
+
+ #
+ # Same as #parse, but removes switches destructively.
+ #
+ def parse!(argv = default_argv)
+ if ENV.include?('POSIXLY_CORRECT')
+ order!(argv)
+ else
+ permute!(argv)
+ end
+ end
+
+ #
+ # Wrapper method for getopts.rb.
+ #
+ # params = ARGV.getopts("ab:", "foo", "bar:")
+ # # params[:a] = true # -a
+ # # params[:b] = "1" # -b1
+ # # params[:foo] = "1" # --foo
+ # # params[:bar] = "x" # --bar x
+ #
+ def getopts(*args)
+ argv = Array === args.first ? args.shift : default_argv
+ single_options, *long_options = *args
+
+ result = {}
+
+ single_options.scan(/(.)(:)?/) do |opt, val|
+ if val
+ result[opt] = nil
+ define("-#{opt} VAL")
+ else
+ result[opt] = false
+ define("-#{opt}")
+ end
+ end if single_options
+
+ long_options.each do |arg|
+ opt, val = arg.split(':', 2)
+ if val
+ result[opt] = val.empty? ? nil : val
+ define("--#{opt} VAL")
+ else
+ result[opt] = false
+ define("--#{opt}")
+ end
+ end
+
+ parse_in_order(argv, result.method(:[]=))
+ result
+ end
+
+ #
+ # See #getopts.
+ #
+ def self.getopts(*args)
+ new.getopts(*args)
+ end
+
+ #
+ # Traverses @stack, sending each element method +id+ with +args+ and
+ # +block+.
+ #
+ def visit(id, *args, &block)
+ @stack.reverse_each do |el|
+ el.send(id, *args, &block)
+ end
+ nil
+ end
+ private :visit
+
+ #
+ # Searches +key+ in @stack for +id+ hash and returns or yields the result.
+ #
+ def search(id, key)
+ block_given = block_given?
+ visit(:search, id, key) do |k|
+ return block_given ? yield(k) : k
+ end
+ end
+ private :search
+
+ #
+ # Completes shortened long style option switch and returns pair of
+ # canonical switch and switch descriptor OptionParser::Switch.
+ #
+ # +id+:: Searching table.
+ # +opt+:: Searching key.
+ # +icase+:: Search case insensitive if true.
+ # +pat+:: Optional pattern for completion.
+ #
+ def complete(typ, opt, icase = false, *pat)
+ if pat.empty?
+ search(typ, opt) {|sw| return [sw, opt]} # exact match or...
+ end
+ raise AmbiguousOption, catch(:ambiguous) {
+ visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw}
+ raise InvalidOption, opt
+ }
+ end
+ private :complete
+
+ #
+ # Loads options from file names as +filename+. Does nothing when the file
+ # is not present. Returns whether successfully loaded.
+ #
+ # +filename+ defaults to basename of the program without suffix in a
+ # directory ~/.options.
+ #
+ def load(filename = nil)
+ begin
+ filename ||= File.expand_path(File.basename($0, '.*'), '~/.options')
+ rescue
+ return false
+ end
+ begin
+ parse(*IO.readlines(filename).each {|s| s.chomp!})
+ true
+ rescue Errno::ENOENT, Errno::ENOTDIR
+ false
+ end
+ end
+
+ #
+ # Parses environment variable +env+ or its uppercase with splitting like a
+ # shell.
+ #
+ # +env+ defaults to the basename of the program.
+ #
+ def environment(env = File.basename($0, '.*'))
+ env = ENV[env] || ENV[env.upcase] or return
+ require 'shellwords'
+ parse(*Shellwords.shellwords(env))
+ end
+
+ #
+ # Acceptable argument classes
+ #
+
+ #
+ # Any string and no conversion. This is fall-back.
+ #
+ accept(Object) {|s,|s or s.nil?}
+
+ accept(NilClass) {|s,|s}
+
+ #
+ # Any non-empty string, and no conversion.
+ #
+ accept(String, /.+/nm) {|s,*|s}
+
+ #
+ # Ruby/C-like integer, octal for 0-7 sequence, binary for 0b, hexadecimal
+ # for 0x, and decimal for others; with optional sign prefix. Converts to
+ # Integer.
+ #
+ decimal = '\d+(?:_\d+)*'
+ binary = 'b[01]+(?:_[01]+)*'
+ hex = 'x[\da-f]+(?:_[\da-f]+)*'
+ octal = "0(?:[0-7]*(?:_[0-7]+)*|#{binary}|#{hex})"
+ integer = "#{octal}|#{decimal}"
+ accept(Integer, %r"\A[-+]?(?:#{integer})"io) {|s,| Integer(s) if s}
+
+ #
+ # Float number format, and converts to Float.
+ #
+ float = "(?:#{decimal}(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
+ floatpat = %r"\A[-+]?#{float}"io
+ accept(Float, floatpat) {|s,| s.to_f if s}
+
+ #
+ # Generic numeric format, converts to Integer for integer format, Float
+ # for float format.
+ #
+ accept(Numeric, %r"\A[-+]?(?:#{octal}|#{float})"io) {|s,| eval(s) if s}
+
+ #
+ # Decimal integer format, to be converted to Integer.
+ #
+ DecimalInteger = /\A[-+]?#{decimal}/io
+ accept(DecimalInteger) {|s,| s.to_i if s}
+
+ #
+ # Ruby/C like octal/hexadecimal/binary integer format, to be converted to
+ # Integer.
+ #
+ OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))/io
+ accept(OctalInteger) {|s,| s.oct if s}
+
+ #
+ # Decimal integer/float number format, to be converted to Integer for
+ # integer format, Float for float format.
+ #
+ DecimalNumeric = floatpat # decimal integer is allowed as float also.
+ accept(DecimalNumeric) {|s,| eval(s) if s}
+
+ #
+ # Boolean switch, which means whether it is present or not, whether it is
+ # absent or not with prefix no-, or it takes an argument
+ # yes/no/true/false/+/-.
+ #
+ yesno = CompletingHash.new
+ %w[- no false].each {|el| yesno[el] = false}
+ %w[+ yes true].each {|el| yesno[el] = true}
+ yesno['nil'] = false # shoud be nil?
+ accept(TrueClass, yesno) {|arg, val| val == nil or val}
+ #
+ # Similar to TrueClass, but defaults to false.
+ #
+ accept(FalseClass, yesno) {|arg, val| val != nil and val}
+
+ #
+ # List of strings separated by ",".
+ #
+ accept(Array) do |s,|
+ if s
+ s = s.split(',').collect {|ss| ss unless ss.empty?}
+ end
+ s
+ end
+
+ #
+ # Regular expression with options.
+ #
+ accept(Regexp, %r"\A/((?:\\.|[^\\])*)/([[:alpha:]]+)?\z|.*") do |all, s, o|
+ f = 0
+ if o
+ f |= Regexp::IGNORECASE if /i/ =~ o
+ f |= Regexp::MULTILINE if /m/ =~ o
+ f |= Regexp::EXTENDED if /x/ =~ o
+ k = o.delete("^imx")
+ end
+ Regexp.new(s || all, f, k)
+ end
+
+ #
+ # Exceptions
+ #
+
+ #
+ # Base class of exceptions from OptionParser.
+ #
+ class ParseError < RuntimeError
+ # Reason which caused the error.
+ Reason = 'parse error'.freeze
+
+ def initialize(*args)
+ @args = args
+ @reason = nil
+ end
+
+ attr_reader :args
+ attr_writer :reason
+
+ #
+ # Pushes back erred argument(s) to +argv+.
+ #
+ def recover(argv)
+ argv[0, 0] = @args
+ argv
+ end
+
+ def self.filter_backtrace(array)
+ unless $DEBUG
+ array.delete_if(&%r"\A#{Regexp.quote(__FILE__)}:"o.method(:=~))
+ end
+ array
+ end
+
+ def set_backtrace(array)
+ super(self.class.filter_backtrace(array))
+ end
+
+ def set_option(opt, eq)
+ if eq
+ @args[0] = opt
+ else
+ @args.unshift(opt)
+ end
+ self
+ end
+
+ #
+ # Returns error reason. Override this for I18N.
+ #
+ def reason
+ @reason || self.class::Reason
+ end
+
+ def inspect
+ "#<#{self.class.to_s}: #{args.join(' ')}>"
+ end
+
+ #
+ # Default stringizing method to emit standard error message.
+ #
+ def message
+ reason + ': ' + args.join(' ')
+ end
+
+ alias to_s message
+ end
+
+ #
+ # Raises when ambiguously completable string is encountered.
+ #
+ class AmbiguousOption < ParseError
+ const_set(:Reason, 'ambiguous option'.freeze)
+ end
+
+ #
+ # Raises when there is an argument for a switch which takes no argument.
+ #
+ class NeedlessArgument < ParseError
+ const_set(:Reason, 'needless argument'.freeze)
+ end
+
+ #
+ # Raises when a switch with mandatory argument has no argument.
+ #
+ class MissingArgument < ParseError
+ const_set(:Reason, 'missing argument'.freeze)
+ end
+
+ #
+ # Raises when switch is undefined.
+ #
+ class InvalidOption < ParseError
+ const_set(:Reason, 'invalid option'.freeze)
+ end
+
+ #
+ # Raises when the given argument does not match required format.
+ #
+ class InvalidArgument < ParseError
+ const_set(:Reason, 'invalid argument'.freeze)
+ end
+
+ #
+ # Raises when the given argument word can't be completed uniquely.
+ #
+ class AmbiguousArgument < InvalidArgument
+ const_set(:Reason, 'ambiguous argument'.freeze)
+ end
+
+ #
+ # Miscellaneous
+ #
+
+ #
+ # Extends command line arguments array (ARGV) to parse itself.
+ #
+ module Arguable
+
+ #
+ # Sets OptionParser object, when +opt+ is +false+ or +nil+, methods
+ # OptionParser::Arguable#options and OptionParser::Arguable#options= are
+ # undefined. Thus, there is no ways to access the OptionParser object
+ # via the receiver object.
+ #
+ def options=(opt)
+ unless @optparse = opt
+ class << self
+ undef_method(:options)
+ undef_method(:options=)
+ end
+ end
+ end
+
+ #
+ # Actual OptionParser object, automatically created if nonexistent.
+ #
+ # If called with a block, yields the OptionParser object and returns the
+ # result of the block. If an OptionParser::ParseError exception occurs
+ # in the block, it is rescued, a error message printed to STDERR and
+ # +nil+ returned.
+ #
+ def options
+ @optparse ||= OptionParser.new
+ @optparse.default_argv = self
+ block_given? or return @optparse
+ begin
+ yield @optparse
+ rescue ParseError
+ @optparse.warn $!
+ nil
+ end
+ end
+
+ #
+ # Parses +self+ destructively in order and returns +self+ containing the
+ # rest arguments left unparsed.
+ #
+ def order!(&blk) options.order!(self, &blk) end
+
+ #
+ # Parses +self+ destructively in permutation mode and returns +self+
+ # containing the rest arguments left unparsed.
+ #
+ def permute!() options.permute!(self) end
+
+ #
+ # Parses +self+ destructively and returns +self+ containing the
+ # rest arguments left unparsed.
+ #
+ def parse!() options.parse!(self) end
+
+ #
+ # Substitution of getopts is possible as follows. Also see
+ # OptionParser#getopts.
+ #
+ # def getopts(*args)
+ # ($OPT = ARGV.getopts(*args)).each do |opt, val|
+ # eval "$OPT_#{opt.gsub(/[^A-Za-z0-9_]/, '_')} = val"
+ # end
+ # rescue OptionParser::ParseError
+ # end
+ #
+ def getopts(*args)
+ options.getopts(self, *args)
+ end
+
+ #
+ # Initializes instance variable.
+ #
+ def self.extend_object(obj)
+ super
+ obj.instance_eval {@optparse = nil}
+ end
+ def initialize(*args)
+ super
+ @optparse = nil
+ end
+ end
+
+ #
+ # Acceptable argument classes. Now contains DecimalInteger, OctalInteger
+ # and DecimalNumeric. See Acceptable argument classes (in source code).
+ #
+ module Acceptables
+ const_set(:DecimalInteger, OptionParser::DecimalInteger)
+ const_set(:OctalInteger, OptionParser::OctalInteger)
+ const_set(:DecimalNumeric, OptionParser::DecimalNumeric)
+ end
+end
+
+# ARGV is arguable by OptionParser
+ARGV.extend(OptionParser::Arguable)
+
+if $0 == __FILE__
+ Version = OptionParser::Version
+ ARGV.options {|q|
+ q.parse!.empty? or puts "what's #{ARGV.join(' ')}?"
+ } or abort(ARGV.options.to_s)
+end
diff --git a/ruby/lib/optparse/date.rb b/ruby/lib/optparse/date.rb
new file mode 100644
index 0000000..d680559
--- /dev/null
+++ b/ruby/lib/optparse/date.rb
@@ -0,0 +1,17 @@
+require 'optparse'
+require 'date'
+
+OptionParser.accept(DateTime) do |s,|
+ begin
+ DateTime.parse(s) if s
+ rescue ArgumentError
+ raise OptionParser::InvalidArgument, s
+ end
+end
+OptionParser.accept(Date) do |s,|
+ begin
+ Date.parse(s) if s
+ rescue ArgumentError
+ raise OptionParser::InvalidArgument, s
+ end
+end
diff --git a/ruby/lib/optparse/shellwords.rb b/ruby/lib/optparse/shellwords.rb
new file mode 100644
index 0000000..0422d7c
--- /dev/null
+++ b/ruby/lib/optparse/shellwords.rb
@@ -0,0 +1,6 @@
+# -*- ruby -*-
+
+require 'shellwords'
+require 'optparse'
+
+OptionParser.accept(Shellwords) {|s,| Shellwords.shellwords(s)}
diff --git a/ruby/lib/optparse/time.rb b/ruby/lib/optparse/time.rb
new file mode 100644
index 0000000..402cadc
--- /dev/null
+++ b/ruby/lib/optparse/time.rb
@@ -0,0 +1,10 @@
+require 'optparse'
+require 'time'
+
+OptionParser.accept(Time) do |s,|
+ begin
+ (Time.httpdate(s) rescue Time.parse(s)) if s
+ rescue
+ raise OptionParser::InvalidArgument, s
+ end
+end
diff --git a/ruby/lib/optparse/uri.rb b/ruby/lib/optparse/uri.rb
new file mode 100644
index 0000000..024dc69
--- /dev/null
+++ b/ruby/lib/optparse/uri.rb
@@ -0,0 +1,6 @@
+# -*- ruby -*-
+
+require 'optparse'
+require 'uri'
+
+OptionParser.accept(URI) {|s,| URI.parse(s) if s}
diff --git a/ruby/lib/optparse/version.rb b/ruby/lib/optparse/version.rb
new file mode 100644
index 0000000..76ed564
--- /dev/null
+++ b/ruby/lib/optparse/version.rb
@@ -0,0 +1,70 @@
+# OptionParser internal utility
+
+class << OptionParser
+ def show_version(*pkgs)
+ progname = ARGV.options.program_name
+ result = false
+ show = proc do |klass, cname, version|
+ str = "#{progname}"
+ unless klass == ::Object and cname == :VERSION
+ version = version.join(".") if Array === version
+ str << ": #{klass}" unless klass == Object
+ str << " version #{version}"
+ end
+ [:Release, :RELEASE].find do |rel|
+ if klass.const_defined?(rel)
+ str << " (#{klass.const_get(rel)})"
+ end
+ end
+ puts str
+ result = true
+ end
+ if pkgs.size == 1 and pkgs[0] == "all"
+ self.search_const(::Object, /\AV(?:ERSION|ersion)\z/) do |klass, cname, version|
+ unless cname[1] == ?e and klass.const_defined?(:Version)
+ show.call(klass, cname.intern, version)
+ end
+ end
+ else
+ pkgs.each do |pkg|
+ begin
+ pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)}
+ v = case
+ when pkg.const_defined?(:Version)
+ pkg.const_get(n = :Version)
+ when pkg.const_defined?(:VERSION)
+ pkg.const_get(n = :VERSION)
+ else
+ n = nil
+ "unknown"
+ end
+ show.call(pkg, n, v)
+ rescue NameError
+ end
+ end
+ end
+ result
+ end
+
+ def each_const(path, base = ::Object)
+ path.split(/::|\//).inject(base) do |klass, name|
+ raise NameError, path unless Module === klass
+ klass.constants.grep(/#{name}/i) do |c|
+ klass.const_defined?(c) or next
+ c = klass.const_get(c)
+ end
+ end
+ end
+
+ def search_const(klass, name)
+ klasses = [klass]
+ while klass = klasses.shift
+ klass.constants.each do |cname|
+ klass.const_defined?(cname) or next
+ const = klass.const_get(cname)
+ yield klass, cname, const if name === cname
+ klasses << const if Module === const and const != ::Object
+ end
+ end
+ end
+end
diff --git a/ruby/lib/ostruct.rb b/ruby/lib/ostruct.rb
new file mode 100644
index 0000000..35a14b4
--- /dev/null
+++ b/ruby/lib/ostruct.rb
@@ -0,0 +1,145 @@
+#
+# = ostruct.rb: OpenStruct implementation
+#
+# Author:: Yukihiro Matsumoto
+# Documentation:: Gavin Sinclair
+#
+# OpenStruct allows the creation of data objects with arbitrary attributes.
+# See OpenStruct for an example.
+#
+
+#
+# OpenStruct allows you to create data objects and set arbitrary attributes.
+# For example:
+#
+# require 'ostruct'
+#
+# record = OpenStruct.new
+# record.name = "John Smith"
+# record.age = 70
+# record.pension = 300
+#
+# puts record.name # -> "John Smith"
+# puts record.address # -> nil
+#
+# It is like a hash with a different way to access the data. In fact, it is
+# implemented with a hash, and you can initialize it with one.
+#
+# hash = { "country" => "Australia", :population => 20_000_000 }
+# data = OpenStruct.new(hash)
+#
+# p data # -> <OpenStruct country="Australia" population=20000000>
+#
+class OpenStruct
+ #
+ # Create a new OpenStruct object. The optional +hash+, if given, will
+ # generate attributes and values. For example.
+ #
+ # require 'ostruct'
+ # hash = { "country" => "Australia", :population => 20_000_000 }
+ # data = OpenStruct.new(hash)
+ #
+ # p data # -> <OpenStruct country="Australia" population=20000000>
+ #
+ # By default, the resulting OpenStruct object will have no attributes.
+ #
+ def initialize(hash=nil)
+ @table = {}
+ if hash
+ for k,v in hash
+ @table[k.to_sym] = v
+ new_ostruct_member(k)
+ end
+ end
+ end
+
+ # Duplicate an OpenStruct object members.
+ def initialize_copy(orig)
+ super
+ @table = @table.dup
+ end
+
+ def marshal_dump
+ @table
+ end
+ def marshal_load(x)
+ @table = x
+ @table.each_key{|key| new_ostruct_member(key)}
+ end
+
+ def new_ostruct_member(name)
+ name = name.to_sym
+ unless self.respond_to?(name)
+ class << self; self; end.class_eval do
+ define_method(name) { @table[name] }
+ define_method(:"#{name}=") { |x| @table[name] = x }
+ end
+ end
+ end
+
+ def method_missing(mid, *args) # :nodoc:
+ mname = mid.id2name
+ len = args.length
+ if mname =~ /=$/
+ if len != 1
+ raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
+ end
+ if self.frozen?
+ raise TypeError, "can't modify frozen #{self.class}", caller(1)
+ end
+ mname.chop!
+ self.new_ostruct_member(mname)
+ @table[mname.intern] = args[0]
+ elsif len == 0
+ @table[mid]
+ else
+ raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
+ end
+ end
+
+ #
+ # Remove the named field from the object.
+ #
+ def delete_field(name)
+ @table.delete name.to_sym
+ end
+
+ InspectKey = :__inspect_key__ # :nodoc:
+
+ #
+ # Returns a string containing a detailed summary of the keys and values.
+ #
+ def inspect
+ str = "#<#{self.class}"
+
+ ids = (Thread.current[InspectKey] ||= [])
+ if ids.include?(object_id)
+ return str << ' ...>'
+ end
+
+ ids << object_id
+ begin
+ first = true
+ for k,v in @table
+ str << "," unless first
+ first = false
+ str << " #{k}=#{v.inspect}"
+ end
+ return str << '>'
+ ensure
+ ids.pop
+ end
+ end
+ alias :to_s :inspect
+
+ attr_reader :table # :nodoc:
+ protected :table
+
+ #
+ # Compare this object and +other+ for equality.
+ #
+ def ==(other)
+ return false unless(other.kind_of?(OpenStruct))
+ return @table == other.table
+ end
+end
diff --git a/ruby/lib/pathname.rb b/ruby/lib/pathname.rb
new file mode 100644
index 0000000..fbb42a6
--- /dev/null
+++ b/ruby/lib/pathname.rb
@@ -0,0 +1,1099 @@
+#
+# = pathname.rb
+#
+# Object-Oriented Pathname Class
+#
+# Author:: Tanaka Akira <akr@m17n.org>
+# Documentation:: Author and Gavin Sinclair
+#
+# For documentation, see class Pathname.
+#
+# <tt>pathname.rb</tt> is distributed with Ruby since 1.8.0.
+#
+
+#
+# == Pathname
+#
+# Pathname represents a pathname which locates a file in a filesystem.
+# The pathname depends on OS: Unix, Windows, etc.
+# Pathname library works with pathnames of local OS.
+# However non-Unix pathnames are supported experimentally.
+#
+# It does not represent the file itself.
+# A Pathname can be relative or absolute. It's not until you try to
+# reference the file that it even matters whether the file exists or not.
+#
+# Pathname is immutable. It has no method for destructive update.
+#
+# The value of this class is to manipulate file path information in a neater
+# way than standard Ruby provides. The examples below demonstrate the
+# difference. *All* functionality from File, FileTest, and some from Dir and
+# FileUtils is included, in an unsurprising way. It is essentially a facade for
+# all of these, and more.
+#
+# == Examples
+#
+# === Example 1: Using Pathname
+#
+# require 'pathname'
+# p = Pathname.new("/usr/bin/ruby")
+# size = p.size # 27662
+# isdir = p.directory? # false
+# dir = p.dirname # Pathname:/usr/bin
+# base = p.basename # Pathname:ruby
+# dir, base = p.split # [Pathname:/usr/bin, Pathname:ruby]
+# data = p.read
+# p.open { |f| _ }
+# p.each_line { |line| _ }
+#
+# === Example 2: Using standard Ruby
+#
+# p = "/usr/bin/ruby"
+# size = File.size(p) # 27662
+# isdir = File.directory?(p) # false
+# dir = File.dirname(p) # "/usr/bin"
+# base = File.basename(p) # "ruby"
+# dir, base = File.split(p) # ["/usr/bin", "ruby"]
+# data = File.read(p)
+# File.open(p) { |f| _ }
+# File.foreach(p) { |line| _ }
+#
+# === Example 3: Special features
+#
+# p1 = Pathname.new("/usr/lib") # Pathname:/usr/lib
+# p2 = p1 + "ruby/1.8" # Pathname:/usr/lib/ruby/1.8
+# p3 = p1.parent # Pathname:/usr
+# p4 = p2.relative_path_from(p3) # Pathname:lib/ruby/1.8
+# pwd = Pathname.pwd # Pathname:/home/gavin
+# pwd.absolute? # true
+# p5 = Pathname.new "." # Pathname:.
+# p5 = p5 + "music/../articles" # Pathname:music/../articles
+# p5.cleanpath # Pathname:articles
+# p5.realpath # Pathname:/home/gavin/articles
+# p5.children # [Pathname:/home/gavin/articles/linux, ...]
+#
+# == Breakdown of functionality
+#
+# === Core methods
+#
+# These methods are effectively manipulating a String, because that's all a path
+# is. Except for #mountpoint?, #children, and #realpath, they don't access the
+# filesystem.
+#
+# - +
+# - #join
+# - #parent
+# - #root?
+# - #absolute?
+# - #relative?
+# - #relative_path_from
+# - #each_filename
+# - #cleanpath
+# - #realpath
+# - #children
+# - #mountpoint?
+#
+# === File status predicate methods
+#
+# These methods are a facade for FileTest:
+# - #blockdev?
+# - #chardev?
+# - #directory?
+# - #executable?
+# - #executable_real?
+# - #exist?
+# - #file?
+# - #grpowned?
+# - #owned?
+# - #pipe?
+# - #readable?
+# - #world_readable?
+# - #readable_real?
+# - #setgid?
+# - #setuid?
+# - #size
+# - #size?
+# - #socket?
+# - #sticky?
+# - #symlink?
+# - #writable?
+# - #world_writable?
+# - #writable_real?
+# - #zero?
+#
+# === File property and manipulation methods
+#
+# These methods are a facade for File:
+# - #atime
+# - #ctime
+# - #mtime
+# - #chmod(mode)
+# - #lchmod(mode)
+# - #chown(owner, group)
+# - #lchown(owner, group)
+# - #fnmatch(pattern, *args)
+# - #fnmatch?(pattern, *args)
+# - #ftype
+# - #make_link(old)
+# - #open(*args, &block)
+# - #readlink
+# - #rename(to)
+# - #stat
+# - #lstat
+# - #make_symlink(old)
+# - #truncate(length)
+# - #utime(atime, mtime)
+# - #basename(*args)
+# - #dirname
+# - #extname
+# - #expand_path(*args)
+# - #split
+#
+# === Directory methods
+#
+# These methods are a facade for Dir:
+# - Pathname.glob(*args)
+# - Pathname.getwd / Pathname.pwd
+# - #rmdir
+# - #entries
+# - #each_entry(&block)
+# - #mkdir(*args)
+# - #opendir(*args)
+#
+# === IO
+#
+# These methods are a facade for IO:
+# - #each_line(*args, &block)
+# - #read(*args)
+# - #readlines(*args)
+# - #sysopen(*args)
+#
+# === Utilities
+#
+# These methods are a mixture of Find, FileUtils, and others:
+# - #find(&block)
+# - #mkpath
+# - #rmtree
+# - #unlink / #delete
+#
+#
+# == Method documentation
+#
+# As the above section shows, most of the methods in Pathname are facades. The
+# documentation for these methods generally just says, for instance, "See
+# FileTest.writable?", as you should be familiar with the original method
+# anyway, and its documentation (e.g. through +ri+) will contain more
+# information. In some cases, a brief description will follow.
+#
+class Pathname
+
+ # :stopdoc:
+ if RUBY_VERSION < "1.9"
+ TO_PATH = :to_str
+ else
+ # to_path is implemented so Pathname objects are usable with File.open, etc.
+ TO_PATH = :to_path
+ end
+
+ SAME_PATHS = if File::FNM_SYSCASE
+ proc {|a, b| a.casecmp(b).zero?}
+ else
+ proc {|a, b| a == b}
+ end
+
+ # :startdoc:
+
+ #
+ # Create a Pathname object from the given String (or String-like object).
+ # If +path+ contains a NUL character (<tt>\0</tt>), an ArgumentError is raised.
+ #
+ def initialize(path)
+ path = path.__send__(TO_PATH) if path.respond_to? TO_PATH
+ @path = path.dup
+
+ if /\0/ =~ @path
+ raise ArgumentError, "pathname contains \\0: #{@path.inspect}"
+ end
+
+ self.taint if @path.tainted?
+ end
+
+ def freeze() super; @path.freeze; self end
+ def taint() super; @path.taint; self end
+ def untaint() super; @path.untaint; self end
+
+ #
+ # Compare this pathname with +other+. The comparison is string-based.
+ # Be aware that two different paths (<tt>foo.txt</tt> and <tt>./foo.txt</tt>)
+ # can refer to the same file.
+ #
+ def ==(other)
+ return false unless Pathname === other
+ other.to_s == @path
+ end
+ alias === ==
+ alias eql? ==
+
+ # Provides for comparing pathnames, case-sensitively.
+ def <=>(other)
+ return nil unless Pathname === other
+ @path.tr('/', "\0") <=> other.to_s.tr('/', "\0")
+ end
+
+ def hash # :nodoc:
+ @path.hash
+ end
+
+ # Return the path as a String.
+ def to_s
+ @path.dup
+ end
+
+ # to_path is implemented so Pathname objects are usable with File.open, etc.
+ alias_method TO_PATH, :to_s
+
+ def inspect # :nodoc:
+ "#<#{self.class}:#{@path}>"
+ end
+
+ # Return a pathname which is substituted by String#sub.
+ def sub(pattern, *rest, &block)
+ if block
+ path = @path.sub(pattern, *rest) {|*args|
+ begin
+ old = Thread.current[:pathname_sub_matchdata]
+ Thread.current[:pathname_sub_matchdata] = $~
+ eval("$~ = Thread.current[:pathname_sub_matchdata]", block.binding)
+ ensure
+ Thread.current[:pathname_sub_matchdata] = old
+ end
+ yield *args
+ }
+ else
+ path = @path.sub(pattern, *rest)
+ end
+ self.class.new(path)
+ end
+
+ if File::ALT_SEPARATOR
+ SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}"
+ SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/
+ else
+ SEPARATOR_LIST = "#{Regexp.quote File::SEPARATOR}"
+ SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
+ end
+
+ # Return a pathname which the extension of the basename is substituted by
+ # <i>repl</i>.
+ #
+ # If self has no extension part, <i>repl</i> is appended.
+ def sub_ext(repl)
+ ext = File.extname(@path)
+ self.class.new(@path.chomp(ext) + repl)
+ end
+
+ # chop_basename(path) -> [pre-basename, basename] or nil
+ def chop_basename(path)
+ base = File.basename(path)
+ if /\A#{SEPARATOR_PAT}?\z/ =~ base
+ return nil
+ else
+ return path[0, path.rindex(base)], base
+ end
+ end
+ private :chop_basename
+
+ # split_names(path) -> prefix, [name, ...]
+ def split_names(path)
+ names = []
+ while r = chop_basename(path)
+ path, basename = r
+ names.unshift basename
+ end
+ return path, names
+ end
+ private :split_names
+
+ def prepend_prefix(prefix, relpath)
+ if relpath.empty?
+ File.dirname(prefix)
+ elsif /#{SEPARATOR_PAT}/ =~ prefix
+ prefix = File.dirname(prefix)
+ prefix = File.join(prefix, "") if File.basename(prefix + 'a') != 'a'
+ prefix + relpath
+ else
+ prefix + relpath
+ end
+ end
+ private :prepend_prefix
+
+ # Returns clean pathname of +self+ with consecutive slashes and useless dots
+ # removed. The filesystem is not accessed.
+ #
+ # If +consider_symlink+ is +true+, then a more conservative algorithm is used
+ # to avoid breaking symbolic linkages. This may retain more <tt>..</tt>
+ # entries than absolutely necessary, but without accessing the filesystem,
+ # this can't be avoided. See #realpath.
+ #
+ def cleanpath(consider_symlink=false)
+ if consider_symlink
+ cleanpath_conservative
+ else
+ cleanpath_aggressive
+ end
+ end
+
+ #
+ # Clean the path simply by resolving and removing excess "." and ".." entries.
+ # Nothing more, nothing less.
+ #
+ def cleanpath_aggressive
+ path = @path
+ names = []
+ pre = path
+ while r = chop_basename(pre)
+ pre, base = r
+ case base
+ when '.'
+ when '..'
+ names.unshift base
+ else
+ if names[0] == '..'
+ names.shift
+ else
+ names.unshift base
+ end
+ end
+ end
+ if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
+ names.shift while names[0] == '..'
+ end
+ self.class.new(prepend_prefix(pre, File.join(*names)))
+ end
+ private :cleanpath_aggressive
+
+ # has_trailing_separator?(path) -> bool
+ def has_trailing_separator?(path)
+ if r = chop_basename(path)
+ pre, basename = r
+ pre.length + basename.length < path.length
+ else
+ false
+ end
+ end
+ private :has_trailing_separator?
+
+ # add_trailing_separator(path) -> path
+ def add_trailing_separator(path)
+ if File.basename(path + 'a') == 'a'
+ path
+ else
+ File.join(path, "") # xxx: Is File.join is appropriate to add separator?
+ end
+ end
+ private :add_trailing_separator
+
+ def del_trailing_separator(path)
+ if r = chop_basename(path)
+ pre, basename = r
+ pre + basename
+ elsif /#{SEPARATOR_PAT}+\z/o =~ path
+ $` + File.dirname(path)[/#{SEPARATOR_PAT}*\z/o]
+ else
+ path
+ end
+ end
+ private :del_trailing_separator
+
+ def cleanpath_conservative
+ path = @path
+ names = []
+ pre = path
+ while r = chop_basename(pre)
+ pre, base = r
+ names.unshift base if base != '.'
+ end
+ if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
+ names.shift while names[0] == '..'
+ end
+ if names.empty?
+ self.class.new(File.dirname(pre))
+ else
+ if names.last != '..' && File.basename(path) == '.'
+ names << '.'
+ end
+ result = prepend_prefix(pre, File.join(*names))
+ if /\A(?:\.|\.\.)\z/ !~ names.last && has_trailing_separator?(path)
+ self.class.new(add_trailing_separator(result))
+ else
+ self.class.new(result)
+ end
+ end
+ end
+ private :cleanpath_conservative
+
+ def realpath_rec(prefix, unresolved, h)
+ resolved = []
+ until unresolved.empty?
+ n = unresolved.shift
+ if n == '.'
+ next
+ elsif n == '..'
+ resolved.pop
+ else
+ path = prepend_prefix(prefix, File.join(*(resolved + [n])))
+ if h.include? path
+ if h[path] == :resolving
+ raise Errno::ELOOP.new(path)
+ else
+ prefix, *resolved = h[path]
+ end
+ else
+ s = File.lstat(path)
+ if s.symlink?
+ h[path] = :resolving
+ link_prefix, link_names = split_names(File.readlink(path))
+ if link_prefix == ''
+ prefix, *resolved = h[path] = realpath_rec(prefix, resolved + link_names, h)
+ else
+ prefix, *resolved = h[path] = realpath_rec(link_prefix, link_names, h)
+ end
+ else
+ resolved << n
+ h[path] = [prefix, *resolved]
+ end
+ end
+ end
+ end
+ return prefix, *resolved
+ end
+ private :realpath_rec
+
+ #
+ # Returns a real (absolute) pathname of +self+ in the actual filesystem.
+ # The real pathname doesn't contain symlinks or useless dots.
+ #
+ # No arguments should be given; the old behaviour is *obsoleted*.
+ #
+ def realpath
+ path = @path
+ prefix, names = split_names(path)
+ if prefix == ''
+ prefix, names2 = split_names(Dir.pwd)
+ names = names2 + names
+ end
+ prefix, *names = realpath_rec(prefix, names, {})
+ self.class.new(prepend_prefix(prefix, File.join(*names)))
+ end
+
+ # #parent returns the parent directory.
+ #
+ # This is same as <tt>self + '..'</tt>.
+ def parent
+ self + '..'
+ end
+
+ # #mountpoint? returns +true+ if <tt>self</tt> points to a mountpoint.
+ def mountpoint?
+ begin
+ stat1 = self.lstat
+ stat2 = self.parent.lstat
+ stat1.dev == stat2.dev && stat1.ino == stat2.ino ||
+ stat1.dev != stat2.dev
+ rescue Errno::ENOENT
+ false
+ end
+ end
+
+ #
+ # #root? is a predicate for root directories. I.e. it returns +true+ if the
+ # pathname consists of consecutive slashes.
+ #
+ # It doesn't access actual filesystem. So it may return +false+ for some
+ # pathnames which points to roots such as <tt>/usr/..</tt>.
+ #
+ def root?
+ !!(chop_basename(@path) == nil && /#{SEPARATOR_PAT}/o =~ @path)
+ end
+
+ # Predicate method for testing whether a path is absolute.
+ # It returns +true+ if the pathname begins with a slash.
+ def absolute?
+ !relative?
+ end
+
+ # The opposite of #absolute?
+ def relative?
+ path = @path
+ while r = chop_basename(path)
+ path, basename = r
+ end
+ path == ''
+ end
+
+ #
+ # Iterates over each component of the path.
+ #
+ # Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
+ # # yields "usr", "bin", and "ruby".
+ #
+ def each_filename # :yield: filename
+ return to_enum(__method__) unless block_given?
+ prefix, names = split_names(@path)
+ names.each {|filename| yield filename }
+ nil
+ end
+
+ # Iterates over and yields a new Pathname object
+ # for each element in the given path in descending order.
+ #
+ # Pathname.new('/path/to/some/file.rb').descend {|v| p v}
+ # #<Pathname:/>
+ # #<Pathname:/path>
+ # #<Pathname:/path/to>
+ # #<Pathname:/path/to/some>
+ # #<Pathname:/path/to/some/file.rb>
+ #
+ # Pathname.new('path/to/some/file.rb').descend {|v| p v}
+ # #<Pathname:path>
+ # #<Pathname:path/to>
+ # #<Pathname:path/to/some>
+ # #<Pathname:path/to/some/file.rb>
+ #
+ # It doesn't access actual filesystem.
+ #
+ # This method is available since 1.8.5.
+ #
+ def descend
+ vs = []
+ ascend {|v| vs << v }
+ vs.reverse_each {|v| yield v }
+ nil
+ end
+
+ # Iterates over and yields a new Pathname object
+ # for each element in the given path in ascending order.
+ #
+ # Pathname.new('/path/to/some/file.rb').ascend {|v| p v}
+ # #<Pathname:/path/to/some/file.rb>
+ # #<Pathname:/path/to/some>
+ # #<Pathname:/path/to>
+ # #<Pathname:/path>
+ # #<Pathname:/>
+ #
+ # Pathname.new('path/to/some/file.rb').ascend {|v| p v}
+ # #<Pathname:path/to/some/file.rb>
+ # #<Pathname:path/to/some>
+ # #<Pathname:path/to>
+ # #<Pathname:path>
+ #
+ # It doesn't access actual filesystem.
+ #
+ # This method is available since 1.8.5.
+ #
+ def ascend
+ path = @path
+ yield self
+ while r = chop_basename(path)
+ path, name = r
+ break if path.empty?
+ yield self.class.new(del_trailing_separator(path))
+ end
+ end
+
+ #
+ # Pathname#+ appends a pathname fragment to this one to produce a new Pathname
+ # object.
+ #
+ # p1 = Pathname.new("/usr") # Pathname:/usr
+ # p2 = p1 + "bin/ruby" # Pathname:/usr/bin/ruby
+ # p3 = p1 + "/etc/passwd" # Pathname:/etc/passwd
+ #
+ # This method doesn't access the file system; it is pure string manipulation.
+ #
+ def +(other)
+ other = Pathname.new(other) unless Pathname === other
+ Pathname.new(plus(@path, other.to_s))
+ end
+
+ def plus(path1, path2) # -> path
+ prefix2 = path2
+ index_list2 = []
+ basename_list2 = []
+ while r2 = chop_basename(prefix2)
+ prefix2, basename2 = r2
+ index_list2.unshift prefix2.length
+ basename_list2.unshift basename2
+ end
+ return path2 if prefix2 != ''
+ prefix1 = path1
+ while true
+ while !basename_list2.empty? && basename_list2.first == '.'
+ index_list2.shift
+ basename_list2.shift
+ end
+ break unless r1 = chop_basename(prefix1)
+ prefix1, basename1 = r1
+ next if basename1 == '.'
+ if basename1 == '..' || basename_list2.empty? || basename_list2.first != '..'
+ prefix1 = prefix1 + basename1
+ break
+ end
+ index_list2.shift
+ basename_list2.shift
+ end
+ r1 = chop_basename(prefix1)
+ if !r1 && /#{SEPARATOR_PAT}/o =~ File.basename(prefix1)
+ while !basename_list2.empty? && basename_list2.first == '..'
+ index_list2.shift
+ basename_list2.shift
+ end
+ end
+ if !basename_list2.empty?
+ suffix2 = path2[index_list2.first..-1]
+ r1 ? File.join(prefix1, suffix2) : prefix1 + suffix2
+ else
+ r1 ? prefix1 : File.dirname(prefix1)
+ end
+ end
+ private :plus
+
+ #
+ # Pathname#join joins pathnames.
+ #
+ # <tt>path0.join(path1, ..., pathN)</tt> is the same as
+ # <tt>path0 + path1 + ... + pathN</tt>.
+ #
+ def join(*args)
+ args.unshift self
+ result = args.pop
+ result = Pathname.new(result) unless Pathname === result
+ return result if result.absolute?
+ args.reverse_each {|arg|
+ arg = Pathname.new(arg) unless Pathname === arg
+ result = arg + result
+ return result if result.absolute?
+ }
+ result
+ end
+
+ #
+ # Returns the children of the directory (files and subdirectories, not
+ # recursive) as an array of Pathname objects. By default, the returned
+ # pathnames will have enough information to access the files. If you set
+ # +with_directory+ to +false+, then the returned pathnames will contain the
+ # filename only.
+ #
+ # For example:
+ # p = Pathname("/usr/lib/ruby/1.8")
+ # p.children
+ # # -> [ Pathname:/usr/lib/ruby/1.8/English.rb,
+ # Pathname:/usr/lib/ruby/1.8/Env.rb,
+ # Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ]
+ # p.children(false)
+ # # -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
+ #
+ # Note that the result never contain the entries <tt>.</tt> and <tt>..</tt> in
+ # the directory because they are not children.
+ #
+ # This method has existed since 1.8.1.
+ #
+ def children(with_directory=true)
+ with_directory = false if @path == '.'
+ result = []
+ Dir.foreach(@path) {|e|
+ next if e == '.' || e == '..'
+ if with_directory
+ result << self.class.new(File.join(@path, e))
+ else
+ result << self.class.new(e)
+ end
+ }
+ result
+ end
+
+ #
+ # #relative_path_from returns a relative path from the argument to the
+ # receiver. If +self+ is absolute, the argument must be absolute too. If
+ # +self+ is relative, the argument must be relative too.
+ #
+ # #relative_path_from doesn't access the filesystem. It assumes no symlinks.
+ #
+ # ArgumentError is raised when it cannot find a relative path.
+ #
+ # This method has existed since 1.8.1.
+ #
+ def relative_path_from(base_directory)
+ dest_directory = self.cleanpath.to_s
+ base_directory = base_directory.cleanpath.to_s
+ dest_prefix = dest_directory
+ dest_names = []
+ while r = chop_basename(dest_prefix)
+ dest_prefix, basename = r
+ dest_names.unshift basename if basename != '.'
+ end
+ base_prefix = base_directory
+ base_names = []
+ while r = chop_basename(base_prefix)
+ base_prefix, basename = r
+ base_names.unshift basename if basename != '.'
+ end
+ unless SAME_PATHS[dest_prefix, base_prefix]
+ raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
+ end
+ while !dest_names.empty? &&
+ !base_names.empty? &&
+ SAME_PATHS[dest_names.first, base_names.first]
+ dest_names.shift
+ base_names.shift
+ end
+ if base_names.include? '..'
+ raise ArgumentError, "base_directory has ..: #{base_directory.inspect}"
+ end
+ base_names.fill('..')
+ relpath_names = base_names + dest_names
+ if relpath_names.empty?
+ Pathname.new('.')
+ else
+ Pathname.new(File.join(*relpath_names))
+ end
+ end
+end
+
+class Pathname # * IO *
+ #
+ # #each_line iterates over the line in the file. It yields a String object
+ # for each line.
+ #
+ # This method has existed since 1.8.1.
+ #
+ def each_line(*args, &block) # :yield: line
+ IO.foreach(@path, *args, &block)
+ end
+
+ # Pathname#foreachline is *obsoleted* at 1.8.1. Use #each_line.
+ def foreachline(*args, &block)
+ warn "Pathname#foreachline is obsoleted. Use Pathname#each_line."
+ each_line(*args, &block)
+ end
+
+ # See <tt>IO.read</tt>. Returns all the bytes from the file, or the first +N+
+ # if specified.
+ def read(*args) IO.read(@path, *args) end
+
+ # See <tt>IO.readlines</tt>. Returns all the lines from the file.
+ def readlines(*args) IO.readlines(@path, *args) end
+
+ # See <tt>IO.sysopen</tt>.
+ def sysopen(*args) IO.sysopen(@path, *args) end
+end
+
+
+class Pathname # * File *
+
+ # See <tt>File.atime</tt>. Returns last access time.
+ def atime() File.atime(@path) end
+
+ # See <tt>File.ctime</tt>. Returns last (directory entry, not file) change time.
+ def ctime() File.ctime(@path) end
+
+ # See <tt>File.mtime</tt>. Returns last modification time.
+ def mtime() File.mtime(@path) end
+
+ # See <tt>File.chmod</tt>. Changes permissions.
+ def chmod(mode) File.chmod(mode, @path) end
+
+ # See <tt>File.lchmod</tt>.
+ def lchmod(mode) File.lchmod(mode, @path) end
+
+ # See <tt>File.chown</tt>. Change owner and group of file.
+ def chown(owner, group) File.chown(owner, group, @path) end
+
+ # See <tt>File.lchown</tt>.
+ def lchown(owner, group) File.lchown(owner, group, @path) end
+
+ # See <tt>File.fnmatch</tt>. Return +true+ if the receiver matches the given
+ # pattern.
+ def fnmatch(pattern, *args) File.fnmatch(pattern, @path, *args) end
+
+ # See <tt>File.fnmatch?</tt> (same as #fnmatch).
+ def fnmatch?(pattern, *args) File.fnmatch?(pattern, @path, *args) end
+
+ # See <tt>File.ftype</tt>. Returns "type" of file ("file", "directory",
+ # etc).
+ def ftype() File.ftype(@path) end
+
+ # See <tt>File.link</tt>. Creates a hard link.
+ def make_link(old) File.link(old, @path) end
+
+ # See <tt>File.open</tt>. Opens the file for reading or writing.
+ def open(*args, &block) # :yield: file
+ File.open(@path, *args, &block)
+ end
+
+ # See <tt>File.readlink</tt>. Read symbolic link.
+ def readlink() self.class.new(File.readlink(@path)) end
+
+ # See <tt>File.rename</tt>. Rename the file.
+ def rename(to) File.rename(@path, to) end
+
+ # See <tt>File.stat</tt>. Returns a <tt>File::Stat</tt> object.
+ def stat() File.stat(@path) end
+
+ # See <tt>File.lstat</tt>.
+ def lstat() File.lstat(@path) end
+
+ # See <tt>File.symlink</tt>. Creates a symbolic link.
+ def make_symlink(old) File.symlink(old, @path) end
+
+ # See <tt>File.truncate</tt>. Truncate the file to +length+ bytes.
+ def truncate(length) File.truncate(@path, length) end
+
+ # See <tt>File.utime</tt>. Update the access and modification times.
+ def utime(atime, mtime) File.utime(atime, mtime, @path) end
+
+ # See <tt>File.basename</tt>. Returns the last component of the path.
+ def basename(*args) self.class.new(File.basename(@path, *args)) end
+
+ # See <tt>File.dirname</tt>. Returns all but the last component of the path.
+ def dirname() self.class.new(File.dirname(@path)) end
+
+ # See <tt>File.extname</tt>. Returns the file's extension.
+ def extname() File.extname(@path) end
+
+ # See <tt>File.expand_path</tt>.
+ def expand_path(*args) self.class.new(File.expand_path(@path, *args)) end
+
+ # See <tt>File.split</tt>. Returns the #dirname and the #basename in an
+ # Array.
+ def split() File.split(@path).map {|f| self.class.new(f) } end
+
+ # Pathname#link is confusing and *obsoleted* because the receiver/argument
+ # order is inverted to corresponding system call.
+ def link(old)
+ warn 'Pathname#link is obsoleted. Use Pathname#make_link.'
+ File.link(old, @path)
+ end
+
+ # Pathname#symlink is confusing and *obsoleted* because the receiver/argument
+ # order is inverted to corresponding system call.
+ def symlink(old)
+ warn 'Pathname#symlink is obsoleted. Use Pathname#make_symlink.'
+ File.symlink(old, @path)
+ end
+end
+
+
+class Pathname # * FileTest *
+
+ # See <tt>FileTest.blockdev?</tt>.
+ def blockdev?() FileTest.blockdev?(@path) end
+
+ # See <tt>FileTest.chardev?</tt>.
+ def chardev?() FileTest.chardev?(@path) end
+
+ # See <tt>FileTest.executable?</tt>.
+ def executable?() FileTest.executable?(@path) end
+
+ # See <tt>FileTest.executable_real?</tt>.
+ def executable_real?() FileTest.executable_real?(@path) end
+
+ # See <tt>FileTest.exist?</tt>.
+ def exist?() FileTest.exist?(@path) end
+
+ # See <tt>FileTest.grpowned?</tt>.
+ def grpowned?() FileTest.grpowned?(@path) end
+
+ # See <tt>FileTest.directory?</tt>.
+ def directory?() FileTest.directory?(@path) end
+
+ # See <tt>FileTest.file?</tt>.
+ def file?() FileTest.file?(@path) end
+
+ # See <tt>FileTest.pipe?</tt>.
+ def pipe?() FileTest.pipe?(@path) end
+
+ # See <tt>FileTest.socket?</tt>.
+ def socket?() FileTest.socket?(@path) end
+
+ # See <tt>FileTest.owned?</tt>.
+ def owned?() FileTest.owned?(@path) end
+
+ # See <tt>FileTest.readable?</tt>.
+ def readable?() FileTest.readable?(@path) end
+
+ # See <tt>FileTest.world_readable?</tt>.
+ def world_readable?() FileTest.world_readable?(@path) end
+
+ # See <tt>FileTest.readable_real?</tt>.
+ def readable_real?() FileTest.readable_real?(@path) end
+
+ # See <tt>FileTest.setuid?</tt>.
+ def setuid?() FileTest.setuid?(@path) end
+
+ # See <tt>FileTest.setgid?</tt>.
+ def setgid?() FileTest.setgid?(@path) end
+
+ # See <tt>FileTest.size</tt>.
+ def size() FileTest.size(@path) end
+
+ # See <tt>FileTest.size?</tt>.
+ def size?() FileTest.size?(@path) end
+
+ # See <tt>FileTest.sticky?</tt>.
+ def sticky?() FileTest.sticky?(@path) end
+
+ # See <tt>FileTest.symlink?</tt>.
+ def symlink?() FileTest.symlink?(@path) end
+
+ # See <tt>FileTest.writable?</tt>.
+ def writable?() FileTest.writable?(@path) end
+
+ # See <tt>FileTest.world_writable?</tt>.
+ def world_writable?() FileTest.world_writable?(@path) end
+
+ # See <tt>FileTest.writable_real?</tt>.
+ def writable_real?() FileTest.writable_real?(@path) end
+
+ # See <tt>FileTest.zero?</tt>.
+ def zero?() FileTest.zero?(@path) end
+end
+
+
+class Pathname # * Dir *
+ # See <tt>Dir.glob</tt>. Returns or yields Pathname objects.
+ def Pathname.glob(*args) # :yield: p
+ if block_given?
+ Dir.glob(*args) {|f| yield self.new(f) }
+ else
+ Dir.glob(*args).map {|f| self.new(f) }
+ end
+ end
+
+ # See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname.
+ def Pathname.getwd() self.new(Dir.getwd) end
+ class << self; alias pwd getwd end
+
+ # Pathname#chdir is *obsoleted* at 1.8.1.
+ def chdir(&block)
+ warn "Pathname#chdir is obsoleted. Use Dir.chdir."
+ Dir.chdir(@path, &block)
+ end
+
+ # Pathname#chroot is *obsoleted* at 1.8.1.
+ def chroot
+ warn "Pathname#chroot is obsoleted. Use Dir.chroot."
+ Dir.chroot(@path)
+ end
+
+ # Return the entries (files and subdirectories) in the directory, each as a
+ # Pathname object.
+ def entries() Dir.entries(@path).map {|f| self.class.new(f) } end
+
+ # Iterates over the entries (files and subdirectories) in the directory. It
+ # yields a Pathname object for each entry.
+ #
+ # This method has existed since 1.8.1.
+ def each_entry(&block) # :yield: p
+ Dir.foreach(@path) {|f| yield self.class.new(f) }
+ end
+
+ # Pathname#dir_foreach is *obsoleted* at 1.8.1.
+ def dir_foreach(*args, &block)
+ warn "Pathname#dir_foreach is obsoleted. Use Pathname#each_entry."
+ each_entry(*args, &block)
+ end
+
+ # See <tt>Dir.mkdir</tt>. Create the referenced directory.
+ def mkdir(*args) Dir.mkdir(@path, *args) end
+
+ # See <tt>Dir.rmdir</tt>. Remove the referenced directory.
+ def rmdir() Dir.rmdir(@path) end
+
+ # See <tt>Dir.open</tt>.
+ def opendir(&block) # :yield: dir
+ Dir.open(@path, &block)
+ end
+end
+
+
+class Pathname # * Find *
+ #
+ # Pathname#find is an iterator to traverse a directory tree in a depth first
+ # manner. It yields a Pathname for each file under "this" directory.
+ #
+ # Since it is implemented by <tt>find.rb</tt>, <tt>Find.prune</tt> can be used
+ # to control the traverse.
+ #
+ # If +self+ is <tt>.</tt>, yielded pathnames begin with a filename in the
+ # current directory, not <tt>./</tt>.
+ #
+ def find(&block) # :yield: p
+ require 'find'
+ if @path == '.'
+ Find.find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
+ else
+ Find.find(@path) {|f| yield self.class.new(f) }
+ end
+ end
+end
+
+
+class Pathname # * FileUtils *
+ # See <tt>FileUtils.mkpath</tt>. Creates a full path, including any
+ # intermediate directories that don't yet exist.
+ def mkpath
+ require 'fileutils'
+ FileUtils.mkpath(@path)
+ nil
+ end
+
+ # See <tt>FileUtils.rm_r</tt>. Deletes a directory and all beneath it.
+ def rmtree
+ # The name "rmtree" is borrowed from File::Path of Perl.
+ # File::Path provides "mkpath" and "rmtree".
+ require 'fileutils'
+ FileUtils.rm_r(@path)
+ nil
+ end
+end
+
+
+class Pathname # * mixed *
+ # Removes a file or directory, using <tt>File.unlink</tt> or
+ # <tt>Dir.unlink</tt> as necessary.
+ def unlink()
+ begin
+ Dir.unlink @path
+ rescue Errno::ENOTDIR
+ File.unlink @path
+ end
+ end
+ alias delete unlink
+
+ # This method is *obsoleted* at 1.8.1. Use #each_line or #each_entry.
+ def foreach(*args, &block)
+ warn "Pathname#foreach is obsoleted. Use each_line or each_entry."
+ if FileTest.directory? @path
+ # For polymorphism between Dir.foreach and IO.foreach,
+ # Pathname#foreach doesn't yield Pathname object.
+ Dir.foreach(@path, *args, &block)
+ else
+ IO.foreach(@path, *args, &block)
+ end
+ end
+end
+
+class Pathname
+ undef =~
+end
+
+module Kernel
+ # create a pathname object.
+ #
+ # This method is available since 1.8.5.
+ def Pathname(path) # :doc:
+ Pathname.new(path)
+ end
+ private :Pathname
+end
diff --git a/ruby/lib/pp.rb b/ruby/lib/pp.rb
new file mode 100644
index 0000000..e8819d7
--- /dev/null
+++ b/ruby/lib/pp.rb
@@ -0,0 +1,532 @@
+# == Pretty-printer for Ruby objects.
+#
+# = Which seems better?
+#
+# non-pretty-printed output by #p is:
+# #<PP:0x81fedf0 @genspace=#<Proc:0x81feda0>, @group_queue=#<PrettyPrint::GroupQueue:0x81fed3c @queue=[[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], []]>, @buffer=[], @newline="\n", @group_stack=[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], @buffer_width=0, @indent=0, @maxwidth=79, @output_width=2, @output=#<IO:0x8114ee4>>
+#
+# pretty-printed output by #pp is:
+# #<PP:0x81fedf0
+# @buffer=[],
+# @buffer_width=0,
+# @genspace=#<Proc:0x81feda0>,
+# @group_queue=
+# #<PrettyPrint::GroupQueue:0x81fed3c
+# @queue=
+# [[#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
+# []]>,
+# @group_stack=
+# [#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
+# @indent=0,
+# @maxwidth=79,
+# @newline="\n",
+# @output=#<IO:0x8114ee4>,
+# @output_width=2>
+#
+# I like the latter. If you do too, this library is for you.
+#
+# = Usage
+#
+# pp(obj)
+#
+# output +obj+ to +$>+ in pretty printed format.
+#
+# It returns +nil+.
+#
+# = Output Customization
+# To define your customized pretty printing function for your classes,
+# redefine a method #pretty_print(+pp+) in the class.
+# It takes an argument +pp+ which is an instance of the class PP.
+# The method should use PP#text, PP#breakable, PP#nest, PP#group and
+# PP#pp to print the object.
+#
+# = Author
+# Tanaka Akira <akr@m17n.org>
+
+require 'prettyprint'
+
+module Kernel
+ # returns a pretty printed object as a string.
+ def pretty_inspect
+ PP.pp(self, '')
+ end
+
+ private
+ # prints arguments in pretty form.
+ #
+ # pp returns nil.
+ def pp(*objs) # :doc:
+ objs.each {|obj|
+ PP.pp(obj)
+ }
+ nil
+ end
+ module_function :pp
+end
+
+class PP < PrettyPrint
+ # Outputs +obj+ to +out+ in pretty printed format of
+ # +width+ columns in width.
+ #
+ # If +out+ is omitted, +$>+ is assumed.
+ # If +width+ is omitted, 79 is assumed.
+ #
+ # PP.pp returns +out+.
+ def PP.pp(obj, out=$>, width=79)
+ q = PP.new(out, width)
+ q.guard_inspect_key {q.pp obj}
+ q.flush
+ #$pp = q
+ out << "\n"
+ end
+
+ # Outputs +obj+ to +out+ like PP.pp but with no indent and
+ # newline.
+ #
+ # PP.singleline_pp returns +out+.
+ def PP.singleline_pp(obj, out=$>)
+ q = SingleLine.new(out)
+ q.guard_inspect_key {q.pp obj}
+ q.flush
+ out
+ end
+
+ # :stopdoc:
+ def PP.mcall(obj, mod, meth, *args, &block)
+ mod.instance_method(meth).bind(obj).call(*args, &block)
+ end
+ # :startdoc:
+
+ @sharing_detection = false
+ class << self
+ # Returns the sharing detection flag as a boolean value.
+ # It is false by default.
+ attr_accessor :sharing_detection
+ end
+
+ module PPMethods
+ def guard_inspect_key
+ if Thread.current[:__recursive_key__] == nil
+ Thread.current[:__recursive_key__] = {}.untrust
+ end
+
+ if Thread.current[:__recursive_key__][:inspect] == nil
+ Thread.current[:__recursive_key__][:inspect] = {}.untrust
+ end
+
+ save = Thread.current[:__recursive_key__][:inspect]
+
+ begin
+ Thread.current[:__recursive_key__][:inspect] = {}.untrust
+ yield
+ ensure
+ Thread.current[:__recursive_key__][:inspect] = save
+ end
+ end
+
+ def check_inspect_key(id)
+ Thread.current[:__recursive_key__] &&
+ Thread.current[:__recursive_key__][:inspect] &&
+ Thread.current[:__recursive_key__][:inspect].include?(id)
+ end
+ def push_inspect_key(id)
+ Thread.current[:__recursive_key__][:inspect][id] = true
+ end
+ def pop_inspect_key(id)
+ Thread.current[:__recursive_key__][:inspect].delete id
+ end
+
+ # Adds +obj+ to the pretty printing buffer
+ # using Object#pretty_print or Object#pretty_print_cycle.
+ #
+ # Object#pretty_print_cycle is used when +obj+ is already
+ # printed, a.k.a the object reference chain has a cycle.
+ def pp(obj)
+ id = obj.object_id
+
+ if check_inspect_key(id)
+ group {obj.pretty_print_cycle self}
+ return
+ end
+
+ begin
+ push_inspect_key(id)
+ group {obj.pretty_print self}
+ ensure
+ pop_inspect_key(id) unless PP.sharing_detection
+ end
+ end
+
+ # A convenience method which is same as follows:
+ #
+ # group(1, '#<' + obj.class.name, '>') { ... }
+ def object_group(obj, &block) # :yield:
+ group(1, '#<' + obj.class.name, '>', &block)
+ end
+
+ if 0x100000000.class == Bignum
+ # 32bit
+ PointerMask = 0xffffffff
+ else
+ # 64bit
+ PointerMask = 0xffffffffffffffff
+ end
+
+ case Object.new.inspect
+ when /\A\#<Object:0x([0-9a-f]+)>\z/
+ PointerFormat = "%0#{$1.length}x"
+ else
+ PointerFormat = "%x"
+ end
+
+ def object_address_group(obj, &block)
+ id = PointerFormat % (obj.object_id * 2 & PointerMask)
+ group(1, "\#<#{obj.class}:0x#{id}", '>', &block)
+ end
+
+ # A convenience method which is same as follows:
+ #
+ # text ','
+ # breakable
+ def comma_breakable
+ text ','
+ breakable
+ end
+
+ # Adds a separated list.
+ # The list is separated by comma with breakable space, by default.
+ #
+ # #seplist iterates the +list+ using +iter_method+.
+ # It yields each object to the block given for #seplist.
+ # The procedure +separator_proc+ is called between each yields.
+ #
+ # If the iteration is zero times, +separator_proc+ is not called at all.
+ #
+ # If +separator_proc+ is nil or not given,
+ # +lambda { comma_breakable }+ is used.
+ # If +iter_method+ is not given, :each is used.
+ #
+ # For example, following 3 code fragments has similar effect.
+ #
+ # q.seplist([1,2,3]) {|v| xxx v }
+ #
+ # q.seplist([1,2,3], lambda { q.comma_breakable }, :each) {|v| xxx v }
+ #
+ # xxx 1
+ # q.comma_breakable
+ # xxx 2
+ # q.comma_breakable
+ # xxx 3
+ def seplist(list, sep=nil, iter_method=:each) # :yield: element
+ sep ||= lambda { comma_breakable }
+ first = true
+ list.__send__(iter_method) {|*v|
+ if first
+ first = false
+ else
+ sep.call
+ end
+ yield(*v)
+ }
+ end
+
+ def pp_object(obj)
+ object_address_group(obj) {
+ seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
+ breakable
+ v = v.to_s if Symbol === v
+ text v
+ text '='
+ group(1) {
+ breakable ''
+ pp(obj.instance_eval(v))
+ }
+ }
+ }
+ end
+
+ def pp_hash(obj)
+ group(1, '{', '}') {
+ seplist(obj, nil, :each_pair) {|k, v|
+ group {
+ pp k
+ text '=>'
+ group(1) {
+ breakable ''
+ pp v
+ }
+ }
+ }
+ }
+ end
+ end
+
+ include PPMethods
+
+ class SingleLine < PrettyPrint::SingleLine
+ include PPMethods
+ end
+
+ module ObjectMixin
+ # 1. specific pretty_print
+ # 2. specific inspect
+ # 3. specific to_s if instance variable is empty
+ # 4. generic pretty_print
+
+ # A default pretty printing method for general objects.
+ # It calls #pretty_print_instance_variables to list instance variables.
+ #
+ # If +self+ has a customized (redefined) #inspect method,
+ # the result of self.inspect is used but it obviously has no
+ # line break hints.
+ #
+ # This module provides predefined #pretty_print methods for some of
+ # the most commonly used built-in classes for convenience.
+ def pretty_print(q)
+ method_method = Object.instance_method(:method).bind(self)
+ begin
+ inspect_method = method_method.call(:inspect)
+ rescue NameError
+ end
+ begin
+ to_s_method = method_method.call(:to_s)
+ rescue NameError
+ end
+ if inspect_method && /\(Kernel\)#/ !~ inspect_method.inspect
+ q.text self.inspect
+ elsif !inspect_method && self.respond_to?(:inspect)
+ q.text self.inspect
+ elsif to_s_method && /\(Kernel\)#/ !~ to_s_method.inspect &&
+ instance_variables.empty?
+ q.text self.to_s
+ elsif !to_s_method && self.respond_to?(:to_s)
+ q.text self.to_s
+ else
+ q.pp_object(self)
+ end
+ end
+
+ # A default pretty printing method for general objects that are
+ # detected as part of a cycle.
+ def pretty_print_cycle(q)
+ q.object_address_group(self) {
+ q.breakable
+ q.text '...'
+ }
+ end
+
+ # Returns a sorted array of instance variable names.
+ #
+ # This method should return an array of names of instance variables as symbols or strings as:
+ # +[:@a, :@b]+.
+ def pretty_print_instance_variables
+ instance_variables.sort
+ end
+
+ # Is #inspect implementation using #pretty_print.
+ # If you implement #pretty_print, it can be used as follows.
+ #
+ # alias inspect pretty_print_inspect
+ #
+ # However, doing this requires that every class that #inspect is called on
+ # implement #pretty_print, or a RuntimeError will be raised.
+ def pretty_print_inspect
+ if /\(PP::ObjectMixin\)#/ =~ Object.instance_method(:method).bind(self).call(:pretty_print).inspect
+ raise "pretty_print is not overridden for #{self.class}"
+ end
+ PP.singleline_pp(self, '')
+ end
+ end
+end
+
+class Array
+ def pretty_print(q)
+ q.group(1, '[', ']') {
+ q.seplist(self) {|v|
+ q.pp v
+ }
+ }
+ end
+
+ def pretty_print_cycle(q)
+ q.text(empty? ? '[]' : '[...]')
+ end
+end
+
+class Hash
+ def pretty_print(q)
+ q.pp_hash self
+ end
+
+ def pretty_print_cycle(q)
+ q.text(empty? ? '{}' : '{...}')
+ end
+end
+
+class << ENV
+ def pretty_print(q)
+ h = {}
+ ENV.keys.sort.each {|k|
+ h[k] = ENV[k]
+ }
+ q.pp_hash h
+ end
+end
+
+class Struct
+ def pretty_print(q)
+ q.group(1, '#<struct ' + PP.mcall(self, Kernel, :class).name, '>') {
+ q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
+ q.breakable
+ q.text member.to_s
+ q.text '='
+ q.group(1) {
+ q.breakable ''
+ q.pp self[member]
+ }
+ }
+ }
+ end
+
+ def pretty_print_cycle(q)
+ q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
+ end
+end
+
+class Range
+ def pretty_print(q)
+ q.pp self.begin
+ q.breakable ''
+ q.text(self.exclude_end? ? '...' : '..')
+ q.breakable ''
+ q.pp self.end
+ end
+end
+
+class File
+ class Stat
+ def pretty_print(q)
+ require 'etc.so'
+ q.object_group(self) {
+ q.breakable
+ q.text sprintf("dev=0x%x", self.dev); q.comma_breakable
+ q.text "ino="; q.pp self.ino; q.comma_breakable
+ q.group {
+ m = self.mode
+ q.text sprintf("mode=0%o", m)
+ q.breakable
+ q.text sprintf("(%s %c%c%c%c%c%c%c%c%c)",
+ self.ftype,
+ (m & 0400 == 0 ? ?- : ?r),
+ (m & 0200 == 0 ? ?- : ?w),
+ (m & 0100 == 0 ? (m & 04000 == 0 ? ?- : ?S) :
+ (m & 04000 == 0 ? ?x : ?s)),
+ (m & 0040 == 0 ? ?- : ?r),
+ (m & 0020 == 0 ? ?- : ?w),
+ (m & 0010 == 0 ? (m & 02000 == 0 ? ?- : ?S) :
+ (m & 02000 == 0 ? ?x : ?s)),
+ (m & 0004 == 0 ? ?- : ?r),
+ (m & 0002 == 0 ? ?- : ?w),
+ (m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) :
+ (m & 01000 == 0 ? ?x : ?t)))
+ }
+ q.comma_breakable
+ q.text "nlink="; q.pp self.nlink; q.comma_breakable
+ q.group {
+ q.text "uid="; q.pp self.uid
+ begin
+ pw = Etc.getpwuid(self.uid)
+ rescue ArgumentError
+ end
+ if pw
+ q.breakable; q.text "(#{pw.name})"
+ end
+ }
+ q.comma_breakable
+ q.group {
+ q.text "gid="; q.pp self.gid
+ begin
+ gr = Etc.getgrgid(self.gid)
+ rescue ArgumentError
+ end
+ if gr
+ q.breakable; q.text "(#{gr.name})"
+ end
+ }
+ q.comma_breakable
+ q.group {
+ q.text sprintf("rdev=0x%x", self.rdev)
+ q.breakable
+ q.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor)
+ }
+ q.comma_breakable
+ q.text "size="; q.pp self.size; q.comma_breakable
+ q.text "blksize="; q.pp self.blksize; q.comma_breakable
+ q.text "blocks="; q.pp self.blocks; q.comma_breakable
+ q.group {
+ t = self.atime
+ q.text "atime="; q.pp t
+ q.breakable; q.text "(#{t.tv_sec})"
+ }
+ q.comma_breakable
+ q.group {
+ t = self.mtime
+ q.text "mtime="; q.pp t
+ q.breakable; q.text "(#{t.tv_sec})"
+ }
+ q.comma_breakable
+ q.group {
+ t = self.ctime
+ q.text "ctime="; q.pp t
+ q.breakable; q.text "(#{t.tv_sec})"
+ }
+ }
+ end
+ end
+end
+
+class MatchData
+ def pretty_print(q)
+ nc = []
+ self.regexp.named_captures.each {|name, indexes|
+ indexes.each {|i| nc[i] = name }
+ }
+ q.object_group(self) {
+ q.breakable
+ q.seplist(0...self.size, lambda { q.breakable }) {|i|
+ if i == 0
+ q.pp self[i]
+ else
+ if nc[i]
+ q.text nc[i]
+ else
+ q.pp i
+ end
+ q.text ':'
+ q.pp self[i]
+ end
+ }
+ }
+ end
+end
+
+class Object
+ include PP::ObjectMixin
+end
+
+[Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each {|c|
+ c.class_eval {
+ def pretty_print_cycle(q)
+ q.text inspect
+ end
+ }
+}
+
+[Numeric, FalseClass, TrueClass, Module].each {|c|
+ c.class_eval {
+ def pretty_print(q)
+ q.text inspect
+ end
+ }
+}
+# :enddoc:
diff --git a/ruby/lib/prettyprint.rb b/ruby/lib/prettyprint.rb
new file mode 100644
index 0000000..48f2ebf
--- /dev/null
+++ b/ruby/lib/prettyprint.rb
@@ -0,0 +1,896 @@
+# $Id$
+
+# This class implements a pretty printing algorithm. It finds line breaks and
+# nice indentations for grouped structure.
+#
+# By default, the class assumes that primitive elements are strings and each
+# byte in the strings have single column in width. But it can be used for
+# other situations by giving suitable arguments for some methods:
+# * newline object and space generation block for PrettyPrint.new
+# * optional width argument for PrettyPrint#text
+# * PrettyPrint#breakable
+#
+# There are several candidate uses:
+# * text formatting using proportional fonts
+# * multibyte characters which has columns different to number of bytes
+# * non-string formatting
+#
+# == Bugs
+# * Box based formatting?
+# * Other (better) model/algorithm?
+#
+# == References
+# Christian Lindig, Strictly Pretty, March 2000,
+# http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
+#
+# Philip Wadler, A prettier printer, March 1998,
+# http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
+#
+# == Author
+# Tanaka Akira <akr@m17n.org>
+#
+class PrettyPrint
+
+ # This is a convenience method which is same as follows:
+ #
+ # begin
+ # q = PrettyPrint.new(output, maxwidth, newline, &genspace)
+ # ...
+ # q.flush
+ # output
+ # end
+ #
+ def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
+ q = PrettyPrint.new(output, maxwidth, newline, &genspace)
+ yield q
+ q.flush
+ output
+ end
+
+ # This is similar to PrettyPrint::format but the result has no breaks.
+ #
+ # +maxwidth+, +newline+ and +genspace+ are ignored.
+ #
+ # The invocation of +breakable+ in the block doesn't break a line and is
+ # treated as just an invocation of +text+.
+ #
+ def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil)
+ q = SingleLine.new(output)
+ yield q
+ output
+ end
+
+ # Creates a buffer for pretty printing.
+ #
+ # +output+ is an output target. If it is not specified, '' is assumed. It
+ # should have a << method which accepts the first argument +obj+ of
+ # PrettyPrint#text, the first argument +sep+ of PrettyPrint#breakable, the
+ # first argument +newline+ of PrettyPrint.new, and the result of a given
+ # block for PrettyPrint.new.
+ #
+ # +maxwidth+ specifies maximum line length. If it is not specified, 79 is
+ # assumed. However actual outputs may overflow +maxwidth+ if long
+ # non-breakable texts are provided.
+ #
+ # +newline+ is used for line breaks. "\n" is used if it is not specified.
+ #
+ # The block is used to generate spaces. {|width| ' ' * width} is used if it
+ # is not given.
+ #
+ def initialize(output='', maxwidth=79, newline="\n", &genspace)
+ @output = output
+ @maxwidth = maxwidth
+ @newline = newline
+ @genspace = genspace || lambda {|n| ' ' * n}
+
+ @output_width = 0
+ @buffer_width = 0
+ @buffer = []
+
+ root_group = Group.new(0)
+ @group_stack = [root_group]
+ @group_queue = GroupQueue.new(root_group)
+ @indent = 0
+ end
+ attr_reader :output, :maxwidth, :newline, :genspace
+ attr_reader :indent, :group_queue
+
+ def current_group
+ @group_stack.last
+ end
+
+ # first? is a predicate to test the call is a first call to first? with
+ # current group.
+ #
+ # It is useful to format comma separated values as:
+ #
+ # q.group(1, '[', ']') {
+ # xxx.each {|yyy|
+ # unless q.first?
+ # q.text ','
+ # q.breakable
+ # end
+ # ... pretty printing yyy ...
+ # }
+ # }
+ #
+ # first? is obsoleted in 1.8.2.
+ #
+ def first?
+ warn "PrettyPrint#first? is obsoleted at 1.8.2."
+ current_group.first?
+ end
+
+ def break_outmost_groups
+ while @maxwidth < @output_width + @buffer_width
+ return unless group = @group_queue.deq
+ until group.breakables.empty?
+ data = @buffer.shift
+ @output_width = data.output(@output, @output_width)
+ @buffer_width -= data.width
+ end
+ while !@buffer.empty? && Text === @buffer.first
+ text = @buffer.shift
+ @output_width = text.output(@output, @output_width)
+ @buffer_width -= text.width
+ end
+ end
+ end
+
+ # This adds +obj+ as a text of +width+ columns in width.
+ #
+ # If +width+ is not specified, obj.length is used.
+ #
+ def text(obj, width=obj.length)
+ if @buffer.empty?
+ @output << obj
+ @output_width += width
+ else
+ text = @buffer.last
+ unless Text === text
+ text = Text.new
+ @buffer << text
+ end
+ text.add(obj, width)
+ @buffer_width += width
+ break_outmost_groups
+ end
+ end
+
+ def fill_breakable(sep=' ', width=sep.length)
+ group { breakable sep, width }
+ end
+
+ # This tells "you can break a line here if necessary", and a +width+\-column
+ # text +sep+ is inserted if a line is not broken at the point.
+ #
+ # If +sep+ is not specified, " " is used.
+ #
+ # If +width+ is not specified, +sep.length+ is used. You will have to
+ # specify this when +sep+ is a multibyte character, for example.
+ #
+ def breakable(sep=' ', width=sep.length)
+ group = @group_stack.last
+ if group.break?
+ flush
+ @output << @newline
+ @output << @genspace.call(@indent)
+ @output_width = @indent
+ @buffer_width = 0
+ else
+ @buffer << Breakable.new(sep, width, self)
+ @buffer_width += width
+ break_outmost_groups
+ end
+ end
+
+ # Groups line break hints added in the block. The line break hints are all
+ # to be used or not.
+ #
+ # If +indent+ is specified, the method call is regarded as nested by
+ # nest(indent) { ... }.
+ #
+ # If +open_obj+ is specified, <tt>text open_obj, open_width</tt> is called
+ # before grouping. If +close_obj+ is specified, <tt>text close_obj,
+ # close_width</tt> is called after grouping.
+ #
+ def group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length)
+ text open_obj, open_width
+ group_sub {
+ nest(indent) {
+ yield
+ }
+ }
+ text close_obj, close_width
+ end
+
+ def group_sub
+ group = Group.new(@group_stack.last.depth + 1)
+ @group_stack.push group
+ @group_queue.enq group
+ begin
+ yield
+ ensure
+ @group_stack.pop
+ if group.breakables.empty?
+ @group_queue.delete group
+ end
+ end
+ end
+
+ # Increases left margin after newline with +indent+ for line breaks added in
+ # the block.
+ #
+ def nest(indent)
+ @indent += indent
+ begin
+ yield
+ ensure
+ @indent -= indent
+ end
+ end
+
+ # outputs buffered data.
+ #
+ def flush
+ @buffer.each {|data|
+ @output_width = data.output(@output, @output_width)
+ }
+ @buffer.clear
+ @buffer_width = 0
+ end
+
+ class Text
+ def initialize
+ @objs = []
+ @width = 0
+ end
+ attr_reader :width
+
+ def output(out, output_width)
+ @objs.each {|obj| out << obj}
+ output_width + @width
+ end
+
+ def add(obj, width)
+ @objs << obj
+ @width += width
+ end
+ end
+
+ class Breakable
+ def initialize(sep, width, q)
+ @obj = sep
+ @width = width
+ @pp = q
+ @indent = q.indent
+ @group = q.current_group
+ @group.breakables.push self
+ end
+ attr_reader :obj, :width, :indent
+
+ def output(out, output_width)
+ @group.breakables.shift
+ if @group.break?
+ out << @pp.newline
+ out << @pp.genspace.call(@indent)
+ @indent
+ else
+ @pp.group_queue.delete @group if @group.breakables.empty?
+ out << @obj
+ output_width + @width
+ end
+ end
+ end
+
+ class Group
+ def initialize(depth)
+ @depth = depth
+ @breakables = []
+ @break = false
+ end
+ attr_reader :depth, :breakables
+
+ def break
+ @break = true
+ end
+
+ def break?
+ @break
+ end
+
+ def first?
+ if defined? @first
+ false
+ else
+ @first = false
+ true
+ end
+ end
+ end
+
+ class GroupQueue
+ def initialize(*groups)
+ @queue = []
+ groups.each {|g| enq g}
+ end
+
+ def enq(group)
+ depth = group.depth
+ @queue << [] until depth < @queue.length
+ @queue[depth] << group
+ end
+
+ def deq
+ @queue.each {|gs|
+ (gs.length-1).downto(0) {|i|
+ unless gs[i].breakables.empty?
+ group = gs.slice!(i, 1).first
+ group.break
+ return group
+ end
+ }
+ gs.each {|group| group.break}
+ gs.clear
+ }
+ return nil
+ end
+
+ def delete(group)
+ @queue[group.depth].delete(group)
+ end
+ end
+
+ class SingleLine
+ def initialize(output, maxwidth=nil, newline=nil)
+ @output = output
+ @first = [true]
+ end
+
+ def text(obj, width=nil)
+ @output << obj
+ end
+
+ def breakable(sep=' ', width=nil)
+ @output << sep
+ end
+
+ def nest(indent)
+ yield
+ end
+
+ def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
+ @first.push true
+ @output << open_obj
+ yield
+ @output << close_obj
+ @first.pop
+ end
+
+ def flush
+ end
+
+ def first?
+ result = @first[-1]
+ @first[-1] = false
+ result
+ end
+ end
+end
+
+if __FILE__ == $0
+ require 'test/unit'
+
+ class WadlerExample < Test::Unit::TestCase # :nodoc:
+ def setup
+ @tree = Tree.new("aaaa", Tree.new("bbbbb", Tree.new("ccc"),
+ Tree.new("dd")),
+ Tree.new("eee"),
+ Tree.new("ffff", Tree.new("gg"),
+ Tree.new("hhh"),
+ Tree.new("ii")))
+ end
+
+ def hello(width)
+ PrettyPrint.format('', width) {|hello|
+ hello.group {
+ hello.group {
+ hello.group {
+ hello.group {
+ hello.text 'hello'
+ hello.breakable; hello.text 'a'
+ }
+ hello.breakable; hello.text 'b'
+ }
+ hello.breakable; hello.text 'c'
+ }
+ hello.breakable; hello.text 'd'
+ }
+ }
+ end
+
+ def test_hello_00_06
+ expected = <<'End'.chomp
+hello
+a
+b
+c
+d
+End
+ assert_equal(expected, hello(0))
+ assert_equal(expected, hello(6))
+ end
+
+ def test_hello_07_08
+ expected = <<'End'.chomp
+hello a
+b
+c
+d
+End
+ assert_equal(expected, hello(7))
+ assert_equal(expected, hello(8))
+ end
+
+ def test_hello_09_10
+ expected = <<'End'.chomp
+hello a b
+c
+d
+End
+ out = hello(9); assert_equal(expected, out)
+ out = hello(10); assert_equal(expected, out)
+ end
+
+ def test_hello_11_12
+ expected = <<'End'.chomp
+hello a b c
+d
+End
+ assert_equal(expected, hello(11))
+ assert_equal(expected, hello(12))
+ end
+
+ def test_hello_13
+ expected = <<'End'.chomp
+hello a b c d
+End
+ assert_equal(expected, hello(13))
+ end
+
+ def tree(width)
+ PrettyPrint.format('', width) {|q| @tree.show(q)}
+ end
+
+ def test_tree_00_19
+ expected = <<'End'.chomp
+aaaa[bbbbb[ccc,
+ dd],
+ eee,
+ ffff[gg,
+ hhh,
+ ii]]
+End
+ assert_equal(expected, tree(0))
+ assert_equal(expected, tree(19))
+ end
+
+ def test_tree_20_22
+ expected = <<'End'.chomp
+aaaa[bbbbb[ccc, dd],
+ eee,
+ ffff[gg,
+ hhh,
+ ii]]
+End
+ assert_equal(expected, tree(20))
+ assert_equal(expected, tree(22))
+ end
+
+ def test_tree_23_43
+ expected = <<'End'.chomp
+aaaa[bbbbb[ccc, dd],
+ eee,
+ ffff[gg, hhh, ii]]
+End
+ assert_equal(expected, tree(23))
+ assert_equal(expected, tree(43))
+ end
+
+ def test_tree_44
+ assert_equal(<<'End'.chomp, tree(44))
+aaaa[bbbbb[ccc, dd], eee, ffff[gg, hhh, ii]]
+End
+ end
+
+ def tree_alt(width)
+ PrettyPrint.format('', width) {|q| @tree.altshow(q)}
+ end
+
+ def test_tree_alt_00_18
+ expected = <<'End'.chomp
+aaaa[
+ bbbbb[
+ ccc,
+ dd
+ ],
+ eee,
+ ffff[
+ gg,
+ hhh,
+ ii
+ ]
+]
+End
+ assert_equal(expected, tree_alt(0))
+ assert_equal(expected, tree_alt(18))
+ end
+
+ def test_tree_alt_19_20
+ expected = <<'End'.chomp
+aaaa[
+ bbbbb[ ccc, dd ],
+ eee,
+ ffff[
+ gg,
+ hhh,
+ ii
+ ]
+]
+End
+ assert_equal(expected, tree_alt(19))
+ assert_equal(expected, tree_alt(20))
+ end
+
+ def test_tree_alt_20_49
+ expected = <<'End'.chomp
+aaaa[
+ bbbbb[ ccc, dd ],
+ eee,
+ ffff[ gg, hhh, ii ]
+]
+End
+ assert_equal(expected, tree_alt(21))
+ assert_equal(expected, tree_alt(49))
+ end
+
+ def test_tree_alt_50
+ expected = <<'End'.chomp
+aaaa[ bbbbb[ ccc, dd ], eee, ffff[ gg, hhh, ii ] ]
+End
+ assert_equal(expected, tree_alt(50))
+ end
+
+ class Tree # :nodoc:
+ def initialize(string, *children)
+ @string = string
+ @children = children
+ end
+
+ def show(q)
+ q.group {
+ q.text @string
+ q.nest(@string.length) {
+ unless @children.empty?
+ q.text '['
+ q.nest(1) {
+ first = true
+ @children.each {|t|
+ if first
+ first = false
+ else
+ q.text ','
+ q.breakable
+ end
+ t.show(q)
+ }
+ }
+ q.text ']'
+ end
+ }
+ }
+ end
+
+ def altshow(q)
+ q.group {
+ q.text @string
+ unless @children.empty?
+ q.text '['
+ q.nest(2) {
+ q.breakable
+ first = true
+ @children.each {|t|
+ if first
+ first = false
+ else
+ q.text ','
+ q.breakable
+ end
+ t.altshow(q)
+ }
+ }
+ q.breakable
+ q.text ']'
+ end
+ }
+ end
+
+ end
+ end
+
+ class StrictPrettyExample < Test::Unit::TestCase # :nodoc:
+ def prog(width)
+ PrettyPrint.format('', width) {|q|
+ q.group {
+ q.group {q.nest(2) {
+ q.text "if"; q.breakable;
+ q.group {
+ q.nest(2) {
+ q.group {q.text "a"; q.breakable; q.text "=="}
+ q.breakable; q.text "b"}}}}
+ q.breakable
+ q.group {q.nest(2) {
+ q.text "then"; q.breakable;
+ q.group {
+ q.nest(2) {
+ q.group {q.text "a"; q.breakable; q.text "<<"}
+ q.breakable; q.text "2"}}}}
+ q.breakable
+ q.group {q.nest(2) {
+ q.text "else"; q.breakable;
+ q.group {
+ q.nest(2) {
+ q.group {q.text "a"; q.breakable; q.text "+"}
+ q.breakable; q.text "b"}}}}}
+ }
+ end
+
+ def test_00_04
+ expected = <<'End'.chomp
+if
+ a
+ ==
+ b
+then
+ a
+ <<
+ 2
+else
+ a
+ +
+ b
+End
+ assert_equal(expected, prog(0))
+ assert_equal(expected, prog(4))
+ end
+
+ def test_05
+ expected = <<'End'.chomp
+if
+ a
+ ==
+ b
+then
+ a
+ <<
+ 2
+else
+ a +
+ b
+End
+ assert_equal(expected, prog(5))
+ end
+
+ def test_06
+ expected = <<'End'.chomp
+if
+ a ==
+ b
+then
+ a <<
+ 2
+else
+ a +
+ b
+End
+ assert_equal(expected, prog(6))
+ end
+
+ def test_07
+ expected = <<'End'.chomp
+if
+ a ==
+ b
+then
+ a <<
+ 2
+else
+ a + b
+End
+ assert_equal(expected, prog(7))
+ end
+
+ def test_08
+ expected = <<'End'.chomp
+if
+ a == b
+then
+ a << 2
+else
+ a + b
+End
+ assert_equal(expected, prog(8))
+ end
+
+ def test_09
+ expected = <<'End'.chomp
+if a == b
+then
+ a << 2
+else
+ a + b
+End
+ assert_equal(expected, prog(9))
+ end
+
+ def test_10
+ expected = <<'End'.chomp
+if a == b
+then
+ a << 2
+else a + b
+End
+ assert_equal(expected, prog(10))
+ end
+
+ def test_11_31
+ expected = <<'End'.chomp
+if a == b
+then a << 2
+else a + b
+End
+ assert_equal(expected, prog(11))
+ assert_equal(expected, prog(15))
+ assert_equal(expected, prog(31))
+ end
+
+ def test_32
+ expected = <<'End'.chomp
+if a == b then a << 2 else a + b
+End
+ assert_equal(expected, prog(32))
+ end
+
+ end
+
+ class TailGroup < Test::Unit::TestCase # :nodoc:
+ def test_1
+ out = PrettyPrint.format('', 10) {|q|
+ q.group {
+ q.group {
+ q.text "abc"
+ q.breakable
+ q.text "def"
+ }
+ q.group {
+ q.text "ghi"
+ q.breakable
+ q.text "jkl"
+ }
+ }
+ }
+ assert_equal("abc defghi\njkl", out)
+ end
+ end
+
+ class NonString < Test::Unit::TestCase # :nodoc:
+ def format(width)
+ PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|q|
+ q.text(3, 3)
+ q.breakable(1, 1)
+ q.text(3, 3)
+ }
+ end
+
+ def test_6
+ assert_equal([3, "newline", "0 spaces", 3], format(6))
+ end
+
+ def test_7
+ assert_equal([3, 1, 3], format(7))
+ end
+
+ end
+
+ class Fill < Test::Unit::TestCase # :nodoc:
+ def format(width)
+ PrettyPrint.format('', width) {|q|
+ q.group {
+ q.text 'abc'
+ q.fill_breakable
+ q.text 'def'
+ q.fill_breakable
+ q.text 'ghi'
+ q.fill_breakable
+ q.text 'jkl'
+ q.fill_breakable
+ q.text 'mno'
+ q.fill_breakable
+ q.text 'pqr'
+ q.fill_breakable
+ q.text 'stu'
+ }
+ }
+ end
+
+ def test_00_06
+ expected = <<'End'.chomp
+abc
+def
+ghi
+jkl
+mno
+pqr
+stu
+End
+ assert_equal(expected, format(0))
+ assert_equal(expected, format(6))
+ end
+
+ def test_07_10
+ expected = <<'End'.chomp
+abc def
+ghi jkl
+mno pqr
+stu
+End
+ assert_equal(expected, format(7))
+ assert_equal(expected, format(10))
+ end
+
+ def test_11_14
+ expected = <<'End'.chomp
+abc def ghi
+jkl mno pqr
+stu
+End
+ assert_equal(expected, format(11))
+ assert_equal(expected, format(14))
+ end
+
+ def test_15_18
+ expected = <<'End'.chomp
+abc def ghi jkl
+mno pqr stu
+End
+ assert_equal(expected, format(15))
+ assert_equal(expected, format(18))
+ end
+
+ def test_19_22
+ expected = <<'End'.chomp
+abc def ghi jkl mno
+pqr stu
+End
+ assert_equal(expected, format(19))
+ assert_equal(expected, format(22))
+ end
+
+ def test_23_26
+ expected = <<'End'.chomp
+abc def ghi jkl mno pqr
+stu
+End
+ assert_equal(expected, format(23))
+ assert_equal(expected, format(26))
+ end
+
+ def test_27
+ expected = <<'End'.chomp
+abc def ghi jkl mno pqr stu
+End
+ assert_equal(expected, format(27))
+ end
+
+ end
+end
diff --git a/ruby/lib/prime.rb b/ruby/lib/prime.rb
new file mode 100644
index 0000000..8846b14
--- /dev/null
+++ b/ruby/lib/prime.rb
@@ -0,0 +1,471 @@
+#
+# = prime.rb
+#
+# Prime numbers and factorization library.
+#
+# Copyright::
+# Copyright (c) 1998-2008 Keiju ISHITSUKA(SHL Japan Inc.)
+# Copyright (c) 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp>
+#
+# Documentation::
+# Yuki Sonoda
+#
+
+require "singleton"
+require "forwardable"
+
+class Integer
+ # Re-composes a prime factorization and returns the product.
+ #
+ # See Prime#int_from_prime_division for more details.
+ def Integer.from_prime_division(pd)
+ Prime.int_from_prime_division(pd)
+ end
+
+ # Returns the factorization of +self+.
+ #
+ # See Prime#prime_division for more details.
+ def prime_division(generator = Prime::Generator23.new)
+ Prime.prime_division(self, generator)
+ end
+
+ # Returns true if +self+ is a prime number, false for a composite.
+ def prime?
+ Prime.prime?(self)
+ end
+
+ # Iterates the given block over all prime numbers.
+ #
+ # See +Prime+#each for more details.
+ def Integer.each_prime(ubound, &block) # :yields: prime
+ Prime.each(ubound, &block)
+ end
+end
+
+#
+# The set of all prime numbers.
+#
+# == Example
+# Prime.each(100) do |prime|
+# p prime #=> 2, 3, 5, 7, 11, ...., 97
+# end
+#
+# == Retrieving the instance
+# +Prime+.new is obsolete. Now +Prime+ has the default instance and you can
+# access it as +Prime+.instance.
+#
+# For convenience, each instance method of +Prime+.instance can be accessed
+# as a class method of +Prime+.
+#
+# e.g.
+# Prime.instance.prime?(2) #=> true
+# Prime.prime?(2) #=> true
+#
+# == Generators
+# A "generator" provides an implementation of enumerating pseudo-prime
+# numbers and it remembers the position of enumeration and upper bound.
+# Futhermore, it is a external iterator of prime enumeration which is
+# compatible to an Enumerator.
+#
+# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.
+# There are few implementations of generator.
+#
+# [+Prime+::+EratosthenesGenerator+]
+# Uses eratosthenes's sieve.
+# [+Prime+::+TrialDivisionGenerator+]
+# Uses the trial division method.
+# [+Prime+::+Generator23+]
+# Generates all positive integers which is not divided by 2 nor 3.
+# This sequence is very bad as a pseudo-prime sequence. But this
+# is faster and uses much less memory than other generators. So,
+# it is suitable for factorizing an integer which is not large but
+# has many prime factors. e.g. for Prime#prime? .
+class Prime
+ include Enumerable
+ @the_instance = Prime.new
+
+ # obsolete. Use +Prime+::+instance+ or class methods of +Prime+.
+ def initialize
+ @generator = EratosthenesGenerator.new
+ extend OldCompatibility
+ warn "Prime::new is obsolete. use Prime::instance or class methods of Prime."
+ end
+
+ class << self
+ extend Forwardable
+ include Enumerable
+ # Returns the default instance of Prime.
+ def instance; @the_instance end
+
+ def method_added(method) # :nodoc:
+ (class<< self;self;end).def_delegator :instance, method
+ end
+ end
+
+ # Iterates the given block over all prime numbers.
+ #
+ # == Parameters
+ # +ubound+::
+ # Optional. An arbitrary positive number.
+ # The upper bound of enumeration. The method enumerates
+ # prime numbers infinitely if +ubound+ is nil.
+ # +generator+::
+ # Optional. An implementation of pseudo-prime generator.
+ #
+ # == Return value
+ # An evaluated value of the given block at the last time.
+ # Or an enumerator which is compatible to an +Enumerator+
+ # if no block given.
+ #
+ # == Description
+ # Calls +block+ once for each prime number, passing the prime as
+ # a parameter.
+ #
+ # +ubound+::
+ # Upper bound of prime numbers. The iterator stops after
+ # yields all prime numbers p <= +ubound+.
+ #
+ # == Note
+ # +Prime+.+new+ returns a object extended by +Prime+::+OldCompatibility+
+ # in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten
+ # by +Prime+::+OldCompatibility+#+each+.
+ #
+ # +Prime+.+new+ is now obsolete. Use +Prime+.+instance+.+each+ or simply
+ # +Prime+.+each+.
+ def each(ubound = nil, generator = EratosthenesGenerator.new, &block)
+ generator.upper_bound = ubound
+ generator.each(&block)
+ end
+
+
+ # Returns true if +value+ is prime, false for a composite.
+ #
+ # == Parameters
+ # +value+:: an arbitrary integer to be checked.
+ # +generator+:: optional. A pseudo-prime generator.
+ def prime?(value, generator = Prime::Generator23.new)
+ value = -value if value < 0
+ return false if value < 2
+ for num in generator
+ q,r = value.divmod num
+ return true if q < num
+ return false if r == 0
+ end
+ end
+
+ # Re-composes a prime factorization and returns the product.
+ #
+ # == Parameters
+ # +pd+:: Array of pairs of integers. The each internal
+ # pair consists of a prime number -- a prime factor --
+ # and a natural number -- an exponent.
+ #
+ # == Example
+ # For [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]], it returns
+ # p_1**e_1 * p_2**e_2 * .... * p_n**e_n.
+ #
+ # Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12
+ def int_from_prime_division(pd)
+ pd.inject(1){|value, (prime, index)|
+ value *= prime**index
+ }
+ end
+
+ # Returns the factorization of +value+.
+ #
+ # == Parameters
+ # +value+:: An arbitrary integer.
+ # +generator+:: Optional. A pseudo-prime generator.
+ # +generator+.succ must return the next
+ # pseudo-prime number in the ascendent
+ # order. It must generate all prime numbers,
+ # but may generate non prime numbers.
+ #
+ # === Exceptions
+ # +ZeroDivisionError+:: when +value+ is zero.
+ #
+ # == Example
+ # For an arbitrary integer
+ # n = p_1**e_1 * p_2**e_2 * .... * p_n**e_n,
+ # prime_division(n) returns
+ # [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]].
+ #
+ # Prime.prime_division(12) #=> [[2,2], [3,1]]
+ #
+ def prime_division(value, generator= Prime::Generator23.new)
+ raise ZeroDivisionError if value == 0
+ if value < 0
+ value = -value
+ pv = [[-1, 1]]
+ else
+ pv = []
+ end
+ for prime in generator
+ count = 0
+ while (value1, mod = value.divmod(prime)
+ mod) == 0
+ value = value1
+ count += 1
+ end
+ if count != 0
+ pv.push [prime, count]
+ end
+ break if value1 <= prime
+ end
+ if value > 1
+ pv.push [value, 1]
+ end
+ return pv
+ end
+
+ # An abstract class for enumerating pseudo-prime numbers.
+ #
+ # Concrete subclasses should override succ, next, rewind.
+ class PseudoPrimeGenerator
+ include Enumerable
+
+ def initialize(ubound = nil)
+ @ubound = ubound
+ end
+
+ def upper_bound=(ubound)
+ @ubound = ubound
+ end
+ def upper_bound
+ @ubound
+ end
+
+ # returns the next pseudo-prime number, and move the internal
+ # position forward.
+ #
+ # +PseudoPrimeGenerator+#succ raises +NotImplementedError+.
+ def succ
+ raise NotImplementedError, "need to define `succ'"
+ end
+
+ # alias of +succ+.
+ def next
+ raise NotImplementedError, "need to define `next'"
+ end
+
+ # Rewinds the internal position for enumeration.
+ #
+ # See +Enumerator+#rewind.
+ def rewind
+ raise NotImplementedError, "need to define `rewind'"
+ end
+
+ # Iterates the given block for each prime numbers.
+ def each(&block)
+ return self.dup unless block
+ if @ubound
+ last_value = nil
+ loop do
+ prime = succ
+ break last_value if prime > @ubound
+ last_value = block.call(prime)
+ end
+ else
+ loop do
+ block.call(succ)
+ end
+ end
+ end
+
+ # see +Enumerator+#with_index.
+ alias with_index each_with_index
+
+ # see +Enumerator+#with_object.
+ def with_object(obj)
+ return enum_for(:with_object) unless block_given?
+ each do |prime|
+ yield prime, obj
+ end
+ end
+ end
+
+ # An implementation of +PseudoPrimeGenerator+.
+ #
+ # Uses +EratosthenesSieve+.
+ class EratosthenesGenerator < PseudoPrimeGenerator
+ def initialize
+ @last_prime = nil
+ super
+ end
+
+ def succ
+ @last_prime = @last_prime ? EratosthenesSieve.instance.next_to(@last_prime) : 2
+ end
+ def rewind
+ initialize
+ end
+ alias next succ
+ end
+
+ # An implementation of +PseudoPrimeGenerator+ which uses
+ # a prime table generated by trial division.
+ class TrialDivisionGenerator<PseudoPrimeGenerator
+ def initialize
+ @index = -1
+ super
+ end
+
+ def succ
+ TrialDivision.instance[@index += 1]
+ end
+ def rewind
+ initialize
+ end
+ alias next succ
+ end
+
+ # Generates all integer which are greater than 2 and
+ # are not divided by 2 nor 3.
+ #
+ # This is a pseudo-prime generator, suitable on
+ # checking primality of a integer by brute force
+ # method.
+ class Generator23<PseudoPrimeGenerator
+ def initialize
+ @prime = 1
+ @step = nil
+ super
+ end
+
+ def succ
+ loop do
+ if (@step)
+ @prime += @step
+ @step = 6 - @step
+ else
+ case @prime
+ when 1; @prime = 2
+ when 2; @prime = 3
+ when 3; @prime = 5; @step = 2
+ end
+ end
+ return @prime
+ end
+ end
+ alias next succ
+ def rewind
+ initialize
+ end
+ end
+
+
+
+
+ # Internal use. An implementation of prime table by trial division method.
+ class TrialDivision
+ include Singleton
+
+ def initialize # :nodoc:
+ # These are included as class variables to cache them for later uses. If memory
+ # usage is a problem, they can be put in Prime#initialize as instance variables.
+
+ # There must be no primes between @primes[-1] and @next_to_check.
+ @primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
+ # @next_to_check % 6 must be 1.
+ @next_to_check = 103 # @primes[-1] - @primes[-1] % 6 + 7
+ @ulticheck_index = 3 # @primes.index(@primes.reverse.find {|n|
+ # n < Math.sqrt(@@next_to_check) })
+ @ulticheck_next_squared = 121 # @primes[@ulticheck_index + 1] ** 2
+ end
+
+ # Returns the cached prime numbers.
+ def cache
+ return @primes
+ end
+ alias primes cache
+ alias primes_so_far cache
+
+ # Returns the +index+th prime number.
+ #
+ # +index+ is a 0-based index.
+ def [](index)
+ while index >= @primes.length
+ # Only check for prime factors up to the square root of the potential primes,
+ # but without the performance hit of an actual square root calculation.
+ if @next_to_check + 4 > @ulticheck_next_squared
+ @ulticheck_index += 1
+ @ulticheck_next_squared = @primes.at(@ulticheck_index + 1) ** 2
+ end
+ # Only check numbers congruent to one and five, modulo six. All others
+
+ # are divisible by two or three. This also allows us to skip checking against
+ # two and three.
+ @primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
+ @next_to_check += 4
+ @primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
+ @next_to_check += 2
+ end
+ return @primes[index]
+ end
+ end
+
+ # Internal use. An implementation of eratosthenes's sieve
+ class EratosthenesSieve
+ include Singleton
+
+ def initialize # :nodoc:
+ # bitmap for odd prime numbers less than 256.
+ # For an arbitrary odd number n, @table[i][j] is 1 when n is prime where i,j = n.divmod(32) .
+ @table = [0xcb6e, 0x64b4, 0x129a, 0x816d, 0x4c32, 0x864a, 0x820d, 0x2196]
+ end
+
+ # returns the least odd prime number which is greater than +n+.
+ def next_to(n)
+ n = (n-1).div(2)*2+3 # the next odd number of given n
+ i,j = n.divmod(32)
+ loop do
+ extend_table until @table.length > i
+ if !@table[i].zero?
+ (j...32).step(2) do |k|
+ return 32*i+k if !@table[i][k.div(2)].zero?
+ end
+ end
+ i += 1; j = 1
+ end
+ end
+
+ private
+ def extend_table
+ orig_len = @table.length
+ new_len = [orig_len**2, orig_len+256].min
+ lbound = orig_len*32
+ ubound = new_len*32
+ @table.fill(0xFFFF, orig_len...new_len)
+ (3..Integer(Math.sqrt(ubound))).step(2) do |p|
+ i, j = p.divmod(32)
+ next if @table[i][j.div(2)].zero?
+
+ start = (lbound.div(2*p)*2+1)*p # odd multiple of p which is greater than or equal to lbound
+ (start...ubound).step(2*p) do |n|
+ i, j = n.divmod(32)
+ @table[i] &= 0xFFFF ^ (1<<(j.div(2)))
+ end
+ end
+ end
+ end
+
+ # Provides a +Prime+ object with compatibility to Ruby 1.8 when instanciated via +Prime+.+new+.
+ module OldCompatibility
+ # Returns the next prime number and forwards internal pointer.
+ def succ
+ @generator.succ
+ end
+ alias next succ
+
+ # Overwrites Prime#each.
+ #
+ # Iterates the given block over all prime numbers. Note that enumeration starts from
+ # the current position of internal pointer, not rewound.
+ def each(&block)
+ return @generator.dup unless block_given?
+ loop do
+ yield succ
+ end
+ end
+ end
+end
diff --git a/ruby/lib/profile.rb b/ruby/lib/profile.rb
new file mode 100644
index 0000000..2aeecce
--- /dev/null
+++ b/ruby/lib/profile.rb
@@ -0,0 +1,10 @@
+require 'profiler'
+
+RubyVM::InstructionSequence.compile_option = {
+ :trace_instruction => true,
+ :specialized_instruction => false
+}
+END {
+ Profiler__::print_profile(STDERR)
+}
+Profiler__::start_profile
diff --git a/ruby/lib/profiler.rb b/ruby/lib/profiler.rb
new file mode 100644
index 0000000..a4b8889
--- /dev/null
+++ b/ruby/lib/profiler.rb
@@ -0,0 +1,59 @@
+module Profiler__
+ # internal values
+ @@start = @@stack = @@map = nil
+ PROFILE_PROC = proc{|event, file, line, id, binding, klass|
+ case event
+ when "call", "c-call"
+ now = Process.times[0]
+ @@stack.push [now, 0.0]
+ when "return", "c-return"
+ now = Process.times[0]
+ key = [klass, id]
+ if tick = @@stack.pop
+ data = (@@map[key] ||= [0, 0.0, 0.0, key])
+ data[0] += 1
+ cost = now - tick[0]
+ data[1] += cost
+ data[2] += cost - tick[1]
+ @@stack[-1][1] += cost if @@stack[-1]
+ end
+ end
+ }
+module_function
+ def start_profile
+ @@start = Process.times[0]
+ @@stack = []
+ @@map = {}
+ set_trace_func PROFILE_PROC
+ end
+ def stop_profile
+ set_trace_func nil
+ end
+ def print_profile(f)
+ stop_profile
+ total = Process.times[0] - @@start
+ if total == 0 then total = 0.01 end
+ data = @@map.values
+ data = data.sort_by{|x| -x[2]}
+ sum = 0
+ f.printf " %% cumulative self self total\n"
+ f.printf " time seconds seconds calls ms/call ms/call name\n"
+ for d in data
+ sum += d[2]
+ f.printf "%6.2f %8.2f %8.2f %8d ", d[2]/total*100, sum, d[2], d[0]
+ f.printf "%8.2f %8.2f %s\n", d[2]*1000/d[0], d[1]*1000/d[0], get_name(*d[3])
+ end
+ f.printf "%6.2f %8.2f %8.2f %8d ", 0.0, total, 0.0, 1 # ???
+ f.printf "%8.2f %8.2f %s\n", 0.0, total*1000, "#toplevel" # ???
+ end
+ def get_name(klass, id)
+ name = klass.to_s || ""
+ if klass.kind_of? Class
+ name += "#"
+ else
+ name += "."
+ end
+ name + id.id2name
+ end
+ private :get_name
+end
diff --git a/ruby/lib/pstore.rb b/ruby/lib/pstore.rb
new file mode 100644
index 0000000..947dc1d
--- /dev/null
+++ b/ruby/lib/pstore.rb
@@ -0,0 +1,543 @@
+# = PStore -- Transactional File Storage for Ruby Objects
+#
+# pstore.rb -
+# originally by matz
+# documentation by Kev Jackson and James Edward Gray II
+# improved by Hongli Lai
+#
+# See PStore for documentation.
+
+
+require "fileutils"
+require "digest/md5"
+require "thread"
+
+#
+# PStore implements a file based persistence mechanism based on a Hash. User
+# code can store hierarchies of Ruby objects (values) into the data store file
+# by name (keys). An object hierarchy may be just a single object. User code
+# may later read values back from the data store or even update data, as needed.
+#
+# The transactional behavior ensures that any changes succeed or fail together.
+# This can be used to ensure that the data store is not left in a transitory
+# state, where some values were updated but others were not.
+#
+# Behind the scenes, Ruby objects are stored to the data store file with
+# Marshal. That carries the usual limitations. Proc objects cannot be
+# marshalled, for example.
+#
+# == Usage example:
+#
+# require "pstore"
+#
+# # a mock wiki object...
+# class WikiPage
+# def initialize( page_name, author, contents )
+# @page_name = page_name
+# @revisions = Array.new
+#
+# add_revision(author, contents)
+# end
+#
+# attr_reader :page_name
+#
+# def add_revision( author, contents )
+# @revisions << { :created => Time.now,
+# :author => author,
+# :contents => contents }
+# end
+#
+# def wiki_page_references
+# [@page_name] + @revisions.last[:contents].scan(/\b(?:[A-Z]+[a-z]+){2,}/)
+# end
+#
+# # ...
+# end
+#
+# # create a new page...
+# home_page = WikiPage.new( "HomePage", "James Edward Gray II",
+# "A page about the JoysOfDocumentation..." )
+#
+# # then we want to update page data and the index together, or not at all...
+# wiki = PStore.new("wiki_pages.pstore")
+# wiki.transaction do # begin transaction; do all of this or none of it
+# # store page...
+# wiki[home_page.page_name] = home_page
+# # ensure that an index has been created...
+# wiki[:wiki_index] ||= Array.new
+# # update wiki index...
+# wiki[:wiki_index].push(*home_page.wiki_page_references)
+# end # commit changes to wiki data store file
+#
+# ### Some time later... ###
+#
+# # read wiki data...
+# wiki.transaction(true) do # begin read-only transaction, no changes allowed
+# wiki.roots.each do |data_root_name|
+# p data_root_name
+# p wiki[data_root_name]
+# end
+# end
+#
+# == Transaction modes
+#
+# By default, file integrity is only ensured as long as the operating system
+# (and the underlying hardware) doesn't raise any unexpected I/O errors. If an
+# I/O error occurs while PStore is writing to its file, then the file will
+# become corrupted.
+#
+# You can prevent this by setting <em>pstore.ultra_safe = true</em>.
+# However, this results in a minor performance loss, and only works on platforms
+# that support atomic file renames. Please consult the documentation for
+# +ultra_safe+ for details.
+#
+# Needless to say, if you're storing valuable data with PStore, then you should
+# backup the PStore files from time to time.
+class PStore
+ binmode = defined?(File::BINARY) ? File::BINARY : 0
+ RDWR_ACCESS = File::RDWR | File::CREAT | binmode
+ RD_ACCESS = File::RDONLY | binmode
+ WR_ACCESS = File::WRONLY | File::CREAT | File::TRUNC | binmode
+
+ # The error type thrown by all PStore methods.
+ class Error < StandardError
+ end
+
+ # Whether PStore should do its best to prevent file corruptions, even when under
+ # unlikely-to-occur error conditions such as out-of-space conditions and other
+ # unusual OS filesystem errors. Setting this flag comes at the price in the form
+ # of a performance loss.
+ #
+ # This flag only has effect on platforms on which file renames are atomic (e.g.
+ # all POSIX platforms: Linux, MacOS X, FreeBSD, etc). The default value is false.
+ attr_accessor :ultra_safe
+
+ #
+ # To construct a PStore object, pass in the _file_ path where you would like
+ # the data to be stored.
+ #
+ # PStore objects are always reentrant. But if _thread_safe_ is set to true,
+ # then it will become thread-safe at the cost of a minor performance hit.
+ #
+ def initialize(file, thread_safe = false)
+ dir = File::dirname(file)
+ unless File::directory? dir
+ raise PStore::Error, format("directory %s does not exist", dir)
+ end
+ if File::exist? file and not File::readable? file
+ raise PStore::Error, format("file %s not readable", file)
+ end
+ @transaction = false
+ @filename = file
+ @abort = false
+ @ultra_safe = false
+ if @thread_safe
+ @lock = Mutex.new
+ else
+ @lock = DummyMutex.new
+ end
+ end
+
+ # Raises PStore::Error if the calling code is not in a PStore#transaction.
+ def in_transaction
+ raise PStore::Error, "not in transaction" unless @transaction
+ end
+ #
+ # Raises PStore::Error if the calling code is not in a PStore#transaction or
+ # if the code is in a read-only PStore#transaction.
+ #
+ def in_transaction_wr()
+ in_transaction()
+ raise PStore::Error, "in read-only transaction" if @rdonly
+ end
+ private :in_transaction, :in_transaction_wr
+
+ #
+ # Retrieves a value from the PStore file data, by _name_. The hierarchy of
+ # Ruby objects stored under that root _name_ will be returned.
+ #
+ # *WARNING*: This method is only valid in a PStore#transaction. It will
+ # raise PStore::Error if called at any other time.
+ #
+ def [](name)
+ in_transaction
+ @table[name]
+ end
+ #
+ # This method is just like PStore#[], save that you may also provide a
+ # _default_ value for the object. In the event the specified _name_ is not
+ # found in the data store, your _default_ will be returned instead. If you do
+ # not specify a default, PStore::Error will be raised if the object is not
+ # found.
+ #
+ # *WARNING*: This method is only valid in a PStore#transaction. It will
+ # raise PStore::Error if called at any other time.
+ #
+ def fetch(name, default=PStore::Error)
+ in_transaction
+ unless @table.key? name
+ if default == PStore::Error
+ raise PStore::Error, format("undefined root name `%s'", name)
+ else
+ return default
+ end
+ end
+ @table[name]
+ end
+ #
+ # Stores an individual Ruby object or a hierarchy of Ruby objects in the data
+ # store file under the root _name_. Assigning to a _name_ already in the data
+ # store clobbers the old data.
+ #
+ # == Example:
+ #
+ # require "pstore"
+ #
+ # store = PStore.new("data_file.pstore")
+ # store.transaction do # begin transaction
+ # # load some data into the store...
+ # store[:single_object] = "My data..."
+ # store[:obj_heirarchy] = { "Kev Jackson" => ["rational.rb", "pstore.rb"],
+ # "James Gray" => ["erb.rb", "pstore.rb"] }
+ # end # commit changes to data store file
+ #
+ # *WARNING*: This method is only valid in a PStore#transaction and it cannot
+ # be read-only. It will raise PStore::Error if called at any other time.
+ #
+ def []=(name, value)
+ in_transaction_wr()
+ @table[name] = value
+ end
+ #
+ # Removes an object hierarchy from the data store, by _name_.
+ #
+ # *WARNING*: This method is only valid in a PStore#transaction and it cannot
+ # be read-only. It will raise PStore::Error if called at any other time.
+ #
+ def delete(name)
+ in_transaction_wr()
+ @table.delete name
+ end
+
+ #
+ # Returns the names of all object hierarchies currently in the store.
+ #
+ # *WARNING*: This method is only valid in a PStore#transaction. It will
+ # raise PStore::Error if called at any other time.
+ #
+ def roots
+ in_transaction
+ @table.keys
+ end
+ #
+ # Returns true if the supplied _name_ is currently in the data store.
+ #
+ # *WARNING*: This method is only valid in a PStore#transaction. It will
+ # raise PStore::Error if called at any other time.
+ #
+ def root?(name)
+ in_transaction
+ @table.key? name
+ end
+ # Returns the path to the data store file.
+ def path
+ @filename
+ end
+
+ #
+ # Ends the current PStore#transaction, committing any changes to the data
+ # store immediately.
+ #
+ # == Example:
+ #
+ # require "pstore"
+ #
+ # store = PStore.new("data_file.pstore")
+ # store.transaction do # begin transaction
+ # # load some data into the store...
+ # store[:one] = 1
+ # store[:two] = 2
+ #
+ # store.commit # end transaction here, committing changes
+ #
+ # store[:three] = 3 # this change is never reached
+ # end
+ #
+ # *WARNING*: This method is only valid in a PStore#transaction. It will
+ # raise PStore::Error if called at any other time.
+ #
+ def commit
+ in_transaction
+ @abort = false
+ throw :pstore_abort_transaction
+ end
+ #
+ # Ends the current PStore#transaction, discarding any changes to the data
+ # store.
+ #
+ # == Example:
+ #
+ # require "pstore"
+ #
+ # store = PStore.new("data_file.pstore")
+ # store.transaction do # begin transaction
+ # store[:one] = 1 # this change is not applied, see below...
+ # store[:two] = 2 # this change is not applied, see below...
+ #
+ # store.abort # end transaction here, discard all changes
+ #
+ # store[:three] = 3 # this change is never reached
+ # end
+ #
+ # *WARNING*: This method is only valid in a PStore#transaction. It will
+ # raise PStore::Error if called at any other time.
+ #
+ def abort
+ in_transaction
+ @abort = true
+ throw :pstore_abort_transaction
+ end
+
+ #
+ # Opens a new transaction for the data store. Code executed inside a block
+ # passed to this method may read and write data to and from the data store
+ # file.
+ #
+ # At the end of the block, changes are committed to the data store
+ # automatically. You may exit the transaction early with a call to either
+ # PStore#commit or PStore#abort. See those methods for details about how
+ # changes are handled. Raising an uncaught Exception in the block is
+ # equivalent to calling PStore#abort.
+ #
+ # If _read_only_ is set to +true+, you will only be allowed to read from the
+ # data store during the transaction and any attempts to change the data will
+ # raise a PStore::Error.
+ #
+ # Note that PStore does not support nested transactions.
+ #
+ def transaction(read_only = false, &block) # :yields: pstore
+ value = nil
+ raise PStore::Error, "nested transaction" if @transaction
+ @lock.synchronize do
+ @rdonly = read_only
+ @transaction = true
+ @abort = false
+ file = open_and_lock_file(@filename, read_only)
+ if file
+ begin
+ @table, checksum, original_data_size = load_data(file, read_only)
+
+ catch(:pstore_abort_transaction) do
+ value = yield(self)
+ end
+
+ if !@abort && !read_only
+ save_data(checksum, original_data_size, file)
+ end
+ ensure
+ file.close if !file.closed?
+ end
+ else
+ # This can only occur if read_only == true.
+ @table = {}
+ catch(:pstore_abort_transaction) do
+ value = yield(self)
+ end
+ end
+ end
+ value
+ ensure
+ @transaction = false
+ end
+
+ private
+ # Constant for relieving Ruby's garbage collector.
+ EMPTY_STRING = ""
+ EMPTY_MARSHAL_DATA = Marshal.dump({})
+ EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA)
+
+ class DummyMutex
+ def synchronize
+ yield
+ end
+ end
+
+ #
+ # Open the specified filename (either in read-only mode or in
+ # read-write mode) and lock it for reading or writing.
+ #
+ # The opened File object will be returned. If _read_only_ is true,
+ # and the file does not exist, then nil will be returned.
+ #
+ # All exceptions are propagated.
+ #
+ def open_and_lock_file(filename, read_only)
+ if read_only
+ begin
+ file = File.new(filename, RD_ACCESS)
+ begin
+ file.flock(File::LOCK_SH)
+ return file
+ rescue
+ file.close
+ raise
+ end
+ rescue Errno::ENOENT
+ return nil
+ end
+ else
+ file = File.new(filename, RDWR_ACCESS)
+ file.flock(File::LOCK_EX)
+ return file
+ end
+ end
+
+ # Load the given PStore file.
+ # If +read_only+ is true, the unmarshalled Hash will be returned.
+ # If +read_only+ is false, a 3-tuple will be returned: the unmarshalled
+ # Hash, an MD5 checksum of the data, and the size of the data.
+ def load_data(file, read_only)
+ if read_only
+ begin
+ table = load(file)
+ if !table.is_a?(Hash)
+ raise Error, "PStore file seems to be corrupted."
+ end
+ rescue EOFError
+ # This seems to be a newly-created file.
+ table = {}
+ end
+ table
+ else
+ data = file.read
+ if data.empty?
+ # This seems to be a newly-created file.
+ table = {}
+ checksum = empty_marshal_checksum
+ size = empty_marshal_data.size
+ else
+ table = load(data)
+ checksum = Digest::MD5.digest(data)
+ size = data.size
+ if !table.is_a?(Hash)
+ raise Error, "PStore file seems to be corrupted."
+ end
+ end
+ data.replace(EMPTY_STRING)
+ [table, checksum, size]
+ end
+ end
+
+ def on_windows?
+ is_windows = RUBY_PLATFORM =~ /mswin/ ||
+ RUBY_PLATFORM =~ /mingw/ ||
+ RUBY_PLATFORM =~ /bccwin/ ||
+ RUBY_PLATFORM =~ /wince/
+ self.class.__send__(:define_method, :on_windows?) do
+ is_windows
+ end
+ is_windows
+ end
+
+ # Check whether Marshal.dump supports the 'canonical' option. This option
+ # makes sure that Marshal.dump always dumps data structures in the same order.
+ # This is important because otherwise, the checksums that we generate may differ.
+ def marshal_dump_supports_canonical_option?
+ begin
+ Marshal.dump(nil, -1, true)
+ result = true
+ rescue
+ result = false
+ end
+ self.class.__send__(:define_method, :marshal_dump_supports_canonical_option?) do
+ result
+ end
+ result
+ end
+
+ def save_data(original_checksum, original_file_size, file)
+ # We only want to save the new data if the size or checksum has changed.
+ # This results in less filesystem calls, which is good for performance.
+ if marshal_dump_supports_canonical_option?
+ new_data = Marshal.dump(@table, -1, true)
+ else
+ new_data = dump(@table)
+ end
+ new_checksum = Digest::MD5.digest(new_data)
+
+ if new_data.size != original_file_size || new_checksum != original_checksum
+ if @ultra_safe && !on_windows?
+ # Windows doesn't support atomic file renames.
+ save_data_with_atomic_file_rename_strategy(new_data, file)
+ else
+ save_data_with_fast_strategy(new_data, file)
+ end
+ end
+
+ new_data.replace(EMPTY_STRING)
+ end
+
+ def save_data_with_atomic_file_rename_strategy(data, file)
+ temp_filename = "#{@filename}.tmp.#{Process.pid}.#{rand 1000000}"
+ temp_file = File.new(temp_filename, WR_ACCESS)
+ begin
+ temp_file.flock(File::LOCK_EX)
+ temp_file.write(data)
+ temp_file.flush
+ File.rename(temp_filename, @filename)
+ rescue
+ File.unlink(temp_file) rescue nil
+ raise
+ ensure
+ temp_file.close
+ end
+ end
+
+ def save_data_with_fast_strategy(data, file)
+ file.rewind
+ file.truncate(0)
+ file.write(data)
+ end
+
+
+ # This method is just a wrapped around Marshal.dump
+ # to allow subclass overriding used in YAML::Store.
+ def dump(table) # :nodoc:
+ Marshal::dump(table)
+ end
+
+ # This method is just a wrapped around Marshal.load.
+ # to allow subclass overriding used in YAML::Store.
+ def load(content) # :nodoc:
+ Marshal::load(content)
+ end
+
+ def empty_marshal_data
+ EMPTY_MARSHAL_DATA
+ end
+ def empty_marshal_checksum
+ EMPTY_MARSHAL_CHECKSUM
+ end
+end
+
+# :enddoc:
+
+if __FILE__ == $0
+ db = PStore.new("/tmp/foo")
+ db.transaction do
+ p db.roots
+ ary = db["root"] = [1,2,3,4]
+ ary[1] = [1,1.5]
+ end
+
+ 1000.times do
+ db.transaction do
+ db["root"][0] += 1
+ p db["root"][0]
+ end
+ end
+
+ db.transaction(true) do
+ p db["root"]
+ end
+end
diff --git a/ruby/lib/racc/parser.rb b/ruby/lib/racc/parser.rb
new file mode 100644
index 0000000..e87a250
--- /dev/null
+++ b/ruby/lib/racc/parser.rb
@@ -0,0 +1,441 @@
+#
+# $originalId: parser.rb,v 1.8 2006/07/06 11:42:07 aamine Exp $
+#
+# Copyright (c) 1999-2006 Minero Aoki
+#
+# This program is free software.
+# You can distribute/modify this program under the same terms of ruby.
+#
+# As a special exception, when this code is copied by Racc
+# into a Racc output file, you may use that output file
+# without restriction.
+#
+
+unless defined?(NotImplementedError)
+ NotImplementedError = NotImplementError
+end
+
+module Racc
+ class ParseError < StandardError; end
+end
+unless defined?(::ParseError)
+ ParseError = Racc::ParseError
+end
+
+module Racc
+
+ unless defined?(Racc_No_Extentions)
+ Racc_No_Extentions = false
+ end
+
+ class Parser
+
+ Racc_Runtime_Version = '1.4.5'
+ Racc_Runtime_Revision = '$originalRevision: 1.8 $'.split[1]
+
+ Racc_Runtime_Core_Version_R = '1.4.5'
+ Racc_Runtime_Core_Revision_R = '$originalRevision: 1.8 $'.split[1]
+ begin
+ require 'racc/cparse'
+ # Racc_Runtime_Core_Version_C = (defined in extention)
+ Racc_Runtime_Core_Revision_C = Racc_Runtime_Core_Id_C.split[2]
+ unless new.respond_to?(:_racc_do_parse_c, true)
+ raise LoadError, 'old cparse.so'
+ end
+ if Racc_No_Extentions
+ raise LoadError, 'selecting ruby version of racc runtime core'
+ end
+
+ Racc_Main_Parsing_Routine = :_racc_do_parse_c
+ Racc_YY_Parse_Method = :_racc_yyparse_c
+ Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_C
+ Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_C
+ Racc_Runtime_Type = 'c'
+ rescue LoadError
+ Racc_Main_Parsing_Routine = :_racc_do_parse_rb
+ Racc_YY_Parse_Method = :_racc_yyparse_rb
+ Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_R
+ Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_R
+ Racc_Runtime_Type = 'ruby'
+ end
+
+ def Parser.racc_runtime_type
+ Racc_Runtime_Type
+ end
+
+ private
+
+ def _racc_setup
+ @yydebug = false unless self.class::Racc_debug_parser
+ @yydebug = false unless defined?(@yydebug)
+ if @yydebug
+ @racc_debug_out = $stderr unless defined?(@racc_debug_out)
+ @racc_debug_out ||= $stderr
+ end
+ arg = self.class::Racc_arg
+ arg[13] = true if arg.size < 14
+ arg
+ end
+
+ def _racc_init_sysvars
+ @racc_state = [0]
+ @racc_tstack = []
+ @racc_vstack = []
+
+ @racc_t = nil
+ @racc_val = nil
+
+ @racc_read_next = true
+
+ @racc_user_yyerror = false
+ @racc_error_status = 0
+ end
+
+ ###
+ ### do_parse
+ ###
+
+ def do_parse
+ __send__(Racc_Main_Parsing_Routine, _racc_setup(), false)
+ end
+
+ def next_token
+ raise NotImplementedError, "#{self.class}\#next_token is not defined"
+ end
+
+ def _racc_do_parse_rb(arg, in_debug)
+ action_table, action_check, action_default, action_pointer,
+ goto_table, goto_check, goto_default, goto_pointer,
+ nt_base, reduce_table, token_table, shift_n,
+ reduce_n, use_result, * = arg
+
+ _racc_init_sysvars
+ tok = act = i = nil
+ nerr = 0
+
+ catch(:racc_end_parse) {
+ while true
+ if i = action_pointer[@racc_state[-1]]
+ if @racc_read_next
+ if @racc_t != 0 # not EOF
+ tok, @racc_val = next_token()
+ unless tok # EOF
+ @racc_t = 0
+ else
+ @racc_t = (token_table[tok] or 1) # error token
+ end
+ racc_read_token(@racc_t, tok, @racc_val) if @yydebug
+ @racc_read_next = false
+ end
+ end
+ i += @racc_t
+ unless i >= 0 and
+ act = action_table[i] and
+ action_check[i] == @racc_state[-1]
+ act = action_default[@racc_state[-1]]
+ end
+ else
+ act = action_default[@racc_state[-1]]
+ end
+ while act = _racc_evalact(act, arg)
+ ;
+ end
+ end
+ }
+ end
+
+ ###
+ ### yyparse
+ ###
+
+ def yyparse(recv, mid)
+ __send__(Racc_YY_Parse_Method, recv, mid, _racc_setup(), true)
+ end
+
+ def _racc_yyparse_rb(recv, mid, arg, c_debug)
+ action_table, action_check, action_default, action_pointer,
+ goto_table, goto_check, goto_default, goto_pointer,
+ nt_base, reduce_table, token_table, shift_n,
+ reduce_n, use_result, * = arg
+
+ _racc_init_sysvars
+ act = nil
+ i = nil
+ nerr = 0
+
+ catch(:racc_end_parse) {
+ until i = action_pointer[@racc_state[-1]]
+ while act = _racc_evalact(action_default[@racc_state[-1]], arg)
+ ;
+ end
+ end
+ recv.__send__(mid) do |tok, val|
+ unless tok
+ @racc_t = 0
+ else
+ @racc_t = (token_table[tok] or 1) # error token
+ end
+ @racc_val = val
+ @racc_read_next = false
+
+ i += @racc_t
+ unless i >= 0 and
+ act = action_table[i] and
+ action_check[i] == @racc_state[-1]
+ act = action_default[@racc_state[-1]]
+ end
+ while act = _racc_evalact(act, arg)
+ ;
+ end
+
+ while not(i = action_pointer[@racc_state[-1]]) or
+ not @racc_read_next or
+ @racc_t == 0 # $
+ unless i and i += @racc_t and
+ i >= 0 and
+ act = action_table[i] and
+ action_check[i] == @racc_state[-1]
+ act = action_default[@racc_state[-1]]
+ end
+ while act = _racc_evalact(act, arg)
+ ;
+ end
+ end
+ end
+ }
+ end
+
+ ###
+ ### common
+ ###
+
+ def _racc_evalact(act, arg)
+ action_table, action_check, action_default, action_pointer,
+ goto_table, goto_check, goto_default, goto_pointer,
+ nt_base, reduce_table, token_table, shift_n,
+ reduce_n, use_result, * = arg
+ nerr = 0 # tmp
+
+ if act > 0 and act < shift_n
+ #
+ # shift
+ #
+ if @racc_error_status > 0
+ @racc_error_status -= 1 unless @racc_t == 1 # error token
+ end
+ @racc_vstack.push @racc_val
+ @racc_state.push act
+ @racc_read_next = true
+ if @yydebug
+ @racc_tstack.push @racc_t
+ racc_shift @racc_t, @racc_tstack, @racc_vstack
+ end
+
+ elsif act < 0 and act > -reduce_n
+ #
+ # reduce
+ #
+ code = catch(:racc_jump) {
+ @racc_state.push _racc_do_reduce(arg, act)
+ false
+ }
+ if code
+ case code
+ when 1 # yyerror
+ @racc_user_yyerror = true # user_yyerror
+ return -reduce_n
+ when 2 # yyaccept
+ return shift_n
+ else
+ raise '[Racc Bug] unknown jump code'
+ end
+ end
+
+ elsif act == shift_n
+ #
+ # accept
+ #
+ racc_accept if @yydebug
+ throw :racc_end_parse, @racc_vstack[0]
+
+ elsif act == -reduce_n
+ #
+ # error
+ #
+ case @racc_error_status
+ when 0
+ unless arg[21] # user_yyerror
+ nerr += 1
+ on_error @racc_t, @racc_val, @racc_vstack
+ end
+ when 3
+ if @racc_t == 0 # is $
+ throw :racc_end_parse, nil
+ end
+ @racc_read_next = true
+ end
+ @racc_user_yyerror = false
+ @racc_error_status = 3
+ while true
+ if i = action_pointer[@racc_state[-1]]
+ i += 1 # error token
+ if i >= 0 and
+ (act = action_table[i]) and
+ action_check[i] == @racc_state[-1]
+ break
+ end
+ end
+ throw :racc_end_parse, nil if @racc_state.size <= 1
+ @racc_state.pop
+ @racc_vstack.pop
+ if @yydebug
+ @racc_tstack.pop
+ racc_e_pop @racc_state, @racc_tstack, @racc_vstack
+ end
+ end
+ return act
+
+ else
+ raise "[Racc Bug] unknown action #{act.inspect}"
+ end
+
+ racc_next_state(@racc_state[-1], @racc_state) if @yydebug
+
+ nil
+ end
+
+ def _racc_do_reduce(arg, act)
+ action_table, action_check, action_default, action_pointer,
+ goto_table, goto_check, goto_default, goto_pointer,
+ nt_base, reduce_table, token_table, shift_n,
+ reduce_n, use_result, * = arg
+ state = @racc_state
+ vstack = @racc_vstack
+ tstack = @racc_tstack
+
+ i = act * -3
+ len = reduce_table[i]
+ reduce_to = reduce_table[i+1]
+ method_id = reduce_table[i+2]
+ void_array = []
+
+ tmp_t = tstack[-len, len] if @yydebug
+ tmp_v = vstack[-len, len]
+ tstack[-len, len] = void_array if @yydebug
+ vstack[-len, len] = void_array
+ state[-len, len] = void_array
+
+ # tstack must be updated AFTER method call
+ if use_result
+ vstack.push __send__(method_id, tmp_v, vstack, tmp_v[0])
+ else
+ vstack.push __send__(method_id, tmp_v, vstack)
+ end
+ tstack.push reduce_to
+
+ racc_reduce(tmp_t, reduce_to, tstack, vstack) if @yydebug
+
+ k1 = reduce_to - nt_base
+ if i = goto_pointer[k1]
+ i += state[-1]
+ if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1
+ return curstate
+ end
+ end
+ goto_default[k1]
+ end
+
+ def on_error(t, val, vstack)
+ raise ParseError, sprintf("\nparse error on value %s (%s)",
+ val.inspect, token_to_str(t) || '?')
+ end
+
+ def yyerror
+ throw :racc_jump, 1
+ end
+
+ def yyaccept
+ throw :racc_jump, 2
+ end
+
+ def yyerrok
+ @racc_error_status = 0
+ end
+
+ #
+ # for debugging output
+ #
+
+ def racc_read_token(t, tok, val)
+ @racc_debug_out.print 'read '
+ @racc_debug_out.print tok.inspect, '(', racc_token2str(t), ') '
+ @racc_debug_out.puts val.inspect
+ @racc_debug_out.puts
+ end
+
+ def racc_shift(tok, tstack, vstack)
+ @racc_debug_out.puts "shift #{racc_token2str tok}"
+ racc_print_stacks tstack, vstack
+ @racc_debug_out.puts
+ end
+
+ def racc_reduce(toks, sim, tstack, vstack)
+ out = @racc_debug_out
+ out.print 'reduce '
+ if toks.empty?
+ out.print ' <none>'
+ else
+ toks.each {|t| out.print ' ', racc_token2str(t) }
+ end
+ out.puts " --> #{racc_token2str(sim)}"
+
+ racc_print_stacks tstack, vstack
+ @racc_debug_out.puts
+ end
+
+ def racc_accept
+ @racc_debug_out.puts 'accept'
+ @racc_debug_out.puts
+ end
+
+ def racc_e_pop(state, tstack, vstack)
+ @racc_debug_out.puts 'error recovering mode: pop token'
+ racc_print_states state
+ racc_print_stacks tstack, vstack
+ @racc_debug_out.puts
+ end
+
+ def racc_next_state(curstate, state)
+ @racc_debug_out.puts "goto #{curstate}"
+ racc_print_states state
+ @racc_debug_out.puts
+ end
+
+ def racc_print_stacks(t, v)
+ out = @racc_debug_out
+ out.print ' ['
+ t.each_index do |i|
+ out.print ' (', racc_token2str(t[i]), ' ', v[i].inspect, ')'
+ end
+ out.puts ' ]'
+ end
+
+ def racc_print_states(s)
+ out = @racc_debug_out
+ out.print ' ['
+ s.each {|st| out.print ' ', st }
+ out.puts ' ]'
+ end
+
+ def racc_token2str(tok)
+ self.class::Racc_token_to_s_table[tok] or
+ raise "[Racc Bug] can't convert token #{tok} to string"
+ end
+
+ def token_to_str(t)
+ self.class::Racc_token_to_s_table[t]
+ end
+
+ end
+
+end
diff --git a/ruby/lib/rake.rb b/ruby/lib/rake.rb
new file mode 100644
index 0000000..d46c49d
--- /dev/null
+++ b/ruby/lib/rake.rb
@@ -0,0 +1,2465 @@
+#!/usr/bin/env ruby
+
+#--
+
+# Copyright (c) 2003, 2004, 2005, 2006, 2007 Jim Weirich
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to
+# deal in the Software without restriction, including without limitation the
+# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+#++
+#
+# = Rake -- Ruby Make
+#
+# This is the main file for the Rake application. Normally it is referenced
+# as a library via a require statement, but it can be distributed
+# independently as an application.
+
+RAKEVERSION = '0.8.3'
+
+require 'rbconfig'
+require 'fileutils'
+require 'singleton'
+require 'monitor'
+require 'optparse'
+require 'ostruct'
+
+require 'rake/win32'
+
+######################################################################
+# Rake extensions to Module.
+#
+class Module
+ # Check for an existing method in the current class before extending. IF
+ # the method already exists, then a warning is printed and the extension is
+ # not added. Otherwise the block is yielded and any definitions in the
+ # block will take effect.
+ #
+ # Usage:
+ #
+ # class String
+ # rake_extension("xyz") do
+ # def xyz
+ # ...
+ # end
+ # end
+ # end
+ #
+ def rake_extension(method)
+ if method_defined?(method)
+ $stderr.puts "WARNING: Possible conflict with Rake extension: #{self}##{method} already exists"
+ else
+ yield
+ end
+ end
+end # module Module
+
+
+######################################################################
+# User defined methods to be added to String.
+#
+class String
+ rake_extension("ext") do
+ # Replace the file extension with +newext+. If there is no extenson on
+ # the string, append the new extension to the end. If the new extension
+ # is not given, or is the empty string, remove any existing extension.
+ #
+ # +ext+ is a user added method for the String class.
+ def ext(newext='')
+ return self.dup if ['.', '..'].include? self
+ if newext != ''
+ newext = (newext =~ /^\./) ? newext : ("." + newext)
+ end
+ self.chomp(File.extname(self)) << newext
+ end
+ end
+
+ rake_extension("pathmap") do
+ # Explode a path into individual components. Used by +pathmap+.
+ def pathmap_explode
+ head, tail = File.split(self)
+ return [self] if head == self
+ return [tail] if head == '.' || tail == '/'
+ return [head, tail] if head == '/'
+ return head.pathmap_explode + [tail]
+ end
+ protected :pathmap_explode
+
+ # Extract a partial path from the path. Include +n+ directories from the
+ # front end (left hand side) if +n+ is positive. Include |+n+|
+ # directories from the back end (right hand side) if +n+ is negative.
+ def pathmap_partial(n)
+ dirs = File.dirname(self).pathmap_explode
+ partial_dirs =
+ if n > 0
+ dirs[0...n]
+ elsif n < 0
+ dirs.reverse[0...-n].reverse
+ else
+ "."
+ end
+ File.join(partial_dirs)
+ end
+ protected :pathmap_partial
+
+ # Preform the pathmap replacement operations on the given path. The
+ # patterns take the form 'pat1,rep1;pat2,rep2...'.
+ def pathmap_replace(patterns, &block)
+ result = self
+ patterns.split(';').each do |pair|
+ pattern, replacement = pair.split(',')
+ pattern = Regexp.new(pattern)
+ if replacement == '*' && block_given?
+ result = result.sub(pattern, &block)
+ elsif replacement
+ result = result.sub(pattern, replacement)
+ else
+ result = result.sub(pattern, '')
+ end
+ end
+ result
+ end
+ protected :pathmap_replace
+
+ # Map the path according to the given specification. The specification
+ # controls the details of the mapping. The following special patterns are
+ # recognized:
+ #
+ # * <b>%p</b> -- The complete path.
+ # * <b>%f</b> -- The base file name of the path, with its file extension,
+ # but without any directories.
+ # * <b>%n</b> -- The file name of the path without its file extension.
+ # * <b>%d</b> -- The directory list of the path.
+ # * <b>%x</b> -- The file extension of the path. An empty string if there
+ # is no extension.
+ # * <b>%X</b> -- Everything *but* the file extension.
+ # * <b>%s</b> -- The alternate file separater if defined, otherwise use
+ # the standard file separator.
+ # * <b>%%</b> -- A percent sign.
+ #
+ # The %d specifier can also have a numeric prefix (e.g. '%2d'). If the
+ # number is positive, only return (up to) +n+ directories in the path,
+ # starting from the left hand side. If +n+ is negative, return (up to)
+ # |+n+| directories from the right hand side of the path.
+ #
+ # Examples:
+ #
+ # 'a/b/c/d/file.txt'.pathmap("%2d") => 'a/b'
+ # 'a/b/c/d/file.txt'.pathmap("%-2d") => 'c/d'
+ #
+ # Also the %d, %p, %f, %n, %x, and %X operators can take a
+ # pattern/replacement argument to perform simple string substititions on a
+ # particular part of the path. The pattern and replacement are speparated
+ # by a comma and are enclosed by curly braces. The replacement spec comes
+ # after the % character but before the operator letter. (e.g.
+ # "%{old,new}d"). Muliple replacement specs should be separated by
+ # semi-colons (e.g. "%{old,new;src,bin}d").
+ #
+ # Regular expressions may be used for the pattern, and back refs may be
+ # used in the replacement text. Curly braces, commas and semi-colons are
+ # excluded from both the pattern and replacement text (let's keep parsing
+ # reasonable).
+ #
+ # For example:
+ #
+ # "src/org/onestepback/proj/A.java".pathmap("%{^src,bin}X.class")
+ #
+ # returns:
+ #
+ # "bin/org/onestepback/proj/A.class"
+ #
+ # If the replacement text is '*', then a block may be provided to perform
+ # some arbitrary calculation for the replacement.
+ #
+ # For example:
+ #
+ # "/path/to/file.TXT".pathmap("%X%{.*,*}x") { |ext|
+ # ext.downcase
+ # }
+ #
+ # Returns:
+ #
+ # "/path/to/file.txt"
+ #
+ def pathmap(spec=nil, &block)
+ return self if spec.nil?
+ result = ''
+ spec.scan(/%\{[^}]*\}-?\d*[sdpfnxX%]|%-?\d+d|%.|[^%]+/) do |frag|
+ case frag
+ when '%f'
+ result << File.basename(self)
+ when '%n'
+ result << File.basename(self, '.*')
+ when '%d'
+ result << File.dirname(self)
+ when '%x'
+ result << File.extname(self)
+ when '%X'
+ result << self.ext
+ when '%p'
+ result << self
+ when '%s'
+ result << (File::ALT_SEPARATOR || File::SEPARATOR)
+ when '%-'
+ # do nothing
+ when '%%'
+ result << "%"
+ when /%(-?\d+)d/
+ result << pathmap_partial($1.to_i)
+ when /^%\{([^}]*)\}(\d*[dpfnxX])/
+ patterns, operator = $1, $2
+ result << pathmap('%' + operator).pathmap_replace(patterns, &block)
+ when /^%/
+ fail ArgumentError, "Unknown pathmap specifier #{frag} in '#{spec}'"
+ else
+ result << frag
+ end
+ end
+ result
+ end
+ end
+end # class String
+
+##############################################################################
+module Rake
+
+ # Errors -----------------------------------------------------------
+
+ # Error indicating an ill-formed task declaration.
+ class TaskArgumentError < ArgumentError
+ end
+
+ # Error indicating a recursion overflow error in task selection.
+ class RuleRecursionOverflowError < StandardError
+ def initialize(*args)
+ super
+ @targets = []
+ end
+
+ def add_target(target)
+ @targets << target
+ end
+
+ def message
+ super + ": [" + @targets.reverse.join(' => ') + "]"
+ end
+ end
+
+ # --------------------------------------------------------------------------
+ # Rake module singleton methods.
+ #
+ class << self
+ # Current Rake Application
+ def application
+ @application ||= Rake::Application.new
+ end
+
+ # Set the current Rake application object.
+ def application=(app)
+ @application = app
+ end
+
+ # Return the original directory where the Rake application was started.
+ def original_dir
+ application.original_dir
+ end
+
+ end
+
+ # ##########################################################################
+ # Mixin for creating easily cloned objects.
+ #
+ module Cloneable
+ # Clone an object by making a new object and setting all the instance
+ # variables to the same values.
+ def dup
+ sibling = self.class.new
+ instance_variables.each do |ivar|
+ value = self.instance_variable_get(ivar)
+ new_value = value.clone rescue value
+ sibling.instance_variable_set(ivar, new_value)
+ end
+ sibling.taint if tainted?
+ sibling
+ end
+
+ def clone
+ sibling = dup
+ sibling.freeze if frozen?
+ sibling
+ end
+ end
+
+ ####################################################################
+ # TaskAguments manage the arguments passed to a task.
+ #
+ class TaskArguments
+ include Enumerable
+
+ attr_reader :names
+
+ # Create a TaskArgument object with a list of named arguments
+ # (given by :names) and a set of associated values (given by
+ # :values). :parent is the parent argument object.
+ def initialize(names, values, parent=nil)
+ @names = names
+ @parent = parent
+ @hash = {}
+ names.each_with_index { |name, i|
+ @hash[name.to_sym] = values[i] unless values[i].nil?
+ }
+ end
+
+ # Create a new argument scope using the prerequisite argument
+ # names.
+ def new_scope(names)
+ values = names.collect { |n| self[n] }
+ self.class.new(names, values, self)
+ end
+
+ # Find an argument value by name or index.
+ def [](index)
+ lookup(index.to_sym)
+ end
+
+ # Specify a hash of default values for task arguments. Use the
+ # defaults only if there is no specific value for the given
+ # argument.
+ def with_defaults(defaults)
+ @hash = defaults.merge(@hash)
+ end
+
+ def each(&block)
+ @hash.each(&block)
+ end
+
+ def method_missing(sym, *args, &block)
+ lookup(sym.to_sym)
+ end
+
+ def to_hash
+ @hash
+ end
+
+ def to_s
+ @hash.inspect
+ end
+
+ def inspect
+ to_s
+ end
+
+ protected
+
+ def lookup(name)
+ if @hash.has_key?(name)
+ @hash[name]
+ elsif ENV.has_key?(name.to_s)
+ ENV[name.to_s]
+ elsif ENV.has_key?(name.to_s.upcase)
+ ENV[name.to_s.upcase]
+ elsif @parent
+ @parent.lookup(name)
+ end
+ end
+ end
+
+ EMPTY_TASK_ARGS = TaskArguments.new([], [])
+
+ ####################################################################
+ # InvocationChain tracks the chain of task invocations to detect
+ # circular dependencies.
+ class InvocationChain
+ def initialize(value, tail)
+ @value = value
+ @tail = tail
+ end
+
+ def member?(obj)
+ @value == obj || @tail.member?(obj)
+ end
+
+ def append(value)
+ if member?(value)
+ fail RuntimeError, "Circular dependency detected: #{to_s} => #{value}"
+ end
+ self.class.new(value, self)
+ end
+
+ def to_s
+ "#{prefix}#{@value}"
+ end
+
+ def self.append(value, chain)
+ chain.append(value)
+ end
+
+ private
+
+ def prefix
+ "#{@tail.to_s} => "
+ end
+
+ class EmptyInvocationChain
+ def member?(obj)
+ false
+ end
+ def append(value)
+ InvocationChain.new(value, self)
+ end
+ def to_s
+ "TOP"
+ end
+ end
+
+ EMPTY = EmptyInvocationChain.new
+
+ end # class InvocationChain
+
+end # module Rake
+
+module Rake
+
+ # #########################################################################
+ # A Task is the basic unit of work in a Rakefile. Tasks have associated
+ # actions (possibly more than one) and a list of prerequisites. When
+ # invoked, a task will first ensure that all of its prerequisites have an
+ # opportunity to run and then it will execute its own actions.
+ #
+ # Tasks are not usually created directly using the new method, but rather
+ # use the +file+ and +task+ convenience methods.
+ #
+ class Task
+ # List of prerequisites for a task.
+ attr_reader :prerequisites
+
+ # List of actions attached to a task.
+ attr_reader :actions
+
+ # Application owning this task.
+ attr_accessor :application
+
+ # Comment for this task. Restricted to a single line of no more than 50
+ # characters.
+ attr_reader :comment
+
+ # Full text of the (possibly multi-line) comment.
+ attr_reader :full_comment
+
+ # Array of nested namespaces names used for task lookup by this task.
+ attr_reader :scope
+
+ # Return task name
+ def to_s
+ name
+ end
+
+ def inspect
+ "<#{self.class} #{name} => [#{prerequisites.join(', ')}]>"
+ end
+
+ # List of sources for task.
+ attr_writer :sources
+ def sources
+ @sources ||= []
+ end
+
+ # First source from a rule (nil if no sources)
+ def source
+ @sources.first if defined?(@sources)
+ end
+
+ # Create a task named +task_name+ with no actions or prerequisites. Use
+ # +enhance+ to add actions and prerequisites.
+ def initialize(task_name, app)
+ @name = task_name.to_s
+ @prerequisites = []
+ @actions = []
+ @already_invoked = false
+ @full_comment = nil
+ @comment = nil
+ @lock = Monitor.new
+ @application = app
+ @scope = app.current_scope
+ @arg_names = nil
+ end
+
+ # Enhance a task with prerequisites or actions. Returns self.
+ def enhance(deps=nil, &block)
+ @prerequisites |= deps if deps
+ @actions << block if block_given?
+ self
+ end
+
+ # Name of the task, including any namespace qualifiers.
+ def name
+ @name.to_s
+ end
+
+ # Name of task with argument list description.
+ def name_with_args # :nodoc:
+ if arg_description
+ "#{name}#{arg_description}"
+ else
+ name
+ end
+ end
+
+ # Argument description (nil if none).
+ def arg_description # :nodoc:
+ @arg_names ? "[#{(arg_names || []).join(',')}]" : nil
+ end
+
+ # Name of arguments for this task.
+ def arg_names
+ @arg_names || []
+ end
+
+ # Reenable the task, allowing its tasks to be executed if the task
+ # is invoked again.
+ def reenable
+ @already_invoked = false
+ end
+
+ # Clear the existing prerequisites and actions of a rake task.
+ def clear
+ clear_prerequisites
+ clear_actions
+ self
+ end
+
+ # Clear the existing prerequisites of a rake task.
+ def clear_prerequisites
+ prerequisites.clear
+ self
+ end
+
+ # Clear the existing actions on a rake task.
+ def clear_actions
+ actions.clear
+ self
+ end
+
+ # Invoke the task if it is needed. Prerequites are invoked first.
+ def invoke(*args)
+ task_args = TaskArguments.new(arg_names, args)
+ invoke_with_call_chain(task_args, InvocationChain::EMPTY)
+ end
+
+ # Same as invoke, but explicitly pass a call chain to detect
+ # circular dependencies.
+ def invoke_with_call_chain(task_args, invocation_chain) # :nodoc:
+ new_chain = InvocationChain.append(self, invocation_chain)
+ @lock.synchronize do
+ if application.options.trace
+ puts "** Invoke #{name} #{format_trace_flags}"
+ end
+ return if @already_invoked
+ @already_invoked = true
+ invoke_prerequisites(task_args, new_chain)
+ execute(task_args) if needed?
+ end
+ end
+ protected :invoke_with_call_chain
+
+ # Invoke all the prerequisites of a task.
+ def invoke_prerequisites(task_args, invocation_chain) # :nodoc:
+ @prerequisites.each { |n|
+ prereq = application[n, @scope]
+ prereq_args = task_args.new_scope(prereq.arg_names)
+ prereq.invoke_with_call_chain(prereq_args, invocation_chain)
+ }
+ end
+
+ # Format the trace flags for display.
+ def format_trace_flags
+ flags = []
+ flags << "first_time" unless @already_invoked
+ flags << "not_needed" unless needed?
+ flags.empty? ? "" : "(" + flags.join(", ") + ")"
+ end
+ private :format_trace_flags
+
+ # Execute the actions associated with this task.
+ def execute(args=nil)
+ args ||= EMPTY_TASK_ARGS
+ if application.options.dryrun
+ puts "** Execute (dry run) #{name}"
+ return
+ end
+ if application.options.trace
+ puts "** Execute #{name}"
+ end
+ application.enhance_with_matching_rule(name) if @actions.empty?
+ @actions.each do |act|
+ case act.arity
+ when 1
+ act.call(self)
+ else
+ act.call(self, args)
+ end
+ end
+ end
+
+ # Is this task needed?
+ def needed?
+ true
+ end
+
+ # Timestamp for this task. Basic tasks return the current time for their
+ # time stamp. Other tasks can be more sophisticated.
+ def timestamp
+ @prerequisites.collect { |p| application[p].timestamp }.max || Time.now
+ end
+
+ # Add a description to the task. The description can consist of an option
+ # argument list (enclosed brackets) and an optional comment.
+ def add_description(description)
+ return if ! description
+ comment = description.strip
+ add_comment(comment) if comment && ! comment.empty?
+ end
+
+ # Writing to the comment attribute is the same as adding a description.
+ def comment=(description)
+ add_description(description)
+ end
+
+ # Add a comment to the task. If a comment alread exists, separate
+ # the new comment with " / ".
+ def add_comment(comment)
+ if @full_comment
+ @full_comment << " / "
+ else
+ @full_comment = ''
+ end
+ @full_comment << comment
+ if @full_comment =~ /\A([^.]+?\.)( |$)/
+ @comment = $1
+ else
+ @comment = @full_comment
+ end
+ end
+ private :add_comment
+
+ # Set the names of the arguments for this task. +args+ should be
+ # an array of symbols, one for each argument name.
+ def set_arg_names(args)
+ @arg_names = args.map { |a| a.to_sym }
+ end
+
+ # Return a string describing the internal state of a task. Useful for
+ # debugging.
+ def investigation
+ result = "------------------------------\n"
+ result << "Investigating #{name}\n"
+ result << "class: #{self.class}\n"
+ result << "task needed: #{needed?}\n"
+ result << "timestamp: #{timestamp}\n"
+ result << "pre-requisites: \n"
+ prereqs = @prerequisites.collect {|name| application[name]}
+ prereqs.sort! {|a,b| a.timestamp <=> b.timestamp}
+ prereqs.each do |p|
+ result << "--#{p.name} (#{p.timestamp})\n"
+ end
+ latest_prereq = @prerequisites.collect{|n| application[n].timestamp}.max
+ result << "latest-prerequisite time: #{latest_prereq}\n"
+ result << "................................\n\n"
+ return result
+ end
+
+ # ----------------------------------------------------------------
+ # Rake Module Methods
+ #
+ class << self
+
+ # Clear the task list. This cause rake to immediately forget all the
+ # tasks that have been assigned. (Normally used in the unit tests.)
+ def clear
+ Rake.application.clear
+ end
+
+ # List of all defined tasks.
+ def tasks
+ Rake.application.tasks
+ end
+
+ # Return a task with the given name. If the task is not currently
+ # known, try to synthesize one from the defined rules. If no rules are
+ # found, but an existing file matches the task name, assume it is a file
+ # task with no dependencies or actions.
+ def [](task_name)
+ Rake.application[task_name]
+ end
+
+ # TRUE if the task name is already defined.
+ def task_defined?(task_name)
+ Rake.application.lookup(task_name) != nil
+ end
+
+ # Define a task given +args+ and an option block. If a rule with the
+ # given name already exists, the prerequisites and actions are added to
+ # the existing task. Returns the defined task.
+ def define_task(*args, &block)
+ Rake.application.define_task(self, *args, &block)
+ end
+
+ # Define a rule for synthesizing tasks.
+ def create_rule(*args, &block)
+ Rake.application.create_rule(*args, &block)
+ end
+
+ # Apply the scope to the task name according to the rules for
+ # this kind of task. Generic tasks will accept the scope as
+ # part of the name.
+ def scope_name(scope, task_name)
+ (scope + [task_name]).join(':')
+ end
+
+ end # class << Rake::Task
+ end # class Rake::Task
+
+
+ # #########################################################################
+ # A FileTask is a task that includes time based dependencies. If any of a
+ # FileTask's prerequisites have a timestamp that is later than the file
+ # represented by this task, then the file must be rebuilt (using the
+ # supplied actions).
+ #
+ class FileTask < Task
+
+ # Is this file task needed? Yes if it doesn't exist, or if its time stamp
+ # is out of date.
+ def needed?
+ return true unless File.exist?(name)
+ return true if out_of_date?(timestamp)
+ false
+ end
+
+ # Time stamp for file task.
+ def timestamp
+ if File.exist?(name)
+ File.mtime(name.to_s)
+ else
+ Rake::EARLY
+ end
+ end
+
+ private
+
+ # Are there any prerequisites with a later time than the given time stamp?
+ def out_of_date?(stamp)
+ @prerequisites.any? { |n| application[n].timestamp > stamp}
+ end
+
+ # ----------------------------------------------------------------
+ # Task class methods.
+ #
+ class << self
+ # Apply the scope to the task name according to the rules for this kind
+ # of task. File based tasks ignore the scope when creating the name.
+ def scope_name(scope, task_name)
+ task_name
+ end
+ end
+ end # class Rake::FileTask
+
+ # #########################################################################
+ # A FileCreationTask is a file task that when used as a dependency will be
+ # needed if and only if the file has not been created. Once created, it is
+ # not re-triggered if any of its dependencies are newer, nor does trigger
+ # any rebuilds of tasks that depend on it whenever it is updated.
+ #
+ class FileCreationTask < FileTask
+ # Is this file task needed? Yes if it doesn't exist.
+ def needed?
+ ! File.exist?(name)
+ end
+
+ # Time stamp for file creation task. This time stamp is earlier
+ # than any other time stamp.
+ def timestamp
+ Rake::EARLY
+ end
+ end
+
+ # #########################################################################
+ # Same as a regular task, but the immediate prerequisites are done in
+ # parallel using Ruby threads.
+ #
+ class MultiTask < Task
+ private
+ def invoke_prerequisites(args, invocation_chain)
+ threads = @prerequisites.collect { |p|
+ Thread.new(p) { |r| application[r].invoke_with_call_chain(args, invocation_chain) }
+ }
+ threads.each { |t| t.join }
+ end
+ end
+end # module Rake
+
+# ###########################################################################
+# Task Definition Functions ...
+
+# Declare a basic task.
+#
+# Example:
+# task :clobber => [:clean] do
+# rm_rf "html"
+# end
+#
+def task(*args, &block)
+ Rake::Task.define_task(*args, &block)
+end
+
+
+# Declare a file task.
+#
+# Example:
+# file "config.cfg" => ["config.template"] do
+# open("config.cfg", "w") do |outfile|
+# open("config.template") do |infile|
+# while line = infile.gets
+# outfile.puts line
+# end
+# end
+# end
+# end
+#
+def file(*args, &block)
+ Rake::FileTask.define_task(*args, &block)
+end
+
+# Declare a file creation task.
+# (Mainly used for the directory command).
+def file_create(args, &block)
+ Rake::FileCreationTask.define_task(args, &block)
+end
+
+# Declare a set of files tasks to create the given directories on demand.
+#
+# Example:
+# directory "testdata/doc"
+#
+def directory(dir)
+ Rake.each_dir_parent(dir) do |d|
+ file_create d do |t|
+ mkdir_p t.name if ! File.exist?(t.name)
+ end
+ end
+end
+
+# Declare a task that performs its prerequisites in parallel. Multitasks does
+# *not* guarantee that its prerequisites will execute in any given order
+# (which is obvious when you think about it)
+#
+# Example:
+# multitask :deploy => [:deploy_gem, :deploy_rdoc]
+#
+def multitask(args, &block)
+ Rake::MultiTask.define_task(args, &block)
+end
+
+# Create a new rake namespace and use it for evaluating the given block.
+# Returns a NameSpace object that can be used to lookup tasks defined in the
+# namespace.
+#
+# E.g.
+#
+# ns = namespace "nested" do
+# task :run
+# end
+# task_run = ns[:run] # find :run in the given namespace.
+#
+def namespace(name=nil, &block)
+ Rake.application.in_namespace(name, &block)
+end
+
+# Declare a rule for auto-tasks.
+#
+# Example:
+# rule '.o' => '.c' do |t|
+# sh %{cc -o #{t.name} #{t.source}}
+# end
+#
+def rule(*args, &block)
+ Rake::Task.create_rule(*args, &block)
+end
+
+# Describe the next rake task.
+#
+# Example:
+# desc "Run the Unit Tests"
+# task :test => [:build]
+# runtests
+# end
+#
+def desc(description)
+ Rake.application.last_description = description
+end
+
+# Import the partial Rakefiles +fn+. Imported files are loaded _after_ the
+# current file is completely loaded. This allows the import statement to
+# appear anywhere in the importing file, and yet allowing the imported files
+# to depend on objects defined in the importing file.
+#
+# A common use of the import statement is to include files containing
+# dependency declarations.
+#
+# See also the --rakelibdir command line option.
+#
+# Example:
+# import ".depend", "my_rules"
+#
+def import(*fns)
+ fns.each do |fn|
+ Rake.application.add_import(fn)
+ end
+end
+
+# ###########################################################################
+# This a FileUtils extension that defines several additional commands to be
+# added to the FileUtils utility functions.
+#
+module FileUtils
+ RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']).
+ sub(/.*\s.*/m, '"\&"')
+
+ OPT_TABLE['sh'] = %w(noop verbose)
+ OPT_TABLE['ruby'] = %w(noop verbose)
+
+ # Run the system command +cmd+. If multiple arguments are given the command
+ # is not run with the shell (same semantics as Kernel::exec and
+ # Kernel::system).
+ #
+ # Example:
+ # sh %{ls -ltr}
+ #
+ # sh 'ls', 'file with spaces'
+ #
+ # # check exit status after command runs
+ # sh %{grep pattern file} do |ok, res|
+ # if ! ok
+ # puts "pattern not found (status = #{res.exitstatus})"
+ # end
+ # end
+ #
+ def sh(*cmd, &block)
+ options = (Hash === cmd.last) ? cmd.pop : {}
+ unless block_given?
+ show_command = cmd.join(" ")
+ show_command = show_command[0,42] + "..."
+ # TODO code application logic heref show_command.length > 45
+ block = lambda { |ok, status|
+ ok or fail "Command failed with status (#{status.exitstatus}): [#{show_command}]"
+ }
+ end
+ if RakeFileUtils.verbose_flag == :default
+ options[:verbose] = false
+ else
+ options[:verbose] ||= RakeFileUtils.verbose_flag
+ end
+ options[:noop] ||= RakeFileUtils.nowrite_flag
+ rake_check_options options, :noop, :verbose
+ rake_output_message cmd.join(" ") if options[:verbose]
+ unless options[:noop]
+ res = rake_system(*cmd)
+ block.call(res, $?)
+ end
+ end
+
+ def rake_system(*cmd)
+ system(*cmd)
+ end
+ private :rake_system
+
+ # Run a Ruby interpreter with the given arguments.
+ #
+ # Example:
+ # ruby %{-pe '$_.upcase!' <README}
+ #
+ def ruby(*args,&block)
+ options = (Hash === args.last) ? args.pop : {}
+ if args.length > 1 then
+ sh(*([RUBY] + args + [options]), &block)
+ else
+ sh("#{RUBY} #{args.first}", options, &block)
+ end
+ end
+
+ LN_SUPPORTED = [true]
+
+ # Attempt to do a normal file link, but fall back to a copy if the link
+ # fails.
+ def safe_ln(*args)
+ unless LN_SUPPORTED[0]
+ cp(*args)
+ else
+ begin
+ ln(*args)
+ rescue StandardError, NotImplementedError => ex
+ LN_SUPPORTED[0] = false
+ cp(*args)
+ end
+ end
+ end
+
+ # Split a file path into individual directory names.
+ #
+ # Example:
+ # split_all("a/b/c") => ['a', 'b', 'c']
+ #
+ def split_all(path)
+ head, tail = File.split(path)
+ return [tail] if head == '.' || tail == '/'
+ return [head, tail] if head == '/'
+ return split_all(head) + [tail]
+ end
+end
+
+# ###########################################################################
+# RakeFileUtils provides a custom version of the FileUtils methods that
+# respond to the <tt>verbose</tt> and <tt>nowrite</tt> commands.
+#
+module RakeFileUtils
+ include FileUtils
+
+ class << self
+ attr_accessor :verbose_flag, :nowrite_flag
+ end
+ RakeFileUtils.verbose_flag = :default
+ RakeFileUtils.nowrite_flag = false
+
+ $fileutils_verbose = true
+ $fileutils_nowrite = false
+
+ FileUtils::OPT_TABLE.each do |name, opts|
+ default_options = []
+ if opts.include?(:verbose) || opts.include?("verbose")
+ default_options << ':verbose => RakeFileUtils.verbose_flag'
+ end
+ if opts.include?(:noop) || opts.include?("noop")
+ default_options << ':noop => RakeFileUtils.nowrite_flag'
+ end
+
+ next if default_options.empty?
+ module_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{name}( *args, &block )
+ super(
+ *rake_merge_option(args,
+ #{default_options.join(', ')}
+ ), &block)
+ end
+ EOS
+ end
+
+ # Get/set the verbose flag controlling output from the FileUtils utilities.
+ # If verbose is true, then the utility method is echoed to standard output.
+ #
+ # Examples:
+ # verbose # return the current value of the verbose flag
+ # verbose(v) # set the verbose flag to _v_.
+ # verbose(v) { code } # Execute code with the verbose flag set temporarily to _v_.
+ # # Return to the original value when code is done.
+ def verbose(value=nil)
+ oldvalue = RakeFileUtils.verbose_flag
+ RakeFileUtils.verbose_flag = value unless value.nil?
+ if block_given?
+ begin
+ yield
+ ensure
+ RakeFileUtils.verbose_flag = oldvalue
+ end
+ end
+ RakeFileUtils.verbose_flag
+ end
+
+ # Get/set the nowrite flag controlling output from the FileUtils utilities.
+ # If verbose is true, then the utility method is echoed to standard output.
+ #
+ # Examples:
+ # nowrite # return the current value of the nowrite flag
+ # nowrite(v) # set the nowrite flag to _v_.
+ # nowrite(v) { code } # Execute code with the nowrite flag set temporarily to _v_.
+ # # Return to the original value when code is done.
+ def nowrite(value=nil)
+ oldvalue = RakeFileUtils.nowrite_flag
+ RakeFileUtils.nowrite_flag = value unless value.nil?
+ if block_given?
+ begin
+ yield
+ ensure
+ RakeFileUtils.nowrite_flag = oldvalue
+ end
+ end
+ oldvalue
+ end
+
+ # Use this function to prevent protentially destructive ruby code from
+ # running when the :nowrite flag is set.
+ #
+ # Example:
+ #
+ # when_writing("Building Project") do
+ # project.build
+ # end
+ #
+ # The following code will build the project under normal conditions. If the
+ # nowrite(true) flag is set, then the example will print:
+ # DRYRUN: Building Project
+ # instead of actually building the project.
+ #
+ def when_writing(msg=nil)
+ if RakeFileUtils.nowrite_flag
+ puts "DRYRUN: #{msg}" if msg
+ else
+ yield
+ end
+ end
+
+ # Merge the given options with the default values.
+ def rake_merge_option(args, defaults)
+ if Hash === args.last
+ defaults.update(args.last)
+ args.pop
+ end
+ args.push defaults
+ args
+ end
+ private :rake_merge_option
+
+ # Send the message to the default rake output (which is $stderr).
+ def rake_output_message(message)
+ $stderr.puts(message)
+ end
+ private :rake_output_message
+
+ # Check that the options do not contain options not listed in +optdecl+. An
+ # ArgumentError exception is thrown if non-declared options are found.
+ def rake_check_options(options, *optdecl)
+ h = options.dup
+ optdecl.each do |name|
+ h.delete name
+ end
+ raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty?
+ end
+ private :rake_check_options
+
+ extend self
+end
+
+# ###########################################################################
+# Include the FileUtils file manipulation functions in the top level module,
+# but mark them private so that they don't unintentionally define methods on
+# other objects.
+
+include RakeFileUtils
+private(*FileUtils.instance_methods(false))
+private(*RakeFileUtils.instance_methods(false))
+
+######################################################################
+module Rake
+
+ # #########################################################################
+ # A FileList is essentially an array with a few helper methods defined to
+ # make file manipulation a bit easier.
+ #
+ # FileLists are lazy. When given a list of glob patterns for possible files
+ # to be included in the file list, instead of searching the file structures
+ # to find the files, a FileList holds the pattern for latter use.
+ #
+ # This allows us to define a number of FileList to match any number of
+ # files, but only search out the actual files when then FileList itself is
+ # actually used. The key is that the first time an element of the
+ # FileList/Array is requested, the pending patterns are resolved into a real
+ # list of file names.
+ #
+ class FileList
+
+ include Cloneable
+
+ # == Method Delegation
+ #
+ # The lazy evaluation magic of FileLists happens by implementing all the
+ # array specific methods to call +resolve+ before delegating the heavy
+ # lifting to an embedded array object (@items).
+ #
+ # In addition, there are two kinds of delegation calls. The regular kind
+ # delegates to the @items array and returns the result directly. Well,
+ # almost directly. It checks if the returned value is the @items object
+ # itself, and if so will return the FileList object instead.
+ #
+ # The second kind of delegation call is used in methods that normally
+ # return a new Array object. We want to capture the return value of these
+ # methods and wrap them in a new FileList object. We enumerate these
+ # methods in the +SPECIAL_RETURN+ list below.
+
+ # List of array methods (that are not in +Object+) that need to be
+ # delegated.
+ ARRAY_METHODS = (Array.instance_methods - Object.instance_methods).map { |n| n.to_s }
+
+ # List of additional methods that must be delegated.
+ MUST_DEFINE = %w[to_a inspect]
+
+ # List of methods that should not be delegated here (we define special
+ # versions of them explicitly below).
+ MUST_NOT_DEFINE = %w[to_a to_ary partition *]
+
+ # List of delegated methods that return new array values which need
+ # wrapping.
+ SPECIAL_RETURN = %w[
+ map collect sort sort_by select find_all reject grep
+ compact flatten uniq values_at
+ + - & |
+ ]
+
+ DELEGATING_METHODS = (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).collect{ |s| s.to_s }.sort.uniq
+
+ # Now do the delegation.
+ DELEGATING_METHODS.each_with_index do |sym, i|
+ if SPECIAL_RETURN.include?(sym)
+ ln = __LINE__+1
+ class_eval %{
+ def #{sym}(*args, &block)
+ resolve
+ result = @items.send(:#{sym}, *args, &block)
+ FileList.new.import(result)
+ end
+ }, __FILE__, ln
+ else
+ ln = __LINE__+1
+ class_eval %{
+ def #{sym}(*args, &block)
+ resolve
+ result = @items.send(:#{sym}, *args, &block)
+ result.object_id == @items.object_id ? self : result
+ end
+ }, __FILE__, ln
+ end
+ end
+
+ # Create a file list from the globbable patterns given. If you wish to
+ # perform multiple includes or excludes at object build time, use the
+ # "yield self" pattern.
+ #
+ # Example:
+ # file_list = FileList.new('lib/**/*.rb', 'test/test*.rb')
+ #
+ # pkg_files = FileList.new('lib/**/*') do |fl|
+ # fl.exclude(/\bCVS\b/)
+ # end
+ #
+ def initialize(*patterns)
+ @pending_add = []
+ @pending = false
+ @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
+ @exclude_procs = DEFAULT_IGNORE_PROCS.dup
+ @exclude_re = nil
+ @items = []
+ patterns.each { |pattern| include(pattern) }
+ yield self if block_given?
+ end
+
+ # Add file names defined by glob patterns to the file list. If an array
+ # is given, add each element of the array.
+ #
+ # Example:
+ # file_list.include("*.java", "*.cfg")
+ # file_list.include %w( math.c lib.h *.o )
+ #
+ def include(*filenames)
+ # TODO: check for pending
+ filenames.each do |fn|
+ if fn.respond_to? :to_ary
+ include(*fn.to_ary)
+ else
+ @pending_add << fn
+ end
+ end
+ @pending = true
+ self
+ end
+ alias :add :include
+
+ # Register a list of file name patterns that should be excluded from the
+ # list. Patterns may be regular expressions, glob patterns or regular
+ # strings. In addition, a block given to exclude will remove entries that
+ # return true when given to the block.
+ #
+ # Note that glob patterns are expanded against the file system. If a file
+ # is explicitly added to a file list, but does not exist in the file
+ # system, then an glob pattern in the exclude list will not exclude the
+ # file.
+ #
+ # Examples:
+ # FileList['a.c', 'b.c'].exclude("a.c") => ['b.c']
+ # FileList['a.c', 'b.c'].exclude(/^a/) => ['b.c']
+ #
+ # If "a.c" is a file, then ...
+ # FileList['a.c', 'b.c'].exclude("a.*") => ['b.c']
+ #
+ # If "a.c" is not a file, then ...
+ # FileList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
+ #
+ def exclude(*patterns, &block)
+ patterns.each do |pat|
+ @exclude_patterns << pat
+ end
+ if block_given?
+ @exclude_procs << block
+ end
+ resolve_exclude if ! @pending
+ self
+ end
+
+
+ # Clear all the exclude patterns so that we exclude nothing.
+ def clear_exclude
+ @exclude_patterns = []
+ @exclude_procs = []
+ calculate_exclude_regexp if ! @pending
+ self
+ end
+
+ # Define equality.
+ def ==(array)
+ to_ary == array
+ end
+
+ # Return the internal array object.
+ def to_a
+ resolve
+ @items
+ end
+
+ # Return the internal array object.
+ def to_ary
+ to_a
+ end
+
+ # Lie about our class.
+ def is_a?(klass)
+ klass == Array || super(klass)
+ end
+ alias kind_of? is_a?
+
+ # Redefine * to return either a string or a new file list.
+ def *(other)
+ result = @items * other
+ case result
+ when Array
+ FileList.new.import(result)
+ else
+ result
+ end
+ end
+
+ # Resolve all the pending adds now.
+ def resolve
+ if @pending
+ @pending = false
+ @pending_add.each do |fn| resolve_add(fn) end
+ @pending_add = []
+ resolve_exclude
+ end
+ self
+ end
+
+ def calculate_exclude_regexp
+ ignores = []
+ @exclude_patterns.each do |pat|
+ case pat
+ when Regexp
+ ignores << pat
+ when /[*?]/
+ Dir[pat].each do |p| ignores << p end
+ else
+ ignores << Regexp.quote(pat)
+ end
+ end
+ if ignores.empty?
+ @exclude_re = /^$/
+ else
+ re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
+ @exclude_re = Regexp.new(re_str)
+ end
+ end
+
+ def resolve_add(fn)
+ case fn
+ when %r{[*?\[\{]}
+ add_matching(fn)
+ else
+ self << fn
+ end
+ end
+ private :resolve_add
+
+ def resolve_exclude
+ calculate_exclude_regexp
+ reject! { |fn| exclude?(fn) }
+ self
+ end
+ private :resolve_exclude
+
+ # Return a new FileList with the results of running +sub+ against each
+ # element of the oringal list.
+ #
+ # Example:
+ # FileList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
+ #
+ def sub(pat, rep)
+ inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
+ end
+
+ # Return a new FileList with the results of running +gsub+ against each
+ # element of the original list.
+ #
+ # Example:
+ # FileList['lib/test/file', 'x/y'].gsub(/\//, "\\")
+ # => ['lib\\test\\file', 'x\\y']
+ #
+ def gsub(pat, rep)
+ inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
+ end
+
+ # Same as +sub+ except that the oringal file list is modified.
+ def sub!(pat, rep)
+ each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
+ self
+ end
+
+ # Same as +gsub+ except that the original file list is modified.
+ def gsub!(pat, rep)
+ each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
+ self
+ end
+
+ # Apply the pathmap spec to each of the included file names, returning a
+ # new file list with the modified paths. (See String#pathmap for
+ # details.)
+ def pathmap(spec=nil)
+ collect { |fn| fn.pathmap(spec) }
+ end
+
+ # Return a new array with <tt>String#ext</tt> method applied to each
+ # member of the array.
+ #
+ # This method is a shortcut for:
+ #
+ # array.collect { |item| item.ext(newext) }
+ #
+ # +ext+ is a user added method for the Array class.
+ def ext(newext='')
+ collect { |fn| fn.ext(newext) }
+ end
+
+
+ # Grep each of the files in the filelist using the given pattern. If a
+ # block is given, call the block on each matching line, passing the file
+ # name, line number, and the matching line of text. If no block is given,
+ # a standard emac style file:linenumber:line message will be printed to
+ # standard out.
+ def egrep(pattern, *opt)
+ each do |fn|
+ open(fn, "rb", *opt) do |inf|
+ count = 0
+ inf.each do |line|
+ count += 1
+ if pattern.match(line)
+ if block_given?
+ yield fn, count, line
+ else
+ puts "#{fn}:#{count}:#{line}"
+ end
+ end
+ end
+ end
+ end
+ end
+
+ # Return a new file list that only contains file names from the current
+ # file list that exist on the file system.
+ def existing
+ select { |fn| File.exist?(fn) }
+ end
+
+ # Modify the current file list so that it contains only file name that
+ # exist on the file system.
+ def existing!
+ resolve
+ @items = @items.select { |fn| File.exist?(fn) }
+ self
+ end
+
+ # FileList version of partition. Needed because the nested arrays should
+ # be FileLists in this version.
+ def partition(&block) # :nodoc:
+ resolve
+ result = @items.partition(&block)
+ [
+ FileList.new.import(result[0]),
+ FileList.new.import(result[1]),
+ ]
+ end
+
+ # Convert a FileList to a string by joining all elements with a space.
+ def to_s
+ resolve
+ self.join(' ')
+ end
+
+ # Add matching glob patterns.
+ def add_matching(pattern)
+ Dir[pattern].each do |fn|
+ self << fn unless exclude?(fn)
+ end
+ end
+ private :add_matching
+
+ # Should the given file name be excluded?
+ def exclude?(fn)
+ calculate_exclude_regexp unless @exclude_re
+ fn =~ @exclude_re || @exclude_procs.any? { |p| p.call(fn) }
+ end
+
+ DEFAULT_IGNORE_PATTERNS = [
+ /(^|[\/\\])CVS([\/\\]|$)/,
+ /(^|[\/\\])\.svn([\/\\]|$)/,
+ /\.bak$/,
+ /~$/
+ ]
+ DEFAULT_IGNORE_PROCS = [
+ proc { |fn| fn =~ /(^|[\/\\])core$/ && ! File.directory?(fn) }
+ ]
+# @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
+
+ def import(array)
+ @items = array
+ self
+ end
+
+ class << self
+ # Create a new file list including the files listed. Similar to:
+ #
+ # FileList.new(*args)
+ def [](*args)
+ new(*args)
+ end
+ end
+ end # FileList
+end
+
+module Rake
+ class << self
+
+ # Yield each file or directory component.
+ def each_dir_parent(dir) # :nodoc:
+ old_length = nil
+ while dir != '.' && dir.length != old_length
+ yield(dir)
+ old_length = dir.length
+ dir = File.dirname(dir)
+ end
+ end
+ end
+end # module Rake
+
+# Alias FileList to be available at the top level.
+FileList = Rake::FileList
+
+# ###########################################################################
+module Rake
+
+ # Default Rakefile loader used by +import+.
+ class DefaultLoader
+ def load(fn)
+ Kernel.load(File.expand_path(fn))
+ end
+ end
+
+ # EarlyTime is a fake timestamp that occurs _before_ any other time value.
+ class EarlyTime
+ include Comparable
+ include Singleton
+
+ def <=>(other)
+ -1
+ end
+
+ def to_s
+ "<EARLY TIME>"
+ end
+ end
+
+ EARLY = EarlyTime.instance
+end # module Rake
+
+# ###########################################################################
+# Extensions to time to allow comparisons with an early time class.
+#
+class Time
+ alias rake_original_time_compare :<=>
+ def <=>(other)
+ if Rake::EarlyTime === other
+ - other.<=>(self)
+ else
+ rake_original_time_compare(other)
+ end
+ end
+end # class Time
+
+module Rake
+
+ ####################################################################
+ # The NameSpace class will lookup task names in the the scope
+ # defined by a +namespace+ command.
+ #
+ class NameSpace
+
+ # Create a namespace lookup object using the given task manager
+ # and the list of scopes.
+ def initialize(task_manager, scope_list)
+ @task_manager = task_manager
+ @scope = scope_list.dup
+ end
+
+ # Lookup a task named +name+ in the namespace.
+ def [](name)
+ @task_manager.lookup(name, @scope)
+ end
+
+ # Return the list of tasks defined in this namespace.
+ def tasks
+ @task_manager.tasks
+ end
+ end # NameSpace
+
+
+ ####################################################################
+ # The TaskManager module is a mixin for managing tasks.
+ module TaskManager
+ # Track the last comment made in the Rakefile.
+ attr_accessor :last_description
+ alias :last_comment :last_description # Backwards compatibility
+
+ def initialize
+ super
+ @tasks = Hash.new
+ @rules = Array.new
+ @scope = Array.new
+ @last_description = nil
+ end
+
+ def create_rule(*args, &block)
+ pattern, arg_names, deps = resolve_args(args)
+ pattern = Regexp.new(Regexp.quote(pattern) + '$') if String === pattern
+ @rules << [pattern, deps, block]
+ end
+
+ def define_task(task_class, *args, &block)
+ task_name, arg_names, deps = resolve_args(args)
+ task_name = task_class.scope_name(@scope, task_name)
+ deps = [deps] unless deps.respond_to?(:to_ary)
+ deps = deps.collect {|d| d.to_s }
+ task = intern(task_class, task_name)
+ task.set_arg_names(arg_names) unless arg_names.empty?
+ task.add_description(@last_description)
+ @last_description = nil
+ task.enhance(deps, &block)
+ task
+ end
+
+ # Lookup a task. Return an existing task if found, otherwise
+ # create a task of the current type.
+ def intern(task_class, task_name)
+ @tasks[task_name.to_s] ||= task_class.new(task_name, self)
+ end
+
+ # Find a matching task for +task_name+.
+ def [](task_name, scopes=nil)
+ task_name = task_name.to_s
+ self.lookup(task_name, scopes) or
+ enhance_with_matching_rule(task_name) or
+ synthesize_file_task(task_name) or
+ fail "Don't know how to build task '#{task_name}'"
+ end
+
+ def synthesize_file_task(task_name)
+ return nil unless File.exist?(task_name)
+ define_task(Rake::FileTask, task_name)
+ end
+
+ # Resolve the arguments for a task/rule. Returns a triplet of
+ # [task_name, arg_name_list, prerequisites].
+ def resolve_args(args)
+ if args.last.is_a?(Hash)
+ deps = args.pop
+ resolve_args_with_dependencies(args, deps)
+ else
+ resolve_args_without_dependencies(args)
+ end
+ end
+
+ # Resolve task arguments for a task or rule when there are no
+ # dependencies declared.
+ #
+ # The patterns recognized by this argument resolving function are:
+ #
+ # task :t
+ # task :t, [:a]
+ # task :t, :a (deprecated)
+ #
+ def resolve_args_without_dependencies(args)
+ task_name = args.shift
+ if args.size == 1 && args.first.respond_to?(:to_ary)
+ arg_names = args.first.to_ary
+ else
+ arg_names = args
+ end
+ [task_name, arg_names, []]
+ end
+ private :resolve_args_without_dependencies
+
+ # Resolve task arguments for a task or rule when there are
+ # dependencies declared.
+ #
+ # The patterns recognized by this argument resolving function are:
+ #
+ # task :t => [:d]
+ # task :t, [a] => [:d]
+ # task :t, :needs => [:d] (deprecated)
+ # task :t, :a, :needs => [:d] (deprecated)
+ #
+ def resolve_args_with_dependencies(args, hash) # :nodoc:
+ fail "Task Argument Error" if hash.size != 1
+ key, value = hash.map { |k, v| [k,v] }.first
+ if args.empty?
+ task_name = key
+ arg_names = []
+ deps = value
+ elsif key == :needs
+ task_name = args.shift
+ arg_names = args
+ deps = value
+ else
+ task_name = args.shift
+ arg_names = key
+ deps = value
+ end
+ deps = [deps] unless deps.respond_to?(:to_ary)
+ [task_name, arg_names, deps]
+ end
+ private :resolve_args_with_dependencies
+
+ # If a rule can be found that matches the task name, enhance the
+ # task with the prerequisites and actions from the rule. Set the
+ # source attribute of the task appropriately for the rule. Return
+ # the enhanced task or nil of no rule was found.
+ def enhance_with_matching_rule(task_name, level=0)
+ fail Rake::RuleRecursionOverflowError,
+ "Rule Recursion Too Deep" if level >= 16
+ @rules.each do |pattern, extensions, block|
+ if md = pattern.match(task_name)
+ task = attempt_rule(task_name, extensions, block, level)
+ return task if task
+ end
+ end
+ nil
+ rescue Rake::RuleRecursionOverflowError => ex
+ ex.add_target(task_name)
+ fail ex
+ end
+
+ # List of all defined tasks in this application.
+ def tasks
+ @tasks.values.sort_by { |t| t.name }
+ end
+
+ # Clear all tasks in this application.
+ def clear
+ @tasks.clear
+ @rules.clear
+ end
+
+ # Lookup a task, using scope and the scope hints in the task name.
+ # This method performs straight lookups without trying to
+ # synthesize file tasks or rules. Special scope names (e.g. '^')
+ # are recognized. If no scope argument is supplied, use the
+ # current scope. Return nil if the task cannot be found.
+ def lookup(task_name, initial_scope=nil)
+ initial_scope ||= @scope
+ task_name = task_name.to_s
+ if task_name =~ /^rake:/
+ scopes = []
+ task_name = task_name.sub(/^rake:/, '')
+ elsif task_name =~ /^(\^+)/
+ scopes = initial_scope[0, initial_scope.size - $1.size]
+ task_name = task_name.sub(/^(\^+)/, '')
+ else
+ scopes = initial_scope
+ end
+ lookup_in_scope(task_name, scopes)
+ end
+
+ # Lookup the task name
+ def lookup_in_scope(name, scope)
+ n = scope.size
+ while n >= 0
+ tn = (scope[0,n] + [name]).join(':')
+ task = @tasks[tn]
+ return task if task
+ n -= 1
+ end
+ nil
+ end
+ private :lookup_in_scope
+
+ # Return the list of scope names currently active in the task
+ # manager.
+ def current_scope
+ @scope.dup
+ end
+
+ # Evaluate the block in a nested namespace named +name+. Create
+ # an anonymous namespace if +name+ is nil.
+ def in_namespace(name)
+ name ||= generate_name
+ @scope.push(name)
+ ns = NameSpace.new(self, @scope)
+ yield(ns)
+ ns
+ ensure
+ @scope.pop
+ end
+
+ private
+
+ # Generate an anonymous namespace name.
+ def generate_name
+ @seed ||= 0
+ @seed += 1
+ "_anon_#{@seed}"
+ end
+
+ def trace_rule(level, message)
+ puts "#{" "*level}#{message}" if Rake.application.options.trace_rules
+ end
+
+ # Attempt to create a rule given the list of prerequisites.
+ def attempt_rule(task_name, extensions, block, level)
+ sources = make_sources(task_name, extensions)
+ prereqs = sources.collect { |source|
+ trace_rule level, "Attempting Rule #{task_name} => #{source}"
+ if File.exist?(source) || Rake::Task.task_defined?(source)
+ trace_rule level, "(#{task_name} => #{source} ... EXIST)"
+ source
+ elsif parent = enhance_with_matching_rule(source, level+1)
+ trace_rule level, "(#{task_name} => #{source} ... ENHANCE)"
+ parent.name
+ else
+ trace_rule level, "(#{task_name} => #{source} ... FAIL)"
+ return nil
+ end
+ }
+ task = FileTask.define_task({task_name => prereqs}, &block)
+ task.sources = prereqs
+ task
+ end
+
+ # Make a list of sources from the list of file name extensions /
+ # translation procs.
+ def make_sources(task_name, extensions)
+ extensions.collect { |ext|
+ case ext
+ when /%/
+ task_name.pathmap(ext)
+ when %r{/}
+ ext
+ when /^\./
+ task_name.ext(ext)
+ when String
+ ext
+ when Proc
+ if ext.arity == 1
+ ext.call(task_name)
+ else
+ ext.call
+ end
+ else
+ fail "Don't know how to handle rule dependent: #{ext.inspect}"
+ end
+ }.flatten
+ end
+
+ end # TaskManager
+
+ ######################################################################
+ # Rake main application object. When invoking +rake+ from the
+ # command line, a Rake::Application object is created and run.
+ #
+ class Application
+ include TaskManager
+
+ # The name of the application (typically 'rake')
+ attr_reader :name
+
+ # The original directory where rake was invoked.
+ attr_reader :original_dir
+
+ # Name of the actual rakefile used.
+ attr_reader :rakefile
+
+ # List of the top level task names (task names from the command line).
+ attr_reader :top_level_tasks
+
+ DEFAULT_RAKEFILES = ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].freeze
+
+ # Initialize a Rake::Application object.
+ def initialize
+ super
+ @name = 'rake'
+ @rakefiles = DEFAULT_RAKEFILES.dup
+ @rakefile = nil
+ @pending_imports = []
+ @imported = []
+ @loaders = {}
+ @default_loader = Rake::DefaultLoader.new
+ @original_dir = Dir.pwd
+ @top_level_tasks = []
+ add_loader('rb', DefaultLoader.new)
+ add_loader('rf', DefaultLoader.new)
+ add_loader('rake', DefaultLoader.new)
+ @tty_output = STDOUT.tty?
+ end
+
+ # Run the Rake application. The run method performs the following three steps:
+ #
+ # * Initialize the command line options (+init+).
+ # * Define the tasks (+load_rakefile+).
+ # * Run the top level tasks (+run_tasks+).
+ #
+ # If you wish to build a custom rake command, you should call +init+ on your
+ # application. The define any tasks. Finally, call +top_level+ to run your top
+ # level tasks.
+ def run
+ standard_exception_handling do
+ init
+ load_rakefile
+ top_level
+ end
+ end
+
+ # Initialize the command line parameters and app name.
+ def init(app_name='rake')
+ standard_exception_handling do
+ @name = app_name
+ collect_tasks handle_options
+ end
+ end
+
+ # Find the rakefile and then load it and any pending imports.
+ def load_rakefile
+ standard_exception_handling do
+ raw_load_rakefile
+ end
+ end
+
+ # Run the top level tasks of a Rake application.
+ def top_level
+ standard_exception_handling do
+ if options.show_tasks
+ display_tasks_and_comments
+ elsif options.show_prereqs
+ display_prerequisites
+ else
+ top_level_tasks.each { |task_name| invoke_task(task_name) }
+ end
+ end
+ end
+
+ # Add a loader to handle imported files ending in the extension
+ # +ext+.
+ def add_loader(ext, loader)
+ ext = ".#{ext}" unless ext =~ /^\./
+ @loaders[ext] = loader
+ end
+
+ # Application options from the command line
+ def options
+ @options ||= OpenStruct.new
+ end
+
+ # private ----------------------------------------------------------------
+
+ def invoke_task(task_string)
+ name, args = parse_task_string(task_string)
+ t = self[name]
+ t.invoke(*args)
+ end
+
+ def parse_task_string(string)
+ if string =~ /^([^\[]+)(\[(.*)\])$/
+ name = $1
+ args = $3.split(/\s*,\s*/)
+ else
+ name = string
+ args = []
+ end
+ [name, args]
+ end
+
+ # Provide standard execption handling for the given block.
+ def standard_exception_handling
+ begin
+ yield
+ rescue SystemExit => ex
+ # Exit silently with current status
+ raise
+ rescue OptionParser::InvalidOption => ex
+ # Exit silently
+ exit(false)
+ rescue Exception => ex
+ # Exit with error message
+ $stderr.puts "rake aborted!"
+ $stderr.puts ex.message
+ if options.trace
+ $stderr.puts ex.backtrace.join("\n")
+ else
+ $stderr.puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
+ $stderr.puts "(See full trace by running task with --trace)"
+ end
+ exit(false)
+ end
+ end
+
+ # True if one of the files in RAKEFILES is in the current directory.
+ # If a match is found, it is copied into @rakefile.
+ def have_rakefile
+ @rakefiles.each do |fn|
+ if File.exist?(fn) || fn == ''
+ return fn
+ end
+ end
+ return nil
+ end
+
+ # True if we are outputting to TTY, false otherwise
+ def tty_output?
+ @tty_output
+ end
+
+ # Override the detected TTY output state (mostly for testing)
+ def tty_output=( tty_output_state )
+ @tty_output = tty_output_state
+ end
+
+ # We will truncate output if we are outputting to a TTY or if we've been
+ # given an explicit column width to honor
+ def truncate_output?
+ tty_output? || ENV['RAKE_COLUMNS']
+ end
+
+ # Display the tasks and dependencies.
+ def display_tasks_and_comments
+ displayable_tasks = tasks.select { |t|
+ t.comment && t.name =~ options.show_task_pattern
+ }
+ if options.full_description
+ displayable_tasks.each do |t|
+ puts "rake #{t.name_with_args}"
+ t.full_comment.split("\n").each do |line|
+ puts " #{line}"
+ end
+ puts
+ end
+ else
+ width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10
+ max_column = truncate_output? ? terminal_width - name.size - width - 7 : nil
+ displayable_tasks.each do |t|
+ printf "#{name} %-#{width}s # %s\n",
+ t.name_with_args, max_column ? truncate(t.comment, max_column) : t.comment
+ end
+ end
+ end
+
+ def terminal_width
+ if ENV['RAKE_COLUMNS']
+ result = ENV['RAKE_COLUMNS'].to_i
+ else
+ result = unix? ? dynamic_width : 80
+ end
+ (result < 10) ? 80 : result
+ rescue
+ 80
+ end
+
+ # Calculate the dynamic width of the
+ def dynamic_width
+ @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
+ end
+
+ def dynamic_width_stty
+ %x{stty size 2>/dev/null}.split[1].to_i
+ end
+
+ def dynamic_width_tput
+ %x{tput cols 2>/dev/null}.to_i
+ end
+
+ def unix?
+ RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
+ end
+
+ def windows?
+ Win32.windows?
+ end
+
+ def truncate(string, width)
+ if string.length <= width
+ string
+ else
+ ( string[0, width-3] || "" ) + "..."
+ end
+ end
+
+ # Display the tasks and prerequisites
+ def display_prerequisites
+ tasks.each do |t|
+ puts "rake #{t.name}"
+ t.prerequisites.each { |pre| puts " #{pre}" }
+ end
+ end
+
+ # A list of all the standard options used in rake, suitable for
+ # passing to OptionParser.
+ def standard_rake_options
+ [
+ ['--classic-namespace', '-C', "Put Task and FileTask in the top level namespace",
+ lambda { |value|
+ require 'rake/classic_namespace'
+ options.classic_namespace = true
+ }
+ ],
+ ['--describe', '-D [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.",
+ lambda { |value|
+ options.show_tasks = true
+ options.full_description = true
+ options.show_task_pattern = Regexp.new(value || '')
+ }
+ ],
+ ['--dry-run', '-n', "Do a dry run without executing actions.",
+ lambda { |value|
+ verbose(true)
+ nowrite(true)
+ options.dryrun = true
+ options.trace = true
+ }
+ ],
+ ['--execute', '-e CODE', "Execute some Ruby code and exit.",
+ lambda { |value|
+ eval(value)
+ exit
+ }
+ ],
+ ['--execute-print', '-p CODE', "Execute some Ruby code, print the result, then exit.",
+ lambda { |value|
+ puts eval(value)
+ exit
+ }
+ ],
+ ['--execute-continue', '-E CODE',
+ "Execute some Ruby code, then continue with normal task processing.",
+ lambda { |value| eval(value) }
+ ],
+ ['--libdir', '-I LIBDIR', "Include LIBDIR in the search path for required modules.",
+ lambda { |value| $:.push(value) }
+ ],
+ ['--prereqs', '-P', "Display the tasks and dependencies, then exit.",
+ lambda { |value| options.show_prereqs = true }
+ ],
+ ['--quiet', '-q', "Do not log messages to standard output.",
+ lambda { |value| verbose(false) }
+ ],
+ ['--rakefile', '-f [FILE]', "Use FILE as the rakefile.",
+ lambda { |value|
+ value ||= ''
+ @rakefiles.clear
+ @rakefiles << value
+ }
+ ],
+ ['--rakelibdir', '--rakelib', '-R RAKELIBDIR',
+ "Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')",
+ lambda { |value| options.rakelib = value.split(':') }
+ ],
+ ['--require', '-r MODULE', "Require MODULE before executing rakefile.",
+ lambda { |value|
+ begin
+ require value
+ rescue LoadError => ex
+ begin
+ rake_require value
+ rescue LoadError => ex2
+ raise ex
+ end
+ end
+ }
+ ],
+ ['--rules', "Trace the rules resolution.",
+ lambda { |value| options.trace_rules = true }
+ ],
+ ['--no-search', '--nosearch', '-N', "Do not search parent directories for the Rakefile.",
+ lambda { |value| options.nosearch = true }
+ ],
+ ['--silent', '-s', "Like --quiet, but also suppresses the 'in directory' announcement.",
+ lambda { |value|
+ verbose(false)
+ options.silent = true
+ }
+ ],
+ ['--system', '-g',
+ "Using system wide (global) rakefiles (usually '~/.rake/*.rake').",
+ lambda { |value| options.load_system = true }
+ ],
+ ['--no-system', '--nosystem', '-G',
+ "Use standard project Rakefile search paths, ignore system wide rakefiles.",
+ lambda { |value| options.ignore_system = true }
+ ],
+ ['--tasks', '-T [PATTERN]', "Display the tasks (matching optional PATTERN) with descriptions, then exit.",
+ lambda { |value|
+ options.show_tasks = true
+ options.show_task_pattern = Regexp.new(value || '')
+ options.full_description = false
+ }
+ ],
+ ['--trace', '-t', "Turn on invoke/execute tracing, enable full backtrace.",
+ lambda { |value|
+ options.trace = true
+ verbose(true)
+ }
+ ],
+ ['--verbose', '-v', "Log message to standard output (default).",
+ lambda { |value| verbose(true) }
+ ],
+ ['--version', '-V', "Display the program version.",
+ lambda { |value|
+ puts "rake, version #{RAKEVERSION}"
+ exit
+ }
+ ]
+ ]
+ end
+
+ # Read and handle the command line options.
+ def handle_options
+ options.rakelib = ['rakelib']
+
+ opts = OptionParser.new
+ opts.banner = "rake [-f rakefile] {options} targets..."
+ opts.separator ""
+ opts.separator "Options are ..."
+
+ opts.on_tail("-h", "--help", "-H", "Display this help message.") do
+ puts opts
+ exit
+ end
+
+ standard_rake_options.each { |args| opts.on(*args) }
+ parsed_argv = opts.parse(ARGV)
+
+ # If class namespaces are requested, set the global options
+ # according to the values in the options structure.
+ if options.classic_namespace
+ $show_tasks = options.show_tasks
+ $show_prereqs = options.show_prereqs
+ $trace = options.trace
+ $dryrun = options.dryrun
+ $silent = options.silent
+ end
+ parsed_argv
+ end
+
+ # Similar to the regular Ruby +require+ command, but will check
+ # for *.rake files in addition to *.rb files.
+ def rake_require(file_name, paths=$LOAD_PATH, loaded=$")
+ return false if loaded.include?(file_name)
+ paths.each do |path|
+ fn = file_name + ".rake"
+ full_path = File.join(path, fn)
+ if File.exist?(full_path)
+ load full_path
+ loaded << fn
+ return true
+ end
+ end
+ fail LoadError, "Can't find #{file_name}"
+ end
+
+ def find_rakefile_location
+ here = Dir.pwd
+ while ! (fn = have_rakefile)
+ Dir.chdir("..")
+ if Dir.pwd == here || options.nosearch
+ return nil
+ end
+ here = Dir.pwd
+ end
+ [fn, here]
+ ensure
+ Dir.chdir(Rake.original_dir)
+ end
+
+ def raw_load_rakefile # :nodoc:
+ rakefile, location = find_rakefile_location
+ if (! options.ignore_system) &&
+ (options.load_system || rakefile.nil?) &&
+ system_dir && File.directory?(system_dir)
+ puts "(in #{Dir.pwd})" unless options.silent
+ glob("#{system_dir}/*.rake") do |name|
+ add_import name
+ end
+ else
+ fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})" if
+ rakefile.nil?
+ @rakefile = rakefile
+ Dir.chdir(location)
+ puts "(in #{Dir.pwd})" unless options.silent
+ $rakefile = @rakefile if options.classic_namespace
+ load File.expand_path(@rakefile) if @rakefile && @rakefile != ''
+ options.rakelib.each do |rlib|
+ glob("#{rlib}/*.rake") do |name|
+ add_import name
+ end
+ end
+ end
+ load_imports
+ end
+
+ def glob(path, &block)
+ Dir[path.gsub("\\", '/')].each(&block)
+ end
+ private :glob
+
+ # The directory path containing the system wide rakefiles.
+ def system_dir
+ @system_dir ||=
+ begin
+ if ENV['RAKE_SYSTEM']
+ ENV['RAKE_SYSTEM']
+ else
+ standard_system_dir
+ end
+ end
+ end
+
+ # The standard directory containing system wide rake files.
+ if Win32.windows?
+ def standard_system_dir #:nodoc:
+ Win32.win32_system_dir
+ end
+ else
+ def standard_system_dir #:nodoc:
+ File.expand_path('.rake', '~')
+ end
+ end
+ private :standard_system_dir
+
+ # Collect the list of tasks on the command line. If no tasks are
+ # given, return a list containing only the default task.
+ # Environmental assignments are processed at this time as well.
+ def collect_tasks(argv)
+ @top_level_tasks = []
+ argv.each do |arg|
+ if arg =~ /^(\w+)=(.*)$/
+ ENV[$1] = $2
+ else
+ @top_level_tasks << arg unless arg =~ /^-/
+ end
+ end
+ @top_level_tasks.push("default") if @top_level_tasks.size == 0
+ end
+
+ # Add a file to the list of files to be imported.
+ def add_import(fn)
+ @pending_imports << fn
+ end
+
+ # Load the pending list of imported files.
+ def load_imports
+ while fn = @pending_imports.shift
+ next if @imported.member?(fn)
+ if fn_task = lookup(fn)
+ fn_task.invoke
+ end
+ ext = File.extname(fn)
+ loader = @loaders[ext] || @default_loader
+ loader.load(fn)
+ @imported << fn
+ end
+ end
+
+ # Warn about deprecated use of top level constant names.
+ def const_warning(const_name)
+ @const_warning ||= false
+ if ! @const_warning
+ $stderr.puts %{WARNING: Deprecated reference to top-level constant '#{const_name}' } +
+ %{found at: #{rakefile_location}} # '
+ $stderr.puts %{ Use --classic-namespace on rake command}
+ $stderr.puts %{ or 'require "rake/classic_namespace"' in Rakefile}
+ end
+ @const_warning = true
+ end
+
+ def rakefile_location
+ begin
+ fail
+ rescue RuntimeError => ex
+ ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
+ end
+ end
+ end
+end
+
+
+class Module
+ # Rename the original handler to make it available.
+ alias :rake_original_const_missing :const_missing
+
+ # Check for deprecated uses of top level (i.e. in Object) uses of
+ # Rake class names. If someone tries to reference the constant
+ # name, display a warning and return the proper object. Using the
+ # --classic-namespace command line option will define these
+ # constants in Object and avoid this handler.
+ def const_missing(const_name)
+ case const_name
+ when :Task
+ Rake.application.const_warning(const_name)
+ Rake::Task
+ when :FileTask
+ Rake.application.const_warning(const_name)
+ Rake::FileTask
+ when :FileCreationTask
+ Rake.application.const_warning(const_name)
+ Rake::FileCreationTask
+ when :RakeApp
+ Rake.application.const_warning(const_name)
+ Rake::Application
+ else
+ rake_original_const_missing(const_name)
+ end
+ end
+end
diff --git a/ruby/lib/rake/classic_namespace.rb b/ruby/lib/rake/classic_namespace.rb
new file mode 100644
index 0000000..feb7569
--- /dev/null
+++ b/ruby/lib/rake/classic_namespace.rb
@@ -0,0 +1,8 @@
+# The following classes used to be in the top level namespace.
+# Loading this file enables compatibility with older Rakefile that
+# referenced Task from the top level.
+
+Task = Rake::Task
+FileTask = Rake::FileTask
+FileCreationTask = Rake::FileCreationTask
+RakeApp = Rake::Application
diff --git a/ruby/lib/rake/clean.rb b/ruby/lib/rake/clean.rb
new file mode 100644
index 0000000..4ee2c5a
--- /dev/null
+++ b/ruby/lib/rake/clean.rb
@@ -0,0 +1,33 @@
+#!/usr/bin/env ruby
+
+# The 'rake/clean' file defines two file lists (CLEAN and CLOBBER) and
+# two rake tasks (:clean and :clobber).
+#
+# [:clean] Clean up the project by deleting scratch files and backup
+# files. Add files to the CLEAN file list to have the :clean
+# target handle them.
+#
+# [:clobber] Clobber all generated and non-source files in a project.
+# The task depends on :clean, so all the clean files will
+# be deleted as well as files in the CLOBBER file list.
+# The intent of this task is to return a project to its
+# pristine, just unpacked state.
+
+require 'rake'
+
+CLEAN = Rake::FileList["**/*~", "**/*.bak", "**/core"]
+CLEAN.clear_exclude.exclude { |fn|
+ fn.pathmap("%f") == 'core' && File.directory?(fn)
+}
+
+desc "Remove any temporary products."
+task :clean do
+ CLEAN.each { |fn| rm_r fn rescue nil }
+end
+
+CLOBBER = Rake::FileList.new
+
+desc "Remove any generated file."
+task :clobber => [:clean] do
+ CLOBBER.each { |fn| rm_r fn rescue nil }
+end
diff --git a/ruby/lib/rake/gempackagetask.rb b/ruby/lib/rake/gempackagetask.rb
new file mode 100644
index 0000000..1e4632a
--- /dev/null
+++ b/ruby/lib/rake/gempackagetask.rb
@@ -0,0 +1,97 @@
+#!/usr/bin/env ruby
+
+# Define a package task library to aid in the definition of GEM
+# packages.
+
+require 'rubygems'
+require 'rake'
+require 'rake/packagetask'
+require 'rubygems/user_interaction'
+require 'rubygems/builder'
+
+module Rake
+
+ # Create a package based upon a Gem spec. Gem packages, as well as
+ # zip files and tar/gzipped packages can be produced by this task.
+ #
+ # In addition to the Rake targets generated by PackageTask, a
+ # GemPackageTask will also generate the following tasks:
+ #
+ # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.gem"</b>]
+ # Create a Ruby GEM package with the given name and version.
+ #
+ # Example using a Ruby GEM spec:
+ #
+ # require 'rubygems'
+ #
+ # spec = Gem::Specification.new do |s|
+ # s.platform = Gem::Platform::RUBY
+ # s.summary = "Ruby based make-like utility."
+ # s.name = 'rake'
+ # s.version = PKG_VERSION
+ # s.requirements << 'none'
+ # s.require_path = 'lib'
+ # s.autorequire = 'rake'
+ # s.files = PKG_FILES
+ # s.description = <<EOF
+ # Rake is a Make-like program implemented in Ruby. Tasks
+ # and dependencies are specified in standard Ruby syntax.
+ # EOF
+ # end
+ #
+ # Rake::GemPackageTask.new(spec) do |pkg|
+ # pkg.need_zip = true
+ # pkg.need_tar = true
+ # end
+ #
+ class GemPackageTask < PackageTask
+ # Ruby GEM spec containing the metadata for this package. The
+ # name, version and package_files are automatically determined
+ # from the GEM spec and don't need to be explicitly provided.
+ attr_accessor :gem_spec
+
+ # Create a GEM Package task library. Automatically define the gem
+ # if a block is given. If no block is supplied, then +define+
+ # needs to be called to define the task.
+ def initialize(gem_spec)
+ init(gem_spec)
+ yield self if block_given?
+ define if block_given?
+ end
+
+ # Initialization tasks without the "yield self" or define
+ # operations.
+ def init(gem)
+ super(gem.name, gem.version)
+ @gem_spec = gem
+ @package_files += gem_spec.files if gem_spec.files
+ end
+
+ # Create the Rake tasks and actions specified by this
+ # GemPackageTask. (+define+ is automatically called if a block is
+ # given to +new+).
+ def define
+ super
+ task :package => [:gem]
+ desc "Build the gem file #{gem_file}"
+ task :gem => ["#{package_dir}/#{gem_file}"]
+ file "#{package_dir}/#{gem_file}" => [package_dir] + @gem_spec.files do
+ when_writing("Creating GEM") {
+ Gem::Builder.new(gem_spec).build
+ verbose(true) {
+ mv gem_file, "#{package_dir}/#{gem_file}"
+ }
+ }
+ end
+ end
+
+ def gem_file
+ if @gem_spec.platform == Gem::Platform::RUBY
+ "#{package_name}.gem"
+ else
+ "#{package_name}-#{@gem_spec.platform}.gem"
+ end
+ end
+
+ end
+end
diff --git a/ruby/lib/rake/loaders/makefile.rb b/ruby/lib/rake/loaders/makefile.rb
new file mode 100644
index 0000000..9ade098
--- /dev/null
+++ b/ruby/lib/rake/loaders/makefile.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/env ruby
+
+module Rake
+
+ # Makefile loader to be used with the import file loader.
+ class MakefileLoader
+
+ # Load the makefile dependencies in +fn+.
+ def load(fn)
+ open(fn) do |mf|
+ lines = mf.read
+ lines.gsub!(/#[^\n]*\n/m, "")
+ lines.gsub!(/\\\n/, ' ')
+ lines.split("\n").each do |line|
+ process_line(line)
+ end
+ end
+ end
+
+ private
+
+ # Process one logical line of makefile data.
+ def process_line(line)
+ file_tasks, args = line.split(':')
+ return if args.nil?
+ dependents = args.split
+ file_tasks.strip.split.each do |file_task|
+ file file_task => dependents
+ end
+ end
+ end
+
+ # Install the handler
+ Rake.application.add_loader('mf', MakefileLoader.new)
+end
diff --git a/ruby/lib/rake/packagetask.rb b/ruby/lib/rake/packagetask.rb
new file mode 100644
index 0000000..6158eaf
--- /dev/null
+++ b/ruby/lib/rake/packagetask.rb
@@ -0,0 +1,185 @@
+#!/usr/bin/env ruby
+
+# Define a package task libarary to aid in the definition of
+# redistributable package files.
+
+require 'rake'
+require 'rake/tasklib'
+
+module Rake
+
+ # Create a packaging task that will package the project into
+ # distributable files (e.g zip archive or tar files).
+ #
+ # The PackageTask will create the following targets:
+ #
+ # [<b>:package</b>]
+ # Create all the requested package files.
+ #
+ # [<b>:clobber_package</b>]
+ # Delete all the package files. This target is automatically
+ # added to the main clobber target.
+ #
+ # [<b>:repackage</b>]
+ # Rebuild the package files from scratch, even if they are not out
+ # of date.
+ #
+ # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tgz"</b>]
+ # Create a gzipped tar package (if <em>need_tar</em> is true).
+ #
+ # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tar.gz"</b>]
+ # Create a gzipped tar package (if <em>need_tar_gz</em> is true).
+ #
+ # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tar.bz2"</b>]
+ # Create a bzip2'd tar package (if <em>need_tar_bz2</em> is true).
+ #
+ # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.zip"</b>]
+ # Create a zip package archive (if <em>need_zip</em> is true).
+ #
+ # Example:
+ #
+ # Rake::PackageTask.new("rake", "1.2.3") do |p|
+ # p.need_tar = true
+ # p.package_files.include("lib/**/*.rb")
+ # end
+ #
+ class PackageTask < TaskLib
+ # Name of the package.
+ attr_accessor :name
+
+ # Version of the package (e.g. '1.3.2').
+ attr_accessor :version
+
+ # Directory used to store the package files (default is 'pkg').
+ attr_accessor :package_dir
+
+ # True if a gzipped tar file (tgz) should be produced (default is false).
+ attr_accessor :need_tar
+
+ # True if a gzipped tar file (tar.gz) should be produced (default is false).
+ attr_accessor :need_tar_gz
+
+ # True if a bzip2'd tar file (tar.bz2) should be produced (default is false).
+ attr_accessor :need_tar_bz2
+
+ # True if a zip file should be produced (default is false)
+ attr_accessor :need_zip
+
+ # List of files to be included in the package.
+ attr_accessor :package_files
+
+ # Tar command for gzipped or bzip2ed archives. The default is 'tar'.
+ attr_accessor :tar_command
+
+ # Zip command for zipped archives. The default is 'zip'.
+ attr_accessor :zip_command
+
+ # Create a Package Task with the given name and version.
+ def initialize(name=nil, version=nil)
+ init(name, version)
+ yield self if block_given?
+ define unless name.nil?
+ end
+
+ # Initialization that bypasses the "yield self" and "define" step.
+ def init(name, version)
+ @name = name
+ @version = version
+ @package_files = Rake::FileList.new
+ @package_dir = 'pkg'
+ @need_tar = false
+ @need_tar_gz = false
+ @need_tar_bz2 = false
+ @need_zip = false
+ @tar_command = 'tar'
+ @zip_command = 'zip'
+ end
+
+ # Create the tasks defined by this task library.
+ def define
+ fail "Version required (or :noversion)" if @version.nil?
+ @version = nil if :noversion == @version
+
+ desc "Build all the packages"
+ task :package
+
+ desc "Force a rebuild of the package files"
+ task :repackage => [:clobber_package, :package]
+
+ desc "Remove package products"
+ task :clobber_package do
+ rm_r package_dir rescue nil
+ end
+
+ task :clobber => [:clobber_package]
+
+ [
+ [need_tar, tgz_file, "z"],
+ [need_tar_gz, tar_gz_file, "z"],
+ [need_tar_bz2, tar_bz2_file, "j"]
+ ].each do |(need, file, flag)|
+ if need
+ task :package => ["#{package_dir}/#{file}"]
+ file "#{package_dir}/#{file}" => [package_dir_path] + package_files do
+ chdir(package_dir) do
+ sh %{env}
+ sh %{#{@tar_command} #{flag}cvf #{file} #{package_name}}
+ end
+ end
+ end
+ end
+
+ if need_zip
+ task :package => ["#{package_dir}/#{zip_file}"]
+ file "#{package_dir}/#{zip_file}" => [package_dir_path] + package_files do
+ chdir(package_dir) do
+ sh %{#{@zip_command} -r #{zip_file} #{package_name}}
+ end
+ end
+ end
+
+ directory package_dir
+
+ file package_dir_path => @package_files do
+ mkdir_p package_dir rescue nil
+ @package_files.each do |fn|
+ f = File.join(package_dir_path, fn)
+ fdir = File.dirname(f)
+ mkdir_p(fdir) if !File.exist?(fdir)
+ if File.directory?(fn)
+ mkdir_p(f)
+ else
+ rm_f f
+ safe_ln(fn, f)
+ end
+ end
+ end
+ self
+ end
+
+ def package_name
+ @version ? "#{@name}-#{@version}" : @name
+ end
+
+ def package_dir_path
+ "#{package_dir}/#{package_name}"
+ end
+
+ def tgz_file
+ "#{package_name}.tgz"
+ end
+
+ def tar_gz_file
+ "#{package_name}.tar.gz"
+ end
+
+ def tar_bz2_file
+ "#{package_name}.tar.bz2"
+ end
+
+ def zip_file
+ "#{package_name}.zip"
+ end
+ end
+
+end
diff --git a/ruby/lib/rake/rake_test_loader.rb b/ruby/lib/rake/rake_test_loader.rb
new file mode 100644
index 0000000..8d7dad3
--- /dev/null
+++ b/ruby/lib/rake/rake_test_loader.rb
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+# Load the test files from the command line.
+
+ARGV.each { |f| load f unless f =~ /^-/ }
diff --git a/ruby/lib/rake/rdoctask.rb b/ruby/lib/rake/rdoctask.rb
new file mode 100644
index 0000000..6cfbda1
--- /dev/null
+++ b/ruby/lib/rake/rdoctask.rb
@@ -0,0 +1,147 @@
+#!/usr/bin/env ruby
+
+require 'rake'
+require 'rake/tasklib'
+
+module Rake
+
+ # Create a documentation task that will generate the RDoc files for
+ # a project.
+ #
+ # The RDocTask will create the following targets:
+ #
+ # [<b><em>rdoc</em></b>]
+ # Main task for this RDOC task.
+ #
+ # [<b>:clobber_<em>rdoc</em></b>]
+ # Delete all the rdoc files. This target is automatically
+ # added to the main clobber target.
+ #
+ # [<b>:re<em>rdoc</em></b>]
+ # Rebuild the rdoc files from scratch, even if they are not out
+ # of date.
+ #
+ # Simple Example:
+ #
+ # Rake::RDocTask.new do |rd|
+ # rd.main = "README.rdoc"
+ # rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
+ # end
+ #
+ # You may wish to give the task a different name, such as if you are
+ # generating two sets of documentation. For instance, if you want to have a
+ # development set of documentation including private methods:
+ #
+ # Rake::RDocTask.new(:rdoc_dev) do |rd|
+ # rd.main = "README.doc"
+ # rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
+ # rd.options << "--all"
+ # end
+ #
+ # The tasks would then be named :<em>rdoc_dev</em>, :clobber_<em>rdoc_dev</em>, and
+ # :re<em>rdoc_dev</em>.
+ #
+ class RDocTask < TaskLib
+ # Name of the main, top level task. (default is :rdoc)
+ attr_accessor :name
+
+ # Name of directory to receive the html output files. (default is "html")
+ attr_accessor :rdoc_dir
+
+ # Title of RDoc documentation. (default is none)
+ attr_accessor :title
+
+ # Name of file to be used as the main, top level file of the
+ # RDoc. (default is none)
+ attr_accessor :main
+
+ # Name of template to be used by rdoc. (defaults to rdoc's default)
+ attr_accessor :template
+
+ # List of files to be included in the rdoc generation. (default is [])
+ attr_accessor :rdoc_files
+
+ # List of options to be passed rdoc. (default is [])
+ attr_accessor :options
+
+ # Run the rdoc process as an external shell (default is false)
+ attr_accessor :external
+
+ # Create an RDoc task named <em>rdoc</em>. Default task name is +rdoc+.
+ def initialize(name=:rdoc) # :yield: self
+ @name = name
+ @rdoc_files = Rake::FileList.new
+ @rdoc_dir = 'html'
+ @main = nil
+ @title = nil
+ @template = nil
+ @external = false
+ @options = []
+ yield self if block_given?
+ define
+ end
+
+ # Create the tasks defined by this task lib.
+ def define
+ if name.to_s != "rdoc"
+ desc "Build the RDOC HTML Files"
+ end
+
+ desc "Build the #{name} HTML Files"
+ task name
+
+ desc "Force a rebuild of the RDOC files"
+ task "re#{name}" => ["clobber_#{name}", name]
+
+ desc "Remove rdoc products"
+ task "clobber_#{name}" do
+ rm_r rdoc_dir rescue nil
+ end
+
+ task :clobber => ["clobber_#{name}"]
+
+ directory @rdoc_dir
+ task name => [rdoc_target]
+ file rdoc_target => @rdoc_files + [Rake.application.rakefile] do
+ rm_r @rdoc_dir rescue nil
+ args = option_list + @rdoc_files
+ if @external
+ argstring = args.join(' ')
+ sh %{ruby -Ivendor vender/rd #{argstring}}
+ else
+ require 'rdoc/rdoc'
+ RDoc::RDoc.new.document(args)
+ end
+ end
+ self
+ end
+
+ def option_list
+ result = @options.dup
+ result << "-o" << @rdoc_dir
+ result << "--main" << quote(main) if main
+ result << "--title" << quote(title) if title
+ result << "-T" << quote(template) if template
+ result
+ end
+
+ def quote(str)
+ if @external
+ "'#{str}'"
+ else
+ str
+ end
+ end
+
+ def option_string
+ option_list.join(' ')
+ end
+
+ private
+
+ def rdoc_target
+ "#{rdoc_dir}/index.html"
+ end
+
+ end
+end
diff --git a/ruby/lib/rake/runtest.rb b/ruby/lib/rake/runtest.rb
new file mode 100644
index 0000000..3f1d205
--- /dev/null
+++ b/ruby/lib/rake/runtest.rb
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+
+require 'test/unit'
+require 'test/unit/assertions'
+
+module Rake
+ include Test::Unit::Assertions
+
+ def run_tests(pattern='test/test*.rb', log_enabled=false)
+ Dir["#{pattern}"].each { |fn|
+ puts fn if log_enabled
+ begin
+ load fn
+ rescue Exception => ex
+ puts "Error in #{fn}: #{ex.message}"
+ puts ex.backtrace
+ assert false
+ end
+ }
+ end
+
+ extend self
+end
diff --git a/ruby/lib/rake/tasklib.rb b/ruby/lib/rake/tasklib.rb
new file mode 100644
index 0000000..c7fd981
--- /dev/null
+++ b/ruby/lib/rake/tasklib.rb
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+
+require 'rake'
+
+module Rake
+
+ # Base class for Task Libraries.
+ class TaskLib
+ include Cloneable
+
+ # Make a symbol by pasting two strings together.
+ #
+ # NOTE: DEPRECATED! This method is kinda stupid. I don't know why
+ # I didn't just use string interpolation. But now other task
+ # libraries depend on this so I can't remove it without breaking
+ # other people's code. So for now it stays for backwards
+ # compatibility. BUT DON'T USE IT.
+ def paste(a,b) # :nodoc:
+ (a.to_s + b.to_s).intern
+ end
+ end
+
+end
diff --git a/ruby/lib/rake/testtask.rb b/ruby/lib/rake/testtask.rb
new file mode 100644
index 0000000..79154e4
--- /dev/null
+++ b/ruby/lib/rake/testtask.rb
@@ -0,0 +1,161 @@
+#!/usr/bin/env ruby
+
+# Define a task library for running unit tests.
+
+require 'rake'
+require 'rake/tasklib'
+
+module Rake
+
+ # Create a task that runs a set of tests.
+ #
+ # Example:
+ #
+ # Rake::TestTask.new do |t|
+ # t.libs << "test"
+ # t.test_files = FileList['test/test*.rb']
+ # t.verbose = true
+ # end
+ #
+ # If rake is invoked with a "TEST=filename" command line option,
+ # then the list of test files will be overridden to include only the
+ # filename specified on the command line. This provides an easy way
+ # to run just one test.
+ #
+ # If rake is invoked with a "TESTOPTS=options" command line option,
+ # then the given options are passed to the test process after a
+ # '--'. This allows Test::Unit options to be passed to the test
+ # suite.
+ #
+ # Examples:
+ #
+ # rake test # run tests normally
+ # rake test TEST=just_one_file.rb # run just one test file.
+ # rake test TESTOPTS="-v" # run in verbose mode
+ # rake test TESTOPTS="--runner=fox" # use the fox test runner
+ #
+ class TestTask < TaskLib
+
+ # Name of test task. (default is :test)
+ attr_accessor :name
+
+ # List of directories to added to $LOAD_PATH before running the
+ # tests. (default is 'lib')
+ attr_accessor :libs
+
+ # True if verbose test output desired. (default is false)
+ attr_accessor :verbose
+
+ # Test options passed to the test suite. An explicit
+ # TESTOPTS=opts on the command line will override this. (default
+ # is NONE)
+ attr_accessor :options
+
+ # Request that the tests be run with the warning flag set.
+ # E.g. warning=true implies "ruby -w" used to run the tests.
+ attr_accessor :warning
+
+ # Glob pattern to match test files. (default is 'test/test*.rb')
+ attr_accessor :pattern
+
+ # Style of test loader to use. Options are:
+ #
+ # * :rake -- Rake provided test loading script (default).
+ # * :testrb -- Ruby provided test loading script.
+ # * :direct -- Load tests using command line loader.
+ #
+ attr_accessor :loader
+
+ # Array of commandline options to pass to ruby when running test loader.
+ attr_accessor :ruby_opts
+
+ # Explicitly define the list of test files to be included in a
+ # test. +list+ is expected to be an array of file names (a
+ # FileList is acceptable). If both +pattern+ and +test_files+ are
+ # used, then the list of test files is the union of the two.
+ def test_files=(list)
+ @test_files = list
+ end
+
+ # Create a testing task.
+ def initialize(name=:test)
+ @name = name
+ @libs = ["lib"]
+ @pattern = nil
+ @options = nil
+ @test_files = nil
+ @verbose = false
+ @warning = false
+ @loader = :rake
+ @ruby_opts = []
+ yield self if block_given?
+ @pattern = 'test/test*.rb' if @pattern.nil? && @test_files.nil?
+ define
+ end
+
+ # Create the tasks defined by this task lib.
+ def define
+ lib_path = @libs.join(File::PATH_SEPARATOR)
+ desc "Run tests" + (@name==:test ? "" : " for #{@name}")
+ task @name do
+ run_code = ''
+ RakeFileUtils.verbose(@verbose) do
+ run_code =
+ case @loader
+ when :direct
+ "-e 'ARGV.each{|f| load f}'"
+ when :testrb
+ "-S testrb #{fix}"
+ when :rake
+ rake_loader
+ end
+ @ruby_opts.unshift( "-I#{lib_path}" )
+ @ruby_opts.unshift( "-w" ) if @warning
+ ruby @ruby_opts.join(" ") +
+ " \"#{run_code}\" " +
+ file_list.collect { |fn| "\"#{fn}\"" }.join(' ') +
+ " #{option_list}"
+ end
+ end
+ self
+ end
+
+ def option_list # :nodoc:
+ ENV['TESTOPTS'] || @options || ""
+ end
+
+ def file_list # :nodoc:
+ if ENV['TEST']
+ FileList[ ENV['TEST'] ]
+ else
+ result = []
+ result += @test_files.to_a if @test_files
+ result += FileList[ @pattern ].to_a if @pattern
+ FileList[result]
+ end
+ end
+
+ def fix # :nodoc:
+ case RUBY_VERSION
+ when '1.8.2'
+ find_file 'rake/ruby182_test_unit_fix'
+ else
+ nil
+ end || ''
+ end
+
+ def rake_loader # :nodoc:
+ find_file('rake/rake_test_loader') or
+ fail "unable to find rake test loader"
+ end
+
+ def find_file(fn) # :nodoc:
+ $LOAD_PATH.each do |path|
+ file_path = File.join(path, "#{fn}.rb")
+ return file_path if File.exist? file_path
+ end
+ nil
+ end
+
+ end
+end
diff --git a/ruby/lib/rake/win32.rb b/ruby/lib/rake/win32.rb
new file mode 100644
index 0000000..96f66d6
--- /dev/null
+++ b/ruby/lib/rake/win32.rb
@@ -0,0 +1,34 @@
+module Rake
+
+ # Win 32 interface methods for Rake. Windows specific functionality
+ # will be placed here to collect that knowledge in one spot.
+ module Win32
+ class << self
+ # True if running on a windows system.
+ def windows?
+ # assume other DOSish systems are extinct.
+ File::ALT_SEPARATOR == '\\'
+ end
+ end
+
+ class << self
+ # The standard directory containing system wide rake files on
+ # Win 32 systems. Try the following environment variables (in
+ # order):
+ #
+ # * APPDATA
+ # * HOME
+ # * HOMEDRIVE + HOMEPATH
+ # * USERPROFILE
+ #
+ # If the above are not defined, retruns the personal folder.
+ def win32_system_dir #:nodoc:
+ win32_shared_path = ENV['APPDATA']
+ if !win32_shared_path or win32_shared_path.empty?
+ win32_shared_path = '~'
+ end
+ File.expand_path('Rake', win32_shared_path)
+ end
+ end if windows?
+ end
+end
diff --git a/ruby/lib/rational.rb b/ruby/lib/rational.rb
new file mode 100644
index 0000000..8bed856
--- /dev/null
+++ b/ruby/lib/rational.rb
@@ -0,0 +1,19 @@
+class Fixnum
+
+ alias quof fdiv
+ alias rdiv quo
+
+ alias power! ** unless method_defined? :power!
+ alias rpower **
+
+end
+
+class Bignum
+
+ alias quof fdiv
+ alias rdiv quo
+
+ alias power! ** unless method_defined? :power!
+ alias rpower **
+
+end
diff --git a/ruby/lib/rbconfig/datadir.rb b/ruby/lib/rbconfig/datadir.rb
new file mode 100644
index 0000000..5b8f077
--- /dev/null
+++ b/ruby/lib/rbconfig/datadir.rb
@@ -0,0 +1,24 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+
+module Config
+
+ # Only define datadir if it doesn't already exist.
+ unless Config.respond_to?(:datadir)
+
+ # Return the path to the data directory associated with the given
+ # package name. Normally this is just
+ # "#{Config::CONFIG['datadir']}/#{package_name}", but may be
+ # modified by packages like RubyGems to handle versioned data
+ # directories.
+ def Config.datadir(package_name)
+ File.join(CONFIG['datadir'], package_name)
+ end
+
+ end
+end
diff --git a/ruby/lib/rdoc.rb b/ruby/lib/rdoc.rb
new file mode 100644
index 0000000..f4fc386
--- /dev/null
+++ b/ruby/lib/rdoc.rb
@@ -0,0 +1,395 @@
+$DEBUG_RDOC = nil
+
+##
+# = \RDoc - Ruby Documentation System
+#
+# This package contains RDoc and RDoc::Markup. RDoc is an application that
+# produces documentation for one or more Ruby source files. It works similarly
+# to JavaDoc, parsing the source, and extracting the definition for classes,
+# modules, and methods (along with includes and requires). It associates with
+# these optional documentation contained in the immediately preceding comment
+# block, and then renders the result using a pluggable output formatter.
+# RDoc::Markup is a library that converts plain text into various output
+# formats. The markup library is used to interpret the comment blocks that
+# RDoc uses to document methods, classes, and so on.
+#
+# == Roadmap
+#
+# * If you want to use RDoc to create documentation for your Ruby source files,
+# read on.
+# * If you want to include extensions written in C, see RDoc::Parser::C
+# * If you want to drive RDoc programmatically, see RDoc::RDoc.
+# * If you want to use the library to format text blocks into HTML, have a look
+# at RDoc::Markup.
+# * If you want to try writing your own HTML output template, see
+# RDoc::Generator::HTML
+#
+# == Summary
+#
+# Once installed, you can create documentation using the +rdoc+ command
+#
+# % rdoc [options] [names...]
+#
+# For an up-to-date option summary, type
+# % rdoc --help
+#
+# A typical use might be to generate documentation for a package of Ruby
+# source (such as RDoc itself).
+#
+# % rdoc
+#
+# This command generates documentation for all the Ruby and C source
+# files in and below the current directory. These will be stored in a
+# documentation tree starting in the subdirectory +doc+.
+#
+# You can make this slightly more useful for your readers by having the
+# index page contain the documentation for the primary file. In our
+# case, we could type
+#
+# % rdoc --main rdoc.rb
+#
+# You'll find information on the various formatting tricks you can use
+# in comment blocks in the documentation this generates.
+#
+# RDoc uses file extensions to determine how to process each file. File names
+# ending +.rb+ and +.rbw+ are assumed to be Ruby source. Files
+# ending +.c+ are parsed as C files. All other files are assumed to
+# contain just Markup-style markup (with or without leading '#' comment
+# markers). If directory names are passed to RDoc, they are scanned
+# recursively for C and Ruby source files only.
+#
+# == \Options
+# rdoc can be passed a variety of command-line options. In addition,
+# options can be specified via the +RDOCOPT+ environment variable, which
+# functions similarly to the +RUBYOPT+ environment variable.
+#
+# % export RDOCOPT="-S"
+#
+# will make rdoc default to inline method source code. Command-line options
+# always will override those in +RDOCOPT+.
+#
+# Run
+#
+# % rdoc --help
+#
+# for full details on rdoc's options.
+#
+# Here are some of the most commonly used options.
+# [-d, --diagram]
+# Generate diagrams showing modules and
+# classes. You need dot V1.8.6 or later to
+# use the --diagram option correctly. Dot is
+# available from http://graphviz.org
+#
+# [-S, --inline-source]
+# Show method source code inline, rather than via a popup link.
+#
+# [-T, --template=NAME]
+# Set the template used when generating output.
+#
+# == Documenting Source Code
+#
+# Comment blocks can be written fairly naturally, either using +#+ on
+# successive lines of the comment, or by including the comment in
+# a =begin/=end block. If you use the latter form, the =begin line must be
+# flagged with an RDoc tag:
+#
+# =begin rdoc
+# Documentation to be processed by RDoc.
+#
+# ...
+# =end
+#
+# RDoc stops processing comments if it finds a comment line containing
+# a <tt>--</tt>. This can be used to separate external from internal
+# comments, or to stop a comment being associated with a method, class, or
+# module. Commenting can be turned back on with a line that starts with a
+# <tt>++</tt>.
+#
+# ##
+# # Extract the age and calculate the date-of-birth.
+# #--
+# # FIXME: fails if the birthday falls on February 29th
+# #++
+# # The DOB is returned as a Time object.
+#
+# def get_dob(person)
+# # ...
+# end
+#
+# Names of classes, files, and any method names containing an
+# underscore or preceded by a hash character are automatically hyperlinked
+# from comment text to their description.
+#
+# Method parameter lists are extracted and displayed with the method
+# description. If a method calls +yield+, then the parameters passed to yield
+# will also be displayed:
+#
+# def fred
+# ...
+# yield line, address
+#
+# This will get documented as:
+#
+# fred() { |line, address| ... }
+#
+# You can override this using a comment containing ':yields: ...' immediately
+# after the method definition
+#
+# def fred # :yields: index, position
+# # ...
+#
+# yield line, address
+#
+# which will get documented as
+#
+# fred() { |index, position| ... }
+#
+# +:yields:+ is an example of a documentation directive. These appear
+# immediately after the start of the document element they are modifying.
+#
+# == \Markup
+#
+# * The markup engine looks for a document's natural left margin. This is
+# used as the initial margin for the document.
+#
+# * Consecutive lines starting at this margin are considered to be a
+# paragraph.
+#
+# * If a paragraph starts with a "*", "-", or with "<digit>.", then it is
+# taken to be the start of a list. The margin in increased to be the first
+# non-space following the list start flag. Subsequent lines should be
+# indented to this new margin until the list ends. For example:
+#
+# * this is a list with three paragraphs in
+# the first item. This is the first paragraph.
+#
+# And this is the second paragraph.
+#
+# 1. This is an indented, numbered list.
+# 2. This is the second item in that list
+#
+# This is the third conventional paragraph in the
+# first list item.
+#
+# * This is the second item in the original list
+#
+# * You can also construct labeled lists, sometimes called description
+# or definition lists. Do this by putting the label in square brackets
+# and indenting the list body:
+#
+# [cat] a small furry mammal
+# that seems to sleep a lot
+#
+# [ant] a little insect that is known
+# to enjoy picnics
+#
+# A minor variation on labeled lists uses two colons to separate the
+# label from the list body:
+#
+# cat:: a small furry mammal
+# that seems to sleep a lot
+#
+# ant:: a little insect that is known
+# to enjoy picnics
+#
+# This latter style guarantees that the list bodies' left margins are
+# aligned: think of them as a two column table.
+#
+# * Any line that starts to the right of the current margin is treated
+# as verbatim text. This is useful for code listings. The example of a
+# list above is also verbatim text.
+#
+# * A line starting with an equals sign (=) is treated as a
+# heading. Level one headings have one equals sign, level two headings
+# have two,and so on.
+#
+# * A line starting with three or more hyphens (at the current indent)
+# generates a horizontal rule. The more hyphens, the thicker the rule
+# (within reason, and if supported by the output device)
+#
+# * You can use markup within text (except verbatim) to change the
+# appearance of parts of that text. Out of the box, RDoc::Markup
+# supports word-based and general markup.
+#
+# Word-based markup uses flag characters around individual words:
+#
+# [\*word*] displays word in a *bold* font
+# [\_word_] displays word in an _emphasized_ font
+# [\+word+] displays word in a +code+ font
+#
+# General markup affects text between a start delimiter and and end
+# delimiter. Not surprisingly, these delimiters look like HTML markup.
+#
+# [\<b>text...</b>] displays word in a *bold* font
+# [\<em>text...</em>] displays word in an _emphasized_ font
+# [\\<i>text...</i>] displays word in an <i>italicized</i> font
+# [\<tt>text...</tt>] displays word in a +code+ font
+#
+# Unlike conventional Wiki markup, general markup can cross line
+# boundaries. You can turn off the interpretation of markup by
+# preceding the first character with a backslash. This only works for
+# simple markup, not HTML-style markup.
+#
+# * Hyperlinks to the web starting http:, mailto:, ftp:, or www. are
+# recognized. An HTTP url that references an external image file is
+# converted into an inline <IMG..>. Hyperlinks starting 'link:' are
+# assumed to refer to local files whose path is relative to the --op
+# directory.
+#
+# Hyperlinks can also be of the form <tt>label</tt>[url], in which
+# case the label is used in the displayed text, and +url+ is
+# used as the target. If +label+ contains multiple words,
+# put it in braces: <em>{multi word label}[</em>url<em>]</em>.
+#
+# == Directives
+#
+# [+:nodoc:+ / +:nodoc:+ all]
+# This directive prevents documentation for the element from
+# being generated. For classes and modules, the methods, aliases,
+# constants, and attributes directly within the affected class or
+# module also will be omitted. By default, though, modules and
+# classes within that class of module _will_ be documented. This is
+# turned off by adding the +all+ modifier.
+#
+# module MyModule # :nodoc:
+# class Input
+# end
+# end
+#
+# module OtherModule # :nodoc: all
+# class Output
+# end
+# end
+#
+# In the above code, only class <tt>MyModule::Input</tt> will be documented.
+# The +:nodoc:+ directive is global across all files for the class or module
+# to which it applies, so use +:stopdoc:+/+:startdoc:+ to suppress
+# documentation only for a particular set of methods, etc.
+#
+# [+:doc:+]
+# Forces a method or attribute to be documented even if it wouldn't be
+# otherwise. Useful if, for example, you want to include documentation of a
+# particular private method.
+#
+# [+:notnew:+]
+# Only applicable to the +initialize+ instance method. Normally RDoc
+# assumes that the documentation and parameters for +initialize+ are
+# actually for the +new+ method, and so fakes out a +new+ for the class.
+# The +:notnew:+ modifier stops this. Remember that +initialize+ is private,
+# so you won't see the documentation unless you use the +-a+ command line
+# option.
+#
+# Comment blocks can contain other directives:
+#
+# [<tt>:section: title</tt>]
+# Starts a new section in the output. The title following +:section:+ is
+# used as the section heading, and the remainder of the comment containing
+# the section is used as introductory text. Subsequent methods, aliases,
+# attributes, and classes will be documented in this section. A :section:
+# comment block may have one or more lines before the :section: directive.
+# These will be removed, and any identical lines at the end of the block are
+# also removed. This allows you to add visual cues such as:
+#
+# # ----------------------------------------
+# # :section: My Section
+# # This is the section that I wrote.
+# # See it glisten in the noon-day sun.
+# # ----------------------------------------
+#
+# [+:call-seq:+]
+# Lines up to the next blank line in the comment are treated as the method's
+# calling sequence, overriding the default parsing of method parameters and
+# yield arguments.
+#
+# [+:include:+ _filename_]
+# \Include the contents of the named file at this point. The file will be
+# searched for in the directories listed by the +--include+ option, or in
+# the current directory by default. The contents of the file will be
+# shifted to have the same indentation as the ':' at the start of
+# the :include: directive.
+#
+# [+:title:+ _text_]
+# Sets the title for the document. Equivalent to the <tt>--title</tt>
+# command line parameter. (The command line parameter overrides any :title:
+# directive in the source).
+#
+# [+:enddoc:+]
+# Document nothing further at the current level.
+#
+# [+:main:+ _name_]
+# Equivalent to the <tt>--main</tt> command line parameter.
+#
+# [+:stopdoc:+ / +:startdoc:+]
+# Stop and start adding new documentation elements to the current container.
+# For example, if a class has a number of constants that you don't want to
+# document, put a +:stopdoc:+ before the first, and a +:startdoc:+ after the
+# last. If you don't specify a +:startdoc:+ by the end of the container,
+# disables documentation for the entire class or module.
+#
+# == Other stuff
+#
+# RDoc is currently being maintained by Eric Hodel <drbrain@segment7.net>
+#
+# Dave Thomas <dave@pragmaticprogrammer.com> is the original author of RDoc.
+#
+# == Credits
+#
+# * The Ruby parser in rdoc/parse.rb is based heavily on the outstanding
+# work of Keiju ISHITSUKA of Nippon Rational Inc, who produced the Ruby
+# parser for irb and the rtags package.
+#
+# * Code to diagram classes and modules was written by Sergey A Yanovitsky
+# (Jah) of Enticla.
+#
+# * Charset patch from MoonWolf.
+#
+# * Rich Kilmer wrote the kilmer.rb output template.
+#
+# * Dan Brickley led the design of the RDF format.
+#
+# == License
+#
+# RDoc is Copyright (c) 2001-2003 Dave Thomas, The Pragmatic Programmers. It
+# is free software, and may be redistributed under the terms specified
+# in the README file of the Ruby distribution.
+#
+# == Warranty
+#
+# This software is provided "as is" and without any express or implied
+# warranties, including, without limitation, the implied warranties of
+# merchantibility and fitness for a particular purpose.
+
+module RDoc
+
+ ##
+ # Exception thrown by any rdoc error.
+
+ class Error < RuntimeError; end
+
+ RDocError = Error # :nodoc:
+
+ ##
+ # RDoc version you are using
+
+ VERSION = "2.2.2"
+
+ ##
+ # Name of the dotfile that contains the description of files to be processed
+ # in the current directory
+
+ DOT_DOC_FILENAME = ".document"
+
+ GENERAL_MODIFIERS = %w[nodoc].freeze
+
+ CLASS_MODIFIERS = GENERAL_MODIFIERS
+
+ ATTR_MODIFIERS = GENERAL_MODIFIERS
+
+ CONSTANT_MODIFIERS = GENERAL_MODIFIERS
+
+ METHOD_MODIFIERS = GENERAL_MODIFIERS +
+ %w[arg args yield yields notnew not-new not_new doc]
+
+end
+
diff --git a/ruby/lib/rdoc/code_objects.rb b/ruby/lib/rdoc/code_objects.rb
new file mode 100644
index 0000000..0916b03
--- /dev/null
+++ b/ruby/lib/rdoc/code_objects.rb
@@ -0,0 +1,1061 @@
+# We represent the various high-level code constructs that appear
+# in Ruby programs: classes, modules, methods, and so on.
+
+require 'rdoc/tokenstream'
+
+module RDoc
+
+ ##
+ # We contain the common stuff for contexts (which are containers) and other
+ # elements (methods, attributes and so on)
+
+ class CodeObject
+
+ attr_accessor :parent
+
+ # We are the model of the code, but we know that at some point
+ # we will be worked on by viewers. By implementing the Viewable
+ # protocol, viewers can associated themselves with these objects.
+
+ attr_accessor :viewer
+
+ # are we done documenting (ie, did we come across a :enddoc:)?
+
+ attr_accessor :done_documenting
+
+ # Which section are we in
+
+ attr_accessor :section
+
+ # do we document ourselves?
+
+ attr_reader :document_self
+
+ def initialize
+ @document_self = true
+ @document_children = true
+ @force_documentation = false
+ @done_documenting = false
+ end
+
+ def document_self=(val)
+ @document_self = val
+ if !val
+ remove_methods_etc
+ end
+ end
+
+ # set and cleared by :startdoc: and :enddoc:, this is used to toggle
+ # the capturing of documentation
+ def start_doc
+ @document_self = true
+ @document_children = true
+ end
+
+ def stop_doc
+ @document_self = false
+ @document_children = false
+ end
+
+ # do we document ourselves and our children
+
+ attr_reader :document_children
+
+ def document_children=(val)
+ @document_children = val
+ if !val
+ remove_classes_and_modules
+ end
+ end
+
+ # Do we _force_ documentation, even is we wouldn't normally show the entity
+ attr_accessor :force_documentation
+
+ def parent_file_name
+ @parent ? @parent.file_base_name : '(unknown)'
+ end
+
+ def parent_name
+ @parent ? @parent.name : '(unknown)'
+ end
+
+ # Default callbacks to nothing, but this is overridden for classes
+ # and modules
+ def remove_classes_and_modules
+ end
+
+ def remove_methods_etc
+ end
+
+ # Access the code object's comment
+ attr_reader :comment
+
+ # Update the comment, but don't overwrite a real comment with an empty one
+ def comment=(comment)
+ @comment = comment unless comment.empty?
+ end
+
+ # There's a wee trick we pull. Comment blocks can have directives that
+ # override the stuff we extract during the parse. So, we have a special
+ # class method, attr_overridable, that lets code objects list
+ # those directives. Wehn a comment is assigned, we then extract
+ # out any matching directives and update our object
+
+ def self.attr_overridable(name, *aliases)
+ @overridables ||= {}
+
+ attr_accessor name
+
+ aliases.unshift name
+ aliases.each do |directive_name|
+ @overridables[directive_name.to_s] = name
+ end
+ end
+
+ end
+
+ ##
+ # A Context is something that can hold modules, classes, methods,
+ # attributes, aliases, requires, and includes. Classes, modules, and files
+ # are all Contexts.
+
+ class Context < CodeObject
+
+ attr_reader :aliases
+ attr_reader :attributes
+ attr_reader :constants
+ attr_reader :current_section
+ attr_reader :in_files
+ attr_reader :includes
+ attr_reader :method_list
+ attr_reader :name
+ attr_reader :requires
+ attr_reader :sections
+ attr_reader :visibility
+
+ class Section
+ attr_reader :title, :comment, :sequence
+
+ @@sequence = "SEC00000"
+
+ def initialize(title, comment)
+ @title = title
+ @@sequence.succ!
+ @sequence = @@sequence.dup
+ @comment = nil
+ set_comment(comment)
+ end
+
+ def ==(other)
+ self.class === other and @sequence == other.sequence
+ end
+
+ def inspect
+ "#<%s:0x%x %s %p>" % [
+ self.class, object_id,
+ @sequence, title
+ ]
+ end
+
+ ##
+ # Set the comment for this section from the original comment block If
+ # the first line contains :section:, strip it and use the rest.
+ # Otherwise remove lines up to the line containing :section:, and look
+ # for those lines again at the end and remove them. This lets us write
+ #
+ # # ---------------------
+ # # :SECTION: The title
+ # # The body
+ # # ---------------------
+
+ def set_comment(comment)
+ return unless comment
+
+ if comment =~ /^#[ \t]*:section:.*\n/
+ start = $`
+ rest = $'
+
+ if start.empty?
+ @comment = rest
+ else
+ @comment = rest.sub(/#{start.chomp}\Z/, '')
+ end
+ else
+ @comment = comment
+ end
+ @comment = nil if @comment.empty?
+ end
+
+ end
+
+ def initialize
+ super
+
+ @in_files = []
+
+ @name ||= "unknown"
+ @comment ||= ""
+ @parent = nil
+ @visibility = :public
+
+ @current_section = Section.new(nil, nil)
+ @sections = [ @current_section ]
+
+ initialize_methods_etc
+ initialize_classes_and_modules
+ end
+
+ ##
+ # map the class hash to an array externally
+
+ def classes
+ @classes.values
+ end
+
+ ##
+ # map the module hash to an array externally
+
+ def modules
+ @modules.values
+ end
+
+ ##
+ # return the classes Hash (only to be used internally)
+
+ def classes_hash
+ @classes
+ end
+ protected :classes_hash
+
+ ##
+ # return the modules Hash (only to be used internally)
+
+ def modules_hash
+ @modules
+ end
+ protected :modules_hash
+
+ ##
+ # Change the default visibility for new methods
+
+ def ongoing_visibility=(vis)
+ @visibility = vis
+ end
+
+ ##
+ # Yields Method and Attr entries matching the list of names in +methods+.
+ # Attributes are only returned when +singleton+ is false.
+
+ def methods_matching(methods, singleton = false)
+ count = 0
+
+ @method_list.each do |m|
+ if methods.include? m.name and m.singleton == singleton then
+ yield m
+ count += 1
+ end
+ end
+
+ return if count == methods.size || singleton
+
+ # perhaps we need to look at attributes
+
+ @attributes.each do |a|
+ yield a if methods.include? a.name
+ end
+ end
+
+ ##
+ # Given an array +methods+ of method names, set the visibility of the
+ # corresponding AnyMethod object
+
+ def set_visibility_for(methods, vis, singleton = false)
+ methods_matching methods, singleton do |m|
+ m.visibility = vis
+ end
+ end
+
+ ##
+ # Record the file that we happen to find it in
+
+ def record_location(toplevel)
+ @in_files << toplevel unless @in_files.include?(toplevel)
+ end
+
+ # Return true if at least part of this thing was defined in +file+
+ def defined_in?(file)
+ @in_files.include?(file)
+ end
+
+ def add_class(class_type, name, superclass)
+ klass = add_class_or_module @classes, class_type, name, superclass
+
+ #
+ # If the parser encounters Container::Item before encountering
+ # Container, then it assumes that Container is a module. This
+ # may not be the case, so remove Container from the module list
+ # if present and transfer any contained classes and modules to
+ # the new class.
+ #
+ mod = @modules.delete(name)
+
+ if mod then
+ klass.classes_hash.update(mod.classes_hash)
+ klass.modules_hash.update(mod.modules_hash)
+ klass.method_list.concat(mod.method_list)
+ end
+
+ return klass
+ end
+
+ def add_module(class_type, name)
+ add_class_or_module(@modules, class_type, name, nil)
+ end
+
+ def add_method(a_method)
+ a_method.visibility = @visibility
+ add_to(@method_list, a_method)
+
+ unmatched_alias_list = @unmatched_alias_lists[a_method.name]
+ if unmatched_alias_list then
+ unmatched_alias_list.each do |unmatched_alias|
+ add_alias_impl unmatched_alias, a_method
+ @aliases.delete unmatched_alias
+ end
+
+ @unmatched_alias_lists.delete a_method.name
+ end
+ end
+
+ def add_attribute(an_attribute)
+ add_to(@attributes, an_attribute)
+ end
+
+ def add_alias_impl(an_alias, meth)
+ new_meth = AnyMethod.new(an_alias.text, an_alias.new_name)
+ new_meth.is_alias_for = meth
+ new_meth.singleton = meth.singleton
+ new_meth.params = meth.params
+ new_meth.comment = "Alias for \##{meth.name}"
+ meth.add_alias(new_meth)
+ add_method(new_meth)
+ end
+
+ def add_alias(an_alias)
+ meth = find_instance_method_named(an_alias.old_name)
+
+ if meth then
+ add_alias_impl(an_alias, meth)
+ else
+ add_to(@aliases, an_alias)
+ unmatched_alias_list = @unmatched_alias_lists[an_alias.old_name] ||= []
+ unmatched_alias_list.push(an_alias)
+ end
+
+ an_alias
+ end
+
+ def add_include(an_include)
+ add_to(@includes, an_include)
+ end
+
+ def add_constant(const)
+ add_to(@constants, const)
+ end
+
+ # Requires always get added to the top-level (file) context
+ def add_require(a_require)
+ if TopLevel === self then
+ add_to @requires, a_require
+ else
+ parent.add_require a_require
+ end
+ end
+
+ def add_class_or_module(collection, class_type, name, superclass=nil)
+ cls = collection[name]
+
+ if cls then
+ cls.superclass = superclass unless cls.module?
+ puts "Reusing class/module #{name}" if $DEBUG_RDOC
+ else
+ cls = class_type.new(name, superclass)
+# collection[name] = cls if @document_self && !@done_documenting
+ collection[name] = cls if !@done_documenting
+ cls.parent = self
+ cls.section = @current_section
+ end
+ cls
+ end
+
+ def add_to(array, thing)
+ array << thing if @document_self and not @done_documenting
+ thing.parent = self
+ thing.section = @current_section
+ end
+
+ # If a class's documentation is turned off after we've started
+ # collecting methods etc., we need to remove the ones
+ # we have
+
+ def remove_methods_etc
+ initialize_methods_etc
+ end
+
+ def initialize_methods_etc
+ @method_list = []
+ @attributes = []
+ @aliases = []
+ @requires = []
+ @includes = []
+ @constants = []
+
+ # This Hash maps a method name to a list of unmatched
+ # aliases (aliases of a method not yet encountered).
+ @unmatched_alias_lists = {}
+ end
+
+ # and remove classes and modules when we see a :nodoc: all
+ def remove_classes_and_modules
+ initialize_classes_and_modules
+ end
+
+ def initialize_classes_and_modules
+ @classes = {}
+ @modules = {}
+ end
+
+ # Find a named module
+ def find_module_named(name)
+ # First check the enclosed modules, then check the module itself,
+ # then check the enclosing modules (this mirrors the check done by
+ # the Ruby parser)
+ res = @modules[name] || @classes[name]
+ return res if res
+ return self if self.name == name
+ find_enclosing_module_named(name)
+ end
+
+ # find a module at a higher scope
+ def find_enclosing_module_named(name)
+ parent && parent.find_module_named(name)
+ end
+
+ # Iterate over all the classes and modules in
+ # this object
+
+ def each_classmodule
+ @modules.each_value {|m| yield m}
+ @classes.each_value {|c| yield c}
+ end
+
+ def each_method
+ @method_list.each {|m| yield m}
+ end
+
+ def each_attribute
+ @attributes.each {|a| yield a}
+ end
+
+ def each_constant
+ @constants.each {|c| yield c}
+ end
+
+ # Return the toplevel that owns us
+
+ def toplevel
+ return @toplevel if defined? @toplevel
+ @toplevel = self
+ @toplevel = @toplevel.parent until TopLevel === @toplevel
+ @toplevel
+ end
+
+ # allow us to sort modules by name
+ def <=>(other)
+ name <=> other.name
+ end
+
+ ##
+ # Look up +symbol+. If +method+ is non-nil, then we assume the symbol
+ # references a module that contains that method.
+
+ def find_symbol(symbol, method = nil)
+ result = nil
+
+ case symbol
+ when /^::(.*)/ then
+ result = toplevel.find_symbol($1)
+ when /::/ then
+ modules = symbol.split(/::/)
+
+ unless modules.empty? then
+ module_name = modules.shift
+ result = find_module_named(module_name)
+
+ if result then
+ modules.each do |name|
+ result = result.find_module_named(name)
+ break unless result
+ end
+ end
+ end
+
+ else
+ # if a method is specified, then we're definitely looking for
+ # a module, otherwise it could be any symbol
+ if method
+ result = find_module_named(symbol)
+ else
+ result = find_local_symbol(symbol)
+ if result.nil?
+ if symbol =~ /^[A-Z]/
+ result = parent
+ while result && result.name != symbol
+ result = result.parent
+ end
+ end
+ end
+ end
+ end
+
+ if result and method then
+ fail unless result.respond_to? :find_local_symbol
+ result = result.find_local_symbol(method)
+ end
+
+ result
+ end
+
+ def find_local_symbol(symbol)
+ res = find_method_named(symbol) ||
+ find_constant_named(symbol) ||
+ find_attribute_named(symbol) ||
+ find_module_named(symbol) ||
+ find_file_named(symbol)
+ end
+
+ # Handle sections
+
+ def set_current_section(title, comment)
+ @current_section = Section.new(title, comment)
+ @sections << @current_section
+ end
+
+ private
+
+ # Find a named method, or return nil
+ def find_method_named(name)
+ @method_list.find {|meth| meth.name == name}
+ end
+
+ # Find a named instance method, or return nil
+ def find_instance_method_named(name)
+ @method_list.find {|meth| meth.name == name && !meth.singleton}
+ end
+
+ # Find a named constant, or return nil
+ def find_constant_named(name)
+ @constants.find {|m| m.name == name}
+ end
+
+ # Find a named attribute, or return nil
+ def find_attribute_named(name)
+ @attributes.find {|m| m.name == name}
+ end
+
+ ##
+ # Find a named file, or return nil
+
+ def find_file_named(name)
+ toplevel.class.find_file_named(name)
+ end
+
+ end
+
+ ##
+ # A TopLevel context is a source file
+
+ class TopLevel < Context
+ attr_accessor :file_stat
+ attr_accessor :file_relative_name
+ attr_accessor :file_absolute_name
+ attr_accessor :diagram
+
+ @@all_classes = {}
+ @@all_modules = {}
+ @@all_files = {}
+
+ def self.reset
+ @@all_classes = {}
+ @@all_modules = {}
+ @@all_files = {}
+ end
+
+ def initialize(file_name)
+ super()
+ @name = "TopLevel"
+ @file_relative_name = file_name
+ @file_absolute_name = file_name
+ @file_stat = File.stat(file_name)
+ @diagram = nil
+ @@all_files[file_name] = self
+ end
+
+ def file_base_name
+ File.basename @file_absolute_name
+ end
+
+ def full_name
+ nil
+ end
+
+ ##
+ # Adding a class or module to a TopLevel is special, as we only want one
+ # copy of a particular top-level class. For example, if both file A and
+ # file B implement class C, we only want one ClassModule object for C.
+ # This code arranges to share classes and modules between files.
+
+ def add_class_or_module(collection, class_type, name, superclass)
+ cls = collection[name]
+
+ if cls then
+ cls.superclass = superclass unless cls.module?
+ puts "Reusing class/module #{cls.full_name}" if $DEBUG_RDOC
+ else
+ if class_type == NormalModule then
+ all = @@all_modules
+ else
+ all = @@all_classes
+ end
+
+ cls = all[name]
+
+ if !cls then
+ cls = class_type.new name, superclass
+ all[name] = cls unless @done_documenting
+ else
+ # If the class has been encountered already, check that its
+ # superclass has been set (it may not have been, depending on
+ # the context in which it was encountered).
+ if class_type == NormalClass
+ if !cls.superclass then
+ cls.superclass = superclass
+ end
+ end
+ end
+
+ collection[name] = cls unless @done_documenting
+
+ cls.parent = self
+ end
+
+ cls
+ end
+
+ def self.all_classes_and_modules
+ @@all_classes.values + @@all_modules.values
+ end
+
+ def self.find_class_named(name)
+ @@all_classes.each_value do |c|
+ res = c.find_class_named(name)
+ return res if res
+ end
+ nil
+ end
+
+ def self.find_file_named(name)
+ @@all_files[name]
+ end
+
+ def find_local_symbol(symbol)
+ find_class_or_module_named(symbol) || super
+ end
+
+ def find_class_or_module_named(symbol)
+ @@all_classes.each_value {|c| return c if c.name == symbol}
+ @@all_modules.each_value {|m| return m if m.name == symbol}
+ nil
+ end
+
+ ##
+ # Find a named module
+
+ def find_module_named(name)
+ find_class_or_module_named(name) || find_enclosing_module_named(name)
+ end
+
+ def inspect
+ "#<%s:0x%x %p modules: %p classes: %p>" % [
+ self.class, object_id,
+ file_base_name,
+ @modules.map { |n,m| m },
+ @classes.map { |n,c| c }
+ ]
+ end
+
+ end
+
+ ##
+ # ClassModule is the base class for objects representing either a class or a
+ # module.
+
+ class ClassModule < Context
+
+ attr_accessor :diagram
+
+ def initialize(name, superclass = nil)
+ @name = name
+ @diagram = nil
+ @superclass = superclass
+ @comment = ""
+ super()
+ end
+
+ def find_class_named(name)
+ return self if full_name == name
+ @classes.each_value {|c| return c if c.find_class_named(name) }
+ nil
+ end
+
+ ##
+ # Return the fully qualified name of this class or module
+
+ def full_name
+ if @parent && @parent.full_name
+ @parent.full_name + "::" + @name
+ else
+ @name
+ end
+ end
+
+ def http_url(prefix)
+ path = full_name.split("::")
+ File.join(prefix, *path) + ".html"
+ end
+
+ ##
+ # Does this object represent a module?
+
+ def module?
+ false
+ end
+
+ ##
+ # Get the superclass of this class. Attempts to retrieve the superclass'
+ # real name by following module nesting.
+
+ def superclass
+ raise NoMethodError, "#{full_name} is a module" if module?
+
+ scope = self
+
+ begin
+ superclass = scope.classes.find { |c| c.name == @superclass }
+
+ return superclass.full_name if superclass
+ scope = scope.parent
+ end until scope.nil? or TopLevel === scope
+
+ @superclass
+ end
+
+ ##
+ # Set the superclass of this class
+
+ def superclass=(superclass)
+ raise NoMethodError, "#{full_name} is a module" if module?
+
+ if @superclass.nil? or @superclass == 'Object' then
+ @superclass = superclass
+ end
+ end
+
+ def to_s
+ "#{self.class}: #{@name} #{@comment} #{super}"
+ end
+
+ end
+
+ ##
+ # Anonymous classes
+
+ class AnonClass < ClassModule
+ end
+
+ ##
+ # Normal classes
+
+ class NormalClass < ClassModule
+
+ def inspect
+ superclass = @superclass ? " < #{@superclass}" : nil
+ "<%s:0x%x class %s%s includes: %p attributes: %p methods: %p aliases: %p>" % [
+ self.class, object_id,
+ @name, superclass, @includes, @attributes, @method_list, @aliases
+ ]
+ end
+
+ end
+
+ ##
+ # Singleton classes
+
+ class SingleClass < ClassModule
+ end
+
+ ##
+ # Module
+
+ class NormalModule < ClassModule
+
+ def comment=(comment)
+ return if comment.empty?
+ comment = @comment << "# ---\n" << comment unless @comment.empty?
+
+ super
+ end
+
+ def inspect
+ "#<%s:0x%x module %s includes: %p attributes: %p methods: %p aliases: %p>" % [
+ self.class, object_id,
+ @name, @includes, @attributes, @method_list, @aliases
+ ]
+ end
+
+ def module?
+ true
+ end
+
+ end
+
+ ##
+ # AnyMethod is the base class for objects representing methods
+
+ class AnyMethod < CodeObject
+
+ attr_accessor :name
+ attr_accessor :visibility
+ attr_accessor :block_params
+ attr_accessor :dont_rename_initialize
+ attr_accessor :singleton
+ attr_reader :text
+
+ # list of other names for this method
+ attr_reader :aliases
+
+ # method we're aliasing
+ attr_accessor :is_alias_for
+
+ attr_overridable :params, :param, :parameters, :parameter
+
+ attr_accessor :call_seq
+
+ include TokenStream
+
+ def initialize(text, name)
+ super()
+ @text = text
+ @name = name
+ @token_stream = nil
+ @visibility = :public
+ @dont_rename_initialize = false
+ @block_params = nil
+ @aliases = []
+ @is_alias_for = nil
+ @comment = ""
+ @call_seq = nil
+ end
+
+ def <=>(other)
+ @name <=> other.name
+ end
+
+ def add_alias(method)
+ @aliases << method
+ end
+
+ def inspect
+ alias_for = @is_alias_for ? " (alias for #{@is_alias_for.name})" : nil
+ "#<%s:0x%x %s%s%s (%s)%s>" % [
+ self.class, object_id,
+ parent_name,
+ singleton ? '::' : '#',
+ name,
+ visibility,
+ alias_for,
+ ]
+ end
+
+ def param_seq
+ params = params.gsub(/\s*\#.*/, '')
+ params = params.tr("\n", " ").squeeze(" ")
+ params = "(#{params})" unless p[0] == ?(
+
+ if block = block_params then # yes, =
+ # If this method has explicit block parameters, remove any explicit
+ # &block
+ params.sub!(/,?\s*&\w+/)
+
+ block.gsub!(/\s*\#.*/, '')
+ block = block.tr("\n", " ").squeeze(" ")
+ if block[0] == ?(
+ block.sub!(/^\(/, '').sub!(/\)/, '')
+ end
+ params << " { |#{block}| ... }"
+ end
+
+ params
+ end
+
+ def to_s
+ res = self.class.name + ": " + @name + " (" + @text + ")\n"
+ res << @comment.to_s
+ res
+ end
+
+ end
+
+ ##
+ # GhostMethod represents a method referenced only by a comment
+
+ class GhostMethod < AnyMethod
+ end
+
+ ##
+ # MetaMethod represents a meta-programmed method
+
+ class MetaMethod < AnyMethod
+ end
+
+ ##
+ # Represent an alias, which is an old_name/ new_name pair associated with a
+ # particular context
+
+ class Alias < CodeObject
+
+ attr_accessor :text, :old_name, :new_name, :comment
+
+ def initialize(text, old_name, new_name, comment)
+ super()
+ @text = text
+ @old_name = old_name
+ @new_name = new_name
+ self.comment = comment
+ end
+
+ def inspect
+ "#<%s:0x%x %s.alias_method %s, %s>" % [
+ self.class, object_id,
+ parent.name, @old_name, @new_name,
+ ]
+ end
+
+ def to_s
+ "alias: #{self.old_name} -> #{self.new_name}\n#{self.comment}"
+ end
+
+ end
+
+ ##
+ # Represent a constant
+
+ class Constant < CodeObject
+ attr_accessor :name, :value
+
+ def initialize(name, value, comment)
+ super()
+ @name = name
+ @value = value
+ self.comment = comment
+ end
+ end
+
+ ##
+ # Represent attributes
+
+ class Attr < CodeObject
+ attr_accessor :text, :name, :rw, :visibility
+
+ def initialize(text, name, rw, comment)
+ super()
+ @text = text
+ @name = name
+ @rw = rw
+ @visibility = :public
+ self.comment = comment
+ end
+
+ def <=>(other)
+ self.name <=> other.name
+ end
+
+ def inspect
+ attr = case rw
+ when 'RW' then :attr_accessor
+ when 'R' then :attr_reader
+ when 'W' then :attr_writer
+ else
+ " (#{rw})"
+ end
+
+ "#<%s:0x%x %s.%s :%s>" % [
+ self.class, object_id,
+ parent_name, attr, @name,
+ ]
+ end
+
+ def to_s
+ "attr: #{self.name} #{self.rw}\n#{self.comment}"
+ end
+
+ end
+
+ ##
+ # A required file
+
+ class Require < CodeObject
+ attr_accessor :name
+
+ def initialize(name, comment)
+ super()
+ @name = name.gsub(/'|"/, "") #'
+ self.comment = comment
+ end
+
+ def inspect
+ "#<%s:0x%x require '%s' in %s>" % [
+ self.class,
+ object_id,
+ @name,
+ parent_file_name,
+ ]
+ end
+
+ end
+
+ ##
+ # An included module
+
+ class Include < CodeObject
+
+ attr_accessor :name
+
+ def initialize(name, comment)
+ super()
+ @name = name
+ self.comment = comment
+
+ end
+
+ def inspect
+ "#<%s:0x%x %s.include %s>" % [
+ self.class,
+ object_id,
+ parent_name, @name,
+ ]
+ end
+
+ end
+
+end
diff --git a/ruby/lib/rdoc/diagram.rb b/ruby/lib/rdoc/diagram.rb
new file mode 100644
index 0000000..4aa2ec5
--- /dev/null
+++ b/ruby/lib/rdoc/diagram.rb
@@ -0,0 +1,340 @@
+# A wonderful hack by to draw package diagrams using the dot package.
+# Originally written by Jah, team Enticla.
+#
+# You must have the V1.7 or later in your path
+# http://www.research.att.com/sw/tools/graphviz/
+
+require 'rdoc/dot'
+
+module RDoc
+
+ ##
+ # Draw a set of diagrams representing the modules and classes in the
+ # system. We draw one diagram for each file, and one for each toplevel
+ # class or module. This means there will be overlap. However, it also
+ # means that you'll get better context for objects.
+ #
+ # To use, simply
+ #
+ # d = Diagram.new(info) # pass in collection of top level infos
+ # d.draw
+ #
+ # The results will be written to the +dot+ subdirectory. The process
+ # also sets the +diagram+ attribute in each object it graphs to
+ # the name of the file containing the image. This can be used
+ # by output generators to insert images.
+
+ class Diagram
+
+ FONT = "Arial"
+
+ DOT_PATH = "dot"
+
+ ##
+ # Pass in the set of top level objects. The method also creates the
+ # subdirectory to hold the images
+
+ def initialize(info, options)
+ @info = info
+ @options = options
+ @counter = 0
+ FileUtils.mkdir_p(DOT_PATH)
+ @diagram_cache = {}
+ end
+
+ ##
+ # Draw the diagrams. We traverse the files, drawing a diagram for each. We
+ # also traverse each top-level class and module in that file drawing a
+ # diagram for these too.
+
+ def draw
+ unless @options.quiet
+ $stderr.print "Diagrams: "
+ $stderr.flush
+ end
+
+ @info.each_with_index do |i, file_count|
+ @done_modules = {}
+ @local_names = find_names(i)
+ @global_names = []
+ @global_graph = graph = DOT::Digraph.new('name' => 'TopLevel',
+ 'fontname' => FONT,
+ 'fontsize' => '8',
+ 'bgcolor' => 'lightcyan1',
+ 'compound' => 'true')
+
+ # it's a little hack %) i'm too lazy to create a separate class
+ # for default node
+ graph << DOT::Node.new('name' => 'node',
+ 'fontname' => FONT,
+ 'color' => 'black',
+ 'fontsize' => 8)
+
+ i.modules.each do |mod|
+ draw_module(mod, graph, true, i.file_relative_name)
+ end
+ add_classes(i, graph, i.file_relative_name)
+
+ i.diagram = convert_to_png("f_#{file_count}", graph)
+
+ # now go through and document each top level class and
+ # module independently
+ i.modules.each_with_index do |mod, count|
+ @done_modules = {}
+ @local_names = find_names(mod)
+ @global_names = []
+
+ @global_graph = graph = DOT::Digraph.new('name' => 'TopLevel',
+ 'fontname' => FONT,
+ 'fontsize' => '8',
+ 'bgcolor' => 'lightcyan1',
+ 'compound' => 'true')
+
+ graph << DOT::Node.new('name' => 'node',
+ 'fontname' => FONT,
+ 'color' => 'black',
+ 'fontsize' => 8)
+ draw_module(mod, graph, true)
+ mod.diagram = convert_to_png("m_#{file_count}_#{count}",
+ graph)
+ end
+ end
+ $stderr.puts unless @options.quiet
+ end
+
+ private
+
+ def find_names(mod)
+ return [mod.full_name] + mod.classes.collect{|cl| cl.full_name} +
+ mod.modules.collect{|m| find_names(m)}.flatten
+ end
+
+ def find_full_name(name, mod)
+ full_name = name.dup
+ return full_name if @local_names.include?(full_name)
+ mod_path = mod.full_name.split('::')[0..-2]
+ unless mod_path.nil?
+ until mod_path.empty?
+ full_name = mod_path.pop + '::' + full_name
+ return full_name if @local_names.include?(full_name)
+ end
+ end
+ return name
+ end
+
+ def draw_module(mod, graph, toplevel = false, file = nil)
+ return if @done_modules[mod.full_name] and not toplevel
+
+ @counter += 1
+ url = mod.http_url("classes")
+ m = DOT::Subgraph.new('name' => "cluster_#{mod.full_name.gsub( /:/,'_' )}",
+ 'label' => mod.name,
+ 'fontname' => FONT,
+ 'color' => 'blue',
+ 'style' => 'filled',
+ 'URL' => %{"#{url}"},
+ 'fillcolor' => toplevel ? 'palegreen1' : 'palegreen3')
+
+ @done_modules[mod.full_name] = m
+ add_classes(mod, m, file)
+ graph << m
+
+ unless mod.includes.empty?
+ mod.includes.each do |inc|
+ m_full_name = find_full_name(inc.name, mod)
+ if @local_names.include?(m_full_name)
+ @global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}",
+ 'to' => "#{mod.full_name.gsub( /:/,'_' )}",
+ 'ltail' => "cluster_#{m_full_name.gsub( /:/,'_' )}",
+ 'lhead' => "cluster_#{mod.full_name.gsub( /:/,'_' )}")
+ else
+ unless @global_names.include?(m_full_name)
+ path = m_full_name.split("::")
+ url = File.join('classes', *path) + ".html"
+ @global_graph << DOT::Node.new('name' => "#{m_full_name.gsub( /:/,'_' )}",
+ 'shape' => 'box',
+ 'label' => "#{m_full_name}",
+ 'URL' => %{"#{url}"})
+ @global_names << m_full_name
+ end
+ @global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}",
+ 'to' => "#{mod.full_name.gsub( /:/,'_' )}",
+ 'lhead' => "cluster_#{mod.full_name.gsub( /:/,'_' )}")
+ end
+ end
+ end
+ end
+
+ def add_classes(container, graph, file = nil )
+
+ use_fileboxes = @options.fileboxes
+
+ files = {}
+
+ # create dummy node (needed if empty and for module includes)
+ if container.full_name
+ graph << DOT::Node.new('name' => "#{container.full_name.gsub( /:/,'_' )}",
+ 'label' => "",
+ 'width' => (container.classes.empty? and
+ container.modules.empty?) ?
+ '0.75' : '0.01',
+ 'height' => '0.01',
+ 'shape' => 'plaintext')
+ end
+
+ container.classes.each_with_index do |cl, cl_index|
+ last_file = cl.in_files[-1].file_relative_name
+
+ if use_fileboxes && !files.include?(last_file)
+ @counter += 1
+ files[last_file] =
+ DOT::Subgraph.new('name' => "cluster_#{@counter}",
+ 'label' => "#{last_file}",
+ 'fontname' => FONT,
+ 'color'=>
+ last_file == file ? 'red' : 'black')
+ end
+
+ next if cl.name == 'Object' || cl.name[0,2] == "<<"
+
+ url = cl.http_url("classes")
+
+ label = cl.name.dup
+ if use_fileboxes && cl.in_files.length > 1
+ label << '\n[' +
+ cl.in_files.collect {|i|
+ i.file_relative_name
+ }.sort.join( '\n' ) +
+ ']'
+ end
+
+ attrs = {
+ 'name' => "#{cl.full_name.gsub( /:/, '_' )}",
+ 'fontcolor' => 'black',
+ 'style'=>'filled',
+ 'color'=>'palegoldenrod',
+ 'label' => label,
+ 'shape' => 'ellipse',
+ 'URL' => %{"#{url}"}
+ }
+
+ c = DOT::Node.new(attrs)
+
+ if use_fileboxes
+ files[last_file].push c
+ else
+ graph << c
+ end
+ end
+
+ if use_fileboxes
+ files.each_value do |val|
+ graph << val
+ end
+ end
+
+ unless container.classes.empty?
+ container.classes.each_with_index do |cl, cl_index|
+ cl.includes.each do |m|
+ m_full_name = find_full_name(m.name, cl)
+ if @local_names.include?(m_full_name)
+ @global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}",
+ 'to' => "#{cl.full_name.gsub( /:/,'_' )}",
+ 'ltail' => "cluster_#{m_full_name.gsub( /:/,'_' )}")
+ else
+ unless @global_names.include?(m_full_name)
+ path = m_full_name.split("::")
+ url = File.join('classes', *path) + ".html"
+ @global_graph << DOT::Node.new('name' => "#{m_full_name.gsub( /:/,'_' )}",
+ 'shape' => 'box',
+ 'label' => "#{m_full_name}",
+ 'URL' => %{"#{url}"})
+ @global_names << m_full_name
+ end
+ @global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}",
+ 'to' => "#{cl.full_name.gsub( /:/, '_')}")
+ end
+ end
+
+ sclass = cl.superclass
+ next if sclass.nil? || sclass == 'Object'
+ sclass_full_name = find_full_name(sclass,cl)
+ unless @local_names.include?(sclass_full_name) or @global_names.include?(sclass_full_name)
+ path = sclass_full_name.split("::")
+ url = File.join('classes', *path) + ".html"
+ @global_graph << DOT::Node.new('name' => "#{sclass_full_name.gsub( /:/, '_' )}",
+ 'label' => sclass_full_name,
+ 'URL' => %{"#{url}"})
+ @global_names << sclass_full_name
+ end
+ @global_graph << DOT::Edge.new('from' => "#{sclass_full_name.gsub( /:/,'_' )}",
+ 'to' => "#{cl.full_name.gsub( /:/, '_')}")
+ end
+ end
+
+ container.modules.each do |submod|
+ draw_module(submod, graph)
+ end
+
+ end
+
+ def convert_to_png(file_base, graph)
+ str = graph.to_s
+ return @diagram_cache[str] if @diagram_cache[str]
+ op_type = @options.image_format
+ dotfile = File.join(DOT_PATH, file_base)
+ src = dotfile + ".dot"
+ dot = dotfile + "." + op_type
+
+ unless @options.quiet
+ $stderr.print "."
+ $stderr.flush
+ end
+
+ File.open(src, 'w+' ) do |f|
+ f << str << "\n"
+ end
+
+ system "dot", "-T#{op_type}", src, "-o", dot
+
+ # Now construct the imagemap wrapper around
+ # that png
+
+ ret = wrap_in_image_map(src, dot)
+ @diagram_cache[str] = ret
+ return ret
+ end
+
+ ##
+ # Extract the client-side image map from dot, and use it to generate the
+ # imagemap proper. Return the whole <map>..<img> combination, suitable for
+ # inclusion on the page
+
+ def wrap_in_image_map(src, dot)
+ res = ""
+ dot_map = `dot -Tismap #{src}`
+
+ if(!dot_map.empty?)
+ res << %{<map id="map" name="map">\n}
+ dot_map.split($/).each do |area|
+ unless area =~ /^rectangle \((\d+),(\d+)\) \((\d+),(\d+)\) ([\/\w.]+)\s*(.*)/
+ $stderr.puts "Unexpected output from dot:\n#{area}"
+ return nil
+ end
+
+ xs, ys = [$1.to_i, $3.to_i], [$2.to_i, $4.to_i]
+ url, area_name = $5, $6
+
+ res << %{ <area shape="rect" coords="#{xs.min},#{ys.min},#{xs.max},#{ys.max}" }
+ res << %{ href="#{url}" alt="#{area_name}" />\n}
+ end
+ res << "</map>\n"
+ end
+
+ res << %{<img src="#{dot}" usemap="#map" alt="#{dot}" />}
+ return res
+ end
+
+ end
+
+end
diff --git a/ruby/lib/rdoc/dot.rb b/ruby/lib/rdoc/dot.rb
new file mode 100644
index 0000000..fbd2cfb
--- /dev/null
+++ b/ruby/lib/rdoc/dot.rb
@@ -0,0 +1,249 @@
+module RDoc; end
+
+module RDoc::DOT
+
+ TAB = ' '
+ TAB2 = TAB * 2
+
+ # options for node declaration
+ NODE_OPTS = [
+ 'bgcolor',
+ 'color',
+ 'fontcolor',
+ 'fontname',
+ 'fontsize',
+ 'height',
+ 'width',
+ 'label',
+ 'layer',
+ 'rank',
+ 'shape',
+ 'shapefile',
+ 'style',
+ 'URL',
+ ]
+
+ # options for edge declaration
+ EDGE_OPTS = [
+ 'color',
+ 'decorate',
+ 'dir',
+ 'fontcolor',
+ 'fontname',
+ 'fontsize',
+ 'id',
+ 'label',
+ 'layer',
+ 'lhead',
+ 'ltail',
+ 'minlen',
+ 'style',
+ 'weight'
+ ]
+
+ # options for graph declaration
+ GRAPH_OPTS = [
+ 'bgcolor',
+ 'center',
+ 'clusterrank',
+ 'color',
+ 'compound',
+ 'concentrate',
+ 'fillcolor',
+ 'fontcolor',
+ 'fontname',
+ 'fontsize',
+ 'label',
+ 'layerseq',
+ 'margin',
+ 'mclimit',
+ 'nodesep',
+ 'nslimit',
+ 'ordering',
+ 'orientation',
+ 'page',
+ 'rank',
+ 'rankdir',
+ 'ranksep',
+ 'ratio',
+ 'size',
+ 'style',
+ 'URL'
+ ]
+
+ # a root class for any element in dot notation
+ class SimpleElement
+ attr_accessor :name
+
+ def initialize( params = {} )
+ @label = params['name'] ? params['name'] : ''
+ end
+
+ def to_s
+ @name
+ end
+ end
+
+ # an element that has options ( node, edge or graph )
+ class Element < SimpleElement
+ #attr_reader :parent
+ attr_accessor :name, :options
+
+ def initialize( params = {}, option_list = [] )
+ super( params )
+ @name = params['name'] ? params['name'] : nil
+ @parent = params['parent'] ? params['parent'] : nil
+ @options = {}
+ option_list.each{ |i|
+ @options[i] = params[i] if params[i]
+ }
+ @options['label'] ||= @name if @name != 'node'
+ end
+
+ def each_option
+ @options.each{ |i| yield i }
+ end
+
+ def each_option_pair
+ @options.each_pair{ |key, val| yield key, val }
+ end
+
+ #def parent=( thing )
+ # @parent.delete( self ) if defined?( @parent ) and @parent
+ # @parent = thing
+ #end
+ end
+
+
+ # this is used when we build nodes that have shape=record
+ # ports don't have options :)
+ class Port < SimpleElement
+ attr_accessor :label
+
+ def initialize( params = {} )
+ super( params )
+ @name = params['label'] ? params['label'] : ''
+ end
+ def to_s
+ ( @name && @name != "" ? "<#{@name}>" : "" ) + "#{@label}"
+ end
+ end
+
+ # node element
+ class Node < Element
+
+ def initialize( params = {}, option_list = NODE_OPTS )
+ super( params, option_list )
+ @ports = params['ports'] ? params['ports'] : []
+ end
+
+ def each_port
+ @ports.each{ |i| yield i }
+ end
+
+ def << ( thing )
+ @ports << thing
+ end
+
+ def push ( thing )
+ @ports.push( thing )
+ end
+
+ def pop
+ @ports.pop
+ end
+
+ def to_s( t = '' )
+
+ label = @options['shape'] != 'record' && @ports.length == 0 ?
+ @options['label'] ?
+ t + TAB + "label = \"#{@options['label']}\"\n" :
+ '' :
+ t + TAB + 'label = "' + " \\\n" +
+ t + TAB2 + "#{@options['label']}| \\\n" +
+ @ports.collect{ |i|
+ t + TAB2 + i.to_s
+ }.join( "| \\\n" ) + " \\\n" +
+ t + TAB + '"' + "\n"
+
+ t + "#{@name} [\n" +
+ @options.to_a.collect{ |i|
+ i[1] && i[0] != 'label' ?
+ t + TAB + "#{i[0]} = #{i[1]}" : nil
+ }.compact.join( ",\n" ) + ( label != '' ? ",\n" : "\n" ) +
+ label +
+ t + "]\n"
+ end
+ end
+
+ # subgraph element is the same to graph, but has another header in dot
+ # notation
+ class Subgraph < Element
+
+ def initialize( params = {}, option_list = GRAPH_OPTS )
+ super( params, option_list )
+ @nodes = params['nodes'] ? params['nodes'] : []
+ @dot_string = 'subgraph'
+ end
+
+ def each_node
+ @nodes.each{ |i| yield i }
+ end
+
+ def << ( thing )
+ @nodes << thing
+ end
+
+ def push( thing )
+ @nodes.push( thing )
+ end
+
+ def pop
+ @nodes.pop
+ end
+
+ def to_s( t = '' )
+ hdr = t + "#{@dot_string} #{@name} {\n"
+
+ options = @options.to_a.collect{ |name, val|
+ val && name != 'label' ?
+ t + TAB + "#{name} = #{val}" :
+ name ? t + TAB + "#{name} = \"#{val}\"" : nil
+ }.compact.join( "\n" ) + "\n"
+
+ nodes = @nodes.collect{ |i|
+ i.to_s( t + TAB )
+ }.join( "\n" ) + "\n"
+ hdr + options + nodes + t + "}\n"
+ end
+ end
+
+ # this is graph
+ class Digraph < Subgraph
+ def initialize( params = {}, option_list = GRAPH_OPTS )
+ super( params, option_list )
+ @dot_string = 'digraph'
+ end
+ end
+
+ # this is edge
+ class Edge < Element
+ attr_accessor :from, :to
+ def initialize( params = {}, option_list = EDGE_OPTS )
+ super( params, option_list )
+ @from = params['from'] ? params['from'] : nil
+ @to = params['to'] ? params['to'] : nil
+ end
+
+ def to_s( t = '' )
+ t + "#{@from} -> #{to} [\n" +
+ @options.to_a.collect{ |i|
+ i[1] && i[0] != 'label' ?
+ t + TAB + "#{i[0]} = #{i[1]}" :
+ i[1] ? t + TAB + "#{i[0]} = \"#{i[1]}\"" : nil
+ }.compact.join( "\n" ) + "\n" + t + "]\n"
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/generator.rb b/ruby/lib/rdoc/generator.rb
new file mode 100644
index 0000000..d695e66
--- /dev/null
+++ b/ruby/lib/rdoc/generator.rb
@@ -0,0 +1,1082 @@
+require 'cgi'
+require 'rdoc'
+require 'rdoc/options'
+require 'rdoc/markup/to_html_crossref'
+require 'rdoc/template'
+
+module RDoc::Generator
+
+ ##
+ # Name of sub-directory that holds file descriptions
+
+ FILE_DIR = "files"
+
+ ##
+ # Name of sub-directory that holds class descriptions
+
+ CLASS_DIR = "classes"
+
+ ##
+ # Name of the RDoc CSS file
+
+ CSS_NAME = "rdoc-style.css"
+
+ ##
+ # Build a hash of all items that can be cross-referenced. This is used when
+ # we output required and included names: if the names appear in this hash,
+ # we can generate an html cross reference to the appropriate description.
+ # We also use this when parsing comment blocks: any decorated words matching
+ # an entry in this list are hyperlinked.
+
+ class AllReferences
+ @@refs = {}
+
+ def AllReferences::reset
+ @@refs = {}
+ end
+
+ def AllReferences.add(name, html_class)
+ @@refs[name] = html_class
+ end
+
+ def AllReferences.[](name)
+ @@refs[name]
+ end
+
+ def AllReferences.keys
+ @@refs.keys
+ end
+ end
+
+ ##
+ # Handle common markup tasks for the various Context subclasses
+
+ module MarkUp
+
+ ##
+ # Convert a string in markup format into HTML.
+
+ def markup(str, remove_para = false)
+ return '' unless str
+
+ # Convert leading comment markers to spaces, but only if all non-blank
+ # lines have them
+ if str =~ /^(?>\s*)[^\#]/ then
+ content = str
+ else
+ content = str.gsub(/^\s*(#+)/) { $1.tr '#', ' ' }
+ end
+
+ res = formatter.convert content
+
+ if remove_para then
+ res.sub!(/^<p>/, '')
+ res.sub!(/<\/p>$/, '')
+ end
+
+ res
+ end
+
+ ##
+ # Qualify a stylesheet URL; if if +css_name+ does not begin with '/' or
+ # 'http[s]://', prepend a prefix relative to +path+. Otherwise, return it
+ # unmodified.
+
+ def style_url(path, css_name=nil)
+# $stderr.puts "style_url( #{path.inspect}, #{css_name.inspect} )"
+ css_name ||= CSS_NAME
+ if %r{^(https?:/)?/} =~ css_name
+ css_name
+ else
+ RDoc::Markup::ToHtml.gen_relative_url path, css_name
+ end
+ end
+
+ ##
+ # Build a webcvs URL with the given 'url' argument. URLs with a '%s' in them
+ # get the file's path sprintfed into them; otherwise they're just catenated
+ # together.
+
+ def cvs_url(url, full_path)
+ if /%s/ =~ url
+ return sprintf( url, full_path )
+ else
+ return url + full_path
+ end
+ end
+
+ end
+
+ ##
+ # A Context is built by the parser to represent a container: contexts hold
+ # classes, modules, methods, require lists and include lists. ClassModule
+ # and TopLevel are the context objects we process here
+
+ class Context
+
+ include MarkUp
+
+ attr_reader :context
+
+ ##
+ # Generate:
+ #
+ # * a list of RDoc::Generator::File objects for each TopLevel object
+ # * a list of RDoc::Generator::Class objects for each first level class or
+ # module in the TopLevel objects
+ # * a complete list of all hyperlinkable terms (file, class, module, and
+ # method names)
+
+ def self.build_indices(toplevels, options)
+ files = []
+ classes = []
+
+ toplevels.each do |toplevel|
+ files << RDoc::Generator::File.new(toplevel, options,
+ RDoc::Generator::FILE_DIR)
+ end
+
+ RDoc::TopLevel.all_classes_and_modules.each do |cls|
+ build_class_list(classes, options, cls, files[0],
+ RDoc::Generator::CLASS_DIR)
+ end
+
+ return files, classes
+ end
+
+ def self.build_class_list(classes, options, from, html_file, class_dir)
+ classes << RDoc::Generator::Class.new(from, html_file, class_dir, options)
+
+ from.each_classmodule do |mod|
+ build_class_list(classes, options, mod, html_file, class_dir)
+ end
+ end
+
+ def initialize(context, options)
+ @context = context
+ @options = options
+
+ # HACK ugly
+ @template = options.template_class
+ end
+
+ def formatter
+ @formatter ||= @options.formatter ||
+ RDoc::Markup::ToHtmlCrossref.new(path, self, @options.show_hash)
+ end
+
+ ##
+ # convenience method to build a hyperlink
+
+ def href(link, cls, name)
+ %{<a href="#{link}" class="#{cls}">#{name}</a>} #"
+ end
+
+ ##
+ # Returns a reference to outselves to be used as an href= the form depends
+ # on whether we're all in one file or in multiple files
+
+ def as_href(from_path)
+ if @options.all_one_file
+ "#" + path
+ else
+ RDoc::Markup::ToHtml.gen_relative_url from_path, path
+ end
+ end
+
+ ##
+ # Create a list of Method objects for each method in the corresponding
+ # context object. If the @options.show_all variable is set (corresponding
+ # to the <tt>--all</tt> option, we include all methods, otherwise just the
+ # public ones.
+
+ def collect_methods
+ list = @context.method_list
+
+ unless @options.show_all then
+ list = list.select do |m|
+ m.visibility == :public or
+ m.visibility == :protected or
+ m.force_documentation
+ end
+ end
+
+ @methods = list.collect do |m|
+ RDoc::Generator::Method.new m, self, @options
+ end
+ end
+
+ ##
+ # Build a summary list of all the methods in this context
+
+ def build_method_summary_list(path_prefix = "")
+ collect_methods unless @methods
+
+ @methods.sort.map do |meth|
+ {
+ "name" => CGI.escapeHTML(meth.name),
+ "aref" => "##{meth.aref}"
+ }
+ end
+ end
+
+ ##
+ # Build a list of aliases for which we couldn't find a
+ # corresponding method
+
+ def build_alias_summary_list(section)
+ @context.aliases.map do |al|
+ next unless al.section == section
+
+ res = {
+ 'old_name' => al.old_name,
+ 'new_name' => al.new_name,
+ }
+
+ if al.comment and not al.comment.empty? then
+ res['desc'] = markup al.comment, true
+ end
+
+ res
+ end.compact
+ end
+
+ ##
+ # Build a list of constants
+
+ def build_constants_summary_list(section)
+ @context.constants.map do |co|
+ next unless co.section == section
+
+ res = {
+ 'name' => co.name,
+ 'value' => CGI.escapeHTML(co.value)
+ }
+
+ if co.comment and not co.comment.empty? then
+ res['desc'] = markup co.comment, true
+ end
+
+ res
+ end.compact
+ end
+
+ def build_requires_list(context)
+ potentially_referenced_list(context.requires) {|fn| [fn + ".rb"] }
+ end
+
+ def build_include_list(context)
+ potentially_referenced_list(context.includes)
+ end
+
+ ##
+ # Build a list from an array of Context items. Look up each in the
+ # AllReferences hash: if we find a corresponding entry, we generate a
+ # hyperlink to it, otherwise just output the name. However, some names
+ # potentially need massaging. For example, you may require a Ruby file
+ # without the .rb extension, but the file names we know about may have it.
+ # To deal with this, we pass in a block which performs the massaging,
+ # returning an array of alternative names to match
+
+ def potentially_referenced_list(array)
+ res = []
+ array.each do |i|
+ ref = AllReferences[i.name]
+# if !ref
+# container = @context.parent
+# while !ref && container
+# name = container.name + "::" + i.name
+# ref = AllReferences[name]
+# container = container.parent
+# end
+# end
+
+ ref = @context.find_symbol(i.name)
+ ref = ref.viewer if ref
+
+ if !ref && block_given?
+ possibles = yield(i.name)
+ while !ref and !possibles.empty?
+ ref = AllReferences[possibles.shift]
+ end
+ end
+ h_name = CGI.escapeHTML(i.name)
+ if ref and ref.document_self
+ path = url(ref.path)
+ res << { "name" => h_name, "aref" => path }
+ else
+ res << { "name" => h_name }
+ end
+ end
+ res
+ end
+
+ ##
+ # Build an array of arrays of method details. The outer array has up
+ # to six entries, public, private, and protected for both class
+ # methods, the other for instance methods. The inner arrays contain
+ # a hash for each method
+
+ def build_method_detail_list(section)
+ outer = []
+
+ methods = @methods.sort.select do |m|
+ m.document_self and m.section == section
+ end
+
+ for singleton in [true, false]
+ for vis in [ :public, :protected, :private ]
+ res = []
+ methods.each do |m|
+ next unless m.visibility == vis and m.singleton == singleton
+
+ row = {}
+
+ if m.call_seq then
+ row["callseq"] = m.call_seq.gsub(/->/, '&rarr;')
+ else
+ row["name"] = CGI.escapeHTML(m.name)
+ row["params"] = m.params
+ end
+
+ desc = m.description.strip
+ row["m_desc"] = desc unless desc.empty?
+ row["aref"] = m.aref
+ row["visibility"] = m.visibility.to_s
+
+ alias_names = []
+
+ m.aliases.each do |other|
+ if other.viewer then # won't be if the alias is private
+ alias_names << {
+ 'name' => other.name,
+ 'aref' => other.viewer.as_href(path)
+ }
+ end
+ end
+
+ row["aka"] = alias_names unless alias_names.empty?
+
+ if @options.inline_source then
+ code = m.source_code
+ row["sourcecode"] = code if code
+ else
+ code = m.src_url
+ if code then
+ row["codeurl"] = code
+ row["imgurl"] = m.img_url
+ end
+ end
+
+ res << row
+ end
+
+ if res.size > 0 then
+ outer << {
+ "type" => vis.to_s.capitalize,
+ "category" => singleton ? "Class" : "Instance",
+ "methods" => res
+ }
+ end
+ end
+ end
+
+ outer
+ end
+
+ ##
+ # Build the structured list of classes and modules contained
+ # in this context.
+
+ def build_class_list(level, from, section, infile=nil)
+ prefix = '&nbsp;&nbsp;::' * level;
+ res = ''
+
+ from.modules.sort.each do |mod|
+ next unless mod.section == section
+ next if infile && !mod.defined_in?(infile)
+ if mod.document_self
+ res <<
+ prefix <<
+ 'Module ' <<
+ href(url(mod.viewer.path), 'link', mod.full_name) <<
+ "<br />\n" <<
+ build_class_list(level + 1, mod, section, infile)
+ end
+ end
+
+ from.classes.sort.each do |cls|
+ next unless cls.section == section
+ next if infile and not cls.defined_in?(infile)
+
+ if cls.document_self
+ res <<
+ prefix <<
+ 'Class ' <<
+ href(url(cls.viewer.path), 'link', cls.full_name) <<
+ "<br />\n" <<
+ build_class_list(level + 1, cls, section, infile)
+ end
+ end
+
+ res
+ end
+
+ def url(target)
+ RDoc::Markup::ToHtml.gen_relative_url path, target
+ end
+
+ def aref_to(target)
+ if @options.all_one_file
+ "#" + target
+ else
+ url(target)
+ end
+ end
+
+ def document_self
+ @context.document_self
+ end
+
+ def diagram_reference(diagram)
+ res = diagram.gsub(/((?:src|href)=")(.*?)"/) {
+ $1 + url($2) + '"'
+ }
+ res
+ end
+
+ ##
+ # Find a symbol in ourselves or our parent
+
+ def find_symbol(symbol, method=nil)
+ res = @context.find_symbol(symbol, method)
+ if res
+ res = res.viewer
+ end
+ res
+ end
+
+ ##
+ # create table of contents if we contain sections
+
+ def add_table_of_sections
+ toc = []
+ @context.sections.each do |section|
+ if section.title then
+ toc << {
+ 'secname' => section.title,
+ 'href' => section.sequence
+ }
+ end
+ end
+
+ @values['toc'] = toc unless toc.empty?
+ end
+
+ end
+
+ ##
+ # Wrap a ClassModule context
+
+ class Class < Context
+
+ attr_reader :methods
+ attr_reader :path
+ attr_reader :values
+
+ def initialize(context, html_file, prefix, options)
+ super context, options
+
+ @html_file = html_file
+ @html_class = self
+ @is_module = context.module?
+ @values = {}
+
+ context.viewer = self
+
+ if options.all_one_file
+ @path = context.full_name
+ else
+ @path = http_url(context.full_name, prefix)
+ end
+
+ collect_methods
+
+ AllReferences.add(name, self)
+ end
+
+ ##
+ # Returns the relative file name to store this class in, which is also its
+ # url
+
+ def http_url(full_name, prefix)
+ path = full_name.dup
+
+ path.gsub!(/<<\s*(\w*)/, 'from-\1') if path['<<']
+
+ ::File.join(prefix, path.split("::")) + ".html"
+ end
+
+ def name
+ @context.full_name
+ end
+
+ def parent_name
+ @context.parent.full_name
+ end
+
+ def index_name
+ name
+ end
+
+ def write_on(f, file_list, class_list, method_list, overrides = {})
+ value_hash
+
+ @values['file_list'] = file_list
+ @values['class_list'] = class_list
+ @values['method_list'] = method_list
+
+ @values.update overrides
+
+ template = RDoc::TemplatePage.new(@template::BODY,
+ @template::CLASS_PAGE,
+ @template::METHOD_LIST)
+
+ template.write_html_on(f, @values)
+ end
+
+ def value_hash
+ class_attribute_values
+ add_table_of_sections
+
+ @values["charset"] = @options.charset
+ @values["style_url"] = style_url(path, @options.css)
+
+ d = markup(@context.comment)
+ @values["description"] = d unless d.empty?
+
+ ml = build_method_summary_list @path
+ @values["methods"] = ml unless ml.empty?
+
+ il = build_include_list @context
+ @values["includes"] = il unless il.empty?
+
+ @values["sections"] = @context.sections.map do |section|
+ secdata = {
+ "sectitle" => section.title,
+ "secsequence" => section.sequence,
+ "seccomment" => markup(section.comment),
+ }
+
+ al = build_alias_summary_list section
+ secdata["aliases"] = al unless al.empty?
+
+ co = build_constants_summary_list section
+ secdata["constants"] = co unless co.empty?
+
+ al = build_attribute_list section
+ secdata["attributes"] = al unless al.empty?
+
+ cl = build_class_list 0, @context, section
+ secdata["classlist"] = cl unless cl.empty?
+
+ mdl = build_method_detail_list section
+ secdata["method_list"] = mdl unless mdl.empty?
+
+ secdata
+ end
+
+ @values
+ end
+
+ def build_attribute_list(section)
+ @context.attributes.sort.map do |att|
+ next unless att.section == section
+
+ if att.visibility == :public or att.visibility == :protected or
+ @options.show_all then
+
+ entry = {
+ "name" => CGI.escapeHTML(att.name),
+ "rw" => att.rw,
+ "a_desc" => markup(att.comment, true)
+ }
+
+ unless att.visibility == :public or att.visibility == :protected then
+ entry["rw"] << "-"
+ end
+
+ entry
+ end
+ end.compact
+ end
+
+ def class_attribute_values
+ h_name = CGI.escapeHTML(name)
+
+ @values["href"] = @path
+ @values["classmod"] = @is_module ? "Module" : "Class"
+ @values["title"] = "#{@values['classmod']}: #{h_name} [#{@options.title}]"
+
+ c = @context
+ c = c.parent while c and not c.diagram
+
+ if c and c.diagram then
+ @values["diagram"] = diagram_reference(c.diagram)
+ end
+
+ @values["full_name"] = h_name
+
+ if not @context.module? and @context.superclass then
+ parent_class = @context.superclass
+ @values["parent"] = CGI.escapeHTML(parent_class)
+
+ if parent_name
+ lookup = parent_name + "::" + parent_class
+ else
+ lookup = parent_class
+ end
+
+ parent_url = AllReferences[lookup] || AllReferences[parent_class]
+
+ if parent_url and parent_url.document_self
+ @values["par_url"] = aref_to(parent_url.path)
+ end
+ end
+
+ files = []
+ @context.in_files.each do |f|
+ res = {}
+ full_path = CGI.escapeHTML(f.file_absolute_name)
+
+ res["full_path"] = full_path
+ res["full_path_url"] = aref_to(f.viewer.path) if f.document_self
+
+ if @options.webcvs
+ res["cvsurl"] = cvs_url( @options.webcvs, full_path )
+ end
+
+ files << res
+ end
+
+ @values['infiles'] = files
+ end
+
+ def <=>(other)
+ self.name <=> other.name
+ end
+
+ end
+
+ ##
+ # Handles the mapping of a file's information to HTML. In reality, a file
+ # corresponds to a +TopLevel+ object, containing modules, classes, and
+ # top-level methods. In theory it _could_ contain attributes and aliases,
+ # but we ignore these for now.
+
+ class File < Context
+
+ attr_reader :path
+ attr_reader :name
+ attr_reader :values
+
+ def initialize(context, options, file_dir)
+ super context, options
+
+ @values = {}
+
+ if options.all_one_file
+ @path = filename_to_label
+ else
+ @path = http_url(file_dir)
+ end
+
+ @name = @context.file_relative_name
+
+ collect_methods
+ AllReferences.add(name, self)
+ context.viewer = self
+ end
+
+ def http_url(file_dir)
+ ::File.join file_dir, "#{@context.file_relative_name.tr '.', '_'}.html"
+ end
+
+ def filename_to_label
+ @context.file_relative_name.gsub(/%|\/|\?|\#/) do
+ ('%%%x' % $&[0]).unpack('C')
+ end
+ end
+
+ def index_name
+ name
+ end
+
+ def parent_name
+ nil
+ end
+
+ def value_hash
+ file_attribute_values
+ add_table_of_sections
+
+ @values["charset"] = @options.charset
+ @values["href"] = path
+ @values["style_url"] = style_url(path, @options.css)
+
+ if @context.comment
+ d = markup(@context.comment)
+ @values["description"] = d if d.size > 0
+ end
+
+ ml = build_method_summary_list
+ @values["methods"] = ml unless ml.empty?
+
+ il = build_include_list(@context)
+ @values["includes"] = il unless il.empty?
+
+ rl = build_requires_list(@context)
+ @values["requires"] = rl unless rl.empty?
+
+ if @options.promiscuous
+ file_context = nil
+ else
+ file_context = @context
+ end
+
+
+ @values["sections"] = @context.sections.map do |section|
+
+ secdata = {
+ "sectitle" => section.title,
+ "secsequence" => section.sequence,
+ "seccomment" => markup(section.comment)
+ }
+
+ cl = build_class_list(0, @context, section, file_context)
+ secdata["classlist"] = cl unless cl.empty?
+
+ mdl = build_method_detail_list(section)
+ secdata["method_list"] = mdl unless mdl.empty?
+
+ al = build_alias_summary_list(section)
+ secdata["aliases"] = al unless al.empty?
+
+ co = build_constants_summary_list(section)
+ secdata["constants"] = co unless co.empty?
+
+ secdata
+ end
+
+ @values
+ end
+
+ def write_on(f, file_list, class_list, method_list, overrides = {})
+ value_hash
+
+ @values['file_list'] = file_list
+ @values['class_list'] = class_list
+ @values['method_list'] = method_list
+
+ @values.update overrides
+
+ template = RDoc::TemplatePage.new(@template::BODY,
+ @template::FILE_PAGE,
+ @template::METHOD_LIST)
+
+ template.write_html_on(f, @values)
+ end
+
+ def file_attribute_values
+ full_path = @context.file_absolute_name
+ short_name = ::File.basename full_path
+
+ @values["title"] = CGI.escapeHTML("File: #{short_name} [#{@options.title}]")
+
+ if @context.diagram then
+ @values["diagram"] = diagram_reference(@context.diagram)
+ end
+
+ @values["short_name"] = CGI.escapeHTML(short_name)
+ @values["full_path"] = CGI.escapeHTML(full_path)
+ @values["dtm_modified"] = @context.file_stat.mtime.to_s
+
+ if @options.webcvs then
+ @values["cvsurl"] = cvs_url @options.webcvs, @values["full_path"]
+ end
+ end
+
+ def <=>(other)
+ self.name <=> other.name
+ end
+
+ end
+
+ class Method
+
+ include MarkUp
+
+ attr_reader :context
+ attr_reader :src_url
+ attr_reader :img_url
+ attr_reader :source_code
+
+ def self.all_methods
+ @@all_methods
+ end
+
+ def self.reset
+ @@all_methods = []
+ @@seq = "M000000"
+ end
+
+ # Initialize the class variables.
+ self.reset
+
+ def initialize(context, html_class, options)
+ # TODO: rethink the class hierarchy here...
+ @context = context
+ @html_class = html_class
+ @options = options
+
+ @@seq = @@seq.succ
+ @seq = @@seq
+
+ # HACK ugly
+ @template = options.template_class
+
+ @@all_methods << self
+
+ context.viewer = self
+
+ if (ts = @context.token_stream)
+ @source_code = markup_code(ts)
+ unless @options.inline_source
+ @src_url = create_source_code_file(@source_code)
+ @img_url = RDoc::Markup::ToHtml.gen_relative_url path, 'source.png'
+ end
+ end
+
+ AllReferences.add(name, self)
+ end
+
+ ##
+ # Returns a reference to outselves to be used as an href= the form depends
+ # on whether we're all in one file or in multiple files
+
+ def as_href(from_path)
+ if @options.all_one_file
+ "#" + path
+ else
+ RDoc::Markup::ToHtml.gen_relative_url from_path, path
+ end
+ end
+
+ def formatter
+ @formatter ||= @options.formatter ||
+ RDoc::Markup::ToHtmlCrossref.new(path, self, @options.show_hash)
+ end
+
+ def inspect
+ alias_for = if @context.is_alias_for then
+ " (alias_for #{@context.is_alias_for})"
+ else
+ nil
+ end
+
+ "#<%s:0x%x %s%s%s (%s)%s>" % [
+ self.class, object_id,
+ @context.parent.name,
+ @context.singleton ? '::' : '#',
+ name,
+ @context.visibility,
+ alias_for
+ ]
+ end
+
+ def name
+ @context.name
+ end
+
+ def section
+ @context.section
+ end
+
+ def index_name
+ "#{@context.name} (#{@html_class.name})"
+ end
+
+ def parent_name
+ if @context.parent.parent
+ @context.parent.parent.full_name
+ else
+ nil
+ end
+ end
+
+ def aref
+ @seq
+ end
+
+ def path
+ if @options.all_one_file
+ aref
+ else
+ @html_class.path + "#" + aref
+ end
+ end
+
+ def description
+ markup(@context.comment)
+ end
+
+ def visibility
+ @context.visibility
+ end
+
+ def singleton
+ @context.singleton
+ end
+
+ def call_seq
+ cs = @context.call_seq
+ if cs
+ cs.gsub(/\n/, "<br />\n")
+ else
+ nil
+ end
+ end
+
+ def params
+ # params coming from a call-seq in 'C' will start with the
+ # method name
+ params = @context.params
+ if params !~ /^\w/
+ params = @context.params.gsub(/\s*\#.*/, '')
+ params = params.tr("\n", " ").squeeze(" ")
+ params = "(" + params + ")" unless params[0] == ?(
+
+ if (block = @context.block_params)
+ # If this method has explicit block parameters, remove any
+ # explicit &block
+
+ params.sub!(/,?\s*&\w+/, '')
+
+ block.gsub!(/\s*\#.*/, '')
+ block = block.tr("\n", " ").squeeze(" ")
+ if block[0] == ?(
+ block.sub!(/^\(/, '').sub!(/\)/, '')
+ end
+ params << " {|#{block.strip}| ...}"
+ end
+ end
+ CGI.escapeHTML(params)
+ end
+
+ def create_source_code_file(code_body)
+ meth_path = @html_class.path.sub(/\.html$/, '.src')
+ FileUtils.mkdir_p(meth_path)
+ file_path = ::File.join meth_path, "#{@seq}.html"
+
+ template = RDoc::TemplatePage.new(@template::SRC_PAGE)
+
+ open file_path, 'w' do |f|
+ values = {
+ 'title' => CGI.escapeHTML(index_name),
+ 'code' => code_body,
+ 'style_url' => style_url(file_path, @options.css),
+ 'charset' => @options.charset
+ }
+ template.write_html_on(f, values)
+ end
+
+ RDoc::Markup::ToHtml.gen_relative_url path, file_path
+ end
+
+ def <=>(other)
+ @context <=> other.context
+ end
+
+ ##
+ # Given a sequence of source tokens, mark up the source code
+ # to make it look purty.
+
+ def markup_code(tokens)
+ src = ""
+ tokens.each do |t|
+ next unless t
+# style = STYLE_MAP[t.class]
+ style = case t
+ when RDoc::RubyToken::TkCONSTANT then "ruby-constant"
+ when RDoc::RubyToken::TkKW then "ruby-keyword kw"
+ when RDoc::RubyToken::TkIVAR then "ruby-ivar"
+ when RDoc::RubyToken::TkOp then "ruby-operator"
+ when RDoc::RubyToken::TkId then "ruby-identifier"
+ when RDoc::RubyToken::TkNode then "ruby-node"
+ when RDoc::RubyToken::TkCOMMENT then "ruby-comment cmt"
+ when RDoc::RubyToken::TkREGEXP then "ruby-regexp re"
+ when RDoc::RubyToken::TkSTRING then "ruby-value str"
+ when RDoc::RubyToken::TkVal then "ruby-value"
+ else
+ nil
+ end
+
+ text = CGI.escapeHTML(t.text)
+
+ if style
+ src << "<span class=\"#{style}\">#{text}</span>"
+ else
+ src << text
+ end
+ end
+
+ add_line_numbers(src) if @options.include_line_numbers
+ src
+ end
+
+ ##
+ # We rely on the fact that the first line of a source code listing has
+ # # File xxxxx, line dddd
+
+ def add_line_numbers(src)
+ if src =~ /\A.*, line (\d+)/
+ first = $1.to_i - 1
+ last = first + src.count("\n")
+ size = last.to_s.length
+ fmt = "%#{size}d: "
+ is_first_line = true
+ line_num = first
+ src.gsub!(/^/) do
+ if is_first_line then
+ is_first_line = false
+ res = " " * (size+2)
+ else
+ res = sprintf(fmt, line_num)
+ end
+
+ line_num += 1
+ res
+ end
+ end
+ end
+
+ def document_self
+ @context.document_self
+ end
+
+ def aliases
+ @context.aliases
+ end
+
+ def find_symbol(symbol, method=nil)
+ res = @context.parent.find_symbol(symbol, method)
+ if res
+ res = res.viewer
+ end
+ res
+ end
+
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/generator/chm.rb b/ruby/lib/rdoc/generator/chm.rb
new file mode 100644
index 0000000..7537365
--- /dev/null
+++ b/ruby/lib/rdoc/generator/chm.rb
@@ -0,0 +1,113 @@
+require 'rdoc/generator/html'
+
+class RDoc::Generator::CHM < RDoc::Generator::HTML
+
+ HHC_PATH = "c:/Program Files/HTML Help Workshop/hhc.exe"
+
+ ##
+ # Standard generator factory
+
+ def self.for(options)
+ new(options)
+ end
+
+ def initialize(*args)
+ super
+ @op_name = @options.op_name || "rdoc"
+ check_for_html_help_workshop
+ end
+
+ def check_for_html_help_workshop
+ stat = File.stat(HHC_PATH)
+ rescue
+ $stderr <<
+ "\n.chm output generation requires that Microsoft's Html Help\n" <<
+ "Workshop is installed. RDoc looks for it in:\n\n " <<
+ HHC_PATH <<
+ "\n\nYou can download a copy for free from:\n\n" <<
+ " http://msdn.microsoft.com/library/default.asp?" <<
+ "url=/library/en-us/htmlhelp/html/hwMicrosoftHTMLHelpDownloads.asp\n\n"
+ end
+
+ ##
+ # Generate the html as normal, then wrap it in a help project
+
+ def generate(info)
+ super
+ @project_name = @op_name + ".hhp"
+ create_help_project
+ end
+
+ ##
+ # The project contains the project file, a table of contents and an index
+
+ def create_help_project
+ create_project_file
+ create_contents_and_index
+ compile_project
+ end
+
+ ##
+ # The project file links together all the various
+ # files that go to make up the help.
+
+ def create_project_file
+ template = RDoc::TemplatePage.new @template::HPP_FILE
+ values = { "title" => @options.title, "opname" => @op_name }
+ files = []
+ @files.each do |f|
+ files << { "html_file_name" => f.path }
+ end
+
+ values['all_html_files'] = files
+
+ File.open(@project_name, "w") do |f|
+ template.write_html_on(f, values)
+ end
+ end
+
+ ##
+ # The contents is a list of all files and modules.
+ # For each we include as sub-entries the list
+ # of methods they contain. As we build the contents
+ # we also build an index file
+
+ def create_contents_and_index
+ contents = []
+ index = []
+
+ (@files+@classes).sort.each do |entry|
+ content_entry = { "c_name" => entry.name, "ref" => entry.path }
+ index << { "name" => entry.name, "aref" => entry.path }
+
+ internals = []
+
+ methods = entry.build_method_summary_list(entry.path)
+
+ content_entry["methods"] = methods unless methods.empty?
+ contents << content_entry
+ index.concat methods
+ end
+
+ values = { "contents" => contents }
+ template = RDoc::TemplatePage.new @template::CONTENTS
+ File.open("contents.hhc", "w") do |f|
+ template.write_html_on(f, values)
+ end
+
+ values = { "index" => index }
+ template = RDoc::TemplatePage.new @template::CHM_INDEX
+ File.open("index.hhk", "w") do |f|
+ template.write_html_on(f, values)
+ end
+ end
+
+ ##
+ # Invoke the windows help compiler to compiler the project
+
+ def compile_project
+ system(HHC_PATH, @project_name)
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/generator/chm/chm.rb b/ruby/lib/rdoc/generator/chm/chm.rb
new file mode 100644
index 0000000..cceeca5
--- /dev/null
+++ b/ruby/lib/rdoc/generator/chm/chm.rb
@@ -0,0 +1,100 @@
+require 'rdoc/generator/chm'
+require 'rdoc/generator/html/html'
+
+module RDoc::Generator::CHM::CHM
+
+ HTML = RDoc::Generator::HTML::HTML
+
+ INDEX = HTML::INDEX
+
+ STYLE = HTML::STYLE
+
+ CLASS_INDEX = HTML::CLASS_INDEX
+ CLASS_PAGE = HTML::CLASS_PAGE
+ FILE_INDEX = HTML::FILE_INDEX
+ FILE_PAGE = HTML::FILE_PAGE
+ METHOD_INDEX = HTML::METHOD_INDEX
+ METHOD_LIST = HTML::METHOD_LIST
+
+ FR_INDEX_BODY = HTML::FR_INDEX_BODY
+
+ # This is a nasty little hack, but hhc doesn't support the <?xml tag, so...
+ BODY = HTML::BODY.sub!(/<\?xml.*\?>/, '')
+ SRC_PAGE = HTML::SRC_PAGE.sub!(/<\?xml.*\?>/, '')
+
+ HPP_FILE = <<-EOF
+[OPTIONS]
+Auto Index = Yes
+Compatibility=1.1 or later
+Compiled file=<%= values["opname"] %>.chm
+Contents file=contents.hhc
+Full-text search=Yes
+Index file=index.hhk
+Language=0x409 English(United States)
+Title=<%= values["title"] %>
+
+[FILES]
+<% values["all_html_files"].each do |all_html_files| %>
+<%= all_html_files["html_file_name"] %>
+<% end # values["all_html_files"] %>
+ EOF
+
+ CONTENTS = <<-EOF
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<HTML>
+<HEAD>
+<meta name="GENERATOR" content="Microsoft&reg; HTML Help Workshop 4.1">
+<!-- Sitemap 1.0 -->
+</HEAD><BODY>
+<OBJECT type="text/site properties">
+ <param name="Foreground" value="0x80">
+ <param name="Window Styles" value="0x800025">
+ <param name="ImageType" value="Folder">
+</OBJECT>
+<UL>
+<% values["contents"].each do |contents| %>
+ <LI> <OBJECT type="text/sitemap">
+ <param name="Name" value="<%= contents["c_name"] %>">
+ <param name="Local" value="<%= contents["ref"] %>">
+ </OBJECT>
+<% if contents["methods"] then %>
+<ul>
+<% contents["methods"].each do |methods| %>
+ <LI> <OBJECT type="text/sitemap">
+ <param name="Name" value="<%= methods["name"] %>">
+ <param name="Local" value="<%= methods["aref"] %>">
+ </OBJECT>
+<% end # contents["methods"] %>
+</ul>
+<% end %>
+ </LI>
+<% end # values["contents"] %>
+</UL>
+</BODY></HTML>
+ EOF
+
+ CHM_INDEX = <<-EOF
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<HTML>
+<HEAD>
+<meta name="GENERATOR" content="Microsoft&reg; HTML Help Workshop 4.1">
+<!-- Sitemap 1.0 -->
+</HEAD><BODY>
+<OBJECT type="text/site properties">
+ <param name="Foreground" value="0x80">
+ <param name="Window Styles" value="0x800025">
+ <param name="ImageType" value="Folder">
+</OBJECT>
+<UL>
+<% values["index"].each do |index| %>
+ <LI> <OBJECT type="text/sitemap">
+ <param name="Name" value="<%= index["name"] %>">
+ <param name="Local" value="<%= index["aref"] %>">
+ </OBJECT>
+<% end # values["index"] %>
+</UL>
+</BODY></HTML>
+ EOF
+
+end
+
diff --git a/ruby/lib/rdoc/generator/html.rb b/ruby/lib/rdoc/generator/html.rb
new file mode 100644
index 0000000..d136de7
--- /dev/null
+++ b/ruby/lib/rdoc/generator/html.rb
@@ -0,0 +1,445 @@
+require 'fileutils'
+
+require 'rdoc/generator'
+require 'rdoc/markup/to_html'
+
+##
+# We're responsible for generating all the HTML files from the object tree
+# defined in code_objects.rb. We generate:
+#
+# [files] an html file for each input file given. These
+# input files appear as objects of class
+# TopLevel
+#
+# [classes] an html file for each class or module encountered.
+# These classes are not grouped by file: if a file
+# contains four classes, we'll generate an html
+# file for the file itself, and four html files
+# for the individual classes.
+#
+# [indices] we generate three indices for files, classes,
+# and methods. These are displayed in a browser
+# like window with three index panes across the
+# top and the selected description below
+#
+# Method descriptions appear in whatever entity (file, class, or module) that
+# contains them.
+#
+# We generate files in a structure below a specified subdirectory, normally
+# +doc+.
+#
+# opdir
+# |
+# |___ files
+# | |__ per file summaries
+# |
+# |___ classes
+# |__ per class/module descriptions
+#
+# HTML is generated using the Template class.
+
+class RDoc::Generator::HTML
+
+ include RDoc::Generator::MarkUp
+
+ ##
+ # Generator may need to return specific subclasses depending on the
+ # options they are passed. Because of this we create them using a factory
+
+ def self.for(options)
+ RDoc::Generator::AllReferences.reset
+ RDoc::Generator::Method.reset
+
+ if options.all_one_file
+ RDoc::Generator::HTMLInOne.new options
+ else
+ new options
+ end
+ end
+
+ class << self
+ protected :new
+ end
+
+ ##
+ # Set up a new HTML generator. Basically all we do here is load up the
+ # correct output temlate
+
+ def initialize(options) #:not-new:
+ @options = options
+ load_html_template
+ end
+
+ ##
+ # Build the initial indices and output objects
+ # based on an array of TopLevel objects containing
+ # the extracted information.
+
+ def generate(toplevels)
+ @toplevels = toplevels
+ @files = []
+ @classes = []
+
+ write_style_sheet
+ gen_sub_directories
+ build_indices
+ generate_html
+ end
+
+ private
+
+ ##
+ # Load up the HTML template specified in the options.
+ # If the template name contains a slash, use it literally
+
+ def load_html_template
+ #
+ # If the template is not a path, first look for it
+ # in rdoc's HTML template directory. Perhaps this behavior should
+ # be reversed (first try to include the template and, only if that
+ # fails, try to include it in the default template directory).
+ # One danger with reversing the behavior, however, is that
+ # if something like require 'html' could load up an
+ # unrelated file in the standard library or in a gem.
+ #
+ template = @options.template
+
+ unless template =~ %r{/|\\} then
+ template = File.join('rdoc', 'generator', @options.generator.key,
+ template)
+ end
+
+ begin
+ require template
+
+ @template = self.class.const_get @options.template.upcase
+ @options.template_class = @template
+ rescue LoadError => e
+ #
+ # The template did not exist in the default template directory, so
+ # see if require can find the template elsewhere (in a gem, for
+ # instance).
+ #
+ if(e.message[template] && template != @options.template)
+ template = @options.template
+ retry
+ end
+
+ $stderr.puts "Could not find HTML template '#{template}': #{e.message}"
+ exit 99
+ end
+ end
+
+ ##
+ # Write out the style sheet used by the main frames
+
+ def write_style_sheet
+ return unless @template.constants.include? :STYLE or
+ @template.constants.include? 'STYLE'
+
+ template = RDoc::TemplatePage.new @template::STYLE
+
+ unless @options.css then
+ open RDoc::Generator::CSS_NAME, 'w' do |f|
+ values = {}
+
+ if @template.constants.include? :FONTS or
+ @template.constants.include? 'FONTS' then
+ values["fonts"] = @template::FONTS
+ end
+
+ template.write_html_on(f, values)
+ end
+ end
+ end
+
+ ##
+ # See the comments at the top for a description of the directory structure
+
+ def gen_sub_directories
+ FileUtils.mkdir_p RDoc::Generator::FILE_DIR
+ FileUtils.mkdir_p RDoc::Generator::CLASS_DIR
+ rescue
+ $stderr.puts $!.message
+ exit 1
+ end
+
+ def build_indices
+ @files, @classes = RDoc::Generator::Context.build_indices(@toplevels,
+ @options)
+ end
+
+ ##
+ # Generate all the HTML
+
+ def generate_html
+ @main_url = main_url
+
+ # the individual descriptions for files and classes
+ gen_into(@files)
+ gen_into(@classes)
+
+ # and the index files
+ gen_file_index
+ gen_class_index
+ gen_method_index
+ gen_main_index
+
+ # this method is defined in the template file
+ values = {
+ 'title_suffix' => CGI.escapeHTML("[#{@options.title}]"),
+ 'charset' => @options.charset,
+ 'style_url' => style_url('', @options.css),
+ }
+
+ @template.write_extra_pages(values) if @template.respond_to?(:write_extra_pages)
+ end
+
+ def gen_into(list)
+ #
+ # The file, class, and method lists technically should be regenerated
+ # for every output file, in order that the relative links be correct
+ # (we are worried here about frameless templates, which need this
+ # information for every generated page). Doing this is a bit slow,
+ # however. For a medium-sized gem, this increased rdoc's runtime by
+ # about 5% (using the 'time' command-line utility). While this is not
+ # necessarily a problem, I do not want to pessimize rdoc for large
+ # projects, however, and so we only regenerate the lists when the
+ # directory of the output file changes, which seems like a reasonable
+ # optimization.
+ #
+ file_list = {}
+ class_list = {}
+ method_list = {}
+ prev_op_dir = nil
+
+ list.each do |item|
+ next unless item.document_self
+
+ op_file = item.path
+ op_dir = File.dirname(op_file)
+
+ if(op_dir != prev_op_dir)
+ file_list = index_to_links op_file, @files
+ class_list = index_to_links op_file, @classes
+ method_list = index_to_links op_file, RDoc::Generator::Method.all_methods
+ end
+ prev_op_dir = op_dir
+
+ FileUtils.mkdir_p op_dir
+
+ open op_file, 'w' do |io|
+ item.write_on io, file_list, class_list, method_list
+ end
+ end
+ end
+
+ def gen_file_index
+ gen_an_index @files, 'Files', @template::FILE_INDEX, "fr_file_index.html"
+ end
+
+ def gen_class_index
+ gen_an_index(@classes, 'Classes', @template::CLASS_INDEX,
+ "fr_class_index.html")
+ end
+
+ def gen_method_index
+ gen_an_index(RDoc::Generator::Method.all_methods, 'Methods',
+ @template::METHOD_INDEX, "fr_method_index.html")
+ end
+
+ def gen_an_index(collection, title, template, filename)
+ template = RDoc::TemplatePage.new @template::FR_INDEX_BODY, template
+ res = []
+ collection.sort.each do |f|
+ if f.document_self
+ res << { "href" => f.path, "name" => f.index_name }
+ end
+ end
+
+ values = {
+ "entries" => res,
+ 'title' => CGI.escapeHTML("#{title} [#{@options.title}]"),
+ 'list_title' => CGI.escapeHTML(title),
+ 'index_url' => @main_url,
+ 'charset' => @options.charset,
+ 'style_url' => style_url('', @options.css),
+ }
+
+ open filename, 'w' do |f|
+ template.write_html_on(f, values)
+ end
+ end
+
+ ##
+ # The main index page is mostly a template frameset, but includes the
+ # initial page. If the <tt>--main</tt> option was given, we use this as
+ # our main page, otherwise we use the first file specified on the command
+ # line.
+
+ def gen_main_index
+ if @template.const_defined? :FRAMELESS then
+ #
+ # If we're using a template without frames, then just redirect
+ # to it from index.html.
+ #
+ # One alternative to this, expanding the main page's template into
+ # index.html, is tricky because the relative URLs will be different
+ # (since index.html is located in at the site's root,
+ # rather than within a files or a classes subdirectory).
+ #
+ open 'index.html', 'w' do |f|
+ f.puts(%{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">})
+ f.puts(%{<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
+ lang="en">})
+ f.puts(%{<head>})
+ f.puts(%{<title>#{CGI.escapeHTML(@options.title)}</title>})
+ f.puts(%{<meta http-equiv="refresh" content="0; url=#{@main_url}" />})
+ f.puts(%{</head>})
+ f.puts(%{<body></body>})
+ f.puts(%{</html>})
+ end
+ else
+ main = RDoc::TemplatePage.new @template::INDEX
+
+ open 'index.html', 'w' do |f|
+ style_url = style_url '', @options.css
+
+ classes = @classes.sort.map { |klass| klass.value_hash }
+
+ values = {
+ 'initial_page' => @main_url,
+ 'style_url' => style_url('', @options.css),
+ 'title' => CGI.escapeHTML(@options.title),
+ 'charset' => @options.charset,
+ 'classes' => classes,
+ }
+
+ values['inline_source'] = @options.inline_source
+
+ main.write_html_on f, values
+ end
+ end
+ end
+
+ def index_to_links(output_path, collection)
+ collection.sort.map do |f|
+ next unless f.document_self
+ { "href" => RDoc::Markup::ToHtml.gen_relative_url(output_path, f.path),
+ "name" => f.index_name }
+ end.compact
+ end
+
+ ##
+ # Returns the url of the main page
+
+ def main_url
+ main_page = @options.main_page
+
+ #
+ # If a main page has been specified (--main), then search for it
+ # in the AllReferences array. This allows either files or classes
+ # to be used for the main page.
+ #
+ if main_page then
+ main_page_ref = RDoc::Generator::AllReferences[main_page]
+
+ if main_page_ref then
+ return main_page_ref.path
+ else
+ $stderr.puts "Could not find main page #{main_page}"
+ end
+ end
+
+ #
+ # No main page has been specified, so just use the README.
+ #
+ @files.each do |file|
+ if file.name =~ /^README/ then
+ return file.path
+ end
+ end
+
+ #
+ # There's no README (shame! shame!). Just use the first file
+ # that will be documented.
+ #
+ @files.each do |file|
+ if file.document_self then
+ return file.path
+ end
+ end
+
+ #
+ # There are no files to be documented... Something seems very wrong.
+ #
+ raise RDoc::Error, "Couldn't find anything to document (perhaps :stopdoc: has been used in all classes)!"
+ end
+ private :main_url
+
+end
+
+class RDoc::Generator::HTMLInOne < RDoc::Generator::HTML
+
+ def initialize(*args)
+ super
+ end
+
+ ##
+ # Build the initial indices and output objects
+ # based on an array of TopLevel objects containing
+ # the extracted information.
+
+ def generate(info)
+ @toplevels = info
+ @hyperlinks = {}
+
+ build_indices
+ generate_xml
+ end
+
+ ##
+ # Generate:
+ #
+ # * a list of RDoc::Generator::File objects for each TopLevel object.
+ # * a list of RDoc::Generator::Class objects for each first level
+ # class or module in the TopLevel objects
+ # * a complete list of all hyperlinkable terms (file,
+ # class, module, and method names)
+
+ def build_indices
+ @files, @classes = RDoc::Generator::Context.build_indices(@toplevels,
+ @options)
+ end
+
+ ##
+ # Generate all the HTML. For the one-file case, we generate
+ # all the information in to one big hash
+
+ def generate_xml
+ values = {
+ 'charset' => @options.charset,
+ 'files' => gen_into(@files),
+ 'classes' => gen_into(@classes),
+ 'title' => CGI.escapeHTML(@options.title),
+ }
+
+ template = RDoc::TemplatePage.new @template::ONE_PAGE
+
+ if @options.op_name
+ opfile = open @options.op_name, 'w'
+ else
+ opfile = $stdout
+ end
+ template.write_html_on(opfile, values)
+ end
+
+ def gen_into(list)
+ res = []
+ list.each do |item|
+ res << item.value_hash
+ end
+ res
+ end
+end
diff --git a/ruby/lib/rdoc/generator/html/common.rb b/ruby/lib/rdoc/generator/html/common.rb
new file mode 100644
index 0000000..b25f009
--- /dev/null
+++ b/ruby/lib/rdoc/generator/html/common.rb
@@ -0,0 +1,24 @@
+#
+# The templates require further refactoring. In particular,
+# * Some kind of HTML generation library should be used.
+#
+# Also, all of the templates require some TLC from a designer.
+#
+# Right now, this file contains some constants that are used by all
+# of the templates.
+#
+module RDoc::Generator::HTML::Common
+ XHTML_STRICT_PREAMBLE = <<-EOF
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+EOF
+
+ XHTML_FRAME_PREAMBLE = <<-EOF
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
+EOF
+
+ HTML_ELEMENT = <<-EOF
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+EOF
+end
diff --git a/ruby/lib/rdoc/generator/html/frameless.rb b/ruby/lib/rdoc/generator/html/frameless.rb
new file mode 100644
index 0000000..0375fee
--- /dev/null
+++ b/ruby/lib/rdoc/generator/html/frameless.rb
@@ -0,0 +1,92 @@
+require 'rdoc/generator/html/html'
+
+##
+# = CSS2 RDoc HTML template
+#
+# This is a template for RDoc that uses XHTML 1.0 Strict and dictates a
+# bit more of the appearance of the output to cascading stylesheets than the
+# default. It was designed for clean inline code display, and uses DHTMl to
+# toggle the visbility of each method's source with each click on the '[source]'
+# link.
+#
+# Frameless basically is the html template without frames.
+#
+# == Authors
+#
+# * Michael Granger <ged@FaerieMUD.org>
+#
+# Copyright (c) 2002, 2003 The FaerieMUD Consortium. Some rights reserved.
+#
+# This work is licensed under the Creative Commons Attribution License. To view
+# a copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or
+# send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
+# 94305, USA.
+
+module RDoc::Generator::HTML::FRAMELESS
+
+ FRAMELESS = true
+
+ FONTS = RDoc::Generator::HTML::HTML::FONTS
+
+ STYLE = RDoc::Generator::HTML::HTML::STYLE
+
+ HEADER = RDoc::Generator::HTML::HTML::HEADER
+
+ FOOTER = <<-EOF
+ <div id="popupmenu" class="index">
+ <br />
+ <h1 class="index-entries section-bar">Files</h1>
+ <ul>
+<% values["file_list"].each do |file| %>
+ <li><a href="<%= file["href"] %>"><%= file["name"] %></a></li>
+<% end %>
+ </ul>
+
+ <br />
+ <h1 class="index-entries section-bar">Classes</h1>
+ <ul>
+<% values["class_list"].each do |klass| %>
+ <li><a href="<%= klass["href"] %>"><%= klass["name"] %></a></li>
+<% end %>
+ </ul>
+
+ <br />
+ <h1 class="index-entries section-bar">Methods</h1>
+ <ul>
+<% values["method_list"].each do |method| %>
+ <li><a href="<%= method["href"] %>"><%= method["name"] %></a></li>
+<% end %>
+ </ul>
+ </div>
+</body>
+</html>
+ EOF
+
+ FILE_PAGE = RDoc::Generator::HTML::HTML::FILE_PAGE
+
+ CLASS_PAGE = RDoc::Generator::HTML::HTML::CLASS_PAGE
+
+ METHOD_LIST = RDoc::Generator::HTML::HTML::METHOD_LIST
+
+ BODY = HEADER + %{
+
+<%= template_include %> <!-- banner header -->
+
+ <div id="bodyContent">
+
+} + METHOD_LIST + %{
+
+ </div>
+
+} + FOOTER
+
+ SRC_PAGE = RDoc::Generator::HTML::HTML::SRC_PAGE
+
+ FR_INDEX_BODY = RDoc::Generator::HTML::HTML::FR_INDEX_BODY
+
+ FILE_INDEX = RDoc::Generator::HTML::HTML::FILE_INDEX
+
+ CLASS_INDEX = RDoc::Generator::HTML::HTML::CLASS_INDEX
+
+ METHOD_INDEX = RDoc::Generator::HTML::HTML::METHOD_INDEX
+end
diff --git a/ruby/lib/rdoc/generator/html/hefss.rb b/ruby/lib/rdoc/generator/html/hefss.rb
new file mode 100644
index 0000000..540c23d
--- /dev/null
+++ b/ruby/lib/rdoc/generator/html/hefss.rb
@@ -0,0 +1,150 @@
+require 'rdoc/generator/html'
+require 'rdoc/generator/html/kilmerfactory'
+
+module RDoc::Generator::HTML::HEFSS
+
+ FONTS = "Verdana, Arial, Helvetica, sans-serif"
+
+ CENTRAL_STYLE = <<-EOF
+body,p { font-family: <%= values["fonts"] %>;
+ color: #000040; background: #BBBBBB;
+}
+
+td { font-family: <%= values["fonts"] %>;
+ color: #000040;
+}
+
+.attr-rw { font-size: small; color: #444488 }
+
+.title-row {color: #eeeeff;
+ background: #BBBBDD;
+}
+
+.big-title-font { color: white;
+ font-family: <%= values["fonts"] %>;
+ font-size: large;
+ height: 50px}
+
+.small-title-font { color: purple;
+ font-family: <%= values["fonts"] %>;
+ font-size: small; }
+
+.aqua { color: purple }
+
+#diagram img {
+ border: 0;
+}
+
+.method-name, attr-name {
+ font-family: monospace; font-weight: bold;
+}
+
+.tablesubtitle {
+ width: 100%;
+ margin-top: 1ex;
+ margin-bottom: .5ex;
+ padding: 5px 0px 5px 20px;
+ font-size: large;
+ color: purple;
+ background: #BBBBCC;
+}
+
+.tablesubsubtitle {
+ width: 100%;
+ margin-top: 1ex;
+ margin-bottom: .5ex;
+ padding: 5px 0px 5px 20px;
+ font-size: medium;
+ color: white;
+ background: #BBBBCC;
+}
+
+.name-list {
+ font-family: monospace;
+ margin-left: 40px;
+ margin-bottom: 2ex;
+ line-height: 140%;
+}
+
+.description {
+ margin-left: 40px;
+ margin-bottom: 2ex;
+ line-height: 140%;
+}
+
+.methodtitle {
+ font-size: medium;
+ text_decoration: none;
+ padding: 3px 3px 3px 20px;
+ color: #0000AA;
+}
+
+.ruby-comment { color: green; font-style: italic }
+.ruby-constant { color: #4433aa; font-weight: bold; }
+.ruby-identifier { color: #222222; }
+.ruby-ivar { color: #2233dd; }
+.ruby-keyword { color: #3333FF; font-weight: bold }
+.ruby-node { color: #777777; }
+.ruby-operator { color: #111111; }
+.ruby-regexp { color: #662222; }
+.ruby-value { color: #662222; font-style: italic }
+
+.srcbut { float: right }
+ EOF
+
+ INDEX_STYLE = <<-EOF
+body {
+ background-color: #bbbbbb;
+ font-family: #{FONTS};
+ font-size: 11px;
+ font-style: normal;
+ line-height: 14px;
+ color: #000040;
+}
+
+div.banner {
+ background: #bbbbcc;
+ color: white;
+ padding: 1;
+ margin: 0;
+ font-size: 90%;
+ font-weight: bold;
+ line-height: 1.1;
+ text-align: center;
+ width: 100%;
+}
+EOF
+
+ FACTORY = RDoc::Generator::HTML::
+ KilmerFactory.new(:central_css => CENTRAL_STYLE,
+ :index_css => INDEX_STYLE,
+ :method_list_heading => "Subroutines and Functions",
+ :class_and_module_list_heading => "Classes and Modules",
+ :attribute_list_heading => "Arguments")
+
+ STYLE = FACTORY.get_STYLE()
+
+ METHOD_LIST = FACTORY.get_METHOD_LIST()
+
+ BODY = FACTORY.get_BODY()
+
+ FILE_PAGE = FACTORY.get_FILE_PAGE()
+
+ CLASS_PAGE = FACTORY.get_CLASS_PAGE()
+
+ SRC_PAGE = FACTORY.get_SRC_PAGE()
+
+ FR_INDEX_BODY = FACTORY.get_FR_INDEX_BODY()
+
+ FILE_INDEX = FACTORY.get_FILE_INDEX()
+
+ CLASS_INDEX = FACTORY.get_CLASS_INDEX()
+
+ METHOD_INDEX = FACTORY.get_METHOD_INDEX()
+
+ INDEX = FACTORY.get_INDEX()
+
+ def self.write_extra_pages(values)
+ FACTORY.write_extra_pages(values)
+ end
+end
diff --git a/ruby/lib/rdoc/generator/html/html.rb b/ruby/lib/rdoc/generator/html/html.rb
new file mode 100644
index 0000000..823d805
--- /dev/null
+++ b/ruby/lib/rdoc/generator/html/html.rb
@@ -0,0 +1,769 @@
+require 'rdoc/generator/html'
+require 'rdoc/generator/html/common'
+
+##
+# = CSS2 RDoc HTML template
+#
+# This is a template for RDoc that uses XHTML 1.0 Strict and dictates a
+# bit more of the appearance of the output to cascading stylesheets than the
+# default. It was designed for clean inline code display, and uses DHTMl to
+# toggle the visibility of each method's source with each click on the
+# '[source]' link.
+#
+# This template *also* forms the basis of the frameless template.
+#
+# == Authors
+#
+# * Michael Granger <ged@FaerieMUD.org>
+#
+# Copyright (c) 2002, 2003 The FaerieMUD Consortium. Some rights reserved.
+#
+# This work is licensed under the Creative Commons Attribution License. To
+# view a copy of this license, visit
+# http://creativecommons.org/licenses/by/1.0/ or send a letter to Creative
+# Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
+
+module RDoc::Generator::HTML::HTML
+
+ include RDoc::Generator::HTML::Common
+
+ FONTS = "Verdana,Arial,Helvetica,sans-serif"
+
+ STYLE = <<-EOF
+body {
+ font-family: #{FONTS};
+ font-size: 90%;
+ margin: 0;
+ margin-left: 40px;
+ padding: 0;
+ background: white;
+ color: black;
+}
+
+h1, h2, h3, h4 {
+ margin: 0;
+ background: transparent;
+}
+
+h1 {
+ font-size: 150%;
+}
+
+h2,h3,h4 {
+ margin-top: 1em;
+}
+
+:link, :visited {
+ background: #eef;
+ color: #039;
+ text-decoration: none;
+}
+
+:link:hover, :visited:hover {
+ background: #039;
+ color: #eef;
+}
+
+/* Override the base stylesheet's Anchor inside a table cell */
+td > :link, td > :visited {
+ background: transparent;
+ color: #039;
+ text-decoration: none;
+}
+
+/* and inside a section title */
+.section-title > :link, .section-title > :visited {
+ background: transparent;
+ color: #eee;
+ text-decoration: none;
+}
+
+/* === Structural elements =================================== */
+
+.index {
+ margin: 0;
+ margin-left: -40px;
+ padding: 0;
+ font-size: 90%;
+}
+
+.index :link, .index :visited {
+ margin-left: 0.7em;
+}
+
+.index .section-bar {
+ margin-left: 0px;
+ padding-left: 0.7em;
+ background: #ccc;
+ font-size: small;
+}
+
+#classHeader, #fileHeader {
+ width: auto;
+ color: white;
+ padding: 0.5em 1.5em 0.5em 1.5em;
+ margin: 0;
+ margin-left: -40px;
+ border-bottom: 3px solid #006;
+}
+
+#classHeader :link, #fileHeader :link,
+#classHeader :visited, #fileHeader :visited {
+ background: inherit;
+ color: white;
+}
+
+#classHeader td, #fileHeader td {
+ background: inherit;
+ color: white;
+}
+
+#fileHeader {
+ background: #057;
+}
+
+#classHeader {
+ background: #048;
+}
+
+.class-name-in-header {
+ font-size: 180%;
+ font-weight: bold;
+}
+
+#bodyContent {
+ padding: 0 1.5em 0 1.5em;
+}
+
+#description {
+ padding: 0.5em 1.5em;
+ background: #efefef;
+ border: 1px dotted #999;
+}
+
+#description h1, #description h2, #description h3,
+#description h4, #description h5, #description h6 {
+ color: #125;
+ background: transparent;
+}
+
+#validator-badges {
+ text-align: center;
+}
+
+#validator-badges img {
+ border: 0;
+}
+
+#copyright {
+ color: #333;
+ background: #efefef;
+ font: 0.75em sans-serif;
+ margin-top: 5em;
+ margin-bottom: 0;
+ padding: 0.5em 2em;
+}
+
+/* === Classes =================================== */
+
+table.header-table {
+ color: white;
+ font-size: small;
+}
+
+.type-note {
+ font-size: small;
+ color: #dedede;
+}
+
+.section-bar {
+ color: #333;
+ border-bottom: 1px solid #999;
+ margin-left: -20px;
+}
+
+.section-title {
+ background: #79a;
+ color: #eee;
+ padding: 3px;
+ margin-top: 2em;
+ margin-left: -30px;
+ border: 1px solid #999;
+}
+
+.top-aligned-row {
+ vertical-align: top
+}
+
+.bottom-aligned-row {
+ vertical-align: bottom
+}
+
+#diagram img {
+ border: 0;
+}
+
+/* --- Context section classes ----------------------- */
+
+.context-row { }
+
+.context-item-name {
+ font-family: monospace;
+ font-weight: bold;
+ color: black;
+}
+
+.context-item-value {
+ font-size: small;
+ color: #448;
+}
+
+.context-item-desc {
+ color: #333;
+ padding-left: 2em;
+}
+
+/* --- Method classes -------------------------- */
+
+.method-detail {
+ background: #efefef;
+ padding: 0;
+ margin-top: 0.5em;
+ margin-bottom: 1em;
+ border: 1px dotted #ccc;
+}
+
+.method-heading {
+ color: black;
+ background: #ccc;
+ border-bottom: 1px solid #666;
+ padding: 0.2em 0.5em 0 0.5em;
+}
+
+.method-signature {
+ color: black;
+ background: inherit;
+}
+
+.method-name {
+ font-weight: bold;
+}
+
+.method-args {
+ font-style: italic;
+}
+
+.method-description {
+ padding: 0 0.5em 0 0.5em;
+}
+
+/* --- Source code sections -------------------- */
+
+:link.source-toggle, :visited.source-toggle {
+ font-size: 90%;
+}
+
+div.method-source-code {
+ background: #262626;
+ color: #ffdead;
+ margin: 1em;
+ padding: 0.5em;
+ border: 1px dashed #999;
+ overflow: auto;
+}
+
+div.method-source-code pre {
+ color: #ffdead;
+}
+
+/* --- Ruby keyword styles --------------------- */
+
+.standalone-code {
+ background: #221111;
+ color: #ffdead;
+ overflow: auto;
+}
+
+.ruby-constant {
+ color: #7fffd4;
+ background: transparent;
+}
+
+.ruby-keyword {
+ color: #00ffff;
+ background: transparent;
+}
+
+.ruby-ivar {
+ color: #eedd82;
+ background: transparent;
+}
+
+.ruby-operator {
+ color: #00ffee;
+ background: transparent;
+}
+
+.ruby-identifier {
+ color: #ffdead;
+ background: transparent;
+}
+
+.ruby-node {
+ color: #ffa07a;
+ background: transparent;
+}
+
+.ruby-comment {
+ color: #b22222;
+ font-weight: bold;
+ background: transparent;
+}
+
+.ruby-regexp {
+ color: #ffa07a;
+ background: transparent;
+}
+
+.ruby-value {
+ color: #7fffd4;
+ background: transparent;
+}
+EOF
+
+
+#####################################################################
+### H E A D E R T E M P L A T E
+#####################################################################
+
+ HEADER = XHTML_STRICT_PREAMBLE + HTML_ELEMENT + <<-EOF
+<head>
+ <title><%= values["title"] %></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
+ <link rel="stylesheet" href="<%= values["style_url"] %>" type="text/css" media="screen" />
+ <script type="text/javascript">
+ // <![CDATA[
+
+ function popupCode( url ) {
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
+ }
+
+ function toggleCode( id ) {
+ if ( document.getElementById )
+ elem = document.getElementById( id );
+ else if ( document.all )
+ elem = eval( "document.all." + id );
+ else
+ return false;
+
+ elemStyle = elem.style;
+
+ if ( elemStyle.display != "block" ) {
+ elemStyle.display = "block"
+ } else {
+ elemStyle.display = "none"
+ }
+
+ return true;
+ }
+
+ // Make codeblocks hidden by default
+ document.writeln( "<style type=\\"text/css\\">div.method-source-code { display: none }<\\/style>" )
+
+ // ]]>
+ </script>
+
+</head>
+<body>
+EOF
+
+#####################################################################
+### F O O T E R T E M P L A T E
+#####################################################################
+
+ FOOTER = <<-EOF
+<div id="validator-badges">
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
+</div>
+
+</body>
+</html>
+ EOF
+
+
+#####################################################################
+### F I L E P A G E H E A D E R T E M P L A T E
+#####################################################################
+
+ FILE_PAGE = <<-EOF
+ <div id="fileHeader">
+ <h1><%= values["short_name"] %></h1>
+ <table class="header-table">
+ <tr class="top-aligned-row">
+ <td><strong>Path:</strong></td>
+ <td><%= values["full_path"] %>
+<% if values["cvsurl"] then %>
+ &nbsp;(<a href="<%= values["cvsurl"] %>"><acronym title="Concurrent Versioning System">CVS</acronym></a>)
+<% end %>
+ </td>
+ </tr>
+ <tr class="top-aligned-row">
+ <td><strong>Last Update:</strong></td>
+ <td><%= values["dtm_modified"] %></td>
+ </tr>
+ </table>
+ </div>
+ EOF
+
+#####################################################################
+### C L A S S P A G E H E A D E R T E M P L A T E
+#####################################################################
+
+ CLASS_PAGE = <<-EOF
+ <div id="classHeader">
+ <table class="header-table">
+ <tr class="top-aligned-row">
+ <td><strong><%= values["classmod"] %></strong></td>
+ <td class="class-name-in-header"><%= values["full_name"] %></td>
+ </tr>
+ <tr class="top-aligned-row">
+ <td><strong>In:</strong></td>
+ <td>
+<% values["infiles"].each do |infiles| %>
+<% if infiles["full_path_url"] then %>
+ <a href="<%= infiles["full_path_url"] %>">
+<% end %>
+ <%= infiles["full_path"] %>
+<% if infiles["full_path_url"] then %>
+ </a>
+<% end %>
+<% if infiles["cvsurl"] then %>
+ &nbsp;(<a href="<%= infiles["cvsurl"] %>"><acronym title="Concurrent Versioning System">CVS</acronym></a>)
+<% end %>
+ <br />
+<% end %><%# values["infiles"] %>
+ </td>
+ </tr>
+
+<% if values["parent"] then %>
+ <tr class="top-aligned-row">
+ <td><strong>Parent:</strong></td>
+ <td>
+<% if values["par_url"] then %>
+ <a href="<%= values["par_url"] %>">
+<% end %>
+ <%= values["parent"] %>
+<% if values["par_url"] then %>
+ </a>
+<% end %>
+ </td>
+ </tr>
+<% end %>
+ </table>
+ </div>
+ EOF
+
+#####################################################################
+### M E T H O D L I S T T E M P L A T E
+#####################################################################
+
+ METHOD_LIST = <<-EOF
+ <div id="contextContent">
+<% if values["diagram"] then %>
+ <div id="diagram">
+ <%= values["diagram"] %>
+ </div>
+<% end
+
+ if values["description"] then %>
+ <div id="description">
+ <%= values["description"] %>
+ </div>
+<% end
+
+ if values["requires"] then %>
+ <div id="requires-list">
+ <h3 class="section-bar">Required files</h3>
+
+ <div class="name-list">
+<% values["requires"].each do |requires| %>
+ <%= href requires["aref"], requires["name"] %>&nbsp;&nbsp;
+<% end %><%# values["requires"] %>
+ </div>
+ </div>
+<% end
+
+ if values["toc"] then %>
+ <div id="contents-list">
+ <h3 class="section-bar">Contents</h3>
+ <ul>
+<% values["toc"].each do |toc| %>
+ <li><a href="#<%= toc["href"] %>"><%= toc["secname"] %></a></li>
+<% end %><%# values["toc"] %>
+ </ul>
+<% end %>
+ </div>
+
+<% if values["methods"] then %>
+ <div id="method-list">
+ <h3 class="section-bar">Methods</h3>
+
+ <div class="name-list">
+<% values["methods"].each do |methods| %>
+ <%= href methods["aref"], methods["name"] %>&nbsp;&nbsp;
+<% end %><%# values["methods"] %>
+ </div>
+ </div>
+<% end %>
+ </div>
+
+ <!-- if includes -->
+<% if values["includes"] then %>
+ <div id="includes">
+ <h3 class="section-bar">Included Modules</h3>
+
+ <div id="includes-list">
+<% values["includes"].each do |includes| %>
+ <span class="include-name"><%= href includes["aref"], includes["name"] %></span>
+<% end %><%# values["includes"] %>
+ </div>
+ </div>
+<% end
+
+ values["sections"].each do |sections| %>
+ <div id="section">
+<% if sections["sectitle"] then %>
+ <h2 class="section-title"><a name="<%= sections["secsequence"] %>"><%= sections["sectitle"] %></a></h2>
+<% if sections["seccomment"] then %>
+ <div class="section-comment">
+ <%= sections["seccomment"] %>
+ </div>
+<% end
+ end
+
+ if sections["classlist"] then %>
+ <div id="class-list">
+ <h3 class="section-bar">Classes and Modules</h3>
+
+ <%= sections["classlist"] %>
+ </div>
+<% end
+
+ if sections["constants"] then %>
+ <div id="constants-list">
+ <h3 class="section-bar">Constants</h3>
+
+ <div class="name-list">
+ <table summary="Constants">
+<% sections["constants"].each do |constants| %>
+ <tr class="top-aligned-row context-row">
+ <td class="context-item-name"><%= constants["name"] %></td>
+ <td>=</td>
+ <td class="context-item-value"><%= constants["value"] %></td>
+<% if constants["desc"] then %>
+ <td>&nbsp;</td>
+ <td class="context-item-desc"><%= constants["desc"] %></td>
+<% end %>
+ </tr>
+<% end %><%# sections["constants"] %>
+ </table>
+ </div>
+ </div>
+<% end
+
+ if sections["aliases"] then %>
+ <div id="aliases-list">
+ <h3 class="section-bar">External Aliases</h3>
+
+ <div class="name-list">
+ <table summary="aliases">
+<% sections["aliases"].each do |aliases| %>
+ <tr class="top-aligned-row context-row">
+ <td class="context-item-name"><%= aliases["old_name"] %></td>
+ <td>-&gt;</td>
+ <td class="context-item-value"><%= aliases["new_name"] %></td>
+ </tr>
+<% if aliases["desc"] then %>
+ <tr class="top-aligned-row context-row">
+ <td>&nbsp;</td>
+ <td colspan="2" class="context-item-desc"><%= aliases["desc"] %></td>
+ </tr>
+<% end
+ end %><%# sections["aliases"] %>
+ </table>
+ </div>
+ </div>
+<% end %>
+
+<% if sections["attributes"] then %>
+ <div id="attribute-list">
+ <h3 class="section-bar">Attributes</h3>
+
+ <div class="name-list">
+ <table>
+<% sections["attributes"].each do |attribute| %>
+ <tr class="top-aligned-row context-row">
+ <td class="context-item-name"><%= attribute["name"] %></td>
+<% if attribute["rw"] then %>
+ <td class="context-item-value">&nbsp;[<%= attribute["rw"] %>]&nbsp;</td>
+<% end
+ unless attribute["rw"] then %>
+ <td class="context-item-value">&nbsp;&nbsp;</td>
+<% end %>
+ <td class="context-item-desc"><%= attribute["a_desc"] %></td>
+ </tr>
+<% end %><%# sections["attributes"] %>
+ </table>
+ </div>
+ </div>
+<% end %>
+
+ <!-- if method_list -->
+<% if sections["method_list"] then %>
+ <div id="methods">
+<% sections["method_list"].each do |method_list|
+ if method_list["methods"] then %>
+ <h3 class="section-bar"><%= method_list["type"] %> <%= method_list["category"] %> methods</h3>
+
+<% method_list["methods"].each do |methods| %>
+ <div id="method-<%= methods["aref"] %>" class="method-detail">
+ <a name="<%= methods["aref"] %>"></a>
+
+ <div class="method-heading">
+<% if methods["codeurl"] then %>
+ <a href="<%= methods["codeurl"] %>" target="Code" class="method-signature"
+ onclick="popupCode('<%= methods["codeurl"] %>');return false;">
+<% end
+ if methods["sourcecode"] then %>
+ <a href="#<%= methods["aref"] %>" class="method-signature">
+<% end
+ if methods["callseq"] then %>
+ <span class="method-name"><%= methods["callseq"] %></span>
+<% end
+ unless methods["callseq"] then %>
+ <span class="method-name"><%= methods["name"] %></span><span class="method-args"><%= methods["params"] %></span>
+<% end
+ if methods["codeurl"] then %>
+ </a>
+<% end
+ if methods["sourcecode"] then %>
+ </a>
+<% end %>
+ </div>
+
+ <div class="method-description">
+<% if methods["m_desc"] then %>
+ <%= methods["m_desc"] %>
+<% end
+ if methods["sourcecode"] then %>
+ <p><a class="source-toggle" href="#"
+ onclick="toggleCode('<%= methods["aref"] %>-source');return false;">[Source]</a></p>
+ <div class="method-source-code" id="<%= methods["aref"] %>-source">
+<pre>
+<%= methods["sourcecode"] %>
+</pre>
+ </div>
+<% end %>
+ </div>
+ </div>
+
+<% end %><%# method_list["methods"] %><%
+ end
+ end %><%# sections["method_list"] %>
+
+ </div>
+<% end %>
+<% end %><%# values["sections"] %>
+ EOF
+
+#####################################################################
+### B O D Y T E M P L A T E
+#####################################################################
+
+ BODY = HEADER + %{
+
+<%= template_include %> <!-- banner header -->
+
+ <div id="bodyContent">
+
+} + METHOD_LIST + %{
+
+ </div>
+
+} + FOOTER
+
+#####################################################################
+### S O U R C E C O D E T E M P L A T E
+#####################################################################
+
+ SRC_PAGE = XHTML_STRICT_PREAMBLE + HTML_ELEMENT + <<-EOF
+<head>
+ <title><%= values["title"] %></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+ <link rel="stylesheet" href="<%= values["style_url"] %>" type="text/css" media="screen" />
+</head>
+<body class="standalone-code">
+ <pre><%= values["code"] %></pre>
+</body>
+</html>
+ EOF
+
+
+#####################################################################
+### I N D E X F I L E T E M P L A T E S
+#####################################################################
+
+ FR_INDEX_BODY = %{<%= template_include %>}
+
+ FILE_INDEX = XHTML_STRICT_PREAMBLE + HTML_ELEMENT + <<-EOF
+<!--
+
+ <%= values["title"] %>
+
+ -->
+<head>
+ <title><%= values["title"] %></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+ <link rel="stylesheet" href="<%= values["style_url"] %>" type="text/css" />
+ <base target="docwin" />
+</head>
+<body>
+<div class="index">
+ <h1 class="section-bar"><%= values["list_title"] %></h1>
+ <div id="index-entries">
+<% values["entries"].each do |entries| %>
+ <a href="<%= entries["href"] %>"><%= entries["name"] %></a><br />
+<% end %><%# values["entries"] %>
+ </div>
+</div>
+</body>
+</html>
+ EOF
+
+ CLASS_INDEX = FILE_INDEX
+ METHOD_INDEX = FILE_INDEX
+
+ INDEX = XHTML_FRAME_PREAMBLE + HTML_ELEMENT + <<-EOF
+<!--
+
+ <%= values["title"] %>
+
+ -->
+<head>
+ <title><%= values["title"] %></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+</head>
+<frameset rows="20%, 80%">
+ <frameset cols="25%,35%,45%">
+ <frame src="fr_file_index.html" title="Files" name="Files" />
+ <frame src="fr_class_index.html" name="Classes" />
+ <frame src="fr_method_index.html" name="Methods" />
+ </frameset>
+ <frame src="<%= values["initial_page"] %>" name="docwin" />
+</frameset>
+</html>
+ EOF
+
+end
+
diff --git a/ruby/lib/rdoc/generator/html/kilmer.rb b/ruby/lib/rdoc/generator/html/kilmer.rb
new file mode 100644
index 0000000..4c5a9ee
--- /dev/null
+++ b/ruby/lib/rdoc/generator/html/kilmer.rb
@@ -0,0 +1,151 @@
+require 'rdoc/generator/html'
+require 'rdoc/generator/html/kilmerfactory'
+
+module RDoc::Generator::HTML::KILMER
+
+ FONTS = "Verdana, Arial, Helvetica, sans-serif"
+
+ CENTRAL_STYLE = <<-EOF
+body,td,p { font-family: <%= values["fonts"] %>;
+ color: #000040;
+}
+
+.attr-rw { font-size: xx-small; color: #444488 }
+
+.title-row { background-color: #CCCCFF;
+ color: #000010;
+}
+
+.big-title-font {
+ color: black;
+ font-weight: bold;
+ font-family: <%= values["fonts"] %>;
+ font-size: large;
+ height: 60px;
+ padding: 10px 3px 10px 3px;
+}
+
+.small-title-font { color: black;
+ font-family: <%= values["fonts"] %>;
+ font-size:10; }
+
+.aqua { color: black }
+
+#diagram img {
+ border: 0;
+}
+
+.method-name, .attr-name {
+ font-family: font-family: <%= values["fonts"] %>;
+ font-weight: bold;
+ font-size: small;
+ margin-left: 20px;
+ color: #000033;
+}
+
+.tablesubtitle, .tablesubsubtitle {
+ width: 100%;
+ margin-top: 1ex;
+ margin-bottom: .5ex;
+ padding: 5px 0px 5px 3px;
+ font-size: large;
+ color: black;
+ background-color: #CCCCFF;
+ border: thin;
+}
+
+.name-list {
+ margin-left: 5px;
+ margin-bottom: 2ex;
+ line-height: 105%;
+}
+
+.description {
+ margin-left: 5px;
+ margin-bottom: 2ex;
+ line-height: 105%;
+ font-size: small;
+}
+
+.methodtitle {
+ font-size: small;
+ font-weight: bold;
+ text-decoration: none;
+ color: #000033;
+ background: #ccc;
+}
+
+.srclink {
+ font-size: small;
+ font-weight: bold;
+ text-decoration: none;
+ color: #0000DD;
+ background-color: white;
+}
+
+.srcbut { float: right }
+
+.ruby-comment { color: green; font-style: italic }
+.ruby-constant { color: #4433aa; font-weight: bold; }
+.ruby-identifier { color: #222222; }
+.ruby-ivar { color: #2233dd; }
+.ruby-keyword { color: #3333FF; font-weight: bold }
+.ruby-node { color: #777777; }
+.ruby-operator { color: #111111; }
+.ruby-regexp { color: #662222; }
+.ruby-value { color: #662222; font-style: italic }
+ EOF
+
+ INDEX_STYLE = <<-EOF
+body {
+ background-color: #ddddff;
+ font-family: #{FONTS};
+ font-size: 11px;
+ font-style: normal;
+ line-height: 14px;
+ color: #000040;
+}
+
+div.banner {
+ background: #0000aa;
+ color: white;
+ padding: 1;
+ margin: 0;
+ font-size: 90%;
+ font-weight: bold;
+ line-height: 1.1;
+ text-align: center;
+ width: 100%;
+}
+EOF
+
+ FACTORY = RDoc::Generator::HTML::
+ KilmerFactory.new(:central_css => CENTRAL_STYLE,
+ :index_css => INDEX_STYLE)
+
+ STYLE = FACTORY.get_STYLE()
+
+ METHOD_LIST = FACTORY.get_METHOD_LIST()
+
+ BODY = FACTORY.get_BODY()
+
+ FILE_PAGE = FACTORY.get_FILE_PAGE()
+
+ CLASS_PAGE = FACTORY.get_CLASS_PAGE()
+
+ SRC_PAGE = FACTORY.get_SRC_PAGE()
+
+ FR_INDEX_BODY = FACTORY.get_FR_INDEX_BODY()
+
+ FILE_INDEX = FACTORY.get_FILE_INDEX()
+
+ CLASS_INDEX = FACTORY.get_CLASS_INDEX()
+
+ METHOD_INDEX = FACTORY.get_METHOD_INDEX()
+
+ INDEX = FACTORY.get_INDEX()
+
+ def self.write_extra_pages(values)
+ FACTORY.write_extra_pages(values)
+ end
+end
diff --git a/ruby/lib/rdoc/generator/html/kilmerfactory.rb b/ruby/lib/rdoc/generator/html/kilmerfactory.rb
new file mode 100644
index 0000000..ef6f3f3
--- /dev/null
+++ b/ruby/lib/rdoc/generator/html/kilmerfactory.rb
@@ -0,0 +1,427 @@
+require 'rdoc/generator/html'
+require 'rdoc/generator/html/common'
+
+#
+# This class generates Kilmer-style templates. Right now,
+# rdoc is shipped with two such templates:
+# * kilmer
+# * hefss
+#
+# Kilmer-style templates use frames. The left side of the page has
+# three frames stacked on top of each other: one lists
+# files, one lists classes, and one lists methods. If source code
+# is not inlined, an additional frame runs across the bottom of
+# the page and will be used to display method source code.
+# The central (and largest frame) display class and file
+# pages.
+#
+# The constructor of this class accepts a Hash containing stylistic
+# attributes. Then, a get_BLAH instance method of this class returns a
+# value for the template's BLAH constant. get_BODY, for instance, returns
+# the value of the template's BODY constant.
+#
+class RDoc::Generator::HTML::KilmerFactory
+
+ include RDoc::Generator::HTML::Common
+
+ #
+ # The contents of the stylesheet that should be used for the
+ # central frame (for the class and file pages).
+ #
+ # This must be specified in the Hash passed to the constructor.
+ #
+ attr_reader :central_css
+
+ #
+ # The contents of the stylesheet that should be used for the
+ # index pages.
+ #
+ # This must be specified in the Hash passed to the constructor.
+ #
+ attr_reader :index_css
+
+ #
+ # The heading that should be displayed before listing methods.
+ #
+ # If not supplied, this defaults to "Methods".
+ #
+ attr_reader :method_list_heading
+
+ #
+ # The heading that should be displayed before listing classes and
+ # modules.
+ #
+ # If not supplied, this defaults to "Classes and Modules".
+ #
+ attr_reader :class_and_module_list_heading
+
+ #
+ # The heading that should be displayed before listing attributes.
+ #
+ # If not supplied, this defaults to "Attributes".
+ #
+ attr_reader :attribute_list_heading
+
+ #
+ # ====Description:
+ # This method constructs a KilmerFactory instance, which
+ # can be used to build Kilmer-style template classes.
+ # The +style_attributes+ argument is a Hash that contains the
+ # values of the classes attributes (Symbols mapped to Strings).
+ #
+ # ====Parameters:
+ # [style_attributes]
+ # A Hash describing the appearance of the Kilmer-style.
+ #
+ def initialize(style_attributes)
+ @central_css = style_attributes[:central_css]
+ if(!@central_css)
+ raise ArgumentError, "did not specify a value for :central_css"
+ end
+
+ @index_css = style_attributes[:index_css]
+ if(!@index_css)
+ raise ArgumentError, "did not specify a value for :index_css"
+ end
+
+ @method_list_heading = style_attributes[:method_list_heading]
+ if(!@method_list_heading)
+ @method_list_heading = "Methods"
+ end
+
+ @class_and_module_list_heading = style_attributes[:class_and_module_list_heading]
+ if(!@class_and_module_list_heading)
+ @class_and_module_list_heading = "Classes and Modules"
+ end
+
+ @attribute_list_heading = style_attributes[:attribute_list_heading]
+ if(!@attribute_list_heading)
+ @attribute_list_heading = "Attributes"
+ end
+ end
+
+ def get_STYLE
+ return @central_css
+ end
+
+ def get_METHOD_LIST
+ return %{
+<% if values["diagram"] then %>
+<div id="diagram">
+<table width="100%"><tr><td align="center">
+<%= values["diagram"] %>
+</td></tr></table>
+</div>
+<% end %>
+
+<% if values["description"] then %>
+<div class="description"><%= values["description"] %></div>
+<% end %>
+
+<% if values["requires"] then %>
+<table cellpadding="5" width="100%">
+<tr><td class="tablesubtitle">Required files</td></tr>
+</table><br />
+<div class="name-list">
+<% values["requires"].each do |requires| %>
+<%= href requires["aref"], requires["name"] %>
+<% end %><%# values["requires"] %>
+</div>
+<% end %>
+
+<% if values["methods"] then %>
+<table cellpadding="5" width="100%">
+<tr><td class="tablesubtitle">#{@method_list_heading}</td></tr>
+</table><br />
+<div class="name-list">
+<% values["methods"].each do |methods| %>
+<%= href methods["aref"], methods["name"] %>,
+<% end %><%# values["methods"] %>
+</div>
+<% end %>
+
+<% if values["includes"] then %>
+<div class="tablesubsubtitle">Included modules</div><br />
+<div class="name-list">
+<% values["includes"].each do |includes| %>
+ <span class="method-name"><%= href includes["aref"], includes["name"] %></span>
+<% end %><%# values["includes"] %>
+</div>
+<% end %>
+
+<% values["sections"].each do |sections| %>
+ <div id="section">
+<% if sections["sectitle"] then %>
+ <h2 class="section-title"><a name="<%= sections["secsequence"] %>"><%= sections["sectitle"] %></a></h2>
+<% if sections["seccomment"] then %>
+ <div class="section-comment">
+ <%= sections["seccomment"] %>
+ </div>
+<% end %>
+<% end %>
+<% if sections["attributes"] then %>
+<table cellpadding="5" width="100%">
+<tr><td class="tablesubtitle">#{@attribute_list_heading}</td></tr>
+</table><br />
+<table cellspacing="5">
+<% sections["attributes"].each do |attributes| %>
+ <tr valign="top">
+<% if attributes["rw"] then %>
+ <td align="center" class="attr-rw">&nbsp;[<%= attributes["rw"] %>]&nbsp;</td>
+<% end %>
+<% unless attributes["rw"] then %>
+ <td></td>
+<% end %>
+ <td class="attr-name"><%= attributes["name"] %></td>
+ <td><%= attributes["a_desc"] %></td>
+ </tr>
+<% end %><%# sections["attributes"] %>
+</table>
+<% end %>
+
+<% if sections["classlist"] then %>
+<table cellpadding="5" width="100%">
+<tr><td class="tablesubtitle">#{@class_and_module_list_heading}</td></tr>
+</table><br />
+<%= sections["classlist"] %><br />
+<% end %>
+
+<% if sections["method_list"] then %>
+<% sections["method_list"].each do |method_list| %>
+<% if method_list["methods"] then %>
+<table cellpadding="5" width="100%">
+<tr><td class="tablesubtitle"><%= method_list["type"] %> <%= method_list["category"] %> methods</td></tr>
+</table>
+<% method_list["methods"].each do |methods| %>
+<table width="100%" cellspacing="0" cellpadding="5" border="0">
+<tr><td class="methodtitle">
+<a name="<%= methods["aref"] %>">
+<% if methods["callseq"] then %>
+<b><%= methods["callseq"] %></b>
+<% end %>
+<% unless methods["callseq"] then %>
+ <b><%= methods["name"] %></b><%= methods["params"] %>
+<% end %>
+</a>
+<% if methods["codeurl"] then %>
+<a href="<%= methods["codeurl"] %>" target="source" class="srclink">src</a>
+<% end %>
+</td></tr>
+</table>
+<% if methods["m_desc"] then %>
+<div class="description">
+<%= methods["m_desc"] %>
+</div>
+<% end %>
+<% if methods["aka"] then %>
+<div class="aka">
+This method is also aliased as
+<% methods["aka"].each do |aka| %>
+<a href="<%= methods["aref"] %>"><%= methods["name"] %></a>
+<% end %><%# methods["aka"] %>
+</div>
+<% end %>
+<% if methods["sourcecode"] then %>
+<pre class="source">
+<%= methods["sourcecode"] %>
+</pre>
+<% end %>
+<% end %><%# method_list["methods"] %>
+<% end %>
+<% end %><%# sections["method_list"] %>
+<% end %>
+
+<% end %><%# values["sections"] %>
+</div>
+}
+ end
+
+ def get_BODY
+ return XHTML_STRICT_PREAMBLE + HTML_ELEMENT + %{
+<head>
+ <title><%= values["title"] %></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+ <link rel="stylesheet" href="<%= values["style_url"] %>" type="text/css" media="screen" />
+ <script type="text/javascript">
+ <!--
+ function popCode(url) {
+ parent.frames.source.location = url
+ }
+ //-->
+ </script>
+</head>
+<body>
+<div class="bodyContent">
+<%= template_include %> <!-- banner header -->
+
+#{get_METHOD_LIST()}
+</div>
+</body>
+</html>
+}
+ end
+
+def get_FILE_PAGE
+ return %{
+<table width="100%">
+ <tr class="title-row">
+ <td><table width="100%"><tr>
+ <td class="big-title-font" colspan="2">File<br /><%= values["short_name"] %></td>
+ <td align="right"><table cellspacing="0" cellpadding="2">
+ <tr>
+ <td class="small-title-font">Path:</td>
+ <td class="small-title-font"><%= values["full_path"] %>
+<% if values["cvsurl"] then %>
+ &nbsp;(<a href="<%= values["cvsurl"] %>"><acronym title="Concurrent Versioning System">CVS</acronym></a>)
+<% end %>
+ </td>
+ </tr>
+ <tr>
+ <td class="small-title-font">Modified:</td>
+ <td class="small-title-font"><%= values["dtm_modified"] %></td>
+ </tr>
+ </table>
+ </td></tr></table></td>
+ </tr>
+</table><br />
+}
+end
+
+def get_CLASS_PAGE
+ return %{
+<table width="100%" border="0" cellspacing="0">
+ <tr class="title-row">
+ <td class="big-title-font">
+ <%= values["classmod"] %><br /><%= values["full_name"] %>
+ </td>
+ <td align="right">
+ <table cellspacing="0" cellpadding="2">
+ <tr valign="top">
+ <td class="small-title-font">In:</td>
+ <td class="small-title-font">
+<% values["infiles"].each do |infiles| %>
+<%= href infiles["full_path_url"], infiles["full_path"] %>
+<% if infiles["cvsurl"] then %>
+&nbsp;(<a href="<%= infiles["cvsurl"] %>"><acronym title="Concurrent Versioning System">CVS</acronym></a>)
+<% end %>
+<% end %><%# values["infiles"] %>
+ </td>
+ </tr>
+<% if values["parent"] then %>
+ <tr>
+ <td class="small-title-font">Parent:</td>
+ <td class="small-title-font">
+<% if values["par_url"] then %>
+ <a href="<%= values["par_url"] %>" class="cyan">
+<% end %>
+<%= values["parent"] %>
+<% if values["par_url"] then %>
+ </a>
+<% end %>
+ </td>
+ </tr>
+<% end %>
+ </table>
+ </td>
+ </tr>
+</table><br />
+}
+end
+
+def get_SRC_PAGE
+ return XHTML_STRICT_PREAMBLE + HTML_ELEMENT + %{
+<head><title><%= values["title"] %></title>
+<meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+<link rel="stylesheet" href="<%= values["style_url"] %>" type="text/css" media="screen" />
+</head>
+<body>
+<pre><%= values["code"] %></pre>
+</body>
+</html>
+}
+end
+
+def get_FR_INDEX_BODY
+ return %{<%= template_include %>}
+end
+
+def get_FILE_INDEX
+ return XHTML_STRICT_PREAMBLE + HTML_ELEMENT + %{
+<head>
+<title><%= values["title"] %></title>
+<meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+<style type="text/css">
+<!--
+#{@index_css}
+-->
+</style>
+<base target="docwin" />
+</head>
+<body>
+<div class="index">
+<div class="banner"><%= values["list_title"] %></div>
+<% values["entries"].each do |entries| %>
+<a href="<%= entries["href"] %>"><%= entries["name"] %></a><br />
+<% end %><%# values["entries"] %>
+</div>
+</body></html>
+}
+end
+
+def get_CLASS_INDEX
+ return get_FILE_INDEX
+end
+
+def get_METHOD_INDEX
+ return get_FILE_INDEX
+end
+
+def get_INDEX
+ return XHTML_FRAME_PREAMBLE + HTML_ELEMENT + %{
+<head>
+ <title><%= values["title"] %></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+</head>
+
+<frameset cols="20%,*">
+ <frameset rows="15%,35%,50%">
+ <frame src="fr_file_index.html" title="Files" name="Files" />
+ <frame src="fr_class_index.html" name="Classes" />
+ <frame src="fr_method_index.html" name="Methods" />
+ </frameset>
+<% if values["inline_source"] then %>
+ <frame src="<%= values["initial_page"] %>" name="docwin" />
+<% end %>
+<% unless values["inline_source"] then %>
+ <frameset rows="80%,20%">
+ <frame src="<%= values["initial_page"] %>" name="docwin" />
+ <frame src="blank.html" name="source" />
+ </frameset>
+<% end %>
+</frameset>
+
+</html>
+}
+end
+
+def get_BLANK
+ # This will be displayed in the source code frame before
+ # any source code has been selected.
+ return XHTML_STRICT_PREAMBLE + HTML_ELEMENT + %{
+<head>
+ <title>Source Code Frame <%= values["title_suffix"] %></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+ <link rel="stylesheet" href="<%= values["style_url"] %>" type="text/css" media="screen" />
+</head>
+<body>
+</body>
+</html>
+}
+end
+
+def write_extra_pages(values)
+ template = RDoc::TemplatePage.new(get_BLANK())
+ File.open("blank.html", "w") { |f| template.write_html_on(f, values) }
+end
+
+end
diff --git a/ruby/lib/rdoc/generator/html/one_page_html.rb b/ruby/lib/rdoc/generator/html/one_page_html.rb
new file mode 100644
index 0000000..51ae323
--- /dev/null
+++ b/ruby/lib/rdoc/generator/html/one_page_html.rb
@@ -0,0 +1,122 @@
+require 'rdoc/generator/html'
+require 'rdoc/generator/html/common'
+
+module RDoc::Generator::HTML::ONE_PAGE_HTML
+
+ include RDoc::Generator::HTML::Common
+
+ CONTENTS_XML = <<-EOF
+<% if defined? classes and classes["description"] then %>
+<%= classes["description"] %>
+<% end %>
+
+<% if defined? files and files["requires"] then %>
+<h4>Requires:</h4>
+<ul>
+<% files["requires"].each do |requires| %>
+<% if requires["aref"] then %>
+<li><a href="<%= requires["aref"] %>"><%= requires["name"] %></a></li>
+<% end %>
+<% unless requires["aref"] then %>
+<li><%= requires["name"] %></li>
+<% end %>
+<% end %><%# files["requires"] %>
+</ul>
+<% end %>
+
+<% if defined? classes and classes["includes"] then %>
+<h4>Includes</h4>
+<ul>
+<% classes["includes"].each do |includes| %>
+<% if includes["aref"] then %>
+<li><a href="<%= includes["aref"] %>"><%= includes["name"] %></a></li>
+<% end %>
+<% unless includes["aref"] then %>
+<li><%= includes["name"] %></li>
+<% end %>
+<% end %><%# classes["includes"] %>
+</ul>
+<% end %>
+
+<% if defined? classes and classes["sections"] then %>
+<% classes["sections"].each do |sections| %>
+<% if sections["attributes"] then %>
+<h4>Attributes</h4>
+<table>
+<% sections["attributes"].each do |attributes| %>
+<tr><td><%= attributes["name"] %></td><td><%= attributes["rw"] %></td><td><%= attributes["a_desc"] %></td></tr>
+<% end %><%# sections["attributes"] %>
+</table>
+<% end %>
+
+<% if sections["method_list"] then %>
+<h3>Methods</h3>
+<% sections["method_list"].each do |method_list| %>
+<% if method_list["methods"] then %>
+<% method_list["methods"].each do |methods| %>
+<h4><%= methods["type"] %> <%= methods["category"] %> method:
+<% if methods["callseq"] then %>
+<a name="<%= methods["aref"] %>"><%= methods["callseq"] %></a>
+<% end %>
+<% unless methods["callseq"] then %>
+<a name="<%= methods["aref"] %>"><%= methods["name"] %><%= methods["params"] %></a></h4>
+<% end %>
+
+<% if methods["m_desc"] then %>
+<%= methods["m_desc"] %>
+<% end %>
+
+<% if methods["sourcecode"] then %>
+<blockquote><pre>
+<%= methods["sourcecode"] %>
+</pre></blockquote>
+<% end %>
+<% end %><%# method_list["methods"] %>
+<% end %>
+<% end %><%# sections["method_list"] %>
+<% end %>
+<% end %><%# classes["sections"] %>
+<% end %>
+ EOF
+
+ ONE_PAGE = XHTML_STRICT_PREAMBLE + HTML_ELEMENT + %{
+<head>
+ <title><%= values["title"] %></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=<%= values["charset"] %>" />
+</head>
+<body>
+<% values["files"].each do |files| %>
+<h2>File: <a name="<%= files["href"] %>"><%= files["short_name"] %></a></h2>
+<table>
+ <tr><td>Path:</td><td><%= files["full_path"] %></td></tr>
+ <tr><td>Modified:</td><td><%= files["dtm_modified"] %></td></tr>
+</table>
+} + CONTENTS_XML + %{
+<% end %><%# values["files"] %>
+
+<% if values["classes"] then %>
+<h2>Classes</h2>
+<% values["classes"].each do |classes| %>
+<% if classes["parent"] then %>
+<h3><%= classes["classmod"] %> <a name="<%= classes["href"] %>"><%= classes["full_name"] %></a> &lt; <%= href classes["par_url"], classes["parent"] %></h3>
+<% end %>
+<% unless classes["parent"] then %>
+<h3><%= classes["classmod"] %> <%= classes["full_name"] %></h3>
+<% end %>
+
+<% if classes["infiles"] then %>
+(in files
+<% classes["infiles"].each do |infiles| %>
+<%= href infiles["full_path_url"], infiles["full_path"] %>
+<% end %><%# classes["infiles"] %>
+)
+<% end %>
+} + CONTENTS_XML + %{
+<% end %><%# values["classes"] %>
+<% end %>
+</body>
+</html>
+}
+
+end
+
diff --git a/ruby/lib/rdoc/generator/ri.rb b/ruby/lib/rdoc/generator/ri.rb
new file mode 100644
index 0000000..6b7a593
--- /dev/null
+++ b/ruby/lib/rdoc/generator/ri.rb
@@ -0,0 +1,226 @@
+require 'rdoc/generator'
+require 'rdoc/markup/to_flow'
+
+require 'rdoc/ri/cache'
+require 'rdoc/ri/reader'
+require 'rdoc/ri/writer'
+require 'rdoc/ri/descriptions'
+
+class RDoc::Generator::RI
+
+ ##
+ # Generator may need to return specific subclasses depending on the
+ # options they are passed. Because of this we create them using a factory
+
+ def self.for(options)
+ new(options)
+ end
+
+ ##
+ # Set up a new ri generator
+
+ def initialize(options) #:not-new:
+ @options = options
+ @ri_writer = RDoc::RI::Writer.new "."
+ @markup = RDoc::Markup.new
+ @to_flow = RDoc::Markup::ToFlow.new
+
+ @generated = {}
+ end
+
+ ##
+ # Build the initial indices and output objects based on an array of
+ # TopLevel objects containing the extracted information.
+
+ def generate(toplevels)
+ RDoc::TopLevel.all_classes_and_modules.each do |cls|
+ process_class cls
+ end
+ end
+
+ def process_class(from_class)
+ generate_class_info(from_class)
+
+ # now recurse into this class' constituent classes
+ from_class.each_classmodule do |mod|
+ process_class(mod)
+ end
+ end
+
+ def generate_class_info(cls)
+ case cls
+ when RDoc::NormalModule then
+ cls_desc = RDoc::RI::ModuleDescription.new
+ else
+ cls_desc = RDoc::RI::ClassDescription.new
+ cls_desc.superclass = cls.superclass
+ end
+
+ cls_desc.name = cls.name
+ cls_desc.full_name = cls.full_name
+ cls_desc.comment = markup(cls.comment)
+
+ cls_desc.attributes = cls.attributes.sort.map do |a|
+ RDoc::RI::Attribute.new(a.name, a.rw, markup(a.comment))
+ end
+
+ cls_desc.constants = cls.constants.map do |c|
+ RDoc::RI::Constant.new(c.name, c.value, markup(c.comment))
+ end
+
+ cls_desc.includes = cls.includes.map do |i|
+ RDoc::RI::IncludedModule.new(i.name)
+ end
+
+ class_methods, instance_methods = method_list(cls)
+
+ cls_desc.class_methods = class_methods.map do |m|
+ RDoc::RI::MethodSummary.new(m.name)
+ end
+
+ cls_desc.instance_methods = instance_methods.map do |m|
+ RDoc::RI::MethodSummary.new(m.name)
+ end
+
+ update_or_replace(cls_desc)
+
+ class_methods.each do |m|
+ generate_method_info(cls_desc, m)
+ end
+
+ instance_methods.each do |m|
+ generate_method_info(cls_desc, m)
+ end
+ end
+
+ def generate_method_info(cls_desc, method)
+ meth_desc = RDoc::RI::MethodDescription.new
+ meth_desc.name = method.name
+ meth_desc.full_name = cls_desc.full_name
+ if method.singleton
+ meth_desc.full_name += "::"
+ else
+ meth_desc.full_name += "#"
+ end
+ meth_desc.full_name << method.name
+
+ meth_desc.comment = markup(method.comment)
+ meth_desc.params = params_of(method)
+ meth_desc.visibility = method.visibility.to_s
+ meth_desc.is_singleton = method.singleton
+ meth_desc.block_params = method.block_params
+
+ meth_desc.aliases = method.aliases.map do |a|
+ RDoc::RI::AliasName.new(a.name)
+ end
+
+ @ri_writer.add_method(cls_desc, meth_desc)
+ end
+
+ private
+
+ ##
+ # Returns a list of class and instance methods that we'll be documenting
+
+ def method_list(cls)
+ list = cls.method_list
+ unless @options.show_all
+ list = list.find_all do |m|
+ m.visibility == :public || m.visibility == :protected || m.force_documentation
+ end
+ end
+
+ c = []
+ i = []
+ list.sort.each do |m|
+ if m.singleton
+ c << m
+ else
+ i << m
+ end
+ end
+ return c,i
+ end
+
+ def params_of(method)
+ if method.call_seq
+ method.call_seq
+ else
+ params = method.params || ""
+
+ p = params.gsub(/\s*\#.*/, '')
+ p = p.tr("\n", " ").squeeze(" ")
+ p = "(" + p + ")" unless p[0] == ?(
+
+ if (block = method.block_params)
+ block.gsub!(/\s*\#.*/, '')
+ block = block.tr("\n", " ").squeeze(" ")
+ if block[0] == ?(
+ block.sub!(/^\(/, '').sub!(/\)/, '')
+ end
+ p << " {|#{block.strip}| ...}"
+ end
+ p
+ end
+ end
+
+ def markup(comment)
+ return nil if !comment || comment.empty?
+
+ # Convert leading comment markers to spaces, but only
+ # if all non-blank lines have them
+
+ if comment =~ /^(?>\s*)[^\#]/
+ content = comment
+ else
+ content = comment.gsub(/^\s*(#+)/) { $1.tr('#',' ') }
+ end
+ @markup.convert(content, @to_flow)
+ end
+
+ ##
+ # By default we replace existing classes with the same name. If the
+ # --merge option was given, we instead merge this definition into an
+ # existing class. We add our methods, aliases, etc to that class, but do
+ # not change the class's description.
+
+ def update_or_replace(cls_desc)
+ old_cls = nil
+
+ if @options.merge
+ rdr = RDoc::RI::Reader.new RDoc::RI::Cache.new(@options.op_dir)
+
+ namespace = rdr.top_level_namespace
+ namespace = rdr.lookup_namespace_in(cls_desc.name, namespace)
+ if namespace.empty?
+ $stderr.puts "You asked me to merge this source into existing "
+ $stderr.puts "documentation. This file references a class or "
+ $stderr.puts "module called #{cls_desc.name} which I don't"
+ $stderr.puts "have existing documentation for."
+ $stderr.puts
+ $stderr.puts "Perhaps you need to generate its documentation first"
+ exit 1
+ else
+ old_cls = namespace[0]
+ end
+ end
+
+ prev_cls = @generated[cls_desc.full_name]
+
+ if old_cls and not prev_cls then
+ old_desc = rdr.get_class old_cls
+ cls_desc.merge_in old_desc
+ end
+
+ if prev_cls then
+ cls_desc.merge_in prev_cls
+ end
+
+ @generated[cls_desc.full_name] = cls_desc
+
+ @ri_writer.remove_class cls_desc
+ @ri_writer.add_class cls_desc
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/generator/texinfo.rb b/ruby/lib/rdoc/generator/texinfo.rb
new file mode 100644
index 0000000..70db875
--- /dev/null
+++ b/ruby/lib/rdoc/generator/texinfo.rb
@@ -0,0 +1,81 @@
+require 'rdoc/rdoc'
+require 'rdoc/generator'
+require 'rdoc/markup/to_texinfo'
+
+module RDoc
+ module Generator
+ # This generates Texinfo files for viewing with GNU Info or Emacs
+ # from RDoc extracted from Ruby source files.
+ class TEXINFO
+ # What should the .info file be named by default?
+ DEFAULT_INFO_FILENAME = 'rdoc.info'
+
+ include Generator::MarkUp
+
+ # Accept some options
+ def initialize(options)
+ @options = options
+ @options.inline_source = true
+ @options.op_name ||= 'rdoc.texinfo'
+ @options.formatter = ::RDoc::Markup::ToTexInfo.new
+ end
+
+ # Generate the +texinfo+ files
+ def generate(toplevels)
+ @toplevels = toplevels
+ @files, @classes = ::RDoc::Generator::Context.build_indices(@toplevels,
+ @options)
+
+ (@files + @classes).each { |x| x.value_hash }
+
+ open(@options.op_name, 'w') do |f|
+ f.puts TexinfoTemplate.new('files' => @files,
+ 'classes' => @classes,
+ 'filename' => @options.op_name.gsub(/texinfo/, 'info'),
+ 'title' => @options.title).render
+ end
+ # TODO: create info files and install?
+ end
+
+ class << self
+ # Factory? We don't need no stinkin' factory!
+ alias_method :for, :new
+ end
+ end
+
+ # Basically just a wrapper around ERB.
+ # Should probably use RDoc::TemplatePage instead
+ class TexinfoTemplate
+ BASE_DIR = ::File.expand_path(::File.dirname(__FILE__)) # have to calculate this when the file's loaded.
+
+ def initialize(values, file = 'texinfo.erb')
+ @v, @file = [values, file]
+ end
+
+ def template
+ ::File.read(::File.join(BASE_DIR, 'texinfo', @file))
+ end
+
+ # Go!
+ def render
+ ERB.new(template).result binding
+ end
+
+ def href(location, text)
+ text # TODO: how does texinfo do hyperlinks?
+ end
+
+ def target(name, text)
+ text # TODO: how do hyperlink targets work?
+ end
+
+ # TODO: this is probably implemented elsewhere?
+ def method_prefix(section)
+ { 'Class' => '.',
+ 'Module' => '::',
+ 'Instance' => '#',
+ }[section['category']]
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rdoc/generator/texinfo/class.texinfo.erb b/ruby/lib/rdoc/generator/texinfo/class.texinfo.erb
new file mode 100644
index 0000000..74ecc59
--- /dev/null
+++ b/ruby/lib/rdoc/generator/texinfo/class.texinfo.erb
@@ -0,0 +1,44 @@
+@node <%= @v['class']['full_name'].gsub(/::/, '-') %>
+@chapter <%= @v['class']["classmod"] %> <%= @v['class']['full_name'] %>
+
+<% if @v['class']["parent"] and @v['class']['par_url'] %>
+Inherits <%= href @v['class']["par_url"], @v['class']["parent"] %><% end %>
+
+<%= @v['class']["description"] %>
+
+<% if @v['class']["includes"] %>
+Includes
+<% @v['class']["includes"].each do |include| %>
+* <%= href include["aref"], include["name"] %>
+<% end # @v['class']["includes"] %>
+<% end %>
+
+<% if @v['class']["sections"] %>
+<% @v['class']["sections"].each do |section| %>
+<% if section["attributes"] %>
+Attributes
+<% section["attributes"].each do |attributes| %>
+* <%= attributes["name"] %> <%= attributes["rw"] %> <%= attributes["a_desc"] %>
+<% end # section["attributes"] %>
+<% end %>
+<% end %>
+
+<% @v['class']["sections"].each do |section| %>
+<% if section["method_list"] %>
+Methods
+@menu
+<% section["method_list"].each_with_index do |method_list, i| %>
+<%= i %>
+<% (method_list["methods"] || []).each do |method| %>
+* <%= @v['class']['full_name'].gsub(/::/, '-') %><%= method_prefix method_list %><%= method['name'] %>::<% end %>
+<% end %>
+@end menu
+
+<% section["method_list"].each do |method_list| %>
+<% (method_list["methods"] || []).uniq.each do |method| %>
+<%= TexinfoTemplate.new(@v.merge({'method' => method, 'list' => method_list}),
+ 'method.texinfo.erb').render %><% end %>
+<% end %>
+<% end # if section["method_list"] %>
+<% end # @v['class']["sections"] %>
+<% end %>
diff --git a/ruby/lib/rdoc/generator/texinfo/file.texinfo.erb b/ruby/lib/rdoc/generator/texinfo/file.texinfo.erb
new file mode 100644
index 0000000..b619b94
--- /dev/null
+++ b/ruby/lib/rdoc/generator/texinfo/file.texinfo.erb
@@ -0,0 +1,6 @@
+<% if false %>
+<h2>File: <%= @v['file']["short_name"] %></h2>
+Path: <%= @v['file']["full_path"] %>
+
+<%= TexinfoTemplate.new(@v, 'content.texinfo.erb').render %>
+<% end %>
diff --git a/ruby/lib/rdoc/generator/texinfo/method.texinfo.erb b/ruby/lib/rdoc/generator/texinfo/method.texinfo.erb
new file mode 100644
index 0000000..f5c2b73
--- /dev/null
+++ b/ruby/lib/rdoc/generator/texinfo/method.texinfo.erb
@@ -0,0 +1,6 @@
+@node <%= @v['class']['full_name'].gsub(/::/, '-') %><%= method_prefix @v['list'] %><%= @v['method']['name'] %>
+@section <%= @v['class']["classmod"] %> <%= @v['class']['full_name'] %><%= method_prefix @v['list'] %><%= @v['method']['name'] %>
+<%= @v['method']["type"] %> <%= @v['method']["category"] %> method:
+<%= target @v['method']["aref"], @v['method']['callseq'] ||
+ @v['method']["name"] + @v['method']["params"] %>
+<%= @v['method']["m_desc"] %>
diff --git a/ruby/lib/rdoc/generator/texinfo/texinfo.erb b/ruby/lib/rdoc/generator/texinfo/texinfo.erb
new file mode 100644
index 0000000..235f63d
--- /dev/null
+++ b/ruby/lib/rdoc/generator/texinfo/texinfo.erb
@@ -0,0 +1,28 @@
+\input texinfo @c -*-texinfo-*-
+@c %**start of header
+@setfilename <%= @v['filename'] %>
+@settitle <%= @v['title'] %>
+@c %**end of header
+
+@contents @c TODO: whitespace is a mess... =\
+
+@ifnottex
+@node Top
+
+@top <%= @v['title'] %>
+@end ifnottex
+
+<% if @f = @v['files'].detect { |f| f.name =~ /Readme/i } %>
+<%= @f.values['description'] %><% end %>
+
+@menu
+<% @v['classes'].each do |klass| %>
+* <%= klass.name.gsub(/::/, '-') %>::<% end %>
+@c TODO: add files
+@end menu
+
+<% (@v['classes'] || []).each_with_index do |klass, i| %>
+<%= TexinfoTemplate.new(@v.merge('class' => klass.values),
+ 'class.texinfo.erb').render %><% end %>
+
+@bye
diff --git a/ruby/lib/rdoc/generator/xml.rb b/ruby/lib/rdoc/generator/xml.rb
new file mode 100644
index 0000000..0d4c5a7
--- /dev/null
+++ b/ruby/lib/rdoc/generator/xml.rb
@@ -0,0 +1,117 @@
+require 'rdoc/generator/html'
+
+##
+# Generate XML output as one big file
+
+class RDoc::Generator::XML < RDoc::Generator::HTML
+
+ ##
+ # Standard generator factory
+
+ def self.for(options)
+ new(options)
+ end
+
+ def initialize(*args)
+ super
+ end
+
+ ##
+ # Build the initial indices and output objects
+ # based on an array of TopLevel objects containing
+ # the extracted information.
+
+ def generate(info)
+ @info = info
+ @files = []
+ @classes = []
+ @hyperlinks = {}
+
+ build_indices
+ generate_xml
+ end
+
+ ##
+ # Generate:
+ #
+ # * a list of File objects for each TopLevel object.
+ # * a list of Class objects for each first level
+ # class or module in the TopLevel objects
+ # * a complete list of all hyperlinkable terms (file,
+ # class, module, and method names)
+
+ def build_indices
+ @info.each do |toplevel|
+ @files << RDoc::Generator::File.new(toplevel, @options, RDoc::Generator::FILE_DIR)
+ end
+
+ RDoc::TopLevel.all_classes_and_modules.each do |cls|
+ build_class_list(cls, @files[0], RDoc::Generator::CLASS_DIR)
+ end
+ end
+
+ def build_class_list(from, html_file, class_dir)
+ @classes << RDoc::Generator::Class.new(from, html_file, class_dir, @options)
+ from.each_classmodule do |mod|
+ build_class_list(mod, html_file, class_dir)
+ end
+ end
+
+ ##
+ # Generate all the HTML. For the one-file case, we generate
+ # all the information in to one big hash
+
+ def generate_xml
+ values = {
+ 'charset' => @options.charset,
+ 'files' => gen_into(@files),
+ 'classes' => gen_into(@classes)
+ }
+
+ template = RDoc::TemplatePage.new @template::ONE_PAGE
+
+ if @options.op_name
+ opfile = File.open(@options.op_name, "w")
+ else
+ opfile = $stdout
+ end
+ template.write_html_on(opfile, values)
+ end
+
+ def gen_into(list)
+ res = []
+ list.each do |item|
+ res << item.value_hash
+ end
+ res
+ end
+
+ def gen_file_index
+ gen_an_index(@files, 'Files')
+ end
+
+ def gen_class_index
+ gen_an_index(@classes, 'Classes')
+ end
+
+ def gen_method_index
+ gen_an_index(RDoc::Generator::HtmlMethod.all_methods, 'Methods')
+ end
+
+ def gen_an_index(collection, title)
+ res = []
+ collection.sort.each do |f|
+ if f.document_self
+ res << { "href" => f.path, "name" => f.index_name }
+ end
+ end
+
+ return {
+ "entries" => res,
+ 'list_title' => title,
+ 'index_url' => main_url,
+ }
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/generator/xml/rdf.rb b/ruby/lib/rdoc/generator/xml/rdf.rb
new file mode 100644
index 0000000..7b15c69
--- /dev/null
+++ b/ruby/lib/rdoc/generator/xml/rdf.rb
@@ -0,0 +1,113 @@
+require 'rdoc/generator/xml'
+
+module RDoc::Generator::XML::RDF
+
+ CONTENTS_RDF = <<-EOF
+<% if defined? classes and classes["description"] then %>
+ <description rd:parseType="Literal">
+<%= classes["description"] %>
+ </description>
+<% end %>
+
+<% if defined? files and files["requires"] then %>
+<% files["requires"].each do |requires| %>
+ <rd:required-file rd:name="<%= requires["name"] %>" />
+<% end # files["requires"] %>
+<% end %>
+
+<% if defined? classes and classes["includes"] then %>
+ <IncludedModuleList>
+<% classes["includes"].each do |includes| %>
+ <included-module rd:name="<%= includes["name"] %>" />
+<% end # includes["includes"] %>
+ </IncludedModuleList>
+<% end %>
+
+<% if defined? classes and classes["sections"] then %>
+<% classes["sections"].each do |sections| %>
+<% if sections["attributes"] then %>
+<% sections["attributes"].each do |attributes| %>
+ <contents>
+ <Attribute rd:name="<%= attributes["name"] %>">
+<% if attributes["rw"] then %>
+ <attribute-rw><%= attributes["rw"] %></attribute-rw>
+<% end %>
+ <description rdf:parseType="Literal"><%= attributes["a_desc"] %></description>
+ </Attribute>
+ </contents>
+<% end # sections["attributes"] %>
+<% end %>
+
+<% if sections["method_list"] then %>
+<% sections["method_list"].each do |method_list| %>
+<% if method_list["methods"] then %>
+<% method_list["methods"].each do |methods| %>
+ <contents>
+ <Method rd:name="<%= methods["name"] %>" rd:visibility="<%= methods["type"] %>"
+ rd:category="<%= methods["category"] %>" rd:id="<%= methods["aref"] %>">
+ <parameters><%= methods["params"] %></parameters>
+<% if methods["m_desc"] then %>
+ <description rdf:parseType="Literal">
+<%= methods["m_desc"] %>
+ </description>
+<% end %>
+<% if methods["sourcecode"] then %>
+ <source-code-listing rdf:parseType="Literal">
+<%= methods["sourcecode"] %>
+ </source-code-listing>
+<% end %>
+ </Method>
+ </contents>
+<% end # method_list["methods"] %>
+<% end %>
+<% end # sections["method_list"] %>
+<% end %>
+ <!-- end method list -->
+<% end # classes["sections"] %>
+<% end %>
+ EOF
+
+########################################################################
+
+ ONE_PAGE = %{<?xml version="1.0" encoding="utf-8"?>
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns="http://pragprog.com/rdoc/rdoc.rdf#"
+ xmlns:rd="http://pragprog.com/rdoc/rdoc.rdf#">
+
+<!-- RDoc -->
+<% values["files"].each do |files| %>
+ <rd:File rd:name="<%= files["short_name"] %>" rd:id="<%= files["href"] %>">
+ <path><%= files["full_path"] %></path>
+ <dtm-modified><%= files["dtm_modified"] %></dtm-modified>
+} + CONTENTS_RDF + %{
+ </rd:File>
+<% end # values["files"] %>
+<% values["classes"].each do |classes| %>
+ <<%= values["classmod"] %> rd:name="<%= classes["full_name"] %>" rd:id="<%= classes["full_name"] %>">
+ <classmod-info>
+<% if classes["infiles"] then %>
+ <InFiles>
+<% classes["infiles"].each do |infiles| %>
+ <infile>
+ <File rd:name="<%= infiles["full_path"] %>"
+<% if infiles["full_path_url"] then %>
+ rdf:about="<%= infiles["full_path_url"] %>"
+<% end %>
+ />
+ </infile>
+<% end # classes["infiles"] %>
+ </InFiles>
+<% end %>
+<% if classes["parent"] then %>
+ <superclass><%= href classes["par_url"], classes["parent"] %></superclass>
+<% end %>
+ </classmod-info>
+} + CONTENTS_RDF + %{
+ </<%= classes["classmod"] %>>
+<% end # values["classes"] %>
+<!-- /RDoc -->
+</rdf:RDF>
+}
+
+end
+
diff --git a/ruby/lib/rdoc/generator/xml/xml.rb b/ruby/lib/rdoc/generator/xml/xml.rb
new file mode 100644
index 0000000..4b54e73
--- /dev/null
+++ b/ruby/lib/rdoc/generator/xml/xml.rb
@@ -0,0 +1,123 @@
+require 'rdoc/generator/xml'
+
+module RDoc::Generator::XML::XML
+
+ CONTENTS_XML = <<-EOF
+<% if defined? classes and classes["description"] then %>
+ <description>
+<%= classes["description"] %>
+ </description>
+<% end %>
+ <contents>
+<% if defined? files and files["requires"] then %>
+ <required-file-list>
+<% files["requires"].each do |requires| %>
+ <required-file name="<%= requires["name"] %>"
+<% if requires["aref"] then %>
+ href="<%= requires["aref"] %>"
+<% end %>
+ />
+<% end %><%# files["requires"] %>
+ </required-file-list>
+<% end %>
+<% if defined? classes and classes["sections"] then %>
+<% classes["sections"].each do |sections| %>
+<% if sections["constants"] then %>
+ <constant-list>
+<% sections["constants"].each do |constant| %>
+ <constant name="<%= constant["name"] %>">
+<% if constant["value"] then %>
+ <value><%= constant["value"] %></value>
+<% end %>
+ <description><%= constant["a_desc"] %></description>
+ </constant>
+<% end %><%# sections["constants"] %>
+ </constant-list>
+<% end %>
+<% if sections["attributes"] then %>
+ <attribute-list>
+<% sections["attributes"].each do |attributes| %>
+ <attribute name="<%= attributes["name"] %>">
+<% if attributes["rw"] then %>
+ <attribute-rw><%= attributes["rw"] %></attribute-rw>
+<% end %>
+ <description><%= attributes["a_desc"] %></description>
+ </attribute>
+<% end %><%# sections["attributes"] %>
+ </attribute-list>
+<% end %>
+<% if sections["method_list"] then %>
+ <method-list>
+<% sections["method_list"].each do |method_list| %>
+<% if method_list["methods"] then %>
+<% method_list["methods"].each do |methods| %>
+ <method name="<%= methods["name"] %>" type="<%= methods["type"] %>" category="<%= methods["category"] %>" id="<%= methods["aref"] %>">
+ <parameters><%= methods["params"] %></parameters>
+<% if methods["m_desc"] then %>
+ <description>
+<%= methods["m_desc"] %>
+ </description>
+<% end %>
+<% if methods["sourcecode"] then %>
+ <source-code-listing>
+<%= methods["sourcecode"] %>
+ </source-code-listing>
+<% end %>
+ </method>
+<% end %><%# method_list["methods"] %>
+<% end %>
+<% end %><%# sections["method_list"] %>
+ </method-list>
+<% end %>
+<% end %><%# classes["sections"] %>
+<% end %>
+<% if defined? classes and classes["includes"] then %>
+ <included-module-list>
+<% classes["includes"].each do |includes| %>
+ <included-module name="<%= includes["name"] %>"
+<% if includes["aref"] then %>
+ href="<%= includes["aref"] %>"
+<% end %>
+ />
+<% end %><%# classes["includes"] %>
+ </included-module-list>
+<% end %>
+ </contents>
+ EOF
+
+ ONE_PAGE = %{<?xml version="1.0" encoding="utf-8"?>
+<rdoc>
+<file-list>
+<% values["files"].each do |files| %>
+ <file name="<%= files["short_name"] %>" id="<%= files["href"] %>">
+ <file-info>
+ <path><%= files["full_path"] %></path>
+ <dtm-modified><%= files["dtm_modified"] %></dtm-modified>
+ </file-info>
+} + CONTENTS_XML + %{
+ </file>
+<% end %><%# values["files"] %>
+</file-list>
+<class-module-list>
+<% values["classes"].each do |classes| %>
+ <<%= classes["classmod"] %> name="<%= classes["full_name"] %>" id="<%= classes["full_name"] %>">
+ <classmod-info>
+<% if classes["infiles"] then %>
+ <infiles>
+<% classes["infiles"].each do |infiles| %>
+ <infile><%= href infiles["full_path_url"], infiles["full_path"] %></infile>
+<% end %><%# classes["infiles"] %>
+ </infiles>
+<% end %>
+<% if classes["parent"] then %>
+ <superclass><%= href classes["par_url"], classes["parent"] %></superclass>
+<% end %>
+ </classmod-info>
+} + CONTENTS_XML + %{
+ </<%= classes["classmod"] %>>
+<% end %><%# values["classes"] %>
+</class-module-list>
+</rdoc>
+}
+
+end
diff --git a/ruby/lib/rdoc/known_classes.rb b/ruby/lib/rdoc/known_classes.rb
new file mode 100644
index 0000000..dbb1802
--- /dev/null
+++ b/ruby/lib/rdoc/known_classes.rb
@@ -0,0 +1,68 @@
+module RDoc
+
+ ##
+ # Ruby's built-in classes, modules and exceptions
+
+ KNOWN_CLASSES = {
+ "rb_cArray" => "Array",
+ "rb_cBignum" => "Bignum",
+ "rb_cClass" => "Class",
+ "rb_cData" => "Data",
+ "rb_cDir" => "Dir",
+ "rb_cFalseClass" => "FalseClass",
+ "rb_cFile" => "File",
+ "rb_cFixnum" => "Fixnum",
+ "rb_cFloat" => "Float",
+ "rb_cHash" => "Hash",
+ "rb_cIO" => "IO",
+ "rb_cInteger" => "Integer",
+ "rb_cModule" => "Module",
+ "rb_cNilClass" => "NilClass",
+ "rb_cNumeric" => "Numeric",
+ "rb_cObject" => "Object",
+ "rb_cProc" => "Proc",
+ "rb_cRange" => "Range",
+ "rb_cRegexp" => "Regexp",
+ "rb_cRubyVM" => "RubyVM",
+ "rb_cString" => "String",
+ "rb_cStruct" => "Struct",
+ "rb_cSymbol" => "Symbol",
+ "rb_cThread" => "Thread",
+ "rb_cTime" => "Time",
+ "rb_cTrueClass" => "TrueClass",
+
+ "rb_eArgError" => "ArgError",
+ "rb_eEOFError" => "EOFError",
+ "rb_eException" => "Exception",
+ "rb_eFatal" => "Fatal",
+ "rb_eFloatDomainError" => "FloatDomainError",
+ "rb_eIOError" => "IOError",
+ "rb_eIndexError" => "IndexError",
+ "rb_eInterrupt" => "Interrupt",
+ "rb_eLoadError" => "LoadError",
+ "rb_eNameError" => "NameError",
+ "rb_eNoMemError" => "NoMemError",
+ "rb_eNotImpError" => "NotImpError",
+ "rb_eRangeError" => "RangeError",
+ "rb_eRuntimeError" => "RuntimeError",
+ "rb_eScriptError" => "ScriptError",
+ "rb_eSecurityError" => "SecurityError",
+ "rb_eSignal" => "Signal",
+ "rb_eStandardError" => "StandardError",
+ "rb_eSyntaxError" => "SyntaxError",
+ "rb_eSystemCallError" => "SystemCallError",
+ "rb_eSystemExit" => "SystemExit",
+ "rb_eTypeError" => "TypeError",
+ "rb_eZeroDivError" => "ZeroDivError",
+
+ "rb_mComparable" => "Comparable",
+ "rb_mEnumerable" => "Enumerable",
+ "rb_mErrno" => "Errno",
+ "rb_mFileTest" => "FileTest",
+ "rb_mGC" => "GC",
+ "rb_mKernel" => "Kernel",
+ "rb_mMath" => "Math",
+ "rb_mProcess" => "Process"
+ }
+
+end
diff --git a/ruby/lib/rdoc/markup.rb b/ruby/lib/rdoc/markup.rb
new file mode 100644
index 0000000..9d22b38
--- /dev/null
+++ b/ruby/lib/rdoc/markup.rb
@@ -0,0 +1,378 @@
+require 'rdoc'
+
+##
+# RDoc::Markup parses plain text documents and attempts to decompose them into
+# their constituent parts. Some of these parts are high-level: paragraphs,
+# chunks of verbatim text, list entries and the like. Other parts happen at
+# the character level: a piece of bold text, a word in code font. This markup
+# is similar in spirit to that used on WikiWiki webs, where folks create web
+# pages using a simple set of formatting rules.
+#
+# RDoc::Markup itself does no output formatting: this is left to a different
+# set of classes.
+#
+# RDoc::Markup is extendable at runtime: you can add \new markup elements to
+# be recognised in the documents that RDoc::Markup parses.
+#
+# RDoc::Markup is intended to be the basis for a family of tools which share
+# the common requirement that simple, plain-text should be rendered in a
+# variety of different output formats and media. It is envisaged that
+# RDoc::Markup could be the basis for formatting RDoc style comment blocks,
+# Wiki entries, and online FAQs.
+#
+# == Synopsis
+#
+# This code converts +input_string+ to HTML. The conversion takes place in
+# the +convert+ method, so you can use the same RDoc::Markup converter to
+# convert multiple input strings.
+#
+# require 'rdoc/markup/to_html'
+#
+# h = RDoc::Markup::ToHtml.new
+#
+# puts h.convert(input_string)
+#
+# You can extend the RDoc::Markup parser to recognise new markup
+# sequences, and to add special processing for text that matches a
+# regular expression. Here we make WikiWords significant to the parser,
+# and also make the sequences {word} and \<no>text...</no> signify
+# strike-through text. When then subclass the HTML output class to deal
+# with these:
+#
+# require 'rdoc/markup'
+# require 'rdoc/markup/to_html'
+#
+# class WikiHtml < RDoc::Markup::ToHtml
+# def handle_special_WIKIWORD(special)
+# "<font color=red>" + special.text + "</font>"
+# end
+# end
+#
+# m = RDoc::Markup.new
+# m.add_word_pair("{", "}", :STRIKE)
+# m.add_html("no", :STRIKE)
+#
+# m.add_special(/\b([A-Z][a-z]+[A-Z]\w+)/, :WIKIWORD)
+#
+# wh = WikiHtml.new
+# wh.add_tag(:STRIKE, "<strike>", "</strike>")
+#
+# puts "<body>#{wh.convert ARGF.read}</body>"
+#
+#--
+# Author:: Dave Thomas, dave@pragmaticprogrammer.com
+# License:: Ruby license
+
+class RDoc::Markup
+
+ SPACE = ?\s
+
+ # List entries look like:
+ # * text
+ # 1. text
+ # [label] text
+ # label:: text
+ #
+ # Flag it as a list entry, and work out the indent for subsequent lines
+
+ SIMPLE_LIST_RE = /^(
+ ( \* (?# bullet)
+ |- (?# bullet)
+ |\d+\. (?# numbered )
+ |[A-Za-z]\. (?# alphabetically numbered )
+ )
+ \s+
+ )\S/x
+
+ LABEL_LIST_RE = /^(
+ ( \[.*?\] (?# labeled )
+ |\S.*:: (?# note )
+ )(?:\s+|$)
+ )/x
+
+ ##
+ # Take a block of text and use various heuristics to determine it's
+ # structure (paragraphs, lists, and so on). Invoke an event handler as we
+ # identify significant chunks.
+
+ def initialize
+ @am = RDoc::Markup::AttributeManager.new
+ @output = nil
+ end
+
+ ##
+ # Add to the sequences used to add formatting to an individual word (such
+ # as *bold*). Matching entries will generate attributes that the output
+ # formatters can recognize by their +name+.
+
+ def add_word_pair(start, stop, name)
+ @am.add_word_pair(start, stop, name)
+ end
+
+ ##
+ # Add to the sequences recognized as general markup.
+
+ def add_html(tag, name)
+ @am.add_html(tag, name)
+ end
+
+ ##
+ # Add to other inline sequences. For example, we could add WikiWords using
+ # something like:
+ #
+ # parser.add_special(/\b([A-Z][a-z]+[A-Z]\w+)/, :WIKIWORD)
+ #
+ # Each wiki word will be presented to the output formatter via the
+ # accept_special method.
+
+ def add_special(pattern, name)
+ @am.add_special(pattern, name)
+ end
+
+ ##
+ # We take a string, split it into lines, work out the type of each line,
+ # and from there deduce groups of lines (for example all lines in a
+ # paragraph). We then invoke the output formatter using a Visitor to
+ # display the result.
+
+ def convert(str, op)
+ lines = str.split(/\r?\n/).map { |line| Line.new line }
+ @lines = Lines.new lines
+
+ return "" if @lines.empty?
+ @lines.normalize
+ assign_types_to_lines
+ group = group_lines
+ # call the output formatter to handle the result
+ #group.each { |line| p line }
+ group.accept @am, op
+ end
+
+ private
+
+ ##
+ # Look through the text at line indentation. We flag each line as being
+ # Blank, a paragraph, a list element, or verbatim text.
+
+ def assign_types_to_lines(margin = 0, level = 0)
+ while line = @lines.next
+ if line.blank? then
+ line.stamp :BLANK, level
+ next
+ end
+
+ # if a line contains non-blanks before the margin, then it must belong
+ # to an outer level
+
+ text = line.text
+
+ for i in 0...margin
+ if text[i] != SPACE
+ @lines.unget
+ return
+ end
+ end
+
+ active_line = text[margin..-1]
+
+ # Rules (horizontal lines) look like
+ #
+ # --- (three or more hyphens)
+ #
+ # The more hyphens, the thicker the rule
+ #
+
+ if /^(---+)\s*$/ =~ active_line
+ line.stamp :RULE, level, $1.length-2
+ next
+ end
+
+ # Then look for list entries. First the ones that have to have
+ # text following them (* xxx, - xxx, and dd. xxx)
+
+ if SIMPLE_LIST_RE =~ active_line
+ offset = margin + $1.length
+ prefix = $2
+ prefix_length = prefix.length
+
+ flag = case prefix
+ when "*","-" then :BULLET
+ when /^\d/ then :NUMBER
+ when /^[A-Z]/ then :UPPERALPHA
+ when /^[a-z]/ then :LOWERALPHA
+ else raise "Invalid List Type: #{self.inspect}"
+ end
+
+ line.stamp :LIST, level+1, prefix, flag
+ text[margin, prefix_length] = " " * prefix_length
+ assign_types_to_lines(offset, level + 1)
+ next
+ end
+
+ if LABEL_LIST_RE =~ active_line
+ offset = margin + $1.length
+ prefix = $2
+ prefix_length = prefix.length
+
+ next if handled_labeled_list(line, level, margin, offset, prefix)
+ end
+
+ # Headings look like
+ # = Main heading
+ # == Second level
+ # === Third
+ #
+ # Headings reset the level to 0
+
+ if active_line[0] == ?= and active_line =~ /^(=+)\s*(.*)/
+ prefix_length = $1.length
+ prefix_length = 6 if prefix_length > 6
+ line.stamp :HEADING, 0, prefix_length
+ line.strip_leading(margin + prefix_length)
+ next
+ end
+
+ # If the character's a space, then we have verbatim text,
+ # otherwise
+
+ if active_line[0] == SPACE
+ line.strip_leading(margin) if margin > 0
+ line.stamp :VERBATIM, level
+ else
+ line.stamp :PARAGRAPH, level
+ end
+ end
+ end
+
+ ##
+ # Handle labeled list entries, We have a special case to deal with.
+ # Because the labels can be long, they force the remaining block of text
+ # over the to right:
+ #
+ # this is a long label that I wrote:: and here is the
+ # block of text with
+ # a silly margin
+ #
+ # So we allow the special case. If the label is followed by nothing, and
+ # if the following line is indented, then we take the indent of that line
+ # as the new margin.
+ #
+ # this is a long label that I wrote::
+ # here is a more reasonably indented block which
+ # will be attached to the label.
+ #
+
+ def handled_labeled_list(line, level, margin, offset, prefix)
+ prefix_length = prefix.length
+ text = line.text
+ flag = nil
+
+ case prefix
+ when /^\[/ then
+ flag = :LABELED
+ prefix = prefix[1, prefix.length-2]
+ when /:$/ then
+ flag = :NOTE
+ prefix.chop!
+ else
+ raise "Invalid List Type: #{self.inspect}"
+ end
+
+ # body is on the next line
+ if text.length <= offset then
+ original_line = line
+ line = @lines.next
+ return false unless line
+ text = line.text
+
+ for i in 0..margin
+ if text[i] != SPACE
+ @lines.unget
+ return false
+ end
+ end
+
+ i = margin
+ i += 1 while text[i] == SPACE
+
+ if i >= text.length then
+ @lines.unget
+ return false
+ else
+ offset = i
+ prefix_length = 0
+
+ if text[offset..-1] =~ SIMPLE_LIST_RE then
+ @lines.unget
+ line = original_line
+ line.text = ''
+ else
+ @lines.delete original_line
+ end
+ end
+ end
+
+ line.stamp :LIST, level+1, prefix, flag
+ text[margin, prefix_length] = " " * prefix_length
+ assign_types_to_lines(offset, level + 1)
+ return true
+ end
+
+ ##
+ # Return a block consisting of fragments which are paragraphs, list
+ # entries or verbatim text. We merge consecutive lines of the same type
+ # and level together. We are also slightly tricky with lists: the lines
+ # following a list introduction look like paragraph lines at the next
+ # level, and we remap them into list entries instead.
+
+ def group_lines
+ @lines.rewind
+
+ in_list = false
+ wanted_type = wanted_level = nil
+
+ block = LineCollection.new
+ group = nil
+
+ while line = @lines.next
+ if line.level == wanted_level and line.type == wanted_type
+ group.add_text(line.text)
+ else
+ group = block.fragment_for(line)
+ block.add(group)
+
+ if line.type == :LIST
+ wanted_type = :PARAGRAPH
+ else
+ wanted_type = line.type
+ end
+
+ wanted_level = line.type == :HEADING ? line.param : line.level
+ end
+ end
+
+ block.normalize
+ block
+ end
+
+ ##
+ # For debugging, we allow access to our line contents as text.
+
+ def content
+ @lines.as_text
+ end
+ public :content
+
+ ##
+ # For debugging, return the list of line types.
+
+ def get_line_types
+ @lines.line_types
+ end
+ public :get_line_types
+
+end
+
+require 'rdoc/markup/fragments'
+require 'rdoc/markup/inline'
+require 'rdoc/markup/lines'
diff --git a/ruby/lib/rdoc/markup/attribute_manager.rb b/ruby/lib/rdoc/markup/attribute_manager.rb
new file mode 100644
index 0000000..d13b793
--- /dev/null
+++ b/ruby/lib/rdoc/markup/attribute_manager.rb
@@ -0,0 +1,265 @@
+require 'rdoc/markup/inline'
+
+class RDoc::Markup::AttributeManager
+
+ NULL = "\000".freeze
+
+ ##
+ # We work by substituting non-printing characters in to the text. For now
+ # I'm assuming that I can substitute a character in the range 0..8 for a 7
+ # bit character without damaging the encoded string, but this might be
+ # optimistic
+
+ A_PROTECT = 004
+ PROTECT_ATTR = A_PROTECT.chr
+
+ ##
+ # This maps delimiters that occur around words (such as *bold* or +tt+)
+ # where the start and end delimiters and the same. This lets us optimize
+ # the regexp
+
+ MATCHING_WORD_PAIRS = {}
+
+ ##
+ # And this is used when the delimiters aren't the same. In this case the
+ # hash maps a pattern to the attribute character
+
+ WORD_PAIR_MAP = {}
+
+ ##
+ # This maps HTML tags to the corresponding attribute char
+
+ HTML_TAGS = {}
+
+ ##
+ # And this maps _special_ sequences to a name. A special sequence is
+ # something like a WikiWord
+
+ SPECIAL = {}
+
+ ##
+ # Return an attribute object with the given turn_on and turn_off bits set
+
+ def attribute(turn_on, turn_off)
+ RDoc::Markup::AttrChanger.new turn_on, turn_off
+ end
+
+ def change_attribute(current, new)
+ diff = current ^ new
+ attribute(new & diff, current & diff)
+ end
+
+ def changed_attribute_by_name(current_set, new_set)
+ current = new = 0
+ current_set.each do |name|
+ current |= RDoc::Markup::Attribute.bitmap_for(name)
+ end
+
+ new_set.each do |name|
+ new |= RDoc::Markup::Attribute.bitmap_for(name)
+ end
+
+ change_attribute(current, new)
+ end
+
+ def copy_string(start_pos, end_pos)
+ res = @str[start_pos...end_pos]
+ res.gsub!(/\000/, '')
+ res
+ end
+
+ ##
+ # Map attributes like <b>text</b>to the sequence
+ # \001\002<char>\001\003<char>, where <char> is a per-attribute specific
+ # character
+
+ def convert_attrs(str, attrs)
+ # first do matching ones
+ tags = MATCHING_WORD_PAIRS.keys.join("")
+
+ re = /(^|\W)([#{tags}])([#:\\]?[\w.\/-]+?\S?)\2(\W|$)/
+
+ 1 while str.gsub!(re) do
+ attr = MATCHING_WORD_PAIRS[$2]
+ attrs.set_attrs($`.length + $1.length + $2.length, $3.length, attr)
+ $1 + NULL * $2.length + $3 + NULL * $2.length + $4
+ end
+
+ # then non-matching
+ unless WORD_PAIR_MAP.empty? then
+ WORD_PAIR_MAP.each do |regexp, attr|
+ str.gsub!(regexp) {
+ attrs.set_attrs($`.length + $1.length, $2.length, attr)
+ NULL * $1.length + $2 + NULL * $3.length
+ }
+ end
+ end
+ end
+
+ def convert_html(str, attrs)
+ tags = HTML_TAGS.keys.join '|'
+
+ 1 while str.gsub!(/<(#{tags})>(.*?)<\/\1>/i) {
+ attr = HTML_TAGS[$1.downcase]
+ html_length = $1.length + 2
+ seq = NULL * html_length
+ attrs.set_attrs($`.length + html_length, $2.length, attr)
+ seq + $2 + seq + NULL
+ }
+ end
+
+ def convert_specials(str, attrs)
+ unless SPECIAL.empty?
+ SPECIAL.each do |regexp, attr|
+ str.scan(regexp) do
+ attrs.set_attrs($`.length, $&.length,
+ attr | RDoc::Markup::Attribute::SPECIAL)
+ end
+ end
+ end
+ end
+
+ ##
+ # A \ in front of a character that would normally be processed turns off
+ # processing. We do this by turning \< into <#{PROTECT}
+
+ PROTECTABLE = %w[<\\]
+
+ def mask_protected_sequences
+ protect_pattern = Regexp.new("\\\\([#{Regexp.escape(PROTECTABLE.join(''))}])")
+ @str.gsub!(protect_pattern, "\\1#{PROTECT_ATTR}")
+ end
+
+ def unmask_protected_sequences
+ @str.gsub!(/(.)#{PROTECT_ATTR}/, "\\1\000")
+ end
+
+ def initialize
+ add_word_pair("*", "*", :BOLD)
+ add_word_pair("_", "_", :EM)
+ add_word_pair("+", "+", :TT)
+
+ add_html("em", :EM)
+ add_html("i", :EM)
+ add_html("b", :BOLD)
+ add_html("tt", :TT)
+ add_html("code", :TT)
+ end
+
+ def add_word_pair(start, stop, name)
+ raise ArgumentError, "Word flags may not start with '<'" if
+ start[0,1] == '<'
+
+ bitmap = RDoc::Markup::Attribute.bitmap_for name
+
+ if start == stop then
+ MATCHING_WORD_PAIRS[start] = bitmap
+ else
+ pattern = /(#{Regexp.escape start})(\S+)(#{Regexp.escape stop})/
+ WORD_PAIR_MAP[pattern] = bitmap
+ end
+
+ PROTECTABLE << start[0,1]
+ PROTECTABLE.uniq!
+ end
+
+ def add_html(tag, name)
+ HTML_TAGS[tag.downcase] = RDoc::Markup::Attribute.bitmap_for name
+ end
+
+ def add_special(pattern, name)
+ SPECIAL[pattern] = RDoc::Markup::Attribute.bitmap_for name
+ end
+
+ def flow(str)
+ @str = str
+
+ mask_protected_sequences
+
+ @attrs = RDoc::Markup::AttrSpan.new @str.length
+
+ convert_attrs(@str, @attrs)
+ convert_html(@str, @attrs)
+ convert_specials(str, @attrs)
+
+ unmask_protected_sequences
+
+ return split_into_flow
+ end
+
+ def display_attributes
+ puts
+ puts @str.tr(NULL, "!")
+ bit = 1
+ 16.times do |bno|
+ line = ""
+ @str.length.times do |i|
+ if (@attrs[i] & bit) == 0
+ line << " "
+ else
+ if bno.zero?
+ line << "S"
+ else
+ line << ("%d" % (bno+1))
+ end
+ end
+ end
+ puts(line) unless line =~ /^ *$/
+ bit <<= 1
+ end
+ end
+
+ def split_into_flow
+ res = []
+ current_attr = 0
+ str = ""
+
+ str_len = @str.length
+
+ # skip leading invisible text
+ i = 0
+ i += 1 while i < str_len and @str[i].chr == "\0"
+ start_pos = i
+
+ # then scan the string, chunking it on attribute changes
+ while i < str_len
+ new_attr = @attrs[i]
+ if new_attr != current_attr
+ if i > start_pos
+ res << copy_string(start_pos, i)
+ start_pos = i
+ end
+
+ res << change_attribute(current_attr, new_attr)
+ current_attr = new_attr
+
+ if (current_attr & RDoc::Markup::Attribute::SPECIAL) != 0 then
+ i += 1 while
+ i < str_len and (@attrs[i] & RDoc::Markup::Attribute::SPECIAL) != 0
+
+ res << RDoc::Markup::Special.new(current_attr,
+ copy_string(start_pos, i))
+ start_pos = i
+ next
+ end
+ end
+
+ # move on, skipping any invisible characters
+ begin
+ i += 1
+ end while i < str_len and @str[i].chr == "\0"
+ end
+
+ # tidy up trailing text
+ if start_pos < str_len
+ res << copy_string(start_pos, str_len)
+ end
+
+ # and reset to all attributes off
+ res << change_attribute(current_attr, 0) if current_attr != 0
+
+ return res
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/formatter.rb b/ruby/lib/rdoc/markup/formatter.rb
new file mode 100644
index 0000000..14cbae5
--- /dev/null
+++ b/ruby/lib/rdoc/markup/formatter.rb
@@ -0,0 +1,14 @@
+require 'rdoc/markup'
+
+class RDoc::Markup::Formatter
+
+ def initialize
+ @markup = RDoc::Markup.new
+ end
+
+ def convert(content)
+ @markup.convert content, self
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/fragments.rb b/ruby/lib/rdoc/markup/fragments.rb
new file mode 100644
index 0000000..b7f9b60
--- /dev/null
+++ b/ruby/lib/rdoc/markup/fragments.rb
@@ -0,0 +1,337 @@
+require 'rdoc/markup'
+require 'rdoc/markup/lines'
+
+class RDoc::Markup
+
+ ##
+ # A Fragment is a chunk of text, subclassed as a paragraph, a list
+ # entry, or verbatim text.
+
+ class Fragment
+ attr_reader :level, :param, :txt
+ attr_accessor :type
+
+ ##
+ # This is a simple factory system that lets us associate fragement
+ # types (a string) with a subclass of fragment
+
+ TYPE_MAP = {}
+
+ def self.type_name(name)
+ TYPE_MAP[name] = self
+ end
+
+ def self.for(line)
+ klass = TYPE_MAP[line.type] ||
+ raise("Unknown line type: '#{line.type.inspect}:' '#{line.text}'")
+ return klass.new(line.level, line.param, line.flag, line.text)
+ end
+
+ def initialize(level, param, type, txt)
+ @level = level
+ @param = param
+ @type = type
+ @txt = ""
+ add_text(txt) if txt
+ end
+
+ def add_text(txt)
+ @txt << " " if @txt.length > 0
+ @txt << txt.tr_s("\n ", " ").strip
+ end
+
+ def to_s
+ "L#@level: #{self.class.name.split('::')[-1]}\n#@txt"
+ end
+
+ end
+
+ ##
+ # A paragraph is a fragment which gets wrapped to fit. We remove all
+ # newlines when we're created, and have them put back on output.
+
+ class Paragraph < Fragment
+ type_name :PARAGRAPH
+ end
+
+ class BlankLine < Paragraph
+ type_name :BLANK
+ end
+
+ class Heading < Paragraph
+ type_name :HEADING
+
+ def head_level
+ @param.to_i
+ end
+ end
+
+ ##
+ # A List is a fragment with some kind of label
+
+ class ListBase < Paragraph
+ LIST_TYPES = [
+ :BULLET,
+ :NUMBER,
+ :UPPERALPHA,
+ :LOWERALPHA,
+ :LABELED,
+ :NOTE,
+ ]
+ end
+
+ class ListItem < ListBase
+ type_name :LIST
+
+ def to_s
+ text = if [:NOTE, :LABELED].include? type then
+ "#{@param}: #{@txt}"
+ else
+ @txt
+ end
+
+ "L#@level: #{type} #{self.class.name.split('::')[-1]}\n#{text}"
+ end
+
+ end
+
+ class ListStart < ListBase
+ def initialize(level, param, type)
+ super(level, param, type, nil)
+ end
+ end
+
+ class ListEnd < ListBase
+ def initialize(level, type)
+ super(level, "", type, nil)
+ end
+ end
+
+ ##
+ # Verbatim code contains lines that don't get wrapped.
+
+ class Verbatim < Fragment
+ type_name :VERBATIM
+
+ def add_text(txt)
+ @txt << txt.chomp << "\n"
+ end
+
+ end
+
+ ##
+ # A horizontal rule
+
+ class Rule < Fragment
+ type_name :RULE
+ end
+
+ ##
+ # Collect groups of lines together. Each group will end up containing a flow
+ # of text.
+
+ class LineCollection
+
+ def initialize
+ @fragments = []
+ end
+
+ def add(fragment)
+ @fragments << fragment
+ end
+
+ def each(&b)
+ @fragments.each(&b)
+ end
+
+ def to_a # :nodoc:
+ @fragments.map {|fragment| fragment.to_s}
+ end
+
+ ##
+ # Factory for different fragment types
+
+ def fragment_for(*args)
+ Fragment.for(*args)
+ end
+
+ ##
+ # Tidy up at the end
+
+ def normalize
+ change_verbatim_blank_lines
+ add_list_start_and_ends
+ add_list_breaks
+ tidy_blank_lines
+ end
+
+ def to_s
+ @fragments.join("\n----\n")
+ end
+
+ def accept(am, visitor)
+ visitor.start_accepting
+
+ @fragments.each do |fragment|
+ case fragment
+ when Verbatim
+ visitor.accept_verbatim(am, fragment)
+ when Rule
+ visitor.accept_rule(am, fragment)
+ when ListStart
+ visitor.accept_list_start(am, fragment)
+ when ListEnd
+ visitor.accept_list_end(am, fragment)
+ when ListItem
+ visitor.accept_list_item(am, fragment)
+ when BlankLine
+ visitor.accept_blank_line(am, fragment)
+ when Heading
+ visitor.accept_heading(am, fragment)
+ when Paragraph
+ visitor.accept_paragraph(am, fragment)
+ end
+ end
+
+ visitor.end_accepting
+ end
+
+ private
+
+ # If you have:
+ #
+ # normal paragraph text.
+ #
+ # this is code
+ #
+ # and more code
+ #
+ # You'll end up with the fragments Paragraph, BlankLine, Verbatim,
+ # BlankLine, Verbatim, BlankLine, etc.
+ #
+ # The BlankLine in the middle of the verbatim chunk needs to be changed to
+ # a real verbatim newline, and the two verbatim blocks merged
+
+ def change_verbatim_blank_lines
+ frag_block = nil
+ blank_count = 0
+ @fragments.each_with_index do |frag, i|
+ if frag_block.nil?
+ frag_block = frag if Verbatim === frag
+ else
+ case frag
+ when Verbatim
+ blank_count.times { frag_block.add_text("\n") }
+ blank_count = 0
+ frag_block.add_text(frag.txt)
+ @fragments[i] = nil # remove out current fragment
+ when BlankLine
+ if frag_block
+ blank_count += 1
+ @fragments[i] = nil
+ end
+ else
+ frag_block = nil
+ blank_count = 0
+ end
+ end
+ end
+ @fragments.compact!
+ end
+
+ ##
+ # List nesting is implicit given the level of indentation. Make it
+ # explicit, just to make life a tad easier for the output processors
+
+ def add_list_start_and_ends
+ level = 0
+ res = []
+ type_stack = []
+
+ @fragments.each do |fragment|
+ # $stderr.puts "#{level} : #{fragment.class.name} : #{fragment.level}"
+ new_level = fragment.level
+ while (level < new_level)
+ level += 1
+ type = fragment.type
+ res << ListStart.new(level, fragment.param, type) if type
+ type_stack.push type
+ # $stderr.puts "Start: #{level}"
+ end
+
+ while level > new_level
+ type = type_stack.pop
+ res << ListEnd.new(level, type) if type
+ level -= 1
+ # $stderr.puts "End: #{level}, #{type}"
+ end
+
+ res << fragment
+ level = fragment.level
+ end
+ level.downto(1) do |i|
+ type = type_stack.pop
+ res << ListEnd.new(i, type) if type
+ end
+
+ @fragments = res
+ end
+
+ ##
+ # Inserts start/ends between list entries at the same level that have
+ # different element types
+
+ def add_list_breaks
+ res = @fragments
+
+ @fragments = []
+ list_stack = []
+
+ res.each do |fragment|
+ case fragment
+ when ListStart
+ list_stack.push fragment
+ when ListEnd
+ start = list_stack.pop
+ fragment.type = start.type
+ when ListItem
+ l = list_stack.last
+ if fragment.type != l.type
+ @fragments << ListEnd.new(l.level, l.type)
+ start = ListStart.new(l.level, fragment.param, fragment.type)
+ @fragments << start
+ list_stack.pop
+ list_stack.push start
+ end
+ else
+ ;
+ end
+ @fragments << fragment
+ end
+ end
+
+ ##
+ # Tidy up the blank lines:
+ # * change Blank/ListEnd into ListEnd/Blank
+ # * remove blank lines at the front
+
+ def tidy_blank_lines
+ (@fragments.size - 1).times do |i|
+ if BlankLine === @fragments[i] and ListEnd === @fragments[i+1] then
+ @fragments[i], @fragments[i+1] = @fragments[i+1], @fragments[i]
+ end
+ end
+
+ # remove leading blanks
+ @fragments.each_with_index do |f, i|
+ break unless f.kind_of? BlankLine
+ @fragments[i] = nil
+ end
+
+ @fragments.compact!
+ end
+
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/inline.rb b/ruby/lib/rdoc/markup/inline.rb
new file mode 100644
index 0000000..46c9b58
--- /dev/null
+++ b/ruby/lib/rdoc/markup/inline.rb
@@ -0,0 +1,101 @@
+require 'rdoc/markup'
+
+class RDoc::Markup
+
+ ##
+ # We manage a set of attributes. Each attribute has a symbol name and a bit
+ # value.
+
+ class Attribute
+ SPECIAL = 1
+
+ @@name_to_bitmap = { :_SPECIAL_ => SPECIAL }
+ @@next_bitmap = 2
+
+ def self.bitmap_for(name)
+ bitmap = @@name_to_bitmap[name]
+ unless bitmap then
+ bitmap = @@next_bitmap
+ @@next_bitmap <<= 1
+ @@name_to_bitmap[name] = bitmap
+ end
+ bitmap
+ end
+
+ def self.as_string(bitmap)
+ return "none" if bitmap.zero?
+ res = []
+ @@name_to_bitmap.each do |name, bit|
+ res << name if (bitmap & bit) != 0
+ end
+ res.join(",")
+ end
+
+ def self.each_name_of(bitmap)
+ @@name_to_bitmap.each do |name, bit|
+ next if bit == SPECIAL
+ yield name.to_s if (bitmap & bit) != 0
+ end
+ end
+ end
+
+ AttrChanger = Struct.new(:turn_on, :turn_off)
+
+ ##
+ # An AttrChanger records a change in attributes. It contains a bitmap of the
+ # attributes to turn on, and a bitmap of those to turn off.
+
+ class AttrChanger
+ def to_s
+ "Attr: +#{Attribute.as_string(turn_on)}/-#{Attribute.as_string(turn_on)}"
+ end
+ end
+
+ ##
+ # An array of attributes which parallels the characters in a string.
+
+ class AttrSpan
+ def initialize(length)
+ @attrs = Array.new(length, 0)
+ end
+
+ def set_attrs(start, length, bits)
+ for i in start ... (start+length)
+ @attrs[i] |= bits
+ end
+ end
+
+ def [](n)
+ @attrs[n]
+ end
+ end
+
+ ##
+ # Hold details of a special sequence
+
+ class Special
+ attr_reader :type
+ attr_accessor :text
+
+ def initialize(type, text)
+ @type, @text = type, text
+ end
+
+ def ==(o)
+ self.text == o.text && self.type == o.type
+ end
+
+ def inspect
+ "#<RDoc::Markup::Special:0x%x @type=%p, name=%p @text=%p>" % [
+ object_id, @type, RDoc::Markup::Attribute.as_string(type), text.dump]
+ end
+
+ def to_s
+ "Special: type=#{type}, name=#{RDoc::Markup::Attribute.as_string type}, text=#{text.dump}"
+ end
+
+ end
+
+end
+
+require 'rdoc/markup/attribute_manager'
diff --git a/ruby/lib/rdoc/markup/lines.rb b/ruby/lib/rdoc/markup/lines.rb
new file mode 100644
index 0000000..0694921
--- /dev/null
+++ b/ruby/lib/rdoc/markup/lines.rb
@@ -0,0 +1,152 @@
+class RDoc::Markup
+
+ ##
+ # We store the lines we're working on as objects of class Line. These
+ # contain the text of the line, along with a flag indicating the line type,
+ # and an indentation level.
+
+ class Line
+ INFINITY = 9999
+
+ LINE_TYPES = [
+ :BLANK,
+ :HEADING,
+ :LIST,
+ :PARAGRAPH,
+ :RULE,
+ :VERBATIM,
+ ]
+
+ # line type
+ attr_accessor :type
+
+ # The indentation nesting level
+ attr_accessor :level
+
+ # The contents
+ attr_accessor :text
+
+ # A prefix or parameter. For LIST lines, this is
+ # the text that introduced the list item (the label)
+ attr_accessor :param
+
+ # A flag. For list lines, this is the type of the list
+ attr_accessor :flag
+
+ # the number of leading spaces
+ attr_accessor :leading_spaces
+
+ # true if this line has been deleted from the list of lines
+ attr_accessor :deleted
+
+ def initialize(text)
+ @text = text.dup
+ @deleted = false
+
+ # expand tabs
+ 1 while @text.gsub!(/\t+/) { ' ' * (8*$&.length - $`.length % 8)} && $~ #`
+
+ # Strip trailing whitespace
+ @text.sub!(/\s+$/, '')
+
+ # and look for leading whitespace
+ if @text.length > 0
+ @text =~ /^(\s*)/
+ @leading_spaces = $1.length
+ else
+ @leading_spaces = INFINITY
+ end
+ end
+
+ # Return true if this line is blank
+ def blank?
+ @text.empty?
+ end
+
+ # stamp a line with a type, a level, a prefix, and a flag
+ def stamp(type, level, param="", flag=nil)
+ @type, @level, @param, @flag = type, level, param, flag
+ end
+
+ ##
+ # Strip off the leading margin
+
+ def strip_leading(size)
+ if @text.size > size
+ @text[0,size] = ""
+ else
+ @text = ""
+ end
+ end
+
+ def to_s
+ "#@type#@level: #@text"
+ end
+ end
+
+ ##
+ # A container for all the lines.
+
+ class Lines
+
+ include Enumerable
+
+ attr_reader :lines # :nodoc:
+
+ def initialize(lines)
+ @lines = lines
+ rewind
+ end
+
+ def empty?
+ @lines.size.zero?
+ end
+
+ def each
+ @lines.each do |line|
+ yield line unless line.deleted
+ end
+ end
+
+# def [](index)
+# @lines[index]
+# end
+
+ def rewind
+ @nextline = 0
+ end
+
+ def next
+ begin
+ res = @lines[@nextline]
+ @nextline += 1 if @nextline < @lines.size
+ end while res and res.deleted and @nextline < @lines.size
+ res
+ end
+
+ def unget
+ @nextline -= 1
+ end
+
+ def delete(a_line)
+ a_line.deleted = true
+ end
+
+ def normalize
+ margin = @lines.collect{|l| l.leading_spaces}.min
+ margin = 0 if margin == :INFINITY
+ @lines.each {|line| line.strip_leading(margin) } if margin > 0
+ end
+
+ def as_text
+ @lines.map {|l| l.text}.join("\n")
+ end
+
+ def line_types
+ @lines.map {|l| l.type }
+ end
+
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/preprocess.rb b/ruby/lib/rdoc/markup/preprocess.rb
new file mode 100644
index 0000000..00dd4be
--- /dev/null
+++ b/ruby/lib/rdoc/markup/preprocess.rb
@@ -0,0 +1,75 @@
+require 'rdoc/markup'
+
+##
+# Handle common directives that can occur in a block of text:
+#
+# : include : filename
+
+class RDoc::Markup::PreProcess
+
+ def initialize(input_file_name, include_path)
+ @input_file_name = input_file_name
+ @include_path = include_path
+ end
+
+ ##
+ # Look for common options in a chunk of text. Options that we don't handle
+ # are yielded to the caller.
+
+ def handle(text)
+ text.gsub!(/^([ \t]*#?[ \t]*):(\w+):([ \t]*)(.+)?\n/) do
+ next $& if $3.empty? and $4 and $4[0, 1] == ':'
+
+ prefix = $1
+ directive = $2.downcase
+ param = $4
+
+ case directive
+ when 'include' then
+ filename = param.split[0]
+ include_file filename, prefix
+
+ else
+ result = yield directive, param
+ result = "#{prefix}:#{directive}: #{param}\n" unless result
+ result
+ end
+ end
+ end
+
+ private
+
+ ##
+ # Include a file, indenting it correctly.
+
+ def include_file(name, indent)
+ if full_name = find_include_file(name) then
+ content = File.open(full_name) {|f| f.read}
+ # strip leading '#'s, but only if all lines start with them
+ if content =~ /^[^#]/
+ content.gsub(/^/, indent)
+ else
+ content.gsub(/^#?/, indent)
+ end
+ else
+ $stderr.puts "Couldn't find file to include: '#{name}'"
+ ''
+ end
+ end
+
+ ##
+ # Look for the given file in the directory containing the current file,
+ # and then in each of the directories specified in the RDOC_INCLUDE path
+
+ def find_include_file(name)
+ to_search = [ File.dirname(@input_file_name) ].concat @include_path
+ to_search.each do |dir|
+ full_name = File.join(dir, name)
+ stat = File.stat(full_name) rescue next
+ return full_name if stat.readable?
+ end
+ nil
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/to_flow.rb b/ruby/lib/rdoc/markup/to_flow.rb
new file mode 100644
index 0000000..3d87b3e
--- /dev/null
+++ b/ruby/lib/rdoc/markup/to_flow.rb
@@ -0,0 +1,185 @@
+require 'rdoc/markup/formatter'
+require 'rdoc/markup/fragments'
+require 'rdoc/markup/inline'
+require 'cgi'
+
+class RDoc::Markup
+
+ module Flow
+ P = Struct.new(:body)
+ VERB = Struct.new(:body)
+ RULE = Struct.new(:width)
+ class LIST
+ attr_reader :type, :contents
+ def initialize(type)
+ @type = type
+ @contents = []
+ end
+ def <<(stuff)
+ @contents << stuff
+ end
+ end
+ LI = Struct.new(:label, :body)
+ H = Struct.new(:level, :text)
+ end
+
+ class ToFlow < RDoc::Markup::Formatter
+ LIST_TYPE_TO_HTML = {
+ :BULLET => [ "<ul>", "</ul>" ],
+ :NUMBER => [ "<ol>", "</ol>" ],
+ :UPPERALPHA => [ "<ol>", "</ol>" ],
+ :LOWERALPHA => [ "<ol>", "</ol>" ],
+ :LABELED => [ "<dl>", "</dl>" ],
+ :NOTE => [ "<table>", "</table>" ],
+ }
+
+ InlineTag = Struct.new(:bit, :on, :off)
+
+ def initialize
+ super
+
+ init_tags
+ end
+
+ ##
+ # Set up the standard mapping of attributes to HTML tags
+
+ def init_tags
+ @attr_tags = [
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:BOLD), "<b>", "</b>"),
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:TT), "<tt>", "</tt>"),
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:EM), "<em>", "</em>"),
+ ]
+ end
+
+ ##
+ # Add a new set of HTML tags for an attribute. We allow separate start and
+ # end tags for flexibility
+
+ def add_tag(name, start, stop)
+ @attr_tags << InlineTag.new(RDoc::Markup::Attribute.bitmap_for(name), start, stop)
+ end
+
+ ##
+ # Given an HTML tag, decorate it with class information and the like if
+ # required. This is a no-op in the base class, but is overridden in HTML
+ # output classes that implement style sheets
+
+ def annotate(tag)
+ tag
+ end
+
+ ##
+ # Here's the client side of the visitor pattern
+
+ def start_accepting
+ @res = []
+ @list_stack = []
+ end
+
+ def end_accepting
+ @res
+ end
+
+ def accept_paragraph(am, fragment)
+ @res << Flow::P.new((convert_flow(am.flow(fragment.txt))))
+ end
+
+ def accept_verbatim(am, fragment)
+ @res << Flow::VERB.new((convert_flow(am.flow(fragment.txt))))
+ end
+
+ def accept_rule(am, fragment)
+ size = fragment.param
+ size = 10 if size > 10
+ @res << Flow::RULE.new(size)
+ end
+
+ def accept_list_start(am, fragment)
+ @list_stack.push(@res)
+ list = Flow::LIST.new(fragment.type)
+ @res << list
+ @res = list
+ end
+
+ def accept_list_end(am, fragment)
+ @res = @list_stack.pop
+ end
+
+ def accept_list_item(am, fragment)
+ @res << Flow::LI.new(fragment.param, convert_flow(am.flow(fragment.txt)))
+ end
+
+ def accept_blank_line(am, fragment)
+ # @res << annotate("<p />") << "\n"
+ end
+
+ def accept_heading(am, fragment)
+ @res << Flow::H.new(fragment.head_level, convert_flow(am.flow(fragment.txt)))
+ end
+
+ private
+
+ def on_tags(res, item)
+ attr_mask = item.turn_on
+ return if attr_mask.zero?
+
+ @attr_tags.each do |tag|
+ if attr_mask & tag.bit != 0
+ res << annotate(tag.on)
+ end
+ end
+ end
+
+ def off_tags(res, item)
+ attr_mask = item.turn_off
+ return if attr_mask.zero?
+
+ @attr_tags.reverse_each do |tag|
+ if attr_mask & tag.bit != 0
+ res << annotate(tag.off)
+ end
+ end
+ end
+
+ def convert_flow(flow)
+ res = ""
+ flow.each do |item|
+ case item
+ when String
+ res << convert_string(item)
+ when AttrChanger
+ off_tags(res, item)
+ on_tags(res, item)
+ when Special
+ res << convert_special(item)
+ else
+ raise "Unknown flow element: #{item.inspect}"
+ end
+ end
+ res
+ end
+
+ def convert_string(item)
+ CGI.escapeHTML(item)
+ end
+
+ def convert_special(special)
+ handled = false
+ Attribute.each_name_of(special.type) do |name|
+ method_name = "handle_special_#{name}"
+ if self.respond_to? method_name
+ special.text = send(method_name, special)
+ handled = true
+ end
+ end
+
+ raise "Unhandled special: #{special}" unless handled
+
+ special.text
+ end
+
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/to_html.rb b/ruby/lib/rdoc/markup/to_html.rb
new file mode 100644
index 0000000..dce7a69
--- /dev/null
+++ b/ruby/lib/rdoc/markup/to_html.rb
@@ -0,0 +1,403 @@
+require 'rdoc/markup/formatter'
+require 'rdoc/markup/fragments'
+require 'rdoc/markup/inline'
+
+require 'cgi'
+
+class RDoc::Markup::ToHtml < RDoc::Markup::Formatter
+
+ LIST_TYPE_TO_HTML = {
+ :BULLET => %w[<ul> </ul>],
+ :NUMBER => %w[<ol> </ol>],
+ :UPPERALPHA => %w[<ol> </ol>],
+ :LOWERALPHA => %w[<ol> </ol>],
+ :LABELED => %w[<dl> </dl>],
+ :NOTE => %w[<table> </table>],
+ }
+
+ InlineTag = Struct.new(:bit, :on, :off)
+
+ def initialize
+ super
+
+ # @in_tt - tt nested levels count
+ # @tt_bit - cache
+ @in_tt = 0
+ @tt_bit = RDoc::Markup::Attribute.bitmap_for :TT
+
+ # external hyperlinks
+ @markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK)
+
+ # and links of the form <text>[<url>]
+ @markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\.\S+?\])/, :TIDYLINK)
+
+ init_tags
+ end
+
+ ##
+ # Converts a target url to one that is relative to a given path
+
+ def self.gen_relative_url(path, target)
+ from = File.dirname path
+ to, to_file = File.split target
+
+ from = from.split "/"
+ to = to.split "/"
+
+ while from.size > 0 and to.size > 0 and from[0] == to[0] do
+ from.shift
+ to.shift
+ end
+
+ from.fill ".."
+ from.concat to
+ from << to_file
+ File.join(*from)
+ end
+
+ ##
+ # Generate a hyperlink for url, labeled with text. Handle the
+ # special cases for img: and link: described under handle_special_HYPERLINK
+
+ def gen_url(url, text)
+ if url =~ /([A-Za-z]+):(.*)/ then
+ type = $1
+ path = $2
+ else
+ type = "http"
+ path = url
+ url = "http://#{url}"
+ end
+
+ if type == "link" then
+ url = if path[0, 1] == '#' then # is this meaningful?
+ path
+ else
+ self.class.gen_relative_url @from_path, path
+ end
+ end
+
+ if (type == "http" or type == "link") and
+ url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
+ "<img src=\"#{url}\" />"
+ else
+ "<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>"
+ end
+ end
+
+ ##
+ # And we're invoked with a potential external hyperlink mailto:
+ # just gets inserted. http: links are checked to see if they
+ # reference an image. If so, that image gets inserted using an
+ # <img> tag. Otherwise a conventional <a href> is used. We also
+ # support a special type of hyperlink, link:, which is a reference
+ # to a local file whose path is relative to the --op directory.
+
+ def handle_special_HYPERLINK(special)
+ url = special.text
+ gen_url url, url
+ end
+
+ ##
+ # Here's a hypedlink where the label is different to the URL
+ # <label>[url] or {long label}[url]
+
+ def handle_special_TIDYLINK(special)
+ text = special.text
+
+ return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/
+
+ label = $1
+ url = $2
+ gen_url url, label
+ end
+
+ ##
+ # are we currently inside <tt> tags?
+
+ def in_tt?
+ @in_tt > 0
+ end
+
+ ##
+ # is +tag+ a <tt> tag?
+
+ def tt?(tag)
+ tag.bit == @tt_bit
+ end
+
+ ##
+ # Set up the standard mapping of attributes to HTML tags
+
+ def init_tags
+ @attr_tags = [
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:BOLD), "<b>", "</b>"),
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:TT), "<tt>", "</tt>"),
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:EM), "<em>", "</em>"),
+ ]
+ end
+
+ ##
+ # Add a new set of HTML tags for an attribute. We allow separate start and
+ # end tags for flexibility.
+
+ def add_tag(name, start, stop)
+ @attr_tags << InlineTag.new(RDoc::Markup::Attribute.bitmap_for(name), start, stop)
+ end
+
+ ##
+ # Given an HTML tag, decorate it with class information and the like if
+ # required. This is a no-op in the base class, but is overridden in HTML
+ # output classes that implement style sheets.
+
+ def annotate(tag)
+ tag
+ end
+
+ ##
+ # Here's the client side of the visitor pattern
+
+ def start_accepting
+ @res = ""
+ @in_list_entry = []
+ end
+
+ def end_accepting
+ @res
+ end
+
+ def accept_paragraph(am, fragment)
+ @res << annotate("<p>") + "\n"
+ @res << wrap(convert_flow(am.flow(fragment.txt)))
+ @res << annotate("</p>") + "\n"
+ end
+
+ def accept_verbatim(am, fragment)
+ @res << annotate("<pre>") + "\n"
+ @res << CGI.escapeHTML(fragment.txt)
+ @res << annotate("</pre>") << "\n"
+ end
+
+ def accept_rule(am, fragment)
+ size = fragment.param
+ size = 10 if size > 10
+ @res << "<hr size=\"#{size}\"></hr>"
+ end
+
+ def accept_list_start(am, fragment)
+ @res << html_list_name(fragment.type, true) << "\n"
+ @in_list_entry.push false
+ end
+
+ def accept_list_end(am, fragment)
+ if tag = @in_list_entry.pop
+ @res << annotate(tag) << "\n"
+ end
+ @res << html_list_name(fragment.type, false) << "\n"
+ end
+
+ def accept_list_item(am, fragment)
+ if tag = @in_list_entry.last
+ @res << annotate(tag) << "\n"
+ end
+
+ @res << list_item_start(am, fragment)
+
+ @res << wrap(convert_flow(am.flow(fragment.txt))) << "\n"
+
+ @in_list_entry[-1] = list_end_for(fragment.type)
+ end
+
+ def accept_blank_line(am, fragment)
+ # @res << annotate("<p />") << "\n"
+ end
+
+ def accept_heading(am, fragment)
+ @res << convert_heading(fragment.head_level, am.flow(fragment.txt))
+ end
+
+ ##
+ # This is a higher speed (if messier) version of wrap
+
+ def wrap(txt, line_len = 76)
+ res = ""
+ sp = 0
+ ep = txt.length
+ while sp < ep
+ # scan back for a space
+ p = sp + line_len - 1
+ if p >= ep
+ p = ep
+ else
+ while p > sp and txt[p] != ?\s
+ p -= 1
+ end
+ if p <= sp
+ p = sp + line_len
+ while p < ep and txt[p] != ?\s
+ p += 1
+ end
+ end
+ end
+ res << txt[sp...p] << "\n"
+ sp = p
+ sp += 1 while sp < ep and txt[sp] == ?\s
+ end
+ res
+ end
+
+ private
+
+ def on_tags(res, item)
+ attr_mask = item.turn_on
+ return if attr_mask.zero?
+
+ @attr_tags.each do |tag|
+ if attr_mask & tag.bit != 0
+ res << annotate(tag.on)
+ @in_tt += 1 if tt?(tag)
+ end
+ end
+ end
+
+ def off_tags(res, item)
+ attr_mask = item.turn_off
+ return if attr_mask.zero?
+
+ @attr_tags.reverse_each do |tag|
+ if attr_mask & tag.bit != 0
+ @in_tt -= 1 if tt?(tag)
+ res << annotate(tag.off)
+ end
+ end
+ end
+
+ def convert_flow(flow)
+ res = ""
+
+ flow.each do |item|
+ case item
+ when String
+ res << convert_string(item)
+ when RDoc::Markup::AttrChanger
+ off_tags(res, item)
+ on_tags(res, item)
+ when RDoc::Markup::Special
+ res << convert_special(item)
+ else
+ raise "Unknown flow element: #{item.inspect}"
+ end
+ end
+
+ res
+ end
+
+ def convert_string(item)
+ in_tt? ? convert_string_simple(item) : convert_string_fancy(item)
+ end
+
+ def convert_string_simple(item)
+ CGI.escapeHTML item
+ end
+
+ ##
+ # some of these patterns are taken from SmartyPants...
+
+ def convert_string_fancy(item)
+ # convert ampersand before doing anything else
+ item.gsub(/&/, '&amp;').
+
+ # convert -- to em-dash, (-- to en-dash)
+ gsub(/---?/, '&#8212;'). #gsub(/--/, '&#8211;').
+
+ # convert ... to elipsis (and make sure .... becomes .<elipsis>)
+ gsub(/\.\.\.\./, '.&#8230;').gsub(/\.\.\./, '&#8230;').
+
+ # convert single closing quote
+ gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1&#8217;'). # }
+ gsub(%r{\'(?=\W|s\b)}, '&#8217;').
+
+ # convert single opening quote
+ gsub(/'/, '&#8216;').
+
+ # convert double closing quote
+ gsub(%r{([^ \t\r\n\[\{\(])\"(?=\W)}, '\1&#8221;'). # }
+
+ # convert double opening quote
+ gsub(/"/, '&#8220;').
+
+ # convert copyright
+ gsub(/\(c\)/, '&#169;').
+
+ # convert registered trademark
+ gsub(/\(r\)/, '&#174;')
+ end
+
+ def convert_special(special)
+ handled = false
+ RDoc::Markup::Attribute.each_name_of(special.type) do |name|
+ method_name = "handle_special_#{name}"
+ if self.respond_to? method_name
+ special.text = send(method_name, special)
+ handled = true
+ end
+ end
+ raise "Unhandled special: #{special}" unless handled
+ special.text
+ end
+
+ def convert_heading(level, flow)
+ res =
+ annotate("<h#{level}>") +
+ convert_flow(flow) +
+ annotate("</h#{level}>\n")
+ end
+
+ def html_list_name(list_type, is_open_tag)
+ tags = LIST_TYPE_TO_HTML[list_type] || raise("Invalid list type: #{list_type.inspect}")
+ annotate(tags[ is_open_tag ? 0 : 1])
+ end
+
+ def list_item_start(am, fragment)
+ case fragment.type
+ when :BULLET, :NUMBER then
+ annotate("<li>")
+
+ when :UPPERALPHA then
+ annotate("<li type=\"A\">")
+
+ when :LOWERALPHA then
+ annotate("<li type=\"a\">")
+
+ when :LABELED then
+ annotate("<dt>") +
+ convert_flow(am.flow(fragment.param)) +
+ annotate("</dt>") +
+ annotate("<dd>")
+
+ when :NOTE then
+ annotate("<tr>") +
+ annotate("<td valign=\"top\">") +
+ convert_flow(am.flow(fragment.param)) +
+ annotate("</td>") +
+ annotate("<td>")
+ else
+ raise "Invalid list type"
+ end
+ end
+
+ def list_end_for(fragment_type)
+ case fragment_type
+ when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA then
+ "</li>"
+ when :LABELED then
+ "</dd>"
+ when :NOTE then
+ "</td></tr>"
+ else
+ raise "Invalid list type"
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/to_html_crossref.rb b/ruby/lib/rdoc/markup/to_html_crossref.rb
new file mode 100644
index 0000000..dc64b30
--- /dev/null
+++ b/ruby/lib/rdoc/markup/to_html_crossref.rb
@@ -0,0 +1,148 @@
+require 'rdoc/markup/to_html'
+
+##
+# Subclass of the RDoc::Markup::ToHtml class that supports looking up words in
+# the AllReferences list. Those that are found (like AllReferences in this
+# comment) will be hyperlinked
+
+class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
+
+ attr_accessor :context
+
+ # Regular expressions to match class and method references.
+ #
+ # 1.) There can be a '\' in front of text to suppress
+ # any cross-references (note, however, that the single '\'
+ # is written as '\\\\' in order to escape it twice, once
+ # in the Ruby String literal and once in the regexp).
+ # 2.) There can be a '::' in front of class names to reference
+ # from the top-level namespace.
+ # 3.) The method can be followed by parenthesis,
+ # which may or may not have things inside (this
+ # apparently is allowed for Fortran 95, but I also think that this
+ # is a good idea for Ruby, as it is very reasonable to want to
+ # reference a call with arguments).
+ #
+ # NOTE: In order to support Fortran 95 properly, the [A-Z] below
+ # should be changed to [A-Za-z]. This slows down rdoc significantly,
+ # however, and the Fortran 95 support is broken in any case due to
+ # the return in handle_special_CROSSREF if the token consists
+ # entirely of lowercase letters.
+ #
+ # The markup/cross-referencing engine needs a rewrite for
+ # Fortran 95 to be supported properly.
+ CLASS_REGEXP_STR = '\\\\?((?:\:{2})?[A-Z]\w*(?:\:\:\w+)*)'
+ METHOD_REGEXP_STR = '(\w+[!?=]?)(?:\([\.\w+\*\/\+\-\=\<\>]*\))?'
+
+ # Regular expressions matching text that should potentially have
+ # cross-reference links generated are passed to add_special.
+ # Note that these expressions are meant to pick up text for which
+ # cross-references have been suppressed, since the suppression
+ # characters are removed by the code that is triggered.
+ CROSSREF_REGEXP = /(
+ # A::B::C.meth
+ #{CLASS_REGEXP_STR}[\.\#]#{METHOD_REGEXP_STR}
+
+ # Stand-alone method (proceeded by a #)
+ | \\?\##{METHOD_REGEXP_STR}
+
+ # A::B::C
+ # The stuff after CLASS_REGEXP_STR is a
+ # nasty hack. CLASS_REGEXP_STR unfortunately matches
+ # words like dog and cat (these are legal "class"
+ # names in Fortran 95). When a word is flagged as a
+ # potential cross-reference, limitations in the markup
+ # engine suppress other processing, such as typesetting.
+ # This is particularly noticeable for contractions.
+ # In order that words like "can't" not
+ # be flagged as potential cross-references, only
+ # flag potential class cross-references if the character
+ # after the cross-referece is a space or sentence
+ # punctuation.
+ | #{CLASS_REGEXP_STR}(?=[\s\)\.\?\!\,\;]|\z)
+
+ # Things that look like filenames
+ # The key thing is that there must be at least
+ # one special character (period, slash, or
+ # underscore).
+ | [\/\w]+[_\/\.][\w\/\.]+
+
+ # Things that have markup suppressed
+ | \\[^\s]
+ )/x
+
+ ##
+ # We need to record the html path of our caller so we can generate
+ # correct relative paths for any hyperlinks that we find
+
+ def initialize(from_path, context, show_hash)
+ raise ArgumentError, 'from_path cannot be nil' if from_path.nil?
+ super()
+
+ @markup.add_special(CROSSREF_REGEXP, :CROSSREF)
+
+ @from_path = from_path
+ @context = context
+ @show_hash = show_hash
+
+ @seen = {}
+ end
+
+ ##
+ # We're invoked when any text matches the CROSSREF pattern
+ # (defined in MarkUp). If we fine the corresponding reference,
+ # generate a hyperlink. If the name we're looking for contains
+ # no punctuation, we look for it up the module/class chain. For
+ # example, HyperlinkHtml is found, even without the Generator::
+ # prefix, because we look for it in module Generator first.
+
+ def handle_special_CROSSREF(special)
+ name = special.text
+
+ # This ensures that words entirely consisting of lowercase letters will
+ # not have cross-references generated (to suppress lots of
+ # erroneous cross-references to "new" in text, for instance)
+ return name if name =~ /\A[a-z]*\z/
+
+ return @seen[name] if @seen.include? name
+
+ if name[0, 1] == '#' then
+ lookup = name[1..-1]
+ name = lookup unless @show_hash
+ else
+ lookup = name
+ end
+
+
+ # Find class, module, or method in class or module.
+ #
+ # Do not, however, use an if/elsif/else chain to do so. Instead, test
+ # each possible pattern until one matches. The reason for this is that a
+ # string like "YAML.txt" could be the txt() class method of class YAML (in
+ # which case it would match the first pattern, which splits the string
+ # into container and method components and looks up both) or a filename
+ # (in which case it would match the last pattern, which just checks
+ # whether the string as a whole is a known symbol).
+
+ if /#{CLASS_REGEXP_STR}[\.\#]#{METHOD_REGEXP_STR}/ =~ lookup then
+ container = $1
+ method = $2
+ ref = @context.find_symbol container, method
+ end
+
+ ref = @context.find_symbol lookup unless ref
+
+ out = if lookup =~ /^\\/ then
+ $'
+ elsif ref and ref.document_self then
+ "<a href=\"#{ref.as_href(@from_path)}\">#{name}</a>"
+ else
+ name
+ end
+
+ @seen[name] = out
+
+ out
+ end
+
+end
diff --git a/ruby/lib/rdoc/markup/to_latex.rb b/ruby/lib/rdoc/markup/to_latex.rb
new file mode 100644
index 0000000..bbf958f
--- /dev/null
+++ b/ruby/lib/rdoc/markup/to_latex.rb
@@ -0,0 +1,328 @@
+require 'rdoc/markup/formatter'
+require 'rdoc/markup/fragments'
+require 'rdoc/markup/inline'
+
+require 'cgi'
+
+##
+# Convert SimpleMarkup to basic LaTeX report format.
+
+class RDoc::Markup::ToLaTeX < RDoc::Markup::Formatter
+
+ BS = "\020" # \
+ OB = "\021" # {
+ CB = "\022" # }
+ DL = "\023" # Dollar
+
+ BACKSLASH = "#{BS}symbol#{OB}92#{CB}"
+ HAT = "#{BS}symbol#{OB}94#{CB}"
+ BACKQUOTE = "#{BS}symbol#{OB}0#{CB}"
+ TILDE = "#{DL}#{BS}sim#{DL}"
+ LESSTHAN = "#{DL}<#{DL}"
+ GREATERTHAN = "#{DL}>#{DL}"
+
+ def self.l(str)
+ str.tr('\\', BS).tr('{', OB).tr('}', CB).tr('$', DL)
+ end
+
+ def l(arg)
+ RDoc::Markup::ToLaTeX.l(arg)
+ end
+
+ LIST_TYPE_TO_LATEX = {
+ :BULLET => [ l("\\begin{itemize}"), l("\\end{itemize}") ],
+ :NUMBER => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\arabic" ],
+ :UPPERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\Alph" ],
+ :LOWERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\alph" ],
+ :LABELED => [ l("\\begin{description}"), l("\\end{description}") ],
+ :NOTE => [
+ l("\\begin{tabularx}{\\linewidth}{@{} l X @{}}"),
+ l("\\end{tabularx}") ],
+ }
+
+ InlineTag = Struct.new(:bit, :on, :off)
+
+ def initialize
+ init_tags
+ @list_depth = 0
+ @prev_list_types = []
+ end
+
+ ##
+ # Set up the standard mapping of attributes to LaTeX
+
+ def init_tags
+ @attr_tags = [
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:BOLD), l("\\textbf{"), l("}")),
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:TT), l("\\texttt{"), l("}")),
+ InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:EM), l("\\emph{"), l("}")),
+ ]
+ end
+
+ ##
+ # Escape a LaTeX string
+
+ def escape(str)
+ $stderr.print "FE: ", str if $DEBUG_RDOC
+ s = str.
+ sub(/\s+$/, '').
+ gsub(/([_\${}&%#])/, "#{BS}\\1").
+ gsub(/\\/, BACKSLASH).
+ gsub(/\^/, HAT).
+ gsub(/~/, TILDE).
+ gsub(/</, LESSTHAN).
+ gsub(/>/, GREATERTHAN).
+ gsub(/,,/, ",{},").
+ gsub(/\`/, BACKQUOTE)
+ $stderr.print "-> ", s, "\n" if $DEBUG_RDOC
+ s
+ end
+
+ ##
+ # Add a new set of LaTeX tags for an attribute. We allow
+ # separate start and end tags for flexibility
+
+ def add_tag(name, start, stop)
+ @attr_tags << InlineTag.new(RDoc::Markup::Attribute.bitmap_for(name), start, stop)
+ end
+
+ ##
+ # Here's the client side of the visitor pattern
+
+ def start_accepting
+ @res = ""
+ @in_list_entry = []
+ end
+
+ def end_accepting
+ @res.tr(BS, '\\').tr(OB, '{').tr(CB, '}').tr(DL, '$')
+ end
+
+ def accept_paragraph(am, fragment)
+ @res << wrap(convert_flow(am.flow(fragment.txt)))
+ @res << "\n"
+ end
+
+ def accept_verbatim(am, fragment)
+ @res << "\n\\begin{code}\n"
+ @res << fragment.txt.sub(/[\n\s]+\Z/, '')
+ @res << "\n\\end{code}\n\n"
+ end
+
+ def accept_rule(am, fragment)
+ size = fragment.param
+ size = 10 if size > 10
+ @res << "\n\n\\rule{\\linewidth}{#{size}pt}\n\n"
+ end
+
+ def accept_list_start(am, fragment)
+ @res << list_name(fragment.type, true) << "\n"
+ @in_list_entry.push false
+ end
+
+ def accept_list_end(am, fragment)
+ if tag = @in_list_entry.pop
+ @res << tag << "\n"
+ end
+ @res << list_name(fragment.type, false) << "\n"
+ end
+
+ def accept_list_item(am, fragment)
+ if tag = @in_list_entry.last
+ @res << tag << "\n"
+ end
+ @res << list_item_start(am, fragment)
+ @res << wrap(convert_flow(am.flow(fragment.txt))) << "\n"
+ @in_list_entry[-1] = list_end_for(fragment.type)
+ end
+
+ def accept_blank_line(am, fragment)
+ # @res << "\n"
+ end
+
+ def accept_heading(am, fragment)
+ @res << convert_heading(fragment.head_level, am.flow(fragment.txt))
+ end
+
+ ##
+ # This is a higher speed (if messier) version of wrap
+
+ def wrap(txt, line_len = 76)
+ res = ""
+ sp = 0
+ ep = txt.length
+ while sp < ep
+ # scan back for a space
+ p = sp + line_len - 1
+ if p >= ep
+ p = ep
+ else
+ while p > sp and txt[p] != ?\s
+ p -= 1
+ end
+ if p <= sp
+ p = sp + line_len
+ while p < ep and txt[p] != ?\s
+ p += 1
+ end
+ end
+ end
+ res << txt[sp...p] << "\n"
+ sp = p
+ sp += 1 while sp < ep and txt[sp] == ?\s
+ end
+ res
+ end
+
+ private
+
+ def on_tags(res, item)
+ attr_mask = item.turn_on
+ return if attr_mask.zero?
+
+ @attr_tags.each do |tag|
+ if attr_mask & tag.bit != 0
+ res << tag.on
+ end
+ end
+ end
+
+ def off_tags(res, item)
+ attr_mask = item.turn_off
+ return if attr_mask.zero?
+
+ @attr_tags.reverse_each do |tag|
+ if attr_mask & tag.bit != 0
+ res << tag.off
+ end
+ end
+ end
+
+ def convert_flow(flow)
+ res = ""
+ flow.each do |item|
+ case item
+ when String
+ $stderr.puts "Converting '#{item}'" if $DEBUG_RDOC
+ res << convert_string(item)
+ when AttrChanger
+ off_tags(res, item)
+ on_tags(res, item)
+ when Special
+ res << convert_special(item)
+ else
+ raise "Unknown flow element: #{item.inspect}"
+ end
+ end
+ res
+ end
+
+ ##
+ # some of these patterns are taken from SmartyPants...
+
+ def convert_string(item)
+ escape(item).
+
+ # convert ... to elipsis (and make sure .... becomes .<elipsis>)
+ gsub(/\.\.\.\./, '.\ldots{}').gsub(/\.\.\./, '\ldots{}').
+
+ # convert single closing quote
+ gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1\'').
+ gsub(%r{\'(?=\W|s\b)}, "'" ).
+
+ # convert single opening quote
+ gsub(/'/, '`').
+
+ # convert double closing quote
+ gsub(%r{([^ \t\r\n\[\{\(])\"(?=\W)}, "\\1''").
+
+ # convert double opening quote
+ gsub(/"/, "``").
+
+ # convert copyright
+ gsub(/\(c\)/, '\copyright{}')
+
+ end
+
+ def convert_special(special)
+ handled = false
+ Attribute.each_name_of(special.type) do |name|
+ method_name = "handle_special_#{name}"
+ if self.respond_to? method_name
+ special.text = send(method_name, special)
+ handled = true
+ end
+ end
+ raise "Unhandled special: #{special}" unless handled
+ special.text
+ end
+
+ def convert_heading(level, flow)
+ res =
+ case level
+ when 1 then "\\chapter{"
+ when 2 then "\\section{"
+ when 3 then "\\subsection{"
+ when 4 then "\\subsubsection{"
+ else "\\paragraph{"
+ end +
+ convert_flow(flow) +
+ "}\n"
+ end
+
+ def list_name(list_type, is_open_tag)
+ tags = LIST_TYPE_TO_LATEX[list_type] || raise("Invalid list type: #{list_type.inspect}")
+ if tags[2] # enumerate
+ if is_open_tag
+ @list_depth += 1
+ if @prev_list_types[@list_depth] != tags[2]
+ case @list_depth
+ when 1
+ roman = "i"
+ when 2
+ roman = "ii"
+ when 3
+ roman = "iii"
+ when 4
+ roman = "iv"
+ else
+ raise("Too deep list: level #{@list_depth}")
+ end
+ @prev_list_types[@list_depth] = tags[2]
+ return l("\\renewcommand{\\labelenum#{roman}}{#{tags[2]}{enum#{roman}}}") + "\n" + tags[0]
+ end
+ else
+ @list_depth -= 1
+ end
+ end
+ tags[ is_open_tag ? 0 : 1]
+ end
+
+ def list_item_start(am, fragment)
+ case fragment.type
+ when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA then
+ "\\item "
+
+ when :LABELED then
+ "\\item[" + convert_flow(am.flow(fragment.param)) + "] "
+
+ when :NOTE then
+ convert_flow(am.flow(fragment.param)) + " & "
+ else
+ raise "Invalid list type"
+ end
+ end
+
+ def list_end_for(fragment_type)
+ case fragment_type
+ when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA, :LABELED then
+ ""
+ when :NOTE
+ "\\\\\n"
+ else
+ raise "Invalid list type"
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/to_test.rb b/ruby/lib/rdoc/markup/to_test.rb
new file mode 100644
index 0000000..ce6aff6
--- /dev/null
+++ b/ruby/lib/rdoc/markup/to_test.rb
@@ -0,0 +1,50 @@
+require 'rdoc/markup'
+require 'rdoc/markup/formatter'
+
+##
+# This Markup outputter is used for testing purposes.
+
+class RDoc::Markup::ToTest < RDoc::Markup::Formatter
+
+ def start_accepting
+ @res = []
+ end
+
+ def end_accepting
+ @res
+ end
+
+ def accept_paragraph(am, fragment)
+ @res << fragment.to_s
+ end
+
+ def accept_verbatim(am, fragment)
+ @res << fragment.to_s
+ end
+
+ def accept_list_start(am, fragment)
+ @res << fragment.to_s
+ end
+
+ def accept_list_end(am, fragment)
+ @res << fragment.to_s
+ end
+
+ def accept_list_item(am, fragment)
+ @res << fragment.to_s
+ end
+
+ def accept_blank_line(am, fragment)
+ @res << fragment.to_s
+ end
+
+ def accept_heading(am, fragment)
+ @res << fragment.to_s
+ end
+
+ def accept_rule(am, fragment)
+ @res << fragment.to_s
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/markup/to_texinfo.rb b/ruby/lib/rdoc/markup/to_texinfo.rb
new file mode 100644
index 0000000..65a1608
--- /dev/null
+++ b/ruby/lib/rdoc/markup/to_texinfo.rb
@@ -0,0 +1,69 @@
+require 'rdoc/markup/formatter'
+require 'rdoc/markup/fragments'
+require 'rdoc/markup/inline'
+
+require 'rdoc/markup'
+require 'rdoc/markup/formatter'
+
+##
+# Convert SimpleMarkup to basic TexInfo format
+#
+# TODO: WTF is AttributeManager for?
+#
+class RDoc::Markup::ToTexInfo < RDoc::Markup::Formatter
+
+ def start_accepting
+ @text = []
+ end
+
+ def end_accepting
+ @text.join("\n")
+ end
+
+ def accept_paragraph(attributes, text)
+ @text << format(text)
+ end
+
+ def accept_verbatim(attributes, text)
+ @text << "@verb{|#{format(text)}|}"
+ end
+
+ def accept_heading(attributes, text)
+ heading = ['@majorheading', '@chapheading'][text.head_level - 1] || '@heading'
+ @text << "#{heading} #{format(text)}"
+ end
+
+ def accept_list_start(attributes, text)
+ @text << '@itemize @bullet'
+ end
+
+ def accept_list_end(attributes, text)
+ @text << '@end itemize'
+ end
+
+ def accept_list_item(attributes, text)
+ @text << "@item\n#{format(text)}"
+ end
+
+ def accept_blank_line(attributes, text)
+ @text << "\n"
+ end
+
+ def accept_rule(attributes, text)
+ @text << '-----'
+ end
+
+ def format(text)
+ text.txt.
+ gsub(/@/, "@@").
+ gsub(/\{/, "@{").
+ gsub(/\}/, "@}").
+ # gsub(/,/, "@,"). # technically only required in cross-refs
+ gsub(/\+([\w]+)\+/, "@code{\\1}").
+ gsub(/\<tt\>([^<]+)\<\/tt\>/, "@code{\\1}").
+ gsub(/\*([\w]+)\*/, "@strong{\\1}").
+ gsub(/\<b\>([^<]+)\<\/b\>/, "@strong{\\1}").
+ gsub(/_([\w]+)_/, "@emph{\\1}").
+ gsub(/\<em\>([^<]+)\<\/em\>/, "@emph{\\1}")
+ end
+end
diff --git a/ruby/lib/rdoc/options.rb b/ruby/lib/rdoc/options.rb
new file mode 100644
index 0000000..1d92bd4
--- /dev/null
+++ b/ruby/lib/rdoc/options.rb
@@ -0,0 +1,638 @@
+# We handle the parsing of options, and subsequently as a singleton
+# object to be queried for option values
+
+require "rdoc/ri/paths"
+require 'optparse'
+
+class RDoc::Options
+
+ ##
+ # Should the output be placed into a single file
+
+ attr_reader :all_one_file
+
+ ##
+ # Character-set
+
+ attr_reader :charset
+
+ ##
+ # URL of stylesheet
+
+ attr_reader :css
+
+ ##
+ # Should diagrams be drawn
+
+ attr_reader :diagram
+
+ ##
+ # Files matching this pattern will be excluded
+
+ attr_accessor :exclude
+
+ ##
+ # Additional attr_... style method flags
+
+ attr_reader :extra_accessor_flags
+
+ ##
+ # Pattern for additional attr_... style methods
+
+ attr_accessor :extra_accessors
+
+ ##
+ # Should we draw fileboxes in diagrams
+
+ attr_reader :fileboxes
+
+ ##
+ # The list of files to be processed
+
+ attr_accessor :files
+
+ ##
+ # Scan newer sources than the flag file if true.
+
+ attr_reader :force_update
+
+ ##
+ # Description of the output generator (set with the <tt>-fmt</tt> option)
+
+ attr_accessor :generator
+
+ ##
+ # Formatter to mark up text with
+
+ attr_accessor :formatter
+
+ ##
+ # image format for diagrams
+
+ attr_reader :image_format
+
+ ##
+ # Include line numbers in the source listings
+
+ attr_reader :include_line_numbers
+
+ ##
+ # Should source code be included inline, or displayed in a popup
+
+ attr_accessor :inline_source
+
+ ##
+ # Name of the file, class or module to display in the initial index page (if
+ # not specified the first file we encounter is used)
+
+ attr_accessor :main_page
+
+ ##
+ # Merge into classes of the same name when generating ri
+
+ attr_reader :merge
+
+ ##
+ # The name of the output directory
+
+ attr_accessor :op_dir
+
+ ##
+ # The name to use for the output
+
+ attr_accessor :op_name
+
+ ##
+ # Are we promiscuous about showing module contents across multiple files
+
+ attr_reader :promiscuous
+
+ ##
+ # Array of directories to search for files to satisfy an :include:
+
+ attr_reader :rdoc_include
+
+ ##
+ # Include private and protected methods in the output
+
+ attr_accessor :show_all
+
+ ##
+ # Include the '#' at the front of hyperlinked instance method names
+
+ attr_reader :show_hash
+
+ ##
+ # The number of columns in a tab
+
+ attr_reader :tab_width
+
+ ##
+ # template to be used when generating output
+
+ attr_reader :template
+
+ ##
+ # Template class for file generation
+ #--
+ # HACK around dependencies in lib/rdoc/generator/html.rb
+
+ attr_accessor :template_class # :nodoc:
+
+ ##
+ # Documentation title
+
+ attr_reader :title
+
+ ##
+ # Verbosity, zero means quiet
+
+ attr_accessor :verbosity
+
+ ##
+ # URL of web cvs frontend
+
+ attr_reader :webcvs
+
+ def initialize(generators = {}) # :nodoc:
+ @op_dir = "doc"
+ @op_name = nil
+ @show_all = false
+ @main_page = nil
+ @merge = false
+ @exclude = []
+ @generators = generators
+ @generator_name = 'html'
+ @generator = @generators[@generator_name]
+ @rdoc_include = []
+ @title = nil
+ @template = nil
+ @template_class = nil
+ @diagram = false
+ @fileboxes = false
+ @show_hash = false
+ @image_format = 'png'
+ @inline_source = false
+ @all_one_file = false
+ @tab_width = 8
+ @include_line_numbers = false
+ @extra_accessor_flags = {}
+ @promiscuous = false
+ @force_update = false
+ @verbosity = 1
+
+ @css = nil
+ @webcvs = nil
+
+ @charset = 'utf-8'
+ end
+
+ ##
+ # Parse command line options.
+
+ def parse(argv)
+ accessors = []
+
+ opts = OptionParser.new do |opt|
+ opt.program_name = File.basename $0
+ opt.version = RDoc::VERSION
+ opt.release = nil
+ opt.summary_indent = ' ' * 4
+ opt.banner = <<-EOF
+Usage: #{opt.program_name} [options] [names...]
+
+ Files are parsed, and the information they contain collected, before any
+ output is produced. This allows cross references between all files to be
+ resolved. If a name is a directory, it is traversed. If no names are
+ specified, all Ruby files in the current directory (and subdirectories) are
+ processed.
+
+ How RDoc generates output depends on the output formatter being used, and on
+ the options you give.
+
+ - HTML output is normally produced into a number of separate files
+ (one per class, module, and file, along with various indices).
+ These files will appear in the directory given by the --op
+ option (doc/ by default).
+
+ - XML output by default is written to standard output. If a
+ --opname option is given, the output will instead be written
+ to a file with that name in the output directory.
+
+ - .chm files (Windows help files) are written in the --op directory.
+ If an --opname parameter is present, that name is used, otherwise
+ the file will be called rdoc.chm.
+ EOF
+
+ opt.separator nil
+ opt.separator "Options:"
+ opt.separator nil
+
+ opt.on("--accessor=ACCESSORS", "-A", Array,
+ "A comma separated list of additional class",
+ "methods that should be treated like",
+ "'attr_reader' and friends.",
+ " ",
+ "Option may be repeated.",
+ " ",
+ "Each accessorname may have '=text'",
+ "appended, in which case that text appears",
+ "where the r/w/rw appears for normal.",
+ "accessors") do |value|
+ value.each do |accessor|
+ if accessor =~ /^(\w+)(=(.*))?$/
+ accessors << $1
+ @extra_accessor_flags[$1] = $3
+ end
+ end
+ end
+
+ opt.separator nil
+
+ opt.on("--all", "-a",
+ "Include all methods (not just public) in",
+ "the output.") do |value|
+ @show_all = value
+ end
+
+ opt.separator nil
+
+ opt.on("--charset=CHARSET", "-c",
+ "Specifies the output HTML character-set.") do |value|
+ @charset = value
+ end
+
+ opt.separator nil
+
+ opt.on("--debug", "-D",
+ "Displays lots on internal stuff.") do |value|
+ $DEBUG_RDOC = value
+ end
+
+ opt.separator nil
+
+ opt.on("--diagram", "-d",
+ "Generate diagrams showing modules and",
+ "classes. You need dot V1.8.6 or later to",
+ "use the --diagram option correctly. Dot is",
+ "available from http://graphviz.org") do |value|
+ check_diagram
+ @diagram = true
+ end
+
+ opt.separator nil
+
+ opt.on("--exclude=PATTERN", "-x", Regexp,
+ "Do not process files or directories",
+ "matching PATTERN.") do |value|
+ @exclude << value
+ end
+
+ opt.separator nil
+
+ opt.on("--extension=NEW=OLD", "-E",
+ "Treat files ending with .new as if they",
+ "ended with .old. Using '-E cgi=rb' will",
+ "cause xxx.cgi to be parsed as a Ruby file.") do |value|
+ new, old = value.split(/=/, 2)
+
+ unless new and old then
+ raise OptionParser::InvalidArgument, "Invalid parameter to '-E'"
+ end
+
+ unless RDoc::ParserFactory.alias_extension old, new then
+ raise OptionParser::InvalidArgument, "Unknown extension .#{old} to -E"
+ end
+ end
+
+ opt.separator nil
+
+ opt.on("--fileboxes", "-F",
+ "Classes are put in boxes which represents",
+ "files, where these classes reside. Classes",
+ "shared between more than one file are",
+ "shown with list of files that are sharing",
+ "them. Silently discarded if --diagram is",
+ "not given.") do |value|
+ @fileboxes = value
+ end
+
+ opt.separator nil
+
+ opt.on("--force-update", "-U",
+ "Forces rdoc to scan all sources even if",
+ "newer than the flag file.") do |value|
+ @force_update = value
+ end
+
+ opt.separator nil
+
+ opt.on("--fmt=FORMAT", "--format=FORMAT", "-f", @generators.keys,
+ "Set the output formatter.") do |value|
+ @generator_name = value.downcase
+ setup_generator
+ end
+
+ opt.separator nil
+
+ image_formats = %w[gif png jpg jpeg]
+ opt.on("--image-format=FORMAT", "-I", image_formats,
+ "Sets output image format for diagrams. Can",
+ "be #{image_formats.join ', '}. If this option",
+ "is omitted, png is used. Requires",
+ "diagrams.") do |value|
+ @image_format = value
+ end
+
+ opt.separator nil
+
+ opt.on("--include=DIRECTORIES", "-i", Array,
+ "set (or add to) the list of directories to",
+ "be searched when satisfying :include:",
+ "requests. Can be used more than once.") do |value|
+ @rdoc_include.concat value.map { |dir| dir.strip }
+ end
+
+ opt.separator nil
+
+ opt.on("--inline-source", "-S",
+ "Show method source code inline, rather than",
+ "via a popup link.") do |value|
+ @inline_source = value
+ end
+
+ opt.separator nil
+
+ opt.on("--line-numbers", "-N",
+ "Include line numbers in the source code.") do |value|
+ @include_line_numbers = value
+ end
+
+ opt.separator nil
+
+ opt.on("--main=NAME", "-m",
+ "NAME will be the initial page displayed.") do |value|
+ @main_page = value
+ end
+
+ opt.separator nil
+
+ opt.on("--merge", "-M",
+ "When creating ri output, merge previously",
+ "processed classes into previously",
+ "documented classes of the same name.") do |value|
+ @merge = value
+ end
+
+ opt.separator nil
+
+ opt.on("--one-file", "-1",
+ "Put all the output into a single file.") do |value|
+ @all_one_file = value
+ @inline_source = value if value
+ @template = 'one_page_html'
+ end
+
+ opt.separator nil
+
+ opt.on("--op=DIR", "-o",
+ "Set the output directory.") do |value|
+ @op_dir = value
+ end
+
+ opt.separator nil
+
+ opt.on("--opname=NAME", "-n",
+ "Set the NAME of the output. Has no effect",
+ "for HTML.") do |value|
+ @op_name = value
+ end
+
+ opt.separator nil
+
+ opt.on("--promiscuous", "-p",
+ "When documenting a file that contains a",
+ "module or class also defined in other",
+ "files, show all stuff for that module or",
+ "class in each files page. By default, only",
+ "show stuff defined in that particular file.") do |value|
+ @promiscuous = value
+ end
+
+ opt.separator nil
+
+ opt.on("--quiet", "-q",
+ "Don't show progress as we parse.") do |value|
+ @verbosity = 0
+ end
+
+ opt.on("--verbose", "-v",
+ "Display extra progress as we parse.") do |value|
+ @verbosity = 2
+ end
+
+
+ opt.separator nil
+
+ opt.on("--ri", "-r",
+ "Generate output for use by `ri`. The files",
+ "are stored in the '.rdoc' directory under",
+ "your home directory unless overridden by a",
+ "subsequent --op parameter, so no special",
+ "privileges are needed.") do |value|
+ @generator_name = "ri"
+ @op_dir = RDoc::RI::Paths::HOMEDIR
+ setup_generator
+ end
+
+ opt.separator nil
+
+ opt.on("--ri-site", "-R",
+ "Generate output for use by `ri`. The files",
+ "are stored in a site-wide directory,",
+ "making them accessible to others, so",
+ "special privileges are needed.") do |value|
+ @generator_name = "ri"
+ @op_dir = RDoc::RI::Paths::SITEDIR
+ setup_generator
+ end
+
+ opt.separator nil
+
+ opt.on("--ri-system", "-Y",
+ "Generate output for use by `ri`. The files",
+ "are stored in a site-wide directory,",
+ "making them accessible to others, so",
+ "special privileges are needed. This",
+ "option is intended to be used during Ruby",
+ "installation.") do |value|
+ @generator_name = "ri"
+ @op_dir = RDoc::RI::Paths::SYSDIR
+ setup_generator
+ end
+
+ opt.separator nil
+
+ opt.on("--show-hash", "-H",
+ "A name of the form #name in a comment is a",
+ "possible hyperlink to an instance method",
+ "name. When displayed, the '#' is removed",
+ "unless this option is specified.") do |value|
+ @show_hash = value
+ end
+
+ opt.separator nil
+
+ opt.on("--style=URL", "-s",
+ "Specifies the URL of a separate stylesheet.") do |value|
+ @css = value
+ end
+
+ opt.separator nil
+
+ opt.on("--tab-width=WIDTH", "-w", OptionParser::DecimalInteger,
+ "Set the width of tab characters.") do |value|
+ @tab_width = value
+ end
+
+ opt.separator nil
+
+ opt.on("--template=NAME", "-T",
+ "Set the template used when generating",
+ "output.") do |value|
+ @template = value
+ end
+
+ opt.separator nil
+
+ opt.on("--title=TITLE", "-t",
+ "Set TITLE as the title for HTML output.") do |value|
+ @title = value
+ end
+
+ opt.separator nil
+
+ opt.on("--webcvs=URL", "-W",
+ "Specify a URL for linking to a web frontend",
+ "to CVS. If the URL contains a '\%s', the",
+ "name of the current file will be",
+ "substituted; if the URL doesn't contain a",
+ "'\%s', the filename will be appended to it.") do |value|
+ @webcvs = value
+ end
+ end
+
+ argv.insert(0, *ENV['RDOCOPT'].split) if ENV['RDOCOPT']
+
+ opts.parse! argv
+
+ @files = argv.dup
+
+ @rdoc_include << "." if @rdoc_include.empty?
+
+ if @exclude.empty? then
+ @exclude = nil
+ else
+ @exclude = Regexp.new(@exclude.join("|"))
+ end
+
+ check_files
+
+ # If no template was specified, use the default template for the output
+ # formatter
+
+ @template ||= @generator_name
+
+ # Generate a regexp from the accessors
+ unless accessors.empty? then
+ re = '^(' + accessors.map { |a| Regexp.quote a }.join('|') + ')$'
+ @extra_accessors = Regexp.new re
+ end
+
+ rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => e
+ puts opts
+ puts
+ puts e
+ exit 1
+ end
+
+ ##
+ # Set the title, but only if not already set. This means that a title set
+ # from the command line trumps one set in a source file
+
+ def title=(string)
+ @title ||= string
+ end
+
+ ##
+ # Don't display progress as we process the files
+
+ def quiet
+ @verbosity.zero?
+ end
+
+ def quiet=(bool)
+ @verbosity = bool ? 0 : 1
+ end
+
+ private
+
+ ##
+ # Set up an output generator for the format in @generator_name
+
+ def setup_generator
+ @generator = @generators[@generator_name]
+
+ unless @generator then
+ raise OptionParser::InvalidArgument, "Invalid output formatter"
+ end
+
+ if @generator_name == "xml" then
+ @all_one_file = true
+ @inline_source = true
+ end
+ end
+
+ # Check that the right version of 'dot' is available. Unfortunately this
+ # doesn't work correctly under Windows NT, so we'll bypass the test under
+ # Windows.
+
+ def check_diagram
+ return if RUBY_PLATFORM =~ /mswin|cygwin|mingw|bccwin/
+
+ ok = false
+ ver = nil
+
+ IO.popen "dot -V 2>&1" do |io|
+ ver = io.read
+ if ver =~ /dot.+version(?:\s+gviz)?\s+(\d+)\.(\d+)/ then
+ ok = ($1.to_i > 1) || ($1.to_i == 1 && $2.to_i >= 8)
+ end
+ end
+
+ unless ok then
+ if ver =~ /^dot.+version/ then
+ $stderr.puts "Warning: You may need dot V1.8.6 or later to use\n",
+ "the --diagram option correctly. You have:\n\n ",
+ ver,
+ "\nDiagrams might have strange background colors.\n\n"
+ else
+ $stderr.puts "You need the 'dot' program to produce diagrams.",
+ "(see http://www.research.att.com/sw/tools/graphviz/)\n\n"
+ exit
+ end
+ end
+ end
+
+ ##
+ # Check that the files on the command line exist
+
+ def check_files
+ @files.each do |f|
+ stat = File.stat f
+ raise RDoc::Error, "file '#{f}' not readable" unless stat.readable?
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/parser.rb b/ruby/lib/rdoc/parser.rb
new file mode 100644
index 0000000..6b1233c
--- /dev/null
+++ b/ruby/lib/rdoc/parser.rb
@@ -0,0 +1,142 @@
+require 'rdoc'
+require 'rdoc/code_objects'
+require 'rdoc/markup/preprocess'
+require 'rdoc/stats'
+
+##
+# A parser is simple a class that implements
+#
+# #initialize(file_name, body, options)
+#
+# and
+#
+# #scan
+#
+# The initialize method takes a file name to be used, the body of the file,
+# and an RDoc::Options object. The scan method is then called to return an
+# appropriately parsed TopLevel code object.
+#
+# The ParseFactory is used to redirect to the correct parser given a
+# filename extension. This magic works because individual parsers have to
+# register themselves with us as they are loaded in. The do this using the
+# following incantation
+#
+# require "rdoc/parser"
+#
+# class RDoc::Parser::Xyz < RDoc::Parser
+# parse_files_matching /\.xyz$/ # <<<<
+#
+# def initialize(file_name, body, options)
+# ...
+# end
+#
+# def scan
+# ...
+# end
+# end
+#
+# Just to make life interesting, if we suspect a plain text file, we also
+# look for a shebang line just in case it's a potential shell script
+
+class RDoc::Parser
+
+ @parsers = []
+
+ class << self
+ attr_reader :parsers
+ end
+
+ ##
+ # Alias an extension to another extension. After this call, files ending
+ # "new_ext" will be parsed using the same parser as "old_ext"
+
+ def self.alias_extension(old_ext, new_ext)
+ old_ext = old_ext.sub(/^\.(.*)/, '\1')
+ new_ext = new_ext.sub(/^\.(.*)/, '\1')
+
+ parser = can_parse "xxx.#{old_ext}"
+ return false unless parser
+
+ RDoc::Parser.parsers.unshift [/\.#{new_ext}$/, parser]
+
+ true
+ end
+
+ ##
+ # Shamelessly stolen from the ptools gem (since RDoc cannot depend on
+ # the gem).
+
+ def self.binary?(file)
+ s = (File.read(file, File.stat(file).blksize, 0, :mode => "rb") || "").split(//)
+
+ if s.size > 0 then
+ ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
+ else
+ false
+ end
+ end
+ private_class_method :binary?
+
+ ##
+ # Return a parser that can handle a particular extension
+
+ def self.can_parse(file_name)
+ parser = RDoc::Parser.parsers.find { |regexp,| regexp =~ file_name }.last
+
+ #
+ # The default parser should *NOT* parse binary files.
+ #
+ if parser == RDoc::Parser::Simple then
+ if binary? file_name then
+ return nil
+ end
+ end
+
+ return parser
+ end
+
+ ##
+ # Find the correct parser for a particular file name. Return a SimpleParser
+ # for ones that we don't know
+
+ def self.for(top_level, file_name, body, options, stats)
+ # If no extension, look for shebang
+ if file_name !~ /\.\w+$/ && body =~ %r{\A#!(.+)} then
+ shebang = $1
+ case shebang
+ when %r{env\s+ruby}, %r{/ruby}
+ file_name = "dummy.rb"
+ end
+ end
+
+ parser = can_parse file_name
+
+ #
+ # This method must return a parser.
+ #
+ if !parser then
+ parser = RDoc::Parser::Simple
+ end
+
+ parser.new top_level, file_name, body, options, stats
+ end
+
+ ##
+ # Record which file types this parser can understand.
+
+ def self.parse_files_matching(regexp)
+ RDoc::Parser.parsers.unshift [regexp, self]
+ end
+
+ def initialize(top_level, file_name, content, options, stats)
+ @top_level = top_level
+ @file_name = file_name
+ @content = content
+ @options = options
+ @stats = stats
+ end
+
+end
+
+require 'rdoc/parser/simple'
+
diff --git a/ruby/lib/rdoc/parser/c.rb b/ruby/lib/rdoc/parser/c.rb
new file mode 100644
index 0000000..933838d
--- /dev/null
+++ b/ruby/lib/rdoc/parser/c.rb
@@ -0,0 +1,661 @@
+require 'rdoc/parser'
+require 'rdoc/parser/ruby'
+require 'rdoc/known_classes'
+
+##
+# We attempt to parse C extension files. Basically we look for
+# the standard patterns that you find in extensions: <tt>rb_define_class,
+# rb_define_method</tt> and so on. We also try to find the corresponding
+# C source for the methods and extract comments, but if we fail
+# we don't worry too much.
+#
+# The comments associated with a Ruby method are extracted from the C
+# comment block associated with the routine that _implements_ that
+# method, that is to say the method whose name is given in the
+# <tt>rb_define_method</tt> call. For example, you might write:
+#
+# /*
+# * Returns a new array that is a one-dimensional flattening of this
+# * array (recursively). That is, for every element that is an array,
+# * extract its elements into the new array.
+# *
+# * s = [ 1, 2, 3 ] #=> [1, 2, 3]
+# * t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
+# * a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
+# * a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+# */
+# static VALUE
+# rb_ary_flatten(ary)
+# VALUE ary;
+# {
+# ary = rb_obj_dup(ary);
+# rb_ary_flatten_bang(ary);
+# return ary;
+# }
+#
+# ...
+#
+# void
+# Init_Array()
+# {
+# ...
+# rb_define_method(rb_cArray, "flatten", rb_ary_flatten, 0);
+#
+# Here RDoc will determine from the rb_define_method line that there's a
+# method called "flatten" in class Array, and will look for the implementation
+# in the method rb_ary_flatten. It will then use the comment from that
+# method in the HTML output. This method must be in the same source file
+# as the rb_define_method.
+#
+# C classes can be diagrammed (see /tc/dl/ruby/ruby/error.c), and RDoc
+# integrates C and Ruby source into one tree
+#
+# The comment blocks may include special directives:
+#
+# [Document-class: <i>name</i>]
+# This comment block is documentation for the given class. Use this
+# when the <tt>Init_xxx</tt> method is not named after the class.
+#
+# [Document-method: <i>name</i>]
+# This comment documents the named method. Use when RDoc cannot
+# automatically find the method from it's declaration
+#
+# [call-seq: <i>text up to an empty line</i>]
+# Because C source doesn't give descripive names to Ruby-level parameters,
+# you need to document the calling sequence explicitly
+#
+# In addition, RDoc assumes by default that the C method implementing a
+# Ruby function is in the same source file as the rb_define_method call.
+# If this isn't the case, add the comment:
+#
+# rb_define_method(....); // in: filename
+#
+# As an example, we might have an extension that defines multiple classes
+# in its Init_xxx method. We could document them using
+#
+# /*
+# * Document-class: MyClass
+# *
+# * Encapsulate the writing and reading of the configuration
+# * file. ...
+# */
+#
+# /*
+# * Document-method: read_value
+# *
+# * call-seq:
+# * cfg.read_value(key) -> value
+# * cfg.read_value(key} { |key| } -> value
+# *
+# * Return the value corresponding to +key+ from the configuration.
+# * In the second form, if the key isn't found, invoke the
+# * block and return its value.
+# */
+
+class RDoc::Parser::C < RDoc::Parser
+
+ parse_files_matching(/\.(?:([CcHh])\1?|c([+xp])\2|y)\z/)
+
+ @@enclosure_classes = {}
+ @@known_bodies = {}
+
+ ##
+ # Prepare to parse a C file
+
+ def initialize(top_level, file_name, content, options, stats)
+ super
+
+ @known_classes = RDoc::KNOWN_CLASSES.dup
+ @content = handle_tab_width handle_ifdefs_in(@content)
+ @classes = Hash.new
+ @file_dir = File.dirname(@file_name)
+ end
+
+ def do_aliases
+ @content.scan(%r{rb_define_alias\s*\(\s*(\w+),\s*"([^"]+)",\s*"([^"]+)"\s*\)}m) do
+ |var_name, new_name, old_name|
+ class_name = @known_classes[var_name] || var_name
+ class_obj = find_class(var_name, class_name)
+
+ as = class_obj.add_alias RDoc::Alias.new("", old_name, new_name, "")
+
+ @stats.add_alias as
+ end
+ end
+
+ def do_classes
+ @content.scan(/(\w+)\s* = \s*rb_define_module\s*\(\s*"(\w+)"\s*\)/mx) do
+ |var_name, class_name|
+ handle_class_module(var_name, "module", class_name, nil, nil)
+ end
+
+ # The '.' lets us handle SWIG-generated files
+ @content.scan(/([\w\.]+)\s* = \s*rb_define_class\s*
+ \(
+ \s*"(\w+)",
+ \s*(\w+)\s*
+ \)/mx) do |var_name, class_name, parent|
+ handle_class_module(var_name, "class", class_name, parent, nil)
+ end
+
+ @content.scan(/(\w+)\s*=\s*boot_defclass\s*\(\s*"(\w+?)",\s*(\w+?)\s*\)/) do
+ |var_name, class_name, parent|
+ parent = nil if parent == "0"
+ handle_class_module(var_name, "class", class_name, parent, nil)
+ end
+
+ @content.scan(/(\w+)\s* = \s*rb_define_module_under\s*
+ \(
+ \s*(\w+),
+ \s*"(\w+)"
+ \s*\)/mx) do |var_name, in_module, class_name|
+ handle_class_module(var_name, "module", class_name, nil, in_module)
+ end
+
+ @content.scan(/([\w\.]+)\s* = \s*rb_define_class_under\s*
+ \(
+ \s*(\w+),
+ \s*"(\w+)",
+ \s*([\w\*\s\(\)\.\->]+)\s* # for SWIG
+ \s*\)/mx) do |var_name, in_module, class_name, parent|
+ handle_class_module(var_name, "class", class_name, parent, in_module)
+ end
+ end
+
+ def do_constants
+ @content.scan(%r{\Wrb_define_
+ (
+ variable |
+ readonly_variable |
+ const |
+ global_const |
+ )
+ \s*\(
+ (?:\s*(\w+),)?
+ \s*"(\w+)",
+ \s*(.*?)\s*\)\s*;
+ }xm) do |type, var_name, const_name, definition|
+ var_name = "rb_cObject" if !var_name or var_name == "rb_mKernel"
+ handle_constants(type, var_name, const_name, definition)
+ end
+ end
+
+ ##
+ # Look for includes of the form:
+ #
+ # rb_include_module(rb_cArray, rb_mEnumerable);
+
+ def do_includes
+ @content.scan(/rb_include_module\s*\(\s*(\w+?),\s*(\w+?)\s*\)/) do |c,m|
+ if cls = @classes[c]
+ m = @known_classes[m] || m
+ cls.add_include RDoc::Include.new(m, "")
+ end
+ end
+ end
+
+ def do_methods
+ @content.scan(%r{rb_define_
+ (
+ singleton_method |
+ method |
+ module_function |
+ private_method
+ )
+ \s*\(\s*([\w\.]+),
+ \s*"([^"]+)",
+ \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
+ \s*(-?\w+)\s*\)
+ (?:;\s*/[*/]\s+in\s+(\w+?\.[cy]))?
+ }xm) do
+ |type, var_name, meth_name, meth_body, param_count, source_file|
+
+ # Ignore top-object and weird struct.c dynamic stuff
+ next if var_name == "ruby_top_self"
+ next if var_name == "nstr"
+ next if var_name == "envtbl"
+ next if var_name == "argf" # it'd be nice to handle this one
+
+ var_name = "rb_cObject" if var_name == "rb_mKernel"
+ handle_method(type, var_name, meth_name,
+ meth_body, param_count, source_file)
+ end
+
+ @content.scan(%r{rb_define_attr\(
+ \s*([\w\.]+),
+ \s*"([^"]+)",
+ \s*(\d+),
+ \s*(\d+)\s*\);
+ }xm) do |var_name, attr_name, attr_reader, attr_writer|
+ #var_name = "rb_cObject" if var_name == "rb_mKernel"
+ handle_attr(var_name, attr_name,
+ attr_reader.to_i != 0,
+ attr_writer.to_i != 0)
+ end
+
+ @content.scan(%r{rb_define_global_function\s*\(
+ \s*"([^"]+)",
+ \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
+ \s*(-?\w+)\s*\)
+ (?:;\s*/[*/]\s+in\s+(\w+?\.[cy]))?
+ }xm) do |meth_name, meth_body, param_count, source_file|
+ handle_method("method", "rb_mKernel", meth_name,
+ meth_body, param_count, source_file)
+ end
+
+ @content.scan(/define_filetest_function\s*\(
+ \s*"([^"]+)",
+ \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
+ \s*(-?\w+)\s*\)/xm) do
+ |meth_name, meth_body, param_count|
+
+ handle_method("method", "rb_mFileTest", meth_name, meth_body, param_count)
+ handle_method("singleton_method", "rb_cFile", meth_name, meth_body, param_count)
+ end
+ end
+
+ def find_attr_comment(attr_name)
+ if @content =~ %r{((?>/\*.*?\*/\s+))
+ rb_define_attr\((?:\s*(\w+),)?\s*"#{attr_name}"\s*,.*?\)\s*;}xmi
+ $1
+ elsif @content =~ %r{Document-attr:\s#{attr_name}\s*?\n((?>.*?\*/))}m
+ $1
+ else
+ ''
+ end
+ end
+
+ ##
+ # Find the C code corresponding to a Ruby method
+
+ def find_body(class_name, meth_name, meth_obj, body, quiet = false)
+ case body
+ when %r"((?>/\*.*?\*/\s*))(?:(?:static|SWIGINTERN)\s+)?(?:intern\s+)?VALUE\s+#{meth_name}
+ \s*(\([^)]*\))([^;]|$)"xm
+ comment, params = $1, $2
+ body_text = $&
+
+ remove_private_comments(comment) if comment
+
+ # see if we can find the whole body
+
+ re = Regexp.escape(body_text) + '[^(]*^\{.*?^\}'
+ body_text = $& if /#{re}/m =~ body
+
+ # The comment block may have been overridden with a 'Document-method'
+ # block. This happens in the interpreter when multiple methods are
+ # vectored through to the same C method but those methods are logically
+ # distinct (for example Kernel.hash and Kernel.object_id share the same
+ # implementation
+
+ override_comment = find_override_comment(class_name, meth_obj.name)
+ comment = override_comment if override_comment
+
+ find_modifiers(comment, meth_obj) if comment
+
+# meth_obj.params = params
+ meth_obj.start_collecting_tokens
+ meth_obj.add_token(RDoc::RubyToken::Token.new(1,1).set_text(body_text))
+ meth_obj.comment = mangle_comment(comment)
+ when %r{((?>/\*.*?\*/\s*))^\s*\#\s*define\s+#{meth_name}\s+(\w+)}m
+ comment = $1
+ find_body(class_name, $2, meth_obj, body, true)
+ find_modifiers(comment, meth_obj)
+ meth_obj.comment = mangle_comment(comment) + meth_obj.comment
+ when %r{^\s*\#\s*define\s+#{meth_name}\s+(\w+)}m
+ unless find_body(class_name, $1, meth_obj, body, true)
+ warn "No definition for #{meth_name}" unless @options.quiet
+ return false
+ end
+ else
+
+ # No body, but might still have an override comment
+ comment = find_override_comment(class_name, meth_obj.name)
+
+ if comment
+ find_modifiers(comment, meth_obj)
+ meth_obj.comment = mangle_comment(comment)
+ else
+ warn "No definition for #{meth_name}" unless @options.quiet
+ return false
+ end
+ end
+ true
+ end
+
+ def find_class(raw_name, name)
+ unless @classes[raw_name]
+ if raw_name =~ /^rb_m/
+ container = @top_level.add_module RDoc::NormalModule, name
+ else
+ container = @top_level.add_class RDoc::NormalClass, name, nil
+ end
+
+ container.record_location @top_level
+ @classes[raw_name] = container
+ end
+ @classes[raw_name]
+ end
+
+ ##
+ # Look for class or module documentation above Init_+class_name+(void),
+ # in a Document-class +class_name+ (or module) comment or above an
+ # rb_define_class (or module). If a comment is supplied above a matching
+ # Init_ and a rb_define_class the Init_ comment is used.
+ #
+ # /*
+ # * This is a comment for Foo
+ # */
+ # Init_Foo(void) {
+ # VALUE cFoo = rb_define_class("Foo", rb_cObject);
+ # }
+ #
+ # /*
+ # * Document-class: Foo
+ # * This is a comment for Foo
+ # */
+ # Init_foo(void) {
+ # VALUE cFoo = rb_define_class("Foo", rb_cObject);
+ # }
+ #
+ # /*
+ # * This is a comment for Foo
+ # */
+ # VALUE cFoo = rb_define_class("Foo", rb_cObject);
+
+ def find_class_comment(class_name, class_meth)
+ comment = nil
+ if @content =~ %r{((?>/\*.*?\*/\s+))
+ (static\s+)?void\s+Init_#{class_name}\s*(?:_\(\s*)?\(\s*(?:void\s*)\)}xmi then
+ comment = $1
+ elsif @content =~ %r{Document-(?:class|module):\s#{class_name}\s*?(?:<\s+[:,\w]+)?\n((?>.*?\*/))}m
+ comment = $1
+ else
+ if @content =~ /rb_define_(class|module)/m then
+ class_name = class_name.split("::").last
+ comments = []
+ @content.split(/(\/\*.*?\*\/)\s*?\n/m).each_with_index do |chunk, index|
+ comments[index] = chunk
+ if chunk =~ /rb_define_(class|module).*?"(#{class_name})"/m then
+ comment = comments[index-1]
+ break
+ end
+ end
+ end
+ end
+ class_meth.comment = mangle_comment(comment) if comment
+ end
+
+ ##
+ # Finds a comment matching +type+ and +const_name+ either above the
+ # comment or in the matching Document- section.
+
+ def find_const_comment(type, const_name)
+ if @content =~ %r{((?>^\s*/\*.*?\*/\s+))
+ rb_define_#{type}\((?:\s*(\w+),)?\s*"#{const_name}"\s*,.*?\)\s*;}xmi
+ $1
+ elsif @content =~ %r{Document-(?:const|global|variable):\s#{const_name}\s*?\n((?>.*?\*/))}m
+ $1
+ else
+ ''
+ end
+ end
+
+ ##
+ # If the comment block contains a section that looks like:
+ #
+ # call-seq:
+ # Array.new
+ # Array.new(10)
+ #
+ # use it for the parameters.
+
+ def find_modifiers(comment, meth_obj)
+ if comment.sub!(/:nodoc:\s*^\s*\*?\s*$/m, '') or
+ comment.sub!(/\A\/\*\s*:nodoc:\s*\*\/\Z/, '')
+ meth_obj.document_self = false
+ end
+ if comment.sub!(/call-seq:(.*?)^\s*\*?\s*$/m, '') or
+ comment.sub!(/\A\/\*\s*call-seq:(.*?)\*\/\Z/, '')
+ seq = $1
+ seq.gsub!(/^\s*\*\s*/, '')
+ meth_obj.call_seq = seq
+ end
+ end
+
+ def find_override_comment(class_name, meth_name)
+ name = Regexp.escape(meth_name)
+ if @content =~ %r{Document-method:\s+#{class_name}(?:\.|::|#)#{name}\s*?\n((?>.*?\*/))}m then
+ $1
+ elsif @content =~ %r{Document-method:\s#{name}\s*?\n((?>.*?\*/))}m then
+ $1
+ end
+ end
+
+ def handle_attr(var_name, attr_name, reader, writer)
+ rw = ''
+ if reader
+ #@stats.num_methods += 1
+ rw << 'R'
+ end
+ if writer
+ #@stats.num_methods += 1
+ rw << 'W'
+ end
+
+ class_name = @known_classes[var_name]
+
+ return unless class_name
+
+ class_obj = find_class(var_name, class_name)
+
+ if class_obj
+ comment = find_attr_comment(attr_name)
+ unless comment.empty?
+ comment = mangle_comment(comment)
+ end
+ att = RDoc::Attr.new '', attr_name, rw, comment
+ class_obj.add_attribute(att)
+ end
+ end
+
+ def handle_class_module(var_name, class_mod, class_name, parent, in_module)
+ parent_name = @known_classes[parent] || parent
+
+ if in_module
+ enclosure = @classes[in_module] || @@enclosure_classes[in_module]
+ unless enclosure
+ if enclosure = @known_classes[in_module]
+ handle_class_module(in_module, (/^rb_m/ =~ in_module ? "module" : "class"),
+ enclosure, nil, nil)
+ enclosure = @classes[in_module]
+ end
+ end
+ unless enclosure
+ warn("Enclosing class/module '#{in_module}' for " +
+ "#{class_mod} #{class_name} not known")
+ return
+ end
+ else
+ enclosure = @top_level
+ end
+
+ if class_mod == "class" then
+ full_name = enclosure.full_name.to_s + "::#{class_name}"
+ if @content =~ %r{Document-class:\s+#{full_name}\s*<\s+([:,\w]+)} then
+ parent_name = $1
+ end
+ cm = enclosure.add_class RDoc::NormalClass, class_name, parent_name
+ @stats.add_class cm
+ else
+ cm = enclosure.add_module RDoc::NormalModule, class_name
+ @stats.add_module cm
+ end
+
+ cm.record_location(enclosure.toplevel)
+
+ find_class_comment(cm.full_name, cm)
+ @classes[var_name] = cm
+ @@enclosure_classes[var_name] = cm
+ @known_classes[var_name] = cm.full_name
+ end
+
+ ##
+ # Adds constant comments. By providing some_value: at the start ofthe
+ # comment you can override the C value of the comment to give a friendly
+ # definition.
+ #
+ # /* 300: The perfect score in bowling */
+ # rb_define_const(cFoo, "PERFECT", INT2FIX(300);
+ #
+ # Will override +INT2FIX(300)+ with the value +300+ in the output RDoc.
+ # Values may include quotes and escaped colons (\:).
+
+ def handle_constants(type, var_name, const_name, definition)
+ #@stats.num_constants += 1
+ class_name = @known_classes[var_name]
+
+ return unless class_name
+
+ class_obj = find_class(var_name, class_name)
+
+ unless class_obj
+ warn("Enclosing class/module '#{const_name}' for not known")
+ return
+ end
+
+ comment = find_const_comment(type, const_name)
+
+ # In the case of rb_define_const, the definition and comment are in
+ # "/* definition: comment */" form. The literal ':' and '\' characters
+ # can be escaped with a backslash.
+ if type.downcase == 'const' then
+ elements = mangle_comment(comment).split(':')
+ if elements.nil? or elements.empty? then
+ con = RDoc::Constant.new(const_name, definition,
+ mangle_comment(comment))
+ else
+ new_definition = elements[0..-2].join(':')
+ if new_definition.empty? then # Default to literal C definition
+ new_definition = definition
+ else
+ new_definition.gsub!("\:", ":")
+ new_definition.gsub!("\\", '\\')
+ end
+ new_definition.sub!(/\A(\s+)/, '')
+ new_comment = $1.nil? ? elements.last : "#{$1}#{elements.last.lstrip}"
+ con = RDoc::Constant.new(const_name, new_definition,
+ mangle_comment(new_comment))
+ end
+ else
+ con = RDoc::Constant.new const_name, definition, mangle_comment(comment)
+ end
+
+ class_obj.add_constant(con)
+ end
+
+ ##
+ # Removes #ifdefs that would otherwise confuse us
+
+ def handle_ifdefs_in(body)
+ body.gsub(/^#ifdef HAVE_PROTOTYPES.*?#else.*?\n(.*?)#endif.*?\n/m, '\1')
+ end
+
+ def handle_method(type, var_name, meth_name, meth_body, param_count,
+ source_file = nil)
+ class_name = @known_classes[var_name]
+
+ return unless class_name
+
+ class_obj = find_class var_name, class_name
+
+ if class_obj then
+ if meth_name == "initialize" then
+ meth_name = "new"
+ type = "singleton_method"
+ end
+
+ meth_obj = RDoc::AnyMethod.new '', meth_name
+ meth_obj.singleton = %w[singleton_method module_function].include? type
+
+ p_count = (Integer(param_count) rescue -1)
+
+ if p_count < 0
+ meth_obj.params = "(...)"
+ elsif p_count == 0
+ meth_obj.params = "()"
+ else
+ meth_obj.params = "(" + (1..p_count).map{|i| "p#{i}"}.join(", ") + ")"
+ end
+
+ if source_file then
+ file_name = File.join(@file_dir, source_file)
+ body = (@@known_bodies[source_file] ||= File.read(file_name))
+ else
+ body = @content
+ end
+
+ if find_body(class_name, meth_body, meth_obj, body) and meth_obj.document_self then
+ class_obj.add_method meth_obj
+ @stats.add_method meth_obj
+ end
+ end
+ end
+
+ def handle_tab_width(body)
+ if /\t/ =~ body
+ tab_width = @options.tab_width
+ body.split(/\n/).map do |line|
+ 1 while line.gsub!(/\t+/) { ' ' * (tab_width*$&.length - $`.length % tab_width)} && $~ #`
+ line
+ end .join("\n")
+ else
+ body
+ end
+ end
+
+ ##
+ # Remove the /*'s and leading asterisks from C comments
+
+ def mangle_comment(comment)
+ comment.sub!(%r{/\*+}) { " " * $&.length }
+ comment.sub!(%r{\*+/}) { " " * $&.length }
+ comment.gsub!(/^[ \t]*\*/m) { " " * $&.length }
+ comment
+ end
+
+ ##
+ # Removes lines that are commented out that might otherwise get picked up
+ # when scanning for classes and methods
+
+ def remove_commented_out_lines
+ @content.gsub!(%r{//.*rb_define_}, '//')
+ end
+
+ def remove_private_comments(comment)
+ comment.gsub!(/\/?\*--\n(.*?)\/?\*\+\+/m, '')
+ comment.sub!(/\/?\*--\n.*/m, '')
+ end
+
+ ##
+ # Extract the classes/modules and methods from a C file and return the
+ # corresponding top-level object
+
+ def scan
+ remove_commented_out_lines
+ do_classes
+ do_constants
+ do_methods
+ do_includes
+ do_aliases
+ @top_level
+ end
+
+ def warn(msg)
+ $stderr.puts
+ $stderr.puts msg
+ $stderr.flush
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/parser/f95.rb b/ruby/lib/rdoc/parser/f95.rb
new file mode 100644
index 0000000..fd372b0
--- /dev/null
+++ b/ruby/lib/rdoc/parser/f95.rb
@@ -0,0 +1,1835 @@
+require 'rdoc/parser'
+
+##
+# = Fortran95 RDoc Parser
+#
+# == Overview
+#
+# This parser parses Fortran95 files with suffixes "f90", "F90", "f95" and
+# "F95". Fortran95 files are expected to be conformed to Fortran95 standards.
+#
+# == Rules
+#
+# Fundamental rules are same as that of the Ruby parser. But comment markers
+# are '!' not '#'.
+#
+# === Correspondence between RDoc documentation and Fortran95 programs
+#
+# F95 parses main programs, modules, subroutines, functions, derived-types,
+# public variables, public constants, defined operators and defined
+# assignments. These components are described in items of RDoc documentation,
+# as follows.
+#
+# Files :: Files (same as Ruby)
+# Classes:: Modules
+# Methods:: Subroutines, functions, variables, constants, derived-types,
+# defined operators, defined assignments
+# Required files:: Files in which imported modules, external subroutines and
+# external functions are defined.
+# Included Modules:: List of imported modules
+# Attributes:: List of derived-types, List of imported modules all of whose
+# components are published again
+#
+# Components listed in 'Methods' (subroutines, functions, ...) defined in
+# modules are described in the item of 'Classes'. On the other hand,
+# components defined in main programs or as external procedures are described
+# in the item of 'Files'.
+#
+# === Components parsed by default
+#
+# By default, documentation on public components (subroutines, functions,
+# variables, constants, derived-types, defined operators, defined assignments)
+# are generated.
+#
+# With "--all" option, documentation on all components are generated (almost
+# same as the Ruby parser).
+#
+# === Information parsed automatically
+#
+# The following information is automatically parsed.
+#
+# * Types of arguments
+# * Types of variables and constants
+# * Types of variables in the derived types, and initial values
+# * NAMELISTs and types of variables in them, and initial values
+#
+# Aliases by interface statement are described in the item of 'Methods'.
+#
+# Components which are imported from other modules and published again are
+# described in the item of 'Methods'.
+#
+# === Format of comment blocks
+#
+# Comment blocks should be written as follows.
+#
+# Comment blocks are considered to be ended when the line without '!' appears.
+#
+# The indentation is not necessary.
+#
+# ! (Top of file)
+# !
+# ! Comment blocks for the files.
+# !
+# !--
+# ! The comment described in the part enclosed by
+# ! "!--" and "!++" is ignored.
+# !++
+# !
+# module hogehoge
+# !
+# ! Comment blocks for the modules (or the programs).
+# !
+#
+# private
+#
+# logical :: a ! a private variable
+# real, public :: b ! a public variable
+# integer, parameter :: c = 0 ! a public constant
+#
+# public :: c
+# public :: MULTI_ARRAY
+# public :: hoge, foo
+#
+# type MULTI_ARRAY
+# !
+# ! Comment blocks for the derived-types.
+# !
+# real, pointer :: var(:) =>null() ! Comments block for the variables.
+# integer :: num = 0
+# end type MULTI_ARRAY
+#
+# contains
+#
+# subroutine hoge( in, & ! Comment blocks between continuation lines are ignored.
+# & out )
+# !
+# ! Comment blocks for the subroutines or functions
+# !
+# character(*),intent(in):: in ! Comment blocks for the arguments.
+# character(*),intent(out),allocatable,target :: in
+# ! Comment blocks can be
+# ! written under Fortran statements.
+#
+# character(32) :: file ! This comment parsed as a variable in below NAMELIST.
+# integer :: id
+#
+# namelist /varinfo_nml/ file, id
+# !
+# ! Comment blocks for the NAMELISTs.
+# ! Information about variables are described above.
+# !
+#
+# ....
+#
+# end subroutine hoge
+#
+# integer function foo( in )
+# !
+# ! This part is considered as comment block.
+#
+# ! Comment blocks under blank lines are ignored.
+# !
+# integer, intent(in):: inA ! This part is considered as comment block.
+#
+# ! This part is ignored.
+#
+# end function foo
+#
+# subroutine hide( in, &
+# & out ) !:nodoc:
+# !
+# ! If "!:nodoc:" is described at end-of-line in subroutine
+# ! statement as above, the subroutine is ignored.
+# ! This assignment can be used to modules, subroutines,
+# ! functions, variables, constants, derived-types,
+# ! defined operators, defined assignments,
+# ! list of imported modules ("use" statement).
+# !
+#
+# ....
+#
+# end subroutine hide
+#
+# end module hogehoge
+
+class RDoc::Parser::F95 < RDoc::Parser
+
+ parse_files_matching(/\.((f|F)9(0|5)|F)$/)
+
+ class Token
+
+ NO_TEXT = "??".freeze
+
+ def initialize(line_no, char_no)
+ @line_no = line_no
+ @char_no = char_no
+ @text = NO_TEXT
+ end
+ # Because we're used in contexts that expect to return a token,
+ # we set the text string and then return ourselves
+ def set_text(text)
+ @text = text
+ self
+ end
+
+ attr_reader :line_no, :char_no, :text
+
+ end
+
+ @@external_aliases = []
+ @@public_methods = []
+
+ ##
+ # "false":: Comments are below source code
+ # "true" :: Comments are upper source code
+
+ COMMENTS_ARE_UPPER = false
+
+ ##
+ # Internal alias message
+
+ INTERNAL_ALIAS_MES = "Alias for"
+
+ ##
+ # External alias message
+
+ EXTERNAL_ALIAS_MES = "The entity is"
+
+ ##
+ # Define code constructs
+
+ def scan
+ # remove private comment
+ remaining_code = remove_private_comments(@content)
+
+ # continuation lines are united to one line
+ remaining_code = united_to_one_line(remaining_code)
+
+ # semicolons are replaced to line feed
+ remaining_code = semicolon_to_linefeed(remaining_code)
+
+ # collect comment for file entity
+ whole_comment, remaining_code = collect_first_comment(remaining_code)
+ @top_level.comment = whole_comment
+
+ # String "remaining_code" is converted to Array "remaining_lines"
+ remaining_lines = remaining_code.split("\n")
+
+ # "module" or "program" parts are parsed (new)
+ #
+ level_depth = 0
+ block_searching_flag = nil
+ block_searching_lines = []
+ pre_comment = []
+ module_program_trailing = ""
+ module_program_name = ""
+ other_block_level_depth = 0
+ other_block_searching_flag = nil
+ remaining_lines.collect!{|line|
+ if !block_searching_flag && !other_block_searching_flag
+ if line =~ /^\s*?module\s+(\w+)\s*?(!.*?)?$/i
+ block_searching_flag = :module
+ block_searching_lines << line
+ module_program_name = $1
+ module_program_trailing = find_comments($2)
+ next false
+ elsif line =~ /^\s*?program\s+(\w+)\s*?(!.*?)?$/i ||
+ line =~ /^\s*?\w/ && !block_start?(line)
+ block_searching_flag = :program
+ block_searching_lines << line
+ module_program_name = $1 || ""
+ module_program_trailing = find_comments($2)
+ next false
+
+ elsif block_start?(line)
+ other_block_searching_flag = true
+ next line
+
+ elsif line =~ /^\s*?!\s?(.*)/
+ pre_comment << line
+ next line
+ else
+ pre_comment = []
+ next line
+ end
+ elsif other_block_searching_flag
+ other_block_level_depth += 1 if block_start?(line)
+ other_block_level_depth -= 1 if block_end?(line)
+ if other_block_level_depth < 0
+ other_block_level_depth = 0
+ other_block_searching_flag = nil
+ end
+ next line
+ end
+
+ block_searching_lines << line
+ level_depth += 1 if block_start?(line)
+ level_depth -= 1 if block_end?(line)
+ if level_depth >= 0
+ next false
+ end
+
+ # "module_program_code" is formatted.
+ # ":nodoc:" flag is checked.
+ #
+ module_program_code = block_searching_lines.join("\n")
+ module_program_code = remove_empty_head_lines(module_program_code)
+ if module_program_trailing =~ /^:nodoc:/
+ # next loop to search next block
+ level_depth = 0
+ block_searching_flag = false
+ block_searching_lines = []
+ pre_comment = []
+ next false
+ end
+
+ # NormalClass is created, and added to @top_level
+ #
+ if block_searching_flag == :module
+ module_name = module_program_name
+ module_code = module_program_code
+ module_trailing = module_program_trailing
+
+ f9x_module = @top_level.add_module NormalClass, module_name
+ f9x_module.record_location @top_level
+
+ @stats.add_module f9x_module
+
+ f9x_comment = COMMENTS_ARE_UPPER ?
+ find_comments(pre_comment.join("\n")) + "\n" + module_trailing :
+ module_trailing + "\n" + find_comments(module_code.sub(/^.*$\n/i, ''))
+ f9x_module.comment = f9x_comment
+ parse_program_or_module(f9x_module, module_code)
+
+ TopLevel.all_files.each do |name, toplevel|
+ if toplevel.include_includes?(module_name, @options.ignore_case)
+ if !toplevel.include_requires?(@file_name, @options.ignore_case)
+ toplevel.add_require(Require.new(@file_name, ""))
+ end
+ end
+ toplevel.each_classmodule{|m|
+ if m.include_includes?(module_name, @options.ignore_case)
+ if !m.include_requires?(@file_name, @options.ignore_case)
+ m.add_require(Require.new(@file_name, ""))
+ end
+ end
+ }
+ end
+ elsif block_searching_flag == :program
+ program_name = module_program_name
+ program_code = module_program_code
+ program_trailing = module_program_trailing
+ # progress "p" # HACK what stats thingy does this correspond to?
+ program_comment = COMMENTS_ARE_UPPER ?
+ find_comments(pre_comment.join("\n")) + "\n" + program_trailing :
+ program_trailing + "\n" + find_comments(program_code.sub(/^.*$\n/i, ''))
+ program_comment = "\n\n= <i>Program</i> <tt>#{program_name}</tt>\n\n" \
+ + program_comment
+ @top_level.comment << program_comment
+ parse_program_or_module(@top_level, program_code, :private)
+ end
+
+ # next loop to search next block
+ level_depth = 0
+ block_searching_flag = false
+ block_searching_lines = []
+ pre_comment = []
+ next false
+ }
+
+ remaining_lines.delete_if{ |line|
+ line == false
+ }
+
+ # External subprograms and functions are parsed
+ #
+ parse_program_or_module(@top_level, remaining_lines.join("\n"),
+ :public, true)
+
+ @top_level
+ end # End of scan
+
+ private
+
+ def parse_program_or_module(container, code,
+ visibility=:public, external=nil)
+ return unless container
+ return unless code
+ remaining_lines = code.split("\n")
+ remaining_code = "#{code}"
+
+ #
+ # Parse variables before "contains" in module
+ #
+ level_depth = 0
+ before_contains_lines = []
+ before_contains_code = nil
+ before_contains_flag = nil
+ remaining_lines.each{ |line|
+ if !before_contains_flag
+ if line =~ /^\s*?module\s+\w+\s*?(!.*?)?$/i
+ before_contains_flag = true
+ end
+ else
+ break if line =~ /^\s*?contains\s*?(!.*?)?$/i
+ level_depth += 1 if block_start?(line)
+ level_depth -= 1 if block_end?(line)
+ break if level_depth < 0
+ before_contains_lines << line
+ end
+ }
+ before_contains_code = before_contains_lines.join("\n")
+ if before_contains_code
+ before_contains_code.gsub!(/^\s*?interface\s+.*?\s+end\s+interface.*?$/im, "")
+ before_contains_code.gsub!(/^\s*?type[\s\,]+.*?\s+end\s+type.*?$/im, "")
+ end
+
+ #
+ # Parse global "use"
+ #
+ use_check_code = "#{before_contains_code}"
+ cascaded_modules_list = []
+ while use_check_code =~ /^\s*?use\s+(\w+)(.*?)(!.*?)?$/i
+ use_check_code = $~.pre_match
+ use_check_code << $~.post_match
+ used_mod_name = $1.strip.chomp
+ used_list = $2 || ""
+ used_trailing = $3 || ""
+ next if used_trailing =~ /!:nodoc:/
+ if !container.include_includes?(used_mod_name, @options.ignore_case)
+ # progress "." # HACK what stats thingy does this correspond to?
+ container.add_include Include.new(used_mod_name, "")
+ end
+ if ! (used_list =~ /\,\s*?only\s*?:/i )
+ cascaded_modules_list << "\#" + used_mod_name
+ end
+ end
+
+ #
+ # Parse public and private, and store information.
+ # This information is used when "add_method" and
+ # "set_visibility_for" are called.
+ #
+ visibility_default, visibility_info =
+ parse_visibility(remaining_lines.join("\n"), visibility, container)
+ @@public_methods.concat visibility_info
+ if visibility_default == :public
+ if !cascaded_modules_list.empty?
+ cascaded_modules =
+ Attr.new("Cascaded Modules",
+ "Imported modules all of whose components are published again",
+ "",
+ cascaded_modules_list.join(", "))
+ container.add_attribute(cascaded_modules)
+ end
+ end
+
+ #
+ # Check rename elements
+ #
+ use_check_code = "#{before_contains_code}"
+ while use_check_code =~ /^\s*?use\s+(\w+)\s*?\,(.+)$/i
+ use_check_code = $~.pre_match
+ use_check_code << $~.post_match
+ used_mod_name = $1.strip.chomp
+ used_elements = $2.sub(/\s*?only\s*?:\s*?/i, '')
+ used_elements.split(",").each{ |used|
+ if /\s*?(\w+)\s*?=>\s*?(\w+)\s*?/ =~ used
+ local = $1
+ org = $2
+ @@public_methods.collect!{ |pub_meth|
+ if local == pub_meth["name"] ||
+ local.upcase == pub_meth["name"].upcase &&
+ @options.ignore_case
+ pub_meth["name"] = org
+ pub_meth["local_name"] = local
+ end
+ pub_meth
+ }
+ end
+ }
+ end
+
+ #
+ # Parse private "use"
+ #
+ use_check_code = remaining_lines.join("\n")
+ while use_check_code =~ /^\s*?use\s+(\w+)(.*?)(!.*?)?$/i
+ use_check_code = $~.pre_match
+ use_check_code << $~.post_match
+ used_mod_name = $1.strip.chomp
+ used_trailing = $3 || ""
+ next if used_trailing =~ /!:nodoc:/
+ if !container.include_includes?(used_mod_name, @options.ignore_case)
+ # progress "." # HACK what stats thingy does this correspond to?
+ container.add_include Include.new(used_mod_name, "")
+ end
+ end
+
+ container.each_includes{ |inc|
+ TopLevel.all_files.each do |name, toplevel|
+ indicated_mod = toplevel.find_symbol(inc.name,
+ nil, @options.ignore_case)
+ if indicated_mod
+ indicated_name = indicated_mod.parent.file_relative_name
+ if !container.include_requires?(indicated_name, @options.ignore_case)
+ container.add_require(Require.new(indicated_name, ""))
+ end
+ break
+ end
+ end
+ }
+
+ #
+ # Parse derived-types definitions
+ #
+ derived_types_comment = ""
+ remaining_code = remaining_lines.join("\n")
+ while remaining_code =~ /^\s*?
+ type[\s\,]+(public|private)?\s*?(::)?\s*?
+ (\w+)\s*?(!.*?)?$
+ (.*?)
+ ^\s*?end\s+type.*?$
+ /imx
+ remaining_code = $~.pre_match
+ remaining_code << $~.post_match
+ typename = $3.chomp.strip
+ type_elements = $5 || ""
+ type_code = remove_empty_head_lines($&)
+ type_trailing = find_comments($4)
+ next if type_trailing =~ /^:nodoc:/
+ type_visibility = $1
+ type_comment = COMMENTS_ARE_UPPER ?
+ find_comments($~.pre_match) + "\n" + type_trailing :
+ type_trailing + "\n" + find_comments(type_code.sub(/^.*$\n/i, ''))
+ type_element_visibility_public = true
+ type_code.split("\n").each{ |line|
+ if /^\s*?private\s*?$/ =~ line
+ type_element_visibility_public = nil
+ break
+ end
+ } if type_code
+
+ args_comment = ""
+ type_args_info = nil
+
+ if @options.show_all
+ args_comment = find_arguments(nil, type_code, true)
+ else
+ type_public_args_list = []
+ type_args_info = definition_info(type_code)
+ type_args_info.each{ |arg|
+ arg_is_public = type_element_visibility_public
+ arg_is_public = true if arg.include_attr?("public")
+ arg_is_public = nil if arg.include_attr?("private")
+ type_public_args_list << arg.varname if arg_is_public
+ }
+ args_comment = find_arguments(type_public_args_list, type_code)
+ end
+
+ type = AnyMethod.new("type #{typename}", typename)
+ type.singleton = false
+ type.params = ""
+ type.comment = "<b><em> Derived Type </em></b> :: <tt></tt>\n"
+ type.comment << args_comment if args_comment
+ type.comment << type_comment if type_comment
+
+ @stats.add_method type
+
+ container.add_method type
+
+ set_visibility(container, typename, visibility_default, @@public_methods)
+
+ if type_visibility
+ type_visibility.gsub!(/\s/,'')
+ type_visibility.gsub!(/\,/,'')
+ type_visibility.gsub!(/:/,'')
+ type_visibility.downcase!
+ if type_visibility == "public"
+ container.set_visibility_for([typename], :public)
+ elsif type_visibility == "private"
+ container.set_visibility_for([typename], :private)
+ end
+ end
+
+ check_public_methods(type, container.name)
+
+ if @options.show_all
+ derived_types_comment << ", " unless derived_types_comment.empty?
+ derived_types_comment << typename
+ else
+ if type.visibility == :public
+ derived_types_comment << ", " unless derived_types_comment.empty?
+ derived_types_comment << typename
+ end
+ end
+
+ end
+
+ if !derived_types_comment.empty?
+ derived_types_table =
+ Attr.new("Derived Types", "Derived_Types", "",
+ derived_types_comment)
+ container.add_attribute(derived_types_table)
+ end
+
+ #
+ # move interface scope
+ #
+ interface_code = ""
+ while remaining_code =~ /^\s*?
+ interface(
+ \s+\w+ |
+ \s+operator\s*?\(.*?\) |
+ \s+assignment\s*?\(\s*?=\s*?\)
+ )?\s*?$
+ (.*?)
+ ^\s*?end\s+interface.*?$
+ /imx
+ interface_code << remove_empty_head_lines($&) + "\n"
+ remaining_code = $~.pre_match
+ remaining_code << $~.post_match
+ end
+
+ #
+ # Parse global constants or variables in modules
+ #
+ const_var_defs = definition_info(before_contains_code)
+ const_var_defs.each{|defitem|
+ next if defitem.nodoc
+ const_or_var_type = "Variable"
+ const_or_var_progress = "v"
+ if defitem.include_attr?("parameter")
+ const_or_var_type = "Constant"
+ const_or_var_progress = "c"
+ end
+ const_or_var = AnyMethod.new(const_or_var_type, defitem.varname)
+ const_or_var.singleton = false
+ const_or_var.params = ""
+ self_comment = find_arguments([defitem.varname], before_contains_code)
+ const_or_var.comment = "<b><em>" + const_or_var_type + "</em></b> :: <tt></tt>\n"
+ const_or_var.comment << self_comment if self_comment
+
+ @stats.add_method const_or_var_progress
+
+ container.add_method const_or_var
+
+ set_visibility(container, defitem.varname, visibility_default, @@public_methods)
+
+ if defitem.include_attr?("public")
+ container.set_visibility_for([defitem.varname], :public)
+ elsif defitem.include_attr?("private")
+ container.set_visibility_for([defitem.varname], :private)
+ end
+
+ check_public_methods(const_or_var, container.name)
+
+ } if const_var_defs
+
+ remaining_lines = remaining_code.split("\n")
+
+ # "subroutine" or "function" parts are parsed (new)
+ #
+ level_depth = 0
+ block_searching_flag = nil
+ block_searching_lines = []
+ pre_comment = []
+ procedure_trailing = ""
+ procedure_name = ""
+ procedure_params = ""
+ procedure_prefix = ""
+ procedure_result_arg = ""
+ procedure_type = ""
+ contains_lines = []
+ contains_flag = nil
+ remaining_lines.collect!{|line|
+ if !block_searching_flag
+ # subroutine
+ if line =~ /^\s*?
+ (recursive|pure|elemental)?\s*?
+ subroutine\s+(\w+)\s*?(\(.*?\))?\s*?(!.*?)?$
+ /ix
+ block_searching_flag = :subroutine
+ block_searching_lines << line
+
+ procedure_name = $2.chomp.strip
+ procedure_params = $3 || ""
+ procedure_prefix = $1 || ""
+ procedure_trailing = $4 || "!"
+ next false
+
+ # function
+ elsif line =~ /^\s*?
+ (recursive|pure|elemental)?\s*?
+ (
+ character\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | type\s*?\([\w\s]+?\)\s+
+ | integer\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | real\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | double\s+precision\s+
+ | logical\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | complex\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ )?
+ function\s+(\w+)\s*?
+ (\(.*?\))?(\s+result\((.*?)\))?\s*?(!.*?)?$
+ /ix
+ block_searching_flag = :function
+ block_searching_lines << line
+
+ procedure_prefix = $1 || ""
+ procedure_type = $2 ? $2.chomp.strip : nil
+ procedure_name = $8.chomp.strip
+ procedure_params = $9 || ""
+ procedure_result_arg = $11 ? $11.chomp.strip : procedure_name
+ procedure_trailing = $12 || "!"
+ next false
+ elsif line =~ /^\s*?!\s?(.*)/
+ pre_comment << line
+ next line
+ else
+ pre_comment = []
+ next line
+ end
+ end
+ contains_flag = true if line =~ /^\s*?contains\s*?(!.*?)?$/
+ block_searching_lines << line
+ contains_lines << line if contains_flag
+
+ level_depth += 1 if block_start?(line)
+ level_depth -= 1 if block_end?(line)
+ if level_depth >= 0
+ next false
+ end
+
+ # "procedure_code" is formatted.
+ # ":nodoc:" flag is checked.
+ #
+ procedure_code = block_searching_lines.join("\n")
+ procedure_code = remove_empty_head_lines(procedure_code)
+ if procedure_trailing =~ /^!:nodoc:/
+ # next loop to search next block
+ level_depth = 0
+ block_searching_flag = nil
+ block_searching_lines = []
+ pre_comment = []
+ procedure_trailing = ""
+ procedure_name = ""
+ procedure_params = ""
+ procedure_prefix = ""
+ procedure_result_arg = ""
+ procedure_type = ""
+ contains_lines = []
+ contains_flag = nil
+ next false
+ end
+
+ # AnyMethod is created, and added to container
+ #
+ subroutine_function = nil
+ if block_searching_flag == :subroutine
+ subroutine_prefix = procedure_prefix
+ subroutine_name = procedure_name
+ subroutine_params = procedure_params
+ subroutine_trailing = procedure_trailing
+ subroutine_code = procedure_code
+
+ subroutine_comment = COMMENTS_ARE_UPPER ?
+ pre_comment.join("\n") + "\n" + subroutine_trailing :
+ subroutine_trailing + "\n" + subroutine_code.sub(/^.*$\n/i, '')
+ subroutine = AnyMethod.new("subroutine", subroutine_name)
+ parse_subprogram(subroutine, subroutine_params,
+ subroutine_comment, subroutine_code,
+ before_contains_code, nil, subroutine_prefix)
+
+ @stats.add_method subroutine
+
+ container.add_method subroutine
+ subroutine_function = subroutine
+
+ elsif block_searching_flag == :function
+ function_prefix = procedure_prefix
+ function_type = procedure_type
+ function_name = procedure_name
+ function_params_org = procedure_params
+ function_result_arg = procedure_result_arg
+ function_trailing = procedure_trailing
+ function_code_org = procedure_code
+
+ function_comment = COMMENTS_ARE_UPPER ?
+ pre_comment.join("\n") + "\n" + function_trailing :
+ function_trailing + "\n " + function_code_org.sub(/^.*$\n/i, '')
+
+ function_code = "#{function_code_org}"
+ if function_type
+ function_code << "\n" + function_type + " :: " + function_result_arg
+ end
+
+ function_params =
+ function_params_org.sub(/^\(/, "\(#{function_result_arg}, ")
+
+ function = AnyMethod.new("function", function_name)
+ parse_subprogram(function, function_params,
+ function_comment, function_code,
+ before_contains_code, true, function_prefix)
+
+ # Specific modification due to function
+ function.params.sub!(/\(\s*?#{function_result_arg}\s*?,\s*?/, "\( ")
+ function.params << " result(" + function_result_arg + ")"
+ function.start_collecting_tokens
+ function.add_token Token.new(1,1).set_text(function_code_org)
+
+ @stats.add_method function
+
+ container.add_method function
+ subroutine_function = function
+
+ end
+
+ # The visibility of procedure is specified
+ #
+ set_visibility(container, procedure_name,
+ visibility_default, @@public_methods)
+
+ # The alias for this procedure from external modules
+ #
+ check_external_aliases(procedure_name,
+ subroutine_function.params,
+ subroutine_function.comment, subroutine_function) if external
+ check_public_methods(subroutine_function, container.name)
+
+
+ # contains_lines are parsed as private procedures
+ if contains_flag
+ parse_program_or_module(container,
+ contains_lines.join("\n"), :private)
+ end
+
+ # next loop to search next block
+ level_depth = 0
+ block_searching_flag = nil
+ block_searching_lines = []
+ pre_comment = []
+ procedure_trailing = ""
+ procedure_name = ""
+ procedure_params = ""
+ procedure_prefix = ""
+ procedure_result_arg = ""
+ contains_lines = []
+ contains_flag = nil
+ next false
+ } # End of remaining_lines.collect!{|line|
+
+ # Array remains_lines is converted to String remains_code again
+ #
+ remaining_code = remaining_lines.join("\n")
+
+ #
+ # Parse interface
+ #
+ interface_scope = false
+ generic_name = ""
+ interface_code.split("\n").each{ |line|
+ if /^\s*?
+ interface(
+ \s+\w+|
+ \s+operator\s*?\(.*?\)|
+ \s+assignment\s*?\(\s*?=\s*?\)
+ )?
+ \s*?(!.*?)?$
+ /ix =~ line
+ generic_name = $1 ? $1.strip.chomp : nil
+ interface_trailing = $2 || "!"
+ interface_scope = true
+ interface_scope = false if interface_trailing =~ /!:nodoc:/
+# if generic_name =~ /operator\s*?\((.*?)\)/i
+# operator_name = $1
+# if operator_name && !operator_name.empty?
+# generic_name = "#{operator_name}"
+# end
+# end
+# if generic_name =~ /assignment\s*?\((.*?)\)/i
+# assignment_name = $1
+# if assignment_name && !assignment_name.empty?
+# generic_name = "#{assignment_name}"
+# end
+# end
+ end
+ if /^\s*?end\s+interface/i =~ line
+ interface_scope = false
+ generic_name = nil
+ end
+ # internal alias
+ if interface_scope && /^\s*?module\s+procedure\s+(.*?)(!.*?)?$/i =~ line
+ procedures = $1.strip.chomp
+ procedures_trailing = $2 || "!"
+ next if procedures_trailing =~ /!:nodoc:/
+ procedures.split(",").each{ |proc|
+ proc.strip!
+ proc.chomp!
+ next if generic_name == proc || !generic_name
+ old_meth = container.find_symbol(proc, nil, @options.ignore_case)
+ next if !old_meth
+ nolink = old_meth.visibility == :private ? true : nil
+ nolink = nil if @options.show_all
+ new_meth =
+ initialize_external_method(generic_name, proc,
+ old_meth.params, nil,
+ old_meth.comment,
+ old_meth.clone.token_stream[0].text,
+ true, nolink)
+ new_meth.singleton = old_meth.singleton
+
+ @stats.add_method new_meth
+
+ container.add_method new_meth
+
+ set_visibility(container, generic_name, visibility_default, @@public_methods)
+
+ check_public_methods(new_meth, container.name)
+
+ }
+ end
+
+ # external aliases
+ if interface_scope
+ # subroutine
+ proc = nil
+ params = nil
+ procedures_trailing = nil
+ if line =~ /^\s*?
+ (recursive|pure|elemental)?\s*?
+ subroutine\s+(\w+)\s*?(\(.*?\))?\s*?(!.*?)?$
+ /ix
+ proc = $2.chomp.strip
+ generic_name = proc unless generic_name
+ params = $3 || ""
+ procedures_trailing = $4 || "!"
+
+ # function
+ elsif line =~ /^\s*?
+ (recursive|pure|elemental)?\s*?
+ (
+ character\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | type\s*?\([\w\s]+?\)\s+
+ | integer\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | real\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | double\s+precision\s+
+ | logical\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | complex\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ )?
+ function\s+(\w+)\s*?
+ (\(.*?\))?(\s+result\((.*?)\))?\s*?(!.*?)?$
+ /ix
+ proc = $8.chomp.strip
+ generic_name = proc unless generic_name
+ params = $9 || ""
+ procedures_trailing = $12 || "!"
+ else
+ next
+ end
+ next if procedures_trailing =~ /!:nodoc:/
+ indicated_method = nil
+ indicated_file = nil
+ TopLevel.all_files.each do |name, toplevel|
+ indicated_method = toplevel.find_local_symbol(proc, @options.ignore_case)
+ indicated_file = name
+ break if indicated_method
+ end
+
+ if indicated_method
+ external_method =
+ initialize_external_method(generic_name, proc,
+ indicated_method.params,
+ indicated_file,
+ indicated_method.comment)
+
+ @stats.add_method external_method
+
+ container.add_method external_method
+ set_visibility(container, generic_name, visibility_default, @@public_methods)
+ if !container.include_requires?(indicated_file, @options.ignore_case)
+ container.add_require(Require.new(indicated_file, ""))
+ end
+ check_public_methods(external_method, container.name)
+
+ else
+ @@external_aliases << {
+ "new_name" => generic_name,
+ "old_name" => proc,
+ "file_or_module" => container,
+ "visibility" => find_visibility(container, generic_name, @@public_methods) || visibility_default
+ }
+ end
+ end
+
+ } if interface_code # End of interface_code.split("\n").each ...
+
+ #
+ # Already imported methods are removed from @@public_methods.
+ # Remainders are assumed to be imported from other modules.
+ #
+ @@public_methods.delete_if{ |method| method["entity_is_discovered"]}
+
+ @@public_methods.each{ |pub_meth|
+ next unless pub_meth["file_or_module"].name == container.name
+ pub_meth["used_modules"].each{ |used_mod|
+ TopLevel.all_classes_and_modules.each{ |modules|
+ if modules.name == used_mod ||
+ modules.name.upcase == used_mod.upcase &&
+ @options.ignore_case
+ modules.method_list.each{ |meth|
+ if meth.name == pub_meth["name"] ||
+ meth.name.upcase == pub_meth["name"].upcase &&
+ @options.ignore_case
+ new_meth = initialize_public_method(meth,
+ modules.name)
+ if pub_meth["local_name"]
+ new_meth.name = pub_meth["local_name"]
+ end
+
+ @stats.add_method new_meth
+
+ container.add_method new_meth
+ end
+ }
+ end
+ }
+ }
+ }
+
+ container
+ end # End of parse_program_or_module
+
+ ##
+ # Parse arguments, comment, code of subroutine and function. Return
+ # AnyMethod object.
+
+ def parse_subprogram(subprogram, params, comment, code,
+ before_contains=nil, function=nil, prefix=nil)
+ subprogram.singleton = false
+ prefix = "" if !prefix
+ arguments = params.sub(/\(/, "").sub(/\)/, "").split(",") if params
+ args_comment, params_opt =
+ find_arguments(arguments, code.sub(/^s*?contains\s*?(!.*?)?$.*/im, ""),
+ nil, nil, true)
+ params_opt = "( " + params_opt + " ) " if params_opt
+ subprogram.params = params_opt || ""
+ namelist_comment = find_namelists(code, before_contains)
+
+ block_comment = find_comments comment
+ if function
+ subprogram.comment = "<b><em> Function </em></b> :: <em>#{prefix}</em>\n"
+ else
+ subprogram.comment = "<b><em> Subroutine </em></b> :: <em>#{prefix}</em>\n"
+ end
+ subprogram.comment << args_comment if args_comment
+ subprogram.comment << block_comment if block_comment
+ subprogram.comment << namelist_comment if namelist_comment
+
+ # For output source code
+ subprogram.start_collecting_tokens
+ subprogram.add_token Token.new(1,1).set_text(code)
+
+ subprogram
+ end
+
+ ##
+ # Collect comment for file entity
+
+ def collect_first_comment(body)
+ comment = ""
+ not_comment = ""
+ comment_start = false
+ comment_end = false
+ body.split("\n").each{ |line|
+ if comment_end
+ not_comment << line
+ not_comment << "\n"
+ elsif /^\s*?!\s?(.*)$/i =~ line
+ comment_start = true
+ comment << $1
+ comment << "\n"
+ elsif /^\s*?$/i =~ line
+ comment_end = true if comment_start && COMMENTS_ARE_UPPER
+ else
+ comment_end = true
+ not_comment << line
+ not_comment << "\n"
+ end
+ }
+ return comment, not_comment
+ end
+
+
+ ##
+ # Return comments of definitions of arguments
+ #
+ # If "all" argument is true, information of all arguments are returned.
+ #
+ # If "modified_params" is true, list of arguments are decorated, for
+ # example, optional arguments are parenthetic as "[arg]".
+
+ def find_arguments(args, text, all=nil, indent=nil, modified_params=nil)
+ return unless args || all
+ indent = "" unless indent
+ args = ["all"] if all
+ params = "" if modified_params
+ comma = ""
+ return unless text
+ args_rdocforms = "\n"
+ remaining_lines = "#{text}"
+ definitions = definition_info(remaining_lines)
+ args.each{ |arg|
+ arg.strip!
+ arg.chomp!
+ definitions.each { |defitem|
+ if arg == defitem.varname.strip.chomp || all
+ args_rdocforms << <<-"EOF"
+
+#{indent}<tt><b>#{defitem.varname.chomp.strip}#{defitem.arraysuffix}</b> #{defitem.inivalue}</tt> ::
+#{indent} <tt>#{defitem.types.chomp.strip}</tt>
+EOF
+ if !defitem.comment.chomp.strip.empty?
+ comment = ""
+ defitem.comment.split("\n").each{ |line|
+ comment << " " + line + "\n"
+ }
+ args_rdocforms << <<-"EOF"
+
+#{indent} <tt></tt> ::
+#{indent} <tt></tt>
+#{indent} #{comment.chomp.strip}
+EOF
+ end
+
+ if modified_params
+ if defitem.include_attr?("optional")
+ params << "#{comma}[#{arg}]"
+ else
+ params << "#{comma}#{arg}"
+ end
+ comma = ", "
+ end
+ end
+ }
+ }
+ if modified_params
+ return args_rdocforms, params
+ else
+ return args_rdocforms
+ end
+ end
+
+ ##
+ # Return comments of definitions of namelists
+
+ def find_namelists(text, before_contains=nil)
+ return nil if !text
+ result = ""
+ lines = "#{text}"
+ before_contains = "" if !before_contains
+ while lines =~ /^\s*?namelist\s+\/\s*?(\w+)\s*?\/([\s\w\,]+)$/i
+ lines = $~.post_match
+ nml_comment = COMMENTS_ARE_UPPER ?
+ find_comments($~.pre_match) : find_comments($~.post_match)
+ nml_name = $1
+ nml_args = $2.split(",")
+ result << "\n\n=== NAMELIST <tt><b>" + nml_name + "</tt></b>\n\n"
+ result << nml_comment + "\n" if nml_comment
+ if lines.split("\n")[0] =~ /^\//i
+ lines = "namelist " + lines
+ end
+ result << find_arguments(nml_args, "#{text}" + "\n" + before_contains)
+ end
+ return result
+ end
+
+ ##
+ # Comments just after module or subprogram, or arguments are returned. If
+ # "COMMENTS_ARE_UPPER" is true, comments just before modules or subprograms
+ # are returnd
+
+ def find_comments text
+ return "" unless text
+ lines = text.split("\n")
+ lines.reverse! if COMMENTS_ARE_UPPER
+ comment_block = Array.new
+ lines.each do |line|
+ break if line =~ /^\s*?\w/ || line =~ /^\s*?$/
+ if COMMENTS_ARE_UPPER
+ comment_block.unshift line.sub(/^\s*?!\s?/,"")
+ else
+ comment_block.push line.sub(/^\s*?!\s?/,"")
+ end
+ end
+ nice_lines = comment_block.join("\n").split "\n\s*?\n"
+ nice_lines[0] ||= ""
+ nice_lines.shift
+ end
+
+ ##
+ # Create method for internal alias
+
+ def initialize_public_method(method, parent)
+ return if !method || !parent
+
+ new_meth = AnyMethod.new("External Alias for module", method.name)
+ new_meth.singleton = method.singleton
+ new_meth.params = method.params.clone
+ new_meth.comment = remove_trailing_alias(method.comment.clone)
+ new_meth.comment << "\n\n#{EXTERNAL_ALIAS_MES} #{parent.strip.chomp}\##{method.name}"
+
+ return new_meth
+ end
+
+ ##
+ # Create method for external alias
+ #
+ # If argument "internal" is true, file is ignored.
+
+ def initialize_external_method(new, old, params, file, comment, token=nil,
+ internal=nil, nolink=nil)
+ return nil unless new || old
+
+ if internal
+ external_alias_header = "#{INTERNAL_ALIAS_MES} "
+ external_alias_text = external_alias_header + old
+ elsif file
+ external_alias_header = "#{EXTERNAL_ALIAS_MES} "
+ external_alias_text = external_alias_header + file + "#" + old
+ else
+ return nil
+ end
+ external_meth = AnyMethod.new(external_alias_text, new)
+ external_meth.singleton = false
+ external_meth.params = params
+ external_comment = remove_trailing_alias(comment) + "\n\n" if comment
+ external_meth.comment = external_comment || ""
+ if nolink && token
+ external_meth.start_collecting_tokens
+ external_meth.add_token Token.new(1,1).set_text(token)
+ else
+ external_meth.comment << external_alias_text
+ end
+
+ return external_meth
+ end
+
+ ##
+ # Parse visibility
+
+ def parse_visibility(code, default, container)
+ result = []
+ visibility_default = default || :public
+
+ used_modules = []
+ container.includes.each{|i| used_modules << i.name} if container
+
+ remaining_code = code.gsub(/^\s*?type[\s\,]+.*?\s+end\s+type.*?$/im, "")
+ remaining_code.split("\n").each{ |line|
+ if /^\s*?private\s*?$/ =~ line
+ visibility_default = :private
+ break
+ end
+ } if remaining_code
+
+ remaining_code.split("\n").each{ |line|
+ if /^\s*?private\s*?(::)?\s+(.*)\s*?(!.*?)?/i =~ line
+ methods = $2.sub(/!.*$/, '')
+ methods.split(",").each{ |meth|
+ meth.sub!(/!.*$/, '')
+ meth.gsub!(/:/, '')
+ result << {
+ "name" => meth.chomp.strip,
+ "visibility" => :private,
+ "used_modules" => used_modules.clone,
+ "file_or_module" => container,
+ "entity_is_discovered" => nil,
+ "local_name" => nil
+ }
+ }
+ elsif /^\s*?public\s*?(::)?\s+(.*)\s*?(!.*?)?/i =~ line
+ methods = $2.sub(/!.*$/, '')
+ methods.split(",").each{ |meth|
+ meth.sub!(/!.*$/, '')
+ meth.gsub!(/:/, '')
+ result << {
+ "name" => meth.chomp.strip,
+ "visibility" => :public,
+ "used_modules" => used_modules.clone,
+ "file_or_module" => container,
+ "entity_is_discovered" => nil,
+ "local_name" => nil
+ }
+ }
+ end
+ } if remaining_code
+
+ if container
+ result.each{ |vis_info|
+ vis_info["parent"] = container.name
+ }
+ end
+
+ return visibility_default, result
+ end
+
+ ##
+ # Set visibility
+ #
+ # "subname" element of "visibility_info" is deleted.
+
+ def set_visibility(container, subname, visibility_default, visibility_info)
+ return unless container || subname || visibility_default || visibility_info
+ not_found = true
+ visibility_info.collect!{ |info|
+ if info["name"] == subname ||
+ @options.ignore_case && info["name"].upcase == subname.upcase
+ if info["file_or_module"].name == container.name
+ container.set_visibility_for([subname], info["visibility"])
+ info["entity_is_discovered"] = true
+ not_found = false
+ end
+ end
+ info
+ }
+ if not_found
+ return container.set_visibility_for([subname], visibility_default)
+ else
+ return container
+ end
+ end
+
+ ##
+ # Find visibility
+
+ def find_visibility(container, subname, visibility_info)
+ return nil if !subname || !visibility_info
+ visibility_info.each{ |info|
+ if info["name"] == subname ||
+ @options.ignore_case && info["name"].upcase == subname.upcase
+ if info["parent"] == container.name
+ return info["visibility"]
+ end
+ end
+ }
+ return nil
+ end
+
+ ##
+ # Check external aliases
+
+ def check_external_aliases(subname, params, comment, test=nil)
+ @@external_aliases.each{ |alias_item|
+ if subname == alias_item["old_name"] ||
+ subname.upcase == alias_item["old_name"].upcase &&
+ @options.ignore_case
+
+ new_meth = initialize_external_method(alias_item["new_name"],
+ subname, params, @file_name,
+ comment)
+ new_meth.visibility = alias_item["visibility"]
+
+ @stats.add_method new_meth
+
+ alias_item["file_or_module"].add_method(new_meth)
+
+ if !alias_item["file_or_module"].include_requires?(@file_name, @options.ignore_case)
+ alias_item["file_or_module"].add_require(Require.new(@file_name, ""))
+ end
+ end
+ }
+ end
+
+ ##
+ # Check public_methods
+
+ def check_public_methods(method, parent)
+ return if !method || !parent
+ @@public_methods.each{ |alias_item|
+ parent_is_used_module = nil
+ alias_item["used_modules"].each{ |used_module|
+ if used_module == parent ||
+ used_module.upcase == parent.upcase &&
+ @options.ignore_case
+ parent_is_used_module = true
+ end
+ }
+ next if !parent_is_used_module
+
+ if method.name == alias_item["name"] ||
+ method.name.upcase == alias_item["name"].upcase &&
+ @options.ignore_case
+
+ new_meth = initialize_public_method(method, parent)
+ if alias_item["local_name"]
+ new_meth.name = alias_item["local_name"]
+ end
+
+ @stats.add_method new_meth
+
+ alias_item["file_or_module"].add_method new_meth
+ end
+ }
+ end
+
+ ##
+ # Continuous lines are united.
+ #
+ # Comments in continuous lines are removed.
+
+ def united_to_one_line(f90src)
+ return "" unless f90src
+ lines = f90src.split("\n")
+ previous_continuing = false
+ now_continuing = false
+ body = ""
+ lines.each{ |line|
+ words = line.split("")
+ next if words.empty? && previous_continuing
+ commentout = false
+ brank_flag = true ; brank_char = ""
+ squote = false ; dquote = false
+ ignore = false
+ words.collect! { |char|
+ if previous_continuing && brank_flag
+ now_continuing = true
+ ignore = true
+ case char
+ when "!" ; break
+ when " " ; brank_char << char ; next ""
+ when "&"
+ brank_flag = false
+ now_continuing = false
+ next ""
+ else
+ brank_flag = false
+ now_continuing = false
+ ignore = false
+ next brank_char + char
+ end
+ end
+ ignore = false
+
+ if now_continuing
+ next ""
+ elsif !(squote) && !(dquote) && !(commentout)
+ case char
+ when "!" ; commentout = true ; next char
+ when "\""; dquote = true ; next char
+ when "\'"; squote = true ; next char
+ when "&" ; now_continuing = true ; next ""
+ else next char
+ end
+ elsif commentout
+ next char
+ elsif squote
+ case char
+ when "\'"; squote = false ; next char
+ else next char
+ end
+ elsif dquote
+ case char
+ when "\""; dquote = false ; next char
+ else next char
+ end
+ end
+ }
+ if !ignore && !previous_continuing || !brank_flag
+ if previous_continuing
+ body << words.join("")
+ else
+ body << "\n" + words.join("")
+ end
+ end
+ previous_continuing = now_continuing ? true : nil
+ now_continuing = nil
+ }
+ return body
+ end
+
+
+ ##
+ # Continuous line checker
+
+ def continuous_line?(line)
+ continuous = false
+ if /&\s*?(!.*)?$/ =~ line
+ continuous = true
+ if comment_out?($~.pre_match)
+ continuous = false
+ end
+ end
+ return continuous
+ end
+
+ ##
+ # Comment out checker
+
+ def comment_out?(line)
+ return nil unless line
+ commentout = false
+ squote = false ; dquote = false
+ line.split("").each { |char|
+ if !(squote) && !(dquote)
+ case char
+ when "!" ; commentout = true ; break
+ when "\""; dquote = true
+ when "\'"; squote = true
+ else next
+ end
+ elsif squote
+ case char
+ when "\'"; squote = false
+ else next
+ end
+ elsif dquote
+ case char
+ when "\""; dquote = false
+ else next
+ end
+ end
+ }
+ return commentout
+ end
+
+ ##
+ # Semicolons are replaced to line feed.
+
+ def semicolon_to_linefeed(text)
+ return "" unless text
+ lines = text.split("\n")
+ lines.collect!{ |line|
+ words = line.split("")
+ commentout = false
+ squote = false ; dquote = false
+ words.collect! { |char|
+ if !(squote) && !(dquote) && !(commentout)
+ case char
+ when "!" ; commentout = true ; next char
+ when "\""; dquote = true ; next char
+ when "\'"; squote = true ; next char
+ when ";" ; "\n"
+ else next char
+ end
+ elsif commentout
+ next char
+ elsif squote
+ case char
+ when "\'"; squote = false ; next char
+ else next char
+ end
+ elsif dquote
+ case char
+ when "\""; dquote = false ; next char
+ else next char
+ end
+ end
+ }
+ words.join("")
+ }
+ return lines.join("\n")
+ end
+
+ ##
+ # Which "line" is start of block (module, program, block data, subroutine,
+ # function) statement ?
+
+ def block_start?(line)
+ return nil if !line
+
+ if line =~ /^\s*?module\s+(\w+)\s*?(!.*?)?$/i ||
+ line =~ /^\s*?program\s+(\w+)\s*?(!.*?)?$/i ||
+ line =~ /^\s*?block\s+data(\s+\w+)?\s*?(!.*?)?$/i ||
+ line =~ \
+ /^\s*?
+ (recursive|pure|elemental)?\s*?
+ subroutine\s+(\w+)\s*?(\(.*?\))?\s*?(!.*?)?$
+ /ix ||
+ line =~ \
+ /^\s*?
+ (recursive|pure|elemental)?\s*?
+ (
+ character\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | type\s*?\([\w\s]+?\)\s+
+ | integer\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | real\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | double\s+precision\s+
+ | logical\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ | complex\s*?(\([\w\s\=\(\)\*]+?\))?\s+
+ )?
+ function\s+(\w+)\s*?
+ (\(.*?\))?(\s+result\((.*?)\))?\s*?(!.*?)?$
+ /ix
+ return true
+ end
+
+ return nil
+ end
+
+ ##
+ # Which "line" is end of block (module, program, block data, subroutine,
+ # function) statement ?
+
+ def block_end?(line)
+ return nil if !line
+
+ if line =~ /^\s*?end\s*?(!.*?)?$/i ||
+ line =~ /^\s*?end\s+module(\s+\w+)?\s*?(!.*?)?$/i ||
+ line =~ /^\s*?end\s+program(\s+\w+)?\s*?(!.*?)?$/i ||
+ line =~ /^\s*?end\s+block\s+data(\s+\w+)?\s*?(!.*?)?$/i ||
+ line =~ /^\s*?end\s+subroutine(\s+\w+)?\s*?(!.*?)?$/i ||
+ line =~ /^\s*?end\s+function(\s+\w+)?\s*?(!.*?)?$/i
+ return true
+ end
+
+ return nil
+ end
+
+ ##
+ # Remove "Alias for" in end of comments
+
+ def remove_trailing_alias(text)
+ return "" if !text
+ lines = text.split("\n").reverse
+ comment_block = Array.new
+ checked = false
+ lines.each do |line|
+ if !checked
+ if /^\s?#{INTERNAL_ALIAS_MES}/ =~ line ||
+ /^\s?#{EXTERNAL_ALIAS_MES}/ =~ line
+ checked = true
+ next
+ end
+ end
+ comment_block.unshift line
+ end
+ nice_lines = comment_block.join("\n")
+ nice_lines ||= ""
+ return nice_lines
+ end
+
+ ##
+ # Empty lines in header are removed
+
+ def remove_empty_head_lines(text)
+ return "" unless text
+ lines = text.split("\n")
+ header = true
+ lines.delete_if{ |line|
+ header = false if /\S/ =~ line
+ header && /^\s*?$/ =~ line
+ }
+ lines.join("\n")
+ end
+
+ ##
+ # header marker "=", "==", ... are removed
+
+ def remove_header_marker(text)
+ return text.gsub(/^\s?(=+)/, '<tt></tt>\1')
+ end
+
+ def remove_private_comments(body)
+ body.gsub!(/^\s*!--\s*?$.*?^\s*!\+\+\s*?$/m, '')
+ return body
+ end
+
+ ##
+ # Information of arguments of subroutines and functions in Fortran95
+
+ class Fortran95Definition
+
+ # Name of variable
+ #
+ attr_reader :varname
+
+ # Types of variable
+ #
+ attr_reader :types
+
+ # Initial Value
+ #
+ attr_reader :inivalue
+
+ # Suffix of array
+ #
+ attr_reader :arraysuffix
+
+ # Comments
+ #
+ attr_accessor :comment
+
+ # Flag of non documentation
+ #
+ attr_accessor :nodoc
+
+ def initialize(varname, types, inivalue, arraysuffix, comment,
+ nodoc=false)
+ @varname = varname
+ @types = types
+ @inivalue = inivalue
+ @arraysuffix = arraysuffix
+ @comment = comment
+ @nodoc = nodoc
+ end
+
+ def to_s
+ return <<-EOF
+<Fortran95Definition:
+varname=#{@varname}, types=#{types},
+inivalue=#{@inivalue}, arraysuffix=#{@arraysuffix}, nodoc=#{@nodoc},
+comment=
+#{@comment}
+>
+EOF
+ end
+
+ #
+ # If attr is included, true is returned
+ #
+ def include_attr?(attr)
+ return if !attr
+ @types.split(",").each{ |type|
+ return true if type.strip.chomp.upcase == attr.strip.chomp.upcase
+ }
+ return nil
+ end
+
+ end # End of Fortran95Definition
+
+ ##
+ # Parse string argument "text", and Return Array of Fortran95Definition
+ # object
+
+ def definition_info(text)
+ return nil unless text
+ lines = "#{text}"
+ defs = Array.new
+ comment = ""
+ trailing_comment = ""
+ under_comment_valid = false
+ lines.split("\n").each{ |line|
+ if /^\s*?!\s?(.*)/ =~ line
+ if COMMENTS_ARE_UPPER
+ comment << remove_header_marker($1)
+ comment << "\n"
+ elsif defs[-1] && under_comment_valid
+ defs[-1].comment << "\n"
+ defs[-1].comment << remove_header_marker($1)
+ end
+ next
+ elsif /^\s*?$/ =~ line
+ comment = ""
+ under_comment_valid = false
+ next
+ end
+ type = ""
+ characters = ""
+ if line =~ /^\s*?
+ (
+ character\s*?(\([\w\s\=\(\)\*]+?\))?[\s\,]*
+ | type\s*?\([\w\s]+?\)[\s\,]*
+ | integer\s*?(\([\w\s\=\(\)\*]+?\))?[\s\,]*
+ | real\s*?(\([\w\s\=\(\)\*]+?\))?[\s\,]*
+ | double\s+precision[\s\,]*
+ | logical\s*?(\([\w\s\=\(\)\*]+?\))?[\s\,]*
+ | complex\s*?(\([\w\s\=\(\)\*]+?\))?[\s\,]*
+ )
+ (.*?::)?
+ (.+)$
+ /ix
+ characters = $8
+ type = $1
+ type << $7.gsub(/::/, '').gsub(/^\s*?\,/, '') if $7
+ else
+ under_comment_valid = false
+ next
+ end
+ squote = false ; dquote = false ; bracket = 0
+ iniflag = false; commentflag = false
+ varname = "" ; arraysuffix = "" ; inivalue = ""
+ start_pos = defs.size
+ characters.split("").each { |char|
+ if !(squote) && !(dquote) && bracket <= 0 && !(iniflag) && !(commentflag)
+ case char
+ when "!" ; commentflag = true
+ when "(" ; bracket += 1 ; arraysuffix = char
+ when "\""; dquote = true
+ when "\'"; squote = true
+ when "=" ; iniflag = true ; inivalue << char
+ when ","
+ defs << Fortran95Definition.new(varname, type, inivalue, arraysuffix, comment)
+ varname = "" ; arraysuffix = "" ; inivalue = ""
+ under_comment_valid = true
+ when " " ; next
+ else ; varname << char
+ end
+ elsif commentflag
+ comment << remove_header_marker(char)
+ trailing_comment << remove_header_marker(char)
+ elsif iniflag
+ if dquote
+ case char
+ when "\"" ; dquote = false ; inivalue << char
+ else ; inivalue << char
+ end
+ elsif squote
+ case char
+ when "\'" ; squote = false ; inivalue << char
+ else ; inivalue << char
+ end
+ elsif bracket > 0
+ case char
+ when "(" ; bracket += 1 ; inivalue << char
+ when ")" ; bracket -= 1 ; inivalue << char
+ else ; inivalue << char
+ end
+ else
+ case char
+ when ","
+ defs << Fortran95Definition.new(varname, type, inivalue, arraysuffix, comment)
+ varname = "" ; arraysuffix = "" ; inivalue = ""
+ iniflag = false
+ under_comment_valid = true
+ when "(" ; bracket += 1 ; inivalue << char
+ when "\""; dquote = true ; inivalue << char
+ when "\'"; squote = true ; inivalue << char
+ when "!" ; commentflag = true
+ else ; inivalue << char
+ end
+ end
+ elsif !(squote) && !(dquote) && bracket > 0
+ case char
+ when "(" ; bracket += 1 ; arraysuffix << char
+ when ")" ; bracket -= 1 ; arraysuffix << char
+ else ; arraysuffix << char
+ end
+ elsif squote
+ case char
+ when "\'"; squote = false ; inivalue << char
+ else ; inivalue << char
+ end
+ elsif dquote
+ case char
+ when "\""; dquote = false ; inivalue << char
+ else ; inivalue << char
+ end
+ end
+ }
+ defs << Fortran95Definition.new(varname, type, inivalue, arraysuffix, comment)
+ if trailing_comment =~ /^:nodoc:/
+ defs[start_pos..-1].collect!{ |defitem|
+ defitem.nodoc = true
+ }
+ end
+ varname = "" ; arraysuffix = "" ; inivalue = ""
+ comment = ""
+ under_comment_valid = true
+ trailing_comment = ""
+ }
+ return defs
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/parser/perl.rb b/ruby/lib/rdoc/parser/perl.rb
new file mode 100644
index 0000000..43d1e9f
--- /dev/null
+++ b/ruby/lib/rdoc/parser/perl.rb
@@ -0,0 +1,165 @@
+require 'rdoc/parser'
+
+##
+#
+# This is an attamept to write a basic parser for Perl's
+# POD (Plain old Documentation) format. Ruby code must
+# co-exist with Perl, and some tasks are easier in Perl
+# than Ruby because of existing libraries.
+#
+# One difficult is that Perl POD has no means of identifying
+# the classes (packages) and methods (subs) with which it
+# is associated, it is more like literate programming in so
+# far as it just happens to be in the same place as the code,
+# but need not be.
+#
+# We would like to support all the markup the POD provides
+# so that it will convert happily to HTML. At the moment
+# I don't think I can do that: time constraints.
+#
+
+class RDoc::Parser::PerlPOD < RDoc::Parser
+
+ parse_files_matching(/.p[lm]$/)
+
+ ##
+ # Prepare to parse a perl file
+
+ def initialize(top_level, file_name, content, options, stats)
+ super
+
+ preprocess = RDoc::Markup::PreProcess.new @file_name, @options.rdoc_include
+
+ preprocess.handle @content do |directive, param|
+ warn "Unrecognized directive '#{directive}' in #{@file_name}"
+ end
+ end
+
+ ##
+ # Extract the Pod(-like) comments from the code.
+ # At its most basic there will ne no need to distinguish
+ # between the different types of header, etc.
+ #
+ # This uses a simple finite state machine, in a very
+ # procedural pattern. I could "replace case with polymorphism"
+ # but I think it would obscure the intent, scatter the
+ # code all over tha place. This machine is necessary
+ # because POD requires that directives be preceded by
+ # blank lines, so reading line by line is necessary,
+ # and preserving state about what is seen is necesary.
+
+ def scan
+
+ @top_level.comment ||= ""
+ state=:code_blank
+ line_number = 0
+ line = nil
+
+ # This started out as a really long nested case statement,
+ # which also led to repetitive code. I'd like to avoid that
+ # so I'm using a "table" instead.
+
+ # Firstly we need some procs to do the transition and processing
+ # work. Because these are procs they are closures, and they can
+ # use variables in the local scope.
+ #
+ # First, the "nothing to see here" stuff.
+ code_noop = lambda do
+ if line =~ /^\s+$/
+ state = :code_blank
+ end
+ end
+
+ pod_noop = lambda do
+ if line =~ /^\s+$/
+ state = :pod_blank
+ end
+ @top_level.comment += filter(line)
+ end
+
+ begin_noop = lambda do
+ if line =~ /^\s+$/
+ state = :begin_blank
+ end
+ @top_level.comment += filter(line)
+ end
+
+ # Now for the blocks that process code and comments...
+
+ transit_to_pod = lambda do
+ case line
+ when /^=(?:pod|head\d+)/
+ state = :pod_no_blank
+ @top_level.comment += filter(line)
+ when /^=over/
+ state = :over_no_blank
+ @top_level.comment += filter(line)
+ when /^=(?:begin|for)/
+ state = :begin_no_blank
+ end
+ end
+
+ process_pod = lambda do
+ case line
+ when /^\s*$/
+ state = :pod_blank
+ @top_level.comment += filter(line)
+ when /^=cut/
+ state = :code_no_blank
+ when /^=end/
+ $stderr.puts "'=end' unexpected at #{line_number} in #{@file_name}"
+ else
+ @top_level.comment += filter(line)
+ end
+ end
+
+
+ process_begin = lambda do
+ case line
+ when /^\s*$/
+ state = :begin_blank
+ @top_level.comment += filter(line)
+ when /^=end/
+ state = :code_no_blank
+ when /^=cut/
+ $stderr.puts "'=cut' unexpected at #{line_number} in #{@file_name}"
+ else
+ @top_level.comment += filter(line)
+ end
+
+ end
+
+
+ transitions = { :code_no_blank => code_noop,
+ :code_blank => transit_to_pod,
+ :pod_no_blank => pod_noop,
+ :pod_blank => process_pod,
+ :begin_no_blank => begin_noop,
+ :begin_blank => process_begin}
+ @content.each_line do |l|
+ line = l
+ line_number += 1
+ transitions[state].call
+ end # each line
+
+ @top_level
+ end
+
+ # Filter the perl markup that does the same as the rdoc
+ # filtering. Only basic for now. Will probably need a
+ # proper parser to cope with C<<...>> etc
+ def filter(comment)
+ return '' if comment =~ /^=pod\s*$/
+ comment.gsub!(/^=pod/, '==')
+ comment.gsub!(/^=head(\d+)/) do
+ "=" * $1.to_i
+ end
+ comment.gsub!(/=item/, '');
+ comment.gsub!(/C<(.*?)>/, '<tt>\1</tt>');
+ comment.gsub!(/I<(.*?)>/, '<i>\1</i>');
+ comment.gsub!(/B<(.*?)>/, '<b>\1</b>');
+ comment
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/parser/ruby.rb b/ruby/lib/rdoc/parser/ruby.rb
new file mode 100644
index 0000000..865cb79
--- /dev/null
+++ b/ruby/lib/rdoc/parser/ruby.rb
@@ -0,0 +1,2829 @@
+##
+# This file contains stuff stolen outright from:
+#
+# rtags.rb -
+# ruby-lex.rb - ruby lexcal analyzer
+# ruby-token.rb - ruby tokens
+# by Keiju ISHITSUKA (Nippon Rational Inc.)
+#
+
+require 'e2mmap'
+require 'irb/slex'
+
+require 'rdoc/code_objects'
+require 'rdoc/tokenstream'
+require 'rdoc/markup/preprocess'
+require 'rdoc/parser'
+
+$TOKEN_DEBUG ||= nil
+#$TOKEN_DEBUG = $DEBUG_RDOC
+
+##
+# Definitions of all tokens involved in the lexical analysis
+
+module RDoc::RubyToken
+
+ EXPR_BEG = :EXPR_BEG
+ EXPR_MID = :EXPR_MID
+ EXPR_END = :EXPR_END
+ EXPR_ARG = :EXPR_ARG
+ EXPR_FNAME = :EXPR_FNAME
+ EXPR_DOT = :EXPR_DOT
+ EXPR_CLASS = :EXPR_CLASS
+
+ class Token
+ NO_TEXT = "??".freeze
+
+ attr_accessor :text
+ attr_reader :line_no
+ attr_reader :char_no
+
+ def initialize(line_no, char_no)
+ @line_no = line_no
+ @char_no = char_no
+ @text = NO_TEXT
+ end
+
+ def ==(other)
+ self.class == other.class and
+ other.line_no == @line_no and
+ other.char_no == @char_no and
+ other.text == @text
+ end
+
+ ##
+ # Because we're used in contexts that expect to return a token, we set the
+ # text string and then return ourselves
+
+ def set_text(text)
+ @text = text
+ self
+ end
+
+ end
+
+ class TkNode < Token
+ attr :node
+ end
+
+ class TkId < Token
+ def initialize(line_no, char_no, name)
+ super(line_no, char_no)
+ @name = name
+ end
+ attr :name
+ end
+
+ class TkKW < TkId
+ end
+
+ class TkVal < Token
+ def initialize(line_no, char_no, value = nil)
+ super(line_no, char_no)
+ set_text(value)
+ end
+ end
+
+ class TkOp < Token
+ def name
+ self.class.op_name
+ end
+ end
+
+ class TkOPASGN < TkOp
+ def initialize(line_no, char_no, op)
+ super(line_no, char_no)
+ op = TkReading2Token[op] unless Symbol === op
+ @op = op
+ end
+ attr :op
+ end
+
+ class TkUnknownChar < Token
+ def initialize(line_no, char_no, id)
+ super(line_no, char_no)
+ @name = char_no.chr
+ end
+ attr :name
+ end
+
+ class TkError < Token
+ end
+
+ def set_token_position(line, char)
+ @prev_line_no = line
+ @prev_char_no = char
+ end
+
+ def Token(token, value = nil)
+ tk = nil
+ case token
+ when String, Symbol
+ source = String === token ? TkReading2Token : TkSymbol2Token
+ raise TkReading2TokenNoKey, token if (tk = source[token]).nil?
+ tk = Token(tk[0], value)
+ else
+ tk = if (token.ancestors & [TkId, TkVal, TkOPASGN, TkUnknownChar]).empty?
+ token.new(@prev_line_no, @prev_char_no)
+ else
+ token.new(@prev_line_no, @prev_char_no, value)
+ end
+ end
+ tk
+ end
+
+ TokenDefinitions = [
+ [:TkCLASS, TkKW, "class", EXPR_CLASS],
+ [:TkMODULE, TkKW, "module", EXPR_CLASS],
+ [:TkDEF, TkKW, "def", EXPR_FNAME],
+ [:TkUNDEF, TkKW, "undef", EXPR_FNAME],
+ [:TkBEGIN, TkKW, "begin", EXPR_BEG],
+ [:TkRESCUE, TkKW, "rescue", EXPR_MID],
+ [:TkENSURE, TkKW, "ensure", EXPR_BEG],
+ [:TkEND, TkKW, "end", EXPR_END],
+ [:TkIF, TkKW, "if", EXPR_BEG, :TkIF_MOD],
+ [:TkUNLESS, TkKW, "unless", EXPR_BEG, :TkUNLESS_MOD],
+ [:TkTHEN, TkKW, "then", EXPR_BEG],
+ [:TkELSIF, TkKW, "elsif", EXPR_BEG],
+ [:TkELSE, TkKW, "else", EXPR_BEG],
+ [:TkCASE, TkKW, "case", EXPR_BEG],
+ [:TkWHEN, TkKW, "when", EXPR_BEG],
+ [:TkWHILE, TkKW, "while", EXPR_BEG, :TkWHILE_MOD],
+ [:TkUNTIL, TkKW, "until", EXPR_BEG, :TkUNTIL_MOD],
+ [:TkFOR, TkKW, "for", EXPR_BEG],
+ [:TkBREAK, TkKW, "break", EXPR_END],
+ [:TkNEXT, TkKW, "next", EXPR_END],
+ [:TkREDO, TkKW, "redo", EXPR_END],
+ [:TkRETRY, TkKW, "retry", EXPR_END],
+ [:TkIN, TkKW, "in", EXPR_BEG],
+ [:TkDO, TkKW, "do", EXPR_BEG],
+ [:TkRETURN, TkKW, "return", EXPR_MID],
+ [:TkYIELD, TkKW, "yield", EXPR_END],
+ [:TkSUPER, TkKW, "super", EXPR_END],
+ [:TkSELF, TkKW, "self", EXPR_END],
+ [:TkNIL, TkKW, "nil", EXPR_END],
+ [:TkTRUE, TkKW, "true", EXPR_END],
+ [:TkFALSE, TkKW, "false", EXPR_END],
+ [:TkAND, TkKW, "and", EXPR_BEG],
+ [:TkOR, TkKW, "or", EXPR_BEG],
+ [:TkNOT, TkKW, "not", EXPR_BEG],
+ [:TkIF_MOD, TkKW],
+ [:TkUNLESS_MOD, TkKW],
+ [:TkWHILE_MOD, TkKW],
+ [:TkUNTIL_MOD, TkKW],
+ [:TkALIAS, TkKW, "alias", EXPR_FNAME],
+ [:TkDEFINED, TkKW, "defined?", EXPR_END],
+ [:TklBEGIN, TkKW, "BEGIN", EXPR_END],
+ [:TklEND, TkKW, "END", EXPR_END],
+ [:Tk__LINE__, TkKW, "__LINE__", EXPR_END],
+ [:Tk__FILE__, TkKW, "__FILE__", EXPR_END],
+
+ [:TkIDENTIFIER, TkId],
+ [:TkFID, TkId],
+ [:TkGVAR, TkId],
+ [:TkIVAR, TkId],
+ [:TkCONSTANT, TkId],
+
+ [:TkINTEGER, TkVal],
+ [:TkFLOAT, TkVal],
+ [:TkSTRING, TkVal],
+ [:TkXSTRING, TkVal],
+ [:TkREGEXP, TkVal],
+ [:TkCOMMENT, TkVal],
+
+ [:TkDSTRING, TkNode],
+ [:TkDXSTRING, TkNode],
+ [:TkDREGEXP, TkNode],
+ [:TkNTH_REF, TkId],
+ [:TkBACK_REF, TkId],
+
+ [:TkUPLUS, TkOp, "+@"],
+ [:TkUMINUS, TkOp, "-@"],
+ [:TkPOW, TkOp, "**"],
+ [:TkCMP, TkOp, "<=>"],
+ [:TkEQ, TkOp, "=="],
+ [:TkEQQ, TkOp, "==="],
+ [:TkNEQ, TkOp, "!="],
+ [:TkGEQ, TkOp, ">="],
+ [:TkLEQ, TkOp, "<="],
+ [:TkANDOP, TkOp, "&&"],
+ [:TkOROP, TkOp, "||"],
+ [:TkMATCH, TkOp, "=~"],
+ [:TkNMATCH, TkOp, "!~"],
+ [:TkDOT2, TkOp, ".."],
+ [:TkDOT3, TkOp, "..."],
+ [:TkAREF, TkOp, "[]"],
+ [:TkASET, TkOp, "[]="],
+ [:TkLSHFT, TkOp, "<<"],
+ [:TkRSHFT, TkOp, ">>"],
+ [:TkCOLON2, TkOp],
+ [:TkCOLON3, TkOp],
+# [:OPASGN, TkOp], # +=, -= etc. #
+ [:TkASSOC, TkOp, "=>"],
+ [:TkQUESTION, TkOp, "?"], #?
+ [:TkCOLON, TkOp, ":"], #:
+
+ [:TkfLPAREN], # func( #
+ [:TkfLBRACK], # func[ #
+ [:TkfLBRACE], # func{ #
+ [:TkSTAR], # *arg
+ [:TkAMPER], # &arg #
+ [:TkSYMBOL, TkId], # :SYMBOL
+ [:TkSYMBEG, TkId],
+ [:TkGT, TkOp, ">"],
+ [:TkLT, TkOp, "<"],
+ [:TkPLUS, TkOp, "+"],
+ [:TkMINUS, TkOp, "-"],
+ [:TkMULT, TkOp, "*"],
+ [:TkDIV, TkOp, "/"],
+ [:TkMOD, TkOp, "%"],
+ [:TkBITOR, TkOp, "|"],
+ [:TkBITXOR, TkOp, "^"],
+ [:TkBITAND, TkOp, "&"],
+ [:TkBITNOT, TkOp, "~"],
+ [:TkNOTOP, TkOp, "!"],
+
+ [:TkBACKQUOTE, TkOp, "`"],
+
+ [:TkASSIGN, Token, "="],
+ [:TkDOT, Token, "."],
+ [:TkLPAREN, Token, "("], #(exp)
+ [:TkLBRACK, Token, "["], #[arry]
+ [:TkLBRACE, Token, "{"], #{hash}
+ [:TkRPAREN, Token, ")"],
+ [:TkRBRACK, Token, "]"],
+ [:TkRBRACE, Token, "}"],
+ [:TkCOMMA, Token, ","],
+ [:TkSEMICOLON, Token, ";"],
+
+ [:TkRD_COMMENT],
+ [:TkSPACE],
+ [:TkNL],
+ [:TkEND_OF_SCRIPT],
+
+ [:TkBACKSLASH, TkUnknownChar, "\\"],
+ [:TkAT, TkUnknownChar, "@"],
+ [:TkDOLLAR, TkUnknownChar, "\$"], #"
+ ]
+
+ # {reading => token_class}
+ # {reading => [token_class, *opt]}
+ TkReading2Token = {}
+ TkSymbol2Token = {}
+
+ def self.def_token(token_n, super_token = Token, reading = nil, *opts)
+ token_n = token_n.id2name unless String === token_n
+
+ fail AlreadyDefinedToken, token_n if const_defined?(token_n)
+
+ token_c = Class.new super_token
+ const_set token_n, token_c
+# token_c.inspect
+
+ if reading
+ if TkReading2Token[reading]
+ fail TkReading2TokenDuplicateError, token_n, reading
+ end
+ if opts.empty?
+ TkReading2Token[reading] = [token_c]
+ else
+ TkReading2Token[reading] = [token_c].concat(opts)
+ end
+ end
+ TkSymbol2Token[token_n.intern] = token_c
+
+ if token_c <= TkOp
+ token_c.class_eval %{
+ def self.op_name; "#{reading}"; end
+ }
+ end
+ end
+
+ for defs in TokenDefinitions
+ def_token(*defs)
+ end
+
+ NEWLINE_TOKEN = TkNL.new(0,0)
+ NEWLINE_TOKEN.set_text("\n")
+
+end
+
+##
+# Lexical analyzer for Ruby source
+
+class RDoc::RubyLex
+
+ ##
+ # Read an input stream character by character. We allow for unlimited
+ # ungetting of characters just read.
+ #
+ # We simplify the implementation greatly by reading the entire input
+ # into a buffer initially, and then simply traversing it using
+ # pointers.
+ #
+ # We also have to allow for the <i>here document diversion</i>. This
+ # little gem comes about when the lexer encounters a here
+ # document. At this point we effectively need to split the input
+ # stream into two parts: one to read the body of the here document,
+ # the other to read the rest of the input line where the here
+ # document was initially encountered. For example, we might have
+ #
+ # do_something(<<-A, <<-B)
+ # stuff
+ # for
+ # A
+ # stuff
+ # for
+ # B
+ #
+ # When the lexer encounters the <<A, it reads until the end of the
+ # line, and keeps it around for later. It then reads the body of the
+ # here document. Once complete, it needs to read the rest of the
+ # original line, but then skip the here document body.
+ #
+
+ class BufferedReader
+
+ attr_reader :line_num
+
+ def initialize(content, options)
+ @options = options
+
+ if /\t/ =~ content
+ tab_width = @options.tab_width
+ content = content.split(/\n/).map do |line|
+ 1 while line.gsub!(/\t+/) { ' ' * (tab_width*$&.length - $`.length % tab_width)} && $~ #`
+ line
+ end .join("\n")
+ end
+ @content = content
+ @content << "\n" unless @content[-1,1] == "\n"
+ @size = @content.size
+ @offset = 0
+ @hwm = 0
+ @line_num = 1
+ @read_back_offset = 0
+ @last_newline = 0
+ @newline_pending = false
+ end
+
+ def column
+ @offset - @last_newline
+ end
+
+ def getc
+ return nil if @offset >= @size
+ ch = @content[@offset, 1]
+
+ @offset += 1
+ @hwm = @offset if @hwm < @offset
+
+ if @newline_pending
+ @line_num += 1
+ @last_newline = @offset - 1
+ @newline_pending = false
+ end
+
+ if ch == "\n"
+ @newline_pending = true
+ end
+ ch
+ end
+
+ def getc_already_read
+ getc
+ end
+
+ def ungetc(ch)
+ raise "unget past beginning of file" if @offset <= 0
+ @offset -= 1
+ if @content[@offset] == ?\n
+ @newline_pending = false
+ end
+ end
+
+ def get_read
+ res = @content[@read_back_offset...@offset]
+ @read_back_offset = @offset
+ res
+ end
+
+ def peek(at)
+ pos = @offset + at
+ if pos >= @size
+ nil
+ else
+ @content[pos, 1]
+ end
+ end
+
+ def peek_equal(str)
+ @content[@offset, str.length] == str
+ end
+
+ def divert_read_from(reserve)
+ @content[@offset, 0] = reserve
+ @size = @content.size
+ end
+ end
+
+ # end of nested class BufferedReader
+
+ extend Exception2MessageMapper
+ def_exception(:AlreadyDefinedToken, "Already defined token(%s)")
+ def_exception(:TkReading2TokenNoKey, "key nothing(key='%s')")
+ def_exception(:TkSymbol2TokenNoKey, "key nothing(key='%s')")
+ def_exception(:TkReading2TokenDuplicateError,
+ "key duplicate(token_n='%s', key='%s')")
+ def_exception(:SyntaxError, "%s")
+
+ include RDoc::RubyToken
+ include IRB
+
+ attr_reader :continue
+ attr_reader :lex_state
+
+ def self.debug?
+ false
+ end
+
+ def initialize(content, options)
+ lex_init
+
+ @options = options
+
+ @reader = BufferedReader.new content, @options
+
+ @exp_line_no = @line_no = 1
+ @base_char_no = 0
+ @indent = 0
+
+ @ltype = nil
+ @quoted = nil
+ @lex_state = EXPR_BEG
+ @space_seen = false
+
+ @continue = false
+ @line = ""
+
+ @skip_space = false
+ @read_auto_clean_up = false
+ @exception_on_syntax_error = true
+ end
+
+ attr_accessor :skip_space
+ attr_accessor :read_auto_clean_up
+ attr_accessor :exception_on_syntax_error
+ attr_reader :indent
+
+ # io functions
+ def line_no
+ @reader.line_num
+ end
+
+ def char_no
+ @reader.column
+ end
+
+ def get_read
+ @reader.get_read
+ end
+
+ def getc
+ @reader.getc
+ end
+
+ def getc_of_rests
+ @reader.getc_already_read
+ end
+
+ def gets
+ c = getc or return
+ l = ""
+ begin
+ l.concat c unless c == "\r"
+ break if c == "\n"
+ end while c = getc
+ l
+ end
+
+
+ def ungetc(c = nil)
+ @reader.ungetc(c)
+ end
+
+ def peek_equal?(str)
+ @reader.peek_equal(str)
+ end
+
+ def peek(i = 0)
+ @reader.peek(i)
+ end
+
+ def lex
+ until (TkNL === (tk = token) or TkEND_OF_SCRIPT === tk) and
+ not @continue or tk.nil?
+ end
+
+ line = get_read
+
+ if line == "" and TkEND_OF_SCRIPT === tk or tk.nil? then
+ nil
+ else
+ line
+ end
+ end
+
+ def token
+ set_token_position(line_no, char_no)
+ begin
+ begin
+ tk = @OP.match(self)
+ @space_seen = TkSPACE === tk
+ rescue SyntaxError => e
+ raise RDoc::Error, "syntax error: #{e.message}" if
+ @exception_on_syntax_error
+
+ tk = TkError.new(line_no, char_no)
+ end
+ end while @skip_space and TkSPACE === tk
+ if @read_auto_clean_up
+ get_read
+ end
+# throw :eof unless tk
+ tk
+ end
+
+ ENINDENT_CLAUSE = [
+ "case", "class", "def", "do", "for", "if",
+ "module", "unless", "until", "while", "begin" #, "when"
+ ]
+ DEINDENT_CLAUSE = ["end" #, "when"
+ ]
+
+ PERCENT_LTYPE = {
+ "q" => "\'",
+ "Q" => "\"",
+ "x" => "\`",
+ "r" => "/",
+ "w" => "]"
+ }
+
+ PERCENT_PAREN = {
+ "{" => "}",
+ "[" => "]",
+ "<" => ">",
+ "(" => ")"
+ }
+
+ Ltype2Token = {
+ "\'" => TkSTRING,
+ "\"" => TkSTRING,
+ "\`" => TkXSTRING,
+ "/" => TkREGEXP,
+ "]" => TkDSTRING
+ }
+ Ltype2Token.default = TkSTRING
+
+ DLtype2Token = {
+ "\"" => TkDSTRING,
+ "\`" => TkDXSTRING,
+ "/" => TkDREGEXP,
+ }
+
+ def lex_init()
+ @OP = IRB::SLex.new
+ @OP.def_rules("\0", "\004", "\032") do |chars, io|
+ Token(TkEND_OF_SCRIPT).set_text(chars)
+ end
+
+ @OP.def_rules(" ", "\t", "\f", "\r", "\13") do |chars, io|
+ @space_seen = TRUE
+ while (ch = getc) =~ /[ \t\f\r\13]/
+ chars << ch
+ end
+ ungetc
+ Token(TkSPACE).set_text(chars)
+ end
+
+ @OP.def_rule("#") do
+ |op, io|
+ identify_comment
+ end
+
+ @OP.def_rule("=begin", proc{@prev_char_no == 0 && peek(0) =~ /\s/}) do
+ |op, io|
+ str = op
+ @ltype = "="
+
+
+ begin
+ line = ""
+ begin
+ ch = getc
+ line << ch
+ end until ch == "\n"
+ str << line
+ end until line =~ /^=end/
+
+ ungetc
+
+ @ltype = nil
+
+ if str =~ /\A=begin\s+rdoc/i
+ str.sub!(/\A=begin.*\n/, '')
+ str.sub!(/^=end.*/m, '')
+ Token(TkCOMMENT).set_text(str)
+ else
+ Token(TkRD_COMMENT)#.set_text(str)
+ end
+ end
+
+ @OP.def_rule("\n") do
+ print "\\n\n" if RDoc::RubyLex.debug?
+ case @lex_state
+ when EXPR_BEG, EXPR_FNAME, EXPR_DOT
+ @continue = TRUE
+ else
+ @continue = FALSE
+ @lex_state = EXPR_BEG
+ end
+ Token(TkNL).set_text("\n")
+ end
+
+ @OP.def_rules("*", "**",
+ "!", "!=", "!~",
+ "=", "==", "===",
+ "=~", "<=>",
+ "<", "<=",
+ ">", ">=", ">>") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token(op).set_text(op)
+ end
+
+ @OP.def_rules("<<") do
+ |op, io|
+ tk = nil
+ if @lex_state != EXPR_END && @lex_state != EXPR_CLASS &&
+ (@lex_state != EXPR_ARG || @space_seen)
+ c = peek(0)
+ if /[-\w_\"\'\`]/ =~ c
+ tk = identify_here_document
+ end
+ end
+ if !tk
+ @lex_state = EXPR_BEG
+ tk = Token(op).set_text(op)
+ end
+ tk
+ end
+
+ @OP.def_rules("'", '"') do
+ |op, io|
+ identify_string(op)
+ end
+
+ @OP.def_rules("`") do
+ |op, io|
+ if @lex_state == EXPR_FNAME
+ Token(op).set_text(op)
+ else
+ identify_string(op)
+ end
+ end
+
+ @OP.def_rules('?') do
+ |op, io|
+ if @lex_state == EXPR_END
+ @lex_state = EXPR_BEG
+ Token(TkQUESTION).set_text(op)
+ else
+ ch = getc
+ if @lex_state == EXPR_ARG && ch !~ /\s/
+ ungetc
+ @lex_state = EXPR_BEG
+ Token(TkQUESTION).set_text(op)
+ else
+ str = op
+ str << ch
+ if (ch == '\\') #'
+ str << read_escape
+ end
+ @lex_state = EXPR_END
+ Token(TkINTEGER).set_text(str)
+ end
+ end
+ end
+
+ @OP.def_rules("&", "&&", "|", "||") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token(op).set_text(op)
+ end
+
+ @OP.def_rules("+=", "-=", "*=", "**=",
+ "&=", "|=", "^=", "<<=", ">>=", "||=", "&&=") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ op =~ /^(.*)=$/
+ Token(TkOPASGN, $1).set_text(op)
+ end
+
+ @OP.def_rule("+@", proc{@lex_state == EXPR_FNAME}) do |op, io|
+ Token(TkUPLUS).set_text(op)
+ end
+
+ @OP.def_rule("-@", proc{@lex_state == EXPR_FNAME}) do |op, io|
+ Token(TkUMINUS).set_text(op)
+ end
+
+ @OP.def_rules("+", "-") do
+ |op, io|
+ catch(:RET) do
+ if @lex_state == EXPR_ARG
+ if @space_seen and peek(0) =~ /[0-9]/
+ throw :RET, identify_number(op)
+ else
+ @lex_state = EXPR_BEG
+ end
+ elsif @lex_state != EXPR_END and peek(0) =~ /[0-9]/
+ throw :RET, identify_number(op)
+ else
+ @lex_state = EXPR_BEG
+ end
+ Token(op).set_text(op)
+ end
+ end
+
+ @OP.def_rule(".") do
+ @lex_state = EXPR_BEG
+ if peek(0) =~ /[0-9]/
+ ungetc
+ identify_number("")
+ else
+ # for obj.if
+ @lex_state = EXPR_DOT
+ Token(TkDOT).set_text(".")
+ end
+ end
+
+ @OP.def_rules("..", "...") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token(op).set_text(op)
+ end
+
+ lex_int2
+ end
+
+ def lex_int2
+ @OP.def_rules("]", "}", ")") do
+ |op, io|
+ @lex_state = EXPR_END
+ @indent -= 1
+ Token(op).set_text(op)
+ end
+
+ @OP.def_rule(":") do
+ if @lex_state == EXPR_END || peek(0) =~ /\s/
+ @lex_state = EXPR_BEG
+ tk = Token(TkCOLON)
+ else
+ @lex_state = EXPR_FNAME
+ tk = Token(TkSYMBEG)
+ end
+ tk.set_text(":")
+ end
+
+ @OP.def_rule("::") do
+ if @lex_state == EXPR_BEG or @lex_state == EXPR_ARG && @space_seen
+ @lex_state = EXPR_BEG
+ tk = Token(TkCOLON3)
+ else
+ @lex_state = EXPR_DOT
+ tk = Token(TkCOLON2)
+ end
+ tk.set_text("::")
+ end
+
+ @OP.def_rule("/") do
+ |op, io|
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ identify_string(op)
+ elsif peek(0) == '='
+ getc
+ @lex_state = EXPR_BEG
+ Token(TkOPASGN, :/).set_text("/=") #")
+ elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
+ identify_string(op)
+ else
+ @lex_state = EXPR_BEG
+ Token("/").set_text(op)
+ end
+ end
+
+ @OP.def_rules("^") do
+ @lex_state = EXPR_BEG
+ Token("^").set_text("^")
+ end
+
+ @OP.def_rules(",", ";") do
+ |op, io|
+ @lex_state = EXPR_BEG
+ Token(op).set_text(op)
+ end
+
+ @OP.def_rule("~") do
+ @lex_state = EXPR_BEG
+ Token("~").set_text("~")
+ end
+
+ @OP.def_rule("~@", proc{@lex_state = EXPR_FNAME}) do
+ @lex_state = EXPR_BEG
+ Token("~").set_text("~@")
+ end
+
+ @OP.def_rule("(") do
+ @indent += 1
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ @lex_state = EXPR_BEG
+ tk = Token(TkfLPAREN)
+ else
+ @lex_state = EXPR_BEG
+ tk = Token(TkLPAREN)
+ end
+ tk.set_text("(")
+ end
+
+ @OP.def_rule("[]", proc{@lex_state == EXPR_FNAME}) do
+ Token("[]").set_text("[]")
+ end
+
+ @OP.def_rule("[]=", proc{@lex_state == EXPR_FNAME}) do
+ Token("[]=").set_text("[]=")
+ end
+
+ @OP.def_rule("[") do
+ @indent += 1
+ if @lex_state == EXPR_FNAME
+ t = Token(TkfLBRACK)
+ else
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ t = Token(TkLBRACK)
+ elsif @lex_state == EXPR_ARG && @space_seen
+ t = Token(TkLBRACK)
+ else
+ t = Token(TkfLBRACK)
+ end
+ @lex_state = EXPR_BEG
+ end
+ t.set_text("[")
+ end
+
+ @OP.def_rule("{") do
+ @indent += 1
+ if @lex_state != EXPR_END && @lex_state != EXPR_ARG
+ t = Token(TkLBRACE)
+ else
+ t = Token(TkfLBRACE)
+ end
+ @lex_state = EXPR_BEG
+ t.set_text("{")
+ end
+
+ @OP.def_rule('\\') do #'
+ if getc == "\n"
+ @space_seen = true
+ @continue = true
+ Token(TkSPACE).set_text("\\\n")
+ else
+ ungetc
+ Token("\\").set_text("\\") #"
+ end
+ end
+
+ @OP.def_rule('%') do
+ |op, io|
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ identify_quotation('%')
+ elsif peek(0) == '='
+ getc
+ Token(TkOPASGN, "%").set_text("%=")
+ elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
+ identify_quotation('%')
+ else
+ @lex_state = EXPR_BEG
+ Token("%").set_text("%")
+ end
+ end
+
+ @OP.def_rule('$') do #'
+ identify_gvar
+ end
+
+ @OP.def_rule('@') do
+ if peek(0) =~ /[@\w_]/
+ ungetc
+ identify_identifier
+ else
+ Token("@").set_text("@")
+ end
+ end
+
+ @OP.def_rule("__END__", proc{@prev_char_no == 0 && peek(0) =~ /[\r\n]/}) do
+ throw :eof
+ end
+
+ @OP.def_rule("") do
+ |op, io|
+ printf "MATCH: start %s: %s\n", op, io.inspect if RDoc::RubyLex.debug?
+ if peek(0) =~ /[0-9]/
+ t = identify_number("")
+ elsif peek(0) =~ /[\w_]/
+ t = identify_identifier
+ end
+ printf "MATCH: end %s: %s\n", op, io.inspect if RDoc::RubyLex.debug?
+ t
+ end
+ end
+
+ def identify_gvar
+ @lex_state = EXPR_END
+ str = "$"
+
+ tk = case ch = getc
+ when /[~_*$?!@\/\\;,=:<>".]/ #"
+ str << ch
+ Token(TkGVAR, str)
+
+ when "-"
+ str << "-" << getc
+ Token(TkGVAR, str)
+
+ when "&", "`", "'", "+"
+ str << ch
+ Token(TkBACK_REF, str)
+
+ when /[1-9]/
+ str << ch
+ while (ch = getc) =~ /[0-9]/
+ str << ch
+ end
+ ungetc
+ Token(TkNTH_REF)
+ when /\w/
+ ungetc
+ ungetc
+ return identify_identifier
+ else
+ ungetc
+ Token("$")
+ end
+ tk.set_text(str)
+ end
+
+ def identify_identifier
+ token = ""
+ token.concat getc if peek(0) =~ /[$@]/
+ token.concat getc if peek(0) == "@"
+
+ while (ch = getc) =~ /\w|_/
+ print ":", ch, ":" if RDoc::RubyLex.debug?
+ token.concat ch
+ end
+ ungetc
+
+ if ch == "!" or ch == "?"
+ token.concat getc
+ end
+ # fix token
+
+ # $stderr.puts "identifier - #{token}, state = #@lex_state"
+
+ case token
+ when /^\$/
+ return Token(TkGVAR, token).set_text(token)
+ when /^\@/
+ @lex_state = EXPR_END
+ return Token(TkIVAR, token).set_text(token)
+ end
+
+ if @lex_state != EXPR_DOT
+ print token, "\n" if RDoc::RubyLex.debug?
+
+ token_c, *trans = TkReading2Token[token]
+ if token_c
+ # reserved word?
+
+ if (@lex_state != EXPR_BEG &&
+ @lex_state != EXPR_FNAME &&
+ trans[1])
+ # modifiers
+ token_c = TkSymbol2Token[trans[1]]
+ @lex_state = trans[0]
+ else
+ if @lex_state != EXPR_FNAME
+ if ENINDENT_CLAUSE.include?(token)
+ @indent += 1
+ elsif DEINDENT_CLAUSE.include?(token)
+ @indent -= 1
+ end
+ @lex_state = trans[0]
+ else
+ @lex_state = EXPR_END
+ end
+ end
+ return Token(token_c, token).set_text(token)
+ end
+ end
+
+ if @lex_state == EXPR_FNAME
+ @lex_state = EXPR_END
+ if peek(0) == '='
+ token.concat getc
+ end
+ elsif @lex_state == EXPR_BEG || @lex_state == EXPR_DOT
+ @lex_state = EXPR_ARG
+ else
+ @lex_state = EXPR_END
+ end
+
+ if token[0, 1] =~ /[A-Z]/
+ return Token(TkCONSTANT, token).set_text(token)
+ elsif token[token.size - 1, 1] =~ /[!?]/
+ return Token(TkFID, token).set_text(token)
+ else
+ return Token(TkIDENTIFIER, token).set_text(token)
+ end
+ end
+
+ def identify_here_document
+ ch = getc
+ if ch == "-"
+ ch = getc
+ indent = true
+ end
+ if /['"`]/ =~ ch # '
+ lt = ch
+ quoted = ""
+ while (c = getc) && c != lt
+ quoted.concat c
+ end
+ else
+ lt = '"'
+ quoted = ch.dup
+ while (c = getc) && c =~ /\w/
+ quoted.concat c
+ end
+ ungetc
+ end
+
+ ltback, @ltype = @ltype, lt
+ reserve = ""
+
+ while ch = getc
+ reserve << ch
+ if ch == "\\" #"
+ ch = getc
+ reserve << ch
+ elsif ch == "\n"
+ break
+ end
+ end
+
+ str = ""
+ while (l = gets)
+ l.chomp!
+ l.strip! if indent
+ break if l == quoted
+ str << l.chomp << "\n"
+ end
+
+ @reader.divert_read_from(reserve)
+
+ @ltype = ltback
+ @lex_state = EXPR_END
+ Token(Ltype2Token[lt], str).set_text(str.dump)
+ end
+
+ def identify_quotation(initial_char)
+ ch = getc
+ if lt = PERCENT_LTYPE[ch]
+ initial_char += ch
+ ch = getc
+ elsif ch =~ /\W/
+ lt = "\""
+ else
+ fail SyntaxError, "unknown type of %string ('#{ch}')"
+ end
+# if ch !~ /\W/
+# ungetc
+# next
+# end
+ #@ltype = lt
+ @quoted = ch unless @quoted = PERCENT_PAREN[ch]
+ identify_string(lt, @quoted, ch, initial_char)
+ end
+
+ def identify_number(start)
+ str = start.dup
+
+ if start == "+" or start == "-" or start == ""
+ start = getc
+ str << start
+ end
+
+ @lex_state = EXPR_END
+
+ if start == "0"
+ if peek(0) == "x"
+ ch = getc
+ str << ch
+ match = /[0-9a-f_]/
+ else
+ match = /[0-7_]/
+ end
+ while ch = getc
+ if ch !~ match
+ ungetc
+ break
+ else
+ str << ch
+ end
+ end
+ return Token(TkINTEGER).set_text(str)
+ end
+
+ type = TkINTEGER
+ allow_point = TRUE
+ allow_e = TRUE
+ while ch = getc
+ case ch
+ when /[0-9_]/
+ str << ch
+
+ when allow_point && "."
+ type = TkFLOAT
+ if peek(0) !~ /[0-9]/
+ ungetc
+ break
+ end
+ str << ch
+ allow_point = false
+
+ when allow_e && "e", allow_e && "E"
+ str << ch
+ type = TkFLOAT
+ if peek(0) =~ /[+-]/
+ str << getc
+ end
+ allow_e = false
+ allow_point = false
+ else
+ ungetc
+ break
+ end
+ end
+ Token(type).set_text(str)
+ end
+
+ def identify_string(ltype, quoted = ltype, opener=nil, initial_char = nil)
+ @ltype = ltype
+ @quoted = quoted
+ subtype = nil
+
+ str = ""
+ str << initial_char if initial_char
+ str << (opener||quoted)
+
+ nest = 0
+ begin
+ while ch = getc
+ str << ch
+ if @quoted == ch
+ if nest == 0
+ break
+ else
+ nest -= 1
+ end
+ elsif opener == ch
+ nest += 1
+ elsif @ltype != "'" && @ltype != "]" and ch == "#"
+ ch = getc
+ if ch == "{"
+ subtype = true
+ str << ch << skip_inner_expression
+ else
+ ungetc(ch)
+ end
+ elsif ch == '\\' #'
+ str << read_escape
+ end
+ end
+ if @ltype == "/"
+ if peek(0) =~ /i|o|n|e|s/
+ str << getc
+ end
+ end
+ if subtype
+ Token(DLtype2Token[ltype], str)
+ else
+ Token(Ltype2Token[ltype], str)
+ end.set_text(str)
+ ensure
+ @ltype = nil
+ @quoted = nil
+ @lex_state = EXPR_END
+ end
+ end
+
+ def skip_inner_expression
+ res = ""
+ nest = 0
+ while (ch = getc)
+ res << ch
+ if ch == '}'
+ break if nest.zero?
+ nest -= 1
+ elsif ch == '{'
+ nest += 1
+ end
+ end
+ res
+ end
+
+ def identify_comment
+ @ltype = "#"
+ comment = "#"
+ while ch = getc
+ if ch == "\\"
+ ch = getc
+ if ch == "\n"
+ ch = " "
+ else
+ comment << "\\"
+ end
+ else
+ if ch == "\n"
+ @ltype = nil
+ ungetc
+ break
+ end
+ end
+ comment << ch
+ end
+ return Token(TkCOMMENT).set_text(comment)
+ end
+
+ def read_escape
+ res = ""
+ case ch = getc
+ when /[0-7]/
+ ungetc ch
+ 3.times do
+ case ch = getc
+ when /[0-7]/
+ when nil
+ break
+ else
+ ungetc
+ break
+ end
+ res << ch
+ end
+
+ when "x"
+ res << ch
+ 2.times do
+ case ch = getc
+ when /[0-9a-fA-F]/
+ when nil
+ break
+ else
+ ungetc
+ break
+ end
+ res << ch
+ end
+
+ when "M"
+ res << ch
+ if (ch = getc) != '-'
+ ungetc
+ else
+ res << ch
+ if (ch = getc) == "\\" #"
+ res << ch
+ res << read_escape
+ else
+ res << ch
+ end
+ end
+
+ when "C", "c" #, "^"
+ res << ch
+ if ch == "C" and (ch = getc) != "-"
+ ungetc
+ else
+ res << ch
+ if (ch = getc) == "\\" #"
+ res << ch
+ res << read_escape
+ else
+ res << ch
+ end
+ end
+ else
+ res << ch
+ end
+ res
+ end
+end
+
+##
+# Extracts code elements from a source file returning a TopLevel object
+# containing the constituent file elements.
+#
+# This file is based on rtags
+#
+# RubyParser understands how to document:
+# * classes
+# * modules
+# * methods
+# * constants
+# * aliases
+# * private, public, protected
+# * private_class_function, public_class_function
+# * module_function
+# * attr, attr_reader, attr_writer, attr_accessor
+# * extra accessors given on the command line
+# * metaprogrammed methods
+# * require
+# * include
+#
+# == Method Arguments
+#
+#--
+# NOTE: I don't think this works, needs tests, remove the paragraph following
+# this block when known to work
+#
+# The parser extracts the arguments from the method definition. You can
+# override this with a custom argument definition using the :args: directive:
+#
+# ##
+# # This method tries over and over until it is tired
+#
+# def go_go_go(thing_to_try, tries = 10) # :args: thing_to_try
+# puts thing_to_try
+# go_go_go thing_to_try, tries - 1
+# end
+#
+# If you have a more-complex set of overrides you can use the :call-seq:
+# directive:
+#++
+#
+# The parser extracts the arguments from the method definition. You can
+# override this with a custom argument definition using the :call-seq:
+# directive:
+#
+# ##
+# # This method can be called with a range or an offset and length
+# #
+# # :call-seq:
+# # my_method(Range)
+# # my_method(offset, length)
+#
+# def my_method(*args)
+# end
+#
+# The parser extracts +yield+ expressions from method bodies to gather the
+# yielded argument names. If your method manually calls a block instead of
+# yielding or you want to override the discovered argument names use
+# the :yields: directive:
+#
+# ##
+# # My method is awesome
+#
+# def my_method(&block) # :yields: happy, times
+# block.call 1, 2
+# end
+#
+# == Metaprogrammed Methods
+#
+# To pick up a metaprogrammed method, the parser looks for a comment starting
+# with '##' before an identifier:
+#
+# ##
+# # This is a meta-programmed method!
+#
+# add_my_method :meta_method, :arg1, :arg2
+#
+# The parser looks at the token after the identifier to determine the name, in
+# this example, :meta_method. If a name cannot be found, a warning is printed
+# and 'unknown is used.
+#
+# You can force the name of a method using the :method: directive:
+#
+# ##
+# # :method: woo_hoo!
+#
+# By default, meta-methods are instance methods. To indicate that a method is
+# a singleton method instead use the :singleton-method: directive:
+#
+# ##
+# # :singleton-method:
+#
+# You can also use the :singleton-method: directive with a name:
+#
+# ##
+# # :singleton-method: woo_hoo!
+#
+# == Hidden methods
+#
+# You can provide documentation for methods that don't appear using
+# the :method: and :singleton-method: directives:
+#
+# ##
+# # :method: ghost_method
+# # There is a method here, but you can't see it!
+#
+# ##
+# # this is a comment for a regular method
+#
+# def regular_method() end
+#
+# Note that by default, the :method: directive will be ignored if there is a
+# standard rdocable item following it.
+
+class RDoc::Parser::Ruby < RDoc::Parser
+
+ parse_files_matching(/\.rbw?$/)
+
+ include RDoc::RubyToken
+ include RDoc::TokenStream
+
+ NORMAL = "::"
+ SINGLE = "<<"
+
+ def initialize(top_level, file_name, content, options, stats)
+ super
+
+ @size = 0
+ @token_listeners = nil
+ @scanner = RDoc::RubyLex.new content, @options
+ @scanner.exception_on_syntax_error = false
+
+ reset
+ end
+
+ def add_token_listener(obj)
+ @token_listeners ||= []
+ @token_listeners << obj
+ end
+
+ ##
+ # Look for the first comment in a file that isn't a shebang line.
+
+ def collect_first_comment
+ skip_tkspace
+ res = ''
+ first_line = true
+
+ tk = get_tk
+
+ while TkCOMMENT === tk
+ if first_line and tk.text =~ /\A#!/ then
+ skip_tkspace
+ tk = get_tk
+ elsif first_line and tk.text =~ /\A#\s*-\*-/ then
+ first_line = false
+ skip_tkspace
+ tk = get_tk
+ else
+ first_line = false
+ res << tk.text << "\n"
+ tk = get_tk
+
+ if TkNL === tk then
+ skip_tkspace false
+ tk = get_tk
+ end
+ end
+ end
+
+ unget_tk tk
+
+ res
+ end
+
+ def error(msg)
+ msg = make_message msg
+ $stderr.puts msg
+ exit(1)
+ end
+
+ ##
+ # Look for a 'call-seq' in the comment, and override the normal parameter
+ # stuff
+
+ def extract_call_seq(comment, meth)
+ if comment.sub!(/:?call-seq:(.*?)^\s*\#?\s*$/m, '') then
+ seq = $1
+ seq.gsub!(/^\s*\#\s*/, '')
+ meth.call_seq = seq
+ end
+
+ meth
+ end
+
+ def get_bool
+ skip_tkspace
+ tk = get_tk
+ case tk
+ when TkTRUE
+ true
+ when TkFALSE, TkNIL
+ false
+ else
+ unget_tk tk
+ true
+ end
+ end
+
+ ##
+ # Look for the name of a class of module (optionally with a leading :: or
+ # with :: separated named) and return the ultimate name and container
+
+ def get_class_or_module(container)
+ skip_tkspace
+ name_t = get_tk
+
+ # class ::A -> A is in the top level
+ if TkCOLON2 === name_t then
+ name_t = get_tk
+ container = @top_level
+ end
+
+ skip_tkspace(false)
+
+ while TkCOLON2 === peek_tk do
+ prev_container = container
+ container = container.find_module_named(name_t.name)
+ if !container
+# warn("Couldn't find module #{name_t.name}")
+ container = prev_container.add_module RDoc::NormalModule, name_t.name
+ end
+ get_tk
+ name_t = get_tk
+ end
+ skip_tkspace(false)
+ return [container, name_t]
+ end
+
+ ##
+ # Return a superclass, which can be either a constant of an expression
+
+ def get_class_specification
+ tk = get_tk
+ return "self" if TkSELF === tk
+
+ res = ""
+ while TkCOLON2 === tk or TkCOLON3 === tk or TkCONSTANT === tk do
+ res += tk.text
+ tk = get_tk
+ end
+
+ unget_tk(tk)
+ skip_tkspace(false)
+
+ get_tkread # empty out read buffer
+
+ tk = get_tk
+
+ case tk
+ when TkNL, TkCOMMENT, TkSEMICOLON then
+ unget_tk(tk)
+ return res
+ end
+
+ res += parse_call_parameters(tk)
+ res
+ end
+
+ ##
+ # Parse a constant, which might be qualified by one or more class or module
+ # names
+
+ def get_constant
+ res = ""
+ skip_tkspace(false)
+ tk = get_tk
+
+ while TkCOLON2 === tk or TkCOLON3 === tk or TkCONSTANT === tk do
+ res += tk.text
+ tk = get_tk
+ end
+
+# if res.empty?
+# warn("Unexpected token #{tk} in constant")
+# end
+ unget_tk(tk)
+ res
+ end
+
+ ##
+ # Get a constant that may be surrounded by parens
+
+ def get_constant_with_optional_parens
+ skip_tkspace(false)
+ nest = 0
+ while TkLPAREN === (tk = peek_tk) or TkfLPAREN === tk do
+ get_tk
+ skip_tkspace(true)
+ nest += 1
+ end
+
+ name = get_constant
+
+ while nest > 0
+ skip_tkspace(true)
+ tk = get_tk
+ nest -= 1 if TkRPAREN === tk
+ end
+ name
+ end
+
+ def get_symbol_or_name
+ tk = get_tk
+ case tk
+ when TkSYMBOL
+ tk.text.sub(/^:/, '')
+ when TkId, TkOp
+ tk.name
+ when TkSTRING
+ tk.text
+ else
+ raise "Name or symbol expected (got #{tk})"
+ end
+ end
+
+ def get_tk
+ tk = nil
+ if @tokens.empty?
+ tk = @scanner.token
+ @read.push @scanner.get_read
+ puts "get_tk1 => #{tk.inspect}" if $TOKEN_DEBUG
+ else
+ @read.push @unget_read.shift
+ tk = @tokens.shift
+ puts "get_tk2 => #{tk.inspect}" if $TOKEN_DEBUG
+ end
+
+ if TkSYMBEG === tk then
+ set_token_position(tk.line_no, tk.char_no)
+ tk1 = get_tk
+ if TkId === tk1 or TkOp === tk1 or TkSTRING === tk1 then
+ if tk1.respond_to?(:name)
+ tk = Token(TkSYMBOL).set_text(":" + tk1.name)
+ else
+ tk = Token(TkSYMBOL).set_text(":" + tk1.text)
+ end
+ # remove the identifier we just read (we're about to
+ # replace it with a symbol)
+ @token_listeners.each do |obj|
+ obj.pop_token
+ end if @token_listeners
+ else
+ warn("':' not followed by identifier or operator")
+ tk = tk1
+ end
+ end
+
+ # inform any listeners of our shiny new token
+ @token_listeners.each do |obj|
+ obj.add_token(tk)
+ end if @token_listeners
+
+ tk
+ end
+
+ def get_tkread
+ read = @read.join("")
+ @read = []
+ read
+ end
+
+ ##
+ # Look for directives in a normal comment block:
+ #
+ # #-- - don't display comment from this point forward
+ #
+ # This routine modifies it's parameter
+
+ def look_for_directives_in(context, comment)
+ preprocess = RDoc::Markup::PreProcess.new(@file_name,
+ @options.rdoc_include)
+
+ preprocess.handle(comment) do |directive, param|
+ case directive
+ when 'enddoc' then
+ throw :enddoc
+ when 'main' then
+ @options.main_page = param
+ ''
+ when 'method', 'singleton-method' then
+ false # ignore
+ when 'section' then
+ context.set_current_section(param, comment)
+ comment.replace ''
+ break
+ when 'startdoc' then
+ context.start_doc
+ context.force_documentation = true
+ ''
+ when 'stopdoc' then
+ context.stop_doc
+ ''
+ when 'title' then
+ @options.title = param
+ ''
+ else
+ warn "Unrecognized directive '#{directive}'"
+ false
+ end
+ end
+
+ remove_private_comments(comment)
+ end
+
+ def make_message(msg)
+ prefix = "\n" + @file_name + ":"
+ if @scanner
+ prefix << "#{@scanner.line_no}:#{@scanner.char_no}: "
+ end
+ return prefix + msg
+ end
+
+ def parse_attr(context, single, tk, comment)
+ args = parse_symbol_arg(1)
+ if args.size > 0
+ name = args[0]
+ rw = "R"
+ skip_tkspace(false)
+ tk = get_tk
+ if TkCOMMA === tk then
+ rw = "RW" if get_bool
+ else
+ unget_tk tk
+ end
+ att = RDoc::Attr.new get_tkread, name, rw, comment
+ read_documentation_modifiers att, RDoc::ATTR_MODIFIERS
+ if att.document_self
+ context.add_attribute(att)
+ end
+ else
+ warn("'attr' ignored - looks like a variable")
+ end
+ end
+
+ def parse_attr_accessor(context, single, tk, comment)
+ args = parse_symbol_arg
+ read = get_tkread
+ rw = "?"
+
+ # If nodoc is given, don't document any of them
+
+ tmp = RDoc::CodeObject.new
+ read_documentation_modifiers tmp, RDoc::ATTR_MODIFIERS
+ return unless tmp.document_self
+
+ case tk.name
+ when "attr_reader" then rw = "R"
+ when "attr_writer" then rw = "W"
+ when "attr_accessor" then rw = "RW"
+ else
+ rw = @options.extra_accessor_flags[tk.name]
+ rw = '?' if rw.nil?
+ end
+
+ for name in args
+ att = RDoc::Attr.new get_tkread, name, rw, comment
+ context.add_attribute att
+ end
+ end
+
+ def parse_alias(context, single, tk, comment)
+ skip_tkspace
+ if TkLPAREN === peek_tk then
+ get_tk
+ skip_tkspace
+ end
+ new_name = get_symbol_or_name
+ @scanner.instance_eval{@lex_state = EXPR_FNAME}
+ skip_tkspace
+ if TkCOMMA === peek_tk then
+ get_tk
+ skip_tkspace
+ end
+ old_name = get_symbol_or_name
+
+ al = RDoc::Alias.new get_tkread, old_name, new_name, comment
+ read_documentation_modifiers al, RDoc::ATTR_MODIFIERS
+ if al.document_self
+ context.add_alias(al)
+ end
+ end
+
+ def parse_call_parameters(tk)
+ end_token = case tk
+ when TkLPAREN, TkfLPAREN
+ TkRPAREN
+ when TkRPAREN
+ return ""
+ else
+ TkNL
+ end
+ nest = 0
+
+ loop do
+ case tk
+ when TkSEMICOLON
+ break
+ when TkLPAREN, TkfLPAREN
+ nest += 1
+ when end_token
+ if end_token == TkRPAREN
+ nest -= 1
+ break if @scanner.lex_state == EXPR_END and nest <= 0
+ else
+ break unless @scanner.continue
+ end
+ when TkCOMMENT
+ unget_tk(tk)
+ break
+ end
+ tk = get_tk
+ end
+ res = get_tkread.tr("\n", " ").strip
+ res = "" if res == ";"
+ res
+ end
+
+ def parse_class(container, single, tk, comment)
+ container, name_t = get_class_or_module(container)
+
+ case name_t
+ when TkCONSTANT
+ name = name_t.name
+ superclass = "Object"
+
+ if TkLT === peek_tk then
+ get_tk
+ skip_tkspace(true)
+ superclass = get_class_specification
+ superclass = "<unknown>" if superclass.empty?
+ end
+
+ cls_type = single == SINGLE ? RDoc::SingleClass : RDoc::NormalClass
+ cls = container.add_class cls_type, name, superclass
+
+ @stats.add_class cls
+
+ read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
+ cls.record_location @top_level
+
+ parse_statements cls
+ cls.comment = comment
+
+ when TkLSHFT
+ case name = get_class_specification
+ when "self", container.name
+ parse_statements(container, SINGLE)
+ else
+ other = RDoc::TopLevel.find_class_named(name)
+ unless other
+ # other = @top_level.add_class(NormalClass, name, nil)
+ # other.record_location(@top_level)
+ # other.comment = comment
+ other = RDoc::NormalClass.new "Dummy", nil
+ end
+
+ @stats.add_class other
+
+ read_documentation_modifiers other, RDoc::CLASS_MODIFIERS
+ parse_statements(other, SINGLE)
+ end
+
+ else
+ warn("Expected class name or '<<'. Got #{name_t.class}: #{name_t.text.inspect}")
+ end
+ end
+
+ def parse_constant(container, single, tk, comment)
+ name = tk.name
+ skip_tkspace(false)
+ eq_tk = get_tk
+
+ unless TkASSIGN === eq_tk then
+ unget_tk(eq_tk)
+ return
+ end
+
+
+ nest = 0
+ get_tkread
+
+ tk = get_tk
+ if TkGT === tk then
+ unget_tk(tk)
+ unget_tk(eq_tk)
+ return
+ end
+
+ loop do
+ case tk
+ when TkSEMICOLON
+ break
+ when TkLPAREN, TkfLPAREN, TkLBRACE, TkLBRACK, TkDO
+ nest += 1
+ when TkRPAREN, TkRBRACE, TkRBRACK, TkEND
+ nest -= 1
+ when TkCOMMENT
+ if nest <= 0 && @scanner.lex_state == EXPR_END
+ unget_tk(tk)
+ break
+ end
+ when TkNL
+ if (nest <= 0) && ((@scanner.lex_state == EXPR_END) || (!@scanner.continue))
+ unget_tk(tk)
+ break
+ end
+ end
+ tk = get_tk
+ end
+
+ res = get_tkread.tr("\n", " ").strip
+ res = "" if res == ";"
+
+ con = RDoc::Constant.new name, res, comment
+ read_documentation_modifiers con, RDoc::CONSTANT_MODIFIERS
+
+ if con.document_self
+ container.add_constant(con)
+ end
+ end
+
+ def parse_comment(container, tk, comment)
+ line_no = tk.line_no
+ column = tk.char_no
+
+ singleton = !!comment.sub!(/(^# +:?)(singleton-)(method:)/, '\1\3')
+
+ if comment.sub!(/^# +:?method: *(\S*).*?\n/i, '') then
+ name = $1 unless $1.empty?
+ else
+ return nil
+ end
+
+ meth = RDoc::GhostMethod.new get_tkread, name
+ meth.singleton = singleton
+
+ @stats.add_method meth
+
+ meth.start_collecting_tokens
+ indent = TkSPACE.new 1, 1
+ indent.set_text " " * column
+
+ position_comment = TkCOMMENT.new(line_no, 1, "# File #{@top_level.file_absolute_name}, line #{line_no}")
+ meth.add_tokens [position_comment, NEWLINE_TOKEN, indent]
+
+ meth.params = ''
+
+ extract_call_seq comment, meth
+
+ container.add_method meth if meth.document_self
+
+ meth.comment = comment
+ end
+
+ def parse_include(context, comment)
+ loop do
+ skip_tkspace_comment
+
+ name = get_constant_with_optional_parens
+ context.add_include RDoc::Include.new(name, comment) unless name.empty?
+
+ return unless TkCOMMA === peek_tk
+ get_tk
+ end
+ end
+
+ ##
+ # Parses a meta-programmed method
+
+ def parse_meta_method(container, single, tk, comment)
+ line_no = tk.line_no
+ column = tk.char_no
+
+ start_collecting_tokens
+ add_token tk
+ add_token_listener self
+
+ skip_tkspace false
+
+ singleton = !!comment.sub!(/(^# +:?)(singleton-)(method:)/, '\1\3')
+
+ if comment.sub!(/^# +:?method: *(\S*).*?\n/i, '') then
+ name = $1 unless $1.empty?
+ end
+
+ if name.nil? then
+ name_t = get_tk
+ case name_t
+ when TkSYMBOL then
+ name = name_t.text[1..-1]
+ when TkSTRING then
+ name = name_t.text[1..-2]
+ else
+ warn "#{container.top_level.file_relative_name}:#{name_t.line_no} unknown name token #{name_t.inspect} for meta-method"
+ name = 'unknown'
+ end
+ end
+
+ meth = RDoc::MetaMethod.new get_tkread, name
+ meth.singleton = singleton
+
+ @stats.add_method meth
+
+ remove_token_listener self
+
+ meth.start_collecting_tokens
+ indent = TkSPACE.new 1, 1
+ indent.set_text " " * column
+
+ position_comment = TkCOMMENT.new(line_no, 1, "# File #{@top_level.file_absolute_name}, line #{line_no}")
+ meth.add_tokens [position_comment, NEWLINE_TOKEN, indent]
+ meth.add_tokens @token_stream
+
+ add_token_listener meth
+
+ meth.params = ''
+
+ extract_call_seq comment, meth
+
+ container.add_method meth if meth.document_self
+
+ last_tk = tk
+
+ while tk = get_tk do
+ case tk
+ when TkSEMICOLON then
+ break
+ when TkNL then
+ break unless last_tk and TkCOMMA === last_tk
+ when TkSPACE then
+ # expression continues
+ else
+ last_tk = tk
+ end
+ end
+
+ remove_token_listener meth
+
+ meth.comment = comment
+ end
+
+ ##
+ # Parses a method
+
+ def parse_method(container, single, tk, comment)
+ line_no = tk.line_no
+ column = tk.char_no
+
+ start_collecting_tokens
+ add_token(tk)
+ add_token_listener(self)
+
+ @scanner.instance_eval do @lex_state = EXPR_FNAME end
+
+ skip_tkspace(false)
+ name_t = get_tk
+ back_tk = skip_tkspace
+ meth = nil
+ added_container = false
+
+ dot = get_tk
+ if TkDOT === dot or TkCOLON2 === dot then
+ @scanner.instance_eval do @lex_state = EXPR_FNAME end
+ skip_tkspace
+ name_t2 = get_tk
+
+ case name_t
+ when TkSELF then
+ name = name_t2.name
+ when TkCONSTANT then
+ name = name_t2.name
+ prev_container = container
+ container = container.find_module_named(name_t.name)
+ unless container then
+ added_container = true
+ obj = name_t.name.split("::").inject(Object) do |state, item|
+ state.const_get(item)
+ end rescue nil
+
+ type = obj.class == Class ? RDoc::NormalClass : RDoc::NormalModule
+
+ unless [Class, Module].include?(obj.class) then
+ warn("Couldn't find #{name_t.name}. Assuming it's a module")
+ end
+
+ if type == RDoc::NormalClass then
+ container = prev_container.add_class(type, name_t.name, obj.superclass.name)
+ else
+ container = prev_container.add_module(type, name_t.name)
+ end
+
+ container.record_location @top_level
+ end
+ else
+ # warn("Unexpected token '#{name_t2.inspect}'")
+ # break
+ skip_method(container)
+ return
+ end
+
+ meth = RDoc::AnyMethod.new(get_tkread, name)
+ meth.singleton = true
+ else
+ unget_tk dot
+ back_tk.reverse_each do |token|
+ unget_tk token
+ end
+ name = name_t.name
+
+ meth = RDoc::AnyMethod.new get_tkread, name
+ meth.singleton = (single == SINGLE)
+ end
+
+ @stats.add_method meth
+
+ remove_token_listener self
+
+ meth.start_collecting_tokens
+ indent = TkSPACE.new 1, 1
+ indent.set_text " " * column
+
+ token = TkCOMMENT.new(line_no, 1, "# File #{@top_level.file_absolute_name}, line #{line_no}")
+ meth.add_tokens [token, NEWLINE_TOKEN, indent]
+ meth.add_tokens @token_stream
+
+ add_token_listener meth
+
+ @scanner.instance_eval do @continue = false end
+ parse_method_parameters meth
+
+ if meth.document_self then
+ container.add_method meth
+ elsif added_container then
+ container.document_self = false
+ end
+
+ # Having now read the method parameters and documentation modifiers, we
+ # now know whether we have to rename #initialize to ::new
+
+ if name == "initialize" && !meth.singleton then
+ if meth.dont_rename_initialize then
+ meth.visibility = :protected
+ else
+ meth.singleton = true
+ meth.name = "new"
+ meth.visibility = :public
+ end
+ end
+
+ parse_statements(container, single, meth)
+
+ remove_token_listener(meth)
+
+ extract_call_seq comment, meth
+
+ meth.comment = comment
+ end
+
+ def parse_method_or_yield_parameters(method = nil,
+ modifiers = RDoc::METHOD_MODIFIERS)
+ skip_tkspace(false)
+ tk = get_tk
+
+ # Little hack going on here. In the statement
+ # f = 2*(1+yield)
+ # We see the RPAREN as the next token, so we need
+ # to exit early. This still won't catch all cases
+ # (such as "a = yield + 1"
+ end_token = case tk
+ when TkLPAREN, TkfLPAREN
+ TkRPAREN
+ when TkRPAREN
+ return ""
+ else
+ TkNL
+ end
+ nest = 0
+
+ loop do
+ case tk
+ when TkSEMICOLON
+ break
+ when TkLBRACE
+ nest += 1
+ when TkRBRACE
+ # we might have a.each {|i| yield i }
+ unget_tk(tk) if nest.zero?
+ nest -= 1
+ break if nest <= 0
+ when TkLPAREN, TkfLPAREN
+ nest += 1
+ when end_token
+ if end_token == TkRPAREN
+ nest -= 1
+ break if @scanner.lex_state == EXPR_END and nest <= 0
+ else
+ break unless @scanner.continue
+ end
+ when method && method.block_params.nil? && TkCOMMENT
+ unget_tk(tk)
+ read_documentation_modifiers(method, modifiers)
+ end
+ tk = get_tk
+ end
+ res = get_tkread.tr("\n", " ").strip
+ res = "" if res == ";"
+ res
+ end
+
+ ##
+ # Capture the method's parameters. Along the way, look for a comment
+ # containing:
+ #
+ # # yields: ....
+ #
+ # and add this as the block_params for the method
+
+ def parse_method_parameters(method)
+ res = parse_method_or_yield_parameters(method)
+ res = "(" + res + ")" unless res[0] == ?(
+ method.params = res unless method.params
+ if method.block_params.nil?
+ skip_tkspace(false)
+ read_documentation_modifiers method, RDoc::METHOD_MODIFIERS
+ end
+ end
+
+ def parse_module(container, single, tk, comment)
+ container, name_t = get_class_or_module(container)
+
+ name = name_t.name
+
+ mod = container.add_module RDoc::NormalModule, name
+ mod.record_location @top_level
+
+ @stats.add_module mod
+
+ read_documentation_modifiers mod, RDoc::CLASS_MODIFIERS
+ parse_statements(mod)
+ mod.comment = comment
+ end
+
+ def parse_require(context, comment)
+ skip_tkspace_comment
+ tk = get_tk
+ if TkLPAREN === tk then
+ skip_tkspace_comment
+ tk = get_tk
+ end
+
+ name = nil
+ case tk
+ when TkSTRING
+ name = tk.text
+ # when TkCONSTANT, TkIDENTIFIER, TkIVAR, TkGVAR
+ # name = tk.name
+ when TkDSTRING
+ warn "Skipping require of dynamic string: #{tk.text}"
+ # else
+ # warn "'require' used as variable"
+ end
+ if name
+ context.add_require RDoc::Require.new(name, comment)
+ else
+ unget_tk(tk)
+ end
+ end
+
+ def parse_statements(container, single = NORMAL, current_method = nil,
+ comment = '')
+ nest = 1
+ save_visibility = container.visibility
+
+ non_comment_seen = true
+
+ while tk = get_tk do
+ keep_comment = false
+
+ non_comment_seen = true unless TkCOMMENT === tk
+
+ case tk
+ when TkNL then
+ skip_tkspace true # Skip blanks and newlines
+ tk = get_tk
+
+ if TkCOMMENT === tk then
+ if non_comment_seen then
+ # Look for RDoc in a comment about to be thrown away
+ parse_comment container, tk, comment unless comment.empty?
+
+ comment = ''
+ non_comment_seen = false
+ end
+
+ while TkCOMMENT === tk do
+ comment << tk.text << "\n"
+ tk = get_tk # this is the newline
+ skip_tkspace(false) # leading spaces
+ tk = get_tk
+ end
+
+ unless comment.empty? then
+ look_for_directives_in container, comment
+
+ if container.done_documenting then
+ container.ongoing_visibility = save_visibility
+ end
+ end
+
+ keep_comment = true
+ else
+ non_comment_seen = true
+ end
+
+ unget_tk tk
+ keep_comment = true
+
+ when TkCLASS then
+ if container.document_children then
+ parse_class container, single, tk, comment
+ else
+ nest += 1
+ end
+
+ when TkMODULE then
+ if container.document_children then
+ parse_module container, single, tk, comment
+ else
+ nest += 1
+ end
+
+ when TkDEF then
+ if container.document_self then
+ parse_method container, single, tk, comment
+ else
+ nest += 1
+ end
+
+ when TkCONSTANT then
+ if container.document_self then
+ parse_constant container, single, tk, comment
+ end
+
+ when TkALIAS then
+ if container.document_self then
+ parse_alias container, single, tk, comment
+ end
+
+ when TkYIELD then
+ if current_method.nil? then
+ warn "Warning: yield outside of method" if container.document_self
+ else
+ parse_yield container, single, tk, current_method
+ end
+
+ # Until and While can have a 'do', which shouldn't increase the nesting.
+ # We can't solve the general case, but we can handle most occurrences by
+ # ignoring a do at the end of a line.
+
+ when TkUNTIL, TkWHILE then
+ nest += 1
+ skip_optional_do_after_expression
+
+ # 'for' is trickier
+ when TkFOR then
+ nest += 1
+ skip_for_variable
+ skip_optional_do_after_expression
+
+ when TkCASE, TkDO, TkIF, TkUNLESS, TkBEGIN then
+ nest += 1
+
+ when TkIDENTIFIER then
+ if nest == 1 and current_method.nil? then
+ case tk.name
+ when 'private', 'protected', 'public', 'private_class_method',
+ 'public_class_method', 'module_function' then
+ parse_visibility container, single, tk
+ keep_comment = true
+ when 'attr' then
+ parse_attr container, single, tk, comment
+ when /^attr_(reader|writer|accessor)$/, @options.extra_accessors then
+ parse_attr_accessor container, single, tk, comment
+ when 'alias_method' then
+ if container.document_self then
+ parse_alias container, single, tk, comment
+ end
+ else
+ if container.document_self and comment =~ /\A#\#$/ then
+ parse_meta_method container, single, tk, comment
+ end
+ end
+ end
+
+ case tk.name
+ when "require" then
+ parse_require container, comment
+ when "include" then
+ parse_include container, comment
+ end
+
+ when TkEND then
+ nest -= 1
+ if nest == 0 then
+ read_documentation_modifiers container, RDoc::CLASS_MODIFIERS
+ container.ongoing_visibility = save_visibility
+ return
+ end
+
+ end
+
+ comment = '' unless keep_comment
+
+ begin
+ get_tkread
+ skip_tkspace(false)
+ end while peek_tk == TkNL
+ end
+ end
+
+ def parse_symbol_arg(no = nil)
+ args = []
+ skip_tkspace_comment
+ case tk = get_tk
+ when TkLPAREN
+ loop do
+ skip_tkspace_comment
+ if tk1 = parse_symbol_in_arg
+ args.push tk1
+ break if no and args.size >= no
+ end
+
+ skip_tkspace_comment
+ case tk2 = get_tk
+ when TkRPAREN
+ break
+ when TkCOMMA
+ else
+ warn("unexpected token: '#{tk2.inspect}'") if $DEBUG_RDOC
+ break
+ end
+ end
+ else
+ unget_tk tk
+ if tk = parse_symbol_in_arg
+ args.push tk
+ return args if no and args.size >= no
+ end
+
+ loop do
+ skip_tkspace(false)
+
+ tk1 = get_tk
+ unless TkCOMMA === tk1 then
+ unget_tk tk1
+ break
+ end
+
+ skip_tkspace_comment
+ if tk = parse_symbol_in_arg
+ args.push tk
+ break if no and args.size >= no
+ end
+ end
+ end
+ args
+ end
+
+ def parse_symbol_in_arg
+ case tk = get_tk
+ when TkSYMBOL
+ tk.text.sub(/^:/, '')
+ when TkSTRING
+ eval @read[-1]
+ else
+ warn("Expected symbol or string, got #{tk.inspect}") if $DEBUG_RDOC
+ nil
+ end
+ end
+
+ def parse_toplevel_statements(container)
+ comment = collect_first_comment
+ look_for_directives_in(container, comment)
+ container.comment = comment unless comment.empty?
+ parse_statements container, NORMAL, nil, comment
+ end
+
+ def parse_visibility(container, single, tk)
+ singleton = (single == SINGLE)
+
+ vis_type = tk.name
+
+ vis = case vis_type
+ when 'private' then :private
+ when 'protected' then :protected
+ when 'public' then :public
+ when 'private_class_method' then
+ singleton = true
+ :private
+ when 'public_class_method' then
+ singleton = true
+ :public
+ when 'module_function' then
+ singleton = true
+ :public
+ else
+ raise "Invalid visibility: #{tk.name}"
+ end
+
+ skip_tkspace_comment false
+
+ case peek_tk
+ # Ryan Davis suggested the extension to ignore modifiers, because he
+ # often writes
+ #
+ # protected unless $TESTING
+ #
+ when TkNL, TkUNLESS_MOD, TkIF_MOD, TkSEMICOLON then
+ container.ongoing_visibility = vis
+ else
+ if vis_type == 'module_function' then
+ args = parse_symbol_arg
+ container.set_visibility_for args, :private, false
+
+ module_functions = []
+
+ container.methods_matching args do |m|
+ s_m = m.dup
+ s_m.singleton = true if RDoc::AnyMethod === s_m
+ s_m.visibility = :public
+ module_functions << s_m
+ end
+
+ module_functions.each do |s_m|
+ case s_m
+ when RDoc::AnyMethod then
+ container.add_method s_m
+ when RDoc::Attr then
+ container.add_attribute s_m
+ end
+ end
+ else
+ args = parse_symbol_arg
+ container.set_visibility_for args, vis, singleton
+ end
+ end
+ end
+
+ def parse_yield_parameters
+ parse_method_or_yield_parameters
+ end
+
+ def parse_yield(context, single, tk, method)
+ if method.block_params.nil?
+ get_tkread
+ @scanner.instance_eval{@continue = false}
+ method.block_params = parse_yield_parameters
+ end
+ end
+
+ def peek_read
+ @read.join('')
+ end
+
+ ##
+ # Peek at the next token, but don't remove it from the stream
+
+ def peek_tk
+ unget_tk(tk = get_tk)
+ tk
+ end
+
+ ##
+ # Directives are modifier comments that can appear after class, module, or
+ # method names. For example:
+ #
+ # def fred # :yields: a, b
+ #
+ # or:
+ #
+ # class MyClass # :nodoc:
+ #
+ # We return the directive name and any parameters as a two element array
+
+ def read_directive(allowed)
+ tk = get_tk
+ result = nil
+ if TkCOMMENT === tk
+ if tk.text =~ /\s*:?(\w+):\s*(.*)/
+ directive = $1.downcase
+ if allowed.include?(directive)
+ result = [directive, $2]
+ end
+ end
+ else
+ unget_tk(tk)
+ end
+ result
+ end
+
+ def read_documentation_modifiers(context, allow)
+ dir = read_directive(allow)
+
+ case dir[0]
+ when "notnew", "not_new", "not-new" then
+ context.dont_rename_initialize = true
+
+ when "nodoc" then
+ context.document_self = false
+ if dir[1].downcase == "all"
+ context.document_children = false
+ end
+
+ when "doc" then
+ context.document_self = true
+ context.force_documentation = true
+
+ when "yield", "yields" then
+ unless context.params.nil?
+ context.params.sub!(/(,|)\s*&\w+/,'') # remove parameter &proc
+ end
+
+ context.block_params = dir[1]
+
+ when "arg", "args" then
+ context.params = dir[1]
+ end if dir
+ end
+
+ def remove_private_comments(comment)
+ comment.gsub!(/^#--\n.*?^#\+\+/m, '')
+ comment.sub!(/^#--\n.*/m, '')
+ end
+
+ def remove_token_listener(obj)
+ @token_listeners.delete(obj)
+ end
+
+ def reset
+ @tokens = []
+ @unget_read = []
+ @read = []
+ end
+
+ def scan
+ reset
+
+ catch(:eof) do
+ catch(:enddoc) do
+ begin
+ parse_toplevel_statements(@top_level)
+ rescue Exception => e
+ $stderr.puts <<-EOF
+
+
+RDoc failure in #{@file_name} at or around line #{@scanner.line_no} column
+#{@scanner.char_no}
+
+Before reporting this, could you check that the file you're documenting
+compiles cleanly--RDoc is not a full Ruby parser, and gets confused easily if
+fed invalid programs.
+
+The internal error was:
+
+ EOF
+
+ e.set_backtrace(e.backtrace[0,4])
+ raise
+ end
+ end
+ end
+
+ @top_level
+ end
+
+ ##
+ # while, until, and for have an optional do
+
+ def skip_optional_do_after_expression
+ skip_tkspace(false)
+ tk = get_tk
+ case tk
+ when TkLPAREN, TkfLPAREN
+ end_token = TkRPAREN
+ else
+ end_token = TkNL
+ end
+
+ nest = 0
+ @scanner.instance_eval{@continue = false}
+
+ loop do
+ case tk
+ when TkSEMICOLON
+ break
+ when TkLPAREN, TkfLPAREN
+ nest += 1
+ when TkDO
+ break if nest.zero?
+ when end_token
+ if end_token == TkRPAREN
+ nest -= 1
+ break if @scanner.lex_state == EXPR_END and nest.zero?
+ else
+ break unless @scanner.continue
+ end
+ end
+ tk = get_tk
+ end
+ skip_tkspace(false)
+
+ get_tk if TkDO === peek_tk
+ end
+
+ ##
+ # skip the var [in] part of a 'for' statement
+
+ def skip_for_variable
+ skip_tkspace(false)
+ tk = get_tk
+ skip_tkspace(false)
+ tk = get_tk
+ unget_tk(tk) unless TkIN === tk
+ end
+
+ def skip_method(container)
+ meth = RDoc::AnyMethod.new "", "anon"
+ parse_method_parameters(meth)
+ parse_statements(container, false, meth)
+ end
+
+ ##
+ # Skip spaces
+
+ def skip_tkspace(skip_nl = true)
+ tokens = []
+
+ while TkSPACE === (tk = get_tk) or (skip_nl and TkNL === tk) do
+ tokens.push tk
+ end
+
+ unget_tk(tk)
+ tokens
+ end
+
+ ##
+ # Skip spaces until a comment is found
+
+ def skip_tkspace_comment(skip_nl = true)
+ loop do
+ skip_tkspace(skip_nl)
+ return unless TkCOMMENT === peek_tk
+ get_tk
+ end
+ end
+
+ def unget_tk(tk)
+ @tokens.unshift tk
+ @unget_read.unshift @read.pop
+
+ # Remove this token from any listeners
+ @token_listeners.each do |obj|
+ obj.pop_token
+ end if @token_listeners
+ end
+
+ def warn(msg)
+ return if @options.quiet
+ msg = make_message msg
+ $stderr.puts msg
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/parser/simple.rb b/ruby/lib/rdoc/parser/simple.rb
new file mode 100644
index 0000000..cdfe686
--- /dev/null
+++ b/ruby/lib/rdoc/parser/simple.rb
@@ -0,0 +1,38 @@
+require 'rdoc/parser'
+
+##
+# Parse a non-source file. We basically take the whole thing as one big
+# comment. If the first character in the file is '#', we strip leading pound
+# signs.
+
+class RDoc::Parser::Simple < RDoc::Parser
+
+ parse_files_matching(//)
+
+ ##
+ # Prepare to parse a plain file
+
+ def initialize(top_level, file_name, content, options, stats)
+ super
+
+ preprocess = RDoc::Markup::PreProcess.new @file_name, @options.rdoc_include
+
+ preprocess.handle @content do |directive, param|
+ warn "Unrecognized directive '#{directive}' in #{@file_name}"
+ end
+ end
+
+ ##
+ # Extract the file contents and attach them to the toplevel as a comment
+
+ def scan
+ @top_level.comment = remove_private_comments(@content)
+ @top_level
+ end
+
+ def remove_private_comments(comment)
+ comment.gsub(/^--\n.*?^\+\+/m, '').sub(/^--\n.*/m, '')
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/rdoc.rb b/ruby/lib/rdoc/rdoc.rb
new file mode 100644
index 0000000..ce1cb1a
--- /dev/null
+++ b/ruby/lib/rdoc/rdoc.rb
@@ -0,0 +1,293 @@
+require 'rdoc'
+
+require 'rdoc/parser'
+
+# Simple must come first
+require 'rdoc/parser/simple'
+require 'rdoc/parser/ruby'
+require 'rdoc/parser/c'
+require 'rdoc/parser/f95'
+require 'rdoc/parser/perl'
+
+require 'rdoc/stats'
+require 'rdoc/options'
+
+require 'rdoc/diagram'
+
+require 'find'
+require 'fileutils'
+require 'time'
+
+module RDoc
+
+ ##
+ # Encapsulate the production of rdoc documentation. Basically you can use
+ # this as you would invoke rdoc from the command line:
+ #
+ # rdoc = RDoc::RDoc.new
+ # rdoc.document(args)
+ #
+ # Where +args+ is an array of strings, each corresponding to an argument
+ # you'd give rdoc on the command line. See rdoc/rdoc.rb for details.
+
+ class RDoc
+
+ Generator = Struct.new(:file_name, :class_name, :key)
+
+ ##
+ # Accessor for statistics. Available after each call to parse_files
+
+ attr_reader :stats
+
+ ##
+ # This is the list of output generator that we support
+
+ GENERATORS = {}
+
+ $LOAD_PATH.collect do |d|
+ File.expand_path d
+ end.find_all do |d|
+ File.directory? "#{d}/rdoc/generator"
+ end.each do |dir|
+ Dir.entries("#{dir}/rdoc/generator").each do |gen|
+ next unless /(\w+)\.rb$/ =~ gen
+ type = $1
+ unless GENERATORS.has_key? type
+ GENERATORS[type] = Generator.new("rdoc/generator/#{gen}",
+ "#{type.upcase}".intern,
+ type)
+ end
+ end
+ end
+
+ def initialize
+ @stats = nil
+ end
+
+ ##
+ # Report an error message and exit
+
+ def error(msg)
+ raise ::RDoc::Error, msg
+ end
+
+ ##
+ # Create an output dir if it doesn't exist. If it does exist, but doesn't
+ # contain the flag file <tt>created.rid</tt> then we refuse to use it, as
+ # we may clobber some manually generated documentation
+
+ def setup_output_dir(op_dir, force)
+ flag_file = output_flag_file(op_dir)
+ if File.exist?(op_dir)
+ unless File.directory?(op_dir)
+ error "'#{op_dir}' exists, and is not a directory"
+ end
+ begin
+ created = File.read(flag_file)
+ rescue SystemCallError
+ error "\nDirectory #{op_dir} already exists, but it looks like it\n" +
+ "isn't an RDoc directory. Because RDoc doesn't want to risk\n" +
+ "destroying any of your existing files, you'll need to\n" +
+ "specify a different output directory name (using the\n" +
+ "--op <dir> option).\n\n"
+ else
+ last = (Time.parse(created) unless force rescue nil)
+ end
+ else
+ FileUtils.mkdir_p(op_dir)
+ end
+ last
+ end
+
+ ##
+ # Update the flag file in an output directory.
+
+ def update_output_dir(op_dir, time)
+ File.open(output_flag_file(op_dir), "w") {|f| f.puts time.rfc2822 }
+ end
+
+ ##
+ # Return the path name of the flag file in an output directory.
+
+ def output_flag_file(op_dir)
+ File.join(op_dir, "created.rid")
+ end
+
+ ##
+ # The .document file contains a list of file and directory name patterns,
+ # representing candidates for documentation. It may also contain comments
+ # (starting with '#')
+
+ def parse_dot_doc_file(in_dir, filename, options)
+ # read and strip comments
+ patterns = File.read(filename).gsub(/#.*/, '')
+
+ result = []
+
+ patterns.split.each do |patt|
+ candidates = Dir.glob(File.join(in_dir, patt))
+ result.concat(normalized_file_list(options, candidates))
+ end
+ result
+ end
+
+ ##
+ # Given a list of files and directories, create a list of all the Ruby
+ # files they contain.
+ #
+ # If +force_doc+ is true we always add the given files, if false, only
+ # add files that we guarantee we can parse. It is true when looking at
+ # files given on the command line, false when recursing through
+ # subdirectories.
+ #
+ # The effect of this is that if you want a file with a non-standard
+ # extension parsed, you must name it explicitly.
+
+ def normalized_file_list(options, relative_files, force_doc = false,
+ exclude_pattern = nil)
+ file_list = []
+
+ relative_files.each do |rel_file_name|
+ next if exclude_pattern && exclude_pattern =~ rel_file_name
+ stat = File.stat(rel_file_name)
+ case type = stat.ftype
+ when "file"
+ next if @last_created and stat.mtime < @last_created
+
+ if force_doc or ::RDoc::Parser.can_parse(rel_file_name) then
+ file_list << rel_file_name.sub(/^\.\//, '')
+ end
+ when "directory"
+ next if rel_file_name == "CVS" || rel_file_name == ".svn"
+ dot_doc = File.join(rel_file_name, DOT_DOC_FILENAME)
+ if File.file?(dot_doc)
+ file_list.concat(parse_dot_doc_file(rel_file_name, dot_doc, options))
+ else
+ file_list.concat(list_files_in_directory(rel_file_name, options))
+ end
+ else
+ raise RDoc::Error, "I can't deal with a #{type} #{rel_file_name}"
+ end
+ end
+
+ file_list
+ end
+
+ ##
+ # Return a list of the files to be processed in a directory. We know that
+ # this directory doesn't have a .document file, so we're looking for real
+ # files. However we may well contain subdirectories which must be tested
+ # for .document files.
+
+ def list_files_in_directory(dir, options)
+ files = Dir.glob File.join(dir, "*")
+
+ normalized_file_list options, files, false, options.exclude
+ end
+
+ ##
+ # Parse each file on the command line, recursively entering directories.
+
+ def parse_files(options)
+ @stats = Stats.new options.verbosity
+
+ files = options.files
+ files = ["."] if files.empty?
+
+ file_list = normalized_file_list(options, files, true, options.exclude)
+
+ return [] if file_list.empty?
+
+ file_info = []
+
+ file_list.each do |filename|
+ @stats.add_file filename
+
+ content = if RUBY_VERSION >= '1.9' then
+ File.open(filename, "r:ascii-8bit") { |f| f.read }
+ else
+ File.read filename
+ end
+
+ if defined? Encoding then
+ if /coding:\s*(\S+)/ =~ content[/\A(?:.*\n){0,2}/]
+ if enc = ::Encoding.find($1)
+ content.force_encoding(enc)
+ end
+ end
+ end
+
+ top_level = ::RDoc::TopLevel.new filename
+
+ parser = ::RDoc::Parser.for top_level, filename, content, options,
+ @stats
+
+ file_info << parser.scan
+ end
+
+ file_info
+ end
+
+ ##
+ # Format up one or more files according to the given arguments.
+ #
+ # For simplicity, _argv_ is an array of strings, equivalent to the strings
+ # that would be passed on the command line. (This isn't a coincidence, as
+ # we _do_ pass in ARGV when running interactively). For a list of options,
+ # see rdoc/rdoc.rb. By default, output will be stored in a directory
+ # called +doc+ below the current directory, so make sure you're somewhere
+ # writable before invoking.
+ #
+ # Throws: RDoc::Error on error
+
+ def document(argv)
+ TopLevel::reset
+
+ @options = Options.new GENERATORS
+ @options.parse argv
+
+ @last_created = nil
+
+ unless @options.all_one_file then
+ @last_created = setup_output_dir @options.op_dir, @options.force_update
+ end
+
+ start_time = Time.now
+
+ file_info = parse_files @options
+
+ @options.title = "RDoc Documentation"
+
+ if file_info.empty?
+ $stderr.puts "\nNo newer files." unless @options.quiet
+ else
+ @gen = @options.generator
+
+ $stderr.puts "\nGenerating #{@gen.key.upcase}..." unless @options.quiet
+
+ require @gen.file_name
+
+ gen_class = ::RDoc::Generator.const_get @gen.class_name
+ @gen = gen_class.for @options
+
+ pwd = Dir.pwd
+
+ Dir.chdir @options.op_dir unless @options.all_one_file
+
+ begin
+ Diagram.new(file_info, @options).draw if @options.diagram
+ @gen.generate(file_info)
+ update_output_dir(".", start_time)
+ ensure
+ Dir.chdir(pwd)
+ end
+ end
+
+ unless @options.quiet
+ puts
+ @stats.print
+ end
+ end
+ end
+end
+
diff --git a/ruby/lib/rdoc/ri.rb b/ruby/lib/rdoc/ri.rb
new file mode 100644
index 0000000..a3a858e
--- /dev/null
+++ b/ruby/lib/rdoc/ri.rb
@@ -0,0 +1,8 @@
+require 'rdoc'
+
+module RDoc::RI
+
+ class Error < RDoc::Error; end
+
+end
+
diff --git a/ruby/lib/rdoc/ri/cache.rb b/ruby/lib/rdoc/ri/cache.rb
new file mode 100644
index 0000000..06177a0
--- /dev/null
+++ b/ruby/lib/rdoc/ri/cache.rb
@@ -0,0 +1,187 @@
+require 'rdoc/ri'
+
+class RDoc::RI::ClassEntry
+
+ attr_reader :name
+ attr_reader :path_names
+
+ def initialize(path_name, name, in_class)
+ @path_names = [ path_name ]
+ @name = name
+ @in_class = in_class
+ @class_methods = []
+ @instance_methods = []
+ @inferior_classes = []
+ end
+
+ # We found this class in more than one place, so add
+ # in the name from there.
+ def add_path(path)
+ @path_names << path
+ end
+
+ # read in our methods and any classes
+ # and modules in our namespace. Methods are
+ # stored in files called name-c|i.yaml,
+ # where the 'name' portion is the external
+ # form of the method name and the c|i is a class|instance
+ # flag
+
+ def load_from(dir)
+ Dir.foreach(dir) do |name|
+ next if name =~ /^\./
+
+ # convert from external to internal form, and
+ # extract the instance/class flag
+
+ if name =~ /^(.*?)-(c|i).yaml$/
+ external_name = $1
+ is_class_method = $2 == "c"
+ internal_name = RDoc::RI::Writer.external_to_internal(external_name)
+ list = is_class_method ? @class_methods : @instance_methods
+ path = File.join(dir, name)
+ list << RDoc::RI::MethodEntry.new(path, internal_name, is_class_method, self)
+ else
+ full_name = File.join(dir, name)
+ if File.directory?(full_name)
+ inf_class = @inferior_classes.find {|c| c.name == name }
+ if inf_class
+ inf_class.add_path(full_name)
+ else
+ inf_class = RDoc::RI::ClassEntry.new(full_name, name, self)
+ @inferior_classes << inf_class
+ end
+ inf_class.load_from(full_name)
+ end
+ end
+ end
+ end
+
+ # Return a list of any classes or modules that we contain
+ # that match a given string
+
+ def contained_modules_matching(name)
+ @inferior_classes.find_all {|c| c.name[name]}
+ end
+
+ def classes_and_modules
+ @inferior_classes
+ end
+
+ # Return an exact match to a particular name
+ def contained_class_named(name)
+ @inferior_classes.find {|c| c.name == name}
+ end
+
+ # return the list of local methods matching name
+ # We're split into two because we need distinct behavior
+ # when called from the _toplevel_
+ def methods_matching(name, is_class_method)
+ local_methods_matching(name, is_class_method)
+ end
+
+ # Find methods matching 'name' in ourselves and in
+ # any classes we contain
+ def recursively_find_methods_matching(name, is_class_method)
+ res = local_methods_matching(name, is_class_method)
+ @inferior_classes.each do |c|
+ res.concat(c.recursively_find_methods_matching(name, is_class_method))
+ end
+ res
+ end
+
+
+ # Return our full name
+ def full_name
+ res = @in_class.full_name
+ res << "::" unless res.empty?
+ res << @name
+ end
+
+ # Return a list of all out method names
+ def all_method_names
+ res = @class_methods.map {|m| m.full_name }
+ @instance_methods.each {|m| res << m.full_name}
+ res
+ end
+
+ private
+
+ # Return a list of all our methods matching a given string.
+ # Is +is_class_methods+ if 'nil', we don't care if the method
+ # is a class method or not, otherwise we only return
+ # those methods that match
+ def local_methods_matching(name, is_class_method)
+
+ list = case is_class_method
+ when nil then @class_methods + @instance_methods
+ when true then @class_methods
+ when false then @instance_methods
+ else fail "Unknown is_class_method: #{is_class_method.inspect}"
+ end
+
+ list.find_all {|m| m.name; m.name[name]}
+ end
+end
+
+##
+# A TopLevelEntry is like a class entry, but when asked to search for methods
+# searches all classes, not just itself
+
+class RDoc::RI::TopLevelEntry < RDoc::RI::ClassEntry
+ def methods_matching(name, is_class_method)
+ res = recursively_find_methods_matching(name, is_class_method)
+ end
+
+ def full_name
+ ""
+ end
+
+ def module_named(name)
+
+ end
+
+end
+
+class RDoc::RI::MethodEntry
+ attr_reader :name
+ attr_reader :path_name
+
+ def initialize(path_name, name, is_class_method, in_class)
+ @path_name = path_name
+ @name = name
+ @is_class_method = is_class_method
+ @in_class = in_class
+ end
+
+ def full_name
+ res = @in_class.full_name
+ unless res.empty?
+ if @is_class_method
+ res << "::"
+ else
+ res << "#"
+ end
+ end
+ res << @name
+ end
+end
+
+##
+# We represent everything known about all 'ri' files accessible to this program
+
+class RDoc::RI::Cache
+
+ attr_reader :toplevel
+
+ def initialize(dirs)
+ # At the top level we have a dummy module holding the
+ # overall namespace
+ @toplevel = RDoc::RI::TopLevelEntry.new('', '::', nil)
+
+ dirs.each do |dir|
+ @toplevel.load_from(dir)
+ end
+ end
+
+end
diff --git a/ruby/lib/rdoc/ri/descriptions.rb b/ruby/lib/rdoc/ri/descriptions.rb
new file mode 100644
index 0000000..467b7de
--- /dev/null
+++ b/ruby/lib/rdoc/ri/descriptions.rb
@@ -0,0 +1,156 @@
+require 'yaml'
+require 'rdoc/markup/fragments'
+require 'rdoc/ri'
+
+##
+# Descriptions are created by RDoc (in ri_generator) and written out in
+# serialized form into the documentation tree. ri then reads these to generate
+# the documentation
+
+class RDoc::RI::NamedThing
+ attr_reader :name
+ def initialize(name)
+ @name = name
+ end
+
+ def <=>(other)
+ @name <=> other.name
+ end
+
+ def hash
+ @name.hash
+ end
+
+ def eql?(other)
+ @name.eql?(other)
+ end
+end
+
+class RDoc::RI::AliasName < RDoc::RI::NamedThing; end
+
+class RDoc::RI::Attribute < RDoc::RI::NamedThing
+ attr_reader :rw, :comment
+
+ def initialize(name, rw, comment)
+ super(name)
+ @rw = rw
+ @comment = comment
+ end
+end
+
+class RDoc::RI::Constant < RDoc::RI::NamedThing
+ attr_reader :value, :comment
+
+ def initialize(name, value, comment)
+ super(name)
+ @value = value
+ @comment = comment
+ end
+end
+
+class RDoc::RI::IncludedModule < RDoc::RI::NamedThing; end
+
+class RDoc::RI::MethodSummary < RDoc::RI::NamedThing
+ def initialize(name="")
+ super
+ end
+end
+
+class RDoc::RI::Description
+ attr_accessor :name
+ attr_accessor :full_name
+ attr_accessor :comment
+
+ def serialize
+ self.to_yaml
+ end
+
+ def self.deserialize(from)
+ YAML.load(from)
+ end
+
+ def <=>(other)
+ @name <=> other.name
+ end
+end
+
+class RDoc::RI::ModuleDescription < RDoc::RI::Description
+
+ attr_accessor :class_methods
+ attr_accessor :class_method_extensions
+ attr_accessor :instance_methods
+ attr_accessor :instance_method_extensions
+ attr_accessor :attributes
+ attr_accessor :constants
+ attr_accessor :includes
+
+ # merge in another class description into this one
+ def merge_in(old)
+ merge(@class_methods, old.class_methods)
+ merge(@instance_methods, old.instance_methods)
+ merge(@attributes, old.attributes)
+ merge(@constants, old.constants)
+ merge(@includes, old.includes)
+ if @comment.nil? || @comment.empty?
+ @comment = old.comment
+ else
+ unless old.comment.nil? or old.comment.empty? then
+ if @comment.nil? or @comment.empty? then
+ @comment = old.comment
+ else
+ @comment << RDoc::Markup::Flow::RULE.new
+ @comment.concat old.comment
+ end
+ end
+ end
+ end
+
+ def display_name
+ "Module"
+ end
+
+ # the 'ClassDescription' subclass overrides this
+ # to format up the name of a parent
+ def superclass_string
+ nil
+ end
+
+ private
+
+ def merge(into, from)
+ names = {}
+ into.each {|i| names[i.name] = i }
+ from.each {|i| names[i.name] = i }
+ into.replace(names.keys.sort.map {|n| names[n]})
+ end
+end
+
+class RDoc::RI::ClassDescription < RDoc::RI::ModuleDescription
+ attr_accessor :superclass
+
+ def display_name
+ "Class"
+ end
+
+ def superclass_string
+ if @superclass && @superclass != "Object"
+ @superclass
+ else
+ nil
+ end
+ end
+end
+
+class RDoc::RI::MethodDescription < RDoc::RI::Description
+
+ attr_accessor :is_class_method
+ attr_accessor :visibility
+ attr_accessor :block_params
+ attr_accessor :is_singleton
+ attr_accessor :aliases
+ attr_accessor :is_alias_for
+ attr_accessor :params
+ attr_accessor :source_path
+
+end
+
diff --git a/ruby/lib/rdoc/ri/display.rb b/ruby/lib/rdoc/ri/display.rb
new file mode 100644
index 0000000..7b0158c
--- /dev/null
+++ b/ruby/lib/rdoc/ri/display.rb
@@ -0,0 +1,392 @@
+require 'rdoc/ri'
+
+# readline support might not be present, so be careful
+# when requiring it.
+begin
+ require('readline')
+ require('abbrev')
+ CAN_USE_READLINE = true # HACK use an RDoc namespace constant
+rescue LoadError
+ CAN_USE_READLINE = false
+end
+
+##
+# This is a kind of 'flag' module. If you want to write your own 'ri' display
+# module (perhaps because you're writing an IDE), you write a class which
+# implements the various 'display' methods in RDoc::RI::DefaultDisplay, and
+# include the RDoc::RI::Display module in that class.
+#
+# To access your class from the command line, you can do
+#
+# ruby -r <your source file> ../ri ....
+
+module RDoc::RI::Display
+
+ @@display_class = nil
+
+ def self.append_features(display_class)
+ @@display_class = display_class
+ end
+
+ def self.new(*args)
+ @@display_class.new(*args)
+ end
+
+end
+
+##
+# A paging display module. Uses the RDoc::RI::Formatter class to do the actual
+# presentation.
+
+class RDoc::RI::DefaultDisplay
+
+ include RDoc::RI::Display
+
+ def initialize(formatter, width, use_stdout, output = $stdout)
+ @use_stdout = use_stdout
+ @formatter = formatter.new output, width, " "
+ end
+
+ ##
+ # Display information about +klass+. Fetches additional information from
+ # +ri_reader+ as necessary.
+
+ def display_class_info(klass)
+ page do
+ superclass = klass.superclass
+
+ if superclass
+ superclass = " < " + superclass
+ else
+ superclass = ""
+ end
+
+ @formatter.draw_line(klass.display_name + ": " +
+ klass.full_name + superclass)
+
+ display_flow(klass.comment)
+ @formatter.draw_line
+
+ unless klass.includes.empty?
+ @formatter.blankline
+ @formatter.display_heading("Includes:", 2, "")
+ incs = []
+
+ klass.includes.each do |inc|
+ incs << inc.name
+ end
+
+ @formatter.wrap(incs.sort.join(', '))
+ end
+
+ unless klass.constants.empty?
+ @formatter.blankline
+ @formatter.display_heading("Constants:", 2, "")
+
+ constants = klass.constants.sort_by { |constant| constant.name }
+
+ constants.each do |constant|
+ @formatter.wrap "#{constant.name} = #{constant.value}"
+ if constant.comment then
+ @formatter.indent do
+ @formatter.display_flow constant.comment
+ end
+ else
+ @formatter.break_to_newline
+ end
+ end
+ end
+
+ unless klass.attributes.empty? then
+ @formatter.blankline
+ @formatter.display_heading 'Attributes:', 2, ''
+
+ attributes = klass.attributes.sort_by { |attribute| attribute.name }
+
+ attributes.each do |attribute|
+ if attribute.comment then
+ @formatter.wrap "#{attribute.name} (#{attribute.rw}):"
+ @formatter.indent do
+ @formatter.display_flow attribute.comment
+ end
+ else
+ @formatter.wrap "#{attribute.name} (#{attribute.rw})"
+ @formatter.break_to_newline
+ end
+ end
+ end
+
+ return display_class_method_list(klass)
+ end
+ end
+
+ ##
+ # Given a Hash mapping a class' methods to method types (returned by
+ # display_class_method_list), this method allows the user to
+ # choose one of the methods.
+
+ def get_class_method_choice(method_map)
+ if CAN_USE_READLINE
+ # prepare abbreviations for tab completion
+ abbreviations = method_map.keys.abbrev
+ Readline.completion_proc = proc do |string|
+ abbreviations.values.uniq.grep(/^#{string}/)
+ end
+ end
+
+ @formatter.raw_print_line "\nEnter the method name you want.\n"
+ @formatter.raw_print_line "Class methods can be preceeded by '::' and instance methods by '#'.\n"
+
+ if CAN_USE_READLINE
+ @formatter.raw_print_line "You can use tab to autocomplete.\n"
+ @formatter.raw_print_line "Enter a blank line to exit.\n"
+
+ choice_string = Readline.readline(">> ").strip
+ else
+ @formatter.raw_print_line "Enter a blank line to exit.\n"
+ @formatter.raw_print_line ">> "
+ choice_string = $stdin.gets.strip
+ end
+
+ if choice_string == ''
+ return nil
+ else
+ class_or_instance = method_map[choice_string]
+
+ if class_or_instance
+ # If the user's choice is not preceeded by a '::' or a '#', figure
+ # out whether they want a class or an instance method and decorate
+ # the choice appropriately.
+ if(choice_string =~ /^[a-zA-Z]/)
+ if(class_or_instance == :class)
+ choice_string = "::#{choice_string}"
+ else
+ choice_string = "##{choice_string}"
+ end
+ end
+
+ return choice_string
+ else
+ @formatter.raw_print_line "No method matched '#{choice_string}'.\n"
+ return nil
+ end
+ end
+ end
+
+
+ ##
+ # Display methods on +klass+
+ # Returns a hash mapping method name to method contents (HACK?)
+
+ def display_class_method_list(klass)
+ method_map = {}
+
+ class_data = [
+ :class_methods,
+ :class_method_extensions,
+ :instance_methods,
+ :instance_method_extensions,
+ ]
+
+ class_data.each do |data_type|
+ data = klass.send data_type
+
+ unless data.nil? or data.empty? then
+ @formatter.blankline
+
+ heading = data_type.to_s.split('_').join(' ').capitalize << ':'
+ @formatter.display_heading heading, 2, ''
+
+ method_names = []
+ data.each do |item|
+ method_names << item.name
+
+ if(data_type == :class_methods ||
+ data_type == :class_method_extensions) then
+ method_map["::#{item.name}"] = :class
+ method_map[item.name] = :class
+ else
+ #
+ # Since we iterate over instance methods after class methods,
+ # an instance method always will overwrite the unqualified
+ # class method entry for a class method of the same name.
+ #
+ method_map["##{item.name}"] = :instance
+ method_map[item.name] = :instance
+ end
+ end
+ method_names.sort!
+
+ @formatter.wrap method_names.join(', ')
+ end
+ end
+
+ method_map
+ end
+ private :display_class_method_list
+
+ ##
+ # Display an Array of RDoc::Markup::Flow objects, +flow+.
+
+ def display_flow(flow)
+ if flow and not flow.empty? then
+ @formatter.display_flow flow
+ else
+ @formatter.wrap '[no description]'
+ end
+ end
+
+ ##
+ # Display information about +method+.
+
+ def display_method_info(method)
+ page do
+ @formatter.draw_line(method.full_name)
+ display_params(method)
+
+ @formatter.draw_line
+ display_flow(method.comment)
+
+ if method.aliases and not method.aliases.empty? then
+ @formatter.blankline
+ aka = "(also known as #{method.aliases.map { |a| a.name }.join(', ')})"
+ @formatter.wrap aka
+ end
+ end
+ end
+
+ ##
+ # Display the list of +methods+.
+
+ def display_method_list(methods)
+ page do
+ @formatter.wrap "More than one method matched your request. You can refine your search by asking for information on one of:"
+ @formatter.blankline
+
+ methods.each do |method|
+ @formatter.raw_print_line "#{method.full_name} [#{method.source_path}]\n"
+ end
+ end
+ end
+
+ ##
+ # Display a list of +methods+ and allow the user to select one of them.
+
+ def display_method_list_choice(methods)
+ page do
+ @formatter.wrap "More than one method matched your request. Please choose one of the possible matches."
+ @formatter.blankline
+
+ methods.each_with_index do |method, index|
+ @formatter.raw_print_line "%3d %s [%s]\n" % [index + 1, method.full_name, method.source_path]
+ end
+
+ @formatter.raw_print_line ">> "
+
+ choice = $stdin.gets.strip!
+
+ if(choice == '')
+ return
+ end
+
+ choice = choice.to_i
+
+ if ((choice == 0) || (choice > methods.size)) then
+ @formatter.raw_print_line "Invalid choice!\n"
+ else
+ method = methods[choice - 1]
+ display_method_info(method)
+ end
+ end
+ end
+
+ ##
+ # Display the params for +method+.
+
+ def display_params(method)
+ params = method.params
+
+ if params[0,1] == "(" then
+ if method.is_singleton
+ params = method.full_name + params
+ else
+ params = method.name + params
+ end
+ end
+
+ params.split(/\n/).each do |param|
+ @formatter.wrap param
+ @formatter.break_to_newline
+ end
+
+ @formatter.blankline
+ @formatter.wrap("From #{method.source_path}")
+ end
+
+ ##
+ # List the classes in +classes+.
+
+ def list_known_classes(classes)
+ if classes.empty?
+ warn_no_database
+ else
+ page do
+ @formatter.draw_line "Known classes and modules"
+ @formatter.blankline
+
+ @formatter.wrap classes.sort.join(', ')
+ end
+ end
+ end
+
+ ##
+ # Paginates output through a pager program.
+
+ def page
+ if pager = setup_pager then
+ begin
+ orig_output = @formatter.output
+ @formatter.output = pager
+ yield
+ ensure
+ @formatter.output = orig_output
+ pager.close
+ end
+ else
+ yield
+ end
+ rescue Errno::EPIPE
+ end
+
+ ##
+ # Sets up a pager program to pass output through.
+
+ def setup_pager
+ unless @use_stdout then
+ for pager in [ ENV['PAGER'], "less", "more", 'pager' ].compact.uniq
+ return IO.popen(pager, "w") rescue nil
+ end
+ @use_stdout = true
+ nil
+ end
+ end
+
+ ##
+ # Displays a message that describes how to build RI data.
+
+ def warn_no_database
+ output = @formatter.output
+
+ output.puts "No ri data found"
+ output.puts
+ output.puts "If you've installed Ruby yourself, you need to generate documentation using:"
+ output.puts
+ output.puts " make install-doc"
+ output.puts
+ output.puts "from the same place you ran `make` to build ruby."
+ output.puts
+ output.puts "If you installed Ruby from a packaging system, then you may need to"
+ output.puts "install an additional package, or ask the packager to enable ri generation."
+ end
+
+end
diff --git a/ruby/lib/rdoc/ri/driver.rb b/ruby/lib/rdoc/ri/driver.rb
new file mode 100644
index 0000000..0c91232
--- /dev/null
+++ b/ruby/lib/rdoc/ri/driver.rb
@@ -0,0 +1,669 @@
+require 'optparse'
+require 'yaml'
+
+require 'rdoc/ri'
+require 'rdoc/ri/paths'
+require 'rdoc/ri/formatter'
+require 'rdoc/ri/display'
+require 'fileutils'
+require 'rdoc/markup'
+require 'rdoc/markup/to_flow'
+
+class RDoc::RI::Driver
+
+ #
+ # This class offers both Hash and OpenStruct functionality.
+ # We convert from the Core Hash to this before calling any of
+ # the display methods, in order to give the display methods
+ # a cleaner API for accessing the data.
+ #
+ class OpenStructHash < Hash
+ #
+ # This method converts from a Hash to an OpenStructHash.
+ #
+ def self.convert(object)
+ case object
+ when Hash then
+ new_hash = new # Convert Hash -> OpenStructHash
+
+ object.each do |key, value|
+ new_hash[key] = convert(value)
+ end
+
+ new_hash
+ when Array then
+ object.map do |element|
+ convert(element)
+ end
+ else
+ object
+ end
+ end
+
+ def merge_enums(other)
+ other.each do |k, v|
+ if self[k] then
+ case v
+ when Array then
+ # HACK dunno
+ if String === self[k] and self[k].empty? then
+ self[k] = v
+ else
+ self[k] += v
+ end
+ when Hash then
+ self[k].update v
+ else
+ # do nothing
+ end
+ else
+ self[k] = v
+ end
+ end
+ end
+
+ def method_missing method, *args
+ self[method.to_s]
+ end
+ end
+
+ class Error < RDoc::RI::Error; end
+
+ class NotFoundError < Error
+ def message
+ "Nothing known about #{super}"
+ end
+ end
+
+ attr_accessor :homepath # :nodoc:
+
+ def self.default_options
+ options = {}
+ options[:use_stdout] = !$stdout.tty?
+ options[:width] = 72
+ options[:formatter] = RDoc::RI::Formatter.for 'plain'
+ options[:interactive] = false
+ options[:use_cache] = true
+
+ # By default all standard paths are used.
+ options[:use_system] = true
+ options[:use_site] = true
+ options[:use_home] = true
+ options[:use_gems] = true
+ options[:extra_doc_dirs] = []
+
+ return options
+ end
+
+ def self.process_args(argv)
+ options = default_options
+
+ opts = OptionParser.new do |opt|
+ opt.program_name = File.basename $0
+ opt.version = RDoc::VERSION
+ opt.release = nil
+ opt.summary_indent = ' ' * 4
+
+ directories = [
+ RDoc::RI::Paths::SYSDIR,
+ RDoc::RI::Paths::SITEDIR,
+ RDoc::RI::Paths::HOMEDIR
+ ]
+
+ if RDoc::RI::Paths::GEMDIRS then
+ Gem.path.each do |dir|
+ directories << "#{dir}/doc/*/ri"
+ end
+ end
+
+ opt.banner = <<-EOT
+Usage: #{opt.program_name} [options] [names...]
+
+Where name can be:
+
+ Class | Class::method | Class#method | Class.method | method
+
+All class names may be abbreviated to their minimum unambiguous form. If a name
+is ambiguous, all valid options will be listed.
+
+The form '.' method matches either class or instance methods, while #method
+matches only instance and ::method matches only class methods.
+
+For example:
+
+ #{opt.program_name} Fil
+ #{opt.program_name} File
+ #{opt.program_name} File.new
+ #{opt.program_name} zip
+
+Note that shell quoting may be required for method names containing
+punctuation:
+
+ #{opt.program_name} 'Array.[]'
+ #{opt.program_name} compact\\!
+
+By default ri searches for documentation in the following directories:
+
+ #{directories.join "\n "}
+
+Specifying the --system, --site, --home, --gems or --doc-dir options will
+limit ri to searching only the specified directories.
+
+Options may also be set in the 'RI' environment variable.
+ EOT
+
+ opt.separator nil
+ opt.separator "Options:"
+ opt.separator nil
+
+ opt.on("--fmt=FORMAT", "--format=FORMAT", "-f",
+ RDoc::RI::Formatter::FORMATTERS.keys,
+ "Format to use when displaying output:",
+ " #{RDoc::RI::Formatter.list}",
+ "Use 'bs' (backspace) with most pager",
+ "programs. To use ANSI, either disable the",
+ "pager or tell the pager to allow control",
+ "characters.") do |value|
+ options[:formatter] = RDoc::RI::Formatter.for value
+ end
+
+ opt.separator nil
+
+ opt.on("--doc-dir=DIRNAME", "-d", Array,
+ "List of directories from which to source",
+ "documentation in addition to the standard",
+ "directories. May be repeated.") do |value|
+ value.each do |dir|
+ unless File.directory? dir then
+ raise OptionParser::InvalidArgument, "#{dir} is not a directory"
+ end
+
+ options[:extra_doc_dirs] << File.expand_path(dir)
+ end
+ end
+
+ opt.separator nil
+
+ opt.on("--[no-]use-cache",
+ "Whether or not to use ri's cache.",
+ "True by default.") do |value|
+ options[:use_cache] = value
+ end
+
+ opt.separator nil
+
+ opt.on("--no-standard-docs",
+ "Do not include documentation from",
+ "the Ruby standard library, site_lib,",
+ "installed gems, or ~/.rdoc.",
+ "Equivalent to specifying",
+ "the options --no-system, --no-site, --no-gems,",
+ "and --no-home") do
+ options[:use_system] = false
+ options[:use_site] = false
+ options[:use_gems] = false
+ options[:use_home] = false
+ end
+
+ opt.separator nil
+
+ opt.on("--[no-]system",
+ "Include documentation from Ruby's standard",
+ "library. Defaults to true.") do |value|
+ options[:use_system] = value
+ end
+
+ opt.separator nil
+
+ opt.on("--[no-]site",
+ "Include documentation from libraries",
+ "installed in site_lib.",
+ "Defaults to true.") do |value|
+ options[:use_site] = value
+ end
+
+ opt.separator nil
+
+ opt.on("--[no-]gems",
+ "Include documentation from RubyGems.",
+ "Defaults to true.") do |value|
+ options[:use_gems] = value
+ end
+
+ opt.separator nil
+
+ opt.on("--[no-]home",
+ "Include documentation stored in ~/.rdoc.",
+ "Defaults to true.") do |value|
+ options[:use_home] = value
+ end
+
+ opt.separator nil
+
+ opt.on("--list-doc-dirs",
+ "List the directories from which ri will",
+ "source documentation on stdout and exit.") do
+ options[:list_doc_dirs] = true
+ end
+
+ opt.separator nil
+
+ opt.on("--no-pager", "-T",
+ "Send output directly to stdout,",
+ "rather than to a pager.") do
+ options[:use_stdout] = true
+ end
+
+ opt.on("--interactive", "-i",
+ "This makes ri go into interactive mode.",
+ "When ri is in interactive mode it will",
+ "allow the user to disambiguate lists of",
+ "methods in case multiple methods match",
+ "against a method search string. It also",
+ "will allow the user to enter in a method",
+ "name (with auto-completion, if readline",
+ "is supported) when viewing a class.") do
+ options[:interactive] = true
+ end
+
+ opt.separator nil
+
+ opt.on("--width=WIDTH", "-w", OptionParser::DecimalInteger,
+ "Set the width of the output.") do |value|
+ options[:width] = value
+ end
+ end
+
+ argv = ENV['RI'].to_s.split.concat argv
+
+ opts.parse! argv
+
+ options[:names] = argv
+
+ options[:formatter] ||= RDoc::RI::Formatter.for('plain')
+ options[:use_stdout] ||= !$stdout.tty?
+ options[:use_stdout] ||= options[:interactive]
+ options[:width] ||= 72
+
+ options
+
+ rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => e
+ puts opts
+ puts
+ puts e
+ exit 1
+ end
+
+ def self.run(argv = ARGV)
+ options = process_args argv
+ ri = new options
+ ri.run
+ end
+
+ def initialize(initial_options={})
+ options = self.class.default_options.update(initial_options)
+
+ @names = options[:names]
+ @class_cache_name = 'classes'
+
+ @doc_dirs = RDoc::RI::Paths.path(options[:use_system],
+ options[:use_site],
+ options[:use_home],
+ options[:use_gems],
+ options[:extra_doc_dirs])
+
+ @homepath = RDoc::RI::Paths.raw_path(false, false, true, false).first
+ @homepath = @homepath.sub(/\.rdoc/, '.ri')
+ @sys_dir = RDoc::RI::Paths.raw_path(true, false, false, false).first
+ @list_doc_dirs = options[:list_doc_dirs]
+
+ FileUtils.mkdir_p cache_file_path unless File.directory? cache_file_path
+ @cache_doc_dirs_path = File.join cache_file_path, ".doc_dirs"
+
+ @use_cache = options[:use_cache]
+ @class_cache = nil
+
+ @interactive = options[:interactive]
+ @display = RDoc::RI::DefaultDisplay.new(options[:formatter],
+ options[:width],
+ options[:use_stdout])
+ end
+
+ def class_cache
+ return @class_cache if @class_cache
+
+ # Get the documentation directories used to make the cache in order to see
+ # whether the cache is valid for the current ri instantiation.
+ if(File.readable?(@cache_doc_dirs_path))
+ cache_doc_dirs = IO.read(@cache_doc_dirs_path).split("\n")
+ else
+ cache_doc_dirs = []
+ end
+
+ newest = map_dirs('created.rid') do |f|
+ File.mtime f if test ?f, f
+ end.max
+
+ # An up to date cache file must have been created more recently than
+ # the last modification of any of the documentation directories. It also
+ # must have been created with the same documentation directories
+ # as those from which ri currently is sourcing documentation.
+ up_to_date = (File.exist?(class_cache_file_path) and
+ newest and newest < File.mtime(class_cache_file_path) and
+ (cache_doc_dirs == @doc_dirs))
+
+ if up_to_date and @use_cache then
+ open class_cache_file_path, 'rb' do |fp|
+ begin
+ @class_cache = Marshal.load fp.read
+ rescue
+ #
+ # This shouldn't be necessary, since the up_to_date logic above
+ # should force the cache to be recreated when a new version of
+ # rdoc is installed. This seems like a worthwhile enhancement
+ # to ri's robustness, however.
+ #
+ $stderr.puts "Error reading the class cache; recreating the class cache!"
+ @class_cache = create_class_cache
+ end
+ end
+ else
+ @class_cache = create_class_cache
+ end
+
+ @class_cache
+ end
+
+ def create_class_cache
+ class_cache = OpenStructHash.new
+
+ if(@use_cache)
+ # Dump the documentation directories to a file in the cache, so that
+ # we only will use the cache for future instantiations with identical
+ # documentation directories.
+ File.open @cache_doc_dirs_path, "wb" do |fp|
+ fp << @doc_dirs.join("\n")
+ end
+ end
+
+ classes = map_dirs('**/cdesc*.yaml') { |f| Dir[f] }
+ warn "Updating class cache with #{classes.size} classes..."
+ populate_class_cache class_cache, classes
+
+ write_cache class_cache, class_cache_file_path
+
+ class_cache
+ end
+
+ def populate_class_cache(class_cache, classes, extension = false)
+ classes.each do |cdesc|
+ desc = read_yaml cdesc
+ klassname = desc["full_name"]
+
+ unless class_cache.has_key? klassname then
+ desc["display_name"] = "Class"
+ desc["sources"] = [cdesc]
+ desc["instance_method_extensions"] = []
+ desc["class_method_extensions"] = []
+ class_cache[klassname] = desc
+ else
+ klass = class_cache[klassname]
+
+ if extension then
+ desc["instance_method_extensions"] = desc.delete "instance_methods"
+ desc["class_method_extensions"] = desc.delete "class_methods"
+ end
+
+ klass.merge_enums desc
+ klass["sources"] << cdesc
+ end
+ end
+ end
+
+ def class_cache_file_path
+ File.join cache_file_path, @class_cache_name
+ end
+
+ def cache_file_for(klassname)
+ File.join cache_file_path, klassname.gsub(/:+/, "-")
+ end
+
+ def cache_file_path
+ File.join @homepath, 'cache'
+ end
+
+ def display_class(name)
+ klass = class_cache[name]
+ @display.display_class_info klass
+ end
+
+ def display_method(method)
+ @display.display_method_info method
+ end
+
+ def get_info_for(arg)
+ @names = [arg]
+ run
+ end
+
+ def load_cache_for(klassname)
+ path = cache_file_for klassname
+
+ cache = nil
+
+ if File.exist? path and
+ File.mtime(path) >= File.mtime(class_cache_file_path) and
+ @use_cache then
+ open path, 'rb' do |fp|
+ begin
+ cache = Marshal.load fp.read
+ rescue
+ #
+ # The cache somehow is bad. Recreate the cache.
+ #
+ $stderr.puts "Error reading the cache for #{klassname}; recreating the cache!"
+ cache = create_cache_for klassname, path
+ end
+ end
+ else
+ cache = create_cache_for klassname, path
+ end
+
+ cache
+ end
+
+ def create_cache_for(klassname, path)
+ klass = class_cache[klassname]
+ return nil unless klass
+
+ method_files = klass["sources"]
+ cache = OpenStructHash.new
+
+ method_files.each do |f|
+ system_file = f.index(@sys_dir) == 0
+ Dir[File.join(File.dirname(f), "*")].each do |yaml|
+ next unless yaml =~ /yaml$/
+ next if yaml =~ /cdesc-[^\/]+yaml$/
+
+ method = read_yaml yaml
+
+ if system_file then
+ method["source_path"] = "Ruby #{RDoc::RI::Paths::VERSION}"
+ else
+ if(f =~ %r%gems/[\d.]+/doc/([^/]+)%) then
+ ext_path = "gem #{$1}"
+ else
+ ext_path = f
+ end
+
+ method["source_path"] = ext_path
+ end
+
+ name = method["full_name"]
+ cache[name] = method
+ end
+ end
+
+ write_cache cache, path
+ end
+
+ ##
+ # Finds the next ancestor of +orig_klass+ after +klass+.
+
+ def lookup_ancestor(klass, orig_klass)
+ # This is a bit hacky, but ri will go into an infinite
+ # loop otherwise, since Object has an Object ancestor
+ # for some reason. Depending on the documentation state, I've seen
+ # Kernel as an ancestor of Object and not as an ancestor of Object.
+ if ((orig_klass == "Object") &&
+ ((klass == "Kernel") || (klass == "Object")))
+ return nil
+ end
+
+ cache = class_cache[orig_klass]
+
+ return nil unless cache
+
+ ancestors = [orig_klass]
+ ancestors.push(*cache.includes.map { |inc| inc['name'] })
+ ancestors << cache.superclass
+
+ ancestor_index = ancestors.index(klass)
+
+ if ancestor_index
+ ancestor = ancestors[ancestors.index(klass) + 1]
+ return ancestor if ancestor
+ end
+
+ lookup_ancestor klass, cache.superclass
+ end
+
+ ##
+ # Finds the method
+
+ def lookup_method(name, klass)
+ cache = load_cache_for klass
+ return nil unless cache
+
+ method = cache[name.gsub('.', '#')]
+ method = cache[name.gsub('.', '::')] unless method
+ method
+ end
+
+ def map_dirs(file_name)
+ @doc_dirs.map { |dir| yield File.join(dir, file_name) }.flatten.compact
+ end
+
+ ##
+ # Extract the class and method name parts from +name+ like Foo::Bar#baz
+
+ def parse_name(name)
+ parts = name.split(/(::|\#|\.)/)
+
+ if parts[-2] != '::' or parts.last !~ /^[A-Z]/ then
+ meth = parts.pop
+ parts.pop
+ end
+
+ klass = parts.join
+
+ [klass, meth]
+ end
+
+ def read_yaml(path)
+ data = File.read path
+
+ # Necessary to be backward-compatible with documentation generated
+ # by earliar RDoc versions.
+ data = data.gsub(/ \!ruby\/(object|struct):(RDoc::RI|RI).*/, '')
+ data = data.gsub(/ \!ruby\/(object|struct):SM::(\S+)/,
+ ' !ruby/\1:RDoc::Markup::\2')
+ OpenStructHash.convert(YAML.load(data))
+ end
+
+ def run
+ if(@list_doc_dirs)
+ puts @doc_dirs.join("\n")
+ elsif @names.empty? then
+ @display.list_known_classes class_cache.keys.sort
+ else
+ @names.each do |name|
+ if class_cache.key? name then
+ method_map = display_class name
+ if(@interactive)
+ method_name = @display.get_class_method_choice(method_map)
+
+ if(method_name != nil)
+ method = lookup_method "#{name}#{method_name}", name
+ display_method method
+ end
+ end
+ elsif name =~ /::|\#|\./ then
+ klass, = parse_name name
+
+ orig_klass = klass
+ orig_name = name
+
+ loop do
+ method = lookup_method name, klass
+
+ break method if method
+
+ ancestor = lookup_ancestor klass, orig_klass
+
+ break unless ancestor
+
+ name = name.sub klass, ancestor
+ klass = ancestor
+ end
+
+ raise NotFoundError, orig_name unless method
+
+ display_method method
+ else
+ methods = select_methods(/#{name}/)
+
+ if methods.size == 0
+ raise NotFoundError, name
+ elsif methods.size == 1
+ display_method methods[0]
+ else
+ if(@interactive)
+ @display.display_method_list_choice methods
+ else
+ @display.display_method_list methods
+ end
+ end
+ end
+ end
+ end
+ rescue NotFoundError => e
+ abort e.message
+ end
+
+ def select_methods(pattern)
+ methods = []
+ class_cache.keys.sort.each do |klass|
+ class_cache[klass]["instance_methods"].map{|h|h["name"]}.grep(pattern) do |name|
+ method = load_cache_for(klass)[klass+'#'+name]
+ methods << method if method
+ end
+ class_cache[klass]["class_methods"].map{|h|h["name"]}.grep(pattern) do |name|
+ method = load_cache_for(klass)[klass+'::'+name]
+ methods << method if method
+ end
+ end
+ methods
+ end
+
+ def write_cache(cache, path)
+ if(@use_cache)
+ File.open path, "wb" do |cache_file|
+ Marshal.dump cache, cache_file
+ end
+ end
+
+ cache
+ end
+
+end
diff --git a/ruby/lib/rdoc/ri/formatter.rb b/ruby/lib/rdoc/ri/formatter.rb
new file mode 100644
index 0000000..933882a
--- /dev/null
+++ b/ruby/lib/rdoc/ri/formatter.rb
@@ -0,0 +1,616 @@
+require 'rdoc/ri'
+require 'rdoc/markup'
+
+class RDoc::RI::Formatter
+
+ attr_writer :indent
+ attr_accessor :output
+
+ FORMATTERS = { }
+
+ def self.for(name)
+ FORMATTERS[name.downcase]
+ end
+
+ def self.list
+ FORMATTERS.keys.sort.join ", "
+ end
+
+ def initialize(output, width, indent)
+ @output = output
+ @width = width
+ @indent = indent
+ @original_indent = indent.dup
+ end
+
+ def draw_line(label=nil)
+ len = @width
+ len -= (label.size + 1) if label
+
+ if len > 0 then
+ @output.print '-' * len
+ if label
+ @output.print ' '
+ bold_print label
+ end
+
+ @output.puts
+ else
+ @output.print '-' * @width
+ @output.puts
+
+ @output.puts label
+ end
+ end
+
+ def indent
+ return @indent unless block_given?
+
+ begin
+ indent = @indent.dup
+ @indent += @original_indent
+ yield
+ ensure
+ @indent = indent
+ end
+ end
+
+ def wrap(txt, prefix=@indent, linelen=@width)
+ return unless txt && !txt.empty?
+
+ work = conv_markup(txt)
+ textLen = linelen - prefix.length
+ patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
+ next_prefix = prefix.tr("^ ", " ")
+
+ res = []
+
+ while work.length > textLen
+ if work =~ patt
+ res << $1
+ work.slice!(0, $&.length)
+ else
+ res << work.slice!(0, textLen)
+ end
+ end
+ res << work if work.length.nonzero?
+ @output.puts(prefix + res.join("\n" + next_prefix))
+ end
+
+ def blankline
+ @output.puts
+ end
+
+ ##
+ # Called when we want to ensure a new 'wrap' starts on a newline. Only
+ # needed for HtmlFormatter, because the rest do their own line breaking.
+
+ def break_to_newline
+ end
+
+ def bold_print(txt)
+ @output.print txt
+ end
+
+ def raw_print_line(txt)
+ @output.print txt
+ end
+
+ ##
+ # Convert HTML entities back to ASCII
+
+ def conv_html(txt)
+ txt = txt.gsub(/&gt;/, '>')
+ txt.gsub!(/&lt;/, '<')
+ txt.gsub!(/&quot;/, '"')
+ txt.gsub!(/&amp;/, '&')
+ txt
+ end
+
+ ##
+ # Convert markup into display form
+
+ def conv_markup(txt)
+ txt = txt.gsub(%r{<tt>(.*?)</tt>}, '+\1+')
+ txt.gsub!(%r{<code>(.*?)</code>}, '+\1+')
+ txt.gsub!(%r{<b>(.*?)</b>}, '*\1*')
+ txt.gsub!(%r{<em>(.*?)</em>}, '_\1_')
+ txt
+ end
+
+ def display_list(list)
+ case list.type
+ when :BULLET
+ prefixer = proc { |ignored| @indent + "* " }
+
+ when :NUMBER, :UPPERALPHA, :LOWERALPHA then
+ start = case list.type
+ when :NUMBER then 1
+ when :UPPERALPHA then 'A'
+ when :LOWERALPHA then 'a'
+ end
+
+ prefixer = proc do |ignored|
+ res = @indent + "#{start}.".ljust(4)
+ start = start.succ
+ res
+ end
+
+ when :LABELED, :NOTE then
+ longest = 0
+
+ list.contents.each do |item|
+ if RDoc::Markup::Flow::LI === item and item.label.length > longest then
+ longest = item.label.length
+ end
+ end
+
+ longest += 1
+
+ prefixer = proc { |li| @indent + li.label.ljust(longest) }
+
+ else
+ raise ArgumentError, "unknown list type #{list.type}"
+ end
+
+ list.contents.each do |item|
+ if RDoc::Markup::Flow::LI === item then
+ prefix = prefixer.call item
+ display_flow_item item, prefix
+ else
+ display_flow_item item
+ end
+ end
+ end
+
+ def display_flow_item(item, prefix = @indent)
+ case item
+ when RDoc::Markup::Flow::P, RDoc::Markup::Flow::LI
+ wrap(conv_html(item.body), prefix)
+ blankline
+
+ when RDoc::Markup::Flow::LIST
+ display_list(item)
+
+ when RDoc::Markup::Flow::VERB
+ display_verbatim_flow_item(item, @indent)
+
+ when RDoc::Markup::Flow::H
+ display_heading(conv_html(item.text), item.level, @indent)
+
+ when RDoc::Markup::Flow::RULE
+ draw_line
+
+ else
+ raise RDoc::Error, "Unknown flow element: #{item.class}"
+ end
+ end
+
+ def display_verbatim_flow_item(item, prefix=@indent)
+ item.body.split(/\n/).each do |line|
+ @output.print @indent, conv_html(line), "\n"
+ end
+ blankline
+ end
+
+ def display_heading(text, level, indent)
+ text = strip_attributes text
+
+ case level
+ when 1 then
+ ul = "=" * text.length
+ @output.puts
+ @output.puts text.upcase
+ @output.puts ul
+
+ when 2 then
+ ul = "-" * text.length
+ @output.puts
+ @output.puts text
+ @output.puts ul
+ else
+ @output.print indent, text, "\n"
+ end
+
+ @output.puts
+ end
+
+ def display_flow(flow)
+ flow.each do |f|
+ display_flow_item(f)
+ end
+ end
+
+ def strip_attributes(text)
+ text.gsub(/(<\/?(?:b|code|em|i|tt)>)/, '')
+ end
+
+end
+
+##
+# Handle text with attributes. We're a base class: there are different
+# presentation classes (one, for example, uses overstrikes to handle bold and
+# underlining, while another using ANSI escape sequences.
+
+class RDoc::RI::AttributeFormatter < RDoc::RI::Formatter
+
+ BOLD = 1
+ ITALIC = 2
+ CODE = 4
+
+ ATTR_MAP = {
+ "b" => BOLD,
+ "code" => CODE,
+ "em" => ITALIC,
+ "i" => ITALIC,
+ "tt" => CODE
+ }
+
+ AttrChar = Struct.new :char, :attr
+
+ class AttributeString
+ attr_reader :txt
+
+ def initialize
+ @txt = []
+ @optr = 0
+ end
+
+ def <<(char)
+ @txt << char
+ end
+
+ def empty?
+ @optr >= @txt.length
+ end
+
+ # accept non space, then all following spaces
+ def next_word
+ start = @optr
+ len = @txt.length
+
+ while @optr < len && @txt[@optr].char != " "
+ @optr += 1
+ end
+
+ while @optr < len && @txt[@optr].char == " "
+ @optr += 1
+ end
+
+ @txt[start...@optr]
+ end
+ end
+
+ ##
+ # Overrides base class. Looks for <tt>...</tt> etc sequences and generates
+ # an array of AttrChars. This array is then used as the basis for the
+ # split.
+
+ def wrap(txt, prefix=@indent, linelen=@width)
+ return unless txt && !txt.empty?
+
+ txt = add_attributes_to(txt)
+ next_prefix = prefix.tr("^ ", " ")
+ linelen -= prefix.size
+
+ line = []
+
+ until txt.empty?
+ word = txt.next_word
+ if word.size + line.size > linelen
+ write_attribute_text(prefix, line)
+ prefix = next_prefix
+ line = []
+ end
+ line.concat(word)
+ end
+
+ write_attribute_text(prefix, line) if line.length > 0
+ end
+
+ protected
+
+ def write_attribute_text(prefix, line)
+ @output.print prefix
+ line.each do |achar|
+ @output.print achar.char
+ end
+ @output.puts
+ end
+
+ def bold_print(txt)
+ @output.print txt
+ end
+
+ private
+
+ def add_attributes_to(txt)
+ tokens = txt.split(%r{(</?(?:b|code|em|i|tt)>)})
+ text = AttributeString.new
+ attributes = 0
+ tokens.each do |tok|
+ case tok
+ when %r{^</(\w+)>$} then attributes &= ~(ATTR_MAP[$1]||0)
+ when %r{^<(\w+)>$} then attributes |= (ATTR_MAP[$1]||0)
+ else
+ tok.split(//).each {|ch| text << AttrChar.new(ch, attributes)}
+ end
+ end
+ text
+ end
+
+end
+
+##
+# This formatter generates overstrike-style formatting, which works with
+# pagers such as man and less.
+
+class RDoc::RI::OverstrikeFormatter < RDoc::RI::AttributeFormatter
+
+ BS = "\C-h"
+
+ def write_attribute_text(prefix, line)
+ @output.print prefix
+
+ line.each do |achar|
+ attr = achar.attr
+ @output.print "_", BS if (attr & (ITALIC + CODE)) != 0
+ @output.print achar.char, BS if (attr & BOLD) != 0
+ @output.print achar.char
+ end
+
+ @output.puts
+ end
+
+ ##
+ # Draw a string in bold
+
+ def bold_print(text)
+ text.split(//).each do |ch|
+ @output.print ch, BS, ch
+ end
+ end
+
+end
+
+##
+# This formatter uses ANSI escape sequences to colorize stuff works with
+# pagers such as man and less.
+
+class RDoc::RI::AnsiFormatter < RDoc::RI::AttributeFormatter
+
+ def initialize(*args)
+ super
+ @output.print "\033[0m"
+ end
+
+ def write_attribute_text(prefix, line)
+ @output.print prefix
+ curr_attr = 0
+ line.each do |achar|
+ attr = achar.attr
+ if achar.attr != curr_attr
+ update_attributes(achar.attr)
+ curr_attr = achar.attr
+ end
+ @output.print achar.char
+ end
+ update_attributes(0) unless curr_attr.zero?
+ @output.puts
+ end
+
+ def bold_print(txt)
+ @output.print "\033[1m#{txt}\033[m"
+ end
+
+ HEADINGS = {
+ 1 => ["\033[1;32m", "\033[m"],
+ 2 => ["\033[4;32m", "\033[m"],
+ 3 => ["\033[32m", "\033[m"],
+ }
+
+ def display_heading(text, level, indent)
+ level = 3 if level > 3
+ heading = HEADINGS[level]
+ @output.print indent
+ @output.print heading[0]
+ @output.print strip_attributes(text)
+ @output.puts heading[1]
+ end
+
+ private
+
+ ATTR_MAP = {
+ BOLD => "1",
+ ITALIC => "33",
+ CODE => "36"
+ }
+
+ def update_attributes(attr)
+ str = "\033["
+ for quality in [ BOLD, ITALIC, CODE]
+ unless (attr & quality).zero?
+ str << ATTR_MAP[quality]
+ end
+ end
+ @output.print str, "m"
+ end
+
+end
+
+##
+# This formatter uses HTML.
+
+class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter
+
+ def write_attribute_text(prefix, line)
+ curr_attr = 0
+ line.each do |achar|
+ attr = achar.attr
+ if achar.attr != curr_attr
+ update_attributes(curr_attr, achar.attr)
+ curr_attr = achar.attr
+ end
+ @output.print(escape(achar.char))
+ end
+ update_attributes(curr_attr, 0) unless curr_attr.zero?
+ end
+
+ def draw_line(label=nil)
+ if label != nil
+ bold_print(label)
+ end
+ @output.puts("<hr>")
+ end
+
+ def bold_print(txt)
+ tag("b") { txt }
+ end
+
+ def blankline()
+ @output.puts("<p>")
+ end
+
+ def break_to_newline
+ @output.puts("<br>")
+ end
+
+ def display_heading(text, level, indent)
+ level = 4 if level > 4
+ tag("h#{level}") { text }
+ @output.puts
+ end
+
+ def display_list(list)
+ case list.type
+ when :BULLET then
+ list_type = "ul"
+ prefixer = proc { |ignored| "<li>" }
+
+ when :NUMBER, :UPPERALPHA, :LOWERALPHA then
+ list_type = "ol"
+ prefixer = proc { |ignored| "<li>" }
+
+ when :LABELED then
+ list_type = "dl"
+ prefixer = proc do |li|
+ "<dt><b>" + escape(li.label) + "</b><dd>"
+ end
+
+ when :NOTE then
+ list_type = "table"
+ prefixer = proc do |li|
+ %{<tr valign="top"><td>#{li.label.gsub(/ /, '&nbsp;')}</td><td>}
+ end
+ else
+ fail "unknown list type"
+ end
+
+ @output.print "<#{list_type}>"
+ list.contents.each do |item|
+ if item.kind_of? RDoc::Markup::Flow::LI
+ prefix = prefixer.call(item)
+ @output.print prefix
+ display_flow_item(item, prefix)
+ else
+ display_flow_item(item)
+ end
+ end
+ @output.print "</#{list_type}>"
+ end
+
+ def display_verbatim_flow_item(item, prefix=@indent)
+ @output.print("<pre>")
+ item.body.split(/\n/).each do |line|
+ @output.puts conv_html(line)
+ end
+ @output.puts("</pre>")
+ end
+
+ private
+
+ ATTR_MAP = {
+ BOLD => "b>",
+ ITALIC => "i>",
+ CODE => "tt>"
+ }
+
+ def update_attributes(current, wanted)
+ str = ""
+ # first turn off unwanted ones
+ off = current & ~wanted
+ for quality in [ BOLD, ITALIC, CODE]
+ if (off & quality) > 0
+ str << "</" + ATTR_MAP[quality]
+ end
+ end
+
+ # now turn on wanted
+ for quality in [ BOLD, ITALIC, CODE]
+ unless (wanted & quality).zero?
+ str << "<" << ATTR_MAP[quality]
+ end
+ end
+ @output.print str
+ end
+
+ def tag(code)
+ @output.print("<#{code}>")
+ @output.print(yield)
+ @output.print("</#{code}>")
+ end
+
+ def escape(str)
+ str = str.gsub(/&/n, '&amp;')
+ str.gsub!(/\"/n, '&quot;')
+ str.gsub!(/>/n, '&gt;')
+ str.gsub!(/</n, '&lt;')
+ str
+ end
+
+end
+
+##
+# This formatter reduces extra lines for a simpler output. It improves way
+# output looks for tools like IRC bots.
+
+class RDoc::RI::SimpleFormatter < RDoc::RI::Formatter
+
+ ##
+ # No extra blank lines
+
+ def blankline
+ end
+
+ ##
+ # Display labels only, no lines
+
+ def draw_line(label=nil)
+ unless label.nil? then
+ bold_print(label)
+ @output.puts
+ end
+ end
+
+ ##
+ # Place heading level indicators inline with heading.
+
+ def display_heading(text, level, indent)
+ text = strip_attributes(text)
+ case level
+ when 1
+ @output.puts "= " + text.upcase
+ when 2
+ @output.puts "-- " + text
+ else
+ @output.print indent, text, "\n"
+ end
+ end
+
+end
+
+RDoc::RI::Formatter::FORMATTERS['plain'] = RDoc::RI::Formatter
+RDoc::RI::Formatter::FORMATTERS['simple'] = RDoc::RI::SimpleFormatter
+RDoc::RI::Formatter::FORMATTERS['bs'] = RDoc::RI::OverstrikeFormatter
+RDoc::RI::Formatter::FORMATTERS['ansi'] = RDoc::RI::AnsiFormatter
+RDoc::RI::Formatter::FORMATTERS['html'] = RDoc::RI::HtmlFormatter
diff --git a/ruby/lib/rdoc/ri/paths.rb b/ruby/lib/rdoc/ri/paths.rb
new file mode 100644
index 0000000..2bcf861
--- /dev/null
+++ b/ruby/lib/rdoc/ri/paths.rb
@@ -0,0 +1,99 @@
+require 'rdoc/ri'
+
+##
+# Encapsulate all the strangeness to do with finding out where to find RDoc
+# files
+#
+# We basically deal with three directories:
+#
+# 1. The 'system' documentation directory, which holds the documentation
+# distributed with Ruby, and which is managed by the Ruby install process
+# 2. The 'site' directory, which contains site-wide documentation added
+# locally.
+# 3. The 'user' documentation directory, stored under the user's own home
+# directory.
+#
+# There's contention about all this, but for now:
+#
+# system:: $datadir/ri/<ver>/system/...
+# site:: $datadir/ri/<ver>/site/...
+# user:: ~/.rdoc
+
+module RDoc::RI::Paths
+
+ #:stopdoc:
+ require 'rbconfig'
+
+ DOC_DIR = "doc/rdoc"
+
+ VERSION = RbConfig::CONFIG['ruby_version']
+
+ if m = /ruby/.match(RbConfig::CONFIG['RUBY_INSTALL_NAME'])
+ m = [m.pre_match, m.post_match]
+ else
+ m = [""] * 2
+ end
+ ri = "#{m[0]}ri#{m[1]}"
+ rdoc = "#{m[0]}rdoc#{m[1]}"
+ base = File.join(RbConfig::CONFIG['datadir'], ri, VERSION)
+ SYSDIR = File.join(base, "system")
+ SITEDIR = File.join(base, "site")
+ homedir = ENV['HOME'] || ENV['USERPROFILE'] || ENV['HOMEPATH']
+
+ if homedir then
+ HOMEDIR = File.join(homedir, ".#{rdoc}")
+ else
+ HOMEDIR = nil
+ end
+
+ begin
+ require 'rubygems' unless defined?(Gem)
+
+ # HACK dup'd from Gem.latest_partials and friends
+ all_paths = []
+
+ all_paths = Gem.path.map do |dir|
+ Dir[File.join(dir, 'doc', '*', 'ri')]
+ end.flatten
+
+ ri_paths = {}
+
+ all_paths.each do |dir|
+ base = File.basename File.dirname(dir)
+ if base =~ /(.*)-((\d+\.)*\d+)/ then
+ name, version = $1, $2
+ ver = Gem::Version.new version
+ if ri_paths[name].nil? or ver > ri_paths[name][0] then
+ ri_paths[name] = [ver, dir]
+ end
+ end
+ end
+
+ GEMDIRS = ri_paths.map { |k,v| v.last }.sort
+ rescue LoadError
+ GEMDIRS = []
+ end
+
+ # Returns the selected documentation directories as an Array, or PATH if no
+ # overriding directories were given.
+
+ def self.path(use_system, use_site, use_home, use_gems, *extra_dirs)
+ path = raw_path(use_system, use_site, use_home, use_gems, *extra_dirs)
+ return path.select { |directory| File.directory? directory }
+ end
+
+ # Returns the selected documentation directories including nonexistent
+ # directories. Used to print out what paths were searched if no ri was
+ # found.
+
+ def self.raw_path(use_system, use_site, use_home, use_gems, *extra_dirs)
+ path = []
+ path << extra_dirs unless extra_dirs.empty?
+ path << SYSDIR if use_system
+ path << SITEDIR if use_site
+ path << HOMEDIR if use_home
+ path << GEMDIRS if use_gems
+
+ return path.flatten.compact
+ end
+end
diff --git a/ruby/lib/rdoc/ri/reader.rb b/ruby/lib/rdoc/ri/reader.rb
new file mode 100644
index 0000000..de3c8d9
--- /dev/null
+++ b/ruby/lib/rdoc/ri/reader.rb
@@ -0,0 +1,106 @@
+require 'rdoc/ri'
+require 'rdoc/ri/descriptions'
+require 'rdoc/ri/writer'
+require 'rdoc/markup/to_flow'
+
+class RDoc::RI::Reader
+
+ def initialize(ri_cache)
+ @cache = ri_cache
+ end
+
+ def top_level_namespace
+ [ @cache.toplevel ]
+ end
+
+ def lookup_namespace_in(target, namespaces)
+ result = []
+ for n in namespaces
+ result.concat(n.contained_modules_matching(target))
+ end
+ result
+ end
+
+ def find_class_by_name(full_name)
+ names = full_name.split(/::/)
+ ns = @cache.toplevel
+ for name in names
+ ns = ns.contained_class_named(name)
+ return nil if ns.nil?
+ end
+ get_class(ns)
+ end
+
+ def find_methods(name, is_class_method, namespaces)
+ result = []
+ namespaces.each do |ns|
+ result.concat ns.methods_matching(name, is_class_method)
+ end
+ result
+ end
+
+ ##
+ # Return the MethodDescription for a given MethodEntry by deserializing the
+ # YAML
+
+ def get_method(method_entry)
+ path = method_entry.path_name
+ File.open(path) { |f| RDoc::RI::Description.deserialize(f) }
+ end
+
+ ##
+ # Return a class description
+
+ def get_class(class_entry)
+ result = nil
+ for path in class_entry.path_names
+ path = RDoc::RI::Writer.class_desc_path(path, class_entry)
+ desc = File.open(path) {|f| RDoc::RI::Description.deserialize(f) }
+ if result
+ result.merge_in(desc)
+ else
+ result = desc
+ end
+ end
+ result
+ end
+
+ ##
+ # Return the names of all classes and modules
+
+ def full_class_names
+ res = []
+ find_classes_in(res, @cache.toplevel)
+ end
+
+ ##
+ # Return a list of all classes, modules, and methods
+
+ def all_names
+ res = []
+ find_names_in(res, @cache.toplevel)
+ end
+
+ private
+
+ def find_classes_in(res, klass)
+ classes = klass.classes_and_modules
+ for c in classes
+ res << c.full_name
+ find_classes_in(res, c)
+ end
+ res
+ end
+
+ def find_names_in(res, klass)
+ classes = klass.classes_and_modules
+ for c in classes
+ res << c.full_name
+ res.concat c.all_method_names
+ find_names_in(res, c)
+ end
+ res
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/ri/util.rb b/ruby/lib/rdoc/ri/util.rb
new file mode 100644
index 0000000..4e91eb9
--- /dev/null
+++ b/ruby/lib/rdoc/ri/util.rb
@@ -0,0 +1,79 @@
+require 'rdoc/ri'
+
+##
+# Break argument into its constituent class or module names, an
+# optional method type, and a method name
+
+class RDoc::RI::NameDescriptor
+
+ attr_reader :class_names
+ attr_reader :method_name
+
+ ##
+ # true and false have the obvious meaning. nil means we don't care
+
+ attr_reader :is_class_method
+
+ ##
+ # +arg+ may be
+ #
+ # 1. A class or module name (optionally qualified with other class or module
+ # names (Kernel, File::Stat etc)
+ # 2. A method name
+ # 3. A method name qualified by a optionally fully qualified class or module
+ # name
+ #
+ # We're fairly casual about delimiters: folks can say Kernel::puts,
+ # Kernel.puts, or Kernel\#puts for example. There's one exception: if you
+ # say IO::read, we look for a class method, but if you say IO.read, we look
+ # for an instance method
+
+ def initialize(arg)
+ @class_names = []
+ separator = nil
+
+ tokens = arg.split(/(\.|::|#)/)
+
+ # Skip leading '::', '#' or '.', but remember it might
+ # be a method name qualifier
+ separator = tokens.shift if tokens[0] =~ /^(\.|::|#)/
+
+ # Skip leading '::', but remember we potentially have an inst
+
+ # leading stuff must be class names
+
+ while tokens[0] =~ /^[A-Z]/
+ @class_names << tokens.shift
+ unless tokens.empty?
+ separator = tokens.shift
+ break unless separator == "::"
+ end
+ end
+
+ # Now must have a single token, the method name, or an empty array
+ unless tokens.empty?
+ @method_name = tokens.shift
+ # We may now have a trailing !, ?, or = to roll into
+ # the method name
+ if !tokens.empty? && tokens[0] =~ /^[!?=]$/
+ @method_name << tokens.shift
+ end
+
+ if @method_name =~ /::|\.|#/ or !tokens.empty?
+ raise RDoc::RI::Error.new("Bad argument: #{arg}")
+ end
+ if separator && separator != '.'
+ @is_class_method = separator == "::"
+ end
+ end
+ end
+
+ # Return the full class name (with '::' between the components) or "" if
+ # there's no class name
+
+ def full_class_name
+ @class_names.join("::")
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/ri/writer.rb b/ruby/lib/rdoc/ri/writer.rb
new file mode 100644
index 0000000..92aaa1c
--- /dev/null
+++ b/ruby/lib/rdoc/ri/writer.rb
@@ -0,0 +1,68 @@
+require 'fileutils'
+require 'rdoc/ri'
+
+class RDoc::RI::Writer
+
+ def self.class_desc_path(dir, class_desc)
+ File.join(dir, "cdesc-" + class_desc.name + ".yaml")
+ end
+
+ ##
+ # Convert a name from internal form (containing punctuation) to an external
+ # form (where punctuation is replaced by %xx)
+
+ def self.internal_to_external(name)
+ if ''.respond_to? :ord then
+ name.gsub(/\W/) { "%%%02x" % $&[0].ord }
+ else
+ name.gsub(/\W/) { "%%%02x" % $&[0] }
+ end
+ end
+
+ ##
+ # And the reverse operation
+
+ def self.external_to_internal(name)
+ name.gsub(/%([0-9a-f]{2,2})/) { $1.to_i(16).chr }
+ end
+
+ def initialize(base_dir)
+ @base_dir = base_dir
+ end
+
+ def remove_class(class_desc)
+ FileUtils.rm_rf(path_to_dir(class_desc.full_name))
+ end
+
+ def add_class(class_desc)
+ dir = path_to_dir(class_desc.full_name)
+ FileUtils.mkdir_p(dir)
+ class_file_name = self.class.class_desc_path(dir, class_desc)
+ File.open(class_file_name, "w") do |f|
+ f.write(class_desc.serialize)
+ end
+ end
+
+ def add_method(class_desc, method_desc)
+ dir = path_to_dir(class_desc.full_name)
+ file_name = self.class.internal_to_external(method_desc.name)
+ meth_file_name = File.join(dir, file_name)
+ if method_desc.is_singleton
+ meth_file_name += "-c.yaml"
+ else
+ meth_file_name += "-i.yaml"
+ end
+
+ File.open(meth_file_name, "w") do |f|
+ f.write(method_desc.serialize)
+ end
+ end
+
+ private
+
+ def path_to_dir(class_name)
+ File.join(@base_dir, *class_name.split('::'))
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/stats.rb b/ruby/lib/rdoc/stats.rb
new file mode 100644
index 0000000..e18e3c2
--- /dev/null
+++ b/ruby/lib/rdoc/stats.rb
@@ -0,0 +1,115 @@
+require 'rdoc'
+
+##
+# Simple stats collector
+
+class RDoc::Stats
+
+ attr_reader :num_classes
+ attr_reader :num_files
+ attr_reader :num_methods
+ attr_reader :num_modules
+
+ def initialize(verbosity = 1)
+ @num_classes = 0
+ @num_files = 0
+ @num_methods = 0
+ @num_modules = 0
+
+ @start = Time.now
+
+ @display = case verbosity
+ when 0 then Quiet.new
+ when 1 then Normal.new
+ else Verbose.new
+ end
+ end
+
+ def add_alias(as)
+ @display.print_alias as
+ @num_methods += 1
+ end
+
+ def add_class(klass)
+ @display.print_class klass
+ @num_classes += 1
+ end
+
+ def add_file(file)
+ @display.print_file file
+ @num_files += 1
+ end
+
+ def add_method(method)
+ @display.print_method method
+ @num_methods += 1
+ end
+
+ def add_module(mod)
+ @display.print_module mod
+ @num_modules += 1
+ end
+
+ def print
+ puts "Files: #@num_files"
+ puts "Classes: #@num_classes"
+ puts "Modules: #@num_modules"
+ puts "Methods: #@num_methods"
+ puts "Elapsed: " + sprintf("%0.1fs", Time.now - @start)
+ end
+
+ class Quiet
+ def print_alias(*) end
+ def print_class(*) end
+ def print_file(*) end
+ def print_method(*) end
+ def print_module(*) end
+ end
+
+ class Normal
+ def print_alias(as)
+ print 'a'
+ end
+
+ def print_class(klass)
+ print 'C'
+ end
+
+ def print_file(file)
+ print "\n#{file}: "
+ end
+
+ def print_method(method)
+ print 'm'
+ end
+
+ def print_module(mod)
+ print 'M'
+ end
+ end
+
+ class Verbose
+ def print_alias(as)
+ puts "\t\talias #{as.new_name} #{as.old_name}"
+ end
+
+ def print_class(klass)
+ puts "\tclass #{klass.full_name}"
+ end
+
+ def print_file(file)
+ puts file
+ end
+
+ def print_method(method)
+ puts "\t\t#{method.singleton ? '::' : '#'}#{method.name}"
+ end
+
+ def print_module(mod)
+ puts "\tmodule #{mod.full_name}"
+ end
+ end
+
+end
+
+
diff --git a/ruby/lib/rdoc/template.rb b/ruby/lib/rdoc/template.rb
new file mode 100644
index 0000000..53d0e3c
--- /dev/null
+++ b/ruby/lib/rdoc/template.rb
@@ -0,0 +1,64 @@
+require 'erb'
+
+module RDoc; end
+
+##
+# An ERb wrapper that allows nesting of one ERb template inside another.
+#
+# This TemplatePage operates similarly to RDoc 1.x's TemplatePage, but uses
+# ERb instead of a custom template language.
+#
+# Converting from a RDoc 1.x template to an RDoc 2.x template is fairly easy.
+#
+# * %blah% becomes <%= values["blah"] %>
+# * !INCLUDE! becomes <%= template_include %>
+# * HREF:aref:name becomes <%= href values["aref"], values["name"] %>
+# * IF:blah becomes <% if values["blah"] then %>
+# * IFNOT:blah becomes <% unless values["blah"] then %>
+# * ENDIF:blah becomes <% end %>
+# * START:blah becomes <% values["blah"].each do |blah| %>
+# * END:blah becomes <% end %>
+#
+# To make nested loops easier to convert, start by converting START statements
+# to:
+#
+# <% values["blah"].each do |blah| $stderr.puts blah.keys %>
+#
+# So you can see what is being used inside which loop.
+
+class RDoc::TemplatePage
+
+ ##
+ # Create a new TemplatePage that will use +templates+.
+
+ def initialize(*templates)
+ @templates = templates
+ end
+
+ ##
+ # Returns "<a href=\"#{ref}\">#{name}</a>"
+
+ def href(ref, name)
+ if ref then
+ "<a href=\"#{ref}\">#{name}</a>"
+ else
+ name
+ end
+ end
+
+ ##
+ # Process the template using +values+, writing the result to +io+.
+
+ def write_html_on(io, values)
+ b = binding
+ template_include = ""
+
+ @templates.reverse_each do |template|
+ template_include = ERB.new(template).result b
+ end
+
+ io.write template_include
+ end
+
+end
+
diff --git a/ruby/lib/rdoc/tokenstream.rb b/ruby/lib/rdoc/tokenstream.rb
new file mode 100644
index 0000000..0a1eb91
--- /dev/null
+++ b/ruby/lib/rdoc/tokenstream.rb
@@ -0,0 +1,33 @@
+module RDoc; end
+
+##
+# A TokenStream is a list of tokens, gathered during the parse of some entity
+# (say a method). Entities populate these streams by being registered with the
+# lexer. Any class can collect tokens by including TokenStream. From the
+# outside, you use such an object by calling the start_collecting_tokens
+# method, followed by calls to add_token and pop_token.
+
+module RDoc::TokenStream
+
+ def token_stream
+ @token_stream
+ end
+
+ def start_collecting_tokens
+ @token_stream = []
+ end
+
+ def add_token(tk)
+ @token_stream << tk
+ end
+
+ def add_tokens(tks)
+ tks.each {|tk| add_token(tk)}
+ end
+
+ def pop_token
+ @token_stream.pop
+ end
+
+end
+
diff --git a/ruby/lib/resolv-replace.rb b/ruby/lib/resolv-replace.rb
new file mode 100644
index 0000000..63d58ce
--- /dev/null
+++ b/ruby/lib/resolv-replace.rb
@@ -0,0 +1,63 @@
+require 'socket'
+require 'resolv'
+
+class << IPSocket
+ alias original_resolv_getaddress getaddress
+ def getaddress(host)
+ begin
+ return Resolv.getaddress(host).to_s
+ rescue Resolv::ResolvError
+ raise SocketError, "Hostname not known: #{host}"
+ end
+ end
+end
+
+class TCPSocket
+ alias original_resolv_initialize initialize
+ def initialize(host, serv, *rest)
+ rest[0] = IPSocket.getaddress(rest[0]) unless rest.empty?
+ original_resolv_initialize(IPSocket.getaddress(host), serv, *rest)
+ end
+end
+
+class UDPSocket
+ alias original_resolv_bind bind
+ def bind(host, port)
+ host = IPSocket.getaddress(host) if host != ""
+ original_resolv_bind(host, port)
+ end
+
+ alias original_resolv_connect connect
+ def connect(host, port)
+ original_resolv_connect(IPSocket.getaddress(host), port)
+ end
+
+ alias original_resolv_send send
+ def send(mesg, flags, *rest)
+ if rest.length == 2
+ host, port = rest
+ begin
+ addrs = Resolv.getaddresses(host)
+ rescue Resolv::ResolvError
+ raise SocketError, "Hostname not known: #{host}"
+ end
+ err = nil
+ addrs[0...-1].each {|addr|
+ begin
+ return original_resolv_send(mesg, flags, addr, port)
+ rescue SystemCallError
+ end
+ }
+ original_resolv_send(mesg, flags, addrs[-1], port)
+ else
+ original_resolv_send(mesg, flags, *rest)
+ end
+ end
+end
+
+class SOCKSSocket
+ alias original_resolv_initialize initialize
+ def initialize(host, serv)
+ original_resolv_initialize(IPSocket.getaddress(host), port)
+ end
+end if defined? SOCKSSocket
diff --git a/ruby/lib/resolv.rb b/ruby/lib/resolv.rb
new file mode 100644
index 0000000..b201fcf
--- /dev/null
+++ b/ruby/lib/resolv.rb
@@ -0,0 +1,2262 @@
+require 'socket'
+require 'fcntl'
+require 'timeout'
+require 'thread'
+
+begin
+ require 'securerandom'
+rescue LoadError
+end
+
+# Resolv is a thread-aware DNS resolver library written in Ruby. Resolv can
+# handle multiple DNS requests concurrently without blocking. The ruby
+# interpreter.
+#
+# See also resolv-replace.rb to replace the libc resolver with # Resolv.
+#
+# Resolv can look up various DNS resources using the DNS module directly.
+#
+# Examples:
+#
+# p Resolv.getaddress "www.ruby-lang.org"
+# p Resolv.getname "210.251.121.214"
+#
+# Resolv::DNS.open do |dns|
+# ress = dns.getresources "www.ruby-lang.org", Resolv::DNS::Resource::IN::A
+# p ress.map { |r| r.address }
+# ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::IN::MX
+# p ress.map { |r| [r.exchange.to_s, r.preference] }
+# end
+#
+#
+# == Bugs
+#
+# * NIS is not supported.
+# * /etc/nsswitch.conf is not supported.
+
+class Resolv
+
+ ##
+ # Looks up the first IP address for +name+.
+
+ def self.getaddress(name)
+ DefaultResolver.getaddress(name)
+ end
+
+ ##
+ # Looks up all IP address for +name+.
+
+ def self.getaddresses(name)
+ DefaultResolver.getaddresses(name)
+ end
+
+ ##
+ # Iterates over all IP addresses for +name+.
+
+ def self.each_address(name, &block)
+ DefaultResolver.each_address(name, &block)
+ end
+
+ ##
+ # Looks up the hostname of +address+.
+
+ def self.getname(address)
+ DefaultResolver.getname(address)
+ end
+
+ ##
+ # Looks up all hostnames for +address+.
+
+ def self.getnames(address)
+ DefaultResolver.getnames(address)
+ end
+
+ ##
+ # Iterates over all hostnames for +address+.
+
+ def self.each_name(address, &proc)
+ DefaultResolver.each_name(address, &proc)
+ end
+
+ ##
+ # Creates a new Resolv using +resolvers+.
+
+ def initialize(resolvers=[Hosts.new, DNS.new])
+ @resolvers = resolvers
+ end
+
+ ##
+ # Looks up the first IP address for +name+.
+
+ def getaddress(name)
+ each_address(name) {|address| return address}
+ raise ResolvError.new("no address for #{name}")
+ end
+
+ ##
+ # Looks up all IP address for +name+.
+
+ def getaddresses(name)
+ ret = []
+ each_address(name) {|address| ret << address}
+ return ret
+ end
+
+ ##
+ # Iterates over all IP addresses for +name+.
+
+ def each_address(name)
+ if AddressRegex =~ name
+ yield name
+ return
+ end
+ yielded = false
+ @resolvers.each {|r|
+ r.each_address(name) {|address|
+ yield address.to_s
+ yielded = true
+ }
+ return if yielded
+ }
+ end
+
+ ##
+ # Looks up the hostname of +address+.
+
+ def getname(address)
+ each_name(address) {|name| return name}
+ raise ResolvError.new("no name for #{address}")
+ end
+
+ ##
+ # Looks up all hostnames for +address+.
+
+ def getnames(address)
+ ret = []
+ each_name(address) {|name| ret << name}
+ return ret
+ end
+
+ ##
+ # Iterates over all hostnames for +address+.
+
+ def each_name(address)
+ yielded = false
+ @resolvers.each {|r|
+ r.each_name(address) {|name|
+ yield name.to_s
+ yielded = true
+ }
+ return if yielded
+ }
+ end
+
+ ##
+ # Indicates a failure to resolve a name or address.
+
+ class ResolvError < StandardError; end
+
+ ##
+ # Indicates a timeout resolving a name or address.
+
+ class ResolvTimeout < TimeoutError; end
+
+ ##
+ # DNS::Hosts is a hostname resolver that uses the system hosts file.
+
+ class Hosts
+ if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM
+ require 'win32/resolv'
+ DefaultFileName = Win32::Resolv.get_hosts_path
+ else
+ DefaultFileName = '/etc/hosts'
+ end
+
+ ##
+ # Creates a new DNS::Hosts, using +filename+ for its data source.
+
+ def initialize(filename = DefaultFileName)
+ @filename = filename
+ @mutex = Mutex.new
+ @initialized = nil
+ end
+
+ def lazy_initialize # :nodoc:
+ @mutex.synchronize {
+ unless @initialized
+ @name2addr = {}
+ @addr2name = {}
+ open(@filename) {|f|
+ f.each {|line|
+ line.sub!(/#.*/, '')
+ addr, hostname, *aliases = line.split(/\s+/)
+ next unless addr
+ addr.untaint
+ hostname.untaint
+ @addr2name[addr] = [] unless @addr2name.include? addr
+ @addr2name[addr] << hostname
+ @addr2name[addr] += aliases
+ @name2addr[hostname] = [] unless @name2addr.include? hostname
+ @name2addr[hostname] << addr
+ aliases.each {|n|
+ n.untaint
+ @name2addr[n] = [] unless @name2addr.include? n
+ @name2addr[n] << addr
+ }
+ }
+ }
+ @name2addr.each {|name, arr| arr.reverse!}
+ @initialized = true
+ end
+ }
+ self
+ end
+
+ ##
+ # Gets the IP address of +name+ from the hosts file.
+
+ def getaddress(name)
+ each_address(name) {|address| return address}
+ raise ResolvError.new("#{@filename} has no name: #{name}")
+ end
+
+ ##
+ # Gets all IP addresses for +name+ from the hosts file.
+
+ def getaddresses(name)
+ ret = []
+ each_address(name) {|address| ret << address}
+ return ret
+ end
+
+ ##
+ # Iterates over all IP addresses for +name+ retrieved from the hosts file.
+
+ def each_address(name, &proc)
+ lazy_initialize
+ if @name2addr.include?(name)
+ @name2addr[name].each(&proc)
+ end
+ end
+
+ ##
+ # Gets the hostname of +address+ from the hosts file.
+
+ def getname(address)
+ each_name(address) {|name| return name}
+ raise ResolvError.new("#{@filename} has no address: #{address}")
+ end
+
+ ##
+ # Gets all hostnames for +address+ from the hosts file.
+
+ def getnames(address)
+ ret = []
+ each_name(address) {|name| ret << name}
+ return ret
+ end
+
+ ##
+ # Iterates over all hostnames for +address+ retrieved from the hosts file.
+
+ def each_name(address, &proc)
+ lazy_initialize
+ if @addr2name.include?(address)
+ @addr2name[address].each(&proc)
+ end
+ end
+ end
+
+ ##
+ # Resolv::DNS is a DNS stub resolver.
+ #
+ # Information taken from the following places:
+ #
+ # * STD0013
+ # * RFC 1035
+ # * ftp://ftp.isi.edu/in-notes/iana/assignments/dns-parameters
+ # * etc.
+
+ class DNS
+
+ ##
+ # Default DNS Port
+
+ Port = 53
+
+ ##
+ # Default DNS UDP packet size
+
+ UDPSize = 512
+
+ ##
+ # Creates a new DNS resolver. See Resolv::DNS.new for argument details.
+ #
+ # Yields the created DNS resolver to the block, if given, otherwise
+ # returns it.
+
+ def self.open(*args)
+ dns = new(*args)
+ return dns unless block_given?
+ begin
+ yield dns
+ ensure
+ dns.close
+ end
+ end
+
+ ##
+ # Creates a new DNS resolver.
+ #
+ # +config_info+ can be:
+ #
+ # nil:: Uses /etc/resolv.conf.
+ # String:: Path to a file using /etc/resolv.conf's format.
+ # Hash:: Must contain :nameserver, :search and :ndots keys.
+ #
+ # Example:
+ #
+ # Resolv::DNS.new(:nameserver => ['210.251.121.21'],
+ # :search => ['ruby-lang.org'],
+ # :ndots => 1)
+
+ def initialize(config_info=nil)
+ @mutex = Mutex.new
+ @config = Config.new(config_info)
+ @initialized = nil
+ end
+
+ def lazy_initialize # :nodoc:
+ @mutex.synchronize {
+ unless @initialized
+ @config.lazy_initialize
+ @initialized = true
+ end
+ }
+ self
+ end
+
+ ##
+ # Closes the DNS resolver.
+
+ def close
+ @mutex.synchronize {
+ if @initialized
+ @initialized = false
+ end
+ }
+ end
+
+ ##
+ # Gets the IP address of +name+ from the DNS resolver.
+ #
+ # +name+ can be a Resolv::DNS::Name or a String. Retrieved address will
+ # be a Resolv::IPv4 or Resolv::IPv6
+
+ def getaddress(name)
+ each_address(name) {|address| return address}
+ raise ResolvError.new("DNS result has no information for #{name}")
+ end
+
+ ##
+ # Gets all IP addresses for +name+ from the DNS resolver.
+ #
+ # +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
+ # be a Resolv::IPv4 or Resolv::IPv6
+
+ def getaddresses(name)
+ ret = []
+ each_address(name) {|address| ret << address}
+ return ret
+ end
+
+ ##
+ # Iterates over all IP addresses for +name+ retrieved from the DNS
+ # resolver.
+ #
+ # +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
+ # be a Resolv::IPv4 or Resolv::IPv6
+
+ def each_address(name)
+ each_resource(name, Resource::IN::A) {|resource| yield resource.address}
+ each_resource(name, Resource::IN::AAAA) {|resource| yield resource.address}
+ end
+
+ ##
+ # Gets the hostname for +address+ from the DNS resolver.
+ #
+ # +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
+ # name will be a Resolv::DNS::Name.
+
+ def getname(address)
+ each_name(address) {|name| return name}
+ raise ResolvError.new("DNS result has no information for #{address}")
+ end
+
+ ##
+ # Gets all hostnames for +address+ from the DNS resolver.
+ #
+ # +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
+ # names will be Resolv::DNS::Name instances.
+
+ def getnames(address)
+ ret = []
+ each_name(address) {|name| ret << name}
+ return ret
+ end
+
+ ##
+ # Iterates over all hostnames for +address+ retrieved from the DNS
+ # resolver.
+ #
+ # +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
+ # names will be Resolv::DNS::Name instances.
+
+ def each_name(address)
+ case address
+ when Name
+ ptr = address
+ when IPv4::Regex
+ ptr = IPv4.create(address).to_name
+ when IPv6::Regex
+ ptr = IPv6.create(address).to_name
+ else
+ raise ResolvError.new("cannot interpret as address: #{address}")
+ end
+ each_resource(ptr, Resource::IN::PTR) {|resource| yield resource.name}
+ end
+
+ ##
+ # Look up the +typeclass+ DNS resource of +name+.
+ #
+ # +name+ must be a Resolv::DNS::Name or a String.
+ #
+ # +typeclass+ should be one of the following:
+ #
+ # * Resolv::DNS::Resource::IN::A
+ # * Resolv::DNS::Resource::IN::AAAA
+ # * Resolv::DNS::Resource::IN::ANY
+ # * Resolv::DNS::Resource::IN::CNAME
+ # * Resolv::DNS::Resource::IN::HINFO
+ # * Resolv::DNS::Resource::IN::MINFO
+ # * Resolv::DNS::Resource::IN::MX
+ # * Resolv::DNS::Resource::IN::NS
+ # * Resolv::DNS::Resource::IN::PTR
+ # * Resolv::DNS::Resource::IN::SOA
+ # * Resolv::DNS::Resource::IN::TXT
+ # * Resolv::DNS::Resource::IN::WKS
+ #
+ # Returned resource is represented as a Resolv::DNS::Resource instance,
+ # i.e. Resolv::DNS::Resource::IN::A.
+
+ def getresource(name, typeclass)
+ each_resource(name, typeclass) {|resource| return resource}
+ raise ResolvError.new("DNS result has no information for #{name}")
+ end
+
+ ##
+ # Looks up all +typeclass+ DNS resources for +name+. See #getresource for
+ # argument details.
+
+ def getresources(name, typeclass)
+ ret = []
+ each_resource(name, typeclass) {|resource| ret << resource}
+ return ret
+ end
+
+ ##
+ # Iterates over all +typeclass+ DNS resources for +name+. See
+ # #getresource for argument details.
+
+ def each_resource(name, typeclass, &proc)
+ lazy_initialize
+ requester = make_requester
+ senders = {}
+ begin
+ @config.resolv(name) {|candidate, tout, nameserver|
+ msg = Message.new
+ msg.rd = 1
+ msg.add_question(candidate, typeclass)
+ unless sender = senders[[candidate, nameserver]]
+ sender = senders[[candidate, nameserver]] =
+ requester.sender(msg, candidate, nameserver)
+ end
+ reply, reply_name = requester.request(sender, tout)
+ case reply.rcode
+ when RCode::NoError
+ extract_resources(reply, reply_name, typeclass, &proc)
+ return
+ when RCode::NXDomain
+ raise Config::NXDomain.new(reply_name.to_s)
+ else
+ raise Config::OtherResolvError.new(reply_name.to_s)
+ end
+ }
+ ensure
+ requester.close
+ end
+ end
+
+ def make_requester # :nodoc:
+ if nameserver = @config.single?
+ Requester::ConnectedUDP.new(nameserver)
+ else
+ Requester::UnconnectedUDP.new
+ end
+ end
+
+ def extract_resources(msg, name, typeclass) # :nodoc:
+ if typeclass < Resource::ANY
+ n0 = Name.create(name)
+ msg.each_answer {|n, ttl, data|
+ yield data if n0 == n
+ }
+ end
+ yielded = false
+ n0 = Name.create(name)
+ msg.each_answer {|n, ttl, data|
+ if n0 == n
+ case data
+ when typeclass
+ yield data
+ yielded = true
+ when Resource::CNAME
+ n0 = data.name
+ end
+ end
+ }
+ return if yielded
+ msg.each_answer {|n, ttl, data|
+ if n0 == n
+ case data
+ when typeclass
+ yield data
+ end
+ end
+ }
+ end
+
+ if defined? SecureRandom
+ def self.random(arg) # :nodoc:
+ begin
+ SecureRandom.random_number(arg)
+ rescue NotImplementedError
+ rand(arg)
+ end
+ end
+ else
+ def self.random(arg) # :nodoc:
+ rand(arg)
+ end
+ end
+
+
+ def self.rangerand(range) # :nodoc:
+ base = range.begin
+ len = range.end - range.begin
+ if !range.exclude_end?
+ len += 1
+ end
+ base + random(len)
+ end
+
+ RequestID = {}
+ RequestIDMutex = Mutex.new
+
+ def self.allocate_request_id(host, port) # :nodoc:
+ id = nil
+ RequestIDMutex.synchronize {
+ h = (RequestID[[host, port]] ||= {})
+ begin
+ id = rangerand(0x0000..0xffff)
+ end while h[id]
+ h[id] = true
+ }
+ id
+ end
+
+ def self.free_request_id(host, port, id) # :nodoc:
+ RequestIDMutex.synchronize {
+ key = [host, port]
+ if h = RequestID[key]
+ h.delete id
+ if h.empty?
+ RequestID.delete key
+ end
+ end
+ }
+ end
+
+ def self.bind_random_port(udpsock) # :nodoc:
+ begin
+ port = rangerand(1024..65535)
+ udpsock.bind("", port)
+ rescue Errno::EADDRINUSE
+ retry
+ end
+ end
+
+ class Requester # :nodoc:
+ def initialize
+ @senders = {}
+ @sock = nil
+ end
+
+ def request(sender, tout)
+ timelimit = Time.now + tout
+ sender.send
+ while (now = Time.now) < timelimit
+ timeout = timelimit - now
+ if !IO.select([@sock], nil, nil, timeout)
+ raise ResolvTimeout
+ end
+ reply, from = recv_reply
+ begin
+ msg = Message.decode(reply)
+ rescue DecodeError
+ next # broken DNS message ignored
+ end
+ if s = @senders[[from,msg.id]]
+ break
+ else
+ # unexpected DNS message ignored
+ end
+ end
+ return msg, s.data
+ end
+
+ def close
+ sock = @sock
+ @sock = nil
+ sock.close if sock
+ end
+
+ class Sender # :nodoc:
+ def initialize(msg, data, sock)
+ @msg = msg
+ @data = data
+ @sock = sock
+ end
+ end
+
+ class UnconnectedUDP < Requester # :nodoc:
+ def initialize
+ super()
+ @sock = UDPSocket.new
+ @sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::F_SETFD
+ DNS.bind_random_port(@sock)
+ end
+
+ def recv_reply
+ reply, from = @sock.recvfrom(UDPSize)
+ return reply, [from[3],from[1]]
+ end
+
+ def sender(msg, data, host, port=Port)
+ service = [host, port]
+ id = DNS.allocate_request_id(host, port)
+ request = msg.encode
+ request[0,2] = [id].pack('n')
+ return @senders[[service, id]] =
+ Sender.new(request, data, @sock, host, port)
+ end
+
+ def close
+ super
+ @senders.each_key {|service, id|
+ DNS.free_request_id(service[0], service[1], id)
+ }
+ end
+
+ class Sender < Requester::Sender # :nodoc:
+ def initialize(msg, data, sock, host, port)
+ super(msg, data, sock)
+ @host = host
+ @port = port
+ end
+ attr_reader :data
+
+ def send
+ @sock.send(@msg, 0, @host, @port)
+ end
+ end
+ end
+
+ class ConnectedUDP < Requester # :nodoc:
+ def initialize(host, port=Port)
+ super()
+ @host = host
+ @port = port
+ @sock = UDPSocket.new(host.index(':') ? Socket::AF_INET6 : Socket::AF_INET)
+ DNS.bind_random_port(@sock)
+ @sock.connect(host, port)
+ @sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::F_SETFD
+ end
+
+ def recv_reply
+ reply = @sock.recv(UDPSize)
+ return reply, nil
+ end
+
+ def sender(msg, data, host=@host, port=@port)
+ unless host == @host && port == @port
+ raise RequestError.new("host/port don't match: #{host}:#{port}")
+ end
+ id = DNS.allocate_request_id(@host, @port)
+ request = msg.encode
+ request[0,2] = [id].pack('n')
+ return @senders[[nil,id]] = Sender.new(request, data, @sock)
+ end
+
+ def close
+ super
+ @senders.each_key {|from, id|
+ DNS.free_request_id(@host, @port, id)
+ }
+ end
+
+ class Sender < Requester::Sender # :nodoc:
+ def send
+ @sock.send(@msg, 0)
+ end
+ attr_reader :data
+ end
+ end
+
+ class TCP < Requester # :nodoc:
+ def initialize(host, port=Port)
+ super()
+ @host = host
+ @port = port
+ @sock = TCPSocket.new(@host, @port)
+ @sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::F_SETFD
+ @senders = {}
+ end
+
+ def recv_reply
+ len = @sock.read(2).unpack('n')[0]
+ reply = @sock.read(len)
+ return reply, nil
+ end
+
+ def sender(msg, data, host=@host, port=@port)
+ unless host == @host && port == @port
+ raise RequestError.new("host/port don't match: #{host}:#{port}")
+ end
+ id = DNS.allocate_request_id(@host, @port)
+ request = msg.encode
+ request[0,2] = [request.length, id].pack('nn')
+ return @senders[[nil,id]] = Sender.new(request, data, @sock)
+ end
+
+ class Sender < Requester::Sender # :nodoc:
+ def send
+ @sock.print(@msg)
+ @sock.flush
+ end
+ attr_reader :data
+ end
+
+ def close
+ super
+ @senders.each_key {|from,id|
+ DNS.free_request_id(@host, @port, id)
+ }
+ end
+ end
+
+ ##
+ # Indicates a problem with the DNS request.
+
+ class RequestError < StandardError
+ end
+ end
+
+ class Config # :nodoc:
+ def initialize(config_info=nil)
+ @mutex = Mutex.new
+ @config_info = config_info
+ @initialized = nil
+ end
+
+ def Config.parse_resolv_conf(filename)
+ nameserver = []
+ search = nil
+ ndots = 1
+ open(filename) {|f|
+ f.each {|line|
+ line.sub!(/[#;].*/, '')
+ keyword, *args = line.split(/\s+/)
+ args.each { |arg|
+ arg.untaint
+ }
+ next unless keyword
+ case keyword
+ when 'nameserver'
+ nameserver += args
+ when 'domain'
+ next if args.empty?
+ search = [args[0]]
+ when 'search'
+ next if args.empty?
+ search = args
+ when 'options'
+ args.each {|arg|
+ case arg
+ when /\Andots:(\d+)\z/
+ ndots = $1.to_i
+ end
+ }
+ end
+ }
+ }
+ return { :nameserver => nameserver, :search => search, :ndots => ndots }
+ end
+
+ def Config.default_config_hash(filename="/etc/resolv.conf")
+ if File.exist? filename
+ config_hash = Config.parse_resolv_conf(filename)
+ else
+ if /mswin|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
+ require 'win32/resolv'
+ search, nameserver = Win32::Resolv.get_resolv_info
+ config_hash = {}
+ config_hash[:nameserver] = nameserver if nameserver
+ config_hash[:search] = [search].flatten if search
+ end
+ end
+ config_hash
+ end
+
+ def lazy_initialize
+ @mutex.synchronize {
+ unless @initialized
+ @nameserver = []
+ @search = nil
+ @ndots = 1
+ case @config_info
+ when nil
+ config_hash = Config.default_config_hash
+ when String
+ config_hash = Config.parse_resolv_conf(@config_info)
+ when Hash
+ config_hash = @config_info.dup
+ if String === config_hash[:nameserver]
+ config_hash[:nameserver] = [config_hash[:nameserver]]
+ end
+ if String === config_hash[:search]
+ config_hash[:search] = [config_hash[:search]]
+ end
+ else
+ raise ArgumentError.new("invalid resolv configuration: #{@config_info.inspect}")
+ end
+ @nameserver = config_hash[:nameserver] if config_hash.include? :nameserver
+ @search = config_hash[:search] if config_hash.include? :search
+ @ndots = config_hash[:ndots] if config_hash.include? :ndots
+
+ @nameserver = ['0.0.0.0'] if @nameserver.empty?
+ if @search
+ @search = @search.map {|arg| Label.split(arg) }
+ else
+ hostname = Socket.gethostname
+ if /\./ =~ hostname
+ @search = [Label.split($')]
+ else
+ @search = [[]]
+ end
+ end
+
+ if !@nameserver.kind_of?(Array) ||
+ !@nameserver.all? {|ns| String === ns }
+ raise ArgumentError.new("invalid nameserver config: #{@nameserver.inspect}")
+ end
+
+ if !@search.kind_of?(Array) ||
+ !@search.all? {|ls| ls.all? {|l| Label::Str === l } }
+ raise ArgumentError.new("invalid search config: #{@search.inspect}")
+ end
+
+ if !@ndots.kind_of?(Integer)
+ raise ArgumentError.new("invalid ndots config: #{@ndots.inspect}")
+ end
+
+ @initialized = true
+ end
+ }
+ self
+ end
+
+ def single?
+ lazy_initialize
+ if @nameserver.length == 1
+ return @nameserver[0]
+ else
+ return nil
+ end
+ end
+
+ def generate_candidates(name)
+ candidates = nil
+ name = Name.create(name)
+ if name.absolute?
+ candidates = [name]
+ else
+ if @ndots <= name.length - 1
+ candidates = [Name.new(name.to_a)]
+ else
+ candidates = []
+ end
+ candidates.concat(@search.map {|domain| Name.new(name.to_a + domain)})
+ end
+ return candidates
+ end
+
+ InitialTimeout = 5
+
+ def generate_timeouts
+ ts = [InitialTimeout]
+ ts << ts[-1] * 2 / @nameserver.length
+ ts << ts[-1] * 2
+ ts << ts[-1] * 2
+ return ts
+ end
+
+ def resolv(name)
+ candidates = generate_candidates(name)
+ timeouts = generate_timeouts
+ begin
+ candidates.each {|candidate|
+ begin
+ timeouts.each {|tout|
+ @nameserver.each {|nameserver|
+ begin
+ yield candidate, tout, nameserver
+ rescue ResolvTimeout
+ end
+ }
+ }
+ raise ResolvError.new("DNS resolv timeout: #{name}")
+ rescue NXDomain
+ end
+ }
+ rescue ResolvError
+ end
+ end
+
+ ##
+ # Indicates no such domain was found.
+
+ class NXDomain < ResolvError
+ end
+
+ ##
+ # Indicates some other unhandled resolver error was encountered.
+
+ class OtherResolvError < ResolvError
+ end
+ end
+
+ module OpCode # :nodoc:
+ Query = 0
+ IQuery = 1
+ Status = 2
+ Notify = 4
+ Update = 5
+ end
+
+ module RCode # :nodoc:
+ NoError = 0
+ FormErr = 1
+ ServFail = 2
+ NXDomain = 3
+ NotImp = 4
+ Refused = 5
+ YXDomain = 6
+ YXRRSet = 7
+ NXRRSet = 8
+ NotAuth = 9
+ NotZone = 10
+ BADVERS = 16
+ BADSIG = 16
+ BADKEY = 17
+ BADTIME = 18
+ BADMODE = 19
+ BADNAME = 20
+ BADALG = 21
+ end
+
+ ##
+ # Indicates that the DNS response was unable to be decoded.
+
+ class DecodeError < StandardError
+ end
+
+ ##
+ # Indicates that the DNS request was unable to be encoded.
+
+ class EncodeError < StandardError
+ end
+
+ module Label # :nodoc:
+ def self.split(arg)
+ labels = []
+ arg.scan(/[^\.]+/) {labels << Str.new($&)}
+ return labels
+ end
+
+ class Str # :nodoc:
+ def initialize(string)
+ @string = string
+ @downcase = string.downcase
+ end
+ attr_reader :string, :downcase
+
+ def to_s
+ return @string
+ end
+
+ def inspect
+ return "#<#{self.class} #{self.to_s}>"
+ end
+
+ def ==(other)
+ return @downcase == other.downcase
+ end
+
+ def eql?(other)
+ return self == other
+ end
+
+ def hash
+ return @downcase.hash
+ end
+ end
+ end
+
+ ##
+ # A representation of a DNS name.
+
+ class Name
+
+ ##
+ # Creates a new DNS name from +arg+. +arg+ can be:
+ #
+ # Name:: returns +arg+.
+ # String:: Creates a new Name.
+
+ def self.create(arg)
+ case arg
+ when Name
+ return arg
+ when String
+ return Name.new(Label.split(arg), /\.\z/ =~ arg ? true : false)
+ else
+ raise ArgumentError.new("cannot interpret as DNS name: #{arg.inspect}")
+ end
+ end
+
+ def initialize(labels, absolute=true) # :nodoc:
+ @labels = labels
+ @absolute = absolute
+ end
+
+ def inspect # :nodoc:
+ "#<#{self.class}: #{self.to_s}#{@absolute ? '.' : ''}>"
+ end
+
+ ##
+ # True if this name is absolute.
+
+ def absolute?
+ return @absolute
+ end
+
+ def ==(other) # :nodoc:
+ return false unless Name === other
+ return @labels.join == other.to_a.join && @absolute == other.absolute?
+ end
+
+ alias eql? == # :nodoc:
+
+ ##
+ # Returns true if +other+ is a subdomain.
+ #
+ # Example:
+ #
+ # domain = Resolv::DNS::Name.create("y.z")
+ # p Resolv::DNS::Name.create("w.x.y.z").subdomain_of?(domain) #=> true
+ # p Resolv::DNS::Name.create("x.y.z").subdomain_of?(domain) #=> true
+ # p Resolv::DNS::Name.create("y.z").subdomain_of?(domain) #=> false
+ # p Resolv::DNS::Name.create("z").subdomain_of?(domain) #=> false
+ # p Resolv::DNS::Name.create("x.y.z.").subdomain_of?(domain) #=> false
+ # p Resolv::DNS::Name.create("w.z").subdomain_of?(domain) #=> false
+ #
+
+ def subdomain_of?(other)
+ raise ArgumentError, "not a domain name: #{other.inspect}" unless Name === other
+ return false if @absolute != other.absolute?
+ other_len = other.length
+ return false if @labels.length <= other_len
+ return @labels[-other_len, other_len] == other.to_a
+ end
+
+ def hash # :nodoc:
+ return @labels.hash ^ @absolute.hash
+ end
+
+ def to_a # :nodoc:
+ return @labels
+ end
+
+ def length # :nodoc:
+ return @labels.length
+ end
+
+ def [](i) # :nodoc:
+ return @labels[i]
+ end
+
+ ##
+ # returns the domain name as a string.
+ #
+ # The domain name doesn't have a trailing dot even if the name object is
+ # absolute.
+ #
+ # Example:
+ #
+ # p Resolv::DNS::Name.create("x.y.z.").to_s #=> "x.y.z"
+ # p Resolv::DNS::Name.create("x.y.z").to_s #=> "x.y.z"
+
+ def to_s
+ return @labels.join('.')
+ end
+ end
+
+ class Message # :nodoc:
+ @@identifier = -1
+
+ def initialize(id = (@@identifier += 1) & 0xffff)
+ @id = id
+ @qr = 0
+ @opcode = 0
+ @aa = 0
+ @tc = 0
+ @rd = 0 # recursion desired
+ @ra = 0 # recursion available
+ @rcode = 0
+ @question = []
+ @answer = []
+ @authority = []
+ @additional = []
+ end
+
+ attr_accessor :id, :qr, :opcode, :aa, :tc, :rd, :ra, :rcode
+ attr_reader :question, :answer, :authority, :additional
+
+ def ==(other)
+ return @id == other.id &&
+ @qr == other.qr &&
+ @opcode == other.opcode &&
+ @aa == other.aa &&
+ @tc == other.tc &&
+ @rd == other.rd &&
+ @ra == other.ra &&
+ @rcode == other.rcode &&
+ @question == other.question &&
+ @answer == other.answer &&
+ @authority == other.authority &&
+ @additional == other.additional
+ end
+
+ def add_question(name, typeclass)
+ @question << [Name.create(name), typeclass]
+ end
+
+ def each_question
+ @question.each {|name, typeclass|
+ yield name, typeclass
+ }
+ end
+
+ def add_answer(name, ttl, data)
+ @answer << [Name.create(name), ttl, data]
+ end
+
+ def each_answer
+ @answer.each {|name, ttl, data|
+ yield name, ttl, data
+ }
+ end
+
+ def add_authority(name, ttl, data)
+ @authority << [Name.create(name), ttl, data]
+ end
+
+ def each_authority
+ @authority.each {|name, ttl, data|
+ yield name, ttl, data
+ }
+ end
+
+ def add_additional(name, ttl, data)
+ @additional << [Name.create(name), ttl, data]
+ end
+
+ def each_additional
+ @additional.each {|name, ttl, data|
+ yield name, ttl, data
+ }
+ end
+
+ def each_resource
+ each_answer {|name, ttl, data| yield name, ttl, data}
+ each_authority {|name, ttl, data| yield name, ttl, data}
+ each_additional {|name, ttl, data| yield name, ttl, data}
+ end
+
+ def encode
+ return MessageEncoder.new {|msg|
+ msg.put_pack('nnnnnn',
+ @id,
+ (@qr & 1) << 15 |
+ (@opcode & 15) << 11 |
+ (@aa & 1) << 10 |
+ (@tc & 1) << 9 |
+ (@rd & 1) << 8 |
+ (@ra & 1) << 7 |
+ (@rcode & 15),
+ @question.length,
+ @answer.length,
+ @authority.length,
+ @additional.length)
+ @question.each {|q|
+ name, typeclass = q
+ msg.put_name(name)
+ msg.put_pack('nn', typeclass::TypeValue, typeclass::ClassValue)
+ }
+ [@answer, @authority, @additional].each {|rr|
+ rr.each {|r|
+ name, ttl, data = r
+ msg.put_name(name)
+ msg.put_pack('nnN', data.class::TypeValue, data.class::ClassValue, ttl)
+ msg.put_length16 {data.encode_rdata(msg)}
+ }
+ }
+ }.to_s
+ end
+
+ class MessageEncoder # :nodoc:
+ def initialize
+ @data = ''
+ @names = {}
+ yield self
+ end
+
+ def to_s
+ return @data
+ end
+
+ def put_bytes(d)
+ @data << d
+ end
+
+ def put_pack(template, *d)
+ @data << d.pack(template)
+ end
+
+ def put_length16
+ length_index = @data.length
+ @data << "\0\0"
+ data_start = @data.length
+ yield
+ data_end = @data.length
+ @data[length_index, 2] = [data_end - data_start].pack("n")
+ end
+
+ def put_string(d)
+ self.put_pack("C", d.length)
+ @data << d
+ end
+
+ def put_string_list(ds)
+ ds.each {|d|
+ self.put_string(d)
+ }
+ end
+
+ def put_name(d)
+ put_labels(d.to_a)
+ end
+
+ def put_labels(d)
+ d.each_index {|i|
+ domain = d[i..-1]
+ if idx = @names[domain]
+ self.put_pack("n", 0xc000 | idx)
+ return
+ else
+ @names[domain] = @data.length
+ self.put_label(d[i])
+ end
+ }
+ @data << "\0"
+ end
+
+ def put_label(d)
+ self.put_string(d.to_s)
+ end
+ end
+
+ def Message.decode(m)
+ o = Message.new(0)
+ MessageDecoder.new(m) {|msg|
+ id, flag, qdcount, ancount, nscount, arcount =
+ msg.get_unpack('nnnnnn')
+ o.id = id
+ o.qr = (flag >> 15) & 1
+ o.opcode = (flag >> 11) & 15
+ o.aa = (flag >> 10) & 1
+ o.tc = (flag >> 9) & 1
+ o.rd = (flag >> 8) & 1
+ o.ra = (flag >> 7) & 1
+ o.rcode = flag & 15
+ (1..qdcount).each {
+ name, typeclass = msg.get_question
+ o.add_question(name, typeclass)
+ }
+ (1..ancount).each {
+ name, ttl, data = msg.get_rr
+ o.add_answer(name, ttl, data)
+ }
+ (1..nscount).each {
+ name, ttl, data = msg.get_rr
+ o.add_authority(name, ttl, data)
+ }
+ (1..arcount).each {
+ name, ttl, data = msg.get_rr
+ o.add_additional(name, ttl, data)
+ }
+ }
+ return o
+ end
+
+ class MessageDecoder # :nodoc:
+ def initialize(data)
+ @data = data
+ @index = 0
+ @limit = data.length
+ yield self
+ end
+
+ def get_length16
+ len, = self.get_unpack('n')
+ save_limit = @limit
+ @limit = @index + len
+ d = yield(len)
+ if @index < @limit
+ raise DecodeError.new("junk exists")
+ elsif @limit < @index
+ raise DecodeError.new("limit exceeded")
+ end
+ @limit = save_limit
+ return d
+ end
+
+ def get_bytes(len = @limit - @index)
+ d = @data[@index, len]
+ @index += len
+ return d
+ end
+
+ def get_unpack(template)
+ len = 0
+ template.each_byte {|byte|
+ byte = "%c" % byte
+ case byte
+ when ?c, ?C
+ len += 1
+ when ?n
+ len += 2
+ when ?N
+ len += 4
+ else
+ raise StandardError.new("unsupported template: '#{byte.chr}' in '#{template}'")
+ end
+ }
+ raise DecodeError.new("limit exceeded") if @limit < @index + len
+ arr = @data.unpack("@#{@index}#{template}")
+ @index += len
+ return arr
+ end
+
+ def get_string
+ len = @data[@index].ord
+ raise DecodeError.new("limit exceeded") if @limit < @index + 1 + len
+ d = @data[@index + 1, len]
+ @index += 1 + len
+ return d
+ end
+
+ def get_string_list
+ strings = []
+ while @index < @limit
+ strings << self.get_string
+ end
+ strings
+ end
+
+ def get_name
+ return Name.new(self.get_labels)
+ end
+
+ def get_labels(limit=nil)
+ limit = @index if !limit || @index < limit
+ d = []
+ while true
+ case @data[@index].ord
+ when 0
+ @index += 1
+ return d
+ when 192..255
+ idx = self.get_unpack('n')[0] & 0x3fff
+ if limit <= idx
+ raise DecodeError.new("non-backward name pointer")
+ end
+ save_index = @index
+ @index = idx
+ d += self.get_labels(limit)
+ @index = save_index
+ return d
+ else
+ d << self.get_label
+ end
+ end
+ return d
+ end
+
+ def get_label
+ return Label::Str.new(self.get_string)
+ end
+
+ def get_question
+ name = self.get_name
+ type, klass = self.get_unpack("nn")
+ return name, Resource.get_class(type, klass)
+ end
+
+ def get_rr
+ name = self.get_name
+ type, klass, ttl = self.get_unpack('nnN')
+ typeclass = Resource.get_class(type, klass)
+ res = self.get_length16 { typeclass.decode_rdata self }
+ res.instance_variable_set :@ttl, ttl
+ return name, ttl, res
+ end
+ end
+ end
+
+ ##
+ # A DNS query abstract class.
+
+ class Query
+ def encode_rdata(msg) # :nodoc:
+ raise EncodeError.new("#{self.class} is query.")
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ raise DecodeError.new("#{self.class} is query.")
+ end
+ end
+
+ ##
+ # A DNS resource abstract class.
+
+ class Resource < Query
+
+ ##
+ # Remaining Time To Live for this Resource.
+
+ attr_reader :ttl
+
+ ClassHash = {} # :nodoc:
+
+ def encode_rdata(msg) # :nodoc:
+ raise NotImplementedError.new
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ raise NotImplementedError.new
+ end
+
+ def ==(other) # :nodoc:
+ return false unless self.class == other.class
+ s_ivars = self.instance_variables
+ s_ivars.sort!
+ s_ivars.delete "@ttl"
+ o_ivars = other.instance_variables
+ o_ivars.sort!
+ o_ivars.delete "@ttl"
+ return s_ivars == o_ivars &&
+ s_ivars.collect {|name| self.instance_variable_get name} ==
+ o_ivars.collect {|name| other.instance_variable_get name}
+ end
+
+ def eql?(other) # :nodoc:
+ return self == other
+ end
+
+ def hash # :nodoc:
+ h = 0
+ vars = self.instance_variables
+ vars.delete "@ttl"
+ vars.each {|name|
+ h ^= self.instance_variable_get(name).hash
+ }
+ return h
+ end
+
+ def self.get_class(type_value, class_value) # :nodoc:
+ return ClassHash[[type_value, class_value]] ||
+ Generic.create(type_value, class_value)
+ end
+
+ ##
+ # A generic resource abstract class.
+
+ class Generic < Resource
+
+ ##
+ # Creates a new generic resource.
+
+ def initialize(data)
+ @data = data
+ end
+
+ ##
+ # Data for this generic resource.
+
+ attr_reader :data
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_bytes(data)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ return self.new(msg.get_bytes)
+ end
+
+ def self.create(type_value, class_value) # :nodoc:
+ c = Class.new(Generic)
+ c.const_set(:TypeValue, type_value)
+ c.const_set(:ClassValue, class_value)
+ Generic.const_set("Type#{type_value}_Class#{class_value}", c)
+ ClassHash[[type_value, class_value]] = c
+ return c
+ end
+ end
+
+ ##
+ # Domain Name resource abstract class.
+
+ class DomainName < Resource
+
+ ##
+ # Creates a new DomainName from +name+.
+
+ def initialize(name)
+ @name = name
+ end
+
+ ##
+ # The name of this DomainName.
+
+ attr_reader :name
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_name(@name)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ return self.new(msg.get_name)
+ end
+ end
+
+ # Standard (class generic) RRs
+
+ ClassValue = nil # :nodoc:
+
+ ##
+ # An authoritative name server.
+
+ class NS < DomainName
+ TypeValue = 2 # :nodoc:
+ end
+
+ ##
+ # The canonical name for an alias.
+
+ class CNAME < DomainName
+ TypeValue = 5 # :nodoc:
+ end
+
+ ##
+ # Start Of Authority resource.
+
+ class SOA < Resource
+
+ TypeValue = 6 # :nodoc:
+
+ ##
+ # Creates a new SOA record. See the attr documentation for the
+ # details of each argument.
+
+ def initialize(mname, rname, serial, refresh, retry_, expire, minimum)
+ @mname = mname
+ @rname = rname
+ @serial = serial
+ @refresh = refresh
+ @retry = retry_
+ @expire = expire
+ @minimum = minimum
+ end
+
+ ##
+ # Name of the host where the master zone file for this zone resides.
+
+ attr_reader :mname
+
+ ##
+ # The person responsible for this domain name.
+
+ attr_reader :rname
+
+ ##
+ # The version number of the zone file.
+
+ attr_reader :serial
+
+ ##
+ # How often, in seconds, a secondary name server is to check for
+ # updates from the primary name server.
+
+ attr_reader :refresh
+
+ ##
+ # How often, in seconds, a secondary name server is to retry after a
+ # failure to check for a refresh.
+
+ attr_reader :retry
+
+ ##
+ # Time in seconds that a secondary name server is to use the data
+ # before refreshing from the primary name server.
+
+ attr_reader :expire
+
+ ##
+ # The minimum number of seconds to be used for TTL values in RRs.
+
+ attr_reader :minimum
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_name(@mname)
+ msg.put_name(@rname)
+ msg.put_pack('NNNNN', @serial, @refresh, @retry, @expire, @minimum)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ mname = msg.get_name
+ rname = msg.get_name
+ serial, refresh, retry_, expire, minimum = msg.get_unpack('NNNNN')
+ return self.new(
+ mname, rname, serial, refresh, retry_, expire, minimum)
+ end
+ end
+
+ ##
+ # A Pointer to another DNS name.
+
+ class PTR < DomainName
+ TypeValue = 12 # :nodoc:
+ end
+
+ ##
+ # Host Information resource.
+
+ class HINFO < Resource
+
+ TypeValue = 13 # :nodoc:
+
+ ##
+ # Creates a new HINFO running +os+ on +cpu+.
+
+ def initialize(cpu, os)
+ @cpu = cpu
+ @os = os
+ end
+
+ ##
+ # CPU architecture for this resource.
+
+ attr_reader :cpu
+
+ ##
+ # Operating system for this resource.
+
+ attr_reader :os
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_string(@cpu)
+ msg.put_string(@os)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ cpu = msg.get_string
+ os = msg.get_string
+ return self.new(cpu, os)
+ end
+ end
+
+ ##
+ # Mailing list or mailbox information.
+
+ class MINFO < Resource
+
+ TypeValue = 14 # :nodoc:
+
+ def initialize(rmailbx, emailbx)
+ @rmailbx = rmailbx
+ @emailbx = emailbx
+ end
+
+ ##
+ # Domain name responsible for this mail list or mailbox.
+
+ attr_reader :rmailbx
+
+ ##
+ # Mailbox to use for error messages related to the mail list or mailbox.
+
+ attr_reader :emailbx
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_name(@rmailbx)
+ msg.put_name(@emailbx)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ rmailbx = msg.get_string
+ emailbx = msg.get_string
+ return self.new(rmailbx, emailbx)
+ end
+ end
+
+ ##
+ # Mail Exchanger resource.
+
+ class MX < Resource
+
+ TypeValue= 15 # :nodoc:
+
+ ##
+ # Creates a new MX record with +preference+, accepting mail at
+ # +exchange+.
+
+ def initialize(preference, exchange)
+ @preference = preference
+ @exchange = exchange
+ end
+
+ ##
+ # The preference for this MX.
+
+ attr_reader :preference
+
+ ##
+ # The host of this MX.
+
+ attr_reader :exchange
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_pack('n', @preference)
+ msg.put_name(@exchange)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ preference, = msg.get_unpack('n')
+ exchange = msg.get_name
+ return self.new(preference, exchange)
+ end
+ end
+
+ ##
+ # Unstructured text resource.
+
+ class TXT < Resource
+
+ TypeValue = 16 # :nodoc:
+
+ def initialize(first_string, *rest_strings)
+ @strings = [first_string, *rest_strings]
+ end
+
+ ##
+ # Returns an Array of Strings for this TXT record.
+
+ attr_reader :strings
+
+ ##
+ # Returns the first string from +strings+.
+
+ def data
+ @strings[0]
+ end
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_string_list(@strings)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ strings = msg.get_string_list
+ return self.new(*strings)
+ end
+ end
+
+ ##
+ # A Query type requesting any RR.
+
+ class ANY < Query
+ TypeValue = 255 # :nodoc:
+ end
+
+ ClassInsensitiveTypes = [ # :nodoc:
+ NS, CNAME, SOA, PTR, HINFO, MINFO, MX, TXT, ANY
+ ]
+
+ ##
+ # module IN contains ARPA Internet specific RRs.
+
+ module IN
+
+ ClassValue = 1 # :nodoc:
+
+ ClassInsensitiveTypes.each {|s|
+ c = Class.new(s)
+ c.const_set(:TypeValue, s::TypeValue)
+ c.const_set(:ClassValue, ClassValue)
+ ClassHash[[s::TypeValue, ClassValue]] = c
+ self.const_set(s.name.sub(/.*::/, ''), c)
+ }
+
+ ##
+ # IPv4 Address resource
+
+ class A < Resource
+ TypeValue = 1
+ ClassValue = IN::ClassValue
+ ClassHash[[TypeValue, ClassValue]] = self # :nodoc:
+
+ ##
+ # Creates a new A for +address+.
+
+ def initialize(address)
+ @address = IPv4.create(address)
+ end
+
+ ##
+ # The Resolv::IPv4 address for this A.
+
+ attr_reader :address
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_bytes(@address.address)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ return self.new(IPv4.new(msg.get_bytes(4)))
+ end
+ end
+
+ ##
+ # Well Known Service resource.
+
+ class WKS < Resource
+ TypeValue = 11
+ ClassValue = IN::ClassValue
+ ClassHash[[TypeValue, ClassValue]] = self # :nodoc:
+
+ def initialize(address, protocol, bitmap)
+ @address = IPv4.create(address)
+ @protocol = protocol
+ @bitmap = bitmap
+ end
+
+ ##
+ # The host these services run on.
+
+ attr_reader :address
+
+ ##
+ # IP protocol number for these services.
+
+ attr_reader :protocol
+
+ ##
+ # A bit map of enabled services on this host.
+ #
+ # If protocol is 6 (TCP) then the 26th bit corresponds to the SMTP
+ # service (port 25). If this bit is set, then an SMTP server should
+ # be listening on TCP port 25; if zero, SMTP service is not
+ # supported.
+
+ attr_reader :bitmap
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_bytes(@address.address)
+ msg.put_pack("n", @protocol)
+ msg.put_bytes(@bitmap)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ address = IPv4.new(msg.get_bytes(4))
+ protocol, = msg.get_unpack("n")
+ bitmap = msg.get_bytes
+ return self.new(address, protocol, bitmap)
+ end
+ end
+
+ ##
+ # An IPv6 address record.
+
+ class AAAA < Resource
+ TypeValue = 28
+ ClassValue = IN::ClassValue
+ ClassHash[[TypeValue, ClassValue]] = self # :nodoc:
+
+ ##
+ # Creates a new AAAA for +address+.
+
+ def initialize(address)
+ @address = IPv6.create(address)
+ end
+
+ ##
+ # The Resolv::IPv6 address for this AAAA.
+
+ attr_reader :address
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_bytes(@address.address)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ return self.new(IPv6.new(msg.get_bytes(16)))
+ end
+ end
+
+ ##
+ # SRV resource record defined in RFC 2782
+ #
+ # These records identify the hostname and port that a service is
+ # available at.
+
+ class SRV < Resource
+ TypeValue = 33
+ ClassValue = IN::ClassValue
+ ClassHash[[TypeValue, ClassValue]] = self # :nodoc:
+
+ # Create a SRV resource record.
+ #
+ # See the documentation for #priority, #weight, #port and #target
+ # for +priority+, +weight+, +port and +target+ respectively.
+
+ def initialize(priority, weight, port, target)
+ @priority = priority.to_int
+ @weight = weight.to_int
+ @port = port.to_int
+ @target = Name.create(target)
+ end
+
+ # The priority of this target host.
+ #
+ # A client MUST attempt to contact the target host with the
+ # lowest-numbered priority it can reach; target hosts with the same
+ # priority SHOULD be tried in an order defined by the weight field.
+ # The range is 0-65535. Note that it is not widely implemented and
+ # should be set to zero.
+
+ attr_reader :priority
+
+ # A server selection mechanism.
+ #
+ # The weight field specifies a relative weight for entries with the
+ # same priority. Larger weights SHOULD be given a proportionately
+ # higher probability of being selected. The range of this number is
+ # 0-65535. Domain administrators SHOULD use Weight 0 when there
+ # isn't any server selection to do, to make the RR easier to read
+ # for humans (less noisy). Note that it is not widely implemented
+ # and should be set to zero.
+
+ attr_reader :weight
+
+ # The port on this target host of this service.
+ #
+ # The range is 0-65535.
+
+ attr_reader :port
+
+ # The domain name of the target host.
+ #
+ # A target of "." means that the service is decidedly not available
+ # at this domain.
+
+ attr_reader :target
+
+ def encode_rdata(msg) # :nodoc:
+ msg.put_pack("n", @priority)
+ msg.put_pack("n", @weight)
+ msg.put_pack("n", @port)
+ msg.put_name(@target)
+ end
+
+ def self.decode_rdata(msg) # :nodoc:
+ priority, = msg.get_unpack("n")
+ weight, = msg.get_unpack("n")
+ port, = msg.get_unpack("n")
+ target = msg.get_name
+ return self.new(priority, weight, port, target)
+ end
+ end
+ end
+ end
+ end
+
+ ##
+ # A Resolv::DNS IPv4 address.
+
+ class IPv4
+
+ ##
+ # Regular expression IPv4 addresses must match.
+
+ Regex = /\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/
+
+ def self.create(arg)
+ case arg
+ when IPv4
+ return arg
+ when Regex
+ if (0..255) === (a = $1.to_i) &&
+ (0..255) === (b = $2.to_i) &&
+ (0..255) === (c = $3.to_i) &&
+ (0..255) === (d = $4.to_i)
+ return self.new([a, b, c, d].pack("CCCC"))
+ else
+ raise ArgumentError.new("IPv4 address with invalid value: " + arg)
+ end
+ else
+ raise ArgumentError.new("cannot interpret as IPv4 address: #{arg.inspect}")
+ end
+ end
+
+ def initialize(address) # :nodoc:
+ unless address.kind_of?(String) && address.length == 4
+ raise ArgumentError.new('IPv4 address must be 4 bytes')
+ end
+ @address = address
+ end
+
+ ##
+ # A String representation of this IPv4 address.
+
+ ##
+ # The raw IPv4 address as a String.
+
+ attr_reader :address
+
+ def to_s # :nodoc:
+ return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC"))
+ end
+
+ def inspect # :nodoc:
+ return "#<#{self.class} #{self.to_s}>"
+ end
+
+ ##
+ # Turns this IPv4 address into a Resolv::DNS::Name.
+
+ def to_name
+ return DNS::Name.create(
+ '%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse)
+ end
+
+ def ==(other) # :nodoc:
+ return @address == other.address
+ end
+
+ def eql?(other) # :nodoc:
+ return self == other
+ end
+
+ def hash # :nodoc:
+ return @address.hash
+ end
+ end
+
+ ##
+ # A Resolv::DNS IPv6 address.
+
+ class IPv6
+
+ ##
+ # IPv6 address format a:b:c:d:e:f:g:h
+ Regex_8Hex = /\A
+ (?:[0-9A-Fa-f]{1,4}:){7}
+ [0-9A-Fa-f]{1,4}
+ \z/x
+
+ ##
+ # Compressed IPv6 address format a::b
+
+ Regex_CompressedHex = /\A
+ ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::
+ ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)
+ \z/x
+
+ ##
+ # IPv4 mapped IPv6 address format a:b:c:d:e:f:w.x.y.z
+
+ Regex_6Hex4Dec = /\A
+ ((?:[0-9A-Fa-f]{1,4}:){6,6})
+ (\d+)\.(\d+)\.(\d+)\.(\d+)
+ \z/x
+
+ ##
+ # Compressed IPv4 mapped IPv6 address format a::b:w.x.y.z
+
+ Regex_CompressedHex4Dec = /\A
+ ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::
+ ((?:[0-9A-Fa-f]{1,4}:)*)
+ (\d+)\.(\d+)\.(\d+)\.(\d+)
+ \z/x
+
+ ##
+ # A composite IPv6 address Regexp.
+
+ Regex = /
+ (?:#{Regex_8Hex}) |
+ (?:#{Regex_CompressedHex}) |
+ (?:#{Regex_6Hex4Dec}) |
+ (?:#{Regex_CompressedHex4Dec})/x
+
+ ##
+ # Creates a new IPv6 address from +arg+ which may be:
+ #
+ # IPv6:: returns +arg+.
+ # String:: +arg+ must match one of the IPv6::Regex* constants
+
+ def self.create(arg)
+ case arg
+ when IPv6
+ return arg
+ when String
+ address = ''
+ if Regex_8Hex =~ arg
+ arg.scan(/[0-9A-Fa-f]+/) {|hex| address << [hex.hex].pack('n')}
+ elsif Regex_CompressedHex =~ arg
+ prefix = $1
+ suffix = $2
+ a1 = ''
+ a2 = ''
+ prefix.scan(/[0-9A-Fa-f]+/) {|hex| a1 << [hex.hex].pack('n')}
+ suffix.scan(/[0-9A-Fa-f]+/) {|hex| a2 << [hex.hex].pack('n')}
+ omitlen = 16 - a1.length - a2.length
+ address << a1 << "\0" * omitlen << a2
+ elsif Regex_6Hex4Dec =~ arg
+ prefix, a, b, c, d = $1, $2.to_i, $3.to_i, $4.to_i, $5.to_i
+ if (0..255) === a && (0..255) === b && (0..255) === c && (0..255) === d
+ prefix.scan(/[0-9A-Fa-f]+/) {|hex| address << [hex.hex].pack('n')}
+ address << [a, b, c, d].pack('CCCC')
+ else
+ raise ArgumentError.new("not numeric IPv6 address: " + arg)
+ end
+ elsif Regex_CompressedHex4Dec =~ arg
+ prefix, suffix, a, b, c, d = $1, $2, $3.to_i, $4.to_i, $5.to_i, $6.to_i
+ if (0..255) === a && (0..255) === b && (0..255) === c && (0..255) === d
+ a1 = ''
+ a2 = ''
+ prefix.scan(/[0-9A-Fa-f]+/) {|hex| a1 << [hex.hex].pack('n')}
+ suffix.scan(/[0-9A-Fa-f]+/) {|hex| a2 << [hex.hex].pack('n')}
+ omitlen = 12 - a1.length - a2.length
+ address << a1 << "\0" * omitlen << a2 << [a, b, c, d].pack('CCCC')
+ else
+ raise ArgumentError.new("not numeric IPv6 address: " + arg)
+ end
+ else
+ raise ArgumentError.new("not numeric IPv6 address: " + arg)
+ end
+ return IPv6.new(address)
+ else
+ raise ArgumentError.new("cannot interpret as IPv6 address: #{arg.inspect}")
+ end
+ end
+
+ def initialize(address) # :nodoc:
+ unless address.kind_of?(String) && address.length == 16
+ raise ArgumentError.new('IPv6 address must be 16 bytes')
+ end
+ @address = address
+ end
+
+ ##
+ # The raw IPv6 address as a String.
+
+ attr_reader :address
+
+ def to_s # :nodoc:
+ address = sprintf("%X:%X:%X:%X:%X:%X:%X:%X", *@address.unpack("nnnnnnnn"))
+ unless address.sub!(/(^|:)0(:0)+(:|$)/, '::')
+ address.sub!(/(^|:)0(:|$)/, '::')
+ end
+ return address
+ end
+
+ def inspect # :nodoc:
+ return "#<#{self.class} #{self.to_s}>"
+ end
+
+ ##
+ # Turns this IPv6 address into a Resolv::DNS::Name.
+ #--
+ # ip6.arpa should be searched too. [RFC3152]
+
+ def to_name
+ return DNS::Name.new(
+ @address.unpack("H32")[0].split(//).reverse + ['ip6', 'arpa'])
+ end
+
+ def ==(other) # :nodoc:
+ return @address == other.address
+ end
+
+ def eql?(other) # :nodoc:
+ return self == other
+ end
+
+ def hash # :nodoc:
+ return @address.hash
+ end
+ end
+
+ ##
+ # Default resolver to use for Resolv class methods.
+
+ DefaultResolver = self.new
+
+ ##
+ # Address Regexp to use for matching IP addresses.
+
+ AddressRegex = /(?:#{IPv4::Regex})|(?:#{IPv6::Regex})/
+
+end
+
diff --git a/ruby/lib/rexml/attlistdecl.rb b/ruby/lib/rexml/attlistdecl.rb
new file mode 100644
index 0000000..ea5a98b
--- /dev/null
+++ b/ruby/lib/rexml/attlistdecl.rb
@@ -0,0 +1,62 @@
+#vim:ts=2 sw=2 noexpandtab:
+require 'rexml/child'
+require 'rexml/source'
+
+module REXML
+ # This class needs:
+ # * Documentation
+ # * Work! Not all types of attlists are intelligently parsed, so we just
+ # spew back out what we get in. This works, but it would be better if
+ # we formatted the output ourselves.
+ #
+ # AttlistDecls provide *just* enough support to allow namespace
+ # declarations. If you need some sort of generalized support, or have an
+ # interesting idea about how to map the hideous, terrible design of DTD
+ # AttlistDecls onto an intuitive Ruby interface, let me know. I'm desperate
+ # for anything to make DTDs more palateable.
+ class AttlistDecl < Child
+ include Enumerable
+
+ # What is this? Got me.
+ attr_reader :element_name
+
+ # Create an AttlistDecl, pulling the information from a Source. Notice
+ # that this isn't very convenient; to create an AttlistDecl, you basically
+ # have to format it yourself, and then have the initializer parse it.
+ # Sorry, but for the forseeable future, DTD support in REXML is pretty
+ # weak on convenience. Have I mentioned how much I hate DTDs?
+ def initialize(source)
+ super()
+ if (source.kind_of? Array)
+ @element_name, @pairs, @contents = *source
+ end
+ end
+
+ # Access the attlist attribute/value pairs.
+ # value = attlist_decl[ attribute_name ]
+ def [](key)
+ @pairs[key]
+ end
+
+ # Whether an attlist declaration includes the given attribute definition
+ # if attlist_decl.include? "xmlns:foobar"
+ def include?(key)
+ @pairs.keys.include? key
+ end
+
+ # Iterate over the key/value pairs:
+ # attlist_decl.each { |attribute_name, attribute_value| ... }
+ def each(&block)
+ @pairs.each(&block)
+ end
+
+ # Write out exactly what we got in.
+ def write out, indent=-1
+ out << @contents
+ end
+
+ def node_type
+ :attlistdecl
+ end
+ end
+end
diff --git a/ruby/lib/rexml/attribute.rb b/ruby/lib/rexml/attribute.rb
new file mode 100644
index 0000000..febcc28
--- /dev/null
+++ b/ruby/lib/rexml/attribute.rb
@@ -0,0 +1,188 @@
+require "rexml/namespace"
+require 'rexml/text'
+
+module REXML
+ # Defines an Element Attribute; IE, a attribute=value pair, as in:
+ # <element attribute="value"/>. Attributes can be in their own
+ # namespaces. General users of REXML will not interact with the
+ # Attribute class much.
+ class Attribute
+ include Node
+ include Namespace
+
+ # The element to which this attribute belongs
+ attr_reader :element
+ # The normalized value of this attribute. That is, the attribute with
+ # entities intact.
+ attr_writer :normalized
+ PATTERN = /\s*(#{NAME_STR})\s*=\s*(["'])(.*?)\2/um
+
+ NEEDS_A_SECOND_CHECK = /(<|&((#{Entity::NAME});|(#0*((?:\d+)|(?:x[a-fA-F0-9]+)));)?)/um
+
+ # Constructor.
+ # FIXME: The parser doesn't catch illegal characters in attributes
+ #
+ # first::
+ # Either: an Attribute, which this new attribute will become a
+ # clone of; or a String, which is the name of this attribute
+ # second::
+ # If +first+ is an Attribute, then this may be an Element, or nil.
+ # If nil, then the Element parent of this attribute is the parent
+ # of the +first+ Attribute. If the first argument is a String,
+ # then this must also be a String, and is the content of the attribute.
+ # If this is the content, it must be fully normalized (contain no
+ # illegal characters).
+ # parent::
+ # Ignored unless +first+ is a String; otherwise, may be the Element
+ # parent of this attribute, or nil.
+ #
+ #
+ # Attribute.new( attribute_to_clone )
+ # Attribute.new( attribute_to_clone, parent_element )
+ # Attribute.new( "attr", "attr_value" )
+ # Attribute.new( "attr", "attr_value", parent_element )
+ def initialize( first, second=nil, parent=nil )
+ @normalized = @unnormalized = @element = nil
+ if first.kind_of? Attribute
+ self.name = first.expanded_name
+ @unnormalized = first.value
+ if second.kind_of? Element
+ @element = second
+ else
+ @element = first.element
+ end
+ elsif first.kind_of? String
+ @element = parent
+ self.name = first
+ @normalized = second.to_s
+ else
+ raise "illegal argument #{first.class.name} to Attribute constructor"
+ end
+ end
+
+ # Returns the namespace of the attribute.
+ #
+ # e = Element.new( "elns:myelement" )
+ # e.add_attribute( "nsa:a", "aval" )
+ # e.add_attribute( "b", "bval" )
+ # e.attributes.get_attribute( "a" ).prefix # -> "nsa"
+ # e.attributes.get_attribute( "b" ).prefix # -> "elns"
+ # a = Attribute.new( "x", "y" )
+ # a.prefix # -> ""
+ def prefix
+ pf = super
+ if pf == ""
+ pf = @element.prefix if @element
+ end
+ pf
+ end
+
+ # Returns the namespace URL, if defined, or nil otherwise
+ #
+ # e = Element.new("el")
+ # e.add_attributes({"xmlns:ns", "http://url"})
+ # e.namespace( "ns" ) # -> "http://url"
+ def namespace arg=nil
+ arg = prefix if arg.nil?
+ @element.namespace arg
+ end
+
+ # Returns true if other is an Attribute and has the same name and value,
+ # false otherwise.
+ def ==( other )
+ other.kind_of?(Attribute) and other.name==name and other.value==value
+ end
+
+ # Creates (and returns) a hash from both the name and value
+ def hash
+ name.hash + value.hash
+ end
+
+ # Returns this attribute out as XML source, expanding the name
+ #
+ # a = Attribute.new( "x", "y" )
+ # a.to_string # -> "x='y'"
+ # b = Attribute.new( "ns:x", "y" )
+ # b.to_string # -> "ns:x='y'"
+ def to_string
+ if @element and @element.context and @element.context[:attribute_quote] == :quote
+ %Q^#@expanded_name="#{to_s().gsub(/"/, '&quote;')}"^
+ else
+ "#@expanded_name='#{to_s().gsub(/'/, '&apos;')}'"
+ end
+ end
+
+ def doctype
+ if @element
+ doc = @element.document
+ doctype = doc.doctype if doc
+ end
+ end
+
+ # Returns the attribute value, with entities replaced
+ def to_s
+ return @normalized if @normalized
+
+ @normalized = Text::normalize( @unnormalized, doctype )
+ @unnormalized = nil
+ @normalized
+ end
+
+ # Returns the UNNORMALIZED value of this attribute. That is, entities
+ # have been expanded to their values
+ def value
+ return @unnormalized if @unnormalized
+ @unnormalized = Text::unnormalize( @normalized, doctype )
+ @normalized = nil
+ @unnormalized
+ end
+
+ # Returns a copy of this attribute
+ def clone
+ Attribute.new self
+ end
+
+ # Sets the element of which this object is an attribute. Normally, this
+ # is not directly called.
+ #
+ # Returns this attribute
+ def element=( element )
+ @element = element
+
+ if @normalized
+ Text.check( @normalized, NEEDS_A_SECOND_CHECK, doctype )
+ end
+
+ self
+ end
+
+ # Removes this Attribute from the tree, and returns true if successfull
+ #
+ # This method is usually not called directly.
+ def remove
+ @element.attributes.delete self.name unless @element.nil?
+ end
+
+ # Writes this attribute (EG, puts 'key="value"' to the output)
+ def write( output, indent=-1 )
+ output << to_string
+ end
+
+ def node_type
+ :attribute
+ end
+
+ def inspect
+ rv = ""
+ write( rv )
+ rv
+ end
+
+ def xpath
+ path = @element.xpath
+ path += "/@#{self.expanded_name}"
+ return path
+ end
+ end
+end
+#vim:ts=2 sw=2 noexpandtab:
diff --git a/ruby/lib/rexml/cdata.rb b/ruby/lib/rexml/cdata.rb
new file mode 100644
index 0000000..123a7c3
--- /dev/null
+++ b/ruby/lib/rexml/cdata.rb
@@ -0,0 +1,67 @@
+require "rexml/text"
+
+module REXML
+ class CData < Text
+ START = '<![CDATA['
+ STOP = ']]>'
+ ILLEGAL = /(\]\]>)/
+
+ # Constructor. CData is data between <![CDATA[ ... ]]>
+ #
+ # _Examples_
+ # CData.new( source )
+ # CData.new( "Here is some CDATA" )
+ # CData.new( "Some unprocessed data", respect_whitespace_TF, parent_element )
+ def initialize( first, whitespace=true, parent=nil )
+ super( first, whitespace, parent, false, true, ILLEGAL )
+ end
+
+ # Make a copy of this object
+ #
+ # _Examples_
+ # c = CData.new( "Some text" )
+ # d = c.clone
+ # d.to_s # -> "Some text"
+ def clone
+ CData.new self
+ end
+
+ # Returns the content of this CData object
+ #
+ # _Examples_
+ # c = CData.new( "Some text" )
+ # c.to_s # -> "Some text"
+ def to_s
+ @string
+ end
+
+ def value
+ @string
+ end
+
+ # == DEPRECATED
+ # See the rexml/formatters package
+ #
+ # Generates XML output of this object
+ #
+ # output::
+ # Where to write the string. Defaults to $stdout
+ # indent::
+ # The amount to indent this node by
+ # transitive::
+ # Ignored
+ # ie_hack::
+ # Ignored
+ #
+ # _Examples_
+ # c = CData.new( " Some text " )
+ # c.write( $stdout ) #-> <![CDATA[ Some text ]]>
+ def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
+ Kernel.warn( "#{self.class.name}.write is deprecated" )
+ indent( output, indent )
+ output << START
+ output << @string
+ output << STOP
+ end
+ end
+end
diff --git a/ruby/lib/rexml/child.rb b/ruby/lib/rexml/child.rb
new file mode 100644
index 0000000..033057d
--- /dev/null
+++ b/ruby/lib/rexml/child.rb
@@ -0,0 +1,96 @@
+require "rexml/node"
+
+module REXML
+ ##
+ # A Child object is something contained by a parent, and this class
+ # contains methods to support that. Most user code will not use this
+ # class directly.
+ class Child
+ include Node
+ attr_reader :parent # The Parent of this object
+
+ # Constructor. Any inheritors of this class should call super to make
+ # sure this method is called.
+ # parent::
+ # if supplied, the parent of this child will be set to the
+ # supplied value, and self will be added to the parent
+ def initialize( parent = nil )
+ @parent = nil
+ # Declare @parent, but don't define it. The next line sets the
+ # parent.
+ parent.add( self ) if parent
+ end
+
+ # Replaces this object with another object. Basically, calls
+ # Parent.replace_child
+ #
+ # Returns:: self
+ def replace_with( child )
+ @parent.replace_child( self, child )
+ self
+ end
+
+ # Removes this child from the parent.
+ #
+ # Returns:: self
+ def remove
+ unless @parent.nil?
+ @parent.delete self
+ end
+ self
+ end
+
+ # Sets the parent of this child to the supplied argument.
+ #
+ # other::
+ # Must be a Parent object. If this object is the same object as the
+ # existing parent of this child, no action is taken. Otherwise, this
+ # child is removed from the current parent (if one exists), and is added
+ # to the new parent.
+ # Returns:: The parent added
+ def parent=( other )
+ return @parent if @parent == other
+ @parent.delete self if defined? @parent and @parent
+ @parent = other
+ end
+
+ alias :next_sibling :next_sibling_node
+ alias :previous_sibling :previous_sibling_node
+
+ # Sets the next sibling of this child. This can be used to insert a child
+ # after some other child.
+ # a = Element.new("a")
+ # b = a.add_element("b")
+ # c = Element.new("c")
+ # b.next_sibling = c
+ # # => <a><b/><c/></a>
+ def next_sibling=( other )
+ parent.insert_after self, other
+ end
+
+ # Sets the previous sibling of this child. This can be used to insert a
+ # child before some other child.
+ # a = Element.new("a")
+ # b = a.add_element("b")
+ # c = Element.new("c")
+ # b.previous_sibling = c
+ # # => <a><b/><c/></a>
+ def previous_sibling=(other)
+ parent.insert_before self, other
+ end
+
+ # Returns:: the document this child belongs to, or nil if this child
+ # belongs to no document
+ def document
+ return parent.document unless parent.nil?
+ nil
+ end
+
+ # This doesn't yet handle encodings
+ def bytes
+ encoding = document.encoding
+
+ to_s
+ end
+ end
+end
diff --git a/ruby/lib/rexml/comment.rb b/ruby/lib/rexml/comment.rb
new file mode 100644
index 0000000..d5be89b
--- /dev/null
+++ b/ruby/lib/rexml/comment.rb
@@ -0,0 +1,80 @@
+require "rexml/child"
+
+module REXML
+ ##
+ # Represents an XML comment; that is, text between \<!-- ... -->
+ class Comment < Child
+ include Comparable
+ START = "<!--"
+ STOP = "-->"
+
+ # The content text
+
+ attr_accessor :string
+
+ ##
+ # Constructor. The first argument can be one of three types:
+ # @param first If String, the contents of this comment are set to the
+ # argument. If Comment, the argument is duplicated. If
+ # Source, the argument is scanned for a comment.
+ # @param second If the first argument is a Source, this argument
+ # should be nil, not supplied, or a Parent to be set as the parent
+ # of this object
+ def initialize( first, second = nil )
+ #puts "IN COMMENT CONSTRUCTOR; SECOND IS #{second.type}"
+ super(second)
+ if first.kind_of? String
+ @string = first
+ elsif first.kind_of? Comment
+ @string = first.string
+ end
+ end
+
+ def clone
+ Comment.new self
+ end
+
+ # == DEPRECATED
+ # See REXML::Formatters
+ #
+ # output::
+ # Where to write the string
+ # indent::
+ # An integer. If -1, no indenting will be used; otherwise, the
+ # indentation will be this number of spaces, and children will be
+ # indented an additional amount.
+ # transitive::
+ # Ignored by this class. The contents of comments are never modified.
+ # ie_hack::
+ # Needed for conformity to the child API, but not used by this class.
+ def write( output, indent=-1, transitive=false, ie_hack=false )
+ Kernel.warn("Comment.write is deprecated. See REXML::Formatters")
+ indent( output, indent )
+ output << START
+ output << @string
+ output << STOP
+ end
+
+ alias :to_s :string
+
+ ##
+ # Compares this Comment to another; the contents of the comment are used
+ # in the comparison.
+ def <=>(other)
+ other.to_s <=> @string
+ end
+
+ ##
+ # Compares this Comment to another; the contents of the comment are used
+ # in the comparison.
+ def ==( other )
+ other.kind_of? Comment and
+ (other <=> self) == 0
+ end
+
+ def node_type
+ :comment
+ end
+ end
+end
+#vim:ts=2 sw=2 noexpandtab:
diff --git a/ruby/lib/rexml/doctype.rb b/ruby/lib/rexml/doctype.rb
new file mode 100644
index 0000000..35beabc
--- /dev/null
+++ b/ruby/lib/rexml/doctype.rb
@@ -0,0 +1,270 @@
+require "rexml/parent"
+require "rexml/parseexception"
+require "rexml/namespace"
+require 'rexml/entity'
+require 'rexml/attlistdecl'
+require 'rexml/xmltokens'
+
+module REXML
+ # Represents an XML DOCTYPE declaration; that is, the contents of <!DOCTYPE
+ # ... >. DOCTYPES can be used to declare the DTD of a document, as well as
+ # being used to declare entities used in the document.
+ class DocType < Parent
+ include XMLTokens
+ START = "<!DOCTYPE"
+ STOP = ">"
+ SYSTEM = "SYSTEM"
+ PUBLIC = "PUBLIC"
+ DEFAULT_ENTITIES = {
+ 'gt'=>EntityConst::GT,
+ 'lt'=>EntityConst::LT,
+ 'quot'=>EntityConst::QUOT,
+ "apos"=>EntityConst::APOS
+ }
+
+ # name is the name of the doctype
+ # external_id is the referenced DTD, if given
+ attr_reader :name, :external_id, :entities, :namespaces
+
+ # Constructor
+ #
+ # dt = DocType.new( 'foo', '-//I/Hate/External/IDs' )
+ # # <!DOCTYPE foo '-//I/Hate/External/IDs'>
+ # dt = DocType.new( doctype_to_clone )
+ # # Incomplete. Shallow clone of doctype
+ #
+ # +Note+ that the constructor:
+ #
+ # Doctype.new( Source.new( "<!DOCTYPE foo 'bar'>" ) )
+ #
+ # is _deprecated_. Do not use it. It will probably disappear.
+ def initialize( first, parent=nil )
+ @entities = DEFAULT_ENTITIES
+ @long_name = @uri = nil
+ if first.kind_of? String
+ super()
+ @name = first
+ @external_id = parent
+ elsif first.kind_of? DocType
+ super( parent )
+ @name = first.name
+ @external_id = first.external_id
+ elsif first.kind_of? Array
+ super( parent )
+ @name = first[0]
+ @external_id = first[1]
+ @long_name = first[2]
+ @uri = first[3]
+ elsif first.kind_of? Source
+ super( parent )
+ parser = Parsers::BaseParser.new( first )
+ event = parser.pull
+ if event[0] == :start_doctype
+ @name, @external_id, @long_name, @uri, = event[1..-1]
+ end
+ else
+ super()
+ end
+ end
+
+ def node_type
+ :doctype
+ end
+
+ def attributes_of element
+ rv = []
+ each do |child|
+ child.each do |key,val|
+ rv << Attribute.new(key,val)
+ end if child.kind_of? AttlistDecl and child.element_name == element
+ end
+ rv
+ end
+
+ def attribute_of element, attribute
+ att_decl = find do |child|
+ child.kind_of? AttlistDecl and
+ child.element_name == element and
+ child.include? attribute
+ end
+ return nil unless att_decl
+ att_decl[attribute]
+ end
+
+ def clone
+ DocType.new self
+ end
+
+ # output::
+ # Where to write the string
+ # indent::
+ # An integer. If -1, no indentation will be used; otherwise, the
+ # indentation will be this number of spaces, and children will be
+ # indented an additional amount.
+ # transitive::
+ # Ignored
+ # ie_hack::
+ # Ignored
+ def write( output, indent=0, transitive=false, ie_hack=false )
+ f = REXML::Formatters::Default.new
+ indent( output, indent )
+ output << START
+ output << ' '
+ output << @name
+ output << " #@external_id" if @external_id
+ output << " #{@long_name.inspect}" if @long_name
+ output << " #{@uri.inspect}" if @uri
+ unless @children.empty?
+ next_indent = indent + 1
+ output << ' ['
+ @children.each { |child|
+ output << "\n"
+ f.write( child, output )
+ }
+ output << "\n]"
+ end
+ output << STOP
+ end
+
+ def context
+ @parent.context
+ end
+
+ def entity( name )
+ @entities[name].unnormalized if @entities[name]
+ end
+
+ def add child
+ super(child)
+ @entities = DEFAULT_ENTITIES.clone if @entities == DEFAULT_ENTITIES
+ @entities[ child.name ] = child if child.kind_of? Entity
+ end
+
+ # This method retrieves the public identifier identifying the document's
+ # DTD.
+ #
+ # Method contributed by Henrik Martensson
+ def public
+ case @external_id
+ when "SYSTEM"
+ nil
+ when "PUBLIC"
+ strip_quotes(@long_name)
+ end
+ end
+
+ # This method retrieves the system identifier identifying the document's DTD
+ #
+ # Method contributed by Henrik Martensson
+ def system
+ case @external_id
+ when "SYSTEM"
+ strip_quotes(@long_name)
+ when "PUBLIC"
+ @uri.kind_of?(String) ? strip_quotes(@uri) : nil
+ end
+ end
+
+ # This method returns a list of notations that have been declared in the
+ # _internal_ DTD subset. Notations in the external DTD subset are not
+ # listed.
+ #
+ # Method contributed by Henrik Martensson
+ def notations
+ children().select {|node| node.kind_of?(REXML::NotationDecl)}
+ end
+
+ # Retrieves a named notation. Only notations declared in the internal
+ # DTD subset can be retrieved.
+ #
+ # Method contributed by Henrik Martensson
+ def notation(name)
+ notations.find { |notation_decl|
+ notation_decl.name == name
+ }
+ end
+
+ private
+
+ # Method contributed by Henrik Martensson
+ def strip_quotes(quoted_string)
+ quoted_string =~ /^[\'\"].*[\'\"]$/ ?
+ quoted_string[1, quoted_string.length-2] :
+ quoted_string
+ end
+ end
+
+ # We don't really handle any of these since we're not a validating
+ # parser, so we can be pretty dumb about them. All we need to be able
+ # to do is spew them back out on a write()
+
+ # This is an abstract class. You never use this directly; it serves as a
+ # parent class for the specific declarations.
+ class Declaration < Child
+ def initialize src
+ super()
+ @string = src
+ end
+
+ def to_s
+ @string+'>'
+ end
+
+ # == DEPRECATED
+ # See REXML::Formatters
+ #
+ def write( output, indent )
+ output << to_s
+ end
+ end
+
+ public
+ class ElementDecl < Declaration
+ def initialize( src )
+ super
+ end
+ end
+
+ class ExternalEntity < Child
+ def initialize( src )
+ super()
+ @entity = src
+ end
+ def to_s
+ @entity
+ end
+ def write( output, indent )
+ output << @entity
+ end
+ end
+
+ class NotationDecl < Child
+ attr_accessor :public, :system
+ def initialize name, middle, pub, sys
+ super(nil)
+ @name = name
+ @middle = middle
+ @public = pub
+ @system = sys
+ end
+
+ def to_s
+ "<!NOTATION #@name #@middle#{
+ @public ? ' ' + public.inspect : ''
+ }#{
+ @system ? ' ' +@system.inspect : ''
+ }>"
+ end
+
+ def write( output, indent=-1 )
+ output << to_s
+ end
+
+ # This method retrieves the name of the notation.
+ #
+ # Method contributed by Henrik Martensson
+ def name
+ @name
+ end
+ end
+end
diff --git a/ruby/lib/rexml/document.rb b/ruby/lib/rexml/document.rb
new file mode 100644
index 0000000..48f1a0e
--- /dev/null
+++ b/ruby/lib/rexml/document.rb
@@ -0,0 +1,231 @@
+require "rexml/element"
+require "rexml/xmldecl"
+require "rexml/source"
+require "rexml/comment"
+require "rexml/doctype"
+require "rexml/instruction"
+require "rexml/rexml"
+require "rexml/parseexception"
+require "rexml/output"
+require "rexml/parsers/baseparser"
+require "rexml/parsers/streamparser"
+require "rexml/parsers/treeparser"
+
+module REXML
+ # Represents a full XML document, including PIs, a doctype, etc. A
+ # Document has a single child that can be accessed by root().
+ # Note that if you want to have an XML declaration written for a document
+ # you create, you must add one; REXML documents do not write a default
+ # declaration for you. See |DECLARATION| and |write|.
+ class Document < Element
+ # A convenient default XML declaration. If you want an XML declaration,
+ # the easiest way to add one is mydoc << Document::DECLARATION
+ # +DEPRECATED+
+ # Use: mydoc << XMLDecl.default
+ DECLARATION = XMLDecl.default
+
+ # Constructor
+ # @param source if supplied, must be a Document, String, or IO.
+ # Documents have their context and Element attributes cloned.
+ # Strings are expected to be valid XML documents. IOs are expected
+ # to be sources of valid XML documents.
+ # @param context if supplied, contains the context of the document;
+ # this should be a Hash.
+ def initialize( source = nil, context = {} )
+ @entity_expansion_count = 0
+ super()
+ @context = context
+ return if source.nil?
+ if source.kind_of? Document
+ @context = source.context
+ super source
+ else
+ build( source )
+ end
+ end
+
+ def node_type
+ :document
+ end
+
+ # Should be obvious
+ def clone
+ Document.new self
+ end
+
+ # According to the XML spec, a root node has no expanded name
+ def expanded_name
+ ''
+ #d = doc_type
+ #d ? d.name : "UNDEFINED"
+ end
+
+ alias :name :expanded_name
+
+ # We override this, because XMLDecls and DocTypes must go at the start
+ # of the document
+ def add( child )
+ if child.kind_of? XMLDecl
+ @children.unshift child
+ child.parent = self
+ elsif child.kind_of? DocType
+ # Find first Element or DocType node and insert the decl right
+ # before it. If there is no such node, just insert the child at the
+ # end. If there is a child and it is an DocType, then replace it.
+ insert_before_index = 0
+ @children.find { |x|
+ insert_before_index += 1
+ x.kind_of?(Element) || x.kind_of?(DocType)
+ }
+ if @children[ insert_before_index ] # Not null = not end of list
+ if @children[ insert_before_index ].kind_of DocType
+ @children[ insert_before_index ] = child
+ else
+ @children[ index_before_index-1, 0 ] = child
+ end
+ else # Insert at end of list
+ @children[insert_before_index] = child
+ end
+ child.parent = self
+ else
+ rv = super
+ raise "attempted adding second root element to document" if @elements.size > 1
+ rv
+ end
+ end
+ alias :<< :add
+
+ def add_element(arg=nil, arg2=nil)
+ rv = super
+ raise "attempted adding second root element to document" if @elements.size > 1
+ rv
+ end
+
+ # @return the root Element of the document, or nil if this document
+ # has no children.
+ def root
+ elements[1]
+ #self
+ #@children.find { |item| item.kind_of? Element }
+ end
+
+ # @return the DocType child of the document, if one exists,
+ # and nil otherwise.
+ def doctype
+ @children.find { |item| item.kind_of? DocType }
+ end
+
+ # @return the XMLDecl of this document; if no XMLDecl has been
+ # set, the default declaration is returned.
+ def xml_decl
+ rv = @children[0]
+ return rv if rv.kind_of? XMLDecl
+ rv = @children.unshift(XMLDecl.default)[0]
+ end
+
+ # @return the XMLDecl version of this document as a String.
+ # If no XMLDecl has been set, returns the default version.
+ def version
+ xml_decl().version
+ end
+
+ # @return the XMLDecl encoding of this document as a String.
+ # If no XMLDecl has been set, returns the default encoding.
+ def encoding
+ xml_decl().encoding
+ end
+
+ # @return the XMLDecl standalone value of this document as a String.
+ # If no XMLDecl has been set, returns the default setting.
+ def stand_alone?
+ xml_decl().stand_alone?
+ end
+
+ # Write the XML tree out, optionally with indent. This writes out the
+ # entire XML document, including XML declarations, doctype declarations,
+ # and processing instructions (if any are given).
+ #
+ # A controversial point is whether Document should always write the XML
+ # declaration (<?xml version='1.0'?>) whether or not one is given by the
+ # user (or source document). REXML does not write one if one was not
+ # specified, because it adds unnecessary bandwidth to applications such
+ # as XML-RPC.
+ #
+ # See also the classes in the rexml/formatters package for the proper way
+ # to change the default formatting of XML output
+ #
+ # _Examples_
+ # Document.new("<a><b/></a>").serialize
+ #
+ # output_string = ""
+ # tr = Transitive.new( output_string )
+ # Document.new("<a><b/></a>").serialize( tr )
+ #
+ # output::
+ # output an object which supports '<< string'; this is where the
+ # document will be written.
+ # indent::
+ # An integer. If -1, no indenting will be used; otherwise, the
+ # indentation will be twice this number of spaces, and children will be
+ # indented an additional amount. For a value of 3, every item will be
+ # indented 3 more levels, or 6 more spaces (2 * 3). Defaults to -1
+ # transitive::
+ # If transitive is true and indent is >= 0, then the output will be
+ # pretty-printed in such a way that the added whitespace does not affect
+ # the absolute *value* of the document -- that is, it leaves the value
+ # and number of Text nodes in the document unchanged.
+ # ie_hack::
+ # Internet Explorer is the worst piece of crap to have ever been
+ # written, with the possible exception of Windows itself. Since IE is
+ # unable to parse proper XML, we have to provide a hack to generate XML
+ # that IE's limited abilities can handle. This hack inserts a space
+ # before the /> on empty tags. Defaults to false
+ def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
+ if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
+ output = Output.new( output, xml_decl.encoding )
+ end
+ formatter = if indent > -1
+ if transitive
+ require "rexml/formatters/transitive"
+ REXML::Formatters::Transitive.new( indent, ie_hack )
+ else
+ REXML::Formatters::Pretty.new( indent, ie_hack )
+ end
+ else
+ REXML::Formatters::Default.new( ie_hack )
+ end
+ formatter.write( self, output )
+ end
+
+
+ def Document::parse_stream( source, listener )
+ Parsers::StreamParser.new( source, listener ).parse
+ end
+
+ @@entity_expansion_limit = 10_000
+
+ # Set the entity expansion limit. By default the limit is set to 10000.
+ def Document::entity_expansion_limit=( val )
+ @@entity_expansion_limit = val
+ end
+
+ # Get the entity expansion limit. By default the limit is set to 10000.
+ def Document::entity_expansion_limit
+ return @@entity_expansion_limit
+ end
+
+ attr_reader :entity_expansion_count
+
+ def record_entity_expansion
+ @entity_expansion_count += 1
+ if @entity_expansion_count > @@entity_expansion_limit
+ raise "number of entity expansions exceeded, processing aborted."
+ end
+ end
+
+ private
+ def build( source )
+ Parsers::TreeParser.new( source, self ).parse
+ end
+ end
+end
diff --git a/ruby/lib/rexml/dtd/attlistdecl.rb b/ruby/lib/rexml/dtd/attlistdecl.rb
new file mode 100644
index 0000000..25955ee
--- /dev/null
+++ b/ruby/lib/rexml/dtd/attlistdecl.rb
@@ -0,0 +1,10 @@
+require "rexml/child"
+module REXML
+ module DTD
+ class AttlistDecl < Child
+ START = "<!ATTLIST"
+ START_RE = /^\s*#{START}/um
+ PATTERN_RE = /\s*(#{START}.*?>)/um
+ end
+ end
+end
diff --git a/ruby/lib/rexml/dtd/dtd.rb b/ruby/lib/rexml/dtd/dtd.rb
new file mode 100644
index 0000000..966e39e
--- /dev/null
+++ b/ruby/lib/rexml/dtd/dtd.rb
@@ -0,0 +1,51 @@
+require "rexml/dtd/elementdecl"
+require "rexml/dtd/entitydecl"
+require "rexml/comment"
+require "rexml/dtd/notationdecl"
+require "rexml/dtd/attlistdecl"
+require "rexml/parent"
+
+module REXML
+ module DTD
+ class Parser
+ def Parser.parse( input )
+ case input
+ when String
+ parse_helper input
+ when File
+ parse_helper input.read
+ end
+ end
+
+ # Takes a String and parses it out
+ def Parser.parse_helper( input )
+ contents = Parent.new
+ while input.size > 0
+ case input
+ when ElementDecl.PATTERN_RE
+ match = $&
+ source = $'
+ contents << ElementDecl.new( match )
+ when AttlistDecl.PATTERN_RE
+ matchdata = $~
+ source = $'
+ contents << AttlistDecl.new( matchdata )
+ when EntityDecl.PATTERN_RE
+ matchdata = $~
+ source = $'
+ contents << EntityDecl.new( matchdata )
+ when Comment.PATTERN_RE
+ matchdata = $~
+ source = $'
+ contents << Comment.new( matchdata )
+ when NotationDecl.PATTERN_RE
+ matchdata = $~
+ source = $'
+ contents << NotationDecl.new( matchdata )
+ end
+ end
+ contents
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/dtd/elementdecl.rb b/ruby/lib/rexml/dtd/elementdecl.rb
new file mode 100644
index 0000000..a0bf641
--- /dev/null
+++ b/ruby/lib/rexml/dtd/elementdecl.rb
@@ -0,0 +1,17 @@
+require "rexml/child"
+module REXML
+ module DTD
+ class ElementDecl < Child
+ START = "<!ELEMENT"
+ START_RE = /^\s*#{START}/um
+ PATTERN_RE = /^\s*(#{START}.*?)>/um
+ PATTERN_RE = /^\s*#{START}\s+((?:[:\w_][-\.\w_]*:)?[-!\*\.\w_]*)(.*?)>/
+ #\s*((((["']).*?\5)|[^\/'">]*)*?)(\/)?>/um, true)
+
+ def initialize match
+ @name = match[1]
+ @rest = match[2]
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/dtd/entitydecl.rb b/ruby/lib/rexml/dtd/entitydecl.rb
new file mode 100644
index 0000000..0adda6f
--- /dev/null
+++ b/ruby/lib/rexml/dtd/entitydecl.rb
@@ -0,0 +1,56 @@
+require "rexml/child"
+module REXML
+ module DTD
+ class EntityDecl < Child
+ START = "<!ENTITY"
+ START_RE = /^\s*#{START}/um
+ PUBLIC = /^\s*#{START}\s+(?:%\s+)?(\w+)\s+PUBLIC\s+((["']).*?\3)\s+((["']).*?\5)\s*>/um
+ SYSTEM = /^\s*#{START}\s+(?:%\s+)?(\w+)\s+SYSTEM\s+((["']).*?\3)(?:\s+NDATA\s+\w+)?\s*>/um
+ PLAIN = /^\s*#{START}\s+(\w+)\s+((["']).*?\3)\s*>/um
+ PERCENT = /^\s*#{START}\s+%\s+(\w+)\s+((["']).*?\3)\s*>/um
+ # <!ENTITY name SYSTEM "...">
+ # <!ENTITY name "...">
+ def initialize src
+ super()
+ md = nil
+ if src.match( PUBLIC )
+ md = src.match( PUBLIC, true )
+ @middle = "PUBLIC"
+ @content = "#{md[2]} #{md[4]}"
+ elsif src.match( SYSTEM )
+ md = src.match( SYSTEM, true )
+ @middle = "SYSTEM"
+ @content = md[2]
+ elsif src.match( PLAIN )
+ md = src.match( PLAIN, true )
+ @middle = ""
+ @content = md[2]
+ elsif src.match( PERCENT )
+ md = src.match( PERCENT, true )
+ @middle = ""
+ @content = md[2]
+ end
+ raise ParseException.new("failed Entity match", src) if md.nil?
+ @name = md[1]
+ end
+
+ def to_s
+ rv = "<!ENTITY #@name "
+ rv << "#@middle " if @middle.size > 0
+ rv << @content
+ rv
+ end
+
+ def write( output, indent )
+ indent( output, indent )
+ output << to_s
+ end
+
+ def EntityDecl.parse_source source, listener
+ md = source.match( PATTERN_RE, true )
+ thing = md[0].squeeze(" \t\n\r")
+ listener.send inspect.downcase, thing
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/dtd/notationdecl.rb b/ruby/lib/rexml/dtd/notationdecl.rb
new file mode 100644
index 0000000..eae71f2
--- /dev/null
+++ b/ruby/lib/rexml/dtd/notationdecl.rb
@@ -0,0 +1,39 @@
+require "rexml/child"
+module REXML
+ module DTD
+ class NotationDecl < Child
+ START = "<!NOTATION"
+ START_RE = /^\s*#{START}/um
+ PUBLIC = /^\s*#{START}\s+(\w[\w-]*)\s+(PUBLIC)\s+((["']).*?\4)\s*>/um
+ SYSTEM = /^\s*#{START}\s+(\w[\w-]*)\s+(SYSTEM)\s+((["']).*?\4)\s*>/um
+ def initialize src
+ super()
+ if src.match( PUBLIC )
+ md = src.match( PUBLIC, true )
+ elsif src.match( SYSTEM )
+ md = src.match( SYSTEM, true )
+ else
+ raise ParseException.new( "error parsing notation: no matching pattern", src )
+ end
+ @name = md[1]
+ @middle = md[2]
+ @rest = md[3]
+ end
+
+ def to_s
+ "<!NOTATION #@name #@middle #@rest>"
+ end
+
+ def write( output, indent )
+ indent( output, indent )
+ output << to_s
+ end
+
+ def NotationDecl.parse_source source, listener
+ md = source.match( PATTERN_RE, true )
+ thing = md[0].squeeze(" \t\n\r")
+ listener.send inspect.downcase, thing
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/element.rb b/ruby/lib/rexml/element.rb
new file mode 100644
index 0000000..92308a5
--- /dev/null
+++ b/ruby/lib/rexml/element.rb
@@ -0,0 +1,1246 @@
+require "rexml/parent"
+require "rexml/namespace"
+require "rexml/attribute"
+require "rexml/cdata"
+require "rexml/xpath"
+require "rexml/parseexception"
+
+module REXML
+ # An implementation note about namespaces:
+ # As we parse, when we find namespaces we put them in a hash and assign
+ # them a unique ID. We then convert the namespace prefix for the node
+ # to the unique ID. This makes namespace lookup much faster for the
+ # cost of extra memory use. We save the namespace prefix for the
+ # context node and convert it back when we write it.
+ @@namespaces = {}
+
+ # Represents a tagged XML element. Elements are characterized by
+ # having children, attributes, and names, and can themselves be
+ # children.
+ class Element < Parent
+ include Namespace
+
+ UNDEFINED = "UNDEFINED"; # The default name
+
+ # Mechanisms for accessing attributes and child elements of this
+ # element.
+ attr_reader :attributes, :elements
+ # The context holds information about the processing environment, such as
+ # whitespace handling.
+ attr_accessor :context
+
+ # Constructor
+ # arg::
+ # if not supplied, will be set to the default value.
+ # If a String, the name of this object will be set to the argument.
+ # If an Element, the object will be shallowly cloned; name,
+ # attributes, and namespaces will be copied. Children will +not+ be
+ # copied.
+ # parent::
+ # if supplied, must be a Parent, and will be used as
+ # the parent of this object.
+ # context::
+ # If supplied, must be a hash containing context items. Context items
+ # include:
+ # * <tt>:respect_whitespace</tt> the value of this is :+all+ or an array of
+ # strings being the names of the elements to respect
+ # whitespace for. Defaults to :+all+.
+ # * <tt>:compress_whitespace</tt> the value can be :+all+ or an array of
+ # strings being the names of the elements to ignore whitespace on.
+ # Overrides :+respect_whitespace+.
+ # * <tt>:ignore_whitespace_nodes</tt> the value can be :+all+ or an array
+ # of strings being the names of the elements in which to ignore
+ # whitespace-only nodes. If this is set, Text nodes which contain only
+ # whitespace will not be added to the document tree.
+ # * <tt>:raw</tt> can be :+all+, or an array of strings being the names of
+ # the elements to process in raw mode. In raw mode, special
+ # characters in text is not converted to or from entities.
+ def initialize( arg = UNDEFINED, parent=nil, context=nil )
+ super(parent)
+
+ @elements = Elements.new(self)
+ @attributes = Attributes.new(self)
+ @context = context
+
+ if arg.kind_of? String
+ self.name = arg
+ elsif arg.kind_of? Element
+ self.name = arg.expanded_name
+ arg.attributes.each_attribute{ |attribute|
+ @attributes << Attribute.new( attribute )
+ }
+ @context = arg.context
+ end
+ end
+
+ def inspect
+ rv = "<#@expanded_name"
+
+ @attributes.each_attribute do |attr|
+ rv << " "
+ attr.write( rv, 0 )
+ end
+
+ if children.size > 0
+ rv << "> ... </>"
+ else
+ rv << "/>"
+ end
+ end
+
+
+ # Creates a shallow copy of self.
+ # d = Document.new "<a><b/><b/><c><d/></c></a>"
+ # new_a = d.root.clone
+ # puts new_a # => "<a/>"
+ def clone
+ self.class.new self
+ end
+
+ # Evaluates to the root node of the document that this element
+ # belongs to. If this element doesn't belong to a document, but does
+ # belong to another Element, the parent's root will be returned, until the
+ # earliest ancestor is found.
+ #
+ # Note that this is not the same as the document element.
+ # In the following example, <a> is the document element, and the root
+ # node is the parent node of the document element. You may ask yourself
+ # why the root node is useful: consider the doctype and XML declaration,
+ # and any processing instructions before the document element... they
+ # are children of the root node, or siblings of the document element.
+ # The only time this isn't true is when an Element is created that is
+ # not part of any Document. In this case, the ancestor that has no
+ # parent acts as the root node.
+ # d = Document.new '<a><b><c/></b></a>'
+ # a = d[1] ; c = a[1][1]
+ # d.root_node == d # TRUE
+ # a.root_node # namely, d
+ # c.root_node # again, d
+ def root_node
+ parent.nil? ? self : parent.root_node
+ end
+
+ def root
+ return elements[1] if self.kind_of? Document
+ return self if parent.kind_of? Document or parent.nil?
+ return parent.root
+ end
+
+ # Evaluates to the document to which this element belongs, or nil if this
+ # element doesn't belong to a document.
+ def document
+ rt = root
+ rt.parent if rt
+ end
+
+ # Evaluates to +true+ if whitespace is respected for this element. This
+ # is the case if:
+ # 1. Neither :+respect_whitespace+ nor :+compress_whitespace+ has any value
+ # 2. The context has :+respect_whitespace+ set to :+all+ or
+ # an array containing the name of this element, and
+ # :+compress_whitespace+ isn't set to :+all+ or an array containing the
+ # name of this element.
+ # The evaluation is tested against +expanded_name+, and so is namespace
+ # sensitive.
+ def whitespace
+ @whitespace = nil
+ if @context
+ if @context[:respect_whitespace]
+ @whitespace = (@context[:respect_whitespace] == :all or
+ @context[:respect_whitespace].include? expanded_name)
+ end
+ @whitespace = false if (@context[:compress_whitespace] and
+ (@context[:compress_whitespace] == :all or
+ @context[:compress_whitespace].include? expanded_name)
+ )
+ end
+ @whitespace = true unless @whitespace == false
+ @whitespace
+ end
+
+ def ignore_whitespace_nodes
+ @ignore_whitespace_nodes = false
+ if @context
+ if @context[:ignore_whitespace_nodes]
+ @ignore_whitespace_nodes =
+ (@context[:ignore_whitespace_nodes] == :all or
+ @context[:ignore_whitespace_nodes].include? expanded_name)
+ end
+ end
+ end
+
+ # Evaluates to +true+ if raw mode is set for this element. This
+ # is the case if the context has :+raw+ set to :+all+ or
+ # an array containing the name of this element.
+ #
+ # The evaluation is tested against +expanded_name+, and so is namespace
+ # sensitive.
+ def raw
+ @raw = (@context and @context[:raw] and
+ (@context[:raw] == :all or
+ @context[:raw].include? expanded_name))
+ @raw
+ end
+
+ #once :whitespace, :raw, :ignore_whitespace_nodes
+
+ #################################################
+ # Namespaces #
+ #################################################
+
+ # Evaluates to an +Array+ containing the prefixes (names) of all defined
+ # namespaces at this context node.
+ # doc = Document.new("<a xmlns:x='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
+ # doc.elements['//b'].prefixes # -> ['x', 'y']
+ def prefixes
+ prefixes = []
+ prefixes = parent.prefixes if parent
+ prefixes |= attributes.prefixes
+ return prefixes
+ end
+
+ def namespaces
+ namespaces = {}
+ namespaces = parent.namespaces if parent
+ namespaces = namespaces.merge( attributes.namespaces )
+ return namespaces
+ end
+
+ # Evalutas to the URI for a prefix, or the empty string if no such
+ # namespace is declared for this element. Evaluates recursively for
+ # ancestors. Returns the default namespace, if there is one.
+ # prefix::
+ # the prefix to search for. If not supplied, returns the default
+ # namespace if one exists
+ # Returns::
+ # the namespace URI as a String, or nil if no such namespace
+ # exists. If the namespace is undefined, returns an empty string
+ # doc = Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
+ # b = doc.elements['//b']
+ # b.namespace # -> '1'
+ # b.namespace("y") # -> '2'
+ def namespace(prefix=nil)
+ if prefix.nil?
+ prefix = prefix()
+ end
+ if prefix == ''
+ prefix = "xmlns"
+ else
+ prefix = "xmlns:#{prefix}" unless prefix[0,5] == 'xmlns'
+ end
+ ns = attributes[ prefix ]
+ ns = parent.namespace(prefix) if ns.nil? and parent
+ ns = '' if ns.nil? and prefix == 'xmlns'
+ return ns
+ end
+
+ # Adds a namespace to this element.
+ # prefix::
+ # the prefix string, or the namespace URI if +uri+ is not
+ # supplied
+ # uri::
+ # the namespace URI. May be nil, in which +prefix+ is used as
+ # the URI
+ # Evaluates to: this Element
+ # a = Element.new("a")
+ # a.add_namespace("xmlns:foo", "bar" )
+ # a.add_namespace("foo", "bar") # shorthand for previous line
+ # a.add_namespace("twiddle")
+ # puts a #-> <a xmlns:foo='bar' xmlns='twiddle'/>
+ def add_namespace( prefix, uri=nil )
+ unless uri
+ @attributes["xmlns"] = prefix
+ else
+ prefix = "xmlns:#{prefix}" unless prefix =~ /^xmlns:/
+ @attributes[ prefix ] = uri
+ end
+ self
+ end
+
+ # Removes a namespace from this node. This only works if the namespace is
+ # actually declared in this node. If no argument is passed, deletes the
+ # default namespace.
+ #
+ # Evaluates to: this element
+ # doc = Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
+ # doc.root.delete_namespace
+ # puts doc # -> <a xmlns:foo='bar'/>
+ # doc.root.delete_namespace 'foo'
+ # puts doc # -> <a/>
+ def delete_namespace namespace="xmlns"
+ namespace = "xmlns:#{namespace}" unless namespace == 'xmlns'
+ attribute = attributes.get_attribute(namespace)
+ attribute.remove unless attribute.nil?
+ self
+ end
+
+ #################################################
+ # Elements #
+ #################################################
+
+ # Adds a child to this element, optionally setting attributes in
+ # the element.
+ # element::
+ # optional. If Element, the element is added.
+ # Otherwise, a new Element is constructed with the argument (see
+ # Element.initialize).
+ # attrs::
+ # If supplied, must be a Hash containing String name,value
+ # pairs, which will be used to set the attributes of the new Element.
+ # Returns:: the Element that was added
+ # el = doc.add_element 'my-tag'
+ # el = doc.add_element 'my-tag', {'attr1'=>'val1', 'attr2'=>'val2'}
+ # el = Element.new 'my-tag'
+ # doc.add_element el
+ def add_element element, attrs=nil
+ raise "First argument must be either an element name, or an Element object" if element.nil?
+ el = @elements.add(element)
+ attrs.each do |key, value|
+ el.attributes[key]=value
+ end if attrs.kind_of? Hash
+ el
+ end
+
+ # Deletes a child element.
+ # element::
+ # Must be an +Element+, +String+, or +Integer+. If Element,
+ # the element is removed. If String, the element is found (via XPath)
+ # and removed. <em>This means that any parent can remove any
+ # descendant.<em> If Integer, the Element indexed by that number will be
+ # removed.
+ # Returns:: the element that was removed.
+ # doc.delete_element "/a/b/c[@id='4']"
+ # doc.delete_element doc.elements["//k"]
+ # doc.delete_element 1
+ def delete_element element
+ @elements.delete element
+ end
+
+ # Evaluates to +true+ if this element has at least one child Element
+ # doc = Document.new "<a><b/><c>Text</c></a>"
+ # doc.root.has_elements # -> true
+ # doc.elements["/a/b"].has_elements # -> false
+ # doc.elements["/a/c"].has_elements # -> false
+ def has_elements?
+ !@elements.empty?
+ end
+
+ # Iterates through the child elements, yielding for each Element that
+ # has a particular attribute set.
+ # key::
+ # the name of the attribute to search for
+ # value::
+ # the value of the attribute
+ # max::
+ # (optional) causes this method to return after yielding
+ # for this number of matching children
+ # name::
+ # (optional) if supplied, this is an XPath that filters
+ # the children to check.
+ #
+ # doc = Document.new "<a><b @id='1'/><c @id='2'/><d @id='1'/><e/></a>"
+ # # Yields b, c, d
+ # doc.root.each_element_with_attribute( 'id' ) {|e| p e}
+ # # Yields b, d
+ # doc.root.each_element_with_attribute( 'id', '1' ) {|e| p e}
+ # # Yields b
+ # doc.root.each_element_with_attribute( 'id', '1', 1 ) {|e| p e}
+ # # Yields d
+ # doc.root.each_element_with_attribute( 'id', '1', 0, 'd' ) {|e| p e}
+ def each_element_with_attribute( key, value=nil, max=0, name=nil, &block ) # :yields: Element
+ each_with_something( proc {|child|
+ if value.nil?
+ child.attributes[key] != nil
+ else
+ child.attributes[key]==value
+ end
+ }, max, name, &block )
+ end
+
+ # Iterates through the children, yielding for each Element that
+ # has a particular text set.
+ # text::
+ # the text to search for. If nil, or not supplied, will iterate
+ # over all +Element+ children that contain at least one +Text+ node.
+ # max::
+ # (optional) causes this method to return after yielding
+ # for this number of matching children
+ # name::
+ # (optional) if supplied, this is an XPath that filters
+ # the children to check.
+ #
+ # doc = Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
+ # # Yields b, c, d
+ # doc.each_element_with_text {|e|p e}
+ # # Yields b, c
+ # doc.each_element_with_text('b'){|e|p e}
+ # # Yields b
+ # doc.each_element_with_text('b', 1){|e|p e}
+ # # Yields d
+ # doc.each_element_with_text(nil, 0, 'd'){|e|p e}
+ def each_element_with_text( text=nil, max=0, name=nil, &block ) # :yields: Element
+ each_with_something( proc {|child|
+ if text.nil?
+ child.has_text?
+ else
+ child.text == text
+ end
+ }, max, name, &block )
+ end
+
+ # Synonym for Element.elements.each
+ def each_element( xpath=nil, &block ) # :yields: Element
+ @elements.each( xpath, &block )
+ end
+
+ # Synonym for Element.to_a
+ # This is a little slower than calling elements.each directly.
+ # xpath:: any XPath by which to search for elements in the tree
+ # Returns:: an array of Elements that match the supplied path
+ def get_elements( xpath )
+ @elements.to_a( xpath )
+ end
+
+ # Returns the next sibling that is an element, or nil if there is
+ # no Element sibling after this one
+ # doc = Document.new '<a><b/>text<c/></a>'
+ # doc.root.elements['b'].next_element #-> <c/>
+ # doc.root.elements['c'].next_element #-> nil
+ def next_element
+ element = next_sibling
+ element = element.next_sibling until element.nil? or element.kind_of? Element
+ return element
+ end
+
+ # Returns the previous sibling that is an element, or nil if there is
+ # no Element sibling prior to this one
+ # doc = Document.new '<a><b/>text<c/></a>'
+ # doc.root.elements['c'].previous_element #-> <b/>
+ # doc.root.elements['b'].previous_element #-> nil
+ def previous_element
+ element = previous_sibling
+ element = element.previous_sibling until element.nil? or element.kind_of? Element
+ return element
+ end
+
+
+ #################################################
+ # Text #
+ #################################################
+
+ # Evaluates to +true+ if this element has at least one Text child
+ def has_text?
+ not text().nil?
+ end
+
+ # A convenience method which returns the String value of the _first_
+ # child text element, if one exists, and +nil+ otherwise.
+ #
+ # <em>Note that an element may have multiple Text elements, perhaps
+ # separated by other children</em>. Be aware that this method only returns
+ # the first Text node.
+ #
+ # This method returns the +value+ of the first text child node, which
+ # ignores the +raw+ setting, so always returns normalized text. See
+ # the Text::value documentation.
+ #
+ # doc = Document.new "<p>some text <b>this is bold!</b> more text</p>"
+ # # The element 'p' has two text elements, "some text " and " more text".
+ # doc.root.text #-> "some text "
+ def text( path = nil )
+ rv = get_text(path)
+ return rv.value unless rv.nil?
+ nil
+ end
+
+ # Returns the first child Text node, if any, or +nil+ otherwise.
+ # This method returns the actual +Text+ node, rather than the String content.
+ # doc = Document.new "<p>some text <b>this is bold!</b> more text</p>"
+ # # The element 'p' has two text elements, "some text " and " more text".
+ # doc.root.get_text.value #-> "some text "
+ def get_text path = nil
+ rv = nil
+ if path
+ element = @elements[ path ]
+ rv = element.get_text unless element.nil?
+ else
+ rv = @children.find { |node| node.kind_of? Text }
+ end
+ return rv
+ end
+
+ # Sets the first Text child of this object. See text() for a
+ # discussion about Text children.
+ #
+ # If a Text child already exists, the child is replaced by this
+ # content. This means that Text content can be deleted by calling
+ # this method with a nil argument. In this case, the next Text
+ # child becomes the first Text child. In no case is the order of
+ # any siblings disturbed.
+ # text::
+ # If a String, a new Text child is created and added to
+ # this Element as the first Text child. If Text, the text is set
+ # as the first Child element. If nil, then any existing first Text
+ # child is removed.
+ # Returns:: this Element.
+ # doc = Document.new '<a><b/></a>'
+ # doc.root.text = 'Sean' #-> '<a><b/>Sean</a>'
+ # doc.root.text = 'Elliott' #-> '<a><b/>Elliott</a>'
+ # doc.root.add_element 'c' #-> '<a><b/>Elliott<c/></a>'
+ # doc.root.text = 'Russell' #-> '<a><b/>Russell<c/></a>'
+ # doc.root.text = nil #-> '<a><b/><c/></a>'
+ def text=( text )
+ if text.kind_of? String
+ text = Text.new( text, whitespace(), nil, raw() )
+ elsif text and !text.kind_of? Text
+ text = Text.new( text.to_s, whitespace(), nil, raw() )
+ end
+ old_text = get_text
+ if text.nil?
+ old_text.remove unless old_text.nil?
+ else
+ if old_text.nil?
+ self << text
+ else
+ old_text.replace_with( text )
+ end
+ end
+ return self
+ end
+
+ # A helper method to add a Text child. Actual Text instances can
+ # be added with regular Parent methods, such as add() and <<()
+ # text::
+ # if a String, a new Text instance is created and added
+ # to the parent. If Text, the object is added directly.
+ # Returns:: this Element
+ # e = Element.new('a') #-> <e/>
+ # e.add_text 'foo' #-> <e>foo</e>
+ # e.add_text Text.new(' bar') #-> <e>foo bar</e>
+ # Note that at the end of this example, the branch has <b>3</b> nodes; the 'e'
+ # element and <b>2</b> Text node children.
+ def add_text( text )
+ if text.kind_of? String
+ if @children[-1].kind_of? Text
+ @children[-1] << text
+ return
+ end
+ text = Text.new( text, whitespace(), nil, raw() )
+ end
+ self << text unless text.nil?
+ return self
+ end
+
+ def node_type
+ :element
+ end
+
+ def xpath
+ path_elements = []
+ cur = self
+ path_elements << __to_xpath_helper( self )
+ while cur.parent
+ cur = cur.parent
+ path_elements << __to_xpath_helper( cur )
+ end
+ return path_elements.reverse.join( "/" )
+ end
+
+ #################################################
+ # Attributes #
+ #################################################
+
+ def attribute( name, namespace=nil )
+ prefix = nil
+ if namespaces.respond_to? :key
+ prefix = namespaces.key(namespace) if namespace
+ else
+ prefix = namespaces.index(namespace) if namespace
+ end
+ prefix = nil if prefix == 'xmlns'
+
+ ret_val =
+ attributes.get_attribute( "#{prefix ? prefix + ':' : ''}#{name}" )
+
+ return ret_val unless ret_val.nil?
+ return nil if prefix.nil?
+
+ # now check that prefix'es namespace is not the same as the
+ # default namespace
+ return nil unless ( namespaces[ prefix ] == namespaces[ 'xmlns' ] )
+
+ attributes.get_attribute( name )
+
+ end
+
+ # Evaluates to +true+ if this element has any attributes set, false
+ # otherwise.
+ def has_attributes?
+ return !@attributes.empty?
+ end
+
+ # Adds an attribute to this element, overwriting any existing attribute
+ # by the same name.
+ # key::
+ # can be either an Attribute or a String. If an Attribute,
+ # the attribute is added to the list of Element attributes. If String,
+ # the argument is used as the name of the new attribute, and the value
+ # parameter must be supplied.
+ # value::
+ # Required if +key+ is a String, and ignored if the first argument is
+ # an Attribute. This is a String, and is used as the value
+ # of the new Attribute. This should be the unnormalized value of the
+ # attribute (without entities).
+ # Returns:: the Attribute added
+ # e = Element.new 'e'
+ # e.add_attribute( 'a', 'b' ) #-> <e a='b'/>
+ # e.add_attribute( 'x:a', 'c' ) #-> <e a='b' x:a='c'/>
+ # e.add_attribute Attribute.new('b', 'd') #-> <e a='b' x:a='c' b='d'/>
+ def add_attribute( key, value=nil )
+ if key.kind_of? Attribute
+ @attributes << key
+ else
+ @attributes[key] = value
+ end
+ end
+
+ # Add multiple attributes to this element.
+ # hash:: is either a hash, or array of arrays
+ # el.add_attributes( {"name1"=>"value1", "name2"=>"value2"} )
+ # el.add_attributes( [ ["name1","value1"], ["name2"=>"value2"] ] )
+ def add_attributes hash
+ if hash.kind_of? Hash
+ hash.each_pair {|key, value| @attributes[key] = value }
+ elsif hash.kind_of? Array
+ hash.each { |value| @attributes[ value[0] ] = value[1] }
+ end
+ end
+
+ # Removes an attribute
+ # key::
+ # either an Attribute or a String. In either case, the
+ # attribute is found by matching the attribute name to the argument,
+ # and then removed. If no attribute is found, no action is taken.
+ # Returns::
+ # the attribute removed, or nil if this Element did not contain
+ # a matching attribute
+ # e = Element.new('E')
+ # e.add_attribute( 'name', 'Sean' ) #-> <E name='Sean'/>
+ # r = e.add_attribute( 'sur:name', 'Russell' ) #-> <E name='Sean' sur:name='Russell'/>
+ # e.delete_attribute( 'name' ) #-> <E sur:name='Russell'/>
+ # e.delete_attribute( r ) #-> <E/>
+ def delete_attribute(key)
+ attr = @attributes.get_attribute(key)
+ attr.remove unless attr.nil?
+ end
+
+ #################################################
+ # Other Utilities #
+ #################################################
+
+ # Get an array of all CData children.
+ # IMMUTABLE
+ def cdatas
+ find_all { |child| child.kind_of? CData }.freeze
+ end
+
+ # Get an array of all Comment children.
+ # IMMUTABLE
+ def comments
+ find_all { |child| child.kind_of? Comment }.freeze
+ end
+
+ # Get an array of all Instruction children.
+ # IMMUTABLE
+ def instructions
+ find_all { |child| child.kind_of? Instruction }.freeze
+ end
+
+ # Get an array of all Text children.
+ # IMMUTABLE
+ def texts
+ find_all { |child| child.kind_of? Text }.freeze
+ end
+
+ # == DEPRECATED
+ # See REXML::Formatters
+ #
+ # Writes out this element, and recursively, all children.
+ # output::
+ # output an object which supports '<< string'; this is where the
+ # document will be written.
+ # indent::
+ # An integer. If -1, no indenting will be used; otherwise, the
+ # indentation will be this number of spaces, and children will be
+ # indented an additional amount. Defaults to -1
+ # transitive::
+ # If transitive is true and indent is >= 0, then the output will be
+ # pretty-printed in such a way that the added whitespace does not affect
+ # the parse tree of the document
+ # ie_hack::
+ # Internet Explorer is the worst piece of crap to have ever been
+ # written, with the possible exception of Windows itself. Since IE is
+ # unable to parse proper XML, we have to provide a hack to generate XML
+ # that IE's limited abilities can handle. This hack inserts a space
+ # before the /> on empty tags. Defaults to false
+ #
+ # out = ''
+ # doc.write( out ) #-> doc is written to the string 'out'
+ # doc.write( $stdout ) #-> doc written to the console
+ def write(output=$stdout, indent=-1, transitive=false, ie_hack=false)
+ Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters")
+ formatter = if indent > -1
+ if transitive
+ require "rexml/formatters/transitive"
+ REXML::Formatters::Transitive.new( indent, ie_hack )
+ else
+ REXML::Formatters::Pretty.new( indent, ie_hack )
+ end
+ else
+ REXML::Formatters::Default.new( ie_hack )
+ end
+ formatter.write( self, output )
+ end
+
+
+ private
+ def __to_xpath_helper node
+ rv = node.expanded_name.clone
+ if node.parent
+ results = node.parent.find_all {|n|
+ n.kind_of?(REXML::Element) and n.expanded_name == node.expanded_name
+ }
+ if results.length > 1
+ idx = results.index( node )
+ rv << "[#{idx+1}]"
+ end
+ end
+ rv
+ end
+
+ # A private helper method
+ def each_with_something( test, max=0, name=nil )
+ num = 0
+ @elements.each( name ){ |child|
+ yield child if test.call(child) and num += 1
+ return if max>0 and num == max
+ }
+ end
+ end
+
+ ########################################################################
+ # ELEMENTS #
+ ########################################################################
+
+ # A class which provides filtering of children for Elements, and
+ # XPath search support. You are expected to only encounter this class as
+ # the <tt>element.elements</tt> object. Therefore, you are
+ # _not_ expected to instantiate this yourself.
+ class Elements
+ include Enumerable
+ # Constructor
+ # parent:: the parent Element
+ def initialize parent
+ @element = parent
+ end
+
+ # Fetches a child element. Filters only Element children, regardless of
+ # the XPath match.
+ # index::
+ # the search parameter. This is either an Integer, which
+ # will be used to find the index'th child Element, or an XPath,
+ # which will be used to search for the Element. <em>Because
+ # of the nature of XPath searches, any element in the connected XML
+ # document can be fetched through any other element.</em> <b>The
+ # Integer index is 1-based, not 0-based.</b> This means that the first
+ # child element is at index 1, not 0, and the +n+th element is at index
+ # +n+, not <tt>n-1</tt>. This is because XPath indexes element children
+ # starting from 1, not 0, and the indexes should be the same.
+ # name::
+ # optional, and only used in the first argument is an
+ # Integer. In that case, the index'th child Element that has the
+ # supplied name will be returned. Note again that the indexes start at 1.
+ # Returns:: the first matching Element, or nil if no child matched
+ # doc = Document.new '<a><b/><c id="1"/><c id="2"/><d/></a>'
+ # doc.root.elements[1] #-> <b/>
+ # doc.root.elements['c'] #-> <c id="1"/>
+ # doc.root.elements[2,'c'] #-> <c id="2"/>
+ def []( index, name=nil)
+ if index.kind_of? Integer
+ raise "index (#{index}) must be >= 1" if index < 1
+ name = literalize(name) if name
+ num = 0
+ @element.find { |child|
+ child.kind_of? Element and
+ (name.nil? ? true : child.has_name?( name )) and
+ (num += 1) == index
+ }
+ else
+ return XPath::first( @element, index )
+ #{ |element|
+ # return element if element.kind_of? Element
+ #}
+ #return nil
+ end
+ end
+
+ # Sets an element, replacing any previous matching element. If no
+ # existing element is found ,the element is added.
+ # index:: Used to find a matching element to replace. See []().
+ # element::
+ # The element to replace the existing element with
+ # the previous element
+ # Returns:: nil if no previous element was found.
+ #
+ # doc = Document.new '<a/>'
+ # doc.root.elements[10] = Element.new('b') #-> <a><b/></a>
+ # doc.root.elements[1] #-> <b/>
+ # doc.root.elements[1] = Element.new('c') #-> <a><c/></a>
+ # doc.root.elements['c'] = Element.new('d') #-> <a><d/></a>
+ def []=( index, element )
+ previous = self[index]
+ if previous.nil?
+ @element.add element
+ else
+ previous.replace_with element
+ end
+ return previous
+ end
+
+ # Returns +true+ if there are no +Element+ children, +false+ otherwise
+ def empty?
+ @element.find{ |child| child.kind_of? Element}.nil?
+ end
+
+ # Returns the index of the supplied child (starting at 1), or -1 if
+ # the element is not a child
+ # element:: an +Element+ child
+ def index element
+ rv = 0
+ found = @element.find do |child|
+ child.kind_of? Element and
+ (rv += 1) and
+ child == element
+ end
+ return rv if found == element
+ return -1
+ end
+
+ # Deletes a child Element
+ # element::
+ # Either an Element, which is removed directly; an
+ # xpath, where the first matching child is removed; or an Integer,
+ # where the n'th Element is removed.
+ # Returns:: the removed child
+ # doc = Document.new '<a><b/><c/><c id="1"/></a>'
+ # b = doc.root.elements[1]
+ # doc.root.elements.delete b #-> <a><c/><c id="1"/></a>
+ # doc.elements.delete("a/c[@id='1']") #-> <a><c/></a>
+ # doc.root.elements.delete 1 #-> <a/>
+ def delete element
+ if element.kind_of? Element
+ @element.delete element
+ else
+ el = self[element]
+ el.remove if el
+ end
+ end
+
+ # Removes multiple elements. Filters for Element children, regardless of
+ # XPath matching.
+ # xpath:: all elements matching this String path are removed.
+ # Returns:: an Array of Elements that have been removed
+ # doc = Document.new '<a><c/><c/><c/><c/></a>'
+ # deleted = doc.elements.delete_all 'a/c' #-> [<c/>, <c/>, <c/>, <c/>]
+ def delete_all( xpath )
+ rv = []
+ XPath::each( @element, xpath) {|element|
+ rv << element if element.kind_of? Element
+ }
+ rv.each do |element|
+ @element.delete element
+ element.remove
+ end
+ return rv
+ end
+
+ # Adds an element
+ # element::
+ # if supplied, is either an Element, String, or
+ # Source (see Element.initialize). If not supplied or nil, a
+ # new, default Element will be constructed
+ # Returns:: the added Element
+ # a = Element.new('a')
+ # a.elements.add(Element.new('b')) #-> <a><b/></a>
+ # a.elements.add('c') #-> <a><b/><c/></a>
+ def add element=nil
+ rv = nil
+ if element.nil?
+ Element.new("", self, @element.context)
+ elsif not element.kind_of?(Element)
+ Element.new(element, self, @element.context)
+ else
+ @element << element
+ element.context = @element.context
+ element
+ end
+ end
+
+ alias :<< :add
+
+ # Iterates through all of the child Elements, optionally filtering
+ # them by a given XPath
+ # xpath::
+ # optional. If supplied, this is a String XPath, and is used to
+ # filter the children, so that only matching children are yielded. Note
+ # that XPaths are automatically filtered for Elements, so that
+ # non-Element children will not be yielded
+ # doc = Document.new '<a><b/><c/><d/>sean<b/><c/><d/></a>'
+ # doc.root.each {|e|p e} #-> Yields b, c, d, b, c, d elements
+ # doc.root.each('b') {|e|p e} #-> Yields b, b elements
+ # doc.root.each('child::node()') {|e|p e}
+ # #-> Yields <b/>, <c/>, <d/>, <b/>, <c/>, <d/>
+ # XPath.each(doc.root, 'child::node()', &block)
+ # #-> Yields <b/>, <c/>, <d/>, sean, <b/>, <c/>, <d/>
+ def each( xpath=nil, &block)
+ XPath::each( @element, xpath ) {|e| yield e if e.kind_of? Element }
+ end
+
+ def collect( xpath=nil, &block )
+ collection = []
+ XPath::each( @element, xpath ) {|e|
+ collection << yield(e) if e.kind_of?(Element)
+ }
+ collection
+ end
+
+ def inject( xpath=nil, initial=nil, &block )
+ first = true
+ XPath::each( @element, xpath ) {|e|
+ if (e.kind_of? Element)
+ if (first and initial == nil)
+ initial = e
+ first = false
+ else
+ initial = yield( initial, e ) if e.kind_of? Element
+ end
+ end
+ }
+ initial
+ end
+
+ # Returns the number of +Element+ children of the parent object.
+ # doc = Document.new '<a>sean<b/>elliott<b/>russell<b/></a>'
+ # doc.root.size #-> 6, 3 element and 3 text nodes
+ # doc.root.elements.size #-> 3
+ def size
+ count = 0
+ @element.each {|child| count+=1 if child.kind_of? Element }
+ count
+ end
+
+ # Returns an Array of Element children. An XPath may be supplied to
+ # filter the children. Only Element children are returned, even if the
+ # supplied XPath matches non-Element children.
+ # doc = Document.new '<a>sean<b/>elliott<c/></a>'
+ # doc.root.elements.to_a #-> [ <b/>, <c/> ]
+ # doc.root.elements.to_a("child::node()") #-> [ <b/>, <c/> ]
+ # XPath.match(doc.root, "child::node()") #-> [ sean, <b/>, elliott, <c/> ]
+ def to_a( xpath=nil )
+ rv = XPath.match( @element, xpath )
+ return rv.find_all{|e| e.kind_of? Element} if xpath
+ rv
+ end
+
+ private
+ # Private helper class. Removes quotes from quoted strings
+ def literalize name
+ name = name[1..-2] if name[0] == ?' or name[0] == ?" #'
+ name
+ end
+ end
+
+ ########################################################################
+ # ATTRIBUTES #
+ ########################################################################
+
+ # A class that defines the set of Attributes of an Element and provides
+ # operations for accessing elements in that set.
+ class Attributes < Hash
+ # Constructor
+ # element:: the Element of which this is an Attribute
+ def initialize element
+ @element = element
+ end
+
+ # Fetches an attribute value. If you want to get the Attribute itself,
+ # use get_attribute()
+ # name:: an XPath attribute name. Namespaces are relevant here.
+ # Returns::
+ # the String value of the matching attribute, or +nil+ if no
+ # matching attribute was found. This is the unnormalized value
+ # (with entities expanded).
+ #
+ # doc = Document.new "<a foo:att='1' bar:att='2' att='&lt;'/>"
+ # doc.root.attributes['att'] #-> '<'
+ # doc.root.attributes['bar:att'] #-> '2'
+ def [](name)
+ attr = get_attribute(name)
+ return attr.value unless attr.nil?
+ return nil
+ end
+
+ def to_a
+ values.flatten
+ end
+
+ # Returns the number of attributes the owning Element contains.
+ # doc = Document "<a x='1' y='2' foo:x='3'/>"
+ # doc.root.attributes.length #-> 3
+ def length
+ c = 0
+ each_attribute { c+=1 }
+ c
+ end
+ alias :size :length
+
+ # Iterates over the attributes of an Element. Yields actual Attribute
+ # nodes, not String values.
+ #
+ # doc = Document.new '<a x="1" y="2"/>'
+ # doc.root.attributes.each_attribute {|attr|
+ # p attr.expanded_name+" => "+attr.value
+ # }
+ def each_attribute # :yields: attribute
+ each_value do |val|
+ if val.kind_of? Attribute
+ yield val
+ else
+ val.each_value { |atr| yield atr }
+ end
+ end
+ end
+
+ # Iterates over each attribute of an Element, yielding the expanded name
+ # and value as a pair of Strings.
+ #
+ # doc = Document.new '<a x="1" y="2"/>'
+ # doc.root.attributes.each {|name, value| p name+" => "+value }
+ def each
+ each_attribute do |attr|
+ yield [attr.expanded_name, attr.value]
+ end
+ end
+
+ # Fetches an attribute
+ # name::
+ # the name by which to search for the attribute. Can be a
+ # <tt>prefix:name</tt> namespace name.
+ # Returns:: The first matching attribute, or nil if there was none. This
+ # value is an Attribute node, not the String value of the attribute.
+ # doc = Document.new '<a x:foo="1" foo="2" bar="3"/>'
+ # doc.root.attributes.get_attribute("foo").value #-> "2"
+ # doc.root.attributes.get_attribute("x:foo").value #-> "1"
+ def get_attribute( name )
+ attr = fetch( name, nil )
+ if attr.nil?
+ return nil if name.nil?
+ # Look for prefix
+ name =~ Namespace::NAMESPLIT
+ prefix, n = $1, $2
+ if prefix
+ attr = fetch( n, nil )
+ # check prefix
+ if attr == nil
+ elsif attr.kind_of? Attribute
+ return attr if prefix == attr.prefix
+ else
+ attr = attr[ prefix ]
+ return attr
+ end
+ end
+ element_document = @element.document
+ if element_document and element_document.doctype
+ expn = @element.expanded_name
+ expn = element_document.doctype.name if expn.size == 0
+ attr_val = element_document.doctype.attribute_of(expn, name)
+ return Attribute.new( name, attr_val ) if attr_val
+ end
+ return nil
+ end
+ if attr.kind_of? Hash
+ attr = attr[ @element.prefix ]
+ end
+ return attr
+ end
+
+ # Sets an attribute, overwriting any existing attribute value by the
+ # same name. Namespace is significant.
+ # name:: the name of the attribute
+ # value::
+ # (optional) If supplied, the value of the attribute. If
+ # nil, any existing matching attribute is deleted.
+ # Returns::
+ # Owning element
+ # doc = Document.new "<a x:foo='1' foo='3'/>"
+ # doc.root.attributes['y:foo'] = '2'
+ # doc.root.attributes['foo'] = '4'
+ # doc.root.attributes['x:foo'] = nil
+ def []=( name, value )
+ if value.nil? # Delete the named attribute
+ attr = get_attribute(name)
+ delete attr
+ return
+ end
+ element_document = @element.document
+ unless value.kind_of? Attribute
+ if @element.document and @element.document.doctype
+ value = Text::normalize( value, @element.document.doctype )
+ else
+ value = Text::normalize( value, nil )
+ end
+ value = Attribute.new(name, value)
+ end
+ value.element = @element
+ old_attr = fetch(value.name, nil)
+ if old_attr.nil?
+ store(value.name, value)
+ elsif old_attr.kind_of? Hash
+ old_attr[value.prefix] = value
+ elsif old_attr.prefix != value.prefix
+ # Check for conflicting namespaces
+ raise ParseException.new(
+ "Namespace conflict in adding attribute \"#{value.name}\": "+
+ "Prefix \"#{old_attr.prefix}\" = "+
+ "\"#{@element.namespace(old_attr.prefix)}\" and prefix "+
+ "\"#{value.prefix}\" = \"#{@element.namespace(value.prefix)}\"") if
+ value.prefix != "xmlns" and old_attr.prefix != "xmlns" and
+ @element.namespace( old_attr.prefix ) ==
+ @element.namespace( value.prefix )
+ store value.name, { old_attr.prefix => old_attr,
+ value.prefix => value }
+ else
+ store value.name, value
+ end
+ return @element
+ end
+
+ # Returns an array of Strings containing all of the prefixes declared
+ # by this set of # attributes. The array does not include the default
+ # namespace declaration, if one exists.
+ # doc = Document.new("<a xmlns='foo' xmlns:x='bar' xmlns:y='twee' "+
+ # "z='glorp' p:k='gru'/>")
+ # prefixes = doc.root.attributes.prefixes #-> ['x', 'y']
+ def prefixes
+ ns = []
+ each_attribute do |attribute|
+ ns << attribute.name if attribute.prefix == 'xmlns'
+ end
+ if @element.document and @element.document.doctype
+ expn = @element.expanded_name
+ expn = @element.document.doctype.name if expn.size == 0
+ @element.document.doctype.attributes_of(expn).each {
+ |attribute|
+ ns << attribute.name if attribute.prefix == 'xmlns'
+ }
+ end
+ ns
+ end
+
+ def namespaces
+ namespaces = {}
+ each_attribute do |attribute|
+ namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
+ end
+ if @element.document and @element.document.doctype
+ expn = @element.expanded_name
+ expn = @element.document.doctype.name if expn.size == 0
+ @element.document.doctype.attributes_of(expn).each {
+ |attribute|
+ namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
+ }
+ end
+ namespaces
+ end
+
+ # Removes an attribute
+ # attribute::
+ # either a String, which is the name of the attribute to remove --
+ # namespaces are significant here -- or the attribute to remove.
+ # Returns:: the owning element
+ # doc = Document.new "<a y:foo='0' x:foo='1' foo='3' z:foo='4'/>"
+ # doc.root.attributes.delete 'foo' #-> <a y:foo='0' x:foo='1' z:foo='4'/>"
+ # doc.root.attributes.delete 'x:foo' #-> <a y:foo='0' z:foo='4'/>"
+ # attr = doc.root.attributes.get_attribute('y:foo')
+ # doc.root.attributes.delete attr #-> <a z:foo='4'/>"
+ def delete( attribute )
+ name = nil
+ prefix = nil
+ if attribute.kind_of? Attribute
+ name = attribute.name
+ prefix = attribute.prefix
+ else
+ attribute =~ Namespace::NAMESPLIT
+ prefix, name = $1, $2
+ prefix = '' unless prefix
+ end
+ old = fetch(name, nil)
+ attr = nil
+ if old.kind_of? Hash # the supplied attribute is one of many
+ attr = old.delete(prefix)
+ if old.size == 1
+ repl = nil
+ old.each_value{|v| repl = v}
+ store name, repl
+ end
+ elsif old.nil?
+ return @element
+ else # the supplied attribute is a top-level one
+ attr = old
+ res = super(name)
+ end
+ @element
+ end
+
+ # Adds an attribute, overriding any existing attribute by the
+ # same name. Namespaces are significant.
+ # attribute:: An Attribute
+ def add( attribute )
+ self[attribute.name] = attribute
+ end
+
+ alias :<< :add
+
+ # Deletes all attributes matching a name. Namespaces are significant.
+ # name::
+ # A String; all attributes that match this path will be removed
+ # Returns:: an Array of the Attributes that were removed
+ def delete_all( name )
+ rv = []
+ each_attribute { |attribute|
+ rv << attribute if attribute.expanded_name == name
+ }
+ rv.each{ |attr| attr.remove }
+ return rv
+ end
+
+ # The +get_attribute_ns+ method retrieves a method by its namespace
+ # and name. Thus it is possible to reliably identify an attribute
+ # even if an XML processor has changed the prefix.
+ #
+ # Method contributed by Henrik Martensson
+ def get_attribute_ns(namespace, name)
+ result = nil
+ each_attribute() { |attribute|
+ if name == attribute.name &&
+ namespace == attribute.namespace() &&
+ ( !namespace.empty? || !attribute.fully_expanded_name.index(':') )
+ # foo will match xmlns:foo, but only if foo isn't also an attribute
+ result = attribute if !result or !namespace.empty? or
+ !attribute.fully_expanded_name.index(':')
+ end
+ }
+ result
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encoding.rb b/ruby/lib/rexml/encoding.rb
new file mode 100644
index 0000000..608c69c
--- /dev/null
+++ b/ruby/lib/rexml/encoding.rb
@@ -0,0 +1,71 @@
+# -*- mode: ruby; ruby-indent-level: 2; indent-tabs-mode: t; tab-width: 2 -*- vim: sw=2 ts=2
+module REXML
+ module Encoding
+ @encoding_methods = {}
+ def self.register(enc, &block)
+ @encoding_methods[enc] = block
+ end
+ def self.apply(obj, enc)
+ @encoding_methods[enc][obj]
+ end
+ def self.encoding_method(enc)
+ @encoding_methods[enc]
+ end
+
+ # Native, default format is UTF-8, so it is declared here rather than in
+ # an encodings/ definition.
+ UTF_8 = 'UTF-8'
+ UTF_16 = 'UTF-16'
+ UNILE = 'UNILE'
+
+ # ID ---> Encoding name
+ attr_reader :encoding
+ def encoding=( enc )
+ old_verbosity = $VERBOSE
+ begin
+ $VERBOSE = false
+ enc = enc.nil? ? nil : enc.upcase
+ return false if defined? @encoding and enc == @encoding
+ if enc and enc != UTF_8
+ @encoding = enc
+ raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/
+ @encoding.untaint
+ begin
+ require 'rexml/encodings/ICONV.rb'
+ Encoding.apply(self, "ICONV")
+ rescue LoadError, Exception
+ begin
+ enc_file = File.join( "rexml", "encodings", "#@encoding.rb" )
+ require enc_file
+ Encoding.apply(self, @encoding)
+ rescue LoadError => err
+ puts err.message
+ raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv."
+ end
+ end
+ else
+ @encoding = UTF_8
+ require 'rexml/encodings/UTF-8.rb'
+ Encoding.apply(self, @encoding)
+ end
+ ensure
+ $VERBOSE = old_verbosity
+ end
+ true
+ end
+
+ def check_encoding str
+ # We have to recognize UTF-16, LSB UTF-16, and UTF-8
+ if str[0,2] == "\xfe\xff"
+ str[0,2] = ""
+ return UTF_16
+ elsif str[0,2] == "\xff\xfe"
+ str[0,2] = ""
+ return UNILE
+ end
+ str =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/m
+ return $3.upcase if $3
+ return UTF_8
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encodings/CP-1252.rb b/ruby/lib/rexml/encodings/CP-1252.rb
new file mode 100644
index 0000000..2ef6a1a
--- /dev/null
+++ b/ruby/lib/rexml/encodings/CP-1252.rb
@@ -0,0 +1,103 @@
+#
+# This class was contributed by Mikko Tiihonen mikko DOT tiihonen AT hut DOT fi
+#
+module REXML
+ module Encoding
+ register( "CP-1252" ) do |o|
+ class << o
+ alias encode encode_cp1252
+ alias decode decode_cp1252
+ end
+ end
+
+ # Convert from UTF-8
+ def encode_cp1252(content)
+ array_utf8 = content.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ case num
+ # shortcut first bunch basic characters
+ when 0..0xFF; array_enc << num
+ # characters added compared to iso-8859-1
+ when 0x20AC; array_enc << 0x80 # 0xe2 0x82 0xac
+ when 0x201A; array_enc << 0x82 # 0xe2 0x82 0x9a
+ when 0x0192; array_enc << 0x83 # 0xc6 0x92
+ when 0x201E; array_enc << 0x84 # 0xe2 0x82 0x9e
+ when 0x2026; array_enc << 0x85 # 0xe2 0x80 0xa6
+ when 0x2020; array_enc << 0x86 # 0xe2 0x80 0xa0
+ when 0x2021; array_enc << 0x87 # 0xe2 0x80 0xa1
+ when 0x02C6; array_enc << 0x88 # 0xcb 0x86
+ when 0x2030; array_enc << 0x89 # 0xe2 0x80 0xb0
+ when 0x0160; array_enc << 0x8A # 0xc5 0xa0
+ when 0x2039; array_enc << 0x8B # 0xe2 0x80 0xb9
+ when 0x0152; array_enc << 0x8C # 0xc5 0x92
+ when 0x017D; array_enc << 0x8E # 0xc5 0xbd
+ when 0x2018; array_enc << 0x91 # 0xe2 0x80 0x98
+ when 0x2019; array_enc << 0x92 # 0xe2 0x80 0x99
+ when 0x201C; array_enc << 0x93 # 0xe2 0x80 0x9c
+ when 0x201D; array_enc << 0x94 # 0xe2 0x80 0x9d
+ when 0x2022; array_enc << 0x95 # 0xe2 0x80 0xa2
+ when 0x2013; array_enc << 0x96 # 0xe2 0x80 0x93
+ when 0x2014; array_enc << 0x97 # 0xe2 0x80 0x94
+ when 0x02DC; array_enc << 0x98 # 0xcb 0x9c
+ when 0x2122; array_enc << 0x99 # 0xe2 0x84 0xa2
+ when 0x0161; array_enc << 0x9A # 0xc5 0xa1
+ when 0x203A; array_enc << 0x9B # 0xe2 0x80 0xba
+ when 0x0152; array_enc << 0x9C # 0xc5 0x93
+ when 0x017E; array_enc << 0x9E # 0xc5 0xbe
+ when 0x0178; array_enc << 0x9F # 0xc5 0xb8
+ else
+ # all remaining basic characters can be used directly
+ if num <= 0xFF
+ array_enc << num
+ else
+ # Numeric entity (&#nnnn;); shard by Stefan Scholl
+ array_enc.concat "&\##{num};".unpack('C*')
+ end
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ # Convert to UTF-8
+ def decode_cp1252(str)
+ array_latin9 = str.unpack('C*')
+ array_enc = []
+ array_latin9.each do |num|
+ case num
+ # characters that added compared to iso-8859-1
+ when 0x80; array_enc << 0x20AC # 0xe2 0x82 0xac
+ when 0x82; array_enc << 0x201A # 0xe2 0x82 0x9a
+ when 0x83; array_enc << 0x0192 # 0xc6 0x92
+ when 0x84; array_enc << 0x201E # 0xe2 0x82 0x9e
+ when 0x85; array_enc << 0x2026 # 0xe2 0x80 0xa6
+ when 0x86; array_enc << 0x2020 # 0xe2 0x80 0xa0
+ when 0x87; array_enc << 0x2021 # 0xe2 0x80 0xa1
+ when 0x88; array_enc << 0x02C6 # 0xcb 0x86
+ when 0x89; array_enc << 0x2030 # 0xe2 0x80 0xb0
+ when 0x8A; array_enc << 0x0160 # 0xc5 0xa0
+ when 0x8B; array_enc << 0x2039 # 0xe2 0x80 0xb9
+ when 0x8C; array_enc << 0x0152 # 0xc5 0x92
+ when 0x8E; array_enc << 0x017D # 0xc5 0xbd
+ when 0x91; array_enc << 0x2018 # 0xe2 0x80 0x98
+ when 0x92; array_enc << 0x2019 # 0xe2 0x80 0x99
+ when 0x93; array_enc << 0x201C # 0xe2 0x80 0x9c
+ when 0x94; array_enc << 0x201D # 0xe2 0x80 0x9d
+ when 0x95; array_enc << 0x2022 # 0xe2 0x80 0xa2
+ when 0x96; array_enc << 0x2013 # 0xe2 0x80 0x93
+ when 0x97; array_enc << 0x2014 # 0xe2 0x80 0x94
+ when 0x98; array_enc << 0x02DC # 0xcb 0x9c
+ when 0x99; array_enc << 0x2122 # 0xe2 0x84 0xa2
+ when 0x9A; array_enc << 0x0161 # 0xc5 0xa1
+ when 0x9B; array_enc << 0x203A # 0xe2 0x80 0xba
+ when 0x9C; array_enc << 0x0152 # 0xc5 0x93
+ when 0x9E; array_enc << 0x017E # 0xc5 0xbe
+ when 0x9F; array_enc << 0x0178 # 0xc5 0xb8
+ else
+ array_enc << num
+ end
+ end
+ array_enc.pack('U*')
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encodings/EUC-JP.rb b/ruby/lib/rexml/encodings/EUC-JP.rb
new file mode 100644
index 0000000..db37b6b
--- /dev/null
+++ b/ruby/lib/rexml/encodings/EUC-JP.rb
@@ -0,0 +1,35 @@
+module REXML
+ module Encoding
+ begin
+ require 'uconv'
+
+ def decode_eucjp(str)
+ Uconv::euctou8(str)
+ end
+
+ def encode_eucjp content
+ Uconv::u8toeuc(content)
+ end
+ rescue LoadError
+ require 'nkf'
+
+ EUCTOU8 = '-Ewm0'
+ U8TOEUC = '-Wem0'
+
+ def decode_eucjp(str)
+ NKF.nkf(EUCTOU8, str)
+ end
+
+ def encode_eucjp content
+ NKF.nkf(U8TOEUC, content)
+ end
+ end
+
+ register("EUC-JP") do |obj|
+ class << obj
+ alias decode decode_eucjp
+ alias encode encode_eucjp
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encodings/ICONV.rb b/ruby/lib/rexml/encodings/ICONV.rb
new file mode 100644
index 0000000..172fba7
--- /dev/null
+++ b/ruby/lib/rexml/encodings/ICONV.rb
@@ -0,0 +1,22 @@
+require "iconv"
+raise LoadError unless defined? Iconv
+
+module REXML
+ module Encoding
+ def decode_iconv(str)
+ Iconv.conv(UTF_8, @encoding, str)
+ end
+
+ def encode_iconv(content)
+ Iconv.conv(@encoding, UTF_8, content)
+ end
+
+ register("ICONV") do |obj|
+ Iconv.conv(UTF_8, obj.encoding, nil)
+ class << obj
+ alias decode decode_iconv
+ alias encode encode_iconv
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encodings/ISO-8859-1.rb b/ruby/lib/rexml/encodings/ISO-8859-1.rb
new file mode 100644
index 0000000..2873d13
--- /dev/null
+++ b/ruby/lib/rexml/encodings/ISO-8859-1.rb
@@ -0,0 +1,7 @@
+require 'rexml/encodings/US-ASCII'
+
+module REXML
+ module Encoding
+ register("ISO-8859-1", &encoding_method("US-ASCII"))
+ end
+end
diff --git a/ruby/lib/rexml/encodings/ISO-8859-15.rb b/ruby/lib/rexml/encodings/ISO-8859-15.rb
new file mode 100644
index 0000000..9532672
--- /dev/null
+++ b/ruby/lib/rexml/encodings/ISO-8859-15.rb
@@ -0,0 +1,72 @@
+#
+# This class was contributed by Mikko Tiihonen mikko DOT tiihonen AT hut DOT fi
+#
+module REXML
+ module Encoding
+ register("ISO-8859-15") do |o|
+ alias encode to_iso_8859_15
+ alias decode from_iso_8859_15
+ end
+
+ # Convert from UTF-8
+ def to_iso_8859_15(content)
+ array_utf8 = content.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ case num
+ # shortcut first bunch basic characters
+ when 0..0xA3; array_enc << num
+ # characters removed compared to iso-8859-1
+ when 0xA4; array_enc << '&#164;'
+ when 0xA6; array_enc << '&#166;'
+ when 0xA8; array_enc << '&#168;'
+ when 0xB4; array_enc << '&#180;'
+ when 0xB8; array_enc << '&#184;'
+ when 0xBC; array_enc << '&#188;'
+ when 0xBD; array_enc << '&#189;'
+ when 0xBE; array_enc << '&#190;'
+ # characters added compared to iso-8859-1
+ when 0x20AC; array_enc << 0xA4 # 0xe2 0x82 0xac
+ when 0x0160; array_enc << 0xA6 # 0xc5 0xa0
+ when 0x0161; array_enc << 0xA8 # 0xc5 0xa1
+ when 0x017D; array_enc << 0xB4 # 0xc5 0xbd
+ when 0x017E; array_enc << 0xB8 # 0xc5 0xbe
+ when 0x0152; array_enc << 0xBC # 0xc5 0x92
+ when 0x0153; array_enc << 0xBD # 0xc5 0x93
+ when 0x0178; array_enc << 0xBE # 0xc5 0xb8
+ else
+ # all remaining basic characters can be used directly
+ if num <= 0xFF
+ array_enc << num
+ else
+ # Numeric entity (&#nnnn;); shard by Stefan Scholl
+ array_enc.concat "&\##{num};".unpack('C*')
+ end
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ # Convert to UTF-8
+ def from_iso_8859_15(str)
+ array_latin9 = str.unpack('C*')
+ array_enc = []
+ array_latin9.each do |num|
+ case num
+ # characters that differ compared to iso-8859-1
+ when 0xA4; array_enc << 0x20AC
+ when 0xA6; array_enc << 0x0160
+ when 0xA8; array_enc << 0x0161
+ when 0xB4; array_enc << 0x017D
+ when 0xB8; array_enc << 0x017E
+ when 0xBC; array_enc << 0x0152
+ when 0xBD; array_enc << 0x0153
+ when 0xBE; array_enc << 0x0178
+ else
+ array_enc << num
+ end
+ end
+ array_enc.pack('U*')
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encodings/SHIFT-JIS.rb b/ruby/lib/rexml/encodings/SHIFT-JIS.rb
new file mode 100644
index 0000000..9e0f4af
--- /dev/null
+++ b/ruby/lib/rexml/encodings/SHIFT-JIS.rb
@@ -0,0 +1,37 @@
+module REXML
+ module Encoding
+ begin
+ require 'uconv'
+
+ def decode_sjis content
+ Uconv::sjistou8(content)
+ end
+
+ def encode_sjis(str)
+ Uconv::u8tosjis(str)
+ end
+ rescue LoadError
+ require 'nkf'
+
+ SJISTOU8 = '-Swm0x'
+ U8TOSJIS = '-Wsm0x'
+
+ def decode_sjis(str)
+ NKF.nkf(SJISTOU8, str)
+ end
+
+ def encode_sjis content
+ NKF.nkf(U8TOSJIS, content)
+ end
+ end
+
+ b = proc do |obj|
+ class << obj
+ alias decode decode_sjis
+ alias encode encode_sjis
+ end
+ end
+ register("SHIFT-JIS", &b)
+ register("SHIFT_JIS", &b)
+ end
+end
diff --git a/ruby/lib/rexml/encodings/SHIFT_JIS.rb b/ruby/lib/rexml/encodings/SHIFT_JIS.rb
new file mode 100644
index 0000000..e355704
--- /dev/null
+++ b/ruby/lib/rexml/encodings/SHIFT_JIS.rb
@@ -0,0 +1 @@
+require 'rexml/encodings/SHIFT-JIS'
diff --git a/ruby/lib/rexml/encodings/UNILE.rb b/ruby/lib/rexml/encodings/UNILE.rb
new file mode 100644
index 0000000..d054140
--- /dev/null
+++ b/ruby/lib/rexml/encodings/UNILE.rb
@@ -0,0 +1,34 @@
+module REXML
+ module Encoding
+ def encode_unile content
+ array_utf8 = content.unpack("U*")
+ array_enc = []
+ array_utf8.each do |num|
+ if ((num>>16) > 0)
+ array_enc << ??
+ array_enc << 0
+ else
+ array_enc << (num & 0xFF)
+ array_enc << (num >> 8)
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ def decode_unile(str)
+ array_enc=str.unpack('C*')
+ array_utf8 = []
+ 0.step(array_enc.size-1, 2){|i|
+ array_utf8 << (array_enc.at(i) + array_enc.at(i+1)*0x100)
+ }
+ array_utf8.pack('U*')
+ end
+
+ register(UNILE) do |obj|
+ class << obj
+ alias decode decode_unile
+ alias encode encode_unile
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encodings/US-ASCII.rb b/ruby/lib/rexml/encodings/US-ASCII.rb
new file mode 100644
index 0000000..fb4c217
--- /dev/null
+++ b/ruby/lib/rexml/encodings/US-ASCII.rb
@@ -0,0 +1,30 @@
+module REXML
+ module Encoding
+ # Convert from UTF-8
+ def encode_ascii content
+ array_utf8 = content.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ if num <= 0x7F
+ array_enc << num
+ else
+ # Numeric entity (&#nnnn;); shard by Stefan Scholl
+ array_enc.concat "&\##{num};".unpack('C*')
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ # Convert to UTF-8
+ def decode_ascii(str)
+ str.unpack('C*').pack('U*')
+ end
+
+ register("US-ASCII") do |obj|
+ class << obj
+ alias decode decode_ascii
+ alias encode encode_ascii
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encodings/UTF-16.rb b/ruby/lib/rexml/encodings/UTF-16.rb
new file mode 100644
index 0000000..007c493
--- /dev/null
+++ b/ruby/lib/rexml/encodings/UTF-16.rb
@@ -0,0 +1,35 @@
+module REXML
+ module Encoding
+ def encode_utf16 content
+ array_utf8 = content.unpack("U*")
+ array_enc = []
+ array_utf8.each do |num|
+ if ((num>>16) > 0)
+ array_enc << 0
+ array_enc << ??
+ else
+ array_enc << (num >> 8)
+ array_enc << (num & 0xFF)
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ def decode_utf16(str)
+ str = str[2..-1] if /^\376\377/n =~ str
+ array_enc=str.unpack('C*')
+ array_utf8 = []
+ 0.step(array_enc.size-1, 2){|i|
+ array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100)
+ }
+ array_utf8.pack('U*')
+ end
+
+ register(UTF_16) do |obj|
+ class << obj
+ alias decode decode_utf16
+ alias encode encode_utf16
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/encodings/UTF-8.rb b/ruby/lib/rexml/encodings/UTF-8.rb
new file mode 100644
index 0000000..bb08f44
--- /dev/null
+++ b/ruby/lib/rexml/encodings/UTF-8.rb
@@ -0,0 +1,18 @@
+module REXML
+ module Encoding
+ def encode_utf8 content
+ content
+ end
+
+ def decode_utf8(str)
+ str
+ end
+
+ register(UTF_8) do |obj|
+ class << obj
+ alias decode decode_utf8
+ alias encode encode_utf8
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/entity.rb b/ruby/lib/rexml/entity.rb
new file mode 100644
index 0000000..d2f27ec
--- /dev/null
+++ b/ruby/lib/rexml/entity.rb
@@ -0,0 +1,166 @@
+require 'rexml/child'
+require 'rexml/source'
+require 'rexml/xmltokens'
+
+module REXML
+ # God, I hate DTDs. I really do. Why this idiot standard still
+ # plagues us is beyond me.
+ class Entity < Child
+ include XMLTokens
+ PUBIDCHAR = "\x20\x0D\x0Aa-zA-Z0-9\\-()+,./:=?;!*@$_%#"
+ SYSTEMLITERAL = %Q{((?:"[^"]*")|(?:'[^']*'))}
+ PUBIDLITERAL = %Q{("[#{PUBIDCHAR}']*"|'[#{PUBIDCHAR}]*')}
+ EXTERNALID = "(?:(?:(SYSTEM)\\s+#{SYSTEMLITERAL})|(?:(PUBLIC)\\s+#{PUBIDLITERAL}\\s+#{SYSTEMLITERAL}))"
+ NDATADECL = "\\s+NDATA\\s+#{NAME}"
+ PEREFERENCE = "%#{NAME};"
+ ENTITYVALUE = %Q{((?:"(?:[^%&"]|#{PEREFERENCE}|#{REFERENCE})*")|(?:'([^%&']|#{PEREFERENCE}|#{REFERENCE})*'))}
+ PEDEF = "(?:#{ENTITYVALUE}|#{EXTERNALID})"
+ ENTITYDEF = "(?:#{ENTITYVALUE}|(?:#{EXTERNALID}(#{NDATADECL})?))"
+ PEDECL = "<!ENTITY\\s+(%)\\s+#{NAME}\\s+#{PEDEF}\\s*>"
+ GEDECL = "<!ENTITY\\s+#{NAME}\\s+#{ENTITYDEF}\\s*>"
+ ENTITYDECL = /\s*(?:#{GEDECL})|(?:#{PEDECL})/um
+
+ attr_reader :name, :external, :ref, :ndata, :pubid
+
+ # Create a new entity. Simple entities can be constructed by passing a
+ # name, value to the constructor; this creates a generic, plain entity
+ # reference. For anything more complicated, you have to pass a Source to
+ # the constructor with the entity definiton, or use the accessor methods.
+ # +WARNING+: There is no validation of entity state except when the entity
+ # is read from a stream. If you start poking around with the accessors,
+ # you can easily create a non-conformant Entity. The best thing to do is
+ # dump the stupid DTDs and use XMLSchema instead.
+ #
+ # e = Entity.new( 'amp', '&' )
+ def initialize stream, value=nil, parent=nil, reference=false
+ super(parent)
+ @ndata = @pubid = @value = @external = nil
+ if stream.kind_of? Array
+ @name = stream[1]
+ if stream[-1] == '%'
+ @reference = true
+ stream.pop
+ else
+ @reference = false
+ end
+ if stream[2] =~ /SYSTEM|PUBLIC/
+ @external = stream[2]
+ if @external == 'SYSTEM'
+ @ref = stream[3]
+ @ndata = stream[4] if stream.size == 5
+ else
+ @pubid = stream[3]
+ @ref = stream[4]
+ end
+ else
+ @value = stream[2]
+ end
+ else
+ @reference = reference
+ @external = nil
+ @name = stream
+ @value = value
+ end
+ end
+
+ # Evaluates whether the given string matchs an entity definition,
+ # returning true if so, and false otherwise.
+ def Entity::matches? string
+ (ENTITYDECL =~ string) == 0
+ end
+
+ # Evaluates to the unnormalized value of this entity; that is, replacing
+ # all entities -- both %ent; and &ent; entities. This differs from
+ # +value()+ in that +value+ only replaces %ent; entities.
+ def unnormalized
+ document.record_entity_expansion unless document.nil?
+ v = value()
+ return nil if v.nil?
+ @unnormalized = Text::unnormalize(v, parent)
+ @unnormalized
+ end
+
+ #once :unnormalized
+
+ # Returns the value of this entity unprocessed -- raw. This is the
+ # normalized value; that is, with all %ent; and &ent; entities intact
+ def normalized
+ @value
+ end
+
+ # Write out a fully formed, correct entity definition (assuming the Entity
+ # object itself is valid.)
+ #
+ # out::
+ # An object implementing <TT>&lt;&lt;<TT> to which the entity will be
+ # output
+ # indent::
+ # *DEPRECATED* and ignored
+ def write out, indent=-1
+ out << '<!ENTITY '
+ out << '% ' if @reference
+ out << @name
+ out << ' '
+ if @external
+ out << @external << ' '
+ if @pubid
+ q = @pubid.include?('"')?"'":'"'
+ out << q << @pubid << q << ' '
+ end
+ q = @ref.include?('"')?"'":'"'
+ out << q << @ref << q
+ out << ' NDATA ' << @ndata if @ndata
+ else
+ q = @value.include?('"')?"'":'"'
+ out << q << @value << q
+ end
+ out << '>'
+ end
+
+ # Returns this entity as a string. See write().
+ def to_s
+ rv = ''
+ write rv
+ rv
+ end
+
+ PEREFERENCE_RE = /#{PEREFERENCE}/um
+ # Returns the value of this entity. At the moment, only internal entities
+ # are processed. If the value contains internal references (IE,
+ # %blah;), those are replaced with their values. IE, if the doctype
+ # contains:
+ # <!ENTITY % foo "bar">
+ # <!ENTITY yada "nanoo %foo; nanoo>
+ # then:
+ # doctype.entity('yada').value #-> "nanoo bar nanoo"
+ def value
+ if @value
+ matches = @value.scan(PEREFERENCE_RE)
+ rv = @value.clone
+ if @parent
+ matches.each do |entity_reference|
+ entity_value = @parent.entity( entity_reference[0] )
+ rv.gsub!( /%#{entity_reference.join};/um, entity_value )
+ end
+ end
+ return rv
+ end
+ nil
+ end
+ end
+
+ # This is a set of entity constants -- the ones defined in the XML
+ # specification. These are +gt+, +lt+, +amp+, +quot+ and +apos+.
+ module EntityConst
+ # +>+
+ GT = Entity.new( 'gt', '>' )
+ # +<+
+ LT = Entity.new( 'lt', '<' )
+ # +&+
+ AMP = Entity.new( 'amp', '&' )
+ # +"+
+ QUOT = Entity.new( 'quot', '"' )
+ # +'+
+ APOS = Entity.new( 'apos', "'" )
+ end
+end
diff --git a/ruby/lib/rexml/formatters/default.rb b/ruby/lib/rexml/formatters/default.rb
new file mode 100644
index 0000000..b4d63bc
--- /dev/null
+++ b/ruby/lib/rexml/formatters/default.rb
@@ -0,0 +1,109 @@
+module REXML
+ module Formatters
+ class Default
+ # Prints out the XML document with no formatting -- except if id_hack is
+ # set.
+ #
+ # ie_hack::
+ # If set to true, then inserts whitespace before the close of an empty
+ # tag, so that IE's bad XML parser doesn't choke.
+ def initialize( ie_hack=false )
+ @ie_hack = ie_hack
+ end
+
+ # Writes the node to some output.
+ #
+ # node::
+ # The node to write
+ # output::
+ # A class implementing <TT>&lt;&lt;</TT>. Pass in an Output object to
+ # change the output encoding.
+ def write( node, output )
+ case node
+
+ when Document
+ if node.xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
+ output = Output.new( output, node.xml_decl.encoding )
+ end
+ write_document( node, output )
+
+ when Element
+ write_element( node, output )
+
+ when Declaration, ElementDecl, NotationDecl, ExternalEntity, Entity,
+ Attribute, AttlistDecl
+ node.write( output,-1 )
+
+ when Instruction
+ write_instruction( node, output )
+
+ when DocType, XMLDecl
+ node.write( output )
+
+ when Comment
+ write_comment( node, output )
+
+ when CData
+ write_cdata( node, output )
+
+ when Text
+ write_text( node, output )
+
+ else
+ raise Exception.new("XML FORMATTING ERROR")
+
+ end
+ end
+
+ protected
+ def write_document( node, output )
+ node.children.each { |child| write( child, output ) }
+ end
+
+ def write_element( node, output )
+ output << "<#{node.expanded_name}"
+
+ node.attributes.to_a.sort_by {|attr| attr.name}.each do |attr|
+ output << " "
+ attr.write( output )
+ end unless node.attributes.empty?
+
+ if node.children.empty?
+ output << " " if @ie_hack
+ output << "/"
+ else
+ output << ">"
+ node.children.each { |child|
+ write( child, output )
+ }
+ output << "</#{node.expanded_name}"
+ end
+ output << ">"
+ end
+
+ def write_text( node, output )
+ output << node.to_s()
+ end
+
+ def write_comment( node, output )
+ output << Comment::START
+ output << node.to_s
+ output << Comment::STOP
+ end
+
+ def write_cdata( node, output )
+ output << CData::START
+ output << node.to_s
+ output << CData::STOP
+ end
+
+ def write_instruction( node, output )
+ output << Instruction::START.sub(/\\/u, '')
+ output << node.target
+ output << ' '
+ output << node.content
+ output << Instruction::STOP.sub(/\\/u, '')
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/formatters/pretty.rb b/ruby/lib/rexml/formatters/pretty.rb
new file mode 100644
index 0000000..84c442e
--- /dev/null
+++ b/ruby/lib/rexml/formatters/pretty.rb
@@ -0,0 +1,139 @@
+require 'rexml/formatters/default'
+
+module REXML
+ module Formatters
+ # Pretty-prints an XML document. This destroys whitespace in text nodes
+ # and will insert carriage returns and indentations.
+ #
+ # TODO: Add an option to print attributes on new lines
+ class Pretty < Default
+
+ # If compact is set to true, then the formatter will attempt to use as
+ # little space as possible
+ attr_accessor :compact
+ # The width of a page. Used for formatting text
+ attr_accessor :width
+
+ # Create a new pretty printer.
+ #
+ # output::
+ # An object implementing '<<(String)', to which the output will be written.
+ # indentation::
+ # An integer greater than 0. The indentation of each level will be
+ # this number of spaces. If this is < 1, the behavior of this object
+ # is undefined. Defaults to 2.
+ # ie_hack::
+ # If true, the printer will insert whitespace before closing empty
+ # tags, thereby allowing Internet Explorer's feeble XML parser to
+ # function. Defaults to false.
+ def initialize( indentation=2, ie_hack=false )
+ @indentation = indentation
+ @level = 0
+ @ie_hack = ie_hack
+ @width = 80
+ @compact = false
+ end
+
+ protected
+ def write_element(node, output)
+ output << ' '*@level
+ output << "<#{node.expanded_name}"
+
+ node.attributes.each_attribute do |attr|
+ output << " "
+ attr.write( output )
+ end unless node.attributes.empty?
+
+ if node.children.empty?
+ if @ie_hack
+ output << " "
+ end
+ output << "/"
+ else
+ output << ">"
+ # If compact and all children are text, and if the formatted output
+ # is less than the specified width, then try to print everything on
+ # one line
+ skip = false
+ if compact
+ if node.children.inject(true) {|s,c| s & c.kind_of?(Text)}
+ string = ""
+ old_level = @level
+ @level = 0
+ node.children.each { |child| write( child, string ) }
+ @level = old_level
+ if string.length < @width
+ output << string
+ skip = true
+ end
+ end
+ end
+ unless skip
+ output << "\n"
+ @level += @indentation
+ node.children.each { |child|
+ next if child.kind_of?(Text) and child.to_s.strip.length == 0
+ write( child, output )
+ output << "\n"
+ }
+ @level -= @indentation
+ output << ' '*@level
+ end
+ output << "</#{node.expanded_name}"
+ end
+ output << ">"
+ end
+
+ def write_text( node, output )
+ s = node.to_s()
+ s.gsub!(/\s/,' ')
+ s.squeeze!(" ")
+ s = wrap(s, 80-@level)
+ s = indent_text(s, @level, " ", true)
+ output << (' '*@level + s)
+ end
+
+ def write_comment( node, output)
+ output << ' ' * @level
+ super
+ end
+
+ def write_cdata( node, output)
+ output << ' ' * @level
+ super
+ end
+
+ def write_document( node, output )
+ # Ok, this is a bit odd. All XML documents have an XML declaration,
+ # but it may not write itself if the user didn't specifically add it,
+ # either through the API or in the input document. If it doesn't write
+ # itself, then we don't need a carriage return... which makes this
+ # logic more complex.
+ node.children.each { |child|
+ next if child == node.children[-1] and child.instance_of?(Text)
+ unless child == node.children[0] or child.instance_of?(Text) or
+ (child == node.children[1] and !node.children[0].writethis)
+ output << "\n"
+ end
+ write( child, output )
+ }
+ end
+
+ private
+ def indent_text(string, level=1, style="\t", indentfirstline=true)
+ return string if level < 0
+ string.gsub(/\n/, "\n#{style*level}")
+ end
+
+ def wrap(string, width)
+ # Recursively wrap string at width.
+ return string if string.length <= width
+ place = string.rindex(' ', width) # Position in string with last ' ' before cutoff
+ return string if place.nil?
+ return string[0,place] + "\n" + wrap(string[place+1..-1], width)
+ end
+
+ end
+ end
+end
+
diff --git a/ruby/lib/rexml/formatters/transitive.rb b/ruby/lib/rexml/formatters/transitive.rb
new file mode 100644
index 0000000..6083f03
--- /dev/null
+++ b/ruby/lib/rexml/formatters/transitive.rb
@@ -0,0 +1,58 @@
+require 'rexml/formatters/pretty'
+
+module REXML
+ module Formatters
+ # The Transitive formatter writes an XML document that parses to an
+ # identical document as the source document. This means that no extra
+ # whitespace nodes are inserted, and whitespace within text nodes is
+ # preserved. Within these constraints, the document is pretty-printed,
+ # with whitespace inserted into the metadata to introduce formatting.
+ #
+ # Note that this is only useful if the original XML is not already
+ # formatted. Since this formatter does not alter whitespace nodes, the
+ # results of formatting already formatted XML will be odd.
+ class Transitive < Default
+ def initialize( indentation=2, ie_hack=false )
+ @indentation = indentation
+ @level = 0
+ @ie_hack = ie_hack
+ end
+
+ protected
+ def write_element( node, output )
+ output << "<#{node.expanded_name}"
+
+ node.attributes.each_attribute do |attr|
+ output << " "
+ attr.write( output )
+ end unless node.attributes.empty?
+
+ output << "\n"
+ output << ' '*@level
+ if node.children.empty?
+ output << " " if @ie_hack
+ output << "/"
+ else
+ output << ">"
+ # If compact and all children are text, and if the formatted output
+ # is less than the specified width, then try to print everything on
+ # one line
+ skip = false
+ @level += @indentation
+ node.children.each { |child|
+ write( child, output )
+ }
+ @level -= @indentation
+ output << "</#{node.expanded_name}"
+ output << "\n"
+ output << ' '*@level
+ end
+ output << ">"
+ end
+
+ def write_text( node, output )
+ output << node.to_s()
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/functions.rb b/ruby/lib/rexml/functions.rb
new file mode 100644
index 0000000..fc9c470
--- /dev/null
+++ b/ruby/lib/rexml/functions.rb
@@ -0,0 +1,388 @@
+module REXML
+ # If you add a method, keep in mind two things:
+ # (1) the first argument will always be a list of nodes from which to
+ # filter. In the case of context methods (such as position), the function
+ # should return an array with a value for each child in the array.
+ # (2) all method calls from XML will have "-" replaced with "_".
+ # Therefore, in XML, "local-name()" is identical (and actually becomes)
+ # "local_name()"
+ module Functions
+ @@context = nil
+ @@namespace_context = {}
+ @@variables = {}
+
+ def Functions::namespace_context=(x) ; @@namespace_context=x ; end
+ def Functions::variables=(x) ; @@variables=x ; end
+ def Functions::namespace_context ; @@namespace_context ; end
+ def Functions::variables ; @@variables ; end
+
+ def Functions::context=(value); @@context = value; end
+
+ def Functions::text( )
+ if @@context[:node].node_type == :element
+ return @@context[:node].find_all{|n| n.node_type == :text}.collect{|n| n.value}
+ elsif @@context[:node].node_type == :text
+ return @@context[:node].value
+ else
+ return false
+ end
+ end
+
+ def Functions::last( )
+ @@context[:size]
+ end
+
+ def Functions::position( )
+ @@context[:index]
+ end
+
+ def Functions::count( node_set )
+ node_set.size
+ end
+
+ # Since REXML is non-validating, this method is not implemented as it
+ # requires a DTD
+ def Functions::id( object )
+ end
+
+ # UNTESTED
+ def Functions::local_name( node_set=nil )
+ get_namespace( node_set ) do |node|
+ return node.local_name
+ end
+ end
+
+ def Functions::namespace_uri( node_set=nil )
+ get_namespace( node_set ) {|node| node.namespace}
+ end
+
+ def Functions::name( node_set=nil )
+ get_namespace( node_set ) do |node|
+ node.expanded_name
+ end
+ end
+
+ # Helper method.
+ def Functions::get_namespace( node_set = nil )
+ if node_set == nil
+ yield @@context[:node] if defined? @@context[:node].namespace
+ else
+ if node_set.respond_to? :each
+ node_set.each { |node| yield node if defined? node.namespace }
+ elsif node_set.respond_to? :namespace
+ yield node_set
+ end
+ end
+ end
+
+ # A node-set is converted to a string by returning the string-value of the
+ # node in the node-set that is first in document order. If the node-set is
+ # empty, an empty string is returned.
+ #
+ # A number is converted to a string as follows
+ #
+ # NaN is converted to the string NaN
+ #
+ # positive zero is converted to the string 0
+ #
+ # negative zero is converted to the string 0
+ #
+ # positive infinity is converted to the string Infinity
+ #
+ # negative infinity is converted to the string -Infinity
+ #
+ # if the number is an integer, the number is represented in decimal form
+ # as a Number with no decimal point and no leading zeros, preceded by a
+ # minus sign (-) if the number is negative
+ #
+ # otherwise, the number is represented in decimal form as a Number
+ # including a decimal point with at least one digit before the decimal
+ # point and at least one digit after the decimal point, preceded by a
+ # minus sign (-) if the number is negative; there must be no leading zeros
+ # before the decimal point apart possibly from the one required digit
+ # immediately before the decimal point; beyond the one required digit
+ # after the decimal point there must be as many, but only as many, more
+ # digits as are needed to uniquely distinguish the number from all other
+ # IEEE 754 numeric values.
+ #
+ # The boolean false value is converted to the string false. The boolean
+ # true value is converted to the string true.
+ #
+ # An object of a type other than the four basic types is converted to a
+ # string in a way that is dependent on that type.
+ def Functions::string( object=nil )
+ #object = @context unless object
+ if object.instance_of? Array
+ string( object[0] )
+ elsif defined? object.node_type
+ if object.node_type == :attribute
+ object.value
+ elsif object.node_type == :element || object.node_type == :document
+ string_value(object)
+ else
+ object.to_s
+ end
+ elsif object.nil?
+ return ""
+ else
+ object.to_s
+ end
+ end
+
+ def Functions::string_value( o )
+ rv = ""
+ o.children.each { |e|
+ if e.node_type == :text
+ rv << e.to_s
+ elsif e.node_type == :element
+ rv << string_value( e )
+ end
+ }
+ rv
+ end
+
+ # UNTESTED
+ def Functions::concat( *objects )
+ objects.join
+ end
+
+ # Fixed by Mike Stok
+ def Functions::starts_with( string, test )
+ string(string).index(string(test)) == 0
+ end
+
+ # Fixed by Mike Stok
+ def Functions::contains( string, test )
+ string(string).include?(string(test))
+ end
+
+ # Kouhei fixed this
+ def Functions::substring_before( string, test )
+ ruby_string = string(string)
+ ruby_index = ruby_string.index(string(test))
+ if ruby_index.nil?
+ ""
+ else
+ ruby_string[ 0...ruby_index ]
+ end
+ end
+
+ # Kouhei fixed this too
+ def Functions::substring_after( string, test )
+ ruby_string = string(string)
+ test_string = string(test)
+ return $1 if ruby_string =~ /#{test}(.*)/
+ ""
+ end
+
+ # Take equal portions of Mike Stok and Sean Russell; mix
+ # vigorously, and pour into a tall, chilled glass. Serves 10,000.
+ def Functions::substring( string, start, length=nil )
+ ruby_string = string(string)
+ ruby_length = if length.nil?
+ ruby_string.length.to_f
+ else
+ number(length)
+ end
+ ruby_start = number(start)
+
+ # Handle the special cases
+ return '' if (
+ ruby_length.nan? or
+ ruby_start.nan? or
+ ruby_start.infinite?
+ )
+
+ infinite_length = ruby_length.infinite? == 1
+ ruby_length = ruby_string.length if infinite_length
+
+ # Now, get the bounds. The XPath bounds are 1..length; the ruby bounds
+ # are 0..length. Therefore, we have to offset the bounds by one.
+ ruby_start = ruby_start.round - 1
+ ruby_length = ruby_length.round
+
+ if ruby_start < 0
+ ruby_length += ruby_start unless infinite_length
+ ruby_start = 0
+ end
+ return '' if ruby_length <= 0
+ ruby_string[ruby_start,ruby_length]
+ end
+
+ # UNTESTED
+ def Functions::string_length( string )
+ string(string).length
+ end
+
+ # UNTESTED
+ def Functions::normalize_space( string=nil )
+ string = string(@@context[:node]) if string.nil?
+ if string.kind_of? Array
+ string.collect{|x| string.to_s.strip.gsub(/\s+/um, ' ') if string}
+ else
+ string.to_s.strip.gsub(/\s+/um, ' ')
+ end
+ end
+
+ # This is entirely Mike Stok's beast
+ def Functions::translate( string, tr1, tr2 )
+ from = string(tr1)
+ to = string(tr2)
+
+ # the map is our translation table.
+ #
+ # if a character occurs more than once in the
+ # from string then we ignore the second &
+ # subsequent mappings
+ #
+ # if a character maps to nil then we delete it
+ # in the output. This happens if the from
+ # string is longer than the to string
+ #
+ # there's nothing about - or ^ being special in
+ # http://www.w3.org/TR/xpath#function-translate
+ # so we don't build ranges or negated classes
+
+ map = Hash.new
+ 0.upto(from.length - 1) { |pos|
+ from_char = from[pos]
+ unless map.has_key? from_char
+ map[from_char] =
+ if pos < to.length
+ to[pos]
+ else
+ nil
+ end
+ end
+ }
+
+ if ''.respond_to? :chars
+ string(string).chars.collect { |c|
+ if map.has_key? c then map[c] else c end
+ }.compact.join
+ else
+ string(string).unpack('U*').collect { |c|
+ if map.has_key? c then map[c] else c end
+ }.compact.pack('U*')
+ end
+ end
+
+ # UNTESTED
+ def Functions::boolean( object=nil )
+ if object.kind_of? String
+ if object =~ /\d+/u
+ return object.to_f != 0
+ else
+ return object.size > 0
+ end
+ elsif object.kind_of? Array
+ object = object.find{|x| x and true}
+ end
+ return object ? true : false
+ end
+
+ # UNTESTED
+ def Functions::not( object )
+ not boolean( object )
+ end
+
+ # UNTESTED
+ def Functions::true( )
+ true
+ end
+
+ # UNTESTED
+ def Functions::false( )
+ false
+ end
+
+ # UNTESTED
+ def Functions::lang( language )
+ lang = false
+ node = @@context[:node]
+ attr = nil
+ until node.nil?
+ if node.node_type == :element
+ attr = node.attributes["xml:lang"]
+ unless attr.nil?
+ lang = compare_language(string(language), attr)
+ break
+ else
+ end
+ end
+ node = node.parent
+ end
+ lang
+ end
+
+ def Functions::compare_language lang1, lang2
+ lang2.downcase.index(lang1.downcase) == 0
+ end
+
+ # a string that consists of optional whitespace followed by an optional
+ # minus sign followed by a Number followed by whitespace is converted to
+ # the IEEE 754 number that is nearest (according to the IEEE 754
+ # round-to-nearest rule) to the mathematical value represented by the
+ # string; any other string is converted to NaN
+ #
+ # boolean true is converted to 1; boolean false is converted to 0
+ #
+ # a node-set is first converted to a string as if by a call to the string
+ # function and then converted in the same way as a string argument
+ #
+ # an object of a type other than the four basic types is converted to a
+ # number in a way that is dependent on that type
+ def Functions::number( object=nil )
+ object = @@context[:node] unless object
+ case object
+ when true
+ Float(1)
+ when false
+ Float(0)
+ when Array
+ number(string( object ))
+ when Numeric
+ object.to_f
+ else
+ str = string( object )
+ # If XPath ever gets scientific notation...
+ #if str =~ /^\s*-?(\d*\.?\d+|\d+\.)([Ee]\d*)?\s*$/
+ if str =~ /^\s*-?(\d*\.?\d+|\d+\.)\s*$/
+ str.to_f
+ else
+ (0.0 / 0.0)
+ end
+ end
+ end
+
+ def Functions::sum( nodes )
+ nodes = [nodes] unless nodes.kind_of? Array
+ nodes.inject(0) { |r,n| r += number(string(n)) }
+ end
+
+ def Functions::floor( number )
+ number(number).floor
+ end
+
+ def Functions::ceiling( number )
+ number(number).ceil
+ end
+
+ def Functions::round( number )
+ begin
+ number(number).round
+ rescue FloatDomainError
+ number(number)
+ end
+ end
+
+ def Functions::processing_instruction( node )
+ node.node_type == :processing_instruction
+ end
+
+ def Functions::method_missing( id )
+ puts "METHOD MISSING #{id.id2name}"
+ XPath.match( @@context[:node], id.id2name )
+ end
+ end
+end
diff --git a/ruby/lib/rexml/instruction.rb b/ruby/lib/rexml/instruction.rb
new file mode 100644
index 0000000..50bf95d
--- /dev/null
+++ b/ruby/lib/rexml/instruction.rb
@@ -0,0 +1,70 @@
+require "rexml/child"
+require "rexml/source"
+
+module REXML
+ # Represents an XML Instruction; IE, <? ... ?>
+ # TODO: Add parent arg (3rd arg) to constructor
+ class Instruction < Child
+ START = '<\?'
+ STOP = '\?>'
+
+ # target is the "name" of the Instruction; IE, the "tag" in <?tag ...?>
+ # content is everything else.
+ attr_accessor :target, :content
+
+ # Constructs a new Instruction
+ # @param target can be one of a number of things. If String, then
+ # the target of this instruction is set to this. If an Instruction,
+ # then the Instruction is shallowly cloned (target and content are
+ # copied). If a Source, then the source is scanned and parsed for
+ # an Instruction declaration.
+ # @param content Must be either a String, or a Parent. Can only
+ # be a Parent if the target argument is a Source. Otherwise, this
+ # String is set as the content of this instruction.
+ def initialize(target, content=nil)
+ if target.kind_of? String
+ super()
+ @target = target
+ @content = content
+ elsif target.kind_of? Instruction
+ super(content)
+ @target = target.target
+ @content = target.content
+ end
+ @content.strip! if @content
+ end
+
+ def clone
+ Instruction.new self
+ end
+
+ # == DEPRECATED
+ # See the rexml/formatters package
+ #
+ def write writer, indent=-1, transitive=false, ie_hack=false
+ Kernel.warn( "#{self.class.name}.write is deprecated" )
+ indent(writer, indent)
+ writer << START.sub(/\\/u, '')
+ writer << @target
+ writer << ' '
+ writer << @content
+ writer << STOP.sub(/\\/u, '')
+ end
+
+ # @return true if other is an Instruction, and the content and target
+ # of the other matches the target and content of this object.
+ def ==( other )
+ other.kind_of? Instruction and
+ other.target == @target and
+ other.content == @content
+ end
+
+ def node_type
+ :processing_instruction
+ end
+
+ def inspect
+ "<?p-i #{target} ...?>"
+ end
+ end
+end
diff --git a/ruby/lib/rexml/light/node.rb b/ruby/lib/rexml/light/node.rb
new file mode 100644
index 0000000..9c90148
--- /dev/null
+++ b/ruby/lib/rexml/light/node.rb
@@ -0,0 +1,196 @@
+require 'rexml/xmltokens'
+require 'rexml/light/node'
+
+# [ :element, parent, name, attributes, children* ]
+ # a = Node.new
+ # a << "B" # => <a>B</a>
+ # a.b # => <a>B<b/></a>
+ # a.b[1] # => <a>B<b/><b/><a>
+ # a.b[1]["x"] = "y" # => <a>B<b/><b x="y"/></a>
+ # a.b[0].c # => <a>B<b><c/></b><b x="y"/></a>
+ # a.b.c << "D" # => <a>B<b><c>D</c></b><b x="y"/></a>
+module REXML
+ module Light
+ # Represents a tagged XML element. Elements are characterized by
+ # having children, attributes, and names, and can themselves be
+ # children.
+ class Node
+ NAMESPLIT = /^(?:(#{XMLTokens::NCNAME_STR}):)?(#{XMLTokens::NCNAME_STR})/u
+ PARENTS = [ :element, :document, :doctype ]
+ # Create a new element.
+ def initialize node=nil
+ @node = node
+ if node.kind_of? String
+ node = [ :text, node ]
+ elsif node.nil?
+ node = [ :document, nil, nil ]
+ elsif node[0] == :start_element
+ node[0] = :element
+ elsif node[0] == :start_doctype
+ node[0] = :doctype
+ elsif node[0] == :start_document
+ node[0] = :document
+ end
+ end
+
+ def size
+ if PARENTS.include? @node[0]
+ @node[-1].size
+ else
+ 0
+ end
+ end
+
+ def each( &block )
+ size.times { |x| yield( at(x+4) ) }
+ end
+
+ def name
+ at(2)
+ end
+
+ def name=( name_str, ns=nil )
+ pfx = ''
+ pfx = "#{prefix(ns)}:" if ns
+ _old_put(2, "#{pfx}#{name_str}")
+ end
+
+ def parent=( node )
+ _old_put(1,node)
+ end
+
+ def local_name
+ namesplit
+ @name
+ end
+
+ def local_name=( name_str )
+ _old_put( 1, "#@prefix:#{name_str}" )
+ end
+
+ def prefix( namespace=nil )
+ prefix_of( self, namespace )
+ end
+
+ def namespace( prefix=prefix() )
+ namespace_of( self, prefix )
+ end
+
+ def namespace=( namespace )
+ @prefix = prefix( namespace )
+ pfx = ''
+ pfx = "#@prefix:" if @prefix.size > 0
+ _old_put(1, "#{pfx}#@name")
+ end
+
+ def []( reference, ns=nil )
+ if reference.kind_of? String
+ pfx = ''
+ pfx = "#{prefix(ns)}:" if ns
+ at(3)["#{pfx}#{reference}"]
+ elsif reference.kind_of? Range
+ _old_get( Range.new(4+reference.begin, reference.end, reference.exclude_end?) )
+ else
+ _old_get( 4+reference )
+ end
+ end
+
+ def =~( path )
+ XPath.match( self, path )
+ end
+
+ # Doesn't handle namespaces yet
+ def []=( reference, ns, value=nil )
+ if reference.kind_of? String
+ value = ns unless value
+ at( 3 )[reference] = value
+ elsif reference.kind_of? Range
+ _old_put( Range.new(3+reference.begin, reference.end, reference.exclude_end?), ns )
+ else
+ if value
+ _old_put( 4+reference, ns, value )
+ else
+ _old_put( 4+reference, ns )
+ end
+ end
+ end
+
+ # Append a child to this element, optionally under a provided namespace.
+ # The namespace argument is ignored if the element argument is an Element
+ # object. Otherwise, the element argument is a string, the namespace (if
+ # provided) is the namespace the element is created in.
+ def << element
+ if node_type() == :text
+ at(-1) << element
+ else
+ newnode = Node.new( element )
+ newnode.parent = self
+ self.push( newnode )
+ end
+ at(-1)
+ end
+
+ def node_type
+ _old_get(0)
+ end
+
+ def text=( foo )
+ replace = at(4).kind_of?(String)? 1 : 0
+ self._old_put(4,replace, normalizefoo)
+ end
+
+ def root
+ context = self
+ context = context.at(1) while context.at(1)
+ end
+
+ def has_name?( name, namespace = '' )
+ at(3) == name and namespace() == namespace
+ end
+
+ def children
+ self
+ end
+
+ def parent
+ at(1)
+ end
+
+ def to_s
+
+ end
+
+ private
+
+ def namesplit
+ return if @name.defined?
+ at(2) =~ NAMESPLIT
+ @prefix = '' || $1
+ @name = $2
+ end
+
+ def namespace_of( node, prefix=nil )
+ if not prefix
+ name = at(2)
+ name =~ NAMESPLIT
+ prefix = $1
+ end
+ to_find = 'xmlns'
+ to_find = "xmlns:#{prefix}" if not prefix.nil?
+ ns = at(3)[ to_find ]
+ ns ? ns : namespace_of( @node[0], prefix )
+ end
+
+ def prefix_of( node, namespace=nil )
+ if not namespace
+ name = node.name
+ name =~ NAMESPLIT
+ $1
+ else
+ ns = at(3).find { |k,v| v == namespace }
+ ns ? ns : prefix_of( node.parent, namespace )
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/namespace.rb b/ruby/lib/rexml/namespace.rb
new file mode 100644
index 0000000..8d43fc8
--- /dev/null
+++ b/ruby/lib/rexml/namespace.rb
@@ -0,0 +1,47 @@
+require 'rexml/xmltokens'
+
+module REXML
+ # Adds named attributes to an object.
+ module Namespace
+ # The name of the object, valid if set
+ attr_reader :name, :expanded_name
+ # The expanded name of the object, valid if name is set
+ attr_accessor :prefix
+ include XMLTokens
+ NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u
+
+ # Sets the name and the expanded name
+ def name=( name )
+ @expanded_name = name
+ name =~ NAMESPLIT
+ if $1
+ @prefix = $1
+ else
+ @prefix = ""
+ @namespace = ""
+ end
+ @name = $2
+ end
+
+ # Compares names optionally WITH namespaces
+ def has_name?( other, ns=nil )
+ if ns
+ return (namespace() == ns and name() == other)
+ elsif other.include? ":"
+ return fully_expanded_name == other
+ else
+ return name == other
+ end
+ end
+
+ alias :local_name :name
+
+ # Fully expand the name, even if the prefix wasn't specified in the
+ # source file.
+ def fully_expanded_name
+ ns = prefix
+ return "#{ns}:#@name" if ns.size > 0
+ return @name
+ end
+ end
+end
diff --git a/ruby/lib/rexml/node.rb b/ruby/lib/rexml/node.rb
new file mode 100644
index 0000000..eb39141
--- /dev/null
+++ b/ruby/lib/rexml/node.rb
@@ -0,0 +1,75 @@
+require "rexml/parseexception"
+require "rexml/formatters/pretty"
+require "rexml/formatters/default"
+
+module REXML
+ # Represents a node in the tree. Nodes are never encountered except as
+ # superclasses of other objects. Nodes have siblings.
+ module Node
+ # @return the next sibling (nil if unset)
+ def next_sibling_node
+ return nil if @parent.nil?
+ @parent[ @parent.index(self) + 1 ]
+ end
+
+ # @return the previous sibling (nil if unset)
+ def previous_sibling_node
+ return nil if @parent.nil?
+ ind = @parent.index(self)
+ return nil if ind == 0
+ @parent[ ind - 1 ]
+ end
+
+ # indent::
+ # *DEPRECATED* This parameter is now ignored. See the formatters in the
+ # REXML::Formatters package for changing the output style.
+ def to_s indent=nil
+ unless indent.nil?
+ Kernel.warn( "#{self.class.name}.to_s(indent) parameter is deprecated" )
+ f = REXML::Formatters::Pretty.new( indent )
+ f.write( self, rv = "" )
+ else
+ f = REXML::Formatters::Default.new
+ f.write( self, rv = "" )
+ end
+ return rv
+ end
+
+ def indent to, ind
+ if @parent and @parent.context and not @parent.context[:indentstyle].nil? then
+ indentstyle = @parent.context[:indentstyle]
+ else
+ indentstyle = ' '
+ end
+ to << indentstyle*ind unless ind<1
+ end
+
+ def parent?
+ false;
+ end
+
+
+ # Visit all subnodes of +self+ recursively
+ def each_recursive(&block) # :yields: node
+ self.elements.each {|node|
+ block.call(node)
+ node.each_recursive(&block)
+ }
+ end
+
+ # Find (and return) first subnode (recursively) for which the block
+ # evaluates to true. Returns +nil+ if none was found.
+ def find_first_recursive(&block) # :yields: node
+ each_recursive {|node|
+ return node if block.call(node)
+ }
+ return nil
+ end
+
+ # Returns the position that +self+ holds in its parent's array, indexed
+ # from 1.
+ def index_in_parent
+ parent.index(self)+1
+ end
+ end
+end
diff --git a/ruby/lib/rexml/output.rb b/ruby/lib/rexml/output.rb
new file mode 100644
index 0000000..997f2b1
--- /dev/null
+++ b/ruby/lib/rexml/output.rb
@@ -0,0 +1,24 @@
+require 'rexml/encoding'
+
+module REXML
+ class Output
+ include Encoding
+
+ attr_reader :encoding
+
+ def initialize real_IO, encd="iso-8859-1"
+ @output = real_IO
+ self.encoding = encd
+
+ @to_utf = encd == UTF_8 ? false : true
+ end
+
+ def <<( content )
+ @output << (@to_utf ? self.encode(content) : content)
+ end
+
+ def to_s
+ "Output[#{encoding}]"
+ end
+ end
+end
diff --git a/ruby/lib/rexml/parent.rb b/ruby/lib/rexml/parent.rb
new file mode 100644
index 0000000..a20aaae
--- /dev/null
+++ b/ruby/lib/rexml/parent.rb
@@ -0,0 +1,166 @@
+require "rexml/child"
+
+module REXML
+ # A parent has children, and has methods for accessing them. The Parent
+ # class is never encountered except as the superclass for some other
+ # object.
+ class Parent < Child
+ include Enumerable
+
+ # Constructor
+ # @param parent if supplied, will be set as the parent of this object
+ def initialize parent=nil
+ super(parent)
+ @children = []
+ end
+
+ def add( object )
+ #puts "PARENT GOTS #{size} CHILDREN"
+ object.parent = self
+ @children << object
+ #puts "PARENT NOW GOTS #{size} CHILDREN"
+ object
+ end
+
+ alias :push :add
+ alias :<< :push
+
+ def unshift( object )
+ object.parent = self
+ @children.unshift object
+ end
+
+ def delete( object )
+ found = false
+ @children.delete_if {|c| c.equal?(object) and found = true }
+ object.parent = nil if found
+ end
+
+ def each(&block)
+ @children.each(&block)
+ end
+
+ def delete_if( &block )
+ @children.delete_if(&block)
+ end
+
+ def delete_at( index )
+ @children.delete_at index
+ end
+
+ def each_index( &block )
+ @children.each_index(&block)
+ end
+
+ # Fetches a child at a given index
+ # @param index the Integer index of the child to fetch
+ def []( index )
+ @children[index]
+ end
+
+ alias :each_child :each
+
+
+
+ # Set an index entry. See Array.[]=
+ # @param index the index of the element to set
+ # @param opt either the object to set, or an Integer length
+ # @param child if opt is an Integer, this is the child to set
+ # @return the parent (self)
+ def []=( *args )
+ args[-1].parent = self
+ @children[*args[0..-2]] = args[-1]
+ end
+
+ # Inserts an child before another child
+ # @param child1 this is either an xpath or an Element. If an Element,
+ # child2 will be inserted before child1 in the child list of the parent.
+ # If an xpath, child2 will be inserted before the first child to match
+ # the xpath.
+ # @param child2 the child to insert
+ # @return the parent (self)
+ def insert_before( child1, child2 )
+ if child1.kind_of? String
+ child1 = XPath.first( self, child1 )
+ child1.parent.insert_before child1, child2
+ else
+ ind = index(child1)
+ child2.parent.delete(child2) if child2.parent
+ @children[ind,0] = child2
+ child2.parent = self
+ end
+ self
+ end
+
+ # Inserts an child after another child
+ # @param child1 this is either an xpath or an Element. If an Element,
+ # child2 will be inserted after child1 in the child list of the parent.
+ # If an xpath, child2 will be inserted after the first child to match
+ # the xpath.
+ # @param child2 the child to insert
+ # @return the parent (self)
+ def insert_after( child1, child2 )
+ if child1.kind_of? String
+ child1 = XPath.first( self, child1 )
+ child1.parent.insert_after child1, child2
+ else
+ ind = index(child1)+1
+ child2.parent.delete(child2) if child2.parent
+ @children[ind,0] = child2
+ child2.parent = self
+ end
+ self
+ end
+
+ def to_a
+ @children.dup
+ end
+
+ # Fetches the index of a given child
+ # @param child the child to get the index of
+ # @return the index of the child, or nil if the object is not a child
+ # of this parent.
+ def index( child )
+ count = -1
+ @children.find { |i| count += 1 ; i.hash == child.hash }
+ count
+ end
+
+ # @return the number of children of this parent
+ def size
+ @children.size
+ end
+
+ alias :length :size
+
+ # Replaces one child with another, making sure the nodelist is correct
+ # @param to_replace the child to replace (must be a Child)
+ # @param replacement the child to insert into the nodelist (must be a
+ # Child)
+ def replace_child( to_replace, replacement )
+ @children.map! {|c| c.equal?( to_replace ) ? replacement : c }
+ to_replace.parent = nil
+ replacement.parent = self
+ end
+
+ # Deeply clones this object. This creates a complete duplicate of this
+ # Parent, including all descendants.
+ def deep_clone
+ cl = clone()
+ each do |child|
+ if child.kind_of? Parent
+ cl << child.deep_clone
+ else
+ cl << child.clone
+ end
+ end
+ cl
+ end
+
+ alias :children :to_a
+
+ def parent?
+ true
+ end
+ end
+end
diff --git a/ruby/lib/rexml/parseexception.rb b/ruby/lib/rexml/parseexception.rb
new file mode 100644
index 0000000..feb7a7e
--- /dev/null
+++ b/ruby/lib/rexml/parseexception.rb
@@ -0,0 +1,51 @@
+module REXML
+ class ParseException < RuntimeError
+ attr_accessor :source, :parser, :continued_exception
+
+ def initialize( message, source=nil, parser=nil, exception=nil )
+ super(message)
+ @source = source
+ @parser = parser
+ @continued_exception = exception
+ end
+
+ def to_s
+ # Quote the original exception, if there was one
+ if @continued_exception
+ err = @continued_exception.inspect
+ err << "\n"
+ err << @continued_exception.backtrace.join("\n")
+ err << "\n...\n"
+ else
+ err = ""
+ end
+
+ # Get the stack trace and error message
+ err << super
+
+ # Add contextual information
+ if @source
+ err << "\nLine: #{line}\n"
+ err << "Position: #{position}\n"
+ err << "Last 80 unconsumed characters:\n"
+ err << @source.buffer[0..80].gsub(/\n/, ' ')
+ end
+
+ err
+ end
+
+ def position
+ @source.current_line[0] if @source and defined? @source.current_line and
+ @source.current_line
+ end
+
+ def line
+ @source.current_line[2] if @source and defined? @source.current_line and
+ @source.current_line
+ end
+
+ def context
+ @source.current_line
+ end
+ end
+end
diff --git a/ruby/lib/rexml/parsers/baseparser.rb b/ruby/lib/rexml/parsers/baseparser.rb
new file mode 100644
index 0000000..162d029
--- /dev/null
+++ b/ruby/lib/rexml/parsers/baseparser.rb
@@ -0,0 +1,530 @@
+require 'rexml/parseexception'
+require 'rexml/undefinednamespaceexception'
+require 'rexml/source'
+require 'set'
+
+module REXML
+ module Parsers
+ # = Using the Pull Parser
+ # <em>This API is experimental, and subject to change.</em>
+ # parser = PullParser.new( "<a>text<b att='val'/>txet</a>" )
+ # while parser.has_next?
+ # res = parser.next
+ # puts res[1]['att'] if res.start_tag? and res[0] == 'b'
+ # end
+ # See the PullEvent class for information on the content of the results.
+ # The data is identical to the arguments passed for the various events to
+ # the StreamListener API.
+ #
+ # Notice that:
+ # parser = PullParser.new( "<a>BAD DOCUMENT" )
+ # while parser.has_next?
+ # res = parser.next
+ # raise res[1] if res.error?
+ # end
+ #
+ # Nat Price gave me some good ideas for the API.
+ class BaseParser
+ if String.method_defined? :encode
+ # Oniguruma / POSIX [understands unicode]
+ LETTER = '[[:alpha:]]'
+ DIGIT = '[[:digit:]]'
+ else
+ # Ruby < 1.9 [doesn't understand unicode]
+ LETTER = 'a-zA-Z'
+ DIGIT = '\d'
+ end
+
+ COMBININGCHAR = '' # TODO
+ EXTENDER = '' # TODO
+
+ NCNAME_STR= "[#{LETTER}_:][-#{LETTER}#{DIGIT}._:#{COMBININGCHAR}#{EXTENDER}]*"
+ NAME_STR= "(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})"
+ UNAME_STR= "(?:#{NCNAME_STR}:)?#{NCNAME_STR}"
+
+ NAMECHAR = '[\-\w\d\.:]'
+ NAME = "([\\w:]#{NAMECHAR}*)"
+ NMTOKEN = "(?:#{NAMECHAR})+"
+ NMTOKENS = "#{NMTOKEN}(\\s+#{NMTOKEN})*"
+ REFERENCE = "&(?:#{NAME};|#\\d+;|#x[0-9a-fA-F]+;)"
+ REFERENCE_RE = /#{REFERENCE}/
+
+ DOCTYPE_START = /\A\s*<!DOCTYPE\s/um
+ DOCTYPE_PATTERN = /\s*<!DOCTYPE\s+(.*?)(\[|>)/um
+ ATTRIBUTE_PATTERN = /\s*(#{NAME_STR})\s*=\s*(["'])(.*?)\4/um
+ COMMENT_START = /\A<!--/u
+ COMMENT_PATTERN = /<!--(.*?)-->/um
+ CDATA_START = /\A<!\[CDATA\[/u
+ CDATA_END = /^\s*\]\s*>/um
+ CDATA_PATTERN = /<!\[CDATA\[(.*?)\]\]>/um
+ XMLDECL_START = /\A<\?xml\s/u;
+ XMLDECL_PATTERN = /<\?xml\s+(.*?)\?>/um
+ INSTRUCTION_START = /\A<\?/u
+ INSTRUCTION_PATTERN = /<\?(.*?)(\s+.*?)?\?>/um
+ TAG_MATCH = /^<((?>#{NAME_STR}))\s*((?>\s+#{UNAME_STR}\s*=\s*(["']).*?\5)*)\s*(\/)?>/um
+ CLOSE_MATCH = /^\s*<\/(#{NAME_STR})\s*>/um
+
+ VERSION = /\bversion\s*=\s*["'](.*?)['"]/um
+ ENCODING = /\bencoding\s*=\s*["'](.*?)['"]/um
+ STANDALONE = /\bstandalone\s*=\s["'](.*?)['"]/um
+
+ ENTITY_START = /^\s*<!ENTITY/
+ IDENTITY = /^([!\*\w\-]+)(\s+#{NCNAME_STR})?(\s+["'](.*?)['"])?(\s+['"](.*?)["'])?/u
+ ELEMENTDECL_START = /^\s*<!ELEMENT/um
+ ELEMENTDECL_PATTERN = /^\s*(<!ELEMENT.*?)>/um
+ SYSTEMENTITY = /^\s*(%.*?;)\s*$/um
+ ENUMERATION = "\\(\\s*#{NMTOKEN}(?:\\s*\\|\\s*#{NMTOKEN})*\\s*\\)"
+ NOTATIONTYPE = "NOTATION\\s+\\(\\s*#{NAME}(?:\\s*\\|\\s*#{NAME})*\\s*\\)"
+ ENUMERATEDTYPE = "(?:(?:#{NOTATIONTYPE})|(?:#{ENUMERATION}))"
+ ATTTYPE = "(CDATA|ID|IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS|#{ENUMERATEDTYPE})"
+ ATTVALUE = "(?:\"((?:[^<&\"]|#{REFERENCE})*)\")|(?:'((?:[^<&']|#{REFERENCE})*)')"
+ DEFAULTDECL = "(#REQUIRED|#IMPLIED|(?:(#FIXED\\s+)?#{ATTVALUE}))"
+ ATTDEF = "\\s+#{NAME}\\s+#{ATTTYPE}\\s+#{DEFAULTDECL}"
+ ATTDEF_RE = /#{ATTDEF}/
+ ATTLISTDECL_START = /^\s*<!ATTLIST/um
+ ATTLISTDECL_PATTERN = /^\s*<!ATTLIST\s+#{NAME}(?:#{ATTDEF})*\s*>/um
+ NOTATIONDECL_START = /^\s*<!NOTATION/um
+ PUBLIC = /^\s*<!NOTATION\s+(\w[\-\w]*)\s+(PUBLIC)\s+(["'])(.*?)\3(?:\s+(["'])(.*?)\5)?\s*>/um
+ SYSTEM = /^\s*<!NOTATION\s+(\w[\-\w]*)\s+(SYSTEM)\s+(["'])(.*?)\3\s*>/um
+
+ TEXT_PATTERN = /\A([^<]*)/um
+
+ # Entity constants
+ PUBIDCHAR = "\x20\x0D\x0Aa-zA-Z0-9\\-()+,./:=?;!*@$_%#"
+ SYSTEMLITERAL = %Q{((?:"[^"]*")|(?:'[^']*'))}
+ PUBIDLITERAL = %Q{("[#{PUBIDCHAR}']*"|'[#{PUBIDCHAR}]*')}
+ EXTERNALID = "(?:(?:(SYSTEM)\\s+#{SYSTEMLITERAL})|(?:(PUBLIC)\\s+#{PUBIDLITERAL}\\s+#{SYSTEMLITERAL}))"
+ NDATADECL = "\\s+NDATA\\s+#{NAME}"
+ PEREFERENCE = "%#{NAME};"
+ ENTITYVALUE = %Q{((?:"(?:[^%&"]|#{PEREFERENCE}|#{REFERENCE})*")|(?:'([^%&']|#{PEREFERENCE}|#{REFERENCE})*'))}
+ PEDEF = "(?:#{ENTITYVALUE}|#{EXTERNALID})"
+ ENTITYDEF = "(?:#{ENTITYVALUE}|(?:#{EXTERNALID}(#{NDATADECL})?))"
+ PEDECL = "<!ENTITY\\s+(%)\\s+#{NAME}\\s+#{PEDEF}\\s*>"
+ GEDECL = "<!ENTITY\\s+#{NAME}\\s+#{ENTITYDEF}\\s*>"
+ ENTITYDECL = /\s*(?:#{GEDECL})|(?:#{PEDECL})/um
+
+ EREFERENCE = /&(?!#{NAME};)/
+
+ DEFAULT_ENTITIES = {
+ 'gt' => [/&gt;/, '&gt;', '>', />/],
+ 'lt' => [/&lt;/, '&lt;', '<', /</],
+ 'quot' => [/&quot;/, '&quot;', '"', /"/],
+ "apos" => [/&apos;/, "&apos;", "'", /'/]
+ }
+
+
+ ######################################################################
+ # These are patterns to identify common markup errors, to make the
+ # error messages more informative.
+ ######################################################################
+ MISSING_ATTRIBUTE_QUOTES = /^<#{NAME_STR}\s+#{NAME_STR}\s*=\s*[^"']/um
+
+ def initialize( source )
+ self.stream = source
+ end
+
+ def add_listener( listener )
+ if !defined?(@listeners) or !@listeners
+ @listeners = []
+ instance_eval <<-EOL
+ alias :_old_pull :pull
+ def pull
+ event = _old_pull
+ @listeners.each do |listener|
+ listener.receive event
+ end
+ event
+ end
+ EOL
+ end
+ @listeners << listener
+ end
+
+ attr_reader :source
+
+ def stream=( source )
+ @source = SourceFactory.create_from( source )
+ @closed = nil
+ @document_status = nil
+ @tags = []
+ @stack = []
+ @entities = []
+ @nsstack = []
+ end
+
+ def position
+ if @source.respond_to? :position
+ @source.position
+ else
+ # FIXME
+ 0
+ end
+ end
+
+ # Returns true if there are no more events
+ def empty?
+ return (@source.empty? and @stack.empty?)
+ end
+
+ # Returns true if there are more events. Synonymous with !empty?
+ def has_next?
+ return !(@source.empty? and @stack.empty?)
+ end
+
+ # Push an event back on the head of the stream. This method
+ # has (theoretically) infinite depth.
+ def unshift token
+ @stack.unshift(token)
+ end
+
+ # Peek at the +depth+ event in the stack. The first element on the stack
+ # is at depth 0. If +depth+ is -1, will parse to the end of the input
+ # stream and return the last event, which is always :end_document.
+ # Be aware that this causes the stream to be parsed up to the +depth+
+ # event, so you can effectively pre-parse the entire document (pull the
+ # entire thing into memory) using this method.
+ def peek depth=0
+ raise %Q[Illegal argument "#{depth}"] if depth < -1
+ temp = []
+ if depth == -1
+ temp.push(pull()) until empty?
+ else
+ while @stack.size+temp.size < depth+1
+ temp.push(pull())
+ end
+ end
+ @stack += temp if temp.size > 0
+ @stack[depth]
+ end
+
+ # Returns the next event. This is a +PullEvent+ object.
+ def pull
+ if @closed
+ x, @closed = @closed, nil
+ return [ :end_element, x ]
+ end
+ return [ :end_document ] if empty?
+ return @stack.shift if @stack.size > 0
+ #STDERR.puts @source.encoding
+ @source.read if @source.buffer.size<2
+ #STDERR.puts "BUFFER = #{@source.buffer.inspect}"
+ if @document_status == nil
+ #@source.consume( /^\s*/um )
+ word = @source.match( /^((?:\s+)|(?:<[^>]*>))/um )
+ word = word[1] unless word.nil?
+ #STDERR.puts "WORD = #{word.inspect}"
+ case word
+ when COMMENT_START
+ return [ :comment, @source.match( COMMENT_PATTERN, true )[1] ]
+ when XMLDECL_START
+ #STDERR.puts "XMLDECL"
+ results = @source.match( XMLDECL_PATTERN, true )[1]
+ version = VERSION.match( results )
+ version = version[1] unless version.nil?
+ encoding = ENCODING.match(results)
+ encoding = encoding[1] unless encoding.nil?
+ @source.encoding = encoding
+ standalone = STANDALONE.match(results)
+ standalone = standalone[1] unless standalone.nil?
+ return [ :xmldecl, version, encoding, standalone ]
+ when INSTRUCTION_START
+ return [ :processing_instruction, *@source.match(INSTRUCTION_PATTERN, true)[1,2] ]
+ when DOCTYPE_START
+ md = @source.match( DOCTYPE_PATTERN, true )
+ @nsstack.unshift(curr_ns=Set.new)
+ identity = md[1]
+ close = md[2]
+ identity =~ IDENTITY
+ name = $1
+ raise REXML::ParseException.new("DOCTYPE is missing a name") if name.nil?
+ pub_sys = $2.nil? ? nil : $2.strip
+ long_name = $4.nil? ? nil : $4.strip
+ uri = $6.nil? ? nil : $6.strip
+ args = [ :start_doctype, name, pub_sys, long_name, uri ]
+ if close == ">"
+ @document_status = :after_doctype
+ @source.read if @source.buffer.size<2
+ md = @source.match(/^\s*/um, true)
+ @stack << [ :end_doctype ]
+ else
+ @document_status = :in_doctype
+ end
+ return args
+ when /^\s+/
+ else
+ @document_status = :after_doctype
+ @source.read if @source.buffer.size<2
+ md = @source.match(/\s*/um, true)
+ if @source.encoding == "UTF-8"
+ if @source.buffer.respond_to? :force_encoding
+ @source.buffer.force_encoding(Encoding::UTF_8)
+ end
+ end
+ end
+ end
+ if @document_status == :in_doctype
+ md = @source.match(/\s*(.*?>)/um)
+ case md[1]
+ when SYSTEMENTITY
+ match = @source.match( SYSTEMENTITY, true )[1]
+ return [ :externalentity, match ]
+
+ when ELEMENTDECL_START
+ return [ :elementdecl, @source.match( ELEMENTDECL_PATTERN, true )[1] ]
+
+ when ENTITY_START
+ match = @source.match( ENTITYDECL, true ).to_a.compact
+ match[0] = :entitydecl
+ ref = false
+ if match[1] == '%'
+ ref = true
+ match.delete_at 1
+ end
+ # Now we have to sort out what kind of entity reference this is
+ if match[2] == 'SYSTEM'
+ # External reference
+ match[3] = match[3][1..-2] # PUBID
+ match.delete_at(4) if match.size > 4 # Chop out NDATA decl
+ # match is [ :entity, name, SYSTEM, pubid(, ndata)? ]
+ elsif match[2] == 'PUBLIC'
+ # External reference
+ match[3] = match[3][1..-2] # PUBID
+ match[4] = match[4][1..-2] # HREF
+ # match is [ :entity, name, PUBLIC, pubid, href ]
+ else
+ match[2] = match[2][1..-2]
+ match.pop if match.size == 4
+ # match is [ :entity, name, value ]
+ end
+ match << '%' if ref
+ return match
+ when ATTLISTDECL_START
+ md = @source.match( ATTLISTDECL_PATTERN, true )
+ raise REXML::ParseException.new( "Bad ATTLIST declaration!", @source ) if md.nil?
+ element = md[1]
+ contents = md[0]
+
+ pairs = {}
+ values = md[0].scan( ATTDEF_RE )
+ values.each do |attdef|
+ unless attdef[3] == "#IMPLIED"
+ attdef.compact!
+ val = attdef[3]
+ val = attdef[4] if val == "#FIXED "
+ pairs[attdef[0]] = val
+ if attdef[0] =~ /^xmlns:(.*)/
+ @nsstack[0] << $1
+ end
+ end
+ end
+ return [ :attlistdecl, element, pairs, contents ]
+ when NOTATIONDECL_START
+ md = nil
+ if @source.match( PUBLIC )
+ md = @source.match( PUBLIC, true )
+ vals = [md[1],md[2],md[4],md[6]]
+ elsif @source.match( SYSTEM )
+ md = @source.match( SYSTEM, true )
+ vals = [md[1],md[2],nil,md[4]]
+ else
+ raise REXML::ParseException.new( "error parsing notation: no matching pattern", @source )
+ end
+ return [ :notationdecl, *vals ]
+ when CDATA_END
+ @document_status = :after_doctype
+ @source.match( CDATA_END, true )
+ return [ :end_doctype ]
+ end
+ end
+ begin
+ if @source.buffer[0] == ?<
+ if @source.buffer[1] == ?/
+ @nsstack.shift
+ last_tag = @tags.pop
+ #md = @source.match_to_consume( '>', CLOSE_MATCH)
+ md = @source.match( CLOSE_MATCH, true )
+ raise REXML::ParseException.new( "Missing end tag for "+
+ "'#{last_tag}' (got \"#{md[1]}\")",
+ @source) unless last_tag == md[1]
+ return [ :end_element, last_tag ]
+ elsif @source.buffer[1] == ?!
+ md = @source.match(/\A(\s*[^>]*>)/um)
+ #STDERR.puts "SOURCE BUFFER = #{source.buffer}, #{source.buffer.size}"
+ raise REXML::ParseException.new("Malformed node", @source) unless md
+ if md[0][2] == ?-
+ md = @source.match( COMMENT_PATTERN, true )
+
+ case md[1]
+ when /--/, /-$/
+ raise REXML::ParseException.new("Malformed comment", @source)
+ end
+
+ return [ :comment, md[1] ] if md
+ else
+ md = @source.match( CDATA_PATTERN, true )
+ return [ :cdata, md[1] ] if md
+ end
+ raise REXML::ParseException.new( "Declarations can only occur "+
+ "in the doctype declaration.", @source)
+ elsif @source.buffer[1] == ??
+ md = @source.match( INSTRUCTION_PATTERN, true )
+ return [ :processing_instruction, md[1], md[2] ] if md
+ raise REXML::ParseException.new( "Bad instruction declaration",
+ @source)
+ else
+ # Get the next tag
+ md = @source.match(TAG_MATCH, true)
+ unless md
+ # Check for missing attribute quotes
+ raise REXML::ParseException.new("missing attribute quote", @source) if @source.match(MISSING_ATTRIBUTE_QUOTES )
+ raise REXML::ParseException.new("malformed XML: missing tag start", @source)
+ end
+ attributes = {}
+ prefixes = Set.new
+ prefixes << md[2] if md[2]
+ @nsstack.unshift(curr_ns=Set.new)
+ if md[4].size > 0
+ attrs = md[4].scan( ATTRIBUTE_PATTERN )
+ raise REXML::ParseException.new( "error parsing attributes: [#{attrs.join ', '}], excess = \"#$'\"", @source) if $' and $'.strip.size > 0
+ attrs.each { |a,b,c,d,e|
+ if b == "xmlns"
+ if c == "xml"
+ if d != "http://www.w3.org/XML/1998/namespace"
+ msg = "The 'xml' prefix must not be bound to any other namespace "+
+ "(http://www.w3.org/TR/REC-xml-names/#ns-decl)"
+ raise REXML::ParseException.new( msg, @source, self )
+ end
+ elsif c == "xmlns"
+ msg = "The 'xmlns' prefix must not be declared "+
+ "(http://www.w3.org/TR/REC-xml-names/#ns-decl)"
+ raise REXML::ParseException.new( msg, @source, self)
+ end
+ curr_ns << c
+ elsif b
+ prefixes << b unless b == "xml"
+ end
+
+ if attributes.has_key? a
+ msg = "Duplicate attribute #{a.inspect}"
+ raise REXML::ParseException.new( msg, @source, self)
+ end
+
+ attributes[a] = e
+ }
+ end
+
+ # Verify that all of the prefixes have been defined
+ for prefix in prefixes
+ unless @nsstack.find{|k| k.member?(prefix)}
+ raise UndefinedNamespaceException.new(prefix,@source,self)
+ end
+ end
+
+ if md[6]
+ @closed = md[1]
+ @nsstack.shift
+ else
+ @tags.push( md[1] )
+ end
+ return [ :start_element, md[1], attributes ]
+ end
+ else
+ md = @source.match( TEXT_PATTERN, true )
+ if md[0].length == 0
+ @source.match( /(\s+)/, true )
+ end
+ #STDERR.puts "GOT #{md[1].inspect}" unless md[0].length == 0
+ #return [ :text, "" ] if md[0].length == 0
+ # unnormalized = Text::unnormalize( md[1], self )
+ # return PullEvent.new( :text, md[1], unnormalized )
+ return [ :text, md[1] ]
+ end
+ rescue REXML::UndefinedNamespaceException
+ raise
+ rescue REXML::ParseException
+ raise
+ rescue Exception, NameError => error
+ raise REXML::ParseException.new( "Exception parsing",
+ @source, self, (error ? error : $!) )
+ end
+ return [ :dummy ]
+ end
+
+ def entity( reference, entities )
+ value = nil
+ value = entities[ reference ] if entities
+ if not value
+ value = DEFAULT_ENTITIES[ reference ]
+ value = value[2] if value
+ end
+ unnormalize( value, entities ) if value
+ end
+
+ # Escapes all possible entities
+ def normalize( input, entities=nil, entity_filter=nil )
+ copy = input.clone
+ # Doing it like this rather than in a loop improves the speed
+ copy.gsub!( EREFERENCE, '&amp;' )
+ entities.each do |key, value|
+ copy.gsub!( value, "&#{key};" ) unless entity_filter and
+ entity_filter.include?(entity)
+ end if entities
+ copy.gsub!( EREFERENCE, '&amp;' )
+ DEFAULT_ENTITIES.each do |key, value|
+ copy.gsub!( value[3], value[1] )
+ end
+ copy
+ end
+
+ # Unescapes all possible entities
+ def unnormalize( string, entities=nil, filter=nil )
+ rv = string.clone
+ rv.gsub!( /\r\n?/, "\n" )
+ matches = rv.scan( REFERENCE_RE )
+ return rv if matches.size == 0
+ rv.gsub!( /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/ ) {
+ m=$1
+ m = "0#{m}" if m[0] == ?x
+ [Integer(m)].pack('U*')
+ }
+ matches.collect!{|x|x[0]}.compact!
+ if matches.size > 0
+ matches.each do |entity_reference|
+ unless filter and filter.include?(entity_reference)
+ entity_value = entity( entity_reference, entities )
+ if entity_value
+ re = /&#{entity_reference};/
+ rv.gsub!( re, entity_value )
+ else
+ er = DEFAULT_ENTITIES[entity_reference]
+ rv.gsub!( er[0], er[2] ) if er
+ end
+ end
+ end
+ rv.gsub!( /&amp;/, '&' )
+ end
+ rv
+ end
+ end
+ end
+end
+
+=begin
+ case event[0]
+ when :start_element
+ when :text
+ when :end_element
+ when :processing_instruction
+ when :cdata
+ when :comment
+ when :xmldecl
+ when :start_doctype
+ when :end_doctype
+ when :externalentity
+ when :elementdecl
+ when :entity
+ when :attlistdecl
+ when :notationdecl
+ when :end_doctype
+ end
+=end
diff --git a/ruby/lib/rexml/parsers/lightparser.rb b/ruby/lib/rexml/parsers/lightparser.rb
new file mode 100644
index 0000000..ca9692c
--- /dev/null
+++ b/ruby/lib/rexml/parsers/lightparser.rb
@@ -0,0 +1,58 @@
+require 'rexml/parsers/streamparser'
+require 'rexml/parsers/baseparser'
+require 'rexml/light/node'
+
+module REXML
+ module Parsers
+ class LightParser
+ def initialize stream
+ @stream = stream
+ @parser = REXML::Parsers::BaseParser.new( stream )
+ end
+
+ def add_listener( listener )
+ @parser.add_listener( listener )
+ end
+
+ def rewind
+ @stream.rewind
+ @parser.stream = @stream
+ end
+
+ def parse
+ root = context = [ :document ]
+ while true
+ event = @parser.pull
+ case event[0]
+ when :end_document
+ break
+ when :start_element, :start_doctype
+ new_node = event
+ context << new_node
+ new_node[1,0] = [context]
+ context = new_node
+ when :end_element, :end_doctype
+ context = context[1]
+ else
+ new_node = event
+ context << new_node
+ new_node[1,0] = [context]
+ end
+ end
+ root
+ end
+ end
+
+ # An element is an array. The array contains:
+ # 0 The parent element
+ # 1 The tag name
+ # 2 A hash of attributes
+ # 3..-1 The child elements
+ # An element is an array of size > 3
+ # Text is a String
+ # PIs are [ :processing_instruction, target, data ]
+ # Comments are [ :comment, data ]
+ # DocTypes are DocType structs
+ # The root is an array with XMLDecls, Text, DocType, Array, Text
+ end
+end
diff --git a/ruby/lib/rexml/parsers/pullparser.rb b/ruby/lib/rexml/parsers/pullparser.rb
new file mode 100644
index 0000000..36dc716
--- /dev/null
+++ b/ruby/lib/rexml/parsers/pullparser.rb
@@ -0,0 +1,196 @@
+require 'forwardable'
+
+require 'rexml/parseexception'
+require 'rexml/parsers/baseparser'
+require 'rexml/xmltokens'
+
+module REXML
+ module Parsers
+ # = Using the Pull Parser
+ # <em>This API is experimental, and subject to change.</em>
+ # parser = PullParser.new( "<a>text<b att='val'/>txet</a>" )
+ # while parser.has_next?
+ # res = parser.next
+ # puts res[1]['att'] if res.start_tag? and res[0] == 'b'
+ # end
+ # See the PullEvent class for information on the content of the results.
+ # The data is identical to the arguments passed for the various events to
+ # the StreamListener API.
+ #
+ # Notice that:
+ # parser = PullParser.new( "<a>BAD DOCUMENT" )
+ # while parser.has_next?
+ # res = parser.next
+ # raise res[1] if res.error?
+ # end
+ #
+ # Nat Price gave me some good ideas for the API.
+ class PullParser
+ include XMLTokens
+ extend Forwardable
+
+ def_delegators( :@parser, :has_next? )
+ def_delegators( :@parser, :entity )
+ def_delegators( :@parser, :empty? )
+ def_delegators( :@parser, :source )
+
+ def initialize stream
+ @entities = {}
+ @listeners = nil
+ @parser = BaseParser.new( stream )
+ @my_stack = []
+ end
+
+ def add_listener( listener )
+ @listeners = [] unless @listeners
+ @listeners << listener
+ end
+
+ def each
+ while has_next?
+ yield self.pull
+ end
+ end
+
+ def peek depth=0
+ if @my_stack.length <= depth
+ (depth - @my_stack.length + 1).times {
+ e = PullEvent.new(@parser.pull)
+ @my_stack.push(e)
+ }
+ end
+ @my_stack[depth]
+ end
+
+ def pull
+ return @my_stack.shift if @my_stack.length > 0
+
+ event = @parser.pull
+ case event[0]
+ when :entitydecl
+ @entities[ event[1] ] =
+ event[2] unless event[2] =~ /PUBLIC|SYSTEM/
+ when :text
+ unnormalized = @parser.unnormalize( event[1], @entities )
+ event << unnormalized
+ end
+ PullEvent.new( event )
+ end
+
+ def unshift token
+ @my_stack.unshift token
+ end
+ end
+
+ # A parsing event. The contents of the event are accessed as an +Array?,
+ # and the type is given either by the ...? methods, or by accessing the
+ # +type+ accessor. The contents of this object vary from event to event,
+ # but are identical to the arguments passed to +StreamListener+s for each
+ # event.
+ class PullEvent
+ # The type of this event. Will be one of :tag_start, :tag_end, :text,
+ # :processing_instruction, :comment, :doctype, :attlistdecl, :entitydecl,
+ # :notationdecl, :entity, :cdata, :xmldecl, or :error.
+ def initialize(arg)
+ @contents = arg
+ end
+
+ def []( start, endd=nil)
+ if start.kind_of? Range
+ @contents.slice( start.begin+1 .. start.end )
+ elsif start.kind_of? Numeric
+ if endd.nil?
+ @contents.slice( start+1 )
+ else
+ @contents.slice( start+1, endd )
+ end
+ else
+ raise "Illegal argument #{start.inspect} (#{start.class})"
+ end
+ end
+
+ def event_type
+ @contents[0]
+ end
+
+ # Content: [ String tag_name, Hash attributes ]
+ def start_element?
+ @contents[0] == :start_element
+ end
+
+ # Content: [ String tag_name ]
+ def end_element?
+ @contents[0] == :end_element
+ end
+
+ # Content: [ String raw_text, String unnormalized_text ]
+ def text?
+ @contents[0] == :text
+ end
+
+ # Content: [ String text ]
+ def instruction?
+ @contents[0] == :processing_instruction
+ end
+
+ # Content: [ String text ]
+ def comment?
+ @contents[0] == :comment
+ end
+
+ # Content: [ String name, String pub_sys, String long_name, String uri ]
+ def doctype?
+ @contents[0] == :start_doctype
+ end
+
+ # Content: [ String text ]
+ def attlistdecl?
+ @contents[0] == :attlistdecl
+ end
+
+ # Content: [ String text ]
+ def elementdecl?
+ @contents[0] == :elementdecl
+ end
+
+ # Due to the wonders of DTDs, an entity declaration can be just about
+ # anything. There's no way to normalize it; you'll have to interpret the
+ # content yourself. However, the following is true:
+ #
+ # * If the entity declaration is an internal entity:
+ # [ String name, String value ]
+ # Content: [ String text ]
+ def entitydecl?
+ @contents[0] == :entitydecl
+ end
+
+ # Content: [ String text ]
+ def notationdecl?
+ @contents[0] == :notationdecl
+ end
+
+ # Content: [ String text ]
+ def entity?
+ @contents[0] == :entity
+ end
+
+ # Content: [ String text ]
+ def cdata?
+ @contents[0] == :cdata
+ end
+
+ # Content: [ String version, String encoding, String standalone ]
+ def xmldecl?
+ @contents[0] == :xmldecl
+ end
+
+ def error?
+ @contents[0] == :error
+ end
+
+ def inspect
+ @contents[0].to_s + ": " + @contents[1..-1].inspect
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/parsers/sax2parser.rb b/ruby/lib/rexml/parsers/sax2parser.rb
new file mode 100644
index 0000000..7213140
--- /dev/null
+++ b/ruby/lib/rexml/parsers/sax2parser.rb
@@ -0,0 +1,247 @@
+require 'rexml/parsers/baseparser'
+require 'rexml/parseexception'
+require 'rexml/namespace'
+require 'rexml/text'
+
+module REXML
+ module Parsers
+ # SAX2Parser
+ class SAX2Parser
+ def initialize source
+ @parser = BaseParser.new(source)
+ @listeners = []
+ @procs = []
+ @namespace_stack = []
+ @has_listeners = false
+ @tag_stack = []
+ @entities = {}
+ end
+
+ def source
+ @parser.source
+ end
+
+ def add_listener( listener )
+ @parser.add_listener( listener )
+ end
+
+ # Listen arguments:
+ #
+ # Symbol, Array, Block
+ # Listen to Symbol events on Array elements
+ # Symbol, Block
+ # Listen to Symbol events
+ # Array, Listener
+ # Listen to all events on Array elements
+ # Array, Block
+ # Listen to :start_element events on Array elements
+ # Listener
+ # Listen to All events
+ #
+ # Symbol can be one of: :start_element, :end_element,
+ # :start_prefix_mapping, :end_prefix_mapping, :characters,
+ # :processing_instruction, :doctype, :attlistdecl, :elementdecl,
+ # :entitydecl, :notationdecl, :cdata, :xmldecl, :comment
+ #
+ # There is an additional symbol that can be listened for: :progress.
+ # This will be called for every event generated, passing in the current
+ # stream position.
+ #
+ # Array contains regular expressions or strings which will be matched
+ # against fully qualified element names.
+ #
+ # Listener must implement the methods in SAX2Listener
+ #
+ # Block will be passed the same arguments as a SAX2Listener method would
+ # be, where the method name is the same as the matched Symbol.
+ # See the SAX2Listener for more information.
+ def listen( *args, &blok )
+ if args[0].kind_of? Symbol
+ if args.size == 2
+ args[1].each { |match| @procs << [args[0], match, blok] }
+ else
+ add( [args[0], nil, blok] )
+ end
+ elsif args[0].kind_of? Array
+ if args.size == 2
+ args[0].each { |match| add( [nil, match, args[1]] ) }
+ else
+ args[0].each { |match| add( [ :start_element, match, blok ] ) }
+ end
+ else
+ add([nil, nil, args[0]])
+ end
+ end
+
+ def deafen( listener=nil, &blok )
+ if listener
+ @listeners.delete_if {|item| item[-1] == listener }
+ @has_listeners = false if @listeners.size == 0
+ else
+ @procs.delete_if {|item| item[-1] == blok }
+ end
+ end
+
+ def parse
+ @procs.each { |sym,match,block| block.call if sym == :start_document }
+ @listeners.each { |sym,match,block|
+ block.start_document if sym == :start_document or sym.nil?
+ }
+ root = context = []
+ while true
+ event = @parser.pull
+ case event[0]
+ when :end_document
+ handle( :end_document )
+ break
+ when :start_doctype
+ handle( :doctype, *event[1..-1])
+ when :end_doctype
+ context = context[1]
+ when :start_element
+ @tag_stack.push(event[1])
+ # find the observers for namespaces
+ procs = get_procs( :start_prefix_mapping, event[1] )
+ listeners = get_listeners( :start_prefix_mapping, event[1] )
+ if procs or listeners
+ # break out the namespace declarations
+ # The attributes live in event[2]
+ event[2].each {|n, v| event[2][n] = @parser.normalize(v)}
+ nsdecl = event[2].find_all { |n, value| n =~ /^xmlns(:|$)/ }
+ nsdecl.collect! { |n, value| [ n[6..-1], value ] }
+ @namespace_stack.push({})
+ nsdecl.each do |n,v|
+ @namespace_stack[-1][n] = v
+ # notify observers of namespaces
+ procs.each { |ob| ob.call( n, v ) } if procs
+ listeners.each { |ob| ob.start_prefix_mapping(n, v) } if listeners
+ end
+ end
+ event[1] =~ Namespace::NAMESPLIT
+ prefix = $1
+ local = $2
+ uri = get_namespace(prefix)
+ # find the observers for start_element
+ procs = get_procs( :start_element, event[1] )
+ listeners = get_listeners( :start_element, event[1] )
+ # notify observers
+ procs.each { |ob| ob.call( uri, local, event[1], event[2] ) } if procs
+ listeners.each { |ob|
+ ob.start_element( uri, local, event[1], event[2] )
+ } if listeners
+ when :end_element
+ @tag_stack.pop
+ event[1] =~ Namespace::NAMESPLIT
+ prefix = $1
+ local = $2
+ uri = get_namespace(prefix)
+ # find the observers for start_element
+ procs = get_procs( :end_element, event[1] )
+ listeners = get_listeners( :end_element, event[1] )
+ # notify observers
+ procs.each { |ob| ob.call( uri, local, event[1] ) } if procs
+ listeners.each { |ob|
+ ob.end_element( uri, local, event[1] )
+ } if listeners
+
+ namespace_mapping = @namespace_stack.pop
+ # find the observers for namespaces
+ procs = get_procs( :end_prefix_mapping, event[1] )
+ listeners = get_listeners( :end_prefix_mapping, event[1] )
+ if procs or listeners
+ namespace_mapping.each do |ns_prefix, ns_uri|
+ # notify observers of namespaces
+ procs.each { |ob| ob.call( ns_prefix ) } if procs
+ listeners.each { |ob| ob.end_prefix_mapping(ns_prefix) } if listeners
+ end
+ end
+ when :text
+ #normalized = @parser.normalize( event[1] )
+ #handle( :characters, normalized )
+ copy = event[1].clone
+
+ esub = proc { |match|
+ if @entities.has_key?($1)
+ @entities[$1].gsub(Text::REFERENCE, &esub)
+ else
+ match
+ end
+ }
+
+ copy.gsub!( Text::REFERENCE, &esub )
+ copy.gsub!( Text::NUMERICENTITY ) {|m|
+ m=$1
+ m = "0#{m}" if m[0] == ?x
+ [Integer(m)].pack('U*')
+ }
+ handle( :characters, copy )
+ when :entitydecl
+ @entities[ event[1] ] = event[2] if event.size == 3
+ handle( *event )
+ when :processing_instruction, :comment, :attlistdecl,
+ :elementdecl, :cdata, :notationdecl, :xmldecl
+ handle( *event )
+ end
+ handle( :progress, @parser.position )
+ end
+ end
+
+ private
+ def handle( symbol, *arguments )
+ tag = @tag_stack[-1]
+ procs = get_procs( symbol, tag )
+ listeners = get_listeners( symbol, tag )
+ # notify observers
+ procs.each { |ob| ob.call( *arguments ) } if procs
+ listeners.each { |l|
+ l.send( symbol.to_s, *arguments )
+ } if listeners
+ end
+
+ # The following methods are duplicates, but it is faster than using
+ # a helper
+ def get_procs( symbol, name )
+ return nil if @procs.size == 0
+ @procs.find_all do |sym, match, block|
+ #puts sym.inspect+"=="+symbol.inspect+ "\t"+match.inspect+"=="+name.inspect+ "\t"+( (sym.nil? or symbol == sym) and ((name.nil? and match.nil?) or match.nil? or ( (name == match) or (match.kind_of? Regexp and name =~ match)))).to_s
+ (
+ (sym.nil? or symbol == sym) and
+ ((name.nil? and match.nil?) or match.nil? or (
+ (name == match) or
+ (match.kind_of? Regexp and name =~ match)
+ )
+ )
+ )
+ end.collect{|x| x[-1]}
+ end
+ def get_listeners( symbol, name )
+ return nil if @listeners.size == 0
+ @listeners.find_all do |sym, match, block|
+ (
+ (sym.nil? or symbol == sym) and
+ ((name.nil? and match.nil?) or match.nil? or (
+ (name == match) or
+ (match.kind_of? Regexp and name =~ match)
+ )
+ )
+ )
+ end.collect{|x| x[-1]}
+ end
+
+ def add( pair )
+ if pair[-1].respond_to? :call
+ @procs << pair unless @procs.include? pair
+ else
+ @listeners << pair unless @listeners.include? pair
+ @has_listeners = true
+ end
+ end
+
+ def get_namespace( prefix )
+ uris = (@namespace_stack.find_all { |ns| not ns[prefix].nil? }) ||
+ (@namespace_stack.find { |ns| not ns[nil].nil? })
+ uris[-1][prefix] unless uris.nil? or 0 == uris.size
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/parsers/streamparser.rb b/ruby/lib/rexml/parsers/streamparser.rb
new file mode 100644
index 0000000..256d0f6
--- /dev/null
+++ b/ruby/lib/rexml/parsers/streamparser.rb
@@ -0,0 +1,46 @@
+module REXML
+ module Parsers
+ class StreamParser
+ def initialize source, listener
+ @listener = listener
+ @parser = BaseParser.new( source )
+ end
+
+ def add_listener( listener )
+ @parser.add_listener( listener )
+ end
+
+ def parse
+ # entity string
+ while true
+ event = @parser.pull
+ case event[0]
+ when :end_document
+ return
+ when :start_element
+ attrs = event[2].each do |n, v|
+ event[2][n] = @parser.unnormalize( v )
+ end
+ @listener.tag_start( event[1], attrs )
+ when :end_element
+ @listener.tag_end( event[1] )
+ when :text
+ normalized = @parser.unnormalize( event[1] )
+ @listener.text( normalized )
+ when :processing_instruction
+ @listener.instruction( *event[1,2] )
+ when :start_doctype
+ @listener.doctype( *event[1..-1] )
+ when :end_doctype
+ # FIXME: remove this condition for milestone:3.2
+ @listener.doctype_end if @listener.respond_to? :doctype_end
+ when :comment, :attlistdecl, :cdata, :xmldecl, :elementdecl
+ @listener.send( event[0].to_s, *event[1..-1] )
+ when :entitydecl, :notationdecl
+ @listener.send( event[0].to_s, event[1..-1] )
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/parsers/treeparser.rb b/ruby/lib/rexml/parsers/treeparser.rb
new file mode 100644
index 0000000..30327d0
--- /dev/null
+++ b/ruby/lib/rexml/parsers/treeparser.rb
@@ -0,0 +1,100 @@
+require 'rexml/validation/validationexception'
+require 'rexml/undefinednamespaceexception'
+
+module REXML
+ module Parsers
+ class TreeParser
+ def initialize( source, build_context = Document.new )
+ @build_context = build_context
+ @parser = Parsers::BaseParser.new( source )
+ end
+
+ def add_listener( listener )
+ @parser.add_listener( listener )
+ end
+
+ def parse
+ tag_stack = []
+ in_doctype = false
+ entities = nil
+ begin
+ while true
+ event = @parser.pull
+ #STDERR.puts "TREEPARSER GOT #{event.inspect}"
+ case event[0]
+ when :end_document
+ unless tag_stack.empty?
+ #raise ParseException.new("No close tag for #{tag_stack.inspect}")
+ raise ParseException.new("No close tag for #{@build_context.xpath}")
+ end
+ return
+ when :start_element
+ tag_stack.push(event[1])
+ el = @build_context = @build_context.add_element( event[1] )
+ event[2].each do |key, value|
+ el.attributes[key]=Attribute.new(key,value,self)
+ end
+ when :end_element
+ tag_stack.pop
+ @build_context = @build_context.parent
+ when :text
+ if not in_doctype
+ if @build_context[-1].instance_of? Text
+ @build_context[-1] << event[1]
+ else
+ @build_context.add(
+ Text.new(event[1], @build_context.whitespace, nil, true)
+ ) unless (
+ @build_context.ignore_whitespace_nodes and
+ event[1].strip.size==0
+ )
+ end
+ end
+ when :comment
+ c = Comment.new( event[1] )
+ @build_context.add( c )
+ when :cdata
+ c = CData.new( event[1] )
+ @build_context.add( c )
+ when :processing_instruction
+ @build_context.add( Instruction.new( event[1], event[2] ) )
+ when :end_doctype
+ in_doctype = false
+ entities.each { |k,v| entities[k] = @build_context.entities[k].value }
+ @build_context = @build_context.parent
+ when :start_doctype
+ doctype = DocType.new( event[1..-1], @build_context )
+ @build_context = doctype
+ entities = {}
+ in_doctype = true
+ when :attlistdecl
+ n = AttlistDecl.new( event[1..-1] )
+ @build_context.add( n )
+ when :externalentity
+ n = ExternalEntity.new( event[1] )
+ @build_context.add( n )
+ when :elementdecl
+ n = ElementDecl.new( event[1] )
+ @build_context.add(n)
+ when :entitydecl
+ entities[ event[1] ] = event[2] unless event[2] =~ /PUBLIC|SYSTEM/
+ @build_context.add(Entity.new(event))
+ when :notationdecl
+ n = NotationDecl.new( *event[1..-1] )
+ @build_context.add( n )
+ when :xmldecl
+ x = XMLDecl.new( event[1], event[2], event[3] )
+ @build_context.add( x )
+ end
+ end
+ rescue REXML::Validation::ValidationException
+ raise
+ rescue REXML::UndefinedNamespaceException
+ raise
+ rescue
+ raise ParseException.new( $!.message, @parser.source, @parser, $! )
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/parsers/ultralightparser.rb b/ruby/lib/rexml/parsers/ultralightparser.rb
new file mode 100644
index 0000000..96c55d8
--- /dev/null
+++ b/ruby/lib/rexml/parsers/ultralightparser.rb
@@ -0,0 +1,56 @@
+require 'rexml/parsers/streamparser'
+require 'rexml/parsers/baseparser'
+
+module REXML
+ module Parsers
+ class UltraLightParser
+ def initialize stream
+ @stream = stream
+ @parser = REXML::Parsers::BaseParser.new( stream )
+ end
+
+ def add_listener( listener )
+ @parser.add_listener( listener )
+ end
+
+ def rewind
+ @stream.rewind
+ @parser.stream = @stream
+ end
+
+ def parse
+ root = context = []
+ while true
+ event = @parser.pull
+ case event[0]
+ when :end_document
+ break
+ when :end_doctype
+ context = context[1]
+ when :start_element, :doctype
+ context << event
+ event[1,0] = [context]
+ context = event
+ when :end_element
+ context = context[1]
+ else
+ context << event
+ end
+ end
+ root
+ end
+ end
+
+ # An element is an array. The array contains:
+ # 0 The parent element
+ # 1 The tag name
+ # 2 A hash of attributes
+ # 3..-1 The child elements
+ # An element is an array of size > 3
+ # Text is a String
+ # PIs are [ :processing_instruction, target, data ]
+ # Comments are [ :comment, data ]
+ # DocTypes are DocType structs
+ # The root is an array with XMLDecls, Text, DocType, Array, Text
+ end
+end
diff --git a/ruby/lib/rexml/parsers/xpathparser.rb b/ruby/lib/rexml/parsers/xpathparser.rb
new file mode 100644
index 0000000..49450b4
--- /dev/null
+++ b/ruby/lib/rexml/parsers/xpathparser.rb
@@ -0,0 +1,698 @@
+require 'rexml/namespace'
+require 'rexml/xmltokens'
+
+module REXML
+ module Parsers
+ # You don't want to use this class. Really. Use XPath, which is a wrapper
+ # for this class. Believe me. You don't want to poke around in here.
+ # There is strange, dark magic at work in this code. Beware. Go back! Go
+ # back while you still can!
+ class XPathParser
+ include XMLTokens
+ LITERAL = /^'([^']*)'|^"([^"]*)"/u
+
+ def namespaces=( namespaces )
+ Functions::namespace_context = namespaces
+ @namespaces = namespaces
+ end
+
+ def parse path
+ path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces
+ path.gsub!( /\s+([\]\)])/, '\1' )
+ parsed = []
+ path = OrExpr(path, parsed)
+ parsed
+ end
+
+ def predicate path
+ parsed = []
+ Predicate( "[#{path}]", parsed )
+ parsed
+ end
+
+ def abbreviate( path )
+ path = path.kind_of?(String) ? parse( path ) : path
+ string = ""
+ document = false
+ while path.size > 0
+ op = path.shift
+ case op
+ when :node
+ when :attribute
+ string << "/" if string.size > 0
+ string << "@"
+ when :child
+ string << "/" if string.size > 0
+ when :descendant_or_self
+ string << "/"
+ when :self
+ string << "."
+ when :parent
+ string << ".."
+ when :any
+ string << "*"
+ when :text
+ string << "text()"
+ when :following, :following_sibling,
+ :ancestor, :ancestor_or_self, :descendant,
+ :namespace, :preceding, :preceding_sibling
+ string << "/" unless string.size == 0
+ string << op.to_s.tr("_", "-")
+ string << "::"
+ when :qname
+ prefix = path.shift
+ name = path.shift
+ string << prefix+":" if prefix.size > 0
+ string << name
+ when :predicate
+ string << '['
+ string << predicate_to_string( path.shift ) {|x| abbreviate( x ) }
+ string << ']'
+ when :document
+ document = true
+ when :function
+ string << path.shift
+ string << "( "
+ string << predicate_to_string( path.shift[0] ) {|x| abbreviate( x )}
+ string << " )"
+ when :literal
+ string << %Q{ "#{path.shift}" }
+ else
+ string << "/" unless string.size == 0
+ string << "UNKNOWN("
+ string << op.inspect
+ string << ")"
+ end
+ end
+ string = "/"+string if document
+ return string
+ end
+
+ def expand( path )
+ path = path.kind_of?(String) ? parse( path ) : path
+ string = ""
+ document = false
+ while path.size > 0
+ op = path.shift
+ case op
+ when :node
+ string << "node()"
+ when :attribute, :child, :following, :following_sibling,
+ :ancestor, :ancestor_or_self, :descendant, :descendant_or_self,
+ :namespace, :preceding, :preceding_sibling, :self, :parent
+ string << "/" unless string.size == 0
+ string << op.to_s.tr("_", "-")
+ string << "::"
+ when :any
+ string << "*"
+ when :qname
+ prefix = path.shift
+ name = path.shift
+ string << prefix+":" if prefix.size > 0
+ string << name
+ when :predicate
+ string << '['
+ string << predicate_to_string( path.shift ) { |x| expand(x) }
+ string << ']'
+ when :document
+ document = true
+ else
+ string << "/" unless string.size == 0
+ string << "UNKNOWN("
+ string << op.inspect
+ string << ")"
+ end
+ end
+ string = "/"+string if document
+ return string
+ end
+
+ def predicate_to_string( path, &block )
+ string = ""
+ case path[0]
+ when :and, :or, :mult, :plus, :minus, :neq, :eq, :lt, :gt, :lteq, :gteq, :div, :mod, :union
+ op = path.shift
+ case op
+ when :eq
+ op = "="
+ when :lt
+ op = "<"
+ when :gt
+ op = ">"
+ when :lteq
+ op = "<="
+ when :gteq
+ op = ">="
+ when :neq
+ op = "!="
+ when :union
+ op = "|"
+ end
+ left = predicate_to_string( path.shift, &block )
+ right = predicate_to_string( path.shift, &block )
+ string << " "
+ string << left
+ string << " "
+ string << op.to_s
+ string << " "
+ string << right
+ string << " "
+ when :function
+ path.shift
+ name = path.shift
+ string << name
+ string << "( "
+ string << predicate_to_string( path.shift, &block )
+ string << " )"
+ when :literal
+ path.shift
+ string << " "
+ string << path.shift.inspect
+ string << " "
+ else
+ string << " "
+ string << yield( path )
+ string << " "
+ end
+ return string.squeeze(" ")
+ end
+
+ private
+ #LocationPath
+ # | RelativeLocationPath
+ # | '/' RelativeLocationPath?
+ # | '//' RelativeLocationPath
+ def LocationPath path, parsed
+ #puts "LocationPath '#{path}'"
+ path = path.strip
+ if path[0] == ?/
+ parsed << :document
+ if path[1] == ?/
+ parsed << :descendant_or_self
+ parsed << :node
+ path = path[2..-1]
+ else
+ path = path[1..-1]
+ end
+ end
+ #puts parsed.inspect
+ return RelativeLocationPath( path, parsed ) if path.size > 0
+ end
+
+ #RelativeLocationPath
+ # | Step
+ # | (AXIS_NAME '::' | '@' | '') AxisSpecifier
+ # NodeTest
+ # Predicate
+ # | '.' | '..' AbbreviatedStep
+ # | RelativeLocationPath '/' Step
+ # | RelativeLocationPath '//' Step
+ AXIS = /^(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/
+ def RelativeLocationPath path, parsed
+ #puts "RelativeLocationPath #{path}"
+ while path.size > 0
+ # (axis or @ or <child::>) nodetest predicate >
+ # OR > / Step
+ # (. or ..) >
+ if path[0] == ?.
+ if path[1] == ?.
+ parsed << :parent
+ parsed << :node
+ path = path[2..-1]
+ else
+ parsed << :self
+ parsed << :node
+ path = path[1..-1]
+ end
+ else
+ if path[0] == ?@
+ #puts "ATTRIBUTE"
+ parsed << :attribute
+ path = path[1..-1]
+ # Goto Nodetest
+ elsif path =~ AXIS
+ parsed << $1.tr('-','_').intern
+ path = $'
+ # Goto Nodetest
+ else
+ parsed << :child
+ end
+
+ #puts "NODETESTING '#{path}'"
+ n = []
+ path = NodeTest( path, n)
+ #puts "NODETEST RETURNED '#{path}'"
+
+ if path[0] == ?[
+ path = Predicate( path, n )
+ end
+
+ parsed.concat(n)
+ end
+
+ if path.size > 0
+ if path[0] == ?/
+ if path[1] == ?/
+ parsed << :descendant_or_self
+ parsed << :node
+ path = path[2..-1]
+ else
+ path = path[1..-1]
+ end
+ else
+ return path
+ end
+ end
+ end
+ return path
+ end
+
+ # Returns a 1-1 map of the nodeset
+ # The contents of the resulting array are either:
+ # true/false, if a positive match
+ # String, if a name match
+ #NodeTest
+ # | ('*' | NCNAME ':' '*' | QNAME) NameTest
+ # | NODE_TYPE '(' ')' NodeType
+ # | PI '(' LITERAL ')' PI
+ # | '[' expr ']' Predicate
+ NCNAMETEST= /^(#{NCNAME_STR}):\*/u
+ QNAME = Namespace::NAMESPLIT
+ NODE_TYPE = /^(comment|text|node)\(\s*\)/m
+ PI = /^processing-instruction\(/
+ def NodeTest path, parsed
+ #puts "NodeTest with #{path}"
+ res = nil
+ case path
+ when /^\*/
+ path = $'
+ parsed << :any
+ when NODE_TYPE
+ type = $1
+ path = $'
+ parsed << type.tr('-', '_').intern
+ when PI
+ path = $'
+ literal = nil
+ if path !~ /^\s*\)/
+ path =~ LITERAL
+ literal = $1
+ path = $'
+ raise ParseException.new("Missing ')' after processing instruction") if path[0] != ?)
+ path = path[1..-1]
+ end
+ parsed << :processing_instruction
+ parsed << (literal || '')
+ when NCNAMETEST
+ #puts "NCNAMETEST"
+ prefix = $1
+ path = $'
+ parsed << :namespace
+ parsed << prefix
+ when QNAME
+ #puts "QNAME"
+ prefix = $1
+ name = $2
+ path = $'
+ prefix = "" unless prefix
+ parsed << :qname
+ parsed << prefix
+ parsed << name
+ end
+ return path
+ end
+
+ # Filters the supplied nodeset on the predicate(s)
+ def Predicate path, parsed
+ #puts "PREDICATE with #{path}"
+ return nil unless path[0] == ?[
+ predicates = []
+ while path[0] == ?[
+ path, expr = get_group(path)
+ predicates << expr[1..-2] if expr
+ end
+ #puts "PREDICATES = #{predicates.inspect}"
+ predicates.each{ |pred|
+ #puts "ORING #{pred}"
+ preds = []
+ parsed << :predicate
+ parsed << preds
+ OrExpr(pred, preds)
+ }
+ #puts "PREDICATES = #{predicates.inspect}"
+ path
+ end
+
+ # The following return arrays of true/false, a 1-1 mapping of the
+ # supplied nodeset, except for axe(), which returns a filtered
+ # nodeset
+
+ #| OrExpr S 'or' S AndExpr
+ #| AndExpr
+ def OrExpr path, parsed
+ #puts "OR >>> #{path}"
+ n = []
+ rest = AndExpr( path, n )
+ #puts "OR <<< #{rest}"
+ if rest != path
+ while rest =~ /^\s*( or )/
+ n = [ :or, n, [] ]
+ rest = AndExpr( $', n[-1] )
+ end
+ end
+ if parsed.size == 0 and n.size != 0
+ parsed.replace(n)
+ elsif n.size > 0
+ parsed << n
+ end
+ rest
+ end
+
+ #| AndExpr S 'and' S EqualityExpr
+ #| EqualityExpr
+ def AndExpr path, parsed
+ #puts "AND >>> #{path}"
+ n = []
+ rest = EqualityExpr( path, n )
+ #puts "AND <<< #{rest}"
+ if rest != path
+ while rest =~ /^\s*( and )/
+ n = [ :and, n, [] ]
+ #puts "AND >>> #{rest}"
+ rest = EqualityExpr( $', n[-1] )
+ #puts "AND <<< #{rest}"
+ end
+ end
+ if parsed.size == 0 and n.size != 0
+ parsed.replace(n)
+ elsif n.size > 0
+ parsed << n
+ end
+ rest
+ end
+
+ #| EqualityExpr ('=' | '!=') RelationalExpr
+ #| RelationalExpr
+ def EqualityExpr path, parsed
+ #puts "EQUALITY >>> #{path}"
+ n = []
+ rest = RelationalExpr( path, n )
+ #puts "EQUALITY <<< #{rest}"
+ if rest != path
+ while rest =~ /^\s*(!?=)\s*/
+ if $1[0] == ?!
+ n = [ :neq, n, [] ]
+ else
+ n = [ :eq, n, [] ]
+ end
+ rest = RelationalExpr( $', n[-1] )
+ end
+ end
+ if parsed.size == 0 and n.size != 0
+ parsed.replace(n)
+ elsif n.size > 0
+ parsed << n
+ end
+ rest
+ end
+
+ #| RelationalExpr ('<' | '>' | '<=' | '>=') AdditiveExpr
+ #| AdditiveExpr
+ def RelationalExpr path, parsed
+ #puts "RELATION >>> #{path}"
+ n = []
+ rest = AdditiveExpr( path, n )
+ #puts "RELATION <<< #{rest}"
+ if rest != path
+ while rest =~ /^\s*([<>]=?)\s*/
+ if $1[0] == ?<
+ sym = "lt"
+ else
+ sym = "gt"
+ end
+ sym << "eq" if $1[-1] == ?=
+ n = [ sym.intern, n, [] ]
+ rest = AdditiveExpr( $', n[-1] )
+ end
+ end
+ if parsed.size == 0 and n.size != 0
+ parsed.replace(n)
+ elsif n.size > 0
+ parsed << n
+ end
+ rest
+ end
+
+ #| AdditiveExpr ('+' | S '-') MultiplicativeExpr
+ #| MultiplicativeExpr
+ def AdditiveExpr path, parsed
+ #puts "ADDITIVE >>> #{path}"
+ n = []
+ rest = MultiplicativeExpr( path, n )
+ #puts "ADDITIVE <<< #{rest}"
+ if rest != path
+ while rest =~ /^\s*(\+| -)\s*/
+ if $1[0] == ?+
+ n = [ :plus, n, [] ]
+ else
+ n = [ :minus, n, [] ]
+ end
+ rest = MultiplicativeExpr( $', n[-1] )
+ end
+ end
+ if parsed.size == 0 and n.size != 0
+ parsed.replace(n)
+ elsif n.size > 0
+ parsed << n
+ end
+ rest
+ end
+
+ #| MultiplicativeExpr ('*' | S ('div' | 'mod') S) UnaryExpr
+ #| UnaryExpr
+ def MultiplicativeExpr path, parsed
+ #puts "MULT >>> #{path}"
+ n = []
+ rest = UnaryExpr( path, n )
+ #puts "MULT <<< #{rest}"
+ if rest != path
+ while rest =~ /^\s*(\*| div | mod )\s*/
+ if $1[0] == ?*
+ n = [ :mult, n, [] ]
+ elsif $1.include?( "div" )
+ n = [ :div, n, [] ]
+ else
+ n = [ :mod, n, [] ]
+ end
+ rest = UnaryExpr( $', n[-1] )
+ end
+ end
+ if parsed.size == 0 and n.size != 0
+ parsed.replace(n)
+ elsif n.size > 0
+ parsed << n
+ end
+ rest
+ end
+
+ #| '-' UnaryExpr
+ #| UnionExpr
+ def UnaryExpr path, parsed
+ path =~ /^(\-*)/
+ path = $'
+ if $1 and (($1.size % 2) != 0)
+ mult = -1
+ else
+ mult = 1
+ end
+ parsed << :neg if mult < 0
+
+ #puts "UNARY >>> #{path}"
+ n = []
+ path = UnionExpr( path, n )
+ #puts "UNARY <<< #{path}"
+ parsed.concat( n )
+ path
+ end
+
+ #| UnionExpr '|' PathExpr
+ #| PathExpr
+ def UnionExpr path, parsed
+ #puts "UNION >>> #{path}"
+ n = []
+ rest = PathExpr( path, n )
+ #puts "UNION <<< #{rest}"
+ if rest != path
+ while rest =~ /^\s*(\|)\s*/
+ n = [ :union, n, [] ]
+ rest = PathExpr( $', n[-1] )
+ end
+ end
+ if parsed.size == 0 and n.size != 0
+ parsed.replace( n )
+ elsif n.size > 0
+ parsed << n
+ end
+ rest
+ end
+
+ #| LocationPath
+ #| FilterExpr ('/' | '//') RelativeLocationPath
+ def PathExpr path, parsed
+ path =~ /^\s*/
+ path = $'
+ #puts "PATH >>> #{path}"
+ n = []
+ rest = FilterExpr( path, n )
+ #puts "PATH <<< '#{rest}'"
+ if rest != path
+ if rest and rest[0] == ?/
+ return RelativeLocationPath(rest, n)
+ end
+ end
+ #puts "BEFORE WITH '#{rest}'"
+ rest = LocationPath(rest, n) if rest =~ /\A[\/\.\@\[\w_*]/
+ parsed.concat(n)
+ return rest
+ end
+
+ #| FilterExpr Predicate
+ #| PrimaryExpr
+ def FilterExpr path, parsed
+ #puts "FILTER >>> #{path}"
+ n = []
+ path = PrimaryExpr( path, n )
+ #puts "FILTER <<< #{path}"
+ path = Predicate(path, n) if path and path[0] == ?[
+ #puts "FILTER <<< #{path}"
+ parsed.concat(n)
+ path
+ end
+
+ #| VARIABLE_REFERENCE
+ #| '(' expr ')'
+ #| LITERAL
+ #| NUMBER
+ #| FunctionCall
+ VARIABLE_REFERENCE = /^\$(#{NAME_STR})/u
+ NUMBER = /^(\d*\.?\d+)/
+ NT = /^comment|text|processing-instruction|node$/
+ def PrimaryExpr path, parsed
+ arry = []
+ case path
+ when VARIABLE_REFERENCE
+ varname = $1
+ path = $'
+ parsed << :variable
+ parsed << varname
+ #arry << @variables[ varname ]
+ when /^(\w[-\w]*)(?:\()/
+ #puts "PrimaryExpr :: Function >>> #$1 -- '#$''"
+ fname = $1
+ tmp = $'
+ #puts "#{fname} =~ #{NT.inspect}"
+ return path if fname =~ NT
+ path = tmp
+ parsed << :function
+ parsed << fname
+ path = FunctionCall(path, parsed)
+ when NUMBER
+ #puts "LITERAL or NUMBER: #$1"
+ varname = $1.nil? ? $2 : $1
+ path = $'
+ parsed << :literal
+ parsed << (varname.include?('.') ? varname.to_f : varname.to_i)
+ when LITERAL
+ #puts "LITERAL or NUMBER: #$1"
+ varname = $1.nil? ? $2 : $1
+ path = $'
+ parsed << :literal
+ parsed << varname
+ when /^\(/ #/
+ path, contents = get_group(path)
+ contents = contents[1..-2]
+ n = []
+ OrExpr( contents, n )
+ parsed.concat(n)
+ end
+ path
+ end
+
+ #| FUNCTION_NAME '(' ( expr ( ',' expr )* )? ')'
+ def FunctionCall rest, parsed
+ path, arguments = parse_args(rest)
+ argset = []
+ for argument in arguments
+ args = []
+ OrExpr( argument, args )
+ argset << args
+ end
+ parsed << argset
+ path
+ end
+
+ # get_group( '[foo]bar' ) -> ['bar', '[foo]']
+ def get_group string
+ ind = 0
+ depth = 0
+ st = string[0,1]
+ en = (st == "(" ? ")" : "]")
+ begin
+ case string[ind,1]
+ when st
+ depth += 1
+ when en
+ depth -= 1
+ end
+ ind += 1
+ end while depth > 0 and ind < string.length
+ return nil unless depth==0
+ [string[ind..-1], string[0..ind-1]]
+ end
+
+ def parse_args( string )
+ arguments = []
+ ind = 0
+ inquot = false
+ inapos = false
+ depth = 1
+ begin
+ case string[ind]
+ when ?"
+ inquot = !inquot unless inapos
+ when ?'
+ inapos = !inapos unless inquot
+ else
+ unless inquot or inapos
+ case string[ind]
+ when ?(
+ depth += 1
+ if depth == 1
+ string = string[1..-1]
+ ind -= 1
+ end
+ when ?)
+ depth -= 1
+ if depth == 0
+ s = string[0,ind].strip
+ arguments << s unless s == ""
+ string = string[ind+1..-1]
+ end
+ when ?,
+ if depth == 1
+ s = string[0,ind].strip
+ arguments << s unless s == ""
+ string = string[ind+1..-1]
+ ind = -1
+ end
+ end
+ end
+ end
+ ind += 1
+ end while depth > 0 and ind < string.length
+ return nil unless depth==0
+ [string,arguments]
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/quickpath.rb b/ruby/lib/rexml/quickpath.rb
new file mode 100644
index 0000000..fd2ebdd
--- /dev/null
+++ b/ruby/lib/rexml/quickpath.rb
@@ -0,0 +1,263 @@
+require 'rexml/functions'
+require 'rexml/xmltokens'
+
+module REXML
+ class QuickPath
+ include Functions
+ include XMLTokens
+
+ EMPTY_HASH = {}
+
+ def QuickPath::first element, path, namespaces=EMPTY_HASH
+ match(element, path, namespaces)[0]
+ end
+
+ def QuickPath::each element, path, namespaces=EMPTY_HASH, &block
+ path = "*" unless path
+ match(element, path, namespaces).each( &block )
+ end
+
+ def QuickPath::match element, path, namespaces=EMPTY_HASH
+ raise "nil is not a valid xpath" unless path
+ results = nil
+ Functions::namespace_context = namespaces
+ case path
+ when /^\/([^\/]|$)/u
+ # match on root
+ path = path[1..-1]
+ return [element.root.parent] if path == ''
+ results = filter([element.root], path)
+ when /^[-\w]*::/u
+ results = filter([element], path)
+ when /^\*/u
+ results = filter(element.to_a, path)
+ when /^[\[!\w:]/u
+ # match on child
+ matches = []
+ children = element.to_a
+ results = filter(children, path)
+ else
+ results = filter([element], path)
+ end
+ return results
+ end
+
+ # Given an array of nodes it filters the array based on the path. The
+ # result is that when this method returns, the array will contain elements
+ # which match the path
+ def QuickPath::filter elements, path
+ return elements if path.nil? or path == '' or elements.size == 0
+ case path
+ when /^\/\//u # Descendant
+ return axe( elements, "descendant-or-self", $' )
+ when /^\/?\b(\w[-\w]*)\b::/u # Axe
+ axe_name = $1
+ rest = $'
+ return axe( elements, $1, $' )
+ when /^\/(?=\b([:!\w][-\.\w]*:)?[-!\*\.\w]*\b([^:(]|$)|\*)/u # Child
+ rest = $'
+ results = []
+ elements.each do |element|
+ results |= filter( element.to_a, rest )
+ end
+ return results
+ when /^\/?(\w[-\w]*)\(/u # / Function
+ return function( elements, $1, $' )
+ when Namespace::NAMESPLIT # Element name
+ name = $2
+ ns = $1
+ rest = $'
+ elements.delete_if do |element|
+ !(element.kind_of? Element and
+ (element.expanded_name == name or
+ (element.name == name and
+ element.namespace == Functions.namespace_context[ns])))
+ end
+ return filter( elements, rest )
+ when /^\/\[/u
+ matches = []
+ elements.each do |element|
+ matches |= predicate( element.to_a, path[1..-1] ) if element.kind_of? Element
+ end
+ return matches
+ when /^\[/u # Predicate
+ return predicate( elements, path )
+ when /^\/?\.\.\./u # Ancestor
+ return axe( elements, "ancestor", $' )
+ when /^\/?\.\./u # Parent
+ return filter( elements.collect{|e|e.parent}, $' )
+ when /^\/?\./u # Self
+ return filter( elements, $' )
+ when /^\*/u # Any
+ results = []
+ elements.each do |element|
+ results |= filter( [element], $' ) if element.kind_of? Element
+ #if element.kind_of? Element
+ # children = element.to_a
+ # children.delete_if { |child| !child.kind_of?(Element) }
+ # results |= filter( children, $' )
+ #end
+ end
+ return results
+ end
+ return []
+ end
+
+ def QuickPath::axe( elements, axe_name, rest )
+ matches = []
+ matches = filter( elements.dup, rest ) if axe_name =~ /-or-self$/u
+ case axe_name
+ when /^descendant/u
+ elements.each do |element|
+ matches |= filter( element.to_a, "descendant-or-self::#{rest}" ) if element.kind_of? Element
+ end
+ when /^ancestor/u
+ elements.each do |element|
+ while element.parent
+ matches << element.parent
+ element = element.parent
+ end
+ end
+ matches = filter( matches, rest )
+ when "self"
+ matches = filter( elements, rest )
+ when "child"
+ elements.each do |element|
+ matches |= filter( element.to_a, rest ) if element.kind_of? Element
+ end
+ when "attribute"
+ elements.each do |element|
+ matches << element.attributes[ rest ] if element.kind_of? Element
+ end
+ when "parent"
+ matches = filter(elements.collect{|element| element.parent}.uniq, rest)
+ when "following-sibling"
+ matches = filter(elements.collect{|element| element.next_sibling}.uniq,
+ rest)
+ when "previous-sibling"
+ matches = filter(elements.collect{|element|
+ element.previous_sibling}.uniq, rest )
+ end
+ return matches.uniq
+ end
+
+ # A predicate filters a node-set with respect to an axis to produce a
+ # new node-set. For each node in the node-set to be filtered, the
+ # PredicateExpr is evaluated with that node as the context node, with
+ # the number of nodes in the node-set as the context size, and with the
+ # proximity position of the node in the node-set with respect to the
+ # axis as the context position; if PredicateExpr evaluates to true for
+ # that node, the node is included in the new node-set; otherwise, it is
+ # not included.
+ #
+ # A PredicateExpr is evaluated by evaluating the Expr and converting
+ # the result to a boolean. If the result is a number, the result will
+ # be converted to true if the number is equal to the context position
+ # and will be converted to false otherwise; if the result is not a
+ # number, then the result will be converted as if by a call to the
+ # boolean function. Thus a location path para[3] is equivalent to
+ # para[position()=3].
+ def QuickPath::predicate( elements, path )
+ ind = 1
+ bcount = 1
+ while bcount > 0
+ bcount += 1 if path[ind] == ?[
+ bcount -= 1 if path[ind] == ?]
+ ind += 1
+ end
+ ind -= 1
+ predicate = path[1..ind-1]
+ rest = path[ind+1..-1]
+
+ # have to change 'a [=<>] b [=<>] c' into 'a [=<>] b and b [=<>] c'
+ predicate.gsub!( /([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)/u,
+ '\1 \2 \3 and \3 \4 \5' )
+ # Let's do some Ruby trickery to avoid some work:
+ predicate.gsub!( /&/u, "&&" )
+ predicate.gsub!( /=/u, "==" )
+ predicate.gsub!( /@(\w[-\w.]*)/u, 'attribute("\1")' )
+ predicate.gsub!( /\bmod\b/u, "%" )
+ predicate.gsub!( /\b(\w[-\w.]*\()/u ) {
+ fname = $1
+ fname.gsub( /-/u, "_" )
+ }
+
+ Functions.pair = [ 0, elements.size ]
+ results = []
+ elements.each do |element|
+ Functions.pair[0] += 1
+ Functions.node = element
+ res = eval( predicate )
+ case res
+ when true
+ results << element
+ when Fixnum
+ results << element if Functions.pair[0] == res
+ when String
+ results << element
+ end
+ end
+ return filter( results, rest )
+ end
+
+ def QuickPath::attribute( name )
+ return Functions.node.attributes[name] if Functions.node.kind_of? Element
+ end
+
+ def QuickPath::name()
+ return Functions.node.name if Functions.node.kind_of? Element
+ end
+
+ def QuickPath::method_missing( id, *args )
+ begin
+ Functions.send( id.id2name, *args )
+ rescue Exception
+ raise "METHOD: #{id.id2name}(#{args.join ', '})\n#{$!.message}"
+ end
+ end
+
+ def QuickPath::function( elements, fname, rest )
+ args = parse_args( elements, rest )
+ Functions.pair = [0, elements.size]
+ results = []
+ elements.each do |element|
+ Functions.pair[0] += 1
+ Functions.node = element
+ res = Functions.send( fname, *args )
+ case res
+ when true
+ results << element
+ when Fixnum
+ results << element if Functions.pair[0] == res
+ end
+ end
+ return results
+ end
+
+ def QuickPath::parse_args( element, string )
+ # /.*?(?:\)|,)/
+ arguments = []
+ buffer = ""
+ while string and string != ""
+ c = string[0]
+ string.sub!(/^./u, "")
+ case c
+ when ?,
+ # if depth = 1, then we start a new argument
+ arguments << evaluate( buffer )
+ #arguments << evaluate( string[0..count] )
+ when ?(
+ # start a new method call
+ function( element, buffer, string )
+ buffer = ""
+ when ?)
+ # close the method call and return arguments
+ return arguments
+ else
+ buffer << c
+ end
+ end
+ ""
+ end
+ end
+end
diff --git a/ruby/lib/rexml/rexml.rb b/ruby/lib/rexml/rexml.rb
new file mode 100644
index 0000000..3eff88f
--- /dev/null
+++ b/ruby/lib/rexml/rexml.rb
@@ -0,0 +1,31 @@
+# -*- encoding: utf-8 -*-
+# REXML is an XML toolkit for Ruby[http://www.ruby-lang.org], in Ruby.
+#
+# REXML is a _pure_ Ruby, XML 1.0 conforming,
+# non-validating[http://www.w3.org/TR/2004/REC-xml-20040204/#sec-conformance]
+# toolkit with an intuitive API. REXML passes 100% of the non-validating Oasis
+# tests[http://www.oasis-open.org/committees/xml-conformance/xml-test-suite.shtml],
+# and provides tree, stream, SAX2, pull, and lightweight APIs. REXML also
+# includes a full XPath[http://www.w3c.org/tr/xpath] 1.0 implementation. Since
+# Ruby 1.8, REXML is included in the standard Ruby distribution.
+#
+# Main page:: http://www.germane-software.com/software/rexml
+# Author:: Sean Russell <serATgermaneHYPHENsoftwareDOTcom>
+# Date:: 2008/019
+# Version:: 3.1.7.3
+#
+# This API documentation can be downloaded from the REXML home page, or can
+# be accessed online[http://www.germane-software.com/software/rexml_doc]
+#
+# A tutorial is available in the REXML distribution in docs/tutorial.html,
+# or can be accessed
+# online[http://www.germane-software.com/software/rexml/docs/tutorial.html]
+module REXML
+ COPYRIGHT = "Copyright © 2001-2008 Sean Russell <ser@germane-software.com>"
+ DATE = "2008/019"
+ VERSION = "3.1.7.3"
+ REVISION = "$Revision: 15141 $".gsub(/\$Revision:|\$/,'').strip
+
+ Copyright = COPYRIGHT
+ Version = VERSION
+end
diff --git a/ruby/lib/rexml/sax2listener.rb b/ruby/lib/rexml/sax2listener.rb
new file mode 100644
index 0000000..9545b08
--- /dev/null
+++ b/ruby/lib/rexml/sax2listener.rb
@@ -0,0 +1,97 @@
+module REXML
+ # A template for stream parser listeners.
+ # Note that the declarations (attlistdecl, elementdecl, etc) are trivially
+ # processed; REXML doesn't yet handle doctype entity declarations, so you
+ # have to parse them out yourself.
+ # === Missing methods from SAX2
+ # ignorable_whitespace
+ # === Methods extending SAX2
+ # +WARNING+
+ # These methods are certainly going to change, until DTDs are fully
+ # supported. Be aware of this.
+ # start_document
+ # end_document
+ # doctype
+ # elementdecl
+ # attlistdecl
+ # entitydecl
+ # notationdecl
+ # cdata
+ # xmldecl
+ # comment
+ module SAX2Listener
+ def start_document
+ end
+ def end_document
+ end
+ def start_prefix_mapping prefix, uri
+ end
+ def end_prefix_mapping prefix
+ end
+ def start_element uri, localname, qname, attributes
+ end
+ def end_element uri, localname, qname
+ end
+ def characters text
+ end
+ def processing_instruction target, data
+ end
+ # Handles a doctype declaration. Any attributes of the doctype which are
+ # not supplied will be nil. # EG, <!DOCTYPE me PUBLIC "foo" "bar">
+ # @p name the name of the doctype; EG, "me"
+ # @p pub_sys "PUBLIC", "SYSTEM", or nil. EG, "PUBLIC"
+ # @p long_name the supplied long name, or nil. EG, "foo"
+ # @p uri the uri of the doctype, or nil. EG, "bar"
+ def doctype name, pub_sys, long_name, uri
+ end
+ # If a doctype includes an ATTLIST declaration, it will cause this
+ # method to be called. The content is the declaration itself, unparsed.
+ # EG, <!ATTLIST el attr CDATA #REQUIRED> will come to this method as "el
+ # attr CDATA #REQUIRED". This is the same for all of the .*decl
+ # methods.
+ def attlistdecl(element, pairs, contents)
+ end
+ # <!ELEMENT ...>
+ def elementdecl content
+ end
+ # <!ENTITY ...>
+ # The argument passed to this method is an array of the entity
+ # declaration. It can be in a number of formats, but in general it
+ # returns (example, result):
+ # <!ENTITY % YN '"Yes"'>
+ # ["%", "YN", "'\"Yes\"'", "\""]
+ # <!ENTITY % YN 'Yes'>
+ # ["%", "YN", "'Yes'", "s"]
+ # <!ENTITY WhatHeSaid "He said %YN;">
+ # ["WhatHeSaid", "\"He said %YN;\"", "YN"]
+ # <!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
+ # ["open-hatch", "SYSTEM", "\"http://www.textuality.com/boilerplate/OpenHatch.xml\""]
+ # <!ENTITY open-hatch PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN" "http://www.textuality.com/boilerplate/OpenHatch.xml">
+ # ["open-hatch", "PUBLIC", "\"-//Textuality//TEXT Standard open-hatch boilerplate//EN\"", "\"http://www.textuality.com/boilerplate/OpenHatch.xml\""]
+ # <!ENTITY hatch-pic SYSTEM "../grafix/OpenHatch.gif" NDATA gif>
+ # ["hatch-pic", "SYSTEM", "\"../grafix/OpenHatch.gif\"", "\n\t\t\t\t\t\t\tNDATA gif", "gif"]
+ def entitydecl name, decl
+ end
+ # <!NOTATION ...>
+ def notationdecl content
+ end
+ # Called when <![CDATA[ ... ]]> is encountered in a document.
+ # @p content "..."
+ def cdata content
+ end
+ # Called when an XML PI is encountered in the document.
+ # EG: <?xml version="1.0" encoding="utf"?>
+ # @p version the version attribute value. EG, "1.0"
+ # @p encoding the encoding attribute value, or nil. EG, "utf"
+ # @p standalone the standalone attribute value, or nil. EG, nil
+ # @p spaced the declaration is followed by a line break
+ def xmldecl version, encoding, standalone
+ end
+ # Called when a comment is encountered.
+ # @p comment The content of the comment
+ def comment comment
+ end
+ def progress position
+ end
+ end
+end
diff --git a/ruby/lib/rexml/source.rb b/ruby/lib/rexml/source.rb
new file mode 100644
index 0000000..d433513
--- /dev/null
+++ b/ruby/lib/rexml/source.rb
@@ -0,0 +1,258 @@
+require 'rexml/encoding'
+
+module REXML
+ # Generates Source-s. USE THIS CLASS.
+ class SourceFactory
+ # Generates a Source object
+ # @param arg Either a String, or an IO
+ # @return a Source, or nil if a bad argument was given
+ def SourceFactory::create_from(arg)
+ if arg.respond_to? :read and
+ arg.respond_to? :readline and
+ arg.respond_to? :nil? and
+ arg.respond_to? :eof?
+ IOSource.new(arg)
+ elsif arg.respond_to? :to_str
+ require 'stringio'
+ IOSource.new(StringIO.new(arg))
+ elsif arg.kind_of? Source
+ arg
+ else
+ raise "#{arg.class} is not a valid input stream. It must walk \n"+
+ "like either a String, an IO, or a Source."
+ end
+ end
+ end
+
+ # A Source can be searched for patterns, and wraps buffers and other
+ # objects and provides consumption of text
+ class Source
+ include Encoding
+ # The current buffer (what we're going to read next)
+ attr_reader :buffer
+ # The line number of the last consumed text
+ attr_reader :line
+ attr_reader :encoding
+
+ # Constructor
+ # @param arg must be a String, and should be a valid XML document
+ # @param encoding if non-null, sets the encoding of the source to this
+ # value, overriding all encoding detection
+ def initialize(arg, encoding=nil)
+ @orig = @buffer = arg
+ if encoding
+ self.encoding = encoding
+ else
+ self.encoding = check_encoding( @buffer )
+ end
+ @line = 0
+ end
+
+
+ # Inherited from Encoding
+ # Overridden to support optimized en/decoding
+ def encoding=(enc)
+ return unless super
+ @line_break = encode( '>' )
+ if enc != UTF_8
+ @buffer = decode(@buffer)
+ @to_utf = true
+ else
+ @to_utf = false
+ if @buffer.respond_to? :force_encoding
+ @buffer.force_encoding Encoding::UTF_8
+ end
+ end
+ end
+
+ # Scans the source for a given pattern. Note, that this is not your
+ # usual scan() method. For one thing, the pattern argument has some
+ # requirements; for another, the source can be consumed. You can easily
+ # confuse this method. Originally, the patterns were easier
+ # to construct and this method more robust, because this method
+ # generated search regexes on the fly; however, this was
+ # computationally expensive and slowed down the entire REXML package
+ # considerably, since this is by far the most commonly called method.
+ # @param pattern must be a Regexp, and must be in the form of
+ # /^\s*(#{your pattern, with no groups})(.*)/. The first group
+ # will be returned; the second group is used if the consume flag is
+ # set.
+ # @param consume if true, the pattern returned will be consumed, leaving
+ # everything after it in the Source.
+ # @return the pattern, if found, or nil if the Source is empty or the
+ # pattern is not found.
+ def scan(pattern, cons=false)
+ return nil if @buffer.nil?
+ rv = @buffer.scan(pattern)
+ @buffer = $' if cons and rv.size>0
+ rv
+ end
+
+ def read
+ end
+
+ def consume( pattern )
+ @buffer = $' if pattern.match( @buffer )
+ end
+
+ def match_to( char, pattern )
+ return pattern.match(@buffer)
+ end
+
+ def match_to_consume( char, pattern )
+ md = pattern.match(@buffer)
+ @buffer = $'
+ return md
+ end
+
+ def match(pattern, cons=false)
+ md = pattern.match(@buffer)
+ @buffer = $' if cons and md
+ return md
+ end
+
+ # @return true if the Source is exhausted
+ def empty?
+ @buffer == ""
+ end
+
+ def position
+ @orig.index( @buffer )
+ end
+
+ # @return the current line in the source
+ def current_line
+ lines = @orig.split
+ res = lines.grep @buffer[0..30]
+ res = res[-1] if res.kind_of? Array
+ lines.index( res ) if res
+ end
+ end
+
+ # A Source that wraps an IO. See the Source class for method
+ # documentation
+ class IOSource < Source
+ #attr_reader :block_size
+
+ # block_size has been deprecated
+ def initialize(arg, block_size=500, encoding=nil)
+ @er_source = @source = arg
+ @to_utf = false
+
+ # Determining the encoding is a deceptively difficult issue to resolve.
+ # First, we check the first two bytes for UTF-16. Then we
+ # assume that the encoding is at least ASCII enough for the '>', and
+ # we read until we get one of those. This gives us the XML declaration,
+ # if there is one. If there isn't one, the file MUST be UTF-8, as per
+ # the XML spec. If there is one, we can determine the encoding from
+ # it.
+ @buffer = ""
+ str = @source.read( 2 ) || ''
+ if encoding
+ self.encoding = encoding
+ elsif str[0,2] == "\xfe\xff"
+ @line_break = "\000>"
+ elsif str[0,2] == "\xff\xfe"
+ @line_break = ">\000"
+ elsif str[0,2] == "\xef\xbb"
+ str += @source.read(1)
+ str = '' if (str[2,1] == "\xBF")
+ @line_break = ">"
+ else
+ @line_break = ">"
+ end
+ super( @source.eof? ? str : str+@source.readline( @line_break ) )
+ end
+
+ def scan(pattern, cons=false)
+ rv = super
+ # You'll notice that this next section is very similar to the same
+ # section in match(), but just a liiittle different. This is
+ # because it is a touch faster to do it this way with scan()
+ # than the way match() does it; enough faster to warrent duplicating
+ # some code
+ if rv.size == 0
+ until @buffer =~ pattern or @source.nil?
+ begin
+ # READLINE OPT
+ #str = @source.read(@block_size)
+ str = @source.readline(@line_break)
+ str = decode(str) if @to_utf and str
+ @buffer << str
+ rescue Iconv::IllegalSequence
+ raise
+ rescue
+ @source = nil
+ end
+ end
+ rv = super
+ end
+ rv.taint
+ rv
+ end
+
+ def read
+ begin
+ str = @source.readline(@line_break)
+ str = decode(str) if @to_utf and str
+ @buffer << str
+ if not @to_utf and @buffer.respond_to? :force_encoding
+ @buffer.force_encoding Encoding::UTF_8
+ end
+ rescue Exception, NameError
+ @source = nil
+ end
+ end
+
+ def consume( pattern )
+ match( pattern, true )
+ end
+
+ def match( pattern, cons=false )
+ rv = pattern.match(@buffer)
+ @buffer = $' if cons and rv
+ while !rv and @source
+ begin
+ str = @source.readline(@line_break)
+ str = decode(str) if @to_utf and str
+ @buffer << str
+ rv = pattern.match(@buffer)
+ @buffer = $' if cons and rv
+ rescue
+ @source = nil
+ end
+ end
+ rv.taint
+ rv
+ end
+
+ def empty?
+ super and ( @source.nil? || @source.eof? )
+ end
+
+ def position
+ @er_source.pos rescue 0
+ end
+
+ # @return the current line in the source
+ def current_line
+ begin
+ pos = @er_source.pos # The byte position in the source
+ lineno = @er_source.lineno # The XML < position in the source
+ @er_source.rewind
+ line = 0 # The \r\n position in the source
+ begin
+ while @er_source.pos < pos
+ @er_source.readline
+ line += 1
+ end
+ rescue
+ end
+ rescue IOError
+ pos = -1
+ line = -1
+ end
+ [pos, lineno, line]
+ end
+ end
+end
diff --git a/ruby/lib/rexml/streamlistener.rb b/ruby/lib/rexml/streamlistener.rb
new file mode 100644
index 0000000..3a4ef9f
--- /dev/null
+++ b/ruby/lib/rexml/streamlistener.rb
@@ -0,0 +1,92 @@
+module REXML
+ # A template for stream parser listeners.
+ # Note that the declarations (attlistdecl, elementdecl, etc) are trivially
+ # processed; REXML doesn't yet handle doctype entity declarations, so you
+ # have to parse them out yourself.
+ module StreamListener
+ # Called when a tag is encountered.
+ # @p name the tag name
+ # @p attrs an array of arrays of attribute/value pairs, suitable for
+ # use with assoc or rassoc. IE, <tag attr1="value1" attr2="value2">
+ # will result in
+ # tag_start( "tag", # [["attr1","value1"],["attr2","value2"]])
+ def tag_start name, attrs
+ end
+ # Called when the end tag is reached. In the case of <tag/>, tag_end
+ # will be called immidiately after tag_start
+ # @p the name of the tag
+ def tag_end name
+ end
+ # Called when text is encountered in the document
+ # @p text the text content.
+ def text text
+ end
+ # Called when an instruction is encountered. EG: <?xsl sheet='foo'?>
+ # @p name the instruction name; in the example, "xsl"
+ # @p instruction the rest of the instruction. In the example,
+ # "sheet='foo'"
+ def instruction name, instruction
+ end
+ # Called when a comment is encountered.
+ # @p comment The content of the comment
+ def comment comment
+ end
+ # Handles a doctype declaration. Any attributes of the doctype which are
+ # not supplied will be nil. # EG, <!DOCTYPE me PUBLIC "foo" "bar">
+ # @p name the name of the doctype; EG, "me"
+ # @p pub_sys "PUBLIC", "SYSTEM", or nil. EG, "PUBLIC"
+ # @p long_name the supplied long name, or nil. EG, "foo"
+ # @p uri the uri of the doctype, or nil. EG, "bar"
+ def doctype name, pub_sys, long_name, uri
+ end
+ # Called when the doctype is done
+ def doctype_end
+ end
+ # If a doctype includes an ATTLIST declaration, it will cause this
+ # method to be called. The content is the declaration itself, unparsed.
+ # EG, <!ATTLIST el attr CDATA #REQUIRED> will come to this method as "el
+ # attr CDATA #REQUIRED". This is the same for all of the .*decl
+ # methods.
+ def attlistdecl element_name, attributes, raw_content
+ end
+ # <!ELEMENT ...>
+ def elementdecl content
+ end
+ # <!ENTITY ...>
+ # The argument passed to this method is an array of the entity
+ # declaration. It can be in a number of formats, but in general it
+ # returns (example, result):
+ # <!ENTITY % YN '"Yes"'>
+ # ["%", "YN", "'\"Yes\"'", "\""]
+ # <!ENTITY % YN 'Yes'>
+ # ["%", "YN", "'Yes'", "s"]
+ # <!ENTITY WhatHeSaid "He said %YN;">
+ # ["WhatHeSaid", "\"He said %YN;\"", "YN"]
+ # <!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
+ # ["open-hatch", "SYSTEM", "\"http://www.textuality.com/boilerplate/OpenHatch.xml\""]
+ # <!ENTITY open-hatch PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN" "http://www.textuality.com/boilerplate/OpenHatch.xml">
+ # ["open-hatch", "PUBLIC", "\"-//Textuality//TEXT Standard open-hatch boilerplate//EN\"", "\"http://www.textuality.com/boilerplate/OpenHatch.xml\""]
+ # <!ENTITY hatch-pic SYSTEM "../grafix/OpenHatch.gif" NDATA gif>
+ # ["hatch-pic", "SYSTEM", "\"../grafix/OpenHatch.gif\"", "\n\t\t\t\t\t\t\tNDATA gif", "gif"]
+ def entitydecl content
+ end
+ # <!NOTATION ...>
+ def notationdecl content
+ end
+ # Called when %foo; is encountered in a doctype declaration.
+ # @p content "foo"
+ def entity content
+ end
+ # Called when <![CDATA[ ... ]]> is encountered in a document.
+ # @p content "..."
+ def cdata content
+ end
+ # Called when an XML PI is encountered in the document.
+ # EG: <?xml version="1.0" encoding="utf"?>
+ # @p version the version attribute value. EG, "1.0"
+ # @p encoding the encoding attribute value, or nil. EG, "utf"
+ # @p standalone the standalone attribute value, or nil. EG, nil
+ def xmldecl version, encoding, standalone
+ end
+ end
+end
diff --git a/ruby/lib/rexml/syncenumerator.rb b/ruby/lib/rexml/syncenumerator.rb
new file mode 100644
index 0000000..11609bd
--- /dev/null
+++ b/ruby/lib/rexml/syncenumerator.rb
@@ -0,0 +1,32 @@
+module REXML
+ class SyncEnumerator
+ include Enumerable
+
+ # Creates a new SyncEnumerator which enumerates rows of given
+ # Enumerable objects.
+ def initialize(*enums)
+ @gens = enums
+ @length = @gens.collect {|x| x.size }.max
+ end
+
+ # Returns the number of enumerated Enumerable objects, i.e. the size
+ # of each row.
+ def size
+ @gens.size
+ end
+
+ # Returns the number of enumerated Enumerable objects, i.e. the size
+ # of each row.
+ def length
+ @gens.length
+ end
+
+ # Enumerates rows of the Enumerable objects.
+ def each
+ @length.times {|i|
+ yield @gens.collect {|x| x[i]}
+ }
+ self
+ end
+ end
+end
diff --git a/ruby/lib/rexml/text.rb b/ruby/lib/rexml/text.rb
new file mode 100644
index 0000000..fac5ac3
--- /dev/null
+++ b/ruby/lib/rexml/text.rb
@@ -0,0 +1,404 @@
+require 'rexml/entity'
+require 'rexml/doctype'
+require 'rexml/child'
+require 'rexml/doctype'
+require 'rexml/parseexception'
+
+module REXML
+ # Represents text nodes in an XML document
+ class Text < Child
+ include Comparable
+ # The order in which the substitutions occur
+ SPECIALS = [ /&(?!#?[\w-]+;)/u, /</u, />/u, /"/u, /'/u, /\r/u ]
+ SUBSTITUTES = ['&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#13;']
+ # Characters which are substituted in written strings
+ SLAICEPS = [ '<', '>', '"', "'", '&' ]
+ SETUTITSBUS = [ /&lt;/u, /&gt;/u, /&quot;/u, /&apos;/u, /&amp;/u ]
+
+ # If +raw+ is true, then REXML leaves the value alone
+ attr_accessor :raw
+
+ NEEDS_A_SECOND_CHECK = /(<|&((#{Entity::NAME});|(#0*((?:\d+)|(?:x[a-fA-F0-9]+)));)?)/um
+ NUMERICENTITY = /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/
+ VALID_CHAR = [
+ 0x9, 0xA, 0xD,
+ (0x20..0xD7FF),
+ (0xE000..0xFFFD),
+ (0x10000..0x10FFFF)
+ ]
+
+ if String.method_defined? :encode
+ VALID_XML_CHARS = Regexp.new('^['+
+ VALID_CHAR.map { |item|
+ case item
+ when Fixnum
+ [item].pack('U').force_encoding('utf-8')
+ when Range
+ [item.first, '-'.ord, item.last].pack('UUU').force_encoding('utf-8')
+ end
+ }.join +
+ ']*$')
+ else
+ VALID_XML_CHARS = /^(
+ [\x09\x0A\x0D\x20-\x7E] # ASCII
+ | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
+ | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
+ | [\xE1-\xEC\xEE][\x80-\xBF]{2} # straight 3-byte
+ | \xEF[\x80-\xBE]{2} #
+ | \xEF\xBF[\x80-\xBD] # excluding U+fffe and U+ffff
+ | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
+ | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
+ | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
+ | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
+ )*$/nx;
+ end
+
+ # Constructor
+ # +arg+ if a String, the content is set to the String. If a Text,
+ # the object is shallowly cloned.
+ #
+ # +respect_whitespace+ (boolean, false) if true, whitespace is
+ # respected
+ #
+ # +parent+ (nil) if this is a Parent object, the parent
+ # will be set to this.
+ #
+ # +raw+ (nil) This argument can be given three values.
+ # If true, then the value of used to construct this object is expected to
+ # contain no unescaped XML markup, and REXML will not change the text. If
+ # this value is false, the string may contain any characters, and REXML will
+ # escape any and all defined entities whose values are contained in the
+ # text. If this value is nil (the default), then the raw value of the
+ # parent will be used as the raw value for this node. If there is no raw
+ # value for the parent, and no value is supplied, the default is false.
+ # Use this field if you have entities defined for some text, and you don't
+ # want REXML to escape that text in output.
+ # Text.new( "<&", false, nil, false ) #-> "&lt;&amp;"
+ # Text.new( "&lt;&amp;", false, nil, false ) #-> "&amp;lt;&amp;amp;"
+ # Text.new( "<&", false, nil, true ) #-> Parse exception
+ # Text.new( "&lt;&amp;", false, nil, true ) #-> "&lt;&amp;"
+ # # Assume that the entity "s" is defined to be "sean"
+ # # and that the entity "r" is defined to be "russell"
+ # Text.new( "sean russell" ) #-> "&s; &r;"
+ # Text.new( "sean russell", false, nil, true ) #-> "sean russell"
+ #
+ # +entity_filter+ (nil) This can be an array of entities to match in the
+ # supplied text. This argument is only useful if +raw+ is set to false.
+ # Text.new( "sean russell", false, nil, false, ["s"] ) #-> "&s; russell"
+ # Text.new( "sean russell", false, nil, true, ["s"] ) #-> "sean russell"
+ # In the last example, the +entity_filter+ argument is ignored.
+ #
+ # +pattern+ INTERNAL USE ONLY
+ def initialize(arg, respect_whitespace=false, parent=nil, raw=nil,
+ entity_filter=nil, illegal=NEEDS_A_SECOND_CHECK )
+
+ @raw = false
+
+ if parent
+ super( parent )
+ @raw = parent.raw
+ else
+ @parent = nil
+ end
+
+ @raw = raw unless raw.nil?
+ @entity_filter = entity_filter
+ @normalized = @unnormalized = nil
+
+ if arg.kind_of? String
+ @string = arg.clone
+ @string.squeeze!(" \n\t") unless respect_whitespace
+ elsif arg.kind_of? Text
+ @string = arg.to_s
+ @raw = arg.raw
+ elsif
+ raise "Illegal argument of type #{arg.type} for Text constructor (#{arg})"
+ end
+
+ @string.gsub!( /\r\n?/, "\n" )
+
+ Text.check(@string, NEEDS_A_SECOND_CHECK, doctype) if @raw and @parent
+ end
+
+ def parent= parent
+ super(parent)
+ Text.check(@string, NEEDS_A_SECOND_CHECK, doctype) if @raw and @parent
+ end
+
+ # check for illegal characters
+ def Text.check string, pattern, doctype
+
+ # illegal anywhere
+ if string !~ VALID_XML_CHARS
+ if String.method_defined? :encode
+ string.chars.each do |c|
+ case c.ord
+ when *VALID_CHAR
+ else
+ raise "Illegal character #{c.inspect} in raw string \"#{string}\""
+ end
+ end
+ else
+ string.scan(/[\x00-\x7F]|[\x80-\xBF][\xC0-\xF0]*|[\xC0-\xF0]/n) do |c|
+ case c.unpack('U')
+ when *VALID_CHAR
+ else
+ raise "Illegal character #{c.inspect} in raw string \"#{string}\""
+ end
+ end
+ end
+ end
+
+ # context sensitive
+ string.scan(pattern) do
+ if $1[-1] != ?;
+ raise "Illegal character '#{$1}' in raw string \"#{string}\""
+ elsif $1[0] == ?&
+ if $5 and $5[0] == ?#
+ case ($5[1] == ?x ? $5[2..-1].to_i(16) : $5[1..-1].to_i)
+ when *VALID_CHAR
+ else
+ raise "Illegal character '#{$1}' in raw string \"#{string}\""
+ end
+ elsif $3 and !SUBSTITUTES.include?($1)
+ if !doctype or !doctype.entities.has_key?($3)
+ raise "Undeclared entity '#{$1}' in raw string \"#{string}\""
+ end
+ end
+ end
+ end
+ end
+
+ def node_type
+ :text
+ end
+
+ def empty?
+ @string.size==0
+ end
+
+
+ def clone
+ return Text.new(self)
+ end
+
+
+ # Appends text to this text node. The text is appended in the +raw+ mode
+ # of this text node.
+ def <<( to_append )
+ @string << to_append.gsub( /\r\n?/, "\n" )
+ end
+
+
+ # +other+ a String or a Text
+ # +returns+ the result of (to_s <=> arg.to_s)
+ def <=>( other )
+ to_s() <=> other.to_s
+ end
+
+ def doctype
+ if @parent
+ doc = @parent.document
+ doc.doctype if doc
+ end
+ end
+
+ REFERENCE = /#{Entity::REFERENCE}/
+ # Returns the string value of this text node. This string is always
+ # escaped, meaning that it is a valid XML text node string, and all
+ # entities that can be escaped, have been inserted. This method respects
+ # the entity filter set in the constructor.
+ #
+ # # Assume that the entity "s" is defined to be "sean", and that the
+ # # entity "r" is defined to be "russell"
+ # t = Text.new( "< & sean russell", false, nil, false, ['s'] )
+ # t.to_s #-> "&lt; &amp; &s; russell"
+ # t = Text.new( "< & &s; russell", false, nil, false )
+ # t.to_s #-> "&lt; &amp; &s; russell"
+ # u = Text.new( "sean russell", false, nil, true )
+ # u.to_s #-> "sean russell"
+ def to_s
+ return @string if @raw
+ return @normalized if @normalized
+
+ @normalized = Text::normalize( @string, doctype, @entity_filter )
+ end
+
+ def inspect
+ @string.inspect
+ end
+
+ # Returns the string value of this text. This is the text without
+ # entities, as it might be used programmatically, or printed to the
+ # console. This ignores the 'raw' attribute setting, and any
+ # entity_filter.
+ #
+ # # Assume that the entity "s" is defined to be "sean", and that the
+ # # entity "r" is defined to be "russell"
+ # t = Text.new( "< & sean russell", false, nil, false, ['s'] )
+ # t.value #-> "< & sean russell"
+ # t = Text.new( "< & &s; russell", false, nil, false )
+ # t.value #-> "< & sean russell"
+ # u = Text.new( "sean russell", false, nil, true )
+ # u.value #-> "sean russell"
+ def value
+ return @unnormalized if @unnormalized
+ @unnormalized = Text::unnormalize( @string, doctype )
+ end
+
+ # Sets the contents of this text node. This expects the text to be
+ # unnormalized. It returns self.
+ #
+ # e = Element.new( "a" )
+ # e.add_text( "foo" ) # <a>foo</a>
+ # e[0].value = "bar" # <a>bar</a>
+ # e[0].value = "<a>" # <a>&lt;a&gt;</a>
+ def value=( val )
+ @string = val.gsub( /\r\n?/, "\n" )
+ @unnormalized = nil
+ @normalized = nil
+ @raw = false
+ end
+
+ def wrap(string, width, addnewline=false)
+ # Recursively wrap string at width.
+ return string if string.length <= width
+ place = string.rindex(' ', width) # Position in string with last ' ' before cutoff
+ if addnewline then
+ return "\n" + string[0,place] + "\n" + wrap(string[place+1..-1], width)
+ else
+ return string[0,place] + "\n" + wrap(string[place+1..-1], width)
+ end
+ end
+
+ def indent_text(string, level=1, style="\t", indentfirstline=true)
+ return string if level < 0
+ new_string = ''
+ string.each { |line|
+ indent_string = style * level
+ new_line = (indent_string + line).sub(/[\s]+$/,'')
+ new_string << new_line
+ }
+ new_string.strip! unless indentfirstline
+ return new_string
+ end
+
+ # == DEPRECATED
+ # See REXML::Formatters
+ #
+ def write( writer, indent=-1, transitive=false, ie_hack=false )
+ Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters")
+ formatter = if indent > -1
+ REXML::Formatters::Pretty.new( indent )
+ else
+ REXML::Formatters::Default.new
+ end
+ formatter.write( self, writer )
+ end
+
+ # FIXME
+ # This probably won't work properly
+ def xpath
+ path = @parent.xpath
+ path += "/text()"
+ return path
+ end
+
+ # Writes out text, substituting special characters beforehand.
+ # +out+ A String, IO, or any other object supporting <<( String )
+ # +input+ the text to substitute and the write out
+ #
+ # z=utf8.unpack("U*")
+ # ascOut=""
+ # z.each{|r|
+ # if r < 0x100
+ # ascOut.concat(r.chr)
+ # else
+ # ascOut.concat(sprintf("&#x%x;", r))
+ # end
+ # }
+ # puts ascOut
+ def write_with_substitution out, input
+ copy = input.clone
+ # Doing it like this rather than in a loop improves the speed
+ copy.gsub!( SPECIALS[0], SUBSTITUTES[0] )
+ copy.gsub!( SPECIALS[1], SUBSTITUTES[1] )
+ copy.gsub!( SPECIALS[2], SUBSTITUTES[2] )
+ copy.gsub!( SPECIALS[3], SUBSTITUTES[3] )
+ copy.gsub!( SPECIALS[4], SUBSTITUTES[4] )
+ copy.gsub!( SPECIALS[5], SUBSTITUTES[5] )
+ out << copy
+ end
+
+ # Reads text, substituting entities
+ def Text::read_with_substitution( input, illegal=nil )
+ copy = input.clone
+
+ if copy =~ illegal
+ raise ParseException.new( "malformed text: Illegal character #$& in \"#{copy}\"" )
+ end if illegal
+
+ copy.gsub!( /\r\n?/, "\n" )
+ if copy.include? ?&
+ copy.gsub!( SETUTITSBUS[0], SLAICEPS[0] )
+ copy.gsub!( SETUTITSBUS[1], SLAICEPS[1] )
+ copy.gsub!( SETUTITSBUS[2], SLAICEPS[2] )
+ copy.gsub!( SETUTITSBUS[3], SLAICEPS[3] )
+ copy.gsub!( SETUTITSBUS[4], SLAICEPS[4] )
+ copy.gsub!( /&#0*((?:\d+)|(?:x[a-f0-9]+));/ ) {
+ m=$1
+ #m='0' if m==''
+ m = "0#{m}" if m[0] == ?x
+ [Integer(m)].pack('U*')
+ }
+ end
+ copy
+ end
+
+ EREFERENCE = /&(?!#{Entity::NAME};)/
+ # Escapes all possible entities
+ def Text::normalize( input, doctype=nil, entity_filter=nil )
+ copy = input.to_s
+ # Doing it like this rather than in a loop improves the speed
+ #copy = copy.gsub( EREFERENCE, '&amp;' )
+ copy = copy.gsub( "&", "&amp;" )
+ if doctype
+ # Replace all ampersands that aren't part of an entity
+ doctype.entities.each_value do |entity|
+ copy = copy.gsub( entity.value,
+ "&#{entity.name};" ) if entity.value and
+ not( entity_filter and entity_filter.include?(entity) )
+ end
+ else
+ # Replace all ampersands that aren't part of an entity
+ DocType::DEFAULT_ENTITIES.each_value do |entity|
+ copy = copy.gsub(entity.value, "&#{entity.name};" )
+ end
+ end
+ copy
+ end
+
+ # Unescapes all possible entities
+ def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil )
+ string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) {
+ ref = $&
+ if ref[1] == ?#
+ if ref[2] == ?x
+ [ref[3...-1].to_i(16)].pack('U*')
+ else
+ [ref[2...-1].to_i].pack('U*')
+ end
+ elsif ref == '&amp;'
+ '&'
+ elsif filter and filter.include?( ref[1...-1] )
+ ref
+ elsif doctype
+ doctype.entity( ref[1...-1] ) or ref
+ else
+ entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ]
+ entity_value ? entity_value.value : ref
+ end
+ }
+ end
+ end
+end
diff --git a/ruby/lib/rexml/undefinednamespaceexception.rb b/ruby/lib/rexml/undefinednamespaceexception.rb
new file mode 100644
index 0000000..8ebfdfd
--- /dev/null
+++ b/ruby/lib/rexml/undefinednamespaceexception.rb
@@ -0,0 +1,8 @@
+require 'rexml/parseexception'
+module REXML
+ class UndefinedNamespaceException < ParseException
+ def initialize( prefix, source, parser )
+ super( "Undefined prefix #{prefix} found" )
+ end
+ end
+end
diff --git a/ruby/lib/rexml/validation/relaxng.rb b/ruby/lib/rexml/validation/relaxng.rb
new file mode 100644
index 0000000..2b86371
--- /dev/null
+++ b/ruby/lib/rexml/validation/relaxng.rb
@@ -0,0 +1,559 @@
+require "rexml/validation/validation"
+require "rexml/parsers/baseparser"
+
+module REXML
+ module Validation
+ # Implemented:
+ # * empty
+ # * element
+ # * attribute
+ # * text
+ # * optional
+ # * choice
+ # * oneOrMore
+ # * zeroOrMore
+ # * group
+ # * value
+ # * interleave
+ # * mixed
+ # * ref
+ # * grammar
+ # * start
+ # * define
+ #
+ # Not implemented:
+ # * data
+ # * param
+ # * include
+ # * externalRef
+ # * notAllowed
+ # * anyName
+ # * nsName
+ # * except
+ # * name
+ class RelaxNG
+ include Validator
+
+ INFINITY = 1.0 / 0.0
+ EMPTY = Event.new( nil )
+ TEXT = [:start_element, "text"]
+ attr_accessor :current
+ attr_accessor :count
+ attr_reader :references
+
+ # FIXME: Namespaces
+ def initialize source
+ parser = REXML::Parsers::BaseParser.new( source )
+
+ @count = 0
+ @references = {}
+ @root = @current = Sequence.new(self)
+ @root.previous = true
+ states = [ @current ]
+ begin
+ event = parser.pull
+ case event[0]
+ when :start_element
+ case event[1]
+ when "empty"
+ when "element", "attribute", "text", "value"
+ states[-1] << event
+ when "optional"
+ states << Optional.new( self )
+ states[-2] << states[-1]
+ when "choice"
+ states << Choice.new( self )
+ states[-2] << states[-1]
+ when "oneOrMore"
+ states << OneOrMore.new( self )
+ states[-2] << states[-1]
+ when "zeroOrMore"
+ states << ZeroOrMore.new( self )
+ states[-2] << states[-1]
+ when "group"
+ states << Sequence.new( self )
+ states[-2] << states[-1]
+ when "interleave"
+ states << Interleave.new( self )
+ states[-2] << states[-1]
+ when "mixed"
+ states << Interleave.new( self )
+ states[-2] << states[-1]
+ states[-1] << TEXT
+ when "define"
+ states << [ event[2]["name"] ]
+ when "ref"
+ states[-1] << Ref.new( event[2]["name"] )
+ when "anyName"
+ states << AnyName.new( self )
+ states[-2] << states[-1]
+ when "nsName"
+ when "except"
+ when "name"
+ when "data"
+ when "param"
+ when "include"
+ when "grammar"
+ when "start"
+ when "externalRef"
+ when "notAllowed"
+ end
+ when :end_element
+ case event[1]
+ when "element", "attribute"
+ states[-1] << event
+ when "zeroOrMore", "oneOrMore", "choice", "optional",
+ "interleave", "group", "mixed"
+ states.pop
+ when "define"
+ ref = states.pop
+ @references[ ref.shift ] = ref
+ #when "empty"
+ end
+ when :end_document
+ states[-1] << event
+ when :text
+ states[-1] << event
+ end
+ end while event[0] != :end_document
+ end
+
+ def receive event
+ validate( event )
+ end
+ end
+
+ class State
+ def initialize( context )
+ @previous = []
+ @events = []
+ @current = 0
+ @count = context.count += 1
+ @references = context.references
+ @value = false
+ end
+
+ def reset
+ return if @current == 0
+ @current = 0
+ @events.each {|s| s.reset if s.kind_of? State }
+ end
+
+ def previous=( previous )
+ @previous << previous
+ end
+
+ def next( event )
+ #print "In next with #{event.inspect}. "
+ #puts "Next (#@current) is #{@events[@current]}"
+ #p @previous
+ return @previous.pop.next( event ) if @events[@current].nil?
+ expand_ref_in( @events, @current ) if @events[@current].class == Ref
+ if ( @events[@current].kind_of? State )
+ @current += 1
+ @events[@current-1].previous = self
+ return @events[@current-1].next( event )
+ end
+ #puts "Current isn't a state"
+ if ( @events[@current].matches?(event) )
+ @current += 1
+ if @events[@current].nil?
+ #puts "#{inspect[0,5]} 1RETURNING #{@previous.inspect[0,5]}"
+ return @previous.pop
+ elsif @events[@current].kind_of? State
+ @current += 1
+ #puts "#{inspect[0,5]} 2RETURNING (#{@current-1}) #{@events[@current-1].inspect[0,5]}; on return, next is #{@events[@current]}"
+ @events[@current-1].previous = self
+ return @events[@current-1]
+ else
+ #puts "#{inspect[0,5]} RETURNING self w/ next(#@current) = #{@events[@current]}"
+ return self
+ end
+ else
+ return nil
+ end
+ end
+
+ def to_s
+ # Abbreviated:
+ self.class.name =~ /(?:::)(\w)\w+$/
+ # Full:
+ #self.class.name =~ /(?:::)(\w+)$/
+ "#$1.#@count"
+ end
+
+ def inspect
+ "< #{to_s} #{@events.collect{|e|
+ pre = e == @events[@current] ? '#' : ''
+ pre + e.inspect unless self == e
+ }.join(', ')} >"
+ end
+
+ def expected
+ return [@events[@current]]
+ end
+
+ def <<( event )
+ add_event_to_arry( @events, event )
+ end
+
+
+ protected
+ def expand_ref_in( arry, ind )
+ new_events = []
+ @references[ arry[ind].to_s ].each{ |evt|
+ add_event_to_arry(new_events,evt)
+ }
+ arry[ind,1] = new_events
+ end
+
+ def add_event_to_arry( arry, evt )
+ evt = generate_event( evt )
+ if evt.kind_of? String
+ arry[-1].event_arg = evt if arry[-1].kind_of? Event and @value
+ @value = false
+ else
+ arry << evt
+ end
+ end
+
+ def generate_event( event )
+ return event if event.kind_of? State or event.class == Ref
+ evt = nil
+ arg = nil
+ case event[0]
+ when :start_element
+ case event[1]
+ when "element"
+ evt = :start_element
+ arg = event[2]["name"]
+ when "attribute"
+ evt = :start_attribute
+ arg = event[2]["name"]
+ when "text"
+ evt = :text
+ when "value"
+ evt = :text
+ @value = true
+ end
+ when :text
+ return event[1]
+ when :end_document
+ return Event.new( event[0] )
+ else # then :end_element
+ case event[1]
+ when "element"
+ evt = :end_element
+ when "attribute"
+ evt = :end_attribute
+ end
+ end
+ return Event.new( evt, arg )
+ end
+ end
+
+
+ class Sequence < State
+ def matches?(event)
+ @events[@current].matches?( event )
+ end
+ end
+
+
+ class Optional < State
+ def next( event )
+ if @current == 0
+ rv = super
+ return rv if rv
+ @prior = @previous.pop
+ return @prior.next( event )
+ end
+ super
+ end
+
+ def matches?(event)
+ @events[@current].matches?(event) ||
+ (@current == 0 and @previous[-1].matches?(event))
+ end
+
+ def expected
+ return [ @prior.expected, @events[0] ].flatten if @current == 0
+ return [@events[@current]]
+ end
+ end
+
+
+ class ZeroOrMore < Optional
+ def next( event )
+ expand_ref_in( @events, @current ) if @events[@current].class == Ref
+ if ( @events[@current].matches?(event) )
+ @current += 1
+ if @events[@current].nil?
+ @current = 0
+ return self
+ elsif @events[@current].kind_of? State
+ @current += 1
+ @events[@current-1].previous = self
+ return @events[@current-1]
+ else
+ return self
+ end
+ else
+ @prior = @previous.pop
+ return @prior.next( event ) if @current == 0
+ return nil
+ end
+ end
+
+ def expected
+ return [ @prior.expected, @events[0] ].flatten if @current == 0
+ return [@events[@current]]
+ end
+ end
+
+
+ class OneOrMore < State
+ def initialize context
+ super
+ @ord = 0
+ end
+
+ def reset
+ super
+ @ord = 0
+ end
+
+ def next( event )
+ expand_ref_in( @events, @current ) if @events[@current].class == Ref
+ if ( @events[@current].matches?(event) )
+ @current += 1
+ @ord += 1
+ if @events[@current].nil?
+ @current = 0
+ return self
+ elsif @events[@current].kind_of? State
+ @current += 1
+ @events[@current-1].previous = self
+ return @events[@current-1]
+ else
+ return self
+ end
+ else
+ return @previous.pop.next( event ) if @current == 0 and @ord > 0
+ return nil
+ end
+ end
+
+ def matches?( event )
+ @events[@current].matches?(event) ||
+ (@current == 0 and @ord > 0 and @previous[-1].matches?(event))
+ end
+
+ def expected
+ if @current == 0 and @ord > 0
+ return [@previous[-1].expected, @events[0]].flatten
+ else
+ return [@events[@current]]
+ end
+ end
+ end
+
+
+ class Choice < State
+ def initialize context
+ super
+ @choices = []
+ end
+
+ def reset
+ super
+ @events = []
+ @choices.each { |c| c.each { |s| s.reset if s.kind_of? State } }
+ end
+
+ def <<( event )
+ add_event_to_arry( @choices, event )
+ end
+
+ def next( event )
+ # Make the choice if we haven't
+ if @events.size == 0
+ c = 0 ; max = @choices.size
+ while c < max
+ if @choices[c][0].class == Ref
+ expand_ref_in( @choices[c], 0 )
+ @choices += @choices[c]
+ @choices.delete( @choices[c] )
+ max -= 1
+ else
+ c += 1
+ end
+ end
+ @events = @choices.find { |evt| evt[0].matches? event }
+ # Remove the references
+ # Find the events
+ end
+ #puts "In next with #{event.inspect}."
+ #puts "events is #{@events.inspect}"
+ unless @events
+ @events = []
+ return nil
+ end
+ #puts "current = #@current"
+ super
+ end
+
+ def matches?( event )
+ return @events[@current].matches?( event ) if @events.size > 0
+ !@choices.find{|evt| evt[0].matches?(event)}.nil?
+ end
+
+ def expected
+ #puts "IN CHOICE EXPECTED"
+ #puts "EVENTS = #{@events.inspect}"
+ return [@events[@current]] if @events.size > 0
+ return @choices.collect do |x|
+ if x[0].kind_of? State
+ x[0].expected
+ else
+ x[0]
+ end
+ end.flatten
+ end
+
+ def inspect
+ "< #{to_s} #{@choices.collect{|e| e.collect{|f|f.to_s}.join(', ')}.join(' or ')} >"
+ end
+
+ protected
+ def add_event_to_arry( arry, evt )
+ if evt.kind_of? State or evt.class == Ref
+ arry << [evt]
+ elsif evt[0] == :text
+ if arry[-1] and
+ arry[-1][-1].kind_of?( Event ) and
+ arry[-1][-1].event_type == :text and @value
+
+ arry[-1][-1].event_arg = evt[1]
+ @value = false
+ end
+ else
+ arry << [] if evt[0] == :start_element
+ arry[-1] << generate_event( evt )
+ end
+ end
+ end
+
+
+ class Interleave < Choice
+ def initialize context
+ super
+ @choice = 0
+ end
+
+ def reset
+ @choice = 0
+ end
+
+ def next_current( event )
+ # Expand references
+ c = 0 ; max = @choices.size
+ while c < max
+ if @choices[c][0].class == Ref
+ expand_ref_in( @choices[c], 0 )
+ @choices += @choices[c]
+ @choices.delete( @choices[c] )
+ max -= 1
+ else
+ c += 1
+ end
+ end
+ @events = @choices[@choice..-1].find { |evt| evt[0].matches? event }
+ @current = 0
+ if @events
+ # reorder the choices
+ old = @choices[@choice]
+ idx = @choices.index( @events )
+ @choices[@choice] = @events
+ @choices[idx] = old
+ @choice += 1
+ end
+
+ #puts "In next with #{event.inspect}."
+ #puts "events is #{@events.inspect}"
+ @events = [] unless @events
+ end
+
+
+ def next( event )
+ # Find the next series
+ next_current(event) unless @events[@current]
+ return nil unless @events[@current]
+
+ expand_ref_in( @events, @current ) if @events[@current].class == Ref
+ #puts "In next with #{event.inspect}."
+ #puts "Next (#@current) is #{@events[@current]}"
+ if ( @events[@current].kind_of? State )
+ @current += 1
+ @events[@current-1].previous = self
+ return @events[@current-1].next( event )
+ end
+ #puts "Current isn't a state"
+ return @previous.pop.next( event ) if @events[@current].nil?
+ if ( @events[@current].matches?(event) )
+ @current += 1
+ if @events[@current].nil?
+ #puts "#{inspect[0,5]} 1RETURNING self" unless @choices[@choice].nil?
+ return self unless @choices[@choice].nil?
+ #puts "#{inspect[0,5]} 1RETURNING #{@previous[-1].inspect[0,5]}"
+ return @previous.pop
+ elsif @events[@current].kind_of? State
+ @current += 1
+ #puts "#{inspect[0,5]} 2RETURNING (#{@current-1}) #{@events[@current-1].inspect[0,5]}; on return, next is #{@events[@current]}"
+ @events[@current-1].previous = self
+ return @events[@current-1]
+ else
+ #puts "#{inspect[0,5]} RETURNING self w/ next(#@current) = #{@events[@current]}"
+ return self
+ end
+ else
+ return nil
+ end
+ end
+
+ def matches?( event )
+ return @events[@current].matches?( event ) if @events[@current]
+ !@choices[@choice..-1].find{|evt| evt[0].matches?(event)}.nil?
+ end
+
+ def expected
+ #puts "IN CHOICE EXPECTED"
+ #puts "EVENTS = #{@events.inspect}"
+ return [@events[@current]] if @events[@current]
+ return @choices[@choice..-1].collect do |x|
+ if x[0].kind_of? State
+ x[0].expected
+ else
+ x[0]
+ end
+ end.flatten
+ end
+
+ def inspect
+ "< #{to_s} #{@choices.collect{|e| e.collect{|f|f.to_s}.join(', ')}.join(' and ')} >"
+ end
+ end
+
+ class Ref
+ def initialize value
+ @value = value
+ end
+ def to_s
+ @value
+ end
+ def inspect
+ "{#{to_s}}"
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/validation/validation.rb b/ruby/lib/rexml/validation/validation.rb
new file mode 100644
index 0000000..93f5bfb
--- /dev/null
+++ b/ruby/lib/rexml/validation/validation.rb
@@ -0,0 +1,155 @@
+require 'rexml/validation/validationexception'
+
+module REXML
+ module Validation
+ module Validator
+ NILEVENT = [ nil ]
+ def reset
+ @current = @root
+ @root.reset
+ @root.previous = true
+ @attr_stack = []
+ self
+ end
+ def dump
+ puts @root.inspect
+ end
+ def validate( event )
+ #puts "Current: #@current"
+ #puts "Event: #{event.inspect}"
+ @attr_stack = [] unless defined? @attr_stack
+ match = @current.next(event)
+ raise ValidationException.new( "Validation error. Expected: "+
+ @current.expected.join( " or " )+" from #{@current.inspect} "+
+ " but got #{Event.new( event[0], event[1] ).inspect}" ) unless match
+ @current = match
+
+ # Check for attributes
+ case event[0]
+ when :start_element
+ #puts "Checking attributes"
+ @attr_stack << event[2]
+ begin
+ sattr = [:start_attribute, nil]
+ eattr = [:end_attribute]
+ text = [:text, nil]
+ k,v = event[2].find { |key,value|
+ sattr[1] = key
+ #puts "Looking for #{sattr.inspect}"
+ m = @current.next( sattr )
+ #puts "Got #{m.inspect}"
+ if m
+ # If the state has text children...
+ #puts "Looking for #{eattr.inspect}"
+ #puts "Expect #{m.expected}"
+ if m.matches?( eattr )
+ #puts "Got end"
+ @current = m
+ else
+ #puts "Didn't get end"
+ text[1] = value
+ #puts "Looking for #{text.inspect}"
+ m = m.next( text )
+ #puts "Got #{m.inspect}"
+ text[1] = nil
+ return false unless m
+ @current = m if m
+ end
+ m = @current.next( eattr )
+ if m
+ @current = m
+ true
+ else
+ false
+ end
+ else
+ false
+ end
+ }
+ event[2].delete(k) if k
+ end while k
+ when :end_element
+ attrs = @attr_stack.pop
+ raise ValidationException.new( "Validation error. Illegal "+
+ " attributes: #{attrs.inspect}") if attrs.length > 0
+ end
+ end
+ end
+
+ class Event
+ def initialize(event_type, event_arg=nil )
+ @event_type = event_type
+ @event_arg = event_arg
+ end
+
+ attr_reader :event_type
+ attr_accessor :event_arg
+
+ def done?
+ @done
+ end
+
+ def single?
+ return (@event_type != :start_element and @event_type != :start_attribute)
+ end
+
+ def matches?( event )
+ #puts "#@event_type =? #{event[0]} && #@event_arg =? #{event[1]} "
+ return false unless event[0] == @event_type
+ case event[0]
+ when nil
+ return true
+ when :start_element
+ return true if event[1] == @event_arg
+ when :end_element
+ return true
+ when :start_attribute
+ return true if event[1] == @event_arg
+ when :end_attribute
+ return true
+ when :end_document
+ return true
+ when :text
+ return (@event_arg.nil? or @event_arg == event[1])
+=begin
+ when :processing_instruction
+ false
+ when :xmldecl
+ false
+ when :start_doctype
+ false
+ when :end_doctype
+ false
+ when :externalentity
+ false
+ when :elementdecl
+ false
+ when :entity
+ false
+ when :attlistdecl
+ false
+ when :notationdecl
+ false
+ when :end_doctype
+ false
+=end
+ else
+ false
+ end
+ end
+
+ def ==( other )
+ return false unless other.kind_of? Event
+ @event_type == other.event_type and @event_arg == other.event_arg
+ end
+
+ def to_s
+ inspect
+ end
+
+ def inspect
+ "#{@event_type.inspect}( #@event_arg )"
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/validation/validationexception.rb b/ruby/lib/rexml/validation/validationexception.rb
new file mode 100644
index 0000000..4723d9e
--- /dev/null
+++ b/ruby/lib/rexml/validation/validationexception.rb
@@ -0,0 +1,9 @@
+module REXML
+ module Validation
+ class ValidationException < RuntimeError
+ def initialize msg
+ super
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rexml/xmldecl.rb b/ruby/lib/rexml/xmldecl.rb
new file mode 100644
index 0000000..361e4b7
--- /dev/null
+++ b/ruby/lib/rexml/xmldecl.rb
@@ -0,0 +1,119 @@
+require 'rexml/encoding'
+require 'rexml/source'
+
+module REXML
+ # NEEDS DOCUMENTATION
+ class XMLDecl < Child
+ include Encoding
+
+ DEFAULT_VERSION = "1.0";
+ DEFAULT_ENCODING = "UTF-8";
+ DEFAULT_STANDALONE = "no";
+ START = '<\?xml';
+ STOP = '\?>';
+
+ attr_accessor :version, :standalone
+ attr_reader :writeencoding, :writethis
+
+ def initialize(version=DEFAULT_VERSION, encoding=nil, standalone=nil)
+ @writethis = true
+ @writeencoding = !encoding.nil?
+ if version.kind_of? XMLDecl
+ super()
+ @version = version.version
+ self.encoding = version.encoding
+ @writeencoding = version.writeencoding
+ @standalone = version.standalone
+ else
+ super()
+ @version = version
+ self.encoding = encoding
+ @standalone = standalone
+ end
+ @version = DEFAULT_VERSION if @version.nil?
+ end
+
+ def clone
+ XMLDecl.new(self)
+ end
+
+ # indent::
+ # Ignored. There must be no whitespace before an XML declaration
+ # transitive::
+ # Ignored
+ # ie_hack::
+ # Ignored
+ def write(writer, indent=-1, transitive=false, ie_hack=false)
+ return nil unless @writethis or writer.kind_of? Output
+ writer << START.sub(/\\/u, '')
+ if writer.kind_of? Output
+ writer << " #{content writer.encoding}"
+ else
+ writer << " #{content encoding}"
+ end
+ writer << STOP.sub(/\\/u, '')
+ end
+
+ def ==( other )
+ other.kind_of?(XMLDecl) and
+ other.version == @version and
+ other.encoding == self.encoding and
+ other.standalone == @standalone
+ end
+
+ def xmldecl version, encoding, standalone
+ @version = version
+ self.encoding = encoding
+ @standalone = standalone
+ end
+
+ def node_type
+ :xmldecl
+ end
+
+ alias :stand_alone? :standalone
+ alias :old_enc= :encoding=
+
+ def encoding=( enc )
+ if enc.nil?
+ self.old_enc = "UTF-8"
+ @writeencoding = false
+ else
+ self.old_enc = enc
+ @writeencoding = true
+ end
+ self.dowrite
+ end
+
+ # Only use this if you do not want the XML declaration to be written;
+ # this object is ignored by the XML writer. Otherwise, instantiate your
+ # own XMLDecl and add it to the document.
+ #
+ # Note that XML 1.1 documents *must* include an XML declaration
+ def XMLDecl.default
+ rv = XMLDecl.new( "1.0" )
+ rv.nowrite
+ rv
+ end
+
+ def nowrite
+ @writethis = false
+ end
+
+ def dowrite
+ @writethis = true
+ end
+
+ def inspect
+ START.sub(/\\/u, '') + " ... " + STOP.sub(/\\/u, '')
+ end
+
+ private
+ def content(enc)
+ rv = "version='#@version'"
+ rv << " encoding='#{enc}'" if @writeencoding || enc !~ /utf-8/i
+ rv << " standalone='#@standalone'" if @standalone
+ rv
+ end
+ end
+end
diff --git a/ruby/lib/rexml/xmltokens.rb b/ruby/lib/rexml/xmltokens.rb
new file mode 100644
index 0000000..83efeb0
--- /dev/null
+++ b/ruby/lib/rexml/xmltokens.rb
@@ -0,0 +1,18 @@
+module REXML
+ # Defines a number of tokens used for parsing XML. Not for general
+ # consumption.
+ module XMLTokens
+ NCNAME_STR= '[\w:][\-\w\d.]*'
+ NAME_STR= "(?:#{NCNAME_STR}:)?#{NCNAME_STR}"
+
+ NAMECHAR = '[\-\w\d\.:]'
+ NAME = "([\\w:]#{NAMECHAR}*)"
+ NMTOKEN = "(?:#{NAMECHAR})+"
+ NMTOKENS = "#{NMTOKEN}(\\s+#{NMTOKEN})*"
+ REFERENCE = "(?:&#{NAME};|&#\\d+;|&#x[0-9a-fA-F]+;)"
+
+ #REFERENCE = "(?:#{ENTITYREF}|#{CHARREF})"
+ #ENTITYREF = "&#{NAME};"
+ #CHARREF = "&#\\d+;|&#x[0-9a-fA-F]+;"
+ end
+end
diff --git a/ruby/lib/rexml/xpath.rb b/ruby/lib/rexml/xpath.rb
new file mode 100644
index 0000000..b22969e
--- /dev/null
+++ b/ruby/lib/rexml/xpath.rb
@@ -0,0 +1,77 @@
+require 'rexml/functions'
+require 'rexml/xpath_parser'
+
+module REXML
+ # Wrapper class. Use this class to access the XPath functions.
+ class XPath
+ include Functions
+ EMPTY_HASH = {}
+
+ # Finds and returns the first node that matches the supplied xpath.
+ # element::
+ # The context element
+ # path::
+ # The xpath to search for. If not supplied or nil, returns the first
+ # node matching '*'.
+ # namespaces::
+ # If supplied, a Hash which defines a namespace mapping.
+ # variables::
+ # If supplied, a Hash which maps $variables in the query
+ # to values. This can be used to avoid XPath injection attacks
+ # or to automatically handle escaping string values.
+ #
+ # XPath.first( node )
+ # XPath.first( doc, "//b"} )
+ # XPath.first( node, "a/x:b", { "x"=>"http://doofus" } )
+ # XPath.first( node, '/book/publisher/text()=$publisher', {}, {"publisher"=>"O'Reilly"})
+ def XPath::first element, path=nil, namespaces=nil, variables={}
+ raise "The namespaces argument, if supplied, must be a hash object." unless namespaces.nil? or namespaces.kind_of?(Hash)
+ raise "The variables argument, if supplied, must be a hash object." unless variables.kind_of?(Hash)
+ parser = XPathParser.new
+ parser.namespaces = namespaces
+ parser.variables = variables
+ path = "*" unless path
+ element = [element] unless element.kind_of? Array
+ parser.parse(path, element).flatten[0]
+ end
+
+ # Iterates over nodes that match the given path, calling the supplied
+ # block with the match.
+ # element::
+ # The context element
+ # path::
+ # The xpath to search for. If not supplied or nil, defaults to '*'
+ # namespaces::
+ # If supplied, a Hash which defines a namespace mapping
+ # variables::
+ # If supplied, a Hash which maps $variables in the query
+ # to values. This can be used to avoid XPath injection attacks
+ # or to automatically handle escaping string values.
+ #
+ # XPath.each( node ) { |el| ... }
+ # XPath.each( node, '/*[@attr='v']' ) { |el| ... }
+ # XPath.each( node, 'ancestor::x' ) { |el| ... }
+ # XPath.each( node, '/book/publisher/text()=$publisher', {}, {"publisher"=>"O'Reilly"}) \
+ # {|el| ... }
+ def XPath::each element, path=nil, namespaces=nil, variables={}, &block
+ raise "The namespaces argument, if supplied, must be a hash object." unless namespaces.nil? or namespaces.kind_of?(Hash)
+ raise "The variables argument, if supplied, must be a hash object." unless variables.kind_of?(Hash)
+ parser = XPathParser.new
+ parser.namespaces = namespaces
+ parser.variables = variables
+ path = "*" unless path
+ element = [element] unless element.kind_of? Array
+ parser.parse(path, element).each( &block )
+ end
+
+ # Returns an array of nodes matching a given XPath.
+ def XPath::match element, path=nil, namespaces=nil, variables={}
+ parser = XPathParser.new
+ parser.namespaces = namespaces
+ parser.variables = variables
+ path = "*" unless path
+ element = [element] unless element.kind_of? Array
+ parser.parse(path,element)
+ end
+ end
+end
diff --git a/ruby/lib/rexml/xpath_parser.rb b/ruby/lib/rexml/xpath_parser.rb
new file mode 100644
index 0000000..ead5ada
--- /dev/null
+++ b/ruby/lib/rexml/xpath_parser.rb
@@ -0,0 +1,792 @@
+require 'rexml/namespace'
+require 'rexml/xmltokens'
+require 'rexml/attribute'
+require 'rexml/syncenumerator'
+require 'rexml/parsers/xpathparser'
+
+class Object
+ def dclone
+ clone
+ end
+end
+class Symbol
+ def dclone ; self ; end
+end
+class Fixnum
+ def dclone ; self ; end
+end
+class Float
+ def dclone ; self ; end
+end
+class Array
+ def dclone
+ klone = self.clone
+ klone.clear
+ self.each{|v| klone << v.dclone}
+ klone
+ end
+end
+
+module REXML
+ # You don't want to use this class. Really. Use XPath, which is a wrapper
+ # for this class. Believe me. You don't want to poke around in here.
+ # There is strange, dark magic at work in this code. Beware. Go back! Go
+ # back while you still can!
+ class XPathParser
+ include XMLTokens
+ LITERAL = /^'([^']*)'|^"([^"]*)"/u
+
+ def initialize( )
+ @parser = REXML::Parsers::XPathParser.new
+ @namespaces = nil
+ @variables = {}
+ end
+
+ def namespaces=( namespaces={} )
+ Functions::namespace_context = namespaces
+ @namespaces = namespaces
+ end
+
+ def variables=( vars={} )
+ Functions::variables = vars
+ @variables = vars
+ end
+
+ def parse path, nodeset
+ #puts "#"*40
+ path_stack = @parser.parse( path )
+ #puts "PARSE: #{path} => #{path_stack.inspect}"
+ #puts "PARSE: nodeset = #{nodeset.inspect}"
+ match( path_stack, nodeset )
+ end
+
+ def get_first path, nodeset
+ #puts "#"*40
+ path_stack = @parser.parse( path )
+ #puts "PARSE: #{path} => #{path_stack.inspect}"
+ #puts "PARSE: nodeset = #{nodeset.inspect}"
+ first( path_stack, nodeset )
+ end
+
+ def predicate path, nodeset
+ path_stack = @parser.parse( path )
+ expr( path_stack, nodeset )
+ end
+
+ def []=( variable_name, value )
+ @variables[ variable_name ] = value
+ end
+
+
+ # Performs a depth-first (document order) XPath search, and returns the
+ # first match. This is the fastest, lightest way to return a single result.
+ #
+ # FIXME: This method is incomplete!
+ def first( path_stack, node )
+ #puts "#{depth}) Entering match( #{path.inspect}, #{tree.inspect} )"
+ return nil if path.size == 0
+
+ case path[0]
+ when :document
+ # do nothing
+ return first( path[1..-1], node )
+ when :child
+ for c in node.children
+ #puts "#{depth}) CHILD checking #{name(c)}"
+ r = first( path[1..-1], c )
+ #puts "#{depth}) RETURNING #{r.inspect}" if r
+ return r if r
+ end
+ when :qname
+ name = path[2]
+ #puts "#{depth}) QNAME #{name(tree)} == #{name} (path => #{path.size})"
+ if node.name == name
+ #puts "#{depth}) RETURNING #{tree.inspect}" if path.size == 3
+ return node if path.size == 3
+ return first( path[3..-1], node )
+ else
+ return nil
+ end
+ when :descendant_or_self
+ r = first( path[1..-1], node )
+ return r if r
+ for c in node.children
+ r = first( path, c )
+ return r if r
+ end
+ when :node
+ return first( path[1..-1], node )
+ when :any
+ return first( path[1..-1], node )
+ end
+ return nil
+ end
+
+
+ def match( path_stack, nodeset )
+ #puts "MATCH: path_stack = #{path_stack.inspect}"
+ #puts "MATCH: nodeset = #{nodeset.inspect}"
+ r = expr( path_stack, nodeset )
+ #puts "MAIN EXPR => #{r.inspect}"
+ r
+ end
+
+ private
+
+
+ # Returns a String namespace for a node, given a prefix
+ # The rules are:
+ #
+ # 1. Use the supplied namespace mapping first.
+ # 2. If no mapping was supplied, use the context node to look up the namespace
+ def get_namespace( node, prefix )
+ if @namespaces
+ return @namespaces[prefix] || ''
+ else
+ return node.namespace( prefix ) if node.node_type == :element
+ return ''
+ end
+ end
+
+
+ # Expr takes a stack of path elements and a set of nodes (either a Parent
+ # or an Array and returns an Array of matching nodes
+ ALL = [ :attribute, :element, :text, :processing_instruction, :comment ]
+ ELEMENTS = [ :element ]
+ def expr( path_stack, nodeset, context=nil )
+ #puts "#"*15
+ #puts "In expr with #{path_stack.inspect}"
+ #puts "Returning" if path_stack.length == 0 || nodeset.length == 0
+ node_types = ELEMENTS
+ return nodeset if path_stack.length == 0 || nodeset.length == 0
+ while path_stack.length > 0
+ #puts "#"*5
+ #puts "Path stack = #{path_stack.inspect}"
+ #puts "Nodeset is #{nodeset.inspect}"
+ if nodeset.length == 0
+ path_stack.clear
+ return []
+ end
+ case (op = path_stack.shift)
+ when :document
+ nodeset = [ nodeset[0].root_node ]
+ #puts ":document, nodeset = #{nodeset.inspect}"
+
+ when :qname
+ #puts "IN QNAME"
+ prefix = path_stack.shift
+ name = path_stack.shift
+ nodeset.delete_if do |node|
+ # FIXME: This DOUBLES the time XPath searches take
+ ns = get_namespace( node, prefix )
+ #puts "NS = #{ns.inspect}"
+ #puts "node.node_type == :element => #{node.node_type == :element}"
+ if node.node_type == :element
+ #puts "node.name == #{name} => #{node.name == name}"
+ if node.name == name
+ #puts "node.namespace == #{ns.inspect} => #{node.namespace == ns}"
+ end
+ end
+ !(node.node_type == :element and
+ node.name == name and
+ node.namespace == ns )
+ end
+ node_types = ELEMENTS
+
+ when :any
+ #puts "ANY 1: nodeset = #{nodeset.inspect}"
+ #puts "ANY 1: node_types = #{node_types.inspect}"
+ nodeset.delete_if { |node| !node_types.include?(node.node_type) }
+ #puts "ANY 2: nodeset = #{nodeset.inspect}"
+
+ when :self
+ # This space left intentionally blank
+
+ when :processing_instruction
+ target = path_stack.shift
+ nodeset.delete_if do |node|
+ (node.node_type != :processing_instruction) or
+ ( target!='' and ( node.target != target ) )
+ end
+
+ when :text
+ nodeset.delete_if { |node| node.node_type != :text }
+
+ when :comment
+ nodeset.delete_if { |node| node.node_type != :comment }
+
+ when :node
+ # This space left intentionally blank
+ node_types = ALL
+
+ when :child
+ new_nodeset = []
+ nt = nil
+ nodeset.each do |node|
+ nt = node.node_type
+ new_nodeset += node.children if nt == :element or nt == :document
+ end
+ nodeset = new_nodeset
+ node_types = ELEMENTS
+
+ when :literal
+ return path_stack.shift
+
+ when :attribute
+ new_nodeset = []
+ case path_stack.shift
+ when :qname
+ prefix = path_stack.shift
+ name = path_stack.shift
+ for element in nodeset
+ if element.node_type == :element
+ #puts "Element name = #{element.name}"
+ #puts "get_namespace( #{element.inspect}, #{prefix} ) = #{get_namespace(element, prefix)}"
+ attrib = element.attribute( name, get_namespace(element, prefix) )
+ #puts "attrib = #{attrib.inspect}"
+ new_nodeset << attrib if attrib
+ end
+ end
+ when :any
+ #puts "ANY"
+ for element in nodeset
+ if element.node_type == :element
+ new_nodeset += element.attributes.to_a
+ end
+ end
+ end
+ nodeset = new_nodeset
+
+ when :parent
+ #puts "PARENT 1: nodeset = #{nodeset}"
+ nodeset = nodeset.collect{|n| n.parent}.compact
+ #nodeset = expr(path_stack.dclone, nodeset.collect{|n| n.parent}.compact)
+ #puts "PARENT 2: nodeset = #{nodeset.inspect}"
+ node_types = ELEMENTS
+
+ when :ancestor
+ new_nodeset = []
+ nodeset.each do |node|
+ while node.parent
+ node = node.parent
+ new_nodeset << node unless new_nodeset.include? node
+ end
+ end
+ nodeset = new_nodeset
+ node_types = ELEMENTS
+
+ when :ancestor_or_self
+ new_nodeset = []
+ nodeset.each do |node|
+ if node.node_type == :element
+ new_nodeset << node
+ while ( node.parent )
+ node = node.parent
+ new_nodeset << node unless new_nodeset.include? node
+ end
+ end
+ end
+ nodeset = new_nodeset
+ node_types = ELEMENTS
+
+ when :predicate
+ new_nodeset = []
+ subcontext = { :size => nodeset.size }
+ pred = path_stack.shift
+ nodeset.each_with_index { |node, index|
+ subcontext[ :node ] = node
+ #puts "PREDICATE SETTING CONTEXT INDEX TO #{index+1}"
+ subcontext[ :index ] = index+1
+ pc = pred.dclone
+ #puts "#{node.hash}) Recursing with #{pred.inspect} and [#{node.inspect}]"
+ result = expr( pc, [node], subcontext )
+ result = result[0] if result.kind_of? Array and result.length == 1
+ #puts "#{node.hash}) Result = #{result.inspect} (#{result.class.name})"
+ if result.kind_of? Numeric
+ #puts "Adding node #{node.inspect}" if result == (index+1)
+ new_nodeset << node if result == (index+1)
+ elsif result.instance_of? Array
+ if result.size > 0 and result.inject(false) {|k,s| s or k}
+ #puts "Adding node #{node.inspect}" if result.size > 0
+ new_nodeset << node if result.size > 0
+ end
+ else
+ #puts "Adding node #{node.inspect}" if result
+ new_nodeset << node if result
+ end
+ }
+ #puts "New nodeset = #{new_nodeset.inspect}"
+ #puts "Path_stack = #{path_stack.inspect}"
+ nodeset = new_nodeset
+=begin
+ predicate = path_stack.shift
+ ns = nodeset.clone
+ result = expr( predicate, ns )
+ #puts "Result = #{result.inspect} (#{result.class.name})"
+ #puts "nodeset = #{nodeset.inspect}"
+ if result.kind_of? Array
+ nodeset = result.zip(ns).collect{|m,n| n if m}.compact
+ else
+ nodeset = result ? nodeset : []
+ end
+ #puts "Outgoing NS = #{nodeset.inspect}"
+=end
+
+ when :descendant_or_self
+ rv = descendant_or_self( path_stack, nodeset )
+ path_stack.clear
+ nodeset = rv
+ node_types = ELEMENTS
+
+ when :descendant
+ results = []
+ nt = nil
+ nodeset.each do |node|
+ nt = node.node_type
+ results += expr( path_stack.dclone.unshift( :descendant_or_self ),
+ node.children ) if nt == :element or nt == :document
+ end
+ nodeset = results
+ node_types = ELEMENTS
+
+ when :following_sibling
+ #puts "FOLLOWING_SIBLING 1: nodeset = #{nodeset}"
+ results = []
+ nodeset.each do |node|
+ next if node.parent.nil?
+ all_siblings = node.parent.children
+ current_index = all_siblings.index( node )
+ following_siblings = all_siblings[ current_index+1 .. -1 ]
+ results += expr( path_stack.dclone, following_siblings )
+ end
+ #puts "FOLLOWING_SIBLING 2: nodeset = #{nodeset}"
+ nodeset = results
+
+ when :preceding_sibling
+ results = []
+ nodeset.each do |node|
+ next if node.parent.nil?
+ all_siblings = node.parent.children
+ current_index = all_siblings.index( node )
+ preceding_siblings = all_siblings[ 0, current_index ].reverse
+ results += preceding_siblings
+ end
+ nodeset = results
+ node_types = ELEMENTS
+
+ when :preceding
+ new_nodeset = []
+ nodeset.each do |node|
+ new_nodeset += preceding( node )
+ end
+ #puts "NEW NODESET => #{new_nodeset.inspect}"
+ nodeset = new_nodeset
+ node_types = ELEMENTS
+
+ when :following
+ new_nodeset = []
+ nodeset.each do |node|
+ new_nodeset += following( node )
+ end
+ nodeset = new_nodeset
+ node_types = ELEMENTS
+
+ when :namespace
+ #puts "In :namespace"
+ new_nodeset = []
+ prefix = path_stack.shift
+ nodeset.each do |node|
+ if (node.node_type == :element or node.node_type == :attribute)
+ if @namespaces
+ namespaces = @namespaces
+ elsif (node.node_type == :element)
+ namespaces = node.namespaces
+ else
+ namespaces = node.element.namesapces
+ end
+ #puts "Namespaces = #{namespaces.inspect}"
+ #puts "Prefix = #{prefix.inspect}"
+ #puts "Node.namespace = #{node.namespace}"
+ if (node.namespace == namespaces[prefix])
+ new_nodeset << node
+ end
+ end
+ end
+ nodeset = new_nodeset
+
+ when :variable
+ var_name = path_stack.shift
+ return @variables[ var_name ]
+
+ # :and, :or, :eq, :neq, :lt, :lteq, :gt, :gteq
+ # TODO: Special case for :or and :and -- not evaluate the right
+ # operand if the left alone determines result (i.e. is true for
+ # :or and false for :and).
+ when :eq, :neq, :lt, :lteq, :gt, :gteq, :or
+ left = expr( path_stack.shift, nodeset.dup, context )
+ #puts "LEFT => #{left.inspect} (#{left.class.name})"
+ right = expr( path_stack.shift, nodeset.dup, context )
+ #puts "RIGHT => #{right.inspect} (#{right.class.name})"
+ res = equality_relational_compare( left, op, right )
+ #puts "RES => #{res.inspect}"
+ return res
+
+ when :and
+ left = expr( path_stack.shift, nodeset.dup, context )
+ #puts "LEFT => #{left.inspect} (#{left.class.name})"
+ if left == false || left.nil? || !left.inject(false) {|a,b| a | b}
+ return []
+ end
+ right = expr( path_stack.shift, nodeset.dup, context )
+ #puts "RIGHT => #{right.inspect} (#{right.class.name})"
+ res = equality_relational_compare( left, op, right )
+ #puts "RES => #{res.inspect}"
+ return res
+
+ when :div
+ left = Functions::number(expr(path_stack.shift, nodeset, context)).to_f
+ right = Functions::number(expr(path_stack.shift, nodeset, context)).to_f
+ return (left / right)
+
+ when :mod
+ left = Functions::number(expr(path_stack.shift, nodeset, context )).to_f
+ right = Functions::number(expr(path_stack.shift, nodeset, context )).to_f
+ return (left % right)
+
+ when :mult
+ left = Functions::number(expr(path_stack.shift, nodeset, context )).to_f
+ right = Functions::number(expr(path_stack.shift, nodeset, context )).to_f
+ return (left * right)
+
+ when :plus
+ left = Functions::number(expr(path_stack.shift, nodeset, context )).to_f
+ right = Functions::number(expr(path_stack.shift, nodeset, context )).to_f
+ return (left + right)
+
+ when :minus
+ left = Functions::number(expr(path_stack.shift, nodeset, context )).to_f
+ right = Functions::number(expr(path_stack.shift, nodeset, context )).to_f
+ return (left - right)
+
+ when :union
+ left = expr( path_stack.shift, nodeset, context )
+ right = expr( path_stack.shift, nodeset, context )
+ return (left | right)
+
+ when :neg
+ res = expr( path_stack, nodeset, context )
+ return -(res.to_f)
+
+ when :not
+ when :function
+ func_name = path_stack.shift.tr('-','_')
+ arguments = path_stack.shift
+ #puts "FUNCTION 0: #{func_name}(#{arguments.collect{|a|a.inspect}.join(', ')})"
+ subcontext = context ? nil : { :size => nodeset.size }
+
+ res = []
+ cont = context
+ nodeset.each_with_index { |n, i|
+ if subcontext
+ subcontext[:node] = n
+ subcontext[:index] = i
+ cont = subcontext
+ end
+ arg_clone = arguments.dclone
+ args = arg_clone.collect { |arg|
+ #puts "FUNCTION 1: Calling expr( #{arg.inspect}, [#{n.inspect}] )"
+ expr( arg, [n], cont )
+ }
+ #puts "FUNCTION 2: #{func_name}(#{args.collect{|a|a.inspect}.join(', ')})"
+ Functions.context = cont
+ res << Functions.send( func_name, *args )
+ #puts "FUNCTION 3: #{res[-1].inspect}"
+ }
+ return res
+
+ end
+ end # while
+ #puts "EXPR returning #{nodeset.inspect}"
+ return nodeset
+ end
+
+
+ ##########################################################
+ # FIXME
+ # The next two methods are BAD MOJO!
+ # This is my achilles heel. If anybody thinks of a better
+ # way of doing this, be my guest. This really sucks, but
+ # it is a wonder it works at all.
+ # ########################################################
+
+ def descendant_or_self( path_stack, nodeset )
+ rs = []
+ #puts "#"*80
+ #puts "PATH_STACK = #{path_stack.inspect}"
+ #puts "NODESET = #{nodeset.collect{|n|n.inspect}.inspect}"
+ d_o_s( path_stack, nodeset, rs )
+ #puts "RS = #{rs.collect{|n|n.inspect}.inspect}"
+ document_order(rs.flatten.compact)
+ #rs.flatten.compact
+ end
+
+ def d_o_s( p, ns, r )
+ #puts "IN DOS with #{ns.inspect}; ALREADY HAVE #{r.inspect}"
+ nt = nil
+ ns.each_index do |i|
+ n = ns[i]
+ #puts "P => #{p.inspect}"
+ x = expr( p.dclone, [ n ] )
+ nt = n.node_type
+ d_o_s( p, n.children, x ) if nt == :element or nt == :document and n.children.size > 0
+ r.concat(x) if x.size > 0
+ end
+ end
+
+
+ # Reorders an array of nodes so that they are in document order
+ # It tries to do this efficiently.
+ #
+ # FIXME: I need to get rid of this, but the issue is that most of the XPath
+ # interpreter functions as a filter, which means that we lose context going
+ # in and out of function calls. If I knew what the index of the nodes was,
+ # I wouldn't have to do this. Maybe add a document IDX for each node?
+ # Problems with mutable documents. Or, rewrite everything.
+ def document_order( array_of_nodes )
+ new_arry = []
+ array_of_nodes.each { |node|
+ node_idx = []
+ np = node.node_type == :attribute ? node.element : node
+ while np.parent and np.parent.node_type == :element
+ node_idx << np.parent.index( np )
+ np = np.parent
+ end
+ new_arry << [ node_idx.reverse, node ]
+ }
+ #puts "new_arry = #{new_arry.inspect}"
+ new_arry.sort{ |s1, s2| s1[0] <=> s2[0] }.collect{ |s| s[1] }
+ end
+
+
+ def recurse( nodeset, &block )
+ for node in nodeset
+ yield node
+ recurse( node, &block ) if node.node_type == :element
+ end
+ end
+
+
+
+ # Builds a nodeset of all of the preceding nodes of the supplied node,
+ # in reverse document order
+ # preceding:: includes every element in the document that precedes this node,
+ # except for ancestors
+ def preceding( node )
+ #puts "IN PRECEDING"
+ ancestors = []
+ p = node.parent
+ while p
+ ancestors << p
+ p = p.parent
+ end
+
+ acc = []
+ p = preceding_node_of( node )
+ #puts "P = #{p.inspect}"
+ while p
+ if ancestors.include? p
+ ancestors.delete(p)
+ else
+ acc << p
+ end
+ p = preceding_node_of( p )
+ #puts "P = #{p.inspect}"
+ end
+ acc
+ end
+
+ def preceding_node_of( node )
+ #puts "NODE: #{node.inspect}"
+ #puts "PREVIOUS NODE: #{node.previous_sibling_node.inspect}"
+ #puts "PARENT NODE: #{node.parent}"
+ psn = node.previous_sibling_node
+ if psn.nil?
+ if node.parent.nil? or node.parent.class == Document
+ return nil
+ end
+ return node.parent
+ #psn = preceding_node_of( node.parent )
+ end
+ while psn and psn.kind_of? Element and psn.children.size > 0
+ psn = psn.children[-1]
+ end
+ psn
+ end
+
+ def following( node )
+ #puts "IN PRECEDING"
+ acc = []
+ p = next_sibling_node( node )
+ #puts "P = #{p.inspect}"
+ while p
+ acc << p
+ p = following_node_of( p )
+ #puts "P = #{p.inspect}"
+ end
+ acc
+ end
+
+ def following_node_of( node )
+ #puts "NODE: #{node.inspect}"
+ #puts "PREVIOUS NODE: #{node.previous_sibling_node.inspect}"
+ #puts "PARENT NODE: #{node.parent}"
+ if node.kind_of? Element and node.children.size > 0
+ return node.children[0]
+ end
+ return next_sibling_node(node)
+ end
+
+ def next_sibling_node(node)
+ psn = node.next_sibling_node
+ while psn.nil?
+ if node.parent.nil? or node.parent.class == Document
+ return nil
+ end
+ node = node.parent
+ psn = node.next_sibling_node
+ #puts "psn = #{psn.inspect}"
+ end
+ return psn
+ end
+
+ def norm b
+ case b
+ when true, false
+ return b
+ when 'true', 'false'
+ return Functions::boolean( b )
+ when /^\d+(\.\d+)?$/
+ return Functions::number( b )
+ else
+ return Functions::string( b )
+ end
+ end
+
+ def equality_relational_compare( set1, op, set2 )
+ #puts "EQ_REL_COMP(#{set1.inspect} #{op.inspect} #{set2.inspect})"
+ if set1.kind_of? Array and set2.kind_of? Array
+ #puts "#{set1.size} & #{set2.size}"
+ if set1.size == 1 and set2.size == 1
+ set1 = set1[0]
+ set2 = set2[0]
+ elsif set1.size == 0 or set2.size == 0
+ nd = set1.size==0 ? set2 : set1
+ rv = nd.collect { |il| compare( il, op, nil ) }
+ #puts "RV = #{rv.inspect}"
+ return rv
+ else
+ res = []
+ enum = SyncEnumerator.new( set1, set2 ).each { |i1, i2|
+ #puts "i1 = #{i1.inspect} (#{i1.class.name})"
+ #puts "i2 = #{i2.inspect} (#{i2.class.name})"
+ i1 = norm( i1 )
+ i2 = norm( i2 )
+ res << compare( i1, op, i2 )
+ }
+ return res
+ end
+ end
+ #puts "EQ_REL_COMP: #{set1.inspect} (#{set1.class.name}), #{op}, #{set2.inspect} (#{set2.class.name})"
+ #puts "COMPARING VALUES"
+ # If one is nodeset and other is number, compare number to each item
+ # in nodeset s.t. number op number(string(item))
+ # If one is nodeset and other is string, compare string to each item
+ # in nodeset s.t. string op string(item)
+ # If one is nodeset and other is boolean, compare boolean to each item
+ # in nodeset s.t. boolean op boolean(item)
+ if set1.kind_of? Array or set2.kind_of? Array
+ #puts "ISA ARRAY"
+ if set1.kind_of? Array
+ a = set1
+ b = set2
+ else
+ a = set2
+ b = set1
+ end
+
+ case b
+ when true, false
+ return a.collect {|v| compare( Functions::boolean(v), op, b ) }
+ when Numeric
+ return a.collect {|v| compare( Functions::number(v), op, b )}
+ when /^\d+(\.\d+)?$/
+ b = Functions::number( b )
+ #puts "B = #{b.inspect}"
+ return a.collect {|v| compare( Functions::number(v), op, b )}
+ else
+ #puts "Functions::string( #{b}(#{b.class.name}) ) = #{Functions::string(b)}"
+ b = Functions::string( b )
+ return a.collect { |v| compare( Functions::string(v), op, b ) }
+ end
+ else
+ # If neither is nodeset,
+ # If op is = or !=
+ # If either boolean, convert to boolean
+ # If either number, convert to number
+ # Else, convert to string
+ # Else
+ # Convert both to numbers and compare
+ s1 = set1.to_s
+ s2 = set2.to_s
+ #puts "EQ_REL_COMP: #{set1}=>#{s1}, #{set2}=>#{s2}"
+ if s1 == 'true' or s1 == 'false' or s2 == 'true' or s2 == 'false'
+ #puts "Functions::boolean(#{set1})=>#{Functions::boolean(set1)}"
+ #puts "Functions::boolean(#{set2})=>#{Functions::boolean(set2)}"
+ set1 = Functions::boolean( set1 )
+ set2 = Functions::boolean( set2 )
+ else
+ if op == :eq or op == :neq
+ if s1 =~ /^\d+(\.\d+)?$/ or s2 =~ /^\d+(\.\d+)?$/
+ set1 = Functions::number( s1 )
+ set2 = Functions::number( s2 )
+ else
+ set1 = Functions::string( set1 )
+ set2 = Functions::string( set2 )
+ end
+ else
+ set1 = Functions::number( set1 )
+ set2 = Functions::number( set2 )
+ end
+ end
+ #puts "EQ_REL_COMP: #{set1} #{op} #{set2}"
+ #puts ">>> #{compare( set1, op, set2 )}"
+ return compare( set1, op, set2 )
+ end
+ return false
+ end
+
+ def compare a, op, b
+ #puts "COMPARE #{a.inspect}(#{a.class.name}) #{op} #{b.inspect}(#{b.class.name})"
+ case op
+ when :eq
+ a == b
+ when :neq
+ a != b
+ when :lt
+ a < b
+ when :lteq
+ a <= b
+ when :gt
+ a > b
+ when :gteq
+ a >= b
+ when :and
+ a and b
+ when :or
+ a or b
+ else
+ false
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rinda/rinda.rb b/ruby/lib/rinda/rinda.rb
new file mode 100644
index 0000000..6c59e68
--- /dev/null
+++ b/ruby/lib/rinda/rinda.rb
@@ -0,0 +1,283 @@
+require 'drb/drb'
+require 'thread'
+
+##
+# A module to implement the Linda distributed computing paradigm in Ruby.
+#
+# Rinda is part of DRb (dRuby).
+#
+# == Example(s)
+#
+# See the sample/drb/ directory in the Ruby distribution, from 1.8.2 onwards.
+#
+#--
+# TODO
+# == Introduction to Linda/rinda?
+#
+# == Why is this library separate from DRb?
+
+module Rinda
+
+ ##
+ # Rinda error base class
+
+ class RindaError < RuntimeError; end
+
+ ##
+ # Raised when a hash-based tuple has an invalid key.
+
+ class InvalidHashTupleKey < RindaError; end
+
+ ##
+ # Raised when trying to use a canceled tuple.
+
+ class RequestCanceledError < ThreadError; end
+
+ ##
+ # Raised when trying to use an expired tuple.
+
+ class RequestExpiredError < ThreadError; end
+
+ ##
+ # A tuple is the elementary object in Rinda programming.
+ # Tuples may be matched against templates if the tuple and
+ # the template are the same size.
+
+ class Tuple
+
+ ##
+ # Creates a new Tuple from +ary_or_hash+ which must be an Array or Hash.
+
+ def initialize(ary_or_hash)
+ if hash?(ary_or_hash)
+ init_with_hash(ary_or_hash)
+ else
+ init_with_ary(ary_or_hash)
+ end
+ end
+
+ ##
+ # The number of elements in the tuple.
+
+ def size
+ @tuple.size
+ end
+
+ ##
+ # Accessor method for elements of the tuple.
+
+ def [](k)
+ @tuple[k]
+ end
+
+ ##
+ # Fetches item +k+ from the tuple.
+
+ def fetch(k)
+ @tuple.fetch(k)
+ end
+
+ ##
+ # Iterate through the tuple, yielding the index or key, and the
+ # value, thus ensuring arrays are iterated similarly to hashes.
+
+ def each # FIXME
+ if Hash === @tuple
+ @tuple.each { |k, v| yield(k, v) }
+ else
+ @tuple.each_with_index { |v, k| yield(k, v) }
+ end
+ end
+
+ ##
+ # Return the tuple itself
+ def value
+ @tuple
+ end
+
+ private
+
+ def hash?(ary_or_hash)
+ ary_or_hash.respond_to?(:keys)
+ end
+
+ ##
+ # Munges +ary+ into a valid Tuple.
+
+ def init_with_ary(ary)
+ @tuple = Array.new(ary.size)
+ @tuple.size.times do |i|
+ @tuple[i] = ary[i]
+ end
+ end
+
+ ##
+ # Ensures +hash+ is a valid Tuple.
+
+ def init_with_hash(hash)
+ @tuple = Hash.new
+ hash.each do |k, v|
+ raise InvalidHashTupleKey unless String === k
+ @tuple[k] = v
+ end
+ end
+
+ end
+
+ ##
+ # Templates are used to match tuples in Rinda.
+
+ class Template < Tuple
+
+ ##
+ # Matches this template against +tuple+. The +tuple+ must be the same
+ # size as the template. An element with a +nil+ value in a template acts
+ # as a wildcard, matching any value in the corresponding position in the
+ # tuple. Elements of the template match the +tuple+ if the are #== or
+ # #===.
+ #
+ # Template.new([:foo, 5]).match Tuple.new([:foo, 5]) # => true
+ # Template.new([:foo, nil]).match Tuple.new([:foo, 5]) # => true
+ # Template.new([String]).match Tuple.new(['hello']) # => true
+ #
+ # Template.new([:foo]).match Tuple.new([:foo, 5]) # => false
+ # Template.new([:foo, 6]).match Tuple.new([:foo, 5]) # => false
+ # Template.new([:foo, nil]).match Tuple.new([:foo]) # => false
+ # Template.new([:foo, 6]).match Tuple.new([:foo]) # => false
+
+ def match(tuple)
+ return false unless tuple.respond_to?(:size)
+ return false unless tuple.respond_to?(:fetch)
+ return false unless self.size == tuple.size
+ each do |k, v|
+ begin
+ it = tuple.fetch(k)
+ rescue
+ return false
+ end
+ next if v.nil?
+ next if v == it
+ next if v === it
+ return false
+ end
+ return true
+ end
+
+ ##
+ # Alias for #match.
+
+ def ===(tuple)
+ match(tuple)
+ end
+
+ end
+
+ ##
+ # <i>Documentation?</i>
+
+ class DRbObjectTemplate
+
+ ##
+ # Creates a new DRbObjectTemplate that will match against +uri+ and +ref+.
+
+ def initialize(uri=nil, ref=nil)
+ @drb_uri = uri
+ @drb_ref = ref
+ end
+
+ ##
+ # This DRbObjectTemplate matches +ro+ if the remote object's drburi and
+ # drbref are the same. +nil+ is used as a wildcard.
+
+ def ===(ro)
+ return true if super(ro)
+ unless @drb_uri.nil?
+ return false unless (@drb_uri === ro.__drburi rescue false)
+ end
+ unless @drb_ref.nil?
+ return false unless (@drb_ref === ro.__drbref rescue false)
+ end
+ true
+ end
+
+ end
+
+ ##
+ # TupleSpaceProxy allows a remote Tuplespace to appear as local.
+
+ class TupleSpaceProxy
+
+ ##
+ # Creates a new TupleSpaceProxy to wrap +ts+.
+
+ def initialize(ts)
+ @ts = ts
+ end
+
+ ##
+ # Adds +tuple+ to the proxied TupleSpace. See TupleSpace#write.
+
+ def write(tuple, sec=nil)
+ @ts.write(tuple, sec)
+ end
+
+ ##
+ # Takes +tuple+ from the proxied TupleSpace. See TupleSpace#take.
+
+ def take(tuple, sec=nil, &block)
+ port = []
+ @ts.move(DRbObject.new(port), tuple, sec, &block)
+ port[0]
+ end
+
+ ##
+ # Reads +tuple+ from the proxied TupleSpace. See TupleSpace#read.
+
+ def read(tuple, sec=nil, &block)
+ @ts.read(tuple, sec, &block)
+ end
+
+ ##
+ # Reads all tuples matching +tuple+ from the proxied TupleSpace. See
+ # TupleSpace#read_all.
+
+ def read_all(tuple)
+ @ts.read_all(tuple)
+ end
+
+ ##
+ # Registers for notifications of event +ev+ on the proxied TupleSpace.
+ # See TupleSpace#notify
+
+ def notify(ev, tuple, sec=nil)
+ @ts.notify(ev, tuple, sec)
+ end
+
+ end
+
+ ##
+ # An SimpleRenewer allows a TupleSpace to check if a TupleEntry is still
+ # alive.
+
+ class SimpleRenewer
+
+ include DRbUndumped
+
+ ##
+ # Creates a new SimpleRenewer that keeps an object alive for another +sec+
+ # seconds.
+
+ def initialize(sec=180)
+ @sec = sec
+ end
+
+ ##
+ # Called by the TupleSpace to check if the object is still alive.
+
+ def renew
+ @sec
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rinda/ring.rb b/ruby/lib/rinda/ring.rb
new file mode 100644
index 0000000..4dc7c7d
--- /dev/null
+++ b/ruby/lib/rinda/ring.rb
@@ -0,0 +1,271 @@
+#
+# Note: Rinda::Ring API is unstable.
+#
+require 'drb/drb'
+require 'rinda/rinda'
+require 'thread'
+
+module Rinda
+
+ ##
+ # The default port Ring discovery will use.
+
+ Ring_PORT = 7647
+
+ ##
+ # A RingServer allows a Rinda::TupleSpace to be located via UDP broadcasts.
+ # Service location uses the following steps:
+ #
+ # 1. A RingServer begins listening on the broadcast UDP address.
+ # 2. A RingFinger sends a UDP packet containing the DRb URI where it will
+ # listen for a reply.
+ # 3. The RingServer receives the UDP packet and connects back to the
+ # provided DRb URI with the DRb service.
+
+ class RingServer
+
+ include DRbUndumped
+
+ ##
+ # Advertises +ts+ on the UDP broadcast address at +port+.
+
+ def initialize(ts, port=Ring_PORT)
+ @ts = ts
+ @soc = UDPSocket.open
+ @soc.bind('', port)
+ @w_service = write_service
+ @r_service = reply_service
+ end
+
+ ##
+ # Creates a thread that picks up UDP packets and passes them to do_write
+ # for decoding.
+
+ def write_service
+ Thread.new do
+ loop do
+ msg = @soc.recv(1024)
+ do_write(msg)
+ end
+ end
+ end
+
+ ##
+ # Extracts the response URI from +msg+ and adds it to TupleSpace where it
+ # will be picked up by +reply_service+ for notification.
+
+ def do_write(msg)
+ Thread.new do
+ begin
+ tuple, sec = Marshal.load(msg)
+ @ts.write(tuple, sec)
+ rescue
+ end
+ end
+ end
+
+ ##
+ # Creates a thread that notifies waiting clients from the TupleSpace.
+
+ def reply_service
+ Thread.new do
+ loop do
+ do_reply
+ end
+ end
+ end
+
+ ##
+ # Pulls lookup tuples out of the TupleSpace and sends their DRb object the
+ # address of the local TupleSpace.
+
+ def do_reply
+ tuple = @ts.take([:lookup_ring, DRbObject])
+ Thread.new { tuple[1].call(@ts) rescue nil}
+ rescue
+ end
+
+ end
+
+ ##
+ # RingFinger is used by RingServer clients to discover the RingServer's
+ # TupleSpace. Typically, all a client needs to do is call
+ # RingFinger.primary to retrieve the remote TupleSpace, which it can then
+ # begin using.
+
+ class RingFinger
+
+ @@broadcast_list = ['<broadcast>', 'localhost']
+
+ @@finger = nil
+
+ ##
+ # Creates a singleton RingFinger and looks for a RingServer. Returns the
+ # created RingFinger.
+
+ def self.finger
+ unless @@finger
+ @@finger = self.new
+ @@finger.lookup_ring_any
+ end
+ @@finger
+ end
+
+ ##
+ # Returns the first advertised TupleSpace.
+
+ def self.primary
+ finger.primary
+ end
+
+ ##
+ # Contains all discovered TupleSpaces except for the primary.
+
+ def self.to_a
+ finger.to_a
+ end
+
+ ##
+ # The list of addresses where RingFinger will send query packets.
+
+ attr_accessor :broadcast_list
+
+ ##
+ # The port that RingFinger will send query packets to.
+
+ attr_accessor :port
+
+ ##
+ # Contain the first advertised TupleSpace after lookup_ring_any is called.
+
+ attr_accessor :primary
+
+ ##
+ # Creates a new RingFinger that will look for RingServers at +port+ on
+ # the addresses in +broadcast_list+.
+
+ def initialize(broadcast_list=@@broadcast_list, port=Ring_PORT)
+ @broadcast_list = broadcast_list || ['localhost']
+ @port = port
+ @primary = nil
+ @rings = []
+ end
+
+ ##
+ # Contains all discovered TupleSpaces except for the primary.
+
+ def to_a
+ @rings
+ end
+
+ ##
+ # Iterates over all discovered TupleSpaces starting with the primary.
+
+ def each
+ lookup_ring_any unless @primary
+ return unless @primary
+ yield(@primary)
+ @rings.each { |x| yield(x) }
+ end
+
+ ##
+ # Looks up RingServers waiting +timeout+ seconds. RingServers will be
+ # given +block+ as a callback, which will be called with the remote
+ # TupleSpace.
+
+ def lookup_ring(timeout=5, &block)
+ return lookup_ring_any(timeout) unless block_given?
+
+ msg = Marshal.dump([[:lookup_ring, DRbObject.new(block)], timeout])
+ @broadcast_list.each do |it|
+ soc = UDPSocket.open
+ begin
+ soc.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
+ soc.send(msg, 0, it, @port)
+ rescue
+ nil
+ ensure
+ soc.close
+ end
+ end
+ sleep(timeout)
+ end
+
+ ##
+ # Returns the first found remote TupleSpace. Any further recovered
+ # TupleSpaces can be found by calling +to_a+.
+
+ def lookup_ring_any(timeout=5)
+ queue = Queue.new
+
+ th = Thread.new do
+ self.lookup_ring(timeout) do |ts|
+ queue.push(ts)
+ end
+ queue.push(nil)
+ while it = queue.pop
+ @rings.push(it)
+ end
+ end
+
+ @primary = queue.pop
+ raise('RingNotFound') if @primary.nil?
+ @primary
+ end
+
+ end
+
+ ##
+ # RingProvider uses a RingServer advertised TupleSpace as a name service.
+ # TupleSpace clients can register themselves with the remote TupleSpace and
+ # look up other provided services via the remote TupleSpace.
+ #
+ # Services are registered with a tuple of the format [:name, klass,
+ # DRbObject, description].
+
+ class RingProvider
+
+ ##
+ # Creates a RingProvider that will provide a +klass+ service running on
+ # +front+, with a +description+. +renewer+ is optional.
+
+ def initialize(klass, front, desc, renewer = nil)
+ @tuple = [:name, klass, front, desc]
+ @renewer = renewer || Rinda::SimpleRenewer.new
+ end
+
+ ##
+ # Advertises this service on the primary remote TupleSpace.
+
+ def provide
+ ts = Rinda::RingFinger.primary
+ ts.write(@tuple, @renewer)
+ end
+
+ end
+
+end
+
+if __FILE__ == $0
+ DRb.start_service
+ case ARGV.shift
+ when 's'
+ require 'rinda/tuplespace'
+ ts = Rinda::TupleSpace.new
+ place = Rinda::RingServer.new(ts)
+ $stdin.gets
+ when 'w'
+ finger = Rinda::RingFinger.new(nil)
+ finger.lookup_ring do |ts2|
+ p ts2
+ ts2.write([:hello, :world])
+ end
+ when 'r'
+ finger = Rinda::RingFinger.new(nil)
+ finger.lookup_ring do |ts2|
+ p ts2
+ p ts2.take([nil, nil])
+ end
+ end
+end
+
diff --git a/ruby/lib/rinda/tuplespace.rb b/ruby/lib/rinda/tuplespace.rb
new file mode 100644
index 0000000..6ca30a7
--- /dev/null
+++ b/ruby/lib/rinda/tuplespace.rb
@@ -0,0 +1,642 @@
+require 'monitor'
+require 'thread'
+require 'drb/drb'
+require 'rinda/rinda'
+require 'enumerator'
+require 'forwardable'
+
+module Rinda
+
+ ##
+ # A TupleEntry is a Tuple (i.e. a possible entry in some Tuplespace)
+ # together with expiry and cancellation data.
+
+ class TupleEntry
+
+ include DRbUndumped
+
+ attr_accessor :expires
+
+ ##
+ # Creates a TupleEntry based on +ary+ with an optional renewer or expiry
+ # time +sec+.
+ #
+ # A renewer must implement the +renew+ method which returns a Numeric,
+ # nil, or true to indicate when the tuple has expired.
+
+ def initialize(ary, sec=nil)
+ @cancel = false
+ @expires = nil
+ @tuple = make_tuple(ary)
+ @renewer = nil
+ renew(sec)
+ end
+
+ ##
+ # Marks this TupleEntry as canceled.
+
+ def cancel
+ @cancel = true
+ end
+
+ ##
+ # A TupleEntry is dead when it is canceled or expired.
+
+ def alive?
+ !canceled? && !expired?
+ end
+
+ ##
+ # Return the object which makes up the tuple itself: the Array
+ # or Hash.
+
+ def value; @tuple.value; end
+
+ ##
+ # Returns the canceled status.
+
+ def canceled?; @cancel; end
+
+ ##
+ # Has this tuple expired? (true/false).
+ #
+ # A tuple has expired when its expiry timer based on the +sec+ argument to
+ # #initialize runs out.
+
+ def expired?
+ return true unless @expires
+ return false if @expires > Time.now
+ return true if @renewer.nil?
+ renew(@renewer)
+ return true unless @expires
+ return @expires < Time.now
+ end
+
+ ##
+ # Reset the expiry time according to +sec_or_renewer+.
+ #
+ # +nil+:: it is set to expire in the far future.
+ # +false+:: it has expired.
+ # Numeric:: it will expire in that many seconds.
+ #
+ # Otherwise the argument refers to some kind of renewer object
+ # which will reset its expiry time.
+
+ def renew(sec_or_renewer)
+ sec, @renewer = get_renewer(sec_or_renewer)
+ @expires = make_expires(sec)
+ end
+
+ ##
+ # Returns an expiry Time based on +sec+ which can be one of:
+ # Numeric:: +sec+ seconds into the future
+ # +true+:: the expiry time is the start of 1970 (i.e. expired)
+ # +nil+:: it is Tue Jan 19 03:14:07 GMT Standard Time 2038 (i.e. when
+ # UNIX clocks will die)
+
+ def make_expires(sec=nil)
+ case sec
+ when Numeric
+ Time.now + sec
+ when true
+ Time.at(1)
+ when nil
+ Time.at(2**31-1)
+ end
+ end
+
+ ##
+ # Retrieves +key+ from the tuple.
+
+ def [](key)
+ @tuple[key]
+ end
+
+ ##
+ # Fetches +key+ from the tuple.
+
+ def fetch(key)
+ @tuple.fetch(key)
+ end
+
+ ##
+ # The size of the tuple.
+
+ def size
+ @tuple.size
+ end
+
+ ##
+ # Creates a Rinda::Tuple for +ary+.
+
+ def make_tuple(ary)
+ Rinda::Tuple.new(ary)
+ end
+
+ private
+
+ ##
+ # Returns a valid argument to make_expires and the renewer or nil.
+ #
+ # Given +true+, +nil+, or Numeric, returns that value and +nil+ (no actual
+ # renewer). Otherwise it returns an expiry value from calling +it.renew+
+ # and the renewer.
+
+ def get_renewer(it)
+ case it
+ when Numeric, true, nil
+ return it, nil
+ else
+ begin
+ return it.renew, it
+ rescue Exception
+ return it, nil
+ end
+ end
+ end
+
+ end
+
+ ##
+ # A TemplateEntry is a Template together with expiry and cancellation data.
+
+ class TemplateEntry < TupleEntry
+ ##
+ # Matches this TemplateEntry against +tuple+. See Template#match for
+ # details on how a Template matches a Tuple.
+
+ def match(tuple)
+ @tuple.match(tuple)
+ end
+
+ alias === match
+
+ def make_tuple(ary) # :nodoc:
+ Rinda::Template.new(ary)
+ end
+
+ end
+
+ ##
+ # <i>Documentation?</i>
+
+ class WaitTemplateEntry < TemplateEntry
+
+ attr_reader :found
+
+ def initialize(place, ary, expires=nil)
+ super(ary, expires)
+ @place = place
+ @cond = place.new_cond
+ @found = nil
+ end
+
+ def cancel
+ super
+ signal
+ end
+
+ def wait
+ @cond.wait
+ end
+
+ def read(tuple)
+ @found = tuple
+ signal
+ end
+
+ def signal
+ @place.synchronize do
+ @cond.signal
+ end
+ end
+
+ end
+
+ ##
+ # A NotifyTemplateEntry is returned by TupleSpace#notify and is notified of
+ # TupleSpace changes. You may receive either your subscribed event or the
+ # 'close' event when iterating over notifications.
+ #
+ # See TupleSpace#notify_event for valid notification types.
+ #
+ # == Example
+ #
+ # ts = Rinda::TupleSpace.new
+ # observer = ts.notify 'write', [nil]
+ #
+ # Thread.start do
+ # observer.each { |t| p t }
+ # end
+ #
+ # 3.times { |i| ts.write [i] }
+ #
+ # Outputs:
+ #
+ # ['write', [0]]
+ # ['write', [1]]
+ # ['write', [2]]
+
+ class NotifyTemplateEntry < TemplateEntry
+
+ ##
+ # Creates a new NotifyTemplateEntry that watches +place+ for +event+s that
+ # match +tuple+.
+
+ def initialize(place, event, tuple, expires=nil)
+ ary = [event, Rinda::Template.new(tuple)]
+ super(ary, expires)
+ @queue = Queue.new
+ @done = false
+ end
+
+ ##
+ # Called by TupleSpace to notify this NotifyTemplateEntry of a new event.
+
+ def notify(ev)
+ @queue.push(ev)
+ end
+
+ ##
+ # Retrieves a notification. Raises RequestExpiredError when this
+ # NotifyTemplateEntry expires.
+
+ def pop
+ raise RequestExpiredError if @done
+ it = @queue.pop
+ @done = true if it[0] == 'close'
+ return it
+ end
+
+ ##
+ # Yields event/tuple pairs until this NotifyTemplateEntry expires.
+
+ def each # :yields: event, tuple
+ while !@done
+ it = pop
+ yield(it)
+ end
+ rescue
+ ensure
+ cancel
+ end
+
+ end
+
+ ##
+ # TupleBag is an unordered collection of tuples. It is the basis
+ # of Tuplespace.
+
+ class TupleBag
+ class TupleBin
+ extend Forwardable
+ def_delegators '@bin', :find_all, :delete_if, :each, :empty?
+
+ def initialize
+ @bin = []
+ end
+
+ def add(tuple)
+ @bin.push(tuple)
+ end
+
+ def delete(tuple)
+ idx = @bin.rindex(tuple)
+ @bin.delete_at(idx) if idx
+ end
+
+ def find(&blk)
+ @bin.reverse_each do |x|
+ return x if yield(x)
+ end
+ nil
+ end
+ end
+
+ def initialize # :nodoc:
+ @hash = {}
+ @enum = enum_for(:each_entry)
+ end
+
+ ##
+ # +true+ if the TupleBag to see if it has any expired entries.
+
+ def has_expires?
+ @enum.find do |tuple|
+ tuple.expires
+ end
+ end
+
+ ##
+ # Add +tuple+ to the TupleBag.
+
+ def push(tuple)
+ key = bin_key(tuple)
+ @hash[key] ||= TupleBin.new
+ @hash[key].add(tuple)
+ end
+
+ ##
+ # Removes +tuple+ from the TupleBag.
+
+ def delete(tuple)
+ key = bin_key(tuple)
+ bin = @hash[key]
+ return nil unless bin
+ bin.delete(tuple)
+ @hash.delete(key) if bin.empty?
+ tuple
+ end
+
+ ##
+ # Finds all live tuples that match +template+.
+ def find_all(template)
+ bin_for_find(template).find_all do |tuple|
+ tuple.alive? && template.match(tuple)
+ end
+ end
+
+ ##
+ # Finds a live tuple that matches +template+.
+
+ def find(template)
+ bin_for_find(template).find do |tuple|
+ tuple.alive? && template.match(tuple)
+ end
+ end
+
+ ##
+ # Finds all tuples in the TupleBag which when treated as templates, match
+ # +tuple+ and are alive.
+
+ def find_all_template(tuple)
+ @enum.find_all do |template|
+ template.alive? && template.match(tuple)
+ end
+ end
+
+ ##
+ # Delete tuples which dead tuples from the TupleBag, returning the deleted
+ # tuples.
+
+ def delete_unless_alive
+ deleted = []
+ @hash.each do |key, bin|
+ bin.delete_if do |tuple|
+ if tuple.alive?
+ false
+ else
+ deleted.push(tuple)
+ true
+ end
+ end
+ end
+ deleted
+ end
+
+ private
+ def each_entry(&blk)
+ @hash.each do |k, v|
+ v.each(&blk)
+ end
+ end
+
+ def bin_key(tuple)
+ head = tuple[0]
+ if head.class == Symbol
+ return head
+ else
+ false
+ end
+ end
+
+ def bin_for_find(template)
+ key = bin_key(template)
+ key ? @hash.fetch(key, []) : @enum
+ end
+ end
+
+ ##
+ # The Tuplespace manages access to the tuples it contains,
+ # ensuring mutual exclusion requirements are met.
+ #
+ # The +sec+ option for the write, take, move, read and notify methods may
+ # either be a number of seconds or a Renewer object.
+
+ class TupleSpace
+
+ include DRbUndumped
+ include MonitorMixin
+
+ ##
+ # Creates a new TupleSpace. +period+ is used to control how often to look
+ # for dead tuples after modifications to the TupleSpace.
+ #
+ # If no dead tuples are found +period+ seconds after the last
+ # modification, the TupleSpace will stop looking for dead tuples.
+
+ def initialize(period=60)
+ super()
+ @bag = TupleBag.new
+ @read_waiter = TupleBag.new
+ @take_waiter = TupleBag.new
+ @notify_waiter = TupleBag.new
+ @period = period
+ @keeper = nil
+ end
+
+ ##
+ # Adds +tuple+
+
+ def write(tuple, sec=nil)
+ entry = create_entry(tuple, sec)
+ synchronize do
+ if entry.expired?
+ @read_waiter.find_all_template(entry).each do |template|
+ template.read(tuple)
+ end
+ notify_event('write', entry.value)
+ notify_event('delete', entry.value)
+ else
+ @bag.push(entry)
+ start_keeper if entry.expires
+ @read_waiter.find_all_template(entry).each do |template|
+ template.read(tuple)
+ end
+ @take_waiter.find_all_template(entry).each do |template|
+ template.signal
+ end
+ notify_event('write', entry.value)
+ end
+ end
+ entry
+ end
+
+ ##
+ # Removes +tuple+
+
+ def take(tuple, sec=nil, &block)
+ move(nil, tuple, sec, &block)
+ end
+
+ ##
+ # Moves +tuple+ to +port+.
+
+ def move(port, tuple, sec=nil)
+ template = WaitTemplateEntry.new(self, tuple, sec)
+ yield(template) if block_given?
+ synchronize do
+ entry = @bag.find(template)
+ if entry
+ port.push(entry.value) if port
+ @bag.delete(entry)
+ notify_event('take', entry.value)
+ return entry.value
+ end
+ raise RequestExpiredError if template.expired?
+
+ begin
+ @take_waiter.push(template)
+ start_keeper if template.expires
+ while true
+ raise RequestCanceledError if template.canceled?
+ raise RequestExpiredError if template.expired?
+ entry = @bag.find(template)
+ if entry
+ port.push(entry.value) if port
+ @bag.delete(entry)
+ notify_event('take', entry.value)
+ return entry.value
+ end
+ template.wait
+ end
+ ensure
+ @take_waiter.delete(template)
+ end
+ end
+ end
+
+ ##
+ # Reads +tuple+, but does not remove it.
+
+ def read(tuple, sec=nil)
+ template = WaitTemplateEntry.new(self, tuple, sec)
+ yield(template) if block_given?
+ synchronize do
+ entry = @bag.find(template)
+ return entry.value if entry
+ raise RequestExpiredError if template.expired?
+
+ begin
+ @read_waiter.push(template)
+ start_keeper if template.expires
+ template.wait
+ raise RequestCanceledError if template.canceled?
+ raise RequestExpiredError if template.expired?
+ return template.found
+ ensure
+ @read_waiter.delete(template)
+ end
+ end
+ end
+
+ ##
+ # Returns all tuples matching +tuple+. Does not remove the found tuples.
+
+ def read_all(tuple)
+ template = WaitTemplateEntry.new(self, tuple, nil)
+ synchronize do
+ entry = @bag.find_all(template)
+ entry.collect do |e|
+ e.value
+ end
+ end
+ end
+
+ ##
+ # Registers for notifications of +event+. Returns a NotifyTemplateEntry.
+ # See NotifyTemplateEntry for examples of how to listen for notifications.
+ #
+ # +event+ can be:
+ # 'write':: A tuple was added
+ # 'take':: A tuple was taken or moved
+ # 'delete':: A tuple was lost after being overwritten or expiring
+ #
+ # The TupleSpace will also notify you of the 'close' event when the
+ # NotifyTemplateEntry has expired.
+
+ def notify(event, tuple, sec=nil)
+ template = NotifyTemplateEntry.new(self, event, tuple, sec)
+ synchronize do
+ @notify_waiter.push(template)
+ end
+ template
+ end
+
+ private
+
+ def create_entry(tuple, sec)
+ TupleEntry.new(tuple, sec)
+ end
+
+ ##
+ # Removes dead tuples.
+
+ def keep_clean
+ synchronize do
+ @read_waiter.delete_unless_alive.each do |e|
+ e.signal
+ end
+ @take_waiter.delete_unless_alive.each do |e|
+ e.signal
+ end
+ @notify_waiter.delete_unless_alive.each do |e|
+ e.notify(['close'])
+ end
+ @bag.delete_unless_alive.each do |e|
+ notify_event('delete', e.value)
+ end
+ end
+ end
+
+ ##
+ # Notifies all registered listeners for +event+ of a status change of
+ # +tuple+.
+
+ def notify_event(event, tuple)
+ ev = [event, tuple]
+ @notify_waiter.find_all_template(ev).each do |template|
+ template.notify(ev)
+ end
+ end
+
+ ##
+ # Creates a thread that scans the tuplespace for expired tuples.
+
+ def start_keeper
+ return if @keeper && @keeper.alive?
+ @keeper = Thread.new do
+ while true
+ sleep(@period)
+ synchronize do
+ break unless need_keeper?
+ keep_clean
+ end
+ end
+ end
+ end
+
+ ##
+ # Checks the tuplespace to see if it needs cleaning.
+
+ def need_keeper?
+ return true if @bag.has_expires?
+ return true if @read_waiter.has_expires?
+ return true if @take_waiter.has_expires?
+ return true if @notify_waiter.has_expires?
+ end
+
+ end
+
+end
+
diff --git a/ruby/lib/ripper.rb b/ruby/lib/ripper.rb
new file mode 100644
index 0000000..cb19da3
--- /dev/null
+++ b/ruby/lib/ripper.rb
@@ -0,0 +1,4 @@
+require 'ripper/core'
+require 'ripper/lexer'
+require 'ripper/filter'
+require 'ripper/sexp'
diff --git a/ruby/lib/ripper/core.rb b/ruby/lib/ripper/core.rb
new file mode 100644
index 0000000..211f722
--- /dev/null
+++ b/ruby/lib/ripper/core.rb
@@ -0,0 +1,70 @@
+#
+# $Id: core.rb 11708 2007-02-12 23:01:19Z shyouhei $
+#
+# Copyright (c) 2003-2005 Minero Aoki
+#
+# This program is free software.
+# You can distribute and/or modify this program under the Ruby License.
+# For details of Ruby License, see ruby/COPYING.
+#
+
+require 'ripper.so'
+
+class Ripper
+
+ # Parses Ruby program read from _src_.
+ # _src_ must be a String or a IO or a object which has #gets method.
+ def Ripper.parse(src, filename = '(ripper)', lineno = 1)
+ new(src, filename, lineno).parse
+ end
+
+ # This array contains name of parser events.
+ PARSER_EVENTS = PARSER_EVENT_TABLE.keys
+
+ # This array contains name of scanner events.
+ SCANNER_EVENTS = SCANNER_EVENT_TABLE.keys
+
+ # This array contains name of all ripper events.
+ EVENTS = PARSER_EVENTS + SCANNER_EVENTS
+
+ private
+
+ #
+ # Parser Events
+ #
+
+ PARSER_EVENT_TABLE.each do |id, arity|
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{id}(#{ ('a'..'z').to_a[0, arity].join(', ') })
+ #{arity == 0 ? 'nil' : 'a'}
+ end
+ End
+ end
+
+ # This method is called when weak warning is produced by the parser.
+ # _fmt_ and _args_ is printf style.
+ def warn(fmt, *args)
+ end
+
+ # This method is called when strong warning is produced by the parser.
+ # _fmt_ and _args_ is printf style.
+ def warning(fmt, *args)
+ end
+
+ # This method is called when the parser found syntax error.
+ def compile_error(msg)
+ end
+
+ #
+ # Scanner Events
+ #
+
+ SCANNER_EVENTS.each do |id|
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{id}(token)
+ token
+ end
+ End
+ end
+
+end
diff --git a/ruby/lib/ripper/filter.rb b/ruby/lib/ripper/filter.rb
new file mode 100644
index 0000000..0c37c79
--- /dev/null
+++ b/ruby/lib/ripper/filter.rb
@@ -0,0 +1,70 @@
+#
+# $Id: filter.rb 11708 2007-02-12 23:01:19Z shyouhei $
+#
+# Copyright (c) 2004,2005 Minero Aoki
+#
+# This program is free software.
+# You can distribute and/or modify this program under the Ruby License.
+# For details of Ruby License, see ruby/COPYING.
+#
+
+require 'ripper/lexer'
+
+class Ripper
+
+ # This class handles only scanner events,
+ # and they are dispatched in the `right' order (same with input).
+ class Filter
+
+ def initialize(src, filename = '-', lineno = 1)
+ @__lexer = Lexer.new(src, filename, lineno)
+ @__line = nil
+ @__col = nil
+ end
+
+ # The file name of the input.
+ def filename
+ @__lexer.filename
+ end
+
+ # The line number of the current token.
+ # This value starts from 1.
+ # This method is valid only in event handlers.
+ def lineno
+ @__line
+ end
+
+ # The column number of the current token.
+ # This value starts from 0.
+ # This method is valid only in event handlers.
+ def column
+ @__col
+ end
+
+ # Starts parsing. _init_ is a data accumulator.
+ # It is passed to the next event handler (as of Enumerable#inject).
+ def parse(init = nil)
+ data = init
+ @__lexer.lex.each do |pos, event, tok|
+ @__line, @__col = *pos
+ data = if respond_to?(event, true)
+ then __send__(event, tok, data)
+ else on_default(event, tok, data)
+ end
+ end
+ data
+ end
+
+ private
+
+ # This method is called when some event handler have not defined.
+ # _event_ is :on_XXX, _token_ is scanned token, _data_ is a data
+ # accumulator. The return value of this method is passed to the
+ # next event handler (as of Enumerable#inject).
+ def on_default(event, token, data)
+ data
+ end
+
+ end
+
+end
diff --git a/ruby/lib/ripper/lexer.rb b/ruby/lib/ripper/lexer.rb
new file mode 100644
index 0000000..df76dd5
--- /dev/null
+++ b/ruby/lib/ripper/lexer.rb
@@ -0,0 +1,179 @@
+#
+# $Id: lexer.rb 13966 2007-11-19 07:10:09Z matz $
+#
+# Copyright (c) 2004,2005 Minero Aoki
+#
+# This program is free software.
+# You can distribute and/or modify this program under the Ruby License.
+# For details of Ruby License, see ruby/COPYING.
+#
+
+require 'ripper/core'
+
+class Ripper
+
+ # Tokenizes Ruby program and returns an Array of String.
+ def Ripper.tokenize(src, filename = '-', lineno = 1)
+ Lexer.new(src, filename, lineno).tokenize
+ end
+
+ # Tokenizes Ruby program and returns an Array of Array,
+ # which is formatted like [[lineno, column], type, token].
+ #
+ # require 'ripper'
+ # require 'pp'
+ #
+ # p Ripper.lex("def m(a) nil end")
+ # #=> [[[1, 0], :on_kw, "def"],
+ # [[1, 3], :on_sp, " " ],
+ # [[1, 4], :on_ident, "m" ],
+ # [[1, 5], :on_lparen, "(" ],
+ # [[1, 6], :on_ident, "a" ],
+ # [[1, 7], :on_rparen, ")" ],
+ # [[1, 8], :on_sp, " " ],
+ # [[1, 9], :on_kw, "nil"],
+ # [[1, 12], :on_sp, " " ],
+ # [[1, 13], :on_kw, "end"]]
+ #
+ def Ripper.lex(src, filename = '-', lineno = 1)
+ Lexer.new(src, filename, lineno).lex
+ end
+
+ class Lexer < ::Ripper #:nodoc: internal use only
+ def tokenize
+ lex().map {|pos, event, tok| tok }
+ end
+
+ def lex
+ parse().sort_by {|pos, event, tok| pos }
+ end
+
+ def parse
+ @buf = []
+ super
+ @buf
+ end
+
+ private
+
+ SCANNER_EVENTS.each do |event|
+ module_eval(<<-End, __FILE__+'/module_eval', __LINE__ + 1)
+ def on_#{event}(tok)
+ @buf.push [[lineno(), column()], :on_#{event}, tok]
+ end
+ End
+ end
+ end
+
+ # [EXPERIMENTAL]
+ # Parses +src+ and return a string which was matched to +pattern+.
+ # +pattern+ should be described as Regexp.
+ #
+ # require 'ripper'
+ #
+ # p Ripper.slice('def m(a) nil end', 'ident') #=> "m"
+ # p Ripper.slice('def m(a) nil end', '[ident lparen rparen]+') #=> "m(a)"
+ # p Ripper.slice("<<EOS\nstring\nEOS",
+ # 'heredoc_beg nl $(tstring_content*) heredoc_end', 1)
+ # #=> "string\n"
+ #
+ def Ripper.slice(src, pattern, n = 0)
+ if m = token_match(src, pattern)
+ then m.string(n)
+ else nil
+ end
+ end
+
+ def Ripper.token_match(src, pattern) #:nodoc:
+ TokenPattern.compile(pattern).match(src)
+ end
+
+ class TokenPattern #:nodoc:
+
+ class Error < ::StandardError; end
+ class CompileError < Error; end
+ class MatchError < Error; end
+
+ class << self
+ alias compile new
+ end
+
+ def initialize(pattern)
+ @source = pattern
+ @re = compile(pattern)
+ end
+
+ def match(str)
+ match_list(::Ripper.lex(str))
+ end
+
+ def match_list(tokens)
+ if m = @re.match(map_tokens(tokens))
+ then MatchData.new(tokens, m)
+ else nil
+ end
+ end
+
+ private
+
+ def compile(pattern)
+ if m = /[^\w\s$()\[\]{}?*+\.]/.match(pattern)
+ raise CompileError, "invalid char in pattern: #{m[0].inspect}"
+ end
+ buf = ''
+ pattern.scan(/(?:\w+|\$\(|[()\[\]\{\}?*+\.]+)/) do |tok|
+ case tok
+ when /\w/
+ buf.concat map_token(tok)
+ when '$('
+ buf.concat '('
+ when '('
+ buf.concat '(?:'
+ when /[?*\[\])\.]/
+ buf.concat tok
+ else
+ raise 'must not happen'
+ end
+ end
+ Regexp.compile(buf)
+ rescue RegexpError => err
+ raise CompileError, err.message
+ end
+
+ def map_tokens(tokens)
+ tokens.map {|pos,type,str| map_token(type.to_s.sub(/\Aon_/,'')) }.join
+ end
+
+ MAP = {}
+ seed = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
+ SCANNER_EVENT_TABLE.each do |ev, |
+ raise CompileError, "[RIPPER FATAL] too many system token" if seed.empty?
+ MAP[ev.to_s.sub(/\Aon_/,'')] = seed.shift
+ end
+
+ def map_token(tok)
+ MAP[tok] or raise CompileError, "unknown token: #{tok}"
+ end
+
+ class MatchData
+ def initialize(tokens, match)
+ @tokens = tokens
+ @match = match
+ end
+
+ def string(n = 0)
+ return nil unless @match
+ match(n).join
+ end
+
+ private
+
+ def match(n = 0)
+ return [] unless @match
+ @tokens[@match.begin(n)...@match.end(n)].map {|pos,type,str| str }
+ end
+ end
+
+ end
+
+end
diff --git a/ruby/lib/ripper/sexp.rb b/ruby/lib/ripper/sexp.rb
new file mode 100644
index 0000000..572def9
--- /dev/null
+++ b/ruby/lib/ripper/sexp.rb
@@ -0,0 +1,99 @@
+#
+# $Id: sexp.rb 13465 2007-09-17 12:02:35Z aamine $
+#
+# Copyright (c) 2004,2005 Minero Aoki
+#
+# This program is free software.
+# You can distribute and/or modify this program under the Ruby License.
+# For details of Ruby License, see ruby/COPYING.
+#
+
+require 'ripper/core'
+
+class Ripper
+
+ # [EXPERIMENTAL]
+ # Parses +src+ and create S-exp tree.
+ # This method is for mainly developper use.
+ #
+ # require 'ripper'
+ # require 'pp
+ #
+ # pp Ripper.sexp("def m(a) nil end")
+ # #=> [:program,
+ # [:stmts_add,
+ # [:stmts_new],
+ # [:def,
+ # [:@ident, "m", [1, 4]],
+ # [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil]],
+ # [:bodystmt,
+ # [:stmts_add, [:stmts_new], [:var_ref, [:@kw, "nil", [1, 9]]]],
+ # nil,
+ # nil,
+ # nil]]]]
+ #
+ def Ripper.sexp(src, filename = '-', lineno = 1)
+ SexpBuilderPP.new(src, filename, lineno).parse
+ end
+
+ def Ripper.sexp_raw(src, filename = '-', lineno = 1)
+ SexpBuilder.new(src, filename, lineno).parse
+ end
+
+ class SexpBuilderPP < ::Ripper #:nodoc:
+ private
+
+ PARSER_EVENT_TABLE.each do |event, arity|
+ if /_new\z/ =~ event.to_s and arity == 0
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}
+ []
+ end
+ End
+ elsif /_add\z/ =~ event.to_s
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(list, item)
+ list.push item
+ list
+ end
+ End
+ else
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(*args)
+ [:#{event}, *args]
+ end
+ End
+ end
+ end
+
+ SCANNER_EVENTS.each do |event|
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(tok)
+ [:@#{event}, tok, [lineno(), column()]]
+ end
+ End
+ end
+ end
+
+ class SexpBuilder < ::Ripper #:nodoc:
+ private
+
+ PARSER_EVENTS.each do |event|
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(*args)
+ args.unshift :#{event}
+ args
+ end
+ End
+ end
+
+ SCANNER_EVENTS.each do |event|
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(tok)
+ [:@#{event}, tok, [lineno(), column()]]
+ end
+ End
+ end
+ end
+
+end
diff --git a/ruby/lib/rss.rb b/ruby/lib/rss.rb
new file mode 100644
index 0000000..a1d0f76
--- /dev/null
+++ b/ruby/lib/rss.rb
@@ -0,0 +1,19 @@
+# Copyright (c) 2003-2007 Kouhei Sutou. You can redistribute it and/or
+# modify it under the same terms as Ruby.
+#
+# Author:: Kouhei Sutou <kou@cozmixng.org>
+# Tutorial:: http://www.cozmixng.org/~rwiki/?cmd=view;name=RSS+Parser%3A%3ATutorial.en
+
+require 'rss/1.0'
+require 'rss/2.0'
+require 'rss/atom'
+require 'rss/content'
+require 'rss/dublincore'
+require 'rss/image'
+require 'rss/itunes'
+require 'rss/slash'
+require 'rss/syndication'
+require 'rss/taxonomy'
+require 'rss/trackback'
+
+require "rss/maker"
diff --git a/ruby/lib/rss/0.9.rb b/ruby/lib/rss/0.9.rb
new file mode 100644
index 0000000..6b879d8
--- /dev/null
+++ b/ruby/lib/rss/0.9.rb
@@ -0,0 +1,427 @@
+require "rss/parser"
+
+module RSS
+
+ module RSS09
+ NSPOOL = {}
+ ELEMENTS = []
+
+ def self.append_features(klass)
+ super
+
+ klass.install_must_call_validator('', "")
+ end
+ end
+
+ class Rss < Element
+
+ include RSS09
+ include RootElementMixin
+
+ %w(channel).each do |name|
+ install_have_child_element(name, "", nil)
+ end
+
+ attr_writer :feed_version
+ alias_method(:rss_version, :feed_version)
+ alias_method(:rss_version=, :feed_version=)
+
+ def initialize(feed_version, version=nil, encoding=nil, standalone=nil)
+ super
+ @feed_type = "rss"
+ end
+
+ def items
+ if @channel
+ @channel.items
+ else
+ []
+ end
+ end
+
+ def image
+ if @channel
+ @channel.image
+ else
+ nil
+ end
+ end
+
+ def textinput
+ if @channel
+ @channel.textInput
+ else
+ nil
+ end
+ end
+
+ def setup_maker_elements(maker)
+ super
+ items.each do |item|
+ item.setup_maker(maker.items)
+ end
+ image.setup_maker(maker) if image
+ textinput.setup_maker(maker) if textinput
+ end
+
+ private
+ def _attrs
+ [
+ ["version", true, "feed_version"],
+ ]
+ end
+
+ class Channel < Element
+
+ include RSS09
+
+ [
+ ["title", nil, :text],
+ ["link", nil, :text],
+ ["description", nil, :text],
+ ["language", nil, :text],
+ ["copyright", "?", :text],
+ ["managingEditor", "?", :text],
+ ["webMaster", "?", :text],
+ ["rating", "?", :text],
+ ["pubDate", "?", :date, :rfc822],
+ ["lastBuildDate", "?", :date, :rfc822],
+ ["docs", "?", :text],
+ ["cloud", "?", :have_attribute],
+ ["skipDays", "?", :have_child],
+ ["skipHours", "?", :have_child],
+ ["image", nil, :have_child],
+ ["item", "*", :have_children],
+ ["textInput", "?", :have_child],
+ ].each do |name, occurs, type, *args|
+ __send__("install_#{type}_element", name, "", occurs, name, *args)
+ end
+ alias date pubDate
+ alias date= pubDate=
+
+ private
+ def maker_target(maker)
+ maker.channel
+ end
+
+ def setup_maker_elements(channel)
+ super
+ [
+ [skipDays, "day"],
+ [skipHours, "hour"],
+ ].each do |skip, key|
+ if skip
+ skip.__send__("#{key}s").each do |val|
+ target_skips = channel.__send__("skip#{key.capitalize}s")
+ new_target = target_skips.__send__("new_#{key}")
+ new_target.content = val.content
+ end
+ end
+ end
+ end
+
+ def not_need_to_call_setup_maker_variables
+ %w(image textInput)
+ end
+
+ class SkipDays < Element
+ include RSS09
+
+ [
+ ["day", "*"]
+ ].each do |name, occurs|
+ install_have_children_element(name, "", occurs)
+ end
+
+ class Day < Element
+ include RSS09
+
+ content_setup
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.content = args[0]
+ end
+ end
+
+ end
+
+ end
+
+ class SkipHours < Element
+ include RSS09
+
+ [
+ ["hour", "*"]
+ ].each do |name, occurs|
+ install_have_children_element(name, "", occurs)
+ end
+
+ class Hour < Element
+ include RSS09
+
+ content_setup(:integer)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.content = args[0]
+ end
+ end
+ end
+
+ end
+
+ class Image < Element
+
+ include RSS09
+
+ %w(url title link).each do |name|
+ install_text_element(name, "", nil)
+ end
+ [
+ ["width", :integer],
+ ["height", :integer],
+ ["description"],
+ ].each do |name, type|
+ install_text_element(name, "", "?", name, type)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.url = args[0]
+ self.title = args[1]
+ self.link = args[2]
+ self.width = args[3]
+ self.height = args[4]
+ self.description = args[5]
+ end
+ end
+
+ private
+ def maker_target(maker)
+ maker.image
+ end
+ end
+
+ class Cloud < Element
+
+ include RSS09
+
+ [
+ ["domain", "", true],
+ ["port", "", true, :integer],
+ ["path", "", true],
+ ["registerProcedure", "", true],
+ ["protocol", "", true],
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.domain = args[0]
+ self.port = args[1]
+ self.path = args[2]
+ self.registerProcedure = args[3]
+ self.protocol = args[4]
+ end
+ end
+ end
+
+ class Item < Element
+
+ include RSS09
+
+ [
+ ["title", '?', :text],
+ ["link", '?', :text],
+ ["description", '?', :text],
+ ["category", '*', :have_children, "categories"],
+ ["source", '?', :have_child],
+ ["enclosure", '?', :have_child],
+ ].each do |tag, occurs, type, *args|
+ __send__("install_#{type}_element", tag, "", occurs, tag, *args)
+ end
+
+ private
+ def maker_target(items)
+ if items.respond_to?("items")
+ # For backward compatibility
+ items = items.items
+ end
+ items.new_item
+ end
+
+ def setup_maker_element(item)
+ super
+ @enclosure.setup_maker(item) if @enclosure
+ @source.setup_maker(item) if @source
+ end
+
+ class Source < Element
+
+ include RSS09
+
+ [
+ ["url", "", true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required)
+ end
+
+ content_setup
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.url = args[0]
+ self.content = args[1]
+ end
+ end
+
+ private
+ def maker_target(item)
+ item.source
+ end
+
+ def setup_maker_attributes(source)
+ source.url = url
+ source.content = content
+ end
+ end
+
+ class Enclosure < Element
+
+ include RSS09
+
+ [
+ ["url", "", true],
+ ["length", "", true, :integer],
+ ["type", "", true],
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.url = args[0]
+ self.length = args[1]
+ self.type = args[2]
+ end
+ end
+
+ private
+ def maker_target(item)
+ item.enclosure
+ end
+
+ def setup_maker_attributes(enclosure)
+ enclosure.url = url
+ enclosure.length = length
+ enclosure.type = type
+ end
+ end
+
+ class Category < Element
+
+ include RSS09
+
+ [
+ ["domain", "", false]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required)
+ end
+
+ content_setup
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.domain = args[0]
+ self.content = args[1]
+ end
+ end
+
+ private
+ def maker_target(item)
+ item.new_category
+ end
+
+ def setup_maker_attributes(category)
+ category.domain = domain
+ category.content = content
+ end
+
+ end
+
+ end
+
+ class TextInput < Element
+
+ include RSS09
+
+ %w(title description name link).each do |name|
+ install_text_element(name, "", nil)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.title = args[0]
+ self.description = args[1]
+ self.name = args[2]
+ self.link = args[3]
+ end
+ end
+
+ private
+ def maker_target(maker)
+ maker.textinput
+ end
+ end
+
+ end
+
+ end
+
+ RSS09::ELEMENTS.each do |name|
+ BaseListener.install_get_text_element("", name, name)
+ end
+
+ module ListenerMixin
+ private
+ def initial_start_rss(tag_name, prefix, attrs, ns)
+ check_ns(tag_name, prefix, ns, "", false)
+ @rss = Rss.new(attrs['version'], @version, @encoding, @standalone)
+ @rss.do_validate = @do_validate
+ @rss.xml_stylesheets = @xml_stylesheets
+ @last_element = @rss
+ pr = Proc.new do |text, tags|
+ @rss.validate_for_stream(tags, @ignore_unknown_element) if @do_validate
+ end
+ @proc_stack.push(pr)
+ end
+
+ end
+
+end
diff --git a/ruby/lib/rss/1.0.rb b/ruby/lib/rss/1.0.rb
new file mode 100644
index 0000000..0d1e9fe
--- /dev/null
+++ b/ruby/lib/rss/1.0.rb
@@ -0,0 +1,452 @@
+require "rss/parser"
+
+module RSS
+
+ module RSS10
+ NSPOOL = {}
+ ELEMENTS = []
+
+ def self.append_features(klass)
+ super
+
+ klass.install_must_call_validator('', ::RSS::URI)
+ end
+
+ end
+
+ class RDF < Element
+
+ include RSS10
+ include RootElementMixin
+
+ class << self
+
+ def required_uri
+ URI
+ end
+
+ end
+
+ @tag_name = 'RDF'
+
+ PREFIX = 'rdf'
+ URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+
+ install_ns('', ::RSS::URI)
+ install_ns(PREFIX, URI)
+
+ [
+ ["channel", nil],
+ ["image", "?"],
+ ["item", "+", :children],
+ ["textinput", "?"],
+ ].each do |tag, occurs, type|
+ type ||= :child
+ __send__("install_have_#{type}_element", tag, ::RSS::URI, occurs)
+ end
+
+ alias_method(:rss_version, :feed_version)
+ def initialize(version=nil, encoding=nil, standalone=nil)
+ super('1.0', version, encoding, standalone)
+ @feed_type = "rss"
+ end
+
+ def full_name
+ tag_name_with_prefix(PREFIX)
+ end
+
+ class Li < Element
+
+ include RSS10
+
+ class << self
+ def required_uri
+ URI
+ end
+ end
+
+ [
+ ["resource", [URI, ""], true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.resource = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(PREFIX)
+ end
+ end
+
+ class Seq < Element
+
+ include RSS10
+
+ Li = ::RSS::RDF::Li
+
+ class << self
+ def required_uri
+ URI
+ end
+ end
+
+ @tag_name = 'Seq'
+
+ install_have_children_element("li", URI, "*")
+ install_must_call_validator('rdf', ::RSS::RDF::URI)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ @li = args[0] if args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(PREFIX)
+ end
+
+ def setup_maker(target)
+ lis.each do |li|
+ target << li.resource
+ end
+ end
+ end
+
+ class Bag < Element
+
+ include RSS10
+
+ Li = ::RSS::RDF::Li
+
+ class << self
+ def required_uri
+ URI
+ end
+ end
+
+ @tag_name = 'Bag'
+
+ install_have_children_element("li", URI, "*")
+ install_must_call_validator('rdf', URI)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ @li = args[0] if args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(PREFIX)
+ end
+
+ def setup_maker(target)
+ lis.each do |li|
+ target << li.resource
+ end
+ end
+ end
+
+ class Channel < Element
+
+ include RSS10
+
+ class << self
+
+ def required_uri
+ ::RSS::URI
+ end
+
+ end
+
+ [
+ ["about", URI, true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{PREFIX}:#{name}")
+ end
+
+ [
+ ['title', nil, :text],
+ ['link', nil, :text],
+ ['description', nil, :text],
+ ['image', '?', :have_child],
+ ['items', nil, :have_child],
+ ['textinput', '?', :have_child],
+ ].each do |tag, occurs, type|
+ __send__("install_#{type}_element", tag, ::RSS::URI, occurs)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.about = args[0]
+ end
+ end
+
+ private
+ def maker_target(maker)
+ maker.channel
+ end
+
+ def setup_maker_attributes(channel)
+ channel.about = about
+ end
+
+ class Image < Element
+
+ include RSS10
+
+ class << self
+
+ def required_uri
+ ::RSS::URI
+ end
+
+ end
+
+ [
+ ["resource", URI, true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{PREFIX}:#{name}")
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.resource = args[0]
+ end
+ end
+ end
+
+ class Textinput < Element
+
+ include RSS10
+
+ class << self
+
+ def required_uri
+ ::RSS::URI
+ end
+
+ end
+
+ [
+ ["resource", URI, true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{PREFIX}:#{name}")
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.resource = args[0]
+ end
+ end
+ end
+
+ class Items < Element
+
+ include RSS10
+
+ Seq = ::RSS::RDF::Seq
+
+ class << self
+
+ def required_uri
+ ::RSS::URI
+ end
+
+ end
+
+ install_have_child_element("Seq", URI, nil)
+ install_must_call_validator('rdf', URI)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.Seq = args[0]
+ end
+ self.Seq ||= Seq.new
+ end
+
+ def resources
+ if @Seq
+ @Seq.lis.collect do |li|
+ li.resource
+ end
+ else
+ []
+ end
+ end
+ end
+ end
+
+ class Image < Element
+
+ include RSS10
+
+ class << self
+
+ def required_uri
+ ::RSS::URI
+ end
+
+ end
+
+ [
+ ["about", URI, true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{PREFIX}:#{name}")
+ end
+
+ %w(title url link).each do |name|
+ install_text_element(name, ::RSS::URI, nil)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.about = args[0]
+ end
+ end
+
+ private
+ def maker_target(maker)
+ maker.image
+ end
+ end
+
+ class Item < Element
+
+ include RSS10
+
+ class << self
+
+ def required_uri
+ ::RSS::URI
+ end
+
+ end
+
+
+ [
+ ["about", URI, true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{PREFIX}:#{name}")
+ end
+
+ [
+ ["title", nil],
+ ["link", nil],
+ ["description", "?"],
+ ].each do |tag, occurs|
+ install_text_element(tag, ::RSS::URI, occurs)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.about = args[0]
+ end
+ end
+
+ private
+ def maker_target(items)
+ if items.respond_to?("items")
+ # For backward compatibility
+ items = items.items
+ end
+ items.new_item
+ end
+ end
+
+ class Textinput < Element
+
+ include RSS10
+
+ class << self
+
+ def required_uri
+ ::RSS::URI
+ end
+
+ end
+
+ [
+ ["about", URI, true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{PREFIX}:#{name}")
+ end
+
+ %w(title description name link).each do |name|
+ install_text_element(name, ::RSS::URI, nil)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.about = args[0]
+ end
+ end
+
+ private
+ def maker_target(maker)
+ maker.textinput
+ end
+ end
+
+ end
+
+ RSS10::ELEMENTS.each do |name|
+ BaseListener.install_get_text_element(URI, name, name)
+ end
+
+ module ListenerMixin
+ private
+ def initial_start_RDF(tag_name, prefix, attrs, ns)
+ check_ns(tag_name, prefix, ns, RDF::URI, false)
+
+ @rss = RDF.new(@version, @encoding, @standalone)
+ @rss.do_validate = @do_validate
+ @rss.xml_stylesheets = @xml_stylesheets
+ @last_element = @rss
+ pr = Proc.new do |text, tags|
+ @rss.validate_for_stream(tags, @ignore_unknown_element) if @do_validate
+ end
+ @proc_stack.push(pr)
+ end
+ end
+
+end
diff --git a/ruby/lib/rss/2.0.rb b/ruby/lib/rss/2.0.rb
new file mode 100644
index 0000000..3798da4
--- /dev/null
+++ b/ruby/lib/rss/2.0.rb
@@ -0,0 +1,111 @@
+require "rss/0.9"
+
+module RSS
+
+ class Rss
+
+ class Channel
+
+ [
+ ["generator"],
+ ["ttl", :integer],
+ ].each do |name, type|
+ install_text_element(name, "", "?", name, type)
+ end
+
+ [
+ %w(category categories),
+ ].each do |name, plural_name|
+ install_have_children_element(name, "", "*", name, plural_name)
+ end
+
+ [
+ ["image", "?"],
+ ["language", "?"],
+ ].each do |name, occurs|
+ install_model(name, "", occurs)
+ end
+
+ Category = Item::Category
+
+ class Item
+
+ [
+ ["comments", "?"],
+ ["author", "?"],
+ ].each do |name, occurs|
+ install_text_element(name, "", occurs)
+ end
+
+ [
+ ["pubDate", '?'],
+ ].each do |name, occurs|
+ install_date_element(name, "", occurs, name, 'rfc822')
+ end
+ alias date pubDate
+ alias date= pubDate=
+
+ [
+ ["guid", '?'],
+ ].each do |name, occurs|
+ install_have_child_element(name, "", occurs)
+ end
+
+ private
+ alias _setup_maker_element setup_maker_element
+ def setup_maker_element(item)
+ _setup_maker_element(item)
+ @guid.setup_maker(item) if @guid
+ end
+
+ class Guid < Element
+
+ include RSS09
+
+ [
+ ["isPermaLink", "", false, :boolean]
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
+ end
+
+ content_setup
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.isPermaLink = args[0]
+ self.content = args[1]
+ end
+ end
+
+ alias_method :_PermaLink?, :PermaLink?
+ private :_PermaLink?
+ def PermaLink?
+ perma = _PermaLink?
+ perma or perma.nil?
+ end
+
+ private
+ def maker_target(item)
+ item.guid
+ end
+
+ def setup_maker_attributes(guid)
+ guid.isPermaLink = isPermaLink
+ guid.content = content
+ end
+ end
+
+ end
+
+ end
+
+ end
+
+ RSS09::ELEMENTS.each do |name|
+ BaseListener.install_get_text_element("", name, name)
+ end
+
+end
diff --git a/ruby/lib/rss/atom.rb b/ruby/lib/rss/atom.rb
new file mode 100644
index 0000000..ff8ff22
--- /dev/null
+++ b/ruby/lib/rss/atom.rb
@@ -0,0 +1,748 @@
+require 'rss/parser'
+
+module RSS
+ module Atom
+ URI = "http://www.w3.org/2005/Atom"
+ XHTML_URI = "http://www.w3.org/1999/xhtml"
+
+ module CommonModel
+ NSPOOL = {}
+ ELEMENTS = []
+
+ def self.append_features(klass)
+ super
+ klass.install_must_call_validator("atom", URI)
+ [
+ ["lang", :xml],
+ ["base", :xml],
+ ].each do |name, uri, required|
+ klass.install_get_attribute(name, uri, required, [nil, :inherit])
+ end
+ klass.class_eval do
+ class << self
+ def required_uri
+ URI
+ end
+
+ def need_parent?
+ true
+ end
+ end
+ end
+ end
+ end
+
+ module ContentModel
+ module ClassMethods
+ def content_type
+ @content_type ||= nil
+ end
+ end
+
+ class << self
+ def append_features(klass)
+ super
+ klass.extend(ClassMethods)
+ klass.content_setup(klass.content_type, klass.tag_name)
+ end
+ end
+
+ def maker_target(target)
+ target
+ end
+
+ private
+ def setup_maker_element_writer
+ "#{self.class.name.split(/::/).last.downcase}="
+ end
+
+ def setup_maker_element(target)
+ target.__send__(setup_maker_element_writer, content)
+ super
+ end
+ end
+
+ module URIContentModel
+ class << self
+ def append_features(klass)
+ super
+ klass.class_eval do
+ @content_type = [nil, :uri]
+ include(ContentModel)
+ end
+ end
+ end
+ end
+
+ module TextConstruct
+ def self.append_features(klass)
+ super
+ klass.class_eval do
+ [
+ ["type", ""],
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, :text_type)
+ end
+
+ content_setup
+ add_need_initialize_variable("xhtml")
+
+ class << self
+ def xml_getter
+ "xhtml"
+ end
+
+ def xml_setter
+ "xhtml="
+ end
+ end
+ end
+ end
+
+ attr_writer :xhtml
+ def xhtml
+ return @xhtml if @xhtml.nil?
+ if @xhtml.is_a?(XML::Element) and
+ [@xhtml.name, @xhtml.uri] == ["div", XHTML_URI]
+ return @xhtml
+ end
+
+ children = @xhtml
+ children = [children] unless children.is_a?(Array)
+ XML::Element.new("div", nil, XHTML_URI,
+ {"xmlns" => XHTML_URI}, children)
+ end
+
+ def have_xml_content?
+ @type == "xhtml"
+ end
+
+ def atom_validate(ignore_unknown_element, tags, uri)
+ if have_xml_content?
+ if @xhtml.nil?
+ raise MissingTagError.new("div", tag_name)
+ end
+ unless [@xhtml.name, @xhtml.uri] == ["div", XHTML_URI]
+ raise NotExpectedTagError.new(@xhtml.name, @xhtml.uri, tag_name)
+ end
+ end
+ end
+
+ private
+ def maker_target(target)
+ target.__send__(self.class.name.split(/::/).last.downcase) {|x| x}
+ end
+
+ def setup_maker_attributes(target)
+ target.type = type
+ target.content = content
+ target.xml_content = @xhtml
+ end
+ end
+
+ module PersonConstruct
+ def self.append_features(klass)
+ super
+ klass.class_eval do
+ [
+ ["name", nil],
+ ["uri", "?"],
+ ["email", "?"],
+ ].each do |tag, occurs|
+ install_have_attribute_element(tag, URI, occurs, nil, :content)
+ end
+ end
+ end
+
+ def maker_target(target)
+ target.__send__("new_#{self.class.name.split(/::/).last.downcase}")
+ end
+
+ class Name < RSS::Element
+ include CommonModel
+ include ContentModel
+ end
+
+ class Uri < RSS::Element
+ include CommonModel
+ include URIContentModel
+ end
+
+ class Email < RSS::Element
+ include CommonModel
+ include ContentModel
+ end
+ end
+
+ module DateConstruct
+ def self.append_features(klass)
+ super
+ klass.class_eval do
+ @content_type = :w3cdtf
+ include(ContentModel)
+ end
+ end
+
+ def atom_validate(ignore_unknown_element, tags, uri)
+ raise NotAvailableValueError.new(tag_name, "") if content.nil?
+ end
+ end
+
+ module DuplicateLinkChecker
+ def validate_duplicate_links(links)
+ link_infos = {}
+ links.each do |link|
+ rel = link.rel || "alternate"
+ next unless rel == "alternate"
+ key = [link.hreflang, link.type]
+ if link_infos.has_key?(key)
+ raise TooMuchTagError.new("link", tag_name)
+ end
+ link_infos[key] = true
+ end
+ end
+ end
+
+ class Feed < RSS::Element
+ include RootElementMixin
+ include CommonModel
+ include DuplicateLinkChecker
+
+ install_ns('', URI)
+
+ [
+ ["author", "*", :children],
+ ["category", "*", :children, "categories"],
+ ["contributor", "*", :children],
+ ["generator", "?"],
+ ["icon", "?", nil, :content],
+ ["id", nil, nil, :content],
+ ["link", "*", :children],
+ ["logo", "?"],
+ ["rights", "?"],
+ ["subtitle", "?", nil, :content],
+ ["title", nil, nil, :content],
+ ["updated", nil, nil, :content],
+ ["entry", "*", :children, "entries"],
+ ].each do |tag, occurs, type, *args|
+ type ||= :child
+ __send__("install_have_#{type}_element",
+ tag, URI, occurs, tag, *args)
+ end
+
+ def initialize(version=nil, encoding=nil, standalone=nil)
+ super("1.0", version, encoding, standalone)
+ @feed_type = "atom"
+ @feed_subtype = "feed"
+ end
+
+ alias_method :items, :entries
+
+ def have_author?
+ authors.any? {|author| !author.to_s.empty?} or
+ entries.any? {|entry| entry.have_author?(false)}
+ end
+
+ private
+ def atom_validate(ignore_unknown_element, tags, uri)
+ unless have_author?
+ raise MissingTagError.new("author", tag_name)
+ end
+ validate_duplicate_links(links)
+ end
+
+ def have_required_elements?
+ super and have_author?
+ end
+
+ def maker_target(maker)
+ maker.channel
+ end
+
+ def setup_maker_element(channel)
+ prev_dc_dates = channel.dc_dates.to_a.dup
+ super
+ channel.about = id.content if id
+ channel.dc_dates.replace(prev_dc_dates)
+ end
+
+ def setup_maker_elements(channel)
+ super
+ items = channel.maker.items
+ entries.each do |entry|
+ entry.setup_maker(items)
+ end
+ end
+
+ class Author < RSS::Element
+ include CommonModel
+ include PersonConstruct
+ end
+
+ class Category < RSS::Element
+ include CommonModel
+
+ [
+ ["term", "", true],
+ ["scheme", "", false, [nil, :uri]],
+ ["label", ""],
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
+ end
+
+ private
+ def maker_target(target)
+ target.new_category
+ end
+ end
+
+ class Contributor < RSS::Element
+ include CommonModel
+ include PersonConstruct
+ end
+
+ class Generator < RSS::Element
+ include CommonModel
+ include ContentModel
+
+ [
+ ["uri", "", false, [nil, :uri]],
+ ["version", ""],
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
+ end
+
+ private
+ def setup_maker_attributes(target)
+ target.generator do |generator|
+ generator.uri = uri if uri
+ generator.version = version if version
+ end
+ end
+ end
+
+ class Icon < RSS::Element
+ include CommonModel
+ include URIContentModel
+ end
+
+ class Id < RSS::Element
+ include CommonModel
+ include URIContentModel
+ end
+
+ class Link < RSS::Element
+ include CommonModel
+
+ [
+ ["href", "", true, [nil, :uri]],
+ ["rel", ""],
+ ["type", ""],
+ ["hreflang", ""],
+ ["title", ""],
+ ["length", ""],
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
+ end
+
+ private
+ def maker_target(target)
+ target.new_link
+ end
+ end
+
+ class Logo < RSS::Element
+ include CommonModel
+ include URIContentModel
+
+ def maker_target(target)
+ target.maker.image
+ end
+
+ private
+ def setup_maker_element_writer
+ "url="
+ end
+ end
+
+ class Rights < RSS::Element
+ include CommonModel
+ include TextConstruct
+ end
+
+ class Subtitle < RSS::Element
+ include CommonModel
+ include TextConstruct
+ end
+
+ class Title < RSS::Element
+ include CommonModel
+ include TextConstruct
+ end
+
+ class Updated < RSS::Element
+ include CommonModel
+ include DateConstruct
+ end
+
+ class Entry < RSS::Element
+ include CommonModel
+ include DuplicateLinkChecker
+
+ [
+ ["author", "*", :children],
+ ["category", "*", :children, "categories"],
+ ["content", "?", :child],
+ ["contributor", "*", :children],
+ ["id", nil, nil, :content],
+ ["link", "*", :children],
+ ["published", "?", :child, :content],
+ ["rights", "?", :child],
+ ["source", "?"],
+ ["summary", "?", :child],
+ ["title", nil],
+ ["updated", nil, :child, :content],
+ ].each do |tag, occurs, type, *args|
+ type ||= :attribute
+ __send__("install_have_#{type}_element",
+ tag, URI, occurs, tag, *args)
+ end
+
+ def have_author?(check_parent=true)
+ authors.any? {|author| !author.to_s.empty?} or
+ (check_parent and @parent and @parent.have_author?) or
+ (source and source.have_author?)
+ end
+
+ private
+ def atom_validate(ignore_unknown_element, tags, uri)
+ unless have_author?
+ raise MissingTagError.new("author", tag_name)
+ end
+ validate_duplicate_links(links)
+ end
+
+ def have_required_elements?
+ super and have_author?
+ end
+
+ def maker_target(items)
+ if items.respond_to?("items")
+ # For backward compatibility
+ items = items.items
+ end
+ items.new_item
+ end
+
+ Author = Feed::Author
+ Category = Feed::Category
+
+ class Content < RSS::Element
+ include CommonModel
+
+ class << self
+ def xml_setter
+ "xml="
+ end
+
+ def xml_getter
+ "xml"
+ end
+ end
+
+ [
+ ["type", ""],
+ ["src", "", false, [nil, :uri]],
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
+ end
+
+ content_setup
+ add_need_initialize_variable("xml")
+
+ attr_writer :xml
+ def have_xml_content?
+ inline_xhtml? or inline_other_xml?
+ end
+
+ def xml
+ return @xml unless inline_xhtml?
+ return @xml if @xml.nil?
+ if @xml.is_a?(XML::Element) and
+ [@xml.name, @xml.uri] == ["div", XHTML_URI]
+ return @xml
+ end
+
+ children = @xml
+ children = [children] unless children.is_a?(Array)
+ XML::Element.new("div", nil, XHTML_URI,
+ {"xmlns" => XHTML_URI}, children)
+ end
+
+ def xhtml
+ if inline_xhtml?
+ xml
+ else
+ nil
+ end
+ end
+
+ def atom_validate(ignore_unknown_element, tags, uri)
+ if out_of_line?
+ raise MissingAttributeError.new(tag_name, "type") if @type.nil?
+ unless (content.nil? or content.empty?)
+ raise NotAvailableValueError.new(tag_name, content)
+ end
+ elsif inline_xhtml?
+ if @xml.nil?
+ raise MissingTagError.new("div", tag_name)
+ end
+ unless @xml.name == "div" and @xml.uri == XHTML_URI
+ raise NotExpectedTagError.new(@xml.name, @xml.uri, tag_name)
+ end
+ end
+ end
+
+ def inline_text?
+ !out_of_line? and [nil, "text", "html"].include?(@type)
+ end
+
+ def inline_html?
+ return false if out_of_line?
+ @type == "html" or mime_split == ["text", "html"]
+ end
+
+ def inline_xhtml?
+ !out_of_line? and @type == "xhtml"
+ end
+
+ def inline_other?
+ return false if out_of_line?
+ media_type, subtype = mime_split
+ return false if media_type.nil? or subtype.nil?
+ true
+ end
+
+ def inline_other_text?
+ return false unless inline_other?
+ return false if inline_other_xml?
+
+ media_type, subtype = mime_split
+ return true if "text" == media_type.downcase
+ false
+ end
+
+ def inline_other_xml?
+ return false unless inline_other?
+
+ media_type, subtype = mime_split
+ normalized_mime_type = "#{media_type}/#{subtype}".downcase
+ if /(?:\+xml|^xml)$/ =~ subtype or
+ %w(text/xml-external-parsed-entity
+ application/xml-external-parsed-entity
+ application/xml-dtd).find {|x| x == normalized_mime_type}
+ return true
+ end
+ false
+ end
+
+ def inline_other_base64?
+ inline_other? and !inline_other_text? and !inline_other_xml?
+ end
+
+ def out_of_line?
+ not @src.nil?
+ end
+
+ def mime_split
+ media_type = subtype = nil
+ if /\A\s*([a-z]+)\/([a-z\+]+)\s*(?:;.*)?\z/i =~ @type.to_s
+ media_type = $1.downcase
+ subtype = $2.downcase
+ end
+ [media_type, subtype]
+ end
+
+ def need_base64_encode?
+ inline_other_base64?
+ end
+
+ private
+ def empty_content?
+ out_of_line? or super
+ end
+ end
+
+ Contributor = Feed::Contributor
+ Id = Feed::Id
+ Link = Feed::Link
+
+ class Published < RSS::Element
+ include CommonModel
+ include DateConstruct
+ end
+
+ Rights = Feed::Rights
+
+ class Source < RSS::Element
+ include CommonModel
+
+ [
+ ["author", "*", :children],
+ ["category", "*", :children, "categories"],
+ ["contributor", "*", :children],
+ ["generator", "?"],
+ ["icon", "?"],
+ ["id", "?", nil, :content],
+ ["link", "*", :children],
+ ["logo", "?"],
+ ["rights", "?"],
+ ["subtitle", "?"],
+ ["title", "?"],
+ ["updated", "?", nil, :content],
+ ].each do |tag, occurs, type, *args|
+ type ||= :attribute
+ __send__("install_have_#{type}_element",
+ tag, URI, occurs, tag, *args)
+ end
+
+ def have_author?
+ !author.to_s.empty?
+ end
+
+ Author = Feed::Author
+ Category = Feed::Category
+ Contributor = Feed::Contributor
+ Generator = Feed::Generator
+ Icon = Feed::Icon
+ Id = Feed::Id
+ Link = Feed::Link
+ Logo = Feed::Logo
+ Rights = Feed::Rights
+ Subtitle = Feed::Subtitle
+ Title = Feed::Title
+ Updated = Feed::Updated
+ end
+
+ class Summary < RSS::Element
+ include CommonModel
+ include TextConstruct
+ end
+
+ Title = Feed::Title
+ Updated = Feed::Updated
+ end
+ end
+
+ class Entry < RSS::Element
+ include RootElementMixin
+ include CommonModel
+ include DuplicateLinkChecker
+
+ [
+ ["author", "*", :children],
+ ["category", "*", :children, "categories"],
+ ["content", "?"],
+ ["contributor", "*", :children],
+ ["id", nil, nil, :content],
+ ["link", "*", :children],
+ ["published", "?", :child, :content],
+ ["rights", "?"],
+ ["source", "?"],
+ ["summary", "?"],
+ ["title", nil],
+ ["updated", nil, nil, :content],
+ ].each do |tag, occurs, type, *args|
+ type ||= :attribute
+ __send__("install_have_#{type}_element",
+ tag, URI, occurs, tag, *args)
+ end
+
+ def initialize(version=nil, encoding=nil, standalone=nil)
+ super("1.0", version, encoding, standalone)
+ @feed_type = "atom"
+ @feed_subtype = "entry"
+ end
+
+ def items
+ [self]
+ end
+
+ def setup_maker(maker)
+ maker = maker.maker if maker.respond_to?("maker")
+ super(maker)
+ end
+
+ def have_author?
+ authors.any? {|author| !author.to_s.empty?} or
+ (source and source.have_author?)
+ end
+
+ private
+ def atom_validate(ignore_unknown_element, tags, uri)
+ unless have_author?
+ raise MissingTagError.new("author", tag_name)
+ end
+ validate_duplicate_links(links)
+ end
+
+ def have_required_elements?
+ super and have_author?
+ end
+
+ def maker_target(maker)
+ maker.items.new_item
+ end
+
+ Author = Feed::Entry::Author
+ Category = Feed::Entry::Category
+ Content = Feed::Entry::Content
+ Contributor = Feed::Entry::Contributor
+ Id = Feed::Entry::Id
+ Link = Feed::Entry::Link
+ Published = Feed::Entry::Published
+ Rights = Feed::Entry::Rights
+ Source = Feed::Entry::Source
+ Summary = Feed::Entry::Summary
+ Title = Feed::Entry::Title
+ Updated = Feed::Entry::Updated
+ end
+ end
+
+ Atom::CommonModel::ELEMENTS.each do |name|
+ BaseListener.install_get_text_element(Atom::URI, name, "#{name}=")
+ end
+
+ module ListenerMixin
+ private
+ def initial_start_feed(tag_name, prefix, attrs, ns)
+ check_ns(tag_name, prefix, ns, Atom::URI, false)
+
+ @rss = Atom::Feed.new(@version, @encoding, @standalone)
+ @rss.do_validate = @do_validate
+ @rss.xml_stylesheets = @xml_stylesheets
+ @rss.lang = attrs["xml:lang"]
+ @rss.base = attrs["xml:base"]
+ @last_element = @rss
+ pr = Proc.new do |text, tags|
+ @rss.validate_for_stream(tags) if @do_validate
+ end
+ @proc_stack.push(pr)
+ end
+
+ def initial_start_entry(tag_name, prefix, attrs, ns)
+ check_ns(tag_name, prefix, ns, Atom::URI, false)
+
+ @rss = Atom::Entry.new(@version, @encoding, @standalone)
+ @rss.do_validate = @do_validate
+ @rss.xml_stylesheets = @xml_stylesheets
+ @rss.lang = attrs["xml:lang"]
+ @rss.base = attrs["xml:base"]
+ @last_element = @rss
+ pr = Proc.new do |text, tags|
+ @rss.validate_for_stream(tags) if @do_validate
+ end
+ @proc_stack.push(pr)
+ end
+ end
+end
diff --git a/ruby/lib/rss/content.rb b/ruby/lib/rss/content.rb
new file mode 100644
index 0000000..b12ee91
--- /dev/null
+++ b/ruby/lib/rss/content.rb
@@ -0,0 +1,31 @@
+require "rss/rss"
+
+module RSS
+ CONTENT_PREFIX = 'content'
+ CONTENT_URI = "http://purl.org/rss/1.0/modules/content/"
+
+ module ContentModel
+ extend BaseModel
+
+ ELEMENTS = ["#{CONTENT_PREFIX}_encoded"]
+
+ def self.append_features(klass)
+ super
+
+ klass.install_must_call_validator(CONTENT_PREFIX, CONTENT_URI)
+ ELEMENTS.each do |full_name|
+ name = full_name[(CONTENT_PREFIX.size + 1)..-1]
+ klass.install_text_element(name, CONTENT_URI, "?", full_name)
+ end
+ end
+ end
+
+ prefix_size = CONTENT_PREFIX.size + 1
+ ContentModel::ELEMENTS.each do |full_name|
+ name = full_name[prefix_size..-1]
+ BaseListener.install_get_text_element(CONTENT_URI, name, full_name)
+ end
+end
+
+require 'rss/content/1.0'
+require 'rss/content/2.0'
diff --git a/ruby/lib/rss/content/1.0.rb b/ruby/lib/rss/content/1.0.rb
new file mode 100644
index 0000000..e7c0c19
--- /dev/null
+++ b/ruby/lib/rss/content/1.0.rb
@@ -0,0 +1,10 @@
+require 'rss/1.0'
+require 'rss/content'
+
+module RSS
+ RDF.install_ns(CONTENT_PREFIX, CONTENT_URI)
+
+ class RDF
+ class Item; include ContentModel; end
+ end
+end
diff --git a/ruby/lib/rss/content/2.0.rb b/ruby/lib/rss/content/2.0.rb
new file mode 100644
index 0000000..8671b5b
--- /dev/null
+++ b/ruby/lib/rss/content/2.0.rb
@@ -0,0 +1,12 @@
+require "rss/2.0"
+require "rss/content"
+
+module RSS
+ Rss.install_ns(CONTENT_PREFIX, CONTENT_URI)
+
+ class Rss
+ class Channel
+ class Item; include ContentModel; end
+ end
+ end
+end
diff --git a/ruby/lib/rss/converter.rb b/ruby/lib/rss/converter.rb
new file mode 100644
index 0000000..745d6de
--- /dev/null
+++ b/ruby/lib/rss/converter.rb
@@ -0,0 +1,170 @@
+require "rss/utils"
+
+module RSS
+
+ class Converter
+
+ include Utils
+
+ def initialize(to_enc, from_enc=nil)
+ if "".respond_to?(:encode)
+ @to_encoding = to_enc
+ return
+ end
+ normalized_to_enc = to_enc.downcase.gsub(/-/, '_')
+ from_enc ||= 'utf-8'
+ normalized_from_enc = from_enc.downcase.gsub(/-/, '_')
+ if normalized_to_enc == normalized_from_enc
+ def_same_enc()
+ else
+ def_diff_enc = "def_to_#{normalized_to_enc}_from_#{normalized_from_enc}"
+ if respond_to?(def_diff_enc)
+ __send__(def_diff_enc)
+ else
+ def_else_enc(to_enc, from_enc)
+ end
+ end
+ end
+
+ def convert(value)
+ if value.is_a?(String) and value.respond_to?(:encode)
+ value.encode(@to_encoding)
+ else
+ value
+ end
+ end
+
+ def def_convert(depth=0)
+ instance_eval(<<-EOC, *get_file_and_line_from_caller(depth))
+ def convert(value)
+ if value.kind_of?(String)
+ #{yield('value')}
+ else
+ value
+ end
+ end
+ EOC
+ end
+
+ def def_iconv_convert(to_enc, from_enc, depth=0)
+ begin
+ require "iconv"
+ @iconv = Iconv.new(to_enc, from_enc)
+ def_convert(depth+1) do |value|
+ <<-EOC
+ begin
+ @iconv.iconv(#{value})
+ rescue Iconv::Failure
+ raise ConversionError.new(#{value}, "#{to_enc}", "#{from_enc}")
+ end
+ EOC
+ end
+ rescue LoadError, ArgumentError, SystemCallError
+ raise UnknownConversionMethodError.new(to_enc, from_enc)
+ end
+ end
+
+ def def_else_enc(to_enc, from_enc)
+ def_iconv_convert(to_enc, from_enc, 0)
+ end
+
+ def def_same_enc()
+ def_convert do |value|
+ value
+ end
+ end
+
+ def def_uconv_convert_if_can(meth, to_enc, from_enc, nkf_arg)
+ begin
+ require "uconv"
+ def_convert(1) do |value|
+ <<-EOC
+ begin
+ Uconv.#{meth}(#{value})
+ rescue Uconv::Error
+ raise ConversionError.new(#{value}, "#{to_enc}", "#{from_enc}")
+ end
+ EOC
+ end
+ rescue LoadError
+ require 'nkf'
+ if NKF.const_defined?(:UTF8)
+ def_convert(1) do |value|
+ "NKF.nkf(#{nkf_arg.dump}, #{value})"
+ end
+ else
+ def_iconv_convert(to_enc, from_enc, 1)
+ end
+ end
+ end
+
+ def def_to_euc_jp_from_utf_8
+ def_uconv_convert_if_can('u8toeuc', 'EUC-JP', 'UTF-8', '-We')
+ end
+
+ def def_to_utf_8_from_euc_jp
+ def_uconv_convert_if_can('euctou8', 'UTF-8', 'EUC-JP', '-Ew')
+ end
+
+ def def_to_shift_jis_from_utf_8
+ def_uconv_convert_if_can('u8tosjis', 'Shift_JIS', 'UTF-8', '-Ws')
+ end
+
+ def def_to_utf_8_from_shift_jis
+ def_uconv_convert_if_can('sjistou8', 'UTF-8', 'Shift_JIS', '-Sw')
+ end
+
+ def def_to_euc_jp_from_shift_jis
+ require "nkf"
+ def_convert do |value|
+ "NKF.nkf('-Se', #{value})"
+ end
+ end
+
+ def def_to_shift_jis_from_euc_jp
+ require "nkf"
+ def_convert do |value|
+ "NKF.nkf('-Es', #{value})"
+ end
+ end
+
+ def def_to_euc_jp_from_iso_2022_jp
+ require "nkf"
+ def_convert do |value|
+ "NKF.nkf('-Je', #{value})"
+ end
+ end
+
+ def def_to_iso_2022_jp_from_euc_jp
+ require "nkf"
+ def_convert do |value|
+ "NKF.nkf('-Ej', #{value})"
+ end
+ end
+
+ def def_to_utf_8_from_iso_8859_1
+ def_convert do |value|
+ "#{value}.unpack('C*').pack('U*')"
+ end
+ end
+
+ def def_to_iso_8859_1_from_utf_8
+ def_convert do |value|
+ <<-EOC
+ array_utf8 = #{value}.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ if num <= 0xFF
+ array_enc << num
+ else
+ array_enc.concat "&\#\#{num};".unpack('C*')
+ end
+ end
+ array_enc.pack('C*')
+ EOC
+ end
+ end
+
+ end
+
+end
diff --git a/ruby/lib/rss/dublincore.rb b/ruby/lib/rss/dublincore.rb
new file mode 100644
index 0000000..7ba239f
--- /dev/null
+++ b/ruby/lib/rss/dublincore.rb
@@ -0,0 +1,161 @@
+require "rss/rss"
+
+module RSS
+ DC_PREFIX = 'dc'
+ DC_URI = "http://purl.org/dc/elements/1.1/"
+
+ module BaseDublinCoreModel
+ def append_features(klass)
+ super
+
+ return if klass.instance_of?(Module)
+ DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
+ plural = plural_name || "#{name}s"
+ full_name = "#{DC_PREFIX}_#{name}"
+ full_plural_name = "#{DC_PREFIX}_#{plural}"
+ klass_name = "DublinCore#{Utils.to_class_name(name)}"
+ klass.install_must_call_validator(DC_PREFIX, DC_URI)
+ klass.install_have_children_element(name, DC_URI, "*",
+ full_name, full_plural_name)
+ klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0))
+ remove_method :#{full_name}
+ remove_method :#{full_name}=
+ remove_method :set_#{full_name}
+
+ def #{full_name}
+ @#{full_name}.first and @#{full_name}.first.value
+ end
+
+ def #{full_name}=(new_value)
+ @#{full_name}[0] = Utils.new_with_value_if_need(#{klass_name}, new_value)
+ end
+ alias set_#{full_name} #{full_name}=
+ EOC
+ end
+ klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0))
+ if method_defined?(:date)
+ alias date_without_#{DC_PREFIX}_date= date=
+
+ def date=(value)
+ self.date_without_#{DC_PREFIX}_date = value
+ self.#{DC_PREFIX}_date = value
+ end
+ else
+ alias date #{DC_PREFIX}_date
+ alias date= #{DC_PREFIX}_date=
+ end
+
+ # For backward compatibility
+ alias #{DC_PREFIX}_rightses #{DC_PREFIX}_rights_list
+ EOC
+ end
+ end
+
+ module DublinCoreModel
+
+ extend BaseModel
+ extend BaseDublinCoreModel
+
+ TEXT_ELEMENTS = {
+ "title" => nil,
+ "description" => nil,
+ "creator" => nil,
+ "subject" => nil,
+ "publisher" => nil,
+ "contributor" => nil,
+ "type" => nil,
+ "format" => nil,
+ "identifier" => nil,
+ "source" => nil,
+ "language" => nil,
+ "relation" => nil,
+ "coverage" => nil,
+ "rights" => "rights_list"
+ }
+
+ DATE_ELEMENTS = {
+ "date" => "w3cdtf",
+ }
+
+ ELEMENT_NAME_INFOS = DublinCoreModel::TEXT_ELEMENTS.to_a
+ DublinCoreModel::DATE_ELEMENTS.each do |name, |
+ ELEMENT_NAME_INFOS << [name, nil]
+ end
+
+ ELEMENTS = TEXT_ELEMENTS.keys + DATE_ELEMENTS.keys
+
+ ELEMENTS.each do |name, plural_name|
+ module_eval(<<-EOC, *get_file_and_line_from_caller(0))
+ class DublinCore#{Utils.to_class_name(name)} < Element
+ include RSS10
+
+ content_setup
+
+ class << self
+ def required_prefix
+ DC_PREFIX
+ end
+
+ def required_uri
+ DC_URI
+ end
+ end
+
+ @tag_name = #{name.dump}
+
+ alias_method(:value, :content)
+ alias_method(:value=, :content=)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.content = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(DC_PREFIX)
+ end
+
+ def maker_target(target)
+ target.new_#{name}
+ end
+
+ def setup_maker_attributes(#{name})
+ #{name}.content = content
+ end
+ end
+ EOC
+ end
+
+ DATE_ELEMENTS.each do |name, type|
+ tag_name = "#{DC_PREFIX}:#{name}"
+ module_eval(<<-EOC, *get_file_and_line_from_caller(0))
+ class DublinCore#{Utils.to_class_name(name)} < Element
+ remove_method(:content=)
+ remove_method(:value=)
+
+ date_writer("content", #{type.dump}, #{tag_name.dump})
+
+ alias_method(:value=, :content=)
+ end
+ EOC
+ end
+ end
+
+ # For backward compatibility
+ DublincoreModel = DublinCoreModel
+
+ DublinCoreModel::ELEMENTS.each do |name|
+ class_name = Utils.to_class_name(name)
+ BaseListener.install_class_name(DC_URI, name, "DublinCore#{class_name}")
+ end
+
+ DublinCoreModel::ELEMENTS.collect! {|name| "#{DC_PREFIX}_#{name}"}
+end
+
+require 'rss/dublincore/1.0'
+require 'rss/dublincore/2.0'
+require 'rss/dublincore/atom'
diff --git a/ruby/lib/rss/dublincore/1.0.rb b/ruby/lib/rss/dublincore/1.0.rb
new file mode 100644
index 0000000..e193c6d
--- /dev/null
+++ b/ruby/lib/rss/dublincore/1.0.rb
@@ -0,0 +1,13 @@
+require "rss/1.0"
+require "rss/dublincore"
+
+module RSS
+ RDF.install_ns(DC_PREFIX, DC_URI)
+
+ class RDF
+ class Channel; include DublinCoreModel; end
+ class Image; include DublinCoreModel; end
+ class Item; include DublinCoreModel; end
+ class Textinput; include DublinCoreModel; end
+ end
+end
diff --git a/ruby/lib/rss/dublincore/2.0.rb b/ruby/lib/rss/dublincore/2.0.rb
new file mode 100644
index 0000000..82ed188
--- /dev/null
+++ b/ruby/lib/rss/dublincore/2.0.rb
@@ -0,0 +1,13 @@
+require "rss/2.0"
+require "rss/dublincore"
+
+module RSS
+ Rss.install_ns(DC_PREFIX, DC_URI)
+
+ class Rss
+ class Channel
+ include DublinCoreModel
+ class Item; include DublinCoreModel; end
+ end
+ end
+end
diff --git a/ruby/lib/rss/dublincore/atom.rb b/ruby/lib/rss/dublincore/atom.rb
new file mode 100644
index 0000000..e78df48
--- /dev/null
+++ b/ruby/lib/rss/dublincore/atom.rb
@@ -0,0 +1,17 @@
+require "rss/atom"
+require "rss/dublincore"
+
+module RSS
+ module Atom
+ Feed.install_ns(DC_PREFIX, DC_URI)
+
+ class Feed
+ include DublinCoreModel
+ class Entry; include DublinCoreModel; end
+ end
+
+ class Entry
+ include DublinCoreModel
+ end
+ end
+end
diff --git a/ruby/lib/rss/image.rb b/ruby/lib/rss/image.rb
new file mode 100644
index 0000000..c4714ae
--- /dev/null
+++ b/ruby/lib/rss/image.rb
@@ -0,0 +1,193 @@
+require 'rss/1.0'
+require 'rss/dublincore'
+
+module RSS
+
+ IMAGE_PREFIX = 'image'
+ IMAGE_URI = 'http://purl.org/rss/1.0/modules/image/'
+
+ RDF.install_ns(IMAGE_PREFIX, IMAGE_URI)
+
+ IMAGE_ELEMENTS = []
+
+ %w(item favicon).each do |name|
+ class_name = Utils.to_class_name(name)
+ BaseListener.install_class_name(IMAGE_URI, name, "Image#{class_name}")
+ IMAGE_ELEMENTS << "#{IMAGE_PREFIX}_#{name}"
+ end
+
+ module ImageModelUtils
+ def validate_one_tag_name(ignore_unknown_element, name, tags)
+ if !ignore_unknown_element
+ invalid = tags.find {|tag| tag != name}
+ raise UnknownTagError.new(invalid, IMAGE_URI) if invalid
+ end
+ raise TooMuchTagError.new(name, tag_name) if tags.size > 1
+ end
+ end
+
+ module ImageItemModel
+ include ImageModelUtils
+ extend BaseModel
+
+ def self.append_features(klass)
+ super
+
+ klass.install_have_child_element("item", IMAGE_URI, "?",
+ "#{IMAGE_PREFIX}_item")
+ klass.install_must_call_validator(IMAGE_PREFIX, IMAGE_URI)
+ end
+
+ class ImageItem < Element
+ include RSS10
+ include DublinCoreModel
+
+ @tag_name = "item"
+
+ class << self
+ def required_prefix
+ IMAGE_PREFIX
+ end
+
+ def required_uri
+ IMAGE_URI
+ end
+ end
+
+ install_must_call_validator(IMAGE_PREFIX, IMAGE_URI)
+
+ [
+ ["about", ::RSS::RDF::URI, true],
+ ["resource", ::RSS::RDF::URI, false],
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{::RSS::RDF::PREFIX}:#{name}")
+ end
+
+ %w(width height).each do |tag|
+ full_name = "#{IMAGE_PREFIX}_#{tag}"
+ disp_name = "#{IMAGE_PREFIX}:#{tag}"
+ install_text_element(tag, IMAGE_URI, "?",
+ full_name, :integer, disp_name)
+ BaseListener.install_get_text_element(IMAGE_URI, tag, full_name)
+ end
+
+ alias width= image_width=
+ alias width image_width
+ alias height= image_height=
+ alias height image_height
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.about = args[0]
+ self.resource = args[1]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(IMAGE_PREFIX)
+ end
+
+ private
+ def maker_target(target)
+ target.image_item
+ end
+
+ def setup_maker_attributes(item)
+ item.about = self.about
+ item.resource = self.resource
+ end
+ end
+ end
+
+ module ImageFaviconModel
+ include ImageModelUtils
+ extend BaseModel
+
+ def self.append_features(klass)
+ super
+
+ unless klass.class == Module
+ klass.install_have_child_element("favicon", IMAGE_URI, "?",
+ "#{IMAGE_PREFIX}_favicon")
+ klass.install_must_call_validator(IMAGE_PREFIX, IMAGE_URI)
+ end
+ end
+
+ class ImageFavicon < Element
+ include RSS10
+ include DublinCoreModel
+
+ @tag_name = "favicon"
+
+ class << self
+ def required_prefix
+ IMAGE_PREFIX
+ end
+
+ def required_uri
+ IMAGE_URI
+ end
+ end
+
+ [
+ ["about", ::RSS::RDF::URI, true, ::RSS::RDF::PREFIX],
+ ["size", IMAGE_URI, true, IMAGE_PREFIX],
+ ].each do |name, uri, required, prefix|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{prefix}:#{name}")
+ end
+
+ AVAILABLE_SIZES = %w(small medium large)
+ alias_method :set_size, :size=
+ private :set_size
+ def size=(new_value)
+ if @do_validate and !new_value.nil?
+ new_value = new_value.strip
+ unless AVAILABLE_SIZES.include?(new_value)
+ attr_name = "#{IMAGE_PREFIX}:size"
+ raise NotAvailableValueError.new(full_name, new_value, attr_name)
+ end
+ end
+ set_size(new_value)
+ end
+
+ alias image_size= size=
+ alias image_size size
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.about = args[0]
+ self.size = args[1]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(IMAGE_PREFIX)
+ end
+
+ private
+ def maker_target(target)
+ target.image_favicon
+ end
+
+ def setup_maker_attributes(favicon)
+ favicon.about = self.about
+ favicon.size = self.size
+ end
+ end
+
+ end
+
+ class RDF
+ class Channel; include ImageFaviconModel; end
+ class Item; include ImageItemModel; end
+ end
+
+end
diff --git a/ruby/lib/rss/itunes.rb b/ruby/lib/rss/itunes.rb
new file mode 100644
index 0000000..f95ca7a
--- /dev/null
+++ b/ruby/lib/rss/itunes.rb
@@ -0,0 +1,410 @@
+require 'rss/2.0'
+
+module RSS
+ ITUNES_PREFIX = 'itunes'
+ ITUNES_URI = 'http://www.itunes.com/dtds/podcast-1.0.dtd'
+
+ Rss.install_ns(ITUNES_PREFIX, ITUNES_URI)
+
+ module ITunesModelUtils
+ include Utils
+
+ def def_class_accessor(klass, name, type, *args)
+ normalized_name = name.gsub(/-/, "_")
+ full_name = "#{ITUNES_PREFIX}_#{normalized_name}"
+ klass_name = "ITunes#{Utils.to_class_name(normalized_name)}"
+
+ case type
+ when :element, :attribute
+ klass::ELEMENTS << full_name
+ def_element_class_accessor(klass, name, full_name, klass_name, *args)
+ when :elements
+ klass::ELEMENTS << full_name
+ def_elements_class_accessor(klass, name, full_name, klass_name, *args)
+ else
+ klass.install_must_call_validator(ITUNES_PREFIX, ITUNES_URI)
+ klass.install_text_element(normalized_name, ITUNES_URI, "?",
+ full_name, type, name)
+ end
+ end
+
+ def def_element_class_accessor(klass, name, full_name, klass_name,
+ recommended_attribute_name=nil)
+ klass.install_have_child_element(name, ITUNES_PREFIX, "?", full_name)
+ end
+
+ def def_elements_class_accessor(klass, name, full_name, klass_name,
+ plural_name, recommended_attribute_name=nil)
+ full_plural_name = "#{ITUNES_PREFIX}_#{plural_name}"
+ klass.install_have_children_element(name, ITUNES_PREFIX, "*",
+ full_name, full_plural_name)
+ end
+ end
+
+ module ITunesBaseModel
+ extend ITunesModelUtils
+
+ ELEMENTS = []
+
+ ELEMENT_INFOS = [["author"],
+ ["block", :yes_other],
+ ["explicit", :yes_clean_other],
+ ["keywords", :csv],
+ ["subtitle"],
+ ["summary"]]
+ end
+
+ module ITunesChannelModel
+ extend BaseModel
+ extend ITunesModelUtils
+ include ITunesBaseModel
+
+ ELEMENTS = []
+
+ class << self
+ def append_features(klass)
+ super
+
+ return if klass.instance_of?(Module)
+ ELEMENT_INFOS.each do |name, type, *additional_infos|
+ def_class_accessor(klass, name, type, *additional_infos)
+ end
+ end
+ end
+
+ ELEMENT_INFOS = [
+ ["category", :elements, "categories", "text"],
+ ["image", :attribute, "href"],
+ ["owner", :element],
+ ["new-feed-url"],
+ ] + ITunesBaseModel::ELEMENT_INFOS
+
+ class ITunesCategory < Element
+ include RSS09
+
+ @tag_name = "category"
+
+ class << self
+ def required_prefix
+ ITUNES_PREFIX
+ end
+
+ def required_uri
+ ITUNES_URI
+ end
+ end
+
+ [
+ ["text", "", true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required)
+ end
+
+ ITunesCategory = self
+ install_have_children_element("category", ITUNES_URI, "*",
+ "#{ITUNES_PREFIX}_category",
+ "#{ITUNES_PREFIX}_categories")
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.text = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(ITUNES_PREFIX)
+ end
+
+ private
+ def maker_target(categories)
+ if text or !itunes_categories.empty?
+ categories.new_category
+ else
+ nil
+ end
+ end
+
+ def setup_maker_attributes(category)
+ category.text = text if text
+ end
+
+ def setup_maker_elements(category)
+ super(category)
+ itunes_categories.each do |sub_category|
+ sub_category.setup_maker(category)
+ end
+ end
+ end
+
+ class ITunesImage < Element
+ include RSS09
+
+ @tag_name = "image"
+
+ class << self
+ def required_prefix
+ ITUNES_PREFIX
+ end
+
+ def required_uri
+ ITUNES_URI
+ end
+ end
+
+ [
+ ["href", "", true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required)
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.href = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(ITUNES_PREFIX)
+ end
+
+ private
+ def maker_target(target)
+ if href
+ target.itunes_image {|image| image}
+ else
+ nil
+ end
+ end
+
+ def setup_maker_attributes(image)
+ image.href = href
+ end
+ end
+
+ class ITunesOwner < Element
+ include RSS09
+
+ @tag_name = "owner"
+
+ class << self
+ def required_prefix
+ ITUNES_PREFIX
+ end
+
+ def required_uri
+ ITUNES_URI
+ end
+ end
+
+ install_must_call_validator(ITUNES_PREFIX, ITUNES_URI)
+ [
+ ["name"],
+ ["email"],
+ ].each do |name,|
+ ITunesBaseModel::ELEMENT_INFOS << name
+ install_text_element(name, ITUNES_URI, nil, "#{ITUNES_PREFIX}_#{name}")
+ end
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.itunes_name = args[0]
+ self.itunes_email = args[1]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(ITUNES_PREFIX)
+ end
+
+ private
+ def maker_target(target)
+ target.itunes_owner
+ end
+
+ def setup_maker_element(owner)
+ super(owner)
+ owner.itunes_name = itunes_name
+ owner.itunes_email = itunes_email
+ end
+ end
+ end
+
+ module ITunesItemModel
+ extend BaseModel
+ extend ITunesModelUtils
+ include ITunesBaseModel
+
+ class << self
+ def append_features(klass)
+ super
+
+ return if klass.instance_of?(Module)
+ ELEMENT_INFOS.each do |name, type|
+ def_class_accessor(klass, name, type)
+ end
+ end
+ end
+
+ ELEMENT_INFOS = ITunesBaseModel::ELEMENT_INFOS +
+ [["duration", :element, "content"]]
+
+ class ITunesDuration < Element
+ include RSS09
+
+ @tag_name = "duration"
+
+ class << self
+ def required_prefix
+ ITUNES_PREFIX
+ end
+
+ def required_uri
+ ITUNES_URI
+ end
+
+ def parse(duration, do_validate=true)
+ if do_validate and /\A(?:
+ \d?\d:[0-5]\d:[0-5]\d|
+ [0-5]?\d:[0-5]\d
+ )\z/x !~ duration
+ raise ArgumentError,
+ "must be one of HH:MM:SS, H:MM:SS, MM::SS, M:SS: " +
+ duration.inspect
+ end
+
+ components = duration.split(':')
+ components[3..-1] = nil if components.size > 3
+
+ components.unshift("00") until components.size == 3
+
+ components.collect do |component|
+ component.to_i
+ end
+ end
+
+ def construct(hour, minute, second)
+ components = [minute, second]
+ if components.include?(nil)
+ nil
+ else
+ components.unshift(hour) if hour and hour > 0
+ components.collect do |component|
+ "%02d" % component
+ end.join(":")
+ end
+ end
+ end
+
+ content_setup
+ alias_method(:value, :content)
+ remove_method(:content=)
+
+ attr_reader :hour, :minute, :second
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ args = args[0] if args.size == 1 and args[0].is_a?(Array)
+ if args.size == 1
+ self.content = args[0]
+ elsif args.size > 3
+ raise ArgumentError,
+ "must be (do_validate, params), (content), " +
+ "(minute, second), ([minute, second]), " +
+ "(hour, minute, second) or ([hour, minute, second]): " +
+ args.inspect
+ else
+ @second, @minute, @hour = args.reverse
+ update_content
+ end
+ end
+ end
+
+ def content=(value)
+ if value.nil?
+ @content = nil
+ elsif value.is_a?(self.class)
+ self.content = value.content
+ else
+ begin
+ @hour, @minute, @second = self.class.parse(value, @do_validate)
+ rescue ArgumentError
+ raise NotAvailableValueError.new(tag_name, value)
+ end
+ @content = value
+ end
+ end
+ alias_method(:value=, :content=)
+
+ def hour=(hour)
+ @hour = @do_validate ? Integer(hour) : hour.to_i
+ update_content
+ hour
+ end
+
+ def minute=(minute)
+ @minute = @do_validate ? Integer(minute) : minute.to_i
+ update_content
+ minute
+ end
+
+ def second=(second)
+ @second = @do_validate ? Integer(second) : second.to_i
+ update_content
+ second
+ end
+
+ def full_name
+ tag_name_with_prefix(ITUNES_PREFIX)
+ end
+
+ private
+ def update_content
+ @content = self.class.construct(hour, minute, second)
+ end
+
+ def maker_target(target)
+ if @content
+ target.itunes_duration {|duration| duration}
+ else
+ nil
+ end
+ end
+
+ def setup_maker_element(duration)
+ super(duration)
+ duration.content = @content
+ end
+ end
+ end
+
+ class Rss
+ class Channel
+ include ITunesChannelModel
+ class Item; include ITunesItemModel; end
+ end
+ end
+
+ element_infos =
+ ITunesChannelModel::ELEMENT_INFOS + ITunesItemModel::ELEMENT_INFOS
+ element_infos.each do |name, type|
+ case type
+ when :element, :elements, :attribute
+ class_name = Utils.to_class_name(name)
+ BaseListener.install_class_name(ITUNES_URI, name, "ITunes#{class_name}")
+ else
+ accessor_base = "#{ITUNES_PREFIX}_#{name.gsub(/-/, '_')}"
+ BaseListener.install_get_text_element(ITUNES_URI, name, accessor_base)
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker.rb b/ruby/lib/rss/maker.rb
new file mode 100644
index 0000000..bcba1aa
--- /dev/null
+++ b/ruby/lib/rss/maker.rb
@@ -0,0 +1,44 @@
+require "rss/rss"
+
+module RSS
+ module Maker
+ MAKERS = {}
+
+ class << self
+ def make(version, &block)
+ m = maker(version)
+ raise UnsupportedMakerVersionError.new(version) if m.nil?
+ m[:maker].make(m[:version], &block)
+ end
+
+ def maker(version)
+ MAKERS[version]
+ end
+
+ def add_maker(version, normalized_version, maker)
+ MAKERS[version] = {:maker => maker, :version => normalized_version}
+ end
+
+ def versions
+ MAKERS.keys.uniq.sort
+ end
+
+ def makers
+ MAKERS.values.collect {|info| info[:maker]}.uniq
+ end
+ end
+ end
+end
+
+require "rss/maker/1.0"
+require "rss/maker/2.0"
+require "rss/maker/feed"
+require "rss/maker/entry"
+require "rss/maker/content"
+require "rss/maker/dublincore"
+require "rss/maker/slash"
+require "rss/maker/syndication"
+require "rss/maker/taxonomy"
+require "rss/maker/trackback"
+require "rss/maker/image"
+require "rss/maker/itunes"
diff --git a/ruby/lib/rss/maker/0.9.rb b/ruby/lib/rss/maker/0.9.rb
new file mode 100644
index 0000000..72b14dc
--- /dev/null
+++ b/ruby/lib/rss/maker/0.9.rb
@@ -0,0 +1,467 @@
+require "rss/0.9"
+
+require "rss/maker/base"
+
+module RSS
+ module Maker
+
+ class RSS09 < RSSBase
+
+ def initialize(feed_version="0.92")
+ super
+ @feed_type = "rss"
+ end
+
+ private
+ def make_feed
+ Rss.new(@feed_version, @version, @encoding, @standalone)
+ end
+
+ def setup_elements(rss)
+ setup_channel(rss)
+ end
+
+ class Channel < ChannelBase
+ def to_feed(rss)
+ channel = Rss::Channel.new
+ set = setup_values(channel)
+ _not_set_required_variables = not_set_required_variables
+ if _not_set_required_variables.empty?
+ rss.channel = channel
+ set_parent(channel, rss)
+ setup_items(rss)
+ setup_image(rss)
+ setup_textinput(rss)
+ setup_other_elements(rss, channel)
+ rss
+ else
+ raise NotSetError.new("maker.channel", _not_set_required_variables)
+ end
+ end
+
+ private
+ def setup_items(rss)
+ @maker.items.to_feed(rss)
+ end
+
+ def setup_image(rss)
+ @maker.image.to_feed(rss)
+ end
+
+ def setup_textinput(rss)
+ @maker.textinput.to_feed(rss)
+ end
+
+ def variables
+ super + ["pubDate"]
+ end
+
+ def required_variable_names
+ %w(link language)
+ end
+
+ def not_set_required_variables
+ vars = super
+ vars << "description" unless description {|d| d.have_required_values?}
+ vars << "title" unless title {|t| t.have_required_values?}
+ vars
+ end
+
+ class SkipDays < SkipDaysBase
+ def to_feed(rss, channel)
+ unless @days.empty?
+ skipDays = Rss::Channel::SkipDays.new
+ channel.skipDays = skipDays
+ set_parent(skipDays, channel)
+ @days.each do |day|
+ day.to_feed(rss, skipDays.days)
+ end
+ end
+ end
+
+ class Day < DayBase
+ def to_feed(rss, days)
+ day = Rss::Channel::SkipDays::Day.new
+ set = setup_values(day)
+ if set
+ days << day
+ set_parent(day, days)
+ setup_other_elements(rss, day)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+
+ class SkipHours < SkipHoursBase
+ def to_feed(rss, channel)
+ unless @hours.empty?
+ skipHours = Rss::Channel::SkipHours.new
+ channel.skipHours = skipHours
+ set_parent(skipHours, channel)
+ @hours.each do |hour|
+ hour.to_feed(rss, skipHours.hours)
+ end
+ end
+ end
+
+ class Hour < HourBase
+ def to_feed(rss, hours)
+ hour = Rss::Channel::SkipHours::Hour.new
+ set = setup_values(hour)
+ if set
+ hours << hour
+ set_parent(hour, hours)
+ setup_other_elements(rss, hour)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+
+ class Cloud < CloudBase
+ def to_feed(*args)
+ end
+ end
+
+ class Categories < CategoriesBase
+ def to_feed(*args)
+ end
+
+ class Category < CategoryBase
+ end
+ end
+
+ class Links < LinksBase
+ def to_feed(rss, channel)
+ return if @links.empty?
+ @links.first.to_feed(rss, channel)
+ end
+
+ class Link < LinkBase
+ def to_feed(rss, channel)
+ if have_required_values?
+ channel.link = href
+ else
+ raise NotSetError.new("maker.channel.link",
+ not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(href)
+ end
+ end
+ end
+
+ class Authors < AuthorsBase
+ def to_feed(rss, channel)
+ end
+
+ class Author < AuthorBase
+ def to_feed(rss, channel)
+ end
+ end
+ end
+
+ class Contributors < ContributorsBase
+ def to_feed(rss, channel)
+ end
+
+ class Contributor < ContributorBase
+ end
+ end
+
+ class Generator < GeneratorBase
+ def to_feed(rss, channel)
+ end
+ end
+
+ class Copyright < CopyrightBase
+ def to_feed(rss, channel)
+ channel.copyright = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+
+ class Description < DescriptionBase
+ def to_feed(rss, channel)
+ channel.description = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+
+ class Title < TitleBase
+ def to_feed(rss, channel)
+ channel.title = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+
+ class Image < ImageBase
+ def to_feed(rss)
+ image = Rss::Channel::Image.new
+ set = setup_values(image)
+ if set
+ image.link = link
+ rss.channel.image = image
+ set_parent(image, rss.channel)
+ setup_other_elements(rss, image)
+ elsif required_element?
+ raise NotSetError.new("maker.image", not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(url title link)
+ end
+
+ def required_element?
+ true
+ end
+ end
+
+ class Items < ItemsBase
+ def to_feed(rss)
+ if rss.channel
+ normalize.each do |item|
+ item.to_feed(rss)
+ end
+ setup_other_elements(rss, rss.items)
+ end
+ end
+
+ class Item < ItemBase
+ def to_feed(rss)
+ item = Rss::Channel::Item.new
+ set = setup_values(item)
+ _not_set_required_variables = not_set_required_variables
+ if _not_set_required_variables.empty?
+ rss.items << item
+ set_parent(item, rss.channel)
+ setup_other_elements(rss, item)
+ elsif variable_is_set?
+ raise NotSetError.new("maker.items", _not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ []
+ end
+
+ def not_set_required_variables
+ vars = super
+ if @maker.feed_version == "0.91"
+ vars << "title" unless title {|t| t.have_required_values?}
+ vars << "link" unless link {|l| l.have_required_values?}
+ end
+ vars
+ end
+
+ class Guid < GuidBase
+ def to_feed(*args)
+ end
+ end
+
+ class Enclosure < EnclosureBase
+ def to_feed(*args)
+ end
+ end
+
+ class Source < SourceBase
+ def to_feed(*args)
+ end
+
+ class Authors < AuthorsBase
+ def to_feed(*args)
+ end
+
+ class Author < AuthorBase
+ end
+ end
+
+ class Categories < CategoriesBase
+ def to_feed(*args)
+ end
+
+ class Category < CategoryBase
+ end
+ end
+
+ class Contributors < ContributorsBase
+ def to_feed(*args)
+ end
+
+ class Contributor < ContributorBase
+ end
+ end
+
+ class Generator < GeneratorBase
+ def to_feed(*args)
+ end
+ end
+
+ class Icon < IconBase
+ def to_feed(*args)
+ end
+ end
+
+ class Links < LinksBase
+ def to_feed(*args)
+ end
+
+ class Link < LinkBase
+ end
+ end
+
+ class Logo < LogoBase
+ def to_feed(*args)
+ end
+ end
+
+ class Rights < RightsBase
+ def to_feed(*args)
+ end
+ end
+
+ class Subtitle < SubtitleBase
+ def to_feed(*args)
+ end
+ end
+
+ class Title < TitleBase
+ def to_feed(*args)
+ end
+ end
+ end
+
+ class Categories < CategoriesBase
+ def to_feed(*args)
+ end
+
+ class Category < CategoryBase
+ end
+ end
+
+ class Authors < AuthorsBase
+ def to_feed(*args)
+ end
+
+ class Author < AuthorBase
+ end
+ end
+
+ class Links < LinksBase
+ def to_feed(rss, item)
+ return if @links.empty?
+ @links.first.to_feed(rss, item)
+ end
+
+ class Link < LinkBase
+ def to_feed(rss, item)
+ if have_required_values?
+ item.link = href
+ else
+ raise NotSetError.new("maker.link",
+ not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(href)
+ end
+ end
+ end
+
+ class Contributors < ContributorsBase
+ def to_feed(rss, item)
+ end
+
+ class Contributor < ContributorBase
+ end
+ end
+
+ class Rights < RightsBase
+ def to_feed(rss, item)
+ end
+ end
+
+ class Description < DescriptionBase
+ def to_feed(rss, item)
+ item.description = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+
+ class Content < ContentBase
+ def to_feed(rss, item)
+ end
+ end
+
+ class Title < TitleBase
+ def to_feed(rss, item)
+ item.title = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+ end
+
+ class Textinput < TextinputBase
+ def to_feed(rss)
+ textInput = Rss::Channel::TextInput.new
+ set = setup_values(textInput)
+ if set
+ rss.channel.textInput = textInput
+ set_parent(textInput, rss.channel)
+ setup_other_elements(rss, textInput)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(title description name link)
+ end
+ end
+ end
+
+ add_maker("0.9", "0.92", RSS09)
+ add_maker("0.91", "0.91", RSS09)
+ add_maker("0.92", "0.92", RSS09)
+ add_maker("rss0.91", "0.91", RSS09)
+ add_maker("rss0.92", "0.92", RSS09)
+ end
+end
diff --git a/ruby/lib/rss/maker/1.0.rb b/ruby/lib/rss/maker/1.0.rb
new file mode 100644
index 0000000..a1e2594
--- /dev/null
+++ b/ruby/lib/rss/maker/1.0.rb
@@ -0,0 +1,434 @@
+require "rss/1.0"
+
+require "rss/maker/base"
+
+module RSS
+ module Maker
+
+ class RSS10 < RSSBase
+
+ def initialize(feed_version="1.0")
+ super
+ @feed_type = "rss"
+ end
+
+ private
+ def make_feed
+ RDF.new(@version, @encoding, @standalone)
+ end
+
+ def setup_elements(rss)
+ setup_channel(rss)
+ setup_image(rss)
+ setup_items(rss)
+ setup_textinput(rss)
+ end
+
+ class Channel < ChannelBase
+
+ def to_feed(rss)
+ set_default_values do
+ _not_set_required_variables = not_set_required_variables
+ if _not_set_required_variables.empty?
+ channel = RDF::Channel.new(@about)
+ set = setup_values(channel)
+ channel.dc_dates.clear
+ rss.channel = channel
+ set_parent(channel, rss)
+ setup_items(rss)
+ setup_image(rss)
+ setup_textinput(rss)
+ setup_other_elements(rss, channel)
+ else
+ raise NotSetError.new("maker.channel", _not_set_required_variables)
+ end
+ end
+ end
+
+ private
+ def setup_items(rss)
+ items = RDF::Channel::Items.new
+ seq = items.Seq
+ set_parent(items, seq)
+ target_items = @maker.items.normalize
+ raise NotSetError.new("maker", ["items"]) if target_items.empty?
+ target_items.each do |item|
+ li = RDF::Channel::Items::Seq::Li.new(item.link)
+ seq.lis << li
+ set_parent(li, seq)
+ end
+ rss.channel.items = items
+ set_parent(rss.channel, items)
+ end
+
+ def setup_image(rss)
+ if @maker.image.have_required_values?
+ image = RDF::Channel::Image.new(@maker.image.url)
+ rss.channel.image = image
+ set_parent(image, rss.channel)
+ end
+ end
+
+ def setup_textinput(rss)
+ if @maker.textinput.have_required_values?
+ textinput = RDF::Channel::Textinput.new(@maker.textinput.link)
+ rss.channel.textinput = textinput
+ set_parent(textinput, rss.channel)
+ end
+ end
+
+ def required_variable_names
+ %w(about link)
+ end
+
+ def not_set_required_variables
+ vars = super
+ vars << "description" unless description {|d| d.have_required_values?}
+ vars << "title" unless title {|t| t.have_required_values?}
+ vars
+ end
+
+ class SkipDays < SkipDaysBase
+ def to_feed(*args)
+ end
+
+ class Day < DayBase
+ end
+ end
+
+ class SkipHours < SkipHoursBase
+ def to_feed(*args)
+ end
+
+ class Hour < HourBase
+ end
+ end
+
+ class Cloud < CloudBase
+ def to_feed(*args)
+ end
+ end
+
+ class Categories < CategoriesBase
+ def to_feed(*args)
+ end
+
+ class Category < CategoryBase
+ end
+ end
+
+ class Links < LinksBase
+ def to_feed(rss, channel)
+ return if @links.empty?
+ @links.first.to_feed(rss, channel)
+ end
+
+ class Link < LinkBase
+ def to_feed(rss, channel)
+ if have_required_values?
+ channel.link = href
+ else
+ raise NotSetError.new("maker.channel.link",
+ not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(href)
+ end
+ end
+ end
+
+ class Authors < AuthorsBase
+ def to_feed(rss, channel)
+ end
+
+ class Author < AuthorBase
+ def to_feed(rss, channel)
+ end
+ end
+ end
+
+ class Contributors < ContributorsBase
+ def to_feed(rss, channel)
+ end
+
+ class Contributor < ContributorBase
+ end
+ end
+
+ class Generator < GeneratorBase
+ def to_feed(rss, channel)
+ end
+ end
+
+ class Copyright < CopyrightBase
+ def to_feed(rss, channel)
+ end
+ end
+
+ class Description < DescriptionBase
+ def to_feed(rss, channel)
+ channel.description = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+
+ class Title < TitleBase
+ def to_feed(rss, channel)
+ channel.title = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+
+ class Image < ImageBase
+ def to_feed(rss)
+ if @url
+ image = RDF::Image.new(@url)
+ set = setup_values(image)
+ if set
+ rss.image = image
+ set_parent(image, rss)
+ setup_other_elements(rss, image)
+ end
+ end
+ end
+
+ def have_required_values?
+ super and @maker.channel.have_required_values?
+ end
+
+ private
+ def variables
+ super + ["link"]
+ end
+
+ def required_variable_names
+ %w(url title link)
+ end
+ end
+
+ class Items < ItemsBase
+ def to_feed(rss)
+ if rss.channel
+ normalize.each do |item|
+ item.to_feed(rss)
+ end
+ setup_other_elements(rss, rss.items)
+ end
+ end
+
+ class Item < ItemBase
+ def to_feed(rss)
+ set_default_values do
+ item = RDF::Item.new(link)
+ set = setup_values(item)
+ if set
+ item.dc_dates.clear
+ rss.items << item
+ set_parent(item, rss)
+ setup_other_elements(rss, item)
+ elsif !have_required_values?
+ raise NotSetError.new("maker.item", not_set_required_variables)
+ end
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(link)
+ end
+
+ def variables
+ super + %w(link)
+ end
+
+ def not_set_required_variables
+ set_default_values do
+ vars = super
+ vars << "title" unless title {|t| t.have_required_values?}
+ vars
+ end
+ end
+
+ class Guid < GuidBase
+ def to_feed(*args)
+ end
+ end
+
+ class Enclosure < EnclosureBase
+ def to_feed(*args)
+ end
+ end
+
+ class Source < SourceBase
+ def to_feed(*args)
+ end
+
+ class Authors < AuthorsBase
+ def to_feed(*args)
+ end
+
+ class Author < AuthorBase
+ end
+ end
+
+ class Categories < CategoriesBase
+ def to_feed(*args)
+ end
+
+ class Category < CategoryBase
+ end
+ end
+
+ class Contributors < ContributorsBase
+ def to_feed(*args)
+ end
+
+ class Contributor < ContributorBase
+ end
+ end
+
+ class Generator < GeneratorBase
+ def to_feed(*args)
+ end
+ end
+
+ class Icon < IconBase
+ def to_feed(*args)
+ end
+ end
+
+ class Links < LinksBase
+ def to_feed(*args)
+ end
+
+ class Link < LinkBase
+ end
+ end
+
+ class Logo < LogoBase
+ def to_feed(*args)
+ end
+ end
+
+ class Rights < RightsBase
+ def to_feed(*args)
+ end
+ end
+
+ class Subtitle < SubtitleBase
+ def to_feed(*args)
+ end
+ end
+
+ class Title < TitleBase
+ def to_feed(*args)
+ end
+ end
+ end
+
+ class Categories < CategoriesBase
+ def to_feed(*args)
+ end
+
+ class Category < CategoryBase
+ end
+ end
+
+ class Authors < AuthorsBase
+ def to_feed(*args)
+ end
+
+ class Author < AuthorBase
+ end
+ end
+
+ class Links < LinksBase
+ def to_feed(*args)
+ end
+
+ class Link < LinkBase
+ end
+ end
+
+ class Contributors < ContributorsBase
+ def to_feed(rss, item)
+ end
+
+ class Contributor < ContributorBase
+ end
+ end
+
+ class Rights < RightsBase
+ def to_feed(rss, item)
+ end
+ end
+
+ class Description < DescriptionBase
+ def to_feed(rss, item)
+ item.description = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+
+ class Content < ContentBase
+ def to_feed(rss, item)
+ end
+ end
+
+ class Title < TitleBase
+ def to_feed(rss, item)
+ item.title = content if have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+ end
+
+ class Textinput < TextinputBase
+ def to_feed(rss)
+ if @link
+ textinput = RDF::Textinput.new(@link)
+ set = setup_values(textinput)
+ if set
+ rss.textinput = textinput
+ set_parent(textinput, rss)
+ setup_other_elements(rss, textinput)
+ end
+ end
+ end
+
+ def have_required_values?
+ super and @maker.channel.have_required_values?
+ end
+
+ private
+ def required_variable_names
+ %w(title description name link)
+ end
+ end
+ end
+
+ add_maker("1.0", "1.0", RSS10)
+ add_maker("rss1.0", "1.0", RSS10)
+ end
+end
diff --git a/ruby/lib/rss/maker/2.0.rb b/ruby/lib/rss/maker/2.0.rb
new file mode 100644
index 0000000..67d6812
--- /dev/null
+++ b/ruby/lib/rss/maker/2.0.rb
@@ -0,0 +1,223 @@
+require "rss/2.0"
+
+require "rss/maker/0.9"
+
+module RSS
+ module Maker
+
+ class RSS20 < RSS09
+
+ def initialize(feed_version="2.0")
+ super
+ end
+
+ class Channel < RSS09::Channel
+
+ private
+ def required_variable_names
+ %w(link)
+ end
+
+ class SkipDays < RSS09::Channel::SkipDays
+ class Day < RSS09::Channel::SkipDays::Day
+ end
+ end
+
+ class SkipHours < RSS09::Channel::SkipHours
+ class Hour < RSS09::Channel::SkipHours::Hour
+ end
+ end
+
+ class Cloud < RSS09::Channel::Cloud
+ def to_feed(rss, channel)
+ cloud = Rss::Channel::Cloud.new
+ set = setup_values(cloud)
+ if set
+ channel.cloud = cloud
+ set_parent(cloud, channel)
+ setup_other_elements(rss, cloud)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(domain port path registerProcedure protocol)
+ end
+ end
+
+ class Categories < RSS09::Channel::Categories
+ def to_feed(rss, channel)
+ @categories.each do |category|
+ category.to_feed(rss, channel)
+ end
+ end
+
+ class Category < RSS09::Channel::Categories::Category
+ def to_feed(rss, channel)
+ category = Rss::Channel::Category.new
+ set = setup_values(category)
+ if set
+ channel.categories << category
+ set_parent(category, channel)
+ setup_other_elements(rss, category)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+
+ class Generator < GeneratorBase
+ def to_feed(rss, channel)
+ channel.generator = content
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+
+ class Image < RSS09::Image
+ private
+ def required_element?
+ false
+ end
+ end
+
+ class Items < RSS09::Items
+ class Item < RSS09::Items::Item
+ private
+ def required_variable_names
+ []
+ end
+
+ def not_set_required_variables
+ vars = super
+ if !title {|t| t.have_required_values?} and
+ !description {|d| d.have_required_values?}
+ vars << "title or description"
+ end
+ vars
+ end
+
+ def variables
+ super + ["pubDate"]
+ end
+
+ class Guid < RSS09::Items::Item::Guid
+ def to_feed(rss, item)
+ guid = Rss::Channel::Item::Guid.new
+ set = setup_values(guid)
+ if set
+ item.guid = guid
+ set_parent(guid, item)
+ setup_other_elements(rss, guid)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+
+ class Enclosure < RSS09::Items::Item::Enclosure
+ def to_feed(rss, item)
+ enclosure = Rss::Channel::Item::Enclosure.new
+ set = setup_values(enclosure)
+ if set
+ item.enclosure = enclosure
+ set_parent(enclosure, item)
+ setup_other_elements(rss, enclosure)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(url length type)
+ end
+ end
+
+ class Source < RSS09::Items::Item::Source
+ def to_feed(rss, item)
+ source = Rss::Channel::Item::Source.new
+ set = setup_values(source)
+ if set
+ item.source = source
+ set_parent(source, item)
+ setup_other_elements(rss, source)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(url content)
+ end
+
+ class Links < RSS09::Items::Item::Source::Links
+ def to_feed(rss, source)
+ return if @links.empty?
+ @links.first.to_feed(rss, source)
+ end
+
+ class Link < RSS09::Items::Item::Source::Links::Link
+ def to_feed(rss, source)
+ source.url = href
+ end
+ end
+ end
+ end
+
+ class Categories < RSS09::Items::Item::Categories
+ def to_feed(rss, item)
+ @categories.each do |category|
+ category.to_feed(rss, item)
+ end
+ end
+
+ class Category < RSS09::Items::Item::Categories::Category
+ def to_feed(rss, item)
+ category = Rss::Channel::Item::Category.new
+ set = setup_values(category)
+ if set
+ item.categories << category
+ set_parent(category, item)
+ setup_other_elements(rss)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+ end
+
+ class Authors < RSS09::Items::Item::Authors
+ def to_feed(rss, item)
+ return if @authors.empty?
+ @authors.first.to_feed(rss, item)
+ end
+
+ class Author < RSS09::Items::Item::Authors::Author
+ def to_feed(rss, item)
+ item.author = name
+ end
+ end
+ end
+ end
+ end
+
+ class Textinput < RSS09::Textinput
+ end
+ end
+
+ add_maker("2.0", "2.0", RSS20)
+ add_maker("rss2.0", "2.0", RSS20)
+ end
+end
diff --git a/ruby/lib/rss/maker/atom.rb b/ruby/lib/rss/maker/atom.rb
new file mode 100644
index 0000000..fd3198c
--- /dev/null
+++ b/ruby/lib/rss/maker/atom.rb
@@ -0,0 +1,172 @@
+require "rss/atom"
+
+require "rss/maker/base"
+
+module RSS
+ module Maker
+ module AtomPersons
+ module_function
+ def def_atom_persons(klass, name, maker_name, plural=nil)
+ plural ||= "#{name}s"
+ klass_name = Utils.to_class_name(name)
+ plural_klass_name = Utils.to_class_name(plural)
+
+ klass.class_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class #{plural_klass_name} < #{plural_klass_name}Base
+ class #{klass_name} < #{klass_name}Base
+ def to_feed(feed, current)
+ #{name} = feed.class::#{klass_name}.new
+ set = setup_values(#{name})
+ unless set
+ raise NotSetError.new(#{maker_name.dump},
+ not_set_required_variables)
+ end
+ current.#{plural} << #{name}
+ set_parent(#{name}, current)
+ setup_other_elements(#{name})
+ end
+
+ private
+ def required_variable_names
+ %w(name)
+ end
+ end
+ end
+EOC
+ end
+ end
+
+ module AtomTextConstruct
+ class << self
+ def def_atom_text_construct(klass, name, maker_name, klass_name=nil,
+ atom_klass_name=nil)
+ klass_name ||= Utils.to_class_name(name)
+ atom_klass_name ||= Utils.to_class_name(name)
+
+ klass.class_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class #{klass_name} < #{klass_name}Base
+ include #{self.name}
+ def to_feed(feed, current)
+ #{name} = current.class::#{atom_klass_name}.new
+ if setup_values(#{name})
+ current.#{name} = #{name}
+ set_parent(#{name}, current)
+ setup_other_elements(feed)
+ elsif variable_is_set?
+ raise NotSetError.new(#{maker_name.dump},
+ not_set_required_variables)
+ end
+ end
+ end
+ EOC
+ end
+ end
+
+ private
+ def required_variable_names
+ if type == "xhtml"
+ %w(xml_content)
+ else
+ %w(content)
+ end
+ end
+
+ def variables
+ if type == "xhtml"
+ super + %w(xhtml)
+ else
+ super
+ end
+ end
+ end
+
+ module AtomCategory
+ def to_feed(feed, current)
+ category = feed.class::Category.new
+ set = setup_values(category)
+ if set
+ current.categories << category
+ set_parent(category, current)
+ setup_other_elements(feed)
+ else
+ raise NotSetError.new(self.class.not_set_name,
+ not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(term)
+ end
+
+ def variables
+ super + ["term", "scheme"]
+ end
+ end
+
+ module AtomLink
+ def to_feed(feed, current)
+ link = feed.class::Link.new
+ set = setup_values(link)
+ if set
+ current.links << link
+ set_parent(link, current)
+ setup_other_elements(feed)
+ else
+ raise NotSetError.new(self.class.not_set_name,
+ not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(href)
+ end
+ end
+
+ module AtomGenerator
+ def to_feed(feed, current)
+ generator = current.class::Generator.new
+ if setup_values(generator)
+ current.generator = generator
+ set_parent(generator, current)
+ setup_other_elements(feed)
+ elsif variable_is_set?
+ raise NotSetError.new(self.class.not_set_name,
+ not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(content)
+ end
+ end
+
+ module AtomLogo
+ def to_feed(feed, current)
+ logo = current.class::Logo.new
+ class << logo
+ alias_method(:uri=, :content=)
+ end
+ set = setup_values(logo)
+ class << logo
+ remove_method(:uri=)
+ end
+ if set
+ current.logo = logo
+ set_parent(logo, current)
+ setup_other_elements(feed)
+ elsif variable_is_set?
+ raise NotSetError.new(self.class.not_set_name,
+ not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(uri)
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker/base.rb b/ruby/lib/rss/maker/base.rb
new file mode 100644
index 0000000..2262a76
--- /dev/null
+++ b/ruby/lib/rss/maker/base.rb
@@ -0,0 +1,880 @@
+require 'forwardable'
+
+require 'rss/rss'
+
+module RSS
+ module Maker
+ class Base
+ extend Utils::InheritedReader
+
+ OTHER_ELEMENTS = []
+ NEED_INITIALIZE_VARIABLES = []
+
+ class << self
+ def other_elements
+ inherited_array_reader("OTHER_ELEMENTS")
+ end
+ def need_initialize_variables
+ inherited_array_reader("NEED_INITIALIZE_VARIABLES")
+ end
+
+ def inherited_base
+ ::RSS::Maker::Base
+ end
+
+ def inherited(subclass)
+ subclass.const_set("OTHER_ELEMENTS", [])
+ subclass.const_set("NEED_INITIALIZE_VARIABLES", [])
+ end
+
+ def add_other_element(variable_name)
+ self::OTHER_ELEMENTS << variable_name
+ end
+
+ def add_need_initialize_variable(variable_name, init_value=nil,
+ &init_block)
+ init_value ||= init_block
+ self::NEED_INITIALIZE_VARIABLES << [variable_name, init_value]
+ end
+
+ def def_array_element(name, plural=nil, klass_name=nil)
+ include Enumerable
+ extend Forwardable
+
+ plural ||= "#{name}s"
+ klass_name ||= Utils.to_class_name(name)
+ def_delegators("@#{plural}", :<<, :[], :[]=, :first, :last)
+ def_delegators("@#{plural}", :push, :pop, :shift, :unshift)
+ def_delegators("@#{plural}", :each, :size, :empty?, :clear)
+
+ add_need_initialize_variable(plural) {[]}
+
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def new_#{name}
+ #{name} = self.class::#{klass_name}.new(@maker)
+ @#{plural} << #{name}
+ if block_given?
+ yield #{name}
+ else
+ #{name}
+ end
+ end
+ alias new_child new_#{name}
+
+ def to_feed(*args)
+ @#{plural}.each do |#{name}|
+ #{name}.to_feed(*args)
+ end
+ end
+
+ def replace(elements)
+ @#{plural}.replace(elements.to_a)
+ end
+ EOC
+ end
+
+ def def_classed_element_without_accessor(name, class_name=nil)
+ class_name ||= Utils.to_class_name(name)
+ add_other_element(name)
+ add_need_initialize_variable(name) do |object|
+ object.send("make_#{name}")
+ end
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ private
+ def setup_#{name}(feed, current)
+ @#{name}.to_feed(feed, current)
+ end
+
+ def make_#{name}
+ self.class::#{class_name}.new(@maker)
+ end
+ EOC
+ end
+
+ def def_classed_element(name, class_name=nil, attribute_name=nil)
+ def_classed_element_without_accessor(name, class_name)
+ if attribute_name
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{name}
+ if block_given?
+ yield(@#{name})
+ else
+ @#{name}.#{attribute_name}
+ end
+ end
+
+ def #{name}=(new_value)
+ @#{name}.#{attribute_name} = new_value
+ end
+ EOC
+ else
+ attr_reader name
+ end
+ end
+
+ def def_classed_elements(name, attribute, plural_class_name=nil,
+ plural_name=nil, new_name=nil)
+ plural_name ||= "#{name}s"
+ new_name ||= name
+ def_classed_element(plural_name, plural_class_name)
+ local_variable_name = "_#{name}"
+ new_value_variable_name = "new_value"
+ additional_setup_code = nil
+ if block_given?
+ additional_setup_code = yield(local_variable_name,
+ new_value_variable_name)
+ end
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{name}
+ #{local_variable_name} = #{plural_name}.first
+ #{local_variable_name} ? #{local_variable_name}.#{attribute} : nil
+ end
+
+ def #{name}=(#{new_value_variable_name})
+ #{local_variable_name} =
+ #{plural_name}.first || #{plural_name}.new_#{new_name}
+ #{additional_setup_code}
+ #{local_variable_name}.#{attribute} = #{new_value_variable_name}
+ end
+ EOC
+ end
+
+ def def_other_element(name)
+ attr_accessor name
+ def_other_element_without_accessor(name)
+ end
+
+ def def_other_element_without_accessor(name)
+ add_need_initialize_variable(name)
+ add_other_element(name)
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def setup_#{name}(feed, current)
+ if !@#{name}.nil? and current.respond_to?(:#{name}=)
+ current.#{name} = @#{name}
+ end
+ end
+ EOC
+ end
+
+ def def_csv_element(name, type=nil)
+ def_other_element_without_accessor(name)
+ attr_reader(name)
+ converter = ""
+ if type == :integer
+ converter = "{|v| Integer(v)}"
+ end
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{name}=(value)
+ @#{name} = Utils::CSV.parse(value)#{converter}
+ end
+ EOC
+ end
+ end
+
+ attr_reader :maker
+ def initialize(maker)
+ @maker = maker
+ @default_values_are_set = false
+ initialize_variables
+ end
+
+ def have_required_values?
+ not_set_required_variables.empty?
+ end
+
+ def variable_is_set?
+ variables.any? {|var| not __send__(var).nil?}
+ end
+
+ private
+ def initialize_variables
+ self.class.need_initialize_variables.each do |variable_name, init_value|
+ if init_value.nil?
+ value = nil
+ else
+ if init_value.respond_to?(:call)
+ value = init_value.call(self)
+ elsif init_value.is_a?(String)
+ # just for backward compatibility
+ value = instance_eval(init_value, __FILE__, __LINE__)
+ else
+ value = init_value
+ end
+ end
+ instance_variable_set("@#{variable_name}", value)
+ end
+ end
+
+ def setup_other_elements(feed, current=nil)
+ current ||= current_element(feed)
+ self.class.other_elements.each do |element|
+ __send__("setup_#{element}", feed, current)
+ end
+ end
+
+ def current_element(feed)
+ feed
+ end
+
+ def set_default_values(&block)
+ return yield if @default_values_are_set
+
+ begin
+ @default_values_are_set = true
+ _set_default_values(&block)
+ ensure
+ @default_values_are_set = false
+ end
+ end
+
+ def _set_default_values(&block)
+ yield
+ end
+
+ def setup_values(target)
+ set = false
+ if have_required_values?
+ variables.each do |var|
+ setter = "#{var}="
+ if target.respond_to?(setter)
+ value = __send__(var)
+ if value
+ target.__send__(setter, value)
+ set = true
+ end
+ end
+ end
+ end
+ set
+ end
+
+ def set_parent(target, parent)
+ target.parent = parent if target.class.need_parent?
+ end
+
+ def variables
+ self.class.need_initialize_variables.find_all do |name, init|
+ # init == "nil" is just for backward compatibility
+ init.nil? or init == "nil"
+ end.collect do |name, init|
+ name
+ end
+ end
+
+ def not_set_required_variables
+ required_variable_names.find_all do |var|
+ __send__(var).nil?
+ end
+ end
+
+ def required_variables_are_set?
+ required_variable_names.each do |var|
+ return false if __send__(var).nil?
+ end
+ true
+ end
+ end
+
+ module AtomPersonConstructBase
+ def self.append_features(klass)
+ super
+
+ klass.class_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ %w(name uri email).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ EOC
+ end
+ end
+
+ module AtomTextConstructBase
+ module EnsureXMLContent
+ class << self
+ def included(base)
+ super
+ base.class_eval do
+ %w(type content xml_content).each do |element|
+ attr_reader element
+ attr_writer element if element != "xml_content"
+ add_need_initialize_variable(element)
+ end
+
+ alias_method(:xhtml, :xml_content)
+ end
+ end
+ end
+
+ def ensure_xml_content(content)
+ xhtml_uri = ::RSS::Atom::XHTML_URI
+ unless content.is_a?(RSS::XML::Element) and
+ ["div", xhtml_uri] == [content.name, content.uri]
+ children = content
+ children = [children] unless content.is_a?(Array)
+ children = set_xhtml_uri_as_default_uri(children)
+ content = RSS::XML::Element.new("div", nil, xhtml_uri,
+ {"xmlns" => xhtml_uri},
+ children)
+ end
+ content
+ end
+
+ def xml_content=(content)
+ @xml_content = ensure_xml_content(content)
+ end
+
+ def xhtml=(content)
+ self.xml_content = content
+ end
+
+ private
+ def set_xhtml_uri_as_default_uri(children)
+ children.collect do |child|
+ if child.is_a?(RSS::XML::Element) and
+ child.prefix.nil? and child.uri.nil?
+ RSS::XML::Element.new(child.name, nil, ::RSS::Atom::XHTML_URI,
+ child.attributes.dup,
+ set_xhtml_uri_as_default_uri(child.children))
+ else
+ child
+ end
+ end
+ end
+ end
+
+ def self.append_features(klass)
+ super
+
+ klass.class_eval do
+ include EnsureXMLContent
+ end
+ end
+ end
+
+ module SetupDefaultDate
+ private
+ def _set_default_values(&block)
+ keep = {
+ :date => date,
+ :dc_dates => dc_dates.to_a.dup,
+ }
+ _date = date
+ if _date and !dc_dates.any? {|dc_date| dc_date.value == _date}
+ dc_date = self.class::DublinCoreDates::DublinCoreDate.new(self)
+ dc_date.value = _date.dup
+ dc_dates.unshift(dc_date)
+ end
+ self.date ||= self.dc_date
+ super(&block)
+ ensure
+ date = keep[:date]
+ dc_dates.replace(keep[:dc_dates])
+ end
+ end
+
+ class RSSBase < Base
+ class << self
+ def make(version, &block)
+ new(version).make(&block)
+ end
+ end
+
+ %w(xml_stylesheets channel image items textinput).each do |element|
+ attr_reader element
+ add_need_initialize_variable(element) do |object|
+ object.send("make_#{element}")
+ end
+ module_eval(<<-EOC, __FILE__, __LINE__)
+ private
+ def setup_#{element}(feed)
+ @#{element}.to_feed(feed)
+ end
+
+ def make_#{element}
+ self.class::#{Utils.to_class_name(element)}.new(self)
+ end
+ EOC
+ end
+
+ attr_reader :feed_version
+ alias_method(:rss_version, :feed_version)
+ attr_accessor :version, :encoding, :standalone
+
+ def initialize(feed_version)
+ super(self)
+ @feed_type = nil
+ @feed_subtype = nil
+ @feed_version = feed_version
+ @version = "1.0"
+ @encoding = "UTF-8"
+ @standalone = nil
+ end
+
+ def make
+ yield(self)
+ to_feed
+ end
+
+ def to_feed
+ feed = make_feed
+ setup_xml_stylesheets(feed)
+ setup_elements(feed)
+ setup_other_elements(feed)
+ feed.validate
+ feed
+ end
+
+ private
+ remove_method :make_xml_stylesheets
+ def make_xml_stylesheets
+ XMLStyleSheets.new(self)
+ end
+ end
+
+ class XMLStyleSheets < Base
+ def_array_element("xml_stylesheet", nil, "XMLStyleSheet")
+
+ class XMLStyleSheet < Base
+
+ ::RSS::XMLStyleSheet::ATTRIBUTES.each do |attribute|
+ attr_accessor attribute
+ add_need_initialize_variable(attribute)
+ end
+
+ def to_feed(feed)
+ xss = ::RSS::XMLStyleSheet.new
+ guess_type_if_need(xss)
+ set = setup_values(xss)
+ if set
+ feed.xml_stylesheets << xss
+ end
+ end
+
+ private
+ def guess_type_if_need(xss)
+ if @type.nil?
+ xss.href = @href
+ @type = xss.type
+ end
+ end
+
+ def required_variable_names
+ %w(href type)
+ end
+ end
+ end
+
+ class ChannelBase < Base
+ include SetupDefaultDate
+
+ %w(cloud categories skipDays skipHours).each do |name|
+ def_classed_element(name)
+ end
+
+ %w(generator copyright description title).each do |name|
+ def_classed_element(name, nil, "content")
+ end
+
+ [
+ ["link", "href", Proc.new {|target,| "#{target}.href = 'self'"}],
+ ["author", "name"],
+ ["contributor", "name"],
+ ].each do |name, attribute, additional_setup_maker|
+ def_classed_elements(name, attribute, &additional_setup_maker)
+ end
+
+ %w(id about language
+ managingEditor webMaster rating docs date
+ lastBuildDate ttl).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+
+ def pubDate
+ date
+ end
+
+ def pubDate=(date)
+ self.date = date
+ end
+
+ def updated
+ date
+ end
+
+ def updated=(date)
+ self.date = date
+ end
+
+ alias_method(:rights, :copyright)
+ alias_method(:rights=, :copyright=)
+
+ alias_method(:subtitle, :description)
+ alias_method(:subtitle=, :description=)
+
+ def icon
+ image_favicon.about
+ end
+
+ def icon=(url)
+ image_favicon.about = url
+ end
+
+ def logo
+ maker.image.url
+ end
+
+ def logo=(url)
+ maker.image.url = url
+ end
+
+ class SkipDaysBase < Base
+ def_array_element("day")
+
+ class DayBase < Base
+ %w(content).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+ end
+
+ class SkipHoursBase < Base
+ def_array_element("hour")
+
+ class HourBase < Base
+ %w(content).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+ end
+
+ class CloudBase < Base
+ %w(domain port path registerProcedure protocol).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+
+ class CategoriesBase < Base
+ def_array_element("category", "categories")
+
+ class CategoryBase < Base
+ %w(domain content label).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+
+ alias_method(:term, :domain)
+ alias_method(:term=, :domain=)
+ alias_method(:scheme, :content)
+ alias_method(:scheme=, :content=)
+ end
+ end
+
+ class LinksBase < Base
+ def_array_element("link")
+
+ class LinkBase < Base
+ %w(href rel type hreflang title length).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+ end
+
+ class AuthorsBase < Base
+ def_array_element("author")
+
+ class AuthorBase < Base
+ include AtomPersonConstructBase
+ end
+ end
+
+ class ContributorsBase < Base
+ def_array_element("contributor")
+
+ class ContributorBase < Base
+ include AtomPersonConstructBase
+ end
+ end
+
+ class GeneratorBase < Base
+ %w(uri version content).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+
+ class CopyrightBase < Base
+ include AtomTextConstructBase
+ end
+
+ class DescriptionBase < Base
+ include AtomTextConstructBase
+ end
+
+ class TitleBase < Base
+ include AtomTextConstructBase
+ end
+ end
+
+ class ImageBase < Base
+ %w(title url width height description).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+
+ def link
+ @maker.channel.link
+ end
+ end
+
+ class ItemsBase < Base
+ def_array_element("item")
+
+ attr_accessor :do_sort, :max_size
+
+ def initialize(maker)
+ super
+ @do_sort = false
+ @max_size = -1
+ end
+
+ def normalize
+ if @max_size >= 0
+ sort_if_need[0...@max_size]
+ else
+ sort_if_need[0..@max_size]
+ end
+ end
+
+ private
+ def sort_if_need
+ if @do_sort.respond_to?(:call)
+ @items.sort do |x, y|
+ @do_sort.call(x, y)
+ end
+ elsif @do_sort
+ @items.sort do |x, y|
+ y <=> x
+ end
+ else
+ @items
+ end
+ end
+
+ class ItemBase < Base
+ include SetupDefaultDate
+
+ %w(guid enclosure source categories content).each do |name|
+ def_classed_element(name)
+ end
+
+ %w(rights description title).each do |name|
+ def_classed_element(name, nil, "content")
+ end
+
+ [
+ ["author", "name"],
+ ["link", "href", Proc.new {|target,| "#{target}.href = 'alternate'"}],
+ ["contributor", "name"],
+ ].each do |name, attribute|
+ def_classed_elements(name, attribute)
+ end
+
+ %w(date comments id published).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+
+ def pubDate
+ date
+ end
+
+ def pubDate=(date)
+ self.date = date
+ end
+
+ def updated
+ date
+ end
+
+ def updated=(date)
+ self.date = date
+ end
+
+ alias_method(:summary, :description)
+ alias_method(:summary=, :description=)
+
+ def <=>(other)
+ _date = date || dc_date
+ _other_date = other.date || other.dc_date
+ if _date and _other_date
+ _date <=> _other_date
+ elsif _date
+ 1
+ elsif _other_date
+ -1
+ else
+ 0
+ end
+ end
+
+ class GuidBase < Base
+ %w(isPermaLink content).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+
+ class EnclosureBase < Base
+ %w(url length type).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+
+ class SourceBase < Base
+ %w(authors categories contributors generator icon
+ logo rights subtitle title).each do |name|
+ def_classed_element(name)
+ end
+
+ [
+ ["link", "href"],
+ ].each do |name, attribute|
+ def_classed_elements(name, attribute)
+ end
+
+ %w(id content date).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+
+ alias_method(:url, :link)
+ alias_method(:url=, :link=)
+
+ def updated
+ date
+ end
+
+ def updated=(date)
+ self.date = date
+ end
+
+ private
+ AuthorsBase = ChannelBase::AuthorsBase
+ CategoriesBase = ChannelBase::CategoriesBase
+ ContributorsBase = ChannelBase::ContributorsBase
+ GeneratorBase = ChannelBase::GeneratorBase
+
+ class IconBase < Base
+ %w(url).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+
+ LinksBase = ChannelBase::LinksBase
+
+ class LogoBase < Base
+ %w(uri).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+
+ class RightsBase < Base
+ include AtomTextConstructBase
+ end
+
+ class SubtitleBase < Base
+ include AtomTextConstructBase
+ end
+
+ class TitleBase < Base
+ include AtomTextConstructBase
+ end
+ end
+
+ CategoriesBase = ChannelBase::CategoriesBase
+ AuthorsBase = ChannelBase::AuthorsBase
+ LinksBase = ChannelBase::LinksBase
+ ContributorsBase = ChannelBase::ContributorsBase
+
+ class RightsBase < Base
+ include AtomTextConstructBase
+ end
+
+ class DescriptionBase < Base
+ include AtomTextConstructBase
+ end
+
+ class ContentBase < Base
+ include AtomTextConstructBase::EnsureXMLContent
+
+ %w(src).each do |element|
+ attr_accessor(element)
+ add_need_initialize_variable(element)
+ end
+
+ def xml_content=(content)
+ content = ensure_xml_content(content) if inline_xhtml?
+ @xml_content = content
+ end
+
+ alias_method(:xml, :xml_content)
+ alias_method(:xml=, :xml_content=)
+
+ def inline_text?
+ [nil, "text", "html"].include?(@type)
+ end
+
+ def inline_html?
+ @type == "html"
+ end
+
+ def inline_xhtml?
+ @type == "xhtml"
+ end
+
+ def inline_other?
+ !out_of_line? and ![nil, "text", "html", "xhtml"].include?(@type)
+ end
+
+ def inline_other_text?
+ return false if @type.nil? or out_of_line?
+ /\Atext\//i.match(@type) ? true : false
+ end
+
+ def inline_other_xml?
+ return false if @type.nil? or out_of_line?
+ /[\+\/]xml\z/i.match(@type) ? true : false
+ end
+
+ def inline_other_base64?
+ return false if @type.nil? or out_of_line?
+ @type.include?("/") and !inline_other_text? and !inline_other_xml?
+ end
+
+ def out_of_line?
+ not @src.nil? and @content.nil?
+ end
+ end
+
+ class TitleBase < Base
+ include AtomTextConstructBase
+ end
+ end
+ end
+
+ class TextinputBase < Base
+ %w(title description name link).each do |element|
+ attr_accessor element
+ add_need_initialize_variable(element)
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker/content.rb b/ruby/lib/rss/maker/content.rb
new file mode 100644
index 0000000..46c4911
--- /dev/null
+++ b/ruby/lib/rss/maker/content.rb
@@ -0,0 +1,21 @@
+require 'rss/content'
+require 'rss/maker/1.0'
+require 'rss/maker/2.0'
+
+module RSS
+ module Maker
+ module ContentModel
+ def self.append_features(klass)
+ super
+
+ ::RSS::ContentModel::ELEMENTS.each do |name|
+ klass.def_other_element(name)
+ end
+ end
+ end
+
+ class ItemsBase
+ class ItemBase; include ContentModel; end
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker/dublincore.rb b/ruby/lib/rss/maker/dublincore.rb
new file mode 100644
index 0000000..ff4813f
--- /dev/null
+++ b/ruby/lib/rss/maker/dublincore.rb
@@ -0,0 +1,124 @@
+require 'rss/dublincore'
+require 'rss/maker/1.0'
+
+module RSS
+ module Maker
+ module DublinCoreModel
+ def self.append_features(klass)
+ super
+
+ ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
+ plural_name ||= "#{name}s"
+ full_name = "#{RSS::DC_PREFIX}_#{name}"
+ full_plural_name = "#{RSS::DC_PREFIX}_#{plural_name}"
+ klass_name = Utils.to_class_name(name)
+ plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}"
+ full_plural_klass_name = "self.class::#{plural_klass_name}"
+ full_klass_name = "#{full_plural_klass_name}::#{klass_name}"
+ klass.def_classed_elements(full_name, "value", plural_klass_name,
+ full_plural_name, name)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def new_#{full_name}(value=nil)
+ _#{full_name} = #{full_plural_name}.new_#{name}
+ _#{full_name}.value = value
+ if block_given?
+ yield _#{full_name}
+ else
+ _#{full_name}
+ end
+ end
+ EOC
+ end
+
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ # For backward compatibility
+ alias #{DC_PREFIX}_rightses #{DC_PREFIX}_rights_list
+ EOC
+ end
+
+ ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
+ plural_name ||= "#{name}s"
+ full_name ||= "#{DC_PREFIX}_#{name}"
+ full_plural_name ||= "#{DC_PREFIX}_#{plural_name}"
+ klass_name = Utils.to_class_name(name)
+ full_klass_name = "DublinCore#{klass_name}"
+ plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}"
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class #{plural_klass_name}Base < Base
+ def_array_element(#{name.dump}, #{full_plural_name.dump},
+ #{full_klass_name.dump})
+
+ class #{full_klass_name}Base < Base
+ attr_accessor :value
+ add_need_initialize_variable("value")
+ alias_method(:content, :value)
+ alias_method(:content=, :value=)
+
+ def have_required_values?
+ @value
+ end
+
+ def to_feed(feed, current)
+ if value and current.respond_to?(:#{full_name})
+ new_item = current.class::#{full_klass_name}.new(value)
+ current.#{full_plural_name} << new_item
+ end
+ end
+ end
+ #{klass_name}Base = #{full_klass_name}Base
+ end
+ EOC
+ end
+
+ def self.install_dublin_core(klass)
+ ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
+ plural_name ||= "#{name}s"
+ klass_name = Utils.to_class_name(name)
+ full_klass_name = "DublinCore#{klass_name}"
+ plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}"
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class #{plural_klass_name} < #{plural_klass_name}Base
+ class #{full_klass_name} < #{full_klass_name}Base
+ end
+ #{klass_name} = #{full_klass_name}
+ end
+EOC
+ end
+ end
+ end
+
+ class ChannelBase
+ include DublinCoreModel
+ end
+
+ class ImageBase; include DublinCoreModel; end
+ class ItemsBase
+ class ItemBase
+ include DublinCoreModel
+ end
+ end
+ class TextinputBase; include DublinCoreModel; end
+
+ makers.each do |maker|
+ maker.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class Channel
+ DublinCoreModel.install_dublin_core(self)
+ end
+
+ class Image
+ DublinCoreModel.install_dublin_core(self)
+ end
+
+ class Items
+ class Item
+ DublinCoreModel.install_dublin_core(self)
+ end
+ end
+
+ class Textinput
+ DublinCoreModel.install_dublin_core(self)
+ end
+ EOC
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker/entry.rb b/ruby/lib/rss/maker/entry.rb
new file mode 100644
index 0000000..b35936b
--- /dev/null
+++ b/ruby/lib/rss/maker/entry.rb
@@ -0,0 +1,163 @@
+require "rss/maker/atom"
+require "rss/maker/feed"
+
+module RSS
+ module Maker
+ module Atom
+ class Entry < RSSBase
+ def initialize(feed_version="1.0")
+ super
+ @feed_type = "atom"
+ @feed_subtype = "entry"
+ end
+
+ private
+ def make_feed
+ ::RSS::Atom::Entry.new(@version, @encoding, @standalone)
+ end
+
+ def setup_elements(entry)
+ setup_items(entry)
+ end
+
+ class Channel < ChannelBase
+ class SkipDays < SkipDaysBase
+ class Day < DayBase
+ end
+ end
+
+ class SkipHours < SkipHoursBase
+ class Hour < HourBase
+ end
+ end
+
+ class Cloud < CloudBase
+ end
+
+ Categories = Feed::Channel::Categories
+ Links = Feed::Channel::Links
+ Authors = Feed::Channel::Authors
+ Contributors = Feed::Channel::Contributors
+
+ class Generator < GeneratorBase
+ include AtomGenerator
+
+ def self.not_set_name
+ "maker.channel.generator"
+ end
+ end
+
+ Copyright = Feed::Channel::Copyright
+
+ class Description < DescriptionBase
+ end
+
+ Title = Feed::Channel::Title
+ end
+
+ class Image < ImageBase
+ end
+
+ class Items < ItemsBase
+ def to_feed(entry)
+ (normalize.first || Item.new(@maker)).to_feed(entry)
+ end
+
+ class Item < ItemBase
+ def to_feed(entry)
+ set_default_values do
+ setup_values(entry)
+ entry.dc_dates.clear
+ setup_other_elements(entry)
+ unless have_required_values?
+ raise NotSetError.new("maker.item", not_set_required_variables)
+ end
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(id updated)
+ end
+
+ def variables
+ super + ["updated"]
+ end
+
+ def variable_is_set?
+ super or !authors.empty?
+ end
+
+ def not_set_required_variables
+ set_default_values do
+ vars = super
+ if authors.all? {|author| !author.have_required_values?}
+ vars << "author"
+ end
+ vars << "title" unless title {|t| t.have_required_values?}
+ vars
+ end
+ end
+
+ def _set_default_values(&block)
+ keep = {
+ :authors => authors.to_a.dup,
+ :contributors => contributors.to_a.dup,
+ :categories => categories.to_a.dup,
+ :id => id,
+ :links => links.to_a.dup,
+ :rights => @rights,
+ :title => @title,
+ :updated => updated,
+ }
+ authors.replace(@maker.channel.authors) if keep[:authors].empty?
+ if keep[:contributors].empty?
+ contributors.replace(@maker.channel.contributors)
+ end
+ if keep[:categories].empty?
+ categories.replace(@maker.channel.categories)
+ end
+ self.id ||= link || @maker.channel.id
+ links.replace(@maker.channel.links) if keep[:links].empty?
+ unless keep[:rights].variable_is_set?
+ @maker.channel.rights {|r| @rights = r}
+ end
+ unless keep[:title].variable_is_set?
+ @maker.channel.title {|t| @title = t}
+ end
+ self.updated ||= @maker.channel.updated
+ super(&block)
+ ensure
+ authors.replace(keep[:authors])
+ contributors.replace(keep[:contributors])
+ categories.replace(keep[:categories])
+ links.replace(keep[:links])
+ self.id = keep[:id]
+ @rights = keep[:rights]
+ @title = keep[:title]
+ self.updated = keep[:updated]
+ end
+
+ Guid = Feed::Items::Item::Guid
+ Enclosure = Feed::Items::Item::Enclosure
+ Source = Feed::Items::Item::Source
+ Categories = Feed::Items::Item::Categories
+ Authors = Feed::Items::Item::Authors
+ Contributors = Feed::Items::Item::Contributors
+ Links = Feed::Items::Item::Links
+ Rights = Feed::Items::Item::Rights
+ Description = Feed::Items::Item::Description
+ Title = Feed::Items::Item::Title
+ Content = Feed::Items::Item::Content
+ end
+ end
+
+ class Textinput < TextinputBase
+ end
+ end
+ end
+
+ add_maker("atom:entry", "1.0", Atom::Entry)
+ add_maker("atom1.0:entry", "1.0", Atom::Entry)
+ end
+end
diff --git a/ruby/lib/rss/maker/feed.rb b/ruby/lib/rss/maker/feed.rb
new file mode 100644
index 0000000..303959a
--- /dev/null
+++ b/ruby/lib/rss/maker/feed.rb
@@ -0,0 +1,430 @@
+require "rss/maker/atom"
+
+module RSS
+ module Maker
+ module Atom
+ class Feed < RSSBase
+ def initialize(feed_version="1.0")
+ super
+ @feed_type = "atom"
+ @feed_subtype = "feed"
+ end
+
+ private
+ def make_feed
+ ::RSS::Atom::Feed.new(@version, @encoding, @standalone)
+ end
+
+ def setup_elements(feed)
+ setup_channel(feed)
+ setup_image(feed)
+ setup_items(feed)
+ end
+
+ class Channel < ChannelBase
+ def to_feed(feed)
+ set_default_values do
+ setup_values(feed)
+ feed.dc_dates.clear
+ setup_other_elements(feed)
+ if image_favicon.about
+ icon = feed.class::Icon.new
+ icon.content = image_favicon.about
+ feed.icon = icon
+ end
+ unless have_required_values?
+ raise NotSetError.new("maker.channel",
+ not_set_required_variables)
+ end
+ end
+ end
+
+ def have_required_values?
+ super and
+ (!authors.empty? or
+ @maker.items.any? {|item| !item.authors.empty?})
+ end
+
+ private
+ def required_variable_names
+ %w(id updated)
+ end
+
+ def variables
+ super + %w(id updated)
+ end
+
+ def variable_is_set?
+ super or !authors.empty?
+ end
+
+ def not_set_required_variables
+ vars = super
+ if authors.empty? and
+ @maker.items.all? {|item| item.author.to_s.empty?}
+ vars << "author"
+ end
+ vars << "title" unless title {|t| t.have_required_values?}
+ vars
+ end
+
+ def _set_default_values(&block)
+ keep = {
+ :id => id,
+ :updated => updated,
+ }
+ self.id ||= about
+ self.updated ||= dc_date
+ super(&block)
+ ensure
+ self.id = keep[:id]
+ self.updated = keep[:updated]
+ end
+
+ class SkipDays < SkipDaysBase
+ def to_feed(*args)
+ end
+
+ class Day < DayBase
+ end
+ end
+
+ class SkipHours < SkipHoursBase
+ def to_feed(*args)
+ end
+
+ class Hour < HourBase
+ end
+ end
+
+ class Cloud < CloudBase
+ def to_feed(*args)
+ end
+ end
+
+ class Categories < CategoriesBase
+ class Category < CategoryBase
+ include AtomCategory
+
+ def self.not_set_name
+ "maker.channel.category"
+ end
+ end
+ end
+
+ class Links < LinksBase
+ class Link < LinkBase
+ include AtomLink
+
+ def self.not_set_name
+ "maker.channel.link"
+ end
+ end
+ end
+
+ AtomPersons.def_atom_persons(self, "author", "maker.channel.author")
+ AtomPersons.def_atom_persons(self, "contributor",
+ "maker.channel.contributor")
+
+ class Generator < GeneratorBase
+ include AtomGenerator
+
+ def self.not_set_name
+ "maker.channel.generator"
+ end
+ end
+
+ AtomTextConstruct.def_atom_text_construct(self, "rights",
+ "maker.channel.copyright",
+ "Copyright")
+ AtomTextConstruct.def_atom_text_construct(self, "subtitle",
+ "maker.channel.description",
+ "Description")
+ AtomTextConstruct.def_atom_text_construct(self, "title",
+ "maker.channel.title")
+ end
+
+ class Image < ImageBase
+ def to_feed(feed)
+ logo = feed.class::Logo.new
+ class << logo
+ alias_method(:url=, :content=)
+ end
+ set = setup_values(logo)
+ class << logo
+ remove_method(:url=)
+ end
+ if set
+ feed.logo = logo
+ set_parent(logo, feed)
+ setup_other_elements(feed, logo)
+ elsif variable_is_set?
+ raise NotSetError.new("maker.image", not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(url)
+ end
+ end
+
+ class Items < ItemsBase
+ def to_feed(feed)
+ normalize.each do |item|
+ item.to_feed(feed)
+ end
+ setup_other_elements(feed, feed.entries)
+ end
+
+ class Item < ItemBase
+ def to_feed(feed)
+ set_default_values do
+ entry = feed.class::Entry.new
+ set = setup_values(entry)
+ entry.dc_dates.clear
+ setup_other_elements(feed, entry)
+ if set
+ feed.entries << entry
+ set_parent(entry, feed)
+ elsif variable_is_set?
+ raise NotSetError.new("maker.item", not_set_required_variables)
+ end
+ end
+ end
+
+ def have_required_values?
+ set_default_values do
+ super and title {|t| t.have_required_values?}
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(id updated)
+ end
+
+ def variables
+ super + ["updated"]
+ end
+
+ def not_set_required_variables
+ vars = super
+ vars << "title" unless title {|t| t.have_required_values?}
+ vars
+ end
+
+ def _set_default_values(&block)
+ keep = {
+ :id => id,
+ :updated => updated,
+ }
+ self.id ||= link
+ self.updated ||= dc_date
+ super(&block)
+ ensure
+ self.id = keep[:id]
+ self.updated = keep[:updated]
+ end
+
+ class Guid < GuidBase
+ def to_feed(feed, current)
+ end
+ end
+
+ class Enclosure < EnclosureBase
+ def to_feed(feed, current)
+ end
+ end
+
+ class Source < SourceBase
+ def to_feed(feed, current)
+ source = current.class::Source.new
+ setup_values(source)
+ current.source = source
+ set_parent(source, current)
+ setup_other_elements(feed, source)
+ current.source = nil if source.to_s == "<source/>"
+ end
+
+ private
+ def required_variable_names
+ []
+ end
+
+ def variables
+ super + ["updated"]
+ end
+
+ AtomPersons.def_atom_persons(self, "author",
+ "maker.item.source.author")
+ AtomPersons.def_atom_persons(self, "contributor",
+ "maker.item.source.contributor")
+
+ class Categories < CategoriesBase
+ class Category < CategoryBase
+ include AtomCategory
+
+ def self.not_set_name
+ "maker.item.source.category"
+ end
+ end
+ end
+
+ class Generator < GeneratorBase
+ include AtomGenerator
+
+ def self.not_set_name
+ "maker.item.source.generator"
+ end
+ end
+
+ class Icon < IconBase
+ def to_feed(feed, current)
+ icon = current.class::Icon.new
+ class << icon
+ alias_method(:url=, :content=)
+ end
+ set = setup_values(icon)
+ class << icon
+ remove_method(:url=)
+ end
+ if set
+ current.icon = icon
+ set_parent(icon, current)
+ setup_other_elements(feed, icon)
+ elsif variable_is_set?
+ raise NotSetError.new("maker.item.source.icon",
+ not_set_required_variables)
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(url)
+ end
+ end
+
+ class Links < LinksBase
+ class Link < LinkBase
+ include AtomLink
+
+ def self.not_set_name
+ "maker.item.source.link"
+ end
+ end
+ end
+
+ class Logo < LogoBase
+ include AtomLogo
+
+ def self.not_set_name
+ "maker.item.source.logo"
+ end
+ end
+
+ maker_name_base = "maker.item.source."
+ maker_name = "#{maker_name_base}rights"
+ AtomTextConstruct.def_atom_text_construct(self, "rights",
+ maker_name)
+ maker_name = "#{maker_name_base}subtitle"
+ AtomTextConstruct.def_atom_text_construct(self, "subtitle",
+ maker_name)
+ maker_name = "#{maker_name_base}title"
+ AtomTextConstruct.def_atom_text_construct(self, "title",
+ maker_name)
+ end
+
+ class Categories < CategoriesBase
+ class Category < CategoryBase
+ include AtomCategory
+
+ def self.not_set_name
+ "maker.item.category"
+ end
+ end
+ end
+
+ AtomPersons.def_atom_persons(self, "author", "maker.item.author")
+ AtomPersons.def_atom_persons(self, "contributor",
+ "maker.item.contributor")
+
+ class Links < LinksBase
+ class Link < LinkBase
+ include AtomLink
+
+ def self.not_set_name
+ "maker.item.link"
+ end
+ end
+ end
+
+ AtomTextConstruct.def_atom_text_construct(self, "rights",
+ "maker.item.rights")
+ AtomTextConstruct.def_atom_text_construct(self, "summary",
+ "maker.item.description",
+ "Description")
+ AtomTextConstruct.def_atom_text_construct(self, "title",
+ "maker.item.title")
+
+ class Content < ContentBase
+ def to_feed(feed, current)
+ content = current.class::Content.new
+ if setup_values(content)
+ content.src = nil if content.src and content.content
+ current.content = content
+ set_parent(content, current)
+ setup_other_elements(feed, content)
+ elsif variable_is_set?
+ raise NotSetError.new("maker.item.content",
+ not_set_required_variables)
+ end
+ end
+
+ alias_method(:xml, :xml_content)
+
+ private
+ def required_variable_names
+ if out_of_line?
+ %w(type)
+ elsif xml_type?
+ %w(xml_content)
+ else
+ %w(content)
+ end
+ end
+
+ def variables
+ if out_of_line?
+ super
+ elsif xml_type?
+ super + %w(xml)
+ else
+ super
+ end
+ end
+
+ def xml_type?
+ _type = type
+ return false if _type.nil?
+ _type == "xhtml" or
+ /(?:\+xml|\/xml)$/i =~ _type or
+ %w(text/xml-external-parsed-entity
+ application/xml-external-parsed-entity
+ application/xml-dtd).include?(_type.downcase)
+ end
+ end
+ end
+ end
+
+ class Textinput < TextinputBase
+ end
+ end
+ end
+
+ add_maker("atom", "1.0", Atom::Feed)
+ add_maker("atom:feed", "1.0", Atom::Feed)
+ add_maker("atom1.0", "1.0", Atom::Feed)
+ add_maker("atom1.0:feed", "1.0", Atom::Feed)
+ end
+end
diff --git a/ruby/lib/rss/maker/image.rb b/ruby/lib/rss/maker/image.rb
new file mode 100644
index 0000000..b95cf4c
--- /dev/null
+++ b/ruby/lib/rss/maker/image.rb
@@ -0,0 +1,111 @@
+require 'rss/image'
+require 'rss/maker/1.0'
+require 'rss/maker/dublincore'
+
+module RSS
+ module Maker
+ module ImageItemModel
+ def self.append_features(klass)
+ super
+
+ name = "#{RSS::IMAGE_PREFIX}_item"
+ klass.def_classed_element(name)
+ end
+
+ def self.install_image_item(klass)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class ImageItem < ImageItemBase
+ DublinCoreModel.install_dublin_core(self)
+ end
+EOC
+ end
+
+ class ImageItemBase < Base
+ include Maker::DublinCoreModel
+
+ attr_accessor :about, :resource, :image_width, :image_height
+ add_need_initialize_variable("about")
+ add_need_initialize_variable("resource")
+ add_need_initialize_variable("image_width")
+ add_need_initialize_variable("image_height")
+ alias width= image_width=
+ alias width image_width
+ alias height= image_height=
+ alias height image_height
+
+ def have_required_values?
+ @about
+ end
+
+ def to_feed(feed, current)
+ if current.respond_to?(:image_item=) and have_required_values?
+ item = current.class::ImageItem.new
+ setup_values(item)
+ setup_other_elements(item)
+ current.image_item = item
+ end
+ end
+ end
+ end
+
+ module ImageFaviconModel
+ def self.append_features(klass)
+ super
+
+ name = "#{RSS::IMAGE_PREFIX}_favicon"
+ klass.def_classed_element(name)
+ end
+
+ def self.install_image_favicon(klass)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class ImageFavicon < ImageFaviconBase
+ DublinCoreModel.install_dublin_core(self)
+ end
+ EOC
+ end
+
+ class ImageFaviconBase < Base
+ include Maker::DublinCoreModel
+
+ attr_accessor :about, :image_size
+ add_need_initialize_variable("about")
+ add_need_initialize_variable("image_size")
+ alias size image_size
+ alias size= image_size=
+
+ def have_required_values?
+ @about and @image_size
+ end
+
+ def to_feed(feed, current)
+ if current.respond_to?(:image_favicon=) and have_required_values?
+ favicon = current.class::ImageFavicon.new
+ setup_values(favicon)
+ setup_other_elements(favicon)
+ current.image_favicon = favicon
+ end
+ end
+ end
+ end
+
+ class ChannelBase; include Maker::ImageFaviconModel; end
+
+ class ItemsBase
+ class ItemBase; include Maker::ImageItemModel; end
+ end
+
+ makers.each do |maker|
+ maker.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class Channel
+ ImageFaviconModel.install_image_favicon(self)
+ end
+
+ class Items
+ class Item
+ ImageItemModel.install_image_item(self)
+ end
+ end
+ EOC
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker/itunes.rb b/ruby/lib/rss/maker/itunes.rb
new file mode 100644
index 0000000..8b7420d
--- /dev/null
+++ b/ruby/lib/rss/maker/itunes.rb
@@ -0,0 +1,242 @@
+require 'rss/itunes'
+require 'rss/maker/2.0'
+
+module RSS
+ module Maker
+ module ITunesBaseModel
+ def def_class_accessor(klass, name, type, *args)
+ name = name.gsub(/-/, "_").gsub(/^itunes_/, '')
+ full_name = "#{RSS::ITUNES_PREFIX}_#{name}"
+ case type
+ when nil
+ klass.def_other_element(full_name)
+ when :yes_other
+ def_yes_other_accessor(klass, full_name)
+ when :yes_clean_other
+ def_yes_clean_other_accessor(klass, full_name)
+ when :csv
+ def_csv_accessor(klass, full_name)
+ when :element, :attribute
+ recommended_attribute_name, = *args
+ klass_name = "ITunes#{Utils.to_class_name(name)}"
+ klass.def_classed_element(full_name, klass_name,
+ recommended_attribute_name)
+ when :elements
+ plural_name, recommended_attribute_name = args
+ plural_name ||= "#{name}s"
+ full_plural_name = "#{RSS::ITUNES_PREFIX}_#{plural_name}"
+ klass_name = "ITunes#{Utils.to_class_name(name)}"
+ plural_klass_name = "ITunes#{Utils.to_class_name(plural_name)}"
+ def_elements_class_accessor(klass, name, full_name, full_plural_name,
+ klass_name, plural_klass_name,
+ recommended_attribute_name)
+ end
+ end
+
+ def def_yes_other_accessor(klass, full_name)
+ klass.def_other_element(full_name)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{full_name}?
+ Utils::YesOther.parse(@#{full_name})
+ end
+ EOC
+ end
+
+ def def_yes_clean_other_accessor(klass, full_name)
+ klass.def_other_element(full_name)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{full_name}?
+ Utils::YesCleanOther.parse(#{full_name})
+ end
+ EOC
+ end
+
+ def def_csv_accessor(klass, full_name)
+ klass.def_csv_element(full_name)
+ end
+
+ def def_elements_class_accessor(klass, name, full_name, full_plural_name,
+ klass_name, plural_klass_name,
+ recommended_attribute_name=nil)
+ if recommended_attribute_name
+ klass.def_classed_elements(full_name, recommended_attribute_name,
+ plural_klass_name, full_plural_name)
+ else
+ klass.def_classed_element(full_plural_name, plural_klass_name)
+ end
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def new_#{full_name}(text=nil)
+ #{full_name} = @#{full_plural_name}.new_#{name}
+ #{full_name}.text = text
+ if block_given?
+ yield #{full_name}
+ else
+ #{full_name}
+ end
+ end
+ EOC
+ end
+ end
+
+ module ITunesChannelModel
+ extend ITunesBaseModel
+
+ class << self
+ def append_features(klass)
+ super
+
+ ::RSS::ITunesChannelModel::ELEMENT_INFOS.each do |name, type, *args|
+ def_class_accessor(klass, name, type, *args)
+ end
+ end
+ end
+
+ class ITunesCategoriesBase < Base
+ def_array_element("category", "itunes_categories",
+ "ITunesCategory")
+ class ITunesCategoryBase < Base
+ attr_accessor :text
+ add_need_initialize_variable("text")
+ def_array_element("category", "itunes_categories",
+ "ITunesCategory")
+
+ def have_required_values?
+ text
+ end
+
+ alias_method :to_feed_for_categories, :to_feed
+ def to_feed(feed, current)
+ if text and current.respond_to?(:itunes_category)
+ new_item = current.class::ITunesCategory.new(text)
+ to_feed_for_categories(feed, new_item)
+ current.itunes_categories << new_item
+ end
+ end
+ end
+ end
+
+ class ITunesImageBase < Base
+ add_need_initialize_variable("href")
+ attr_accessor("href")
+
+ def to_feed(feed, current)
+ if @href and current.respond_to?(:itunes_image)
+ current.itunes_image ||= current.class::ITunesImage.new
+ current.itunes_image.href = @href
+ end
+ end
+ end
+
+ class ITunesOwnerBase < Base
+ %w(itunes_name itunes_email).each do |name|
+ add_need_initialize_variable(name)
+ attr_accessor(name)
+ end
+
+ def to_feed(feed, current)
+ if current.respond_to?(:itunes_owner=)
+ _not_set_required_variables = not_set_required_variables
+ if (required_variable_names - _not_set_required_variables).empty?
+ return
+ end
+
+ unless have_required_values?
+ raise NotSetError.new("maker.channel.itunes_owner",
+ _not_set_required_variables)
+ end
+ current.itunes_owner ||= current.class::ITunesOwner.new
+ current.itunes_owner.itunes_name = @itunes_name
+ current.itunes_owner.itunes_email = @itunes_email
+ end
+ end
+
+ private
+ def required_variable_names
+ %w(itunes_name itunes_email)
+ end
+ end
+ end
+
+ module ITunesItemModel
+ extend ITunesBaseModel
+
+ class << self
+ def append_features(klass)
+ super
+
+ ::RSS::ITunesItemModel::ELEMENT_INFOS.each do |name, type, *args|
+ def_class_accessor(klass, name, type, *args)
+ end
+ end
+ end
+
+ class ITunesDurationBase < Base
+ attr_reader :content
+ add_need_initialize_variable("content")
+
+ %w(hour minute second).each do |name|
+ attr_reader(name)
+ add_need_initialize_variable(name, 0)
+ end
+
+ def content=(content)
+ if content.nil?
+ @hour, @minute, @second, @content = nil
+ else
+ @hour, @minute, @second =
+ ::RSS::ITunesItemModel::ITunesDuration.parse(content)
+ @content = content
+ end
+ end
+
+ def hour=(hour)
+ @hour = Integer(hour)
+ update_content
+ end
+
+ def minute=(minute)
+ @minute = Integer(minute)
+ update_content
+ end
+
+ def second=(second)
+ @second = Integer(second)
+ update_content
+ end
+
+ def to_feed(feed, current)
+ if @content and current.respond_to?(:itunes_duration=)
+ current.itunes_duration ||= current.class::ITunesDuration.new
+ current.itunes_duration.content = @content
+ end
+ end
+
+ private
+ def update_content
+ components = [@hour, @minute, @second]
+ @content =
+ ::RSS::ITunesItemModel::ITunesDuration.construct(*components)
+ end
+ end
+ end
+
+ class ChannelBase
+ include Maker::ITunesChannelModel
+ class ITunesCategories < ITunesCategoriesBase
+ class ITunesCategory < ITunesCategoryBase
+ ITunesCategory = self
+ end
+ end
+
+ class ITunesImage < ITunesImageBase; end
+ class ITunesOwner < ITunesOwnerBase; end
+ end
+
+ class ItemsBase
+ class ItemBase
+ include Maker::ITunesItemModel
+ class ITunesDuration < ITunesDurationBase; end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker/slash.rb b/ruby/lib/rss/maker/slash.rb
new file mode 100644
index 0000000..27adef3
--- /dev/null
+++ b/ruby/lib/rss/maker/slash.rb
@@ -0,0 +1,33 @@
+require 'rss/slash'
+require 'rss/maker/1.0'
+
+module RSS
+ module Maker
+ module SlashModel
+ def self.append_features(klass)
+ super
+
+ ::RSS::SlashModel::ELEMENT_INFOS.each do |name, type|
+ full_name = "#{RSS::SLASH_PREFIX}_#{name}"
+ case type
+ when :csv_integer
+ klass.def_csv_element(full_name, :integer)
+ else
+ klass.def_other_element(full_name)
+ end
+ end
+
+ klass.module_eval do
+ alias_method(:slash_hit_parades, :slash_hit_parade)
+ alias_method(:slash_hit_parades=, :slash_hit_parade=)
+ end
+ end
+ end
+
+ class ItemsBase
+ class ItemBase
+ include SlashModel
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker/syndication.rb b/ruby/lib/rss/maker/syndication.rb
new file mode 100644
index 0000000..b812304
--- /dev/null
+++ b/ruby/lib/rss/maker/syndication.rb
@@ -0,0 +1,18 @@
+require 'rss/syndication'
+require 'rss/maker/1.0'
+
+module RSS
+ module Maker
+ module SyndicationModel
+ def self.append_features(klass)
+ super
+
+ ::RSS::SyndicationModel::ELEMENTS.each do |name|
+ klass.def_other_element(name)
+ end
+ end
+ end
+
+ class ChannelBase; include SyndicationModel; end
+ end
+end
diff --git a/ruby/lib/rss/maker/taxonomy.rb b/ruby/lib/rss/maker/taxonomy.rb
new file mode 100644
index 0000000..2116038
--- /dev/null
+++ b/ruby/lib/rss/maker/taxonomy.rb
@@ -0,0 +1,118 @@
+require 'rss/taxonomy'
+require 'rss/maker/1.0'
+require 'rss/maker/dublincore'
+
+module RSS
+ module Maker
+ module TaxonomyTopicsModel
+ def self.append_features(klass)
+ super
+
+ klass.def_classed_element("#{RSS::TAXO_PREFIX}_topics",
+ "TaxonomyTopics")
+ end
+
+ def self.install_taxo_topics(klass)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class TaxonomyTopics < TaxonomyTopicsBase
+ def to_feed(feed, current)
+ if current.respond_to?(:taxo_topics)
+ topics = current.class::TaxonomyTopics.new
+ bag = topics.Bag
+ @resources.each do |resource|
+ bag.lis << RDF::Bag::Li.new(resource)
+ end
+ current.taxo_topics = topics
+ end
+ end
+ end
+EOC
+ end
+
+ class TaxonomyTopicsBase < Base
+ attr_reader :resources
+ def_array_element("resource")
+ remove_method :new_resource
+ end
+ end
+
+ module TaxonomyTopicModel
+ def self.append_features(klass)
+ super
+
+ class_name = "TaxonomyTopics"
+ klass.def_classed_elements("#{TAXO_PREFIX}_topic", "value", class_name)
+ end
+
+ def self.install_taxo_topic(klass)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class TaxonomyTopics < TaxonomyTopicsBase
+ class TaxonomyTopic < TaxonomyTopicBase
+ DublinCoreModel.install_dublin_core(self)
+ TaxonomyTopicsModel.install_taxo_topics(self)
+
+ def to_feed(feed, current)
+ if current.respond_to?(:taxo_topics)
+ topic = current.class::TaxonomyTopic.new(value)
+ topic.taxo_link = value
+ taxo_topics.to_feed(feed, topic) if taxo_topics
+ current.taxo_topics << topic
+ setup_other_elements(feed, topic)
+ end
+ end
+ end
+ end
+EOC
+ end
+
+ class TaxonomyTopicsBase < Base
+ def_array_element("topic", nil, "TaxonomyTopic")
+ alias_method(:new_taxo_topic, :new_topic) # For backward compatibility
+
+ class TaxonomyTopicBase < Base
+ include DublinCoreModel
+ include TaxonomyTopicsModel
+
+ attr_accessor :value
+ add_need_initialize_variable("value")
+ alias_method(:taxo_link, :value)
+ alias_method(:taxo_link=, :value=)
+
+ def have_required_values?
+ @value
+ end
+ end
+ end
+ end
+
+ class RSSBase
+ include TaxonomyTopicModel
+ end
+
+ class ChannelBase
+ include TaxonomyTopicsModel
+ end
+
+ class ItemsBase
+ class ItemBase
+ include TaxonomyTopicsModel
+ end
+ end
+
+ makers.each do |maker|
+ maker.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ TaxonomyTopicModel.install_taxo_topic(self)
+
+ class Channel
+ TaxonomyTopicsModel.install_taxo_topics(self)
+ end
+
+ class Items
+ class Item
+ TaxonomyTopicsModel.install_taxo_topics(self)
+ end
+ end
+ EOC
+ end
+ end
+end
diff --git a/ruby/lib/rss/maker/trackback.rb b/ruby/lib/rss/maker/trackback.rb
new file mode 100644
index 0000000..278fe53
--- /dev/null
+++ b/ruby/lib/rss/maker/trackback.rb
@@ -0,0 +1,61 @@
+require 'rss/trackback'
+require 'rss/maker/1.0'
+require 'rss/maker/2.0'
+
+module RSS
+ module Maker
+ module TrackBackModel
+ def self.append_features(klass)
+ super
+
+ klass.def_other_element("#{RSS::TRACKBACK_PREFIX}_ping")
+ klass.def_classed_elements("#{RSS::TRACKBACK_PREFIX}_about", "value",
+ "TrackBackAbouts")
+ end
+
+ class TrackBackAboutsBase < Base
+ def_array_element("about", nil, "TrackBackAbout")
+
+ class TrackBackAboutBase < Base
+ attr_accessor :value
+ add_need_initialize_variable("value")
+
+ alias_method(:resource, :value)
+ alias_method(:resource=, :value=)
+ alias_method(:content, :value)
+ alias_method(:content=, :value=)
+
+ def have_required_values?
+ @value
+ end
+
+ def to_feed(feed, current)
+ if current.respond_to?(:trackback_abouts) and have_required_values?
+ about = current.class::TrackBackAbout.new
+ setup_values(about)
+ setup_other_elements(about)
+ current.trackback_abouts << about
+ end
+ end
+ end
+ end
+ end
+
+ class ItemsBase
+ class ItemBase; include TrackBackModel; end
+ end
+
+ makers.each do |maker|
+ maker.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ class Items
+ class Item
+ class TrackBackAbouts < TrackBackAboutsBase
+ class TrackBackAbout < TrackBackAboutBase
+ end
+ end
+ end
+ end
+ EOC
+ end
+ end
+end
diff --git a/ruby/lib/rss/parser.rb b/ruby/lib/rss/parser.rb
new file mode 100644
index 0000000..359c014
--- /dev/null
+++ b/ruby/lib/rss/parser.rb
@@ -0,0 +1,568 @@
+require "forwardable"
+require "open-uri"
+
+require "rss/rss"
+require "rss/xml"
+
+module RSS
+
+ class NotWellFormedError < Error
+ attr_reader :line, :element
+
+ # Create a new NotWellFormedError for an error at +line+
+ # in +element+. If a block is given the return value of
+ # the block ends up in the error message.
+ def initialize(line=nil, element=nil)
+ message = "This is not well formed XML"
+ if element or line
+ message << "\nerror occurred"
+ message << " in #{element}" if element
+ message << " at about #{line} line" if line
+ end
+ message << "\n#{yield}" if block_given?
+ super(message)
+ end
+ end
+
+ class XMLParserNotFound < Error
+ def initialize
+ super("available XML parser was not found in " <<
+ "#{AVAILABLE_PARSER_LIBRARIES.inspect}.")
+ end
+ end
+
+ class NotValidXMLParser < Error
+ def initialize(parser)
+ super("#{parser} is not an available XML parser. " <<
+ "Available XML parser" <<
+ (AVAILABLE_PARSERS.size > 1 ? "s are " : " is ") <<
+ "#{AVAILABLE_PARSERS.inspect}.")
+ end
+ end
+
+ class NSError < InvalidRSSError
+ attr_reader :tag, :prefix, :uri
+ def initialize(tag, prefix, require_uri)
+ @tag, @prefix, @uri = tag, prefix, require_uri
+ super("prefix <#{prefix}> doesn't associate uri " <<
+ "<#{require_uri}> in tag <#{tag}>")
+ end
+ end
+
+ class Parser
+
+ extend Forwardable
+
+ class << self
+
+ @@default_parser = nil
+
+ def default_parser
+ @@default_parser || AVAILABLE_PARSERS.first
+ end
+
+ # Set @@default_parser to new_value if it is one of the
+ # available parsers. Else raise NotValidXMLParser error.
+ def default_parser=(new_value)
+ if AVAILABLE_PARSERS.include?(new_value)
+ @@default_parser = new_value
+ else
+ raise NotValidXMLParser.new(new_value)
+ end
+ end
+
+ def parse(rss, do_validate=true, ignore_unknown_element=true,
+ parser_class=default_parser)
+ parser = new(rss, parser_class)
+ parser.do_validate = do_validate
+ parser.ignore_unknown_element = ignore_unknown_element
+ parser.parse
+ end
+ end
+
+ def_delegators(:@parser, :parse, :rss,
+ :ignore_unknown_element,
+ :ignore_unknown_element=, :do_validate,
+ :do_validate=)
+
+ def initialize(rss, parser_class=self.class.default_parser)
+ @parser = parser_class.new(normalize_rss(rss))
+ end
+
+ private
+
+ # Try to get the XML associated with +rss+.
+ # Return +rss+ if it already looks like XML, or treat it as a URI,
+ # or a file to get the XML,
+ def normalize_rss(rss)
+ return rss if maybe_xml?(rss)
+
+ uri = to_uri(rss)
+
+ if uri.respond_to?(:read)
+ uri.read
+ elsif !rss.tainted? and File.readable?(rss)
+ File.open(rss) {|f| f.read}
+ else
+ rss
+ end
+ end
+
+ # maybe_xml? tests if source is a string that looks like XML.
+ def maybe_xml?(source)
+ source.is_a?(String) and /</ =~ source
+ end
+
+ # Attempt to convert rss to a URI, but just return it if
+ # there's a ::URI::Error
+ def to_uri(rss)
+ return rss if rss.is_a?(::URI::Generic)
+
+ begin
+ ::URI.parse(rss)
+ rescue ::URI::Error
+ rss
+ end
+ end
+ end
+
+ class BaseParser
+
+ class << self
+ def raise_for_undefined_entity?
+ listener.raise_for_undefined_entity?
+ end
+ end
+
+ def initialize(rss)
+ @listener = self.class.listener.new
+ @rss = rss
+ end
+
+ def rss
+ @listener.rss
+ end
+
+ def ignore_unknown_element
+ @listener.ignore_unknown_element
+ end
+
+ def ignore_unknown_element=(new_value)
+ @listener.ignore_unknown_element = new_value
+ end
+
+ def do_validate
+ @listener.do_validate
+ end
+
+ def do_validate=(new_value)
+ @listener.do_validate = new_value
+ end
+
+ def parse
+ if @listener.rss.nil?
+ _parse
+ end
+ @listener.rss
+ end
+
+ end
+
+ class BaseListener
+
+ extend Utils
+
+ class << self
+
+ @@accessor_bases = {}
+ @@registered_uris = {}
+ @@class_names = {}
+
+ # return the setter for the uri, tag_name pair, or nil.
+ def setter(uri, tag_name)
+ _getter = getter(uri, tag_name)
+ if _getter
+ "#{_getter}="
+ else
+ nil
+ end
+ end
+
+ def getter(uri, tag_name)
+ (@@accessor_bases[uri] || {})[tag_name]
+ end
+
+ # return the tag_names for setters associated with uri
+ def available_tags(uri)
+ (@@accessor_bases[uri] || {}).keys
+ end
+
+ # register uri against this name.
+ def register_uri(uri, name)
+ @@registered_uris[name] ||= {}
+ @@registered_uris[name][uri] = nil
+ end
+
+ # test if this uri is registered against this name
+ def uri_registered?(uri, name)
+ @@registered_uris[name].has_key?(uri)
+ end
+
+ # record class_name for the supplied uri and tag_name
+ def install_class_name(uri, tag_name, class_name)
+ @@class_names[uri] ||= {}
+ @@class_names[uri][tag_name] = class_name
+ end
+
+ # retrieve class_name for the supplied uri and tag_name
+ # If it doesn't exist, capitalize the tag_name
+ def class_name(uri, tag_name)
+ name = (@@class_names[uri] || {})[tag_name]
+ return name if name
+
+ tag_name = tag_name.gsub(/[_\-]([a-z]?)/) {$1.upcase}
+ tag_name[0, 1].upcase + tag_name[1..-1]
+ end
+
+ def install_get_text_element(uri, name, accessor_base)
+ install_accessor_base(uri, name, accessor_base)
+ def_get_text_element(uri, name, *get_file_and_line_from_caller(1))
+ end
+
+ def raise_for_undefined_entity?
+ true
+ end
+
+ private
+ # set the accessor for the uri, tag_name pair
+ def install_accessor_base(uri, tag_name, accessor_base)
+ @@accessor_bases[uri] ||= {}
+ @@accessor_bases[uri][tag_name] = accessor_base.chomp("=")
+ end
+
+ def def_get_text_element(uri, element_name, file, line)
+ register_uri(uri, element_name)
+ method_name = "start_#{element_name}"
+ unless private_method_defined?(method_name)
+ define_method(method_name) do |name, prefix, attrs, ns|
+ uri = _ns(ns, prefix)
+ if self.class.uri_registered?(uri, element_name)
+ start_get_text_element(name, prefix, ns, uri)
+ else
+ start_else_element(name, prefix, attrs, ns)
+ end
+ end
+ private(method_name)
+ end
+ end
+ end
+ end
+
+ module ListenerMixin
+ attr_reader :rss
+
+ attr_accessor :ignore_unknown_element
+ attr_accessor :do_validate
+
+ def initialize
+ @rss = nil
+ @ignore_unknown_element = true
+ @do_validate = true
+ @ns_stack = [{"xml" => :xml}]
+ @tag_stack = [[]]
+ @text_stack = ['']
+ @proc_stack = []
+ @last_element = nil
+ @version = @encoding = @standalone = nil
+ @xml_stylesheets = []
+ @xml_child_mode = false
+ @xml_element = nil
+ @last_xml_element = nil
+ end
+
+ # set instance vars for version, encoding, standalone
+ def xmldecl(version, encoding, standalone)
+ @version, @encoding, @standalone = version, encoding, standalone
+ end
+
+ def instruction(name, content)
+ if name == "xml-stylesheet"
+ params = parse_pi_content(content)
+ if params.has_key?("href")
+ @xml_stylesheets << XMLStyleSheet.new(params)
+ end
+ end
+ end
+
+ def tag_start(name, attributes)
+ @text_stack.push('')
+
+ ns = @ns_stack.last.dup
+ attrs = {}
+ attributes.each do |n, v|
+ if /\Axmlns(?:\z|:)/ =~ n
+ ns[$POSTMATCH] = v
+ else
+ attrs[n] = v
+ end
+ end
+ @ns_stack.push(ns)
+
+ prefix, local = split_name(name)
+ @tag_stack.last.push([_ns(ns, prefix), local])
+ @tag_stack.push([])
+ if @xml_child_mode
+ previous = @last_xml_element
+ element_attrs = attributes.dup
+ unless previous
+ ns.each do |ns_prefix, value|
+ next if ns_prefix == "xml"
+ key = ns_prefix.empty? ? "xmlns" : "xmlns:#{ns_prefix}"
+ element_attrs[key] ||= value
+ end
+ end
+ next_element = XML::Element.new(local,
+ prefix.empty? ? nil : prefix,
+ _ns(ns, prefix),
+ element_attrs)
+ previous << next_element if previous
+ @last_xml_element = next_element
+ pr = Proc.new do |text, tags|
+ if previous
+ @last_xml_element = previous
+ else
+ @xml_element = @last_xml_element
+ @last_xml_element = nil
+ end
+ end
+ @proc_stack.push(pr)
+ else
+ if @rss.nil? and respond_to?("initial_start_#{local}", true)
+ __send__("initial_start_#{local}", local, prefix, attrs, ns.dup)
+ elsif respond_to?("start_#{local}", true)
+ __send__("start_#{local}", local, prefix, attrs, ns.dup)
+ else
+ start_else_element(local, prefix, attrs, ns.dup)
+ end
+ end
+ end
+
+ def tag_end(name)
+ if DEBUG
+ p "end tag #{name}"
+ p @tag_stack
+ end
+ text = @text_stack.pop
+ tags = @tag_stack.pop
+ pr = @proc_stack.pop
+ pr.call(text, tags) unless pr.nil?
+ @ns_stack.pop
+ end
+
+ def text(data)
+ if @xml_child_mode
+ @last_xml_element << data if @last_xml_element
+ else
+ @text_stack.last << data
+ end
+ end
+
+ private
+ def _ns(ns, prefix)
+ ns.fetch(prefix, "")
+ end
+
+ CONTENT_PATTERN = /\s*([^=]+)=(["'])([^\2]+?)\2/
+ # Extract the first name="value" pair from content.
+ # Works with single quotes according to the constant
+ # CONTENT_PATTERN. Return a Hash.
+ def parse_pi_content(content)
+ params = {}
+ content.scan(CONTENT_PATTERN) do |name, quote, value|
+ params[name] = value
+ end
+ params
+ end
+
+ def start_else_element(local, prefix, attrs, ns)
+ class_name = self.class.class_name(_ns(ns, prefix), local)
+ current_class = @last_element.class
+ if known_class?(current_class, class_name)
+ next_class = current_class.const_get(class_name)
+ start_have_something_element(local, prefix, attrs, ns, next_class)
+ else
+ if !@do_validate or @ignore_unknown_element
+ @proc_stack.push(setup_next_element_in_unknown_element)
+ else
+ parent = "ROOT ELEMENT???"
+ if current_class.tag_name
+ parent = current_class.tag_name
+ end
+ raise NotExpectedTagError.new(local, _ns(ns, prefix), parent)
+ end
+ end
+ end
+
+ if Module.method(:const_defined?).arity == -1
+ def known_class?(target_class, class_name)
+ class_name and
+ (target_class.const_defined?(class_name, false) or
+ target_class.constants.include?(class_name.to_sym))
+ end
+ else
+ def known_class?(target_class, class_name)
+ class_name and
+ (target_class.const_defined?(class_name) or
+ target_class.constants.include?(class_name))
+ end
+ end
+
+ NAMESPLIT = /^(?:([\w:][-\w\d.]*):)?([\w:][-\w\d.]*)/
+ def split_name(name)
+ name =~ NAMESPLIT
+ [$1 || '', $2]
+ end
+
+ def check_ns(tag_name, prefix, ns, require_uri, ignore_unknown_element=nil)
+ if _ns(ns, prefix) == require_uri
+ true
+ else
+ if ignore_unknown_element.nil?
+ ignore_unknown_element = @ignore_unknown_element
+ end
+
+ if ignore_unknown_element
+ false
+ elsif @do_validate
+ raise NSError.new(tag_name, prefix, require_uri)
+ else
+ # Force bind required URI with prefix
+ @ns_stack.last[prefix] = require_uri
+ true
+ end
+ end
+ end
+
+ def start_get_text_element(tag_name, prefix, ns, required_uri)
+ pr = Proc.new do |text, tags|
+ setter = self.class.setter(required_uri, tag_name)
+ if @last_element.respond_to?(setter)
+ if @do_validate
+ getter = self.class.getter(required_uri, tag_name)
+ if @last_element.__send__(getter)
+ raise TooMuchTagError.new(tag_name, @last_element.tag_name)
+ end
+ end
+ @last_element.__send__(setter, text.to_s)
+ else
+ if @do_validate and !@ignore_unknown_element
+ raise NotExpectedTagError.new(tag_name, _ns(ns, prefix),
+ @last_element.tag_name)
+ end
+ end
+ end
+ @proc_stack.push(pr)
+ end
+
+ def start_have_something_element(tag_name, prefix, attrs, ns, klass)
+ if check_ns(tag_name, prefix, ns, klass.required_uri)
+ attributes = collect_attributes(tag_name, prefix, attrs, ns, klass)
+ @proc_stack.push(setup_next_element(tag_name, klass, attributes))
+ else
+ @proc_stack.push(setup_next_element_in_unknown_element)
+ end
+ end
+
+ def collect_attributes(tag_name, prefix, attrs, ns, klass)
+ attributes = {}
+ klass.get_attributes.each do |a_name, a_uri, required, element_name|
+ if a_uri.is_a?(String) or !a_uri.respond_to?(:include?)
+ a_uri = [a_uri]
+ end
+ unless a_uri == [""]
+ for prefix, uri in ns
+ if a_uri.include?(uri)
+ val = attrs["#{prefix}:#{a_name}"]
+ break if val
+ end
+ end
+ end
+ if val.nil? and a_uri.include?("")
+ val = attrs[a_name]
+ end
+
+ if @do_validate and required and val.nil?
+ unless a_uri.include?("")
+ for prefix, uri in ns
+ if a_uri.include?(uri)
+ a_name = "#{prefix}:#{a_name}"
+ end
+ end
+ end
+ raise MissingAttributeError.new(tag_name, a_name)
+ end
+
+ attributes[a_name] = val
+ end
+ attributes
+ end
+
+ def setup_next_element(tag_name, klass, attributes)
+ previous = @last_element
+ next_element = klass.new(@do_validate, attributes)
+ previous.set_next_element(tag_name, next_element)
+ @last_element = next_element
+ @last_element.parent = previous if klass.need_parent?
+ @xml_child_mode = @last_element.have_xml_content?
+
+ Proc.new do |text, tags|
+ p(@last_element.class) if DEBUG
+ if @xml_child_mode
+ @last_element.content = @xml_element.to_s
+ xml_setter = @last_element.class.xml_setter
+ @last_element.__send__(xml_setter, @xml_element)
+ @xml_element = nil
+ @xml_child_mode = false
+ else
+ if klass.have_content?
+ if @last_element.need_base64_encode?
+ text = text.lstrip.unpack("m").first
+ end
+ @last_element.content = text
+ end
+ end
+ if @do_validate
+ @last_element.validate_for_stream(tags, @ignore_unknown_element)
+ end
+ @last_element = previous
+ end
+ end
+
+ def setup_next_element_in_unknown_element
+ current_element, @last_element = @last_element, nil
+ Proc.new {@last_element = current_element}
+ end
+ end
+
+ unless const_defined? :AVAILABLE_PARSER_LIBRARIES
+ AVAILABLE_PARSER_LIBRARIES = [
+ ["rss/xmlparser", :XMLParserParser],
+ ["rss/xmlscanner", :XMLScanParser],
+ ["rss/rexmlparser", :REXMLParser],
+ ]
+ end
+
+ AVAILABLE_PARSERS = []
+
+ AVAILABLE_PARSER_LIBRARIES.each do |lib, parser|
+ begin
+ require lib
+ AVAILABLE_PARSERS.push(const_get(parser))
+ rescue LoadError
+ end
+ end
+
+ if AVAILABLE_PARSERS.empty?
+ raise XMLParserNotFound
+ end
+end
diff --git a/ruby/lib/rss/rexmlparser.rb b/ruby/lib/rss/rexmlparser.rb
new file mode 100644
index 0000000..4dabf59
--- /dev/null
+++ b/ruby/lib/rss/rexmlparser.rb
@@ -0,0 +1,54 @@
+require "rexml/document"
+require "rexml/streamlistener"
+
+/\A(\d+)\.(\d+)(?:\.\d+)+\z/ =~ REXML::Version
+if ([$1.to_i, $2.to_i] <=> [2, 5]) < 0
+ raise LoadError, "needs REXML 2.5 or later (#{REXML::Version})"
+end
+
+module RSS
+
+ class REXMLParser < BaseParser
+
+ class << self
+ def listener
+ REXMLListener
+ end
+ end
+
+ private
+ def _parse
+ begin
+ REXML::Document.parse_stream(@rss, @listener)
+ rescue RuntimeError => e
+ raise NotWellFormedError.new{e.message}
+ rescue REXML::ParseException => e
+ context = e.context
+ line = context[0] if context
+ raise NotWellFormedError.new(line){e.message}
+ end
+ end
+
+ end
+
+ class REXMLListener < BaseListener
+
+ include REXML::StreamListener
+ include ListenerMixin
+
+ class << self
+ def raise_for_undefined_entity?
+ false
+ end
+ end
+
+ def xmldecl(version, encoding, standalone)
+ super(version, encoding, standalone == "yes")
+ # Encoding is converted to UTF-8 when REXML parse XML.
+ @encoding = 'UTF-8'
+ end
+
+ alias_method(:cdata, :text)
+ end
+
+end
diff --git a/ruby/lib/rss/rss.rb b/ruby/lib/rss/rss.rb
new file mode 100644
index 0000000..4b943ec
--- /dev/null
+++ b/ruby/lib/rss/rss.rb
@@ -0,0 +1,1313 @@
+require "time"
+
+class Time
+ class << self
+ unless respond_to?(:w3cdtf)
+ def w3cdtf(date)
+ if /\A\s*
+ (-?\d+)-(\d\d)-(\d\d)
+ (?:T
+ (\d\d):(\d\d)(?::(\d\d))?
+ (\.\d+)?
+ (Z|[+-]\d\d:\d\d)?)?
+ \s*\z/ix =~ date and (($5 and $8) or (!$5 and !$8))
+ datetime = [$1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i]
+ usec = 0
+ usec = $7.to_f * 1000000 if $7
+ zone = $8
+ if zone
+ off = zone_offset(zone, datetime[0])
+ datetime = apply_offset(*(datetime + [off]))
+ datetime << usec
+ time = Time.utc(*datetime)
+ time.localtime unless zone_utc?(zone)
+ time
+ else
+ datetime << usec
+ Time.local(*datetime)
+ end
+ else
+ raise ArgumentError.new("invalid date: #{date.inspect}")
+ end
+ end
+ end
+ end
+
+ unless method_defined?(:w3cdtf)
+ def w3cdtf
+ if usec.zero?
+ fraction_digits = 0
+ else
+ fraction_digits = Math.log10(usec.to_s.sub(/0*$/, '').to_i).floor + 1
+ end
+ xmlschema(fraction_digits)
+ end
+ end
+end
+
+
+require "English"
+require "rss/utils"
+require "rss/converter"
+require "rss/xml-stylesheet"
+
+module RSS
+
+ VERSION = "0.2.5"
+
+ URI = "http://purl.org/rss/1.0/"
+
+ DEBUG = false
+
+ class Error < StandardError; end
+
+ class OverlappedPrefixError < Error
+ attr_reader :prefix
+ def initialize(prefix)
+ @prefix = prefix
+ end
+ end
+
+ class InvalidRSSError < Error; end
+
+ class MissingTagError < InvalidRSSError
+ attr_reader :tag, :parent
+ def initialize(tag, parent)
+ @tag, @parent = tag, parent
+ super("tag <#{tag}> is missing in tag <#{parent}>")
+ end
+ end
+
+ class TooMuchTagError < InvalidRSSError
+ attr_reader :tag, :parent
+ def initialize(tag, parent)
+ @tag, @parent = tag, parent
+ super("tag <#{tag}> is too much in tag <#{parent}>")
+ end
+ end
+
+ class MissingAttributeError < InvalidRSSError
+ attr_reader :tag, :attribute
+ def initialize(tag, attribute)
+ @tag, @attribute = tag, attribute
+ super("attribute <#{attribute}> is missing in tag <#{tag}>")
+ end
+ end
+
+ class UnknownTagError < InvalidRSSError
+ attr_reader :tag, :uri
+ def initialize(tag, uri)
+ @tag, @uri = tag, uri
+ super("tag <#{tag}> is unknown in namespace specified by uri <#{uri}>")
+ end
+ end
+
+ class NotExpectedTagError < InvalidRSSError
+ attr_reader :tag, :uri, :parent
+ def initialize(tag, uri, parent)
+ @tag, @uri, @parent = tag, uri, parent
+ super("tag <{#{uri}}#{tag}> is not expected in tag <#{parent}>")
+ end
+ end
+ # For backward compatibility :X
+ NotExceptedTagError = NotExpectedTagError
+
+ class NotAvailableValueError < InvalidRSSError
+ attr_reader :tag, :value, :attribute
+ def initialize(tag, value, attribute=nil)
+ @tag, @value, @attribute = tag, value, attribute
+ message = "value <#{value}> of "
+ message << "attribute <#{attribute}> of " if attribute
+ message << "tag <#{tag}> is not available."
+ super(message)
+ end
+ end
+
+ class UnknownConversionMethodError < Error
+ attr_reader :to, :from
+ def initialize(to, from)
+ @to = to
+ @from = from
+ super("can't convert to #{to} from #{from}.")
+ end
+ end
+ # for backward compatibility
+ UnknownConvertMethod = UnknownConversionMethodError
+
+ class ConversionError < Error
+ attr_reader :string, :to, :from
+ def initialize(string, to, from)
+ @string = string
+ @to = to
+ @from = from
+ super("can't convert #{@string} to #{to} from #{from}.")
+ end
+ end
+
+ class NotSetError < Error
+ attr_reader :name, :variables
+ def initialize(name, variables)
+ @name = name
+ @variables = variables
+ super("required variables of #{@name} are not set: #{@variables.join(', ')}")
+ end
+ end
+
+ class UnsupportedMakerVersionError < Error
+ attr_reader :version
+ def initialize(version)
+ @version = version
+ super("Maker doesn't support version: #{@version}")
+ end
+ end
+
+ module BaseModel
+ include Utils
+
+ def install_have_child_element(tag_name, uri, occurs, name=nil, type=nil)
+ name ||= tag_name
+ add_need_initialize_variable(name)
+ install_model(tag_name, uri, occurs, name)
+
+ writer_type, reader_type = type
+ def_corresponded_attr_writer name, writer_type
+ def_corresponded_attr_reader name, reader_type
+ install_element(name) do |n, elem_name|
+ <<-EOC
+ if @#{n}
+ "\#{@#{n}.to_s(need_convert, indent)}"
+ else
+ ''
+ end
+EOC
+ end
+ end
+ alias_method(:install_have_attribute_element, :install_have_child_element)
+
+ def install_have_children_element(tag_name, uri, occurs, name=nil, plural_name=nil)
+ name ||= tag_name
+ plural_name ||= "#{name}s"
+ add_have_children_element(name, plural_name)
+ add_plural_form(name, plural_name)
+ install_model(tag_name, uri, occurs, plural_name, true)
+
+ def_children_accessor(name, plural_name)
+ install_element(name, "s") do |n, elem_name|
+ <<-EOC
+ rv = []
+ @#{n}.each do |x|
+ value = "\#{x.to_s(need_convert, indent)}"
+ rv << value if /\\A\\s*\\z/ !~ value
+ end
+ rv.join("\n")
+EOC
+ end
+ end
+
+ def install_text_element(tag_name, uri, occurs, name=nil, type=nil,
+ disp_name=nil)
+ name ||= tag_name
+ disp_name ||= name
+ self::ELEMENTS << name unless self::ELEMENTS.include?(name)
+ add_need_initialize_variable(name)
+ install_model(tag_name, uri, occurs, name)
+
+ def_corresponded_attr_writer(name, type, disp_name)
+ def_corresponded_attr_reader(name, type || :convert)
+ install_element(name) do |n, elem_name|
+ <<-EOC
+ if respond_to?(:#{n}_content)
+ content = #{n}_content
+ else
+ content = @#{n}
+ end
+ if content
+ rv = "\#{indent}<#{elem_name}>"
+ value = html_escape(content)
+ if need_convert
+ rv << convert(value)
+ else
+ rv << value
+ end
+ rv << "</#{elem_name}>"
+ rv
+ else
+ ''
+ end
+EOC
+ end
+ end
+
+ def install_date_element(tag_name, uri, occurs, name=nil, type=nil, disp_name=nil)
+ name ||= tag_name
+ type ||= :w3cdtf
+ disp_name ||= name
+ self::ELEMENTS << name
+ add_need_initialize_variable(name)
+ install_model(tag_name, uri, occurs, name)
+
+ # accessor
+ convert_attr_reader name
+ date_writer(name, type, disp_name)
+
+ install_element(name) do |n, elem_name|
+ <<-EOC
+ if @#{n}
+ rv = "\#{indent}<#{elem_name}>"
+ value = html_escape(@#{n}.#{type})
+ if need_convert
+ rv << convert(value)
+ else
+ rv << value
+ end
+ rv << "</#{elem_name}>"
+ rv
+ else
+ ''
+ end
+EOC
+ end
+
+ end
+
+ private
+ def install_element(name, postfix="")
+ elem_name = name.sub('_', ':')
+ method_name = "#{name}_element#{postfix}"
+ add_to_element_method(method_name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{method_name}(need_convert=true, indent='')
+ #{yield(name, elem_name)}
+ end
+ private :#{method_name}
+EOC
+ end
+
+ def inherit_convert_attr_reader(*attrs)
+ attrs.each do |attr|
+ attr = attr.id2name if attr.kind_of?(Integer)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{attr}_without_inherit
+ convert(@#{attr})
+ end
+
+ def #{attr}
+ if @#{attr}
+ #{attr}_without_inherit
+ elsif @parent
+ @parent.#{attr}
+ else
+ nil
+ end
+ end
+EOC
+ end
+ end
+
+ def uri_convert_attr_reader(*attrs)
+ attrs.each do |attr|
+ attr = attr.id2name if attr.kind_of?(Integer)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{attr}_without_base
+ convert(@#{attr})
+ end
+
+ def #{attr}
+ value = #{attr}_without_base
+ return nil if value.nil?
+ if /\\A[a-z][a-z0-9+.\\-]*:/i =~ value
+ value
+ else
+ "\#{base}\#{value}"
+ end
+ end
+EOC
+ end
+ end
+
+ def convert_attr_reader(*attrs)
+ attrs.each do |attr|
+ attr = attr.id2name if attr.kind_of?(Integer)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{attr}
+ convert(@#{attr})
+ end
+EOC
+ end
+ end
+
+ def yes_clean_other_attr_reader(*attrs)
+ attrs.each do |attr|
+ attr = attr.id2name if attr.kind_of?(Integer)
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ attr_reader(:#{attr})
+ def #{attr}?
+ YesCleanOther.parse(@#{attr})
+ end
+ EOC
+ end
+ end
+
+ def yes_other_attr_reader(*attrs)
+ attrs.each do |attr|
+ attr = attr.id2name if attr.kind_of?(Integer)
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ attr_reader(:#{attr})
+ def #{attr}?
+ Utils::YesOther.parse(@#{attr})
+ end
+ EOC
+ end
+ end
+
+ def csv_attr_reader(*attrs)
+ separator = nil
+ if attrs.last.is_a?(Hash)
+ options = attrs.pop
+ separator = options[:separator]
+ end
+ separator ||= ", "
+ attrs.each do |attr|
+ attr = attr.id2name if attr.kind_of?(Integer)
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ attr_reader(:#{attr})
+ def #{attr}_content
+ if @#{attr}.nil?
+ @#{attr}
+ else
+ @#{attr}.join(#{separator.dump})
+ end
+ end
+ EOC
+ end
+ end
+
+ def date_writer(name, type, disp_name=name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if new_value.nil?
+ @#{name} = new_value
+ elsif new_value.kind_of?(Time)
+ @#{name} = new_value.dup
+ else
+ if @do_validate
+ begin
+ @#{name} = Time.__send__('#{type}', new_value)
+ rescue ArgumentError
+ raise NotAvailableValueError.new('#{disp_name}', new_value)
+ end
+ else
+ @#{name} = nil
+ if /\\A\\s*\\z/ !~ new_value.to_s
+ begin
+ unless Date._parse(new_value, false).empty?
+ @#{name} = Time.parse(new_value)
+ end
+ rescue ArgumentError
+ end
+ end
+ end
+ end
+
+ # Is it need?
+ if @#{name}
+ class << @#{name}
+ undef_method(:to_s)
+ alias_method(:to_s, :#{type})
+ end
+ end
+
+ end
+EOC
+ end
+
+ def integer_writer(name, disp_name=name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if new_value.nil?
+ @#{name} = new_value
+ else
+ if @do_validate
+ begin
+ @#{name} = Integer(new_value)
+ rescue ArgumentError
+ raise NotAvailableValueError.new('#{disp_name}', new_value)
+ end
+ else
+ @#{name} = new_value.to_i
+ end
+ end
+ end
+EOC
+ end
+
+ def positive_integer_writer(name, disp_name=name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if new_value.nil?
+ @#{name} = new_value
+ else
+ if @do_validate
+ begin
+ tmp = Integer(new_value)
+ raise ArgumentError if tmp <= 0
+ @#{name} = tmp
+ rescue ArgumentError
+ raise NotAvailableValueError.new('#{disp_name}', new_value)
+ end
+ else
+ @#{name} = new_value.to_i
+ end
+ end
+ end
+EOC
+ end
+
+ def boolean_writer(name, disp_name=name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if new_value.nil?
+ @#{name} = new_value
+ else
+ if @do_validate and
+ ![true, false, "true", "false"].include?(new_value)
+ raise NotAvailableValueError.new('#{disp_name}', new_value)
+ end
+ if [true, false].include?(new_value)
+ @#{name} = new_value
+ else
+ @#{name} = new_value == "true"
+ end
+ end
+ end
+EOC
+ end
+
+ def text_type_writer(name, disp_name=name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if @do_validate and
+ !["text", "html", "xhtml", nil].include?(new_value)
+ raise NotAvailableValueError.new('#{disp_name}', new_value)
+ end
+ @#{name} = new_value
+ end
+EOC
+ end
+
+ def content_writer(name, disp_name=name)
+ klass_name = "self.class::#{Utils.to_class_name(name)}"
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if new_value.is_a?(#{klass_name})
+ @#{name} = new_value
+ else
+ @#{name} = #{klass_name}.new
+ @#{name}.content = new_value
+ end
+ end
+EOC
+ end
+
+ def yes_clean_other_writer(name, disp_name=name)
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{name}=(value)
+ value = (value ? "yes" : "no") if [true, false].include?(value)
+ @#{name} = value
+ end
+ EOC
+ end
+
+ def yes_other_writer(name, disp_name=name)
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{name}=(new_value)
+ if [true, false].include?(new_value)
+ new_value = new_value ? "yes" : "no"
+ end
+ @#{name} = new_value
+ end
+ EOC
+ end
+
+ def csv_writer(name, disp_name=name)
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{name}=(new_value)
+ @#{name} = Utils::CSV.parse(new_value)
+ end
+ EOC
+ end
+
+ def csv_integer_writer(name, disp_name=name)
+ module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ def #{name}=(new_value)
+ @#{name} = Utils::CSV.parse(new_value) {|v| Integer(v)}
+ end
+ EOC
+ end
+
+ def def_children_accessor(accessor_name, plural_name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{plural_name}
+ @#{accessor_name}
+ end
+
+ def #{accessor_name}(*args)
+ if args.empty?
+ @#{accessor_name}.first
+ else
+ @#{accessor_name}[*args]
+ end
+ end
+
+ def #{accessor_name}=(*args)
+ receiver = self.class.name
+ warn("Warning:\#{caller.first.sub(/:in `.*'\z/, '')}: " \
+ "Don't use `\#{receiver}\##{accessor_name} = XXX'/" \
+ "`\#{receiver}\#set_#{accessor_name}(XXX)'. " \
+ "Those APIs are not sense of Ruby. " \
+ "Use `\#{receiver}\##{plural_name} << XXX' instead of them.")
+ if args.size == 1
+ @#{accessor_name}.push(args[0])
+ else
+ @#{accessor_name}.__send__("[]=", *args)
+ end
+ end
+ alias_method(:set_#{accessor_name}, :#{accessor_name}=)
+EOC
+ end
+ end
+
+ module SetupMaker
+ def setup_maker(maker)
+ target = maker_target(maker)
+ unless target.nil?
+ setup_maker_attributes(target)
+ setup_maker_element(target)
+ setup_maker_elements(target)
+ end
+ end
+
+ private
+ def maker_target(maker)
+ nil
+ end
+
+ def setup_maker_attributes(target)
+ end
+
+ def setup_maker_element(target)
+ self.class.need_initialize_variables.each do |var|
+ value = __send__(var)
+ next if value.nil?
+ if value.respond_to?("setup_maker") and
+ !not_need_to_call_setup_maker_variables.include?(var)
+ value.setup_maker(target)
+ else
+ setter = "#{var}="
+ if target.respond_to?(setter)
+ target.__send__(setter, value)
+ end
+ end
+ end
+ end
+
+ def not_need_to_call_setup_maker_variables
+ []
+ end
+
+ def setup_maker_elements(parent)
+ self.class.have_children_elements.each do |name, plural_name|
+ if parent.respond_to?(plural_name)
+ target = parent.__send__(plural_name)
+ __send__(plural_name).each do |elem|
+ elem.setup_maker(target)
+ end
+ end
+ end
+ end
+ end
+
+ class Element
+ extend BaseModel
+ include Utils
+ extend Utils::InheritedReader
+ include SetupMaker
+
+ INDENT = " "
+
+ MUST_CALL_VALIDATORS = {}
+ MODELS = []
+ GET_ATTRIBUTES = []
+ HAVE_CHILDREN_ELEMENTS = []
+ TO_ELEMENT_METHODS = []
+ NEED_INITIALIZE_VARIABLES = []
+ PLURAL_FORMS = {}
+
+ class << self
+ def must_call_validators
+ inherited_hash_reader("MUST_CALL_VALIDATORS")
+ end
+ def models
+ inherited_array_reader("MODELS")
+ end
+ def get_attributes
+ inherited_array_reader("GET_ATTRIBUTES")
+ end
+ def have_children_elements
+ inherited_array_reader("HAVE_CHILDREN_ELEMENTS")
+ end
+ def to_element_methods
+ inherited_array_reader("TO_ELEMENT_METHODS")
+ end
+ def need_initialize_variables
+ inherited_array_reader("NEED_INITIALIZE_VARIABLES")
+ end
+ def plural_forms
+ inherited_hash_reader("PLURAL_FORMS")
+ end
+
+ def inherited_base
+ ::RSS::Element
+ end
+
+ def inherited(klass)
+ klass.const_set("MUST_CALL_VALIDATORS", {})
+ klass.const_set("MODELS", [])
+ klass.const_set("GET_ATTRIBUTES", [])
+ klass.const_set("HAVE_CHILDREN_ELEMENTS", [])
+ klass.const_set("TO_ELEMENT_METHODS", [])
+ klass.const_set("NEED_INITIALIZE_VARIABLES", [])
+ klass.const_set("PLURAL_FORMS", {})
+
+ tag_name = klass.name.split(/::/).last
+ tag_name[0, 1] = tag_name[0, 1].downcase
+ klass.instance_variable_set("@tag_name", tag_name)
+ klass.instance_variable_set("@have_content", false)
+ end
+
+ def install_must_call_validator(prefix, uri)
+ self::MUST_CALL_VALIDATORS[uri] = prefix
+ end
+
+ def install_model(tag, uri, occurs=nil, getter=nil, plural=false)
+ getter ||= tag
+ if m = self::MODELS.find {|t, u, o, g, p| t == tag and u == uri}
+ m[2] = occurs
+ else
+ self::MODELS << [tag, uri, occurs, getter, plural]
+ end
+ end
+
+ def install_get_attribute(name, uri, required=true,
+ type=nil, disp_name=nil,
+ element_name=nil)
+ disp_name ||= name
+ element_name ||= name
+ writer_type, reader_type = type
+ def_corresponded_attr_writer name, writer_type, disp_name
+ def_corresponded_attr_reader name, reader_type
+ if type == :boolean and /^is/ =~ name
+ alias_method "#{$POSTMATCH}?", name
+ end
+ self::GET_ATTRIBUTES << [name, uri, required, element_name]
+ add_need_initialize_variable(disp_name)
+ end
+
+ def def_corresponded_attr_writer(name, type=nil, disp_name=nil)
+ disp_name ||= name
+ case type
+ when :integer
+ integer_writer name, disp_name
+ when :positive_integer
+ positive_integer_writer name, disp_name
+ when :boolean
+ boolean_writer name, disp_name
+ when :w3cdtf, :rfc822, :rfc2822
+ date_writer name, type, disp_name
+ when :text_type
+ text_type_writer name, disp_name
+ when :content
+ content_writer name, disp_name
+ when :yes_clean_other
+ yes_clean_other_writer name, disp_name
+ when :yes_other
+ yes_other_writer name, disp_name
+ when :csv
+ csv_writer name
+ when :csv_integer
+ csv_integer_writer name
+ else
+ attr_writer name
+ end
+ end
+
+ def def_corresponded_attr_reader(name, type=nil)
+ case type
+ when :inherit
+ inherit_convert_attr_reader name
+ when :uri
+ uri_convert_attr_reader name
+ when :yes_clean_other
+ yes_clean_other_attr_reader name
+ when :yes_other
+ yes_other_attr_reader name
+ when :csv
+ csv_attr_reader name
+ when :csv_integer
+ csv_attr_reader name, :separator => ","
+ else
+ convert_attr_reader name
+ end
+ end
+
+ def content_setup(type=nil, disp_name=nil)
+ writer_type, reader_type = type
+ def_corresponded_attr_writer :content, writer_type, disp_name
+ def_corresponded_attr_reader :content, reader_type
+ @have_content = true
+ end
+
+ def have_content?
+ @have_content
+ end
+
+ def add_have_children_element(variable_name, plural_name)
+ self::HAVE_CHILDREN_ELEMENTS << [variable_name, plural_name]
+ end
+
+ def add_to_element_method(method_name)
+ self::TO_ELEMENT_METHODS << method_name
+ end
+
+ def add_need_initialize_variable(variable_name)
+ self::NEED_INITIALIZE_VARIABLES << variable_name
+ end
+
+ def add_plural_form(singular, plural)
+ self::PLURAL_FORMS[singular] = plural
+ end
+
+ def required_prefix
+ nil
+ end
+
+ def required_uri
+ ""
+ end
+
+ def need_parent?
+ false
+ end
+
+ def install_ns(prefix, uri)
+ if self::NSPOOL.has_key?(prefix)
+ raise OverlappedPrefixError.new(prefix)
+ end
+ self::NSPOOL[prefix] = uri
+ end
+
+ def tag_name
+ @tag_name
+ end
+ end
+
+ attr_accessor :parent, :do_validate
+
+ def initialize(do_validate=true, attrs=nil)
+ @parent = nil
+ @converter = nil
+ if attrs.nil? and (do_validate.is_a?(Hash) or do_validate.is_a?(Array))
+ do_validate, attrs = true, do_validate
+ end
+ @do_validate = do_validate
+ initialize_variables(attrs || {})
+ end
+
+ def tag_name
+ self.class.tag_name
+ end
+
+ def full_name
+ tag_name
+ end
+
+ def converter=(converter)
+ @converter = converter
+ targets = children.dup
+ self.class.have_children_elements.each do |variable_name, plural_name|
+ targets.concat(__send__(plural_name))
+ end
+ targets.each do |target|
+ target.converter = converter unless target.nil?
+ end
+ end
+
+ def convert(value)
+ if @converter
+ @converter.convert(value)
+ else
+ value
+ end
+ end
+
+ def valid?(ignore_unknown_element=true)
+ validate(ignore_unknown_element)
+ true
+ rescue RSS::Error
+ false
+ end
+
+ def validate(ignore_unknown_element=true)
+ do_validate = @do_validate
+ @do_validate = true
+ validate_attribute
+ __validate(ignore_unknown_element)
+ ensure
+ @do_validate = do_validate
+ end
+
+ def validate_for_stream(tags, ignore_unknown_element=true)
+ validate_attribute
+ __validate(ignore_unknown_element, tags, false)
+ end
+
+ def to_s(need_convert=true, indent='')
+ if self.class.have_content?
+ return "" if !empty_content? and !content_is_set?
+ rv = tag(indent) do |next_indent|
+ if empty_content?
+ ""
+ else
+ xmled_content
+ end
+ end
+ else
+ rv = tag(indent) do |next_indent|
+ self.class.to_element_methods.collect do |method_name|
+ __send__(method_name, false, next_indent)
+ end
+ end
+ end
+ rv = convert(rv) if need_convert
+ rv
+ end
+
+ def have_xml_content?
+ false
+ end
+
+ def need_base64_encode?
+ false
+ end
+
+ def set_next_element(tag_name, next_element)
+ klass = next_element.class
+ prefix = ""
+ prefix << "#{klass.required_prefix}_" if klass.required_prefix
+ key = "#{prefix}#{tag_name.gsub(/-/, '_')}"
+ if self.class.plural_forms.has_key?(key)
+ ary = __send__("#{self.class.plural_forms[key]}")
+ ary << next_element
+ else
+ __send__("#{key}=", next_element)
+ end
+ end
+
+ protected
+ def have_required_elements?
+ self.class::MODELS.all? do |tag, uri, occurs, getter|
+ if occurs.nil? or occurs == "+"
+ child = __send__(getter)
+ if child.is_a?(Array)
+ children = child
+ children.any? {|c| c.have_required_elements?}
+ else
+ !child.to_s.empty?
+ end
+ else
+ true
+ end
+ end
+ end
+
+ private
+ def initialize_variables(attrs)
+ normalized_attrs = {}
+ attrs.each do |key, value|
+ normalized_attrs[key.to_s] = value
+ end
+ self.class.need_initialize_variables.each do |variable_name|
+ value = normalized_attrs[variable_name.to_s]
+ if value
+ __send__("#{variable_name}=", value)
+ else
+ instance_variable_set("@#{variable_name}", nil)
+ end
+ end
+ initialize_have_children_elements
+ @content = normalized_attrs["content"] if self.class.have_content?
+ end
+
+ def initialize_have_children_elements
+ self.class.have_children_elements.each do |variable_name, plural_name|
+ instance_variable_set("@#{variable_name}", [])
+ end
+ end
+
+ def tag(indent, additional_attrs={}, &block)
+ next_indent = indent + INDENT
+
+ attrs = collect_attrs
+ return "" if attrs.nil?
+
+ return "" unless have_required_elements?
+
+ attrs.update(additional_attrs)
+ start_tag = make_start_tag(indent, next_indent, attrs.dup)
+
+ if block
+ content = block.call(next_indent)
+ else
+ content = []
+ end
+
+ if content.is_a?(String)
+ content = [content]
+ start_tag << ">"
+ end_tag = "</#{full_name}>"
+ else
+ content = content.reject{|x| x.empty?}
+ if content.empty?
+ return "" if attrs.empty?
+ end_tag = "/>"
+ else
+ start_tag << ">\n"
+ end_tag = "\n#{indent}</#{full_name}>"
+ end
+ end
+
+ start_tag + content.join("\n") + end_tag
+ end
+
+ def make_start_tag(indent, next_indent, attrs)
+ start_tag = ["#{indent}<#{full_name}"]
+ unless attrs.empty?
+ start_tag << attrs.collect do |key, value|
+ %Q[#{h key}="#{h value}"]
+ end.join("\n#{next_indent}")
+ end
+ start_tag.join(" ")
+ end
+
+ def collect_attrs
+ attrs = {}
+ _attrs.each do |name, required, alias_name|
+ value = __send__(alias_name || name)
+ return nil if required and value.nil?
+ next if value.nil?
+ return nil if attrs.has_key?(name)
+ attrs[name] = value
+ end
+ attrs
+ end
+
+ def tag_name_with_prefix(prefix)
+ "#{prefix}:#{tag_name}"
+ end
+
+ # For backward compatibility
+ def calc_indent
+ ''
+ end
+
+ def children
+ rv = []
+ self.class.models.each do |name, uri, occurs, getter|
+ value = __send__(getter)
+ next if value.nil?
+ value = [value] unless value.is_a?(Array)
+ value.each do |v|
+ rv << v if v.is_a?(Element)
+ end
+ end
+ rv
+ end
+
+ def _tags
+ rv = []
+ self.class.models.each do |name, uri, occurs, getter, plural|
+ value = __send__(getter)
+ next if value.nil?
+ if plural and value.is_a?(Array)
+ rv.concat([[uri, name]] * value.size)
+ else
+ rv << [uri, name]
+ end
+ end
+ rv
+ end
+
+ def _attrs
+ self.class.get_attributes.collect do |name, uri, required, element_name|
+ [element_name, required, name]
+ end
+ end
+
+ def __validate(ignore_unknown_element, tags=_tags, recursive=true)
+ if recursive
+ children.compact.each do |child|
+ child.validate
+ end
+ end
+ must_call_validators = self.class.must_call_validators
+ tags = tag_filter(tags.dup)
+ p tags if DEBUG
+ must_call_validators.each do |uri, prefix|
+ _validate(ignore_unknown_element, tags[uri], uri)
+ meth = "#{prefix}_validate"
+ if !prefix.empty? and respond_to?(meth, true)
+ __send__(meth, ignore_unknown_element, tags[uri], uri)
+ end
+ end
+ end
+
+ def validate_attribute
+ _attrs.each do |a_name, required, alias_name|
+ value = instance_variable_get("@#{alias_name || a_name}")
+ if required and value.nil?
+ raise MissingAttributeError.new(tag_name, a_name)
+ end
+ __send__("#{alias_name || a_name}=", value)
+ end
+ end
+
+ def _validate(ignore_unknown_element, tags, uri, models=self.class.models)
+ count = 1
+ do_redo = false
+ not_shift = false
+ tag = nil
+ models = models.find_all {|model| model[1] == uri}
+ element_names = models.collect {|model| model[0]}
+ if tags
+ tags_size = tags.size
+ tags = tags.sort_by {|x| element_names.index(x) || tags_size}
+ end
+
+ _tags = tags.dup if tags
+ models.each_with_index do |model, i|
+ name, model_uri, occurs, getter = model
+
+ if DEBUG
+ p "before"
+ p tags
+ p model
+ end
+
+ if not_shift
+ not_shift = false
+ elsif tags
+ tag = tags.shift
+ end
+
+ if DEBUG
+ p "mid"
+ p count
+ end
+
+ case occurs
+ when '?'
+ if count > 2
+ raise TooMuchTagError.new(name, tag_name)
+ else
+ if name == tag
+ do_redo = true
+ else
+ not_shift = true
+ end
+ end
+ when '*'
+ if name == tag
+ do_redo = true
+ else
+ not_shift = true
+ end
+ when '+'
+ if name == tag
+ do_redo = true
+ else
+ if count > 1
+ not_shift = true
+ else
+ raise MissingTagError.new(name, tag_name)
+ end
+ end
+ else
+ if name == tag
+ if models[i+1] and models[i+1][0] != name and
+ tags and tags.first == name
+ raise TooMuchTagError.new(name, tag_name)
+ end
+ else
+ raise MissingTagError.new(name, tag_name)
+ end
+ end
+
+ if DEBUG
+ p "after"
+ p not_shift
+ p do_redo
+ p tag
+ end
+
+ if do_redo
+ do_redo = false
+ count += 1
+ redo
+ else
+ count = 1
+ end
+
+ end
+
+ if !ignore_unknown_element and !tags.nil? and !tags.empty?
+ raise NotExpectedTagError.new(tags.first, uri, tag_name)
+ end
+
+ end
+
+ def tag_filter(tags)
+ rv = {}
+ tags.each do |tag|
+ rv[tag[0]] = [] unless rv.has_key?(tag[0])
+ rv[tag[0]].push(tag[1])
+ end
+ rv
+ end
+
+ def empty_content?
+ false
+ end
+
+ def content_is_set?
+ if have_xml_content?
+ __send__(self.class.xml_getter)
+ else
+ content
+ end
+ end
+
+ def xmled_content
+ if have_xml_content?
+ __send__(self.class.xml_getter).to_s
+ else
+ _content = content
+ _content = [_content].pack("m").delete("\n") if need_base64_encode?
+ h(_content)
+ end
+ end
+ end
+
+ module RootElementMixin
+
+ include XMLStyleSheetMixin
+
+ attr_reader :output_encoding
+ attr_reader :feed_type, :feed_subtype, :feed_version
+ attr_accessor :version, :encoding, :standalone
+ def initialize(feed_version, version=nil, encoding=nil, standalone=nil)
+ super()
+ @feed_type = nil
+ @feed_subtype = nil
+ @feed_version = feed_version
+ @version = version || '1.0'
+ @encoding = encoding
+ @standalone = standalone
+ @output_encoding = nil
+ end
+
+ def feed_info
+ [@feed_type, @feed_version, @feed_subtype]
+ end
+
+ def output_encoding=(enc)
+ @output_encoding = enc
+ self.converter = Converter.new(@output_encoding, @encoding)
+ end
+
+ def setup_maker(maker)
+ maker.version = version
+ maker.encoding = encoding
+ maker.standalone = standalone
+
+ xml_stylesheets.each do |xss|
+ xss.setup_maker(maker)
+ end
+
+ super
+ end
+
+ def to_feed(type, &block)
+ Maker.make(type) do |maker|
+ setup_maker(maker)
+ block.call(maker) if block
+ end
+ end
+
+ def to_rss(type, &block)
+ to_feed("rss#{type}", &block)
+ end
+
+ def to_atom(type, &block)
+ to_feed("atom:#{type}", &block)
+ end
+
+ def to_xml(type=nil, &block)
+ if type.nil? or same_feed_type?(type)
+ to_s
+ else
+ to_feed(type, &block).to_s
+ end
+ end
+
+ private
+ def same_feed_type?(type)
+ if /^(atom|rss)?(\d+\.\d+)?(?::(.+))?$/i =~ type
+ feed_type = ($1 || @feed_type).downcase
+ feed_version = $2 || @feed_version
+ feed_subtype = $3 || @feed_subtype
+ [feed_type, feed_version, feed_subtype] == feed_info
+ else
+ false
+ end
+ end
+
+ def tag(indent, attrs={}, &block)
+ rv = super(indent, ns_declarations.merge(attrs), &block)
+ return rv if rv.empty?
+ "#{xmldecl}#{xml_stylesheet_pi}#{rv}"
+ end
+
+ def xmldecl
+ rv = %Q[<?xml version="#{@version}"]
+ if @output_encoding or @encoding
+ rv << %Q[ encoding="#{@output_encoding or @encoding}"]
+ end
+ rv << %Q[ standalone="yes"] if @standalone
+ rv << "?>\n"
+ rv
+ end
+
+ def ns_declarations
+ decls = {}
+ self.class::NSPOOL.collect do |prefix, uri|
+ prefix = ":#{prefix}" unless prefix.empty?
+ decls["xmlns#{prefix}"] = uri
+ end
+ decls
+ end
+
+ def maker_target(target)
+ target
+ end
+ end
+end
diff --git a/ruby/lib/rss/slash.rb b/ruby/lib/rss/slash.rb
new file mode 100644
index 0000000..f102413
--- /dev/null
+++ b/ruby/lib/rss/slash.rb
@@ -0,0 +1,49 @@
+require 'rss/1.0'
+
+module RSS
+ SLASH_PREFIX = 'slash'
+ SLASH_URI = "http://purl.org/rss/1.0/modules/slash/"
+
+ RDF.install_ns(SLASH_PREFIX, SLASH_URI)
+
+ module SlashModel
+ extend BaseModel
+
+ ELEMENT_INFOS = \
+ [
+ ["section"],
+ ["department"],
+ ["comments", :positive_integer],
+ ["hit_parade", :csv_integer],
+ ]
+
+ class << self
+ def append_features(klass)
+ super
+
+ return if klass.instance_of?(Module)
+ klass.install_must_call_validator(SLASH_PREFIX, SLASH_URI)
+ ELEMENT_INFOS.each do |name, type, *additional_infos|
+ full_name = "#{SLASH_PREFIX}_#{name}"
+ klass.install_text_element(full_name, SLASH_URI, "?",
+ full_name, type, name)
+ end
+
+ klass.module_eval do
+ alias_method(:slash_hit_parades, :slash_hit_parade)
+ undef_method(:slash_hit_parade)
+ alias_method(:slash_hit_parade, :slash_hit_parade_content)
+ end
+ end
+ end
+ end
+
+ class RDF
+ class Item; include SlashModel; end
+ end
+
+ SlashModel::ELEMENT_INFOS.each do |name, type|
+ accessor_base = "#{SLASH_PREFIX}_#{name}"
+ BaseListener.install_get_text_element(SLASH_URI, name, accessor_base)
+ end
+end
diff --git a/ruby/lib/rss/syndication.rb b/ruby/lib/rss/syndication.rb
new file mode 100644
index 0000000..3eb1542
--- /dev/null
+++ b/ruby/lib/rss/syndication.rb
@@ -0,0 +1,67 @@
+require "rss/1.0"
+
+module RSS
+
+ SY_PREFIX = 'sy'
+ SY_URI = "http://purl.org/rss/1.0/modules/syndication/"
+
+ RDF.install_ns(SY_PREFIX, SY_URI)
+
+ module SyndicationModel
+
+ extend BaseModel
+
+ ELEMENTS = []
+
+ def self.append_features(klass)
+ super
+
+ klass.install_must_call_validator(SY_PREFIX, SY_URI)
+ klass.module_eval do
+ [
+ ["updatePeriod"],
+ ["updateFrequency", :positive_integer]
+ ].each do |name, type|
+ install_text_element(name, SY_URI, "?",
+ "#{SY_PREFIX}_#{name}", type,
+ "#{SY_PREFIX}:#{name}")
+ end
+
+ %w(updateBase).each do |name|
+ install_date_element(name, SY_URI, "?",
+ "#{SY_PREFIX}_#{name}", 'w3cdtf',
+ "#{SY_PREFIX}:#{name}")
+ end
+ end
+
+ klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
+ alias_method(:_sy_updatePeriod=, :sy_updatePeriod=)
+ def sy_updatePeriod=(new_value)
+ new_value = new_value.strip
+ validate_sy_updatePeriod(new_value) if @do_validate
+ self._sy_updatePeriod = new_value
+ end
+ EOC
+ end
+
+ private
+ SY_UPDATEPERIOD_AVAILABLE_VALUES = %w(hourly daily weekly monthly yearly)
+ def validate_sy_updatePeriod(value)
+ unless SY_UPDATEPERIOD_AVAILABLE_VALUES.include?(value)
+ raise NotAvailableValueError.new("updatePeriod", value)
+ end
+ end
+ end
+
+ class RDF
+ class Channel; include SyndicationModel; end
+ end
+
+ prefix_size = SY_PREFIX.size + 1
+ SyndicationModel::ELEMENTS.uniq!
+ SyndicationModel::ELEMENTS.each do |full_name|
+ name = full_name[prefix_size..-1]
+ BaseListener.install_get_text_element(SY_URI, name, full_name)
+ end
+
+end
diff --git a/ruby/lib/rss/taxonomy.rb b/ruby/lib/rss/taxonomy.rb
new file mode 100644
index 0000000..276f63b
--- /dev/null
+++ b/ruby/lib/rss/taxonomy.rb
@@ -0,0 +1,145 @@
+require "rss/1.0"
+require "rss/dublincore"
+
+module RSS
+
+ TAXO_PREFIX = "taxo"
+ TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/"
+
+ RDF.install_ns(TAXO_PREFIX, TAXO_URI)
+
+ TAXO_ELEMENTS = []
+
+ %w(link).each do |name|
+ full_name = "#{TAXO_PREFIX}_#{name}"
+ BaseListener.install_get_text_element(TAXO_URI, name, full_name)
+ TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}"
+ end
+
+ %w(topic topics).each do |name|
+ class_name = Utils.to_class_name(name)
+ BaseListener.install_class_name(TAXO_URI, name, "Taxonomy#{class_name}")
+ TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}"
+ end
+
+ module TaxonomyTopicsModel
+ extend BaseModel
+
+ def self.append_features(klass)
+ super
+
+ klass.install_must_call_validator(TAXO_PREFIX, TAXO_URI)
+ %w(topics).each do |name|
+ klass.install_have_child_element(name, TAXO_URI, "?",
+ "#{TAXO_PREFIX}_#{name}")
+ end
+ end
+
+ class TaxonomyTopics < Element
+ include RSS10
+
+ Bag = ::RSS::RDF::Bag
+
+ class << self
+ def required_prefix
+ TAXO_PREFIX
+ end
+
+ def required_uri
+ TAXO_URI
+ end
+ end
+
+ @tag_name = "topics"
+
+ install_have_child_element("Bag", RDF::URI, nil)
+ install_must_call_validator('rdf', RDF::URI)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.Bag = args[0]
+ end
+ self.Bag ||= Bag.new
+ end
+
+ def full_name
+ tag_name_with_prefix(TAXO_PREFIX)
+ end
+
+ def maker_target(target)
+ target.taxo_topics
+ end
+
+ def resources
+ if @Bag
+ @Bag.lis.collect do |li|
+ li.resource
+ end
+ else
+ []
+ end
+ end
+ end
+ end
+
+ module TaxonomyTopicModel
+ extend BaseModel
+
+ def self.append_features(klass)
+ super
+ var_name = "#{TAXO_PREFIX}_topic"
+ klass.install_have_children_element("topic", TAXO_URI, "*", var_name)
+ end
+
+ class TaxonomyTopic < Element
+ include RSS10
+
+ include DublinCoreModel
+ include TaxonomyTopicsModel
+
+ class << self
+ def required_prefix
+ TAXO_PREFIX
+ end
+
+ def required_uri
+ TAXO_URI
+ end
+ end
+
+ @tag_name = "topic"
+
+ install_get_attribute("about", ::RSS::RDF::URI, true, nil, nil,
+ "#{RDF::PREFIX}:about")
+ install_text_element("link", TAXO_URI, "?", "#{TAXO_PREFIX}_link")
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.about = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(TAXO_PREFIX)
+ end
+
+ def maker_target(target)
+ target.new_taxo_topic
+ end
+ end
+ end
+
+ class RDF
+ include TaxonomyTopicModel
+ class Channel
+ include TaxonomyTopicsModel
+ end
+ class Item; include TaxonomyTopicsModel; end
+ end
+end
diff --git a/ruby/lib/rss/trackback.rb b/ruby/lib/rss/trackback.rb
new file mode 100644
index 0000000..ee2491f
--- /dev/null
+++ b/ruby/lib/rss/trackback.rb
@@ -0,0 +1,288 @@
+require 'rss/1.0'
+require 'rss/2.0'
+
+module RSS
+
+ TRACKBACK_PREFIX = 'trackback'
+ TRACKBACK_URI = 'http://madskills.com/public/xml/rss/module/trackback/'
+
+ RDF.install_ns(TRACKBACK_PREFIX, TRACKBACK_URI)
+ Rss.install_ns(TRACKBACK_PREFIX, TRACKBACK_URI)
+
+ module TrackBackUtils
+ private
+ def trackback_validate(ignore_unknown_element, tags, uri)
+ return if tags.nil?
+ if tags.find {|tag| tag == "about"} and
+ !tags.find {|tag| tag == "ping"}
+ raise MissingTagError.new("#{TRACKBACK_PREFIX}:ping", tag_name)
+ end
+ end
+ end
+
+ module BaseTrackBackModel
+
+ ELEMENTS = %w(ping about)
+
+ def append_features(klass)
+ super
+
+ unless klass.class == Module
+ klass.module_eval {include TrackBackUtils}
+
+ klass.install_must_call_validator(TRACKBACK_PREFIX, TRACKBACK_URI)
+ %w(ping).each do |name|
+ var_name = "#{TRACKBACK_PREFIX}_#{name}"
+ klass_name = "TrackBack#{Utils.to_class_name(name)}"
+ klass.install_have_child_element(name, TRACKBACK_URI, "?", var_name)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__)
+ remove_method :#{var_name}
+ def #{var_name}
+ @#{var_name} and @#{var_name}.value
+ end
+
+ remove_method :#{var_name}=
+ def #{var_name}=(value)
+ @#{var_name} = Utils.new_with_value_if_need(#{klass_name}, value)
+ end
+ EOC
+ end
+
+ [%w(about s)].each do |name, postfix|
+ var_name = "#{TRACKBACK_PREFIX}_#{name}"
+ klass_name = "TrackBack#{Utils.to_class_name(name)}"
+ klass.install_have_children_element(name, TRACKBACK_URI, "*",
+ var_name)
+ klass.module_eval(<<-EOC, __FILE__, __LINE__)
+ remove_method :#{var_name}
+ def #{var_name}(*args)
+ if args.empty?
+ @#{var_name}.first and @#{var_name}.first.value
+ else
+ ret = @#{var_name}.__send__("[]", *args)
+ if ret.is_a?(Array)
+ ret.collect {|x| x.value}
+ else
+ ret.value
+ end
+ end
+ end
+
+ remove_method :#{var_name}=
+ remove_method :set_#{var_name}
+ def #{var_name}=(*args)
+ if args.size == 1
+ item = Utils.new_with_value_if_need(#{klass_name}, args[0])
+ @#{var_name}.push(item)
+ else
+ new_val = args.last
+ if new_val.is_a?(Array)
+ new_val = new_value.collect do |val|
+ Utils.new_with_value_if_need(#{klass_name}, val)
+ end
+ else
+ new_val = Utils.new_with_value_if_need(#{klass_name}, new_val)
+ end
+ @#{var_name}.__send__("[]=", *(args[0..-2] + [new_val]))
+ end
+ end
+ alias set_#{var_name} #{var_name}=
+ EOC
+ end
+ end
+ end
+ end
+
+ module TrackBackModel10
+ extend BaseModel
+ extend BaseTrackBackModel
+
+ class TrackBackPing < Element
+ include RSS10
+
+ class << self
+
+ def required_prefix
+ TRACKBACK_PREFIX
+ end
+
+ def required_uri
+ TRACKBACK_URI
+ end
+
+ end
+
+ @tag_name = "ping"
+
+ [
+ ["resource", ::RSS::RDF::URI, true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{::RSS::RDF::PREFIX}:#{name}")
+ end
+
+ alias_method(:value, :resource)
+ alias_method(:value=, :resource=)
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.resource = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(TRACKBACK_PREFIX)
+ end
+ end
+
+ class TrackBackAbout < Element
+ include RSS10
+
+ class << self
+
+ def required_prefix
+ TRACKBACK_PREFIX
+ end
+
+ def required_uri
+ TRACKBACK_URI
+ end
+
+ end
+
+ @tag_name = "about"
+
+ [
+ ["resource", ::RSS::RDF::URI, true]
+ ].each do |name, uri, required|
+ install_get_attribute(name, uri, required, nil, nil,
+ "#{::RSS::RDF::PREFIX}:#{name}")
+ end
+
+ alias_method(:value, :resource)
+ alias_method(:value=, :resource=)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.resource = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(TRACKBACK_PREFIX)
+ end
+
+ private
+ def maker_target(abouts)
+ abouts.new_about
+ end
+
+ def setup_maker_attributes(about)
+ about.resource = self.resource
+ end
+
+ end
+ end
+
+ module TrackBackModel20
+ extend BaseModel
+ extend BaseTrackBackModel
+
+ class TrackBackPing < Element
+ include RSS09
+
+ @tag_name = "ping"
+
+ content_setup
+
+ class << self
+
+ def required_prefix
+ TRACKBACK_PREFIX
+ end
+
+ def required_uri
+ TRACKBACK_URI
+ end
+
+ end
+
+ alias_method(:value, :content)
+ alias_method(:value=, :content=)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.content = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(TRACKBACK_PREFIX)
+ end
+
+ end
+
+ class TrackBackAbout < Element
+ include RSS09
+
+ @tag_name = "about"
+
+ content_setup
+
+ class << self
+
+ def required_prefix
+ TRACKBACK_PREFIX
+ end
+
+ def required_uri
+ TRACKBACK_URI
+ end
+
+ end
+
+ alias_method(:value, :content)
+ alias_method(:value=, :content=)
+
+ def initialize(*args)
+ if Utils.element_initialize_arguments?(args)
+ super
+ else
+ super()
+ self.content = args[0]
+ end
+ end
+
+ def full_name
+ tag_name_with_prefix(TRACKBACK_PREFIX)
+ end
+
+ end
+ end
+
+ class RDF
+ class Item; include TrackBackModel10; end
+ end
+
+ class Rss
+ class Channel
+ class Item; include TrackBackModel20; end
+ end
+ end
+
+ BaseTrackBackModel::ELEMENTS.each do |name|
+ class_name = Utils.to_class_name(name)
+ BaseListener.install_class_name(TRACKBACK_URI, name,
+ "TrackBack#{class_name}")
+ end
+
+ BaseTrackBackModel::ELEMENTS.collect! {|name| "#{TRACKBACK_PREFIX}_#{name}"}
+end
diff --git a/ruby/lib/rss/utils.rb b/ruby/lib/rss/utils.rb
new file mode 100644
index 0000000..0e4001e
--- /dev/null
+++ b/ruby/lib/rss/utils.rb
@@ -0,0 +1,111 @@
+module RSS
+ module Utils
+ module_function
+
+ # Convert a name_with_underscores to CamelCase.
+ def to_class_name(name)
+ name.split(/[_\-]/).collect do |part|
+ "#{part[0, 1].upcase}#{part[1..-1]}"
+ end.join("")
+ end
+
+ def get_file_and_line_from_caller(i=0)
+ file, line, = caller[i].split(':')
+ line = line.to_i
+ line += 1 if i.zero?
+ [file, line]
+ end
+
+ # escape '&', '"', '<' and '>' for use in HTML.
+ def html_escape(s)
+ s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
+ end
+ alias h html_escape
+
+ # If +value+ is an instance of class +klass+, return it, else
+ # create a new instance of +klass+ with value +value+.
+ def new_with_value_if_need(klass, value)
+ if value.is_a?(klass)
+ value
+ else
+ klass.new(value)
+ end
+ end
+
+ def element_initialize_arguments?(args)
+ [true, false].include?(args[0]) and args[1].is_a?(Hash)
+ end
+
+ module YesCleanOther
+ module_function
+ def parse(value)
+ if [true, false, nil].include?(value)
+ value
+ else
+ case value.to_s
+ when /\Ayes\z/i
+ true
+ when /\Aclean\z/i
+ false
+ else
+ nil
+ end
+ end
+ end
+ end
+
+ module YesOther
+ module_function
+ def parse(value)
+ if [true, false].include?(value)
+ value
+ else
+ /\Ayes\z/i.match(value.to_s) ? true : false
+ end
+ end
+ end
+
+ module CSV
+ module_function
+ def parse(value, &block)
+ if value.is_a?(String)
+ value = value.strip.split(/\s*,\s*/)
+ value = value.collect(&block) if block_given?
+ value
+ else
+ value
+ end
+ end
+ end
+
+ module InheritedReader
+ def inherited_reader(constant_name)
+ base_class = inherited_base
+ result = base_class.const_get(constant_name)
+ found_base_class = false
+ ancestors.reverse_each do |klass|
+ if found_base_class
+ if klass.const_defined?(constant_name)
+ result = yield(result, klass.const_get(constant_name))
+ end
+ else
+ found_base_class = klass == base_class
+ end
+ end
+ result
+ end
+
+ def inherited_array_reader(constant_name)
+ inherited_reader(constant_name) do |result, current|
+ current + result
+ end
+ end
+
+ def inherited_hash_reader(constant_name)
+ inherited_reader(constant_name) do |result, current|
+ result.merge(current)
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rss/xml-stylesheet.rb b/ruby/lib/rss/xml-stylesheet.rb
new file mode 100644
index 0000000..559d6bc
--- /dev/null
+++ b/ruby/lib/rss/xml-stylesheet.rb
@@ -0,0 +1,105 @@
+require "rss/utils"
+
+module RSS
+
+ module XMLStyleSheetMixin
+ attr_accessor :xml_stylesheets
+ def initialize(*args)
+ super
+ @xml_stylesheets = []
+ end
+
+ private
+ def xml_stylesheet_pi
+ xsss = @xml_stylesheets.collect do |xss|
+ pi = xss.to_s
+ pi = nil if /\A\s*\z/ =~ pi
+ pi
+ end.compact
+ xsss.push("") unless xsss.empty?
+ xsss.join("\n")
+ end
+ end
+
+ class XMLStyleSheet
+
+ include Utils
+
+ ATTRIBUTES = %w(href type title media charset alternate)
+
+ GUESS_TABLE = {
+ "xsl" => "text/xsl",
+ "css" => "text/css",
+ }
+
+ attr_accessor(*ATTRIBUTES)
+ attr_accessor(:do_validate)
+ def initialize(*attrs)
+ if attrs.size == 1 and
+ (attrs.first.is_a?(Hash) or attrs.first.is_a?(Array))
+ attrs = attrs.first
+ end
+ @do_validate = true
+ ATTRIBUTES.each do |attr|
+ __send__("#{attr}=", nil)
+ end
+ vars = ATTRIBUTES.dup
+ vars.unshift(:do_validate)
+ attrs.each do |name, value|
+ if vars.include?(name.to_s)
+ __send__("#{name}=", value)
+ end
+ end
+ end
+
+ def to_s
+ rv = ""
+ if @href
+ rv << %Q[<?xml-stylesheet]
+ ATTRIBUTES.each do |name|
+ if __send__(name)
+ rv << %Q[ #{name}="#{h __send__(name)}"]
+ end
+ end
+ rv << %Q[?>]
+ end
+ rv
+ end
+
+ remove_method(:href=)
+ def href=(value)
+ @href = value
+ if @href and @type.nil?
+ @type = guess_type(@href)
+ end
+ @href
+ end
+
+ remove_method(:alternate=)
+ def alternate=(value)
+ if value.nil? or /\A(?:yes|no)\z/ =~ value
+ @alternate = value
+ else
+ if @do_validate
+ args = ["?xml-stylesheet?", %Q[alternate="#{value}"]]
+ raise NotAvailableValueError.new(*args)
+ end
+ end
+ @alternate
+ end
+
+ def setup_maker(maker)
+ xss = maker.xml_stylesheets.new_xml_stylesheet
+ ATTRIBUTES.each do |attr|
+ xss.__send__("#{attr}=", __send__(attr))
+ end
+ end
+
+ private
+ def guess_type(filename)
+ /\.([^.]+)$/ =~ filename
+ GUESS_TABLE[$1]
+ end
+
+ end
+end
diff --git a/ruby/lib/rss/xml.rb b/ruby/lib/rss/xml.rb
new file mode 100644
index 0000000..1ae878b
--- /dev/null
+++ b/ruby/lib/rss/xml.rb
@@ -0,0 +1,71 @@
+require "rss/utils"
+
+module RSS
+ module XML
+ class Element
+ include Enumerable
+
+ attr_reader :name, :prefix, :uri, :attributes, :children
+ def initialize(name, prefix=nil, uri=nil, attributes={}, children=[])
+ @name = name
+ @prefix = prefix
+ @uri = uri
+ @attributes = attributes
+ if children.is_a?(String) or !children.respond_to?(:each)
+ @children = [children]
+ else
+ @children = children
+ end
+ end
+
+ def [](name)
+ @attributes[name]
+ end
+
+ def []=(name, value)
+ @attributes[name] = value
+ end
+
+ def <<(child)
+ @children << child
+ end
+
+ def each(&block)
+ @children.each(&block)
+ end
+
+ def ==(other)
+ other.kind_of?(self.class) and
+ @name == other.name and
+ @uri == other.uri and
+ @attributes == other.attributes and
+ @children == other.children
+ end
+
+ def to_s
+ rv = "<#{full_name}"
+ attributes.each do |key, value|
+ rv << " #{Utils.html_escape(key)}=\"#{Utils.html_escape(value)}\""
+ end
+ if children.empty?
+ rv << "/>"
+ else
+ rv << ">"
+ children.each do |child|
+ rv << child.to_s
+ end
+ rv << "</#{full_name}>"
+ end
+ rv
+ end
+
+ def full_name
+ if @prefix
+ "#{@prefix}:#{@name}"
+ else
+ @name
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rss/xmlparser.rb b/ruby/lib/rss/xmlparser.rb
new file mode 100644
index 0000000..3dfe7d4
--- /dev/null
+++ b/ruby/lib/rss/xmlparser.rb
@@ -0,0 +1,93 @@
+begin
+ require "xml/parser"
+rescue LoadError
+ require "xmlparser"
+end
+
+begin
+ require "xml/encoding-ja"
+rescue LoadError
+ require "xmlencoding-ja"
+ if defined?(Kconv)
+ module XMLEncoding_ja
+ class SJISHandler
+ include Kconv
+ end
+ end
+ end
+end
+
+module XML
+ class Parser
+ unless defined?(Error)
+ Error = ::XMLParserError
+ end
+ end
+end
+
+module RSS
+
+ class REXMLLikeXMLParser < ::XML::Parser
+
+ include ::XML::Encoding_ja
+
+ def listener=(listener)
+ @listener = listener
+ end
+
+ def startElement(name, attrs)
+ @listener.tag_start(name, attrs)
+ end
+
+ def endElement(name)
+ @listener.tag_end(name)
+ end
+
+ def character(data)
+ @listener.text(data)
+ end
+
+ def xmlDecl(version, encoding, standalone)
+ @listener.xmldecl(version, encoding, standalone == 1)
+ end
+
+ def processingInstruction(target, content)
+ @listener.instruction(target, content)
+ end
+
+ end
+
+ class XMLParserParser < BaseParser
+
+ class << self
+ def listener
+ XMLParserListener
+ end
+ end
+
+ private
+ def _parse
+ begin
+ parser = REXMLLikeXMLParser.new
+ parser.listener = @listener
+ parser.parse(@rss)
+ rescue ::XML::Parser::Error => e
+ raise NotWellFormedError.new(parser.line){e.message}
+ end
+ end
+
+ end
+
+ class XMLParserListener < BaseListener
+
+ include ListenerMixin
+
+ def xmldecl(version, encoding, standalone)
+ super
+ # Encoding is converted to UTF-8 when XMLParser parses XML.
+ @encoding = 'UTF-8'
+ end
+
+ end
+
+end
diff --git a/ruby/lib/rss/xmlscanner.rb b/ruby/lib/rss/xmlscanner.rb
new file mode 100644
index 0000000..61b9fa6
--- /dev/null
+++ b/ruby/lib/rss/xmlscanner.rb
@@ -0,0 +1,121 @@
+require 'xmlscan/scanner'
+require 'stringio'
+
+module RSS
+
+ class XMLScanParser < BaseParser
+
+ class << self
+ def listener
+ XMLScanListener
+ end
+ end
+
+ private
+ def _parse
+ begin
+ if @rss.is_a?(String)
+ input = StringIO.new(@rss)
+ else
+ input = @rss
+ end
+ scanner = XMLScan::XMLScanner.new(@listener)
+ scanner.parse(input)
+ rescue XMLScan::Error => e
+ lineno = e.lineno || scanner.lineno || input.lineno
+ raise NotWellFormedError.new(lineno){e.message}
+ end
+ end
+
+ end
+
+ class XMLScanListener < BaseListener
+
+ include XMLScan::Visitor
+ include ListenerMixin
+
+ ENTITIES = {
+ 'lt' => '<',
+ 'gt' => '>',
+ 'amp' => '&',
+ 'quot' => '"',
+ 'apos' => '\''
+ }
+
+ def on_xmldecl_version(str)
+ @version = str
+ end
+
+ def on_xmldecl_encoding(str)
+ @encoding = str
+ end
+
+ def on_xmldecl_standalone(str)
+ @standalone = str
+ end
+
+ def on_xmldecl_end
+ xmldecl(@version, @encoding, @standalone == "yes")
+ end
+
+ alias_method(:on_pi, :instruction)
+ alias_method(:on_chardata, :text)
+ alias_method(:on_cdata, :text)
+
+ def on_etag(name)
+ tag_end(name)
+ end
+
+ def on_entityref(ref)
+ text(entity(ref))
+ end
+
+ def on_charref(code)
+ text([code].pack('U'))
+ end
+
+ alias_method(:on_charref_hex, :on_charref)
+
+ def on_stag(name)
+ @attrs = {}
+ end
+
+ def on_attribute(name)
+ @attrs[name] = @current_attr = ''
+ end
+
+ def on_attr_value(str)
+ @current_attr << str
+ end
+
+ def on_attr_entityref(ref)
+ @current_attr << entity(ref)
+ end
+
+ def on_attr_charref(code)
+ @current_attr << [code].pack('U')
+ end
+
+ alias_method(:on_attr_charref_hex, :on_attr_charref)
+
+ def on_stag_end(name)
+ tag_start(name, @attrs)
+ end
+
+ def on_stag_end_empty(name)
+ tag_start(name, @attrs)
+ tag_end(name)
+ end
+
+ private
+ def entity(ref)
+ ent = ENTITIES[ref]
+ if ent
+ ent
+ else
+ wellformed_error("undefined entity: #{ref}")
+ end
+ end
+ end
+
+end
diff --git a/ruby/lib/rubygems.rb b/ruby/lib/rubygems.rb
new file mode 100644
index 0000000..9913b59
--- /dev/null
+++ b/ruby/lib/rubygems.rb
@@ -0,0 +1,889 @@
+# -*- ruby -*-
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems/rubygems_version'
+require 'rubygems/defaults'
+require 'thread'
+
+module Gem
+ class LoadError < ::LoadError
+ attr_accessor :name, :version_requirement
+ end
+end
+
+module Kernel
+
+ ##
+ # Use Kernel#gem to activate a specific version of +gem_name+.
+ #
+ # +version_requirements+ is a list of version requirements that the
+ # specified gem must match, most commonly "= example.version.number". See
+ # Gem::Requirement for how to specify a version requirement.
+ #
+ # If you will be activating the latest version of a gem, there is no need to
+ # call Kernel#gem, Kernel#require will do the right thing for you.
+ #
+ # Kernel#gem returns true if the gem was activated, otherwise false. If the
+ # gem could not be found, didn't match the version requirements, or a
+ # different version was already activated, an exception will be raised.
+ #
+ # Kernel#gem should be called *before* any require statements (otherwise
+ # RubyGems may load a conflicting library version).
+ #
+ # In older RubyGems versions, the environment variable GEM_SKIP could be
+ # used to skip activation of specified gems, for example to test out changes
+ # that haven't been installed yet. Now RubyGems defers to -I and the
+ # RUBYLIB environment variable to skip activation of a gem.
+ #
+ # Example:
+ #
+ # GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb
+
+ def gem(gem_name, *version_requirements) # :doc:
+ skip_list = (ENV['GEM_SKIP'] || "").split(/:/)
+ raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name
+ Gem.activate(gem_name, *version_requirements)
+ end
+
+ private :gem
+
+end
+
+##
+# Main module to hold all RubyGem classes/modules.
+
+module Gem
+
+ ConfigMap = {} unless defined?(ConfigMap)
+ require 'rbconfig'
+ RbConfig = Config unless defined? ::RbConfig
+
+ ConfigMap.merge!(
+ :BASERUBY => RbConfig::CONFIG["BASERUBY"],
+ :EXEEXT => RbConfig::CONFIG["EXEEXT"],
+ :RUBY_INSTALL_NAME => RbConfig::CONFIG["RUBY_INSTALL_NAME"],
+ :RUBY_SO_NAME => RbConfig::CONFIG["RUBY_SO_NAME"],
+ :arch => RbConfig::CONFIG["arch"],
+ :bindir => RbConfig::CONFIG["bindir"],
+ :datadir => RbConfig::CONFIG["datadir"],
+ :libdir => RbConfig::CONFIG["libdir"],
+ :ruby_install_name => RbConfig::CONFIG["ruby_install_name"],
+ :ruby_version => RbConfig::CONFIG["ruby_version"],
+ :sitedir => RbConfig::CONFIG["sitedir"],
+ :sitelibdir => RbConfig::CONFIG["sitelibdir"],
+ :vendordir => RbConfig::CONFIG["vendordir"] ,
+ :vendorlibdir => RbConfig::CONFIG["vendorlibdir"]
+ )
+
+ DIRECTORIES = %w[cache doc gems specifications] unless defined?(DIRECTORIES)
+
+ MUTEX = Mutex.new
+
+ RubyGemsPackageVersion = RubyGemsVersion
+
+ ##
+ # An Array of Regexps that match windows ruby platforms.
+
+ WIN_PATTERNS = [
+ /bccwin/i,
+ /cygwin/i,
+ /djgpp/i,
+ /mingw/i,
+ /mswin/i,
+ /wince/i,
+ ]
+
+ @@source_index = nil
+ @@win_platform = nil
+
+ @configuration = nil
+ @loaded_specs = {}
+ @platforms = []
+ @ruby = nil
+ @sources = []
+
+ @post_install_hooks ||= []
+ @post_uninstall_hooks ||= []
+ @pre_uninstall_hooks ||= []
+ @pre_install_hooks ||= []
+
+ ##
+ # Activates an installed gem matching +gem+. The gem must satisfy
+ # +version_requirements+.
+ #
+ # Returns true if the gem is activated, false if it is already
+ # loaded, or an exception otherwise.
+ #
+ # Gem#activate adds the library paths in +gem+ to $LOAD_PATH. Before a Gem
+ # is activated its required Gems are activated. If the version information
+ # is omitted, the highest version Gem of the supplied name is loaded. If a
+ # Gem is not found that meets the version requirements or a required Gem is
+ # not found, a Gem::LoadError is raised.
+ #
+ # More information on version requirements can be found in the
+ # Gem::Requirement and Gem::Version documentation.
+
+ def self.activate(gem, *version_requirements)
+ if version_requirements.empty? then
+ version_requirements = Gem::Requirement.default
+ end
+
+ unless gem.respond_to?(:name) and
+ gem.respond_to?(:version_requirements) then
+ gem = Gem::Dependency.new(gem, version_requirements)
+ end
+
+ matches = Gem.source_index.find_name(gem.name, gem.version_requirements)
+ report_activate_error(gem) if matches.empty?
+
+ if @loaded_specs[gem.name] then
+ # This gem is already loaded. If the currently loaded gem is not in the
+ # list of candidate gems, then we have a version conflict.
+ existing_spec = @loaded_specs[gem.name]
+
+ unless matches.any? { |spec| spec.version == existing_spec.version } then
+ raise Gem::Exception,
+ "can't activate #{gem}, already activated #{existing_spec.full_name}"
+ end
+
+ return false
+ end
+
+ # new load
+ spec = matches.last
+ return false if spec.loaded?
+
+ spec.loaded = true
+ @loaded_specs[spec.name] = spec
+
+ # Load dependent gems first
+ spec.runtime_dependencies.each do |dep_gem|
+ activate dep_gem
+ end
+
+ # bin directory must come before library directories
+ spec.require_paths.unshift spec.bindir if spec.bindir
+
+ require_paths = spec.require_paths.map do |path|
+ File.join spec.full_gem_path, path
+ end
+
+ sitelibdir = ConfigMap[:sitelibdir]
+
+ # gem directories must come after -I and ENV['RUBYLIB']
+ insert_index = load_path_insert_index
+
+ if insert_index then
+ # gem directories must come after -I and ENV['RUBYLIB']
+ $LOAD_PATH.insert(insert_index, *require_paths)
+ else
+ # we are probably testing in core, -I and RUBYLIB don't apply
+ $LOAD_PATH.unshift(*require_paths)
+ end
+
+ return true
+ end
+
+ ##
+ # An Array of all possible load paths for all versions of all gems in the
+ # Gem installation.
+
+ def self.all_load_paths
+ result = []
+
+ Gem.path.each do |gemdir|
+ each_load_path all_partials(gemdir) do |load_path|
+ result << load_path
+ end
+ end
+
+ result
+ end
+
+ ##
+ # Return all the partial paths in +gemdir+.
+
+ def self.all_partials(gemdir)
+ Dir[File.join(gemdir, 'gems/*')]
+ end
+
+ private_class_method :all_partials
+
+ ##
+ # See if a given gem is available.
+
+ def self.available?(gem, *requirements)
+ requirements = Gem::Requirement.default if requirements.empty?
+
+ unless gem.respond_to?(:name) and
+ gem.respond_to?(:version_requirements) then
+ gem = Gem::Dependency.new gem, requirements
+ end
+
+ !Gem.source_index.search(gem).empty?
+ end
+
+ ##
+ # The mode needed to read a file as straight binary.
+
+ def self.binary_mode
+ @binary_mode ||= RUBY_VERSION > '1.9' ? 'rb:ascii-8bit' : 'rb'
+ end
+
+ ##
+ # The path where gem executables are to be installed.
+
+ def self.bindir(install_dir=Gem.dir)
+ return File.join(install_dir, 'bin') unless
+ install_dir.to_s == Gem.default_dir
+ Gem.default_bindir
+ end
+
+ ##
+ # Reset the +dir+ and +path+ values. The next time +dir+ or +path+
+ # is requested, the values will be calculated from scratch. This is
+ # mainly used by the unit tests to provide test isolation.
+
+ def self.clear_paths
+ @gem_home = nil
+ @gem_path = nil
+ @user_home = nil
+
+ @@source_index = nil
+
+ MUTEX.synchronize do
+ @searcher = nil
+ end
+ end
+
+ ##
+ # The path to standard location of the user's .gemrc file.
+
+ def self.config_file
+ File.join Gem.user_home, '.gemrc'
+ end
+
+ ##
+ # The standard configuration object for gems.
+
+ def self.configuration
+ @configuration ||= Gem::ConfigFile.new []
+ end
+
+ ##
+ # Use the given configuration object (which implements the ConfigFile
+ # protocol) as the standard configuration object.
+
+ def self.configuration=(config)
+ @configuration = config
+ end
+
+ ##
+ # The path the the data directory specified by the gem name. If the
+ # package is not available as a gem, return nil.
+
+ def self.datadir(gem_name)
+ spec = @loaded_specs[gem_name]
+ return nil if spec.nil?
+ File.join(spec.full_gem_path, 'data', gem_name)
+ end
+
+ ##
+ # A Zlib::Deflate.deflate wrapper
+
+ def self.deflate(data)
+ require 'zlib'
+ Zlib::Deflate.deflate data
+ end
+
+ ##
+ # The path where gems are to be installed.
+
+ def self.dir
+ @gem_home ||= nil
+ set_home(ENV['GEM_HOME'] || Gem.configuration.home || default_dir) unless @gem_home
+ @gem_home
+ end
+
+ ##
+ # Expand each partial gem path with each of the required paths specified
+ # in the Gem spec. Each expanded path is yielded.
+
+ def self.each_load_path(partials)
+ partials.each do |gp|
+ base = File.basename(gp)
+ specfn = File.join(dir, "specifications", base + ".gemspec")
+ if File.exist?(specfn)
+ spec = eval(File.read(specfn))
+ spec.require_paths.each do |rp|
+ yield(File.join(gp, rp))
+ end
+ else
+ filename = File.join(gp, 'lib')
+ yield(filename) if File.exist?(filename)
+ end
+ end
+ end
+
+ private_class_method :each_load_path
+
+ ##
+ # Quietly ensure the named Gem directory contains all the proper
+ # subdirectories. If we can't create a directory due to a permission
+ # problem, then we will silently continue.
+
+ def self.ensure_gem_subdirectories(gemdir)
+ require 'fileutils'
+
+ Gem::DIRECTORIES.each do |filename|
+ fn = File.join gemdir, filename
+ FileUtils.mkdir_p fn rescue nil unless File.exist? fn
+ end
+ end
+
+ ##
+ # Returns a list of paths matching +file+ that can be used by a gem to pick
+ # up features from other gems. For example:
+ #
+ # Gem.find_files('rdoc/discover').each do |path| load path end
+ #
+ # find_files does not search $LOAD_PATH for files, only gems.
+
+ def self.find_files(path)
+ specs = searcher.find_all path
+
+ specs.map do |spec|
+ searcher.matching_files spec, path
+ end.flatten
+ end
+
+ ##
+ # Finds the user's home directory.
+ #--
+ # Some comments from the ruby-talk list regarding finding the home
+ # directory:
+ #
+ # I have HOME, USERPROFILE and HOMEDRIVE + HOMEPATH. Ruby seems
+ # to be depending on HOME in those code samples. I propose that
+ # it should fallback to USERPROFILE and HOMEDRIVE + HOMEPATH (at
+ # least on Win32).
+
+ def self.find_home
+ ['HOME', 'USERPROFILE'].each do |homekey|
+ return ENV[homekey] if ENV[homekey]
+ end
+
+ if ENV['HOMEDRIVE'] && ENV['HOMEPATH'] then
+ return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}"
+ end
+
+ begin
+ File.expand_path("~")
+ rescue
+ if File::ALT_SEPARATOR then
+ "C:/"
+ else
+ "/"
+ end
+ end
+ end
+
+ private_class_method :find_home
+
+ ##
+ # Zlib::GzipReader wrapper that unzips +data+.
+
+ def self.gunzip(data)
+ require 'stringio'
+ require 'zlib'
+ data = StringIO.new data
+
+ Zlib::GzipReader.new(data).read
+ end
+
+ ##
+ # Zlib::GzipWriter wrapper that zips +data+.
+
+ def self.gzip(data)
+ require 'stringio'
+ require 'zlib'
+ zipped = StringIO.new
+
+ Zlib::GzipWriter.wrap zipped do |io| io.write data end
+
+ zipped.string
+ end
+
+ ##
+ # A Zlib::Inflate#inflate wrapper
+
+ def self.inflate(data)
+ require 'zlib'
+ Zlib::Inflate.inflate data
+ end
+
+ ##
+ # Return a list of all possible load paths for the latest version for all
+ # gems in the Gem installation.
+
+ def self.latest_load_paths
+ result = []
+
+ Gem.path.each do |gemdir|
+ each_load_path(latest_partials(gemdir)) do |load_path|
+ result << load_path
+ end
+ end
+
+ result
+ end
+
+ ##
+ # Return only the latest partial paths in the given +gemdir+.
+
+ def self.latest_partials(gemdir)
+ latest = {}
+ all_partials(gemdir).each do |gp|
+ base = File.basename(gp)
+ if base =~ /(.*)-((\d+\.)*\d+)/ then
+ name, version = $1, $2
+ ver = Gem::Version.new(version)
+ if latest[name].nil? || ver > latest[name][0]
+ latest[name] = [ver, gp]
+ end
+ end
+ end
+ latest.collect { |k,v| v[1] }
+ end
+
+ private_class_method :latest_partials
+
+ ##
+ # The index to insert activated gem paths into the $LOAD_PATH.
+ #
+ # Defaults to the site lib directory unless gem_prelude.rb has loaded paths,
+ # then it inserts the activated gem's paths before the gem_prelude.rb paths
+ # so you can override the gem_prelude.rb default $LOAD_PATH paths.
+
+ def self.load_path_insert_index
+ index = $LOAD_PATH.index ConfigMap[:sitelibdir]
+
+ $LOAD_PATH.each_with_index do |path, i|
+ if path.instance_variables.include?(:@gem_prelude_index) or
+ path.instance_variables.include?('@gem_prelude_index') then
+ index = i
+ break
+ end
+ end
+
+ index
+ end
+
+ ##
+ # The file name and line number of the caller of the caller of this method.
+
+ def self.location_of_caller
+ caller[1] =~ /(.*?):(\d+)$/i
+ file = $1
+ lineno = $2.to_i
+
+ [file, lineno]
+ end
+
+ ##
+ # manage_gems is useless and deprecated. Don't call it anymore.
+
+ def self.manage_gems # :nodoc:
+ file, lineno = location_of_caller
+
+ warn "#{file}:#{lineno}:Warning: Gem::manage_gems is deprecated and will be removed on or after March 2009."
+ end
+
+ ##
+ # The version of the Marshal format for your Ruby.
+
+ def self.marshal_version
+ "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
+ end
+
+ ##
+ # Array of paths to search for Gems.
+
+ def self.path
+ @gem_path ||= nil
+
+ unless @gem_path then
+ paths = [ENV['GEM_PATH'] || Gem.configuration.path || default_path]
+
+ if defined?(APPLE_GEM_HOME) and not ENV['GEM_PATH'] then
+ paths << APPLE_GEM_HOME
+ end
+
+ set_paths paths.compact.join(File::PATH_SEPARATOR)
+ end
+
+ @gem_path
+ end
+
+ ##
+ # Set array of platforms this RubyGems supports (primarily for testing).
+
+ def self.platforms=(platforms)
+ @platforms = platforms
+ end
+
+ ##
+ # Array of platforms this RubyGems supports.
+
+ def self.platforms
+ @platforms ||= []
+ if @platforms.empty?
+ @platforms = [Gem::Platform::RUBY, Gem::Platform.local]
+ end
+ @platforms
+ end
+
+ ##
+ # Adds a post-install hook that will be passed an Gem::Installer instance
+ # when Gem::Installer#install is called
+
+ def self.post_install(&hook)
+ @post_install_hooks << hook
+ end
+
+ ##
+ # Adds a post-uninstall hook that will be passed a Gem::Uninstaller instance
+ # and the spec that was uninstalled when Gem::Uninstaller#uninstall is
+ # called
+
+ def self.post_uninstall(&hook)
+ @post_uninstall_hooks << hook
+ end
+
+ ##
+ # Adds a pre-install hook that will be passed an Gem::Installer instance
+ # when Gem::Installer#install is called
+
+ def self.pre_install(&hook)
+ @pre_install_hooks << hook
+ end
+
+ ##
+ # Adds a pre-uninstall hook that will be passed an Gem::Uninstaller instance
+ # and the spec that will be uninstalled when Gem::Uninstaller#uninstall is
+ # called
+
+ def self.pre_uninstall(&hook)
+ @pre_uninstall_hooks << hook
+ end
+
+ ##
+ # The directory prefix this RubyGems was installed at.
+
+ def self.prefix
+ prefix = File.dirname File.expand_path(__FILE__)
+
+ if File.dirname(prefix) == File.expand_path(ConfigMap[:sitelibdir]) or
+ File.dirname(prefix) == File.expand_path(ConfigMap[:libdir]) or
+ 'lib' != File.basename(prefix) then
+ nil
+ else
+ File.dirname prefix
+ end
+ end
+
+ ##
+ # Refresh source_index from disk and clear searcher.
+
+ def self.refresh
+ source_index.refresh!
+
+ MUTEX.synchronize do
+ @searcher = nil
+ end
+ end
+
+ ##
+ # Safely read a file in binary mode on all platforms.
+
+ def self.read_binary(path)
+ File.open path, binary_mode do |f| f.read end
+ end
+
+ ##
+ # Report a load error during activation. The message of load error
+ # depends on whether it was a version mismatch or if there are not gems of
+ # any version by the requested name.
+
+ def self.report_activate_error(gem)
+ matches = Gem.source_index.find_name(gem.name)
+
+ if matches.empty? then
+ error = Gem::LoadError.new(
+ "Could not find RubyGem #{gem.name} (#{gem.version_requirements})\n")
+ else
+ error = Gem::LoadError.new(
+ "RubyGem version error: " +
+ "#{gem.name}(#{matches.first.version} not #{gem.version_requirements})\n")
+ end
+
+ error.name = gem.name
+ error.version_requirement = gem.version_requirements
+ raise error
+ end
+
+ private_class_method :report_activate_error
+
+ def self.required_location(gemname, libfile, *version_constraints)
+ version_constraints = Gem::Requirement.default if version_constraints.empty?
+ matches = Gem.source_index.find_name(gemname, version_constraints)
+ return nil if matches.empty?
+ spec = matches.last
+ spec.require_paths.each do |path|
+ result = File.join(spec.full_gem_path, path, libfile)
+ return result if File.exist?(result)
+ end
+ nil
+ end
+
+ ##
+ # The path to the running Ruby interpreter.
+
+ def self.ruby
+ if @ruby.nil? then
+ @ruby = File.join(ConfigMap[:bindir],
+ ConfigMap[:ruby_install_name])
+ @ruby << ConfigMap[:EXEEXT]
+
+ # escape string in case path to ruby executable contain spaces.
+ @ruby.sub!(/.*\s.*/m, '"\&"')
+ end
+
+ @ruby
+ end
+
+ ##
+ # A Gem::Version for the currently running ruby.
+
+ def self.ruby_version
+ return @ruby_version if defined? @ruby_version
+ version = RUBY_VERSION.dup
+ version << ".#{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
+ @ruby_version = Gem::Version.new version
+ end
+
+ ##
+ # The GemPathSearcher object used to search for matching installed gems.
+
+ def self.searcher
+ MUTEX.synchronize do
+ @searcher ||= Gem::GemPathSearcher.new
+ end
+ end
+
+ ##
+ # Set the Gem home directory (as reported by Gem.dir).
+
+ def self.set_home(home)
+ home = home.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
+ @gem_home = home
+ ensure_gem_subdirectories(@gem_home)
+ end
+
+ private_class_method :set_home
+
+ ##
+ # Set the Gem search path (as reported by Gem.path).
+
+ def self.set_paths(gpaths)
+ if gpaths
+ @gem_path = gpaths.split(File::PATH_SEPARATOR)
+
+ if File::ALT_SEPARATOR then
+ @gem_path.map! do |path|
+ path.gsub File::ALT_SEPARATOR, File::SEPARATOR
+ end
+ end
+
+ @gem_path << Gem.dir
+ else
+ # TODO: should this be Gem.default_path instead?
+ @gem_path = [Gem.dir]
+ end
+
+ @gem_path.uniq!
+ @gem_path.each do |path|
+ if 0 == File.expand_path(path).index(Gem.user_home)
+ next unless File.directory? Gem.user_home
+ unless win_platform? then
+ # only create by matching user
+ next if Etc.getpwuid.uid != File::Stat.new(Gem.user_home).uid
+ end
+ end
+ ensure_gem_subdirectories path
+ end
+ end
+
+ private_class_method :set_paths
+
+ ##
+ # Returns the Gem::SourceIndex of specifications that are in the Gem.path
+
+ def self.source_index
+ @@source_index ||= SourceIndex.from_installed_gems
+ end
+
+ ##
+ # Returns an Array of sources to fetch remote gems from. If the sources
+ # list is empty, attempts to load the "sources" gem, then uses
+ # default_sources if it is not installed.
+
+ def self.sources
+ if @sources.empty? then
+ begin
+ gem 'sources', '> 0.0.1'
+ require 'sources'
+ rescue LoadError
+ @sources = default_sources
+ end
+ end
+
+ @sources
+ end
+
+ ##
+ # Need to be able to set the sources without calling
+ # Gem.sources.replace since that would cause an infinite loop.
+
+ def self.sources=(new_sources)
+ @sources = new_sources
+ end
+
+ ##
+ # Glob pattern for require-able path suffixes.
+
+ def self.suffix_pattern
+ @suffix_pattern ||= "{#{suffixes.join(',')}}"
+ end
+
+ ##
+ # Suffixes for require-able paths.
+
+ def self.suffixes
+ ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']
+ end
+
+ ##
+ # Use the +home+ and +paths+ values for Gem.dir and Gem.path. Used mainly
+ # by the unit tests to provide environment isolation.
+
+ def self.use_paths(home, paths=[])
+ clear_paths
+ set_home(home) if home
+ set_paths(paths.join(File::PATH_SEPARATOR)) if paths
+ end
+
+ ##
+ # The home directory for the user.
+
+ def self.user_home
+ @user_home ||= find_home
+ end
+
+ ##
+ # Is this a windows platform?
+
+ def self.win_platform?
+ if @@win_platform.nil? then
+ @@win_platform = !!WIN_PATTERNS.find { |r| RUBY_PLATFORM =~ r }
+ end
+
+ @@win_platform
+ end
+
+ class << self
+
+ attr_reader :loaded_specs
+
+ ##
+ # The list of hooks to be run before Gem::Install#install does any work
+
+ attr_reader :post_install_hooks
+
+ ##
+ # The list of hooks to be run before Gem::Uninstall#uninstall does any
+ # work
+
+ attr_reader :post_uninstall_hooks
+
+ ##
+ # The list of hooks to be run after Gem::Install#install is finished
+
+ attr_reader :pre_install_hooks
+
+ ##
+ # The list of hooks to be run after Gem::Uninstall#uninstall is finished
+
+ attr_reader :pre_uninstall_hooks
+
+ # :stopdoc:
+
+ alias cache source_index # an alias for the old name
+
+ # :startdoc:
+
+ end
+
+ MARSHAL_SPEC_DIR = "quick/Marshal.#{Gem.marshal_version}/"
+
+ YAML_SPEC_DIR = 'quick/'
+
+end
+
+module Config
+ # :stopdoc:
+ class << self
+ # Return the path to the data directory associated with the named
+ # package. If the package is loaded as a gem, return the gem
+ # specific data directory. Otherwise return a path to the share
+ # area as define by "#{ConfigMap[:datadir]}/#{package_name}".
+ def datadir(package_name)
+ Gem.datadir(package_name) ||
+ File.join(Gem::ConfigMap[:datadir], package_name)
+ end
+ end
+ # :startdoc:
+end
+
+require 'rubygems/exceptions'
+require 'rubygems/version'
+require 'rubygems/requirement'
+require 'rubygems/dependency'
+require 'rubygems/gem_path_searcher' # Needed for Kernel#gem
+require 'rubygems/source_index' # Needed for Kernel#gem
+require 'rubygems/platform'
+require 'rubygems/builder' # HACK: Needed for rake's package task.
+
+begin
+ require 'rubygems/defaults/operating_system'
+rescue LoadError
+end
+
+if defined?(RUBY_ENGINE) then
+ begin
+ require "rubygems/defaults/#{RUBY_ENGINE}"
+ rescue LoadError
+ end
+end
+
+require 'rubygems/config_file'
+
+if RUBY_VERSION < '1.9' then
+ require 'rubygems/custom_require'
+end
+
+Gem.clear_paths
diff --git a/ruby/lib/rubygems/builder.rb b/ruby/lib/rubygems/builder.rb
new file mode 100644
index 0000000..6fd8528
--- /dev/null
+++ b/ruby/lib/rubygems/builder.rb
@@ -0,0 +1,88 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+module Gem
+
+ ##
+ # The Builder class processes RubyGem specification files
+ # to produce a .gem file.
+ #
+ class Builder
+
+ include UserInteraction
+ ##
+ # Constructs a builder instance for the provided specification
+ #
+ # spec:: [Gem::Specification] The specification instance
+ #
+ def initialize(spec)
+ require "yaml"
+ require "rubygems/package"
+ require "rubygems/security"
+
+ @spec = spec
+ end
+
+ ##
+ # Builds the gem from the specification. Returns the name of the file
+ # written.
+ #
+ def build
+ @spec.mark_version
+ @spec.validate
+ @signer = sign
+ write_package
+ say success
+ @spec.file_name
+ end
+
+ def success
+ <<-EOM
+ Successfully built RubyGem
+ Name: #{@spec.name}
+ Version: #{@spec.version}
+ File: #{@spec.full_name+'.gem'}
+EOM
+ end
+
+ private
+
+ def sign
+ # if the signing key was specified, then load the file, and swap
+ # to the public key (TODO: we should probably just omit the
+ # signing key in favor of the signing certificate, but that's for
+ # the future, also the signature algorithm should be configurable)
+ signer = nil
+ if @spec.respond_to?(:signing_key) && @spec.signing_key
+ signer = Gem::Security::Signer.new(@spec.signing_key, @spec.cert_chain)
+ @spec.signing_key = nil
+ @spec.cert_chain = signer.cert_chain.map { |cert| cert.to_s }
+ end
+ signer
+ end
+
+ def write_package
+ open @spec.file_name, 'wb' do |gem_io|
+ Gem::Package.open gem_io, 'w', @signer do |pkg|
+ pkg.metadata = @spec.to_yaml
+
+ @spec.files.each do |file|
+ next if File.directory? file
+
+ stat = File.stat file
+ mode = stat.mode & 0777
+ size = stat.size
+
+ pkg.add_file_simple file, mode, size do |tar_io|
+ tar_io.write open(file, "rb") { |f| f.read }
+ end
+ end
+ end
+ end
+ end
+ end
+end
+
diff --git a/ruby/lib/rubygems/command.rb b/ruby/lib/rubygems/command.rb
new file mode 100644
index 0000000..860764e
--- /dev/null
+++ b/ruby/lib/rubygems/command.rb
@@ -0,0 +1,406 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'optparse'
+
+require 'rubygems/user_interaction'
+
+module Gem
+
+ # Base class for all Gem commands. When creating a new gem command, define
+ # #arguments, #defaults_str, #description and #usage (as appropriate).
+ class Command
+
+ include UserInteraction
+
+ # The name of the command.
+ attr_reader :command
+
+ # The options for the command.
+ attr_reader :options
+
+ # The default options for the command.
+ attr_accessor :defaults
+
+ # The name of the command for command-line invocation.
+ attr_accessor :program_name
+
+ # A short description of the command.
+ attr_accessor :summary
+
+ # Initializes a generic gem command named +command+. +summary+ is a short
+ # description displayed in `gem help commands`. +defaults+ are the
+ # default options. Defaults should be mirrored in #defaults_str, unless
+ # there are none.
+ #
+ # Use add_option to add command-line switches.
+ def initialize(command, summary=nil, defaults={})
+ @command = command
+ @summary = summary
+ @program_name = "gem #{command}"
+ @defaults = defaults
+ @options = defaults.dup
+ @option_groups = Hash.new { |h,k| h[k] = [] }
+ @parser = nil
+ @when_invoked = nil
+ end
+
+ # True if +long+ begins with the characters from +short+.
+ def begins?(long, short)
+ return false if short.nil?
+ long[0, short.length] == short
+ end
+
+ # Override to provide command handling.
+ def execute
+ fail "Generic command has no actions"
+ end
+
+ # Get all gem names from the command line.
+ def get_all_gem_names
+ args = options[:args]
+
+ if args.nil? or args.empty? then
+ raise Gem::CommandLineError,
+ "Please specify at least one gem name (e.g. gem build GEMNAME)"
+ end
+
+ gem_names = args.select { |arg| arg !~ /^-/ }
+ end
+
+ # Get the single gem name from the command line. Fail if there is no gem
+ # name or if there is more than one gem name given.
+ def get_one_gem_name
+ args = options[:args]
+
+ if args.nil? or args.empty? then
+ raise Gem::CommandLineError,
+ "Please specify a gem name on the command line (e.g. gem build GEMNAME)"
+ end
+
+ if args.size > 1 then
+ raise Gem::CommandLineError,
+ "Too many gem names (#{args.join(', ')}); please specify only one"
+ end
+
+ args.first
+ end
+
+ # Get a single optional argument from the command line. If more than one
+ # argument is given, return only the first. Return nil if none are given.
+ def get_one_optional_argument
+ args = options[:args] || []
+ args.first
+ end
+
+ # Override to provide details of the arguments a command takes.
+ # It should return a left-justified string, one argument per line.
+ def arguments
+ ""
+ end
+
+ # Override to display the default values of the command
+ # options. (similar to +arguments+, but displays the default
+ # values).
+ def defaults_str
+ ""
+ end
+
+ # Override to display a longer description of what this command does.
+ def description
+ nil
+ end
+
+ # Override to display the usage for an individual gem command.
+ def usage
+ program_name
+ end
+
+ # Display the help message for the command.
+ def show_help
+ parser.program_name = usage
+ say parser
+ end
+
+ # Invoke the command with the given list of arguments.
+ def invoke(*args)
+ handle_options(args)
+ if options[:help]
+ show_help
+ elsif @when_invoked
+ @when_invoked.call(options)
+ else
+ execute
+ end
+ end
+
+ # Call the given block when invoked.
+ #
+ # Normal command invocations just executes the +execute+ method of
+ # the command. Specifying an invocation block allows the test
+ # methods to override the normal action of a command to determine
+ # that it has been invoked correctly.
+ def when_invoked(&block)
+ @when_invoked = block
+ end
+
+ # Add a command-line option and handler to the command.
+ #
+ # See OptionParser#make_switch for an explanation of +opts+.
+ #
+ # +handler+ will be called with two values, the value of the argument and
+ # the options hash.
+ def add_option(*opts, &handler) # :yields: value, options
+ group_name = Symbol === opts.first ? opts.shift : :options
+
+ @option_groups[group_name] << [opts, handler]
+ end
+
+ # Remove previously defined command-line argument +name+.
+ def remove_option(name)
+ @option_groups.each do |_, option_list|
+ option_list.reject! { |args, _| args.any? { |x| x =~ /^#{name}/ } }
+ end
+ end
+
+ # Merge a set of command options with the set of default options
+ # (without modifying the default option hash).
+ def merge_options(new_options)
+ @options = @defaults.clone
+ new_options.each do |k,v| @options[k] = v end
+ end
+
+ # True if the command handles the given argument list.
+ def handles?(args)
+ begin
+ parser.parse!(args.dup)
+ return true
+ rescue
+ return false
+ end
+ end
+
+ # Handle the given list of arguments by parsing them and recording
+ # the results.
+ def handle_options(args)
+ args = add_extra_args(args)
+ @options = @defaults.clone
+ parser.parse!(args)
+ @options[:args] = args
+ end
+
+ def add_extra_args(args)
+ result = []
+ s_extra = Command.specific_extra_args(@command)
+ extra = Command.extra_args + s_extra
+ while ! extra.empty?
+ ex = []
+ ex << extra.shift
+ ex << extra.shift if extra.first.to_s =~ /^[^-]/
+ result << ex if handles?(ex)
+ end
+ result.flatten!
+ result.concat(args)
+ result
+ end
+
+ private
+
+ # Create on demand parser.
+ def parser
+ create_option_parser if @parser.nil?
+ @parser
+ end
+
+ def create_option_parser
+ @parser = OptionParser.new
+
+ @parser.separator("")
+ regular_options = @option_groups.delete :options
+
+ configure_options "", regular_options
+
+ @option_groups.sort_by { |n,_| n.to_s }.each do |group_name, option_list|
+ configure_options group_name, option_list
+ end
+
+ configure_options "Common", Command.common_options
+
+ @parser.separator("")
+ unless arguments.empty?
+ @parser.separator(" Arguments:")
+ arguments.split(/\n/).each do |arg_desc|
+ @parser.separator(" #{arg_desc}")
+ end
+ @parser.separator("")
+ end
+
+ @parser.separator(" Summary:")
+ wrap(@summary, 80 - 4).split("\n").each do |line|
+ @parser.separator(" #{line.strip}")
+ end
+
+ if description then
+ formatted = description.split("\n\n").map do |chunk|
+ wrap(chunk, 80 - 4)
+ end.join("\n")
+
+ @parser.separator ""
+ @parser.separator " Description:"
+ formatted.split("\n").each do |line|
+ @parser.separator " #{line.rstrip}"
+ end
+ end
+
+ unless defaults_str.empty?
+ @parser.separator("")
+ @parser.separator(" Defaults:")
+ defaults_str.split(/\n/).each do |line|
+ @parser.separator(" #{line}")
+ end
+ end
+ end
+
+ def configure_options(header, option_list)
+ return if option_list.nil? or option_list.empty?
+
+ header = header.to_s.empty? ? '' : "#{header} "
+ @parser.separator " #{header}Options:"
+
+ option_list.each do |args, handler|
+ dashes = args.select { |arg| arg =~ /^-/ }
+ @parser.on(*args) do |value|
+ handler.call(value, @options)
+ end
+ end
+
+ @parser.separator ''
+ end
+
+ # Wraps +text+ to +width+
+ def wrap(text, width)
+ text.gsub(/(.{1,#{width}})( +|$\n?)|(.{1,#{width}})/, "\\1\\3\n")
+ end
+
+ ##################################################################
+ # Class methods for Command.
+ class << self
+ def common_options
+ @common_options ||= []
+ end
+
+ def add_common_option(*args, &handler)
+ Gem::Command.common_options << [args, handler]
+ end
+
+ def extra_args
+ @extra_args ||= []
+ end
+
+ def extra_args=(value)
+ case value
+ when Array
+ @extra_args = value
+ when String
+ @extra_args = value.split
+ end
+ end
+
+ # Return an array of extra arguments for the command. The extra
+ # arguments come from the gem configuration file read at program
+ # startup.
+ def specific_extra_args(cmd)
+ specific_extra_args_hash[cmd]
+ end
+
+ # Add a list of extra arguments for the given command. +args+
+ # may be an array or a string to be split on white space.
+ def add_specific_extra_args(cmd,args)
+ args = args.split(/\s+/) if args.kind_of? String
+ specific_extra_args_hash[cmd] = args
+ end
+
+ # Accessor for the specific extra args hash (self initializing).
+ def specific_extra_args_hash
+ @specific_extra_args_hash ||= Hash.new do |h,k|
+ h[k] = Array.new
+ end
+ end
+ end
+
+ # ----------------------------------------------------------------
+ # Add the options common to all commands.
+
+ add_common_option('-h', '--help',
+ 'Get help on this command') do
+ |value, options|
+ options[:help] = true
+ end
+
+ add_common_option('-V', '--[no-]verbose',
+ 'Set the verbose level of output') do |value, options|
+ # Set us to "really verbose" so the progress meter works
+ if Gem.configuration.verbose and value then
+ Gem.configuration.verbose = 1
+ else
+ Gem.configuration.verbose = value
+ end
+ end
+
+ add_common_option('-q', '--quiet', 'Silence commands') do |value, options|
+ Gem.configuration.verbose = false
+ end
+
+ # Backtrace and config-file are added so they show up in the help
+ # commands. Both options are actually handled before the other
+ # options get parsed.
+
+ add_common_option('--config-file FILE',
+ "Use this config file instead of default") do
+ end
+
+ add_common_option('--backtrace',
+ 'Show stack backtrace on errors') do
+ end
+
+ add_common_option('--debug',
+ 'Turn on Ruby debugging') do
+ end
+
+ # :stopdoc:
+ HELP = %{
+ RubyGems is a sophisticated package manager for Ruby. This is a
+ basic help message containing pointers to more information.
+
+ Usage:
+ gem -h/--help
+ gem -v/--version
+ gem command [arguments...] [options...]
+
+ Examples:
+ gem install rake
+ gem list --local
+ gem build package.gemspec
+ gem help install
+
+ Further help:
+ gem help commands list all 'gem' commands
+ gem help examples show some examples of usage
+ gem help platforms show information about platforms
+ gem help <COMMAND> show help on COMMAND
+ (e.g. 'gem help install')
+ Further information:
+ http://rubygems.rubyforge.org
+ }.gsub(/^ /, "")
+
+ # :startdoc:
+
+ end # class
+
+ # This is where Commands will be placed in the namespace
+ module Commands; end
+
+end
diff --git a/ruby/lib/rubygems/command_manager.rb b/ruby/lib/rubygems/command_manager.rb
new file mode 100644
index 0000000..dd9a1ae
--- /dev/null
+++ b/ruby/lib/rubygems/command_manager.rb
@@ -0,0 +1,146 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'timeout'
+require 'rubygems/command'
+require 'rubygems/user_interaction'
+
+module Gem
+
+ ####################################################################
+ # The command manager registers and installs all the individual
+ # sub-commands supported by the gem command.
+ class CommandManager
+ include UserInteraction
+
+ # Return the authoritative instance of the command manager.
+ def self.instance
+ @command_manager ||= CommandManager.new
+ end
+
+ # Register all the subcommands supported by the gem command.
+ def initialize
+ @commands = {}
+ register_command :build
+ register_command :cert
+ register_command :check
+ register_command :cleanup
+ register_command :contents
+ register_command :dependency
+ register_command :environment
+ register_command :fetch
+ register_command :generate_index
+ register_command :help
+ register_command :install
+ register_command :list
+ register_command :lock
+ register_command :mirror
+ register_command :outdated
+ register_command :pristine
+ register_command :query
+ register_command :rdoc
+ register_command :search
+ register_command :server
+ register_command :sources
+ register_command :specification
+ register_command :stale
+ register_command :uninstall
+ register_command :unpack
+ register_command :update
+ register_command :which
+ end
+
+ # Register the command object.
+ def register_command(command_obj)
+ @commands[command_obj] = false
+ end
+
+ # Return the registered command from the command name.
+ def [](command_name)
+ command_name = command_name.intern
+ return nil if @commands[command_name].nil?
+ @commands[command_name] ||= load_and_instantiate(command_name)
+ end
+
+ # Return a list of all command names (as strings).
+ def command_names
+ @commands.keys.collect {|key| key.to_s}.sort
+ end
+
+ # Run the config specified by +args+.
+ def run(args)
+ process_args(args)
+ rescue StandardError, Timeout::Error => ex
+ alert_error "While executing gem ... (#{ex.class})\n #{ex.to_s}"
+ ui.errs.puts "\t#{ex.backtrace.join "\n\t"}" if
+ Gem.configuration.backtrace
+ terminate_interaction(1)
+ rescue Interrupt
+ alert_error "Interrupted"
+ terminate_interaction(1)
+ end
+
+ def process_args(args)
+ args = args.to_str.split(/\s+/) if args.respond_to?(:to_str)
+ if args.size == 0
+ say Gem::Command::HELP
+ terminate_interaction(1)
+ end
+ case args[0]
+ when '-h', '--help'
+ say Gem::Command::HELP
+ terminate_interaction(0)
+ when '-v', '--version'
+ say Gem::RubyGemsVersion
+ terminate_interaction(0)
+ when /^-/
+ alert_error "Invalid option: #{args[0]}. See 'gem --help'."
+ terminate_interaction(1)
+ else
+ cmd_name = args.shift.downcase
+ cmd = find_command(cmd_name)
+ cmd.invoke(*args)
+ end
+ end
+
+ def find_command(cmd_name)
+ possibilities = find_command_possibilities(cmd_name)
+ if possibilities.size > 1
+ raise "Ambiguous command #{cmd_name} matches [#{possibilities.join(', ')}]"
+ end
+ if possibilities.size < 1
+ raise "Unknown command #{cmd_name}"
+ end
+
+ self[possibilities.first]
+ end
+
+ def find_command_possibilities(cmd_name)
+ len = cmd_name.length
+ self.command_names.select { |n| cmd_name == n[0,len] }
+ end
+
+ private
+
+ def load_and_instantiate(command_name)
+ command_name = command_name.to_s
+ retried = false
+
+ begin
+ const_name = command_name.capitalize.gsub(/_(.)/) { $1.upcase }
+ Gem::Commands.const_get("#{const_name}Command").new
+ rescue NameError
+ if retried then
+ raise
+ else
+ retried = true
+ require "rubygems/commands/#{command_name}_command"
+ retry
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rubygems/commands/build_command.rb b/ruby/lib/rubygems/commands/build_command.rb
new file mode 100644
index 0000000..e1f0122
--- /dev/null
+++ b/ruby/lib/rubygems/commands/build_command.rb
@@ -0,0 +1,53 @@
+require 'rubygems/command'
+require 'rubygems/builder'
+
+class Gem::Commands::BuildCommand < Gem::Command
+
+ def initialize
+ super('build', 'Build a gem from a gemspec')
+ end
+
+ def arguments # :nodoc:
+ "GEMSPEC_FILE gemspec file name to build a gem for"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMSPEC_FILE"
+ end
+
+ def execute
+ gemspec = get_one_gem_name
+ if File.exist?(gemspec)
+ specs = load_gemspecs(gemspec)
+ specs.each do |spec|
+ Gem::Builder.new(spec).build
+ end
+ else
+ alert_error "Gemspec file not found: #{gemspec}"
+ end
+ end
+
+ def load_gemspecs(filename)
+ if yaml?(filename)
+ result = []
+ open(filename) do |f|
+ begin
+ while not f.eof? and spec = Gem::Specification.from_yaml(f)
+ result << spec
+ end
+ rescue Gem::EndOfYAMLException => e
+ # OK
+ end
+ end
+ else
+ result = [Gem::Specification.load(filename)]
+ end
+ result
+ end
+
+ def yaml?(filename)
+ line = open(filename) { |f| line = f.gets }
+ result = line =~ %r{!ruby/object:Gem::Specification}
+ result
+ end
+end
diff --git a/ruby/lib/rubygems/commands/cert_command.rb b/ruby/lib/rubygems/commands/cert_command.rb
new file mode 100644
index 0000000..f5b6988
--- /dev/null
+++ b/ruby/lib/rubygems/commands/cert_command.rb
@@ -0,0 +1,86 @@
+require 'rubygems/command'
+require 'rubygems/security'
+
+class Gem::Commands::CertCommand < Gem::Command
+
+ def initialize
+ super 'cert', 'Manage RubyGems certificates and signing settings'
+
+ add_option('-a', '--add CERT',
+ 'Add a trusted certificate.') do |value, options|
+ cert = OpenSSL::X509::Certificate.new(File.read(value))
+ Gem::Security.add_trusted_cert(cert)
+ say "Added '#{cert.subject.to_s}'"
+ end
+
+ add_option('-l', '--list',
+ 'List trusted certificates.') do |value, options|
+ glob_str = File::join(Gem::Security::OPT[:trust_dir], '*.pem')
+ Dir::glob(glob_str) do |path|
+ begin
+ cert = OpenSSL::X509::Certificate.new(File.read(path))
+ # this could probably be formatted more gracefully
+ say cert.subject.to_s
+ rescue OpenSSL::X509::CertificateError
+ next
+ end
+ end
+ end
+
+ add_option('-r', '--remove STRING',
+ 'Remove trusted certificates containing',
+ 'STRING.') do |value, options|
+ trust_dir = Gem::Security::OPT[:trust_dir]
+ glob_str = File::join(trust_dir, '*.pem')
+
+ Dir::glob(glob_str) do |path|
+ begin
+ cert = OpenSSL::X509::Certificate.new(File.read(path))
+ if cert.subject.to_s.downcase.index(value)
+ say "Removed '#{cert.subject.to_s}'"
+ File.unlink(path)
+ end
+ rescue OpenSSL::X509::CertificateError
+ next
+ end
+ end
+ end
+
+ add_option('-b', '--build EMAIL_ADDR',
+ 'Build private key and self-signed',
+ 'certificate for EMAIL_ADDR.') do |value, options|
+ vals = Gem::Security.build_self_signed_cert(value)
+ File.chmod 0600, vals[:key_path]
+ say "Public Cert: #{vals[:cert_path]}"
+ say "Private Key: #{vals[:key_path]}"
+ say "Don't forget to move the key file to somewhere private..."
+ end
+
+ add_option('-C', '--certificate CERT',
+ 'Certificate for --sign command.') do |value, options|
+ cert = OpenSSL::X509::Certificate.new(File.read(value))
+ Gem::Security::OPT[:issuer_cert] = cert
+ end
+
+ add_option('-K', '--private-key KEY',
+ 'Private key for --sign command.') do |value, options|
+ key = OpenSSL::PKey::RSA.new(File.read(value))
+ Gem::Security::OPT[:issuer_key] = key
+ end
+
+ add_option('-s', '--sign NEWCERT',
+ 'Sign a certificate with my key and',
+ 'certificate.') do |value, options|
+ cert = OpenSSL::X509::Certificate.new(File.read(value))
+ my_cert = Gem::Security::OPT[:issuer_cert]
+ my_key = Gem::Security::OPT[:issuer_key]
+ cert = Gem::Security.sign_cert(cert, my_key, my_cert)
+ File.open(value, 'wb') { |file| file.write(cert.to_pem) }
+ end
+ end
+
+ def execute
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/check_command.rb b/ruby/lib/rubygems/commands/check_command.rb
new file mode 100644
index 0000000..17c2c8f
--- /dev/null
+++ b/ruby/lib/rubygems/commands/check_command.rb
@@ -0,0 +1,75 @@
+require 'rubygems/command'
+require 'rubygems/version_option'
+require 'rubygems/validator'
+
+class Gem::Commands::CheckCommand < Gem::Command
+
+ include Gem::VersionOption
+
+ def initialize
+ super 'check', 'Check installed gems',
+ :verify => false, :alien => false
+
+ add_option( '--verify FILE',
+ 'Verify gem file against its internal',
+ 'checksum') do |value, options|
+ options[:verify] = value
+ end
+
+ add_option('-a', '--alien', "Report 'unmanaged' or rogue files in the",
+ "gem repository") do |value, options|
+ options[:alien] = true
+ end
+
+ add_option('-t', '--test', "Run unit tests for gem") do |value, options|
+ options[:test] = true
+ end
+
+ add_version_option 'run tests for'
+ end
+
+ def execute
+ if options[:test]
+ version = options[:version] || Gem::Requirement.default
+ dep = Gem::Dependency.new get_one_gem_name, version
+ gem_spec = Gem::SourceIndex.from_installed_gems.search(dep).first
+ Gem::Validator.new.unit_test(gem_spec)
+ end
+
+ if options[:alien]
+ say "Performing the 'alien' operation"
+ Gem::Validator.new.alien.each do |key, val|
+ if(val.size > 0)
+ say "#{key} has #{val.size} problems"
+ val.each do |error_entry|
+ say "\t#{error_entry.path}:"
+ say "\t#{error_entry.problem}"
+ say
+ end
+ else
+ say "#{key} is error-free"
+ end
+ say
+ end
+ end
+
+ if options[:verify]
+ gem_name = options[:verify]
+ unless gem_name
+ alert_error "Must specify a .gem file with --verify NAME"
+ return
+ end
+ unless File.exist?(gem_name)
+ alert_error "Unknown file: #{gem_name}."
+ return
+ end
+ say "Verifying gem: '#{gem_name}'"
+ begin
+ Gem::Validator.new.verify_gem_file(gem_name)
+ rescue Exception => e
+ alert_error "#{gem_name} is invalid."
+ end
+ end
+ end
+
+end
diff --git a/ruby/lib/rubygems/commands/cleanup_command.rb b/ruby/lib/rubygems/commands/cleanup_command.rb
new file mode 100644
index 0000000..40dcb9d
--- /dev/null
+++ b/ruby/lib/rubygems/commands/cleanup_command.rb
@@ -0,0 +1,91 @@
+require 'rubygems/command'
+require 'rubygems/source_index'
+require 'rubygems/dependency_list'
+
+class Gem::Commands::CleanupCommand < Gem::Command
+
+ def initialize
+ super 'cleanup',
+ 'Clean up old versions of installed gems in the local repository',
+ :force => false, :test => false, :install_dir => Gem.dir
+
+ add_option('-d', '--dryrun', "") do |value, options|
+ options[:dryrun] = true
+ end
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to cleanup"
+ end
+
+ def defaults_str # :nodoc:
+ "--no-dryrun"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} [GEMNAME ...]"
+ end
+
+ def execute
+ say "Cleaning up installed gems..."
+ primary_gems = {}
+
+ Gem.source_index.each do |name, spec|
+ if primary_gems[spec.name].nil? or
+ primary_gems[spec.name].version < spec.version then
+ primary_gems[spec.name] = spec
+ end
+ end
+
+ gems_to_cleanup = []
+
+ unless options[:args].empty? then
+ options[:args].each do |gem_name|
+ specs = Gem.cache.search(/^#{gem_name}$/i)
+ specs.each do |spec|
+ gems_to_cleanup << spec
+ end
+ end
+ else
+ Gem.source_index.each do |name, spec|
+ gems_to_cleanup << spec
+ end
+ end
+
+ gems_to_cleanup = gems_to_cleanup.select { |spec|
+ primary_gems[spec.name].version != spec.version
+ }
+
+ uninstall_command = Gem::CommandManager.instance['uninstall']
+ deplist = Gem::DependencyList.new
+ gems_to_cleanup.uniq.each do |spec| deplist.add spec end
+
+ deps = deplist.strongly_connected_components.flatten.reverse
+
+ deps.each do |spec|
+ if options[:dryrun] then
+ say "Dry Run Mode: Would uninstall #{spec.full_name}"
+ else
+ say "Attempting to uninstall #{spec.full_name}"
+
+ options[:args] = [spec.name]
+ options[:version] = "= #{spec.version}"
+ options[:executables] = false
+
+ uninstaller = Gem::Uninstaller.new spec.name, options
+
+ begin
+ uninstaller.uninstall
+ rescue Gem::DependencyRemovalException,
+ Gem::GemNotInHomeException => e
+ say "Unable to uninstall #{spec.full_name}:"
+ say "\t#{e.class}: #{e.message}"
+ end
+ end
+ end
+
+ say "Clean Up Complete"
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/contents_command.rb b/ruby/lib/rubygems/commands/contents_command.rb
new file mode 100644
index 0000000..bc75fb5
--- /dev/null
+++ b/ruby/lib/rubygems/commands/contents_command.rb
@@ -0,0 +1,74 @@
+require 'rubygems/command'
+require 'rubygems/version_option'
+
+class Gem::Commands::ContentsCommand < Gem::Command
+
+ include Gem::VersionOption
+
+ def initialize
+ super 'contents', 'Display the contents of the installed gems',
+ :specdirs => [], :lib_only => false
+
+ add_version_option
+
+ add_option('-s', '--spec-dir a,b,c', Array,
+ "Search for gems under specific paths") do |spec_dirs, options|
+ options[:specdirs] = spec_dirs
+ end
+
+ add_option('-l', '--[no-]lib-only',
+ "Only return files in the Gem's lib_dirs") do |lib_only, options|
+ options[:lib_only] = lib_only
+ end
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to list contents for"
+ end
+
+ def defaults_str # :nodoc:
+ "--no-lib-only"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME"
+ end
+
+ def execute
+ version = options[:version] || Gem::Requirement.default
+ gem = get_one_gem_name
+
+ s = options[:specdirs].map do |i|
+ [i, File.join(i, "specifications")]
+ end.flatten
+
+ path_kind = if s.empty? then
+ s = Gem::SourceIndex.installed_spec_directories
+ "default gem paths"
+ else
+ "specified path"
+ end
+
+ si = Gem::SourceIndex.from_gems_in(*s)
+
+ gem_spec = si.find_name(gem, version).last
+
+ unless gem_spec then
+ say "Unable to find gem '#{gem}' in #{path_kind}"
+
+ if Gem.configuration.verbose then
+ say "\nDirectories searched:"
+ s.each { |dir| say dir }
+ end
+
+ terminate_interaction
+ end
+
+ files = options[:lib_only] ? gem_spec.lib_files : gem_spec.files
+ files.each do |f|
+ say File.join(gem_spec.full_gem_path, f)
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/dependency_command.rb b/ruby/lib/rubygems/commands/dependency_command.rb
new file mode 100644
index 0000000..44b269b
--- /dev/null
+++ b/ruby/lib/rubygems/commands/dependency_command.rb
@@ -0,0 +1,188 @@
+require 'rubygems/command'
+require 'rubygems/local_remote_options'
+require 'rubygems/version_option'
+require 'rubygems/source_info_cache'
+
+class Gem::Commands::DependencyCommand < Gem::Command
+
+ include Gem::LocalRemoteOptions
+ include Gem::VersionOption
+
+ def initialize
+ super 'dependency',
+ 'Show the dependencies of an installed gem',
+ :version => Gem::Requirement.default, :domain => :local
+
+ add_version_option
+ add_platform_option
+
+ add_option('-R', '--[no-]reverse-dependencies',
+ 'Include reverse dependencies in the output') do
+ |value, options|
+ options[:reverse_dependencies] = value
+ end
+
+ add_option('-p', '--pipe',
+ "Pipe Format (name --version ver)") do |value, options|
+ options[:pipe_format] = value
+ end
+
+ add_local_remote_options
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to show dependencies for"
+ end
+
+ def defaults_str # :nodoc:
+ "--local --version '#{Gem::Requirement.default}' --no-reverse-dependencies"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME"
+ end
+
+ def execute
+ options[:args] << '' if options[:args].empty?
+ specs = {}
+
+ source_indexes = Hash.new do |h, source_uri|
+ h[source_uri] = Gem::SourceIndex.new
+ end
+
+ pattern = if options[:args].length == 1 and
+ options[:args].first =~ /\A\/(.*)\/(i)?\z/m then
+ flags = $2 ? Regexp::IGNORECASE : nil
+ Regexp.new $1, flags
+ else
+ /\A#{Regexp.union(*options[:args])}/
+ end
+
+ dependency = Gem::Dependency.new pattern, options[:version]
+
+ if options[:reverse_dependencies] and remote? and not local? then
+ alert_error 'Only reverse dependencies for local gems are supported.'
+ terminate_interaction 1
+ end
+
+ if local? then
+ Gem.source_index.search(dependency).each do |spec|
+ source_indexes[:local].add_spec spec
+ end
+ end
+
+ if remote? and not options[:reverse_dependencies] then
+ fetcher = Gem::SpecFetcher.fetcher
+
+ begin
+ fetcher.find_matching(dependency).each do |spec_tuple, source_uri|
+ spec = fetcher.fetch_spec spec_tuple, URI.parse(source_uri)
+
+ source_indexes[source_uri].add_spec spec
+ end
+ rescue Gem::RemoteFetcher::FetchError => e
+ raise unless fetcher.warn_legacy e do
+ require 'rubygems/source_info_cache'
+
+ specs = Gem::SourceInfoCache.search_with_source dependency, false
+
+ specs.each do |spec, source_uri|
+ source_indexes[source_uri].add_spec spec
+ end
+ end
+ end
+ end
+
+ if source_indexes.empty? then
+ patterns = options[:args].join ','
+ say "No gems found matching #{patterns} (#{options[:version]})" if
+ Gem.configuration.verbose
+
+ terminate_interaction 1
+ end
+
+ specs = {}
+
+ source_indexes.values.each do |source_index|
+ source_index.gems.each do |name, spec|
+ specs[spec.full_name] = [source_index, spec]
+ end
+ end
+
+ reverse = Hash.new { |h, k| h[k] = [] }
+
+ if options[:reverse_dependencies] then
+ specs.values.each do |_, spec|
+ reverse[spec.full_name] = find_reverse_dependencies spec
+ end
+ end
+
+ if options[:pipe_format] then
+ specs.values.sort_by { |_, spec| spec }.each do |_, spec|
+ unless spec.dependencies.empty?
+ spec.dependencies.each do |dep|
+ say "#{dep.name} --version '#{dep.version_requirements}'"
+ end
+ end
+ end
+ else
+ response = ''
+
+ specs.values.sort_by { |_, spec| spec }.each do |_, spec|
+ response << print_dependencies(spec)
+ unless reverse[spec.full_name].empty? then
+ response << " Used by\n"
+ reverse[spec.full_name].each do |sp, dep|
+ response << " #{sp} (#{dep})\n"
+ end
+ end
+ response << "\n"
+ end
+
+ say response
+ end
+ end
+
+ def print_dependencies(spec, level = 0)
+ response = ''
+ response << ' ' * level + "Gem #{spec.full_name}\n"
+ unless spec.dependencies.empty? then
+ spec.dependencies.each do |dep|
+ response << ' ' * level + " #{dep}\n"
+ end
+ end
+ response
+ end
+
+ # Retuns list of [specification, dep] that are satisfied by spec.
+ def find_reverse_dependencies(spec)
+ result = []
+
+ Gem.source_index.each do |name, sp|
+ sp.dependencies.each do |dep|
+ dep = Gem::Dependency.new(*dep) unless Gem::Dependency === dep
+
+ if spec.name == dep.name and
+ dep.version_requirements.satisfied_by?(spec.version) then
+ result << [sp.full_name, dep]
+ end
+ end
+ end
+
+ result
+ end
+
+ def find_gems(name, source_index)
+ specs = {}
+
+ spec_list = source_index.search name, options[:version]
+
+ spec_list.each do |spec|
+ specs[spec.full_name] = [source_index, spec]
+ end
+
+ specs
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/environment_command.rb b/ruby/lib/rubygems/commands/environment_command.rb
new file mode 100644
index 0000000..e672da5
--- /dev/null
+++ b/ruby/lib/rubygems/commands/environment_command.rb
@@ -0,0 +1,128 @@
+require 'rubygems/command'
+
+class Gem::Commands::EnvironmentCommand < Gem::Command
+
+ def initialize
+ super 'environment', 'Display information about the RubyGems environment'
+ end
+
+ def arguments # :nodoc:
+ args = <<-EOF
+ packageversion display the package version
+ gemdir display the path where gems are installed
+ gempath display path used to search for gems
+ version display the gem format version
+ remotesources display the remote gem servers
+ <omitted> display everything
+ EOF
+ return args.gsub(/^\s+/, '')
+ end
+
+ def description # :nodoc:
+ <<-EOF
+The RubyGems environment can be controlled through command line arguments,
+gemrc files, environment variables and built-in defaults.
+
+Command line argument defaults and some RubyGems defaults can be set in
+~/.gemrc file for individual users and a /etc/gemrc for all users. A gemrc
+is a YAML file with the following YAML keys:
+
+ :sources: A YAML array of remote gem repositories to install gems from
+ :verbose: Verbosity of the gem command. false, true, and :really are the
+ levels
+ :update_sources: Enable/disable automatic updating of repository metadata
+ :backtrace: Print backtrace when RubyGems encounters an error
+ :bulk_threshold: Switch to a bulk update when this many sources are out of
+ date (legacy setting)
+ :gempath: The paths in which to look for gems
+ gem_command: A string containing arguments for the specified gem command
+
+Example:
+
+ :verbose: false
+ install: --no-wrappers
+ update: --no-wrappers
+
+RubyGems' default local repository can be overriden with the GEM_PATH and
+GEM_HOME environment variables. GEM_HOME sets the default repository to
+install into. GEM_PATH allows multiple local repositories to be searched for
+gems.
+
+If you are behind a proxy server, RubyGems uses the HTTP_PROXY,
+HTTP_PROXY_USER and HTTP_PROXY_PASS environment variables to discover the
+proxy server.
+
+If you are packaging RubyGems all of RubyGems' defaults are in
+lib/rubygems/defaults.rb. You may override these in
+lib/rubygems/defaults/operating_system.rb
+ EOF
+ end
+
+ def usage # :nodoc:
+ "#{program_name} [arg]"
+ end
+
+ def execute
+ out = ''
+ arg = options[:args][0]
+ case arg
+ when /^packageversion/ then
+ out << Gem::RubyGemsPackageVersion
+ when /^version/ then
+ out << Gem::RubyGemsVersion
+ when /^gemdir/, /^gemhome/, /^home/, /^GEM_HOME/ then
+ out << Gem.dir
+ when /^gempath/, /^path/, /^GEM_PATH/ then
+ out << Gem.path.join(File::PATH_SEPARATOR)
+ when /^remotesources/ then
+ out << Gem.sources.join("\n")
+ when nil then
+ out = "RubyGems Environment:\n"
+
+ out << " - RUBYGEMS VERSION: #{Gem::RubyGemsVersion}\n"
+
+ out << " - RUBY VERSION: #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}"
+ out << " patchlevel #{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
+ out << ") [#{RUBY_PLATFORM}]\n"
+
+ out << " - INSTALLATION DIRECTORY: #{Gem.dir}\n"
+
+ out << " - RUBYGEMS PREFIX: #{Gem.prefix}\n" unless Gem.prefix.nil?
+
+ out << " - RUBY EXECUTABLE: #{Gem.ruby}\n"
+
+ out << " - EXECUTABLE DIRECTORY: #{Gem.bindir}\n"
+
+ out << " - RUBYGEMS PLATFORMS:\n"
+ Gem.platforms.each do |platform|
+ out << " - #{platform}\n"
+ end
+
+ out << " - GEM PATHS:\n"
+ out << " - #{Gem.dir}\n"
+
+ path = Gem.path.dup
+ path.delete Gem.dir
+ path.each do |p|
+ out << " - #{p}\n"
+ end
+
+ out << " - GEM CONFIGURATION:\n"
+ Gem.configuration.each do |name, value|
+ out << " - #{name.inspect} => #{value.inspect}\n"
+ end
+
+ out << " - REMOTE SOURCES:\n"
+ Gem.sources.each do |s|
+ out << " - #{s}\n"
+ end
+
+ else
+ fail Gem::CommandLineError, "Unknown enviroment option [#{arg}]"
+ end
+ say out
+ true
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/fetch_command.rb b/ruby/lib/rubygems/commands/fetch_command.rb
new file mode 100644
index 0000000..76c9924
--- /dev/null
+++ b/ruby/lib/rubygems/commands/fetch_command.rb
@@ -0,0 +1,62 @@
+require 'rubygems/command'
+require 'rubygems/local_remote_options'
+require 'rubygems/version_option'
+require 'rubygems/source_info_cache'
+
+class Gem::Commands::FetchCommand < Gem::Command
+
+ include Gem::LocalRemoteOptions
+ include Gem::VersionOption
+
+ def initialize
+ super 'fetch', 'Download a gem and place it in the current directory'
+
+ add_bulk_threshold_option
+ add_proxy_option
+ add_source_option
+
+ add_version_option
+ add_platform_option
+ end
+
+ def arguments # :nodoc:
+ 'GEMNAME name of gem to download'
+ end
+
+ def defaults_str # :nodoc:
+ "--version '#{Gem::Requirement.default}'"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME [GEMNAME ...]"
+ end
+
+ def execute
+ version = options[:version] || Gem::Requirement.default
+ all = Gem::Requirement.default
+
+ gem_names = get_all_gem_names
+
+ gem_names.each do |gem_name|
+ dep = Gem::Dependency.new gem_name, version
+
+ specs_and_sources = Gem::SpecFetcher.fetcher.fetch dep, all
+
+ specs_and_sources.sort_by { |spec,| spec.version }
+
+ spec, source_uri = specs_and_sources.last
+
+ if spec.nil? then
+ alert_error "Could not find #{gem_name} in any repository"
+ next
+ end
+
+ path = Gem::RemoteFetcher.fetcher.download spec, source_uri
+ FileUtils.mv path, "#{spec.full_name}.gem"
+
+ say "Downloaded #{spec.full_name}"
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/generate_index_command.rb b/ruby/lib/rubygems/commands/generate_index_command.rb
new file mode 100644
index 0000000..1bd8756
--- /dev/null
+++ b/ruby/lib/rubygems/commands/generate_index_command.rb
@@ -0,0 +1,57 @@
+require 'rubygems/command'
+require 'rubygems/indexer'
+
+class Gem::Commands::GenerateIndexCommand < Gem::Command
+
+ def initialize
+ super 'generate_index',
+ 'Generates the index files for a gem server directory',
+ :directory => '.'
+
+ add_option '-d', '--directory=DIRNAME',
+ 'repository base dir containing gems subdir' do |dir, options|
+ options[:directory] = File.expand_path dir
+ end
+ end
+
+ def defaults_str # :nodoc:
+ "--directory ."
+ end
+
+ def description # :nodoc:
+ <<-EOF
+The generate_index command creates a set of indexes for serving gems
+statically. The command expects a 'gems' directory under the path given to
+the --directory option. When done, it will generate a set of files like this:
+
+ gems/ # .gem files you want to index
+ quick/index
+ quick/index.rz # quick index manifest
+ quick/<gemname>.gemspec.rz # legacy YAML quick index file
+ quick/Marshal.<version>/<gemname>.gemspec.rz # Marshal quick index file
+ Marshal.<version>
+ Marshal.<version>.Z # Marshal full index
+ yaml
+ yaml.Z # legacy YAML full index
+
+The .Z and .rz extension files are compressed with the inflate algorithm. The
+Marshal version number comes from ruby's Marshal::MAJOR_VERSION and
+Marshal::MINOR_VERSION constants. It is used to ensure compatibility. The
+yaml indexes exist for legacy RubyGems clients and fallback in case of Marshal
+version changes.
+ EOF
+ end
+
+ def execute
+ if not File.exist?(options[:directory]) or
+ not File.directory?(options[:directory]) then
+ alert_error "unknown directory name #{directory}."
+ terminate_interaction 1
+ else
+ indexer = Gem::Indexer.new options[:directory]
+ indexer.generate_index
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/help_command.rb b/ruby/lib/rubygems/commands/help_command.rb
new file mode 100644
index 0000000..0c4a4ec
--- /dev/null
+++ b/ruby/lib/rubygems/commands/help_command.rb
@@ -0,0 +1,172 @@
+require 'rubygems/command'
+
+class Gem::Commands::HelpCommand < Gem::Command
+
+ # :stopdoc:
+ EXAMPLES = <<-EOF
+Some examples of 'gem' usage.
+
+* Install 'rake', either from local directory or remote server:
+
+ gem install rake
+
+* Install 'rake', only from remote server:
+
+ gem install rake --remote
+
+* Install 'rake' from remote server, and run unit tests,
+ and generate RDocs:
+
+ gem install --remote rake --test --rdoc --ri
+
+* Install 'rake', but only version 0.3.1, even if dependencies
+ are not met, and into a user-specific directory:
+
+ gem install rake --version 0.3.1 --force --user-install
+
+* List local gems whose name begins with 'D':
+
+ gem list D
+
+* List local and remote gems whose name contains 'log':
+
+ gem search log --both
+
+* List only remote gems whose name contains 'log':
+
+ gem search log --remote
+
+* Uninstall 'rake':
+
+ gem uninstall rake
+
+* Create a gem:
+
+ See http://rubygems.rubyforge.org/wiki/wiki.pl?CreateAGemInTenMinutes
+
+* See information about RubyGems:
+
+ gem environment
+
+* Update all gems on your system:
+
+ gem update
+ EOF
+
+ PLATFORMS = <<-'EOF'
+RubyGems platforms are composed of three parts, a CPU, an OS, and a
+version. These values are taken from values in rbconfig.rb. You can view
+your current platform by running `gem environment`.
+
+RubyGems matches platforms as follows:
+
+ * The CPU must match exactly, unless one of the platforms has
+ "universal" as the CPU.
+ * The OS must match exactly.
+ * The versions must match exactly unless one of the versions is nil.
+
+For commands that install, uninstall and list gems, you can override what
+RubyGems thinks your platform is with the --platform option. The platform
+you pass must match "#{cpu}-#{os}" or "#{cpu}-#{os}-#{version}". On mswin
+platforms, the version is the compiler version, not the OS version. (Ruby
+compiled with VC6 uses "60" as the compiler version, VC8 uses "80".)
+
+Example platforms:
+
+ x86-freebsd # Any FreeBSD version on an x86 CPU
+ universal-darwin-8 # Darwin 8 only gems that run on any CPU
+ x86-mswin32-80 # Windows gems compiled with VC8
+
+When building platform gems, set the platform in the gem specification to
+Gem::Platform::CURRENT. This will correctly mark the gem with your ruby's
+platform.
+ EOF
+ # :startdoc:
+
+ def initialize
+ super 'help', "Provide help on the 'gem' command"
+ end
+
+ def arguments # :nodoc:
+ args = <<-EOF
+ commands List all 'gem' commands
+ examples Show examples of 'gem' usage
+ <command> Show specific help for <command>
+ EOF
+ return args.gsub(/^\s+/, '')
+ end
+
+ def usage # :nodoc:
+ "#{program_name} ARGUMENT"
+ end
+
+ def execute
+ command_manager = Gem::CommandManager.instance
+ arg = options[:args][0]
+
+ if begins? "commands", arg then
+ out = []
+ out << "GEM commands are:"
+ out << nil
+
+ margin_width = 4
+
+ desc_width = command_manager.command_names.map { |n| n.size }.max + 4
+
+ summary_width = 80 - margin_width - desc_width
+ wrap_indent = ' ' * (margin_width + desc_width)
+ format = "#{' ' * margin_width}%-#{desc_width}s%s"
+
+ command_manager.command_names.each do |cmd_name|
+ summary = command_manager[cmd_name].summary
+ summary = wrap(summary, summary_width).split "\n"
+ out << sprintf(format, cmd_name, summary.shift)
+ until summary.empty? do
+ out << "#{wrap_indent}#{summary.shift}"
+ end
+ end
+
+ out << nil
+ out << "For help on a particular command, use 'gem help COMMAND'."
+ out << nil
+ out << "Commands may be abbreviated, so long as they are unambiguous."
+ out << "e.g. 'gem i rake' is short for 'gem install rake'."
+
+ say out.join("\n")
+
+ elsif begins? "options", arg then
+ say Gem::Command::HELP
+
+ elsif begins? "examples", arg then
+ say EXAMPLES
+
+ elsif begins? "platforms", arg then
+ say PLATFORMS
+
+ elsif options[:help] then
+ command = command_manager[options[:help]]
+ if command
+ # help with provided command
+ command.invoke("--help")
+ else
+ alert_error "Unknown command #{options[:help]}. Try 'gem help commands'"
+ end
+
+ elsif arg then
+ possibilities = command_manager.find_command_possibilities(arg.downcase)
+ if possibilities.size == 1
+ command = command_manager[possibilities.first]
+ command.invoke("--help")
+ elsif possibilities.size > 1
+ alert_warning "Ambiguous command #{arg} (#{possibilities.join(', ')})"
+ else
+ alert_warning "Unknown command #{arg}. Try gem help commands"
+ end
+
+ else
+ say Gem::Command::HELP
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/install_command.rb b/ruby/lib/rubygems/commands/install_command.rb
new file mode 100644
index 0000000..1a6eb68
--- /dev/null
+++ b/ruby/lib/rubygems/commands/install_command.rb
@@ -0,0 +1,148 @@
+require 'rubygems/command'
+require 'rubygems/doc_manager'
+require 'rubygems/install_update_options'
+require 'rubygems/dependency_installer'
+require 'rubygems/local_remote_options'
+require 'rubygems/validator'
+require 'rubygems/version_option'
+
+class Gem::Commands::InstallCommand < Gem::Command
+
+ include Gem::VersionOption
+ include Gem::LocalRemoteOptions
+ include Gem::InstallUpdateOptions
+
+ def initialize
+ defaults = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({
+ :generate_rdoc => true,
+ :generate_ri => true,
+ :format_executable => false,
+ :test => false,
+ :version => Gem::Requirement.default,
+ })
+
+ super 'install', 'Install a gem into the local repository', defaults
+
+ add_install_update_options
+ add_local_remote_options
+ add_platform_option
+ add_version_option
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to install"
+ end
+
+ def defaults_str # :nodoc:
+ "--both --version '#{Gem::Requirement.default}' --rdoc --ri --no-force\n" \
+ "--no-test --install-dir #{Gem.dir}"
+ end
+
+ def description # :nodoc:
+ <<-EOF
+The install command installs local or remote gem into a gem repository.
+
+For gems with executables ruby installs a wrapper file into the executable
+directory by deault. This can be overridden with the --no-wrappers option.
+The wrapper allows you to choose among alternate gem versions using _version_.
+
+For example `rake _0.7.3_ --version` will run rake version 0.7.3 if a newer
+version is also installed.
+ EOF
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME [GEMNAME ...] [options] -- --build-flags"
+ end
+
+ def execute
+ if options[:include_dependencies] then
+ alert "`gem install -y` is now default and will be removed"
+ alert "use --ignore-dependencies to install only the gems you list"
+ end
+
+ installed_gems = []
+
+ ENV.delete 'GEM_PATH' if options[:install_dir].nil? and RUBY_VERSION > '1.9'
+
+ install_options = {
+ :env_shebang => options[:env_shebang],
+ :domain => options[:domain],
+ :force => options[:force],
+ :format_executable => options[:format_executable],
+ :ignore_dependencies => options[:ignore_dependencies],
+ :install_dir => options[:install_dir],
+ :security_policy => options[:security_policy],
+ :wrappers => options[:wrappers],
+ :bin_dir => options[:bin_dir],
+ :development => options[:development],
+ }
+
+ exit_code = 0
+
+ get_all_gem_names.each do |gem_name|
+ begin
+ inst = Gem::DependencyInstaller.new install_options
+ inst.install gem_name, options[:version]
+
+ inst.installed_gems.each do |spec|
+ say "Successfully installed #{spec.full_name}"
+ end
+
+ installed_gems.push(*inst.installed_gems)
+ rescue Gem::InstallError => e
+ alert_error "Error installing #{gem_name}:\n\t#{e.message}"
+ exit_code |= 1
+ rescue Gem::GemNotFoundException => e
+ alert_error e.message
+ exit_code |= 2
+# rescue => e
+# # TODO: Fix this handle to allow the error to propagate to
+# # the top level handler. Examine the other errors as
+# # well. This implementation here looks suspicious to me --
+# # JimWeirich (4/Jan/05)
+# alert_error "Error installing gem #{gem_name}: #{e.message}"
+# return
+ end
+ end
+
+ unless installed_gems.empty? then
+ gems = installed_gems.length == 1 ? 'gem' : 'gems'
+ say "#{installed_gems.length} #{gems} installed"
+ end
+
+ # NOTE: *All* of the RI documents must be generated first.
+ # For some reason, RI docs cannot be generated after any RDoc
+ # documents are generated.
+
+ if options[:generate_ri] then
+ installed_gems.each do |gem|
+ Gem::DocManager.new(gem, options[:rdoc_args]).generate_ri
+ end
+
+ Gem::DocManager.update_ri_cache
+ end
+
+ if options[:generate_rdoc] then
+ installed_gems.each do |gem|
+ Gem::DocManager.new(gem, options[:rdoc_args]).generate_rdoc
+ end
+ end
+
+ if options[:test] then
+ installed_gems.each do |spec|
+ gem_spec = Gem::SourceIndex.from_installed_gems.search(spec.name, spec.version.version).first
+ result = Gem::Validator.new.unit_test(gem_spec)
+ if result and not result.passed?
+ unless ask_yes_no("...keep Gem?", true) then
+ Gem::Uninstaller.new(spec.name, :version => spec.version.version).uninstall
+ end
+ end
+ end
+ end
+
+ raise Gem::SystemExitException, exit_code
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/list_command.rb b/ruby/lib/rubygems/commands/list_command.rb
new file mode 100644
index 0000000..f3e5da9
--- /dev/null
+++ b/ruby/lib/rubygems/commands/list_command.rb
@@ -0,0 +1,35 @@
+require 'rubygems/command'
+require 'rubygems/commands/query_command'
+
+##
+# An alternate to Gem::Commands::QueryCommand that searches for gems starting
+# with the the supplied argument.
+
+class Gem::Commands::ListCommand < Gem::Commands::QueryCommand
+
+ def initialize
+ super 'list', 'Display gems whose name starts with STRING'
+
+ remove_option('--name-matches')
+ end
+
+ def arguments # :nodoc:
+ "STRING start of gem name to look for"
+ end
+
+ def defaults_str # :nodoc:
+ "--local --no-details"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} [STRING]"
+ end
+
+ def execute
+ string = get_one_optional_argument || ''
+ options[:name] = /^#{string}/i
+ super
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/lock_command.rb b/ruby/lib/rubygems/commands/lock_command.rb
new file mode 100644
index 0000000..5a43978
--- /dev/null
+++ b/ruby/lib/rubygems/commands/lock_command.rb
@@ -0,0 +1,110 @@
+require 'rubygems/command'
+
+class Gem::Commands::LockCommand < Gem::Command
+
+ def initialize
+ super 'lock', 'Generate a lockdown list of gems',
+ :strict => false
+
+ add_option '-s', '--[no-]strict',
+ 'fail if unable to satisfy a dependency' do |strict, options|
+ options[:strict] = strict
+ end
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to lock\nVERSION version of gem to lock"
+ end
+
+ def defaults_str # :nodoc:
+ "--no-strict"
+ end
+
+ def description # :nodoc:
+ <<-EOF
+The lock command will generate a list of +gem+ statements that will lock down
+the versions for the gem given in the command line. It will specify exact
+versions in the requirements list to ensure that the gems loaded will always
+be consistent. A full recursive search of all effected gems will be
+generated.
+
+Example:
+
+ gemlock rails-1.0.0 > lockdown.rb
+
+will produce in lockdown.rb:
+
+ require "rubygems"
+ gem 'rails', '= 1.0.0'
+ gem 'rake', '= 0.7.0.1'
+ gem 'activesupport', '= 1.2.5'
+ gem 'activerecord', '= 1.13.2'
+ gem 'actionpack', '= 1.11.2'
+ gem 'actionmailer', '= 1.1.5'
+ gem 'actionwebservice', '= 1.0.0'
+
+Just load lockdown.rb from your application to ensure that the current
+versions are loaded. Make sure that lockdown.rb is loaded *before* any
+other require statements.
+
+Notice that rails 1.0.0 only requires that rake 0.6.2 or better be used.
+Rake-0.7.0.1 is the most recent version installed that satisfies that, so we
+lock it down to the exact version.
+ EOF
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME-VERSION [GEMNAME-VERSION ...]"
+ end
+
+ def complain(message)
+ if options[:strict] then
+ raise Gem::Exception, message
+ else
+ say "# #{message}"
+ end
+ end
+
+ def execute
+ say "require 'rubygems'"
+
+ locked = {}
+
+ pending = options[:args]
+
+ until pending.empty? do
+ full_name = pending.shift
+
+ spec = Gem::SourceIndex.load_specification spec_path(full_name)
+
+ if spec.nil? then
+ complain "Could not find gem #{full_name}, try using the full name"
+ next
+ end
+
+ say "gem '#{spec.name}', '= #{spec.version}'" unless locked[spec.name]
+ locked[spec.name] = true
+
+ spec.runtime_dependencies.each do |dep|
+ next if locked[dep.name]
+ candidates = Gem.source_index.search dep
+
+ if candidates.empty? then
+ complain "Unable to satisfy '#{dep}' from currently installed gems"
+ else
+ pending << candidates.last.full_name
+ end
+ end
+ end
+ end
+
+ def spec_path(gem_full_name)
+ gemspecs = Gem.path.map do |path|
+ File.join path, "specifications", "#{gem_full_name}.gemspec"
+ end
+
+ gemspecs.find { |gemspec| File.exist? gemspec }
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/mirror_command.rb b/ruby/lib/rubygems/commands/mirror_command.rb
new file mode 100644
index 0000000..959b8ea
--- /dev/null
+++ b/ruby/lib/rubygems/commands/mirror_command.rb
@@ -0,0 +1,111 @@
+require 'yaml'
+require 'zlib'
+
+require 'rubygems/command'
+require 'open-uri'
+
+class Gem::Commands::MirrorCommand < Gem::Command
+
+ def initialize
+ super 'mirror', 'Mirror a gem repository'
+ end
+
+ def description # :nodoc:
+ <<-EOF
+The mirror command uses the ~/.gemmirrorrc config file to mirror remote gem
+repositories to a local path. The config file is a YAML document that looks
+like this:
+
+ ---
+ - from: http://gems.example.com # source repository URI
+ to: /path/to/mirror # destination directory
+
+Multiple sources and destinations may be specified.
+ EOF
+ end
+
+ def execute
+ config_file = File.join Gem.user_home, '.gemmirrorrc'
+
+ raise "Config file #{config_file} not found" unless File.exist? config_file
+
+ mirrors = YAML.load_file config_file
+
+ raise "Invalid config file #{config_file}" unless mirrors.respond_to? :each
+
+ mirrors.each do |mir|
+ raise "mirror missing 'from' field" unless mir.has_key? 'from'
+ raise "mirror missing 'to' field" unless mir.has_key? 'to'
+
+ get_from = mir['from']
+ save_to = File.expand_path mir['to']
+
+ raise "Directory not found: #{save_to}" unless File.exist? save_to
+ raise "Not a directory: #{save_to}" unless File.directory? save_to
+
+ gems_dir = File.join save_to, "gems"
+
+ if File.exist? gems_dir then
+ raise "Not a directory: #{gems_dir}" unless File.directory? gems_dir
+ else
+ Dir.mkdir gems_dir
+ end
+
+ sourceindex_data = ''
+
+ say "fetching: #{get_from}/Marshal.#{Gem.marshal_version}.Z"
+
+ get_from = URI.parse get_from
+
+ if get_from.scheme.nil? then
+ get_from = get_from.to_s
+ elsif get_from.scheme == 'file' then
+ # check if specified URI contains a drive letter (file:/D:/Temp)
+ get_from = get_from.to_s
+ get_from = if get_from =~ /^file:.*[a-z]:/i then
+ get_from[6..-1]
+ else
+ get_from[5..-1]
+ end
+ end
+
+ open File.join(get_from.to_s, "Marshal.#{Gem.marshal_version}.Z"), "rb" do |y|
+ sourceindex_data = Zlib::Inflate.inflate y.read
+ open File.join(save_to, "Marshal.#{Gem.marshal_version}"), "wb" do |out|
+ out.write sourceindex_data
+ end
+ end
+
+ sourceindex = Marshal.load(sourceindex_data)
+
+ progress = ui.progress_reporter sourceindex.size,
+ "Fetching #{sourceindex.size} gems"
+ sourceindex.each do |fullname, gem|
+ gem_file = "#{fullname}.gem"
+ gem_dest = File.join gems_dir, gem_file
+
+ unless File.exist? gem_dest then
+ begin
+ open "#{get_from}/gems/#{gem_file}", "rb" do |g|
+ contents = g.read
+ open gem_dest, "wb" do |out|
+ out.write contents
+ end
+ end
+ rescue
+ old_gf = gem_file
+ gem_file = gem_file.downcase
+ retry if old_gf != gem_file
+ alert_error $!
+ end
+ end
+
+ progress.updated gem_file
+ end
+
+ progress.done
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/outdated_command.rb b/ruby/lib/rubygems/commands/outdated_command.rb
new file mode 100644
index 0000000..9e054f9
--- /dev/null
+++ b/ruby/lib/rubygems/commands/outdated_command.rb
@@ -0,0 +1,33 @@
+require 'rubygems/command'
+require 'rubygems/local_remote_options'
+require 'rubygems/spec_fetcher'
+require 'rubygems/version_option'
+
+class Gem::Commands::OutdatedCommand < Gem::Command
+
+ include Gem::LocalRemoteOptions
+ include Gem::VersionOption
+
+ def initialize
+ super 'outdated', 'Display all gems that need updates'
+
+ add_local_remote_options
+ add_platform_option
+ end
+
+ def execute
+ locals = Gem::SourceIndex.from_installed_gems
+
+ locals.outdated.sort.each do |name|
+ local = locals.find_name(name).last
+
+ dep = Gem::Dependency.new local.name, ">= #{local.version}"
+ remotes = Gem::SpecFetcher.fetcher.fetch dep
+ remote = remotes.last.first
+
+ say "#{local.name} (#{local.version} < #{remote.version})"
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/pristine_command.rb b/ruby/lib/rubygems/commands/pristine_command.rb
new file mode 100644
index 0000000..d47fe54
--- /dev/null
+++ b/ruby/lib/rubygems/commands/pristine_command.rb
@@ -0,0 +1,93 @@
+require 'fileutils'
+require 'rubygems/command'
+require 'rubygems/format'
+require 'rubygems/installer'
+require 'rubygems/version_option'
+
+class Gem::Commands::PristineCommand < Gem::Command
+
+ include Gem::VersionOption
+
+ def initialize
+ super 'pristine',
+ 'Restores installed gems to pristine condition from files located in the gem cache',
+ :version => Gem::Requirement.default
+
+ add_option('--all',
+ 'Restore all installed gems to pristine',
+ 'condition') do |value, options|
+ options[:all] = value
+ end
+
+ add_version_option('restore to', 'pristine condition')
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME gem to restore to pristine condition (unless --all)"
+ end
+
+ def defaults_str # :nodoc:
+ "--all"
+ end
+
+ def description # :nodoc:
+ <<-EOF
+The pristine command compares the installed gems with the contents of the
+cached gem and restores any files that don't match the cached gem's copy.
+
+If you have made modifications to your installed gems, the pristine command
+will revert them. After all the gem's files have been checked all bin stubs
+for the gem are regenerated.
+
+If the cached gem cannot be found, you will need to use `gem install` to
+revert the gem.
+ EOF
+ end
+
+ def usage # :nodoc:
+ "#{program_name} [args]"
+ end
+
+ def execute
+ gem_name = nil
+
+ specs = if options[:all] then
+ Gem::SourceIndex.from_installed_gems.map do |name, spec|
+ spec
+ end
+ else
+ gem_name = get_one_gem_name
+ Gem::SourceIndex.from_installed_gems.find_name(gem_name,
+ options[:version])
+ end
+
+ if specs.empty? then
+ raise Gem::Exception,
+ "Failed to find gem #{gem_name} #{options[:version]}"
+ end
+
+ install_dir = Gem.dir # TODO use installer option
+
+ raise Gem::FilePermissionError.new(install_dir) unless
+ File.writable?(install_dir)
+
+ say "Restoring gem(s) to pristine condition..."
+
+ specs.each do |spec|
+ gem = Dir[File.join(Gem.dir, 'cache', "#{spec.full_name}.gem")].first
+
+ if gem.nil? then
+ alert_error "Cached gem for #{spec.full_name} not found, use `gem install` to restore"
+ next
+ end
+
+ # TODO use installer options
+ installer = Gem::Installer.new gem, :wrappers => true, :force => true
+ installer.install
+
+ say "Restored #{spec.full_name}"
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/query_command.rb b/ruby/lib/rubygems/commands/query_command.rb
new file mode 100644
index 0000000..29fe8ac
--- /dev/null
+++ b/ruby/lib/rubygems/commands/query_command.rb
@@ -0,0 +1,233 @@
+require 'rubygems/command'
+require 'rubygems/local_remote_options'
+require 'rubygems/spec_fetcher'
+require 'rubygems/version_option'
+
+class Gem::Commands::QueryCommand < Gem::Command
+
+ include Gem::LocalRemoteOptions
+ include Gem::VersionOption
+
+ def initialize(name = 'query',
+ summary = 'Query gem information in local or remote repositories')
+ super name, summary,
+ :name => //, :domain => :local, :details => false, :versions => true,
+ :installed => false, :version => Gem::Requirement.default
+
+ add_option('-i', '--[no-]installed',
+ 'Check for installed gem') do |value, options|
+ options[:installed] = value
+ end
+
+ add_version_option
+
+ add_option('-n', '--name-matches REGEXP',
+ 'Name of gem(s) to query on matches the',
+ 'provided REGEXP') do |value, options|
+ options[:name] = /#{value}/i
+ end
+
+ add_option('-d', '--[no-]details',
+ 'Display detailed information of gem(s)') do |value, options|
+ options[:details] = value
+ end
+
+ add_option( '--[no-]versions',
+ 'Display only gem names') do |value, options|
+ options[:versions] = value
+ options[:details] = false unless value
+ end
+
+ add_option('-a', '--all',
+ 'Display all gem versions') do |value, options|
+ options[:all] = value
+ end
+
+ add_local_remote_options
+ end
+
+ def defaults_str # :nodoc:
+ "--local --name-matches // --no-details --versions --no-installed"
+ end
+
+ def execute
+ exit_code = 0
+
+ name = options[:name]
+
+ if options[:installed] then
+ if name.source.empty? then
+ alert_error "You must specify a gem name"
+ exit_code |= 4
+ elsif installed? name, options[:version] then
+ say "true"
+ else
+ say "false"
+ exit_code |= 1
+ end
+
+ raise Gem::SystemExitException, exit_code
+ end
+
+ dep = Gem::Dependency.new name, Gem::Requirement.default
+
+ if local? then
+ if ui.outs.tty? or both? then
+ say
+ say "*** LOCAL GEMS ***"
+ say
+ end
+
+ specs = Gem.source_index.search dep
+
+ spec_tuples = specs.map do |spec|
+ [[spec.name, spec.version, spec.original_platform, spec], :local]
+ end
+
+ output_query_results spec_tuples
+ end
+
+ if remote? then
+ if ui.outs.tty? or both? then
+ say
+ say "*** REMOTE GEMS ***"
+ say
+ end
+
+ all = options[:all]
+
+ begin
+ fetcher = Gem::SpecFetcher.fetcher
+ spec_tuples = fetcher.find_matching dep, all, false
+ rescue Gem::RemoteFetcher::FetchError => e
+ raise unless fetcher.warn_legacy e do
+ require 'rubygems/source_info_cache'
+
+ dep.name = '' if dep.name == //
+
+ specs = Gem::SourceInfoCache.search_with_source dep, false, all
+
+ spec_tuples = specs.map do |spec, source_uri|
+ [[spec.name, spec.version, spec.original_platform, spec],
+ source_uri]
+ end
+ end
+ end
+
+ output_query_results spec_tuples
+ end
+ end
+
+ private
+
+ ##
+ # Check if gem +name+ version +version+ is installed.
+
+ def installed?(name, version = Gem::Requirement.default)
+ dep = Gem::Dependency.new name, version
+ !Gem.source_index.search(dep).empty?
+ end
+
+ def output_query_results(spec_tuples)
+ output = []
+ versions = Hash.new { |h,name| h[name] = [] }
+
+ spec_tuples.each do |spec_tuple, source_uri|
+ versions[spec_tuple.first] << [spec_tuple, source_uri]
+ end
+
+ versions = versions.sort_by do |(name,_),_|
+ name.downcase
+ end
+
+ versions.each do |gem_name, matching_tuples|
+ matching_tuples = matching_tuples.sort_by do |(name, version,_),_|
+ version
+ end.reverse
+
+ seen = {}
+
+ matching_tuples.delete_if do |(name, version,_),_|
+ if seen[version] then
+ true
+ else
+ seen[version] = true
+ false
+ end
+ end
+
+ entry = gem_name.dup
+
+ if options[:versions] then
+ versions = matching_tuples.map { |(name, version,_),_| version }.uniq
+ entry << " (#{versions.join ', '})"
+ end
+
+ if options[:details] then
+ detail_tuple = matching_tuples.first
+
+ spec = if detail_tuple.first.length == 4 then
+ detail_tuple.first.last
+ else
+ uri = URI.parse detail_tuple.last
+ Gem::SpecFetcher.fetcher.fetch_spec detail_tuple.first, uri
+ end
+
+ entry << "\n"
+ authors = "Author#{spec.authors.length > 1 ? 's' : ''}: "
+ authors << spec.authors.join(', ')
+ entry << format_text(authors, 68, 4)
+
+ if spec.rubyforge_project and not spec.rubyforge_project.empty? then
+ rubyforge = "Rubyforge: http://rubyforge.org/projects/#{spec.rubyforge_project}"
+ entry << "\n" << format_text(rubyforge, 68, 4)
+ end
+
+ if spec.homepage and not spec.homepage.empty? then
+ entry << "\n" << format_text("Homepage: #{spec.homepage}", 68, 4)
+ end
+
+ if spec.loaded_from then
+ if matching_tuples.length == 1 then
+ loaded_from = File.dirname File.dirname(spec.loaded_from)
+ entry << "\n" << " Installed at: #{loaded_from}"
+ else
+ label = 'Installed at'
+ matching_tuples.each do |(_,version,_,s),|
+ loaded_from = File.dirname File.dirname(s.loaded_from)
+ entry << "\n" << " #{label} (#{version}): #{loaded_from}"
+ label = ' ' * label.length
+ end
+ end
+ end
+
+ entry << "\n\n" << format_text(spec.summary, 68, 4)
+ end
+ output << entry
+ end
+
+ say output.join(options[:details] ? "\n\n" : "\n")
+ end
+
+ ##
+ # Used for wrapping and indenting text
+
+ def format_text(text, wrap, indent=0)
+ result = []
+ work = text.dup
+
+ while work.length > wrap
+ if work =~ /^(.{0,#{wrap}})[ \n]/o then
+ result << $1
+ work.slice!(0, $&.length)
+ else
+ result << work.slice!(0, wrap)
+ end
+ end
+
+ result << work if work.length.nonzero?
+ result.join("\n").gsub(/^/, " " * indent)
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/rdoc_command.rb b/ruby/lib/rubygems/commands/rdoc_command.rb
new file mode 100644
index 0000000..82180d4
--- /dev/null
+++ b/ruby/lib/rubygems/commands/rdoc_command.rb
@@ -0,0 +1,82 @@
+require 'rubygems/command'
+require 'rubygems/version_option'
+require 'rubygems/doc_manager'
+
+module Gem
+ module Commands
+ class RdocCommand < Command
+ include VersionOption
+
+ def initialize
+ super('rdoc',
+ 'Generates RDoc for pre-installed gems',
+ {
+ :version => Gem::Requirement.default,
+ :include_rdoc => true,
+ :include_ri => true,
+ })
+ add_option('--all',
+ 'Generate RDoc/RI documentation for all',
+ 'installed gems') do |value, options|
+ options[:all] = value
+ end
+ add_option('--[no-]rdoc',
+ 'Include RDoc generated documents') do
+ |value, options|
+ options[:include_rdoc] = value
+ end
+ add_option('--[no-]ri',
+ 'Include RI generated documents'
+ ) do |value, options|
+ options[:include_ri] = value
+ end
+ add_version_option
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME gem to generate documentation for (unless --all)"
+ end
+
+ def defaults_str # :nodoc:
+ "--version '#{Gem::Requirement.default}' --rdoc --ri"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} [args]"
+ end
+
+ def execute
+ if options[:all]
+ specs = Gem::SourceIndex.from_installed_gems.collect { |name, spec|
+ spec
+ }
+ else
+ gem_name = get_one_gem_name
+ specs = Gem::SourceIndex.from_installed_gems.search(
+ gem_name, options[:version])
+ end
+
+ if specs.empty?
+ fail "Failed to find gem #{gem_name} to generate RDoc for #{options[:version]}"
+ end
+
+ if options[:include_ri]
+ specs.each do |spec|
+ Gem::DocManager.new(spec).generate_ri
+ end
+
+ Gem::DocManager.update_ri_cache
+ end
+
+ if options[:include_rdoc]
+ specs.each do |spec|
+ Gem::DocManager.new(spec).generate_rdoc
+ end
+ end
+
+ true
+ end
+ end
+
+ end
+end
diff --git a/ruby/lib/rubygems/commands/search_command.rb b/ruby/lib/rubygems/commands/search_command.rb
new file mode 100644
index 0000000..96da19c
--- /dev/null
+++ b/ruby/lib/rubygems/commands/search_command.rb
@@ -0,0 +1,37 @@
+require 'rubygems/command'
+require 'rubygems/commands/query_command'
+
+module Gem
+ module Commands
+
+ class SearchCommand < QueryCommand
+
+ def initialize
+ super(
+ 'search',
+ 'Display all gems whose name contains STRING'
+ )
+ remove_option('--name-matches')
+ end
+
+ def arguments # :nodoc:
+ "STRING fragment of gem name to search for"
+ end
+
+ def defaults_str # :nodoc:
+ "--local --no-details"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} [STRING]"
+ end
+
+ def execute
+ string = get_one_optional_argument
+ options[:name] = /#{string}/i
+ super
+ end
+ end
+
+ end
+end
diff --git a/ruby/lib/rubygems/commands/server_command.rb b/ruby/lib/rubygems/commands/server_command.rb
new file mode 100644
index 0000000..992ae1c
--- /dev/null
+++ b/ruby/lib/rubygems/commands/server_command.rb
@@ -0,0 +1,48 @@
+require 'rubygems/command'
+require 'rubygems/server'
+
+class Gem::Commands::ServerCommand < Gem::Command
+
+ def initialize
+ super 'server', 'Documentation and gem repository HTTP server',
+ :port => 8808, :gemdir => Gem.dir, :daemon => false
+
+ add_option '-p', '--port=PORT', Integer,
+ 'port to listen on' do |port, options|
+ options[:port] = port
+ end
+
+ add_option '-d', '--dir=GEMDIR',
+ 'directory from which to serve gems' do |gemdir, options|
+ options[:gemdir] = File.expand_path gemdir
+ end
+
+ add_option '--[no-]daemon', 'run as a daemon' do |daemon, options|
+ options[:daemon] = daemon
+ end
+ end
+
+ def defaults_str # :nodoc:
+ "--port 8808 --dir #{Gem.dir} --no-daemon"
+ end
+
+ def description # :nodoc:
+ <<-EOF
+The server command starts up a web server that hosts the RDoc for your
+installed gems and can operate as a server for installation of gems on other
+machines.
+
+The cache files for installed gems must exist to use the server as a source
+for gem installation.
+
+To install gems from a running server, use `gem install GEMNAME --source
+http://gem_server_host:8808`
+ EOF
+ end
+
+ def execute
+ Gem::Server.run options
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/sources_command.rb b/ruby/lib/rubygems/commands/sources_command.rb
new file mode 100644
index 0000000..9aabb77
--- /dev/null
+++ b/ruby/lib/rubygems/commands/sources_command.rb
@@ -0,0 +1,152 @@
+require 'fileutils'
+require 'rubygems/command'
+require 'rubygems/remote_fetcher'
+require 'rubygems/source_info_cache'
+require 'rubygems/spec_fetcher'
+
+class Gem::Commands::SourcesCommand < Gem::Command
+
+ def initialize
+ super 'sources',
+ 'Manage the sources and cache file RubyGems uses to search for gems'
+
+ add_option '-a', '--add SOURCE_URI', 'Add source' do |value, options|
+ options[:add] = value
+ end
+
+ add_option '-l', '--list', 'List sources' do |value, options|
+ options[:list] = value
+ end
+
+ add_option '-r', '--remove SOURCE_URI', 'Remove source' do |value, options|
+ options[:remove] = value
+ end
+
+ add_option '-c', '--clear-all',
+ 'Remove all sources (clear the cache)' do |value, options|
+ options[:clear_all] = value
+ end
+
+ add_option '-u', '--update', 'Update source cache' do |value, options|
+ options[:update] = value
+ end
+ end
+
+ def defaults_str
+ '--list'
+ end
+
+ def execute
+ options[:list] = !(options[:add] ||
+ options[:clear_all] ||
+ options[:remove] ||
+ options[:update])
+
+ if options[:clear_all] then
+ path = Gem::SpecFetcher.fetcher.dir
+ FileUtils.rm_rf path
+
+ if not File.exist?(path) then
+ say "*** Removed specs cache ***"
+ elsif not File.writable?(path) then
+ say "*** Unable to remove source cache (write protected) ***"
+ else
+ say "*** Unable to remove source cache ***"
+ end
+
+ sic = Gem::SourceInfoCache
+ remove_cache_file 'user', sic.user_cache_file
+ remove_cache_file 'latest user', sic.latest_user_cache_file
+ remove_cache_file 'system', sic.system_cache_file
+ remove_cache_file 'latest system', sic.latest_system_cache_file
+ end
+
+ if options[:add] then
+ source_uri = options[:add]
+ uri = URI.parse source_uri
+
+ begin
+ Gem::SpecFetcher.fetcher.load_specs uri, 'specs'
+ Gem.sources << source_uri
+ Gem.configuration.write
+
+ say "#{source_uri} added to sources"
+ rescue URI::Error, ArgumentError
+ say "#{source_uri} is not a URI"
+ rescue Gem::RemoteFetcher::FetchError => e
+ yaml_uri = uri + 'yaml'
+ gem_repo = Gem::RemoteFetcher.fetcher.fetch_size yaml_uri rescue false
+
+ if e.uri =~ /specs\.#{Regexp.escape Gem.marshal_version}\.gz$/ and
+ gem_repo then
+
+ alert_warning <<-EOF
+RubyGems 1.2+ index not found for:
+\t#{source_uri}
+
+Will cause RubyGems to revert to legacy indexes, degrading performance.
+ EOF
+
+ say "#{source_uri} added to sources"
+ else
+ say "Error fetching #{source_uri}:\n\t#{e.message}"
+ end
+ end
+ end
+
+ if options[:remove] then
+ source_uri = options[:remove]
+
+ unless Gem.sources.include? source_uri then
+ say "source #{source_uri} not present in cache"
+ else
+ Gem.sources.delete source_uri
+ Gem.configuration.write
+
+ say "#{source_uri} removed from sources"
+ end
+ end
+
+ if options[:update] then
+ fetcher = Gem::SpecFetcher.fetcher
+
+ if fetcher.legacy_repos.empty? then
+ Gem.sources.each do |update_uri|
+ update_uri = URI.parse update_uri
+ fetcher.load_specs update_uri, 'specs'
+ fetcher.load_specs update_uri, 'latest_specs'
+ end
+ else
+ Gem::SourceInfoCache.cache true
+ Gem::SourceInfoCache.cache.flush
+ end
+
+ say "source cache successfully updated"
+ end
+
+ if options[:list] then
+ say "*** CURRENT SOURCES ***"
+ say
+
+ Gem.sources.each do |source|
+ say source
+ end
+ end
+ end
+
+ private
+
+ def remove_cache_file(desc, path)
+ FileUtils.rm_rf path
+
+ if not File.exist?(path) then
+ say "*** Removed #{desc} source cache ***"
+ elsif not File.writable?(path) then
+ say "*** Unable to remove #{desc} source cache (write protected) ***"
+ else
+ say "*** Unable to remove #{desc} source cache ***"
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/specification_command.rb b/ruby/lib/rubygems/commands/specification_command.rb
new file mode 100644
index 0000000..5aaf6d1
--- /dev/null
+++ b/ruby/lib/rubygems/commands/specification_command.rb
@@ -0,0 +1,77 @@
+require 'yaml'
+require 'rubygems/command'
+require 'rubygems/local_remote_options'
+require 'rubygems/version_option'
+require 'rubygems/source_info_cache'
+require 'rubygems/format'
+
+class Gem::Commands::SpecificationCommand < Gem::Command
+
+ include Gem::LocalRemoteOptions
+ include Gem::VersionOption
+
+ def initialize
+ super 'specification', 'Display gem specification (in yaml)',
+ :domain => :local, :version => Gem::Requirement.default
+
+ add_version_option('examine')
+ add_platform_option
+
+ add_option('--all', 'Output specifications for all versions of',
+ 'the gem') do |value, options|
+ options[:all] = true
+ end
+
+ add_local_remote_options
+ end
+
+ def arguments # :nodoc:
+ "GEMFILE name of gem to show the gemspec for"
+ end
+
+ def defaults_str # :nodoc:
+ "--local --version '#{Gem::Requirement.default}'"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} [GEMFILE]"
+ end
+
+ def execute
+ specs = []
+ gem = get_one_gem_name
+ dep = Gem::Dependency.new gem, options[:version]
+
+ if local? then
+ if File.exist? gem then
+ specs << Gem::Format.from_file_by_path(gem).spec rescue nil
+ end
+
+ if specs.empty? then
+ specs.push(*Gem.source_index.search(dep))
+ end
+ end
+
+ if remote? then
+ found = Gem::SpecFetcher.fetcher.fetch dep
+
+ specs.push(*found.map { |spec,| spec })
+ end
+
+ if specs.empty? then
+ alert_error "Unknown gem '#{gem}'"
+ terminate_interaction 1
+ end
+
+ output = lambda { |s| say s.to_yaml; say "\n" }
+
+ if options[:all] then
+ specs.each(&output)
+ else
+ spec = specs.sort_by { |s| s.version }.last
+ output[spec]
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/stale_command.rb b/ruby/lib/rubygems/commands/stale_command.rb
new file mode 100644
index 0000000..78cbdcc
--- /dev/null
+++ b/ruby/lib/rubygems/commands/stale_command.rb
@@ -0,0 +1,27 @@
+require 'rubygems/command'
+
+class Gem::Commands::StaleCommand < Gem::Command
+ def initialize
+ super('stale', 'List gems along with access times')
+ end
+
+ def usage # :nodoc:
+ "#{program_name}"
+ end
+
+ def execute
+ gem_to_atime = {}
+ Gem.source_index.each do |name, spec|
+ Dir["#{spec.full_gem_path}/**/*.*"].each do |file|
+ next if File.directory?(file)
+ stat = File.stat(file)
+ gem_to_atime[name] ||= stat.atime
+ gem_to_atime[name] = stat.atime if gem_to_atime[name] < stat.atime
+ end
+ end
+
+ gem_to_atime.sort_by { |_, atime| atime }.each do |name, atime|
+ say "#{name} at #{atime.strftime '%c'}"
+ end
+ end
+end
diff --git a/ruby/lib/rubygems/commands/uninstall_command.rb b/ruby/lib/rubygems/commands/uninstall_command.rb
new file mode 100644
index 0000000..3d6e238
--- /dev/null
+++ b/ruby/lib/rubygems/commands/uninstall_command.rb
@@ -0,0 +1,73 @@
+require 'rubygems/command'
+require 'rubygems/version_option'
+require 'rubygems/uninstaller'
+
+module Gem
+ module Commands
+ class UninstallCommand < Command
+
+ include VersionOption
+
+ def initialize
+ super 'uninstall', 'Uninstall gems from the local repository',
+ :version => Gem::Requirement.default
+
+ add_option('-a', '--[no-]all',
+ 'Uninstall all matching versions'
+ ) do |value, options|
+ options[:all] = value
+ end
+
+ add_option('-I', '--[no-]ignore-dependencies',
+ 'Ignore dependency requirements while',
+ 'uninstalling') do |value, options|
+ options[:ignore] = value
+ end
+
+ add_option('-x', '--[no-]executables',
+ 'Uninstall applicable executables without',
+ 'confirmation') do |value, options|
+ options[:executables] = value
+ end
+
+ add_option('-i', '--install-dir DIR',
+ 'Directory to uninstall gem from') do |value, options|
+ options[:install_dir] = File.expand_path(value)
+ end
+
+ add_option('-n', '--bindir DIR',
+ 'Directory to remove binaries from') do |value, options|
+ options[:bin_dir] = File.expand_path(value)
+ end
+
+ add_version_option
+ add_platform_option
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to uninstall"
+ end
+
+ def defaults_str # :nodoc:
+ "--version '#{Gem::Requirement.default}' --no-force " \
+ "--install-dir #{Gem.dir}"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME [GEMNAME ...]"
+ end
+
+ def execute
+ get_all_gem_names.each do |gem_name|
+ begin
+ Gem::Uninstaller.new(gem_name, options).uninstall
+ rescue Gem::GemNotInHomeException => e
+ spec = e.spec
+ alert("In order to remove #{spec.name}, please execute:\n" \
+ "\tgem uninstall #{spec.name} --install-dir=#{spec.installation_path}")
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rubygems/commands/unpack_command.rb b/ruby/lib/rubygems/commands/unpack_command.rb
new file mode 100644
index 0000000..ab2494b
--- /dev/null
+++ b/ruby/lib/rubygems/commands/unpack_command.rb
@@ -0,0 +1,95 @@
+require 'fileutils'
+require 'rubygems/command'
+require 'rubygems/installer'
+require 'rubygems/version_option'
+
+class Gem::Commands::UnpackCommand < Gem::Command
+
+ include Gem::VersionOption
+
+ def initialize
+ super 'unpack', 'Unpack an installed gem to the current directory',
+ :version => Gem::Requirement.default,
+ :target => Dir.pwd
+
+ add_option('--target', 'target directory for unpacking') do |value, options|
+ options[:target] = value
+ end
+
+ add_version_option
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to unpack"
+ end
+
+ def defaults_str # :nodoc:
+ "--version '#{Gem::Requirement.default}'"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME"
+ end
+
+ #--
+ # TODO: allow, e.g., 'gem unpack rake-0.3.1'. Find a general solution for
+ # this, so that it works for uninstall as well. (And check other commands
+ # at the same time.)
+ def execute
+ gemname = get_one_gem_name
+ path = get_path(gemname, options[:version])
+
+ if path then
+ basename = File.basename(path).sub(/\.gem$/, '')
+ target_dir = File.expand_path File.join(options[:target], basename)
+ FileUtils.mkdir_p target_dir
+ Gem::Installer.new(path, :unpack => true).unpack target_dir
+ say "Unpacked gem: '#{target_dir}'"
+ else
+ alert_error "Gem '#{gemname}' not installed."
+ end
+ end
+
+ # Return the full path to the cached gem file matching the given
+ # name and version requirement. Returns 'nil' if no match.
+ #
+ # Example:
+ #
+ # get_path('rake', '> 0.4') # -> '/usr/lib/ruby/gems/1.8/cache/rake-0.4.2.gem'
+ # get_path('rake', '< 0.1') # -> nil
+ # get_path('rak') # -> nil (exact name required)
+ #--
+ # TODO: This should be refactored so that it's a general service. I don't
+ # think any of our existing classes are the right place though. Just maybe
+ # 'Cache'?
+ #
+ # TODO: It just uses Gem.dir for now. What's an easy way to get the list of
+ # source directories?
+ def get_path(gemname, version_req)
+ return gemname if gemname =~ /\.gem$/i
+
+ specs = Gem::source_index.find_name gemname, version_req
+
+ selected = specs.sort_by { |s| s.version }.last
+
+ return nil if selected.nil?
+
+ # We expect to find (basename).gem in the 'cache' directory.
+ # Furthermore, the name match must be exact (ignoring case).
+ if gemname =~ /^#{selected.name}$/i
+ filename = selected.full_name + '.gem'
+ path = nil
+
+ Gem.path.find do |gem_dir|
+ path = File.join gem_dir, 'cache', filename
+ File.exist? path
+ end
+
+ path
+ else
+ nil
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/update_command.rb b/ruby/lib/rubygems/commands/update_command.rb
new file mode 100644
index 0000000..28d3a5d
--- /dev/null
+++ b/ruby/lib/rubygems/commands/update_command.rb
@@ -0,0 +1,181 @@
+require 'rubygems/command'
+require 'rubygems/command_manager'
+require 'rubygems/install_update_options'
+require 'rubygems/local_remote_options'
+require 'rubygems/spec_fetcher'
+require 'rubygems/version_option'
+require 'rubygems/commands/install_command'
+
+class Gem::Commands::UpdateCommand < Gem::Command
+
+ include Gem::InstallUpdateOptions
+ include Gem::LocalRemoteOptions
+ include Gem::VersionOption
+
+ def initialize
+ super 'update',
+ 'Update the named gems (or all installed gems) in the local repository',
+ :generate_rdoc => true,
+ :generate_ri => true,
+ :force => false,
+ :test => false
+
+ add_install_update_options
+
+ add_option('--system',
+ 'Update the RubyGems system software') do |value, options|
+ options[:system] = value
+ end
+
+ add_local_remote_options
+
+ add_platform_option
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to update"
+ end
+
+ def defaults_str # :nodoc:
+ "--rdoc --ri --no-force --no-test --install-dir #{Gem.dir}"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME [GEMNAME ...]"
+ end
+
+ def execute
+ hig = {}
+
+ if options[:system] then
+ say "Updating RubyGems"
+
+ unless options[:args].empty? then
+ fail "No gem names are allowed with the --system option"
+ end
+
+ rubygems_update = Gem::Specification.new
+ rubygems_update.name = 'rubygems-update'
+ rubygems_update.version = Gem::Version.new Gem::RubyGemsVersion
+ hig['rubygems-update'] = rubygems_update
+
+ options[:user_install] = false
+ else
+ say "Updating installed gems"
+
+ hig = {} # highest installed gems
+
+ Gem.source_index.each do |name, spec|
+ if hig[spec.name].nil? or hig[spec.name].version < spec.version then
+ hig[spec.name] = spec
+ end
+ end
+ end
+
+ gems_to_update = which_to_update hig, options[:args]
+
+ updated = []
+
+ installer = Gem::DependencyInstaller.new options
+
+ gems_to_update.uniq.sort.each do |name|
+ next if updated.any? { |spec| spec.name == name }
+
+ say "Updating #{name}"
+ installer.install name
+
+ installer.installed_gems.each do |spec|
+ updated << spec
+ say "Successfully installed #{spec.full_name}"
+ end
+ end
+
+ if gems_to_update.include? "rubygems-update" then
+ Gem.source_index.refresh!
+
+ update_gems = Gem.source_index.search 'rubygems-update'
+
+ latest_update_gem = update_gems.sort_by { |s| s.version }.last
+
+ say "Updating RubyGems to #{latest_update_gem.version}"
+ installed = do_rubygems_update latest_update_gem.version
+
+ say "RubyGems system software updated" if installed
+ else
+ if updated.empty? then
+ say "Nothing to update"
+ else
+ say "Gems updated: #{updated.map { |spec| spec.name }.join ', '}"
+ end
+ end
+ end
+
+ ##
+ # Update the RubyGems software to +version+.
+
+ def do_rubygems_update(version)
+ args = []
+ args.push '--prefix', Gem.prefix unless Gem.prefix.nil?
+ args << '--no-rdoc' unless options[:generate_rdoc]
+ args << '--no-ri' unless options[:generate_ri]
+ args << '--no-format-executable' if options[:no_format_executable]
+
+ update_dir = File.join Gem.dir, 'gems', "rubygems-update-#{version}"
+
+ Dir.chdir update_dir do
+ say "Installing RubyGems #{version}"
+ setup_cmd = "#{Gem.ruby} setup.rb #{args.join ' '}"
+
+ # Make sure old rubygems isn't loaded
+ old = ENV["RUBYOPT"]
+ ENV.delete("RUBYOPT")
+ system setup_cmd
+ ENV["RUBYOPT"] = old if old
+ end
+ end
+
+ def which_to_update(highest_installed_gems, gem_names)
+ result = []
+
+ highest_installed_gems.each do |l_name, l_spec|
+ next if not gem_names.empty? and
+ gem_names.all? { |name| /#{name}/ !~ l_spec.name }
+
+ dependency = Gem::Dependency.new l_spec.name, "> #{l_spec.version}"
+
+ begin
+ fetcher = Gem::SpecFetcher.fetcher
+ spec_tuples = fetcher.find_matching dependency
+ rescue Gem::RemoteFetcher::FetchError => e
+ raise unless fetcher.warn_legacy e do
+ require 'rubygems/source_info_cache'
+
+ dependency.name = '' if dependency.name == //
+
+ specs = Gem::SourceInfoCache.search_with_source dependency
+
+ spec_tuples = specs.map do |spec, source_uri|
+ [[spec.name, spec.version, spec.original_platform], source_uri]
+ end
+ end
+ end
+
+ matching_gems = spec_tuples.select do |(name, version, platform),|
+ name == l_name and Gem::Platform.match platform
+ end
+
+ highest_remote_gem = matching_gems.sort_by do |(name, version),|
+ version
+ end.last
+
+ if highest_remote_gem and
+ l_spec.version < highest_remote_gem.first[1] then
+ result << l_name
+ end
+ end
+
+ result
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/commands/which_command.rb b/ruby/lib/rubygems/commands/which_command.rb
new file mode 100644
index 0000000..2267e44
--- /dev/null
+++ b/ruby/lib/rubygems/commands/which_command.rb
@@ -0,0 +1,87 @@
+require 'rubygems/command'
+require 'rubygems/gem_path_searcher'
+
+class Gem::Commands::WhichCommand < Gem::Command
+
+ EXT = %w[.rb .rbw .so .dll .bundle] # HACK
+
+ def initialize
+ super 'which', 'Find the location of a library file you can require',
+ :search_gems_first => false, :show_all => false
+
+ add_option '-a', '--[no-]all', 'show all matching files' do |show_all, options|
+ options[:show_all] = show_all
+ end
+
+ add_option '-g', '--[no-]gems-first',
+ 'search gems before non-gems' do |gems_first, options|
+ options[:search_gems_first] = gems_first
+ end
+ end
+
+ def arguments # :nodoc:
+ "FILE name of file to find"
+ end
+
+ def defaults_str # :nodoc:
+ "--no-gems-first --no-all"
+ end
+
+ def usage # :nodoc:
+ "#{program_name} FILE [FILE ...]"
+ end
+
+ def execute
+ searcher = Gem::GemPathSearcher.new
+
+ options[:args].each do |arg|
+ dirs = $LOAD_PATH
+ spec = searcher.find arg
+
+ if spec then
+ if options[:search_gems_first] then
+ dirs = gem_paths(spec) + $LOAD_PATH
+ else
+ dirs = $LOAD_PATH + gem_paths(spec)
+ end
+
+ say "(checking gem #{spec.full_name} for #{arg})" if
+ Gem.configuration.verbose
+ end
+
+ paths = find_paths arg, dirs
+
+ if paths.empty? then
+ say "Can't find ruby library file or shared library #{arg}"
+ else
+ say paths
+ end
+ end
+ end
+
+ def find_paths(package_name, dirs)
+ result = []
+
+ dirs.each do |dir|
+ EXT.each do |ext|
+ full_path = File.join dir, "#{package_name}#{ext}"
+ if File.exist? full_path then
+ result << full_path
+ return result unless options[:show_all]
+ end
+ end
+ end
+
+ result
+ end
+
+ def gem_paths(spec)
+ spec.require_paths.collect { |d| File.join spec.full_gem_path, d }
+ end
+
+ def usage # :nodoc:
+ "#{program_name} FILE [...]"
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/config_file.rb b/ruby/lib/rubygems/config_file.rb
new file mode 100644
index 0000000..934516e
--- /dev/null
+++ b/ruby/lib/rubygems/config_file.rb
@@ -0,0 +1,266 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'yaml'
+require 'rubygems'
+
+# Store the gem command options specified in the configuration file. The
+# config file object acts much like a hash.
+
+class Gem::ConfigFile
+
+ DEFAULT_BACKTRACE = false
+ DEFAULT_BENCHMARK = false
+ DEFAULT_BULK_THRESHOLD = 1000
+ DEFAULT_VERBOSITY = true
+ DEFAULT_UPDATE_SOURCES = true
+
+ ##
+ # For Ruby packagers to set configuration defaults. Set in
+ # rubygems/defaults/operating_system.rb
+
+ OPERATING_SYSTEM_DEFAULTS = {}
+
+ ##
+ # For Ruby implementers to set configuration defaults. Set in
+ # rubygems/defaults/#{RUBY_ENGINE}.rb
+
+ PLATFORM_DEFAULTS = {}
+
+ system_config_path =
+ begin
+ require 'Win32API'
+
+ CSIDL_COMMON_APPDATA = 0x0023
+ path = 0.chr * 260
+ SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'PLPLP', 'L', :stdcall
+ SHGetFolderPath.call nil, CSIDL_COMMON_APPDATA, nil, 1, path
+
+ path.strip
+ rescue LoadError
+ '/etc'
+ end
+
+ SYSTEM_WIDE_CONFIG_FILE = File.join system_config_path, 'gemrc'
+
+ # List of arguments supplied to the config file object.
+ attr_reader :args
+
+ # Where to look for gems
+ attr_accessor :path
+
+ attr_accessor :home
+
+ # True if we print backtraces on errors.
+ attr_writer :backtrace
+
+ # True if we are benchmarking this run.
+ attr_accessor :benchmark
+
+ # Bulk threshold value. If the number of missing gems are above
+ # this threshold value, then a bulk download technique is used.
+ attr_accessor :bulk_threshold
+
+ # Verbose level of output:
+ # * false -- No output
+ # * true -- Normal output
+ # * :loud -- Extra output
+ attr_accessor :verbose
+
+ # True if we want to update the SourceInfoCache every time, false otherwise
+ attr_accessor :update_sources
+
+ # Create the config file object. +args+ is the list of arguments
+ # from the command line.
+ #
+ # The following command line options are handled early here rather
+ # than later at the time most command options are processed.
+ #
+ # * --config-file and --config-file==NAME -- Obviously these need
+ # to be handled by the ConfigFile object to ensure we get the
+ # right config file.
+ #
+ # * --backtrace -- Backtrace needs to be turned on early so that
+ # errors before normal option parsing can be properly handled.
+ #
+ # * --debug -- Enable Ruby level debug messages. Handled early
+ # for the same reason as --backtrace.
+ #
+ def initialize(arg_list)
+ @config_file_name = nil
+ need_config_file_name = false
+
+ arg_list = arg_list.map do |arg|
+ if need_config_file_name then
+ @config_file_name = arg
+ need_config_file_name = false
+ nil
+ elsif arg =~ /^--config-file=(.*)/ then
+ @config_file_name = $1
+ nil
+ elsif arg =~ /^--config-file$/ then
+ need_config_file_name = true
+ nil
+ else
+ arg
+ end
+ end.compact
+
+ @backtrace = DEFAULT_BACKTRACE
+ @benchmark = DEFAULT_BENCHMARK
+ @bulk_threshold = DEFAULT_BULK_THRESHOLD
+ @verbose = DEFAULT_VERBOSITY
+ @update_sources = DEFAULT_UPDATE_SOURCES
+
+ operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS)
+ platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS)
+ system_config = load_file SYSTEM_WIDE_CONFIG_FILE
+ user_config = load_file config_file_name.dup.untaint
+
+ @hash = operating_system_config.merge platform_config
+ @hash = @hash.merge system_config
+ @hash = @hash.merge user_config
+
+ # HACK these override command-line args, which is bad
+ @backtrace = @hash[:backtrace] if @hash.key? :backtrace
+ @benchmark = @hash[:benchmark] if @hash.key? :benchmark
+ @bulk_threshold = @hash[:bulk_threshold] if @hash.key? :bulk_threshold
+ Gem.sources = @hash[:sources] if @hash.key? :sources
+ @verbose = @hash[:verbose] if @hash.key? :verbose
+ @update_sources = @hash[:update_sources] if @hash.key? :update_sources
+ @path = @hash[:gempath] if @hash.key? :gempath
+ @home = @hash[:gemhome] if @hash.key? :gemhome
+
+ handle_arguments arg_list
+ end
+
+ def load_file(filename)
+ begin
+ YAML.load(File.read(filename)) if filename and File.exist?(filename)
+ rescue ArgumentError
+ warn "Failed to load #{config_file_name}"
+ rescue Errno::EACCES
+ warn "Failed to load #{config_file_name} due to permissions problem."
+ end or {}
+ end
+
+ # True if the backtrace option has been specified, or debug is on.
+ def backtrace
+ @backtrace or $DEBUG
+ end
+
+ # The name of the configuration file.
+ def config_file_name
+ @config_file_name || Gem.config_file
+ end
+
+ # Delegates to @hash
+ def each(&block)
+ hash = @hash.dup
+ hash.delete :update_sources
+ hash.delete :verbose
+ hash.delete :benchmark
+ hash.delete :backtrace
+ hash.delete :bulk_threshold
+
+ yield :update_sources, @update_sources
+ yield :verbose, @verbose
+ yield :benchmark, @benchmark
+ yield :backtrace, @backtrace
+ yield :bulk_threshold, @bulk_threshold
+
+ yield 'config_file_name', @config_file_name if @config_file_name
+
+ hash.each(&block)
+ end
+
+ # Handle the command arguments.
+ def handle_arguments(arg_list)
+ @args = []
+
+ arg_list.each do |arg|
+ case arg
+ when /^--(backtrace|traceback)$/ then
+ @backtrace = true
+ when /^--bench(mark)?$/ then
+ @benchmark = true
+ when /^--debug$/ then
+ $DEBUG = true
+ else
+ @args << arg
+ end
+ end
+ end
+
+ # Really verbose mode gives you extra output.
+ def really_verbose
+ case verbose
+ when true, false, nil then false
+ else true
+ end
+ end
+
+ # to_yaml only overwrites things you can't override on the command line.
+ def to_yaml # :nodoc:
+ yaml_hash = {}
+ yaml_hash[:backtrace] = @hash.key?(:backtrace) ? @hash[:backtrace] :
+ DEFAULT_BACKTRACE
+ yaml_hash[:benchmark] = @hash.key?(:benchmark) ? @hash[:benchmark] :
+ DEFAULT_BENCHMARK
+ yaml_hash[:bulk_threshold] = @hash.key?(:bulk_threshold) ?
+ @hash[:bulk_threshold] : DEFAULT_BULK_THRESHOLD
+ yaml_hash[:sources] = Gem.sources
+ yaml_hash[:update_sources] = @hash.key?(:update_sources) ?
+ @hash[:update_sources] : DEFAULT_UPDATE_SOURCES
+ yaml_hash[:verbose] = @hash.key?(:verbose) ? @hash[:verbose] :
+ DEFAULT_VERBOSITY
+
+ keys = yaml_hash.keys.map { |key| key.to_s }
+ keys << 'debug'
+ re = Regexp.union(*keys)
+
+ @hash.each do |key, value|
+ key = key.to_s
+ next if key =~ re
+ yaml_hash[key.to_s] = value
+ end
+
+ yaml_hash.to_yaml
+ end
+
+ # Writes out this config file, replacing its source.
+ def write
+ File.open config_file_name, 'w' do |fp|
+ fp.write self.to_yaml
+ end
+ end
+
+ # Return the configuration information for +key+.
+ def [](key)
+ @hash[key.to_s]
+ end
+
+ # Set configuration option +key+ to +value+.
+ def []=(key, value)
+ @hash[key.to_s] = value
+ end
+
+ def ==(other) # :nodoc:
+ self.class === other and
+ @backtrace == other.backtrace and
+ @benchmark == other.benchmark and
+ @bulk_threshold == other.bulk_threshold and
+ @verbose == other.verbose and
+ @update_sources == other.update_sources and
+ @hash == other.hash
+ end
+
+ protected
+
+ attr_reader :hash
+
+end
+
diff --git a/ruby/lib/rubygems/custom_require.rb b/ruby/lib/rubygems/custom_require.rb
new file mode 100644
index 0000000..78c7872
--- /dev/null
+++ b/ruby/lib/rubygems/custom_require.rb
@@ -0,0 +1,46 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+
+module Kernel
+
+ ##
+ # The Kernel#require from before RubyGems was loaded.
+
+ alias gem_original_require require
+
+ ##
+ # When RubyGems is required, Kernel#require is replaced with our own which
+ # is capable of loading gems on demand.
+ #
+ # When you call <tt>require 'x'</tt>, this is what happens:
+ # * If the file can be loaded from the existing Ruby loadpath, it
+ # is.
+ # * Otherwise, installed gems are searched for a file that matches.
+ # If it's found in gem 'y', that gem is activated (added to the
+ # loadpath).
+ #
+ # The normal <tt>require</tt> functionality of returning false if
+ # that file has already been loaded is preserved.
+
+ def require(path) # :doc:
+ gem_original_require path
+ rescue LoadError => load_error
+ if load_error.message =~ /#{Regexp.escape path}\z/ and
+ spec = Gem.searcher.find(path) then
+ Gem.activate(spec.name, "= #{spec.version}")
+ gem_original_require path
+ else
+ raise load_error
+ end
+ end
+
+ private :require
+ private :gem_original_require
+
+end
+
diff --git a/ruby/lib/rubygems/defaults.rb b/ruby/lib/rubygems/defaults.rb
new file mode 100644
index 0000000..995b81e
--- /dev/null
+++ b/ruby/lib/rubygems/defaults.rb
@@ -0,0 +1,88 @@
+module Gem
+
+ @post_install_hooks ||= []
+ @post_uninstall_hooks ||= []
+ @pre_uninstall_hooks ||= []
+ @pre_install_hooks ||= []
+
+ ##
+ # An Array of the default sources that come with RubyGems
+
+ def self.default_sources
+ %w[http://gems.rubyforge.org/]
+ end
+
+ ##
+ # Default home directory path to be used if an alternate value is not
+ # specified in the environment
+
+ def self.default_dir
+ if defined? RUBY_FRAMEWORK_VERSION then
+ File.join File.dirname(ConfigMap[:sitedir]), 'Gems',
+ ConfigMap[:ruby_version]
+ else
+ ConfigMap[:sitelibdir].sub(%r'/site_ruby/(?=[^/]+)', '/gems/')
+ end
+ end
+
+ ##
+ # Path for gems in the user's home directory
+
+ def self.user_dir
+ File.join(Gem.user_home, '.gem', ruby_engine,
+ ConfigMap[:ruby_version])
+ end
+
+ ##
+ # Default gem load path
+
+ def self.default_path
+ [user_dir, default_dir]
+ end
+
+ ##
+ # Deduce Ruby's --program-prefix and --program-suffix from its install name
+
+ def self.default_exec_format
+ baseruby = ConfigMap[:BASERUBY] || 'ruby'
+ ConfigMap[:RUBY_INSTALL_NAME].sub(baseruby, '%s') rescue '%s'
+ end
+
+ ##
+ # The default directory for binaries
+
+ def self.default_bindir
+ if defined? RUBY_FRAMEWORK_VERSION then # mac framework support
+ '/usr/bin'
+ else # generic install
+ ConfigMap[:bindir]
+ end
+ end
+
+ ##
+ # The default system-wide source info cache directory
+
+ def self.default_system_source_cache_dir
+ File.join Gem.dir, 'source_cache'
+ end
+
+ ##
+ # The default user-specific source info cache directory
+
+ def self.default_user_source_cache_dir
+ File.join Gem.user_home, '.gem', 'source_cache'
+ end
+
+ ##
+ # A wrapper around RUBY_ENGINE const that may not be defined
+
+ def self.ruby_engine
+ if defined? RUBY_ENGINE then
+ RUBY_ENGINE
+ else
+ 'ruby'
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/dependency.rb b/ruby/lib/rubygems/dependency.rb
new file mode 100644
index 0000000..7b9904d
--- /dev/null
+++ b/ruby/lib/rubygems/dependency.rb
@@ -0,0 +1,119 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+
+##
+# The Dependency class holds a Gem name and a Gem::Requirement
+
+class Gem::Dependency
+
+ ##
+ # Valid dependency types.
+ #--
+ # When this list is updated, be sure to change
+ # Gem::Specification::CURRENT_SPECIFICATION_VERSION as well.
+
+ TYPES = [
+ :development,
+ :runtime,
+ ]
+
+ ##
+ # Dependency name or regular expression.
+
+ attr_accessor :name
+
+ ##
+ # Dependency type.
+
+ attr_reader :type
+
+ ##
+ # Dependent versions.
+
+ attr_writer :version_requirements
+
+ ##
+ # Orders dependencies by name only.
+
+ def <=>(other)
+ [@name] <=> [other.name]
+ end
+
+ ##
+ # Constructs a dependency with +name+ and +requirements+.
+
+ def initialize(name, version_requirements, type=:runtime)
+ @name = name
+
+ unless TYPES.include? type
+ raise ArgumentError, "Valid types are #{TYPES.inspect}, not #{@type.inspect}"
+ end
+
+ @type = type
+
+ @version_requirements = Gem::Requirement.create version_requirements
+ @version_requirement = nil # Avoid warnings.
+ end
+
+ def version_requirements
+ normalize if defined? @version_requirement and @version_requirement
+ @version_requirements
+ end
+
+ def requirement_list
+ version_requirements.as_list
+ end
+
+ alias requirements_list requirement_list
+
+ def normalize
+ ver = @version_requirement.instance_eval { @version }
+ @version_requirements = Gem::Requirement.new([ver])
+ @version_requirement = nil
+ end
+
+ def to_s # :nodoc:
+ "#{name} (#{version_requirements}, #{@type || :runtime})"
+ end
+
+ def ==(other) # :nodoc:
+ self.class === other &&
+ self.name == other.name &&
+ self.type == other.type &&
+ self.version_requirements == other.version_requirements
+ end
+
+ ##
+ # Uses this dependency as a pattern to compare to the dependency +other+.
+ # This dependency will match if the name matches the other's name, and other
+ # has only an equal version requirement that satisfies this dependency.
+
+ def =~(other)
+ return false unless self.class === other
+
+ pattern = @name
+ pattern = /\A#{@name}\Z/ unless Regexp === pattern
+
+ return false unless pattern =~ other.name
+
+ reqs = other.version_requirements.requirements
+
+ return false unless reqs.length == 1
+ return false unless reqs.first.first == '='
+
+ version = reqs.first.last
+
+ version_requirements.satisfied_by? version
+ end
+
+ def hash # :nodoc:
+ name.hash + type.hash + version_requirements.hash
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/dependency_installer.rb b/ruby/lib/rubygems/dependency_installer.rb
new file mode 100644
index 0000000..9ae2659
--- /dev/null
+++ b/ruby/lib/rubygems/dependency_installer.rb
@@ -0,0 +1,258 @@
+require 'rubygems'
+require 'rubygems/dependency_list'
+require 'rubygems/installer'
+require 'rubygems/spec_fetcher'
+require 'rubygems/user_interaction'
+
+##
+# Installs a gem along with all its dependencies from local and remote gems.
+
+class Gem::DependencyInstaller
+
+ include Gem::UserInteraction
+
+ attr_reader :gems_to_install
+ attr_reader :installed_gems
+
+ DEFAULT_OPTIONS = {
+ :env_shebang => false,
+ :domain => :both, # HACK dup
+ :force => false,
+ :format_executable => false, # HACK dup
+ :ignore_dependencies => false,
+ :security_policy => nil, # HACK NoSecurity requires OpenSSL. AlmostNo? Low?
+ :wrappers => true
+ }
+
+ ##
+ # Creates a new installer instance.
+ #
+ # Options are:
+ # :cache_dir:: Alternate repository path to store .gem files in.
+ # :domain:: :local, :remote, or :both. :local only searches gems in the
+ # current directory. :remote searches only gems in Gem::sources.
+ # :both searches both.
+ # :env_shebang:: See Gem::Installer::new.
+ # :force:: See Gem::Installer#install.
+ # :format_executable:: See Gem::Installer#initialize.
+ # :ignore_dependencies:: Don't install any dependencies.
+ # :install_dir:: See Gem::Installer#install.
+ # :security_policy:: See Gem::Installer::new and Gem::Security.
+ # :user_install:: See Gem::Installer.new
+ # :wrappers:: See Gem::Installer::new
+
+ def initialize(options = {})
+ if options[:install_dir] then
+ spec_dir = options[:install_dir], 'specifications'
+ @source_index = Gem::SourceIndex.from_gems_in spec_dir
+ else
+ @source_index = Gem.source_index
+ end
+
+ options = DEFAULT_OPTIONS.merge options
+
+ @bin_dir = options[:bin_dir]
+ @development = options[:development]
+ @domain = options[:domain]
+ @env_shebang = options[:env_shebang]
+ @force = options[:force]
+ @format_executable = options[:format_executable]
+ @ignore_dependencies = options[:ignore_dependencies]
+ @security_policy = options[:security_policy]
+ @user_install = options[:user_install]
+ @wrappers = options[:wrappers]
+
+ @installed_gems = []
+
+ @install_dir = options[:install_dir] || Gem.dir
+ @cache_dir = options[:cache_dir] || @install_dir
+ end
+
+ ##
+ # Returns a list of pairs of gemspecs and source_uris that match
+ # Gem::Dependency +dep+ from both local (Dir.pwd) and remote (Gem.sources)
+ # sources. Gems are sorted with newer gems prefered over older gems, and
+ # local gems preferred over remote gems.
+
+ def find_gems_with_sources(dep)
+ gems_and_sources = []
+
+ if @domain == :both or @domain == :local then
+ Dir[File.join(Dir.pwd, "#{dep.name}-[0-9]*.gem")].each do |gem_file|
+ spec = Gem::Format.from_file_by_path(gem_file).spec
+ gems_and_sources << [spec, gem_file] if spec.name == dep.name
+ end
+ end
+
+ if @domain == :both or @domain == :remote then
+ begin
+ requirements = dep.version_requirements.requirements.map do |req, ver|
+ req
+ end
+
+ all = requirements.length > 1 ||
+ (requirements.first != ">=" and requirements.first != ">")
+
+ found = Gem::SpecFetcher.fetcher.fetch dep, all
+ gems_and_sources.push(*found)
+
+ rescue Gem::RemoteFetcher::FetchError => e
+ if Gem.configuration.really_verbose then
+ say "Error fetching remote data:\t\t#{e.message}"
+ say "Falling back to local-only install"
+ end
+ @domain = :local
+ end
+ end
+
+ gems_and_sources.sort_by do |gem, source|
+ [gem, source =~ /^http:\/\// ? 0 : 1] # local gems win
+ end
+ end
+
+ ##
+ # Gathers all dependencies necessary for the installation from local and
+ # remote sources unless the ignore_dependencies was given.
+
+ def gather_dependencies
+ specs = @specs_and_sources.map { |spec,_| spec }
+
+ dependency_list = Gem::DependencyList.new
+ dependency_list.add(*specs)
+
+ unless @ignore_dependencies then
+ to_do = specs.dup
+ seen = {}
+
+ until to_do.empty? do
+ spec = to_do.shift
+ next if spec.nil? or seen[spec.name]
+ seen[spec.name] = true
+
+ deps = spec.runtime_dependencies
+ deps |= spec.development_dependencies if @development
+
+ deps.each do |dep|
+ results = find_gems_with_sources(dep).reverse
+
+ results.reject! do |dep_spec,|
+ to_do.push dep_spec
+
+ @source_index.any? do |_, installed_spec|
+ dep.name == installed_spec.name and
+ dep.version_requirements.satisfied_by? installed_spec.version
+ end
+ end
+
+ results.each do |dep_spec, source_uri|
+ next if seen[dep_spec.name]
+ @specs_and_sources << [dep_spec, source_uri]
+ dependency_list.add dep_spec
+ end
+ end
+ end
+ end
+
+ @gems_to_install = dependency_list.dependency_order.reverse
+ end
+
+ ##
+ # Finds a spec and the source_uri it came from for gem +gem_name+ and
+ # +version+. Returns an Array of specs and sources required for
+ # installation of the gem.
+
+ def find_spec_by_name_and_version gem_name, version = Gem::Requirement.default
+ spec_and_source = nil
+
+ glob = if File::ALT_SEPARATOR then
+ gem_name.gsub File::ALT_SEPARATOR, File::SEPARATOR
+ else
+ gem_name
+ end
+
+ local_gems = Dir["#{glob}*"].sort.reverse
+
+ unless local_gems.empty? then
+ local_gems.each do |gem_file|
+ next unless gem_file =~ /gem$/
+ begin
+ spec = Gem::Format.from_file_by_path(gem_file).spec
+ spec_and_source = [spec, gem_file]
+ break
+ rescue SystemCallError, Gem::Package::FormatError
+ end
+ end
+ end
+
+ if spec_and_source.nil? then
+ dep = Gem::Dependency.new gem_name, version
+ spec_and_sources = find_gems_with_sources(dep).reverse
+
+ spec_and_source = spec_and_sources.find { |spec, source|
+ Gem::Platform.match spec.platform
+ }
+ end
+
+ if spec_and_source.nil? then
+ raise Gem::GemNotFoundException,
+ "could not find gem #{gem_name} locally or in a repository"
+ end
+
+ @specs_and_sources = [spec_and_source]
+ end
+
+ ##
+ # Installs the gem and all its dependencies. Returns an Array of installed
+ # gems specifications.
+
+ def install dep_or_name, version = Gem::Requirement.default
+ if String === dep_or_name then
+ find_spec_by_name_and_version dep_or_name, version
+ else
+ @specs_and_sources = [find_gems_with_sources(dep_or_name).last]
+ end
+
+ @installed_gems = []
+
+ gather_dependencies
+
+ @gems_to_install.each do |spec|
+ last = spec == @gems_to_install.last
+ # HACK is this test for full_name acceptable?
+ next if @source_index.any? { |n,_| n == spec.full_name } and not last
+
+ # TODO: make this sorta_verbose so other users can benefit from it
+ say "Installing gem #{spec.full_name}" if Gem.configuration.really_verbose
+
+ _, source_uri = @specs_and_sources.assoc spec
+ begin
+ local_gem_path = Gem::RemoteFetcher.fetcher.download spec, source_uri,
+ @cache_dir
+ rescue Gem::RemoteFetcher::FetchError
+ next if @force
+ raise
+ end
+
+ inst = Gem::Installer.new local_gem_path,
+ :bin_dir => @bin_dir,
+ :development => @development,
+ :env_shebang => @env_shebang,
+ :force => @force,
+ :format_executable => @format_executable,
+ :ignore_dependencies => @ignore_dependencies,
+ :install_dir => @install_dir,
+ :security_policy => @security_policy,
+ :source_index => @source_index,
+ :user_install => @user_install,
+ :wrappers => @wrappers
+
+ spec = inst.install
+
+ @installed_gems << spec
+ end
+
+ @installed_gems
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/dependency_list.rb b/ruby/lib/rubygems/dependency_list.rb
new file mode 100644
index 0000000..a129743
--- /dev/null
+++ b/ruby/lib/rubygems/dependency_list.rb
@@ -0,0 +1,165 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'tsort'
+
+class Gem::DependencyList
+
+ include TSort
+
+ def self.from_source_index(src_index)
+ deps = new
+
+ src_index.each do |full_name, spec|
+ deps.add spec
+ end
+
+ deps
+ end
+
+ def initialize
+ @specs = []
+ end
+
+ # Adds +gemspecs+ to the dependency list.
+ def add(*gemspecs)
+ @specs.push(*gemspecs)
+ end
+
+ # Return a list of the specifications in the dependency list,
+ # sorted in order so that no spec in the list depends on a gem
+ # earlier in the list.
+ #
+ # This is useful when removing gems from a set of installed gems.
+ # By removing them in the returned order, you don't get into as
+ # many dependency issues.
+ #
+ # If there are circular dependencies (yuck!), then gems will be
+ # returned in order until only the circular dependents and anything
+ # they reference are left. Then arbitrary gemspecs will be returned
+ # until the circular dependency is broken, after which gems will be
+ # returned in dependency order again.
+ def dependency_order
+ sorted = strongly_connected_components.flatten
+
+ result = []
+ seen = {}
+
+ sorted.each do |spec|
+ if index = seen[spec.name] then
+ if result[index].version < spec.version then
+ result[index] = spec
+ end
+ else
+ seen[spec.name] = result.length
+ result << spec
+ end
+ end
+
+ result.reverse
+ end
+
+ def find_name(full_name)
+ @specs.find { |spec| spec.full_name == full_name }
+ end
+
+ # Are all the dependencies in the list satisfied?
+ def ok?
+ @specs.all? do |spec|
+ spec.runtime_dependencies.all? do |dep|
+ @specs.find { |s| s.satisfies_requirement? dep }
+ end
+ end
+ end
+
+ # Is is ok to remove a gem from the dependency list?
+ #
+ # If removing the gemspec creates breaks a currently ok dependency,
+ # then it is NOT ok to remove the gem.
+ def ok_to_remove?(full_name)
+ gem_to_remove = find_name full_name
+
+ siblings = @specs.find_all { |s|
+ s.name == gem_to_remove.name &&
+ s.full_name != gem_to_remove.full_name
+ }
+
+ deps = []
+
+ @specs.each do |spec|
+ spec.dependencies.each do |dep|
+ deps << dep if gem_to_remove.satisfies_requirement?(dep)
+ end
+ end
+
+ deps.all? { |dep|
+ siblings.any? { |s|
+ s.satisfies_requirement? dep
+ }
+ }
+ end
+
+ def remove_by_name(full_name)
+ @specs.delete_if { |spec| spec.full_name == full_name }
+ end
+
+ # Return a hash of predecessors. <tt>result[spec]</tt> is an
+ # Array of gemspecs that have a dependency satisfied by the named
+ # spec.
+ def spec_predecessors
+ result = Hash.new { |h,k| h[k] = [] }
+
+ specs = @specs.sort.reverse
+
+ specs.each do |spec|
+ specs.each do |other|
+ next if spec == other
+
+ other.dependencies.each do |dep|
+ if spec.satisfies_requirement? dep then
+ result[spec] << other
+ end
+ end
+ end
+ end
+
+ result
+ end
+
+ def tsort_each_node(&block)
+ @specs.each(&block)
+ end
+
+ def tsort_each_child(node, &block)
+ specs = @specs.sort.reverse
+
+ node.dependencies.each do |dep|
+ specs.each do |spec|
+ if spec.satisfies_requirement? dep then
+ begin
+ yield spec
+ rescue TSort::Cyclic
+ end
+ break
+ end
+ end
+ end
+ end
+
+ private
+
+ # Count the number of gemspecs in the list +specs+ that are not in
+ # +ignored+.
+ def active_count(specs, ignored)
+ result = 0
+ specs.each do |spec|
+ result += 1 unless ignored[spec.full_name]
+ end
+ result
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/digest/digest_adapter.rb b/ruby/lib/rubygems/digest/digest_adapter.rb
new file mode 100644
index 0000000..d5a00b0
--- /dev/null
+++ b/ruby/lib/rubygems/digest/digest_adapter.rb
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+module Gem
+
+ # There is an incompatibility between the way Ruby 1.8.5 and 1.8.6
+ # handles digests. This DigestAdapter will take a pre-1.8.6 digest
+ # and adapt it to the 1.8.6 API.
+ #
+ # Note that only the digest and hexdigest methods are adapted,
+ # since these are the only functions used by Gems.
+ #
+ class DigestAdapter
+
+ # Initialize a digest adapter.
+ def initialize(digest_class)
+ @digest_class = digest_class
+ end
+
+ # Return a new digester. Since we are only implementing the stateless
+ # methods, we will return ourself as the instance.
+ def new
+ self
+ end
+
+ # Return the digest of +string+ as a hex string.
+ def hexdigest(string)
+ @digest_class.new(string).hexdigest
+ end
+
+ # Return the digest of +string+ as a binary string.
+ def digest(string)
+ @digest_class.new(string).digest
+ end
+ end
+end \ No newline at end of file
diff --git a/ruby/lib/rubygems/digest/md5.rb b/ruby/lib/rubygems/digest/md5.rb
new file mode 100644
index 0000000..f924579
--- /dev/null
+++ b/ruby/lib/rubygems/digest/md5.rb
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'digest/md5'
+
+# :stopdoc:
+module Gem
+ if RUBY_VERSION >= '1.8.6'
+ MD5 = Digest::MD5
+ else
+ require 'rubygems/digest/digest_adapter'
+ MD5 = DigestAdapter.new(Digest::MD5)
+ def MD5.md5(string)
+ self.hexdigest(string)
+ end
+ end
+end
+# :startdoc:
+
diff --git a/ruby/lib/rubygems/digest/sha1.rb b/ruby/lib/rubygems/digest/sha1.rb
new file mode 100644
index 0000000..2a6245d
--- /dev/null
+++ b/ruby/lib/rubygems/digest/sha1.rb
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'digest/sha1'
+
+module Gem
+ if RUBY_VERSION >= '1.8.6'
+ SHA1 = Digest::SHA1
+ else
+ require 'rubygems/digest/digest_adapter'
+ SHA1 = DigestAdapter.new(Digest::SHA1)
+ end
+end \ No newline at end of file
diff --git a/ruby/lib/rubygems/digest/sha2.rb b/ruby/lib/rubygems/digest/sha2.rb
new file mode 100644
index 0000000..7bef16a
--- /dev/null
+++ b/ruby/lib/rubygems/digest/sha2.rb
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'digest/sha2'
+
+module Gem
+ if RUBY_VERSION >= '1.8.6'
+ SHA256 = Digest::SHA256
+ else
+ require 'rubygems/digest/digest_adapter'
+ SHA256 = DigestAdapter.new(Digest::SHA256)
+ end
+end
diff --git a/ruby/lib/rubygems/doc_manager.rb b/ruby/lib/rubygems/doc_manager.rb
new file mode 100644
index 0000000..00ef4c5
--- /dev/null
+++ b/ruby/lib/rubygems/doc_manager.rb
@@ -0,0 +1,214 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'fileutils'
+require 'rubygems'
+
+##
+# The documentation manager generates RDoc and RI for RubyGems.
+
+class Gem::DocManager
+
+ include Gem::UserInteraction
+
+ @configured_args = []
+
+ def self.configured_args
+ @configured_args ||= []
+ end
+
+ def self.configured_args=(args)
+ case args
+ when Array
+ @configured_args = args
+ when String
+ @configured_args = args.split
+ end
+ end
+
+ ##
+ # Load RDoc from a gem if it is available, otherwise from Ruby's stdlib
+
+ def self.load_rdoc
+ begin
+ gem 'rdoc'
+ rescue Gem::LoadError
+ # use built-in RDoc
+ end
+
+ begin
+ require 'rdoc/rdoc'
+ rescue LoadError => e
+ raise Gem::DocumentError,
+ "ERROR: RDoc documentation generator not installed!"
+ end
+ end
+
+ ##
+ # Updates the RI cache for RDoc 2 if it is installed
+
+ def self.update_ri_cache
+ load_rdoc rescue return
+
+ return unless defined? RDoc::VERSION # RDoc 1 does not have VERSION
+
+ require 'rdoc/ri/driver'
+
+ options = {
+ :use_cache => true,
+ :use_system => true,
+ :use_site => true,
+ :use_home => true,
+ :use_gems => true,
+ :formatter => RDoc::RI::Formatter,
+ }
+
+ driver = RDoc::RI::Driver.new(options).class_cache
+ end
+
+ ##
+ # Create a document manager for +spec+. +rdoc_args+ contains arguments for
+ # RDoc (template etc.) as a String.
+
+ def initialize(spec, rdoc_args="")
+ @spec = spec
+ @doc_dir = File.join(spec.installation_path, "doc", spec.full_name)
+ @rdoc_args = rdoc_args.nil? ? [] : rdoc_args.split
+ end
+
+ ##
+ # Is the RDoc documentation installed?
+
+ def rdoc_installed?
+ File.exist?(File.join(@doc_dir, "rdoc"))
+ end
+
+ ##
+ # Generate the RI documents for this gem spec.
+ #
+ # Note that if both RI and RDoc documents are generated from the same
+ # process, the RI docs should be done first (a likely bug in RDoc will cause
+ # RI docs generation to fail if run after RDoc).
+
+ def generate_ri
+ if @spec.has_rdoc then
+ setup_rdoc
+ install_ri # RDoc bug, ri goes first
+ end
+
+ FileUtils.mkdir_p @doc_dir unless File.exist?(@doc_dir)
+ end
+
+ ##
+ # Generate the RDoc documents for this gem spec.
+ #
+ # Note that if both RI and RDoc documents are generated from the same
+ # process, the RI docs should be done first (a likely bug in RDoc will cause
+ # RI docs generation to fail if run after RDoc).
+
+ def generate_rdoc
+ if @spec.has_rdoc then
+ setup_rdoc
+ install_rdoc
+ end
+
+ FileUtils.mkdir_p @doc_dir unless File.exist?(@doc_dir)
+ end
+
+ ##
+ # Generate and install RDoc into the documentation directory
+
+ def install_rdoc
+ rdoc_dir = File.join @doc_dir, 'rdoc'
+
+ FileUtils.rm_rf rdoc_dir
+
+ say "Installing RDoc documentation for #{@spec.full_name}..."
+ run_rdoc '--op', rdoc_dir
+ end
+
+ ##
+ # Generate and install RI into the documentation directory
+
+ def install_ri
+ ri_dir = File.join @doc_dir, 'ri'
+
+ FileUtils.rm_rf ri_dir
+
+ say "Installing ri documentation for #{@spec.full_name}..."
+ run_rdoc '--ri', '--op', ri_dir
+ end
+
+ ##
+ # Run RDoc with +args+, which is an ARGV style argument list
+
+ def run_rdoc(*args)
+ args << @spec.rdoc_options
+ args << self.class.configured_args
+ args << '--quiet'
+ args << @spec.require_paths.clone
+ args << @spec.extra_rdoc_files
+ args = args.flatten.map do |arg| arg.to_s end
+
+ r = RDoc::RDoc.new
+
+ old_pwd = Dir.pwd
+ Dir.chdir(@spec.full_gem_path)
+ begin
+ r.document args
+ rescue Errno::EACCES => e
+ dirname = File.dirname e.message.split("-")[1].strip
+ raise Gem::FilePermissionError.new(dirname)
+ rescue RuntimeError => ex
+ alert_error "While generating documentation for #{@spec.full_name}"
+ ui.errs.puts "... MESSAGE: #{ex}"
+ ui.errs.puts "... RDOC args: #{args.join(' ')}"
+ ui.errs.puts "\t#{ex.backtrace.join "\n\t"}" if
+ Gem.configuration.backtrace
+ ui.errs.puts "(continuing with the rest of the installation)"
+ ensure
+ Dir.chdir(old_pwd)
+ end
+ end
+
+ def setup_rdoc
+ if File.exist?(@doc_dir) && !File.writable?(@doc_dir) then
+ raise Gem::FilePermissionError.new(@doc_dir)
+ end
+
+ FileUtils.mkdir_p @doc_dir unless File.exist?(@doc_dir)
+
+ self.class.load_rdoc
+ end
+
+ ##
+ # Remove RDoc and RI documentation
+
+ def uninstall_doc
+ raise Gem::FilePermissionError.new(@spec.installation_path) unless
+ File.writable? @spec.installation_path
+
+ original_name = [
+ @spec.name, @spec.version, @spec.original_platform].join '-'
+
+ doc_dir = File.join @spec.installation_path, 'doc', @spec.full_name
+ unless File.directory? doc_dir then
+ doc_dir = File.join @spec.installation_path, 'doc', original_name
+ end
+
+ FileUtils.rm_rf doc_dir
+
+ ri_dir = File.join @spec.installation_path, 'ri', @spec.full_name
+
+ unless File.directory? ri_dir then
+ ri_dir = File.join @spec.installation_path, 'ri', original_name
+ end
+
+ FileUtils.rm_rf ri_dir
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/exceptions.rb b/ruby/lib/rubygems/exceptions.rb
new file mode 100644
index 0000000..c37507c
--- /dev/null
+++ b/ruby/lib/rubygems/exceptions.rb
@@ -0,0 +1,84 @@
+require 'rubygems'
+
+##
+# Base exception class for RubyGems. All exception raised by RubyGems are a
+# subclass of this one.
+class Gem::Exception < RuntimeError; end
+
+class Gem::CommandLineError < Gem::Exception; end
+
+class Gem::DependencyError < Gem::Exception; end
+
+class Gem::DependencyRemovalException < Gem::Exception; end
+
+##
+# Raised when attempting to uninstall a gem that isn't in GEM_HOME.
+
+class Gem::GemNotInHomeException < Gem::Exception
+ attr_accessor :spec
+end
+
+class Gem::DocumentError < Gem::Exception; end
+
+##
+# Potentially raised when a specification is validated.
+class Gem::EndOfYAMLException < Gem::Exception; end
+
+##
+# Signals that a file permission error is preventing the user from
+# installing in the requested directories.
+class Gem::FilePermissionError < Gem::Exception
+ def initialize(path)
+ super("You don't have write permissions into the #{path} directory.")
+ end
+end
+
+##
+# Used to raise parsing and loading errors
+class Gem::FormatException < Gem::Exception
+ attr_accessor :file_path
+end
+
+class Gem::GemNotFoundException < Gem::Exception; end
+
+class Gem::InstallError < Gem::Exception; end
+
+##
+# Potentially raised when a specification is validated.
+class Gem::InvalidSpecificationException < Gem::Exception; end
+
+class Gem::OperationNotSupportedError < Gem::Exception; end
+
+##
+# Signals that a remote operation cannot be conducted, probably due to not
+# being connected (or just not finding host).
+#--
+# TODO: create a method that tests connection to the preferred gems server.
+# All code dealing with remote operations will want this. Failure in that
+# method should raise this error.
+class Gem::RemoteError < Gem::Exception; end
+
+class Gem::RemoteInstallationCancelled < Gem::Exception; end
+
+class Gem::RemoteInstallationSkipped < Gem::Exception; end
+
+##
+# Represents an error communicating via HTTP.
+class Gem::RemoteSourceException < Gem::Exception; end
+
+class Gem::VerificationError < Gem::Exception; end
+
+##
+# Raised to indicate that a system exit should occur with the specified
+# exit_code
+
+class Gem::SystemExitException < SystemExit
+ attr_accessor :exit_code
+
+ def initialize(exit_code)
+ @exit_code = exit_code
+
+ super "Exiting RubyGems with exit_code #{exit_code}"
+ end
+
+end
diff --git a/ruby/lib/rubygems/ext.rb b/ruby/lib/rubygems/ext.rb
new file mode 100644
index 0000000..97ee762
--- /dev/null
+++ b/ruby/lib/rubygems/ext.rb
@@ -0,0 +1,18 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+
+##
+# Classes for building C extensions live here.
+
+module Gem::Ext; end
+
+require 'rubygems/ext/builder'
+require 'rubygems/ext/configure_builder'
+require 'rubygems/ext/ext_conf_builder'
+require 'rubygems/ext/rake_builder'
+
diff --git a/ruby/lib/rubygems/ext/builder.rb b/ruby/lib/rubygems/ext/builder.rb
new file mode 100644
index 0000000..36e9ec1
--- /dev/null
+++ b/ruby/lib/rubygems/ext/builder.rb
@@ -0,0 +1,56 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems/ext'
+
+class Gem::Ext::Builder
+
+ def self.class_name
+ name =~ /Ext::(.*)Builder/
+ $1.downcase
+ end
+
+ def self.make(dest_path, results)
+ unless File.exist? 'Makefile' then
+ raise Gem::InstallError, "Makefile not found:\n\n#{results.join "\n"}"
+ end
+
+ mf = File.read('Makefile')
+ mf = mf.gsub(/^RUBYARCHDIR\s*=\s*\$[^$]*/, "RUBYARCHDIR = #{dest_path}")
+ mf = mf.gsub(/^RUBYLIBDIR\s*=\s*\$[^$]*/, "RUBYLIBDIR = #{dest_path}")
+
+ File.open('Makefile', 'wb') {|f| f.print mf}
+
+ make_program = ENV['make']
+ unless make_program then
+ make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
+ end
+
+ ['', ' install'].each do |target|
+ cmd = "#{make_program}#{target}"
+ results << cmd
+ results << `#{cmd} #{redirector}`
+
+ raise Gem::InstallError, "make#{target} failed:\n\n#{results}" unless
+ $?.success?
+ end
+ end
+
+ def self.redirector
+ '2>&1'
+ end
+
+ def self.run(command, results)
+ results << command
+ results << `#{command} #{redirector}`
+
+ unless $?.success? then
+ raise Gem::InstallError, "#{class_name} failed:\n\n#{results.join "\n"}"
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/ext/configure_builder.rb b/ruby/lib/rubygems/ext/configure_builder.rb
new file mode 100644
index 0000000..1cde691
--- /dev/null
+++ b/ruby/lib/rubygems/ext/configure_builder.rb
@@ -0,0 +1,24 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems/ext/builder'
+
+class Gem::Ext::ConfigureBuilder < Gem::Ext::Builder
+
+ def self.build(extension, directory, dest_path, results)
+ unless File.exist?('Makefile') then
+ cmd = "sh ./configure --prefix=#{dest_path}"
+
+ run cmd, results
+ end
+
+ make dest_path, results
+
+ results
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/ext/ext_conf_builder.rb b/ruby/lib/rubygems/ext/ext_conf_builder.rb
new file mode 100644
index 0000000..cbe0e80
--- /dev/null
+++ b/ruby/lib/rubygems/ext/ext_conf_builder.rb
@@ -0,0 +1,23 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems/ext/builder'
+
+class Gem::Ext::ExtConfBuilder < Gem::Ext::Builder
+
+ def self.build(extension, directory, dest_path, results)
+ cmd = "#{Gem.ruby} #{File.basename extension}"
+ cmd << " #{ARGV.join ' '}" unless ARGV.empty?
+
+ run cmd, results
+
+ make dest_path, results
+
+ results
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/ext/rake_builder.rb b/ruby/lib/rubygems/ext/rake_builder.rb
new file mode 100644
index 0000000..0c64e61
--- /dev/null
+++ b/ruby/lib/rubygems/ext/rake_builder.rb
@@ -0,0 +1,27 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems/ext/builder'
+
+class Gem::Ext::RakeBuilder < Gem::Ext::Builder
+
+ def self.build(extension, directory, dest_path, results)
+ if File.basename(extension) =~ /mkrf_conf/i then
+ cmd = "#{Gem.ruby} #{File.basename extension}"
+ cmd << " #{ARGV.join " "}" unless ARGV.empty?
+ run cmd, results
+ end
+
+ cmd = ENV['rake'] || 'rake'
+ cmd += " RUBYARCHDIR=#{dest_path} RUBYLIBDIR=#{dest_path}" # ENV is frozen
+
+ run cmd, results
+
+ results
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/format.rb b/ruby/lib/rubygems/format.rb
new file mode 100644
index 0000000..7dc127d
--- /dev/null
+++ b/ruby/lib/rubygems/format.rb
@@ -0,0 +1,87 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'fileutils'
+
+require 'rubygems/package'
+
+module Gem
+
+ ##
+ # The format class knows the guts of the RubyGem .gem file format
+ # and provides the capability to read gem files
+ #
+ class Format
+ attr_accessor :spec, :file_entries, :gem_path
+ extend Gem::UserInteraction
+
+ ##
+ # Constructs an instance of a Format object, representing the gem's
+ # data structure.
+ #
+ # gem:: [String] The file name of the gem
+ #
+ def initialize(gem_path)
+ @gem_path = gem_path
+ end
+
+ ##
+ # Reads the named gem file and returns a Format object, representing
+ # the data from the gem file
+ #
+ # file_path:: [String] Path to the gem file
+ #
+ def self.from_file_by_path(file_path, security_policy = nil)
+ format = nil
+
+ unless File.exist?(file_path)
+ raise Gem::Exception, "Cannot load gem at [#{file_path}] in #{Dir.pwd}"
+ end
+
+ # check for old version gem
+ if File.read(file_path, 20).include?("MD5SUM =")
+ require 'rubygems/old_format'
+
+ format = OldFormat.from_file_by_path(file_path)
+ else
+ open file_path, Gem.binary_mode do |io|
+ format = from_io io, file_path, security_policy
+ end
+ end
+
+ return format
+ end
+
+ ##
+ # Reads a gem from an io stream and returns a Format object, representing
+ # the data from the gem file
+ #
+ # io:: [IO] Stream from which to read the gem
+ #
+ def self.from_io(io, gem_path="(io)", security_policy = nil)
+ format = new gem_path
+
+ Package.open io, 'r', security_policy do |pkg|
+ format.spec = pkg.metadata
+ format.file_entries = []
+
+ pkg.each do |entry|
+ size = entry.header.size
+ mode = entry.header.mode
+
+ format.file_entries << [{
+ "size" => size, "mode" => mode, "path" => entry.full_name,
+ },
+ entry.read
+ ]
+ end
+ end
+
+ format
+ end
+
+ end
+end
diff --git a/ruby/lib/rubygems/gem_openssl.rb b/ruby/lib/rubygems/gem_openssl.rb
new file mode 100644
index 0000000..1456f2d
--- /dev/null
+++ b/ruby/lib/rubygems/gem_openssl.rb
@@ -0,0 +1,83 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+# Some system might not have OpenSSL installed, therefore the core
+# library file openssl might not be available. We localize testing
+# for the presence of OpenSSL in this file.
+
+module Gem
+ class << self
+ # Is SSL (used by the signing commands) available on this
+ # platform?
+ def ssl_available?
+ require 'rubygems/gem_openssl'
+ @ssl_available
+ end
+
+ # Set the value of the ssl_available flag.
+ attr_writer :ssl_available
+
+ # Ensure that SSL is available. Throw an exception if it is not.
+ def ensure_ssl_available
+ unless ssl_available?
+ fail Gem::Exception, "SSL is not installed on this system"
+ end
+ end
+ end
+end
+
+begin
+ require 'openssl'
+
+ # Reference a constant defined in the .rb portion of ssl (just to
+ # make sure that part is loaded too).
+
+ dummy = OpenSSL::Digest::SHA1
+
+ Gem.ssl_available = true
+
+ class OpenSSL::X509::Certificate # :nodoc:
+ # Check the validity of this certificate.
+ def check_validity(issuer_cert = nil, time = Time.now)
+ ret = if @not_before && @not_before > time
+ [false, :expired, "not valid before '#@not_before'"]
+ elsif @not_after && @not_after < time
+ [false, :expired, "not valid after '#@not_after'"]
+ elsif issuer_cert && !verify(issuer_cert.public_key)
+ [false, :issuer, "#{issuer_cert.subject} is not issuer"]
+ else
+ [true, :ok, 'Valid certificate']
+ end
+
+ # return hash
+ { :is_valid => ret[0], :error => ret[1], :desc => ret[2] }
+ end
+ end
+
+rescue LoadError, StandardError
+ Gem.ssl_available = false
+end
+
+module Gem::SSL
+
+ # We make our own versions of the constants here. This allows us
+ # to reference the constants, even though some systems might not
+ # have SSL installed in the Ruby core package.
+ #
+ # These constants are only used during load time. At runtime, any
+ # method that makes a direct reference to SSL software must be
+ # protected with a Gem.ensure_ssl_available call.
+ #
+ if Gem.ssl_available? then
+ PKEY_RSA = OpenSSL::PKey::RSA
+ DIGEST_SHA1 = OpenSSL::Digest::SHA1
+ else
+ PKEY_RSA = :rsa
+ DIGEST_SHA1 = :sha1
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/gem_path_searcher.rb b/ruby/lib/rubygems/gem_path_searcher.rb
new file mode 100644
index 0000000..e2b8543
--- /dev/null
+++ b/ruby/lib/rubygems/gem_path_searcher.rb
@@ -0,0 +1,100 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+
+##
+# GemPathSearcher has the capability to find loadable files inside
+# gems. It generates data up front to speed up searches later.
+
+class Gem::GemPathSearcher
+
+ ##
+ # Initialise the data we need to make searches later.
+
+ def initialize
+ # We want a record of all the installed gemspecs, in the order
+ # we wish to examine them.
+ @gemspecs = init_gemspecs
+ # Map gem spec to glob of full require_path directories.
+ # Preparing this information may speed up searches later.
+ @lib_dirs = {}
+ @gemspecs.each do |spec|
+ @lib_dirs[spec.object_id] = lib_dirs_for(spec)
+ end
+ end
+
+ ##
+ # Look in all the installed gems until a matching _path_ is found.
+ # Return the _gemspec_ of the gem where it was found. If no match
+ # is found, return nil.
+ #
+ # The gems are searched in alphabetical order, and in reverse
+ # version order.
+ #
+ # For example:
+ #
+ # find('log4r') # -> (log4r-1.1 spec)
+ # find('log4r.rb') # -> (log4r-1.1 spec)
+ # find('rake/rdoctask') # -> (rake-0.4.12 spec)
+ # find('foobarbaz') # -> nil
+ #
+ # Matching paths can have various suffixes ('.rb', '.so', and
+ # others), which may or may not already be attached to _file_.
+ # This method doesn't care about the full filename that matches;
+ # only that there is a match.
+
+ def find(path)
+ @gemspecs.find do |spec| matching_file? spec, path end
+ end
+
+ ##
+ # Works like #find, but finds all gemspecs matching +path+.
+
+ def find_all(path)
+ @gemspecs.select do |spec|
+ matching_file? spec, path
+ end
+ end
+
+ ##
+ # Attempts to find a matching path using the require_paths of the given
+ # +spec+.
+
+ def matching_file?(spec, path)
+ !matching_files(spec, path).empty?
+ end
+
+ ##
+ # Returns files matching +path+ in +spec+.
+ #--
+ # Some of the intermediate results are cached in @lib_dirs for speed.
+
+ def matching_files(spec, path)
+ glob = File.join @lib_dirs[spec.object_id], "#{path}#{Gem.suffix_pattern}"
+ Dir[glob].select { |f| File.file? f.untaint }
+ end
+
+ ##
+ # Return a list of all installed gemspecs, sorted by alphabetical order and
+ # in reverse version order.
+
+ def init_gemspecs
+ Gem.source_index.map { |_, spec| spec }.sort { |a,b|
+ (a.name <=> b.name).nonzero? || (b.version <=> a.version)
+ }
+ end
+
+ ##
+ # Returns library directories glob for a gemspec. For example,
+ # '/usr/local/lib/ruby/gems/1.8/gems/foobar-1.0/{lib,ext}'
+
+ def lib_dirs_for(spec)
+ "#{spec.full_gem_path}/{#{spec.require_paths.join(',')}}"
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/gem_runner.rb b/ruby/lib/rubygems/gem_runner.rb
new file mode 100644
index 0000000..5f91398
--- /dev/null
+++ b/ruby/lib/rubygems/gem_runner.rb
@@ -0,0 +1,58 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems/command_manager'
+require 'rubygems/config_file'
+require 'rubygems/doc_manager'
+
+module Gem
+
+ ####################################################################
+ # Run an instance of the gem program.
+ #
+ class GemRunner
+
+ def initialize(options={})
+ @command_manager_class = options[:command_manager] || Gem::CommandManager
+ @config_file_class = options[:config_file] || Gem::ConfigFile
+ @doc_manager_class = options[:doc_manager] || Gem::DocManager
+ end
+
+ # Run the gem command with the following arguments.
+ def run(args)
+ start_time = Time.now
+ do_configuration(args)
+ cmd = @command_manager_class.instance
+ cmd.command_names.each do |command_name|
+ config_args = Gem.configuration[command_name]
+ config_args = case config_args
+ when String
+ config_args.split ' '
+ else
+ Array(config_args)
+ end
+ Command.add_specific_extra_args command_name, config_args
+ end
+ cmd.run(Gem.configuration.args)
+ end_time = Time.now
+ if Gem.configuration.benchmark
+ printf "\nExecution time: %0.2f seconds.\n", end_time-start_time
+ puts "Press Enter to finish"
+ STDIN.gets
+ end
+ end
+
+ private
+
+ def do_configuration(args)
+ Gem.configuration = @config_file_class.new(args)
+ Gem.use_paths(Gem.configuration[:gemhome], Gem.configuration[:gempath])
+ Gem::Command.extra_args = Gem.configuration[:gem]
+ @doc_manager_class.configured_args = Gem.configuration[:rdoc]
+ end
+
+ end # class
+end # module
diff --git a/ruby/lib/rubygems/indexer.rb b/ruby/lib/rubygems/indexer.rb
new file mode 100644
index 0000000..e2dd57d
--- /dev/null
+++ b/ruby/lib/rubygems/indexer.rb
@@ -0,0 +1,370 @@
+require 'fileutils'
+require 'tmpdir'
+require 'zlib'
+
+require 'rubygems'
+require 'rubygems/format'
+
+begin
+ require 'builder/xchar'
+rescue LoadError
+end
+
+##
+# Top level class for building the gem repository index.
+
+class Gem::Indexer
+
+ include Gem::UserInteraction
+
+ ##
+ # Index install location
+
+ attr_reader :dest_directory
+
+ ##
+ # Index build directory
+
+ attr_reader :directory
+
+ ##
+ # Create an indexer that will index the gems in +directory+.
+
+ def initialize(directory)
+ unless ''.respond_to? :to_xs then
+ fail "Gem::Indexer requires that the XML Builder library be installed:" \
+ "\n\tgem install builder"
+ end
+
+ @dest_directory = directory
+ @directory = File.join Dir.tmpdir, "gem_generate_index_#{$$}"
+
+ marshal_name = "Marshal.#{Gem.marshal_version}"
+
+ @master_index = File.join @directory, 'yaml'
+ @marshal_index = File.join @directory, marshal_name
+
+ @quick_dir = File.join @directory, 'quick'
+
+ @quick_marshal_dir = File.join @quick_dir, marshal_name
+
+ @quick_index = File.join @quick_dir, 'index'
+ @latest_index = File.join @quick_dir, 'latest_index'
+
+ @specs_index = File.join @directory, "specs.#{Gem.marshal_version}"
+ @latest_specs_index = File.join @directory,
+ "latest_specs.#{Gem.marshal_version}"
+
+ files = [
+ @specs_index,
+ "#{@specs_index}.gz",
+ @latest_specs_index,
+ "#{@latest_specs_index}.gz",
+ @quick_dir,
+ @master_index,
+ "#{@master_index}.Z",
+ @marshal_index,
+ "#{@marshal_index}.Z",
+ ]
+
+ @files = files.map do |path|
+ path.sub @directory, ''
+ end
+ end
+
+ ##
+ # Abbreviate the spec for downloading. Abbreviated specs are only used for
+ # searching, downloading and related activities and do not need deployment
+ # specific information (e.g. list of files). So we abbreviate the spec,
+ # making it much smaller for quicker downloads.
+
+ def abbreviate(spec)
+ spec.files = []
+ spec.test_files = []
+ spec.rdoc_options = []
+ spec.extra_rdoc_files = []
+ spec.cert_chain = []
+ spec
+ end
+
+ ##
+ # Build various indicies
+
+ def build_indicies(index)
+ progress = ui.progress_reporter index.size,
+ "Generating quick index gemspecs for #{index.size} gems",
+ "Complete"
+
+ index.each do |original_name, spec|
+ spec_file_name = "#{original_name}.gemspec.rz"
+ yaml_name = File.join @quick_dir, spec_file_name
+ marshal_name = File.join @quick_marshal_dir, spec_file_name
+
+ yaml_zipped = Gem.deflate spec.to_yaml
+ open yaml_name, 'wb' do |io| io.write yaml_zipped end
+
+ marshal_zipped = Gem.deflate Marshal.dump(spec)
+ open marshal_name, 'wb' do |io| io.write marshal_zipped end
+
+ progress.updated original_name
+ end
+
+ progress.done
+
+ say "Generating specs index"
+
+ open @specs_index, 'wb' do |io|
+ specs = index.sort.map do |_, spec|
+ platform = spec.original_platform
+ platform = Gem::Platform::RUBY if platform.nil? or platform.empty?
+ [spec.name, spec.version, platform]
+ end
+
+ specs = compact_specs specs
+
+ Marshal.dump specs, io
+ end
+
+ say "Generating latest specs index"
+
+ open @latest_specs_index, 'wb' do |io|
+ specs = index.latest_specs.sort.map do |spec|
+ platform = spec.original_platform
+ platform = Gem::Platform::RUBY if platform.nil? or platform.empty?
+ [spec.name, spec.version, platform]
+ end
+
+ specs = compact_specs specs
+
+ Marshal.dump specs, io
+ end
+
+ say "Generating quick index"
+
+ quick_index = File.join @quick_dir, 'index'
+ open quick_index, 'wb' do |io|
+ io.puts index.sort.map { |_, spec| spec.original_name }
+ end
+
+ say "Generating latest index"
+
+ latest_index = File.join @quick_dir, 'latest_index'
+ open latest_index, 'wb' do |io|
+ io.puts index.latest_specs.sort.map { |spec| spec.original_name }
+ end
+
+ say "Generating Marshal master index"
+
+ open @marshal_index, 'wb' do |io|
+ io.write index.dump
+ end
+
+ progress = ui.progress_reporter index.size,
+ "Generating YAML master index for #{index.size} gems (this may take a while)",
+ "Complete"
+
+ open @master_index, 'wb' do |io|
+ io.puts "--- !ruby/object:#{index.class}"
+ io.puts "gems:"
+
+ gems = index.sort_by { |name, gemspec| gemspec.sort_obj }
+ gems.each do |original_name, gemspec|
+ yaml = gemspec.to_yaml.gsub(/^/, ' ')
+ yaml = yaml.sub(/\A ---/, '') # there's a needed extra ' ' here
+ io.print " #{original_name}:"
+ io.puts yaml
+
+ progress.updated original_name
+ end
+ end
+
+ progress.done
+
+ say "Compressing indicies"
+ # use gzip for future files.
+
+ compress quick_index, 'rz'
+ paranoid quick_index, 'rz'
+
+ compress latest_index, 'rz'
+ paranoid latest_index, 'rz'
+
+ compress @marshal_index, 'Z'
+ paranoid @marshal_index, 'Z'
+
+ compress @master_index, 'Z'
+ paranoid @master_index, 'Z'
+
+ gzip @specs_index
+ gzip @latest_specs_index
+ end
+
+ ##
+ # Collect specifications from .gem files from the gem directory.
+
+ def collect_specs
+ index = Gem::SourceIndex.new
+
+ progress = ui.progress_reporter gem_file_list.size,
+ "Loading #{gem_file_list.size} gems from #{@dest_directory}",
+ "Loaded all gems"
+
+ gem_file_list.each do |gemfile|
+ if File.size(gemfile.to_s) == 0 then
+ alert_warning "Skipping zero-length gem: #{gemfile}"
+ next
+ end
+
+ begin
+ spec = Gem::Format.from_file_by_path(gemfile).spec
+
+ unless gemfile =~ /\/#{Regexp.escape spec.original_name}.*\.gem\z/i then
+ alert_warning "Skipping misnamed gem: #{gemfile} => #{spec.full_name} (#{spec.original_name})"
+ next
+ end
+
+ abbreviate spec
+ sanitize spec
+
+ index.gems[spec.original_name] = spec
+
+ progress.updated spec.original_name
+
+ rescue SignalException => e
+ alert_error "Received signal, exiting"
+ raise
+ rescue Exception => e
+ alert_error "Unable to process #{gemfile}\n#{e.message} (#{e.class})\n\t#{e.backtrace.join "\n\t"}"
+ end
+ end
+
+ progress.done
+
+ index
+ end
+
+ ##
+ # Compacts Marshal output for the specs index data source by using identical
+ # objects as much as possible.
+
+ def compact_specs(specs)
+ names = {}
+ versions = {}
+ platforms = {}
+
+ specs.map do |(name, version, platform)|
+ names[name] = name unless names.include? name
+ versions[version] = version unless versions.include? version
+ platforms[platform] = platform unless platforms.include? platform
+
+ [names[name], versions[version], platforms[platform]]
+ end
+ end
+
+ ##
+ # Compress +filename+ with +extension+.
+
+ def compress(filename, extension)
+ data = Gem.read_binary filename
+
+ zipped = Gem.deflate data
+
+ open "#{filename}.#{extension}", 'wb' do |io|
+ io.write zipped
+ end
+ end
+
+ ##
+ # List of gem file names to index.
+
+ def gem_file_list
+ Dir.glob(File.join(@dest_directory, "gems", "*.gem"))
+ end
+
+ ##
+ # Builds and installs indexicies.
+
+ def generate_index
+ make_temp_directories
+ index = collect_specs
+ build_indicies index
+ install_indicies
+ rescue SignalException
+ ensure
+ FileUtils.rm_rf @directory
+ end
+
+ ##
+ # Zlib::GzipWriter wrapper that gzips +filename+ on disk.
+
+ def gzip(filename)
+ Zlib::GzipWriter.open "#{filename}.gz" do |io|
+ io.write Gem.read_binary(filename)
+ end
+ end
+
+ ##
+ # Install generated indicies into the destination directory.
+
+ def install_indicies
+ verbose = Gem.configuration.really_verbose
+
+ say "Moving index into production dir #{@dest_directory}" if verbose
+
+ @files.each do |file|
+ src_name = File.join @directory, file
+ dst_name = File.join @dest_directory, file
+
+ FileUtils.rm_rf dst_name, :verbose => verbose
+ FileUtils.mv src_name, @dest_directory, :verbose => verbose,
+ :force => true
+ end
+ end
+
+ ##
+ # Make directories for index generation
+
+ def make_temp_directories
+ FileUtils.rm_rf @directory
+ FileUtils.mkdir_p @directory, :mode => 0700
+ FileUtils.mkdir_p @quick_marshal_dir
+ end
+
+ ##
+ # Ensure +path+ and path with +extension+ are identical.
+
+ def paranoid(path, extension)
+ data = Gem.read_binary path
+ compressed_data = Gem.read_binary "#{path}.#{extension}"
+
+ unless data == Gem.inflate(compressed_data) then
+ raise "Compressed file #{compressed_path} does not match uncompressed file #{path}"
+ end
+ end
+
+ ##
+ # Sanitize the descriptive fields in the spec. Sometimes non-ASCII
+ # characters will garble the site index. Non-ASCII characters will
+ # be replaced by their XML entity equivalent.
+
+ def sanitize(spec)
+ spec.summary = sanitize_string(spec.summary)
+ spec.description = sanitize_string(spec.description)
+ spec.post_install_message = sanitize_string(spec.post_install_message)
+ spec.authors = spec.authors.collect { |a| sanitize_string(a) }
+
+ spec
+ end
+
+ ##
+ # Sanitize a single string.
+
+ def sanitize_string(string)
+ # HACK the #to_s is in here because RSpec has an Array of Arrays of
+ # Strings for authors. Need a way to disallow bad values on gempsec
+ # generation. (Probably won't happen.)
+ string ? string.to_s.to_xs : string
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/indexer/abstract_index_builder.rb b/ruby/lib/rubygems/indexer/abstract_index_builder.rb
new file mode 100644
index 0000000..5815dcd
--- /dev/null
+++ b/ruby/lib/rubygems/indexer/abstract_index_builder.rb
@@ -0,0 +1,88 @@
+require 'zlib'
+
+require 'rubygems/indexer'
+
+# Abstract base class for building gem indicies. Uses the template pattern
+# with subclass specialization in the +begin_index+, +end_index+ and +cleanup+
+# methods.
+class Gem::Indexer::AbstractIndexBuilder
+
+ # Directory to put index files in
+ attr_reader :directory
+
+ # File name of the generated index
+ attr_reader :filename
+
+ # List of written files/directories to move into production
+ attr_reader :files
+
+ def initialize(filename, directory)
+ @filename = filename
+ @directory = directory
+ @files = []
+ end
+
+ ##
+ # Build a Gem index. Yields to block to handle the details of the
+ # actual building. Calls +begin_index+, +end_index+ and +cleanup+ at
+ # appropriate times to customize basic operations.
+
+ def build
+ FileUtils.mkdir_p @directory unless File.exist? @directory
+ raise "not a directory: #{@directory}" unless File.directory? @directory
+
+ file_path = File.join @directory, @filename
+
+ @files << @filename
+
+ File.open file_path, "wb" do |file|
+ @file = file
+ start_index
+ yield
+ end_index
+ end
+
+ cleanup
+ ensure
+ @file = nil
+ end
+
+ ##
+ # Compress the given file.
+
+ def compress(filename, ext="rz")
+ data = open filename, 'rb' do |fp| fp.read end
+
+ zipped = zip data
+
+ File.open "#{filename}.#{ext}", "wb" do |file|
+ file.write zipped
+ end
+ end
+
+ # Called immediately before the yield in build. The index file is open and
+ # available as @file.
+ def start_index
+ end
+
+ # Called immediately after the yield in build. The index file is still open
+ # and available as @file.
+ def end_index
+ end
+
+ # Called from within builder after the index file has been closed.
+ def cleanup
+ end
+
+ # Return an uncompressed version of a compressed string.
+ def unzip(string)
+ Zlib::Inflate.inflate(string)
+ end
+
+ # Return a compressed version of the given string.
+ def zip(string)
+ Zlib::Deflate.deflate(string)
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/indexer/latest_index_builder.rb b/ruby/lib/rubygems/indexer/latest_index_builder.rb
new file mode 100644
index 0000000..a579858
--- /dev/null
+++ b/ruby/lib/rubygems/indexer/latest_index_builder.rb
@@ -0,0 +1,35 @@
+require 'rubygems/indexer'
+
+##
+# Construct the latest Gem index file.
+
+class Gem::Indexer::LatestIndexBuilder < Gem::Indexer::AbstractIndexBuilder
+
+ def start_index
+ super
+
+ @index = Gem::SourceIndex.new
+ end
+
+ def end_index
+ super
+
+ latest = @index.latest_specs.sort.map { |spec| spec.original_name }
+
+ @file.write latest.join("\n")
+ end
+
+ def cleanup
+ super
+
+ compress @file.path
+
+ @files.delete 'latest_index' # HACK installed via QuickIndexBuilder :/
+ end
+
+ def add(spec)
+ @index.add_spec(spec)
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/indexer/marshal_index_builder.rb b/ruby/lib/rubygems/indexer/marshal_index_builder.rb
new file mode 100644
index 0000000..e1a4d9f
--- /dev/null
+++ b/ruby/lib/rubygems/indexer/marshal_index_builder.rb
@@ -0,0 +1,17 @@
+require 'rubygems/indexer'
+
+# Construct the master Gem index file.
+class Gem::Indexer::MarshalIndexBuilder < Gem::Indexer::MasterIndexBuilder
+ def end_index
+ gems = {}
+ index = Gem::SourceIndex.new
+
+ @index.each do |name, gemspec|
+ gems[gemspec.original_name] = gemspec
+ end
+
+ index.instance_variable_get(:@gems).replace gems
+
+ @file.write index.dump
+ end
+end
diff --git a/ruby/lib/rubygems/indexer/master_index_builder.rb b/ruby/lib/rubygems/indexer/master_index_builder.rb
new file mode 100644
index 0000000..669ea5a
--- /dev/null
+++ b/ruby/lib/rubygems/indexer/master_index_builder.rb
@@ -0,0 +1,54 @@
+require 'rubygems/indexer'
+
+##
+# Construct the master Gem index file.
+
+class Gem::Indexer::MasterIndexBuilder < Gem::Indexer::AbstractIndexBuilder
+
+ def start_index
+ super
+ @index = Gem::SourceIndex.new
+ end
+
+ def end_index
+ super
+
+ @file.puts "--- !ruby/object:#{@index.class}"
+ @file.puts "gems:"
+
+ gems = @index.sort_by { |name, gemspec| gemspec.sort_obj }
+ gems.each do |name, gemspec|
+ yaml = gemspec.to_yaml.gsub(/^/, ' ')
+ yaml = yaml.sub(/\A ---/, '') # there's a needed extra ' ' here
+ @file.print " #{gemspec.original_name}:"
+ @file.puts yaml
+ end
+ end
+
+ def cleanup
+ super
+
+ index_file_name = File.join @directory, @filename
+
+ compress index_file_name, "Z"
+ paranoid index_file_name, "#{index_file_name}.Z"
+
+ @files << "#{@filename}.Z"
+ end
+
+ def add(spec)
+ @index.add_spec(spec)
+ end
+
+ private
+
+ def paranoid(path, compressed_path)
+ data = Gem.read_binary path
+ compressed_data = Gem.read_binary compressed_path
+
+ if data != unzip(compressed_data) then
+ raise "Compressed file #{compressed_path} does not match uncompressed file #{path}"
+ end
+ end
+
+end
diff --git a/ruby/lib/rubygems/indexer/quick_index_builder.rb b/ruby/lib/rubygems/indexer/quick_index_builder.rb
new file mode 100644
index 0000000..dc36179
--- /dev/null
+++ b/ruby/lib/rubygems/indexer/quick_index_builder.rb
@@ -0,0 +1,50 @@
+require 'rubygems/indexer'
+
+##
+# Construct a quick index file and all of the individual specs to support
+# incremental loading.
+
+class Gem::Indexer::QuickIndexBuilder < Gem::Indexer::AbstractIndexBuilder
+
+ def initialize(filename, directory)
+ directory = File.join directory, 'quick'
+
+ super filename, directory
+ end
+
+ def cleanup
+ super
+
+ quick_index_file = File.join @directory, @filename
+ compress quick_index_file
+
+ # the complete quick index is in a directory, so move it as a whole
+ @files.delete 'index'
+ @files << 'quick'
+ end
+
+ def add(spec)
+ @file.puts spec.original_name
+ add_yaml(spec)
+ add_marshal(spec)
+ end
+
+ def add_yaml(spec)
+ fn = File.join @directory, "#{spec.original_name}.gemspec.rz"
+ zipped = zip spec.to_yaml
+ File.open fn, "wb" do |gsfile| gsfile.write zipped end
+ end
+
+ def add_marshal(spec)
+ # HACK why does this not work in #initialize?
+ FileUtils.mkdir_p File.join(@directory, "Marshal.#{Gem.marshal_version}")
+
+ fn = File.join @directory, "Marshal.#{Gem.marshal_version}",
+ "#{spec.original_name}.gemspec.rz"
+
+ zipped = zip Marshal.dump(spec)
+ File.open fn, "wb" do |gsfile| gsfile.write zipped end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/install_update_options.rb b/ruby/lib/rubygems/install_update_options.rb
new file mode 100644
index 0000000..dd35acb
--- /dev/null
+++ b/ruby/lib/rubygems/install_update_options.rb
@@ -0,0 +1,113 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+require 'rubygems/security'
+
+##
+# Mixin methods for install and update options for Gem::Commands
+module Gem::InstallUpdateOptions
+
+ # Add the install/update options to the option parser.
+ def add_install_update_options
+ OptionParser.accept Gem::Security::Policy do |value|
+ value = Gem::Security::Policies[value]
+ raise OptionParser::InvalidArgument, value if value.nil?
+ value
+ end
+
+ add_option(:"Install/Update", '-i', '--install-dir DIR',
+ 'Gem repository directory to get installed',
+ 'gems') do |value, options|
+ options[:install_dir] = File.expand_path(value)
+ end
+
+ add_option(:"Install/Update", '-n', '--bindir DIR',
+ 'Directory where binary files are',
+ 'located') do |value, options|
+ options[:bin_dir] = File.expand_path(value)
+ end
+
+ add_option(:"Install/Update", '-d', '--[no-]rdoc',
+ 'Generate RDoc documentation for the gem on',
+ 'install') do |value, options|
+ options[:generate_rdoc] = value
+ end
+
+ add_option(:"Install/Update", '--[no-]ri',
+ 'Generate RI documentation for the gem on',
+ 'install') do |value, options|
+ options[:generate_ri] = value
+ end
+
+ add_option(:"Install/Update", '-E', '--[no-]env-shebang',
+ "Rewrite the shebang line on installed",
+ "scripts to use /usr/bin/env") do |value, options|
+ options[:env_shebang] = value
+ end
+
+ add_option(:"Install/Update", '-f', '--[no-]force',
+ 'Force gem to install, bypassing dependency',
+ 'checks') do |value, options|
+ options[:force] = value
+ end
+
+ add_option(:"Install/Update", '-t', '--[no-]test',
+ 'Run unit tests prior to installation') do |value, options|
+ options[:test] = value
+ end
+
+ add_option(:"Install/Update", '-w', '--[no-]wrappers',
+ 'Use bin wrappers for executables',
+ 'Not available on dosish platforms') do |value, options|
+ options[:wrappers] = value
+ end
+
+ add_option(:"Install/Update", '-P', '--trust-policy POLICY',
+ Gem::Security::Policy,
+ 'Specify gem trust policy') do |value, options|
+ options[:security_policy] = value
+ end
+
+ add_option(:"Install/Update", '--ignore-dependencies',
+ 'Do not install any required dependent gems') do |value, options|
+ options[:ignore_dependencies] = value
+ end
+
+ add_option(:"Install/Update", '-y', '--include-dependencies',
+ 'Unconditionally install the required',
+ 'dependent gems') do |value, options|
+ options[:include_dependencies] = value
+ end
+
+ add_option(:"Install/Update", '--[no-]format-executable',
+ 'Make installed executable names match ruby.',
+ 'If ruby is ruby18, foo_exec will be',
+ 'foo_exec18') do |value, options|
+ options[:format_executable] = value
+ end
+
+ add_option(:"Install/Update", '--[no-]user-install',
+ 'Install in user\'s home directory instead',
+ 'of GEM_HOME. Defaults to using home directory',
+ 'only if GEM_HOME is not writable.') do |value, options|
+ options[:user_install] = value
+ end
+
+ add_option(:"Install/Update", "--development",
+ "Install any additional development",
+ "dependencies") do |value, options|
+ options[:development] = true
+ end
+ end
+
+ # Default options for the gem install command.
+ def install_update_defaults_str
+ '--rdoc --no-force --no-test --wrappers'
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/installer.rb b/ruby/lib/rubygems/installer.rb
new file mode 100644
index 0000000..bb08549
--- /dev/null
+++ b/ruby/lib/rubygems/installer.rb
@@ -0,0 +1,575 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'fileutils'
+require 'pathname'
+require 'rbconfig'
+
+require 'rubygems/format'
+require 'rubygems/ext'
+require 'rubygems/require_paths_builder'
+
+##
+# The installer class processes RubyGem .gem files and installs the
+# files contained in the .gem into the Gem.path.
+#
+# Gem::Installer does the work of putting files in all the right places on the
+# filesystem including unpacking the gem into its gem dir, installing the
+# gemspec in the specifications dir, storing the cached gem in the cache dir,
+# and installing either wrappers or symlinks for executables.
+
+class Gem::Installer
+
+ ##
+ # Raised when there is an error while building extensions.
+ #
+ class ExtensionBuildError < Gem::InstallError; end
+
+ include Gem::UserInteraction
+
+ include Gem::RequirePathsBuilder
+
+ ##
+ # The directory a gem's executables will be installed into
+
+ attr_reader :bin_dir
+
+ ##
+ # The gem repository the gem will be installed into
+
+ attr_reader :gem_home
+
+ ##
+ # The Gem::Specification for the gem being installed
+
+ attr_reader :spec
+
+ @home_install_warning = false
+ @path_warning = false
+
+ class << self
+
+ ##
+ # True if we've warned about ~/.gems install
+
+ attr_accessor :home_install_warning
+
+ ##
+ # True if we've warned about PATH not including Gem.bindir
+
+ attr_accessor :path_warning
+
+ attr_writer :exec_format
+
+ # Defaults to use Ruby's program prefix and suffix.
+ def exec_format
+ @exec_format ||= Gem.default_exec_format
+ end
+
+ end
+
+ ##
+ # Constructs an Installer instance that will install the gem located at
+ # +gem+. +options+ is a Hash with the following keys:
+ #
+ # :env_shebang:: Use /usr/bin/env in bin wrappers.
+ # :force:: Overrides all version checks and security policy checks, except
+ # for a signed-gems-only policy.
+ # :ignore_dependencies:: Don't raise if a dependency is missing.
+ # :install_dir:: The directory to install the gem into.
+ # :format_executable:: Format the executable the same as the ruby executable.
+ # If your ruby is ruby18, foo_exec will be installed as
+ # foo_exec18.
+ # :security_policy:: Use the specified security policy. See Gem::Security
+ # :wrappers:: Install wrappers if true, symlinks if false.
+
+ def initialize(gem, options={})
+ @gem = gem
+
+ options = {
+ :bin_dir => nil,
+ :env_shebang => false,
+ :exec_format => false,
+ :force => false,
+ :install_dir => Gem.dir,
+ :source_index => Gem.source_index,
+ }.merge options
+
+ @env_shebang = options[:env_shebang]
+ @force = options[:force]
+ gem_home = options[:install_dir]
+ @gem_home = Pathname.new(gem_home).expand_path
+ @ignore_dependencies = options[:ignore_dependencies]
+ @format_executable = options[:format_executable]
+ @security_policy = options[:security_policy]
+ @wrappers = options[:wrappers]
+ @bin_dir = options[:bin_dir]
+ @development = options[:development]
+ @source_index = options[:source_index]
+
+ begin
+ @format = Gem::Format.from_file_by_path @gem, @security_policy
+ rescue Gem::Package::FormatError
+ raise Gem::InstallError, "invalid gem format for #{@gem}"
+ end
+
+ begin
+ FileUtils.mkdir_p @gem_home
+ rescue Errno::EACCES, Errno::ENOTDIR
+ # We'll divert to ~/.gem below
+ end
+
+ if not File.writable? @gem_home or
+ # TODO: Shouldn't have to test for existence of bindir; tests need it.
+ (@gem_home.to_s == Gem.dir and File.exist? Gem.bindir and
+ not File.writable? Gem.bindir) then
+ if options[:user_install] == false then # You don't want to use ~
+ raise Gem::FilePermissionError, @gem_home
+ elsif options[:user_install].nil? then
+ unless self.class.home_install_warning then
+ alert_warning "Installing to ~/.gem since #{@gem_home} and\n\t #{Gem.bindir} aren't both writable."
+ self.class.home_install_warning = true
+ end
+ end
+ options[:user_install] = true
+ end
+
+ if options[:user_install] and not options[:unpack] then
+ @gem_home = Gem.user_dir
+
+ user_bin_dir = File.join(@gem_home, 'bin')
+ unless ENV['PATH'].split(File::PATH_SEPARATOR).include? user_bin_dir then
+ unless self.class.path_warning then
+ alert_warning "You don't have #{user_bin_dir} in your PATH,\n\t gem executables will not run."
+ self.class.path_warning = true
+ end
+ end
+
+ FileUtils.mkdir_p @gem_home unless File.directory? @gem_home
+ # If it's still not writable, you've got issues.
+ raise Gem::FilePermissionError, @gem_home unless File.writable? @gem_home
+ end
+
+ @spec = @format.spec
+
+ @gem_dir = File.join(@gem_home, "gems", @spec.full_name).untaint
+ end
+
+ ##
+ # Installs the gem and returns a loaded Gem::Specification for the installed
+ # gem.
+ #
+ # The gem will be installed with the following structure:
+ #
+ # @gem_home/
+ # cache/<gem-version>.gem #=> a cached copy of the installed gem
+ # gems/<gem-version>/... #=> extracted files
+ # specifications/<gem-version>.gemspec #=> the Gem::Specification
+
+ def install
+ # If we're forcing the install then disable security unless the security
+ # policy says that we only install singed gems.
+ @security_policy = nil if @force and @security_policy and
+ not @security_policy.only_signed
+
+ unless @force then
+ if rrv = @spec.required_ruby_version then
+ unless rrv.satisfied_by? Gem.ruby_version then
+ raise Gem::InstallError, "#{@spec.name} requires Ruby version #{rrv}"
+ end
+ end
+
+ if rrgv = @spec.required_rubygems_version then
+ unless rrgv.satisfied_by? Gem::Version.new(Gem::RubyGemsVersion) then
+ raise Gem::InstallError,
+ "#{@spec.name} requires RubyGems version #{rrgv}"
+ end
+ end
+
+ unless @ignore_dependencies then
+ deps = @spec.runtime_dependencies
+ deps |= @spec.development_dependencies if @development
+
+ deps.each do |dep_gem|
+ ensure_dependency @spec, dep_gem
+ end
+ end
+ end
+
+ Gem.pre_install_hooks.each do |hook|
+ hook.call self
+ end
+
+ FileUtils.mkdir_p @gem_home unless File.directory? @gem_home
+
+ Gem.ensure_gem_subdirectories @gem_home
+
+ FileUtils.mkdir_p @gem_dir
+
+ extract_files
+ generate_bin
+ build_extensions
+ write_spec
+
+ write_require_paths_file_if_needed
+
+ # HACK remove? Isn't this done in multiple places?
+ cached_gem = File.join @gem_home, "cache", @gem.split(/\//).pop
+ unless File.exist? cached_gem then
+ FileUtils.cp @gem, File.join(@gem_home, "cache")
+ end
+
+ say @spec.post_install_message unless @spec.post_install_message.nil?
+
+ @spec.loaded_from = File.join(@gem_home, 'specifications',
+ "#{@spec.full_name}.gemspec")
+
+ @source_index.add_spec @spec
+
+ Gem.post_install_hooks.each do |hook|
+ hook.call self
+ end
+
+ return @spec
+ rescue Zlib::GzipFile::Error
+ raise Gem::InstallError, "gzip error installing #{@gem}"
+ end
+
+ ##
+ # Ensure that the dependency is satisfied by the current installation of
+ # gem. If it is not an exception is raised.
+ #
+ # spec :: Gem::Specification
+ # dependency :: Gem::Dependency
+
+ def ensure_dependency(spec, dependency)
+ unless installation_satisfies_dependency? dependency then
+ raise Gem::InstallError, "#{spec.name} requires #{dependency}"
+ end
+
+ true
+ end
+
+ ##
+ # True if the gems in the source_index satisfy +dependency+.
+
+ def installation_satisfies_dependency?(dependency)
+ @source_index.find_name(dependency.name, dependency.version_requirements).size > 0
+ end
+
+ ##
+ # Unpacks the gem into the given directory.
+
+ def unpack(directory)
+ @gem_dir = directory
+ @format = Gem::Format.from_file_by_path @gem, @security_policy
+ extract_files
+ end
+
+ ##
+ # Writes the .gemspec specification (in Ruby) to the gem home's
+ # specifications directory.
+
+ def write_spec
+ rubycode = @spec.to_ruby
+
+ file_name = File.join @gem_home, 'specifications',
+ "#{@spec.full_name}.gemspec"
+
+ file_name.untaint
+
+ File.open(file_name, "w") do |file|
+ file.puts rubycode
+ end
+ end
+
+ ##
+ # Creates windows .bat files for easy running of commands
+
+ def generate_windows_script(bindir, filename)
+ if Gem.win_platform? then
+ script_name = filename + ".bat"
+ script_path = File.join bindir, File.basename(script_name)
+ File.open script_path, 'w' do |file|
+ file.puts windows_stub_script(bindir, filename)
+ end
+
+ say script_path if Gem.configuration.really_verbose
+ end
+ end
+
+ def generate_bin
+ return if @spec.executables.nil? or @spec.executables.empty?
+
+ # If the user has asked for the gem to be installed in a directory that is
+ # the system gem directory, then use the system bin directory, else create
+ # (or use) a new bin dir under the gem_home.
+ bindir = @bin_dir ? @bin_dir : Gem.bindir(@gem_home)
+
+ Dir.mkdir bindir unless File.exist? bindir
+ raise Gem::FilePermissionError.new(bindir) unless File.writable? bindir
+
+ @spec.executables.each do |filename|
+ filename.untaint
+ bin_path = File.expand_path File.join(@gem_dir, @spec.bindir, filename)
+ mode = File.stat(bin_path).mode | 0111
+ File.chmod mode, bin_path
+
+ if @wrappers then
+ generate_bin_script filename, bindir
+ else
+ generate_bin_symlink filename, bindir
+ end
+ end
+ end
+
+ ##
+ # Creates the scripts to run the applications in the gem.
+ #--
+ # The Windows script is generated in addition to the regular one due to a
+ # bug or misfeature in the Windows shell's pipe. See
+ # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/193379
+
+ def generate_bin_script(filename, bindir)
+ bin_script_path = File.join bindir, formatted_program_filename(filename)
+
+ exec_path = File.join @gem_dir, @spec.bindir, filename
+
+ # HACK some gems don't have #! in their executables, restore 2008/06
+ #if File.read(exec_path, 2) == '#!' then
+ FileUtils.rm_f bin_script_path # prior install may have been --no-wrappers
+
+ File.open bin_script_path, 'w', 0755 do |file|
+ file.print app_script_text(filename)
+ end
+
+ say bin_script_path if Gem.configuration.really_verbose
+
+ generate_windows_script bindir, filename
+ #else
+ # FileUtils.rm_f bin_script_path
+ # FileUtils.cp exec_path, bin_script_path,
+ # :verbose => Gem.configuration.really_verbose
+ #end
+ end
+
+ ##
+ # Creates the symlinks to run the applications in the gem. Moves
+ # the symlink if the gem being installed has a newer version.
+
+ def generate_bin_symlink(filename, bindir)
+ if Gem.win_platform? then
+ alert_warning "Unable to use symlinks on Windows, installing wrapper"
+ generate_bin_script filename, bindir
+ return
+ end
+
+ src = File.join @gem_dir, 'bin', filename
+ dst = File.join bindir, formatted_program_filename(filename)
+
+ if File.exist? dst then
+ if File.symlink? dst then
+ link = File.readlink(dst).split File::SEPARATOR
+ cur_version = Gem::Version.create(link[-3].sub(/^.*-/, ''))
+ return if @spec.version < cur_version
+ end
+ File.unlink dst
+ end
+
+ FileUtils.symlink src, dst, :verbose => Gem.configuration.really_verbose
+ end
+
+ ##
+ # Generates a #! line for +bin_file_name+'s wrapper copying arguments if
+ # necessary.
+
+ def shebang(bin_file_name)
+ if @env_shebang then
+ "#!/usr/bin/env " + Gem::ConfigMap[:ruby_install_name]
+ else
+ path = File.join @gem_dir, @spec.bindir, bin_file_name
+
+ File.open(path, "rb") do |file|
+ first_line = file.gets
+ if first_line =~ /^#!/ then
+ # Preserve extra words on shebang line, like "-w". Thanks RPA.
+ shebang = first_line.sub(/\A\#!.*?ruby\S*/, "#!#{Gem.ruby}")
+ else
+ # Create a plain shebang line.
+ shebang = "#!#{Gem.ruby}"
+ end
+
+ shebang.strip # Avoid nasty ^M issues.
+ end
+ end
+ end
+
+ ##
+ # Return the text for an application file.
+
+ def app_script_text(bin_file_name)
+ <<-TEXT
+#{shebang bin_file_name}
+#
+# This file was generated by RubyGems.
+#
+# The application '#{@spec.name}' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
+
+require 'rubygems'
+
+version = "#{Gem::Requirement.default}"
+
+if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
+ version = $1
+ ARGV.shift
+end
+
+gem '#{@spec.name}', version
+load '#{bin_file_name}'
+TEXT
+ end
+
+ ##
+ # return the stub script text used to launch the true ruby script
+
+ def windows_stub_script(bindir, bin_file_name)
+ <<-TEXT
+@ECHO OFF
+IF NOT "%~f0" == "~f0" GOTO :WinNT
+@"#{File.basename(Gem.ruby)}" "#{File.join(bindir, bin_file_name)}" %1 %2 %3 %4 %5 %6 %7 %8 %9
+GOTO :EOF
+:WinNT
+@"#{File.basename(Gem.ruby)}" "%~dpn0" %*
+TEXT
+ end
+
+ ##
+ # Builds extensions. Valid types of extensions are extconf.rb files,
+ # configure scripts and rakefiles or mkrf_conf files.
+
+ def build_extensions
+ return if @spec.extensions.empty?
+ say "Building native extensions. This could take a while..."
+ start_dir = Dir.pwd
+ dest_path = File.join @gem_dir, @spec.require_paths.first
+ ran_rake = false # only run rake once
+
+ @spec.extensions.each do |extension|
+ break if ran_rake
+ results = []
+
+ builder = case extension
+ when /extconf/ then
+ Gem::Ext::ExtConfBuilder
+ when /configure/ then
+ Gem::Ext::ConfigureBuilder
+ when /rakefile/i, /mkrf_conf/i then
+ ran_rake = true
+ Gem::Ext::RakeBuilder
+ else
+ results = ["No builder for extension '#{extension}'"]
+ nil
+ end
+
+ begin
+ Dir.chdir File.join(@gem_dir, File.dirname(extension))
+ results = builder.build(extension, @gem_dir, dest_path, results)
+
+ say results.join("\n") if Gem.configuration.really_verbose
+
+ rescue => ex
+ results = results.join "\n"
+
+ File.open('gem_make.out', 'wb') { |f| f.puts results }
+
+ message = <<-EOF
+ERROR: Failed to build gem native extension.
+
+#{results}
+
+Gem files will remain installed in #{@gem_dir} for inspection.
+Results logged to #{File.join(Dir.pwd, 'gem_make.out')}
+ EOF
+
+ raise ExtensionBuildError, message
+ ensure
+ Dir.chdir start_dir
+ end
+ end
+ end
+
+ ##
+ # Reads the file index and extracts each file into the gem directory.
+ #
+ # Ensures that files can't be installed outside the gem directory.
+
+ def extract_files
+ expand_and_validate_gem_dir
+
+ raise ArgumentError, "format required to extract from" if @format.nil?
+
+ @format.file_entries.each do |entry, file_data|
+ path = entry['path'].untaint
+
+ if path =~ /\A\// then # for extra sanity
+ raise Gem::InstallError,
+ "attempt to install file into #{entry['path'].inspect}"
+ end
+
+ path = File.expand_path File.join(@gem_dir, path)
+
+ if path !~ /\A#{Regexp.escape @gem_dir}/ then
+ msg = "attempt to install file into %p under %p" %
+ [entry['path'], @gem_dir]
+ raise Gem::InstallError, msg
+ end
+
+ FileUtils.mkdir_p File.dirname(path)
+
+ File.open(path, "wb") do |out|
+ out.write file_data
+ end
+
+ FileUtils.chmod entry['mode'], path
+
+ say path if Gem.configuration.really_verbose
+ end
+ end
+
+ ##
+ # Prefix and suffix the program filename the same as ruby.
+
+ def formatted_program_filename(filename)
+ if @format_executable then
+ self.class.exec_format % File.basename(filename)
+ else
+ filename
+ end
+ end
+
+ private
+
+ ##
+ # HACK Pathname is broken on windows.
+
+ def absolute_path? pathname
+ pathname.absolute? or (Gem.win_platform? and pathname.to_s =~ /\A[a-z]:/i)
+ end
+
+ def expand_and_validate_gem_dir
+ @gem_dir = Pathname.new(@gem_dir).expand_path
+
+ unless absolute_path?(@gem_dir) then # HACK is this possible after #expand_path?
+ raise ArgumentError, "install directory %p not absolute" % @gem_dir
+ end
+
+ @gem_dir = @gem_dir.to_s
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/local_remote_options.rb b/ruby/lib/rubygems/local_remote_options.rb
new file mode 100644
index 0000000..730cb69
--- /dev/null
+++ b/ruby/lib/rubygems/local_remote_options.rb
@@ -0,0 +1,134 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'uri'
+require 'rubygems'
+
+##
+# Mixin methods for local and remote Gem::Command options.
+
+module Gem::LocalRemoteOptions
+
+ ##
+ # Allows OptionParser to handle HTTP URIs.
+
+ def accept_uri_http
+ OptionParser.accept URI::HTTP do |value|
+ begin
+ uri = URI.parse value
+ rescue URI::InvalidURIError
+ raise OptionParser::InvalidArgument, value
+ end
+
+ raise OptionParser::InvalidArgument, value unless uri.scheme == 'http'
+
+ value
+ end
+ end
+
+ ##
+ # Add local/remote options to the command line parser.
+
+ def add_local_remote_options
+ add_option(:"Local/Remote", '-l', '--local',
+ 'Restrict operations to the LOCAL domain') do |value, options|
+ options[:domain] = :local
+ end
+
+ add_option(:"Local/Remote", '-r', '--remote',
+ 'Restrict operations to the REMOTE domain') do |value, options|
+ options[:domain] = :remote
+ end
+
+ add_option(:"Local/Remote", '-b', '--both',
+ 'Allow LOCAL and REMOTE operations') do |value, options|
+ options[:domain] = :both
+ end
+
+ add_bulk_threshold_option
+ add_source_option
+ add_proxy_option
+ add_update_sources_option
+ end
+
+ ##
+ # Add the --bulk-threshold option
+
+ def add_bulk_threshold_option
+ add_option(:"Local/Remote", '-B', '--bulk-threshold COUNT',
+ "Threshold for switching to bulk",
+ "synchronization (default #{Gem.configuration.bulk_threshold})") do
+ |value, options|
+ Gem.configuration.bulk_threshold = value.to_i
+ end
+ end
+
+ ##
+ # Add the --http-proxy option
+
+ def add_proxy_option
+ accept_uri_http
+
+ add_option(:"Local/Remote", '-p', '--[no-]http-proxy [URL]', URI::HTTP,
+ 'Use HTTP proxy for remote operations') do |value, options|
+ options[:http_proxy] = (value == false) ? :no_proxy : value
+ Gem.configuration[:http_proxy] = options[:http_proxy]
+ end
+ end
+
+ ##
+ # Add the --source option
+
+ def add_source_option
+ accept_uri_http
+
+ add_option(:"Local/Remote", '--source URL', URI::HTTP,
+ 'Use URL as the remote source for gems') do |source, options|
+ source << '/' if source !~ /\/\z/
+
+ if options[:added_source] then
+ Gem.sources << source
+ else
+ options[:added_source] = true
+ Gem.sources.replace [source]
+ end
+ end
+ end
+
+ ##
+ # Add the --update-source option
+
+ def add_update_sources_option
+
+ add_option(:"Local/Remote", '-u', '--[no-]update-sources',
+ 'Update local source cache') do |value, options|
+ Gem.configuration.update_sources = value
+ end
+ end
+
+ ##
+ # Is fetching of local and remote information enabled?
+
+ def both?
+ options[:domain] == :both
+ end
+
+ ##
+ # Is local fetching enabled?
+
+ def local?
+ options[:domain] == :local || options[:domain] == :both
+ end
+
+ ##
+ # Is remote fetching enabled?
+
+ def remote?
+ options[:domain] == :remote || options[:domain] == :both
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/old_format.rb b/ruby/lib/rubygems/old_format.rb
new file mode 100644
index 0000000..ef5d621
--- /dev/null
+++ b/ruby/lib/rubygems/old_format.rb
@@ -0,0 +1,148 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'fileutils'
+require 'yaml'
+require 'zlib'
+
+module Gem
+
+ ##
+ # The format class knows the guts of the RubyGem .gem file format
+ # and provides the capability to read gem files
+ #
+ class OldFormat
+ attr_accessor :spec, :file_entries, :gem_path
+
+ ##
+ # Constructs an instance of a Format object, representing the gem's
+ # data structure.
+ #
+ # gem:: [String] The file name of the gem
+ #
+ def initialize(gem_path)
+ @gem_path = gem_path
+ end
+
+ ##
+ # Reads the named gem file and returns a Format object, representing
+ # the data from the gem file
+ #
+ # file_path:: [String] Path to the gem file
+ #
+ def self.from_file_by_path(file_path)
+ unless File.exist?(file_path)
+ raise Gem::Exception, "Cannot load gem file [#{file_path}]"
+ end
+ File.open(file_path, 'rb') do |file|
+ from_io(file, file_path)
+ end
+ end
+
+ ##
+ # Reads a gem from an io stream and returns a Format object, representing
+ # the data from the gem file
+ #
+ # io:: [IO] Stream from which to read the gem
+ #
+ def self.from_io(io, gem_path="(io)")
+ format = self.new(gem_path)
+ skip_ruby(io)
+ format.spec = read_spec(io)
+ format.file_entries = []
+ read_files_from_gem(io) do |entry, file_data|
+ format.file_entries << [entry, file_data]
+ end
+ format
+ end
+
+ private
+ ##
+ # Skips the Ruby self-install header. After calling this method, the
+ # IO index will be set after the Ruby code.
+ #
+ # file:: [IO] The IO to process (skip the Ruby code)
+ #
+ def self.skip_ruby(file)
+ end_seen = false
+ loop {
+ line = file.gets
+ if(line == nil || line.chomp == "__END__") then
+ end_seen = true
+ break
+ end
+ }
+ if(end_seen == false) then
+ raise Gem::Exception.new("Failed to find end of ruby script while reading gem")
+ end
+ end
+
+ ##
+ # Reads the specification YAML from the supplied IO and constructs
+ # a Gem::Specification from it. After calling this method, the
+ # IO index will be set after the specification header.
+ #
+ # file:: [IO] The IO to process
+ #
+ def self.read_spec(file)
+ yaml = ''
+ begin
+ read_until_dashes(file) do |line|
+ yaml << line
+ end
+ Specification.from_yaml(yaml)
+ rescue YAML::Error => e
+ raise Gem::Exception.new("Failed to parse gem specification out of gem file")
+ rescue ArgumentError => e
+ raise Gem::Exception.new("Failed to parse gem specification out of gem file")
+ end
+ end
+
+ ##
+ # Reads lines from the supplied IO until a end-of-yaml (---) is
+ # reached
+ #
+ # file:: [IO] The IO to process
+ # block:: [String] The read line
+ #
+ def self.read_until_dashes(file)
+ while((line = file.gets) && line.chomp.strip != "---") do
+ yield line
+ end
+ end
+
+
+ ##
+ # Reads the embedded file data from a gem file, yielding an entry
+ # containing metadata about the file and the file contents themselves
+ # for each file that's archived in the gem.
+ # NOTE: Many of these methods should be extracted into some kind of
+ # Gem file read/writer
+ #
+ # gem_file:: [IO] The IO to process
+ #
+ def self.read_files_from_gem(gem_file)
+ errstr = "Error reading files from gem"
+ header_yaml = ''
+ begin
+ self.read_until_dashes(gem_file) do |line|
+ header_yaml << line
+ end
+ header = YAML.load(header_yaml)
+ raise Gem::Exception.new(errstr) unless header
+ header.each do |entry|
+ file_data = ''
+ self.read_until_dashes(gem_file) do |line|
+ file_data << line
+ end
+ yield [entry, Zlib::Inflate.inflate(file_data.strip.unpack("m")[0])]
+ end
+ rescue Exception,Zlib::DataError => e
+ raise Gem::Exception.new(errstr)
+ end
+ end
+ end
+end
diff --git a/ruby/lib/rubygems/package.rb b/ruby/lib/rubygems/package.rb
new file mode 100644
index 0000000..9cb393b
--- /dev/null
+++ b/ruby/lib/rubygems/package.rb
@@ -0,0 +1,95 @@
+#++
+# Copyright (C) 2004 Mauricio Julio Fernández Pradier
+# See LICENSE.txt for additional licensing information.
+#--
+
+require 'fileutils'
+require 'find'
+require 'stringio'
+require 'yaml'
+require 'zlib'
+
+require 'rubygems/digest/md5'
+require 'rubygems/security'
+require 'rubygems/specification'
+
+# Wrapper for FileUtils meant to provide logging and additional operations if
+# needed.
+class Gem::FileOperations
+
+ def initialize(logger = nil)
+ @logger = logger
+ end
+
+ def method_missing(meth, *args, &block)
+ case
+ when FileUtils.respond_to?(meth)
+ @logger.log "#{meth}: #{args}" if @logger
+ FileUtils.send meth, *args, &block
+ when Gem::FileOperations.respond_to?(meth)
+ @logger.log "#{meth}: #{args}" if @logger
+ Gem::FileOperations.send meth, *args, &block
+ else
+ super
+ end
+ end
+
+end
+
+module Gem::Package
+
+ class Error < StandardError; end
+ class NonSeekableIO < Error; end
+ class ClosedIO < Error; end
+ class BadCheckSum < Error; end
+ class TooLongFileName < Error; end
+ class FormatError < Error; end
+
+ def self.open(io, mode = "r", signer = nil, &block)
+ tar_type = case mode
+ when 'r' then TarInput
+ when 'w' then TarOutput
+ else
+ raise "Unknown Package open mode"
+ end
+
+ tar_type.open(io, signer, &block)
+ end
+
+ def self.pack(src, destname, signer = nil)
+ TarOutput.open(destname, signer) do |outp|
+ dir_class.chdir(src) do
+ outp.metadata = (file_class.read("RPA/metadata") rescue nil)
+ find_class.find('.') do |entry|
+ case
+ when file_class.file?(entry)
+ entry.sub!(%r{\./}, "")
+ next if entry =~ /\ARPA\//
+ stat = File.stat(entry)
+ outp.add_file_simple(entry, stat.mode, stat.size) do |os|
+ file_class.open(entry, "rb") do |f|
+ os.write(f.read(4096)) until f.eof?
+ end
+ end
+ when file_class.dir?(entry)
+ entry.sub!(%r{\./}, "")
+ next if entry == "RPA"
+ outp.mkdir(entry, file_class.stat(entry).mode)
+ else
+ raise "Don't know how to pack this yet!"
+ end
+ end
+ end
+ end
+ end
+
+end
+
+require 'rubygems/package/f_sync_dir'
+require 'rubygems/package/tar_header'
+require 'rubygems/package/tar_input'
+require 'rubygems/package/tar_output'
+require 'rubygems/package/tar_reader'
+require 'rubygems/package/tar_reader/entry'
+require 'rubygems/package/tar_writer'
+
diff --git a/ruby/lib/rubygems/package/f_sync_dir.rb b/ruby/lib/rubygems/package/f_sync_dir.rb
new file mode 100644
index 0000000..3e2e4a5
--- /dev/null
+++ b/ruby/lib/rubygems/package/f_sync_dir.rb
@@ -0,0 +1,24 @@
+#++
+# Copyright (C) 2004 Mauricio Julio Fernández Pradier
+# See LICENSE.txt for additional licensing information.
+#--
+
+require 'rubygems/package'
+
+module Gem::Package::FSyncDir
+
+ private
+
+ ##
+ # make sure this hits the disc
+
+ def fsync_dir(dirname)
+ dir = open dirname, 'r'
+ dir.fsync
+ rescue # ignore IOError if it's an unpatched (old) Ruby
+ ensure
+ dir.close if dir rescue nil
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/package/tar_header.rb b/ruby/lib/rubygems/package/tar_header.rb
new file mode 100644
index 0000000..c194cc0
--- /dev/null
+++ b/ruby/lib/rubygems/package/tar_header.rb
@@ -0,0 +1,245 @@
+#++
+# Copyright (C) 2004 Mauricio Julio Fernández Pradier
+# See LICENSE.txt for additional licensing information.
+#--
+
+require 'rubygems/package'
+
+##
+#--
+# struct tarfile_entry_posix {
+# char name[100]; # ASCII + (Z unless filled)
+# char mode[8]; # 0 padded, octal, null
+# char uid[8]; # ditto
+# char gid[8]; # ditto
+# char size[12]; # 0 padded, octal, null
+# char mtime[12]; # 0 padded, octal, null
+# char checksum[8]; # 0 padded, octal, null, space
+# char typeflag[1]; # file: "0" dir: "5"
+# char linkname[100]; # ASCII + (Z unless filled)
+# char magic[6]; # "ustar\0"
+# char version[2]; # "00"
+# char uname[32]; # ASCIIZ
+# char gname[32]; # ASCIIZ
+# char devmajor[8]; # 0 padded, octal, null
+# char devminor[8]; # o padded, octal, null
+# char prefix[155]; # ASCII + (Z unless filled)
+# };
+#++
+
+class Gem::Package::TarHeader
+
+ FIELDS = [
+ :checksum,
+ :devmajor,
+ :devminor,
+ :gid,
+ :gname,
+ :linkname,
+ :magic,
+ :mode,
+ :mtime,
+ :name,
+ :prefix,
+ :size,
+ :typeflag,
+ :uid,
+ :uname,
+ :version,
+ ]
+
+ PACK_FORMAT = 'a100' + # name
+ 'a8' + # mode
+ 'a8' + # uid
+ 'a8' + # gid
+ 'a12' + # size
+ 'a12' + # mtime
+ 'a7a' + # chksum
+ 'a' + # typeflag
+ 'a100' + # linkname
+ 'a6' + # magic
+ 'a2' + # version
+ 'a32' + # uname
+ 'a32' + # gname
+ 'a8' + # devmajor
+ 'a8' + # devminor
+ 'a155' # prefix
+
+ UNPACK_FORMAT = 'A100' + # name
+ 'A8' + # mode
+ 'A8' + # uid
+ 'A8' + # gid
+ 'A12' + # size
+ 'A12' + # mtime
+ 'A8' + # checksum
+ 'A' + # typeflag
+ 'A100' + # linkname
+ 'A6' + # magic
+ 'A2' + # version
+ 'A32' + # uname
+ 'A32' + # gname
+ 'A8' + # devmajor
+ 'A8' + # devminor
+ 'A155' # prefix
+
+ attr_reader(*FIELDS)
+
+ def self.from(stream)
+ header = stream.read 512
+ empty = (header == "\0" * 512)
+
+ fields = header.unpack UNPACK_FORMAT
+
+ name = fields.shift
+ mode = fields.shift.oct
+ uid = fields.shift.oct
+ gid = fields.shift.oct
+ size = fields.shift.oct
+ mtime = fields.shift.oct
+ checksum = fields.shift.oct
+ typeflag = fields.shift
+ linkname = fields.shift
+ magic = fields.shift
+ version = fields.shift.oct
+ uname = fields.shift
+ gname = fields.shift
+ devmajor = fields.shift.oct
+ devminor = fields.shift.oct
+ prefix = fields.shift
+
+ new :name => name,
+ :mode => mode,
+ :uid => uid,
+ :gid => gid,
+ :size => size,
+ :mtime => mtime,
+ :checksum => checksum,
+ :typeflag => typeflag,
+ :linkname => linkname,
+ :magic => magic,
+ :version => version,
+ :uname => uname,
+ :gname => gname,
+ :devmajor => devmajor,
+ :devminor => devminor,
+ :prefix => prefix,
+
+ :empty => empty
+
+ # HACK unfactor for Rubinius
+ #new :name => fields.shift,
+ # :mode => fields.shift.oct,
+ # :uid => fields.shift.oct,
+ # :gid => fields.shift.oct,
+ # :size => fields.shift.oct,
+ # :mtime => fields.shift.oct,
+ # :checksum => fields.shift.oct,
+ # :typeflag => fields.shift,
+ # :linkname => fields.shift,
+ # :magic => fields.shift,
+ # :version => fields.shift.oct,
+ # :uname => fields.shift,
+ # :gname => fields.shift,
+ # :devmajor => fields.shift.oct,
+ # :devminor => fields.shift.oct,
+ # :prefix => fields.shift,
+
+ # :empty => empty
+ end
+
+ def initialize(vals)
+ unless vals[:name] && vals[:size] && vals[:prefix] && vals[:mode] then
+ raise ArgumentError, ":name, :size, :prefix and :mode required"
+ end
+
+ vals[:uid] ||= 0
+ vals[:gid] ||= 0
+ vals[:mtime] ||= 0
+ vals[:checksum] ||= ""
+ vals[:typeflag] ||= "0"
+ vals[:magic] ||= "ustar"
+ vals[:version] ||= "00"
+ vals[:uname] ||= "wheel"
+ vals[:gname] ||= "wheel"
+ vals[:devmajor] ||= 0
+ vals[:devminor] ||= 0
+
+ FIELDS.each do |name|
+ instance_variable_set "@#{name}", vals[name]
+ end
+
+ @empty = vals[:empty]
+ end
+
+ def empty?
+ @empty
+ end
+
+ def ==(other)
+ self.class === other and
+ @checksum == other.checksum and
+ @devmajor == other.devmajor and
+ @devminor == other.devminor and
+ @gid == other.gid and
+ @gname == other.gname and
+ @linkname == other.linkname and
+ @magic == other.magic and
+ @mode == other.mode and
+ @mtime == other.mtime and
+ @name == other.name and
+ @prefix == other.prefix and
+ @size == other.size and
+ @typeflag == other.typeflag and
+ @uid == other.uid and
+ @uname == other.uname and
+ @version == other.version
+ end
+
+ def to_s
+ update_checksum
+ header
+ end
+
+ def update_checksum
+ header = header " " * 8
+ @checksum = oct calculate_checksum(header), 6
+ end
+
+ private
+
+ def calculate_checksum(header)
+ header.unpack("C*").inject { |a, b| a + b }
+ end
+
+ def header(checksum = @checksum)
+ header = [
+ name,
+ oct(mode, 7),
+ oct(uid, 7),
+ oct(gid, 7),
+ oct(size, 11),
+ oct(mtime, 11),
+ checksum,
+ " ",
+ typeflag,
+ linkname,
+ magic,
+ oct(version, 2),
+ uname,
+ gname,
+ oct(devmajor, 7),
+ oct(devminor, 7),
+ prefix
+ ]
+
+ header = header.pack PACK_FORMAT
+
+ header << ("\0" * ((512 - header.size) % 512))
+ end
+
+ def oct(num, len)
+ "%0#{len}o" % num
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/package/tar_input.rb b/ruby/lib/rubygems/package/tar_input.rb
new file mode 100644
index 0000000..2ed3d6b
--- /dev/null
+++ b/ruby/lib/rubygems/package/tar_input.rb
@@ -0,0 +1,219 @@
+#++
+# Copyright (C) 2004 Mauricio Julio Fernández Pradier
+# See LICENSE.txt for additional licensing information.
+#--
+
+require 'rubygems/package'
+
+class Gem::Package::TarInput
+
+ include Gem::Package::FSyncDir
+ include Enumerable
+
+ attr_reader :metadata
+
+ private_class_method :new
+
+ def self.open(io, security_policy = nil, &block)
+ is = new io, security_policy
+
+ yield is
+ ensure
+ is.close if is
+ end
+
+ def initialize(io, security_policy = nil)
+ @io = io
+ @tarreader = Gem::Package::TarReader.new @io
+ has_meta = false
+
+ data_sig, meta_sig, data_dgst, meta_dgst = nil, nil, nil, nil
+ dgst_algo = security_policy ? Gem::Security::OPT[:dgst_algo] : nil
+
+ @tarreader.each do |entry|
+ case entry.full_name
+ when "metadata"
+ @metadata = load_gemspec entry.read
+ has_meta = true
+ when "metadata.gz"
+ begin
+ # if we have a security_policy, then pre-read the metadata file
+ # and calculate it's digest
+ sio = nil
+ if security_policy
+ Gem.ensure_ssl_available
+ sio = StringIO.new(entry.read)
+ meta_dgst = dgst_algo.digest(sio.string)
+ sio.rewind
+ end
+
+ gzis = Zlib::GzipReader.new(sio || entry)
+ # YAML wants an instance of IO
+ @metadata = load_gemspec(gzis)
+ has_meta = true
+ ensure
+ gzis.close unless gzis.nil?
+ end
+ when 'metadata.gz.sig'
+ meta_sig = entry.read
+ when 'data.tar.gz.sig'
+ data_sig = entry.read
+ when 'data.tar.gz'
+ if security_policy
+ Gem.ensure_ssl_available
+ data_dgst = dgst_algo.digest(entry.read)
+ end
+ end
+ end
+
+ if security_policy then
+ Gem.ensure_ssl_available
+
+ # map trust policy from string to actual class (or a serialized YAML
+ # file, if that exists)
+ if String === security_policy then
+ if Gem::Security::Policy.key? security_policy then
+ # load one of the pre-defined security policies
+ security_policy = Gem::Security::Policy[security_policy]
+ elsif File.exist? security_policy then
+ # FIXME: this doesn't work yet
+ security_policy = YAML.load File.read(security_policy)
+ else
+ raise Gem::Exception, "Unknown trust policy '#{security_policy}'"
+ end
+ end
+
+ if data_sig && data_dgst && meta_sig && meta_dgst then
+ # the user has a trust policy, and we have a signed gem
+ # file, so use the trust policy to verify the gem signature
+
+ begin
+ security_policy.verify_gem(data_sig, data_dgst, @metadata.cert_chain)
+ rescue Exception => e
+ raise "Couldn't verify data signature: #{e}"
+ end
+
+ begin
+ security_policy.verify_gem(meta_sig, meta_dgst, @metadata.cert_chain)
+ rescue Exception => e
+ raise "Couldn't verify metadata signature: #{e}"
+ end
+ elsif security_policy.only_signed
+ raise Gem::Exception, "Unsigned gem"
+ else
+ # FIXME: should display warning here (trust policy, but
+ # either unsigned or badly signed gem file)
+ end
+ end
+
+ @tarreader.rewind
+ @fileops = Gem::FileOperations.new
+
+ raise Gem::Package::FormatError, "No metadata found!" unless has_meta
+ end
+
+ def close
+ @io.close
+ @tarreader.close
+ end
+
+ def each(&block)
+ @tarreader.each do |entry|
+ next unless entry.full_name == "data.tar.gz"
+ is = zipped_stream entry
+
+ begin
+ Gem::Package::TarReader.new is do |inner|
+ inner.each(&block)
+ end
+ ensure
+ is.close if is
+ end
+ end
+
+ @tarreader.rewind
+ end
+
+ def extract_entry(destdir, entry, expected_md5sum = nil)
+ if entry.directory? then
+ dest = File.join(destdir, entry.full_name)
+
+ if File.dir? dest then
+ @fileops.chmod entry.header.mode, dest, :verbose=>false
+ else
+ @fileops.mkdir_p dest, :mode => entry.header.mode, :verbose => false
+ end
+
+ fsync_dir dest
+ fsync_dir File.join(dest, "..")
+
+ return
+ end
+
+ # it's a file
+ md5 = Digest::MD5.new if expected_md5sum
+ destdir = File.join destdir, File.dirname(entry.full_name)
+ @fileops.mkdir_p destdir, :mode => 0755, :verbose => false
+ destfile = File.join destdir, File.basename(entry.full_name)
+ @fileops.chmod 0600, destfile, :verbose => false rescue nil # Errno::ENOENT
+
+ open destfile, "wb", entry.header.mode do |os|
+ loop do
+ data = entry.read 4096
+ break unless data
+ # HACK shouldn't we check the MD5 before writing to disk?
+ md5 << data if expected_md5sum
+ os.write(data)
+ end
+
+ os.fsync
+ end
+
+ @fileops.chmod entry.header.mode, destfile, :verbose => false
+ fsync_dir File.dirname(destfile)
+ fsync_dir File.join(File.dirname(destfile), "..")
+
+ if expected_md5sum && expected_md5sum != md5.hexdigest then
+ raise Gem::Package::BadCheckSum
+ end
+ end
+
+ # Attempt to YAML-load a gemspec from the given _io_ parameter. Return
+ # nil if it fails.
+ def load_gemspec(io)
+ Gem::Specification.from_yaml io
+ rescue Gem::Exception
+ nil
+ end
+
+ ##
+ # Return an IO stream for the zipped entry.
+ #
+ # NOTE: Originally this method used two approaches, Return a GZipReader
+ # directly, or read the GZipReader into a string and return a StringIO on
+ # the string. The string IO approach was used for versions of ZLib before
+ # 1.2.1 to avoid buffer errors on windows machines. Then we found that
+ # errors happened with 1.2.1 as well, so we changed the condition. Then
+ # we discovered errors occurred with versions as late as 1.2.3. At this
+ # point (after some benchmarking to show we weren't seriously crippling
+ # the unpacking speed) we threw our hands in the air and declared that
+ # this method would use the String IO approach on all platforms at all
+ # times. And that's the way it is.
+
+ def zipped_stream(entry)
+ if defined? Rubinius then
+ zis = Zlib::GzipReader.new entry
+ dis = zis.read
+ is = StringIO.new(dis)
+ else
+ # This is Jamis Buck's Zlib workaround for some unknown issue
+ entry.read(10) # skip the gzip header
+ zis = Zlib::Inflate.new(-Zlib::MAX_WBITS)
+ is = StringIO.new(zis.inflate(entry.read))
+ end
+ ensure
+ zis.finish if zis
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/package/tar_output.rb b/ruby/lib/rubygems/package/tar_output.rb
new file mode 100644
index 0000000..b22f7dd
--- /dev/null
+++ b/ruby/lib/rubygems/package/tar_output.rb
@@ -0,0 +1,143 @@
+#++
+# Copyright (C) 2004 Mauricio Julio Fernández Pradier
+# See LICENSE.txt for additional licensing information.
+#--
+
+require 'rubygems/package'
+
+##
+# TarOutput is a wrapper to TarWriter that builds gem-format tar file.
+#
+# Gem-format tar files contain the following files:
+# [data.tar.gz] A gzipped tar file containing the files that compose the gem
+# which will be extracted into the gem/ dir on installation.
+# [metadata.gz] A YAML format Gem::Specification.
+# [data.tar.gz.sig] A signature for the gem's data.tar.gz.
+# [metadata.gz.sig] A signature for the gem's metadata.gz.
+#
+# See TarOutput::open for usage details.
+
+class Gem::Package::TarOutput
+
+ ##
+ # Creates a new TarOutput which will yield a TarWriter object for the
+ # data.tar.gz portion of a gem-format tar file.
+ #
+ # See #initialize for details on +io+ and +signer+.
+ #
+ # See #add_gem_contents for details on adding metadata to the tar file.
+
+ def self.open(io, signer = nil, &block) # :yield: data_tar_writer
+ tar_outputter = new io, signer
+ tar_outputter.add_gem_contents(&block)
+ tar_outputter.add_metadata
+ tar_outputter.add_signatures
+
+ ensure
+ tar_outputter.close
+ end
+
+ ##
+ # Creates a new TarOutput that will write a gem-format tar file to +io+. If
+ # +signer+ is given, the data.tar.gz and metadata.gz will be signed and
+ # the signatures will be added to the tar file.
+
+ def initialize(io, signer)
+ @io = io
+ @signer = signer
+
+ @tar_writer = Gem::Package::TarWriter.new @io
+
+ @metadata = nil
+
+ @data_signature = nil
+ @meta_signature = nil
+ end
+
+ ##
+ # Yields a TarWriter for the data.tar.gz inside a gem-format tar file.
+ # The yielded TarWriter has been extended with a #metadata= method for
+ # attaching a YAML format Gem::Specification which will be written by
+ # add_metadata.
+
+ def add_gem_contents
+ @tar_writer.add_file "data.tar.gz", 0644 do |inner|
+ sio = @signer ? StringIO.new : nil
+ Zlib::GzipWriter.wrap(sio || inner) do |os|
+
+ Gem::Package::TarWriter.new os do |data_tar_writer|
+ def data_tar_writer.metadata() @metadata end
+ def data_tar_writer.metadata=(metadata) @metadata = metadata end
+
+ yield data_tar_writer
+
+ @metadata = data_tar_writer.metadata
+ end
+ end
+
+ # if we have a signing key, then sign the data
+ # digest and return the signature
+ if @signer then
+ digest = Gem::Security::OPT[:dgst_algo].digest sio.string
+ @data_signature = @signer.sign digest
+ inner.write sio.string
+ end
+ end
+
+ self
+ end
+
+ ##
+ # Adds metadata.gz to the gem-format tar file which was saved from a
+ # previous #add_gem_contents call.
+
+ def add_metadata
+ return if @metadata.nil?
+
+ @tar_writer.add_file "metadata.gz", 0644 do |io|
+ begin
+ sio = @signer ? StringIO.new : nil
+ gzos = Zlib::GzipWriter.new(sio || io)
+ gzos.write @metadata
+ ensure
+ gzos.flush
+ gzos.finish
+
+ # if we have a signing key, then sign the metadata digest and return
+ # the signature
+ if @signer then
+ digest = Gem::Security::OPT[:dgst_algo].digest sio.string
+ @meta_signature = @signer.sign digest
+ io.write sio.string
+ end
+ end
+ end
+ end
+
+ ##
+ # Adds data.tar.gz.sig and metadata.gz.sig to the gem-format tar files if
+ # a Gem::Security::Signer was sent to initialize.
+
+ def add_signatures
+ if @data_signature then
+ @tar_writer.add_file 'data.tar.gz.sig', 0644 do |io|
+ io.write @data_signature
+ end
+ end
+
+ if @meta_signature then
+ @tar_writer.add_file 'metadata.gz.sig', 0644 do |io|
+ io.write @meta_signature
+ end
+ end
+ end
+
+ ##
+ # Closes the TarOutput.
+
+ def close
+ @tar_writer.close
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/package/tar_reader.rb b/ruby/lib/rubygems/package/tar_reader.rb
new file mode 100644
index 0000000..4aa9c26
--- /dev/null
+++ b/ruby/lib/rubygems/package/tar_reader.rb
@@ -0,0 +1,86 @@
+#++
+# Copyright (C) 2004 Mauricio Julio Fernández Pradier
+# See LICENSE.txt for additional licensing information.
+#--
+
+require 'rubygems/package'
+
+class Gem::Package::TarReader
+
+ include Gem::Package
+
+ class UnexpectedEOF < StandardError; end
+
+ def self.new(io)
+ reader = super
+
+ return reader unless block_given?
+
+ begin
+ yield reader
+ ensure
+ reader.close
+ end
+
+ nil
+ end
+
+ def initialize(io)
+ @io = io
+ @init_pos = io.pos
+ end
+
+ def close
+ end
+
+ def each
+ loop do
+ return if @io.eof?
+
+ header = Gem::Package::TarHeader.from @io
+ return if header.empty?
+
+ entry = Gem::Package::TarReader::Entry.new header, @io
+ size = entry.header.size
+
+ yield entry
+
+ skip = (512 - (size % 512)) % 512
+ pending = size - entry.bytes_read
+
+ begin
+ # avoid reading...
+ @io.seek pending, IO::SEEK_CUR
+ pending = 0
+ rescue Errno::EINVAL, NameError
+ while pending > 0 do
+ bytes_read = @io.read([pending, 4096].min).size
+ raise UnexpectedEOF if @io.eof?
+ pending -= bytes_read
+ end
+ end
+
+ @io.read skip # discard trailing zeros
+
+ # make sure nobody can use #read, #getc or #rewind anymore
+ entry.close
+ end
+ end
+
+ alias each_entry each
+
+ ##
+ # NOTE: Do not call #rewind during #each
+
+ def rewind
+ if @init_pos == 0 then
+ raise Gem::Package::NonSeekableIO unless @io.respond_to? :rewind
+ @io.rewind
+ else
+ raise Gem::Package::NonSeekableIO unless @io.respond_to? :pos=
+ @io.pos = @init_pos
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/package/tar_reader/entry.rb b/ruby/lib/rubygems/package/tar_reader/entry.rb
new file mode 100644
index 0000000..dcc6615
--- /dev/null
+++ b/ruby/lib/rubygems/package/tar_reader/entry.rb
@@ -0,0 +1,99 @@
+#++
+# Copyright (C) 2004 Mauricio Julio Fernández Pradier
+# See LICENSE.txt for additional licensing information.
+#--
+
+require 'rubygems/package'
+
+class Gem::Package::TarReader::Entry
+
+ attr_reader :header
+
+ def initialize(header, io)
+ @closed = false
+ @header = header
+ @io = io
+ @orig_pos = @io.pos
+ @read = 0
+ end
+
+ def check_closed # :nodoc:
+ raise IOError, "closed #{self.class}" if closed?
+ end
+
+ def bytes_read
+ @read
+ end
+
+ def close
+ @closed = true
+ end
+
+ def closed?
+ @closed
+ end
+
+ def eof?
+ check_closed
+
+ @read >= @header.size
+ end
+
+ def full_name
+ if @header.prefix != "" then
+ File.join @header.prefix, @header.name
+ else
+ @header.name
+ end
+ end
+
+ def getc
+ check_closed
+
+ return nil if @read >= @header.size
+
+ ret = @io.getc
+ @read += 1 if ret
+
+ ret
+ end
+
+ def directory?
+ @header.typeflag == "5"
+ end
+
+ def file?
+ @header.typeflag == "0"
+ end
+
+ def pos
+ check_closed
+
+ bytes_read
+ end
+
+ def read(len = nil)
+ check_closed
+
+ return nil if @read >= @header.size
+
+ len ||= @header.size - @read
+ max_read = [len, @header.size - @read].min
+
+ ret = @io.read max_read
+ @read += ret.size
+
+ ret
+ end
+
+ def rewind
+ check_closed
+
+ raise Gem::Package::NonSeekableIO unless @io.respond_to? :pos=
+
+ @io.pos = @orig_pos
+ @read = 0
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/package/tar_writer.rb b/ruby/lib/rubygems/package/tar_writer.rb
new file mode 100644
index 0000000..6e11440
--- /dev/null
+++ b/ruby/lib/rubygems/package/tar_writer.rb
@@ -0,0 +1,180 @@
+#++
+# Copyright (C) 2004 Mauricio Julio Fernández Pradier
+# See LICENSE.txt for additional licensing information.
+#--
+
+require 'rubygems/package'
+
+class Gem::Package::TarWriter
+
+ class FileOverflow < StandardError; end
+
+ class BoundedStream
+
+ attr_reader :limit, :written
+
+ def initialize(io, limit)
+ @io = io
+ @limit = limit
+ @written = 0
+ end
+
+ def write(data)
+ if data.size + @written > @limit
+ raise FileOverflow, "You tried to feed more data than fits in the file."
+ end
+ @io.write data
+ @written += data.size
+ data.size
+ end
+
+ end
+
+ class RestrictedStream
+
+ def initialize(io)
+ @io = io
+ end
+
+ def write(data)
+ @io.write data
+ end
+
+ end
+
+ def self.new(io)
+ writer = super
+
+ return writer unless block_given?
+
+ begin
+ yield writer
+ ensure
+ writer.close
+ end
+
+ nil
+ end
+
+ def initialize(io)
+ @io = io
+ @closed = false
+ end
+
+ def add_file(name, mode)
+ check_closed
+
+ raise Gem::Package::NonSeekableIO unless @io.respond_to? :pos=
+
+ name, prefix = split_name name
+
+ init_pos = @io.pos
+ @io.write "\0" * 512 # placeholder for the header
+
+ yield RestrictedStream.new(@io) if block_given?
+
+ size = @io.pos - init_pos - 512
+
+ remainder = (512 - (size % 512)) % 512
+ @io.write "\0" * remainder
+
+ final_pos = @io.pos
+ @io.pos = init_pos
+
+ header = Gem::Package::TarHeader.new :name => name, :mode => mode,
+ :size => size, :prefix => prefix
+
+ @io.write header
+ @io.pos = final_pos
+
+ self
+ end
+
+ def add_file_simple(name, mode, size)
+ check_closed
+
+ name, prefix = split_name name
+
+ header = Gem::Package::TarHeader.new(:name => name, :mode => mode,
+ :size => size, :prefix => prefix).to_s
+
+ @io.write header
+ os = BoundedStream.new @io, size
+
+ yield os if block_given?
+
+ min_padding = size - os.written
+ @io.write("\0" * min_padding)
+
+ remainder = (512 - (size % 512)) % 512
+ @io.write("\0" * remainder)
+
+ self
+ end
+
+ def check_closed
+ raise IOError, "closed #{self.class}" if closed?
+ end
+
+ def close
+ check_closed
+
+ @io.write "\0" * 1024
+ flush
+
+ @closed = true
+ end
+
+ def closed?
+ @closed
+ end
+
+ def flush
+ check_closed
+
+ @io.flush if @io.respond_to? :flush
+ end
+
+ def mkdir(name, mode)
+ check_closed
+
+ name, prefix = split_name(name)
+
+ header = Gem::Package::TarHeader.new :name => name, :mode => mode,
+ :typeflag => "5", :size => 0,
+ :prefix => prefix
+
+ @io.write header
+
+ self
+ end
+
+ def split_name(name) # :nodoc:
+ raise Gem::Package::TooLongFileName if name.size > 256
+
+ if name.size <= 100 then
+ prefix = ""
+ else
+ parts = name.split(/\//)
+ newname = parts.pop
+ nxt = ""
+
+ loop do
+ nxt = parts.pop
+ break if newname.size + 1 + nxt.size > 100
+ newname = nxt + "/" + newname
+ end
+
+ prefix = (parts + [nxt]).join "/"
+ name = newname
+
+ if name.size > 100 or prefix.size > 155 then
+ raise Gem::Package::TooLongFileName
+ end
+ end
+
+ return name, prefix
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/platform.rb b/ruby/lib/rubygems/platform.rb
new file mode 100644
index 0000000..3e5b5cd
--- /dev/null
+++ b/ruby/lib/rubygems/platform.rb
@@ -0,0 +1,178 @@
+require 'rubygems'
+
+##
+# Available list of platforms for targeting Gem installations.
+
+class Gem::Platform
+
+ @local = nil
+
+ attr_accessor :cpu
+
+ attr_accessor :os
+
+ attr_accessor :version
+
+ def self.local
+ arch = Gem::ConfigMap[:arch]
+ arch = "#{arch}_60" if arch =~ /mswin32$/
+ @local ||= new(arch)
+ end
+
+ def self.match(platform)
+ Gem.platforms.any? do |local_platform|
+ platform.nil? or local_platform == platform or
+ (local_platform != Gem::Platform::RUBY and local_platform =~ platform)
+ end
+ end
+
+ def self.new(arch) # :nodoc:
+ case arch
+ when Gem::Platform::CURRENT then
+ Gem::Platform.local
+ when Gem::Platform::RUBY, nil, '' then
+ Gem::Platform::RUBY
+ else
+ super
+ end
+ end
+
+ def initialize(arch)
+ case arch
+ when Array then
+ @cpu, @os, @version = arch
+ when String then
+ arch = arch.split '-'
+
+ if arch.length > 2 and arch.last !~ /\d/ then # reassemble x86-linux-gnu
+ extra = arch.pop
+ arch.last << "-#{extra}"
+ end
+
+ cpu = arch.shift
+
+ @cpu = case cpu
+ when /i\d86/ then 'x86'
+ else cpu
+ end
+
+ if arch.length == 2 and arch.last =~ /^\d+(\.\d+)?$/ then # for command-line
+ @os, @version = arch
+ return
+ end
+
+ os, = arch
+ @cpu, os = nil, cpu if os.nil? # legacy jruby
+
+ @os, @version = case os
+ when /aix(\d+)/ then [ 'aix', $1 ]
+ when /cygwin/ then [ 'cygwin', nil ]
+ when /darwin(\d+)?/ then [ 'darwin', $1 ]
+ when /freebsd(\d+)/ then [ 'freebsd', $1 ]
+ when /hpux(\d+)/ then [ 'hpux', $1 ]
+ when /^java$/, /^jruby$/ then [ 'java', nil ]
+ when /^java([\d.]*)/ then [ 'java', $1 ]
+ when /linux/ then [ 'linux', $1 ]
+ when /mingw32/ then [ 'mingw32', nil ]
+ when /(mswin\d+)(\_(\d+))?/ then
+ os, version = $1, $3
+ @cpu = 'x86' if @cpu.nil? and os =~ /32$/
+ [os, version]
+ when /netbsdelf/ then [ 'netbsdelf', nil ]
+ when /openbsd(\d+\.\d+)/ then [ 'openbsd', $1 ]
+ when /solaris(\d+\.\d+)/ then [ 'solaris', $1 ]
+ # test
+ when /^(\w+_platform)(\d+)/ then [ $1, $2 ]
+ else [ 'unknown', nil ]
+ end
+ when Gem::Platform then
+ @cpu = arch.cpu
+ @os = arch.os
+ @version = arch.version
+ else
+ raise ArgumentError, "invalid argument #{arch.inspect}"
+ end
+ end
+
+ def inspect
+ "#<%s:0x%x @cpu=%p, @os=%p, @version=%p>" % [self.class, object_id, *to_a]
+ end
+
+ def to_a
+ [@cpu, @os, @version]
+ end
+
+ def to_s
+ to_a.compact.join '-'
+ end
+
+ ##
+ # Is +other+ equal to this platform? Two platforms are equal if they have
+ # the same CPU, OS and version.
+
+ def ==(other)
+ self.class === other and
+ @cpu == other.cpu and @os == other.os and @version == other.version
+ end
+
+ ##
+ # Does +other+ match this platform? Two platforms match if they have the
+ # same CPU, or either has a CPU of 'universal', they have the same OS, and
+ # they have the same version, or either has no version.
+
+ def ===(other)
+ return nil unless Gem::Platform === other
+
+ # cpu
+ (@cpu == 'universal' or other.cpu == 'universal' or @cpu == other.cpu) and
+
+ # os
+ @os == other.os and
+
+ # version
+ (@version.nil? or other.version.nil? or @version == other.version)
+ end
+
+ ##
+ # Does +other+ match this platform? If +other+ is a String it will be
+ # converted to a Gem::Platform first. See #=== for matching rules.
+
+ def =~(other)
+ case other
+ when Gem::Platform then # nop
+ when String then
+ # This data is from http://gems.rubyforge.org/gems/yaml on 19 Aug 2007
+ other = case other
+ when /^i686-darwin(\d)/ then ['x86', 'darwin', $1]
+ when /^i\d86-linux/ then ['x86', 'linux', nil]
+ when 'java', 'jruby' then [nil, 'java', nil]
+ when /mswin32(\_(\d+))?/ then ['x86', 'mswin32', $2]
+ when 'powerpc-darwin' then ['powerpc', 'darwin', nil]
+ when /powerpc-darwin(\d)/ then ['powerpc', 'darwin', $1]
+ when /sparc-solaris2.8/ then ['sparc', 'solaris', '2.8']
+ when /universal-darwin(\d)/ then ['universal', 'darwin', $1]
+ else other
+ end
+
+ other = Gem::Platform.new other
+ else
+ return nil
+ end
+
+ self === other
+ end
+
+ ##
+ # A pure-ruby gem that may use Gem::Specification#extensions to build
+ # binary files.
+
+ RUBY = 'ruby'
+
+ ##
+ # A platform-specific gem that is built for the packaging ruby's platform.
+ # This will be replaced with Gem::Platform::local.
+
+ CURRENT = 'current'
+
+end
+
diff --git a/ruby/lib/rubygems/remote_fetcher.rb b/ruby/lib/rubygems/remote_fetcher.rb
new file mode 100644
index 0000000..1570740
--- /dev/null
+++ b/ruby/lib/rubygems/remote_fetcher.rb
@@ -0,0 +1,344 @@
+require 'net/http'
+require 'stringio'
+require 'time'
+require 'uri'
+
+require 'rubygems'
+
+##
+# RemoteFetcher handles the details of fetching gems and gem information from
+# a remote source.
+
+class Gem::RemoteFetcher
+
+ include Gem::UserInteraction
+
+ ##
+ # A FetchError exception wraps up the various possible IO and HTTP failures
+ # that could happen while downloading from the internet.
+
+ class FetchError < Gem::Exception
+
+ ##
+ # The URI which was being accessed when the exception happened.
+
+ attr_accessor :uri
+
+ def initialize(message, uri)
+ super message
+ @uri = uri
+ end
+
+ def to_s # :nodoc:
+ "#{super} (#{uri})"
+ end
+
+ end
+
+ @fetcher = nil
+
+ ##
+ # Cached RemoteFetcher instance.
+
+ def self.fetcher
+ @fetcher ||= self.new Gem.configuration[:http_proxy]
+ end
+
+ ##
+ # Initialize a remote fetcher using the source URI and possible proxy
+ # information.
+ #
+ # +proxy+
+ # * [String]: explicit specification of proxy; overrides any environment
+ # variable setting
+ # * nil: respect environment variables (HTTP_PROXY, HTTP_PROXY_USER,
+ # HTTP_PROXY_PASS)
+ # * <tt>:no_proxy</tt>: ignore environment variables and _don't_ use a proxy
+
+ def initialize(proxy)
+ Socket.do_not_reverse_lookup = true
+
+ @connections = {}
+ @requests = Hash.new 0
+ @proxy_uri =
+ case proxy
+ when :no_proxy then nil
+ when nil then get_proxy_from_env
+ when URI::HTTP then proxy
+ else URI.parse(proxy)
+ end
+ end
+
+ ##
+ # Moves the gem +spec+ from +source_uri+ to the cache dir unless it is
+ # already there. If the source_uri is local the gem cache dir copy is
+ # always replaced.
+
+ def download(spec, source_uri, install_dir = Gem.dir)
+ if File.writable?(install_dir)
+ cache_dir = File.join install_dir, 'cache'
+ else
+ cache_dir = File.join(Gem.user_dir, 'cache')
+ end
+
+ gem_file_name = "#{spec.full_name}.gem"
+ local_gem_path = File.join cache_dir, gem_file_name
+
+ FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir
+
+ source_uri = URI.parse source_uri unless URI::Generic === source_uri
+ scheme = source_uri.scheme
+
+ # URI.parse gets confused by MS Windows paths with forward slashes.
+ scheme = nil if scheme =~ /^[a-z]$/i
+
+ case scheme
+ when 'http', 'https' then
+ unless File.exist? local_gem_path then
+ begin
+ say "Downloading gem #{gem_file_name}" if
+ Gem.configuration.really_verbose
+
+ remote_gem_path = source_uri + "gems/#{gem_file_name}"
+
+ gem = Gem::RemoteFetcher.fetcher.fetch_path remote_gem_path
+ rescue Gem::RemoteFetcher::FetchError
+ raise if spec.original_platform == spec.platform
+
+ alternate_name = "#{spec.original_name}.gem"
+
+ say "Failed, downloading gem #{alternate_name}" if
+ Gem.configuration.really_verbose
+
+ remote_gem_path = source_uri + "gems/#{alternate_name}"
+
+ gem = Gem::RemoteFetcher.fetcher.fetch_path remote_gem_path
+ end
+
+ File.open local_gem_path, 'wb' do |fp|
+ fp.write gem
+ end
+ end
+ when nil, 'file' then # TODO test for local overriding cache
+ begin
+ FileUtils.cp source_uri.to_s, local_gem_path
+ rescue Errno::EACCES
+ local_gem_path = source_uri.to_s
+ end
+
+ say "Using local gem #{local_gem_path}" if
+ Gem.configuration.really_verbose
+ else
+ raise Gem::InstallError, "unsupported URI scheme #{source_uri.scheme}"
+ end
+
+ local_gem_path
+ end
+
+ ##
+ # Downloads +uri+ and returns it as a String.
+
+ def fetch_path(uri, mtime = nil, head = false)
+ data = open_uri_or_path uri, mtime, head
+ data = Gem.gunzip data if data and not head and uri.to_s =~ /gz$/
+ data
+ rescue FetchError
+ raise
+ rescue Timeout::Error
+ raise FetchError.new('timed out', uri)
+ rescue IOError, SocketError, SystemCallError => e
+ raise FetchError.new("#{e.class}: #{e}", uri)
+ end
+
+ ##
+ # Returns the size of +uri+ in bytes.
+
+ def fetch_size(uri) # TODO: phase this out
+ response = fetch_path(uri, nil, true)
+
+ response['content-length'].to_i
+ end
+
+ def escape(str)
+ return unless str
+ URI.escape(str)
+ end
+
+ def unescape(str)
+ return unless str
+ URI.unescape(str)
+ end
+
+ ##
+ # Returns an HTTP proxy URI if one is set in the environment variables.
+
+ def get_proxy_from_env
+ env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
+
+ return nil if env_proxy.nil? or env_proxy.empty?
+
+ uri = URI.parse env_proxy
+
+ if uri and uri.user.nil? and uri.password.nil? then
+ # Probably we have http_proxy_* variables?
+ uri.user = escape(ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER'])
+ uri.password = escape(ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS'])
+ end
+
+ uri
+ end
+
+ ##
+ # Normalize the URI by adding "http://" if it is missing.
+
+ def normalize_uri(uri)
+ (uri =~ /^(https?|ftp|file):/) ? uri : "http://#{uri}"
+ end
+
+ ##
+ # Creates or an HTTP connection based on +uri+, or retrieves an existing
+ # connection, using a proxy if needed.
+
+ def connection_for(uri)
+ net_http_args = [uri.host, uri.port]
+
+ if @proxy_uri then
+ net_http_args += [
+ @proxy_uri.host,
+ @proxy_uri.port,
+ @proxy_uri.user,
+ @proxy_uri.password
+ ]
+ end
+
+ connection_id = net_http_args.join ':'
+ @connections[connection_id] ||= Net::HTTP.new(*net_http_args)
+ connection = @connections[connection_id]
+
+ if uri.scheme == 'https' and not connection.started? then
+ require 'net/https'
+ connection.use_ssl = true
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ end
+
+ connection.start unless connection.started?
+
+ connection
+ end
+
+ ##
+ # Read the data from the (source based) URI, but if it is a file:// URI,
+ # read from the filesystem instead.
+
+ def open_uri_or_path(uri, last_modified = nil, head = false, depth = 0)
+ raise "block is dead" if block_given?
+
+ return open(get_file_uri_path(uri)) if file_uri? uri
+
+ uri = URI.parse uri unless URI::Generic === uri
+ raise ArgumentError, 'uri is not an HTTP URI' unless URI::HTTP === uri
+
+ fetch_type = head ? Net::HTTP::Head : Net::HTTP::Get
+ response = request uri, fetch_type, last_modified
+
+ case response
+ when Net::HTTPOK, Net::HTTPNotModified then
+ head ? response : response.body
+ when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther,
+ Net::HTTPTemporaryRedirect then
+ raise FetchError.new('too many redirects', uri) if depth > 10
+
+ open_uri_or_path(response['Location'], last_modified, head, depth + 1)
+ else
+ raise FetchError.new("bad response #{response.message} #{response.code}", uri)
+ end
+ end
+
+ ##
+ # Performs a Net::HTTP request of type +request_class+ on +uri+ returning
+ # a Net::HTTP response object. request maintains a table of persistent
+ # connections to reduce connect overhead.
+
+ def request(uri, request_class, last_modified = nil)
+ request = request_class.new uri.request_uri
+
+ unless uri.nil? || uri.user.nil? || uri.user.empty? then
+ request.basic_auth uri.user, uri.password
+ end
+
+ ua = "RubyGems/#{Gem::RubyGemsVersion} #{Gem::Platform.local}"
+ ua << " Ruby/#{RUBY_VERSION} (#{RUBY_RELEASE_DATE}"
+ ua << " patchlevel #{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
+ ua << ")"
+
+ request.add_field 'User-Agent', ua
+ request.add_field 'Connection', 'keep-alive'
+ request.add_field 'Keep-Alive', '30'
+
+ if last_modified then
+ last_modified = last_modified.utc
+ request.add_field 'If-Modified-Since', last_modified.rfc2822
+ end
+
+ connection = connection_for uri
+
+ retried = false
+ bad_response = false
+
+ begin
+ @requests[connection.object_id] += 1
+ response = connection.request request
+ say "#{request.method} #{response.code} #{response.message}: #{uri}" if
+ Gem.configuration.really_verbose
+ rescue Net::HTTPBadResponse
+ reset connection
+
+ raise FetchError.new('too many bad responses', uri) if bad_response
+
+ bad_response = true
+ retry
+ # HACK work around EOFError bug in Net::HTTP
+ # NOTE Errno::ECONNABORTED raised a lot on Windows, and make impossible
+ # to install gems.
+ rescue EOFError, Errno::ECONNABORTED, Errno::ECONNRESET
+ requests = @requests[connection.object_id]
+ say "connection reset after #{requests} requests, retrying" if
+ Gem.configuration.really_verbose
+
+ raise FetchError.new('too many connection resets', uri) if retried
+
+ reset connection
+
+ retried = true
+ retry
+ end
+
+ response
+ end
+
+ ##
+ # Resets HTTP connection +connection+.
+
+ def reset(connection)
+ @requests.delete connection.object_id
+
+ connection.finish
+ connection.start
+ end
+
+ ##
+ # Checks if the provided string is a file:// URI.
+
+ def file_uri?(uri)
+ uri =~ %r{\Afile://}
+ end
+
+ ##
+ # Given a file:// URI, returns its local path.
+
+ def get_file_uri_path(uri)
+ uri.sub(%r{\Afile://}, '')
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/require_paths_builder.rb b/ruby/lib/rubygems/require_paths_builder.rb
new file mode 100644
index 0000000..fe4f593
--- /dev/null
+++ b/ruby/lib/rubygems/require_paths_builder.rb
@@ -0,0 +1,15 @@
+module Gem
+ module RequirePathsBuilder
+ def write_require_paths_file_if_needed(spec = @spec, gem_home = @gem_home)
+ return if spec.require_paths == ["lib"] && (spec.bindir.nil? || spec.bindir == "bin")
+ file_name = File.join(gem_home, 'gems', "#{@spec.full_name}", ".require_paths")
+ file_name.untaint
+ File.open(file_name, "w") do |file|
+ spec.require_paths.each do |path|
+ file.puts path
+ end
+ file.puts spec.bindir if spec.bindir
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/ruby/lib/rubygems/requirement.rb b/ruby/lib/rubygems/requirement.rb
new file mode 100644
index 0000000..c9128b5
--- /dev/null
+++ b/ruby/lib/rubygems/requirement.rb
@@ -0,0 +1,163 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems/version'
+
+##
+# Requirement version includes a prefaced comparator in addition
+# to a version number.
+#
+# A Requirement object can actually contain multiple, er,
+# requirements, as in (> 1.2, < 2.0).
+
+class Gem::Requirement
+
+ include Comparable
+
+ attr_reader :requirements
+
+ OPS = {
+ "=" => lambda { |v, r| v == r },
+ "!=" => lambda { |v, r| v != r },
+ ">" => lambda { |v, r| v > r },
+ "<" => lambda { |v, r| v < r },
+ ">=" => lambda { |v, r| v >= r },
+ "<=" => lambda { |v, r| v <= r },
+ "~>" => lambda { |v, r| v >= r && v < r.bump }
+ }
+
+ OP_RE = /#{OPS.keys.map{ |k| Regexp.quote k }.join '|'}/o
+
+ ##
+ # Factory method to create a Gem::Requirement object. Input may be a
+ # Version, a String, or nil. Intended to simplify client code.
+ #
+ # If the input is "weird", the default version requirement is returned.
+
+ def self.create(input)
+ case input
+ when Gem::Requirement then
+ input
+ when Gem::Version, Array then
+ new input
+ else
+ if input.respond_to? :to_str then
+ self.new [input.to_str]
+ else
+ self.default
+ end
+ end
+ end
+
+ ##
+ # A default "version requirement" can surely _only_ be '>= 0'.
+ #--
+ # This comment once said:
+ #
+ # "A default "version requirement" can surely _only_ be '> 0'."
+
+ def self.default
+ self.new ['>= 0']
+ end
+
+ ##
+ # Constructs a Requirement from +requirements+ which can be a String, a
+ # Gem::Version, or an Array of those. See parse for details on the
+ # formatting of requirement strings.
+
+ def initialize(requirements)
+ @requirements = case requirements
+ when Array then
+ requirements.map do |requirement|
+ parse(requirement)
+ end
+ else
+ [parse(requirements)]
+ end
+ @version = nil # Avoid warnings.
+ end
+
+ ##
+ # Marshal raw requirements, rather than the full object
+
+ def marshal_dump # :nodoc:
+ [@requirements]
+ end
+
+ ##
+ # Load custom marshal format
+
+ def marshal_load(array) # :nodoc:
+ @requirements = array[0]
+ @version = nil
+ end
+
+ def to_s # :nodoc:
+ as_list.join(", ")
+ end
+
+ def as_list
+ normalize
+ @requirements.collect { |req|
+ "#{req[0]} #{req[1]}"
+ }
+ end
+
+ def normalize
+ return if not defined? @version or @version.nil?
+ @requirements = [parse(@version)]
+ @nums = nil
+ @version = nil
+ @op = nil
+ end
+
+ ##
+ # True if this requirement satisfied by the Gem::Version +version+.
+
+ def satisfied_by?(version)
+ normalize
+ @requirements.all? { |op, rv| satisfy?(op, version, rv) }
+ end
+
+ ##
+ # Is "+version+ +op+ +required_version+" satisfied?
+
+ def satisfy?(op, version, required_version)
+ OPS[op].call(version, required_version)
+ end
+
+ ##
+ # Parse the version requirement obj returning the operator and version.
+ #
+ # The requirement can be a String or a Gem::Version. A String can be an
+ # operator (<, <=, =, =>, >, !=, ~>), a version number, or both, operator
+ # first.
+
+ def parse(obj)
+ case obj
+ when /^\s*(#{OP_RE})\s*([0-9.]+)\s*$/o then
+ [$1, Gem::Version.new($2)]
+ when /^\s*([0-9.]+)\s*$/ then
+ ['=', Gem::Version.new($1)]
+ when /^\s*(#{OP_RE})\s*$/o then
+ [$1, Gem::Version.new('0')]
+ when Gem::Version then
+ ['=', obj]
+ else
+ fail ArgumentError, "Illformed requirement [#{obj.inspect}]"
+ end
+ end
+
+ def <=>(other) # :nodoc:
+ to_s <=> other.to_s
+ end
+
+ def hash # :nodoc:
+ to_s.hash
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/rubygems_version.rb b/ruby/lib/rubygems/rubygems_version.rb
new file mode 100644
index 0000000..d7b5622
--- /dev/null
+++ b/ruby/lib/rubygems/rubygems_version.rb
@@ -0,0 +1,6 @@
+# DO NOT EDIT
+# This file is auto-generated by build scripts.
+# See: rake update_version
+module Gem
+ RubyGemsVersion = '1.3.1'
+end
diff --git a/ruby/lib/rubygems/security.rb b/ruby/lib/rubygems/security.rb
new file mode 100644
index 0000000..abf3cf4
--- /dev/null
+++ b/ruby/lib/rubygems/security.rb
@@ -0,0 +1,786 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+require 'rubygems/gem_openssl'
+
+# = Signed Gems README
+#
+# == Table of Contents
+# * Overview
+# * Walkthrough
+# * Command-Line Options
+# * OpenSSL Reference
+# * Bugs/TODO
+# * About the Author
+#
+# == Overview
+#
+# Gem::Security implements cryptographic signatures in RubyGems. The section
+# below is a step-by-step guide to using signed gems and generating your own.
+#
+# == Walkthrough
+#
+# In order to start signing your gems, you'll need to build a private key and
+# a self-signed certificate. Here's how:
+#
+# # build a private key and certificate for gemmaster@example.com
+# $ gem cert --build gemmaster@example.com
+#
+# This could take anywhere from 5 seconds to 10 minutes, depending on the
+# speed of your computer (public key algorithms aren't exactly the speediest
+# crypto algorithms in the world). When it's finished, you'll see the files
+# "gem-private_key.pem" and "gem-public_cert.pem" in the current directory.
+#
+# First things first: take the "gem-private_key.pem" file and move it
+# somewhere private, preferably a directory only you have access to, a floppy
+# (yuck!), a CD-ROM, or something comparably secure. Keep your private key
+# hidden; if it's compromised, someone can sign packages as you (note: PKI has
+# ways of mitigating the risk of stolen keys; more on that later).
+#
+# Now, let's sign an existing gem. I'll be using my Imlib2-Ruby bindings, but
+# you can use whatever gem you'd like. Open up your existing gemspec file and
+# add the following lines:
+#
+# # signing key and certificate chain
+# s.signing_key = '/mnt/floppy/gem-private_key.pem'
+# s.cert_chain = ['gem-public_cert.pem']
+#
+# (Be sure to replace "/mnt/floppy" with the ultra-secret path to your private
+# key).
+#
+# After that, go ahead and build your gem as usual. Congratulations, you've
+# just built your first signed gem! If you peek inside your gem file, you'll
+# see a couple of new files have been added:
+#
+# $ tar tf tar tf Imlib2-Ruby-0.5.0.gem
+# data.tar.gz
+# data.tar.gz.sig
+# metadata.gz
+# metadata.gz.sig
+#
+# Now let's verify the signature. Go ahead and install the gem, but add the
+# following options: "-P HighSecurity", like this:
+#
+# # install the gem with using the security policy "HighSecurity"
+# $ sudo gem install Imlib2-Ruby-0.5.0.gem -P HighSecurity
+#
+# The -P option sets your security policy -- we'll talk about that in just a
+# minute. Eh, what's this?
+#
+# Attempting local installation of 'Imlib2-Ruby-0.5.0.gem'
+# ERROR: Error installing gem Imlib2-Ruby-0.5.0.gem[.gem]: Couldn't
+# verify data signature: Untrusted Signing Chain Root: cert =
+# '/CN=gemmaster/DC=example/DC=com', error = 'path
+# "/root/.rubygems/trust/cert-15dbb43a6edf6a70a85d4e784e2e45312cff7030.pem"
+# does not exist'
+#
+# The culprit here is the security policy. RubyGems has several different
+# security policies. Let's take a short break and go over the security
+# policies. Here's a list of the available security policies, and a brief
+# description of each one:
+#
+# * NoSecurity - Well, no security at all. Signed packages are treated like
+# unsigned packages.
+# * LowSecurity - Pretty much no security. If a package is signed then
+# RubyGems will make sure the signature matches the signing
+# certificate, and that the signing certificate hasn't expired, but
+# that's it. A malicious user could easily circumvent this kind of
+# security.
+# * MediumSecurity - Better than LowSecurity and NoSecurity, but still
+# fallible. Package contents are verified against the signing
+# certificate, and the signing certificate is checked for validity,
+# and checked against the rest of the certificate chain (if you don't
+# know what a certificate chain is, stay tuned, we'll get to that).
+# The biggest improvement over LowSecurity is that MediumSecurity
+# won't install packages that are signed by untrusted sources.
+# Unfortunately, MediumSecurity still isn't totally secure -- a
+# malicious user can still unpack the gem, strip the signatures, and
+# distribute the gem unsigned.
+# * HighSecurity - Here's the bugger that got us into this mess.
+# The HighSecurity policy is identical to the MediumSecurity policy,
+# except that it does not allow unsigned gems. A malicious user
+# doesn't have a whole lot of options here; he can't modify the
+# package contents without invalidating the signature, and he can't
+# modify or remove signature or the signing certificate chain, or
+# RubyGems will simply refuse to install the package. Oh well, maybe
+# he'll have better luck causing problems for CPAN users instead :).
+#
+# So, the reason RubyGems refused to install our shiny new signed gem was
+# because it was from an untrusted source. Well, my code is infallible
+# (hah!), so I'm going to add myself as a trusted source.
+#
+# Here's how:
+#
+# # add trusted certificate
+# gem cert --add gem-public_cert.pem
+#
+# I've added my public certificate as a trusted source. Now I can install
+# packages signed my private key without any hassle. Let's try the install
+# command above again:
+#
+# # install the gem with using the HighSecurity policy (and this time
+# # without any shenanigans)
+# $ sudo gem install Imlib2-Ruby-0.5.0.gem -P HighSecurity
+#
+# This time RubyGems should accept your signed package and begin installing.
+# While you're waiting for RubyGems to work it's magic, have a look at some of
+# the other security commands:
+#
+# Usage: gem cert [options]
+#
+# Options:
+# -a, --add CERT Add a trusted certificate.
+# -l, --list List trusted certificates.
+# -r, --remove STRING Remove trusted certificates containing STRING.
+# -b, --build EMAIL_ADDR Build private key and self-signed certificate
+# for EMAIL_ADDR.
+# -C, --certificate CERT Certificate for --sign command.
+# -K, --private-key KEY Private key for --sign command.
+# -s, --sign NEWCERT Sign a certificate with my key and certificate.
+#
+# (By the way, you can pull up this list any time you'd like by typing "gem
+# cert --help")
+#
+# Hmm. We've already covered the "--build" option, and the "--add", "--list",
+# and "--remove" commands seem fairly straightforward; they allow you to add,
+# list, and remove the certificates in your trusted certificate list. But
+# what's with this "--sign" option?
+#
+# To answer that question, let's take a look at "certificate chains", a
+# concept I mentioned earlier. There are a couple of problems with
+# self-signed certificates: first of all, self-signed certificates don't offer
+# a whole lot of security. Sure, the certificate says Yukihiro Matsumoto, but
+# how do I know it was actually generated and signed by matz himself unless he
+# gave me the certificate in person?
+#
+# The second problem is scalability. Sure, if there are 50 gem authors, then
+# I have 50 trusted certificates, no problem. What if there are 500 gem
+# authors? 1000? Having to constantly add new trusted certificates is a
+# pain, and it actually makes the trust system less secure by encouraging
+# RubyGems users to blindly trust new certificates.
+#
+# Here's where certificate chains come in. A certificate chain establishes an
+# arbitrarily long chain of trust between an issuing certificate and a child
+# certificate. So instead of trusting certificates on a per-developer basis,
+# we use the PKI concept of certificate chains to build a logical hierarchy of
+# trust. Here's a hypothetical example of a trust hierarchy based (roughly)
+# on geography:
+#
+#
+# --------------------------
+# | rubygems@rubyforge.org |
+# --------------------------
+# |
+# -----------------------------------
+# | |
+# ---------------------------- -----------------------------
+# | seattle.rb@zenspider.com | | dcrubyists@richkilmer.com |
+# ---------------------------- -----------------------------
+# | | | |
+# --------------- ---------------- ----------- --------------
+# | alf@seattle | | bob@portland | | pabs@dc | | tomcope@dc |
+# --------------- ---------------- ----------- --------------
+#
+#
+# Now, rather than having 4 trusted certificates (one for alf@seattle,
+# bob@portland, pabs@dc, and tomecope@dc), a user could actually get by with 1
+# certificate: the "rubygems@rubyforge.org" certificate. Here's how it works:
+#
+# I install "Alf2000-Ruby-0.1.0.gem", a package signed by "alf@seattle". I've
+# never heard of "alf@seattle", but his certificate has a valid signature from
+# the "seattle.rb@zenspider.com" certificate, which in turn has a valid
+# signature from the "rubygems@rubyforge.org" certificate. Voila! At this
+# point, it's much more reasonable for me to trust a package signed by
+# "alf@seattle", because I can establish a chain to "rubygems@rubyforge.org",
+# which I do trust.
+#
+# And the "--sign" option allows all this to happen. A developer creates
+# their build certificate with the "--build" option, then has their
+# certificate signed by taking it with them to their next regional Ruby meetup
+# (in our hypothetical example), and it's signed there by the person holding
+# the regional RubyGems signing certificate, which is signed at the next
+# RubyConf by the holder of the top-level RubyGems certificate. At each point
+# the issuer runs the same command:
+#
+# # sign a certificate with the specified key and certificate
+# # (note that this modifies client_cert.pem!)
+# $ gem cert -K /mnt/floppy/issuer-priv_key.pem -C issuer-pub_cert.pem
+# --sign client_cert.pem
+#
+# Then the holder of issued certificate (in this case, our buddy
+# "alf@seattle"), can start using this signed certificate to sign RubyGems.
+# By the way, in order to let everyone else know about his new fancy signed
+# certificate, "alf@seattle" would change his gemspec file to look like this:
+#
+# # signing key (still kept in an undisclosed location!)
+# s.signing_key = '/mnt/floppy/alf-private_key.pem'
+#
+# # certificate chain (includes the issuer certificate now too)
+# s.cert_chain = ['/home/alf/doc/seattlerb-public_cert.pem',
+# '/home/alf/doc/alf_at_seattle-public_cert.pem']
+#
+# Obviously, this RubyGems trust infrastructure doesn't exist yet. Also, in
+# the "real world" issuers actually generate the child certificate from a
+# certificate request, rather than sign an existing certificate. And our
+# hypothetical infrastructure is missing a certificate revocation system.
+# These are that can be fixed in the future...
+#
+# I'm sure your new signed gem has finished installing by now (unless you're
+# installing rails and all it's dependencies, that is ;D). At this point you
+# should know how to do all of these new and interesting things:
+#
+# * build a gem signing key and certificate
+# * modify your existing gems to support signing
+# * adjust your security policy
+# * modify your trusted certificate list
+# * sign a certificate
+#
+# If you've got any questions, feel free to contact me at the email address
+# below. The next couple of sections
+#
+#
+# == Command-Line Options
+#
+# Here's a brief summary of the certificate-related command line options:
+#
+# gem install
+# -P, --trust-policy POLICY Specify gem trust policy.
+#
+# gem cert
+# -a, --add CERT Add a trusted certificate.
+# -l, --list List trusted certificates.
+# -r, --remove STRING Remove trusted certificates containing
+# STRING.
+# -b, --build EMAIL_ADDR Build private key and self-signed
+# certificate for EMAIL_ADDR.
+# -C, --certificate CERT Certificate for --sign command.
+# -K, --private-key KEY Private key for --sign command.
+# -s, --sign NEWCERT Sign a certificate with my key and
+# certificate.
+#
+# A more detailed description of each options is available in the walkthrough
+# above.
+#
+#
+# == OpenSSL Reference
+#
+# The .pem files generated by --build and --sign are just basic OpenSSL PEM
+# files. Here's a couple of useful commands for manipulating them:
+#
+# # convert a PEM format X509 certificate into DER format:
+# # (note: Windows .cer files are X509 certificates in DER format)
+# $ openssl x509 -in input.pem -outform der -out output.der
+#
+# # print out the certificate in a human-readable format:
+# $ openssl x509 -in input.pem -noout -text
+#
+# And you can do the same thing with the private key file as well:
+#
+# # convert a PEM format RSA key into DER format:
+# $ openssl rsa -in input_key.pem -outform der -out output_key.der
+#
+# # print out the key in a human readable format:
+# $ openssl rsa -in input_key.pem -noout -text
+#
+# == Bugs/TODO
+#
+# * There's no way to define a system-wide trust list.
+# * custom security policies (from a YAML file, etc)
+# * Simple method to generate a signed certificate request
+# * Support for OCSP, SCVP, CRLs, or some other form of cert
+# status check (list is in order of preference)
+# * Support for encrypted private keys
+# * Some sort of semi-formal trust hierarchy (see long-winded explanation
+# above)
+# * Path discovery (for gem certificate chains that don't have a self-signed
+# root) -- by the way, since we don't have this, THE ROOT OF THE CERTIFICATE
+# CHAIN MUST BE SELF SIGNED if Policy#verify_root is true (and it is for the
+# MediumSecurity and HighSecurity policies)
+# * Better explanation of X509 naming (ie, we don't have to use email
+# addresses)
+# * Possible alternate signing mechanisms (eg, via PGP). this could be done
+# pretty easily by adding a :signing_type attribute to the gemspec, then add
+# the necessary support in other places
+# * Honor AIA field (see note about OCSP above)
+# * Maybe honor restriction extensions?
+# * Might be better to store the certificate chain as a PKCS#7 or PKCS#12
+# file, instead of an array embedded in the metadata. ideas?
+# * Possibly embed signature and key algorithms into metadata (right now
+# they're assumed to be the same as what's set in Gem::Security::OPT)
+#
+# == About the Author
+#
+# Paul Duncan <pabs@pablotron.org>
+# http://pablotron.org/
+
+module Gem::Security
+
+ class Exception < Gem::Exception; end
+
+ #
+ # default options for most of the methods below
+ #
+ OPT = {
+ # private key options
+ :key_algo => Gem::SSL::PKEY_RSA,
+ :key_size => 2048,
+
+ # public cert options
+ :cert_age => 365 * 24 * 3600, # 1 year
+ :dgst_algo => Gem::SSL::DIGEST_SHA1,
+
+ # x509 certificate extensions
+ :cert_exts => {
+ 'basicConstraints' => 'CA:FALSE',
+ 'subjectKeyIdentifier' => 'hash',
+ 'keyUsage' => 'keyEncipherment,dataEncipherment,digitalSignature',
+ },
+
+ # save the key and cert to a file in build_self_signed_cert()?
+ :save_key => true,
+ :save_cert => true,
+
+ # if you define either of these, then they'll be used instead of
+ # the output_fmt macro below
+ :save_key_path => nil,
+ :save_cert_path => nil,
+
+ # output name format for self-signed certs
+ :output_fmt => 'gem-%s.pem',
+ :munge_re => Regexp.new(/[^a-z0-9_.-]+/),
+
+ # output directory for trusted certificate checksums
+ :trust_dir => File::join(Gem.user_home, '.gem', 'trust'),
+
+ # default permissions for trust directory and certs
+ :perms => {
+ :trust_dir => 0700,
+ :trusted_cert => 0600,
+ :signing_cert => 0600,
+ :signing_key => 0600,
+ },
+ }
+
+ #
+ # A Gem::Security::Policy object encapsulates the settings for verifying
+ # signed gem files. This is the base class. You can either declare an
+ # instance of this or use one of the preset security policies below.
+ #
+ class Policy
+ attr_accessor :verify_data, :verify_signer, :verify_chain,
+ :verify_root, :only_trusted, :only_signed
+
+ #
+ # Create a new Gem::Security::Policy object with the given mode and
+ # options.
+ #
+ def initialize(policy = {}, opt = {})
+ # set options
+ @opt = Gem::Security::OPT.merge(opt)
+
+ # build policy
+ policy.each_pair do |key, val|
+ case key
+ when :verify_data then @verify_data = val
+ when :verify_signer then @verify_signer = val
+ when :verify_chain then @verify_chain = val
+ when :verify_root then @verify_root = val
+ when :only_trusted then @only_trusted = val
+ when :only_signed then @only_signed = val
+ end
+ end
+ end
+
+ #
+ # Get the path to the file for this cert.
+ #
+ def self.trusted_cert_path(cert, opt = {})
+ opt = Gem::Security::OPT.merge(opt)
+
+ # get digest algorithm, calculate checksum of root.subject
+ algo = opt[:dgst_algo]
+ dgst = algo.hexdigest(cert.subject.to_s)
+
+ # build path to trusted cert file
+ name = "cert-#{dgst}.pem"
+
+ # join and return path components
+ File::join(opt[:trust_dir], name)
+ end
+
+ #
+ # Verify that the gem data with the given signature and signing chain
+ # matched this security policy at the specified time.
+ #
+ def verify_gem(signature, data, chain, time = Time.now)
+ Gem.ensure_ssl_available
+ cert_class = OpenSSL::X509::Certificate
+ exc = Gem::Security::Exception
+ chain ||= []
+
+ chain = chain.map{ |str| cert_class.new(str) }
+ signer, ch_len = chain[-1], chain.size
+
+ # make sure signature is valid
+ if @verify_data
+ # get digest algorithm (TODO: this should be configurable)
+ dgst = @opt[:dgst_algo]
+
+ # verify the data signature (this is the most important part, so don't
+ # screw it up :D)
+ v = signer.public_key.verify(dgst.new, signature, data)
+ raise exc, "Invalid Gem Signature" unless v
+
+ # make sure the signer is valid
+ if @verify_signer
+ # make sure the signing cert is valid right now
+ v = signer.check_validity(nil, time)
+ raise exc, "Invalid Signature: #{v[:desc]}" unless v[:is_valid]
+ end
+ end
+
+ # make sure the certificate chain is valid
+ if @verify_chain
+ # iterate down over the chain and verify each certificate against it's
+ # issuer
+ (ch_len - 1).downto(1) do |i|
+ issuer, cert = chain[i - 1, 2]
+ v = cert.check_validity(issuer, time)
+ raise exc, "%s: cert = '%s', error = '%s'" % [
+ 'Invalid Signing Chain', cert.subject, v[:desc]
+ ] unless v[:is_valid]
+ end
+
+ # verify root of chain
+ if @verify_root
+ # make sure root is self-signed
+ root = chain[0]
+ raise exc, "%s: %s (subject = '%s', issuer = '%s')" % [
+ 'Invalid Signing Chain Root',
+ 'Subject does not match Issuer for Gem Signing Chain',
+ root.subject.to_s,
+ root.issuer.to_s,
+ ] unless root.issuer.to_s == root.subject.to_s
+
+ # make sure root is valid
+ v = root.check_validity(root, time)
+ raise exc, "%s: cert = '%s', error = '%s'" % [
+ 'Invalid Signing Chain Root', root.subject, v[:desc]
+ ] unless v[:is_valid]
+
+ # verify that the chain root is trusted
+ if @only_trusted
+ # get digest algorithm, calculate checksum of root.subject
+ algo = @opt[:dgst_algo]
+ path = Gem::Security::Policy.trusted_cert_path(root, @opt)
+
+ # check to make sure trusted path exists
+ raise exc, "%s: cert = '%s', error = '%s'" % [
+ 'Untrusted Signing Chain Root',
+ root.subject.to_s,
+ "path \"#{path}\" does not exist",
+ ] unless File.exist?(path)
+
+ # load calculate digest from saved cert file
+ save_cert = OpenSSL::X509::Certificate.new(File.read(path))
+ save_dgst = algo.digest(save_cert.public_key.to_s)
+
+ # create digest of public key
+ pkey_str = root.public_key.to_s
+ cert_dgst = algo.digest(pkey_str)
+
+ # now compare the two digests, raise exception
+ # if they don't match
+ raise exc, "%s: %s (saved = '%s', root = '%s')" % [
+ 'Invalid Signing Chain Root',
+ "Saved checksum doesn't match root checksum",
+ save_dgst, cert_dgst,
+ ] unless save_dgst == cert_dgst
+ end
+ end
+
+ # return the signing chain
+ chain.map { |cert| cert.subject }
+ end
+ end
+ end
+
+ #
+ # No security policy: all package signature checks are disabled.
+ #
+ NoSecurity = Policy.new(
+ :verify_data => false,
+ :verify_signer => false,
+ :verify_chain => false,
+ :verify_root => false,
+ :only_trusted => false,
+ :only_signed => false
+ )
+
+ #
+ # AlmostNo security policy: only verify that the signing certificate is the
+ # one that actually signed the data. Make no attempt to verify the signing
+ # certificate chain.
+ #
+ # This policy is basically useless. better than nothing, but can still be
+ # easily spoofed, and is not recommended.
+ #
+ AlmostNoSecurity = Policy.new(
+ :verify_data => true,
+ :verify_signer => false,
+ :verify_chain => false,
+ :verify_root => false,
+ :only_trusted => false,
+ :only_signed => false
+ )
+
+ #
+ # Low security policy: only verify that the signing certificate is actually
+ # the gem signer, and that the signing certificate is valid.
+ #
+ # This policy is better than nothing, but can still be easily spoofed, and
+ # is not recommended.
+ #
+ LowSecurity = Policy.new(
+ :verify_data => true,
+ :verify_signer => true,
+ :verify_chain => false,
+ :verify_root => false,
+ :only_trusted => false,
+ :only_signed => false
+ )
+
+ #
+ # Medium security policy: verify the signing certificate, verify the signing
+ # certificate chain all the way to the root certificate, and only trust root
+ # certificates that we have explicitly allowed trust for.
+ #
+ # This security policy is reasonable, but it allows unsigned packages, so a
+ # malicious person could simply delete the package signature and pass the
+ # gem off as unsigned.
+ #
+ MediumSecurity = Policy.new(
+ :verify_data => true,
+ :verify_signer => true,
+ :verify_chain => true,
+ :verify_root => true,
+ :only_trusted => true,
+ :only_signed => false
+ )
+
+ #
+ # High security policy: only allow signed gems to be installed, verify the
+ # signing certificate, verify the signing certificate chain all the way to
+ # the root certificate, and only trust root certificates that we have
+ # explicitly allowed trust for.
+ #
+ # This security policy is significantly more difficult to bypass, and offers
+ # a reasonable guarantee that the contents of the gem have not been altered.
+ #
+ HighSecurity = Policy.new(
+ :verify_data => true,
+ :verify_signer => true,
+ :verify_chain => true,
+ :verify_root => true,
+ :only_trusted => true,
+ :only_signed => true
+ )
+
+ #
+ # Hash of configured security policies
+ #
+ Policies = {
+ 'NoSecurity' => NoSecurity,
+ 'AlmostNoSecurity' => AlmostNoSecurity,
+ 'LowSecurity' => LowSecurity,
+ 'MediumSecurity' => MediumSecurity,
+ 'HighSecurity' => HighSecurity,
+ }
+
+ #
+ # Sign the cert cert with @signing_key and @signing_cert, using the digest
+ # algorithm opt[:dgst_algo]. Returns the newly signed certificate.
+ #
+ def self.sign_cert(cert, signing_key, signing_cert, opt = {})
+ opt = OPT.merge(opt)
+
+ # set up issuer information
+ cert.issuer = signing_cert.subject
+ cert.sign(signing_key, opt[:dgst_algo].new)
+
+ cert
+ end
+
+ #
+ # Make sure the trust directory exists. If it does exist, make sure it's
+ # actually a directory. If not, then create it with the appropriate
+ # permissions.
+ #
+ def self.verify_trust_dir(path, perms)
+ # if the directory exists, then make sure it is in fact a directory. if
+ # it doesn't exist, then create it with the appropriate permissions
+ if File.exist?(path)
+ # verify that the trust directory is actually a directory
+ unless File.directory?(path)
+ err = "trust directory #{path} isn't a directory"
+ raise Gem::Security::Exception, err
+ end
+ else
+ # trust directory doesn't exist, so create it with permissions
+ FileUtils.mkdir_p(path)
+ FileUtils.chmod(perms, path)
+ end
+ end
+
+ #
+ # Build a certificate from the given DN and private key.
+ #
+ def self.build_cert(name, key, opt = {})
+ Gem.ensure_ssl_available
+ opt = OPT.merge(opt)
+
+ # create new cert
+ ret = OpenSSL::X509::Certificate.new
+
+ # populate cert attributes
+ ret.version = 2
+ ret.serial = 0
+ ret.public_key = key.public_key
+ ret.not_before = Time.now
+ ret.not_after = Time.now + opt[:cert_age]
+ ret.subject = name
+
+ # add certificate extensions
+ ef = OpenSSL::X509::ExtensionFactory.new(nil, ret)
+ ret.extensions = opt[:cert_exts].map { |k, v| ef.create_extension(k, v) }
+
+ # sign cert
+ i_key, i_cert = opt[:issuer_key] || key, opt[:issuer_cert] || ret
+ ret = sign_cert(ret, i_key, i_cert, opt)
+
+ # return cert
+ ret
+ end
+
+ #
+ # Build a self-signed certificate for the given email address.
+ #
+ def self.build_self_signed_cert(email_addr, opt = {})
+ Gem.ensure_ssl_available
+ opt = OPT.merge(opt)
+ path = { :key => nil, :cert => nil }
+
+ # split email address up
+ cn, dcs = email_addr.split('@')
+ dcs = dcs.split('.')
+
+ # munge email CN and DCs
+ cn = cn.gsub(opt[:munge_re], '_')
+ dcs = dcs.map { |dc| dc.gsub(opt[:munge_re], '_') }
+
+ # create DN
+ name = "CN=#{cn}/" << dcs.map { |dc| "DC=#{dc}" }.join('/')
+ name = OpenSSL::X509::Name::parse(name)
+
+ # build private key
+ key = opt[:key_algo].new(opt[:key_size])
+
+ # method name pretty much says it all :)
+ verify_trust_dir(opt[:trust_dir], opt[:perms][:trust_dir])
+
+ # if we're saving the key, then write it out
+ if opt[:save_key]
+ path[:key] = opt[:save_key_path] || (opt[:output_fmt] % 'private_key')
+ File.open(path[:key], 'wb') do |file|
+ file.chmod(opt[:perms][:signing_key])
+ file.write(key.to_pem)
+ end
+ end
+
+ # build self-signed public cert from key
+ cert = build_cert(name, key, opt)
+
+ # if we're saving the cert, then write it out
+ if opt[:save_cert]
+ path[:cert] = opt[:save_cert_path] || (opt[:output_fmt] % 'public_cert')
+ File.open(path[:cert], 'wb') do |file|
+ file.chmod(opt[:perms][:signing_cert])
+ file.write(cert.to_pem)
+ end
+ end
+
+ # return key, cert, and paths (if applicable)
+ { :key => key, :cert => cert,
+ :key_path => path[:key], :cert_path => path[:cert] }
+ end
+
+ #
+ # Add certificate to trusted cert list.
+ #
+ # Note: At the moment these are stored in OPT[:trust_dir], although that
+ # directory may change in the future.
+ #
+ def self.add_trusted_cert(cert, opt = {})
+ opt = OPT.merge(opt)
+
+ # get destination path
+ path = Gem::Security::Policy.trusted_cert_path(cert, opt)
+
+ # verify trust directory (can't write to nowhere, you know)
+ verify_trust_dir(opt[:trust_dir], opt[:perms][:trust_dir])
+
+ # write cert to output file
+ File.open(path, 'wb') do |file|
+ file.chmod(opt[:perms][:trusted_cert])
+ file.write(cert.to_pem)
+ end
+
+ # return nil
+ nil
+ end
+
+ #
+ # Basic OpenSSL-based package signing class.
+ #
+ class Signer
+ attr_accessor :key, :cert_chain
+
+ def initialize(key, cert_chain)
+ Gem.ensure_ssl_available
+ @algo = Gem::Security::OPT[:dgst_algo]
+ @key, @cert_chain = key, cert_chain
+
+ # check key, if it's a file, and if it's key, leave it alone
+ if @key && !@key.kind_of?(OpenSSL::PKey::PKey)
+ @key = OpenSSL::PKey::RSA.new(File.read(@key))
+ end
+
+ # check cert chain, if it's a file, load it, if it's cert data, convert
+ # it into a cert object, and if it's a cert object, leave it alone
+ if @cert_chain
+ @cert_chain = @cert_chain.map do |cert|
+ # check cert, if it's a file, load it, if it's cert data, convert it
+ # into a cert object, and if it's a cert object, leave it alone
+ if cert && !cert.kind_of?(OpenSSL::X509::Certificate)
+ cert = File.read(cert) if File::exist?(cert)
+ cert = OpenSSL::X509::Certificate.new(cert)
+ end
+ cert
+ end
+ end
+ end
+
+ #
+ # Sign data with given digest algorithm
+ #
+ def sign(data)
+ @key.sign(@algo.new, data)
+ end
+
+ end
+end
+
diff --git a/ruby/lib/rubygems/server.rb b/ruby/lib/rubygems/server.rb
new file mode 100644
index 0000000..2c617ff
--- /dev/null
+++ b/ruby/lib/rubygems/server.rb
@@ -0,0 +1,629 @@
+require 'webrick'
+require 'yaml'
+require 'zlib'
+require 'erb'
+
+require 'rubygems'
+require 'rubygems/doc_manager'
+
+##
+# Gem::Server and allows users to serve gems for consumption by
+# `gem --remote-install`.
+#
+# gem_server starts an HTTP server on the given port and serves the following:
+# * "/" - Browsing of gem spec files for installed gems
+# * "/specs.#{Gem.marshal_version}.gz" - specs name/version/platform index
+# * "/latest_specs.#{Gem.marshal_version}.gz" - latest specs
+# name/version/platform index
+# * "/quick/" - Individual gemspecs
+# * "/gems" - Direct access to download the installable gems
+# * legacy indexes:
+# * "/Marshal.#{Gem.marshal_version}" - Full SourceIndex dump of metadata
+# for installed gems
+# * "/yaml" - YAML dump of metadata for installed gems - deprecated
+#
+# == Usage
+#
+# gem_server = Gem::Server.new Gem.dir, 8089, false
+# gem_server.run
+#
+#--
+# TODO Refactor into a real WEBrick servlet to remove code duplication.
+
+class Gem::Server
+
+ include Gem::UserInteraction
+
+ DOC_TEMPLATE = <<-'WEBPAGE'
+ <?xml version="1.0" encoding="iso-8859-1"?>
+ <!DOCTYPE html
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+ <head>
+ <title>RubyGems Documentation Index</title>
+ <link rel="stylesheet" href="gem-server-rdoc-style.css" type="text/css" media="screen" />
+ </head>
+ <body>
+ <div id="fileHeader">
+ <h1>RubyGems Documentation Index</h1>
+ </div>
+ <!-- banner header -->
+
+ <div id="bodyContent">
+ <div id="contextContent">
+ <div id="description">
+ <h1>Summary</h1>
+ <p>There are <%=values["gem_count"]%> gems installed:</p>
+ <p>
+ <%= values["specs"].map { |v| "<a href=\"##{v["name"]}\">#{v["name"]}</a>" }.join ', ' %>.
+ <h1>Gems</h1>
+
+ <dl>
+ <% values["specs"].each do |spec| %>
+ <dt>
+ <% if spec["first_name_entry"] then %>
+ <a name="<%=spec["name"]%>"></a>
+ <% end %>
+
+ <b><%=spec["name"]%> <%=spec["version"]%></b>
+
+ <% if spec["rdoc_installed"] then %>
+ <a href="<%=spec["doc_path"]%>">[rdoc]</a>
+ <% else %>
+ <span title="rdoc not installed">[rdoc]</span>
+ <% end %>
+
+ <% if spec["homepage"] then %>
+ <a href="<%=spec["homepage"]%>" title="<%=spec["homepage"]%>">[www]</a>
+ <% else %>
+ <span title="no homepage available">[www]</span>
+ <% end %>
+
+ <% if spec["has_deps"] then %>
+ - depends on
+ <%= spec["dependencies"].map { |v| "<a href=\"##{v["name"]}\">#{v["name"]}</a>" }.join ', ' %>.
+ <% end %>
+ </dt>
+ <dd>
+ <%=spec["summary"]%>
+ <% if spec["executables"] then %>
+ <br/>
+
+ <% if spec["only_one_executable"] then %>
+ Executable is
+ <% else %>
+ Executables are
+ <%end%>
+
+ <%= spec["executables"].map { |v| "<span class=\"context-item-name\">#{v["executable"]}</span>"}.join ', ' %>.
+
+ <%end%>
+ <br/>
+ <br/>
+ </dd>
+ <% end %>
+ </dl>
+
+ </div>
+ </div>
+ </div>
+ <div id="validator-badges">
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
+ </div>
+ </body>
+ </html>
+ WEBPAGE
+
+ # CSS is copy & paste from rdoc-style.css, RDoc V1.0.1 - 20041108
+ RDOC_CSS = <<-RDOCCSS
+body {
+ font-family: Verdana,Arial,Helvetica,sans-serif;
+ font-size: 90%;
+ margin: 0;
+ margin-left: 40px;
+ padding: 0;
+ background: white;
+}
+
+h1,h2,h3,h4 { margin: 0; color: #efefef; background: transparent; }
+h1 { font-size: 150%; }
+h2,h3,h4 { margin-top: 1em; }
+
+a { background: #eef; color: #039; text-decoration: none; }
+a:hover { background: #039; color: #eef; }
+
+/* Override the base stylesheets Anchor inside a table cell */
+td > a {
+ background: transparent;
+ color: #039;
+ text-decoration: none;
+}
+
+/* and inside a section title */
+.section-title > a {
+ background: transparent;
+ color: #eee;
+ text-decoration: none;
+}
+
+/* === Structural elements =================================== */
+
+div#index {
+ margin: 0;
+ margin-left: -40px;
+ padding: 0;
+ font-size: 90%;
+}
+
+
+div#index a {
+ margin-left: 0.7em;
+}
+
+div#index .section-bar {
+ margin-left: 0px;
+ padding-left: 0.7em;
+ background: #ccc;
+ font-size: small;
+}
+
+
+div#classHeader, div#fileHeader {
+ width: auto;
+ color: white;
+ padding: 0.5em 1.5em 0.5em 1.5em;
+ margin: 0;
+ margin-left: -40px;
+ border-bottom: 3px solid #006;
+}
+
+div#classHeader a, div#fileHeader a {
+ background: inherit;
+ color: white;
+}
+
+div#classHeader td, div#fileHeader td {
+ background: inherit;
+ color: white;
+}
+
+
+div#fileHeader {
+ background: #057;
+}
+
+div#classHeader {
+ background: #048;
+}
+
+
+.class-name-in-header {
+ font-size: 180%;
+ font-weight: bold;
+}
+
+
+div#bodyContent {
+ padding: 0 1.5em 0 1.5em;
+}
+
+div#description {
+ padding: 0.5em 1.5em;
+ background: #efefef;
+ border: 1px dotted #999;
+}
+
+div#description h1,h2,h3,h4,h5,h6 {
+ color: #125;;
+ background: transparent;
+}
+
+div#validator-badges {
+ text-align: center;
+}
+div#validator-badges img { border: 0; }
+
+div#copyright {
+ color: #333;
+ background: #efefef;
+ font: 0.75em sans-serif;
+ margin-top: 5em;
+ margin-bottom: 0;
+ padding: 0.5em 2em;
+}
+
+
+/* === Classes =================================== */
+
+table.header-table {
+ color: white;
+ font-size: small;
+}
+
+.type-note {
+ font-size: small;
+ color: #DEDEDE;
+}
+
+.xxsection-bar {
+ background: #eee;
+ color: #333;
+ padding: 3px;
+}
+
+.section-bar {
+ color: #333;
+ border-bottom: 1px solid #999;
+ margin-left: -20px;
+}
+
+
+.section-title {
+ background: #79a;
+ color: #eee;
+ padding: 3px;
+ margin-top: 2em;
+ margin-left: -30px;
+ border: 1px solid #999;
+}
+
+.top-aligned-row { vertical-align: top }
+.bottom-aligned-row { vertical-align: bottom }
+
+/* --- Context section classes ----------------------- */
+
+.context-row { }
+.context-item-name { font-family: monospace; font-weight: bold; color: black; }
+.context-item-value { font-size: small; color: #448; }
+.context-item-desc { color: #333; padding-left: 2em; }
+
+/* --- Method classes -------------------------- */
+.method-detail {
+ background: #efefef;
+ padding: 0;
+ margin-top: 0.5em;
+ margin-bottom: 1em;
+ border: 1px dotted #ccc;
+}
+.method-heading {
+ color: black;
+ background: #ccc;
+ border-bottom: 1px solid #666;
+ padding: 0.2em 0.5em 0 0.5em;
+}
+.method-signature { color: black; background: inherit; }
+.method-name { font-weight: bold; }
+.method-args { font-style: italic; }
+.method-description { padding: 0 0.5em 0 0.5em; }
+
+/* --- Source code sections -------------------- */
+
+a.source-toggle { font-size: 90%; }
+div.method-source-code {
+ background: #262626;
+ color: #ffdead;
+ margin: 1em;
+ padding: 0.5em;
+ border: 1px dashed #999;
+ overflow: hidden;
+}
+
+div.method-source-code pre { color: #ffdead; overflow: hidden; }
+
+/* --- Ruby keyword styles --------------------- */
+
+.standalone-code { background: #221111; color: #ffdead; overflow: hidden; }
+
+.ruby-constant { color: #7fffd4; background: transparent; }
+.ruby-keyword { color: #00ffff; background: transparent; }
+.ruby-ivar { color: #eedd82; background: transparent; }
+.ruby-operator { color: #00ffee; background: transparent; }
+.ruby-identifier { color: #ffdead; background: transparent; }
+.ruby-node { color: #ffa07a; background: transparent; }
+.ruby-comment { color: #b22222; font-weight: bold; background: transparent; }
+.ruby-regexp { color: #ffa07a; background: transparent; }
+.ruby-value { color: #7fffd4; background: transparent; }
+ RDOCCSS
+
+ def self.run(options)
+ new(options[:gemdir], options[:port], options[:daemon]).run
+ end
+
+ def initialize(gem_dir, port, daemon)
+ Socket.do_not_reverse_lookup = true
+
+ @gem_dir = gem_dir
+ @port = port
+ @daemon = daemon
+ logger = WEBrick::Log.new nil, WEBrick::BasicLog::FATAL
+ @server = WEBrick::HTTPServer.new :DoNotListen => true, :Logger => logger
+
+ @spec_dir = File.join @gem_dir, 'specifications'
+
+ unless File.directory? @spec_dir then
+ raise ArgumentError, "#{@gem_dir} does not appear to be a gem repository"
+ end
+
+ @source_index = Gem::SourceIndex.from_gems_in @spec_dir
+ end
+
+ def Marshal(req, res)
+ @source_index.refresh!
+
+ res['date'] = File.stat(@spec_dir).mtime
+
+ index = Marshal.dump @source_index
+
+ if req.request_method == 'HEAD' then
+ res['content-length'] = index.length
+ return
+ end
+
+ if req.path =~ /Z$/ then
+ res['content-type'] = 'application/x-deflate'
+ index = Gem.deflate index
+ else
+ res['content-type'] = 'application/octet-stream'
+ end
+
+ res.body << index
+ end
+
+ def latest_specs(req, res)
+ @source_index.refresh!
+
+ res['content-type'] = 'application/x-gzip'
+
+ res['date'] = File.stat(@spec_dir).mtime
+
+ specs = @source_index.latest_specs.sort.map do |spec|
+ platform = spec.original_platform
+ platform = Gem::Platform::RUBY if platform.nil?
+ [spec.name, spec.version, platform]
+ end
+
+ specs = Marshal.dump specs
+
+ if req.path =~ /\.gz$/ then
+ specs = Gem.gzip specs
+ res['content-type'] = 'application/x-gzip'
+ else
+ res['content-type'] = 'application/octet-stream'
+ end
+
+ if req.request_method == 'HEAD' then
+ res['content-length'] = specs.length
+ else
+ res.body << specs
+ end
+ end
+
+ def quick(req, res)
+ @source_index.refresh!
+
+ res['content-type'] = 'text/plain'
+ res['date'] = File.stat(@spec_dir).mtime
+
+ case req.request_uri.path
+ when '/quick/index' then
+ res.body << @source_index.map { |name,| name }.sort.join("\n")
+ when '/quick/index.rz' then
+ index = @source_index.map { |name,| name }.sort.join("\n")
+ res['content-type'] = 'application/x-deflate'
+ res.body << Gem.deflate(index)
+ when '/quick/latest_index' then
+ index = @source_index.latest_specs.map { |spec| spec.full_name }
+ res.body << index.sort.join("\n")
+ when '/quick/latest_index.rz' then
+ index = @source_index.latest_specs.map { |spec| spec.full_name }
+ res['content-type'] = 'application/x-deflate'
+ res.body << Gem.deflate(index.sort.join("\n"))
+ when %r|^/quick/(Marshal.#{Regexp.escape Gem.marshal_version}/)?(.*?)-([0-9.]+)(-.*?)?\.gemspec\.rz$| then
+ dep = Gem::Dependency.new $2, $3
+ specs = @source_index.search dep
+ marshal_format = $1
+
+ selector = [$2, $3, $4].map { |s| s.inspect }.join ' '
+
+ platform = if $4 then
+ Gem::Platform.new $4.sub(/^-/, '')
+ else
+ Gem::Platform::RUBY
+ end
+
+ specs = specs.select { |s| s.platform == platform }
+
+ if specs.empty? then
+ res.status = 404
+ res.body = "No gems found matching #{selector}"
+ elsif specs.length > 1 then
+ res.status = 500
+ res.body = "Multiple gems found matching #{selector}"
+ elsif marshal_format then
+ res['content-type'] = 'application/x-deflate'
+ res.body << Gem.deflate(Marshal.dump(specs.first))
+ else # deprecated YAML format
+ res['content-type'] = 'application/x-deflate'
+ res.body << Gem.deflate(specs.first.to_yaml)
+ end
+ else
+ raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found."
+ end
+ end
+
+ def root(req, res)
+ @source_index.refresh!
+ res['date'] = File.stat(@spec_dir).mtime
+
+ raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." unless
+ req.path == '/'
+
+ specs = []
+ total_file_count = 0
+
+ @source_index.each do |path, spec|
+ total_file_count += spec.files.size
+ deps = spec.dependencies.map do |dep|
+ { "name" => dep.name,
+ "type" => dep.type,
+ "version" => dep.version_requirements.to_s, }
+ end
+
+ deps = deps.sort_by { |dep| [dep["name"].downcase, dep["version"]] }
+ deps.last["is_last"] = true unless deps.empty?
+
+ # executables
+ executables = spec.executables.sort.collect { |exec| {"executable" => exec} }
+ executables = nil if executables.empty?
+ executables.last["is_last"] = true if executables
+
+ specs << {
+ "authors" => spec.authors.sort.join(", "),
+ "date" => spec.date.to_s,
+ "dependencies" => deps,
+ "doc_path" => "/doc_root/#{spec.full_name}/rdoc/index.html",
+ "executables" => executables,
+ "only_one_executable" => (executables && executables.size == 1),
+ "full_name" => spec.full_name,
+ "has_deps" => !deps.empty?,
+ "homepage" => spec.homepage,
+ "name" => spec.name,
+ "rdoc_installed" => Gem::DocManager.new(spec).rdoc_installed?,
+ "summary" => spec.summary,
+ "version" => spec.version.to_s,
+ }
+ end
+
+ specs << {
+ "authors" => "Chad Fowler, Rich Kilmer, Jim Weirich, Eric Hodel and others",
+ "dependencies" => [],
+ "doc_path" => "/doc_root/rubygems-#{Gem::RubyGemsVersion}/rdoc/index.html",
+ "executables" => [{"executable" => 'gem', "is_last" => true}],
+ "only_one_executable" => true,
+ "full_name" => "rubygems-#{Gem::RubyGemsVersion}",
+ "has_deps" => false,
+ "homepage" => "http://rubygems.org/",
+ "name" => 'rubygems',
+ "rdoc_installed" => true,
+ "summary" => "RubyGems itself",
+ "version" => Gem::RubyGemsVersion,
+ }
+
+ specs = specs.sort_by { |spec| [spec["name"].downcase, spec["version"]] }
+ specs.last["is_last"] = true
+
+ # tag all specs with first_name_entry
+ last_spec = nil
+ specs.each do |spec|
+ is_first = last_spec.nil? || (last_spec["name"].downcase != spec["name"].downcase)
+ spec["first_name_entry"] = is_first
+ last_spec = spec
+ end
+
+ # create page from template
+ template = ERB.new(DOC_TEMPLATE)
+ res['content-type'] = 'text/html'
+
+ values = { "gem_count" => specs.size.to_s, "specs" => specs,
+ "total_file_count" => total_file_count.to_s }
+
+ result = template.result binding
+ res.body = result
+ end
+
+ def run
+ @server.listen nil, @port
+
+ say "Starting gem server on http://localhost:#{@port}/"
+
+ WEBrick::Daemon.start if @daemon
+
+ @server.mount_proc "/yaml", method(:yaml)
+ @server.mount_proc "/yaml.Z", method(:yaml)
+
+ @server.mount_proc "/Marshal.#{Gem.marshal_version}", method(:Marshal)
+ @server.mount_proc "/Marshal.#{Gem.marshal_version}.Z", method(:Marshal)
+
+ @server.mount_proc "/specs.#{Gem.marshal_version}", method(:specs)
+ @server.mount_proc "/specs.#{Gem.marshal_version}.gz", method(:specs)
+
+ @server.mount_proc "/latest_specs.#{Gem.marshal_version}",
+ method(:latest_specs)
+ @server.mount_proc "/latest_specs.#{Gem.marshal_version}.gz",
+ method(:latest_specs)
+
+ @server.mount_proc "/quick/", method(:quick)
+
+ @server.mount_proc("/gem-server-rdoc-style.css") do |req, res|
+ res['content-type'] = 'text/css'
+ res['date'] = File.stat(@spec_dir).mtime
+ res.body << RDOC_CSS
+ end
+
+ @server.mount_proc "/", method(:root)
+
+ paths = { "/gems" => "/cache/", "/doc_root" => "/doc/" }
+ paths.each do |mount_point, mount_dir|
+ @server.mount(mount_point, WEBrick::HTTPServlet::FileHandler,
+ File.join(@gem_dir, mount_dir), true)
+ end
+
+ trap("INT") { @server.shutdown; exit! }
+ trap("TERM") { @server.shutdown; exit! }
+
+ @server.start
+ end
+
+ def specs(req, res)
+ @source_index.refresh!
+
+ res['date'] = File.stat(@spec_dir).mtime
+
+ specs = @source_index.sort.map do |_, spec|
+ platform = spec.original_platform
+ platform = Gem::Platform::RUBY if platform.nil?
+ [spec.name, spec.version, platform]
+ end
+
+ specs = Marshal.dump specs
+
+ if req.path =~ /\.gz$/ then
+ specs = Gem.gzip specs
+ res['content-type'] = 'application/x-gzip'
+ else
+ res['content-type'] = 'application/octet-stream'
+ end
+
+ if req.request_method == 'HEAD' then
+ res['content-length'] = specs.length
+ else
+ res.body << specs
+ end
+ end
+
+ def yaml(req, res)
+ @source_index.refresh!
+
+ res['date'] = File.stat(@spec_dir).mtime
+
+ index = @source_index.to_yaml
+
+ if req.path =~ /Z$/ then
+ res['content-type'] = 'application/x-deflate'
+ index = Gem.deflate index
+ else
+ res['content-type'] = 'text/plain'
+ end
+
+ if req.request_method == 'HEAD' then
+ res['content-length'] = index.length
+ return
+ end
+
+ res.body << index
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/source_index.rb b/ruby/lib/rubygems/source_index.rb
new file mode 100644
index 0000000..8a8db2e
--- /dev/null
+++ b/ruby/lib/rubygems/source_index.rb
@@ -0,0 +1,559 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+require 'rubygems/user_interaction'
+require 'rubygems/specification'
+module Gem
+ autoload(:SpecFetcher, 'rubygems/spec_fetcher')
+end
+
+##
+# The SourceIndex object indexes all the gems available from a
+# particular source (e.g. a list of gem directories, or a remote
+# source). A SourceIndex maps a gem full name to a gem
+# specification.
+#
+# NOTE:: The class used to be named Cache, but that became
+# confusing when cached source fetchers where introduced. The
+# constant Gem::Cache is an alias for this class to allow old
+# YAMLized source index objects to load properly.
+
+class Gem::SourceIndex
+
+ include Enumerable
+
+ include Gem::UserInteraction
+
+ attr_reader :gems # :nodoc:
+
+ ##
+ # Directories to use to refresh this SourceIndex when calling refresh!
+
+ attr_accessor :spec_dirs
+
+ class << self
+ include Gem::UserInteraction
+
+ ##
+ # Factory method to construct a source index instance for a given
+ # path.
+ #
+ # deprecated::
+ # If supplied, from_installed_gems will act just like
+ # +from_gems_in+. This argument is deprecated and is provided
+ # just for backwards compatibility, and should not generally
+ # be used.
+ #
+ # return::
+ # SourceIndex instance
+
+ def from_installed_gems(*deprecated)
+ if deprecated.empty?
+ from_gems_in(*installed_spec_directories)
+ else
+ from_gems_in(*deprecated) # HACK warn
+ end
+ end
+
+ ##
+ # Returns a list of directories from Gem.path that contain specifications.
+
+ def installed_spec_directories
+ Gem.path.collect { |dir| File.join(dir, "specifications") }
+ end
+
+ ##
+ # Creates a new SourceIndex from the ruby format gem specifications in
+ # +spec_dirs+.
+
+ def from_gems_in(*spec_dirs)
+ source_index = new
+ source_index.spec_dirs = spec_dirs
+ source_index.refresh!
+ end
+
+ ##
+ # Loads a ruby-format specification from +file_name+ and returns the
+ # loaded spec.
+
+ def load_specification(file_name)
+ begin
+ spec_code = if RUBY_VERSION < '1.9' then
+ File.read file_name
+ else
+ File.read file_name, :encoding => 'UTF-8'
+ end.untaint
+
+ gemspec = eval spec_code, binding, file_name
+
+ if gemspec.is_a?(Gem::Specification)
+ gemspec.loaded_from = file_name
+ return gemspec
+ end
+ alert_warning "File '#{file_name}' does not evaluate to a gem specification"
+ rescue SignalException, SystemExit
+ raise
+ rescue SyntaxError => e
+ alert_warning e
+ alert_warning spec_code
+ rescue Exception => e
+ alert_warning "#{e.inspect}\n#{spec_code}"
+ alert_warning "Invalid .gemspec format in '#{file_name}'"
+ end
+ return nil
+ end
+
+ end
+
+ ##
+ # Constructs a source index instance from the provided
+ # specifications
+ #
+ # specifications::
+ # [Hash] hash of [Gem name, Gem::Specification] pairs
+
+ def initialize(specifications={})
+ @gems = specifications
+ @spec_dirs = nil
+ end
+
+ ##
+ # Reconstruct the source index from the specifications in +spec_dirs+.
+
+ def load_gems_in(*spec_dirs)
+ @gems.clear
+
+ spec_dirs.reverse_each do |spec_dir|
+ spec_files = Dir.glob File.join(spec_dir, '*.gemspec')
+
+ spec_files.each do |spec_file|
+ gemspec = self.class.load_specification spec_file.untaint
+ add_spec gemspec if gemspec
+ end
+ end
+
+ self
+ end
+
+ ##
+ # Returns an Array specifications for the latest versions of each gem in
+ # this index.
+
+ def latest_specs
+ result = Hash.new { |h,k| h[k] = [] }
+ latest = {}
+
+ sort.each do |_, spec|
+ name = spec.name
+ curr_ver = spec.version
+ prev_ver = latest.key?(name) ? latest[name].version : nil
+
+ next unless prev_ver.nil? or curr_ver >= prev_ver or
+ latest[name].platform != Gem::Platform::RUBY
+
+ if prev_ver.nil? or
+ (curr_ver > prev_ver and spec.platform == Gem::Platform::RUBY) then
+ result[name].clear
+ latest[name] = spec
+ end
+
+ if spec.platform != Gem::Platform::RUBY then
+ result[name].delete_if do |result_spec|
+ result_spec.platform == spec.platform
+ end
+ end
+
+ result[name] << spec
+ end
+
+ result.values.flatten
+ end
+
+ ##
+ # Add a gem specification to the source index.
+
+ def add_spec(gem_spec)
+ @gems[gem_spec.full_name] = gem_spec
+ end
+
+ ##
+ # Add gem specifications to the source index.
+
+ def add_specs(*gem_specs)
+ gem_specs.each do |spec|
+ add_spec spec
+ end
+ end
+
+ ##
+ # Remove a gem specification named +full_name+.
+
+ def remove_spec(full_name)
+ @gems.delete(full_name)
+ end
+
+ ##
+ # Iterate over the specifications in the source index.
+
+ def each(&block) # :yields: gem.full_name, gem
+ @gems.each(&block)
+ end
+
+ ##
+ # The gem specification given a full gem spec name.
+
+ def specification(full_name)
+ @gems[full_name]
+ end
+
+ ##
+ # The signature for the source index. Changes in the signature indicate a
+ # change in the index.
+
+ def index_signature
+ require 'rubygems/digest/sha2'
+
+ Gem::SHA256.new.hexdigest(@gems.keys.sort.join(',')).to_s
+ end
+
+ ##
+ # The signature for the given gem specification.
+
+ def gem_signature(gem_full_name)
+ require 'rubygems/digest/sha2'
+
+ Gem::SHA256.new.hexdigest(@gems[gem_full_name].to_yaml).to_s
+ end
+
+ def size
+ @gems.size
+ end
+ alias length size
+
+ ##
+ # Find a gem by an exact match on the short name.
+
+ def find_name(gem_name, version_requirement = Gem::Requirement.default)
+ dep = Gem::Dependency.new(/^#{gem_name}$/, version_requirement)
+ search dep
+ end
+
+ ##
+ # Search for a gem by Gem::Dependency +gem_pattern+. If +only_platform+
+ # is true, only gems matching Gem::Platform.local will be returned. An
+ # Array of matching Gem::Specification objects is returned.
+ #
+ # For backwards compatibility, a String or Regexp pattern may be passed as
+ # +gem_pattern+, and a Gem::Requirement for +platform_only+. This
+ # behavior is deprecated and will be removed.
+
+ def search(gem_pattern, platform_only = false)
+ version_requirement = nil
+ only_platform = false
+
+ # TODO - Remove support and warning for legacy arguments after 2008/11
+ unless Gem::Dependency === gem_pattern
+ warn "#{Gem.location_of_caller.join ':'}:Warning: Gem::SourceIndex#search support for #{gem_pattern.class} patterns is deprecated"
+ end
+
+ case gem_pattern
+ when Regexp then
+ version_requirement = platform_only || Gem::Requirement.default
+ when Gem::Dependency then
+ only_platform = platform_only
+ version_requirement = gem_pattern.version_requirements
+ gem_pattern = if Regexp === gem_pattern.name then
+ gem_pattern.name
+ elsif gem_pattern.name.empty? then
+ //
+ else
+ /^#{Regexp.escape gem_pattern.name}$/
+ end
+ else
+ version_requirement = platform_only || Gem::Requirement.default
+ gem_pattern = /#{gem_pattern}/i
+ end
+
+ unless Gem::Requirement === version_requirement then
+ version_requirement = Gem::Requirement.create version_requirement
+ end
+
+ specs = @gems.values.select do |spec|
+ spec.name =~ gem_pattern and
+ version_requirement.satisfied_by? spec.version
+ end
+
+ if only_platform then
+ specs = specs.select do |spec|
+ Gem::Platform.match spec.platform
+ end
+ end
+
+ specs.sort_by { |s| s.sort_obj }
+ end
+
+ ##
+ # Replaces the gems in the source index from specifications in the
+ # directories this source index was created from. Raises an exception if
+ # this source index wasn't created from a directory (via from_gems_in or
+ # from_installed_gems, or having spec_dirs set).
+
+ def refresh!
+ raise 'source index not created from disk' if @spec_dirs.nil?
+ load_gems_in(*@spec_dirs)
+ end
+
+ ##
+ # Returns an Array of Gem::Specifications that are not up to date.
+
+ def outdated
+ outdateds = []
+
+ latest_specs.each do |local|
+ dependency = Gem::Dependency.new local.name, ">= #{local.version}"
+
+ begin
+ fetcher = Gem::SpecFetcher.fetcher
+ remotes = fetcher.find_matching dependency
+ remotes = remotes.map { |(name, version,_),_| version }
+ rescue Gem::RemoteFetcher::FetchError => e
+ raise unless fetcher.warn_legacy e do
+ require 'rubygems/source_info_cache'
+
+ specs = Gem::SourceInfoCache.search_with_source dependency, true
+
+ remotes = specs.map { |spec,| spec.version }
+ end
+ end
+
+ latest = remotes.sort.last
+
+ outdateds << local.name if latest and local.version < latest
+ end
+
+ outdateds
+ end
+
+ ##
+ # Updates this SourceIndex from +source_uri+. If +all+ is false, only the
+ # latest gems are fetched.
+
+ def update(source_uri, all)
+ source_uri = URI.parse source_uri unless URI::Generic === source_uri
+ source_uri.path += '/' unless source_uri.path =~ /\/$/
+
+ use_incremental = false
+
+ begin
+ gem_names = fetch_quick_index source_uri, all
+ remove_extra gem_names
+ missing_gems = find_missing gem_names
+
+ return false if missing_gems.size.zero?
+
+ say "Missing metadata for #{missing_gems.size} gems" if
+ missing_gems.size > 0 and Gem.configuration.really_verbose
+
+ use_incremental = missing_gems.size <= Gem.configuration.bulk_threshold
+ rescue Gem::OperationNotSupportedError => ex
+ alert_error "Falling back to bulk fetch: #{ex.message}" if
+ Gem.configuration.really_verbose
+ use_incremental = false
+ end
+
+ if use_incremental then
+ update_with_missing(source_uri, missing_gems)
+ else
+ new_index = fetch_bulk_index(source_uri)
+ @gems.replace(new_index.gems)
+ end
+
+ true
+ end
+
+ def ==(other) # :nodoc:
+ self.class === other and @gems == other.gems
+ end
+
+ def dump
+ Marshal.dump(self)
+ end
+
+ private
+
+ def fetcher
+ require 'rubygems/remote_fetcher'
+
+ Gem::RemoteFetcher.fetcher
+ end
+
+ def fetch_index_from(source_uri)
+ @fetch_error = nil
+
+ indexes = %W[
+ Marshal.#{Gem.marshal_version}.Z
+ Marshal.#{Gem.marshal_version}
+ yaml.Z
+ yaml
+ ]
+
+ indexes.each do |name|
+ spec_data = nil
+ index = source_uri + name
+ begin
+ spec_data = fetcher.fetch_path index
+ spec_data = unzip(spec_data) if name =~ /\.Z$/
+
+ if name =~ /Marshal/ then
+ return Marshal.load(spec_data)
+ else
+ return YAML.load(spec_data)
+ end
+ rescue => e
+ if Gem.configuration.really_verbose then
+ alert_error "Unable to fetch #{name}: #{e.message}"
+ end
+
+ @fetch_error = e
+ end
+ end
+
+ nil
+ end
+
+ def fetch_bulk_index(source_uri)
+ say "Bulk updating Gem source index for: #{source_uri}" if
+ Gem.configuration.verbose
+
+ index = fetch_index_from(source_uri)
+ if index.nil? then
+ raise Gem::RemoteSourceException,
+ "Error fetching remote gem cache: #{@fetch_error}"
+ end
+ @fetch_error = nil
+ index
+ end
+
+ ##
+ # Get the quick index needed for incremental updates.
+
+ def fetch_quick_index(source_uri, all)
+ index = all ? 'index' : 'latest_index'
+
+ zipped_index = fetcher.fetch_path source_uri + "quick/#{index}.rz"
+
+ unzip(zipped_index).split("\n")
+ rescue ::Exception => e
+ unless all then
+ say "Latest index not found, using quick index" if
+ Gem.configuration.really_verbose
+
+ fetch_quick_index source_uri, true
+ else
+ raise Gem::OperationNotSupportedError,
+ "No quick index found: #{e.message}"
+ end
+ end
+
+ ##
+ # Make a list of full names for all the missing gemspecs.
+
+ def find_missing(spec_names)
+ unless defined? @originals then
+ @originals = {}
+ each do |full_name, spec|
+ @originals[spec.original_name] = spec
+ end
+ end
+
+ spec_names.find_all { |full_name|
+ @originals[full_name].nil?
+ }
+ end
+
+ def remove_extra(spec_names)
+ dictionary = spec_names.inject({}) { |h, k| h[k] = true; h }
+ each do |name, spec|
+ remove_spec name unless dictionary.include? spec.original_name
+ end
+ end
+
+ ##
+ # Unzip the given string.
+
+ def unzip(string)
+ require 'zlib'
+ Gem.inflate string
+ end
+
+ ##
+ # Tries to fetch Marshal representation first, then YAML
+
+ def fetch_single_spec(source_uri, spec_name)
+ @fetch_error = nil
+
+ begin
+ marshal_uri = source_uri + "quick/Marshal.#{Gem.marshal_version}/#{spec_name}.gemspec.rz"
+ zipped = fetcher.fetch_path marshal_uri
+ return Marshal.load(unzip(zipped))
+ rescue => ex
+ @fetch_error = ex
+
+ if Gem.configuration.really_verbose then
+ say "unable to fetch marshal gemspec #{marshal_uri}: #{ex.class} - #{ex}"
+ end
+ end
+
+ begin
+ yaml_uri = source_uri + "quick/#{spec_name}.gemspec.rz"
+ zipped = fetcher.fetch_path yaml_uri
+ return YAML.load(unzip(zipped))
+ rescue => ex
+ @fetch_error = ex
+ if Gem.configuration.really_verbose then
+ say "unable to fetch YAML gemspec #{yaml_uri}: #{ex.class} - #{ex}"
+ end
+ end
+
+ nil
+ end
+
+ ##
+ # Update the cached source index with the missing names.
+
+ def update_with_missing(source_uri, missing_names)
+ progress = ui.progress_reporter(missing_names.size,
+ "Updating metadata for #{missing_names.size} gems from #{source_uri}")
+ missing_names.each do |spec_name|
+ gemspec = fetch_single_spec(source_uri, spec_name)
+ if gemspec.nil? then
+ ui.say "Failed to download spec #{spec_name} from #{source_uri}:\n" \
+ "\t#{@fetch_error.message}"
+ else
+ add_spec gemspec
+ progress.updated spec_name
+ end
+ @fetch_error = nil
+ end
+ progress.done
+ progress.count
+ end
+
+end
+
+module Gem
+
+ # :stopdoc:
+
+ # Cache is an alias for SourceIndex to allow older YAMLized source index
+ # objects to load properly.
+ Cache = SourceIndex
+
+ # :startdoc:
+
+end
+
diff --git a/ruby/lib/rubygems/source_info_cache.rb b/ruby/lib/rubygems/source_info_cache.rb
new file mode 100644
index 0000000..fdb30ad
--- /dev/null
+++ b/ruby/lib/rubygems/source_info_cache.rb
@@ -0,0 +1,393 @@
+require 'fileutils'
+
+require 'rubygems'
+require 'rubygems/source_info_cache_entry'
+require 'rubygems/user_interaction'
+
+##
+# SourceInfoCache stores a copy of the gem index for each gem source.
+#
+# There are two possible cache locations, the system cache and the user cache:
+# * The system cache is preferred if it is writable or can be created.
+# * The user cache is used otherwise
+#
+# Once a cache is selected, it will be used for all operations.
+# SourceInfoCache will not switch between cache files dynamically.
+#
+# Cache data is a Hash mapping a source URI to a SourceInfoCacheEntry.
+#
+#--
+# To keep things straight, this is how the cache objects all fit together:
+#
+# Gem::SourceInfoCache
+# @cache_data = {
+# source_uri => Gem::SourceInfoCacheEntry
+# @size = source index size
+# @source_index = Gem::SourceIndex
+# ...
+# }
+
+class Gem::SourceInfoCache
+
+ include Gem::UserInteraction
+
+ ##
+ # The singleton Gem::SourceInfoCache. If +all+ is true, a full refresh will
+ # be performed if the singleton instance is being initialized.
+
+ def self.cache(all = false)
+ return @cache if @cache
+ @cache = new
+ @cache.refresh all if Gem.configuration.update_sources
+ @cache
+ end
+
+ def self.cache_data
+ cache.cache_data
+ end
+
+ ##
+ # The name of the system cache file.
+
+ def self.latest_system_cache_file
+ File.join File.dirname(system_cache_file),
+ "latest_#{File.basename system_cache_file}"
+ end
+
+ ##
+ # The name of the latest user cache file.
+
+ def self.latest_user_cache_file
+ File.join File.dirname(user_cache_file),
+ "latest_#{File.basename user_cache_file}"
+ end
+
+ ##
+ # Reset all singletons, discarding any changes.
+
+ def self.reset
+ @cache = nil
+ @system_cache_file = nil
+ @user_cache_file = nil
+ end
+
+ ##
+ # Search all source indexes. See Gem::SourceInfoCache#search.
+
+ def self.search(*args)
+ cache.search(*args)
+ end
+
+ ##
+ # Search all source indexes returning the source_uri. See
+ # Gem::SourceInfoCache#search_with_source.
+
+ def self.search_with_source(*args)
+ cache.search_with_source(*args)
+ end
+
+ ##
+ # The name of the system cache file. (class method)
+
+ def self.system_cache_file
+ @system_cache_file ||= Gem.default_system_source_cache_dir
+ end
+
+ ##
+ # The name of the user cache file.
+
+ def self.user_cache_file
+ @user_cache_file ||=
+ ENV['GEMCACHE'] || Gem.default_user_source_cache_dir
+ end
+
+ def initialize # :nodoc:
+ @cache_data = nil
+ @cache_file = nil
+ @dirty = false
+ @only_latest = true
+ end
+
+ ##
+ # The most recent cache data.
+
+ def cache_data
+ return @cache_data if @cache_data
+ cache_file # HACK writable check
+
+ @only_latest = true
+
+ @cache_data = read_cache_data latest_cache_file
+
+ @cache_data
+ end
+
+ ##
+ # The name of the cache file.
+
+ def cache_file
+ return @cache_file if @cache_file
+ @cache_file = (try_file(system_cache_file) or
+ try_file(user_cache_file) or
+ raise "unable to locate a writable cache file")
+ end
+
+ ##
+ # Write the cache to a local file (if it is dirty).
+
+ def flush
+ write_cache if @dirty
+ @dirty = false
+ end
+
+ def latest_cache_data
+ latest_cache_data = {}
+
+ cache_data.each do |repo, sice|
+ latest = sice.source_index.latest_specs
+
+ new_si = Gem::SourceIndex.new
+ new_si.add_specs(*latest)
+
+ latest_sice = Gem::SourceInfoCacheEntry.new new_si, sice.size
+ latest_cache_data[repo] = latest_sice
+ end
+
+ latest_cache_data
+ end
+
+ ##
+ # The name of the latest cache file.
+
+ def latest_cache_file
+ File.join File.dirname(cache_file), "latest_#{File.basename cache_file}"
+ end
+
+ ##
+ # The name of the latest system cache file.
+
+ def latest_system_cache_file
+ self.class.latest_system_cache_file
+ end
+
+ ##
+ # The name of the latest user cache file.
+
+ def latest_user_cache_file
+ self.class.latest_user_cache_file
+ end
+
+ ##
+ # Merges the complete cache file into this Gem::SourceInfoCache.
+
+ def read_all_cache_data
+ if @only_latest then
+ @only_latest = false
+ all_data = read_cache_data cache_file
+
+ cache_data.update all_data do |source_uri, latest_sice, all_sice|
+ all_sice.source_index.gems.update latest_sice.source_index.gems
+
+ Gem::SourceInfoCacheEntry.new all_sice.source_index, latest_sice.size
+ end
+
+ begin
+ refresh true
+ rescue Gem::RemoteFetcher::FetchError
+ end
+ end
+ end
+
+ ##
+ # Reads cached data from +file+.
+
+ def read_cache_data(file)
+ # Marshal loads 30-40% faster from a String, and 2MB on 20061116 is small
+ data = open file, 'rb' do |fp| fp.read end
+ cache_data = Marshal.load data
+
+ cache_data.each do |url, sice|
+ next unless sice.is_a?(Hash)
+ update
+
+ cache = sice['cache']
+ size = sice['size']
+
+ if cache.is_a?(Gem::SourceIndex) and size.is_a?(Numeric) then
+ new_sice = Gem::SourceInfoCacheEntry.new cache, size
+ cache_data[url] = new_sice
+ else # irreperable, force refetch.
+ reset_cache_for url, cache_data
+ end
+ end
+
+ cache_data
+ rescue Errno::ENOENT
+ {}
+ rescue => e
+ if Gem.configuration.really_verbose then
+ say "Exception during cache_data handling: #{e.class} - #{e}"
+ say "Cache file was: #{file}"
+ say "\t#{e.backtrace.join "\n\t"}"
+ end
+
+ {}
+ end
+
+ ##
+ # Refreshes each source in the cache from its repository. If +all+ is
+ # false, only latest gems are updated.
+
+ def refresh(all)
+ Gem.sources.each do |source_uri|
+ cache_entry = cache_data[source_uri]
+ if cache_entry.nil? then
+ cache_entry = Gem::SourceInfoCacheEntry.new nil, 0
+ cache_data[source_uri] = cache_entry
+ end
+
+ update if cache_entry.refresh source_uri, all
+ end
+
+ flush
+ end
+
+ def reset_cache_for(url, cache_data)
+ say "Reseting cache for #{url}" if Gem.configuration.really_verbose
+
+ sice = Gem::SourceInfoCacheEntry.new Gem::SourceIndex.new, 0
+ sice.refresh url, false # HACK may be unnecessary, see ::cache and #refresh
+
+ cache_data[url] = sice
+ cache_data
+ end
+
+ def reset_cache_data
+ @cache_data = nil
+ @only_latest = true
+ end
+
+ ##
+ # Force cache file to be reset, useful for integration testing of rubygems
+
+ def reset_cache_file
+ @cache_file = nil
+ end
+
+ ##
+ # Searches all source indexes. See Gem::SourceIndex#search for details on
+ # +pattern+ and +platform_only+. If +all+ is set to true, the full index
+ # will be loaded before searching.
+
+ def search(pattern, platform_only = false, all = false)
+ read_all_cache_data if all
+
+ cache_data.map do |source_uri, sic_entry|
+ next unless Gem.sources.include? source_uri
+ # TODO - Remove this gunk after 2008/11
+ unless pattern.kind_of?(Gem::Dependency)
+ pattern = Gem::Dependency.new(pattern, Gem::Requirement.default)
+ end
+ sic_entry.source_index.search pattern, platform_only
+ end.flatten.compact
+ end
+
+ # Searches all source indexes for +pattern+. If +only_platform+ is true,
+ # only gems matching Gem.platforms will be selected. Returns an Array of
+ # pairs containing the Gem::Specification found and the source_uri it was
+ # found at.
+ def search_with_source(pattern, only_platform = false, all = false)
+ read_all_cache_data if all
+
+ results = []
+
+ cache_data.map do |source_uri, sic_entry|
+ next unless Gem.sources.include? source_uri
+
+ # TODO - Remove this gunk after 2008/11
+ unless pattern.kind_of?(Gem::Dependency)
+ pattern = Gem::Dependency.new(pattern, Gem::Requirement.default)
+ end
+
+ sic_entry.source_index.search(pattern, only_platform).each do |spec|
+ results << [spec, source_uri]
+ end
+ end
+
+ results
+ end
+
+ ##
+ # Set the source info cache data directly. This is mainly used for unit
+ # testing when we don't want to read a file system to grab the cached source
+ # index information. The +hash+ should map a source URL into a
+ # SourceInfoCacheEntry.
+
+ def set_cache_data(hash)
+ @cache_data = hash
+ update
+ end
+
+ ##
+ # The name of the system cache file.
+
+ def system_cache_file
+ self.class.system_cache_file
+ end
+
+ ##
+ # Determine if +path+ is a candidate for a cache file. Returns +path+ if
+ # it is, nil if not.
+
+ def try_file(path)
+ return path if File.writable? path
+ return nil if File.exist? path
+
+ dir = File.dirname path
+
+ unless File.exist? dir then
+ begin
+ FileUtils.mkdir_p dir
+ rescue RuntimeError, SystemCallError
+ return nil
+ end
+ end
+
+ return path if File.writable? dir
+
+ nil
+ end
+
+ ##
+ # Mark the cache as updated (i.e. dirty).
+
+ def update
+ @dirty = true
+ end
+
+ ##
+ # The name of the user cache file.
+
+ def user_cache_file
+ self.class.user_cache_file
+ end
+
+ ##
+ # Write data to the proper cache files.
+
+ def write_cache
+ if not File.exist?(cache_file) or not @only_latest then
+ open cache_file, 'wb' do |io|
+ io.write Marshal.dump(cache_data)
+ end
+ end
+
+ open latest_cache_file, 'wb' do |io|
+ io.write Marshal.dump(latest_cache_data)
+ end
+ end
+
+ reset
+
+end
+
diff --git a/ruby/lib/rubygems/source_info_cache_entry.rb b/ruby/lib/rubygems/source_info_cache_entry.rb
new file mode 100644
index 0000000..c3f75e5
--- /dev/null
+++ b/ruby/lib/rubygems/source_info_cache_entry.rb
@@ -0,0 +1,56 @@
+require 'rubygems'
+require 'rubygems/source_index'
+require 'rubygems/remote_fetcher'
+
+##
+# Entries held by a SourceInfoCache.
+
+class Gem::SourceInfoCacheEntry
+
+ ##
+ # The source index for this cache entry.
+
+ attr_reader :source_index
+
+ ##
+ # The size of the of the source entry. Used to determine if the
+ # source index has changed.
+
+ attr_reader :size
+
+ ##
+ # Create a cache entry.
+
+ def initialize(si, size)
+ @source_index = si || Gem::SourceIndex.new({})
+ @size = size
+ @all = false
+ end
+
+ def refresh(source_uri, all)
+ begin
+ marshal_uri = URI.join source_uri.to_s, "Marshal.#{Gem.marshal_version}"
+ remote_size = Gem::RemoteFetcher.fetcher.fetch_size marshal_uri
+ rescue Gem::RemoteSourceException
+ yaml_uri = URI.join source_uri.to_s, 'yaml'
+ remote_size = Gem::RemoteFetcher.fetcher.fetch_size yaml_uri
+ end
+
+ # TODO Use index_signature instead of size?
+ return false if @size == remote_size and @all
+
+ updated = @source_index.update source_uri, all
+ @size = remote_size
+ @all = all
+
+ updated
+ end
+
+ def ==(other) # :nodoc:
+ self.class === other and
+ @size == other.size and
+ @source_index == other.source_index
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/spec_fetcher.rb b/ruby/lib/rubygems/spec_fetcher.rb
new file mode 100644
index 0000000..a1fc82e
--- /dev/null
+++ b/ruby/lib/rubygems/spec_fetcher.rb
@@ -0,0 +1,249 @@
+require 'zlib'
+
+require 'rubygems'
+require 'rubygems/remote_fetcher'
+require 'rubygems/user_interaction'
+
+##
+# SpecFetcher handles metadata updates from remote gem repositories.
+
+class Gem::SpecFetcher
+
+ include Gem::UserInteraction
+
+ ##
+ # The SpecFetcher cache dir.
+
+ attr_reader :dir # :nodoc:
+
+ ##
+ # Cache of latest specs
+
+ attr_reader :latest_specs # :nodoc:
+
+ ##
+ # Cache of all spces
+
+ attr_reader :specs # :nodoc:
+
+ @fetcher = nil
+
+ def self.fetcher
+ @fetcher ||= new
+ end
+
+ def self.fetcher=(fetcher) # :nodoc:
+ @fetcher = fetcher
+ end
+
+ def initialize
+ @dir = File.join Gem.user_home, '.gem', 'specs'
+ @update_cache = File.stat(Gem.user_home).uid == Process.uid
+
+ @specs = {}
+ @latest_specs = {}
+
+ @fetcher = Gem::RemoteFetcher.fetcher
+ end
+
+ ##
+ # Retuns the local directory to write +uri+ to.
+
+ def cache_dir(uri)
+ File.join @dir, "#{uri.host}%#{uri.port}", File.dirname(uri.path)
+ end
+
+ ##
+ # Fetch specs matching +dependency+. If +all+ is true, all matching
+ # versions are returned. If +matching_platform+ is false, all platforms are
+ # returned.
+
+ def fetch(dependency, all = false, matching_platform = true)
+ specs_and_sources = find_matching dependency, all, matching_platform
+
+ specs_and_sources.map do |spec_tuple, source_uri|
+ [fetch_spec(spec_tuple, URI.parse(source_uri)), source_uri]
+ end
+
+ rescue Gem::RemoteFetcher::FetchError => e
+ raise unless warn_legacy e do
+ require 'rubygems/source_info_cache'
+
+ return Gem::SourceInfoCache.search_with_source(dependency,
+ matching_platform, all)
+ end
+ end
+
+ def fetch_spec(spec, source_uri)
+ spec = spec - [nil, 'ruby', '']
+ spec_file_name = "#{spec.join '-'}.gemspec"
+
+ uri = source_uri + "#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}"
+
+ cache_dir = cache_dir uri
+
+ local_spec = File.join cache_dir, spec_file_name
+
+ if File.exist? local_spec then
+ spec = Gem.read_binary local_spec
+ else
+ uri.path << '.rz'
+
+ spec = @fetcher.fetch_path uri
+ spec = Gem.inflate spec
+
+ if @update_cache then
+ FileUtils.mkdir_p cache_dir
+
+ open local_spec, 'wb' do |io|
+ io.write spec
+ end
+ end
+ end
+
+ # TODO: Investigate setting Gem::Specification#loaded_from to a URI
+ Marshal.load spec
+ end
+
+ ##
+ # Find spec names that match +dependency+. If +all+ is true, all matching
+ # versions are returned. If +matching_platform+ is false, gems for all
+ # platforms are returned.
+
+ def find_matching(dependency, all = false, matching_platform = true)
+ found = {}
+
+ list(all).each do |source_uri, specs|
+ found[source_uri] = specs.select do |spec_name, version, spec_platform|
+ dependency =~ Gem::Dependency.new(spec_name, version) and
+ (not matching_platform or Gem::Platform.match(spec_platform))
+ end
+ end
+
+ specs_and_sources = []
+
+ found.each do |source_uri, specs|
+ uri_str = source_uri.to_s
+ specs_and_sources.push(*specs.map { |spec| [spec, uri_str] })
+ end
+
+ specs_and_sources
+ end
+
+ ##
+ # Returns Array of gem repositories that were generated with RubyGems less
+ # than 1.2.
+
+ def legacy_repos
+ Gem.sources.reject do |source_uri|
+ source_uri = URI.parse source_uri
+ spec_path = source_uri + "specs.#{Gem.marshal_version}.gz"
+
+ begin
+ @fetcher.fetch_size spec_path
+ rescue Gem::RemoteFetcher::FetchError
+ begin
+ @fetcher.fetch_size(source_uri + 'yaml') # re-raise if non-repo
+ rescue Gem::RemoteFetcher::FetchError
+ alert_error "#{source_uri} does not appear to be a repository"
+ raise
+ end
+ false
+ end
+ end
+ end
+
+ ##
+ # Returns a list of gems available for each source in Gem::sources. If
+ # +all+ is true, all versions are returned instead of only latest versions.
+
+ def list(all = false)
+ list = {}
+
+ file = all ? 'specs' : 'latest_specs'
+
+ Gem.sources.each do |source_uri|
+ source_uri = URI.parse source_uri
+
+ if all and @specs.include? source_uri then
+ list[source_uri] = @specs[source_uri]
+ elsif not all and @latest_specs.include? source_uri then
+ list[source_uri] = @latest_specs[source_uri]
+ else
+ specs = load_specs source_uri, file
+
+ cache = all ? @specs : @latest_specs
+
+ cache[source_uri] = specs
+ list[source_uri] = specs
+ end
+ end
+
+ list
+ end
+
+ ##
+ # Loads specs in +file+, fetching from +source_uri+ if the on-disk cache is
+ # out of date.
+
+ def load_specs(source_uri, file)
+ file_name = "#{file}.#{Gem.marshal_version}"
+ spec_path = source_uri + "#{file_name}.gz"
+ cache_dir = cache_dir spec_path
+ local_file = File.join(cache_dir, file_name)
+ loaded = false
+
+ if File.exist? local_file then
+ spec_dump = @fetcher.fetch_path spec_path, File.mtime(local_file)
+
+ if spec_dump.nil? then
+ spec_dump = Gem.read_binary local_file
+ else
+ loaded = true
+ end
+ else
+ spec_dump = @fetcher.fetch_path spec_path
+ loaded = true
+ end
+
+ specs = Marshal.load spec_dump
+
+ if loaded and @update_cache then
+ begin
+ FileUtils.mkdir_p cache_dir
+
+ open local_file, 'wb' do |io|
+ Marshal.dump specs, io
+ end
+ rescue
+ end
+ end
+
+ specs
+ end
+
+ ##
+ # Warn about legacy repositories if +exception+ indicates only legacy
+ # repositories are available, and yield to the block. Returns false if the
+ # exception indicates some other FetchError.
+
+ def warn_legacy(exception)
+ uri = exception.uri.to_s
+ if uri =~ /specs\.#{Regexp.escape Gem.marshal_version}\.gz$/ then
+ alert_warning <<-EOF
+RubyGems 1.2+ index not found for:
+\t#{legacy_repos.join "\n\t"}
+
+RubyGems will revert to legacy indexes degrading performance.
+ EOF
+
+ yield
+
+ return true
+ end
+
+ false
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/specification.rb b/ruby/lib/rubygems/specification.rb
new file mode 100644
index 0000000..b3a42cf
--- /dev/null
+++ b/ruby/lib/rubygems/specification.rb
@@ -0,0 +1,1264 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+require 'rubygems/version'
+require 'rubygems/requirement'
+require 'rubygems/platform'
+
+# :stopdoc:
+# Time::today has been deprecated in 0.9.5 and will be removed.
+if RUBY_VERSION < '1.9' then
+ def Time.today
+ t = Time.now
+ t - ((t.to_f + t.gmt_offset) % 86400)
+ end unless defined? Time.today
+end
+
+class Date; end # for ruby_code if date.rb wasn't required
+
+# :startdoc:
+
+module Gem
+
+ ##
+ # == Gem::Specification
+ #
+ # The Specification class contains the metadata for a Gem. Typically
+ # defined in a .gemspec file or a Rakefile, and looks like this:
+ #
+ # spec = Gem::Specification.new do |s|
+ # s.name = 'rfoo'
+ # s.version = '1.0'
+ # s.summary = 'Example gem specification'
+ # ...
+ # end
+ #
+ # There are many <em>gemspec attributes</em>, and the best place to learn
+ # about them in the "Gemspec Reference" linked from the RubyGems wiki.
+
+ class Specification
+
+ ##
+ # Allows deinstallation of gems with legacy platforms.
+
+ attr_accessor :original_platform # :nodoc:
+
+ ##
+ # The the version number of a specification that does not specify one
+ # (i.e. RubyGems 0.7 or earlier).
+
+ NONEXISTENT_SPECIFICATION_VERSION = -1
+
+ ##
+ # The specification version applied to any new Specification instances
+ # created. This should be bumped whenever something in the spec format
+ # changes.
+ #--
+ # When updating this number, be sure to also update #to_ruby.
+ #
+ # NOTE RubyGems < 1.2 cannot load specification versions > 2.
+
+ CURRENT_SPECIFICATION_VERSION = 2
+
+ ##
+ # An informal list of changes to the specification. The highest-valued
+ # key should be equal to the CURRENT_SPECIFICATION_VERSION.
+
+ SPECIFICATION_VERSION_HISTORY = {
+ -1 => ['(RubyGems versions up to and including 0.7 did not have versioned specifications)'],
+ 1 => [
+ 'Deprecated "test_suite_file" in favor of the new, but equivalent, "test_files"',
+ '"test_file=x" is a shortcut for "test_files=[x]"'
+ ],
+ 2 => [
+ 'Added "required_rubygems_version"',
+ 'Now forward-compatible with future versions',
+ ],
+ }
+
+ # :stopdoc:
+ MARSHAL_FIELDS = { -1 => 16, 1 => 16, 2 => 16 }
+
+ now = Time.at(Time.now.to_i)
+ TODAY = now - ((now.to_i + now.gmt_offset) % 86400)
+ # :startdoc:
+
+ ##
+ # List of Specification instances.
+
+ @@list = []
+
+ ##
+ # Optional block used to gather newly defined instances.
+
+ @@gather = nil
+
+ ##
+ # List of attribute names: [:name, :version, ...]
+ @@required_attributes = []
+
+ ##
+ # List of _all_ attributes and default values:
+ #
+ # [[:name, nil],
+ # [:bindir, 'bin'],
+ # ...]
+
+ @@attributes = []
+
+ @@nil_attributes = []
+ @@non_nil_attributes = [:@original_platform]
+
+ ##
+ # List of array attributes
+
+ @@array_attributes = []
+
+ ##
+ # Map of attribute names to default values.
+
+ @@default_value = {}
+
+ ##
+ # Names of all specification attributes
+
+ def self.attribute_names
+ @@attributes.map { |name, default| name }
+ end
+
+ ##
+ # Default values for specification attributes
+
+ def self.attribute_defaults
+ @@attributes.dup
+ end
+
+ ##
+ # The default value for specification attribute +name+
+
+ def self.default_value(name)
+ @@default_value[name]
+ end
+
+ ##
+ # Required specification attributes
+
+ def self.required_attributes
+ @@required_attributes.dup
+ end
+
+ ##
+ # Is +name+ a required attribute?
+
+ def self.required_attribute?(name)
+ @@required_attributes.include? name.to_sym
+ end
+
+ ##
+ # Specification attributes that are arrays (appendable and so-forth)
+
+ def self.array_attributes
+ @@array_attributes.dup
+ end
+
+ ##
+ # A list of Specification instances that have been defined in this Ruby
+ # instance.
+
+ def self.list
+ @@list
+ end
+
+ ##
+ # Specifies the +name+ and +default+ for a specification attribute, and
+ # creates a reader and writer method like Module#attr_accessor.
+ #
+ # The reader method returns the default if the value hasn't been set.
+
+ def self.attribute(name, default=nil)
+ ivar_name = "@#{name}".intern
+ if default.nil? then
+ @@nil_attributes << ivar_name
+ else
+ @@non_nil_attributes << [ivar_name, default]
+ end
+
+ @@attributes << [name, default]
+ @@default_value[name] = default
+ attr_accessor(name)
+ end
+
+ ##
+ # Same as :attribute, but ensures that values assigned to the attribute
+ # are array values by applying :to_a to the value.
+
+ def self.array_attribute(name)
+ @@non_nil_attributes << ["@#{name}".intern, []]
+
+ @@array_attributes << name
+ @@attributes << [name, []]
+ @@default_value[name] = []
+ code = %{
+ def #{name}
+ @#{name} ||= []
+ end
+ def #{name}=(value)
+ @#{name} = Array(value)
+ end
+ }
+
+ module_eval code, __FILE__, __LINE__ - 9
+ end
+
+ ##
+ # Same as attribute above, but also records this attribute as mandatory.
+
+ def self.required_attribute(*args)
+ @@required_attributes << args.first
+ attribute(*args)
+ end
+
+ ##
+ # Sometimes we don't want the world to use a setter method for a
+ # particular attribute.
+ #
+ # +read_only+ makes it private so we can still use it internally.
+
+ def self.read_only(*names)
+ names.each do |name|
+ private "#{name}="
+ end
+ end
+
+ # Shortcut for creating several attributes at once (each with a default
+ # value of +nil+).
+
+ def self.attributes(*args)
+ args.each do |arg|
+ attribute(arg, nil)
+ end
+ end
+
+ ##
+ # Some attributes require special behaviour when they are accessed. This
+ # allows for that.
+
+ def self.overwrite_accessor(name, &block)
+ remove_method name
+ define_method(name, &block)
+ end
+
+ ##
+ # Defines a _singular_ version of an existing _plural_ attribute (i.e. one
+ # whose value is expected to be an array). This means just creating a
+ # helper method that takes a single value and appends it to the array.
+ # These are created for convenience, so that in a spec, one can write
+ #
+ # s.require_path = 'mylib'
+ #
+ # instead of:
+ #
+ # s.require_paths = ['mylib']
+ #
+ # That above convenience is available courtesy of:
+ #
+ # attribute_alias_singular :require_path, :require_paths
+
+ def self.attribute_alias_singular(singular, plural)
+ define_method("#{singular}=") { |val|
+ send("#{plural}=", [val])
+ }
+ define_method("#{singular}") {
+ val = send("#{plural}")
+ val.nil? ? nil : val.first
+ }
+ end
+
+ ##
+ # Dump only crucial instance variables.
+ #--
+ # MAINTAIN ORDER!
+
+ def _dump(limit)
+ Marshal.dump [
+ @rubygems_version,
+ @specification_version,
+ @name,
+ @version,
+ (Time === @date ? @date : (require 'time'; Time.parse(@date.to_s))),
+ @summary,
+ @required_ruby_version,
+ @required_rubygems_version,
+ @original_platform,
+ @dependencies,
+ @rubyforge_project,
+ @email,
+ @authors,
+ @description,
+ @homepage,
+ @has_rdoc,
+ @new_platform,
+ ]
+ end
+
+ ##
+ # Load custom marshal format, re-initializing defaults as needed
+
+ def self._load(str)
+ array = Marshal.load str
+
+ spec = Gem::Specification.new
+ spec.instance_variable_set :@specification_version, array[1]
+
+ current_version = CURRENT_SPECIFICATION_VERSION
+
+ field_count = if spec.specification_version > current_version then
+ spec.instance_variable_set :@specification_version,
+ current_version
+ MARSHAL_FIELDS[current_version]
+ else
+ MARSHAL_FIELDS[spec.specification_version]
+ end
+
+ if array.size < field_count then
+ raise TypeError, "invalid Gem::Specification format #{array.inspect}"
+ end
+
+ spec.instance_variable_set :@rubygems_version, array[0]
+ # spec version
+ spec.instance_variable_set :@name, array[2]
+ spec.instance_variable_set :@version, array[3]
+ spec.instance_variable_set :@date, array[4]
+ spec.instance_variable_set :@summary, array[5]
+ spec.instance_variable_set :@required_ruby_version, array[6]
+ spec.instance_variable_set :@required_rubygems_version, array[7]
+ spec.instance_variable_set :@original_platform, array[8]
+ spec.instance_variable_set :@dependencies, array[9]
+ spec.instance_variable_set :@rubyforge_project, array[10]
+ spec.instance_variable_set :@email, array[11]
+ spec.instance_variable_set :@authors, array[12]
+ spec.instance_variable_set :@description, array[13]
+ spec.instance_variable_set :@homepage, array[14]
+ spec.instance_variable_set :@has_rdoc, array[15]
+ spec.instance_variable_set :@new_platform, array[16]
+ spec.instance_variable_set :@platform, array[16].to_s
+ spec.instance_variable_set :@loaded, false
+
+ spec
+ end
+
+ ##
+ # List of depedencies that will automatically be activated at runtime.
+
+ def runtime_dependencies
+ dependencies.select { |d| d.type == :runtime || d.type == nil }
+ end
+
+ ##
+ # List of dependencies that are used for development
+
+ def development_dependencies
+ dependencies.select { |d| d.type == :development }
+ end
+
+ def test_suite_file # :nodoc:
+ warn 'test_suite_file deprecated, use test_files'
+ test_files.first
+ end
+
+ def test_suite_file=(val) # :nodoc:
+ warn 'test_suite_file= deprecated, use test_files='
+ @test_files = [] unless defined? @test_files
+ @test_files << val
+ end
+
+ ##
+ # true when this gemspec has been loaded from a specifications directory.
+ # This attribute is not persisted.
+
+ attr_accessor :loaded
+
+ ##
+ # Path this gemspec was loaded from. This attribute is not persisted.
+
+ attr_accessor :loaded_from
+
+ ##
+ # Returns an array with bindir attached to each executable in the
+ # executables list
+
+ def add_bindir(executables)
+ return nil if executables.nil?
+
+ if @bindir then
+ Array(executables).map { |e| File.join(@bindir, e) }
+ else
+ executables
+ end
+ rescue
+ return nil
+ end
+
+ ##
+ # Files in the Gem under one of the require_paths
+
+ def lib_files
+ @files.select do |file|
+ require_paths.any? do |path|
+ file.index(path) == 0
+ end
+ end
+ end
+
+ ##
+ # True if this gem was loaded from disk
+
+ alias :loaded? :loaded
+
+ ##
+ # True if this gem has files in test_files
+
+ def has_unit_tests?
+ not test_files.empty?
+ end
+
+ alias has_test_suite? has_unit_tests? # :nodoc: deprecated
+
+ ##
+ # Specification constructor. Assigns the default values to the
+ # attributes, adds this spec to the list of loaded specs (see
+ # Specification.list), and yields itself for further initialization.
+
+ def initialize
+ @new_platform = nil
+ assign_defaults
+ @loaded = false
+ @loaded_from = nil
+ @@list << self
+
+ yield self if block_given?
+
+ @@gather.call(self) if @@gather
+ end
+
+ ##
+ # Each attribute has a default value (possibly nil). Here, we initialize
+ # all attributes to their default value. This is done through the
+ # accessor methods, so special behaviours will be honored. Furthermore,
+ # we take a _copy_ of the default so each specification instance has its
+ # own empty arrays, etc.
+
+ def assign_defaults
+ @@nil_attributes.each do |name|
+ instance_variable_set name, nil
+ end
+
+ @@non_nil_attributes.each do |name, default|
+ value = case default
+ when Time, Numeric, Symbol, true, false, nil then default
+ else default.dup
+ end
+
+ instance_variable_set name, value
+ end
+
+ # HACK
+ instance_variable_set :@new_platform, Gem::Platform::RUBY
+ end
+
+ ##
+ # Special loader for YAML files. When a Specification object is loaded
+ # from a YAML file, it bypasses the normal Ruby object initialization
+ # routine (#initialize). This method makes up for that and deals with
+ # gems of different ages.
+ #
+ # 'input' can be anything that YAML.load() accepts: String or IO.
+
+ def self.from_yaml(input)
+ input = normalize_yaml_input input
+ spec = YAML.load input
+
+ if spec && spec.class == FalseClass then
+ raise Gem::EndOfYAMLException
+ end
+
+ unless Gem::Specification === spec then
+ raise Gem::Exception, "YAML data doesn't evaluate to gem specification"
+ end
+
+ unless (spec.instance_variables.include? '@specification_version' or
+ spec.instance_variables.include? :@specification_version) and
+ spec.instance_variable_get :@specification_version
+ spec.instance_variable_set :@specification_version,
+ NONEXISTENT_SPECIFICATION_VERSION
+ end
+
+ spec
+ end
+
+ ##
+ # Loads ruby format gemspec from +filename+
+
+ def self.load(filename)
+ gemspec = nil
+ fail "NESTED Specification.load calls not allowed!" if @@gather
+ @@gather = proc { |gs| gemspec = gs }
+ data = File.read(filename)
+ eval(data)
+ gemspec
+ ensure
+ @@gather = nil
+ end
+
+ ##
+ # Make sure the YAML specification is properly formatted with dashes
+
+ def self.normalize_yaml_input(input)
+ result = input.respond_to?(:read) ? input.read : input
+ result = "--- " + result unless result =~ /^--- /
+ result
+ end
+
+ ##
+ # Sets the rubygems_version to the current RubyGems version
+
+ def mark_version
+ @rubygems_version = RubyGemsVersion
+ end
+
+ ##
+ # Ignore unknown attributes while loading
+
+ def method_missing(sym, *a, &b) # :nodoc:
+ if @specification_version > CURRENT_SPECIFICATION_VERSION and
+ sym.to_s =~ /=$/ then
+ warn "ignoring #{sym} loading #{full_name}" if $DEBUG
+ else
+ super
+ end
+ end
+
+ ##
+ # Adds a development dependency named +gem+ with +requirements+ to this
+ # Gem. For example:
+ #
+ # spec.add_development_dependency 'jabber4r', '> 0.1', '<= 0.5'
+ #
+ # Development dependencies aren't installed by default and aren't
+ # activated when a gem is required.
+
+ def add_development_dependency(gem, *requirements)
+ add_dependency_with_type(gem, :development, *requirements)
+ end
+
+ ##
+ # Adds a runtime dependency named +gem+ with +requirements+ to this Gem.
+ # For example:
+ #
+ # spec.add_runtime_dependency 'jabber4r', '> 0.1', '<= 0.5'
+
+ def add_runtime_dependency(gem, *requirements)
+ add_dependency_with_type(gem, :runtime, *requirements)
+ end
+
+ ##
+ # Adds a runtime dependency
+
+ alias add_dependency add_runtime_dependency
+
+ ##
+ # Returns the full name (name-version) of this Gem. Platform information
+ # is included (name-version-platform) if it is specified and not the
+ # default Ruby platform.
+
+ def full_name
+ if platform == Gem::Platform::RUBY or platform.nil? then
+ "#{@name}-#{@version}"
+ else
+ "#{@name}-#{@version}-#{platform}"
+ end
+ end
+
+ ##
+ # Returns the full name (name-version) of this gemspec using the original
+ # platform. For use with legacy gems.
+
+ def original_name # :nodoc:
+ if platform == Gem::Platform::RUBY or platform.nil? then
+ "#{@name}-#{@version}"
+ else
+ "#{@name}-#{@version}-#{@original_platform}"
+ end
+ end
+
+ ##
+ # The full path to the gem (install path + full name).
+
+ def full_gem_path
+ path = File.join installation_path, 'gems', full_name
+ return path if File.directory? path
+ File.join installation_path, 'gems', original_name
+ end
+
+ ##
+ # The default (generated) file name of the gem.
+
+ def file_name
+ full_name + ".gem"
+ end
+
+ ##
+ # The directory that this gem was installed into.
+
+ def installation_path
+ path = File.dirname(@loaded_from).split(File::SEPARATOR)[0..-2]
+ path = path.join File::SEPARATOR
+ File.expand_path path
+ end
+
+ ##
+ # Checks if this specification meets the requirement of +dependency+.
+
+ def satisfies_requirement?(dependency)
+ return @name == dependency.name &&
+ dependency.version_requirements.satisfied_by?(@version)
+ end
+
+ ##
+ # Returns an object you can use to sort specifications in #sort_by.
+
+ def sort_obj
+ [@name, @version.to_ints, @new_platform == Gem::Platform::RUBY ? -1 : 1]
+ end
+
+ def <=>(other) # :nodoc:
+ sort_obj <=> other.sort_obj
+ end
+
+ ##
+ # Tests specs for equality (across all attributes).
+
+ def ==(other) # :nodoc:
+ self.class === other && same_attributes?(other)
+ end
+
+ alias eql? == # :nodoc:
+
+ ##
+ # True if this gem has the same attributes as +other+.
+
+ def same_attributes?(other)
+ @@attributes.each do |name, default|
+ return false unless self.send(name) == other.send(name)
+ end
+ true
+ end
+
+ private :same_attributes?
+
+ def hash # :nodoc:
+ @@attributes.inject(0) { |hash_code, (name, default_value)|
+ n = self.send(name).hash
+ hash_code + n
+ }
+ end
+
+ def to_yaml(opts = {}) # :nodoc:
+ mark_version
+
+ attributes = @@attributes.map { |name,| name.to_s }.sort
+ attributes = attributes - %w[name version platform]
+
+ yaml = YAML.quick_emit object_id, opts do |out|
+ out.map taguri, to_yaml_style do |map|
+ map.add 'name', @name
+ map.add 'version', @version
+ platform = case @original_platform
+ when nil, '' then
+ 'ruby'
+ when String then
+ @original_platform
+ else
+ @original_platform.to_s
+ end
+ map.add 'platform', platform
+
+ attributes.each do |name|
+ map.add name, instance_variable_get("@#{name}")
+ end
+ end
+ end
+ end
+
+ def yaml_initialize(tag, vals) # :nodoc:
+ vals.each do |ivar, val|
+ instance_variable_set "@#{ivar}", val
+ end
+
+ @original_platform = @platform # for backwards compatibility
+ self.platform = Gem::Platform.new @platform
+ end
+
+ ##
+ # Returns a Ruby code representation of this specification, such that it
+ # can be eval'ed and reconstruct the same specification later. Attributes
+ # that still have their default values are omitted.
+
+ def to_ruby
+ mark_version
+ result = []
+ result << "# -*- encoding: utf-8 -*-"
+ result << nil
+ result << "Gem::Specification.new do |s|"
+
+ result << " s.name = #{ruby_code name}"
+ result << " s.version = #{ruby_code version}"
+ unless platform.nil? or platform == Gem::Platform::RUBY then
+ result << " s.platform = #{ruby_code original_platform}"
+ end
+ result << ""
+ result << " s.required_rubygems_version = #{ruby_code required_rubygems_version} if s.respond_to? :required_rubygems_version="
+
+ handled = [
+ :dependencies,
+ :name,
+ :platform,
+ :required_rubygems_version,
+ :specification_version,
+ :version,
+ ]
+
+ attributes = @@attributes.sort_by { |attr_name,| attr_name.to_s }
+
+ attributes.each do |attr_name, default|
+ next if handled.include? attr_name
+ current_value = self.send(attr_name)
+ if current_value != default or
+ self.class.required_attribute? attr_name then
+ result << " s.#{attr_name} = #{ruby_code current_value}"
+ end
+ end
+
+ result << nil
+ result << " if s.respond_to? :specification_version then"
+ result << " current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION"
+ result << " s.specification_version = #{specification_version}"
+ result << nil
+
+ result << " if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then"
+
+ unless dependencies.empty? then
+ dependencies.each do |dep|
+ version_reqs_param = dep.requirements_list.inspect
+ dep.instance_variable_set :@type, :runtime if dep.type.nil? # HACK
+ result << " s.add_#{dep.type}_dependency(%q<#{dep.name}>, #{version_reqs_param})"
+ end
+ end
+
+ result << " else"
+
+ unless dependencies.empty? then
+ dependencies.each do |dep|
+ version_reqs_param = dep.requirements_list.inspect
+ result << " s.add_dependency(%q<#{dep.name}>, #{version_reqs_param})"
+ end
+ end
+
+ result << ' end'
+
+ result << " else"
+ dependencies.each do |dep|
+ version_reqs_param = dep.requirements_list.inspect
+ result << " s.add_dependency(%q<#{dep.name}>, #{version_reqs_param})"
+ end
+ result << " end"
+
+ result << "end"
+ result << nil
+
+ result.join "\n"
+ end
+
+ ##
+ # Checks that the specification contains all required fields, and does a
+ # very basic sanity check.
+ #
+ # Raises InvalidSpecificationException if the spec does not pass the
+ # checks..
+
+ def validate
+ extend Gem::UserInteraction
+ normalize
+
+ if rubygems_version != RubyGemsVersion then
+ raise Gem::InvalidSpecificationException,
+ "expected RubyGems version #{RubyGemsVersion}, was #{rubygems_version}"
+ end
+
+ @@required_attributes.each do |symbol|
+ unless self.send symbol then
+ raise Gem::InvalidSpecificationException,
+ "missing value for attribute #{symbol}"
+ end
+ end
+
+ if require_paths.empty? then
+ raise Gem::InvalidSpecificationException,
+ "specification must have at least one require_path"
+ end
+
+ case platform
+ when Gem::Platform, Platform::RUBY then # ok
+ else
+ raise Gem::InvalidSpecificationException,
+ "invalid platform #{platform.inspect}, see Gem::Platform"
+ end
+
+ unless Array === authors and
+ authors.all? { |author| String === author } then
+ raise Gem::InvalidSpecificationException,
+ 'authors must be Array of Strings'
+ end
+
+ # Warnings
+
+ %w[author email homepage rubyforge_project summary].each do |attribute|
+ value = self.send attribute
+ alert_warning "no #{attribute} specified" if value.nil? or value.empty?
+ end
+
+ alert_warning "RDoc will not be generated (has_rdoc == false)" unless
+ has_rdoc
+
+ alert_warning "deprecated autorequire specified" if autorequire
+
+ executables.each do |executable|
+ executable_path = File.join bindir, executable
+ shebang = File.read(executable_path, 2) == '#!'
+
+ alert_warning "#{executable_path} is missing #! line" unless shebang
+ end
+
+ true
+ end
+
+ ##
+ # Normalize the list of files so that:
+ # * All file lists have redundancies removed.
+ # * Files referenced in the extra_rdoc_files are included in the package
+ # file list.
+ #
+ # Also, the summary and description are converted to a normal format.
+
+ def normalize
+ if defined?(@extra_rdoc_files) and @extra_rdoc_files then
+ @extra_rdoc_files.uniq!
+ @files ||= []
+ @files.concat(@extra_rdoc_files)
+ end
+ @files.uniq! if @files
+ end
+
+ ##
+ # Return a list of all gems that have a dependency on this gemspec. The
+ # list is structured with entries that conform to:
+ #
+ # [depending_gem, dependency, [list_of_gems_that_satisfy_dependency]]
+
+ def dependent_gems
+ out = []
+ Gem.source_index.each do |name,gem|
+ gem.dependencies.each do |dep|
+ if self.satisfies_requirement?(dep) then
+ sats = []
+ find_all_satisfiers(dep) do |sat|
+ sats << sat
+ end
+ out << [gem, dep, sats]
+ end
+ end
+ end
+ out
+ end
+
+ def to_s
+ "#<Gem::Specification name=#{@name} version=#{@version}>"
+ end
+
+ def add_dependency_with_type(dependency, type, *requirements)
+ requirements = if requirements.empty? then
+ Gem::Requirement.default
+ else
+ requirements.flatten
+ end
+
+ unless dependency.respond_to?(:name) &&
+ dependency.respond_to?(:version_requirements)
+
+ dependency = Dependency.new(dependency, requirements, type)
+ end
+
+ dependencies << dependency
+ end
+
+ private :add_dependency_with_type
+
+ def find_all_satisfiers(dep)
+ Gem.source_index.each do |name,gem|
+ if(gem.satisfies_requirement?(dep)) then
+ yield gem
+ end
+ end
+ end
+
+ private :find_all_satisfiers
+
+ ##
+ # Return a string containing a Ruby code representation of the given
+ # object.
+
+ def ruby_code(obj)
+ case obj
+ when String then '%q{' + obj + '}'
+ when Array then obj.inspect
+ when Gem::Version then obj.to_s.inspect
+ when Date then '%q{' + obj.strftime('%Y-%m-%d') + '}'
+ when Time then '%q{' + obj.strftime('%Y-%m-%d') + '}'
+ when Numeric then obj.inspect
+ when true, false, nil then obj.inspect
+ when Gem::Platform then "Gem::Platform.new(#{obj.to_a.inspect})"
+ when Gem::Requirement then "Gem::Requirement.new(#{obj.to_s.inspect})"
+ else raise Exception, "ruby_code case not handled: #{obj.class}"
+ end
+ end
+
+ private :ruby_code
+
+ # :section: Required gemspec attributes
+
+ ##
+ # The version of RubyGems used to create this gem
+
+ required_attribute :rubygems_version, Gem::RubyGemsVersion
+
+ ##
+ # The Gem::Specification version of this gemspec
+
+ required_attribute :specification_version, CURRENT_SPECIFICATION_VERSION
+
+ ##
+ # This gem's name
+
+ required_attribute :name
+
+ ##
+ # This gem's version
+
+ required_attribute :version
+
+ ##
+ # The date this gem was created
+
+ required_attribute :date, TODAY
+
+ ##
+ # A short summary of this gem's description. Displayed in `gem list -d`.
+
+ required_attribute :summary
+
+ ##
+ # Paths in the gem to add to $LOAD_PATH when this gem is activated
+
+ required_attribute :require_paths, ['lib']
+
+ # :section: Optional gemspec attributes
+
+ ##
+ # A contact email for this gem
+
+ attribute :email
+
+ ##
+ # The URL of this gem's home page
+
+ attribute :homepage
+
+ ##
+ # The rubyforge project this gem lives under. i.e. RubyGems'
+ # rubyforge_project is "rubygems".
+
+ attribute :rubyforge_project
+
+ ##
+ # A long description of this gem
+
+ attribute :description
+
+ ##
+ # Autorequire was used by old RubyGems to automatically require a file.
+ # It no longer is supported.
+
+ attribute :autorequire
+
+ ##
+ # The default executable for this gem.
+
+ attribute :default_executable
+
+ ##
+ # The path in the gem for executable scripts
+
+ attribute :bindir, 'bin'
+
+ ##
+ # True if this gem is RDoc-compliant
+
+ attribute :has_rdoc, false
+
+ ##
+ # True if this gem supports RDoc
+
+ alias :has_rdoc? :has_rdoc
+
+ ##
+ # The ruby of version required by this gem
+
+ attribute :required_ruby_version, Gem::Requirement.default
+
+ ##
+ # The RubyGems version required by this gem
+
+ attribute :required_rubygems_version, Gem::Requirement.default
+
+ ##
+ # The platform this gem runs on. See Gem::Platform for details.
+
+ attribute :platform, Gem::Platform::RUBY
+
+ ##
+ # The key used to sign this gem. See Gem::Security for details.
+
+ attribute :signing_key, nil
+
+ ##
+ # The certificate chain used to sign this gem. See Gem::Security for
+ # details.
+
+ attribute :cert_chain, []
+
+ ##
+ # A message that gets displayed after the gem is installed
+
+ attribute :post_install_message, nil
+
+ ##
+ # The list of authors who wrote this gem
+
+ array_attribute :authors
+
+ ##
+ # Files included in this gem
+
+ array_attribute :files
+
+ ##
+ # Test files included in this gem
+
+ array_attribute :test_files
+
+ ##
+ # An ARGV-style array of options to RDoc
+
+ array_attribute :rdoc_options
+
+ ##
+ # Extra files to add to RDoc
+
+ array_attribute :extra_rdoc_files
+
+ ##
+ # Executables included in the gem
+
+ array_attribute :executables
+
+ ##
+ # Extensions to build when installing the gem. See
+ # Gem::Installer#build_extensions for valid values.
+
+ array_attribute :extensions
+
+ ##
+ # An array or things required by this gem. Not used by anything
+ # presently.
+
+ array_attribute :requirements
+
+ ##
+ # A list of Gem::Dependency objects this gem depends on. Only appendable.
+
+ array_attribute :dependencies
+
+ read_only :dependencies
+
+ # :section: Aliased gemspec attributes
+
+ ##
+ # Singular accessor for executables
+
+ attribute_alias_singular :executable, :executables
+
+ ##
+ # Singular accessor for authors
+
+ attribute_alias_singular :author, :authors
+
+ ##
+ # Singular accessor for require_paths
+
+ attribute_alias_singular :require_path, :require_paths
+
+ ##
+ # Singular accessor for test_files
+
+ attribute_alias_singular :test_file, :test_files
+
+ overwrite_accessor :version= do |version|
+ @version = Version.create(version)
+ end
+
+ overwrite_accessor :platform do
+ @new_platform
+ end
+
+ overwrite_accessor :platform= do |platform|
+ if @original_platform.nil? or
+ @original_platform == Gem::Platform::RUBY then
+ @original_platform = platform
+ end
+
+ case platform
+ when Gem::Platform::CURRENT then
+ @new_platform = Gem::Platform.local
+ @original_platform = @new_platform.to_s
+
+ when Gem::Platform then
+ @new_platform = platform
+
+ # legacy constants
+ when nil, Gem::Platform::RUBY then
+ @new_platform = Gem::Platform::RUBY
+ when 'mswin32' then # was Gem::Platform::WIN32
+ @new_platform = Gem::Platform.new 'x86-mswin32'
+ when 'mswin64' then
+ @new_platform = Gem::Platform.new 'x86-mswin64'
+ when 'i586-linux' then # was Gem::Platform::LINUX_586
+ @new_platform = Gem::Platform.new 'x86-linux'
+ when 'powerpc-darwin' then # was Gem::Platform::DARWIN
+ @new_platform = Gem::Platform.new 'ppc-darwin'
+ else
+ @new_platform = Gem::Platform.new platform
+ end
+
+ @platform = @new_platform.to_s
+
+ @new_platform
+ end
+
+ overwrite_accessor :required_ruby_version= do |value|
+ @required_ruby_version = Gem::Requirement.create(value)
+ end
+
+ overwrite_accessor :required_rubygems_version= do |value|
+ @required_rubygems_version = Gem::Requirement.create(value)
+ end
+
+ overwrite_accessor :date= do |date|
+ # We want to end up with a Time object with one-day resolution.
+ # This is the cleanest, most-readable, faster-than-using-Date
+ # way to do it.
+ case date
+ when String then
+ @date = if /\A(\d{4})-(\d{2})-(\d{2})\Z/ =~ date then
+ Time.local($1.to_i, $2.to_i, $3.to_i)
+ else
+ require 'time'
+ Time.parse date
+ end
+ when Time then
+ @date = Time.local(date.year, date.month, date.day)
+ when Date then
+ @date = Time.local(date.year, date.month, date.day)
+ else
+ @date = TODAY
+ end
+ end
+
+ overwrite_accessor :date do
+ self.date = nil if @date.nil? # HACK Sets the default value for date
+ @date
+ end
+
+ overwrite_accessor :summary= do |str|
+ @summary = if str then
+ str.strip.
+ gsub(/(\w-)\n[ \t]*(\w)/, '\1\2').
+ gsub(/\n[ \t]*/, " ")
+ end
+ end
+
+ overwrite_accessor :description= do |str|
+ @description = if str then
+ str.strip.
+ gsub(/(\w-)\n[ \t]*(\w)/, '\1\2').
+ gsub(/\n[ \t]*/, " ")
+ end
+ end
+
+ overwrite_accessor :default_executable do
+ begin
+ if defined?(@default_executable) and @default_executable
+ result = @default_executable
+ elsif @executables and @executables.size == 1
+ result = Array(@executables).first
+ else
+ result = nil
+ end
+ result
+ rescue
+ nil
+ end
+ end
+
+ overwrite_accessor :test_files do
+ # Handle the possibility that we have @test_suite_file but not
+ # @test_files. This will happen when an old gem is loaded via
+ # YAML.
+ if defined? @test_suite_file then
+ @test_files = [@test_suite_file].flatten
+ @test_suite_file = nil
+ end
+ if defined?(@test_files) and @test_files then
+ @test_files
+ else
+ @test_files = []
+ end
+ end
+
+ overwrite_accessor :files do
+ result = []
+ result.push(*@files) if defined?(@files)
+ result.push(*@test_files) if defined?(@test_files)
+ result.push(*(add_bindir(@executables)))
+ result.push(*@extra_rdoc_files) if defined?(@extra_rdoc_files)
+ result.push(*@extensions) if defined?(@extensions)
+ result.uniq.compact
+ end
+
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/test_utilities.rb b/ruby/lib/rubygems/test_utilities.rb
new file mode 100644
index 0000000..8b23d32
--- /dev/null
+++ b/ruby/lib/rubygems/test_utilities.rb
@@ -0,0 +1,131 @@
+require 'tempfile'
+require 'rubygems'
+require 'rubygems/remote_fetcher'
+
+##
+# A fake Gem::RemoteFetcher for use in tests or to avoid real live HTTP
+# requests when testing code that uses RubyGems.
+#
+# Example:
+#
+# @fetcher = Gem::FakeFetcher.new
+# @fetcher.data['http://gems.example.com/yaml'] = source_index.to_yaml
+# Gem::RemoteFetcher.fetcher = @fetcher
+#
+# # invoke RubyGems code
+#
+# paths = @fetcher.paths
+# assert_equal 'http://gems.example.com/yaml', paths.shift
+# assert paths.empty?, paths.join(', ')
+#
+# See RubyGems' tests for more examples of FakeFetcher.
+
+class Gem::FakeFetcher
+
+ attr_reader :data
+ attr_accessor :paths
+
+ def initialize
+ @data = {}
+ @paths = []
+ end
+
+ def fetch_path path, mtime = nil
+ path = path.to_s
+ @paths << path
+ raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
+
+ unless @data.key? path then
+ raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
+ end
+
+ data = @data[path]
+
+ if data.respond_to?(:call) then
+ data.call
+ else
+ if path.to_s =~ /gz$/ and not data.nil? and not data.empty? then
+ data = Gem.gunzip data
+ end
+
+ data
+ end
+ end
+
+ def fetch_size(path)
+ path = path.to_s
+ @paths << path
+
+ raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
+
+ unless @data.key? path then
+ raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
+ end
+
+ data = @data[path]
+
+ data.respond_to?(:call) ? data.call : data.length
+ end
+
+ def download spec, source_uri, install_dir = Gem.dir
+ name = "#{spec.full_name}.gem"
+ path = File.join(install_dir, 'cache', name)
+
+ Gem.ensure_gem_subdirectories install_dir
+
+ if source_uri =~ /^http/ then
+ File.open(path, "wb") do |f|
+ f.write fetch_path(File.join(source_uri, "gems", name))
+ end
+ else
+ FileUtils.cp source_uri, path
+ end
+
+ path
+ end
+
+end
+
+# :stopdoc:
+class Gem::RemoteFetcher
+
+ def self.fetcher=(fetcher)
+ @fetcher = fetcher
+ end
+
+end
+# :startdoc:
+
+##
+# A StringIO duck-typed class that uses Tempfile instead of String as the
+# backing store.
+#--
+# This class was added to flush out problems in Rubinius' IO implementation.
+
+class TempIO
+
+ @@count = 0
+
+ def initialize(string = '')
+ @tempfile = Tempfile.new "TempIO-#{@@count += 1}"
+ @tempfile.binmode
+ @tempfile.write string
+ @tempfile.rewind
+ end
+
+ def method_missing(meth, *args, &block)
+ @tempfile.send(meth, *args, &block)
+ end
+
+ def respond_to?(meth)
+ @tempfile.respond_to? meth
+ end
+
+ def string
+ @tempfile.flush
+
+ Gem.read_binary @tempfile.path
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/timer.rb b/ruby/lib/rubygems/timer.rb
new file mode 100644
index 0000000..06250f2
--- /dev/null
+++ b/ruby/lib/rubygems/timer.rb
@@ -0,0 +1,25 @@
+#
+# This file defines a $log variable for logging, and a time() method for recording timing
+# information.
+#
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+
+$log = Object.new
+def $log.debug(str)
+ STDERR.puts str
+end
+
+def time(msg, width=25)
+ t = Time.now
+ return_value = yield
+ elapsed = Time.now.to_f - t.to_f
+ elapsed = sprintf("%3.3f", elapsed)
+ $log.debug "#{msg.ljust(width)}: #{elapsed}s"
+ return_value
+end
+
diff --git a/ruby/lib/rubygems/uninstaller.rb b/ruby/lib/rubygems/uninstaller.rb
new file mode 100644
index 0000000..5f19da5
--- /dev/null
+++ b/ruby/lib/rubygems/uninstaller.rb
@@ -0,0 +1,242 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'fileutils'
+require 'rubygems'
+require 'rubygems/dependency_list'
+require 'rubygems/doc_manager'
+require 'rubygems/user_interaction'
+
+##
+# An Uninstaller.
+
+class Gem::Uninstaller
+
+ include Gem::UserInteraction
+
+ ##
+ # The directory a gem's executables will be installed into
+
+ attr_reader :bin_dir
+
+ ##
+ # The gem repository the gem will be installed into
+
+ attr_reader :gem_home
+
+ ##
+ # The Gem::Specification for the gem being uninstalled, only set during
+ # #uninstall_gem
+
+ attr_reader :spec
+
+ ##
+ # Constructs an uninstaller that will uninstall +gem+
+
+ def initialize(gem, options = {})
+ @gem = gem
+ @version = options[:version] || Gem::Requirement.default
+ gem_home = options[:install_dir] || Gem.dir
+ @gem_home = File.expand_path gem_home
+ @force_executables = options[:executables]
+ @force_all = options[:all]
+ @force_ignore = options[:ignore]
+ @bin_dir = options[:bin_dir]
+
+ spec_dir = File.join @gem_home, 'specifications'
+ @source_index = Gem::SourceIndex.from_gems_in spec_dir
+ end
+
+ ##
+ # Performs the uninstall of the gem. This removes the spec, the Gem
+ # directory, and the cached .gem file.
+
+ def uninstall
+ list = @source_index.find_name @gem, @version
+
+ if list.empty? then
+ raise Gem::InstallError, "Unknown gem #{@gem} #{@version}"
+
+ elsif list.size > 1 and @force_all then
+ remove_all list.dup
+
+ elsif list.size > 1 then
+ gem_names = list.collect {|gem| gem.full_name} + ["All versions"]
+
+ say
+ gem_name, index = choose_from_list "Select gem to uninstall:", gem_names
+
+ if index == list.size then
+ remove_all list.dup
+ elsif index >= 0 && index < list.size then
+ uninstall_gem list[index], list.dup
+ else
+ say "Error: must enter a number [1-#{list.size+1}]"
+ end
+ else
+ uninstall_gem list.first, list.dup
+ end
+ end
+
+ ##
+ # Uninstalls gem +spec+
+
+ def uninstall_gem(spec, specs)
+ @spec = spec
+
+ Gem.pre_uninstall_hooks.each do |hook|
+ hook.call self
+ end
+
+ specs.each { |s| remove_executables s }
+ remove spec, specs
+
+ Gem.post_uninstall_hooks.each do |hook|
+ hook.call self
+ end
+
+ @spec = nil
+ end
+
+ ##
+ # Removes installed executables and batch files (windows only) for
+ # +gemspec+.
+
+ def remove_executables(gemspec)
+ return if gemspec.nil?
+
+ if gemspec.executables.size > 0 then
+ bindir = @bin_dir ? @bin_dir : (Gem.bindir @gem_home)
+
+ list = @source_index.find_name(gemspec.name).delete_if { |spec|
+ spec.version == gemspec.version
+ }
+
+ executables = gemspec.executables.clone
+
+ list.each do |spec|
+ spec.executables.each do |exe_name|
+ executables.delete(exe_name)
+ end
+ end
+
+ return if executables.size == 0
+
+ answer = if @force_executables.nil? then
+ ask_yes_no("Remove executables:\n" \
+ "\t#{gemspec.executables.join(", ")}\n\nin addition to the gem?",
+ true) # " # appease ruby-mode - don't ask
+ else
+ @force_executables
+ end
+
+ unless answer then
+ say "Executables and scripts will remain installed."
+ else
+ raise Gem::FilePermissionError, bindir unless File.writable? bindir
+
+ gemspec.executables.each do |exe_name|
+ say "Removing #{exe_name}"
+ FileUtils.rm_f File.join(bindir, exe_name)
+ FileUtils.rm_f File.join(bindir, "#{exe_name}.bat")
+ end
+ end
+ end
+ end
+
+ ##
+ # Removes all gems in +list+.
+ #
+ # NOTE: removes uninstalled gems from +list+.
+
+ def remove_all(list)
+ list.dup.each { |spec| uninstall_gem spec, list }
+ end
+
+ ##
+ # spec:: the spec of the gem to be uninstalled
+ # list:: the list of all such gems
+ #
+ # Warning: this method modifies the +list+ parameter. Once it has
+ # uninstalled a gem, it is removed from that list.
+
+ def remove(spec, list)
+ unless dependencies_ok? spec then
+ raise Gem::DependencyRemovalException,
+ "Uninstallation aborted due to dependent gem(s)"
+ end
+
+ unless path_ok? spec then
+ e = Gem::GemNotInHomeException.new \
+ "Gem is not installed in directory #{@gem_home}"
+ e.spec = spec
+
+ raise e
+ end
+
+ raise Gem::FilePermissionError, spec.installation_path unless
+ File.writable?(spec.installation_path)
+
+ FileUtils.rm_rf spec.full_gem_path
+
+ original_platform_name = [
+ spec.name, spec.version, spec.original_platform].join '-'
+
+ spec_dir = File.join spec.installation_path, 'specifications'
+ gemspec = File.join spec_dir, "#{spec.full_name}.gemspec"
+
+ unless File.exist? gemspec then
+ gemspec = File.join spec_dir, "#{original_platform_name}.gemspec"
+ end
+
+ FileUtils.rm_rf gemspec
+
+ cache_dir = File.join spec.installation_path, 'cache'
+ gem = File.join cache_dir, "#{spec.full_name}.gem"
+
+ unless File.exist? gem then
+ gem = File.join cache_dir, "#{original_platform_name}.gem"
+ end
+
+ FileUtils.rm_rf gem
+
+ Gem::DocManager.new(spec).uninstall_doc
+
+ say "Successfully uninstalled #{spec.full_name}"
+
+ list.delete spec
+ end
+
+ def path_ok?(spec)
+ full_path = File.join @gem_home, 'gems', spec.full_name
+ original_path = File.join @gem_home, 'gems', spec.original_name
+
+ full_path == spec.full_gem_path || original_path == spec.full_gem_path
+ end
+
+ def dependencies_ok?(spec)
+ return true if @force_ignore
+
+ deplist = Gem::DependencyList.from_source_index @source_index
+ deplist.ok_to_remove?(spec.full_name) || ask_if_ok(spec)
+ end
+
+ def ask_if_ok(spec)
+ msg = ['']
+ msg << 'You have requested to uninstall the gem:'
+ msg << "\t#{spec.full_name}"
+ spec.dependent_gems.each do |gem,dep,satlist|
+ msg <<
+ ("#{gem.name}-#{gem.version} depends on " +
+ "[#{dep.name} (#{dep.version_requirements})]")
+ end
+ msg << 'If you remove this gems, one or more dependencies will not be met.'
+ msg << 'Continue with Uninstall?'
+ return ask_yes_no(msg.join("\n"), true)
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/user_interaction.rb b/ruby/lib/rubygems/user_interaction.rb
new file mode 100644
index 0000000..30a728c
--- /dev/null
+++ b/ruby/lib/rubygems/user_interaction.rb
@@ -0,0 +1,360 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+module Gem
+
+ ##
+ # Module that defines the default UserInteraction. Any class including this
+ # module will have access to the +ui+ method that returns the default UI.
+
+ module DefaultUserInteraction
+
+ ##
+ # The default UI is a class variable of the singleton class for this
+ # module.
+
+ @ui = nil
+
+ ##
+ # Return the default UI.
+
+ def self.ui
+ @ui ||= Gem::ConsoleUI.new
+ end
+
+ ##
+ # Set the default UI. If the default UI is never explicitly set, a simple
+ # console based UserInteraction will be used automatically.
+
+ def self.ui=(new_ui)
+ @ui = new_ui
+ end
+
+ ##
+ # Use +new_ui+ for the duration of +block+.
+
+ def self.use_ui(new_ui)
+ old_ui = @ui
+ @ui = new_ui
+ yield
+ ensure
+ @ui = old_ui
+ end
+
+ ##
+ # See DefaultUserInteraction::ui
+
+ def ui
+ DefaultUserInteraction.ui
+ end
+
+ ##
+ # See DefaultUserInteraction::ui=
+
+ def ui=(new_ui)
+ DefaultUserInteraction.ui = new_ui
+ end
+
+ ##
+ # See DefaultUserInteraction::use_ui
+
+ def use_ui(new_ui, &block)
+ DefaultUserInteraction.use_ui(new_ui, &block)
+ end
+
+ end
+
+ ##
+ # Make the default UI accessable without the "ui." prefix. Classes
+ # including this module may use the interaction methods on the default UI
+ # directly. Classes may also reference the ui and ui= methods.
+ #
+ # Example:
+ #
+ # class X
+ # include Gem::UserInteraction
+ #
+ # def get_answer
+ # n = ask("What is the meaning of life?")
+ # end
+ # end
+
+ module UserInteraction
+
+ include DefaultUserInteraction
+
+ [:alert,
+ :alert_error,
+ :alert_warning,
+ :ask,
+ :ask_yes_no,
+ :choose_from_list,
+ :say,
+ :terminate_interaction ].each do |methname|
+ class_eval %{
+ def #{methname}(*args)
+ ui.#{methname}(*args)
+ end
+ }, __FILE__, __LINE__
+ end
+ end
+
+ ##
+ # StreamUI implements a simple stream based user interface.
+
+ class StreamUI
+
+ attr_reader :ins, :outs, :errs
+
+ def initialize(in_stream, out_stream, err_stream=STDERR)
+ @ins = in_stream
+ @outs = out_stream
+ @errs = err_stream
+ end
+
+ ##
+ # Choose from a list of options. +question+ is a prompt displayed above
+ # the list. +list+ is a list of option strings. Returns the pair
+ # [option_name, option_index].
+
+ def choose_from_list(question, list)
+ @outs.puts question
+
+ list.each_with_index do |item, index|
+ @outs.puts " #{index+1}. #{item}"
+ end
+
+ @outs.print "> "
+ @outs.flush
+
+ result = @ins.gets
+
+ return nil, nil unless result
+
+ result = result.strip.to_i - 1
+ return list[result], result
+ end
+
+ ##
+ # Ask a question. Returns a true for yes, false for no. If not connected
+ # to a tty, raises an exception if default is nil, otherwise returns
+ # default.
+
+ def ask_yes_no(question, default=nil)
+ unless @ins.tty? then
+ if default.nil? then
+ raise Gem::OperationNotSupportedError,
+ "Not connected to a tty and no default specified"
+ else
+ return default
+ end
+ end
+
+ qstr = case default
+ when nil
+ 'yn'
+ when true
+ 'Yn'
+ else
+ 'yN'
+ end
+
+ result = nil
+
+ while result.nil?
+ result = ask("#{question} [#{qstr}]")
+ result = case result
+ when /^[Yy].*/
+ true
+ when /^[Nn].*/
+ false
+ when /^$/
+ default
+ else
+ nil
+ end
+ end
+
+ return result
+ end
+
+ ##
+ # Ask a question. Returns an answer if connected to a tty, nil otherwise.
+
+ def ask(question)
+ return nil if not @ins.tty?
+
+ @outs.print(question + " ")
+ @outs.flush
+
+ result = @ins.gets
+ result.chomp! if result
+ result
+ end
+
+ ##
+ # Display a statement.
+
+ def say(statement="")
+ @outs.puts statement
+ end
+
+ ##
+ # Display an informational alert. Will ask +question+ if it is not nil.
+
+ def alert(statement, question=nil)
+ @outs.puts "INFO: #{statement}"
+ ask(question) if question
+ end
+
+ ##
+ # Display a warning in a location expected to get error messages. Will
+ # ask +question+ if it is not nil.
+
+ def alert_warning(statement, question=nil)
+ @errs.puts "WARNING: #{statement}"
+ ask(question) if question
+ end
+
+ ##
+ # Display an error message in a location expected to get error messages.
+ # Will ask +question+ if it is not nil.
+
+ def alert_error(statement, question=nil)
+ @errs.puts "ERROR: #{statement}"
+ ask(question) if question
+ end
+
+ ##
+ # Terminate the application with exit code +status+, running any exit
+ # handlers that might have been defined.
+
+ def terminate_interaction(status = 0)
+ raise Gem::SystemExitException, status
+ end
+
+ ##
+ # Return a progress reporter object chosen from the current verbosity.
+
+ def progress_reporter(*args)
+ case Gem.configuration.verbose
+ when nil, false
+ SilentProgressReporter.new(@outs, *args)
+ when true
+ SimpleProgressReporter.new(@outs, *args)
+ else
+ VerboseProgressReporter.new(@outs, *args)
+ end
+ end
+
+ ##
+ # An absolutely silent progress reporter.
+
+ class SilentProgressReporter
+ attr_reader :count
+
+ def initialize(out_stream, size, initial_message, terminal_message = nil)
+ end
+
+ def updated(message)
+ end
+
+ def done
+ end
+ end
+
+ ##
+ # A basic dotted progress reporter.
+
+ class SimpleProgressReporter
+ include DefaultUserInteraction
+
+ attr_reader :count
+
+ def initialize(out_stream, size, initial_message,
+ terminal_message = "complete")
+ @out = out_stream
+ @total = size
+ @count = 0
+ @terminal_message = terminal_message
+
+ @out.puts initial_message
+ end
+
+ ##
+ # Prints out a dot and ignores +message+.
+
+ def updated(message)
+ @count += 1
+ @out.print "."
+ @out.flush
+ end
+
+ ##
+ # Prints out the terminal message.
+
+ def done
+ @out.puts "\n#{@terminal_message}"
+ end
+
+ end
+
+ ##
+ # A progress reporter that prints out messages about the current progress.
+
+ class VerboseProgressReporter
+ include DefaultUserInteraction
+
+ attr_reader :count
+
+ def initialize(out_stream, size, initial_message,
+ terminal_message = 'complete')
+ @out = out_stream
+ @total = size
+ @count = 0
+ @terminal_message = terminal_message
+
+ @out.puts initial_message
+ end
+
+ ##
+ # Prints out the position relative to the total and the +message+.
+
+ def updated(message)
+ @count += 1
+ @out.puts "#{@count}/#{@total}: #{message}"
+ end
+
+ ##
+ # Prints out the terminal message.
+
+ def done
+ @out.puts @terminal_message
+ end
+ end
+ end
+
+ ##
+ # Subclass of StreamUI that instantiates the user interaction using STDIN,
+ # STDOUT, and STDERR.
+
+ class ConsoleUI < StreamUI
+ def initialize
+ super(STDIN, STDOUT, STDERR)
+ end
+ end
+
+ ##
+ # SilentUI is a UI choice that is absolutely silent.
+
+ class SilentUI
+ def method_missing(sym, *args, &block)
+ self
+ end
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/validator.rb b/ruby/lib/rubygems/validator.rb
new file mode 100644
index 0000000..4dd12ad
--- /dev/null
+++ b/ruby/lib/rubygems/validator.rb
@@ -0,0 +1,209 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'find'
+
+require 'rubygems/digest/md5'
+require 'rubygems/format'
+require 'rubygems/installer'
+
+##
+# Validator performs various gem file and gem database validation
+
+class Gem::Validator
+
+ include Gem::UserInteraction
+
+ ##
+ # Given a gem file's contents, validates against its own MD5 checksum
+ # gem_data:: [String] Contents of the gem file
+
+ def verify_gem(gem_data)
+ raise Gem::VerificationError, 'empty gem file' if gem_data.size == 0
+
+ unless gem_data =~ /MD5SUM/ then
+ return # Don't worry about it...this sucks. Need to fix MD5 stuff for
+ # new format
+ # FIXME
+ end
+
+ sum_data = gem_data.gsub(/MD5SUM = "([a-z0-9]+)"/,
+ "MD5SUM = \"#{"F" * 32}\"")
+
+ unless Gem::MD5.hexdigest(sum_data) == $1.to_s then
+ raise Gem::VerificationError, 'invalid checksum for gem file'
+ end
+ end
+
+ ##
+ # Given the path to a gem file, validates against its own MD5 checksum
+ #
+ # gem_path:: [String] Path to gem file
+
+ def verify_gem_file(gem_path)
+ open gem_path, Gem.binary_mode do |file|
+ gem_data = file.read
+ verify_gem gem_data
+ end
+ rescue Errno::ENOENT
+ raise Gem::VerificationError, "missing gem file #{gem_path}"
+ end
+
+ private
+
+ def find_files_for_gem(gem_directory)
+ installed_files = []
+ Find.find(gem_directory) {|file_name|
+ fn = file_name.slice((gem_directory.size)..(file_name.size-1)).sub(/^\//, "")
+ if(!(fn =~ /CVS/ || File.directory?(fn) || fn == "")) then
+ installed_files << fn
+ end
+
+ }
+ installed_files
+ end
+
+ public
+
+ ErrorData = Struct.new :path, :problem
+
+ ##
+ # Checks the gem directory for the following potential
+ # inconsistencies/problems:
+ #
+ # * Checksum gem itself
+ # * For each file in each gem, check consistency of installed versions
+ # * Check for files that aren't part of the gem but are in the gems directory
+ # * 1 cache - 1 spec - 1 directory.
+ #
+ # returns a hash of ErrorData objects, keyed on the problem gem's name.
+
+ def alien
+ errors = {}
+
+ Gem::SourceIndex.from_installed_gems.each do |gem_name, gem_spec|
+ errors[gem_name] ||= []
+
+ gem_path = File.join(Gem.dir, "cache", gem_spec.full_name) + ".gem"
+ spec_path = File.join(Gem.dir, "specifications", gem_spec.full_name) + ".gemspec"
+ gem_directory = File.join(Gem.dir, "gems", gem_spec.full_name)
+
+ installed_files = find_files_for_gem(gem_directory)
+
+ unless File.exist? spec_path then
+ errors[gem_name] << ErrorData.new(spec_path, "Spec file doesn't exist for installed gem")
+ end
+
+ begin
+ verify_gem_file(gem_path)
+
+ open gem_path, Gem.binary_mode do |file|
+ format = Gem::Format.from_file_by_path(gem_path)
+ format.file_entries.each do |entry, data|
+ # Found this file. Delete it from list
+ installed_files.delete remove_leading_dot_dir(entry['path'])
+
+ next unless data # HACK `gem check -a mkrf`
+
+ open File.join(gem_directory, entry['path']), Gem.binary_mode do |f|
+ unless Gem::MD5.hexdigest(f.read).to_s ==
+ Gem::MD5.hexdigest(data).to_s then
+ errors[gem_name] << ErrorData.new(entry['path'], "installed file doesn't match original from gem")
+ end
+ end
+ end
+ end
+ rescue Gem::VerificationError => e
+ errors[gem_name] << ErrorData.new(gem_path, e.message)
+ end
+
+ # Clean out directories that weren't explicitly included in the gemspec
+ # FIXME: This still allows arbitrary incorrect directories.
+ installed_files.delete_if {|potential_directory|
+ File.directory?(File.join(gem_directory, potential_directory))
+ }
+ if(installed_files.size > 0) then
+ errors[gem_name] << ErrorData.new(gem_path, "Unmanaged files in gem: #{installed_files.inspect}")
+ end
+ end
+
+ errors
+ end
+
+ if RUBY_VERSION < '1.9' then
+ class TestRunner
+ def initialize(suite, ui)
+ @suite = suite
+ @ui = ui
+ end
+
+ def self.run(suite, ui)
+ require 'test/unit/ui/testrunnermediator'
+ return new(suite, ui).start
+ end
+
+ def start
+ @mediator = Test::Unit::UI::TestRunnerMediator.new(@suite)
+ @mediator.add_listener(Test::Unit::TestResult::FAULT, &method(:add_fault))
+ return @mediator.run_suite
+ end
+
+ def add_fault(fault)
+ if Gem.configuration.verbose then
+ @ui.say fault.long_display
+ end
+ end
+ end
+
+ autoload :TestRunner, 'test/unit/ui/testrunnerutilities'
+ end
+
+ ##
+ # Runs unit tests for a given gem specification
+
+ def unit_test(gem_spec)
+ start_dir = Dir.pwd
+ Dir.chdir(gem_spec.full_gem_path)
+ $: << File.join(Gem.dir, "gems", gem_spec.full_name)
+ # XXX: why do we need this gem_spec when we've already got 'spec'?
+ test_files = gem_spec.test_files
+
+ if test_files.empty? then
+ say "There are no unit tests to run for #{gem_spec.full_name}"
+ return nil
+ end
+
+ gem gem_spec.name, "= #{gem_spec.version.version}"
+
+ test_files.each do |f| require f end
+
+ if RUBY_VERSION < '1.9' then
+ suite = Test::Unit::TestSuite.new("#{gem_spec.name}-#{gem_spec.version}")
+
+ ObjectSpace.each_object(Class) do |klass|
+ suite << klass.suite if (klass < Test::Unit::TestCase)
+ end
+
+ result = TestRunner.run suite, ui
+
+ alert_error result.to_s unless result.passed?
+ else
+ result = MiniTest::Unit.new
+ result.run
+ end
+
+ result
+ ensure
+ Dir.chdir(start_dir)
+ end
+
+ private
+ def remove_leading_dot_dir(path)
+ path.sub(/^\.\//, "")
+ end
+
+end
+
diff --git a/ruby/lib/rubygems/version.rb b/ruby/lib/rubygems/version.rb
new file mode 100644
index 0000000..ff4a7bf
--- /dev/null
+++ b/ruby/lib/rubygems/version.rb
@@ -0,0 +1,167 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+
+##
+# The Version class processes string versions into comparable values
+
+class Gem::Version
+
+ include Comparable
+
+ attr_reader :ints
+
+ attr_reader :version
+
+ ##
+ # Returns true if +version+ is a valid version string.
+
+ def self.correct?(version)
+ case version
+ when Integer, /\A\s*(\d+(\.\d+)*)*\s*\z/ then true
+ else false
+ end
+ end
+
+ ##
+ # Factory method to create a Version object. Input may be a Version or a
+ # String. Intended to simplify client code.
+ #
+ # ver1 = Version.create('1.3.17') # -> (Version object)
+ # ver2 = Version.create(ver1) # -> (ver1)
+ # ver3 = Version.create(nil) # -> nil
+
+ def self.create(input)
+ if input.respond_to? :version then
+ input
+ elsif input.nil? then
+ nil
+ else
+ new input
+ end
+ end
+
+ ##
+ # Constructs a Version from the +version+ string. A version string is a
+ # series of digits separated by dots.
+
+ def initialize(version)
+ raise ArgumentError, "Malformed version number string #{version}" unless
+ self.class.correct?(version)
+
+ self.version = version
+ end
+
+ def inspect # :nodoc:
+ "#<#{self.class} #{@version.inspect}>"
+ end
+
+ # Dump only the raw version string, not the complete object
+ def marshal_dump
+ [@version]
+ end
+
+ # Load custom marshal format
+ def marshal_load(array)
+ self.version = array[0]
+ end
+
+ ##
+ # Strip ignored trailing zeros.
+
+ def normalize
+ @ints = build_array_from_version_string
+
+ return if @ints.length == 1
+
+ @ints.pop while @ints.last == 0
+
+ @ints = [0] if @ints.empty?
+ end
+
+ ##
+ # Returns the text representation of the version
+ #
+ # return:: [String] version as string
+ #
+ def to_s
+ @version
+ end
+
+ ##
+ # Returns an integer array representation of this Version.
+
+ def to_ints
+ normalize unless @ints
+ @ints
+ end
+
+ def to_yaml_properties
+ ['@version']
+ end
+
+ def version=(version)
+ @version = version.to_s.strip
+ normalize
+ end
+
+ def yaml_initialize(tag, values)
+ self.version = values['version']
+ end
+
+ ##
+ # Compares this version with +other+ returning -1, 0, or 1 if the other
+ # version is larger, the same, or smaller than this one.
+
+ def <=>(other)
+ return nil unless self.class === other
+ return 1 unless other
+ @ints <=> other.ints
+ end
+
+ ##
+ # A Version is only eql? to another version if it has the same version
+ # string. "1.0" is not the same version as "1".
+
+ def eql?(other)
+ self.class === other and @version == other.version
+ end
+
+ def hash # :nodoc:
+ @version.hash
+ end
+
+ # Return a new version object where the next to the last revision
+ # number is one greater. (e.g. 5.3.1 => 5.4)
+ def bump
+ ints = build_array_from_version_string
+ ints.pop if ints.size > 1
+ ints[-1] += 1
+ self.class.new(ints.join("."))
+ end
+
+ def build_array_from_version_string
+ @version.to_s.scan(/\d+/).map { |s| s.to_i }
+ end
+ private :build_array_from_version_string
+
+ #:stopdoc:
+
+ require 'rubygems/requirement'
+
+ # Gem::Requirement's original definition is nested in Version.
+ # Although an inappropriate place, current gems specs reference the nested
+ # class name explicitly. To remain compatible with old software loading
+ # gemspecs, we leave a copy of original definition in Version, but define an
+ # alias Gem::Requirement for use everywhere else.
+
+ Requirement = ::Gem::Requirement
+
+ # :startdoc:
+
+end
+
diff --git a/ruby/lib/rubygems/version_option.rb b/ruby/lib/rubygems/version_option.rb
new file mode 100644
index 0000000..1374018
--- /dev/null
+++ b/ruby/lib/rubygems/version_option.rb
@@ -0,0 +1,48 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems'
+
+# Mixin methods for --version and --platform Gem::Command options.
+module Gem::VersionOption
+
+ # Add the --platform option to the option parser.
+ def add_platform_option(task = command, *wrap)
+ OptionParser.accept Gem::Platform do |value|
+ if value == Gem::Platform::RUBY then
+ value
+ else
+ Gem::Platform.new value
+ end
+ end
+
+ add_option('--platform PLATFORM', Gem::Platform,
+ "Specify the platform of gem to #{task}", *wrap) do
+ |value, options|
+ unless options[:added_platform] then
+ Gem.platforms = [Gem::Platform::RUBY]
+ options[:added_platform] = true
+ end
+
+ Gem.platforms << value unless Gem.platforms.include? value
+ end
+ end
+
+ # Add the --version option to the option parser.
+ def add_version_option(task = command, *wrap)
+ OptionParser.accept Gem::Requirement do |value|
+ Gem::Requirement.new value
+ end
+
+ add_option('-v', '--version VERSION', Gem::Requirement,
+ "Specify version of gem to #{task}", *wrap) do
+ |value, options|
+ options[:version] = value
+ end
+ end
+
+end
+
diff --git a/ruby/lib/scanf.rb b/ruby/lib/scanf.rb
new file mode 100644
index 0000000..549f540
--- /dev/null
+++ b/ruby/lib/scanf.rb
@@ -0,0 +1,703 @@
+# scanf for Ruby
+#
+# $Release Version: 1.1.2 $
+# $Revision: 19094 $
+# $Id: scanf.rb 19094 2008-09-03 12:54:13Z dblack $
+# $Author: dblack $
+#
+# A product of the Austin Ruby Codefest (Austin, Texas, August 2002)
+
+=begin
+
+=scanf for Ruby
+
+==Description
+
+scanf for Ruby is an implementation of the C function scanf(3),
+modified as necessary for Ruby compatibility.
+
+The methods provided are String#scanf, IO#scanf, and
+Kernel#scanf. Kernel#scanf is a wrapper around STDIN.scanf. IO#scanf
+can be used on any IO stream, including file handles and sockets.
+scanf can be called either with or without a block.
+
+scanf for Ruby scans an input string or stream according to a
+<b>format</b>, as described below ("Conversions"), and returns an
+array of matches between the format and the input. The format is
+defined in a string, and is similar (though not identical) to the
+formats used in Kernel#printf and Kernel#sprintf.
+
+The format may contain <b>conversion specifiers</b>, which tell scanf
+what form (type) each particular matched substring should be converted
+to (e.g., decimal integer, floating point number, literal string,
+etc.) The matches and conversions take place from left to right, and
+the conversions themselves are returned as an array.
+
+The format string may also contain characters other than those in the
+conversion specifiers. White space (blanks, tabs, or newlines) in the
+format string matches any amount of white space, including none, in
+the input. Everything else matches only itself.
+
+Scanning stops, and scanf returns, when any input character fails to
+match the specifications in the format string, or when input is
+exhausted, or when everything in the format string has been
+matched. All matches found up to the stopping point are returned in
+the return array (or yielded to the block, if a block was given).
+
+
+==Basic usage
+
+ require 'scanf.rb'
+
+ # String#scanf and IO#scanf take a single argument (a format string)
+ array = aString.scanf("%d%s")
+ array = anIO.scanf("%d%s")
+
+ # Kernel#scanf reads from STDIN
+ array = scanf("%d%s")
+
+==Block usage
+
+When called with a block, scanf keeps scanning the input, cycling back
+to the beginning of the format string, and yields a new array of
+conversions to the block every time the format string is matched
+(including partial matches, but not including complete failures). The
+actual return value of scanf when called with a block is an array
+containing the results of all the executions of the block.
+
+ str = "123 abc 456 def 789 ghi"
+ str.scanf("%d%s") { |num,str| [ num * 2, str.upcase ] }
+ # => [[246, "ABC"], [912, "DEF"], [1578, "GHI"]]
+
+==Conversions
+
+The single argument to scanf is a format string, which generally
+includes one or more conversion specifiers. Conversion specifiers
+begin with the percent character ('%') and include information about
+what scanf should next scan for (string, decimal number, single
+character, etc.).
+
+There may be an optional maximum field width, expressed as a decimal
+integer, between the % and the conversion. If no width is given, a
+default of `infinity' is used (with the exception of the %c specifier;
+see below). Otherwise, given a field width of <em>n</em> for a given
+conversion, at most <em>n</em> characters are scanned in processing
+that conversion. Before conversion begins, most conversions skip
+white space in the input string; this white space is not counted
+against the field width.
+
+The following conversions are available. (See the files EXAMPLES
+and <tt>tests/scanftests.rb</tt> for examples.)
+
+[%]
+ Matches a literal `%'. That is, `%%' in the format string matches a
+ single input `%' character. No conversion is done, and the resulting
+ '%' is not included in the return array.
+
+[d]
+ Matches an optionally signed decimal integer.
+
+[u]
+ Same as d.
+
+[i]
+ Matches an optionally signed integer. The integer is read in base
+ 16 if it begins with `0x' or `0X', in base 8 if it begins with `0',
+ and in base 10 other- wise. Only characters that correspond to the
+ base are recognized.
+
+[o]
+ Matches an optionally signed octal integer.
+
+[x,X]
+ Matches an optionally signed hexadecimal integer,
+
+[f,g,e,E]
+ Matches an optionally signed floating-point number.
+
+[s]
+ Matches a sequence of non-white-space character. The input string stops at
+ white space or at the maximum field width, whichever occurs first.
+
+[c]
+ Matches a single character, or a sequence of <em>n</em> characters if a
+ field width of <em>n</em> is specified. The usual skip of leading white
+ space is suppressed. To skip white space first, use an explicit space in
+ the format.
+
+[<tt>[</tt>]
+ Matches a nonempty sequence of characters from the specified set
+ of accepted characters. The usual skip of leading white space is
+ suppressed. This bracketed sub-expression is interpreted exactly like a
+ character class in a Ruby regular expression. (In fact, it is placed as-is
+ in a regular expression.) The matching against the input string ends with
+ the appearance of a character not in (or, with a circumflex, in) the set,
+ or when the field width runs out, whichever comes first.
+
+===Assignment suppression
+
+To require that a particular match occur, but without including the result
+in the return array, place the <b>assignment suppression flag</b>, which is
+the star character ('*'), immediately after the leading '%' of a format
+specifier (just before the field width, if any).
+
+==Examples
+
+See the files <tt>EXAMPLES</tt> and <tt>tests/scanftests.rb</tt>.
+
+==scanf for Ruby compared with scanf in C
+
+scanf for Ruby is based on the C function scanf(3), but with modifications,
+dictated mainly by the underlying differences between the languages.
+
+===Unimplemented flags and specifiers
+
+* The only flag implemented in scanf for Ruby is '<tt>*</tt>' (ignore
+ upcoming conversion). Many of the flags available in C versions of scanf(4)
+ have to do with the type of upcoming pointer arguments, and are literally
+ meaningless in Ruby.
+
+* The <tt>n</tt> specifier (store number of characters consumed so far in
+ next pointer) is not implemented.
+
+* The <tt>p</tt> specifier (match a pointer value) is not implemented.
+
+===Altered specifiers
+
+[o,u,x,X]
+ In scanf for Ruby, all of these specifiers scan for an optionally signed
+ integer, rather than for an unsigned integer like their C counterparts.
+
+===Return values
+
+scanf for Ruby returns an array of successful conversions, whereas
+scanf(3) returns the number of conversions successfully
+completed. (See below for more details on scanf for Ruby's return
+values.)
+
+==Return values
+
+Without a block, scanf returns an array containing all the conversions
+it has found. If none are found, scanf will return an empty array. An
+unsuccesful match is never ignored, but rather always signals the end
+of the scanning operation. If the first unsuccessful match takes place
+after one or more successful matches have already taken place, the
+returned array will contain the results of those successful matches.
+
+With a block scanf returns a 'map'-like array of transformations from
+the block -- that is, an array reflecting what the block did with each
+yielded result from the iterative scanf operation. (See "Block
+usage", above.)
+
+==Test suite
+
+scanf for Ruby includes a suite of unit tests (requiring the
+<tt>TestUnit</tt> package), which can be run with the command <tt>ruby
+tests/scanftests.rb</tt> or the command <tt>make test</tt>.
+
+==Current limitations and bugs
+
+When using IO#scanf under Windows, make sure you open your files in
+binary mode:
+
+ File.open("filename", "rb")
+
+so that scanf can keep track of characters correctly.
+
+Support for character classes is reasonably complete (since it
+essentially piggy-backs on Ruby's regular expression handling of
+character classes), but users are advised that character class testing
+has not been exhaustive, and that they should exercise some caution
+in using any of the more complex and/or arcane character class
+idioms.
+
+
+==Technical notes
+
+===Rationale behind scanf for Ruby
+
+The impetus for a scanf implementation in Ruby comes chiefly from the fact
+that existing pattern matching operations, such as Regexp#match and
+String#scan, return all results as strings, which have to be converted to
+integers or floats explicitly in cases where what's ultimately wanted are
+integer or float values.
+
+===Design of scanf for Ruby
+
+scanf for Ruby is essentially a <format string>-to-<regular
+expression> converter.
+
+When scanf is called, a FormatString object is generated from the
+format string ("%d%s...") argument. The FormatString object breaks the
+format string down into atoms ("%d", "%5f", "blah", etc.), and from
+each atom it creates a FormatSpecifier object, which it
+saves.
+
+Each FormatSpecifier has a regular expression fragment and a "handler"
+associated with it. For example, the regular expression fragment
+associated with the format "%d" is "([-+]?\d+)", and the handler
+associated with it is a wrapper around String#to_i. scanf itself calls
+FormatString#match, passing in the input string. FormatString#match
+iterates through its FormatSpecifiers; for each one, it matches the
+corresponding regular expression fragment against the string. If
+there's a match, it sends the matched string to the handler associated
+with the FormatSpecifier.
+
+Thus, to follow up the "%d" example: if "123" occurs in the input
+string when a FormatSpecifier consisting of "%d" is reached, the "123"
+will be matched against "([-+]?\d+)", and the matched string will be
+rendered into an integer by a call to to_i.
+
+The rendered match is then saved to an accumulator array, and the
+input string is reduced to the post-match substring. Thus the string
+is "eaten" from the left as the FormatSpecifiers are applied in
+sequence. (This is done to a duplicate string; the original string is
+not altered.)
+
+As soon as a regular expression fragment fails to match the string, or
+when the FormatString object runs out of FormatSpecifiers, scanning
+stops and results accumulated so far are returned in an array.
+
+==License and copyright
+
+Copyright:: (c) 2002-2003 David Alan Black
+License:: Distributed on the same licensing terms as Ruby itself
+
+==Warranty disclaimer
+
+This software is provided "as is" and without any express or implied
+warranties, including, without limitation, the implied warranties of
+merchantibility and fitness for a particular purpose.
+
+==Credits and acknowledgements
+
+scanf for Ruby was developed as the major activity of the Austin
+Ruby Codefest (Austin, Texas, August 2002).
+
+Principal author:: David Alan Black (mailto:dblack@superlink.net)
+Co-author:: Hal Fulton (mailto:hal9000@hypermetrics.com)
+Project contributors:: Nolan Darilek, Jason Johnston
+
+Thanks to Hal Fulton for hosting the Codefest.
+
+Thanks to Matz for suggestions about the class design.
+
+Thanks to Gavin Sinclair for some feedback on the documentation.
+
+The text for parts of this document, especially the Description and
+Conversions sections, above, were adapted from the Linux Programmer's
+Manual manpage for scanf(3), dated 1995-11-01.
+
+==Bugs and bug reports
+
+scanf for Ruby is based on something of an amalgam of C scanf
+implementations and documentation, rather than on a single canonical
+description. Suggestions for features and behaviors which appear in
+other scanfs, and would be meaningful in Ruby, are welcome, as are
+reports of suspicious behaviors and/or bugs. (Please see "Credits and
+acknowledgements", above, for email addresses.)
+
+=end
+
+module Scanf
+
+ class FormatSpecifier
+
+ attr_reader :re_string, :matched_string, :conversion, :matched
+
+ private
+
+ def skip; /^\s*%\*/.match(@spec_string); end
+
+ def extract_float(s); s.to_f if s &&! skip; end
+ def extract_decimal(s); s.to_i if s &&! skip; end
+ def extract_hex(s); s.hex if s &&! skip; end
+ def extract_octal(s); s.oct if s &&! skip; end
+ def extract_integer(s); Integer(s) if s &&! skip; end
+ def extract_plain(s); s unless skip; end
+
+ def nil_proc(s); nil; end
+
+ public
+
+ def to_s
+ @spec_string
+ end
+
+ def count_space?
+ /(?:\A|\S)%\*?\d*c|%\d*\[/.match(@spec_string)
+ end
+
+ def initialize(str)
+ @spec_string = str
+ h = '[A-Fa-f0-9]'
+
+ @re_string, @handler =
+ case @spec_string
+
+ # %[[:...:]]
+ when /%\*?(\[\[:[a-z]+:\]\])/
+ [ "(#{$1}+)", :extract_plain ]
+
+ # %5[[:...:]]
+ when /%\*?(\d+)(\[\[:[a-z]+:\]\])/
+ [ "(#{$2}{1,#{$1}})", :extract_plain ]
+
+ # %[...]
+ when /%\*?\[([^\]]*)\]/
+ yes = $1
+ if /^\^/.match(yes) then no = yes[1..-1] else no = '^' + yes end
+ [ "([#{yes}]+)(?=[#{no}]|\\z)", :extract_plain ]
+
+ # %5[...]
+ when /%\*?(\d+)\[([^\]]*)\]/
+ yes = $2
+ w = $1
+ [ "([#{yes}]{1,#{w}})", :extract_plain ]
+
+ # %i
+ when /%\*?i/
+ [ "([-+]?(?:(?:0[0-7]+)|(?:0[Xx]#{h}+)|(?:[1-9]\\d*)))", :extract_integer ]
+
+ # %5i
+ when /%\*?(\d+)i/
+ n = $1.to_i
+ s = "("
+ if n > 1 then s += "[1-9]\\d{1,#{n-1}}|" end
+ if n > 1 then s += "0[0-7]{1,#{n-1}}|" end
+ if n > 2 then s += "[-+]0[0-7]{1,#{n-2}}|" end
+ if n > 2 then s += "[-+][1-9]\\d{1,#{n-2}}|" end
+ if n > 2 then s += "0[Xx]#{h}{1,#{n-2}}|" end
+ if n > 3 then s += "[-+]0[Xx]#{h}{1,#{n-3}}|" end
+ s += "\\d"
+ s += ")"
+ [ s, :extract_integer ]
+
+ # %d, %u
+ when /%\*?[du]/
+ [ '([-+]?\d+)', :extract_decimal ]
+
+ # %5d, %5u
+ when /%\*?(\d+)[du]/
+ n = $1.to_i
+ s = "("
+ if n > 1 then s += "[-+]\\d{1,#{n-1}}|" end
+ s += "\\d{1,#{$1}})"
+ [ s, :extract_decimal ]
+
+ # %x
+ when /%\*?[Xx]/
+ [ "([-+]?(?:0[Xx])?#{h}+)", :extract_hex ]
+
+ # %5x
+ when /%\*?(\d+)[Xx]/
+ n = $1.to_i
+ s = "("
+ if n > 3 then s += "[-+]0[Xx]#{h}{1,#{n-3}}|" end
+ if n > 2 then s += "0[Xx]#{h}{1,#{n-2}}|" end
+ if n > 1 then s += "[-+]#{h}{1,#{n-1}}|" end
+ s += "#{h}{1,#{n}}"
+ s += ")"
+ [ s, :extract_hex ]
+
+ # %o
+ when /%\*?o/
+ [ '([-+]?[0-7]+)', :extract_octal ]
+
+ # %5o
+ when /%\*?(\d+)o/
+ [ "([-+][0-7]{1,#{$1.to_i-1}}|[0-7]{1,#{$1}})", :extract_octal ]
+
+ # %f
+ when /%\*?f/
+ [ '([-+]?((\d+(?>(?=[^\d.]|$)))|(\d*(\.(\d*([eE][-+]?\d+)?)))))', :extract_float ]
+
+ # %5f
+ when /%\*?(\d+)f/
+ [ "(\\S{1,#{$1}})", :extract_float ]
+
+ # %5s
+ when /%\*?(\d+)s/
+ [ "(\\S{1,#{$1}})", :extract_plain ]
+
+ # %s
+ when /%\*?s/
+ [ '(\S+)', :extract_plain ]
+
+ # %c
+ when /\s%\*?c/
+ [ "\\s*(.)", :extract_plain ]
+
+ # %c
+ when /%\*?c/
+ [ "(.)", :extract_plain ]
+
+ # %5c (whitespace issues are handled by the count_*_space? methods)
+ when /%\*?(\d+)c/
+ [ "(.{1,#{$1}})", :extract_plain ]
+
+ # %%
+ when /%%/
+ [ '(\s*%)', :nil_proc ]
+
+ # literal characters
+ else
+ [ "(#{Regexp.escape(@spec_string)})", :nil_proc ]
+ end
+
+ @re_string = '\A' + @re_string
+ end
+
+ def to_re
+ Regexp.new(@re_string,Regexp::MULTILINE)
+ end
+
+ def match(str)
+ @matched = false
+ s = str.dup
+ s.sub!(/\A\s+/,'') unless count_space?
+ res = to_re.match(s)
+ if res
+ @conversion = send(@handler, res[1])
+ @matched_string = @conversion.to_s
+ @matched = true
+ end
+ res
+ end
+
+ def letter
+ @spec_string[/%\*?\d*([a-z\[])/, 1]
+ end
+
+ def width
+ w = @spec_string[/%\*?(\d+)/, 1]
+ w && w.to_i
+ end
+
+ def mid_match?
+ return false unless @matched
+ cc_no_width = letter == '[' &&! width
+ c_or_cc_width = (letter == 'c' || letter == '[') && width
+ width_left = c_or_cc_width && (matched_string.size < width)
+
+ return width_left || cc_no_width
+ end
+
+ end
+
+ class FormatString
+
+ attr_reader :string_left, :last_spec_tried,
+ :last_match_tried, :matched_count, :space
+
+ SPECIFIERS = 'diuXxofeEgsc'
+ REGEX = /
+ # possible space, followed by...
+ (?:\s*
+ # percent sign, followed by...
+ %
+ # another percent sign, or...
+ (?:%|
+ # optional assignment suppression flag
+ \*?
+ # optional maximum field width
+ \d*
+ # named character class, ...
+ (?:\[\[:\w+:\]\]|
+ # traditional character class, or...
+ \[[^\]]*\]|
+ # specifier letter.
+ [#{SPECIFIERS}])))|
+ # or miscellaneous characters
+ [^%\s]+/ix
+
+ def initialize(str)
+ @specs = []
+ @i = 1
+ s = str.to_s
+ return unless /\S/.match(s)
+ @space = true if /\s\z/.match(s)
+ @specs.replace s.scan(REGEX).map {|spec| FormatSpecifier.new(spec) }
+ end
+
+ def to_s
+ @specs.join('')
+ end
+
+ def prune(n=matched_count)
+ n.times { @specs.shift }
+ end
+
+ def spec_count
+ @specs.size
+ end
+
+ def last_spec
+ @i == spec_count - 1
+ end
+
+ def match(str)
+ accum = []
+ @string_left = str
+ @matched_count = 0
+
+ @specs.each_with_index do |spec,i|
+ @i=i
+ @last_spec_tried = spec
+ @last_match_tried = spec.match(@string_left)
+ break unless @last_match_tried
+ @matched_count += 1
+
+ accum << spec.conversion
+
+ @string_left = @last_match_tried.post_match
+ break if @string_left.empty?
+ end
+ return accum.compact
+ end
+ end
+end
+
+class IO
+
+# The trick here is doing a match where you grab one *line*
+# of input at a time. The linebreak may or may not occur
+# at the boundary where the string matches a format specifier.
+# And if it does, some rule about whitespace may or may not
+# be in effect...
+#
+# That's why this is much more elaborate than the string
+# version.
+#
+# For each line:
+# Match succeeds (non-emptily)
+# and the last attempted spec/string sub-match succeeded:
+#
+# could the last spec keep matching?
+# yes: save interim results and continue (next line)
+#
+# The last attempted spec/string did not match:
+#
+# are we on the next-to-last spec in the string?
+# yes:
+# is fmt_string.string_left all spaces?
+# yes: does current spec care about input space?
+# yes: fatal failure
+# no: save interim results and continue
+# no: continue [this state could be analyzed further]
+#
+#
+
+ def scanf(str,&b)
+ return block_scanf(str,&b) if b
+ return [] unless str.size > 0
+
+ start_position = pos rescue 0
+ matched_so_far = 0
+ source_buffer = ""
+ result_buffer = []
+ final_result = []
+
+ fstr = Scanf::FormatString.new(str)
+
+ loop do
+ if eof || (tty? &&! fstr.match(source_buffer))
+ final_result.concat(result_buffer)
+ break
+ end
+
+ source_buffer << gets
+
+ current_match = fstr.match(source_buffer)
+
+ spec = fstr.last_spec_tried
+
+ if spec.matched
+ if spec.mid_match?
+ result_buffer.replace(current_match)
+ next
+ end
+
+ elsif (fstr.matched_count == fstr.spec_count - 1)
+ if /\A\s*\z/.match(fstr.string_left)
+ break if spec.count_space?
+ result_buffer.replace(current_match)
+ next
+ end
+ end
+
+ final_result.concat(current_match)
+
+ matched_so_far += source_buffer.size
+ source_buffer.replace(fstr.string_left)
+ matched_so_far -= source_buffer.size
+ break if fstr.last_spec
+ fstr.prune
+ end
+ seek(start_position + matched_so_far, IO::SEEK_SET) rescue Errno::ESPIPE
+ soak_up_spaces if fstr.last_spec && fstr.space
+
+ return final_result
+ end
+
+ private
+
+ def soak_up_spaces
+ c = getc
+ ungetc(c) if c
+ until eof ||! c || /\S/.match(c.chr)
+ c = getc
+ end
+ ungetc(c) if (c && /\S/.match(c.chr))
+ end
+
+ def block_scanf(str)
+ final = []
+# Sub-ideal, since another FS gets created in scanf.
+# But used here to determine the number of specifiers.
+ fstr = Scanf::FormatString.new(str)
+ last_spec = fstr.last_spec
+ begin
+ current = scanf(str)
+ break if current.empty?
+ final.push(yield(current))
+ end until eof || fstr.last_spec_tried == last_spec
+ return final
+ end
+end
+
+class String
+
+ def scanf(fstr,&b)
+ if b
+ block_scanf(fstr,&b)
+ else
+ fs =
+ if fstr.is_a? Scanf::FormatString
+ fstr
+ else
+ Scanf::FormatString.new(fstr)
+ end
+ fs.match(self)
+ end
+ end
+
+ def block_scanf(fstr,&b)
+ fs = Scanf::FormatString.new(fstr)
+ str = self.dup
+ final = []
+ begin
+ current = str.scanf(fs)
+ final.push(yield(current)) unless current.empty?
+ str = fs.string_left
+ end until current.empty? || str.empty?
+ return final
+ end
+end
+
+module Kernel
+ private
+ def scanf(fs,&b)
+ STDIN.scanf(fs,&b)
+ end
+end
diff --git a/ruby/lib/securerandom.rb b/ruby/lib/securerandom.rb
new file mode 100644
index 0000000..0f7c05b
--- /dev/null
+++ b/ruby/lib/securerandom.rb
@@ -0,0 +1,182 @@
+# = Secure random number generator interface.
+#
+# This library is an interface for secure random number generator which is
+# suitable for generating session key in HTTP cookies, etc.
+#
+# It supports following secure random number generators.
+#
+# * openssl
+# * /dev/urandom
+# * Win32
+#
+# == Example
+#
+# # random hexadecimal string.
+# p SecureRandom.hex(10) #=> "52750b30ffbc7de3b362"
+# p SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559"
+# p SecureRandom.hex(11) #=> "6aca1b5c58e4863e6b81b8"
+# p SecureRandom.hex(12) #=> "94b2fff3e7fd9b9c391a2306"
+# p SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23"
+# ...
+#
+# # random base64 string.
+# p SecureRandom.base64(10) #=> "EcmTPZwWRAozdA=="
+# p SecureRandom.base64(10) #=> "9b0nsevdwNuM/w=="
+# p SecureRandom.base64(10) #=> "KO1nIU+p9DKxGg=="
+# p SecureRandom.base64(11) #=> "l7XEiFja+8EKEtY="
+# p SecureRandom.base64(12) #=> "7kJSM/MzBJI+75j8"
+# p SecureRandom.base64(13) #=> "vKLJ0tXBHqQOuIcSIg=="
+# ...
+#
+# # random binary string.
+# p SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
+# p SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
+# ...
+
+begin
+ require 'openssl'
+rescue LoadError
+end
+
+module SecureRandom
+ # SecureRandom.random_bytes generates a random binary string.
+ #
+ # The argument n specifies the length of the result string.
+ #
+ # If n is not specified, 16 is assumed.
+ # It may be larger in future.
+ #
+ # If secure random number generator is not available,
+ # NotImplementedError is raised.
+ def self.random_bytes(n=nil)
+ n ||= 16
+
+ if defined? OpenSSL::Random
+ return OpenSSL::Random.random_bytes(n)
+ end
+
+ if !defined?(@has_urandom) || @has_urandom
+ flags = File::RDONLY
+ flags |= File::NONBLOCK if defined? File::NONBLOCK
+ flags |= File::NOCTTY if defined? File::NOCTTY
+ flags |= File::NOFOLLOW if defined? File::NOFOLLOW
+ begin
+ File.open("/dev/urandom", flags) {|f|
+ unless f.stat.chardev?
+ raise Errno::ENOENT
+ end
+ @has_urandom = true
+ ret = f.readpartial(n)
+ if ret.length != n
+ raise NotImplementedError, "Unexpected partial read from random device"
+ end
+ return ret
+ }
+ rescue Errno::ENOENT
+ @has_urandom = false
+ end
+ end
+
+ if !defined?(@has_win32)
+ begin
+ require 'Win32API'
+
+ crypt_acquire_context = Win32API.new("advapi32", "CryptAcquireContext", 'PPPII', 'L')
+ @crypt_gen_random = Win32API.new("advapi32", "CryptGenRandom", 'LIP', 'L')
+
+ hProvStr = " " * 4
+ prov_rsa_full = 1
+ crypt_verifycontext = 0xF0000000
+
+ if crypt_acquire_context.call(hProvStr, nil, nil, prov_rsa_full, crypt_verifycontext) == 0
+ raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}"
+ end
+ @hProv, = hProvStr.unpack('L')
+
+ @has_win32 = true
+ rescue LoadError
+ @has_win32 = false
+ end
+ end
+ if @has_win32
+ bytes = " ".force_encoding("ASCII-8BIT") * n
+ if @crypt_gen_random.call(@hProv, bytes.size, bytes) == 0
+ raise SystemCallError, "CryptGenRandom failed: #{lastWin32ErrorMessage}"
+ end
+ return bytes
+ end
+
+ raise NotImplementedError, "No random device"
+ end
+
+ # SecureRandom.hex generates a random hex string.
+ #
+ # The argument n specifies the length of the random length.
+ # The length of the result string is twice of n.
+ #
+ # If n is not specified, 16 is assumed.
+ # It may be larger in future.
+ #
+ # If secure random number generator is not available,
+ # NotImplementedError is raised.
+ def self.hex(n=nil)
+ random_bytes(n).unpack("H*")[0]
+ end
+
+ # SecureRandom.base64 generates a random base64 string.
+ #
+ # The argument n specifies the length of the random length.
+ # The length of the result string is about 4/3 of n.
+ #
+ # If n is not specified, 16 is assumed.
+ # It may be larger in future.
+ #
+ # If secure random number generator is not available,
+ # NotImplementedError is raised.
+ def self.base64(n=nil)
+ [random_bytes(n)].pack("m*").delete("\n")
+ end
+
+ # SecureRandom.random_number generates a random number.
+ #
+ # If an positive integer is given as n,
+ # SecureRandom.random_number returns an integer:
+ # 0 <= SecureRandom.random_number(n) < n.
+ #
+ # If 0 is given or an argument is not given,
+ # SecureRandom.random_number returns an float:
+ # 0.0 <= SecureRandom.random_number() < 1.0.
+ def self.random_number(n=0)
+ if 0 < n
+ hex = n.to_s(16)
+ hex = '0' + hex if (hex.length & 1) == 1
+ bin = [hex].pack("H*")
+ mask = bin[0].ord
+ mask |= mask >> 1
+ mask |= mask >> 2
+ mask |= mask >> 4
+ begin
+ rnd = SecureRandom.random_bytes(bin.length)
+ rnd[0] = (rnd[0].ord & mask).chr
+ end until rnd < bin
+ rnd.unpack("H*")[0].hex
+ else
+ # assumption: Float::MANT_DIG <= 64
+ i64 = SecureRandom.random_bytes(8).unpack("Q")[0]
+ Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG)
+ end
+ end
+
+ # Following code is based on David Garamond's GUID library for Ruby.
+ def self.lastWin32ErrorMessage # :nodoc:
+ get_last_error = Win32API.new("kernel32", "GetLastError", '', 'L')
+ format_message = Win32API.new("kernel32", "FormatMessageA", 'LPLLPLPPPPPPPP', 'L')
+ format_message_ignore_inserts = 0x00000200
+ format_message_from_system = 0x00001000
+
+ code = get_last_error.call
+ msg = "\0" * 1024
+ len = format_message.call(format_message_ignore_inserts + format_message_from_system, 0, code, 0, msg, 1024, nil, nil, nil, nil, nil, nil, nil, nil)
+ msg[0, len].tr("\r", '').chomp
+ end
+end
diff --git a/ruby/lib/set.rb b/ruby/lib/set.rb
new file mode 100644
index 0000000..635652b
--- /dev/null
+++ b/ruby/lib/set.rb
@@ -0,0 +1,1274 @@
+#!/usr/bin/env ruby
+#--
+# set.rb - defines the Set class
+#++
+# Copyright (c) 2002-2008 Akinori MUSHA <knu@iDaemons.org>
+#
+# Documentation by Akinori MUSHA and Gavin Sinclair.
+#
+# All rights reserved. You can redistribute and/or modify it under the same
+# terms as Ruby.
+#
+# $Id: set.rb 18571 2008-08-13 08:03:30Z knu $
+#
+# == Overview
+#
+# This library provides the Set class, which deals with a collection
+# of unordered values with no duplicates. It is a hybrid of Array's
+# intuitive inter-operation facilities and Hash's fast lookup. If you
+# need to keep values ordered, use the SortedSet class.
+#
+# The method +to_set+ is added to Enumerable for convenience.
+#
+# See the Set class for an example of usage.
+
+
+#
+# Set implements a collection of unordered values with no duplicates.
+# This is a hybrid of Array's intuitive inter-operation facilities and
+# Hash's fast lookup.
+#
+# The equality of each couple of elements is determined according to
+# Object#eql? and Object#hash, since Set uses Hash as storage.
+#
+# Set is easy to use with Enumerable objects (implementing +each+).
+# Most of the initializer methods and binary operators accept generic
+# Enumerable objects besides sets and arrays. An Enumerable object
+# can be converted to Set using the +to_set+ method.
+#
+# == Example
+#
+# require 'set'
+# s1 = Set.new [1, 2] # -> #<Set: {1, 2}>
+# s2 = [1, 2].to_set # -> #<Set: {1, 2}>
+# s1 == s2 # -> true
+# s1.add("foo") # -> #<Set: {1, 2, "foo"}>
+# s1.merge([2, 6]) # -> #<Set: {6, 1, 2, "foo"}>
+# s1.subset? s2 # -> false
+# s2.subset? s1 # -> true
+#
+# == Contact
+#
+# - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
+#
+class Set
+ include Enumerable
+
+ # Creates a new set containing the given objects.
+ def self.[](*ary)
+ new(ary)
+ end
+
+ # Creates a new set containing the elements of the given enumerable
+ # object.
+ #
+ # If a block is given, the elements of enum are preprocessed by the
+ # given block.
+ def initialize(enum = nil, &block) # :yields: o
+ @hash ||= Hash.new
+
+ enum.nil? and return
+
+ if block
+ enum.each { |o| add(block[o]) }
+ else
+ merge(enum)
+ end
+ end
+
+ # Copy internal hash.
+ def initialize_copy(orig)
+ @hash = orig.instance_eval{@hash}.dup
+ end
+
+ def freeze # :nodoc:
+ super
+ @hash.freeze
+ self
+ end
+
+ def taint # :nodoc:
+ super
+ @hash.taint
+ self
+ end
+
+ def untaint # :nodoc:
+ super
+ @hash.untaint
+ self
+ end
+
+ # Returns the number of elements.
+ def size
+ @hash.size
+ end
+ alias length size
+
+ # Returns true if the set contains no elements.
+ def empty?
+ @hash.empty?
+ end
+
+ # Removes all elements and returns self.
+ def clear
+ @hash.clear
+ self
+ end
+
+ # Replaces the contents of the set with the contents of the given
+ # enumerable object and returns self.
+ def replace(enum)
+ if enum.class == self.class
+ @hash.replace(enum.instance_eval { @hash })
+ else
+ clear
+ enum.each { |o| add(o) }
+ end
+
+ self
+ end
+
+ # Converts the set to an array. The order of elements is uncertain.
+ def to_a
+ @hash.keys
+ end
+
+ def flatten_merge(set, seen = Set.new)
+ set.each { |e|
+ if e.is_a?(Set)
+ if seen.include?(e_id = e.object_id)
+ raise ArgumentError, "tried to flatten recursive Set"
+ end
+
+ seen.add(e_id)
+ flatten_merge(e, seen)
+ seen.delete(e_id)
+ else
+ add(e)
+ end
+ }
+
+ self
+ end
+ protected :flatten_merge
+
+ # Returns a new set that is a copy of the set, flattening each
+ # containing set recursively.
+ def flatten
+ self.class.new.flatten_merge(self)
+ end
+
+ # Equivalent to Set#flatten, but replaces the receiver with the
+ # result in place. Returns nil if no modifications were made.
+ def flatten!
+ if detect { |e| e.is_a?(Set) }
+ replace(flatten())
+ else
+ nil
+ end
+ end
+
+ # Returns true if the set contains the given object.
+ def include?(o)
+ @hash.include?(o)
+ end
+ alias member? include?
+
+ # Returns true if the set is a superset of the given set.
+ def superset?(set)
+ set.is_a?(Set) or raise ArgumentError, "value must be a set"
+ return false if size < set.size
+ set.all? { |o| include?(o) }
+ end
+
+ # Returns true if the set is a proper superset of the given set.
+ def proper_superset?(set)
+ set.is_a?(Set) or raise ArgumentError, "value must be a set"
+ return false if size <= set.size
+ set.all? { |o| include?(o) }
+ end
+
+ # Returns true if the set is a subset of the given set.
+ def subset?(set)
+ set.is_a?(Set) or raise ArgumentError, "value must be a set"
+ return false if set.size < size
+ all? { |o| set.include?(o) }
+ end
+
+ # Returns true if the set is a proper subset of the given set.
+ def proper_subset?(set)
+ set.is_a?(Set) or raise ArgumentError, "value must be a set"
+ return false if set.size <= size
+ all? { |o| set.include?(o) }
+ end
+
+ # Calls the given block once for each element in the set, passing
+ # the element as parameter. Returns an enumerator if no block is
+ # given.
+ def each
+ block_given? or return enum_for(__method__)
+ @hash.each_key { |o| yield(o) }
+ self
+ end
+
+ # Adds the given object to the set and returns self. Use +merge+ to
+ # add many elements at once.
+ def add(o)
+ @hash[o] = true
+ self
+ end
+ alias << add
+
+ # Adds the given object to the set and returns self. If the
+ # object is already in the set, returns nil.
+ def add?(o)
+ if include?(o)
+ nil
+ else
+ add(o)
+ end
+ end
+
+ # Deletes the given object from the set and returns self. Use +subtract+ to
+ # delete many items at once.
+ def delete(o)
+ @hash.delete(o)
+ self
+ end
+
+ # Deletes the given object from the set and returns self. If the
+ # object is not in the set, returns nil.
+ def delete?(o)
+ if include?(o)
+ delete(o)
+ else
+ nil
+ end
+ end
+
+ # Deletes every element of the set for which block evaluates to
+ # true, and returns self.
+ def delete_if
+ block_given? or return enum_for(__method__)
+ to_a.each { |o| @hash.delete(o) if yield(o) }
+ self
+ end
+
+ # Replaces the elements with ones returned by collect().
+ def collect!
+ block_given? or return enum_for(__method__)
+ set = self.class.new
+ each { |o| set << yield(o) }
+ replace(set)
+ end
+ alias map! collect!
+
+ # Equivalent to Set#delete_if, but returns nil if no changes were
+ # made.
+ def reject!
+ block_given? or return enum_for(__method__)
+ n = size
+ delete_if { |o| yield(o) }
+ size == n ? nil : self
+ end
+
+ # Merges the elements of the given enumerable object to the set and
+ # returns self.
+ def merge(enum)
+ if enum.is_a?(Set)
+ @hash.update(enum.instance_eval { @hash })
+ else
+ enum.each { |o| add(o) }
+ end
+
+ self
+ end
+
+ # Deletes every element that appears in the given enumerable object
+ # and returns self.
+ def subtract(enum)
+ enum.each { |o| delete(o) }
+ self
+ end
+
+ # Returns a new set built by merging the set and the elements of the
+ # given enumerable object.
+ def |(enum)
+ dup.merge(enum)
+ end
+ alias + | ##
+ alias union | ##
+
+ # Returns a new set built by duplicating the set, removing every
+ # element that appears in the given enumerable object.
+ def -(enum)
+ dup.subtract(enum)
+ end
+ alias difference - ##
+
+ # Returns a new set containing elements common to the set and the
+ # given enumerable object.
+ def &(enum)
+ n = self.class.new
+ enum.each { |o| n.add(o) if include?(o) }
+ n
+ end
+ alias intersection & ##
+
+ # Returns a new set containing elements exclusive between the set
+ # and the given enumerable object. (set ^ enum) is equivalent to
+ # ((set | enum) - (set & enum)).
+ def ^(enum)
+ n = Set.new(enum)
+ each { |o| if n.include?(o) then n.delete(o) else n.add(o) end }
+ n
+ end
+
+ # Returns true if two sets are equal. The equality of each couple
+ # of elements is defined according to Object#eql?.
+ def ==(set)
+ equal?(set) and return true
+
+ set.is_a?(Set) && size == set.size or return false
+
+ hash = @hash.dup
+ set.all? { |o| hash.include?(o) }
+ end
+
+ def hash # :nodoc:
+ @hash.hash
+ end
+
+ def eql?(o) # :nodoc:
+ return false unless o.is_a?(Set)
+ @hash.eql?(o.instance_eval{@hash})
+ end
+
+ # Classifies the set by the return value of the given block and
+ # returns a hash of {value => set of elements} pairs. The block is
+ # called once for each element of the set, passing the element as
+ # parameter.
+ #
+ # e.g.:
+ #
+ # require 'set'
+ # files = Set.new(Dir.glob("*.rb"))
+ # hash = files.classify { |f| File.mtime(f).year }
+ # p hash # => {2000=>#<Set: {"a.rb", "b.rb"}>,
+ # # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
+ # # 2002=>#<Set: {"f.rb"}>}
+ def classify # :yields: o
+ block_given? or return enum_for(__method__)
+
+ h = {}
+
+ each { |i|
+ x = yield(i)
+ (h[x] ||= self.class.new).add(i)
+ }
+
+ h
+ end
+
+ # Divides the set into a set of subsets according to the commonality
+ # defined by the given block.
+ #
+ # If the arity of the block is 2, elements o1 and o2 are in common
+ # if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are
+ # in common if block.call(o1) == block.call(o2).
+ #
+ # e.g.:
+ #
+ # require 'set'
+ # numbers = Set[1, 3, 4, 6, 9, 10, 11]
+ # set = numbers.divide { |i,j| (i - j).abs == 1 }
+ # p set # => #<Set: {#<Set: {1}>,
+ # # #<Set: {11, 9, 10}>,
+ # # #<Set: {3, 4}>,
+ # # #<Set: {6}>}>
+ def divide(&func)
+ func or return enum_for(__method__)
+
+ if func.arity == 2
+ require 'tsort'
+
+ class << dig = {} # :nodoc:
+ include TSort
+
+ alias tsort_each_node each_key
+ def tsort_each_child(node, &block)
+ fetch(node).each(&block)
+ end
+ end
+
+ each { |u|
+ dig[u] = a = []
+ each{ |v| func.call(u, v) and a << v }
+ }
+
+ set = Set.new()
+ dig.each_strongly_connected_component { |css|
+ set.add(self.class.new(css))
+ }
+ set
+ else
+ Set.new(classify(&func).values)
+ end
+ end
+
+ InspectKey = :__inspect_key__ # :nodoc:
+
+ # Returns a string containing a human-readable representation of the
+ # set. ("#<Set: {element1, element2, ...}>")
+ def inspect
+ ids = (Thread.current[InspectKey] ||= [])
+
+ if ids.include?(object_id)
+ return sprintf('#<%s: {...}>', self.class.name)
+ end
+
+ begin
+ ids << object_id
+ return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
+ ensure
+ ids.pop
+ end
+ end
+
+ def pretty_print(pp) # :nodoc:
+ pp.text sprintf('#<%s: {', self.class.name)
+ pp.nest(1) {
+ pp.seplist(self) { |o|
+ pp.pp o
+ }
+ }
+ pp.text "}>"
+ end
+
+ def pretty_print_cycle(pp) # :nodoc:
+ pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...')
+ end
+end
+
+# SortedSet implements a set which elements are sorted in order. See Set.
+class SortedSet < Set
+ @@setup = false
+
+ class << self
+ def [](*ary) # :nodoc:
+ new(ary)
+ end
+
+ def setup # :nodoc:
+ @@setup and return
+
+ module_eval {
+ # a hack to shut up warning
+ alias old_init initialize
+ remove_method :old_init
+ }
+ begin
+ require 'rbtree'
+
+ module_eval %{
+ def initialize(*args, &block)
+ @hash = RBTree.new
+ super
+ end
+ }
+ rescue LoadError
+ module_eval %{
+ def initialize(*args, &block)
+ @keys = nil
+ super
+ end
+
+ def clear
+ @keys = nil
+ super
+ end
+
+ def replace(enum)
+ @keys = nil
+ super
+ end
+
+ def add(o)
+ @keys = nil
+ @hash[o] = true
+ self
+ end
+ alias << add
+
+ def delete(o)
+ @keys = nil
+ @hash.delete(o)
+ self
+ end
+
+ def delete_if
+ block_given? or return enum_for(__method__)
+ n = @hash.size
+ super
+ @keys = nil if @hash.size != n
+ self
+ end
+
+ def merge(enum)
+ @keys = nil
+ super
+ end
+
+ def each
+ block_given? or return enum_for(__method__)
+ to_a.each { |o| yield(o) }
+ self
+ end
+
+ def to_a
+ (@keys = @hash.keys).sort! unless @keys
+ @keys
+ end
+ }
+ end
+
+ @@setup = true
+ end
+ end
+
+ def initialize(*args, &block) # :nodoc:
+ SortedSet.setup
+ initialize(*args, &block)
+ end
+end
+
+module Enumerable
+ # Makes a set from the enumerable object with given arguments.
+ # Needs to +require "set"+ to use this method.
+ def to_set(klass = Set, *args, &block)
+ klass.new(self, *args, &block)
+ end
+end
+
+# =begin
+# == RestricedSet class
+# RestricedSet implements a set with restrictions defined by a given
+# block.
+#
+# === Super class
+# Set
+#
+# === Class Methods
+# --- RestricedSet::new(enum = nil) { |o| ... }
+# --- RestricedSet::new(enum = nil) { |rset, o| ... }
+# Creates a new restricted set containing the elements of the given
+# enumerable object. Restrictions are defined by the given block.
+#
+# If the block's arity is 2, it is called with the RestrictedSet
+# itself and an object to see if the object is allowed to be put in
+# the set.
+#
+# Otherwise, the block is called with an object to see if the object
+# is allowed to be put in the set.
+#
+# === Instance Methods
+# --- restriction_proc
+# Returns the restriction procedure of the set.
+#
+# =end
+#
+# class RestricedSet < Set
+# def initialize(*args, &block)
+# @proc = block or raise ArgumentError, "missing a block"
+#
+# if @proc.arity == 2
+# instance_eval %{
+# def add(o)
+# @hash[o] = true if @proc.call(self, o)
+# self
+# end
+# alias << add
+#
+# def add?(o)
+# if include?(o) || !@proc.call(self, o)
+# nil
+# else
+# @hash[o] = true
+# self
+# end
+# end
+#
+# def replace(enum)
+# clear
+# enum.each { |o| add(o) }
+#
+# self
+# end
+#
+# def merge(enum)
+# enum.each { |o| add(o) }
+#
+# self
+# end
+# }
+# else
+# instance_eval %{
+# def add(o)
+# if @proc.call(o)
+# @hash[o] = true
+# end
+# self
+# end
+# alias << add
+#
+# def add?(o)
+# if include?(o) || !@proc.call(o)
+# nil
+# else
+# @hash[o] = true
+# self
+# end
+# end
+# }
+# end
+#
+# super(*args)
+# end
+#
+# def restriction_proc
+# @proc
+# end
+# end
+
+if $0 == __FILE__
+ eval DATA.read, nil, $0, __LINE__+4
+end
+
+__END__
+
+require 'test/unit'
+
+class TC_Set < Test::Unit::TestCase
+ def test_aref
+ assert_nothing_raised {
+ Set[]
+ Set[nil]
+ Set[1,2,3]
+ }
+
+ assert_equal(0, Set[].size)
+ assert_equal(1, Set[nil].size)
+ assert_equal(1, Set[[]].size)
+ assert_equal(1, Set[[nil]].size)
+
+ set = Set[2,4,6,4]
+ assert_equal(Set.new([2,4,6]), set)
+ end
+
+ def test_s_new
+ assert_nothing_raised {
+ Set.new()
+ Set.new(nil)
+ Set.new([])
+ Set.new([1,2])
+ Set.new('a'..'c')
+ }
+ assert_raises(NoMethodError) {
+ Set.new(false)
+ }
+ assert_raises(NoMethodError) {
+ Set.new(1)
+ }
+ assert_raises(ArgumentError) {
+ Set.new(1,2)
+ }
+
+ assert_equal(0, Set.new().size)
+ assert_equal(0, Set.new(nil).size)
+ assert_equal(0, Set.new([]).size)
+ assert_equal(1, Set.new([nil]).size)
+
+ ary = [2,4,6,4]
+ set = Set.new(ary)
+ ary.clear
+ assert_equal(false, set.empty?)
+ assert_equal(3, set.size)
+
+ ary = [1,2,3]
+
+ s = Set.new(ary) { |o| o * 2 }
+ assert_equal([2,4,6], s.sort)
+ end
+
+ def test_clone
+ set1 = Set.new
+ set2 = set1.clone
+ set1 << 'abc'
+ assert_equal(Set.new, set2)
+ end
+
+ def test_dup
+ set1 = Set[1,2]
+ set2 = set1.dup
+
+ assert_not_same(set1, set2)
+
+ assert_equal(set1, set2)
+
+ set1.add(3)
+
+ assert_not_equal(set1, set2)
+ end
+
+ def test_size
+ assert_equal(0, Set[].size)
+ assert_equal(2, Set[1,2].size)
+ assert_equal(2, Set[1,2,1].size)
+ end
+
+ def test_empty?
+ assert_equal(true, Set[].empty?)
+ assert_equal(false, Set[1, 2].empty?)
+ end
+
+ def test_clear
+ set = Set[1,2]
+ ret = set.clear
+
+ assert_same(set, ret)
+ assert_equal(true, set.empty?)
+ end
+
+ def test_replace
+ set = Set[1,2]
+ ret = set.replace('a'..'c')
+
+ assert_same(set, ret)
+ assert_equal(Set['a','b','c'], set)
+ end
+
+ def test_to_a
+ set = Set[1,2,3,2]
+ ary = set.to_a
+
+ assert_equal([1,2,3], ary.sort)
+ end
+
+ def test_flatten
+ # test1
+ set1 = Set[
+ 1,
+ Set[
+ 5,
+ Set[7,
+ Set[0]
+ ],
+ Set[6,2],
+ 1
+ ],
+ 3,
+ Set[3,4]
+ ]
+
+ set2 = set1.flatten
+ set3 = Set.new(0..7)
+
+ assert_not_same(set2, set1)
+ assert_equal(set3, set2)
+
+ # test2; destructive
+ orig_set1 = set1
+ set1.flatten!
+
+ assert_same(orig_set1, set1)
+ assert_equal(set3, set1)
+
+ # test3; multiple occurrences of a set in an set
+ set1 = Set[1, 2]
+ set2 = Set[set1, Set[set1, 4], 3]
+
+ assert_nothing_raised {
+ set2.flatten!
+ }
+
+ assert_equal(Set.new(1..4), set2)
+
+ # test4; recursion
+ set2 = Set[]
+ set1 = Set[1, set2]
+ set2.add(set1)
+
+ assert_raises(ArgumentError) {
+ set1.flatten!
+ }
+
+ # test5; miscellaneous
+ empty = Set[]
+ set = Set[Set[empty, "a"],Set[empty, "b"]]
+
+ assert_nothing_raised {
+ set.flatten
+ }
+
+ set1 = empty.merge(Set["no_more", set])
+
+ assert_nil(Set.new(0..31).flatten!)
+
+ x = Set[Set[],Set[1,2]].flatten!
+ y = Set[1,2]
+
+ assert_equal(x, y)
+ end
+
+ def test_include?
+ set = Set[1,2,3]
+
+ assert_equal(true, set.include?(1))
+ assert_equal(true, set.include?(2))
+ assert_equal(true, set.include?(3))
+ assert_equal(false, set.include?(0))
+ assert_equal(false, set.include?(nil))
+
+ set = Set["1",nil,"2",nil,"0","1",false]
+ assert_equal(true, set.include?(nil))
+ assert_equal(true, set.include?(false))
+ assert_equal(true, set.include?("1"))
+ assert_equal(false, set.include?(0))
+ assert_equal(false, set.include?(true))
+ end
+
+ def test_superset?
+ set = Set[1,2,3]
+
+ assert_raises(ArgumentError) {
+ set.superset?()
+ }
+
+ assert_raises(ArgumentError) {
+ set.superset?(2)
+ }
+
+ assert_raises(ArgumentError) {
+ set.superset?([2])
+ }
+
+ assert_equal(true, set.superset?(Set[]))
+ assert_equal(true, set.superset?(Set[1,2]))
+ assert_equal(true, set.superset?(Set[1,2,3]))
+ assert_equal(false, set.superset?(Set[1,2,3,4]))
+ assert_equal(false, set.superset?(Set[1,4]))
+
+ assert_equal(true, Set[].superset?(Set[]))
+ end
+
+ def test_proper_superset?
+ set = Set[1,2,3]
+
+ assert_raises(ArgumentError) {
+ set.proper_superset?()
+ }
+
+ assert_raises(ArgumentError) {
+ set.proper_superset?(2)
+ }
+
+ assert_raises(ArgumentError) {
+ set.proper_superset?([2])
+ }
+
+ assert_equal(true, set.proper_superset?(Set[]))
+ assert_equal(true, set.proper_superset?(Set[1,2]))
+ assert_equal(false, set.proper_superset?(Set[1,2,3]))
+ assert_equal(false, set.proper_superset?(Set[1,2,3,4]))
+ assert_equal(false, set.proper_superset?(Set[1,4]))
+
+ assert_equal(false, Set[].proper_superset?(Set[]))
+ end
+
+ def test_subset?
+ set = Set[1,2,3]
+
+ assert_raises(ArgumentError) {
+ set.subset?()
+ }
+
+ assert_raises(ArgumentError) {
+ set.subset?(2)
+ }
+
+ assert_raises(ArgumentError) {
+ set.subset?([2])
+ }
+
+ assert_equal(true, set.subset?(Set[1,2,3,4]))
+ assert_equal(true, set.subset?(Set[1,2,3]))
+ assert_equal(false, set.subset?(Set[1,2]))
+ assert_equal(false, set.subset?(Set[]))
+
+ assert_equal(true, Set[].subset?(Set[1]))
+ assert_equal(true, Set[].subset?(Set[]))
+ end
+
+ def test_proper_subset?
+ set = Set[1,2,3]
+
+ assert_raises(ArgumentError) {
+ set.proper_subset?()
+ }
+
+ assert_raises(ArgumentError) {
+ set.proper_subset?(2)
+ }
+
+ assert_raises(ArgumentError) {
+ set.proper_subset?([2])
+ }
+
+ assert_equal(true, set.proper_subset?(Set[1,2,3,4]))
+ assert_equal(false, set.proper_subset?(Set[1,2,3]))
+ assert_equal(false, set.proper_subset?(Set[1,2]))
+ assert_equal(false, set.proper_subset?(Set[]))
+
+ assert_equal(false, Set[].proper_subset?(Set[]))
+ end
+
+ def test_each
+ ary = [1,3,5,7,10,20]
+ set = Set.new(ary)
+
+ ret = set.each { |o| }
+ assert_same(set, ret)
+
+ e = set.each
+ assert_instance_of(Enumerator, e)
+
+ assert_nothing_raised {
+ set.each { |o|
+ ary.delete(o) or raise "unexpected element: #{o}"
+ }
+
+ ary.empty? or raise "forgotten elements: #{ary.join(', ')}"
+ }
+ end
+
+ def test_add
+ set = Set[1,2,3]
+
+ ret = set.add(2)
+ assert_same(set, ret)
+ assert_equal(Set[1,2,3], set)
+
+ ret = set.add?(2)
+ assert_nil(ret)
+ assert_equal(Set[1,2,3], set)
+
+ ret = set.add(4)
+ assert_same(set, ret)
+ assert_equal(Set[1,2,3,4], set)
+
+ ret = set.add?(5)
+ assert_same(set, ret)
+ assert_equal(Set[1,2,3,4,5], set)
+ end
+
+ def test_delete
+ set = Set[1,2,3]
+
+ ret = set.delete(4)
+ assert_same(set, ret)
+ assert_equal(Set[1,2,3], set)
+
+ ret = set.delete?(4)
+ assert_nil(ret)
+ assert_equal(Set[1,2,3], set)
+
+ ret = set.delete(2)
+ assert_equal(set, ret)
+ assert_equal(Set[1,3], set)
+
+ ret = set.delete?(1)
+ assert_equal(set, ret)
+ assert_equal(Set[3], set)
+ end
+
+ def test_delete_if
+ set = Set.new(1..10)
+ ret = set.delete_if { |i| i > 10 }
+ assert_same(set, ret)
+ assert_equal(Set.new(1..10), set)
+
+ set = Set.new(1..10)
+ ret = set.delete_if { |i| i % 3 == 0 }
+ assert_same(set, ret)
+ assert_equal(Set[1,2,4,5,7,8,10], set)
+ end
+
+ def test_collect!
+ set = Set[1,2,3,'a','b','c',-1..1,2..4]
+
+ ret = set.collect! { |i|
+ case i
+ when Numeric
+ i * 2
+ when String
+ i.upcase
+ else
+ nil
+ end
+ }
+
+ assert_same(set, ret)
+ assert_equal(Set[2,4,6,'A','B','C',nil], set)
+ end
+
+ def test_reject!
+ set = Set.new(1..10)
+
+ ret = set.reject! { |i| i > 10 }
+ assert_nil(ret)
+ assert_equal(Set.new(1..10), set)
+
+ ret = set.reject! { |i| i % 3 == 0 }
+ assert_same(set, ret)
+ assert_equal(Set[1,2,4,5,7,8,10], set)
+ end
+
+ def test_merge
+ set = Set[1,2,3]
+
+ ret = set.merge([2,4,6])
+ assert_same(set, ret)
+ assert_equal(Set[1,2,3,4,6], set)
+ end
+
+ def test_subtract
+ set = Set[1,2,3]
+
+ ret = set.subtract([2,4,6])
+ assert_same(set, ret)
+ assert_equal(Set[1,3], set)
+ end
+
+ def test_plus
+ set = Set[1,2,3]
+
+ ret = set + [2,4,6]
+ assert_not_same(set, ret)
+ assert_equal(Set[1,2,3,4,6], ret)
+ end
+
+ def test_minus
+ set = Set[1,2,3]
+
+ ret = set - [2,4,6]
+ assert_not_same(set, ret)
+ assert_equal(Set[1,3], ret)
+ end
+
+ def test_and
+ set = Set[1,2,3,4]
+
+ ret = set & [2,4,6]
+ assert_not_same(set, ret)
+ assert_equal(Set[2,4], ret)
+ end
+
+ def test_xor
+ set = Set[1,2,3,4]
+ ret = set ^ [2,4,5,5]
+ assert_not_same(set, ret)
+ assert_equal(Set[1,3,5], ret)
+ end
+
+ def test_eq
+ set1 = Set[2,3,1]
+ set2 = Set[1,2,3]
+
+ assert_equal(set1, set1)
+ assert_equal(set1, set2)
+ assert_not_equal(Set[1], [1])
+
+ set1 = Class.new(Set)["a", "b"]
+ set2 = Set["a", "b", set1]
+ set1 = set1.add(set1.clone)
+
+# assert_equal(set1, set2)
+# assert_equal(set2, set1)
+ assert_equal(set2, set2.clone)
+ assert_equal(set1.clone, set1)
+
+ assert_not_equal(Set[Exception.new,nil], Set[Exception.new,Exception.new], "[ruby-dev:26127]")
+ end
+
+ # def test_hash
+ # end
+
+ # def test_eql?
+ # end
+
+ def test_classify
+ set = Set.new(1..10)
+ ret = set.classify { |i| i % 3 }
+
+ assert_equal(3, ret.size)
+ assert_instance_of(Hash, ret)
+ ret.each_value { |value| assert_instance_of(Set, value) }
+ assert_equal(Set[3,6,9], ret[0])
+ assert_equal(Set[1,4,7,10], ret[1])
+ assert_equal(Set[2,5,8], ret[2])
+ end
+
+ def test_divide
+ set = Set.new(1..10)
+ ret = set.divide { |i| i % 3 }
+
+ assert_equal(3, ret.size)
+ n = 0
+ ret.each { |s| n += s.size }
+ assert_equal(set.size, n)
+ assert_equal(set, ret.flatten)
+
+ set = Set[7,10,5,11,1,3,4,9,0]
+ ret = set.divide { |a,b| (a - b).abs == 1 }
+
+ assert_equal(4, ret.size)
+ n = 0
+ ret.each { |s| n += s.size }
+ assert_equal(set.size, n)
+ assert_equal(set, ret.flatten)
+ ret.each { |s|
+ if s.include?(0)
+ assert_equal(Set[0,1], s)
+ elsif s.include?(3)
+ assert_equal(Set[3,4,5], s)
+ elsif s.include?(7)
+ assert_equal(Set[7], s)
+ elsif s.include?(9)
+ assert_equal(Set[9,10,11], s)
+ else
+ raise "unexpected group: #{s.inspect}"
+ end
+ }
+ end
+
+ def test_inspect
+ set1 = Set[1]
+
+ assert_equal('#<Set: {1}>', set1.inspect)
+
+ set2 = Set[Set[0], 1, 2, set1]
+ assert_equal(false, set2.inspect.include?('#<Set: {...}>'))
+
+ set1.add(set2)
+ assert_equal(true, set1.inspect.include?('#<Set: {...}>'))
+ end
+
+ # def test_pretty_print
+ # end
+
+ # def test_pretty_print_cycle
+ # end
+end
+
+class TC_SortedSet < Test::Unit::TestCase
+ def test_sortedset
+ s = SortedSet[4,5,3,1,2]
+
+ assert_equal([1,2,3,4,5], s.to_a)
+
+ prev = nil
+ s.each { |o| assert(prev < o) if prev; prev = o }
+ assert_not_nil(prev)
+
+ s.map! { |o| -2 * o }
+
+ assert_equal([-10,-8,-6,-4,-2], s.to_a)
+
+ prev = nil
+ ret = s.each { |o| assert(prev < o) if prev; prev = o }
+ assert_not_nil(prev)
+ assert_same(s, ret)
+
+ s = SortedSet.new([2,1,3]) { |o| o * -2 }
+ assert_equal([-6,-4,-2], s.to_a)
+
+ s = SortedSet.new(['one', 'two', 'three', 'four'])
+ a = []
+ ret = s.delete_if { |o| a << o; o.start_with?('t') }
+ assert_same(s, ret)
+ assert_equal(['four', 'one'], s.to_a)
+ assert_equal(['four', 'one', 'three', 'two'], a)
+
+ s = SortedSet.new(['one', 'two', 'three', 'four'])
+ a = []
+ ret = s.reject! { |o| a << o; o.start_with?('t') }
+ assert_same(s, ret)
+ assert_equal(['four', 'one'], s.to_a)
+ assert_equal(['four', 'one', 'three', 'two'], a)
+
+ s = SortedSet.new(['one', 'two', 'three', 'four'])
+ a = []
+ ret = s.reject! { |o| a << o; false }
+ assert_same(nil, ret)
+ assert_equal(['four', 'one', 'three', 'two'], s.to_a)
+ assert_equal(['four', 'one', 'three', 'two'], a)
+ end
+end
+
+class TC_Enumerable < Test::Unit::TestCase
+ def test_to_set
+ ary = [2,5,4,3,2,1,3]
+
+ set = ary.to_set
+ assert_instance_of(Set, set)
+ assert_equal([1,2,3,4,5], set.sort)
+
+ set = ary.to_set { |o| o * -2 }
+ assert_instance_of(Set, set)
+ assert_equal([-10,-8,-6,-4,-2], set.sort)
+
+ set = ary.to_set(SortedSet)
+ assert_instance_of(SortedSet, set)
+ assert_equal([1,2,3,4,5], set.to_a)
+
+ set = ary.to_set(SortedSet) { |o| o * -2 }
+ assert_instance_of(SortedSet, set)
+ assert_equal([-10,-8,-6,-4,-2], set.sort)
+ end
+end
+
+# class TC_RestricedSet < Test::Unit::TestCase
+# def test_s_new
+# assert_raises(ArgumentError) { RestricedSet.new }
+#
+# s = RestricedSet.new([-1,2,3]) { |o| o > 0 }
+# assert_equal([2,3], s.sort)
+# end
+#
+# def test_restriction_proc
+# s = RestricedSet.new([-1,2,3]) { |o| o > 0 }
+#
+# f = s.restriction_proc
+# assert_instance_of(Proc, f)
+# assert(f[1])
+# assert(!f[0])
+# end
+#
+# def test_replace
+# s = RestricedSet.new(-3..3) { |o| o > 0 }
+# assert_equal([1,2,3], s.sort)
+#
+# s.replace([-2,0,3,4,5])
+# assert_equal([3,4,5], s.sort)
+# end
+#
+# def test_merge
+# s = RestricedSet.new { |o| o > 0 }
+# s.merge(-5..5)
+# assert_equal([1,2,3,4,5], s.sort)
+#
+# s.merge([10,-10,-8,8])
+# assert_equal([1,2,3,4,5,8,10], s.sort)
+# end
+# end
diff --git a/ruby/lib/shell.rb b/ruby/lib/shell.rb
new file mode 100644
index 0000000..6a64cb2
--- /dev/null
+++ b/ruby/lib/shell.rb
@@ -0,0 +1,300 @@
+#
+# shell.rb -
+# $Release Version: 0.7 $
+# $Revision: 1.9 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+
+require "thread" unless defined?(Mutex)
+
+require "forwardable"
+
+require "shell/error"
+require "shell/command-processor"
+require "shell/process-controller"
+
+class Shell
+ @RCS_ID='-$Id: shell.rb,v 1.9 2002/03/04 12:01:10 keiju Exp keiju $-'
+
+ include Error
+ extend Exception2MessageMapper
+
+# @cascade = true
+ # debug: true -> normal debug
+ # debug: 1 -> eval definition debug
+ # debug: 2 -> detail inspect debug
+ @debug = false
+ @verbose = true
+
+ @debug_display_process_id = false
+ @debug_display_thread_id = true
+ @debug_output_mutex = Mutex.new
+
+ class << Shell
+ extend Forwardable
+
+ attr_accessor :cascade, :debug, :verbose
+
+# alias cascade? cascade
+ alias debug? debug
+ alias verbose? verbose
+ @verbose = true
+
+ def debug=(val)
+ @debug = val
+ @verbose = val if val
+ end
+
+ def cd(path)
+ new(path)
+ end
+
+ def default_system_path
+ if @default_system_path
+ @default_system_path
+ else
+ ENV["PATH"].split(":")
+ end
+ end
+
+ def default_system_path=(path)
+ @default_system_path = path
+ end
+
+ def default_record_separator
+ if @default_record_separator
+ @default_record_separator
+ else
+ $/
+ end
+ end
+
+ def default_record_separator=(rs)
+ @default_record_separator = rs
+ end
+
+ # os resource mutex
+ mutex_methods = ["unlock", "lock", "locked?", "synchronize", "try_lock", "exclusive_unlock"]
+ for m in mutex_methods
+ def_delegator("@debug_output_mutex", m, "debug_output_"+m.to_s)
+ end
+
+ end
+
+ def initialize(pwd = Dir.pwd, umask = nil)
+ @cwd = File.expand_path(pwd)
+ @dir_stack = []
+ @umask = umask
+
+ @system_path = Shell.default_system_path
+ @record_separator = Shell.default_record_separator
+
+ @command_processor = CommandProcessor.new(self)
+ @process_controller = ProcessController.new(self)
+
+ @verbose = Shell.verbose
+ @debug = Shell.debug
+ end
+
+ attr_reader :system_path
+
+ def system_path=(path)
+ @system_path = path
+ rehash
+ end
+
+ attr_accessor :umask, :record_separator
+ attr_accessor :verbose, :debug
+
+ def debug=(val)
+ @debug = val
+ @verbose = val if val
+ end
+
+ alias verbose? verbose
+ alias debug? debug
+
+ attr_reader :command_processor
+ attr_reader :process_controller
+
+ def expand_path(path)
+ File.expand_path(path, @cwd)
+ end
+
+ # Most Shell commands are defined via CommandProcessor
+
+ #
+ # Dir related methods
+ #
+ # Shell#cwd/dir/getwd/pwd
+ # Shell#chdir/cd
+ # Shell#pushdir/pushd
+ # Shell#popdir/popd
+ # Shell#mkdir
+ # Shell#rmdir
+
+ attr_reader :cwd
+ alias dir cwd
+ alias getwd cwd
+ alias pwd cwd
+
+ attr_reader :dir_stack
+ alias dirs dir_stack
+
+ # If called as iterator, it restores the current directory when the
+ # block ends.
+ def chdir(path = nil, verbose = @verbose)
+ check_point
+
+ if iterator?
+ notify("chdir(with block) #{path}") if verbose
+ cwd_old = @cwd
+ begin
+ chdir(path, nil)
+ yield
+ ensure
+ chdir(cwd_old, nil)
+ end
+ else
+ notify("chdir #{path}") if verbose
+ path = "~" unless path
+ @cwd = expand_path(path)
+ notify "current dir: #{@cwd}"
+ rehash
+ Void.new(self)
+ end
+ end
+ alias cd chdir
+
+ def pushdir(path = nil, verbose = @verbose)
+ check_point
+
+ if iterator?
+ notify("pushdir(with block) #{path}") if verbose
+ pushdir(path, nil)
+ begin
+ yield
+ ensure
+ popdir
+ end
+ elsif path
+ notify("pushdir #{path}") if verbose
+ @dir_stack.push @cwd
+ chdir(path, nil)
+ notify "dir stack: [#{@dir_stack.join ', '}]"
+ self
+ else
+ notify("pushdir") if verbose
+ if pop = @dir_stack.pop
+ @dir_stack.push @cwd
+ chdir pop
+ notify "dir stack: [#{@dir_stack.join ', '}]"
+ self
+ else
+ Shell.Fail DirStackEmpty
+ end
+ end
+ Void.new(self)
+ end
+ alias pushd pushdir
+
+ def popdir
+ check_point
+
+ notify("popdir")
+ if pop = @dir_stack.pop
+ chdir pop
+ notify "dir stack: [#{@dir_stack.join ', '}]"
+ self
+ else
+ Shell.Fail DirStackEmpty
+ end
+ Void.new(self)
+ end
+ alias popd popdir
+
+ #
+ # process management
+ #
+ def jobs
+ @process_controller.jobs
+ end
+
+ def kill(sig, command)
+ @process_controller.kill_job(sig, command)
+ end
+
+ #
+ # command definitions
+ #
+ def Shell.def_system_command(command, path = command)
+ CommandProcessor.def_system_command(command, path)
+ end
+
+ def Shell.undef_system_command(command)
+ CommandProcessor.undef_system_command(command)
+ end
+
+ def Shell.alias_command(ali, command, *opts, &block)
+ CommandProcessor.alias_command(ali, command, *opts, &block)
+ end
+
+ def Shell.unalias_command(ali)
+ CommandProcessor.unalias_command(ali)
+ end
+
+ def Shell.install_system_commands(pre = "sys_")
+ CommandProcessor.install_system_commands(pre)
+ end
+
+ #
+ def inspect
+ if debug.kind_of?(Integer) && debug > 2
+ super
+ else
+ to_s
+ end
+ end
+
+ def self.notify(*opts, &block)
+ Shell::debug_output_synchronize do
+ if opts[-1].kind_of?(String)
+ yorn = verbose?
+ else
+ yorn = opts.pop
+ end
+ return unless yorn
+
+ if @debug_display_thread_id
+ if @debug_display_process_id
+ prefix = "shell(##{Process.pid}:#{Thread.current.to_s.sub("Thread", "Th")}): "
+ else
+ prefix = "shell(#{Thread.current.to_s.sub("Thread", "Th")}): "
+ end
+ else
+ prefix = "shell: "
+ end
+ _head = true
+ STDERR.print opts.collect{|mes|
+ mes = mes.dup
+ yield mes if iterator?
+ if _head
+ _head = false
+# "shell" " + mes
+ prefix + mes
+ else
+ " "* prefix.size + mes
+ end
+ }.join("\n")+"\n"
+ end
+ end
+
+ CommandProcessor.initialize
+ CommandProcessor.run_config
+end
diff --git a/ruby/lib/shell/builtin-command.rb b/ruby/lib/shell/builtin-command.rb
new file mode 100644
index 0000000..270c65c
--- /dev/null
+++ b/ruby/lib/shell/builtin-command.rb
@@ -0,0 +1,160 @@
+#
+# shell/builtin-command.rb -
+# $Release Version: 0.7 $
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "shell/filter"
+
+class Shell
+ class BuiltInCommand<Filter
+ def wait?
+ false
+ end
+ def active?
+ true
+ end
+ end
+
+ class Void < BuiltInCommand
+ def initialize(sh, *opts)
+ super sh
+ end
+
+ def each(rs = nil)
+ # do nothing
+ end
+ end
+
+ class Echo < BuiltInCommand
+ def initialize(sh, *strings)
+ super sh
+ @strings = strings
+ end
+
+ def each(rs = nil)
+ rs = @shell.record_separator unless rs
+ for str in @strings
+ yield str + rs
+ end
+ end
+ end
+
+ class Cat < BuiltInCommand
+ def initialize(sh, *filenames)
+ super sh
+ @cat_files = filenames
+ end
+
+ def each(rs = nil)
+ if @cat_files.empty?
+ super
+ else
+ for src in @cat_files
+ @shell.foreach(src, rs){|l| yield l}
+ end
+ end
+ end
+ end
+
+ class Glob < BuiltInCommand
+ def initialize(sh, pattern)
+ super sh
+
+ @pattern = pattern
+ end
+
+ def each(rs = nil)
+ if @pattern[0] == ?/
+ @files = Dir[@pattern]
+ else
+ prefix = @shell.pwd+"/"
+ @files = Dir[prefix+@pattern].collect{|p| p.sub(prefix, "")}
+ end
+ rs = @shell.record_separator unless rs
+ for f in @files
+ yield f+rs
+ end
+ end
+ end
+
+# class Sort < Cat
+# def initialize(sh, *filenames)
+# super
+# end
+#
+# def each(rs = nil)
+# ary = []
+# super{|l| ary.push l}
+# for l in ary.sort!
+# yield l
+# end
+# end
+# end
+
+ class AppendIO < BuiltInCommand
+ def initialize(sh, io, filter)
+ super sh
+ @input = filter
+ @io = io
+ end
+
+ def input=(filter)
+ @input.input=filter
+ for l in @input
+ @io << l
+ end
+ end
+
+ end
+
+ class AppendFile < AppendIO
+ def initialize(sh, to_filename, filter)
+ @file_name = to_filename
+ io = sh.open(to_filename, "a")
+ super(sh, io, filter)
+ end
+
+ def input=(filter)
+ begin
+ super
+ ensure
+ @io.close
+ end
+ end
+ end
+
+ class Tee < BuiltInCommand
+ def initialize(sh, filename)
+ super sh
+ @to_filename = filename
+ end
+
+ def each(rs = nil)
+ to = @shell.open(@to_filename, "w")
+ begin
+ super{|l| to << l; yield l}
+ ensure
+ to.close
+ end
+ end
+ end
+
+ class Concat < BuiltInCommand
+ def initialize(sh, *jobs)
+ super(sh)
+ @jobs = jobs
+ end
+
+ def each(rs = nil)
+ while job = @jobs.shift
+ job.each{|l| yield l}
+ end
+ end
+ end
+end
diff --git a/ruby/lib/shell/command-processor.rb b/ruby/lib/shell/command-processor.rb
new file mode 100644
index 0000000..cdc63d7
--- /dev/null
+++ b/ruby/lib/shell/command-processor.rb
@@ -0,0 +1,593 @@
+#
+# shell/command-controller.rb -
+# $Release Version: 0.7 $
+# $Revision: 20880 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+require "thread"
+
+require "shell/error"
+require "shell/filter"
+require "shell/system-command"
+require "shell/builtin-command"
+
+class Shell
+ class CommandProcessor
+# include Error
+
+ #
+ # initialize of Shell and related classes.
+ #
+ m = [:initialize, :expand_path]
+ if Object.methods.first.kind_of?(String)
+ NoDelegateMethods = m.collect{|x| x.id2name}
+ else
+ NoDelegateMethods = m
+ end
+
+ def self.initialize
+
+ install_builtin_commands
+
+ # define CommandProccessor#methods to Shell#methods and Filter#methods
+ for m in CommandProcessor.instance_methods(false) - NoDelegateMethods
+ add_delegate_command_to_shell(m)
+ end
+
+ def self.method_added(id)
+ add_delegate_command_to_shell(id)
+ end
+ end
+
+ #
+ # include run file.
+ #
+ def self.run_config
+ begin
+ load File.expand_path("~/.rb_shell") if ENV.key?("HOME")
+ rescue LoadError, Errno::ENOENT
+ rescue
+ print "load error: #{rc}\n"
+ print $!.class, ": ", $!, "\n"
+ for err in $@[0, $@.size - 2]
+ print "\t", err, "\n"
+ end
+ end
+ end
+
+ def initialize(shell)
+ @shell = shell
+ @system_commands = {}
+ end
+
+ #
+ # CommandProcessor#expand_path(path)
+ # path: String
+ # return: String
+ # returns the absolute path for <path>
+ #
+ def expand_path(path)
+ @shell.expand_path(path)
+ end
+
+ #
+ # File related commands
+ # Shell#foreach
+ # Shell#open
+ # Shell#unlink
+ # Shell#test
+ #
+ # -
+ #
+ # CommandProcessor#foreach(path, rs)
+ # path: String
+ # rs: String - record separator
+ # iterator
+ # Same as:
+ # File#foreach (when path is file)
+ # Dir#foreach (when path is directory)
+ # path is relative to pwd
+ #
+ def foreach(path = nil, *rs)
+ path = "." unless path
+ path = expand_path(path)
+
+ if File.directory?(path)
+ Dir.foreach(path){|fn| yield fn}
+ else
+ IO.foreach(path, *rs){|l| yield l}
+ end
+ end
+
+ #
+ # CommandProcessor#open(path, mode)
+ # path: String
+ # mode: String
+ # return: File or Dir
+ # Same as:
+ # File#open (when path is file)
+ # Dir#open (when path is directory)
+ # mode has an effect only when path is a file
+ #
+ def open(path, mode = nil, perm = 0666, &b)
+ path = expand_path(path)
+ if File.directory?(path)
+ Dir.open(path, &b)
+ else
+ if @shell.umask
+ f = File.open(path, mode, perm)
+ File.chmod(perm & ~@shell.umask, path)
+ if block_given?
+ f.each(&b)
+ end
+ f
+ else
+ f = File.open(path, mode, perm, &b)
+ end
+ end
+ end
+ # public :open
+
+ #
+ # CommandProcessor#unlink(path)
+ # same as:
+ # Dir#unlink (when path is directory)
+ # File#unlink (when path is file)
+ #
+ def unlink(path)
+ @shell.check_point
+
+ path = expand_path(path)
+ if File.directory?(path)
+ Dir.unlink(path)
+ else
+ IO.unlink(path)
+ end
+ Void.new(@shell)
+ end
+
+ #
+ # CommandProcessor#test(command, file1, file2)
+ # CommandProcessor#[command, file1, file2]
+ # command: char or String or Symbol
+ # file1: String
+ # file2: String(optional)
+ # return: Boolean
+ # same as:
+ # test() (when command is char or length 1 string or symbol)
+ # FileTest.command (others)
+ # example:
+ # sh[?e, "foo"]
+ # sh[:e, "foo"]
+ # sh["e", "foo"]
+ # sh[:exists?, "foo"]
+ # sh["exists?", "foo"]
+ #
+ alias top_level_test test
+ def test(command, file1, file2=nil)
+ file1 = expand_path(file1)
+ file2 = expand_path(file2) if file2
+ command = command.id2name if command.kind_of?(Symbol)
+
+ case command
+ when Integer
+ if file2
+ top_level_test(command, file1, file2)
+ else
+ top_level_test(command, file1)
+ end
+ when String
+ if command.size == 1
+ if file2
+ top_level_test(command, file1, file2)
+ else
+ top_level_test(command, file1)
+ end
+ else
+ if file2
+ FileTest.send(command, file1, file2)
+ else
+ FileTest.send(command, file1)
+ end
+ end
+ end
+ end
+ alias [] test
+
+ #
+ # Dir related methods
+ #
+ # Shell#mkdir
+ # Shell#rmdir
+ #
+ #--
+ #
+ # CommandProcessor#mkdir(*path)
+ # path: String
+ # same as Dir.mkdir()
+ #
+ def mkdir(*path)
+ @shell.check_point
+ notify("mkdir #{path.join(' ')}")
+
+ perm = nil
+ if path.last.kind_of?(Integer)
+ perm = path.pop
+ end
+ for dir in path
+ d = expand_path(dir)
+ if perm
+ Dir.mkdir(d, perm)
+ else
+ Dir.mkdir(d)
+ end
+ File.chmod(d, 0666 & ~@shell.umask) if @shell.umask
+ end
+ Void.new(@shell)
+ end
+
+ #
+ # CommandProcessor#rmdir(*path)
+ # path: String
+ # same as Dir.rmdir()
+ #
+ def rmdir(*path)
+ @shell.check_point
+ notify("rmdir #{path.join(' ')}")
+
+ for dir in path
+ Dir.rmdir(expand_path(dir))
+ end
+ Void.new(@shell)
+ end
+
+ #
+ # CommandProcessor#system(command, *opts)
+ # command: String
+ # opts: String
+ # return: SystemCommand
+ # Same as system() function
+ # example:
+ # print sh.system("ls", "-l")
+ # sh.system("ls", "-l") | sh.head > STDOUT
+ #
+ def system(command, *opts)
+ if opts.empty?
+ if command =~ /\*|\?|\{|\}|\[|\]|<|>|\(|\)|~|&|\||\\|\$|;|'|`|"|\n/
+ return SystemCommand.new(@shell, find_system_command("sh"), "-c", command)
+ else
+ command, *opts = command.split(/\s+/)
+ end
+ end
+ SystemCommand.new(@shell, find_system_command(command), *opts)
+ end
+
+ #
+ # ProcessCommand#rehash
+ # clear command hash table.
+ #
+ def rehash
+ @system_commands = {}
+ end
+
+ #
+ # ProcessCommand#transact
+ #
+ def check_point
+ @shell.process_controller.wait_all_jobs_execution
+ end
+ alias finish_all_jobs check_point
+
+ def transact(&block)
+ begin
+ @shell.instance_eval(&block)
+ ensure
+ check_point
+ end
+ end
+
+ #
+ # internal commands
+ #
+ def out(dev = STDOUT, &block)
+ dev.print transact(&block)
+ end
+
+ def echo(*strings)
+ Echo.new(@shell, *strings)
+ end
+
+ def cat(*filenames)
+ Cat.new(@shell, *filenames)
+ end
+
+ # def sort(*filenames)
+ # Sort.new(self, *filenames)
+ # end
+
+ def glob(pattern)
+ Glob.new(@shell, pattern)
+ end
+
+ def append(to, filter)
+ case to
+ when String
+ AppendFile.new(@shell, to, filter)
+ when IO
+ AppendIO.new(@shell, to, filter)
+ else
+ Shell.Fail Error::CantApplyMethod, "append", to.class
+ end
+ end
+
+ def tee(file)
+ Tee.new(@shell, file)
+ end
+
+ def concat(*jobs)
+ Concat.new(@shell, *jobs)
+ end
+
+ # %pwd, %cwd -> @pwd
+ def notify(*opts, &block)
+ Shell.notify(*opts) {|mes|
+ yield mes if iterator?
+
+ mes.gsub!("%pwd", "#{@cwd}")
+ mes.gsub!("%cwd", "#{@cwd}")
+ }
+ end
+
+ #
+ # private functions
+ #
+ def find_system_command(command)
+ return command if /^\// =~ command
+ case path = @system_commands[command]
+ when String
+ if exists?(path)
+ return path
+ else
+ Shell.Fail Error::CommandNotFound, command
+ end
+ when false
+ Shell.Fail Error::CommandNotFound, command
+ end
+
+ for p in @shell.system_path
+ path = join(p, command)
+ if FileTest.exist?(path)
+ @system_commands[command] = path
+ return path
+ end
+ end
+ @system_commands[command] = false
+ Shell.Fail Error::CommandNotFound, command
+ end
+
+ #
+ # CommandProcessor.def_system_command(command, path)
+ # command: String
+ # path: String
+ # define 'command()' method as method.
+ #
+ def self.def_system_command(command, path = command)
+ begin
+ eval((d = %Q[def #{command}(*opts)
+ SystemCommand.new(@shell, '#{path}', *opts)
+ end]), nil, __FILE__, __LINE__ - 1)
+ rescue SyntaxError
+ Shell.notify "warn: Can't define #{command} path: #{path}."
+ end
+ Shell.notify "Define #{command} path: #{path}.", Shell.debug?
+ Shell.notify("Definition of #{command}: ", d,
+ Shell.debug.kind_of?(Integer) && Shell.debug > 1)
+ end
+
+ def self.undef_system_command(command)
+ command = command.id2name if command.kind_of?(Symbol)
+ remove_method(command)
+ Shell.module_eval{remove_method(command)}
+ Filter.module_eval{remove_method(command)}
+ self
+ end
+
+ # define command alias
+ # ex)
+ # def_alias_command("ls_c", "ls", "-C", "-F")
+ # def_alias_command("ls_c", "ls"){|*opts| ["-C", "-F", *opts]}
+ #
+ @alias_map = {}
+ def self.alias_map
+ @alias_map
+ end
+ def self.alias_command(ali, command, *opts, &block)
+ ali = ali.id2name if ali.kind_of?(Symbol)
+ command = command.id2name if command.kind_of?(Symbol)
+ begin
+ if iterator?
+ @alias_map[ali.intern] = proc
+
+ eval((d = %Q[def #{ali}(*opts)
+ @shell.__send__(:#{command},
+ *(CommandProcessor.alias_map[:#{ali}].call *opts))
+ end]), nil, __FILE__, __LINE__ - 1)
+
+ else
+ args = opts.collect{|opt| '"' + opt + '"'}.join(",")
+ eval((d = %Q[def #{ali}(*opts)
+ @shell.__send__(:#{command}, #{args}, *opts)
+ end]), nil, __FILE__, __LINE__ - 1)
+ end
+ rescue SyntaxError
+ Shell.notify "warn: Can't alias #{ali} command: #{command}."
+ Shell.notify("Definition of #{ali}: ", d)
+ raise
+ end
+ Shell.notify "Define #{ali} command: #{command}.", Shell.debug?
+ Shell.notify("Definition of #{ali}: ", d,
+ Shell.debug.kind_of?(Integer) && Shell.debug > 1)
+ self
+ end
+
+ def self.unalias_command(ali)
+ ali = ali.id2name if ali.kind_of?(Symbol)
+ @alias_map.delete ali.intern
+ undef_system_command(ali)
+ end
+
+ #
+ # CommandProcessor.def_builtin_commands(delegation_class, command_specs)
+ # delegation_class: Class or Module
+ # command_specs: [[command_name, [argument,...]],...]
+ # command_name: String
+ # arguments: String
+ # FILENAME?? -> expand_path(filename??)
+ # *FILENAME?? -> filename??.collect{|f|expand_path(f)}.join(", ")
+ # define command_name(argument,...) as
+ # delegation_class.command_name(argument,...)
+ #
+ def self.def_builtin_commands(delegation_class, command_specs)
+ for meth, args in command_specs
+ arg_str = args.collect{|arg| arg.downcase}.join(", ")
+ call_arg_str = args.collect{
+ |arg|
+ case arg
+ when /^(FILENAME.*)$/
+ format("expand_path(%s)", $1.downcase)
+ when /^(\*FILENAME.*)$/
+ # \*FILENAME* -> filenames.collect{|fn| expand_path(fn)}.join(", ")
+ $1.downcase + '.collect{|fn| expand_path(fn)}'
+ else
+ arg
+ end
+ }.join(", ")
+ d = %Q[def #{meth}(#{arg_str})
+ #{delegation_class}.#{meth}(#{call_arg_str})
+ end]
+ Shell.notify "Define #{meth}(#{arg_str})", Shell.debug?
+ Shell.notify("Definition of #{meth}: ", d,
+ Shell.debug.kind_of?(Integer) && Shell.debug > 1)
+ eval d
+ end
+ end
+
+ #
+ # CommandProcessor.install_system_commands(pre)
+ # pre: String - command name prefix
+ # defines every command which belongs in default_system_path via
+ # CommandProcessor.command(). It doesn't define already defined
+ # methods twice. By default, "pre_" is prefixes to each method
+ # name. Characters that may not be used in a method name are
+ # all converted to '_'. Definition errors are just ignored.
+ #
+ def self.install_system_commands(pre = "sys_")
+ defined_meth = {}
+ for m in Shell.methods
+ defined_meth[m] = true
+ end
+ sh = Shell.new
+ for path in Shell.default_system_path
+ next unless sh.directory? path
+ sh.cd path
+ sh.foreach do
+ |cn|
+ if !defined_meth[pre + cn] && sh.file?(cn) && sh.executable?(cn)
+ command = (pre + cn).gsub(/\W/, "_").sub(/^([0-9])/, '_\1')
+ begin
+ def_system_command(command, sh.expand_path(cn))
+ rescue
+ Shell.notify "warn: Can't define #{command} path: #{cn}"
+ end
+ defined_meth[command] = command
+ end
+ end
+ end
+ end
+
+ #----------------------------------------------------------------------
+ #
+ # class initializing methods -
+ #
+ #----------------------------------------------------------------------
+ def self.add_delegate_command_to_shell(id)
+ id = id.intern if id.kind_of?(String)
+ name = id.id2name
+ if Shell.method_defined?(id)
+ Shell.notify "warn: override definnition of Shell##{name}."
+ Shell.notify "warn: alias Shell##{name} to Shell##{name}_org.\n"
+ Shell.module_eval "alias #{name}_org #{name}"
+ end
+ Shell.notify "method added: Shell##{name}.", Shell.debug?
+ Shell.module_eval(%Q[def #{name}(*args, &block)
+ begin
+ @command_processor.__send__(:#{name}, *args, &block)
+ rescue Exception
+ $@.delete_if{|s| /:in `__getobj__'$/ =~ s} #`
+ $@.delete_if{|s| /^\\(eval\\):/ =~ s}
+ raise
+ end
+ end], __FILE__, __LINE__)
+
+ if Shell::Filter.method_defined?(id)
+ Shell.notify "warn: override definnition of Shell::Filter##{name}."
+ Shell.notify "warn: alias Shell##{name} to Shell::Filter##{name}_org."
+ Filter.module_eval "alias #{name}_org #{name}"
+ end
+ Shell.notify "method added: Shell::Filter##{name}.", Shell.debug?
+ Filter.module_eval(%Q[def #{name}(*args, &block)
+ begin
+ self | @shell.__send__(:#{name}, *args, &block)
+ rescue Exception
+ $@.delete_if{|s| /:in `__getobj__'$/ =~ s} #`
+ $@.delete_if{|s| /^\\(eval\\):/ =~ s}
+ raise
+ end
+ end], __FILE__, __LINE__)
+ end
+
+ #
+ # define default builtin commands
+ #
+ def self.install_builtin_commands
+ # method related File.
+ # (exclude open/foreach/unlink)
+ normal_delegation_file_methods = [
+ ["atime", ["FILENAME"]],
+ ["basename", ["fn", "*opts"]],
+ ["chmod", ["mode", "*FILENAMES"]],
+ ["chown", ["owner", "group", "*FILENAME"]],
+ ["ctime", ["FILENAMES"]],
+ ["delete", ["*FILENAMES"]],
+ ["dirname", ["FILENAME"]],
+ ["ftype", ["FILENAME"]],
+ ["join", ["*items"]],
+ ["link", ["FILENAME_O", "FILENAME_N"]],
+ ["lstat", ["FILENAME"]],
+ ["mtime", ["FILENAME"]],
+ ["readlink", ["FILENAME"]],
+ ["rename", ["FILENAME_FROM", "FILENAME_TO"]],
+ # ["size", ["FILENAME"]],
+ ["split", ["pathname"]],
+ ["stat", ["FILENAME"]],
+ ["symlink", ["FILENAME_O", "FILENAME_N"]],
+ ["truncate", ["FILENAME", "length"]],
+ ["utime", ["atime", "mtime", "*FILENAMES"]]]
+
+ def_builtin_commands(File, normal_delegation_file_methods)
+ alias_method :rm, :delete
+
+ # method related FileTest
+ def_builtin_commands(FileTest,
+ FileTest.singleton_methods(false).collect{|m| [m, ["FILENAME"]]})
+
+ end
+
+ end
+end
diff --git a/ruby/lib/shell/error.rb b/ruby/lib/shell/error.rb
new file mode 100644
index 0000000..a4a6c1a
--- /dev/null
+++ b/ruby/lib/shell/error.rb
@@ -0,0 +1,25 @@
+#
+# shell/error.rb -
+# $Release Version: 0.7 $
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+
+class Shell
+ module Error
+ extend Exception2MessageMapper
+ def_e2message TypeError, "wrong argument type %s (expected %s)"
+
+ def_exception :DirStackEmpty, "Directory stack empty."
+ def_exception :CantDefine, "Can't define method(%s, %s)."
+ def_exception :CantApplyMethod, "This method(%s) does not apply to this type(%s)."
+ def_exception :CommandNotFound, "Command not found(%s)."
+ end
+end
+
diff --git a/ruby/lib/shell/filter.rb b/ruby/lib/shell/filter.rb
new file mode 100644
index 0000000..63f5777
--- /dev/null
+++ b/ruby/lib/shell/filter.rb
@@ -0,0 +1,109 @@
+#
+# shell/filter.rb -
+# $Release Version: 0.7 $
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+class Shell
+ #
+ # Filter
+ # A method to require
+ # each()
+ #
+ class Filter
+ include Enumerable
+
+ def initialize(sh)
+ @shell = sh # parent shell
+ @input = nil # input filter
+ end
+
+ attr_reader :input
+
+ def input=(filter)
+ @input = filter
+ end
+
+ def each(rs = nil)
+ rs = @shell.record_separator unless rs
+ if @input
+ @input.each(rs){|l| yield l}
+ end
+ end
+
+ def < (src)
+ case src
+ when String
+ cat = Cat.new(@shell, src)
+ cat | self
+ when IO
+ self.input = src
+ self
+ else
+ Shell.Fail Error::CantApplyMethod, "<", to.class
+ end
+ end
+
+ def > (to)
+ case to
+ when String
+ dst = @shell.open(to, "w")
+ begin
+ each(){|l| dst << l}
+ ensure
+ dst.close
+ end
+ when IO
+ each(){|l| to << l}
+ else
+ Shell.Fail Error::CantApplyMethod, ">", to.class
+ end
+ self
+ end
+
+ def >> (to)
+ begin
+ Shell.cd(@shell.pwd).append(to, self)
+ rescue CantApplyMethod
+ Shell.Fail Error::CantApplyMethod, ">>", to.class
+ end
+ end
+
+ def | (filter)
+ filter.input = self
+ if active?
+ @shell.process_controller.start_job filter
+ end
+ filter
+ end
+
+ def + (filter)
+ Join.new(@shell, self, filter)
+ end
+
+ def to_a
+ ary = []
+ each(){|l| ary.push l}
+ ary
+ end
+
+ def to_s
+ str = ""
+ each(){|l| str.concat l}
+ str
+ end
+
+ def inspect
+ if @shell.debug.kind_of?(Integer) && @shell.debug > 2
+ super
+ else
+ to_s
+ end
+ end
+ end
+end
diff --git a/ruby/lib/shell/process-controller.rb b/ruby/lib/shell/process-controller.rb
new file mode 100644
index 0000000..1ae60cc
--- /dev/null
+++ b/ruby/lib/shell/process-controller.rb
@@ -0,0 +1,319 @@
+#
+# shell/process-controller.rb -
+# $Release Version: 0.7 $
+# $Revision: 20880 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+require "forwardable"
+
+require "thread"
+require "sync"
+
+class Shell
+ class ProcessController
+
+ @ProcessControllers = {}
+ @ProcessControllersMonitor = Mutex.new
+ @ProcessControllersCV = ConditionVariable.new
+
+ @BlockOutputMonitor = Mutex.new
+ @BlockOutputCV = ConditionVariable.new
+
+ class<<self
+ extend Forwardable
+
+ def_delegator("@ProcessControllersMonitor",
+ "synchronize", "process_controllers_exclusive")
+
+ def active_process_controllers
+ process_controllers_exclusive do
+ @ProcessControllers.dup
+ end
+ end
+
+ def activate(pc)
+ process_controllers_exclusive do
+ @ProcessControllers[pc] ||= 0
+ @ProcessControllers[pc] += 1
+ end
+ end
+
+ def inactivate(pc)
+ process_controllers_exclusive do
+ if @ProcessControllers[pc]
+ if (@ProcessControllers[pc] -= 1) == 0
+ @ProcessControllers.delete(pc)
+ @ProcessControllersCV.signal
+ end
+ end
+ end
+ end
+
+ def each_active_object
+ process_controllers_exclusive do
+ for ref in @ProcessControllers.keys
+ yield ref
+ end
+ end
+ end
+
+ def block_output_synchronize(&b)
+ @BlockOutputMonitor.synchronize(&b)
+ end
+
+ def wait_to_finish_all_process_controllers
+ process_controllers_exclusive do
+ while !@ProcessControllers.empty?
+ Shell::notify("Process finishing, but active shell exists",
+ "You can use Shell#transact or Shell#check_point for more safe execution.")
+ if Shell.debug?
+ for pc in @ProcessControllers.keys
+ Shell::notify(" Not finished jobs in "+pc.shell.to_s)
+ for com in pc.jobs
+ com.notify(" Jobs: %id")
+ end
+ end
+ end
+ @ProcessControllersCV.wait(@ProcessControllersMonitor)
+ end
+ end
+ end
+ end
+
+ # for shell-command complete finish at this process exit.
+ USING_AT_EXIT_WHEN_PROCESS_EXIT = true
+ at_exit do
+ wait_to_finish_all_process_controllers unless $@
+ end
+
+ def initialize(shell)
+ @shell = shell
+ @waiting_jobs = []
+ @active_jobs = []
+ @jobs_sync = Sync.new
+
+ @job_monitor = Mutex.new
+ @job_condition = ConditionVariable.new
+ end
+
+ attr_reader :shell
+
+ def jobs
+ jobs = []
+ @jobs_sync.synchronize(:SH) do
+ jobs.concat @waiting_jobs
+ jobs.concat @active_jobs
+ end
+ jobs
+ end
+
+ def active_jobs
+ @active_jobs
+ end
+
+ def waiting_jobs
+ @waiting_jobs
+ end
+
+ def jobs_exist?
+ @jobs_sync.synchronize(:SH) do
+ @active_jobs.empty? or @waiting_jobs.empty?
+ end
+ end
+
+ def active_jobs_exist?
+ @jobs_sync.synchronize(:SH) do
+ @active_jobs.empty?
+ end
+ end
+
+ def waiting_jobs_exist?
+ @jobs_sync.synchronize(:SH) do
+ @waiting_jobs.empty?
+ end
+ end
+
+ # schedule a command
+ def add_schedule(command)
+ @jobs_sync.synchronize(:EX) do
+ ProcessController.activate(self)
+ if @active_jobs.empty?
+ start_job command
+ else
+ @waiting_jobs.push(command)
+ end
+ end
+ end
+
+ # start a job
+ def start_job(command = nil)
+ @jobs_sync.synchronize(:EX) do
+ if command
+ return if command.active?
+ @waiting_jobs.delete command
+ else
+ command = @waiting_jobs.shift
+# command.notify "job(%id) pre-start.", @shell.debug?
+
+ return unless command
+ end
+ @active_jobs.push command
+ command.start
+# command.notify "job(%id) post-start.", @shell.debug?
+
+ # start all jobs that input from the job
+ for job in @waiting_jobs.dup
+ start_job(job) if job.input == command
+ end
+# command.notify "job(%id) post2-start.", @shell.debug?
+ end
+ end
+
+ def waiting_job?(job)
+ @jobs_sync.synchronize(:SH) do
+ @waiting_jobs.include?(job)
+ end
+ end
+
+ def active_job?(job)
+ @jobs_sync.synchronize(:SH) do
+ @active_jobs.include?(job)
+ end
+ end
+
+ # terminate a job
+ def terminate_job(command)
+ @jobs_sync.synchronize(:EX) do
+ @active_jobs.delete command
+ ProcessController.inactivate(self)
+ if @active_jobs.empty?
+ command.notify("start_jon in ierminate_jon(%id)", Shell::debug?)
+ start_job
+ end
+ end
+ end
+
+ # kill a job
+ def kill_job(sig, command)
+ @jobs_sync.synchronize(:EX) do
+ if @waiting_jobs.delete command
+ ProcessController.inactivate(self)
+ return
+ elsif @active_jobs.include?(command)
+ begin
+ r = command.kill(sig)
+ ProcessController.inactivate(self)
+ rescue
+ print "Shell: Warn: $!\n" if @shell.verbose?
+ return nil
+ end
+ @active_jobs.delete command
+ r
+ end
+ end
+ end
+
+ # wait for all jobs to terminate
+ def wait_all_jobs_execution
+ @job_monitor.synchronize do
+ begin
+ while !jobs.empty?
+ @job_condition.wait(@job_monitor)
+ for job in jobs
+ job.notify("waiting job(%id)", Shell::debug?)
+ end
+ end
+ ensure
+ redo unless jobs.empty?
+ end
+ end
+ end
+
+ # simple fork
+ def sfork(command, &block)
+ pipe_me_in, pipe_peer_out = IO.pipe
+ pipe_peer_in, pipe_me_out = IO.pipe
+
+
+ pid = nil
+ pid_mutex = Mutex.new
+ pid_cv = ConditionVariable.new
+
+ Thread.start do
+ ProcessController.block_output_synchronize do
+ STDOUT.flush
+ ProcessController.each_active_object do |pc|
+ for jobs in pc.active_jobs
+ jobs.flush
+ end
+ end
+
+ pid = fork {
+ Thread.list.each do |th|
+# th.kill unless [Thread.main, Thread.current].include?(th)
+ th.kill unless Thread.current == th
+ end
+
+ STDIN.reopen(pipe_peer_in)
+ STDOUT.reopen(pipe_peer_out)
+
+ ObjectSpace.each_object(IO) do |io|
+ if ![STDIN, STDOUT, STDERR].include?(io)
+ io.close unless io.closed?
+ end
+ end
+
+ yield
+ }
+ end
+ pid_cv.signal
+
+ pipe_peer_in.close
+ pipe_peer_out.close
+ command.notify "job(%name:##{pid}) start", @shell.debug?
+
+ begin
+ _pid = nil
+ command.notify("job(%id) start to waiting finish.", @shell.debug?)
+ _pid = Process.waitpid(pid, nil)
+ rescue Errno::ECHILD
+ command.notify "warn: job(%id) was done already waitipd."
+ _pid = true
+ # rescue
+ # STDERR.puts $!
+ ensure
+ command.notify("Job(%id): Wait to finish when Process finished.", @shell.debug?)
+ # when the process ends, wait until the command termintes
+ if USING_AT_EXIT_WHEN_PROCESS_EXIT or _pid
+ else
+ command.notify("notice: Process finishing...",
+ "wait for Job[%id] to finish.",
+ "You can use Shell#transact or Shell#check_point for more safe execution.")
+ redo
+ end
+
+# command.notify "job(%id) pre-pre-finish.", @shell.debug?
+ @job_monitor.synchronize do
+# command.notify "job(%id) pre-finish.", @shell.debug?
+ terminate_job(command)
+# command.notify "job(%id) pre-finish2.", @shell.debug?
+ @job_condition.signal
+ command.notify "job(%id) finish.", @shell.debug?
+ end
+ end
+ end
+
+ pid_mutex.synchronize do
+ while !pid
+ pid_cv.wait(pid_mutex)
+ end
+ end
+
+ return pid, pipe_me_in, pipe_me_out
+ end
+ end
+end
diff --git a/ruby/lib/shell/system-command.rb b/ruby/lib/shell/system-command.rb
new file mode 100644
index 0000000..12664ca
--- /dev/null
+++ b/ruby/lib/shell/system-command.rb
@@ -0,0 +1,159 @@
+#
+# shell/system-command.rb -
+# $Release Version: 0.7 $
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+require "shell/filter"
+
+class Shell
+ class SystemCommand < Filter
+ def initialize(sh, command, *opts)
+ if t = opts.find{|opt| !opt.kind_of?(String) && opt.class}
+ Shell.Fail Error::TypeError, t.class, "String"
+ end
+ super(sh)
+ @command = command
+ @opts = opts
+
+ @input_queue = Queue.new
+ @pid = nil
+
+ sh.process_controller.add_schedule(self)
+ end
+
+ attr_reader :command
+ alias name command
+
+ def wait?
+ @shell.process_controller.waiting_job?(self)
+ end
+
+ def active?
+ @shell.process_controller.active_job?(self)
+ end
+
+ def input=(inp)
+ super
+ if active?
+ start_export
+ end
+ end
+
+ def start
+ notify([@command, *@opts].join(" "))
+
+ @pid, @pipe_in, @pipe_out = @shell.process_controller.sfork(self) {
+ Dir.chdir @shell.pwd
+ $0 = @command
+ exec(@command, *@opts)
+ }
+ if @input
+ start_export
+ end
+ start_import
+ end
+
+ def flush
+ @pipe_out.flush if @pipe_out and !@pipe_out.closed?
+ end
+
+ def terminate
+ begin
+ @pipe_in.close
+ rescue IOError
+ end
+ begin
+ @pipe_out.close
+ rescue IOError
+ end
+ end
+
+ def kill(sig)
+ if @pid
+ Process.kill(sig, @pid)
+ end
+ end
+
+ def start_import
+ notify "Job(%id) start imp-pipe.", @shell.debug?
+ rs = @shell.record_separator unless rs
+ _eop = true
+ th = Thread.start {
+ begin
+ while l = @pipe_in.gets
+ @input_queue.push l
+ end
+ _eop = false
+ rescue Errno::EPIPE
+ _eop = false
+ ensure
+ if !ProcessController::USING_AT_EXIT_WHEN_PROCESS_EXIT and _eop
+ notify("warn: Process finishing...",
+ "wait for Job[%id] to finish pipe importing.",
+ "You can use Shell#transact or Shell#check_point for more safe execution.")
+ redo
+ end
+ notify "job(%id}) close imp-pipe.", @shell.debug?
+ @input_queue.push :EOF
+ @pipe_in.close
+ end
+ }
+ end
+
+ def start_export
+ notify "job(%id) start exp-pipe.", @shell.debug?
+ _eop = true
+ th = Thread.start{
+ begin
+ @input.each do |l|
+ ProcessController::block_output_synchronize do
+ @pipe_out.print l
+ end
+ end
+ _eop = false
+ rescue Errno::EPIPE, Errno::EIO
+ _eop = false
+ ensure
+ if !ProcessController::USING_AT_EXIT_WHEN_PROCESS_EXIT and _eop
+ notify("shell: warn: Process finishing...",
+ "wait for Job(%id) to finish pipe exporting.",
+ "You can use Shell#transact or Shell#check_point for more safe execution.")
+ redo
+ end
+ notify "job(%id) close exp-pipe.", @shell.debug?
+ @pipe_out.close
+ end
+ }
+ end
+
+ alias super_each each
+ def each(rs = nil)
+ while (l = @input_queue.pop) != :EOF
+ yield l
+ end
+ end
+
+ # ex)
+ # if you wish to output:
+ # "shell: job(#{@command}:#{@pid}) close pipe-out."
+ # then
+ # mes: "job(%id) close pipe-out."
+ # yorn: Boolean(@shell.debug? or @shell.verbose?)
+ def notify(*opts, &block)
+ @shell.notify(*opts) do |mes|
+ yield mes if iterator?
+
+ mes.gsub!("%id", "#{@command}:##{@pid}")
+ mes.gsub!("%name", "#{@command}")
+ mes.gsub!("%pid", "#{@pid}")
+ mes
+ end
+ end
+ end
+end
diff --git a/ruby/lib/shell/version.rb b/ruby/lib/shell/version.rb
new file mode 100644
index 0000000..5a33516
--- /dev/null
+++ b/ruby/lib/shell/version.rb
@@ -0,0 +1,15 @@
+#
+# version.rb - shell version definition file
+# $Release Version: 0.7$
+# $Revision: 14912 $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+#
+# --
+#
+#
+#
+
+class Shell
+ @RELEASE_VERSION = "0.7"
+ @LAST_UPDATE_DATE = "07/03/20"
+end
diff --git a/ruby/lib/shellwords.rb b/ruby/lib/shellwords.rb
new file mode 100644
index 0000000..f130061
--- /dev/null
+++ b/ruby/lib/shellwords.rb
@@ -0,0 +1,156 @@
+#
+# shellwords.rb: Manipulates strings a la UNIX Bourne shell
+#
+
+#
+# This module manipulates strings according to the word parsing rules
+# of the UNIX Bourne shell.
+#
+# The shellwords() function was originally a port of shellwords.pl,
+# but modified to conform to POSIX / SUSv3 (IEEE Std 1003.1-2001).
+#
+# Authors:
+# - Wakou Aoyama
+# - Akinori MUSHA <knu@iDaemons.org>
+#
+# Contact:
+# - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
+#
+module Shellwords
+ #
+ # Splits a string into an array of tokens in the same way the UNIX
+ # Bourne shell does.
+ #
+ # argv = Shellwords.split('here are "two words"')
+ # argv #=> ["here", "are", "two words"]
+ #
+ # +String#shellsplit+ is a shorthand for this function.
+ #
+ # argv = 'here are "two words"'.shellsplit
+ # argv #=> ["here", "are", "two words"]
+ #
+ def shellsplit(line)
+ words = []
+ field = ''
+ line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
+ |word, sq, dq, esc, garbage, sep|
+ raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage
+ field << (word || sq || (dq || esc).gsub(/\\(?=.)/, ''))
+ if sep
+ words << field
+ field = ''
+ end
+ end
+ words
+ end
+
+ alias shellwords shellsplit
+
+ module_function :shellsplit, :shellwords
+
+ class << self
+ alias split shellsplit
+ end
+
+ #
+ # Escapes a string so that it can be safely used in a Bourne shell
+ # command line.
+ #
+ # Note that a resulted string should be used unquoted and is not
+ # intended for use in double quotes nor in single quotes.
+ #
+ # open("| grep #{Shellwords.escape(pattern)} file") { |pipe|
+ # # ...
+ # }
+ #
+ # +String#shellescape+ is a shorthand for this function.
+ #
+ # open("| grep #{pattern.shellescape} file") { |pipe|
+ # # ...
+ # }
+ #
+ def shellescape(str)
+ # An empty argument will be skipped, so return empty quotes.
+ return "''" if str.empty?
+
+ str = str.dup
+
+ # Process as a single byte sequence because not all shell
+ # implementations are multibyte aware.
+ str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
+
+ # A LF cannot be escaped with a backslash because a backslash + LF
+ # combo is regarded as line continuation and simply ignored.
+ str.gsub!(/\n/, "'\n'")
+
+ return str
+ end
+
+ module_function :shellescape
+
+ class << self
+ alias escape shellescape
+ end
+
+ #
+ # Builds a command line string from an argument list +array+ joining
+ # all elements escaped for Bourne shell and separated by a space.
+ #
+ # open('|' + Shellwords.join(['grep', pattern, *files])) { |pipe|
+ # # ...
+ # }
+ #
+ # +Array#shelljoin+ is a shorthand for this function.
+ #
+ # open('|' + ['grep', pattern, *files].shelljoin) { |pipe|
+ # # ...
+ # }
+ #
+ def shelljoin(array)
+ array.map { |arg| shellescape(arg) }.join(' ')
+ end
+
+ module_function :shelljoin
+
+ class << self
+ alias join shelljoin
+ end
+end
+
+class String
+ #
+ # call-seq:
+ # str.shellsplit => array
+ #
+ # Splits +str+ into an array of tokens in the same way the UNIX
+ # Bourne shell does. See +Shellwords::shellsplit+ for details.
+ #
+ def shellsplit
+ Shellwords.split(self)
+ end
+
+ #
+ # call-seq:
+ # str.shellescape => string
+ #
+ # Escapes +str+ so that it can be safely used in a Bourne shell
+ # command line. See +Shellwords::shellescape+ for details.
+ #
+ def shellescape
+ Shellwords.escape(self)
+ end
+end
+
+class Array
+ #
+ # call-seq:
+ # array.shelljoin => string
+ #
+ # Builds a command line string from an argument list +array+ joining
+ # all elements escaped for Bourne shell and separated by a space.
+ # See +Shellwords::shelljoin+ for details.
+ #
+ def shelljoin
+ Shellwords.join(self)
+ end
+end
diff --git a/ruby/lib/singleton.rb b/ruby/lib/singleton.rb
new file mode 100644
index 0000000..3c81b2d
--- /dev/null
+++ b/ruby/lib/singleton.rb
@@ -0,0 +1,313 @@
+# The Singleton module implements the Singleton pattern.
+#
+# Usage:
+# class Klass
+# include Singleton
+# # ...
+# end
+#
+# * this ensures that only one instance of Klass lets call it
+# ``the instance'' can be created.
+#
+# a,b = Klass.instance, Klass.instance
+# a == b # => true
+# Klass.new # NoMethodError - new is private ...
+#
+# * ``The instance'' is created at instantiation time, in other
+# words the first call of Klass.instance(), thus
+#
+# class OtherKlass
+# include Singleton
+# # ...
+# end
+# ObjectSpace.each_object(OtherKlass){} # => 0.
+#
+# * This behavior is preserved under inheritance and cloning.
+#
+#
+#
+# This is achieved by marking
+# * Klass.new and Klass.allocate - as private
+#
+# Providing (or modifying) the class methods
+# * Klass.inherited(sub_klass) and Klass.clone() -
+# to ensure that the Singleton pattern is properly
+# inherited and cloned.
+#
+# * Klass.instance() - returning ``the instance''. After a
+# successful self modifying (normally the first) call the
+# method body is a simple:
+#
+# def Klass.instance()
+# return @singleton__instance__
+# end
+#
+# * Klass._load(str) - calling Klass.instance()
+#
+# * Klass._instantiate?() - returning ``the instance'' or
+# nil. This hook method puts a second (or nth) thread calling
+# Klass.instance() on a waiting loop. The return value
+# signifies the successful completion or premature termination
+# of the first, or more generally, current "instantiation thread".
+#
+#
+# The instance method of Singleton are
+# * clone and dup - raising TypeErrors to prevent cloning or duping
+#
+# * _dump(depth) - returning the empty string. Marshalling strips
+# by default all state information, e.g. instance variables and
+# taint state, from ``the instance''. Providing custom _load(str)
+# and _dump(depth) hooks allows the (partially) resurrections of
+# a previous state of ``the instance''.
+
+require 'thread'
+
+module Singleton
+ # disable build-in copying methods
+ def clone
+ raise TypeError, "can't clone instance of singleton #{self.class}"
+ end
+ def dup
+ raise TypeError, "can't dup instance of singleton #{self.class}"
+ end
+
+ # default marshalling strategy
+ def _dump(depth = -1)
+ ''
+ end
+
+ module SingletonClassMethods
+ # properly clone the Singleton pattern - did you know
+ # that duping doesn't copy class methods?
+ def clone
+ Singleton.__init__(super)
+ end
+
+ def _load(str)
+ instance
+ end
+
+ private
+
+ # ensure that the Singleton pattern is properly inherited
+ def inherited(sub_klass)
+ super
+ Singleton.__init__(sub_klass)
+ end
+ end
+
+ class << Singleton
+ def __init__(klass)
+ klass.instance_eval {
+ @singleton__instance__ = nil
+ @singleton__mutex__ = Mutex.new
+ }
+ def klass.instance
+ return @singleton__instance__ if @singleton__instance__
+ @singleton__mutex__.synchronize {
+ return @singleton__instance__ if @singleton__instance__
+ @singleton__instance__ = new()
+ }
+ @singleton__instance__
+ end
+ klass
+ end
+
+ private
+
+ # extending an object with Singleton is a bad idea
+ undef_method :extend_object
+
+ def append_features(mod)
+ # help out people counting on transitive mixins
+ unless mod.instance_of?(Class)
+ raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
+ end
+ super
+ end
+
+ def included(klass)
+ super
+ klass.private_class_method :new, :allocate
+ klass.extend SingletonClassMethods
+ Singleton.__init__(klass)
+ end
+ end
+
+end
+
+
+if __FILE__ == $0
+
+def num_of_instances(klass)
+ "#{ObjectSpace.each_object(klass){}} #{klass} instance(s)"
+end
+
+# The basic and most important example.
+
+class SomeSingletonClass
+ include Singleton
+end
+puts "There are #{num_of_instances(SomeSingletonClass)}"
+
+a = SomeSingletonClass.instance
+b = SomeSingletonClass.instance # a and b are same object
+puts "basic test is #{a == b}"
+
+begin
+ SomeSingletonClass.new
+rescue NoMethodError => mes
+ puts mes
+end
+
+
+
+puts "\nThreaded example with exception and customized #_instantiate?() hook"; p
+Thread.abort_on_exception = false
+
+class Ups < SomeSingletonClass
+ def initialize
+ self.class.__sleep
+ puts "initialize called by thread ##{Thread.current[:i]}"
+ end
+end
+
+class << Ups
+ def _instantiate?
+ @enter.push Thread.current[:i]
+ while false.equal?(@singleton__instance__)
+ @singleton__mutex__.unlock
+ sleep 0.08
+ @singleton__mutex__.lock
+ end
+ @leave.push Thread.current[:i]
+ @singleton__instance__
+ end
+
+ def __sleep
+ sleep(rand(0.08))
+ end
+
+ def new
+ begin
+ __sleep
+ raise "boom - thread ##{Thread.current[:i]} failed to create instance"
+ ensure
+ # simple flip-flop
+ class << self
+ remove_method :new
+ end
+ end
+ end
+
+ def instantiate_all
+ @enter = []
+ @leave = []
+ 1.upto(9) {|i|
+ Thread.new {
+ begin
+ Thread.current[:i] = i
+ __sleep
+ instance
+ rescue RuntimeError => mes
+ puts mes
+ end
+ }
+ }
+ puts "Before there were #{num_of_instances(self)}"
+ sleep 3
+ puts "Now there is #{num_of_instances(self)}"
+ puts "#{@enter.join '; '} was the order of threads entering the waiting loop"
+ puts "#{@leave.join '; '} was the order of threads leaving the waiting loop"
+ end
+end
+
+
+Ups.instantiate_all
+# results in message like
+# Before there were 0 Ups instance(s)
+# boom - thread #6 failed to create instance
+# initialize called by thread #3
+# Now there is 1 Ups instance(s)
+# 3; 2; 1; 8; 4; 7; 5 was the order of threads entering the waiting loop
+# 3; 2; 1; 7; 4; 8; 5 was the order of threads leaving the waiting loop
+
+
+puts "\nLets see if class level cloning really works"
+Yup = Ups.clone
+def Yup.new
+ begin
+ __sleep
+ raise "boom - thread ##{Thread.current[:i]} failed to create instance"
+ ensure
+ # simple flip-flop
+ class << self
+ remove_method :new
+ end
+ end
+end
+Yup.instantiate_all
+
+
+puts "\n\n","Customized marshalling"
+class A
+ include Singleton
+ attr_accessor :persist, :die
+ def _dump(depth)
+ # this strips the @die information from the instance
+ Marshal.dump(@persist,depth)
+ end
+end
+
+def A._load(str)
+ instance.persist = Marshal.load(str)
+ instance
+end
+
+a = A.instance
+a.persist = ["persist"]
+a.die = "die"
+a.taint
+
+stored_state = Marshal.dump(a)
+# change state
+a.persist = nil
+a.die = nil
+b = Marshal.load(stored_state)
+p a == b # => true
+p a.persist # => ["persist"]
+p a.die # => nil
+
+
+puts "\n\nSingleton with overridden default #inherited() hook"
+class Up
+end
+def Up.inherited(sub_klass)
+ puts "#{sub_klass} subclasses #{self}"
+end
+
+
+class Middle < Up
+ include Singleton
+end
+
+class Down < Middle; end
+
+puts "and basic \"Down test\" is #{Down.instance == Down.instance}\n
+Various exceptions"
+
+begin
+ module AModule
+ include Singleton
+ end
+rescue TypeError => mes
+ puts mes #=> Inclusion of the OO-Singleton module in module AModule
+end
+
+begin
+ 'aString'.extend Singleton
+rescue NoMethodError => mes
+ puts mes #=> undefined method `extend_object' for Singleton:Module
+end
+
+end
diff --git a/ruby/lib/sync.rb b/ruby/lib/sync.rb
new file mode 100644
index 0000000..3853ba2
--- /dev/null
+++ b/ruby/lib/sync.rb
@@ -0,0 +1,307 @@
+#
+# sync.rb - 2 phase lock with counter
+# $Release Version: 1.0$
+# $Revision: 19280 $
+# by Keiju ISHITSUKA(keiju@ishitsuka.com)
+#
+# --
+# Sync_m, Synchronizer_m
+# Usage:
+# obj.extend(Sync_m)
+# or
+# class Foo
+# include Sync_m
+# :
+# end
+#
+# Sync_m#sync_mode
+# Sync_m#sync_locked?, locked?
+# Sync_m#sync_shared?, shared?
+# Sync_m#sync_exclusive?, sync_exclusive?
+# Sync_m#sync_try_lock, try_lock
+# Sync_m#sync_lock, lock
+# Sync_m#sync_unlock, unlock
+#
+# Sync, Synchronizer:
+# Usage:
+# sync = Sync.new
+#
+# Sync#mode
+# Sync#locked?
+# Sync#shared?
+# Sync#exclusive?
+# Sync#try_lock(mode) -- mode = :EX, :SH, :UN
+# Sync#lock(mode) -- mode = :EX, :SH, :UN
+# Sync#unlock
+# Sync#synchronize(mode) {...}
+#
+#
+
+unless defined? Thread
+ raise "Thread not available for this ruby interpreter"
+end
+
+module Sync_m
+ RCS_ID='-$Header$-'
+
+ # lock mode
+ UN = :UN
+ SH = :SH
+ EX = :EX
+
+ # exceptions
+ class Err < StandardError
+ def Err.Fail(*opt)
+ fail self, sprintf(self::Message, *opt)
+ end
+
+ class UnknownLocker < Err
+ Message = "Thread(%s) not locked."
+ def UnknownLocker.Fail(th)
+ super(th.inspect)
+ end
+ end
+
+ class LockModeFailer < Err
+ Message = "Unknown lock mode(%s)"
+ def LockModeFailer.Fail(mode)
+ if mode.id2name
+ mode = id2name
+ end
+ super(mode)
+ end
+ end
+ end
+
+ def Sync_m.define_aliases(cl)
+ cl.module_eval %q{
+ alias locked? sync_locked?
+ alias shared? sync_shared?
+ alias exclusive? sync_exclusive?
+ alias lock sync_lock
+ alias unlock sync_unlock
+ alias try_lock sync_try_lock
+ alias synchronize sync_synchronize
+ }
+ end
+
+ def Sync_m.append_features(cl)
+ super
+ # do nothing for Modules
+ # make aliases for Classes.
+ define_aliases(cl) unless cl.instance_of?(Module)
+ self
+ end
+
+ def Sync_m.extend_object(obj)
+ super
+ obj.sync_extend
+ end
+
+ def sync_extend
+ unless (defined? locked? and
+ defined? shared? and
+ defined? exclusive? and
+ defined? lock and
+ defined? unlock and
+ defined? try_lock and
+ defined? synchronize)
+ Sync_m.define_aliases(class<<self;self;end)
+ end
+ sync_initialize
+ end
+
+ # accessing
+ def sync_locked?
+ sync_mode != UN
+ end
+
+ def sync_shared?
+ sync_mode == SH
+ end
+
+ def sync_exclusive?
+ sync_mode == EX
+ end
+
+ # locking methods.
+ def sync_try_lock(mode = EX)
+ return unlock if mode == UN
+ @sync_mutex.synchronize do
+ ret = sync_try_lock_sub(mode)
+ end
+ ret
+ end
+
+ def sync_lock(m = EX)
+ return unlock if m == UN
+
+ while true
+ @sync_mutex.synchronize do
+ if sync_try_lock_sub(m)
+ return self
+ else
+ if sync_sh_locker[Thread.current]
+ sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]]
+ sync_sh_locker.delete(Thread.current)
+ else
+ sync_waiting.push Thread.current
+ end
+ @sync_mutex.sleep
+ end
+ end
+ end
+ self
+ end
+
+ def sync_unlock(m = EX)
+ wakeup_threads = []
+ @sync_mutex.synchronize do
+ if sync_mode == UN
+ Err::UnknownLocker.Fail(Thread.current)
+ end
+
+ m = sync_mode if m == EX and sync_mode == SH
+
+ runnable = false
+ case m
+ when UN
+ Err::UnknownLocker.Fail(Thread.current)
+
+ when EX
+ if sync_ex_locker == Thread.current
+ if (self.sync_ex_count = sync_ex_count - 1) == 0
+ self.sync_ex_locker = nil
+ if sync_sh_locker.include?(Thread.current)
+ self.sync_mode = SH
+ else
+ self.sync_mode = UN
+ end
+ runnable = true
+ end
+ else
+ Err::UnknownLocker.Fail(Thread.current)
+ end
+
+ when SH
+ if (count = sync_sh_locker[Thread.current]).nil?
+ Err::UnknownLocker.Fail(Thread.current)
+ else
+ if (sync_sh_locker[Thread.current] = count - 1) == 0
+ sync_sh_locker.delete(Thread.current)
+ if sync_sh_locker.empty? and sync_ex_count == 0
+ self.sync_mode = UN
+ runnable = true
+ end
+ end
+ end
+ end
+
+ if runnable
+ if sync_upgrade_waiting.size > 0
+ th, count = sync_upgrade_waiting.shift
+ sync_sh_locker[th] = count
+ th.wakeup
+ wakeup_threads.push th
+ else
+ wait = sync_waiting
+ self.sync_waiting = []
+ for th in wait
+ th.wakeup
+ wakeup_threads.push th
+ end
+ end
+ end
+ end
+ for th in wakeup_threads
+ th.run
+ end
+ self
+ end
+
+ def sync_synchronize(mode = EX)
+ sync_lock(mode)
+ begin
+ yield
+ ensure
+ sync_unlock
+ end
+ end
+
+ attr_accessor :sync_mode
+
+ attr_accessor :sync_waiting
+ attr_accessor :sync_upgrade_waiting
+ attr_accessor :sync_sh_locker
+ attr_accessor :sync_ex_locker
+ attr_accessor :sync_ex_count
+
+ def sync_inspect
+ sync_iv = instance_variables.select{|iv| /^@sync_/ =~ iv.id2name}.collect{|iv| iv.id2name + '=' + instance_eval(iv.id2name).inspect}.join(",")
+ print "<#{self.class}.extend Sync_m: #{inspect}, <Sync_m: #{sync_iv}>"
+ end
+
+ private
+
+ def sync_initialize
+ @sync_mode = UN
+ @sync_waiting = []
+ @sync_upgrade_waiting = []
+ @sync_sh_locker = Hash.new
+ @sync_ex_locker = nil
+ @sync_ex_count = 0
+
+ @sync_mutex = Mutex.new
+ end
+
+ def initialize(*args)
+ super
+ sync_initialize
+ end
+
+ def sync_try_lock_sub(m)
+ case m
+ when SH
+ case sync_mode
+ when UN
+ self.sync_mode = m
+ sync_sh_locker[Thread.current] = 1
+ ret = true
+ when SH
+ count = 0 unless count = sync_sh_locker[Thread.current]
+ sync_sh_locker[Thread.current] = count + 1
+ ret = true
+ when EX
+ # in EX mode, lock will upgrade to EX lock
+ if sync_ex_locker == Thread.current
+ self.sync_ex_count = sync_ex_count + 1
+ ret = true
+ else
+ ret = false
+ end
+ end
+ when EX
+ if sync_mode == UN or
+ sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
+ self.sync_mode = m
+ self.sync_ex_locker = Thread.current
+ self.sync_ex_count = 1
+ ret = true
+ elsif sync_mode == EX && sync_ex_locker == Thread.current
+ self.sync_ex_count = sync_ex_count + 1
+ ret = true
+ else
+ ret = false
+ end
+ else
+ Err::LockModeFailer.Fail mode
+ end
+ return ret
+ end
+end
+Synchronizer_m = Sync_m
+
+class Sync
+ include Sync_m
+end
+Synchronizer = Sync
diff --git a/ruby/lib/tempfile.rb b/ruby/lib/tempfile.rb
new file mode 100644
index 0000000..4d3a2f0
--- /dev/null
+++ b/ruby/lib/tempfile.rb
@@ -0,0 +1,218 @@
+#
+# tempfile - manipulates temporary files
+#
+# $Id: tempfile.rb 24119 2009-07-15 11:57:41Z yugui $
+#
+
+require 'delegate'
+require 'tmpdir'
+require 'thread'
+
+# A class for managing temporary files. This library is written to be
+# thread safe.
+class Tempfile < DelegateClass(File)
+ MAX_TRY = 10
+ @@cleanlist = []
+ @@lock = Mutex.new
+
+ # Creates a temporary file of mode 0600 in the temporary directory,
+ # opens it with mode "w+", and returns a Tempfile object which
+ # represents the created temporary file. A Tempfile object can be
+ # treated just like a normal File object.
+ #
+ # The basename parameter is used to determine the name of a
+ # temporary file. If an Array is given, the first element is used
+ # as prefix string and the second as suffix string, respectively.
+ # Otherwise it is treated as prefix string.
+ #
+ # If tmpdir is omitted, the temporary directory is determined by
+ # Dir::tmpdir provided by 'tmpdir.rb'.
+ # When $SAFE > 0 and the given tmpdir is tainted, it uses
+ # /tmp. (Note that ENV values are tainted by default)
+ def initialize(basename, *rest)
+ # I wish keyword argument settled soon.
+ if opts = Hash.try_convert(rest[-1])
+ rest.pop
+ end
+ tmpdir = rest[0] || Dir::tmpdir
+ if $SAFE > 0 and tmpdir.tainted?
+ tmpdir = '/tmp'
+ end
+
+ lock = tmpname = nil
+ n = failure = 0
+ @@lock.synchronize {
+ begin
+ begin
+ tmpname = File.join(tmpdir, make_tmpname(basename, n))
+ lock = tmpname + '.lock'
+ n += 1
+ end while @@cleanlist.include?(tmpname) or
+ File.exist?(lock) or File.exist?(tmpname)
+ Dir.mkdir(lock)
+ rescue
+ failure += 1
+ retry if failure < MAX_TRY
+ raise "cannot generate tempfile `%s'" % tmpname
+ end
+ }
+
+ @data = [tmpname]
+ @clean_proc = Tempfile.callback(@data)
+ ObjectSpace.define_finalizer(self, @clean_proc)
+
+ if opts.nil?
+ opts = []
+ else
+ opts = [opts]
+ end
+ @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600, *opts)
+ @tmpname = tmpname
+ @@cleanlist << @tmpname
+ @data[1] = @tmpfile
+ @data[2] = @@cleanlist
+
+ super(@tmpfile)
+
+ # Now we have all the File/IO methods defined, you must not
+ # carelessly put bare puts(), etc. after this.
+
+ Dir.rmdir(lock)
+ end
+
+ def make_tmpname(basename, n)
+ case basename
+ when Array
+ prefix, suffix = *basename
+ else
+ prefix, suffix = basename, ''
+ end
+
+ t = Time.now.strftime("%Y%m%d")
+ path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
+ end
+ private :make_tmpname
+
+ # Opens or reopens the file with mode "r+".
+ def open
+ @tmpfile.close if @tmpfile
+ @tmpfile = File.open(@tmpname, 'r+')
+ @data[1] = @tmpfile
+ __setobj__(@tmpfile)
+ end
+
+ def _close # :nodoc:
+ @tmpfile.close if @tmpfile
+ @tmpfile = nil
+ @data[1] = nil if @data
+ end
+ protected :_close
+
+ #Closes the file. If the optional flag is true, unlinks the file
+ # after closing.
+ #
+ # If you don't explicitly unlink the temporary file, the removal
+ # will be delayed until the object is finalized.
+ def close(unlink_now=false)
+ if unlink_now
+ close!
+ else
+ _close
+ end
+ end
+
+ # Closes and unlinks the file.
+ def close!
+ _close
+ @clean_proc.call
+ ObjectSpace.undefine_finalizer(self)
+ @data = @tmpname = nil
+ end
+
+ # Unlinks the file. On UNIX-like systems, it is often a good idea
+ # to unlink a temporary file immediately after creating and opening
+ # it, because it leaves other programs zero chance to access the
+ # file.
+ def unlink
+ # keep this order for thread safeness
+ begin
+ if File.exist?(@tmpname)
+ closed? or close
+ File.unlink(@tmpname)
+ end
+ @@cleanlist.delete(@tmpname)
+ @data = @tmpname = nil
+ ObjectSpace.undefine_finalizer(self)
+ rescue Errno::EACCES
+ # may not be able to unlink on Windows; just ignore
+ end
+ end
+ alias delete unlink
+
+ # Returns the full path name of the temporary file.
+ def path
+ @tmpname
+ end
+
+ # Returns the size of the temporary file. As a side effect, the IO
+ # buffer is flushed before determining the size.
+ def size
+ if @tmpfile
+ @tmpfile.flush
+ @tmpfile.stat.size
+ else
+ 0
+ end
+ end
+ alias length size
+
+ class << self
+ def callback(data) # :nodoc:
+ pid = $$
+ Proc.new {
+ if pid == $$
+ path, tmpfile, cleanlist = *data
+
+ print "removing ", path, "..." if $DEBUG
+
+ tmpfile.close if tmpfile
+
+ # keep this order for thread safeness
+ File.unlink(path) if File.exist?(path)
+ cleanlist.delete(path) if cleanlist
+
+ print "done\n" if $DEBUG
+ end
+ }
+ end
+
+ # If no block is given, this is a synonym for new().
+ #
+ # If a block is given, it will be passed tempfile as an argument,
+ # and the tempfile will automatically be closed when the block
+ # terminates. The call returns the value of the block.
+ def open(*args)
+ tempfile = new(*args)
+
+ if block_given?
+ begin
+ yield(tempfile)
+ ensure
+ tempfile.close
+ end
+ else
+ tempfile
+ end
+ end
+ end
+end
+
+if __FILE__ == $0
+# $DEBUG = true
+ f = Tempfile.new("foo")
+ f.print("foo\n")
+ f.close
+ f.open
+ p f.gets # => "foo\n"
+ f.close!
+end
diff --git a/ruby/lib/test/unit.rb b/ruby/lib/test/unit.rb
new file mode 100644
index 0000000..ec248c3
--- /dev/null
+++ b/ruby/lib/test/unit.rb
@@ -0,0 +1,66 @@
+# test/unit compatibility layer using minitest.
+
+require 'minitest/unit'
+require 'test/unit/assertions'
+require 'test/unit/testcase'
+
+module Test
+ module Unit
+ TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'
+
+ def self.setup_argv(original_argv=ARGV)
+ minitest_argv = []
+ files = []
+ reject = []
+ original_argv = original_argv.dup
+ while arg = original_argv.shift
+ case arg
+ when '-v'
+ minitest_argv << '-v'
+ when '-n', '--name'
+ minitest_argv << arg
+ minitest_argv << original_argv.shift
+ when '-x'
+ reject << original_argv.shift
+ else
+ files << arg
+ end
+ end
+
+ if block_given?
+ files = yield files
+ end
+
+ files.map! {|f|
+ f = f.gsub(Regexp.compile(Regexp.quote(File::ALT_SEPARATOR)), File::SEPARATOR) if File::ALT_SEPARATOR
+ if File.directory? f
+ Dir["#{f}/**/test_*.rb"]
+ elsif File.file? f
+ f
+ else
+ raise ArgumentError, "file not found: #{f}"
+ end
+ }
+ files.flatten!
+
+ reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
+ files.reject! {|f| reject_pat =~ f }
+
+ files.each {|f|
+ d = File.dirname(File.expand_path(f))
+ unless $:.include? d
+ $: << d
+ end
+ begin
+ require f
+ rescue LoadError
+ puts "#{f}: #{$!}"
+ end
+ }
+
+ ARGV.replace minitest_argv
+ end
+ end
+end
+
+MiniTest::Unit.autorun
diff --git a/ruby/lib/test/unit/assertions.rb b/ruby/lib/test/unit/assertions.rb
new file mode 100644
index 0000000..ac3ecf9
--- /dev/null
+++ b/ruby/lib/test/unit/assertions.rb
@@ -0,0 +1,122 @@
+require 'minitest/unit'
+require 'pp'
+
+module Test
+ module Unit
+ module Assertions
+ include MiniTest::Assertions
+
+ def mu_pp(obj)
+ obj.pretty_inspect.chomp
+ end
+
+ def assert_raise(*args, &b)
+ assert_raises(*args, &b)
+ end
+
+ def assert_nothing_raised(*args)
+ self._assertions += 1
+ if Module === args.last
+ msg = nil
+ else
+ msg = args.pop
+ end
+ begin
+ line = __LINE__; yield
+ rescue Exception => e
+ bt = e.backtrace
+ as = e.instance_of?(MiniTest::Assertion)
+ if as
+ ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
+ bt.reject! {|line| ans =~ line}
+ end
+ if ((args.empty? && !as) ||
+ args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
+ msg = message(msg) { "Exception raised:\n<#{mu_pp(e)}>" }
+ raise MiniTest::Assertion, msg.call, bt
+ else
+ raise
+ end
+ end
+ nil
+ end
+
+ def assert_nothing_thrown(msg=nil)
+ begin
+ yield
+ rescue ArgumentError => error
+ raise error if /\Auncaught throw (.+)\z/m !~ error.message
+ msg = message(msg) { "<#{$1}> was thrown when nothing was expected" }
+ flunk(msg)
+ end
+ assert(true, "Expected nothing to be thrown")
+ end
+
+ def assert_equal(exp, act, msg = nil)
+ msg = message(msg) {
+ exp_str = mu_pp(exp)
+ act_str = mu_pp(act)
+ exp_comment = ''
+ act_comment = ''
+ if exp_str == act_str
+ if (exp.is_a?(String) && act.is_a?(String)) ||
+ (exp.is_a?(Regexp) && act.is_a?(Regexp))
+ exp_comment = " (#{exp.encoding})"
+ act_comment = " (#{act.encoding})"
+ elsif exp.is_a?(Float) && act.is_a?(Float)
+ exp_str = "%\#.#{Float::DIG+2}g" % exp
+ act_str = "%\#.#{Float::DIG+2}g" % act
+ elsif exp.is_a?(Time) && act.is_a?(Time)
+ exp_comment = " (nsec=#{exp.nsec})"
+ act_comment = " (nsec=#{act.nsec})"
+ end
+ elsif !Encoding.compatible?(exp_str, act_str)
+ if exp.is_a?(String) && act.is_a?(String)
+ exp_str = exp.dump
+ act_str = act.dump
+ exp_comment = " (#{exp.encoding})"
+ act_comment = " (#{act.encoding})"
+ else
+ exp_str = exp_str.dump
+ act_str = act_str.dump
+ end
+ end
+ "<#{exp_str}>#{exp_comment} expected but was\n<#{act_str}>#{act_comment}"
+ }
+ assert(exp == act, msg)
+ end
+
+ def assert_not_nil(exp, msg=nil)
+ msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
+ assert(!exp.nil?, msg)
+ end
+
+ def assert_not_equal(exp, act, msg=nil)
+ msg = message(msg) { "<#{mu_pp(exp)}> expected to be != to\n<#{mu_pp(act)}>" }
+ assert(exp != act, msg)
+ end
+
+ def assert_no_match(regexp, string, msg=nil)
+ assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
+ self._assertions -= 1
+ msg = message(msg) { "<#{mu_pp(regexp)}> expected to not match\n<#{mu_pp(string)}>" }
+ assert(regexp !~ string, msg)
+ end
+
+ def assert_not_same(expected, actual, message="")
+ msg = message(msg) { build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__) }
+<?>
+with id <?> expected to not be equal\\? to
+<?>
+with id <?>.
+EOT
+ assert(!actual.equal?(expected), msg)
+ end
+
+ def build_message(head, template=nil, *arguments)
+ template &&= template.chomp
+ template.gsub(/\?/) { mu_pp(arguments.shift) }
+ end
+ end
+ end
+end
diff --git a/ruby/lib/test/unit/testcase.rb b/ruby/lib/test/unit/testcase.rb
new file mode 100644
index 0000000..89aa0f3
--- /dev/null
+++ b/ruby/lib/test/unit/testcase.rb
@@ -0,0 +1,12 @@
+require 'test/unit/assertions'
+
+module Test
+ module Unit
+ class TestCase < MiniTest::Unit::TestCase
+ include Assertions
+ def self.test_order
+ :sorted
+ end
+ end
+ end
+end
diff --git a/ruby/lib/thread.rb b/ruby/lib/thread.rb
new file mode 100644
index 0000000..e5585c3
--- /dev/null
+++ b/ruby/lib/thread.rb
@@ -0,0 +1,367 @@
+#
+# thread.rb - thread support classes
+# by Yukihiro Matsumoto <matz@netlab.co.jp>
+#
+# Copyright (C) 2001 Yukihiro Matsumoto
+# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
+# Copyright (C) 2000 Information-technology Promotion Agency, Japan
+#
+
+unless defined? Thread
+ raise "Thread not available for this ruby interpreter"
+end
+
+unless defined? ThreadError
+ class ThreadError < StandardError
+ end
+end
+
+if $DEBUG
+ Thread.abort_on_exception = true
+end
+
+#
+# ConditionVariable objects augment class Mutex. Using condition variables,
+# it is possible to suspend while in the middle of a critical section until a
+# resource becomes available.
+#
+# Example:
+#
+# require 'thread'
+#
+# mutex = Mutex.new
+# resource = ConditionVariable.new
+#
+# a = Thread.new {
+# mutex.synchronize {
+# # Thread 'a' now needs the resource
+# resource.wait(mutex)
+# # 'a' can now have the resource
+# }
+# }
+#
+# b = Thread.new {
+# mutex.synchronize {
+# # Thread 'b' has finished using the resource
+# resource.signal
+# }
+# }
+#
+class ConditionVariable
+ #
+ # Creates a new ConditionVariable
+ #
+ def initialize
+ @waiters = []
+ @waiters_mutex = Mutex.new
+ end
+
+ #
+ # Releases the lock held in +mutex+ and waits; reacquires the lock on wakeup.
+ #
+ def wait(mutex)
+ begin
+ # TODO: mutex should not be used
+ @waiters_mutex.synchronize do
+ @waiters.push(Thread.current)
+ end
+ mutex.sleep
+ end
+ end
+
+ #
+ # Wakes up the first thread in line waiting for this lock.
+ #
+ def signal
+ begin
+ t = @waiters_mutex.synchronize { @waiters.shift }
+ t.run if t
+ rescue ThreadError
+ retry
+ end
+ end
+
+ #
+ # Wakes up all threads waiting for this lock.
+ #
+ def broadcast
+ # TODO: imcomplete
+ waiters0 = nil
+ @waiters_mutex.synchronize do
+ waiters0 = @waiters.dup
+ @waiters.clear
+ end
+ for t in waiters0
+ begin
+ t.run
+ rescue ThreadError
+ end
+ end
+ end
+end
+
+#
+# This class provides a way to synchronize communication between threads.
+#
+# Example:
+#
+# require 'thread'
+#
+# queue = Queue.new
+#
+# producer = Thread.new do
+# 5.times do |i|
+# sleep rand(i) # simulate expense
+# queue << i
+# puts "#{i} produced"
+# end
+# end
+#
+# consumer = Thread.new do
+# 5.times do |i|
+# value = queue.pop
+# sleep rand(i/2) # simulate expense
+# puts "consumed #{value}"
+# end
+# end
+#
+# consumer.join
+#
+class Queue
+ #
+ # Creates a new queue.
+ #
+ def initialize
+ @que = []
+ @waiting = []
+ @que.taint # enable tainted comunication
+ @waiting.taint
+ self.taint
+ @mutex = Mutex.new
+ end
+
+ #
+ # Pushes +obj+ to the queue.
+ #
+ def push(obj)
+ t = nil
+ @mutex.synchronize{
+ @que.push obj
+ begin
+ t = @waiting.shift
+ t.wakeup if t
+ rescue ThreadError
+ retry
+ end
+ }
+ begin
+ t.run if t
+ rescue ThreadError
+ end
+ end
+
+ #
+ # Alias of push
+ #
+ alias << push
+
+ #
+ # Alias of push
+ #
+ alias enq push
+
+ #
+ # Retrieves data from the queue. If the queue is empty, the calling thread is
+ # suspended until data is pushed onto the queue. If +non_block+ is true, the
+ # thread isn't suspended, and an exception is raised.
+ #
+ def pop(non_block=false)
+ while true
+ @mutex.synchronize{
+ if @que.empty?
+ raise ThreadError, "queue empty" if non_block
+ @waiting.push Thread.current
+ @mutex.sleep
+ else
+ return @que.shift
+ end
+ }
+ end
+ end
+
+ #
+ # Alias of pop
+ #
+ alias shift pop
+
+ #
+ # Alias of pop
+ #
+ alias deq pop
+
+ #
+ # Returns +true+ if the queue is empty.
+ #
+ def empty?
+ @que.empty?
+ end
+
+ #
+ # Removes all objects from the queue.
+ #
+ def clear
+ @que.clear
+ end
+
+ #
+ # Returns the length of the queue.
+ #
+ def length
+ @que.length
+ end
+
+ #
+ # Alias of length.
+ #
+ alias size length
+
+ #
+ # Returns the number of threads waiting on the queue.
+ #
+ def num_waiting
+ @waiting.size
+ end
+end
+
+#
+# This class represents queues of specified size capacity. The push operation
+# may be blocked if the capacity is full.
+#
+# See Queue for an example of how a SizedQueue works.
+#
+class SizedQueue < Queue
+ #
+ # Creates a fixed-length queue with a maximum size of +max+.
+ #
+ def initialize(max)
+ raise ArgumentError, "queue size must be positive" unless max > 0
+ @max = max
+ @queue_wait = []
+ @queue_wait.taint # enable tainted comunication
+ super()
+ end
+
+ #
+ # Returns the maximum size of the queue.
+ #
+ def max
+ @max
+ end
+
+ #
+ # Sets the maximum size of the queue.
+ #
+ def max=(max)
+ diff = nil
+ @mutex.synchronize {
+ if max <= @max
+ @max = max
+ else
+ diff = max - @max
+ @max = max
+ end
+ }
+ if diff
+ diff.times do
+ begin
+ t = @queue_wait.shift
+ t.run if t
+ rescue ThreadError
+ retry
+ end
+ end
+ end
+ max
+ end
+
+ #
+ # Pushes +obj+ to the queue. If there is no space left in the queue, waits
+ # until space becomes available.
+ #
+ def push(obj)
+ t = nil
+ @mutex.synchronize{
+ while true
+ break if @que.length < @max
+ @queue_wait.push Thread.current
+ @mutex.sleep
+ end
+
+ @que.push obj
+ begin
+ t = @waiting.shift
+ t.wakeup if t
+ rescue ThreadError
+ retry
+ end
+ }
+
+ begin
+ t.run if t
+ rescue ThreadError
+ end
+ end
+
+ #
+ # Alias of push
+ #
+ alias << push
+
+ #
+ # Alias of push
+ #
+ alias enq push
+
+ #
+ # Retrieves data from the queue and runs a waiting thread, if any.
+ #
+ def pop(*args)
+ retval = super
+ t = nil
+ @mutex.synchronize {
+ if @que.length < @max
+ begin
+ t = @queue_wait.shift
+ t.wakeup if t
+ rescue ThreadError
+ retry
+ end
+ end
+ }
+ begin
+ t.run if t
+ rescue ThreadError
+ end
+ retval
+ end
+
+ #
+ # Alias of pop
+ #
+ alias shift pop
+
+ #
+ # Alias of pop
+ #
+ alias deq pop
+
+ #
+ # Returns the number of threads waiting on the queue.
+ #
+ def num_waiting
+ @waiting.size + @queue_wait.size
+ end
+end
+
+# Documentation comments:
+# - How do you make RDoc inherit documentation from superclass?
diff --git a/ruby/lib/thwait.rb b/ruby/lib/thwait.rb
new file mode 100644
index 0000000..029b259
--- /dev/null
+++ b/ruby/lib/thwait.rb
@@ -0,0 +1,168 @@
+#
+# thwait.rb - thread synchronization class
+# $Release Version: 0.9 $
+# $Revision: 1.3 $
+# by Keiju ISHITSUKA(Nihpon Rational Software Co.,Ltd.)
+#
+# --
+# feature:
+# provides synchronization for multiple threads.
+#
+# class methods:
+# * ThreadsWait.all_waits(thread1,...)
+# waits until all of specified threads are terminated.
+# if a block is supplied for the method, evaluates it for
+# each thread termination.
+# * th = ThreadsWait.new(thread1,...)
+# creates synchronization object, specifying thread(s) to wait.
+#
+# methods:
+# * th.threads
+# list threads to be synchronized
+# * th.empty?
+# is there any thread to be synchronized.
+# * th.finished?
+# is there already terminated thread.
+# * th.join(thread1,...)
+# wait for specified thread(s).
+# * th.join_nowait(threa1,...)
+# specifies thread(s) to wait. non-blocking.
+# * th.next_wait
+# waits until any of specified threads is terminated.
+# * th.all_waits
+# waits until all of specified threads are terminated.
+# if a block is supplied for the method, evaluates it for
+# each thread termination.
+#
+
+require "thread.rb"
+require "e2mmap.rb"
+
+#
+# This class watches for termination of multiple threads. Basic functionality
+# (wait until specified threads have terminated) can be accessed through the
+# class method ThreadsWait::all_waits. Finer control can be gained using
+# instance methods.
+#
+# Example:
+#
+# ThreadsWait.all_wait(thr1, thr2, ...) do |t|
+# STDERR.puts "Thread #{t} has terminated."
+# end
+#
+class ThreadsWait
+ RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
+
+ extend Exception2MessageMapper
+ def_exception("ErrNoWaitingThread", "No threads for waiting.")
+ def_exception("ErrNoFinishedThread", "No finished threads.")
+
+ #
+ # Waits until all specified threads have terminated. If a block is provided,
+ # it is executed for each thread termination.
+ #
+ def ThreadsWait.all_waits(*threads) # :yield: thread
+ tw = ThreadsWait.new(*threads)
+ if block_given?
+ tw.all_waits do |th|
+ yield th
+ end
+ else
+ tw.all_waits
+ end
+ end
+
+ #
+ # Creates a ThreadsWait object, specifying the threads to wait on.
+ # Non-blocking.
+ #
+ def initialize(*threads)
+ @threads = []
+ @wait_queue = Queue.new
+ join_nowait(*threads) unless threads.empty?
+ end
+
+ # Returns the array of threads in the wait queue.
+ attr :threads
+
+ #
+ # Returns +true+ if there are no threads to be synchronized.
+ #
+ def empty?
+ @threads.empty?
+ end
+
+ #
+ # Returns +true+ if any thread has terminated.
+ #
+ def finished?
+ !@wait_queue.empty?
+ end
+
+ #
+ # Waits for specified threads to terminate.
+ #
+ def join(*threads)
+ join_nowait(*threads)
+ next_wait
+ end
+
+ #
+ # Specifies the threads that this object will wait for, but does not actually
+ # wait.
+ #
+ def join_nowait(*threads)
+ threads.flatten!
+ @threads.concat threads
+ for th in threads
+ Thread.start(th) do |t|
+ begin
+ t.join
+ ensure
+ @wait_queue.push t
+ end
+ end
+ end
+ end
+
+ #
+ # Waits until any of the specified threads has terminated, and returns the one
+ # that does.
+ #
+ # If there is no thread to wait, raises +ErrNoWaitingThread+. If +nonblock+
+ # is true, and there is no terminated thread, raises +ErrNoFinishedThread+.
+ #
+ def next_wait(nonblock = nil)
+ ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
+ begin
+ @threads.delete(th = @wait_queue.pop(nonblock))
+ th
+ rescue ThreadError
+ ThreadsWait.fail ErrNoFinishedThread
+ end
+ end
+
+ #
+ # Waits until all of the specified threads are terminated. If a block is
+ # supplied for the method, it is executed for each thread termination.
+ #
+ # Raises exceptions in the same manner as +next_wait+.
+ #
+ def all_waits
+ until @threads.empty?
+ th = next_wait
+ yield th if block_given?
+ end
+ end
+end
+
+ThWait = ThreadsWait
+
+
+# Documentation comments:
+# - Source of documentation is evenly split between Nutshell, existing
+# comments, and my own rephrasing.
+# - I'm not particularly confident that the comments are all exactly correct.
+# - The history, etc., up the top appears in the RDoc output. Perhaps it would
+# be better to direct that not to appear, and put something else there
+# instead.
diff --git a/ruby/lib/time.rb b/ruby/lib/time.rb
new file mode 100644
index 0000000..3555571
--- /dev/null
+++ b/ruby/lib/time.rb
@@ -0,0 +1,869 @@
+
+#
+# == Introduction
+#
+# This library extends the Time class:
+# * conversion between date string and time object.
+# * date-time defined by RFC 2822
+# * HTTP-date defined by RFC 2616
+# * dateTime defined by XML Schema Part 2: Datatypes (ISO 8601)
+# * various formats handled by Date._parse (string to time only)
+#
+# == Design Issues
+#
+# === Specialized interface
+#
+# This library provides methods dedicated to special purposes:
+# * RFC 2822, RFC 2616 and XML Schema.
+# * They makes usual life easier.
+#
+# === Doesn't depend on strftime
+#
+# This library doesn't use +strftime+. Especially #rfc2822 doesn't depend
+# on +strftime+ because:
+#
+# * %a and %b are locale sensitive
+#
+# Since they are locale sensitive, they may be replaced to
+# invalid weekday/month name in some locales.
+# Since ruby-1.6 doesn't invoke setlocale by default,
+# the problem doesn't arise until some external library invokes setlocale.
+# Ruby/GTK is the example of such library.
+#
+# * %z is not portable
+#
+# %z is required to generate zone in date-time of RFC 2822
+# but it is not portable.
+#
+# == Revision Information
+#
+# $Id$
+#
+
+require 'date/format'
+
+#
+# Implements the extensions to the Time class that are described in the
+# documentation for the time.rb library.
+#
+class Time
+ class << Time
+
+ ZoneOffset = {
+ 'UTC' => 0,
+ # ISO 8601
+ 'Z' => 0,
+ # RFC 822
+ 'UT' => 0, 'GMT' => 0,
+ 'EST' => -5, 'EDT' => -4,
+ 'CST' => -6, 'CDT' => -5,
+ 'MST' => -7, 'MDT' => -6,
+ 'PST' => -8, 'PDT' => -7,
+ # Following definition of military zones is original one.
+ # See RFC 1123 and RFC 2822 for the error in RFC 822.
+ 'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6,
+ 'G' => +7, 'H' => +8, 'I' => +9, 'K' => +10, 'L' => +11, 'M' => +12,
+ 'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6,
+ 'T' => -7, 'U' => -8, 'V' => -9, 'W' => -10, 'X' => -11, 'Y' => -12,
+ }
+ def zone_offset(zone, year=self.now.year)
+ off = nil
+ zone = zone.upcase
+ if /\A([+-])(\d\d):?(\d\d)\z/ =~ zone
+ off = ($1 == '-' ? -1 : 1) * ($2.to_i * 60 + $3.to_i) * 60
+ elsif /\A[+-]\d\d\z/ =~ zone
+ off = zone.to_i * 3600
+ elsif ZoneOffset.include?(zone)
+ off = ZoneOffset[zone] * 3600
+ elsif ((t = self.local(year, 1, 1)).zone.upcase == zone rescue false)
+ off = t.utc_offset
+ elsif ((t = self.local(year, 7, 1)).zone.upcase == zone rescue false)
+ off = t.utc_offset
+ end
+ off
+ end
+
+ def zone_utc?(zone)
+ # * +0000
+ # In RFC 2822, +0000 indicate a time zone at Universal Time.
+ # Europe/London is "a time zone at Universal Time" in Winter.
+ # Europe/Lisbon is "a time zone at Universal Time" in Winter.
+ # Atlantic/Reykjavik is "a time zone at Universal Time".
+ # Africa/Dakar is "a time zone at Universal Time".
+ # So +0000 is a local time such as Europe/London, etc.
+ # * GMT
+ # GMT is used as a time zone abbreviation in Europe/London,
+ # Africa/Dakar, etc.
+ # So it is a local time.
+ #
+ # * -0000, -00:00
+ # In RFC 2822, -0000 the date-time contains no information about the
+ # local time zone.
+ # In RFC 3339, -00:00 is used for the time in UTC is known,
+ # but the offset to local time is unknown.
+ # They are not appropriate for specific time zone such as
+ # Europe/London because time zone neutral,
+ # So -00:00 and -0000 are treated as UTC.
+ if /\A(?:-00:00|-0000|-00|UTC|Z|UT)\z/i =~ zone
+ true
+ else
+ false
+ end
+ end
+ private :zone_utc?
+
+ LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+ CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+ def month_days(y, m)
+ if ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)
+ LeapYearMonthDays[m-1]
+ else
+ CommonYearMonthDays[m-1]
+ end
+ end
+ private :month_days
+
+ def apply_offset(year, mon, day, hour, min, sec, off)
+ if off < 0
+ off = -off
+ off, o = off.divmod(60)
+ if o != 0 then sec += o; o, sec = sec.divmod(60); off += o end
+ off, o = off.divmod(60)
+ if o != 0 then min += o; o, min = min.divmod(60); off += o end
+ off, o = off.divmod(24)
+ if o != 0 then hour += o; o, hour = hour.divmod(24); off += o end
+ if off != 0
+ day += off
+ if month_days(year, mon) < day
+ mon += 1
+ if 12 < mon
+ mon = 1
+ year += 1
+ end
+ day = 1
+ end
+ end
+ elsif 0 < off
+ off, o = off.divmod(60)
+ if o != 0 then sec -= o; o, sec = sec.divmod(60); off -= o end
+ off, o = off.divmod(60)
+ if o != 0 then min -= o; o, min = min.divmod(60); off -= o end
+ off, o = off.divmod(24)
+ if o != 0 then hour -= o; o, hour = hour.divmod(24); off -= o end
+ if off != 0 then
+ day -= off
+ if day < 1
+ mon -= 1
+ if mon < 1
+ year -= 1
+ mon = 12
+ end
+ day = month_days(year, mon)
+ end
+ end
+ end
+ return year, mon, day, hour, min, sec
+ end
+ private :apply_offset
+
+ def make_time(year, mon, day, hour, min, sec, sec_fraction, zone, now)
+ usec = nil
+ usec = sec_fraction * 1000000 if sec_fraction
+ if now
+ begin
+ break if year; year = now.year
+ break if mon; mon = now.mon
+ break if day; day = now.day
+ break if hour; hour = now.hour
+ break if min; min = now.min
+ break if sec; sec = now.sec
+ break if sec_fraction; usec = now.tv_usec
+ end until true
+ end
+
+ year ||= 1970
+ mon ||= 1
+ day ||= 1
+ hour ||= 0
+ min ||= 0
+ sec ||= 0
+ usec ||= 0
+
+ off = nil
+ off = zone_offset(zone, year) if zone
+
+ if off
+ year, mon, day, hour, min, sec =
+ apply_offset(year, mon, day, hour, min, sec, off)
+ t = self.utc(year, mon, day, hour, min, sec, usec)
+ t.localtime if !zone_utc?(zone)
+ t
+ else
+ self.local(year, mon, day, hour, min, sec, usec)
+ end
+ end
+ private :make_time
+
+ #
+ # Parses +date+ using Date._parse and converts it to a Time object.
+ #
+ # If a block is given, the year described in +date+ is converted by the
+ # block. For example:
+ #
+ # Time.parse(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
+ #
+ # If the upper components of the given time are broken or missing, they are
+ # supplied with those of +now+. For the lower components, the minimum
+ # values (1 or 0) are assumed if broken or missing. For example:
+ #
+ # # Suppose it is "Thu Nov 29 14:33:20 GMT 2001" now and
+ # # your timezone is GMT:
+ # Time.parse("16:30") #=> Thu Nov 29 16:30:00 GMT 2001
+ # Time.parse("7/23") #=> Mon Jul 23 00:00:00 GMT 2001
+ # Time.parse("Aug 31") #=> Fri Aug 31 00:00:00 GMT 2001
+ #
+ # Since there are numerous conflicts among locally defined timezone
+ # abbreviations all over the world, this method is not made to
+ # understand all of them. For example, the abbreviation "CST" is
+ # used variously as:
+ #
+ # -06:00 in America/Chicago,
+ # -05:00 in America/Havana,
+ # +08:00 in Asia/Harbin,
+ # +09:30 in Australia/Darwin,
+ # +10:30 in Australia/Adelaide,
+ # etc.
+ #
+ # Based on the fact, this method only understands the timezone
+ # abbreviations described in RFC 822 and the system timezone, in the
+ # order named. (i.e. a definition in RFC 822 overrides the system
+ # timezone definition.) The system timezone is taken from
+ # <tt>Time.local(year, 1, 1).zone</tt> and
+ # <tt>Time.local(year, 7, 1).zone</tt>.
+ # If the extracted timezone abbreviation does not match any of them,
+ # it is ignored and the given time is regarded as a local time.
+ #
+ # ArgumentError is raised if Date._parse cannot extract information from
+ # +date+ or Time class cannot represent specified date.
+ #
+ # This method can be used as fail-safe for other parsing methods as:
+ #
+ # Time.rfc2822(date) rescue Time.parse(date)
+ # Time.httpdate(date) rescue Time.parse(date)
+ # Time.xmlschema(date) rescue Time.parse(date)
+ #
+ # A failure for Time.parse should be checked, though.
+ #
+ def parse(date, now=self.now)
+ d = Date._parse(date, false)
+ year = d[:year]
+ year = yield(year) if year && block_given?
+ make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
+ end
+
+ #
+ # Parses +date+ using Date._strptime and converts it to a Time object.
+ #
+ # If a block is given, the year described in +date+ is converted by the
+ # block. For example:
+ #
+ # Time.strptime(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
+ def strptime(date, format, now=self.now)
+ d = Date._strptime(date, format)
+ raise ArgumentError, "invalid strptime format - `#{format}'" unless d
+ year = d[:year]
+ year = yield(year) if year && block_given?
+ make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
+ end
+
+ MonthValue = {
+ 'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6,
+ 'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
+ }
+
+ #
+ # Parses +date+ as date-time defined by RFC 2822 and converts it to a Time
+ # object. The format is identical to the date format defined by RFC 822 and
+ # updated by RFC 1123.
+ #
+ # ArgumentError is raised if +date+ is not compliant with RFC 2822
+ # or Time class cannot represent specified date.
+ #
+ # See #rfc2822 for more information on this format.
+ #
+ def rfc2822(date)
+ if /\A\s*
+ (?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*,\s*)?
+ (\d{1,2})\s+
+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+
+ (\d{2,})\s+
+ (\d{2})\s*
+ :\s*(\d{2})\s*
+ (?::\s*(\d{2}))?\s+
+ ([+-]\d{4}|
+ UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/ix =~ date
+ # Since RFC 2822 permit comments, the regexp has no right anchor.
+ day = $1.to_i
+ mon = MonthValue[$2.upcase]
+ year = $3.to_i
+ hour = $4.to_i
+ min = $5.to_i
+ sec = $6 ? $6.to_i : 0
+ zone = $7
+
+ # following year completion is compliant with RFC 2822.
+ year = if year < 50
+ 2000 + year
+ elsif year < 1000
+ 1900 + year
+ else
+ year
+ end
+
+ year, mon, day, hour, min, sec =
+ apply_offset(year, mon, day, hour, min, sec, zone_offset(zone))
+ t = self.utc(year, mon, day, hour, min, sec)
+ t.localtime if !zone_utc?(zone)
+ t
+ else
+ raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}")
+ end
+ end
+ alias rfc822 rfc2822
+
+ #
+ # Parses +date+ as HTTP-date defined by RFC 2616 and converts it to a Time
+ # object.
+ #
+ # ArgumentError is raised if +date+ is not compliant with RFC 2616 or Time
+ # class cannot represent specified date.
+ #
+ # See #httpdate for more information on this format.
+ #
+ def httpdate(date)
+ if /\A\s*
+ (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\x20
+ (\d{2})\x20
+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\x20
+ (\d{4})\x20
+ (\d{2}):(\d{2}):(\d{2})\x20
+ GMT
+ \s*\z/ix =~ date
+ self.rfc2822(date)
+ elsif /\A\s*
+ (?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday),\x20
+ (\d\d)-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d\d)\x20
+ (\d\d):(\d\d):(\d\d)\x20
+ GMT
+ \s*\z/ix =~ date
+ year = $3.to_i
+ if year < 50
+ year += 2000
+ else
+ year += 1900
+ end
+ self.utc(year, $2, $1.to_i, $4.to_i, $5.to_i, $6.to_i)
+ elsif /\A\s*
+ (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\x20
+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\x20
+ (\d\d|\x20\d)\x20
+ (\d\d):(\d\d):(\d\d)\x20
+ (\d{4})
+ \s*\z/ix =~ date
+ self.utc($6.to_i, MonthValue[$1.upcase], $2.to_i,
+ $3.to_i, $4.to_i, $5.to_i)
+ else
+ raise ArgumentError.new("not RFC 2616 compliant date: #{date.inspect}")
+ end
+ end
+
+ #
+ # Parses +date+ as dateTime defined by XML Schema and converts it to a Time
+ # object. The format is restricted version of the format defined by ISO
+ # 8601.
+ #
+ # ArgumentError is raised if +date+ is not compliant with the format or Time
+ # class cannot represent specified date.
+ #
+ # See #xmlschema for more information on this format.
+ #
+ def xmlschema(date)
+ if /\A\s*
+ (-?\d+)-(\d\d)-(\d\d)
+ T
+ (\d\d):(\d\d):(\d\d)
+ (\.\d+)?
+ (Z|[+-]\d\d:\d\d)?
+ \s*\z/ix =~ date
+ year = $1.to_i
+ mon = $2.to_i
+ day = $3.to_i
+ hour = $4.to_i
+ min = $5.to_i
+ sec = $6.to_i
+ usec = 0
+ if $7
+ usec = Rational($7) * 1000000
+ end
+ if $8
+ zone = $8
+ year, mon, day, hour, min, sec =
+ apply_offset(year, mon, day, hour, min, sec, zone_offset(zone))
+ self.utc(year, mon, day, hour, min, sec, usec)
+ else
+ self.local(year, mon, day, hour, min, sec, usec)
+ end
+ else
+ raise ArgumentError.new("invalid date: #{date.inspect}")
+ end
+ end
+ alias iso8601 xmlschema
+ end # class << self
+
+ #
+ # Returns a string which represents the time as date-time defined by RFC 2822:
+ #
+ # day-of-week, DD month-name CCYY hh:mm:ss zone
+ #
+ # where zone is [+-]hhmm.
+ #
+ # If +self+ is a UTC time, -0000 is used as zone.
+ #
+ def rfc2822
+ sprintf('%s, %02d %s %d %02d:%02d:%02d ',
+ RFC2822_DAY_NAME[wday],
+ day, RFC2822_MONTH_NAME[mon-1], year,
+ hour, min, sec) +
+ if utc?
+ '-0000'
+ else
+ off = utc_offset
+ sign = off < 0 ? '-' : '+'
+ sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60))
+ end
+ end
+ alias rfc822 rfc2822
+
+ RFC2822_DAY_NAME = [
+ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
+ ]
+ RFC2822_MONTH_NAME = [
+ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
+ 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
+ ]
+
+ #
+ # Returns a string which represents the time as rfc1123-date of HTTP-date
+ # defined by RFC 2616:
+ #
+ # day-of-week, DD month-name CCYY hh:mm:ss GMT
+ #
+ # Note that the result is always UTC (GMT).
+ #
+ def httpdate
+ t = dup.utc
+ sprintf('%s, %02d %s %d %02d:%02d:%02d GMT',
+ RFC2822_DAY_NAME[t.wday],
+ t.day, RFC2822_MONTH_NAME[t.mon-1], t.year,
+ t.hour, t.min, t.sec)
+ end
+
+ #
+ # Returns a string which represents the time as dateTime defined by XML
+ # Schema:
+ #
+ # CCYY-MM-DDThh:mm:ssTZD
+ # CCYY-MM-DDThh:mm:ss.sssTZD
+ #
+ # where TZD is Z or [+-]hh:mm.
+ #
+ # If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise.
+ #
+ # +fractional_seconds+ specifies a number of digits of fractional seconds.
+ # Its default value is 0.
+ #
+ def xmlschema(fraction_digits=0)
+ sprintf('%d-%02d-%02dT%02d:%02d:%02d',
+ year, mon, day, hour, min, sec) +
+ if fraction_digits == 0
+ ''
+ elsif fraction_digits <= 9
+ '.' + sprintf('%09d', nsec)[0, fraction_digits]
+ else
+ '.' + sprintf('%09d', nsec) + '0' * (fraction_digits - 9)
+ end +
+ if utc?
+ 'Z'
+ else
+ off = utc_offset
+ sign = off < 0 ? '-' : '+'
+ sprintf('%s%02d:%02d', sign, *(off.abs / 60).divmod(60))
+ end
+ end
+ alias iso8601 xmlschema
+end
+
+if __FILE__ == $0
+ require 'test/unit'
+
+ class TimeExtentionTest < Test::Unit::TestCase # :nodoc:
+ def test_rfc822
+ assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600,
+ Time.rfc2822("26 Aug 76 14:30 EDT"))
+ assert_equal(Time.utc(1976, 8, 27, 9, 32) + 7 * 3600,
+ Time.rfc2822("27 Aug 76 09:32 PDT"))
+ end
+
+ def test_rfc2822
+ assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
+ Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600"))
+ assert_equal(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600,
+ Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200"))
+ assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600,
+ Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600"))
+ assert_equal(Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600,
+ Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600"))
+ assert_equal(Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600,
+ Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800"))
+ begin
+ Time.at(-1)
+ rescue ArgumentError
+ # ignore
+ else
+ assert_equal(Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60,
+ Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330"))
+ assert_equal(Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60,
+ Time.rfc2822(" Thu,
+ 13
+ Feb
+ 1969
+ 23:32
+ -0330 (Newfoundland Time)"))
+ end
+ assert_equal(Time.utc(1997, 11, 21, 9, 55, 6),
+ Time.rfc2822("21 Nov 97 09:55:06 GMT"))
+ assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
+ Time.rfc2822("Fri, 21 Nov 1997 09 : 55 : 06 -0600"))
+ assert_raise(ArgumentError) {
+ # inner comment is not supported.
+ Time.rfc2822("Fri, 21 Nov 1997 09(comment): 55 : 06 -0600")
+ }
+ end
+
+ def test_rfc2616
+ t = Time.utc(1994, 11, 6, 8, 49, 37)
+ assert_equal(t, Time.httpdate("Sun, 06 Nov 1994 08:49:37 GMT"))
+ assert_equal(t, Time.httpdate("Sunday, 06-Nov-94 08:49:37 GMT"))
+ assert_equal(t, Time.httpdate("Sun Nov 6 08:49:37 1994"))
+ assert_equal(Time.utc(1995, 11, 15, 6, 25, 24),
+ Time.httpdate("Wed, 15 Nov 1995 06:25:24 GMT"))
+ assert_equal(Time.utc(1995, 11, 15, 4, 58, 8),
+ Time.httpdate("Wed, 15 Nov 1995 04:58:08 GMT"))
+ assert_equal(Time.utc(1994, 11, 15, 8, 12, 31),
+ Time.httpdate("Tue, 15 Nov 1994 08:12:31 GMT"))
+ assert_equal(Time.utc(1994, 12, 1, 16, 0, 0),
+ Time.httpdate("Thu, 01 Dec 1994 16:00:00 GMT"))
+ assert_equal(Time.utc(1994, 10, 29, 19, 43, 31),
+ Time.httpdate("Sat, 29 Oct 1994 19:43:31 GMT"))
+ assert_equal(Time.utc(1994, 11, 15, 12, 45, 26),
+ Time.httpdate("Tue, 15 Nov 1994 12:45:26 GMT"))
+ assert_equal(Time.utc(1999, 12, 31, 23, 59, 59),
+ Time.httpdate("Fri, 31 Dec 1999 23:59:59 GMT"))
+
+ assert_equal(Time.utc(2007, 12, 23, 11, 22, 33),
+ Time.httpdate('Sunday, 23-Dec-07 11:22:33 GMT'))
+ end
+
+ def test_rfc3339
+ t = Time.utc(1985, 4, 12, 23, 20, 50, 520000)
+ s = "1985-04-12T23:20:50.52Z"
+ assert_equal(t, Time.iso8601(s))
+ assert_equal(s, t.iso8601(2))
+
+ t = Time.utc(1996, 12, 20, 0, 39, 57)
+ s = "1996-12-19T16:39:57-08:00"
+ assert_equal(t, Time.iso8601(s))
+ # There is no way to generate time string with arbitrary timezone.
+ s = "1996-12-20T00:39:57Z"
+ assert_equal(t, Time.iso8601(s))
+ assert_equal(s, t.iso8601)
+
+ t = Time.utc(1990, 12, 31, 23, 59, 60)
+ s = "1990-12-31T23:59:60Z"
+ assert_equal(t, Time.iso8601(s))
+ # leap second is representable only if timezone file has it.
+ s = "1990-12-31T15:59:60-08:00"
+ assert_equal(t, Time.iso8601(s))
+
+ begin
+ Time.at(-1)
+ rescue ArgumentError
+ # ignore
+ else
+ t = Time.utc(1937, 1, 1, 11, 40, 27, 870000)
+ s = "1937-01-01T12:00:27.87+00:20"
+ assert_equal(t, Time.iso8601(s))
+ end
+ end
+
+ # http://www.w3.org/TR/xmlschema-2/
+ def test_xmlschema
+ assert_equal(Time.utc(1999, 5, 31, 13, 20, 0) + 5 * 3600,
+ Time.xmlschema("1999-05-31T13:20:00-05:00"))
+ assert_equal(Time.local(2000, 1, 20, 12, 0, 0),
+ Time.xmlschema("2000-01-20T12:00:00"))
+ assert_equal(Time.utc(2000, 1, 20, 12, 0, 0),
+ Time.xmlschema("2000-01-20T12:00:00Z"))
+ assert_equal(Time.utc(2000, 1, 20, 12, 0, 0) - 12 * 3600,
+ Time.xmlschema("2000-01-20T12:00:00+12:00"))
+ assert_equal(Time.utc(2000, 1, 20, 12, 0, 0) + 13 * 3600,
+ Time.xmlschema("2000-01-20T12:00:00-13:00"))
+ assert_equal(Time.utc(2000, 3, 4, 23, 0, 0) - 3 * 3600,
+ Time.xmlschema("2000-03-04T23:00:00+03:00"))
+ assert_equal(Time.utc(2000, 3, 4, 20, 0, 0),
+ Time.xmlschema("2000-03-04T20:00:00Z"))
+ assert_equal(Time.local(2000, 1, 15, 0, 0, 0),
+ Time.xmlschema("2000-01-15T00:00:00"))
+ assert_equal(Time.local(2000, 2, 15, 0, 0, 0),
+ Time.xmlschema("2000-02-15T00:00:00"))
+ assert_equal(Time.local(2000, 1, 15, 12, 0, 0),
+ Time.xmlschema("2000-01-15T12:00:00"))
+ assert_equal(Time.utc(2000, 1, 16, 12, 0, 0),
+ Time.xmlschema("2000-01-16T12:00:00Z"))
+ assert_equal(Time.local(2000, 1, 1, 12, 0, 0),
+ Time.xmlschema("2000-01-01T12:00:00"))
+ assert_equal(Time.utc(1999, 12, 31, 23, 0, 0),
+ Time.xmlschema("1999-12-31T23:00:00Z"))
+ assert_equal(Time.local(2000, 1, 16, 12, 0, 0),
+ Time.xmlschema("2000-01-16T12:00:00"))
+ assert_equal(Time.local(2000, 1, 16, 0, 0, 0),
+ Time.xmlschema("2000-01-16T00:00:00"))
+ assert_equal(Time.utc(2000, 1, 12, 12, 13, 14),
+ Time.xmlschema("2000-01-12T12:13:14Z"))
+ assert_equal(Time.utc(2001, 4, 17, 19, 23, 17, 300000),
+ Time.xmlschema("2001-04-17T19:23:17.3Z"))
+ assert_raise(ArgumentError) { Time.xmlschema("2000-01-01T00:00:00.+00:00") }
+ end
+
+ def test_encode_xmlschema
+ t = Time.utc(2001, 4, 17, 19, 23, 17, 300000)
+ assert_equal("2001-04-17T19:23:17Z", t.xmlschema)
+ assert_equal("2001-04-17T19:23:17.3Z", t.xmlschema(1))
+ assert_equal("2001-04-17T19:23:17.300000Z", t.xmlschema(6))
+ assert_equal("2001-04-17T19:23:17.3000000Z", t.xmlschema(7))
+
+ t = Time.utc(2001, 4, 17, 19, 23, 17, 123456)
+ assert_equal("2001-04-17T19:23:17.1234560Z", t.xmlschema(7))
+ assert_equal("2001-04-17T19:23:17.123456Z", t.xmlschema(6))
+ assert_equal("2001-04-17T19:23:17.12345Z", t.xmlschema(5))
+ assert_equal("2001-04-17T19:23:17.1Z", t.xmlschema(1))
+
+ begin
+ Time.at(-1)
+ rescue ArgumentError
+ # ignore
+ else
+ t = Time.utc(1960, 12, 31, 23, 0, 0, 123456)
+ assert_equal("1960-12-31T23:00:00.123456Z", t.xmlschema(6))
+ end
+
+ assert_equal(249, Time.xmlschema("2008-06-05T23:49:23.000249+09:00").usec)
+ end
+
+ def test_completion
+ now = Time.local(2001,11,29,21,26,35)
+ assert_equal(Time.local( 2001,11,29,21,12),
+ Time.parse("2001/11/29 21:12", now))
+ assert_equal(Time.local( 2001,11,29),
+ Time.parse("2001/11/29", now))
+ assert_equal(Time.local( 2001,11,29),
+ Time.parse( "11/29", now))
+ #assert_equal(Time.local(2001,11,1), Time.parse("Nov", now))
+ assert_equal(Time.local( 2001,11,29,10,22),
+ Time.parse( "10:22", now))
+ end
+
+ def test_invalid
+ # They were actually used in some web sites.
+ assert_raise(ArgumentError) { Time.httpdate("1 Dec 2001 10:23:57 GMT") }
+ assert_raise(ArgumentError) { Time.httpdate("Sat, 1 Dec 2001 10:25:42 GMT") }
+ assert_raise(ArgumentError) { Time.httpdate("Sat, 1-Dec-2001 10:53:55 GMT") }
+ assert_raise(ArgumentError) { Time.httpdate("Saturday, 01-Dec-2001 10:15:34 GMT") }
+ assert_raise(ArgumentError) { Time.httpdate("Saturday, 01-Dec-101 11:10:07 GMT") }
+ assert_raise(ArgumentError) { Time.httpdate("Fri, 30 Nov 2001 21:30:00 JST") }
+
+ # They were actually used in some mails.
+ assert_raise(ArgumentError) { Time.rfc2822("01-5-20") }
+ assert_raise(ArgumentError) { Time.rfc2822("7/21/00") }
+ assert_raise(ArgumentError) { Time.rfc2822("2001-8-28") }
+ assert_raise(ArgumentError) { Time.rfc2822("00-5-6 1:13:06") }
+ assert_raise(ArgumentError) { Time.rfc2822("2001-9-27 9:36:49") }
+ assert_raise(ArgumentError) { Time.rfc2822("2000-12-13 11:01:11") }
+ assert_raise(ArgumentError) { Time.rfc2822("2001/10/17 04:29:55") }
+ assert_raise(ArgumentError) { Time.rfc2822("9/4/2001 9:23:19 PM") }
+ assert_raise(ArgumentError) { Time.rfc2822("01 Nov 2001 09:04:31") }
+ assert_raise(ArgumentError) { Time.rfc2822("13 Feb 2001 16:4 GMT") }
+ assert_raise(ArgumentError) { Time.rfc2822("01 Oct 00 5:41:19 PM") }
+ assert_raise(ArgumentError) { Time.rfc2822("2 Jul 00 00:51:37 JST") }
+ assert_raise(ArgumentError) { Time.rfc2822("01 11 2001 06:55:57 -0500") }
+ assert_raise(ArgumentError) { Time.rfc2822("18 \343\366\356\341\370 2000") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, Oct 2001 18:53:32") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 2 Nov 2001 03:47:54") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 27 Jul 2001 11.14.14 +0200") }
+ assert_raise(ArgumentError) { Time.rfc2822("Thu, 2 Nov 2000 04:13:53 -600") }
+ assert_raise(ArgumentError) { Time.rfc2822("Wed, 5 Apr 2000 22:57:09 JST") }
+ assert_raise(ArgumentError) { Time.rfc2822("Mon, 11 Sep 2000 19:47:33 00000") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 28 Apr 2000 20:40:47 +-900") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 19 Jan 2001 8:15:36 AM -0500") }
+ assert_raise(ArgumentError) { Time.rfc2822("Thursday, Sep 27 2001 7:42:35 AM EST") }
+ assert_raise(ArgumentError) { Time.rfc2822("3/11/2001 1:31:57 PM Pacific Daylight Time") }
+ assert_raise(ArgumentError) { Time.rfc2822("Mi, 28 Mrz 2001 11:51:36") }
+ assert_raise(ArgumentError) { Time.rfc2822("P, 30 sept 2001 23:03:14") }
+ assert_raise(ArgumentError) { Time.rfc2822("fr, 11 aug 2000 18:39:22") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fr, 21 Sep 2001 17:44:03 -1000") }
+ assert_raise(ArgumentError) { Time.rfc2822("Mo, 18 Jun 2001 19:21:40 -1000") }
+ assert_raise(ArgumentError) { Time.rfc2822("l\366, 12 aug 2000 18:53:20") }
+ assert_raise(ArgumentError) { Time.rfc2822("l\366, 26 maj 2001 00:15:58") }
+ assert_raise(ArgumentError) { Time.rfc2822("Dom, 30 Sep 2001 17:36:30") }
+ assert_raise(ArgumentError) { Time.rfc2822("%&, 31 %2/ 2000 15:44:47 -0500") }
+ assert_raise(ArgumentError) { Time.rfc2822("dom, 26 ago 2001 03:57:07 -0300") }
+ assert_raise(ArgumentError) { Time.rfc2822("ter, 04 set 2001 16:27:58 -0300") }
+ assert_raise(ArgumentError) { Time.rfc2822("Wen, 3 oct 2001 23:17:49 -0400") }
+ assert_raise(ArgumentError) { Time.rfc2822("Wen, 3 oct 2001 23:17:49 -0400") }
+ assert_raise(ArgumentError) { Time.rfc2822("ele, 11 h: 2000 12:42:15 -0500") }
+ assert_raise(ArgumentError) { Time.rfc2822("Tue, 14 Aug 2001 3:55:3 +0200") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 25 Aug 2000 9:3:48 +0800") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 1 Dec 2000 0:57:50 EST") }
+ assert_raise(ArgumentError) { Time.rfc2822("Mon, 7 May 2001 9:39:51 +0200") }
+ assert_raise(ArgumentError) { Time.rfc2822("Wed, 1 Aug 2001 16:9:15 +0200") }
+ assert_raise(ArgumentError) { Time.rfc2822("Wed, 23 Aug 2000 9:17:36 +0800") }
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 11 Aug 2000 10:4:42 +0800") }
+ assert_raise(ArgumentError) { Time.rfc2822("Sat, 15 Sep 2001 13:22:2 +0300") }
+ assert_raise(ArgumentError) { Time.rfc2822("Wed,16 \276\305\324\302 2001 20:06:25 +0800") }
+ assert_raise(ArgumentError) { Time.rfc2822("Wed,7 \312\256\322\273\324\302 2001 23:47:22 +0800") }
+ assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=C5=DA),?= 10 2 2001 23:32:26 +0900 (JST)") }
+ assert_raise(ArgumentError) { Time.rfc2822("\307\341\314\343\332\311, 30 \344\346\335\343\310\321 2001 10:01:06") }
+ assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=BF=E5),?= 12 =?iso-8859-1?Q?9=B7=EE?= 2001 14:52:41\n+0900 (JST)") }
+ end
+
+ def test_zone_0000
+ assert_equal(true, Time.parse("2000-01-01T00:00:00Z").utc?)
+ assert_equal(true, Time.parse("2000-01-01T00:00:00-00:00").utc?)
+ assert_equal(false, Time.parse("2000-01-01T00:00:00+00:00").utc?)
+ assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
+ assert_equal(true, Time.parse("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
+ assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
+ assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
+ assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
+ assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
+ assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 UTC").utc?)
+ end
+
+ def test_rfc2822_utc_roundtrip_winter
+ t1 = Time.local(2008,12,1)
+ t2 = Time.rfc2822(t1.rfc2822)
+ assert_equal(t1.utc?, t2.utc?, "[ruby-dev:37126]")
+ end
+
+ def test_rfc2822_utc_roundtrip_summer
+ t1 = Time.local(2008,8,1)
+ t2 = Time.rfc2822(t1.rfc2822)
+ assert_equal(t1.utc?, t2.utc?)
+ end
+
+ def test_parse_leap_second
+ t = Time.utc(1998,12,31,23,59,59)
+ assert_equal(t, Time.parse("Thu Dec 31 23:59:59 UTC 1998"))
+ assert_equal(t, Time.parse("Fri Dec 31 23:59:59 -0000 1998"));t.localtime
+ assert_equal(t, Time.parse("Fri Jan 1 08:59:59 +0900 1999"))
+ assert_equal(t, Time.parse("Fri Jan 1 00:59:59 +0100 1999"))
+ assert_equal(t, Time.parse("Fri Dec 31 23:59:59 +0000 1998"))
+ assert_equal(t, Time.parse("Fri Dec 31 22:59:59 -0100 1998"));t.utc
+ t += 1
+ assert_equal(t, Time.parse("Thu Dec 31 23:59:60 UTC 1998"))
+ assert_equal(t, Time.parse("Fri Dec 31 23:59:60 -0000 1998"));t.localtime
+ assert_equal(t, Time.parse("Fri Jan 1 08:59:60 +0900 1999"))
+ assert_equal(t, Time.parse("Fri Jan 1 00:59:60 +0100 1999"))
+ assert_equal(t, Time.parse("Fri Dec 31 23:59:60 +0000 1998"))
+ assert_equal(t, Time.parse("Fri Dec 31 22:59:60 -0100 1998"));t.utc
+ t += 1 if t.sec == 60
+ assert_equal(t, Time.parse("Thu Jan 1 00:00:00 UTC 1999"))
+ assert_equal(t, Time.parse("Fri Jan 1 00:00:00 -0000 1999"));t.localtime
+ assert_equal(t, Time.parse("Fri Jan 1 09:00:00 +0900 1999"))
+ assert_equal(t, Time.parse("Fri Jan 1 01:00:00 +0100 1999"))
+ assert_equal(t, Time.parse("Fri Jan 1 00:00:00 +0000 1999"))
+ assert_equal(t, Time.parse("Fri Dec 31 23:00:00 -0100 1998"))
+ end
+
+ def test_rfc2822_leap_second
+ t = Time.utc(1998,12,31,23,59,59)
+ assert_equal(t, Time.rfc2822("Thu, 31 Dec 1998 23:59:59 UTC"))
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:59 -0000"));t.localtime
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 08:59:59 +0900"))
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:59:59 +0100"))
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:59 +0000"))
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 22:59:59 -0100"));t.utc
+ t += 1
+ assert_equal(t, Time.rfc2822("Thu, 31 Dec 1998 23:59:60 UTC"))
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:60 -0000"));t.localtime
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 08:59:60 +0900"))
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:59:60 +0100"))
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:60 +0000"))
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 22:59:60 -0100"));t.utc
+ t += 1 if t.sec == 60
+ assert_equal(t, Time.rfc2822("Thu, 1 Jan 1999 00:00:00 UTC"))
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:00:00 -0000"));t.localtime
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 09:00:00 +0900"))
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 01:00:00 +0100"))
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:00:00 +0000"))
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:00:00 -0100"))
+ end
+
+ def test_xmlschema_leap_second
+ t = Time.utc(1998,12,31,23,59,59)
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:59Z"))
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:59-00:00"));t.localtime
+ assert_equal(t, Time.xmlschema("1999-01-01T08:59:59+09:00"))
+ assert_equal(t, Time.xmlschema("1999-01-01T00:59:59+01:00"))
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:59+00:00"))
+ assert_equal(t, Time.xmlschema("1998-12-31T22:59:59-01:00"));t.utc
+ t += 1
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:60Z"))
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:60-00:00"));t.localtime
+ assert_equal(t, Time.xmlschema("1999-01-01T08:59:60+09:00"))
+ assert_equal(t, Time.xmlschema("1999-01-01T00:59:60+01:00"))
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:60+00:00"))
+ assert_equal(t, Time.xmlschema("1998-12-31T22:59:60-01:00"));t.utc
+ t += 1 if t.sec == 60
+ assert_equal(t, Time.xmlschema("1999-01-01T00:00:00Z"))
+ assert_equal(t, Time.xmlschema("1999-01-01T00:00:00-00:00"));t.localtime
+ assert_equal(t, Time.xmlschema("1999-01-01T09:00:00+09:00"))
+ assert_equal(t, Time.xmlschema("1999-01-01T01:00:00+01:00"))
+ assert_equal(t, Time.xmlschema("1999-01-01T00:00:00+00:00"))
+ assert_equal(t, Time.xmlschema("1998-12-31T23:00:00-01:00"))
+ end
+
+ def test_xmlschema_fraction
+ assert_equal(500000, Time.xmlschema("2000-01-01T00:00:00.5+00:00").tv_usec)
+ end
+
+ def test_ruby_talk_152866
+ t = Time::xmlschema('2005-08-30T22:48:00-07:00')
+ assert_equal(31, t.day)
+ assert_equal(8, t.mon)
+ end
+
+ def test_parse_fraction
+ assert_equal(500000, Time.parse("2000-01-01T00:00:00.5+00:00").tv_usec)
+ end
+
+ def test_strptime
+ assert_equal(Time.utc(2005, 8, 28, 06, 54, 20), Time.strptime("28/Aug/2005:06:54:20 +0000", "%d/%b/%Y:%T %z"))
+ end
+
+ def test_nsec
+ assert_equal(123456789, Time.xmlschema("2000-01-01T00:00:00.123456789+00:00").tv_nsec)
+ assert_equal(123456789, Time.parse("2000-01-01T00:00:00.123456789+00:00").tv_nsec)
+ end
+ end
+end
diff --git a/ruby/lib/timeout.rb b/ruby/lib/timeout.rb
new file mode 100644
index 0000000..19ccb96
--- /dev/null
+++ b/ruby/lib/timeout.rb
@@ -0,0 +1,108 @@
+# = timeout.rb
+#
+# execution timeout
+#
+# = Synopsis
+#
+# require 'timeout'
+# status = Timeout::timeout(5) {
+# # Something that should be interrupted if it takes too much time...
+# }
+#
+# = Description
+#
+# A way of performing a potentially long-running operation in a thread, and terminating
+# it's execution if it hasn't finished by a fixed amount of time.
+#
+# Previous versions of timeout didn't provide use a module for namespace. This version
+# provides both Timeout.timeout, and a backwards-compatible #timeout.
+#
+# = Copyright
+#
+# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
+# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
+
+module Timeout
+ # Raised by Timeout#timeout when the block times out.
+ class Error < RuntimeError
+ end
+ class ExitException < ::Exception # :nodoc:
+ end
+
+ THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
+ CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
+
+ # Executes the method's block. If the block execution terminates before
+ # +sec+ seconds has passed, it returns the result value of the block.
+ # If not, it terminates the execution and raises +exception+ (which defaults
+ # to Timeout::Error).
+ #
+ # Note that this is both a method of module Timeout, so you can 'include Timeout'
+ # into your classes so they have a #timeout method, as well as a module method,
+ # so you can call it directly as Timeout.timeout().
+ def timeout(sec, klass = nil) #:yield: +sec+
+ return yield(sec) if sec == nil or sec.zero?
+ exception = klass || Class.new(ExitException)
+ begin
+ x = Thread.current
+ y = Thread.start {
+ sleep sec
+ x.raise exception, "execution expired" if x.alive?
+ }
+ return yield(sec)
+ rescue exception => e
+ rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
+ (bt = e.backtrace).reject! {|m| rej =~ m}
+ level = -caller(CALLER_OFFSET).size
+ while THIS_FILE =~ bt[level]
+ bt.delete_at(level)
+ level += 1
+ end
+ raise if klass # if exception class is specified, it
+ # would be expected outside.
+ raise Error, e.message, e.backtrace
+ ensure
+ if y and y.alive?
+ y.kill
+ y.join # make sure y is dead.
+ end
+ end
+ end
+
+ module_function :timeout
+end
+
+# Identical to:
+#
+# Timeout::timeout(n, e, &block).
+#
+# Defined for backwards compatibility with earlier versions of timeout.rb, see
+# Timeout#timeout.
+def timeout(n, e = nil, &block)
+ Timeout::timeout(n, e, &block)
+end
+
+# Another name for Timeout::Error, defined for backwards compatibility with
+# earlier versions of timeout.rb.
+TimeoutError = Timeout::Error
+
+if __FILE__ == $0
+ p timeout(5) {
+ 45
+ }
+ p timeout(5, TimeoutError) {
+ 45
+ }
+ p timeout(nil) {
+ 54
+ }
+ p timeout(0) {
+ 54
+ }
+ p timeout(5) {
+ loop {
+ p 10
+ sleep 1
+ }
+ }
+end
diff --git a/ruby/lib/tmpdir.rb b/ruby/lib/tmpdir.rb
new file mode 100644
index 0000000..8cecb8f
--- /dev/null
+++ b/ruby/lib/tmpdir.rb
@@ -0,0 +1,138 @@
+#
+# tmpdir - retrieve temporary directory path
+#
+# $Id: tmpdir.rb 23781 2009-06-21 09:14:14Z yugui $
+#
+
+require 'fileutils'
+
+class Dir
+
+ @@systmpdir = '/tmp'
+
+ begin
+ require 'Win32API'
+ CSIDL_LOCAL_APPDATA = 0x001c
+ max_pathlen = 260
+ windir = "\0"*(max_pathlen+1)
+ begin
+ getdir = Win32API.new('shell32', 'SHGetFolderPath', 'LLLLP', 'L')
+ raise RuntimeError if getdir.call(0, CSIDL_LOCAL_APPDATA, 0, 0, windir) != 0
+ windir = File.expand_path(windir.rstrip)
+ rescue RuntimeError
+ begin
+ getdir = Win32API.new('kernel32', 'GetSystemWindowsDirectory', 'PL', 'L')
+ rescue RuntimeError
+ getdir = Win32API.new('kernel32', 'GetWindowsDirectory', 'PL', 'L')
+ end
+ len = getdir.call(windir, windir.size)
+ windir = File.expand_path(windir[0, len])
+ end
+ windir.force_encoding(Dir.pwd.encoding)
+ temp = File.join(windir.untaint, 'temp')
+ @@systmpdir = temp if File.directory?(temp) and File.writable?(temp)
+ rescue LoadError
+ end
+
+ ##
+ # Returns the operating system's temporary file path.
+
+ def Dir::tmpdir
+ tmp = '.'
+ if $SAFE > 0
+ tmp = @@systmpdir
+ else
+ for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'],
+ ENV['USERPROFILE'], @@systmpdir, '/tmp']
+ if dir and File.directory?(dir) and File.writable?(dir)
+ tmp = dir
+ break
+ end
+ end
+ File.expand_path(tmp)
+ end
+ end
+
+ # Dir.mktmpdir creates a temporary directory.
+ #
+ # The directory is created with 0700 permission.
+ #
+ # The prefix and suffix of the name of the directory is specified by
+ # the optional first argument, <i>prefix_suffix</i>.
+ # - If it is not specified or nil, "d" is used as the prefix and no suffix is used.
+ # - If it is a string, it is used as the prefix and no suffix is used.
+ # - If it is an array, first element is used as the prefix and second element is used as a suffix.
+ #
+ # Dir.mktmpdir {|dir| dir is ".../d..." }
+ # Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
+ # Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
+ #
+ # The directory is created under Dir.tmpdir or
+ # the optional second argument <i>tmpdir</i> if non-nil value is given.
+ #
+ # Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
+ # Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
+ #
+ # If a block is given,
+ # it is yielded with the path of the directory.
+ # The directory and its contents are removed
+ # using FileUtils.remove_entry_secure before Dir.mktmpdir returns.
+ # The value of the block is returned.
+ #
+ # Dir.mktmpdir {|dir|
+ # # use the directory...
+ # open("#{dir}/foo", "w") { ... }
+ # }
+ #
+ # If a block is not given,
+ # The path of the directory is returned.
+ # In this case, Dir.mktmpdir doesn't remove the directory.
+ #
+ # dir = Dir.mktmpdir
+ # begin
+ # # use the directory...
+ # open("#{dir}/foo", "w") { ... }
+ # ensure
+ # # remove the directory.
+ # FileUtils.remove_entry_secure dir
+ # end
+ #
+ def Dir.mktmpdir(prefix_suffix=nil, tmpdir=nil)
+ case prefix_suffix
+ when nil
+ prefix = "d"
+ suffix = ""
+ when String
+ prefix = prefix_suffix
+ suffix = ""
+ when Array
+ prefix = prefix_suffix[0]
+ suffix = prefix_suffix[1]
+ else
+ raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
+ end
+ tmpdir ||= Dir.tmpdir
+ t = Time.now.strftime("%Y%m%d")
+ n = nil
+ begin
+ path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
+ path << "-#{n}" if n
+ path << suffix
+ Dir.mkdir(path, 0700)
+ rescue Errno::EEXIST
+ n ||= 0
+ n += 1
+ retry
+ end
+
+ if block_given?
+ begin
+ yield path
+ ensure
+ FileUtils.remove_entry_secure path
+ end
+ else
+ path
+ end
+ end
+end
diff --git a/ruby/lib/tracer.rb b/ruby/lib/tracer.rb
new file mode 100644
index 0000000..9d197a6
--- /dev/null
+++ b/ruby/lib/tracer.rb
@@ -0,0 +1,166 @@
+#
+# tracer.rb -
+# $Release Version: 0.2$
+# $Revision: 1.8 $
+# by Keiju ISHITSUKA(Nippon Rational Inc.)
+#
+# --
+#
+#
+#
+
+#
+# tracer main class
+#
+class Tracer
+ @RCS_ID='-$Id: tracer.rb,v 1.8 1998/05/19 03:42:49 keiju Exp keiju $-'
+
+ @stdout = STDOUT
+ @verbose = false
+ class << self
+ attr_accessor :verbose
+ alias verbose? verbose
+ attr_accessor :stdout
+ end
+
+ EVENT_SYMBOL = {
+ "line" => "-",
+ "call" => ">",
+ "return" => "<",
+ "class" => "C",
+ "end" => "E",
+ "c-call" => ">",
+ "c-return" => "<",
+ }
+
+ def initialize
+ @threads = Hash.new
+ if defined? Thread.main
+ @threads[Thread.main.object_id] = 0
+ else
+ @threads[Thread.current.object_id] = 0
+ end
+
+ @get_line_procs = {}
+
+ @filters = []
+ end
+
+ def stdout
+ Tracer.stdout
+ end
+
+ def on
+ if block_given?
+ on
+ begin
+ yield
+ ensure
+ off
+ end
+ else
+ set_trace_func method(:trace_func).to_proc
+ stdout.print "Trace on\n" if Tracer.verbose?
+ end
+ end
+
+ def off
+ set_trace_func nil
+ stdout.print "Trace off\n" if Tracer.verbose?
+ end
+
+ def add_filter(p = proc)
+ @filters.push p
+ end
+
+ def set_get_line_procs(file, p = proc)
+ @get_line_procs[file] = p
+ end
+
+ def get_line(file, line)
+ if p = @get_line_procs[file]
+ return p.call(line)
+ end
+
+ unless list = SCRIPT_LINES__[file]
+ begin
+ f = open(file)
+ begin
+ SCRIPT_LINES__[file] = list = f.readlines
+ ensure
+ f.close
+ end
+ rescue
+ SCRIPT_LINES__[file] = list = []
+ end
+ end
+
+ if l = list[line - 1]
+ l
+ else
+ "-\n"
+ end
+ end
+
+ def get_thread_no
+ if no = @threads[Thread.current.object_id]
+ no
+ else
+ @threads[Thread.current.object_id] = @threads.size
+ end
+ end
+
+ def trace_func(event, file, line, id, binding, klass, *)
+ return if file == __FILE__
+
+ for p in @filters
+ return unless p.call event, file, line, id, binding, klass
+ end
+
+ # saved_crit = Thread.critical
+ # Thread.critical = true
+ stdout.printf("#%d:%s:%d:%s:%s: %s",
+ get_thread_no,
+ file,
+ line,
+ klass || '',
+ EVENT_SYMBOL[event],
+ line == 0 ? "?\n" : get_line(file, line))
+ # Thread.critical = saved_crit
+ end
+
+ Single = new
+ def Tracer.on
+ if block_given?
+ Single.on{yield}
+ else
+ Single.on
+ end
+ end
+
+ def Tracer.off
+ Single.off
+ end
+
+ def Tracer.set_get_line_procs(file_name, p = proc)
+ Single.set_get_line_procs(file_name, p)
+ end
+
+ def Tracer.add_filter(p = proc)
+ Single.add_filter(p)
+ end
+
+end
+
+SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
+
+if $0 == __FILE__
+ # direct call
+
+ $0 = ARGV[0]
+ ARGV.shift
+ Tracer.on
+ require $0
+elsif caller.size == 1
+ Tracer.on
+end
diff --git a/ruby/lib/tsort.rb b/ruby/lib/tsort.rb
new file mode 100644
index 0000000..9fc4fea
--- /dev/null
+++ b/ruby/lib/tsort.rb
@@ -0,0 +1,290 @@
+#!/usr/bin/env ruby
+#--
+# tsort.rb - provides a module for topological sorting and strongly connected components.
+#++
+#
+
+#
+# TSort implements topological sorting using Tarjan's algorithm for
+# strongly connected components.
+#
+# TSort is designed to be able to be used with any object which can be
+# interpreted as a directed graph.
+#
+# TSort requires two methods to interpret an object as a graph,
+# tsort_each_node and tsort_each_child.
+#
+# * tsort_each_node is used to iterate for all nodes over a graph.
+# * tsort_each_child is used to iterate for child nodes of a given node.
+#
+# The equality of nodes are defined by eql? and hash since
+# TSort uses Hash internally.
+#
+# == A Simple Example
+#
+# The following example demonstrates how to mix the TSort module into an
+# existing class (in this case, Hash). Here, we're treating each key in
+# the hash as a node in the graph, and so we simply alias the required
+# #tsort_each_node method to Hash's #each_key method. For each key in the
+# hash, the associated value is an array of the node's child nodes. This
+# choice in turn leads to our implementation of the required #tsort_each_child
+# method, which fetches the array of child nodes and then iterates over that
+# array using the user-supplied block.
+#
+# require 'tsort'
+#
+# class Hash
+# include TSort
+# alias tsort_each_node each_key
+# def tsort_each_child(node, &block)
+# fetch(node).each(&block)
+# end
+# end
+#
+# {1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort
+# #=> [3, 2, 1, 4]
+#
+# {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}.strongly_connected_components
+# #=> [[4], [2, 3], [1]]
+#
+# == A More Realistic Example
+#
+# A very simple `make' like tool can be implemented as follows:
+#
+# require 'tsort'
+#
+# class Make
+# def initialize
+# @dep = {}
+# @dep.default = []
+# end
+#
+# def rule(outputs, inputs=[], &block)
+# triple = [outputs, inputs, block]
+# outputs.each {|f| @dep[f] = [triple]}
+# @dep[triple] = inputs
+# end
+#
+# def build(target)
+# each_strongly_connected_component_from(target) {|ns|
+# if ns.length != 1
+# fs = ns.delete_if {|n| Array === n}
+# raise TSort::Cyclic.new("cyclic dependencies: #{fs.join ', '}")
+# end
+# n = ns.first
+# if Array === n
+# outputs, inputs, block = n
+# inputs_time = inputs.map {|f| File.mtime f}.max
+# begin
+# outputs_time = outputs.map {|f| File.mtime f}.min
+# rescue Errno::ENOENT
+# outputs_time = nil
+# end
+# if outputs_time == nil ||
+# inputs_time != nil && outputs_time <= inputs_time
+# sleep 1 if inputs_time != nil && inputs_time.to_i == Time.now.to_i
+# block.call
+# end
+# end
+# }
+# end
+#
+# def tsort_each_child(node, &block)
+# @dep[node].each(&block)
+# end
+# include TSort
+# end
+#
+# def command(arg)
+# print arg, "\n"
+# system arg
+# end
+#
+# m = Make.new
+# m.rule(%w[t1]) { command 'date > t1' }
+# m.rule(%w[t2]) { command 'date > t2' }
+# m.rule(%w[t3]) { command 'date > t3' }
+# m.rule(%w[t4], %w[t1 t3]) { command 'cat t1 t3 > t4' }
+# m.rule(%w[t5], %w[t4 t2]) { command 'cat t4 t2 > t5' }
+# m.build('t5')
+#
+# == Bugs
+#
+# * 'tsort.rb' is wrong name because this library uses
+# Tarjan's algorithm for strongly connected components.
+# Although 'strongly_connected_components.rb' is correct but too long.
+#
+# == References
+#
+# R. E. Tarjan, "Depth First Search and Linear Graph Algorithms",
+# <em>SIAM Journal on Computing</em>, Vol. 1, No. 2, pp. 146-160, June 1972.
+#
+
+module TSort
+ class Cyclic < StandardError
+ end
+
+ #
+ # Returns a topologically sorted array of nodes.
+ # The array is sorted from children to parents, i.e.
+ # the first element has no child and the last node has no parent.
+ #
+ # If there is a cycle, TSort::Cyclic is raised.
+ #
+ def tsort
+ result = []
+ tsort_each {|element| result << element}
+ result
+ end
+
+ #
+ # The iterator version of the #tsort method.
+ # <tt><em>obj</em>.tsort_each</tt> is similar to <tt><em>obj</em>.tsort.each</tt>, but
+ # modification of _obj_ during the iteration may lead to unexpected results.
+ #
+ # #tsort_each returns +nil+.
+ # If there is a cycle, TSort::Cyclic is raised.
+ #
+ def tsort_each # :yields: node
+ each_strongly_connected_component {|component|
+ if component.size == 1
+ yield component.first
+ else
+ raise Cyclic.new("topological sort failed: #{component.inspect}")
+ end
+ }
+ end
+
+ #
+ # Returns strongly connected components as an array of arrays of nodes.
+ # The array is sorted from children to parents.
+ # Each elements of the array represents a strongly connected component.
+ #
+ def strongly_connected_components
+ result = []
+ each_strongly_connected_component {|component| result << component}
+ result
+ end
+
+ #
+ # The iterator version of the #strongly_connected_components method.
+ # <tt><em>obj</em>.each_strongly_connected_component</tt> is similar to
+ # <tt><em>obj</em>.strongly_connected_components.each</tt>, but
+ # modification of _obj_ during the iteration may lead to unexpected results.
+ #
+ #
+ # #each_strongly_connected_component returns +nil+.
+ #
+ def each_strongly_connected_component # :yields: nodes
+ id_map = {}
+ stack = []
+ tsort_each_node {|node|
+ unless id_map.include? node
+ each_strongly_connected_component_from(node, id_map, stack) {|c|
+ yield c
+ }
+ end
+ }
+ nil
+ end
+
+ #
+ # Iterates over strongly connected component in the subgraph reachable from
+ # _node_.
+ #
+ # Return value is unspecified.
+ #
+ # #each_strongly_connected_component_from doesn't call #tsort_each_node.
+ #
+ def each_strongly_connected_component_from(node, id_map={}, stack=[]) # :yields: nodes
+ minimum_id = node_id = id_map[node] = id_map.size
+ stack_length = stack.length
+ stack << node
+
+ tsort_each_child(node) {|child|
+ if id_map.include? child
+ child_id = id_map[child]
+ minimum_id = child_id if child_id && child_id < minimum_id
+ else
+ sub_minimum_id =
+ each_strongly_connected_component_from(child, id_map, stack) {|c|
+ yield c
+ }
+ minimum_id = sub_minimum_id if sub_minimum_id < minimum_id
+ end
+ }
+
+ if node_id == minimum_id
+ component = stack.slice!(stack_length .. -1)
+ component.each {|n| id_map[n] = nil}
+ yield component
+ end
+
+ minimum_id
+ end
+
+ #
+ # Should be implemented by a extended class.
+ #
+ # #tsort_each_node is used to iterate for all nodes over a graph.
+ #
+ def tsort_each_node # :yields: node
+ raise NotImplementedError.new
+ end
+
+ #
+ # Should be implemented by a extended class.
+ #
+ # #tsort_each_child is used to iterate for child nodes of _node_.
+ #
+ def tsort_each_child(node) # :yields: child
+ raise NotImplementedError.new
+ end
+end
+
+if __FILE__ == $0
+ require 'test/unit'
+
+ class TSortHash < Hash # :nodoc:
+ include TSort
+ alias tsort_each_node each_key
+ def tsort_each_child(node, &block)
+ fetch(node).each(&block)
+ end
+ end
+
+ class TSortArray < Array # :nodoc:
+ include TSort
+ alias tsort_each_node each_index
+ def tsort_each_child(node, &block)
+ fetch(node).each(&block)
+ end
+ end
+
+ class TSortTest < Test::Unit::TestCase # :nodoc:
+ def test_dag
+ h = TSortHash[{1=>[2, 3], 2=>[3], 3=>[]}]
+ assert_equal([3, 2, 1], h.tsort)
+ assert_equal([[3], [2], [1]], h.strongly_connected_components)
+ end
+
+ def test_cycle
+ h = TSortHash[{1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}]
+ assert_equal([[4], [2, 3], [1]],
+ h.strongly_connected_components.map {|nodes| nodes.sort})
+ assert_raise(TSort::Cyclic) { h.tsort }
+ end
+
+ def test_array
+ a = TSortArray[[1], [0], [0], [2]]
+ assert_equal([[0, 1], [2], [3]],
+ a.strongly_connected_components.map {|nodes| nodes.sort})
+
+ a = TSortArray[[], [0]]
+ assert_equal([[0], [1]],
+ a.strongly_connected_components.map {|nodes| nodes.sort})
+ end
+ end
+
+end
+
diff --git a/ruby/lib/ubygems.rb b/ruby/lib/ubygems.rb
new file mode 100644
index 0000000..fec880f
--- /dev/null
+++ b/ruby/lib/ubygems.rb
@@ -0,0 +1,10 @@
+# This file allows for the running of rubygems with a nice
+# command line look-and-feel: ruby -rubygems foo.rb
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+
+require 'rubygems'
diff --git a/ruby/lib/un.rb b/ruby/lib/un.rb
new file mode 100644
index 0000000..42afeac
--- /dev/null
+++ b/ruby/lib/un.rb
@@ -0,0 +1,304 @@
+#
+# = un.rb
+#
+# Copyright (c) 2003 WATANABE Hirofumi <eban@ruby-lang.org>
+#
+# This program is free software.
+# You can distribute/modify this program under the same terms of Ruby.
+#
+# == Utilities to replace common UNIX commands in Makefiles etc
+#
+# == SYNOPSIS
+#
+# ruby -run -e cp -- [OPTION] SOURCE DEST
+# ruby -run -e ln -- [OPTION] TARGET LINK_NAME
+# ruby -run -e mv -- [OPTION] SOURCE DEST
+# ruby -run -e rm -- [OPTION] FILE
+# ruby -run -e mkdir -- [OPTION] DIRS
+# ruby -run -e rmdir -- [OPTION] DIRS
+# ruby -run -e install -- [OPTION] SOURCE DEST
+# ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
+# ruby -run -e touch -- [OPTION] FILE
+# ruby -run -e wait_writable -- [OPTION] FILE
+# ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION]
+# ruby -run -e help [COMMAND]
+
+require "fileutils"
+require "optparse"
+
+module FileUtils
+# @fileutils_label = ""
+ @fileutils_output = $stdout
+end
+
+def setup(options = "", *long_options)
+ opt_hash = {}
+ argv = []
+ OptionParser.new do |o|
+ options.scan(/.:?/) do |s|
+ opt_name = s.delete(":").intern
+ o.on("-" + s.tr(":", " ")) do |val|
+ opt_hash[opt_name] = val
+ end
+ end
+ long_options.each do |s|
+ opt_name = s[/\A(?:--)?([^\s=]+)/, 1].intern
+ o.on(s.sub(/\A(?!--)/, '--')) do |val|
+ opt_hash[opt_name] = val
+ end
+ end
+ o.on("-v") do opt_hash[:verbose] = true end
+ o.order!(ARGV) do |x|
+ if /[*?\[{]/ =~ x
+ argv.concat(Dir[x])
+ else
+ argv << x
+ end
+ end
+ end
+ yield argv, opt_hash
+end
+
+##
+# Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
+#
+# ruby -run -e cp -- [OPTION] SOURCE DEST
+#
+# -p preserve file attributes if possible
+# -r copy recursively
+# -v verbose
+#
+
+def cp
+ setup("pr") do |argv, options|
+ cmd = "cp"
+ cmd += "_r" if options.delete :r
+ options[:preserve] = true if options.delete :p
+ dest = argv.pop
+ argv = argv[0] if argv.size == 1
+ FileUtils.send cmd, argv, dest, options
+ end
+end
+
+##
+# Create a link to the specified TARGET with LINK_NAME.
+#
+# ruby -run -e ln -- [OPTION] TARGET LINK_NAME
+#
+# -s make symbolic links instead of hard links
+# -f remove existing destination files
+# -v verbose
+#
+
+def ln
+ setup("sf") do |argv, options|
+ cmd = "ln"
+ cmd += "_s" if options.delete :s
+ options[:force] = true if options.delete :f
+ dest = argv.pop
+ argv = argv[0] if argv.size == 1
+ FileUtils.send cmd, argv, dest, options
+ end
+end
+
+##
+# Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
+#
+# ruby -run -e mv -- [OPTION] SOURCE DEST
+#
+# -v verbose
+#
+
+def mv
+ setup do |argv, options|
+ dest = argv.pop
+ argv = argv[0] if argv.size == 1
+ FileUtils.mv argv, dest, options
+ end
+end
+
+##
+# Remove the FILE
+#
+# ruby -run -e rm -- [OPTION] FILE
+#
+# -f ignore nonexistent files
+# -r remove the contents of directories recursively
+# -v verbose
+#
+
+def rm
+ setup("fr") do |argv, options|
+ cmd = "rm"
+ cmd += "_r" if options.delete :r
+ options[:force] = true if options.delete :f
+ FileUtils.send cmd, argv, options
+ end
+end
+
+##
+# Create the DIR, if they do not already exist.
+#
+# ruby -run -e mkdir -- [OPTION] DIR
+#
+# -p no error if existing, make parent directories as needed
+# -v verbose
+#
+
+def mkdir
+ setup("p") do |argv, options|
+ cmd = "mkdir"
+ cmd += "_p" if options.delete :p
+ FileUtils.send cmd, argv, options
+ end
+end
+
+##
+# Remove the DIR.
+#
+# ruby -run -e rmdir -- [OPTION] DIR
+#
+# -p remove DIRECTORY and its ancestors.
+# -v verbose
+#
+
+def rmdir
+ setup("p") do |argv, options|
+ options[:parents] = true if options.delete :p
+ FileUtils.rmdir argv, options
+ end
+end
+
+##
+# Copy SOURCE to DEST.
+#
+# ruby -run -e install -- [OPTION] SOURCE DEST
+#
+# -p apply access/modification times of SOURCE files to
+# corresponding destination files
+# -m set permission mode (as in chmod), instead of 0755
+# -v verbose
+#
+
+def install
+ setup("pm:") do |argv, options|
+ options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
+ options[:preserve] = true if options.delete :p
+ dest = argv.pop
+ argv = argv[0] if argv.size == 1
+ FileUtils.install argv, dest, options
+ end
+end
+
+##
+# Change the mode of each FILE to OCTAL-MODE.
+#
+# ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
+#
+# -v verbose
+#
+
+def chmod
+ setup do |argv, options|
+ mode = argv.shift.oct
+ FileUtils.chmod mode, argv, options
+ end
+end
+
+##
+# Update the access and modification times of each FILE to the current time.
+#
+# ruby -run -e touch -- [OPTION] FILE
+#
+# -v verbose
+#
+
+def touch
+ setup do |argv, options|
+ FileUtils.touch argv, options
+ end
+end
+
+##
+# Wait until the file becomes writable.
+#
+# ruby -run -e wait_writable -- [OPTION] FILE
+#
+# -n RETRY count to retry
+# -w SEC each wait time in seconds
+# -v verbose
+#
+
+def wait_writable
+ setup("n:w:v") do |argv, options|
+ verbose = options[:verbose]
+ n = options[:n] and n = Integer(n)
+ wait = (wait = options[:w]) ? Float(wait) : 0.2
+ argv.each do |file|
+ begin
+ open(file, "r+b")
+ rescue Errno::ENOENT
+ break
+ rescue Errno::EACCES => e
+ raise if n and (n -= 1) <= 0
+ puts e
+ STDOUT.flush
+ sleep wait
+ retry
+ end
+ end
+ end
+end
+
+##
+# Create makefile using mkmf.
+#
+# ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION]
+#
+# -d ARGS run dir_config
+# -h ARGS run have_header
+# -l ARGS run have_library
+# -f ARGS run have_func
+# -v ARGS run have_var
+# -t ARGS run have_type
+# -m ARGS run have_macro
+# -c ARGS run have_const
+# --vendor install to vendor_ruby
+#
+
+def mkmf
+ setup("d:h:l:f:v:t:m:c:", "vendor") do |argv, options|
+ require 'mkmf'
+ opt = options[:d] and opt.split(/:/).each {|n| dir_config(*n.split(/,/))}
+ opt = options[:h] and opt.split(/:/).each {|n| have_header(*n.split(/,/))}
+ opt = options[:l] and opt.split(/:/).each {|n| have_library(*n.split(/,/))}
+ opt = options[:f] and opt.split(/:/).each {|n| have_func(*n.split(/,/))}
+ opt = options[:v] and opt.split(/:/).each {|n| have_var(*n.split(/,/))}
+ opt = options[:t] and opt.split(/:/).each {|n| have_type(*n.split(/,/))}
+ opt = options[:m] and opt.split(/:/).each {|n| have_macro(*n.split(/,/))}
+ opt = options[:c] and opt.split(/:/).each {|n| have_const(*n.split(/,/))}
+ $configure_args["--vendor"] = true if options[:vendor]
+ create_makefile(*argv)
+ end
+end
+
+##
+# Display help message.
+#
+# ruby -run -e help [COMMAND]
+#
+
+def help
+ setup do |argv,|
+ all = argv.empty?
+ open(__FILE__) do |me|
+ while me.gets("##\n")
+ if help = me.gets("\n\n")
+ if all or argv.delete help[/-e \w+/].sub(/-e /, "")
+ print help.gsub(/^# ?/, "")
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/uri.rb b/ruby/lib/uri.rb
new file mode 100644
index 0000000..e354612
--- /dev/null
+++ b/ruby/lib/uri.rb
@@ -0,0 +1,29 @@
+#
+# URI support for Ruby
+#
+# Author:: Akira Yamada <akira@ruby-lang.org>
+# Documentation:: Akira Yamada <akira@ruby-lang.org>, Dmitry V. Sabanin <sdmitry@lrn.ru>
+# License::
+# Copyright (c) 2001 akira yamada <akira@ruby-lang.org>
+# You can redistribute it and/or modify it under the same term as Ruby.
+# Revision:: $Id: uri.rb 13772 2007-10-25 00:53:34Z akira $
+#
+# See URI for documentation
+#
+
+module URI
+ # :stopdoc:
+ VERSION_CODE = '000911'.freeze
+ VERSION = VERSION_CODE.scan(/../).collect{|n| n.to_i}.join('.').freeze
+ # :startdoc:
+
+end
+
+require 'uri/common'
+require 'uri/generic'
+require 'uri/ftp'
+require 'uri/http'
+require 'uri/https'
+require 'uri/ldap'
+require 'uri/ldaps'
+require 'uri/mailto'
diff --git a/ruby/lib/uri/common.rb b/ruby/lib/uri/common.rb
new file mode 100644
index 0000000..5853e9c
--- /dev/null
+++ b/ruby/lib/uri/common.rb
@@ -0,0 +1,727 @@
+# = uri/common.rb
+#
+# Author:: Akira Yamada <akira@ruby-lang.org>
+# Revision:: $Id: common.rb 22760 2009-03-04 09:21:12Z yugui $
+# License::
+# You can redistribute it and/or modify it under the same term as Ruby.
+#
+
+module URI
+ module REGEXP
+ #
+ # Patterns used to parse URI's
+ #
+ module PATTERN
+ # :stopdoc:
+
+ # RFC 2396 (URI Generic Syntax)
+ # RFC 2732 (IPv6 Literal Addresses in URL's)
+ # RFC 2373 (IPv6 Addressing Architecture)
+
+ # alpha = lowalpha | upalpha
+ ALPHA = "a-zA-Z"
+ # alphanum = alpha | digit
+ ALNUM = "#{ALPHA}\\d"
+
+ # hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
+ # "a" | "b" | "c" | "d" | "e" | "f"
+ HEX = "a-fA-F\\d"
+ # escaped = "%" hex hex
+ ESCAPED = "%[#{HEX}]{2}"
+ # mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
+ # "(" | ")"
+ # unreserved = alphanum | mark
+ UNRESERVED = "-_.!~*'()#{ALNUM}"
+ # reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
+ # "$" | ","
+ # reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
+ # "$" | "," | "[" | "]" (RFC 2732)
+ RESERVED = ";/?:@&=+$,\\[\\]"
+
+ # domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
+ DOMLABEL = "(?:[#{ALNUM}](?:[-#{ALNUM}]*[#{ALNUM}])?)"
+ # toplabel = alpha | alpha *( alphanum | "-" ) alphanum
+ TOPLABEL = "(?:[#{ALPHA}](?:[-#{ALNUM}]*[#{ALNUM}])?)"
+ # hostname = *( domainlabel "." ) toplabel [ "." ]
+ HOSTNAME = "(?:#{DOMLABEL}\\.)*#{TOPLABEL}\\.?"
+
+ # :startdoc:
+ end # PATTERN
+
+ # :startdoc:
+ end # REGEXP
+
+ class Parser
+ include REGEXP
+
+ #
+ # == Synopsis
+ #
+ # URI::Parser.new([opts])
+ #
+ # == Args
+ #
+ # The constructor accepts a hash as options for parser.
+ # Keys of options are pattern names of URI components
+ # and values of options are pattern strings.
+ # The constructor generetes set of regexps for parsing URIs.
+ #
+ # You can use the following keys:
+ #
+ # * <tt>:ESCAPED</tt> (URI::PATTERN::ESCAPED in default)
+ # * <tt>:UNRESERVED</tt> (URI::PATTERN::UNRESERVED in default)
+ # * <tt>:DOMLABEL</tt> (URI::PATTERN::DOMLABEL in default)
+ # * <tt>:TOPLABEL</tt> (URI::PATTERN::TOPLABEL in default)
+ # * <tt>:HOSTNAME</tt> (URI::PATTERN::HOSTNAME in default)
+ #
+ # == Examples
+ #
+ # p = URI::Parser.new(:ESCPAED => "(?:%[a-fA-F0-9]{2}|%u[a-fA-F0-9]{4})"
+ # u = p.parse("http://example.jp/%uABCD") #=> #<URI::HTTP:0xb78cf4f8 URL:http://example.jp/%uABCD>
+ # URI.parse(u.to_s) #=> raises URI::InvalidURIError
+ #
+ # s = "http://examle.com/ABCD"
+ # u1 = p.parse(s) #=> #<URI::HTTP:0xb78c3220 URL:http://example.com/ABCD>
+ # u2 = URI.parse(s) #=> #<URI::HTTP:0xb78b6d54 URL:http://example.com/ABCD>
+ # u1 == u2 #=> true
+ # u1.eql?(u2) #=> false
+ #
+ def initialize(opts = {})
+ @pattern = initialize_pattern(opts)
+ @pattern.each_value {|v| v.freeze}
+ @pattern.freeze
+
+ @regexp = initialize_regexp(@pattern)
+ @regexp.each_value {|v| v.freeze}
+ @regexp.freeze
+ end
+ attr_reader :pattern, :regexp
+
+ def split(uri)
+ case uri
+ when ''
+ # null uri
+
+ when @regexp[:ABS_URI]
+ scheme, opaque, userinfo, host, port,
+ registry, path, query, fragment = $~[1..-1]
+
+ # URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
+
+ # absoluteURI = scheme ":" ( hier_part | opaque_part )
+ # hier_part = ( net_path | abs_path ) [ "?" query ]
+ # opaque_part = uric_no_slash *uric
+
+ # abs_path = "/" path_segments
+ # net_path = "//" authority [ abs_path ]
+
+ # authority = server | reg_name
+ # server = [ [ userinfo "@" ] hostport ]
+
+ if !scheme
+ raise InvalidURIError,
+ "bad URI(absolute but no scheme): #{uri}"
+ end
+ if !opaque && (!path && (!host && !registry))
+ raise InvalidURIError,
+ "bad URI(absolute but no path): #{uri}"
+ end
+
+ when @regexp[:REL_URI]
+ scheme = nil
+ opaque = nil
+
+ userinfo, host, port, registry,
+ rel_segment, abs_path, query, fragment = $~[1..-1]
+ if rel_segment && abs_path
+ path = rel_segment + abs_path
+ elsif rel_segment
+ path = rel_segment
+ elsif abs_path
+ path = abs_path
+ end
+
+ # URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
+
+ # relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
+
+ # net_path = "//" authority [ abs_path ]
+ # abs_path = "/" path_segments
+ # rel_path = rel_segment [ abs_path ]
+
+ # authority = server | reg_name
+ # server = [ [ userinfo "@" ] hostport ]
+
+ else
+ raise InvalidURIError, "bad URI(is not URI?): #{uri}"
+ end
+
+ path = '' if !path && !opaque # (see RFC2396 Section 5.2)
+ ret = [
+ scheme,
+ userinfo, host, port, # X
+ registry, # X
+ path, # Y
+ opaque, # Y
+ query,
+ fragment
+ ]
+ return ret
+ end
+
+ def parse(uri)
+ scheme, userinfo, host, port,
+ registry, path, opaque, query, fragment = self.split(uri)
+
+ if scheme && URI.scheme_list.include?(scheme.upcase)
+ URI.scheme_list[scheme.upcase].new(scheme, userinfo, host, port,
+ registry, path, opaque, query,
+ fragment, self)
+ else
+ Generic.new(scheme, userinfo, host, port,
+ registry, path, opaque, query,
+ fragment, self)
+ end
+ end
+
+ def join(*str)
+ u = self.parse(str[0])
+ str[1 .. -1].each do |x|
+ u = u.merge(x)
+ end
+ u
+ end
+
+ def extract(str, schemes = nil, &block)
+ if block_given?
+ str.scan(make_regexp(schemes)) { yield $& }
+ nil
+ else
+ result = []
+ str.scan(make_regexp(schemes)) { result.push $& }
+ result
+ end
+ end
+
+ def make_regexp(schemes = nil)
+ unless schemes
+ @regexp[:ABS_URI_REF]
+ else
+ /(?=#{Regexp.union(*schemes)}:)#{@pattern[:X_ABS_URI]}/x
+ end
+ end
+
+ def escape(str, unsafe = @regexp[:UNSAFE])
+ unless unsafe.kind_of?(Regexp)
+ # perhaps unsafe is String object
+ unsafe = Regexp.new("[#{Regexp.quote(unsafe)}]", false)
+ end
+ str.gsub(unsafe) do
+ us = $&
+ tmp = ''
+ us.each_byte do |uc|
+ tmp << sprintf('%%%02X', uc)
+ end
+ tmp
+ end.force_encoding(Encoding::US_ASCII)
+ end
+
+ def unescape(str, escaped = @regexp[:ESCAPED])
+ str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(str.encoding)
+ end
+
+ @@to_s = Kernel.instance_method(:to_s)
+ def inspect
+ @@to_s.bind(self).call
+ end
+
+ private
+
+ def initialize_pattern(opts = {})
+ ret = {}
+ ret[:ESCAPED] = escaped = (opts.delete(:ESCAPED) || PATTERN::ESCAPED)
+ ret[:UNRESERVED] = unreserved = opts.delete(:UNRESERVED) || PATTERN::UNRESERVED
+ ret[:RESERVED] = reserved = opts.delete(:RESERVED) || PATTERN::RESERVED
+ ret[:DOMLABEL] = domlabel = opts.delete(:DOMLABEL) || PATTERN::DOMLABEL
+ ret[:TOPLABEL] = toplabel = opts.delete(:TOPLABEL) || PATTERN::TOPLABEL
+ ret[:HOSTNAME] = hostname = opts.delete(:HOSTNAME)
+
+ # RFC 2396 (URI Generic Syntax)
+ # RFC 2732 (IPv6 Literal Addresses in URL's)
+ # RFC 2373 (IPv6 Addressing Architecture)
+
+ # uric = reserved | unreserved | escaped
+ ret[:URIC] = uric = "(?:[#{unreserved}#{reserved}]|#{escaped})"
+ # uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
+ # "&" | "=" | "+" | "$" | ","
+ ret[:URIC_NO_SLASH] = uric_no_slash = "(?:[#{unreserved};?:@&=+$,]|#{escaped})"
+ # query = *uric
+ ret[:QUERY] = query = "#{uric}*"
+ # fragment = *uric
+ ret[:FRAGMENT] = fragment = "#{uric}*"
+
+ # hostname = *( domainlabel "." ) toplabel [ "." ]
+ unless hostname
+ ret[:HOSTNAME] = hostname = "(?:#{domlabel}\\.)*#{toplabel}\\.?"
+ end
+
+ # RFC 2373, APPENDIX B:
+ # IPv6address = hexpart [ ":" IPv4address ]
+ # IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT
+ # hexpart = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
+ # hexseq = hex4 *( ":" hex4)
+ # hex4 = 1*4HEXDIG
+ #
+ # XXX: This definition has a flaw. "::" + IPv4address must be
+ # allowed too. Here is a replacement.
+ #
+ # IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT
+ ret[:IPV4ADDR] = ipv4addr = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"
+ # hex4 = 1*4HEXDIG
+ hex4 = "[#{PATTERN::HEX}]{1,4}"
+ # lastpart = hex4 | IPv4address
+ lastpart = "(?:#{hex4}|#{ipv4addr})"
+ # hexseq1 = *( hex4 ":" ) hex4
+ hexseq1 = "(?:#{hex4}:)*#{hex4}"
+ # hexseq2 = *( hex4 ":" ) lastpart
+ hexseq2 = "(?:#{hex4}:)*#{lastpart}"
+ # IPv6address = hexseq2 | [ hexseq1 ] "::" [ hexseq2 ]
+ ret[:IPV6ADDR] = ipv6addr = "(?:#{hexseq2}|(?:#{hexseq1})?::(?:#{hexseq2})?)"
+
+ # IPv6prefix = ( hexseq1 | [ hexseq1 ] "::" [ hexseq1 ] ) "/" 1*2DIGIT
+ # unused
+
+ # ipv6reference = "[" IPv6address "]" (RFC 2732)
+ ret[:IPV6REF] = ipv6ref = "\\[#{ipv6addr}\\]"
+
+ # host = hostname | IPv4address
+ # host = hostname | IPv4address | IPv6reference (RFC 2732)
+ ret[:HOST] = host = "(?:#{hostname}|#{ipv4addr}|#{ipv6ref})"
+ # port = *digit
+ port = '\d*'
+ # hostport = host [ ":" port ]
+ ret[:HOSTPORT] = hostport = "#{host}(?::#{port})?"
+
+ # userinfo = *( unreserved | escaped |
+ # ";" | ":" | "&" | "=" | "+" | "$" | "," )
+ ret[:USERINFO] = userinfo = "(?:[#{unreserved};:&=+$,]|#{escaped})*"
+
+ # pchar = unreserved | escaped |
+ # ":" | "@" | "&" | "=" | "+" | "$" | ","
+ pchar = "(?:[#{unreserved}:@&=+$,]|#{escaped})"
+ # param = *pchar
+ param = "#{pchar}*"
+ # segment = *pchar *( ";" param )
+ segment = "#{pchar}*(?:;#{param})*"
+ # path_segments = segment *( "/" segment )
+ ret[:PATH_SEGMENTS] = path_segments = "#{segment}(?:/#{segment})*"
+
+ # server = [ [ userinfo "@" ] hostport ]
+ server = "(?:#{userinfo}@)?#{hostport}"
+ # reg_name = 1*( unreserved | escaped | "$" | "," |
+ # ";" | ":" | "@" | "&" | "=" | "+" )
+ ret[:REG_NAME] = reg_name = "(?:[#{unreserved}$,;:@&=+]|#{escaped})+"
+ # authority = server | reg_name
+ authority = "(?:#{server}|#{reg_name})"
+
+ # rel_segment = 1*( unreserved | escaped |
+ # ";" | "@" | "&" | "=" | "+" | "$" | "," )
+ ret[:REL_SEGMENT] = rel_segment = "(?:[#{unreserved};@&=+$,]|#{escaped})+"
+
+ # scheme = alpha *( alpha | digit | "+" | "-" | "." )
+ ret[:SCHEME] = scheme = "[#{PATTERN::ALPHA}][-+.#{PATTERN::ALPHA}\\d]*"
+
+ # abs_path = "/" path_segments
+ ret[:ABS_PATH] = abs_path = "/#{path_segments}"
+ # rel_path = rel_segment [ abs_path ]
+ ret[:REL_PATH] = rel_path = "#{rel_segment}(?:#{abs_path})?"
+ # net_path = "//" authority [ abs_path ]
+ ret[:NET_PATH] = net_path = "//#{authority}(?:#{abs_path})?"
+
+ # hier_part = ( net_path | abs_path ) [ "?" query ]
+ ret[:HIER_PART] = hier_part = "(?:#{net_path}|#{abs_path})(?:\\?(?:#{query}))?"
+ # opaque_part = uric_no_slash *uric
+ ret[:OPAQUE_PART] = opaque_part = "#{uric_no_slash}#{uric}*"
+
+ # absoluteURI = scheme ":" ( hier_part | opaque_part )
+ ret[:ABS_URI] = abs_uri = "#{scheme}:(?:#{hier_part}|#{opaque_part})"
+ # relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
+ ret[:REL_URI] = rel_uri = "(?:#{net_path}|#{abs_path}|#{rel_path})(?:\\?#{query})?"
+
+ # URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
+ ret[:URI_REF] = uri_ref = "(?:#{abs_uri}|#{rel_uri})?(?:##{fragment})?"
+
+ ret[:X_ABS_URI] = "
+ (#{scheme}): (?# 1: scheme)
+ (?:
+ (#{opaque_part}) (?# 2: opaque)
+ |
+ (?:(?:
+ //(?:
+ (?:(?:(#{userinfo})@)? (?# 3: userinfo)
+ (?:(#{host})(?::(\\d*))?))? (?# 4: host, 5: port)
+ |
+ (#{reg_name}) (?# 6: registry)
+ )
+ |
+ (?!//)) (?# XXX: '//' is the mark for hostport)
+ (#{abs_path})? (?# 7: path)
+ )(?:\\?(#{query}))? (?# 8: query)
+ )
+ (?:\\#(#{fragment}))? (?# 9: fragment)
+ "
+
+ ret[:X_REL_URI] = "
+ (?:
+ (?:
+ //
+ (?:
+ (?:(#{userinfo})@)? (?# 1: userinfo)
+ (#{host})?(?::(\\d*))? (?# 2: host, 3: port)
+ |
+ (#{reg_name}) (?# 4: registry)
+ )
+ )
+ |
+ (#{rel_segment}) (?# 5: rel_segment)
+ )?
+ (#{abs_path})? (?# 6: abs_path)
+ (?:\\?(#{query}))? (?# 7: query)
+ (?:\\#(#{fragment}))? (?# 8: fragment)
+ "
+
+ ret
+ end
+
+ def initialize_regexp(pattern)
+ ret = {}
+
+ # for URI::split
+ ret[:ABS_URI] = Regexp.new('^' + pattern[:X_ABS_URI] + '$', Regexp::EXTENDED)
+ ret[:REL_URI] = Regexp.new('^' + pattern[:X_REL_URI] + '$', Regexp::EXTENDED)
+
+ # for URI::extract
+ ret[:URI_REF] = Regexp.new(pattern[:URI_REF])
+ ret[:ABS_URI_REF] = Regexp.new(pattern[:X_ABS_URI], Regexp::EXTENDED)
+ ret[:REL_URI_REF] = Regexp.new(pattern[:X_REL_URI], Regexp::EXTENDED)
+
+ # for URI::escape/unescape
+ ret[:ESCAPED] = Regexp.new(pattern[:ESCAPED])
+ ret[:UNSAFE] = Regexp.new("[^#{pattern[:UNRESERVED]}#{pattern[:RESERVED]}]")
+
+ # for Generic#initialize
+ ret[:SCHEME] = Regexp.new("^#{pattern[:SCHEME]}$")
+ ret[:USERINFO] = Regexp.new("^#{pattern[:USERINFO]}$")
+ ret[:HOST] = Regexp.new("^#{pattern[:HOST]}$")
+ ret[:PORT] = Regexp.new("^#{pattern[:PORT]}$")
+ ret[:OPAQUE] = Regexp.new("^#{pattern[:OPAQUE_PART]}$")
+ ret[:REGISTRY] = Regexp.new("^#{pattern[:REG_NAME]}$")
+ ret[:ABS_PATH] = Regexp.new("^#{pattern[:ABS_PATH]}$")
+ ret[:REL_PATH] = Regexp.new("^#{pattern[:REL_PATH]}$")
+ ret[:QUERY] = Regexp.new("^#{pattern[:QUERY]}$")
+ ret[:FRAGMENT] = Regexp.new("^#{pattern[:FRAGMENT]}$")
+
+ ret
+ end
+ end # class Parser
+
+ DEFAULT_PARSER = Parser.new
+ DEFAULT_PARSER.pattern.each_pair do |sym, str|
+ unless REGEXP::PATTERN.const_defined?(sym)
+ REGEXP::PATTERN.const_set(sym, str)
+ end
+ end
+ DEFAULT_PARSER.regexp.each_pair do |sym, str|
+ const_set(sym, str)
+ end
+
+ module Util # :nodoc:
+ def make_components_hash(klass, array_hash)
+ tmp = {}
+ if array_hash.kind_of?(Array) &&
+ array_hash.size == klass.component.size - 1
+ klass.component[1..-1].each_index do |i|
+ begin
+ tmp[klass.component[i + 1]] = array_hash[i].clone
+ rescue TypeError
+ tmp[klass.component[i + 1]] = array_hash[i]
+ end
+ end
+
+ elsif array_hash.kind_of?(Hash)
+ array_hash.each do |key, value|
+ begin
+ tmp[key] = value.clone
+ rescue TypeError
+ tmp[key] = value
+ end
+ end
+ else
+ raise ArgumentError,
+ "expected Array of or Hash of components of #{klass.to_s} (#{klass.component[1..-1].join(', ')})"
+ end
+ tmp[:scheme] = klass.to_s.sub(/\A.*::/, '').downcase
+
+ return tmp
+ end
+ module_function :make_components_hash
+ end
+
+ module Escape
+ #
+ # == Synopsis
+ #
+ # URI.escape(str [, unsafe])
+ #
+ # == Args
+ #
+ # +str+::
+ # String to replaces in.
+ # +unsafe+::
+ # Regexp that matches all symbols that must be replaced with codes.
+ # By default uses <tt>REGEXP::UNSAFE</tt>.
+ # When this argument is a String, it represents a character set.
+ #
+ # == Description
+ #
+ # Escapes the string, replacing all unsafe characters with codes.
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # enc_uri = URI.escape("http://example.com/?a=\11\15")
+ # p enc_uri
+ # # => "http://example.com/?a=%09%0D"
+ #
+ # p URI.unescape(enc_uri)
+ # # => "http://example.com/?a=\t\r"
+ #
+ # p URI.escape("@?@!", "!?")
+ # # => "@%3F@%21"
+ #
+ def escape(*arg)
+ DEFAULT_PARSER.escape(*arg)
+ end
+ alias encode escape
+ #
+ # == Synopsis
+ #
+ # URI.unescape(str)
+ #
+ # == Args
+ #
+ # +str+::
+ # Unescapes the string.
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # enc_uri = URI.escape("http://example.com/?a=\11\15")
+ # p enc_uri
+ # # => "http://example.com/?a=%09%0D"
+ #
+ # p URI.unescape(enc_uri)
+ # # => "http://example.com/?a=\t\r"
+ #
+ def unescape(*arg)
+ DEFAULT_PARSER.unescape(*arg)
+ end
+ alias decode unescape
+ end
+
+ extend Escape
+ include REGEXP
+
+ @@schemes = {}
+ def self.scheme_list
+ @@schemes
+ end
+
+ #
+ # Base class for all URI exceptions.
+ #
+ class Error < StandardError; end
+ #
+ # Not a URI.
+ #
+ class InvalidURIError < Error; end
+ #
+ # Not a URI component.
+ #
+ class InvalidComponentError < Error; end
+ #
+ # URI is valid, bad usage is not.
+ #
+ class BadURIError < Error; end
+
+ #
+ # == Synopsis
+ #
+ # URI::split(uri)
+ #
+ # == Args
+ #
+ # +uri+::
+ # String with URI.
+ #
+ # == Description
+ #
+ # Splits the string on following parts and returns array with result:
+ #
+ # * Scheme
+ # * Userinfo
+ # * Host
+ # * Port
+ # * Registry
+ # * Path
+ # * Opaque
+ # * Query
+ # * Fragment
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # p URI.split("http://www.ruby-lang.org/")
+ # # => ["http", nil, "www.ruby-lang.org", nil, nil, "/", nil, nil, nil]
+ #
+ def self.split(uri)
+ DEFAULT_PARSER.split(uri)
+ end
+
+ #
+ # == Synopsis
+ #
+ # URI::parse(uri_str)
+ #
+ # == Args
+ #
+ # +uri_str+::
+ # String with URI.
+ #
+ # == Description
+ #
+ # Creates one of the URI's subclasses instance from the string.
+ #
+ # == Raises
+ #
+ # URI::InvalidURIError
+ # Raised if URI given is not a correct one.
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # uri = URI.parse("http://www.ruby-lang.org/")
+ # p uri
+ # # => #<URI::HTTP:0x202281be URL:http://www.ruby-lang.org/>
+ # p uri.scheme
+ # # => "http"
+ # p uri.host
+ # # => "www.ruby-lang.org"
+ #
+ def self.parse(uri)
+ DEFAULT_PARSER.parse(uri)
+ end
+
+ #
+ # == Synopsis
+ #
+ # URI::join(str[, str, ...])
+ #
+ # == Args
+ #
+ # +str+::
+ # String(s) to work with
+ #
+ # == Description
+ #
+ # Joins URIs.
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # p URI.join("http://localhost/","main.rbx")
+ # # => #<URI::HTTP:0x2022ac02 URL:http://localhost/main.rbx>
+ #
+ def self.join(*str)
+ DEFAULT_PARSER.join(*str)
+ end
+
+ #
+ # == Synopsis
+ #
+ # URI::extract(str[, schemes][,&blk])
+ #
+ # == Args
+ #
+ # +str+::
+ # String to extract URIs from.
+ # +schemes+::
+ # Limit URI matching to a specific schemes.
+ #
+ # == Description
+ #
+ # Extracts URIs from a string. If block given, iterates through all matched URIs.
+ # Returns nil if block given or array with matches.
+ #
+ # == Usage
+ #
+ # require "uri"
+ #
+ # URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
+ # # => ["http://foo.example.com/bla", "mailto:test@example.com"]
+ #
+ def self.extract(str, schemes = nil, &block)
+ DEFAULT_PARSER.extract(str, schemes, &block)
+ end
+
+ #
+ # == Synopsis
+ #
+ # URI::regexp([match_schemes])
+ #
+ # == Args
+ #
+ # +match_schemes+::
+ # Array of schemes. If given, resulting regexp matches to URIs
+ # whose scheme is one of the match_schemes.
+ #
+ # == Description
+ # Returns a Regexp object which matches to URI-like strings.
+ # The Regexp object returned by this method includes arbitrary
+ # number of capture group (parentheses). Never rely on it's number.
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # # extract first URI from html_string
+ # html_string.slice(URI.regexp)
+ #
+ # # remove ftp URIs
+ # html_string.sub(URI.regexp(['ftp'])
+ #
+ # # You should not rely on the number of parentheses
+ # html_string.scan(URI.regexp) do |*matches|
+ # p $&
+ # end
+ #
+ def self.regexp(schemes = nil)
+ DEFAULT_PARSER.make_regexp(schemes)
+ end
+
+end
+
+module Kernel
+ # alias for URI.parse.
+ #
+ # This method is introduced at 1.8.2.
+ def URI(uri_str) # :doc:
+ URI.parse(uri_str)
+ end
+ module_function :URI
+end
diff --git a/ruby/lib/uri/ftp.rb b/ruby/lib/uri/ftp.rb
new file mode 100644
index 0000000..6428634
--- /dev/null
+++ b/ruby/lib/uri/ftp.rb
@@ -0,0 +1,198 @@
+#
+# = uri/ftp.rb
+#
+# Author:: Akira Yamada <akira@ruby-lang.org>
+# License:: You can redistribute it and/or modify it under the same term as Ruby.
+# Revision:: $Id: ftp.rb 11708 2007-02-12 23:01:19Z shyouhei $
+#
+
+require 'uri/generic'
+
+module URI
+
+ #
+ # FTP URI syntax is defined by RFC1738 section 3.2.
+ #
+ class FTP < Generic
+ DEFAULT_PORT = 21
+
+ COMPONENT = [
+ :scheme,
+ :userinfo, :host, :port,
+ :path, :typecode
+ ].freeze
+ #
+ # Typecode is "a", "i" or "d".
+ #
+ # * "a" indicates a text file (the FTP command was ASCII)
+ # * "i" indicates a binary file (FTP command IMAGE)
+ # * "d" indicates the contents of a directory should be displayed
+ #
+ TYPECODE = ['a', 'i', 'd'].freeze
+ TYPECODE_PREFIX = ';type='.freeze
+
+ def self.new2(user, password, host, port, path,
+ typecode = nil, arg_check = true)
+ typecode = nil if typecode.size == 0
+ if typecode && !TYPECODE.include?(typecode)
+ raise ArgumentError,
+ "bad typecode is specified: #{typecode}"
+ end
+
+ # do escape
+
+ self.new('ftp',
+ [user, password],
+ host, port, nil,
+ typecode ? path + TYPECODE_PREFIX + typecode : path,
+ nil, nil, nil, arg_check)
+ end
+
+ #
+ # == Description
+ #
+ # Creates a new URI::FTP object from components, with syntax checking.
+ #
+ # The components accepted are +userinfo+, +host+, +port+, +path+ and
+ # +typecode+.
+ #
+ # The components should be provided either as an Array, or as a Hash
+ # with keys formed by preceding the component names with a colon.
+ #
+ # If an Array is used, the components must be passed in the order
+ # [userinfo, host, port, path, typecode]
+ #
+ # If the path supplied is absolute, it will be escaped in order to
+ # make it absolute in the URI. Examples:
+ #
+ # require 'uri'
+ #
+ # uri = URI::FTP.build(['user:password', 'ftp.example.com', nil,
+ # '/path/file.> zip', 'i'])
+ # puts uri.to_s -> ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=a
+ #
+ # uri2 = URI::FTP.build({:host => 'ftp.example.com',
+ # :path => 'ruby/src'})
+ # puts uri2.to_s -> ftp://ftp.example.com/ruby/src
+ #
+ def self.build(args)
+
+ # Fix the incoming path to be generic URL syntax
+ # FTP path -> URL path
+ # foo/bar /foo/bar
+ # /foo/bar /%2Ffoo/bar
+ #
+ if args.kind_of?(Array)
+ args[3] = '/' + args[3].sub(/^\//, '%2F')
+ else
+ args[:path] = '/' + args[:path].sub(/^\//, '%2F')
+ end
+
+ tmp = Util::make_components_hash(self, args)
+
+ if tmp[:typecode]
+ if tmp[:typecode].size == 1
+ tmp[:typecode] = TYPECODE_PREFIX + tmp[:typecode]
+ end
+ tmp[:path] << tmp[:typecode]
+ end
+
+ return super(tmp)
+ end
+
+ #
+ # == Description
+ #
+ # Creates a new URI::FTP object from generic URL components with no
+ # syntax checking.
+ #
+ # Unlike build(), this method does not escape the path component as
+ # required by RFC1738; instead it is treated as per RFC2396.
+ #
+ # Arguments are +scheme+, +userinfo+, +host+, +port+, +registry+, +path+,
+ # +opaque+, +query+ and +fragment+, in that order.
+ #
+ def initialize(*arg)
+ super(*arg)
+ @typecode = nil
+ tmp = @path.index(TYPECODE_PREFIX)
+ if tmp
+ typecode = @path[tmp + TYPECODE_PREFIX.size..-1]
+ self.set_path(@path[0..tmp - 1])
+
+ if arg[-1]
+ self.typecode = typecode
+ else
+ self.set_typecode(typecode)
+ end
+ end
+ end
+ attr_reader :typecode
+
+ def check_typecode(v)
+ if TYPECODE.include?(v)
+ return true
+ else
+ raise InvalidComponentError,
+ "bad typecode(expected #{TYPECODE.join(', ')}): #{v}"
+ end
+ end
+ private :check_typecode
+
+ def set_typecode(v)
+ @typecode = v
+ end
+ protected :set_typecode
+
+ def typecode=(typecode)
+ check_typecode(typecode)
+ set_typecode(typecode)
+ typecode
+ end
+
+ def merge(oth) # :nodoc:
+ tmp = super(oth)
+ if self != tmp
+ tmp.set_typecode(oth.typecode)
+ end
+
+ return tmp
+ end
+
+ # Returns the path from an FTP URI.
+ #
+ # RFC 1738 specifically states that the path for an FTP URI does not
+ # include the / which separates the URI path from the URI host. Example:
+ #
+ # ftp://ftp.example.com/pub/ruby
+ #
+ # The above URI indicates that the client should connect to
+ # ftp.example.com then cd pub/ruby from the initial login directory.
+ #
+ # If you want to cd to an absolute directory, you must include an
+ # escaped / (%2F) in the path. Example:
+ #
+ # ftp://ftp.example.com/%2Fpub/ruby
+ #
+ # This method will then return "/pub/ruby"
+ #
+ def path
+ return @path.sub(/^\//,'').sub(/^%2F/,'/')
+ end
+
+ def to_s
+ save_path = nil
+ if @typecode
+ save_path = @path
+ @path = @path + TYPECODE_PREFIX + @typecode
+ end
+ str = super
+ if @typecode
+ @path = save_path
+ end
+
+ return str
+ end
+ end
+ @@schemes['FTP'] = FTP
+end
diff --git a/ruby/lib/uri/generic.rb b/ruby/lib/uri/generic.rb
new file mode 100644
index 0000000..004a23c
--- /dev/null
+++ b/ruby/lib/uri/generic.rb
@@ -0,0 +1,1128 @@
+#
+# = uri/generic.rb
+#
+# Author:: Akira Yamada <akira@ruby-lang.org>
+# License:: You can redistribute it and/or modify it under the same term as Ruby.
+# Revision:: $Id: generic.rb 20258 2008-11-18 16:46:16Z yugui $
+#
+
+require 'uri/common'
+
+module URI
+
+ #
+ # Base class for all URI classes.
+ # Implements generic URI syntax as per RFC 2396.
+ #
+ class Generic
+ include URI
+
+ DEFAULT_PORT = nil
+
+ #
+ # Returns default port
+ #
+ def self.default_port
+ self::DEFAULT_PORT
+ end
+
+ def default_port
+ self.class.default_port
+ end
+
+ COMPONENT = [
+ :scheme,
+ :userinfo, :host, :port, :registry,
+ :path, :opaque,
+ :query,
+ :fragment
+ ].freeze
+
+ #
+ # Components of the URI in the order.
+ #
+ def self.component
+ self::COMPONENT
+ end
+
+ USE_REGISTRY = false
+
+ #
+ # DOC: FIXME!
+ #
+ def self.use_registry
+ self::USE_REGISTRY
+ end
+
+ #
+ # == Synopsis
+ #
+ # See #new
+ #
+ # == Description
+ #
+ # At first, tries to create a new URI::Generic instance using
+ # URI::Generic::build. But, if exception URI::InvalidComponentError is raised,
+ # then it URI::Escape.escape all URI components and tries again.
+ #
+ #
+ def self.build2(args)
+ begin
+ return self.build(args)
+ rescue InvalidComponentError
+ if args.kind_of?(Array)
+ return self.build(args.collect{|x|
+ if x
+ @parser.escape(x)
+ else
+ x
+ end
+ })
+ elsif args.kind_of?(Hash)
+ tmp = {}
+ args.each do |key, value|
+ tmp[key] = if value
+ @parser.escape(value)
+ else
+ value
+ end
+ end
+ return self.build(tmp)
+ end
+ end
+ end
+
+ #
+ # == Synopsis
+ #
+ # See #new
+ #
+ # == Description
+ #
+ # Creates a new URI::Generic instance from components of URI::Generic
+ # with check. Components are: scheme, userinfo, host, port, registry, path,
+ # opaque, query and fragment. You can provide arguments either by an Array or a Hash.
+ # See #new for hash keys to use or for order of array items.
+ #
+ def self.build(args)
+ if args.kind_of?(Array) &&
+ args.size == ::URI::Generic::COMPONENT.size
+ tmp = args
+ elsif args.kind_of?(Hash)
+ tmp = ::URI::Generic::COMPONENT.collect do |c|
+ if args.include?(c)
+ args[c]
+ else
+ nil
+ end
+ end
+ else
+ raise ArgumentError,
+ "expected Array of or Hash of components of #{self.class} (#{self.class.component.join(', ')})"
+ end
+
+ tmp << DEFAULT_PARSER
+ tmp << true
+ return self.new(*tmp)
+ end
+ #
+ # == Args
+ #
+ # +scheme+::
+ # Protocol scheme, i.e. 'http','ftp','mailto' and so on.
+ # +userinfo+::
+ # User name and password, i.e. 'sdmitry:bla'
+ # +host+::
+ # Server host name
+ # +port+::
+ # Server port
+ # +registry+::
+ # DOC: FIXME!
+ # +path+::
+ # Path on server
+ # +opaque+::
+ # DOC: FIXME!
+ # +query+::
+ # Query data
+ # +fragment+::
+ # A part of URI after '#' sign
+ # +parser+::
+ # Parser for internal use [URI::DEFAULT_PARSER by default]
+ # +arg_check+::
+ # Check arguments [false by default]
+ #
+ # == Description
+ #
+ # Creates a new URI::Generic instance from ``generic'' components without check.
+ #
+ def initialize(scheme,
+ userinfo, host, port, registry,
+ path, opaque,
+ query,
+ fragment,
+ parser = DEFAULT_PARSER,
+ arg_check = false)
+ @scheme = nil
+ @user = nil
+ @password = nil
+ @host = nil
+ @port = nil
+ @path = nil
+ @query = nil
+ @opaque = nil
+ @registry = nil
+ @fragment = nil
+ @parser = parser
+
+ if arg_check
+ self.scheme = scheme
+ self.userinfo = userinfo
+ self.host = host
+ self.port = port
+ self.path = path
+ self.query = query
+ self.opaque = opaque
+ self.registry = registry
+ self.fragment = fragment
+ else
+ self.set_scheme(scheme)
+ self.set_userinfo(userinfo)
+ self.set_host(host)
+ self.set_port(port)
+ self.set_path(path)
+ self.set_query(query)
+ self.set_opaque(opaque)
+ self.set_registry(registry)
+ self.set_fragment(fragment)
+ end
+ if @registry && !self.class.use_registry
+ raise InvalidURIError,
+ "the scheme #{@scheme} does not accept registry part: #{@registry} (or bad hostname?)"
+ end
+
+ @scheme.freeze if @scheme
+ self.set_path('') if !@path && !@opaque # (see RFC2396 Section 5.2)
+ self.set_port(self.default_port) if self.default_port && !@port
+ end
+ attr_reader :scheme
+ attr_reader :host
+ attr_reader :port
+ attr_reader :registry
+ attr_reader :path
+ attr_reader :query
+ attr_reader :opaque
+ attr_reader :fragment
+ attr_reader :parser
+
+ # replace self by other URI object
+ def replace!(oth)
+ if self.class != oth.class
+ raise ArgumentError, "expected #{self.class} object"
+ end
+
+ component.each do |c|
+ self.__send__("#{c}=", oth.__send__(c))
+ end
+ end
+ private :replace!
+
+ def component
+ self.class.component
+ end
+
+ def check_scheme(v)
+ if v && @parser.regexp[:SCHEME] !~ v
+ raise InvalidComponentError,
+ "bad component(expected scheme component): #{v}"
+ end
+
+ return true
+ end
+ private :check_scheme
+
+ def set_scheme(v)
+ @scheme = v
+ end
+ protected :set_scheme
+
+ def scheme=(v)
+ check_scheme(v)
+ set_scheme(v)
+ v
+ end
+
+ def check_userinfo(user, password = nil)
+ if !password
+ user, password = split_userinfo(user)
+ end
+ check_user(user)
+ check_password(password, user)
+
+ return true
+ end
+ private :check_userinfo
+
+ def check_user(v)
+ if @registry || @opaque
+ raise InvalidURIError,
+ "can not set user with registry or opaque"
+ end
+
+ return v unless v
+
+ if @parser.regexp[:USERINFO] !~ v
+ raise InvalidComponentError,
+ "bad component(expected userinfo component or user component): #{v}"
+ end
+
+ return true
+ end
+ private :check_user
+
+ def check_password(v, user = @user)
+ if @registry || @opaque
+ raise InvalidURIError,
+ "can not set password with registry or opaque"
+ end
+ return v unless v
+
+ if !user
+ raise InvalidURIError,
+ "password component depends user component"
+ end
+
+ if @parser.regexp[:USERINFO] !~ v
+ raise InvalidComponentError,
+ "bad component(expected user component): #{v}"
+ end
+
+ return true
+ end
+ private :check_password
+
+ #
+ # Sets userinfo, argument is string like 'name:pass'
+ #
+ def userinfo=(userinfo)
+ if userinfo.nil?
+ return nil
+ end
+ check_userinfo(*userinfo)
+ set_userinfo(*userinfo)
+ # returns userinfo
+ end
+
+ def user=(user)
+ check_user(user)
+ set_user(user)
+ # returns user
+ end
+
+ def password=(password)
+ check_password(password)
+ set_password(password)
+ # returns password
+ end
+
+ def set_userinfo(user, password = nil)
+ unless password
+ user, password = split_userinfo(user)
+ end
+ @user = user
+ @password = password if password
+
+ [@user, @password]
+ end
+ protected :set_userinfo
+
+ def set_user(v)
+ set_userinfo(v, @password)
+ v
+ end
+ protected :set_user
+
+ def set_password(v)
+ @password = v
+ # returns v
+ end
+ protected :set_password
+
+ def split_userinfo(ui)
+ return nil, nil unless ui
+ user, password = ui.split(/:/, 2)
+
+ return user, password
+ end
+ private :split_userinfo
+
+ def escape_userpass(v)
+ v = @parser.escape(v, /[@:\/]/o) # RFC 1738 section 3.1 #/
+ end
+ private :escape_userpass
+
+ def userinfo
+ if @user.nil?
+ nil
+ elsif @password.nil?
+ @user
+ else
+ @user + ':' + @password
+ end
+ end
+
+ def user
+ @user
+ end
+
+ def password
+ @password
+ end
+
+ def check_host(v)
+ return v unless v
+
+ if @registry || @opaque
+ raise InvalidURIError,
+ "can not set host with registry or opaque"
+ elsif @parser.regexp[:HOST] !~ v
+ raise InvalidComponentError,
+ "bad component(expected host component): #{v}"
+ end
+
+ return true
+ end
+ private :check_host
+
+ def set_host(v)
+ @host = v
+ end
+ protected :set_host
+
+ def host=(v)
+ check_host(v)
+ set_host(v)
+ v
+ end
+
+ def check_port(v)
+ return v unless v
+
+ if @registry || @opaque
+ raise InvalidURIError,
+ "can not set port with registry or opaque"
+ elsif !v.kind_of?(Fixnum) && @parser.regexp[:PORT] !~ v
+ raise InvalidComponentError,
+ "bad component(expected port component): #{v}"
+ end
+
+ return true
+ end
+ private :check_port
+
+ def set_port(v)
+ unless !v || v.kind_of?(Fixnum)
+ if v.empty?
+ v = nil
+ else
+ v = v.to_i
+ end
+ end
+ @port = v
+ end
+ protected :set_port
+
+ def port=(v)
+ check_port(v)
+ set_port(v)
+ port
+ end
+
+ def check_registry(v)
+ return v unless v
+
+ # raise if both server and registry are not nil, because:
+ # authority = server | reg_name
+ # server = [ [ userinfo "@" ] hostport ]
+ if @host || @port || @user # userinfo = @user + ':' + @password
+ raise InvalidURIError,
+ "can not set registry with host, port, or userinfo"
+ elsif v && @parser.regexp[:REGISTRY] !~ v
+ raise InvalidComponentError,
+ "bad component(expected registry component): #{v}"
+ end
+
+ return true
+ end
+ private :check_registry
+
+ def set_registry(v)
+ @registry = v
+ end
+ protected :set_registry
+
+ def registry=(v)
+ check_registry(v)
+ set_registry(v)
+ v
+ end
+
+ def check_path(v)
+ # raise if both hier and opaque are not nil, because:
+ # absoluteURI = scheme ":" ( hier_part | opaque_part )
+ # hier_part = ( net_path | abs_path ) [ "?" query ]
+ if v && @opaque
+ raise InvalidURIError,
+ "path conflicts with opaque"
+ end
+
+ if @scheme
+ if v && v != '' && @parser.regexp[:ABS_PATH] !~ v
+ raise InvalidComponentError,
+ "bad component(expected absolute path component): #{v}"
+ end
+ else
+ if v && v != '' && @parser.regexp[:ABS_PATH] !~ v && @parser.regexp[:REL_PATH] !~ v
+ raise InvalidComponentError,
+ "bad component(expected relative path component): #{v}"
+ end
+ end
+
+ return true
+ end
+ private :check_path
+
+ def set_path(v)
+ @path = v
+ end
+ protected :set_path
+
+ def path=(v)
+ check_path(v)
+ set_path(v)
+ v
+ end
+
+ def check_query(v)
+ return v unless v
+
+ # raise if both hier and opaque are not nil, because:
+ # absoluteURI = scheme ":" ( hier_part | opaque_part )
+ # hier_part = ( net_path | abs_path ) [ "?" query ]
+ if @opaque
+ raise InvalidURIError,
+ "query conflicts with opaque"
+ end
+
+ if v && v != '' && @parser.regexp[:QUERY] !~ v
+ raise InvalidComponentError,
+ "bad component(expected query component): #{v}"
+ end
+
+ return true
+ end
+ private :check_query
+
+ def set_query(v)
+ @query = v
+ end
+ protected :set_query
+
+ def query=(v)
+ check_query(v)
+ set_query(v)
+ v
+ end
+
+ def check_opaque(v)
+ return v unless v
+
+ # raise if both hier and opaque are not nil, because:
+ # absoluteURI = scheme ":" ( hier_part | opaque_part )
+ # hier_part = ( net_path | abs_path ) [ "?" query ]
+ if @host || @port || @user || @path # userinfo = @user + ':' + @password
+ raise InvalidURIError,
+ "can not set opaque with host, port, userinfo or path"
+ elsif v && @parser.regexp[:OPAQUE] !~ v
+ raise InvalidComponentError,
+ "bad component(expected opaque component): #{v}"
+ end
+
+ return true
+ end
+ private :check_opaque
+
+ def set_opaque(v)
+ @opaque = v
+ end
+ protected :set_opaque
+
+ def opaque=(v)
+ check_opaque(v)
+ set_opaque(v)
+ v
+ end
+
+ def check_fragment(v)
+ return v unless v
+
+ if v && v != '' && @parser.regexp[:FRAGMENT] !~ v
+ raise InvalidComponentError,
+ "bad component(expected fragment component): #{v}"
+ end
+
+ return true
+ end
+ private :check_fragment
+
+ def set_fragment(v)
+ @fragment = v
+ end
+ protected :set_fragment
+
+ def fragment=(v)
+ check_fragment(v)
+ set_fragment(v)
+ v
+ end
+
+ #
+ # Checks if URI has a path
+ #
+ def hierarchical?
+ if @path
+ true
+ else
+ false
+ end
+ end
+
+ #
+ # Checks if URI is an absolute one
+ #
+ def absolute?
+ if @scheme
+ true
+ else
+ false
+ end
+ end
+ alias absolute absolute?
+
+ #
+ # Checks if URI is relative
+ #
+ def relative?
+ !absolute?
+ end
+
+ def split_path(path)
+ path.split(%r{/+}, -1)
+ end
+ private :split_path
+
+ def merge_path(base, rel)
+
+ # RFC2396, Section 5.2, 5)
+ # RFC2396, Section 5.2, 6)
+ base_path = split_path(base)
+ rel_path = split_path(rel)
+
+ # RFC2396, Section 5.2, 6), a)
+ base_path << '' if base_path.last == '..'
+ while i = base_path.index('..')
+ base_path.slice!(i - 1, 2)
+ end
+
+ if (first = rel_path.first) and first.empty?
+ base_path.clear
+ rel_path.shift
+ end
+
+ # RFC2396, Section 5.2, 6), c)
+ # RFC2396, Section 5.2, 6), d)
+ rel_path.push('') if rel_path.last == '.' || rel_path.last == '..'
+ rel_path.delete('.')
+
+ # RFC2396, Section 5.2, 6), e)
+ tmp = []
+ rel_path.each do |x|
+ if x == '..' &&
+ !(tmp.empty? || tmp.last == '..')
+ tmp.pop
+ else
+ tmp << x
+ end
+ end
+
+ add_trailer_slash = !tmp.empty?
+ if base_path.empty?
+ base_path = [''] # keep '/' for root directory
+ elsif add_trailer_slash
+ base_path.pop
+ end
+ while x = tmp.shift
+ if x == '..'
+ # RFC2396, Section 4
+ # a .. or . in an absolute path has no special meaning
+ base_path.pop if base_path.size > 1
+ else
+ # if x == '..'
+ # valid absolute (but abnormal) path "/../..."
+ # else
+ # valid absolute path
+ # end
+ base_path << x
+ tmp.each {|t| base_path << t}
+ add_trailer_slash = false
+ break
+ end
+ end
+ base_path.push('') if add_trailer_slash
+
+ return base_path.join('/')
+ end
+ private :merge_path
+
+ #
+ # == Args
+ #
+ # +oth+::
+ # URI or String
+ #
+ # == Description
+ #
+ # Destructive form of #merge
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # uri = URI.parse("http://my.example.com")
+ # uri.merge!("/main.rbx?page=1")
+ # p uri
+ # # => #<URI::HTTP:0x2021f3b0 URL:http://my.example.com/main.rbx?page=1>
+ #
+ def merge!(oth)
+ t = merge(oth)
+ if self == t
+ nil
+ else
+ replace!(t)
+ self
+ end
+ end
+
+ #
+ # == Args
+ #
+ # +oth+::
+ # URI or String
+ #
+ # == Description
+ #
+ # Merges two URI's.
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # uri = URI.parse("http://my.example.com")
+ # p uri.merge("/main.rbx?page=1")
+ # # => #<URI::HTTP:0x2021f3b0 URL:http://my.example.com/main.rbx?page=1>
+ #
+ def merge(oth)
+ begin
+ base, rel = merge0(oth)
+ rescue
+ raise $!.class, $!.message
+ end
+
+ if base == rel
+ return base
+ end
+
+ authority = rel.userinfo || rel.host || rel.port
+
+ # RFC2396, Section 5.2, 2)
+ if (rel.path.nil? || rel.path.empty?) && !authority && !rel.query
+ base.set_fragment(rel.fragment) if rel.fragment
+ return base
+ end
+
+ base.set_query(nil)
+ base.set_fragment(nil)
+
+ # RFC2396, Section 5.2, 4)
+ if !authority
+ base.set_path(merge_path(base.path, rel.path)) if base.path && rel.path
+ else
+ # RFC2396, Section 5.2, 4)
+ base.set_path(rel.path) if rel.path
+ end
+
+ # RFC2396, Section 5.2, 7)
+ base.set_userinfo(rel.userinfo) if rel.userinfo
+ base.set_host(rel.host) if rel.host
+ base.set_port(rel.port) if rel.port
+ base.set_query(rel.query) if rel.query
+ base.set_fragment(rel.fragment) if rel.fragment
+
+ return base
+ end # merge
+ alias + merge
+
+ # return base and rel.
+ # you can modify `base', but can not `rel'.
+ def merge0(oth)
+ case oth
+ when Generic
+ when String
+ oth = @parser.parse(oth)
+ else
+ raise ArgumentError,
+ "bad argument(expected URI object or URI string)"
+ end
+
+ if self.relative? && oth.relative?
+ raise BadURIError,
+ "both URI are relative"
+ end
+
+ if self.absolute? && oth.absolute?
+ #raise BadURIError,
+ # "both URI are absolute"
+ # hmm... should return oth for usability?
+ return oth, oth
+ end
+
+ if self.absolute?
+ return self.dup, oth
+ else
+ return oth, oth
+ end
+ end
+ private :merge0
+
+ def route_from_path(src, dst)
+ # RFC2396, Section 4.2
+ return '' if src == dst
+
+ src_path = split_path(src)
+ dst_path = split_path(dst)
+
+ # hmm... dst has abnormal absolute path,
+ # like "/./", "/../", "/x/../", ...
+ if dst_path.include?('..') ||
+ dst_path.include?('.')
+ return dst.dup
+ end
+
+ src_path.pop
+
+ # discard same parts
+ while dst_path.first == src_path.first
+ break if dst_path.empty?
+
+ src_path.shift
+ dst_path.shift
+ end
+
+ tmp = dst_path.join('/')
+
+ # calculate
+ if src_path.empty?
+ if tmp.empty?
+ return './'
+ elsif dst_path.first.include?(':') # (see RFC2396 Section 5)
+ return './' + tmp
+ else
+ return tmp
+ end
+ end
+
+ return '../' * src_path.size + tmp
+ end
+ private :route_from_path
+
+ def route_from0(oth)
+ case oth
+ when Generic
+ when String
+ oth = @parser.parse(oth)
+ else
+ raise ArgumentError,
+ "bad argument(expected URI object or URI string)"
+ end
+
+ if self.relative?
+ raise BadURIError,
+ "relative URI: #{self}"
+ end
+ if oth.relative?
+ raise BadURIError,
+ "relative URI: #{oth}"
+ end
+
+ if self.scheme != oth.scheme
+ return self, self.dup
+ end
+ rel = URI::Generic.new(nil, # it is relative URI
+ self.userinfo, self.host, self.port,
+ self.registry, self.path, self.opaque,
+ self.query, self.fragment, @parser)
+
+ if rel.userinfo != oth.userinfo ||
+ rel.host.to_s.downcase != oth.host.to_s.downcase ||
+ rel.port != oth.port
+ if self.userinfo.nil? && self.host.nil?
+ return self, self.dup
+ end
+ rel.set_port(nil) if rel.port == oth.default_port
+ return rel, rel
+ end
+ rel.set_userinfo(nil)
+ rel.set_host(nil)
+ rel.set_port(nil)
+
+ if rel.path && rel.path == oth.path
+ rel.set_path('')
+ rel.set_query(nil) if rel.query == oth.query
+ return rel, rel
+ elsif rel.opaque && rel.opaque == oth.opaque
+ rel.set_opaque('')
+ rel.set_query(nil) if rel.query == oth.query
+ return rel, rel
+ end
+
+ # you can modify `rel', but can not `oth'.
+ return oth, rel
+ end
+ private :route_from0
+ #
+ # == Args
+ #
+ # +oth+::
+ # URI or String
+ #
+ # == Description
+ #
+ # Calculates relative path from oth to self
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # uri = URI.parse('http://my.example.com/main.rbx?page=1')
+ # p uri.route_from('http://my.example.com')
+ # #=> #<URI::Generic:0x20218858 URL:/main.rbx?page=1>
+ #
+ def route_from(oth)
+ # you can modify `rel', but can not `oth'.
+ begin
+ oth, rel = route_from0(oth)
+ rescue
+ raise $!.class, $!.message
+ end
+ if oth == rel
+ return rel
+ end
+
+ rel.set_path(route_from_path(oth.path, self.path))
+ if rel.path == './' && self.query
+ # "./?foo" -> "?foo"
+ rel.set_path('')
+ end
+
+ return rel
+ end
+
+ alias - route_from
+
+ #
+ # == Args
+ #
+ # +oth+::
+ # URI or String
+ #
+ # == Description
+ #
+ # Calculates relative path to oth from self
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # uri = URI.parse('http://my.example.com')
+ # p uri.route_to('http://my.example.com/main.rbx?page=1')
+ # #=> #<URI::Generic:0x2020c2f6 URL:/main.rbx?page=1>
+ #
+ def route_to(oth)
+ case oth
+ when Generic
+ when String
+ oth = @parser.parse(oth)
+ else
+ raise ArgumentError,
+ "bad argument(expected URI object or URI string)"
+ end
+
+ oth.route_from(self)
+ end
+
+ #
+ # Returns normalized URI
+ #
+ def normalize
+ uri = dup
+ uri.normalize!
+ uri
+ end
+
+ #
+ # Destructive version of #normalize
+ #
+ def normalize!
+ if path && path == ''
+ set_path('/')
+ end
+ if host && host != host.downcase
+ set_host(self.host.downcase)
+ end
+ end
+
+ def path_query
+ str = @path
+ if @query
+ str += '?' + @query
+ end
+ str
+ end
+ private :path_query
+
+ #
+ # Constructs String from URI
+ #
+ def to_s
+ str = ''
+ if @scheme
+ str << @scheme
+ str << ':'
+ end
+
+ if @opaque
+ str << @opaque
+
+ else
+ if @registry
+ str << @registry
+ else
+ if @host
+ str << '//'
+ end
+ if self.userinfo
+ str << self.userinfo
+ str << '@'
+ end
+ if @host
+ str << @host
+ end
+ if @port && @port != self.default_port
+ str << ':'
+ str << @port.to_s
+ end
+ end
+
+ str << path_query
+ end
+
+ if @fragment
+ str << '#'
+ str << @fragment
+ end
+
+ str
+ end
+
+ #
+ # Compares to URI's
+ #
+ def ==(oth)
+ if self.class == oth.class
+ self.normalize.component_ary == oth.normalize.component_ary
+ else
+ false
+ end
+ end
+
+ def hash
+ self.component_ary.hash
+ end
+
+ def eql?(oth)
+ @parser == oth.parser &&
+ self.component_ary.eql?(oth.component_ary)
+ end
+
+=begin
+
+--- URI::Generic#===(oth)
+
+=end
+# def ===(oth)
+# raise NotImplementedError
+# end
+
+=begin
+=end
+ def component_ary
+ component.collect do |x|
+ self.send(x)
+ end
+ end
+ protected :component_ary
+
+ # == Args
+ #
+ # +components+::
+ # Multiple Symbol arguments defined in URI::HTTP
+ #
+ # == Description
+ #
+ # Selects specified components from URI
+ #
+ # == Usage
+ #
+ # require 'uri'
+ #
+ # uri = URI.parse('http://myuser:mypass@my.example.com/test.rbx')
+ # p uri.select(:userinfo, :host, :path)
+ # # => ["myuser:mypass", "my.example.com", "/test.rbx"]
+ #
+ def select(*components)
+ components.collect do |c|
+ if component.include?(c)
+ self.send(c)
+ else
+ raise ArgumentError,
+ "expected of components of #{self.class} (#{self.class.component.join(', ')})"
+ end
+ end
+ end
+
+ @@to_s = Kernel.instance_method(:to_s)
+ def inspect
+ @@to_s.bind(self).call.sub!(/>\z/) {" URL:#{self}>"}
+ end
+
+ def coerce(oth)
+ case oth
+ when String
+ oth = @parser.parse(oth)
+ else
+ super
+ end
+
+ return oth, self
+ end
+ end
+end
diff --git a/ruby/lib/uri/http.rb b/ruby/lib/uri/http.rb
new file mode 100644
index 0000000..e1aa06d
--- /dev/null
+++ b/ruby/lib/uri/http.rb
@@ -0,0 +1,100 @@
+#
+# = uri/http.rb
+#
+# Author:: Akira Yamada <akira@ruby-lang.org>
+# License:: You can redistribute it and/or modify it under the same term as Ruby.
+# Revision:: $Id: http.rb 11708 2007-02-12 23:01:19Z shyouhei $
+#
+
+require 'uri/generic'
+
+module URI
+
+ #
+ # The syntax of HTTP URIs is defined in RFC1738 section 3.3.
+ #
+ # Note that the Ruby URI library allows HTTP URLs containing usernames and
+ # passwords. This is not legal as per the RFC, but used to be
+ # supported in Internet Explorer 5 and 6, before the MS04-004 security
+ # update. See <URL:http://support.microsoft.com/kb/834489>.
+ #
+ class HTTP < Generic
+ DEFAULT_PORT = 80
+
+ COMPONENT = [
+ :scheme,
+ :userinfo, :host, :port,
+ :path,
+ :query,
+ :fragment
+ ].freeze
+
+ #
+ # == Description
+ #
+ # Create a new URI::HTTP object from components, with syntax checking.
+ #
+ # The components accepted are userinfo, host, port, path, query and
+ # fragment.
+ #
+ # The components should be provided either as an Array, or as a Hash
+ # with keys formed by preceding the component names with a colon.
+ #
+ # If an Array is used, the components must be passed in the order
+ # [userinfo, host, port, path, query, fragment].
+ #
+ # Example:
+ #
+ # newuri = URI::HTTP.build({:host => 'www.example.com',
+ # :path> => '/foo/bar'})
+ #
+ # newuri = URI::HTTP.build([nil, "www.example.com", nil, "/path",
+ # "query", 'fragment'])
+ #
+ # Currently, if passed userinfo components this method generates
+ # invalid HTTP URIs as per RFC 1738.
+ #
+ def self.build(args)
+ tmp = Util::make_components_hash(self, args)
+ return super(tmp)
+ end
+
+ #
+ # == Description
+ #
+ # Create a new URI::HTTP object from generic URI components as per
+ # RFC 2396. No HTTP-specific syntax checking (as per RFC 1738) is
+ # performed.
+ #
+ # Arguments are +scheme+, +userinfo+, +host+, +port+, +registry+, +path+,
+ # +opaque+, +query+ and +fragment+, in that order.
+ #
+ # Example:
+ #
+ # uri = URI::HTTP.new(['http', nil, "www.example.com", nil, "/path",
+ # "query", 'fragment'])
+ #
+ def initialize(*arg)
+ super(*arg)
+ end
+
+ #
+ # == Description
+ #
+ # Returns the full path for an HTTP request, as required by Net::HTTP::Get.
+ #
+ # If the URI contains a query, the full path is URI#path + '?' + URI#query.
+ # Otherwise, the path is simply URI#path.
+ #
+ def request_uri
+ r = path_query
+ if r[0] != ?/
+ r = '/' + r
+ end
+
+ r
+ end
+ end
+
+ @@schemes['HTTP'] = HTTP
+end
diff --git a/ruby/lib/uri/https.rb b/ruby/lib/uri/https.rb
new file mode 100644
index 0000000..c8e9e35
--- /dev/null
+++ b/ruby/lib/uri/https.rb
@@ -0,0 +1,20 @@
+#
+# = uri/https.rb
+#
+# Author:: Akira Yamada <akira@ruby-lang.org>
+# License:: You can redistribute it and/or modify it under the same term as Ruby.
+# Revision:: $Id: https.rb 11708 2007-02-12 23:01:19Z shyouhei $
+#
+
+require 'uri/http'
+
+module URI
+
+ # The default port for HTTPS URIs is 443, and the scheme is 'https:' rather
+ # than 'http:'. Other than that, HTTPS URIs are identical to HTTP URIs;
+ # see URI::HTTP.
+ class HTTPS < HTTP
+ DEFAULT_PORT = 443
+ end
+ @@schemes['HTTPS'] = HTTPS
+end
diff --git a/ruby/lib/uri/ldap.rb b/ruby/lib/uri/ldap.rb
new file mode 100644
index 0000000..7c14ff8
--- /dev/null
+++ b/ruby/lib/uri/ldap.rb
@@ -0,0 +1,190 @@
+#
+# = uri/ldap.rb
+#
+# Author::
+# Takaaki Tateishi <ttate@jaist.ac.jp>
+# Akira Yamada <akira@ruby-lang.org>
+# License::
+# URI::LDAP is copyrighted free software by Takaaki Tateishi and Akira Yamada.
+# You can redistribute it and/or modify it under the same term as Ruby.
+# Revision:: $Id: ldap.rb 11708 2007-02-12 23:01:19Z shyouhei $
+#
+
+require 'uri/generic'
+
+module URI
+
+ #
+ # LDAP URI SCHEMA (described in RFC2255)
+ # ldap://<host>/<dn>[?<attrs>[?<scope>[?<filter>[?<extensions>]]]]
+ #
+ class LDAP < Generic
+
+ DEFAULT_PORT = 389
+
+ COMPONENT = [
+ :scheme,
+ :host, :port,
+ :dn,
+ :attributes,
+ :scope,
+ :filter,
+ :extensions,
+ ].freeze
+
+ SCOPE = [
+ SCOPE_ONE = 'one',
+ SCOPE_SUB = 'sub',
+ SCOPE_BASE = 'base',
+ ].freeze
+
+ def self.build(args)
+ tmp = Util::make_components_hash(self, args)
+
+ if tmp[:dn]
+ tmp[:path] = tmp[:dn]
+ end
+
+ query = []
+ [:extensions, :filter, :scope, :attributes].collect do |x|
+ next if !tmp[x] && query.size == 0
+ query.unshift(tmp[x])
+ end
+
+ tmp[:query] = query.join('?')
+
+ return super(tmp)
+ end
+
+ def initialize(*arg)
+ super(*arg)
+
+ if @fragment
+ raise InvalidURIError, 'bad LDAP URL'
+ end
+
+ parse_dn
+ parse_query
+ end
+
+ def parse_dn
+ @dn = @path[1..-1]
+ end
+ private :parse_dn
+
+ def parse_query
+ @attributes = nil
+ @scope = nil
+ @filter = nil
+ @extensions = nil
+
+ if @query
+ attrs, scope, filter, extensions = @query.split('?')
+
+ @attributes = attrs if attrs && attrs.size > 0
+ @scope = scope if scope && scope.size > 0
+ @filter = filter if filter && filter.size > 0
+ @extensions = extensions if extensions && extensions.size > 0
+ end
+ end
+ private :parse_query
+
+ def build_path_query
+ @path = '/' + @dn
+
+ query = []
+ [@extensions, @filter, @scope, @attributes].each do |x|
+ next if !x && query.size == 0
+ query.unshift(x)
+ end
+ @query = query.join('?')
+ end
+ private :build_path_query
+
+ def dn
+ @dn
+ end
+
+ def set_dn(val)
+ @dn = val
+ build_path_query
+ @dn
+ end
+ protected :set_dn
+
+ def dn=(val)
+ set_dn(val)
+ val
+ end
+
+ def attributes
+ @attributes
+ end
+
+ def set_attributes(val)
+ @attributes = val
+ build_path_query
+ @attributes
+ end
+ protected :set_attributes
+
+ def attributes=(val)
+ set_attributes(val)
+ val
+ end
+
+ def scope
+ @scope
+ end
+
+ def set_scope(val)
+ @scope = val
+ build_path_query
+ @scope
+ end
+ protected :set_scope
+
+ def scope=(val)
+ set_scope(val)
+ val
+ end
+
+ def filter
+ @filter
+ end
+
+ def set_filter(val)
+ @filter = val
+ build_path_query
+ @filter
+ end
+ protected :set_filter
+
+ def filter=(val)
+ set_filter(val)
+ val
+ end
+
+ def extensions
+ @extensions
+ end
+
+ def set_extensions(val)
+ @extensions = val
+ build_path_query
+ @extensions
+ end
+ protected :set_extensions
+
+ def extensions=(val)
+ set_extensions(val)
+ val
+ end
+
+ def hierarchical?
+ false
+ end
+ end
+
+ @@schemes['LDAP'] = LDAP
+end
diff --git a/ruby/lib/uri/ldaps.rb b/ruby/lib/uri/ldaps.rb
new file mode 100644
index 0000000..6da3331
--- /dev/null
+++ b/ruby/lib/uri/ldaps.rb
@@ -0,0 +1,12 @@
+require 'uri/ldap'
+
+module URI
+
+ # The default port for LDAPS URIs is 636, and the scheme is 'ldaps:' rather
+ # than 'ldap:'. Other than that, LDAPS URIs are identical to LDAP URIs;
+ # see URI::LDAP.
+ class LDAPS < LDAP
+ DEFAULT_PORT = 636
+ end
+ @@schemes['LDAPS'] = LDAPS
+end
diff --git a/ruby/lib/uri/mailto.rb b/ruby/lib/uri/mailto.rb
new file mode 100644
index 0000000..ed3d77e
--- /dev/null
+++ b/ruby/lib/uri/mailto.rb
@@ -0,0 +1,266 @@
+#
+# = uri/mailto.rb
+#
+# Author:: Akira Yamada <akira@ruby-lang.org>
+# License:: You can redistribute it and/or modify it under the same term as Ruby.
+# Revision:: $Id: mailto.rb 19495 2008-09-23 18:16:08Z drbrain $
+#
+
+require 'uri/generic'
+
+module URI
+
+ #
+ # RFC2368, The mailto URL scheme
+ #
+ class MailTo < Generic
+ include REGEXP
+
+ DEFAULT_PORT = nil
+
+ COMPONENT = [ :scheme, :to, :headers ].freeze
+
+ # :stopdoc:
+ # "hname" and "hvalue" are encodings of an RFC 822 header name and
+ # value, respectively. As with "to", all URL reserved characters must
+ # be encoded.
+ #
+ # "#mailbox" is as specified in RFC 822 [RFC822]. This means that it
+ # consists of zero or more comma-separated mail addresses, possibly
+ # including "phrase" and "comment" components. Note that all URL
+ # reserved characters in "to" must be encoded: in particular,
+ # parentheses, commas, and the percent sign ("%"), which commonly occur
+ # in the "mailbox" syntax.
+ #
+ # Within mailto URLs, the characters "?", "=", "&" are reserved.
+
+ # hname = *urlc
+ # hvalue = *urlc
+ # header = hname "=" hvalue
+ HEADER_PATTERN = "(?:[^?=&]*=[^?=&]*)".freeze
+ HEADER_REGEXP = Regexp.new(HEADER_PATTERN, 'N').freeze
+ # headers = "?" header *( "&" header )
+ # to = #mailbox
+ # mailtoURL = "mailto:" [ to ] [ headers ]
+ MAILBOX_PATTERN = "(?:#{PATTERN::ESCAPED}|[^(),%?=&])".freeze
+ MAILTO_REGEXP = Regexp.new(" # :nodoc:
+ \\A
+ (#{MAILBOX_PATTERN}*?) (?# 1: to)
+ (?:
+ \\?
+ (#{HEADER_PATTERN}(?:\\&#{HEADER_PATTERN})*) (?# 2: headers)
+ )?
+ (?:
+ \\#
+ (#{PATTERN::FRAGMENT}) (?# 3: fragment)
+ )?
+ \\z
+ ", Regexp::EXTENDED).freeze
+ # :startdoc:
+
+ #
+ # == Description
+ #
+ # Creates a new URI::MailTo object from components, with syntax checking.
+ #
+ # Components can be provided as an Array or Hash. If an Array is used,
+ # the components must be supplied as [to, headers].
+ #
+ # If a Hash is used, the keys are the component names preceded by colons.
+ #
+ # The headers can be supplied as a pre-encoded string, such as
+ # "subject=subscribe&cc=address", or as an Array of Arrays like
+ # [['subject', 'subscribe'], ['cc', 'address']]
+ #
+ # Examples:
+ #
+ # require 'uri'
+ #
+ # m1 = URI::MailTo.build(['joe@example.com', 'subject=Ruby'])
+ # puts m1.to_s -> mailto:joe@example.com?subject=Ruby
+ #
+ # m2 = URI::MailTo.build(['john@example.com', [['Subject', 'Ruby'], ['Cc', 'jack@example.com']]])
+ # puts m2.to_s -> mailto:john@example.com?Subject=Ruby&Cc=jack@example.com
+ #
+ # m3 = URI::MailTo.build({:to => 'listman@example.com', :headers => [['subject', 'subscribe']]})
+ # puts m3.to_s -> mailto:listman@example.com?subject=subscribe
+ #
+ def self.build(args)
+ tmp = Util::make_components_hash(self, args)
+
+ if tmp[:to]
+ tmp[:opaque] = tmp[:to]
+ else
+ tmp[:opaque] = ''
+ end
+
+ if tmp[:headers]
+ tmp[:opaque] << '?'
+
+ if tmp[:headers].kind_of?(Array)
+ tmp[:opaque] << tmp[:headers].collect { |x|
+ if x.kind_of?(Array)
+ x[0] + '=' + x[1..-1].to_s
+ else
+ x.to_s
+ end
+ }.join('&')
+
+ elsif tmp[:headers].kind_of?(Hash)
+ tmp[:opaque] << tmp[:headers].collect { |h,v|
+ h + '=' + v
+ }.join('&')
+
+ else
+ tmp[:opaque] << tmp[:headers].to_s
+ end
+ end
+
+ return super(tmp)
+ end
+
+ #
+ # == Description
+ #
+ # Creates a new URI::MailTo object from generic URL components with
+ # no syntax checking.
+ #
+ # This method is usually called from URI::parse, which checks
+ # the validity of each component.
+ #
+ def initialize(*arg)
+ super(*arg)
+
+ @to = nil
+ @headers = []
+
+ if MAILTO_REGEXP =~ @opaque
+ if arg[-1]
+ self.to = $1
+ self.headers = $2
+ else
+ set_to($1)
+ set_headers($2)
+ end
+
+ else
+ raise InvalidComponentError,
+ "unrecognised opaque part for mailtoURL: #{@opaque}"
+ end
+ end
+
+ # The primary e-mail address of the URL, as a String
+ attr_reader :to
+
+ # E-mail headers set by the URL, as an Array of Arrays
+ attr_reader :headers
+
+ def check_to(v)
+ return true unless v
+ return true if v.size == 0
+
+ if @parser.regexp[:OPAQUE] !~ v || /\A#{MAILBOX_PATTERN}*\z/o !~ v
+ raise InvalidComponentError,
+ "bad component(expected opaque component): #{v}"
+ end
+
+ return true
+ end
+ private :check_to
+
+ def set_to(v)
+ @to = v
+ end
+ protected :set_to
+
+ def to=(v)
+ check_to(v)
+ set_to(v)
+ v
+ end
+
+ def check_headers(v)
+ return true unless v
+ return true if v.size == 0
+
+ if @parser.regexp[:OPAQUE] !~ v ||
+ /\A(#{HEADER_PATTERN}(?:\&#{HEADER_PATTERN})*)\z/o !~ v
+ raise InvalidComponentError,
+ "bad component(expected opaque component): #{v}"
+ end
+
+ return true
+ end
+ private :check_headers
+
+ def set_headers(v)
+ @headers = []
+ if v
+ v.scan(HEADER_REGEXP) do |x|
+ @headers << x.split(/=/o, 2)
+ end
+ end
+ end
+ protected :set_headers
+
+ def headers=(v)
+ check_headers(v)
+ set_headers(v)
+ v
+ end
+
+ def to_s
+ @scheme + ':' +
+ if @to
+ @to
+ else
+ ''
+ end +
+ if @headers.size > 0
+ '?' + @headers.collect{|x| x.join('=')}.join('&')
+ else
+ ''
+ end +
+ if @fragment
+ '#' + @fragment
+ else
+ ''
+ end
+ end
+
+ # Returns the RFC822 e-mail text equivalent of the URL, as a String.
+ #
+ # Example:
+ #
+ # require 'uri'
+ #
+ # uri = URI.parse("mailto:ruby-list@ruby-lang.org?Subject=subscribe&cc=myaddr")
+ # uri.to_mailtext
+ # # => "To: ruby-list@ruby-lang.org\nSubject: subscribe\nCc: myaddr\n\n\n"
+ #
+ def to_mailtext
+ to = @parser.unescape(@to)
+ head = ''
+ body = ''
+ @headers.each do |x|
+ case x[0]
+ when 'body'
+ body = @parser.unescape(x[1])
+ when 'to'
+ to << ', ' + @parser.unescape(x[1])
+ else
+ head << @parser.unescape(x[0]).capitalize + ': ' +
+ @parser.unescape(x[1]) + "\n"
+ end
+ end
+
+ return "To: #{to}
+#{head}
+#{body}
+"
+ end
+ alias to_rfc822text to_mailtext
+ end
+
+ @@schemes['MAILTO'] = MailTo
+end
diff --git a/ruby/lib/weakref.rb b/ruby/lib/weakref.rb
new file mode 100644
index 0000000..ba39242
--- /dev/null
+++ b/ruby/lib/weakref.rb
@@ -0,0 +1,80 @@
+# Weak Reference class that does not bother GCing.
+#
+# Usage:
+# foo = Object.new
+# foo = Object.new
+# p foo.to_s # original's class
+# foo = WeakRef.new(foo)
+# p foo.to_s # should be same class
+# ObjectSpace.garbage_collect
+# p foo.to_s # should raise exception (recycled)
+
+require "delegate"
+require 'thread'
+
+class WeakRef < Delegator
+
+ class RefError < StandardError
+ end
+
+ @@id_map = {} # obj -> [ref,...]
+ @@id_rev_map = {} # ref -> obj
+ @@mutex = Mutex.new
+ @@final = lambda {|id|
+ @@mutex.synchronize {
+ rids = @@id_map[id]
+ if rids
+ for rid in rids
+ @@id_rev_map.delete(rid)
+ end
+ @@id_map.delete(id)
+ end
+ rid = @@id_rev_map[id]
+ if rid
+ @@id_rev_map.delete(id)
+ @@id_map[rid].delete(id)
+ @@id_map.delete(rid) if @@id_map[rid].empty?
+ end
+ }
+ }
+
+ def initialize(orig)
+ @__id = orig.object_id
+ ObjectSpace.define_finalizer orig, @@final
+ ObjectSpace.define_finalizer self, @@final
+ @@mutex.synchronize {
+ @@id_map[@__id] = [] unless @@id_map[@__id]
+ }
+ @@id_map[@__id].push self.object_id
+ @@id_rev_map[self.object_id] = @__id
+ super
+ end
+
+ def __getobj__
+ unless @@id_rev_map[self.object_id] == @__id
+ Kernel::raise RefError, "Invalid Reference - probably recycled", Kernel::caller(2)
+ end
+ begin
+ ObjectSpace._id2ref(@__id)
+ rescue RangeError
+ Kernel::raise RefError, "Invalid Reference - probably recycled", Kernel::caller(2)
+ end
+ end
+ def __setobj__(obj)
+ end
+
+ def weakref_alive?
+ @@id_rev_map[self.object_id] == @__id
+ end
+end
+
+if __FILE__ == $0
+# require 'thread'
+ foo = Object.new
+ p foo.to_s # original's class
+ foo = WeakRef.new(foo)
+ p foo.to_s # should be same class
+ ObjectSpace.garbage_collect
+ ObjectSpace.garbage_collect
+ p foo.to_s # should raise exception (recycled)
+end
diff --git a/ruby/lib/webrick.rb b/ruby/lib/webrick.rb
new file mode 100644
index 0000000..8fca81b
--- /dev/null
+++ b/ruby/lib/webrick.rb
@@ -0,0 +1,29 @@
+#
+# WEBrick -- WEB server toolkit.
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000 TAKAHASHI Masayoshi, GOTOU YUUZOU
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: webrick.rb,v 1.12 2002/10/01 17:16:31 gotoyuzo Exp $
+
+require 'webrick/compat.rb'
+
+require 'webrick/version.rb'
+require 'webrick/config.rb'
+require 'webrick/log.rb'
+require 'webrick/server.rb'
+require 'webrick/utils.rb'
+require 'webrick/accesslog'
+
+require 'webrick/htmlutils.rb'
+require 'webrick/httputils.rb'
+require 'webrick/cookie.rb'
+require 'webrick/httpversion.rb'
+require 'webrick/httpstatus.rb'
+require 'webrick/httprequest.rb'
+require 'webrick/httpresponse.rb'
+require 'webrick/httpserver.rb'
+require 'webrick/httpservlet.rb'
+require 'webrick/httpauth.rb'
diff --git a/ruby/lib/webrick/accesslog.rb b/ruby/lib/webrick/accesslog.rb
new file mode 100644
index 0000000..75a3a3e
--- /dev/null
+++ b/ruby/lib/webrick/accesslog.rb
@@ -0,0 +1,75 @@
+#
+# accesslog.rb -- Access log handling utilities
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2002 keita yamaguchi
+# Copyright (c) 2002 Internet Programming with Ruby writers
+#
+# $IPR: accesslog.rb,v 1.1 2002/10/01 17:16:32 gotoyuzo Exp $
+
+module WEBrick
+ module AccessLog
+ class AccessLogError < StandardError; end
+
+ CLF_TIME_FORMAT = "[%d/%b/%Y:%H:%M:%S %Z]"
+ COMMON_LOG_FORMAT = "%h %l %u %t \"%r\" %s %b"
+ CLF = COMMON_LOG_FORMAT
+ REFERER_LOG_FORMAT = "%{Referer}i -> %U"
+ AGENT_LOG_FORMAT = "%{User-Agent}i"
+ COMBINED_LOG_FORMAT = "#{CLF} \"%{Referer}i\" \"%{User-agent}i\""
+
+ module_function
+
+ # This format specification is a subset of mod_log_config of Apache.
+ # http://httpd.apache.org/docs/mod/mod_log_config.html#formats
+ def setup_params(config, req, res)
+ params = Hash.new("")
+ params["a"] = req.peeraddr[3]
+ params["b"] = res.sent_size
+ params["e"] = ENV
+ params["f"] = res.filename || ""
+ params["h"] = req.peeraddr[2]
+ params["i"] = req
+ params["l"] = "-"
+ params["m"] = req.request_method
+ params["n"] = req.attributes
+ params["o"] = res
+ params["p"] = req.port
+ params["q"] = req.query_string
+ params["r"] = req.request_line.sub(/\x0d?\x0a\z/o, '')
+ params["s"] = res.status # won't support "%>s"
+ params["t"] = req.request_time
+ params["T"] = Time.now - req.request_time
+ params["u"] = req.user || "-"
+ params["U"] = req.unparsed_uri
+ params["v"] = config[:ServerName]
+ params
+ end
+
+ def format(format_string, params)
+ format_string.gsub(/\%(?:\{(.*?)\})?>?([a-zA-Z%])/){
+ param, spec = $1, $2
+ case spec[0]
+ when ?e, ?i, ?n, ?o
+ raise AccessLogError,
+ "parameter is required for \"#{spec}\"" unless param
+ param = params[spec][param] ? escape(param) : "-"
+ when ?t
+ params[spec].strftime(param || CLF_TIME_FORMAT)
+ when ?%
+ "%"
+ else
+ escape(params[spec].to_s)
+ end
+ }
+ end
+
+ def escape(data)
+ if data.tainted?
+ data.gsub(/[[:cntrl:]\\]+/) {$&.dump[1...-1]}.untaint
+ else
+ data
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/cgi.rb b/ruby/lib/webrick/cgi.rb
new file mode 100644
index 0000000..056997d
--- /dev/null
+++ b/ruby/lib/webrick/cgi.rb
@@ -0,0 +1,260 @@
+#
+# cgi.rb -- Yet another CGI library
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $Id: cgi.rb 18678 2008-08-17 17:33:13Z gotoyuzo $
+
+require "webrick/httprequest"
+require "webrick/httpresponse"
+require "webrick/config"
+require "stringio"
+
+module WEBrick
+ class CGI
+ CGIError = Class.new(StandardError)
+
+ attr_reader :config, :logger
+
+ def initialize(*args)
+ if defined?(MOD_RUBY)
+ unless ENV.has_key?("GATEWAY_INTERFACE")
+ Apache.request.setup_cgi_env
+ end
+ end
+ if %r{HTTP/(\d+\.\d+)} =~ ENV["SERVER_PROTOCOL"]
+ httpv = $1
+ end
+ @config = WEBrick::Config::HTTP.dup.update(
+ :ServerSoftware => ENV["SERVER_SOFTWARE"] || "null",
+ :HTTPVersion => HTTPVersion.new(httpv || "1.0"),
+ :RunOnCGI => true, # to detect if it runs on CGI.
+ :NPH => false # set true to run as NPH script.
+ )
+ if config = args.shift
+ @config.update(config)
+ end
+ @config[:Logger] ||= WEBrick::BasicLog.new($stderr)
+ @logger = @config[:Logger]
+ @options = args
+ end
+
+ def [](key)
+ @config[key]
+ end
+
+ def start(env=ENV, stdin=$stdin, stdout=$stdout)
+ sock = WEBrick::CGI::Socket.new(@config, env, stdin, stdout)
+ req = HTTPRequest.new(@config)
+ res = HTTPResponse.new(@config)
+ unless @config[:NPH] or defined?(MOD_RUBY)
+ def res.setup_header
+ unless @header["status"]
+ phrase = HTTPStatus::reason_phrase(@status)
+ @header["status"] = "#{@status} #{phrase}"
+ end
+ super
+ end
+ def res.status_line
+ ""
+ end
+ end
+
+ begin
+ req.parse(sock)
+ req.script_name = (env["SCRIPT_NAME"] || File.expand_path($0)).dup
+ req.path_info = (env["PATH_INFO"] || "").dup
+ req.query_string = env["QUERY_STRING"]
+ req.user = env["REMOTE_USER"]
+ res.request_method = req.request_method
+ res.request_uri = req.request_uri
+ res.request_http_version = req.http_version
+ res.keep_alive = req.keep_alive?
+ self.service(req, res)
+ rescue HTTPStatus::Error => ex
+ res.set_error(ex)
+ rescue HTTPStatus::Status => ex
+ res.status = ex.code
+ rescue Exception => ex
+ @logger.error(ex)
+ res.set_error(ex, true)
+ ensure
+ req.fixup
+ if defined?(MOD_RUBY)
+ res.setup_header
+ Apache.request.status_line = "#{res.status} #{res.reason_phrase}"
+ Apache.request.status = res.status
+ table = Apache.request.headers_out
+ res.header.each{|key, val|
+ case key
+ when /^content-encoding$/i
+ Apache::request.content_encoding = val
+ when /^content-type$/i
+ Apache::request.content_type = val
+ else
+ table[key] = val.to_s
+ end
+ }
+ res.cookies.each{|cookie|
+ table.add("Set-Cookie", cookie.to_s)
+ }
+ Apache.request.send_http_header
+ res.send_body(sock)
+ else
+ res.send_response(sock)
+ end
+ end
+ end
+
+ def service(req, res)
+ method_name = "do_" + req.request_method.gsub(/-/, "_")
+ if respond_to?(method_name)
+ __send__(method_name, req, res)
+ else
+ raise HTTPStatus::MethodNotAllowed,
+ "unsupported method `#{req.request_method}'."
+ end
+ end
+
+ class Socket
+ include Enumerable
+
+ private
+
+ def initialize(config, env, stdin, stdout)
+ @config = config
+ @env = env
+ @header_part = StringIO.new
+ @body_part = stdin
+ @out_port = stdout
+ @out_port.binmode
+
+ @server_addr = @env["SERVER_ADDR"] || "0.0.0.0"
+ @server_name = @env["SERVER_NAME"]
+ @server_port = @env["SERVER_PORT"]
+ @remote_addr = @env["REMOTE_ADDR"]
+ @remote_host = @env["REMOTE_HOST"] || @remote_addr
+ @remote_port = @env["REMOTE_PORT"] || 0
+
+ begin
+ @header_part << request_line << CRLF
+ setup_header
+ @header_part << CRLF
+ @header_part.rewind
+ rescue Exception => ex
+ raise CGIError, "invalid CGI environment"
+ end
+ end
+
+ def request_line
+ meth = @env["REQUEST_METHOD"] || "GET"
+ unless url = @env["REQUEST_URI"]
+ url = (@env["SCRIPT_NAME"] || File.expand_path($0)).dup
+ url << @env["PATH_INFO"].to_s
+ url = WEBrick::HTTPUtils.escape_path(url)
+ if query_string = @env["QUERY_STRING"]
+ unless query_string.empty?
+ url << "?" << query_string
+ end
+ end
+ end
+ # we cannot get real HTTP version of client ;)
+ httpv = @config[:HTTPVersion]
+ return "#{meth} #{url} HTTP/#{httpv}"
+ end
+
+ def setup_header
+ @env.each{|key, value|
+ case key
+ when "CONTENT_TYPE", "CONTENT_LENGTH"
+ add_header(key.gsub(/_/, "-"), value)
+ when /^HTTP_(.*)/
+ add_header($1.gsub(/_/, "-"), value)
+ end
+ }
+ end
+
+ def add_header(hdrname, value)
+ unless value.empty?
+ @header_part << hdrname << ": " << value << CRLF
+ end
+ end
+
+ def input
+ @header_part.eof? ? @body_part : @header_part
+ end
+
+ public
+
+ def peeraddr
+ [nil, @remote_port, @remote_host, @remote_addr]
+ end
+
+ def addr
+ [nil, @server_port, @server_name, @server_addr]
+ end
+
+ def gets(eol=LF, size=nil)
+ input.gets(eol, size)
+ end
+
+ def read(size=nil)
+ input.read(size)
+ end
+
+ def each
+ input.each{|line| yield(line) }
+ end
+
+ def eof?
+ input.eof?
+ end
+
+ def <<(data)
+ @out_port << data
+ end
+
+ def cert
+ return nil unless defined?(OpenSSL)
+ if pem = @env["SSL_SERVER_CERT"]
+ OpenSSL::X509::Certificate.new(pem) unless pem.empty?
+ end
+ end
+
+ def peer_cert
+ return nil unless defined?(OpenSSL)
+ if pem = @env["SSL_CLIENT_CERT"]
+ OpenSSL::X509::Certificate.new(pem) unless pem.empty?
+ end
+ end
+
+ def peer_cert_chain
+ return nil unless defined?(OpenSSL)
+ if @env["SSL_CLIENT_CERT_CHAIN_0"]
+ keys = @env.keys
+ certs = keys.sort.collect{|k|
+ if /^SSL_CLIENT_CERT_CHAIN_\d+$/ =~ k
+ if pem = @env[k]
+ OpenSSL::X509::Certificate.new(pem) unless pem.empty?
+ end
+ end
+ }
+ certs.compact
+ end
+ end
+
+ def cipher
+ return nil unless defined?(OpenSSL)
+ if cipher = @env["SSL_CIPHER"]
+ ret = [ cipher ]
+ ret << @env["SSL_PROTOCOL"]
+ ret << @env["SSL_CIPHER_USEKEYSIZE"]
+ ret << @env["SSL_CIPHER_ALGKEYSIZE"]
+ ret
+ end
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/compat.rb b/ruby/lib/webrick/compat.rb
new file mode 100644
index 0000000..ad7760b
--- /dev/null
+++ b/ruby/lib/webrick/compat.rb
@@ -0,0 +1,15 @@
+#
+# compat.rb -- cross platform compatibility
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2002 GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: compat.rb,v 1.6 2002/10/01 17:16:32 gotoyuzo Exp $
+
+module Errno
+ class EPROTO < SystemCallError; end
+ class ECONNRESET < SystemCallError; end
+ class ECONNABORTED < SystemCallError; end
+end
diff --git a/ruby/lib/webrick/config.rb b/ruby/lib/webrick/config.rb
new file mode 100644
index 0000000..121669c
--- /dev/null
+++ b/ruby/lib/webrick/config.rb
@@ -0,0 +1,100 @@
+#
+# config.rb -- Default configurations.
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: config.rb,v 1.52 2003/07/22 19:20:42 gotoyuzo Exp $
+
+require 'webrick/version'
+require 'webrick/httpversion'
+require 'webrick/httputils'
+require 'webrick/utils'
+require 'webrick/log'
+
+module WEBrick
+ module Config
+ LIBDIR = File::dirname(__FILE__)
+
+ # for GenericServer
+ General = {
+ :ServerName => Utils::getservername,
+ :BindAddress => nil, # "0.0.0.0" or "::" or nil
+ :Port => nil, # users MUST specifiy this!!
+ :MaxClients => 100, # maximum number of the concurrent connections
+ :ServerType => nil, # default: WEBrick::SimpleServer
+ :Logger => nil, # default: WEBrick::Log.new
+ :ServerSoftware => "WEBrick/#{WEBrick::VERSION} " +
+ "(Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})",
+ :TempDir => ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'/tmp',
+ :DoNotListen => false,
+ :StartCallback => nil,
+ :StopCallback => nil,
+ :AcceptCallback => nil,
+ :DoNotReverseLookup => nil,
+ :ShutdownSocketWithoutClose => false,
+ }
+
+ # for HTTPServer, HTTPRequest, HTTPResponse ...
+ HTTP = General.dup.update(
+ :Port => 80,
+ :RequestTimeout => 30,
+ :HTTPVersion => HTTPVersion.new("1.1"),
+ :AccessLog => nil,
+ :MimeTypes => HTTPUtils::DefaultMimeTypes,
+ :DirectoryIndex => ["index.html","index.htm","index.cgi","index.rhtml"],
+ :DocumentRoot => nil,
+ :DocumentRootOptions => { :FancyIndexing => true },
+ :RequestCallback => nil,
+ :ServerAlias => nil,
+ :InputBufferSize => 65536, # input buffer size in reading request body
+ :OutputBufferSize => 65536, # output buffer size in sending File or IO
+
+ # for HTTPProxyServer
+ :ProxyAuthProc => nil,
+ :ProxyContentHandler => nil,
+ :ProxyVia => true,
+ :ProxyTimeout => true,
+ :ProxyURI => nil,
+
+ :CGIInterpreter => nil,
+ :CGIPathEnv => nil,
+
+ # workaround: if Request-URIs contain 8bit chars,
+ # they should be escaped before calling of URI::parse().
+ :Escape8bitURI => false
+ )
+
+ FileHandler = {
+ :NondisclosureName => [".ht*", "*~"],
+ :FancyIndexing => false,
+ :HandlerTable => {},
+ :HandlerCallback => nil,
+ :DirectoryCallback => nil,
+ :FileCallback => nil,
+ :UserDir => nil, # e.g. "public_html"
+ :AcceptableLanguages => [] # ["en", "ja", ... ]
+ }
+
+ BasicAuth = {
+ :AutoReloadUserDB => true,
+ }
+
+ DigestAuth = {
+ :Algorithm => 'MD5-sess', # or 'MD5'
+ :Domain => nil, # an array includes domain names.
+ :Qop => [ 'auth' ], # 'auth' or 'auth-int' or both.
+ :UseOpaque => true,
+ :UseNextNonce => false,
+ :CheckNc => false,
+ :UseAuthenticationInfoHeader => true,
+ :AutoReloadUserDB => true,
+ :NonceExpirePeriod => 30*60,
+ :NonceExpireDelta => 60,
+ :InternetExplorerHack => true,
+ :OperaHack => true,
+ }
+ end
+end
diff --git a/ruby/lib/webrick/cookie.rb b/ruby/lib/webrick/cookie.rb
new file mode 100644
index 0000000..814e664
--- /dev/null
+++ b/ruby/lib/webrick/cookie.rb
@@ -0,0 +1,110 @@
+#
+# cookie.rb -- Cookie class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: cookie.rb,v 1.16 2002/09/21 12:23:35 gotoyuzo Exp $
+
+require 'time'
+require 'webrick/httputils'
+
+module WEBrick
+ class Cookie
+
+ attr_reader :name
+ attr_accessor :value, :version
+ attr_accessor :domain, :path, :secure
+ attr_accessor :comment, :max_age
+ #attr_accessor :comment_url, :discard, :port
+
+ def initialize(name, value)
+ @name = name
+ @value = value
+ @version = 0 # Netscape Cookie
+
+ @domain = @path = @secure = @comment = @max_age =
+ @expires = @comment_url = @discard = @port = nil
+ end
+
+ def expires=(t)
+ @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
+ end
+
+ def expires
+ @expires && Time.parse(@expires)
+ end
+
+ def to_s
+ ret = ""
+ ret << @name << "=" << @value
+ ret << "; " << "Version=" << @version.to_s if @version > 0
+ ret << "; " << "Domain=" << @domain if @domain
+ ret << "; " << "Expires=" << @expires if @expires
+ ret << "; " << "Max-Age=" << @max_age.to_s if @max_age
+ ret << "; " << "Comment=" << @comment if @comment
+ ret << "; " << "Path=" << @path if @path
+ ret << "; " << "Secure" if @secure
+ ret
+ end
+
+ # Cookie::parse()
+ # It parses Cookie field sent from the user agent.
+ def self.parse(str)
+ if str
+ ret = []
+ cookie = nil
+ ver = 0
+ str.split(/[;,]\s+/).each{|x|
+ key, val = x.split(/=/,2)
+ val = val ? HTTPUtils::dequote(val) : ""
+ case key
+ when "$Version"; ver = val.to_i
+ when "$Path"; cookie.path = val
+ when "$Domain"; cookie.domain = val
+ when "$Port"; cookie.port = val
+ else
+ ret << cookie if cookie
+ cookie = self.new(key, val)
+ cookie.version = ver
+ end
+ }
+ ret << cookie if cookie
+ ret
+ end
+ end
+
+ def self.parse_set_cookie(str)
+ cookie_elem = str.split(/;/)
+ first_elem = cookie_elem.shift
+ first_elem.strip!
+ key, value = first_elem.split(/=/, 2)
+ cookie = new(key, HTTPUtils.dequote(value))
+ cookie_elem.each{|pair|
+ pair.strip!
+ key, value = pair.split(/=/, 2)
+ if value
+ value = HTTPUtils.dequote(value.strip)
+ end
+ case key.downcase
+ when "domain" then cookie.domain = value
+ when "path" then cookie.path = value
+ when "expires" then cookie.expires = value
+ when "max-age" then cookie.max_age = Integer(value)
+ when "comment" then cookie.comment = value
+ when "version" then cookie.version = Integer(value)
+ when "secure" then cookie.secure = true
+ end
+ }
+ return cookie
+ end
+
+ def self.parse_set_cookies(str)
+ return str.split(/,(?=[^;,]*=)|,$/).collect{|c|
+ parse_set_cookie(c)
+ }
+ end
+ end
+end
diff --git a/ruby/lib/webrick/htmlutils.rb b/ruby/lib/webrick/htmlutils.rb
new file mode 100644
index 0000000..cf8d542
--- /dev/null
+++ b/ruby/lib/webrick/htmlutils.rb
@@ -0,0 +1,25 @@
+#
+# htmlutils.rb -- HTMLUtils Module
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: htmlutils.rb,v 1.7 2002/09/21 12:23:35 gotoyuzo Exp $
+
+module WEBrick
+ module HTMLUtils
+
+ def escape(string)
+ str = string ? string.dup : ""
+ str.gsub!(/&/n, '&amp;')
+ str.gsub!(/\"/n, '&quot;')
+ str.gsub!(/>/n, '&gt;')
+ str.gsub!(/</n, '&lt;')
+ str
+ end
+ module_function :escape
+
+ end
+end
diff --git a/ruby/lib/webrick/httpauth.rb b/ruby/lib/webrick/httpauth.rb
new file mode 100644
index 0000000..147c040
--- /dev/null
+++ b/ruby/lib/webrick/httpauth.rb
@@ -0,0 +1,45 @@
+#
+# httpauth.rb -- HTTP access authentication
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httpauth.rb,v 1.14 2003/07/22 19:20:42 gotoyuzo Exp $
+
+require 'webrick/httpauth/basicauth'
+require 'webrick/httpauth/digestauth'
+require 'webrick/httpauth/htpasswd'
+require 'webrick/httpauth/htdigest'
+require 'webrick/httpauth/htgroup'
+
+module WEBrick
+ module HTTPAuth
+ module_function
+
+ def _basic_auth(req, res, realm, req_field, res_field, err_type, block)
+ user = pass = nil
+ if /^Basic\s+(.*)/o =~ req[req_field]
+ userpass = $1
+ user, pass = userpass.unpack("m*")[0].split(":", 2)
+ end
+ if block.call(user, pass)
+ req.user = user
+ return
+ end
+ res[res_field] = "Basic realm=\"#{realm}\""
+ raise err_type
+ end
+
+ def basic_auth(req, res, realm, &block)
+ _basic_auth(req, res, realm, "Authorization", "WWW-Authenticate",
+ HTTPStatus::Unauthorized, block)
+ end
+
+ def proxy_basic_auth(req, res, realm, &block)
+ _basic_auth(req, res, realm, "Proxy-Authorization", "Proxy-Authenticate",
+ HTTPStatus::ProxyAuthenticationRequired, block)
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpauth/authenticator.rb b/ruby/lib/webrick/httpauth/authenticator.rb
new file mode 100644
index 0000000..f90d1bf
--- /dev/null
+++ b/ruby/lib/webrick/httpauth/authenticator.rb
@@ -0,0 +1,79 @@
+#
+# httpauth/authenticator.rb -- Authenticator mix-in module.
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: authenticator.rb,v 1.3 2003/02/20 07:15:47 gotoyuzo Exp $
+
+module WEBrick
+ module HTTPAuth
+ module Authenticator
+ RequestField = "Authorization"
+ ResponseField = "WWW-Authenticate"
+ ResponseInfoField = "Authentication-Info"
+ AuthException = HTTPStatus::Unauthorized
+ AuthScheme = nil # must override by the derived class
+
+ attr_reader :realm, :userdb, :logger
+
+ private
+
+ def check_init(config)
+ [:UserDB, :Realm].each{|sym|
+ unless config[sym]
+ raise ArgumentError, "Argument #{sym.inspect} missing."
+ end
+ }
+ @realm = config[:Realm]
+ @userdb = config[:UserDB]
+ @logger = config[:Logger] || Log::new($stderr)
+ @reload_db = config[:AutoReloadUserDB]
+ @request_field = self::class::RequestField
+ @response_field = self::class::ResponseField
+ @resp_info_field = self::class::ResponseInfoField
+ @auth_exception = self::class::AuthException
+ @auth_scheme = self::class::AuthScheme
+ end
+
+ def check_scheme(req)
+ unless credentials = req[@request_field]
+ error("no credentials in the request.")
+ return nil
+ end
+ unless match = /^#{@auth_scheme}\s+/i.match(credentials)
+ error("invalid scheme in %s.", credentials)
+ info("%s: %s", @request_field, credentials) if $DEBUG
+ return nil
+ end
+ return match.post_match
+ end
+
+ def log(meth, fmt, *args)
+ msg = format("%s %s: ", @auth_scheme, @realm)
+ msg << fmt % args
+ @logger.send(meth, msg)
+ end
+
+ def error(fmt, *args)
+ if @logger.error?
+ log(:error, fmt, *args)
+ end
+ end
+
+ def info(fmt, *args)
+ if @logger.info?
+ log(:info, fmt, *args)
+ end
+ end
+ end
+
+ module ProxyAuthenticator
+ RequestField = "Proxy-Authorization"
+ ResponseField = "Proxy-Authenticate"
+ InfoField = "Proxy-Authentication-Info"
+ AuthException = HTTPStatus::ProxyAuthenticationRequired
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpauth/basicauth.rb b/ruby/lib/webrick/httpauth/basicauth.rb
new file mode 100644
index 0000000..210fb00
--- /dev/null
+++ b/ruby/lib/webrick/httpauth/basicauth.rb
@@ -0,0 +1,65 @@
+#
+# httpauth/basicauth.rb -- HTTP basic access authentication
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: basicauth.rb,v 1.5 2003/02/20 07:15:47 gotoyuzo Exp $
+
+require 'webrick/config'
+require 'webrick/httpstatus'
+require 'webrick/httpauth/authenticator'
+
+module WEBrick
+ module HTTPAuth
+ class BasicAuth
+ include Authenticator
+
+ AuthScheme = "Basic"
+
+ def self.make_passwd(realm, user, pass)
+ pass ||= ""
+ pass.crypt(Utils::random_string(2))
+ end
+
+ attr_reader :realm, :userdb, :logger
+
+ def initialize(config, default=Config::BasicAuth)
+ check_init(config)
+ @config = default.dup.update(config)
+ end
+
+ def authenticate(req, res)
+ unless basic_credentials = check_scheme(req)
+ challenge(req, res)
+ end
+ userid, password = basic_credentials.unpack("m*")[0].split(":", 2)
+ password ||= ""
+ if userid.empty?
+ error("user id was not given.")
+ challenge(req, res)
+ end
+ unless encpass = @userdb.get_passwd(@realm, userid, @reload_db)
+ error("%s: the user is not allowed.", userid)
+ challenge(req, res)
+ end
+ if password.crypt(encpass) != encpass
+ error("%s: password unmatch.", userid)
+ challenge(req, res)
+ end
+ info("%s: authentication succeeded.", userid)
+ req.user = userid
+ end
+
+ def challenge(req, res)
+ res[@response_field] = "#{@auth_scheme} realm=\"#{@realm}\""
+ raise @auth_exception
+ end
+ end
+
+ class ProxyBasicAuth < BasicAuth
+ include ProxyAuthenticator
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpauth/digestauth.rb b/ruby/lib/webrick/httpauth/digestauth.rb
new file mode 100644
index 0000000..eec064c
--- /dev/null
+++ b/ruby/lib/webrick/httpauth/digestauth.rb
@@ -0,0 +1,344 @@
+#
+# httpauth/digestauth.rb -- HTTP digest access authentication
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2003 Internet Programming with Ruby writers.
+# Copyright (c) 2003 H.M.
+#
+# The original implementation is provided by H.M.
+# URL: http://rwiki.jin.gr.jp/cgi-bin/rw-cgi.rb?cmd=view;name=
+# %C7%A7%BE%DA%B5%A1%C7%BD%A4%F2%B2%FE%C2%A4%A4%B7%A4%C6%A4%DF%A4%EB
+#
+# $IPR: digestauth.rb,v 1.5 2003/02/20 07:15:47 gotoyuzo Exp $
+
+require 'webrick/config'
+require 'webrick/httpstatus'
+require 'webrick/httpauth/authenticator'
+require 'digest/md5'
+require 'digest/sha1'
+
+module WEBrick
+ module HTTPAuth
+ class DigestAuth
+ include Authenticator
+
+ AuthScheme = "Digest"
+ OpaqueInfo = Struct.new(:time, :nonce, :nc)
+ attr_reader :algorithm, :qop
+
+ def self.make_passwd(realm, user, pass)
+ pass ||= ""
+ Digest::MD5::hexdigest([user, realm, pass].join(":"))
+ end
+
+ def initialize(config, default=Config::DigestAuth)
+ check_init(config)
+ @config = default.dup.update(config)
+ @algorithm = @config[:Algorithm]
+ @domain = @config[:Domain]
+ @qop = @config[:Qop]
+ @use_opaque = @config[:UseOpaque]
+ @use_next_nonce = @config[:UseNextNonce]
+ @check_nc = @config[:CheckNc]
+ @use_auth_info_header = @config[:UseAuthenticationInfoHeader]
+ @nonce_expire_period = @config[:NonceExpirePeriod]
+ @nonce_expire_delta = @config[:NonceExpireDelta]
+ @internet_explorer_hack = @config[:InternetExplorerHack]
+ @opera_hack = @config[:OperaHack]
+
+ case @algorithm
+ when 'MD5','MD5-sess'
+ @h = Digest::MD5
+ when 'SHA1','SHA1-sess' # it is a bonus feature :-)
+ @h = Digest::SHA1
+ else
+ msg = format('Algorithm "%s" is not supported.', @algorithm)
+ raise ArgumentError.new(msg)
+ end
+
+ @instance_key = hexdigest(self.__id__, Time.now.to_i, Process.pid)
+ @opaques = {}
+ @last_nonce_expire = Time.now
+ @mutex = Mutex.new
+ end
+
+ def authenticate(req, res)
+ unless result = @mutex.synchronize{ _authenticate(req, res) }
+ challenge(req, res)
+ end
+ if result == :nonce_is_stale
+ challenge(req, res, true)
+ end
+ return true
+ end
+
+ def challenge(req, res, stale=false)
+ nonce = generate_next_nonce(req)
+ if @use_opaque
+ opaque = generate_opaque(req)
+ @opaques[opaque].nonce = nonce
+ end
+
+ param = Hash.new
+ param["realm"] = HTTPUtils::quote(@realm)
+ param["domain"] = HTTPUtils::quote(@domain.to_a.join(" ")) if @domain
+ param["nonce"] = HTTPUtils::quote(nonce)
+ param["opaque"] = HTTPUtils::quote(opaque) if opaque
+ param["stale"] = stale.to_s
+ param["algorithm"] = @algorithm
+ param["qop"] = HTTPUtils::quote(@qop.to_a.join(",")) if @qop
+
+ res[@response_field] =
+ "#{@auth_scheme} " + param.map{|k,v| "#{k}=#{v}" }.join(", ")
+ info("%s: %s", @response_field, res[@response_field]) if $DEBUG
+ raise @auth_exception
+ end
+
+ private
+
+ MustParams = ['username','realm','nonce','uri','response']
+ MustParamsAuth = ['cnonce','nc']
+
+ def _authenticate(req, res)
+ unless digest_credentials = check_scheme(req)
+ return false
+ end
+
+ auth_req = split_param_value(digest_credentials)
+ if auth_req['qop'] == "auth" || auth_req['qop'] == "auth-int"
+ req_params = MustParams + MustParamsAuth
+ else
+ req_params = MustParams
+ end
+ req_params.each{|key|
+ unless auth_req.has_key?(key)
+ error('%s: parameter missing. "%s"', auth_req['username'], key)
+ raise HTTPStatus::BadRequest
+ end
+ }
+
+ if !check_uri(req, auth_req)
+ raise HTTPStatus::BadRequest
+ end
+
+ if auth_req['realm'] != @realm
+ error('%s: realm unmatch. "%s" for "%s"',
+ auth_req['username'], auth_req['realm'], @realm)
+ return false
+ end
+
+ auth_req['algorithm'] ||= 'MD5'
+ if auth_req['algorithm'] != @algorithm &&
+ (@opera_hack && auth_req['algorithm'] != @algorithm.upcase)
+ error('%s: algorithm unmatch. "%s" for "%s"',
+ auth_req['username'], auth_req['algorithm'], @algorithm)
+ return false
+ end
+
+ if (@qop.nil? && auth_req.has_key?('qop')) ||
+ (@qop && (! @qop.member?(auth_req['qop'])))
+ error('%s: the qop is not allowed. "%s"',
+ auth_req['username'], auth_req['qop'])
+ return false
+ end
+
+ password = @userdb.get_passwd(@realm, auth_req['username'], @reload_db)
+ unless password
+ error('%s: the user is not allowd.', auth_req['username'])
+ return false
+ end
+
+ nonce_is_invalid = false
+ if @use_opaque
+ info("@opaque = %s", @opaque.inspect) if $DEBUG
+ if !(opaque = auth_req['opaque'])
+ error('%s: opaque is not given.', auth_req['username'])
+ nonce_is_invalid = true
+ elsif !(opaque_struct = @opaques[opaque])
+ error('%s: invalid opaque is given.', auth_req['username'])
+ nonce_is_invalid = true
+ elsif !check_opaque(opaque_struct, req, auth_req)
+ @opaques.delete(auth_req['opaque'])
+ nonce_is_invalid = true
+ end
+ elsif !check_nonce(req, auth_req)
+ nonce_is_invalid = true
+ end
+
+ if /-sess$/ =~ auth_req['algorithm'] ||
+ (@opera_hack && /-SESS$/ =~ auth_req['algorithm'])
+ ha1 = hexdigest(password, auth_req['nonce'], auth_req['cnonce'])
+ else
+ ha1 = password
+ end
+
+ if auth_req['qop'] == "auth" || auth_req['qop'] == nil
+ ha2 = hexdigest(req.request_method, auth_req['uri'])
+ ha2_res = hexdigest("", auth_req['uri'])
+ elsif auth_req['qop'] == "auth-int"
+ ha2 = hexdigest(req.request_method, auth_req['uri'],
+ hexdigest(req.body))
+ ha2_res = hexdigest("", auth_req['uri'], hexdigest(res.body))
+ end
+
+ if auth_req['qop'] == "auth" || auth_req['qop'] == "auth-int"
+ param2 = ['nonce', 'nc', 'cnonce', 'qop'].map{|key|
+ auth_req[key]
+ }.join(':')
+ digest = hexdigest(ha1, param2, ha2)
+ digest_res = hexdigest(ha1, param2, ha2_res)
+ else
+ digest = hexdigest(ha1, auth_req['nonce'], ha2)
+ digest_res = hexdigest(ha1, auth_req['nonce'], ha2_res)
+ end
+
+ if digest != auth_req['response']
+ error("%s: digest unmatch.", auth_req['username'])
+ return false
+ elsif nonce_is_invalid
+ error('%s: digest is valid, but nonce is not valid.',
+ auth_req['username'])
+ return :nonce_is_stale
+ elsif @use_auth_info_header
+ auth_info = {
+ 'nextnonce' => generate_next_nonce(req),
+ 'rspauth' => digest_res
+ }
+ if @use_opaque
+ opaque_struct.time = req.request_time
+ opaque_struct.nonce = auth_info['nextnonce']
+ opaque_struct.nc = "%08x" % (auth_req['nc'].hex + 1)
+ end
+ if auth_req['qop'] == "auth" || auth_req['qop'] == "auth-int"
+ ['qop','cnonce','nc'].each{|key|
+ auth_info[key] = auth_req[key]
+ }
+ end
+ res[@resp_info_field] = auth_info.keys.map{|key|
+ if key == 'nc'
+ key + '=' + auth_info[key]
+ else
+ key + "=" + HTTPUtils::quote(auth_info[key])
+ end
+ }.join(', ')
+ end
+ info('%s: authentication scceeded.', auth_req['username'])
+ req.user = auth_req['username']
+ return true
+ end
+
+ def split_param_value(string)
+ ret = {}
+ while string.bytesize != 0
+ case string
+ when /^\s*([\w\-\.\*\%\!]+)=\s*\"((\\.|[^\"])*)\"\s*,?/
+ key = $1
+ matched = $2
+ string = $'
+ ret[key] = matched.gsub(/\\(.)/, "\\1")
+ when /^\s*([\w\-\.\*\%\!]+)=\s*([^,\"]*),?/
+ key = $1
+ matched = $2
+ string = $'
+ ret[key] = matched.clone
+ when /^s*^,/
+ string = $'
+ else
+ break
+ end
+ end
+ ret
+ end
+
+ def generate_next_nonce(req)
+ now = "%012d" % req.request_time.to_i
+ pk = hexdigest(now, @instance_key)[0,32]
+ nonce = [now + ":" + pk].pack("m*").chop # it has 60 length of chars.
+ nonce
+ end
+
+ def check_nonce(req, auth_req)
+ username = auth_req['username']
+ nonce = auth_req['nonce']
+
+ pub_time, pk = nonce.unpack("m*")[0].split(":", 2)
+ if (!pub_time || !pk)
+ error("%s: empty nonce is given", username)
+ return false
+ elsif (hexdigest(pub_time, @instance_key)[0,32] != pk)
+ error("%s: invalid private-key: %s for %s",
+ username, hexdigest(pub_time, @instance_key)[0,32], pk)
+ return false
+ end
+
+ diff_time = req.request_time.to_i - pub_time.to_i
+ if (diff_time < 0)
+ error("%s: difference of time-stamp is negative.", username)
+ return false
+ elsif diff_time > @nonce_expire_period
+ error("%s: nonce is expired.", username)
+ return false
+ end
+
+ return true
+ end
+
+ def generate_opaque(req)
+ @mutex.synchronize{
+ now = req.request_time
+ if now - @last_nonce_expire > @nonce_expire_delta
+ @opaques.delete_if{|key,val|
+ (now - val.time) > @nonce_expire_period
+ }
+ @last_nonce_expire = now
+ end
+ begin
+ opaque = Utils::random_string(16)
+ end while @opaques[opaque]
+ @opaques[opaque] = OpaqueInfo.new(now, nil, '00000001')
+ opaque
+ }
+ end
+
+ def check_opaque(opaque_struct, req, auth_req)
+ if (@use_next_nonce && auth_req['nonce'] != opaque_struct.nonce)
+ error('%s: nonce unmatched. "%s" for "%s"',
+ auth_req['username'], auth_req['nonce'], opaque_struct.nonce)
+ return false
+ elsif !check_nonce(req, auth_req)
+ return false
+ end
+ if (@check_nc && auth_req['nc'] != opaque_struct.nc)
+ error('%s: nc unmatched."%s" for "%s"',
+ auth_req['username'], auth_req['nc'], opaque_struct.nc)
+ return false
+ end
+ true
+ end
+
+ def check_uri(req, auth_req)
+ uri = auth_req['uri']
+ if uri != req.request_uri.to_s && uri != req.unparsed_uri &&
+ (@internet_explorer_hack && uri != req.path)
+ error('%s: uri unmatch. "%s" for "%s"', auth_req['username'],
+ auth_req['uri'], req.request_uri.to_s)
+ return false
+ end
+ true
+ end
+
+ def hexdigest(*args)
+ @h.hexdigest(args.join(":"))
+ end
+
+ end
+
+ class ProxyDigestAuth < DigestAuth
+ include ProxyAuthenticator
+
+ def check_uri(req, auth_req)
+ return true
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpauth/htdigest.rb b/ruby/lib/webrick/httpauth/htdigest.rb
new file mode 100644
index 0000000..3949756
--- /dev/null
+++ b/ruby/lib/webrick/httpauth/htdigest.rb
@@ -0,0 +1,91 @@
+#
+# httpauth/htdigest.rb -- Apache compatible htdigest file
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: htdigest.rb,v 1.4 2003/07/22 19:20:45 gotoyuzo Exp $
+
+require 'webrick/httpauth/userdb'
+require 'webrick/httpauth/digestauth'
+require 'tempfile'
+
+module WEBrick
+ module HTTPAuth
+ class Htdigest
+ include UserDB
+
+ def initialize(path)
+ @path = path
+ @mtime = Time.at(0)
+ @digest = Hash.new
+ @mutex = Mutex::new
+ @auth_type = DigestAuth
+ open(@path,"a").close unless File::exist?(@path)
+ reload
+ end
+
+ def reload
+ mtime = File::mtime(@path)
+ if mtime > @mtime
+ @digest.clear
+ open(@path){|io|
+ while line = io.gets
+ line.chomp!
+ user, realm, pass = line.split(/:/, 3)
+ unless @digest[realm]
+ @digest[realm] = Hash.new
+ end
+ @digest[realm][user] = pass
+ end
+ }
+ @mtime = mtime
+ end
+ end
+
+ def flush(output=nil)
+ output ||= @path
+ tmp = Tempfile.new("htpasswd", File::dirname(output))
+ begin
+ each{|item| tmp.puts(item.join(":")) }
+ tmp.close
+ File::rename(tmp.path, output)
+ rescue
+ tmp.close(true)
+ end
+ end
+
+ def get_passwd(realm, user, reload_db)
+ reload() if reload_db
+ if hash = @digest[realm]
+ hash[user]
+ end
+ end
+
+ def set_passwd(realm, user, pass)
+ @mutex.synchronize{
+ unless @digest[realm]
+ @digest[realm] = Hash.new
+ end
+ @digest[realm][user] = make_passwd(realm, user, pass)
+ }
+ end
+
+ def delete_passwd(realm, user)
+ if hash = @digest[realm]
+ hash.delete(user)
+ end
+ end
+
+ def each
+ @digest.keys.sort.each{|realm|
+ hash = @digest[realm]
+ hash.keys.sort.each{|user|
+ yield([user, realm, hash[user]])
+ }
+ }
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpauth/htgroup.rb b/ruby/lib/webrick/httpauth/htgroup.rb
new file mode 100644
index 0000000..c9270c6
--- /dev/null
+++ b/ruby/lib/webrick/httpauth/htgroup.rb
@@ -0,0 +1,61 @@
+#
+# httpauth/htgroup.rb -- Apache compatible htgroup file
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: htgroup.rb,v 1.1 2003/02/16 22:22:56 gotoyuzo Exp $
+
+require 'tempfile'
+
+module WEBrick
+ module HTTPAuth
+ class Htgroup
+ def initialize(path)
+ @path = path
+ @mtime = Time.at(0)
+ @group = Hash.new
+ open(@path,"a").close unless File::exist?(@path)
+ reload
+ end
+
+ def reload
+ if (mtime = File::mtime(@path)) > @mtime
+ @group.clear
+ open(@path){|io|
+ while line = io.gets
+ line.chomp!
+ group, members = line.split(/:\s*/)
+ @group[group] = members.split(/\s+/)
+ end
+ }
+ @mtime = mtime
+ end
+ end
+
+ def flush(output=nil)
+ output ||= @path
+ tmp = Tempfile.new("htgroup", File::dirname(output))
+ begin
+ @group.keys.sort.each{|group|
+ tmp.puts(format("%s: %s", group, self.members(group).join(" ")))
+ }
+ tmp.close
+ File::rename(tmp.path, output)
+ rescue
+ tmp.close(true)
+ end
+ end
+
+ def members(group)
+ reload
+ @group[group] || []
+ end
+
+ def add(group, members)
+ @group[group] = members(group) | members
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpauth/htpasswd.rb b/ruby/lib/webrick/httpauth/htpasswd.rb
new file mode 100644
index 0000000..8a05886
--- /dev/null
+++ b/ruby/lib/webrick/httpauth/htpasswd.rb
@@ -0,0 +1,83 @@
+#
+# httpauth/htpasswd -- Apache compatible htpasswd file
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: htpasswd.rb,v 1.4 2003/07/22 19:20:45 gotoyuzo Exp $
+
+require 'webrick/httpauth/userdb'
+require 'webrick/httpauth/basicauth'
+require 'tempfile'
+
+module WEBrick
+ module HTTPAuth
+ class Htpasswd
+ include UserDB
+
+ def initialize(path)
+ @path = path
+ @mtime = Time.at(0)
+ @passwd = Hash.new
+ @auth_type = BasicAuth
+ open(@path,"a").close unless File::exist?(@path)
+ reload
+ end
+
+ def reload
+ mtime = File::mtime(@path)
+ if mtime > @mtime
+ @passwd.clear
+ open(@path){|io|
+ while line = io.gets
+ line.chomp!
+ case line
+ when %r!\A[^:]+:[a-zA-Z0-9./]{13}\z!
+ user, pass = line.split(":")
+ when /:\$/, /:{SHA}/
+ raise NotImplementedError,
+ 'MD5, SHA1 .htpasswd file not supported'
+ else
+ raise StandardError, 'bad .htpasswd file'
+ end
+ @passwd[user] = pass
+ end
+ }
+ @mtime = mtime
+ end
+ end
+
+ def flush(output=nil)
+ output ||= @path
+ tmp = Tempfile.new("htpasswd", File::dirname(output))
+ begin
+ each{|item| tmp.puts(item.join(":")) }
+ tmp.close
+ File::rename(tmp.path, output)
+ rescue
+ tmp.close(true)
+ end
+ end
+
+ def get_passwd(realm, user, reload_db)
+ reload() if reload_db
+ @passwd[user]
+ end
+
+ def set_passwd(realm, user, pass)
+ @passwd[user] = make_passwd(realm, user, pass)
+ end
+
+ def delete_passwd(realm, user)
+ @passwd.delete(user)
+ end
+
+ def each
+ @passwd.keys.sort.each{|user|
+ yield([user, @passwd[user]])
+ }
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpauth/userdb.rb b/ruby/lib/webrick/httpauth/userdb.rb
new file mode 100644
index 0000000..33e0140
--- /dev/null
+++ b/ruby/lib/webrick/httpauth/userdb.rb
@@ -0,0 +1,29 @@
+#
+# httpauth/userdb.rb -- UserDB mix-in module.
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: userdb.rb,v 1.2 2003/02/20 07:15:48 gotoyuzo Exp $
+
+module WEBrick
+ module HTTPAuth
+ module UserDB
+ attr_accessor :auth_type # BasicAuth or DigestAuth
+
+ def make_passwd(realm, user, pass)
+ @auth_type::make_passwd(realm, user, pass)
+ end
+
+ def set_passwd(realm, user, pass)
+ self[user] = pass
+ end
+
+ def get_passwd(realm, user, reload_db=false)
+ # reload_db is dummy
+ make_passwd(realm, user, self[user])
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpproxy.rb b/ruby/lib/webrick/httpproxy.rb
new file mode 100644
index 0000000..f35a177
--- /dev/null
+++ b/ruby/lib/webrick/httpproxy.rb
@@ -0,0 +1,288 @@
+#
+# httpproxy.rb -- HTTPProxy Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2002 GOTO Kentaro
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httpproxy.rb,v 1.18 2003/03/08 18:58:10 gotoyuzo Exp $
+# $kNotwork: straw.rb,v 1.3 2002/02/12 15:13:07 gotoken Exp $
+
+require "webrick/httpserver"
+require "net/http"
+
+Net::HTTP::version_1_2 if RUBY_VERSION < "1.7"
+
+module WEBrick
+ NullReader = Object.new
+ class << NullReader
+ def read(*args)
+ nil
+ end
+ alias gets read
+ end
+
+ FakeProxyURI = Object.new
+ class << FakeProxyURI
+ def method_missing(meth, *args)
+ if %w(scheme host port path query userinfo).member?(meth.to_s)
+ return nil
+ end
+ super
+ end
+ end
+
+ class HTTPProxyServer < HTTPServer
+ def initialize(config={}, default=Config::HTTP)
+ super(config, default)
+ c = @config
+ @via = "#{c[:HTTPVersion]} #{c[:ServerName]}:#{c[:Port]}"
+ end
+
+ def service(req, res)
+ if req.request_method == "CONNECT"
+ do_CONNECT(req, res)
+ elsif req.unparsed_uri =~ %r!^http://!
+ proxy_service(req, res)
+ else
+ super(req, res)
+ end
+ end
+
+ def proxy_auth(req, res)
+ if proc = @config[:ProxyAuthProc]
+ proc.call(req, res)
+ end
+ req.header.delete("proxy-authorization")
+ end
+
+ def proxy_uri(req, res)
+ # should return upstream proxy server's URI
+ return @config[:ProxyURI]
+ end
+
+ def proxy_service(req, res)
+ # Proxy Authentication
+ proxy_auth(req, res)
+
+ begin
+ self.send("do_#{req.request_method}", req, res)
+ rescue NoMethodError
+ raise HTTPStatus::MethodNotAllowed,
+ "unsupported method `#{req.request_method}'."
+ rescue => err
+ logger.debug("#{err.class}: #{err.message}")
+ raise HTTPStatus::ServiceUnavailable, err.message
+ end
+
+ # Process contents
+ if handler = @config[:ProxyContentHandler]
+ handler.call(req, res)
+ end
+ end
+
+ def do_CONNECT(req, res)
+ # Proxy Authentication
+ proxy_auth(req, res)
+
+ ua = Thread.current[:WEBrickSocket] # User-Agent
+ raise HTTPStatus::InternalServerError,
+ "[BUG] cannot get socket" unless ua
+
+ host, port = req.unparsed_uri.split(":", 2)
+ # Proxy authentication for upstream proxy server
+ if proxy = proxy_uri(req, res)
+ proxy_request_line = "CONNECT #{host}:#{port} HTTP/1.0"
+ if proxy.userinfo
+ credentials = "Basic " + [proxy.userinfo].pack("m").delete("\n")
+ end
+ host, port = proxy.host, proxy.port
+ end
+
+ begin
+ @logger.debug("CONNECT: upstream proxy is `#{host}:#{port}'.")
+ os = TCPSocket.new(host, port) # origin server
+
+ if proxy
+ @logger.debug("CONNECT: sending a Request-Line")
+ os << proxy_request_line << CRLF
+ @logger.debug("CONNECT: > #{proxy_request_line}")
+ if credentials
+ @logger.debug("CONNECT: sending a credentials")
+ os << "Proxy-Authorization: " << credentials << CRLF
+ end
+ os << CRLF
+ proxy_status_line = os.gets(LF)
+ @logger.debug("CONNECT: read a Status-Line form the upstream server")
+ @logger.debug("CONNECT: < #{proxy_status_line}")
+ if %r{^HTTP/\d+\.\d+\s+200\s*} =~ proxy_status_line
+ while line = os.gets(LF)
+ break if /\A(#{CRLF}|#{LF})\z/om =~ line
+ end
+ else
+ raise HTTPStatus::BadGateway
+ end
+ end
+ @logger.debug("CONNECT #{host}:#{port}: succeeded")
+ res.status = HTTPStatus::RC_OK
+ rescue => ex
+ @logger.debug("CONNECT #{host}:#{port}: failed `#{ex.message}'")
+ res.set_error(ex)
+ raise HTTPStatus::EOFError
+ ensure
+ if handler = @config[:ProxyContentHandler]
+ handler.call(req, res)
+ end
+ res.send_response(ua)
+ access_log(@config, req, res)
+
+ # Should clear request-line not to send the sesponse twice.
+ # see: HTTPServer#run
+ req.parse(NullReader) rescue nil
+ end
+
+ begin
+ while fds = IO::select([ua, os])
+ if fds[0].member?(ua)
+ buf = ua.sysread(1024);
+ @logger.debug("CONNECT: #{buf.bytesize} byte from User-Agent")
+ os.syswrite(buf)
+ elsif fds[0].member?(os)
+ buf = os.sysread(1024);
+ @logger.debug("CONNECT: #{buf.bytesize} byte from #{host}:#{port}")
+ ua.syswrite(buf)
+ end
+ end
+ rescue => ex
+ os.close
+ @logger.debug("CONNECT #{host}:#{port}: closed")
+ end
+
+ raise HTTPStatus::EOFError
+ end
+
+ def do_GET(req, res)
+ perform_proxy_request(req, res) do |http, path, header|
+ http.get(path, header)
+ end
+ end
+
+ def do_HEAD(req, res)
+ perform_proxy_request(req, res) do |http, path, header|
+ http.head(path, header)
+ end
+ end
+
+ def do_POST(req, res)
+ perform_proxy_request(req, res) do |http, path, header|
+ http.post(path, req.body || "", header)
+ end
+ end
+
+ def do_OPTIONS(req, res)
+ res['allow'] = "GET,HEAD,POST,OPTIONS,CONNECT"
+ end
+
+ private
+
+ # Some header fields should not be transferred.
+ HopByHop = %w( connection keep-alive proxy-authenticate upgrade
+ proxy-authorization te trailers transfer-encoding )
+ ShouldNotTransfer = %w( set-cookie proxy-connection )
+ def split_field(f) f ? f.split(/,\s+/).collect{|i| i.downcase } : [] end
+
+ def choose_header(src, dst)
+ connections = split_field(src['connection'])
+ src.each{|key, value|
+ key = key.downcase
+ if HopByHop.member?(key) || # RFC2616: 13.5.1
+ connections.member?(key) || # RFC2616: 14.10
+ ShouldNotTransfer.member?(key) # pragmatics
+ @logger.debug("choose_header: `#{key}: #{value}'")
+ next
+ end
+ dst[key] = value
+ }
+ end
+
+ # Net::HTTP is stupid about the multiple header fields.
+ # Here is workaround:
+ def set_cookie(src, dst)
+ if str = src['set-cookie']
+ cookies = []
+ str.split(/,\s*/).each{|token|
+ if /^[^=]+;/o =~ token
+ cookies[-1] << ", " << token
+ elsif /=/o =~ token
+ cookies << token
+ else
+ cookies[-1] << ", " << token
+ end
+ }
+ dst.cookies.replace(cookies)
+ end
+ end
+
+ def set_via(h)
+ if @config[:ProxyVia]
+ if h['via']
+ h['via'] << ", " << @via
+ else
+ h['via'] = @via
+ end
+ end
+ end
+
+ def setup_proxy_header(req, res)
+ # Choose header fields to transfer
+ header = Hash.new
+ choose_header(req, header)
+ set_via(header)
+ return header
+ end
+
+ def setup_upstream_proxy_authentication(req, res, header)
+ if upstream = proxy_uri(req, res)
+ if upstream.userinfo
+ header['proxy-authorization'] =
+ "Basic " + [upstream.userinfo].pack("m").delete("\n")
+ end
+ return upstream
+ end
+ return FakeProxyURI
+ end
+
+ def perform_proxy_request(req, res)
+ uri = req.request_uri
+ path = uri.path.dup
+ path << "?" << uri.query if uri.query
+ header = setup_proxy_header(req, res)
+ upstream = setup_upstream_proxy_authentication(req, res, header)
+ response = nil
+
+ http = Net::HTTP.new(uri.host, uri.port, upstream.host, upstream.port)
+ http.start do
+ if @config[:ProxyTimeout]
+ ################################## these issues are
+ http.open_timeout = 30 # secs # necessary (maybe bacause
+ http.read_timeout = 60 # secs # Ruby's bug, but why?)
+ ##################################
+ end
+ response = yield(http, path, header)
+ end
+
+ # Persistent connection requirements are mysterious for me.
+ # So I will close the connection in every response.
+ res['proxy-connection'] = "close"
+ res['connection'] = "close"
+
+ # Convert Net::HTTP::HTTPResponse to WEBrick::HTTPResponse
+ res.status = response.code.to_i
+ choose_header(response, res)
+ set_cookie(response, res)
+ set_via(res)
+ res.body = response.body
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httprequest.rb b/ruby/lib/webrick/httprequest.rb
new file mode 100644
index 0000000..47102b6
--- /dev/null
+++ b/ruby/lib/webrick/httprequest.rb
@@ -0,0 +1,402 @@
+#
+# httprequest.rb -- HTTPRequest Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httprequest.rb,v 1.64 2003/07/13 17:18:22 gotoyuzo Exp $
+
+require 'uri'
+require 'webrick/httpversion'
+require 'webrick/httpstatus'
+require 'webrick/httputils'
+require 'webrick/cookie'
+
+module WEBrick
+ class HTTPRequest
+ BODY_CONTAINABLE_METHODS = [ "POST", "PUT" ]
+
+ # Request line
+ attr_reader :request_line
+ attr_reader :request_method, :unparsed_uri, :http_version
+
+ # Request-URI
+ attr_reader :request_uri, :path
+ attr_accessor :script_name, :path_info, :query_string
+
+ # Header and entity body
+ attr_reader :raw_header, :header, :cookies
+ attr_reader :accept, :accept_charset
+ attr_reader :accept_encoding, :accept_language
+
+ # Misc
+ attr_accessor :user
+ attr_reader :addr, :peeraddr
+ attr_reader :attributes
+ attr_reader :keep_alive
+ attr_reader :request_time
+
+ def initialize(config)
+ @config = config
+ @buffer_size = @config[:InputBufferSize]
+ @logger = config[:Logger]
+
+ @request_line = @request_method =
+ @unparsed_uri = @http_version = nil
+
+ @request_uri = @host = @port = @path = nil
+ @script_name = @path_info = nil
+ @query_string = nil
+ @query = nil
+ @form_data = nil
+
+ @raw_header = Array.new
+ @header = nil
+ @cookies = []
+ @accept = []
+ @accept_charset = []
+ @accept_encoding = []
+ @accept_language = []
+ @body = ""
+
+ @addr = @peeraddr = nil
+ @attributes = {}
+ @user = nil
+ @keep_alive = false
+ @request_time = nil
+
+ @remaining_size = nil
+ @socket = nil
+
+ @forwarded_proto = @forwarded_host = @forwarded_port =
+ @forwarded_server = @forwarded_for = nil
+ end
+
+ def parse(socket=nil)
+ @socket = socket
+ begin
+ @peeraddr = socket.respond_to?(:peeraddr) ? socket.peeraddr : []
+ @addr = socket.respond_to?(:addr) ? socket.addr : []
+ rescue Errno::ENOTCONN
+ raise HTTPStatus::EOFError
+ end
+
+ read_request_line(socket)
+ if @http_version.major > 0
+ read_header(socket)
+ @header['cookie'].each{|cookie|
+ @cookies += Cookie::parse(cookie)
+ }
+ @accept = HTTPUtils.parse_qvalues(self['accept'])
+ @accept_charset = HTTPUtils.parse_qvalues(self['accept-charset'])
+ @accept_encoding = HTTPUtils.parse_qvalues(self['accept-encoding'])
+ @accept_language = HTTPUtils.parse_qvalues(self['accept-language'])
+ end
+ return if @request_method == "CONNECT"
+ return if @unparsed_uri == "*"
+
+ begin
+ setup_forwarded_info
+ @request_uri = parse_uri(@unparsed_uri)
+ @path = HTTPUtils::unescape(@request_uri.path)
+ @path = HTTPUtils::normalize_path(@path)
+ @host = @request_uri.host
+ @port = @request_uri.port
+ @query_string = @request_uri.query
+ @script_name = ""
+ @path_info = @path.dup
+ rescue
+ raise HTTPStatus::BadRequest, "bad URI `#{@unparsed_uri}'."
+ end
+
+ if /close/io =~ self["connection"]
+ @keep_alive = false
+ elsif /keep-alive/io =~ self["connection"]
+ @keep_alive = true
+ elsif @http_version < "1.1"
+ @keep_alive = false
+ else
+ @keep_alive = true
+ end
+ end
+
+ def body(&block)
+ block ||= Proc.new{|chunk| @body << chunk }
+ read_body(@socket, block)
+ @body.empty? ? nil : @body
+ end
+
+ def query
+ unless @query
+ parse_query()
+ end
+ @query
+ end
+
+ def content_length
+ return Integer(self['content-length'])
+ end
+
+ def content_type
+ return self['content-type']
+ end
+
+ def [](header_name)
+ if @header
+ value = @header[header_name.downcase]
+ value.empty? ? nil : value.join(", ")
+ end
+ end
+
+ def each
+ @header.each{|k, v|
+ value = @header[k]
+ yield(k, value.empty? ? nil : value.join(", "))
+ }
+ end
+
+ def host
+ return @forwarded_host || @host
+ end
+
+ def port
+ return @forwarded_port || @port
+ end
+
+ def server_name
+ return @forwarded_server || @config[:ServerName]
+ end
+
+ def remote_ip
+ return self["client-ip"] || @forwarded_for || @peeraddr[3]
+ end
+
+ def ssl?
+ return @request_uri.scheme == "https"
+ end
+
+ def keep_alive?
+ @keep_alive
+ end
+
+ def to_s
+ ret = @request_line.dup
+ @raw_header.each{|line| ret << line }
+ ret << CRLF
+ ret << body if body
+ ret
+ end
+
+ def fixup()
+ begin
+ body{|chunk| } # read remaining body
+ rescue HTTPStatus::Error => ex
+ @logger.error("HTTPRequest#fixup: #{ex.class} occured.")
+ @keep_alive = false
+ rescue => ex
+ @logger.error(ex)
+ @keep_alive = false
+ end
+ end
+
+ def meta_vars
+ # This method provides the metavariables defined by the revision 3
+ # of ``The WWW Common Gateway Interface Version 1.1''.
+ # (http://Web.Golux.Com/coar/cgi/)
+
+ meta = Hash.new
+
+ cl = self["Content-Length"]
+ ct = self["Content-Type"]
+ meta["CONTENT_LENGTH"] = cl if cl.to_i > 0
+ meta["CONTENT_TYPE"] = ct.dup if ct
+ meta["GATEWAY_INTERFACE"] = "CGI/1.1"
+ meta["PATH_INFO"] = @path_info ? @path_info.dup : ""
+ #meta["PATH_TRANSLATED"] = nil # no plan to be provided
+ meta["QUERY_STRING"] = @query_string ? @query_string.dup : ""
+ meta["REMOTE_ADDR"] = @peeraddr[3]
+ meta["REMOTE_HOST"] = @peeraddr[2]
+ #meta["REMOTE_IDENT"] = nil # no plan to be provided
+ meta["REMOTE_USER"] = @user
+ meta["REQUEST_METHOD"] = @request_method.dup
+ meta["REQUEST_URI"] = @request_uri.to_s
+ meta["SCRIPT_NAME"] = @script_name.dup
+ meta["SERVER_NAME"] = @host
+ meta["SERVER_PORT"] = @port.to_s
+ meta["SERVER_PROTOCOL"] = "HTTP/" + @config[:HTTPVersion].to_s
+ meta["SERVER_SOFTWARE"] = @config[:ServerSoftware].dup
+
+ self.each{|key, val|
+ next if /^content-type$/i =~ key
+ next if /^content-length$/i =~ key
+ name = "HTTP_" + key
+ name.gsub!(/-/o, "_")
+ name.upcase!
+ meta[name] = val
+ }
+
+ meta
+ end
+
+ private
+
+ def read_request_line(socket)
+ @request_line = read_line(socket, 1024) if socket
+ if @request_line.bytesize >= 1024 and @request_line[-1, 1] != LF
+ raise HTTPStatus::RequestURITooLarge
+ end
+ @request_time = Time.now
+ raise HTTPStatus::EOFError unless @request_line
+ if /^(\S+)\s+(\S++)(?:\s+HTTP\/(\d+\.\d+))?\r?\n/mo =~ @request_line
+ @request_method = $1
+ @unparsed_uri = $2
+ @http_version = HTTPVersion.new($3 ? $3 : "0.9")
+ else
+ rl = @request_line.sub(/\x0d?\x0a\z/o, '')
+ raise HTTPStatus::BadRequest, "bad Request-Line `#{rl}'."
+ end
+ end
+
+ def read_header(socket)
+ if socket
+ while line = read_line(socket)
+ break if /\A(#{CRLF}|#{LF})\z/om =~ line
+ @raw_header << line
+ end
+ end
+ @header = HTTPUtils::parse_header(@raw_header.join)
+ end
+
+ def parse_uri(str, scheme="http")
+ if @config[:Escape8bitURI]
+ str = HTTPUtils::escape8bit(str)
+ end
+ uri = URI::parse(str)
+ return uri if uri.absolute?
+ if @forwarded_host
+ host, port = @forwarded_host, @forwarded_port
+ elsif self["host"]
+ pattern = /\A(#{URI::REGEXP::PATTERN::HOST})(?::(\d+))?\z/n
+ host, port = *self['host'].scan(pattern)[0]
+ elsif @addr.size > 0
+ host, port = @addr[2], @addr[1]
+ else
+ host, port = @config[:ServerName], @config[:Port]
+ end
+ uri.scheme = @forwarded_proto || scheme
+ uri.host = host
+ uri.port = port ? port.to_i : nil
+ return URI::parse(uri.to_s)
+ end
+
+ def read_body(socket, block)
+ return unless socket
+ if tc = self['transfer-encoding']
+ case tc
+ when /chunked/io then read_chunked(socket, block)
+ else raise HTTPStatus::NotImplemented, "Transfer-Encoding: #{tc}."
+ end
+ elsif self['content-length'] || @remaining_size
+ @remaining_size ||= self['content-length'].to_i
+ while @remaining_size > 0
+ sz = [@buffer_size, @remaining_size].min
+ break unless buf = read_data(socket, sz)
+ @remaining_size -= buf.bytesize
+ block.call(buf)
+ end
+ if @remaining_size > 0 && @socket.eof?
+ raise HTTPStatus::BadRequest, "invalid body size."
+ end
+ elsif BODY_CONTAINABLE_METHODS.member?(@request_method)
+ raise HTTPStatus::LengthRequired
+ end
+ return @body
+ end
+
+ def read_chunk_size(socket)
+ line = read_line(socket)
+ if /^([0-9a-fA-F]+)(?:;(\S+))?/ =~ line
+ chunk_size = $1.hex
+ chunk_ext = $2
+ [ chunk_size, chunk_ext ]
+ else
+ raise HTTPStatus::BadRequest, "bad chunk `#{line}'."
+ end
+ end
+
+ def read_chunked(socket, block)
+ chunk_size, = read_chunk_size(socket)
+ while chunk_size > 0
+ data = read_data(socket, chunk_size) # read chunk-data
+ if data.nil? || data.bytesize != chunk_size
+ raise BadRequest, "bad chunk data size."
+ end
+ read_line(socket) # skip CRLF
+ block.call(data)
+ chunk_size, = read_chunk_size(socket)
+ end
+ read_header(socket) # trailer + CRLF
+ @header.delete("transfer-encoding")
+ @remaining_size = 0
+ end
+
+ def _read_data(io, method, *arg)
+ begin
+ WEBrick::Utils.timeout(@config[:RequestTimeout]){
+ return io.__send__(method, *arg)
+ }
+ rescue Errno::ECONNRESET
+ return nil
+ rescue TimeoutError
+ raise HTTPStatus::RequestTimeout
+ end
+ end
+
+ def read_line(io, size=4096)
+ _read_data(io, :gets, LF, size)
+ end
+
+ def read_data(io, size)
+ _read_data(io, :read, size)
+ end
+
+ def parse_query()
+ begin
+ if @request_method == "GET" || @request_method == "HEAD"
+ @query = HTTPUtils::parse_query(@query_string)
+ elsif self['content-type'] =~ /^application\/x-www-form-urlencoded/
+ @query = HTTPUtils::parse_query(body)
+ elsif self['content-type'] =~ /^multipart\/form-data; boundary=(.+)/
+ boundary = HTTPUtils::dequote($1)
+ @query = HTTPUtils::parse_form_data(body, boundary)
+ else
+ @query = Hash.new
+ end
+ rescue => ex
+ raise HTTPStatus::BadRequest, ex.message
+ end
+ end
+
+ PrivateNetworkRegexp = /
+ ^unknown$|
+ ^((::ffff:)?127.0.0.1|::1)$|
+ ^(::ffff:)?(10|172\.(1[6-9]|2[0-9]|3[01])|192\.168)\.
+ /ixo
+
+ def setup_forwarded_info
+ @forwarded_server = self["x-forwarded-server"]
+ @forwarded_proto = self["x-forwarded-proto"]
+ if host_port = self["x-forwarded-host"]
+ @forwarded_host, tmp = host_port.split(":", 2)
+ @forwarded_port = (tmp || (@forwarded_proto == "https" ? 443 : 80)).to_i
+ end
+ if addrs = self["x-forwarded-for"]
+ addrs = addrs.split(",").collect(&:strip)
+ addrs.reject!{|ip| PrivateNetworkRegexp =~ ip }
+ @forwarded_for = addrs.first
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpresponse.rb b/ruby/lib/webrick/httpresponse.rb
new file mode 100644
index 0000000..740a8fe
--- /dev/null
+++ b/ruby/lib/webrick/httpresponse.rb
@@ -0,0 +1,326 @@
+#
+# httpresponse.rb -- HTTPResponse Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httpresponse.rb,v 1.45 2003/07/11 11:02:25 gotoyuzo Exp $
+
+require 'time'
+require 'webrick/httpversion'
+require 'webrick/htmlutils'
+require 'webrick/httputils'
+require 'webrick/httpstatus'
+
+module WEBrick
+ class HTTPResponse
+ attr_reader :http_version, :status, :header
+ attr_reader :cookies
+ attr_accessor :reason_phrase
+ attr_accessor :body
+
+ attr_accessor :request_method, :request_uri, :request_http_version
+ attr_accessor :filename
+ attr_accessor :keep_alive
+ attr_reader :config, :sent_size
+
+ def initialize(config)
+ @config = config
+ @buffer_size = config[:OutputBufferSize]
+ @logger = config[:Logger]
+ @header = Hash.new
+ @status = HTTPStatus::RC_OK
+ @reason_phrase = nil
+ @http_version = HTTPVersion::convert(@config[:HTTPVersion])
+ @body = ''
+ @keep_alive = true
+ @cookies = []
+ @request_method = nil
+ @request_uri = nil
+ @request_http_version = @http_version # temporary
+ @chunked = false
+ @filename = nil
+ @sent_size = 0
+ end
+
+ def status_line
+ "HTTP/#@http_version #@status #@reason_phrase #{CRLF}"
+ end
+
+ def status=(status)
+ @status = status
+ @reason_phrase = HTTPStatus::reason_phrase(status)
+ end
+
+ def [](field)
+ @header[field.downcase]
+ end
+
+ def []=(field, value)
+ @header[field.downcase] = value.to_s
+ end
+
+ def content_length
+ if len = self['content-length']
+ return Integer(len)
+ end
+ end
+
+ def content_length=(len)
+ self['content-length'] = len.to_s
+ end
+
+ def content_type
+ self['content-type']
+ end
+
+ def content_type=(type)
+ self['content-type'] = type
+ end
+
+ def each
+ @header.each{|k, v| yield(k, v) }
+ end
+
+ def chunked?
+ @chunked
+ end
+
+ def chunked=(val)
+ @chunked = val ? true : false
+ end
+
+ def keep_alive?
+ @keep_alive
+ end
+
+ def send_response(socket)
+ begin
+ setup_header()
+ send_header(socket)
+ send_body(socket)
+ rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ENOTCONN => ex
+ @logger.debug(ex)
+ @keep_alive = false
+ rescue Exception => ex
+ @logger.error(ex)
+ @keep_alive = false
+ end
+ end
+
+ def setup_header()
+ @reason_phrase ||= HTTPStatus::reason_phrase(@status)
+ @header['server'] ||= @config[:ServerSoftware]
+ @header['date'] ||= Time.now.httpdate
+
+ # HTTP/0.9 features
+ if @request_http_version < "1.0"
+ @http_version = HTTPVersion.new("0.9")
+ @keep_alive = false
+ end
+
+ # HTTP/1.0 features
+ if @request_http_version < "1.1"
+ if chunked?
+ @chunked = false
+ ver = @request_http_version.to_s
+ msg = "chunked is set for an HTTP/#{ver} request. (ignored)"
+ @logger.warn(msg)
+ end
+ end
+
+ # Determine the message length (RFC2616 -- 4.4 Message Length)
+ if @status == 304 || @status == 204 || HTTPStatus::info?(@status)
+ @header.delete('content-length')
+ @body = ""
+ elsif chunked?
+ @header["transfer-encoding"] = "chunked"
+ @header.delete('content-length')
+ elsif %r{^multipart/byteranges} =~ @header['content-type']
+ @header.delete('content-length')
+ elsif @header['content-length'].nil?
+ unless @body.is_a?(IO)
+ @header['content-length'] = @body ? @body.bytesize : 0
+ end
+ end
+
+ # Keep-Alive connection.
+ if @header['connection'] == "close"
+ @keep_alive = false
+ elsif keep_alive?
+ if chunked? || @header['content-length']
+ @header['connection'] = "Keep-Alive"
+ end
+ else
+ @header['connection'] = "close"
+ end
+
+ # Location is a single absoluteURI.
+ if location = @header['location']
+ if @request_uri
+ @header['location'] = @request_uri.merge(location)
+ end
+ end
+ end
+
+ def send_header(socket)
+ if @http_version.major > 0
+ data = status_line()
+ @header.each{|key, value|
+ tmp = key.gsub(/\bwww|^te$|\b\w/){ $&.upcase }
+ data << "#{tmp}: #{value}" << CRLF
+ }
+ @cookies.each{|cookie|
+ data << "Set-Cookie: " << cookie.to_s << CRLF
+ }
+ data << CRLF
+ _write_data(socket, data)
+ end
+ end
+
+ def send_body(socket)
+ case @body
+ when IO then send_body_io(socket)
+ else send_body_string(socket)
+ end
+ end
+
+ def to_s
+ ret = ""
+ send_response(ret)
+ ret
+ end
+
+ def set_redirect(status, url)
+ @body = "<HTML><A HREF=\"#{url.to_s}\">#{url.to_s}</A>.</HTML>\n"
+ @header['location'] = url.to_s
+ raise status
+ end
+
+ def set_error(ex, backtrace=false)
+ case ex
+ when HTTPStatus::Status
+ @keep_alive = false if HTTPStatus::error?(ex.code)
+ self.status = ex.code
+ else
+ @keep_alive = false
+ self.status = HTTPStatus::RC_INTERNAL_SERVER_ERROR
+ end
+ @header['content-type'] = "text/html"
+
+ if respond_to?(:create_error_page)
+ create_error_page()
+ return
+ end
+
+ if @request_uri
+ host, port = @request_uri.host, @request_uri.port
+ else
+ host, port = @config[:ServerName], @config[:Port]
+ end
+
+ @body = ''
+ @body << <<-_end_of_html_
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
+<HTML>
+ <HEAD><TITLE>#{HTMLUtils::escape(@reason_phrase)}</TITLE></HEAD>
+ <BODY>
+ <H1>#{HTMLUtils::escape(@reason_phrase)}</H1>
+ #{HTMLUtils::escape(ex.message)}
+ <HR>
+ _end_of_html_
+
+ if backtrace && $DEBUG
+ @body << "backtrace of `#{HTMLUtils::escape(ex.class.to_s)}' "
+ @body << "#{HTMLUtils::escape(ex.message)}"
+ @body << "<PRE>"
+ ex.backtrace.each{|line| @body << "\t#{line}\n"}
+ @body << "</PRE><HR>"
+ end
+
+ @body << <<-_end_of_html_
+ <ADDRESS>
+ #{HTMLUtils::escape(@config[:ServerSoftware])} at
+ #{host}:#{port}
+ </ADDRESS>
+ </BODY>
+</HTML>
+ _end_of_html_
+ end
+
+ private
+
+ def send_body_io(socket)
+ begin
+ if @request_method == "HEAD"
+ # do nothing
+ elsif chunked?
+ while buf = @body.read(@buffer_size)
+ next if buf.empty?
+ data = ""
+ data << format("%x", buf.bytesize) << CRLF
+ data << buf << CRLF
+ _write_data(socket, data)
+ @sent_size += buf.bytesize
+ end
+ _write_data(socket, "0#{CRLF}#{CRLF}")
+ else
+ size = @header['content-length'].to_i
+ _send_file(socket, @body, 0, size)
+ @sent_size = size
+ end
+ ensure
+ @body.close
+ end
+ end
+
+ def send_body_string(socket)
+ if @request_method == "HEAD"
+ # do nothing
+ elsif chunked?
+ remain = body ? @body.bytesize : 0
+ while buf = @body[@sent_size, @buffer_size]
+ break if buf.empty?
+ data = ""
+ data << format("%x", buf.bytesize) << CRLF
+ data << buf << CRLF
+ _write_data(socket, data)
+ @sent_size += buf.bytesize
+ end
+ _write_data(socket, "0#{CRLF}#{CRLF}")
+ else
+ if @body && @body.bytesize > 0
+ _write_data(socket, @body)
+ @sent_size = @body.bytesize
+ end
+ end
+ end
+
+ def _send_file(output, input, offset, size)
+ while offset > 0
+ sz = @buffer_size < size ? @buffer_size : size
+ buf = input.read(sz)
+ offset -= buf.bytesize
+ end
+
+ if size == 0
+ while buf = input.read(@buffer_size)
+ _write_data(output, buf)
+ end
+ else
+ while size > 0
+ sz = @buffer_size < size ? @buffer_size : size
+ buf = input.read(sz)
+ _write_data(output, buf)
+ size -= buf.bytesize
+ end
+ end
+ end
+
+ def _write_data(socket, data)
+ socket << data
+ end
+ end
+end
diff --git a/ruby/lib/webrick/https.rb b/ruby/lib/webrick/https.rb
new file mode 100644
index 0000000..81b65ce
--- /dev/null
+++ b/ruby/lib/webrick/https.rb
@@ -0,0 +1,63 @@
+#
+# https.rb -- SSL/TLS enhancement for HTTPServer
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2001 GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: https.rb,v 1.15 2003/07/22 19:20:42 gotoyuzo Exp $
+
+require 'webrick/ssl'
+
+module WEBrick
+ module Config
+ HTTP.update(SSL)
+ end
+
+ class HTTPRequest
+ attr_reader :cipher, :server_cert, :client_cert
+
+ alias orig_parse parse
+
+ def parse(socket=nil)
+ if socket.respond_to?(:cert)
+ @server_cert = socket.cert || @config[:SSLCertificate]
+ @client_cert = socket.peer_cert
+ @client_cert_chain = socket.peer_cert_chain
+ @cipher = socket.cipher
+ end
+ orig_parse(socket)
+ end
+
+ alias orig_parse_uri parse_uri
+
+ def parse_uri(str, scheme="https")
+ if @server_cert
+ return orig_parse_uri(str, scheme)
+ end
+ return orig_parse_uri(str)
+ end
+
+ alias orig_meta_vars meta_vars
+
+ def meta_vars
+ meta = orig_meta_vars
+ if @server_cert
+ meta["HTTPS"] = "on"
+ meta["SSL_SERVER_CERT"] = @server_cert.to_pem
+ meta["SSL_CLIENT_CERT"] = @client_cert ? @client_cert.to_pem : ""
+ if @client_cert_chain
+ @client_cert_chain.each_with_index{|cert, i|
+ meta["SSL_CLIENT_CERT_CHAIN_#{i}"] = cert.to_pem
+ }
+ end
+ meta["SSL_CIPHER"] = @cipher[0]
+ meta["SSL_PROTOCOL"] = @cipher[1]
+ meta["SSL_CIPHER_USEKEYSIZE"] = @cipher[2].to_s
+ meta["SSL_CIPHER_ALGKEYSIZE"] = @cipher[3].to_s
+ end
+ meta
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpserver.rb b/ruby/lib/webrick/httpserver.rb
new file mode 100644
index 0000000..495be26
--- /dev/null
+++ b/ruby/lib/webrick/httpserver.rb
@@ -0,0 +1,217 @@
+#
+# httpserver.rb -- HTTPServer Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httpserver.rb,v 1.63 2002/10/01 17:16:32 gotoyuzo Exp $
+
+require 'webrick/server'
+require 'webrick/httputils'
+require 'webrick/httpstatus'
+require 'webrick/httprequest'
+require 'webrick/httpresponse'
+require 'webrick/httpservlet'
+require 'webrick/accesslog'
+
+module WEBrick
+ class HTTPServerError < ServerError; end
+
+ class HTTPServer < ::WEBrick::GenericServer
+ def initialize(config={}, default=Config::HTTP)
+ super(config, default)
+ @http_version = HTTPVersion::convert(@config[:HTTPVersion])
+
+ @mount_tab = MountTable.new
+ if @config[:DocumentRoot]
+ mount("/", HTTPServlet::FileHandler, @config[:DocumentRoot],
+ @config[:DocumentRootOptions])
+ end
+
+ unless @config[:AccessLog]
+ @config[:AccessLog] = [
+ [ $stderr, AccessLog::COMMON_LOG_FORMAT ],
+ [ $stderr, AccessLog::REFERER_LOG_FORMAT ]
+ ]
+ end
+
+ @virtual_hosts = Array.new
+ end
+
+ def run(sock)
+ while true
+ res = HTTPResponse.new(@config)
+ req = HTTPRequest.new(@config)
+ server = self
+ begin
+ timeout = @config[:RequestTimeout]
+ while timeout > 0
+ break if IO.select([sock], nil, nil, 0.5)
+ timeout = 0 if @status != :Running
+ timeout -= 0.5
+ end
+ raise HTTPStatus::EOFError if timeout <= 0
+ raise HTTPStatus::EOFError if sock.eof?
+ req.parse(sock)
+ res.request_method = req.request_method
+ res.request_uri = req.request_uri
+ res.request_http_version = req.http_version
+ res.keep_alive = req.keep_alive?
+ server = lookup_server(req) || self
+ if callback = server[:RequestCallback]
+ callback.call(req, res)
+ elsif callback = server[:RequestHandler]
+ msg = ":RequestHandler is deprecated, please use :RequestCallback"
+ @logger.warn(msg)
+ callback.call(req, res)
+ end
+ server.service(req, res)
+ rescue HTTPStatus::EOFError, HTTPStatus::RequestTimeout => ex
+ res.set_error(ex)
+ rescue HTTPStatus::Error => ex
+ @logger.error(ex.message)
+ res.set_error(ex)
+ rescue HTTPStatus::Status => ex
+ res.status = ex.code
+ rescue StandardError => ex
+ @logger.error(ex)
+ res.set_error(ex, true)
+ ensure
+ if req.request_line
+ if req.keep_alive? && res.keep_alive?
+ req.fixup()
+ end
+ res.send_response(sock)
+ server.access_log(@config, req, res)
+ end
+ end
+ break if @http_version < "1.1"
+ break unless req.keep_alive?
+ break unless res.keep_alive?
+ end
+ end
+
+ def service(req, res)
+ if req.unparsed_uri == "*"
+ if req.request_method == "OPTIONS"
+ do_OPTIONS(req, res)
+ raise HTTPStatus::OK
+ end
+ raise HTTPStatus::NotFound, "`#{req.unparsed_uri}' not found."
+ end
+
+ servlet, options, script_name, path_info = search_servlet(req.path)
+ raise HTTPStatus::NotFound, "`#{req.path}' not found." unless servlet
+ req.script_name = script_name
+ req.path_info = path_info
+ si = servlet.get_instance(self, *options)
+ @logger.debug(format("%s is invoked.", si.class.name))
+ si.service(req, res)
+ end
+
+ def do_OPTIONS(req, res)
+ res["allow"] = "GET,HEAD,POST,OPTIONS"
+ end
+
+ def mount(dir, servlet, *options)
+ @logger.debug(sprintf("%s is mounted on %s.", servlet.inspect, dir))
+ @mount_tab[dir] = [ servlet, options ]
+ end
+
+ def mount_proc(dir, proc=nil, &block)
+ proc ||= block
+ raise HTTPServerError, "must pass a proc or block" unless proc
+ mount(dir, HTTPServlet::ProcHandler.new(proc))
+ end
+
+ def unmount(dir)
+ @logger.debug(sprintf("unmount %s.", dir))
+ @mount_tab.delete(dir)
+ end
+ alias umount unmount
+
+ def search_servlet(path)
+ script_name, path_info = @mount_tab.scan(path)
+ servlet, options = @mount_tab[script_name]
+ if servlet
+ [ servlet, options, script_name, path_info ]
+ end
+ end
+
+ def virtual_host(server)
+ @virtual_hosts << server
+ @virtual_hosts = @virtual_hosts.sort_by{|s|
+ num = 0
+ num -= 4 if s[:BindAddress]
+ num -= 2 if s[:Port]
+ num -= 1 if s[:ServerName]
+ num
+ }
+ end
+
+ def lookup_server(req)
+ @virtual_hosts.find{|s|
+ (s[:BindAddress].nil? || req.addr[3] == s[:BindAddress]) &&
+ (s[:Port].nil? || req.port == s[:Port]) &&
+ ((s[:ServerName].nil? || req.host == s[:ServerName]) ||
+ (!s[:ServerAlias].nil? && s[:ServerAlias].find{|h| h === req.host}))
+ }
+ end
+
+ def access_log(config, req, res)
+ param = AccessLog::setup_params(config, req, res)
+ @config[:AccessLog].each{|logger, fmt|
+ logger << AccessLog::format(fmt+"\n", param)
+ }
+ end
+
+ class MountTable
+ def initialize
+ @tab = Hash.new
+ compile
+ end
+
+ def [](dir)
+ dir = normalize(dir)
+ @tab[dir]
+ end
+
+ def []=(dir, val)
+ dir = normalize(dir)
+ @tab[dir] = val
+ compile
+ val
+ end
+
+ def delete(dir)
+ dir = normalize(dir)
+ res = @tab.delete(dir)
+ compile
+ res
+ end
+
+ def scan(path)
+ @scanner =~ path
+ [ $&, $' ]
+ end
+
+ private
+
+ def compile
+ k = @tab.keys
+ k.sort!
+ k.reverse!
+ k.collect!{|path| Regexp.escape(path) }
+ @scanner = Regexp.new("^(" + k.join("|") +")(?=/|$)")
+ end
+
+ def normalize(dir)
+ ret = dir ? dir.dup : ""
+ ret.sub!(%r|/+$|, "")
+ ret
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpservlet.rb b/ruby/lib/webrick/httpservlet.rb
new file mode 100644
index 0000000..ac7c022
--- /dev/null
+++ b/ruby/lib/webrick/httpservlet.rb
@@ -0,0 +1,22 @@
+#
+# httpservlet.rb -- HTTPServlet Utility File
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httpservlet.rb,v 1.21 2003/02/23 12:24:46 gotoyuzo Exp $
+
+require 'webrick/httpservlet/abstract'
+require 'webrick/httpservlet/filehandler'
+require 'webrick/httpservlet/cgihandler'
+require 'webrick/httpservlet/erbhandler'
+require 'webrick/httpservlet/prochandler'
+
+module WEBrick
+ module HTTPServlet
+ FileHandler.add_handler("cgi", CGIHandler)
+ FileHandler.add_handler("rhtml", ERBHandler)
+ end
+end
diff --git a/ruby/lib/webrick/httpservlet/abstract.rb b/ruby/lib/webrick/httpservlet/abstract.rb
new file mode 100644
index 0000000..f8bf14a
--- /dev/null
+++ b/ruby/lib/webrick/httpservlet/abstract.rb
@@ -0,0 +1,70 @@
+#
+# httpservlet.rb -- HTTPServlet Module
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: abstract.rb,v 1.24 2003/07/11 11:16:46 gotoyuzo Exp $
+
+require 'thread'
+
+require 'webrick/htmlutils'
+require 'webrick/httputils'
+require 'webrick/httpstatus'
+
+module WEBrick
+ module HTTPServlet
+ class HTTPServletError < StandardError; end
+
+ class AbstractServlet
+ def self.get_instance(config, *options)
+ self.new(config, *options)
+ end
+
+ def initialize(server, *options)
+ @server = @config = server
+ @logger = @server[:Logger]
+ @options = options
+ end
+
+ def service(req, res)
+ method_name = "do_" + req.request_method.gsub(/-/, "_")
+ if respond_to?(method_name)
+ __send__(method_name, req, res)
+ else
+ raise HTTPStatus::MethodNotAllowed,
+ "unsupported method `#{req.request_method}'."
+ end
+ end
+
+ def do_GET(req, res)
+ raise HTTPStatus::NotFound, "not found."
+ end
+
+ def do_HEAD(req, res)
+ do_GET(req, res)
+ end
+
+ def do_OPTIONS(req, res)
+ m = self.methods.grep(/\Ado_([A-Z]+)\z/) {$1}
+ m.sort!
+ res["allow"] = m.join(",")
+ end
+
+ private
+
+ def redirect_to_directory_uri(req, res)
+ if req.path[-1] != ?/
+ location = WEBrick::HTTPUtils.escape_path(req.path + "/")
+ if req.query_string && req.query_string.bytesize > 0
+ location << "?" << req.query_string
+ end
+ res.set_redirect(HTTPStatus::MovedPermanently, location)
+ end
+ end
+ end
+
+ end
+end
diff --git a/ruby/lib/webrick/httpservlet/cgi_runner.rb b/ruby/lib/webrick/httpservlet/cgi_runner.rb
new file mode 100644
index 0000000..dd7325d
--- /dev/null
+++ b/ruby/lib/webrick/httpservlet/cgi_runner.rb
@@ -0,0 +1,47 @@
+#
+# cgi_runner.rb -- CGI launcher.
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000 TAKAHASHI Masayoshi, GOTOU YUUZOU
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: cgi_runner.rb,v 1.9 2002/09/25 11:33:15 gotoyuzo Exp $
+
+def sysread(io, size)
+ buf = ""
+ while size > 0
+ tmp = io.sysread(size)
+ buf << tmp
+ size -= tmp.bytesize
+ end
+ return buf
+end
+
+STDIN.binmode
+
+buf = ""
+len = sysread(STDIN, 8).to_i
+out = sysread(STDIN, len)
+STDOUT.reopen(open(out, "w"))
+
+len = sysread(STDIN, 8).to_i
+err = sysread(STDIN, len)
+STDERR.reopen(open(err, "w"))
+
+len = sysread(STDIN, 8).to_i
+dump = sysread(STDIN, len)
+hash = Marshal.restore(dump)
+ENV.keys.each{|name| ENV.delete(name) }
+hash.each{|k, v| ENV[k] = v if v }
+
+dir = File::dirname(ENV["SCRIPT_FILENAME"])
+Dir::chdir dir
+
+if interpreter = ARGV[0]
+ argv = ARGV.dup
+ argv << ENV["SCRIPT_FILENAME"]
+ exec(*argv)
+ # NOTREACHED
+end
+exec ENV["SCRIPT_FILENAME"]
diff --git a/ruby/lib/webrick/httpservlet/cgihandler.rb b/ruby/lib/webrick/httpservlet/cgihandler.rb
new file mode 100644
index 0000000..f504f4d
--- /dev/null
+++ b/ruby/lib/webrick/httpservlet/cgihandler.rb
@@ -0,0 +1,110 @@
+#
+# cgihandler.rb -- CGIHandler Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: cgihandler.rb,v 1.27 2003/03/21 19:56:01 gotoyuzo Exp $
+
+require 'rbconfig'
+require 'tempfile'
+require 'webrick/config'
+require 'webrick/httpservlet/abstract'
+
+module WEBrick
+ module HTTPServlet
+
+ class CGIHandler < AbstractServlet
+ Ruby = File::join(RbConfig::CONFIG['bindir'],
+ RbConfig::CONFIG['ruby_install_name'])
+ Ruby << RbConfig::CONFIG['EXEEXT']
+ CGIRunner = "\"#{Ruby}\" \"#{WEBrick::Config::LIBDIR}/httpservlet/cgi_runner.rb\""
+
+ def initialize(server, name)
+ super(server, name)
+ @script_filename = name
+ @tempdir = server[:TempDir]
+ @cgicmd = "#{CGIRunner} #{server[:CGIInterpreter]}"
+ end
+
+ def do_GET(req, res)
+ data = nil
+ status = -1
+
+ cgi_in = IO::popen(@cgicmd, "wb")
+ cgi_out = Tempfile.new("webrick.cgiout.", @tempdir)
+ cgi_out.set_encoding("ASCII-8BIT")
+ cgi_err = Tempfile.new("webrick.cgierr.", @tempdir)
+ cgi_err.set_encoding("ASCII-8BIT")
+ begin
+ cgi_in.sync = true
+ meta = req.meta_vars
+ meta["SCRIPT_FILENAME"] = @script_filename
+ meta["PATH"] = @config[:CGIPathEnv]
+ if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
+ meta["SystemRoot"] = ENV["SystemRoot"]
+ end
+ dump = Marshal.dump(meta)
+
+ cgi_in.write("%8d" % cgi_out.path.bytesize)
+ cgi_in.write(cgi_out.path)
+ cgi_in.write("%8d" % cgi_err.path.bytesize)
+ cgi_in.write(cgi_err.path)
+ cgi_in.write("%8d" % dump.bytesize)
+ cgi_in.write(dump)
+
+ if req.body and req.body.bytesize > 0
+ cgi_in.write(req.body)
+ end
+ ensure
+ cgi_in.close
+ status = $?.exitstatus
+ sleep 0.1 if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
+ data = cgi_out.read
+ cgi_out.close(true)
+ if errmsg = cgi_err.read
+ if errmsg.bytesize > 0
+ @logger.error("CGIHandler: #{@script_filename}:\n" + errmsg)
+ end
+ end
+ cgi_err.close(true)
+ end
+
+ if status != 0
+ @logger.error("CGIHandler: #{@script_filename} exit with #{status}")
+ end
+
+ data = "" unless data
+ raw_header, body = data.split(/^[\xd\xa]+/, 2)
+ raise HTTPStatus::InternalServerError,
+ "Premature end of script headers: #{@script_filename}" if body.nil?
+
+ begin
+ header = HTTPUtils::parse_header(raw_header)
+ if /^(\d+)/ =~ header['status'][0]
+ res.status = $1.to_i
+ header.delete('status')
+ end
+ if header.has_key?('location')
+ # RFC 3875 6.2.3, 6.2.4
+ res.status = 302 unless (300...400) === res.status
+ end
+ if header.has_key?('set-cookie')
+ header['set-cookie'].each{|k|
+ res.cookies << Cookie.parse_set_cookie(k)
+ }
+ header.delete('set-cookie')
+ end
+ header.each{|key, val| res[key] = val.join(", ") }
+ rescue => ex
+ raise HTTPStatus::InternalServerError, ex.message
+ end
+ res.body = body
+ end
+ alias do_POST do_GET
+ end
+
+ end
+end
diff --git a/ruby/lib/webrick/httpservlet/erbhandler.rb b/ruby/lib/webrick/httpservlet/erbhandler.rb
new file mode 100644
index 0000000..4979219
--- /dev/null
+++ b/ruby/lib/webrick/httpservlet/erbhandler.rb
@@ -0,0 +1,54 @@
+#
+# erbhandler.rb -- ERBHandler Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: erbhandler.rb,v 1.25 2003/02/24 19:25:31 gotoyuzo Exp $
+
+require 'webrick/httpservlet/abstract.rb'
+
+require 'erb'
+
+module WEBrick
+ module HTTPServlet
+
+ class ERBHandler < AbstractServlet
+ def initialize(server, name)
+ super(server, name)
+ @script_filename = name
+ end
+
+ def do_GET(req, res)
+ unless defined?(ERB)
+ @logger.warn "#{self.class}: ERB not defined."
+ raise HTTPStatus::Forbidden, "ERBHandler cannot work."
+ end
+ begin
+ data = open(@script_filename){|io| io.read }
+ res.body = evaluate(ERB.new(data), req, res)
+ res['content-type'] =
+ HTTPUtils::mime_type(@script_filename, @config[:MimeTypes])
+ rescue StandardError => ex
+ raise
+ rescue Exception => ex
+ @logger.error(ex)
+ raise HTTPStatus::InternalServerError, ex.message
+ end
+ end
+
+ alias do_POST do_GET
+
+ private
+ def evaluate(erb, servlet_request, servlet_response)
+ Module.new.module_eval{
+ meta_vars = servlet_request.meta_vars
+ query = servlet_request.query
+ erb.result(binding)
+ }
+ end
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpservlet/filehandler.rb b/ruby/lib/webrick/httpservlet/filehandler.rb
new file mode 100644
index 0000000..f1cc88b
--- /dev/null
+++ b/ruby/lib/webrick/httpservlet/filehandler.rb
@@ -0,0 +1,435 @@
+#
+# filehandler.rb -- FileHandler Module
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: filehandler.rb,v 1.44 2003/06/07 01:34:51 gotoyuzo Exp $
+
+require 'thread'
+require 'time'
+
+require 'webrick/htmlutils'
+require 'webrick/httputils'
+require 'webrick/httpstatus'
+
+module WEBrick
+ module HTTPServlet
+
+ class DefaultFileHandler < AbstractServlet
+ def initialize(server, local_path)
+ super(server, local_path)
+ @local_path = local_path
+ end
+
+ def do_GET(req, res)
+ st = File::stat(@local_path)
+ mtime = st.mtime
+ res['etag'] = sprintf("%x-%x-%x", st.ino, st.size, st.mtime.to_i)
+
+ if not_modified?(req, res, mtime, res['etag'])
+ res.body = ''
+ raise HTTPStatus::NotModified
+ elsif req['range']
+ make_partial_content(req, res, @local_path, st.size)
+ raise HTTPStatus::PartialContent
+ else
+ mtype = HTTPUtils::mime_type(@local_path, @config[:MimeTypes])
+ res['content-type'] = mtype
+ res['content-length'] = st.size
+ res['last-modified'] = mtime.httpdate
+ res.body = open(@local_path, "rb")
+ end
+ end
+
+ def not_modified?(req, res, mtime, etag)
+ if ir = req['if-range']
+ begin
+ if Time.httpdate(ir) >= mtime
+ return true
+ end
+ rescue
+ if HTTPUtils::split_header_value(ir).member?(res['etag'])
+ return true
+ end
+ end
+ end
+
+ if (ims = req['if-modified-since']) && Time.parse(ims) >= mtime
+ return true
+ end
+
+ if (inm = req['if-none-match']) &&
+ HTTPUtils::split_header_value(inm).member?(res['etag'])
+ return true
+ end
+
+ return false
+ end
+
+ def make_partial_content(req, res, filename, filesize)
+ mtype = HTTPUtils::mime_type(filename, @config[:MimeTypes])
+ unless ranges = HTTPUtils::parse_range_header(req['range'])
+ raise HTTPStatus::BadRequest,
+ "Unrecognized range-spec: \"#{req['range']}\""
+ end
+ open(filename, "rb"){|io|
+ if ranges.size > 1
+ time = Time.now
+ boundary = "#{time.sec}_#{time.usec}_#{Process::pid}"
+ body = ''
+ ranges.each{|range|
+ first, last = prepare_range(range, filesize)
+ next if first < 0
+ io.pos = first
+ content = io.read(last-first+1)
+ body << "--" << boundary << CRLF
+ body << "Content-Type: #{mtype}" << CRLF
+ body << "Content-Range: #{first}-#{last}/#{filesize}" << CRLF
+ body << CRLF
+ body << content
+ body << CRLF
+ }
+ raise HTTPStatus::RequestRangeNotSatisfiable if body.empty?
+ body << "--" << boundary << "--" << CRLF
+ res["content-type"] = "multipart/byteranges; boundary=#{boundary}"
+ res.body = body
+ elsif range = ranges[0]
+ first, last = prepare_range(range, filesize)
+ raise HTTPStatus::RequestRangeNotSatisfiable if first < 0
+ if last == filesize - 1
+ content = io.dup
+ content.pos = first
+ else
+ io.pos = first
+ content = io.read(last-first+1)
+ end
+ res['content-type'] = mtype
+ res['content-range'] = "#{first}-#{last}/#{filesize}"
+ res['content-length'] = last - first + 1
+ res.body = content
+ else
+ raise HTTPStatus::BadRequest
+ end
+ }
+ end
+
+ def prepare_range(range, filesize)
+ first = range.first < 0 ? filesize + range.first : range.first
+ return -1, -1 if first < 0 || first >= filesize
+ last = range.last < 0 ? filesize + range.last : range.last
+ last = filesize - 1 if last >= filesize
+ return first, last
+ end
+ end
+
+ class FileHandler < AbstractServlet
+ HandlerTable = Hash.new
+
+ def self.add_handler(suffix, handler)
+ HandlerTable[suffix] = handler
+ end
+
+ def self.remove_handler(suffix)
+ HandlerTable.delete(suffix)
+ end
+
+ def initialize(server, root, options={}, default=Config::FileHandler)
+ @config = server.config
+ @logger = @config[:Logger]
+ @root = File.expand_path(root)
+ if options == true || options == false
+ options = { :FancyIndexing => options }
+ end
+ @options = default.dup.update(options)
+ end
+
+ def service(req, res)
+ # if this class is mounted on "/" and /~username is requested.
+ # we're going to override path informations before invoking service.
+ if defined?(Etc) && @options[:UserDir] && req.script_name.empty?
+ if %r|^(/~([^/]+))| =~ req.path_info
+ script_name, user = $1, $2
+ path_info = $'
+ begin
+ passwd = Etc::getpwnam(user)
+ @root = File::join(passwd.dir, @options[:UserDir])
+ req.script_name = script_name
+ req.path_info = path_info
+ rescue
+ @logger.debug "#{self.class}#do_GET: getpwnam(#{user}) failed"
+ end
+ end
+ end
+ prevent_directory_traversal(req, res)
+ super(req, res)
+ end
+
+ def do_GET(req, res)
+ unless exec_handler(req, res)
+ set_dir_list(req, res)
+ end
+ end
+
+ def do_POST(req, res)
+ unless exec_handler(req, res)
+ raise HTTPStatus::NotFound, "`#{req.path}' not found."
+ end
+ end
+
+ def do_OPTIONS(req, res)
+ unless exec_handler(req, res)
+ super(req, res)
+ end
+ end
+
+ # ToDo
+ # RFC2518: HTTP Extensions for Distributed Authoring -- WEBDAV
+ #
+ # PROPFIND PROPPATCH MKCOL DELETE PUT COPY MOVE
+ # LOCK UNLOCK
+
+ # RFC3253: Versioning Extensions to WebDAV
+ # (Web Distributed Authoring and Versioning)
+ #
+ # VERSION-CONTROL REPORT CHECKOUT CHECK_IN UNCHECKOUT
+ # MKWORKSPACE UPDATE LABEL MERGE ACTIVITY
+
+ private
+
+ def trailing_pathsep?(path)
+ # check for trailing path separator:
+ # File.dirname("/aaaa/bbbb/") #=> "/aaaa")
+ # File.dirname("/aaaa/bbbb/x") #=> "/aaaa/bbbb")
+ # File.dirname("/aaaa/bbbb") #=> "/aaaa")
+ # File.dirname("/aaaa/bbbbx") #=> "/aaaa")
+ return File.dirname(path) != File.dirname(path+"x")
+ end
+
+ def prevent_directory_traversal(req, res)
+ # Preventing directory traversal on Windows platforms;
+ # Backslashes (0x5c) in path_info are not interpreted as special
+ # character in URI notation. So the value of path_info should be
+ # normalize before accessing to the filesystem.
+
+ if trailing_pathsep?(req.path_info)
+ # File.expand_path removes the trailing path separator.
+ # Adding a character is a workaround to save it.
+ # File.expand_path("/aaa/") #=> "/aaa"
+ # File.expand_path("/aaa/" + "x") #=> "/aaa/x"
+ expanded = File.expand_path(req.path_info + "x")
+ expanded.chop! # remove trailing "x"
+ else
+ expanded = File.expand_path(req.path_info)
+ end
+ req.path_info = expanded
+ end
+
+ def exec_handler(req, res)
+ raise HTTPStatus::NotFound, "`#{req.path}' not found" unless @root
+ if set_filename(req, res)
+ handler = get_handler(req, res)
+ call_callback(:HandlerCallback, req, res)
+ h = handler.get_instance(@config, res.filename)
+ h.service(req, res)
+ return true
+ end
+ call_callback(:HandlerCallback, req, res)
+ return false
+ end
+
+ def get_handler(req, res)
+ suffix1 = (/\.(\w+)\z/ =~ res.filename) && $1.downcase
+ if /\.(\w+)\.([\w\-]+)\z/ =~ res.filename
+ if @options[:AcceptableLanguages].include?($2.downcase)
+ suffix2 = $1.downcase
+ end
+ end
+ handler_table = @options[:HandlerTable]
+ return handler_table[suffix1] || handler_table[suffix2] ||
+ HandlerTable[suffix1] || HandlerTable[suffix2] ||
+ DefaultFileHandler
+ end
+
+ def set_filename(req, res)
+ res.filename = @root.dup
+ path_info = req.path_info.scan(%r|/[^/]*|)
+
+ path_info.unshift("") # dummy for checking @root dir
+ while base = path_info.first
+ break if base == "/"
+ break unless File.directory?(File.expand_path(res.filename + base))
+ shift_path_info(req, res, path_info)
+ call_callback(:DirectoryCallback, req, res)
+ end
+
+ if base = path_info.first
+ if base == "/"
+ if file = search_index_file(req, res)
+ shift_path_info(req, res, path_info, file)
+ call_callback(:FileCallback, req, res)
+ return true
+ end
+ shift_path_info(req, res, path_info)
+ elsif file = search_file(req, res, base)
+ shift_path_info(req, res, path_info, file)
+ call_callback(:FileCallback, req, res)
+ return true
+ else
+ raise HTTPStatus::NotFound, "`#{req.path}' not found."
+ end
+ end
+
+ return false
+ end
+
+ def check_filename(req, res, name)
+ if nondisclosure_name?(name) || windows_ambiguous_name?(name)
+ @logger.warn("the request refers nondisclosure name `#{name}'.")
+ raise HTTPStatus::NotFound, "`#{req.path}' not found."
+ end
+ end
+
+ def shift_path_info(req, res, path_info, base=nil)
+ tmp = path_info.shift
+ base = base || tmp
+ req.path_info = path_info.join
+ req.script_name << base
+ res.filename = File.expand_path(res.filename + base)
+ check_filename(req, res, File.basename(res.filename))
+ end
+
+ def search_index_file(req, res)
+ @config[:DirectoryIndex].each{|index|
+ if file = search_file(req, res, "/"+index)
+ return file
+ end
+ }
+ return nil
+ end
+
+ def search_file(req, res, basename)
+ langs = @options[:AcceptableLanguages]
+ path = res.filename + basename
+ if File.file?(path)
+ return basename
+ elsif langs.size > 0
+ req.accept_language.each{|lang|
+ path_with_lang = path + ".#{lang}"
+ if langs.member?(lang) && File.file?(path_with_lang)
+ return basename + ".#{lang}"
+ end
+ }
+ (langs - req.accept_language).each{|lang|
+ path_with_lang = path + ".#{lang}"
+ if File.file?(path_with_lang)
+ return basename + ".#{lang}"
+ end
+ }
+ end
+ return nil
+ end
+
+ def call_callback(callback_name, req, res)
+ if cb = @options[callback_name]
+ cb.call(req, res)
+ end
+ end
+
+ def windows_ambiguous_name?(name)
+ return true if /[. ]+\z/ =~ name
+ return true if /::\$DATA\z/ =~ name
+ return false
+ end
+
+ def nondisclosure_name?(name)
+ @options[:NondisclosureName].each{|pattern|
+ if File.fnmatch(pattern, name, File::FNM_CASEFOLD)
+ return true
+ end
+ }
+ return false
+ end
+
+ def set_dir_list(req, res)
+ redirect_to_directory_uri(req, res)
+ unless @options[:FancyIndexing]
+ raise HTTPStatus::Forbidden, "no access permission to `#{req.path}'"
+ end
+ local_path = res.filename
+ list = Dir::entries(local_path).collect{|name|
+ next if name == "." || name == ".."
+ next if nondisclosure_name?(name)
+ next if windows_ambiguous_name?(name)
+ st = (File::stat(File.join(local_path, name)) rescue nil)
+ if st.nil?
+ [ name, nil, -1 ]
+ elsif st.directory?
+ [ name + "/", st.mtime, -1 ]
+ else
+ [ name, st.mtime, st.size ]
+ end
+ }
+ list.compact!
+
+ if d0 = req.query["N"]; idx = 0
+ elsif d0 = req.query["M"]; idx = 1
+ elsif d0 = req.query["S"]; idx = 2
+ else d0 = "A" ; idx = 0
+ end
+ d1 = (d0 == "A") ? "D" : "A"
+
+ if d0 == "A"
+ list.sort!{|a,b| a[idx] <=> b[idx] }
+ else
+ list.sort!{|a,b| b[idx] <=> a[idx] }
+ end
+
+ res['content-type'] = "text/html"
+
+ res.body = <<-_end_of_html_
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<HTML>
+ <HEAD><TITLE>Index of #{HTMLUtils::escape(req.path)}</TITLE></HEAD>
+ <BODY>
+ <H1>Index of #{HTMLUtils::escape(req.path)}</H1>
+ _end_of_html_
+
+ res.body << "<PRE>\n"
+ res.body << " <A HREF=\"?N=#{d1}\">Name</A> "
+ res.body << "<A HREF=\"?M=#{d1}\">Last modified</A> "
+ res.body << "<A HREF=\"?S=#{d1}\">Size</A>\n"
+ res.body << "<HR>\n"
+
+ list.unshift [ "..", File::mtime(local_path+"/.."), -1 ]
+ list.each{ |name, time, size|
+ if name == ".."
+ dname = "Parent Directory"
+ elsif name.bytesize > 25
+ dname = name.sub(/^(.{23})(?:.*)/, '\1..')
+ else
+ dname = name
+ end
+ s = " <A HREF=\"#{HTTPUtils::escape(name)}\">#{dname}</A>"
+ s << " " * (30 - dname.bytesize)
+ s << (time ? time.strftime("%Y/%m/%d %H:%M ") : " " * 22)
+ s << (size >= 0 ? size.to_s : "-") << "\n"
+ res.body << s
+ }
+ res.body << "</PRE><HR>"
+
+ res.body << <<-_end_of_html_
+ <ADDRESS>
+ #{HTMLUtils::escape(@config[:ServerSoftware])}<BR>
+ at #{req.host}:#{req.port}
+ </ADDRESS>
+ </BODY>
+</HTML>
+ _end_of_html_
+ end
+
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpservlet/prochandler.rb b/ruby/lib/webrick/httpservlet/prochandler.rb
new file mode 100644
index 0000000..783cb27
--- /dev/null
+++ b/ruby/lib/webrick/httpservlet/prochandler.rb
@@ -0,0 +1,33 @@
+#
+# prochandler.rb -- ProcHandler Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: prochandler.rb,v 1.7 2002/09/21 12:23:42 gotoyuzo Exp $
+
+require 'webrick/httpservlet/abstract.rb'
+
+module WEBrick
+ module HTTPServlet
+
+ class ProcHandler < AbstractServlet
+ def get_instance(server, *options)
+ self
+ end
+
+ def initialize(proc)
+ @proc = proc
+ end
+
+ def do_GET(request, response)
+ @proc.call(request, response)
+ end
+
+ alias do_POST do_GET
+ end
+
+ end
+end
diff --git a/ruby/lib/webrick/httpstatus.rb b/ruby/lib/webrick/httpstatus.rb
new file mode 100644
index 0000000..f354b7d
--- /dev/null
+++ b/ruby/lib/webrick/httpstatus.rb
@@ -0,0 +1,132 @@
+#
+# httpstatus.rb -- HTTPStatus Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httpstatus.rb,v 1.11 2003/03/24 20:18:55 gotoyuzo Exp $
+
+module WEBrick
+
+ module HTTPStatus
+
+ class Status < StandardError
+ def initialize(*args)
+ args[0] = AccessLog.escape(args[0]) unless args.empty?
+ super(*args)
+ end
+ class << self
+ attr_reader :code, :reason_phrase
+ end
+ def code() self::class::code end
+ def reason_phrase() self::class::reason_phrase end
+ alias to_i code
+ end
+ class Info < Status; end
+ class Success < Status; end
+ class Redirect < Status; end
+ class Error < Status; end
+ class ClientError < Error; end
+ class ServerError < Error; end
+
+ class EOFError < StandardError; end
+
+ StatusMessage = {
+ 100 => 'Continue',
+ 101 => 'Switching Protocols',
+ 200 => 'OK',
+ 201 => 'Created',
+ 202 => 'Accepted',
+ 203 => 'Non-Authoritative Information',
+ 204 => 'No Content',
+ 205 => 'Reset Content',
+ 206 => 'Partial Content',
+ 300 => 'Multiple Choices',
+ 301 => 'Moved Permanently',
+ 302 => 'Found',
+ 303 => 'See Other',
+ 304 => 'Not Modified',
+ 305 => 'Use Proxy',
+ 307 => 'Temporary Redirect',
+ 400 => 'Bad Request',
+ 401 => 'Unauthorized',
+ 402 => 'Payment Required',
+ 403 => 'Forbidden',
+ 404 => 'Not Found',
+ 405 => 'Method Not Allowed',
+ 406 => 'Not Acceptable',
+ 407 => 'Proxy Authentication Required',
+ 408 => 'Request Timeout',
+ 409 => 'Conflict',
+ 410 => 'Gone',
+ 411 => 'Length Required',
+ 412 => 'Precondition Failed',
+ 413 => 'Request Entity Too Large',
+ 414 => 'Request-URI Too Large',
+ 415 => 'Unsupported Media Type',
+ 416 => 'Request Range Not Satisfiable',
+ 417 => 'Expectation Failed',
+ 500 => 'Internal Server Error',
+ 501 => 'Not Implemented',
+ 502 => 'Bad Gateway',
+ 503 => 'Service Unavailable',
+ 504 => 'Gateway Timeout',
+ 505 => 'HTTP Version Not Supported'
+ }
+
+ CodeToError = {}
+
+ StatusMessage.each{|code, message|
+ message.freeze
+ var_name = message.gsub(/[ \-]/,'_').upcase
+ err_name = message.gsub(/[ \-]/,'')
+
+ case code
+ when 100...200; parent = Info
+ when 200...300; parent = Success
+ when 300...400; parent = Redirect
+ when 400...500; parent = ClientError
+ when 500...600; parent = ServerError
+ end
+
+ const_set("RC_#{var_name}", code)
+ err_class = Class.new(parent)
+ err_class.instance_variable_set(:@code, code)
+ err_class.instance_variable_set(:@reason_phrase, message)
+ const_set(err_name, err_class)
+ CodeToError[code] = err_class
+ }
+
+ def reason_phrase(code)
+ StatusMessage[code.to_i]
+ end
+ def info?(code)
+ code.to_i >= 100 and code.to_i < 200
+ end
+ def success?(code)
+ code.to_i >= 200 and code.to_i < 300
+ end
+ def redirect?(code)
+ code.to_i >= 300 and code.to_i < 400
+ end
+ def error?(code)
+ code.to_i >= 400 and code.to_i < 600
+ end
+ def client_error?(code)
+ code.to_i >= 400 and code.to_i < 500
+ end
+ def server_error?(code)
+ code.to_i >= 500 and code.to_i < 600
+ end
+
+ def self.[](code)
+ CodeToError[code]
+ end
+
+ module_function :reason_phrase
+ module_function :info?, :success?, :redirect?, :error?
+ module_function :client_error?, :server_error?
+ end
+end
diff --git a/ruby/lib/webrick/httputils.rb b/ruby/lib/webrick/httputils.rb
new file mode 100644
index 0000000..e77910b
--- /dev/null
+++ b/ruby/lib/webrick/httputils.rb
@@ -0,0 +1,392 @@
+#
+# httputils.rb -- HTTPUtils Module
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httputils.rb,v 1.34 2003/06/05 21:34:08 gotoyuzo Exp $
+
+require 'socket'
+require 'tempfile'
+
+module WEBrick
+ CR = "\x0d"
+ LF = "\x0a"
+ CRLF = "\x0d\x0a"
+
+ module HTTPUtils
+
+ def normalize_path(path)
+ raise "abnormal path `#{path}'" if path[0] != ?/
+ ret = path.dup
+
+ ret.gsub!(%r{/+}o, '/') # // => /
+ while ret.sub!(%r'/\.(?:/|\Z)', '/'); end # /. => /
+ while ret.sub!(%r'/(?!\.\./)[^/]+/\.\.(?:/|\Z)', '/'); end # /foo/.. => /foo
+
+ raise "abnormal path `#{path}'" if %r{/\.\.(/|\Z)} =~ ret
+ ret
+ end
+ module_function :normalize_path
+
+ #####
+
+ DefaultMimeTypes = {
+ "ai" => "application/postscript",
+ "asc" => "text/plain",
+ "avi" => "video/x-msvideo",
+ "bin" => "application/octet-stream",
+ "bmp" => "image/bmp",
+ "class" => "application/octet-stream",
+ "cer" => "application/pkix-cert",
+ "crl" => "application/pkix-crl",
+ "crt" => "application/x-x509-ca-cert",
+ #"crl" => "application/x-pkcs7-crl",
+ "css" => "text/css",
+ "dms" => "application/octet-stream",
+ "doc" => "application/msword",
+ "dvi" => "application/x-dvi",
+ "eps" => "application/postscript",
+ "etx" => "text/x-setext",
+ "exe" => "application/octet-stream",
+ "gif" => "image/gif",
+ "htm" => "text/html",
+ "html" => "text/html",
+ "jpe" => "image/jpeg",
+ "jpeg" => "image/jpeg",
+ "jpg" => "image/jpeg",
+ "lha" => "application/octet-stream",
+ "lzh" => "application/octet-stream",
+ "mov" => "video/quicktime",
+ "mpe" => "video/mpeg",
+ "mpeg" => "video/mpeg",
+ "mpg" => "video/mpeg",
+ "pbm" => "image/x-portable-bitmap",
+ "pdf" => "application/pdf",
+ "pgm" => "image/x-portable-graymap",
+ "png" => "image/png",
+ "pnm" => "image/x-portable-anymap",
+ "ppm" => "image/x-portable-pixmap",
+ "ppt" => "application/vnd.ms-powerpoint",
+ "ps" => "application/postscript",
+ "qt" => "video/quicktime",
+ "ras" => "image/x-cmu-raster",
+ "rb" => "text/plain",
+ "rd" => "text/plain",
+ "rtf" => "application/rtf",
+ "sgm" => "text/sgml",
+ "sgml" => "text/sgml",
+ "tif" => "image/tiff",
+ "tiff" => "image/tiff",
+ "txt" => "text/plain",
+ "xbm" => "image/x-xbitmap",
+ "xhtml" => "text/html",
+ "xls" => "application/vnd.ms-excel",
+ "xml" => "text/xml",
+ "xpm" => "image/x-xpixmap",
+ "xwd" => "image/x-xwindowdump",
+ "zip" => "application/zip",
+ }
+
+ # Load Apache compatible mime.types file.
+ def load_mime_types(file)
+ open(file){ |io|
+ hash = Hash.new
+ io.each{ |line|
+ next if /^#/ =~ line
+ line.chomp!
+ mimetype, ext0 = line.split(/\s+/, 2)
+ next unless ext0
+ next if ext0.empty?
+ ext0.split(/\s+/).each{ |ext| hash[ext] = mimetype }
+ }
+ hash
+ }
+ end
+ module_function :load_mime_types
+
+ def mime_type(filename, mime_tab)
+ suffix1 = (/\.(\w+)$/ =~ filename && $1.downcase)
+ suffix2 = (/\.(\w+)\.[\w\-]+$/ =~ filename && $1.downcase)
+ mime_tab[suffix1] || mime_tab[suffix2] || "application/octet-stream"
+ end
+ module_function :mime_type
+
+ #####
+
+ def parse_header(raw)
+ header = Hash.new([].freeze)
+ field = nil
+ raw.each_line{|line|
+ case line
+ when /^([A-Za-z0-9!\#$%&'*+\-.^_`|~]+):\s*(.*?)\s*\z/om
+ field, value = $1, $2
+ field.downcase!
+ header[field] = [] unless header.has_key?(field)
+ header[field] << value
+ when /^\s+(.*?)\s*\z/om
+ value = $1
+ unless field
+ raise HTTPStatus::BadRequest, "bad header '#{line}'."
+ end
+ header[field][-1] << " " << value
+ else
+ raise HTTPStatus::BadRequest, "bad header '#{line}'."
+ end
+ }
+ header.each{|key, values|
+ values.each{|value|
+ value.strip!
+ value.gsub!(/\s+/, " ")
+ }
+ }
+ header
+ end
+ module_function :parse_header
+
+ def split_header_value(str)
+ str.scan(%r'\G((?:"(?:\\.|[^"])+?"|[^",]+)+)
+ (?:,\s*|\Z)'xn).flatten
+ end
+ module_function :split_header_value
+
+ def parse_range_header(ranges_specifier)
+ if /^bytes=(.*)/ =~ ranges_specifier
+ byte_range_set = split_header_value($1)
+ byte_range_set.collect{|range_spec|
+ case range_spec
+ when /^(\d+)-(\d+)/ then $1.to_i .. $2.to_i
+ when /^(\d+)-/ then $1.to_i .. -1
+ when /^-(\d+)/ then -($1.to_i) .. -1
+ else return nil
+ end
+ }
+ end
+ end
+ module_function :parse_range_header
+
+ def parse_qvalues(value)
+ tmp = []
+ if value
+ parts = value.split(/,\s*/)
+ parts.each {|part|
+ if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
+ val = m[1]
+ q = (m[2] or 1).to_f
+ tmp.push([val, q])
+ end
+ }
+ tmp = tmp.sort_by{|val, q| -q}
+ tmp.collect!{|val, q| val}
+ end
+ return tmp
+ end
+ module_function :parse_qvalues
+
+ #####
+
+ def dequote(str)
+ ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
+ ret.gsub!(/\\(.)/, "\\1")
+ ret
+ end
+ module_function :dequote
+
+ def quote(str)
+ '"' << str.gsub(/[\\\"]/o, "\\\1") << '"'
+ end
+ module_function :quote
+
+ #####
+
+ class FormData < String
+ EmptyRawHeader = [].freeze
+ EmptyHeader = {}.freeze
+
+ attr_accessor :name, :filename, :next_data
+ protected :next_data
+
+ def initialize(*args)
+ @name = @filename = @next_data = nil
+ if args.empty?
+ @raw_header = []
+ @header = nil
+ super("")
+ else
+ @raw_header = EmptyRawHeader
+ @header = EmptyHeader
+ super(args.shift)
+ unless args.empty?
+ @next_data = self.class.new(*args)
+ end
+ end
+ end
+
+ def [](*key)
+ begin
+ @header[key[0].downcase].join(", ")
+ rescue StandardError, NameError
+ super
+ end
+ end
+
+ def <<(str)
+ if @header
+ super
+ elsif str == CRLF
+ @header = HTTPUtils::parse_header(@raw_header.join)
+ if cd = self['content-disposition']
+ if /\s+name="(.*?)"/ =~ cd then @name = $1 end
+ if /\s+filename="(.*?)"/ =~ cd then @filename = $1 end
+ end
+ else
+ @raw_header << str
+ end
+ self
+ end
+
+ def append_data(data)
+ tmp = self
+ while tmp
+ unless tmp.next_data
+ tmp.next_data = data
+ break
+ end
+ tmp = tmp.next_data
+ end
+ self
+ end
+
+ def each_data
+ tmp = self
+ while tmp
+ next_data = tmp.next_data
+ yield(tmp)
+ tmp = next_data
+ end
+ end
+
+ def list
+ ret = []
+ each_data{|data|
+ ret << data.to_s
+ }
+ ret
+ end
+
+ alias :to_ary :list
+
+ def to_s
+ String.new(self)
+ end
+ end
+
+ def parse_query(str)
+ query = Hash.new
+ if str
+ str.split(/[&;]/).each{|x|
+ next if x.empty?
+ key, val = x.split(/=/,2)
+ key = unescape_form(key)
+ val = unescape_form(val.to_s)
+ val = FormData.new(val)
+ val.name = key
+ if query.has_key?(key)
+ query[key].append_data(val)
+ next
+ end
+ query[key] = val
+ }
+ end
+ query
+ end
+ module_function :parse_query
+
+ def parse_form_data(io, boundary)
+ boundary_regexp = /\A--#{Regexp.quote(boundary)}(--)?#{CRLF}\z/
+ form_data = Hash.new
+ return form_data unless io
+ data = nil
+ io.each_line{|line|
+ if boundary_regexp =~ line
+ if data
+ data.chop!
+ key = data.name
+ if form_data.has_key?(key)
+ form_data[key].append_data(data)
+ else
+ form_data[key] = data
+ end
+ end
+ data = FormData.new
+ next
+ else
+ if data
+ data << line
+ end
+ end
+ }
+ return form_data
+ end
+ module_function :parse_form_data
+
+ #####
+
+ reserved = ';/?:@&=+$,'
+ num = '0123456789'
+ lowalpha = 'abcdefghijklmnopqrstuvwxyz'
+ upalpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ mark = '-_.!~*\'()'
+ unreserved = num + lowalpha + upalpha + mark
+ control = (0x0..0x1f).collect{|c| c.chr }.join + "\x7f"
+ space = " "
+ delims = '<>#%"'
+ unwise = '{}|\\^[]`'
+ nonascii = (0x80..0xff).collect{|c| c.chr }.join
+
+ module_function
+
+ def _make_regex(str) /([#{Regexp.escape(str)}])/n end
+ def _make_regex!(str) /([^#{Regexp.escape(str)}])/n end
+ def _escape(str, regex) str.gsub(regex){ "%%%02X" % $1.ord } end
+ def _unescape(str, regex) str.gsub(regex){ $1.hex.chr } end
+
+ UNESCAPED = _make_regex(control+space+delims+unwise+nonascii)
+ UNESCAPED_FORM = _make_regex(reserved+control+delims+unwise+nonascii)
+ NONASCII = _make_regex(nonascii)
+ ESCAPED = /%([0-9a-fA-F]{2})/
+ UNESCAPED_PCHAR = _make_regex!(unreserved+":@&=+$,")
+
+ def escape(str)
+ _escape(str, UNESCAPED)
+ end
+
+ def unescape(str)
+ _unescape(str, ESCAPED)
+ end
+
+ def escape_form(str)
+ ret = _escape(str, UNESCAPED_FORM)
+ ret.gsub!(/ /, "+")
+ ret
+ end
+
+ def unescape_form(str)
+ _unescape(str.gsub(/\+/, " "), ESCAPED)
+ end
+
+ def escape_path(str)
+ result = ""
+ str.scan(%r{/([^/]*)}).each{|i|
+ result << "/" << _escape(i[0], UNESCAPED_PCHAR)
+ }
+ return result
+ end
+
+ def escape8bit(str)
+ _escape(str, NONASCII)
+ end
+ end
+end
diff --git a/ruby/lib/webrick/httpversion.rb b/ruby/lib/webrick/httpversion.rb
new file mode 100644
index 0000000..86907a2
--- /dev/null
+++ b/ruby/lib/webrick/httpversion.rb
@@ -0,0 +1,49 @@
+#
+# HTTPVersion.rb -- presentation of HTTP version
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: httpversion.rb,v 1.5 2002/09/21 12:23:37 gotoyuzo Exp $
+
+module WEBrick
+ class HTTPVersion
+ include Comparable
+
+ attr_accessor :major, :minor
+
+ def self.convert(version)
+ version.is_a?(self) ? version : new(version)
+ end
+
+ def initialize(version)
+ case version
+ when HTTPVersion
+ @major, @minor = version.major, version.minor
+ when String
+ if /^(\d+)\.(\d+)$/ =~ version
+ @major, @minor = $1.to_i, $2.to_i
+ end
+ end
+ if @major.nil? || @minor.nil?
+ raise ArgumentError,
+ format("cannot convert %s into %s", version.class, self.class)
+ end
+ end
+
+ def <=>(other)
+ unless other.is_a?(self.class)
+ other = self.class.new(other)
+ end
+ if (ret = @major <=> other.major) == 0
+ return @minor <=> other.minor
+ end
+ return ret
+ end
+
+ def to_s
+ format("%d.%d", @major, @minor)
+ end
+ end
+end
diff --git a/ruby/lib/webrick/log.rb b/ruby/lib/webrick/log.rb
new file mode 100644
index 0000000..5d4fd0a
--- /dev/null
+++ b/ruby/lib/webrick/log.rb
@@ -0,0 +1,88 @@
+#
+# log.rb -- Log Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: log.rb,v 1.26 2002/10/06 17:06:10 gotoyuzo Exp $
+
+module WEBrick
+ class BasicLog
+ # log-level constant
+ FATAL, ERROR, WARN, INFO, DEBUG = 1, 2, 3, 4, 5
+
+ attr_accessor :level
+
+ def initialize(log_file=nil, level=nil)
+ @level = level || INFO
+ case log_file
+ when String
+ @log = open(log_file, "a+")
+ @log.sync = true
+ @opened = true
+ when NilClass
+ @log = $stderr
+ else
+ @log = log_file # requires "<<". (see BasicLog#log)
+ end
+ end
+
+ def close
+ @log.close if @opened
+ @log = nil
+ end
+
+ def log(level, data)
+ if @log && level <= @level
+ data += "\n" if /\n\Z/ !~ data
+ @log << data
+ end
+ end
+
+ def <<(obj)
+ log(INFO, obj.to_s)
+ end
+
+ def fatal(msg) log(FATAL, "FATAL " << format(msg)); end
+ def error(msg) log(ERROR, "ERROR " << format(msg)); end
+ def warn(msg) log(WARN, "WARN " << format(msg)); end
+ def info(msg) log(INFO, "INFO " << format(msg)); end
+ def debug(msg) log(DEBUG, "DEBUG " << format(msg)); end
+
+ def fatal?; @level >= FATAL; end
+ def error?; @level >= ERROR; end
+ def warn?; @level >= WARN; end
+ def info?; @level >= INFO; end
+ def debug?; @level >= DEBUG; end
+
+ private
+
+ def format(arg)
+ str = if arg.is_a?(Exception)
+ "#{arg.class}: #{arg.message}\n\t" <<
+ arg.backtrace.join("\n\t") << "\n"
+ elsif arg.respond_to?(:to_str)
+ arg.to_str
+ else
+ arg.inspect
+ end
+ end
+ end
+
+ class Log < BasicLog
+ attr_accessor :time_format
+
+ def initialize(log_file=nil, level=nil)
+ super(log_file, level)
+ @time_format = "[%Y-%m-%d %H:%M:%S]"
+ end
+
+ def log(level, data)
+ tmp = Time.now.strftime(@time_format)
+ tmp << " " << data
+ super(level, tmp)
+ end
+ end
+end
diff --git a/ruby/lib/webrick/server.rb b/ruby/lib/webrick/server.rb
new file mode 100644
index 0000000..d0b6f2b
--- /dev/null
+++ b/ruby/lib/webrick/server.rb
@@ -0,0 +1,210 @@
+#
+# server.rb -- GenericServer Class
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: server.rb,v 1.62 2003/07/22 19:20:43 gotoyuzo Exp $
+
+require 'thread'
+require 'socket'
+require 'webrick/config'
+require 'webrick/log'
+
+module WEBrick
+
+ class ServerError < StandardError; end
+
+ class SimpleServer
+ def SimpleServer.start
+ yield
+ end
+ end
+
+ class Daemon
+ def Daemon.start
+ exit!(0) if fork
+ Process::setsid
+ exit!(0) if fork
+ Dir::chdir("/")
+ File::umask(0)
+ STDIN.reopen("/dev/null")
+ STDOUT.reopen("/dev/null", "w")
+ STDERR.reopen("/dev/null", "w")
+ yield if block_given?
+ end
+ end
+
+ class GenericServer
+ attr_reader :status, :config, :logger, :tokens, :listeners
+
+ def initialize(config={}, default=Config::General)
+ @config = default.dup.update(config)
+ @status = :Stop
+ @config[:Logger] ||= Log::new
+ @logger = @config[:Logger]
+
+ @tokens = SizedQueue.new(@config[:MaxClients])
+ @config[:MaxClients].times{ @tokens.push(nil) }
+
+ webrickv = WEBrick::VERSION
+ rubyv = "#{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
+ @logger.info("WEBrick #{webrickv}")
+ @logger.info("ruby #{rubyv}")
+
+ @listeners = []
+ unless @config[:DoNotListen]
+ if @config[:Listen]
+ warn(":Listen option is deprecated; use GenericServer#listen")
+ end
+ listen(@config[:BindAddress], @config[:Port])
+ if @config[:Port] == 0
+ @config[:Port] = @listeners[0].addr[1]
+ end
+ end
+ end
+
+ def [](key)
+ @config[key]
+ end
+
+ def listen(address, port)
+ @listeners += Utils::create_listeners(address, port, @logger)
+ end
+
+ def start(&block)
+ raise ServerError, "already started." if @status != :Stop
+ server_type = @config[:ServerType] || SimpleServer
+
+ server_type.start{
+ @logger.info \
+ "#{self.class}#start: pid=#{$$} port=#{@config[:Port]}"
+ call_callback(:StartCallback)
+
+ thgroup = ThreadGroup.new
+ @status = :Running
+ while @status == :Running
+ begin
+ if svrs = IO.select(@listeners, nil, nil, 2.0)
+ svrs[0].each{|svr|
+ @tokens.pop # blocks while no token is there.
+ if sock = accept_client(svr)
+ sock.do_not_reverse_lookup = config[:DoNotReverseLookup]
+ th = start_thread(sock, &block)
+ th[:WEBrickThread] = true
+ thgroup.add(th)
+ else
+ @tokens.push(nil)
+ end
+ }
+ end
+ rescue Errno::EBADF, IOError => ex
+ # if the listening socket was closed in GenericServer#shutdown,
+ # IO::select raise it.
+ rescue Exception => ex
+ msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
+ @logger.error msg
+ end
+ end
+
+ @logger.info "going to shutdown ..."
+ thgroup.list.each{|th| th.join if th[:WEBrickThread] }
+ call_callback(:StopCallback)
+ @logger.info "#{self.class}#start done."
+ @status = :Stop
+ }
+ end
+
+ def stop
+ if @status == :Running
+ @status = :Shutdown
+ end
+ end
+
+ def shutdown
+ stop
+ @listeners.each{|s|
+ if @logger.debug?
+ addr = s.addr
+ @logger.debug("close TCPSocket(#{addr[2]}, #{addr[1]})")
+ end
+ begin
+ s.shutdown
+ rescue Errno::ENOTCONN
+ # when `Errno::ENOTCONN: Socket is not connected' on some platforms,
+ # call #close instead of #shutdown.
+ # (ignore @config[:ShutdownSocketWithoutClose])
+ s.close
+ else
+ unless @config[:ShutdownSocketWithoutClose]
+ s.close
+ end
+ end
+ }
+ @listeners.clear
+ end
+
+ def run(sock)
+ @logger.fatal "run() must be provided by user."
+ end
+
+ private
+
+ def accept_client(svr)
+ sock = nil
+ begin
+ sock = svr.accept
+ sock.sync = true
+ Utils::set_non_blocking(sock)
+ Utils::set_close_on_exec(sock)
+ rescue Errno::ECONNRESET, Errno::ECONNABORTED,
+ Errno::EPROTO, Errno::EINVAL => ex
+ rescue Exception => ex
+ msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
+ @logger.error msg
+ end
+ return sock
+ end
+
+ def start_thread(sock, &block)
+ Thread.start{
+ begin
+ Thread.current[:WEBrickSocket] = sock
+ begin
+ addr = sock.peeraddr
+ @logger.debug "accept: #{addr[3]}:#{addr[1]}"
+ rescue SocketError
+ @logger.debug "accept: <address unknown>"
+ raise
+ end
+ call_callback(:AcceptCallback, sock)
+ block ? block.call(sock) : run(sock)
+ rescue Errno::ENOTCONN
+ @logger.debug "Errno::ENOTCONN raised"
+ rescue ServerError => ex
+ msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
+ @logger.error msg
+ rescue Exception => ex
+ @logger.error ex
+ ensure
+ @tokens.push(nil)
+ Thread.current[:WEBrickSocket] = nil
+ if addr
+ @logger.debug "close: #{addr[3]}:#{addr[1]}"
+ else
+ @logger.debug "close: <address unknown>"
+ end
+ sock.close
+ end
+ }
+ end
+
+ def call_callback(callback_name, *args)
+ if cb = @config[callback_name]
+ cb.call(*args)
+ end
+ end
+ end # end of GenericServer
+end
diff --git a/ruby/lib/webrick/ssl.rb b/ruby/lib/webrick/ssl.rb
new file mode 100644
index 0000000..4c71c87
--- /dev/null
+++ b/ruby/lib/webrick/ssl.rb
@@ -0,0 +1,126 @@
+#
+# ssl.rb -- SSL/TLS enhancement for GenericServer
+#
+# Copyright (c) 2003 GOTOU Yuuzou All rights reserved.
+#
+# $Id: ssl.rb 11708 2007-02-12 23:01:19Z shyouhei $
+
+require 'webrick'
+require 'openssl'
+
+module WEBrick
+ module Config
+ svrsoft = General[:ServerSoftware]
+ osslv = ::OpenSSL::OPENSSL_VERSION.split[1]
+ SSL = {
+ :ServerSoftware => "#{svrsoft} OpenSSL/#{osslv}",
+ :SSLEnable => false,
+ :SSLCertificate => nil,
+ :SSLPrivateKey => nil,
+ :SSLClientCA => nil,
+ :SSLExtraChainCert => nil,
+ :SSLCACertificateFile => nil,
+ :SSLCACertificatePath => nil,
+ :SSLCertificateStore => nil,
+ :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE,
+ :SSLVerifyDepth => nil,
+ :SSLVerifyCallback => nil, # custom verification
+ :SSLTimeout => nil,
+ :SSLOptions => nil,
+ :SSLStartImmediately => true,
+ # Must specify if you use auto generated certificate.
+ :SSLCertName => nil,
+ :SSLCertComment => "Generated by Ruby/OpenSSL"
+ }
+ General.update(SSL)
+ end
+
+ module Utils
+ def create_self_signed_cert(bits, cn, comment)
+ rsa = OpenSSL::PKey::RSA.new(bits){|p, n|
+ case p
+ when 0; $stderr.putc "." # BN_generate_prime
+ when 1; $stderr.putc "+" # BN_generate_prime
+ when 2; $stderr.putc "*" # searching good prime,
+ # n = #of try,
+ # but also data from BN_generate_prime
+ when 3; $stderr.putc "\n" # found good prime, n==0 - p, n==1 - q,
+ # but also data from BN_generate_prime
+ else; $stderr.putc "*" # BN_generate_prime
+ end
+ }
+ cert = OpenSSL::X509::Certificate.new
+ cert.version = 3
+ cert.serial = 0
+ name = OpenSSL::X509::Name.new(cn)
+ cert.subject = name
+ cert.issuer = name
+ cert.not_before = Time.now
+ cert.not_after = Time.now + (365*24*60*60)
+ cert.public_key = rsa.public_key
+
+ ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
+ ef.issuer_certificate = cert
+ cert.extensions = [
+ ef.create_extension("basicConstraints","CA:FALSE"),
+ ef.create_extension("keyUsage", "keyEncipherment"),
+ ef.create_extension("subjectKeyIdentifier", "hash"),
+ ef.create_extension("extendedKeyUsage", "serverAuth"),
+ ef.create_extension("nsComment", comment),
+ ]
+ aki = ef.create_extension("authorityKeyIdentifier",
+ "keyid:always,issuer:always")
+ cert.add_extension(aki)
+ cert.sign(rsa, OpenSSL::Digest::SHA1.new)
+
+ return [ cert, rsa ]
+ end
+ module_function :create_self_signed_cert
+ end
+
+ class GenericServer
+ def ssl_context
+ @ssl_context ||= nil
+ end
+
+ def listen(address, port)
+ listeners = Utils::create_listeners(address, port, @logger)
+ if @config[:SSLEnable]
+ unless ssl_context
+ @ssl_context = setup_ssl_context(@config)
+ @logger.info("\n" + @config[:SSLCertificate].to_text)
+ end
+ listeners.collect!{|svr|
+ ssvr = ::OpenSSL::SSL::SSLServer.new(svr, ssl_context)
+ ssvr.start_immediately = @config[:SSLStartImmediately]
+ ssvr
+ }
+ end
+ @listeners += listeners
+ end
+
+ def setup_ssl_context(config)
+ unless config[:SSLCertificate]
+ cn = config[:SSLCertName]
+ comment = config[:SSLCertComment]
+ cert, key = Utils::create_self_signed_cert(1024, cn, comment)
+ config[:SSLCertificate] = cert
+ config[:SSLPrivateKey] = key
+ end
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.key = config[:SSLPrivateKey]
+ ctx.cert = config[:SSLCertificate]
+ ctx.client_ca = config[:SSLClientCA]
+ ctx.extra_chain_cert = config[:SSLExtraChainCert]
+ ctx.ca_file = config[:SSLCACertificateFile]
+ ctx.ca_path = config[:SSLCACertificatePath]
+ ctx.cert_store = config[:SSLCertificateStore]
+ ctx.verify_mode = config[:SSLVerifyClient]
+ ctx.verify_depth = config[:SSLVerifyDepth]
+ ctx.verify_callback = config[:SSLVerifyCallback]
+ ctx.timeout = config[:SSLTimeout]
+ ctx.options = config[:SSLOptions]
+ ctx
+ end
+ end
+end
diff --git a/ruby/lib/webrick/utils.rb b/ruby/lib/webrick/utils.rb
new file mode 100644
index 0000000..f2ecfc1
--- /dev/null
+++ b/ruby/lib/webrick/utils.rb
@@ -0,0 +1,175 @@
+#
+# utils.rb -- Miscellaneous utilities
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: utils.rb,v 1.10 2003/02/16 22:22:54 gotoyuzo Exp $
+
+require 'socket'
+require 'fcntl'
+begin
+ require 'etc'
+rescue LoadError
+ nil
+end
+
+module WEBrick
+ module Utils
+ def set_non_blocking(io)
+ flag = File::NONBLOCK
+ if defined?(Fcntl::F_GETFL)
+ flag |= io.fcntl(Fcntl::F_GETFL)
+ end
+ io.fcntl(Fcntl::F_SETFL, flag)
+ end
+ module_function :set_non_blocking
+
+ def set_close_on_exec(io)
+ if defined?(Fcntl::FD_CLOEXEC)
+ io.fcntl(Fcntl::FD_CLOEXEC, 1)
+ end
+ end
+ module_function :set_close_on_exec
+
+ def su(user)
+ if defined?(Etc)
+ pw = Etc.getpwnam(user)
+ Process::initgroups(user, pw.gid)
+ Process::Sys::setgid(pw.gid)
+ Process::Sys::setuid(pw.uid)
+ else
+ warn("WEBrick::Utils::su doesn't work on this platform")
+ end
+ end
+ module_function :su
+
+ def getservername
+ host = Socket::gethostname
+ begin
+ Socket::gethostbyname(host)[0]
+ rescue
+ host
+ end
+ end
+ module_function :getservername
+
+ def create_listeners(address, port, logger=nil)
+ unless port
+ raise ArgumentError, "must specify port"
+ end
+ res = Socket::getaddrinfo(address, port,
+ Socket::AF_UNSPEC, # address family
+ Socket::SOCK_STREAM, # socket type
+ 0, # protocol
+ Socket::AI_PASSIVE) # flag
+ last_error = nil
+ sockets = []
+ res.each{|ai|
+ begin
+ logger.debug("TCPServer.new(#{ai[3]}, #{port})") if logger
+ sock = TCPServer.new(ai[3], port)
+ port = sock.addr[1] if port == 0
+ Utils::set_close_on_exec(sock)
+ sockets << sock
+ rescue => ex
+ logger.warn("TCPServer Error: #{ex}") if logger
+ last_error = ex
+ end
+ }
+ raise last_error if sockets.empty?
+ return sockets
+ end
+ module_function :create_listeners
+
+ RAND_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
+ "0123456789" +
+ "abcdefghijklmnopqrstuvwxyz"
+
+ def random_string(len)
+ rand_max = RAND_CHARS.bytesize
+ ret = ""
+ len.times{ ret << RAND_CHARS[rand(rand_max)] }
+ ret
+ end
+ module_function :random_string
+
+ ###########
+
+ require "thread"
+ require "timeout"
+ require "singleton"
+
+ class TimeoutHandler
+ include Singleton
+ TimeoutMutex = Mutex.new
+
+ def TimeoutHandler.register(seconds, exception)
+ TimeoutMutex.synchronize{
+ instance.register(Thread.current, Time.now + seconds, exception)
+ }
+ end
+
+ def TimeoutHandler.cancel(id)
+ TimeoutMutex.synchronize{
+ instance.cancel(Thread.current, id)
+ }
+ end
+
+ def initialize
+ @timeout_info = Hash.new
+ Thread.start{
+ while true
+ now = Time.now
+ @timeout_info.each{|thread, ary|
+ ary.dup.each{|info|
+ time, exception = *info
+ interrupt(thread, info.object_id, exception) if time < now
+ }
+ }
+ sleep 0.5
+ end
+ }
+ end
+
+ def interrupt(thread, id, exception)
+ TimeoutMutex.synchronize{
+ if cancel(thread, id) && thread.alive?
+ thread.raise(exception, "execution timeout")
+ end
+ }
+ end
+
+ def register(thread, time, exception)
+ @timeout_info[thread] ||= Array.new
+ @timeout_info[thread] << [time, exception]
+ return @timeout_info[thread].last.object_id
+ end
+
+ def cancel(thread, id)
+ if ary = @timeout_info[thread]
+ ary.delete_if{|info| info.object_id == id }
+ if ary.empty?
+ @timeout_info.delete(thread)
+ end
+ return true
+ end
+ return false
+ end
+ end
+
+ def timeout(seconds, exception=Timeout::Error)
+ return yield if seconds.nil? or seconds.zero?
+ # raise ThreadError, "timeout within critical session" if Thread.critical
+ id = TimeoutHandler.register(seconds, exception)
+ begin
+ yield(seconds)
+ ensure
+ TimeoutHandler.cancel(id)
+ end
+ end
+ module_function :timeout
+ end
+end
diff --git a/ruby/lib/webrick/version.rb b/ruby/lib/webrick/version.rb
new file mode 100644
index 0000000..b2b9fd3
--- /dev/null
+++ b/ruby/lib/webrick/version.rb
@@ -0,0 +1,13 @@
+#
+# version.rb -- version and release date
+#
+# Author: IPR -- Internet Programming with Ruby -- writers
+# Copyright (c) 2000 TAKAHASHI Masayoshi, GOTOU YUUZOU
+# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
+# reserved.
+#
+# $IPR: version.rb,v 1.74 2003/07/22 19:20:43 gotoyuzo Exp $
+
+module WEBrick
+ VERSION = "1.3.1"
+end
diff --git a/ruby/lib/xmlrpc/base64.rb b/ruby/lib/xmlrpc/base64.rb
new file mode 100644
index 0000000..0fe5b82
--- /dev/null
+++ b/ruby/lib/xmlrpc/base64.rb
@@ -0,0 +1,81 @@
+=begin
+= xmlrpc/base64.rb
+Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
+
+Released under the same term of license as Ruby.
+
+= Classes
+* ((<XMLRPC::Base64>))
+
+= XMLRPC::Base64
+== Description
+This class is necessary for (('xmlrpc4r')) to determine that a string should
+be transmitted base64-encoded and not as a raw-string.
+You can use (({XMLRPC::Base64})) on the client and server-side as a
+parameter and/or return-value.
+
+== Class Methods
+--- XMLRPC::Base64.new( str, state = :dec )
+ Creates a new (({XMLRPC::Base64})) instance with string ((|str|)) as the
+ internal string. When ((|state|)) is (({:dec})) it assumes that the
+ string ((|str|)) is not in base64 format (perhaps already decoded),
+ otherwise if ((|state|)) is (({:enc})) it decodes ((|str|))
+ and stores it as the internal string.
+
+--- XMLRPC::Base64.decode( str )
+ Decodes string ((|str|)) with base64 and returns that value.
+
+--- XMLRPC::Base64.encode( str )
+ Encodes string ((|str|)) with base64 and returns that value.
+
+== Instance Methods
+--- XMLRPC::Base64#decoded
+ Returns the internal string decoded.
+
+--- XMLRPC::Base64#encoded
+ Returns the internal string encoded with base64.
+
+=end
+
+module XMLRPC
+
+class Base64
+
+ def initialize(str, state = :dec)
+ case state
+ when :enc
+ @str = Base64.decode(str)
+ when :dec
+ @str = str
+ else
+ raise ArgumentError, "wrong argument; either :enc or :dec"
+ end
+ end
+
+ def decoded
+ @str
+ end
+
+ def encoded
+ Base64.encode(@str)
+ end
+
+
+ def Base64.decode(str)
+ str.gsub(/\s+/, "").unpack("m")[0]
+ end
+
+ def Base64.encode(str)
+ [str].pack("m")
+ end
+
+end
+
+
+end # module XMLRPC
+
+
+=begin
+= History
+ $Id: base64.rb 11708 2007-02-12 23:01:19Z shyouhei $
+=end
diff --git a/ruby/lib/xmlrpc/client.rb b/ruby/lib/xmlrpc/client.rb
new file mode 100644
index 0000000..07ea688
--- /dev/null
+++ b/ruby/lib/xmlrpc/client.rb
@@ -0,0 +1,625 @@
+=begin
+= xmlrpc/client.rb
+Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
+
+Released under the same term of license as Ruby.
+
+= Classes
+* ((<XMLRPC::Client>))
+* ((<XMLRPC::Client::Proxy>))
+
+
+= XMLRPC::Client
+== Synopsis
+ require "xmlrpc/client"
+
+ server = XMLRPC::Client.new("www.ruby-lang.org", "/RPC2", 80)
+ begin
+ param = server.call("michael.add", 4, 5)
+ puts "4 + 5 = #{param}"
+ rescue XMLRPC::FaultException => e
+ puts "Error:"
+ puts e.faultCode
+ puts e.faultString
+ end
+
+or
+
+ require "xmlrpc/client"
+
+ server = XMLRPC::Client.new("www.ruby-lang.org", "/RPC2", 80)
+ ok, param = server.call2("michael.add", 4, 5)
+ if ok then
+ puts "4 + 5 = #{param}"
+ else
+ puts "Error:"
+ puts param.faultCode
+ puts param.faultString
+ end
+
+== Description
+Class (({XMLRPC::Client})) provides remote procedure calls to a XML-RPC server.
+After setting the connection-parameters with ((<XMLRPC::Client.new>)) which
+creates a new (({XMLRPC::Client})) instance, you can execute a remote procedure
+by sending the ((<call|XMLRPC::Client#call>)) or ((<call2|XMLRPC::Client#call2>))
+message to this new instance. The given parameters indicate which method to
+call on the remote-side and of course the parameters for the remote procedure.
+
+== Class Methods
+--- XMLRPC::Client.new( host=nil, path=nil, port=nil, proxy_host=nil, proxy_port=nil, user=nil, password=nil, use_ssl=false, timeout =nil)
+ Creates an object which represents the remote XML-RPC server on the
+ given host ((|host|)). If the server is CGI-based, ((|path|)) is the
+ path to the CGI-script, which will be called, otherwise (in the
+ case of a standalone server) ((|path|)) should be (({"/RPC2"})).
+ ((|port|)) is the port on which the XML-RPC server listens.
+ If ((|proxy_host|)) is given, then a proxy server listening at
+ ((|proxy_host|)) is used. ((|proxy_port|)) is the port of the
+ proxy server.
+
+ Default values for ((|host|)), ((|path|)) and ((|port|)) are 'localhost', '/RPC2' and
+ '80' respectively using SSL '443'.
+
+ If ((|user|)) and ((|password|)) are given, each time a request is send,
+ a Authorization header is send. Currently only Basic Authentification is
+ implemented no Digest.
+
+ If ((|use_ssl|)) is set to (({true})), comunication over SSL is enabled.
+ Note, that you need the SSL package from RAA installed.
+
+ Parameter ((|timeout|)) is the time to wait for a XML-RPC response, defaults to 30.
+
+--- XMLRPC::Client.new2( uri, proxy=nil, timeout=nil)
+--- XMLRPC::Client.new_from_uri( uri, proxy=nil, timeout=nil)
+: uri
+ URI specifying protocol (http or https), host, port, path, user and password.
+ Example: https://user:password@host:port/path
+
+: proxy
+ Is of the form "host:port".
+
+: timeout
+ Defaults to 30.
+
+--- XMLRPC::Client.new3( hash={} )
+--- XMLRPC::Client.new_from_hash( hash={} )
+ Parameter ((|hash|)) has following case-insensitive keys:
+ * host
+ * path
+ * port
+ * proxy_host
+ * proxy_port
+ * user
+ * password
+ * use_ssl
+ * timeout
+
+ Calls ((<XMLRPC::Client.new>)) with the corresponding values.
+
+== Instance Methods
+--- XMLRPC::Client#call( method, *args )
+ Invokes the method named ((|method|)) with the parameters given by
+ ((|args|)) on the XML-RPC server.
+ The parameter ((|method|)) is converted into a (({String})) and should
+ be a valid XML-RPC method-name.
+ Each parameter of ((|args|)) must be of one of the following types,
+ where (({Hash})), (({Struct})) and (({Array})) can contain any of these listed ((:types:)):
+ * (({Fixnum})), (({Bignum}))
+ * (({TrueClass})), (({FalseClass})) ((({true})), (({false})))
+ * (({String})), (({Symbol}))
+ * (({Float}))
+ * (({Hash})), (({Struct}))
+ * (({Array}))
+ * (({Date})), (({Time})), (({XMLRPC::DateTime}))
+ * (({XMLRPC::Base64}))
+ * A Ruby object which class includes XMLRPC::Marshallable (only if Config::ENABLE_MARSHALLABLE is (({true}))).
+ That object is converted into a hash, with one additional key/value pair "___class___" which contains the class name
+ for restoring later that object.
+
+ The method returns the return-value from the RPC
+ ((-stands for Remote Procedure Call-)).
+ The type of the return-value is one of the above shown,
+ only that a (({Bignum})) is only allowed when it fits in 32-bit and
+ that a XML-RPC (('dateTime.iso8601')) type is always returned as
+ a ((<(({XMLRPC::DateTime}))|URL:datetime.html>)) object and
+ a (({Struct})) is never returned, only a (({Hash})), the same for a (({Symbol})), where
+ always a (({String})) is returned.
+ A (({XMLRPC::Base64})) is returned as a (({String})) from xmlrpc4r version 1.6.1 on.
+
+ If the remote procedure returned a fault-structure, then a
+ (({XMLRPC::FaultException})) exception is raised, which has two accessor-methods
+ (({faultCode})) and (({faultString})) of type (({Integer})) and (({String})).
+
+--- XMLRPC::Client#call2( method, *args )
+ The difference between this method and ((<call|XMLRPC::Client#call>)) is, that
+ this method do ((*not*)) raise a (({XMLRPC::FaultException})) exception.
+ The method returns an array of two values. The first value indicates if
+ the second value is a return-value ((({true}))) or an object of type
+ (({XMLRPC::FaultException})).
+ Both are explained in ((<call|XMLRPC::Client#call>)).
+
+ Simple to remember: The "2" in "call2" denotes the number of values it returns.
+
+--- XMLRPC::Client#multicall( *methods )
+ You can use this method to execute several methods on a XMLRPC server which supports
+ the multi-call extension.
+ Example:
+
+ s.multicall(
+ ['michael.add', 3, 4],
+ ['michael.sub', 4, 5]
+ )
+ # => [7, -1]
+
+--- XMLRPC::Client#multicall2( *methods )
+ Same as ((<XMLRPC::Client#multicall>)), but returns like ((<XMLRPC::Client#call2>)) two parameters
+ instead of raising an (({XMLRPC::FaultException})).
+
+--- XMLRPC::Client#proxy( prefix, *args )
+ Returns an object of class (({XMLRPC::Client::Proxy})), initialized with
+ ((|prefix|)) and ((|args|)). A proxy object returned by this method behaves
+ like ((<XMLRPC::Client#call>)), i.e. a call on that object will raise a
+ (({XMLRPC::FaultException})) when a fault-structure is returned by that call.
+
+--- XMLRPC::Client#proxy2( prefix, *args )
+ Almost the same like ((<XMLRPC::Client#proxy>)) only that a call on the returned
+ (({XMLRPC::Client::Proxy})) object behaves like ((<XMLRPC::Client#call2>)), i.e.
+ a call on that object will return two parameters.
+
+
+
+
+--- XMLRPC::Client#call_async(...)
+--- XMLRPC::Client#call2_async(...)
+--- XMLRPC::Client#multicall_async(...)
+--- XMLRPC::Client#multicall2_async(...)
+--- XMLRPC::Client#proxy_async(...)
+--- XMLRPC::Client#proxy2_async(...)
+ In contrast to corresponding methods without "_async", these can be
+ called concurrently and use for each request a new connection, where the
+ non-asynchronous counterparts use connection-alive (one connection for all requests)
+ if possible.
+
+ Note, that you have to use Threads to call these methods concurrently.
+ The following example calls two methods concurrently:
+
+ Thread.new {
+ p client.call_async("michael.add", 4, 5)
+ }
+
+ Thread.new {
+ p client.call_async("michael.div", 7, 9)
+ }
+
+
+--- XMLRPC::Client#timeout
+--- XMLRPC::Client#user
+--- XMLRPC::Client#password
+ Return the corresponding attributes.
+
+--- XMLRPC::Client#timeout= (new_timeout)
+--- XMLRPC::Client#user= (new_user)
+--- XMLRPC::Client#password= (new_password)
+ Set the corresponding attributes.
+
+
+--- XMLRPC::Client#set_writer( writer )
+ Sets the XML writer to use for generating XML output.
+ Should be an instance of a class from module (({XMLRPC::XMLWriter})).
+ If this method is not called, then (({XMLRPC::Config::DEFAULT_WRITER})) is used.
+
+--- XMLRPC::Client#set_parser( parser )
+ Sets the XML parser to use for parsing XML documents.
+ Should be an instance of a class from module (({XMLRPC::XMLParser})).
+ If this method is not called, then (({XMLRPC::Config::DEFAULT_PARSER})) is used.
+
+--- XMLRPC::Client#cookie
+--- XMLRPC::Client#cookie= (cookieString)
+ Get and set the HTTP Cookie header.
+
+--- XMLRPC::Client#http_header_extra= (additionalHeaders)
+ Set extra HTTP headers that are included in the request.
+
+--- XMLRPC::Client#http_header_extra
+ Access the via ((<XMLRPC::Client#http_header_extra=>)) assigned header.
+
+--- XMLRPC::Client#http_last_response
+ Returns the (({Net::HTTPResponse})) object of the last RPC.
+
+= XMLRPC::Client::Proxy
+== Synopsis
+ require "xmlrpc/client"
+
+ server = XMLRPC::Client.new("www.ruby-lang.org", "/RPC2", 80)
+
+ michael = server.proxy("michael")
+ michael2 = server.proxy("michael", 4)
+
+ # both calls should return the same value '9'.
+ p michael.add(4,5)
+ p michael2.add(5)
+
+== Description
+Class (({XMLRPC::Client::Proxy})) makes XML-RPC calls look nicer!
+You can call any method onto objects of that class - the object handles
+(({method_missing})) and will forward the method call to a XML-RPC server.
+Don't use this class directly, but use instead method ((<XMLRPC::Client#proxy>)) or
+((<XMLRPC::Client#proxy2>)).
+
+== Class Methods
+--- XMLRPC::Client::Proxy.new( server, prefix, args=[], meth=:call, delim="." )
+ Creates an object which provides (({method_missing})).
+
+ ((|server|)) must be of type (({XMLRPC::Client})), which is the XML-RPC server to be used
+ for a XML-RPC call. ((|prefix|)) and ((|delim|)) will be prepended to the methodname
+ called onto this object.
+
+ Parameter ((|meth|)) is the method (call, call2, call_async, call2_async) to use for
+ a RPC.
+
+ ((|args|)) are arguments which are automatically given
+ to every XML-RPC call before the arguments provides through (({method_missing})).
+
+== Instance Methods
+Every method call is forwarded to the XML-RPC server defined in ((<new|XMLRPC::Client::Proxy#new>)).
+
+Note: Inherited methods from class (({Object})) cannot be used as XML-RPC names, because they get around
+(({method_missing})).
+
+
+
+= History
+ $Id: client.rb 19657 2008-10-01 13:46:53Z mame $
+
+=end
+
+
+
+require "xmlrpc/parser"
+require "xmlrpc/create"
+require "xmlrpc/config"
+require "xmlrpc/utils" # ParserWriterChooseMixin
+require "net/http"
+
+module XMLRPC
+
+ class Client
+
+ USER_AGENT = "XMLRPC::Client (Ruby #{RUBY_VERSION})"
+
+ include ParserWriterChooseMixin
+ include ParseContentType
+
+
+ # Constructors -------------------------------------------------------------------
+
+ def initialize(host=nil, path=nil, port=nil, proxy_host=nil, proxy_port=nil,
+ user=nil, password=nil, use_ssl=nil, timeout=nil)
+
+ @http_header_extra = nil
+ @http_last_response = nil
+ @cookie = nil
+
+ @host = host || "localhost"
+ @path = path || "/RPC2"
+ @proxy_host = proxy_host
+ @proxy_port = proxy_port
+ @proxy_host ||= 'localhost' if @proxy_port != nil
+ @proxy_port ||= 8080 if @proxy_host != nil
+ @use_ssl = use_ssl || false
+ @timeout = timeout || 30
+
+ if use_ssl
+ require "net/https"
+ @port = port || 443
+ else
+ @port = port || 80
+ end
+
+ @user, @password = user, password
+
+ set_auth
+
+ # convert ports to integers
+ @port = @port.to_i if @port != nil
+ @proxy_port = @proxy_port.to_i if @proxy_port != nil
+
+ # HTTP object for synchronous calls
+ Net::HTTP.version_1_2
+ @http = Net::HTTP.new(@host, @port, @proxy_host, @proxy_port)
+ @http.use_ssl = @use_ssl if @use_ssl
+ @http.read_timeout = @timeout
+ @http.open_timeout = @timeout
+
+ @parser = nil
+ @create = nil
+ end
+
+
+ class << self
+
+ def new2(uri, proxy=nil, timeout=nil)
+ if match = /^([^:]+):\/\/(([^@]+)@)?([^\/]+)(\/.*)?$/.match(uri)
+ proto = match[1]
+ user, passwd = (match[3] || "").split(":")
+ host, port = match[4].split(":")
+ path = match[5]
+
+ if proto != "http" and proto != "https"
+ raise "Wrong protocol specified. Only http or https allowed!"
+ end
+
+ else
+ raise "Wrong URI as parameter!"
+ end
+
+ proxy_host, proxy_port = (proxy || "").split(":")
+
+ self.new(host, path, port, proxy_host, proxy_port, user, passwd, (proto == "https"), timeout)
+ end
+
+ alias new_from_uri new2
+
+ def new3(hash={})
+
+ # convert all keys into lowercase strings
+ h = {}
+ hash.each { |k,v| h[k.to_s.downcase] = v }
+
+ self.new(h['host'], h['path'], h['port'], h['proxy_host'], h['proxy_port'], h['user'], h['password'],
+ h['use_ssl'], h['timeout'])
+ end
+
+ alias new_from_hash new3
+
+ end
+
+
+ # Attribute Accessors -------------------------------------------------------------------
+
+ # add additional HTTP headers to the request
+ attr_accessor :http_header_extra
+
+ # makes last HTTP response accessible
+ attr_reader :http_last_response
+
+ # Cookie support
+ attr_accessor :cookie
+
+
+ attr_reader :timeout, :user, :password
+
+ def timeout=(new_timeout)
+ @timeout = new_timeout
+ @http.read_timeout = @timeout
+ @http.open_timeout = @timeout
+ end
+
+ def user=(new_user)
+ @user = new_user
+ set_auth
+ end
+
+ def password=(new_password)
+ @password = new_password
+ set_auth
+ end
+
+ # Call methods --------------------------------------------------------------
+
+ def call(method, *args)
+ ok, param = call2(method, *args)
+ if ok
+ param
+ else
+ raise param
+ end
+ end
+
+ def call2(method, *args)
+ request = create().methodCall(method, *args)
+ data = do_rpc(request, false)
+ parser().parseMethodResponse(data)
+ end
+
+ def call_async(method, *args)
+ ok, param = call2_async(method, *args)
+ if ok
+ param
+ else
+ raise param
+ end
+ end
+
+ def call2_async(method, *args)
+ request = create().methodCall(method, *args)
+ data = do_rpc(request, true)
+ parser().parseMethodResponse(data)
+ end
+
+
+ # Multicall methods --------------------------------------------------------------
+
+ def multicall(*methods)
+ ok, params = multicall2(*methods)
+ if ok
+ params
+ else
+ raise params
+ end
+ end
+
+ def multicall2(*methods)
+ gen_multicall(methods, false)
+ end
+
+ def multicall_async(*methods)
+ ok, params = multicall2_async(*methods)
+ if ok
+ params
+ else
+ raise params
+ end
+ end
+
+ def multicall2_async(*methods)
+ gen_multicall(methods, true)
+ end
+
+
+ # Proxy generating methods ------------------------------------------
+
+ def proxy(prefix=nil, *args)
+ Proxy.new(self, prefix, args, :call)
+ end
+
+ def proxy2(prefix=nil, *args)
+ Proxy.new(self, prefix, args, :call2)
+ end
+
+ def proxy_async(prefix=nil, *args)
+ Proxy.new(self, prefix, args, :call_async)
+ end
+
+ def proxy2_async(prefix=nil, *args)
+ Proxy.new(self, prefix, args, :call2_async)
+ end
+
+
+ private # ----------------------------------------------------------
+
+ def set_auth
+ if @user.nil?
+ @auth = nil
+ else
+ a = "#@user"
+ a << ":#@password" if @password != nil
+ @auth = ("Basic " + [a].pack("m")).chomp
+ end
+ end
+
+ def do_rpc(request, async=false)
+ header = {
+ "User-Agent" => USER_AGENT,
+ "Content-Type" => "text/xml; charset=utf-8",
+ "Content-Length" => request.size.to_s,
+ "Connection" => (async ? "close" : "keep-alive")
+ }
+
+ header["Cookie"] = @cookie if @cookie
+ header.update(@http_header_extra) if @http_header_extra
+
+ if @auth != nil
+ # add authorization header
+ header["Authorization"] = @auth
+ end
+
+ resp = nil
+ @http_last_response = nil
+
+ if async
+ # use a new HTTP object for each call
+ Net::HTTP.version_1_2
+ http = Net::HTTP.new(@host, @port, @proxy_host, @proxy_port)
+ http.use_ssl = @use_ssl if @use_ssl
+ http.read_timeout = @timeout
+ http.open_timeout = @timeout
+
+ # post request
+ http.start {
+ resp = http.post2(@path, request, header)
+ }
+ else
+ # reuse the HTTP object for each call => connection alive is possible
+ # we must start connection explicitely first time so that http.request
+ # does not assume that we don't want keepalive
+ @http.start if not @http.started?
+
+ # post request
+ resp = @http.post2(@path, request, header)
+ end
+
+ @http_last_response = resp
+
+ data = resp.body
+
+ if resp.code == "401"
+ # Authorization Required
+ raise "Authorization failed.\nHTTP-Error: #{resp.code} #{resp.message}"
+ elsif resp.code[0,1] != "2"
+ raise "HTTP-Error: #{resp.code} #{resp.message}"
+ end
+
+ ct = parse_content_type(resp["Content-Type"]).first
+ if ct != "text/xml"
+ if ct == "text/html"
+ raise "Wrong content-type (received '#{ct}' but expected 'text/xml'): \n#{data}"
+ else
+ raise "Wrong content-type (received '#{ct}' but expected 'text/xml')"
+ end
+ end
+
+ expected = resp["Content-Length"] || "<unknown>"
+ if data.nil? or data.size == 0
+ raise "Wrong size. Was #{data.size}, should be #{expected}"
+ elsif expected != "<unknown>" and expected.to_i != data.size and resp["Transfer-Encoding"].nil?
+ raise "Wrong size. Was #{data.size}, should be #{expected}"
+ end
+
+ set_cookies = resp.get_fields("Set-Cookie")
+ if set_cookies and !set_cookies.empty?
+ require 'webrick/cookie'
+ @cookie = set_cookies.collect do |set_cookie|
+ cookie = WEBrick::Cookie.parse_set_cookie(set_cookie)
+ WEBrick::Cookie.new(cookie.name, cookie.value).to_s
+ end.join("; ")
+ end
+
+ return data
+ end
+
+ def gen_multicall(methods=[], async=false)
+ meth = :call2
+ meth = :call2_async if async
+
+ ok, params = self.send(meth, "system.multicall",
+ methods.collect {|m| {'methodName' => m[0], 'params' => m[1..-1]} }
+ )
+
+ if ok
+ params = params.collect do |param|
+ if param.is_a? Array
+ param[0]
+ elsif param.is_a? Hash
+ XMLRPC::FaultException.new(param["faultCode"], param["faultString"])
+ else
+ raise "Wrong multicall return value"
+ end
+ end
+ end
+
+ return ok, params
+ end
+
+
+
+ class Proxy
+
+ def initialize(server, prefix, args=[], meth=:call, delim=".")
+ @server = server
+ @prefix = prefix ? prefix + delim : ""
+ @args = args
+ @meth = meth
+ end
+
+ def method_missing(mid, *args)
+ pre = @prefix + mid.to_s
+ arg = @args + args
+ @server.send(@meth, pre, *arg)
+ end
+
+ end # class Proxy
+
+ end # class Client
+
+end # module XMLRPC
+
diff --git a/ruby/lib/xmlrpc/config.rb b/ruby/lib/xmlrpc/config.rb
new file mode 100644
index 0000000..52f7340
--- /dev/null
+++ b/ruby/lib/xmlrpc/config.rb
@@ -0,0 +1,40 @@
+#
+# $Id: config.rb 11708 2007-02-12 23:01:19Z shyouhei $
+# Configuration file for XML-RPC for Ruby
+#
+
+module XMLRPC
+
+ module Config
+
+ DEFAULT_WRITER = XMLWriter::Simple # or XMLWriter::XMLParser
+
+ # available parser:
+ # * XMLParser::NQXMLTreeParser
+ # * XMLParser::NQXMLStreamParser
+ # * XMLParser::XMLTreeParser
+ # * XMLParser::XMLStreamParser (fastest)
+ # * XMLParser::REXMLStreamParser
+ # * XMLParser::XMLScanStreamParser
+ DEFAULT_PARSER = XMLParser::REXMLStreamParser
+
+ # enable <nil/> tag
+ ENABLE_NIL_CREATE = false
+ ENABLE_NIL_PARSER = false
+
+ # allows integers greater than 32-bit if true
+ ENABLE_BIGINT = false
+
+ # enable marshalling ruby objects which include XMLRPC::Marshallable
+ ENABLE_MARSHALLING = true
+
+ # enable multiCall extension by default
+ ENABLE_MULTICALL = false
+
+ # enable Introspection extension by default
+ ENABLE_INTROSPECTION = false
+
+ end
+
+end
+
diff --git a/ruby/lib/xmlrpc/create.rb b/ruby/lib/xmlrpc/create.rb
new file mode 100644
index 0000000..31fdfb4
--- /dev/null
+++ b/ruby/lib/xmlrpc/create.rb
@@ -0,0 +1,290 @@
+#
+# Creates XML-RPC call/response documents
+#
+# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
+#
+# $Id: create.rb 19657 2008-10-01 13:46:53Z mame $
+#
+
+require "date"
+require "xmlrpc/base64"
+
+module XMLRPC
+
+ module XMLWriter
+
+ class Abstract
+ def ele(name, *children)
+ element(name, nil, *children)
+ end
+
+ def tag(name, txt)
+ element(name, nil, text(txt))
+ end
+ end
+
+
+ class Simple < Abstract
+
+ def document_to_str(doc)
+ doc
+ end
+
+ def document(*params)
+ params.join("")
+ end
+
+ def pi(name, *params)
+ "<?#{name} " + params.join(" ") + " ?>"
+ end
+
+ def element(name, attrs, *children)
+ raise "attributes not yet implemented" unless attrs.nil?
+ if children.empty?
+ "<#{name}/>"
+ else
+ "<#{name}>" + children.join("") + "</#{name}>"
+ end
+ end
+
+ def text(txt)
+ cleaned = txt.dup
+ cleaned.gsub!(/&/, '&amp;')
+ cleaned.gsub!(/</, '&lt;')
+ cleaned.gsub!(/>/, '&gt;')
+ cleaned
+ end
+
+ end # class Simple
+
+
+ class XMLParser < Abstract
+
+ def initialize
+ require "xmltreebuilder"
+ end
+
+ def document_to_str(doc)
+ doc.to_s
+ end
+
+ def document(*params)
+ XML::SimpleTree::Document.new(*params)
+ end
+
+ def pi(name, *params)
+ XML::SimpleTree::ProcessingInstruction.new(name, *params)
+ end
+
+ def element(name, attrs, *children)
+ XML::SimpleTree::Element.new(name, attrs, *children)
+ end
+
+ def text(txt)
+ XML::SimpleTree::Text.new(txt)
+ end
+
+ end # class XMLParser
+
+ Classes = [Simple, XMLParser]
+
+ # yields an instance of each installed XML writer
+ def self.each_installed_writer
+ XMLRPC::XMLWriter::Classes.each do |klass|
+ begin
+ yield klass.new
+ rescue LoadError
+ end
+ end
+ end
+
+ end # module XMLWriter
+
+ class Create
+
+ def initialize(xml_writer = nil)
+ @writer = xml_writer || Config::DEFAULT_WRITER.new
+ end
+
+
+ def methodCall(name, *params)
+ name = name.to_s
+
+ if name !~ /[a-zA-Z0-9_.:\/]+/
+ raise ArgumentError, "Wrong XML-RPC method-name"
+ end
+
+ parameter = params.collect do |param|
+ @writer.ele("param", conv2value(param))
+ end
+
+ tree = @writer.document(
+ @writer.pi("xml", 'version="1.0"'),
+ @writer.ele("methodCall",
+ @writer.tag("methodName", name),
+ @writer.ele("params", *parameter)
+ )
+ )
+
+ @writer.document_to_str(tree) + "\n"
+ end
+
+
+
+ #
+ # generates a XML-RPC methodResponse document
+ #
+ # if is_ret == false then the params array must
+ # contain only one element, which is a structure
+ # of a fault return-value.
+ #
+ # if is_ret == true then a normal
+ # return-value of all the given params is created.
+ #
+ def methodResponse(is_ret, *params)
+
+ if is_ret
+ resp = params.collect do |param|
+ @writer.ele("param", conv2value(param))
+ end
+
+ resp = [@writer.ele("params", *resp)]
+ else
+ if params.size != 1 or params[0] === XMLRPC::FaultException
+ raise ArgumentError, "no valid fault-structure given"
+ end
+ resp = @writer.ele("fault", conv2value(params[0].to_h))
+ end
+
+
+ tree = @writer.document(
+ @writer.pi("xml", 'version="1.0"'),
+ @writer.ele("methodResponse", resp)
+ )
+
+ @writer.document_to_str(tree) + "\n"
+ end
+
+
+
+ #####################################
+ private
+ #####################################
+
+ #
+ # converts a Ruby object into
+ # a XML-RPC <value> tag
+ #
+ def conv2value(param)
+
+ val = case param
+ when Fixnum
+ @writer.tag("i4", param.to_s)
+
+ when Bignum
+ if Config::ENABLE_BIGINT
+ @writer.tag("i4", param.to_s)
+ else
+ if param >= -(2**31) and param <= (2**31-1)
+ @writer.tag("i4", param.to_s)
+ else
+ raise "Bignum is too big! Must be signed 32-bit integer!"
+ end
+ end
+ when TrueClass, FalseClass
+ @writer.tag("boolean", param ? "1" : "0")
+
+ when Symbol
+ @writer.tag("string", param.to_s)
+
+ when String
+ @writer.tag("string", param)
+
+ when NilClass
+ if Config::ENABLE_NIL_CREATE
+ @writer.ele("nil")
+ else
+ raise "Wrong type NilClass. Not allowed!"
+ end
+
+ when Float
+ @writer.tag("double", param.to_s)
+
+ when Struct
+ h = param.members.collect do |key|
+ value = param[key]
+ @writer.ele("member",
+ @writer.tag("name", key.to_s),
+ conv2value(value)
+ )
+ end
+
+ @writer.ele("struct", *h)
+
+ when Hash
+ # TODO: can a Hash be empty?
+
+ h = param.collect do |key, value|
+ @writer.ele("member",
+ @writer.tag("name", key.to_s),
+ conv2value(value)
+ )
+ end
+
+ @writer.ele("struct", *h)
+
+ when Array
+ # TODO: can an Array be empty?
+ a = param.collect {|v| conv2value(v) }
+
+ @writer.ele("array",
+ @writer.ele("data", *a)
+ )
+
+ when Time, Date, ::DateTime
+ @writer.tag("dateTime.iso8601", param.strftime("%Y%m%dT%H:%M:%S"))
+
+ when XMLRPC::DateTime
+ @writer.tag("dateTime.iso8601",
+ format("%.4d%02d%02dT%02d:%02d:%02d", *param.to_a))
+
+ when XMLRPC::Base64
+ @writer.tag("base64", param.encoded)
+
+ else
+ if Config::ENABLE_MARSHALLING and param.class.included_modules.include? XMLRPC::Marshallable
+ # convert Ruby object into Hash
+ ret = {"___class___" => param.class.name}
+ param.instance_variables.each {|v|
+ name = v[1..-1]
+ val = param.instance_variable_get(v)
+
+ if val.nil?
+ ret[name] = val if Config::ENABLE_NIL_CREATE
+ else
+ ret[name] = val
+ end
+ }
+ return conv2value(ret)
+ else
+ ok, pa = wrong_type(param)
+ if ok
+ return conv2value(pa)
+ else
+ raise "Wrong type!"
+ end
+ end
+ end
+
+ @writer.ele("value", val)
+ end
+
+ def wrong_type(value)
+ false
+ end
+
+
+ end # class Create
+
+end # module XMLRPC
+
diff --git a/ruby/lib/xmlrpc/datetime.rb b/ruby/lib/xmlrpc/datetime.rb
new file mode 100644
index 0000000..b6e7a5a
--- /dev/null
+++ b/ruby/lib/xmlrpc/datetime.rb
@@ -0,0 +1,142 @@
+=begin
+= xmlrpc/datetime.rb
+Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
+
+Released under the same term of license as Ruby.
+
+= Classes
+* ((<XMLRPC::DateTime>))
+
+= XMLRPC::DateTime
+== Description
+This class is important to handle XMLRPC (('dateTime.iso8601')) values,
+correcly, because normal UNIX-dates (class (({Date}))) only handle dates
+from year 1970 on, and class (({Time})) handles dates without the time
+component. (({XMLRPC::DateTime})) is able to store a XMLRPC
+(('dateTime.iso8601')) value correctly.
+
+== Class Methods
+--- XMLRPC::DateTime.new( year, month, day, hour, min, sec )
+ Creates a new (({XMLRPC::DateTime})) instance with the
+ parameters ((|year|)), ((|month|)), ((|day|)) as date and
+ ((|hour|)), ((|min|)), ((|sec|)) as time.
+ Raises (({ArgumentError})) if a parameter is out of range, or ((|year|)) is not
+ of type (({Integer})).
+
+== Instance Methods
+--- XMLRPC::DateTime#year
+--- XMLRPC::DateTime#month
+--- XMLRPC::DateTime#day
+--- XMLRPC::DateTime#hour
+--- XMLRPC::DateTime#min
+--- XMLRPC::DateTime#sec
+ Return the value of the specified date/time component.
+
+--- XMLRPC::DateTime#mon
+ Alias for ((<XMLRPC::DateTime#month>)).
+
+--- XMLRPC::DateTime#year=( value )
+--- XMLRPC::DateTime#month=( value )
+--- XMLRPC::DateTime#day=( value )
+--- XMLRPC::DateTime#hour=( value )
+--- XMLRPC::DateTime#min=( value )
+--- XMLRPC::DateTime#sec=( value )
+ Set ((|value|)) as the new date/time component.
+ Raises (({ArgumentError})) if ((|value|)) is out of range, or in the case
+ of (({XMLRPC::DateTime#year=})) if ((|value|)) is not of type (({Integer})).
+
+--- XMLRPC::DateTime#mon=( value )
+ Alias for ((<XMLRPC::DateTime#month=>)).
+
+--- XMLRPC::DateTime#to_time
+ Return a (({Time})) object of the date/time which (({self})) represents.
+ If the (('year')) is below 1970, this method returns (({nil})),
+ because (({Time})) cannot handle years below 1970.
+ The used timezone is GMT.
+
+--- XMLRPC::DateTime#to_date
+ Return a (({Date})) object of the date which (({self})) represents.
+ The (({Date})) object do ((*not*)) contain the time component (only date).
+
+--- XMLRPC::DateTime#to_a
+ Returns all date/time components in an array.
+ Returns (({[year, month, day, hour, min, sec]})).
+=end
+
+require "date"
+
+module XMLRPC
+
+class DateTime
+
+ attr_reader :year, :month, :day, :hour, :min, :sec
+
+ def year= (value)
+ raise ArgumentError, "date/time out of range" unless value.is_a? Integer
+ @year = value
+ end
+
+ def month= (value)
+ raise ArgumentError, "date/time out of range" unless (1..12).include? value
+ @month = value
+ end
+
+ def day= (value)
+ raise ArgumentError, "date/time out of range" unless (1..31).include? value
+ @day = value
+ end
+
+ def hour= (value)
+ raise ArgumentError, "date/time out of range" unless (0..24).include? value
+ @hour = value
+ end
+
+ def min= (value)
+ raise ArgumentError, "date/time out of range" unless (0..59).include? value
+ @min = value
+ end
+
+ def sec= (value)
+ raise ArgumentError, "date/time out of range" unless (0..59).include? value
+ @sec = value
+ end
+
+ alias mon month
+ alias mon= month=
+
+
+ def initialize(year, month, day, hour, min, sec)
+ self.year, self.month, self.day = year, month, day
+ self.hour, self.min, self.sec = hour, min, sec
+ end
+
+ def to_time
+ if @year >= 1970
+ Time.gm(*to_a)
+ else
+ nil
+ end
+ end
+
+ def to_date
+ Date.new(*to_a[0,3])
+ end
+
+ def to_a
+ [@year, @month, @day, @hour, @min, @sec]
+ end
+
+ def ==(o)
+ self.to_a == Array(o) rescue false
+ end
+
+end
+
+
+end # module XMLRPC
+
+
+=begin
+= History
+ $Id: datetime.rb 11708 2007-02-12 23:01:19Z shyouhei $
+=end
diff --git a/ruby/lib/xmlrpc/httpserver.rb b/ruby/lib/xmlrpc/httpserver.rb
new file mode 100644
index 0000000..1e3412b
--- /dev/null
+++ b/ruby/lib/xmlrpc/httpserver.rb
@@ -0,0 +1,178 @@
+#
+# Implements a simple HTTP-server by using John W. Small's (jsmall@laser.net)
+# ruby-generic-server.
+#
+# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
+#
+# $Id: httpserver.rb 19657 2008-10-01 13:46:53Z mame $
+#
+
+
+require "gserver"
+
+class HttpServer < GServer
+
+ ##
+ # handle_obj specifies the object, that receives calls to request_handler
+ # and ip_auth_handler
+ def initialize(handle_obj, port = 8080, host = DEFAULT_HOST, maxConnections = 4,
+ stdlog = $stdout, audit = true, debug = true)
+ @handler = handle_obj
+ super(port, host, maxConnections, stdlog, audit, debug)
+ end
+
+private
+
+ # Constants -----------------------------------------------
+
+ CRLF = "\r\n"
+ HTTP_PROTO = "HTTP/1.0"
+ SERVER_NAME = "HttpServer (Ruby #{RUBY_VERSION})"
+
+ DEFAULT_HEADER = {
+ "Server" => SERVER_NAME
+ }
+
+ ##
+ # Mapping of status code and error message
+ #
+ StatusCodeMapping = {
+ 200 => "OK",
+ 400 => "Bad Request",
+ 403 => "Forbidden",
+ 405 => "Method Not Allowed",
+ 411 => "Length Required",
+ 500 => "Internal Server Error"
+ }
+
+ # Classes -------------------------------------------------
+
+ class Request
+ attr_reader :data, :header, :method, :path, :proto
+
+ def initialize(data, method=nil, path=nil, proto=nil)
+ @header, @data = Table.new, data
+ @method, @path, @proto = method, path, proto
+ end
+
+ def content_length
+ len = @header['Content-Length']
+ return nil if len.nil?
+ return len.to_i
+ end
+
+ end
+
+ class Response
+ attr_reader :header
+ attr_accessor :body, :status, :status_message
+
+ def initialize(status=200)
+ @status = status
+ @status_message = nil
+ @header = Table.new
+ end
+ end
+
+
+ ##
+ # a case-insensitive Hash class for HTTP header
+ #
+ class Table
+ include Enumerable
+
+ def initialize(hash={})
+ @hash = hash
+ update(hash)
+ end
+
+ def [](key)
+ @hash[key.to_s.capitalize]
+ end
+
+ def []=(key, value)
+ @hash[key.to_s.capitalize] = value
+ end
+
+ def update(hash)
+ hash.each {|k,v| self[k] = v}
+ self
+ end
+
+ def each
+ @hash.each {|k,v| yield k.capitalize, v }
+ end
+
+ def writeTo(port)
+ each { |k,v| port << "#{k}: #{v}" << CRLF }
+ end
+ end # class Table
+
+
+ # Helper Methods ------------------------------------------
+
+ def http_header(header=nil)
+ new_header = Table.new(DEFAULT_HEADER)
+ new_header.update(header) unless header.nil?
+
+ new_header["Connection"] = "close"
+ new_header["Date"] = http_date(Time.now)
+
+ new_header
+ end
+
+ def http_date( aTime )
+ aTime.gmtime.strftime( "%a, %d %b %Y %H:%M:%S GMT" )
+ end
+
+ def http_resp(status_code, status_message=nil, header=nil, body=nil)
+ status_message ||= StatusCodeMapping[status_code]
+
+ str = ""
+ str << "#{HTTP_PROTO} #{status_code} #{status_message}" << CRLF
+ http_header(header).writeTo(str)
+ str << CRLF
+ str << body unless body.nil?
+ str
+ end
+
+ # Main Serve Loop -----------------------------------------
+
+ def serve(io)
+ # perform IP authentification
+ unless @handler.ip_auth_handler(io)
+ io << http_resp(403, "Forbidden")
+ return
+ end
+
+ # parse first line
+ if io.gets =~ /^(\S+)\s+(\S+)\s+(\S+)/
+ request = Request.new(io, $1, $2, $3)
+ else
+ io << http_resp(400, "Bad Request")
+ return
+ end
+
+ # parse HTTP headers
+ while (line=io.gets) !~ /^(\n|\r)/
+ if line =~ /^([\w-]+):\s*(.*)$/
+ request.header[$1] = $2.strip
+ end
+ end
+
+ io.binmode
+ response = Response.new
+
+ # execute request handler
+ @handler.request_handler(request, response)
+
+ # write response back to the client
+ io << http_resp(response.status, response.status_message,
+ response.header, response.body)
+
+ rescue Exception => e
+ io << http_resp(500, "Internal Server Error")
+ end
+
+end # class HttpServer
+
diff --git a/ruby/lib/xmlrpc/marshal.rb b/ruby/lib/xmlrpc/marshal.rb
new file mode 100644
index 0000000..ba8c720
--- /dev/null
+++ b/ruby/lib/xmlrpc/marshal.rb
@@ -0,0 +1,76 @@
+#
+# Marshalling of XML-RPC methodCall and methodResponse
+#
+# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
+#
+# $Id: marshal.rb 11708 2007-02-12 23:01:19Z shyouhei $
+#
+
+require "xmlrpc/parser"
+require "xmlrpc/create"
+require "xmlrpc/config"
+require "xmlrpc/utils"
+
+module XMLRPC
+
+ class Marshal
+ include ParserWriterChooseMixin
+
+ # class methods -------------------------------
+
+ class << self
+
+ def dump_call( methodName, *params )
+ new.dump_call( methodName, *params )
+ end
+
+ def dump_response( param )
+ new.dump_response( param )
+ end
+
+ def load_call( stringOrReadable )
+ new.load_call( stringOrReadable )
+ end
+
+ def load_response( stringOrReadable )
+ new.load_response( stringOrReadable )
+ end
+
+ alias dump dump_response
+ alias load load_response
+
+ end # class self
+
+ # instance methods ----------------------------
+
+ def initialize( parser = nil, writer = nil )
+ set_parser( parser )
+ set_writer( writer )
+ end
+
+ def dump_call( methodName, *params )
+ create.methodCall( methodName, *params )
+ end
+
+ def dump_response( param )
+ create.methodResponse( ! param.kind_of?( XMLRPC::FaultException ) , param )
+ end
+
+ ##
+ # returns [ methodname, params ]
+ #
+ def load_call( stringOrReadable )
+ parser.parseMethodCall( stringOrReadable )
+ end
+
+ ##
+ # returns paramOrFault
+ #
+ def load_response( stringOrReadable )
+ parser.parseMethodResponse( stringOrReadable )[1]
+ end
+
+ end # class Marshal
+
+end
+
diff --git a/ruby/lib/xmlrpc/parser.rb b/ruby/lib/xmlrpc/parser.rb
new file mode 100644
index 0000000..23ecc62
--- /dev/null
+++ b/ruby/lib/xmlrpc/parser.rb
@@ -0,0 +1,813 @@
+#
+# Parser for XML-RPC call and response
+#
+# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
+#
+# $Id: parser.rb 19657 2008-10-01 13:46:53Z mame $
+#
+
+
+require "date"
+require "xmlrpc/base64"
+require "xmlrpc/datetime"
+
+
+# add some methods to NQXML::Node
+module NQXML
+ class Node
+
+ def removeChild(node)
+ @children.delete(node)
+ end
+ def childNodes
+ @children
+ end
+ def hasChildNodes
+ not @children.empty?
+ end
+ def [] (index)
+ @children[index]
+ end
+
+ def nodeType
+ if @entity.instance_of? NQXML::Text then :TEXT
+ elsif @entity.instance_of? NQXML::Comment then :COMMENT
+ #elsif @entity.instance_of? NQXML::Element then :ELEMENT
+ elsif @entity.instance_of? NQXML::Tag then :ELEMENT
+ else :ELSE
+ end
+ end
+
+ def nodeValue
+ #TODO: error when wrong Entity-type
+ @entity.text
+ end
+ def nodeName
+ #TODO: error when wrong Entity-type
+ @entity.name
+ end
+ end # class Node
+end # module NQXML
+
+module XMLRPC
+
+ class FaultException < StandardError
+ attr_reader :faultCode, :faultString
+
+ alias message faultString
+
+ def initialize(faultCode, faultString)
+ @faultCode = faultCode
+ @faultString = faultString
+ end
+
+ # returns a hash
+ def to_h
+ {"faultCode" => @faultCode, "faultString" => @faultString}
+ end
+ end
+
+ module Convert
+ def self.int(str)
+ str.to_i
+ end
+
+ def self.boolean(str)
+ case str
+ when "0" then false
+ when "1" then true
+ else
+ raise "RPC-value of type boolean is wrong"
+ end
+ end
+
+ def self.double(str)
+ str.to_f
+ end
+
+ def self.dateTime(str)
+ case str
+ when /^(-?\d\d\d\d)-?(\d\d)-?(\d\d)T(\d\d):(\d\d):(\d\d)(?:Z|([+-])(\d\d):?(\d\d))?$/
+ a = [$1, $2, $3, $4, $5, $6].collect{|i| i.to_i}
+ if $7
+ ofs = $8.to_i*3600 + $9.to_i*60
+ ofs = -ofs if $7=='+'
+ utc = Time.utc(*a) + ofs
+ a = [ utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec ]
+ end
+ XMLRPC::DateTime.new(*a)
+ when /^(-?\d\d)-?(\d\d)-?(\d\d)T(\d\d):(\d\d):(\d\d)(Z|([+-]\d\d):(\d\d))?$/
+ a = [$1, $2, $3, $4, $5, $6].collect{|i| i.to_i}
+ if a[0] < 70
+ a[0] += 2000
+ else
+ a[0] += 1900
+ end
+ if $7
+ ofs = $8.to_i*3600 + $9.to_i*60
+ ofs = -ofs if $7=='+'
+ utc = Time.utc(*a) + ofs
+ a = [ utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec ]
+ end
+ XMLRPC::DateTime.new(*a)
+ else
+ raise "wrong dateTime.iso8601 format " + str
+ end
+ end
+
+ def self.base64(str)
+ XMLRPC::Base64.decode(str)
+ end
+
+ def self.struct(hash)
+ # convert to marhalled object
+ klass = hash["___class___"]
+ if klass.nil? or Config::ENABLE_MARSHALLING == false
+ hash
+ else
+ begin
+ mod = Module
+ klass.split("::").each {|const| mod = mod.const_get(const.strip)}
+
+ obj = mod.allocate
+
+ hash.delete "___class___"
+ hash.each {|key, value|
+ obj.instance_variable_set("@#{ key }", value) if key =~ /^([\w_][\w_0-9]*)$/
+ }
+ obj
+ rescue
+ hash
+ end
+ end
+ end
+
+ def self.fault(hash)
+ if hash.kind_of? Hash and hash.size == 2 and
+ hash.has_key? "faultCode" and hash.has_key? "faultString" and
+ hash["faultCode"].kind_of? Integer and hash["faultString"].kind_of? String
+
+ XMLRPC::FaultException.new(hash["faultCode"], hash["faultString"])
+ else
+ raise "wrong fault-structure: #{hash.inspect}"
+ end
+ end
+
+ end # module Convert
+
+ module XMLParser
+
+ class AbstractTreeParser
+
+ def parseMethodResponse(str)
+ methodResponse_document(createCleanedTree(str))
+ end
+
+ def parseMethodCall(str)
+ methodCall_document(createCleanedTree(str))
+ end
+
+ private
+
+ #
+ # remove all whitespaces but in the tags i4, int, boolean....
+ # and all comments
+ #
+ def removeWhitespacesAndComments(node)
+ remove = []
+ childs = node.childNodes.to_a
+ childs.each do |nd|
+ case _nodeType(nd)
+ when :TEXT
+ # TODO: add nil?
+ unless %w(i4 int boolean string double dateTime.iso8601 base64).include? node.nodeName
+
+ if node.nodeName == "value"
+ if not node.childNodes.to_a.detect {|n| _nodeType(n) == :ELEMENT}.nil?
+ remove << nd if nd.nodeValue.strip == ""
+ end
+ else
+ remove << nd if nd.nodeValue.strip == ""
+ end
+ end
+ when :COMMENT
+ remove << nd
+ else
+ removeWhitespacesAndComments(nd)
+ end
+ end
+
+ remove.each { |i| node.removeChild(i) }
+ end
+
+
+ def nodeMustBe(node, name)
+ cmp = case name
+ when Array
+ name.include?(node.nodeName)
+ when String
+ name == node.nodeName
+ else
+ raise "error"
+ end
+
+ if not cmp then
+ raise "wrong xml-rpc (name)"
+ end
+
+ node
+ end
+
+ #
+ # returns, when successfully the only child-node
+ #
+ def hasOnlyOneChild(node, name=nil)
+ if node.childNodes.to_a.size != 1
+ raise "wrong xml-rpc (size)"
+ end
+ if name != nil then
+ nodeMustBe(node.firstChild, name)
+ end
+ end
+
+
+ def assert(b)
+ if not b then
+ raise "assert-fail"
+ end
+ end
+
+ # the node `node` has empty string or string
+ def text_zero_one(node)
+ nodes = node.childNodes.to_a.size
+
+ if nodes == 1
+ text(node.firstChild)
+ elsif nodes == 0
+ ""
+ else
+ raise "wrong xml-rpc (size)"
+ end
+ end
+
+
+ def integer(node)
+ #TODO: check string for float because to_i returnsa
+ # 0 when wrong string
+ nodeMustBe(node, %w(i4 int))
+ hasOnlyOneChild(node)
+
+ Convert.int(text(node.firstChild))
+ end
+
+ def boolean(node)
+ nodeMustBe(node, "boolean")
+ hasOnlyOneChild(node)
+
+ Convert.boolean(text(node.firstChild))
+ end
+
+ def v_nil(node)
+ nodeMustBe(node, "nil")
+ assert( node.childNodes.to_a.size == 0 )
+ nil
+ end
+
+ def string(node)
+ nodeMustBe(node, "string")
+ text_zero_one(node)
+ end
+
+ def double(node)
+ #TODO: check string for float because to_f returnsa
+ # 0.0 when wrong string
+ nodeMustBe(node, "double")
+ hasOnlyOneChild(node)
+
+ Convert.double(text(node.firstChild))
+ end
+
+ def dateTime(node)
+ nodeMustBe(node, "dateTime.iso8601")
+ hasOnlyOneChild(node)
+
+ Convert.dateTime( text(node.firstChild) )
+ end
+
+ def base64(node)
+ nodeMustBe(node, "base64")
+ #hasOnlyOneChild(node)
+
+ Convert.base64(text_zero_one(node))
+ end
+
+ def member(node)
+ nodeMustBe(node, "member")
+ assert( node.childNodes.to_a.size == 2 )
+
+ [ name(node[0]), value(node[1]) ]
+ end
+
+ def name(node)
+ nodeMustBe(node, "name")
+ #hasOnlyOneChild(node)
+ text_zero_one(node)
+ end
+
+ def array(node)
+ nodeMustBe(node, "array")
+ hasOnlyOneChild(node, "data")
+ data(node.firstChild)
+ end
+
+ def data(node)
+ nodeMustBe(node, "data")
+
+ node.childNodes.to_a.collect do |val|
+ value(val)
+ end
+ end
+
+ def param(node)
+ nodeMustBe(node, "param")
+ hasOnlyOneChild(node, "value")
+ value(node.firstChild)
+ end
+
+ def methodResponse(node)
+ nodeMustBe(node, "methodResponse")
+ hasOnlyOneChild(node, %w(params fault))
+ child = node.firstChild
+
+ case child.nodeName
+ when "params"
+ [ true, params(child,false) ]
+ when "fault"
+ [ false, fault(child) ]
+ else
+ raise "unexpected error"
+ end
+
+ end
+
+ def methodName(node)
+ nodeMustBe(node, "methodName")
+ hasOnlyOneChild(node)
+ text(node.firstChild)
+ end
+
+ def params(node, call=true)
+ nodeMustBe(node, "params")
+
+ if call
+ node.childNodes.to_a.collect do |n|
+ param(n)
+ end
+ else # response (only one param)
+ hasOnlyOneChild(node)
+ param(node.firstChild)
+ end
+ end
+
+ def fault(node)
+ nodeMustBe(node, "fault")
+ hasOnlyOneChild(node, "value")
+ f = value(node.firstChild)
+ Convert.fault(f)
+ end
+
+
+
+ # _nodeType is defined in the subclass
+ def text(node)
+ assert( _nodeType(node) == :TEXT )
+ assert( node.hasChildNodes == false )
+ assert( node.nodeValue != nil )
+
+ node.nodeValue.to_s
+ end
+
+ def struct(node)
+ nodeMustBe(node, "struct")
+
+ hash = {}
+ node.childNodes.to_a.each do |me|
+ n, v = member(me)
+ hash[n] = v
+ end
+
+ Convert.struct(hash)
+ end
+
+
+ def value(node)
+ nodeMustBe(node, "value")
+ nodes = node.childNodes.to_a.size
+ if nodes == 0
+ return ""
+ elsif nodes > 1
+ raise "wrong xml-rpc (size)"
+ end
+
+ child = node.firstChild
+
+ case _nodeType(child)
+ when :TEXT
+ text_zero_one(node)
+ when :ELEMENT
+ case child.nodeName
+ when "i4", "int" then integer(child)
+ when "boolean" then boolean(child)
+ when "string" then string(child)
+ when "double" then double(child)
+ when "dateTime.iso8601" then dateTime(child)
+ when "base64" then base64(child)
+ when "struct" then struct(child)
+ when "array" then array(child)
+ when "nil"
+ if Config::ENABLE_NIL_PARSER
+ v_nil(child)
+ else
+ raise "wrong/unknown XML-RPC type 'nil'"
+ end
+ else
+ raise "wrong/unknown XML-RPC type"
+ end
+ else
+ raise "wrong type of node"
+ end
+
+ end
+
+ def methodCall(node)
+ nodeMustBe(node, "methodCall")
+ assert( (1..2).include?( node.childNodes.to_a.size ) )
+ name = methodName(node[0])
+
+ if node.childNodes.to_a.size == 2 then
+ pa = params(node[1])
+ else # no parameters given
+ pa = []
+ end
+ [name, pa]
+ end
+
+ end # module TreeParserMixin
+
+ class AbstractStreamParser
+ def parseMethodResponse(str)
+ parser = @parser_class.new
+ parser.parse(str)
+ raise "No valid method response!" if parser.method_name != nil
+ if parser.fault != nil
+ # is a fault structure
+ [false, parser.fault]
+ else
+ # is a normal return value
+ raise "Missing return value!" if parser.params.size == 0
+ raise "Too many return values. Only one allowed!" if parser.params.size > 1
+ [true, parser.params[0]]
+ end
+ end
+
+ def parseMethodCall(str)
+ parser = @parser_class.new
+ parser.parse(str)
+ raise "No valid method call - missing method name!" if parser.method_name.nil?
+ [parser.method_name, parser.params]
+ end
+ end
+
+ module StreamParserMixin
+ attr_reader :params
+ attr_reader :method_name
+ attr_reader :fault
+
+ def initialize(*a)
+ super(*a)
+ @params = []
+ @values = []
+ @val_stack = []
+
+ @names = []
+ @name = []
+
+ @structs = []
+ @struct = {}
+
+ @method_name = nil
+ @fault = nil
+
+ @data = nil
+ end
+
+ def startElement(name, attrs=[])
+ @data = nil
+ case name
+ when "value"
+ @value = nil
+ when "nil"
+ raise "wrong/unknown XML-RPC type 'nil'" unless Config::ENABLE_NIL_PARSER
+ @value = :nil
+ when "array"
+ @val_stack << @values
+ @values = []
+ when "struct"
+ @names << @name
+ @name = []
+
+ @structs << @struct
+ @struct = {}
+ end
+ end
+
+ def endElement(name)
+ @data ||= ""
+ case name
+ when "string"
+ @value = @data
+ when "i4", "int"
+ @value = Convert.int(@data)
+ when "boolean"
+ @value = Convert.boolean(@data)
+ when "double"
+ @value = Convert.double(@data)
+ when "dateTime.iso8601"
+ @value = Convert.dateTime(@data)
+ when "base64"
+ @value = Convert.base64(@data)
+ when "value"
+ @value = @data if @value.nil?
+ @values << (@value == :nil ? nil : @value)
+ when "array"
+ @value = @values
+ @values = @val_stack.pop
+ when "struct"
+ @value = Convert.struct(@struct)
+
+ @name = @names.pop
+ @struct = @structs.pop
+ when "name"
+ @name[0] = @data
+ when "member"
+ @struct[@name[0]] = @values.pop
+
+ when "param"
+ @params << @values[0]
+ @values = []
+
+ when "fault"
+ @fault = Convert.fault(@values[0])
+
+ when "methodName"
+ @method_name = @data
+ end
+
+ @data = nil
+ end
+
+ def character(data)
+ if @data
+ @data << data
+ else
+ @data = data
+ end
+ end
+
+ end # module StreamParserMixin
+
+ # ---------------------------------------------------------------------------
+ class XMLStreamParser < AbstractStreamParser
+ def initialize
+ require "xmlparser"
+ @parser_class = Class.new(::XMLParser) {
+ include StreamParserMixin
+ }
+ end
+ end # class XMLStreamParser
+ # ---------------------------------------------------------------------------
+ class NQXMLStreamParser < AbstractStreamParser
+ def initialize
+ require "nqxml/streamingparser"
+ @parser_class = XMLRPCParser
+ end
+
+ class XMLRPCParser
+ include StreamParserMixin
+
+ def parse(str)
+ parser = NQXML::StreamingParser.new(str)
+ parser.each do |ele|
+ case ele
+ when NQXML::Text
+ @data = ele.text
+ #character(ele.text)
+ when NQXML::Tag
+ if ele.isTagEnd
+ endElement(ele.name)
+ else
+ startElement(ele.name, ele.attrs)
+ end
+ end
+ end # do
+ end # method parse
+ end # class XMLRPCParser
+
+ end # class NQXMLStreamParser
+ # ---------------------------------------------------------------------------
+ class XMLTreeParser < AbstractTreeParser
+
+ def initialize
+ require "xmltreebuilder"
+
+ # The new XMLParser library (0.6.2+) uses a slightly different DOM implementation.
+ # The following code removes the differences between both versions.
+ if defined? XML::DOM::Builder
+ return if defined? XML::DOM::Node::DOCUMENT # code below has been already executed
+ klass = XML::DOM::Node
+ klass.const_set("DOCUMENT", klass::DOCUMENT_NODE)
+ klass.const_set("TEXT", klass::TEXT_NODE)
+ klass.const_set("COMMENT", klass::COMMENT_NODE)
+ klass.const_set("ELEMENT", klass::ELEMENT_NODE)
+ end
+ end
+
+ private
+
+ def _nodeType(node)
+ tp = node.nodeType
+ if tp == XML::SimpleTree::Node::TEXT then :TEXT
+ elsif tp == XML::SimpleTree::Node::COMMENT then :COMMENT
+ elsif tp == XML::SimpleTree::Node::ELEMENT then :ELEMENT
+ else :ELSE
+ end
+ end
+
+
+ def methodResponse_document(node)
+ assert( node.nodeType == XML::SimpleTree::Node::DOCUMENT )
+ hasOnlyOneChild(node, "methodResponse")
+
+ methodResponse(node.firstChild)
+ end
+
+ def methodCall_document(node)
+ assert( node.nodeType == XML::SimpleTree::Node::DOCUMENT )
+ hasOnlyOneChild(node, "methodCall")
+
+ methodCall(node.firstChild)
+ end
+
+ def createCleanedTree(str)
+ doc = XML::SimpleTreeBuilder.new.parse(str)
+ doc.documentElement.normalize
+ removeWhitespacesAndComments(doc)
+ doc
+ end
+
+ end # class XMLParser
+ # ---------------------------------------------------------------------------
+ class NQXMLTreeParser < AbstractTreeParser
+
+ def initialize
+ require "nqxml/treeparser"
+ end
+
+ private
+
+ def _nodeType(node)
+ node.nodeType
+ end
+
+ def methodResponse_document(node)
+ methodResponse(node)
+ end
+
+ def methodCall_document(node)
+ methodCall(node)
+ end
+
+ def createCleanedTree(str)
+ doc = ::NQXML::TreeParser.new(str).document.rootNode
+ removeWhitespacesAndComments(doc)
+ doc
+ end
+
+ end # class NQXMLTreeParser
+ # ---------------------------------------------------------------------------
+ class REXMLStreamParser < AbstractStreamParser
+ def initialize
+ require "rexml/document"
+ @parser_class = StreamListener
+ end
+
+ class StreamListener
+ include StreamParserMixin
+
+ alias :tag_start :startElement
+ alias :tag_end :endElement
+ alias :text :character
+ alias :cdata :character
+
+ def method_missing(*a)
+ # ignore
+ end
+
+ def parse(str)
+ parser = REXML::Document.parse_stream(str, self)
+ end
+ end
+
+ end
+ # ---------------------------------------------------------------------------
+ class XMLScanStreamParser < AbstractStreamParser
+ def initialize
+ require "xmlscan/parser"
+ @parser_class = XMLScanParser
+ end
+
+ class XMLScanParser
+ include StreamParserMixin
+
+ Entities = {
+ "lt" => "<",
+ "gt" => ">",
+ "amp" => "&",
+ "quot" => '"',
+ "apos" => "'"
+ }
+
+ def parse(str)
+ parser = XMLScan::XMLParser.new(self)
+ parser.parse(str)
+ end
+
+ alias :on_stag :startElement
+ alias :on_etag :endElement
+
+ def on_stag_end(name); end
+
+ def on_stag_end_empty(name)
+ startElement(name)
+ endElement(name)
+ end
+
+ def on_chardata(str)
+ character(str)
+ end
+
+ def on_cdata(str)
+ character(str)
+ end
+
+ def on_entityref(ent)
+ str = Entities[ent]
+ if str
+ character(str)
+ else
+ raise "unknown entity"
+ end
+ end
+
+ def on_charref(code)
+ character(code.chr)
+ end
+
+ def on_charref_hex(code)
+ character(code.chr)
+ end
+
+ def method_missing(*a)
+ end
+
+ # TODO: call/implement?
+ # valid_name?
+ # valid_chardata?
+ # valid_char?
+ # parse_error
+
+ end
+ end
+ # ---------------------------------------------------------------------------
+ XMLParser = XMLTreeParser
+ NQXMLParser = NQXMLTreeParser
+
+ Classes = [XMLStreamParser, XMLTreeParser,
+ NQXMLStreamParser, NQXMLTreeParser,
+ REXMLStreamParser, XMLScanStreamParser]
+
+ # yields an instance of each installed parser
+ def self.each_installed_parser
+ XMLRPC::XMLParser::Classes.each do |klass|
+ begin
+ yield klass.new
+ rescue LoadError
+ end
+ end
+ end
+
+ end # module XMLParser
+
+
+end # module XMLRPC
+
diff --git a/ruby/lib/xmlrpc/server.rb b/ruby/lib/xmlrpc/server.rb
new file mode 100644
index 0000000..1e5230a
--- /dev/null
+++ b/ruby/lib/xmlrpc/server.rb
@@ -0,0 +1,778 @@
+=begin
+= xmlrpc/server.rb
+Copyright (C) 2001, 2002, 2003, 2005 by Michael Neumann (mneumann@ntecs.de)
+
+Released under the same term of license as Ruby.
+
+= Classes
+* ((<XMLRPC::BasicServer>))
+* ((<XMLRPC::CGIServer>))
+* ((<XMLRPC::ModRubyServer>))
+* ((<XMLRPC::Server>))
+* ((<XMLRPC::WEBrickServlet>))
+
+= XMLRPC::BasicServer
+== Description
+Is the base class for all XML-RPC server-types (CGI, standalone).
+You can add handler and set a default handler.
+Do not use this server, as this is/should be an abstract class.
+
+=== How the method to call is found
+The arity (number of accepted arguments) of a handler (method or (({Proc})) object) is
+compared to the given arguments submitted by the client for a RPC ((-Remote Procedure Call-)).
+A handler is only called if it accepts the number of arguments, otherwise the search
+for another handler will go on. When at the end no handler was found,
+the ((<default_handler|XMLRPC::BasicServer#set_default_handler>)) will be called.
+With this technique it is possible to do overloading by number of parameters, but
+only for (({Proc})) handler, because you cannot define two methods of the same name in
+the same class.
+
+
+== Class Methods
+--- XMLRPC::BasicServer.new( class_delim="." )
+ Creates a new (({XMLRPC::BasicServer})) instance, which should not be
+ done, because (({XMLRPC::BasicServer})) is an abstract class. This
+ method should be called from a subclass indirectly by a (({super})) call
+ in the method (({initialize})). The paramter ((|class_delim|)) is used
+ in ((<add_handler|XMLRPC::BasicServer#add_handler>)) when an object is
+ added as handler, to delimit the object-prefix and the method-name.
+
+== Instance Methods
+--- XMLRPC::BasicServer#add_handler( name, signature=nil, help=nil ) { aBlock }
+ Adds ((|aBlock|)) to the list of handlers, with ((|name|)) as the name of the method.
+ Parameters ((|signature|)) and ((|help|)) are used by the Introspection method if specified,
+ where ((|signature|)) is either an Array containing strings each representing a type of it's
+ signature (the first is the return value) or an Array of Arrays if the method has multiple
+ signatures. Value type-names are "int, boolean, double, string, dateTime.iso8601, base64, array, struct".
+
+ Parameter ((|help|)) is a String with informations about how to call this method etc.
+
+ A handler method or code-block can return the types listed at
+ ((<XMLRPC::Client#call|URL:client.html#index:0>)).
+ When a method fails, it can tell it the client by throwing an
+ (({XMLRPC::FaultException})) like in this example:
+ s.add_handler("michael.div") do |a,b|
+ if b == 0
+ raise XMLRPC::FaultException.new(1, "division by zero")
+ else
+ a / b
+ end
+ end
+ The client gets in the case of (({b==0})) an object back of type
+ (({XMLRPC::FaultException})) that has a ((|faultCode|)) and ((|faultString|))
+ field.
+
+--- XMLRPC::BasicServer#add_handler( prefix, obj )
+ This is the second form of ((<add_handler|XMLRPC::BasicServer#add_handler>)).
+ To add an object write:
+ server.add_handler("michael", MyHandlerClass.new)
+ All public methods of (({MyHandlerClass})) are accessible to
+ the XML-RPC clients by (('michael."name of method"')). This is
+ where the ((|class_delim|)) in ((<new|XMLRPC::BasicServer.new>))
+ has it's role, a XML-RPC method-name is defined by
+ ((|prefix|)) + ((|class_delim|)) + (('"name of method"')).
+
+--- XMLRPC::BasicServer#add_handler( interface, obj )
+ This is the third form of ((<add_handler|XMLRPC::BasicServer#add_handler>)).
+
+ Use (({XMLRPC::interface})) to generate an ServiceInterface object, which
+ represents an interface (with signature and help text) for a handler class.
+
+ Parameter ((|interface|)) must be of type (({XMLRPC::ServiceInterface})).
+ Adds all methods of ((|obj|)) which are defined in ((|interface|)) to the
+ server.
+
+ This is the recommended way of adding services to a server!
+
+
+--- XMLRPC::BasicServer#get_default_handler
+ Returns the default-handler, which is called when no handler for
+ a method-name is found.
+ It is a (({Proc})) object or (({nil})).
+
+--- XMLRPC::BasicServer#set_default_handler ( &handler )
+ Sets ((|handler|)) as the default-handler, which is called when
+ no handler for a method-name is found. ((|handler|)) is a code-block.
+ The default-handler is called with the (XML-RPC) method-name as first argument, and
+ the other arguments are the parameters given by the client-call.
+
+ If no block is specified the default of (({XMLRPC::BasicServer})) is used, which raises a
+ XMLRPC::FaultException saying "method missing".
+
+
+--- XMLRPC::BasicServer#set_writer( writer )
+ Sets the XML writer to use for generating XML output.
+ Should be an instance of a class from module (({XMLRPC::XMLWriter})).
+ If this method is not called, then (({XMLRPC::Config::DEFAULT_WRITER})) is used.
+
+--- XMLRPC::BasicServer#set_parser( parser )
+ Sets the XML parser to use for parsing XML documents.
+ Should be an instance of a class from module (({XMLRPC::XMLParser})).
+ If this method is not called, then (({XMLRPC::Config::DEFAULT_PARSER})) is used.
+
+--- XMLRPC::BasicServer#add_introspection
+ Adds the introspection handlers "system.listMethods", "system.methodSignature" and "system.methodHelp",
+ where only the first one works.
+
+--- XMLRPC::BasicServer#add_multicall
+ Adds the multi-call handler "system.multicall".
+
+--- XMLRPC::BasicServer#get_service_hook
+ Returns the service-hook, which is called on each service request (RPC) unless it's (({nil})).
+
+--- XMLRPC::BasicServer#set_service_hook ( &handler )
+ A service-hook is called for each service request (RPC).
+ You can use a service-hook for example to wrap existing methods and catch exceptions of them or
+ convert values to values recognized by XMLRPC. You can disable it by passing (({nil})) as parameter
+ ((|handler|)) .
+
+ The service-hook is called with a (({Proc})) object and with the parameters for this (({Proc})).
+ An example:
+
+ server.set_service_hook {|obj, *args|
+ begin
+ ret = obj.call(*args) # call the original service-method
+ # could convert the return value
+ resuce
+ # rescue exceptions
+ end
+ }
+
+=end
+
+
+
+require "xmlrpc/parser"
+require "xmlrpc/create"
+require "xmlrpc/config"
+require "xmlrpc/utils" # ParserWriterChooseMixin
+
+
+
+module XMLRPC
+
+
+class BasicServer
+
+ include ParserWriterChooseMixin
+ include ParseContentType
+
+ ERR_METHOD_MISSING = 1
+ ERR_UNCAUGHT_EXCEPTION = 2
+ ERR_MC_WRONG_PARAM = 3
+ ERR_MC_MISSING_PARAMS = 4
+ ERR_MC_MISSING_METHNAME = 5
+ ERR_MC_RECURSIVE_CALL = 6
+ ERR_MC_WRONG_PARAM_PARAMS = 7
+ ERR_MC_EXPECTED_STRUCT = 8
+
+
+ def initialize(class_delim=".")
+ @handler = []
+ @default_handler = nil
+ @service_hook = nil
+
+ @class_delim = class_delim
+ @create = nil
+ @parser = nil
+
+ add_multicall if Config::ENABLE_MULTICALL
+ add_introspection if Config::ENABLE_INTROSPECTION
+ end
+
+ def add_handler(prefix, obj_or_signature=nil, help=nil, &block)
+ if block_given?
+ # proc-handler
+ @handler << [prefix, block, obj_or_signature, help]
+ else
+ if prefix.kind_of? String
+ # class-handler
+ raise ArgumentError, "Expected non-nil value" if obj_or_signature.nil?
+ @handler << [prefix + @class_delim, obj_or_signature]
+ elsif prefix.kind_of? XMLRPC::Service::BasicInterface
+ # class-handler with interface
+ # add all methods
+ @handler += prefix.get_methods(obj_or_signature, @class_delim)
+ else
+ raise ArgumentError, "Wrong type for parameter 'prefix'"
+ end
+ end
+ self
+ end
+
+ def get_service_hook
+ @service_hook
+ end
+
+ def set_service_hook(&handler)
+ @service_hook = handler
+ self
+ end
+
+ def get_default_handler
+ @default_handler
+ end
+
+ def set_default_handler (&handler)
+ @default_handler = handler
+ self
+ end
+
+ def add_multicall
+ add_handler("system.multicall", %w(array array), "Multicall Extension") do |arrStructs|
+ unless arrStructs.is_a? Array
+ raise XMLRPC::FaultException.new(ERR_MC_WRONG_PARAM, "system.multicall expects an array")
+ end
+
+ arrStructs.collect {|call|
+ if call.is_a? Hash
+ methodName = call["methodName"]
+ params = call["params"]
+
+ if params.nil?
+ multicall_fault(ERR_MC_MISSING_PARAMS, "Missing params")
+ elsif methodName.nil?
+ multicall_fault(ERR_MC_MISSING_METHNAME, "Missing methodName")
+ else
+ if methodName == "system.multicall"
+ multicall_fault(ERR_MC_RECURSIVE_CALL, "Recursive system.multicall forbidden")
+ else
+ unless params.is_a? Array
+ multicall_fault(ERR_MC_WRONG_PARAM_PARAMS, "Parameter params have to be an Array")
+ else
+ ok, val = call_method(methodName, *params)
+ if ok
+ # correct return value
+ [val]
+ else
+ # exception
+ multicall_fault(val.faultCode, val.faultString)
+ end
+ end
+ end
+ end
+
+ else
+ multicall_fault(ERR_MC_EXPECTED_STRUCT, "system.multicall expected struct")
+ end
+ }
+ end # end add_handler
+ self
+ end
+
+ def add_introspection
+ add_handler("system.listMethods",%w(array), "List methods available on this XML-RPC server") do
+ methods = []
+ @handler.each do |name, obj|
+ if obj.kind_of? Proc
+ methods << name
+ else
+ obj.class.public_instance_methods(false).each do |meth|
+ methods << "#{name}#{meth}"
+ end
+ end
+ end
+ methods
+ end
+
+ add_handler("system.methodSignature", %w(array string), "Returns method signature") do |meth|
+ sigs = []
+ @handler.each do |name, obj, sig|
+ if obj.kind_of? Proc and sig != nil and name == meth
+ if sig[0].kind_of? Array
+ # sig contains multiple signatures, e.g. [["array"], ["array", "string"]]
+ sig.each {|s| sigs << s}
+ else
+ # sig is a single signature, e.g. ["array"]
+ sigs << sig
+ end
+ end
+ end
+ sigs.uniq! || sigs # remove eventually duplicated signatures
+ end
+
+ add_handler("system.methodHelp", %w(string string), "Returns help on using this method") do |meth|
+ help = nil
+ @handler.each do |name, obj, sig, hlp|
+ if obj.kind_of? Proc and name == meth
+ help = hlp
+ break
+ end
+ end
+ help || ""
+ end
+
+ self
+ end
+
+
+
+ def process(data)
+ method, params = parser().parseMethodCall(data)
+ handle(method, *params)
+ end
+
+ private # --------------------------------------------------------------
+
+ def multicall_fault(nr, str)
+ {"faultCode" => nr, "faultString" => str}
+ end
+
+ #
+ # method dispatch
+ #
+ def dispatch(methodname, *args)
+ for name, obj in @handler
+ if obj.kind_of? Proc
+ next unless methodname == name
+ else
+ next unless methodname =~ /^#{name}(.+)$/
+ next unless obj.respond_to? $1
+ obj = obj.method($1)
+ end
+
+ if check_arity(obj, args.size)
+ if @service_hook.nil?
+ return obj.call(*args)
+ else
+ return @service_hook.call(obj, *args)
+ end
+ end
+ end
+
+ if @default_handler.nil?
+ raise XMLRPC::FaultException.new(ERR_METHOD_MISSING, "Method #{methodname} missing or wrong number of parameters!")
+ else
+ @default_handler.call(methodname, *args)
+ end
+ end
+
+
+ #
+ # returns true, if the arity of "obj" matches
+ #
+ def check_arity(obj, n_args)
+ ary = obj.arity
+
+ if ary >= 0
+ n_args == ary
+ else
+ n_args >= (ary+1).abs
+ end
+ end
+
+
+
+ def call_method(methodname, *args)
+ begin
+ [true, dispatch(methodname, *args)]
+ rescue XMLRPC::FaultException => e
+ [false, e]
+ rescue Exception => e
+ [false, XMLRPC::FaultException.new(ERR_UNCAUGHT_EXCEPTION, "Uncaught exception #{e.message} in method #{methodname}")]
+ end
+ end
+
+ #
+ #
+ #
+ def handle(methodname, *args)
+ create().methodResponse(*call_method(methodname, *args))
+ end
+
+
+end
+
+
+=begin
+= XMLRPC::CGIServer
+== Synopsis
+ require "xmlrpc/server"
+
+ s = XMLRPC::CGIServer.new
+
+ s.add_handler("michael.add") do |a,b|
+ a + b
+ end
+
+ s.add_handler("michael.div") do |a,b|
+ if b == 0
+ raise XMLRPC::FaultException.new(1, "division by zero")
+ else
+ a / b
+ end
+ end
+
+ s.set_default_handler do |name, *args|
+ raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
+ " or wrong number of parameters!")
+ end
+
+ s.serve
+
+== Description
+Implements a CGI-based XML-RPC server.
+
+== Superclass
+((<XMLRPC::BasicServer>))
+
+== Class Methods
+--- XMLRPC::CGIServer.new( *a )
+ Creates a new (({XMLRPC::CGIServer})) instance. All parameters given
+ are by-passed to ((<XMLRPC::BasicServer.new>)). You can only create
+ ((*one*)) (({XMLRPC::CGIServer})) instance, because more than one makes
+ no sense.
+
+== Instance Methods
+--- XMLRPC::CGIServer#serve
+ Call this after you have added all you handlers to the server.
+ This method processes a XML-RPC methodCall and sends the answer
+ back to the client.
+ Make sure that you don't write to standard-output in a handler, or in
+ any other part of your program, this would case a CGI-based server to fail!
+=end
+
+class CGIServer < BasicServer
+ @@obj = nil
+
+ def CGIServer.new(*a)
+ @@obj = super(*a) if @@obj.nil?
+ @@obj
+ end
+
+ def initialize(*a)
+ super(*a)
+ end
+
+ def serve
+ catch(:exit_serve) {
+ length = ENV['CONTENT_LENGTH'].to_i
+
+ http_error(405, "Method Not Allowed") unless ENV['REQUEST_METHOD'] == "POST"
+ http_error(400, "Bad Request") unless parse_content_type(ENV['CONTENT_TYPE']).first == "text/xml"
+ http_error(411, "Length Required") unless length > 0
+
+ # TODO: do we need a call to binmode?
+ $stdin.binmode if $stdin.respond_to? :binmode
+ data = $stdin.read(length)
+
+ http_error(400, "Bad Request") if data.nil? or data.size != length
+
+ http_write(process(data), "Content-type" => "text/xml; charset=utf-8")
+ }
+ end
+
+
+ private
+
+ def http_error(status, message)
+ err = "#{status} #{message}"
+ msg = <<-"MSGEND"
+ <html>
+ <head>
+ <title>#{err}</title>
+ </head>
+ <body>
+ <h1>#{err}</h1>
+ <p>Unexpected error occured while processing XML-RPC request!</p>
+ </body>
+ </html>
+ MSGEND
+
+ http_write(msg, "Status" => err, "Content-type" => "text/html")
+ throw :exit_serve # exit from the #serve method
+ end
+
+ def http_write(body, header)
+ h = {}
+ header.each {|key, value| h[key.to_s.capitalize] = value}
+ h['Status'] ||= "200 OK"
+ h['Content-length'] ||= body.size.to_s
+
+ str = ""
+ h.each {|key, value| str << "#{key}: #{value}\r\n"}
+ str << "\r\n#{body}"
+
+ print str
+ end
+
+end
+
+=begin
+= XMLRPC::ModRubyServer
+== Description
+Implements a XML-RPC server, which works with Apache mod_ruby.
+
+Use it in the same way as CGIServer!
+
+== Superclass
+((<XMLRPC::BasicServer>))
+=end
+
+class ModRubyServer < BasicServer
+
+ def initialize(*a)
+ @ap = Apache::request
+ super(*a)
+ end
+
+ def serve
+ catch(:exit_serve) {
+ header = {}
+ @ap.headers_in.each {|key, value| header[key.capitalize] = value}
+
+ length = header['Content-length'].to_i
+
+ http_error(405, "Method Not Allowed") unless @ap.request_method == "POST"
+ http_error(400, "Bad Request") unless parse_content_type(header['Content-type']).first == "text/xml"
+ http_error(411, "Length Required") unless length > 0
+
+ # TODO: do we need a call to binmode?
+ @ap.binmode
+ data = @ap.read(length)
+
+ http_error(400, "Bad Request") if data.nil? or data.size != length
+
+ http_write(process(data), 200, "Content-type" => "text/xml; charset=utf-8")
+ }
+ end
+
+
+ private
+
+ def http_error(status, message)
+ err = "#{status} #{message}"
+ msg = <<-"MSGEND"
+ <html>
+ <head>
+ <title>#{err}</title>
+ </head>
+ <body>
+ <h1>#{err}</h1>
+ <p>Unexpected error occured while processing XML-RPC request!</p>
+ </body>
+ </html>
+ MSGEND
+
+ http_write(msg, status, "Status" => err, "Content-type" => "text/html")
+ throw :exit_serve # exit from the #serve method
+ end
+
+ def http_write(body, status, header)
+ h = {}
+ header.each {|key, value| h[key.to_s.capitalize] = value}
+ h['Status'] ||= "200 OK"
+ h['Content-length'] ||= body.size.to_s
+
+ h.each {|key, value| @ap.headers_out[key] = value }
+ @ap.content_type = h["Content-type"]
+ @ap.status = status.to_i
+ @ap.send_http_header
+
+ @ap.print body
+ end
+
+end
+
+=begin
+= XMLRPC::Server
+== Synopsis
+ require "xmlrpc/server"
+
+ s = XMLRPC::Server.new(8080)
+
+ s.add_handler("michael.add") do |a,b|
+ a + b
+ end
+
+ s.add_handler("michael.div") do |a,b|
+ if b == 0
+ raise XMLRPC::FaultException.new(1, "division by zero")
+ else
+ a / b
+ end
+ end
+
+ s.set_default_handler do |name, *args|
+ raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
+ " or wrong number of parameters!")
+ end
+
+ s.serve
+
+== Description
+Implements a standalone XML-RPC server. The method (({serve}))) is left if a SIGHUP is sent to the
+program.
+
+== Superclass
+((<XMLRPC::WEBrickServlet>))
+
+== Class Methods
+--- XMLRPC::Server.new( port=8080, host="127.0.0.1", maxConnections=4, stdlog=$stdout, audit=true, debug=true, *a )
+ Creates a new (({XMLRPC::Server})) instance, which is a XML-RPC server listening on
+ port ((|port|)) and accepts requests for the host ((|host|)), which is by default only the localhost.
+ The server is not started, to start it you have to call ((<serve|XMLRPC::Server#serve>)).
+
+ Parameters ((|audit|)) and ((|debug|)) are obsolete!
+
+ All additionally given parameters in ((|*a|)) are by-passed to ((<XMLRPC::BasicServer.new>)).
+
+== Instance Methods
+--- XMLRPC::Server#serve
+ Call this after you have added all you handlers to the server.
+ This method starts the server to listen for XML-RPC requests and answer them.
+
+--- XMLRPC::Server#shutdown
+ Stops and shuts the server down.
+
+=end
+
+class WEBrickServlet < BasicServer; end # forward declaration
+
+class Server < WEBrickServlet
+
+ def initialize(port=8080, host="127.0.0.1", maxConnections=4, stdlog=$stdout, audit=true, debug=true, *a)
+ super(*a)
+ require 'webrick'
+ @server = WEBrick::HTTPServer.new(:Port => port, :BindAddress => host, :MaxClients => maxConnections,
+ :Logger => WEBrick::Log.new(stdlog))
+ @server.mount("/", self)
+ end
+
+ def serve
+ signals = %w[INT TERM HUP] & Signal.list.keys
+ signals.each { |signal| trap(signal) { @server.shutdown } }
+
+ @server.start
+ end
+
+ def shutdown
+ @server.shutdown
+ end
+
+end
+
+=begin
+= XMLRPC::WEBrickServlet
+== Synopsis
+
+ require "webrick"
+ require "xmlrpc/server"
+
+ s = XMLRPC::WEBrickServlet.new
+ s.add_handler("michael.add") do |a,b|
+ a + b
+ end
+
+ s.add_handler("michael.div") do |a,b|
+ if b == 0
+ raise XMLRPC::FaultException.new(1, "division by zero")
+ else
+ a / b
+ end
+ end
+
+ s.set_default_handler do |name, *args|
+ raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
+ " or wrong number of parameters!")
+ end
+
+ httpserver = WEBrick::HTTPServer.new(:Port => 8080)
+ httpserver.mount("/RPC2", s)
+ trap("HUP") { httpserver.shutdown } # use 1 instead of "HUP" on Windows
+ httpserver.start
+
+== Instance Methods
+
+--- XMLRPC::WEBrickServlet#set_valid_ip( *ip_addr )
+ Specifies the valid IP addresses that are allowed to connect to the server.
+ Each IP is either a (({String})) or a (({Regexp})).
+
+--- XMLRPC::WEBrickServlet#get_valid_ip
+ Return the via method ((<set_valid_ip|XMLRPC::Server#set_valid_ip>)) specified
+ valid IP addresses.
+
+== Description
+Implements a servlet for use with WEBrick, a pure Ruby (HTTP-) server framework.
+
+== Superclass
+((<XMLRPC::BasicServer>))
+
+=end
+
+class WEBrickServlet < BasicServer
+ def initialize(*a)
+ super
+ require "webrick/httpstatus"
+ @valid_ip = nil
+ end
+
+ # deprecated from WEBrick/1.2.2.
+ # but does not break anything.
+ def require_path_info?
+ false
+ end
+
+ def get_instance(config, *options)
+ # TODO: set config & options
+ self
+ end
+
+ def set_valid_ip(*ip_addr)
+ if ip_addr.size == 1 and ip_addr[0].nil?
+ @valid_ip = nil
+ else
+ @valid_ip = ip_addr
+ end
+ end
+
+ def get_valid_ip
+ @valid_ip
+ end
+
+ def service(request, response)
+
+ if @valid_ip
+ raise WEBrick::HTTPStatus::Forbidden unless @valid_ip.any? { |ip| request.peeraddr[3] =~ ip }
+ end
+
+ if request.request_method != "POST"
+ raise WEBrick::HTTPStatus::MethodNotAllowed,
+ "unsupported method `#{request.request_method}'."
+ end
+
+ if parse_content_type(request['Content-type']).first != "text/xml"
+ raise WEBrick::HTTPStatus::BadRequest
+ end
+
+ length = (request['Content-length'] || 0).to_i
+
+ raise WEBrick::HTTPStatus::LengthRequired unless length > 0
+
+ data = request.body
+
+ if data.nil? or data.size != length
+ raise WEBrick::HTTPStatus::BadRequest
+ end
+
+ resp = process(data)
+ if resp.nil? or resp.size <= 0
+ raise WEBrick::HTTPStatus::InternalServerError
+ end
+
+ response.status = 200
+ response['Content-Length'] = resp.size
+ response['Content-Type'] = "text/xml; charset=utf-8"
+ response.body = resp
+ end
+end
+
+
+end # module XMLRPC
+
+
+=begin
+= History
+ $Id: server.rb 21960 2009-02-02 08:07:47Z yugui $
+=end
+
diff --git a/ruby/lib/xmlrpc/utils.rb b/ruby/lib/xmlrpc/utils.rb
new file mode 100644
index 0000000..1c9df14
--- /dev/null
+++ b/ruby/lib/xmlrpc/utils.rb
@@ -0,0 +1,165 @@
+#
+# Defines ParserWriterChooseMixin, which makes it possible to choose a
+# different XML writer and/or XML parser then the default one.
+# The Mixin is used in client.rb (class Client) and server.rb (class
+# BasicServer)
+#
+# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
+#
+# $Id: utils.rb 19657 2008-10-01 13:46:53Z mame $
+#
+
+module XMLRPC
+
+ #
+ # This module enables a user-class to be marshalled
+ # by XML-RPC for Ruby into a Hash, with one additional
+ # key/value pair "___class___" => ClassName
+ #
+ module Marshallable
+ end
+
+
+ module ParserWriterChooseMixin
+
+ def set_writer(writer)
+ @create = Create.new(writer)
+ self
+ end
+
+ def set_parser(parser)
+ @parser = parser
+ self
+ end
+
+ private
+
+ def create
+ # if set_writer was not already called then call it now
+ if @create.nil? then
+ set_writer(Config::DEFAULT_WRITER.new)
+ end
+ @create
+ end
+
+ def parser
+ # if set_parser was not already called then call it now
+ if @parser.nil? then
+ set_parser(Config::DEFAULT_PARSER.new)
+ end
+ @parser
+ end
+
+ end # module ParserWriterChooseMixin
+
+
+ module Service
+
+ #
+ # base class for Service Interface definitions, used
+ # by BasicServer#add_handler
+ #
+
+ class BasicInterface
+ attr_reader :prefix, :methods
+
+ def initialize(prefix)
+ @prefix = prefix
+ @methods = []
+ end
+
+ def add_method(sig, help=nil, meth_name=nil)
+ mname = nil
+ sig = [sig] if sig.kind_of? String
+
+ sig = sig.collect do |s|
+ name, si = parse_sig(s)
+ raise "Wrong signatures!" if mname != nil and name != mname
+ mname = name
+ si
+ end
+
+ @methods << [mname, meth_name || mname, sig, help]
+ end
+
+ private # ---------------------------------
+
+ def parse_sig(sig)
+ # sig is a String
+ if sig =~ /^\s*(\w+)\s+([^(]+)(\(([^)]*)\))?\s*$/
+ params = [$1]
+ name = $2.strip
+ $4.split(",").each {|i| params << i.strip} if $4 != nil
+ return name, params
+ else
+ raise "Syntax error in signature"
+ end
+ end
+
+ end # class BasicInterface
+
+ #
+ # class which wraps a Service Interface definition, used
+ # by BasicServer#add_handler
+ #
+ class Interface < BasicInterface
+ def initialize(prefix, &p)
+ raise "No interface specified" if p.nil?
+ super(prefix)
+ instance_eval(&p)
+ end
+
+ def get_methods(obj, delim=".")
+ prefix = @prefix + delim
+ @methods.collect { |name, meth, sig, help|
+ [prefix + name, obj.method(meth).to_proc, sig, help]
+ }
+ end
+
+ private # ---------------------------------
+
+ def meth(*a)
+ add_method(*a)
+ end
+
+ end # class Interface
+
+ class PublicInstanceMethodsInterface < BasicInterface
+ def initialize(prefix)
+ super(prefix)
+ end
+
+ def get_methods(obj, delim=".")
+ prefix = @prefix + delim
+ obj.class.public_instance_methods(false).collect { |name|
+ [prefix + name, obj.method(name).to_proc, nil, nil]
+ }
+ end
+ end
+
+
+ end # module Service
+
+
+ #
+ # short-form to create a Service::Interface
+ #
+ def self.interface(prefix, &p)
+ Service::Interface.new(prefix, &p)
+ end
+
+ # short-cut for creating a PublicInstanceMethodsInterface
+ def self.iPIMethods(prefix)
+ Service::PublicInstanceMethodsInterface.new(prefix)
+ end
+
+
+ module ParseContentType
+ def parse_content_type(str)
+ a, *b = str.split(";")
+ return a.strip.downcase, *b
+ end
+ end
+
+end # module XMLRPC
+
diff --git a/ruby/lib/yaml.rb b/ruby/lib/yaml.rb
new file mode 100644
index 0000000..7843f7f
--- /dev/null
+++ b/ruby/lib/yaml.rb
@@ -0,0 +1,440 @@
+# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
+# $Id: yaml.rb 19495 2008-09-23 18:16:08Z drbrain $
+#
+# = yaml.rb: top-level module with methods for loading and parsing YAML documents
+#
+# Author:: why the lucky stiff
+#
+
+require 'stringio'
+require 'yaml/error'
+require 'yaml/syck'
+require 'yaml/tag'
+require 'yaml/stream'
+require 'yaml/constants'
+
+# == YAML
+#
+# YAML(tm) (rhymes with 'camel') is a
+# straightforward machine parsable data serialization format designed for
+# human readability and interaction with scripting languages such as Perl
+# and Python. YAML is optimized for data serialization, formatted
+# dumping, configuration files, log files, Internet messaging and
+# filtering. This specification describes the YAML information model and
+# serialization format. Together with the Unicode standard for characters, it
+# provides all the information necessary to understand YAML Version 1.0
+# and construct computer programs to process it.
+#
+# See http://yaml.org/ for more information. For a quick tutorial, please
+# visit YAML In Five Minutes (http://yaml.kwiki.org/?YamlInFiveMinutes).
+#
+# == About This Library
+#
+# The YAML 1.0 specification outlines four stages of YAML loading and dumping.
+# This library honors all four of those stages, although data is really only
+# available to you in three stages.
+#
+# The four stages are: native, representation, serialization, and presentation.
+#
+# The native stage refers to data which has been loaded completely into Ruby's
+# own types. (See +YAML::load+.)
+#
+# The representation stage means data which has been composed into
+# +YAML::BaseNode+ objects. In this stage, the document is available as a
+# tree of node objects. You can perform YPath queries and transformations
+# at this level. (See +YAML::parse+.)
+#
+# The serialization stage happens inside the parser. The YAML parser used in
+# Ruby is called Syck. Serialized nodes are available in the extension as
+# SyckNode structs.
+#
+# The presentation stage is the YAML document itself. This is accessible
+# to you as a string. (See +YAML::dump+.)
+#
+# For more information about the various information models, see Chapter
+# 3 of the YAML 1.0 Specification (http://yaml.org/spec/#id2491269).
+#
+# The YAML module provides quick access to the most common loading (YAML::load)
+# and dumping (YAML::dump) tasks. This module also provides an API for registering
+# global types (YAML::add_domain_type).
+#
+# == Example
+#
+# A simple round-trip (load and dump) of an object.
+#
+# require "yaml"
+#
+# test_obj = ["dogs", "cats", "badgers"]
+#
+# yaml_obj = YAML::dump( test_obj )
+# # -> ---
+# - dogs
+# - cats
+# - badgers
+# ruby_obj = YAML::load( yaml_obj )
+# # => ["dogs", "cats", "badgers"]
+# ruby_obj == test_obj
+# # => true
+#
+# To register your custom types with the global resolver, use +add_domain_type+.
+#
+# YAML::add_domain_type( "your-site.com,2004", "widget" ) do |type, val|
+# Widget.new( val )
+# end
+#
+module YAML
+
+ Resolver = YAML::Syck::Resolver
+ DefaultResolver = YAML::Syck::DefaultResolver
+ DefaultResolver.use_types_at( @@tagged_classes )
+ GenericResolver = YAML::Syck::GenericResolver
+ Parser = YAML::Syck::Parser
+ Emitter = YAML::Syck::Emitter
+
+ # Returns a new default parser
+ def YAML.parser; Parser.new.set_resolver( YAML.resolver ); end
+ # Returns a new generic parser
+ def YAML.generic_parser; Parser.new.set_resolver( GenericResolver ); end
+ # Returns the default resolver
+ def YAML.resolver; DefaultResolver; end
+ # Returns a new default emitter
+ def YAML.emitter; Emitter.new.set_resolver( YAML.resolver ); end
+
+ #
+ # Converts _obj_ to YAML and writes the YAML result to _io_.
+ #
+ # File.open( 'animals.yaml', 'w' ) do |out|
+ # YAML.dump( ['badger', 'elephant', 'tiger'], out )
+ # end
+ #
+ # If no _io_ is provided, a string containing the dumped YAML
+ # is returned.
+ #
+ # YAML.dump( :locked )
+ # #=> "--- :locked"
+ #
+ def YAML.dump( obj, io = nil )
+ obj.to_yaml( io || io2 = StringIO.new )
+ io || ( io2.rewind; io2.read )
+ end
+
+ #
+ # Load a document from the current _io_ stream.
+ #
+ # File.open( 'animals.yaml' ) { |yf| YAML::load( yf ) }
+ # #=> ['badger', 'elephant', 'tiger']
+ #
+ # Can also load from a string.
+ #
+ # YAML.load( "--- :locked" )
+ # #=> :locked
+ #
+ def YAML.load( io )
+ yp = parser.load( io )
+ end
+
+ #
+ # Load a document from the file located at _filepath_.
+ #
+ # YAML.load_file( 'animals.yaml' )
+ # #=> ['badger', 'elephant', 'tiger']
+ #
+ def YAML.load_file( filepath )
+ File.open( filepath ) do |f|
+ load( f )
+ end
+ end
+
+ #
+ # Parse the first document from the current _io_ stream
+ #
+ # File.open( 'animals.yaml' ) { |yf| YAML::load( yf ) }
+ # #=> #<YAML::Syck::Node:0x82ccce0
+ # @kind=:seq,
+ # @value=
+ # [#<YAML::Syck::Node:0x82ccd94
+ # @kind=:scalar,
+ # @type_id="str",
+ # @value="badger">,
+ # #<YAML::Syck::Node:0x82ccd58
+ # @kind=:scalar,
+ # @type_id="str",
+ # @value="elephant">,
+ # #<YAML::Syck::Node:0x82ccd1c
+ # @kind=:scalar,
+ # @type_id="str",
+ # @value="tiger">]>
+ #
+ # Can also load from a string.
+ #
+ # YAML.parse( "--- :locked" )
+ # #=> #<YAML::Syck::Node:0x82edddc
+ # @type_id="tag:ruby.yaml.org,2002:sym",
+ # @value=":locked", @kind=:scalar>
+ #
+ def YAML.parse( io )
+ yp = generic_parser.load( io )
+ end
+
+ #
+ # Parse a document from the file located at _filepath_.
+ #
+ # YAML.parse_file( 'animals.yaml' )
+ # #=> #<YAML::Syck::Node:0x82ccce0
+ # @kind=:seq,
+ # @value=
+ # [#<YAML::Syck::Node:0x82ccd94
+ # @kind=:scalar,
+ # @type_id="str",
+ # @value="badger">,
+ # #<YAML::Syck::Node:0x82ccd58
+ # @kind=:scalar,
+ # @type_id="str",
+ # @value="elephant">,
+ # #<YAML::Syck::Node:0x82ccd1c
+ # @kind=:scalar,
+ # @type_id="str",
+ # @value="tiger">]>
+ #
+ def YAML.parse_file( filepath )
+ File.open( filepath ) do |f|
+ parse( f )
+ end
+ end
+
+ #
+ # Calls _block_ with each consecutive document in the YAML
+ # stream contained in _io_.
+ #
+ # File.open( 'many-docs.yaml' ) do |yf|
+ # YAML.each_document( yf ) do |ydoc|
+ # ## ydoc contains the single object
+ # ## from the YAML document
+ # end
+ # end
+ #
+ def YAML.each_document( io, &block )
+ yp = parser.load_documents( io, &block )
+ end
+
+ #
+ # Calls _block_ with each consecutive document in the YAML
+ # stream contained in _io_.
+ #
+ # File.open( 'many-docs.yaml' ) do |yf|
+ # YAML.load_documents( yf ) do |ydoc|
+ # ## ydoc contains the single object
+ # ## from the YAML document
+ # end
+ # end
+ #
+ def YAML.load_documents( io, &doc_proc )
+ YAML.each_document( io, &doc_proc )
+ end
+
+ #
+ # Calls _block_ with a tree of +YAML::BaseNodes+, one tree for
+ # each consecutive document in the YAML stream contained in _io_.
+ #
+ # File.open( 'many-docs.yaml' ) do |yf|
+ # YAML.each_node( yf ) do |ydoc|
+ # ## ydoc contains a tree of nodes
+ # ## from the YAML document
+ # end
+ # end
+ #
+ def YAML.each_node( io, &doc_proc )
+ yp = generic_parser.load_documents( io, &doc_proc )
+ end
+
+ #
+ # Calls _block_ with a tree of +YAML::BaseNodes+, one tree for
+ # each consecutive document in the YAML stream contained in _io_.
+ #
+ # File.open( 'many-docs.yaml' ) do |yf|
+ # YAML.parse_documents( yf ) do |ydoc|
+ # ## ydoc contains a tree of nodes
+ # ## from the YAML document
+ # end
+ # end
+ #
+ def YAML.parse_documents( io, &doc_proc )
+ YAML.each_node( io, &doc_proc )
+ end
+
+ #
+ # Loads all documents from the current _io_ stream,
+ # returning a +YAML::Stream+ object containing all
+ # loaded documents.
+ #
+ def YAML.load_stream( io )
+ d = nil
+ parser.load_documents( io ) do |doc|
+ d = YAML::Stream.new if not d
+ d.add( doc )
+ end
+ return d
+ end
+
+ #
+ # Returns a YAML stream containing each of the items in +objs+,
+ # each having their own document.
+ #
+ # YAML.dump_stream( 0, [], {} )
+ # #=> --- 0
+ # --- []
+ # --- {}
+ #
+ def YAML.dump_stream( *objs )
+ d = YAML::Stream.new
+ objs.each do |doc|
+ d.add( doc )
+ end
+ d.emit
+ end
+
+ #
+ # Add a global handler for a YAML domain type.
+ #
+ def YAML.add_domain_type( domain, type_tag, &transfer_proc )
+ resolver.add_type( "tag:#{ domain }:#{ type_tag }", transfer_proc )
+ end
+
+ #
+ # Add a transfer method for a builtin type
+ #
+ def YAML.add_builtin_type( type_tag, &transfer_proc )
+ resolver.add_type( "tag:yaml.org,2002:#{ type_tag }", transfer_proc )
+ end
+
+ #
+ # Add a transfer method for a builtin type
+ #
+ def YAML.add_ruby_type( type_tag, &transfer_proc )
+ resolver.add_type( "tag:ruby.yaml.org,2002:#{ type_tag }", transfer_proc )
+ end
+
+ #
+ # Add a private document type
+ #
+ def YAML.add_private_type( type_re, &transfer_proc )
+ resolver.add_type( "x-private:" + type_re, transfer_proc )
+ end
+
+ #
+ # Detect typing of a string
+ #
+ def YAML.detect_implicit( val )
+ resolver.detect_implicit( val )
+ end
+
+ #
+ # Convert a type_id to a taguri
+ #
+ def YAML.tagurize( val )
+ resolver.tagurize( val )
+ end
+
+ #
+ # Apply a transfer method to a Ruby object
+ #
+ def YAML.transfer( type_id, obj )
+ resolver.transfer( YAML.tagurize( type_id ), obj )
+ end
+
+ #
+ # Apply any implicit a node may qualify for
+ #
+ def YAML.try_implicit( obj )
+ YAML.transfer( YAML.detect_implicit( obj ), obj )
+ end
+
+ #
+ # Method to extract colon-seperated type and class, returning
+ # the type and the constant of the class
+ #
+ def YAML.read_type_class( type, obj_class )
+ scheme, domain, type, tclass = type.split( ':', 4 )
+ tclass.split( "::" ).each { |c| obj_class = obj_class.const_get( c ) } if tclass
+ return [ type, obj_class ]
+ end
+
+ #
+ # Allocate blank object
+ #
+ def YAML.object_maker( obj_class, val )
+ if Hash === val
+ o = obj_class.allocate
+ val.each_pair { |k,v|
+ o.instance_variable_set("@#{k}", v)
+ }
+ o
+ else
+ raise YAML::Error, "Invalid object explicitly tagged !ruby/Object: " + val.inspect
+ end
+ end
+
+ #
+ # Allocate an Emitter if needed
+ #
+ def YAML.quick_emit( oid, opts = {}, &e )
+ out =
+ if opts.is_a? YAML::Emitter
+ opts
+ else
+ emitter.reset( opts )
+ end
+ oid =
+ case oid when Fixnum, NilClass; oid
+ else oid = "#{oid.object_id}-#{oid.hash}"
+ end
+ out.emit( oid, &e )
+ end
+
+end
+
+require 'yaml/rubytypes'
+require 'yaml/types'
+
+module Kernel
+ #
+ # ryan:: You know how Kernel.p is a really convenient way to dump ruby
+ # structures? The only downside is that it's not as legible as
+ # YAML.
+ #
+ # _why:: (listening)
+ #
+ # ryan:: I know you don't want to urinate all over your users' namespaces.
+ # But, on the other hand, convenience of dumping for debugging is,
+ # IMO, a big YAML use case.
+ #
+ # _why:: Go nuts! Have a pony parade!
+ #
+ # ryan:: Either way, I certainly will have a pony parade.
+ #
+
+ # Prints any supplied _objects_ out in YAML. Intended as
+ # a variation on +Kernel::p+.
+ #
+ # S = Struct.new(:name, :state)
+ # s = S['dave', 'TX']
+ # y s
+ #
+ # _produces:_
+ #
+ # --- !ruby/struct:S
+ # name: dave
+ # state: TX
+ #
+ def y( object, *objects )
+ objects.unshift object
+ puts( if objects.length == 1
+ YAML::dump( *objects )
+ else
+ YAML::dump_stream( *objects )
+ end )
+ end
+ private :y
+end
+
+
diff --git a/ruby/lib/yaml/baseemitter.rb b/ruby/lib/yaml/baseemitter.rb
new file mode 100644
index 0000000..4bdc796
--- /dev/null
+++ b/ruby/lib/yaml/baseemitter.rb
@@ -0,0 +1,242 @@
+#
+# BaseEmitter
+#
+
+require 'yaml/constants'
+require 'yaml/encoding'
+require 'yaml/error'
+
+module YAML
+ module BaseEmitter
+ def options( opt = nil )
+ if opt
+ @options[opt] || YAML::DEFAULTS[opt]
+ else
+ @options
+ end
+ end
+
+ def options=( opt )
+ @options = opt
+ end
+
+ #
+ # Emit binary data
+ #
+ def binary_base64( value )
+ self << "!binary "
+ self.node_text( [value].pack("m"), '|' )
+ end
+
+ #
+ # Emit plain, normal flowing text
+ #
+ def node_text( value, block = nil )
+ @seq_map = false
+ valx = value.dup
+ unless block
+ block =
+ if options(:UseBlock)
+ '|'
+ elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/
+ '|'
+ else
+ '>'
+ end
+ indt = $&.to_i if block =~ /\d+/
+ if valx =~ /(\A\n*[ \t#]|^---\s+)/
+ indt = options(:Indent) unless indt.to_i > 0
+ block += indt.to_s
+ end
+
+ block +=
+ if valx =~ /\n\Z\n/
+ "+"
+ elsif valx =~ /\Z\n/
+ ""
+ else
+ "-"
+ end
+ end
+ block += "\n"
+ if block[0] == ?"
+ esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || ""
+ valx = fold( YAML::escape( valx, esc_skip ) + "\"" ).chomp
+ self << '"' + indent_text( valx, indt, false )
+ else
+ if block[0] == ?>
+ valx = fold( valx )
+ end
+ #p [block, indt]
+ self << block + indent_text( valx, indt )
+ end
+ end
+
+ #
+ # Emit a simple, unqouted string
+ #
+ def simple( value )
+ @seq_map = false
+ self << value.to_s
+ end
+
+ #
+ # Emit double-quoted string
+ #
+ def double( value )
+ "\"#{YAML.escape( value )}\""
+ end
+
+ #
+ # Emit single-quoted string
+ #
+ def single( value )
+ "'#{value}'"
+ end
+
+ #
+ # Write a text block with the current indent
+ #
+ def indent_text( text, mod, first_line = true )
+ return "" if text.to_s.empty?
+ spacing = indent( mod )
+ text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line
+ return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" )
+ end
+
+ #
+ # Write a current indent
+ #
+ def indent( mod = nil )
+ #p [ self.id, level, mod, :INDENT ]
+ if level <= 0
+ mod ||= 0
+ else
+ mod ||= options(:Indent)
+ mod += ( level - 1 ) * options(:Indent)
+ end
+ return " " * mod
+ end
+
+ #
+ # Add indent to the buffer
+ #
+ def indent!
+ self << indent
+ end
+
+ #
+ # Folding paragraphs within a column
+ #
+ def fold( value )
+ value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do
+ $1 || $2 + ( $3 || "\n" )
+ end
+ end
+
+ #
+ # Quick mapping
+ #
+ def map( type, &e )
+ val = Mapping.new
+ e.call( val )
+ self << "#{type} " if type.length.nonzero?
+
+ #
+ # Empty hashes
+ #
+ if val.length.zero?
+ self << "{}"
+ @seq_map = false
+ else
+ # FIXME
+ # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
+ # @headless = 1
+ # end
+
+ defkey = @options.delete( :DefaultKey )
+ if defkey
+ seq_map_shortcut
+ self << "= : "
+ defkey.to_yaml( :Emitter => self )
+ end
+
+ #
+ # Emit the key and value
+ #
+ val.each { |v|
+ seq_map_shortcut
+ if v[0].is_complex_yaml?
+ self << "? "
+ end
+ v[0].to_yaml( :Emitter => self )
+ if v[0].is_complex_yaml?
+ self << "\n"
+ indent!
+ end
+ self << ": "
+ v[1].to_yaml( :Emitter => self )
+ }
+ end
+ end
+
+ def seq_map_shortcut
+ # FIXME: seq_map needs to work with the new anchoring system
+ # if @seq_map
+ # @anchor_extras[@buffer.length - 1] = "\n" + indent
+ # @seq_map = false
+ # else
+ self << "\n"
+ indent!
+ # end
+ end
+
+ #
+ # Quick sequence
+ #
+ def seq( type, &e )
+ @seq_map = false
+ val = Sequence.new
+ e.call( val )
+ self << "#{type} " if type.length.nonzero?
+
+ #
+ # Empty arrays
+ #
+ if val.length.zero?
+ self << "[]"
+ else
+ # FIXME
+ # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
+ # @headless = 1
+ # end
+
+ #
+ # Emit the key and value
+ #
+ val.each { |v|
+ self << "\n"
+ indent!
+ self << "- "
+ @seq_map = true if v.class == Hash
+ v.to_yaml( :Emitter => self )
+ }
+ end
+ end
+ end
+
+ #
+ # Emitter helper classes
+ #
+ class Mapping < Array
+ def add( k, v )
+ push [k, v]
+ end
+ end
+
+ class Sequence < Array
+ def add( v )
+ push v
+ end
+ end
+end
diff --git a/ruby/lib/yaml/basenode.rb b/ruby/lib/yaml/basenode.rb
new file mode 100644
index 0000000..5439903
--- /dev/null
+++ b/ruby/lib/yaml/basenode.rb
@@ -0,0 +1,216 @@
+#
+# YAML::BaseNode class
+#
+require 'yaml/ypath'
+
+module YAML
+
+ #
+ # YAML Generic Model container
+ #
+ module BaseNode
+
+ #
+ # Search for YPath entry and return
+ # qualified nodes.
+ #
+ def select( ypath_str )
+ matches = match_path( ypath_str )
+
+ #
+ # Create a new generic view of the elements selected
+ #
+ if matches
+ result = []
+ matches.each { |m|
+ result.push m.last
+ }
+ YAML.transfer( 'seq', result )
+ end
+ end
+
+ #
+ # Search for YPath entry and return
+ # transformed nodes.
+ #
+ def select!( ypath_str )
+ matches = match_path( ypath_str )
+
+ #
+ # Create a new generic view of the elements selected
+ #
+ if matches
+ result = []
+ matches.each { |m|
+ result.push m.last.transform
+ }
+ result
+ end
+ end
+
+ #
+ # Search for YPath entry and return a list of
+ # qualified paths.
+ #
+ def search( ypath_str )
+ matches = match_path( ypath_str )
+
+ if matches
+ matches.collect { |m|
+ path = []
+ m.each_index { |i|
+ path.push m[i] if ( i % 2 ).zero?
+ }
+ "/" + path.compact.join( "/" )
+ }
+ end
+ end
+
+ def at( seg )
+ if Hash === @value
+ self[seg]
+ elsif Array === @value and seg =~ /\A\d+\Z/ and @value[seg.to_i]
+ @value[seg.to_i]
+ end
+ end
+
+ #
+ # YPath search returning a complete depth array
+ #
+ def match_path( ypath_str )
+ depth = 0
+ matches = []
+ YPath.each_path( ypath_str ) do |ypath|
+ seg = match_segment( ypath, 0 )
+ matches += seg if seg
+ end
+ matches.uniq
+ end
+
+ #
+ # Search a node for a single YPath segment
+ #
+ def match_segment( ypath, depth )
+ deep_nodes = []
+ seg = ypath.segments[ depth ]
+ if seg == "/"
+ unless String === @value
+ idx = -1
+ @value.collect { |v|
+ idx += 1
+ if Hash === @value
+ match_init = [v[0].transform, v[1]]
+ match_deep = v[1].match_segment( ypath, depth )
+ else
+ match_init = [idx, v]
+ match_deep = v.match_segment( ypath, depth )
+ end
+ if match_deep
+ match_deep.each { |m|
+ deep_nodes.push( match_init + m )
+ }
+ end
+ }
+ end
+ depth += 1
+ seg = ypath.segments[ depth ]
+ end
+ match_nodes =
+ case seg
+ when "."
+ [[nil, self]]
+ when ".."
+ [["..", nil]]
+ when "*"
+ if @value.is_a? Enumerable
+ idx = -1
+ @value.collect { |h|
+ idx += 1
+ if Hash === @value
+ [h[0].transform, h[1]]
+ else
+ [idx, h]
+ end
+ }
+ end
+ else
+ if seg =~ /^"(.*)"$/
+ seg = $1
+ elsif seg =~ /^'(.*)'$/
+ seg = $1
+ end
+ if ( v = at( seg ) )
+ [[ seg, v ]]
+ end
+ end
+ return deep_nodes unless match_nodes
+ pred = ypath.predicates[ depth ]
+ if pred
+ case pred
+ when /^\.=/
+ pred = $' # '
+ match_nodes.reject! { |n|
+ n.last.value != pred
+ }
+ else
+ match_nodes.reject! { |n|
+ n.last.at( pred ).nil?
+ }
+ end
+ end
+ return match_nodes + deep_nodes unless ypath.segments.length > depth + 1
+
+ #puts "DEPTH: #{depth + 1}"
+ deep_nodes = []
+ match_nodes.each { |n|
+ if n[1].is_a? BaseNode
+ match_deep = n[1].match_segment( ypath, depth + 1 )
+ if match_deep
+ match_deep.each { |m|
+ deep_nodes.push( n + m )
+ }
+ end
+ else
+ deep_nodes = []
+ end
+ }
+ deep_nodes = nil if deep_nodes.length == 0
+ deep_nodes
+ end
+
+ #
+ # We want the node to act like as Hash
+ # if it is.
+ #
+ def []( *key )
+ if Hash === @value
+ v = @value.detect { |k,| k.transform == key.first }
+ v[1] if v
+ elsif Array === @value
+ @value.[]( *key )
+ end
+ end
+
+ def children
+ if Hash === @value
+ @value.values.collect { |c| c[1] }
+ elsif Array === @value
+ @value
+ end
+ end
+
+ def children_with_index
+ if Hash === @value
+ @value.keys.collect { |i| [self[i], i] }
+ elsif Array === @value
+ i = -1; @value.collect { |v| i += 1; [v, i] }
+ end
+ end
+
+ def emit
+ transform.to_yaml
+ end
+ end
+
+end
+
diff --git a/ruby/lib/yaml/constants.rb b/ruby/lib/yaml/constants.rb
new file mode 100644
index 0000000..fb833d3
--- /dev/null
+++ b/ruby/lib/yaml/constants.rb
@@ -0,0 +1,45 @@
+#
+# Constants used throughout the library
+#
+module YAML
+
+ #
+ # Constants
+ #
+ VERSION = '0.60'
+ SUPPORTED_YAML_VERSIONS = ['1.0']
+
+ #
+ # Parser tokens
+ #
+ WORD_CHAR = 'A-Za-z0-9'
+ PRINTABLE_CHAR = '-_A-Za-z0-9!?/()$\'". '
+ NOT_PLAIN_CHAR = '\x7f\x0-\x1f\x80-\x9f'
+ ESCAPE_CHAR = '[\\x00-\\x09\\x0b-\\x1f]'
+ INDICATOR_CHAR = '*&!|\\\\^@%{}[]='
+ SPACE_INDICATORS = '-#:,?'
+ RESTRICTED_INDICATORS = '#:,}]'
+ DNS_COMP_RE = "\\w(?:[-\\w]*\\w)?"
+ DNS_NAME_RE = "(?:(?:#{DNS_COMP_RE}\\.)+#{DNS_COMP_RE}|#{DNS_COMP_RE})"
+ ESCAPES = %w{\x00 \x01 \x02 \x03 \x04 \x05 \x06 \a
+ \x08 \t \n \v \f \r \x0e \x0f
+ \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17
+ \x18 \x19 \x1a \e \x1c \x1d \x1e \x1f
+ }
+ UNESCAPES = {
+ 'a' => "\x07", 'b' => "\x08", 't' => "\x09",
+ 'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c",
+ 'r' => "\x0d", 'e' => "\x1b", '\\' => '\\',
+ }
+
+ #
+ # Default settings
+ #
+ DEFAULTS = {
+ :Indent => 2, :UseHeader => false, :UseVersion => false, :Version => '1.0',
+ :SortKeys => false, :AnchorFormat => 'id%03d', :ExplicitTypes => false,
+ :WidthType => 'absolute', :BestWidth => 80,
+ :UseBlock => false, :UseFold => false, :Encoding => :None
+ }
+
+end
diff --git a/ruby/lib/yaml/dbm.rb b/ruby/lib/yaml/dbm.rb
new file mode 100644
index 0000000..87d6009
--- /dev/null
+++ b/ruby/lib/yaml/dbm.rb
@@ -0,0 +1,111 @@
+require 'yaml'
+require 'dbm'
+#
+# YAML + DBM = YDBM
+# - Same interface as DBM class
+#
+module YAML
+
+class DBM < ::DBM
+ VERSION = "0.1"
+ def []( key )
+ fetch( key )
+ end
+ def []=( key, val )
+ store( key, val )
+ end
+ def fetch( keystr, ifnone = nil )
+ begin
+ val = super( keystr )
+ return YAML::load( val ) if String === val
+ rescue IndexError
+ end
+ if block_given?
+ yield keystr
+ else
+ ifnone
+ end
+ end
+ def index( keystr )
+ super( keystr.to_yaml )
+ end
+ def values_at( *keys )
+ keys.collect { |k| fetch( k ) }
+ end
+ def delete( key )
+ v = super( key )
+ if String === v
+ v = YAML::load( v )
+ end
+ v
+ end
+ def delete_if
+ del_keys = keys.dup
+ del_keys.delete_if { |k| yield( k, fetch( k ) ) == false }
+ del_keys.each { |k| delete( k ) }
+ self
+ end
+ def reject
+ hsh = self.to_hash
+ hsh.reject { |k,v| yield k, v }
+ end
+ def each_pair
+ keys.each { |k| yield k, fetch( k ) }
+ self
+ end
+ def each_value
+ super { |v| yield YAML::load( v ) }
+ self
+ end
+ def values
+ super.collect { |v| YAML::load( v ) }
+ end
+ def has_value?( val )
+ each_value { |v| return true if v == val }
+ return false
+ end
+ def invert
+ h = {}
+ keys.each { |k| h[ self.fetch( k ) ] = k }
+ h
+ end
+ def replace( hsh )
+ clear
+ update( hsh )
+ end
+ def shift
+ a = super
+ a[1] = YAML::load( a[1] ) if a
+ a
+ end
+ def select( *keys )
+ if block_given?
+ self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact
+ else
+ values_at( *keys )
+ end
+ end
+ def store( key, val )
+ super( key, val.to_yaml )
+ val
+ end
+ def update( hsh )
+ hsh.keys.each do |k|
+ self.store( k, hsh.fetch( k ) )
+ end
+ self
+ end
+ def to_a
+ a = []
+ keys.each { |k| a.push [ k, self.fetch( k ) ] }
+ a
+ end
+ def to_hash
+ h = {}
+ keys.each { |k| h[ k ] = self.fetch( k ) }
+ h
+ end
+ alias :each :each_pair
+end
+
+end
diff --git a/ruby/lib/yaml/encoding.rb b/ruby/lib/yaml/encoding.rb
new file mode 100644
index 0000000..57dc553
--- /dev/null
+++ b/ruby/lib/yaml/encoding.rb
@@ -0,0 +1,33 @@
+#
+# Handle Unicode-to-Internal conversion
+#
+
+module YAML
+
+ #
+ # Escape the string, condensing common escapes
+ #
+ def YAML.escape( value, skip = "" )
+ value.gsub( /\\/, "\\\\\\" ).
+ gsub( /"/, "\\\"" ).
+ gsub( /([\x00-\x1f])/ ) do
+ skip[$&] || ESCAPES[ $&.unpack("C")[0] ]
+ end
+ end
+
+ #
+ # Unescape the condenses escapes
+ #
+ def YAML.unescape( value )
+ value.gsub( /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ ) {
+ if $3
+ ["#$3".hex ].pack('U*')
+ elsif $2
+ [$2].pack( "H2" )
+ else
+ UNESCAPES[$1]
+ end
+ }
+ end
+
+end
diff --git a/ruby/lib/yaml/error.rb b/ruby/lib/yaml/error.rb
new file mode 100644
index 0000000..15865a9
--- /dev/null
+++ b/ruby/lib/yaml/error.rb
@@ -0,0 +1,34 @@
+#
+# Error messages and exception class
+#
+
+module YAML
+
+ #
+ # Error messages
+ #
+
+ ERROR_NO_HEADER_NODE = "With UseHeader=false, the node Array or Hash must have elements"
+ ERROR_NEED_HEADER = "With UseHeader=false, the node must be an Array or Hash"
+ ERROR_BAD_EXPLICIT = "Unsupported explicit transfer: '%s'"
+ ERROR_MANY_EXPLICIT = "More than one explicit transfer"
+ ERROR_MANY_IMPLICIT = "More than one implicit request"
+ ERROR_NO_ANCHOR = "No anchor for alias '%s'"
+ ERROR_BAD_ANCHOR = "Invalid anchor: %s"
+ ERROR_MANY_ANCHOR = "More than one anchor"
+ ERROR_ANCHOR_ALIAS = "Can't define both an anchor and an alias"
+ ERROR_BAD_ALIAS = "Invalid alias: %s"
+ ERROR_MANY_ALIAS = "More than one alias"
+ ERROR_ZERO_INDENT = "Can't use zero as an indentation width"
+ ERROR_UNSUPPORTED_VERSION = "This release of YAML.rb does not support YAML version %s"
+ ERROR_UNSUPPORTED_ENCODING = "Attempt to use unsupported encoding: %s"
+
+ #
+ # YAML Error classes
+ #
+
+ class Error < StandardError; end
+ class ParseError < Error; end
+ class TypeError < StandardError; end
+
+end
diff --git a/ruby/lib/yaml/loader.rb b/ruby/lib/yaml/loader.rb
new file mode 100644
index 0000000..eb0709e
--- /dev/null
+++ b/ruby/lib/yaml/loader.rb
@@ -0,0 +1,14 @@
+#
+# YAML::Loader class
+# .. type handling ..
+#
+module YAML
+ class Loader
+ TRANSFER_DOMAINS = {
+ 'yaml.org,2002' => {},
+ 'ruby.yaml.org,2002' => {}
+ }
+ PRIVATE_TYPES = {}
+ IMPLICIT_TYPES = [ 'null', 'bool', 'time', 'int', 'float' ]
+ end
+end
diff --git a/ruby/lib/yaml/rubytypes.rb b/ruby/lib/yaml/rubytypes.rb
new file mode 100644
index 0000000..ae65b35
--- /dev/null
+++ b/ruby/lib/yaml/rubytypes.rb
@@ -0,0 +1,446 @@
+# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
+require 'date'
+
+class Class
+ def to_yaml( opts = {} )
+ raise TypeError, "can't dump anonymous class %s" % self.class
+ end
+end
+
+class Object
+ yaml_as "tag:ruby.yaml.org,2002:object"
+ def to_yaml_style; end
+ def to_yaml_properties; instance_variables.sort; end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.map( taguri, to_yaml_style ) do |map|
+ to_yaml_properties.each do |m|
+ map.add( m[1..-1], instance_variable_get( m ) )
+ end
+ end
+ end
+ end
+end
+
+class Hash
+ yaml_as "tag:ruby.yaml.org,2002:hash"
+ yaml_as "tag:yaml.org,2002:map"
+ def yaml_initialize( tag, val )
+ if Array === val
+ update Hash.[]( *val ) # Convert the map to a sequence
+ elsif Hash === val
+ update val
+ else
+ raise YAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.map( taguri, to_yaml_style ) do |map|
+ each do |k, v|
+ map.add( k, v )
+ end
+ end
+ end
+ end
+end
+
+class Struct
+ yaml_as "tag:ruby.yaml.org,2002:struct"
+ def self.yaml_tag_class_name; self.name.gsub( "Struct::", "" ); end
+ def self.yaml_tag_read_class( name ); "Struct::#{ name }"; end
+ def self.yaml_new( klass, tag, val )
+ if Hash === val
+ struct_type = nil
+
+ #
+ # Use existing Struct if it exists
+ #
+ props = {}
+ val.delete_if { |k,v| props[k] = v if k =~ /^@/ }
+ begin
+ struct_name, struct_type = YAML.read_type_class( tag, Struct )
+ rescue NameError
+ end
+ if not struct_type
+ struct_def = [ tag.split( ':', 4 ).last ]
+ struct_type = Struct.new( *struct_def.concat( val.keys.collect { |k| k.intern } ) )
+ end
+
+ #
+ # Set the Struct properties
+ #
+ st = YAML::object_maker( struct_type, {} )
+ st.members.each do |m|
+ st.send( "#{m}=", val[m] )
+ end
+ props.each do |k,v|
+ st.instance_variable_set(k, v)
+ end
+ st
+ else
+ raise YAML::TypeError, "Invalid Ruby Struct: " + val.inspect
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ #
+ # Basic struct is passed as a YAML map
+ #
+ out.map( taguri, to_yaml_style ) do |map|
+ self.members.each do |m|
+ map.add( m, self[m] )
+ end
+ self.to_yaml_properties.each do |m|
+ map.add( m, instance_variable_get( m ) )
+ end
+ end
+ end
+ end
+end
+
+class Array
+ yaml_as "tag:ruby.yaml.org,2002:array"
+ yaml_as "tag:yaml.org,2002:seq"
+ def yaml_initialize( tag, val ); concat( val.to_a ); end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.seq( taguri, to_yaml_style ) do |seq|
+ each do |x|
+ seq.add( x )
+ end
+ end
+ end
+ end
+end
+
+class Exception
+ yaml_as "tag:ruby.yaml.org,2002:exception"
+ def Exception.yaml_new( klass, tag, val )
+ o = YAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
+ val.each_pair do |k,v|
+ o.instance_variable_set("@#{k}", v)
+ end
+ o
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.map( taguri, to_yaml_style ) do |map|
+ map.add( 'message', message )
+ to_yaml_properties.each do |m|
+ map.add( m[1..-1], instance_variable_get( m ) )
+ end
+ end
+ end
+ end
+end
+
+class String
+ yaml_as "tag:ruby.yaml.org,2002:string"
+ yaml_as "tag:yaml.org,2002:binary"
+ yaml_as "tag:yaml.org,2002:str"
+ def is_complex_yaml?
+ to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
+ end
+ def is_binary_data?
+ ( self.count( "^ -~", "^\r\n" ).fdiv(self.size) > 0.3 || self.index( "\x00" ) ) unless empty?
+ end
+ def String.yaml_new( klass, tag, val )
+ val = val.unpack("m")[0] if tag == "tag:yaml.org,2002:binary"
+ val = { 'str' => val } if String === val
+ if Hash === val
+ s = klass.allocate
+ # Thank you, NaHi
+ String.instance_method(:initialize).
+ bind(s).
+ call( val.delete( 'str' ) )
+ val.each { |k,v| s.instance_variable_set( k, v ) }
+ s
+ else
+ raise YAML::TypeError, "Invalid String: " + val.inspect
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( is_complex_yaml? ? self : nil, opts ) do |out|
+ if is_binary_data?
+ out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal )
+ elsif to_yaml_properties.empty?
+ out.scalar( taguri, self, self =~ /^:/ ? :quote2 : to_yaml_style )
+ else
+ out.map( taguri, to_yaml_style ) do |map|
+ map.add( 'str', "#{self}" )
+ to_yaml_properties.each do |m|
+ map.add( m, instance_variable_get( m ) )
+ end
+ end
+ end
+ end
+ end
+end
+
+class Symbol
+ yaml_as "tag:ruby.yaml.org,2002:symbol"
+ yaml_as "tag:ruby.yaml.org,2002:sym"
+ def Symbol.yaml_new( klass, tag, val )
+ if String === val
+ val = YAML::load( val ) if val =~ /\A(["']).*\1\z/
+ val.intern
+ else
+ raise YAML::TypeError, "Invalid Symbol: " + val.inspect
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( nil, opts ) do |out|
+ out.scalar( "tag:yaml.org,2002:str", self.inspect, :plain )
+ end
+ end
+end
+
+class Range
+ yaml_as "tag:ruby.yaml.org,2002:range"
+ def Range.yaml_new( klass, tag, val )
+ inr = %r'(\w+|[+-]?\d+(?:\.\d+)?(?:e[+-]\d+)?|"(?:[^\\"]|\\.)*")'
+ opts = {}
+ if String === val and val =~ /^#{inr}(\.{2,3})#{inr}$/o
+ r1, rdots, r2 = $1, $2, $3
+ opts = {
+ 'begin' => YAML.load( "--- #{r1}" ),
+ 'end' => YAML.load( "--- #{r2}" ),
+ 'excl' => rdots.length == 3
+ }
+ val = {}
+ elsif Hash === val
+ opts['begin'] = val.delete('begin')
+ opts['end'] = val.delete('end')
+ opts['excl'] = val.delete('excl')
+ end
+ if Hash === opts
+ r = YAML::object_maker( klass, {} )
+ # Thank you, NaHi
+ Range.instance_method(:initialize).
+ bind(r).
+ call( opts['begin'], opts['end'], opts['excl'] )
+ val.each { |k,v| r.instance_variable_set( k, v ) }
+ r
+ else
+ raise YAML::TypeError, "Invalid Range: " + val.inspect
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ # if self.begin.is_complex_yaml? or self.begin.respond_to? :to_str or
+ # self.end.is_complex_yaml? or self.end.respond_to? :to_str or
+ # not to_yaml_properties.empty?
+ out.map( taguri, to_yaml_style ) do |map|
+ map.add( 'begin', self.begin )
+ map.add( 'end', self.end )
+ map.add( 'excl', self.exclude_end? )
+ to_yaml_properties.each do |m|
+ map.add( m, instance_variable_get( m ) )
+ end
+ end
+ # else
+ # out.scalar( taguri ) do |sc|
+ # sc.embed( self.begin )
+ # sc.concat( self.exclude_end? ? "..." : ".." )
+ # sc.embed( self.end )
+ # end
+ # end
+ end
+ end
+end
+
+class Regexp
+ yaml_as "tag:ruby.yaml.org,2002:regexp"
+ def Regexp.yaml_new( klass, tag, val )
+ if String === val and val =~ /^\/(.*)\/([mix]*)$/
+ val = { 'regexp' => $1, 'mods' => $2 }
+ end
+ if Hash === val
+ mods = nil
+ unless val['mods'].to_s.empty?
+ mods = 0x00
+ mods |= Regexp::EXTENDED if val['mods'].include?( 'x' )
+ mods |= Regexp::IGNORECASE if val['mods'].include?( 'i' )
+ mods |= Regexp::MULTILINE if val['mods'].include?( 'm' )
+ end
+ val.delete( 'mods' )
+ r = YAML::object_maker( klass, {} )
+ Regexp.instance_method(:initialize).
+ bind(r).
+ call( val.delete( 'regexp' ), mods )
+ val.each { |k,v| r.instance_variable_set( k, v ) }
+ r
+ else
+ raise YAML::TypeError, "Invalid Regular expression: " + val.inspect
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( nil, opts ) do |out|
+ if to_yaml_properties.empty?
+ out.scalar( taguri, self.inspect, :plain )
+ else
+ out.map( taguri, to_yaml_style ) do |map|
+ src = self.inspect
+ if src =~ /\A\/(.*)\/([a-z]*)\Z/
+ map.add( 'regexp', $1 )
+ map.add( 'mods', $2 )
+ else
+ raise YAML::TypeError, "Invalid Regular expression: " + src
+ end
+ to_yaml_properties.each do |m|
+ map.add( m, instance_variable_get( m ) )
+ end
+ end
+ end
+ end
+ end
+end
+
+class Time
+ yaml_as "tag:ruby.yaml.org,2002:time"
+ yaml_as "tag:yaml.org,2002:timestamp"
+ def Time.yaml_new( klass, tag, val )
+ if Hash === val
+ t = val.delete( 'at' )
+ val.each { |k,v| t.instance_variable_set( k, v ) }
+ t
+ else
+ raise YAML::TypeError, "Invalid Time: " + val.inspect
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ tz = "Z"
+ # from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
+ unless self.utc?
+ utc_same_instant = self.dup.utc
+ utc_same_writing = Time.utc(year,month,day,hour,min,sec,usec)
+ difference_to_utc = utc_same_writing - utc_same_instant
+ if (difference_to_utc < 0)
+ difference_sign = '-'
+ absolute_difference = -difference_to_utc
+ else
+ difference_sign = '+'
+ absolute_difference = difference_to_utc
+ end
+ difference_minutes = (absolute_difference/60).round
+ tz = "%s%02d:%02d" % [ difference_sign, difference_minutes / 60, difference_minutes % 60]
+ end
+ standard = self.strftime( "%Y-%m-%d %H:%M:%S" )
+ standard += ".%06d" % [usec] if usec.nonzero?
+ standard += " %s" % [tz]
+ if to_yaml_properties.empty?
+ out.scalar( taguri, standard, :plain )
+ else
+ out.map( taguri, to_yaml_style ) do |map|
+ map.add( 'at', standard )
+ to_yaml_properties.each do |m|
+ map.add( m, instance_variable_get( m ) )
+ end
+ end
+ end
+ end
+ end
+end
+
+class Date
+ yaml_as "tag:yaml.org,2002:timestamp#ymd"
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
+ end
+ end
+end
+
+class Integer
+ yaml_as "tag:yaml.org,2002:int"
+ def to_yaml( opts = {} )
+ YAML::quick_emit( nil, opts ) do |out|
+ out.scalar( "tag:yaml.org,2002:int", self.to_s, :plain )
+ end
+ end
+end
+
+class Float
+ yaml_as "tag:yaml.org,2002:float"
+ def to_yaml( opts = {} )
+ YAML::quick_emit( nil, opts ) do |out|
+ str = self.to_s
+ if str == "Infinity"
+ str = ".Inf"
+ elsif str == "-Infinity"
+ str = "-.Inf"
+ elsif str == "NaN"
+ str = ".NaN"
+ end
+ out.scalar( "tag:yaml.org,2002:float", str, :plain )
+ end
+ end
+end
+
+class Rational
+ yaml_as "tag:ruby.yaml.org,2002:object:Rational"
+ def Rational.yaml_new( klass, tag, val )
+ if val.is_a? String
+ Rational( val )
+ else
+ Rational( val['numerator'], val['denominator'] )
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.map( taguri, nil ) do |map|
+ map.add( 'denominator', denominator )
+ map.add( 'numerator', numerator )
+ end
+ end
+ end
+end
+
+class Complex
+ yaml_as "tag:ruby.yaml.org,2002:object:Complex"
+ def Complex.yaml_new( klass, tag, val )
+ if val.is_a? String
+ Complex( val )
+ else
+ Complex( val['real'], val['image'] )
+ end
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.map( taguri, nil ) do |map|
+ map.add( 'image', imaginary )
+ map.add( 'real', real )
+ end
+ end
+ end
+end
+
+class TrueClass
+ yaml_as "tag:yaml.org,2002:bool#yes"
+ def to_yaml( opts = {} )
+ YAML::quick_emit( nil, opts ) do |out|
+ out.scalar( taguri, "true", :plain )
+ end
+ end
+end
+
+class FalseClass
+ yaml_as "tag:yaml.org,2002:bool#no"
+ def to_yaml( opts = {} )
+ YAML::quick_emit( nil, opts ) do |out|
+ out.scalar( taguri, "false", :plain )
+ end
+ end
+end
+
+class NilClass
+ yaml_as "tag:yaml.org,2002:null"
+ def to_yaml( opts = {} )
+ YAML::quick_emit( nil, opts ) do |out|
+ out.scalar( taguri, "", :plain )
+ end
+ end
+end
+
diff --git a/ruby/lib/yaml/store.rb b/ruby/lib/yaml/store.rb
new file mode 100644
index 0000000..e3a8e9f
--- /dev/null
+++ b/ruby/lib/yaml/store.rb
@@ -0,0 +1,43 @@
+#
+# YAML::Store
+#
+require 'yaml'
+require 'pstore'
+
+class YAML::Store < PStore
+ def initialize( *o )
+ @opt = YAML::DEFAULTS.dup
+ if String === o.first
+ super(o.shift)
+ end
+ if o.last.is_a? Hash
+ @opt.update(o.pop)
+ end
+ end
+
+ def dump(table)
+ @table.to_yaml(@opt)
+ end
+
+ def load(content)
+ table = YAML::load(content)
+ if table == false
+ {}
+ else
+ table
+ end
+ end
+
+ def marshal_dump_supports_canonical_option?
+ false
+ end
+
+ EMPTY_MARSHAL_DATA = {}.to_yaml
+ EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA)
+ def empty_marshal_data
+ EMPTY_MARSHAL_DATA
+ end
+ def empty_marshal_checksum
+ EMPTY_MARSHAL_CHECKSUM
+ end
+end
diff --git a/ruby/lib/yaml/stream.rb b/ruby/lib/yaml/stream.rb
new file mode 100644
index 0000000..060fbc4
--- /dev/null
+++ b/ruby/lib/yaml/stream.rb
@@ -0,0 +1,40 @@
+module YAML
+
+ #
+ # YAML::Stream -- for emitting many documents
+ #
+ class Stream
+
+ attr_accessor :documents, :options
+
+ def initialize( opts = {} )
+ @options = opts
+ @documents = []
+ end
+
+ def []( i )
+ @documents[ i ]
+ end
+
+ def add( doc )
+ @documents << doc
+ end
+
+ def edit( doc_num, doc )
+ @documents[ doc_num ] = doc
+ end
+
+ def emit( io = nil )
+ # opts = @options.dup
+ # opts[:UseHeader] = true if @documents.length > 1
+ out = YAML.emitter
+ out.reset( io || io2 = StringIO.new )
+ @documents.each { |v|
+ v.to_yaml( out )
+ }
+ io || ( io2.rewind; io2.read )
+ end
+
+ end
+
+end
diff --git a/ruby/lib/yaml/stringio.rb b/ruby/lib/yaml/stringio.rb
new file mode 100644
index 0000000..8ad949f
--- /dev/null
+++ b/ruby/lib/yaml/stringio.rb
@@ -0,0 +1,83 @@
+#
+# Limited StringIO if no core lib is available
+#
+begin
+require 'stringio'
+rescue LoadError
+ # StringIO based on code by MoonWolf
+ class StringIO
+ def initialize(string="")
+ @string=string
+ @pos=0
+ @eof=(string.size==0)
+ end
+ def pos
+ @pos
+ end
+ def eof
+ @eof
+ end
+ alias eof? eof
+ def readline(rs=$/)
+ if @eof
+ raise EOFError
+ else
+ if p = @string[@pos..-1]=~rs
+ line = @string[@pos,p+1]
+ else
+ line = @string[@pos..-1]
+ end
+ @pos+=line.size
+ @eof =true if @pos==@string.size
+ $_ = line
+ end
+ end
+ def rewind
+ seek(0,0)
+ end
+ def seek(offset,whence)
+ case whence
+ when 0
+ @pos=offset
+ when 1
+ @pos+=offset
+ when 2
+ @pos=@string.size+offset
+ end
+ @eof=(@pos>=@string.size)
+ 0
+ end
+ end
+
+ #
+ # Class method for creating streams
+ #
+ def YAML.make_stream( io )
+ if String === io
+ io = StringIO.new( io )
+ elsif not IO === io
+ raise YAML::Error, "YAML stream must be an IO or String object."
+ end
+ if YAML::unicode
+ def io.readline
+ YAML.utf_to_internal( readline( @ln_sep ), @utf_encoding )
+ end
+ def io.check_unicode
+ @utf_encoding = YAML.sniff_encoding( read( 4 ) )
+ @ln_sep = YAML.enc_separator( @utf_encoding )
+ seek( -4, IO::SEEK_CUR )
+ end
+ def io.utf_encoding
+ @utf_encoding
+ end
+ io.check_unicode
+ else
+ def io.utf_encoding
+ :None
+ end
+ end
+ io
+ end
+
+end
+
diff --git a/ruby/lib/yaml/syck.rb b/ruby/lib/yaml/syck.rb
new file mode 100644
index 0000000..faf57e8
--- /dev/null
+++ b/ruby/lib/yaml/syck.rb
@@ -0,0 +1,19 @@
+#
+# YAML::Syck module
+# .. glues syck and yaml.rb together ..
+#
+require 'syck'
+require 'yaml/basenode'
+
+module YAML
+ module Syck
+
+ #
+ # Mixin BaseNode functionality
+ #
+ class Node
+ include YAML::BaseNode
+ end
+
+ end
+end
diff --git a/ruby/lib/yaml/tag.rb b/ruby/lib/yaml/tag.rb
new file mode 100644
index 0000000..a91f2bd
--- /dev/null
+++ b/ruby/lib/yaml/tag.rb
@@ -0,0 +1,91 @@
+# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
+# $Id: tag.rb 11708 2007-02-12 23:01:19Z shyouhei $
+#
+# = yaml/tag.rb: methods for associating a taguri to a class.
+#
+# Author:: why the lucky stiff
+#
+module YAML
+ # A dictionary of taguris which map to
+ # Ruby classes.
+ @@tagged_classes = {}
+
+ #
+ # Associates a taguri _tag_ with a Ruby class _cls_. The taguri is used to give types
+ # to classes when loading YAML. Taguris are of the form:
+ #
+ # tag:authorityName,date:specific
+ #
+ # The +authorityName+ is a domain name or email address. The +date+ is the date the type
+ # was issued in YYYY or YYYY-MM or YYYY-MM-DD format. The +specific+ is a name for
+ # the type being added.
+ #
+ # For example, built-in YAML types have 'yaml.org' as the +authorityName+ and '2002' as the
+ # +date+. The +specific+ is simply the name of the type:
+ #
+ # tag:yaml.org,2002:int
+ # tag:yaml.org,2002:float
+ # tag:yaml.org,2002:timestamp
+ #
+ # The domain must be owned by you on the +date+ declared. If you don't own any domains on the
+ # date you declare the type, you can simply use an e-mail address.
+ #
+ # tag:why@ruby-lang.org,2004:notes/personal
+ #
+ def YAML.tag_class( tag, cls )
+ if @@tagged_classes.has_key? tag
+ warn "class #{ @@tagged_classes[tag] } held ownership of the #{ tag } tag"
+ end
+ @@tagged_classes[tag] = cls
+ end
+
+ # Returns the complete dictionary of taguris, paired with classes. The key for
+ # the dictionary is the full taguri. The value for each key is the class constant
+ # associated to that taguri.
+ #
+ # YAML.tagged_classes["tag:yaml.org,2002:int"] => Integer
+ #
+ def YAML.tagged_classes
+ @@tagged_classes
+ end
+end
+
+class Module
+ # :stopdoc:
+
+ # Adds a taguri _tag_ to a class, used when dumping or loading the class
+ # in YAML. See YAML::tag_class for detailed information on typing and
+ # taguris.
+ def yaml_as( tag, sc = true )
+ verbose, $VERBOSE = $VERBOSE, nil
+ class_eval <<-"end;", __FILE__, __LINE__+1
+ attr_writer :taguri
+ def taguri
+ if respond_to? :to_yaml_type
+ YAML::tagurize( to_yaml_type[1..-1] )
+ else
+ return @taguri if defined?(@taguri) and @taguri
+ tag = #{ tag.dump }
+ if self.class.yaml_tag_subclasses? and self.class != YAML::tagged_classes[tag]
+ tag = "\#{ tag }:\#{ self.class.yaml_tag_class_name }"
+ end
+ tag
+ end
+ end
+ def self.yaml_tag_subclasses?; #{ sc ? 'true' : 'false' }; end
+ end;
+ YAML::tag_class tag, self
+ ensure
+ $VERBOSE = verbose
+ end
+ # Transforms the subclass name into a name suitable for display
+ # in a subclassed tag.
+ def yaml_tag_class_name
+ self.name
+ end
+ # Transforms the subclass name found in the tag into a Ruby
+ # constant name.
+ def yaml_tag_read_class( name )
+ name
+ end
+end
diff --git a/ruby/lib/yaml/types.rb b/ruby/lib/yaml/types.rb
new file mode 100644
index 0000000..3871c62
--- /dev/null
+++ b/ruby/lib/yaml/types.rb
@@ -0,0 +1,192 @@
+# -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
+#
+# Classes required by the full core typeset
+#
+
+module YAML
+
+ #
+ # Default private type
+ #
+ class PrivateType
+ def self.tag_subclasses?; false; end
+ verbose, $VERBOSE = $VERBOSE, nil
+ def initialize( type, val )
+ @type_id = type; @value = val
+ @value.taguri = "x-private:#{ @type_id }"
+ end
+ def to_yaml( opts = {} )
+ @value.to_yaml( opts )
+ end
+ ensure
+ $VERBOSE = verbose
+ end
+
+ #
+ # Default domain type
+ #
+ class DomainType
+ def self.tag_subclasses?; false; end
+ verbose, $VERBOSE = $VERBOSE, nil
+ def initialize( domain, type, val )
+ @domain = domain; @type_id = type; @value = val
+ @value.taguri = "tag:#{ @domain }:#{ @type_id }"
+ end
+ def to_yaml( opts = {} )
+ @value.to_yaml( opts )
+ end
+ ensure
+ $VERBOSE = verbose
+ end
+
+ #
+ # Unresolved objects
+ #
+ class Object
+ def self.tag_subclasses?; false; end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.map( "tag:ruby.yaml.org,2002:object:#{ @class }", to_yaml_style ) do |map|
+ @ivars.each do |k,v|
+ map.add( k, v )
+ end
+ end
+ end
+ end
+ end
+
+ #
+ # YAML Hash class to support comments and defaults
+ #
+ class SpecialHash < ::Hash
+ attr_accessor :default
+ def inspect
+ self.default.to_s
+ end
+ def to_s
+ self.default.to_s
+ end
+ def update( h )
+ if YAML::SpecialHash === h
+ @default = h.default if h.default
+ end
+ super( h )
+ end
+ def to_yaml( opts = {} )
+ opts[:DefaultKey] = self.default
+ super( opts )
+ end
+ end
+
+ #
+ # Builtin collection: !omap
+ #
+ class Omap < ::Array
+ yaml_as "tag:yaml.org,2002:omap"
+ def yaml_initialize( tag, val )
+ if Array === val
+ val.each do |v|
+ if Hash === v
+ concat( v.to_a ) # Convert the map to a sequence
+ else
+ raise YAML::Error, "Invalid !omap entry: " + val.inspect
+ end
+ end
+ else
+ raise YAML::Error, "Invalid !omap: " + val.inspect
+ end
+ self
+ end
+ def self.[]( *vals )
+ o = Omap.new
+ 0.step( vals.length - 1, 2 ) do |i|
+ o[vals[i]] = vals[i+1]
+ end
+ o
+ end
+ def []( k )
+ self.assoc( k ).to_a[1]
+ end
+ def []=( k, *rest )
+ val, set = rest.reverse
+ if ( tmp = self.assoc( k ) ) and not set
+ tmp[1] = val
+ else
+ self << [ k, val ]
+ end
+ val
+ end
+ def has_key?( k )
+ self.assoc( k ) ? true : false
+ end
+ def is_complex_yaml?
+ true
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.seq( taguri, to_yaml_style ) do |seq|
+ self.each do |v|
+ seq.add( Hash[ *v ] )
+ end
+ end
+ end
+ end
+ end
+
+ #
+ # Builtin collection: !pairs
+ #
+ class Pairs < ::Array
+ yaml_as "tag:yaml.org,2002:pairs"
+ def yaml_initialize( tag, val )
+ if Array === val
+ val.each do |v|
+ if Hash === v
+ concat( v.to_a ) # Convert the map to a sequence
+ else
+ raise YAML::Error, "Invalid !pairs entry: " + val.inspect
+ end
+ end
+ else
+ raise YAML::Error, "Invalid !pairs: " + val.inspect
+ end
+ self
+ end
+ def self.[]( *vals )
+ p = Pairs.new
+ 0.step( vals.length - 1, 2 ) { |i|
+ p[vals[i]] = vals[i+1]
+ }
+ p
+ end
+ def []( k )
+ self.assoc( k ).to_a
+ end
+ def []=( k, val )
+ self << [ k, val ]
+ val
+ end
+ def has_key?( k )
+ self.assoc( k ) ? true : false
+ end
+ def is_complex_yaml?
+ true
+ end
+ def to_yaml( opts = {} )
+ YAML::quick_emit( self, opts ) do |out|
+ out.seq( taguri, to_yaml_style ) do |seq|
+ self.each do |v|
+ seq.add( Hash[ *v ] )
+ end
+ end
+ end
+ end
+ end
+
+ #
+ # Builtin collection: !set
+ #
+ class Set < ::Hash
+ yaml_as "tag:yaml.org,2002:set"
+ end
+end
diff --git a/ruby/lib/yaml/yamlnode.rb b/ruby/lib/yaml/yamlnode.rb
new file mode 100644
index 0000000..8afa142
--- /dev/null
+++ b/ruby/lib/yaml/yamlnode.rb
@@ -0,0 +1,54 @@
+#
+# YAML::YamlNode class
+#
+require 'yaml/basenode'
+
+module YAML
+
+ #
+ # YAML Generic Model container
+ #
+ class YamlNode
+ include BaseNode
+ attr_accessor :kind, :type_id, :value, :anchor
+ def initialize(t, v)
+ @type_id = t
+ if Hash === v
+ @kind = 'map'
+ @value = {}
+ v.each {|key,val|
+ @value[key.transform] = [key, val]
+ }
+ elsif Array === v
+ @kind = 'seq'
+ @value = v
+ elsif String === v
+ @kind = 'scalar'
+ @value = v
+ end
+ end
+
+ #
+ # Transform this node fully into a native type
+ #
+ def transform
+ t = nil
+ if @value.is_a? Hash
+ t = {}
+ @value.each { |k,v|
+ t[ k ] = v[1].transform
+ }
+ elsif @value.is_a? Array
+ t = []
+ @value.each { |v|
+ t.push v.transform
+ }
+ else
+ t = @value
+ end
+ YAML.transfer_method( @type_id, t )
+ end
+
+ end
+
+end
diff --git a/ruby/lib/yaml/ypath.rb b/ruby/lib/yaml/ypath.rb
new file mode 100644
index 0000000..81348ca
--- /dev/null
+++ b/ruby/lib/yaml/ypath.rb
@@ -0,0 +1,52 @@
+#
+# YAML::YPath
+#
+
+module YAML
+
+ class YPath
+ attr_accessor :segments, :predicates, :flags
+ def initialize( str )
+ @segments = []
+ @predicates = []
+ @flags = nil
+ while str =~ /^\/?(\/|[^\/\[]+)(?:\[([^\]]+)\])?/
+ @segments.push $1
+ @predicates.push $2
+ str = $'
+ end
+ unless str.to_s.empty?
+ @segments += str.split( "/" )
+ end
+ if @segments.length == 0
+ @segments.push "."
+ end
+ end
+ def YPath.each_path( str )
+ #
+ # Find choices
+ #
+ paths = []
+ str = "(#{ str })"
+ while str.sub!( /\(([^()]+)\)/, "\n#{ paths.length }\n" )
+ paths.push $1.split( '|' )
+ end
+
+ #
+ # Construct all possible paths
+ #
+ all = [ str ]
+ ( paths.length - 1 ).downto( 0 ) do |i|
+ all = all.collect do |a|
+ paths[i].collect do |p|
+ a.gsub( /\n#{ i }\n/, p )
+ end
+ end.flatten.uniq
+ end
+ all.collect do |path|
+ yield YPath.new( path )
+ end
+ end
+ end
+
+end
diff --git a/samples/class-book.rb b/samples/class-book.rb
new file mode 100644
index 0000000..54b56ec
--- /dev/null
+++ b/samples/class-book.rb
@@ -0,0 +1,43 @@
+require 'yaml'
+
+class Book < Shoes
+ url '/', :index
+ url '/incidents/(\d+)', :incident
+
+ def index
+ incident(0)
+ end
+
+ INCIDENTS = YAML.load_file('class-book.yaml')
+
+ def table_of_contents
+ toc = []
+ INCIDENTS.each_with_index do |(title, story), i|
+ toc.push "(#{i + 1}) ",
+ link(title, :click => "/incidents/#{i}"),
+ " / "
+ end
+ toc.pop
+ span *toc
+ end
+
+ def incident(num)
+ num = num.to_i
+ background white
+ stack :margin => 10, :margin_left => 190, :margin_top => 20 do
+ banner "Incident", :margin => 4
+ para strong("No. #{num + 1}: #{INCIDENTS[num][0]}"), :margin => 4
+ end
+ flow :width => 180, :margin_left => 10, :margin_top => 0 do
+ para table_of_contents, :size => 8
+ end
+ stack :width => -190, :margin => 10, :margin_top => 0 do
+ INCIDENTS[num][1].split(/\n\n+/).each do |p|
+ para p
+ end
+ end
+ end
+end
+
+Shoes.app :width => 640, :height => 700,
+ :title => "Incidents, a Book"
diff --git a/samples/class-book.yaml b/samples/class-book.yaml
new file mode 100644
index 0000000..c8f7c8d
--- /dev/null
+++ b/samples/class-book.yaml
@@ -0,0 +1,387 @@
+- - The Milkman Who Couldn't Sleep
+ - |
+ Ronaldo was a milkman who found that he couldn’t sleep at night. Rather than fight it, he determined to leave his house and sell milk.
+
+ The first door was answered by an elderly man who invited Ronaldo in for a glass of milk. Ronaldo sat at the table and said nothing until he realized that the old man was pouring him a glass of milk product from a competitor. Ronaldo went to stop the milk with his hands and his hands perished in the stream. As Ronaldo could no longer hold his own glass, the old man promptly ejected Ronaldo from his house.
+
+ The second door was answered by a man named Milton who had no defining characteristics except for an orange hankerchief tied around his head, a bionic arm, and an unforgettable expression of loyalty to governments of all manner. Ronaldo offered him a milk bottle. Milton picked up the bottle with his bionic arm. Impressed, Ronaldo retrieved the bottle of milk back from Milton’s bionic arm. Milton uttered something in the language spoken by human internal organs. Ronaldo’s stomach and lungs turned on their jetpacks and flew skyward.
+
+ The third door was answered by a priest millionaire. He had a giant cross made from crushed tens and twenties. As Ronaldo took a tour of the house, he looked around for spare internal organs and hands. As it happened the good Reverend took Ronaldo through a laboratory containing replacement body parts. Such excitement surged through Ronaldo that he ran toward a possible candidate stomach and lungs, running with such velocity so that he tripped and flew into a shoot that could only fit his lower body and his milk supplies. It was this remaining Ronaldo that was deposited outside the house and resumed the route.
+
+ The fourth door was answered by a pair of white-clad gentleman who took a beltsaw and whittled Ronaldo down to just a leg.
+
+ The fifth door was answered by a little colorblind girl with scorpions crawling all over her neck and arms. All she saw was the milk carton and Ronaldo’s leg. So she thanked the carton of milk and squeezed a glass of blood from the opening in Ronaldo’s leg.
+
+- - Emptiness
+ - |
+ Harriet and Clover were at the mall, trying on hats. Harriet happened to put on a certain hat that was loaded to the brim with mosquitoes. A few mosquitoes got the idea to hide there and a bunch of other mosquitoes simply copied the idea. They worked quickly on Harriet, devouring her to such an extent that when Clover came over to demonstrate a skirt, Clover screamed and dropped the skirt. Harriet had been replaced by emptiness.
+
+ These days the universe is all about emptiness.
+
+- - Regrets
+ - |
+ Humphrey worked pouring concrete for over twenty-five years, when one day he discovered that he was pouring concrete on to a cat. He stopped the mixer to rescue the cat, but he discovered that several cats were now firmly encased in concrete. Reaching, lumpy, and buried cats. Firmly encased.
+
+ Later that day, he wandered to an old job site and found that the sidewalk wound up and over the shapes of cats, dogs, birds and ostriches. His eyes darkened as he continued to visit curbs where he had inadvertantly paved over families, arcade machines, moving trucks. He was abhorred by his neglect and fiercely pounded his face with his fists.
+
+ His regret was vast and contoured. He longed to set things right. He took up a jackhammer and began banging into the solidifed shape of a man on a hangglider, but couldn’t make progress. Consumed by his guilt, he took a long walk through a parkway where the cars screamed by furiously. He made up his mind to throw himself in front of a car.
+
+ Fortunately, two stories above Humphrey, a minister happened to drop a massive Bible on Humphrey’s head, which crushed him instantly. A man should not have to kill himself if a large Bible can do it for him.
+
+- - The Berkowitz Manuevre
+ - |
+ Long before the actual city of New York, the city of New York existed in the mind of its creator. In this dream city, there lived an ordinary man. (Ordinary in the sense that only dream people can be. Which is to say: Extraordinarily plain for a very wispy, immaterial person.)
+
+ This man drew no attention to himself. He walked in such a way that he completely complied with crowds. His height was such that he was constantly overshadowed, though always present. His home was situated in a place where no one would ever look and his street address consisted of an imaginary number, such that it didn’t translate from the dream into the mind. It is common for such people to live this way in a dream, and it is most orderly of him to do so.
+
+ This ordinary man concealed a great secret from the mind of his creator. This secret was called the Berkowitz Manuevre. You know enough about it now, so please go away.
+
+- - Ignored
+ - |
+ Ormus the canary was born the size of a single atom. Which means that no one actually knew about him. He is much like the dream people and the other things that have happened so far.
+
+ He attends martial arts exhibitions and has seen some pretty cheap shots in his time.
+
+- - Birthstones
+ - |
+ Because she drank malts to excessive degrees, Edgar divorced his wife and decided to go back to school. He purchased school supplies and enrolled at a community college. He looked around the big campus with wonderment and met a young girl with a green glitter pen over by the fountain.
+
+ He stood by the fountain and said to her, “Hi, I’m Edgar. Do you like malts?â€
+
+ She said, “No,†and Edgar smiled deeply.
+
+ Her name was Tepiolia and she identified Edgar’s birthstone as the sapphire. This startled Edgar, as he had never had any connection to jewelry before. He was flattered and laughed like a child.
+
+ At the conclusion of their conversation, Edgar began his quest to find the world’s largest natural sapphire. He mountaineered for seventeen weeks and finally found a massive rock wall composed entirely of sapphire.
+
+ He surveyed the surface of the glimmering mountain, his thoughts returning to Tepiolia and her glitter pen. How strangely it has glittered greenly. His heart beat thickly as he drove the pick into the mountainside, desiring an adequate stone to return home with. Look at these stones, he thought. Birthstones.
+
+ From then on, everything he did involved sapphires and he missed out on some of the other brilliant things people do, such as playing pinball.
+
+- - Water
+ - |
+ Curdy Applehorn lived in a time when nothing existed except for a vast body of water. He felt serenity as he gazed upon the still waters.
+
+ Then he said, “If there is nothing but water, then what am I? Am I mere water as well?â€
+
+ At this point I must interject. Of course Curdy is not water. He is a character in a novel that someone else has written and I have borrowed him for this story.
+
+ And so Curdy wondered, “How delightful. I wonder what I am doing in the novel.â€
+
+ To which I add that the novel is a tale of a man named Curdy Applecorn, who (out of unsurpassed love) attempts to live with a species of oversized scorpions. As their only plaything, Curdy becomes subject to incessant stinging and a gradual excavation of his visceral cavity.
+
+ So then Curdy came to dread the coming of the end of this story. But he came to realize that there was nothing else to talk about really, as there was not a suitable environment for plot, motif, or character development here. So he said goodbye to the water.
+
+- - Waking
+ - |
+ Though she has been suicidal for some five months, Ruthie awoke one morning in an excellent prescence of mind. She descended the staircase and entered the kitchen with her arms spread. She kissed all the children and all the house animals. She kissed her servants, her attendants and accountants, finally ending with a kiss to the center of the floor. She also did a twirl after that.
+
+ The faces of her servants were especially grim, so she asked them, “Does my sudden change of face frighten you? Does it shock you really?â€
+
+ “Noâ€, a maid said. “But did you forget? You locked the other maid Madeline in the meat locker last night and burned her possessions. Do you not remember? It was dreadful.â€
+
+ But Ruthie’s mind was in such a state of bliss that she couldn’t comprehend such disaster. And she did let Madeline out of the meat locker and gave her four days of paid vacation and $500 spending money.
+
+- - The Man
+ - |
+ A man built an automobile entirely from carrots and was known for it in several counties. When people spoke with him, they would speak of nothing but carrots. There came a day when the man realized that he couldn’t remember ever having non-carrot discussions. Fueled by an intense will to free himself from this cruel and perpetual cycle, the man set off to blaze a new trail for himself.
+
+ The man rented a bird costume and jumped off the roof of a hotel in town. He didn’t survive the fall, which was probably best, as soon there wasn’t a man nor woman on Earth that didn’t drive a carrot car.
+
+- - Ghosts
+ - |
+ Spencer woke up one night to a crash downstairs in his manor. He raised the torch off the wall and crept silently to the stairs.
+
+ The lights were on all across his estate. From his vantage point at the top of the stairs, he could see twelve ghosts painting his walls, touching up the trim, and hanging new drapes. He screamed in terror, possessed by the thought: These ghosts must think this house is theirs now. He turned to hide and sort out his strategy, but the ghosts sang in twelve-part harmony and soothed Spencer into a calming stupor. It was no time at all until Spencer determined that these were genuinely good ghosts that had simply come to remodel his beautiful palace.
+
+ In the end, Spencer overworked the twelve ghosts to the point that each ghost wound up looking like a long thread with some bumps in it.
+
+- - The End
+ - |
+ Michel not only found a decent rope at the hardware store, but he purchased it at a discounted rate because the rope only had one end. He went to stow the rope in the garage, but his wife stopped him and poked him with her hand.
+
+ She pointed back at all of the rope lingering outside the door and said, “Aren’t you going to wind this rope up on a spool?â€
+
+ “I can’t,†replied Michel helplessly. “This rope has no end. I could begin winding and never stop.â€
+
+ Michel’s wife then asked, “Well, then why didn’t you cut a section off?â€
+
+ He caught himself just as he was about to say, “I didn’t think of that.†He didn’t say it, as he knew it would confirm many of his wife’s suspicions about men. So he said, “Here,†and tossed the rope over his wife’s head and a coil of it landed in a box in the garage.
+
+ He went upstairs to look at photographs through a magnifying glass.
+
+ Michel’s wife ventured into the garage and poured gasoline on to the rope and let it burn, confirming many of Michel’s suspicions about women. The flame burned rope from their home to the hardware store, then began travelling along the rope through space, towards the point at which infinity begins.
+
+ As it raced along, the tiny flame ignited the atmospheres of planets, each of which glowed brightly for a time and then crumbled distantly in the rope’s trail. Space crackled as the strength of the flame grew. Soon it was a comet and, soon, a blazing monstrosity. The fireball grew to fill an entire universe and its size began to grow quicker than its acceleration. And so it reached backwards toward Michel.
+
+ At first, he was confused by the intense heat that came through the magnifying glass and lapped up his photographs. But soon he was no more.
+
+- - The Incident
+ - |
+ A crew of news reporters was wandering the neighborhoods, looking for incidents to report. They found several to choose from and finally focused on a certain infestation of fruit flies. They were alarmed at the quality and intimacy of the footage they recorded that afternoon and found it difficult to leave the scene of the incident.
+
+ Taking some artistic license, the cameraman broadened the camera view to capture scenes of children who (hearing of the news crew) came to ride their bicycles and appear on the news. The cameraman continued to film one dark-haired boy and eventually followed the young man on to college at Stanford, also filming his adventures in London and the Slavic countries.
+
+ Some stayed and lived with the fruit flies and others evaporated at sunset.
+
+- - Duck Typing
+ - |
+ Once there was a man who walked like a duck and talked like a duck. He was made into a very long pillow.
+
+- - The Man Who Happened to Reach Up
+ - |
+ There was a man who happened to reach up higher than anyone ever had before, so his hand got stuck in a cloud. And he got dragged all over the sky.
+
+ A lot of his old buddies really got a kick out of seeing him fly by whenever they showed weather maps on the news.
+
+- - The Man Who Happened to Have Legs
+ - |
+ One of the first strange things to ever happen was when a man was born and turned out to be nothing more than twelve legs coming from a cloud. When people saw him, they usually ended up thinking that he was David Blaine or some other magician pulling a stunt.
+
+ So David Blaine got a really bad rap about it and people started putting a lot of pressure on him to do regular card tricks that just told a story or something.
+
+- - The Skier
+ - |
+ One man had become such an impulsive skier that he started skiing down dirt roads in the summer. It actually wasn’t such a bad sport and the guy won a silver medal when Dust Country Skiing went national.
+
+ The fashion these people wore was atrocious, though. It was common to see ski poles with leather tassles on the handles and such nonsense.
+
+- - Javek and The Candle
+ - |
+ Devilish beasts invaded the property of a man Javek and chased him from his premises. He ran for days, with only a small candle to help guide him. The chase became so exhausting and elaborate that Javek’s fear became absolutely perfect. Running from broad monsters and always hearing their thick breath, he viewed himself as a tiny creature, a morsel. He ultimately sought love and guardianship from the candle, which the candle willingly gave for many years, long after the monsters had lost interest in Javek.
+
+ On a certain spring day, Javek sought shelter from the rain and walked along the parkway, peering into the village stores. One sign read:
+
+ We need someone strong to watch our candles at night.
+ Call us if you can do it.
+
+ Javek walked into the store and forthrightly told the store manager, “Let me watch your candles. I am not strong, but I will show them such love! Here, see how I always carry a candle with me.â€
+
+ He held out the candle, which had formed its own grooves for Javek’s fingers. The store manager was in the middle of a sale and asked Javek to pardon him for a moment while he finished ringing up a customer’s bill. When the customer left, the manager told Javek that he had just sold all the candles in the store (to that customer who just left!) and was retiring. He no longer needed a candlewatcher. The manager apologized that Javek had come all the way down to the shop and encouraged Javek to call in advance next time.
+
+ Javek raced from the store and followed the customer who had purchased the store’s supply of candles. He spied on the man all day, watching the man as he negotiated at the market and as he met up with a pilot friend for lunch.
+
+ The day became evening and the man walked home. Javek shadowed the man, walking with soft, waxy steps. When they hit Hoyt Ave., the man turned into one of those devilish beasts.
+
+ In that moment of terror, Javek’s first thought was, “Oh no, he’ll eat my candle!†Which was ridiculous. And Javek knew it was ridiculous, so he laughed. A person can only get so serious about a candle before he has to loosen up about it.
+
+ (And for those of you who are wondering: No, the devilish beast didn’t hear Javek laughing. And no, the beast didn’t come and eat Javek. The beast went home and ate half a pig and a few candles, then slept soundly.)
+
+- - Milk Powers
+ - |
+ Trace was carrying five cartons of milk into her room and she lost control of them, spilled them all on to her bed. The milk made a decent bedspread.
+
+- - The Little Piece of Cloth
+ - |
+ Let us say that there was a little piece of cloth that could fly. And it was joined in midair by a tile which could fly in like manner. The little piece of cloth and the tile became extremely devoted to each other over the years. The little piece of cloth felt she was in love with the tile, though she never spoke candidly of it.
+
+ It turns out that so much flying can cultivate love. And their love advanced to the point that they actually made a promise to each other. They promised on the number one that they would always fly with each other and never stop flying. (Cloth and tile are so simple as to only understand the number one. It is like God, but also like a discus.)
+
+ The promise brought pleasure to their lives and they flew for a decade in a sleep, side-by-side. Their calmness settled upon the world as well and there was rich, warm silence.
+
+ Slowly, the wind died down until it could carry them no longer and they fell straight into a pond. Though initially startled, the water refreshed them and their senses enlarged. And for some reason, they didn’t care to much about the promise. They gave each other sinister looks and laugh at their naughtiness. Ah, how lovely that good friends can easily fall into pools and laugh jointly at their broken promises!
+
+- - The Tandem Bicycle
+ - |
+ A man and his wife were vacationing on the beach and decided to rent a tandem bicycle. After browsing over the various models, they decided upon one which had seating enough for all of the world’s inhabitants. (This included all kinds of animals, crustaceans, plant life, etc.) They took the bike for a spin, kindly offering rides to everyone they met.
+
+ Soon enough, the bike was full of all of the inhabitants of the entire Earth. The couple set a very relaxing pace, so no one got tired. Onwards the great string of entities calmly rode together, watching the skyline.
+
+ Suddenly, it occured to the couple that if they could sneak off the bicycle, then they could steal the entire planet while no one was looking. They snuck off the bike, still moving their feet in the air, as if they were pedaling. The illusion worked perfectly and the husband conquered America while the wife took over Asia and Europe. It turns out that a little boy named Tyler had also snuck away and captured Africa, so he was adopted by the husband and wife.
+
+ Everybody else stayed on the tandem bicycle. So, years later, when the husband and the wife and Tyler got bored and wanted to get back on the bike, they couldn’t find their old seats.
+
+- - The Queen-Sized
+ - |
+ Once there was a queen-sized mattress who had back problems of her own. This went on for years.
+
+ Then, the movers came. The people who slept on her were moving. The movers picked up the weary mattress and a massive spinal cord fell out from the underside of the queen-sized. The movers screamed in horror, but the queen-sized felt such relief. Euphoria spread across her quilted lumps and she whispered to the movers, “Be still, my busy friends. (Though it be easier for me since my nervous system fled.)â€
+
+ And I just want you all to know that this story is not in any way about accepting paralysis or even wishing for it. I honestly can’t say it’s about much at all.
+
+- - Wristwatches
+ - |
+ A man had a talking wristwatch. It’s true. Unfortunately, this man was a very poor conversationalist and he rarely spoke to the poor watch. In time, the watch became estranged from its owner.
+
+ But one day there was an attack on Earth. A roller coaster of aliens, with two to each seat, and with the over-the-shoulder restraints, came writhing through the atmosphere and started blasting away at all the people. The man with the wristwatch was there. He panicked. Sprinted through the streets looking for an obscure hiding place where he could never be found.
+
+ The wristwatch screamed as well, “Get us out of here! Over in to the cathedral there!â€
+
+ In the hysteria, the man found himself naturally speaking with the watch, uninhibited. “I’m going to take care of you!†he yelled.
+
+ The cathedral happened to be the alien homebase, though. When the man came in close proximity, his bones were ripped out of his body by a huge bone magnet the aliens had. Tragic, I know. The watch wailed horribly, but it used vibrate mode, so the sound didn’t attract the attention of the assailants.
+
+ To swiftly close, let me add that the humans ended up winning the battle, saved by some quick thinking on the behalf of a bunch of inner city skateboarders. The rescue team secured the watch, who was immediately informed that he was not the only watch of his kind, that there were in fact millions of talking wristwatches. But he had a rough recovery, was left with a pretty bad tick, and it turned out that the other watches were a pretty ruthless group, so they referred to him by a derogatory term which isn’t a vulgarity for humans. Still, out of respect for these devices, I’ll curb my sharing of it with you.
+
+- - The Advisor
+ - |
+ It just so happened that Klaus Mashwitz was excellent at building robots and he built himself a robot to act as his personal confidante and close advisor. The Advisor accompanied him everywhere and the two were inseparable. Their fame spread. Klaus built other machines. He burned through forty million dollars and five marriages before The Advisor stopped him mid-stride, right when they were approaching the bamboo garden in Golden Gate Park.
+
+ “I’ve been a good friend and a proper counselor these many years,†said The Advisor, his hand on Klaus’ chest, holding him still, “and yet, looking back, I can see that my advice has been worthless to you. You’ve ignored all of it, the very information you designed that I give to you. And now, your life is in ruin.â€
+
+ Klaus was silent and nodded carefully. He spread his fingers out and answered only with, “What can I tell you?â€
+
+ The Advisor was firm, “You can start taking my advice.â€
+
+ “I’ll tell you what,†said Klaus. “You’ve always been kind and good to me. So, I’ll strike a deal with you. You give me one bit of advice and I will follow it. If it works out, I will follow the next bit of advice. And so on. I’m an old guy now, but—who knows—maybe it’s time for me to put it in cruise control and hand things over to my oldest friend.â€
+
+ Now, you’re not going to believe this, but Klaus took the bit of advice offered by the shrewd robot, his advisor! Gruesome as it is, The Advisor asked Klaus to kill himself. That way, The Advisor could replace Klaus’ insides with new robotic viscerals and brain functions. A new Klaus which would live forever and live right and no one would know any different.
+
+ And so, Klaus did the shooting and The Advisor did the gutting.
+
+ Soon enough, the new Klaus was out on the streets, charming and dazzling mankind. Most people knew it wasn’t Klaus, though. The Advisor’s measurements were off a bit and about an inch of metal was exposed on the inside of Klaus’ left eye. And the new Klaus had such a weak stomach that he often disturbed family meals by vomiting diodes all over the place.
+
+ But people got along with him. Good advice is good advice.
+
+- - Speaking of Flutes
+ - |
+ I am authorized to speak of flutes and certain floutists, one of whom was interested in furthering his career and in forcing flutework to the forefront of pop culture. So this floutist, he parachuted into small towns, playing trills all the way down.
+
+ The first town he landed in was called Eccles. He captivated the crowd below, but fell straight through a new walnut bureau, crafted by a local artisan. Newpapers read: “Stray Piper Strikes Through Solid Drawers†or such nonsense.
+
+ Fall came and he orchestrated a second drop over a larger city, Merriwealth. And, again, bedazzlement. People loved it. The city pool was also opening that day. Sadly, no ripples were made in its surface. And a child left his pet bat’s cage open. A suit store was robbed. The printed headlines broadly rang, “Charm Upon City Unstitches One Tailor’s Already Threadbare Pockets.â€
+
+ And the floutist also saw Rascot. A town which is nothing but forest. The preceding day, the town was ravaged by an upright dog. The floutist ran for ten miles before the monstrous dog overtook him and bellied him whole.
+
+ So, from the newspapers: “Dog Man At Last Acts Favorably.â€
+
+- - Kimothy's Mouth
+ - |
+ Kimothy’s mouth was a warp zone. Which meant that infrequently—gosh, let’s say about as often as two archers dive into an ocean simultaneously—a pair of trousers or a coupon book or something would fly off her tongue. This even happened at church and at job interviews.
+
+ You should also know about one day in her history when a man dressed in radios all over ran up to her and said, “I need to deliver this Mondrian Cowl to the Dreyfrowns!†And he shoved a monkey statue in her mouth.
+
+ Okay, so get this. A little while later she was waiting for a friend, down in this hotel lobby. And she was looking around and she saw that same monkey statue over by the fireplace! The one for the Dreyfrowns.
+
+ She tucked her chin down and, very discreetly, nudged the monkey statue back into the fireplace. And, slowly, everso slowly, she let it topple out of her lips.
+
+ The rest of the afternoon Kimothy spent swallowing coins and watching hot nickels land on the feet of people who went to warm their hands. That is, until a guy with giraffe hands came out of her mouth and said, “Stop screwing with our warp zone!†and he slapped her a memorable number of times right on the forehead with his flattened palm.
+
+- - The Jump Wanter
+ - |
+ After watching The Absent-Minded Professor, this kid named Esel Striff wanted his own bouncing shoes. He was a bright kid, so he invented a pair of shoes that could leap twelve solid feet into the air.
+
+ His calculations were, sadly, very completely wrong. And when he went to try the shoes out, he found that he had not invented jumping shoes; but, instead, he had invented shoes which rumbled and popped, before transforming his legs into the thick, hairy legs of a Clydesdale horse.
+
+ His legs were beautiful and most professional equestrians considered his legs to be “show quality.†One horse enthusiast, Liberty Shale, was intensely attracted to him, and pursued him shamelessly, having wanted a real centaur for herself since purchasing a pewter figurine (equipped with hair beads and a powderhorn) at the tender age of 25.
+
+ This, along with the cloud of flies continually harvesting the mucus from his eyes, put tremendous strain on Esel’s psyche. He soon felt the only answer was to invent a complete set of bouncing horseshoes. (Yes, after a viewing of The Absent-Minded Professor.)
+
+ And that’s why, to this day, you can spot a centaur with a lab coat, wandering the countryside with Korean food spontaneously spewing from his hooves. Esel Striff, ladies and gentlemen.
+
+- - The Life Guy
+ - |
+ This guy—the Life Guy—ate nothing but heat lamps and defecated only wholy intact live salmon. Such was his appetite. And talk about healthy. Renaissance painters could have spent years attempting to capture the rosiness in the Life Guy’s cheeks.
+
+ At the coaxing of one of his roommates, who was pretty convinced that the Life Guy’s eating habits were a manifestation of deeper issues (the Life Guy lacked a solid father figure) and that perhaps the lamp devourings were shredding the esophagus, the Life Guy went in for a colonoscopy. Well, the doctor discovered that his insides were fine and that they simply consisted of a complex ecosystem of lifeforms. An entire world where the heat from the lamps fed the plants under the sea, which lifted the rivers up and pushed them along, which leaked springs and bubbled the lilypads, which stimulated the activity of crocodiles and caused their teeth to swell into marionettes which harvested fish as a pasttime—fish which were ultimately rubbish to the entire life cycle and were tossed in the rain gutter.
+
+ Anyway, the gastroenterologist had kind of a crazy idea and, considering that such an opportunity was unlikely to present itself again, he went ahead and transplanted a dollhouse art museum in the very center of the universal innards. He then stitched up Life Guy and sent him on home.
+
+ However, I am sad to report that the art museum’s grand opening was very well attended by the marionettes. So many guests arrived that the caterers of the event ended up tossing out leagues of soiled paper place settings, some of which they sent up in hot air balloons to clear off the driveways. When Life Guy woke up the next morning, every passageway was clogged, he tried popping a few Sudafed but suffocation couldn’t wait for its moment. You can blame the gastroenterologist if you like for the death of Life Guy, but it’s just another textbook case for microdecoration’s misplaced zeal.
+
+- - The Story Life Doesn't Explain
+ - |
+ Once there was another guy, same as The Life Guy: he ate heat lamps and crapped fish. But this one didn’t have an ecosystem inside. This time, it was just the guy’s diet.
+
+ He went deepsea diving with his business partner, Jamie Tartreuse. While he was down there, he saw a swordfish swimming along that had a whole giraffe carcass stuck on its nose.
+
+ The guy didn’t panick or anything, but he did say to himself, inside his mask, “From now on, life isn’t self-explanatory.†That guy hit the nail on the head.
+
+- - The Grieving Boar
+ - |
+ So this random boar lost his whole family in a missile attack. Some guerillas were camped out by his family’s cave and the whole thing went down bad. Worse yet, his favorite fruit, the seedless storkberry was wiped out during the attack. He wandered for three-hundred miles and found nothing close to the succulence and aroma of the seedless storkberry. Like I say, we have a grieving boar on our hands.
+
+ Anyway, he resorted to eating live storks which happened to perch on low branches. He really acquired a taste for them, since they are seedless as well. And so, years later, when he happened to wander across an actual seedless storkberry tree, he didn’t even recognize it. In fact, he was in charge of a special ops team plagued by poor visibility and he didn’t think twice before blowing up the tree with a missile and feasting upon the blasted-out remains of the pile of storks who fell from the tree.
+
+ He was a pretty despicable creature by now. When he couldn’t reach his kids on mobile phone, he’d have his subordinates dial the number over and over until someone picked up. My sister’s boss does the same thing to her and I simply refuse to write incidents about this kind of scum.
+
+- - Jentle and Pailey
+ - |
+ I don’t know how he figured it out, but this one particular 21st century human male discovered that by rubbing his face against a goat’s face, he could produce this ungodly wailing sound that was so loud most people in the countryside thought a village was getting ripped in half. One old man heard the sound through ten stacked windshields he fell asleep underneath. The soundmaker’s name was Jentle and he was a real troublesome guy, always kicking milk pails or running off to making that ungodly sound with the goat.
+
+ Now believe it or not, on the other side of the world, exactly opposite Jentle was a little girl who could produce the same exact sound by running a frog along her shoe laces. It was truly startling! People thought an ocean liner had crashed into a dinosaur and the dinosaur had screamed. An indian fellow jumped out of his bathroom and threw his tomahawk for a new world record because of it! The little girl’s name was Pailey. Oh and, I forgot, a guy fell off his rollerblades.
+
+ The sad part in all this is that these two, Jentle and Pailey, never got to meet each other. They both died of shattered bones because of sound waves which continued to ricochet in their skeletons. A reporter in Miami who was the first to piece together these two stories called it tragic and just another example of the growing dischord in our generation. I wouldn’t say it’s that bad, but I do think their names sound good together. Jentle and Pailey. When you say it like that, it is heart-breaking, no doubt about it.
+
+- - The Hand Which Fell Apart
+ - |
+ Years ago, I lost touch with Arthur Aide, but I just ran into him at the theater. Turns out he got into a huge fight with his girlfriend and she moved out after that. But instead of his heart breaking, his right hand broke. The minute she was gone, it fell to pieces.
+
+ So he went to the hospital to get his digits stitched back on and during recooperation he started to reflect on his relationship and he remembered that this distressed hand was often the most committed part of him. It stroked her hair, it wiped the smudges from her chin, it snapped the photos, pushed the button, focused the lens. And often she would remark on how warm his hand was, such that he felt an obligation to run it under the hot faucet prior to their encounters. It really made sense. His hand loved his girlfriend more than he himself did—and its heart had broken.
+
+ But this was entirely untrue. During a vacation, while hiking in Mexico, the girl had dropped a rock on Arthur Aide’s hand, killing it dead, but managed to keep the hand together with a willing telepathic maneuver. She had, with great effort, held the appendage intact for nearly a decade. And then she left.
+
+ Well, the night they broke up, she slept at her friend Alloieann’s place for a few evenings and was astonished at how deep her restful state was. Normally she was taking four Excedrin a day. None of that. She jumped off a diving board for the first time in just as long. In fact, everything was just smashing until a waiter at Red Lobster dropped a tray and she involuntarily snagged it with her mental powers.
+
+ The jig was up and Arthur says the last he heard, his girlfriend was sold to the FBI by her own father for $280,000 and a lifetime supply of cheesesteaks. My brother interrupted me just then to borrow five bucks, but have to admit that I was quite disappointed that she was unable to bamboozle her captors. Why didn’t she use the FBI’s neckties against them? Why didn’t she topple a jar of dice from an upper shelf upon them? Couldn’t there have been a sudden appearance by a jetski?
+
+ I waited until after the matinee to catch up with Arthur again and I let the bell at city hall ring twice before taking the crosswalk.
+
+- - A Smart Curtsy
+ - |
+ Princess Tesgid of the Kingdom of Alsacious was universally loved by her subjects. And how genuine it was. Their wall-hangings were images of her. Their works of calligraphy all memorialized her finest sayings. They used her name so much that the air became more accustomed to that series of vocal vibrations and you could hear it repeating against the clouds and mountains. And rightfully so, she wrote very good poetry about the plight of her citizen’s poverty and was incredibly sympathetic toward them all.
+
+ In her most recent anthology So Many Fingers Doth Dwindle and Chime, she wrote:
+
+ When I see the urchin’s knees,
+ shuffling through the clover patch,
+ their socks let go miles past,
+ their gloves reek as an ostrich gizzard
+ and their eyes, oh,
+ now their eyes do still give a smart curtsy.
+
+ While you or I might neglect to sense the nuances of these words, I assure you that this was a terribly tragic stanza for the Princess and the message received by the street folk was: we have been coarsely mocked. In the ruling society, “a smart curtsy†would generally be regarded as a polite dip of the head with an underlying bouquet of wonderment. However, a divergent meaning had sprung from the darker slums and shipyard embankments which twisted the term “a smart curtsy†to describe the motion of beheading a child infected by the parasitic spirit of a deceased feral cat.
+
+ What’s more, the clover patch reference was unmistakable! That was up Swollery’s Cliff, the field where those cats had been petrified into an upright column of maple sap. How dare this Princess rail against the totems. Be she in league with the cats now?
+
+ The city stirred, many ill with worry of riots and looting, others retreating to quiet places where they could internalize the poems under the soft light of the moon and the warm pyres of freshly massacred cats. All were scraping that third line for meaning. Where did the socks let go? Discovering the exact location could possibly vindicate the Princess, particularly if it were at the national Cemetary of the Beloved. Or better yet, the dessert counter at Stage and Svenson’s. That would be quite hilarious actually!
+
+ The Princess was aloof from all this, working at home on upgrading her word processing software which insisted that it needed Disk 3 when she was sure that very disk was in the slot right now!
+
+- - The Secret Sandwich
+ - |
+ Legend speaks of a sandwich—very discreet, somewhere—a sandwich well-concealed and expertly invisible. A sandwich so secret that its most vocal defenders absolutely doubt its existence. And they cite this prevelant doubt as the only actual proof of its existence. Sure, but do they have to say it with such skepticism?
+
+ The only tangible proof was uncovered some years ago when an advertisement appeared in the Aberdeen Examiner. A mail-order cassette titled Sounds of the Secret Sandwich. A cassette which was largely blank, save for a brief conversation at 23’10â€. A little child says, “Dad, dad!†and the wind is blowing. Then, this raspy voice says, “What is that? Are you wearing a beret?†The kid says really loud, “WHAT??†And the older voice says, “That’s cute, come here, show me, what is that you’re wearing on your head?†And there’s some movement and the kid is quiet, the wind dies down and the older voice says, “Oh, sure, I know what this is—it’s a—uh—it’s a dead bird.†So, I have my doubts about the validity of this recording.
+
+- - A Magic Milk
+ - |
+ So, one day, at about two in the afternoon, a furniture salesman named Shelts took a break and sat down to enjoy a glass of milk outside, a delicious glass of whole milk, above a velutinous panorama of hills and mists. Well, halfway into his glass, he almost gulped down a sock that was floating in the drink. Quickly, he fished it out. Aha! A long, wet tube sock with red stripes at the top.
+
+ Shelts was absolutely grossed out, how revolting, especially considering that the bottom of the sock had some wear and tear, and he dashed off to chuck it over the cliff into the velutinous panorama. But before he let get of it, the sock cried out, “Please, Shelts! Don’t do it. Don’t throw me over! I will do anything!â€
+
+ He said to the sock, “Oh, really? What exactly can you do for me? I am really furious about this.â€
+
+ “Well,†said the sock, “I can talk. Do you happen to like conversation?â€
+
+ “Not really,†said Shelts. “I prefer peace and quiet.â€
+
+ “Oh, well, no problem,†said the sock. “I can just shut up and keep one of your feet all warm and cozy.â€
+
+ “Oh no no, that just sounds uneven,†said Shelts. “I really don’t like any of the options you’ve offered so far. What’s more I didn’t at all like the taste of you skulking around in my milk in the first place. So off you go.â€
+
+ And with that, he threw the sock off the cliff, off to drift down the hills and mists, off into the velutinous panorama. And that would have been the end of it, except that the magic of milk did not stop there. No, hardly.
+
+ Milk enjoyed such a remunerative renaissance, this new kind of milk that could spontaneously generate sentient socks and curious coats of all kinds. Milk became the great worldwide seamstress and no gulp or swallow went without a complimentary sleeve streaming from the side of every cup.
+
+ Eventually, it dawned upon Shelts that he had been quite unfair. And as the furniture business took one of its occassional plummets, Shelts found himself begging the cartons in his own refrigerator for some kind of gloves or vest. Even a sock, a worn tube sock, with a dirty sole and red stripes, perhaps? What a terrible spot Shelts had got himself into, not a single gallon or half would pay mind.
+
+ Ah, but no matter, he didn’t last long. Milk and its magic soon learned to generate more milk and more milk magic. And the land was awash in self-reciprocating dairies, happily lapping against each other, so that there were no more roads and no furniture and no more furniture salesman to sell furniture and no hills or mists and no velutinous panoramas.
+
+ All that was left in that whole world was a clumsy antelope who had gotten herself trapped in an airtight barrel, bored and snorting, bobbing across the many milks.
+
+- - Adventure of The Apple's Mom
+ - |
+ One of the most amazing things to ever happen in Peru happened to a monkey, an adorable, little trained female monkey, the property of one Emilio DeBuana. Emilio truly loved his pet monkey—I mean this animal had such a fantastic smile and huge pearly eyes.
+
+ But, see here: this monkey’s life was suddenly tossed upside-down when she gave birth to an apple, well-polished with a squinty and agonized male face. The monkey worked hard to give the little apple a proper life, frantically soothing it and swaddling it in hot towels, breast-feeding it, but it was often inconsolable, moaning deeply night and morning.
+
+ The monkey, once so happy and frolicsome, now found her life to be miserable. The crispy apricot leaves which had once made so her so ecstatic and backflippy, now dissolved in her pockets no thanks to the erosive stains left where the apple had soiled. She also could not count with her fingers, which crossed over themselves, perplexing the eyes. Everything had been cursed, had been smitten by the apple’s constant foul language. Her bookcase was even eaten by flies!
+
+ So she planned her escape one night. She folded an origami tortilla airplane. She got inside. She set a course for the North Pole. And she sailed off the top of Mt. Abalacion.
+
+ This left Emilio with custody of the apple. He planned to poison the apple or set a worm upon it. These plans did not come to fruition. He simply panicked and offered the apple some chewing tobacco, which the apple man gladly chomped down on with blocky porcelain teeth and the two made their days in a lazy trance on the front stoop, filling up spittoons and rolling bottles to each other and hitting dogs with slingshots.
+
+ The monkey’s airplane eventually landed on a distant moon, a very fragile moon, delicate as an egg shell dipped in a mixture of thyme leaves and coconut milk. In fact, her plane had a very sharp nose which pricked a hole in the light shell of the moon. The inhabitants of other nearby planets had called it Nordium—which is to say The Best Ball. Now, after the plane crash, it was renamed Jenny, which translates Ruined For Everyone.
+
+ For over a decade, the monkey sat stranded, managing to subsist on the milk fuzz inside the hollow moon. Oh, and tortilla. In time, a technician from the phone company dropped in to set the monkey up with basic cable and call waiting. He told her to hit the TALK button twice to click over to call waiting, but I swear she managed to hang up on every single monkey that tried to call.
diff --git a/samples/expert-definr.rb b/samples/expert-definr.rb
new file mode 100644
index 0000000..169e07a
--- /dev/null
+++ b/samples/expert-definr.rb
@@ -0,0 +1,23 @@
+Shoes.app :title => "Dictionary, powered by Definr", :width => 370, :height => 320 do
+ stack do
+ background red, :height => 60
+ flow :margin => 20 do
+ caption "Define: ", :stroke => white
+ @lookup = edit_line
+ button "Go" do
+ download "http://definr.com/definr/show/#{@lookup.text}" do |dl|
+ doc = dl.response.body.gsub('&nbsp;', ' ').
+ gsub(%r!(</a>|<br />|<a href.+?>)!, '').
+ gsub(%r!\(http://.+?\)!, '').strip
+ title, doc = doc.split(/\n+/, 2)
+ @deft.replace title
+ @defn.replace doc
+ end
+ end
+ end
+ stack :margin => 20 do
+ @deft = subtitle "", :margin => 10
+ @defn = para ""
+ end
+ end
+end
diff --git a/samples/expert-funnies.rb b/samples/expert-funnies.rb
new file mode 100644
index 0000000..27e5198
--- /dev/null
+++ b/samples/expert-funnies.rb
@@ -0,0 +1,51 @@
+require 'hpricot'
+
+class Comic
+ attr_reader :rss, :title
+
+ def initialize(body)
+ @rss = Hpricot.XML(body)
+ @title = @rss.at("//channel/title").inner_text
+ end
+
+ def items
+ @rss.search("//channel/item")
+ end
+
+ def latest_image
+ @rss.search("//channel/item").first.inner_html.scan(/src="([^"]+\.\w+)"/).first
+ end
+end
+
+Shoes.app :width => 800, :height => 600 do
+ background "#555"
+
+ @title = "Web Funnies"
+ @feeds = [
+ "http://xkcd.com/rss.xml",
+ "http://feedproxy.google.com/DilbertDailyStrip?format=xml",
+ "http://www.smbc-comics.com/rss.php",
+ "http://www.daybydaycartoon.com/index.xml",
+ "http://www.questionablecontent.net/QCRSS.xml",
+ "http://indexed.blogspot.com/feeds/posts/default?alt=rss"
+ ]
+
+ stack :margin => 10 do
+ title strong(@title), :align => "center", :stroke => "#DFA", :margin => 0
+ para "(loaded from RSS feeds)", :align => "center", :stroke => "#DFA",
+ :margin => 0
+
+ @feeds.each do |feed|
+ download feed do |dl|
+ stack :width => "100%", :margin => 10, :border => 1 do
+ c = Comic.new dl.response.body
+ stack :margin_right => gutter do
+ background "#333", :curve => 4
+ caption c.title, :stroke => "#CD9", :margin => 4
+ end
+ image c.latest_image.first, :margin => 8
+ end
+ end
+ end
+ end
+end
diff --git a/samples/expert-irb.rb b/samples/expert-irb.rb
new file mode 100644
index 0000000..8960ef7
--- /dev/null
+++ b/samples/expert-irb.rb
@@ -0,0 +1,112 @@
+require 'irb/ruby-lex'
+require 'stringio'
+
+class MimickIRB < RubyLex
+ attr_accessor :started
+
+ class Continue < StandardError; end
+ class Empty < StandardError; end
+
+ def initialize
+ super
+ set_input(StringIO.new)
+ end
+
+ def run(str)
+ obj = nil
+ @io << str
+ @io.rewind
+ unless l = lex
+ raise Empty if @line == ''
+ else
+ case l.strip
+ when "reset"
+ @line = ""
+ when "time"
+ @line = "puts %{You started \#{IRBalike.started.since} ago.}"
+ else
+ @line << l << "\n"
+ if @ltype or @continue or @indent > 0
+ raise Continue
+ end
+ end
+ end
+ unless @line.empty?
+ obj = eval @line, TOPLEVEL_BINDING, "(irb)", @line_no
+ end
+ @line_no += @line.scan(/\n/).length
+ @line = ''
+ @exp_line_no = @line_no
+
+ @indent = 0
+ @indent_stack = []
+
+ $stdout.rewind
+ output = $stdout.read
+ $stdout.truncate(0)
+ $stdout.rewind
+ [output, obj]
+ rescue Object => e
+ case e when Empty, Continue
+ else @line = ""
+ end
+ raise e
+ ensure
+ set_input(StringIO.new)
+ end
+
+end
+
+CURSOR = ">>"
+IRBalike = MimickIRB.new
+$stdout = StringIO.new
+
+Shoes.app do
+ @str, @cmd = [CURSOR + " "], ""
+ stack :width => 1.0, :height => 1.0 do
+ background "#555"
+ stack :width => 1.0, :height => 50 do
+ para "Interactive Ruby ready.", :fill => white, :stroke => red
+ end
+ @scroll =
+ stack :width => 1.0, :height => -50, :scroll => true do
+ background "#555"
+ @console = para @str, :font => "Monospace 12px", :stroke => "#dfa"
+ @console.cursor = -1
+ end
+ end
+ keypress do |k|
+ case k
+ when "\n"
+ begin
+ out, obj = IRBalike.run(@cmd + ';')
+ @str += ["#@cmd\n",
+ span("#{out}=> #{obj.inspect}\n", :stroke => "#fda"),
+ "#{CURSOR} "]
+ @cmd = ""
+ rescue MimickIRB::Empty
+ rescue MimickIRB::Continue
+ @str += ["#@cmd\n.. "]
+ @cmd = ""
+ rescue Object => e
+ @str += ["#@cmd\n", span("#{e.class}: #{e.message}\n", :stroke => "#fcf"),
+ "#{CURSOR} "]
+ @cmd = ""
+ end
+ when String
+ @cmd += k
+ when :backspace
+ @cmd.slice!(-1)
+ when :tab
+ @cmd += " "
+ when :alt_q
+ quit
+ when :alt_c
+ self.clipboard = @cmd
+ when :alt_v
+ @cmd += self.clipboard
+ end
+ @console.replace *(@str + [@cmd])
+ @scroll.scroll_top = @scroll.scroll_max
+ end
+end
diff --git a/samples/expert-minesweeper.rb b/samples/expert-minesweeper.rb
new file mode 100644
index 0000000..8053f90
--- /dev/null
+++ b/samples/expert-minesweeper.rb
@@ -0,0 +1,267 @@
+#
+# Shoes Minesweeper by que/varyform
+#
+LEVELS = { :beginner => [9, 9, 10], :intermediate => [16, 16, 40], :expert => [30, 16, 99] }
+
+class Field
+ CELL_SIZE = 20
+ COLORS = %w(#00A #0A0 #A00 #004 #040 #400 #000)
+
+ class Cell
+ attr_accessor :flag
+ def initialize(aflag = false)
+ @flag = aflag
+ end
+ end
+
+ class Bomb < Cell
+ attr_accessor :exploded
+ def initialize(exploded = false)
+ @exploded = exploded
+ end
+ end
+
+ class OpenCell < Cell
+ attr_accessor :number
+ def initialize(bombs_around = 0)
+ @number = bombs_around
+ end
+ end
+
+ class EmptyCell < Cell; end
+
+ attr_reader :cell_size, :offset
+
+ def initialize(app, level = :beginner)
+ @app = app
+ @field = []
+ @w, @h, @bombs = LEVELS[level][0], LEVELS[level][1], LEVELS[level][2]
+ @h.times { @field << Array.new(@w) { EmptyCell.new } }
+ @game_over = false
+ @width, @height, @cell_size = @w * CELL_SIZE, @h * CELL_SIZE, CELL_SIZE
+ @offset = [(@app.width - @width.to_i) / 2, (@app.height - @height.to_i) / 2]
+ plant_bombs
+ @start_time = Time.now
+ end
+
+ def total_time
+ @latest_time = Time.now - @start_time unless game_over? || all_found?
+ @latest_time
+ end
+
+ def click!(x, y)
+ return unless cell_exists?(x, y)
+ return if has_flag?(x, y)
+ return die!(x, y) if bomb?(x, y)
+ open(x, y)
+ discover(x, y) if bombs_around(x, y) == 0
+ end
+
+ def flag!(x, y)
+ return unless cell_exists?(x, y)
+ self[x, y].flag = !self[x, y].flag unless self[x, y].is_a?(OpenCell)
+ end
+
+ def game_over?
+ @game_over
+ end
+
+ def render_cell(x, y, color = "#AAA", stroke = true)
+ @app.stroke "#666" if stroke
+ @app.fill color
+ @app.rect x*cell_size, y*cell_size, cell_size-1, cell_size-1
+ @app.stroke "#BBB" if stroke
+ @app.line x*cell_size+1, y*cell_size+1, x*cell_size+cell_size-1, y*cell_size
+ @app.line x*cell_size+1, y*cell_size+1, x*cell_size, y*cell_size+cell_size-1
+ end
+
+ def render_flag(x, y)
+ @app.stroke "#000"
+ @app.line(x*cell_size+cell_size / 4 + 1, y*cell_size + cell_size / 5, x*cell_size+cell_size / 4 + 1, y*cell_size+cell_size / 5 * 4)
+ @app.fill "#A00"
+ @app.rect(x*cell_size+cell_size / 4+2, y*cell_size + cell_size / 5,
+ cell_size / 3, cell_size / 4)
+ end
+
+ def render_bomb(x, y)
+ render_cell(x, y)
+ if (game_over? or all_found?) then # draw bomb
+ if self[x, y].exploded then
+ render_cell(x, y, @app.rgb(0xFF, 0, 0, 0.5))
+ end
+ @app.nostroke
+ @app.fill @app.rgb(0, 0, 0, 0.8)
+ @app.oval(x*cell_size+3, y*cell_size+3, 13)
+ @app.fill "#333"
+ @app.oval(x*cell_size+5, y*cell_size+5, 7)
+ @app.fill "#AAA"
+ @app.oval(x*cell_size+6, y*cell_size+6, 3)
+ @app.fill @app.rgb(0, 0, 0, 0.8)
+ @app.stroke "#222"
+ @app.strokewidth 2
+ @app.oval(x*cell_size + cell_size / 2 + 2, y*cell_size + cell_size / 4 - 2, 2)
+ @app.oval(x*cell_size + cell_size / 2 + 4, y*cell_size + cell_size / 4 - 2, 1)
+ @app.strokewidth 1
+ end
+ end
+
+ def render_number(x, y)
+ render_cell(x, y, "#999", false)
+ if self[x, y].number != 0 then
+ @app.nostroke
+ @app.para self[x, y].number.to_s, :left => x*cell_size + 3, :top => y*cell_size - 2,
+ :font => '13px', :stroke => COLORS[self[x, y].number - 1]
+ end
+ end
+
+ def paint
+ 0.upto @h-1 do |y|
+ 0.upto @w-1 do |x|
+ @app.nostroke
+ case self[x, y]
+ when EmptyCell then render_cell(x, y)
+ when Bomb then render_bomb(x, y)
+ when OpenCell then render_number(x, y)
+ end
+ render_flag(x, y) if has_flag?(x, y) && !(game_over? && bomb?(x, y))
+ end
+ end
+ end
+
+ def bombs_left
+ @bombs - @field.flatten.compact.reject {|e| !e.flag }.size
+ end
+
+ def all_found?
+ @field.flatten.compact.reject {|e| !e.is_a?(OpenCell) }.size + @bombs == @w*@h
+ end
+
+ def reveal!(x, y)
+ return unless cell_exists?(x, y)
+ return unless self[x, y].is_a?(Field::OpenCell)
+ if flags_around(x, y) >= self[x, y].number then
+ (-1..1).each do |v|
+ (-1..1).each { |h| click!(x+h, y+v) unless (v==0 && h==0) or has_flag?(x+h, y+v) }
+ end
+ end
+ end
+
+ private
+
+ def cell_exists?(x, y)
+ ((0...@w).include? x) && ((0...@h).include? y)
+ end
+
+ def has_flag?(x, y)
+ return false unless cell_exists?(x, y)
+ return self[x, y].flag
+ end
+
+ def bomb?(x, y)
+ cell_exists?(x, y) && (self[x, y].is_a? Bomb)
+ end
+
+ def can_be_discovered?(x, y)
+ return false unless cell_exists?(x, y)
+ return false if self[x, y].flag
+ cell_exists?(x, y) && (self[x, y].is_a? EmptyCell) && !bomb?(x, y) && (bombs_around(x, y) == 0)
+ end
+
+ def open(x, y)
+ self[x, y] = OpenCell.new(bombs_around(x, y)) unless (self[x, y].is_a? OpenCell) or has_flag?(x, y)
+ end
+
+ def neighbors
+ (-1..1).each do |col|
+ (-1..1).each { |row| yield row, col unless col==0 && row == 0 }
+ end
+ end
+
+ def discover(x, y)
+ open(x, y)
+ neighbors do |col, row|
+ cx, cy = x+row, y+col
+ next unless cell_exists?(cx, cy)
+ discover(cx, cy) if can_be_discovered?(cx, cy)
+ open(cx, cy)
+ end
+ end
+
+ def count_neighbors
+ return 0 unless block_given?
+ count = 0
+ neighbors { |h, v| count += 1 if yield(h, v) }
+ count
+ end
+
+ def bombs_around(x, y)
+ count_neighbors { |v, h| bomb?(x+h, y+v) }
+ end
+
+ def flags_around(x, y)
+ count_neighbors { |v, h| has_flag?(x+h, y+v) }
+ end
+
+ def die!(x, y)
+ self[x, y].exploded = true
+ @game_over = true
+ end
+
+ def plant_bomb(x, y)
+ self[x, y].is_a?(EmptyCell) ? self[x, y] = Bomb.new : false
+ end
+
+ def plant_bombs
+ @bombs.times { redo unless plant_bomb(rand(@w), rand(@h)) }
+ end
+
+ def [](*args)
+ x, y = args
+ raise "Cell #{x}:#{y} does not exists!" unless cell_exists?(x, y)
+ @field[y][x]
+ end
+
+ def []=(*args)
+ x, y, v = args
+ cell_exists?(x, y) ? @field[y][x] = v : false
+ end
+end
+
+Shoes.app :width => 730, :height => 450, :title => 'Minesweeper' do
+ def render_field
+ clear do
+ background rgb(50, 50, 90, 0.7)
+ flow :margin => 4 do
+ button("Beginner") { new_game :beginner }
+ button("Intermediate") { new_game :intermediate }
+ button("Expert") { new_game :expert }
+ end
+ stack do @status = para :stroke => white end
+ @field.paint
+ para "Left click - open cell, right click - put flag, middle click - reveal empty cells", :top => 420, :left => 0, :stroke => white, :font => "11px"
+ end
+ end
+
+ def new_game level
+ @field = Field.new self, level
+ translate -@old_offset.first, -@old_offset.last unless @old_offset.nil?
+ translate @field.offset.first, @field.offset.last
+ @old_offset = @field.offset
+ render_field
+ end
+
+ new_game :beginner
+ animate(5) { @status.replace "Time: #{@field.total_time.to_i} Bombs left: #{@field.bombs_left}" }
+
+ click do |button, x, y|
+ next if @field.game_over? || @field.all_found?
+ fx, fy = ((x-@field.offset.first) / @field.cell_size).to_i, ((y-@field.offset.last) / @field.cell_size).to_i
+ @field.click!(fx, fy) if button == 1
+ @field.flag!(fx, fy) if button == 2
+ @field.reveal!(fx, fy) if button == 3
+
+ render_field
+ alert("Winner!\nTotal time: #{@field.total_time}") if @field.all_found?
+ alert("Bang!\nYou loose.") if @field.game_over?
+ end
+end
diff --git a/samples/expert-othello.rb b/samples/expert-othello.rb
new file mode 100644
index 0000000..5442d80
--- /dev/null
+++ b/samples/expert-othello.rb
@@ -0,0 +1,319 @@
+# Othello
+# by tieg
+# 1/13/08
+# with help: DeeJay, Ryan M. from mailing list
+#
+# FIXME bug if it memorizes it but it's not a valid move
+#
+module Othello
+
+ PIECE_WIDTH = 62
+ PIECE_HEIGHT = 62
+ TOP_OFFSET = 47
+ LEFT_OFFSET = 12
+
+ class Game
+ BOARD_SIZE = [8,8]
+
+ attr_accessor :p1, :p2, :board, :board_history
+
+ def initialize
+ @board_history = []
+ @p1 = Player.new(:black, pieces_per_player)
+ @p2 = Player.new(:white, pieces_per_player)
+ @board = new_board
+ lay_initial_pieces
+ end
+
+ def next_turn(check_available_moves=true)
+ @current_player = next_player
+ if check_available_moves && skip_turn?
+ # FIXME Possible infinite loop if neither player has a good move?
+ next_turn
+ raise "Player #{@current_player.piece} (#{@current_player.color}) has no available moves. Player #{next_player.piece}'s (#{next_player.color}) turn."
+ end
+ end
+
+ def current_player
+ @current_player ||= @p1
+ end
+
+ def next_player
+ current_player == @p1 ? @p2 : @p1
+ end
+
+ # Build the array for the board, with zero-based arrays.
+ def new_board
+ Array.new(BOARD_SIZE[0]) do # build each cols L to R
+ Array.new(BOARD_SIZE[1]) do # insert cells in each col
+ 0
+ end
+ end
+ end
+
+ # Lay the initial 4 pieces in the middle
+ def lay_initial_pieces
+ lay_piece([4, 3], false)
+ next_turn(false)
+ lay_piece([3, 3], false)
+ next_turn(false)
+ lay_piece([3, 4], false)
+ next_turn(false)
+ lay_piece([4, 4], false)
+ next_turn(false)
+ end
+
+ def lay_piece(c=[0,0], check_adjacent_pieces=true)
+ memorize_board
+ piece = current_player.piece
+ opp_piece = current_player.opp_piece
+ raise "Spot already taken." if board_at(c) != 0
+ if check_adjacent_pieces
+ pieces_to_change = []
+ pieces_to_change << check_direction(c, [ 0, 1], piece, opp_piece) # N
+ pieces_to_change << check_direction(c, [ 1, 1], piece, opp_piece) # NE
+ pieces_to_change << check_direction(c, [ 1, 0], piece, opp_piece) # E
+ pieces_to_change << check_direction(c, [ 1,-1], piece, opp_piece) # SE
+ pieces_to_change << check_direction(c, [ 0,-1], piece, opp_piece) # S
+ pieces_to_change << check_direction(c, [-1,-1], piece, opp_piece) # SW
+ pieces_to_change << check_direction(c, [-1, 0], piece, opp_piece) # W
+ pieces_to_change << check_direction(c, [-1, 1], piece, opp_piece) # NW
+ raise "You must move to a spot that will turn your opponent's piece." if pieces_to_change.compact.all? { |a| a.empty? }
+ pieces_to_change.compact.each { |direction| direction.each { |i| @board[i[0]][i[1]] = piece } }
+ end
+ current_player.pieces -= 1
+ @board[c[0]][c[1]] = piece
+ current_winner = calculate_current_winner
+ raise "Game over. Player #{current_winner.piece} wins with #{current_winner.pieces_on_board} pieces!" if @p1.pieces + @p2.pieces == 0
+ end
+
+ def skip_turn?
+ possibles = []
+ @board.each_with_index { |col,col_index|
+ col.each_with_index { |cell,row_index|
+ return false if possible_move?([col_index,row_index])
+ }
+ }
+ true
+ end
+
+ def possible_move?(c=[0,0])
+ return nil if board_at(c) != 0
+ possible_moves = []
+ piece = current_player.piece
+ opp_piece = current_player.opp_piece
+ pieces_to_change = []
+ pieces_to_change << check_direction(c, [ 0, 1], piece, opp_piece) # N
+ pieces_to_change << check_direction(c, [ 1, 1], piece, opp_piece) # NE
+ pieces_to_change << check_direction(c, [ 1, 0], piece, opp_piece) # E
+ pieces_to_change << check_direction(c, [ 1,-1], piece, opp_piece) # SE
+ pieces_to_change << check_direction(c, [ 0,-1], piece, opp_piece) # S
+ pieces_to_change << check_direction(c, [-1,-1], piece, opp_piece) # SW
+ pieces_to_change << check_direction(c, [-1, 0], piece, opp_piece) # W
+ pieces_to_change << check_direction(c, [-1, 1], piece, opp_piece) # NW
+ return nil if pieces_to_change.compact.all? { |a| a.empty? }
+ true
+ end
+
+ def memorize_board
+ dup_board = new_board
+ dup_board = []
+ @board.each do |col|
+ dup_board << col.dup
+ end
+ @board_history << { :player => current_player, :board => dup_board }
+ end
+
+ def undo!
+ last_move = @board_history.pop
+ @board = last_move[:board]
+ @current_player = last_move[:player]
+ @current_player.pieces += 1
+ end
+
+ def calculate_current_winner
+ @p1.pieces_on_board, @p2.pieces_on_board = 0, 0
+ @board.each { |row|
+ row.each { |cell|
+ if cell == 1
+ @p1.pieces_on_board += 1
+ else
+ @p2.pieces_on_board += 1
+ end
+ }
+ }
+ @p1.pieces_on_board > @p2.pieces_on_board ? @p1 : @p2
+ end
+
+ def check_direction(c, dir, piece, opp_piece)
+ c_adjacent = next_in_direction(c.dup, dir)
+ c_last = nil
+ pieces_in_between = []
+ # Find the last piece if there is one.
+ while(valid_location?(c_adjacent))
+ if board_at(c_adjacent) == opp_piece
+ pieces_in_between << c_adjacent.dup
+ elsif board_at(c_adjacent) == piece && pieces_in_between.size > 0
+ c_last = c_adjacent
+ break
+ else
+ break
+ end
+ c_adjacent = next_in_direction(c_adjacent, dir)
+ end
+
+ pieces_in_between.empty? || c_last.nil? ? nil : pieces_in_between
+ end
+
+ # Find the value of the board at the given coordinate.
+ def board_at(c)
+ @board[c[0]][c[1]]
+ end
+
+ # Is this a valid location on board?
+ def valid_location?(c=[1,1])
+ c[0] >= 0 && c[1] >= 0 && c[0] < BOARD_SIZE[0] && c[1] < BOARD_SIZE[1]
+ end
+
+ # Perform the operations to get the next spot in the appropriate direction
+ def next_in_direction(c, dir)
+ c[0] += dir[0]
+ c[1] += dir[1]
+ c
+ end
+
+ private
+ def pieces_per_player
+ total_squares / 2
+ end
+
+ # The total number of squares
+ def total_squares
+ BOARD_SIZE[0] * BOARD_SIZE[1]
+ end
+
+ class Player
+ attr_accessor :pieces, :color, :pieces_on_board
+
+ def initialize(color=:black,pieces=0)
+ @pieces = pieces
+ @pieces_on_board = 0 # used only in calculating winner
+ @color = color
+ end
+
+ def piece
+ color == :black ? 1 : 2
+ end
+
+ def opp_piece
+ color == :black ? 2 : 1
+ end
+ end
+ end
+
+ def draw_player_1(first_turn=false)
+ stack :margin => 10 do
+ if GAME.current_player==GAME.p1
+ background yellow
+ para span("Player 1 (#{GAME.current_player.color}) turn", :stroke => black, :font => "Trebuchet 20px bold"), :margin => 4
+ else
+ background white
+ para span("Player 1", :stroke => black, :font => "Trebuchet 10px bold"), :margin => 4
+
+ button("Undo last move", :top => 0, :left => -150) { GAME.undo!; draw_board } unless GAME.board_history.empty?
+ end
+ end
+ end
+
+ def draw_player_2(first_turn=false)
+ stack :top => 550, :left => 0, :margin => 10 do
+ if GAME.current_player==GAME.p2
+ background yellow
+ para span("Player 2's (#{GAME.current_player.color}) turn", :stroke => black, :font => "Trebuchet 20px bold"), :margin => 4
+ else
+ background white
+ para span("Player 2", :stroke => black, :font => "Trebuchet 10px bold"), :margin => 4
+
+ button("Undo last move", :top => 0, :left => -150) { GAME.undo!; draw_board } unless GAME.board_history.empty?
+ end
+ end
+ end
+
+
+ def draw_board
+ clear do
+ background black
+ draw_player_1
+ stack :margin => 10 do
+ fill rgb(0, 190, 0)
+ rect :left => 0, :top => 0, :width => 495, :height => 495
+
+ GAME.board.each_with_index do |col, col_index|
+ col.each_with_index do |cell, row_index|
+ left, top = left_top_corner_of_piece(col_index, row_index)
+ left = left - LEFT_OFFSET
+ top = top - TOP_OFFSET
+ fill rgb(0, 440, 0, 90)
+ strokewidth 1
+ stroke rgb(0, 100, 0)
+ rect :left => left, :top => top, :width => PIECE_WIDTH, :height => PIECE_HEIGHT
+
+ if cell != 0
+ strokewidth 0
+ fill (cell == 1 ? rgb(100,100,100) : rgb(155,155,155))
+ oval(left+3, top+4, PIECE_WIDTH-10, PIECE_HEIGHT-10)
+
+ fill (cell == 1 ? black : white)
+ oval(left+5, top+5, PIECE_WIDTH-10, PIECE_HEIGHT-10)
+ end
+ end
+ end
+ end
+ draw_player_2
+ end
+ end
+
+ def left_top_corner_of_piece(a,b)
+ [(a*PIECE_WIDTH+LEFT_OFFSET), (b*PIECE_HEIGHT+TOP_OFFSET)]
+ end
+
+ def right_bottom_corner_of_piece(a,b)
+ left_top_corner_of_piece(a,b).map { |coord| coord + PIECE_WIDTH }
+ end
+
+ def find_piece(x,y)
+ GAME.board.each_with_index { |row_array, row|
+ row_array.each_with_index { |col_array, col|
+ left, top = left_top_corner_of_piece(col, row).map { |i| i - 5}
+ right, bottom = right_bottom_corner_of_piece(col, row).map { |i| i -5 }
+ return [col, row] if x >= left && x <= right && y >= top && y <= bottom
+ }
+ }
+ return false
+ end
+
+ GAME = Othello::Game.new
+end
+
+
+Shoes.app :width => 520, :height => 600 do
+ extend Othello
+
+ draw_board
+
+ click { |button, x, y|
+ if coords = find_piece(x,y)
+ begin
+ GAME.lay_piece(coords)
+ GAME.next_turn
+ draw_board
+ rescue => e
+ draw_board
+ alert(e.message)
+ end
+ else
+ # alert("Not a piece.")
+ end
+ }
+end
+
diff --git a/samples/expert-pong.rb b/samples/expert-pong.rb
new file mode 100644
index 0000000..a6ca5e7
--- /dev/null
+++ b/samples/expert-pong.rb
@@ -0,0 +1,62 @@
+#
+# Pong in Shoes
+# a clone of http://billmill.org/pong.html
+# and shoes is at http://shoooes.net
+#
+# This is just for kicks -- I'm very fond of NodeBox as well.
+#
+# There's a slightly different approach in Shoes: rather than
+# redrawing the shapes, you can move the shapes around as objects.
+# Yeah, see, notice how @you, @comp and @ball are used.
+#
+Shoes.app :width => 400, :height => 400, :resizable => false do
+ paddle_size = 75
+ ball_diameter = 20
+ vx, vy = [3, 4]
+ compuspeed = 10
+ bounce = 1.2
+
+ # set up the playing board
+ nostroke and background white
+ @ball = oval 0, 0, ball_diameter, :fill => "#9B7"
+ @you, @comp = [app.height-4, 0].map {|y| rect 0, y, paddle_size, 4, :curve => 2}
+
+ # animates at 40 frames per second
+ @anim = animate 40 do
+
+ # check for game over
+ if @ball.top + ball_diameter < 0 or @ball.top > app.height
+ para strong("GAME OVER", :size => 32), "\n",
+ @ball.top < 0 ? "You win!" : "Computer wins", :top => 140, :align => 'center'
+ @ball.hide and @anim.stop
+ end
+
+ # move the @you paddle, following the mouse
+ @you.left = mouse[1] - (paddle_size / 2)
+ nx, ny = (@ball.left + vx).to_i, (@ball.top + vy).to_i
+
+ # move the @comp paddle, speed based on `compuspeed` variable
+ @comp.left +=
+ if nx + (ball_diameter / 2) > @comp.left + paddle_size; compuspeed
+ elsif nx < @comp.left; -compuspeed
+ else 0 end
+
+ # if the @you paddle hits the ball
+ if ny + ball_diameter > app.height and vy > 0 and
+ (0..paddle_size).include? nx + (ball_diameter / 2) - @you.left
+ vx, vy = (nx - @you.left - (paddle_size / 2)) * 0.25, -vy * bounce
+ ny = app.height - ball_diameter
+ end
+
+ # if the @comp paddle hits the ball
+ if ny < 0 and vy < 0 and
+ (0..paddle_size).include? nx + (ball_diameter / 2) - @comp.left
+ vx, vy = (nx - @comp.left - (paddle_size / 2)) * 0.25, -vy * bounce
+ ny = 0
+ elsif nx + ball_diameter > app.width or nx < 0
+ vx = -vx
+ end
+
+ @ball.move nx, ny
+ end
+end
diff --git a/samples/expert-tankspank.rb b/samples/expert-tankspank.rb
new file mode 100644
index 0000000..b28f6d7
--- /dev/null
+++ b/samples/expert-tankspank.rb
@@ -0,0 +1,385 @@
+# Tankspank
+# kevin conner
+# connerk@gmail.com
+# version 3, 13 March 2008
+# this code is free, do what you like with it!
+
+$width, $height = 700, 500
+$camera_tightness = 0.1
+
+module Collisions
+ def contains? x, y
+ not (x < west or x > east or y < north or y > south)
+ end
+
+ def intersects? other
+ not (other.east < west or other.west > east or
+ other.south < north or other.north > south)
+ end
+end
+
+class Building
+ include Collisions
+
+ attr_reader :west, :east, :north, :south
+
+ def initialize(west, east, north, south)
+ @west, @east, @north, @south = west, east, north, south
+ @top, @bottom = 1.1 + rand(3) * 0.15, 1.0
+
+ color = (1..3).collect { 0.2 + 0.4 * rand }
+ color << 0.9
+ @stroke = $app.rgb *color
+ color[-1] = 0.3
+ @fill = $app.rgb *color
+ end
+
+ def draw
+ $app.stroke @stroke
+ $app.fill @fill
+ Opp.draw_opp_box(@west, @east, @north, @south, @top, @bottom)
+ end
+end
+
+module Guidance
+ def guidance_system x, y, dest_x, dest_y, angle
+ vx, vy = dest_x - x, dest_y - y
+ if vx.abs < 0.1 and vy.abs <= 0.1
+ yield 0, 0
+ else
+ length = Math.sqrt(vx * vx + vy * vy)
+ vx /= length
+ vy /= length
+ ax, ay = Math.cos(angle), Math.sin(angle)
+ cos_between = vx * ax + vy * ay
+ sin_between = vx * -ay + vy * ax
+ yield sin_between, cos_between
+ end
+ end
+end
+
+module Life
+ attr_reader :health
+ def dead?
+ @health == 0
+ end
+ def hurt damage
+ @health = [@health - damage, 0].max
+ end
+end
+
+class Tank
+ include Collisions
+ include Guidance
+ include Life
+ # ^ sounds like insurance
+
+ @@collide_size = 15
+ def west; @x - @@collide_size; end
+ def east; @x + @@collide_size; end
+ def north; @y - @@collide_size; end
+ def south; @y + @@collide_size; end
+
+ attr_reader :x, :y
+
+ def initialize
+ @x, @y = 0, -125
+ @last_x, @last_y = @x, @y
+ @tank_angle = 0.0
+ @dest_x, @dest_y = 0, 0
+ @acceleration = 0.0
+ @speed = 0.0
+ @moving = false
+
+ @aim_angle = 0.0
+ @target_x, @target_y = 0, 0
+ @aimed = false
+
+ @health = 100
+ end
+
+ def set_destination
+ @dest_x, @dest_y = @target_x, @target_y
+ @moving = true
+ end
+
+ def fire
+ Opp.add_shell Shell.new(@x + 30 * Math.cos(@aim_angle),
+ @y + 30 * Math.sin(@aim_angle), @aim_angle)
+ end
+
+ def update button, mouse_x, mouse_y
+ @target_x, @target_y = mouse_x, mouse_y
+
+ if @moving
+ guidance_system @x, @y, @dest_x, @dest_y, @tank_angle do |direction, on_target|
+ turn direction
+ @acceleration = on_target * 0.25
+ end
+
+ distance = Math.sqrt((@dest_x - @x) ** 2 + (@dest_y - @y) ** 2)
+ @moving = false if distance < 50
+ else
+ @acceleration = 0.0
+ end
+
+ guidance_system @x, @y, @target_x, @target_y, @aim_angle do |direction, on_target|
+ aim direction
+ @aimed = on_target > 0.98
+ end
+
+ integrity = @health / 100.0 # the more hurt you are, the slower you go
+ @speed = [[@speed + @acceleration, 5.0 * integrity].min, -3.0 * integrity].max
+ @speed *= 0.9 if !@moving
+
+ @last_x, @last_y = @x, @y
+ @x += @speed * Math.cos(@tank_angle)
+ @y += @speed * Math.sin(@tank_angle)
+ end
+
+ def collide_and_stop
+ @x, @y = @last_x, @last_y
+ hurt @speed.abs * 3 + 5
+ @speed = 0
+ @moving = false
+ end
+
+ def turn direction
+ @tank_angle += [[-0.03, direction].max, 0.03].min
+ end
+
+ def aim direction
+ @aim_angle += [[-0.1, direction].max, 0.1].min
+ end
+
+ def draw
+ $app.stroke $app.blue
+ $app.fill $app.blue(0.4)
+ Opp.draw_opp_rect @x - 20, @x + 20, @y - 15, @y + 15, 1.05, @tank_angle
+ #Opp.draw_opp_box @x - 20, @x + 20, @y - 20, @y + 20, 1.03, 1.0
+ Opp.draw_opp_rect @x - 10, @x + 10, @y - 7, @y + 7, 1.05, @aim_angle
+ x, unused1, y, unused2 = Opp.project(@x, 0, @y, 0, 1.05)
+ $app.line x, y, x + 25 * Math.cos(@aim_angle), y + 25 * Math.sin(@aim_angle)
+
+ $app.stroke $app.red
+ $app.fill $app.red(@aimed ? 0.4 : 0.1)
+ Opp.draw_opp_oval @target_x - 10, @target_x + 10, @target_y - 10, @target_y + 10, 1.00
+
+ if @moving
+ $app.stroke $app.green
+ $app.fill $app.green(0.2)
+ Opp.draw_opp_oval @dest_x - 20, @dest_x + 20, @dest_y - 20, @dest_y + 20, 1.00
+ end
+ end
+end
+
+class Shell
+ attr_reader :x, :y
+
+ def initialize x, y, angle
+ @x, @y, @angle = x, y, angle
+ @speed = 10.0
+ end
+
+ def update
+ @x += @speed * Math.cos(@angle)
+ @y += @speed * Math.sin(@angle)
+ end
+
+ def draw
+ $app.stroke $app.red
+ $app.fill $app.red(0.1)
+ Opp.draw_opp_box @x - 2, @x + 2, @y - 2, @y + 2, 1.05, 1.04
+ end
+end
+
+class Opp
+ def self.new_game
+ @offset_x, @offset_y = 0, 0
+ @buildings = [
+ [-1000, -750, -750, -250],
+ [-500, 250, -750, -250],
+ [500, 1000, -750, -500],
+ [750, 1250, -250, 0],
+ [750, 1250, 250, 750],
+ [250, 500, 0, 750],
+ [-250, 0, 0, 500],
+ [-500, 0, 750, 1000],
+ [-1000, -500, 0, 500],
+ [400, 600, -350, -150]
+ ].collect { |p| Building.new *p }
+ @shells = []
+ @boundary = [-1250, 1500, -1250, 1250]
+ @tank = Tank.new
+ @center_x, @center_y = $app.width / 2, $app.height / 2
+ end
+
+ def self.tank
+ @tank
+ end
+
+ def self.read_input
+ @input = $app.mouse
+ end
+
+ def self.update_scene
+ button, x, y = @input
+ x += @offset_x - @center_x
+ y += @offset_y - @center_y
+
+ @tank.update(button, x, y) if !@tank.dead?
+ @buildings.each do |b|
+ @tank.collide_and_stop if b.intersects? @tank
+ end
+
+ @shells.each { |s| s.update }
+ @buildings.each do |b|
+ @shells.reject! do |s|
+ b.contains?(s.x, s.y)
+ end
+ end
+ #collide shells with tanks -- don't need this until there are enemy tanks
+ #@shells.reject! do |s|
+ # @tank.contains?(s.x, s.y)
+ #end
+
+ $app.clear do
+ @offset_x += $camera_tightness * (@tank.x - @offset_x)
+ @offset_y += $camera_tightness * (@tank.y - @offset_y)
+
+ $app.background $app.black
+ @center_x, @center_y = $app.width / 2, $app.height / 2
+
+ $app.stroke $app.red(0.9)
+ $app.nofill
+ draw_opp_box *(@boundary + [1.1, 1.0, false])
+
+ @tank.draw
+ @shells.each { |s| s.draw }
+ @buildings.each { |b| b.draw }
+ end
+ end
+
+ def self.add_shell shell
+ @shells << shell
+ @shells.shift if @shells.size > 10
+ end
+
+ def self.project left, right, top, bottom, depth
+ [left, right].collect { |x| @center_x + depth * (x - @offset_x) } +
+ [top, bottom].collect { |y| @center_y + depth * (y - @offset_y) }
+ end
+
+ # here "front" and "back" push the rect into and out of the window.
+ # 1.0 means your x and y units are pixels on the surface.
+ # greater than that brings the box closer. less pushes it back. 0.0 => infinity.
+ # the front will be filled but the rest is wireframe only.
+ def self.draw_opp_box left, right, top, bottom, front, back, occlude = true
+ near_left, near_right, near_top, near_bottom = project(left, right, top, bottom, front)
+ far_left, far_right, far_top, far_bottom = project(left, right, top, bottom, back)
+
+ # determine which sides of the box are visible
+ if occlude
+ draw_left = @center_x < near_left
+ draw_right = near_right < @center_x
+ draw_top = @center_y < near_top
+ draw_bottom = near_bottom < @center_y
+ else
+ draw_left, draw_right, draw_top, draw_bottom = [true] * 4
+ end
+
+ # draw lines for the back edges
+ $app.line far_left, far_top, far_right, far_top if draw_top
+ $app.line far_left, far_bottom, far_right, far_bottom if draw_bottom
+ $app.line far_left, far_top, far_left, far_bottom if draw_left
+ $app.line far_right, far_top, far_right, far_bottom if draw_right
+
+ # draw lines to connect the front and back
+ $app.line near_left, near_top, far_left, far_top if draw_left or draw_top
+ $app.line near_right, near_top, far_right, far_top if draw_right or draw_top
+ $app.line near_left, near_bottom, far_left, far_bottom if draw_left or draw_bottom
+ $app.line near_right, near_bottom, far_right, far_bottom if draw_right or draw_bottom
+
+ # draw the front, filled
+ $app.rect near_left, near_top, near_right - near_left, near_bottom - near_top
+ end
+
+ def self.draw_opp_rect left, right, top, bottom, depth, angle, with_x = false
+ pl, pr, pt, pb = project(left, right, top, bottom, depth)
+ cos = Math.cos(angle)
+ sin = Math.sin(angle)
+ cx, cy = (pr + pl) / 2.0, (pb + pt) / 2.0
+ points = [[pl, pt], [pr, pt], [pr, pb], [pl, pb]].collect do |x, y|
+ [cx + (x - cx) * cos - (y - cy) * sin,
+ cy + (x - cx) * sin + (y - cy) * cos]
+ end
+
+ $app.line *(points[0] + points[1])
+ $app.line *(points[1] + points[2])
+ $app.line *(points[2] + points[3])
+ $app.line *(points[3] + points[0])
+ end
+
+ def self.draw_opp_oval left, right, top, bottom, depth
+ pl, pr, pt, pb = project(left, right, top, bottom, depth)
+ $app.oval(pl, pt, pr - pl, pb - pt)
+ end
+
+ def self.draw_opp_plane x1, y1, x2, y2, front, back, stroke_color
+ near_x1, near_x2, near_y1, near_y2 = project(x1, x2, y1, y2, front)
+ far_x1, far_x2, far_y1, far_y2 = project(x1, x2, y1, y2, back)
+
+ $app.stroke stroke_color
+
+ $app.line far_x1, far_y1, far_x2, far_y2
+ $app.line far_x1, far_y1, near_x1, near_y1
+ $app.line far_x2, far_y2, near_x2, near_y2
+ $app.line near_x1, near_y1, near_x2, near_y2
+ end
+end
+
+Shoes.app :width => $width, :height => $height do
+ $app = self
+
+ Opp.new_game
+ @playing = true
+
+ keypress do |key|
+ if @playing
+ if key == "1" or key == "z"
+ Opp.tank.set_destination
+ elsif key == "2" or key == "x" or key == " "
+ Opp.tank.fire
+ end
+ else
+ if key == "n"
+ Opp.new_game
+ @playing = true
+ end
+ end
+ end
+
+ click do |button, x, y|
+ if @playing
+ if button == 1
+ Opp.tank.set_destination
+ else
+ Opp.tank.fire
+ end
+ end
+ end
+
+ game_over_count = -1
+ animate(60) do
+ Opp.read_input if @playing
+ Opp.update_scene
+
+ @playing = false if Opp.tank.dead?
+ if !@playing
+ stack do
+ banner "Game Over", :stroke => white, :margin => 10
+ caption "learn to drive!", :stroke => white, :margin => 20
+ end
+ end
+ end
+end
diff --git a/samples/good-arc.rb b/samples/good-arc.rb
new file mode 100644
index 0000000..126a6ad
--- /dev/null
+++ b/samples/good-arc.rb
@@ -0,0 +1,37 @@
+#
+# a translation from a processing example
+# http://vormplus.be/weging/an-introduction-to-processing/
+#
+Shoes.app :width => 420, :height => 420, :resizable => false do
+ stage, wide, sw, basesize, step = 0, 3.0, 1.0, 600, 60
+ stroke gray(127)
+ nofill
+
+ animate 40 do |i|
+ stage = (1...8).rand if i % 40 == 0
+ rotation = -(HALF_PI / wide)
+ clear do
+ background gray(240)
+ 10.times do |i|
+ strokewidth sw * i
+ size = (basesize / 3) + ((step / 3) * i)
+ shape do
+ arc self.width / 2, self.height / 2,
+ size, size,
+ rotation * i, rotation * i + TWO_PI - HALF_PI
+ end
+ end
+ end
+
+ case stage
+ when 1; wide -= 0.1
+ when 2; wide += 0.1
+ when 3; basesize -= 1
+ when 4; basesize += 2
+ when 5; sw += 0.1
+ when 6; sw -= 0.01
+ when 7; step += 2
+ else step -= 1
+ end
+ end
+end
diff --git a/samples/good-clock.rb b/samples/good-clock.rb
new file mode 100644
index 0000000..55de51c
--- /dev/null
+++ b/samples/good-clock.rb
@@ -0,0 +1,51 @@
+#
+# Shoes Clock by Thomas Bell
+# posted to the Shoes mailing list on 04 Dec 2007
+#
+Shoes.app :height => 260, :width => 250 do
+ @radius, @centerx, @centery = 90, 126, 140
+ animate(8) do
+ @time = Time.now
+ clear do
+ draw_background
+ stack do
+ background black
+ para @time.strftime("%a"),
+ span(@time.strftime(" %b %d, %Y "), :stroke => "#ccc"),
+ strong(@time.strftime("%I:%M"), :stroke => white),
+ @time.strftime(".%S"), :align => "center", :stroke => "#666",
+ :margin => 4
+ end
+ clock_hand @time.sec + (@time.usec * 0.000001),2,30,red
+ clock_hand @time.min + (@time.sec / 60.0),5
+ clock_hand @time.hour + (@time.min / 60.0),8,6
+ end
+ end
+ def draw_background
+ background rgb(230, 240, 200)
+
+ fill white
+ stroke black
+ strokewidth 4
+ oval @centerx - 102, @centery - 102, 204, 204
+
+ fill black
+ nostroke
+ oval @centerx - 5, @centery - 5, 10, 10
+
+ stroke black
+ strokewidth 1
+ line(@centerx, @centery - 102, @centerx, @centery - 95)
+ line(@centerx - 102, @centery, @centerx - 95, @centery)
+ line(@centerx + 95, @centery, @centerx + 102, @centery)
+ line(@centerx, @centery + 95, @centerx, @centery + 102)
+ end
+ def clock_hand(time, sw, unit=30, color=black)
+ radius_local = unit == 30 ? @radius : @radius - 15
+ _x = radius_local * Math.sin( time * Math::PI / unit )
+ _y = radius_local * Math.cos( time * Math::PI / unit )
+ stroke color
+ strokewidth sw
+ line(@centerx, @centery, @centerx + _x, @centery - _y)
+ end
+end
diff --git a/samples/good-follow.rb b/samples/good-follow.rb
new file mode 100644
index 0000000..3b98206
--- /dev/null
+++ b/samples/good-follow.rb
@@ -0,0 +1,26 @@
+trails = [[0, 0]] * 60
+Shoes.app :width => 200, :height => 200, :resizable => false do
+ nostroke
+ fill rgb(0x3, 0x1, 0x3, 0.6)
+
+ # animation at 100 frames per second
+ animate(60) do
+ trails.shift
+ trails << self.mouse[1, 2]
+
+ clear do
+ # change the background based on where the pointer is
+ background rgb(
+ 20 + (70 * (trails.last[0].to_f / self.width)).to_i,
+ 20 + (70 * (trails.last[1].to_f / self.height)).to_i,
+ 51)
+
+ # draw circles progressively bigger
+ trails.each_with_index do |(x, y), i|
+ i += 1
+ oval :left => x, :top => y, :radius => (i*0.5), :center => true
+ end
+ end
+ end
+
+end
diff --git a/samples/good-reminder.rb b/samples/good-reminder.rb
new file mode 100644
index 0000000..b20e561
--- /dev/null
+++ b/samples/good-reminder.rb
@@ -0,0 +1,174 @@
+require 'yaml'
+
+Shoes.app :title => "A Gentle Reminder",
+ :width => 370, :height => 560, :resizable => false do
+
+ background white
+ background tan, :height => 40
+
+ caption "A Gentle Reminder", :margin => 8, :stroke => white
+
+ stack :margin => 10, :margin_top => 50 do
+ para "You need to", :stroke => red, :fill => yellow
+
+ stack :margin_left => 5, :margin_right => 10, :width => 1.0, :height => 200, :scroll => true do
+ background white
+ border white, :strokewidth => 3
+ @gui_todo = para
+ end
+
+ flow :margin_top => 10 do
+ para "Remember to"
+ @add = edit_line(:margin_left => 10, :width => 180)
+ button("Add", :margin_left => 5) { add_todo(@add.text); @add.text = '' }
+ end
+ end
+
+ stack :margin_top => 10 do
+ background darkgray
+ para strong('Completed'), :stroke => white
+ end
+
+ @gui_completed = stack :width => 1.0, :height => 207, :margin_right => 20
+
+
+ def data_path
+ if RUBY_PLATFORM =~ /win32/
+ if ENV['USERPROFILE']
+ if File.exist?(File.join(File.expand_path(ENV['USERPROFILE']), "Application Data"))
+ user_data_directory = File.join File.expand_path(ENV['USERPROFILE']), "Application Data", "GentleReminder"
+ else
+ user_data_directory = File.join File.expand_path(ENV['USERPROFILE']), "GentleReminder"
+ end
+ else
+ user_data_directory = File.join File.expand_path(Dir.getwd), "data"
+ end
+ else
+ user_data_directory = File.expand_path(File.join("~", ".gentlereminder"))
+ end
+
+ unless File.exist?(user_data_directory)
+ Dir.mkdir(user_data_directory)
+ end
+
+ return File.join(user_data_directory, "data.yaml")
+ end
+
+
+ def refresh_todo
+ @gui_todo.replace *(
+ @todo.map { |item|
+ [ item, ' ' ] + [ link('Done') { complete_todo item } ] + [ ' ' ] +
+ [ link('Forget it') { forget_todo item } ] + [ "\n" ]
+ }.flatten
+ )
+ end
+
+
+ def refresh
+ refresh_todo
+
+ @gui_completed.clear
+
+ @gui_completed.append do
+ background white
+
+ @completed.keys.sort.reverse.each { |day|
+ stack do
+ background lightgrey
+ para strong(Time.at(day).strftime('%B %d, %Y')), :stroke => white
+ end
+
+ stack do
+ inscription *(
+ @completed[day].map { |item|
+ [ item ] + [ ' ' ] + [ link('Not Done') { undo_todo day, item } ] +
+ (@completed[day].index(item) == @completed[day].length - 1 ? [ '' ] : [ "\n" ])
+ }.flatten
+ )
+ end
+
+ }
+ end
+ end
+
+
+ def complete_todo(item)
+ day = Time.today.to_i
+
+ if @completed.keys.include? day
+ @completed[day] << item
+ else
+ @completed[day] = [ item ]
+ end
+
+ @todo.delete(item)
+
+ save
+
+ refresh
+ end
+
+
+ def undo_todo(day, item)
+ @completed[day].delete item
+
+ @completed.delete(day) if @completed[day].empty?
+
+ @todo << item unless @todo.include? item
+
+ save
+
+ refresh
+ end
+
+
+ def add_todo(item)
+ item = item.strip
+
+ return if item == ''
+
+ if @todo.include? item
+ alert('You have already added that to the list!')
+ return
+ end
+
+ @todo << item
+
+ save
+
+ refresh_todo
+ end
+
+
+ def forget_todo(item)
+ @todo.delete item
+
+ save
+
+ refresh_todo
+ end
+
+
+ def load
+ if File.exist?(data_path)
+ @todo, @completed = YAML::load(File.open(data_path, 'r'))
+ else
+ @todo = []
+ @completed = {}
+ end
+
+ refresh
+ end
+
+
+ def save
+ File.open(data_path, 'w') { |f|
+ f.write [ @todo, @completed ].to_yaml
+ }
+ end
+
+
+ load
+
+end
diff --git a/samples/good-vjot.rb b/samples/good-vjot.rb
new file mode 100644
index 0000000..dda42a3
--- /dev/null
+++ b/samples/good-vjot.rb
@@ -0,0 +1,56 @@
+NOTES = [['Welcome to the vJot Clone', <<-'END']]
+This sample app is a notetaker, a clone of PJ Hyett's vjot.com.
+
+Creating
+----------
+Click "Add a New Note" and the jot will be loaded into the editor for reading or editing.
+
+Editing
+---------
+Click a jot's title to load it.
+
+Saving
+--------
+There is no save button, the jot is saved as you edit.
+
+END
+
+Shoes.app :title => "vJot",
+ :width => 420, :height => 560, :resizable => false do
+
+ @note = NOTES.first
+ background "#C7EAFB"
+ stack :width => 400, :margin => 20 do
+ background "#eee", :curve => 12
+ border "#00D0FF", :strokewidth => 3, :curve => 12
+ stack :margin => 20 do
+ caption "vJot"
+ @title = edit_line @note[0], :width => 1.0 do
+ @note[0] = @title.text
+ load_list
+ end
+ stack :width => 1.0, :height => 200, :scroll => true do
+ @list = para
+ end
+ @jot = edit_box @note[1], :width => 1.0, :height => 200, :margin_bottom => 20 do
+ @note[1] = @jot.text
+ end
+ end
+ end
+
+ def load_list
+ @list.replace *(NOTES.map { |note|
+ [link(note.first) { @note = load_note(note); load_list }, "\n"]
+ }.flatten +
+ [link("+ Add a new Note") { NOTES << (@note = load_note); load_list }])
+ end
+
+ def load_note(note = ['New Note', ''])
+ @note = note
+ @title.text = note[0]
+ @jot.text = note[1]
+ note
+ end
+
+ load_list
+end
diff --git a/samples/simple-accordion.rb b/samples/simple-accordion.rb
new file mode 100644
index 0000000..f753766
--- /dev/null
+++ b/samples/simple-accordion.rb
@@ -0,0 +1,75 @@
+module Accordion
+ def open_page stack
+ active = app.slot.contents.map { |x| x.contents[1] }.
+ detect { |x| x.height > 0 }
+ return if active == stack
+ a = animate 60 do
+ stack.height += 20
+ active.height = 240 - stack.height if active
+ a.stop if stack.height == 240
+ end
+ end
+ def page title, text
+ @pages ||= []
+ @pages <<
+ stack do
+ page_text = nil
+ stack :width => "100%" do
+ background "#fff".."#eed"
+ hi = background "#ddd".."#ba9", :hidden => true
+ para link(title) {}, :size => 26
+ hover { hi.show }
+ leave { hi.hide }
+ click { open_page page_text }
+ end
+ page_text =
+ stack :width => "100%", :height => (@pages.empty? ? 240 : 0) do
+ stack :margin => 10 do
+ text.split(/\n{2,}/).each do |pg|
+ para pg
+ end
+ end
+ end
+ end
+ end
+end
+
+Shoes.app do
+ extend Accordion
+ style(Link, :stroke => black, :underline => nil, :weight => "strong")
+ style(LinkHover, :stroke => black, :fill => nil, :underline => nil)
+
+ page "0.0", <<-'END'
+There is a thought
+I have just had
+Which I don’t care to pass to
+Anyone at all at this time.
+
+I have even forgotten it now,
+But kept only the pleasures
+Of my property
+And of my controlled mental slippage.
+END
+ page "0.1", <<-'END'
+My eyes have blinked again
+And I have just realized
+This upright world
+I have been in.
+
+My eyelids wipe
+My eyes hundreds of times
+Reseting and renovating
+The scenery.
+END
+ page "0.2", <<-'END'
+Sister, without you,
+The universe would
+Have such a hole through it,
+Where infinity has been shot.
+
+This cannot be, though.
+There will always be room
+For you—all of us are
+Holding the way open.
+END
+end
diff --git a/samples/simple-anim-shapes.rb b/samples/simple-anim-shapes.rb
new file mode 100644
index 0000000..be39276
--- /dev/null
+++ b/samples/simple-anim-shapes.rb
@@ -0,0 +1,17 @@
+Shoes.app do
+ background rgb(0, 0, 0)
+ fill rgb(255, 255, 255)
+ rects = [
+ rect(0, 0, 50, 50),
+ rect(0, 0, 100, 100),
+ rect(0, 0, 75, 75)
+ ]
+ animate(24) do |i|
+ rects.each do |r|
+ r.move((0..400).rand, (0..400).rand)
+ end
+ end
+ button "OK", :top => 0.5, :left => 0.5 do
+ quit unless confirm "You ARE sure you're OK??"
+ end
+end
diff --git a/samples/simple-anim-text.rb b/samples/simple-anim-text.rb
new file mode 100644
index 0000000..553dd62
--- /dev/null
+++ b/samples/simple-anim-text.rb
@@ -0,0 +1,13 @@
+Shoes.app do
+ stack :top => 0.5, :left => 0.5 do
+ para "Counting up:"
+ l = para "0"
+ animate(24) do |i|
+ f = ['Arial 14px', 'Serif 34px', 'Monospace 18px', 'Arial 48px'][rand(3)]
+ l.replace "#{i}", :font => f
+ end
+ motion do |x, y|
+ Shoes.p [x, y]
+ end
+ end
+end
diff --git a/samples/simple-arc.rb b/samples/simple-arc.rb
new file mode 100644
index 0000000..6eaf28d
--- /dev/null
+++ b/samples/simple-arc.rb
@@ -0,0 +1,23 @@
+#
+# a translation from a processing example
+# http://vormplus.be/weging/an-introduction-to-processing/
+#
+Shoes.app :width => 420, :height => 420, :resizable => false do
+ rotation = -(Shoes::HALF_PI / 3)
+ step = 20
+
+ background gray(240)
+ stroke gray(127)
+ cap :curve
+ nofill
+
+ 10.times do |i|
+ strokewidth i
+ size = 200 + (step * i)
+ shape do
+ arc self.width / 2, self.height / 2,
+ size, size,
+ rotation * i, rotation * i + Shoes::TWO_PI - Shoes::HALF_PI
+ end
+ end
+end
diff --git a/samples/simple-bounce.rb b/samples/simple-bounce.rb
new file mode 100644
index 0000000..88c8ac0
--- /dev/null
+++ b/samples/simple-bounce.rb
@@ -0,0 +1,24 @@
+xspeed, yspeed = 8.4, 6.6
+xdir, ydir = 1, 1
+
+Shoes.app do
+ background "#DFA"
+ border black, :strokewidth => 6
+
+ nostroke
+ @icon = image "#{DIR}/static/shoes-icon.png", :left => 100, :top => 100 do
+ alert "You're soooo quick."
+ end
+
+ x, y = self.width / 2, self.height / 2
+ size = @icon.size
+ animate(30) do
+ x += xspeed * xdir
+ y += yspeed * ydir
+
+ xdir *= -1 if x > self.width - size[0] or x < 0
+ ydir *= -1 if y > self.height - size[1] or y < 0
+
+ @icon.move x.to_i, y.to_i
+ end
+end
diff --git a/samples/simple-calc.rb b/samples/simple-calc.rb
new file mode 100644
index 0000000..f49587b
--- /dev/null
+++ b/samples/simple-calc.rb
@@ -0,0 +1,70 @@
+class Calc
+ def initialize
+ @number = 0
+ @previous = nil
+ @op = nil
+ end
+
+ def to_s
+ @number.to_s
+ end
+
+ (0..9).each do |n|
+ define_method "press_#{n}" do
+ @number = @number.to_i * 10 + n
+ end
+ end
+
+ def press_clear
+ @number = 0
+ end
+
+ {'add' => '+', 'sub' => '-', 'times' => '*', 'div' => '/'}.each do |meth, op|
+ define_method "press_#{meth}" do
+ if @op
+ press_equals
+ end
+ @op = op
+ @previous, @number = @number, nil
+ end
+ end
+
+ def press_equals
+ @number = @previous.send(@op, @number.to_i)
+ @op = nil
+ end
+
+end
+
+number_field = nil
+number = Calc.new
+Shoes.app :height => 250, :width => 200, :resizable => false do
+ background "#EEC".."#996", :curve => 5, :margin => 2
+
+ stack :margin => 2 do
+
+ stack :margin => 8 do
+ number_field = para strong(number)
+ end
+
+ flow :width => 218, :margin => 4 do
+ %w(7 8 9 / 4 5 6 * 1 2 3 - 0 Clr = +).each do |btn|
+ button btn, :width => 46, :height => 46 do
+ method = case btn
+ when /[0-9]/; 'press_'+btn
+ when 'Clr'; 'press_clear'
+ when '='; 'press_equals'
+ when '+'; 'press_add'
+ when '-'; 'press_sub'
+ when '*'; 'press_times'
+ when '/'; 'press_div'
+ end
+
+ number.send(method)
+ number_field.replace strong(number)
+ end
+ end
+ end
+ end
+
+end
diff --git a/samples/simple-chipmunk.rb b/samples/simple-chipmunk.rb
new file mode 100644
index 0000000..2c51e93
--- /dev/null
+++ b/samples/simple-chipmunk.rb
@@ -0,0 +1,26 @@
+# simple-chipmunk.rb
+require 'shoes/chipmunk'
+
+Shoes.app title: 'A Tiny Chipmunk Physics Demo' do
+ extend ChipMunk
+ space = cp_space
+ balls = []
+
+ nofill
+ cp_line 0, 180, 200, 280, stroke: gold
+ cp_line 200, 280, 300, 270, stroke: gold
+ cp_line 250, 350, 350, 330
+ cp_line 170, 370, 220, 380
+ cp_line 100, 450, 300, 430, stroke: lightslategray
+ cp_line 300, 430, 500, 450, stroke: lightslategray
+
+ nostroke
+ oval(10, 30, 40, fill: blue).click{balls << cp_oval(30, 50, 20, stroke: blue, strokewidth: 2)}
+ oval(70, 40, 20, fill: green).click{balls << cp_oval(80, 50, 10, fill: green)}
+ oval(105, 45, 10, fill: red).click{balls << cp_oval(110, 50, 5, fill: red)}
+
+ every do
+ 6.times{space.step 1.0/60}
+ balls.each{|ball| ball.cp_move}
+ end
+end
diff --git a/samples/simple-control-sizes.rb b/samples/simple-control-sizes.rb
new file mode 100644
index 0000000..433bcdb
--- /dev/null
+++ b/samples/simple-control-sizes.rb
@@ -0,0 +1,24 @@
+Shoes.app :width => 360, :height => 600, :resizable => false do
+ stroke "#dde"
+ background "#f1f5ff"
+ 13.times { |x| line 20, 142 + (30 * x), 320, 142 + (30 * x) }
+ 11.times { |x| line 20 + (30 * x), 142, 20 + (30 * x), 502 }
+
+ stack :margin => 20 do
+ title "Control Sizes", :size => 16
+ para "This app measures various controls against a grid of lines, to be sure they size appropriately despite the platform."
+ stack :top => 122, :left => 40 do
+ button "Standard"
+ button "Margin: 2, Height: 28", :margin => 2, :height => 30
+ edit_line "Standard", :margin => 1
+ edit_line "Margin: 4, Height: 30", :height => 30, :margin => 4
+ list_box :items => ["Standard"], :choose => "Standard"
+ list_box :items => ["Margin: 4, Height: 32"],
+ :choose => "Margin: 4, Height: 32",
+ :height => 32, :margin => 4
+ progress
+ progress :height => 32, :margin => 4
+ edit_box
+ end
+ end
+end
diff --git a/samples/simple-curve.rb b/samples/simple-curve.rb
new file mode 100644
index 0000000..c8a9910
--- /dev/null
+++ b/samples/simple-curve.rb
@@ -0,0 +1,26 @@
+#
+# based on the cairo curve_to example
+# http://www.cairographics.org/samples/curve_to/
+#
+Shoes.app do
+ x, y = 25.6, 128.0
+ x1 = 102.4; y1 = 230.4
+ x2 = 153.6; y2 = 25.6
+ x3 = 230.4; y3 = 128.0
+
+ nofill
+ strokewidth 10.0
+ shape do
+ move_to x, y
+ curve_to x1, y1, x2, y2, x3, y3
+ end
+
+ strokewidth 6.0
+ stroke rgb(1.0, 0.2, 0.2, 0.6)
+ shape do
+ move_to x, y
+ line_to x1, y1
+ move_to x2, y2
+ line_to x3, y3
+ end
+end
diff --git a/samples/simple-dialogs.rb b/samples/simple-dialogs.rb
new file mode 100644
index 0000000..a1b1a08
--- /dev/null
+++ b/samples/simple-dialogs.rb
@@ -0,0 +1,29 @@
+Shoes.app :width => 300, :height => 150, :margin => 10 do
+ def answer(v)
+ @answer.replace v.inspect
+ end
+
+ button "Ask" do
+ answer ask("What is your name?")
+ end
+ button "Confirm" do
+ answer confirm("Would you like to proceed?")
+ end
+ button "Open File..." do
+ answer ask_open_file
+ end
+ button "Save File..." do
+ answer ask_save_file
+ end
+ button "Open Folder..." do
+ answer ask_open_folder
+ end
+ button "Save Folder..." do
+ answer ask_save_folder
+ end
+ button "Color" do
+ answer ask_color("Pick a Color")
+ end
+
+ @answer = para "Answers appear here"
+end
diff --git a/samples/simple-downloader.rb b/samples/simple-downloader.rb
new file mode 100644
index 0000000..754eda3
--- /dev/null
+++ b/samples/simple-downloader.rb
@@ -0,0 +1,27 @@
+Shoes.app do
+ background "#eee"
+ @list = stack do
+ para "Enter a URL to download:", :margin => [10, 8, 10, 0]
+ flow :margin => 10 do
+ @url = edit_line :width => -120
+ button "Download", :width => 120 do
+ @list.append do
+ stack do
+ background "#eee".."#ccd"
+ stack :margin => 10 do
+ dl = nil
+ para @url.text, " [", link("cancel") { dl.abort }, "]", :margin => 0
+ d = inscription "Beginning transfer.", :margin => 0
+ p = progress :width => 1.0, :height => 14
+ dl = download @url.text, :save => File.basename(@url.text),
+ :progress => proc { |dl|
+ d.text = "Transferred #{dl.transferred} of #{dl.length} bytes (#{dl.percent}%)"
+ p.fraction = dl.percent * 0.01 },
+ :finish => proc { |dl| d.text = "Download completed" }
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/samples/simple-draw.rb b/samples/simple-draw.rb
new file mode 100644
index 0000000..4dc115c
--- /dev/null
+++ b/samples/simple-draw.rb
@@ -0,0 +1,13 @@
+Shoes.app do
+ background "#999"
+ stroke "#000"
+ x, y = nil, nil
+ motion do |_x, _y|
+ if x and y and (x != _x or y != _y)
+ append do
+ line x, y, _x, _y
+ end
+ end
+ x, y = _x, _y
+ end
+end
diff --git a/samples/simple-editor.rb b/samples/simple-editor.rb
new file mode 100644
index 0000000..b6c1190
--- /dev/null
+++ b/samples/simple-editor.rb
@@ -0,0 +1,28 @@
+str, t = "", nil
+Shoes.app :height => 500, :width => 450 do
+ background rgb(77, 77, 77)
+ stack :margin => 10 do
+ para span("TEXT EDITOR", :stroke => red, :fill => white), " * USE ALT-Q TO QUIT", :stroke => white
+ end
+ stack :margin => 10 do
+ t = para "", :font => "Monospace 12px", :stroke => white
+ t.cursor = -1
+ end
+ keypress do |k|
+ case k
+ when String
+ str += k
+ when :backspace
+ str.slice!(-1)
+ when :tab
+ str += " "
+ when :alt_q
+ quit
+ when :alt_c
+ self.clipboard = str
+ when :alt_v
+ str += self.clipboard
+ end
+ t.replace str
+ end
+end
diff --git a/samples/simple-form.rb b/samples/simple-form.rb
new file mode 100644
index 0000000..4c0ad14
--- /dev/null
+++ b/samples/simple-form.rb
@@ -0,0 +1,28 @@
+Shoes.app :width => 320, :height => 420 do
+ background "../static/menu-gray.png"
+ background "../static/menu-top.png", :height => 50
+ background "../static/menu-left.png", :top => 50, :width => 55
+ background "../static/menu-right.png", :right => 0, :top => 50, :width => 55
+ image "../static/menu-corner1.png", :top => 0, :left => 0
+ image "../static/menu-corner2.png", :right => 0, :top => 0
+
+ stack :margin => 40 do
+ stack :margin => 10 do
+ para "Name"
+ @name = list_box :items => ["Phyllis", "Ronald", "Wyatt"]
+ end
+ stack :margin => 10 do
+ para "Address"
+ @address = edit_line
+ end
+ stack :margin => 10 do
+ para "Phone"
+ @phone = edit_line
+ end
+ stack :margin => 10 do
+ button "Save" do
+ Shoes.p [@name.text, @address.text, @phone.text]
+ end
+ end
+ end
+end
diff --git a/samples/simple-form.shy b/samples/simple-form.shy
new file mode 100644
index 0000000..ddf53eb
--- /dev/null
+++ b/samples/simple-form.shy
Binary files differ
diff --git a/samples/simple-mask.rb b/samples/simple-mask.rb
new file mode 100644
index 0000000..875a55b
--- /dev/null
+++ b/samples/simple-mask.rb
@@ -0,0 +1,21 @@
+Shoes.app do
+ background black
+
+ stack :top => 0.4, :left => 0.2 do
+ @stripes = stack
+
+ mask do
+ title "Shoes", :weight => "bold", :size => 82
+ end
+ end
+
+ animate 10 do
+ @stripes.clear do
+ 20.times do |i|
+ strokewidth 4
+ stroke rgb((0.0..0.5).rand, (0.0..1.0).rand, (0.0..0.3).rand)
+ line 0, i * 5, 400, i * 8
+ end
+ end
+ end
+end
diff --git a/samples/simple-menu.rb b/samples/simple-menu.rb
new file mode 100644
index 0000000..aba8788
--- /dev/null
+++ b/samples/simple-menu.rb
@@ -0,0 +1,31 @@
+class MenuPanel < Shoes::Widget
+ @@boxes = []
+ def initialize(color, args)
+ @@boxes << self
+ background color
+ para link("Box #{@@boxes.length}", :stroke => white, :fill => nil).
+ click { visit "/" },
+ :margin => 18, :align => "center", :size => 20
+ hover { expand }
+ end
+ def expand
+ if self.width < 170
+ a = animate 30 do
+ @@boxes.each do |b|
+ b.width -= 5 if b != self and b.width > 140
+ end
+ self.width += 5
+ a.stop if self.width >= 170
+ end
+ end
+ end
+end
+
+Shoes.app :width => 400, :height => 130 do
+ style(Link, :underline => nil)
+ style(LinkHover, :fill => nil, :underline => nil)
+ menu_panel green, :width => 170, :height => 120, :margin => 4
+ menu_panel blue, :width => 140, :height => 120, :margin => 4
+ menu_panel red, :width => 140, :height => 120, :margin => 4
+ menu_panel purple, :width => 140, :height => 120, :margin => 4
+end
diff --git a/samples/simple-menu1.rb b/samples/simple-menu1.rb
new file mode 100644
index 0000000..60fb1b6
--- /dev/null
+++ b/samples/simple-menu1.rb
@@ -0,0 +1,35 @@
+class MenuPanel < Shoes::Widget
+ $boxes = []
+ def initialize(color, args)
+ $boxes << self
+ background color
+ para link("Box #{$boxes.length}", :stroke => white, :fill => nil).
+ click { visit "/" },
+ :margin => 18, :align => "center", :size => 20
+ yield
+ end
+end
+
+Shoes.app :width => 600, :height => 130 do
+ style(Link, :underline => nil)
+ style(LinkHover, :fill => nil, :underline => nil)
+
+ expand = proc do
+ hover do |box|
+ if box.width < 170
+ a = animate 30 do
+ $boxes.each do |b|
+ b.width -= 5 if b != box and b.width > 140
+ end
+ box.width += 5
+ a.stop if box.width >= 170
+ end
+ end
+ end
+ end
+
+ menu_panel green, :width => 170, :height => 120, :margin => 4, &expand
+ menu_panel blue, :width => 140, :height => 120, :margin => 4, &expand
+ menu_panel red, :width => 140, :height => 120, :margin => 4, &expand
+ menu_panel purple, :width => 140, :height => 120, :margin => 4, &expand
+end
diff --git a/samples/simple-rubygems.rb b/samples/simple-rubygems.rb
new file mode 100644
index 0000000..7aff52d
--- /dev/null
+++ b/samples/simple-rubygems.rb
@@ -0,0 +1,29 @@
+#
+# The setup block will install gems before launching the
+# rest of the app below it.
+#
+Shoes.setup do
+ gem 'bluecloth =2.0.6'
+ gem 'metaid'
+end
+
+require 'bluecloth'
+require 'metaid'
+
+Shoes.app :width => 300, :height => 400, :resizable => false do
+ background "#eed"
+
+ stack :margin => 40 do
+ tagline "Loaded Gems:", :align => "center", :underline => "single"
+ Gem.loaded_specs.each do |name, spec|
+ para "#{name}\n#{spec.version}", :align => "center"
+ end
+
+ caption "Total Gems: #{Gem.source_index.length}", :align => "center", :margin_bottom => 0
+ para "(includes unloaded gems)", :align => "center", :margin_top => 0
+ button "OK", :bottom => 30, :left => 0.4 do
+ self.close
+ end
+ end
+
+end
diff --git a/samples/simple-slide.rb b/samples/simple-slide.rb
new file mode 100644
index 0000000..d29d617
--- /dev/null
+++ b/samples/simple-slide.rb
@@ -0,0 +1,45 @@
+#
+# mimicking the mootools demo for Fx.Slide
+# http://demos.mootools.net/Fx.Slide
+#
+Shoes.app do
+ def stop_anim
+ @anim.stop
+ @anim = nil
+ end
+ def slide_anim &blk
+ stop_anim if @anim
+ @anim = animate 30, &blk
+ end
+ def slide_out slot
+ slide_anim do |i|
+ slot.height = 150 - (i * 3)
+ slot.contents[0].top = -i * 3
+ if slot.height == 0
+ stop_anim
+ slot.hide
+ end
+ end
+ end
+ def slide_in slot
+ slot.show
+ slide_anim do |i|
+ slot.height = i * 6
+ slot.contents[0].top = slot.height - 150
+ stop_anim if slot.height == 150
+ end
+ end
+
+ background white
+ stack :margin => 10 do
+ para link("slide out") { slide_out @lipsum }, " | ",
+ link("slide in") { slide_in @lipsum }
+ @lipsum = stack :width => 1.0, :height => 150 do
+ stack do
+ background "#ddd"
+ border "#eee", :strokewidth => 5
+ para "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", :margin => 10
+ end
+ end
+ end
+end
diff --git a/samples/simple-sphere.rb b/samples/simple-sphere.rb
new file mode 100644
index 0000000..ab5f23f
--- /dev/null
+++ b/samples/simple-sphere.rb
@@ -0,0 +1,28 @@
+Shoes.app :width => 500, :height => 500, :resizable => false do
+ image 400, 470, :top => 30, :left => 50 do
+ nostroke
+ fill "#127"
+ image :top => 230, :left => 0 do
+ oval 70, 130, 260, 40
+ blur 30
+ end
+ oval 10, 10, 380, 380
+ image :top => 0, :left => 0 do
+ fill "#46D"
+ oval 30, 30, 338, 338
+ blur 10
+ end
+ fill gradient(rgb(1.0, 1.0, 1.0, 0.7), rgb(1.0, 1.0, 1.0, 0.0))
+ oval 80, 14, 240, 176
+ image :top => 0, :left => 0 do
+ fill "#79F"
+ oval 134, 134, 130, 130
+ blur 40
+ end
+ image :top => 150, :left => 40, :width => 320, :height => 260 do
+ fill gradient(rgb(0.7, 0.9, 1.0, 0.0), rgb(0.7, 0.9, 1.0, 0.6))
+ oval 60, 60, 200, 136
+ blur 20
+ end
+ end
+end
diff --git a/samples/simple-sqlite3.rb b/samples/simple-sqlite3.rb
new file mode 100644
index 0000000..fbe131e
--- /dev/null
+++ b/samples/simple-sqlite3.rb
@@ -0,0 +1,13 @@
+require 'sqlite3'
+Shoes.app :width => 350, :height => 130 do
+ db = SQLite3::Database.new "simple-sqlite3.db"
+ db.execute "create table t1 (t1key INTEGER PRIMARY KEY,data " \
+ "TEXT,num double,timeEnter DATE)"
+ db.execute "insert into t1 (data,num) values ('This is sample data',3)"
+ db.execute "insert into t1 (data,num) values ('More sample data',6)"
+ db.execute "insert into t1 (data,num) values ('Aurélio, Küng, Stärk, Uña, Åuksza',6)"
+ db.execute "insert into t1 (data,num) values ('And a little more',9)"
+ rows = db.execute "select * from t1"
+ rows.each{|k, d, n| para "#{k} : #{d} : #{n}\n"}
+end
+
diff --git a/samples/simple-timer.rb b/samples/simple-timer.rb
new file mode 100644
index 0000000..9087345
--- /dev/null
+++ b/samples/simple-timer.rb
@@ -0,0 +1,13 @@
+Shoes.app :height => 150, :width => 250 do
+ background rgb(240, 250, 208)
+ stack :margin => 10 do
+ button "Start" do
+ @time = Time.now
+ @label.replace "Stop watch started at #@time"
+ end
+ button "Stop" do
+ @label.replace "Stopped, ", strong("#{Time.now - @time}"), " seconds elapsed."
+ end
+ @label = para "Press ", strong("start"), " to begin timing."
+ end
+end
diff --git a/samples/simple-video.rb b/samples/simple-video.rb
new file mode 100644
index 0000000..a062db5
--- /dev/null
+++ b/samples/simple-video.rb
@@ -0,0 +1,13 @@
+Shoes.app :width => 408, :height => 346, :resizable => false do
+ background "#eee"
+ stack :margin => 4 do
+ @vid = video "http://www.rin-shun.com/shoes/AdventureTimewithFinnandJakeFinnTime.mp4"
+ end
+ para "controls: ",
+ link("play") { @vid.play }, ", ",
+ link("pause") { @vid.pause }, ", ",
+ link("stop") { @vid.stop }, ", ",
+ link("hide") { @vid.hide }, ", ",
+ link("show") { @vid.show }, ", ",
+ link("+5 sec") { @vid.time += 5000 }
+end
diff --git a/static/Shoes.icns b/static/Shoes.icns
new file mode 100644
index 0000000..fe60fe3
--- /dev/null
+++ b/static/Shoes.icns
Binary files differ
diff --git a/static/app-icon.png b/static/app-icon.png
new file mode 100644
index 0000000..617aa3b
--- /dev/null
+++ b/static/app-icon.png
Binary files differ
diff --git a/static/avatar.png b/static/avatar.png
new file mode 100644
index 0000000..4e0d64b
--- /dev/null
+++ b/static/avatar.png
Binary files differ
diff --git a/static/code_highlighter.js b/static/code_highlighter.js
new file mode 100644
index 0000000..ded1839
--- /dev/null
+++ b/static/code_highlighter.js
@@ -0,0 +1,188 @@
+/* Unobtrustive Code Highlighter By Dan Webb 11/2005
+ Version: 0.4
+
+ Usage:
+ Add a script tag for this script and any stylesets you need to use
+ to the page in question, add correct class names to CODE elements,
+ define CSS styles for elements. That's it!
+
+ Known to work on:
+ IE 5.5+ PC
+ Firefox/Mozilla PC/Mac
+ Opera 7.23 + PC
+ Safari 2
+
+ Known to degrade gracefully on:
+ IE5.0 PC
+
+ Note: IE5.0 fails due to the use of lookahead in some stylesets. To avoid script errors
+ in older browsers use expressions that use lookahead in string format when defining stylesets.
+
+ This script is inspired by star-light by entirely cunning Dean Edwards
+ http://dean.edwards.name/star-light/.
+*/
+
+// replace callback support for safari.
+if ("a".replace(/a/, function() {return "b"}) != "b") (function(){
+ var default_replace = String.prototype.replace;
+ String.prototype.replace = function(search,replace){
+ // replace is not function
+ if(typeof replace != "function"){
+ return default_replace.apply(this,arguments)
+ }
+ var str = "" + this;
+ var callback = replace;
+ // search string is not RegExp
+ if(!(search instanceof RegExp)){
+ var idx = str.indexOf(search);
+ return (
+ idx == -1 ? str :
+ default_replace.apply(str,[search,callback(search, idx, str)])
+ )
+ }
+ var reg = search;
+ var result = [];
+ var lastidx = reg.lastIndex;
+ var re;
+ while((re = reg.exec(str)) != null){
+ var idx = re.index;
+ var args = re.concat(idx, str);
+ result.push(
+ str.slice(lastidx,idx),
+ callback.apply(null,args).toString()
+ );
+ if(!reg.global){
+ lastidx += RegExp.lastMatch.length;
+ break
+ }else{
+ lastidx = reg.lastIndex;
+ }
+ }
+ result.push(str.slice(lastidx));
+ return result.join("")
+ }
+})();
+
+var CodeHighlighter = { styleSets : new Array };
+
+CodeHighlighter.addStyle = function(name, rules) {
+ // using push test to disallow older browsers from adding styleSets
+ if ([].push) this.styleSets.push({
+ name : name,
+ rules : rules,
+ ignoreCase : arguments[2] || false
+ })
+
+ function setEvent() {
+ // set highlighter to run on load (use LowPro if present)
+ if (typeof Event != 'undefined' && typeof Event.onReady == 'function')
+ return Event.onReady(CodeHighlighter.init.bind(CodeHighlighter));
+
+ var old = window.onload;
+
+ if (typeof window.onload != 'function') {
+ window.onload = function() { CodeHighlighter.init() };
+ } else {
+ window.onload = function() {
+ old();
+ CodeHighlighter.init();
+ }
+ }
+ }
+
+ // only set the event when the first style is added
+ if (this.styleSets.length==1) setEvent();
+}
+
+CodeHighlighter.init = function() {
+ if (!document.getElementsByTagName) return;
+ if ("a".replace(/a/, function() {return "b"}) != "b") return; // throw out Safari versions that don't support replace function
+ // throw out older browsers
+
+ var codeEls = document.getElementsByTagName("CODE");
+ // collect array of all pre elements
+ codeEls.filter = function(f) {
+ var a = new Array;
+ for (var i = 0; i < this.length; i++) if (f(this[i])) a[a.length] = this[i];
+ return a;
+ }
+
+ var rules = new Array;
+ rules.toString = function() {
+ // joins regexes into one big parallel regex
+ var exps = new Array;
+ for (var i = 0; i < this.length; i++) exps.push(this[i].exp);
+ return exps.join("|");
+ }
+
+ function addRule(className, rule) {
+ // add a replace rule
+ var exp = (typeof rule.exp != "string")?String(rule.exp).substr(1, String(rule.exp).length-2):rule.exp;
+ // converts regex rules to strings and chops of the slashes
+ rules.push({
+ className : className,
+ exp : "(" + exp + ")",
+ length : (exp.match(/(^|[^\\])\([^?]/g) || "").length + 1, // number of subexps in rule
+ replacement : rule.replacement || null
+ });
+ }
+
+ function parse(text, ignoreCase) {
+ // main text parsing and replacement
+ return text.replace(new RegExp(rules, (ignoreCase)?"gi":"g"), function() {
+ var i = 0, j = 1, rule;
+ while (rule = rules[i++]) {
+ if (arguments[j]) {
+ // if no custom replacement defined do the simple replacement
+ if (!rule.replacement) return "<span class=\"" + rule.className + "\">" + arguments[0] + "</span>";
+ else {
+ // replace $0 with the className then do normal replaces
+ var str = rule.replacement.replace("$0", rule.className);
+ for (var k = 1; k <= rule.length - 1; k++) str = str.replace("$" + k, arguments[j + k]);
+ return str;
+ }
+ } else j+= rule.length;
+ }
+ });
+ }
+
+ function highlightCode(styleSet) {
+ // clear rules array
+ var parsed, clsRx = new RegExp("(\\s|^)" + styleSet.name + "(\\s|$)");
+ rules.length = 0;
+
+ // get stylable elements by filtering out all code elements without the correct className
+ var stylableEls = codeEls.filter(function(item) { return clsRx.test(item.className) });
+
+ // add style rules to parser
+ for (var className in styleSet.rules) addRule(className, styleSet.rules[className]);
+
+
+ // replace for all elements
+ for (var i = 0; i < stylableEls.length; i++) {
+ // EVIL hack to fix IE whitespace badness if it's inside a <pre>
+ if (/MSIE/.test(navigator.appVersion) && stylableEls[i].parentNode.nodeName == 'PRE') {
+ stylableEls[i] = stylableEls[i].parentNode;
+
+ parsed = stylableEls[i].innerHTML.replace(/(<code[^>]*>)([^<]*)<\/code>/i, function() {
+ return arguments[1] + parse(arguments[2], styleSet.ignoreCase) + "</code>"
+ });
+ parsed = parsed.replace(/\n( *)/g, function() {
+ var spaces = "";
+ for (var i = 0; i < arguments[1].length; i++) spaces+= "&nbsp;";
+ return "\n" + spaces;
+ });
+ parsed = parsed.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
+ parsed = parsed.replace(/\n(<\/\w+>)?/g, "<br />$1").replace(/<br \/>[\n\r\s]*<br \/>/g, "<p><br></p>");
+
+ } else parsed = parse(stylableEls[i].innerHTML, styleSet.ignoreCase);
+
+ stylableEls[i].innerHTML = parsed;
+ }
+ }
+
+ // run highlighter on all stylesets
+ for (var i=0; i < this.styleSets.length; i++) {
+ highlightCode(this.styleSets[i]);
+ }
+}
diff --git a/static/code_highlighter_ruby.js b/static/code_highlighter_ruby.js
new file mode 100644
index 0000000..e603118
--- /dev/null
+++ b/static/code_highlighter_ruby.js
@@ -0,0 +1,26 @@
+CodeHighlighter.addStyle("rb",{
+ comment : {
+ exp : /#[^\n]+/
+ },
+ brackets : {
+ exp : /\(|\)|\{|\}/
+ },
+ string : {
+ exp : /'[^']*'|"[^"]*"/
+ },
+ keywords : {
+ exp : /\b(do|end|self|class|def|if|module|yield|then|else|for|until|unless|while|elsif|case|when|break|retry|redo|rescue|raise)\b/
+ },
+ constant : {
+ exp : /\b([A-Z]\w+)\b/
+ },
+ ivar : {
+ exp : /([^@])(@{1,2}\w+)\b/
+ },
+ ns : {
+ exp : /(:{2,})/
+ },
+ symbol : {
+ exp : /(:[A-Za-z0-9_!?]+)/
+ }
+});
diff --git a/static/icon-debug.png b/static/icon-debug.png
new file mode 100644
index 0000000..b3d8ce0
--- /dev/null
+++ b/static/icon-debug.png
Binary files differ
diff --git a/static/icon-error.png b/static/icon-error.png
new file mode 100644
index 0000000..c37bd06
--- /dev/null
+++ b/static/icon-error.png
Binary files differ
diff --git a/static/icon-info.png b/static/icon-info.png
new file mode 100644
index 0000000..12cd1ae
--- /dev/null
+++ b/static/icon-info.png
Binary files differ
diff --git a/static/icon-warn.png b/static/icon-warn.png
new file mode 100644
index 0000000..628cf2d
--- /dev/null
+++ b/static/icon-warn.png
Binary files differ
diff --git a/static/listbox_button1.png b/static/listbox_button1.png
new file mode 100644
index 0000000..985b233
--- /dev/null
+++ b/static/listbox_button1.png
Binary files differ
diff --git a/static/listbox_button2.png b/static/listbox_button2.png
new file mode 100644
index 0000000..4e2b005
--- /dev/null
+++ b/static/listbox_button2.png
Binary files differ
diff --git a/static/man-app.png b/static/man-app.png
new file mode 100644
index 0000000..27f6321
--- /dev/null
+++ b/static/man-app.png
Binary files differ
diff --git a/static/man-builds.png b/static/man-builds.png
new file mode 100644
index 0000000..1d3eafb
--- /dev/null
+++ b/static/man-builds.png
Binary files differ
diff --git a/static/man-builds1.png b/static/man-builds1.png
new file mode 100644
index 0000000..4230778
--- /dev/null
+++ b/static/man-builds1.png
Binary files differ
diff --git a/static/man-editor-notepad.png b/static/man-editor-notepad.png
new file mode 100644
index 0000000..165012f
--- /dev/null
+++ b/static/man-editor-notepad.png
Binary files differ
diff --git a/static/man-editor-osx.png b/static/man-editor-osx.png
new file mode 100644
index 0000000..7d7f5d6
--- /dev/null
+++ b/static/man-editor-osx.png
Binary files differ
diff --git a/static/man-ele-background.png b/static/man-ele-background.png
new file mode 100644
index 0000000..af18a7e
--- /dev/null
+++ b/static/man-ele-background.png
Binary files differ
diff --git a/static/man-ele-border.png b/static/man-ele-border.png
new file mode 100644
index 0000000..9d7c3eb
--- /dev/null
+++ b/static/man-ele-border.png
Binary files differ
diff --git a/static/man-ele-button.png b/static/man-ele-button.png
new file mode 100644
index 0000000..2f4c662
--- /dev/null
+++ b/static/man-ele-button.png
Binary files differ
diff --git a/static/man-ele-check.png b/static/man-ele-check.png
new file mode 100644
index 0000000..a33b9b3
--- /dev/null
+++ b/static/man-ele-check.png
Binary files differ
diff --git a/static/man-ele-editbox.png b/static/man-ele-editbox.png
new file mode 100644
index 0000000..5e4ed3c
--- /dev/null
+++ b/static/man-ele-editbox.png
Binary files differ
diff --git a/static/man-ele-editline.png b/static/man-ele-editline.png
new file mode 100644
index 0000000..784b2aa
--- /dev/null
+++ b/static/man-ele-editline.png
Binary files differ
diff --git a/static/man-ele-image.png b/static/man-ele-image.png
new file mode 100644
index 0000000..0209fa2
--- /dev/null
+++ b/static/man-ele-image.png
Binary files differ
diff --git a/static/man-ele-listbox.png b/static/man-ele-listbox.png
new file mode 100644
index 0000000..785ba5f
--- /dev/null
+++ b/static/man-ele-listbox.png
Binary files differ
diff --git a/static/man-ele-progress.png b/static/man-ele-progress.png
new file mode 100644
index 0000000..0e95a86
--- /dev/null
+++ b/static/man-ele-progress.png
Binary files differ
diff --git a/static/man-ele-radio.png b/static/man-ele-radio.png
new file mode 100644
index 0000000..c16bcd5
--- /dev/null
+++ b/static/man-ele-radio.png
Binary files differ
diff --git a/static/man-ele-shape.png b/static/man-ele-shape.png
new file mode 100644
index 0000000..a014d06
--- /dev/null
+++ b/static/man-ele-shape.png
Binary files differ
diff --git a/static/man-ele-textblock.png b/static/man-ele-textblock.png
new file mode 100644
index 0000000..4c14217
--- /dev/null
+++ b/static/man-ele-textblock.png
Binary files differ
diff --git a/static/man-ele-video.png b/static/man-ele-video.png
new file mode 100644
index 0000000..86a016a
--- /dev/null
+++ b/static/man-ele-video.png
Binary files differ
diff --git a/static/man-intro-dmg.png b/static/man-intro-dmg.png
new file mode 100644
index 0000000..a63f388
--- /dev/null
+++ b/static/man-intro-dmg.png
Binary files differ
diff --git a/static/man-intro-exe.png b/static/man-intro-exe.png
new file mode 100644
index 0000000..0e747b9
--- /dev/null
+++ b/static/man-intro-exe.png
Binary files differ
diff --git a/static/man-look-tiger.png b/static/man-look-tiger.png
new file mode 100644
index 0000000..f086593
--- /dev/null
+++ b/static/man-look-tiger.png
Binary files differ
diff --git a/static/man-look-ubuntu.png b/static/man-look-ubuntu.png
new file mode 100644
index 0000000..e5470e5
--- /dev/null
+++ b/static/man-look-ubuntu.png
Binary files differ
diff --git a/static/man-look-vista.png b/static/man-look-vista.png
new file mode 100644
index 0000000..442564e
--- /dev/null
+++ b/static/man-look-vista.png
Binary files differ
diff --git a/static/man-run-osx.png b/static/man-run-osx.png
new file mode 100644
index 0000000..31b80c2
--- /dev/null
+++ b/static/man-run-osx.png
Binary files differ
diff --git a/static/man-run-vista.png b/static/man-run-vista.png
new file mode 100644
index 0000000..34fcc9b
--- /dev/null
+++ b/static/man-run-vista.png
Binary files differ
diff --git a/static/man-run-xp.png b/static/man-run-xp.png
new file mode 100644
index 0000000..ee6eedb
--- /dev/null
+++ b/static/man-run-xp.png
Binary files differ
diff --git a/static/man-shot1.png b/static/man-shot1.png
new file mode 100644
index 0000000..d18a3fa
--- /dev/null
+++ b/static/man-shot1.png
Binary files differ
diff --git a/static/manual-en.txt b/static/manual-en.txt
new file mode 100644
index 0000000..f452661
--- /dev/null
+++ b/static/manual-en.txt
@@ -0,0 +1,3531 @@
+= Hello! =
+
+Shoes is a tiny graphics toolkit. It's simple and straightforward. Shoes was
+born to be easy! Really, it was made for absolute beginners. There's really
+nothing to it.
+
+You see, the trivial Shoes program can be just one line:
+
+{{{
+ #!ruby
+ Shoes.app { button("Click me!") { alert("Good job.") } }
+}}}
+
+Shoes programs are written in a language called Ruby. When Shoes is handed
+this simple line of Ruby code, a window appears with a button inside reading
+"Click me!" When the button is clicked, a message pops up.
+
+On Linux, here's how this might look: !{:margin_left => 100}man-shot1.png!
+
+While lots of Shoes apps are graphical games and art programs, you can also
+layout text and edit controls easily. !{:margin_left =>
+40}shoes-manual-apps.gif!
+
+And, ideally, Shoes programs will run on any of the major platforms out there.
+Microsoft Windows, Apple's Mac OS X, Linux and many others.
+
+^So, welcome to Shoes' built-in manual. This manual is a Shoes program itself!^
+
+== Introducing Shoes ==
+
+How does Shoes look on OS X and Windows? Does it really look okay? Is it all
+ugly and awkward? People must immediately convulse! It must be so watered down
+trying to do everything.
+
+Well, before getting into the stuff about installing and running Shoes, time to
+just check out some screenshots, to give you an idea of what you can do.
+
+==== Mac OS X ====
+
+Shoes runs on Apple Mac OS X Leopard, as well as Tiger. Shoes supports PowerPC
+machines as well, however, there is no video support on that platform.
+!man-look-tiger.png!
+
+This is the `simple-sphere.rb` sample running on Tiger. Notice that the app
+runs inside a normal OS X window border.
+
+The whole sphere is drawn with blurred ovals and shadows. You can draw and
+animate shapes and apply effects to those shapes in Shoes.
+
+==== Windows ====
+
+Shoes runs on all versions of '''Microsoft Windows XP''', '''Windows Vista''',
+'''Windows 7''', and anything else '''Windows 2000''' compatible.
+!man-look-vista.png!
+
+Above is pictured the `simple-clock.rb` sample running on Windows Vista. This
+example is also draws ovals and lines to build the clock, which is animated to
+repaint itself several times each second.
+
+Notice the text on the top of the app, showing the current time. Shoes has the
+skills to layout words using any color, bold, italics, underlines, and supports
+loading fonts from a file.
+
+==== Linux ====
+
+Here's a screenshot of the `simple-downloader.rb` sample running on '''Ubuntu
+Linux'''. !man-look-ubuntu.png!
+
+Notice the buttons and progress bars. These types of controls look different on
+OS X and Windows. The text and links would look the same, though.
+
+Shapes, text, images and videos all look the same on every platforms. However,
+native controls (like edit lines and edit boxes) will match the look of the
+window theme. Shoes will try to keep native controls all within the size you
+give them, only the look will vary.
+
+== Installing Shoes ==
+
+Okay, on to installing Shoes. I'm sure you're wondering: do I need to install
+Ruby? Do I need to unzip anything? What commands do I need to type?
+
+Nope. You don't need Ruby. You don't need WinZip. Nothing to type.
+
+On most systems, starting Shoes is just a matter of running the installer and
+clicking the Shoes icon. Shoes comes with everything built in. We'll talk
+through all the steps, though, just to be clear about it.
+
+==== Step 1: Installing Shoes ====
+
+You'll want to visit [[http://shoes.heroku.com/ the site of Shoes]] to download
+the Shoes installer. Usually, you'll just want one of the installers on the
+downloads page of the site. !man-builds1.png!
+
+Here's how to run the installer:
+
+ * On '''Mac OS X''', you'll have a file ending with '''.dmg'''. Double-click this file and a window should appear with a '''Shoes''' icon and an '''Applications''' folder. Following the arrow, drag the Shoes icon into the '''Applications''' folder. !man-intro-dmg.png!
+ * On '''Windows''', you'll download a '''.exe''' file. Double-click this file and follow the instructions. !man-intro-exe.png!
+ * On '''Linux''', you'll download a file ending with '''.run'''. Double-click this file and Shoes will start up. (You can also run this file from a prompt as if it was a shell script. In fact, it is a shell script!)
+
+==== Step 2: Start a New Text File ====
+
+Shoes programs are just plain text files ending with a '''.rb''' extension.
+
+Here are a few ways to create a blank text file:
+
+ * On '''Mac OS X''', visit your '''Applications''' folder and double-click on the '''TextEdit''' app. A blank editor window should come up. Now, go to the '''Format''' menu and select the '''Make Plain Text''' option. Okay, you're all set! !man-editor-osx.png!
+ * On '''Windows''', go to the Start menu. Select '''All Programs''', then '''Accessories''', then '''Notepad'''. !man-editor-notepad.png!
+ * On '''Linux''', most distros come with '''gedit'''. You might try running that. Or, if your distro is KDE-based, run '''kate'''.
+
+Now, in your blank window, type in the following:
+
+{{{
+ Shoes.app do
+ background "#DFA"
+ para "Welcome to Shoes"
+ end
+}}}
+
+Save to your desktop as `welcome.rb`.
+
+==== Step 3: Run It! Go Shoes! ====
+
+To run your program:
+
+ * On '''Mac OS X''', visit your '''Applications''' folder again. This time, double-click the '''Shoes''' icon in that folder. You should see the red shoes icon appear in the dock. Drag your `welcome.rb` from the desktop on to that dock icon. !man-run-osx.png!
+ * On '''Windows''', get to the Start menu. Go into '''All Programs''', then '''Shoes''', then '''Shoes'''. A file selector box should come up. Browse to your desktop and select `welcome.rb`. Click '''OK''' and you're on your way. !man-run-xp.png! !man-run-vista.png!
+ * On '''Linux''', run Shoes just like you did in step one. You should see a file selector box. Browse to your desktop, select `welcome.rb` and hit '''OK'''.
+
+So, not much of a program yet. But it's something! You've got the knack of it, at least!
+
+==== What Can You Make With Shoes? ====
+
+Well, you can make windowing applications. But Shoes is inspired by the web, so
+applications tend to use images and text layout rather than a lot of widgets.
+For example, Shoes doesn't come with tabbed controls or toolbars. Shoes is a
+''tiny'' toolkit, remember?
+
+Still, Shoes does have a few widgets like buttons and edit boxes. And many
+missing elements (like tabbed controls or toolbars) can be simulated with
+images.
+
+Shoes is written in part thanks to a very good art engine called Cairo, which
+is used for drawing with shapes and colors. In this way, Shoes is inspired by
+NodeBox and Processing, two very good languages for drawing animated graphics.
+
+== The Rules of Shoes ==
+
+Time to stop guessing how Shoes works. Some of the tricky things will come
+back to haunt you. I've boiled down the central rules to Shoes. These are the
+things you MUST know to really make it all work.
+
+These are general rules found throughout Shoes. While Shoes has an overall
+philosophy of simplicity and clarity, there are a few points that need to be
+studied and remembered.
+
+==== Shoes Tricky Blocks ====
+
+Okay, this is absolutely crucial. Shoes does a trick with blocks. This trick
+makes everything easier to read. But it also can make blocks harder to use
+once you're in deep.
+
+'''Let's take a normal Ruby block:'''
+
+{{{
+ ary = ['potion', 'swords', 'shields']
+ ary.each do |item|
+ puts item
+ end
+}}}
+
+In Shoes, these sorts of blocks work the same. This block above loops through
+the array and stores each object in the `item` variable. The `item` variable
+disappears (goes out of scope) when the block ends.
+
+One other thing to keep in mind is that `self` stays the same inside normal
+Ruby blocks. Whatever `self` was before the call to `each`, it is the same
+inside the `each` block.
+
+'''Both of these things are also true for most Shoes blocks.'''
+
+{{{
+ Shoes.app do
+ stack do
+ para "First"
+ para "Second"
+ para "Third"
+ end
+ end
+}}}
+
+Here we have two blocks. The first block is sent to `Shoes.app`. This `app`
+block changes `self`.
+
+The other block is the `stack` block. That block does NOT change self.
+
+'''For what reason does the `app` block change self?''' Let's start by
+spelling out that last example completely.
+
+{{{
+ Shoes.app do
+ self.stack do
+ self.para "First"
+ self.para "Second"
+ self.para "Third"
+ end
+ end
+}}}
+
+All of the `self`s in the above example are the App object. Shoes uses Ruby's
+`instance_eval` to change self inside the `app` block. So the method calls to
+`stack` and `para` get sent to the app.
+
+'''This also is why you can use instance variables throughout a Shoes app:'''
+
+{{{
+ Shoes.app do
+ @s = stack do
+ @p1 = para "First"
+ @p2 = para "Second"
+ @p3 = para "Third"
+ end
+ end
+}}}
+
+These instance variables will all end up inside the App object.
+
+'''Whenever you create a new window, `self` is also changed.''' So, this means
+the [[Element.window]] and [[Element.dialog]] methods, in addition to
+Shoes.app.
+
+{{{
+ Shoes.app :title => "MAIN" do
+ para self
+ button "Spawn" do
+ window :title => "CHILD" do
+ para self
+ end
+ end
+ end
+}}}
+
+==== Block Redirection ====
+
+The `stack` block is a different story, though. It doesn't change `self` and
+it's basically a regular block.
+
+'''But there's a trick:''' when you attach a `stack` and give it a block, the
+App object places that stack in its memory. The stack gets popped off when the
+block ends. So all drawing inside the block gets '''redirected''' from the
+App's top slot to the new stack.
+
+So those three `para`s will get drawn on the `stack`, even though they actually
+get sent to the App object first.
+
+{{{
+ Shoes.app do
+ stack do
+ para "First"
+ para "Second"
+ para "Third"
+ end
+ end
+}}}
+
+A bit tricky, you see? This can bite you even if you know about it.
+
+One way it'll get you is if you try to edit a stack somewhere else in your
+program, outside the `app` block.
+
+Like let's say you pass around a stack object. And you have a class that edits
+that object.
+
+{{{
+ class Messenger
+ def initialize(stack)
+ @stack = stack
+ end
+ def add(msg)
+ @stack.append do
+ para msg
+ end
+ end
+ end
+}}}
+
+So, let's assume you pass the stack object into your Messenger class when the
+app starts. And, later, when a message comes in, the `add` method gets used to
+append a paragraph to that stack. Should work, right?
+
+Nope, it won't work. The `para` method won't be found. The App object isn't
+around any more. And it's the one with the `para` method.
+
+Fortunately, each Shoes object has an `app` method that will let you reopen the
+App object so you can do somefurther editing.
+
+{{{
+ class Messenger
+ def initialize(stack)
+ @stack = stack
+ end
+ def add(msg)
+ @stack.app do
+ @stack.append do
+ para msg
+ end
+ end
+ end
+ end
+}}}
+
+As you can imagine, the `app` object changes `self` to the App object.
+
+So the rules here are:
+
+1. '''Methods named "app" or which create new windows alter `self` to the App
+object.'''[[BR]](This is true for both Shoes.app and Slot.app, as well as
+[[Element.window]] and [[Element.dialog]].)[[BR]]
+2. '''Blocks attached to stacks, flows or any manipulation method (such as
+append) do not change self. Instead, they pop the slot on to the app's editing
+stack.'''
+
+==== Careful With Fixed Heights ====
+
+Fixed widths on slots are great so you can split the window into columns.
+
+{{{
+ Shoes.app do
+ flow do
+ stack :width => 200 do
+ caption "Column one"
+ para "is 200 pixels wide"
+ end
+ stack :width => -200 do
+ caption "Column two"
+ para "is 100% minus 200 pixels wide"
+ end
+ end
+ end
+}}}
+
+Fixed heights on slots should be less common. Usually you want your text and
+images to just flow down the window as far as they can. Height usually happens
+naturally.
+
+The important thing here is that fixed heights actually force slots to behave
+differently. To be sure that the end of the slot is chopped off perfectly, the
+slot becomes a '''nested window'''. A new layer is created by the operating
+system to keep the slot in a fixed square.
+
+On difference between normal slots and nested window slots is that the latter
+can have scrollbars.
+
+{{{
+ Shoes.app do
+ stack :width => 200, :height => 200, :scroll => true do
+ background "#DFA"
+ 100.times do |i|
+ para "Paragraph No. #{i}"
+ end
+ end
+ end
+}}}
+
+These nested windows require more memory. They tax the application a bit more.
+So if you're experiencing some slowness with hundreds of fixed-height slots,
+try a different approach.
+
+==== Image and Shape Blocks ====
+
+Most beginners start littering the window with shapes. It's just easier to
+throw all your rectangles and ovals in a slot.
+
+'''However, bear in mind that Shoes will create objects for all those
+shapes!'''
+
+{{{
+ Shoes.app do
+ fill black(0.1)
+ 100.times do |i|
+ oval i, i, i * 2
+ end
+ end
+}}}
+
+In this example, one-hundred Oval objects are created. This isn't too bad.
+But things would be slimmer if we made these into a single shape.
+
+{{{
+ Shoes.app do
+ fill black(0.1)
+ shape do
+ 100.times do |i|
+ oval i, i, i * 2
+ end
+ end
+ end
+}}}
+
+Oh, wait. The ovals aren't filled in this time! That's because the ovals have
+been combined into a single huge shape. And Shoes isn't sure where to fill in
+this case.
+
+So you usually only want to combine into a single shape when you're dealing
+strictly with outlines.
+
+Another option is to combine all those ovals into a single image.
+
+{{{
+ Shoes.app do
+ fill black(0.1)
+ image 300, 300 do
+ 100.times do |i|
+ oval i, i, i * 2
+ end
+ end
+ end
+}}}
+
+There we go! The ovals are all combined into a single 300 x 300 pixel image.
+In this case, storing that image in memory might be much bigger than having
+one-hundred ovals around. But when you're dealing with thousands of shapes,
+the image block can be cheaper.
+
+The point is: it's easy to group shapes together into image or shape blocks, so
+give it a try if you're looking to gain some speed. Shape blocks particularly
+will save you some memory and speed.
+
+==== UTF-8 Everywhere ====
+
+Ruby itself isn't Unicode aware. And UTF-8 is a type of Unicode. (See
+[[http://en.wikipedia.org/wiki/UTF-8 Wikipedia]] for a full explanation of
+UTF-8.)
+
+However, UTF-8 is common on the web. And lots of different platforms support
+it. So to cut down on the amount of conversion that Shoes has to do, Shoes
+expects all strings to be in UTF-8 format.
+
+This is great because you can show a myriad of languages (Russian, Japanese,
+Spanish, English) using UTF-8 in Shoes. Just be sure that your text editor
+uses UTF-8!
+
+To illustrate:
+
+{{{
+ Shoes.app do
+ stack :margin => 10 do
+ @edit = edit_box :width => 1.0 do
+ @para.text = @edit.text
+ end
+ @para = para ""
+ end
+ end
+}}}
+
+This app will copy anything you paste into the edit box and display it in a
+Shoes paragraph. You can try copying some foreign text (such as Greek or
+Japanese) into this box to see how it displays.
+
+This is a good test because it proves that the edit box gives back UTF-8
+characters. And the paragraph can be set to any UTF-8 characters.
+
+'''Important note:''' if some UTF-8 characters don't display for you, you will
+need to change the paragraph's font. This is especially common on OS X.
+
+So, a good Japanese font on OS X is '''AppleGothic''' and on Windows is '''MS
+UI Gothic'''.
+
+{{{
+ Shoes.app do
+ para "ã¦ã™ã¨ (te-su-to)", :font => case RUBY_PLATFORM
+ when /mingw/; "MS UI Gothic"
+ when /darwin/; "AppleGothic, Arial"
+ else "Arial"
+ end
+ end
+}}}
+
+Again, anything which takes a string in Shoes will need a UTF-8 string. Edit
+boxes, edit lines, list boxes, window titles and text blocks all take UTF-8. If
+you give a string with bad characters in it, an error will show up in the
+console.
+
+==== The Main App and Its Requires ====
+
+'''NOTE:''' This rule is for Raisins. Policeman uses TOPLEVEL_BINDING. So, you
+can get `main`, Ruby top-level object, with the first snippet. Although you
+need to use `Shoes::Para` instead of `Para` outside `Shoes.app` block.
+
+Each Shoes app is given a little room where it can create itself. You can
+create classes and set variables and they won't be seen by other Shoes
+programs. Each program runs inside its own anonymous class.
+
+{{{
+ main = self
+ Shoes.app do
+ para main.to_s
+ end
+}}}
+
+This anonymous class is called `(shoes)` and it's just an empty, unnamed class.
+The `Shoes` module is mixed into this class (using `include Shoes`) so that you
+can use either `Para` or `Shoes::Para` when referring to the paragraph class.
+
+The advantages of this approach are:
+
+ * Shoes apps cannot share local variables.
+ * Classes created in the main app code are temporary.
+ * The Shoes module can be mixed in to the anonymous class, but not the top-level environment of Ruby itself.
+ * Garbage collection can clean up apps entirely once they complete.
+
+The second part is especially important to remember.
+
+{{{
+ class Storage; end
+
+ Shoes.app do
+ para Storage.new
+ end
+}}}
+
+The `Storage` class will disappear once the app completes. Other apps aren't
+able to use the Storage class. And it can't be gotten to from files that are
+loaded using `require`.
+
+When you `require` code, though, that code will stick around. It will be kept
+in the Ruby top-level environment.
+
+So, the rule is: '''keep your temporary classes in the code with the app and
+keep your permanent classes in requires.'''
+
+= Shoes =
+
+Shoes is all about drawing windows and the stuff inside those windows. Let's
+focus on the window itself, for now. The other sections [[Slots]] and
+[[Elements]] cover everything that goes inside the window.
+
+For here on, the manual reads more like a dictionary. Each page is mostly a
+list of methods you can use for each topic covered. The idea is to be very
+thorough and clear about everything.
+
+So, if you've hit this far in the manual and you're still hazy about getting
+started, you should probably either go back to the [[Hello! beginning]] of the
+manual. Or you could try [[http://github.com/shoes/shoes/downloads Nobody Knows
+Shoes]], the beginner's leaflet PDF.
+
+==== Finding Your Way ====
+
+This section covers:
+
+ * [[Built-in Built-in methods]] - general methods available anywhere in a Shoes program.
+ * [[App The App window]] - methods found attached to every main Shoes window.
+ * [[Styles The Styles Master List]] - a complete list of every style in Shoes.
+ * [[Classes The Classes list]] - a chart showing what Shoes classes subclass what.
+ * [[Colors The Colors list]] - a chart of all built-in colors and the [[Built-in.rgb]] numbers for each.
+
+If you find yourself paging around a lot and not finding something, give the
+[[Search]] page a try. It's the quickest way to get around.
+
+After this general reference, there are two other more specific sections:
+
+ * [[Slots]] - covering [[Element.stack]] and [[Element.flow]], the two types of slots.
+ * [[Elements]] - documentation for all the buttons, shapes, images, and so on.
+
+Two really important pages in there are the [[Element Element Creation]] page
+(which lists all the elements you can add) and the [[Common Common Methods]]
+page (which lists methods you'll find on any slot or element.)
+
+== Built-in Methods ==
+
+These methods can be used anywhere throughout Shoes programs.
+
+All of these commands are unusual because you don't attach them with a dot.
+'''Every other method in this manual must be attached to an object with a dot.'''
+But these are built-in methods (also called: Kernel methods.) Which means no dot!
+
+A common one is `alert`:
+
+{{{
+ #!ruby
+ alert "No dots in sight"
+}}}
+
+Compare that to the method `reverse`, which isn't a Kernel method and is only
+available for Arrays and Strings:
+
+{{{
+ #!ruby
+ "Plaster of Paris".reverse
+ #=> "siraP fo retsalP"
+ [:dogs, :cows, :snakes].reverse
+ #=> [:snakes, :cows, :dogs]
+}}}
+
+Most Shoes methods for drawing and making buttons and so on are attached to
+slots. See the section on [[Slots]] for more.
+
+==== Built-in Constants ====
+
+Shoes also has a handful of built-in constants which may prove useful if you
+are trying to sniff out what release of Shoes is running.
+
+'''Shoes::RELEASE_NAME''' contains a string with the name of the Shoes release.
+All Shoes releases are named, starting with Curious.
+
+'''Shoes::RELEASE_ID''' contains a number representing the Shoes release. So,
+for example, Curious is number 1, as it was the first official release.
+
+'''Shoes::REVISION''' is the Subversion revision number for this build.
+
+'''Shoes::FONTS''' is a complete list of fonts available to the app. This list
+includes any fonts loaded by the [[Built-in.font]] method.
+
+=== alert(message: a string) » nil ===
+
+Pops up a window containing a short message.
+
+{{{
+ #!ruby
+ alert("I'm afraid I must interject!")
+}}}
+
+Please use alerts sparingly, as they are incredibly annoying! If you are using
+alerts to show messages to help you debug your program, try checking out the
+[[Built-in.debug]] or [[Built-in.info]] methods.
+
+=== ask(message: a string) » a string ===
+
+Pops up a window and asks a question. For example, you may want to ask someone
+their name.
+
+{{{
+ #!ruby
+ name = ask("Please, enter your name:")
+}}}
+
+When the above script is run, the person at the computer will see a window with
+a blank box for entering their name. The name will then be saved in the `name`
+variable.
+
+=== ask_color(title: a string) » Shoes::Color ===
+
+Pops up a color picker window. The program will wait for a color to be picked,
+then gives you back a Color object. See the `Color` help for some ways you can
+use this color.
+
+{{{
+ #!ruby
+ backcolor = ask_color("Pick a background")
+ Shoes.app do
+ background backcolor
+ end
+}}}
+
+=== ask_open_file() » a string ===
+
+Pops up an "Open file..." window. It's the standard window which shows all of
+your folders and lets you select a file to open. Hands you back the name of the
+file.
+
+{{{
+ #!ruby
+ filename = ask_open_file
+ Shoes.app do
+ para File.read(filename)
+ end
+}}}
+
+=== ask_save_file() » a string ===
+
+Pops up a "Save file..." window, similiar to `ask_open_file`, described
+previously.
+
+{{{
+ #!ruby
+ save_as = ask_save_file
+}}}
+
+=== ask_open_folder() » a string ===
+
+Pops up an "Open folder..." window. It's the standard window which shows all of
+your folders and lets you select a folder to open. Hands you back the name of
+the folder.
+
+{{{
+ #!ruby
+ folder = ask_open_folder
+ Shoes.app do
+ para Dir.entries(folder)
+ end
+}}}
+
+=== ask_save_folder() » a string ===
+
+Pops up a "Save folder..." window, similiar to `ask_open_folder`, described
+previously. On OS X, this method currently behaves like an alias of
+`ask_open_folder`.
+
+{{{
+ #!ruby
+ save_to = ask_save_folder
+}}}
+
+
+=== confirm(question: a string) » true or false ===
+
+Pops up a yes-or-no question. If the person at the computer, clicks '''yes''',
+you'll get back a `true`. If not, you'll get back `false`.
+
+{{{
+ #!ruby
+ if confirm("Draw a circle?")
+ Shoes.app{ oval :top => 0, :left => 0, :radius => 50 }
+ end
+}}}
+
+=== debug(message: a string) » nil ===
+
+Sends a debug message to the Shoes console. You can bring up the Shoes console
+by pressing `Alt-/` on any Shoes window (or `⌘-/` on OS X.)
+
+{{{
+ #!ruby
+ debug("Running Shoes on " + RUBY_PLATFORM)
+}}}
+
+Also check out the [[Built-in.error]], [[Built-in.warn]] and [[Built-in.info]]
+methods.
+
+=== error(message: a string) » nil ===
+
+Sends an error message to the Shoes console. This method should only be used
+to log errors. Try the [[Built-in.debug]] method for logging messages to
+yourself.
+
+Oh, and, rather than a string, you may also hand exceptions directly to this
+method and they'll be formatted appropriately.
+
+=== exit() ===
+
+Stops your program. Call this anytime you want to suddenly call it quits.
+
+'''PLEASE NOTE:''' If you need to use Ruby's own `exit` method (like in a
+forked Ruby process,) call `Kernel.exit`.
+
+=== font(message: a string) » an array of font family names ===
+
+Loads a TrueType (or other type of font) from a file. While TrueType is
+supported by all platforms, your platform may support other types of fonts.
+Shoes uses each operating system's built-in font system to make this work.
+
+Here's a rough idea of what fonts work on which platforms:
+
+ * Bitmap fonts (.bdf, .pcf, .snf) - Linux
+ * Font resource (.fon) - Windows
+ * Windows bitmap font file (.fnt) - Linux, Windows
+ * PostScript OpenType font (.otf) - Mac OS X, Linux, Windows
+ * Type1 multiple master (.mmm) - Windows
+ * Type1 font bits (.pfb) - Linux, Windows
+ * Type1 font metrics (.pfm) - Linux, Windows
+ * TrueType font (.ttf) - Mac OS X, Linux, Windows
+ * TrueType collection (.ttc) - Mac OS X, Linux, Windows
+
+If the font is properly loaded, you'll get back an array of font names found in
+the file. Otherwise, `nil` is returned if no fonts were found in the file.
+
+Also of interest: the `Shoes::FONTS` constant is a complete list of fonts
+available to you on this platform. You can check for a certain font by using
+`include?`.
+
+{{{
+ if Shoes::FONTS.include? "Helvetica"
+ alert "Helvetica is available on this system."
+ else
+ alert "You do not have the Helvetica font."
+ end
+}}}
+
+If you have trouble with fonts showing up, make sure your app loads the font
+before it is used. Especially on OS X, if fonts are used before they are
+loaded, the font cache will tend to ignore loaded fonts.
+
+=== gradient(color1, color2) » Shoes::Pattern ===
+
+Builds a linear gradient from two colors. For each color, you may pass in a
+Shoes::Color object or a string describing the color.
+
+=== gray(the numbers: darkness, alpha) » Shoes::Color ===
+
+Create a grayscale color from a level of darkness and, optionally, an alpha
+level.
+
+{{{
+ black = gray(0.0)
+ white = gray(1.0)
+}}}
+
+=== info(message: a string) » nil ===
+
+Logs an informational message to the user in the Shoes console. So, where
+debug messages are designed to help the program figure out what's happening,
+`info` messages tell the user extra information about the program.
+
+{{{
+ #!ruby
+
+ info("You just ran the info example on Shoes #{Shoes::RELEASE_NAME}.")
+}}}
+
+For example, whenever a Shy file loads, Shoes prints an informational message
+in the console describing the author of the Shy and its version.
+
+=== rgb(a series of numbers: red, green, blue, alpha) » Shoes::Color ===
+
+Create a color from red, green and blue components. An alpha level (indicating
+transparency) can also be added, optionally.
+
+When passing in a whole number, use values from 0 to 255.
+
+{{{
+ blueviolet = rgb(138, 43, 226)
+ darkgreen = rgb(0, 100, 0)
+}}}
+
+Or, use a decimal number from 0.0 to 1.0.
+
+{{{
+ blueviolet = rgb(0.54, 0.17, 0.89)
+ darkgreen = rgb(0, 0.4, 0)
+}}}
+
+This method may also be called as `Shoes.rgb`.
+
+=== warn(message: a string) » nil ===
+
+Logs a warning for the user. A warning is not a catastrophic error (see
+[[Built-in.error]] for that.) It is just a notice that the program will be
+changing in the future or that certain parts of the program aren't reliable
+yet.
+
+To view warnings and errors, open the Shoes console with `Alt-/` (or `⌘-/` on
+OS X.)
+
+== The App Object ==
+
+An App is a single window running code at a URL. When you switch URLs, a new
+App object is created and filled up with stacks, flows and other Shoes
+elements.
+
+The App is the window itself. Which may be closed or cleared and filled with
+new elements. !{:margin_left => 100}man-app.png!
+
+The App itself, in slot/box terminology, is a flow. See the ''Slots'' section
+for more, but this just means that any elements placed directly at the
+top-level will flow.
+
+=== Shoes.app(styles) { ... } » Shoes::App ===
+
+Starts up a Shoes app window. This is the starting place for making a Shoes
+program. Inside the block, you fill the window with various Shoes elements
+(buttons, artwork, etc.) and, outside the block, you use the `styles` to
+describe how big the window is. Perhaps also the name of the app or if it's
+resizable.
+
+{{{
+ #!ruby
+ Shoes.app(:title => "White Circle",
+ :width => 200, :height => 200, :resizable => false) {
+ background black
+ fill white
+ oval :top => 20, :left => 20, :radius => 160
+ }
+}}}
+
+In the case above, a small window is built. 200 pixels by 200 pixels. It's
+not resizable. And, inside the window, two elements: a black background and a
+white circle.
+
+Once an app is created, it is added to the [[App.Shoes.APPS]] list. If you
+want an app to spawn more windows, see the [[Element.window]] method and the
+[[Element.dialog]] method.
+
+=== Shoes.APPS() » An array of Shoes::App objects ===
+
+Builds a complete list of all the Shoes apps that are open right now. Once an
+app is closed, it is removed from the list. Yes, you can run many apps at once
+in Shoes. It's completely encouraged.
+
+=== clipboard() » a string ===
+
+Returns a string containing all of the text that's on the system clipboard.
+This is the global clipboard that every program on the computer cuts and pastes
+into.
+
+=== clipboard = a string ===
+
+Stores `a string` of text in the system clipboard.
+
+=== close() ===
+
+Closes the app window. If multiple windows are open and you want to close the
+entire application, use the built-in method `exit`.
+
+=== download(url: a string, styles) ===
+
+Starts a download thread (much like XMLHttpRequest, if you're familiar with
+JavaScript.) This method returns immediately and runs the download in the
+background. Each download thread also fires `start`, `progress` and `finish`
+events. You can send the download to a file or just get back a string (in the
+`finish` event.)
+
+If you attach a block to a download, it'll get called as the `finish` event.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ title "Searching Google", :size => 16
+ @status = para "One moment..."
+
+ # Search Google for 'shoes' and print the HTTP headers
+ download "http://www.google.com/search?q=shoes" do |goog|
+ @status.text = "Headers: " + goog.response.headers.inspect
+ end
+ end
+ end
+}}}
+
+And, if we wanted to use the downloaded data, we'd get it using
+`goog.response.body`. This example is truly the simplest form of `download`:
+pulling some web data down into memory and handling it once it's done.
+
+Another simple use of `download` is to save some web data to a file, using the
+`:save` style.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ title "Downloading Google image", :size => 16
+ @status = para "One moment..."
+
+ download "http://www.google.com/logos/nasa50th.gif",
+ :save => "nasa50th.gif" do
+ @status.text = "Okay, is downloaded."
+ end
+ end
+ end
+}}}
+
+In this case, you can still get the headers for the downloaded file, but
+`response.body` will be `nil`, since the data wasn't saved to memory. You will
+need to open the file to get the downloaded goods.
+
+If you need to send certain headers or actions to the web server, you can use
+the `:method`, `:headers` and `:body` styles to customize the HTTP request.
+(And, if you need to go beyond these, you can always break out Ruby's OpenURI
+class.)
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ title "GET Google", :size => 16
+ @status = para "One moment..."
+
+ download "http://www.google.com/search?q=shoes",
+ :method => "GET" do |dump|
+ @status.text = dump.response.body
+ end
+ end
+ end
+}}}
+
+As you can see from the above example, Shoes makes use of the "GET" method to
+query google's search engine.
+
+=== location() » a string ===
+
+Gets a string containing the URL of the current app.
+
+=== mouse() » an array of numbers: button, left, top ===
+
+Identifies the mouse cursor's location, along with which button is being
+pressed.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @p = para
+ animate do
+ button, left, top = self.mouse
+ @p.replace "mouse: #{button}, #{left}, #{top}"
+ end
+ end
+}}}
+
+=== owner() » Shoes::App ===
+
+Gets the app which launched this app. In most cases, this will be `nil`. But
+if this app was launched using the [[Element.window]] method, the owner will be
+the app which called `window`.
+
+=== started?() » true or false ===
+
+Has the window been fully constructed and displayed? This is useful for
+threaded code which may try to use the window before it is completely built.
+(Also see the `start` event which fires once the window is open.)
+
+=== visit(url: a string) ===
+
+Changes the location, in order to view a different Shoes URL.
+
+Absolute URLs (such as http://google.com) are okay, but Shoes will be expecting
+a Shoes application to be at that address. (So, google.com won't work, as it's
+an HTML app.)
+
+== The Styles Master List ==
+
+You want to mess with the look of things? Well, throughout Shoes, styles are
+used to change the way elements appear. In some cases, you can even style an
+entire class of elements. (Like giving all paragraphs a certain font.)
+
+Styles are easy to spot. They usually show up when the element is created.
+
+{{{
+ Shoes.app :title => "A Styling Sample" do
+ para "Red with an underline", :stroke => red, :underline => "single"
+ end
+}}}
+
+Here we've got a `:title` style on the app. And on the paragraph inside the
+app, a red `:stroke` style and an `:underline` style.
+
+The style hash can also be changed by using the [[Common.style]] method,
+available on every element and slot.
+
+{{{
+ Shoes.app :title => "A Styling Sample" do
+ @text = para "Red with an underline"
+ @text.style(:stroke => red, :underline => "single")
+ end
+}}}
+
+Most styles can also be set by calling them as methods. (I'd use the manual
+search to find the method.)
+
+{{{
+ Shoes.app :title => "A Styling Sample" do
+ @text = para "Red with an underline"
+ @text.stroke = red
+ @text.underline = "single"
+ end
+}}}
+
+Rather than making you plow through the whole manual to figure out what styles
+go where, this helpful page speeds through every style in Shoes and suggests
+where that style is used.
+
+=== :align » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''
+
+The alignment of the text. It is either:
+
+ * "left" - Align the text to the left.
+ * "center" - Align the text in the center.
+ * "right" - Align the text to the right.
+
+=== :angle » a number ===
+
+For: ''background, border, gradient''.
+
+The angle at which to apply a gradient. Normally, gradient colors range from
+top to bottom. If the `:angle` is set to 90, the gradient will rotate 90
+degrees counter-clockwise and the gradient will go from left to right.
+
+=== :attach » a slot or element ===
+
+For: ''flow, stack''.
+
+Pins a slot relative to another slot or element. Also, one may write `:attach
+=> Window` to position the slot at the window's top, left corner. Taking this
+a bit further, the style `:top => 10, :left => 10, :attach => Window` would
+place the slot at (10, 10) in the window's coordinates.
+
+If a slot is attached to an element that moves, the slot will move with it. If
+the attachment is reset to `nil`, the slot will flow in with the other objects
+that surround, as normal.
+
+=== :autoplay » true or false ===
+
+For: ''video''.
+
+Should this video begin playing after it appears? If set to `true`, the video
+will start without asking the user.
+
+=== :bottom » a number ===
+
+For: ''all slots and elements''.
+
+Sets the pixel coordinate of an element's lower edge. The edge is placed
+relative to its container's lower edge. So, `:bottom => 0` will align the
+element so that its bottom edge and the bottom edge of its slot touch.
+
+=== :cap » :curve or :rect or :project ===
+
+For: ''arc, arrow, border, flow, image, mask, rect, star, shape, stack''.
+
+Sets the shape of the line endpoint, whether curved or square. See the
+[[Art.cap]] method for more explanation.
+
+=== :center » true or false ===
+
+For: ''arc, image, oval, rect, shape''.
+
+Indicates whether the `:top` and `:left` coordinates refer to the center of the
+shape or not. If set to `true`, this is similar to setting the
+[[Art.transform]] method to `:center`.
+
+=== :change » a proc ===
+
+For: ''edit_box, edit_line, list_box''.
+
+The `change` event handler is stored in this style. See the [[EditBox.change]]
+method for the edit_box, as an example.
+
+=== :checked » true or false ===
+
+For: ''check, radio''.
+
+Is this checkbox or radio button checked? If set to `true`, the box is
+checked. Also see the [[Check.checked=]] method.
+
+=== :choose » a string ===
+
+For: ''list_box''.
+
+Sets the currently chosen item in the list. More information at
+[[ListBox.choose]].
+
+=== :click » a proc ===
+
+For: ''arc, arrow, banner, button, caption, check, flow, image, inscription,
+line, link, mask, oval, para, radio, rect, shape, stack, star, subtitle,
+tagline, title''.
+
+The `click` event handler is stored in this style. See the [[Events.click]]
+method for a description.
+
+=== :curve » a number ===
+
+For: ''background, border, rect''.
+
+The radius of curved corners on each of these rectangular elements. As an
+example, if this is set to 6, the corners of the rectangle are given a curve
+with a 6-pixel radius.
+
+=== :displace_left » a number ===
+
+For: ''all slots and elements''.
+
+Moves a shape, text block or any other kind of object to the left or right. A
+positive number displaces to the right by the given number of pixels; a
+negative number displaces to the left. Displacing an object doesn't effect the
+actual layout of the page. Before using this style, be sure to read the
+[[Position.displace]] docs, since its behavior can be a bit surprising.
+
+=== :displace_top » a number ===
+
+For: ''all slots and elements''.
+
+Moves a shape, text block or any other kind of object up or down. A positive
+number moves the object down by this number of pixels; a negative number moves
+it up. Displacing doesn't effect the actual layout of the page or the object's
+true coordinates. Read the [[Position.displace]] docs, since its behavior can
+be a bit surprising.
+
+=== :emphasis » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Styles the text with an emphasis (commonly italicized.)
+
+This style recognizes three possible settings:
+
+ * "normal" - the font is upright.
+ * "oblique" - the font is slanted, but in a roman style.
+ * "italic" - the font is slanted in an italic style.
+
+=== :family » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Styles the text with a given font family. The string should contain the family
+name or a comma-separated list of families.
+
+=== :fill » a hex code, a Shoes::Color or a range of either ===
+
+For: ''arc, arrow, background, banner, caption, code, del, em, flow, image,
+ins, inscription, line, link, mask, oval, para, rect, shape, span, stack, star,
+strong, sub, sup, subtitle, tagline, title''.
+
+The color of the background pen. For shapes, this is the fill color, the paint
+inside the shape. For text stuffs, this color is painted in the background (as
+if marked with a highlighter pen.)
+
+=== :font » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Styles the text with a font description. The string is pretty flexible, but
+can take the form "[FAMILY-LIST] [STYLE-OPTIONS] [SIZE]", where FAMILY-LIST is
+a comma separated list of families optionally terminated by a comma,
+STYLE_OPTIONS is a whitespace separated list of words where each WORD describes
+one of style, variant, weight, stretch, or gravity, and SIZE is a decimal
+number (size in points) or optionally followed by the unit modifier "px" for
+absolute size. Any one of the options may be absent. If FAMILY-LIST is absent,
+then the default font family (Arial) will be used.
+
+=== :group » a string ===
+
+For: ''radio''.
+
+Indicates what group a radio button belongs to. Without this setting, radio
+buttons are grouped together with other radio buttons in their immediate slot.
+"Grouping" radio buttons doesn't mean they'll be grouped next to each other on
+the screen. It means that only one radio button from the group can be selected
+at a time.
+
+By giving this style a string, the radio button will be grouped with other
+radio buttons that have the same group name.
+
+=== :height » a number ===
+
+For: ''all slots and elements''.
+
+Sets the pixel height of this object. If the number is a decimal number, the
+height becomes a percentage of its parent's height (with 0.0 being 0% and 1.0
+being 100%.)
+
+=== :hidden » true or false ===
+
+For: ''all slots and elements''.
+
+Hides or shows this object. Any object with `:hidden => true` are not
+displayed on the screen. Neither are its children.
+
+=== :inner » a number ===
+
+For: ''star''.
+
+The size of the inner radius (in pixels.) The inner radius describes the solid
+circle within the star where the points begin to separate.
+
+=== :items » an array ===
+
+For: ''list_box''.
+
+The list of selections in the list box. See the [[Element.list_box]] method
+for an example.
+
+=== :justify » true or false ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''
+
+Evenly spaces the text horizontally.
+
+=== :kerning » a number ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Adds to the natural spacing between letters, in pixels.
+
+=== :leading » a number ===
+
+For: ''banner, caption, inscription, para, subtitle, tagline, title''.
+
+Sets the spacing between lines in a text block. Defaults to 4 pixels.
+
+=== :left » a number ===
+
+For: ''all slots and elements''.
+
+Sets the left coordinate of this object to a specific pixel. Setting `:left =>
+10` places the object's left edge ten pixels away from the left edge of the
+slot containing it. If this style is left unset (or set to `nil`,) the object
+will flow in with the other objects surrounding it.
+
+=== :margin » a number or an array of four numbers ===
+
+For: ''all slots and elements''.
+
+Margins space an element out from its surroundings. Each element has a left,
+top, right, and bottom margin. If the `:margin` style is set to a single
+number, the spacing around the element uniformly matches that number. In other
+words, if `:margin => 8` is set, all the margins around the element are set to
+eight pixels in length.
+
+This style can also be given an array of four numbers in the form `[left, top,
+right, bottom]`.
+
+=== :margin_bottom » a number ===
+
+For: ''all slots and elements''.
+
+Sets the bottom margin of the element to a specific pixel size.
+
+=== :margin_left » a number ===
+
+For: ''all slots and elements''.
+
+Sets the left margin of the element to a specific pixel size.
+
+=== :margin_right » a number ===
+
+For: ''all slots and elements''.
+
+Sets the right margin of the element to a specific pixel size.
+
+=== :margin_top » a number ===
+
+For: ''all slots and elements''.
+
+Sets the top margin of the element to a specific pixel size.
+
+=== :outer » a number ===
+
+For: ''star''.
+
+Sets the outer radius (half of the ''total'' width) of the star, in pixels.
+
+=== :points » a number ===
+
+For: ''star''.
+
+How many points does this star have? A style of `:points => 5` creates a
+five-pointed star.
+
+=== :radius » a number ===
+
+For: ''arc, arrow, background, border, gradient, oval, rect, shape''.
+
+Sets the radius (half of the diameter or total width) for each of these
+elements. Setting this is equivalent to setting both `:width` and `:height` to
+double this number.
+
+=== :right » a number ===
+
+For: ''all slots and elements''.
+
+Sets the pixel coordinate of an element's right edge. The edge is placed
+relative to its container's rightmost edge. So, `:right => 0` will align the
+element so that its own right edge and the right edge of its slot touch.
+Whereas `:right => 20` will position the right edge of the element off to the
+left of its slot's right edge by twenty pixels.
+
+=== :rise » a number ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Lifts or plunges the font baseline for some text. For example, a
+[[Element.sup]] has a `:rise` of 10 pixels. Conversely, the [[Element.sub]]
+element has a `:rise` of -10 pixels.
+
+=== :scroll » true or false ===
+
+For: ''flow, stack''.
+
+Establishes this slot as a scrolling slot. If `:scroll => true` is set, the
+slot will show a scrollbar if any of its contents go past its height. The
+scrollbar will appear and disappear as needed. It will also appear inside the
+width of the slot, meaning the slot's width will never change, regardless of
+whether there is a scrollbar or not.
+
+=== :secret » true or false ===
+
+For: ''ask, edit_line''.
+
+Used for password fields, this setting keeps any characters typed in from
+becoming visible on the screen. Instead, a replacement character (such as an
+asterisk) is show for each letter typed.
+
+=== :size » a number ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Sets the pixel size for the font used inside this text block or text fragment.
+
+Font size may also be augmented, through use of the following strings:
+
+ * "xx-small" - 57% of present size.
+ * "x-small" - 64% of present size.
+ * "small" - 83% of present size.
+ * "medium" - no change in size.
+ * "large" - 120% of present size.
+ * "x-large" - 143% of present size.
+ * "xx-large" - 173% of present size.
+
+=== :state » a string ===
+
+For: ''button, check, edit_box, edit_line, list_box, radio''.
+
+The `:state` style is for disabling or locking certain controls, if you don't
+want them to be edited.
+
+Here are the possible style settings:
+
+ * nil - the control is active and editable.
+ * "readonly" - the control is active but cannot be edited.
+ * "disabled" - the control is not active (grayed out) and cannot be edited.
+
+=== :stretch » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Sets the font stretching used for a text object.
+
+Possible settings are:
+
+ * "condensed" - a smaller width of letters.
+ * "normal" - the standard width of letters.
+ * "expanded" - a larger width of letters.
+
+=== :strikecolor » a Shoes::Color ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+The color used to paint any lines stricken through this text.
+
+=== :strikethrough » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Is this text stricken through? Two options here:
+
+ * "none" - no strikethrough
+ * "single" - a single-line strikethrough.
+
+=== :stroke » a hex code, a Shoes::Color or a range of either ===
+
+For: ''arc, arrow, banner, border, caption, code, del, em, flow, image, ins,
+inscription, line, link, mask, oval, para, rect, shape, span, stack, star,
+strong, sub, sup, subtitle, tagline, title''.
+
+The color of the foreground pen. In the case of shapes, this is the color the
+lines are drawn with. For paragraphs and other text, the letters are printed
+in this color.
+
+=== :strokewidth » a number ===
+
+For: ''arc, arrow, border, flow, image, line, mask, oval, rect, shape, star, stack''.
+
+The thickness of the stroke, in pixels, of the line defining each of these
+shapes. For example, the number two would set the strokewidth to 2 pixels.
+
+=== :text » a string ===
+
+For: ''button, edit_box, edit_line''.
+
+Sets the message displayed on a button control, or the contents of an edit_box
+or edit_line.
+
+=== :top » a number ===
+
+For: ''all slots and elements''.
+
+Sets the top coordinate for an object, relative to its parent slot. If an
+object is set with `:top => 40`, this means the object's top edge will be
+placed 40 pixels beneath the top edge of the slot that contains it. If no
+`:top` style is given, the object is automatically placed in the natural flow
+of its slot.
+
+=== :undercolor » a Shoes::Color ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+The color used to underline text.
+
+=== :underline » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Dictates the style of underline used in the text.
+
+The choices for this setting are:
+
+ * "none" - no underline at all.
+ * "single" - a continuous underline.
+ * "double" - two continuous parallel underlines.
+ * "low" - a lower underline, beneath the font baseline. (This is generally recommended only for single characters, particularly when showing keyboard accelerators.)
+ * "error" - a wavy underline, usually found indicating a misspelling.
+
+=== :variant » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Vary the font for a group of text. Two choices:
+
+ * "normal" - standard font.
+ * "smallcaps" - font with the lower case characters replaced by smaller variants of the capital characters.
+
+=== :weight » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''.
+
+Set the boldness of the text. Commonly, this style is set to one of the
+following strings:
+
+ * "ultralight" - the ultralight weight (= 200)
+ * "light" - the light weight (= 300)
+ * "normal" - the default weight (= 400)
+ * "semibold" - a weight intermediate between normal and bold (= 600)
+ * "bold" - the bold weight (= 700)
+ * "ultrabold" - the ultrabold weight (= 800)
+ * "heavy" - the heavy weight (= 900)
+
+However, you may also pass in the numerical weight directly.
+
+=== :width » a number ===
+
+For: ''all slots and elements''.
+
+Sets the pixel width for the element. If the number is a decimal, the width is
+converted to a percentage (with 0.0 being 0% and 1.0 being 100%.) A width of
+100% means the object fills its parent slot.
+
+=== :wrap » a string ===
+
+For: ''banner, caption, code, del, em, ins, inscription, link, para, span,
+strong, sub, sup, subtitle, tagline, title''
+
+How should the text wrap when it fills its width? Possible options are:
+
+ * "word" - Break lines at word breaks.
+ * "char" - Break lines between characters, thus breaking some words.
+ * "trim" - Cut the line off with an ellipsis if it goes too long.
+
+== Classes List ==
+
+Here is a complete list of all the classes introduced by Shoes. This chart is
+laid out according to how classes inherits from each other. Subclasses are
+indented one level to the right, beneath their parent class.
+
+{INDEX}
+
+== Colors List ==
+
+The following list of colors can be used throughout Shoes. As background
+colors or border colors. As stroke and fill colors. Most of these colors come
+from the X11 and HTML palettes.
+
+All of these colors can be used by name. (So calling the `tomato` method from
+inside any slot will get you a nice reddish color.) Below each color, also
+find the exact numbers which can be used with the [[Built-in.rgb]] method.
+
+{COLORS}
+
+= Slots =
+
+Slots are boxes used to lay out images, text and so on. The two most common
+slots are `stacks` and `flows`. Slots can also be referred to as "boxes" or
+"canvases" in Shoes terminology.
+
+Since the mouse wheel and PageUp and PageDown are so pervasive on every
+platform, vertical scrolling has really become the only overflow that matters.
+So, in Shoes, just as on the web, width is generally fixed. While height goes
+on and on.
+
+Now, you can also just use specific widths and heights for everything, if you
+want. That'll take some math, but everything could be perfect.
+
+Generally, I'd suggest using stacks and flows. The idea here is that you want
+to fill up a certain width with things, then advance down the page, filling up
+further widths. You can think of these as being analogous to HTML's "block" and
+"inline" styles.
+
+==== Stacks ====
+
+A stack is simply a vertical stack of elements. Each element in a stack is
+placed directly under the element preceding it.
+
+A stack is also shaped like a box. So if a stack is given a width of 250, that
+stack is itself an element which is 250 pixels wide.
+
+To create a new stack, use the [[Element.stack]] method, which is available
+inside any slot. So stacks can contain other stacks and flows.
+
+==== Flows ====
+
+A flow will pack elements in as tightly as it can. A width will be filled, then
+will wrap beneath those elements. Text elements placed next to each other will
+appear as a single paragraph. Images and widgets will run together as a series.
+
+Like the stack, a flow is a box. So stacks and flows can safely be embedded
+and, without respect to their contents, are identical. They just treat their
+contents differently.
+
+Making a flow means calling the [[Element.flow]] method. Flows may contain
+other flows and stacks.
+
+Last thing: The Shoes window itself is a flow.
+
+== Art for Slots ==
+
+Each slot is like a canvas, a blank surface which can be covered with an
+assortment of colored shapes or gradients.
+
+Many common shapes can be drawn with methods like `oval` and `rect`. You'll
+need to set up the paintbrush colors first, though.
+
+The `stroke` command sets the line color. And the `fill` command sets the
+color used to paint inside the lines.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stroke red
+ fill blue
+ oval :top => 10, :left => 10,
+ :radius => 100
+ end
+}}}
+
+That code gives you a blue pie with a red line around it. One-hundred pixels
+wide, placed just a few pixels southeast of the window's upper left corner.
+
+The `blue` and `red` methods above are Color objects. See the section on
+Colors for more on how to mix colors.
+
+==== Inspiration from Processing and NodeBox ====
+
+The artful methods generally come verbatim from NodeBox, a drawing kit for
+Python. In turn, NodeBox gets much of its ideas from Processing, a Java-like
+language for graphics and animation. I owe a great debt to the creators of
+these wonderful programs!
+
+Shoes does a few things differently from NodeBox and Processing. For example,
+Shoes has different color methods, including having its own Color objects,
+though these are very similar to Processing's color methods. And Shoes also
+allows images and gradients to be used for drawing lines and filling in shapes.
+
+Shoes also borrows some animation ideas from Processing and will continue to
+closely consult Processing's methods as it expands.
+
+=== arc(left, top, width, height, angle1, angle2) » Shoes::Shape ===
+
+Draws an arc shape (a section of an oval) at coordinates (left, top). This
+method just give you a bit more control than [[oval]], by offering the
+`:angle1` and `:angle2` styles. (In fact, you can mimick the `oval` method by
+setting `:angle1` to 0 and `:angle2` to `Shoes::TWO_PI`.)
+
+=== arrow(left, top, width) » Shoes::Shape ===
+
+Draws an arrow at coordinates (left, top) with a pixel `width`.
+
+=== cap(:curve or :rect or :project) » self ===
+
+Sets the line cap, which is the shape at the end of every line you draw. If
+set to `:curve`, the end is rounded. The default is `:rect`, a line which ends
+abruptly flat. The `:project` cap is also fat, but sticks out a bit longer.
+
+=== fill(pattern) » pattern ===
+
+Sets the fill bucket to a specific color (or pattern.) Patterns can be colors,
+gradients or images. So, once the fill bucket is set, you can draw shapes and
+they will be colored in with the pattern you've chosen.
+
+To draw a star with an image pattern:
+
+{{{
+ #!ruby
+ Shoes.app do
+ fill "static/avatar.png"
+ star 200, 200, 5
+ end
+}}}
+
+To clear the fill bucket, use `nofill`. And to set the line color (the border
+of the star,) use the `stroke` method.
+
+=== nofill() » self ===
+
+Blanks the fill color, so that any shapes drawn will not be filled in.
+Instead, shapes will have only a lining, leaving the middle transparent.
+
+=== nostroke() » self ===
+
+Empties the line color. Shapes drawn will have no outer line. If `nofill` is
+also set, shapes drawn will not be visible.
+
+=== line(left, top, x2, y2) » Shoes::Shape ===
+
+Draws a line using the current line color (aka "stroke") starting at
+coordinates (left, top) and ending at coordinates (x2, y2).
+
+=== oval(left, top, radius) » Shoes::Shape ===
+
+Draws a circular form at pixel coordinates (left, top) with a width and height
+of `radius` pixels. The line and fill colors are used to draw the shape. By
+default, the coordinates are for the oval's leftmost, top corner, but this can
+be changed by calling the [[Art.transform]] method or by using the `:center`
+style on the next method below.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stroke blue
+ strokewidth 4
+ fill black
+ oval 10, 10, 50
+ end
+}}}
+
+To draw an oval of varied proportions, you may also use the syntax: `oval(left, top, width, height)`.
+
+=== oval(styles) » Shoes::Shape ===
+
+Draw circular form using a style hash. The following styles are supported:
+
+ * `top`: the y-coordinate for the oval pen.
+ * `left`: the x-coordinate for the oval pen.
+ * `radius`: the width and height of the circle.
+ * `width`: a specific pixel width for the oval.
+ * `height`: a specific pixel height for the oval.
+ * `center`: do the coordinates specific the oval's center? (true or false)
+
+These styles may also be altered using the `style` method on the Shape object.
+
+=== rect(top, left, width, height, corners = 0) » Shoes::Shape ===
+
+Draws a rectangle starting from coordinates (top, left) with dimensions of
+width x height. Optionally, you may give the rectangle rounded corners with a
+fifth argument: the radius of the corners in pixels.
+
+As with all other shapes, the rectangle is drawn using the stroke and fill colors.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stroke rgb(0.5, 0.5, 0.7)
+ fill rgb(1.0, 1.0, 0.9)
+ rect 10, 10, self.width - 20, self.height - 20
+ end
+}}}
+
+The above sample draws a rectangle which fills the area of its parent box,
+leaving a margin of 10 pixels around the edge. Also see the `background`
+method for a rectangle which defaults to filling its parent box.
+
+=== rect(styles) » Shoes::Shape ===
+
+Draw a rectangle using a style hash. The following styles are supported:
+
+ * `top`: the y-coordinate for the rectangle.
+ * `left`: the x-coordinate for the rectangle.
+ * `curve`: the pixel radius of the rectangle's corners.
+ * `width`: a specific pixel width for the rectangle.
+ * `height`: a specific pixel height for the rectangle.
+ * `center`: do the coordinates specific the rectangle's center? (true or false)
+
+These styles may also be altered using the `style` method on the Shape object.
+
+=== rotate(degrees: a number) » self ===
+
+Rotates the pen used for drawing by a certain number of `degrees`, so that any
+shapes will be drawn at that angle.
+
+In this example below, the rectangle drawn at (30, 30) will be rotated 45 degrees.
+
+{{{
+ #!ruby
+ Shoes.app do
+ fill "#333"
+ rotate 45
+ rect 30, 30, 40, 40
+ end
+}}}
+
+=== shape(left, top) { ... } » Shoes::Shape ===
+
+Describes an arbitrary shape to draw, beginning at coordinates (left, top) and
+continued by calls to `line_to`, `move_to`, `curve_to` and `arc_to` inside the
+block. You can look at it as sketching a shape with a long line that curves
+and arcs and bends.
+
+{{{
+ #!ruby
+ Shoes.app do
+ fill red(0.2)
+ shape do
+ move_to(90, 55)
+ arc_to(50, 55, 50, 50, 0, PI/2)
+ arc_to(50, 55, 60, 60, PI/2, PI)
+ arc_to(50, 55, 70, 70, PI, TWO_PI-PI/2)
+ arc_to(50, 55, 80, 80, TWO_PI-PI/2, TWO_PI)
+ end
+ end
+}}}
+
+A shape can also contain other shapes. So, you can place an [[Art.oval]], a
+[[Art.rect]], a [[Art.line]], a [[Art.star]] or an [[Art.arrow]] (and all of
+the other methods in this [[Art]] section) inside a shape, but they will not be
+part of the line. They will be more like a group of shapes are all drawn as
+one.
+
+=== star(left, top, points = 10, outer = 100.0, inner = 50.0) » Shoes::Shape ===
+
+Draws a star using the stroke and fill colors. The star is positioned with its
+center point at coordinates (left, top) with a certain number of `points`. The
+`outer` width defines the full radius of the star; the `inner` width specifies
+the radius of the star's middle, where points stem from.
+
+=== stroke(pattern) » pattern ===
+
+Set the active line color for this slot. The `pattern` may be a color, a
+gradient or an image, all of which are categorized as "patterns." The line
+color is then used to draw the borders of any subsequent shape.
+
+So, to draw an arrow with a red line around it:
+
+{{{
+ #!ruby
+ Shoes.app do
+ stroke red
+ arrow 0, 100, 10
+ end
+}}}
+
+To clear the line color, use the `nostroke` method.
+
+=== strokewidth(a number) » self ===
+
+Sets the line size for all drawing within this slot. Whereas the `stroke`
+method alters the line color, the `strokewidth` method alters the line size in
+pixels. Calling `strokewidth(4)` will cause lines to be drawn 4 pixels wide.
+
+=== transform(:center or :corner) » self ===
+
+Should transformations (such as `skew` and `rotate`) be performed around the
+center of the shape? Or the corner of the shape? Shoes defaults to `:corner`.
+
+=== translate(left, top) » self ===
+
+Moves the starting point of the drawing pen for this slot. Normally, the pen
+starts at (0, 0) in the top-left corner, so that all shapes are drawn from that
+point. With `translate`, if the starting point is moved to (10, 20) and a
+shape is drawn at (50, 60), then the shape is actually drawn at (60, 80) on the
+slot.
+
+== Element Creation ==
+
+Shoes has a wide variety of elements, many cherry-picked from HTML. This page
+describes how to create these elements in a slot. See the Elements section of
+the manual for more on how to modify and use these elements after they have
+been placed.
+
+=== animate(fps) { |frame| ... } » Shoes::Animation ===
+
+Starts an animation timer, which runs parallel to the rest of the app. The
+`fps` is a number, the frames per seconds. This number dictates how many times
+per second the attached block will be called.
+
+The block is given a `frame` number. Starting with zero, the `frame` number
+tells the block how many frames of the animation have been shown.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @counter = para "STARTING"
+ animate(24) do |frame|
+ @counter.replace "FRAME #{frame}"
+ end
+ end
+}}}
+
+The above animation is shown 24 times per second. If no number is given, the
+`fps` defaults to 10.
+
+=== background(pattern) » Shoes::Background ===
+
+Draws a Background element with a specific color (or pattern.) Patterns can be
+colors, gradients or images. Colors and images will tile across the
+background. Gradients stretch to fill the background.
+
+'''PLEASE NOTE:''' Backgrounds are actual elements, not styles. HTML treats
+backgrounds like styles. Which means every box can only have one background.
+Shoes layers background elements.
+
+{{{
+ #!ruby
+ Shoes.app do
+ background black
+ background white, :width => 50
+ end
+}}}
+
+The above example paints two backgrounds. First, a black background is painted
+over the entire app's surface area. Then a 50 pixel white stripe is painted
+along the left side.
+
+=== banner(text) » Shoes::Banner ===
+
+Creates a Banner text block. Shoes automatically styles this text to 48 pixels high.
+
+=== border(text, :strokewidth => a number) » Shoes::Border ===
+
+Draws a Border element using a specific color (or pattern.) Patterns can be
+colors, gradients or images. Colors and images will tile across the border.
+Gradients stretch to fill the border.
+
+'''PLEASE NOTE:''' Like Backgrounds, Borders are actual elements, not styles.
+HTML treats backgrounds and borders like styles. Which means every box can
+only have one borders. Shoes layers border and background elements, along with
+text blocks, images, and everything else.
+
+=== button(text) { ... } » Shoes::Button ===
+
+Adds a push button with the message `text` written across its surface. An
+optional block can be attached, which is called if the button is pressed.
+
+=== caption(text) » Shoes::Caption ===
+
+Creates a Caption text block. Shoes styles this text to 14 pixels high.
+
+=== check() » Shoes::Check ===
+
+Adds a check box.
+
+=== code(text) » Shoes::Code ===
+
+Create a Code text fragment. This text defaults to a monospaced font.
+
+=== del(text) » Shoes::Del ===
+
+Creates a Del text fragment (short for "deleted") which defaults to text with a
+single strikethrough in its middle.
+
+=== dialog(styles) { ... } » Shoes::App ===
+
+Opens a new app window (just like the [[Element.window]] method does,) but the
+window is given a dialog box look.
+
+=== edit_box(text) » Shoes::EditBox ===
+
+Adds a large, multi-line textarea to this slot. The `text` is optional and
+should be a string that will start out the box. An optional block can be
+attached here which is called any type the user changes the text in the box.
+
+{{{
+ #!ruby
+ Shoes.app do
+ edit_box
+ edit_box "HORRAY EDIT ME"
+ edit_box "small one", :width => 100, :height => 160
+ end
+}}}
+
+=== edit_line(text) » Shoes::EditLine ===
+
+Adds a single-line text box to this slot. The `text` is optional and should be
+a string that will start out the box. An optional block can be attached here
+which is called any type the user changes the text in the box.
+
+=== em(text) » Shoes::Em ===
+
+Creates an Em text fragment (short for "emphasized") which, by default, is
+styled with italics.
+
+=== every(seconds) { |count| ... } » Shoes::Every ===
+
+A timer similar to the `animation` method, but much slower. This timer fires a
+given number of seconds, running the block attached. So, for example, if you
+need to check a web site every five minutes, you'd call `every(300)` with a
+block containing the code to actually ping the web site.
+
+=== flow(styles) { ... } » Shoes::Flow ===
+
+A flow is an invisible box (or "slot") in which you place Shoes elements. Both
+flows and stacks are explained in great detail on the main [[Slots]] page.
+
+Flows organize elements horizontally. Where one would use a [[Element.stack]]
+to keep things stacked vertically, a flow places its contents end-to-end across
+the page. Once the end of the page is reached, the flow starts a new line of
+elements.
+
+=== image(path) » Shoes::Image ===
+
+Creates an [[Image]] element for displaying a picture. PNG, JPEG and GIF
+formats are allowed.
+
+The `path` can be a file path or a URL. All images loaded are temporarily
+cached in memory, but remote images are also cached locally in the user's
+personal Shoes directory. Remote images are loaded in the background; as with
+browsers, the images will not appear right away, but will be shown when they
+are loaded.
+
+=== imagesize(path) » [width, height] ===
+
+Quickly grab the width and height of an image. The image won't be loaded into
+the cache or displayed.
+
+URGENT NOTE: This method cannot be used with remote images (loaded from HTTP,
+rather than the hard drive.)
+
+=== ins(text) » Shoes::Ins ===
+
+Creates an Ins text fragment (short for "inserted") which Shoes styles with a
+single underline.
+
+=== inscription(text) » Shoes::Inscription ===
+
+Creates an Inscription text block. Shoes styles this text at 10 pixels high.
+
+=== link(text, :click => proc or string) » Shoes::Link ===
+
+Creates a Link text block, which Shoes styles with a single underline and
+colors with a #06E (blue) colored stroke.
+
+The default LinkHover style is also single-underlined with a #039 (dark blue) stroke.
+
+=== list_box(:items => [strings, ...]) » Shoes::ListBox ===
+
+Adds a drop-down list box containing entries for everything in the `items`
+array. An optional block may be attached, which is called if anything in the
+box becomes selected by the user.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :margin => 10 do
+ para "Pick a card:"
+ list_box :items => ["Jack", "Ace", "Joker"]
+ end
+ end
+}}}
+
+Call `ListBox#text` to get the selected string. See the `ListBox` section
+under `Native` controls for more help.
+
+=== progress() » Shoes::Progress ===
+
+Adds a progress bar.
+
+=== para(text) » Shoes::Para ===
+
+Create a Para text block (short for "paragraph") which Shoes styles at 12
+pixels high.
+
+=== radio(group name: a string or symbol) » Shoes::Radio ===
+
+Adds a radio button. If a `group name` is given, the radio button is
+considered part of a group. Among radio buttons in the same group, only one
+may be checked. (If no group name is given, the radio button is grouped with
+any other radio buttons in the same slot.)
+
+=== span(text) » Shoes::Span ===
+
+Creates a Span text fragment, unstyled by default.
+
+=== stack(styles) { ... } » Shoes::Stack ===
+
+Creates a new stack. A stack is a type of slot. (See the main [[Slots]] page
+for a full explanation of both stacks and flows.)
+
+In short, stacks are an invisible box (a "slot") for placing stuff. As you add
+things to the stack, such as buttons or images, those things pile up
+vertically. Yes, they stack up!
+
+=== strong(text) » Shoes::Strong ===
+
+Creates a Strong text fragment, styled in bold by default.
+
+=== sub(text) » Shoes::Sub ===
+
+Creates a Sub text fragment (short for "subscript") which defaults to lowering
+the text by 10 pixels and styling it in an x-small font.
+
+=== subtitle(text) » Shoes::Subtitle ===
+
+Creates a Subtitle text block. Shoes styles this text to 26 pixels high.
+
+=== sup(text) » Shoes::Sup ===
+
+Creates a Sup text fragment (short for "superscript") which defaults to raising
+the text by 10 pixels and styling it in an x-small font.
+
+=== tagline(text) » Shoes::Tagline ===
+
+Creates a Tagline text block. Shoes styles this text to 18 pixels high.
+
+=== timer(seconds) { ... } » Shoes::Timer ===
+
+A one-shot timer. If you want to schedule to run some code in a few seconds
+(or minutes, hours) you can attach the code as a block here.
+
+To display an alert box five seconds from now:
+
+{{{
+ #!ruby
+ Shoes.app do
+ timer(5) do
+ alert("Your five seconds are up.")
+ end
+ end
+}}}
+
+=== title(text) » Shoes::Title ===
+
+Creates a Title text block. Shoes styles these elements to 34 pixels high.
+
+=== video(path or url) » Shoes::Video ===
+
+Embeds a movie in this slot.
+
+=== window(styles) { ... } » Shoes::App ===
+
+Opens a new app window. This method is almost identical to the
+[[App.Shoes.app]] method used to start an app in the first place. The
+difference is that the `window` method sets the new window's [[App.owner]]
+property. (A normal Shoes.app has its `owner` set to `nil`.)
+
+So, the new window's `owner` will be set to the Shoes::App which launched the
+window. This way the child window can call the parent.
+
+{{{
+ #!ruby
+ Shoes.app :title => "The Owner" do
+ button "Pop up?" do
+ window do
+ para "Okay, popped up from #{owner}"
+ end
+ end
+ end
+}}}
+
+== Events ==
+
+Wondering how to catch stray mouse clicks or keyboard typing? Events are sent
+to a slot whenever a mouse moves inside the slot. Or whenever a key is
+pressed. Even when the slot is created or destroyed. You can attach a block
+to each of these events.
+
+Mouse events include `motion`, `click`, `hover` and `leave`. Keyboard typing
+is represented by the `keypress` event. And the `start` and `finish` events
+indicate when a canvas comes into play or is discarded.
+
+So, let's say you want to change the background of a slot whenever the mouse
+floats over it. We can use the `hover` event to change the background when the
+mouse comes inside the slot. And `leave` to change back when the mouse floats
+away.
+
+{{{
+ #!ruby
+ Shoes.app do
+ s = stack :width => 200, :height => 200 do
+ background red
+ hover do
+ s.clear { background blue }
+ end
+ leave do
+ s.clear { background red }
+ end
+ end
+ end
+}}}
+
+=== click { |button, left, top| ... } » self ===
+
+The click block is called when a mouse button is clicked. The `button` is the
+number of the mouse button which has been pressed. The `left` and `top` are
+the mouse coordinates at which the click happened.
+
+To catch the moment when the mouse is unclicked, see the [[Events.release]] event.
+
+=== finish { |self| ... } » self ===
+
+When a slot is removed, it's finish event occurs. The finish block is
+immediately handed `self`, the slot object which has been removed.
+
+=== hover { |self| ... } » self ===
+
+The hover event happens when the mouse enters the slot. The block gets `self`,
+meaning the object which was hovered over.
+
+To catch the mouse exiting the slot, check out the [[Events.leave]] event.
+
+=== keypress { |key| ... } » self ===
+
+Whenever a key (or combination of keys) is pressed, the block gets called. The
+block is sent a `key` which is a string representing the character (such as the
+letter or number) on the key. For special keys and key combos, a Ruby symbol
+is sent, rather than a string.
+
+So, for example, if `Shift-a` is pressed, the block will get the string `"A"`.
+
+However, if the F1 key is pressed, the `:f1` symbol is received. For
+`Shift-F1`, the symbol would be `:shift_f1`.
+
+The modifier keys are `control`, `shift` and `alt`. They appear in that order.
+If `Shift-Control-Alt-PgUp` is pressed, the symbol will be
+`:control_shift_alt_page_up`.
+
+One thing about the shift key. You won't see the shift key on most keys. On
+US keyboards, `Shift-7` is an ampersand. So you'll get the string `"&"` rather
+than `:shift_5`. And, if you press `Shift-Alt-7` on such a keyboard, you'll
+get the symbol: `:alt_&`. You'll only see the shift modifier on the special
+keys listed a few paragraphs down.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @info = para "NO KEY is PRESSED."
+ keypress do |k|
+ @info.replace "#{k.inspect} was PRESSED."
+ end
+ end
+}}}
+
+Keep in mind that Shoes itself uses a few hotkeys. Alt-Period (`:alt_.`),
+Alt-Question (`:alt_?`) and Alt-Slash (`:alt_/`) are reserved for Shoes.
+
+The list of special keys is as follows: `:escape`, `:delete`, `:backspace`,
+`:tab`, `:page_up`, `:page_down`, `:home`, `:end`, `:left`, `:up`, `:right`,
+`:down`, `:f1`, `:f2`, `:f3`, `:f4`, `:f5`, `:f6`, `:f7`, `:f8`, `:f9`, `:f10`,
+`:f11` and `:f12`.
+
+One caveat to all of those rules: normally the Return key gives you a string
+`"\n"`. When pressed with modifier keys, however, you end up with
+`:control_enter`, `:control_alt_enter`, `:shift_alt_enter` and the like.
+
+=== leave { |self| ... } » self ===
+
+The leave event takes place when the mouse cursor exits a slot. The moment it
+no longer is inside the slot's edges. When that takes place, the block is
+called with `self`, the slot object which is being left.
+
+Also see [[Events.hover]] if you'd like to detect the mouse entering a slot.
+
+=== motion { |left, top| ... } » self ===
+
+The motion block gets called every time the mouse moves around inside the slot.
+The block is handed the cursor's `left` and `top` coordinates.
+
+{{{
+ #!ruby
+ Shoes.app :width => 200, :height => 200 do
+ background black
+ fill white
+ @circ = oval 0, 0, 100, 100
+
+ motion do |top, left|
+ @circ.move top - 50, left - 50
+ end
+ end
+}}}
+
+=== release { |button, left, top| ... } » self ===
+
+The release block runs whenever the mouse is unclicked (on mouse up). When the
+finger is lifted. The `button` is the number of the button that was depressed.
+The `left` and `top` are the coordinates of the mouse at the time the button
+was released.
+
+To catch the actual mouse click, use the [[Events.click]] event.
+
+=== start { |self| ... } » self ===
+
+The first time the slot is drawn, the start event fires. The block is handed
+`self`, the slot object which has just been drawn.
+
+== Manipulation Blocks ==
+
+The manipulation methods below make quick work of shifting around slots and
+inserting new elements.
+
+=== append() { ... } » self ===
+
+Adds elements to the end of a slot.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @slot = stack { para 'Good Morning' }
+ timer 3 do
+ @slot.append do
+ title "Breaking News"
+ tagline "Astronauts arrested for space shuttle DUI."
+ end
+ end
+ end
+}}}
+
+The `title` and `tagline` elements will be added to the end of the `@slot`.
+
+=== after(element) { ... } » self ===
+
+Adds elements to a specific place in a slot, just after the `element` which is
+a child of the slot.
+
+=== before(element) { ... } » self ===
+
+Adds elements to a specific place in a slot, just before the `element` which is
+a child of the slot.
+
+=== clear() » self ===
+
+Empties the slot of any elements, timers and nested slots. This is effectively
+identical to looping through the contents of the slot and calling each
+element's `remove` method.
+
+=== clear() { ... } » self ===
+
+The clear method also takes an optional block. The block will be used to
+replace the contents of the slot.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @slot = stack { para "Old text" }
+ timer 3 do
+ @slot.clear { para "Brand new text" }
+ end
+ end
+}}}
+
+In this example, the "Old text" paragraph will be cleared out, replaced by the
+"Brand new text" paragraph.
+
+=== prepend() { ... } » self ===
+
+Adds elements to the beginning of a slot.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @slot = stack { para 'Good Morning' }
+ timer 3 do
+ @slot.prepend { para "Your car is ready." }
+ end
+ end
+}}}
+
+The `para` element is added to the beginning of the `@slot`.
+
+== Position of a Slot ==
+
+Like any other element, slots can be styled and customized when they are created.
+
+To set the width of a stack to 150 pixels:
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack(:width => 150) { para "Now that's precision." }
+ end
+}}}
+
+Each style setting also has a method, which can be used to grab that particular
+setting. (So, like, the `width` method returns the width of the slot in
+pixels.)
+
+=== displace(left: a number, top: a number) » self ===
+
+A shortcut method for setting the :displace_left and :displace_top styles.
+Displacing is a handy way of moving a slot without altering the layout. In
+fact, the `top` and `left` methods will not report displacement at all. So,
+generally, displacement is only for temporary animations. For example,
+jiggling a button in place.
+
+The `left` and `top` numbers sent to `displace` are added to the slot's own
+top-left coordinates. To subtract from the top-left coordinate, use negative
+numbers.
+
+=== gutter() » a number ===
+
+The size of the scrollbar area. When Shoes needs to show a scrollbar, the
+scrollbar may end up covering up some elements that touch the edge of the
+window. The `gutter` tells you how many pixels to expect the scrollbar to
+cover.
+
+This is commonly used to pad elements on the right, like so:
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :margin_right => 20 + gutter do
+ para "Insert fat and ratified declaration of independence here..."
+ end
+ end
+}}}
+
+=== height() » a number ===
+
+The vertical size of the viewable slot in pixels. So, if this is a scrolling
+slot, you'll need to use `scroll_height()` to get the full size of the slot.
+
+=== hide() » self ===
+
+Hides the slot, so that it can't be seen. See also [[Position.show]] and [[Position.toggle]].
+
+=== left() » a number ===
+
+The left pixel location of the slot. Also known as the x-axis coordinate.
+
+=== move(left, top) » self ===
+
+Moves the slot to specific coordinates, the (left, top) being the upper left
+hand corner of the slot.
+
+=== remove() » self ===
+
+Removes the slot. It will no longer be displayed and will not be listed in its
+parent's contents. It's gone.
+
+=== scroll() » true or false ===
+
+Is this slot allowed to show a scrollbar? True or false. The scrollbar will
+only appear if the height of the slot is also fixed.
+
+=== scroll_height() » a number ===
+
+The vertical size of the full slot, including any of it which is hidden by scrolling.
+
+=== scroll_max() » a number ===
+
+The top coordinate which this slot can be scrolled down to. The top coordinate
+of a scroll bar is always zero. The bottom coordinate is the full height of
+the slot minus one page of scrolling. This bottom coordinate is what
+`scroll_max` returns.
+
+This is basically a shortcut for writing `slot.scroll_height - slot.height`.
+
+To scroll to the bottom of a slot, use `slot.scroll_top = slot.scroll_max`.
+
+=== scroll_top() » a number ===
+
+The top coordinate which this slot is scrolled down to. So, if the slot is
+scrolled down twenty pixels, this method will return `20`.
+
+=== scroll_top = a number ===
+
+Scrolls the slot to a certain coordinate. This must be between zero and
+`scroll_max`.
+
+=== show() » self ===
+
+Reveals the slot, if it is hidden. See also [[Position.hide]] and
+[[Position.toggle]].
+
+=== style() » styles ===
+
+Calling the `style` method with no arguments returns a hash of the styles
+presently applied to this slot.
+
+While methods such as `height` and `width` return the true pixel dimensions of
+the slot, you can use `style[:height]` or `style[:width]` to get the dimensions
+originally requested.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @s = stack :width => "100%"
+ para @s.style[:width]
+ end
+}}}
+
+In this example, the paragraph under the stack will display the string "100%".
+
+=== style(styles) » styles ===
+
+Alter the slot using a hash of style settings. Any of the methods on this page
+(aside from this method, of course) can be used as a style setting. So, for
+example, there is a `width` method, thus there is also a `width` style.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @s = stack { background green }
+ @s.style(:width => 400, :height => 200)
+ end
+}}}
+
+=== toggle() » self ===
+
+Hides the slot, if it is shown. Or shows the slot, if it is hidden.
+
+=== top() » a number ===
+
+The top pixel location of the slot. Also known as the y-axis coordinate.
+
+=== width() » a number ===
+
+The horizontal size of the slot in pixels.
+
+== Traversing the Page ==
+
+You may find yourself needing to loop through the elements inside a slot. Or
+maybe you need to climb the page, looking for a stack that is the parent of an
+element.
+
+On any element, you may call the `parent` method to get the slot directly above
+it. And on slots, you can call the `contents` method to get all of the
+children. (Some elements, such as text blocks, also have a `contents` method
+for getting their children.)
+
+=== contents() » an array of elements ===
+
+Lists all elements in a slot.
+
+=== parent() » a Shoes::Stack or Shoes::Flow ===
+
+Gets the object for this element's container.
+
+= Elements =
+
+Ah, here's the stuff of Shoes. An element can be as simple as an oval shape. Or
+as complex as a video stream. You've encountered all of these elements before
+in the Slots section of th manual.
+
+Shoes has seven native controls: the Button, the EditLine, the EditBox, the
+ListBox, the Progress meter, the Check box and the Radio. By "native"
+controls, we mean that each of these seven elements is drawn by the operating
+system. So, a Progress bar will look one way on Windows and another way on OS
+X.
+
+Shoes also has seven basic other types of elements: Background, Border, Image,
+Shape, TextBlock, Timer and Video. These all should look and act the same on
+every operating system.
+
+Once an element is created, you will often still want to change it. To move it
+or hide it or get rid of it. You'll use the commands in this section to do that
+sort of stuff. (Especially check out the [[Common Common Methods]] section for
+commands you can use on any element.)
+
+So, for example, use the `image` method of a Slot to place a PNG on the screen.
+The `image` method gives you back an Image object. Use the methods of the Image
+object to change things up.
+
+== Common Methods ==
+
+A few methods are shared by every little element in Shoes. Moving, showing,
+hiding. Removing an element. Basic and very general things. This list
+encompasses those common commands.
+
+One of the most general methods of all is the `style` method (which is also
+covered as the [[Position.style]] method for slots.)
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ # Background, text and a button: both are elements!
+ @back = background green
+ @text = banner "A Message for You, Rudy"
+ @press = button "Stop your messin about!"
+
+ # And so, both can be styled.
+ @text.style :size => 12, :stroke => red, :margin => 10
+ @press.style :width => 400
+ @back.style :height => 10
+ end
+ end
+}}}
+
+For specific commands, see the other links to the left in the Elements section.
+Like if you want to pause or play a video file, check the [[Video]] section,
+since pausing and playing is peculiar to videos. No sense pausing a button.
+
+=== displace(left: a number, top: a number) » self ===
+
+Displacing an element moves it. But without changing the layout around it.
+This is great for subtle animations, especially if you want to reserve a place
+for an element while it is still animating. Like maybe a quick button shake or
+a slot sliding into view.
+
+When you displace an element, it moves relative to the upper-left corner where
+it was placed. So, if an element is at the coordinates (20, 40) and you
+displace it 2 pixels left and 6 pixels on top, you end up with the coordinates
+(22, 46).
+
+{{{
+ #!ruby
+ Shoes.app do
+ flow :margin => 12 do
+ # Set up three buttons
+ button "One"
+ @two = button "Two"
+ button "Three"
+
+ # Bounce the second button
+ animate do |i|
+ @two.displace(0, (Math.sin(i) * 6).to_i)
+ end
+ end
+ end
+}}}
+
+Notice that while the second button bounces, the other two buttons stay put.
+If we used a normal `move` in this situation, the second button would be moved
+out of the layout and the buttons would act as if the second button wasn't
+there at all. (See the [[Common.move]] example.)
+
+'''Of particular note:''' if you use the `left` and `top` methods to get the
+coordinates of a displaced element, you'll just get back the normal
+coordinates. As if there was no displacement. Displacing is just intended for
+quick animations!
+
+=== height() » a number ===
+
+The vertical screen size of the element in pixels. In the case of images, this
+is not the full size of the image. This is the height of the element as it is
+shown right now.
+
+If you have a 150x150 pixel image and you set the width to 50 pixels, this
+method will return 50.
+
+Also see the [[Common.width]] method for an example and some other comments.
+
+=== hide() » self ===
+
+Hides the element, so that it can't be seen. See also [[Common.show]] and
+[[Common.toggle]].
+
+=== left() » a number ===
+
+Gets you the pixel position of the left edge of the element.
+
+=== move(left: a number, top: a number) » self ===
+
+Moves the element to a specific pixel position within its slot. The element is
+still inside the slot. But it will no longer be stacked or flowed in with the
+other stuff in the slot. The element will float freely, now absolutely
+positioned instead.
+
+{{{
+ #!ruby
+ Shoes.app do
+ flow :margin => 12 do
+ # Set up three buttons
+ button "One"
+ @two = button "Two"
+ button "Three"
+
+ # Bounce the second button
+ animate do |i|
+ @two.move(40, 40 + (Math.sin(i) * 6).to_i)
+ end
+ end
+ end
+}}}
+
+The second button is moved to a specific place, allowing the third button to
+slide over into its place. If you want to move an element without shifting
+other pieces, see the [[Common.displace]] method.
+
+=== parent() » a Shoes::Stack or Shoes::Flow ===
+
+Gets the object for this element's container. Also see the slot's
+[[Traversing.contents]] to do the opposite: get a container's elements.
+
+=== remove() » self ===
+
+Removes the element from its slot. (In other words: throws it in the garbage.)
+The element will no longer be displayed.
+
+=== show() » self ===
+
+Reveals the element, if it is hidden. See also [[Common.hide]] and
+[[Common.toggle]].
+
+=== style() » styles ===
+
+Gives you the full set of styles applied to this element, in the form of a
+Hash. While methods like `width` and `height` and `top` give you back specific
+pixel dimensions, using `style[:width]` or `style[:top]`, you can get the
+original setting (things like "100%" for width or "10px" for top.)
+
+{{{
+ #!ruby
+ Shoes.app do
+ # A button which take up the whole page
+ @b = button "All of it", :width => 1.0, :height => 1.0
+
+ # When clicked, show the styles
+ @b.click { alert(@b.style.inspect) }
+ end
+}}}
+
+=== style(styles) » styles ===
+
+Changes the style of an element. This could include the `:width` and `:height`
+of an element, the font `:size` of some text, the `:stroke` and `:fill` of a
+shape. Or any other number of style settings.
+
+=== toggle() » self ===
+
+Hides an element if it is shown. Or shows the element, if it is hidden.
+
+=== top() » a number ===
+
+Gets the pixel position of the top edge of the element.
+
+=== width() » a number ===
+
+Gets the pixel width for the full size of the element. This method always
+returns an exact pixel size. In the case of images, this is not the full width
+of the image, just the size it is shown at. See the [[Common.height]] method
+for more.
+
+Also, if you create an element with a width of 100% and that element is inside
+a stack which is 120 pixels wide, you'll get back `120`. However, if you call
+`style[:width]`, you'll get `"100%"`.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :width => 120 do
+ @b = button "Click me", :width => "100%" do
+ alert "button.width = #{@b.width}\n" +
+ "button.style[:width] = #{@b.style[:width]}"
+ end
+ end
+ end
+}}}
+
+In order to set the width, you'll have to go through the [[Common.style]]
+method again. So, to set the button to 150 pixels wide: `@b.style(:width =>
+150)`.
+
+To let Shoes pick the element's width, go with `@b.style(:width => nil)` to
+empty out the setting.
+
+== Background ==
+
+A background is a color, a gradient or an image that is painted across an
+entire slot. Both backgrounds and borders are a type of Shoes::Pattern.
+!{:margin_left => 100}man-ele-background.png!
+
+Even though it's called a ''background'', you may still place this element in
+front of other elements. If a background comes after something else painted on
+the slot (like a `rect` or an `oval`,) the background will be painted over that
+element.
+
+The simplest background is just a plain color background, created with the
+[[Element.background]] method, such as this black background:
+
+{{{
+ #!ruby
+ Shoes.app do
+ background black
+ end
+}}}
+
+A simple background like that paints the entire slot that contains it. (In
+this case, the whole window is painted black.)
+
+You can use styles to cut down the size or move around the background to your liking.
+
+To paint a black background across the top fifty pixels of the window:
+
+{{{
+ #!ruby
+ Shoes.app do
+ background black, :height => 50
+ end
+}}}
+
+Or, to paint a fifty pixel column on the right-side of the window:
+
+{{{
+ #!ruby
+ Shoes.app do
+ background black, :width => 50, :right => 50
+ end
+}}}
+
+Since Backgrounds are normal elements as well, see also the start of the
+[[Elements]] section for all of its other methods.
+
+=== to_pattern() » a Shoes::Pattern ===
+
+Yanks out the color, gradient or image used to paint this background and places
+it in a normal Shoes::Pattern object. You can then pass that object to other
+backgrounds and borders. Reuse it as you like.
+
+== Border ==
+
+A border is a color, gradient or image painted in a line around the edge of any
+slot. Like the Background element in the last section, a Border is a kind of
+Shoes::Pattern. !{:margin_left => 100}man-ele-border.png!
+
+The first, crucial thing to know about border is that all borders paint a line
+around the '''inside''' of a slot, not the outside. So, if you have a slot
+which is fifty pixels wide and you paint a five pixel border on it, that means
+there is a fourty pixel wide area inside the slot which is surrounded by the
+border.
+
+This also means that if you paint a Border on top of a [[Background]], the
+edges of the background will be painted over by the border.
+
+Here is just such a slot:
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :width => 50 do
+ border black, :strokewidth => 5
+ para "=^.^=", :stroke => green
+ end
+ end
+}}}
+
+If you want to paint a border around the outside of a slot, you'll need to wrap
+that slot in another slot. Then, place the border in the outside slot.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :width => 60 do
+ border black, :strokewidth => 5
+ stack :width => 50 do
+ para "=^.^=", :stroke => green
+ end
+ end
+ end
+}}}
+
+In HTML and many other languages, the border is painted on the outside of the
+box, thus increasing the overall width of the box. Shoes was designed with
+consistency in mind, so that if you say that a box is fifty pixels wide, it
+stays fifty pixels wide regardless of its borders or margins or anything else.
+
+Please also check out the [[Elements]] section for other methods used on borders.
+
+=== to_pattern() » a Shoes::Pattern ===
+
+Creates a basic pattern object based on the color, gradient or image used to
+paint this border. The pattern may then be re-used in new borders and
+backgrounds.
+
+== Button ==
+
+Buttons are, you know, push buttons. You click them and they do something.
+Buttons are known to say "OK" or "Are you sure?" And, then, if you're sure,
+you click the button. !{:margin_left => 100}man-ele-button.png!
+
+{{{
+ #!ruby
+ Shoes.app do
+ button "OK!"
+ button "Are you sure?"
+ end
+}}}
+
+The buttons in the example above don't do anything when you click them. In
+order to get them to work, you've got to hook up a block to each button.
+
+{{{
+ #!ruby
+ Shoes.app do
+ button "OK!" do
+ append { para "Well okay then." }
+ end
+ button "Are you sure?" do
+ append { para "Your confidence is inspiring." }
+ end
+ end
+}}}
+
+So now we've got blocks for the buttons. Each block appends a new paragraph to
+the page. The more you click, the more paragraphs get added.
+
+It doesn't go much deeper than that. A button is just a clickable phrase.
+
+Just to be pedantic, though, here's another way to write that last example.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @b1 = button "OK!"
+ @b1.click { para "Well okay then." }
+ @b2 = button "Are you sure?"
+ @b2.click { para "Your confidence is inspiring." }
+ end
+}}}
+
+This looks dramatically different, but it does the same thing. The first
+difference: rather than attaching the block directly to the button, the block
+is attached later, through the `click` method.
+
+The second change isn't related to buttons at all. The `append` block was
+dropped since Shoes allows you to add new elements directly to the slot. So we
+can just call `para` directly. (This isn't the case with the `prepend`,
+`before` or `after` methods.)
+
+Beside the methods below, buttons also inherit all of the methods that are
+[[Common]].
+
+=== click() { |self| ... } » self ===
+
+When a button is clicked, its `click` block is called. The block is handed
+`self`. Meaning: the button which was clicked.
+
+=== focus() » self ===
+
+Moves focus to the button. The button will be highlighted and, if the user
+hits Enter, the button will be clicked.
+
+== Check ==
+
+Check boxes are clickable square boxes than can be either checked or unchecked.
+A single checkbox usually asks a "yes" or "no" question. Sets of checkboxes
+are also seen in to-do lists. !{:margin_left => 100}man-ele-check.png!
+
+Here's a sample checklist.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ flow { check; para "Frances Johnson" }
+ flow { check; para "Ignatius J. Reilly" }
+ flow { check; para "Winston Niles Rumfoord" }
+ end
+ end
+}}}
+
+You basically have two ways to use a check. You can attach a block to the
+check and it'll get called when the check gets clicked. And/or you can just
+use the `checked?` method to go back and see if a box has been checked or not.
+
+Okay, let's add to the above example.
+
+{{{
+ #!ruby
+ Shoes.app do
+ @list = ['Frances Johnson', 'Ignatius J. Reilly',
+ 'Winston Niles Rumfoord']
+
+ stack do
+ @list.map! do |name|
+ flow { @c = check; para name }
+ [@c, name]
+ end
+
+ button "What's been checked?" do
+ selected = @list.map { |c, name| name if c.checked? }.compact
+ alert("You selected: " + selected.join(', '))
+ end
+ end
+ end
+}}}
+
+So, when the button gets pressed, each of the checks gets asked for its status,
+using the `checked?` method.
+
+Button methods are listed below, but also see the list of [[Common]] methods,
+which all elements respond to.
+
+=== checked?() » true or false ===
+
+Returns whether the box is checked or not. So, `true` means "yes, the box is checked!"
+
+=== checked = true or false ===
+
+Marks or unmarks the check box. Using `checked = false`, for instance,
+unchecks the box.
+
+=== click() { |self| ... } » self ===
+
+When the check is clicked, its `click` block is called. The block is handed
+`self`, which is the check object which was clicked.
+
+Clicks are sent for both checking and unchecking the box.
+
+=== focus() » self ===
+
+Moves focus to the check. The check will be highlighted and, if the user hits
+Enter, the check will be toggled between its checked and unchecked states.
+
+== EditBox ==
+
+Edit boxes are wide, rectangular boxes for entering text. On the web, they
+call these textareas. These are multi-line edit boxes for entering longer
+descriptions. Essays, even! !{:margin_left => 100}man-ele-editbox.png!
+
+Without any other styling, edit boxes are sized 200 pixels by 108 pixels. You
+can also use `:width` and `:height` styles to set specific sizes.
+
+{{{
+ #!ruby
+ Shoes.app do
+ edit_box
+ edit_box :width => 100, :height => 100
+ end
+}}}
+
+Other controls (like [[Button]] and [[Check]]) have only click events, but both
+[[EditLine]] and EditBox have a `change` event. The `change` block is called
+every time someone types into or deletes from the box.
+
+{{{
+ #!ruby
+ Shoes.app do
+ edit_box do |e|
+ @counter.text = e.text.size
+ end
+ @counter = strong("0")
+ para @counter, " characters"
+ end
+}}}
+
+Notice that the example also uses the [[EditBox.text]] method inside the block.
+That method gives you a string of all the characters typed into the box.
+
+More edit box methods are listed below, but also see the list of [[Common]]
+methods, which all elements respond to.
+
+=== change() { |self| ... } » self ===
+
+Each time a character is added to or removed from the edit box, its `change`
+block is called. The block is given `self`, which is the edit box object which
+has changed.
+
+=== focus() » self ===
+
+Moves focus to the edit box. The edit box will be highlighted and the user will
+be able to type into the edit box.
+
+=== text() » self ===
+
+Return a string of characters which have been typed into the box.
+
+=== text = a string ===
+
+Fills the edit box with the characters of `a string`.
+
+== EditLine ==
+
+Edit lines are a slender, little box for entering text. While the EditBox is
+multi-line, an edit line is just one. Line, that is. Horizontal, in fact.
+!{:margin_left => 100}man-ele-editline.png!
+
+The unstyled edit line is 200 pixels wide and 28 pixels wide. Roughly. The
+height may vary on some platforms.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ edit_line
+ edit_line :width => 400
+ end
+ end
+}}}
+
+You can change the size by styling both the `:width` and the `:height`.
+However, you generally only want to style the `:width`, as the height will be
+sized to fit the font. (And, in current versions of Shoes, the font for edit
+lines and edit boxes cannot be altered anyway.)
+
+If a block is given to an edit line, it receives `change` events. Check out the
+[[EditBox]] page for an example of using a change block. In fact, the edit box
+has all the same methods as an edit line. Also see the list of [[Common]]
+methods, which all elements respond to.
+
+=== change() { |self| ... } » self ===
+
+Each time a character is added to or removed from the edit line, its `change`
+block is called. The block is given `self`, which is the edit line object which
+has changed.
+
+=== focus() » self ===
+
+Moves focus to the edit line. The edit line will be highlighted and the user
+will be able to type into the edit line.
+
+=== text() » self ===
+
+Return a string of characters which have been typed into the box.
+
+=== text = a string ===
+
+Fills the edit line with the characters of `a string`.
+
+== Image ==
+
+An image is a picture in PNG, JPEG or GIF format. Shoes can resize images or
+flow them in with text. Images can be loaded from a file or directly off the
+web. !{:margin_left => 100}man-ele-image.png!
+
+To create an image, use the `image` method in a slot:
+
+{{{
+ #!ruby
+ Shoes.app do
+ para "Nice, nice, very nice. Busy, busy, busy."
+ image "static/shoes-manual-apps.gif"
+ end
+}}}
+
+When you load any image into Shoes, it is cached in memory. This means that if
+you load up many image elements from the same file, it'll only really load the
+file once.
+
+You can use web URLs directly as well.
+
+{{{
+ #!ruby
+ Shoes.app do
+ image "http://hacketyhack.heroku.com/images/logo.png"
+ end
+}}}
+
+When an image is loaded from the web, it's cached on the hard drive as well as
+in memory. This prevents a repeat download unless the image has changed. (In
+case you're wondering: Shoes keeps track of modification times and etags just
+like a browser would.)
+
+Shoes also loads remote images in the background using system threads. So,
+using remote images will not block Ruby or any intense graphical displays you
+may have going on.
+
+=== full_height() » a number ===
+
+The full pixel height of the image. Normally, you can just use the
+[[Common.height]] method to figure out how many pixels high the image is. But
+if you've resized the image or styled it to be larger or something, then
+`height` will return the scaled size.
+
+The `full_height` method gives you the height of image (in pixels) as it was
+stored in the original file.
+
+=== full_width() » a number ===
+
+The full pixel width of the image. See the [[Image.full_height]] method for an
+explanation of why you might use this method rather than [[Common.width]].
+
+=== path() » a string ===
+
+The URL or file name of the image.
+
+=== path = a string ===
+
+Swaps the image with a different one, loaded from a file or URL.
+
+== ListBox ==
+
+List boxes (also called "combo boxes" or "drop-down boxes" or "select boxes" in
+some places) are a list of options that drop down when you click on the box.
+!{:margin_left => 100}man-ele-listbox.png!
+
+A list box gets its options from an array. An array (a list) of strings,
+passed into the `:items` style.
+
+{{{
+ #!ruby
+ Shoes.app do
+ para "Choose a fruit:"
+ list_box :items => ["Grapes", "Pears", "Apricots"]
+ end
+}}}
+
+So, the basic size of a list box is about 200 pixels wide and 28 pixels high.
+You can adjust this length using the `:width` style.
+
+{{{
+ #!ruby
+ Shoes.app do
+ para "Choose a fruit:"
+ list_box :items => ["Grapes", "Pears", "Apricots"],
+ :width => 120, :choose => "Apricots" do |list|
+ @fruit.text = list.text
+ end
+
+ @fruit = para "No fruit selected"
+ end
+}}}
+
+Next to the `:width` style, the example uses another useful option. The
+`:choose` option tells the list box which of the items should be highlighted
+from the beginning. (There's also a [[ListBox.choose]] method for highlighting
+an item after the box is created.)
+
+List boxes also have a [[ListBox.change]] event. In the last example, we've got
+a block hooked up to the list box. Well, okay, see, that's a `change` block.
+The block is called each time someone changes the selected item.
+
+Those are the basics. Might you also be persuaded to look at the [[Common]]
+methods page, a complete list of the methods that all elements have?
+
+=== change() { |self| ... } » self ===
+
+Whenever someone highlights a new option in the list box (by clicking on an
+item, for instance,) its `change` block is called. The block is given `self`,
+which is the list box object which has changed.
+
+=== choose(item: a string) » self ===
+
+Selects the option in the list box that matches the string given by `item`.
+
+=== focus() » self ===
+
+Moves focus to the list box. The list box will be highlighted and, if the user
+hits the up and down arrow keys, other options in the list will be selected.
+
+=== items() » an array of strings ===
+
+Returns the complete list of strings that the list box presently shows as its options.
+
+=== items = an array of strings ===
+
+Replaces the list box's options with a new list of strings.
+
+=== text() » a string ===
+
+A string containing whatever text is shown highlighted in the list box right
+now. If nothing is selected, `nil` will be the reply.
+
+== Progress ==
+
+Progress bars show you how far along you are in an activity. Usually, a
+progress bar represents a percentage (from 0% to 100%.) Shoes thinks of
+progress in terms of the decimal numbers 0.0 to 1.0. !{:margin_left =>
+100}man-ele-progress.png!
+
+A simple progress bar is 200 pixels wide, but you can use the `:width` style
+(as with all Shoes elements) to lengthen it.
+
+{{{
+ Shoes.app do
+ stack :margin => 0.1 do
+ title "Progress example"
+ @p = progress :width => 1.0
+
+ animate do |i|
+ @p.fraction = (i % 100) / 100.0
+ end
+ end
+ end
+}}}
+
+Take a look at the [[Common]] methods page for a list of methods found an all
+elements, including progress bars.
+
+=== fraction() » a decimal number ===
+
+Returns a decimal number from 0.0 to 1.0, indicating how far along the progress bar is.
+
+=== fraction = a decimal number ===
+
+Sets the progress to a decimal number between 0.0 and 1.0.
+
+== Radio ==
+
+Radio buttons are a group of clickable circles. Click a circle and it'll be
+marked. Only one radio button can be marked at a time. (This is similar to the
+ListBox, where only one option can be selected at a time.) !{:margin_left =>
+100}man-ele-radio.png!
+
+So, how do you decide when to use radio buttons and when to use list boxes?
+Well, list boxes only show one highlighted item unless you click on the box and
+the drop-down appears. But radio buttons are all shown, regardless of which is
+marked.
+
+{{{
+ #!ruby
+ Shoes.app do
+ para "Among these films, which do you prefer?\n"
+ radio; para strong("The Taste of Tea"), " by Katsuhito Ishii\n"
+ radio; para strong("Kin-Dza-Dza"), " by Georgi Danelia\n"
+ radio; para strong("Children of Heaven"), " by Majid Majidi\n"
+ end
+}}}
+
+Only one of these three radios can be checked at a time, since they are grouped
+together in the same slot (along with a bunch of `para`.)
+
+If we move them each into their own slot, the example breaks.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ para "Among these films, which do you prefer?"
+ flow { radio; para "The Taste of Tea by Katsuhito Ishii" }
+ flow { radio; para "Kin-Dza-Dza by Georgi Danelia" }
+ flow { radio; para "Children of Heaven by Majid Majidi" }
+ end
+ end
+}}}
+
+This can be fixed, though. You can group together radios from different slots,
+you just have to give them all the same group name.
+
+Here, let's group all these radios in the `:films` group.
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ para "Among these films, which do you prefer?"
+ flow do
+ radio :films
+ para "The Taste of Tea by Katsuhito Ishii"
+ end
+ flow do
+ radio :films
+ para "Kin-Dza-Dza by Georgi Danelia"
+ end
+ flow do
+ radio :films
+ para "Children of Heaven by Majid Majidi"
+ end
+ end
+ end
+}}}
+
+For more methods beyond those listed below, also look into the [[Common]]
+methods page. Because you get those methods on every radio as well.
+
+=== checked?() » true or false ===
+
+Returns whether the radio button is checked or not. So, `true` means "yes, it
+is checked!"
+
+=== checked = true or false ===
+
+Marks or unmarks the radio button. Using `checked = false`, for instance,
+clears the radio.
+
+=== click() { |self| ... } » self ===
+
+When the radio button is clicked, its `click` block is called. The block is
+handed `self`, which is an object representing the radio which was clicked.
+
+Clicks are sent for both marking and unmarking the radio.
+
+=== focus() » self ===
+
+Moves focus to the radio. The radio will be highlighted and, if the user hits
+Enter, the radio will be toggled between its marked and unmarked states.
+
+== Shape ==
+
+A shape is a path outline usually created by drawing methods like `oval` and
+`rect`. !{:margin_left => 100}man-ele-shape.png!
+
+See the [[Common]] methods page. Shapes respond to all of those methods.
+
+== TextBlock ==
+
+The TextBlock object represents a group of text organized as a single element.
+A paragraph containing bolded text, for example. A caption containing links and
+bolded text. (So, a `caption` is a TextBlock type. However, `link` and
+`strong` are TextClass types.) !{:margin_left => 100}man-ele-textblock.png!
+
+All of the various types of TextBlock are found on the [[Element Element
+Creation]] page.
+
+ * [[Element.banner]], a 48 pixel font.
+ * [[Element.title]], a 34 pixel font.
+ * [[Element.subtitle]], a 26 pixel font.
+ * [[Element.tagline]], an 18 pixel font.
+ * [[Element.caption]], a 14 pixel font.
+ * [[Element.para]], a 12 pixel font.
+ * [[Element.inscription]], a 10 pixel font.
+
+=== contents() » an array of elements ===
+
+Lists all of the strings and styled text objects inside this block.
+
+=== replace(a string) ===
+
+Replaces the text of the entire block with the characters of `a string`.
+
+=== text() » a string ===
+
+Return a string of all of the characters in this text box. This will strip off
+any style or text classes and just return the actual characters, as if seen on
+the screen.
+
+=== text = a string ===
+
+Replaces the text of the entire block with the characters of `a string`.
+
+=== to_s() » a string ===
+
+An alias for [[TextBlock.text]]. Returns a flattened string of all of this
+TextBlock's contents.
+
+== Timers ==
+
+Shoes contains three timer classes: the Animation class, the Every class and
+the Timer class. Both Animations and Everies loop over and over after they
+start. Timers happen once. A one-shot timer.
+
+Animations and Everies are basically the same thing. The difference is that
+Animations usually happen many, many times per second. And Everies happen only
+once every few seconds or rarely.
+
+=== start() » self ===
+
+Both types of timers automatically start themselves, so there's no need to use
+this normally. But if you [[Timers.stop]] a timer and would like to start it up
+again, then by all means: use this!
+
+=== stop() » self ===
+
+Pauses the animation or timer. In the case of a one-shot timer that's already
+happened, it's already stopped and this method will have no effect.
+
+=== toggle() » self ===
+
+If the animation or timer is stopped, it is started. Otherwise, if it is
+already running, it is stopped.
+
+== Video ==
+
+Shoes supports embedding of QuickTime, Flash video (FLV), DivX, Xvid and
+various other popular video formats. This is all thanks to VideoLAN and ffmpeg,
+two sensational open source libraries. Use the `video` method on a slot to
+setup a Shoes::Video object. !{:margin_left => 100}man-ele-video.png!
+
+In addition to video formats, some audio formats are also supported, such as
+MP3, WAV and Ogg Vorbis.
+
+Video support is optional in Shoes and some builds do not support video. For
+example, video support is unavailable for PowerPC. When you download Shoes, the
+build for your platform will be marked `novideo` in the filename if no video
+support is available.
+
+=== hide() » self ===
+
+Hides the video. If already playing, the video will continue to play. This just
+turns off display of the video. One possible use of this method is to collapse
+the video area when it is playing an audio file, such as an MP3.
+
+=== length() » a number ===
+
+The full length of the video in milliseconds. Returns nil if the video is not
+yet loaded.
+
+=== move(left, top) » self ===
+
+Moves the video to specific coordinates, the (left, top) being the upper left
+hand corner of the video.
+
+=== pause() » self ===
+
+Pauses the video, if it is playing.
+
+=== playing?() » true of false ===
+
+Returns true if the video is currently playing. Or, false if the video is
+paused or stopped.
+
+=== play() » self ===
+
+Starts playing the video, if it isn't already playing. If already playing, the
+video is restarted from the beginning.
+
+=== position() » a decimal ===
+
+The position of the video as a decimanl number (a Float) between the beginning
+(0.0) and the end (1.0). For instance, a Float value of 0.5 indicates the
+halfway point of the video.
+
+=== position = a decimal ===
+
+Sets the position of the video using a Float value. To move the video to its
+25% position: `@video.position = 0.25`.
+
+=== remove() » self ===
+
+Removes the video from its slot. This will stop the video as well.
+
+=== show() » self ===
+
+Reveals the video, if it has been hidden by the `hide()` method.
+
+=== stop() » self ===
+
+Stops the video, if it is playing.
+
+=== time() » a number ===
+
+The time position of the video in milliseconds. So, if the video is 10 seconds
+into play, this method would return the number 10000.
+
+=== time = a number ===
+
+Set the position of the video to a time in milliseconds.
+
+=== toggle() » self ===
+
+Toggles the visibility of the video. If the video can be seen, then `hide` is
+called. Otherwise, `show` is called.
+
+= AndSoForth =
+
+A place for some other information.
+
+== Sample Apps ==
+
+Have fun!
+
+{SAMPLES}
+
+== FAQ ==
+
+Hope this helps:
+
+ * You can join [[http://librelist.com/browser/shoes/ Shoes ML]] and feel free ask your questions.
+ * [[http://github.com/shoes/shoes/ Official Current Source Code]] is on GitHub.
+ * [[http://wiki.github.com/shoes/shoes/recentbuilds/ Recent Builds]] for your platform.
diff --git a/static/manual-ja.txt b/static/manual-ja.txt
new file mode 100644
index 0000000..e0a239a
--- /dev/null
+++ b/static/manual-ja.txt
@@ -0,0 +1,2825 @@
+= Hello! =
+
+Shoesã¯è»½é‡ãªã‚°ãƒ©ãƒ•ã‚£ãƒƒã‚¯ãƒ„ールキットã§ã™ã€‚ã“ã‚Œã¯å˜ç´”ã§åˆ†ã‹ã‚Šã‚„ã™ã„ã§ã™ã€‚Shoesã¯ç°¡å˜ã«ãªã‚‹ã‚ˆã†ã«ç”Ÿã¾ã‚Œã¾ã—ãŸã€‚本当ã«ã€ã“ã‚Œã¯å…¨ãã®åˆå¿ƒè€…ã®ãŸã‚ã«ä½œã‚‰ã‚Œã¾ã—ãŸã€‚本当ã«ç°¡å˜ã§ã™ã€‚
+
+ã“ã®ãŸã£ãŸä¸€è¡Œã®å–ã‚‹ã«è¶³ã‚Šãªã„Shoesã®ãƒ—ログラムを見ã¦ãã ã•ã„:
+
+{{{
+ #!ruby
+ Shoes.app { button("Click me!") { alert("Good job.") } }
+}}}
+
+Shoesプログラムã¯Rubyã¨å‘¼ã°ã‚Œã‚‹è¨€èªžã§æ›¸ã‹ã‚Œã¦ã„ã¾ã™ã€‚ShoesãŒã“ã®Rubyコードã®å˜ç´”ãªè¡Œã‚’渡ã•ã‚ŒãŸã¨ãã€"Click me!"ã¨ä¸­ã«æ›¸ã‹ã‚ŒãŸãƒœã‚¿ãƒ³ã‚’æŒã£ãŸã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’表示ã—ã¾ã™ã€‚ã“ã®ãƒœã‚¿ãƒ³ã‚’クリックã™ã‚‹ã¨ã€ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ãŒãƒãƒƒãƒ—アップã—ã¾ã™ã€‚
+
+Linuxã§ã¯ã€ã“ã®ã‚ˆã†ã«è¦‹ãˆã‚‹ã§ã—ょã†: !{:margin_left => 100}man-shot1.png!
+
+多ãã®Shoesã®ã‚¢ãƒ—リケーションãŒã‚°ãƒ©ãƒ•ã‚£ã‚«ãƒ«ãªã‚²ãƒ¼ãƒ ã‚„アートã®ãƒ—ログラムã§ã‚る一方ã€ãƒ†ã‚­ã‚¹ãƒˆã‚’é…ç½®ã—ãŸã‚Šç·¨é›†ã—ãŸã‚Šã™ã‚‹ã“ã¨ã‚‚ç°¡å˜ã«ã§ãã¾ã™ã€‚ !{:margin_left => 40}shoes-manual-apps.gif!
+
+ãã—ã¦ã€ç†æƒ³çš„ã«ã¯ã€Shoesプログラムã¯ä¸–ã®ä¸­ã®ã„ãã¤ã‹ã®æœ‰åãªãƒ—ラットフォームã§å®Ÿè¡Œã§ãã‚‹ã§ã—ょã†ã€‚マイクロソフトWindowsã€ã‚¢ãƒƒãƒ—ルã®Mac OS Xã€Linuxや多ãã®ä»–ã®ãƒ—ラットフォームã§ã€‚
+
+^ãã—ã¦ã€Shoesã®ãƒ“ルトインマニュアルã¸ã‚ˆã†ã“ã。ã“ã®ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã¯Shoesã®ãƒ—ログラムãã®ã‚‚ã®ã§ã™ã€‚^
+
+== Introducing Shoes ==
+
+OS Xã‚„Windowsã§ã¯Shoesã¯ã©ã®ã‚ˆã†ã«è¦‹ãˆã‚‹ã§ã—ょã†ã‹ï¼Ÿè¦‹æ „ãˆã¯ã„ã„ã§ã™ã‹ï¼Ÿå…¨ä½“çš„ã«è¦‹è‹¦ã—ãã¦ä¸æ ¼å¥½ã§ã™ã‹ï¼Ÿã¿ã‚“ãªã™ãã«èº«æ‚¶ãˆã—ãŸã«é•ã„ãªã„ã§ã™ï¼ãã‚Œã¯ä½•ã‚’ã—よã†ã¨ã—ã¦ã‚‚ã€ã¨ã¦ã‚‚質ãŒè½ã¡ãŸã‚‚ã®ã«é•ã„ã‚ã‚Šã¾ã›ã‚“。
+
+ãã‚Œã§ã¯ã€Shoesã®ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ«ã‚„実行ã«ã¤ã„ã¦å…¥ã‚‹å‰ã«ã€ä½•ãŒã§ãã‚‹ã®ã‹ã®å‚考ã«ã€ã¡ã‚‡ã£ã¨ã„ãã¤ã‹ã®ã‚¹ã‚¯ãƒªãƒ¼ãƒ³ã‚·ãƒ§ãƒƒãƒˆã‚’確èªã—ã¾ã™ã€‚
+
+==== Mac OS X ====
+
+Shoesã¯ã‚¢ãƒƒãƒ—ルã®Mac OS X Leopardã§ã€åŒæ§˜ã«Tigerã§ã‚‚動作ã—ã¾ã™ã€‚Shoesã¯PowerPCマシンã®åŒæ§˜ã«ã‚µãƒãƒ¼ãƒˆã—ã¾ã™ãŒã€ãã®ãƒ—ラットフォームã§ã¯ãƒ“デオ機能ã¯ã‚µãƒãƒ¼ãƒˆã•ã‚Œã¾ã›ã‚“。 !man-look-tiger.png!
+
+ã“ã‚Œã¯Tigerã§å®Ÿè¡Œã—ã¦ã„ã‚‹`simple-sphere.rb`サンプルã§ã™ã€‚アプリケーションã¯é€šå¸¸ã®OS Xウィンドウã®æž ã®ä¸­ã§å®Ÿè¡Œã•ã‚Œã¦ã„ã‚‹ã“ã¨ã«æ³¨æ„ã—ã¦ä¸‹ã•ã„。
+
+ã“ã®çƒã®å…¨ä½“ã¯ä¸é®®æ˜Žãªæ¥•å††å½¢ã¨å½±ã«ã‚ˆã£ã¦æã‹ã‚Œã¦ã„ã¾ã™ã€‚Shoesã§ã¯ã€ç”Ÿã生ãã¨ã—ãŸå½¢çŠ¶ã‚’æãã“ã¨ã‚„ã€ãれらã®å½¢çŠ¶ã«åŠ¹æžœã‚’é©ç”¨ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+==== Windows ====
+
+Shoesã¯ã™ã¹ã¦ã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³ã®'''Microsoft Windows XP'''〠'''Windows Vista'''〠'''Windows 7'''ã§å®Ÿè¡Œã§ãã€ä»–ã«ã‚‚'''Windows 2000'''ã§äº’æ›æ€§ãŒã‚ã‚Šã¾ã™ã€‚ !man-look-vista.png!
+
+上記ã¯Windows Vistaã§`simple-clock.rb`サンプルãŒå‹•ä½œã—ã¦ã„る図ã§ã™ã€‚ã“ã®ä¾‹ã§ã‚‚時計を作るãŸã‚ã«æ¥•å††ã¨ç·šãŒæã‹ã‚Œã¦ãŠã‚Šã€ã“ã‚Œã¯ä¸€ç§’ã«ä½•å›žã‹ãれ自信ãŒå†æç”»ã™ã‚‹ã®ã®ã§ç”Ÿã生ãã¨æã‹ã‚Œã¾ã™ã€‚
+
+アプリケーション上部ã®ãƒ†ã‚­ã‚¹ãƒˆãŒã€ç¾åœ¨ã®æ™‚刻を表示ã—ã¦ã„ã‚‹ã“ã¨ã«æ³¨æ„ã—ã¦ãã ã•ã„。Shoesã¯ã€ã„ãã¤ã‹ã®è‰²ã€å¤ªå­—ã€æ–œå­—ã€ä¸‹ç·šã€ãã—ã¦ãƒ•ã‚¡ã‚¤ãƒ«ã‹ã‚‰ãƒ•ã‚©ãƒ³ãƒˆã‚’ロードã—ã¦èªžå¥ã‚’é…ç½®ã™ã‚‹æ©Ÿèƒ½ã‚’æŒã£ã¦ã„ã¾ã™ã€‚
+
+==== Linux ====
+
+ã“ã‚Œã¯'''Ubuntu Linux'''上ã§`simple-downloader.rb`サンプルãŒå‹•ä½œã—ã¦ã„るスクリーンショットã§ã™ã€‚!man-look-ubuntu.png!
+
+ボタンã¨ãƒ—ログレスãƒãƒ¼ã«æ³¨æ„ã—ã¦ãã ã•ã„。ã“れらã®ç¨®é¡žã®ã‚³ãƒ³ãƒˆãƒ­ãƒ¼ãƒ«ã¯OS Xã¨Windowsã¨ã¯ç•°ãªã‚‹ã‚ˆã†ã«è¦‹ãˆã¾ã™ã€‚ã—ã‹ã—ã€ãƒ†ã‚­ã‚¹ãƒˆã¨ãƒªãƒ³ã‚¯ã¯åŒã˜ã‚ˆã†ã«è¦‹ãˆã‚‹ã§ã—ょã†ã€‚
+
+形状ã€ãƒ†ã‚­ã‚¹ãƒˆã€ç”»åƒã‚„å‹•ç”»ã¯ã™ã¹ã¦ã®ãƒ—ラットフォームã§åŒã˜ã‚ˆã†ã«è¦‹ãˆã¾ã™ã€‚ã—ã‹ã—ãªãŒã‚‰ã€ãƒã‚¤ãƒ†ã‚£ãƒ–コントロール(エディットラインやエディットボックスã®ã‚ˆã†ãªï¼‰ã¯ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ãƒ†ãƒ¼ãƒžã®è¦‹ãŸç›®ã«ä¸€è‡´ã—ã¾ã™ã€‚Shoesã¯ãƒã‚¤ãƒ†ã‚£ãƒ–コントロールをã€è¦‹ãŸç›®ã¯å¤‰åŒ–ã—ã¾ã™ãŒã€æŒ‡å®šã—ãŸå¤§ãã•ã®ç¯„囲内ã§ç¶­æŒã—よã†ã¨ã—ã¾ã™ã€‚
+
+== Installing Shoes ==
+
+ã¯ã„ã€ã§ã¯Shoesã®ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ«ã‚’è¡Œã„ã¾ã™ã€‚ã‚ãªãŸã¯æ¬¡ã®ã‚ˆã†ãªç–‘å•ã‚’æŒã£ã¦ã„ã‚‹ã§ã—ょã†:Rubyã®ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ«ã¯å¿…è¦ã§ã™ã‹ï¼Ÿãªã«ã‚‚解å‡ã—ãªãã¦ã‚‚ã„ã„ã§ã™ã‹ï¼Ÿã©ã‚“ãªã‚³ãƒžãƒ³ãƒ‰ã‚’タイプã™ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ã‹ï¼Ÿ
+
+ã„や。Rubyã‚’å¿…è¦ã¨ã—ã¾ã›ã‚“。WinZipã‚’å¿…è¦ã¨ã—ã¾ã›ã‚“。ãªã«ã‚‚タイプã—ãªãã¦ã„ã„ã§ã™ã€‚
+
+Shoesを開始ã™ã‚‹ã«ã¯ã€å¤šãã®ã‚·ã‚¹ãƒ†ãƒ ã§ã¯Shoesã®ã‚¢ã‚¤ã‚³ãƒ³ã‚’クリックã—ã¦ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ©ã‚’実行ã™ã‚‹ã ã‘ã§ã™ã€‚Shoesã¯ã™ã¹ã¦ã‚’組ã¿è¾¼ã¿ã§å‚™ãˆã¦ã„ã¾ã™ã€‚ã‚‚ã£ã¨ã‚‚ã€ã¾ã•ã—ããã‚Œã«é–¢ã—ã¦æ˜Žç¢ºã«ãªã‚‹ã‚ˆã†ã«ã€ç§ãŸã¡ã¯ã™ã¹ã¦ã®ã‚¹ãƒ†ãƒƒãƒ—ã«ã¤ã„ã¦è©±ã‚’ã—ã¾ã™ã€‚
+
+==== Step 1: Shoesã®ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ« ====
+
+[[http://shoes.heroku.com/ the site of Shoes]]ã¸ã‚¢ã‚¯ã‚»ã‚¹ã—ã¦Shoesã®ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ©ã‚’ダウンロードã—ã¾ã™ã€‚通常ã¯ãƒ›ãƒ¼ãƒ ãƒšãƒ¼ã‚¸ã®ä¸Šéƒ¨ã®è§’ã«ã‚るインストーラã®ã²ã¨ã¤ã‚’手ã«å…¥ã‚Œã¾ã™ã€‚ !man-builds.png!
+
+ã“ã“ã«ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ©ã®å®Ÿè¡Œæ–¹æ³•ãŒã‚ã‚Šã¾ã™ï¼š
+
+ * '''Mac OS X'''ã§ã¯ã€'''.dmg'''ã§çµ‚ã‚ã£ã¦ã„るファイルを手ã«å…¥ã‚Œã¾ã™ã€‚ã“ã®ãƒ•ã‚¡ã‚¤ãƒ«ã‚’ダブルクリックã™ã‚‹ã¨ã€'''Shoes'''アイコンã¨'''Applications'''フォルダã¨å…±ã«ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ãŒè¡¨ç¤ºã•ã‚Œã¾ã™ã€‚矢å°ã«å¾“ã£ã¦'''Applications'''フォルダã«Shoesアイコンをドラッグã—ã¾ã™ã€‚ !man-intro-dmg.png!
+* '''Windows'''ã§ã¯ã€'''.exe'''ファイルをダウンロードã—ã¾ã™ã€‚ã“ã®ãƒ•ã‚¡ã‚¤ãƒ«ã‚’ダブルクリックã—ã¦æ¬¡ã®æŒ‡ç¤ºã«å¾“ã£ã¦ãã ã•ã„。!man-intro-exe.png!
+* '''Linux'''ã§ã¯ã€'''.run'''ã§çµ‚ã‚ã£ã¦ã„るファイルをダウンロードã—ã¾ã™ã€‚ã“ã®ãƒ•ã‚¡ã‚¤ãƒ«ã‚’ダブルクリックã™ã‚‹ã¨ShoesãŒèµ·å‹•ã—ã¾ã™ã€‚(シェルスクリプトã®ã‚ˆã†ã«ãƒ—ロンプトã‹ã‚‰ã“ã®ãƒ•ã‚¡ã‚¤ãƒ«ã‚’実行ã™ã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚実際ã«ã¯ã€ã“ã‚Œã¯å°ã•ãªã‚·ã‚§ãƒ«ã‚¹ã‚¯ãƒªãƒ—トã§ã™ã€‚)
+
+==== Step 2: æ–°ã—ã„テキストファイルã®ä½œæˆ ====
+
+Shoesプログラムã¯'''.rb'''ã®æ‹¡å¼µå­ã§çµ‚ã‚ã‚‹ã€ãŸã ã®å˜ç´”ãªãƒ†ã‚­ã‚¹ãƒˆãƒ•ã‚¡ã‚¤ãƒ«ã§ã™ã€‚
+
+空ã®ãƒ†ã‚­ã‚¹ãƒˆãƒ•ã‚¡ã‚¤ãƒ«ã‚’作æˆã™ã‚‹ã„ãã¤ã‹ã®æ–¹æ³•ã¯:
+
+ * '''Mac OS X'''ã§ã¯'''Applications'''フォルダã«ç§»å‹•ã—ã¦'''TextEdit'''アプリケーションをダブルクリックã—ã¾ã™ã€‚空ã®ã‚¨ãƒ‡ã‚£ã‚¿ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ãŒè¡¨ç¤ºã•ã‚Œã¾ã™ã€‚ãã—ã¦'''Format'''メニューã‹ã‚‰'''Make Plain Text'''オプションをé¸æŠžã—ã¾ã™ã€‚ã¯ã„ã€æº–å‚™ãŒã§ãã¾ã—ãŸã€‚!man-editor-osx.png!
+ * '''Windows'''ã§ã¯ã€ã‚¹ã‚¿ãƒ¼ãƒˆãƒ¡ãƒ‹ãƒ¥ãƒ¼ã¸è¡Œãã¾ã™ã€‚'''All Programs'''ã€'''Accessories'''ãã—ã¦'''Notepad'''ã‚’é¸æŠžã—ã¾ã™ã€‚. !man-editor-notepad.png!
+ * '''Linux'''ã§ã¯ã€å¤šãã®ãƒ‡ã‚£ã‚¹ãƒˆãƒªãƒ“ューションãŒ'''gedit'''ã‚’å‚™ãˆã¦ã„ã¾ã™ã€‚ãれを実行ã—ã¦ãã ã•ã„。ã¾ãŸã¯ã€ã‚‚ã—ã‚ãªãŸã®ãƒ‡ã‚£ã‚¹ãƒˆãƒªãƒ“ューションãŒKDEを基ã«ã—ã¦ã„ã‚‹ã®ã§ã‚ã‚Œã°'''kate'''を実行ã—ã¦ãã ã•ã„。
+
+ãã—ã¦ã€ç©ºã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã«æ¬¡ã®ã‚ˆã†ã«ã‚¿ã‚¤ãƒ—ã—ã¦ãã ã•ã„:
+
+{{{
+ Shoes.app do
+ background "#DFA"
+ para "Welcome to Shoes"
+ end
+}}}
+
+`welcome.rb`ã¨ã—ã¦ãƒ‡ã‚¹ã‚¯ãƒˆãƒƒãƒ—ã«ä¿å­˜ã—ã¦ãã ã•ã„。
+
+==== Step 3: ãれを実行ã—ã¦ãã ã•ã„ï¼Shoesã¸è¡Œã“ã†ï¼ ====
+
+プログラムを実行ã™ã‚‹ãŸã‚ã«ã¯:
+
+ * '''Mac OS X'''ã§ã¯ã€'''Applications'''ã¸å†åº¦è¡Œãã¾ã™ã€‚今度ã¯ã€ãã®ãƒ•ã‚©ãƒ«ãƒ€ã®'''Shoes'''アイコンをダブルクリックã—ã¾ã™ã€‚ãã®ãƒ‰ãƒƒã‚¯ã®ä¸­ã«èµ¤ã„é´ã®ã‚¢ã‚¤ã‚³ãƒ³ãŒç¾ã‚Œã¾ã™ã€‚'welcome.rb'をデスクトップã‹ã‚‰ã€ãã®ãƒ‰ãƒƒã‚¯ã‚¢ã‚¤ã‚³ãƒ³ã¸ãƒ‰ãƒ©ãƒƒã‚°ã—ã¦ãã ã•ã„。
+ * '''Windows'''ã§ã¯ã€ã‚¹ã‚¿ãƒ¼ãƒˆãƒ¡ãƒ‹ãƒ¥ãƒ¼ã¸ã‹ã‚‰'''All Programs'''ã€'''Shoes'''ãã—ã¦'''Shoes'''ã¸è¡Œãã¾ã™ã€‚ファイルã®é¸æŠžãƒœãƒƒã‚¯ã‚¹ãŒè¡¨ç¤ºã•ã‚Œã¾ã™ã€‚デスクトップã«ç›®ã‚’通ã—ã¦'welcome.rb'ã‚’é¸æŠžã—ã¾ã™ã€‚'''OK'''をクリックã—ãŸã‚‰ã€å¾Œã¯è‡ªåˆ†ã§ã‚„ã£ã¦ãã ã•ã„。!man-run-xp.png! !man-run-vista.png!
+ * '''Linux'''ã§ã¯ã€ã»ã¨ã‚“ã©ä¸€å›žã®æ‰‹ç¶šãã§ã ã‘ã§Shoesを実行ã—ã¾ã™ã€‚ã‚ãªãŸã¯ãƒ•ã‚¡ã‚¤ãƒ«é¸æŠžãƒœãƒƒã‚¯ã‚¹ã‚’見るã¯ãšã§ã™ã€‚デスクトップã«ç›®ã‚’通ã—ã¦ã€`welcome.rb`ã‚’é¸æŠžã—ã¦'''OK'''を押ã—ã¦ãã ã•ã„。
+
+ã¾ã å¤§ã—ãŸã‚‚ã®ã§ã¯ãªã„ã§ã™ãŒã€ç¢ºã‹ã«ãã‚Œã¯ãƒ—ログラムã§ã™ï¼å°‘ãªãã¨ã‚‚ã€ãã®ã‚³ãƒ„ãŒåˆ†ã‹ã£ãŸã§ã—ょã†ï¼
+
+==== Shoesã§ä½•ã‚’作りã¾ã™ã‹ï¼Ÿ ====
+
+ã•ã¦ã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚¢ãƒ—リケーションを作æˆã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚ã—ã‹ã—ã€Shoesã¯ã‚¦ã‚§ãƒ–ã‹ã‚‰å½±éŸ¿ã‚’å—ã‘ã¦ã„ã‚‹ã®ã§ã€ã‚¢ãƒ—リケーションã¯å¤šãã®ã‚¦ã‚£ã‚¸ã‚§ãƒƒãƒˆã‚ˆã‚Šã‚‚ã€ç”»åƒã‚’使ã£ãŸã‚Šãƒ†ã‚­ã‚¹ãƒˆã‚’é…ç½®ã™ã‚‹å‚¾å‘ãŒã‚ã‚Šã¾ã™ã€‚例ãˆã°ã€Shoesã¯ã‚¿ãƒ–コントロールやツールãƒãƒ¼ã‚’å‚™ãˆã¦ã„ã¾ã›ã‚“。Shoesã¯''軽é‡ãª''ツールãã£ã¨ã ã¨è¨€ã†ã“ã¨ã‚’覚ãˆã¦ã„ã¾ã™ã‹ï¼Ÿ
+
+ãã‚Œã§ã‚‚Shoesã¯ãƒœã‚¿ãƒ³ã‚„エディットボックスã®ã‚ˆã†ãªã‚¦ã‚£ã‚¸ã‚§ãƒƒãƒˆã‚’å°‘ã—ã ã‘æŒã£ã¦ã„ã¾ã™ã€‚ãã—ã¦ã€å¤šãã®ï¼ˆã‚¿ãƒ–コントロールã¾ãŸã¯ãƒ„ールãƒãƒ¼ã®ã‚ˆã†ãªï¼‰ä¸è¶³ã—ã¦ã„ã‚‹è¦ç´ ã¯ã€ç”»åƒã§ã‚·ãƒŸãƒ¥ãƒ¬ãƒ¼ã‚·ãƒ§ãƒ³ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+Shoesã¯Cairoã¨å‘¼ã°ã‚Œã‚‹ã¨ã¦ã‚‚よãã§ããŸæ画エンジンã«ã‚ˆã£ã¦ä¸€éƒ¨æ›¸ã‹ã‚Œã¦ãŠã‚Šã€ãã‚Œã¯å½¢çŠ¶ã‚„色彩をæãã®ã«åˆ©ç”¨ã•ã‚Œã¦ã„ã¾ã™ã€‚ã“ã®ã‚ˆã†ã«ã€Shoesã¯ã€ç”Ÿã生ãã¨ã—ãŸã‚°ãƒ©ãƒ•ã‚£ãƒƒã‚¯ã‚’æç”»ã™ã‚‹ãŸã‚ã«ã¨ã¦ã‚‚ã„ã„言語ã§ã‚ã‚‹ã€NodeBoxã¨Processingã‹ã‚‰å½±éŸ¿ã‚’å—ã‘ã¦ã„ã¾ã™ã€‚
+
+== The Rules of Shoes ==
+
+ShoesãŒã©ã®ã‚ˆã†ã«å‹•ä½œã™ã‚‹ã®ã‹æŽ¨æ¸¬ã™ã‚‹ã®ã¯ã‚„ã‚ã¾ã—ょã†ã€‚トリッキーãªå‹•ä½œã§æ‚©ã‚€ã¨æ€ã„ã¾ã™ã€‚ç§ã¯Shoesã®ä¸­å¿ƒçš„ãªè¦å‰‡ã‚’è¦ç´„ã—ã¾ã„ãŸã€‚ã“れらã¯ã€ãã‚Œã«ã™ã¹ã¦ã®åƒãã‚’ã•ã›ã‚‹ãŸã‚ã«ã¯çŸ¥ã‚‰ãªãã¦ã¯ãªã‚‰ãªã„ã‚‚ã®ã§ã™ã€‚
+
+ã“れらã¯Shoesã®è‡³ã‚‹æ‰€ã«ç›®ã«ã™ã‚‹ä¸€èˆ¬çš„ãªè¦å‰‡ã§ã™ã€‚Shoesã¯å˜ç´”ã•ã¨æ˜Žè§£ã•ã¨ã„ã†å…¨ä½“çš„ãªç†å¿µã‚’æŒã£ã¦ã„ã¾ã™ãŒã€ã„ãã¤ã‹å‹‰å¼·ã—ãŸã‚Šè¦šãˆãŸã‚Šã™ã‚‹å¿…è¦ã®ã‚ã‚‹ãƒã‚¤ãƒ³ãƒˆãŒã‚ã‚Šã¾ã™ã€‚
+
+==== Shoesã®ãƒˆãƒªãƒƒã‚­ãƒ¼ãªãƒ–ロック ====
+
+ã¯ã„ã€ã“ã‚Œã¯æ¥µã‚ã¦é‡è¦ã§ã™ã€‚Shoesã¯ãƒ–ロックã«ã‚ˆã£ã¦ãƒˆãƒªãƒƒã‚¯ã‚’ã—ã¾ã™ã€‚ã“ã®ãƒˆãƒªãƒƒã‚¯ã¯ã™ã¹ã¦ã®ã‚‚ã®ã‚’読ã¿ã‚„ã™ãã—ã¾ã™ã€‚ã—ã‹ã—ã€ã“ã‚Œã¯æ·±ã„階層ã§ãƒ–ロックを利用ã™ã‚‹ã“ã¨ã‚’難ã—ãã‚‚ã—ã¾ã™ã€‚
+
+'''普通ã®Rubyブロックを試ã—ã¾ã—ょã†ï¼š'''
+
+{{{
+ ary = ['potion', 'swords', 'shields']
+ ary.each do |item|
+ puts item
+ end
+}}}
+
+Shoesã§ã¯ã€ã“れらã®ç¨®é¡žã®ãƒ–ロックã¯åŒã˜åƒãã‚’ã—ã¾ã™ã€‚ã“ã®ä¸Šè¨˜ã®ãƒ–ロックã¯é…列をループã—ã¦å„オブジェクトを`item`変数ã«æ ¼ç´ã—ã¾ã™ã€‚ã“ã®`item`変数ã¯ãƒ–ロックãŒçµ‚ã‚ã‚‹ã¨æ¶ˆæ»…(スコープã‹ã‚‰å‡ºã‚‹ï¼‰ã—ã¾ã™ã€‚
+
+考ãˆæ–¹ã‚’守ã£ã¦ã„ã‚‹ã‚‚ã†ä¸€ã¤ã®ã“ã¨ã¯ã€æ™®é€šã®Rubyブロックã®å†…部ã¨`self`ã‚’åŒã˜ã¾ã¾ã«ã—ã¦ã„ã¾ã™ã€‚`each`ã®å‰ã«å‘¼ã°ã‚Œã‚‹ã©ã‚“ãª`self`ã§ã‚‚ã€ãã‚Œã¯`each`ブロック内部ã¨åŒã˜ã§ã™ã€‚
+
+'''ã“れらã¯ã©ã¡ã‚‰ã‚‚大部分ã®Shoesã®ãƒ–ロックã§æ­£ã—ã„ã§ã™ã€‚'''
+
+{{{
+ Shoes.app do
+ stack do
+ para "First"
+ para "Second"
+ para "Third"
+ end
+ end
+}}}
+
+ã“ã“ã§ã¯äºŒã¤ã®ãƒ–ロックãŒã‚ã‚Šã¾ã™ã€‚一ã¤ç›®ã¯`Shoes.app`ã«ã‚ˆã‚‹ã‚‚ã®ã§ã™ã€‚ã“ã®`app`ブロックã¯`self`を変更ã—ã¾ã™ã€‚
+
+ã‚‚ã†ä¸€æ–¹ã®ãƒ–ロックã¯`stack`ブロックã§ã™ã€‚ã“ã®ãƒ–ロックã¯selfを変更ã—ã¾ã›ã‚“。
+
+'''ã©ã‚“ãªç†ç”±ãŒã‚ã£ã¦`app`ブロックã¯selfを変更ã™ã‚‹ã®ã§ã—ょã†ã‹ï¼Ÿ''' 最後ã®ä¾‹ã‚’徹底的ã«è©³ã—ã説明ã™ã‚‹ã“ã¨ã‹ã‚‰å§‹ã‚ã¾ã—ょã†ã€‚
+
+{{{
+ Shoes.app do
+ self.stack do
+ self.para "First"
+ self.para "Second"
+ self.para "Third"
+ end
+ end
+}}}
+
+上記ã®ä¾‹ã«ãŠã‘ã‚‹ã™ã¹ã¦ã®`self`ã¯ã‚¢ãƒ—リケーションオブジェクトã§ã™ã€‚Shoesã¯`app`ブロック内部ã§selfを変更ã™ã‚‹ãŸã‚ã«ã€Rubyã®`instance_eval`を利用ã—ã¾ã™ã€‚ãã—ã¦ã€ãã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯`stack`を呼ã³å‡ºã—ã¦`para`をアプリケーションã¸é€ã‚Šã¾ã™ã€‚
+
+'''ã“ã‚Œã¯Shoesアプリケーション全体ã§ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹å¤‰æ•°ãŒåˆ©ç”¨ã§ãã‚‹ç†ç”±ã§ã‚‚ã‚ã‚Šã¾ã™ï¼š'''
+
+{{{
+ Shoes.app do
+ @s = stack do
+ @p1 = para "First"
+ @p2 = para "Second"
+ @p3 = para "Third"
+ end
+ end
+}}}
+
+ã“れらã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹å¤‰æ•°ã¯ã€ã™ã¹ã¦ã‚¢ãƒ—リケーションオブジェクト内部ã§çµ‚了ã—ã¾ã™ã€‚
+
+'''æ–°ã—ã„ウィンドウを作æˆã™ã‚‹ã¨ãã¯ã„ã¤ã§ã‚‚ã€`self`も変更ã•ã‚Œã¾ã™ã€‚'''ãã—ã¦ã€Shoes.appã«åŠ ãˆã¦ã€ã“ã‚Œã¯[[Element.window]]ã¨[[Element.dialog]]メソッドをæ„味ã—ã¾ã™ã€‚
+
+{{{
+ Shoes.app :title => "MAIN" do
+ para self
+ button "Spawn" do
+ window :title => "CHILD" do
+ para self
+ end
+ end
+ end
+}}}
+
+==== ブロックリダイレクション ====
+
+ã‚‚ã£ã¨ã‚‚ã€`stack`ブロックã¯åˆ¥ã®è©±ã§ã™ã€‚ã“ã‚Œã¯`self`を変ãˆã¾ã›ã‚“ã—ã€åŸºæœ¬çš„ã«æ¨™æº–ã®ãƒ–ロックã§ã™ã€‚
+
+'''ã—ã‹ã—トリックãŒã‚ã‚Šã¾ã™ï¼š'''`stack`ã‚’é…ç½®ã—ã¦ãƒ–ロックを与ãˆãŸã¨ãã€ã‚¢ãƒ—リケーションオブジェクトã¯stackをメモリã¸é…ç½®ã—ã¾ã™ã€‚ブロックãŒçµ‚了ã™ã‚‹ã¨ãstackã¯ç«‹ã¡åŽ»ã‚Šã¾ã™ã€‚ãã®ãŸã‚ã€ã‚¢ãƒ—リケーションã®ãƒˆãƒƒãƒ—スロットã‹ã‚‰æ–°ã—ã„stackã¾ã§ã€ãƒ–ロック内部ã®ã™ã¹ã¦ã®æç”»ã¯'''リダイレクト'''ã•ã‚Œã¾ã™ã€‚
+
+ãã®ãŸã‚ã€ã¾ãšã€ãŸã¨ãˆå½¼ã‚‰ãŒç¢ºã‹ã«ã‚¢ãƒ—リケーションオブジェクトã¸æ¸¡ã—ãŸã¨ã—ã¦ã‚‚ã€ãれらã®3ã¤ã®`para`ã¯`stack`上ã«æç”»ã•ã‚Œã¾ã™ã€‚
+
+{{{
+ Shoes.app do
+ stack do
+ para "First"
+ para "Second"
+ para "Third"
+ end
+ end
+}}}
+
+å°‘ã—トリッキーã§ã™ã‚ˆã­ï¼Ÿã“ã‚Œã«ã¤ã„ã¦çŸ¥ã£ã¦ã„ã¦ã‚‚å™›ã¿ã¤ã‹ã‚Œã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。
+
+ãã‚ŒãŒã‚ãªãŸã‚’ã¤ã‹ã¾ãˆã‚‹ä¸€ã¤ã®æ–¹æ³•ã¯ã€`app`ブロック外ã®ãƒ—ログラム内ã®ã©ã“ã‹ä»–ã§ã€stackを編集ã—よã†ã¨ã™ã‚‹ã“ã¨ã§ã™ã€‚
+
+例ãˆã°stackオブジェクトを使ã„ã¾ã‚ã™ã¨ã—ã¾ã—ょã†ã€‚ãã—ã¦ã€ãã®ã‚ªãƒ–ジェクトを編集ã™ã‚‹ã‚¯ãƒ©ã‚¹ã‚’æŒã¡ã¾ã™ã€‚
+
+{{{
+ class Messenger
+ def initialize(stack)
+ @stack = stack
+ end
+ def add(msg)
+ @stack.append do
+ para msg
+ end
+ end
+ end
+}}}
+
+アプリケーションãŒå§‹ã¾ã‚‹ã¨ãã«ã€stackオブジェクトをMessengerクラスã¸æ¸¡ã™ã¨ä»®å®šã—ã¾ã™ã€‚ãã—ã¦ã€å¾Œã§ã€ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ãŒæ¥ã‚‹ã¨ãã€`add`メソッドã¯ãã®stackã«ãƒ‘ラグラフを追加ã™ã‚‹ã®ã«ç”¨ã„られã¾ã™ã€‚æ­£ã—ã動作ã™ã‚‹ã§ã—ょã†ã‹ï¼Ÿ
+
+ã„ã‚„ã€ãã‚Œã¯å‹•ä½œã—ã¾ã›ã‚“。`para`メソッドãŒè¦‹ã¤ã‹ã‚Šã¾ã›ã‚“。ã™ã§ã«ã‚¢ãƒ—リケーションオブジェクトã¯ã¾ã‚ã‚Šã«ã‚ã‚Šã¾ã›ã‚“。ãã—ã¦ã€ãã‚Œã¯`para`メソッドã«ã‚ˆã‚‹ã‚‚ã®ã§ã™ã€‚
+
+幸ã„ã«ã‚‚ã€ãã‚Œãžã‚Œã®Shoesオブジェクトã¯ã‚¢ãƒ—リケーションオブジェクトをå†ã³é–‹ã‹ã›ã‚‹`app`メソッドをæŒã£ã¦ã„ã‚‹ãŸã‚ã€ã•ã‚‰ãªã‚‹ç·¨é›†ãŒã§ãã¾ã™ã€‚
+
+{{{
+ class Messenger
+ def initialize(stack)
+ @stack = stack
+ end
+ def add(msg)
+ @stack.app do
+ @stack.append do
+ para msg
+ end
+ end
+ end
+ end
+}}}
+
+ã”想åƒã®ã¨ãŠã‚Šã€ãã®`app`オブジェクトã¯`self`をアプリケーションオブジェクトã«å¤‰æ›´ã—ã¾ã™ã€‚
+
+ルールã¯æ¬¡ã®ã‚ˆã†ã«ãªã‚Šã¾ã™ï¼š
+
+1. '''"app"ã¨ã„ã†åå‰ã‹æ–°ã—ã„ウィンドウを作æˆã™ã‚‹ãƒ¡ã‚½ãƒƒãƒ‰ã¯ã‚¢ãƒ—リケーションオブジェクトã®`self`を変更ã—ã¾ã™ã€‚'''[[BR]](ã“ã‚Œã¯ã€[[Element.window]] ã¨[[Element.dialog]]åŒæ§˜ã«ã€Shoes.appã¨Slot.appã®ä¸¡æ–¹ã«ã¨ã£ã¦æ­£ã—ã„ã§ã™ã€‚)[[BR]]2. '''stackã¸åŠ ãˆã‚‰ã‚Œã‚‹ãƒ–ロックã¯ã€ãƒ•ãƒ­ãƒ¼ã‚„(追加ãªã©ã®ï¼‰ã©ã‚“ãªæ“作メソッドã§ã‚‚selfを変更ã—ã¾ã›ã‚“。ãã®ä»£ã‚ã‚Šã«ã€å½¼ã‚‰ã¯ã‚¹ãƒ­ãƒƒãƒˆã‚’アプリケーションã®ç·¨é›†stackã«ãƒãƒƒãƒ—ã—ã¾ã™ã€‚'''
+
+==== 固定ã•ã‚ŒãŸé«˜ã•ã«æ³¨æ„ ====
+
+列ã§ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’区切るよã†ã«ã€ã‚¹ãƒ­ãƒƒãƒˆã®å›ºå®šã•ã‚ŒãŸå¹…ã¯é‡è¦ã§ã™ã€‚
+
+{{{
+ Shoes.app do
+ flow do
+ stack :width => 200 do
+ caption "Column one"
+ para "is 200 pixels wide"
+ end
+ stack :width => -200 do
+ caption "Column two"
+ para "is 100% minus 200 pixels wide"
+ end
+ end
+ end
+}}}
+
+スロットã®å›ºå®šã•ã‚ŒãŸé«˜ã•ã¯ã€ã‚ˆã‚Šä¸€èˆ¬çš„ã§ã¯ã‚ã‚Šã¾ã›ã‚“。通常ã¯ã€ãƒ†ã‚­ã‚¹ãƒˆã‚„ç”»åƒãŒã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã®ä¸‹ã¸ã§ãã‚‹ã ã‘æµã‚Œã‚‹ã“ã¨ã‚’望ã¿ã¾ã™ã€‚高ã•ã¯é€šå¸¸ã¯è‡ªç„¶ã«æ±ºå®šã—ã¾ã™ã€‚
+
+ã“ã“ã§é‡è¦ãªã“ã¨ã¯ã€å›ºå®šã•ã‚ŒãŸé«˜ã•ãŒå®Ÿéš›ã¯ã‚¹ãƒ­ãƒƒãƒˆã«é•ã†ã‚ˆã†ã«æŒ¯ã‚‹èˆžã‚ã›ã‚‹ã“ã¨ã§ã™ã€‚確ã‹ã«æœ€å¾Œã«ã¯ã‚¹ãƒ­ãƒƒãƒˆã¯å®Œå…¨ã«åˆ‡ã‚Šé›¢ãªã•ã‚Œã¦ã€'''入れå­ã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦'''ã«ãªã‚Šã¾ã™ã€‚æ–°ã—ã„レイヤーã¯ã‚ªãƒšãƒ¬ãƒ¼ãƒ†ã‚£ãƒ³ã‚°ã‚·ã‚¹ãƒ†ãƒ ã«ã‚ˆã£ã¦ã‚¹ãƒ­ãƒƒãƒˆã‚’一定ã®æ­£æ–¹å½¢ã«ä¿ã¤ãŸã‚ã«ä½œæˆã•ã‚Œã¾ã™ã€‚
+
+通常ã®ã‚¹ãƒ­ãƒƒãƒˆã¨å…¥ã‚Œå­ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨ã®é•ã„ã¯ã€å¾Œè€…ã¯ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ãƒãƒ¼ã‚’æŒã¦ã‚‹ã“ã¨ã§ã™ã€‚
+
+{{{
+ Shoes.app do
+ stack :width => 200, :height => 200, :scroll => true do
+ background "#DFA"
+ 100.times do |i|
+ para "Paragraph No. #{i}"
+ end
+ end
+ end
+}}}
+
+ã“れらã®å…¥ã‚Œå­ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã¯ã‚ˆã‚Šå¤šãã®ãƒ¡ãƒ¢ãƒªã‚’å¿…è¦ã¨ã—ã¾ã™ã€‚彼らã¯ã‚¢ãƒ—リケーションã«ã‚‚ã†å°‘ã—è² æ‹…ã‚’ã‹ã‘ã¾ã™ã€‚ã‚‚ã—ã‚ãªãŸãŒã€å›ºå®šã•ã‚ŒãŸé«˜ã•ã®ãŸãã•ã‚“ã®ã‚¹ãƒ­ãƒƒãƒˆã§é…ã•ã‚’経験ã—ã¦ã„ã‚‹ãªã‚‰ã€é•ã†ã‚¢ãƒ—ローãƒã‚’試ã—ã¦ãã ã•ã„。
+
+==== ç”»åƒã¨å½¢çŠ¶ã®ãƒ–ロック ====
+
+多ãã®åˆå¿ƒè€…ãŒå½¢çŠ¶ã§ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’散らã‹ã—始ã‚ã¾ã™ã€‚ã™ã¹ã¦ã®é•·æ–¹å½¢ã‚„楕円形をスロットã¸æŠ•ã’入れるã®ã¯ç¢ºã‹ã«ç°¡å˜ã§ã™ã€‚
+
+'''ã—ã‹ã—ãªãŒã‚‰ã€Shoesã¯ãれらã™ã¹ã¦ã®å½¢çŠ¶ã®ãŸã‚ã«ã‚ªãƒ–ジェクトを生æˆã™ã‚‹ã“ã¨ã‚’覚ãˆã¦ãŠã„ã¦ä¸‹ã•ã„ï¼ã€‚'''
+
+{{{
+ Shoes.app do
+ fill black(0.1)
+ 100.times do |i|
+ oval i, i, i * 2
+ end
+ end
+}}}
+
+ã“ã®ä¾‹ã§ã¯ã€100個ã®æ¥•å††å½¢ã‚ªãƒ–ジェクトãŒç”Ÿæˆã•ã‚Œã¾ã™ã€‚ã“ã‚Œã¯æ‚ªã™ãŽã‚‹ã‚ã‘ã§ã¯ã‚ã‚Šã¾ã›ã‚“。ã—ã‹ã—ã€ä¸€ã¤ã®å½¢çŠ¶ã®ä¸­ã«ã“れらを作æˆã™ã‚‹ã®ãªã‚‰ã€ã“ã‚Œã¯ã‚ˆã‚Šè»½é‡ã«ãªã‚‹ã§ã—ょã†ã€‚
+
+{{{
+ Shoes.app do
+ fill black(0.1)
+ shape do
+ 100.times do |i|
+ oval i, i, i * 2
+ end
+ end
+ end
+}}}
+
+ã‚ã‚ã€å¾…ã£ã¦ãã ã•ã„。ã“ã®æ¥•å††å½¢ã¯ä»Šå›žã¯æº€ãŸã•ã‚Œã¾ã›ã‚“。ãªãœãªã‚‰ã€ã“ã®å½¢çŠ¶ãŸã¡ã¯ä¸€ã¤ã®å¤§ããªå½¢çŠ¶ã«çµåˆã•ã‚ŒãŸã‹ã‚‰ã§ã™ã€‚ãã—ã¦ã€ã“ã®ã‚±ãƒ¼ã‚¹ã§ã¯Shoesã¯ã©ã“を満ãŸã™ã¹ãã‹ã€åˆ†ã‹ã‚Šã¾ã›ã‚“。
+
+ãã—ã¦ã€ã‚¢ã‚¦ãƒˆãƒ©ã‚¤ãƒ³ã‚’厳密ã«æ‰±ã†ã¨ãã€é€šå¸¸ã¯ä¸€ã¤ã®å½¢çŠ¶ã«çµåˆã™ã‚‹ã“ã¨ã‚’望ã¿ã¾ã™ã€‚
+
+別ã®ã‚ªãƒ—ションã§ã¯ã€ã“れらã™ã¹ã¦ã®æ¥•å††å½¢ã‚’一ã¤ã®ç”»åƒã«çµåˆã—ã¾ã™ã€‚
+
+{{{
+ Shoes.app do
+ fill black(0.1)
+ image 300, 300 do
+ 100.times do |i|
+ oval i, i, i * 2
+ end
+ end
+ end
+}}}
+
+ãã†ã—よã†ï¼ãã®æ¥•å††å½¢ã¯ã™ã¹ã¦ä¸€ã¤ã®300 x 300ã®ç”»åƒã«çµåˆã•ã‚Œã¾ã™ã€‚ã“ã®å ´åˆã§ã¯ã€ãã®ç”»åƒã‚’メモリã«ä¿ç®¡ã™ã‚‹ã®ã¯ã€ãŠãらã100個ã®æ¥•å††å½¢ã‚’æŒã¤ã‚ˆã‚Šã¯ã‚‹ã‹ã«å¤§ãããªã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。ã—ã‹ã—ã€ä½•åƒã‚‚ã®å½¢çŠ¶ã‚’扱ã†å ´åˆã«ã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ãƒ–ロックã¯ã‚ˆã‚Šå®‰ã£ã½ããªã‚‹å¯èƒ½æ€§ãŒã‚ã‚Šã¾ã™ã€‚
+
+ãƒã‚¤ãƒ³ãƒˆã¯ä»¥ä¸‹ã®é€šã‚Šã§ã™ï¼šç”»åƒã‚„ブロックã¸å½¢çŠ¶ã‚’グループ化ã™ã‚‹ã“ã¨ã¯ç°¡å˜ã§ã™ã®ã§ã€ã‚‚ã—速度を得よã†ã¨ã™ã‚‹ã®ãªã‚‰ã€ãれを試ã—ã¦ãã ã•ã„。形状ブロックã¯ç‰¹ã«ãƒ¡ãƒ¢ãƒªã¨é€Ÿåº¦ã‚’節約ã•ã›ã‚‹ã§ã—ょã†ã€‚
+
+==== ã©ã“ã§ã‚‚UTF-8 ====
+
+Ruby自体ã¯Unicodeã‚’æ„è­˜ã—ã¾ã›ã‚“。ãã—ã¦ã€UTF-8ã¯ä¸€ç¨®ã®Unicodeã§ã™ã€‚(UTF-8ã®å®Œå…¨ãªèª¬æ˜Žã¯[[http://en.wikipedia.org/wiki/UTF-8 Wikipedia]]を見ã¦ãã ã•ã„。)
+
+ã—ã‹ã—ãªãŒã‚‰ã€UTF-8ã¯WEBã§ä¸€èˆ¬çš„ã§ã™ã€‚ãã—ã¦ã€å¤šãã®ç•°ãªã£ãŸãƒ—ラットホームãŒãれをサãƒãƒ¼ãƒˆã—ã¾ã™ã€‚ãã“ã§ã€ShoesãŒã—ãªãã¦ã¯ãªã‚‰ãªã„変æ›ã®é‡ã‚’減らã™ãŸã‚ã«ã€Shoesã¯ã™ã¹ã¦ã®æ–‡å­—列ãŒUTF-8フォーマットã§ã‚ã‚‹ã“ã¨ã‚’期待ã—ã¾ã™ã€‚
+
+ã™ã°ã‚‰ã—ã„ã“ã¨ã«ã€Shoesã§UTF-8を使ãˆã°ç„¡æ•°ã®è¨€èªžï¼ˆãƒ­ã‚·ã‚¢èªžã€æ—¥æœ¬èªžã€ã‚¹ãƒšã‚¤ãƒ³èªžã€è‹±èªžï¼‰ã‚’表示ã“ã¨ãŒã§ãã¾ã™ã€‚テキストエディタã§UTF-8を使用ã™ã‚‹ã“ã¨ã ã‘を確èªã—ã¦ãã ã•ã„!
+
+実例を示ã—ã¾ã™ï¼š
+
+{{{
+ Shoes.app do
+ stack :margin => 10 do
+ @edit = edit_box :width => 1.0 do
+ @para.text = @edit.text
+ end
+ @para = para ""
+ end
+ end
+}}}
+
+ã“ã®ã‚¢ãƒ—リケーションã¯ä½•ã§ã‚‚コピーã—ã¦ç·¨é›†ãƒœãƒƒã‚¯ã‚¹ã«è²¼ã‚Šä»˜ã‘ã¦ã€Shoesパラグラフã§è¡¨ç¤ºã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚外国語(ギリシャ語ã‹æ—¥æœ¬èªžã®ã‚ˆã†ãªï¼‰ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’ã“ã®ãƒœãƒƒã‚¯ã‚¹ã«ã‚³ãƒ”ーã—ã¦ã€ã©ã®ã‚ˆã†ã«è¡¨ç¤ºã•ã‚Œã‚‹ã‹ã‚’見るã“ã¨ãŒã§ãã¾ã™ã€‚
+
+ã“ã‚Œã¯ã€ãã®ç·¨é›†ãƒœãƒƒã‚¯ã‚¹ãŒUTF-8ã®æ–‡å­—ã‚’è¿”ã™ã“ã¨ã‚’確ã‹ã‚ã‚‹ã®ã«ã„ã„テストã§ã™ã€‚ãã—ã¦ã€ãã®ãƒ‘ラグラフã¯ã©ã‚“ãªUTF-8ã®æ–‡å­—ã§ã‚‚設定ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+'''é‡è¦äº‹é …:'''ã‚‚ã—ã„ãã¤ã‹ã®UTF-8ã®æ–‡å­—ãŒè¡¨ç¤ºã•ã‚Œãªã„ãªã‚‰ã€ãƒ‘ラグラフã®ãƒ•ã‚©ãƒ³ãƒˆã‚’変更ã™ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ã€‚ã“ã‚Œã¯ç‰¹ã«OS Xã§ä¸€èˆ¬çš„ã§ã™ã€‚
+
+ãã—ã¦ã€OS Xã§ã®ãŠã™ã™ã‚ã®æ—¥æœ¬èªžãƒ•ã‚©ãƒ³ãƒˆã¯'''AppleGothic'''ã§ã™ã€‚Windowsã§ã¯'''MS UI Gothic'''ã§ã™ã€‚
+
+{{{
+ Shoes.app do
+ para "ã¦ã™ã¨ (te-su-to)", :font => case RUBY_PLATFORM
+ when /mingw/; "MS UI Gothic"
+ when /darwin/; "AppleGothic, Arial"
+ else "Arial"
+ end
+ end
+}}}
+
+ã•ã‚‰ã«ã€Shoesã§æ–‡å­—列を扱ã†å ´åˆã‚‚UTF-8ã®æ–‡å­—列を必è¦ã¨ã—ã¾ã™ã€‚編集ボックスã€ç·¨é›†ãƒ©ã‚¤ãƒ³ã€ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚¿ã‚¤ãƒˆãƒ«ã‚„テキストブロックã¯ã™ã¹ã¦UTF-8ã‚’ã¨ã‚Šã¾ã™ã€‚é–“é•ã£ãŸæ–‡å­—ã®å…¥ã£ãŸæ–‡å­—列をã‚ãŸãˆãŸå ´åˆã¯ã€ã‚³ãƒ³ã‚½ãƒ¼ãƒ«ã«ã‚¨ãƒ©ãƒ¼ãŒè¡¨ç¤ºã•ã‚Œã¾ã™ã€‚
+
+==== メインアプリケーションã¨Require ====
+
+'''注æ„:''' ã“ã®ãƒ«ãƒ¼ãƒ«ã¯Raisinsã®ãŸã‚ã®ã‚‚ã®ã§ã™ã€‚Policemanã§ã¯TOPLEVEL_BINDINGを使ã£ã¦ã„ã‚‹ã®ã§ã€æœ€åˆã®ä¾‹ã§ã¯ `main` (Rubyトップレベルオブジェクト)ãŒè¡¨ç¤ºã•ã‚Œã¾ã™ã€‚`Shoes.app`ブロックã®å¤–å´ã§ã¯ã€`Para`ã§ã¯ãªã`Shoes::Para`ã¨è¨˜è¿°ã™ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ãŒã€‚
+
+
+ãã‚Œãžã‚Œã®Shoesアプリケーションã¯ã€ãれ自体を作るã“ã¨ãŒã§ãã‚‹å°ã•ãªéƒ¨å±‹ã‚’与ãˆã‚‰ã‚Œã¾ã™ã€‚クラスを作æˆã—ãŸã‚Šå¤‰æ•°ã‚’設定ã§ãã¾ã™ãŒã€ãれらã¯ä»–ã®Shoesプログラムã‹ã‚‰è¦‹ã‚‹ã“ã¨ã¯ã§ãã¾ã›ã‚“。ãã‚Œãžã‚Œã®ãƒ—ログラムã¯ãれ自身ã®åŒ¿åクラス内ã§å®Ÿè¡Œã•ã‚Œã¾ã™ã€‚
+
+{{{
+ main = self
+ Shoes.app do
+ para main.to_s
+ end
+}}}
+
+ã“ã®åŒ¿åクラスã¯`(shoes)`ã¨å‘¼ã°ã‚Œã€ãã‚Œã¯ç©ºã®ç„¡åクラスã§ã™ã€‚`Shoes`モジュールã¯ï¼ˆ`include Shoes`を利用ã—ã¦ï¼‰ã“ã®ã‚¯ãƒ©ã‚¹ã«ãƒŸãƒƒã‚¯ã‚¹ã‚¤ãƒ³ã•ã‚Œã¦ã„ã‚‹ãŸã‚ã€ãƒ‘ラグラフクラスをå‚ç…§ã—ã¦ã„ã‚‹ã¨ãã«`Para`ã‚„`Shoes::Para`を利用ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+ã“ã®ã‚¢ãƒ—ローãƒã®é•·æ‰€ã¯ï¼š
+
+ * Shoesアプリケーションã¯ãƒ­ãƒ¼ã‚«ãƒ«å¤‰æ•°ã‚’共有ã§ãã¾ã›ã‚“。
+ * メインアプリケーションコードã«ä½œæˆã•ã‚Œã‚‹ã‚¯ãƒ©ã‚¹ã¯ä¸€æ™‚çš„ã§ã™ã€‚
+ * Shoesモジュールã¯ã€Ruby自身ã®ãƒˆãƒƒãƒ—レベル環境ã§ã¯ãªãã€åŒ¿åクラスã«ãƒŸãƒƒã‚¯ã‚¹ã‚¤ãƒ³ã•ã‚Œã‚‹ã“ã¨ãŒã§ãã¾ã™ã€
+ * ガベージコレクションãŒä¸€åº¦å®Œäº†ã™ã‚Œã°ã€ã‚¢ãƒ—リケーションを完全ã«ãã‚Œã„ã«ã§ãã¾ã™ã€‚
+
+二ã¤ç›®ã®éƒ¨åˆ†ã¯ç‰¹ã«é‡è¦ãªã®ã§å¿˜ã‚Œãªã„ã“ã¨ã€‚
+
+{{{
+ class Storage; end
+
+ Shoes.app do
+ para Storage.new
+ end
+}}}
+
+アプリケーションãŒå®Œäº†ã™ã‚Œã°`Storage`クラスã¯æ¶ˆãˆã¾ã™ã€‚ã»ã‹ã®ã‚¢ãƒ—リケーションã¯Storageクラスを利用ã§ãã¾ã›ã‚“。ãã—ã¦ã€ãã‚Œã¯`require`を利用ã—ã¦ãƒ­ãƒ¼ãƒ‰ã•ã‚Œã‚‹ãƒ•ã‚¡ã‚¤ãƒ«ã‹ã‚‰æ‰‹ã«å…¥ã‚Œã‚‹ã“ã¨ã¯ã§ãã¾ã›ã‚“。
+
+ã‚‚ã£ã¨ã‚‚ã€`require`ã™ã‚‹ã¨ããã®ã‚³ãƒ¼ãƒ‰ã¯è¿‘ãã«ã„ã¾ã™ã€‚ãã‚Œã¯Rubyã®ãƒˆãƒƒãƒ—レベル環境ã«ä¿æŒã•ã‚Œã¾ã™ã€‚
+
+ãã—ã¦ã€ã“ã®è¦å‰‡ã¯ï¼š'''アプリケーションã®ã‚³ãƒ¼ãƒ‰ã«ä¸€æ™‚çš„ãªã‚¯ãƒ©ã‚¹ã‚’ä¿æŒã—ã€requireã«æ°¸ç¶šçš„ãªã‚¯ãƒ©ã‚¹ã‚’ä¿æŒã—ãªã•ã„'''ã§ã™ã€‚
+
+= Shoes =
+
+Shoesã¯ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚„ã€ãれらウィンドウ内部ã®è¦ç´ ã‚’æãã“ã¨ãŒã™ã¹ã¦ã§ã™ã€‚今ã¯ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦è‡ªä½“ã«ç„¦ç‚¹ã‚’当ã¦ã¾ã—ょã†ã€‚ä»–ã®ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã®[[Slots]]ã‚„[[Elements]]ãŒã‚¦ã‚£ãƒ³ãƒ‰ã‚¦å†…部ã«é–¢ã—ã¦ã®ã™ã¹ã¦ã‚’対象ã¨ã—ã¦ã„ã¾ã™ã€‚
+
+ã“ã“ã§ã¯ã€ã“ã®ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã¯ã‚ˆã‚Šè¾žæ›¸ã®ã‚ˆã†ã«èª­ã‚“ã§ãã ã•ã„。ãã‚Œãžã‚Œã®ãƒšãƒ¼ã‚¸ã®ã»ã¨ã‚“ã©ã¯ã€åˆ©ç”¨å¯èƒ½ãªãƒ¡ã‚½ãƒƒãƒ‰ã®ä¸€è¦§ã§ã‚ã‚Šã€å„トピックを対象ã¨ã—ã¦ã„ã¾ã™ã€‚ã“ã®è€ƒãˆã¯ã€ã™ã¹ã¦ã«é–¢ã—ã¦ã¨ã¦ã‚‚詳細ã‹ã¤æ˜Žç¢ºã§ã‚ã‚‹ã“ã¨ã§ã™ã€‚
+
+ãã—ã¦ã€ã“ã®ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã®é›£ã—ã•ã«ã¶ã¤ã‹ã£ã¦ã€å§‹ã‚ã‚‹ã“ã¨ã«ã¤ã„ã¦ã¾ã åˆ†ã‹ã‚‰ãªã„ãªã‚‰ã€ãŠãらãã“ã®ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã®[[Hello! beginning]]ã«æˆ»ã‚‹ã¹ãã§ã™ã€‚
+ã¾ãŸã¯ã€ã‚¦ã‚§ãƒ–上ã®åˆå¿ƒè€…ã®ãƒªãƒ¼ãƒ•ãƒ¬ãƒƒãƒˆã§ã‚ã‚‹[[http://github.com/shoes/shoes/downloads Nobody Knows Shoes]]を試ã—ã¦ãã ã•ã„。
+
+==== 方法ã®è¦‹ã¤ã‘æ–¹ ====
+
+ã“ã®ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã¯ä»¥ä¸‹ã‚’対象ã¨ã—ã¾ã™ï¼š
+
+ * [[Built-in Built-in methods]] - Shoesプログラムã®ã©ã“ã§ã‚‚利用ã§ãる一般的ãªãƒ¡ã‚½ãƒƒãƒ‰ã€‚
+ * [[App The App window]] - Shoesã®ã™ã¹ã¦ã®ãƒ¡ã‚¤ãƒ³ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã«æ·»ä»˜ã•ã‚ŒãŸãƒ¡ã‚½ãƒƒãƒ‰ã€‚
+ * [[Styles The Styles Master List]] - Shoesã®ã™ã¹ã¦ã®ã‚¹ã‚¿ã‚¤ãƒ«ã®å®Œå…¨ãªä¸€è¦§ã€‚
+ * [[Classes The Classes list]] - Shoesã®ã‚¯ãƒ©ã‚¹ã‚„サブクラスã«ã¤ã„ã¦è¡¨ç¤ºã—ã¦ã„る表。
+ * [[Colors The Colors list]] - ã™ã¹ã¦ã®ãƒ“ルトインカラーã¨[[Built-in.rgb]]ã«ãŠã‘ã‚‹ãã‚Œãžã‚Œã®æ•°ã®è¡¨ã€‚
+
+ページをよã見ã¦ã‚‚見ã¤ã‹ã‚‰ãªã„ã‚‚ã®ãŒã‚ã‚Œã°ã€[[Search]]ページを試ã—ã¦ãã ã•ã„。ãã‚Œã¯å•é¡Œã‚’ã•ã‘る手ã£å–ã‚Šæ—©ã„方法ã§ã™ã€‚
+
+ã“ã®ä¸€èˆ¬çš„ãªãƒªãƒ•ã‚¡ãƒ¬ãƒ³ã‚¹ã®ã‚ã¨ã«ã€ä»–ã®ï¼’ã¤ã®ç‰¹åˆ¥ãªã‚»ã‚¯ã‚·ãƒ§ãƒ³ãŒã‚ã‚Šã¾ã™ï¼š
+
+ * [[Slots]] - [[Element.stack]]ã¨[[Element.flow]]を対象ã¨ã™ã‚‹ã€ï¼’ã¤ã®ç¨®é¡žã®ã‚¹ãƒ­ãƒƒãƒˆã€‚
+ * [[Elements]] - ã™ã¹ã¦ã®ãƒœã‚¿ãƒ³ã€å½¢çŠ¶ã€ç”»åƒãªã©ã®ãŸã‚ã®ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆã€‚
+
+ã“ã“ã«[[Element Element Creation]]ページ(追加ã§ãã‚‹ã™ã¹ã¦ã®ã‚¨ãƒ¬ãƒ¡ãƒ³ãƒˆã®ä¸€è¦§ï¼‰ã¨[[Common Common Methods]] ページ(ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã‚„エレメントã«ã‚るメソッドã®ä¸€è¦§ï¼‰ã®ï¼’ã¤ã®ã¨ã¦ã‚‚大切ãªãƒšãƒ¼ã‚¸ãŒã‚ã‚Šã¾ã™ã€‚
+
+== Built-in Methods ==
+
+ã“れらã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯Shoesã®ãƒ—ログラムを通ã—ã¦ã©ã“ã§ã‚‚利用ã§ãã¾ã™ã€‚
+
+ã“れらã™ã¹ã¦ã®ã‚³ãƒžãƒ³ãƒ‰ã¯ã€ã‚ãªãŸãŒãƒ‰ãƒƒãƒˆã‚’彼らã«ä»˜ä¸Žã—ãªã„点ãŒçã—ã„ã§ã™ã€‚
+'''ã“ã®ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã®ã»ã‹ã®ã™ã¹ã¦ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ã‚ªãƒ–ジェクトã«ãƒ‰ãƒƒãƒˆã‚’付与ã™ã¹ãã§ã™ã€‚'''
+ã—ã‹ã—ã€ã“れらã®ãƒ“ルトインメソッド(カーãƒãƒ«ãƒ¡ã‚½ãƒƒãƒ‰ã¨ã‚‚呼ã°ã‚Œã¦ã„る)ã¯ãƒ‰ãƒƒãƒˆãŒãªã„ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚
+
+一般的ãªã‚‚ã®ã¨ã—ã¦`alert`ãŒã‚ã‚Šã¾ã™ï¼š
+
+{{{
+ #!ruby
+ alert "No dots in sight"
+}}}
+
+ã“ã‚Œã¨ã€ã‚«ãƒ¼ãƒãƒ«ãƒ¡ã‚½ãƒƒãƒ‰ã¯ãªãArrayã¨Stringã«å¯¾ã—ã¦ã ã‘利用å¯èƒ½ãª`reverse`メソッドを比較ã—ã¦ãã ã•ã„:
+
+{{{
+ #!ruby
+ "Plaster of Paris".reverse
+ #=> "siraP fo retsalP"
+ [:dogs, :cows, :snakes].reverse
+ #=> [:snakes, :cows, :dogs]
+}}}
+
+æ画やボタンを作æˆã—ãŸã‚Šã™ã‚‹ãŸã‚ã®å¤šãã®Shoesメソッドã¯ã‚¹ãƒ­ãƒƒãƒˆã¸ä»˜ä¸Žã•ã‚Œã¾ã™ã€‚より詳ã—ã„情報ã«ã¤ã„ã¦ã¯[[Slots]]ã®ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã‚’見ã¦ãã ã•ã„。
+
+==== ビルトイン定数 ====
+
+Shoesã«ã¯ã„ãã¤ã‹ã®ãƒ“ルトイン定数ãŒã‚ã‚Šã€ãã‚Œã¯ã©ã‚“ãªãƒãƒ¼ã‚¸ãƒ§ãƒ³ã®ShoesãŒå®Ÿè¡Œã•ã‚Œã¦ã„ã‚‹ã‹ã‚’判別ã™ã‚‹ã“ã¨ã‚’証明ã™ã‚‹ã®ã«åˆ©ç”¨ã§ãã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“
+
+'''Shoes::RELEASE_NAME''' Shoesリリースåã®æ–‡å­—列定数ã§ã™ã€‚Curiousã‹ã‚‰å§‹ã¾ã£ã¦ã€ã™ã¹ã¦ã®Shoesリリースã¯å付ã‘られã¾ã™ã€‚
+
+'''Shoes::RELEASE_ID''' Shoesリリース表ã™æ•°å­—ã‚’å«ã¿ã¾ã™ã€‚ãã—ã¦ã€ä¾‹ãˆã°Curiousã¯ãƒŠãƒ³ãƒãƒ¼1ã§ã‚ã‚Šã€ãã‚Œã¯åˆã‚ã¦ã®å…¬å¼ãƒªãƒªãƒ¼ã‚¹ã§ã™ã€‚
+
+'''Shoes::REVISION''' ã¯ã€ãã®ãƒ“ルドã®Subversionã®ãƒªãƒ“ジョン番å·ã§ã™ã€‚
+
+'''Shoes::FONTS''' ã¯ã€ã‚¢ãƒ—リケーションã§åˆ©ç”¨ã§ãるフォントã®å®Œå…¨ãªä¸€è¦§ã§ã™ã€‚ã“ã®ä¸€è¦§ã¯[[Built-in.font]]メソッドã«ã‚ˆã£ã¦ãƒ­ãƒ¼ãƒ‰ã•ã‚ŒãŸã™ã¹ã¦ã®ãƒ•ã‚©ãƒ³ãƒˆã‚’å«ã¿ã¾ã™ã€‚
+
+=== alert(message: a string) » nil ===
+
+短ã„メッセージをå«ã‚€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’ãƒãƒƒãƒ—アップã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ alert("I'm afraid I must interject!")
+}}}
+
+alertã¯ä¿¡ã˜ã‚‰ã‚Œãªã„ã»ã©ç…©ã‚ã—ã„ã®ã§æŽ§ãˆã‚ã«åˆ©ç”¨ã—ã¦ãã ã•ã„ï¼ãƒ—ログラムをデãƒãƒƒã‚°ã™ã‚‹æ‰‹åŠ©ã‘ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’表示ã™ã‚‹ãŸã‚ã«alertを利用ã™ã‚‹ãªã‚‰ã€[[Built-in.debug]]ã¾ãŸã¯[[Built-in.info]]メソッドを調ã¹ã¦ã¿ã¦ãã ã•ã„。
+
+=== ask(message: a string) » a string ===
+
+ウィンドウをãƒãƒƒãƒ—アップã—ã¦è³ªå•ã‚’ã—ã¾ã™ã€‚例ãˆã°ã€ã‚ãªãŸã¯èª°ã‹ã«åå‰ã‚’å°‹ã­ãŸã„ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。
+
+{{{
+ #!ruby
+ name = ask("Please, enter your name:")
+}}}
+
+上記ã®ã‚¹ã‚¯ãƒªãƒ—トを実行ã™ã‚‹ã¨ãã€ã‚³ãƒ³ãƒ”ュータを利用ã—ã¦ã„る人ã¯ã€åå‰ã‚’入力ã™ã‚‹ãŸã‚ã®ç©ºã®ãƒœãƒƒã‚¯ã‚¹ã‚’æŒã¤ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’見るã§ã—ょã†ã€‚ãã—ã¦ã€ãã®åå‰ã¯`name`変数ã«ä¿å­˜ã•ã‚Œã¾ã™ã€‚
+
+=== ask_color(title: a string) » Shoes::Color ===
+
+カラーピッカーウィンドウをãƒãƒƒãƒ—アップã—ã¾ã™ã€‚ã“ã®ãƒ—ログラムã¯è‰²ãŒé¸ã°ã‚Œã‚‹ã®ã‚’å¾…ã¡ã€ãã—ã¦ã‚ãªãŸã«è‰²ã‚ªãƒ–ジェクトを与ãˆã¾ã™ã€‚ã„ãã¤ã‹ã®æ–¹æ³•ã§ã“ã®è‰²ã‚’利用ã™ã‚‹ãŸã‚ã«`Color`ヘルプを見ã¦ãã ã•ã„。
+
+{{{
+ #!ruby
+ backcolor = ask_color("Pick a background")
+ Shoes.app do
+ background backcolor
+ end
+}}}
+
+=== ask_open_file() » a string ===
+
+"ファイルを開ã。。。"ウィンドウをãƒãƒƒãƒ—アップã—ã¾ã™ã€‚ã“ã‚Œã¯æ¨™æº–ã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã§ã‚ã‚Šã€ã™ã¹ã¦ã®ãƒ•ã‚©ãƒ«ãƒ€ã‚’表示ã—ã¦é–‹ãファイルをé¸æŠžã•ã›ã¾ã™ã€‚ãã—ã¦ãƒ•ã‚¡ã‚¤ãƒ«ã®åå‰ã‚’è¿”ã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ filename = ask_open_file
+ Shoes.app do
+ para File.read(filename)
+ end
+}}}
+
+=== ask_save_file() » a string ===
+
+ã“ã‚Œã¯å…ˆã»ã©è¿°ã¹ãŸ`ask_open_file`ã¨ä¼¼ã¦ãŠã‚Šã€"ファイルをä¿å­˜ã™ã‚‹ã€‚。。"ウィンドウをãƒãƒƒãƒ—アップã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ save_as = ask_save_file
+}}}
+
+=== ask_open_folder() » a string ===
+
+"フォルダを開ã。。。"ウィンドウをãƒãƒƒãƒ—アップã—ã¾ã™ã€‚ã“ã‚Œã¯ã€ã™ã¹ã¦ã®ãƒ•ã‚©ãƒ«ãƒ€ã‚’表示ã—ã€é–‹ãフォルダをé¸æŠžã•ã›ã‚‹æ¨™æº–ã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã§ã™ã€‚ã“ã‚Œã¯ã‚ãªãŸã«ãƒ•ã‚©ãƒ«ãƒ€ã®åå‰ã‚’渡ã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ folder = ask_open_folder
+ Shoes.app do
+ para Dir.entries(folder)
+ end
+}}}
+
+=== ask_save_folder() » a string ===
+
+ã“ã‚Œã¯å…ˆã»ã©è¿°ã¹ãŸ`ask_open_folder`ã¨ä¼¼ã¦ãŠã‚Šã€"フォルダをä¿å­˜ã™ã‚‹ã€‚。。"ウィンドウをãƒãƒƒãƒ—アップã—ã¾ã™ã€‚OS X上ã§ã¯ã€ç¾åœ¨ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯`ask_open_folder`ã®ã‚¨ã‚¤ãƒªã‚¢ã‚¹ã®ã‚ˆã†ã«ãªã£ã¦ã„ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ save_to = ask_save_folder
+}}}
+
+
+=== confirm(question: a string) » true or false ===
+
+yes-ã¾ãŸã¯-noã®è³ªå•ã‚’ãƒãƒƒãƒ—アップã—ã¾ã™ã€‚コンピュータã®å‰ã®äººãŒ'''yes'''をクリックã™ã‚‹ãªã‚‰ã€è¿”ã•ã‚Œã‚‹`true`ã‚’å—ã‘å–ã‚Šã¾ã™ã€‚ãã†ã§ã¯ç„¡ã„ãªã‚‰ã€è¿”ã•ã‚Œã‚‹`false`ã‚’å—ã‘å–ã‚Šã¾ã™ã€‚
+
+{{{
+ #!ruby
+ if confirm("Draw a circle?")
+ Shoes.app{ oval :top => 0, :left => 0, :radius => 50 }
+ end
+}}}
+
+=== debug(message: a string) » nil ===
+
+Shoesコンソールã¸ãƒ‡ãƒãƒƒã‚°ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’é€ã‚Šã¾ã™ã€‚
+ã©ã‚“ãªShoesウィンドウ上ã§ã‚‚ã€`Alt-/`(ã¾ãŸã¯ã€OS X上ã§ã¯`⌘-/`)を押ã™ã“ã¨ã«ã‚ˆã£ã¦ã€Shoesコンソールを立ã¡ä¸Šã’ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+{{{
+ #!ruby
+ debug("Running Shoes on " + RUBY_PLATFORM)
+}}}
+
+[[Built-in.error]]ã€[[Built-in.warn]]ã¨[[Built-in.info]]メソッドも確èªã—ã¦ãã ã•ã„。
+
+=== error(message: a string) » nil ===
+
+Shoesコンソールã¸ã‚¨ãƒ©ãƒ¼ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸é€ã‚Šã¾ã™ã€‚ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ã‚¨ãƒ©ãƒ¼ã‚’ログã™ã‚‹ãŸã‚ã ã‘ã«åˆ©ç”¨ã™ã¹ãã§ã™ã€‚自分ãŸã‚ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’ログã™ã‚‹ã«ã¯[[Built-in.debug]]メソッドを試ã—ã¦ãã ã•ã„。
+
+ãŠãŠã€ãã—ã¦ã€æ–‡å­—列よりもã€ç›´æŽ¥ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã«ä¾‹å¤–を手渡ã™ã¹ãã§ã™ã€‚ãã—ã¦ãã‚Œã¯é©åˆ‡ã«ãƒ•ã‚©ãƒ¼ãƒžãƒƒãƒˆã•ã‚Œã‚‹ã§ã—ょã†ã€‚
+
+=== exit() ===
+
+プログラムを止ã‚ã¾ã™ã€‚çªç„¶ã«çµ‚了ã—ãŸã„ã¨ãã¯ã„ã¤ã§ã‚‚ã€ã“れを呼んã§ãã ã•ã„。
+
+=== font(message: a string) » an array of font family names ===
+
+ファイルã‹ã‚‰TrueType(ã¾ãŸã¯ä»–ã®ç¨®é¡žã®ãƒ•ã‚©ãƒ³ãƒˆï¼‰ã‚’ロードã—ã¾ã™ã€‚TrueTypeã¯ã™ã¹ã¦ã®ãƒ—ラットフォームã§ã‚µãƒãƒ¼ãƒˆã•ã‚Œã‚‹ã¨ã¯ã„ãˆã€ã‚ãªãŸã®ãƒ—ラットフォームã¯ä»–ã®ç¨®é¡žã®ãƒ•ã‚©ãƒ³ãƒˆã‚’サãƒãƒ¼ãƒˆã™ã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。Shoesã¯ã“ã®å‹•ä½œã«ã€ãã‚Œãžã‚Œã®ã‚ªãƒšãƒ¬ãƒ¼ãƒ†ã‚£ãƒ³ã‚°ã‚·ã‚¹ãƒ†ãƒ ã®ãƒ“ルトインフォントシステムを利用ã—ã¾ã™ã€‚
+
+ã“ã“ã«ã©ã®ãƒ—ラットフォームã§ä½•ã®ãƒ•ã‚©ãƒ³ãƒˆãŒå‹•ä½œã™ã‚‹ã‹ã®ç›®å®‰ãŒã‚ã‚Šã¾ã™ã€‚
+
+ * Bitmap fonts (.bdf, .pcf, .snf) - Linux
+ * Font resource (.fon) - Windows
+ * Windows bitmap font file (.fnt) - Linux, Windows
+ * PostScript OpenType font (.otf) - Mac OS X, Linux, Windows
+ * Type1 multiple master (.mmm) - Windows
+ * Type1 font bits (.pfb) - Linux, Windows
+ * Type1 font metrics (.pfm) - Linux, Windows
+ * TrueType font (.ttf) - Mac OS X, Linux, Windows
+ * TrueType collection (.ttc) - Mac OS X, Linux, Windows
+
+フォントãŒé©åˆ‡ã«ãƒ­ãƒ¼ãƒ‰ã•ã‚ŒãŸãªã‚‰ã€ãƒ•ã‚¡ã‚¤ãƒ«ã«è¦‹ã¤ã‹ã£ãŸãƒ•ã‚©ãƒ³ãƒˆã®åå‰ã®é…列をå–り戻ã™ã§ã—ょã†ã€‚ãã†ã§ã¯ãªãã€ãƒ•ã‚¡ã‚¤ãƒ«ã«ã¯ãƒ•ã‚©ãƒ³ãƒˆãŒè¦‹ã¤ã‹ã‚‰ãªã„ãªã‚‰`nil`ãŒè¿”ã•ã‚Œã¾ã™ã€‚
+
+ã¾ãŸèˆˆå‘³æ·±ã„ã“ã¨ã«:`Shoes::FONTS`定数ã¯ã“ã®ãƒ—ラットフォームã§åˆ©ç”¨å¯èƒ½ãªãƒ•ã‚©ãƒ³ãƒˆã®å®Œå…¨ãªä¸€è¦§ã§ã™ã€‚`include?`を利用ã—ã¦ã„ãã¤ã‹ã®ãƒ•ã‚©ãƒ³ãƒˆã‚’ãƒã‚§ãƒƒã‚¯ã§ãã¾ã™ã€‚
+
+{{{
+ if Shoes::FONTS.include? "Helvetica"
+ alert "Helvetica is available on this system."
+ else
+ alert "You do not have the Helvetica font."
+ end
+}}}
+
+ã‚‚ã—フォントを表示ã™ã‚‹ã“ã¨ã«å•é¡ŒãŒã‚ã‚‹ãªã‚‰ã€ãれを利用ã™ã‚‹å‰ã«ã€ã‚¢ãƒ—リケーションãŒãƒ•ã‚©ãƒ³ãƒˆã‚’ロードã™ã‚‹ã“ã¨ã‚’確èªã—ã¦ãã ã•ã„。特ã«OS Xã§ã¯ã€ã‚‚ã—フォントãŒãƒ­ãƒ¼ãƒ‰ã•ã‚Œã‚‹å‰ã«åˆ©ç”¨ã•ã‚ŒãŸãªã‚‰ã€ãƒ•ã‚©ãƒ³ãƒˆã‚­ãƒ£ãƒƒã‚·ãƒ¥ã¯ãƒ­ãƒ¼ãƒ‰ã•ã‚ŒãŸãƒ•ã‚©ãƒ³ãƒˆã‚’無視ã™ã‚‹å‚¾å‘ãŒã‚ã‚Šã¾ã™ã€‚
+
+=== gradient(color1, color2) » Shoes::Pattern ===
+
+二ã¤ã®è‰²ã‹ã‚‰ç›´ç·šå‹¾é…を作りã¾ã™ã€‚ãã‚Œãžã‚Œã®è‰²ã«ã€è‰²ã‚’æç”»ã™ã‚‹ãŸã‚ã«Shoes::Colorオブジェクトã‹æ–‡å­—列を渡ã—ã¾ã™ã€‚
+
+=== gray(the numbers: darkness, alpha) » Shoes::Color ===
+
+æš—ã•ã®ãƒ¬ãƒ™ãƒ«ã‚„ã€ä»»æ„çš„ã«ã¯ã‚¢ãƒ«ãƒ•ã‚¡ãƒ¬ãƒ™ãƒ«ã‹ã‚‰ã‚°ãƒ¬ãƒ¼ã‚¹ã‚±ãƒ¼ãƒ«ã‚«ãƒ©ãƒ¼ã‚’作æˆã—ã¾ã™ã€‚
+
+{{{
+ black = gray(0.0)
+ white = gray(1.0)
+}}}
+
+=== info(message: a string) » nil ===
+
+Shoesコンソールã§ãƒ¦ãƒ¼ã‚¶ã¸ã®æƒ…報をå«ã‚€ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’記録ã—ã¾ã™ã€‚ãã—ã¦ã€ãã®ãƒ‡ãƒãƒƒã‚°ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã¯ãƒ—ログラムã§ä½•ãŒç™ºç”Ÿã—ãŸã®ã‹è¦‹ã¤ã‘ã‚‹ã“ã¨ã‚’助ã‘るよã†ã«ãƒ‡ã‚¶ã‚¤ãƒ³ã•ã‚Œã¦ãŠã‚Šã€`info`メッセージã¯ãƒ—ログラムã«ã¤ã„ã¦ãƒ¦ãƒ¼ã‚¶ã«è¿½åŠ ã®æƒ…報を教ãˆã¾ã™ã€‚
+
+{{{
+ #!ruby
+
+ info("You just ran the info example on Shoes #{Shoes::RELEASE_NAME}.")
+}}}
+
+例ãˆã°ã€Shyファイルをロードã™ã‚Œã°ã„ã¤ã§ã‚‚ã€Shoesã¯ã‚³ãƒ³ã‚½ãƒ¼ãƒ«ã«Shyã®è‘—者ã¨ãƒãƒ¼ã‚¸ãƒ§ãƒ³ã®æƒ…報をå«ã‚€ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’å°å­—ã—ã¾ã™ã€‚
+
+=== rgb(a series of numbers: red, green, blue, alpha) » Shoes::Color ===
+
+赤ã€ç·‘ã€é’ã®æ§‹æˆè¦ç´ ã‹ã‚‰è‰²ã‚’作æˆã—ã¾ã™ã€‚アルファレベル(é€æ˜Žåº¦ã‚’示ã™ï¼‰ã¯ä»»æ„ã«åŠ ãˆã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+整数を渡ã™ã¨ãã¯ã€0ã‹ã‚‰255ã¾ã§ã®å€¤ã‚’利用ã—ã¦ãã ã•ã„。
+
+{{{
+ blueviolet = rgb(138, 43, 226)
+ darkgreen = rgb(0, 100, 0)
+}}}
+
+ã¾ãŸã¯ã€0.0ã‹ã‚‰1.0ã¾ã§ã®10進数を利用ã—ã¦ãã ã•ã„。
+
+{{{
+ blueviolet = rgb(0.54, 0.17, 0.89)
+ darkgreen = rgb(0, 0.4, 0)
+}}}
+
+ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯`Shoes.rgb`ã¨å‘¼ã°ã‚Œã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。
+
+=== warn(message: a string) » nil ===
+
+ユーザã®ãŸã‚ã«è­¦å‘Šã‚’記録ã—ã¾ã™ã€‚警告ã¯å£Šæ»…çš„ãªã‚¨ãƒ©ãƒ¼ï¼ˆãã‚Œã¯[[Built-in.error]]を見ã¦ãã ã•ã„。)ã§ã¯ã‚ã‚Šã¾ã›ã‚“。ã“ã‚Œã¯ã€ãƒ—ログラムãŒå°†æ¥å¤‰åŒ–ã—ãŸã‚Šã€ãƒ—ログラムã®ä¸€éƒ¨ãŒä¿¡é ¼ã§ããªããªã‚‹ãªã©ã®é€šçŸ¥ã§ã™ã€‚
+
+警告やエラーを見るãŸã‚ã«ã¯ã€`Alt-/`(OS Xã®å ´åˆã¯`⌘-/`)ã«ã‚ˆã‚ŠShoesコンソールを開ã„ã¦ãã ã•ã„。
+
+== The App Object ==
+
+アプリケーションã¯URLã§ã‚³ãƒ¼ãƒ‰ã‚’実行ã™ã‚‹ä¸€ã¤ã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã§ã™ã€‚URLを切り替ãˆã‚‹ã¨ãã€æ–°ã—ã„アプリケーションオブジェクトãŒä½œæˆã•ã‚Œã€ã‚¹ã‚¿ãƒƒã‚¯ã€ãƒ•ãƒ­ãƒ¼ã‚„ä»–ã®è¦ç´ ã§æº€ãŸã•ã‚Œã¾ã™ã€‚
+
+アプリケーションã¯ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦è‡ªä½“ã§ã™ã€‚ãã‚Œã¯é–‰ã˜ã‚‰ã‚Œã‚‹ã‹ã€ã‚¯ãƒªã‚¢ã•ã‚Œã‚‹ã‹ã€æ–°ã—ã„è¦ç´ ã§æº€ãŸã•ã‚Œã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。!{:margin_left => 100}man-app.png!
+
+スロットï¼ãƒœãƒƒã‚¯ã‚¹ã®ç”¨èªžã§ã¯ã€ã‚¢ãƒ—リケーション自体ãŒãƒ•ãƒ­ãƒ¼ã§ã™ã€‚詳ã—ãã¯''Slots''セクションを見ã¦ãã ã•ã„ã€ã—ã‹ã—ã€ã“ã‚Œã¯ã©ã‚“ãªè¦ç´ ã‚‚直接フローã®ãƒˆãƒƒãƒ—レベルã«ç½®ã‹ã‚Œã‚‹ã“ã¨ã‚’å˜ã«æ„味ã—ã¾ã™ã€‚
+
+=== Shoes.app(styles) { ... } » Shoes::App ===
+
+Shoesã®ã‚¢ãƒ—リケーションウィンドウを開始ã—ã¾ã™ã€‚ã“ã‚Œã¯Shoesプログラムを作るãŸã‚ã®å‡ºç™ºåœ°ç‚¹ã§ã™ã€‚ブロックã®å†…部ã§ã¯ã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’様々ãªShoesã®è¦ç´ ï¼ˆãƒœã‚¿ãƒ³ã€ã‚¢ãƒ¼ãƒˆãƒ¯ãƒ¼ã‚¯ã€ãã®ä»–)ã§æº€ãŸã—ã€ãã—ã¦ãƒ–ロックã®å¤–ã§ã¯ã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ãŒã©ã‚Œãらã„大ãã„ã®ã‹ã‚’説明ã™ã‚‹ãŸã‚ã«`styles`を利用ã—ã¾ã™ã€‚ãŠãらãアプリケーションã®åå‰ã‚„ã€ãã‚ŒãŒãƒªã‚µã‚¤ã‚ºå¯èƒ½ã‹ã©ã†ã‹ã«ã¤ã„ã¦ã‚‚ã§ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app(:title => "White Circle",
+ :width => 200, :height => 200, :resizable => false) {
+ background black
+ fill white
+ oval :top => 20, :left => 20, :radius => 160
+ }
+}}}
+
+上記ã®ã‚±ãƒ¼ã‚¹ã§ã¯ã€å°ã•ãªã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’作æˆã—ã¾ã™ã€‚200×200ピクセルã§ã™ã€‚ãã‚Œã¯ãƒªã‚µã‚¤ã‚ºä¸å¯èƒ½ã§ã™ã€‚ãã—ã¦ã€ãã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã«ã¯é»’ã„背景ã¨ç™½ã„輪ã®2ã¤ã®è¦ç´ ãŒã‚ã‚Šã¾ã™ã€‚
+
+ã„ã£ãŸã‚“アプリケーションãŒä½œæˆã•ã‚Œã‚Œã°ã€ãã‚Œã¯[[App.Shoes.APPS]]ã®ä¸€è¦§ã«è¿½åŠ ã•ã‚Œã¾ã™ã€‚ã‚‚ã—ã‚ãªãŸãŒã‚ˆã‚Šå¤šãã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’生æˆã—ãŸã„ãªã‚‰ã€[[Element.window]]メソッドや[[Element.dialog]]メソッドを見ã¦ãã ã•ã„。
+
+=== Shoes.APPS() » An array of Shoes::App objects ===
+
+ç¾åœ¨é–‹ã„ã¦ã„ã‚‹ã™ã¹ã¦ã®Shoesアプリケーションã®å®Œå…¨ãªä¸€è¦§ã‚’作æˆã—ã¾ã™ã€‚ã„ã£ãŸã‚“アプリケーションãŒé–‰ã˜ã‚‰ã‚Œã‚‹ã¨ã€ãã®ä¸€è¦§ã‹ã‚‰å–り除ã‹ã‚Œã¾ã™ã€‚ãã†ã€Shoesã§ã¯ä¸€åº¦ã«å¤šãã®å®Ÿè¡Œã§ãã¾ã™ã€‚ãã‚Œã¯ã¨ã¦ã‚‚元気付ã‘られã¾ã™ã€‚
+
+=== clipboard() » a string ===
+
+システムã®ã‚¯ãƒªãƒƒãƒ—ボードã®ã™ã¹ã¦ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’å«ã‚€æ–‡å­—列を返ã—ã¾ã™ã€‚ã“ã‚Œã¯ã‚³ãƒ³ãƒ”ュータ上ã®ã©ã®ãƒ—ログラムã‹ã‚‰ã§ã‚‚カットアンドペーストã§ãるグローãƒãƒ«ã‚¯ãƒªãƒƒãƒ—ボードã§ã™ã€‚
+
+=== clipboard = a string ===
+
+システムクリップボードã«`a string`ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’ä¿å­˜ã—ã¾ã™ã€‚
+
+=== close() ===
+
+アプリケーションã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’é–‰ã˜ã¾ã™ã€‚複数ã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’é–‹ã„ã¦ã„ã¦ã€ã™ã¹ã¦ã®ã‚¢ãƒ—リケーションを閉ã˜ãŸã„ãªã‚‰ã€ãƒ“ルトインメソッドã®`exit`を利用ã—ã¦ãã ã•ã„。
+
+=== download(url: a string, styles) ===
+
+ダウンロードã®ã‚¹ãƒ¬ãƒƒãƒ‰ï¼ˆã‚ãªãŸãŒJavaScriptã«è©³ã—ã„ã®ãªã‚‰ã€ãŠã‚ˆãXMLHttpRequestã®ã‚ˆã†ãªã‚‚ã®ã§ã™ï¼‰ã‚’開始ã—ã¾ã™ã€‚ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ã€ã™ãã«æˆ»ã‚Šå€¤ã‚’è¿”ã—ã¦ãƒãƒƒã‚¯ã‚°ãƒ©ã‚¦ãƒ³ãƒ‰ã§ãƒ€ã‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ã‚’開始ã—ã¾ã™ã€‚ã¾ãŸã€ãã‚Œãžã‚Œã®ãƒ€ã‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ã‚¹ãƒ¬ãƒƒãƒ‰ãŒ`start`ã€`progress`ã‚„`finish`イベントを開始ã—ã¾ã™ã€‚
+downloadã«ãƒ•ã‚¡ã‚¤ãƒ«ã‚’é€ã‚‹ã“ã¨ã‚„ã€ï¼ˆ`finish`イベントã®ä¸­ã§ï¼‰æ–‡å­—列をå–り戻ã™ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+downloadã«ãƒ–ロックを付ã‘ã‚‹ã¨ã€ãã‚Œã¯`finish`イベントã¨ã—ã¦å‘¼ã°ã‚Œã¾ã™ã€‚
+
+download
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ title "Searching Google", :size => 16
+ @status = para "One moment..."
+
+ # Search Google for 'shoes' and print the HTTP headers
+ download "http://www.google.com/search?q=shoes" do |goog|
+ @status.text = "Headers: " + goog.response.headers.inspect
+ end
+ end
+ end
+}}}
+
+ãã—ã¦ã€ãƒ€ã‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ã—ãŸãƒ‡ãƒ¼ã‚¿ã‚’利用ã—ãŸã„ãªã‚‰`goog.response.body`を利用ã™ã‚‹ã“ã¨ã«ã‚ˆã‚Šè¡Œã„ã¾ã™ã€‚ã“ã®ä¾‹ã¯æœ¬å½“ã«`download`ã®æœ€ã‚‚ç°¡å˜ãªå½¢ã§ã™ï¼šã„ãã¤ã‹ã®ã‚¦ã‚§ãƒ–データをメモリã«å–ã£ã¦ãã¦ã€ãれを一度ãƒãƒ³ãƒ‰ãƒªãƒ³ã‚°ã—ã¦ã„ã¾ã™ã€‚
+
+`download`ã®ã‚‚ã†ä¸€ã¤ã®ã‚µãƒ³ãƒ—ルã¯ã„ãã¤ã‹ã®ã‚¦ã‚§ãƒ–データをã€`:save`スタイルを利用ã—ã¦ãƒ•ã‚¡ã‚¤ãƒ«ã«ä¿å­˜ã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ title "Downloading Google image", :size => 16
+ @status = para "One moment..."
+
+ download "http://www.google.com/logos/nasa50th.gif",
+ :save => "nasa50th.gif" do
+ @status.text = "Okay, is downloaded."
+ end
+ end
+ end
+}}}
+
+ã“ã®ã‚±ãƒ¼ã‚¹ã§ã‚‚ã€ãƒ€ã‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ãƒ•ã‚¡ã‚¤ãƒ«ã®ãƒ˜ãƒƒãƒ€ã‚’å–å¾—ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ãŒã€ãƒ¡ãƒ¢ãƒªã«ãã®ãƒ‡ãƒ¼ã‚¿ãŒä¿å­˜ã•ã‚Œã¦ã„ãªã„ãŸã‚`response.body`ã¯`nil`ã«ãªã‚Šã¾ã™ã€‚ダウンロードã—ãŸã‚‚ã®ã‚’å¾—ã‚‹ãŸã‚ã«ã¯ãã®ãƒ•ã‚¡ã‚¤ãƒ«ã‚’é–‹ãå¿…è¦ãŒã‚ã‚Šã¾ã™ã€‚
+
+特定ã®ãƒ˜ãƒƒãƒ€ã‹ã‚¢ã‚¯ã‚·ãƒ§ãƒ³ã‚’ウェブサーãƒã¸é€ã‚‹å¿…è¦ãŒã‚ã‚‹ã®ãªã‚‰ã°ã€HTTPリクエストをカスタマイズã™ã‚‹ãŸã‚ã«`:method`ã€`:headers`ã‚„`:body`スタイルを利用ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚(ãã—ã¦ã€ãれら以上ã®å¤‰æ›´ã®å¿…è¦ãŒã‚ã‚‹ã®ãªã‚‰ã€ã„ã¤ã§ã‚‚Rubyã®OpenURIクラスを破壊ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚)
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ title "POSTing to Google", :size => 16
+ @status = para "One moment..."
+
+ download "http://www.stevex.net/dump.php",
+ :method => "POST", :body => "v=1.0&q=shoes" do |dump|
+ require 'hpricot'
+ @status.text = Hpricot(dump.response.body).inner_text
+ end
+ end
+ end
+}}}
+
+上記ã®ä¾‹ã‹ã‚‰ã€Shoesã¯HTMLを解æžã™ã‚‹Hpricotãªãƒ©ã‚¤ãƒ–ラリをå«ã‚“ã§ã„ã‚‹ã“ã¨ãŒåˆ†ã‹ã‚Šã¾ã™ã€‚
+
+=== location() » a string ===
+
+ç¾åœ¨ã®ã‚¢ãƒ—リケーションã®URLã‚’å«ã‚€æ–‡å­—列をå–å¾—ã—ã¾ã™ã€‚
+
+=== mouse() » an array of numbers: button, left, top ===
+
+ã©ã¡ã‚‰ã®ãƒœã‚¿ãƒ³ãŒæŠ¼ã•ã‚ŒãŸã®ã‹ã¨å…±ã«ã€ãƒžã‚¦ã‚¹ã‚«ãƒ¼ã‚½ãƒ«ã®ä½ç½®ã‚’特定ã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @p = para
+ animate do
+ button, left, top = self.mouse
+ @p.replace "mouse: #{button}, #{left}, #{top}"
+ end
+ end
+}}}
+
+=== owner() » Shoes::App ===
+
+ã“ã®ã‚¢ãƒ—リケーションを開始ã—ãŸã‚¢ãƒ—リケーションをå–å¾—ã—ã¾ã™ã€‚多ãã®å ´åˆã€ã“ã‚Œã¯`nil`ã§ã—ょã†ã€‚ã—ã‹ã—[[Element.window]]メソッドを利用ã—ã¦ã‚¢ãƒ—リケーションãŒé–‹å§‹ã•ã‚ŒãŸãªã‚‰ã€ãã®æ‰€æœ‰è€…ã¯`window`ã¨å‘¼ã°ã‚Œã‚‹ã‚¢ãƒ—リケーションã§ã—ょã†ã€‚
+
+=== started?() » true or false ===
+
+ウィンドウã¯ã™ã¹ã¦æ§‹ç¯‰ã•ã‚Œã€è¡¨ç¤ºã•ã‚Œã¾ã—ãŸã‹ï¼Ÿã“ã‚Œã¯å®Œå…¨ã«æ§‹ç¯‰ã•ã‚Œã‚‹å‰ã«ã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’利用ã—よã†ã¨ã™ã‚‹ã‚¹ãƒ¬ãƒƒãƒ‰åŒ–ã•ã‚ŒãŸã‚³ãƒ¼ãƒ‰ã®ãŸã‚ã«å½¹ã«ç«‹ã¡ã¾ã™ã€‚
+(ã¾ãŸã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ãŒé–‹ãã¨ãã«å®Ÿè¡Œã•ã‚Œã‚‹`start`イベントも見ã¦ãã ã•ã„。)
+
+=== visit(url: a string) ===
+
+ç•°ãªã‚‹Shoesã®URLを見るãŸã‚ã«ã€ãƒ­ã‚±ãƒ¼ã‚·ãƒ§ãƒ³ã‚’変更ã—ã¾ã™ã€‚
+
+(http://google.comã®ã‚ˆã†ãªï¼‰çµ¶å¯¾ãƒ‘スã®URLã¯æ‚ªããªã„ã§ã™ãŒã€Shoesã¯ShoesアプリケーションãŒãã®ã‚¢ãƒ‰ãƒ¬ã‚¹ã«å­˜åœ¨ã™ã‚‹ã“ã¨ã‚’期待ã™ã‚‹ã§ã—ょã†ã€‚(ãã®ãŸã‚ã€google.comã¯HTMLアプリケーションã¨ã—ã¦ã¯å‹•ä½œã—ã¾ã›ã‚“。)
+
+== The Styles Master List ==
+
+外観を変更ã—ãŸã„ã§ã™ã‹ï¼ŸShoesã«ãŠã„ã¦ã¯ã‚¹ã‚¿ã‚¤ãƒ«ãŒè¦ç´ ã®è¡¨ç¤ºæ–¹æ³•ã‚’変更ã™ã‚‹ãŸã‚ã«åˆ©ç”¨ã•ã‚Œã¾ã™ã€‚å ´åˆã«ã‚ˆã£ã¦ã¯ã€è¦ç´ ã®ã™ã¹ã¦ã®ã‚¯ãƒ©ã‚¹ã®ã‚¹ã‚¿ã‚¤ãƒ«ã§ã•ãˆè¨­å®šã§ãã¾ã™ã€‚(ã™ã¹ã¦ã®æ®µè½ã«ç‰¹å®šã®ãƒ•ã‚©ãƒ³ãƒˆã‚’与ãˆã‚‹ã‚ˆã†ã«ï¼‰
+
+スタイルã¯ç°¡å˜ã«spotã§ãã¾ã™ã€‚通常ã¯è¦ç´ ãŒç”Ÿæˆã•ã‚Œã‚‹ã¨ãã«ç¾ã‚Œã¾ã™ã€‚
+
+{{{
+ Shoes.app :title => "A Styling Sample" do
+ para "Red with an underline", :stroke => red, :underline => "single"
+ end
+}}}
+
+ã“ã®appã«ã¯`:title`スタイルãŒè¨­å®šã•ã‚Œã¦ã„ã¾ã™ã€‚ãã—ã¦ã“ã®appã®å†…部ã®æ®µè½ã«ã¯ã€èµ¤ã„`:stroke`スタイルã¨`:underline`スタイルãŒè¨­å®šã•ã‚Œã¾ã™ã€‚
+
+ã“ã®ã‚¹ã‚¿ã‚¤ãƒ«ã®ãƒãƒƒã‚·ãƒ¥ã¯ã€ã©ã‚“ãªè¦ç´ ã‚„スロットも利用ã§ãã‚‹[[Common.style]]メソッドを利用ã—ã¦å¤‰æ›´ã§ãã¾ã™ã€‚
+
+{{{
+ Shoes.app :title => "A Styling Sample" do
+ @text = para "Red with an underline"
+ @text.style(:stroke => red, :underline => "single")
+ end
+}}}
+
+多ãã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚‚メソッドã¨ã—ã¦å‘¼ã³å‡ºã™ã“ã¨ã§è¨­å®šã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚(メソッドを見ã¤ã‘ã‚‹ãŸã‚ã«æ‰‹å‹•ã§ã®æ¤œç´¢ã‚’è¡Œã†ã§ã—ょã†ã€‚)
+
+{{{
+ Shoes.app :title => "A Styling Sample" do
+ @text = para "Red with an underline"
+ @text.stroke = red
+ @text.underline = "single"
+ end
+}}}
+
+ã©ã‚“ãªã‚¹ã‚¿ã‚¤ãƒ«ã§ã‚‚分ã‹ã‚‹ã‚ˆã†ã«ã™ã¹ã¦ã®ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã‚’苦労ã—ã¦èª­ã¾ã›ã‚‹ã‚ˆã‚Šã‚‚ã€ã“ã®å½¹ã«ç«‹ã¤ãƒšãƒ¼ã‚¸ã¯Shoesã®ã‚らゆるスタイルを急ã„ã§é§†ã‘抜ã‘ã¦ã€ã©ã“ã§ã‚¹ã‚¿ã‚¤ãƒ«ãŒåˆ©ç”¨ã•ã‚Œã‚‹ã‹ã«ã¤ã„ã¦ç¤ºå”†ã—ã¾ã™ã€‚
+
+=== :align » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+テキストã®æ•´åˆ—ã§ã™ã€‚ã“ã‚Œã¯æ¬¡ã®ã©ã‚Œã‹ã§ã™ï¼š
+
+ * 'left': å·¦ã¸ãƒ†ã‚­ã‚¹ãƒˆã‚’整列ã—ã¾ã™ã€‚
+ * 'center': 中央ã¸ãƒ†ã‚­ã‚¹ãƒˆã‚’整列ã—ã¾ã™ã€‚
+ * 'right': å³ã¸ãƒ†ã‚­ã‚¹ãƒˆã‚’整列ã—ã¾ã™ã€‚
+
+=== :angle » a number ===
+
+''background, border, gradient''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+グラデーションã«æ‘˜è¦ã™ã‚‹è§’度ã§ã™ã€‚通常ã¯ã‚°ãƒ©ãƒ‡ãƒ¼ã‚·ãƒ§ãƒ³ã®è‰²ã®åŠ¹æžœã¯ä¸Šã‹ã‚‰ä¸‹ã§ã™ã€‚`:angle`ã‚’90ã«è¨­å®šã™ã‚‹ãªã‚‰å時計回りã«90度回転ã—ã€ã‚°ãƒ©ãƒ‡ãƒ¼ã‚·ãƒ§ãƒ³ã¯å·¦ã‹ã‚‰å³ã«ãªã‚‹ã§ã—ょã†ã€‚
+
+=== :attach » a slot or element ===
+
+''flow, stack''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ä»–ã®ã‚¹ãƒ­ãƒƒãƒˆã‚„è¦ç´ ã¨æ¯”較ã—ã¦ã‚¹ãƒ­ãƒƒãƒˆã‚’ピンã§æ­¢ã‚ã¾ã™ã€‚ウィンドウã®å·¦ä¸Šã®è§’ã‹ã‚‰ã‚¹ãƒ­ãƒƒãƒˆã‚’é…ç½®ã™ã‚‹ãŸã‚ã«`:attach => Window`ã¨æ›¸ã人もã„ã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。
+ã“ã‚Œã«ã¤ã„ã¦ã‚‚ã†å°‘ã—å–り上ã’ã‚‹ã¨ã€`:top => 10, :left => 10, :attach => Window`ã®ã‚¹ã‚¿ã‚¤ãƒ«ã¯ã‚¹ãƒ­ãƒƒãƒˆã‚’ウインドウã®åº§æ¨™ã®(10, 10)ã«é…ç½®ã—ã¾ã™ã€‚
+
+å‹•ãè¦ç´ ã«ã‚¹ãƒ­ãƒƒãƒˆãŒã‚¢ã‚¿ãƒƒãƒã•ã‚ŒãŸå ´åˆã¯ã€ãã®ã‚¹ãƒ­ãƒƒãƒˆã¯ãã‚Œã¨ã¨ã‚‚ã«å‹•ãã¾ã™ã€‚アタッãƒãƒ¡ãƒ³ãƒˆãŒ`nil`ã«ãƒªã‚»ãƒƒãƒˆã•ã‚Œã‚‹ãªã‚‰ã€é€šå¸¸ã¯ãã®ã‚¹ãƒ­ãƒƒãƒˆã¯ãれをå–り囲む他ã®ã‚ªãƒ–ジェクトã¨ã¨ã‚‚ã«æµã‚Œã¾ã™ã€‚
+
+=== :autoplay » true or false ===
+
+''video''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ビデオã¯ç¾ã‚ŒãŸå¾Œã§å†ç”Ÿã‚’開始ã™ã¹ãã§ã™ã‹ï¼Ÿ`true`を設定ã™ã‚‹ã¨ã€ãƒ“デオã¯ãƒ¦ãƒ¼ã‚¶ã«å°‹ã­ã‚‹ã“ã¨ç„¡ã開始ã™ã‚‹ã§ã—ょã†ã€‚
+
+=== :bottom » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+è¦ç´ ã®ä¸‹ã®ç«¯ã®åº§æ¨™ã«ãƒ”クセルを設定ã—ã¾ã™ã€‚ãã®ç«¯ã¯ã‚³ãƒ³ãƒ†ãƒŠã®ä¸‹ã®ç«¯ã«å¯¾ã—ã¦é…ç½®ã•ã‚Œã¾ã™ã€‚ãã®ãŸã‚ã€`:bottom => 0`ã¯ã€ã‚¹ãƒ­ãƒƒãƒˆã®ä¸‹ã®ç«¯ã¨ãã®ä¸‹ã®ç«¯ãŒæŽ¥ã™ã‚‹ã‚ˆã†ã«è¦ç´ ã‚’é…ç½®ã™ã‚‹ã§ã—ょã†ã€‚
+
+=== :cap » :curve or :rect or :project ===
+
+''arc, arrow, border, flow, image, mask, rect, star, shape, stack''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ç·šã®çµ‚点ã®å½¢çŠ¶ã‚’曲ãŒã£ãŸã‚‚ã®ï¼ˆcurved)ã‹è§’å¼µã£ãŸã‚‚ã®ï¼ˆsquare)ã«è¨­å®šã—ã¾ã™ã€‚追加ã®èª¬æ˜Žã¯[[Art.cap]]メソッドを見ã¦ãã ã•ã„。
+
+=== :center » true or false ===
+
+''arc, image, oval, rect, shape''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+`:top`ã¨`:left`ã®åº§æ¨™ãŒå½¢çŠ¶ã®ä¸­å¿ƒã‚’æ„味ã™ã‚‹ã‹ã©ã†ã‹ç¤ºã—ã¾ã™ã€‚`true`を設定ã™ã‚‹ã¨ã€[[Art.transform]]メソッドã«`:center`を設定ã—ãŸã®ã¨ä¼¼ã¦ã„ã¾ã™ã€‚
+
+=== :change » a proc ===
+
+''edit_box, edit_line, list_box''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+`change`イベントãƒãƒ³ãƒ‰ãƒ©ã¯ã‚¹ã‚¿ã‚¤ãƒ«ã«ä¿å­˜ã•ã‚Œã¾ã™ã€‚例ã¨ã—ã¦ã€edit_boxã®[[EditBox.change]]メソッドを見ã¦ãã ã•ã„。
+
+=== :checked » true or false ===
+
+''check, radio''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã¾ãŸã¯ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ãŒã‚¯ãƒªãƒƒã‚¯ã•ã‚Œã¾ã—ãŸã‹ï¼Ÿ`true`ãŒè¨­å®šã•ã‚Œã‚‹ãªã‚‰ã€ãã®ãƒœãƒƒã‚¯ã‚¹ã¯ãƒã‚§ãƒƒã‚¯ã•ã‚Œã¾ã™ã€‚[[Check.checked=]]メソッドも見ã¦ãã ã•ã„。
+
+=== :choose » a string ===
+
+''list_box''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+リスト内ã®ç¾åœ¨é¸æŠžã•ã‚ŒãŸã‚¢ã‚¤ãƒ†ãƒ ã‚’設定ã—ã¾ã™ã€‚追加ã®æƒ…å ±ã¯[[ListBox.choose]]ã«ã‚ã‚Šã¾ã™ã€‚
+
+=== :click » a proc ===
+
+''arc, arrow, banner, button, caption, check, flow, image, inscription, line, link, mask, oval, para, radio, rect, shape, stack, star, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+`click`イベントãƒãƒ³ãƒ‰ãƒ©ã¯ã‚¹ã‚¿ã‚¤ãƒ«ã«ä¿å­˜ã•ã‚Œã¾ã™ã€‚解説ã¯[[Events.click]]メソッドを見ã¦ãã ã•ã„。
+
+=== :curve » a number ===
+
+''background, border, rect''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+長方形ã®è¦ç´ ã®ãã‚Œãžã‚Œã®æ›²ãŒã£ãŸè§’ã®åŠå¾„ã§ã™ã€‚例ã¨ã—ã¦ã€6を設定ã—ãŸå ´åˆã€é•·æ–¹å½¢ã®è§’ã¯6ピクセルã®åŠå¾„ã®ã‚«ãƒ¼ãƒ–を与ãˆã‚‰ã‚Œã¾ã™ã€‚
+
+=== :displace_left » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+形状ã€ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックã¾ãŸã¯ãã®ä»–ã®ã©ã‚“ãªç¨®é¡žã®ã‚ªãƒ–ジェクトã§ã‚‚å·¦ã‹å³ã«ç½®ãæ›ãˆã¾ã™ã€‚正数ã¯ä¸Žãˆã‚‰ã‚ŒãŸæ•°ã®ãƒ”クセルã«ã‚ˆã£ã¦å³ã¸ç½®ãæ›ãˆã€è² æ•°ã¯å·¦ã¸ç½®ãæ›ãˆã¾ã™ã€‚オブジェクトを置ãæ›ãˆã‚‹ã“ã¨ã¯ãƒšãƒ¼ã‚¸ã®å®Ÿéš›ã®ãƒ¬ã‚¤ã‚¢ã‚¦ãƒˆã«å½±éŸ¿ã‚’与ãˆã¾ã›ã‚“。ã“ã®æŒ¯ã‚‹èˆžã„ã«å°‘ã—é©šãã‹ã‚‚ã—ã‚Œãªã„ã®ã§ã€ã“ã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’利用ã™ã‚‹å‰ã«ã€[[Position.displace]]ã®ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆã‚’読むよã†ã«ã—ã¦ãã ã•ã„。
+
+=== :displace_top » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+形状ã€ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックã¾ãŸã¯ãã®ä»–ã®ã©ã‚“ãªç¨®é¡žã®ã‚ªãƒ–ジェクトã§ã‚‚上ã‹ä¸‹ã«ç½®ãæ›ãˆã¾ã™ã€‚正数ã¯ä¸Žãˆã‚‰ã‚ŒãŸæ•°ã®ãƒ”クセルã«ã‚ˆã£ã¦ä¸‹ã¸ç½®ãæ›ãˆã€è² æ•°ã¯ä¸Šã¸ç½®ãæ›ãˆã¾ã™ã€‚オブジェクトを置ãæ›ãˆã‚‹ã“ã¨ã¯ãƒšãƒ¼ã‚¸ã®å®Ÿéš›ã®ãƒ¬ã‚¤ã‚¢ã‚¦ãƒˆã‚„オブジェクトã®æœ¬å½“ã®åº§æ¨™ã«å½±éŸ¿ã‚’与ãˆã¾ã›ã‚“。ã“ã®æŒ¯ã‚‹èˆžã„ã«å°‘ã—é©šãã‹ã‚‚ã—ã‚Œãªã„ã®ã§ã€[[Position.displace]]ã®ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆã‚’読んã§ãã ã•ã„。
+
+=== :emphasis » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+強調ã«ã‚ˆã£ã¦ãƒ†ã‚­ã‚¹ãƒˆã‚’æ•´ãˆã¾ã™ã€‚(一般的ã«ã¯ã‚¤ã‚¿ãƒªãƒƒã‚¯ä½“ã«ã•ã‚Œã¾ã™ã€‚)
+
+ã“ã®ã‚¹ã‚¿ã‚¤ãƒ«ã¯3ã¤ã®è¨­å®šãŒã§ãã¾ã™ï¼š
+
+ * "normal" - ç›´ç«‹ã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * "oblique" - ローマン体ã®å‚¾ã„ãŸãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * "italic" - イタリック体ã®å‚¾ã„ãŸãƒ•ã‚©ãƒ³ãƒˆã€‚
+
+=== :family » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+与ãˆã‚‰ã‚ŒãŸãƒ•ã‚©ãƒ³ãƒˆãƒ•ã‚¡ãƒŸãƒªãƒ¼ã§ãƒ†ã‚­ã‚¹ãƒˆã‚’æ•´ãˆã¾ã™ã€‚文字列ã¯ãƒ•ã‚©ãƒ³ãƒˆãƒ•ã‚¡ãƒŸãƒªãƒ¼åã‹ã‚«ãƒ³ãƒžã§åŒºåˆ‡ã‚‰ã‚ŒãŸãƒ•ã‚©ãƒ³ãƒˆãƒ•ã‚¡ãƒŸãƒªãƒ¼ã®ä¸€è¦§ã‚’å«ã‚€ã¹ãã§ã™ã€‚
+
+=== :fill » a hex code, a Shoes::Color or a range of either ===
+
+''arc, arrow, background, banner, caption, code, del, em, flow, image, ins, inscription, line, link, mask, oval, para, rect, shape, span, stack, star, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+背景ã®ãƒšãƒ³ã®è‰²ã§ã™ã€‚形状ã§ã¯ã€ã“ã‚Œã¯å½¢çŠ¶ã®å†…å´ã‚’å¡—ã‚Šã¤ã¶ã™ãƒšãƒ³ã‚­ã®è‰²ã§ã™ã€‚テキストãªã©ã§ã¯ã€ã“ã®è‰²ã§èƒŒæ™¯ãŒå¡—られã¾ã™ã€‚(ã¾ã‚‹ã§è›å…‰ãƒšãƒ³ã§ãƒžãƒ¼ã‚¯ã•ã‚ŒãŸã‚ˆã†ã«ï¼‰
+
+=== :font » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+フォントã®ç¨®é¡žã§ãƒ†ã‚­ã‚¹ãƒˆã‚’æ•´ãˆã¾ã™ã€‚ã“ã®æ–‡å­—列ã¯éžå¸¸ã«æŸ”軟ã§ã™ãŒã€"[FAMILY-LIST] [STYLE-OPTIONS] [SIZE]"ã®å½¢ã§ã‚ã‚‹å¿…è¦ãŒã‚ã‚Šã€FAMILY-LISTã®éƒ¨åˆ†ã¯ä»»æ„ã«ã‚«ãƒ³ãƒžã§çµ‚ã‚りカンマã§åŒºåˆ‡ã‚‰ã‚ŒãŸãƒ•ã‚©ãƒ³ãƒˆãƒ•ã‚¡ãƒŸãƒªãƒ¼ã®ä¸€è¦§ã€STYLE_OPTIONSã¯variantã€weightã€stretchã€ã¾ãŸã¯gravityãªã©ã®ç©ºç™½ã§åŒºåˆ‡ã‚‰ã‚ŒãŸã‚¹ã‚¿ã‚¤ãƒ«ã‚’表ç¾ã™ã‚‹å˜èªžã®ä¸€è¦§ã€ãã—ã¦SIZEã¯ï¼ˆãƒã‚¤ãƒ³ãƒˆã®ã‚µã‚¤ã‚ºã®ï¼‰10進数ã¾ãŸã¯çµ¶å¯¾çš„ãªã‚µã‚¤ã‚ºã®ãŸã‚ã«å˜ä½ä¿®é£¾å­"px"ã‚’ä»»æ„ã«ç¶šã‘ã¾ã™ã€‚オプションã®ã©ã‚Œã‹ã¯è¨­å®šã•ã‚Œãªã„ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。FAMILY-LISTãŒè¨­å®šã•ã‚Œãªã„å ´åˆã¯ã€ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆãƒ•ã‚©ãƒ³ãƒˆãƒ•ã‚¡ãƒŸãƒªãƒ¼ï¼ˆArial)ãŒåˆ©ç”¨ã•ã‚Œã¾ã™ã€‚
+
+=== :group » a string ===
+
+''radio''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ã©ã®ã‚°ãƒ«ãƒ¼ãƒ—ã«ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ãŒæ‰€å±žã™ã‚‹ã‹ã‚’示ã—ã¾ã™ã€‚ã“ã®è¨­å®šãŒãªã„å ´åˆã¯ã€ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¯å‘¨è¾ºã®ã‚¹ãƒ­ãƒƒãƒˆã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¨ã‚°ãƒ«ãƒ¼ãƒ—化ã•ã‚Œã¾ã™ã€‚ラジオボタンを"グループ化"ã™ã‚‹ã“ã¨ã¯ã‚¹ã‚¯ãƒªãƒ¼ãƒ³ä¸Šã§ãŠäº’ã„ã«éš£æŽ¥ã—ã¦ã‚°ãƒ«ãƒ¼ãƒ—化ã•ã‚Œã‚‹ã“ã¨ã‚’æ„味ã™ã‚‹ã®ã§ã¯ã‚ã‚Šã¾ã›ã‚“。ãã‚Œã¯ã€ä¸€åº¦ã«ã‚°ãƒ«ãƒ¼ãƒ—ã‹ã‚‰ä¸€ã¤ã ã‘ã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã ã‘ã‚’é¸æŠžã§ãã‚‹ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚
+
+文字列ã«ã‚¹ã‚¿ã‚¤ãƒ«ã‚’与ãˆã‚‹ã“ã¨ã«ã‚ˆã£ã¦ã€ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¯åŒã˜ã‚°ãƒ«ãƒ¼ãƒ—åã‚’æŒã¤ä»–ã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¨ã‚°ãƒ«ãƒ¼ãƒ—化ã•ã‚Œã¾ã™ã€‚
+
+=== :height » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+オブジェクトã®é«˜ã•ã‚’ピクセルã§è¨­å®šã—ã¾ã™ã€‚数値ãŒ10進数ãªã‚‰ã€ãã®é«˜ã•ã¯è¦ªã®é«˜ã•ã®ãƒ‘ーセンテージã«ãªã‚Šã¾ã™ã€‚(0.0ã¯0%ã«ã€1.0ã¯100%ã«ãªã‚Šã¾ã™ã€‚)
+
+=== :hidden » true or false ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+オブジェクトã®è¡¨ç¤ºã¾ãŸã¯éžè¡¨ç¤ºã§ã™ã€‚ã™ã¹ã¦ã®ã‚ªãƒ–ジェクトã«ã¨ã£ã¦`:hidden => true`ã¯ç”»é¢ä¸Šã§ã®éžè¡¨ç¤ºã«ãªã‚Šã¾ã™ã€‚ãã®å­ä¾›ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ã§ã‚‚åŒæ§˜ã§ã™ã€‚
+
+=== :inner » a number ===
+
+''star''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+内å´ã®åŠå¾„ã®ã‚µã‚¤ã‚ºï¼ˆãƒ”クセル)ã§ã™ã€‚ãã®å†…å´ã®åŠå¾„ã¯ç‚¹ãŒåˆ¥ã‚Œå§‹ã‚る星ã®ä¸­ã«ä¸­ç©ºã§ãªã„円をæãã¾ã™
+
+=== :items » an array ===
+
+''list_box''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+リストボックスã®é …ç›®ã®ä¸€è¦§ã§ã™ã€‚例ã®ãŸã‚ã«[[Element.list_box]]メソッドを見ã¦ãã ã•ã„。
+
+=== :justify » true or false ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+å‡ä¸€ã«æ°´å¹³ã«ãƒ†ã‚­ã‚¹ãƒˆã®é–“隔を開ã‘ã¾ã™ã€‚
+
+=== :kerning » a number ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+文字ã®é–“ã«è‡ªç„¶ãªç©ºç™½ã‚’ピクセルã§è¿½åŠ ã—ã¾ã™ã€‚
+
+=== :leading » a number ===
+
+''banner, caption, inscription, para, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+テキストブロックã®è¡Œé–“ã«ç©ºç™½ã‚’設定ã—ã¾ã™ã€‚デフォルトã¯4ピクセルã§ã™ã€‚
+
+=== :left » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+オブジェクトã®å·¦ã®åº§æ¨™ã‚’特定ã®ãƒ”クセルã«è¨­å®šã—ã¾ã™ã€‚`:left => 10`ã®è¨­å®šã¯ãã®ã‚ªãƒ–ジェクトをå«ã‚€ã‚¹ãƒ­ãƒƒãƒˆã®å·¦ã®ç«¯ã‹ã‚‰ã‚ªãƒ–ジェクトã®å·¦ã®ç«¯ãŒ10ピクセル離れãŸä½ç½®ã«é…ç½®ã—ã¾ã™ã€‚ã“ã®leftã®ã‚¹ã‚¿ã‚¤ãƒ«ãŒè¨­å®šã•ã‚Œã¦ã„ãªã„(ã¾ãŸã¯`nil`ãŒè¨­å®šã•ã‚Œã‚‹ï¼‰ãªã‚‰ã€ãã®ã‚ªãƒ–ジェクトã¯ãれを囲んã§ã„ã‚‹ä»–ã®ã‚ªãƒ–ジェクトã¨ã¨ã‚‚ã«å‹•ãã§ã—ょã†ã€‚
+
+=== :margin » a number or an array of four numbers ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+マージンã¯è¦ç´ ã®å‘¨å›²ã«é–“隔をã‚ã‘ã¾ã™ã€‚ãã‚Œãžã‚Œã®è¦ç´ ã¯leftã€topã€rightã€ãã—ã¦bottomã®ãƒžãƒ¼ã‚¸ãƒ³ã‚’æŒã£ã¦ã„ã¾ã™ã€‚`:margin`スタイルã«ä¸€ã¤ã®æ•°ãŒè¨­å®šã•ã‚Œã‚‹ã¨ã€è¦ç´ ã®å‘¨å›²ã®é–“éš”ã¯å‡ä¸€ã«ãã®æ•°ã«ã¨ãªã‚Šã¾ã™ã€‚言ã„æ›ãˆã‚‹ã¨ã€`:margin => 8`を設定ã™ã‚‹ã¨ã€ãã®è¦ç´ ã®å‘¨å›²ã®ã™ã¹ã¦ã®ãƒžãƒ¼ã‚¸ãƒ³ã¯8ピクセルã®é•·ã•ã«è¨­å®šã•ã‚Œã¾ã™ã€‚
+
+ã“ã®ã‚¹ã‚¿ã‚¤ãƒ«ã¯4ã¤ã®æ•°ã‚’`[left, top, right, bottom]`ã®å½¢ã®é…列ã§ä¸Žãˆã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚
+
+=== :margin_bottom » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+è¦ç´ ã®ä¸‹å´ï¼ˆbottom)ã®ãƒžãƒ¼ã‚¸ãƒ³ã‚’ピクセルã§è¨­å®šã—ã¾ã™ã€‚
+
+=== :margin_left » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+è¦ç´ ã®å·¦å´ï¼ˆleft)ã®ãƒžãƒ¼ã‚¸ãƒ³ã‚’ピクセルã§è¨­å®šã—ã¾ã™ã€‚
+
+=== :margin_right » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+è¦ç´ ã®å³å´ï¼ˆright)ã®ãƒžãƒ¼ã‚¸ãƒ³ã‚’ピクセルã§è¨­å®šã—ã¾ã™ã€‚
+
+=== :margin_top » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+è¦ç´ ã®ä¸Šå´ï¼ˆtop)ã®ãƒžãƒ¼ã‚¸ãƒ³ã‚’ピクセルã§è¨­å®šã—ã¾ã™ã€‚
+
+=== :outer » a number ===
+
+''star''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+星ã®å¤–å´ã®åŠå¾„(''å…¨''å¹…ã®åŠåˆ†ï¼‰ã‚’ピクセルã§è¨­å®šã—ã¾ã™ã€‚
+
+=== :points » a number ===
+
+''star''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ãã®æ˜Ÿã¯ã„ãã¤ã®é ‚点をæŒã¡ã¾ã™ã‹ï¼Ÿ`:points => 5`ã®ã‚¹ã‚¿ã‚¤ãƒ«ã¯5ã¤ã®é ‚点をæŒã¤æ˜Ÿã‚’作æˆã—ã¾ã™ã€‚
+
+=== :radius » a number ===
+
+''arc, arrow, background, border, gradient, oval, rect, shape''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ãれらã®è¦ç´ ã«åŠå¾„(直径ã¾ãŸã¯å…¨å¹…ã®åŠåˆ†ï¼‰ã‚’設定ã—ã¾ã™ã€‚ã“れを設定ã™ã‚‹ã“ã¨ã¯ã€ã“ã®æ•°å€¤ã®2å€ã®`:width`ã¨`:height`を設定ã™ã‚‹ã“ã¨ã¨åŒç­‰ã§ã™ã€‚
+
+=== :right » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+è¦ç´ ã®å³ç«¯ã®åº§æ¨™ã‚’ピクセルã§è¨­å®šã—ã¾ã™ã€‚ãã®ç«¯ã¯ã‚³ãƒ³ãƒ†ãƒŠã®å³ç«¯ã«å¯¾ã—ã¦é…ç½®ã•ã‚Œã¾ã™ã€‚ãã®ãŸã‚ã€`:right => 0`ã¯ã€ã‚¹ãƒ­ãƒƒãƒˆã®å³ç«¯ã¨ãã®å³ç«¯ãŒæŽ¥ã™ã‚‹ã‚ˆã†ã«è¦ç´ ã‚’é…ç½®ã™ã‚‹ã§ã—ょã†ã€‚一方`:right => 20`ã¯è¦ç´ ã®å³ç«¯ã‚’ãã®ã‚¹ãƒ­ãƒƒãƒˆã®å³ç«¯ã‹ã‚‰å·¦å´ã«å‘ã‘ã¦20ピクセル離れãŸã¨ã“ã‚ã«é…ç½®ã—ã¾ã™ã€‚
+
+=== :rise » a number ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+テキストã®ãƒ•ã‚©ãƒ³ãƒˆã‚’基準ã‹ã‚‰ä¸Šã’ãŸã‚Šä¸‹ã’ãŸã‚Šã—ã¾ã™ã€‚例ãˆã°ã€[[Element.sup]]ã¯10ピクセルã®`:rise`ã‚’è¡Œã„ã¾ã™ã€‚逆ã«ã€[[Element.sub]]ã®è¦ç´ ã¯-10ピクセルã®`:rise`ã¨ãªã‚Šã¾ã™ã€‚
+
+=== :scroll » true or false ===
+
+''flow, stack''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+スロットをスクロールã™ã‚‹ã‚¹ãƒ­ãƒƒãƒˆã¨ã—ã¾ã™ã€‚`:scroll => true`ãŒè¨­å®šã•ã‚Œã€ãã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„ãŒã‚¹ãƒ­ãƒƒãƒˆã®é«˜ã•ä»¥ä¸Šã®å ´åˆã¯ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ãƒãƒ¼ãŒã‚¹ãƒ­ãƒƒãƒˆã«è¡¨ç¤ºã•ã‚Œã¾ã™ã€‚スクロールãƒãƒ¼ã¯å¿…è¦ã«å¿œã˜ã¦è¡¨ç¤ºã—ãŸã‚Šéžè¡¨ç¤ºã«ãªã‚Šã¾ã™ã€‚ãã‚Œã¯ã‚¹ãƒ­ãƒƒãƒˆã®å¹…ã®å†…å´ã§è¡¨ç¤ºã•ã‚Œã‚‹ã®ã§ã€ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ãƒãƒ¼ã®ã‚ã‚‹ãªã—ã«é–¢ã‚らãšã€ã‚¹ãƒ­ãƒƒãƒˆã®å¹…ã¯æ±ºã—ã¦å¤‰ã‚らãªã„ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚
+
+=== :secret » true or false ===
+
+''ask, edit_line''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+Used for password fields, this setting keeps any characters typed in from becoming visible on the screen. Instead, a replacement character (such as an asterisk) is show for each letter typed.
+
+パスワードフィールドã®ãŸã‚ã«åˆ©ç”¨ã•ã‚Œã€ã“ã®è¨­å®šã¯å…¥åŠ›ã•ã‚ŒãŸæ–‡å­—ã‚’ç”»é¢ä¸Šã§è¡¨ç¤ºã•ã‚Œãªã„よã†ã«ã—ã¾ã™ã€‚ãã®ã‹ã‚ã‚Šã€ç½®ãæ›ãˆã‚‰ã‚ŒãŸæ–‡å­—(ãŸã¨ãˆã°ã‚¢ã‚¹ã‚¿ãƒªã‚¹ã‚¯ï¼‰ã‚’ãã‚Œãžã‚Œã®æ–‡å­—ãŒå…¥åŠ›ã•ã‚Œã‚‹ã”ã¨ã«è¡¨ç¤ºã—ã¾ã™ã€‚
+
+=== :size » a number ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ã“ã®ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックã¾ãŸã¯ãƒ†ã‚­ã‚¹ãƒˆã®ä¸€éƒ¨ã®å†…部ã§åˆ©ç”¨ã•ã‚ŒãŸãƒ•ã‚©ãƒ³ãƒˆã®ã‚µã‚¤ã‚ºã‚’ピクセルã§è¨­å®šã—ã¾ã™ã€‚
+
+フォントサイズã¯æ¬¡ã®æ–‡å­—列を利用ã™ã‚‹ã“ã¨ã§ã‚‚大ããã™ã‚‹ã“ã¨ãŒã§ãã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“:
+
+ * "xx-small" - ç¾åœ¨ã®ã‚µã‚¤ã‚ºã®57%
+ * "x-small" - ç¾åœ¨ã®ã‚µã‚¤ã‚ºã®64%
+ * "small" - ç¾åœ¨ã®ã‚µã‚¤ã‚ºã®83%
+ * "medium" - サイズ変更ãªã—
+ * "large" - ç¾åœ¨ã®ã‚µã‚¤ã‚ºã®120%
+ * "x-large" - ç¾åœ¨ã®ã‚µã‚¤ã‚ºã®143%
+ * "xx-large" - ç¾åœ¨ã®ã‚µã‚¤ã‚ºã®173%
+
+=== :state » a string ===
+
+''button, check, edit_box, edit_line, list_box, radio''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ã“ã®`:state`スタイルã¯ç·¨é›†ã•ã‚ŒãŸããªã„コントロールを利用ä¸èƒ½ã¾ãŸã¯å›ºå®šã™ã‚‹ãŸã‚ã«ã‚ã‚Šã¾ã™ã€‚
+
+利用å¯èƒ½ãªã‚¹ã‚¿ã‚¤ãƒ«ã®è¨­å®šï¼š
+
+ * nil - コントロールã¯ã‚¢ã‚¯ãƒ†ã‚£ãƒ–ã§ç·¨é›†å¯èƒ½ã§ã™ã€‚
+ * "readonly" - コントロールã¯ã‚¢ã‚¯ãƒ†ã‚£ãƒ–ã§ã™ãŒç·¨é›†ä¸å¯èƒ½ã§ã™ã€‚
+ * "disabled" - コントロールã¯ã‚¢ã‚¯ãƒ†ã‚£ãƒ–ã¯ãªã(グレイアウト)ã€ç·¨é›†ä¸å¯èƒ½ã§ã™ã€‚
+
+=== :stretch » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+テキストオブジェクトã«ä¼¸ç¸®ã—ãŸãƒ•ã‚©ãƒ³ãƒˆã‚’設定ã—ã¾ã™ã€‚
+
+利用å¯èƒ½ãªè¨­å®šï¼š
+
+ * "condensed" - ç‹­ã„å¹…ã®æ–‡å­—
+ * "normal" - 標準ã®å¹…ã®æ–‡å­—
+ * "expanded" - 広ã„å¹…ã®æ–‡å­—
+
+=== :strikecolor » a Shoes::Color ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+テキストã«æ£’線を引ã„ã¦å‰Šé™¤ã™ã‚‹ã¨ãã®ç·šã®è‰²ã§ã™ã€‚
+
+=== :strikethrough » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+ã“ã®ãƒ†ã‚­ã‚¹ãƒˆã«æ£’線を引ã„ã¦å‰Šé™¤ã—ã¾ã™ã‹ï¼Ÿ2ã¤ã®ã‚ªãƒ—ションãŒã‚ã‚Šã¾ã™ï¼š
+
+ * "none" - 棒線を引ã„ã¦å‰Šé™¤ã—ã¾ã›ã‚“。
+ * "single" - 1本ã®æ£’線を引ã„ã¦å‰Šé™¤ã—ã¾ã™ã€‚
+
+=== :stroke » a hex code, a Shoes::Color or a range of either ===
+
+''arc, arrow, banner, border, caption, code, del, em, flow, image, ins, inscription, line, link, mask, oval, para, rect, shape, span, stack, star, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+フォアグラウンドã®ãƒšãƒ³ã®è‰²ã§ã™ã€‚形状ã®å ´åˆã¯æã‹ã‚Œã‚‹ç·šã®è‰²ã§ã™ã€‚段è½ã‚„ä»–ã®ãƒ†ã‚­ã‚¹ãƒˆã§ã¯ã€ã“ã®è‰²ã§æ–‡å­—ãŒè¡¨ç¤ºã•ã‚Œã¾ã™ã€‚
+
+=== :strokewidth » a number ===
+
+''arc, arrow, border, flow, image, line, mask, oval, rect, shape, star, stack''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+æã‹ã‚Œã‚‹ç·šã®ãƒ”クセルã§ã®å¤ªã•ã§ã€å½¢çŠ¶ã®ç·šã‚’特徴付ã‘ã¾ã™ã€‚例ãˆã°ã€æ•°å€¤ã®2ãŒè¨­å®šã•ã‚Œã‚Œã°strokewidthã¯2ピクセルã«ãªã‚Šã¾ã™ã€‚
+
+=== :text » a string ===
+
+''button, edit_box, edit_line''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+edit_boxã‚„edit_lineã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„ã€ã¾ãŸã¯ãƒœã‚¿ãƒ³ã‚³ãƒ³ãƒˆãƒ­ãƒ¼ãƒ«ã«è¡¨ç¤ºã•ã‚Œã‚‹ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’設定ã—ã¾ã™ã€‚
+
+=== :top » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+オブジェクトã®ä¸Šå´ã®åº§æ¨™ã‚’ã€ãã®è¦ªã®ã‚¹ãƒ­ãƒƒãƒˆã«å¯¾ã—ã¦è¨­å®šã—ã¾ã™ã€‚オブジェクトã«`:top => 40`ãŒè¨­å®šã•ã‚ŒãŸãªã‚‰ã€ã‚ªãƒ–ジェクトã®ä¸Šç«¯ã¯ãã®ã‚ªãƒ–ジェクトをå«ã‚€ã‚¹ãƒ­ãƒƒãƒˆã®ä¸Šç«¯ã‹ã‚‰40ピクセル下ã«é…ç½®ã•ã‚Œã‚‹ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚`:top`スタイルãŒä¸Žãˆã‚‰ã‚Œãªã„ãªã‚‰ã€ãã®ã‚¹ãƒ­ãƒƒãƒˆã®è‡ªç„¶ãªæµã‚Œã§ã‚ªãƒ–ジェクトã¯è‡ªå‹•çš„ã«é…ç½®ã•ã‚Œã¾ã™ã€‚
+
+=== :undercolor » a Shoes::Color ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+テキストã®ä¸‹ç·šã«åˆ©ç”¨ã•ã‚Œã‚‹è‰²ã§ã™ã€‚
+
+=== :underline » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+テキストã«ä¸‹ç·šã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’指示ã—ã¾ã™ã€‚
+
+ã“ã®è¨­å®šã®é¸æŠžè‚¢ã¯ï¼š
+
+ * "none" - 下線ãªã—。 
+ * "single" - 途切れã®ãªã„下線。
+ * "double" - 平行ãªé€”切れã®ãªã„2本ã®ä¸‹ç·šã€‚
+ * "low" - フォントã®åŸºæº–より下ã®ä½Žã„下線。(一般的ã«ï¼‘ã¤ã®æ–‡å­—ã«å¯¾ã—ã¦ã ã‘ã€å¾—ã«ã‚­ãƒ¼ãƒœãƒ¼ãƒ‰ã‚¢ã‚¯ã‚»ãƒ©ãƒ¬ãƒ¼ã‚¿ã‚’表示ã™ã‚‹ã¨ãã«æŽ¨å¥¨ã•ã‚Œã¾ã™ã€‚)
+ * "error" - 波状ã®ä¸‹ç·šã€é€šå¸¸ã¯ãƒŸã‚¹ã‚¹ãƒšãƒ«ã®æŒ‡æ‘˜ã‚’見ã¤ã‘ã¾ã™ã€‚
+
+=== :variant » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+テキストã®ã‚°ãƒ«ãƒ¼ãƒ—ã®ãŸã‚ã«ãƒ•ã‚©ãƒ³ãƒˆã‚’変化ã•ã›ã¾ã™ã€‚2ã¤ã®é¸æŠžè‚¢ï¼š
+
+ * "normal" - 通常ã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * "smallcaps" - 大文字ãŒå°ã•ã変化ã™ã‚‹ã“ã¨ã«ã‚ˆã£ã¦ç½®ãæ›ãˆã‚‰ã‚ŒãŸå°æ–‡å­—ã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+
+=== :weight » a string ===
+
+''banner, caption, code, del, em, ins, inscription, link, para, span, strong, sub, sup, subtitle, tagline, title''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+Set the boldness of the text. Commonly, this style is set to one of the following strings:
+テキストを太文字ã«è¨­å®šã—ã¾ã™ã€‚一般的ã«ã¯ã€ã“ã®ã‚¹ã‚¿ã‚¤ãƒ«ã¯æ¬¡ã®æ–‡å­—列ã®å†…ã®1ã¤ã‚’設定ã—ã¾ã™ï¼š
+
+ * "ultralight" - 超軽é‡ã®å¤ªã• (= 200)
+ * "light" - 軽é‡ã®å¤ªã• (=300)
+ * "normal" - 通常ã®å¤ªã• (= 400)
+ * "semibold" - 通常ã¨å¤ªæ–‡å­—ã®ä¸­é–“ã®å¤ªã• (=600)
+ * "bold" - 太文字 (= 700)
+ * "ultrabold" - 極端ãªå¤ªæ–‡å­—ã®å¤ªã• (= 800)
+ * "heavy" - é‡åŽšãªå¤ªã• (= 900)
+
+ã—ã‹ã—ãªãŒã‚‰ã€æ•°å€¤ã§å¤ªã•ã‚’直接渡ã™ã“ã¨ã‚‚ã§ãã¾ã™ã€‚
+
+=== :width » a number ===
+
+''ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã¨è¦ç´ ''ã§åˆ©ç”¨ã§ãã¾ã™ã€‚
+
+è¦ç´ ã®å¹…をピクセルã§è¨­å®šã—ã¾ã™ã€‚数値ãŒ10進数ãªã‚‰ã€ãã®å¹…ã¯ãƒ‘ーセンテージã«å¤‰æ›ã•ã‚Œã¾ã™ã€‚(0.0ã¯0%ã«ã€1.0ã¯100%ã«ãªã‚Šã¾ã™ã€‚)100%ã®å¹…ã¯è¦ªã®ã‚¹ãƒ­ãƒƒãƒˆã‚’埋ã‚å°½ãã™ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚
+
+== Classes List ==
+
+Shoesã§ç´¹ä»‹ã™ã‚‹ã™ã¹ã¦ã®ã‚¯ãƒ©ã‚¹ã®å®Œå…¨ãªä¸€è¦§ã§ã™ã€‚ã“ã®è¡¨ã¯ã©ã®ã‚ˆã†ã«ã—ã¦ã‚¯ãƒ©ã‚¹ãŒãŠäº’ã„ã«ç¶™æ‰¿ã—ã¦ã„ã‚‹ã‹ã«å¾“ã£ã¦ãƒ¬ã‚¤ã‚¢ã‚¦ãƒˆã•ã‚Œã¦ã„ã¾ã™ã€‚サブクラスã¯è¦ªã‚¯ãƒ©ã‚¹ã®ä¸‹ã«ã€1レベルå³ã«ã‚¤ãƒ³ãƒ‡ãƒ³ãƒˆã•ã‚Œã¦ã„ã¾ã™ã€‚
+
+{INDEX}
+
+== Colors List ==
+
+以下ã¯Shoes全体ã§åˆ©ç”¨å¯èƒ½ãªè‰²ã®ä¸€è¦§ã§ã™ã€‚背景色ã¾ãŸã¯æž ç·šã®è‰²ã¨ã—ã¦ã€‚æã‹ã‚Œã‚‹ç·šã‚„å¡—ã‚Šã¤ã¶ã•ã‚Œã‚‹è‰²ã¨ã—ã¦ã€‚ã“れらã®è‰²ã®å¤§éƒ¨åˆ†ã¯X11ã‚„HTMLã®ãƒ‘レットã‹ã‚‰æ¥ã¦ã„ã¾ã™ã€‚
+
+ã“れらã®è‰²ã¯ã™ã¹ã¦åå‰ã«ã‚ˆã£ã¦åˆ©ç”¨ã§ãã¾ã™ã€‚(ãã®ãŸã‚ã€ã©ã‚“ãªã‚¹ãƒ­ãƒƒãƒˆã®å†…部ã‹ã‚‰ã§ã‚‚`tomato`メソッドを呼ã¶ã“ã¨ã§ã€ã™ã¦ããªèµ¤ã¿ãŒã‹ã£ãŸè‰²ãŒæ‰‹ã«å…¥ã‚‹ã§ã—ょã†ã€‚)ãã‚Œãžã‚Œã®è‰²ã®ä¸‹ã«ã¯ã€[[Built-in.rgb]]メソッドã§åˆ©ç”¨ã§ãる正確ãªæ•°å€¤ã‚’見ã¤ã‘ã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚
+
+{COLORS}
+
+= Slots =
+
+スロットã¯ç”»åƒã‚„テキストãªã©ã®ãƒ¬ã‚¤ã‚¢ã‚¦ãƒˆã«ä½¿ã‚れる箱ã§ã™ã€‚2ã¤ã®æœ€ã‚‚一般的ãªã‚¹ãƒ­ãƒƒãƒˆã¯`スタック(stack)`ã¨`フロー(flow)`ã§ã™ã€‚スロットã¯Shoesã®å°‚門用語ã§"ç®±"ã¾ãŸã¯"キャンãƒã‚¹"ã¨ã‚‚言ãˆã¾ã™
+
+マウスホイールやページアップやページダウンã¯ã‚らゆるプラットフォームã§æ™®åŠã—ã¦ã„ã‚‹ãŸã‚ã€ç¸¦ã®ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ã ã‘ãŒæº¢ã‚Œã¦å•é¡Œã«ãªã‚Šã¾ã—ãŸã€‚ãã®ãŸã‚Shoesã§ã¯ã€ã¾ã•ã«WEBã®ã‚ˆã†ã«é€šå¸¸ã¯å¹…ãŒå›ºå®šã§ã™ã€‚一方ã§é«˜ã•ã¯éš›é™ãªã続ã„ã¦è¡Œãã¾ã™ã€‚
+
+ã•ã¦ã€ãã†ã—ãŸã„ãªã‚‰ã€ã©ã‚“ãªã‚‚ã®ã§ã‚‚幅や高ã•ã‚’指定ã™ã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚ãã‚Œã¯ã„ãらã‹ã®æ•°å­¦ã‚’用ã„ã‚‹ã§ã—ょã†ãŒã€å®Œç’§ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。
+
+一般的ã«ã¯ã€ã‚¹ã‚¿ãƒƒã‚¯ã¨ãƒ•ãƒ­ãƒ¼ã‚’使ã†ã“ã¨ã‚’æ案ã™ã‚‹ã§ã—ょã†ã€‚ã“ã“ã§ã®ç›®çš„ã¯ã€ã‚ãªãŸãŒã„ãらã‹ã®å¹…を何ã‹ã§æº€ãŸã—ã¦ã€å¹…を満ãŸã—ãªãŒã‚‰ã€ãƒšãƒ¼ã‚¸ã®ä¸‹ã«é€²ã¿ãŸã„ã¨ã„ã†ã“ã¨ã§ã™ã€‚ã“れらをã€HTMLã®"ブロック"ã¨"インライン"ã®ã‚¹ã‚¿ã‚¤ãƒ«ã¨é¡žä¼¼ã—ã¦ã„ã‚‹ã®ã‚ˆã†ã«è€ƒãˆã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+==== Stacks ====
+
+スタックã¯å˜ç´”ã«è¦ç´ ã®åž‚ç›´ãªã‚¹ã‚¿ãƒƒã‚¯ã§ã™ã€‚スタック内ã®ãã‚Œãžã‚Œã®è¦ç´ ã¯ã€ãã®ä¸Šä½ã®è¦ç´ ã®ç›´ä¸‹ã«é…ç½®ã•ã‚Œã¾ã™ã€‚
+
+ã¾ãŸã€ã‚¹ã‚¿ãƒƒã‚¯ã¯ç®±ã®ã‚ˆã†ã«å½¢ä½œã‚‰ã‚Œã¾ã™ã€‚ãã®ãŸã‚ã€ã‚¹ã‚¿ãƒƒã‚¯ãŒ250ã®å¹…を与ãˆã‚‰ã‚ŒãŸã‚‰ã€ãã®ã‚¹ã‚¿ãƒƒã‚¯ã¯ãれ自身ãŒ250ピクセルã®å¹…ã®è¦ç´ ã¨ãªã‚Šã¾ã™ã€‚
+
+æ–°ã—ã„スタックを作æˆã™ã‚‹ã«ã¯[[Element.stack]]メソッドを利用ã—ã€ãã‚Œã¯ã™ã¹ã¦ã®ã‚¹ãƒ­ãƒƒãƒˆã®å†…部ã§åˆ©ç”¨ã§ãã¾ã™ã€‚ãã®ãŸã‚スタックã¯ä»–ã®ã‚¹ã‚¿ãƒƒã‚¯ã‚„フローをå«ã‚€ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+==== Flows ====
+
+フローã¯è¦ç´ ã‚’ã§ãã‚‹ã ã‘ã—ã£ã‹ã‚Šã¨è©°ã‚è¾¼ã¿ã¾ã™ã€‚å¹…ã¯æº€ãŸã•ã‚Œã€ãれらã®ä¸‹ã®è¦ç´ ã‚’包ã¿ã¾ã™ã€‚互ã„ã«éš£æŽ¥ã—ã¦é…ç½®ã•ã‚ŒãŸãƒ†ã‚­ã‚¹ãƒˆã®è¦ç´ ã¯ä¸€ã¤ã®ãƒ‘ラグラフã¨ã—ã¦è¡¨ç¤ºã•ã‚Œã¾ã™ã€‚ç”»åƒã¨ã‚¦ã‚£ã‚¸ã‚§ãƒƒãƒˆã¯åŒã˜ã‚·ãƒªãƒ¼ã‚ºã¨ã—ã¦å®Ÿè¡Œã•ã‚Œã¾ã™ã€‚
+
+スタックã®ã‚ˆã†ã«ã€ãƒ•ãƒ­ãƒ¼ã¯ç®±ã§ã™ã€‚ãã®ãŸã‚スタックã¨ãƒ•ãƒ­ãƒ¼ã¯å®‰å…¨ã«åŸ‹ã‚込むã“ã¨ãŒã§ãã€ãれらã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„ã«æ°—を使ã†ã“ã¨ãªãã€åŒè³ªã§ã™ã€‚スタックã¨ãƒ•ãƒ­ãƒ¼ã¯ãれらã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„ã ã‘ã‚’ç•°ãªã£ã¦æ‰±ã„ã¾ã™ã€‚
+
+フローを作æˆã™ã‚‹ã«ã¯[[Element.flow]]を呼ã³ã¾ã™ã€‚フローã¯ä»–ã®ãƒ•ãƒ­ãƒ¼ã‚„スタックをå«ã‚€ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。
+
+最後ã«ï¼šShoesã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã¯ãれ自身ãŒãƒ•ãƒ­ãƒ¼ã§ã™ã€‚
+
+== Art for Slots ==
+
+ãã‚Œãžã‚Œã®ã‚¹ãƒ­ãƒƒãƒˆã¯ã€è‰²ã®ã¤ã„ãŸå½¢çŠ¶ã‚„グラデーションãªã©ã§ãŠãŠã†ã“ã¨ã«ã§ãる白紙ã®è¡¨é¢ã®ã‚­ãƒ£ãƒ³ãƒã‚¹ã®ã‚ˆã†ãªã‚‚ã®ã§ã™ã€‚
+
+多ãã®ä¸€èˆ¬çš„ãªå½¢çŠ¶ã¯`oval`ã¨`rect`メソッドã§æã‹ã‚Œã¾ã™ã€‚ã¾ãšã€çµµç­†ã®è‰²ã‚’準備ã™ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ã€‚
+
+`stroke`コマンドã¯ç·šã®è‰²ã‚’設定ã—ã¾ã™ã€‚`fill`コマンドã¯ç·šã®å†…部を塗りã¤ã¶ã™ãŸã‚ã«åˆ©ç”¨ã™ã‚‹è‰²ã‚’設定ã—ã¾ã™ã€‚ 
+
+{{{
+ #!ruby
+ Shoes.app do
+ stroke red
+ fill blue
+ oval :top => 10, :left => 10,
+ :radius => 100
+ end
+}}}
+
+ã“ã®ã‚³ãƒ¼ãƒ‰ã¯ã¾ã‚ã‚Šã«èµ¤ã„ç·šã®ã‚ã‚‹é’ã„パイを与ãˆã¾ã™ã€‚100ピクセルã®å¹…ã§ã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã®å·¦ä¸Šã‹ã‚‰å—æ±ã«å°‘ã—ã®ãƒ”クセルをé…ç½®ã—ã¾ã™ã€‚
+
+上記ã®`blue`ã¨`red`メソッドã¯ã‚«ãƒ©ãƒ¼ã‚ªãƒ–ジェクトã§ã™ã€‚ã©ã†ã‚„ã£ã¦è‰²ã‚’æ··ãœã‚‹ã‹ã¯Colorsセクションを見ã¦ãã ã•ã„。
+
+==== Processing 㨠NodeBox ã‹ã‚‰ã®ã‚¤ãƒ³ã‚¹ãƒ”レーション ====
+
+ã“ã®æŠ€å·§çš„ãªãƒ¡ã‚½ãƒƒãƒ‰ã®å¤§æŠµã¯æ–‡å­—通りPythonã®ãƒ‰ãƒ­ãƒ¼ã‚¤ãƒ³ã‚°ã‚­ãƒƒãƒˆã®NodeBoxã‹ã‚‰æ¥ã¦ã„ã¾ã™ã€‚次ã«ã€NodeBoxã¯å¤šãã®ã‚¢ã‚¤ãƒ‡ã‚¢ã‚’ã€ã‚°ãƒ©ãƒ•ã‚£ãƒƒã‚¯ã¨ã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã®ãŸã‚ã®Javaã®ã‚ˆã†ãªè¨€èªžã§ã‚ã‚‹Processingã‹ã‚‰å¾—ã¦ã„ã¾ã™ã€‚ç§ã¯ãれらã®ã™ã°ã‚‰ã—ã„プログラムã®ä½œè€…ã‹ã‚‰å¤§ããªæ©ã‚’å—ã‘ã¦ã„ã¾ã™ã€‚
+
+Shoesã¯NodeBoxã¨Processingã‹ã‚‰å°‘ã—ã®ç‚¹ãŒé•ã„ã¾ã™ã€‚例ãˆã°ã€Shoesã¯ãれ自身ã®ã‚«ãƒ©ãƒ¼ã‚ªãƒ–ジェクトをæŒã£ã¦ã„ã‚‹ã“ã¨ã‚’å«ã‚ã¦ã€ç•°ãªã‚‹ã‚«ãƒ©ãƒ¼ãƒ¡ã‚½ãƒƒãƒ‰ã‚’æŒã£ã¦ã„ã¾ã™ãŒã€ãれらã¯ã¨ã¦ã‚‚Processingã®ã‚«ãƒ©ãƒ¼ãƒ¡ã‚½ãƒƒãƒ‰ã«ä¼¼ã¦ã„ã¾ã™ã€‚ãã—ã¦Shoesã¯ç·šã‚’æãã“ã¨ã‚„形状内を塗りã¤ã¶ã™ãŸã‚ã«ç”»åƒã‚„グラデーションを利用ã™ã‚‹ã“ã¨ã‚‚許ã—ã¦ã„ã¾ã™ã€‚
+
+Shoesã¯ã„ãã¤ã‹ã®ã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã®ã‚¢ã‚¤ãƒ‡ã‚¢ã‚’Processingã‹ã‚‰å–り入れã¦ãŠã‚Šã€Processingã®ãƒ¡ã‚½ãƒƒãƒ‰ã‚’ã—ã£ã‹ã‚Šã¨å‚考ã«ã—よã†ã¨ã—ãªãŒã‚‰ãれを拡張ã—ã¦ã„ã¾ã™ã€‚
+
+=== arc(left, top, width, height, angle1, angle2) » Shoes::Shape ===
+
+弧ã®å½¢çŠ¶ï¼ˆæ¥•å††å½¢ã®ä¸€éƒ¨ï¼‰ã‚’座標(left, top)ã«æãã¾ã™ã€‚ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯`:angle1`ã¨`:angle2`ã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’æä¾›ã™ã‚‹ã“ã¨ã«ã‚ˆã‚Š [[oval]]より少ã—多ãã®åˆ¶å¾¡ã‚’ãŒè¡Œãˆã¾ã™ã€‚(実際ã«ã¯ã€`:angle1`ã«0ã¨`:angle2`ã«`Shoes::TWO_PI`を設定ã™ã‚‹ã“ã¨ã«ã‚ˆã‚Šã€`oval`メソッドをã¾ã­ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚)
+
+=== arrow(left, top, width) » Shoes::Shape ===
+
+座標(left, top)ã«`width`ピクセルã®çŸ¢ã‚’æãã¾ã™ã€‚
+
+=== cap(:curve or :rect or :project) » self ===
+
+æãã™ã¹ã¦ã®ç·šã®çµ‚点ã®å½¢çŠ¶ã§ã‚ã‚‹ç·šã®é ‚点を設定ã—ã¾ã™ã€‚`:curve`ãŒè¨­å®šã•ã‚Œã‚‹ãªã‚‰ã€çµ‚点ã¯ã¾ã‚‹ããªã‚Šã¾ã™ã€‚デフォルトã¯`:rect`ã§ã€ç·šã®çµ‚点ã¯çªç„¶ã«å¹³ã‚‰ã«ãªã‚Šã¾ã™ã€‚`:project`ã®é ‚点も平らã§ã™ãŒã€æ£’ã®å¤–å´ã¯å°‘ã—é•·ããªã‚Šã¾ã™ã€‚
+
+=== fill(pattern) » pattern ===
+
+å¡—ã‚Šã¤ã¶ã™ãƒã‚±ãƒ„ã®è‰²ã®æŒ‡å®šï¼ˆã¾ãŸã¯ãƒ‘ターン)を設定ã—ã¾ã™ã€‚パターンã¯è‰²ã‚„グラデーションã¾ãŸã¯ç”»åƒãŒè¨­å®šã§ãã¾ã™ã€‚ãã—ã¦ã€ä¸€åº¦å¡—ã‚Šã¤ã¶ã™ãƒã‚±ãƒ„ãŒè¨­å®šã•ã‚ŒãŸã‚‰ã€é¸æŠžã•ã‚ŒãŸãƒ‘ターンã§è‰²ã¥ã‘られãŸå½¢çŠ¶ã‚’æãã“ã¨ãŒã§ãã¾ã™ã€‚
+
+ç”»åƒã®ãƒ‘ターンã§æ˜Ÿã‚’æããŸã‚ã«ã¯ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ fill "static/avatar.png"
+ star 200, 200, 5
+ end
+}}}
+
+å¡—ã‚Šã¤ã¶ã™ãƒã‚±ãƒ„をクリアã™ã‚‹ã«ã¯`nofill`を使ã£ã¦ãã ã•ã„。ãã—ã¦`stroke`メソッドを利用ã—ã¦ç·šï¼ˆæ˜Ÿã®æž ç·šï¼‰ã®è‰²ã‚’設定ã—ã¦ãã ã•ã„。
+
+=== nofill() » self ===
+
+å¡—ã‚Šã¤ã¶ã™è‰²ã‚’削除ã™ã‚‹ãŸã‚ã€å½¢çŠ¶ã¯å¡—ã‚Šã¤ã¶ã•ã‚Œãšã«æã‹ã‚Œã¾ã™ã€‚ãã®ä»£ã‚ã‚Šã«ã€å½¢çŠ¶ã¯ç·šã ã‘ã‚’æŒã¡ã€ä¸­å¤®ã‚’é€æ˜Žã®ã¾ã¾ã«ã—ã¾ã™ã€‚
+
+=== nostroke() » self ===
+
+ç·šã®è‰²ã‚’空ã«ã—ã¾ã™ã€‚形状ã¯å¤–å´ã®ç·šãŒæã‹ã‚Œãªããªã‚Šã¾ã™ã€‚`nofill`も設定ã•ã‚ŒãŸå ´åˆã¯ã€å½¢çŠ¶ã¯è¡¨ç¤ºã•ã‚Œãšã«æã‹ã‚Œã¾ã™ã€‚
+
+=== line(left, top, x2, y2) » Shoes::Shape ===
+
+ç¾åœ¨ã®ç·šã®è‰²ï¼ˆåˆ¥å"stroke")を使ã£ã¦åº§æ¨™ï¼ˆleft, top)ã‹ã‚‰ï¼ˆx2, y2)ã¾ã§ç·šã‚’æãã¾ã™ã€‚
+
+=== oval(left, top, radius) » Shoes::Shape ===
+
+座標(left, top)ピクセルã«`radius`ピクセルã®å¹…ã¨é«˜ã•ã®å††ã‚’æãã¾ã™ã€‚ç·šã®è‰²ã‚„å¡—ã‚Šã¤ã¶ã™è‰²ãŒå½¢çŠ¶ã‚’æããŸã‚ã«åˆ©ç”¨ã•ã‚Œã¾ã™ã€‚デフォルトã§ã¯ã€åº§æ¨™ã¯æ¥•å††å½¢ã®æœ€ã‚‚左上ã®è§’ã§ã™ãŒã€ã“ã‚Œã¯[[Art.transform]]メソッドを呼ã¶ã“ã¨ã‚„ã€æ¬¡ã®ãƒ¡ã‚½ãƒƒãƒ‰ã®ä¸‹ã®`:center`スタイルを使用ã™ã‚‹ã“ã¨ã«ã‚ˆã£ã¦å¤‰æ›´ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stroke blue
+ strokewidth 4
+ fill black
+
+ oval 10, 10, 50
+ end
+}}}
+
+様々ãªæ¯”率ã®æ¥•å††å½¢ã‚’æããŸã‚ã«ã¯ã€`oval(left, top, width, height)`ã®ã‚·ãƒ³ã‚¿ãƒƒã‚¯ã‚¹ã‚’利用ã—ã¦ã‚‚ã„ã„ã§ã™ã€‚
+
+=== oval(styles) » Shoes::Shape ===
+
+スタイルã®ãƒãƒƒã‚·ãƒ¥ã‚’利用ã—ã¦å††ã‚’æãã¾ã™ã€‚次ã®ã‚¹ã‚¿ã‚¤ãƒ«ãŒã‚µãƒãƒ¼ãƒˆã•ã‚Œã¦ã„ã¾ã™ï¼š
+
+ * `top`: 楕円形ã®å›²ã„ã®y座標。
+ * `left`: 楕円形ã®å›²ã„ã®x座標。
+ * `radius`: 円ã®å¹…ã¨é«˜ã•ã€‚
+ * `width`: 楕円形ã®å¹…ã®ãƒ”クセルã§ã®æŒ‡å®šã€‚
+ * `height`: 楕円形ã®é«˜ã•ã®ãƒ”クセルã§ã®æŒ‡å®šã€‚
+ * `center`: 座標を楕円形ã®ä¸­å¤®ã«æŒ‡å®šã—ã¾ã™ã‹ï¼Ÿï¼ˆtrueã¾ãŸã¯false)
+
+ã“れらã®ã‚¹ã‚¿ã‚¤ãƒ«ã¯Shoesオブジェクトã®`style`メソッドを利用ã—ã¦å¤‰æ›´ã•ã‚Œã¾ã™ã€‚
+
+=== rect(top, left, width, height, corners = 0) » Shoes::Shape ===
+
+座標(left, top)ã‹ã‚‰width x heightã®å¯¸æ³•ã®é•·æ–¹å½¢ã‚’æãã¾ã™ã€‚オプションã¨ã—ã¦ã€5番目ã®å¼•æ•°ï¼ˆãƒ”クセルã§ã®è§’ã®åŠå¾„)ã«ã‚ˆã‚Šé•·æ–¹å½¢ã®è§’を丸ãã™ã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚ä»–ã®å½¢çŠ¶ã¨åŒæ§˜ã«ã€é•·æ–¹å½¢ã¯æãç·šã®è‰²ã‚„å¡—ã‚Šã¤ã¶ã™è‰²ã‚’利用ã—ã¦æã‹ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stroke rgb(0.5, 0.5, 0.7)
+ fill rgb(1.0, 1.0, 0.9)
+ rect 10, 10, self.width - 20, self.height - 20
+ end
+}}}
+
+上記ã®ã‚µãƒ³ãƒ—ルã¯è§’ã®å‘¨å›²ã«10ピクセルã®ãƒžãƒ¼ã‚¸ãƒ³ã‚’残ã—ã¦ã€ãã®è¦ªã®ç®±ã®ç¯„囲を塗りã¤ã¶ã™é•·æ–¹å½¢ã‚’æãã¾ã™ã€‚デフォルトã§è¦ªã®ç®±ã‚’å¡—ã‚Šã¤ã¶ã—ãŸé•·æ–¹å½¢ã®ãŸã‚ã«ã¯`background`も見ã¦ãã ã•ã„。
+
+=== rect(styles) » Shoes::Shape ===
+
+スタイルã®ãƒãƒƒã‚·ãƒ¥ã‚’利用ã—ã¦é•·æ–¹å½¢ã‚’æãã¾ã™ã€‚次ã®ã‚¹ã‚¿ã‚¤ãƒ«ãŒã‚µãƒãƒ¼ãƒˆã•ã‚Œã¦ã„ã¾ã™ï¼š
+
+ * `top`: 長方形ã®y座標。
+ * `left`: 長方形ã®x座標。
+ * `curve`: 長方形ã®è§’ã®åŠå¾„ã®ãƒ”クセル。
+ * `width`: 長方形ã®ãƒ”クセルã«ã‚ˆã‚‹å¹…。
+ * `height`:長方形ã®ãƒ”クセルã«ã‚ˆã‚‹é«˜ã•ã€‚
+ * `center`: 座標を長方形ã®ä¸­å¤®ã«æŒ‡å®šã—ã¾ã™ã‹ï¼Ÿï¼ˆtrueã¾ãŸã¯false)
+
+ã“れらã®ã‚¹ã‚¿ã‚¤ãƒ«ã¯Shoesオブジェクトã®`style`メソッドを利用ã—ã¦å¤‰æ›´ã•ã‚Œã¾ã™ã€‚
+
+=== rotate(degrees: a number) » self ===
+
+形状をãã®è§’度ã§æç”»ã™ã‚‹ãŸã‚ã«ã€`度(degrees)`æ•°ã«ã‚ˆã‚Šæç”»ã®ãŸã‚ã«åˆ©ç”¨ã•ã‚Œã‚‹ç¯„囲を回転ã•ã›ã¾ã™ã€‚
+
+下ã®ä¾‹ã§ã¯ã€é•·æ–¹å½¢ã¯(30, 30)ã«45度回転ã•ã‚Œã¦æã‹ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ fill "#333"
+ rotate 45
+ rect 30, 30, 40, 40
+ end
+}}}
+
+=== shape(left, top) { ... } » Shoes::Shape ===
+
+(left, top)ã‹ã‚‰é–‹å§‹ã—ã¦ãƒ–ロックã®å†…部ã§`line_to`ã€`move_to`ã€`curve_to`ãã—ã¦`arc_to`を呼ã¶ã“ã¨ã«ã‚ˆã‚Š
+続ãã€æã‹ã‚Œã‚‹ä»»æ„ã®å½¢çŠ¶ã‚’表ç¾ï¼ˆè¨˜è¿°ï¼‰ã—ã¾ã™
+
+曲ãŒã£ãŸã‚Šå¼§ã‚’æã„ãŸã‚Šã™ã‚‹é•·ã„ç·šã®å½¢çŠ¶ã‚’スケッãƒã¨ã—ã¦è¦‹ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ fill red(0.2)
+ shape do
+ move_to(90, 55)
+ arc_to(50, 55, 50, 50, 0, PI/2)
+ arc_to(50, 55, 60, 60, PI/2, PI)
+ arc_to(50, 55, 70, 70, PI, TWO_PI-PI/2)
+ arc_to(50, 55, 80, 80, TWO_PI-PI/2, TWO_PI)
+ end
+ end
+}}}
+
+形状ã¯ä»–ã®å½¢çŠ¶ã‚’å«ã‚€ã“ã¨ã‚‚ã§ãã¾ã™ã€‚ãã—ã¦ã€å½¢çŠ¶ã®å†…部ã«[[Art.oval]]ã€[[Art.rect]]ã€[[Art.line]]ã€[[Art.star]]ã¾ãŸã¯[[Art.arrow]](ã•ã‚‰ã«ã€[[Art]]セクションã®ä»–ã®ãƒ¡ã‚½ãƒƒãƒ‰ã™ã¹ã¦ï¼‰ã‚’é…ç½®ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ãŒã€ãれらã¯ç·šã®ä¸€éƒ¨ã§ã¯ãªã„ã§ã—ょã†ã€‚形状ã®ã‚°ãƒ«ãƒ¼ãƒ—ã®ã‚ˆã†ãªãれらã¯ã€ã™ã¹ã¦1ã¤ã¨ã—ã¦æã‹ã‚Œã¾ã™ã€‚
+
+=== star(left, top, points = 10, outer = 100.0, inner = 50.0) » Shoes::Shape ===
+
+æãç·šã®è‰²ã‚„å¡—ã‚Šã¤ã¶ã™è‰²ã‚’利用ã—ã¦æ˜Ÿã‚’æãã¾ã™ã€‚星ã¯(left, top)ã®åº§æ¨™ã‚’中心点ã¨ã—ã¦`頂点(points)`ã®æ•°ã¨ã¨ã‚‚ã«é…ç½®ã•ã‚Œã¾ã™ã€‚`outer`ã®å¹…ã¯æ˜Ÿã®å…¨åŠå¾„をを定義ã—ã¾ã™ï¼›`inner`ã®å¹…ã¯é ‚点ã®å§‹ã¾ã‚‹æ˜Ÿã®ä¸­å¤®ã®åŠå¾„を指定ã—ã¾ã™ã€‚
+
+=== stroke(pattern) » pattern ===
+
+スロットã®ã‚¢ã‚¯ãƒ†ã‚£ãƒ–ãªç·šã®è‰²ã‚’設定ã—ã¾ã™ã€‚`pattern`ã¯è‰²ã€ã‚°ãƒ©ãƒ‡ãƒ¼ã‚·ãƒ§ãƒ³ã¾ãŸã¯ç”»åƒã§ã€ãれらã¯ã™ã¹ã¦"patterns"ã«åˆ†é¡žã•ã‚Œã¾ã™ã€‚ãã®ç·šã®è‰²ã¯ãã‚Œã«ç¶šã形状ã™ã¹ã¦ã®æž ç·šã‚’æãã¨ãã«åˆ©ç”¨ã•ã‚Œã¾ã™ã€‚
+
+ã¤ã¾ã‚Šã€ã¾ã‚ã‚Šã«èµ¤ã„ç·šã‚’æŒã¤çŸ¢å°ã‚’æãã«ã¯ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ stroke red
+ arrow 0, 100, 10
+ end
+}}}
+
+ç·šã®è‰²ã‚’クリアã™ã‚‹ã«ã¯ã€`nostroke`メソッドを利用ã—ã¾ã™ã€‚
+
+=== strokewidth(a number) » self ===
+
+スロットã®å†…部ã§æã‹ã‚Œã‚‹ã™ã¹ã¦ã®ç·šã®ã‚µã‚¤ã‚ºã‚’設定ã—ã¾ã™ã€‚`stroke`メソッドãŒç·šã®è‰²ã‚’変更ã™ã‚‹ä¸€æ–¹ã€`strokewidth`メソッドã¯ç·šã®ã‚µã‚¤ã‚ºã‚’ピクセルã§å¤‰æ›´ã—ã¾ã™ã€‚`strokewidth(4)`を呼ã¶ã“ã¨ã«ã‚ˆã‚Š4ピクセルã®å¤ªã•ã§ç·šã‚’æãã¾ã™ã€‚
+
+=== transform(:center or :corner) » self ===
+
+(`skew`ã‚„`rotate`ã®ã‚ˆã†ãªï¼‰å¤‰åŒ–ã¯å½¢çŠ¶ã®ä¸­å¿ƒã‚ãŸã‚Šã§å®Ÿè¡Œã•ã‚Œã‚‹ã¹ãã§ã™ã‹ï¼Ÿã¾ãŸã¯ãã®å½¢çŠ¶ã®è§’ã§ã™ã‹ï¼ŸShoesã®åˆæœŸå€¤ã¯`:corner`ã§ã™ã€‚
+
+=== translate(left, top) » self ===
+
+スロットã®æç”»ã®ç¯„囲を開始ã™ã‚‹ä½ç½®ã‚’移動ã—ã¾ã™ã€‚通常ã¯ã€ã™ã¹ã¦ã®å½¢çŠ¶ãŒã“ã®ä½ç½®ã‹ã‚‰æãã“ã¨ãŒã§ãるよã†ã«ã€ãã®ç¯„囲ã¯å·¦ä¸Šã®è§’ã®(0, 0)ã‹ã‚‰é–‹å§‹ã—ã¾ã™ã€‚`translate`ã«ã‚ˆã‚Šé–‹å§‹ä½ç½®ãŒ(10, 20)ã«ç§»å‹•ã•ã‚Œã¦ã€(50, 60)ã«å½¢çŠ¶ãŒæã‹ã‚Œã‚‹ãªã‚‰ã€ãã®å½¢çŠ¶ã¯å®Ÿéš›ã«ã¯ã‚¹ãƒ­ãƒƒãƒˆã®(60, 80)ã«æã‹ã‚Œã¾ã™ã€‚
+
+== Element Creation ==
+
+Shoesã¯å¹…広ã„種類ã®è¦ç´ ã‚’æŒã£ã¦ãŠã‚Šã€ãã®å¤šãã‚’HTMLã‹ã‚‰ã‚ˆã„ã¨ã“ã‚ã‚’é¸ã‚“ã§æŽ¡ç”¨ã—ã¦ã„ã¾ã™ã€‚ã“ã®ãƒšãƒ¼ã‚¸ã¯ã©ã®ã‚ˆã†ã«ã—ã¦ã‚¹ãƒ­ãƒƒãƒˆã«ãれらã®è¦ç´ ã‚’作æˆã™ã‚‹ã‹ã‚’記述ã—ã¦ã„ã¾ã™ã€‚ãれらã®è¦ç´ ã‚’é…ç½®ã—ãŸå¾Œã§ã•ã‚‰ã«ã©ã®ã‚ˆã†ã«ã—ã¦å¤‰æ›´ã—ãŸã‚Šåˆ©ç”¨ã™ã‚‹ã®ã‹ã«ã¤ã„ã¦ã¯ã€ã“ã®ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã®è¦ç´ ï¼ˆElements)ã®ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã‚’見ã¦ãã ã•ã„。
+
+=== animate(fps) { |frame| ... } » Shoes::Animation ===
+
+アプリケーションをãã®ã¾ã¾ã«ã—ã¦ä¸¦åˆ—ã§å®Ÿè¡Œã™ã‚‹ã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã‚¿ã‚¤ãƒžãƒ¼ã‚’開始ã—ã¾ã™ã€‚`fps`ã¯ç§’ã”ã¨ã®ãƒ•ãƒ¬ãƒ¼ãƒ ã®æ•°ã§ã™ã€‚ã“ã®æ•°ã¯ä»˜å±žã™ã‚‹ãƒ–ロックãŒ1秒ã«ä½•å›žå‘¼ã°ã‚Œã‚‹ã®ã‹ã‚’決定ã—ã¾ã™ã€‚
+
+ã“ã®ãƒ–ロックã¯`frame`ã®æ•°ãŒä¸Žãˆã‚‰ã‚Œã¾ã™ã€‚`frame`ã®æ•°ã¯ã‚¼ãƒ­ã‹ã‚‰å§‹ã¾ã‚Šã€ãã®ãƒ–ロックãŒä½•ãƒ•ãƒ¬ãƒ¼ãƒ ã®ã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã‚’表示ã—ãŸã®ã‹ã‚’æ•™ãˆã¾ã™
+
+{{{
+ #!ruby
+ Shoes.app do
+ @counter = para "STARTING"
+ animate(24) do |frame|
+ @counter.replace "FRAME #{frame}"
+ end
+ end
+}}}
+
+上記ã®ã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã¯1秒間ã«24回表示ã•ã‚Œã¾ã™ã€‚æ•°ãŒä¸Žãˆã‚‰ã‚Œãªã„ãªã‚‰ã€`fps`ã®ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã¯10ã§ã™ã€‚
+
+=== background(pattern) » Shoes::Background ===
+
+色(ã¾ãŸã¯ãƒ‘ターン)を指定ã—ã¦èƒŒæ™¯ï¼ˆBackground)è¦ç´ ã‚’æãã¾ã™ã€‚パターンã¯è‰²ã€ã‚°ãƒ©ãƒ‡ãƒ¼ã‚·ãƒ§ãƒ³ã¾ãŸã¯ç”»åƒã§ã™ã€‚色ã¨ç”»åƒã¯èƒŒæ™¯å…¨ä½“ã«æ•·ãè©°ã‚ã¾ã™ã€‚グラデーションã¯èƒŒæ™¯ã‚’å¡—ã‚Šã¤ã¶ã™ã‚ˆã†ã«ä¼¸ã³ã¾ã™ã€‚
+
+'''注æ„ã—ã¦ãã ã•ã„:''' 背景ã¯å®Ÿéš›ã«ã¯è¦ç´ ã§ã‚ã‚Šã€ã‚¹ã‚¿ã‚¤ãƒ«ã§ã¯ã‚ã‚Šã¾ã›ã‚“。HTMLã¯èƒŒæ™¯ã‚’スタイルã®ã‚ˆã†ã«ã‚ã¤ã‹ã„ã¾ã™ã€‚ãã‚Œã¯ã™ã¹ã¦ã®ç®±ã¯ä¸€ã¤ã®èƒŒæ™¯ã ã‘ã‚’æŒã¦ã‚‹ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚Shoesã¯èƒŒæ™¯ã®è¦ç´ ã‚’é‡ã­ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ background black
+ background white, :width => 50
+ end
+}}}
+
+上記ã®ä¾‹ã¯äºŒã¤ã®èƒŒæ™¯ã‚’å¡—ã‚Šã¾ã™ã€‚ã¾ãšã€é»’ã„背景ãŒã‚¢ãƒ—リケーションã®è¡¨é¢ã®ã‚¨ãƒªã‚¢å…¨ä½“ã«å¡—られã¾ã™ã€‚ãã—ã¦ã€50ピクセルã®ç™½ã„縞ãŒå·¦å´ã«ãã£ã¦å¡—られã¾ã™ã€‚
+
+=== banner(text) » Shoes::Banner ===
+
+ãƒãƒŠãƒ¼ï¼ˆBanner)ã®ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックを作æˆã—ã¾ã™ã€‚Shoesã¯è‡ªå‹•çš„ã«48ピクセルã®å¤§ãã•ã«ãƒ†ã‚­ã‚¹ãƒˆã‚’æ•´å½¢ã—ã¾ã™ã€‚
+
+=== border(text, :strokewidth => a number) » Shoes::Border ===
+
+色(ã¾ãŸã¯ãƒ‘ターン)を指定ã—ã¦æž ç·šï¼ˆBorder)ã®è¦ç´ ã‚’æãã¾ã™ã€‚パターンã¯è‰²ã€ã‚°ãƒ©ãƒ‡ãƒ¼ã‚·ãƒ§ãƒ³ã¾ãŸã¯ç”»åƒã§ã™ã€‚色ã¨ç”»åƒã¯æž ç·šå…¨ä½“ã«æ•·ãè©°ã‚ã¾ã™ã€‚グラデーションã¯èƒŒæ™¯ã‚’å¡—ã‚Šã¤ã¶ã™ã‚ˆã†ã«ä¼¸ã³ã¾ã™ã€‚
+
+'''注æ„ã—ã¦ãã ã•ã„:''' 背景ã®ã‚ˆã†ã«ã€æž ç·šã¯å®Ÿéš›ã«ã¯è¦ç´ ã§ã‚ã‚Šã€ã‚¹ã‚¿ã‚¤ãƒ«ã§ã¯ã‚ã‚Šã¾ã›ã‚“。HTMLã¯èƒŒæ™¯ã‚„枠線をスタイルã®ã‚ˆã†ã«ã‚ã¤ã‹ã„ã¾ã™ã€‚ãã‚Œã¯ã™ã¹ã¦ã®ç®±ã¯ä¸€ã¤ã®æž ç·šã ã‘ã‚’æŒã¦ã‚‹ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚Shoesã¯ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックã€ç”»åƒã€ä»–ã®ã™ã¹ã¦ã®ã‚‚ã®ã«ãã£ã¦ã€æž ç·šã‚„背景ã®è¦ç´ ã‚’é‡ã­ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+=== button(text) { ... } » Shoes::Button ===
+
+表é¢ã«ã‚ãŸã£ã¦ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸`テキスト(text)`ã®æ›¸ã‹ã‚ŒãŸæŠ¼ã—ボタンを追加ã—ã¾ã™ã€‚
+ボタンãŒæŠ¼ã•ã‚ŒãŸã¨ãã«å‘¼ã°ã‚Œã‚‹ã€ä»»æ„ã®ãƒ–ロックをå–り付ã‘ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+=== caption(text) » Shoes::Caption ===
+
+キャプション(Caption)テキストブロックを作æˆã—ã¾ã™ã€‚Shoesã¯14ピクセルã®å¤§ãã•ã«ã“ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’æ•´å½¢ã—ã¾ã™ã€‚
+
+=== check() » Shoes::Check ===
+
+ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã‚’追加ã—ã¾ã™ã€‚
+
+=== code(text) » Shoes::Code ===
+
+コード(Code)ã®ãƒ†ã‚­ã‚¹ãƒˆã®ä¸€éƒ¨ã‚’作æˆã—ã¾ã™ã€‚ã“ã®ãƒ†ã‚­ã‚¹ãƒˆã¯ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã§ç­‰å¹…フォントã«ãªã‚Šã¾ã™ã€‚
+
+=== del(text) » Shoes::Del ===
+
+デフォルトã§ä¸­å¤®ã«1本ã®æ£’線を引ã„ã¦ãƒ†ã‚­ã‚¹ãƒˆã‚’削除ã—ãŸã€å‰Šé™¤ã•ã‚ŒãŸï¼ˆDel)("deleted"ã®çœç•¥å½¢ï¼‰ãƒ†ã‚­ã‚¹ãƒˆã®ä¸€éƒ¨ã‚’作æˆã—ã¾ã™ã€‚
+
+=== dialog(styles) { ... } » Shoes::App ===
+
+(ã¾ã•ã« [[Element.window]]メソッドを実効ã—ãŸã‚ˆã†ã«ï¼‰æ–°ã—ã„アプリケーションã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’é–‹ãã¾ã™ãŒã€ãã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã¯ãƒ€ã‚¤ã‚¢ãƒ­ã‚°ãƒœãƒƒã‚¯ã‚¹ã®å¤–観を与ãˆã‚‰ã‚Œã¾ã™ã€‚
+
+=== edit_box(text) » Shoes::EditBox ===
+
+スロットã«å¤§ããªãƒžãƒ«ãƒãƒ©ã‚¤ãƒ³ã®ãƒ†ã‚­ã‚¹ãƒˆã‚¨ãƒªã‚¢ã‚’追加ã—ã¾ã™ã€‚
+ã“ã®`text`ã¯ã‚ªãƒ—ションã§ã“ã®ç®±ã®é–‹å§‹æ™‚ã«ä¸Žãˆã‚‰ã‚Œã‚‹æ–‡å­—列ã§ã™ã€‚
+オプションã®ãƒ–ロックをã“ã“ã«å–り付ã‘ã‚‹ã“ã¨ãŒã§ãã€ã“ã‚Œã¯ãƒœãƒƒã‚¯ã‚¹ã®ãƒ†ã‚­ã‚¹ãƒˆã«å¯¾ã™ã‚‹ã©ã‚“ãªç¨®é¡žã®ãƒ¦ãƒ¼ã‚¶ã®å¤‰æ›´ã§ã‚‚呼ã³å‡ºã•ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ edit_box
+ edit_box "HORRAY EDIT ME"
+ edit_box "small one", :width => 100, :height => 160
+ end
+}}}
+
+=== edit_line(text) » Shoes::EditLine ===
+
+スロットã«ä¸€è¡Œã®ãƒ†ã‚­ã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã‚’追加ã—ã¾ã™ã€‚
+ã“ã®`text`ã¯ã‚ªãƒ—ションã§ã“ã®ç®±ã®é–‹å§‹æ™‚ã«ä¸Žãˆã‚‰ã‚Œã‚‹æ–‡å­—列ã§ã™ã€‚
+オプションã®ãƒ–ロックをã“ã“ã«å–り付ã‘ã‚‹ã“ã¨ãŒã§ãã€ã“ã‚Œã¯ãƒœãƒƒã‚¯ã‚¹ã®ãƒ†ã‚­ã‚¹ãƒˆã«å¯¾ã™ã‚‹ã©ã‚“ãªç¨®é¡žã®ãƒ¦ãƒ¼ã‚¶ã®å¤‰æ›´ã§ã‚‚呼ã³å‡ºã•ã‚Œã¾ã™ã€‚
+
+=== em(text) » Shoes::Em ===
+
+Em("emphasized"ã®çœç•¥å½¢ï¼‰ãƒ†ã‚­ã‚¹ãƒˆã®ä¸€éƒ¨ã‚’作æˆã—ã€ã“ã‚Œã¯ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã§ã‚¤ã‚¿ãƒªãƒƒã‚¯ä½“ã§æ•´å½¢ã•ã‚Œã¾ã™ã€‚
+
+=== every(seconds) { |count| ... } » Shoes::Every ===
+
+`animation`メソッドã¨ã‚ˆãä¼¼ãŸã‚¿ã‚¤ãƒžãƒ¼ã§ã™ãŒã€ã‚ˆã‚Šéžå¸¸ã«é…ã„ã§ã™ã€‚ã“ã®ã‚¿ã‚¤ãƒžãƒ¼ã¯ä¸Žãˆã‚‰ã‚ŒãŸæ•°ã®ç§’(seconds)ã§ã€å–り付ã‘られãŸãƒ–ロックを実行ã—ã¾ã™ã€‚ãã®ãŸã‚ã€ä¾‹ãˆã°5秒毎ã«webサイトを確èªã™ã‚‹å¿…è¦ãŒã‚ã‚‹ãªã‚‰ã€å®Ÿéš›ã«webサイトã«pingを打ã¤ã‚³ãƒ¼ãƒ‰ã‚’å«ã‚€ãƒ–ロックã¨å…±ã«`every(300)`を呼ã³å‡ºã—ã¾ã™ã€‚
+
+=== flow(styles) { ... } » Shoes::Flow ===
+
+フロー(flow)ã¯Shoesã®è¦ç´ ã‚’é…ç½®ã§ãã‚‹ç›®ã«è¦‹ãˆãªã„箱(ã¾ãŸã¯"スロット")ã§ã™ã€‚フローã¨ã‚¹ã‚¿ãƒƒã‚¯ã®ã©ã¡ã‚‰ã‚‚メインã®[[Slots]]ã®ãƒšãƒ¼ã‚¸ã§ã¨ã¦ã‚‚詳細ã«èª¬æ˜Žã•ã‚Œã¾ã™ã€‚
+
+フローã¯è¦ç´ ã‚’æ°´å¹³ã«ã¾ã¨ã‚ã¾ã™ã€‚ã‚‚ã®ã‚’åž‚ç›´ã«ç©ã¿é‡ã­ãŸã¾ã¾ã«ã™ã‚‹ãŸã‚ã«[[Element.stack]]を利用ã™ã‚‹ã¨ã“ã‚ã§ã€ãƒ•ãƒ­ãƒ¼ã¯ãã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„をページã®ç«¯ã‹ã‚‰ç«¯ã«ã‚ãŸã£ã¦é…ç½®ã—ã¾ã™ã€‚ã„ã£ãŸã‚“ページã®æœ€å¾Œã«åˆ°é”ã—ãŸã‚‰ã€ãƒ•ãƒ­ãƒ¼ã¯è¦ç´ ã®æ–°ã—ã„行を開始ã—ã¾ã™ã€‚
+
+=== image(path) » Shoes::Image ===
+
+写真を表示ã™ã‚‹ãŸã‚ã«[[Image]]ã®è¦ç´ ã‚’作æˆã—ã¾ã™ã€‚
+PNGã€JPEGãã—ã¦GIFã®ãƒ•ã‚©ãƒ¼ãƒžãƒƒãƒˆãŒè¨±ã•ã‚Œã¾ã™ã€‚
+
+`path`ã¯ãƒ•ã‚¡ã‚¤ãƒ«ã®ãƒ‘スã¾ãŸã¯URLã§ã™ã€‚ã™ã¹ã¦ã®ç”»åƒã¯ãƒ¡ãƒ¢ãƒªã«ä¸€æ™‚çš„ã«ã‚­ãƒ£ãƒƒã‚·ãƒ¥ã•ã‚Œã€ãƒªãƒ¢ãƒ¼ãƒˆã®ç”»åƒã¯ãƒ­ãƒ¼ã‚«ãƒ«ã®ãƒ¦ãƒ¼ã‚¶ã®å€‹äººçš„ãªShoesディレクトリã«ã‚‚キャッシュã•ã‚Œã¾ã™ã€‚リモートã®ç”»åƒã¯ãƒãƒƒã‚¯ã‚°ãƒ©ã‚¦ãƒ³ãƒ‰ã§ãƒ­ãƒ¼ãƒ‰ã•ã‚Œã¾ã™ï¼›ãƒ–ラウザã¨åŒæ§˜ã«ã€ç”»åƒã¯ã™ãã«ã¯è¡¨ç¤ºã•ã‚Œã¾ã›ã‚“ãŒã€ãれらãŒãƒ­ãƒ¼ãƒ‰ã•ã‚ŒãŸã¨ãã«è¡¨ç¤ºã•ã‚Œã¾ã™ã€‚
+
+=== imagesize(path) » [width, height] ===
+
+ç”»åƒã®å¹…ã¨é«˜ã•ã‚’ç´ æ—©ã手ã«å…¥ã‚Œã¾ã™ã€‚
+ç”»åƒã¯ã‚­ãƒ£ãƒƒã‚·ãƒ¥ã«ãƒ­ãƒ¼ãƒ‰ã•ã‚Œãšè¡¨ç¤ºã‚‚ã•ã‚Œã¾ã›ã‚“。
+
+緊急ã®æ³¨æ„:ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ãƒªãƒ¢ãƒ¼ãƒˆã®ç”»åƒï¼ˆãƒãƒ¼ãƒ‰ãƒ‡ã‚£ã‚¹ã‚¯ãƒ‰ãƒ©ã‚¤ãƒ–ã‹ã‚‰ã§ã¯ãªãHTTPã«ã‚ˆã‚Šãƒ­ãƒ¼ãƒ‰ã•ã‚ŒãŸï¼‰ã«ã¯åˆ©ç”¨ã§ãã¾ã›ã‚“。
+
+=== ins(text) » Shoes::Ins ===
+
+一本ã®ä¸‹ç·šã®Shoesスタイルã§ã‚ã‚‹ã€Ins("inserted"ã®çœç•¥å½¢ï¼‰ãƒ†ã‚­ã‚¹ãƒˆã®ä¸€éƒ¨ã‚’作æˆã—ã¾ã™ã€‚
+
+=== inscription(text) » Shoes::Inscription ===
+
+é¡Œå(Inscription)ã®ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックを作æˆã—ã¾ã™ã€‚Shoesã¯10ピクセルã®å¤§ãã•ã«ã“ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’æ•´å½¢ã—ã¾ã™ã€‚
+
+=== link(text, :click => proc or string) » Shoes::Link ===
+
+一本ã®ä¸‹ç·šã‚’æŒã¡ç·šã®è‰²ã‚’#06E(é’色)ã«ShoesãŒæ•´å½¢ã—ãŸã€ãƒªãƒ³ã‚¯ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックを作æˆã—ã¾ã™ã€‚デフォルトã®ãƒªãƒ³ã‚¯ãƒ›ãƒãƒ¼ã‚¹ã‚¿ã‚¤ãƒ«ã‚‚一本ã®ä¸‹ç·šã‚’æŒã¡ç·šã®è‰²ã‚’#039(ダークブルー)ã«æ•´å½¢ã—ã¾ã™ã€‚
+
+=== list_box(:items => [strings, ...]) » Shoes::ListBox ===
+
+`items`ã®é…列ã®ã™ã¹ã¦ã®ã‚¨ãƒ³ãƒˆãƒªã‚’å«ã‚€ãƒ‰ãƒ­ãƒƒãƒ—ダウンリストボックスを追加ã—ã¾ã™ã€‚オプションã§ãƒ–ロックãŒå–り付ã‘ã‚‹ã“ã¨ãŒã§ãã€ã“ã‚Œã¯ãƒ¦ãƒ¼ã‚¶ãŒãƒœãƒƒã‚¯ã‚¹ã®é …目をé¸æŠžã—ãŸã‚‰å‘¼ã³å‡ºã•ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :margin => 10 do
+ para "Pick a card:"
+ list_box :items => ["Jack", "Ace", "Joker"]
+ end
+ end
+}}}
+
+é¸æŠžã•ã‚ŒãŸæ–‡å­—列をを得るãŸã‚ã«`ListBox#text`を呼ã³å‡ºã—ã¾ã™ã€‚より多ãã®ãƒ˜ãƒ«ãƒ—ã¯`リストボックス(ListBox)`ã®ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã®`ãƒã‚¤ãƒ†ã‚£ãƒ–`コントロールを見ã¦ãã ã•ã„。
+
+=== progress() » Shoes::Progress ===
+
+プログレスãƒãƒ¼ã‚’追加ã—ã¾ã™ã€‚
+
+=== para(text) » Shoes::Para ===
+
+ShoesãŒ12ピクセルã®å¤§ãã•ã«æ•´å½¢ã™ã‚‹ã€Para("paragraph"ã®çœç•¥å½¢ï¼‰ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックを作æˆã—ã¾ã™ã€‚
+
+=== radio(group name: a string or symbol) » Shoes::Radio ===
+
+ラジオボタンを追加ã—ã¾ã™ã€‚`グループå(group name)`ãŒä¸Žãˆã‚‰ã‚ŒãŸã‚‰ã€ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¯ã‚°ãƒ«ãƒ¼ãƒ—ã®ä¸€éƒ¨ã ã¨ã¿ãªã•ã‚Œã¾ã™ã€‚åŒã˜ã‚°ãƒ«ãƒ¼ãƒ—ã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã®ã†ã¡ã§ã€1ã¤ã ã‘をクリックã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚(もã—グループåãŒä¸Žãˆã‚‰ã‚Œãªã‘ã‚Œã°ã€ãã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¯åŒã˜ã‚¹ãƒ­ãƒƒãƒˆã®ä»–ã®ã™ã¹ã¦ã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¨ã‚°ãƒ«ãƒ¼ãƒ—化ã•ã‚Œã¾ã™ï¼‰
+
+=== span(text) » Shoes::Span ===
+
+デフォルトã§ã•ã‚Œãªã„ã€Spanテキストã®ä¸€éƒ¨ã‚’作æˆã—ã¾ã™ã€‚
+
+=== stack(styles) { ... } » Shoes::Stack ===
+
+æ–°ã—ã„スタックを作æˆã—ã¾ã™ã€‚スタックã¯ã‚¹ãƒ­ãƒƒãƒˆã®ä¸€ç¨®ã§ã™ã€‚(スタックã¨ãƒ•ãƒ­ãƒ¼ã®å®Œå…¨ãªèª¬æ˜Žã¯ãƒ¡ã‚¤ãƒ³ã®[[Slots]]ã®ãƒšãƒ¼ã‚¸ã‚’見ã¦ãã ã•ã„。)
+
+è¦ã™ã‚‹ã«ã€ã‚¹ã‚¿ãƒƒã‚¯ã¯è¦ç´ ã‚’é…ç½®ã™ã‚‹ãŸã‚ã®ç›®ã«è¦‹ãˆãªã„箱("スロット")ã§ã™ã€‚スタックã«ãƒœã‚¿ãƒ³ã‚„ç”»åƒãªã©ã‚’追加ã—ã¦ã€ãれらã¯åž‚ç›´ã«ç©ã¿ä¸Šã’られã¾ã™ã€‚ãã†ã€ãれらã¯é‡ãªã‚Šã¾ã™ã€‚
+
+=== strong(text) » Shoes::Strong ===
+
+デフォルトã§å¤ªå­—ã«æ•´å½¢ã•ã‚ŒãŸã€Strongテキストã®ä¸€éƒ¨ã‚’作æˆã—ã¾ã™ã€‚
+
+=== sub(text) » Shoes::Sub ===
+
+デフォルトã§ãƒ†ã‚­ã‚¹ãƒˆã¯10ピクセル(ä½ç½®ã‚’)下ã’られx-smallフォントã«æ•´å½¢ã•ã‚ŒãŸã€Sub("subscript"ã®çœç•¥å½¢ï¼‰ãƒ†ã‚­ã‚¹ãƒˆã®ä¸€éƒ¨ã‚’作æˆã—ã¾ã™ã€‚
+
+=== subtitle(text) » Shoes::Subtitle ===
+
+サブタイトル(Subtitle)テキストブロックを作æˆã—ã¾ã™ã€‚Shoesã¯26ピクセルã®å¤§ãã•ã«ã“ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’æ•´å½¢ã—ã¾ã™ã€‚
+
+=== sup(text) » Shoes::Sup ===
+
+デフォルトã§ãƒ†ã‚­ã‚¹ãƒˆã¯10ピクセル(ä½ç½®ã‚’)上ã’られx-smallフォントã«æ•´å½¢ã•ã‚ŒãŸã€Sup("superscript"ã®çœç•¥å½¢ï¼‰ãƒ†ã‚­ã‚¹ãƒˆã®ä¸€éƒ¨ã‚’作æˆã—ã¾ã™ã€‚
+
+=== tagline(text) » Shoes::Tagline ===
+
+タグライン(Tagline)テキストブロックを作æˆã—ã¾ã™ã€‚Shoesã¯ã“ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’18ピクセルã®å¤§ãã•ã«æ•´å½¢ã—ã¾ã™ã€‚
+
+=== timer(seconds) { ... } » Shoes::Timer ===
+
+1回ã ã‘ã®ã‚¿ã‚¤ãƒžãƒ¼ã§ã™ã€‚å°‘ã—ã®ç§’(ã¾ãŸã¯åˆ†ã€æ™‚)後ã«ã„ãらã‹ã®ã‚³ãƒ¼ãƒ‰ã®å®Ÿè¡Œã‚’スケジュールã—ãŸã„ãªã‚‰ã€ã“ã“ã«ãƒ–ロックã¨ã—ã¦ã‚³ãƒ¼ãƒ‰ã‚’å–り付ã‘ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+今ã‹ã‚‰5秒後ã«ã‚¢ãƒ©ãƒ¼ãƒˆãƒœãƒƒã‚¯ã‚¹ã‚’表示ã™ã‚‹ãŸã‚ã«ã¯ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ timer(5) do
+ alert("Your five seconds are up.")
+ end
+ end
+}}}
+
+=== title(text) » Shoes::Title ===
+
+タイトル(Title)テキストブロックを作æˆã—ã¾ã™ã€‚Shoesã¯ã“れらã®è¦ç´ ã‚’34ピクセルã®å¤§ãã•ã«æ•´å½¢ã—ã¾ã™ã€‚
+
+=== video(path or url) » Shoes::Video ===
+
+スロットã«å‹•ç”»ã‚’埋ã‚è¾¼ã¿ã¾ã™ã€‚
+
+=== window(styles) { ... } » Shoes::App ===
+
+æ–°ã—ã„アプリケーションウィンドウを開ãã¾ã™ã€‚ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯åˆã‚ã«ã‚¢ãƒ—リケーションを開始ã™ã‚‹ãŸã‚ã«ä½¿ã‚れる[[App.Shoes.app]]メソッドã¨ã»ã¨ã‚“ã©åŒä¸€ã§ã™ã€‚é•ã„ã¯`window`メソッドã¯æ–°ã—ã„ウィンドウã®[[App.owner]]プロパティを設定ã™ã‚‹ã“ã¨ã§ã™ã€‚(普通ã®Shoes.appã¯ãã®`owner`ã‚’`nil`ã«è¨­å®šã—ã¾ã™ã€‚)
+
+ãã®ãŸã‚ã€æ–°ã—ã„ウィンドウã®`owner`ã¯ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã‚’開始ã—ãŸShoes::Appã«è¨­å®šã•ã‚Œã‚‹ã§ã—ょã†ã€‚ã“ã®æ–¹æ³•ã«ã‚ˆã‚Šå­ã®ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ãŒè¦ªã‚’呼ã¹ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app :title => "The Owner" do
+ button "Pop up?" do
+ window do
+ para "Okay, popped up from #{owner}"
+ end
+ end
+ end
+}}}
+
+== Events ==
+
+ã©ã®ã‚ˆã†ã«ã—ã¦ãƒžã‚¦ã‚¹ã®ã‚¯ãƒªãƒƒã‚¯ã‚’離ã—ãŸã“ã¨ã‚„キーボードをタイプã—ãŸã“ã¨ãŒåˆ†ã‹ã‚‹ã‹ä¸æ€è­°ã«æ€ã„ã¾ã›ã‚“ã‹ï¼Ÿã‚¹ãƒ­ãƒƒãƒˆã®å†…部ã§ãƒžã‚¦ã‚¹ãŒå‹•ã„ãŸã¨ãã¯ã„ã¤ã§ã‚‚イベントãŒã‚¹ãƒ­ãƒƒãƒˆã«é€ã‚‰ã‚Œã¾ã™ã€‚ã¾ãŸã€ã‚­ãƒ¼ãŒæŠ¼ã•ã‚ŒãŸã¨ãã¯ã„ã¤ã§ã‚‚。スロットãŒä½œæˆã•ã‚ŒãŸã‚Šç ´å£Šã•ã‚ŒãŸã¨ãã§ã•ãˆã€‚ãã‚Œãžã‚Œã®ãれらã®ã‚¤ãƒ™ãƒ³ãƒˆã‚’ブロックをå–り付ã‘ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+マウスイベントã¯`motion`ã€`click`ã€`hover`ãã—ã¦`leave`ã‚’å«ã¿ã¾ã™ã€‚キーボードã®ã‚¿ã‚¤ãƒ”ングã¯`keypress`イベントã«ã‚ˆã£ã¦è¡¨ã•ã‚Œã¾ã™ã€‚ãã—ã¦`start`ã‚„`finish`イベントã¯ã‚­ãƒ£ãƒ³ãƒã‚¹ã‚’開始ã™ã‚‹ã¨ãや破棄ã•ã‚ŒãŸã¨ãを指ã—示ã—ã¾ã™ã€‚
+
+ã§ã¯ã€ãƒžã‚¦ã‚¹ã§ã‚¹ãƒ­ãƒƒãƒˆã®ä¸Šã‚’フロートã™ã‚‹ã¨ãã«èƒŒæ™¯ã‚’変更ã—ãŸã„ã¨ã—ã¾ã—ょã†ã€‚スロットã®å†…部ã«ãƒžã‚¦ã‚¹ãŒãã‚‹ã¨ãã«èƒŒæ™¯ã‚’変更ã™ã‚‹ãŸã‚ã«ã¯`hover`イベントを使ã„ã¾ã™ã€‚ãã—ã¦ã€ãƒžã‚¦ã‚¹ãŒãƒ•ãƒ­ãƒ¼ãƒˆã—ã¦é›¢ã‚Œã‚‹ã¨ãã«æˆ»ã™ã«ã¯`leave`を使ã„ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ s = stack :width => 200, :height => 200 do
+ background red
+ hover do
+ s.clear { background blue }
+ end
+ leave do
+ s.clear { background red }
+ end
+ end
+ end
+}}}
+
+=== click { |button, left, top| ... } » self ===
+
+マウスボタンãŒã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸã¨ãã«ã¯clickブロックãŒå‘¼ã°ã‚Œã¾ã™ã€‚`button`ã¯ãƒžã‚¦ã‚¹ãƒœã‚¿ãƒ³ã®ã©ã‚ŒãŒæŠ¼ã•ã‚ŒãŸã‹ã®æ•°ã§ã™ã€‚`left`ã‚„`top`ã¯ã©ã“ãŒã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸã‹ã®ãƒžã‚¦ã‚¹ã®åº§æ¨™ã§ã™ã€‚
+
+マウスã®ã‚¯ãƒªãƒƒã‚¯ã‚’離ã—ãŸçž¬é–“ã‚’ã¨ã‚‰ãˆã‚‹ã«ã¯ã€[[Events.release]]イベントを見ã¦ãã ã•ã„。
+
+=== finish { |self| ... } » self ===
+
+スロットãŒå–り除ã‹ã‚ŒãŸã¨ãã¯ã€finishイベントãŒç™ºç”Ÿã—ã¾ã™ã€‚finishブロックã¯ã™ãã«`self`を手渡ã—ã€ã‚¹ãƒ­ãƒƒãƒˆã‚ªãƒ–ジェクトã¯å–り除ã‹ã‚Œã¾ã™ã€‚
+
+=== hover { |self| ... } » self ===
+
+hoverイベントã¯ã‚¹ãƒ­ãƒƒãƒˆã«ãƒžã‚¦ã‚¹ãŒå…¥ã£ãŸã¨ãã«ç™ºç”Ÿã—ã¾ã™ã€‚
+ã“ã®ãƒ–ロックã¯`self`を手ã«å…¥ã‚Œã€ã©ã®ã‚ªãƒ–ジェクトã®ä¸Šã‚’通ã£ãŸã‹ã‚’æ„味ã—ã¾ã™ã€‚
+
+スロットã‹ã‚‰ãƒžã‚¦ã‚¹ãŒå‡ºã¦ã„ãã“ã¨ã‚’ã¨ã‚‰ãˆã‚‹ã«ã¯ã€[[Events.leave]]イベントを確èªã—ã¦ãã ã•ã„。
+
+=== keypress { |key| ... } » self ===
+
+キー(ã¾ãŸã¯ã‚­ãƒ¼ã®çµ„ã¿åˆã‚ã›ï¼‰ãŒã„ã¤æŠ¼ã•ã‚Œã¦ã‚‚ã€ãã®ãƒ–ロックã¯å‘¼ã°ã‚Œã¾ã™ã€‚ãã®ãƒ–ロックã¯ã‚­ãƒ¼ã®æ€§è³ªã‚’表ã™æ–‡å­—列ã§ã‚ã‚‹`key`ã‚’é€ã‚Šã¾ã™ã€‚特別ãªã‚­ãƒ¼ã‚„キーã®çµ„ã¿åˆã‚ã›ã®å ´åˆã¯ã€æ–‡å­—列ã§ã¯ãªãRubyã®ã‚·ãƒ³ãƒœãƒ«ãŒé€ã‚‰ã‚Œã¾ã™ã€‚
+
+ãã—ã¦ã€ä¾‹ãˆã°ã€`Shift-a`ãŒæŠ¼ã•ã‚ŒãŸãªã‚‰ã€ãã®ãƒ–ロックã¯`"A"`ã®æ–‡å­—列を得ã¾ã™ã€‚
+
+ã—ã‹ã—ãªãŒã‚‰ã€F1ãŒæŠ¼ã•ã‚ŒãŸãªã‚‰ã€ `:f1`ã®ã‚·ãƒ³ãƒœãƒ«ã‚’å—ã‘ã¨ã‚Šã¾ã™ã€‚`Shift-F1`ãªã‚‰ã€ãã®ã‚·ãƒ³ãƒœãƒ«ã¯`:shift_f1`ã§ã™ã€‚
+
+`control`ã€`shift`ãã—ã¦`alt`ã¯ä¿®é£¾ã‚­ãƒ¼ã§ã™ã€‚ãれらã¯ä»¥ä¸‹ã®é †ç•ªã§ç¾ã‚Œã¾ã™ã€‚`Shift-Control-Alt-PgUp`ãŒæŠ¼ã•ã‚ŒãŸãªã‚‰ã€ãã®ã‚·ãƒ³ãƒœãƒ«ã¯`:control_shift_alt_page_up`ã«ãªã‚Šã¾ã™ã€‚
+
+シフトキーã«ã¤ã„ã¦ï¼‘ã¤ã€‚多ãã®ã‚­ãƒ¼ã§ã‚·ãƒ•ãƒˆã‚­ãƒ¼ã‚’見ãªã„ã§ã—ょã†ã€‚USキーボードã§ã¯ã€`Shift-7`ã¯ã‚¢ãƒ³ãƒ‘サンド(&)ã§ã™ã€‚ãã®ãŸã‚ã€`:shift_5`ã§ã¯ãªã`"&"`ã‚’å¾—ã¾ã™ã€‚ãã—ã¦ã€ãã®ã‚ˆã†ãªã‚­ãƒ¼ãƒœãƒ¼ãƒ‰ã§`Shift-Alt-7`を押ã—ãŸã‚‰ã€`:alt_&`ã®ã‚·ãƒ³ãƒœãƒ«ã‚’å¾—ã¾ã™ã€‚ã„ãã¤ã‹ä¸‹ã®ãƒ‘ラグラフã§ç‰¹åˆ¥ãªã‚­ãƒ¼ã®ã‚·ãƒ•ãƒˆä¿®é£¾å­ã®ä¸€è¦§ãŒè¦‹ãˆã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @info = para "NO KEY is PRESSED."
+ keypress do |k|
+ @info.replace "#{k.inspect} was PRESSED."
+ end
+ end
+}}}
+
+ShoesãŒãれ自身ã®ã„ãã¤ã‹ã®ãƒ›ãƒƒãƒˆã‚­ãƒ¼ã‚’利用ã™ã‚‹ã“ã¨ã‚’覚ãˆã¦ãŠã„ã¦ãã ã•ã„。Alt-ピリオド(`:alt_.`)ã€Alt-クエッション(`:alt_?`)ãã—ã¦Alt-スラッシュ(`:alt_/`)ã¯Shoesã®äºˆç´„語ã§ã™ã€‚
+
+以下ã¯ã‚¹ãƒšã‚·ãƒ£ãƒ«ã‚­ãƒ¼ã®ä¸€è¦§ã§ã™: `:escape`ã€`:delete`ã€ã€€
+`:backspace`ã€`:tab`ã€`:page_up`ã€`:page_down`ã€`:home`ã€`:end`ã€`:left`ã€`:up`ã€ã€€
+`:right`ã€`:down`ã€`:f1`ã€`:f2`ã€`:f3`ã€`:f4`ã€`:f5`ã€`:f6`ã€`:f7`ã€`:f8`ã€`:f9`ã€ã€€
+`:f10`ã€`:f11`ãã—ã¦`:f12`。
+
+ãれらã™ã¹ã¦ã®ãƒ«ãƒ¼ãƒ«ã«é–¢ã™ã‚‹ä¸€ã¤ã®è­¦å‘Šï¼šé€šå¸¸ã¯ãƒªã‚¿ãƒ¼ãƒ³ã‚­ãƒ¼ã¯`"\n"`を与ãˆã¾ã™ã€‚ã—ã‹ã—ãªãŒã‚‰ã€ä¿®é£¾ã‚­ãƒ¼ãŒæŠ¼ã•ã‚ŒãŸã¨ãã«ã¯ã€æœ€å¾Œã«ã¯`:control_enter`ã€`:control_alt_enter`ã€`:shift_alt_enter`ã«ãªã‚Šã¾ã™ã€‚
+
+=== leave { |self| ... } » self ===
+
+スロットã‹ã‚‰ãƒžã‚¦ã‚¹ã‚«ãƒ¼ã‚½ãƒ«ãŒå‡ºã¦è¡Œãã¨ãleaveイベントãŒç™ºç”Ÿã—ã¾ã™ã€‚ãã®çž¬é–“ã™ã§ã«ãƒžã‚¦ã‚¹ã‚«ãƒ¼ã‚½ãƒ«ã¯ã‚¹ãƒ­ãƒƒãƒˆã®ç«¯ã®ä¸­ã«ã¯ã‚ã‚Šã¾ã›ã‚“。leaveイベントãŒç™ºç”Ÿã™ã‚‹ã¨ãã€`self`ã¨ã¨ã‚‚ã«ãƒ–ロックãŒå‘¼ã°ã‚Œã€ãã®ã‚¹ãƒ­ãƒƒãƒˆã‚ªãƒ–ジェクトã¯å–り残ã•ã‚Œã¾ã™ã€‚
+
+スロットã«ãƒžã‚¦ã‚¹ãŒå…¥ã‚‹ã“ã¨ã‚’見ã¤ã‘ãŸã„ãªã‚‰[[Events.hover]]も見ã¦ãã ã•ã„。
+
+=== motion { |left, top| ... } » self ===
+
+マウスãŒã‚¹ãƒ­ãƒƒãƒˆã®å†…部を移動ã™ã‚‹ãŸã³ã«ãƒ¢ãƒ¼ã‚·ãƒ§ãƒ³ã®ãƒ–ロックã¯å‘¼ã°ã‚Œã¾ã™ã€‚ブロックã¯ã‚«ãƒ¼ã‚½ãƒ«ã®`left`ã‚„`top`ã®åº§æ¨™ã‚’渡ã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app :width => 200, :height => 200 do
+ background black
+ fill white
+ @circ = oval 0, 0, 100, 100
+
+ motion do |top, left|
+ @circ.move top - 50, left - 50
+ end
+ end
+}}}
+
+=== release { |button, left, top| ... } » self ===
+
+マウスãŒã‚¢ãƒ³ã‚¯ãƒªãƒƒã‚¯ï¼ˆãƒžã‚¦ã‚¹ã‚¢ãƒƒãƒ—)ã®ã¨ãã«releaseã®ãƒ–ロックã¯å®Ÿè¡Œã•ã‚Œã¾ã™ã€‚ãã‚Œã¯æŒ‡ãŒæŒã¡ä¸Šã’られãŸã¨ãã§ã™ã€‚`button`ã¯æŠ¼ã—下ã’られãŸãƒœã‚¿ãƒ³ã«å¯¾å¿œã™ã‚‹æ•°ã§ã™ã€‚`left`ã‚„`top`ã¯ãƒœã‚¿ãƒ³ãŒé›¢ã•ã‚ŒãŸã¨ãã®ãƒžã‚¦ã‚¹ã®ã®åº§æ¨™ã§ã™ã€‚
+
+実際ã®ãƒžã‚¦ã‚¹ã‚¯ãƒªãƒƒã‚¯ã‚’æ•ã¾ãˆã‚‹ã«ã¯ã€[[Events.click]]イベントを利用ã—ã¦ãã ã•ã„。
+
+=== start { |self| ... } » self ===
+
+åˆã‚ã¦ã‚¹ãƒ­ãƒƒãƒˆãŒæã‹ã‚Œã‚‹ã¨ãã€ã‚¹ã‚¿ãƒ¼ãƒˆï¼ˆstart)イベントãŒå®Ÿè¡Œã•ã‚Œã¾ã™ã€‚ã¾ã•ã«ä»Šæã‹ã‚ŒãŸã‚¹ãƒ­ãƒƒãƒˆã‚ªãƒ–ジェクトãŒ`self`ã¨ã—ã¦ãƒ–ロックã«æ¸¡ã•ã‚Œã¾ã™ã€‚
+
+== Manipulation Blocks ==
+
+以下ã®manipulationメソッドã¯ã‚¹ãƒ­ãƒƒãƒˆã®å‘¨å›²ã‚’変更ã—ãŸã‚Šæ–°ã—ã„è¦ç´ ã‚’挿入ã™ã‚‹ã“ã¨ã‚’手早ã片付ã‘ã¾ã™ã€‚
+
+=== append() { ... } » self ===
+
+スロットã®æœ€å¾Œã«è¦ç´ ã‚’追加ã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @slot = stack { para 'Good Morning' }
+ timer 3 do
+ @slot.append do
+ title "Breaking News"
+ tagline "Astronauts arrested for space shuttle DUI."
+ end
+ end
+ end
+}}}
+
+`title`ã‚„`tagline`ã®è¦ç´ ã‚’`@slot`ã®æœ€å¾Œã«è¿½åŠ ã—ã¾ã™ã€‚
+
+=== after(element) { ... } » self ===
+
+スロットã®å­ã¨ã—ã¦`element`ã®ã™ã後ã«ã€ã‚¹ãƒ­ãƒƒãƒˆã®æŒ‡å®šã—ãŸç®‡æ‰€ã«è¦ç´ ã‚’追加ã—ã¾ã™ã€‚
+
+=== before(element) { ... } » self ===
+
+スロットã®å­ã¨ã—ã¦`element`ã®ã™ãå‰ã«ã€ã‚¹ãƒ­ãƒƒãƒˆã®æŒ‡å®šã—ãŸç®‡æ‰€ã«è¦ç´ ã‚’追加ã—ã¾ã™ã€‚
+
+=== clear() » self ===
+
+タイマーやãƒã‚¹ãƒˆã—ãŸã‚¹ãƒ­ãƒƒãƒˆãªã©ã€ã‚¹ãƒ­ãƒƒãƒˆã®ã™ã¹ã¦ã®è¦ç´ ã‚’空ã«ã—ã¾ã™ã€‚スロットã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„を最åˆã‹ã‚‰æœ€å¾Œã¾ã§ãƒ«ãƒ¼ãƒ—ã—ã¦ãã‚Œãžã‚Œã®è¦ç´ ã®`remove`メソッドを呼ã¶ã“ã¨ã¨åŠ¹æžœã¨ã—ã¦ã¯åŒä¸€ã§ã™ã€‚
+
+=== clear() { ... } » self ===
+
+clearメソッドã¯ã‚ªãƒ—ションã§ãƒ–ロックもå–ã‚Šã¾ã™ã€‚ã“ã®ãƒ–ロックã¯ã‚¹ãƒ­ãƒƒãƒˆã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„ã‚’ç½®ãæ›ãˆã‚‹ãŸã‚ã«åˆ©ç”¨ã•ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @slot = stack { para "Old text" }
+ timer 3 do
+ @slot.clear { para "Brand new text" }
+ end
+ end
+}}}
+
+ã“ã®ä¾‹ã§ã¯ã€"Old text"パラグラフã¯æŽ’除ã•ã‚Œã€"Brand new text"ã«ã‚ˆã£ã¦ç½®ãæ›ãˆã‚‰ã‚Œã¾ã™ã€‚
+
+=== prepend() { ... } » self ===
+
+スロットã®åˆã‚ã«è¦ç´ ã‚’追加ã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @slot = stack { para 'Good Morning' }
+ timer 3 do
+ @slot.prepend { para "Your car is ready." }
+ end
+ end
+}}}
+
+`@slot`ã®åˆã‚ã«`para`è¦ç´ ã‚’追加ã—ã¾ã™ã€‚
+
+== Position of a Slot ==
+
+ä»–ã®ã™ã¹ã¦ã®è¦ç´ ã¨åŒæ§˜ã«ã€ã‚¹ãƒ­ãƒƒãƒˆã¯ä½œæˆã•ã‚ŒãŸã¨ãã«æ•´å½¢ã™ã‚‹ã“ã¨ã‚„カスタマイズã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+スタックã®å¹…ã‚’150ピクセルã«è¨­å®šã™ã‚‹ãŸã‚ã«ã¯ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack(:width => 150) { para "Now that's precision." }
+ end
+}}}
+
+ãã‚Œãžã‚Œã®ã‚¹ã‚¿ã‚¤ãƒ«ã®è¨­å®šã¯ã€è©³ç´°ãªè¨­å®šã‚’手ã«å…¥ã‚Œã‚‹ãŸã‚ã«åˆ©ç”¨ã™ã‚‹ã“ã¨ã®ã§ãるメソッドもæŒã£ã¦ã„ã¾ã™ã€‚(ãã®ãŸã‚ã€ä¾‹ãˆã°ã€`width`メソッドã¯ã‚¹ãƒ­ãƒƒãƒˆã®å¹…をピクセルã§è¿”ã—ã¾ã™ã€‚)
+
+=== displace(left: a number, top: a number) » self ===
+
+:displace_leftã¨:displace_topスタイルã®è¨­å®šã®ãŸã‚ã®ã‚·ãƒ§ãƒ¼ãƒˆã‚«ãƒƒãƒˆãƒ¡ã‚½ãƒƒãƒ‰ã§ã™ã€‚ç½®ãæ›ãˆã¯ãƒ¬ã‚¤ã‚¢ã‚¦ãƒˆã‚’変更ã—ãªã„ã§ã‚¹ãƒ­ãƒƒãƒˆã‚’移動ã™ã‚‹ä¾¿åˆ©ãªæ–¹æ³•ã§ã™ã€‚実際ã«ã¯ã€`top`ã¨`left`メソッドã¯ç½®ãæ›ãˆã‚’å…¨ã報告ã—ã¾ã›ã‚“。ãã®ãŸã‚ã€é€šå¸¸ã€ç½®ãæ›ãˆã¯ä¸€æ™‚çš„ãªã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã®ãŸã‚ã«ã‚ã‚Šã¾ã™ã€‚例ãˆã°ã€é©å½“ãªä½ç½®ã«ãƒœã‚¿ãƒ³ã‚’å°‘ã—移動ã™ã‚‹ãªã©ã€‚
+
+`left`ã¨`top`ã®æ•°ã¯`displace`ã«é€ã‚‰ã‚Œã€ã‚¹ãƒ­ãƒƒãƒˆè‡ªèº«ã®topã¨leftã®åº§æ¨™ã«è¿½åŠ ã•ã‚Œã¾ã™ã€‚topã¨leftã®åº§æ¨™ã‹ã‚‰å·®ã—引ãã«ã¯ã€è² æ•°ã‚’利用ã—ã¦ãã ã•ã„。
+
+=== gutter() » a number ===
+
+スクロールãƒãƒ¼ã‚¨ãƒªã‚¢ã®å¤§ãã•ã§ã™ã€‚ShoesãŒã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ãƒãƒ¼ã‚’表示ã™ã‚‹å¿…è¦ãŒã‚ã‚‹ã¨ãã€ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ãƒãƒ¼ãŒã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã®ç«¯ã«ãµã‚Œã¦ã„ã‚‹ã„ãã¤ã‹ã®è¦ç´ ã‚’éš ã—ã¦ã—ã¾ã†ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。`gutter`ã¯ã©ã®ãらã„ã®ãƒ”クセルをスクロールãƒãƒ¼ãŒéš ã™ã“ã¨ã‚’期待ã™ã‚‹ã‹ã‚’æ•™ãˆã¾ã™ã€‚
+
+ã“ã‚Œã¯ä¸€èˆ¬çš„ã«ã¯ã€æ¬¡ã®ã‚ˆã†ã«å³å´ã«è©°ã‚物ã®è¦ç´ ã¨ã—ã¦åˆ©ç”¨ã—ã¾ã™ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :margin_right => 20 + gutter do
+ para "Insert fat and ratified declaration of independence here..."
+ end
+ end
+}}}
+
+=== height() » a number ===
+
+スロットã®ç›®ã«è¦‹ãˆã‚‹ãƒ”クセルã§ã®åž‚ç›´ã®å¤§ãã•ã§ã™ã€‚ãã—ã¦ã€ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ã™ã‚‹ã‚¹ãƒ­ãƒƒãƒˆã®å ´åˆã¯ã€ã‚¹ãƒ­ãƒƒãƒˆã®å…¨ä½“ã®å¤§ãã•ã‚’å¾—ã‚‹ãŸã‚ã«`scroll_height()`ã®åˆ©ç”¨ã‚’å¿…è¦ã¨ã™ã‚‹ã§ã—ょã†ã€‚
+
+=== hide() » self ===
+
+見ãˆãªãã™ã‚‹ãŸã‚ã«ã€ã‚¹ãƒ­ãƒƒãƒˆã‚’éš ã—ã¾ã™ã€‚[[Position.show]]ã¨[[Position.toggle]]も見ã¦ãã ã•ã„。
+
+=== left() » a number ===
+
+The left pixel location of the slot. Also known as the x-axis coordinate.
+スロットã®ä½ç½®ã®å·¦ã®ãƒ”クセルã§ã™ã€‚x-axis座標ã§ã‚‚知るã“ã¨ãŒã§ãã¾ã™ã€‚
+
+=== move(left, top) » self ===
+
+スロットã®å·¦ä¸Šã®è§’ã§ã‚る(leftã€top)ã®åº§æ¨™ã‚’指定ã—ã¦ã‚¹ãƒ­ãƒƒãƒˆã‚’移動ã—ã¾ã™ã€‚
+
+=== remove() » self ===
+
+スロットを削除ã—ã¾ã™ã€‚ãã‚Œã¯è¡¨ç¤ºã•ã‚Œãªããªã‚Šè¦ªã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„ã«è¨˜è¼‰ã•ã‚Œãªããªã‚Šã¾ã™ã€‚ãã‚Œã¯æ¶ˆãˆåŽ»ã‚Šã¾ã™ã€‚
+
+=== scroll() » true or false ===
+
+スロットã«ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ãƒãƒ¼ã‚’表示ã™ã‚‹ã“ã¨ã‚’許ã—ã¾ã™ã‹ï¼Ÿtrueã‹falseã§ã™ã€‚スロットã®é«˜ã•ã‚‚固定ã•ã‚Œã¦ã„ã‚‹ã¨ãã ã‘スクロールãƒãƒ¼ãŒè¡¨ç¤ºã•ã‚Œã¾ã™ã€‚
+
+=== scroll_height() » a number ===
+
+スクロールã«ã‚ˆã£ã¦éš ã•ã‚Œã¦ã„ã‚‹ã™ã¹ã¦ã‚’å«ã‚€ã€ã‚¹ãƒ­ãƒƒãƒˆå…¨ä½“ã®åž‚ç›´ã®å¤§ãã•ã§ã™ã€‚
+
+=== scroll_max() » a number ===
+
+ã“ã®ã‚¹ãƒ­ãƒƒãƒˆã§ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ãƒ€ã‚¦ãƒ³ã§ãる上å´ï¼ˆtop)ã®åº§æ¨™ã§ã™ã€‚スクロールãƒãƒ¼ã®ä¸Šå´ï¼ˆtop)ã®åº§æ¨™ã¯ã„ã¤ã‚‚ゼロã§ã™ã€‚下å´ï¼ˆbottom)ã®åº§æ¨™ã¯ã‚¹ãƒ­ãƒƒãƒˆå…¨ä½“ã®é«˜ã•ã‹ã‚‰1ページã®ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«åˆ†ã‚’引ã„ãŸã‚‚ã®ã§ã™ã€‚ã“ã®ä¸‹å´ï¼ˆbottom)ã®åº§æ¨™ã¯`scroll_max`ãŒè¿”ã™å€¤ã§ã™ã€‚
+
+ã“ã‚Œã¯åŸºæœ¬çš„ã«`slot.scroll_height - slot.height`ã¨æ›¸ããŸã‚ã®ã‚·ãƒ§ãƒ¼ãƒˆã‚«ãƒƒãƒˆã§ã™ã€‚
+
+スロットã®ä¸‹å´ã«ã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ã™ã‚‹ãŸã‚ã«ã¯ã€`slot.scroll_top = slot.scroll_max`を利用ã—ã¾ã™ã€‚
+
+=== scroll_top() » a number ===
+
+スロットãŒã‚¹ã‚¯ãƒ­ãƒ¼ãƒ«ãƒ€ã‚¦ãƒ³ã™ã‚‹ä¸Šå´ï¼ˆtop)ã®åº§æ¨™ã§ã™ã€‚ãã®ãŸã‚ã€ã‚¹ãƒ­ãƒƒãƒˆãŒ20ピクセルスクロールダウンã•ã‚ŒãŸã‚‰ã€ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯`20`ã‚’è¿”ã—ã¾ã™ã€‚
+
+=== scroll_top = a number ===
+
+ä»»æ„ã®åº§æ¨™ã«ã‚¹ãƒ­ãƒƒãƒˆã‚’スクロールã—ã¾ã™ã€‚ゼロã‹ã‚‰`scroll_max`ã¾ã§ã®é–“ã§ã‚ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ã€‚
+
+=== show() » self ===
+
+スロットãŒéš ã•ã‚Œã¦ã„ãŸå ´åˆã€è¡¨ç¤ºã—ã¾ã™ã€‚[[Position.hide]]ã¨[[Position.toggle]]も見ã¦ãã ã•ã„。
+
+=== style() » styles ===
+
+引数ãªã—ã§`style`メソッドを呼ã¶ã“ã¨ã§ã‚¹ãƒ­ãƒƒãƒˆã«ç¾åœ¨é©ç”¨ã•ã‚Œã¦ã„るスタイルã®ãƒãƒƒã‚·ãƒ¥ã‚’è¿”ã—ã¾ã™ã€‚
+
+`height`ã¨`width`ãªã©ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ã‚¹ãƒ­ãƒƒãƒˆã®æœ¬å½“ã®ã‚µã‚¤ã‚ºã‚’ピクセルã§è¿”ã—ã¾ã™ãŒã€`style[:height]`ã¾ãŸã¯`style[:width]`を利用ã™ã‚‹ã“ã¨ã§åˆã‚ã«è¦æ±‚ã•ã‚ŒãŸã‚µã‚¤ã‚ºã‚’å¾—ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @s = stack :width => "100%"
+ para @s.style[:width]
+ end
+}}}
+
+ã“ã®ä¾‹ã§ã¯ã€ã“ã®ã‚¹ã‚¿ãƒƒã‚¯ã®ä¸‹ã®ãƒ‘ラグラフã¯"100%"ã®æ–‡å­—列を表示ã—ã¾ã™ã€‚
+
+=== style(styles) » styles ===
+
+ãƒãƒƒã‚·ãƒ¥ã®ã‚¹ã‚¿ã‚¤ãƒ«è¨­å®šã‚’使ã£ã¦ã‚¹ãƒ­ãƒƒãƒˆã‚’修正ã—ã¦ãã ã•ã„。ã“ã®ãƒšãƒ¼ã‚¸ã®ã©ã‚“ãªãƒ¡ã‚½ãƒƒãƒ‰ã§ã‚‚(もã¡ã‚ã‚“ã€ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯é™¤ã„ã¦ï¼‰ã‚¹ã‚¿ã‚¤ãƒ«ã®è¨­å®šã«åˆ©ç”¨ã§ãã¾ã™ã€‚ãã—ã¦ã€ä¾‹ãˆã°ã€`width`メソッドãŒã‚ã‚Šã€ã“ã®ã‚ˆã†ã«`width`スタイルもã‚ã‚Šã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @s = stack { background green }
+ @s.style(:width => 400, :height => 200)
+ end
+}}}
+
+=== toggle() » self ===
+
+スロットãŒè¡¨ç¤ºã•ã‚Œã¦ã„ã‚‹ãªã‚‰éžè¡¨ç¤ºã«ã—ã¾ã™ã€‚ã¾ãŸã€ã‚¹ãƒ­ãƒƒãƒˆãŒéžè¡¨ç¤ºãªã‚‰è¡¨ç¤ºã—ã¾ã™ã€‚
+
+=== top() » a number ===
+
+スロットã®ä¸Šï¼ˆtop)ã®ä½ç½®ã§ã™ã€‚y軸ã®åº§æ¨™ã¨ã—ã¦ã‚‚知られã¦ã„ã¾ã™ã€‚
+
+=== width() » a number ===
+
+スロットã®æ°´å¹³ã®ãƒ”クセルサイズã§ã™ã€‚
+
+== Traversing the Page ==
+
+スロット内部ã®è¦ç´ ã‚’最åˆã‹ã‚‰æœ€å¾Œã¾ã§ãƒ«ãƒ¼ãƒ—ã™ã‚‹å¿…è¦æ€§ã«æ°—ã¥ãã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。ã¾ãŸã¯ã€ãƒšãƒ¼ã‚¸ã‚’ç™»ã£ã¦è¦ç´ ã®è¦ªã®ã‚¹ã‚¿ãƒƒã‚¯ã‚’探ã™å¿…è¦ãŒã‚ã‚‹ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“。
+
+ã™ã¹ã¦ã®è¦ç´ ã§ã€ä¸Šä½ã®ã‚¹ãƒ­ãƒƒãƒˆç›´æŽ¥å¾—ã‚‹ãŸã‚ã«`parent`メソッドを呼ã¶ã“ã¨ãŒã§ãã¾ã™ã€‚ãã—ã¦ã‚¹ãƒ­ãƒƒãƒˆã§ã¯ã€ã™ã¹ã¦ã®å­ã‚’å¾—ã‚‹ãŸã‚ã«`contents`メソッドを呼ã¶ã“ã¨ãŒã§ãã¾ã™ã€‚(テキストブロックãªã©ã®ã„ãã¤ã‹ã®è¦ç´ ã¯ã€ãれらã®å­ã‚’å¾—ã‚‹ãŸã‚ã®`contents`メソッドもæŒã£ã¦ã„ã¾ã™ã€‚)
+
+=== contents() » an array of elements ===
+
+スロットã®ã™ã¹ã¦ã®è¦ç´ ã‚’一覧ã«ã—ã¾ã™ã€‚
+
+=== parent() » a Shoes::Stack or Shoes::Flow ===
+
+è¦ç´ ã®ã‚³ãƒ³ãƒ†ãƒŠã®ã‚ªãƒ–ジェクトを得ã¾ã™ã€‚
+
+= Elements =
+
+ã“ã‚Œã¯Shoesã®è¦ç´ ã§ã™ã€‚è¦ç´ ã¯æ¥•å††ã®å½¢çŠ¶ã¨åŒã˜ãらã„å˜ç´”ã§ã™ã€‚ã¾ãŸã¯ãƒ“デオストリームã¨åŒã˜ãらã„複雑ã§ã™ã€‚ã‚ãªãŸã¯ä»¥å‰ã“ã®ãƒžãƒ‹ãƒ¥ã‚¢ãƒ«ã®ã‚¹ãƒ­ãƒƒãƒˆã®ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã§ã“れらã™ã¹ã¦ã®è¦ç´ ã«å‡ºä¼šã£ãŸã“ã¨ãŒã‚ã‚Šã¾ã™ã€‚
+
+Shoesã¯7ã¤ã®ãƒã‚¤ãƒ†ã‚£ãƒ–コントロールをæŒã¡ã¾ã™ï¼šãƒœã‚¿ãƒ³ï¼ˆButton)ã€ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒ©ã‚¤ãƒ³ï¼ˆEditLine)ã€ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒœãƒƒã‚¯ã‚¹ï¼ˆEditBox)ã€ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ï¼ˆListBox)ã€ãƒ—ログレスメータ(Progress meter)ã€ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ï¼ˆCheck box)ã€ãƒ©ã‚¸ã‚ªï¼ˆRadio)。ç§ãŸã¡ã®è¨€ã†"native"コントロールã¨ã¯ã€ãれらã®7ã¤ã®è¦ç´ ãŒã‚ªãƒšãƒ¬ãƒ¼ãƒ†ã‚£ãƒ³ã‚°ã‚·ã‚¹ãƒ†ãƒ ã«ã‚ˆã£ã¦æç”»ã•ã‚Œã‚‹ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚ãã®ãŸã‚ã€ãƒ—ログレスãƒãƒ¼ã®Windowsã§ã®è¦‹ãˆæ–¹ã¨OS Xã§ã®è¦‹ãˆæ–¹ã¯é•ã„ã¾ã™ã€‚
+
+ã¾ãŸShoesã¯7ã¤ã®ä»–ã®ç¨®é¡žã®è¦ç´ ã‚’æŒã£ã¦ã„ã¾ã™ï¼šèƒŒæ™¯ï¼ˆBackground)ã€ãƒœãƒ¼ãƒ€ãƒ¼ï¼ˆBorder)ã€ç”»åƒï¼ˆImage)ã€å½¢çŠ¶ï¼ˆShape)ã€ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロック(TextBlock)ã€ã‚¿ã‚¤ãƒžãƒ¼ï¼ˆTimer)ã€ãã—ã¦ãƒ“デオ(Video)。ãれらã™ã¹ã¦ã¯ã©ã‚“ãªã‚ªãƒšãƒ¬ãƒ¼ãƒ†ã‚£ãƒ³ã‚°ã‚·ã‚¹ãƒ†ãƒ ã§ã‚‚åŒã˜ã‚ˆã†ãªè¦‹ãˆæ–¹ã¨å‹•ãã«ãªã‚‹ã¹ãã§ã™ã€‚
+
+ã„ã£ãŸã‚“è¦ç´ ã‚’生æˆã—ãŸå¾Œã§ã‚‚ã€ãれを変更ã—ãŸããªã‚‹ã§ã—ょã†ã€‚ãれを動ã‹ã—ãŸã‚Šéš ã—ãŸã‚Šã¾ãŸã¯ãれをå–り除ããŸã‚ã«ã€‚ãれらã®ç¨®é¡žã®ã“ã¨ã‚’è¡Œã†ãŸã‚ã«ã€ã“ã®ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã®ã‚³ãƒžãƒ³ãƒ‰åˆ©ç”¨ã™ã‚‹ã§ã—ょã†ã€‚(特ã«ã©ã‚“ãªè¦ç´ ä¸Šã§ã‚‚利用ã§ãるコマンドã®[[Common Common Methods]]セクションを確èªã—ã¦ãã ã•ã„。)
+
+ãã—ã¦ã€ä¾‹ã¨ã—ã¦ã€PNGをスクリーンをé…ç½®ã™ã‚‹ãŸã‚ã«ã‚¹ãƒ­ãƒƒãƒˆã®`image`メソッドを使ã£ã¦ãã ã•ã„。ã“ã®`image`メソッドã¯ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚ªãƒ–ジェクトを返ã—ã¾ã™ã€‚ã“れらをã™ã£ã‹ã‚Šå¤‰æ›´ã™ã‚‹ãŸã‚ã«ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚ªãƒ–ジェクトã®ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã‚’使ã£ã¦ã¿ã¦ãã ã•ã„。
+
+== Common Methods ==
+
+Shoesã§ã¯å°‘ã—ã®ãƒ¡ã‚½ãƒƒãƒ‰ãŒã™ã¹ã¦ã®å°ã•ãªè¦ç´ ã«ã‚ˆã£ã¦å…±æœ‰ã•ã‚Œã¦ã¾ã™ã€‚移動ã€è¡¨ç¤ºã€éžè¡¨ç¤ºã€‚è¦ç´ ã®å‰Šé™¤ã€‚基本的ã§ã¨ã¦ã‚‚一般的ãªã“ã¨ã§ã™ã€‚ã“ã®ä¸€è¦§ã¯ãれらã®ä¸€èˆ¬çš„ãªã‚³ãƒžãƒ³ãƒ‰ã‚’å«ã‚“ã§ã„ã¾ã™ã€‚
+
+ã™ã¹ã¦ã®ãƒ¡ã‚½ãƒƒãƒ‰ã®ä¸­ã§ã‚‚ã£ã¨ã‚‚一般的ãªãƒ¡ã‚½ãƒƒãƒ‰ã®ä¸€ã¤ã¯`style`ã§ã™ã€‚(ã“ã‚Œã¯ã‚¹ãƒ­ãƒƒãƒˆã®[[Position.style]]メソッドã¨ã—ã¦ã‚‚ã‚«ãƒãƒ¼ã•ã‚Œã¾ã™ã€‚)
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ # Background, text and a button: both are elements!
+ @back = background green
+ @text = banner "A Message for You, Rudy"
+ @press = button "Stop your messin about!"
+
+ # And so, both can be styled.
+ @text.style :size => 12, :stroke => red, :margin => 10
+ @press.style :width => 400
+ @back.style :height => 10
+ end
+ end
+}}}
+
+個別ã®ã‚³ãƒžãƒ³ãƒ‰ã«ã¤ã„ã¦ã¯ã€å·¦ã«ã‚ã‚‹Elementsセクションã®ä»–ã®ãƒªãƒ³ã‚¯ã‚’見ã¦ãã ã•ã„。ビデオファイルã®ä¸­æ–­ã¾ãŸã¯å†ç”ŸãŒã—ãŸã„ãªã‚‰ã€ãƒ“デオã®ä¸­æ–­ã‚„å†ç”Ÿã¯ç‰¹ç•°ãªã®ã§ã€[[Video]]セクションを確èªã—ã¦ãã ã•ã„。中断ã™ã‚‹ãƒœã‚¿ãƒ³ã¨ã„ã†æ„Ÿã˜ã§ã¯ã‚ã‚Šã¾ã›ã‚“。
+
+=== displace(left: a number, top: a number) » self ===
+
+è¦ç´ ã‚’移動ã—ã¦ç½®ãæ›ãˆã¾ã™ã€‚ã—ã‹ã—周囲ã®ãƒ¬ã‚¤ã‚¢ã‚¦ãƒˆã¯å¤‰æ›´ã—ã¾ã›ã‚“。特ã«ã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã—ã¦ã„ã‚‹é–“ã§ã‚‚è¦ç´ ã®ä½ç½®ã‚’ä¿æŒã—ãŸã„ãªã‚‰ã€ã“ã‚Œã¯å¾®å¦™ãªã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã«é‡è¦ã§ã™ã€‚ãŠãらãç´ æ—©ã震ãˆã‚‹ãƒœã‚¿ãƒ³ã‚„視界ã«ã‚¹ãƒ­ãƒƒãƒˆã‚’滑り込ã¾ã›ã‚‹ã‚ˆã†ãªã€‚
+
+è¦ç´ ã‚’ç½®ãæ›ãˆã‚‹ã¨ãã€ãã‚ŒãŒé…ç½®ã•ã‚Œã¦ã„る左上ã®è§’ã‹ã‚‰ç›¸å¯¾çš„ã«ç§»å‹•ã—ã¾ã™ã€‚ãã®ãŸã‚ã€è¦ç´ ãŒ(20, 40)ã®åº§æ¨™ã«ã‚ã‚Šã€2ピクセル左ã¨6ピクセル上ã«ç½®ãæ›ãˆã‚‹ãªã‚‰ã€çµæžœçš„ã«(22, 46)ã®åº§æ¨™ã¨ãªã‚Šã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ flow :margin => 12 do
+ # Set up three buttons
+ button "One"
+ @two = button "Two"
+ button "Three"
+
+ # Bounce the second button
+ animate do |i|
+ @two.displace(0, (Math.sin(i) * 6).to_i)
+ end
+ end
+ end
+}}}
+
+ä»–ã®ï¼’ã¤ã®ãƒœã‚¿ãƒ³ã¯å‹•ã‹ãšã«ã˜ã£ã¨ã—ã¦ã„ã¾ã™ãŒã€ï¼’番目ã®ãƒœã‚¿ãƒ³ãŒé£›ã³è·³ã­ã‚‹ã“ã¨ã«æ³¨ç›®ã—ã¦ãã ã•ã„。ã“ã®çŠ¶æ³ã§æ™®é€šã®`move`を使ã†ãªã‚‰ã€ï¼’番目ã®ãƒœã‚¿ãƒ³ã¯ãƒ¬ã‚¤ã‚¢ã‚¦ãƒˆã‹ã‚‰å–り除ã‹ã‚Œã¦ã€ï¼’番目ã®ãƒœã‚¿ãƒ³ãŒå…¨ããã“ã«ãªã„ã‹ã®ã‚ˆã†ã«æŒ¯ã‚‹èˆžã†ã§ã—ょã†ã€‚([[Common.move]]ã®ä¾‹ã‚’見ã¦ãã ã•ã„。)
+
+'''特ã«æ³¨æ„ã—ã¦ãã ã•ã„:'''表示ã•ã‚Œã‚‹è¦ç´ ã®åº§æ¨™ã‚’å¾—ã‚‹ãŸã‚ã«`left`ã¨`top`メソッドを利用ã™ã‚‹ãªã‚‰ã€é€šå¸¸ã®åº§æ¨™ã‚’å¾—ã‚‹ã ã‘ã§ã™ã€‚ãã‚Œã¯ç½®ãæ›ãˆãŒè¡Œã‚ã‚Œã¦ã„ãªã„ã‹ã®ã‚ˆã†ã§ã™ã€‚ç½®ãæ›ãˆã¯å³åº§ã®ã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã ã‘を目的ã¨ã—ã¾ã™ã€‚
+
+=== height() » a number ===
+
+エレメントã®åž‚ç›´ã®ãƒ”クセルã«ã‚ˆã‚‹ã‚¹ã‚¯ãƒªãƒ¼ãƒ³ã‚µã‚¤ã‚ºã§ã™ã€‚ç”»åƒã®å ´åˆã«ã¯ã€ã“ã‚Œã¯ç”»åƒå…¨ä½“ã®ã‚µã‚¤ã‚ºã§ã¯ã‚ã‚Šã¾ã›ã‚“。ã“ã‚Œã¯è¦ç´ ã®ç¾åœ¨è¡¨ç¤ºã•ã‚Œã¦ã„る高ã•ã§ã™ã€‚
+
+150x150ピクセルã®ç”»åƒã‚’æŒã£ã¦ã„ã¦50ピクセルã«å¹…を設定ã™ã‚‹ãªã‚‰ã€ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯50ã‚’è¿”ã—ã¾ã™ã€‚
+
+例や他ã®è§£èª¬ã®ãŸã‚ã«[[Common.width]]メソッドも見ã¦ãã ã•ã„。
+
+=== hide() » self ===
+
+è¦ç´ ãŒè¦‹ãˆãªã„よã†ã«ã€éžè¡¨ç¤ºã«ã—ã¾ã™ã€‚[[Common.show]]ã‚„[[Common.toggle]]も見ã¦ãã ã•ã„。
+
+=== left() » a number ===
+
+è¦ç´ ã®å·¦ç«¯ã®ä½ç½®ã‚’ピクセルã§å¾—ã¾ã™ã€‚
+
+=== move(left: a number, top: a number) » self ===
+
+スロットã®ç¯„囲内ã§ãƒ”クセルã«ã‚ˆã£ã¦æŒ‡å®šã—ãŸä½ç½®ã«è¦ç´ ã‚’移動ã—ã¾ã™ã€‚ãã®è¦ç´ ã¯ã‚¹ãƒ­ãƒƒãƒˆã®å†…部ã«ã‚ã‚Šã¾ã™ã€‚ã—ã‹ã—ã€ã‚‚ã¯ã‚„スロットã®ä»–ã®è¦ç´ ã¨ä¸€ç·’ã«ç©ã¿ä¸Šã’られãŸã‚Šãƒ•ãƒ­ãƒ¼ã•ã‚ŒãŸã‚Šã—ã¾ã›ã‚“。ãã®è¦ç´ ã¯çµ¶å¯¾çš„ãªä½ç½®æŒ‡å®šã§ã¯ãªãã€è‡ªç”±ã«æµ®ã‹ã‚“ã§ã„ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ flow :margin => 12 do
+ # Set up three buttons
+ button "One"
+ @two = button "Two"
+ button "Three"
+
+ # Bounce the second button
+ animate do |i|
+ @two.move(40, 40 + (Math.sin(i) * 6).to_i)
+ end
+ end
+ end
+}}}
+
+3番目ã®ãƒœã‚¿ãƒ³ãŒãã®ä½ç½®ã«ã‚¹ãƒ©ã‚¤ãƒ‰ã™ã‚‹ã“ã¨ã‚’許ã—ã¦ãŠã‚Šã€ï¼’番目ã®ãƒœã‚¿ãƒ³ã¯ç‰¹å®šã®å ´æ‰€ã«å‹•ã‹ã•ã‚Œã¦ã—ã¾ã™ã€‚è¦ç´ ã‚’別ã®å ´æ‰€ã«å¤‰æ›´ã—ãªã„ã§ç§»å‹•ã—ãŸã„ãªã‚‰ã€[[Common.displace]]メソッドを見ã¦ãã ã•ã„。
+
+=== parent() » a Shoes::Stack or Shoes::Flow ===
+
+ãã®è¦ç´ ã®ã‚³ãƒ³ãƒ†ãƒŠã®ã‚ªãƒ–ジェクトを得ã¾ã™ã€‚å対ã®ã“ã¨ã‚’è¡Œã†ãŸã‚ã«ã¯ã‚¹ãƒ­ãƒƒãƒˆã®[[Traversing.contents]]も見ã¦ãã ã•ã„:コンテナã®è¦ç´ ã‚’å¾—ã¾ã™ã€‚
+
+=== remove() » self ===
+
+スロットã‹ã‚‰è¦ç´ ã‚’削除ã—ã¾ã™ã€‚(他ã®è¨€è‘‰ã§è¨€ã„æ›ãˆã‚‹ã¨ï¼šã‚¬ãƒ™ãƒ¼ã‚¸ã«æŠ•ã’ã¾ã™ã€‚)ãã®è¦ç´ ã¯ã‚‚ã†è¡¨ç¤ºã•ã‚Œã¾ã›ã‚“。
+
+=== show() » self ===
+
+è¦ç´ ãŒéžè¡¨ç¤ºãªã‚‰ã€è¡¨ç¤ºã—ã¾ã™ã€‚[[Common.hide]]ã‚„[[Common.toggle]]も見ã¦ãã ã•ã„。
+
+=== style() » styles ===
+
+ãƒãƒƒã‚·ãƒ¥ã®å½¢ã§ã€è¦ç´ ã«é©ç”¨ã™ã‚‹ãƒ•ãƒ«ã‚»ãƒƒãƒˆã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’å¾—ã¾ã™ã€‚`width`ã‚„`height`ã‚„`top`ã®ã‚ˆã†ãªãƒ¡ã‚½ãƒƒãƒ‰ã¯ç‰¹å®šã®ãƒ”クセルã§ã®ã‚µã‚¤ã‚ºã‚’è¿”ã—ã¾ã™ãŒã€`style[:width]`ã¾ãŸã¯`style[:top]`を利用ã™ã‚‹ã¨ã€åˆã‚ã®è¨­å®šã‚’å¾—ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚("100%"ã®å¹…ã¾ãŸã¯"10px"ã®ãƒˆãƒƒãƒ—ã®ã‚ˆã†ãªï¼‰
+
+{{{
+ #!ruby
+ Shoes.app do
+ # A button which take up the whole page
+ @b = button "All of it", :width => 1.0, :height => 1.0
+
+ # When clicked, show the styles
+ @b.click { alert(@b.style.inspect) }
+ end
+}}}
+
+=== style(styles) » styles ===
+
+è¦ç´ ã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’変更ã—ã¾ã™ã€‚ã“ã‚Œã¯è¦ç´ ã®`:width`ã¨`:height`ã€ãƒ†ã‚­ã‚¹ãƒˆã®ãƒ•ã‚©ãƒ³ãƒˆã®`:size`ã€å½¢çŠ¶ã®`:stroke`ã‚„`:fill`ã‚’å«ã¿ã¾ã™ã€‚ã¾ãŸã¯ä»–ã®å¤šãã®ã‚¹ã‚¿ã‚¤ãƒ«ã®è¨­å®šã‚‚ã§ã™ã€‚
+
+=== toggle() » self ===
+
+è¦ç´ ãŒè¡¨ç¤ºã•ã‚Œã¦ã„ã‚‹ãªã‚‰éžè¡¨ç¤ºã«ã—ã¾ã™ã€‚ã¾ãŸã¯è¦ç´ ãŒéžè¡¨ç¤ºãªã‚‰è¡¨ç¤ºã—ã¾ã™ã€‚
+
+=== top() » a number ===
+
+è¦ç´ ã®ä¸Šç«¯ã®ãƒ”クセルã®ä½ç½®ã‚’å¾—ã¾ã™ã€‚
+
+=== width() » a number ===
+
+è¦ç´ ã®å…¨ä½“ã®å¤§ãã•ã®å¹…をピクセルã§å¾—ã¾ã™ã€‚ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ã„ã¤ã‚‚正確ãªãƒ”クセルサイズを返ã—ã¾ã™ã€‚ç”»åƒã®å ´åˆã¯ã€ç”»åƒã®å…¨å¹…ã§ã¯ãªãã€è¡¨ç¤ºã•ã‚Œã¦ã„るサイズã ã‘ã§ã™ã€‚詳ã—ãã¯[[Common.height]]メソッドも見ã¦ãã ã•ã„。
+
+ã¾ãŸã€120ピクセルã®å¹…ã®ã‚¹ã‚¿ãƒƒã‚¯å†…ã«100%ã®å¹…ã®è¦ç´ ã‚’作æˆã—ãŸãªã‚‰ã€`120`ãŒè¿”ã•ã‚Œã¾ã™ã€‚ã—ã‹ã—ãªãŒã‚‰ã€`style[:width]`を呼んã ãªã‚‰ã€`"100%"`ã‚’å¾—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :width => 120 do
+ @b = button "Click me", :width => "100%" do
+ alert "button.width = #{@b.width}\n" +
+ "button.style[:width] = #{@b.style[:width]}"
+ end
+ end
+ end
+}}}
+
+幅を設定ã™ã‚‹ãŸã‚ã«ã¯ã€[[Common.style]]メソッドをもã†ä¸€åº¦èª¿ã¹ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ã€‚ãã—ã¦ã€150ピクセルã®å¹…ã«ãƒœã‚¿ãƒ³ã‚’設定ã™ã‚‹ã«ã¯ï¼š`@b.style(:width => 150)`。
+
+è¦ç´ ã®å¹…ã‚’å–ã‚‹ã«ã¯ã€è¨­å®šã‚’空ã«ã™ã‚‹ãŸã‚ã«`@b.style(:width => nil)`ã¨ã—ã¾ã™
+
+== Background ==
+
+背景ã¯ã‚¹ãƒ­ãƒƒãƒˆå…¨ä½“ã«æ¸¡ã£ã¦å¡—られãŸã€ã‚°ãƒ©ãƒ‡ãƒ¼ã‚·ãƒ§ãƒ³ã¾ãŸã¯ç”»åƒã®è‰²ã§ã™ã€‚背景ã¨æž ç·šã¯Shoes::Patternã®ç¨®é¡žã®ä¸€ã¤ã§ã™ã€‚!{:margin_left => 100}man-ele-background.png!
+
+''背景(background)''ã¨å‘¼ã°ã‚Œã¦ã„ã‚‹ã«ã‚‚é–¢ã‚らãšã€ã“ã®è¦ç´ ã¯ä»–ã®è¦ç´ ã‚ˆã‚Šã‚‚å‰é¢ã«è¡¨ç¤ºã•ã‚Œã¾ã™ã€‚背景ãŒã‚¹ãƒ­ãƒƒãƒˆã§å¡—られãŸä½•ã‹ä»–ã®ã‚‚ã®ï¼ˆ`rect`ã¾ãŸã¯`oval`ã®ã‚ˆã†ãªï¼‰ã®å¾Œã«ããŸå ´åˆã€èƒŒæ™¯ã¯ãã®è¦ç´ ã®ä¸Šã«å¡—られã¾ã™ã€‚
+
+ã‚‚ã£ã¨ã‚‚å˜ç´”ãªèƒŒæ™¯ã¯ã€é»’ã®èƒŒæ™¯ã®ã‚ˆã†ãª[[Element.background]]メソッドã«ã‚ˆã£ã¦ä½œæˆã•ã‚ŒãŸã€å˜è‰²ã®èƒŒæ™¯ã§ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ background black
+ end
+}}}
+
+ã“ã®ã‚ˆã†ãªå˜ç´”ãªèƒŒæ™¯ã¯ã‚¹ãƒ­ãƒƒãƒˆãŒå«ã‚€ã‚‚ã®å…¨ä½“ã‚’å¡—ã‚Šã¤ã¶ã—ã¾ã™ï¼ˆã“ã®å ´åˆã¯ã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦å…¨ä½“ãŒé»’ã§å¡—られã¾ã™ã€‚)
+
+好ããªã‚ˆã†ã«èƒŒæ™¯ã®ã‚µã‚¤ã‚ºã‚’切り詰ã‚ãŸã‚Šã‚ã¡ã“ã¡ç§»å‹•ã—ãŸã‚Šã™ã‚‹ãŸã‚ã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’利用ã§ãã¾ã™ã€‚
+
+ウィンドウã®ä¸Šå´ã‚’50ピクセルã«æ¸¡ã£ã¦é»’ã„背景ã§å¡—ã‚Šã¤ã¶ã—ã¾ã™ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ background black, :height => 50
+ end
+}}}
+
+ã¾ãŸã¯ã€ã‚¦ã‚£ãƒ³ãƒ‰ã‚¦ã®å³ç«¯ã®50ピクセルã®åˆ—ã‚’å¡—ã‚Šã¤ã¶ã—ã¾ã™ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ background black, :width => 50, :right => 50
+ end
+}}}
+
+背景ã¯æ™®é€šã®è¦ç´ ã¨åŒæ§˜ãªã®ã§ã€ãã®ä»–ã®ã™ã¹ã¦ã®ãƒ¡ã‚½ãƒƒãƒ‰ã«ã¤ã„ã¦ã¯[[Elements]]セクションã®å§‹ã‚ã®éƒ¨åˆ†ã‚‚見ã¦ãã ã•ã„。
+
+=== to_pattern() » a Shoes::Pattern ===
+
+背景を塗りã¤ã¶ã™ãŸã‚ã«åˆ©ç”¨ã•ã‚ŒãŸã‚°ãƒ©ãƒ‡ãƒ¼ã‚·ãƒ§ãƒ³ã‚„ç”»åƒã‚’通常ã®Shoes::Patternオブジェクトã«é…ç½®ã—ã€è‰²ã‚’ヤンクã—ã¾ã™ã€‚ãã—ã¦ã€ä»–ã®ã‚ªãƒ–ジェクトã«èƒŒæ™¯ã‚„æž ç·šã«æ¸¡ã™ã“ã¨ãŒã§ãã¾ã™ã€‚好ããªã‚ˆã†ã«å†åˆ©ç”¨ã—ã¦ãã ã•ã„。
+
+== Border ==
+
+æž ç·šã¯ã‚¹ãƒ­ãƒƒãƒˆã®å‘¨å›²ã®ç·šã«å¡—られãŸã€è‰²ã‚„グラデーションや画åƒã§ã™ã€‚次ã®ã‚»ã‚¯ã‚·ãƒ§ãƒ³ã®èƒŒæ™¯ã®è¦ç´ ã§ã¯ã€æž ç·šï¼ˆBorder)ã¯Shoes::Patternã®ä¸€ç¨®ã§ã™ã€‚!{:margin_left => 100}man-ele-border.png!
+
+ã¯ã˜ã‚ã«ã€ã™ã¹ã¦ã®æž ç·šã¯ã‚¹ãƒ­ãƒƒãƒˆã®å‘¨å›²ã®å¤–å´ã§ã¯ãªãã€'''内å´'''ã‚’å¡—ã‚‹ã“ã¨ã«ã¤ã„ã¦çŸ¥ã‚‹ã“ã¨ã¯é‡è¦ã§ã™ã€‚ãã®ãŸã‚ã€50ピクセルã®å¹…ã®ã‚¹ãƒ­ãƒƒãƒˆã«5ピクセルã®æž ç·šã‚’å¡—ã‚‹ãªã‚‰ã€ãã‚Œã¯æž ç·šã§å›²ã¾ã‚ŒãŸã‚¹ãƒ­ãƒƒãƒˆãŒå†…部ã«40ピクセルã®å¹…ã®ã‚¨ãƒªã‚¢ã‚’æŒã¤ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚
+
+ã“ã‚Œã¯æž ç·šï¼ˆBorder)を[[Background]]ã®ä¸Šã«å¡—ã‚‹ãªã‚‰ã€ãã®èƒŒæ™¯ã®ç«¯ã®ä¸Šã‚’æž ç·šã«ã‚ˆã£ã¦å¡—られるã“ã¨ã‚‚æ„味ã—ã¾ã™ã€‚
+
+æ­£ã«ãã®ã‚ˆã†ãªã‚¹ãƒ­ãƒƒãƒˆãŒã“ã“ã«ã‚ã‚Šã¾ã™ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :width => 50 do
+ border black, :strokewidth => 5
+ para "=^.^=", :stroke => green
+ end
+ end
+}}}
+
+スロットã®å¤–å´ã®ä¿®æ­£ã«æž ç·šã‚’å¡—ã‚ŠãŸã„ãªã‚‰ã€ã‚‚ã†ä¸€ã¤ã®ã‚¹ãƒ­ãƒƒãƒˆã§ãã®ã‚¹ãƒ­ãƒƒãƒˆã‚’ラップã™ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ã€‚ãã®çµæžœã€ã‚¹ãƒ­ãƒƒãƒˆã®å¤–å´ã«æž ç·šãŒé…ç½®ã•ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack :width => 60 do
+ border black, :strokewidth => 5
+ stack :width => 50 do
+ para "=^.^=", :stroke => green
+ end
+ end
+ end
+}}}
+
+HTMLã‚„ä»–ã®å¤šãã®è¨€èªžã§ã¯ã€æž ç·šã¯ç®±ã®å¤–å´ã«å¡—られるãŸã‚ã€ãƒœãƒƒã‚¯ã‚¹å…¨ä½“ã®å¹…ãŒå¢—加ã—ã¾ã™ã€‚Shoesã¯ä¸€è²«æ€§ã‚’考慮ã—ã¦ãƒ‡ã‚¶ã‚¤ãƒ³ã•ã‚Œã¦ã„ã‚‹ãŸã‚ã€æž ç·šã‚„マージンや他ã®ã©ã‚“ãªã‚‚ã®ã‚‚æ°—ã«ã—ãªã„ã§ãã‚Œã¯50ピクセルã®å¹…ã®ã¾ã¾ã§ã™ã€‚
+
+æž ç·šã«åˆ©ç”¨ã™ã‚‹ä»–ã®ãƒ¡ã‚½ãƒƒãƒ‰ã«ã¤ã„ã¦ã¯[[Elements]]セクションも確èªã—ã¦ãã ã•ã„。
+
+=== to_pattern() » a Shoes::Pattern ===
+
+æž ç·šã‚’å¡—ã‚‹ãŸã‚ã®è‰²ã€ã‚°ãƒ©ãƒ‡ãƒ¼ã‚·ãƒ§ãƒ³ã¾ãŸã¯ç”»åƒã‚’å…ƒã«ã—ãŸåŸºæœ¬ã®ãƒ‘ターンオブジェクトを作æˆã—ã¾ã™ã€‚
+
+== Button ==
+
+ボタン(Button)ã¯ã€ã”存知ã®ã¨ãŠã‚Šã€æŠ¼ã—ボタンã§ã™ã€‚ãれらをクリックã—ãŸã‚‰ä½•ã‹ã‚’è¡Œã„ã¾ã™ã€‚ボタンã¯"OK"ã¾ãŸã¯"Are you sure?"ãªã©ã‚’表示ã—ã¾ã™ã€‚ãã—ã¦ã€ã‚ˆã‘ã‚Œã°ãƒœã‚¿ãƒ³ã‚’クリックã—ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ button "OK!"
+ button "Are you sure?"
+ end
+}}}
+
+上記ã®ä¾‹ã®ãƒœã‚¿ãƒ³ã¯ãれらãŒã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸã¨ãã«ä½•ã‚‚è¡Œã„ã¾ã›ã‚“。åƒãã‚’è¡Œã‚ã›ã‚‹ãŸã‚ã«ã¯ã€ãã‚Œãžã‚Œã®ãƒœã‚¿ãƒ³ã«ãƒ–ロックをå–り付ã‘ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ button "OK!" do
+ append { para "Well okay then." }
+ end
+ button "Are you sure?" do
+ append { para "Your confidence is inspiring." }
+ end
+ end
+}}}
+
+ã“ã®ã‚ˆã†ã«ãƒœã‚¿ãƒ³ã«ãƒ–ロックをå–り付ã‘ã¾ã—ãŸã€‚ãã‚Œãžã‚Œã®ãƒ–ロックã¯ãƒšãƒ¼ã‚¸ã«æ–°ã—ã„パラグラフをå–り付ã‘ã¾ã™ã€‚クリックã™ã‚‹åº¦ã«ã€ãƒ‘ラグラフãŒè¿½åŠ ã•ã‚Œã¾ã™ã€‚
+
+ã“ã‚Œã¯ã“れ以上深ãã¯ã—ã¾ã›ã‚“。ボタンã¯ã‚¯ãƒªãƒƒã‚¯å¯èƒ½ãªå¥ã§ã—ã‹ã‚ã‚Šã¾ã›ã‚“。
+
+厳密ã«è¨€ãˆã°ã€æ¬¡ã«ç¶šã例ã§ã¯ä»–ã®æ–¹æ³•ã§ãれを書ã„ã¦ã„ã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @b1 = button "OK!"
+ @b1.click { para "Well okay then." }
+ @b2 = button "Are you sure?"
+ @b2.click { para "Your confidence is inspiring." }
+ end
+}}}
+
+見ãŸç›®ã¯åŠ‡çš„ã«é•ã„ã¾ã™ãŒã€ã“ã‚Œã¯åŒã˜å‹•ãã§ã™ã€‚1ã¤ç›®ã®é•ã„:直接ブロックをボタンã«å–り付ã‘ã‚‹ã®ã§ã¯ãªãã€`click`メソッドを通ã—ã¦ã€ãƒ–ロックを後ã§å–り付ã‘ã¦ã„ã¾ã™ã€‚
+
+ï¼’ã¤ç›®ã®é•ã„ã¯ãƒœã‚¿ãƒ³ã«ã¯å…¨ã関係ãŒã‚ã‚Šã¾ã›ã‚“。ShoesãŒã‚¹ãƒ­ãƒƒãƒˆã«è¦ç´ ã‚’直接追加ã™ã‚‹ã“ã¨ã‚’許ã—ã¦ã„ã‚‹ã®ã§ã€`append`ブロックãŒå–り除ã‹ã‚Œã¦ã„ã¾ã™ã€‚ãã®ãŸã‚直接`para`を呼ã¶ã“ã¨ãŒã§ãã¾ã™ã€‚(`prepend`ã€`before`ã¾ãŸã¯`after`メソッドã®å ´åˆã«ã¯ã§ãã¾ã›ã‚“。)
+
+以下ã®ãƒ¡ã‚½ãƒƒãƒ‰ã«åŠ ãˆã¦ã€ãƒœã‚¿ãƒ³ã¯[[Common]]ã®ã™ã¹ã¦ã®ãƒ¡ã‚½ãƒƒãƒ‰ã‚‚継承ã—ã¾ã™ã€‚
+
+=== click() { |self| ... } » self ===
+
+ボタンãŒã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸã¨ãã«ã¯ã€`click`ブロックãŒå‘¼ã°ã‚Œã¾ã™ã€‚ã“ã®ãƒ–ロックã¯`self`を渡ã—ã¾ã™ã€‚æ„味ã™ã‚‹ã“ã¨ï¼šã©ã¡ã‚‰ã®ãƒœã‚¿ãƒ³ã§ã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸã‹ã€‚
+
+=== focus() » self ===
+
+ボタンã®ãƒ•ã‚©ãƒ¼ã‚«ã‚¹ã‚’移動ã—ã¾ã™ã€‚ãã®ãƒœã‚¿ãƒ³ã¯ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚Œã€ãƒ¦ãƒ¼ã‚¶ãŒã‚¨ãƒ³ã‚¿ãƒ¼ã‚­ãƒ¼ã‚’打ã¦ã°ã€ã‚¯ãƒªãƒƒã‚¯ã•ã‚Œã¾ã™ã€‚
+
+== Check ==
+
+ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã¯ãƒã‚§ãƒƒã‚¯ã•ã‚ŒãŸçŠ¶æ…‹ã¾ãŸã¯ãƒã‚§ãƒƒã‚¯ã•ã‚Œã¦ã„ãªã„状態ã«ãªã‚‹ã‚¯ãƒªãƒƒã‚¯å¯èƒ½ãªå››è§’ã„ç®±ã§ã™ã€‚1ã¤ã®ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã§ã¯é€šå¸¸ã¯"ã¯ã„(yes)"ã¾ãŸã¯"ã„ã„ãˆï¼ˆno)"ã®è³ªå•ã‚’ãŸãšã­ã¾ã™ã€‚複数ã®ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã®ã‚»ãƒƒãƒˆã§ã¯to-doリストã§ã‚‚見られã¾ã™ã€‚
+
+ã“ã“ã«ãƒã‚§ãƒƒã‚¯ãƒªã‚¹ãƒˆã®ã‚µãƒ³ãƒ—ルãŒã‚ã‚Šã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ flow { check; para "Frances Johnson" }
+ flow { check; para "Ignatius J. Reilly" }
+ flow { check; para "Winston Niles Rumfoord" }
+ end
+ end
+}}}
+
+基本的ã«ã¯ãƒã‚§ãƒƒã‚¯ï¼ˆcheck)を利用ã™ã‚‹ãŸã‚ã®ï¼’ã¤ã®æ–¹æ³•ãŒã‚ã‚Šã¾ã™ã€‚ãƒã‚§ãƒƒã‚¯ã«ãƒã‚§ãƒƒã‚¯ãŒã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸã¨ãã«å‘¼ã°ã‚Œã‚‹ãƒ–ロックをå–り付ã‘ã¾ã™ã€‚ãã—ã¦ï¼ã¾ãŸã¯ã€ãƒœãƒƒã‚¯ã‚¹ãŒãƒã‚§ãƒƒã‚¯ã•ã‚Œã¦ã„ã‚‹ã‹ã©ã†ã‹ç¢ºèªã™ã‚‹`checked?`メソッドを利用ã§ãã¾ã™ã€‚
+
+ã§ã¯ã€ä¸Šè¨˜ã®ä¾‹ã«è¿½åŠ ã—ã¾ã—ょã†ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ @list = ['Frances Johnson', 'Ignatius J. Reilly',
+ 'Winston Niles Rumfoord']
+
+ stack do
+ @list.map! do |name|
+ flow { @c = check; para name }
+ [@c, name]
+ end
+
+ button "What's been checked?" do
+ selected = @list.map { |c, name| name if c.checked? }.compact
+ alert("You selected: " + selected.join(', '))
+ end
+ end
+ end
+}}}
+
+ãã—ã¦ã€ãƒœã‚¿ãƒ³ãŒæŠ¼ã•ã‚ŒãŸã¨ãã«ã€`checked?`メソッドを利用ã—ã¦ã€ãã‚Œãžã‚Œã®ãƒã‚§ãƒƒã‚¯ã¯ãã®çŠ¶æ…‹ã‚’å°‹ã­ã¾ã™ã€‚
+
+下ã®ãƒœã‚¿ãƒ³ãƒ¡ã‚½ãƒƒãƒ‰ã®ä¸€è¦§ã ã‘ã§ãªãã€ã™ã¹ã¦ã®è¦ç´ ãŒå¿œç­”ã§ãã‚‹ã€[[Common]]メソッドã®ä¸€è¦§ã‚‚見ã¦ãã ã•ã„。
+
+=== checked?() » true or false ===
+
+ãã®ç®±ãŒãƒã‚§ãƒƒã‚¯ã•ã‚Œã¦ã„ã‚‹ã‹ã©ã†ã‹ã‚’è¿”ã—ã¾ã™ã€‚ãã—ã¦ã€`true`ã®æ„味ã¯"ã¯ã„ã€ã“ã®ç®±ã¯ãƒã‚§ãƒƒã‚¯ã•ã‚Œã¦ã„ã¾ã™ï¼"ã§ã™ã€‚
+
+=== checked = true or false ===
+
+ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã‚’マークã—ãŸã‚Šãƒžãƒ¼ã‚¯ã‚’外ã—ãŸã‚Šã—ã¾ã™ã€‚例ãˆã°ã€`checked = false`を利用ã—ã¦ã€ç®±ã®ãƒã‚§ãƒƒã‚¯ã‚’外ã—ã¾ã™ã€‚
+
+=== click() { |self| ... } » self ===
+
+ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ãŒã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸã¨ãã«ã€ãã®`click`ブロックãŒå‘¼ã°ã‚Œã¾ã™ã€‚ã“ã®ãƒ–ロックã¯ã€ã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã‚ªãƒ–ジェクトã®`self`を渡ã—ã¾ã™ã€‚
+
+ç®±ãŒãƒã‚§ãƒƒã‚¯ã•ã‚Œã‚‹ã¨ãã¨ãƒã‚§ãƒƒã‚¯ã‚’外ã™ã¨ãã®ã©ã¡ã‚‰ã‚‚クリックãŒæ¸¡ã•ã‚Œã¾ã™ã€‚
+
+=== focus() » self ===
+
+ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã«ãƒ•ã‚©ãƒ¼ã‚«ã‚¹ã‚’移動ã—ã¾ã™ã€‚ãã®ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã¯ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚Œã€ãƒ¦ãƒ¼ã‚¶ãŒã‚¨ãƒ³ã‚¿ãƒ¼ã‚­ãƒ¼ã‚’打ã¦ã°ã€ãã®ãƒã‚§ãƒƒã‚¯ãƒœãƒƒã‚¯ã‚¹ã¯ãƒã‚§ãƒƒã‚¯ã•ã‚ŒãŸçŠ¶æ…‹ã¨ãƒã‚§ãƒƒã‚¯ã•ã‚Œã¦ã„ãªã„状態ã®é–“をトグルã—ã¾ã™ã€‚
+
+== EditBox ==
+
+エディットボックス(Edit box)ã¯ã€ãƒ†ã‚­ã‚¹ãƒˆã‚’入力ã™ã‚‹ãŸã‚ã®å¹…広ã„長方形ã®ç®±ã§ã™ã€‚webã§ã¯ã€ãれらã¯ãƒ†ã‚­ã‚¹ãƒˆã‚¨ãƒªã‚¢ã¨å‘¼ã°ã‚Œã¾ã™ã€‚ãれらã¯é•·ã„記述を入力ã™ã‚‹ãŸã‚ã®ãƒžãƒ«ãƒãƒ©ã‚¤ãƒ³ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒœãƒƒã‚¯ã‚¹ã§ã™ã€‚エッセイã§ã•ãˆæ›¸ã‘ã¾ã™ï¼ã€€!{:margin_left => 100}man-ele-editbox.png!
+
+スタイルを何も設定ã—ãªã‘ã‚Œã°ã€ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒœãƒƒã‚¯ã‚¹ã¯200ピクセルx108ピクセルã®ã‚µã‚¤ã‚ºã§ã™ã€‚特定ã®ã‚µã‚¤ã‚ºã«è¨­å®šã™ã‚‹ãŸã‚ã«`:width`ã¨`:height`ã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’利用ã™ã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ edit_box
+ edit_box :width => 100, :height => 100
+ end
+}}}
+
+([[Button]]ã‚„[[Check]]ãªã©ã®ï¼‰ä»–ã®ã‚³ãƒ³ãƒˆãƒ­ãƒ¼ãƒ«ã¯ã‚¯ãƒªãƒƒã‚¯ã‚¤ãƒ™ãƒ³ãƒˆã ã‘ã‚’æŒã£ã¦ã„ã¾ã™ãŒã€[[EditLine]]やエディットボックス(EditBox)ã¯`change`イベントもæŒã£ã¦ã„ã¾ã™ã€‚誰ã‹ãŒã‚¿ã‚¤ãƒ—ã—ãŸã‚Šç®±ã‹ã‚‰å‰Šé™¤ã—ãŸã‚‰`change`ã®ãƒ–ロックã¯ã„ã¤ã§ã‚‚呼ã°ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ edit_box do |e|
+ @counter.text = e.text.size
+ end
+ @counter = strong("0")
+ para @counter, " characters"
+ end
+}}}
+
+ã“ã®ä¾‹ã§ã¯ãƒ–ロック内部ã§[[EditBox.text]]メソッドを利用ã—ã¦ã„ã‚‹ã“ã¨ã«ã‚‚注æ„ã—ã¦ãã ã•ã„。ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯ç®±ã«å¯¾ã—ã¦ã‚¿ã‚¤ãƒ—ã—ãŸã™ã¹ã¦ã®æ–‡å­—ã®æ–‡å­—列をã‚ãªãŸã«ä¸Žãˆã¾ã™ã€‚
+
+エディットボックスã®æ›´ãªã‚‹ãƒ¡ã‚½ãƒƒãƒ‰ã¯ä»¥ä¸‹ã«ä¸€è¦§ã«ã—ã¾ã™ãŒã€ã™ã¹ã¦ã®è¦ç´ ãŒå¿œç­”ã§ãã‚‹ã€[[Common]]メソッドã®ä¸€è¦§ã‚‚見ã¦ãã ã•ã„。
+
+=== change() { |self| ... } » self ===
+
+エディットボックスã«æ–‡å­—ãŒè¿½åŠ ã•ã‚ŒãŸã‚Šå–り除ã‹ã‚Œã‚‹ãŸã³ã«ã€`chenage`ブロックãŒå‘¼ã°ã‚Œã¾ã™ã€‚ブロックã«ã¯å¤‰æ›´ã•ã‚ŒãŸã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒœãƒƒã‚¯ã‚¹ã®ã‚ªãƒ–ジェクトã§ã‚ã‚‹`self`ãŒä¸Žãˆã‚‰ã‚Œã¾ã™ã€‚
+
+=== focus() » self ===
+
+エディットボックスã«ãƒ•ã‚©ãƒ¼ã‚«ã‚¹ã‚’移動ã—ã¾ã™ã€‚ãã®ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒœãƒƒã‚¯ã‚¹ã¯ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚Œã€ãƒ¦ãƒ¼ã‚¶ã¯ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒœãƒƒã‚¯ã‚¹ã«ã‚¿ã‚¤ãƒ—ã§ãã¾ã™ã€‚
+
+=== text() » self ===
+
+ç®±ã«ã‚¿ã‚¤ãƒ—ã•ã‚ŒãŸæ–‡å­—を文字列ã¨ã—ã¦è¿”ã—ã¾ã™ã€‚
+
+=== text = a string ===
+
+`a string`ã®æ–‡å­—をエディットボックスã«ä»£å…¥ã—ã¾ã™ã€‚
+
+== EditLine ==
+
+エディットライン(Edit line)ã¯ç´°é•·ã„ã€å°ã•ãªãƒ†ã‚­ã‚¹ãƒˆã‚’入力ã™ã‚‹ç®±ã§ã™ã€‚エディットボックスã¯ãƒžãƒ«ãƒãƒ©ã‚¤ãƒ³ã§ã™ãŒã€ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒ©ã‚¤ãƒ³ã¯ï¼‘è¡Œã§ã™ã€‚ã“ã‚Œã¯ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒ©ã‚¤ãƒ³ã§ã™ã€‚ã¡ãªã¿ã«æ°´å¹³ã§ã™ã€‚
+
+スタイルãŒè¨­å®šã•ã‚Œã¦ã„ãªã„エディットラインã¯200ピクセルã®å¹…ã¨28ピクセルã®é«˜ã•ã§ã™ã€‚ã“ã‚Œã¯ãŠãŠã‚ˆãã§ã™ã€‚プラットフォームã«ã‚ˆã£ã¦é«˜ã•ã¯æ§˜ã€…ã§ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ edit_line
+ edit_line :width => 400
+ end
+ end
+}}}
+
+`:width`ã¨`:height`ã®ä¸¡æ–¹ã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’設定ã™ã‚‹ã“ã¨ã«ã‚ˆã‚Šã‚µã‚¤ã‚ºã‚’変更ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚ã—ã‹ã—ãªãŒã‚‰ã€é«˜ã•ã¯ãƒ•ã‚©ãƒ³ãƒˆã«åˆã‚ã›ã¦èª¿æ•´ã•ã‚Œã‚‹ãŸã‚ã€ä¸€èˆ¬çš„ã«ã¯`:width`ã ã‘ã®ã‚¹ã‚¿ã‚¤ãƒ«ã‚’設定ã—ã¾ã™ã€‚(ãã—ã¦ã€ç¾åœ¨ã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³ã®Shoesã§ã¯ã€ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒ©ã‚¤ãƒ³ã¨ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒœãƒƒã‚¯ã‚¹ãƒ•ã‚©ãƒ³ãƒˆã¯ã©ã†ã‚„ã£ã¦ã‚‚変更ã§ãã¾ã›ã‚“。)
+
+エディットラインã«ãƒ–ロックãŒä¸Žãˆã‚‰ã‚ŒãŸã‚‰ã€`change`イベントをå—ã‘ã¨ã‚Šã¾ã™ã€‚changeブロックを利用ã—ãŸä¾‹ã«ã¤ã„ã¦ã¯[[EditBox]]ã®ãƒšãƒ¼ã‚¸ã‚’確èªã—ã¦ãã ã•ã„。実際ã«ã¯ã€ãã®ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒœãƒƒã‚¯ã‚¹ã¯ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒ©ã‚¤ãƒ³ã¨ã™ã¹ã¦åŒã˜ãƒ¡ã‚½ãƒƒãƒ‰ã‚’æŒã¡ã¾ã™ã€‚ã™ã¹ã¦ã®è¦ç´ ãŒå¿œç­”ã§ãã‚‹ã€[[Common]]メソッドã®ä¸€è¦§ã‚‚見ã¦ãã ã•ã„。
+
+=== change() { |self| ... } » self ===
+
+エディットラインã«æ–‡å­—ãŒè¿½åŠ ã•ã‚ŒãŸã‚Šå–り除ã‹ã‚Œã‚‹ãŸã³ã«ã€`chenage`ブロックãŒå‘¼ã°ã‚Œã¾ã™ã€‚ブロックã«ã¯å¤‰æ›´ã•ã‚ŒãŸã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒ©ã‚¤ãƒ³ã®ã‚ªãƒ–ジェクトã§ã‚ã‚‹`self`ãŒä¸Žãˆã‚‰ã‚Œã¾ã™ã€‚
+
+=== focus() » self ===
+
+エディットラインã«ãƒ•ã‚©ãƒ¼ã‚«ã‚¹ã‚’移動ã—ã¾ã™ã€‚ãã®ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒ©ã‚¤ãƒ³ã¯ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚Œã€ãƒ¦ãƒ¼ã‚¶ã¯ã‚¨ãƒ‡ã‚£ãƒƒãƒˆãƒ©ã‚¤ãƒ³ã«ã‚¿ã‚¤ãƒ—ã§ãã¾ã™ã€‚
+
+=== text() » self ===
+
+ç®±ã«ã‚¿ã‚¤ãƒ—ã•ã‚ŒãŸæ–‡å­—を文字列ã¨ã—ã¦è¿”ã—ã¾ã™ã€‚
+
+=== text = a string ===
+
+`a string`ã®æ–‡å­—をエディットボックスã«ä»£å…¥ã—ã¾ã™ã€‚
+
+== Image ==
+
+ç”»åƒï¼ˆimage)ã¯PNGã€JPEGã¾ãŸã¯GIFフォーマットã®ç”»åƒãƒ•ã‚¡ã‚¤ãƒ«ã§ã™ã€‚Shoesã¯ç”»åƒã‚’リサイズã¾ãŸã¯ãƒ†ã‚­ã‚¹ãƒˆã¨ã¨ã‚‚ã«ãれらをフローã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚!{:margin_left => 100}man-ele-image.png!
+
+ç”»åƒã‚’作æˆã™ã‚‹ãŸã‚ã«ã€ã‚¹ãƒ­ãƒƒãƒˆå†…部ã§`image`メソッドを利用ã—ã¾ã™ï¼š
+
+{{{
+ #!ruby
+ Shoes.app do
+ para "Nice, nice, very nice. Busy, busy, busy."
+ image "static/shoes-manual-apps.gif"
+ end
+}}}
+
+Shoesã«ä½•ã‚‰ã‹ã®ç”»åƒã‚’ロードã—ãŸã¨ãã€ãã‚Œã¯ãƒ¡ãƒ¢ãƒªã«ã‚­ãƒ£ãƒƒã‚·ãƒ¥ã•ã‚Œã¾ã™ã€‚ãã‚Œã¯ã€åŒã˜ãƒ•ã‚¡ã‚¤ãƒ«ã®ç”»åƒã®è¦ç´ ã‚’ãŸãã•ã‚“ロードã—ãŸå ´åˆã§ã‚‚ã€ãã‚Œã¯å®Ÿéš›ã«ã¯ï¼‘回ã ã‘ファイルをロードã™ã‚‹ã“ã¨ã‚’æ„味ã—ã¾ã™ã€‚
+
+webã®URLを直接利用ã™ã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ image "http://hacketyhack.heroku.com/images/logo.png"
+ end
+}}}
+
+webã‹ã‚‰ç”»åƒãŒãƒ­ãƒ¼ãƒ‰ã•ã‚ŒãŸã¨ãã€ãã‚Œã¯ãƒãƒ¼ãƒ‰ãƒ‡ã‚£ã‚¹ã‚¯ãƒ‰ãƒ©ã‚¤ãƒ–ã¨ãƒ¡ãƒ¢ãƒªã®ä¸¡æ–¹ã«ã‚­ãƒ£ãƒƒã‚·ãƒ¥ã•ã‚Œã¾ã™ã€‚ã“ã‚Œã¯ç”»åƒãŒå¤‰æ›´ã•ã‚Œãªã‘ã‚Œã°å†åº¦ãƒ€ã‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ã‚’ã™ã‚‹ã“ã¨ã‚’防止ã—ã¾ã™ã€‚(ä¸æ€è­°ã«æ€ã†å ´åˆï¼šæ­£ã«ãƒ–ラウザãŒè¡Œã†etagã®ã‚ˆã†ã«Shoesã¯å¤‰æ›´æ™‚é–“ã®è»Œè·¡ã‚’ä¿æŒã—ã¾ã™ã€‚)
+
+Shoesã¯ãƒãƒƒã‚¯ã‚°ãƒ©ã‚¦ãƒ³ãƒ‰ã§ã‚·ã‚¹ãƒ†ãƒ ã®ã‚¹ãƒ¬ãƒƒãƒ‰ã‚’使ã£ã¦ãƒªãƒ¢ãƒ¼ãƒˆã®ç”»åƒã‚‚ロードã—ã¾ã™ã€‚ãã®ãŸã‚ã€ãƒªãƒ¢ãƒ¼ãƒˆã®ç”»åƒã‚’利用ã™ã‚‹ã“ã¨ã¯Rubyを妨ã’ã‚‹ã“ã¨ã¯ãªãã€ã¾ãŸã©ã‚“ãªå¼·çƒˆãªã‚°ãƒ©ãƒ•ã‚£ã‚«ãƒ«ãªè¡¨ç¤ºã§ã‚‚進ã¿ç¶šã‘ã‚‹ã§ã—ょã†ã€‚
+
+=== full_height() » a number ===
+
+ç”»åƒå…¨ä½“ã®ãƒ”クセルã§ã®é«˜ã•ã§ã™ã€‚通常ã¯ã€ãƒ”クセルã§ã®ç”»åƒã®é«˜ã•ã‚’知るãŸã‚ã«[[Common.height]]メソッドを利用ã§ãã¾ã™ã€‚ã—ã‹ã—ç”»åƒãŒãƒªã‚µã‚¤ã‚ºã•ã‚Œã¦ã„ãŸã‚Šã‚ˆã‚Šå¤§ãã„サイズãªã©ã«ã‚¹ã‚¿ã‚¤ãƒ«ãŒè¨­å®šã•ã‚Œã¦ã„ãŸå ´åˆã¯ã€`height`ã¯å¤‰æ›´ã•ã‚ŒãŸã‚µã‚¤ã‚ºã‚’è¿”ã—ã¾ã™ã€‚
+
+`full_height`メソッドã¯ä¿å­˜ã•ã‚ŒãŸã‚ªãƒªã‚¸ãƒŠãƒ«ãƒ•ã‚¡ã‚¤ãƒ«ã®ç”»åƒã®ï¼ˆãƒ”クセルã§ã®ï¼‰é«˜ã•ã‚’与ãˆã¾ã™ã€‚
+
+=== full_width() » a number ===
+
+ç”»åƒå…¨ä½“ã®ãƒ”クセルã§ã®å¹…ã§ã™ã€‚[[Common.width]]ã§ã¯ãªãã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã‚’使ã†ç†ç”±ã®èª¬æ˜Žã«ã¤ã„ã¦ã¯[[Image.full_height]]メソッドを見ã¦ãã ã•ã„。
+
+=== path() » a string ===
+
+ç”»åƒã®URLã¾ãŸã¯ãƒ•ã‚¡ã‚¤ãƒ«åã§ã™ã€‚
+
+=== path = a string ===
+
+ファイルã¾ãŸã¯URLã‹ã‚‰ãƒ­ãƒ¼ãƒ‰ã—ã¦ã€ç”»åƒã‚’ä»–ã®ã‚‚ã®ã«å…¥ã‚Œæ›¿ãˆã¾ã™ã€‚
+
+== ListBox ==
+
+リストボックス(List box)(環境ã«ã‚ˆã£ã¦"コンボボックス(combo box)"ã¾ãŸã¯"ドロップダウンボックス(drop-down box)"ã¾ãŸã¯"セレクトボックス(select box)"ã¨ã‚‚呼ã°ã‚Œã¦ã„ã¾ã™ã€‚)ã¯ç®±ã‚’クリックã—ãŸã¨ãドロップダウンã—ã¦ã‚ªãƒ—ションãŒä¸€è¦§ã¨ã—ã¦è¡¨ç¤ºã•ã‚Œã¾ã™ã€‚!{:margin_left => 100}man-ele-listbox.png!
+
+リストボックスã¯é…列ã‹ã‚‰ã‚ªãƒ—ションをå–å¾—ã—ã¾ã™ã€‚é…列(リスト)ã®æ–‡å­—列ã¯ã€`:items`スタイルã«æ¸¡ã•ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ para "Choose a fruit:"
+ list_box :items => ["Grapes", "Pears", "Apricots"]
+ end
+}}}
+
+ãã—ã¦ã€ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã®åŸºæœ¬ã®ã‚µã‚¤ã‚ºã¯200ピクセルã®å¹…ã¨28ピクセルã®é«˜ã•ã§ã™ã€‚ã“ã®é•·ã•ã¯`width`スタイルを利用ã—ã¦èª¿æ•´ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ para "Choose a fruit:"
+ list_box :items => ["Grapes", "Pears", "Apricots"],
+ :width => 120, :choose => "Apricots" do |list|
+ @fruit.text = list.text
+ end
+
+ @fruit = para "No fruit selected"
+ end
+}}}
+
+`:width`スタイルã«ç¶šã„ã¦ã€ã“ã®ä¾‹ã§ã¯ã‚‚ã†ï¼‘ã¤ã®ä¾¿åˆ©ãªã‚ªãƒ—ションを利用ã—ã¾ã™ã€‚`:choose`オプションã¯å§‹ã‚ã‹ã‚‰ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚Œã‚‹ã¹ãアイテムをリストボックスã«æ•™ãˆã¾ã™ã€‚(箱ãŒä½œæˆã•ã‚ŒãŸå¾Œã§ã‚¢ã‚¤ãƒ†ãƒ ã‚’ãƒã‚¤ãƒ©ã‚¤ãƒˆã™ã‚‹ã«ã¯[[ListBox.choose]]メソッドもã‚ã‚Šã¾ã™ã€‚)
+
+リストボックスã¯[[ListBox.change]]イベントもæŒã£ã¦ã„ã¾ã™ã€‚次ã®ä¾‹ã§ã¯ã€ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã«ãƒ–ロックをå–り付ã‘ã¾ã—ãŸã€‚ã„ã„ã§ã™ã‹ã€ã“ã®`change`ブロックを見ã¦ãã ã•ã„。ã“ã®ãƒ–ロックã¯èª°ã‹ãŒé¸æŠžã•ã‚ŒãŸã‚¢ã‚¤ãƒ†ãƒ ã‚’変更ã™ã‚‹ãŸã³ã«å‘¼ã°ã‚Œã¾ã™ã€‚
+
+ã“れらã¯åŸºæœ¬çš„ãªã“ã¨ã§ã™ã€‚ã™ã¹ã¦ã®è¦ç´ ãŒæŒã£ã¦ã„るメソッドã®å®Œå…¨ãªä¸€è¦§ã§ã‚ã‚‹ã€[[Common]]メソッドã®ãƒšãƒ¼ã‚¸ã‚’見ã¦ãã ã•ã„。
+
+=== change() { |self| ... } » self ===
+
+誰ã‹ãŒãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã®æ–°ã—ã„オプションをãƒã‚¤ãƒ©ã‚¤ãƒˆã™ã‚‹ãŸã³ã«ï¼ˆä¾‹ãˆã°ã€ã‚¢ã‚¤ãƒ†ãƒ ã‚’クリックã™ã‚‹ã“ã¨ã«ã‚ˆã£ã¦ï¼‰`change`ブロックã¯å‘¼ã°ã‚Œã¾ã™ã€‚ブロックã«ã¯å¤‰æ›´ã•ã‚ŒãŸãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã®ã‚ªãƒ–ジェクトã§ã‚ã‚‹`self`ãŒä¸Žãˆã‚‰ã‚Œã¾ã™ã€‚
+
+=== choose(item: a string) » self ===
+
+`item`ã¨ã—ã¦ä¸Žãˆã‚‰ã‚ŒãŸæ–‡å­—列ã¨ä¸€è‡´ã™ã‚‹ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹å†…ã®ã‚ªãƒ—ションをé¸æŠžã—ã¾ã™
+
+=== focus() » self ===
+
+リストボックスã«ãƒ•ã‚©ãƒ¼ã‚«ã‚¹ã‚’移動ã—ã¾ã™ã€‚ãã®ãƒªã‚¹ãƒˆã¯ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚Œã€ãƒ¦ãƒ¼ã‚¶ãŒä¸Šã‚„下ã®çŸ¢å°ã‚­ãƒ¼ã‚’押ã—ãŸå ´åˆã€ãƒªã‚¹ãƒˆå†…ã®åˆ¥ã®ã‚ªãƒ—ションãŒé¸æŠžã•ã‚Œã¾ã™ã€‚
+
+=== items() » an array of strings ===
+
+リストボックスã«ã‚ªãƒ—ションã¨ã—ã¦ç¾åœ¨è¡¨ç¤ºã•ã‚Œã¦ã„る文字列ã®å®Œå…¨ãªä¸€è¦§ã‚’è¿”ã—ã¾ã™ã€‚
+
+=== items = an array of strings ===
+
+リストボックスã®ã‚ªãƒ—ションを新ã—ã„文字列ã®ä¸€è¦§ã§ç½®ãæ›ãˆã¾ã™ã€‚
+
+=== text() » a string ===
+
+ç¾åœ¨ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹å†…ã§ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚Œã¦è¡¨ç¤ºã•ã‚Œã¦ã„るテキストをå«ã‚€æ–‡å­—列ã§ã™ã€‚何もé¸æŠžã•ã‚Œã¦ã„ãªã„ãªã‚‰ã€`nil`ãŒå¿œç­”ã•ã‚Œã¾ã™ã€‚
+
+== Progress ==
+
+プログレスãƒãƒ¼ï¼ˆProgress bar)ã¯æ´»å‹•ãŒã©ã“ã¾ã§é€²ã‚“ã§ã„ã‚‹ã‹ã‚’表示ã—ã¾ã™ã€‚一般的ã«ã¯ã€ãƒ—ログレスãƒãƒ¼ã¯ãƒ‘ーセンテージã§ç¤ºã•ã‚Œã¾ã™ã€‚(0%ã‹ã‚‰100%ã¾ã§ï¼‰Shoesã¯0.0ã‹ã‚‰1.0ã®10進数を使ã£ã¦é€²è¡Œã‚’考ãˆã¾ã™ã€‚!{:margin_left => 100}man-ele-progress.png!
+
+シンプルãªãƒ—ログレスãƒãƒ¼ã¯200ピクセルã®å¹…ã§ã™ãŒã€é•·ãã™ã‚‹ãŸã‚ã«ï¼ˆã™ã¹ã¦ã®Shoesã®è¦ç´ ã®ã‚ˆã†ã«ï¼‰`:width`スタイルã¯åˆ©ç”¨ã§ãã¾ã›ã‚“。
+
+{{{
+ Shoes.app do
+ stack :margin => 0.1 do
+ title "Progress example"
+ @p = progress :width => 1.0
+
+ animate do |i|
+ @p.fraction = (i % 100) / 100.0
+ end
+ end
+ end
+}}}
+
+プログレスãƒãƒ¼ã‚’å«ã‚€ã€ã™ã¹ã¦ã®è¦ç´ ã«å‚™ãˆä»˜ã‘られãŸãƒ¡ã‚½ãƒƒãƒ‰ã®ä¸€è¦§ã«ã¤ã„ã¦ã¯[[Common]]メソッドã®ãƒšãƒ¼ã‚¸ã‚’見ã¦ãã ã•ã„。
+
+=== fraction() » a decimal number ===
+
+ã©ã“ã¾ã§ãƒ—ログレスãƒãƒ¼ãŒé€²ã‚“ã§ã„ã‚‹ã‹ã‚’指ã—示ã™ã€0.0ã‹ã‚‰1.0ã®10進数ã®æ•°ã‚’è¿”ã—ã¾ã™ã€‚
+
+=== fraction = a decimal number ===
+
+0.0ã‹ã‚‰1.0ã®é–“ã®10進数ã®æ•°ã§é€²è¡Œã‚’設定ã—ã¾ã™ã€‚
+
+== Radio ==
+
+ラジオボタン(Radio button)ã¯ã‚¯ãƒªãƒƒã‚¯å¯èƒ½ãªå††ã®ã‚°ãƒ«ãƒ¼ãƒ—ã§ã™ã€‚ãれをマークã™ã‚‹ã«ã¯å††ã‚’クリックã—ã¦ãã ã•ã„。ラジオボタンã¯ï¼‘度ã«ï¼‘ã¤ã ã‘マークã§ãã¾ã™ã€‚(1度ã«ï¼‘ã¤ã®ã‚ªãƒ—ションã ã‘ã—ã‹é¸æŠžã§ããªã„ã¨ã“ã‚ã¯ã€ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã«ä¼¼ã¦ã„ã¾ã™ã€‚)!{:margin_left => 100}man-ele-radio.png!
+
+ãã‚Œã§ã¯ã€ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã‚’利用ã™ã¹ãã¨ãã¨ã€ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã‚’利用ã™ã¹ãã¨ãã‚’ã©ã®ã‚ˆã†ã«ã—ã¦æ±ºå®šã—ã¾ã™ã‹ï¼Ÿãã†ã§ã™ã­ã€ãƒªã‚¹ãƒˆãƒœãƒƒã‚¯ã‚¹ã¯ãƒœãƒƒã‚¯ã‚¹ã‚’クリックã—ã¦ãƒ‰ãƒ­ãƒƒãƒ—ダウンを表示ã™ã‚‹ã“ã¨ãªã1ã¤ã®ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚ŒãŸã‚¢ã‚¤ãƒ†ãƒ ã‚’表示ã—ã¾ã™ã€‚ã—ã‹ã—ã€ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¯ã©ã‚ŒãŒãƒžãƒ¼ã‚¯ã•ã‚Œã¦ã„ã‚‹ã‹æ°—ã«ã™ã‚‹ã“ã¨ãªãã€ã™ã¹ã¦è¡¨ç¤ºã•ã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ para "Among these films, which do you prefer?\n"
+ radio; para strong("The Taste of Tea"), " by Katsuhito Ishii\n"
+ radio; para strong("Kin-Dza-Dza"), " by Georgi Danelia\n"
+ radio; para strong("Children of Heaven"), " by Majid Majidi\n"
+ end
+}}}
+
+ãれらã¯ï¼ˆãŸãã•ã‚“ã®`para`ã¨ã¨ã‚‚ã«ï¼‰åŒã˜ã‚¹ãƒ­ãƒƒãƒˆå†…ã§ä¸€ç·’ã«ã‚°ãƒ«ãƒ¼ãƒ—化ã•ã‚Œã¦ã„ã‚‹ãŸã‚ã€ï¼“ã¤ã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã‹ã‚‰ï¼‘度ã«ï¼‘ã¤ã ã‘ãŒé¸æŠžã§ãã¾ã™ã€‚
+
+ã“れらをãれら自身ã®ã‚¹ãƒ­ãƒƒãƒˆã«ç§»å‹•ã—ãŸã‚‰ã€ã“ã®ä¾‹ã¯å£Šã‚Œã¾ã™ã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ para "Among these films, which do you prefer?"
+ flow { radio; para "The Taste of Tea by Katsuhito Ishii" }
+ flow { radio; para "Kin-Dza-Dza by Georgi Danelia" }
+ flow { radio; para "Children of Heaven by Majid Majidi" }
+ end
+ end
+}}}
+
+ã—ã‹ã—ã€ã“ã‚Œã¯ä¿®æ­£ã§ãã¾ã™ã€‚
+é•ã†ã‚¹ãƒ­ãƒƒãƒˆã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã‚’一緒ã«ã‚°ãƒ«ãƒ¼ãƒ—化ã™ã‚‹ã“ã¨ãŒã§ãã€ãれらã«ã™ã¹ã¦åŒã˜ã‚°ãƒ«ãƒ¼ãƒ—åを与ãˆã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ã€‚
+
+ã“ã“ã§ã¯`:films`グループã«ãれらã™ã¹ã¦ã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã‚’グループ化ã—ã¾ã—ãŸã€‚
+
+{{{
+ #!ruby
+ Shoes.app do
+ stack do
+ para "Among these films, which do you prefer?"
+ flow do
+ radio :films
+ para "The Taste of Tea by Katsuhito Ishii"
+ end
+ flow do
+ radio :films
+ para "Kin-Dza-Dza by Georgi Danelia"
+ end
+ flow do
+ radio :films
+ para "Children of Heaven by Majid Majidi"
+ end
+ end
+ end
+}}}
+
+下ã«ã‚ã‚‹ãれらã®ä¸€è¦§ã‚’越ãˆã‚‹æ›´ãªã‚‹ãƒ¡ã‚½ãƒƒãƒ‰ã«ã¤ã„ã¦ã¯ã€[[Common]]メソッドã®ãƒšãƒ¼ã‚¸ã‚’詳ã—ã調ã¹ã¦ãã ã•ã„。ãれらã®ãƒ¡ã‚½ãƒƒãƒ‰ã‚’ã™ã¹ã¦ã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã§åŒæ§˜ã«åˆ©ç”¨ã§ãã‚‹ã‹ã‚‰ã§ã™ã€‚
+
+=== checked?() » true or false ===
+
+ラジオボタンãŒãƒã‚§ãƒƒã‚¯ã•ã‚Œã¦ã„ã‚‹ã‹ã©ã†ã‹ã‚’è¿”ã—ã¾ã™ã€‚
+ãã—ã¦ã€`true`ã®æ„味ã¯"ã¯ã„ã€ãƒã‚§ãƒƒã‚¯ã•ã‚Œã¦ã„ã¾ã™ï¼"ã§ã™ã€‚
+
+=== checked = true or false ===
+
+ラジオボタンをマークã—ãŸã‚Šãƒžãƒ¼ã‚¯ã‚’外ã—ãŸã‚Šã—ã¾ã™ã€‚例ãˆã°ã€`checked = false`を利用ã—ã¦ã€ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã‚’クリアã—ã¾ã™ã€‚
+
+=== click() { |self| ... } » self ===
+
+ラジオボタンãŒã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸã¨ãã€ãã®`click`ブロックãŒå‘¼ã°ã‚Œã¾ã™ã€‚
+ã“ã®ãƒ–ロックã¯ã€ã‚¯ãƒªãƒƒã‚¯ã•ã‚ŒãŸãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã‚’示ã™ã‚ªãƒ–ジェクトã®`self`を渡ã—ã¾ã™ã€‚
+
+ラジオボタンをマークã—ãŸã¨ãã¨ãƒžãƒ¼ã‚¯ã‚’外ã—ãŸã¨ãã®ä¸¡æ–¹ã§ã€ã‚¯ãƒªãƒƒã‚¯ãŒé€ã‚‰ã‚Œã¾ã™ã€‚
+
+=== focus() » self ===
+
+ラジオボタンã®ãƒ•ã‚©ãƒ¼ã‚«ã‚¹ã‚’移動ã—ã¾ã™ã€‚ãã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¯ãƒã‚¤ãƒ©ã‚¤ãƒˆã•ã‚Œã€ãƒ¦ãƒ¼ã‚¶ãŒã‚¨ãƒ³ã‚¿ãƒ¼ã‚­ãƒ¼ã‚’打ã¦ã°ã€ãã®ãƒ©ã‚¸ã‚ªãƒœã‚¿ãƒ³ã¯ãƒžãƒ¼ã‚¯ã•ã‚ŒãŸçŠ¶æ…‹ã¨ãƒžãƒ¼ã‚¯ã•ã‚Œã¦ã„ãªã„状態ã®é–“をトグルã—ã¾ã™ã€‚
+
+== Shape ==
+
+形状(shape)ã¯é€šå¸¸ã¯`oval`ã¨`rect`ã®ã‚ˆã†ã«ãƒ‰ãƒ­ãƒ¼ã‚¤ãƒ³ã‚°ãƒ¡ã‚½ãƒƒãƒ‰ã«ã‚ˆã£ã¦ä½œæˆã•ã‚Œã‚‹ã‚¢ã‚¦ãƒˆãƒ©ã‚¤ãƒ³ãƒ‘スã§ã™ã€‚!{:margin_left => 100}man-ele-shape.png!
+
+[[Common]]メソッドã®ãƒšãƒ¼ã‚¸ã‚’見ã¦ãã ã•ã„。形状ã¯ãれらã™ã¹ã¦ã®ãƒ¡ã‚½ãƒƒãƒ‰ã«å¿œç­”ã§ãã¾ã™ã€‚
+
+== TextBlock ==
+
+テキストブロック(TextBlock)オブジェクトã¯å˜ç‹¬ã®è¦ç´ ã«ã¨ã—ã¦å½¢æˆã•ã‚Œã‚‹ãƒ†ã‚­ã‚¹ãƒˆã®ã‚°ãƒ«ãƒ¼ãƒ—を示ã—ã¾ã™ã€‚例ãˆã°ã€å¤ªå­—ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’å«ã‚€ãƒ‘ラグラフã§ã™ã€‚リンクã¨å¤ªå­—ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’å«ã‚€ã‚­ãƒ£ãƒ—ションã§ã™ã€‚(ãã—ã¦ã€`caption`ã¯ãƒ†ã‚­ã‚¹ãƒˆãƒ–ロックã®ã‚¿ã‚¤ãƒ—ã§ã™ã€‚ã—ã‹ã—ãªãŒã‚‰ã€`link`ã¨`strong`ã¯ãƒ†ã‚­ã‚¹ãƒˆã‚¯ãƒ©ã‚¹ã®ã‚¿ã‚¤ãƒ—ã§ã™ã€‚)!{:margin_left => 100}man-ele-textblock.png!
+
+テキストブロックã®ã™ã¹ã¦ã®ç¨®é¡žã¯ã‚¿ã‚¤ãƒ—ã¯[[Element Element Creation]]ページã§ç¢ºèªã§ãã¾ã™ã€‚
+
+ * [[Element.banner]], 48ピクセルã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * [[Element.title]], 34ピクセルã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * [[Element.subtitle]], 26ピクセルã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * [[Element.tagline]], 18ピクセルã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * [[Element.caption]], 14ピクセルã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * [[Element.para]], 12ピクセルã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+ * [[Element.inscription]], 10ピクセルã®ãƒ•ã‚©ãƒ³ãƒˆã€‚
+
+=== contents() » an array of elements ===
+
+ブロック内部ã®æ•´å½¢ã•ã‚ŒãŸæ–‡å­—列ã™ã¹ã¦ã®ãƒªã‚¹ãƒˆã§ã™ã€‚
+
+=== replace(a string) ===
+
+ブロック全体ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’`a string`ã®æ–‡å­—ã§ç½®ãæ›ãˆã¾ã™ã€‚
+
+=== text() » a string ===
+
+テキストボックスã®ã™ã¹ã¦ã®æ–‡å­—ã®æ–‡å­—列を返ã—ã¾ã™ã€‚ç”»é¢ã«è¡¨ç¤ºã•ã‚Œã‚‹ã‹ã®ã‚ˆã†ã«ã€ã™ã¹ã¦ã®ã‚¹ã‚¿ã‚¤ãƒ«ã¾ãŸã¯ãƒ†ã‚­ã‚¹ãƒˆã‚¯ãƒ©ã‚¹ãŒå–り除ã‹ã‚Œã¦å®Ÿéš›ã®æ–‡å­—ã ã‘ã‚’è¿”ã—ã¾ã™ã€‚
+
+=== text = a string ===
+
+ブロック全体ã®ãƒ†ã‚­ã‚¹ãƒˆã‚’`a string`ã®æ–‡å­—ã§ç½®ãæ›ãˆã¾ã™ã€‚
+
+=== to_s() » a string ===
+
+[[TextBlock.text]]ã®ã‚¨ã‚¤ãƒªã‚¢ã‚¹ã§ã™ã€‚テキストブロックã®ã™ã¹ã¦ã®ã‚³ãƒ³ãƒ†ãƒ³ãƒ„をフラットã«ã—ãŸæ–‡å­—列を返ã—ã¾ã™ã€‚
+
+== Timers ==
+
+Shoesã¯3ã¤ã®ã‚¿ã‚¤ãƒžãƒ¼ï¼ˆtimer)クラスをæŒã£ã¦ã„ã¾ã™ï¼šã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ï¼ˆAnimation)クラスã€Everyクラスãã—ã¦ã‚¿ã‚¤ãƒžãƒ¼ï¼ˆTimer)クラスã§ã™ã€‚アニメーションã¨Everyã®ä¸¡æ–¹ã¯é–‹å§‹ã—ã¦ã‹ã‚‰ä½•åº¦ã‚‚何度も繰り替ãˆã—ã¾ã™ã€‚タイマーã¯ï¼‘度ã ã‘実行ã•ã‚Œã¾ã™ã€‚1回é™ã‚Šã®ã‚¿ã‚¤ãƒžãƒ¼ã§ã™ã€‚
+
+アニメーションã¨Everyã¯åŸºæœ¬çš„ã«ã¯åŒã˜ã‚‚ã®ã§ã™ã€‚é•ã„ã¯ã‚¢ãƒ‹ãƒ¡ãƒ¼ã‚·ãƒ§ãƒ³ã¯æ™®é€šã¯ï¼‘秒間ã«ã¨ã¦ã‚‚ãŸãã•ã‚“実行ã•ã‚Œã¾ã™ã€‚ãã—ã¦Everyã¯æ•°ç§’é–“ã”ã¨ã«å®Ÿè¡Œã•ã‚Œã‚‹ã‹ã€ã¾ã‚Œã«ã—ã‹å®Ÿè¡Œã•ã‚Œã¾ã›ã‚“。
+
+=== start() » self ===
+
+ã©ã¡ã‚‰ã®ã‚¿ã‚¤ãƒ—ã®ã‚¿ã‚¤ãƒžãƒ¼ã‚‚ãれ自身ã§è‡ªå‹•çš„ã«é–‹å§‹ã•ã‚Œã‚‹ãŸã‚ã€é€šå¸¸ã¯ã“ã‚Œã¯åˆ©ç”¨ã™ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã›ã‚“。ã—ã‹ã—ã€[[Timers.stop]]ã§ã‚¿ã‚¤ãƒžãƒ¼ã‚’æ­¢ã‚ã¦å†é–‹ã—ãŸã„å ´åˆã¯ã€ã‚‚ã¡ã‚ん:ã“れを利用ã—ã¦ãã ã•ã„ï¼
+
+=== stop() » self ===
+
+アニメーションã¾ãŸã¯ã‚¿ã‚¤ãƒžãƒ¼ã‚’中断ã—ã¾ã™ã€‚1回é™ã‚Šã®ã‚¿ã‚¤ãƒžãƒ¼ã®å ´åˆã¯ã“ã‚Œã¯æ—¢ã«å®Ÿè¡Œã•ã‚Œã¦ãŠã‚Šã€æ—¢ã«åœæ­¢ã—ã¦ã„ã‚‹ã®ã§ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã®åŠ¹æžœã¯ã‚ã‚Šã¾ã›ã‚“。
+
+=== toggle() » self ===
+
+アニメーションã¾ãŸã¯ã‚¿ã‚¤ãƒžãƒ¼ãŒåœæ­¢ã•ã‚Œã¦ã„ã‚Œã°ã€é–‹å§‹ã—ã¾ã™ã€‚æ—¢ã«å®Ÿè¡Œã•ã‚Œã¦ã„ã‚Œã°ã€åœæ­¢ã—ã¾ã™ã€‚
+
+== Video ==
+
+Shoesã¯åŸ‹ã‚è¾¼ã¿ã®QuickTimeã€Flashビデオ(FLV)ã€DivXã€Xvidãã—ã¦æ§˜ã€…ãªä»–ã®äººæ°—ã®ã‚るビデオフォーマットをサãƒãƒ¼ãƒˆã—ã¦ã„ã¾ã™ã€‚ã“ã‚Œã¯ã™ã¹ã¦ï¼’ã¤ã®é©šãã¹ãオープンソースライブラリã§ã‚ã‚‹ã€VideoLANã¨ffmpegã®ãŠã‹ã’ã§ã™ã€‚Shoes::Videoオブジェクトをセットアップã™ã‚‹ãŸã‚ã«ã‚¹ãƒ­ãƒƒãƒˆä¸Šã§`video`メソッドを利用ã—ã¦ãã ã•ã„。!{:margin_left => 100}man-ele-video.png!
+
+ビデオフォーマットã«åŠ ãˆã¦ã€MP3ã€WAVã¨Ogg Vorbisã®ã‚ˆã†ãªã€ã„ãã¤ã‹ã®ã‚ªãƒ¼ãƒ‡ã‚£ã‚ªãƒ•ã‚©ãƒ¼ãƒžãƒƒãƒˆã‚‚サãƒãƒ¼ãƒˆã•ã‚Œã¦ã„ã¾ã™ã€‚
+
+ビデオサãƒãƒ¼ãƒˆã¯Shoesã§ã¯ã‚ªãƒ—ションã§ã‚ã‚Šã€ã„ãã¤ã‹ã®ãƒ“ルドã§ã¯ãƒ“デオをサãƒãƒ¼ãƒˆã—ã¦ã„ã¾ã›ã‚“。例ãˆã°ã€PowerPCã§ã¯ãƒ“デオサãƒãƒ¼ãƒˆã¯åˆ©ç”¨ã§ãã¾ã›ã‚“。Shoesをダウンロードã—ãŸã¨ãã«ã€ãƒ“デオサãƒãƒ¼ãƒˆãŒåˆ©ç”¨ã§ããªã„ãªã‚‰ãƒ—ラットフォーム用ã®ãƒ“ルドã®ãƒ•ã‚¡ã‚¤ãƒ«åã«`novideo`ã¨è¨˜ã•ã‚Œã¦ã„ã¾ã™
+
+=== hide() » self ===
+
+ビデオをéžè¡¨ç¤ºã—ã¾ã™ã€‚æ—¢ã«å†ç”Ÿã—ã¦ã„ã‚‹ãªã‚‰ã€ãƒ“デオã¯å†ç”Ÿã‚’続ã‘ã¾ã™ã€‚ã“ã‚Œã¯ãƒ“デオã®è¡¨ç¤ºã‚’オフã«ã™ã‚‹ã ã‘ã§ã™ã€‚ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã®æœ‰åŠ›ãªä½¿ã„æ–¹ã®ï¼‘ã¤ã¯ã€MP3ã®ã‚ˆã†ãªã‚ªãƒ¼ãƒ‡ã‚£ã‚ªãƒ•ã‚¡ã‚¤ãƒ«ã‚’å†ç”Ÿã™ã‚‹ã¨ãã«ã€ãƒ“デオã®ç¯„囲を破壊ã™ã‚‹ã“ã¨ã§ã™ã€‚
+
+=== length() » a number ===
+
+ミリ秒ã§ã®ãƒ“デオ全体ã®é•·ã•ã§ã™ã€‚ビデオãŒã¾ã ãƒ­ãƒ¼ãƒ‰ã•ã‚Œã¦ã„ãªã„å ´åˆã¯nilã‚’è¿”ã—ã¾ã™ã€‚
+
+=== move(left, top) » self ===
+
+(left, top)ã¯ãƒ“デオã®å·¦ä¸Šã®è§’ã§ã‚ã‚Šã€ç‰¹å®šã®åº§æ¨™ã«ãƒ“デオを移動ã—ã¾ã™ã€‚
+
+=== pause() » self ===
+
+ビデオãŒå†ç”Ÿã•ã‚Œã¦ã„ã‚Œã°ã€ä¸€æ™‚åœæ­¢ã—ã¾ã™ã€‚
+
+=== playing?() » true of false ===
+
+ビデオをç¾åœ¨å†ç”Ÿã—ã¦ã„ã‚Œã°ã€trueã‚’è¿”ã—ã¾ã™ã€‚ã¾ãŸã¯ã€ãƒ“デオãŒä¸€æ™‚åœæ­¢ã•ã‚Œã¦ã„ãŸã‚Šåœæ­¢ã•ã‚Œã¦ã„ã‚‹å ´åˆã¯falseã§ã™ã€‚
+
+=== play() » self ===
+
+æ—¢ã«å†ç”Ÿã„ãªã‘ã‚Œã°ã€ãƒ“デオã®å†ç”Ÿã‚’開始ã—ã¾ã™ã€‚æ—¢ã«å†ç”Ÿã„ã‚‹ã®ãªã‚‰ã€ãƒ“デオã¯å§‹ã‚ã‹ã‚‰å†åº¦é–‹å§‹ã—ã¾ã™ã€‚
+
+=== position() » a decimal ===
+
+(0.0)ã‹ã‚‰ï¼ˆ1.0)ã®é–“ã®10進数ã®ï¼ˆFloatã®ï¼‰æ•°ã«ã‚ˆã‚‹ãƒ“デオã®ä½ç½®ã§ã™ã€‚例ãˆã°ã€0.5ã®Floatã®å€¤ã¯ãƒ“デオã®ä¸­é–“ã®ä½ç½®ã‚’示ã—ã¾ã™ã€‚
+
+=== position = a decimal ===
+
+Floatã®å€¤ã‚’利用ã—ã¦ãƒ“デオã®ä½ç½®ã‚’設定ã—ã¾ã™ã€‚25%ã®ä½ç½®ã«ãƒ“デオを移動ã™ã‚‹ãªã‚‰ï¼š`@video.position = 0.25`。
+
+=== remove() » self ===
+
+ビデオをスロットã‹ã‚‰å–り除ãã¾ã™ã€‚ãªãŠãƒ“デオをåœæ­¢ã—ã¾ã™ã€‚
+
+=== show() » self ===
+
+`hide()`メソッドã«ã‚ˆã£ã¦éžè¡¨ç¤ºã«ã•ã‚Œã¦ã„ãŸãªã‚‰ã€ãƒ“デオを表示ã—ã¾ã™ã€‚
+
+=== stop() » self ===
+
+ビデオãŒå†ç”Ÿã•ã‚Œã¦ã„ã‚Œã°ã€åœæ­¢ã—ã¾ã™ã€‚
+
+=== time() » a number ===
+
+ビデオã®ãƒŸãƒªç§’ã§ã®æ™‚é–“ã®ä½ç½®ã§ã™ã€‚ãã®ãŸã‚ã€ãã®ãƒ“デオãŒ10秒ã®å†ç”Ÿæ™‚é–“ãªã‚‰ã€ã“ã®ãƒ¡ã‚½ãƒƒãƒ‰ã¯10000ã®æ•°ã‚’è¿”ã—ã¾ã™
+
+=== time = a number ===
+
+ミリ秒ã®æ™‚é–“ã§ãƒ“デオã®ä½ç½®ã‚’設定ã—ã¾ã™ã€‚
+
+=== toggle() » self ===
+
+ビデオã®å¯è¦–性をトグルã—ã¾ã™ã€‚ビデオãŒè¡¨ç¤ºã•ã‚Œã¦ã„ã‚Œã°ã€`hide`ãŒå‘¼ã°ã‚Œã¾ã™ã€‚ãã†ã§ãªã‘ã‚Œã°ã€`show`ãŒå‘¼ã°ã‚Œã¾ã™ã€‚
+
+= AndSoForth =
+
+ãã®ä»–ã®æƒ…報をã“ã®å ´æ‰€ã§ã€‚
+
+== Sample Apps ==
+
+楽ã—ã¿ã¾ã—ょã†ã€‚
+
+{SAMPLES}
+
+== FAQ ==
+
+ãŠå½¹ç«‹ã¡æƒ…å ±:
+
+ * [[http://librelist.com/browser/shoes/ Shoes ML]] 気軽ã«ãƒ¡ãƒ¼ãƒªãƒ³ã‚°ãƒªã‚¹ãƒˆã¸å‚加下ã•ã„。
+ * [[http://github.com/shoes/shoes/ 最新ã®ã‚½ãƒ¼ã‚¹ã‚³ãƒ¼ãƒ‰]] 㯠GitHubã«ã‚ã‚Šã¾ã™ã€‚
+ * [[http://wiki.github.com/shoes/shoes/recentbuilds/ 最新ã®ãƒ“ルド]] ã‚ãªãŸã®ãƒ—ラットフォームã«åˆã‚ã›ã¦é¸ã‚“ã§ä¸‹ã•ã„。
+
diff --git a/static/manual.css b/static/manual.css
new file mode 100644
index 0000000..fa849a6
--- /dev/null
+++ b/static/manual.css
@@ -0,0 +1,167 @@
+body {
+ font-family: verdana, arial, sans-serif;
+ background: #DDD;
+ margin: 0; padding: 0;
+}
+a {
+ color: #378;
+ text-decoration: none;
+}
+a.hi {
+ color: #C30;
+ font-weight: bold;
+}
+a:hover {
+ text-decoration: underline;
+}
+#main {
+ width: 720px;
+ margin: 40px auto;
+}
+.sidebar {
+ position: fixed;
+ width: 120px;
+}
+#manual {
+ float: right;
+ width: 540px;
+ padding: 20px;
+ border: solid 1px #BBB;
+ background: #eee;
+ margin-bottom: 80px;
+}
+#manual li {
+ margin-bottom: 8px;
+}
+h1 {
+ font-weight: normal;
+ font-size: 42px;
+ margin-top: 0;
+}
+h2 {
+ font-weight: normal;
+ font-size: 12px;
+ color: #777;
+ margin: 0;
+}
+h4 {
+ font-weight: normal;
+ font-size: 32px;
+ margin-bottom: 0;
+}
+#manual img {
+ display: block;
+ margin: 0 auto;
+ padding: 10px;
+}
+div.method {
+ background: #333;
+ padding: 4px;
+ color: #CCC;
+}
+div.method a {
+ color: white;
+ text-decoration: none;
+ font-weight: bold;
+}
+.intro {
+ font-size: 140%;
+ border-bottom: solid 1px #BBB;
+}
+.sidebar ul {
+ list-style: none;
+ text-align: center;
+ margin: 0; padding: 10px;
+ font-size: 18px;
+}
+.sidebar ul.sub {
+ margin: 6px 0; padding: 0;
+ border-left: solid 1px #CCC;
+ border-right: solid 1px #CCC;
+}
+.sidebar ul.sub li {
+ margin: 0; padding: 0;
+ font-size: 14px;
+}
+.sidebar ul.sub a {
+ font-weight: normal;
+}
+.sidebar a {
+ color: #666;
+ font-weight: bold;
+ text-decoration: none;
+}
+.sidebar .prime {
+ display: block;
+ color: #BBB;
+ font-size: 38px;
+ margin-bottom: 20px;
+}
+.sidebar a:hover {
+ color: black;
+}
+div.color {
+ width: 31%;
+ float: left;
+ text-align: center;
+ padding: 6px;
+ font-size: 80%;
+}
+div.color h3, div.color p {
+ margin: 4px;
+}
+p.next {
+ clear: both;
+ border-top: solid 1px #BBB;
+ text-align: right;
+ font-size: 120%;
+ padding: 8px;
+}
+
+/* code highlighting */
+pre {
+ background: white;
+ padding: 8px 0;
+ border: solid 1px #ddd;
+}
+pre .comment, .ruby .comment {
+ color: #696;
+} pre .string, .ruby .string {
+ color: teal;
+}
+pre .constant, .ruby .constant {
+ font-weight: bold;
+}
+pre .symbol, .ruby .symbol {
+ color: green;
+}
+pre .keywords, .ruby .keywords {
+ color: #662;
+}
+pre .global, pre .ivar, .ruby .ivar {
+ color: #F60;
+}
+pre .brackets, .ruby .brackets {
+ color: #993;
+}
+
+/* index pages */
+#index .hibox {
+ background: white;
+ border: solid 1px #ddd;
+}
+#index .hibox p {
+ font-size: 14px;
+ margin: 8px;
+}
+#index h1 {
+ margin: 0;
+}
+#index ul {
+ list-style: none;
+ font-size: 13px;
+}
+#index ul a.hi,
+#index ul a.lo {
+ font-size: 18px;
+}
diff --git a/static/menu-corner1.png b/static/menu-corner1.png
new file mode 100644
index 0000000..b81d33a
--- /dev/null
+++ b/static/menu-corner1.png
Binary files differ
diff --git a/static/menu-corner2.png b/static/menu-corner2.png
new file mode 100644
index 0000000..f68e291
--- /dev/null
+++ b/static/menu-corner2.png
Binary files differ
diff --git a/static/menu-gray.png b/static/menu-gray.png
new file mode 100644
index 0000000..ca69e1a
--- /dev/null
+++ b/static/menu-gray.png
Binary files differ
diff --git a/static/menu-left.png b/static/menu-left.png
new file mode 100644
index 0000000..3b616ae
--- /dev/null
+++ b/static/menu-left.png
Binary files differ
diff --git a/static/menu-right.png b/static/menu-right.png
new file mode 100644
index 0000000..97629e8
--- /dev/null
+++ b/static/menu-right.png
Binary files differ
diff --git a/static/menu-top.png b/static/menu-top.png
new file mode 100644
index 0000000..2397dd3
--- /dev/null
+++ b/static/menu-top.png
Binary files differ
diff --git a/static/shoes-dmg.jpg b/static/shoes-dmg.jpg
new file mode 100644
index 0000000..12a52ca
--- /dev/null
+++ b/static/shoes-dmg.jpg
Binary files differ
diff --git a/static/shoes-icon-blue.png b/static/shoes-icon-blue.png
new file mode 100644
index 0000000..d8aa9b7
--- /dev/null
+++ b/static/shoes-icon-blue.png
Binary files differ
diff --git a/static/shoes-icon.png b/static/shoes-icon.png
new file mode 100644
index 0000000..d533f7b
--- /dev/null
+++ b/static/shoes-icon.png
Binary files differ
diff --git a/static/shoes-manual-apps.gif b/static/shoes-manual-apps.gif
new file mode 100644
index 0000000..3ff2e9e
--- /dev/null
+++ b/static/shoes-manual-apps.gif
Binary files differ
diff --git a/static/shoes_main_window.png b/static/shoes_main_window.png
new file mode 100644
index 0000000..4efc3ee
--- /dev/null
+++ b/static/shoes_main_window.png
Binary files differ
diff --git a/static/stripe.png b/static/stripe.png
new file mode 100644
index 0000000..6acb327
--- /dev/null
+++ b/static/stripe.png
Binary files differ
diff --git a/static/stubs/blank.exe b/static/stubs/blank.exe
new file mode 100644
index 0000000..73acc7c
--- /dev/null
+++ b/static/stubs/blank.exe
Binary files differ
diff --git a/static/stubs/blank.hfz b/static/stubs/blank.hfz
new file mode 100644
index 0000000..0ee6d4f
--- /dev/null
+++ b/static/stubs/blank.hfz
Binary files differ
diff --git a/static/stubs/blank.run b/static/stubs/blank.run
new file mode 100644
index 0000000..e2c4673
--- /dev/null
+++ b/static/stubs/blank.run
@@ -0,0 +1,375 @@
+#!/bin/sh
+# This script was generated using Makeself 2.1.4
+FULLSIZE=#{FULLSIZE}
+CRCsum="#{CRC}"
+MD5="#{MD5}"
+TMPROOT=${TMPDIR:=/tmp}
+
+label="#{LABEL}"
+script="./sh-install"
+scriptargs=""
+targetdir="dist"
+filesizes="#{SIZE}"
+keep=n
+
+print_cmd_arg=""
+if type printf > /dev/null; then
+ print_cmd="printf"
+elif test -x /usr/ucb/echo; then
+ print_cmd="/usr/ucb/echo"
+else
+ print_cmd="echo"
+fi
+
+unset CDPATH
+
+MS_Printf()
+{
+ $print_cmd $print_cmd_arg "$1"
+}
+
+MS_Progress()
+{
+ while read a; do
+ MS_Printf .
+ done
+}
+
+MS_dd()
+{
+ blocks=`expr $3 / 1024`
+ bytes=`expr $3 % 1024`
+ dd if="$1" ibs=$2 skip=1 obs=1024 conv=sync 2> /dev/null | \
+ { test $blocks -gt 0 && dd ibs=1024 obs=1024 count=$blocks ; \
+ test $bytes -gt 0 && dd ibs=1 obs=1024 count=$bytes ; } 2> /dev/null
+}
+
+MS_Help()
+{
+ cat << EOH >&2
+Makeself version 2.1.4
+ 1) Getting help or info about $0 :
+ $0 --help Print this message
+ $0 --info Print embedded info : title, default target directory, embedded script ...
+ $0 --lsm Print embedded lsm entry (or no LSM)
+ $0 --list Print the list of files in the archive
+ $0 --check Checks integrity of the archive
+
+ 2) Running $0 :
+ $0 [options] [--] [additional arguments to embedded script]
+ with following options (in that order)
+ --confirm Ask before running embedded script
+ --noexec Do not run embedded script
+ --keep Do not erase target directory after running
+ the embedded script
+ --nox11 Do not spawn an xterm
+ --nochown Do not give the extracted files to the current user
+ --target NewDirectory Extract in NewDirectory
+ --tar arg1 [arg2 ...] Access the contents of the archive through the tar command
+ -- Following arguments will be passed to the embedded script
+EOH
+}
+
+MS_Check()
+{
+ OLD_PATH=$PATH
+ PATH=${GUESS_MD5_PATH:-"$OLD_PATH:/bin:/usr/bin:/sbin:/usr/local/ssl/bin:/usr/local/bin:/opt/openssl/bin"}
+ MD5_PATH=`exec 2>&-; which md5sum || type md5sum | cut -c 11-`
+ MD5_PATH=${MD5_PATH:-`exec 2>&-; which md5 || type md5 | cut -c 8-`}
+ PATH=$OLD_PATH
+ MS_Printf "Verifying archive integrity..."
+ offset=`head -n 375 "$1" | wc -c | tr -d " "`
+ verb=$2
+ i=1
+ for s in $filesizes
+ do
+ crc=`echo $CRCsum | cut -d" " -f$i`
+ if test -x "$MD5_PATH"; then
+ md5=`echo $MD5 | cut -d" " -f$i`
+ if test $md5 = "00000000000000000000000000000000"; then
+ test x$verb = xy && echo " $1 does not contain an embedded MD5 checksum." >&2
+ else
+ md5sum=`MS_dd "$1" $offset $s | "$MD5_PATH" | cut -b-32`;
+ if test "$md5sum" != "$md5"; then
+ echo "Error in MD5 checksums: $md5sum is different from $md5" >&2
+ exit 2
+ else
+ test x$verb = xy && MS_Printf " MD5 checksums are OK." >&2
+ fi
+ crc="0000000000"; verb=n
+ fi
+ fi
+ if test $crc = "0000000000"; then
+ test x$verb = xy && echo " $1 does not contain a CRC checksum." >&2
+ else
+ sum1=`MS_dd "$1" $offset $s | CMD_ENV=xpg4 cksum | awk '{print $1}'`
+ if test "$sum1" = "$crc"; then
+ test x$verb = xy && MS_Printf " CRC checksums are OK." >&2
+ else
+ echo "Error in checksums: $sum1 is different from $crc"
+ exit 2;
+ fi
+ fi
+ i=`expr $i + 1`
+ offset=`expr $offset + $s`
+ done
+ echo " All good."
+}
+
+UnTAR()
+{
+ tar $1vf - 2>&1 || { echo Extraction failed. > /dev/tty; kill -15 $$; }
+}
+
+finish=true
+xterm_loop=
+nox11=n
+copy=none
+ownership=y
+verbose=n
+
+initargs="$@"
+
+while true
+do
+ case "$1" in
+ -h | --help)
+ MS_Help
+ exit 0
+ ;;
+ --info)
+ echo Identification: "$label"
+ echo Target directory: "$targetdir"
+ echo Uncompressed size: #{RAWSIZE} KB
+ echo Compression: gzip
+ echo Date of packaging: #{TIME}
+ echo Built with Makeself version 2.1.4 on
+ echo Build command was: "/usr/bin/makeself \\
+ \"dist\" \\
+ \"pkg/#{NAME}.run\" \\
+ \"#{LABEL}\" \\
+ \"./sh-install\""
+ if test x$script != x; then
+ echo Script run after extraction:
+ echo " " $script $scriptargs
+ fi
+ if test x"" = xcopy; then
+ echo "Archive will copy itself to a temporary location"
+ fi
+ if test x"n" = xy; then
+ echo "directory $targetdir is permanent"
+ else
+ echo "$targetdir will be removed after extraction"
+ fi
+ exit 0
+ ;;
+ --dumpconf)
+ echo LABEL=\"$label\"
+ echo SCRIPT=\"$script\"
+ echo SCRIPTARGS=\"$scriptargs\"
+ echo archdirname=\"dist\"
+ echo KEEP=n
+ echo COMPRESS=gzip
+ echo filesizes=\"$filesizes\"
+ echo CRCsum=\"$CRCsum\"
+ echo MD5sum=\"$MD5\"
+ echo OLDUSIZE=#{RAWSIZE}
+ echo OLDSKIP=376
+ exit 0
+ ;;
+ --lsm)
+cat << EOLSM
+No LSM.
+EOLSM
+ exit 0
+ ;;
+ --list)
+ echo Target directory: $targetdir
+ offset=`head -n 375 "$0" | wc -c | tr -d " "`
+ for s in $filesizes
+ do
+ MS_dd "$0" $offset $s | eval "gzip -cd" | UnTAR t
+ offset=`expr $offset + $s`
+ done
+ exit 0
+ ;;
+ --tar)
+ offset=`head -n 375 "$0" | wc -c | tr -d " "`
+ arg1="$2"
+ shift 2
+ for s in $filesizes
+ do
+ MS_dd "$0" $offset $s | eval "gzip -cd" | tar "$arg1" - $*
+ offset=`expr $offset + $s`
+ done
+ exit 0
+ ;;
+ --check)
+ MS_Check "$0" y
+ exit 0
+ ;;
+ --confirm)
+ verbose=y
+ shift
+ ;;
+ --noexec)
+ script=""
+ shift
+ ;;
+ --keep)
+ keep=y
+ shift
+ ;;
+ --target)
+ keep=y
+ targetdir=${2:-.}
+ shift 2
+ ;;
+ --nox11)
+ nox11=y
+ shift
+ ;;
+ --nochown)
+ ownership=n
+ shift
+ ;;
+ --xwin)
+ finish="echo Press Return to close this window...; read junk"
+ xterm_loop=1
+ shift
+ ;;
+ --phase2)
+ copy=phase2
+ shift
+ ;;
+ --)
+ shift
+ break ;;
+ -*)
+ echo Unrecognized flag : "$1" >&2
+ MS_Help
+ exit 1
+ ;;
+ *)
+ break ;;
+ esac
+done
+
+case "$copy" in
+copy)
+ tmpdir=$TMPROOT/makeself.$RANDOM.`date +"%y%m%d%H%M%S"`.$$
+ mkdir "$tmpdir" || {
+ echo "Could not create temporary directory $tmpdir" >&2
+ exit 1
+ }
+ SCRIPT_COPY="$tmpdir/makeself"
+ echo "Copying to a temporary location..." >&2
+ cp "$0" "$SCRIPT_COPY"
+ chmod +x "$SCRIPT_COPY"
+ cd "$TMPROOT"
+ exec "$SCRIPT_COPY" --phase2
+ ;;
+phase2)
+ finish="$finish ; rm -rf `dirname $0`"
+ ;;
+esac
+
+if test "$nox11" = "n"; then
+ if tty -s; then # Do we have a terminal?
+ :
+ else
+ if test x"$DISPLAY" != x -a x"$xterm_loop" = x; then # No, but do we have X?
+ if xset q > /dev/null 2>&1; then # Check for valid DISPLAY variable
+ GUESS_XTERMS="xterm rxvt dtterm eterm Eterm kvt konsole aterm"
+ for a in $GUESS_XTERMS; do
+ if type $a >/dev/null 2>&1; then
+ XTERM=$a
+ break
+ fi
+ done
+ chmod a+x $0 || echo Please add execution rights on $0
+ if test `echo "$0" | cut -c1` = "/"; then # Spawn a terminal!
+ exec $XTERM -title "$label" -e "$0" --xwin "$initargs"
+ else
+ exec $XTERM -title "$label" -e "./$0" --xwin "$initargs"
+ fi
+ fi
+ fi
+ fi
+fi
+
+if test "$targetdir" = "."; then
+ tmpdir="."
+else
+ if test "$keep" = y; then
+ echo "Creating directory $targetdir" >&2
+ tmpdir="$targetdir"
+ dashp="-p"
+ else
+ tmpdir="$TMPROOT/selfgz$$$RANDOM"
+ dashp=""
+ fi
+ mkdir $dashp $tmpdir || {
+ echo 'Cannot create target directory' $tmpdir >&2
+ echo 'You should try option --target OtherDirectory' >&2
+ eval $finish
+ exit 1
+ }
+fi
+
+location="`pwd`"
+if test x$SETUP_NOCHECK != x1; then
+ MS_Check "$0"
+fi
+offset=`head -n 375 "$0" | wc -c | tr -d " "`
+
+if test x"$verbose" = xy; then
+ MS_Printf "About to extract #{RAWSIZE} KB in $tmpdir ... Proceed ? [Y/n] "
+ read yn
+ if test x"$yn" = xn; then
+ eval $finish; exit 1
+ fi
+fi
+
+MS_Printf "Uncompressing $label"
+res=3
+if test "$keep" = n; then
+ trap 'echo Signal caught, cleaning up >&2; cd $TMPROOT; /bin/rm -rf $tmpdir; eval $finish; exit 15' 1 2 3 15
+fi
+
+for s in $filesizes
+do
+ if MS_dd "$0" $offset $s | eval "gzip -cd" | ( cd "$tmpdir"; UnTAR x ) | MS_Progress; then
+ if test x"$ownership" = xy; then
+ (PATH=/usr/xpg4/bin:$PATH; cd "$tmpdir"; chown -R `id -u` .; chgrp -R `id -g` .)
+ fi
+ else
+ echo
+ echo "Unable to decompress $0" >&2
+ eval $finish; exit 1
+ fi
+ offset=`expr $offset + $s`
+done
+echo
+
+cd "$tmpdir"
+res=0
+if test x"$script" != x; then
+ if test x"$verbose" = xy; then
+ MS_Printf "OK to execute: $script $scriptargs $* ? [Y/n] "
+ read yn
+ if test x"$yn" = x -o x"$yn" = xy -o x"$yn" = xY; then
+ eval $script $scriptargs $*; res=$?;
+ fi
+ else
+ eval $script $scriptargs $*; res=$?
+ fi
+ if test $res -ne 0; then
+ test x"$verbose" = xy && echo "The program '$script' returned an error code ($res)" >&2
+ fi
+fi
+if test "$keep" = n; then
+ cd $TMPROOT
+ /bin/rm -rf $tmpdir
+fi
+eval $finish; exit $res
diff --git a/static/stubs/cocoa-install b/static/stubs/cocoa-install
new file mode 100644
index 0000000..267821b
--- /dev/null
+++ b/static/stubs/cocoa-install
Binary files differ
diff --git a/static/stubs/sh-install b/static/stubs/sh-install
new file mode 100644
index 0000000..098b3f0
--- /dev/null
+++ b/static/stubs/sh-install
@@ -0,0 +1,49 @@
+#!/bin/sh
+shoes_host="http://shoes.heroku.com"
+shoes_url="$shoes_host/pkg/policeman/linux/shoes-novideo"
+this_dir=`pwd`
+
+shoes="$(which shoes)"
+if [ -x "$this_dir/shoes" ] ; then
+ shoes="$this_dir/shoes"
+fi
+if [ -z "$shoes" ] ; then
+ shoes="$HOME/.shoes/shoes.run"
+fi
+
+if [ ! -x "$shoes" ] ; then
+ echo "Downloading Shoes... "
+
+ # First, try wget.
+ wget="wget -q -O -"
+ wdl="wget -q"
+ shoes_pkg="$($wget "$shoes_url" 2>/dev/null)"
+
+ if [ -z "$shoes_pkg" ] ; then
+ # Then, try curl.
+ wget="curl -s"
+ wdl="curl -s -O"
+ shoes_pkg="$($wget "$shoes_url" 2>/dev/null)"
+
+ if [ -z "$shoes_pkg" ] ; then
+ # Lastly, try bsd fetch.
+ wget="fetch -q -o -"
+ wdl="fetch -q"
+ shoes_pkg="$($wget "$shoes_url" 2>/dev/null)"
+
+ if [ -z "$shoes_pkg" ] ; then
+ echo "sorry, couldn't find wget or curl."
+ exit 1;
+ fi
+ fi
+ fi
+
+ #shoes_run="$shoes_host$shoes_pkg"
+ shoes_run="$shoes_pkg"
+ echo "Fetching $shoes_run..."
+ mkdir -p $HOME/.shoes
+ eval $wget "$shoes_run" > $shoes
+ chmod 755 $shoes
+fi
+
+eval $shoes -- "$this_dir/#{SCRIPT}"
diff --git a/static/stubs/shoes-stub-inject.exe b/static/stubs/shoes-stub-inject.exe
new file mode 100644
index 0000000..afcb796
--- /dev/null
+++ b/static/stubs/shoes-stub-inject.exe
Binary files differ
diff --git a/static/stubs/shoes-stub.exe b/static/stubs/shoes-stub.exe
new file mode 100644
index 0000000..d803dc1
--- /dev/null
+++ b/static/stubs/shoes-stub.exe
Binary files differ
diff --git a/static/tutor-back.png b/static/tutor-back.png
new file mode 100644
index 0000000..0905c7a
--- /dev/null
+++ b/static/tutor-back.png
Binary files differ