Sha256: 4db4df223c3542fd3cb0da5a6692f81fdbbde44c8b9010da0882595fe98de929

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

require_relative './with_gobstones_css'

module StonesSpec
  class HtmlBoardRenderer
    include StonesSpec::WithGobstonesCSS

    def initialize(options = {})
      @options = options
    end

    def render(board)
"<style type=\"text/css\">
#{render_css}</style>

#{render_html board}"
    end

    def method_missing(name)
      @options[name]
    end

    def render_html(board)
      width, height = board.size

"#{table_title}
#{html_row_titles width, 'top'}
#{(0...height).to_a.reverse.map {|y| html_row(board, y)}.join}#{html_row_titles width, 'bottom'}
</table>
"
    end

    def render_css
      gobstones_css '9pt', 30
    end

    private

    def table_title
      base = "<table class=\"gbs_board\">"
      caption ? base + "\n<caption>#{caption}</caption>" : base
    end

    def html_row(board, y)
"  <tr>
    <td class=\"lv\">#{y}</td>
#{(0...board.size[0]).map {|x| html_cell board, [x, y] }.join "\n"}
    <td class=\"lv\">#{y}</td>
  </tr>
"
    end

    def html_cell(board, position)
      cell = board.cell_at position

"    <td class=\"gc#{board.head_position == position ? ' gh' : ''}\">
      <table>
        <tr>#{html_stone cell, :black}#{html_stone cell, :blue}</tr>
        <tr>#{html_stone cell, :red}#{html_stone cell, :green}</tr>
      </table>
    </td>"
    end

    def html_stone(cell, color)
      quantity = cell[color]

      if cell[color] == 0
        '<td><div class="O"></div></td>'
      else
        "<td><div class=\"gbs_stone #{Stones::Color.all_with_names.invert[color][0]}\"><span>#{quantity}</span></div></td>"
      end
    end

    def html_row_titles(width, caption)
"<tr><td class=\"lx #{caption}_left\"></td>#{(0...width).map {|x| "<td class=\"lh\">#{x}</td>"}.join}<td class=\"lx #{caption}_right\"></td></tr>"
    end
  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
mumuki-xgobstones-runner-2.1.0 lib/stones_spec/html_board_renderer.rb
mumuki-xgobstones-runner-2.0.0 lib/stones_spec/html_board_renderer.rb
stones-spec-1.0.4 lib/renderers/html_board_renderer.rb
stones-spec-1.0.3 lib/renderers/html_board_renderer.rb
stones-spec-1.0.2 lib/renderers/html_board_renderer.rb
stones-spec-1.0.1 lib/renderers/html_board_renderer.rb
mumuki-gobstones-runner-1.0.1 lib/stones_spec/html_board_renderer.rb
mumuki-gobstones-runner-1.0.0 lib/stones_spec/html_board_renderer.rb