mirror of https://github.com/iv-org/invidious.git
Videos: fix description text offset when emojis are present
This commit is contained in:
parent
73d2ed6f77
commit
b3c0afef02
|
@ -46,37 +46,60 @@ def parse_command(command : JSON::Any?, string : String) : String?
|
||||||
return "(unknown YouTube desc command)"
|
return "(unknown YouTube desc command)"
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_description(desc : JSON::Any?) : String?
|
private def copy_string(str : String::Builder, iter : Iterator, count : Int) : Int
|
||||||
if desc.nil?
|
copied = 0
|
||||||
return ""
|
while copied < count
|
||||||
|
cp = iter.next
|
||||||
|
break if cp.is_a?(Iterator::Stop)
|
||||||
|
|
||||||
|
str << cp.chr
|
||||||
|
|
||||||
|
# A codepoint from the SMP counts twice
|
||||||
|
copied += 1 if cp > 0xFFFF
|
||||||
|
copied += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return copied
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_description(desc : JSON::Any?) : String?
|
||||||
|
return "" if desc.nil?
|
||||||
|
|
||||||
content = desc["content"].as_s
|
content = desc["content"].as_s
|
||||||
if content.empty?
|
return "" if content.empty?
|
||||||
return ""
|
|
||||||
end
|
commands = desc["commandRuns"]?.try &.as_a
|
||||||
|
return content if commands.nil?
|
||||||
|
|
||||||
|
# Not everything is stored in UTF-8 on youtube's side. The SMP codepoints
|
||||||
|
# (0x10000 and above) are encoded as UTF-16 surrogate pairs, which are
|
||||||
|
# automatically decoded by the JSON parser. It means that we need to count
|
||||||
|
# copied byte in a special manner, preventing the use of regular string copy.
|
||||||
|
iter = content.each_codepoint
|
||||||
|
|
||||||
if commands = desc["commandRuns"]?.try &.as_a
|
|
||||||
description = String.build do |str|
|
|
||||||
index = 0
|
index = 0
|
||||||
|
|
||||||
|
return String.build do |str|
|
||||||
commands.each do |command|
|
commands.each do |command|
|
||||||
start_index = command["startIndex"].as_i
|
cmd_start = command["startIndex"].as_i
|
||||||
length = command["length"].as_i
|
cmd_length = command["length"].as_i
|
||||||
|
|
||||||
if start_index > 0 && start_index - index > 0
|
# Copy the text chunk between this command and the previous if needed.
|
||||||
str << content[index...start_index]
|
length = cmd_start - index
|
||||||
index = start_index
|
index += copy_string(str, iter, length)
|
||||||
|
|
||||||
|
# We need to copy the command's text using the iterator
|
||||||
|
# and the special function defined above.
|
||||||
|
cmd_content = String.build(cmd_length) do |str2|
|
||||||
|
copy_string(str2, iter, cmd_length)
|
||||||
end
|
end
|
||||||
|
|
||||||
str << parse_command(command, content[start_index, length])
|
str << parse_command(command, cmd_content)
|
||||||
index += length
|
index += cmd_length
|
||||||
end
|
|
||||||
if index < content.size
|
|
||||||
str << content[index..content.size]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return description
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return content
|
# Copy the end of the string (past the last command).
|
||||||
|
remaining_length = content.size - index
|
||||||
|
copy_string(str, iter, remaining_length) if remaining_length > 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue