FEATURE: option to use ruby-bbcode-to-md in bulk import script

FEATURE: option to use ruby-bbcode-to-md in bulk import script

ruby-bbcode-to-md provides better bbcode to markdown conversion

From 735a48415d4d8da878c52f504cbab3713a6b0eb6 Mon Sep 17 00:00:00 2001
From: Arpit Jalan <arpit@techapj.com>
Date: Mon, 10 Dec 2018 10:28:07 +0530
Subject: [PATCH] FEATURE: option to use ruby-bbcode-to-md in bulk import
 script

ruby-bbcode-to-md provides better bbcode to markdown conversion

diff --git a/script/bulk_import/base.rb b/script/bulk_import/base.rb
index ce6ef27..cf1be39 100644
--- a/script/bulk_import/base.rb
+++ b/script/bulk_import/base.rb
@@ -1,3 +1,16 @@
+if ARGV.include?('bbcode-to-md')
+  # Replace (most) bbcode with markdown before creating posts.
+  # This will dramatically clean up the final posts in Discourse.
+  #
+  # In a temp dir:
+  #
+  # git clone https://github.com/nlalonde/ruby-bbcode-to-md.git
+  # cd ruby-bbcode-to-md
+  # gem build ruby-bbcode-to-md.gemspec
+  # gem install ruby-bbcode-to-md-*.gem
+  require 'ruby-bbcode-to-md'
+end
+
 require "pg"
 require "set"
 require "redcarpet"
@@ -66,6 +79,7 @@ class BulkImport::Base
     @uploader = ImportScripts::Uploader.new
     @html_entities = HTMLEntities.new
     @encoding = CHARSET_MAP[charset]
+    @bbcode_to_md = true if use_bbcode_to_md?
 
     @markdown = Redcarpet::Markdown.new(
       Redcarpet::Render::HTML.new(hard_wrap: true),
@@ -172,6 +186,10 @@ class BulkImport::Base
     @last_post_action_id = last_id(PostAction)
   end
 
+  def use_bbcode_to_md?
+    ARGV.include?("bbcode-to-md")
+  end
+
   def execute
     raise NotImplementedError
   end
@@ -463,6 +481,9 @@ class BulkImport::Base
     @topic_id_by_post_id[post[:id]] = post[:topic_id]
     post[:raw] = (post[:raw] || "").scrub.strip.presence || "<Empty imported post>"
     post[:raw] = process_raw post[:raw]
+    if @bbcode_to_md
+      post[:raw] = post[:raw].bbcode_to_md(false) rescue post[:raw]
+    end
     post[:like_count] ||= 0
     post[:cooked] = pre_cook post[:raw]
     post[:hidden] ||= false

GitHub