stupider_formatter 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3aa339e868dec2d179d8bdcfff89d657ae427c6f9ed43ea3638bcc9a6cb34e8
4
+ data.tar.gz: 2397a49f2d19e667df4f4ff624a5371627406b3174a276499547449b92f28487
5
+ SHA512:
6
+ metadata.gz: 719b38d16b0c5ece526e405beef0d64a22890684d7bdef39d3a6153fe116d9e4a1563c907a4a14595ad8388bd44f6347ae4aeebace94837ec93781db237a4743
7
+ data.tar.gz: e351317d869247b6df50ca5f8826b3e60ed8ddd2a86b99c7ee48ef9da5776a68d5f4196e3916536dd3c3bf6e998aff27fbafc4c307113106f87cb9b0229bfdb7
data/.bundle/config ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ BUNDLE_PATH: "vendor"
3
+ BUNDLE_BIN: "bin"
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
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,129 @@
1
+ = StupiderFormatter
2
+
3
+ This is a fork from https://github.com/colszowka/stupid_formatter
4
+
5
+ = StupidFormatter
6
+
7
+ StupidFormatter let's you pipe text through processors in a chain by abstracting the actual implementation
8
+ details of each processor into a unified API. By default, it can do ERb, Markdown (via RDiscount) and
9
+ syntax-highlight with CodeRay.
10
+
11
+ Say you want to format some markdown-formatted text with RDiscount and also include some ERb processing
12
+ with CodeRay syntax highlighting, you could configure the chain somewhere at the beginning of your app like so:
13
+
14
+ StupidFormatter.chain = [StupidFormatter::Erb, StupidFormatter::RDiscount]
15
+
16
+ Then, pass in your Markdown-marked-up, ERb-riddled string to <code>StupidFormatter.result</code> or
17
+ just call <code>string.formatted</code> and you'll see results.
18
+
19
+ The ERb, RDiscount chain mentioned above is actually the default, so if you want to stick with that,
20
+ just skip that chain config step (yes, it is a stupid example).
21
+
22
+ Formatters are processed in the order they are given in the Array, so ERb comes first, then RDiscount
23
+ in this example.
24
+
25
+ To install, do:
26
+
27
+ sudo gem install stupid_formatter
28
+
29
+ This is so completely alpha, you have no idea! Don't expect beautiful code or RDoc.
30
+
31
+ == Using the formatters directly
32
+
33
+ Of course, you can access the formatters directly as well. To format a string with RDiscount, do:
34
+
35
+ StupidFormatter::RDiscount.new("# some markdown").result
36
+
37
+ == Custom formatters
38
+
39
+ You can define your own formatters very easily by conforming to the public API, which is:
40
+
41
+ * inherit from StupidFormatter::AbstractFormatter
42
+ * make a result instance method that does not take any arguments
43
+ * use the local attribute 'input' to do whatever you want with the text, and return it
44
+
45
+ === Example custom formatter
46
+
47
+ In our example, let's create a formatter that will reverse the text.
48
+
49
+ class StupidFormatter::WrittenInReverse < StupidFormatter::AbstractFormatter
50
+ def result
51
+ input.reverse
52
+ end
53
+ end
54
+
55
+ After adding that to the chain with
56
+
57
+ StupidFormatter.chain << StupidFormatter::WrittenInReverse
58
+
59
+ Your text will be reversed when calling <code>"some string".formatted</code>
60
+
61
+ == ERb Helpers
62
+
63
+ === Capturing blocks with ERb
64
+
65
+ The ERb-formatter offers a capture-helper, which allows you to capture the content of a block into
66
+ a variable like so:
67
+
68
+ <% @foo = capture do %>
69
+ Bar
70
+ <% end %>
71
+ Foo<%= @foo %>
72
+
73
+ The above example will render into "FooBar". This functionality is used in the Coderay helper, which
74
+ is automatically included in the ERb formatter, see next section.
75
+
76
+ === Syntax highlighting with Coderay
77
+
78
+ With the CoderayHelper that gets included into the ERb formatter automatically, you can get syntax-highlighting
79
+ very easily:
80
+
81
+ <% highlight do %>
82
+ puts 'Your Awesome Ruby(tm) here!'
83
+ <% end %>
84
+
85
+ The language defaults to :ruby, but you can pass in any CodeRay-language to the highlight method:
86
+
87
+ <% highlight :javascript do %>
88
+ var foo = 'bar';
89
+ <% end %>
90
+
91
+ The highlighting is configured to use CSS instead of in-place colors, so you might want to add a
92
+ decent (http://railscasts.com/stylesheets/coderay.css) coderay css file into your HTML.
93
+
94
+ Thanks Ryan for making that great CodeRay CSS and giving it away (http://railscasts.com/episodes/46-catch-all-route)!
95
+
96
+ === Making your own helpers
97
+
98
+ Let's make a helper that reverses the text you gave in the block, like so:
99
+
100
+ <% written_in_reverse do %>
101
+ anna
102
+ <% end %>
103
+
104
+ Just create a module that uses the capture helper and appends the result to the @output_buffer and include it
105
+ in the ERb helper, like so:
106
+
107
+ module WrittenInReverse
108
+ def written_in_reverse
109
+ text = capture { yield }
110
+ @output_buffer << text.reverse
111
+ end
112
+ end
113
+ StupidFormatter::ERb.send :include, WrittenInReverse
114
+
115
+ This will print 'anna' in your ERb. Easy, right?
116
+
117
+ == Note on Patches/Pull Requests
118
+
119
+ * Fork the project.
120
+ * Make your feature addition or bug fix.
121
+ * Add tests for it. This is important so I don't break it in a
122
+ future version unintentionally.
123
+ * Commit, do not mess with rakefile, version, or history.
124
+ (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)
125
+ * Send me a pull request. Bonus points for topic branches.
126
+
127
+ == Copyright
128
+
129
+ Copyright (c) 2010 Christoph Olszowka. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "stupider_formatter"
8
+ gem.summary = %Q{A stupid formatter for piping text through markup processors}
9
+ gem.description = %Q{A stupid formatter for piping text through markup processors with a unified API}
10
+ gem.email = "hi at l33t dot name"
11
+ gem.homepage = "https://github.com/fliiiix/stupider_formatter"
12
+ gem.authors = ["Christoph Olszowka", "l33tname"]
13
+ gem.add_dependency "rdiscount", ">= 1.5.0"
14
+ gem.add_dependency "coderay", ">= 0.9.0"
15
+ gem.add_development_dependency "shoulda", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rdoc/task'
48
+ RDoc::Task.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "stupider_formatter #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,100 @@
1
+ require 'rdiscount'
2
+ require 'erb'
3
+ require 'coderay'
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
+
15
+ module StupidFormatter
16
+ class << self
17
+ #
18
+ # Returns the current processing chain. Defaults to [StupidFormatter::Erb, StupidFormatter::RDiscount]
19
+ #
20
+ def chain
21
+ @chain ||= [StupidFormatter::Erb, StupidFormatter::RDiscount]
22
+ end
23
+
24
+ #
25
+ # Make the formatter chain configurable. Pass in an Array of Formatters in the order
26
+ # you want them processed in.
27
+ #
28
+ def chain=(chain)
29
+ @chain = chain
30
+ end
31
+
32
+ #
33
+ # Will put the input string through all formatters in the chain
34
+ #
35
+ def result(input)
36
+ output = input.clone
37
+ chain.each do |formatter|
38
+ output = formatter.new(output).result
39
+ end
40
+ output
41
+ end
42
+ end
43
+
44
+ #
45
+ # Base class for formatters, providing the basic API.
46
+ #
47
+ class AbstractFormatter
48
+ attr_reader :input
49
+
50
+ def initialize(input)
51
+ @input = input.to_s.strip
52
+ raise "Please use this only in subclasses" if self.class == AbstractFormatter
53
+ end
54
+
55
+ def result
56
+ raise "This should be implemented by subclasses"
57
+ end
58
+ end
59
+
60
+ class Erb < AbstractFormatter
61
+ def result(alternative_binding=nil)
62
+ if RUBY_VERSION >= '2.6'
63
+ ERB.new(input, trim_mode: "%<>", eoutvar: "@output_buffer").result(alternative_binding || binding)
64
+ else
65
+ ERB.new(input, 0, "%<>", "@output_buffer").result(alternative_binding || binding)
66
+ end
67
+ end
68
+
69
+ # Helper for capturing output in a erb block for later use, i.e.
70
+ # <% @my_var = capture do %>
71
+ # Bar
72
+ # <% end %>
73
+ # Foo<%= @my_var %>
74
+ # will render FooBar.
75
+ #
76
+ def capture
77
+ old_buffer, @output_buffer = @output_buffer, ''
78
+ yield
79
+ @output_buffer
80
+ ensure
81
+ @output_buffer = old_buffer
82
+ end
83
+ end
84
+
85
+ class RDiscount < AbstractFormatter
86
+ def result
87
+ Markdown.new(input).to_html
88
+ end
89
+ end
90
+
91
+ module CoderayHelper
92
+ def highlight(language=:ruby)
93
+ code = capture { yield }
94
+ @output_buffer << CodeRay.scan(code, language).div(:css => :class)
95
+ end
96
+ end
97
+ end
98
+
99
+ # Add coderay helper by default
100
+ StupidFormatter::Erb.send :include, StupidFormatter::CoderayHelper
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: stupider_formatter 0.3.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "stupider_formatter".freeze
9
+ s.version = "0.3.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib".freeze]
13
+ s.authors = ["Christoph Olszowka".freeze, "l33tname".freeze]
14
+ s.date = "2021-11-20"
15
+ s.description = "A stupid formatter for piping text through markup processors with a unified API".freeze
16
+ s.email = "hi at l33t dot name".freeze
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".bundle/config",
23
+ ".document",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/stupid_formatter.rb",
29
+ "stupider_formatter.gemspec",
30
+ "test/fixtures/erb_coderay_markdown_example.txt",
31
+ "test/fixtures/erb_coderay_markdown_expectation.txt",
32
+ "test/helper.rb",
33
+ "test/test_erb_formatter.rb",
34
+ "test/test_rdiscount_formatter.rb",
35
+ "test/test_stupid_formatter.rb"
36
+ ]
37
+ s.homepage = "https://github.com/fliiiix/stupider_formatter".freeze
38
+ s.rubygems_version = "3.2.3".freeze
39
+ s.summary = "A stupid formatter for piping text through markup processors".freeze
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 4
43
+ end
44
+
45
+ if s.respond_to? :add_runtime_dependency then
46
+ s.add_runtime_dependency(%q<rdiscount>.freeze, [">= 1.5.0"])
47
+ s.add_runtime_dependency(%q<coderay>.freeze, [">= 0.9.0"])
48
+ s.add_development_dependency(%q<shoulda>.freeze, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<rdiscount>.freeze, [">= 1.5.0"])
51
+ s.add_dependency(%q<coderay>.freeze, [">= 0.9.0"])
52
+ s.add_dependency(%q<shoulda>.freeze, [">= 0"])
53
+ end
54
+ end
55
+
@@ -0,0 +1,11 @@
1
+ # A heading
2
+
3
+ <p>A basic paragraph</p>
4
+
5
+ <% if 1 == 2 %>
6
+ Some plain ERB conditional
7
+ <% end %>
8
+
9
+ <% highlight do %>
10
+ puts "foo bar baz"
11
+ <% end %>
@@ -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="string"><span class="delimiter">&quot;</span><span class="content">foo bar baz</span><span class="delimiter">&quot;</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 instance" do
26
+ setup do
27
+ @result = StupidFormatter::Erb.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,40 @@
1
+ require 'helper'
2
+
3
+ class TestStupidFormatter < Test::Unit::TestCase
4
+ context "After configuring the StupidFormatter.chain to [StupidFormatter::Erb, StupidFormatter::RDiscount]" do
5
+ setup do
6
+ StupidFormatter.chain = [StupidFormatter::Erb, StupidFormatter::RDiscount]
7
+ end
8
+
9
+ should "return StupidFormatter::Erb for the first chain item" do
10
+ assert_equal StupidFormatter::Erb, 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
+
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
38
+ end
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stupider_formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Christoph Olszowka
8
+ - l33tname
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdiscount
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 1.5.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 1.5.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: coderay
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.9.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 0.9.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: shoulda
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: A stupid formatter for piping text through markup processors with a unified
57
+ API
58
+ email: hi at l33t dot name
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - ".bundle/config"
66
+ - ".document"
67
+ - LICENSE
68
+ - README.rdoc
69
+ - Rakefile
70
+ - VERSION
71
+ - lib/stupid_formatter.rb
72
+ - stupider_formatter.gemspec
73
+ - test/fixtures/erb_coderay_markdown_example.txt
74
+ - test/fixtures/erb_coderay_markdown_expectation.txt
75
+ - test/helper.rb
76
+ - test/test_erb_formatter.rb
77
+ - test/test_rdiscount_formatter.rb
78
+ - test/test_stupid_formatter.rb
79
+ homepage: https://github.com/fliiiix/stupider_formatter
80
+ licenses: []
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.2.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A stupid formatter for piping text through markup processors
101
+ test_files: []