karakuri 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.
Files changed (6) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +122 -0
  3. data/Rakefile +46 -0
  4. data/lib/karakuri.rb +107 -0
  5. data/lib/serious_blog_helper.rb +13 -0
  6. metadata +72 -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,122 @@
1
+ # BlogHelper
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.
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 use the `gem` tool to get going with the _blog_helper_: `sudo gem install blog_helper`
17
+
18
+ If you want to use features that rely on accessing the http requests like the _tag_ feature, you'll need to use the [_toto_prerelease_](http://github.com/5v3n/toto).
19
+
20
+ Please follow the instrucions there to do so.
21
+
22
+ ## Usage
23
+
24
+ Piece of cake, again: all you have to do is use `<% require 'blog_helper'%>` in your .rhtml or .erb file and call the corresponding methods.
25
+
26
+ ### SEO friendly titles
27
+ For example, to use seo friendly titles, your layout.rhtml should be looking like this:
28
+
29
+
30
+ <!doctype html>
31
+ <html>
32
+ <head>
33
+ <% require 'blog_helper'
34
+ page_title = BlogHelper::seo_friendly_title(@path, title, 'yourSitesTitle.com')
35
+ %>
36
+ <title><%= page_title %></title>
37
+ <link rel="alternate" type="application/atom+xml" title="<%= page_title %> - feed" href="/index.xml" />
38
+ .
39
+ .
40
+ .
41
+
42
+ ### Tags
43
+ Adding the tagging feature requires the _toto_prerelease_ as mentioned above, since we need the http request to apply our little hack.
44
+
45
+ To add a list of tags to your article, just use a custom yaml attribute:
46
+
47
+ title: The Wonderful Wizard of Oz
48
+ author: Lyman Frank Baum
49
+ date: 1900/05/17
50
+ tags: hacks, love, rock 'n' roll
51
+
52
+ Dorothy lived in the midst of the great Kansas prairies, with Uncle Henry,
53
+ who was a farmer, and Aunt Em, who was the farmer's wife.
54
+
55
+ Next, you need a place to show the tag links, for example the index.rhtml:
56
+
57
+ <section id="articles">
58
+ <% require 'blog_helper' %>
59
+ <% for article in articles[0...10] %>
60
+ <article class="post">
61
+ <header>
62
+ <h1><a href="<%= article.path %>"><%= article.title %></a></h1>
63
+ <span class="descr"><%= article.date %></span><% 10.times { %>&nbsp;<%}%>
64
+ <span class="tags">
65
+ <%= BlogHelper::tag_link_list(article[:tags]) %>
66
+ </span><% 10.times { %>&nbsp;<%}%>
67
+ .
68
+ .
69
+ .
70
+
71
+
72
+
73
+ And again: piece of caked ;-). Now all we need to add is a page that displays articles belonging to a ceratin tag:
74
+
75
+ Create a page called `tagged.rhtml` in your `templates/pages` directory that looks like this:
76
+
77
+
78
+ <%
79
+ require 'blog_helper'
80
+ desired_tag = BlogHelper::desired_tag(env["QUERY_STRING"])
81
+ %>
82
+ <h1>Posts filed under '<%= desired_tag %>': </h1>
83
+ <ul>
84
+
85
+ <% BlogHelper::desired_articles(@articles, desired_tag).each do |article| %>
86
+ <li>
87
+ <span class="descr"><a href="<%= article.path %>" alt="<%= article.title %>"><%= article.title %></a><br/></span>
88
+ </li>
89
+ <% end %>
90
+ </ul>
91
+ <br/>
92
+
93
+ Now, you did most likely implement a tag listing on your toto blog. Congrats!
94
+
95
+
96
+ ### short url (via bit.ly)
97
+
98
+ To use a bit.ly shortened URL, just call the followin function inside a .rhtml file:
99
+
100
+ <%= BlogHelper::short_url_bitly(<url>, <bit.ly login name>, <bit.ly api key>) %>
101
+
102
+
103
+ ### disqus comment counter
104
+
105
+ Basically just adds the necessary java script to enable the disqus comment counter. For best performance, place it near the end of the page:
106
+
107
+ <%= BlogHelper::disqus_comment_count_js(@config[:disqus]) %>
108
+ </body>
109
+
110
+ </html>
111
+
112
+ Mind the usage of `@config[:disqus]`, this enables configuration via `config.ru`.
113
+
114
+ 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:
115
+
116
+ <a href="<%= article.path %>#disqus_thread">&nbsp;</a>
117
+
118
+ Will result in the number of comments of the article the permalink posts to.
119
+
120
+ ### serious custom yaml field reader
121
+
122
+ TBD... I have to refer to the source & ri for now.
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 = 'karakuri'
15
+ s.version = '0.1.0'
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ['README.md', 'LICENSE']
18
+ s.summary = 'Some handy helpers for toto and the likes...'
19
+ s.description = s.summary
20
+ s.author = 'Sven Kraeuter'
21
+ s.email = 'mail@5v3n.com'
22
+ s.homepage = 'http://github.com/5v3n/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" # 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,107 @@
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
+ end
@@ -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,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: karakuri
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Sven Kraeuter
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-21 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Some handy helpers for toto and the likes...
23
+ email: mail@5v3n.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.md
30
+ - LICENSE
31
+ files:
32
+ - LICENSE
33
+ - README.md
34
+ - Rakefile
35
+ - lib/karakuri.rb
36
+ - lib/serious_blog_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/5v3n/karakuri
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Some handy helpers for toto and the likes...
71
+ test_files: []
72
+