stupid_formatter 0.1.1 → 0.2.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.
- data/README.rdoc +97 -5
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/stupid_formatter.rb +23 -3
- data/stupid_formatter.gemspec +6 -6
- data/test/test_stupid_formatter.rb +10 -0
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,23 +1,115 @@
|
|
1
1
|
= StupidFormatter
|
2
2
|
|
3
|
-
|
3
|
+
StupidFormatter let's you pipe text through processors in a chain by abstracting the actual implementation
|
4
|
+
details of each processor into a unified API. By default, it can do ERb, Markdown (via RDiscount) and
|
5
|
+
syntax-highlight with CodeRay.
|
6
|
+
|
7
|
+
Say you want to format some markdown-formatted text with RDiscount and also include some ERb processing
|
8
|
+
with CodeRay syntax highlighting, you could configure the chain somewhere at the beginning of your app like so:
|
4
9
|
|
5
10
|
StupidFormatter.chain = [StupidFormatter::Erb, StupidFormatter::RDiscount]
|
11
|
+
|
12
|
+
Then, pass in your Markdown-marked-up, ERb-riddled string to <code>StupidFormatter.result</code> or
|
13
|
+
just call <code>string.formatted</code> and you'll see results.
|
14
|
+
|
15
|
+
The ERb, RDiscount chain mentioned above is actually the default, so if you want to stick with that,
|
16
|
+
just skip that chain config step (yes, it is a stupid example).
|
17
|
+
|
18
|
+
Formatters are processed in the order they are given in the Array, so ERb comes first, then RDiscount
|
19
|
+
in this example.
|
6
20
|
|
7
|
-
|
8
|
-
see results. A coderay helper is included for erb, see test/fixtures/erb_coderay_markdown_example.txt
|
21
|
+
To install, do:
|
9
22
|
|
10
|
-
|
23
|
+
sudo gem install stupid_formatter
|
24
|
+
|
25
|
+
This is so completely alpha, you have no idea! Don't expect beautiful code or RDoc.
|
11
26
|
|
27
|
+
== Using the formatters directly
|
28
|
+
|
29
|
+
Of course, you can access the formatters directly as well. To format a string with RDiscount, do:
|
30
|
+
|
31
|
+
StupidFormatter::RDiscount.new("# some markdown").result
|
32
|
+
|
12
33
|
== Custom formatters
|
13
34
|
|
14
|
-
You can define your own formatters
|
35
|
+
You can define your own formatters very easily by conforming to the public API, which is:
|
15
36
|
|
16
37
|
* inherit from StupidFormatter::AbstractFormatter
|
17
38
|
* make a result instance method that does not take any arguments
|
18
39
|
* use the local attribute 'input' to do whatever you want with the text, and return it
|
19
40
|
|
41
|
+
=== Example custom formatter
|
42
|
+
|
43
|
+
In our example, let's create a formatter that will reverse the text.
|
44
|
+
|
45
|
+
class StupidFormatter::WrittenInReverse < StupidFormatter::AbstractFormatter
|
46
|
+
def result
|
47
|
+
input.reverse
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
After adding that to the chain with
|
52
|
+
|
53
|
+
StupidFormatter.chain << StupidFormatter::WrittenInReverse
|
54
|
+
|
55
|
+
Your text will be reversed when calling <code>"some string".formatted</code>
|
56
|
+
|
57
|
+
== ERb Helpers
|
58
|
+
|
59
|
+
=== Capturing blocks with ERb
|
60
|
+
|
61
|
+
The ERb-formatter offers a capture-helper, which allows you to capture the content of a block into
|
62
|
+
a variable like so:
|
63
|
+
|
64
|
+
<% @foo = capture do %>
|
65
|
+
Bar
|
66
|
+
<% end %>
|
67
|
+
Foo<%= @foo %>
|
68
|
+
|
69
|
+
The above example will render into "FooBar". This functionality is used in the Coderay helper, which
|
70
|
+
is automatically included in the ERb formatter, see next section.
|
71
|
+
|
72
|
+
=== Syntax highlighting with Coderay
|
73
|
+
|
74
|
+
With the CoderayHelper that gets included into the ERb formatter automatically, you can get syntax-highlighting
|
75
|
+
very easily:
|
76
|
+
|
77
|
+
<% highlight do %>
|
78
|
+
puts 'Your Awesome Ruby(tm) here!'
|
79
|
+
<% end %>
|
80
|
+
|
81
|
+
The language defaults to :ruby, but you can pass in any CodeRay-language to the highlight method:
|
82
|
+
|
83
|
+
<% highlight :javascript do %>
|
84
|
+
var foo = 'bar';
|
85
|
+
<% end %>
|
86
|
+
|
87
|
+
The highlighting is configured to use CSS instead of in-place colors, so you might want to add a
|
88
|
+
decent (http://railscasts.com/stylesheets/coderay.css) coderay css file into your HTML.
|
89
|
+
|
90
|
+
Thanks Ryan for making that great CodeRay CSS and giving it away (http://railscasts.com/episodes/46-catch-all-route)!
|
91
|
+
|
92
|
+
=== Making your own helpers
|
93
|
+
|
94
|
+
Let's make a helper that reverses the text you gave in the block, like so:
|
95
|
+
|
96
|
+
<% written_in_reverse do %>
|
97
|
+
anna
|
98
|
+
<% end %>
|
99
|
+
|
100
|
+
Just create a module that uses the capture helper and appends the result to the @output_buffer and include it
|
101
|
+
in the ERb helper, like so:
|
102
|
+
|
103
|
+
module WrittenInReverse
|
104
|
+
def written_in_reverse
|
105
|
+
text = capture { yield }
|
106
|
+
@output_buffer << text.reverse
|
107
|
+
end
|
108
|
+
end
|
109
|
+
StupidFormatter::ERb.send :include, WrittenInReverse
|
20
110
|
|
111
|
+
This will print 'anna' in your ERb. Easy, right?
|
112
|
+
|
21
113
|
== Note on Patches/Pull Requests
|
22
114
|
|
23
115
|
* Fork the project.
|
data/Rakefile
CHANGED
@@ -6,13 +6,13 @@ begin
|
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "stupid_formatter"
|
8
8
|
gem.summary = %Q{A stupid formatter for piping text through markup processors}
|
9
|
-
gem.description = %Q{
|
9
|
+
gem.description = %Q{A stupid formatter for piping text through markup processors with a unified API}
|
10
10
|
gem.email = "christoph at olszowka dot de"
|
11
11
|
gem.homepage = "http://github.com/colszowka/stupid_formatter"
|
12
12
|
gem.authors = ["Christoph Olszowka"]
|
13
13
|
gem.add_dependency "rdiscount", ">= 1.5.0"
|
14
14
|
gem.add_dependency "coderay", ">= 0.9.0"
|
15
|
-
gem.add_development_dependency "
|
15
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
16
16
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
17
|
end
|
18
18
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/stupid_formatter.rb
CHANGED
@@ -2,19 +2,36 @@ require 'rdiscount'
|
|
2
2
|
require 'erb'
|
3
3
|
require 'coderay'
|
4
4
|
|
5
|
+
class String
|
6
|
+
#
|
7
|
+
# Pipes this string through the StupidFormatter and it's configured chain.
|
8
|
+
# The same as StupidFormatter.result(some_string)
|
9
|
+
#
|
10
|
+
def formatted
|
11
|
+
StupidFormatter.result(self)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
5
15
|
module StupidFormatter
|
6
|
-
|
7
|
-
# Make the formatter chain configurable.
|
8
16
|
class << self
|
17
|
+
#
|
18
|
+
# Returns the current processing chain. Defaults to [StupidFormatter::Erb, StupidFormatter::RDiscount]
|
19
|
+
#
|
9
20
|
def chain
|
10
|
-
@chain ||= [StupidFormatter::RDiscount]
|
21
|
+
@chain ||= [StupidFormatter::Erb, StupidFormatter::RDiscount]
|
11
22
|
end
|
12
23
|
|
24
|
+
#
|
25
|
+
# Make the formatter chain configurable. Pass in an Array of Formatters in the order
|
26
|
+
# you want them processed in.
|
27
|
+
#
|
13
28
|
def chain=(chain)
|
14
29
|
@chain = chain
|
15
30
|
end
|
16
31
|
|
32
|
+
#
|
17
33
|
# Will put the input string through all formatters in the chain
|
34
|
+
#
|
18
35
|
def result(input)
|
19
36
|
output = input.clone
|
20
37
|
chain.each do |formatter|
|
@@ -24,6 +41,9 @@ module StupidFormatter
|
|
24
41
|
end
|
25
42
|
end
|
26
43
|
|
44
|
+
#
|
45
|
+
# Base class for formatters, providing the basic API.
|
46
|
+
#
|
27
47
|
class AbstractFormatter
|
28
48
|
attr_reader :input
|
29
49
|
|
data/stupid_formatter.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{stupid_formatter}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Christoph Olszowka"]
|
12
|
-
s.date = %q{2010-02-
|
13
|
-
s.description = %q{
|
12
|
+
s.date = %q{2010-02-13}
|
13
|
+
s.description = %q{A stupid formatter for piping text through markup processors with a unified API}
|
14
14
|
s.email = %q{christoph at olszowka dot de}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
@@ -51,16 +51,16 @@ Gem::Specification.new do |s|
|
|
51
51
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
52
|
s.add_runtime_dependency(%q<rdiscount>, [">= 1.5.0"])
|
53
53
|
s.add_runtime_dependency(%q<coderay>, [">= 0.9.0"])
|
54
|
-
s.add_development_dependency(%q<
|
54
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
55
55
|
else
|
56
56
|
s.add_dependency(%q<rdiscount>, [">= 1.5.0"])
|
57
57
|
s.add_dependency(%q<coderay>, [">= 0.9.0"])
|
58
|
-
s.add_dependency(%q<
|
58
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
59
59
|
end
|
60
60
|
else
|
61
61
|
s.add_dependency(%q<rdiscount>, [">= 1.5.0"])
|
62
62
|
s.add_dependency(%q<coderay>, [">= 0.9.0"])
|
63
|
-
s.add_dependency(%q<
|
63
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -25,6 +25,16 @@ class TestStupidFormatter < Test::Unit::TestCase
|
|
25
25
|
assert_equal fixtures(:erb_coderay_markdown_expectation), @result
|
26
26
|
end
|
27
27
|
end
|
28
|
+
|
29
|
+
context "when I call formatted on a complex fixture string" do
|
30
|
+
setup do
|
31
|
+
@result = fixtures(:erb_coderay_markdown_example).formatted
|
32
|
+
end
|
33
|
+
|
34
|
+
should "render as in the expectation fixture" do
|
35
|
+
assert_equal fixtures(:erb_coderay_markdown_expectation), @result
|
36
|
+
end
|
37
|
+
end
|
28
38
|
end
|
29
39
|
|
30
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stupid_formatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christoph Olszowka
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-13 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: 0.9.0
|
34
34
|
version:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
|
-
name:
|
36
|
+
name: shoulda
|
37
37
|
type: :development
|
38
38
|
version_requirement:
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -42,7 +42,7 @@ dependencies:
|
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: "0"
|
44
44
|
version:
|
45
|
-
description:
|
45
|
+
description: A stupid formatter for piping text through markup processors with a unified API
|
46
46
|
email: christoph at olszowka dot de
|
47
47
|
executables: []
|
48
48
|
|