Merge commit 'f378f10404' into kb_migration

This commit is contained in:
KMY 2023-06-07 11:45:52 +09:00
commit edb2a5dcf3
153 changed files with 1225 additions and 1025 deletions

View file

@ -7,15 +7,15 @@ class LinkDetailsExtractor
# Some publications wrap their JSON-LD data in their <script> tags
# in commented-out CDATA blocks, they need to be removed before
# attempting to parse JSON
CDATA_JUNK_PATTERN = %r{^[\s]*(
(/\*[\s]*<!\[CDATA\[[\s]*\*/) # Block comment style opening
CDATA_JUNK_PATTERN = %r{^\s*(
(/\*\s*<!\[CDATA\[\s*\*/) # Block comment style opening
|
(//[\s]*<!\[CDATA\[) # Single-line comment style opening
(//\s*<!\[CDATA\[) # Single-line comment style opening
|
(/\*[\s]*\]\]>[\s]*\*/) # Block comment style closing
(/\*\s*\]\]>\s*\*/) # Block comment style closing
|
(//[\s]*\]\]>) # Single-line comment style closing
)[\s]*$}x
(//\s*\]\]>) # Single-line comment style closing
)\s*$}x
class StructuredData
SUPPORTED_TYPES = %w(
@ -204,7 +204,7 @@ class LinkDetailsExtractor
def host_to_url(str)
return if str.blank?
str.start_with?(/https?:\/\//) ? str : "http://#{str}"
str.start_with?(%r{https?://}) ? str : "http://#{str}"
end
def valid_url_or_nil(str, same_origin_only: false)

View file

@ -3,7 +3,7 @@
class PlainTextFormatter
include ActionView::Helpers::TextHelper
NEWLINE_TAGS_RE = /(<br \/>|<br>|<\/p>)+/
NEWLINE_TAGS_RE = %r{(<br />|<br>|</p>)+}
attr_reader :text, :local

View file

@ -7,18 +7,18 @@ class TagManager
include RoutingHelper
def web_domain?(domain)
domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.web_domain).zero?
domain.nil? || domain.delete('/').casecmp(Rails.configuration.x.web_domain).zero?
end
def local_domain?(domain)
domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.local_domain).zero?
domain.nil? || domain.delete('/').casecmp(Rails.configuration.x.local_domain).zero?
end
def normalize_domain(domain)
return if domain.nil?
uri = Addressable::URI.new
uri.host = domain.gsub(/[\/]/, '')
uri.host = domain.delete('/')
uri.normalized_host
end

View file

@ -5,7 +5,7 @@ class TextFormatter
include ERB::Util
include RoutingHelper
URL_PREFIX_REGEX = /\A(https?:\/\/(www\.)?|xmpp:)/
URL_PREFIX_REGEX = %r{\A(https?://(www\.)?|xmpp:)}
DEFAULT_REL = %w(nofollow noopener noreferrer).freeze

View file

@ -13,7 +13,7 @@ class WebfingerResource
case resource
when /\Ahttps?/i
username_from_url
when /\@/
when /@/
username_from_acct
else
raise InvalidRequest

View file

@ -0,0 +1,67 @@
# frozen_string_literal: true
class Webhooks::PayloadRenderer
class DocumentTraverser
INT_REGEX = /[0-9]+/
def initialize(document)
@document = document.with_indifferent_access
end
def get(path)
value = @document.dig(*parse_path(path))
string = Oj.dump(value)
# We want to make sure people can use the variable inside
# other strings, so it can't be wrapped in quotes.
if value.is_a?(String)
string[1...-1]
else
string
end
end
private
def parse_path(path)
path.split('.').filter_map do |segment|
if segment.match(INT_REGEX)
segment.to_i
else
segment.presence
end
end
end
end
class TemplateParser < Parslet::Parser
rule(:dot) { str('.') }
rule(:digit) { match('[0-9]') }
rule(:property_name) { match('[a-z_]').repeat(1) }
rule(:array_index) { digit.repeat(1) }
rule(:segment) { (property_name | array_index) }
rule(:path) { property_name >> (dot >> segment).repeat }
rule(:variable) { (str('}}').absent? >> path).repeat.as(:variable) }
rule(:expression) { str('{{') >> variable >> str('}}') }
rule(:text) { (str('{{').absent? >> any).repeat(1) }
rule(:text_with_expressions) { (text.as(:text) | expression).repeat.as(:text) }
root(:text_with_expressions)
end
EXPRESSION_REGEXP = /
\{\{
[a-z_]+
(\.
([a-z_]+|[0-9]+)
)*
\}\}
/iox
def initialize(json)
@document = DocumentTraverser.new(Oj.load(json))
end
def render(template)
template.gsub(EXPRESSION_REGEXP) { |match| @document.get(match[2...-2]) }
end
end