セキュリティ無視でDSLをそのまま入力するフォーム追加

diff --git a/README.org b/README.org
index 734366f..3364351 100644
--- a/README.org
+++ b/README.org
@@ -3,7 +3,7 @@

 * 将棋のプログラム

-  [[https://raw.github.com/akicho8/bushido/master/brawser/demo1.png]]
+  [[https://raw.github.com/akicho8/bushido/master/brawser/demo2.png]]

   DEMO → http://fast-plateau-7460.herokuapp.com/

@@ -253,7 +253,7 @@
     : | 香 桂 ・ ・ ・ ・ 飛 ・ ・|
     : +---------------------------+
     : EOT
-    : BaseFormat.board_parse_as_str(board)
+    : BaseFormat.board_parse(board)
     : # => {
     :   :white => {
     :     "８一桂", "２一桂", "１一香", "９二飛", "５三歩", "３三玉", "３四桂", "１四金",
@@ -536,6 +536,7 @@

 ** _TODO_

+   - :dsl => KifuDsl.define("文字列") とも書けるようにする。evalのように。
    - フォームで入力した棋譜の再生
    - 囲い一覧をWEBブラウザで確認できるようにする
    - XtraPattern を Rails の routing.rb を参考に綺麗に書けるようにする
diff --git a/brawser/brawser.rb b/brawser/brawser.rb
index 39f2ded..dbdb121 100644
--- a/brawser/brawser.rb
+++ b/brawser/brawser.rb
@@ -114,6 +114,51 @@ class Brawser < Sinatra::Base
     haml :tactics
   end

+  get "/kif_form" do
+    @pattern_body_default = %(
+board <<-BOARD
+  ９ ８ ７ ６ ５ ４ ３ ２ １
++---------------------------+
+| ・ ・ ・ ・ ・ ・ ・v桂v香|一
+| ・ ・ ・ ・ ・v銀v金v角 ・|二
+| ・ ・ ・ ・ ・v歩v歩 ・v歩|三
+| ・ ・ ・ ・ ・ ・ ・ ・ ・|四
+| ・ ・ ・ ・ ・ ・ ・ ・ ・|五
+| ・ ・ ・ ・ ・ ・ ・ ・ ・|六
+| ・ ・ ・ ・ ・ ・ 歩 ・ 歩|七
+| ・ ・ ・ ・ ・ ・ ・ 飛 ・|八
+| ・ ・ ・ ・ ・ ・ ・ 桂 香|九
++---------------------------+
+BOARD
+pieces "先手" => "歩1"
+auto_flushing {
+  push {
+    comment "成功するパターン"
+    mov "▲２四歩"
+    mov "△３一角"
+    mov "▲２三歩成"
+  }
+  push {
+    comment "最初に戻って失敗するパターン"
+    mov "▲２三歩"
+    mov "△３一角"
+    mov "▲２二歩成"
+    mov "△２二角"
+  }
+})
+    if params[:kif_title].present?
+      @pattern = Bushido::XtraPattern.new({
+          :title => params[:kif_title],
+          :dsl => Bushido::KifuDsl.define(params){|params|eval(params[:kif_body])},
+        })
+      @frames = Bushido::HybridSequencer.execute(@pattern)
+    else
+      params[:kif_title] ||= "垂らしの歩"
+      params[:kif_body] ||= @pattern_body_default
+    end
+    haml :kif_form
+  end
+
   get "/tactics_and_enclosure" do
     load "bushido/board_libs.rb"
     haml :tactics_and_enclosure
diff --git a/brawser/demo2.png b/brawser/demo2.png
new file mode 100644
index 0000000..66e1e66
Binary files /dev/null and b/brawser/demo2.png differ
diff --git a/brawser/views/kif_form.haml b/brawser/views/kif_form.haml
new file mode 100644
index 0000000..aa0381c
--- /dev/null
+++ b/brawser/views/kif_form.haml
@@ -0,0 +1,43 @@
+-# .well
+-#   - Bushido::XtraPattern.each.with_index do |pattern, id|
+-#     %a{:href => "/tactics?id=#{id}"}= "【#{pattern[:title]}】"
+
+%form{action: "/kif_form", method: "get", :class => "form-vertical"}
+  .control-group
+    %label{:class => "control-label"}= "タイトル"
+    .controls
+      %input{type: "text", :name => "kif_title", :value => params[:kif_title], :class => "input-xxlarge"}
+  .control-group
+    %label{:class => "control-label"}= "棋譜データ"
+    .controls
+      %tt
+        %textarea{:name => "kif_body", :class => "input-xxlarge", :rows => [10, params[:kif_body].to_s.lines.count].max, :style => "font-family:monospace"}
+          = preserve do
+            = params[:kif_body]
+  .form-actions
+    %input{type: "submit", value: "実行", :class => "btn btn-primary"}
+
+- if @pattern
+  %p
+    %strong= @pattern[:title]
+  - if @pattern[:url]
+    %p
+      %a{href: @pattern[:url]}
+        = @pattern[:url]
+  - if @pattern[:comment]
+    %p
+      = @pattern[:comment]
+  %hr
+  - @frames.each do |mediator|
+    - if v = mediator.variables[:comment]
+      .alert
+        = v
+    - if mediator.variables[:hide_human_kif_logs]
+    - else
+      %p= mediator.human_kif_logs.join(" ")
+    - if mediator.player_at(:white).pieces.present?
+      .pieces= mediator.player_at(:white).to_s_pieces
+    = MediatorDecorator.new(mediator).to_html_board(:small)
+    - if mediator.player_at(:black).pieces.present?
+      .pieces= mediator.player_at(:black).to_s_pieces
+    %hr
diff --git a/brawser/views/layout.haml b/brawser/views/layout.haml
index cce3dfd..458d19b 100644
--- a/brawser/views/layout.haml
+++ b/brawser/views/layout.haml
@@ -28,6 +28,8 @@
               %li
                 %a{href:"/tactics_and_enclosure"}= "囲い戦術"
               %li
+                %a{href:"/kif_form"}= "フォーム"
+              %li
                 %a{href:"/learn_board_points"}= "座標"
     .container
       - if @error
diff --git a/examples/0120_example.rb b/examples/0120_example.rb
index 678afaf..601d1f8 100644
--- a/examples/0120_example.rb
+++ b/examples/0120_example.rb
@@ -15,4 +15,5 @@ pp Piece["飛"].to_h
 # >>  :basic_series_vectors=>[nil, [0, -1], nil, [-1, 0], [1, 0], nil, [0, 1], nil],
 # >>  :promoted_step_vectors=>
 # >>   [[-1, -1], [0, -1], [1, -1], [-1, 0], nil, [1, 0], [-1, 1], [0, 1], [1, 1]],
-# >>  :promoted_series_vectors=>[nil, [0, -1], nil, [-1, 0], [1, 0], nil, [0, 1], nil]}
+# >>  :promoted_series_vectors=>
+# >>   [nil, [0, -1], nil, [-1, 0], [1, 0], nil, [0, 1], nil]}
diff --git a/examples/0140_example.rb b/examples/0140_example.rb
index 11f6edc..f4b0e69 100644
--- a/examples/0140_example.rb
+++ b/examples/0140_example.rb
@@ -12,5 +12,16 @@ mediator.piece_plot
 }
 puts mediator.board
 pp mediator.simple_kif_logs
-# ~> -:4:in `require_relative': cannot infer basepath (LoadError)
-# ~> 	from -:4:in `<main>'
+# >>   ９ ８ ７ ６ ５ ４ ３ ２ １
+# >> +---------------------------+
+# >> |v香v桂v銀v金v玉 ・v銀v桂v香|一
+# >> | ・v飛 ・ ・ ・ ・v金v角 ・|二
+# >> |v歩 ・v歩v歩v歩v歩v歩v歩v歩|三
+# >> | ・v歩 ・ ・ ・ ・ ・ ・ ・|四
+# >> | ・ ・ ・ ・ ・ ・ ・ ・ ・|五
+# >> | ・ ・ 歩 ・ ・ ・ ・ ・ ・|六
+# >> | 歩 歩 ・ 歩 歩 歩 歩 歩 歩|七
+# >> | ・ 角 金 ・ ・ ・ ・ 飛 ・|八
+# >> | 香 桂 銀 ・ 玉 金 銀 桂 香|九
+# >> +---------------------------+
+# >> ["▲7六歩(77)", "▽8四歩(83)", "▲7八金(69)", "▽3二金(41)"]
diff --git a/examples/0150_example.rb b/examples/0150_example.rb
index 1568386..3e3bd32 100644
--- a/examples/0150_example.rb
+++ b/examples/0150_example.rb
@@ -8,7 +8,7 @@ pp info.header

 mediator = Mediator.start
 mediator.piece_plot
-info.move_infos.each{|info|
+info.move_infos.each{|info| # !> shadowing outer local variable - info
   mediator.execute(info[:input])
 }
 puts mediator.inspect
diff --git a/examples/0160_example.rb b/examples/0160_example.rb
index 73f5526..5611834 100644
--- a/examples/0160_example.rb
+++ b/examples/0160_example.rb
@@ -9,7 +9,7 @@ board = <<-EOT
 | ・ ・|
 +------+
 EOT
-BaseFormat.board_parse_as_str(board) # => {:black=>[], :white=>["２一桂", "１一香"]}
+BaseFormat.board_parse(board) # => {#<Bushido::Location:0x007f86c2a64e38 @attributes={:key=>:black, :mark=>"▲", :other_marks=>["b", "▼", "^"], :name=>"先手", :varrow=>" ", :index=>0}>=>[], #<Bushido::Location:0x007f86c2a64a28 @attributes={:key=>:white, :mark=>"▽", :other_marks=>["w", "△"], :name=>"後手", :varrow=>"v", :index=>1}>=>[{:piece=><Bushido::Piece::Knight:70108381891140 桂 knight>, :promoted=>false, :point=>#<Bushido::Point:70108381925140 "2一">}, {:piece=><Bushido::Piece::Lance:70108381884460 香 lance>, :promoted=>false, :point=>#<Bushido::Point:70108381944780 "1一">}]}

 board = <<-EOT
 +---------------------------+
@@ -24,4 +24,4 @@ board = <<-EOT
 | 香 桂 ・ ・ ・ ・ 飛 ・ ・|
 +---------------------------+ # !> method redefined; discarding old promoted=
 EOT
-BaseFormat.board_parse_as_str(board) # => {:black=>["５一馬", "４二と", "６三全", "４五銀", "７六歩", "８七歩", "７七銀", "９八歩", "７八玉", "６八香", "１八香", "９九香", "８九桂", "３九飛"], :white=>["８一桂", "２一桂", "１一香", "９二飛", "５三歩", "３三玉", "３四桂", "１四金", "８五歩", "３五歩", "２五歩", "１五歩", "９六歩", "６六角", "６七歩", "５七と"]}
+BaseFormat.board_parse(board) # => {#<Bushido::Location:0x007f86c2a64e38 @attributes={:key=>:black, :mark=>"▲", :other_marks=>["b", "▼", "^"], :name=>"先手", :varrow=>" ", :index=>0}>=>[{:piece=><Bushido::Piece::Bishop:70108381861260 角 bishop>, :promoted=>true, :point=>#<Bushido::Point:70108382017240 "5一">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>true, :point=>#<Bushido::Point:70108382126080 "4二">}, {:piece=><Bushido::Piece::Silver:70108381887560 銀 silver>, :promoted=>true, :point=>#<Bushido::Point:70108382147680 "6三">}, {:piece=><Bushido::Piece::Silver:70108381887560 銀 silver>, :promoted=>false, :point=>#<Bushido::Point:70108382326060 "4五">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382345800 "7六">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382302280 "8七">}, {:piece=><Bushido::Piece::Silver:70108381887560 銀 silver>, :promoted=>false, :point=>#<Bushido::Point:70108382281800 "7七">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382193560 "9八">}, {:piece=><Bushido::Piece::King:70108381897460 玉 king>, :promoted=>false, :point=>#<Bushido::Point:70108382174680 "7八">}, {:piece=><Bushido::Piece::Lance:70108381884460 香 lance>, :promoted=>false, :point=>#<Bushido::Point:70108382148960 "6八">}, {:piece=><Bushido::Piece::Lance:70108381884460 香 lance>, :promoted=>false, :point=>#<Bushido::Point:70108382124060 "1八">}, {:piece=><Bushido::Piece::Lance:70108381884460 香 lance>, :promoted=>false, :point=>#<Bushido::Point:70108382102340 "9九">}, {:piece=><Bushido::Piece::Knight:70108381891140 桂 knight>, :promoted=>false, :point=>#<Bushido::Point:70108382075280 "8九">}, {:piece=><Bushido::Piece::Rook:70108381873020 飛 rook>, :promoted=>false, :point=>#<Bushido::Point:70108382049900 "3九">}], #<Bushido::Location:0x007f86c2a64a28 @attributes={:key=>:white, :mark=>"▽", :other_marks=>["w", "△"], :name=>"後手", :varrow=>"v", :index=>1}>=>[{:piece=><Bushido::Piece::Knight:70108381891140 桂 knight>, :promoted=>false, :point=>#<Bushido::Point:70108381992640 "8一">}, {:piece=><Bushido::Piece::Knight:70108381891140 桂 knight>, :promoted=>false, :point=>#<Bushido::Point:70108382051240 "2一">}, {:piece=><Bushido::Piece::Lance:70108381884460 香 lance>, :promoted=>false, :point=>#<Bushido::Point:70108382075160 "1一">}, {:piece=><Bushido::Piece::Rook:70108381873020 飛 rook>, :promoted=>false, :point=>#<Bushido::Point:70108382098140 "9二">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382194800 "5三">}, {:piece=><Bushido::Piece::King:70108381897460 玉 king>, :promoted=>false, :point=>#<Bushido::Point:70108382206020 "3三">}, {:piece=><Bushido::Piece::Knight:70108381891140 桂 knight>, :promoted=>false, :point=>#<Bushido::Point:70108382241720 "3四">}, {:piece=><Bushido::Piece::Gold:70108381900980 金 gold>, :promoted=>false, :point=>#<Bushido::Point:70108382277540 "1四">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382297340 "8五">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382335700 "3五">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382365660 "2五">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382378840 "1五">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382363700 "9六">}, {:piece=><Bushido::Piece::Bishop:70108381861260 角 bishop>, :promoted=>false, :point=>#<Bushido::Point:70108382324640 "6六">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>false, :point=>#<Bushido::Point:70108382252140 "6七">}, {:piece=><Bushido::Piece::Pawn:70108381868080 歩 pawn>, :promoted=>true, :point=>#<Bushido::Point:70108382229180 "5七">}]}
diff --git a/examples/0210_example.rb b/examples/0210_example.rb
index d9ce6bd..0c522e7 100644
--- a/examples/0210_example.rb
+++ b/examples/0210_example.rb
@@ -22,7 +22,7 @@ mediator.execute("３二角")
 mediator.execute("２三銀")
 mediator.execute("４三銀")
 mediator.execute("１二銀")
-mediator.execute("同金")
+mediator.execute("同金") # !> method redefined; discarding old promoted=
 mediator.execute("同角")
 p mediator
 # >> 1手目: ▲先手番
diff --git a/lib/bushido/base_format.rb b/lib/bushido/base_format.rb
index 44643e2..e3dda08 100644
--- a/lib/bushido/base_format.rb
+++ b/lib/bushido/base_format.rb
@@ -14,7 +14,7 @@ module Bushido
     end

     def self.board_format?(source)
-      normalized_source(source).match(/^[\+\|]/)
+      normalized_source(source).match(/^\s*[\+\|]/)
     end

     # ほぼ標準の柿木フォーマットのテーブルの読み取り
@@ -34,8 +34,8 @@ module Bushido
     #   +---------------------------+
     #   "
     #
-    #   Bushido::BaseFormat.board_parse_as_str(str) # => {:white => ["４二玉"], :black => []}
-    #   Bushido::BaseFormat.board_parse(str)        # => {:white => [<MiniSoldier ...>], :black => []}
+    #   Bushido::BaseFormat.board_parse(str) # => {:white => ["４二玉"], :black => []}
+    #   Bushido::BaseFormat.board_parse(str) # => {:white => [<MiniSoldier ...>], :black => []}
     #
     def self.board_parse(source)
       lines = normalized_source(source).strip.lines.to_a
@@ -60,7 +60,7 @@ module Bushido
       inlines = mds.collect{|v|v[:inline]}

       players = Location.inject({}){|h, location|h.merge(location => [])}
-      inlines.each_with_index{|s, y|
+      inlines.each_with_index{|s, y| # !> shadowing outer local variable - s
         s.scan(/(.)(\S|\s{2})/).each_with_index{|(prefix, piece), x|
           unless piece == "・" || piece.strip == ""
             unless Piece.names.include?(piece)
diff --git a/lib/bushido/contrib/other_files/0005_合わせの歩で飛車先突破.rb b/lib/bushido/contrib/other_files/0005_合わせの歩で飛車先突破.rb
new file mode 100644
index 0000000..8213491
--- /dev/null
+++ b/lib/bushido/contrib/other_files/0005_合わせの歩で飛車先突破.rb
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+
+require_relative "../../../bushido"
+
+module Bushido
+  XtraPattern.define do
+    [
+      {
+        :title => Pathname(__FILE__).basename(".*"),
+        :dsl => KifuDsl.define do
+          board <<-BOARD
+  ９ ８ ７ ６ ５ ４ ３ ２ １
++---------------------------+
+| ・ ・ ・ ・ ・ ・ ・v桂v香|一
+| ・ ・ ・ ・ ・ ・v金v歩 ・|二
+| ・ ・ ・ ・ ・v歩v歩 ・v歩|三
+| ・ ・ ・ ・ ・ ・ ・ 銀 ・|四
+| ・ ・ ・ ・ ・ ・ ・ ・ ・|五
+| ・ ・ ・ ・ ・ ・ ・ ・ ・|六
+| ・ ・ ・ ・ ・ ・ ・ ・ ・|七
+| ・ ・ ・ ・ ・ ・ ・ 飛 ・|八
+| ・ ・ ・ ・ ・ ・ ・ 桂 ・|九
++---------------------------+
+BOARD
+          pieces "先手" => "歩"
+          auto_flushing {
+            mov "▲２三歩"
+            mov "△同歩"
+            mov "▲同銀成"
+          }
+        end,
+      },
+    ]
+  end
+
+  if $0 == __FILE__
+    # XtraPattern.each{|pattern|HybridSequencer.execute(pattern)}
+    HybridSequencer.execute(XtraPattern.list.last).each{|frame|
+      puts frame.to_text
+    }
+  end
+end
diff --git a/lib/bushido/contrib/xfiles/0_第2電王戦第一局_阿部光瑠_vs_しゅうそ.rb b/lib/bushido/contrib/xfiles/0_第2電王戦第一局_阿部光瑠_vs_しゅうそ.rb
deleted file mode 100644
index cbe933b..0000000
--- a/lib/bushido/contrib/xfiles/0_第2電王戦第一局_阿部光瑠_vs_しゅうそ.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-# -*- coding: utf-8 -*-
-
-require_relative "../../../bushido"
-
-module Bushido
-  XtraPattern.define do
-    [
-      {
-        :title => Pathname(__FILE__).basename(".*"),
-        :dsl => KifuDsl.define do
-          board "平手"
-          auto_flushing {
-            set :hide_human_kif_logs, true
-            mov "▲７六歩(77) △３四歩(33) ▲２六歩(27) △８四歩(83) ▲７八金(69) △８五歩(84) ▲２二角成(88) △同銀(31) ▲８八銀(79) △７二銀(71) ▲３八銀(39) △３二金(41) ▲７七銀(88) △６四歩(63) ▲９六歩(97) △６三銀(72) ▲９五歩(96) △４二王(51) ▲４六歩(47) △７四歩(73) ▲６八王(59) △７三桂(81) ▲４七銀(38) △３三銀(22) ▲５八金(49) △３一王(42) ▲５六銀(47) △５四銀(63) ▲７九王(68) △２二王(31) ▲３六歩(37) △５二金(61) ▲３七桂(29) △６五桂(73) ▲６八銀(77) △８六歩(85) ▲同歩(87) △同飛(82) ▲８七歩打 △７六飛(86) ▲６六歩(67) △同飛(76) ▲６七銀(68) △４四角打 ▲８八角打 △５六飛(66) ▲同歩(57) △８八角成(44) ▲同王(79) △４四角打 ▲９八王(88) △６九銀打 ▲６八金(58) △７八銀(69) ▲同金(68) △５七桂成(65) ▲７六銀(67) △７五歩(74) ▲８五銀(76) △６七金打 ▲８八金(78) △４二金(52) ▲７一飛打 △５六成桂(57) ▲９一飛成(71) △８四歩打 ▲同銀(85) △７六歩(75) ▲７八歩打 △６六角(44) ▲５一角打 △４一金(42) ▲６二角成(51) △４二金(41) ▲５一馬(62) △３九角成(66) ▲１八飛(28) △６六馬(39) ▲８二竜(91) △８六歩打 ▲３五歩(36) △８七歩成(86) ▲同金(88) △１二王(22) ▲３四歩(35) △同銀(33) ▲４二馬(51) △同金(32) ▲同竜(82) △２二角打 ▲３一銀打 △３二歩打 ▲３三歩打 △同馬(66) ▲同竜(42) △同桂(21) ▲４二角打 △２一飛打 ▲２二銀成(31) △同飛(21) ▲３一角打 △２一銀打 ▲３六香打 △８六歩打 ▲同金(87) △８五歩打 ▲同金(86) △７七歩成(76) ▲同歩(78) △８六歩打 ▲３四香(36) △８七歩成(86) ▲同王(98)"
-          }
-        end,
-      },
-    ]
-  end
-
-  if $0 == __FILE__
-    # XtraPattern.each{|pattern|HybridSequencer.execute(pattern)}
-    HybridSequencer.execute(XtraPattern.list.last).each{|frame|
-      puts frame.to_text
-    }
-  end
-end
diff --git a/lib/bushido/contrib/xfiles/tactical_move_of_glance.rb b/lib/bushido/contrib/xfiles/tactical_move_of_glance.rb
deleted file mode 100644
index e69de29..0000000
diff --git a/lib/bushido/kifu_dsl.rb b/lib/bushido/kifu_dsl.rb
index b28b3f7..17c3c35 100644
--- a/lib/bushido/kifu_dsl.rb
+++ b/lib/bushido/kifu_dsl.rb
@@ -154,8 +154,8 @@ module Bushido
       end
     end

-    def self.define(&block)
-      new.tap{|e|e.instance_eval(&block)}
+    def self.define(*args, &block)
+      new.tap{|e|e.instance_exec(*args, &block)}
     end

     attr_reader :seqs
「ダンスの歩」追加