bao-karakuri 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +131 -0
  3. data/Rakefile +46 -0
  4. data/lib/karakuri.rb +131 -0
  5. data/lib/serious_blog_helper.rb +13 -0
  6. metadata +67 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ == Karakuri
2
+
3
+ Copyright (c) 2010 Sven Kraeuter mail@5v3n.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,131 @@
1
+ # Karakuri
2
+
3
+ Having a closer look at ruby based blogging platforms like [serious](http://github.com/colszowka/serious) and [toto](http://cloudhead.io/toto), I missed some functionality.
4
+
5
+ This a a collection of tools usable in both platforms. The project was formerly known as blog_helper, but Karakuri is a far more suitable name & will be used from now on.
6
+
7
+ ## Features
8
+
9
+ - tag your posts
10
+ - use seo friendly page titles
11
+ - use the disqus comment counter
12
+ - a workaround for serious allowing you to use generic yaml fields
13
+
14
+ ## Installation
15
+
16
+ It's a piece of cake: just install the gem. Simply add `gem 'karakuri'` to your Gemfile or use `sudo gem install karakuri` to get going.
17
+
18
+ For a default karakuri powered toto template, check out [dorothy's sister karathy](https://github.com/5v3n/karathy).
19
+
20
+ ## Usage
21
+
22
+ Piece of cake, again: all you have to do is use `<% require 'karakuri'%>` in your .rhtml or .erb file and call the corresponding methods.
23
+
24
+ ### SEO friendly titles
25
+ For example, to use seo friendly titles, your layout.rhtml should be looking like this:
26
+
27
+
28
+ <!doctype html>
29
+ <html>
30
+ <head>
31
+ <% require 'karakuri'
32
+ page_title = Karakuri::seo_friendly_title(@path, title, 'yourSitesTitle.com')
33
+ %>
34
+ <title><%= page_title %></title>
35
+ <link rel="alternate" type="application/atom+xml" title="<%= page_title %> - feed" href="/index.xml" />
36
+ .
37
+ .
38
+ .
39
+
40
+ ### Tags
41
+ Adding the tagging feature requires the _toto_prerelease_ as mentioned above, since we need the http request to apply our little hack.
42
+
43
+ To add a list of tags to your article, just use a custom yaml attribute:
44
+
45
+ title: The Wonderful Wizard of Oz
46
+ author: Lyman Frank Baum
47
+ date: 1900/05/17
48
+ tags: hacks, love, rock 'n' roll
49
+
50
+ Dorothy lived in the midst of the great Kansas prairies, with Uncle Henry,
51
+ who was a farmer, and Aunt Em, who was the farmer's wife.
52
+
53
+ Next, you need a place to show the tag links, for example the index.rhtml:
54
+
55
+ <section id="articles">
56
+ <% require 'karakuri' %>
57
+ <% for article in articles[0...10] %>
58
+ <article class="post">
59
+ <header>
60
+ <h1><a href="<%= article.path %>"><%= article.title %></a></h1>
61
+ <span class="descr"><%= article.date %></span><% 10.times { %>&nbsp;<%}%>
62
+ <span class="tags">
63
+ <%= Karakuri::tag_link_list(article[:tags]) %>
64
+ </span><% 10.times { %>&nbsp;<%}%>
65
+ .
66
+ .
67
+ .
68
+
69
+
70
+
71
+ And again: piece of cake ;-). Now all we need to add is a page that displays articles belonging to a ceratin tag:
72
+
73
+ Create a page called `tagged.rhtml` in your `templates/pages` directory that looks like this:
74
+
75
+
76
+ <%
77
+ require 'karakuri'
78
+ desired_tag = Karakuri::desired_tag(env["QUERY_STRING"])
79
+ %>
80
+ <h1>Posts filed under '<%= desired_tag %>': </h1>
81
+ <ul>
82
+
83
+ <% Karakuri::desired_articles(@articles, desired_tag).each do |article| %>
84
+ <li>
85
+ <span class="descr"><a href="<%= article.path %>" alt="<%= article.title %>"><%= article.title %></a><br/></span>
86
+ </li>
87
+ <% end %>
88
+ </ul>
89
+ <br/>
90
+
91
+ Now, you did most likely implement a tag listing on your toto blog. Congrats!
92
+
93
+ ### Tag Cloud
94
+
95
+ Example usage:
96
+
97
+ <ul>
98
+ <li class="nav-header">Tags</li>
99
+ <% Karakuri::tag_cloud(@articles).each do |tag, freq| %>
100
+ <% size = (10 * freq) %>
101
+ <li style="font-size:<%= size %>px"><%= tag %></li>
102
+ <% end %>
103
+ </ul>
104
+
105
+ ### short url (via bit.ly)
106
+
107
+ To use a bit.ly shortened URL, just call the followin function inside a .rhtml file:
108
+
109
+ <%= Karakuri::short_url_bitly(<url>, <bit.ly login name>, <bit.ly api key>) %>
110
+
111
+
112
+ ### disqus comment counter
113
+
114
+ Basically just adds the necessary java script to enable the disqus comment counter. For best performance, place it near the end of the page:
115
+
116
+ <%= Karakuri::disqus_comment_count_js(@config[:disqus]) %>
117
+ </body>
118
+
119
+ </html>
120
+
121
+ Mind the usage of `@config[:disqus]`, this enables configuration via `config.ru`.
122
+
123
+ To access the comment count, use `#disqus_thread` at the end of the permalink to the post & it will be replaced with the disqus comment count:
124
+
125
+ <a href="<%= article.path %>#disqus_thread">&nbsp;</a>
126
+
127
+ Will result in the number of comments of the article the permalink posts to.
128
+
129
+ ### serious custom yaml field reader
130
+
131
+ I hacked serious' custom yaml field access (quite dirty hack) - but please refer to the source & ri for that, I don't use serious anymore.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require 'rake/rdoctask'
11
+ require 'rake/testtask'
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.name = 'bao-karakuri'
15
+ s.version = '0.1.2'
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ['README.md', 'LICENSE']
18
+ s.summary = 'Fork of 5v3n/karakuri - Some handy helpers for toto and the likes...'
19
+ s.description = s.summary
20
+ s.author = 'Bao Pham'
21
+ s.email = 'gbaopham@gmail.com'
22
+ s.homepage = 'http://github.com/baopham/karakuri'
23
+ # s.executables = ['your_executable_here']
24
+ s.files = %w(LICENSE README.md Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
25
+ s.require_path = "lib"
26
+ s.bindir = "bin"
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |p|
30
+ p.gem_spec = spec
31
+ p.need_tar = true
32
+ p.need_zip = true
33
+ end
34
+
35
+ Rake::RDocTask.new do |rdoc|
36
+ files =['README.md', 'LICENSE', 'lib/**/*.rb']
37
+ rdoc.rdoc_files.add(files)
38
+ rdoc.main = "README.md" # page to start on
39
+ rdoc.title = "Karakuri Docs"
40
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
41
+ rdoc.options << '--line-numbers'
42
+ end
43
+
44
+ Rake::TestTask.new do |t|
45
+ t.test_files = FileList['test/**/*.rb']
46
+ end
data/lib/karakuri.rb ADDED
@@ -0,0 +1,131 @@
1
+ require 'cgi'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ #
6
+ # Some useful feature for toto and the likes. Basically, any ruby based blog or site.
7
+ #
8
+ module Karakuri
9
+ #
10
+ # create a list of links to tagged articles, default link_format: <code>%&amp;&lt;a href=&quot;/tagged?tag=#{tag}&quot; alt=&quot;articles concerning #{tag}&quot; &gt;#{tag}&lt;/a&gt; &amp;</code>
11
+ #
12
+ def Karakuri.tag_link_list(csv_string)
13
+ # read csv-string into array
14
+ tag_list = csv_to_array(csv_string)
15
+ if tag_list
16
+ tag_string = ""
17
+ #TODO pass a format via parameter
18
+ tag_list.each { |tag| tag_string << %&<a href="/tagged?tag=#{tag}" alt="articles concerning #{tag}" >#{tag}</a> & }
19
+ end
20
+ tag_string
21
+ end
22
+ #
23
+ # processes a csv-string into an array
24
+ #
25
+ def Karakuri.csv_to_array(csv_string)
26
+ #split & handle forgotten spaces after the separator. then flatten the multidemnsional array:
27
+ csv_string.split(', ').map{ |e| e.split(',')}.flatten if csv_string
28
+ end
29
+ #
30
+ # pass the path (@path for a toto blog) & the desired SEO ending, e.g. the name of your blog.
31
+ # example for toto: <code>seo_friendly_title(@path, title, "mysite.com") will produce 'subpage | mysite.com' as seo friendly page title.</code>
32
+ #
33
+ def Karakuri.seo_friendly_title(path, title, seo_ending)
34
+ #TODO use custom title separator...
35
+ if path == 'index'
36
+ page_title = seo_ending
37
+ elsif path.split('/').compact.length == 4
38
+ page_title = title << " | #{seo_ending}"
39
+ else
40
+ page_title = path.capitalize.gsub(/[-]/, ' ') << " | #{seo_ending}"
41
+ end
42
+ page_title
43
+ end
44
+ #
45
+ #Generates javascript to include to the bottom of your index page.
46
+ #Appending '#disqus_thread' to the end of permalinks will replace the text of these links with the comment count.
47
+ #
48
+ #For example, you may have a link with this HTML: <code>&lt;a href=&quot;http://example.com/my_article.html#disqus_thread&quot;&gt;Comments&lt;/a&gt; </code> The comment count code will replace the text "Comments" with the number of comments on the page
49
+ #
50
+ #(see http://disqus.com/comments/universal/ for details)
51
+ #
52
+ def Karakuri.disqus_comment_count_js(disqus_shortname)
53
+ %&
54
+ <script type="text/javascript">
55
+ var disqus_shortname = '#{disqus_shortname}';
56
+ (function () {
57
+ var s = document.createElement('script'); s.async = true;
58
+ s.src = 'http://disqus.com/forums/#{disqus_shortname}/count.js';
59
+ (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
60
+ }());
61
+ </script>
62
+
63
+ & if disqus_shortname
64
+ end
65
+ #
66
+ # Retrieve bit.ly shortened url
67
+ #
68
+ def Karakuri.short_url_bitly(url, login, api_key)
69
+ if api_key != "" && login != ""
70
+ rest_call=%{http://api.bit.ly/v3/shorten?login=#{login}&apikey=#{api_key}&longUrl=#{url}&format=txt}
71
+ begin
72
+ Net::HTTP::get(URI.parse(rest_call)) # handle http errors (esp. timeouts!)
73
+ rescue URI::InvalidURIError
74
+ raise URI::InvalidURIError
75
+ rescue
76
+ url#in the case of a web service or HTTP error, we'll just ignore it & return the long url
77
+ end
78
+ else
79
+ url #fallback: return long url if no proper login has been provided. TODO: check if a call w/o login is possible
80
+ end
81
+ end
82
+ #desired articles matching a corresponding tag
83
+ def Karakuri.desired_articles(articles, tag)
84
+ if(articles && tag)
85
+ articles.select do |a|
86
+ tags = Karakuri::csv_to_array(a[:tags])
87
+ tags.include?(tag) if tags
88
+ end
89
+ end
90
+ end
91
+ # extract desired tag from <code>env["QUERY_STRING"]</code> or an equally formed expression, e.g. tag=desired_tag
92
+ def Karakuri.desired_tag(query_string)
93
+ if query_string
94
+ start = query_string.index("tag=")
95
+ if start
96
+ start = start + 3
97
+ stop = query_string.index("&")
98
+ stop = 0 unless stop
99
+ desired_tag = query_string[start+1..stop-1]
100
+ desired_tag = CGI::unescape(desired_tag)
101
+ else
102
+ '' #fallback: return empty string to prevent nil errors
103
+ end
104
+ end
105
+ end
106
+
107
+ #
108
+ # Tag cloud, return a hash of tags and their frequency
109
+ #
110
+ def Karakuri.tag_cloud(articles)
111
+ tag_cloud = {}
112
+ if(articles)
113
+ articles.select do |a|
114
+ tags = csv_to_array(a[:tags])
115
+ if tags
116
+ tags.each { |tag|
117
+ tag_string = %&<a href="/tagged?tag=#{tag}" alt="articles concerning #{tag}" >#{tag}</a>&
118
+ if tag_cloud.has_key? tag_string
119
+ tag_cloud[tag_string] += 1
120
+ else
121
+ tag_cloud[tag_string] = 1
122
+ end
123
+ }
124
+ end
125
+ end
126
+ end
127
+ tag_cloud
128
+ end
129
+
130
+ end
131
+
@@ -0,0 +1,13 @@
1
+ # extension to Karakuri with special methods for serious
2
+ require 'karakuri'
3
+ module SeriousBlogHelper
4
+ #extract tags from article
5
+ def SeriousBlogHelper.tags_from_article(article=nil)
6
+ tags_marker = %&tags"=>&
7
+ if article && article.inspect.index(tags_marker)
8
+ tags_start_index=article.inspect.index(tags_marker) + tags_marker.length
9
+ tags_end_index = article.inspect.index("\"," ,tags_start_index)
10
+ tags = article.inspect[tags_start_index+1..tags_end_index-1]
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bao-karakuri
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Bao Pham
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-03-22 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Fork of 5v3n/karakuri - Some handy helpers for toto and the likes...
22
+ email: gbaopham@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.md
29
+ - LICENSE
30
+ files:
31
+ - LICENSE
32
+ - README.md
33
+ - Rakefile
34
+ - lib/karakuri.rb
35
+ - lib/serious_blog_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/baopham/karakuri
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.6
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Fork of 5v3n/karakuri - Some handy helpers for toto and the likes...
66
+ test_files: []
67
+