kirana 0.1.0.pre1
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +14 -0
- data/lib/kirana/class_map.rb +67 -0
- data/lib/kirana/merge.rb +114 -0
- data/lib/kirana/railtie.rb +16 -0
- data/lib/kirana.rb +13 -0
- metadata +46 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ec04ffb0d1203c354bee5abaa99909fc167c3e3ab58c045371fab59ba32e89dc
|
|
4
|
+
data.tar.gz: 985f62160305402c359939eaad2e6a0af6bc8f086d59c80d90e76d9592115ae2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3ab74b718e0639a6d02eab20f7e8ef54bbf60f6de8534afca861a9074d9890cc679fcd6086a6017fa310f5893ec6328bd45e85dd411d53f6a2db5254cd623b2e
|
|
7
|
+
data.tar.gz: c2be4913738f14e7e7c093bf34091e499dc1d2624b4d94c9b292198a5eb32a00a375a2cf9aa37defcc580086461d141ae2565aec52cf6db4867ac2c90952d367
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kirana authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# kirana (Ruby)
|
|
2
|
+
|
|
3
|
+
Kirana for Rails templates (D-069): the `kirana` view helper, which reads the class map
|
|
4
|
+
`kirana build` and `kirana dev` write and merges named styles the way `sx` does.
|
|
5
|
+
|
|
6
|
+
```erb
|
|
7
|
+
<div class="<%= kirana :card, active && "card.active" %>">…</div>
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
The Railtie adds the helper to every view; `config.kirana.classes` overrides the default
|
|
11
|
+
`public/kirana/classes.json`. The map is read again whenever it changes. Outside Rails,
|
|
12
|
+
`Kirana::ClassMap.from_file(path).classes(:card)`.
|
|
13
|
+
|
|
14
|
+
`bundle exec rake test` runs the tests, the merge vectors included, at 100 % of lines and branches.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Kirana
|
|
6
|
+
# A class map (D-069): the class string of every handle a server app's style modules export, by
|
|
7
|
+
# name, as `kirana build` and `kirana dev` write it. `classes` merges named handles the way `sx`
|
|
8
|
+
# merges them, later ones winning.
|
|
9
|
+
class ClassMap
|
|
10
|
+
# A name the class map does not list.
|
|
11
|
+
class UnknownStyle < KeyError; end
|
|
12
|
+
|
|
13
|
+
def initialize(map)
|
|
14
|
+
raise ArgumentError, "kirana: this class map was written by another version of Kirana; rebuild it" unless map["version"] == 1
|
|
15
|
+
|
|
16
|
+
@classes = map.fetch("classes", {})
|
|
17
|
+
families = map.fetch("families", {})
|
|
18
|
+
@merge = Merge.new(map.fetch("prefix", "k"), families.fetch("four", 0), families.fetch("two", 0))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Reads the class map `kirana build` or `kirana dev` wrote.
|
|
22
|
+
def self.from_file(path)
|
|
23
|
+
raise Errno::ENOENT, "kirana: no class map at #{path}; run `kirana dev` or `kirana build` first" unless File.file?(path)
|
|
24
|
+
|
|
25
|
+
new(JSON.parse(File.read(path)))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The classes of the named handles, merged: `classes(:card, active && "card.active")`. Falsy names
|
|
29
|
+
# are skipped, so a condition can leave one out.
|
|
30
|
+
def classes(*names)
|
|
31
|
+
strings = names.reject { |name| !name || name.to_s.empty? }.map do |name|
|
|
32
|
+
@classes.fetch(name.to_s) { raise UnknownStyle, "kirana: no style named \"#{name}\" in the class map" }
|
|
33
|
+
end
|
|
34
|
+
@merge.dom(strings)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Whether the map has a handle by this name.
|
|
38
|
+
def key?(name)
|
|
39
|
+
@classes.key?(name.to_s)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
class << self
|
|
44
|
+
# Where the class map is: set by the Railtie from `config.kirana.classes`.
|
|
45
|
+
attr_writer :classes_path
|
|
46
|
+
|
|
47
|
+
def classes_path
|
|
48
|
+
@classes_path || "public/kirana/classes.json"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The class map at `classes_path`, read again when the file changes (`kirana dev` rewrites it).
|
|
52
|
+
def class_map
|
|
53
|
+
stamp = File.file?(classes_path) && File.mtime(classes_path)
|
|
54
|
+
@class_map = nil unless @stamp == stamp && @path == classes_path
|
|
55
|
+
@stamp = stamp
|
|
56
|
+
@path = classes_path
|
|
57
|
+
@class_map ||= ClassMap.from_file(classes_path)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# The view helper: `<div class="<%= kirana :card, active && "card.active" %>">`.
|
|
62
|
+
module Helper
|
|
63
|
+
def kirana(*names)
|
|
64
|
+
Kirana.class_map.classes(*names)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
data/lib/kirana/merge.rb
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kirana
|
|
4
|
+
# Kirana's class merge, as the private runtime does it (`@kirana/core/internal`, D-018), for
|
|
5
|
+
# templates that hold class strings from a class map (D-069).
|
|
6
|
+
#
|
|
7
|
+
# Kirana class names are `{prefix}{key}_{value}`: merging keeps the last class per key, a
|
|
8
|
+
# `{prefix}{key}_` tombstone removes one key, `{prefix}{property}*_` removes every key of a
|
|
9
|
+
# property, and `{prefix}{shorthand}/{codes}{scope}_` unfolds a shorthand atom into longhands.
|
|
10
|
+
# Classes without the prefix pass through in order. Held to the runtime by
|
|
11
|
+
# `fixtures/merge/vectors.json`.
|
|
12
|
+
class Merge
|
|
13
|
+
# `four`: property codes below it are four-longhand box families; `two`: codes from `four` below
|
|
14
|
+
# it are two-longhand families.
|
|
15
|
+
def initialize(prefix, four, two)
|
|
16
|
+
@prefix = prefix
|
|
17
|
+
@four = four
|
|
18
|
+
@two = two
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Every class the strings merge to, in merge order, tombstones included.
|
|
22
|
+
def merge(strings)
|
|
23
|
+
classes = {}
|
|
24
|
+
strings.each do |string|
|
|
25
|
+
string.split(" ").each { |token| classes = add(classes, token) }
|
|
26
|
+
end
|
|
27
|
+
classes.values
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The classes an element receives: the merge without its tombstones.
|
|
31
|
+
def dom(strings)
|
|
32
|
+
merge(strings).reject { |token| token.end_with?("_") }.join(" ")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def add(classes, token)
|
|
38
|
+
finish = token.rindex("_")
|
|
39
|
+
unless token.start_with?(@prefix) && finish
|
|
40
|
+
classes[" #{token}"] = token
|
|
41
|
+
return classes
|
|
42
|
+
end
|
|
43
|
+
key = token[@prefix.length...finish]
|
|
44
|
+
head = key.split("-", 2).first.to_s
|
|
45
|
+
remove_longhands(classes, key, head) if head.length < 3
|
|
46
|
+
slash = key.index("/")
|
|
47
|
+
if slash&.positive?
|
|
48
|
+
classes = unfold(classes, key, slash)
|
|
49
|
+
elsif key.end_with?("*")
|
|
50
|
+
# A target's own `-xyz*` tombstone has the length of a condition key and must survive.
|
|
51
|
+
base = key[0...-1]
|
|
52
|
+
classes.delete_if { |existing, _| scoped?(existing, base, true) && !existing.end_with?("*") }
|
|
53
|
+
end
|
|
54
|
+
classes.delete(key)
|
|
55
|
+
classes[key] = token
|
|
56
|
+
classes
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# A box family atom (or its tombstone) replaces its longhands, whose codes follow its own (D-045).
|
|
60
|
+
def remove_longhands(classes, key, head)
|
|
61
|
+
index = head.each_char.reduce(0) do |sum, digit|
|
|
62
|
+
unit = digit.ord
|
|
63
|
+
(sum * 62) + unit - (if unit > 96 then 87 elsif unit > 64 then 29 else 48 end)
|
|
64
|
+
end
|
|
65
|
+
size = if index < @four
|
|
66
|
+
(index % 5).zero? ? 4 : 0
|
|
67
|
+
else
|
|
68
|
+
index < @two && ((index - @four) % 3).zero? ? 2 : 0
|
|
69
|
+
end
|
|
70
|
+
rest = key[head.length..]
|
|
71
|
+
size.downto(1) { |offset| classes.delete(code(index + offset) + rest) }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# `{shorthand}/{kept codes}{target}{-conditions | *}`: replace earlier shorthand atoms by atoms of
|
|
75
|
+
# the kept longhands in their place, reusing each atom's value (D-045).
|
|
76
|
+
def unfold(earlier, key, slash)
|
|
77
|
+
every = key.end_with?("*")
|
|
78
|
+
tail = every ? key[(slash + 1)...-1] : key[(slash + 1)..]
|
|
79
|
+
dash = tail.index("-")
|
|
80
|
+
codes = dash ? tail[0...dash] : tail
|
|
81
|
+
scope = dash ? tail[dash..] : ""
|
|
82
|
+
shorthand = key[0...slash]
|
|
83
|
+
classes = {}
|
|
84
|
+
earlier.each do |existing, atom|
|
|
85
|
+
if !atom.end_with?("_") && scoped?(existing, shorthand + scope, every)
|
|
86
|
+
rest = existing[shorthand.length..]
|
|
87
|
+
codes.split(".").each do |kept|
|
|
88
|
+
kept += rest
|
|
89
|
+
# A longhand entry after the atom (not copied yet) was written after it and keeps its
|
|
90
|
+
# value; one before it is the atom's own tombstone or an earlier, broader atom's value.
|
|
91
|
+
deleted = classes.key?(kept)
|
|
92
|
+
classes.delete(kept)
|
|
93
|
+
classes[kept] = @prefix + kept + atom[atom.rindex("_")..] if deleted || !earlier.key?(kept)
|
|
94
|
+
end
|
|
95
|
+
else
|
|
96
|
+
classes[existing] = atom
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
classes
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# `key` is `base`, or — for every condition set — `base` plus `-` and a 4-character hash.
|
|
103
|
+
def scoped?(key, base, every)
|
|
104
|
+
key == base || (every && key.start_with?("#{base}-") && key.length == base.length + 5)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# A base-62 property code (`0-9a-zA-Z`) for an index, as class names spell them (D-037).
|
|
108
|
+
def code(index)
|
|
109
|
+
prefix = index > 61 ? code(index / 62) : ""
|
|
110
|
+
index %= 62
|
|
111
|
+
prefix + (if index < 10 then index + 48 elsif index < 36 then index + 87 else index + 29 end).chr
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/railtie"
|
|
4
|
+
|
|
5
|
+
module Kirana
|
|
6
|
+
# Rails: the `kirana` view helper in every view, and `config.kirana.classes` for where the class
|
|
7
|
+
# map is (default `public/kirana/classes.json`).
|
|
8
|
+
class Railtie < ::Rails::Railtie
|
|
9
|
+
config.kirana = ActiveSupport::OrderedOptions.new
|
|
10
|
+
|
|
11
|
+
initializer "kirana.helper" do |app|
|
|
12
|
+
Kirana.classes_path = (app.config.kirana.classes || app.root.join("public/kirana/classes.json")).to_s
|
|
13
|
+
ActiveSupport.on_load(:action_view) { include Kirana::Helper }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/kirana.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "kirana/merge"
|
|
4
|
+
require_relative "kirana/class_map"
|
|
5
|
+
|
|
6
|
+
module Kirana
|
|
7
|
+
# Loads the Railtie when Rails is present; without it the gem is the class map alone.
|
|
8
|
+
def self.integrate(rails = defined?(::Rails::Railtie))
|
|
9
|
+
require_relative "kirana/railtie" if rails
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Kirana.integrate
|
metadata
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kirana
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0.pre1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- The Kirana authors
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
executables: []
|
|
13
|
+
extensions: []
|
|
14
|
+
extra_rdoc_files: []
|
|
15
|
+
files:
|
|
16
|
+
- LICENSE
|
|
17
|
+
- README.md
|
|
18
|
+
- lib/kirana.rb
|
|
19
|
+
- lib/kirana/class_map.rb
|
|
20
|
+
- lib/kirana/merge.rb
|
|
21
|
+
- lib/kirana/railtie.rb
|
|
22
|
+
homepage: https://kirana.style
|
|
23
|
+
licenses:
|
|
24
|
+
- MIT
|
|
25
|
+
metadata:
|
|
26
|
+
rubygems_mfa_required: 'true'
|
|
27
|
+
source_code_uri: https://github.com/kirana-style/kirana/tree/main/packages/kirana/helpers/ruby
|
|
28
|
+
bug_tracker_uri: https://github.com/kirana-style/kirana/issues
|
|
29
|
+
rdoc_options: []
|
|
30
|
+
require_paths:
|
|
31
|
+
- lib
|
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '3.3'
|
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
requirements: []
|
|
43
|
+
rubygems_version: 4.0.16
|
|
44
|
+
specification_version: 4
|
|
45
|
+
summary: 'Kirana for Rails templates: the class map reader and the kirana view helper.'
|
|
46
|
+
test_files: []
|