No commit activity in last 3 years
No release in over 3 years
A stupid formatter for piping text through markup processors with a unified API
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 0.9.0
>= 1.5.0
 Project Readme

StupiderFormatter¶ ↑

This is a fork from github.com/colszowka/stupid_formatter

StupidFormatter¶ ↑

StupidFormatter let’s you pipe text through processors in a chain by abstracting the actual implementation details of each processor into a unified API. By default, it can do ERb, Markdown (via RDiscount) and syntax-highlight with CodeRay.

Say you want to format some markdown-formatted text with RDiscount and also include some ERb processing with CodeRay syntax highlighting, you could configure the chain somewhere at the beginning of your app like so:

StupidFormatter.chain = [StupidFormatter::Erb, StupidFormatter::RDiscount]

Then, pass in your Markdown-marked-up, ERb-riddled string to StupidFormatter.result or just call string.formatted and you’ll see results.

The ERb, RDiscount chain mentioned above is actually the default, so if you want to stick with that, just skip that chain config step (yes, it is a stupid example).

Formatters are processed in the order they are given in the Array, so ERb comes first, then RDiscount in this example.

To install, do:

sudo gem install stupid_formatter

This is so completely alpha, you have no idea! Don’t expect beautiful code or RDoc.

Using the formatters directly¶ ↑

Of course, you can access the formatters directly as well. To format a string with RDiscount, do:

StupidFormatter::RDiscount.new("# some markdown").result

Custom formatters¶ ↑

You can define your own formatters very easily by conforming to the public API, which is:

  • inherit from StupidFormatter::AbstractFormatter

  • make a result instance method that does not take any arguments

  • use the local attribute ‘input’ to do whatever you want with the text, and return it

Example custom formatter¶ ↑

In our example, let’s create a formatter that will reverse the text.

class StupidFormatter::WrittenInReverse < StupidFormatter::AbstractFormatter
  def result
    input.reverse
  end
end

After adding that to the chain with

StupidFormatter.chain << StupidFormatter::WrittenInReverse

Your text will be reversed when calling "some string".formatted

ERb Helpers¶ ↑

Capturing blocks with ERb¶ ↑

The ERb-formatter offers a capture-helper, which allows you to capture the content of a block into a variable like so:

<% @foo = capture do %>
  Bar
<% end %>
Foo<%= @foo %>

The above example will render into “FooBar”. This functionality is used in the Coderay helper, which is automatically included in the ERb formatter, see next section.

Syntax highlighting with Coderay¶ ↑

With the CoderayHelper that gets included into the ERb formatter automatically, you can get syntax-highlighting very easily:

<% highlight do %>
  puts 'Your Awesome Ruby(tm) here!'
<% end %>

The language defaults to :ruby, but you can pass in any CodeRay-language to the highlight method:

<% highlight :javascript do %>
  var foo = 'bar';
<% end %>

The highlighting is configured to use CSS instead of in-place colors, so you might want to add a decent (railscasts.com/stylesheets/coderay.css) coderay css file into your HTML.

Thanks Ryan for making that great CodeRay CSS and giving it away (railscasts.com/episodes/46-catch-all-route)!

Making your own helpers¶ ↑

Let’s make a helper that reverses the text you gave in the block, like so:

<% written_in_reverse do %>
  anna
<% end %>

Just create a module that uses the capture helper and appends the result to the @output_buffer and include it in the ERb helper, like so:

module WrittenInReverse
  def written_in_reverse
    text = capture { yield }
    @output_buffer << text.reverse
  end
end
StupidFormatter::ERb.send :include, WrittenInReverse

This will print ‘anna’ in your ERb. Easy, right?

Note on Patches/Pull Requests¶ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (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)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Christoph Olszowka. See LICENSE for details.