1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
24 using System.Globalization;
27 using System.Text.RegularExpressions;
28 using OpenTween.Api.DataModel;
33 /// ツイートの Entity 情報をもとにリンク化などを施すクラス
35 public static class TweetFormatter
37 public static string AutoLinkHtml(string text, IEnumerable<TwitterEntity> entities, bool keepTco = false)
40 entities = Enumerable.Empty<TwitterEntity>();
42 var entitiesQuery = entities
43 .Where(x => x != null)
44 .Where(x => x.Indices != null && x.Indices.Length == 2);
46 return string.Concat(AutoLinkHtmlInternal(text, entitiesQuery, keepTco));
49 private static IEnumerable<string> AutoLinkHtmlInternal(string text, IEnumerable<TwitterEntity> entities, bool keepTco)
53 foreach (var entity in FixEntityIndices(text, entities))
55 var startIndex = entity.Indices[0];
56 var endIndex = entity.Indices[1];
58 if (curIndex > startIndex)
59 continue; // 区間が重複する不正なエンティティを無視する
61 if (startIndex > endIndex)
62 continue; // 区間が不正なエンティティを無視する
64 if (startIndex > text.Length || endIndex > text.Length)
65 continue; // 区間が文字列長を越えている不正なエンティティを無視する
67 if (curIndex != startIndex)
68 yield return t(e(text.Substring(curIndex, startIndex - curIndex)));
70 var targetText = text.Substring(startIndex, endIndex - startIndex);
72 if (entity is TwitterEntityUrl urlEntity)
73 yield return FormatUrlEntity(targetText, urlEntity, keepTco);
74 else if (entity is TwitterEntityHashtag hashtagEntity)
75 yield return FormatHashtagEntity(targetText, hashtagEntity);
76 else if (entity is TwitterEntityMention mentionEntity)
77 yield return FormatMentionEntity(targetText, mentionEntity);
79 yield return t(e(targetText));
84 if (curIndex != text.Length)
85 yield return t(e(text.Substring(curIndex)));
89 /// エンティティの Indices をサロゲートペアを考慮して調整します
91 private static IEnumerable<TwitterEntity> FixEntityIndices(string text, IEnumerable<TwitterEntity> entities)
94 var indexOffset = 0; // サロゲートペアによる indices のズレを表す
96 foreach (var entity in entities.OrderBy(x => x.Indices[0]))
98 var startIndex = entity.Indices[0];
99 var endIndex = entity.Indices[1];
101 for (var i = curIndex; i < (startIndex + indexOffset); i++)
102 if (i + 1 < text.Length && char.IsSurrogatePair(text[i], text[i + 1]))
105 startIndex += indexOffset;
106 curIndex = startIndex;
108 for (var i = curIndex; i < (endIndex + indexOffset); i++)
109 if (i + 1 < text.Length && char.IsSurrogatePair(text[i], text[i + 1]))
112 endIndex += indexOffset;
115 entity.Indices[0] = startIndex;
116 entity.Indices[1] = endIndex;
122 private static string FormatUrlEntity(string targetText, TwitterEntityUrl entity, bool keepTco)
126 // 過去に存在した壊れたエンティティの対策
127 // 参照: https://dev.twitter.com/discussions/12628
128 if (entity.DisplayUrl == null)
130 expandedUrl = MyCommon.ConvertToReadableUrl(targetText);
131 return "<a href=\"" + e(entity.Url) + "\" title=\"" + e(expandedUrl) + "\">" + t(e(targetText)) + "</a>";
134 var linkUrl = entity.Url;
136 expandedUrl = keepTco ? linkUrl : MyCommon.ConvertToReadableUrl(entity.ExpandedUrl);
138 var mediaEntity = entity as TwitterEntityMedia;
140 var titleText = mediaEntity?.AltText ?? expandedUrl;
142 // twitter.com へのリンクは t.co を経由せずに直接リンクする (但し pic.twitter.com はそのまま)
143 if (mediaEntity == null)
145 if (entity.ExpandedUrl.StartsWith("https://twitter.com/", StringComparison.Ordinal) ||
146 entity.ExpandedUrl.StartsWith("http://twitter.com/", StringComparison.Ordinal))
148 linkUrl = entity.ExpandedUrl;
152 return "<a href=\"" + e(linkUrl) + "\" title=\"" + e(titleText) + "\">" + t(e(entity.DisplayUrl)) + "</a>";
155 private static string FormatHashtagEntity(string targetText, TwitterEntityHashtag entity)
157 return "<a class=\"hashtag\" href=\"https://twitter.com/search?q=%23" + eu(entity.Text) + "\">" + t(e(targetText)) + "</a>";
160 private static string FormatMentionEntity(string targetText, TwitterEntityMention entity)
162 return "<a class=\"mention\" href=\"https://twitter.com/" + eu(entity.ScreenName) + "\">" + t(e(targetText)) + "</a>";
165 // 長いのでエイリアスとして e(...), eu(...), t(...) でエスケープできるようにする
166 private static Func<string, string> e = EscapeHtml;
167 private static Func<string, string> eu = Uri.EscapeDataString;
168 private static Func<string, string> t = FilterText;
170 private static string EscapeHtml(string text)
172 // Twitter API は "<" ">" "&" だけ中途半端にエスケープした状態のテキストを返すため、
173 // これらの文字だけ一旦エスケープを解除する
174 text = text.Replace("<", "<").Replace(">", ">").Replace("&", "&");
176 var result = new StringBuilder(100);
177 foreach (var c in text)
179 // 「<」「>」「&」「"」「'」についてエスケープ処理を施す
180 // 参照: http://d.hatena.ne.jp/ockeghem/20070510/1178813849
184 result.Append("<");
187 result.Append(">");
190 result.Append("&");
193 result.Append(""");
196 result.Append("'");
204 return result.ToString();
208 /// HTML の属性値ではない、通常のテキストに対するフィルタ処理
210 private static string FilterText(string text)
212 text = text.Replace("\n", "<br>");
213 text = Regex.Replace(text, " ", " ");