Skip to content

Commit

Permalink
Moving from RDiscount to Redcarpet (like github)
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed Apr 21, 2011
1 parent c52607d commit dd69e9a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ gem "kaminari", "~>0.11"
gem "mini_magick", "~>3.2"
gem "mysql2", "~>0.2"
gem "nokogiri", "~>1.4"
gem "rdiscount", "~>1.6"
gem "redcarpet", "~>1.10"
gem "hiredis", "~>0.3"
gem "redis", "~>2.2", :require => ["redis/connection/hiredis", "redis"]
gem "sanitize", "~>2.0"
Expand Down
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ GEM
activesupport (= 3.0.7)
activesupport (3.0.7)
acts_as_list (0.1.2)
albino (1.3.2)
posix-spawn (>= 0.3.3)
albino (1.3.3)
posix-spawn (>= 0.3.6)
annotate (2.4.0)
arel (2.0.9)
autotest (4.4.6)
Expand All @@ -56,7 +56,7 @@ GEM
chalofa_ruby-progressbar (0.0.9.1)
closure-compiler (1.1.1)
daemons (1.1.2)
devise (1.3.1)
devise (1.3.3)
bcrypt-ruby (~> 2.1.2)
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
Expand Down Expand Up @@ -92,7 +92,7 @@ GEM
html_spellchecker (0.1.4)
hunspell-ffi (= 0.1.3.alpha2)
nokogiri (~> 1.4)
html_truncator (0.2.1)
html_truncator (0.2.2)
nokogiri (~> 1.4)
htmlentities (4.3.0)
hunspell-ffi (0.1.3.alpha2)
Expand Down Expand Up @@ -129,7 +129,7 @@ GEM
open4 (1.0.1)
orm_adapter (0.0.4)
polyglot (0.3.1)
posix-spawn (0.3.5)
posix-spawn (0.3.6)
rack (1.2.2)
rack-mount (0.6.14)
rack (>= 1.0.0)
Expand All @@ -151,7 +151,7 @@ GEM
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
rdiscount (1.6.8)
redcarpet (1.10.0)
redis (2.2.0)
rspec (2.5.0)
rspec-core (~> 2.5.0)
Expand Down Expand Up @@ -182,7 +182,7 @@ GEM
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.26)
unicorn (3.5.0)
unicorn (3.6.0)
kgio (~> 2.3)
rack
warden (1.0.3)
Expand Down Expand Up @@ -228,7 +228,7 @@ DEPENDENCIES
nono-redis-store (~> 1.0)
rails (= 3.0.7)
rails3-generators
rdiscount (~> 1.6)
redcarpet (~> 1.10)
redis (~> 2.2)
rspec-rails (~> 2.5)
sanitize (~> 2.0)
Expand Down
18 changes: 6 additions & 12 deletions lib/lfmarkdown.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: UTF-8
require "rdiscount"
require "redcarpet"
require "digest/sha1"

# LinuxFr Flavored Markdown
Expand All @@ -13,23 +13,25 @@
# * PHP Markdown Extra-style tables are supported
# * and some other extensions listed on http://www.pell.portland.or.us/~orc/Code/discount/#Language+extensions
#
class LFMarkdown < Markdown
class LFMarkdown < Redcarpet

def initialize(text, *extensions)
text ||= ''
@filter_styles = true
@filter_html = true
@autolink = true
@tables = true
@strikethrough = true
@hard_wrap = true
@codemap = {}
@generate_toc = text.length > 20_000 # See https://github.com/rtomayko/rdiscount/issues/36
@generate_toc = text.length > 5_000
super(text.dup, *extensions)
end

def to_html
extract_code
process_internal_wiki_links
process_wikipedia_links
process_newlines
ret = fix_heading_levels(super)
ret = process_code(ret)
ret = add_toc_content(ret) if @generate_toc
Expand All @@ -49,14 +51,6 @@ def process_wikipedia_links
@text.gsub!(WP_LINK_REGEXP, '[\1](http://fr.wikipedia.org/wiki/\1 "Définition Wikipédia")')
end

# Code taken from http://github.com/github-flavored-markdown/
def process_newlines
@text.gsub!("\r", "")
@text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
x.gsub(/^(.+)$/, "\\1 ")
end
end

def fix_heading_levels(str)
str.gsub!(/<(\/?)h(\d)/) { |_| "<#{$1}h#{$2.to_i + 1}" }
str
Expand Down
2 changes: 1 addition & 1 deletion lib/lfsanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LFSanitizer
th thead tr time ul var video wbr),
:attributes => {
:all => ['data-after', 'data-id', 'id', 'title', 'class'],
'a' => ['href'],
'a' => ['href', 'name'],
'blockquote' => ['cite'],
'img' => ['alt', 'height', 'src', 'width'],
'q' => ['cite'],
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/lfmarkdown_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

it "transforms [[]] to wikipedia links, even with spaces and accents" do
html = LFMarkdown.new("[[Paul Erdős]]").to_html
html.should == "<p><a href=\"http://fr.wikipedia.org/wiki/Paul%20Erd%C5%91s\" title=\"Définition Wikipédia\">Paul Erdős</a></p>\n"
html.should == "<p><a href=\"http://fr.wikipedia.org/wiki/Paul Erdős\" title=\"Définition Wikipédia\">Paul Erdős</a></p>\n"
end

it "transforms [[]] to wikipedia links even for categories (with : . and -)" do
Expand Down

0 comments on commit dd69e9a

Please sign in to comment.