hiki2md 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db76e2902d739b22fea3bca6db5878c03b98b7ef
4
- data.tar.gz: 14fae6cbf09185cb6bd084523081fe9432322ce7
3
+ metadata.gz: 3144012b65184c7a5362f65f9f004d1c9bcbeff3
4
+ data.tar.gz: a4424168aab76259fe55e50e2dd3116d65a3b806
5
5
  SHA512:
6
- metadata.gz: 764fd9840a546519ab02e84fd5ffbeb20b5e48b87208aa31e789cadb97e337dad6a0493483fd35312cb1115c972f55d2741bdfb38431f8800751115c68cac9c5
7
- data.tar.gz: 09e53c7f75e6dc868c66273e4acf0ee704de70967a64dab9c57e1ddf1af9328e50c3081057151a1eb04866c10a0b05db39b16a0b8357f1b20807f6c214c7f90b
6
+ metadata.gz: d3eafa88b9204d1284124bceb9aca242a5084df2d2c160e7c90eaecfb85bda7037b54193e9aef4d4012d1286deeb05eeafe58fada04db20d5ea3b3cbb0bd9aa5
7
+ data.tar.gz: a8639a667f70cfc182f997ea78e8084df40072736c8ade63834a305a4416fd40184a5e9efc8daa6571eb0e66224d4f8fc5d95ac2689fbba674d19037fc7bd49a
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Hiki2md
2
2
 
