Added code block normalization routing for import
This commit is contained in:
parent
544e7b999e
commit
05ca1e6e46
2
Gemfile
2
Gemfile
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
|
@ -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
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<code>
|
||||||
|
I am a te "
|
||||||
|
</code></pre>
|
||||||
|
test
|
||||||
|
<pre><code>this is a ""</code></pre>
|
||||||
|
MD
|
||||||
|
expected = " \n \n```\n I am a te \"\n \n```\n\n test \n \n```\nthis is a \"\"\n```\n\n"
|
||||||
|
Import::Normalize.normalize_code_blocks(markdown).should == expected
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue