woshidan's loose leaf

ぼんやり勉強しています

esaやGitHubに検証結果貼りつけたいが、 tableタグや img タグなど書くのが面倒な時用のスニペット

esaGitHubに検証結果貼りつけたいが、 img タグなど書くのが面倒な時用のスニペット

出力用コード

require 'yaml'
require 'erb'

puts ARGV[0]

file    = File.open ARGV[0]
raw_str = file.read
@tests  = YAML.load(raw_str)

template_path = File.expand_path('template.erb', File.dirname($0))
erb = ERB.new(File.new(template_path).read)

puts erb.result(binding)

erbテンプレート

<!-- HTML -->
<% @tests.each do |test| %>

<h3><%=  test["title"] %></h3>

<table>
   <tr>
      <td>
      </td>
        <%  test["rows"].each do |row| %><th>
                <%= row %>
            </th><% end %>
  </tr>
    <% test["cols"].each do |col| %><tr>
        <th>
            <%= col %>
        </th>
      <%  test["rows"].each do |row| %><td>
          <% if test["with_image"] %>
            <img src="" alt="<%= "#{col} x #{row}" %>" width="<%= test["image_width"] %>px" />
            <% end %>
        </td>
      <% end %></tr>
    <% end %>
</table>

<% end %>
# Markdown
<% @tests.each do |test| %>

### <%=  test["title"] %>

|   <%  test["rows"].each do |row| %>|<%= row %><% end %>|
|---<%  test["rows"].each do |row| %>|---<% end %>|
<% test["cols"].each do |col| %>|<%= col %><%  test["rows"].each do |row| %>|<% if test["with_image"] %> <img src="" alt="<%= "#{col} x #{row}" %>" width="<%= test["image_width"] %>px" /><% end %><% end %>|
<% end %>
<% end %>

使い方

$ ruby build_table.rb sample.yml 

入力として用意するYAML

- with_image: true
  image_width: 100
  title: AとBの条件についてテスト(要スクショ)
  rows:
    - A = 1
    - A = 2
    - A = 3
    - A = 4
    - A = 5
  cols:
    - B = 1
    - B = 2
    - B = 3
- with_image: false
  title: AとBの条件についてテスト(スクショ不要)
  rows:
    - A = 1
    - A = 2
  cols:
    - B = 1
    - B = 2
    - B = 3
    - B = 4

出力

HTML

<h3>AとBの条件についてテスト(要スクショ)</h3>

<table>
   <tr>
        <td>
        </td>
        <th>
                A = 1
            </th><th>
                A = 2
            </th><th>
                A = 3
            </th><th>
                A = 4
            </th><th>
                A = 5
            </th>
    </tr>
    <tr>
        <th>
            B = 1
        </th>
        <td>
            
              <img src="https://cdn-ak.f.st-hatena.com/images/fotolife/w/woshidan/20171018/20171018132818.png" alt="B = 1 x A = 1" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 1 x A = 2" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 1 x A = 3" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 1 x A = 4" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 1 x A = 5" width="100px" />
            
        </td>
        </tr>
    <tr>
        <th>
            B = 2
        </th>
        <td>
            
              <img src="" alt="B = 2 x A = 1" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 2 x A = 2" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 2 x A = 3" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 2 x A = 4" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 2 x A = 5" width="100px" />
            
        </td>
        </tr>
    <tr>
        <th>
            B = 3
        </th>
        <td>
            
              <img src="" alt="B = 3 x A = 1" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 3 x A = 2" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 3 x A = 3" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 3 x A = 4" width="100px" />
            
        </td>
        <td>
            
              <img src="" alt="B = 3 x A = 5" width="100px" />
            
        </td>
        </tr>
    
</table>



<h3>AとBの条件についてテスト(スクショ不要)</h3>

<table>
   <tr>
        <td>
        </td>
        <th>
                A = 1
            </th><th>
                A = 2
            </th>
    </tr>
    <tr>
        <th>
            B = 1
        </th>
        <td>
            
        </td>
        <td>
            
        </td>
        </tr>
    <tr>
        <th>
            B = 2
        </th>
        <td>
            
        </td>
        <td>
            
        </td>
        </tr>
    <tr>
        <th>
            B = 3
        </th>
        <td>
            
        </td>
        <td>
            
        </td>
        </tr>
    <tr>
        <th>
            B = 4
        </th>
        <td>
            
        </td>
        <td>
            
        </td>
        </tr>
    
</table>

esaに貼り付けてみたときの見た目

f:id:woshidan:20171018133017p:plain

画像入れてく部分は画像が入ってなかったらaltに条件の文字列入れてるのでどの条件のテスト終わってないかわかりやすい。