Skip to content

Commit

Permalink
GetExtendedTweetElements
Browse files Browse the repository at this point in the history
  • Loading branch information
azyobuzin committed May 29, 2016
1 parent 22657f3 commit b7b9f1c
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 36 deletions.
4 changes: 2 additions & 2 deletions CoreTweetSupplement.Pcl/CoreTweetSupplement.Pcl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
</Compile>
</ItemGroup>
<ItemGroup>
<Reference Include="CoreTweet, Version=0.6.1.267, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CoreTweet.0.6.1.267\lib\portable-net45+dnxcore50+win8+wpa81+MonoAndroid+xamarinios+MonoTouch\CoreTweet.dll</HintPath>
<Reference Include="CoreTweet, Version=0.6.3.296, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CoreTweet.0.6.3.296\lib\portable-net45+dnxcore50+win8+wpa81+MonoAndroid+xamarinios+MonoTouch\CoreTweet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
Expand Down
2 changes: 1 addition & 1 deletion CoreTweetSupplement.Pcl/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CoreTweet" version="0.6.1.267" targetFramework="portable45-net45+win8+wpa81" />
<package id="CoreTweet" version="0.6.3.296" targetFramework="portable45-net45+win8+wpa81" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="portable45-net45+win8+wpa81" />
</packages>
107 changes: 86 additions & 21 deletions CoreTweetSupplement/CoreTweetSupplement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static Source ParseSource(this Status status)
return ParseSource(status.Source);
}

private static List<DoubleUtf16Char> EnumerateChars(string str)
private static List<DoubleUtf16Char> GetCodePoints(string str)
{
var result = new List<DoubleUtf16Char>(str.Length);
for (var i = 0; i < str.Length; i++)
Expand All @@ -116,21 +116,6 @@ private static List<DoubleUtf16Char> EnumerateChars(string str)
return result;
}

private static string ToString(IList<DoubleUtf16Char> source, int start)
{
var sourceLen = source.Count;
var arr = new char[sourceLen * 2];
var strLen = 0;
for (var i = start; i < sourceLen; i++)
{
var x = source[i];
arr[strLen++] = x.X;
if (char.IsHighSurrogate(x.X))
arr[strLen++] = x.Y;
}
return new string(arr, 0, strLen);
}

