68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Katteker
|
|
{
|
|
public static class ChangelogHelper
|
|
{
|
|
public static string ChangelogAsHtml(this string text, string extension)
|
|
{
|
|
string result;
|
|
switch (extension)
|
|
{
|
|
case ".txt":
|
|
var plainText = WebUtility.HtmlEncode(text);
|
|
result = plainText.Replace(Environment.NewLine, "<br />");
|
|
break;
|
|
case ".md":
|
|
result = new MarkdownSharp.Markdown().Transform(text);
|
|
break;
|
|
default:
|
|
result = text;
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static async Task<string> LoadChangelogAsync(string filename, string path)
|
|
{
|
|
if (!string.IsNullOrEmpty(filename) || !string.IsNullOrEmpty(path))
|
|
{
|
|
if (!path.EndsWith("/", StringComparison.Ordinal))
|
|
path += "/";
|
|
var webReq = WebRequest.Create(path + filename);
|
|
try
|
|
{
|
|
using (var response = await webReq.GetResponseAsync().ConfigureAwait(false))
|
|
using (var sr = new StreamReader(response.GetResponseStream()))
|
|
{
|
|
return await sr.ReadToEndAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
try
|
|
{
|
|
var changelogFilename = Path.GetFileName(filename);
|
|
if (changelogFilename == null) return null;
|
|
var currentChangelogPath = Path.Combine(Environment.CurrentDirectory, changelogFilename);
|
|
if (File.Exists(currentChangelogPath))
|
|
{
|
|
return File.ReadAllText(currentChangelogPath);
|
|
}
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignore;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
} |