Class: Mdextab::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/mdextab/token.rb

Overview

Tokenクラス

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mes) ⇒ Token

初期化

Parameters:

  • mes (Messagex)

    Messagexクラスのインスタンス



14
15
16
17
# File 'lib/mdextab/token.rb', line 14

def initialize(mes)
  @mes = mes
  @token_struct = Struct.new(:kind, :opt)
end

Instance Attribute Details

#kindSymbol (readonly)

Returns トークンの種類.

Returns:

  • (Symbol)

    トークンの種類



6
7
8
# File 'lib/mdextab/token.rb', line 6

def kind
  @kind
end

#optHash (readonly)

Returns トークンのオプション.

Returns:

  • (Hash)

    トークンのオプション



8
9
10
# File 'lib/mdextab/token.rb', line 8

def opt
  @opt
end

Instance Method Details

#create_token(kind, opt = {}) ⇒ Struct

トークンの生成

Parameters:

  • kind (Symbol)

    生成するトークンの種類

  • opt (Hash) (defaults to: {})

    生成するトークンのオプション設定

Options Hash (opt):

  • :content (String)

    トークンの内容

  • :lineno (Integer)

    トークン出現行の行番号

  • :nth (Integer)

    “:”の並び文字列長

  • :attr (String, nil)

    トークンの属性またはnil(属性が存在しない場合)

  • :lineno (Integer)

    トークン出現行の行番号

Returns:

  • (Struct)

    生成されたトークン



30
31
32
# File 'lib/mdextab/token.rb', line 30

def create_token(kind, opt={})
  @token_struct.new(kind, opt)
end

#get_token(line, lineno) ⇒ Struct?

トークンの取得

Parameters:

  • line (String)

    現在行

  • lineno (Integer)

    現在行の行番号

Returns:

  • (Struct, nil)

    生成されたトークンまたはnil(トークンが存在しない場合)



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/mdextab/token.rb', line 129

def get_token(line, lineno)
  case line
  when /^\*S(.+)$/
    content = Regexp.last_match(1)
    ret = create_token(:STAR_START, { content: content, lineno: lineno })
  when /^\*E(.+)$/
    content = Regexp.last_match(1)
    ret = create_token(:STAR_END, { content: content, lineno: lineno })
  when /^\s*<table/
    ret = get_token_table_start(line, lineno)
  when /^\s*<tbody/
    ret = get_token_tbody_start(line, lineno)
  when /^\s*(\:+)(.*)$/
    nth = Regexp.last_match(1).size
    cont = Regexp.last_match(2)
    ret = get_token_colon_start(line, lineno, nth, cont)
  when %r{^\s*</table}
    ret = get_token_end_table(line, lineno)
  when %r{^\s*</tbody}
    if %r{^\s*</tbody>\s*$}.match?(line)
      ret = create_token(:TBODY_END, { lineno: lineno })
    else
      @mes.output_debug("E001 line=#{line}")
      ret = nil
    end
  else
    ret = create_token(:ELSE, { content: line, lineno: lineno })
  end

  ret
end

#get_token_colon_start(line, lineno, nth, cont) ⇒ Struct?

Note:

先頭が:のときに呼ばれる

先頭が:で始まる場合の適切なトークンの取得

Parameters:

  • line (String)

    現在行

  • lineno (Integer)

    現在行の行番号

  • nth (Integer)

    並んでいる:の個数

  • cont (String)

    現在行の中の:の並びを区切り文字列とした場合の右側の部分

Returns:

  • (Struct, nil)

    生成されたトークンまたはnil(トークンが存在しない場合)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mdextab/token.rb', line 77

def get_token_colon_start(line, lineno, nth, cont)
  if (m = /^th(.*)/.match(cont))
    cont2 = m[1]
    if (m2 = /^\s(.*)/.match(cont2))
      cont3 = m2[1]
      if (m3 = /^([^<]*)>(.*)$/.match(cont3))
        attr = m3[1]
        cont4 = m3[2]
        ret = create_token(:TH, { nth: nth, attr: attr, content: cont4, lineno: lineno })
      else
        # error
        # ret = nil
        ret = create_token(:ELSE, { nth: nth, attr: nil, content: cont, lineno: lineno })
      end
    elsif (m = /^>(.*)$/.match(cont2))
      cont3 = m[1]
      ret = create_token(:TH, { nth: nth, attr: nil, content: cont3, lineno: lineno })
    else
      ret = create_token(:ELSE, { nth: nth, attr: nil, content: cont, lineno: lineno })
    end
  elsif (m = /^([^<]*)>(.*)$/.match(cont))
    attr = m[1]
    cont2 = m[2]
    ret = create_token(:TD, { nth: nth, attr: attr, content: cont2, lineno: lineno })
  else
    ret = create_token(:TD, { nth: nth, attr: attr, content: cont, lineno: lineno })
  end
  ret
end

#get_token_table_end(line, lineno) ⇒ Struct?

Note:

TABLE_ENDトークンが存在するかもしれないと判断されたときに呼ばれる

TABLE_ENDトークンの取得

Parameters:

  • line (String)

    現在行

  • lineno (Integer)

    現在行の行番号

Returns:

  • (Struct, nil)

    生成されたトークンまたはnil(トークンが存在しない場合)



114
115
116
117
118
119
120
121
# File 'lib/mdextab/token.rb', line 114

def get_token_table_end(line, lineno)
  if %r{^\s*</table>\s*$}.match?(line)
    ret = create_token(:TABLE_END, { lineno: lineno })
  else
    ret = nil
  end
  ret
end

#get_token_table_start(line, lineno) ⇒ Struct?

Note:

TABLE_STARTトークンが存在するかもしれないと判断されたときに呼ばれる

TABLE_STARTトークンの取得

Parameters:

  • line (String)

    現在行

  • lineno (Integer)

    現在行の行番号

Returns:

  • (Struct, nil)

    生成されたトークンまたはnil(トークンが存在しない場合)



41
42
43
44
45
46
47
48
49
50
# File 'lib/mdextab/token.rb', line 41

def get_token_table_start(line, lineno)
  if /^\s*<table>\s*$/.match?(line)
    ret = create_token(:TABLE_START, { lineno: lineno })
  elsif (m = /^\s*<table\s+(.+)>\s*$/.match(line))
    ret = create_token(:TABLE_START, { attr: m[1], lineno: lineno })
  else
    ret = nil
  end
  ret
end

#get_token_tbody_start(line, lineno) ⇒ Struct?

Note:

TBODY_STARTトークンが存在するかもしれないと判断されたときに呼ばれる

TBODY_STARTトークンの取得

Parameters:

  • line (String)

    現在行

  • lineno (Integer)

    現在行の行番号

Returns:

  • (Struct, nil)

    生成されたトークンまたはnil(トークンが存在しない場合)



59
60
61
62
63
64
65
66
# File 'lib/mdextab/token.rb', line 59

def get_token_tbody_start(line, lineno)
  if /^\s*<tbody>\s*$/.match?(line)
    ret = create_token(:TBODY_START, { lineno: lineno })
  else
    ret = nil
  end
  ret
end