jekyll-sqlite 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -1
- data/README.md +92 -23
- data/lib/jekyll-sqlite/generator.rb +63 -5
- data/lib/jekyll-sqlite/version.rb +1 -1
- data/lib/jekyll_sqlite.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25c9ba502f7b75dd59633f9cd88c9412dfe49d0146cd5e4bc3510bd517158c4c
|
4
|
+
data.tar.gz: 29656b5f23cffc2912c498562c5e1547a1e2ffa865cfec3fee35892deaa5d1b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0a65da0d781e76cd8bb2605cc57eef3dea1a89e45d059eaf2eb8b9de3b2423b9252831e9d7ae4cc0011097fa9dfc4c533d8593ced72e97e85fe2e86d2151054
|
7
|
+
data.tar.gz: 9c96bdb90d10f05a1d30c3d5e389f180d30e73f69534a047f8e8b755abb16850f4f76a4bdaf3fdf867a71f77a6d93e826a5caac363bca679b6887bdd0160650f
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
jekyll-sqlite (0.1.
|
4
|
+
jekyll-sqlite (0.1.1)
|
5
5
|
sqlite3 (~> 1.6)
|
6
6
|
|
7
7
|
GEM
|
@@ -32,10 +32,12 @@ GEM
|
|
32
32
|
rubocop (~> 1.0)
|
33
33
|
ruby-progressbar (1.13.0)
|
34
34
|
sqlite3 (1.6.2-aarch64-linux)
|
35
|
+
sqlite3 (1.6.2-x86_64-linux)
|
35
36
|
unicode-display_width (2.4.2)
|
36
37
|
|
37
38
|
PLATFORMS
|
38
39
|
aarch64-linux
|
40
|
+
x86_64-linux
|
39
41
|
|
40
42
|
DEPENDENCIES
|
41
43
|
jekyll-sqlite!
|
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
# Jekyll
|
1
|
+
# Jekyll SQLite plugin
|
2
2
|
|
3
|
-
A Jekyll generator plugin to lets you use SQLite database instead of data files as a data source.
|
3
|
+
A Jekyll generator plugin to lets you use SQLite database instead of data files as a data source. It lets you easily create APIs and websites from a SQLite database, by linking together a database file, your template, and the relevant queries.
|
4
|
+
|
5
|
+
It additionally supports nested queries, so that you can use the rows of `site.data.items` as bind_params for your nested query.
|
4
6
|
|
5
7
|
[![Continuous Integration](https://github.com/captn3m0/jekyll-sqlite/actions/workflows/main.yml/badge.svg)](https://github.com/captn3m0/jekyll-sqlite/actions/workflows/main.yml) [![Gem Version](https://badge.fury.io/rb/jekyll-sqlite.svg)](https://badge.fury.io/rb/jekyll-sqlite)
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
9
|
-
Add this line to your site's Gemfile
|
11
|
+
Add this line to your site's `Gemfile`:
|
10
12
|
|
11
13
|
```ruby
|
12
14
|
gem 'jekyll-sqlite'
|
@@ -16,46 +18,42 @@ And then add this line to your site's `_config.yml`:
|
|
16
18
|
|
17
19
|
```yml
|
18
20
|
plugins:
|
19
|
-
-
|
21
|
+
- jekyll_sqlite
|
20
22
|
```
|
21
23
|
|
22
24
|
:warning: If you are using Jekyll < 3.5.0 use the `gems` key instead of `plugins`.
|
23
25
|
|
24
|
-
|
25
|
-
## Installation
|
26
|
-
|
27
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
28
|
-
|
29
|
-
Install the gem and add to the application's Gemfile by executing:
|
30
|
-
|
31
|
-
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
32
|
-
|
33
|
-
If bundler is not being used to manage dependencies, install the gem by executing:
|
34
|
-
|
35
|
-
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
36
|
-
|
37
26
|
## Usage
|
38
27
|
|
39
28
|
Update your `_config.yml` to define your data sources with your SQLite database.
|
40
29
|
|
41
30
|
```yml
|
42
31
|
...
|
32
|
+
# These are run in sequence, so any nested data can work well.
|
43
33
|
sqlite:
|
44
|
-
members
|
34
|
+
- data: members
|
45
35
|
file: _db/users.db
|
46
|
-
|
36
|
+
query: SELECT * FROM members ORDER by created_at DESC
|
47
37
|
# You can use `results_as_hash` to switch between array or hash results (default).
|
48
|
-
verified
|
38
|
+
- data: verified
|
49
39
|
results_as_hash: false # default true
|
50
40
|
file: _db/users.db
|
51
|
-
|
41
|
+
query: SELECT username, email FROM members WHERE verified=1
|
42
|
+
- data: members.posts
|
43
|
+
file: _db/posts.db
|
44
|
+
query: SELECT * FROM posts WHERE user_id = :id
|
52
45
|
```
|
53
46
|
|
54
47
|
Then, you can use the `site.data` attributes accordingly:
|
55
48
|
|
56
49
|
```liquid
|
57
|
-
{% for
|
58
|
-
- {{
|
50
|
+
{% for member in site.data.members %}
|
51
|
+
- {{member.username}}
|
52
|
+
|
53
|
+
# Your Posts
|
54
|
+
{% for post in member.posts %}
|
55
|
+
{{post}}
|
56
|
+
{% endfor %}
|
59
57
|
{% endfor %}
|
60
58
|
|
61
59
|
# Result here is an array instead of a hash.
|
@@ -64,6 +62,77 @@ Then, you can use the `site.data` attributes accordingly:
|
|
64
62
|
{% endfor %}
|
65
63
|
```
|
66
64
|
|
65
|
+
## Generating Pages
|
66
|
+
|
67
|
+
It works well with the `datapage_gen` plugin:
|
68
|
+
|
69
|
+
See the [datapage_gen](https://github.com/avillafiorita/jekyll-datapage_gen) docs for more details.
|
70
|
+
|
71
|
+
Here's a sample configuration:
|
72
|
+
|
73
|
+
```yaml
|
74
|
+
# This will automatically generate a file for each restaurant
|
75
|
+
# restaurants/#{id}.html file
|
76
|
+
# with the layout `_layouts/restaurant.html`
|
77
|
+
# and page.id, page.name, page.active set
|
78
|
+
# and page.title set to restaurant name
|
79
|
+
sqlite:
|
80
|
+
restaurants:
|
81
|
+
file: _db/reviews.db
|
82
|
+
sql: SELECT id, name, last_review_date > 1672531200 as active, address FROM restaurants;
|
83
|
+
page_gen:
|
84
|
+
- data: restaurants
|
85
|
+
template: restaurant
|
86
|
+
name: id
|
87
|
+
title: name
|
88
|
+
filter: active
|
89
|
+
```
|
90
|
+
|
91
|
+
## Nested Queries
|
92
|
+
|
93
|
+
You can use the rows of `site.data.items` as bind_params for your nested query. For this to work against
|
94
|
+
data generated by the plugin, the configuration order must be correct, so you need `site.data.items` above `site.data.items.nested` in your configuration.
|
95
|
+
|
96
|
+
Say you have a YAML file defining your items (`data/items.yaml`):
|
97
|
+
|
98
|
+
```yaml
|
99
|
+
- id: 31323952-2708-42dc-a995-6006a23cbf00
|
100
|
+
name: Item 1
|
101
|
+
- id: 5c8e67a0-d490-4743-b5b8-8e67bd1f95a2
|
102
|
+
name: Item 2
|
103
|
+
```
|
104
|
+
and the prices for the items in your SQLite database, the following configuration will enrich the `items` array with the price:
|
105
|
+
|
106
|
+
```yaml
|
107
|
+
sql:
|
108
|
+
- data: site.data.items.meta
|
109
|
+
query: SELECT price,author FROM pricing WHERE id =:id
|
110
|
+
```
|
111
|
+
This would allow the following Liquid loop to be written:
|
112
|
+
|
113
|
+
```liquid
|
114
|
+
{% for item in site.data.items %}
|
115
|
+
{{item.meta.price}}, {{item.meta.author}}
|
116
|
+
{% endfor %}
|
117
|
+
```
|
118
|
+
|
119
|
+
This works well with `results_as_configuration` as well.
|
120
|
+
|
121
|
+
```yaml
|
122
|
+
sql:
|
123
|
+
- data: site.data.items.meta
|
124
|
+
query: SELECT price,author FROM pricing WHERE id =:id
|
125
|
+
results_as_hash: false
|
126
|
+
```
|
127
|
+
|
128
|
+
The following also renders the price and author:
|
129
|
+
|
130
|
+
```liquid
|
131
|
+
{% for item in site.data.items %}
|
132
|
+
{{item.meta[0]}}, {{item.meta[1]}}
|
133
|
+
{% endfor %}
|
134
|
+
```
|
135
|
+
|
67
136
|
## Development
|
68
137
|
|
69
138
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
require 'sqlite3'
|
3
3
|
module JekyllSQlite
|
4
4
|
# Main generator class
|
5
5
|
class Generator < Jekyll::Generator
|
@@ -13,14 +13,72 @@ module JekyllSQlite
|
|
13
13
|
db.execute("PRAGMA query_only = ON")
|
14
14
|
end
|
15
15
|
|
16
|
+
# Get the root of where we are generating the data
|
17
|
+
def get_root(root, name)
|
18
|
+
name.split(".")[0..-2].each do |p|
|
19
|
+
root = root[p]
|
20
|
+
end
|
21
|
+
root
|
22
|
+
end
|
23
|
+
|
24
|
+
def gen_hash_data(root, db, _query)
|
25
|
+
root ||= {}
|
26
|
+
|
27
|
+
root[name] = db.execute(config["query"])
|
28
|
+
root[name].size
|
29
|
+
end
|
30
|
+
|
31
|
+
def gen_nested_data(item, db, query, name)
|
32
|
+
item[name] = []
|
33
|
+
db.prepare(query) do |stmt|
|
34
|
+
# We bind params, ignoring any errors
|
35
|
+
# Since there's no way to get required params
|
36
|
+
# From a statement
|
37
|
+
item.each do |key, value|
|
38
|
+
stmt.bind_param key, value
|
39
|
+
rescue StandardError # rubocop:disable Lint/SuppressedException
|
40
|
+
end
|
41
|
+
stmt.execute.each { |d| item[name] << d }
|
42
|
+
end
|
43
|
+
item[name].size
|
44
|
+
end
|
45
|
+
|
46
|
+
def array_gen(root, config, name, db)
|
47
|
+
count = 0
|
48
|
+
root.each do |item|
|
49
|
+
# TODO: Add support for binding Arrays as well.
|
50
|
+
if item.is_a? Hash
|
51
|
+
count += gen_nested_data(item, db, config["query"], name)
|
52
|
+
else
|
53
|
+
Jekyll.logger.info "Jekyll SQLite:", "Item is not a hash for #{name}. Unsupported configuration"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
count
|
57
|
+
end
|
58
|
+
|
59
|
+
def gen_data(root, config, name, db)
|
60
|
+
count = 0
|
61
|
+
if root.nil? || (root.is_a? Hash)
|
62
|
+
count = gen_hash_data(root, db, config["query"])
|
63
|
+
elsif root.is_a? Array
|
64
|
+
count = array_gen(root, config, name, db)
|
65
|
+
end
|
66
|
+
count
|
67
|
+
end
|
68
|
+
|
69
|
+
def get_tip(name)
|
70
|
+
name.split(".")[-1]
|
71
|
+
end
|
72
|
+
|
16
73
|
def generate(site)
|
17
|
-
site.config["sqlite"].each do |
|
74
|
+
site.config["sqlite"].each do |config|
|
75
|
+
name = config["data"]
|
18
76
|
SQLite3::Database.new config["file"], readonly: true do |db|
|
19
|
-
Jekyll.logger.info "Jekyll SQLite:", "Starting to load #{name}"
|
20
77
|
fast_setup db
|
21
78
|
db.results_as_hash = config.fetch("results_as_hash", true)
|
22
|
-
site.data
|
23
|
-
|
79
|
+
root = get_root(site.data, name)
|
80
|
+
count = gen_data(root, config, get_tip(name), db)
|
81
|
+
Jekyll.logger.info "Jekyll SQLite:", "Loaded #{name}. Count=#{count}. as_hash=#{db.results_as_hash}"
|
24
82
|
end
|
25
83
|
end
|
26
84
|
end
|
data/lib/jekyll_sqlite.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-sqlite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nemo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sqlite3
|