mdless 0.0.6 → 0.0.7
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 +4 -4
- data/lib/mdless/converter.rb +9 -26
- data/lib/mdless/tables.rb +130 -0
- data/lib/mdless/version.rb +1 -1
- data/lib/mdless.rb +1 -0
- metadata +3 -3
- data/lib/helpers/formattables.py +0 -105
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fcccb4da5a1692b5452b814fd4741b7ab95b2e9
|
4
|
+
data.tar.gz: 6225634e845e46cfd9bebd9f0726454d379bce6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be0b61b2ef4802e06dc425acd67bc7280207880ffc3b87676631e21f4575023d6c588b3438e6640e7cb633f7b0475520f4d3439d8c5fe56b2e9cba42517474b5
|
7
|
+
data.tar.gz: 2f1be952a4c5f84a54b1a7cb9170095522363c1cc4e91b2168298c71f4db3c5129d8d90c99086ae5f4b1af77082931cb55c7db945ddef18dd1146f520b3c83e2
|
data/lib/mdless/converter.rb
CHANGED
@@ -113,6 +113,7 @@ module CLIMarkdown
|
|
113
113
|
rescue
|
114
114
|
input = IO.read(file)
|
115
115
|
end
|
116
|
+
input.gsub!(/\r?\n/,"\n")
|
116
117
|
if @options[:list]
|
117
118
|
list_headers(input)
|
118
119
|
else
|
@@ -127,6 +128,7 @@ module CLIMarkdown
|
|
127
128
|
rescue
|
128
129
|
input = STDIN.read
|
129
130
|
end
|
131
|
+
input.gsub!(/\r?\n/,"\n")
|
130
132
|
if @options[:list]
|
131
133
|
list_headers(input)
|
132
134
|
else
|
@@ -206,7 +208,7 @@ module CLIMarkdown
|
|
206
208
|
}.join("\n")
|
207
209
|
end
|
208
210
|
|
209
|
-
def
|
211
|
+
def cleanup_tables(input)
|
210
212
|
in_table = false
|
211
213
|
header_row = false
|
212
214
|
all_content = []
|
@@ -224,7 +226,7 @@ module CLIMarkdown
|
|
224
226
|
orig_table.push(line)
|
225
227
|
else
|
226
228
|
if in_table
|
227
|
-
if this_table.length >
|
229
|
+
if this_table.length > 2
|
228
230
|
# if there's no header row, add one, cleanup requires it
|
229
231
|
unless header_row
|
230
232
|
cells = this_table[0].sub(/^\|/,'').scan(/.*?\|/).length
|
@@ -232,14 +234,15 @@ module CLIMarkdown
|
|
232
234
|
this_table.insert(1, cell_row)
|
233
235
|
end
|
234
236
|
|
235
|
-
table = this_table.join("\n")
|
237
|
+
table = this_table.join("\n").strip
|
236
238
|
begin
|
237
|
-
|
239
|
+
formatted = MDTableFormatter.new(table)
|
240
|
+
res = formatted.to_md
|
238
241
|
res = color_table(res)
|
239
242
|
rescue
|
240
243
|
res = orig_table.join("\n")
|
241
244
|
end
|
242
|
-
all_content.push(
|
245
|
+
all_content.push(res)
|
243
246
|
else
|
244
247
|
all_content.push(orig_table.join("\n"))
|
245
248
|
end
|
@@ -255,27 +258,7 @@ module CLIMarkdown
|
|
255
258
|
end
|
256
259
|
|
257
260
|
def clean_table(input)
|
258
|
-
dir = File.dirname(__FILE__)
|
259
|
-
lib = File.expand_path(dir + '/../../lib')
|
260
|
-
script = File.join(lib, 'helpers/formattables.py')
|
261
261
|
|
262
|
-
if File.exists?(script) and File.executable?(script)
|
263
|
-
begin
|
264
|
-
|
265
|
-
res, s = Open3.capture2(script, :stdin_data=>input.strip)
|
266
|
-
|
267
|
-
if s.success?
|
268
|
-
res
|
269
|
-
else
|
270
|
-
input
|
271
|
-
end
|
272
|
-
rescue => e
|
273
|
-
@log.error(e)
|
274
|
-
input
|
275
|
-
end
|
276
|
-
else
|
277
|
-
input
|
278
|
-
end
|
279
262
|
end
|
280
263
|
|
281
264
|
def clean_markers(input)
|
@@ -738,7 +721,7 @@ module CLIMarkdown
|
|
738
721
|
Process.exit
|
739
722
|
end
|
740
723
|
|
741
|
-
out =
|
724
|
+
out = cleanup_tables(out)
|
742
725
|
out = clean_markers(out)
|
743
726
|
out = out.gsub(/\n+{2,}/m,"\n\n") + "\n#{xc}\n\n"
|
744
727
|
|
@@ -0,0 +1,130 @@
|
|
1
|
+
module CLIMarkdown
|
2
|
+
class MDTableFormatter
|
3
|
+
|
4
|
+
def initialize(string)
|
5
|
+
@doc = string
|
6
|
+
@format_row = []
|
7
|
+
parse
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse
|
11
|
+
@table = []
|
12
|
+
format = []
|
13
|
+
cols = 0
|
14
|
+
rows = @doc.split(/\r?\n/)
|
15
|
+
rows.each do |row|
|
16
|
+
row.strip!
|
17
|
+
row.sub!(/^\s*\|?/,'').sub!(/\|?\s*$/,'')
|
18
|
+
row_array = row.split(/\|/)
|
19
|
+
row_array.map! { |cell| cell.strip }
|
20
|
+
if row =~ /^[\|:\- ]+$/
|
21
|
+
format = row_array
|
22
|
+
else
|
23
|
+
@table.push row_array
|
24
|
+
end
|
25
|
+
cols = row_array.length if row_array.length > cols
|
26
|
+
end
|
27
|
+
|
28
|
+
format.each_with_index {|cell, i|
|
29
|
+
f = 'left'
|
30
|
+
if cell =~ /^:.*?:$/
|
31
|
+
f = 'center'
|
32
|
+
elsif cell =~ /:$/
|
33
|
+
f = 'right'
|
34
|
+
else
|
35
|
+
f = 'just'
|
36
|
+
end
|
37
|
+
@format_row.push(f)
|
38
|
+
}
|
39
|
+
|
40
|
+
if @format_row.length < cols
|
41
|
+
(cols - @format_row.length).times do
|
42
|
+
@format_row.push('left')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
@table.map! do |row|
|
47
|
+
if row.length < cols
|
48
|
+
(cols - row.length).times do
|
49
|
+
row.push("")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
row
|
53
|
+
end
|
54
|
+
@table
|
55
|
+
end
|
56
|
+
|
57
|
+
def table
|
58
|
+
@table ||= parse
|
59
|
+
end
|
60
|
+
|
61
|
+
def column_width(i)
|
62
|
+
@widths ||= column_widths
|
63
|
+
@widths[i]
|
64
|
+
end
|
65
|
+
|
66
|
+
def column_widths
|
67
|
+
@widths = []
|
68
|
+
@format_row.length.times do
|
69
|
+
@widths.push(0)
|
70
|
+
end
|
71
|
+
|
72
|
+
table.each do |row|
|
73
|
+
@format_row.each_with_index do |cell, i|
|
74
|
+
length = row[i].strip.length
|
75
|
+
@widths[i] = length if length > @widths[i]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
@widths
|
80
|
+
end
|
81
|
+
|
82
|
+
def pad(string,type,length)
|
83
|
+
string.strip!
|
84
|
+
if type == 'center'
|
85
|
+
string.center(length, ' ')
|
86
|
+
elsif type == 'right'
|
87
|
+
string.rjust(length, ' ')
|
88
|
+
elsif type == 'left'
|
89
|
+
string.ljust(length, ' ')
|
90
|
+
else
|
91
|
+
string.ljust(length, ' ')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def separator(length, alignment)
|
96
|
+
out = "".ljust(length,'-')
|
97
|
+
case alignment
|
98
|
+
when 'left'
|
99
|
+
out = ':' + out + '-'
|
100
|
+
when 'right'
|
101
|
+
out = '-' + out + ':'
|
102
|
+
when 'center'
|
103
|
+
out = ":#{out}:"
|
104
|
+
else
|
105
|
+
out = "-#{out}-"
|
106
|
+
end
|
107
|
+
out
|
108
|
+
end
|
109
|
+
|
110
|
+
def header_separator_row
|
111
|
+
output = []
|
112
|
+
@format_row.each_with_index do |column, i|
|
113
|
+
output.push separator(column_width(i), column)
|
114
|
+
end
|
115
|
+
"|#{output.join('|')}|"
|
116
|
+
end
|
117
|
+
|
118
|
+
def to_md
|
119
|
+
output = []
|
120
|
+
t = table.clone
|
121
|
+
|
122
|
+
t.each_with_index do |row, index|
|
123
|
+
row.map!.with_index { |cell, i| cell = pad(cell, @format_row[i], column_width(i)) }
|
124
|
+
output.push("| #{row.join(' | ').lstrip} |")
|
125
|
+
end
|
126
|
+
output.insert(1, header_separator_row)
|
127
|
+
output.join("\n")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
data/lib/mdless/version.rb
CHANGED
data/lib/mdless.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mdless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brett Terpstra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -69,10 +69,10 @@ extra_rdoc_files:
|
|
69
69
|
files:
|
70
70
|
- README.md
|
71
71
|
- bin/mdless
|
72
|
-
- lib/helpers/formattables.py
|
73
72
|
- lib/mdless.rb
|
74
73
|
- lib/mdless/colors.rb
|
75
74
|
- lib/mdless/converter.rb
|
75
|
+
- lib/mdless/tables.rb
|
76
76
|
- lib/mdless/version.rb
|
77
77
|
homepage: http://brettterpstra.com/project/mdless/
|
78
78
|
licenses:
|
data/lib/helpers/formattables.py
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
#!/usr/bin/python
|
2
|
-
|
3
|
-
import sys
|
4
|
-
import re
|
5
|
-
|
6
|
-
def just(string, type, n):
|
7
|
-
"Justify a string to length n according to type."
|
8
|
-
|
9
|
-
if type == '::':
|
10
|
-
return string.center(n)
|
11
|
-
elif type == '-:':
|
12
|
-
return string.rjust(n)
|
13
|
-
elif type == ':-':
|
14
|
-
return string.ljust(n)
|
15
|
-
else:
|
16
|
-
return string
|
17
|
-
|
18
|
-
|
19
|
-
def normtable(text):
|
20
|
-
"Aligns the vertical bars in a text table."
|
21
|
-
|
22
|
-
# Start by turning the text into a list of lines.
|
23
|
-
lines = text.splitlines()
|
24
|
-
rows = len(lines)
|
25
|
-
|
26
|
-
# Figure out the cell formatting.
|
27
|
-
# First, find the separator line.
|
28
|
-
for i in range(rows):
|
29
|
-
if set(lines[i]).issubset('|:.-'):
|
30
|
-
formatline = lines[i]
|
31
|
-
formatrow = i
|
32
|
-
break
|
33
|
-
|
34
|
-
# Delete the separator line from the content.
|
35
|
-
del lines[formatrow]
|
36
|
-
|
37
|
-
# Determine how each column is to be justified.
|
38
|
-
formatline = formatline.strip(' ')
|
39
|
-
if formatline[0] == '|': formatline = formatline[1:]
|
40
|
-
if formatline[-1] == '|': formatline = formatline[:-1]
|
41
|
-
fstrings = formatline.split('|')
|
42
|
-
justify = []
|
43
|
-
for cell in fstrings:
|
44
|
-
ends = cell[0] + cell[-1]
|
45
|
-
if ends == '::':
|
46
|
-
justify.append('::')
|
47
|
-
elif ends == '-:':
|
48
|
-
justify.append('-:')
|
49
|
-
else:
|
50
|
-
justify.append(':-')
|
51
|
-
|
52
|
-
# Assume the number of columns in the separator line is the number
|
53
|
-
# for the entire table.
|
54
|
-
columns = len(justify)
|
55
|
-
|
56
|
-
# Extract the content into a matrix.
|
57
|
-
content = []
|
58
|
-
for line in lines:
|
59
|
-
line = line.strip(' ')
|
60
|
-
if line[0] == '|': line = line[1:]
|
61
|
-
if line[-1] == '|': line = line[:-1]
|
62
|
-
cells = line.split('|')
|
63
|
-
# Put exactly one space at each end as "bumpers."
|
64
|
-
linecontent = [ ' ' + x.strip() + ' ' for x in cells ]
|
65
|
-
content.append(linecontent)
|
66
|
-
|
67
|
-
# Append cells to rows that don't have enough.
|
68
|
-
rows = len(content)
|
69
|
-
for i in range(rows):
|
70
|
-
while len(content[i]) < columns:
|
71
|
-
content[i].append('')
|
72
|
-
|
73
|
-
# Get the width of the content in each column. The minimum width will
|
74
|
-
# be 2, because that's the shortest length of a formatting string and
|
75
|
-
# because that matches an empty column with "bumper" spaces.
|
76
|
-
widths = [2] * columns
|
77
|
-
for row in content:
|
78
|
-
for i in range(columns):
|
79
|
-
widths[i] = max(len(row[i]), widths[i])
|
80
|
-
|
81
|
-
# Add whitespace to make all the columns the same width and
|
82
|
-
formatted = []
|
83
|
-
for row in content:
|
84
|
-
formatted.append('|' + '|'.join([ just(s, t, n) for (s, t, n) in zip(row, justify, widths) ]) + '|')
|
85
|
-
|
86
|
-
# Recreate the format line with the appropriate column widths.
|
87
|
-
formatline = '|' + '|'.join([ s[0] + '-'*(n-2) + s[-1] for (s, n) in zip(justify, widths) ]) + '|'
|
88
|
-
|
89
|
-
# Insert the formatline back into the table.
|
90
|
-
formatted.insert(formatrow, formatline)
|
91
|
-
|
92
|
-
# Return the formatted table.
|
93
|
-
return '\n'.join(formatted)
|
94
|
-
|
95
|
-
space_cleaner = r'''(?s)[ \t]*(?P<value>[\|:])[ \t]*'''
|
96
|
-
|
97
|
-
def whitespaceProcess(group_object):
|
98
|
-
return group_object.group('value')
|
99
|
-
|
100
|
-
|
101
|
-
# Read the input, process, and print.
|
102
|
-
unformatted = sys.stdin.read()
|
103
|
-
unformatted = re.sub(space_cleaner, whitespaceProcess, unformatted, flags=re.DOTALL)
|
104
|
-
|
105
|
-
print normtable(unformatted)
|