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

Commit

Permalink
adding console option --script-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sdesalas committed Jan 6, 2015
1 parent fc84b54 commit 2b4adb0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
8 changes: 7 additions & 1 deletion API/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public object RunFile(string filepath)
throw new Exception(String.Format("File does not exist {0}.", filepath));
}
// Read file
string script = File.ReadAllText(file.FullName);
string script = File.ReadAllText(file.FullName, Context.Encoding ?? Encoding.UTF8);
// Execute file
return RunScript(script, file.Name);
}
Expand Down Expand Up @@ -75,6 +75,12 @@ public static Context Current
get { return Program.Context; }
}

/// <summary>
/// Encoding for reading script files
/// </summary>
/// <param name="encoding"></param>
public static Encoding Encoding { get; set; }

/// <summary>
/// Handles an exception
/// </summary>
Expand Down
47 changes: 44 additions & 3 deletions API/Phantom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace TrifleJS.API
Expand Down Expand Up @@ -132,12 +133,52 @@ public static string outputEncoding {
{
try
{
System.Console.OutputEncoding = Encoding.GetEncoding(value);
string encoding = SanitizeEncoding(value);
System.Console.OutputEncoding = Encoding.GetEncoding(encoding);
}
catch {
catch
{
Console.error(String.Format("Unknown Encoding '{0}'", value));
};
}
}
}

/// <summary>
/// Note that windows uses "UTF-8" instead of "UTF8"
/// so we have to sanitize these strings
/// </summary>
internal static string scriptEncoding
{
get { return Context.Encoding.WebName.ToUpper(); }
set
{
try
{
string encoding = SanitizeEncoding(value);
Context.Encoding = Encoding.GetEncoding(encoding);
}
catch
{
Console.error(String.Format("Unknown Encoding '{0}'", value));
}
}
}

/// <summary>
/// Note that windows uses "UTF-8" instead of "UTF8"
/// so we have to sanitize these strings
/// </summary>
internal static string SanitizeEncoding(string encoding)
{
if (encoding != null)
{
if (encoding.IndexOf("utf", StringComparison.InvariantCultureIgnoreCase) == 0
&& !encoding.Contains("-"))
{
return Regex.Replace(encoding, "utf", "UTF-", RegexOptions.IgnoreCase);
}
}
return encoding;
}

#region Cookies
Expand Down
6 changes: 6 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ static void Main(string[] args)
case "--emulate":
isVersionSet = Browser.Emulate(arg.Replace("--emulate=", "").ToUpper());
break;
case "--output-encoding":
API.Phantom.outputEncoding = arg.Replace("--output-encoding=", "");
break;
case "--script-encoding":
API.Phantom.scriptEncoding = arg.Replace("--script-encoding=", "");
break;
case "--proxy":
Proxy.server = arg.Replace("--proxy=", "");
break;
Expand Down

0 comments on commit 2b4adb0

Please sign in to comment.