Skip to content

Commit

Permalink
Add Target, TargetUrl & DisplayText properties to WikiLink.
Browse files Browse the repository at this point in the history
  • Loading branch information
CXuesong committed Aug 29, 2018
1 parent 0553f62 commit eaeb772
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
12 changes: 10 additions & 2 deletions UnitTestProject1/Tests/WikiLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,30 @@ public async Task WikiLinkTest1()
var link2 = WikiLink.Parse(WpTestSite, "__ _pROject_ _talk_:___sandbox_", BuiltInNamespaces.Category);
var link3 = WikiLink.Parse(WpTestSite, "___sandbox_ test__", BuiltInNamespaces.Category);
var link4 = WikiLink.Parse(WpTestSite, "__: sandbox test ", BuiltInNamespaces.Template);
var link5 = WikiLink.Parse(WpTestSite, "___lZh__:project:test", BuiltInNamespaces.Template);
var link5 = WikiLink.Parse(WpTestSite, "___lZh__:project:test|", BuiltInNamespaces.Template);
Assert.Equal("Wikipedia talk:Sandbox", link1.ToString());
Assert.Equal("Wikipedia talk", link1.NamespaceName);
Assert.Equal("Sandbox", link1.Title);
Assert.Equal("Wikipedia talk:Sandbox", link1.Target);
Assert.Equal("Wikipedia talk:Sandbox", link1.DisplayText);
Assert.Equal("https://test2.wikipedia.org/wiki/Wikipedia%20talk:Sandbox", link1.TargetUrl);
Assert.Null(link1.InterwikiPrefix);
Assert.Null(link1.Section);
Assert.Null(link1.Anchor);
Assert.Equal("Wikipedia talk:Sandbox", link2.ToString());
Assert.Equal("Category:Sandbox test", link3.ToString());
Assert.Equal("Sandbox test", link4.ToString());
Assert.Equal("lzh:Project:test", link5.ToString());
Assert.Equal("lzh:Project:test|", link5.ToString());
Assert.Equal("Project:test", link5.DisplayText);
Assert.Equal("lzh", link5.InterwikiPrefix);
Assert.Equal("lzh:Project:test", link5.Target);
Assert.Equal("", link5.Anchor);
var link6 = WikiLink.Parse(WpTestSite, "sandbox#sect|anchor", BuiltInNamespaces.Template);
Assert.Equal("Template:Sandbox#sect|anchor", link6.ToString());
Assert.Equal("Template:Sandbox#sect", link6.Target);
Assert.Equal("sect", link6.Section);
Assert.Equal("anchor", link6.Anchor);
Assert.Equal("anchor", link6.DisplayText);
}

[Fact]
Expand Down
82 changes: 77 additions & 5 deletions WikiClientLibrary/WikiLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public static WikiLink TryParse(WikiSite site, string text)

