Class: Tefil::MdToFswiki

Inherits:
TextFilterBase show all
Defined in:
lib/tefil/mdtofswiki.rb

Overview

! /usr/bin/env ruby coding: utf-8

Instance Method Summary collapse

Methods inherited from TextFilterBase

#filter, #initialize

Constructor Details

This class inherits a constructor from Tefil::TextFilterBase

Instance Method Details

#process_stream(in_io, out_io) ⇒ Object

def initialize(options)

super(options)

end



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tefil/mdtofswiki.rb', line 9

def process_stream(in_io, out_io)

  in_io.readlines.each do |line|
    # 行頭処理
    case
    when line.sub!(/^\#\#\#/  , '') then type = :head3
    when line.sub!(/^\#\#/    , '') then type = :head2
    when line.sub!(/^\#/      , '') then type = :head1
    when line.sub!(/^      \*/, '') then type = :item4
    when line.sub!(/^    \*/  , '') then type = :item3
    when line.sub!(/^  \*/    , '') then type = :item2
    when line.sub!(/^\*/      , '') then type = :item1
    when line.sub!(/^      0./, '') then type = :enum4
    when line.sub!(/^    0./  , '') then type = :enum3
    when line.sub!(/^  0./    , '') then type = :enum2
    when line.sub!(/^0./      , '') then type = :enum1
    when line.sub!(/^    /    , '') then type = :pre
    when line.sub!(/^---/     , '') then type = :hline
    else                                 type = :plain
    end

    # 行中要素の処理
    line.gsub!('**', "'''")
    line.gsub!('*' , "''"  )
    if /\[(.*)\]\((.*)\)/ =~ line # 複数ある場合は非対応。
      str = $1
      url = $2
      line.sub!(/\[(.*)\]\((.*)\)/, "\[#{str}|#{url}\]")
    end

    case
    when type == :head3 then line.sub!(/^/, '!'     )
    when type == :head2 then line.sub!(/^/, '!!'    )
    when type == :head1 then line.sub!(/^/, '!!!'   )
    when type == :item4 then line.sub!(/^/, '****'  )
    when type == :item3 then line.sub!(/^/, '***'   )
    when type == :item2 then line.sub!(/^/, '**'    )
    when type == :item1 then line.sub!(/^/, '*'     )
    when type == :enum4 then line.sub!(/^/, '++++'  )
    when type == :enum3 then line.sub!(/^/, '+++'   )
    when type == :enum2 then line.sub!(/^/, '++'    )
    when type == :enum1 then line.sub!(/^/, '+'     )
    when type == :pre   then line.sub!(/^/, ' '     )
    when type == :hline then line.sub!(/^/, '----'  )
    else # type == :pain  then  'do nothing'
    end

    # 出力
    out_io.puts line
  end
end