Class: Mdextab::Token
- Inherits:
-
Object
- Object
- Mdextab::Token
- Defined in:
- lib/mdextab/token.rb
Overview
Tokenクラス
Instance Attribute Summary collapse
-
#kind ⇒ Symbol
readonly
トークンの種類.
-
#opt ⇒ Hash
readonly
トークンのオプション.
Instance Method Summary collapse
-
#create_token(kind, opt = {}) ⇒ Struct
トークンの生成.
-
#get_token(line, lineno) ⇒ Struct?
トークンの取得.
-
#get_token_colon_start(_line, lineno, nth, cont) ⇒ Struct?
先頭が:で始まる場合の適切なトークンの取得.
-
#get_token_table_end(line, lineno) ⇒ Struct?
TABLE_ENDトークンの取得.
-
#get_token_table_start(line, lineno) ⇒ Struct?
TABLE_STARTトークンの取得.
-
#get_token_tbody_start(line, lineno) ⇒ Struct?
TBODY_STARTトークンの取得.
-
#initialize(mes) ⇒ Token
constructor
初期化.
Constructor Details
#initialize(mes) ⇒ Token
初期化
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
#kind ⇒ Symbol (readonly)
Returns トークンの種類.
6 7 8 |
# File 'lib/mdextab/token.rb', line 6 def kind @kind end |
#opt ⇒ Hash (readonly)
Returns トークンのオプション.
8 9 10 |
# File 'lib/mdextab/token.rb', line 8 def opt @opt end |
Instance Method Details
#create_token(kind, opt = {}) ⇒ 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?
トークンの取得
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/mdextab/token.rb', line 116 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_table_end(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:
先頭が:のときに呼ばれる
先頭が:で始まる場合の適切なトークンの取得
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mdextab/token.rb', line 69 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トークンの取得
106 107 108 |
# File 'lib/mdextab/token.rb', line 106 def get_token_table_end(line, lineno) (create_token(:TABLE_END, { lineno: lineno }) if %r{^\s*</table>\s*$}.match?(line)) end |
#get_token_table_start(line, lineno) ⇒ Struct?
Note:
TABLE_STARTトークンが存在するかもしれないと判断されたときに呼ばれる
TABLE_STARTトークンの取得
41 42 43 44 45 46 47 |
# File 'lib/mdextab/token.rb', line 41 def get_token_table_start(line, lineno) if /^\s*<table>\s*$/.match?(line) create_token(:TABLE_START, { lineno: lineno }) elsif (m = /^\s*<table\s+(.+)>\s*$/.match(line)) create_token(:TABLE_START, { attr: m[1], lineno: lineno }) end end |
#get_token_tbody_start(line, lineno) ⇒ Struct?
Note:
TBODY_STARTトークンが存在するかもしれないと判断されたときに呼ばれる
TBODY_STARTトークンの取得
56 57 58 |
# File 'lib/mdextab/token.rb', line 56 def get_token_tbody_start(line, lineno) (create_token(:TBODY_START, { lineno: lineno }) if /^\s*<tbody>\s*$/.match?(line)) end |