diff --git a/app/assets/javascripts/discourse/dialects/bbcode_dialect.js b/app/assets/javascripts/discourse/dialects/bbcode_dialect.js index bf83367b3..c2ccdad76 100644 --- a/app/assets/javascripts/discourse/dialects/bbcode_dialect.js +++ b/app/assets/javascripts/discourse/dialects/bbcode_dialect.js @@ -1,3 +1,5 @@ +Discourse.BBCode = {}; + /** Create a simple BBCode tag handler @@ -13,7 +15,6 @@ @param {Boolean} [opts.wordBoundary] If true, the match must be on a word boundary @param {Boolean} [opts.spaceBoundary] If true, the match must be on a sppace boundary **/ -Discourse.BBCode = {}; Discourse.BBCode.register = function(codeName, args, emitter) { @@ -64,7 +65,7 @@ Discourse.BBCode.register = function(codeName, args, emitter) { }); }; -function replaceBBCode(tag, emitter, opts) { +Discourse.BBCode.replaceBBCode = function (tag, emitter, opts) { opts = opts || {}; opts = _.merge(opts, { start: "[" + tag + "]", stop: "[/" + tag + "]", emitter: emitter }); Discourse.Dialect.inlineBetween(opts); @@ -72,7 +73,7 @@ function replaceBBCode(tag, emitter, opts) { tag = tag.toUpperCase(); opts = _.merge(opts, { start: "[" + tag + "]", stop: "[/" + tag + "]", emitter: emitter }); Discourse.Dialect.inlineBetween(opts); -} +}; /** Shortcut to call replaceBBCode with `rawContents` as true. @@ -81,9 +82,9 @@ function replaceBBCode(tag, emitter, opts) { @param {tag} tag the tag we want to match @param {function} emitter the function that creates JsonML for the tag **/ -function rawBBCode(tag, emitter) { - replaceBBCode(tag, emitter, { rawContents: true }); -} +Discourse.BBCode.rawBBCode = function (tag, emitter) { + Discourse.BBCode.replaceBBCode(tag, emitter, { rawContents: true }); +}; /** Creates a BBCode handler that accepts parameters. Passes them to the emitter. @@ -92,10 +93,8 @@ function rawBBCode(tag, emitter) { @param {tag} tag the tag we want to match @param {function} emitter the function that creates JsonML for the tag **/ -function replaceBBCodeParamsRaw(tag, emitter) { - Discourse.Dialect.inlineBetween({ - start: "[" + tag + "=", - stop: "[/" + tag + "]", +Discourse.BBCode.replaceBBCodeParamsRaw = function (tag, emitter) { + var opts = { rawContents: true, emitter: function(contents) { var regexp = /^([^\]]+)\]([\S\s]*)$/, @@ -103,8 +102,13 @@ function replaceBBCodeParamsRaw(tag, emitter) { if (m) { return emitter.call(this, m[1], m[2]); } } - }); -} + }; + + Discourse.Dialect.inlineBetween(_.merge(opts, { start: "[" + tag + "=", stop: "[/" + tag + "]" })); + + tag = tag.toUpperCase(); + Discourse.Dialect.inlineBetween(_.merge(opts, { start: "[" + tag + "=", stop: "[/" + tag + "]" })); +}; /** Filters an array of JSON-ML nodes, removing nodes that represent empty lines ("\n"). @@ -112,28 +116,28 @@ function replaceBBCodeParamsRaw(tag, emitter) { @method removeEmptyLines @param {Array} [contents] Array of JSON-ML nodes **/ -function removeEmptyLines(contents) { +Discourse.BBCode.removeEmptyLines = function (contents) { var result = []; for (var i=0; i < contents.length; i++) { if (contents[i] !== "\n") { result.push(contents[i]); } } return result; -} +}; -replaceBBCode('b', function(contents) { return ['span', {'class': 'bbcode-b'}].concat(contents); }); -replaceBBCode('i', function(contents) { return ['span', {'class': 'bbcode-i'}].concat(contents); }); -replaceBBCode('u', function(contents) { return ['span', {'class': 'bbcode-u'}].concat(contents); }); -replaceBBCode('s', function(contents) { return ['span', {'class': 'bbcode-s'}].concat(contents); }); +Discourse.BBCode.replaceBBCode('b', function(contents) { return ['span', {'class': 'bbcode-b'}].concat(contents); }); +Discourse.BBCode.replaceBBCode('i', function(contents) { return ['span', {'class': 'bbcode-i'}].concat(contents); }); +Discourse.BBCode.replaceBBCode('u', function(contents) { return ['span', {'class': 'bbcode-u'}].concat(contents); }); +Discourse.BBCode.replaceBBCode('s', function(contents) { return ['span', {'class': 'bbcode-s'}].concat(contents); }); Discourse.Markdown.whiteListTag('span', 'class', /^bbcode-[bius]$/); -replaceBBCode('ul', function(contents) { return ['ul'].concat(removeEmptyLines(contents)); }); -replaceBBCode('ol', function(contents) { return ['ol'].concat(removeEmptyLines(contents)); }); -replaceBBCode('li', function(contents) { return ['li'].concat(removeEmptyLines(contents)); }); +Discourse.BBCode.replaceBBCode('ul', function(contents) { return ['ul'].concat(Discourse.BBCode.removeEmptyLines(contents)); }); +Discourse.BBCode.replaceBBCode('ol', function(contents) { return ['ol'].concat(Discourse.BBCode.removeEmptyLines(contents)); }); +Discourse.BBCode.replaceBBCode('li', function(contents) { return ['li'].concat(Discourse.BBCode.removeEmptyLines(contents)); }); -rawBBCode('img', function(contents) { return ['img', {href: contents}]; }); -rawBBCode('email', function(contents) { return ['a', {href: "mailto:" + contents, 'data-bbcode': true}, contents]; }); -rawBBCode('url', function(contents) { return ['a', {href: contents, 'data-bbcode': true}, contents]; }); -rawBBCode('spoiler', function(contents) { +Discourse.BBCode.rawBBCode('img', function(contents) { return ['img', {href: contents}]; }); +Discourse.BBCode.rawBBCode('email', function(contents) { return ['a', {href: "mailto:" + contents, 'data-bbcode': true}, contents]; }); +Discourse.BBCode.rawBBCode('url', function(contents) { return ['a', {href: contents, 'data-bbcode': true}, contents]; }); +Discourse.BBCode.rawBBCode('spoiler', function(contents) { if (/]...[/MENTION] - raw = raw.gsub(/\[mention=(\d+)\].+?\[\/mention\]/i) do - user_id = $1 + # [MENTION=][/MENTION] + raw = raw.gsub(/\[mention=(\d+)\](.+?)\[\/mention\]/i) do + user_id, old_username = $1, $2 if user = @users.select { |u| u[:userid] == user_id }.first - username = @old_username_to_new_usernames[user[:username]] || user[:username] - "@#{username}" - else - $& + old_username = @old_username_to_new_usernames[user[:username]] || user[:username] end + "@#{old_username}" end # [QUOTE]...[/QUOTE] @@ -368,11 +374,9 @@ class ImportScripts::VBulletin < ImportScripts::Base raw = raw.gsub(/\[quote=([^;\]]+)\](.+?)\[\/quote\]/im) do old_username, quote = $1, $2 if @old_username_to_new_usernames.has_key?(old_username) - username = @old_username_to_new_usernames[old_username] - "\n[quote=\"#{username}\"]\n#{quote}\n[/quote]\n" - else - $& + old_username = @old_username_to_new_usernames[old_username] end + "\n[quote=\"#{old_username}\"]\n#{quote}\n[/quote]\n" end # [HTML]...[/HTML] @@ -399,12 +403,24 @@ class ImportScripts::VBulletin < ImportScripts::Base # [MP3][/MP3] raw = raw.gsub(/\[MP3\](.+?)\[\/MP3\]/i) { "\n#{$1}\n" } + # replace all chevrons with HTML entities + raw = raw.gsub(/`([^`]+)`/im) { "`" + $1.gsub("<", "\u2603") + "`" } + .gsub("<", "<") + .gsub("\u2603", "<") + + raw = raw.gsub(/`([^`]+)`/im) { "`" + $1.gsub(">", "\u2603") + "`" } + .gsub(">", ">") + .gsub("\u2603", ">") + raw end def import_topics puts "", "Importing topics..." + # sort the topics + @topics.sort_by! { |topic| topic[:threadid].to_i } + create_posts(@topics) do |topic| next unless post = @posts.select { |p| p[:postid] == topic[:firstpostid] }.first @@ -441,8 +457,11 @@ class ImportScripts::VBulletin < ImportScripts::Base first_post_ids = Set.new(@topics.map { |t| t[:firstpostid] }) @posts.reject! { |post| first_post_ids.include?(post[:postid]) } + # sort the posts + @posts.sort_by! { |post| post[:postid].to_i } + create_posts(@posts) do |post| - t = topic_lookup_from_imported_post_id("thread#" + post[:threadid]) + next unless t = topic_lookup_from_imported_post_id("thread#" + post[:threadid]) p = { id: post[:postid], @@ -487,13 +506,17 @@ class ImportScripts::VBulletin < ImportScripts::Base # [QUOTE=;]...[/QUOTE] raw = raw.gsub(/\[quote=([^;]+);(\d+)\](.+?)\[\/quote\]/im) do old_username, post_id, quote = $1, $2, $3 - if @old_username_to_new_usernames.has_key?(old_username) && topic_lookup = topic_lookup_from_imported_post_id(post_id) + + if @old_username_to_new_usernames.has_key?(old_username) + old_username = @old_username_to_new_usernames[old_username] + end + + if topic_lookup = topic_lookup_from_imported_post_id(post_id) post_number = topic_lookup[:post_number] topic_id = topic_lookup[:topic_id] - username = @old_username_to_new_usernames[old_username] - "\n[quote=\"#{username},post:#{post_number},topic:#{topic_id}\"]\n#{quote}\n[/quote]\n" + "\n[quote=\"#{old_username},post:#{post_number},topic:#{topic_id}\"]\n#{quote}\n[/quote]\n" else - $& + "\n[quote=\"#{old_username}\"]\n#{quote}\n[/quote]\n" end end