jekyll-csvy 0.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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTkyYWRiZDU1NTg0YzRmYzM3N2E4ZWM1ZThiZDViZWQ0OTE2YmJhYg==
5
+ data.tar.gz: !binary |-
6
+ ZWVkMWNjODA0ZGZkZWJhZDJjMzE4MmI2ZjI2Yjc3ZDY4ODcyNTE3ZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NDI0ZTlhYzBjOWE2ZDBiNjAwNzhiZTNmMGJkNmM3ZmFhM2JiZWViMTE4OTE2
10
+ MTUyOTFiNjIwMzIwZTU2YmJiNzk2Yjc5OTEwZTUzNWU1YmM2NmZjMGYxNDE0
11
+ ZjZmOTQ3Y2FhYTk4OTRkMzYyYWE5ZmJlZThiNzVmY2QyZmIyNTg=
12
+ data.tar.gz: !binary |-
13
+ OWYxMGI0MDdkMGNmNjJkNjYyMmUwZTQzZTQxN2JmY2I1YjhjZWQ4NGQwMTlm
14
+ YmU1OWNjOTRmMGY0ZGZjYTljMGQwNjk2YmVkZDU3NGIzY2YxZTZmYTlhMWNk
15
+ MGE4ODEwMTZhYmEzMzRjZDdjMDg3N2IxYzZhNGM1YzQ0MDcwYjg=
@@ -0,0 +1,66 @@
1
+ require 'csv'
2
+
3
+ module Jekyll
4
+ module Converters
5
+ class Csvy
6
+ def initialize(config)
7
+ Jekyll::External.require_with_graceful_fail "jekyll-pandoc"
8
+
9
+ @config = config
10
+ end
11
+
12
+ def matches(ext)
13
+ ext =~ /^\.csvy$/i
14
+ end
15
+
16
+ def output_ext(ext)
17
+ ".html"
18
+ end
19
+
20
+ def convert(content)
21
+ # convert csv content into markdown table using grid table format
22
+ content = convert_csv(content)
23
+
24
+ # convert markdown into HTML table
25
+ # uses pandoc with the grid_tables extension
26
+ site = Jekyll::Site.new(@config)
27
+ mkconverter = site.find_converter_instance(::Jekyll::Converters::Markdown)
28
+ mkconverter.convert(content)
29
+ end
30
+
31
+ def convert_csv(content)
32
+ rows = ::CSV.parse(content)
33
+
34
+ # calculate max width of each column
35
+ columns = rows.transpose
36
+ max_widths = columns.map { |c| c.max_by { |x| x.length }}.map { |c| c.length }
37
+
38
+ # header
39
+ table = []
40
+ table << separator_row(rows.first, max_widths)
41
+
42
+ # header row
43
+ table << content_row(rows.first, max_widths)
44
+ table << separator_row(rows.first, max_widths, delimiter = "=")
45
+
46
+ # body rows
47
+ rows[1..-1].each do |row|
48
+ table << content_row(row, max_widths)
49
+ table << separator_row(row, max_widths)
50
+ end
51
+
52
+ table.join("\n") + "\n"
53
+ rescue => e
54
+ raise Jekyll::Errors::FatalException, "Conversion failed with error: #{e.message}"
55
+ end
56
+
57
+ def content_row(row, max_widths)
58
+ row.each_with_index.reduce("|") { |sum, (x,i)| sum + ' ' + x + ' ' * (max_widths[i] - x.length) + " |" }
59
+ end
60
+
61
+ def separator_row(row, max_widths, delimiter = "-")
62
+ row.each_with_index.reduce("+") { |sum, (x,i)| sum + delimiter * (max_widths[i] + 2) + "+" }
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module JekyllCsvy
2
+ VERSION = "0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-csvy
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Martin Fenner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jekyll-pandoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 2.0.0
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.0'
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 2.0.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: pandoc-ruby
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: 2.0.0
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: '2.0'
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 2.0.0
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: '3.4'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: '3.4'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rdiscount
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '2.1'
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: 2.1.8
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ~>
110
+ - !ruby/object:Gem::Version
111
+ version: '2.1'
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: 2.1.8
115
+ description: A Jekyll CSVY converter that uses Pandoc grid tables.
116
+ email: mfenner@datacite.org
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - lib/jekyll-csvy.rb
122
+ - lib/jekyll-csvy/version.rb
123
+ homepage: https://github.com/datacite/jekyll-csvy
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '2.2'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.5.1
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Jekyll CSVY converter
147
+ test_files: []