jekyll-csvy 0.2 → 0.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.
- checksums.yaml +8 -8
- data/lib/jekyll-csvy.rb +51 -45
- data/lib/jekyll-csvy/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZmEzMTliZTllMjMwYmQ5ZmU3MTQ4NTIzMjNhMDk0ZWUzYzMzMjJmYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NDg5ZmE0NzVlM2Y5ZWFkMDk5OTE2MmE0NTk4MmJkYmJjNGNiOWY4Zg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OTExNDYyN2M0MmM4ZDcxZTAwMTc2OTE3MDAwNWM3NDIwOTllNWViZTE1ZGJk
|
10
|
+
MmEyZDBhNjhiZjk4MDc4NDA5MGIzNDgwODJhOWZmNzU1ZTBiODVlYTYxZmRm
|
11
|
+
NjZjODBhZWM4MWI4ODNkNjYyOTk5MGJkMDY0MTRjMDhlYjJlYzM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZWUxZDVkMTM1Njc4ZTNhN2IyZjA2ZjdjMTE0NDgzNjk2MjA2YjVmMzE0ODhj
|
14
|
+
ODA3NmFhNTBjYjlmZWY4MmQyMDM3YTU0NjRjYTA0ZGE3NGRkZDBkNzA3OTAz
|
15
|
+
OWExODRhNmQ1NmNiNTlmZGVkMGUzNjJkMDNjMDdjYzE3NGE5MWM=
|
data/lib/jekyll-csvy.rb
CHANGED
@@ -1,66 +1,72 @@
|
|
1
1
|
require 'csv'
|
2
2
|
|
3
3
|
module Jekyll
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
Jekyll::External.require_with_graceful_fail "jekyll-pandoc"
|
4
|
+
class Csvy < Converter
|
5
|
+
safe true
|
6
|
+
priority :low
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
def initialize(config)
|
9
|
+
Jekyll::External.require_with_graceful_fail "jekyll-pandoc"
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
# requires the Pandoc markdown converter
|
12
|
+
# install jekyll-pandoc gem and set "markdown: Pandoc" in _config.yml
|
13
|
+
unless config["markdown"] == "Pandoc"
|
14
|
+
raise Jekyll::Errors::FatalException, "Pandoc markdown converter required"
|
14
15
|
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
end
|
17
|
+
@config = config
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def matches(ext)
|
21
|
+
ext =~ /^\.csvy$/i
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
mkconverter = site.find_converter_instance(::Jekyll::Converters::Markdown)
|
28
|
-
mkconverter.convert(content)
|
29
|
-
end
|
24
|
+
def output_ext(ext)
|
25
|
+
".html"
|
26
|
+
end
|
30
27
|
|
31
|
-
|
32
|
-
|
28
|
+
def convert(content)
|
29
|
+
# convert csv content into markdown table using grid table format
|
30
|
+
content = convert_csv(content)
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
# convert grid_tables markdown into HTML table using pandoc
|
33
|
+
site = Jekyll::Site.new(@config)
|
34
|
+
markdown_converter = site.find_converter_instance(Jekyll::Converters::Markdown)
|
35
|
+
markdown_converter.convert(content)
|
36
|
+
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
table << separator_row(rows.first, max_widths)
|
38
|
+
def convert_csv(content)
|
39
|
+
rows = ::CSV.parse(content)
|
41
40
|
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
# calculate max width of each column
|
42
|
+
columns = rows.transpose
|
43
|
+
max_widths = columns.map { |c| c.max_by { |x| x.length }}.map { |c| c.length }
|
45
44
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
table << separator_row(row, max_widths)
|
50
|
-
end
|
45
|
+
# header
|
46
|
+
table = []
|
47
|
+
table << separator_row(rows.first, max_widths)
|
51
48
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
49
|
+
# header row
|
50
|
+
table << content_row(rows.first, max_widths)
|
51
|
+
table << separator_row(rows.first, max_widths, delimiter = "=")
|
56
52
|
|
57
|
-
|
58
|
-
|
53
|
+
# body rows
|
54
|
+
rows[1..-1].each do |row|
|
55
|
+
table << content_row(row, max_widths)
|
56
|
+
table << separator_row(row, max_widths)
|
59
57
|
end
|
60
58
|
|
61
|
-
|
62
|
-
|
63
|
-
|
59
|
+
table.join("\n") + "\n"
|
60
|
+
rescue => e
|
61
|
+
raise Jekyll::Errors::FatalException, "Conversion failed with error: #{e.message}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def content_row(row, max_widths)
|
65
|
+
row.each_with_index.reduce("|") { |sum, (x,i)| sum + ' ' + x + ' ' * (max_widths[i] - x.length) + " |" }
|
66
|
+
end
|
67
|
+
|
68
|
+
def separator_row(row, max_widths, delimiter = "-")
|
69
|
+
row.each_with_index.reduce("+") { |sum, (x,i)| sum + delimiter * (max_widths[i] + 2) + "+" }
|
64
70
|
end
|
65
71
|
end
|
66
72
|
end
|
data/lib/jekyll-csvy/version.rb
CHANGED