html_unicoder

Version, currently 0.12 versions

github.com/kostya/html_unicoder

Convert html page to utf-8 for Crystal language

3 stars
0 dependents
License: MIT

Nothing has been indexed for 0.1 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  html_unicoder:
    github: kostya/html_unicoder
    version: ~> 0.1

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

# html_unicoder

Convert html page to utf-8 for Crystal language.

## Features
* Encoding name parsed from http headers
* Encoding name parsed from page meta tag
* Encoding name normalized to be used in internal Crystal decoder.
* Correctly handle many edge cases
* Convert page as IO to IO
* Result page should be safe utf-8 to use in Crystal

## Installation


Add this to your application's `shard.yml`:

```yaml
dependencies:
  html_unicoder:
    github: kostya/html_unicoder
```


## Usage


```crystal
require "html_unicoder"

# basic usage, encoding only from meta tag, or use UTF-8//ignore, by default
page = HtmlUnicoder.new(page, default_encoding: "UTF-8").to_s

# use headers Array(String)
page = HtmlUnicoder.new(page, headers: ["Content-type: text/html; charset=Windows-1251"]).to_s

# use custom encoding
page = HtmlUnicoder.new(page, encoding: "CP1251").to_s

# set use default encoding
HtmlUnicoder.default_encoding = "CP1251"
page = HtmlUnicoder.new(page).to_s

# with http client
require "http/client"
page = ""
HTTP::Client.get("http://www.example.com") do |response|
  page = HtmlUnicoder.new(response.body_io, response.headers).to_s
end

# io -> io
io = HtmlUnicoder.new(io).io

# streaming http client body_io
#   steps to find encoding
#   1) finding encoding in headers
#   2) finding encoding in response.body meta tag
#   3) consider io as CP1251
body_io = HtmlUnicoder.new(response.body_io, response.headers, default_encoding: "CP1251").io
```