markdownizer 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +46 -0
- data/Rakefile +10 -0
- data/Readme.md +59 -0
- data/lib/markdownizer/version.rb +3 -0
- data/lib/markdownizer.rb +35 -0
- data/markdownizer.gemspec +27 -0
- data/spec/markdownizer_spec.rb +92 -0
- data/spec/spec_helper.rb +2 -0
- metadata +134 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use ruby-1.9.2@markdownizer
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
markdownizer (0.0.1)
|
5
|
+
activerecord (~> 3.0.3)
|
6
|
+
coderay
|
7
|
+
rdiscount
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activemodel (3.0.3)
|
13
|
+
activesupport (= 3.0.3)
|
14
|
+
builder (~> 2.1.2)
|
15
|
+
i18n (~> 0.4)
|
16
|
+
activerecord (3.0.3)
|
17
|
+
activemodel (= 3.0.3)
|
18
|
+
activesupport (= 3.0.3)
|
19
|
+
arel (~> 2.0.2)
|
20
|
+
tzinfo (~> 0.3.23)
|
21
|
+
activesupport (3.0.3)
|
22
|
+
arel (2.0.7)
|
23
|
+
builder (2.1.2)
|
24
|
+
coderay (0.9.7)
|
25
|
+
diff-lcs (1.1.2)
|
26
|
+
i18n (0.5.0)
|
27
|
+
rdiscount (1.6.8)
|
28
|
+
rspec (2.4.0)
|
29
|
+
rspec-core (~> 2.4.0)
|
30
|
+
rspec-expectations (~> 2.4.0)
|
31
|
+
rspec-mocks (~> 2.4.0)
|
32
|
+
rspec-core (2.4.0)
|
33
|
+
rspec-expectations (2.4.0)
|
34
|
+
diff-lcs (~> 1.1.2)
|
35
|
+
rspec-mocks (2.4.0)
|
36
|
+
tzinfo (0.3.24)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
activerecord (~> 3.0.3)
|
43
|
+
coderay
|
44
|
+
markdownizer!
|
45
|
+
rdiscount
|
46
|
+
rspec (~> 2.4.0)
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
#Markdownizer
|
2
|
+
|
3
|
+
A simple gem for Rails 3 to render some ActiveRecord text field as Markdown!
|
4
|
+
|
5
|
+
It mixes CodeRay and RDiscount to give you awesome code highlighting :)
|
6
|
+
|
7
|
+
##Install
|
8
|
+
|
9
|
+
In your Gemfile:
|
10
|
+
|
11
|
+
gem 'markdownizer'
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
In your model, let's say, Post:
|
16
|
+
|
17
|
+
class Post < ActiveRecord::Base
|
18
|
+
markdownize! :body # In this case we want to treat :body as markdown
|
19
|
+
end
|
20
|
+
|
21
|
+
Markdownizer needs an additional field (`:rendered_body`), which you should
|
22
|
+
generate in a migration. (If the attribute was `:some_other_field`, it would need
|
23
|
+
`:rendered_some_other_field`!) All these fields should have the type `:text`.
|
24
|
+
|
25
|
+
You save your posts with markdown text like this:
|
26
|
+
|
27
|
+
Post.create body: """
|
28
|
+
# My H1 title
|
29
|
+
Markdown is awesome!
|
30
|
+
## Some H2 title...
|
31
|
+
|
32
|
+
{% highlight ruby %}
|
33
|
+
|
34
|
+
# All this code will be highlighted properly! :)
|
35
|
+
def my_method(*my_args)
|
36
|
+
something do
|
37
|
+
. . .
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
{% endhighlight %}
|
42
|
+
"""
|
43
|
+
|
44
|
+
And then, in your view you just have to call `@post.rendered_body` :)
|
45
|
+
|
46
|
+
##Contribute!
|
47
|
+
|
48
|
+
* Fork the project.
|
49
|
+
* Make your feature addition or bug fix.
|
50
|
+
* Add specs for it. This is important so I don't break it in a future
|
51
|
+
version unintentionally.
|
52
|
+
* Commit, do not mess with rakefile, version, or history.
|
53
|
+
If you want to have your own version, that is fine but bump version
|
54
|
+
in a commit by itself I can ignore when I pull.
|
55
|
+
* Send me a pull request. Bonus points for topic branches.
|
56
|
+
|
57
|
+
## Copyright
|
58
|
+
|
59
|
+
Copyright (c) 2011 Codegram. See LICENSE for details.
|
data/lib/markdownizer.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rdiscount'
|
2
|
+
require 'coderay'
|
3
|
+
require 'active_record' unless defined?(ActiveRecord)
|
4
|
+
|
5
|
+
module Markdownizer
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def markdown(text)
|
9
|
+
RDiscount.new(text).to_html
|
10
|
+
end
|
11
|
+
|
12
|
+
def coderay(text)
|
13
|
+
text.gsub(%r[\{% highlight (\w+?) %\}(.+?)\{% endhighlight %\}]m) do
|
14
|
+
CodeRay.scan($2, $1).div(:css => :class)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module DSL
|
20
|
+
def markdownize! attribute
|
21
|
+
unless self.column_names.include?(attribute.to_s) &&
|
22
|
+
self.column_names.include?("rendered_#{attribute}")
|
23
|
+
raise "#{self.name} doesn't have required attributes :#{attribute} and :rendered_#{attribute}\nPlease generate a migration to add these attributes -- both should have type :text."
|
24
|
+
end
|
25
|
+
self.before_save :"render_#{attribute}"
|
26
|
+
|
27
|
+
define_method :"render_#{attribute}" do
|
28
|
+
self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute))))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
ActiveRecord::Base.send(:extend, Markdownizer::DSL)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "markdownizer/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "markdownizer"
|
7
|
+
s.version = Markdownizer::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Josep M. Bach", "Josep Jaume Rey", "Oriol Gual"]
|
10
|
+
s.email = ["info@codegram.com"]
|
11
|
+
s.homepage = "http://github.com/codegram/markdownizer"
|
12
|
+
s.summary = %q{Render any text as markdown, with code highlighting and all!}
|
13
|
+
s.description = %q{Render any text as markdown, with code highlighting and all!}
|
14
|
+
|
15
|
+
s.rubyforge_project = "markdownizer"
|
16
|
+
|
17
|
+
s.add_runtime_dependency 'activerecord', '~> 3.0.3'
|
18
|
+
s.add_runtime_dependency 'rdiscount'
|
19
|
+
s.add_runtime_dependency 'coderay'
|
20
|
+
|
21
|
+
s.add_development_dependency 'rspec', '~> 2.4.0'
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ["lib"]
|
27
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Markdownizer do
|
4
|
+
describe ".markdown(text)" do
|
5
|
+
let(:text) { "#My markdown text"}
|
6
|
+
it 'calls RDiscount to markdownize the text' do
|
7
|
+
rdiscount, html_markdown = double(:rdiscount), double(:html_markdown)
|
8
|
+
|
9
|
+
RDiscount.should_receive(:new).with(text).and_return rdiscount
|
10
|
+
rdiscount.should_receive(:to_html).and_return html_markdown
|
11
|
+
|
12
|
+
subject.markdown(text).should == html_markdown
|
13
|
+
end
|
14
|
+
end
|
15
|
+
describe ".coderay(text)" do
|
16
|
+
let(:text) { """
|
17
|
+
#My markdown text
|
18
|
+
|
19
|
+
{% highlight ruby %}
|
20
|
+
def function(*args)
|
21
|
+
puts 'result'
|
22
|
+
end
|
23
|
+
{% endhighlight %}
|
24
|
+
|
25
|
+
"""
|
26
|
+
}
|
27
|
+
it 'calls CodeRay to parse the code inside {% highlight ruby %} blocks' do
|
28
|
+
scanned_code, html_code = double(:scanned_code), double(:html_code)
|
29
|
+
|
30
|
+
CodeRay.should_receive(:scan).with("""
|
31
|
+
def function(*args)
|
32
|
+
puts 'result'
|
33
|
+
end
|
34
|
+
""", 'ruby').and_return scanned_code
|
35
|
+
|
36
|
+
scanned_code.should_receive(:div).with(:css => :class).and_return 'parsed code'
|
37
|
+
|
38
|
+
subject.coderay(text).should match('parsed code')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe Markdownizer::DSL do
|
43
|
+
it 'integrates with ActiveRecord::Base' do
|
44
|
+
(class << ActiveRecord::Base; self; end).ancestors.should include(Markdownizer::DSL)
|
45
|
+
end
|
46
|
+
|
47
|
+
before do
|
48
|
+
ActiveRecord::Base.stub(:send)
|
49
|
+
@klass = Class.new(ActiveRecord::Base)
|
50
|
+
@klass.stub(:column_names) { %{body rendered_body} }
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#markdownize!(attribute)" do
|
54
|
+
context "when either of attribute or rendered_attribute does not exist" do
|
55
|
+
it 'raises' do
|
56
|
+
expect {
|
57
|
+
@klass.markdownize! :some_attribute
|
58
|
+
}.to raise_error
|
59
|
+
end
|
60
|
+
end
|
61
|
+
context "otherwise" do
|
62
|
+
it 'creates a before_save callback for render_attribute' do
|
63
|
+
@klass.should_receive(:before_save).with(:render_body)
|
64
|
+
@klass.markdownize! :body
|
65
|
+
end
|
66
|
+
it 'defines this render_attribute method' do
|
67
|
+
klass = Class.new do
|
68
|
+
extend Markdownizer::DSL
|
69
|
+
def self.column_names
|
70
|
+
%{body rendered_body}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
klass.stub(:before_save)
|
75
|
+
klass.markdownize! :body
|
76
|
+
|
77
|
+
raw_body, raw_body_with_code, final_code = double(:raw_body),
|
78
|
+
double(:raw_body_with_code),
|
79
|
+
double(:final_code)
|
80
|
+
|
81
|
+
instance = klass.new
|
82
|
+
instance.should_receive(:send).with(:body).and_return raw_body
|
83
|
+
Markdownizer.should_receive(:coderay).with(raw_body).and_return raw_body_with_code
|
84
|
+
Markdownizer.should_receive(:markdown).with(raw_body_with_code).and_return final_code
|
85
|
+
|
86
|
+
instance.should_receive(:send).with(:rendered_body=, final_code)
|
87
|
+
instance.render_body
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markdownizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Josep M. Bach
|
13
|
+
- Josep Jaume Rey
|
14
|
+
- Oriol Gual
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-02-02 00:00:00 +01:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: activerecord
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 3
|
34
|
+
version: 3.0.3
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rdiscount
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: coderay
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :runtime
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ~>
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 2
|
73
|
+
- 4
|
74
|
+
- 0
|
75
|
+
version: 2.4.0
|
76
|
+
type: :development
|
77
|
+
version_requirements: *id004
|
78
|
+
description: Render any text as markdown, with code highlighting and all!
|
79
|
+
email:
|
80
|
+
- info@codegram.com
|
81
|
+
executables: []
|
82
|
+
|
83
|
+
extensions: []
|
84
|
+
|
85
|
+
extra_rdoc_files: []
|
86
|
+
|
87
|
+
files:
|
88
|
+
- .gitignore
|
89
|
+
- .rspec
|
90
|
+
- .rvmrc
|
91
|
+
- Gemfile
|
92
|
+
- Gemfile.lock
|
93
|
+
- Rakefile
|
94
|
+
- Readme.md
|
95
|
+
- lib/markdownizer.rb
|
96
|
+
- lib/markdownizer/version.rb
|
97
|
+
- markdownizer.gemspec
|
98
|
+
- spec/markdownizer_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
has_rdoc: true
|
101
|
+
homepage: http://github.com/codegram/markdownizer
|
102
|
+
licenses: []
|
103
|
+
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
version: "0"
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
segments:
|
123
|
+
- 0
|
124
|
+
version: "0"
|
125
|
+
requirements: []
|
126
|
+
|
127
|
+
rubyforge_project: markdownizer
|
128
|
+
rubygems_version: 1.3.7
|
129
|
+
signing_key:
|
130
|
+
specification_version: 3
|
131
|
+
summary: Render any text as markdown, with code highlighting and all!
|
132
|
+
test_files:
|
133
|
+
- spec/markdownizer_spec.rb
|
134
|
+
- spec/spec_helper.rb
|