mdextab 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/mdextab/table.rb CHANGED
@@ -1,20 +1,20 @@
1
- require 'forwardable'
1
+ require "forwardable"
2
2
 
3
3
  module Mdextab
4
4
  class Table
5
5
  extend Forwardable
6
- def_delegators :@tbody, :add_th, :add_td, :tdAppend, :thAppend, :add
6
+ def_delegators :@tbody, :add_th, :add_td, :td_append, :th_append, :add
7
7
  attr_reader :lineno, :tbody
8
8
 
9
- def initialize(lineno, logger,attr=nil)
9
+ def initialize(lineno, mes, attr=nil)
10
10
  @lineno = lineno
11
11
  @attr = attr
12
12
  @tbody = nil
13
- @logger = logger
13
+ @mes = mes
14
14
  end
15
15
 
16
16
  def add_tbody(lineno)
17
- @tbody = Tbody.new(lineno, @logger)
17
+ @tbody = Tbody.new(lineno, @mes)
18
18
  end
19
19
 
20
20
  def tbody_end
@@ -32,20 +32,17 @@ module Mdextab
32
32
  def to_s(debug=false)
33
33
  if @attr
34
34
  if debug
35
- str_1 = %Q!<table #{@attr} lineno:#{@lineno}>!
35
+ str = %Q(<table #{@attr} lineno:#{@lineno}>)
36
36
  else
37
- str_1 = %Q!<table #{@attr}>!
37
+ str = %Q(<table #{@attr}>)
38
38
  end
39
+ elsif debug
40
+ str = %Q(<table lineno:#{@lineno}>)
39
41
  else
40
- if debug
41
- str_1 = %Q!<table lineno:#{@lineno}>!
42
- else
43
- str_1 = %Q!<table>!
44
- end
42
+ str = %Q(<table>)
45
43
  end
46
44
 
47
- [str_1, @tbody.to_s, "</table>"].join("\n")
45
+ [str, @tbody.to_s, "</table>"].join("\n")
48
46
  end
49
-
50
47
  end
51
48
  end
data/lib/mdextab/tbody.rb CHANGED
@@ -2,16 +2,16 @@ module Mdextab
2
2
  class Tbody
3
3
  attr_reader :lineno
4
4
 
5
- def initialize(lineno, logger)
5
+ def initialize(lineno, mes)
6
6
  @array = []
7
7
  @tr = nil
8
8
  @th = nil
9
9
  @td = nil
10
10
  @lineno = lineno
11
- @logger = logger
11
+ @mes = mes
12
12
  end
13
13
 
14
- def add_th(lineno, content, nth, attr=nil, condense)
14
+ def add_th(lineno, content, nth, attr, condense)
15
15
  if nth == 1
16
16
  @tr = Tr.new(lineno)
17
17
  @array << @tr
@@ -21,22 +21,22 @@ module Mdextab
21
21
  @tr.add(@th)
22
22
  end
23
23
 
24
- def add_td(lineno, content, nth, attr=nil, condense)
25
- @logger.debug( "content=#{content}|nth=#{nth}|attr=#{attr}" )
24
+ def add_td(lineno, content, nth, attr, condense)
25
+ @mes.output_debug("content=#{content}|nth=#{nth}|attr=#{attr}")
26
26
  if nth == 1
27
27
  @tr = Tr.new(lineno)
28
28
  @array << @tr
29
29
  end
30
- @td = Td.new(lineno,attr)
30
+ @td = Td.new(lineno, attr)
31
31
  @td.add(content, condense)
32
32
  @tr.add(@td)
33
33
  end
34
34
 
35
- def tdAppend(content, condense)
35
+ def td_append(content, condense)
36
36
  @td.add(content, condense)
37
37
  end
38
38
 
39
- def thAppend(content, condense)
39
+ def th_append(content, condense)
40
40
  @th.add(content, condense)
41
41
  end
42
42
 
@@ -49,7 +49,7 @@ module Mdextab
49
49
  end
50
50
 
51
51
  def to_s
52
- ["<tbody>" , @array.map{|x| x.to_s} , "</tbody>"].join("\n")
52
+ ["<tbody>", @array.map(&:to_s), "</tbody>"].join("\n")
53
53
  end
54
54
  end
55
55
  end
data/lib/mdextab/td.rb CHANGED
@@ -1,31 +1,31 @@
1
- class Td
2
- def initialize(lineno, attr=nil)
3
- @lineno = lineno
4
- @attr = attr
5
- @content = ""
6
- end
1
+ class Td
2
+ def initialize(lineno, attr=nil)
3
+ @lineno = lineno
4
+ @attr = attr
5
+ @content = ""
6
+ end
7
7
 
8
- def add(content, condnese)
9
- if condnese
10
- if @content
11
- if @contnet.match?(/^\s*$/)
12
- @content=content.to_s
13
- else
14
- @content+=content.to_s
15
- end
8
+ def add(content, condnese)
9
+ if condnese
10
+ if @content
11
+ if @contnet.match?(/^\s*$/)
12
+ @content = content.to_s
16
13
  else
17
- @content=content.to_s
14
+ @content += content.to_s
18
15
  end
19
16
  else
20
- @content = [@content, content].join("\n") if content
17
+ @content = content.to_s
21
18
  end
19
+ elsif content
20
+ @content = [@content, content].join("\n")
22
21
  end
22
+ end
23
23
 
24
- def to_s
25
- if @attr != nil
26
- %Q!<td #{@attr}>#{@content}</td>!
27
- else
28
- %Q!<td>#{@content}</td>!
29
- end
24
+ def to_s
25
+ if @attr.nil?
26
+ %Q(<td>#{@content}</td>)
27
+ else
28
+ %Q(<td #{@attr}>#{@content}</td>)
30
29
  end
31
30
  end
31
+ end
data/lib/mdextab/th.rb CHANGED
@@ -12,21 +12,21 @@ module Mdextab
12
12
  if @content.match?(/^\s*$/)
13
13
  @content = content.to_s
14
14
  else
15
- @content+=content.to_s
15
+ @content += content.to_s
16
16
  end
17
17
  else
18
- @content=content.to_s
18
+ @content = content.to_s
19
19
  end
20
- else
21
- @content = [@content , content].join("\n") if content
20
+ elsif content
21
+ @content = [@content, content].join("\n")
22
22
  end
23
23
  end
24
24
 
25
25
  def to_s
26
- if @attr != nil
27
- %Q!<th #{@attr}>#{@content}</th>!
26
+ if @attr.nil?
27
+ %Q(<th>#{@content}</th>)
28
28
  else
29
- %Q!<th>#{@content}</th>!
29
+ %Q(<th #{@attr}>#{@content}</th>)
30
30
  end
31
31
  end
32
32
  end
data/lib/mdextab/token.rb CHANGED
@@ -2,10 +2,104 @@ module Mdextab
2
2
  class Token
3
3
  attr_reader :kind, :opt
4
4
 
5
- def initialize(kind , opt={})
6
- @kind = kind
7
- @opt = opt
5
+ def initialize(mes)
6
+ @mes = mes
7
+ @token_struct = Struct.new(:kind, :opt)
8
+ end
9
+
10
+ def create_token(kind, opt={})
11
+ @token_struct.new(kind, opt)
12
+ end
13
+
14
+ def get_token_start_table(line, lineno)
15
+ if /^\s*<table>\s*$/.match?(line)
16
+ ret = create_token(:TABLE_START, { lineno: lineno })
17
+ elsif (m = /^\s*<table\s+(.+)>\s*$/.match(line))
18
+ ret = create_token(:TABLE_START, { attr: m[1], lineno: lineno })
19
+ else
20
+ ret = nil
21
+ end
22
+ ret
23
+ end
24
+
25
+ def get_token_start_tbody(line, lineno)
26
+ if /^\s*<tbody>\s*$/.match?(line)
27
+ ret = create_token(:TBODY_START, { lineno: lineno })
28
+ else
29
+ ret = nil
30
+ end
31
+ ret
32
+ end
33
+
34
+ def get_token_start_colon(line, lineno, nth, cont)
35
+ if (m = /^th(.*)/.match(cont))
36
+ cont2 = m[1]
37
+ if (m2 = /^\s(.*)/.match(cont2))
38
+ cont3 = m2[1]
39
+ if (m3 = /^([^<]*)>(.*)$/.match(cont3))
40
+ attr = m3[1]
41
+ cont4 = m3[2]
42
+ ret = create_token(:TH, { nth: nth, attr: attr, content: cont4, lineno: lineno })
43
+ else
44
+ # error
45
+ # ret = nil
46
+ ret = create_token(:ELSE, { nth: nth, attr: nil, content: cont, lineno: lineno })
47
+ end
48
+ elsif (m = /^>(.*)$/.match(cont2))
49
+ cont3 = m[1]
50
+ ret = create_token(:TH, { nth: nth, attr: nil, content: cont3, lineno: lineno })
51
+ else
52
+ ret = create_token(:ELSE, { nth: nth, attr: nil, content: cont, lineno: lineno })
53
+ end
54
+ elsif (m = /^([^<]*)>(.*)$/.match(cont))
55
+ attr = m[1]
56
+ cont2 = m[2]
57
+ ret = create_token(:TD, { nth: nth, attr: attr, content: cont2, lineno: lineno })
58
+ else
59
+ ret = create_token(:TD, { nth: nth, attr: attr, content: cont, lineno: lineno })
60
+ end
61
+ ret
62
+ end
63
+
64
+ def get_token_end_table(line, lineno)
65
+ if %r{^\s*</table>\s*$}.match?(line)
66
+ ret = create_token(:TABLE_END, { lineno: lineno })
67
+ else
68
+ ret = nil
69
+ end
70
+ ret
71
+ end
72
+
73
+ def get_token(line, lineno)
74
+ case line
75
+ when /^\*S(.+)$/
76
+ content = Regexp.last_match(1)
77
+ ret = create_token(:STAR_START, { content: content, lineno: lineno })
78
+ when /^\*E(.+)$/
79
+ content = Regexp.last_match(1)
80
+ ret = create_token(:STAR_END, { content: content, lineno: lineno })
81
+ when /^\s*<table/
82
+ ret = get_token_start_table(line, lineno)
83
+ when /^\s*<tbody/
84
+ ret = get_token_start_tbody(line, lineno)
85
+ when /^\s*(\:+)(.*)$/
86
+ nth = Regexp.last_match(1).size
87
+ cont = Regexp.last_match(2)
88
+ ret = get_token_start_colon(line, lineno, nth, cont)
89
+ when %r{^\s*</table}
90
+ ret = get_token_end_table(line, lineno)
91
+ when %r{^\s*</tbody}
92
+ if %r{^\s*</tbody>\s*$}.match?(line)
93
+ ret = create_token(:TBODY_END, { lineno: lineno })
94
+ else
95
+ @mes.output_debug("E001 line=#{line}")
96
+ ret = nil
97
+ end
98
+ else
99
+ ret = create_token(:ELSE, { content: line, lineno: lineno })
100
+ end
101
+
102
+ ret
8
103
  end
9
104
  end
10
105
  end
11
-
data/lib/mdextab/tr.rb CHANGED
@@ -10,7 +10,7 @@ module Mdextab
10
10
  end
11
11
 
12
12
  def to_s
13
- ["<tr>", @array.map{|x| x.to_s} , "</tr>"].join("\n")
13
+ ["<tr>", @array.map(&:to_s), "</tr>"].join("\n")
14
14
  end
15
15
  end
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module Mdextab
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.3"
3
3
  end