read_cvs 0.0.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/read_cvs.rb +104 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ea607b47157a3af137b1c963c52f74f535549d846230435eee648c8db349927a
4
+ data.tar.gz: c32af5971fc09f0cc7d9d35895e0358867c29a3044f76f52b768e56c63654ebc
5
+ SHA512:
6
+ metadata.gz: c6a89931e5e01aa0d86ad132234f755fc004d0dc41cf5bd4c59e37742ef5e0f4230ad92aad540fd25c95b7d835d410d63d68544639ca4c4b8130607913d3e2fa
7
+ data.tar.gz: e1e50a5314282545cfa4c478e80290c44348f22130c1514475e6eb1259213d8d3a320a2411f653dc107a5ae75840b65582f0e2a2e7104c941b335ba583b0956f
data/lib/read_cvs.rb ADDED
@@ -0,0 +1,104 @@
1
+ #
2
+ #
3
+ # Add data read from _csv directory to site.data
4
+ #
5
+ # For each csv file <<name>> found in _csv/, this plugin adds the
6
+ # following information:
7
+ #
8
+ # * site.data.<<name>>.rows (number of rows)
9
+ # * site.data.<<name>>.cols (number of columns)
10
+ #
11
+ # * site.data.<<name>>.keys (Array containing the headers of the csv file)
12
+ # * site.data.<<name>>.content (array of arrays: content of the csv file)
13
+ # * site.data.<<name>>.content_hash (array of hashes: content of the csv file)
14
+ #
15
+ # The content is available similar to @_data@
16
+ #
17
+ # Example
18
+ #
19
+ # Suppose you have a my_table.csv file in the _csv directory,
20
+ # with the following content:
21
+ #
22
+ # h1,h2,h3
23
+ # a,b,c
24
+ # 1,2,3
25
+ #
26
+ # You can build an HTML representation of the table with the following code:
27
+ #
28
+ # <table>
29
+ # <thead>
30
+ # <tr>
31
+ # {% for header in site.data.my_table.keys %}
32
+ # <td>{{header}}</td>
33
+ # {% endfor %}
34
+ # </tr>
35
+ # </thead>
36
+ #
37
+ # <tbody>
38
+ # {% for row in site.data.my_table.content %}
39
+ # <tr>
40
+ # {% for column in row %}
41
+ # <td>{{column}}</td>
42
+ # {% endfor %}
43
+ # </tr>
44
+ # {% endfor %}
45
+ # </tbody>
46
+ # </table>
47
+ #
48
+ # content_hash can be used to access the content using the header keys.
49
+ # For instance:
50
+ #
51
+ # {% for row in site.data.my_table.content_hash %}
52
+ # <tr>
53
+ # <td>{{row.h1}}</td>
54
+ # <td>{{row.h3}}</td>
55
+ # </tr>
56
+ # {% endfor %}
57
+ # </tbody>
58
+ # </table>
59
+
60
+
61
+ module CSVDataReader
62
+ require 'csv'
63
+
64
+ class Generator < Jekyll::Generator
65
+ def generate(site)
66
+ #dir = config['csv_data_source'] || '_csv'
67
+ dir = "_csv"
68
+ base = File.join(site.source, dir)
69
+ return unless File.directory?(base) && (!site.safe || !File.symlink?(base))
70
+
71
+ entries = Dir.chdir(base) { Dir['*.csv'] }
72
+ entries.delete_if { |e| File.directory?(File.join(base, e)) }
73
+
74
+ entries.each do |entry|
75
+ path = File.join(site.source, dir, entry)
76
+ next if File.symlink?(path) && site.safe
77
+
78
+ key = sanitize_filename(File.basename(entry, '.*'))
79
+ file_data = CSV.read(path, :headers => true)
80
+
81
+ data = Hash.new
82
+ data['content'] = file_data.to_a[1..-1]
83
+ data['content_hash'] = file_data.map(&:to_hash)
84
+ data['keys'] = file_data.headers
85
+ data['rows'] = data['content'].size
86
+ data['cols'] = file_data.headers.size
87
+
88
+ csv_data = Hash.new
89
+ csv_data[key] = data
90
+
91
+ site.data.merge!(csv_data){ |shared_key| raise "csv and data named \"#{shared_key}\"" }
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ # copied from Jekyll
98
+ def sanitize_filename(name)
99
+ name = name.gsub(/[^\w\s_-]+/, '')
100
+ name = name.gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
101
+ name = name.gsub(/\s+/, '_')
102
+ end
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: read_cvs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - lee seung kyu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: read_cvs gem
14
+ email: zx6658@naver.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/read_cvs.rb
20
+ homepage: http://rubygems.org/gems/read_cvs
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.2.22
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: read_cvs gem!
43
+ test_files: []