3
+ [![Build Status](https://travis-ci.org/kdmsnr/hiki2md.svg?branch=master)](https://travis-ci.org/kdmsnr/hiki2md)
4
+
5
+ Converter of Hiki to Markdown.
6
+
3
7
  ## Installation
4
8
 
5
9
  Add this line to your application's Gemfile:
@@ -1,3 +1,3 @@
1
1
  class Hiki2md
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/hiki2md.rb CHANGED
@@ -6,7 +6,11 @@ class Hiki2md
6
6
  @outputs = []
7
7
 
8
8
  @in_plugin_block = false
9
- @in_preformated_block = false
9
+ @in_preformatted_block = false
10
+ @in_multiline_preformatted_block = false
11
+ @in_table_block = false
12
+ @in_dl_block = false
13
+ @table_contents = []
10
14
 
11
15
  lines.split(/\n/).each do |line|
12
16
  # プラグイン
@@ -22,10 +26,23 @@ class Hiki2md
22
26
  @in_plugin_block = true
23
27
  end
24
28
 
25
- # 整形済みテキスト
26
- if @in_preformated_block
29
+ # テーブル
30
+ if line =~ /\A\|\|/
31
+ @in_table_block = true
32
+ @table_contents << line
33
+ next
34
+ end
35
+
36
+ if @in_table_block
37
+ @outputs << make_table(@table_contents)
38
+ @in_table_block = false
39
+ @table_contents = []
40
+ end
41
+
42
+ # 整形済みテキスト(複数行)
43
+ if @in_multiline_preformatted_block
27
44
  if line =~ /\A>>>/
28
- @in_preformated_block = false
45
+ @in_multiline_preformatted_block = false
29
46
  @outputs << '```'
30
47
  next
31
48
  end
@@ -33,24 +50,45 @@ class Hiki2md
33
50
  next
34
51
  end
35
52
 
36
- if line =~ /\A<<<\z/
37
- @in_preformated_block = true
38
- @outputs << '```'
53
+ if line =~ /\A<<<\s*(.*)/
54
+ @in_multiline_preformatted_block = true
55
+ @outputs << "```#{$1}"
56
+ next
57
+ end
58
+
59
+ # 整形済みテキスト
60
+ if @in_preformatted_block
61
+ if line =~ /\A[ \t]+/
62
+ @outputs << line.strip
63
+ next
64
+ else
65
+ @outputs << "```"
66
+ @in_preformatted_block = false
67
+ end
68
+ end
69
+
70
+ if line =~ /\A[ \t]+/
71
+ @in_preformatted_block = true
72
+ @outputs << "```\n#{line.strip}"
39
73
  next
40
74
  end
41
75
 
42
76
  # コメント削除
43
77
  next if line =~ %r|\A//.*\z|
44
78
 
45
- # 整形済みテキスト
46
- line.gsub! /\A[ \t]+/, ' '
47
-
48
79
  # 引用
49
80
  line.gsub! /\A""/, '>'
50
81
 
51
82
  # リンク
52
83
  line.gsub! /\[{2}([^\[\]\|]+?)\|([^\[\]\|]+?)\]{2}/, "[\\1](\\2)"
53
84
 
85
+ # 強調
86
+ line.gsub! /'''(.+)'''/, "**\\1**"
87
+ line.gsub! /''(.+)''/, "*\\1*"
88
+
89
+ # 取り消し
90
+ line.gsub! /\=\=(.+)\=\=/, "~~\\1~~"
91
+
54
92
  # 箇条書き
55
93
  line.gsub! /\A[*]{3} ?/, ' - '
56
94
  line.gsub! /\A[*]{2} ?/, ' - '
@@ -60,6 +98,23 @@ class Hiki2md
60
98
  line.gsub! /\A[#]{2} ?/ , ' 1. '
61
99
  line.gsub! /\A[#] ?/ , '1. '
62
100
 
101
+ # 定義リスト
102
+ if line =~ /\A\:(.+)\:(.+)/
103
+ unless @in_dl_block
104
+ @outputs << "<dl>"
105
+ end
106
+ @outputs << "<dt>#{$1}</dt><dd>#{$2}</dd>"
107
+ @in_dl_block = true
108
+ next
109
+ end
110
+
111
+ if @in_dl_block
112
+ if line !=~ /\A\:.+\:.+/
113
+ @outputs << "</dl>"
114
+ @in_dl_block = false
115
+ end
116
+ end
117
+
63
118
  # 見出し
64
119
  line.gsub! /\A!{5} ?/ , '##### '
65
120
  line.gsub! /\A!{4} ?/ , '#### '
@@ -67,18 +122,105 @@ class Hiki2md
67
122
  line.gsub! /\A!{2} ?/ , '## '
68
123
  line.gsub! /\A! ?/ , '# '
69
124
 
70
- # 強調
71
- line.gsub! /'''(.+)'''/, "**\\1**"
72
- line.gsub! /''(.+)''/, "*\\1*"
73
-
74
- # 取り消し
75
- line.gsub! /\=\=(.+)\=\=/, "~~\\1~~"
76
-
77
125
  # 画像
78
126
  line.gsub! /\[{2}([^\[\]\|]+?)\]{2}/, "![](\\1)"
79
127
 
80
128
  @outputs << line
81
129
  end
130
+
131
+ # ensure
132
+ if @in_table_block
133
+ @outputs << make_table(@table_contents)
134
+ @in_table_block = false
135
+ @table_contents = []
136
+ end
137
+
138
+ # ensure
139
+ if @in_preformatted_block
140
+ @outputs << "```"
141
+ end
142
+
143
+ # ensure
144
+ if @in_dl_block
145
+ @outputs << "</dl>"
146
+ @in_dl_block = false
147
+ end
148
+
149
+
82
150
  @outputs.join("\n")
83
151
  end
152
+
153
+ # tableから連結作用素に対応したmatrixを作る
154
+ # input:lineごとに分割されたcontents
155
+ # output:matrixと最長列数
156
+ def make_matrix(contents)
157
+ t_matrix = []
158
+ contents.each do |line|
159
+ row = line.split('||')
160
+ row.shift
161
+ t_matrix << row
162
+ end
163
+
164
+ # vertical joint row
165
+ t_matrix.each_with_index do |line, i|
166
+ line.each_with_index do |e, j|
167
+ if e =~ /\^+/
168
+ t_matrix[i][j] = Regexp.last_match.post_match
169
+ Regexp.last_match.size.times do |k|
170
+ t_matrix[i + k + 1] ||= []
171
+ t_matrix[i + k + 1].insert(j, " ")
172
+ end
173
+ end
174
+ end
175
+ end
176
+
177
+ # horizontal joint column
178
+ max_col = 0
179
+ t_matrix.each_with_index do |line, i|
180
+ n_col = line.size
181
+ j_col = 0
182
+ line.each do |e|
183
+ if e =~ />+/
184
+ t_matrix[i][j_col] = Regexp.last_match.post_match
185
+ cs = Regexp.last_match.size
186
+ cs.times do
187
+ j_col += 1
188
+ t_matrix[i][j_col] = ""
189
+ end
190
+ n_col += cs
191
+ else
192
+ t_matrix[i][j_col] = e
193
+ j_col += 1
194
+ end
195
+ end
196
+ max_col = n_col if n_col > max_col
197
+ end
198
+
199
+ [t_matrix, max_col]
200
+ end
201
+
202
+ # tableを整形する
203
+ def make_table(table_contents)
204
+ contents, max_col = make_matrix(table_contents)
205
+
206
+ align_line = "|"
207
+ max_col.times { align_line << ':----|' }
208
+ align_line << "\n"
209
+
210
+ table = "\n"
211
+ contents.each_with_index do |line, idx|
212
+ row = "|"
213
+ line.each do |e|
214
+ row << "#{e}|"
215
+ end
216
+ table << row + "\n"
217
+
218
+ # insert table alignment after 1st line
219
+ if idx == 0
220
+ table << align_line
221
+ end
222
+ end
223
+
224
+ table
225
+ end
84
226
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiki2md
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masanori Kado
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-12 00:00:00.000000000 Z
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler