lawrencepit-flavoured_markdown 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.markdown +78 -0
- data/lib/flavoured_markdown.rb +91 -0
- metadata +55 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Lawrence Pit
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
Flavoured Markdown
|
2
|
+
==================
|
3
|
+
|
4
|
+
*Websafe Markdown with some extra flavour.*
|
5
|
+
|
6
|
+
Markdown is a way to use an easy-to-read, easy-to-write plain text format that is turned into valid XHTML or HTML. See the <a href='http://daringfireball.net/projects/markdown/syntax'>Markdown Syntax</a> at Daring Fireball.
|
7
|
+
|
8
|
+
It's important to realize that Markdown by default accepts any HTML code as its source. If you use Markdown as a way for your users to enter text easily via a web interface, providing easy markup capabilities, they could potentially enter harmful code (javascripts, styles, etc.). Flavoured Markdown escapes all HTML code by default.
|
9
|
+
|
10
|
+
The extra flavors provided by Flavoured Markdown:
|
11
|
+
|
12
|
+
* Do not allow javascript, styles and HTML codes
|
13
|
+
* Except for A tags that have no on* attributes
|
14
|
+
* And except for BR tags
|
15
|
+
* HTML entities are alllowed (e.g.: ©, &, etc.)
|
16
|
+
* URLs and emails are automatically hyperlinked. Long hyperlinks are automatically limited to 55 characters.
|
17
|
+
* Three consecutive dots are replaced by an ellipsis character
|
18
|
+
* Fancy pants mode
|
19
|
+
* Prevents paragraphs and list items from having an <a href='http://en.wikipedia.org/wiki/Widows_and_orphans'>orphan</a>.
|
20
|
+
|
21
|
+
|
22
|
+
Usage
|
23
|
+
-----
|
24
|
+
|
25
|
+
flavoured_markdown("# Hello World #\n----\n* Item A\n* Item B")
|
26
|
+
|
27
|
+
|
28
|
+
Requirements
|
29
|
+
------------
|
30
|
+
|
31
|
+
This gem/plugin depends on the rdiscount and actionpack gems. To install, run:
|
32
|
+
|
33
|
+
$ sudo gem install rdiscount
|
34
|
+
$ sudo gem install actionpack
|
35
|
+
|
36
|
+
|
37
|
+
Installation
|
38
|
+
------------
|
39
|
+
|
40
|
+
#### As a Gem
|
41
|
+
|
42
|
+
Use this if you prefer to use versioned releases of Flavoured Markdown.
|
43
|
+
Specify the gem dependency in your config/environment.rb file:
|
44
|
+
|
45
|
+
Rails::Initializer.run do |config|
|
46
|
+
config.gem "lawrencepit-flavoured_markdown", :lib => "flavoured_markdown", :source => "http://gems.github.com"
|
47
|
+
end
|
48
|
+
|
49
|
+
Then:
|
50
|
+
|
51
|
+
$ rake gems:install
|
52
|
+
$ rake gems:unpack
|
53
|
+
|
54
|
+
#### As a Rails Plugin
|
55
|
+
|
56
|
+
Use this if you prefer to use the edge version of Flavoured Markdown within a Rails application:
|
57
|
+
|
58
|
+
$ script/plugin install git://github.com/lawrencepit/flavoured_markdown.git
|
59
|
+
|
60
|
+
|
61
|
+
Options
|
62
|
+
-------
|
63
|
+
|
64
|
+
No options. Hack it.
|
65
|
+
|
66
|
+
|
67
|
+
Credits
|
68
|
+
-------
|
69
|
+
|
70
|
+
Written by [Lawrence Pit](http://lawrencepit.com).
|
71
|
+
|
72
|
+
Thanks to GitHub for their amazing services!
|
73
|
+
GitHub has it's own <a href='http://github.github.com/github-flavored-markdown/'>GitHub Flavored Markdown</a>.
|
74
|
+
You can have yours too.
|
75
|
+
|
76
|
+
|
77
|
+
----
|
78
|
+
Copyright (c) 2009 Lawrence Pit, released under the MIT license
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'rdiscount'
|
2
|
+
require 'digest/md5'
|
3
|
+
require 'action_view'
|
4
|
+
|
5
|
+
module ActionView
|
6
|
+
module Helpers
|
7
|
+
module Markdown
|
8
|
+
include TagHelper
|
9
|
+
include TextHelper
|
10
|
+
include CaptureHelper
|
11
|
+
include UrlHelper
|
12
|
+
|
13
|
+
def flavoured_markdown(str)
|
14
|
+
str = RDiscount.new(str, :smart, :filter_styles, :filter_html).to_html
|
15
|
+
|
16
|
+
# Extract pre blocks
|
17
|
+
str = extract_markup(str, :pre)
|
18
|
+
|
19
|
+
# Undo markdown's html encoding of breaks
|
20
|
+
str.gsub!(%r{<br\s*\/?>}m, "<br/>")
|
21
|
+
|
22
|
+
# Undo markdowns' html encoding of hyperlinks, except for those that
|
23
|
+
# contain an on* attribute which may contain javascript code.
|
24
|
+
str.gsub!(%r{<a\s(.*?)</a>}m) do
|
25
|
+
match = $1
|
26
|
+
match =~ %r{\son\w}m ? "<a #{match}</a>" : "<a #{match}</a>"
|
27
|
+
end
|
28
|
+
|
29
|
+
# Extract hyperlink html code
|
30
|
+
str = extract_markup(str, :a)
|
31
|
+
|
32
|
+
# Auto hyperlink urls and email addresses
|
33
|
+
str = auto_link(str, :all) do |txt|
|
34
|
+
txt.size < 55 ? txt : truncate(txt, :length => 50, :omission => "...")
|
35
|
+
end
|
36
|
+
|
37
|
+
# Extract the new hyperlinks
|
38
|
+
str = extract_markup(str, :a)
|
39
|
+
|
40
|
+
# preserve whitespace for haml
|
41
|
+
#str = str.chomp("\n").gsub(/\n/, '
').gsub(/\r/, '')
|
42
|
+
|
43
|
+
# Prevent an orphan for P and LI tags
|
44
|
+
str.gsub!(%r{<(p|li)\b[^>]*>(.*?)</\1>}m) do |match|
|
45
|
+
last_space_index = match.rindex(' ')
|
46
|
+
if last_space_index
|
47
|
+
match = match.slice(0, last_space_index) + " " + match.slice(last_space_index + 1, match.length - last_space_index - 1)
|
48
|
+
end
|
49
|
+
match
|
50
|
+
end
|
51
|
+
|
52
|
+
# Add css class "last" to last P tag if it's the last element
|
53
|
+
str.sub!(%r{<p>(.*?)</p>\Z}, "<p class=\"last\">\\1</p>")
|
54
|
+
|
55
|
+
# Turn 3 consecutive dot characters into an ellipsis character
|
56
|
+
str = hellip(str)
|
57
|
+
|
58
|
+
# Put back in the previously extracted markup
|
59
|
+
str = add_extracted_markup(str)
|
60
|
+
|
61
|
+
str
|
62
|
+
end
|
63
|
+
alias_method :flavored_markdown, :flavoured_markdown
|
64
|
+
|
65
|
+
private
|
66
|
+
# Parts from Github Flavored Markdown
|
67
|
+
# http://github.github.com/github-flavored-markdown/
|
68
|
+
def extract_markup(str, mode = :a)
|
69
|
+
regexp = mode == :pre ? %r{<pre>.*?</pre>}m : %r{<a\s(.*?)>(.*?)</a>}m
|
70
|
+
@extractions ||= {}
|
71
|
+
str.gsub(regexp) do |match|
|
72
|
+
match = "<a #{$1}>#{hellip($2)}</a>" if mode == :a
|
73
|
+
md5 = Digest::MD5.hexdigest(match)
|
74
|
+
@extractions[md5] = match
|
75
|
+
"{extraction-#{md5}}"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Insert back the extracted markup
|
80
|
+
def add_extracted_markup(str)
|
81
|
+
str.gsub(/\{extraction-([0-9a-f]{32})\}/) do
|
82
|
+
@extractions[$1]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def hellip(str)
|
87
|
+
str.gsub(%r{\b\.\.\.}m, "…")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lawrencepit-flavoured_markdown
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lawrence Pit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: lawrence.pit@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.markdown
|
26
|
+
- MIT-LICENSE
|
27
|
+
- lib/flavoured_markdown.rb
|
28
|
+
has_rdoc: false
|
29
|
+
homepage: http://github.com/lawrencepit/flavoured_markdown
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.2.0
|
51
|
+
signing_key:
|
52
|
+
specification_version: 2
|
53
|
+
summary: Websafe markdown with some extra flavour.
|
54
|
+
test_files: []
|
55
|
+
|