satanic_pages 0.0.1 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f33a12b44515dc388c1cf2f447c90d52439100f89d21b2c2657b4de2ec99d34
4
- data.tar.gz: 3868dd61e1dd7b994870cfa03c593f5a35c365fdcf91cd4b8264a7f9f5f962c7
3
+ metadata.gz: 27039023737b744f359ee8ecff3085761338d9a7418220cf37657fe84572402e
4
+ data.tar.gz: 285411e1a9e1360bf16231ec4077940e15981848485efe1d1540863497d38282
5
5
  SHA512:
6
- metadata.gz: 5eca5f6ab90383fd669e518fb2575d39af1faa4eb6b4f4369c6afd46a772f1bfb9f3bb0215df0d99ce5c49155f57bb6b9142d453667a7dd29880d477dee21ae1
7
- data.tar.gz: 9bac938b82e0aec3f5cb1443bad6c8cc97318973d49f5bfb0f797a5dfdf21ffb7610c8473df2d2f8958ca4e7ef4e3fedcfa8e31f08003ff96de4464d42bd4937
6
+ metadata.gz: 240fc63f3d3ef88e0e6fab247ba49ebf742acc4f7bc4df6ec7d735973bc898fadd039c480d18b269de0e3e7f25776e79e6f88ddf407cbaf78ed798beadf7875e
7
+ data.tar.gz: 5c9a82247c92fb5ff4bff9ee5916debd5f9487c02afc365b4439d1955c42903da295d2253efef2b38e0a850e120d2602f8f2b3428ed083cb05baf400a4eca8f5
data/README.md CHANGED
@@ -1,28 +1,65 @@
1
- # SatanicPages
2
- Short description and motivation.
1
+ <center><img src="https://s3.brnbw.com/Artboard-GDaMBcpYEJ.png" alt="Satanic Pages" width=762 /></center>
2
+
3
+ Static pages with YAML frontmatter, embedded in your Rails app.
4
+
5
+ As simple as, …
6
+ **`app/views/pages/exorcism_tips.html.md`:**
7
+
8
+ ```markdown
9
+ ---
10
+ title: 3 tips for the PERFECT exorcism
11
+ author: Beelzebub
12
+ tags:
13
+ - listicles
14
+ - devil worship
15
+ - activity ideas
16
+ ---
17
+ By <%= current_page.author %>.
18
+
19
+ Through trial and error, I've found that the perfect exorcism
20
+ starts with a refreshing drink.
21
+
22
+ Tagged <%= render "tag_list", tags: current_page.tags %>:
23
+
24
+ ## Devilishly good lemonade recipe
25
+
26
+
27
+ ```
3
28
 
4
29
  ## Usage
5
- How to use my plugin.
6
30
 
7
- ## Installation
8
- Add this line to your application's Gemfile:
31
+ In `app/views/layouts/pages.html.erb`:
9
32
 
10
33
  ```ruby
11
- gem "satanic_pages"
34
+ <%= render_layout "application" do %>
35
+ <h1><%= current_page.title %></h1>
36
+ <article>
37
+ <%= yield %>
38
+ </article>
39
+ <% end %>
12
40
  ```
13
41
 
