Code Monkey home page Code Monkey logo

guess_html_encoding's Introduction

GuessHTMLEncoding

GuessHTMLEncoding is a simple library to guess HTML encodings in Ruby 1.9. It considers HTTP headers and META tags.

Install

(sudo) gem install guess_html_encoding

Usage

GuessHTMLEncoding can guess the encoding of an HTML file based on the http-equiv content-type:

require 'rubygems'
require 'guess_html_encoding'

guess = GuessHtmlEncoding.guess(<<-HTML)
  <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=LATIN1">
    </head>
    <body>
      <div>hi!</div>
    </body>
  </html>
HTML
guess.should == "ISO-8859-1"

You can also give it HTTP headers to guess from, which it will prefer, both as a string or as a hash:

guess = GuessHtmlEncoding.guess("<html><body><div>hi!</div></body></html>",
                                "Hello: world\nContent-Type: text/html; charset=LATIN1\nFoo: bar")
guess.should == "ISO-8859-1"

guess = GuessHtmlEncoding.guess("<html><body><div>hi!</div></body></html>",
                                {"Hello" => "world", "Content-Type" => "text/html; charset=LATIN1", "Foo" => "bar"})
guess.should == "ISO-8859-1"

It's likely that you want to force the encoding of the given HTML into the guessed encoding. This is easy to do:

data = "<html><head><meta http-equiv='content-type' content='text/html; charset=utf8;'></head><body><div>hi!♥</div></body></html>"
encoded = GuessHtmlEncoding.encode(data)
encoded.encoding.to_s.should == "UTF-8"

If an encoding cannot be guessed, or Ruby doesn't understand it, UTF-8 will be used and unknown characters will be ignored.

Pull requests welcome!

guess_html_encoding's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.