endleaf 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e57280cf9e74833917adf64e42296f18550963bab92d7c2ccb98e7ad735d55aa
4
- data.tar.gz: e4ebb14b254fd6614d1cf88f396ebcff36028256035a03a0d159c251347ff8f5
3
+ metadata.gz: 1dce8df1c349a41f7306c53756797ceb9ca48596ea457f09b1dd3af5be062808
4
+ data.tar.gz: 0eedffd912530827f20871c74510b1ffa755bb87406d278be90e2d6cfecd7aa9
5
5
  SHA512:
6
- metadata.gz: 73b9cd8dd06eed1b9c5abb277339a41b4cad7941c2d29c2196e13237848250738b35a05793a7b0d2a3f7c3d96639209b9ed85a271a06328657f5220b827a3a50
7
- data.tar.gz: 23fa2ceffaa1f8f0c466f07d4cc0645c69d5b20c73a396696c4dc3ca321cc7b1dbe77248d081bb59dff725801f77b9eef97598adb535f81c97236804f038f11c
6
+ metadata.gz: d9811eaf75bab78144af3a6aff8c46990bd6295bc923c8ebb5d37eb1b27cfd1e5a4e7f277f652abacba70a499de98c089c14fb638afe8f9a5c5f30fb25a90828
7
+ data.tar.gz: 9876dce6918c40c5dfc72da5f5c147e91183ec73619fa0075425522067d755e186952ab6837de368f5fdcc79bd31d5f216e45c0e1235ffaba31f9772ec856296
data/README.md CHANGED
@@ -10,14 +10,20 @@ 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
19
26
  ```
20
- - Create pages in `app/views/pages` with filenames corresponding to their routes.
21
27
 
22
28
  ```ruby
23
29
  # can be used in specific routes e.g. homepage
@@ -3,7 +3,10 @@ module Endleaf
3
3
  before_action :set_layout
4
4
 
5
5
  def show
6
- render template: "pages/#{params[:page]}"
6
+ path_segments = params[:page].split('/')
7
+ view_path = path_segments.join('/')
8
+
9
+ render template: "pages/#{view_path}"
7
10
  end
8
11
 
9
12
  private
@@ -1,3 +1,3 @@
1
1
  module Endleaf
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,35 +3,56 @@ namespace :endleaf do
3
3
  desc 'Render and save static pages as HTML'
4
4
 
5
5
  task :generate_html => :environment do
6
- # Include the necessary modules to access render_to_string
6
+ # Manually set the controller path and action name
7
7
  controller_path = 'endleaf/pages'
8
8
  action_name = 'show'
9
9
 
10
- # Create a controller instance
11
10
  controller = ActionController::Base.new
12
11
  controller.request = ActionDispatch::Request.new({})
13
12
 
14
13
  # Set the controller path and action name
15
14
  controller.params = { controller: controller_path, action: action_name }
16
15
 
16
+ # Set static pages directory
17
+ pages_dir = "pages"
18
+
17
19
  # Define the directory containing your static pages within the engine
18
- pages_directory = Rails.root.join('app', 'views', 'pages')
20
+ pages_directory = Rails.root.join("app", "views", pages_dir)
21
+
22
+ # Supported template extensions
23
+ template_extensions = %w[html.erb erb html.haml haml html.slim slim]
24
+
25
+ puts "#{pages_directory}/**/*.{#{template_extensions.join(',')}}"
19
26
 
20
- # Loop through each file in the pages directory
21
- Dir.glob("#{pages_directory}/*.html.erb") do |file|
22
- # Extract the filename without the directory and extension
23
- filename = File.basename(file, '.html.erb')
27
+ # Loop through each file in the pages directory and subdirectories
28
+ Dir.glob("#{pages_directory}/**/*.{#{template_extensions.join(',')}}") do |file|
29
+ # Extract the relative path from the pages directory
30
+ relative_path = Pathname.new(file).relative_path_from(pages_directory).to_s
24
31
 
25
- # Render the page
26
- html_content = controller.render_to_string(template: "pages/#{filename}", layout: 'endleaf')
32
+ # Extract the filename without the extension
33
+ filename = File.basename(file, '.*')
34
+
35
+ # Construct the absolute path to the template file
36
+ template_path = "#{pages_dir}/#{relative_path.sub(/\.(#{template_extensions.join('|')})$/, '')}"
37
+ puts "template: #{template_path}"
38
+
39
+ # Render the page using the controller's render_to_string method
40
+ html_content = controller.render_to_string(
41
+ template: template_path,
42
+ layout: "endleaf"
43
+ )
27
44
 
28
45
  # Define the path to save the HTML file in the public directory
29
- save_path = Rails.root.join('public', "#{filename}.html")
46
+ # Modify save_path logic to generate 'example/index.html' format
47
+ save_path = Rails.root.join('public', "#{relative_path.sub(/\.html\.erb$/, '')}/index.html")
48
+
49
+ # Create directories if they don't exist
50
+ FileUtils.mkdir_p(File.dirname(save_path))
30
51
 
31
52
  # Save the rendered HTML to the file
32
53
  File.write(save_path, html_content)
33
54
 
34
- puts "Rendered and saved #{filename}.html"
55
+ puts "Rendered and saved #{save_path}"
35
56
  end
36
57
  end
37
58
  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.1.0
4
+ version: 0.2.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-09-28 00:00:00.000000000 Z
11
+ date: 2023-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -36,7 +36,6 @@ files:
36
36
  - Rakefile
37
37
  - app/controllers/endleaf/pages_controller.rb
38
38
  - app/views/layouts/endleaf.html.erb
39
- - config/routes.rb
40
39
  - lib/endleaf.rb
41
40
  - lib/endleaf/engine.rb
42
41
  - lib/endleaf/railtie.rb
@@ -48,7 +47,7 @@ licenses:
48
47
  metadata:
49
48
  homepage_uri: https://github.com/wittawasw/endleaf
50
49
  source_code_uri: https://github.com/wittawasw/endleaf
51
- changelog_uri: https://github.com/wittawasw/endleaf
50
+ changelog_uri: https://github.com/wittawasw/endleaf/blob/main/changelog.md
52
51
  post_install_message:
53
52
  rdoc_options: []
54
53
  require_paths:
@@ -64,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
63
  - !ruby/object:Gem::Version
65
64
  version: '0'
66
65
  requirements: []
67
- rubygems_version: 3.4.19
66
+ rubygems_version: 3.4.20
68
67
  signing_key:
69
68
  specification_version: 4
70
69
  summary: Routing and utilities for static pages creation in Rails.
data/config/routes.rb DELETED
@@ -1,5 +0,0 @@
1
- Rails.application.routes.draw do
2
- scope module: 'endleaf' do
3
- get '/pages/:page', to: 'pages#show', as: :page
4
- end
5
- end