From 192a0886e2dea106fa6f1c5eaa0ece137ecbd70e Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Tue, 30 Jan 2018 12:49:51 +0100 Subject: [PATCH] FIX: BBCode to Markdown conversion in phpBB3 importer was broken This fixes the conversion for quotes, code blocks and lists (except for nested lists). It also discourages the usage of the ruby-bbcode-to-md gem. --- script/import_scripts/phpbb3/settings.yml | 4 ++-- .../phpbb3/support/text_processor.rb | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/script/import_scripts/phpbb3/settings.yml b/script/import_scripts/phpbb3/settings.yml index 4fd15fc9d0b..08b50e283a1 100644 --- a/script/import_scripts/phpbb3/settings.yml +++ b/script/import_scripts/phpbb3/settings.yml @@ -11,8 +11,8 @@ database: batch_size: 1000 # Don't change this unless you know what you're doing. The default (1000) should work just fine. import: - # Enable this option if you want to have a better conversion of BBCodes to Markdown. - # WARNING: This can slow down your import. + # WARNING: Do not activate this option unless you know what you are doing. + # It will probably break the BBCode to Markdown conversion and slows down your import. use_bbcode_to_md: false # This is the path to the root directory of your current phpBB installation (or a copy of it). diff --git a/script/import_scripts/phpbb3/support/text_processor.rb b/script/import_scripts/phpbb3/support/text_processor.rb index d89a6cc99fb..fa6e623d328 100644 --- a/script/import_scripts/phpbb3/support/text_processor.rb +++ b/script/import_scripts/phpbb3/support/text_processor.rb @@ -27,6 +27,7 @@ module ImportScripts::PhpBB3 process_links(text) process_lists(text) process_code(text) + fix_markdown(text) text end @@ -114,11 +115,13 @@ module ImportScripts::PhpBB3 # convert list tags to ul and list=1 tags to ol # list=a is not supported, so handle it like list=1 # list=9 and list=x have the same result as list=1 and list=a - text.gsub!(/\[list\](.*?)\[\/list:u\]/mi, '[ul]\1[/ul]') - text.gsub!(/\[list=.*?\](.*?)\[\/list:o\]/mi, '[ol]\1[/ol]') + text.gsub!(/\[list\](.*?)\[\/list:u\]/mi) do + $1.gsub(/\[\*\](.*?)\[\/\*:m\]\n*/mi) { "* #{$1}\n" } + end - # convert *-tags to li-tags so bbcode-to-md can do its magic on phpBB's lists: - text.gsub!(/\[\*\](.*?)\[\/\*:m\]/mi, '[li]\1[/li]') + text.gsub!(/\[list=.*?\](.*?)\[\/list:o\]/mi) do + $1.gsub(/\[\*\](.*?)\[\/\*:m\]\n*/mi) { "1. #{$1}\n" } + end end # This replaces existing [attachment] BBCodes with the corresponding HTML tags for Discourse. @@ -149,9 +152,14 @@ module ImportScripts::PhpBB3 def process_code(text) text.gsub!(//, "\n") text end + + def fix_markdown(text) + text.gsub!(/(\n*\[\/?quote.*?\]\n*)/mi) { |q| "\n#{q.strip}\n" } + text + end end end