Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for htmlescape.cs

(view source code of htmlescape.cs as plain text)

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Net;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	internal class HTMLEscape
  10. 	{
  11. 		static readonly string progver = "1.01";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			int rc = 0;
  17.  
  18. 			int argumentscount = args.Length;
  19. 			if ( Console.IsInputRedirected )
  20. 			{
  21. 				argumentscount++;
  22. 			}
  23. 			if ( argumentscount != 1 || args.Contains( "/?" ) )
  24. 			{
  25. 				return ShowHelp( );
  26. 			}
  27.  
  28. 			string unescapedtext = string.Empty;
  29. 			if ( Console.IsInputRedirected )
  30. 			{
  31. 				unescapedtext = Console.In.ReadToEnd( );
  32. 			}
  33. 			else if ( File.Exists( args[0] ) )
  34. 			{
  35. 				unescapedtext= File.ReadAllText( args[0] );
  36. 			}
  37. 			else
  38. 			{
  39. 				unescapedtext = args[0];
  40. 			}
  41. 			string escapedtext = WebUtility.HtmlEncode( unescapedtext );
  42. 			escapedtext = ConvertHTMLEntities( escapedtext );
  43. 			Console.Write( escapedtext );
  44.  
  45. 			return rc;
  46. 		}
  47.  
  48.  
  49.  
  50. 		static string ConvertHTMLEntities( string input )
  51. 		{
  52. 			string output = input.Replace( "'", "'" );
  53. 			output = output.Replace( "À", "À" );
  54. 			output = output.Replace( "&3193;", "Á" );
  55. 			output = output.Replace( "Â", "Â" );
  56. 			output = output.Replace( "Ã", "Ã" );
  57. 			output = output.Replace( "Ä", "Ä" );
  58. 			output = output.Replace( "Å", "Å" );
  59. 			output = output.Replace( "Æ", "Æ" );
  60. 			output = output.Replace( "Ç", "Ç" );
  61. 			output = output.Replace( "È", "È" );
  62. 			output = output.Replace( "É", "É" );
  63. 			output = output.Replace( "Ê", "Ê" );
  64. 			output = output.Replace( "Ë", "Ë" );
  65. 			output = output.Replace( "Ì", "Ì" );
  66. 			output = output.Replace( "Í", "Í" );
  67. 			output = output.Replace( "Î", "Î" );
  68. 			output = output.Replace( "Ï", "Ï" );
  69. 			output = output.Replace( "Ð", "Ð" );
  70. 			output = output.Replace( "Ñ", "Ñ" );
  71. 			output = output.Replace( "Ò", "Ò" );
  72. 			output = output.Replace( "Ó", "Ó" );
  73. 			output = output.Replace( "Ô", "Ô" );
  74. 			output = output.Replace( "Õ", "Õ" );
  75. 			output = output.Replace( "Ö", "Ö" );
  76. 			output = output.Replace( "Ø", "Ø" );
  77. 			output = output.Replace( "Ù", "Ù" );
  78. 			output = output.Replace( "Ú", "Ú" );
  79. 			output = output.Replace( "Û", "Û" );
  80. 			output = output.Replace( "Ü", "Ü" );
  81. 			output = output.Replace( "Ý", "Ý" );
  82. 			output = output.Replace( "Þ", "Þ" );
  83. 			output = output.Replace( "ß", "ß" );
  84. 			output = output.Replace( "à", "à" );
  85. 			output = output.Replace( "á", "á" );
  86. 			output = output.Replace( "â", "â" );
  87. 			output = output.Replace( "ã", "ã" );
  88. 			output = output.Replace( "ä", "ä" );
  89. 			output = output.Replace( "å", "å" );
  90. 			output = output.Replace( "æ", "æ" );
  91. 			output = output.Replace( "ç", "ç" );
  92. 			output = output.Replace( "è", "è" );
  93. 			output = output.Replace( "é", "é" );
  94. 			output = output.Replace( "ê", "ê" );
  95. 			output = output.Replace( "ë", "ë" );
  96. 			output = output.Replace( "ì", "ì" );
  97. 			output = output.Replace( "í", "í" );
  98. 			output = output.Replace( "î", "î" );
  99. 			output = output.Replace( "ï", "ï" );
  100. 			output = output.Replace( "ð", "ð" );
  101. 			output = output.Replace( "ñ", "ñ" );
  102. 			output = output.Replace( "ò", "ò" );
  103. 			output = output.Replace( "ó", "ó" );
  104. 			output = output.Replace( "ô", "ô" );
  105. 			output = output.Replace( "õ", "õ" );
  106. 			output = output.Replace( "ö", "ö" );
  107. 			output = output.Replace( "ø", "ø" );
  108. 			output = output.Replace( "ù", "ù" );
  109. 			output = output.Replace( "ú", "ú" );
  110. 			output = output.Replace( "û", "û" );
  111. 			output = output.Replace( "ü", "ü" );
  112. 			output = output.Replace( "ý", "ý" );
  113. 			output = output.Replace( "þ", "þ" );
  114. 			output = output.Replace( "ÿ", "ÿ" );
  115. 			return output;
  116. 		}
  117.  
  118.  
  119. 		public static int ShowHelp( string errorMessage = "" )
  120. 		{
  121. 			#region Help Text
  122.  
  123. 			/*
  124. 			HTMLEscape,  Version 1.00
  125. 			Display HTML-escaped input string in console
  126.  
  127. 			Usage:  HTMLESCAPE  textfile
  128.  
  129. 			   or:  HTMLESCAPE  unescapedstring
  130.  
  131. 			   or:  some_program | HTMLESCAPE
  132.  
  133. 			Where:  textfile         file containing text to be escaped
  134. 			        unescapedstring  string to be escaped
  135. 			        some_program     program whose output is to be escaped
  136.  
  137. 			Note:   Exit code ("errorlevel") -1 in case of errors, otherwise 0.
  138.  
  139. 			Written by Rob van der Woude
  140. 			https://www.robvanderwoude.com
  141. 			*/
  142.  
  143. 			#endregion Help Text
  144.  
  145.  
  146. 			#region Error Message
  147.  
  148. 			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  149. 			{
  150. 				Console.Error.WriteLine( );
  151. 				Console.ForegroundColor = ConsoleColor.Red;
  152. 				Console.Error.Write( "ERROR: " );
  153. 				Console.ForegroundColor = ConsoleColor.White;
  154. 				Console.Error.WriteLine( errorMessage );
  155. 				Console.ResetColor( );
  156. 			}
  157.  
  158. 			#endregion Error Message
  159.  
  160.  
  161. 			#region Display Help
  162.  
  163. 			Console.Error.WriteLine( );
  164.  
  165. 			Console.Error.WriteLine( "HTMLEscape,  Version {0}", progver );
  166.  
  167. 			Console.Error.WriteLine( "Display HTML-escaped input string in console" );
  168.  
  169. 			Console.Error.WriteLine( );
  170.  
  171. 			Console.Error.Write( "Usage:  " );
  172. 			Console.ForegroundColor = ConsoleColor.White;
  173. 			Console.Error.WriteLine( "HTMLESCAPE  textfile" );
  174. 			Console.ResetColor( );
  175.  
  176. 			Console.Error.WriteLine( );
  177.  
  178. 			Console.Error.Write( "   or:  " );
  179. 			Console.ForegroundColor = ConsoleColor.White;
  180. 			Console.Error.WriteLine( "HTMLESCAPE  unescapedstring" );
  181. 			Console.ResetColor( );
  182.  
  183. 			Console.Error.WriteLine( );
  184.  
  185. 			Console.Error.Write( "   or:  " );
  186. 			Console.ForegroundColor = ConsoleColor.White;
  187. 			Console.Error.WriteLine( "some_program | HTMLESCAPE" );
  188. 			Console.ResetColor( );
  189.  
  190. 			Console.Error.WriteLine( );
  191.  
  192. 			Console.Error.Write( "Where:  " );
  193. 			Console.ForegroundColor = ConsoleColor.White;
  194. 			Console.Error.Write( "textfile" );
  195. 			Console.ResetColor( );
  196. 			Console.Error.WriteLine( "         file containing text to be escaped" );
  197.  
  198. 			Console.ForegroundColor = ConsoleColor.White;
  199. 			Console.Error.Write( "        unescapedstring" );
  200. 			Console.ResetColor( );
  201. 			Console.Error.WriteLine( "  string to be escaped" );
  202.  
  203. 			Console.ForegroundColor = ConsoleColor.White;
  204. 			Console.Error.Write( "        some_program" );
  205. 			Console.ResetColor( );
  206. 			Console.Error.WriteLine( "     program whose output is to be escaped" );
  207.  
  208. 			Console.Error.WriteLine( );
  209.  
  210. 			Console.Error.WriteLine( "Note:   Exit code (\"errorlevel\") -1 in case of errors, otherwise 0." );
  211.  
  212. 			Console.Error.WriteLine( );
  213.  
  214. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  215.  
  216. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  217.  
  218. 			#endregion Display Help
  219.  
  220.  
  221. 			return -1;
  222. 		}
  223. 	}
  224. }

page last modified: 2024-04-16; loaded in 0.0078 seconds