示例#1
0
        public Encoding Match(string html)
        {
            var match = charsetExpression.Match(html);
            var value = match.Groups["charset"].Value;

            return(string.IsNullOrEmpty(value)
                       ? null
                       : EncodingUtil.TryGetEncoding(value));
        }
示例#2
0
        public string Convert(Stream stream, string backupEncoding)
        {
            var defaultEncoding = Encoding.UTF8;
            var bytes           = GetBytes(stream);
            var html            = ConvertToString(bytes, defaultEncoding);

            var charsetEncodingInHtml = matcher.Match(html);

            if (CharsetIsSpecifiedInHtml(charsetEncodingInHtml))
            {
                return(EncodingEquals(charsetEncodingInHtml, defaultEncoding)
                    ? html
                    : ConvertToString(bytes, charsetEncodingInHtml));
            }

            if (!string.IsNullOrEmpty(backupEncoding))
            {
                return(EncodingEquals(EncodingUtil.TryGetEncoding(backupEncoding), defaultEncoding)
                    ? html
                    : ConvertToString(bytes, Encoding.GetEncoding(backupEncoding)));
            }
            return(html);
        }