Permalink
Browse files

Merge pull request #2 from miloshadzic/master

Make it easier to inject your own Markdown renderer.
  • Loading branch information...
2 parents 84f4bb7 + 4b62eb3 commit 67622c062f2bdf5b08f2bbc3b7529b62cbffe7d1 @steveklabnik committed Oct 1, 2012
Showing with 30 additions and 14 deletions.
  1. +26 −10 README.md
  2. +2 −2 lib/metadown.rb
  3. +2 −2 spec/metadown_spec.rb
View
@@ -63,16 +63,32 @@ back with two attributes: output and metadata.
data.output #=> "<p>hello, world</p>\n"
data.metadata #=> {"key" => "value"}
-If you don't want to use Markdown, I assume you're using a Tilt
-template of some kind:
-
- require 'metadown'
- require 'erb'
- require 'tilt'
-
- data = Metadown.render("<h1><%= 'Hi' %></h1>", Tilt::ERBTemplate)
- data.output #=> "<h1>Hi</h1>"
- data.metadata #=> "{}"
+The default Markdown parser Metadown uses is pretty simple. If you'd
+like to change it, you can inject your own in the standard Redcarpet
+fashion. Here's an example with code highlighting using Pygments.
+
+```ruby
+class HTMLwithPygments < Metadown::Renderer
+ def block_code(code, language)
+ Pygments.highlight(code, :lexer => language)
+ end
+end
+```
+
+Then use it with Metadown like this:
+
+```ruby
+require 'metadown'
+require 'html_with_pygments'
+
+renderer = Redcarpet::Markdown.new(HTMLwithPygments, :fenced_code_blocks => true)
+data = Metadown.render("```ruby\nself\n```", renderer)
+data.output #=> "<div class=\"highlight\"><pre><span class=\"nb\">self</span>\n</pre>\n</div>\n"
+data.metadata #=> "{}"
+```
+
+The Redcarpet [README](https://github.com/vmg/redcarpet/blob/master/README.markdown)
+has more examples on how to customize your Markdown rendering.
## Contributing
View
@@ -7,7 +7,7 @@
module Metadown
# This struct is what gets returned from a call to #render.
- #
+ #
# It has two attributes, one for the metadata, and one for the rendered
# output.
Data = Struct.new(:metadata, :output)
@@ -20,7 +20,7 @@ def render(text, renderer=nil)
metadata = MetadataParser.new(text).parse
- Data.new(metadata, renderer.new { text }.render)
+ Data.new(metadata, renderer.render(text))
end
module_function :render
@@ -20,12 +20,12 @@
end
it "allows you to inject a parser" do
- parser = stub(:new => stub(:render => "wat"))
+ parser = double :render => "<p>hello world</p>"
Metadown.render(text, parser).tap do |data|
data.should be_kind_of(Metadown::Data)
data.metadata.should eql({"key" => "value"})
- data.output.should eql("wat")
+ data.output.should eql("<p>hello world</p>")
end
end
end

0 comments on commit 67622c0

Please sign in to comment.