/// <summary>
/// Tries to parse a new instance using specified Wikilink expression.
/// This overload also resolves the target interwiki site with the interwiki map provided by <paramref name="site"/>,
/// and the specified <seealso cref="IWikiFamily"/> implementation.
/// </summary>
/// <param name="site">Site instance.</param>
/// <param name="family">Wiki family. You need to provide this argument if you want to parse into interwiki links.</param>
Expand All @@ -117,6 +119,8 @@ public static Task<WikiLink> TryParseAsync(WikiSite site, IWikiFamily family, st

/// <summary>
/// Tries to parse a new instance using specified Wikilink expression.
/// This overload also resolves the target interwiki site with the interwiki map provided by <paramref name="site"/>,
/// and the specified <seealso cref="IWikiFamily"/> implementation.
/// </summary>
/// <param name="site">Site instance.</param>
/// <param name="family">Wiki family. You need to provide this argument if you want to parse into interwiki links.</param>
Expand Down Expand Up @@ -215,6 +219,7 @@ private static async Task<WikiLink> ParseInternalAsync(WikiSite site, IWikiFamil
sb.Append('#');
sb.Append(link.Section);
}
link.Target = sb.ToString();
if (link.Anchor != null)
{
sb.Append('|');
Expand All @@ -231,7 +236,7 @@ private static async Task<WikiLink> ParseInternalAsync(WikiSite site, IWikiFamil
public WikiSite Site { get; }

/// <summary>
/// The wiki site containing the specified page title. If the parsed wikilink expression
/// Gets the wiki site containing the specified page title. If the parsed wikilink expression
/// does not contain interwiki prefix, this property is the same as <see cref="Site"/>.
/// If this wikilink is parsed with no <see cref="IWikiFamily"/> provided, while it contains inerwiki
/// prefix, this property will be <c>null</c>.
Expand Down Expand Up @@ -355,31 +360,97 @@ 2 Page title
public string Title { get; private set; }

/// <summary>
/// Title, including namespace name, if exists.
/// Title of the page, including namespace name, if exists.
/// </summary>
public string FullTitle { get; private set; }

/// <summary>
/// The section title of a section on the page.
/// The section title of a section on the page, without leading #.
/// </summary>
public string Section { get; private set; }

/// <summary>
/// For wikilink expression in the form [[target|anchor]], excluding the brackets,
/// gets the actual displayed text (anchor) for the link.
/// For wikilink expression in the form <c>[[target|anchor]]</c>, excluding the square brackets,
/// gets the full page title and section title (<c>target</c>) for the link, including interwiki prefix.
/// </summary>
public string Target { get; private set; }

/// <summary>
/// For wikilink expression in the form <c>[[target|anchor]]</c>, excluding the square brackets,
/// gets the actual displayed text (<c>anchor</c>) for the link.
/// </summary>
/// <remarks>For the actual text this wikilink should show, use <see cref="DisplayText"/>.</remarks>
public string Anchor { get; private set; }

private string _DisplayText;

/// <summary>
/// Gets the actual link text that should be shown.
/// </summary>
/// <value>
/// <see cref="Anchor"/>, if the value is not <c>null</c>; otherwise <see cref="Target"/>.
/// if <see cref="Anchor"/> is <see cref="string.Empty"/>,
/// this property returns the text after the first colon in <see cref="Target"/>.
/// See <a href="https://en.wikipedia.org/wiki/Help:Pipe_trick">w:H:Pipe trick</a> for more information.
/// </value>
public string DisplayText
{
get
{
var localValue = _DisplayText;
if (localValue == null)
{
if (Anchor == null)
{
localValue = Target;
}
else if (Anchor.Length == 0)
{
localValue = Target;
var colonPos = localValue.IndexOf(':');
if (colonPos >= 0) localValue = localValue.Substring(colonPos + 1);
}
else
{
localValue = Anchor;
}
_DisplayText = localValue;
}
return localValue;
}
}

/// <summary>
/// Gets the original wikitext expression that was passed to the Parse or ParseAsync methods.
/// </summary>
public string OriginalText { get; }

private string _TargetUrl;

/// <summary>
/// Gets the full URL of the wikilink target.
/// </summary>
/// <remarks>This property uses <see cref="SiteInfo.MakeArticleUrl(string)"/> to build the article URL.</remarks>
public string TargetUrl
{
get
{
var localValue = _TargetUrl;
if (localValue == null)
{
localValue = Site.SiteInfo.MakeArticleUrl(Target);
_TargetUrl = localValue;
}
return localValue;
}
}

private string _FormattedText;

/// <summary>
/// Gets the formatted expression of the wikilink.
/// </summary>
/// <returns>The wikilink expression, excluding the surrounding square brackets [[ ]].</returns>
public override string ToString()
{
return _FormattedText;
Expand Down Expand Up @@ -413,4 +484,5 @@ public static string NormalizeWikiLink(WikiSite site, string text, int defaultNa
return link._FormattedText;
}
}

}

0 comments on commit eaeb772

Please sign in to comment.