blogish 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +1 -0
- data/blogish.gemspec +24 -0
- data/lib/blogish/rouge_html.rb +6 -0
- data/lib/blogish/version.rb +3 -0
- data/lib/blogish.rb +77 -0
- data/spec/blogish_spec.rb +66 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alex Edwards
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Blogish
|
2
|
+
|
3
|
+
Blogish is a tiny and powerful blogging engine, wrapped up in a Rubygem. It supports markdown-based entries, RSS feeds and syntax highlighting.
|
4
|
+
|
5
|
+
It aims to provide the functions you need to create and display entries, but without dictating site layout or design. That makes it well suited for situations where you want to incorporate a simple blog into an pre-existing site.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'blogish'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install blogish
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
require 'blogish'
|
25
|
+
```
|
26
|
+
|
27
|
+
### Creating new posts
|
28
|
+
|
29
|
+
Blogish reads entries stored in flat [markdown](http://daringfireball.net/projects/markdown/) files in a `views/blog` directory.
|
30
|
+
|
31
|
+
It relies on certain conventions to work. Each entry should be named using the date of the entry and the URL slug, separated by an underscore and with the `.mkd` extension.
|
32
|
+
|
33
|
+
```
|
34
|
+
<date>_<slug>.mkd
|
35
|
+
```
|
36
|
+
|
37
|
+
The first line of the each entry should contain its title, then three dashes to separate it from the main body of the entry. Putting it all together:
|
38
|
+
|
39
|
+
```
|
40
|
+
$ cat views/blog/2013-02-07_hello-world.mkd
|
41
|
+
Hello World!
|
42
|
+
---
|
43
|
+
This is the first paragraph.
|
44
|
+
|
45
|
+
This is the second paragraph. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Hic porro quae ea amet
|
46
|
+
dolores quibusdam magni illum harum unde tempore nisi totam adipisci quisquam aliquid quis ab necessitatibus?
|
47
|
+
Mollitia explicabo aperiam asperiores doloremque officiis ipsam. Animi distinctio a dolor consequuntur.
|
48
|
+
```
|
49
|
+
|
50
|
+
### Displaying a post
|
51
|
+
|
52
|
+
You can display a single post based on it's slug using the `fetch` method. For example:
|
53
|
+
|
54
|
+
```
|
55
|
+
Blogish.fetch('hello-world')
|
56
|
+
```
|
57
|
+
|
58
|
+
If a matching entry is found, then a Hash with the contents of the post is returned. This Hash can then be passed to your view file, or wherever else you need to use the information.
|
59
|
+
|
60
|
+
```
|
61
|
+
{
|
62
|
+
:title => 'Hello World',
|
63
|
+
:date => #<Date: 2013-02-07 ((2456088j,0s,0n),+0s,2299161j)>,
|
64
|
+
:slug => 'hello-world',
|
65
|
+
:intro => '<p>This is the first paragraph</p>',
|
66
|
+
:body => '<p>This is the first paragraph</p><p>This is the second paragraph. Lorem ipsum ...</p>'
|
67
|
+
}
|
68
|
+
```
|
69
|
+
|
70
|
+
If no entry with a matching slug is found, the method returns `nil`.
|
71
|
+
|
72
|
+
### Displaying multiple posts
|
73
|
+
|
74
|
+
You can also display all entries using the `fetch_all` method. This returns an Array of all entries in the directory, sorted with the most recently dated first.
|
75
|
+
|
76
|
+
```
|
77
|
+
Blogish.fetch_all
|
78
|
+
```
|
79
|
+
|
80
|
+
This Array can then be passed to your view file, or wherever else you need to use it.
|
81
|
+
|
82
|
+
### Using Syntax Highlighting
|
83
|
+
|
84
|
+
Syntax highlighting is supported out of the box thanks to [Rouge](https://github.com/jayferd/rouge). A list of supported languages is [here](http://rouge.jayferd.us/demo). Simply wrap any code in your markdown files in fenced blocks, github style.
|
85
|
+
|
86
|
+
<pre><code>```ruby
|
87
|
+
# This is a comment
|
88
|
+
@foo = bar
|
89
|
+
```
|
90
|
+
</code></pre>
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
1. Fork it
|
95
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
96
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
97
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
98
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/blogish.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'blogish/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "blogish"
|
8
|
+
gem.version = Blogish::VERSION
|
9
|
+
gem.authors = ["Alex Edwards"]
|
10
|
+
gem.email = ["ajmedwards@gmail.com"]
|
11
|
+
gem.description = %q{A tiny and powerful blogging engine, wrapped up in a Rubygem. Supports markdown-based posts, syntax highlighting, and RSS feeds.}
|
12
|
+
gem.summary = %q{Markdown-based blog engine}
|
13
|
+
gem.homepage = "https://github.com/alexedwards/blogish"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "redcarpet"
|
21
|
+
gem.add_dependency "rouge"
|
22
|
+
|
23
|
+
gem.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/blogish.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "redcarpet"
|
2
|
+
require "blogish/version"
|
3
|
+
require "blogish/rouge_html"
|
4
|
+
|
5
|
+
module Blogish
|
6
|
+
extend self
|
7
|
+
|
8
|
+
# Fetch and parse a single blog entry based on its slug.
|
9
|
+
#
|
10
|
+
# * @params slug [String]
|
11
|
+
# * @return [nil] if no matching slug is found
|
12
|
+
# * @return [Hash] representing the blog entry if a matching slug is found.
|
13
|
+
#
|
14
|
+
# == Example:
|
15
|
+
#
|
16
|
+
# If we have a file named
|
17
|
+
# views/blog/2013-02-07_hello-world.mkd
|
18
|
+
# then running
|
19
|
+
# Blogish.fetch('hello-world')
|
20
|
+
# will find the file and return a hash containing the following keys:
|
21
|
+
#
|
22
|
+
# * :title [String]
|
23
|
+
# * :date [Date]
|
24
|
+
# * :slug [String]
|
25
|
+
# * :intro [String]
|
26
|
+
# * :body [String]
|
27
|
+
|
28
|
+
def fetch(slug)
|
29
|
+
path = entries.detect { |file| file =~ /_#{Regexp.quote(slug)}.mkd$/ }
|
30
|
+
path ? open(path) : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
# Fetch and parse all blog entries in the `views/blog` directory, sorted
|
34
|
+
# by date with the most recent entry first.
|
35
|
+
#
|
36
|
+
# * @params none
|
37
|
+
# * @return [Array] of hashes representing each blog entry.
|
38
|
+
#
|
39
|
+
# == Example:
|
40
|
+
#
|
41
|
+
# If we have files named
|
42
|
+
# views/blog/2013-02-07_hello-world.mkd
|
43
|
+
# and
|
44
|
+
# views/blog/2013-02-05_foo-post.mkd
|
45
|
+
# then running
|
46
|
+
# Blogish.fetch
|
47
|
+
# will return an Array containing both entries:
|
48
|
+
# [{:title => 'Foo Post', ...}, {:title => 'Hello World', ...}]
|
49
|
+
|
50
|
+
def fetch_all
|
51
|
+
entries.sort.reverse.map { |entry| open(entry) }
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def entries
|
57
|
+
Dir.chdir("views/blog") { glob("*.mkd") }
|
58
|
+
end
|
59
|
+
|
60
|
+
def open(path)
|
61
|
+
date, slug = path.chomp('.mkd').split('_', 2)
|
62
|
+
title, body = File.read(path).split(/^---+$/, 2)
|
63
|
+
date = Date.parse(date)
|
64
|
+
title.strip!
|
65
|
+
body = convert_to_html(body).strip
|
66
|
+
intro = convert_to_html(body.lines.map.first).strip
|
67
|
+
{date: date, title: title, slug: slug, body: body, intro: intro}
|
68
|
+
end
|
69
|
+
|
70
|
+
def convert_to_html(markdown)
|
71
|
+
renderer = RougeHTML
|
72
|
+
extensions = {fenced_code_blocks: true}
|
73
|
+
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
|
74
|
+
redcarpet.render markdown
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'date'
|
2
|
+
require_relative "../lib/blogish"
|
3
|
+
|
4
|
+
describe Blogish do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
Blogish.stub!(:entries).and_return(['2013-02-03_hello-world.mkd', '2013-02-05_an-example-post.mkd', '2013-02-04_another-example-post.mkd'])
|
8
|
+
File.stub!(:read).with('2013-02-03_hello-world.mkd').and_return("Hello World\n---\nThis is an introduction.\n\nLorem ipsum dolor et sans.")
|
9
|
+
File.stub!(:read).with('2013-02-05_an-example-post.mkd').and_return("An Example Post\n---\nLorem ipsum dolor et sans.")
|
10
|
+
File.stub!(:read).with('2013-02-04_another-example-post.mkd').and_return("Another Example Post\n---\nLorem ipsum dolor et sans.")
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#fetch" do
|
14
|
+
|
15
|
+
context "when the slug is invalid" do
|
16
|
+
|
17
|
+
it "should return nil" do
|
18
|
+
Blogish.fetch('an-invalid-slug').should be nil
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
context "when the slug is valid" do
|
24
|
+
|
25
|
+
it "should return a hash of post information" do
|
26
|
+
Blogish.fetch('hello-world').should be_a_kind_of Hash
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should contain the title" do
|
30
|
+
Blogish.fetch('hello-world')[:title].should eql 'Hello World'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should contain the date" do
|
34
|
+
Blogish.fetch('hello-world')[:date].to_s.should eql '2013-02-03'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should contain the slug" do
|
38
|
+
Blogish.fetch('hello-world')[:slug].should eql 'hello-world'
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should contain the body as html" do
|
42
|
+
Blogish.fetch('hello-world')[:body].should eql "<p>This is an introduction.</p>\n\n<p>Lorem ipsum dolor et sans.</p>"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should contain the introductory paragraph as html" do
|
46
|
+
Blogish.fetch('hello-world')[:intro].should eql "<p>This is an introduction.</p>"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#fetch_all" do
|
54
|
+
|
55
|
+
it "should return all entries" do
|
56
|
+
Blogish.fetch_all.size.should eql 3
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should order the entries most recent first" do
|
60
|
+
dates = Blogish.fetch_all.map {|x| x[:date].to_s }
|
61
|
+
dates.should eql ["2013-02-05", "2013-02-04", "2013-02-03"]
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blogish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Edwards
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redcarpet
|
16
|
+
requirement: &19413020 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *19413020
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rouge
|
27
|
+
requirement: &19412520 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *19412520
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &19411840 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *19411840
|
47
|
+
description: A tiny and powerful blogging engine, wrapped up in a Rubygem. Supports
|
48
|
+
markdown-based posts, syntax highlighting, and RSS feeds.
|
49
|
+
email:
|
50
|
+
- ajmedwards@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- blogish.gemspec
|
61
|
+
- lib/blogish.rb
|
62
|
+
- lib/blogish/rouge_html.rb
|
63
|
+
- lib/blogish/version.rb
|
64
|
+
- spec/blogish_spec.rb
|
65
|
+
homepage: https://github.com/alexedwards/blogish
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.11
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Markdown-based blog engine
|
89
|
+
test_files:
|
90
|
+
- spec/blogish_spec.rb
|