public
Description: Syntax highlighting and textile formatting for your ActiveRecord models
Homepage: http://aktagon.com/projects/rails/has_markup/
Clone URL: git://github.com/christianhellsten/has_markup.git
name age message
file .gitignore Loading commit data...
file README.textile
file init.rb
directory lib/
file redclother.rb
directory test/
README.textile

HasMarkup – Syntax Highlighting and Textile Formatting for Your AR Models

HasMarkup is a Ruby on Rails Plugin that adds syntax highlighting and textile formatting capabilities to your ActiveRecord models.

Features:

  1. Supports all popular languages and several themes thanks to the Ultraviolet library.
  2. Data is processed once and then cached in the database, which means it’s as fast as it should be.
  3. Configurable which means it’s probably flexible enough to fit your database schema (if not tell me).

Installation

	
script/plugin install git://github.com/christianhellsten/has_markup.git
	

Usage

The schema:

	
ActiveRecord::Schema.define :version => 0 do

  create_table :snippets, :force => true do |t|
    t.string  :title
    t.text    :body
    t.text    :rendered_body
  end

end
	

The options:

HasMarkup can be configured to use other columns than the default body and rendered_body columns. Here’s a list of all available options:

	
has_markup  :language => lambda { |snippet| snippet.language.syntax }, # Either the name of the column where the language is stored or a Proc
	:input        => :body, 		# Name of column where content is stored 
	:output       => :rendered_body,	# Name of column where rendered content should be stored
	# Ultraviolet configuration
	:theme        => 'active4d',		# Default Ultraviolet theme
	:language     => :language,		# Default Ultraviolet syntax, can be a Proc
	:line_numbers => false,			# Display line numbers, or not.
	# RedCloth configuration
	:redcloth      => [:filter_html, :filter_styles, :filter_ids, :filter_classes] 	# Textile options, see http://redcloth.rubyforge.org/
	

The model:


class Snippet < ActiveRecord::Base has_markup validates_presence_of :body end

The controller:

	
body = <<BODY
<code language="html">
<html>
<body>
  <span>hello</span>
</body>
</html>
</code>
BODY

@snippet = Snippet.create(:title => 'A Ruby snippet', :body => body)

	

The view:

	
<%= @snippet.rendered_body %>
	

Questions?

Advanced Usage

See:

  • has_markup_test.rb (unit/integration tests)
  • snippet.rb and language.rb (model examples)

Credits

Author

Christian Hellsten (Aktagon Ltd.)