Skip to content
Permalink
Browse files
Allow you to inject your own parser.
Why bother just letting you do markdown? And why tie you to
RedCarpet?
  • Loading branch information
steveklabnik committed Jan 26, 2012
1 parent 56aa762 commit 84f4bb751cf7fd3284d1e0eeb8871da4fda53fe7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
@@ -23,6 +23,9 @@ at the head of your file:
Woudn't that be neat to use on other projects? I thought so too! Hence,
metadown.

Furthermore, you don't have to have just markdown. Inject any kind of
parser you'd like!

## Installation

Add this line to your application's Gemfile:
@@ -60,6 +63,17 @@ 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 #=> "{}"

## Contributing

1. Fork it
@@ -1,4 +1,5 @@
require "metadown/renderer"
require "metadown/metadata_parser"
require "metadown/version"

# This module namespaces everything in the gem. It's also where the factory
@@ -11,13 +12,26 @@ module Metadown
# output.
Data = Struct.new(:metadata, :output)

# The render method is a convenient factory. It parses some text via our
# Renderer class and Redcarpet's own class, and gives us a Data back.
def render(text)
renderer = Metadown::Renderer.new
# The render method is a convenient factory. If we give it text, it
# delegates to the classic markdown renderer, otherwise, we can inject
# one of our own.
def render(text, renderer=nil)
return redcarpet_render(text) if renderer.nil?

metadata = MetadataParser.new(text).parse

Data.new(metadata, renderer.new { text }.render)
end
module_function :render

# Classic compatiblity mode: fall back to old redcarpet renderer
def redcarpet_render(text)
renderer = Renderer.new
markdown = Redcarpet::Markdown.new(renderer)

output = markdown.render(text)

Data.new(renderer.metadata, output)
end
module_function :render
module_function :redcarpet_render
end
@@ -0,0 +1,13 @@
module Metadown
class MetadataParser
def initialize(text)
@text = text
@metadata = {}
end

def parse
@text =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
@mtext = YAML.load($1) if $1
end
end
end
@@ -1,4 +1,4 @@
module Metadown
# The current released version of Metadown
VERSION = "1.0.1"
VERSION = "1.1.0.beta"
end
@@ -2,18 +2,30 @@
require 'metadown'

describe Metadown do
it "provides a factory" do
let(:text) do
text = <<-MARKDOWN
---
key: "value"
---
hello world
MARKDOWN
end

it "provides a factory" do
Metadown.render(text).tap do |data|
data.should be_kind_of(Metadown::Data)
data.metadata.should eql({"key" => "value"})
data.output.should eql("<p>hello world</p>\n")
end
end

it "allows you to inject a parser" do
parser = stub(:new => stub(:render => "wat"))

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")
end
end
end

0 comments on commit 84f4bb7

Please sign in to comment.