将 vBulletin 4 论坛迁移到 Discourse

您好。只是想分享我的解决方案。
关于引用问题。正如我之前所说,我遇到了问题——正则表达式无法捕获引用,因为:

  • 用户名和帖子 ID 可能用双引号括起来
  • 嵌套引用

我决定使用不同的逻辑进行搜索和替换。我没有搜索标签及其内容,而是使用了一个只搜索标签的正则表达式:

以前:
raw.gsub!(%r{\\[quote=\"?([^;]+);(\\d+)\"?\\](.+?)\\[\\/quote\\]}im) do

变成:
raw.gsub!(%r{(\\[QUOTE(=\"?([^;]+);(\\d+)\"?)?\\])|(\\[\\/QUOTE\\])}im) do

然后稍微改变了确定引用来源的方法:

      if $3 && $4
        if topic_lookup = topic_lookup_from_imported_post_id(post_id)
          post_number = topic_lookup[:post_number]
          topic_id = topic_lookup[:topic_id]
          "\n[quote=\"#{new_username},post:#{post_number},topic:#{topic_id}\"]\n"
        else
          "\n[quote=\"#{new_username}\"]\n"
        end
      elsif $5
        "\n[/quote]\n"
      end

此外,我还更改了 spoiler 的代码。以前:

    # [spoiler=Some hidden stuff]SPOILER HERE!![/spoiler]
    raw.gsub!(%r{\\[spoiler=\"?(.+?)\"?\\](.+?)\\[/spoiler\\]}im) do
      "\n#{$1}\n[spoiler]#{$2}[/spoiler]\n"
    end

这会模糊文本,我将其转换为 details 标签:

    raw.gsub!(%r{(\\[spoiler(=\"?(.*?)\"?)?\\])|(\\[\\/spoiler\\])}im) do
      if $3
        "\n[details=#{$3}]\n"
      elsif $1
        "\n[details]\n"
      elsif $4
        "\n[/details]\n"
      end
    end

因为碰巧在 vbulletin 世界中,spoiler 不是模糊的内容,而是折叠的内容。所以我认为对于 vbulletin 导入脚本来说,将 spoiler 转换为 details 比模糊的 spoiler 更合适。

我还注意到 mention 标签。在我的情况下,在 vbulletin 中,mention 的外观如下:
[mention=XXX]username[/mention]

脚本中使用的正则表达式没有考虑到标签可能包含用户 ID。

    # [MENTION]<username>[/MENTION]
    raw.gsub!(%r{\\[mention\\](.+?)\\[/mention\\]}i) do
      new_username = get_username_for_old_username($1)
      "@#{new_username}"
    end

我也以自己的方式修复了这个问题:

    # [MENTION]<username>[/MENTION]
    raw.gsub!(%r{\\[mention(=\\d+)?\\](.+?)\\[/mention\\]}i) do
      new_username = get_username_for_old_username($2)
      "@#{new_username}"
    end
2 个赞