stupid_formatter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/stupid_formatter.rb +74 -0
- data/stupid_formatter.gemspec +60 -0
- data/test/fixtures/erb_coderay_markdown_example.txt +11 -0
- data/test/fixtures/erb_coderay_markdown_expectation.txt +12 -0
- data/test/helper.rb +18 -0
- data/test/test_erb_formatter.rb +33 -0
- data/test/test_rdiscount_formatter.rb +17 -0
- data/test/test_stupid_formatter.rb +30 -0
- metadata +81 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Christoph Olszowka
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= StupidFormatter
|
2
|
+
|
3
|
+
This is a super-stupid gem for formatting text in a chain. You can change the chain by doing this:
|
4
|
+
|
5
|
+
StupidFormatter.chain = [StupidFormatter::ErbWithCoderay, StupidFormatter::RDiscount]
|
6
|
+
|
7
|
+
Then, pass in your Markdown-markedup, erb-riddled string to StupidFormatter.result and you'll
|
8
|
+
see results.
|
9
|
+
|
10
|
+
This is so completely alpha, you have no idea!
|
11
|
+
|
12
|
+
== Custom formatters
|
13
|
+
|
14
|
+
You can define your own formatters and by conforming to the public api, which is:
|
15
|
+
|
16
|
+
* inherit from StupidFormatter::AbstractFormatter
|
17
|
+
* make a result instance method that does not take any arguments
|
18
|
+
* use the local attribute 'input' to do whatever you want with the text, and return it
|
19
|
+
|
20
|
+
|
21
|
+
== Note on Patches/Pull Requests
|
22
|
+
|
23
|
+
* Fork the project.
|
24
|
+
* Make your feature addition or bug fix.
|
25
|
+
* Add tests for it. This is important so I don't break it in a
|
26
|
+
future version unintentionally.
|
27
|
+
* Commit, do not mess with rakefile, version, or history.
|
28
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
29
|
+
* Send me a pull request. Bonus points for topic branches.
|
30
|
+
|
31
|
+
== Copyright
|
32
|
+
|
33
|
+
Copyright (c) 2010 Christoph Olszowka. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "stupid_formatter"
|
8
|
+
gem.summary = %Q{A stupid formatter for piping text through markup processors}
|
9
|
+
gem.description = %Q{With stupid_formatter you can pipe your blog post through erb and markdown in a chain}
|
10
|
+
gem.email = "christoph at olszowka dot de"
|
11
|
+
gem.homepage = "http://github.com/colszowka/stupid_formatter"
|
12
|
+
gem.authors = ["Christoph Olszowka"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "stupid_formatter #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'rdiscount'
|
2
|
+
require 'erb'
|
3
|
+
require 'coderay'
|
4
|
+
|
5
|
+
module StupidFormatter
|
6
|
+
|
7
|
+
# Make the formatter chain configurable.
|
8
|
+
class << self
|
9
|
+
def chain
|
10
|
+
@chain ||= [StupidFormatter::RDiscount]
|
11
|
+
end
|
12
|
+
|
13
|
+
def chain=(chain)
|
14
|
+
@chain = chain
|
15
|
+
end
|
16
|
+
|
17
|
+
# Will put the input string through all formatters in the chain
|
18
|
+
def result(input)
|
19
|
+
output = input.clone
|
20
|
+
chain.each do |formatter|
|
21
|
+
output = formatter.new(output).result
|
22
|
+
end
|
23
|
+
output
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class AbstractFormatter
|
28
|
+
attr_reader :input
|
29
|
+
|
30
|
+
def initialize(input)
|
31
|
+
@input = input.to_s.strip
|
32
|
+
raise "Please use this only in subclasses" if self.class == AbstractFormatter
|
33
|
+
end
|
34
|
+
|
35
|
+
def result
|
36
|
+
raise "This should be implemented by subclasses"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Erb < AbstractFormatter
|
41
|
+
def result(alternative_binding=nil)
|
42
|
+
ERB.new(input, 0, "%<>", "@output_buffer").result(alternative_binding || binding)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Helper for capturing output in a erb block for later use, i.e.
|
46
|
+
# <% @my_var = capture do %>
|
47
|
+
# Bar
|
48
|
+
# <% end %>
|
49
|
+
# Foo<%= @my_var %>
|
50
|
+
# will render FooBar.
|
51
|
+
#
|
52
|
+
def capture
|
53
|
+
old_buffer, @output_buffer = @output_buffer, ''
|
54
|
+
yield
|
55
|
+
@output_buffer
|
56
|
+
ensure
|
57
|
+
@output_buffer = old_buffer
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Actually, this might get converted into a module that can optionally be mixed into the formatter
|
62
|
+
class ErbWithCoderay < Erb
|
63
|
+
def highlight(language=:ruby)
|
64
|
+
code = capture { yield }
|
65
|
+
@output_buffer << CodeRay.scan(code, language).div(:css => :class)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class RDiscount < AbstractFormatter
|
70
|
+
def result
|
71
|
+
Markdown.new(input).to_html
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{stupid_formatter}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Christoph Olszowka"]
|
12
|
+
s.date = %q{2010-02-11}
|
13
|
+
s.description = %q{With stupid_formatter you can pipe your blog post through erb and markdown in a chain}
|
14
|
+
s.email = %q{christoph at olszowka dot de}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/stupid_formatter.rb",
|
27
|
+
"stupid_formatter.gemspec",
|
28
|
+
"test/fixtures/erb_coderay_markdown_example.txt",
|
29
|
+
"test/fixtures/erb_coderay_markdown_expectation.txt",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/test_erb_formatter.rb",
|
32
|
+
"test/test_rdiscount_formatter.rb",
|
33
|
+
"test/test_stupid_formatter.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/colszowka/stupid_formatter}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.5}
|
39
|
+
s.summary = %q{A stupid formatter for piping text through markup processors}
|
40
|
+
s.test_files = [
|
41
|
+
"test/test_stupid_formatter.rb",
|
42
|
+
"test/helper.rb",
|
43
|
+
"test/test_erb_formatter.rb",
|
44
|
+
"test/test_rdiscount_formatter.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<h1>A heading</h1>
|
2
|
+
|
3
|
+
<p>A basic paragraph</p>
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
<div class="CodeRay">
|
9
|
+
<div class="code"><pre> puts <span class="s"><span class="dl">"</span><span class="k">foo bar baz</span><span class="dl">"</span></span>
|
10
|
+
</pre></div>
|
11
|
+
</div>
|
12
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
require 'stupid_formatter'
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
#
|
11
|
+
# Loads fixture files into a string
|
12
|
+
#
|
13
|
+
def fixtures(filename)
|
14
|
+
@fixtures ||= {} # Initialize cache if not present
|
15
|
+
return @fixtures[filename.to_sym] if @fixtures[filename.to_sym] # Return if cached
|
16
|
+
@fixtures[filename.to_sym] = File.read( File.join(File.dirname(__FILE__), 'fixtures', "#{filename}.txt") )
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestErbFormatter < Test::Unit::TestCase
|
4
|
+
context "A chunk of erb-formatted text" do
|
5
|
+
setup do
|
6
|
+
@text = "<%= 'foo bar baz' %>"
|
7
|
+
end
|
8
|
+
|
9
|
+
context "after piping it through a new erb formatter instance" do
|
10
|
+
setup do
|
11
|
+
@result = StupidFormatter::Erb.new(@text).result
|
12
|
+
end
|
13
|
+
|
14
|
+
should("render 'foo bar baz'") { assert_equal 'foo bar baz', @result.strip.chomp }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "A chunk of erb-formatted text that includes a block using the highlight-syntax" do
|
19
|
+
setup do
|
20
|
+
@text = '<% highlight do %>
|
21
|
+
puts "foo bar baz"
|
22
|
+
<% end %>'
|
23
|
+
end
|
24
|
+
|
25
|
+
context "after piping it through a new erb with coderay formatter instance" do
|
26
|
+
setup do
|
27
|
+
@result = StupidFormatter::ErbWithCoderay.new(@text).result
|
28
|
+
end
|
29
|
+
|
30
|
+
should('match <div class="CodeRay">') { assert_match /\<div class="CodeRay"\>/, @result.strip.chomp }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestRDiscountFormatter < Test::Unit::TestCase
|
4
|
+
context "With a chunk of markdown-formatted text" do
|
5
|
+
setup do
|
6
|
+
@text = "# some heading"
|
7
|
+
end
|
8
|
+
|
9
|
+
context "after piping it through a new rdiscount formatter instance" do
|
10
|
+
setup do
|
11
|
+
@result = StupidFormatter::RDiscount.new(@text).result
|
12
|
+
end
|
13
|
+
|
14
|
+
should("render '<h1>some heading</h1>'") { assert_equal '<h1>some heading</h1>', @result.strip.chomp }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestStupidFormatter < Test::Unit::TestCase
|
4
|
+
context "After configuring the StupidFormatter.chain to [StupidFormatter::ErbWithCoderay, StupidFormatter::RDiscount]" do
|
5
|
+
setup do
|
6
|
+
StupidFormatter.chain = [StupidFormatter::ErbWithCoderay, StupidFormatter::RDiscount]
|
7
|
+
end
|
8
|
+
|
9
|
+
should "return StupidFormatter::ErbWithCoderay for the first chain item" do
|
10
|
+
assert_equal StupidFormatter::ErbWithCoderay, StupidFormatter.chain[0]
|
11
|
+
end
|
12
|
+
|
13
|
+
should "return StupidFormatter::RDiscount for the last chain item" do
|
14
|
+
assert_equal StupidFormatter::RDiscount, StupidFormatter.chain[-1]
|
15
|
+
end
|
16
|
+
|
17
|
+
should("have 2 chain items") { assert_equal 2, StupidFormatter.chain.length }
|
18
|
+
|
19
|
+
context "when I put a complex erb/coderay/markdown fixture through the formatter" do
|
20
|
+
setup do
|
21
|
+
@result = StupidFormatter.result(fixtures(:erb_coderay_markdown_example))
|
22
|
+
end
|
23
|
+
|
24
|
+
should "render as in the expectation fixture" do
|
25
|
+
assert_equal fixtures(:erb_coderay_markdown_expectation), @result
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stupid_formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christoph Olszowka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-11 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: With stupid_formatter you can pipe your blog post through erb and markdown in a chain
|
26
|
+
email: christoph at olszowka dot de
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/stupid_formatter.rb
|
42
|
+
- stupid_formatter.gemspec
|
43
|
+
- test/fixtures/erb_coderay_markdown_example.txt
|
44
|
+
- test/fixtures/erb_coderay_markdown_expectation.txt
|
45
|
+
- test/helper.rb
|
46
|
+
- test/test_erb_formatter.rb
|
47
|
+
- test/test_rdiscount_formatter.rb
|
48
|
+
- test/test_stupid_formatter.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/colszowka/stupid_formatter
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --charset=UTF-8
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: A stupid formatter for piping text through markup processors
|
77
|
+
test_files:
|
78
|
+
- test/test_stupid_formatter.rb
|
79
|
+
- test/helper.rb
|
80
|
+
- test/test_erb_formatter.rb
|
81
|
+
- test/test_rdiscount_formatter.rb
|