14
- And then execute:
15
- ```bash
16
- $ bundle
42
+ ## Installation
43
+ Add this line to your application's Gemfile:
44
+
45
+ ```sh
46
+ $ bundle add satanic_pages
47
+ $ bundle add markdown-rails # optional, but probably
17
48
  ```
18
49
 
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install satanic_pages
50
+ Add the _Concern_ to a regular controller:
51
+
52
+ ```ruby
53
+ class PagesController < ApplicationController
54
+ include SatanicPages::Controller
55
+ end
22
56
  ```
23
57
 
24
- ## Contributing
25
- Contribution directions go here.
58
+ And set up a wildcard route:
59
+
60
+ ```ruby
61
+ get("*page", to: "pages#show", as: :page, constraints: PagesController.constraint)
62
+ ```
26
63
 
27
64
  ## License
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
65
+ [MIT](https://opensource.org/licenses/MIT)
@@ -0,0 +1,50 @@
1
+ module SatanicPages
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :current_page
7
+ end
8
+
9
+ class_methods do
10
+ def constraint
11
+ -> (req) { pages.match?(req.path) }
12
+ end
13
+
14
+ def pages
15
+ @pages ||= PageList.new(controller_name)
16
+ end
17
+ end
18
+
19
+ def show
20
+ @test = "wut"
21
+ render_static_page
22
+ end
23
+
24
+ private
25
+
26
+ def slug_param
27
+ params.require(:page)
28
+ end
29
+
30
+ def pages
31
+ self.class.pages
32
+ end
33
+
34
+ def current_page
35
+ pages.find(slug_param)
36
+ end
37
+
38
+ # Bit of a hack here. As Rails resolves the layout pretty late, there's no
39
+ # public way of looking it up at this point.
40
+ # We do however control the rendering, probably, so the only thing we check
41
+ # for is if the controller class has called self.layout.
42
+ def page_layout
43
+ self.class._layout || "application"
44
+ end
45
+
46
+ def render_static_page
47
+ render(current_page.slug)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module SatanicPages::LayoutHelper
2
+ # Thank you Sitepress for this lil' goodie
3
+ # https://github.com/sitepress/sitepress/blob/738aae7eaef61b494ad90786bc9f796374088ee1/sitepress-server/rails/app/helpers/application_helper.rb#L18
4
+ def render_layout(layout, **kwargs, &block)
5
+ render(html: capture(&block), layout: "layouts/#{layout}", **kwargs)
6
+ end
7
+ end
@@ -1,4 +1,26 @@
1
1
  module SatanicPages
2
2
  class Engine < ::Rails::Engine
3
+ initializer("satanic_pages.view_helpers") do
4
+ ActiveSupport.on_load(:action_view) { include SatanicPages::LayoutHelper }
5
+ end
6
+
7
+ initializer("satanic_pages.register_markdown") do
8
+ if has_markdown_rails?
9
+ require "satanic_pages/markdown_template_handler"
10
+
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def has_markdown_rails?
17
+ return true if defined?(MarkdownRails)
18
+
19
+ require "markdown-rails"
20
+
21
+ true
22
+ rescue LoadError
23
+ false
24
+ end
3
25
  end
4
26
  end
@@ -0,0 +1,29 @@
1
+ module SatanicPages
2
+ class MarkdownTemplateHandler < MarkdownRails::Renderer::Rails
3
+ def preprocess(source)
4
+ frontmatter = {}
5
+
6
+ content = source.gsub(/\A---\n(.*?)\n---\n/m) do
7
+ begin
8
+ frontmatter = YAML.safe_load($1)
9
+ ""
10
+ rescue => e
11
+ Rails.logger.error("Error parsing frontmatter: #{e.message}")
12
+ $&
13
+ end
14
+ end
15
+
16
+ data = OpenStruct.new(frontmatter)
17
+
18
+ render(inline: content, handler: :erb, locals: {current_page: data})
19
+ # Remove template comments
20
+ .gsub(/<!-- (BEGIN|END) (.*) -->/, "")
21
+ # Force HTML tags to be inline
22
+ .gsub(/<[^>]*?>/) { |tag| tag.gsub(/\n\s*/, " ") }
23
+ end
24
+ end
25
+ end
26
+
27
+ MarkdownRails.handle(:md) do
28
+ SatanicPages::MarkdownTemplateHandler.new
29
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ostruct"
4
+
5
+ module SatanicPages
6
+ class Page
7
+ def initialize(full_path)
8
+ @full_path = full_path
9
+ @slug = nil
10
+ @content = nil
11
+ @data = nil
12
+ parse!
13
+ end
14
+
15
+ attr_reader :slug, :full_path, :content, :data
16
+
17
+ delegate_missing_to :data
18
+
19
+ private
20
+
21
+ def parse!
22
+ @slug = File
23
+ .basename(full_path)
24
+ .gsub(/(\.\w+)+$/, "")
25
+
26
+ @raw = File.read(full_path)
27
+
28
+ @content = @raw.gsub(/\A---\n(.*?)\n---\n/m) do
29
+ begin
30
+ @data = OpenStruct.new(YAML.safe_load($1))
31
+ nil
32
+ rescue => e
33
+ Rails.logger.error("Error parsing frontmatter: #{e.message}")
34
+ @data = OpenStruct.new
35
+ $&
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SatanicPages
4
+ class PageList
5
+ class PageNotFound < StandardError
6
+ end
7
+
8
+ def initialize(ctrl)
9
+ @base_path = Rails.root.join("app/views/", ctrl)
10
+
11
+ @pages = Dir[base_path + "**/*.html*"]
12
+ .reject { |path| path.match?(/(^_|\/_)/) }
13
+ .each_with_object({}) do |full_path, hash|
14
+ page = Page.new(full_path)
15
+ hash[page.slug] = page
16
+ end
17
+ end
18
+
19
+ attr_reader :base_path, :pages
20
+
21
+ def find(slug)
22
+ @pages.fetch(slug)
23
+ rescue KeyError
24
+ raise PageNotFound, "No page `#{slug}' found at #{base_path}"
25
+ end
26
+
27
+ def match?(path)
28
+ path = path.gsub(/^\//, "")
29
+ pages.keys.include?(path)
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module SatanicPages
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/satanic_pages.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "satanic_pages/version"
2
2
  require "satanic_pages/engine"
3
3
 
4
+ require "satanic_pages/page"
5
+ require "satanic_pages/page_list"
6
+
4
7
  module SatanicPages
5
- # Your code goes here...
6
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: satanic_pages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikkel Malmberg
@@ -9,6 +9,20 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ostruct
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0.6'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0.6'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: rails
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -33,11 +47,14 @@ files:
33
47
  - MIT-LICENSE
34
48
  - README.md
35
49
  - Rakefile
36
- - config/routes.rb
50
+ - app/controllers/concerns/satanic_pages/controller.rb
51
+ - app/helpers/satanic_pages/layout_helper.rb
37
52
  - lib/satanic_pages.rb
38
53
  - lib/satanic_pages/engine.rb
54
+ - lib/satanic_pages/markdown_template_handler.rb
55
+ - lib/satanic_pages/page.rb
56
+ - lib/satanic_pages/page_list.rb
39
57
  - lib/satanic_pages/version.rb
40
- - lib/tasks/satanic_pages_tasks.rake
41
58
  homepage: https://github.com/mikker/satanic_pages
42
59
  licenses:
43
60
  - MIT
data/config/routes.rb DELETED
@@ -1,2 +0,0 @@
1
- Rails.application.routes.draw do
2
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :satanic_pages do
3
- # # Task goes here
4
- # end