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;
32 /// ツイートの Entity 情報をもとにリンク化などを施すクラス
34 public class TweetFormatter
36 public static string AutoLinkHtml(string text, TwitterEntities entities)
39 return AutoLinkHtml(text, Enumerable.Empty<TwitterEntity>());
41 var urls = entities.Urls ?? new TwitterEntityUrl[0];
42 var hashtags = entities.Hashtags ?? new TwitterEntityHashtag[0];
43 var mentions = entities.UserMentions ?? new TwitterEntityMention[0];
44 var media = entities.Media ?? new TwitterEntityMedia[0];
46 var entitiesQuery = urls.Cast<TwitterEntity>()
50 .Where(x => x != null)
51 .Where(x => x.Indices != null && x.Indices.Length == 2);
53 return string.Concat(AutoLinkHtmlInternal(text, entitiesQuery));
56 public static string AutoLinkHtml(string text, IEnumerable<TwitterEntity> entities)
59 entities = Enumerable.Empty<TwitterEntity>();
61 var entitiesQuery = entities
62 .Where(x => x != null)
63 .Where(x => x.Indices != null && x.Indices.Length == 2);
65 return string.Concat(AutoLinkHtmlInternal(text, entitiesQuery));
68 private static IEnumerable<string> AutoLinkHtmlInternal(string text, IEnumerable<TwitterEntity> entities)
72 foreach (var entity in FixEntityIndices(text, entities))
74 var startIndex = entity.Indices[0];
75 var endIndex = entity.Indices[1];
77 if (curIndex > startIndex)
78 continue; // 区間が重複する不正なエンティティを無視する
80 if (startIndex > endIndex)
81 continue; // 区間が不正なエンティティを無視する
83 if (startIndex > text.Length || endIndex > text.Length)
84 continue; // 区間が文字列長を越えている不正なエンティティを無視する
86 if (curIndex != startIndex)
87 yield return t(e(text.Substring(curIndex, startIndex - curIndex)));
89 var targetText = text.Substring(startIndex, endIndex - startIndex);
91 if (entity is TwitterEntityUrl)
92 yield return FormatUrlEntity(targetText, (TwitterEntityUrl)entity);
93 else if (entity is TwitterEntityHashtag)
94 yield return FormatHashtagEntity(targetText, (TwitterEntityHashtag)entity);
95 else if (entity is TwitterEntityMention)
96 yield return FormatMentionEntity(targetText, (TwitterEntityMention)entity);
98 yield return t(e(targetText));
103 if (curIndex != text.Length)
104 yield return t(e(text.Substring(curIndex)));
108 /// エンティティの Indices をサロゲートペアを考慮して調整します
110 private static IEnumerable<TwitterEntity> FixEntityIndices(string text, IEnumerable<TwitterEntity> entities)
113 var indexOffset = 0; // サロゲートペアによる indices のズレを表す
115 foreach (var entity in entities.OrderBy(x => x.Indices[0]))
117 var startIndex = entity.Indices[0];
118 var endIndex = entity.Indices[1];
120 for (var i = curIndex; i < (startIndex + indexOffset); i++)
121 if (i + 1 < text.Length && char.IsSurrogatePair(text[i], text[i + 1]))
124 startIndex += indexOffset;
125 curIndex = startIndex;
127 for (var i = curIndex; i < (endIndex + indexOffset); i++)
128 if (i + 1 < text.Length && char.IsSurrogatePair(text[i], text[i + 1]))
131 endIndex += indexOffset;
134 entity.Indices[0] = startIndex;
135 entity.Indices[1] = endIndex;
141 private static string FormatUrlEntity(string targetText, TwitterEntityUrl entity)
145 // 過去に存在した壊れたエンティティの対策
146 // 参照: https://dev.twitter.com/discussions/12628
147 if (entity.DisplayUrl == null)
149 expandedUrl = MyCommon.ConvertToReadableUrl(targetText);
150 return "<a href=\"" + e(entity.Url) + "\" title=\"" + e(expandedUrl) + "\">" + t(e(targetText)) + "</a>";
153 expandedUrl = MyCommon.ConvertToReadableUrl(entity.ExpandedUrl);
154 return "<a href=\"" + e(entity.Url) + "\" title=\"" + e(expandedUrl) + "\">" + t(e(entity.DisplayUrl)) + "</a>";
157 private static string FormatHashtagEntity(string targetText, TwitterEntityHashtag entity)
159 return "<a class=\"hashtag\" href=\"https://twitter.com/search?q=%23" + eu(entity.Text) + "\">" + t(e(targetText)) + "</a>";
162 private static string FormatMentionEntity(string targetText, TwitterEntityMention entity)
164 return "<a class=\"mention\" href=\"https://twitter.com/" + eu(entity.ScreenName) + "\">" + t(e(targetText)) + "</a>";
167 // 長いのでエイリアスとして e(...), eu(...), t(...) でエスケープできるようにする
168 private static Func<string, string> e = EscapeHtml;
169 private static Func<string, string> eu = Uri.EscapeDataString;
170 private static Func<string, string> t = FilterText;
172 private static string EscapeHtml(string text)
174 // Twitter API は "<" ">" "&" だけ中途半端にエスケープした状態のテキストを返すため、
175 // これらの文字だけ一旦エスケープを解除する
176 text = text.Replace("<", "<").Replace(">", ">").Replace("&", "&");
178 var result = new StringBuilder(100);
179 foreach (var c in text)
181 // 「<」「>」「&」「"」「'」についてエスケープ処理を施す
182 // 参照: http://d.hatena.ne.jp/ockeghem/20070510/1178813849
186 result.Append("<");
189 result.Append(">");
192 result.Append("&");
195 result.Append(""");
198 result.Append("'");
206 return result.ToString();
210 /// HTML の属性値ではない、通常のテキストに対するフィルタ処理
212 private static string FilterText(string text)
214 text = text.Replace("\n", "<br>");