diff --git a/Gemfile b/Gemfile index 13fdc577e..bb5ddeedd 100644 --- a/Gemfile +++ b/Gemfile @@ -199,6 +199,8 @@ gem 'fast_blank' #, github: "SamSaffron/fast_blank" # this provides a very efficient lru cache gem 'lru_redux' +gem 'htmlentities', require: false + # 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 # we are open to it. by deferring require to the initializer we can configure discourse installs without it diff --git a/Gemfile.lock b/Gemfile.lock index 473ff28ab..06c6daeb7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -130,6 +130,7 @@ GEM highline (1.6.20) hike (1.2.3) hiredis (0.5.1) + htmlentities (4.3.2) i18n (0.6.9) image_optim (0.9.1) exifr (~> 1.1.3) @@ -420,6 +421,7 @@ DEPENDENCIES handlebars-source (= 1.3.0) highline hiredis + htmlentities image_optim (= 0.9.1) librarian (>= 0.0.25) listen (= 0.7.3) diff --git a/lib/import/normalize.rb b/lib/import/normalize.rb new file mode 100644 index 000000000..368147c59 --- /dev/null +++ b/lib/import/normalize.rb @@ -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 diff --git a/spec/components/import/normalize_spec.rb b/spec/components/import/normalize_spec.rb new file mode 100644 index 000000000..209e2d76b --- /dev/null +++ b/spec/components/import/normalize_spec.rb @@ -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 +