Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
#211 Add urlencoded_filename token in custom URL provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Ducatel committed Jun 6, 2019
1 parent 3ef11e0 commit efe8c86
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/GitLink/GitLink.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
<ItemGroup>
<Content Include="Logo.ico" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Web" />
</ItemGroup>
</Project>
14 changes: 11 additions & 3 deletions src/GitLink/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace GitLink
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using Catel;
using Catel.Logging;
using GitTools;
Expand All @@ -28,6 +29,7 @@ public static class Linker
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

private static readonly string FilenamePlaceholder = Uri.EscapeUriString("{filename}");
private static readonly string UrlEncodedFileNamePlaceHolder = Uri.EscapeUriString("{urlencoded_filename}");
private static readonly string RevisionPlaceholder = Uri.EscapeUriString("{revision}");
private static readonly string PdbStrExePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "pdbstr.exe");
private static readonly string[] ExtensionsToIgnore = new string[] { ".g.cs" };
Expand Down Expand Up @@ -170,17 +172,18 @@ public static class Linker
}

string rawUrl = provider.RawGitUrl;
if (rawUrl.Contains(RevisionPlaceholder) || rawUrl.Contains(FilenamePlaceholder))
if (rawUrl.Contains(RevisionPlaceholder) || rawUrl.Contains(FilenamePlaceholder) || rawUrl.Contains(UrlEncodedFileNamePlaceHolder))
{
if (!rawUrl.Contains(RevisionPlaceholder) || !rawUrl.Contains(FilenamePlaceholder))
if (!rawUrl.Contains(RevisionPlaceholder) || !(rawUrl.Contains(FilenamePlaceholder) || rawUrl.Contains(UrlEncodedFileNamePlaceHolder)))
{
Log.Error("Supplied custom URL pattern must contain both a revision and a filename placeholder.");
return false;
}

rawUrl = rawUrl
.Replace(RevisionPlaceholder, "{0}")
.Replace(FilenamePlaceholder, "%var2%");
.Replace(FilenamePlaceholder, "%var2%")
.Replace(UrlEncodedFileNamePlaceHolder, "%var2%");
}
else
{
Expand Down Expand Up @@ -217,6 +220,11 @@ public static class Linker
if (sourceFile.Value != null)
{
var relativePathForUrl = ReplaceSlashes(provider, sourceFile.Value);
if (provider.RawGitUrl.Contains(UrlEncodedFileNamePlaceHolder))
{
relativePathForUrl = HttpUtility.UrlEncode(relativePathForUrl);
}

srcSrvContext.Paths.Add(Tuple.Create(sourceFile.Key, relativePathForUrl));
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/GitLink/Providers/CustomUrlProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace GitLink.Providers
public sealed class CustomUrlProvider : ProviderBase
{
private const string FileNamePlaceHolder = "{filename}";
private const string UrlEncodedFileNamePlaceHolder = "{urlencoded_filename}";
private const string RevisionPlaceHolder = "{revision}";
private static readonly Regex HostingUrlPattern = new Regex(@"https?://.+");

Expand All @@ -27,14 +28,16 @@ public CustomUrlProvider()
public override bool Initialize(string url)
{
if (string.IsNullOrEmpty(url) || !HostingUrlPattern.IsMatch(url) ||
(!url.Contains(FileNamePlaceHolder) && !url.Contains(RevisionPlaceHolder)))
(!(url.Contains(FileNamePlaceHolder) || url.Contains(UrlEncodedFileNamePlaceHolder)) &&
!url.Contains(RevisionPlaceHolder)))
{
return false;
}

if (url.Contains(FileNamePlaceHolder))
if (url.Contains(FileNamePlaceHolder) || url.Contains(UrlEncodedFileNamePlaceHolder))
{
_rawUrl = url.Replace(FileNamePlaceHolder, "%var2%");
_rawUrl = url.Replace(FileNamePlaceHolder, "%var2%")
.Replace(UrlEncodedFileNamePlaceHolder, "%var2%");
}

if (url.Contains(RevisionPlaceHolder))
Expand Down

0 comments on commit efe8c86

Please sign in to comment.