(view source code of htmlescape.cs as plain text)
using System;
using System.IO;
using System.Linq;
using System.Net;
namespace RobvanderWoude
{
internal class HTMLEscape
{
static readonly string progver = "1.01";
static int Main( string[] args )
{
int rc = 0;
int argumentscount = args.Length;
if ( Console.IsInputRedirected )
{
argumentscount++;
}
if ( argumentscount != 1 || args.Contains( "/?" ) )
{
return ShowHelp( );
}
string unescapedtext = string.Empty;
if ( Console.IsInputRedirected )
{
unescapedtext = Console.In.ReadToEnd( );
}
else if ( File.Exists( args[0] ) )
{
unescapedtext= File.ReadAllText( args[0] );
}
else
{
unescapedtext = args[0];
}
string escapedtext = WebUtility.HtmlEncode( unescapedtext );
escapedtext = ConvertHTMLEntities( escapedtext );
Console.Write( escapedtext );
return rc;
}
static string ConvertHTMLEntities( string input )
{
string output = input.Replace( "'", "'" );
output = output.Replace( "À", "À" );
output = output.Replace( "&3193;", "Á" );
output = output.Replace( "Â", "Â" );
output = output.Replace( "Ã", "Ã" );
output = output.Replace( "Ä", "Ä" );
output = output.Replace( "Å", "Å" );
output = output.Replace( "Æ", "Æ" );
output = output.Replace( "Ç", "Ç" );
output = output.Replace( "È", "È" );
output = output.Replace( "É", "É" );
output = output.Replace( "Ê", "Ê" );
output = output.Replace( "Ë", "Ë" );
output = output.Replace( "Ì", "Ì" );
output = output.Replace( "Í", "Í" );
output = output.Replace( "Î", "Î" );
output = output.Replace( "Ï", "Ï" );
output = output.Replace( "Ð", "Ð" );
output = output.Replace( "Ñ", "Ñ" );
output = output.Replace( "Ò", "Ò" );
output = output.Replace( "Ó", "Ó" );
output = output.Replace( "Ô", "Ô" );
output = output.Replace( "Õ", "Õ" );
output = output.Replace( "Ö", "Ö" );
output = output.Replace( "Ø", "Ø" );
output = output.Replace( "Ù", "Ù" );
output = output.Replace( "Ú", "Ú" );
output = output.Replace( "Û", "Û" );
output = output.Replace( "Ü", "Ü" );
output = output.Replace( "Ý", "Ý" );
output = output.Replace( "Þ", "Þ" );
output = output.Replace( "ß", "ß" );
output = output.Replace( "à", "à" );
output = output.Replace( "á", "á" );
output = output.Replace( "â", "â" );
output = output.Replace( "ã", "ã" );
output = output.Replace( "ä", "ä" );
output = output.Replace( "å", "å" );
output = output.Replace( "æ", "æ" );
output = output.Replace( "ç", "ç" );
output = output.Replace( "è", "è" );
output = output.Replace( "é", "é" );
output = output.Replace( "ê", "ê" );
output = output.Replace( "ë", "ë" );
output = output.Replace( "ì", "ì" );
output = output.Replace( "í", "í" );
output = output.Replace( "î", "î" );
output = output.Replace( "ï", "ï" );
output = output.Replace( "ð", "ð" );
output = output.Replace( "ñ", "ñ" );
output = output.Replace( "ò", "ò" );
output = output.Replace( "ó", "ó" );
output = output.Replace( "ô", "ô" );
output = output.Replace( "õ", "õ" );
output = output.Replace( "ö", "ö" );
output = output.Replace( "ø", "ø" );
output = output.Replace( "ù", "ù" );
output = output.Replace( "ú", "ú" );
output = output.Replace( "û", "û" );
output = output.Replace( "ü", "ü" );
output = output.Replace( "ý", "ý" );
output = output.Replace( "þ", "þ" );
output = output.Replace( "ÿ", "ÿ" );
return output;
}
public static int ShowHelp( string errorMessage = "" )
{
#region Help Text
/*
HTMLEscape, Version 1.00
Display HTML-escaped input string in console
Usage: HTMLESCAPE textfile
or: HTMLESCAPE unescapedstring
or: some_program | HTMLESCAPE
Where: textfile file containing text to be escaped
unescapedstring string to be escaped
some_program program whose output is to be escaped
Note: Exit code ("errorlevel") -1 in case of errors, otherwise 0.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Error Message
if ( !string.IsNullOrWhiteSpace( errorMessage ) )
{
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errorMessage );
Console.ResetColor( );
}
#endregion Error Message
#region Display Help
Console.Error.WriteLine( );
Console.Error.WriteLine( "HTMLEscape, Version {0}", progver );
Console.Error.WriteLine( "Display HTML-escaped input string in console" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "HTMLESCAPE textfile" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( " or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "HTMLESCAPE unescapedstring" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( " or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "some_program | HTMLESCAPE" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "textfile" );
Console.ResetColor( );
Console.Error.WriteLine( " file containing text to be escaped" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " unescapedstring" );
Console.ResetColor( );
Console.Error.WriteLine( " string to be escaped" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " some_program" );
Console.ResetColor( );
Console.Error.WriteLine( " program whose output is to be escaped" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Note: Exit code (\"errorlevel\") -1 in case of errors, otherwise 0." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help
return -1;
}
}
}
page last modified: 2024-04-16; loaded in 0.0078 seconds