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

Commit efe8c86

Browse files
committed
#211 Add urlencoded_filename token in custom URL provider
1 parent 3ef11e0 commit efe8c86

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/GitLink/GitLink.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@
2626
<ItemGroup>
2727
<Content Include="Logo.ico" />
2828
</ItemGroup>
29+
<ItemGroup>
30+
<Reference Include="System.Web" />
31+
</ItemGroup>
2932
</Project>

src/GitLink/Linker.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace GitLink
1212
using System.IO;
1313
using System.Linq;
1414
using System.Reflection;
15+
using System.Web;
1516
using Catel;
1617
using Catel.Logging;
1718
using GitTools;
@@ -28,6 +29,7 @@ public static class Linker
2829
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
2930

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

172174
string rawUrl = provider.RawGitUrl;
173-
if (rawUrl.Contains(RevisionPlaceholder) || rawUrl.Contains(FilenamePlaceholder))
175+
if (rawUrl.Contains(RevisionPlaceholder) || rawUrl.Contains(FilenamePlaceholder) || rawUrl.Contains(UrlEncodedFileNamePlaceHolder))
174176
{
175-
if (!rawUrl.Contains(RevisionPlaceholder) || !rawUrl.Contains(FilenamePlaceholder))
177+
if (!rawUrl.Contains(RevisionPlaceholder) || !(rawUrl.Contains(FilenamePlaceholder) || rawUrl.Contains(UrlEncodedFileNamePlaceHolder)))
176178
{
177179
Log.Error("Supplied custom URL pattern must contain both a revision and a filename placeholder.");
178180
return false;
179181
}
180182

181183
rawUrl = rawUrl
182184
.Replace(RevisionPlaceholder, "{0}")
183-
.Replace(FilenamePlaceholder, "%var2%");
185+
.Replace(FilenamePlaceholder, "%var2%")
186+
.Replace(UrlEncodedFileNamePlaceHolder, "%var2%");
184187
}
185188
else
186189
{
@@ -217,6 +220,11 @@ public static class Linker
217220
if (sourceFile.Value != null)
218221
{
219222
var relativePathForUrl = ReplaceSlashes(provider, sourceFile.Value);
223+
if (provider.RawGitUrl.Contains(UrlEncodedFileNamePlaceHolder))
224+
{
225+
relativePathForUrl = HttpUtility.UrlEncode(relativePathForUrl);
226+
}
227+
220228
srcSrvContext.Paths.Add(Tuple.Create(sourceFile.Key, relativePathForUrl));
221229
}
222230
}

src/GitLink/Providers/CustomUrlProvider.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace GitLink.Providers
1212
public sealed class CustomUrlProvider : ProviderBase
1313
{
1414
private const string FileNamePlaceHolder = "{filename}";
15+
private const string UrlEncodedFileNamePlaceHolder = "{urlencoded_filename}";
1516
private const string RevisionPlaceHolder = "{revision}";
1617
private static readonly Regex HostingUrlPattern = new Regex(@"https?://.+");
1718

@@ -27,14 +28,16 @@ public CustomUrlProvider()
2728
public override bool Initialize(string url)
2829
{
2930
if (string.IsNullOrEmpty(url) || !HostingUrlPattern.IsMatch(url) ||
30-
(!url.Contains(FileNamePlaceHolder) && !url.Contains(RevisionPlaceHolder)))
31+
(!(url.Contains(FileNamePlaceHolder) || url.Contains(UrlEncodedFileNamePlaceHolder)) &&
32+
!url.Contains(RevisionPlaceHolder)))
3133
{
3234
return false;
3335
}
3436

35-
if (url.Contains(FileNamePlaceHolder))
37+
if (url.Contains(FileNamePlaceHolder) || url.Contains(UrlEncodedFileNamePlaceHolder))
3638
{
37-
_rawUrl = url.Replace(FileNamePlaceHolder, "%var2%");
39+
_rawUrl = url.Replace(FileNamePlaceHolder, "%var2%")
40+
.Replace(UrlEncodedFileNamePlaceHolder, "%var2%");
3841
}
3942

4043
if (url.Contains(RevisionPlaceHolder))

0 commit comments

Comments
 (0)