LoginSignup
2

More than 5 years have passed since last update.

Re:VIEWでtableブロック内のカラム区切りをスペースにする

Posted at

Re:VIEWでtableを埋め込む場合、カラムの区切りはデフォルトではタブのみになっています。

が、正直タブのみは苦痛。エディタはsofttabですよ(´・ω・`)
スペース区切りでしょjkということで、Re:VIEWの生成を上書きできるreview-ext.rbを追加して対応します。
(かなり文書への影響が大きいからぷるりは無理ですね。。

review-ext.rbとは

Re:VIEWは独自に機能を追加したい、既存の機能を変えたい場合に、config.ymlと同ディレクトリにreview-ext.rbを作成して定義することで対応ができるようになっています。

という事で、既存のtableメソッド部分をコピーして、必要な箇所を変更していきます。(lib/review/pdfbuilder.rb)

※今回は自分が使っているpdf文書に限った変更になります。HTML等でやりたい場合は、HTMLの書式に合わせてreview-ext.rbを弄る必要があります

オリジナルからの変更点

現在タブのみが区切りとなっている部分があるので、ここを修正して、タブもしくはスペース☓2以上続いた場合に区切るように修正します。

rows.push line.strip.split(/\t+).map {|s| s.sub(/\A\./, '') }

↓ 変更

rows.push line.strip.split(/\t+|\s{2,}/).map {|s| s.sub(/\A\./, '') }


完成版

以下変更部分も合わせた、review-ext.rbの中身になります

ReVIEW::Compiler.definline :seqsplit

module ReVIEW
  class LATEXBuilder
    def table(lines, id = nil, caption = nil)
      rows = []
      sepidx = nil
      lines.each_with_index do |line, idx|
        if /\A[\=\{\-\}]{12}/ =~ line
          # just ignore
          #error "too many table separator" if sepidx
          sepidx ||= idx
          next
        end
        rows.push line.strip.split(/\t+|\s{2,}/).map {|s| s.sub(/\A\./, '') }
      end
      rows = adjust_n_cols(rows)

      begin
        table_header id, caption unless caption.nil?
      rescue KeyError
        error "no such table: #{id}"
      end
      return if rows.empty?
      table_begin rows.first.size
      if sepidx
        sepidx.times do
          tr rows.shift.map {|s| th(s) }
        end
        rows.each do |cols|
          tr cols.map {|s| td(s) }
        end
      else
        rows.each do |cols|
          h, *cs = *cols
          tr [th(h)] + cs.map {|s| td(s) }
        end
      end
      table_end
    end

  end
end

テーブルを生成してみる

以下のreview文書で正しくテーブルが生成されました!

//tsize[20,20,20]
//table[ラベル][キャプション]{
header1   header2   header3
---------------------------
a         hogehoge  fugafuga
.         hoge2     fuga2
//}

スクリーンショット 2016-03-03 15.37.41.png

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2