jekyll-sqlite
Advanced tools
+4
-0
| ## [Unreleased] | ||
| ## [0.1.3] - 2024-07-02 | ||
| - First functional version | ||
| - Adds tests | ||
| ## [0.1.0] - 2023-05-08 | ||
| - Initial release |
+2
-0
@@ -8,4 +8,6 @@ # frozen_string_literal: true | ||
| # These are development dependencies | ||
| gem "rake", "~> 13.0" | ||
| gem "jekyll", "~> 4.0" | ||
| gem "rubocop", "~> 1.21" | ||
| gem "rubocop-rake", "~> 0.6.0" |
| # frozen_string_literal: true | ||
| require 'sqlite3' | ||
| require "sqlite3" | ||
| module JekyllSQlite | ||
@@ -17,4 +18,4 @@ # Main generator class | ||
| # Get the root of where we are generating the data | ||
| def get_root(root, name) | ||
| name.split(".")[0..-2].each do |p| | ||
| def get_root(root, db_name) | ||
| db_name.split(".")[0..-2].each do |p| | ||
| root = root[p] | ||
@@ -25,11 +26,11 @@ end | ||
| def gen_hash_data(root, db, _query) | ||
| def gen_hash_data(root, db, db_name, query) | ||
| root ||= {} | ||
| root[name] = db.execute(config["query"]) | ||
| root[name].size | ||
| root[db_name] = db.execute(query) | ||
| root[db_name].size | ||
| end | ||
| def gen_nested_data(item, db, query, name) | ||
| item[name] = [] | ||
| def gen_nested_data(item, db, query, db_name) | ||
| item[db_name] = [] | ||
| db.prepare(query) do |stmt| | ||
@@ -43,8 +44,8 @@ # We bind params, ignoring any errors | ||
| end | ||
| stmt.execute.each { |d| item[name] << d } | ||
| stmt.execute.each { |d| item[db_name] << d } | ||
| end | ||
| item[name].size | ||
| item[db_name].size | ||
| end | ||
| def array_gen(root, config, name, db) | ||
| def array_gen(root, config, db_name, db) | ||
| count = 0 | ||
@@ -54,5 +55,5 @@ root.each do |item| | ||
| if item.is_a? Hash | ||
| count += gen_nested_data(item, db, config["query"], name) | ||
| count += gen_nested_data(item, db, config["query"], db_name) | ||
| else | ||
| Jekyll.logger.info "Jekyll SQLite:", "Item is not a hash for #{name}. Unsupported configuration" | ||
| Jekyll.logger.info "Jekyll SQLite:", "Item is not a hash for #{db_name}. Unsupported configuration" | ||
| end | ||
@@ -63,8 +64,8 @@ end | ||
| def gen_data(root, config, name, db) | ||
| def gen_data(root, config, db_name, db) | ||
| count = 0 | ||
| if root.nil? || (root.is_a? Hash) | ||
| count = gen_hash_data(root, db, config["query"]) | ||
| count = gen_hash_data(root, db, db_name, config["query"]) | ||
| elsif root.is_a? Array | ||
| count = array_gen(root, config, name, db) | ||
| count = array_gen(root, config, db_name, db) | ||
| end | ||
@@ -78,11 +79,24 @@ count | ||
| def validate_config(config) | ||
| return false unless config.is_a? Hash | ||
| return false unless config.key?("query") | ||
| return false unless File.exist?(config["file"]) | ||
| return false unless config.key?("data") | ||
| true | ||
| end | ||
| def generate(site) | ||
| site.config["sqlite"].each do |config| | ||
| name = config["data"] | ||
| gem_config = site.config['sqlite'] || [] | ||
| gem_config.each do |config| | ||
| unless validate_config(config) | ||
| Jekyll.logger.error "Jekyll SQLite:", "Invalid Configuration. Skipping" | ||
| next | ||
| end | ||
| d_name = config["data"] | ||
| SQLite3::Database.new config["file"], readonly: true do |db| | ||
| fast_setup db | ||
| db.results_as_hash = config.fetch("results_as_hash", true) | ||
| root = get_root(site.data, name) | ||
| count = gen_data(root, config, get_tip(name), db) | ||
| Jekyll.logger.info "Jekyll SQLite:", "Loaded #{name}. Count=#{count}. as_hash=#{db.results_as_hash}" | ||
| root = get_root(site.data, d_name) | ||
| count = gen_data(root, config, get_tip(d_name), db) | ||
| Jekyll.logger.info "Jekyll SQLite:", "Loaded #{d_name}. Count=#{count}" | ||
| end | ||
@@ -89,0 +103,0 @@ end |
@@ -5,4 +5,4 @@ # frozen_string_literal: true | ||
| module Sqlite | ||
| VERSION = "0.1.2" | ||
| VERSION = "0.1.3" | ||
| end | ||
| end |
+17
-26
@@ -28,19 +28,22 @@ # Jekyll SQLite plugin | ||
| Update your `_config.yml` to define your data sources with your SQLite database. | ||
| Update your `_config.yml` to define your data sources with your SQLite database. Please see | ||
| the `test` directory for a functional example with the [Northwind database](https://github.com/jpwhite3/northwind-SQLite3). | ||
| ```yml | ||
| ... | ||
| # These are run in sequence, so any nested data can work well. | ||
| sqlite: | ||
| - data: members | ||
| file: _db/users.db | ||
| query: SELECT * FROM members ORDER by created_at DESC | ||
| # You can use `results_as_hash` to switch between array or hash results (default). | ||
| - data: verified | ||
| results_as_hash: false # default true | ||
| file: _db/users.db | ||
| query: SELECT username, email FROM members WHERE verified=1 | ||
| - data: members.posts | ||
| file: _db/posts.db | ||
| query: SELECT * FROM posts WHERE user_id = :id | ||
| - data: orders | ||
| file: &db "_db/northwind.db" | ||
| query: SELECT * from Orders | ||
| - data: customers | ||
| file: *db | ||
| query: SELECT * from Customers | ||
| - data: categories | ||
| file: *db | ||
| query: SELECT CategoryID, CategoryName, Description FROM Categories | ||
| # Note that the CategoryID parameter in the query is coming from site.data.categories[].CategoryID | ||
| # which was picked up in the previous query | ||
| - data: categories.products | ||
| file: *db | ||
| query: SELECT ProductID, ProductName FROM Products WHERE Products.CategoryID=:CategoryID | ||
| ``` | ||
@@ -51,15 +54,3 @@ | ||
| ```liquid | ||
| {% for member in site.data.members %} | ||
| - {{member.username}} | ||
| # Your Posts | ||
| {% for post in member.posts %} | ||
| {{post}} | ||
| {% endfor %} | ||
| {% endfor %} | ||
| # Result here is an array instead of a hash. | ||
| {% for user in site.data.verified %} | ||
| - :check: {{user[0]}} (Email: {{user[1]}}) | ||
| {% endfor %} | ||
| {{ site.data.categories | jsonify }} | ||
| ``` | ||
@@ -66,0 +57,0 @@ |
-49
| PATH | ||
| remote: . | ||
| specs: | ||
| jekyll-sqlite (0.1.1) | ||
| sqlite3 (~> 1.6) | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| ast (2.4.2) | ||
| json (2.6.3) | ||
| parallel (1.23.0) | ||
| parser (3.2.2.1) | ||
| ast (~> 2.4.1) | ||
| rainbow (3.1.1) | ||
| rake (13.0.6) | ||
| regexp_parser (2.8.0) | ||
| rexml (3.2.5) | ||
| rubocop (1.50.2) | ||
| json (~> 2.3) | ||
| parallel (~> 1.10) | ||
| parser (>= 3.2.0.0) | ||
| rainbow (>= 2.2.2, < 4.0) | ||
| regexp_parser (>= 1.8, < 3.0) | ||
| rexml (>= 3.2.5, < 4.0) | ||
| rubocop-ast (>= 1.28.0, < 2.0) | ||
| ruby-progressbar (~> 1.7) | ||
| unicode-display_width (>= 2.4.0, < 3.0) | ||
| rubocop-ast (1.28.1) | ||
| parser (>= 3.2.1.0) | ||
| rubocop-rake (0.6.0) | ||
| rubocop (~> 1.0) | ||
| ruby-progressbar (1.13.0) | ||
| sqlite3 (1.6.2-aarch64-linux) | ||
| sqlite3 (1.6.2-x86_64-linux) | ||
| unicode-display_width (2.4.2) | ||
| PLATFORMS | ||
| aarch64-linux | ||
| x86_64-linux | ||
| DEPENDENCIES | ||
| jekyll-sqlite! | ||
| rake (~> 13.0) | ||
| rubocop (~> 1.21) | ||
| rubocop-rake (~> 0.6.0) | ||
| BUNDLED WITH | ||
| 2.4.1 |