static_post 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d13f87edd7a06afd15292127669b0ed8c67d516a
4
+ data.tar.gz: 5b34eb26147e9652076ec47bb34c8191a284431e
5
+ SHA512:
6
+ metadata.gz: aebb0142fc0bddeed1ebbe123b14c02ee03fe670a851a3b77a50338228f6e28ed9e66045eb6b99f841b7250674de3d26a9d0b0b33fb98ad5b9607daf998b1b16
7
+ data.tar.gz: 216148bc676c3332e72f7277e026f72b9d65ca910fa55e73ad2a1076fdbfc4a6a53b0bb51786c0aef63b3e6cb59ec620d1ccf3ef768a4b2dd9e8df95b2ed909e
data/.travis.yml ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ static_post (0.1.0)
5
+ activemodel
6
+ redcarpet
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.2.1)
12
+ activesupport (= 4.2.1)
13
+ builder (~> 3.1)
14
+ activesupport (4.2.1)
15
+ i18n (~> 0.7)
16
+ json (~> 1.7, >= 1.7.7)
17
+ minitest (~> 5.1)
18
+ thread_safe (~> 0.3, >= 0.3.4)
19
+ tzinfo (~> 1.1)
20
+ ast (2.0.0)
21
+ astrolabe (1.3.0)
22
+ parser (>= 2.2.0.pre.3, < 3.0)
23
+ builder (3.2.2)
24
+ coderay (1.1.0)
25
+ diff-lcs (1.2.5)
26
+ i18n (0.7.0)
27
+ json (1.8.2)
28
+ method_source (0.8.2)
29
+ minitest (5.5.1)
30
+ parser (2.2.0.3)
31
+ ast (>= 1.1, < 3.0)
32
+ powerpack (0.1.0)
33
+ pry (0.10.1)
34
+ coderay (~> 1.1.0)
35
+ method_source (~> 0.8.1)
36
+ slop (~> 3.4)
37
+ rainbow (2.0.0)
38
+ rake (10.4.2)
39
+ redcarpet (3.2.2)
40
+ rspec (3.2.0)
41
+ rspec-core (~> 3.2.0)
42
+ rspec-expectations (~> 3.2.0)
43
+ rspec-mocks (~> 3.2.0)
44
+ rspec-core (3.2.2)
45
+ rspec-support (~> 3.2.0)
46
+ rspec-expectations (3.2.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.2.0)
49
+ rspec-mocks (3.2.1)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.2.0)
52
+ rspec-support (3.2.2)
53
+ rubocop (0.29.1)
54
+ astrolabe (~> 1.3)
55
+ parser (>= 2.2.0.1, < 3.0)
56
+ powerpack (~> 0.1)
57
+ rainbow (>= 1.99.1, < 3.0)
58
+ ruby-progressbar (~> 1.4)
59
+ ruby-progressbar (1.7.5)
60
+ slop (3.6.0)
61
+ thread_safe (0.3.5)
62
+ tzinfo (1.2.2)
63
+ thread_safe (~> 0.1)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ bundler (~> 1.5)
70
+ pry
71
+ rake
72
+ rspec
73
+ rubocop
74
+ static_post!
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # Static Post
2
+ *Please note:* This project is still a work in progress.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ Dir.glob('lib/tasks/*.rake').each { |r| import r }
2
+
3
+ task default: %w[test]
@@ -0,0 +1,11 @@
1
+ require 'active_model'
2
+ require 'redcarpet'
3
+ require 'yaml'
4
+ require 'static_post/version'
5
+ require 'static_post/base'
6
+
7
+ module StaticPost
8
+ SETTINGS = {
9
+ renderer: Redcarpet::Render::HTML.new
10
+ }
11
+ end
@@ -0,0 +1,75 @@
1
+ module StaticPost
2
+ class Base
3
+ extend ActiveModel::Naming
4
+
5
+ class << self
6
+ FRONTMATTER_CONTENT = /^\s*---\n+(.*?)\n+---/
7
+ FRONTMATTER = /^\s*---\n+.*?\n+---/
8
+
9
+ def all
10
+ @posts || load_posts
11
+ end
12
+
13
+ def post_path(path = nil)
14
+ if path.nil?
15
+ @post_path.nil? ? default_post_path : @post_path
16
+ else
17
+ @post_path = File.expand_path(path)
18
+ end
19
+ end
20
+
21
+ def default_post_path
22
+ if defined?(Rails)
23
+ File.expand_path('app/views/posts', Rails.root)
24
+ else
25
+ File.expand_path('.')
26
+ end
27
+ end
28
+
29
+ def compile(content)
30
+ @compiler = Redcarpet::Markdown.new(SETTINGS[:renderer])
31
+ @compiler.render(content)
32
+ end
33
+
34
+ def load_posts
35
+ @posts = post_files.map do |file, i|
36
+ create_record(file, i)
37
+ end
38
+ end
39
+
40
+ def post_files
41
+ Dir.entries("#{@post_path}").select do |file|
42
+ file unless File.directory?(file)
43
+ end
44
+ end
45
+
46
+ def create_record(file, id)
47
+ {
48
+ id: id,
49
+ attributes: extract_attributes(file)
50
+ }
51
+ end
52
+
53
+ def extract_attributes(file)
54
+ file_content = File.read(file)
55
+ {
56
+ content: post_content(file_content)
57
+ }.merge(extract_frontmatter(file_content))
58
+ end
59
+
60
+ def extract_frontmatter(content)
61
+ frontmatter = content.match(FRONTMATTER_CONTENT).captures
62
+ if frontmatter.any?
63
+ attributes = YAML.load(frontmatter.first)
64
+ Hash[attributes.map { |k, v| [k.to_sym, v] }]
65
+ else
66
+ {}
67
+ end
68
+ end
69
+
70
+ def post_content(content)
71
+ compile(content.sub(FRONTMATTER, ''))
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module StaticPost
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubocop/rake_task'
2
+
3
+ desc 'Lint source code with RuboCop'
4
+ RuboCop::RakeTask.new(:lint) do |t|
5
+ t.patterns = ['lib/**/*.rb', 'spec/*.rb']
6
+ t.fail_on_error = false
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ desc 'Run tests with Rspec'
4
+ RSpec::Core::RakeTask.new(:test) do |t|
5
+ t.pattern = Dir.glob('spec/*_spec.rb')
6
+ t.rspec_opts = '--format documentation'
7
+ t.rspec_opts << ' --color'
8
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe StaticPost::Base do
4
+ before(:each) do
5
+ class TestPost < StaticPost::Base; end
6
+ end
7
+
8
+ it '#post_path returns default path' do
9
+ expect(TestPost.post_path).to eql(File.expand_path('.'))
10
+ end
11
+
12
+ it '#post_path to set post path' do
13
+ TestPost.post_path("#{__dir__}/fixtures")
14
+ expect(TestPost.post_path).to eql("#{__dir__}/fixtures")
15
+ end
16
+
17
+ it '#post_path used in class' do
18
+ class AnotherTestPost < StaticPost::Base
19
+ post_path 'new_path'
20
+ end
21
+ expect(AnotherTestPost.post_path).to eql(File.expand_path('new_path'))
22
+ end
23
+
24
+ it '#post_files' do
25
+ TestPost.post_path("#{__dir__}/fixtures")
26
+ expect(TestPost.post_files).to eql(['test_doc.md'])
27
+ end
28
+
29
+ it '#extract_frontmatter' do
30
+ content = "---\ntitle: Test Post\n---\nDocument Content"
31
+ parsed_yaml = TestPost.extract_frontmatter(content)
32
+ expect(parsed_yaml).to eql(title: 'Test Post')
33
+ end
34
+
35
+ it '#post_content' do
36
+ content = "---\ntitle: Test Post\n---\nDocument Content"
37
+ parsed_yaml = TestPost.post_content(content)
38
+ expect(parsed_yaml).to eql("<p>Document Content</p>\n")
39
+ end
40
+
41
+ it '#default_post_path' do
42
+ expect(TestPost.default_post_path).to eql(File.expand_path('.'))
43
+ end
44
+
45
+ it '#default_post_path with rails' do
46
+ class App
47
+ def root
48
+ 'test/path'
49
+ end
50
+ end
51
+ Rails = App.new
52
+ expected_path = File.expand_path('test/path/app/views/posts')
53
+ expect(TestPost.default_post_path).to eql(expected_path)
54
+ end
55
+ end
@@ -0,0 +1,7 @@
1
+ ---
2
+ title: Test post
3
+ author: Brennan Spellacy
4
+ published: true
5
+ ---
6
+
7
+ # A markdown document
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup(:default)
3
+
4
+ require 'pry'
5
+ require 'static_post'
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'static_post/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'static_post'
8
+ spec.version = StaticPost::VERSION
9
+ spec.authors = ['Brennan Spellacy']
10
+ spec.email = ['brennanspellacy@gmail.com']
11
+ spec.summary = %q{ActiveRecord like interface to access static markup files.}
12
+ spec.homepage = 'https://github.com/bspellio/static_post'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'activemodel'
21
+ spec.add_runtime_dependency 'redcarpet'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'pry'
26
+ spec.add_development_dependency 'rubocop'
27
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_post
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brennan Spellacy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redcarpet
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: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - brennanspellacy@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".travis.yml"
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - README.md
122
+ - Rakefile
123
+ - lib/static_post.rb
124
+ - lib/static_post/base.rb
125
+ - lib/static_post/version.rb
126
+ - lib/tasks/lint.rake
127
+ - lib/tasks/test.rake
128
+ - spec/base_spec.rb
129
+ - spec/fixtures/test_doc.md
130
+ - spec/spec_helper.rb
131
+ - static_post.gemspec
132
+ homepage: https://github.com/bspellio/static_post
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.4.6
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: ActiveRecord like interface to access static markup files.
156
+ test_files:
157
+ - spec/base_spec.rb
158
+ - spec/fixtures/test_doc.md
159
+ - spec/spec_helper.rb