From 3767080f3bc371862ed0b7c597e65e88b495d097 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 6 Jun 2014 14:08:39 +1000 Subject: [PATCH] Added rake task to normalize code --- lib/import/normalize.rb | 1 + lib/tasks/posts.rake | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/import/normalize.rb b/lib/import/normalize.rb index 368147c59..79dfb255f 100644 --- a/lib/import/normalize.rb +++ b/lib/import/normalize.rb @@ -2,6 +2,7 @@ # # require 'htmlentities' +module Import; end module Import::Normalize def self.normalize_code_blocks(code, lang=nil) coder = HTMLEntities.new diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rake index 5b448e3c1..2b5c023dc 100644 --- a/lib/tasks/posts.rake +++ b/lib/tasks/posts.rake @@ -36,3 +36,24 @@ def rebake_posts(opts = {}) puts "\n\n#{total} posts done!\n#{'-' * 50}\n" end + + +desc 'normalize all markdown so
 is not used and instead backticks'
+task 'posts:normalize_code' => :environment do
+  lang = ENV['CODE_LANG'] || ''
+  require 'import/normalize'
+
+  puts "Normalizing"
+  i = 0
+  Post.where("raw like '%
%%'").each do |p|
+    normalized = Import::Normalize.normalize_code_blocks(p.raw, lang)
+    if normalized != p.raw
+      p.revise(Discourse.system_user, normalized)
+      putc "."
+      i += 1
+    end
+  end
+
+  puts
+  puts "#{i} posts normalized!"
+end