hobby-pages 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f742c6369b2e2a12560bdedff5e425d004c4a881
4
+ data.tar.gz: 0720736511e432a6732b9aef7260cdef15987f8d
5
+ SHA512:
6
+ metadata.gz: 30f5d0cf472f37c08a398cfe31a931730f108a5c02a4e8cf3ae6997b79fe8f7cf5f5ddc1e2970e42c96a8d4de8ba2faacf4407a19984573a0be68817042b0db4
7
+ data.tar.gz: 494eafecd9e4fd8edb15df02c94579e8b4bc40a23867b019252a08a0c1127f6fa6a0560246a0609ce58a0b4130fbd85755894178958b7741ca24bc2bd7aa5378
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'hobby-devtools', '>=0.0.11'
4
+ gem 'nokogiri'
5
+
6
+ gemspec
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |g|
2
+ g.name = 'hobby-pages'
3
+ g.files = `git ls-files`.split($/)
4
+ g.version = '0.0.0'
5
+ g.summary = 'A Hobby app to return a bunch of HTML pages from a directory.'
6
+ g.authors = ['Anatoly Chernow']
7
+
8
+ g.add_dependency 'hobby', '>=0.0.8'
9
+ g.add_dependency 'tilt'
10
+ g.add_dependency 'slim'
11
+ g.add_dependency 'sass'
12
+ end
@@ -0,0 +1,99 @@
1
+ require 'hobby'
2
+ require 'tilt'
3
+ require 'slim'
4
+ require 'sass'
5
+
6
+ module Hobby
7
+ class Pages
8
+ include Hobby
9
+
10
+ use Rack::ContentType, 'text/html'
11
+
12
+ attr_reader :name, :directory
13
+ def initialize directory
14
+ @directory = Directory.new directory
15
+ end
16
+
17
+ get '/:name' do
18
+ @name = my[:name]
19
+
20
+ if page = @directory[@name]
21
+ page.render self
22
+ else
23
+ not_found
24
+ end
25
+ end
26
+
27
+ def not_found
28
+ response.status = 404
29
+
30
+ name, @name = @name, '404'
31
+
32
+ if page = @directory[@name]
33
+ page.render self
34
+ else
35
+ "404. The page named '#{name}' was not found."
36
+ end
37
+ end
38
+
39
+ class Directory
40
+ def initialize root
41
+ @root = root
42
+ @pages = Dir["#{root}/html/pages/*.slim"].map do |path|
43
+ page = Page.new path, self
44
+ [page.name, page]
45
+ end.to_h
46
+ end
47
+
48
+ def to_s
49
+ @root
50
+ end
51
+
52
+ def default_layout
53
+ @default_layout ||= begin
54
+ path_to_default_layout = "#{@root}/html/layouts/default.slim"
55
+ if File.exist? path_to_default_layout
56
+ Tilt.new path_to_default_layout
57
+ else
58
+ fail "No layout was found at #{path_to_default_layout}"
59
+ end
60
+ end
61
+ end
62
+
63
+
64
+ def [] name
65
+ @pages[name]
66
+ end
67
+
68
+ class Page
69
+ attr_reader :name
70
+
71
+ def initialize path, directory
72
+ @layout = directory.default_layout
73
+
74
+ @name = File.basename path, '.slim'
75
+ @template = Tilt.new path
76
+
77
+ script_file = "#{directory.to_s}/html/ruby/#{name}.rb"
78
+ @script = IO.read script_file if File.exist? script_file
79
+
80
+ style_file = "#{directory.to_s}/css/pages/#{name}.sass"
81
+ if File.exist? style_file
82
+ sass_string = IO.read style_file
83
+ load_path = "#{directory.to_s}/css/pages/#{name}"
84
+ css = Sass::Engine.new(sass_string, load_paths: [load_path]).render
85
+ @css_tag = "<style id='for_page_#{name}'>#{css}</style>"
86
+ end
87
+ end
88
+
89
+ def render app
90
+ app.instance_eval @script if @script
91
+
92
+ @layout.render app do
93
+ "#{@css_tag}\n#{@template.render app}"
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1 @@
1
+ $width: 100%
@@ -0,0 +1,4 @@
1
+ @import 'width'
2
+
3
+ input
4
+ width: $width
@@ -0,0 +1,10 @@
1
+ doctype 5
2
+ html
3
+ head
4
+ - if @head
5
+ title= @head
6
+ - else
7
+ title Default layout
8
+
9
+ body
10
+ == yield
@@ -0,0 +1 @@
1
+ p some
@@ -0,0 +1 @@
1
+ input
@@ -0,0 +1 @@
1
+ p main content
@@ -0,0 +1 @@
1
+ @head = 'Head from with-head'
data/spec/helper.rb ADDED
@@ -0,0 +1 @@
1
+ require 'hobby/devtools/rspec_helper'
data/spec/main_spec.rb ADDED
@@ -0,0 +1,57 @@
1
+ require_relative 'helper'
2
+
3
+ require 'hobby/pages'
4
+ require 'securerandom'
5
+ require 'nokogiri'
6
+
7
+ describe Hobby::Pages do
8
+ before :all do
9
+ @socket = "app.#{SecureRandom.uuid}.socket"
10
+ @pid = fork do
11
+ server = Puma::Server.new described_class.new 'spec/dirs/main'
12
+ server.add_unix_listener @socket
13
+ server.run
14
+ sleep
15
+ end
16
+ sleep 0.01 until File.exist? @socket
17
+ @conn = Excon.new 'unix:///', socket: @socket
18
+ end
19
+
20
+ after(:all) { `kill -9 #{@pid}` }
21
+
22
+ it 'creates pages for existing templates' do
23
+ doc = Nokogiri.HTML @conn.get(path: '/exist').body
24
+
25
+ text = doc.at_css('p').text
26
+ expect(text).to eq 'some'
27
+
28
+ text = doc.at_css('title').text
29
+ expect(text).to eq 'Default layout'
30
+ end
31
+
32
+ it 'returns 404 for non-existing templates' do
33
+ response = @conn.get(path: '/nonexist')
34
+
35
+ expect(response.status).to eq 404
36
+ expect(response.body).to eq "404. The page named 'nonexist' was not found."
37
+ end
38
+
39
+ it 'supports layouts with multiple content sections' do
40
+ response = @conn.get(path: '/with-head')
41
+ doc = Nokogiri.HTML response.body
42
+
43
+ text = doc.at_css('title').text
44
+ expect(text).to eq 'Head from with-head'
45
+
46
+ text = doc.at_css('p').text
47
+ expect(text).to eq 'main content'
48
+ end
49
+
50
+ it 'creates pages with CSS when CSS was supplied' do
51
+ response = @conn.get path: '/with-css'
52
+ doc = Nokogiri.HTML response.body
53
+
54
+ text = doc.at_css('style#for_page_with-css').text
55
+ expect(text).to eq "input {\n width: 100%; }\n"
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hobby-pages
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anatoly Chernow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hobby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: tilt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: slim
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sass
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - Gemfile
77
+ - hobby-pages.gemspec
78
+ - lib/hobby/pages.rb
79
+ - spec/dirs/main/css/pages/with-css.sass
80
+ - spec/dirs/main/css/pages/with-css/width.sass
81
+ - spec/dirs/main/html/layouts/default.slim
82
+ - spec/dirs/main/html/pages/exist.slim
83
+ - spec/dirs/main/html/pages/with-css.slim
84
+ - spec/dirs/main/html/pages/with-head.slim
85
+ - spec/dirs/main/html/ruby/with-head.rb
86
+ - spec/helper.rb
87
+ - spec/main_spec.rb
88
+ homepage:
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.11
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A Hobby app to return a bunch of HTML pages from a directory.
111
+ test_files: []