endleaf 0.1.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -1
- data/app/controllers/endleaf/pages_controller.rb +21 -2
- data/lib/endleaf/config.rb +6 -0
- data/lib/endleaf/version.rb +1 -1
- data/lib/endleaf.rb +1 -0
- data/lib/tasks/endleaf/render.rake +38 -15
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90f0e34500f5b54264548be3dcc719321a373698836ab9e4947895f76525ec7e
|
4
|
+
data.tar.gz: 786a0759dc2c2dc0d685bfb94e9adc560476a86aeb25240b9d5add113b64a3f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4304bce0e6ccd3d3e56f272b948d2dbf8ef7de06c016f25452ee9994ad494450222be0214dae79e9000bec431737dc7a08af753e60a9f3d52dfc785a2c8f87fd
|
7
|
+
data.tar.gz: 3353e33205c82259354930e068d08d6c54e7f9e3baee42860151e2980393b19ec4497e35c6d0a185be323362fab01ea29522337befa0834d6c1b54ed1f6945bd
|
data/README.md
CHANGED
@@ -10,14 +10,27 @@ gem "endleaf"
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
- Use with Ruby on Rails only.
|
13
|
+
- Create pages in `app/views/pages` with filenames corresponding to their routes.
|
13
14
|
- Add routes to `config/routes.rb`
|
14
15
|
|
15
16
|
```ruby
|
17
|
+
# Simple usage
|
16
18
|
scope module: 'endleaf' do
|
17
19
|
get '/pages/:page', to: 'pages#show', as: :page
|
18
20
|
end
|
21
|
+
|
22
|
+
# Use scope and constraints to achieve routes into sub directories
|
23
|
+
scope "/pages", constraints: { path: /pages\/.*/ } do
|
24
|
+
get "*page", to: "endleaf/pages#show"
|
25
|
+
end
|
26
|
+
|
27
|
+
# With custom path and layout
|
28
|
+
# path: app/views/posts
|
29
|
+
# layout: app/views/layouts/posts.html.erb
|
30
|
+
scope "/posts", constraints: { path: /posts\/.*/ } do
|
31
|
+
get "*page", to: "endleaf/pages#show", as: :endleaf_posts_post, defaults: { path: "posts", layout: "posts" }
|
32
|
+
end
|
19
33
|
```
|
20
|
-
- Create pages in `app/views/pages` with filenames corresponding to their routes.
|
21
34
|
|
22
35
|
```ruby
|
23
36
|
# can be used in specific routes e.g. homepage
|
@@ -32,6 +45,14 @@ get 'endleaf/pages#show' , page: 'about'
|
|
32
45
|
|
33
46
|
```shell
|
34
47
|
bundle exec rake endleaf:render:generate_html
|
48
|
+
|
49
|
+
# custom path, layout, output with variables [path,layout,output]
|
50
|
+
# Example:
|
51
|
+
# path: app/views/posts
|
52
|
+
# layout: app/views/layouts/posts.html.erb
|
53
|
+
# output: public/static/posts
|
54
|
+
bundle exec rake endleaf:render:generate_html\[posts,posts,static\/posts\]
|
55
|
+
|
35
56
|
```
|
36
57
|
|
37
58
|
|
@@ -3,13 +3,32 @@ module Endleaf
|
|
3
3
|
before_action :set_layout
|
4
4
|
|
5
5
|
def show
|
6
|
-
|
6
|
+
path_segments = params[:page].split('/')
|
7
|
+
view_path = path_segments.join('/')
|
8
|
+
|
9
|
+
render template: "#{path}/#{view_path}"
|
7
10
|
end
|
8
11
|
|
9
12
|
private
|
10
13
|
|
11
14
|
def set_layout
|
12
|
-
self.class.layout(
|
15
|
+
self.class.layout(page_layout)
|
16
|
+
end
|
17
|
+
|
18
|
+
def page_layout
|
19
|
+
unless params[:layout].blank?
|
20
|
+
params[:layout]
|
21
|
+
else
|
22
|
+
Endleaf::Config::DEFAULT_LAYOUT
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def path
|
27
|
+
unless params[:path].blank?
|
28
|
+
params[:path]
|
29
|
+
else
|
30
|
+
Endleaf::Config::DEFAULT_PATH
|
31
|
+
end
|
13
32
|
end
|
14
33
|
end
|
15
34
|
end
|
data/lib/endleaf/version.rb
CHANGED
data/lib/endleaf.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
namespace :endleaf do
|
2
2
|
namespace :render do
|
3
|
-
desc
|
3
|
+
desc "Render and save static pages as HTML"
|
4
4
|
|
5
|
-
task :generate_html => :environment do
|
6
|
-
#
|
7
|
-
controller_path =
|
8
|
-
action_name =
|
5
|
+
task :generate_html, [:path, :layout, :output] => :environment do |t, args|
|
6
|
+
# Manually set the controller path and action name
|
7
|
+
controller_path = "endleaf/pages"
|
8
|
+
action_name = "show"
|
9
|
+
|
10
|
+
# Set static pages directory and layout
|
11
|
+
page_path = args[:path] || Endleaf::Config::DEFAULT_PATH
|
12
|
+
layout = args[:layout] || Endleaf::Config::DEFAULT_LAYOUT
|
13
|
+
output = args[:output].to_s
|
9
14
|
|
10
|
-
# Create a controller instance
|
11
15
|
controller = ActionController::Base.new
|
12
16
|
controller.request = ActionDispatch::Request.new({})
|
13
17
|
|
@@ -15,23 +19,42 @@ namespace :endleaf do
|
|
15
19
|
controller.params = { controller: controller_path, action: action_name }
|
16
20
|
|
17
21
|
# Define the directory containing your static pages within the engine
|
18
|
-
pages_directory = Rails.root.join(
|
22
|
+
pages_directory = Rails.root.join("app", "views", page_path)
|
23
|
+
|
24
|
+
# Supported template extensions
|
25
|
+
template_extensions = %w[html.erb erb html.haml haml html.slim slim]
|
26
|
+
|
27
|
+
puts "#{pages_directory}/**/*.{#{template_extensions.join(',')}}"
|
19
28
|
|
20
|
-
# Loop through each file in the pages directory
|
21
|
-
Dir.glob("#{pages_directory}
|
22
|
-
# Extract the
|
23
|
-
|
29
|
+
# Loop through each file in the pages directory and subdirectories
|
30
|
+
Dir.glob("#{pages_directory}/**/*.{#{template_extensions.join(',')}}") do |file|
|
31
|
+
# Extract the relative path from the pages directory
|
32
|
+
relative_path = Pathname.new(file).relative_path_from(pages_directory).to_s
|
24
33
|
|
25
|
-
#
|
26
|
-
|
34
|
+
# Extract the filename without the extension
|
35
|
+
filename = File.basename(file, '.*')
|
36
|
+
|
37
|
+
# Construct the absolute path to the template file
|
38
|
+
template_path = "#{page_path}/#{relative_path.sub(/\.(#{template_extensions.join('|')})$/, '')}"
|
39
|
+
puts "template: #{template_path}"
|
40
|
+
|
41
|
+
# Render the page using the controller's render_to_string method
|
42
|
+
html_content = controller.render_to_string(
|
43
|
+
template: template_path,
|
44
|
+
layout: layout
|
45
|
+
)
|
27
46
|
|
28
47
|
# Define the path to save the HTML file in the public directory
|
29
|
-
save_path
|
48
|
+
# Modify save_path logic to generate 'example/index.html' format
|
49
|
+
save_path = Rails.root.join('public', output, "#{relative_path.sub(/\.html\.erb$/, '')}/index.html")
|
50
|
+
|
51
|
+
# Create directories if they don't exist
|
52
|
+
FileUtils.mkdir_p(File.dirname(save_path))
|
30
53
|
|
31
54
|
# Save the rendered HTML to the file
|
32
55
|
File.write(save_path, html_content)
|
33
56
|
|
34
|
-
puts "Rendered and saved #{
|
57
|
+
puts "Rendered and saved #{save_path}"
|
35
58
|
end
|
36
59
|
end
|
37
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: endleaf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wittawas W
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- app/controllers/endleaf/pages_controller.rb
|
38
38
|
- app/views/layouts/endleaf.html.erb
|
39
39
|
- lib/endleaf.rb
|
40
|
+
- lib/endleaf/config.rb
|
40
41
|
- lib/endleaf/engine.rb
|
41
42
|
- lib/endleaf/railtie.rb
|
42
43
|
- lib/endleaf/version.rb
|
@@ -47,7 +48,7 @@ licenses:
|
|
47
48
|
metadata:
|
48
49
|
homepage_uri: https://github.com/wittawasw/endleaf
|
49
50
|
source_code_uri: https://github.com/wittawasw/endleaf
|
50
|
-
changelog_uri: https://github.com/wittawasw/endleaf
|
51
|
+
changelog_uri: https://github.com/wittawasw/endleaf/blob/main/changelog.md
|
51
52
|
post_install_message:
|
52
53
|
rdoc_options: []
|
53
54
|
require_paths:
|
@@ -63,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
64
|
- !ruby/object:Gem::Version
|
64
65
|
version: '0'
|
65
66
|
requirements: []
|
66
|
-
rubygems_version: 3.4.
|
67
|
+
rubygems_version: 3.4.20
|
67
68
|
signing_key:
|
68
69
|
specification_version: 4
|
69
70
|
summary: Routing and utilities for static pages creation in Rails.
|