flora 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 40ed44ae102275fee1de3f04afaced4471d977191202551df9e0f623b8b0964f
4
+ data.tar.gz: bdb9bde15da9c73dda13b5ec1fed25821ff8f66526e9d86abf75840673d44338
5
+ SHA512:
6
+ metadata.gz: b744c499d5047e8f33ee77fab63c41a4ac812b10e464a38456cd45a302187aba1e9fc929241ac8a49da74e58d2b27d8eee7e84aeba33ba69d887374079bfa87f
7
+ data.tar.gz: 4a489b8bee42df766d7eaeff185df836f1f632f4aa483a1865021e107e43c4336bd7e62d5731d7b91886b334ad42ddf46f37e70b3ae6e1e1d49116a1db180667
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Nick Vladimiroff
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Flora
2
+
3
+ A static site generator.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
data/exe/flora ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'flora'
3
+
4
+ Flora::Cli.start
data/lib/flora/app.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'rack'
2
+
3
+ class Flora::App
4
+
5
+ # UGLY. Is there a better way to do this?
6
+ class StaticWithoutHtml
7
+
8
+ def initialize(app, dir)
9
+ @app = app
10
+ @dir = dir
11
+ end
12
+
13
+ def call(env)
14
+ # /about -> about.html
15
+ html_file = @dir.join(env['REQUEST_PATH'][1..] + '.html')
16
+ if html_file.exist?
17
+ [200, {'Content-Type' => 'text/html'}, [html_file.read]]
18
+ else
19
+ @app.call(env)
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+
26
+ def self.app(path)
27
+ flora = Flora::Engine.new(path, '/tmp/flora/')
28
+ # TODO: rebuild every req?
29
+ flora.build
30
+
31
+ Rack::Builder.new do
32
+ use StaticWithoutHtml, flora.out_dir
33
+ use Rack::Static, urls: [''], root: flora.out_dir.to_s, index: 'index.html'
34
+ map "/" do
35
+ run ->(env) do
36
+ [404, {'Content-Type' => 'text/plain'}, ['Page Not Found!']]
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ end
data/lib/flora/cli.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'thor'
2
+
3
+ class Flora::Cli < Thor
4
+
5
+ desc 'build OUT_DIR', 'Build a site'
6
+ def build(out_dir)
7
+ Flora::Engine.new('.', out_dir).build
8
+ end
9
+
10
+
11
+ desc 'serve', 'Serve a site'
12
+ def serve
13
+ require 'puma'
14
+ require 'puma/configuration'
15
+
16
+ conf = Puma::Configuration.new do |user_config|
17
+ user_config.log_requests(true)
18
+ user_config.bind('tcp://localhost:3000')
19
+ user_config.app(Flora::App.app('.'))
20
+ end
21
+ Puma::Launcher.new(conf, log_writer: Puma::LogWriter.stdio).run
22
+ end
23
+
24
+ end
@@ -0,0 +1,15 @@
1
+ class Flora::Engine::Config
2
+
3
+ attr_reader(:plugins)
4
+
5
+
6
+ def initialize
7
+ @plugins = []
8
+ end
9
+
10
+
11
+ def plugin(mod)
12
+ @plugins << mod
13
+ end
14
+
15
+ end
@@ -0,0 +1,90 @@
1
+ module Flora::Engine::Page::Html
2
+
3
+ TAGS = %i[
4
+ a abbr address area article aside audio
5
+ b bdi bdo blockquote body br button
6
+ canvas caption cite code col colgroup command
7
+ datalist dd del details dfn div dl dt
8
+ em embed
9
+ fieldset figcaption figure footer form
10
+ h1 h2 h3 h4 h5 h6 head header hr html i iframe img input ins
11
+ kbd keygen
12
+ label legend li
13
+ main map mark menu meter
14
+ nav
15
+ object ol optgroup option output
16
+ p param pre progress
17
+ q
18
+ rp rt ruby
19
+ s samp section select small source span strong sub summary sup
20
+ table tbody td textarea tfoot th thead time tr track
21
+ u ul
22
+ var video
23
+ wbr
24
+ ]
25
+
26
+
27
+ class << self
28
+
29
+ def to_html(tree)
30
+ document = Nokogiri::HTML5::Document.new
31
+ root = to_html_inner(tree, document)
32
+ document.add_child(root)
33
+ document.to_html
34
+ end
35
+
36
+
37
+ private
38
+
39
+ def to_html_inner(tree, document)
40
+ if tree[:text]
41
+ return document.create_text_node(tree[:text])
42
+ end
43
+
44
+ if tree[:html]
45
+ return document.fragment("<div>#{tree[:html]}</div>")
46
+ end
47
+
48
+ node = document.create_element(tree[:tag].to_s, **tree[:opts])
49
+
50
+ tree[:children].each do |child|
51
+ child_node = to_html_inner(child, document)
52
+ node.add_child(child_node)
53
+ end
54
+
55
+ node
56
+ end
57
+
58
+ end
59
+
60
+
61
+ TAGS.each do |tag|
62
+ define_method(tag) do |*args, **opts, &block|
63
+ children = []
64
+
65
+ if block
66
+ old_added = $flora_added
67
+ $flora_added = []
68
+ result = block.call
69
+ if result.is_a?(String)
70
+ children = [{ tag: 'text', opts: {}, text: result, children: [] }]
71
+ else
72
+ children = $flora_added
73
+ end
74
+ $flora_added = old_added
75
+ end
76
+
77
+ node = { tag:, opts:, children: }
78
+ $flora_added << node
79
+ node
80
+ end
81
+ end
82
+
83
+
84
+ def raw_html(string)
85
+ node = { tag: 'raw', opts: {}, html: string, children: [] }
86
+ $flora_added << node
87
+ node
88
+ end
89
+
90
+ end
@@ -0,0 +1,29 @@
1
+ class Flora::Engine::Page::Layout
2
+
3
+ def initialize(layouts)
4
+ @layouts = layouts
5
+ end
6
+
7
+
8
+ def render(&block)
9
+ render_internal(@layouts.size - 1, &block)
10
+ end
11
+
12
+
13
+ private
14
+
15
+ def render_internal(i, &block)
16
+ if i <= 0
17
+ return block.call
18
+ end
19
+
20
+ cont = -> { render_internal(i-1, &block) }
21
+ eval_with_block(@layouts[i].read, &cont)
22
+ end
23
+
24
+
25
+ def eval_with_block(str, &block)
26
+ eval(str)
27
+ end
28
+
29
+ end
@@ -0,0 +1,20 @@
1
+ class Flora::Engine::Page::MarkdownPage < Flora::Engine::Page
2
+
3
+ def render
4
+ silence_warnings do
5
+ raw_html(Kramdown::Document.new(@file.read).to_html)
6
+ end
7
+ end
8
+
9
+
10
+ private
11
+
12
+ # Remove this if kramdown ever fixes its one warning.
13
+ def silence_warnings
14
+ old_verbose, $VERBOSE = $VERBOSE, nil
15
+ yield
16
+ ensure
17
+ $VERBOSE = old_verbose
18
+ end
19
+
20
+ end
@@ -0,0 +1,10 @@
1
+ module Flora::Engine::Page::PluginHooks
2
+
3
+ def before_render
4
+ end
5
+
6
+
7
+ def after_render(html)
8
+ end
9
+
10
+ end
@@ -0,0 +1,8 @@
1
+ class Flora::Engine::Page::RubyPage < Flora::Engine::Page
2
+
3
+ def render
4
+ super
5
+ instance_eval(@file.read)
6
+ end
7
+
8
+ end
@@ -0,0 +1,56 @@
1
+ # A page is something can be turned into HTML. It might also have a layout to render into.
2
+ class Flora::Engine::Page
3
+
4
+ IGNORES = ['.git/*', 'lib/*', '**_layout.rb', '_config.rb']
5
+
6
+
7
+ class << self
8
+
9
+ def each(path)
10
+ path.find do |file|
11
+ next if IGNORES.any? { file.fnmatch((path / it).to_s) }
12
+ next if file.directory?
13
+
14
+ yield(self.for(path, file))
15
+ end
16
+ end
17
+
18
+
19
+ def for(root, file)
20
+ case file.extname
21
+ when '.rb'
22
+ RubyPage.new(root, file)
23
+ when '.md'
24
+ MarkdownPage.new(root, file)
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+
31
+ def initialize(root, file)
32
+ @root = root
33
+ @file = file
34
+ end
35
+
36
+
37
+ def render
38
+ end
39
+
40
+
41
+ def outname
42
+ # TODO: this might sub something in the middle instead of just the ext.
43
+ @file.relative_path_from(@root).sub(@file.extname, '.html')
44
+ end
45
+
46
+
47
+ def url
48
+ @file.relative_path_from(@root).sub(@file.extname, '')
49
+ end
50
+
51
+
52
+ def dir
53
+ @file.dirname
54
+ end
55
+
56
+ end
@@ -0,0 +1,61 @@
1
+ class Flora::Engine
2
+
3
+ attr_reader(:out_dir)
4
+
5
+
6
+ def initialize(path, out_dir)
7
+ @path = Pathname.new(path)
8
+ @out_dir = Pathname.new(out_dir)
9
+ @out_dir.mkdir unless @out_dir.exist?
10
+ @config = Config.new
11
+
12
+ Kernel.prepend(Flora::Engine::Page::Html)
13
+ end
14
+
15
+
16
+ def build
17
+ with_load_path(@path) do
18
+ config_file = @path.join('_config.rb')
19
+ @config.instance_eval(config_file.read) if config_file.exist?
20
+
21
+ init_plugins
22
+
23
+ Page.each(@path) do |page|
24
+ layouts = []
25
+ page.dir.ascend do |dir|
26
+ maybe_layout = dir.join('_layout.rb')
27
+ layouts << maybe_layout if maybe_layout.exist?
28
+ break if dir == @path
29
+ end
30
+
31
+ $flora_added = []
32
+ tree = Page::Layout.new(layouts).render do
33
+ page.render
34
+ end
35
+ html = Page::Html.to_html(tree)
36
+
37
+ out_filename = @out_dir.join(page.outname)
38
+ out_filename.dirname.mkdir unless out_filename.dirname.exist?
39
+ out_filename.write(html)
40
+ end
41
+ end
42
+ end
43
+
44
+
45
+ private
46
+
47
+ def with_load_path(path)
48
+ $LOAD_PATH << path.to_s
49
+ yield
50
+ $LOAD_PATH.delete(path.to_s)
51
+ end
52
+
53
+
54
+ def init_plugins
55
+ @config.plugins.each do |plugin|
56
+ Page.include(plugin::Page) if self.class.const_defined?("#{plugin}::Page")
57
+ Page::RubyPage.include(plugin::RubyPage) if self.class.const_defined?("#{plugin}::RubyPage")
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,3 @@
1
+ module Flora
2
+ VERSION = "0.1.1"
3
+ end
data/lib/flora.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'zeitwerk'
2
+ require 'nokogiri'
3
+ require 'kramdown'
4
+
5
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
6
+ loader.setup
7
+
8
+ module Flora
9
+ end
@@ -0,0 +1,37 @@
1
+ class FloraBlog::Post
2
+
3
+ def initialize(page)
4
+ @page = page
5
+ @frontmatter = parse_frontmatter
6
+ end
7
+
8
+
9
+ def url
10
+ 'https://google.com'
11
+ end
12
+
13
+
14
+ def title
15
+ 'google'
16
+ end
17
+
18
+
19
+ def method_missing(name, ...)
20
+ return @frontmatter[name] if @frontmatter[name]
21
+
22
+ super(name, ...)
23
+ end
24
+
25
+
26
+ private
27
+
28
+ def parse_frontmatter
29
+ page_data = @page.read
30
+
31
+ return {} unless page_data.start_with?('---')
32
+ /(?<=---\n)(?<yaml>.*)(?=---\n)/m =~ @page.read
33
+
34
+ YAML.load(yaml)
35
+ end
36
+
37
+ end
data/lib/flora_blog.rb ADDED
@@ -0,0 +1,13 @@
1
+ module FloraBlog
2
+
3
+ module RubyPage
4
+
5
+ def render
6
+ @posts = @root.glob('posts/**').map { Post.new(it) }
7
+
8
+ super
9
+ end
10
+
11
+ end
12
+
13
+ end
data/tmp/.keep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flora
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nick Vladimiroff
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: zeitwerk
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: thor
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: nokogiri
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: kramdown
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rack
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ - !ruby/object:Gem::Dependency
83
+ name: puma
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ email:
97
+ - nickvladimiroff@hey.com
98
+ executables:
99
+ - flora
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - exe/flora
107
+ - lib/flora.rb
108
+ - lib/flora/app.rb
109
+ - lib/flora/cli.rb
110
+ - lib/flora/engine.rb
111
+ - lib/flora/engine/config.rb
112
+ - lib/flora/engine/page.rb
113
+ - lib/flora/engine/page/html.rb
114
+ - lib/flora/engine/page/layout.rb
115
+ - lib/flora/engine/page/markdown_page.rb
116
+ - lib/flora/engine/page/plugin_hooks.rb
117
+ - lib/flora/engine/page/ruby_page.rb
118
+ - lib/flora/version.rb
119
+ - lib/flora_blog.rb
120
+ - lib/flora_blog/post.rb
121
+ - tmp/.keep
122
+ homepage: https://code.kat5.dev/nick/flora
123
+ licenses:
124
+ - MIT
125
+ metadata:
126
+ homepage_uri: https://code.kat5.dev/nick/flora
127
+ source_code_uri: https://code.kat5.dev/nick/flora
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: 3.2.0
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 4.0.0.dev
143
+ specification_version: 4
144
+ summary: A static site generator.
145
+ test_files: []