markdownizer 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile.lock +13 -1
- data/Rakefile +54 -0
- data/Readme.md +3 -0
- data/lib/markdownizer.rb +100 -1
- data/lib/markdownizer/version.rb +1 -1
- data/markdownizer.gemspec +3 -0
- metadata +44 -5
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
markdownizer (0.1.
|
4
|
+
markdownizer (0.1.2)
|
5
5
|
activerecord (~> 3.0.3)
|
6
6
|
coderay
|
7
7
|
rdiscount
|
@@ -23,8 +23,14 @@ GEM
|
|
23
23
|
builder (2.1.2)
|
24
24
|
coderay (0.9.7)
|
25
25
|
diff-lcs (1.1.2)
|
26
|
+
git (1.2.5)
|
26
27
|
i18n (0.5.0)
|
28
|
+
mustache (0.12.0)
|
29
|
+
pygments (0.0.1.a)
|
27
30
|
rdiscount (1.6.8)
|
31
|
+
rocco (0.5)
|
32
|
+
mustache
|
33
|
+
rdiscount
|
28
34
|
rspec (2.4.0)
|
29
35
|
rspec-core (~> 2.4.0)
|
30
36
|
rspec-expectations (~> 2.4.0)
|
@@ -39,5 +45,11 @@ PLATFORMS
|
|
39
45
|
ruby
|
40
46
|
|
41
47
|
DEPENDENCIES
|
48
|
+
activerecord (~> 3.0.3)
|
49
|
+
coderay
|
50
|
+
git
|
42
51
|
markdownizer!
|
52
|
+
pygments
|
53
|
+
rdiscount
|
54
|
+
rocco
|
43
55
|
rspec (~> 2.4.0)
|
data/Rakefile
CHANGED
@@ -1,6 +1,60 @@
|
|
1
1
|
require 'bundler'
|
2
2
|
Bundler::GemHelper.install_tasks
|
3
3
|
|
4
|
+
# Bring in Rocco tasks
|
5
|
+
require 'rocco/tasks'
|
6
|
+
require 'rake/clean'
|
7
|
+
Rocco::make 'docs/', 'lib/markdownizer.rb'
|
8
|
+
|
9
|
+
desc 'Build markdownizer docs'
|
10
|
+
task :docs => :rocco
|
11
|
+
directory 'docs/'
|
12
|
+
|
13
|
+
desc 'Build docs and open in browser for the reading'
|
14
|
+
task :read => :docs do
|
15
|
+
sh 'open docs/lib/rocco.html'
|
16
|
+
end
|
17
|
+
|
18
|
+
# Make index.html a copy of markdownizer.html
|
19
|
+
file 'docs/index.html' => 'docs/lib/markdownizer.html' do |f|
|
20
|
+
cp 'docs/lib/markdownizer.html', 'docs/index.html', :preserve => true
|
21
|
+
end
|
22
|
+
task :docs => 'docs/index.html'
|
23
|
+
CLEAN.include 'docs/index.html'
|
24
|
+
|
25
|
+
# Alias for docs task
|
26
|
+
task :doc => :docs
|
27
|
+
|
28
|
+
# GITHUB PAGES ===============================================================
|
29
|
+
|
30
|
+
site = 'docs'
|
31
|
+
source_branch = 'master'
|
32
|
+
deploy_branch = 'gh-pages'
|
33
|
+
|
34
|
+
desc "generate and deploy website to github user pages"
|
35
|
+
multitask :pages do
|
36
|
+
puts ">>> Deploying #{deploy_branch} branch to Github Pages <<<"
|
37
|
+
require 'git'
|
38
|
+
repo = Git.open('.')
|
39
|
+
puts "\n>>> Checking out #{deploy_branch} branch <<<\n"
|
40
|
+
repo.branch("#{deploy_branch}").checkout
|
41
|
+
(Dir["*"] - [site]).each { |f| rm_rf(f) }
|
42
|
+
Dir["#{site}/*"].each {|f| mv(f, "index.html")}
|
43
|
+
rm_rf(site)
|
44
|
+
puts "\n>>> Moving generated site files <<<\n"
|
45
|
+
Dir["**/*"].each {|f| repo.add(f) }
|
46
|
+
repo.status.deleted.each {|f, s| repo.remove(f)}
|
47
|
+
puts "\n>>> Commiting: Site updated at #{Time.now.utc} <<<\n"
|
48
|
+
message = ENV["MESSAGE"] || "Site updated at #{Time.now.utc}"
|
49
|
+
repo.commit(message)
|
50
|
+
puts "\n>>> Pushing generated site to #{deploy_branch} branch <<<\n"
|
51
|
+
repo.push
|
52
|
+
puts "\n>>> Github Pages deploy complete <<<\n"
|
53
|
+
repo.branch("#{source_branch}").checkout
|
54
|
+
end
|
55
|
+
|
56
|
+
# TESTS =====================================================================
|
57
|
+
|
4
58
|
require 'rspec/core'
|
5
59
|
require 'rspec/core/rake_task'
|
6
60
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
data/Readme.md
CHANGED
@@ -4,6 +4,9 @@ A simple gem for Rails 3 to render some ActiveRecord text field as Markdown!
|
|
4
4
|
|
5
5
|
It mixes CodeRay and RDiscount to give you awesome code highlighting :)
|
6
6
|
|
7
|
+
You can check the generated Rocco documentation on the [project
|
8
|
+
page](http://codegram.github.com/markdownizer).
|
9
|
+
|
7
10
|
##Install
|
8
11
|
|
9
12
|
In your Gemfile:
|
data/lib/markdownizer.rb
CHANGED
@@ -1,3 +1,77 @@
|
|
1
|
+
# **Markdownizer** is a simple solution to a simple problem.
|
2
|
+
#
|
3
|
+
# It is a lightweight Rails 3 gem which enhances any ActiveRecord model with a
|
4
|
+
# singleton `markdownize!` method, which makes any text attribute renderable as
|
5
|
+
# Markdown. It mixes CodeRay and RDiscount to give you awesome code
|
6
|
+
# highlighting, too!
|
7
|
+
#
|
8
|
+
# If you have any suggestion regarding new features, Check out the [Github repo][gh],
|
9
|
+
# fork it and submit a nice pull request :)
|
10
|
+
#
|
11
|
+
|
12
|
+
#### Install Markdownizer
|
13
|
+
|
14
|
+
# Get Markdownizer in your Rails 3 app through your Gemfile:
|
15
|
+
#
|
16
|
+
# gem 'markdownizer'
|
17
|
+
#
|
18
|
+
# If you don't use Bundler, you can alternatively install Markdownizer with
|
19
|
+
# Rubygems:
|
20
|
+
#
|
21
|
+
# gem install markdownizer
|
22
|
+
#
|
23
|
+
# If you want code highlighting, you should run this generator too:
|
24
|
+
#
|
25
|
+
# rails generate markdownizer:install
|
26
|
+
#
|
27
|
+
# This will place a `markdownizer.css` file in your `public/stylesheets`
|
28
|
+
# folder. You will have to require it manually in your layouts, or through
|
29
|
+
# `jammit`, or whatever.
|
30
|
+
#
|
31
|
+
# [gh]: http://github.com/codegram/markdownizer
|
32
|
+
|
33
|
+
#### Usage
|
34
|
+
|
35
|
+
# In your model, let's say, `Post`:
|
36
|
+
#
|
37
|
+
# class Post < ActiveRecord::Base
|
38
|
+
# # In this case we want to treat :body as markdown
|
39
|
+
# # This will require `body` and `rendered_body` fields
|
40
|
+
# # to exist previously (otherwise it will raise an error).
|
41
|
+
# markdownize! :body
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# Then you can create a `Post` using Markdown syntax, like this:
|
45
|
+
#
|
46
|
+
# Post.create body: """
|
47
|
+
# # My H1 title
|
48
|
+
# Markdown is awesome!
|
49
|
+
# ## Some H2 title...
|
50
|
+
#
|
51
|
+
# {% code ruby %}
|
52
|
+
#
|
53
|
+
# # All this code will be highlighted properly! :)
|
54
|
+
# def my_method(*my_args)
|
55
|
+
# something do
|
56
|
+
# 3 + 4
|
57
|
+
# end
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# {% endcode %}
|
61
|
+
# """
|
62
|
+
#
|
63
|
+
# After that, in your view you just have to call `@post.rendered_body` and,
|
64
|
+
# provided you included the generated css in your layout, it will display nice
|
65
|
+
# and coloured :)
|
66
|
+
|
67
|
+
#### Show me the code!
|
68
|
+
|
69
|
+
# We'll need to include [RDiscount][rd]. This is the Markdown parsing library.
|
70
|
+
# Also, [CodeRay][cr] will provide code highlighting. We require `active_record` as
|
71
|
+
# well, since we want to extend it with our DSL.
|
72
|
+
#
|
73
|
+
# [rd]: http://github.com/rtomayko/rdiscount
|
74
|
+
# [cr]: http://github.com/rubychan/coderay
|
1
75
|
require 'rdiscount'
|
2
76
|
require 'coderay'
|
3
77
|
require 'active_record' unless defined?(ActiveRecord)
|
@@ -5,25 +79,47 @@ require 'active_record' unless defined?(ActiveRecord)
|
|
5
79
|
module Markdownizer
|
6
80
|
|
7
81
|
class << self
|
82
|
+
# Here we define two helper methods. These will be called from the model to
|
83
|
+
# perform the corresponding conversions and parsings.
|
84
|
+
|
85
|
+
# `Markdownizer.markdown` method converts plain Markdown text to formatted html.
|
8
86
|
def markdown(text)
|
9
87
|
RDiscount.new(text).to_html
|
10
88
|
end
|
11
89
|
|
90
|
+
# `Markdownizer.coderay` method parses a code block delimited from `{% code
|
91
|
+
# ruby %}` until `{% endcode %}` and replaces it with appropriate classes for
|
92
|
+
# code highlighting. It can take many languages aside from Ruby.
|
12
93
|
def coderay(text)
|
13
|
-
text.gsub(%r[\{%
|
94
|
+
text.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do
|
14
95
|
CodeRay.scan($2, $1).div(:css => :class)
|
15
96
|
end
|
16
97
|
end
|
17
98
|
end
|
18
99
|
|
100
|
+
#### Public interface
|
101
|
+
|
102
|
+
# The Markdownizer DSL is the public interface of the gem, and can be called
|
103
|
+
# from any ActiveRecord model.
|
19
104
|
module DSL
|
105
|
+
|
106
|
+
# Calling `markdownize! :attribute` (where `:attribute` can be any database
|
107
|
+
# attribute with type `text`) will treat this field as Markdown.
|
20
108
|
def markdownize! attribute
|
109
|
+
# Check that both `:attribute` and `:rendered_attribute` columns exist.
|
110
|
+
# If they don't, it raises an error indicating that the user should generate
|
111
|
+
# a migration.
|
21
112
|
unless self.column_names.include?(attribute.to_s) &&
|
22
113
|
self.column_names.include?("rendered_#{attribute}")
|
23
114
|
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
115
|
end
|
116
|
+
|
117
|
+
# Create a `before_save` callback which will convert plain text to
|
118
|
+
# Markdownized html every time the model is saved.
|
25
119
|
self.before_save :"render_#{attribute}"
|
26
120
|
|
121
|
+
# Define the converter method, which will assign the rendered html to the
|
122
|
+
# `:rendered_attribute` field.
|
27
123
|
define_method :"render_#{attribute}" do
|
28
124
|
self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute))))
|
29
125
|
end
|
@@ -32,4 +128,7 @@ module Markdownizer
|
|
32
128
|
|
33
129
|
end
|
34
130
|
|
131
|
+
# Finally, make our DSL available to any class inheriting from ActiveRecord::Base.
|
35
132
|
ActiveRecord::Base.send(:extend, Markdownizer::DSL)
|
133
|
+
|
134
|
+
# And that's it!
|
data/lib/markdownizer/version.rb
CHANGED
data/markdownizer.gemspec
CHANGED
@@ -18,6 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.add_runtime_dependency 'rdiscount'
|
19
19
|
s.add_runtime_dependency 'coderay'
|
20
20
|
|
21
|
+
s.add_development_dependency 'rocco'
|
22
|
+
s.add_development_dependency 'git'
|
23
|
+
s.add_development_dependency 'pygments'
|
21
24
|
s.add_development_dependency 'rspec', '~> 2.4.0'
|
22
25
|
|
23
26
|
s.files = `git ls-files`.split("\n")
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Josep M. Bach
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-02-
|
19
|
+
date: 2011-02-03 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -61,9 +61,48 @@ dependencies:
|
|
61
61
|
type: :runtime
|
62
62
|
version_requirements: *id003
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
|
-
name:
|
64
|
+
name: rocco
|
65
65
|
prerelease: false
|
66
66
|
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: git
|
78
|
+
prerelease: false
|
79
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
type: :development
|
88
|
+
version_requirements: *id005
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: pygments
|
91
|
+
prerelease: false
|
92
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
type: :development
|
101
|
+
version_requirements: *id006
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: rspec
|
104
|
+
prerelease: false
|
105
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
67
106
|
none: false
|
68
107
|
requirements:
|
69
108
|
- - ~>
|
@@ -74,7 +113,7 @@ dependencies:
|
|
74
113
|
- 0
|
75
114
|
version: 2.4.0
|
76
115
|
type: :development
|
77
|
-
version_requirements: *
|
116
|
+
version_requirements: *id007
|
78
117
|
description: Render any text as markdown, with code highlighting and all!
|
79
118
|
email:
|
80
119
|
- info@codegram.com
|