private static string ToString(IList<DoubleUtf16Char> source, int start, int count)
{
var arr = new char[count * 2];
Expand All @@ -154,8 +139,34 @@ private static string ToString(IList<DoubleUtf16Char> source, int start, int cou
/// <returns>An <see cref="IEnumerable{ITextPart}"/> whose elements are parts of <paramref name="text"/>.</returns>
public static IEnumerable<TextPart> EnumerateTextParts(string text, Entities entities)
{
var chars = GetCodePoints(text);
return EnumerateTextParts(chars, entities, 0, chars.Count);
}

/// <summary>
/// Enumerates parts split into Tweet Entities.
/// </summary>
/// <param name="text">The text such as <see cref="Status.Text"/>, <see cref="DirectMessage.Text"/> and <see cref="User.Description"/>.</param>
/// <param name="entities">The <see cref="Entities"/> instance.</param>
/// <param name="startIndex">The starting character position in code point.</param>
/// <param name="endIndex">The ending character position in code point.</param>
/// <returns>An <see cref="IEnumerable{ITextPart}"/> whose elements are parts of <paramref name="text"/>.</returns>
public static IEnumerable<TextPart> EnumerateTextParts(string text, Entities entities, int startIndex, int endIndex)
{
return EnumerateTextParts(GetCodePoints(text), entities, startIndex, endIndex);
}

private static IEnumerable<TextPart> EnumerateTextParts(IList<DoubleUtf16Char> chars, Entities entities, int startIndex, int endIndex)
{
if (startIndex < 0 || startIndex >= chars.Count)
throw new ArgumentOutOfRangeException(nameof(startIndex));

if (endIndex < startIndex || endIndex > chars.Count)
throw new ArgumentOutOfRangeException(nameof(endIndex));

if (entities == null)
{
var text = ToString(chars, startIndex, endIndex - startIndex);
yield return new TextPart()
{
RawText = text,
Expand Down Expand Up @@ -202,7 +213,7 @@ public static IEnumerable<TextPart> EnumerateTextParts(string text, Entities ent
Type = TextPartType.Url,
Start = e.Indices[0],
End = e.Indices[1],
RawText = e.Url.ToString(),
RawText = e.Url,
Text = e.DisplayUrl,
Entity = e
})
Expand All @@ -219,11 +230,13 @@ public static IEnumerable<TextPart> EnumerateTextParts(string text, Entities ent
Entity = e
})
)
.Where(e => e.Start >= startIndex && e.Start < endIndex)
.OrderBy(part => part.Start)
);

if (list.Count == 0)
{
var text = ToString(chars, startIndex, endIndex - startIndex);
yield return new TextPart()
{
RawText = text,
Expand All @@ -233,11 +246,10 @@ public static IEnumerable<TextPart> EnumerateTextParts(string text, Entities ent
}

var current = list.First;
var chars = EnumerateChars(text);

while (true)
{
var start = current.Previous?.Value.End ?? 0;
var start = current.Previous?.Value.End ?? startIndex;
var count = current.Value.Start - start;
if (count > 0)
{
Expand All @@ -256,9 +268,9 @@ public static IEnumerable<TextPart> EnumerateTextParts(string text, Entities ent
}

var lastStart = current.Value.End;
if (lastStart < chars.Count)
if (lastStart < endIndex)
{
var lastOutput = ToString(chars, lastStart);
var lastOutput = ToString(chars, lastStart, endIndex - lastStart);
yield return new TextPart()
{
RawText = lastOutput,
Expand Down Expand Up @@ -287,6 +299,38 @@ public static IEnumerable<TextPart> EnumerateTextParts(this DirectMessage dm)
return EnumerateTextParts(dm.Text, dm.Entities);
}

/// <summary>
/// Enumerates parts split into Tweet Entities.
/// </summary>
/// <param name="status">The <see cref="Status"/> instance.</param>
/// <returns>An <see cref="ExtendedTweetInfo"/> instance.</returns>
public static ExtendedTweetInfo GetExtendedTweetElements(this Status status)
{
var displayTextRange = status.DisplayTextRange ?? status.ExtendedTweet?.DisplayTextRange;
if (displayTextRange == null)
return new ExtendedTweetInfo
{
TweetText = status.EnumerateTextParts().ToArray(),
HiddenPrefix = new UserMentionEntity[0],
HiddenSuffix = new UrlEntity[0]
};

var start = displayTextRange[0];
var end = displayTextRange[1];
return new ExtendedTweetInfo
{
TweetText = EnumerateTextParts(
status.FullText ?? status.ExtendedTweet?.FullText ?? status.Text,
status.Entities,
start, end).ToArray(),
HiddenPrefix = status.Entities?.UserMentions == null ? new UserMentionEntity[0]
: status.Entities.UserMentions.Where(x => x.Indices[0] < start).ToArray(),
HiddenSuffix = (status.Entities?.Urls ?? Enumerable.Empty<UrlEntity>())
.Concat(status.Entities?.Media ?? Enumerable.Empty<UrlEntity>())
.Where(x => x.Indices[0] >= end).ToArray()
};
}

/// <summary>
/// Returns the URI suffix for given profile size image variant. See User Profile Images and Banners.
/// </summary>
Expand Down Expand Up @@ -443,4 +487,25 @@ public DoubleUtf16Char(char x, char y)
this.Y = y;
}
}

/// <summary>
/// Represents a Tweet rendered in Extended mode.
/// </summary>
public class ExtendedTweetInfo
{
/// <summary>
/// The elements of Tweet Text part.
/// </summary>
public TextPart[] TweetText { get; set; }

/// <summary>
/// Replies.
/// </summary>
public UserMentionEntity[] HiddenPrefix { get; set; }

/// <summary>
/// Attachment URLs.
/// </summary>
public UrlEntity[] HiddenSuffix { get; set; }
}
}
4 changes: 2 additions & 2 deletions CoreTweetSupplement/CoreTweetSupplement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="CoreTweet, Version=0.6.1.267, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CoreTweet.0.6.1.267\lib\net40\CoreTweet.dll</HintPath>
<Reference Include="CoreTweet, Version=0.6.3.296, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CoreTweet.0.6.3.296\lib\net40\CoreTweet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
6 changes: 3 additions & 3 deletions CoreTweetSupplement/CoreTweetSupplement.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
<copyright>(c) 2014-2016 CoreTweet Development Team</copyright>
<tags>Twitter</tags>
<dependencies>
<dependency id="CoreTweet" version="0.6.1.267" />
<dependency id="CoreTweet" version="0.6.3.296" />
</dependencies>
</metadata>
<files>
<file src="bin\Release\CoreTweetSupplement.dll" target="lib\net40\CoreTweetSupplement.dll" />
<file src="bin\Release\CoreTweetSupplement.xml" target="lib\net40\CoreTweetSupplement.xml" />
<file src="..\CoreTweetSupplement.Pcl\bin\Release\CoreTweetSupplement.dll" target="lib\portable-net4+wp8+win81+wpa81+MonoAndroid+MonoTouch\CoreTweetSupplement.dll" />
<file src="..\CoreTweetSupplement.Pcl\bin\Release\CoreTweetSupplement.xml" target="lib\portable-net4+wp8+win81+wpa81+MonoAndroid+MonoTouch\CoreTweetSupplement.xml" />
<file src="..\CoreTweetSupplement.Pcl\bin\Release\CoreTweetSupplement.dll" target="lib\portable-net45+dnxcore50+win8+wpa81+MonoAndroid+xamarinios+MonoTouch\CoreTweetSupplement.dll" />
<file src="..\CoreTweetSupplement.Pcl\bin\Release\CoreTweetSupplement.xml" target="lib\portable-net45+dnxcore50+win8+wpa81+MonoAndroid+xamarinios+MonoTouch\CoreTweetSupplement.xml" />
</files>
</package>
4 changes: 2 additions & 2 deletions CoreTweetSupplement/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]

[assembly: AssemblyVersion("1.5.0.0")]
[assembly: AssemblyFileVersion("1.5.0.0")]
[assembly: AssemblyVersion("1.6.0.0")]
[assembly: AssemblyFileVersion("1.6.0.0")]
2 changes: 1 addition & 1 deletion CoreTweetSupplement/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CoreTweet" version="0.6.1.267" targetFramework="net40" />
<package id="CoreTweet" version="0.6.3.296" targetFramework="net40" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net4" />
</packages>
4 changes: 2 additions & 2 deletions CoreTweetSupplementTest/CoreTweetSupplementTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="CoreTweet, Version=0.6.1.267, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CoreTweet.0.6.1.267\lib\net45\CoreTweet.dll</HintPath>
<Reference Include="CoreTweet, Version=0.6.3.296, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CoreTweet.0.6.3.296\lib\net45\CoreTweet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
Expand Down
2 changes: 1 addition & 1 deletion CoreTweetSupplementTest/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ChainingAssertion" version="1.7.1.0" targetFramework="net45" />
<package id="CoreTweet" version="0.6.1.267" targetFramework="net45" />
<package id="CoreTweet" version="0.6.3.296" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
</packages>
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Enumerates order sorted and HTML-decoded parts of the text.

Example: [TestEnumerateTextParts](https://github.com/azyobuzin/CoreTweetSupplement/blob/f695b971fb2415180b7091bbc0b78280bda5e7ff/CoreTweetSupplementTest/CoreTweetSupplementTest.cs#L53-L332)

## GetExtendedTweetElements(this Status) ##
A variation of EnumerateTextParts which supports the new structure of Tweets.

## GetProfileImageUrl / GetProfileImageUrlHttps(this User, string) ##
This is a method to get a URL pointing to the user's avatar image with given size.

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.5.0.{build}
version: 1.6.0.{build}

os: Visual Studio 2015

Expand Down

0 comments on commit b7b9f1c

Please sign in to comment.