endleaf 0.1.1 → 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: 3b87bcf46951a064d0f7096c84934fa9b5a59b7a3027c97b68e4ae24b7262ede
4
- data.tar.gz: ba5ed3ff9cff7c16113e91ce28f0e7c5daf222f24b90307a79d8fe805a84ef6e
3
+ metadata.gz: 1dce8df1c349a41f7306c53756797ceb9ca48596ea457f09b1dd3af5be062808
4
+ data.tar.gz: 0eedffd912530827f20871c74510b1ffa755bb87406d278be90e2d6cfecd7aa9
5
5
  SHA512:
6
- metadata.gz: 80bd03da2778c6aa1f3506c806974ba0c055185deea8fee2c16763b1c0d034b88d367d5f1aa7ccee109f7f9470525a07a0b966c270bbf4815ebe07524c1e4544
7
- data.tar.gz: 231eab7930780dcf295dbcb531f05ce2486e41ba6cfbefa9ae0b9ba25845c9c3023db47b6b4d8667f7497b6d40037f9a3e21c3e5d4548b67c856e6ce9436c657
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.1"
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.1
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
@@ -47,7 +47,7 @@ licenses:
47
47
  metadata:
48
48
  homepage_uri: https://github.com/wittawasw/endleaf
49
49
  source_code_uri: https://github.com/wittawasw/endleaf
50
- changelog_uri: https://github.com/wittawasw/endleaf
50
+ changelog_uri: https://github.com/wittawasw/endleaf/blob/main/changelog.md
51
51
  post_install_message:
52
52
  rdoc_options: []
53
53
  require_paths:
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
65
  requirements: []
66
- rubygems_version: 3.4.19
66
+ rubygems_version: 3.4.20
67
67
  signing_key:
68
68
  specification_version: 4
69
69
  summary: Routing and utilities for static pages creation in Rails.