Added code block normalization routing for import

This commit is contained in:
Sam Saffron 2014-06-06 10:34:21 +10:00
parent 544e7b999e
commit 05ca1e6e46
4 changed files with 37 additions and 0 deletions

View File

@ -199,6 +199,8 @@ gem 'fast_blank' #, github: "SamSaffron/fast_blank"
# this provides a very efficient lru cache # this provides a very efficient lru cache
gem 'lru_redux' gem 'lru_redux'
gem 'htmlentities', require: false
# IMPORTANT: mini profiler monkey patches, so it better be required last # IMPORTANT: mini profiler monkey patches, so it better be required last
# If you want to amend mini profiler to do the monkey patches in the railstie # If you want to amend mini profiler to do the monkey patches in the railstie
# we are open to it. by deferring require to the initializer we can configure discourse installs without it # we are open to it. by deferring require to the initializer we can configure discourse installs without it

View File

@ -130,6 +130,7 @@ GEM
highline (1.6.20) highline (1.6.20)
hike (1.2.3) hike (1.2.3)
hiredis (0.5.1) hiredis (0.5.1)
htmlentities (4.3.2)
i18n (0.6.9) i18n (0.6.9)
image_optim (0.9.1) image_optim (0.9.1)
exifr (~> 1.1.3) exifr (~> 1.1.3)
@ -420,6 +421,7 @@ DEPENDENCIES
handlebars-source (= 1.3.0) handlebars-source (= 1.3.0)
highline highline
hiredis hiredis
htmlentities
image_optim (= 0.9.1) image_optim (= 0.9.1)
librarian (>= 0.0.25) librarian (>= 0.0.25)
listen (= 0.7.3) listen (= 0.7.3)

12
lib/import/normalize.rb Normal file
View File

@ -0,0 +1,12 @@
# markdown normalizer to be used by importers
#
#
require 'htmlentities'
module Import::Normalize
def self.normalize_code_blocks(code, lang=nil)
coder = HTMLEntities.new
code.gsub(/<pre>\s*<code>\n?(.*?)\n?<\/code>\s*<\/pre>/m) {
"\n```#{lang}\n#{coder.decode($1)}\n```\n"
}
end
end

View File

@ -0,0 +1,21 @@
require "spec_helper"
require_dependency "import/normalize"
describe Import::Normalize do
describe "#normalize_code_blocks" do
it "normalizes 2 code blocks correctly" do
markdown = <<MD
&nbsp;
<pre>
<code>
I am a te&nbsp;&quot;
</code></pre>
test &nbsp;
<pre><code>this is a &quot;&quot;</code></pre>
MD
expected = " &nbsp;\n \n```\n I am a te \"\n \n```\n\n test &nbsp;\n \n```\nthis is a \"\"\n```\n\n"
Import::Normalize.normalize_code_blocks(markdown).should == expected
end
end
end