# **Markdownizer** is a simple solution to a simple problem. # # It is a lightweight Rails 3 gem which enhances any ActiveRecord model with a # singleton `markdownize!` method, which makes any text attribute renderable as # Markdown. It mixes CodeRay and RDiscount to give you awesome code # highlighting, too! # # If you have any suggestion regarding new features, Check out the [Github repo][gh], # fork it and submit a nice pull request :) # #### Install Markdownizer # Get Markdownizer in your Rails 3 app through your Gemfile: # # gem 'markdownizer' # # If you don't use Bundler, you can alternatively install Markdownizer with # Rubygems: # # gem install markdownizer # # If you want code highlighting, you should run this generator too: # # rails generate markdownizer:install # # This will place a `markdownizer.css` file in your `public/stylesheets` # folder. You will have to require it manually in your layouts, or through # `jammit`, or whatever. # # [gh]: http://github.com/codegram/markdownizer #### Usage # In your model, let's say, `Post`: # # class Post < ActiveRecord::Base # # In this case we want to treat :body as markdown # # This will require `body` and `rendered_body` fields # # to exist previously (otherwise it will raise an error). # markdownize! :body # end # # Then you can create a `Post` using Markdown syntax, like this: # # Post.create body: """ # # My H1 title # Markdown is awesome! # ## Some H2 title... # # {% code ruby %} # # # All this code will be highlighted properly! :) # def my_method(*my_args) # something do # 3 + 4 # end # end # # {% endcode %} # """ # # After that, in your view you just have to call `@post.rendered_body` and, # provided you included the generated css in your layout, it will display nice # and coloured :) #### Show me the code! # We'll need to include [RDiscount][rd]. This is the Markdown parsing library. # Also, [CodeRay][cr] will provide code highlighting. We require `active_record` as # well, since we want to extend it with our DSL. # # [rd]: http://github.com/rtomayko/rdiscount # [cr]: http://github.com/rubychan/coderay require 'rdiscount' require 'coderay' require 'active_record' unless defined?(ActiveRecord) module Markdownizer class << self # Here we define two helper methods. These will be called from the model to # perform the corresponding conversions and parsings. # `Markdownizer.markdown` method converts plain Markdown text to formatted html. # To parse the markdown in a coherent hierarchical context, you must provide it # with the current hierarchical level of the text to be parsed. def markdown(text, hierarchy = 0) text.gsub! %r[^(\s*)(#+)(.+)$] do $1 << ('#' * hierarchy) << $2 << $3 end text.gsub!("\\#",'#') RDiscount.new(text).to_html end # `Markdownizer.coderay` method parses a code block delimited from `{% code # ruby %}` until `{% endcode %}` and replaces it with appropriate classes for # code highlighting. It can take many languages aside from Ruby. # # With a hash of options you can specify `:line_numbers` (`:table` or `:inline`), # and the class of the enclosing div with `:enclosing_class`. # # It also parses a couple of special idioms: # # * {% caption 'my caption' %} introduces an h5 before the code and passes # the caption to the enclosing div as well. # # * {% highlight [1,2,3] %} highlights lines 1, 2 and 3. It accepts any # Enumerable, so you can also give a Range (1..3). # def coderay(text, options = {}) text.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do options.delete(:highlight_lines) options.delete(:caption) enclosing_class = options[:enclosing_class] || 'markdownizer_code' code, language = $2.strip, $1.strip code, options, caption = extract_caption_from(code, options) code, options = extract_highlights_from(code, options) html_caption = caption ? '