mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 13:31:18 +08:00
DEV: Apply syntax_tree formatting to script/*
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'nokogiri'
|
||||
require 'optparse'
|
||||
require "nokogiri"
|
||||
require "optparse"
|
||||
require File.expand_path(File.dirname(__FILE__) + "/base")
|
||||
|
||||
class ImportScripts::Disqus < ImportScripts::Base
|
||||
@ -35,7 +35,7 @@ class ImportScripts::Disqus < ImportScripts::Base
|
||||
by_email = {}
|
||||
|
||||
@parser.posts.each do |id, p|
|
||||
next if p[:is_spam] == 'true' || p[:is_deleted] == 'true'
|
||||
next if p[:is_spam] == "true" || p[:is_deleted] == "true"
|
||||
by_email[p[:author_email]] = { name: p[:author_name], username: p[:author_username] }
|
||||
end
|
||||
|
||||
@ -45,13 +45,7 @@ class ImportScripts::Disqus < ImportScripts::Base
|
||||
|
||||
create_users(by_email.keys) do |email|
|
||||
user = by_email[email]
|
||||
{
|
||||
id: email,
|
||||
email: email,
|
||||
username: user[:username],
|
||||
name: user[:name],
|
||||
merge: true
|
||||
}
|
||||
{ id: email, email: email, username: user[:username], name: user[:name], merge: true }
|
||||
end
|
||||
end
|
||||
|
||||
@ -59,7 +53,6 @@ class ImportScripts::Disqus < ImportScripts::Base
|
||||
puts "", "importing topics..."
|
||||
|
||||
@parser.threads.each do |id, t|
|
||||
|
||||
title = t[:title]
|
||||
title.gsub!(/“/, '"')
|
||||
title.gsub!(/”/, '"')
|
||||
@ -79,7 +72,7 @@ class ImportScripts::Disqus < ImportScripts::Base
|
||||
|
||||
if post.present? && post.topic.posts_count <= 1
|
||||
(t[:posts] || []).each do |p|
|
||||
post_user = find_existing_user(p[:author_email] || '', p[:author_username])
|
||||
post_user = find_existing_user(p[:author_email] || "", p[:author_username])
|
||||
next unless post_user.present?
|
||||
|
||||
attrs = {
|
||||
@ -87,7 +80,7 @@ class ImportScripts::Disqus < ImportScripts::Base
|
||||
topic_id: post.topic_id,
|
||||
raw: p[:cooked],
|
||||
cooked: p[:cooked],
|
||||
created_at: Date.parse(p[:created_at])
|
||||
created_at: Date.parse(p[:created_at]),
|
||||
}
|
||||
|
||||
if p[:parent_id]
|
||||
@ -125,23 +118,22 @@ class DisqusSAX < Nokogiri::XML::SAX::Document
|
||||
end
|
||||
|
||||
def start_element(name, attrs = [])
|
||||
|
||||
hashed = Hash[attrs]
|
||||
case name
|
||||
when 'post'
|
||||
when "post"
|
||||
@post = {}
|
||||
@post[:id] = hashed['dsq:id'] if @post
|
||||
when 'thread'
|
||||
id = hashed['dsq:id']
|
||||
@post[:id] = hashed["dsq:id"] if @post
|
||||
when "thread"
|
||||
id = hashed["dsq:id"]
|
||||
if @post
|
||||
thread = @threads[id]
|
||||
thread[:posts] << @post
|
||||
else
|
||||
@thread = { id: id, posts: [] }
|
||||
end
|
||||
when 'parent'
|
||||
when "parent"
|
||||
if @post
|
||||
id = hashed['dsq:id']
|
||||
id = hashed["dsq:id"]
|
||||
@post[:parent_id] = id
|
||||
end
|
||||
end
|
||||
@ -151,10 +143,10 @@ class DisqusSAX < Nokogiri::XML::SAX::Document
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'post'
|
||||
when "post"
|
||||
@posts[@post[:id]] = @post
|
||||
@post = nil
|
||||
when 'thread'
|
||||
when "thread"
|
||||
if @post.nil?
|
||||
@threads[@thread[:id]] = @thread
|
||||
@thread = nil
|
||||
@ -165,25 +157,25 @@ class DisqusSAX < Nokogiri::XML::SAX::Document
|
||||
end
|
||||
|
||||
def characters(str)
|
||||
record(@post, :author_email, str, 'author', 'email')
|
||||
record(@post, :author_name, str, 'author', 'name')
|
||||
record(@post, :author_username, str, 'author', 'username')
|
||||
record(@post, :author_anonymous, str, 'author', 'isAnonymous')
|
||||
record(@post, :created_at, str, 'createdAt')
|
||||
record(@post, :is_deleted, str, 'isDeleted')
|
||||
record(@post, :is_spam, str, 'isSpam')
|
||||
record(@post, :author_email, str, "author", "email")
|
||||
record(@post, :author_name, str, "author", "name")
|
||||
record(@post, :author_username, str, "author", "username")
|
||||
record(@post, :author_anonymous, str, "author", "isAnonymous")
|
||||
record(@post, :created_at, str, "createdAt")
|
||||
record(@post, :is_deleted, str, "isDeleted")
|
||||
record(@post, :is_spam, str, "isSpam")
|
||||
|
||||
record(@thread, :link, str, 'link')
|
||||
record(@thread, :title, str, 'title')
|
||||
record(@thread, :created_at, str, 'createdAt')
|
||||
record(@thread, :author_email, str, 'author', 'email')
|
||||
record(@thread, :author_name, str, 'author', 'name')
|
||||
record(@thread, :author_username, str, 'author', 'username')
|
||||
record(@thread, :author_anonymous, str, 'author', 'isAnonymous')
|
||||
record(@thread, :link, str, "link")
|
||||
record(@thread, :title, str, "title")
|
||||
record(@thread, :created_at, str, "createdAt")
|
||||
record(@thread, :author_email, str, "author", "email")
|
||||
record(@thread, :author_name, str, "author", "name")
|
||||
record(@thread, :author_username, str, "author", "username")
|
||||
record(@thread, :author_anonymous, str, "author", "isAnonymous")
|
||||
end
|
||||
|
||||
def cdata_block(str)
|
||||
record(@post, :cooked, str, 'message')
|
||||
record(@post, :cooked, str, "message")
|
||||
end
|
||||
|
||||
def record(target, sym, str, *params)
|
||||
@ -205,7 +197,7 @@ class DisqusSAX < Nokogiri::XML::SAX::Document
|
||||
# Remove any threads that have no posts
|
||||
@threads.delete(id)
|
||||
else
|
||||
t[:posts].delete_if { |p| p[:is_spam] == 'true' || p[:is_deleted] == 'true' }
|
||||
t[:posts].delete_if { |p| p[:is_spam] == "true" || p[:is_deleted] == "true" }
|
||||
end
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user