Debian Codegraph @

plugin/mastodon/parser.rb

raw symbols SCIP
1 require 'cgi' # unescapeHTML
2
3 module Plugin::Mastodon::Parser
4 def self.dehtmlize(html)
5 result = html
6 .gsub(%r!</p><p>!) { "\n\n" }
7 .gsub(%r!<span class="ellipsis">([^<]*)</span>!) {|s| $1 + "..." }
8 .gsub(%r!^<p>|</p>|<span class="invisible">[^<]*</span>|</?span[^>]*>!, '')
9 .gsub(/<br[^>]*>|<p>/) { "\n" }
10 .gsub(/&apos;/) { "'" }
11 result
12 end
13
14 def self.dictate_score(html, mentions: [], emojis: [], media_attachments: [], poll: nil)
15 desc = dehtmlize(html)
16
17 score = []
18
19 pos = 0
20 anchor_re = %r|<a(?<attr1>[^>]*) href="(?<url>[^"]*)"(?<attr2>[^>]*)>(?<text>[^<]*)</a>|
21 appeared_urls = Set.new
22
23 while m = anchor_re.match(desc, pos)
24 anchor_begin = m.begin(0)
25 anchor_end = m.end(0)
26 if pos < anchor_begin
27 score << Plugin::Score::TextNote.new(description: CGI.unescapeHTML(desc[pos...anchor_begin]))
28 end
29 url = Diva::URI.new(CGI.unescapeHTML(m["url"]))
30 if m["text"][0] == '#' || (score.last.to_s[-1] == '#')
31 score << Plugin::Mastodon::Tag.new(name: CGI.unescapeHTML(m["text"]).sub(/\A#/, ''))
32 else
33 account = nil
34 if mentions.any? { |mention| mention.url == url }
35 mention = mentions.lazy.select { |mention| mention.url == url }.first
36 acct = Plugin::Mastodon::Account.regularize_acct_by_domain(mention.url.host, mention.acct)
37 account = Plugin::Mastodon::Account.findbyacct(acct)
38 end
39 if account
40 score << account
41 else
42 link_hash = {
43 description: CGI.unescapeHTML(m["text"]),
44 uri: url,
45 mastodon_link_attr: Hash.new,
46 }
47 attrs = m["attr1"] + m["attr2"]
48 attr_pos = 0
49 attr_re = %r| (?<name>[^=]+)="(?<value>[^"]*)"|
50 while m2 = attr_re.match(attrs, attr_pos)
51 attr_name = m2["name"].to_sym
52 attr_value = m2["value"]
53 if [:class, :rel].include? attr_name
54 link_hash[:mastodon_link_attr][attr_name] = attr_value.split(' ')
55 else
56 link_hash[:mastodon_link_attr][attr_name] = attr_value
57 end
58 attr_pos = m2.end(0)
59 end
60 score << Plugin::Score::HyperLinkNote.new(link_hash)
61 end
62 end
63 appeared_urls << url
64 pos = anchor_end
65 end
66 if pos < desc.size
67 score << Plugin::Score::TextNote.new(description: CGI.unescapeHTML(desc[pos...desc.size]))
68 end
69
70 # 添付ファイル用のwork around
71 # TODO: mikutter本体側が添付ファイル用のNoteを用意したらそちらに移行する
72 media_attachments.reject { |a|
73 appeared_urls.include?(a.url.to_s) || appeared_urls.include?(a.text_url.to_s)
74 }.each do |attachment|
75 url = attachment.url
76 score <<
77 Plugin::Score::TextNote.new(description: "\n") <<
78 Plugin::Score::HyperLinkNote.new(
79 description: attachment.text_url || url,
80 uri: url,
81 reference: Plugin.collect(:photo_filter, url).first
82 )
83 end
84
85 if poll
86 y = []
87 poll.options.each do |opt|
88 y << '○ %{title}' % {title: opt.title}
89 end
90 y << '%{count}票' % {count: poll.votes_count}
91 if poll.expires_at
92 y << '%{expire}に終了' % {expire: poll.expires_at.strftime("%Y-%m-%d %H:%M:%S")}
93 end
94 score << Plugin::Score::TextNote.new(description: "\n" + y.join("\n"))
95 end
96
97 score = score.flat_map do |note|
98 if !note.is_a?(Plugin::Score::TextNote)
99 [note]
100 else
101 emoji_score = Enumerator.new{|y|
102 dictate_emoji(note.description, emojis, y)
103 }.first.to_a
104 if emoji_score.size > 0
105 emoji_score
106 else
107 [note]
108 end
109 end
110 end
111
112 description = score.inject('') do |acc, note|
113 desc = note.is_a?(Plugin::Score::HyperLinkNote) ? note.uri.to_s : note.description
114 acc + desc
115 end
116
117 [description, score]
118 end
119
120 # 与えられたテキスト断片に対し、emojisでEmojiを置換するscoreを返します。
121 def self.dictate_emoji(text, emojis, yielder)
122 score = emojis.inject(Array(text)){ |fragments, emoji|
123 shortcode = ":#{emoji.shortcode}:"
124 fragments.flat_map{|fragment|
125 if fragment.is_a?(String)
126 if fragment === shortcode
127 [emoji]
128 else
129 sub_fragments = fragment.split(shortcode).flat_map{|str|
130 [str, emoji]
131 }
132 sub_fragments.pop unless fragment.end_with?(shortcode)
133 sub_fragments
134 end
135 else
136 [fragment]
137 end
138 }
139 }.map{|chunk|
140 if chunk.is_a?(String)
141 Plugin::Score::TextNote.new(description: chunk)
142 else
143 chunk
144 end
145 }
146
147 if (score.size > 1 || score.size == 1 && !score[0].is_a?(Plugin::Score::TextNote))
148 yielder << score
149 end
150 end
151 end