scrapbook 0.2.0 → 0.2.2

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: ca937f0e8c26f5b3ea2676b64135cf711e53ce2e17ac407c67abc7f934bfc482
4
- data.tar.gz: 90069e6ac7f4e344b8a6f245dc8e8c2fc02c5b052df51fec6fea7cac7658c9e2
3
+ metadata.gz: 215d5d39bc38386f6e2c7c08e2126e26718305961ae675a3b6d433b13ff5a4d9
4
+ data.tar.gz: 07662ed1539e36da1323a74fcb2f4314ea23546586d86036f90d60635b62c913
5
5
  SHA512:
6
- metadata.gz: d53088db3d75baeca477c9358ffe7c52483e9f13a370d5c1c4a021ec5017f5d0d108bea466d9194547bd6258852ca2e2b47b2dc468ef66126f784302d88be911
7
- data.tar.gz: d337a10422bed9966cb1cba24fa8362b63a8f5afed5e16572dc13248679a0d993c122fd06972ff8fd89eed2257892698ec33052c4d8d6945cabfc9788507d361
6
+ metadata.gz: 2f2c0d4853af4a8fa82f5a9175874c082932ee36eb9825eccfe1f733afa8bb01025feaa70811321d1f118392ef7803a3da9c0b7addf240967262d6a23171fd6c
7
+ data.tar.gz: e6e4215895e268d0bb554c1b7fa88ffb1f60e3e18ae2a8ed29fdd62762585ea6998ef582528a637bb2e55f8f3a048e6599732807bfc10ae8c1a899cbd2ea2120
data/README.md CHANGED
@@ -26,13 +26,14 @@ end
26
26
 
27
27
  And then execute:
28
28
 
29
- ```bash
29
+ ```
30
30
  $> bundle install
31
31
  $> bundle exec rails generate scrapbook:install
32
32
  ```
33
33
 
34
- This will install the gem and setup the default Scrapbook route and create the default
35
- scrapbook directory structure at the root of your Rails application.
34
+ This will install the gem and then setup a scrapbook directory structure with a folder named
35
+ "scrapbook" at the root of your Rails application as well as modify your routes to make the
36
+ scrapbook available.
36
37
 
37
38
  ## Usage
38
39
 
@@ -48,54 +49,48 @@ the main display area describing how to customize what is displayed when a folde
48
49
  selected. In short, you need to create a template file with the same base name as the folder
49
50
  in the same directory as the folder. So if you had a folder at "scrapbook/pages/scratch",
50
51
  then you would need to create a file named something like "scrapbook/pages/scratch.html.erb"
51
- with the custom display you want to see when you click on the "scratch" folder in the file
52
+ with the custom view you want to see when you click on the "scratch" folder in the file
52
53
  browser. This also works for the main screen you see at the base scrapbook URL. You can
53
54
  customize that screen by creating a "pages.html" template file in the root scrapbook folder.
54
55
  (The default installation creates a "pages.html.erb" template file for the basic welcome
55
56
  message.)
56
57
 
57
- To view a scrapbook, navigate to its base URL. The default scarpbook can always be accessed
58
- at the mountpoint setup in the routes file (this is "/scrapbook" by default). The base URL
59
- for other scrapbooks is that path followed by the name of the scrapbook's root folder (the
60
- folder that contains the "pages" folder). By default, only one scrapbook is configured and
61
- it's name is "scrapbook" which means that both "/scrapbook" and "/scrapbook/scrapbook" point
62
- to the same scrapbook root page.
58
+ To view a scrapbook, navigate to its base URL setup by the route file configuration. (See
59
+ the documentation for the `scrapbook` route helper for more information on how to conigure
60
+ this route.)
63
61
 
64
62
  ## Configuration
65
63
 
66
- You can configure the path that Scrapbook runs on in your "config/routes.rb" file. The
67
- default configuration is to mount Scrapbook on "/scrapbook", but you can use whatever
68
- path you want: `mount Scrapbook::Engine => "/scrappy"`.
69
-
70
- You can configure one or more folders to be separate root scrapbooks via the
71
- `config.scrapbook.paths` option. This option defaults to an empty array which allows for
72
- using the shovel operator to add your paths:
73
- ```ruby
74
- Rails.application.configure do
75
- config.scrapbook.paths << Rails.root.join("scrapbooks/main")
76
- config.scrapbook.paths << Rails.root.join("scrapbooks/scratch")
77
- end
78
- ```
79
- If no paths have been added, Scrapbook will use `Rails.root.join('scrapbook')` as the
80
- location it expects the scrapbook to be at. The first folder path in the array is considered
81
- the default scrapbook and will be available at the root mount point configured in the routes
82
- file.
64
+ The installation task adds `extend Scrapbook::Routing` to the routes configuration block
65
+ which gives it access to the `scrapbook` route helper. It also addds a scrapbook named
66
+ "scrapbook" using that helper (`scrapbook('scrapbook')`). You can modify that configuration,
67
+ specifying the location of the folder root or the URL path to mount the scrapbook at. You
68
+ can also add additional scrapbooks using this helper. For example:
69
+
70
+ ```ruby
71
+ Rails.application.routes.draw do
72
+ extend Scrapbook::Routing
73
+
74
+ scrapbook('main', at: '/scrapbook', folder_root: 'scrapbooks/main')
75
+ scrapbook('scratch', folder_root: 'scrapbooks/scratch')
76
+ end
77
+ ```
83
78
 
84
79
  We recommend only running Scrapbook in development / non-production Rails environments.
85
80
  However, if you find yourself needing to be able to run it in an environment precompiles its
86
81
  assets, you will need to configure Scrapbook to be part of the precompilation. You can do
87
82
  this by setting `config.scrapbook.precompile_assets` to "true":
88
- ```ruby
89
- Rails.application.configure do
90
- config.scrapbook.precompile_assets = true
91
- end
92
- ```
83
+ ```ruby
84
+ Rails.application.configure do
85
+ config.scrapbook.precompile_assets = true
86
+ end
87
+ ```
93
88
 
94
89
 
95
90
  ## Contributing
96
91
 
97
- If you have a question about Scrapbook, feel free to ask in [the repository's Discussions]
98
- [Discussions]. Before starting any work or creating any issues, please read [the
92
+ If you have a question about Scrapbook, feel free to ask in [the repository's
93
+ Discussions][Discussions]. Before starting any work or creating any issues, please read [the
99
94
  contribution guidelines](CONTRIBUTING.md).
100
95
 
101
96
  ## Development Setup
@@ -116,7 +111,7 @@ Once Tailwind's CLI has been installed, you can run it using the command below t
116
111
  Scrapbook's CSS:
117
112
 
118
113
  ```
119
- $> npx tailwindcss -i app/assets/stylesheets/scrapbook/application.tailwind.css -o app/assets/builds/scrapbook/tailwind.css --minify --watch
114
+ $> npx tailwindcss -i app/assets/stylesheets/scrapbook/application.tailwind.css -o app/assets/builds/scrapbook/application.css --minify --watch
120
115
  ```
121
116
 
122
117
  (Note, the "tailwindcss-rails" gem currently doesn't support Rails engines, so we have to
@@ -130,7 +125,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
130
125
  you may not use this file except in compliance with the License.
131
126
  You may obtain a copy of the License at
132
127
 
133
- http://www.apache.org/licenses/LICENSE-2.0
128
+ [http://www.apache.org/licenses/LICENSE-2.0]()
134
129
 
135
130
  Unless required by applicable law or agreed to in writing, software
136
131
  distributed under the License is distributed on an "AS IS" BASIS,
@@ -139,4 +134,4 @@ See the License for the specific language governing permissions and
139
134
  limitations under the License.
140
135
 
141
136
 
142
- [Discussion]: https://github.com/bfad/scrapbook/discussions
137
+ [Discussions]: https://github.com/bfad/scrapbook/discussions
@@ -5,42 +5,30 @@ module Scrapbook
5
5
  class PagesController < ApplicationController
6
6
  self.view_paths = Engine.config.paths['app/views'].to_a
7
7
 
8
- def index
9
- return head(:not_found) if (scrapbook = find_scrapbook).nil?
10
- return head(:not_found) unless (pathname = calculate_pathname(scrapbook, params[:path])).directory?
11
-
12
- if pathname == scrapbook.pages_pathname
13
- prepend_view_path(scrapbook.root)
14
- render template: 'pages', locals: {scrapbook: scrapbook, pathname: pathname}
15
- else
16
- render locals: {scrapbook: scrapbook, pathname: pathname}
17
- end
18
- end
19
-
20
8
  def show
21
9
  return head(:not_found) if (scrapbook = find_scrapbook).nil?
22
10
 
23
11
  pathname = calculate_pathname(scrapbook, params[:id])
24
- template = params[:id].delete_suffix('.html')
12
+ template = calculate_template
25
13
 
26
- if !scrapbook_template_exists?(scrapbook, template) && pathname.directory?
27
- render 'scrapbook/pages/index', locals: {scrapbook: scrapbook, pathname: pathname}
28
- else
29
- render locals: {scrapbook: scrapbook, pathname: pathname}, formats: [:html]
30
- end
14
+ render locals: {scrapbook: scrapbook, pathname: pathname}, formats: [:html]
31
15
  end
32
16
 
33
17
  def raw
34
18
  return head(:not_found) if (scrapbook = find_scrapbook).nil?
35
19
 
36
20
  pathname = calculate_pathname(scrapbook, params[:id])
37
- template = params[:id].delete_suffix('.html')
21
+ template = calculate_template
38
22
 
39
23
  if scrapbook_template_exists?(scrapbook, template)
40
- prepend_view_path(scrapbook.pages_pathname)
24
+ prepend_view_path(scrapbook.root)
41
25
  render template: template,
42
26
  locals: {scrapbook: scrapbook, pathname: pathname},
43
27
  layout: 'layouts/scrapbook/host_application'
28
+ elsif pathname.directory?
29
+ render '/pages',
30
+ locals: {scrapbook: scrapbook, pathname: pathname},
31
+ layout: 'layouts/scrapbook/host_application'
44
32
  elsif pathname.exist?
45
33
  render file: pathname
46
34
  else
@@ -51,25 +39,31 @@ module Scrapbook
51
39
  private
52
40
 
53
41
  def find_scrapbook
54
- scrapbook_path = if params[:book].present?
55
- ::Scrapbook::Engine.config.scrapbook.paths.find { File.basename(_1) == params[:book] }
56
- else
57
- ::Scrapbook::Engine.config.scrapbook.paths.first
58
- end
42
+ return nil if book_name.blank?
59
43
 
44
+ scrapbook_path = Engine.config.scrapbook.paths[book_name]
60
45
  scrapbook_path && Scrapbook.new(scrapbook_path)
61
46
  end
62
47
 
63
48
  def calculate_pathname(scrapbook, path)
64
- if path.present?
65
- scrapbook.pages_pathname.join(path)
66
- else
67
- scrapbook.pages_pathname
68
- end
49
+ scrapbook.pages_pathname.join(path || '')
50
+ end
51
+
52
+ def calculate_template
53
+ return 'pages' if params[:id].blank?
54
+
55
+ "pages/#{params[:id].delete_suffix('.html')}"
69
56
  end
70
57
 
71
58
  def scrapbook_template_exists?(scrapbook, template)
72
- EmptyController.new.tap { |c| c.prepend_view_path(scrapbook.pages_pathname) }.template_exists?(template)
59
+ # It's deprecated, but Rails 6 allows for templates to be specified with extensions.
60
+ return false if Rails.version.to_i == 6 && template.include?('.')
61
+
62
+ EmptyController.new.tap { |c| c.prepend_view_path(scrapbook.root) }.template_exists?(template)
63
+ end
64
+
65
+ def book_name
66
+ params[:'.book']
73
67
  end
74
68
  end
75
69
  end
@@ -10,8 +10,7 @@ module Scrapbook
10
10
 
11
11
  def short_path_to(pathname, scrapbook = nil)
12
12
  scrapbook ||= Scrapbook.find_scrapbook_for(pathname)
13
-
14
- view.short_page_path(scrapbook.relative_page_path_for(pathname)).gsub(/%2F/i, '/')
13
+ view.short_page_path(scrapbook.relative_page_path_for(pathname))
15
14
  end
16
15
 
17
16
  def remove_handler_exts_from(pathname)
@@ -9,11 +9,11 @@ module Scrapbook
9
9
 
10
10
  def self.find_scrapbook_for(pathname)
11
11
  scrapbooks = ::Scrapbook::Engine.config.scrapbook.paths
12
- candidates = scrapbooks.each_with_index.filter_map do |pname, index|
12
+ candidates = scrapbooks.filter_map do |book, pname|
13
13
  relative_path = pathname.relative_path_from(pname)
14
14
  next if relative_path.to_s.start_with?('..')
15
15
 
16
- [index, relative_path.each_filename.count]
16
+ [book, relative_path.each_filename.count]
17
17
  end
18
18
  raise NotFoundError if candidates.empty?
19
19
 
@@ -25,7 +25,7 @@ module Scrapbook
25
25
  end
26
26
 
27
27
  def name
28
- root.basename
28
+ root.basename.to_s
29
29
  end
30
30
 
31
31
  def pages_pathname
@@ -1,12 +1,11 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
- <title><%= local_assigns[:scrapbook].name || 'Scrapbook' %>: <%= content_for?(:title) ? yield(:title) : local_assigns[:pathname].basename %></title>
4
+ <title><%= local_assigns[:scrapbook]&.name || 'Scrapbook' %>: <%= content_for?(:title) ? yield(:title) : local_assigns[:pathname]&.basename %></title>
5
5
  <%= csrf_meta_tags %>
6
6
  <%= csp_meta_tag %>
7
7
 
8
- <%= stylesheet_link_tag "scrapbook/tailwind", "data-turbo-track": "reload" %>
9
- <%= stylesheet_link_tag "scrapbook/application", media: "all" %>
8
+ <%= stylesheet_link_tag "scrapbook/application", media: "all", "data-turbo-track": "reload" %>
10
9
  </head>
11
10
  <body class="flex">
12
11
  <nav class="flex-none min-w-[20ch] max-w-[50%] h-screen overflow-scroll children:px-4 py-2 border-solid border-sky-700 border-0 border-r"><%= render 'layouts/scrapbook/folder_listing', scrapbook: local_assigns[:scrapbook], pathname: local_assigns[:pathname] %></nav>
@@ -1 +1,2 @@
1
- This directory has no corresponding template. You can add a template named "pages" to the Scrapbook root directory and its contents will show up here.
1
+ <% if pathname == scrapbook.pages_pathname %>This directory has no corresponding template. You can add a template named "pages" to the Scrapbook root directory and its contents will show up here.
2
+ <% else %>This directory has no corresponding template. You can add a template with the same name as the directory as a sibling to the directory and its contents will show up here.<% end %>
@@ -1 +1 @@
1
- <iframe src="<%= raw_page_path(book: scrapbook.name, id: scrapbook.relative_page_path_for(pathname)) %>" class="w-full h-full"></iframe>
1
+ <iframe src="<%= raw_page_path(scrapbook.relative_page_path_for(pathname)) %>" class="w-full h-full"></iframe>
data/config/routes.rb CHANGED
@@ -1,16 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Scrapbook::Engine.routes.draw do
4
- book_regex = /#{Scrapbook::Engine.config.scrapbook.paths.map { File.basename(_1) }.join('|')}/
4
+ # TODO: Future plans
5
+ # scope path: '/.editor' do
6
+ # resources :pages, id: /.+/, only: %i[new create edit update]
7
+ # end
5
8
 
6
- resources :pages, id: /.+/
7
- resources :pages, path: ':book/pages', id: /.+/, constraints: {book: book_regex}
9
+ root 'pages#show'
8
10
 
9
- get ':book', to: 'pages#index', constraints: {book: book_regex}
10
- root 'pages#index'
11
-
12
- get '.raw/:book/pages/*id', to: 'pages#raw', as: :raw_page,
13
- constraints: {book: book_regex, id: /.*/}, defaults: {raw: true}
14
- get ':book/*id', to: 'pages#show', constraints: {book: book_regex, id: /.*/}
11
+ get '.raw/pages(/*id)', to: 'pages#raw', constraints: {id: /.*/}, as: :raw_page
15
12
  get '*id', to: 'pages#show', constraints: {id: /.*/}, as: :short_page
16
13
  end
@@ -5,12 +5,23 @@ require 'rails/generators'
5
5
  module Scrapbook
6
6
  # Initial default setup of Scrapbook
7
7
  class InstallGenerator < Rails::Generators::Base
8
- class_option 'url-path', default: '/scrapbook'
9
8
  class_option 'path-with-name', default: 'scrapbook'
10
9
 
11
10
  def install
12
- generate 'scrapbook:routes', options.fetch('url-path')
11
+ name = Pathname.new(options.fetch('path-with-name')).basename.to_s
12
+
13
+ generate 'scrapbook:routes', name
13
14
  generate 'scrapbook:new', options.fetch('path-with-name')
15
+ sprockets_support
16
+ end
17
+
18
+ private
19
+
20
+ def sprockets_support
21
+ relative_path = 'app/assets/config/manifest.js'
22
+ return unless FileTest.exist?(File.expand_path(relative_path, destination_root))
23
+
24
+ insert_into_file(relative_path, '//= link scrapbook/application.css')
14
25
  end
15
26
  end
16
27
  end
@@ -3,19 +3,21 @@
3
3
  require 'rails/generators'
4
4
 
5
5
  module Scrapbook
6
- # A generator to create a new scrapbook at either the default (scrapbook) or specified
7
- # path from the Rails application root.
6
+ # A generator to create a new scrapbook with either the default (scrapbook) or specified
7
+ # name. (Using the default options for the URL path and folder root.)
8
8
  class RoutesGenerator < Rails::Generators::Base
9
- argument :path, optional: true, default: '/scrapbook'
9
+ argument :name, optional: true, default: 'scrapbook'
10
10
 
11
11
  def routes
12
12
  # TODO: Investigate using a Rubocop rule to determine using single or double auotes.
13
- url_path = path.start_with?('/') ? path : "/#{path}"
14
- route <<~ROUTES
15
- if Rails.env.development?
16
- mount Scrapbook::Engine => '#{url_path}'
17
- end
18
- ROUTES
13
+
14
+ inject_into_file('config/routes.rb', after: "Rails.application.routes.draw do\n") do
15
+ " extend Scrapbook::Routing\n"
16
+ end
17
+
18
+ inject_into_file('config/routes.rb', after: "extend Scrapbook::Routing\n") do
19
+ "\n scrapbook('#{name}') if Rails.env.development?\n"
20
+ end
19
21
  end
20
22
  end
21
23
  end
@@ -6,19 +6,13 @@ module Scrapbook
6
6
  isolate_namespace Scrapbook
7
7
 
8
8
  config.scrapbook = ActiveSupport::OrderedOptions.new
9
- config.scrapbook.paths ||= []
9
+ config.scrapbook.paths ||= {}
10
10
  config.scrapbook.precompile_assets = config.scrapbook.precompile_assets || false
11
11
 
12
- initializer 'scrapbook.configuration' do |app|
13
- settings = app.config.scrapbook
14
-
15
- settings.paths << Rails.root.join('scrapbook') if settings.paths.empty?
16
- end
17
-
18
12
  initializer 'scrapbook.assets' do |app|
19
13
  if app.config.scrapbook.precompile_assets && app.config.respond_to?(:assets)
20
14
  app.config.assets.precompile.concat %w[
21
- scrapbook/tailwind.css
15
+ scrapbook/application.css
22
16
  ]
23
17
  end
24
18
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scrapbook
4
+ # This is a helper method for making it easier to add scrapbooks to routes.
5
+ module Routing
6
+ def scrapbook(name, at: "/#{name}", folder_root: name)
7
+ folder_root = Pathname.new(folder_root)
8
+ folder_root = Rails.root.join(folder_root) if folder_root.relative?
9
+
10
+ Engine.config.scrapbook.paths[name] = folder_root
11
+ mount Engine => at, defaults: {'.book': name}, as: "#{name}_scrapbook"
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Scrapbook
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.2'
5
5
  end
data/lib/scrapbook.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'scrapbook/version'
4
4
  require 'scrapbook/engine'
5
+ require 'scrapbook/routing'
5
6
 
6
7
  # TODO: Figure out documentation
7
8
  # Placeholder
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrapbook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Lindsay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-18 00:00:00.000000000 Z
11
+ date: 2022-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '7.0'
19
+ version: '6.1'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '7.1'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '7.0'
29
+ version: '6.1'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '7.1'
@@ -41,9 +41,8 @@ files:
41
41
  - LICENSE.txt
42
42
  - README.md
43
43
  - Rakefile
44
- - app/assets/builds/scrapbook/tailwind.css
44
+ - app/assets/builds/scrapbook/application.css
45
45
  - app/assets/config/scrapbook_manifest.js
46
- - app/assets/stylesheets/scrapbook/application.css
47
46
  - app/assets/stylesheets/scrapbook/application.tailwind.css
48
47
  - app/controllers/scrapbook/application_controller.rb
49
48
  - app/controllers/scrapbook/empty_controller.rb
@@ -57,7 +56,6 @@ files:
57
56
  - app/views/layouts/scrapbook/application.html.erb
58
57
  - app/views/layouts/scrapbook/host_application.html.erb
59
58
  - app/views/pages.html.erb
60
- - app/views/scrapbook/pages/index.html.erb
61
59
  - app/views/scrapbook/pages/show.html.erb
62
60
  - config/routes.rb
63
61
  - lib/generators/scrapbook/install_generator.rb
@@ -65,6 +63,7 @@ files:
65
63
  - lib/generators/scrapbook/routes_generator.rb
66
64
  - lib/scrapbook.rb
67
65
  - lib/scrapbook/engine.rb
66
+ - lib/scrapbook/routing.rb
68
67
  - lib/scrapbook/version.rb
69
68
  - lib/tasks/scrapbook_tasks.rake
70
69
  homepage: https://bfad.github.io/scrapbook
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
- * files in this directory. Styles in this file should be added after the last require_* statement.
11
- * It is generally better to create a new file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */
@@ -1 +0,0 @@
1
- This directory has no corresponding template. You can add a template with the same name as the directory as a sibling to the directory and its contents will show up here.