quotedprintable

package module
Version: v3.0.0-...-2caba25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2015 License: MIT Imports: 10 Imported by: 9

README

quotedprintable

Introduction

Package quotedprintable implements quoted-printable and message header encoding as specified by RFC 2045 and RFC 2047.

It is a copy of the Go 1.5 package mime/quotedprintable. It also includes the new functions of package mime concerning RFC 2047.

This code has minor changes with the standard library code in order to work with Go 1.0 and newer.

Documentation

https://godoc.org/gopkg.in/alexcesaro/quotedprintable.v3

Documentation

Overview

Package quotedprintable implements quoted-printable encoding as specified by RFC 2045.

Index

Examples

Constants

View Source
const (
	// BEncoding represents Base64 encoding scheme as defined by RFC 2045.
	BEncoding = WordEncoder('b')
	// QEncoding represents the Q-encoding scheme as defined by RFC 2047.
	QEncoding = WordEncoder('q')
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader is a quoted-printable decoder.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a quoted-printable reader, decoding from r.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read reads and decodes quoted-printable data from the underlying reader.

type WordDecoder

type WordDecoder struct {
	// CharsetReader, if non-nil, defines a function to generate
	// charset-conversion readers, converting from the provided
	// charset into UTF-8.
	// Charsets are always lower-case. utf-8, iso-8859-1 and us-ascii charsets
	// are handled by default.
	// One of the the CharsetReader's result values must be non-nil.
	CharsetReader func(charset string, input io.Reader) (io.Reader, error)
}

A WordDecoder decodes MIME headers containing RFC 2047 encoded-words.

func (*WordDecoder) Decode

func (d *WordDecoder) Decode(word string) (string, error)

Decode decodes an encoded-word. If word is not a valid RFC 2047 encoded-word, word is returned unchanged.

Example
dec := new(WordDecoder)
header, err := dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
if err != nil {
	panic(err)
}
fmt.Println(header)

dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
	switch charset {
	case "x-case":
		// Fake character set for example.
		// Real use would integrate with packages such
		// as code.google.com/p/go-charset
		content, err := ioutil.ReadAll(input)
		if err != nil {
			return nil, err
		}
		return bytes.NewReader(bytes.ToUpper(content)), nil
	}
	return nil, fmt.Errorf("unhandled charset %q", charset)
}
header, err = dec.Decode("=?x-case?q?hello!?=")
if err != nil {
	panic(err)
}
fmt.Println(header)
Output:

¡Hola, señor!
HELLO!

func (*WordDecoder) DecodeHeader

func (d *WordDecoder) DecodeHeader(header string) (string, error)

DecodeHeader decodes all encoded-words of the given string. It returns an error if and only if CharsetReader of d returns an error.

Example
dec := new(WordDecoder)
header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <eric@example.org>, =?utf-8?q?Ana=C3=AFs?= <anais@example.org>")
if err != nil {
	panic(err)
}
fmt.Println(header)

header, err = dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=")
if err != nil {
	panic(err)
}
fmt.Println(header)

dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
	switch charset {
	case "x-case":
		// Fake character set for example.
		// Real use would integrate with packages such
		// as code.google.com/p/go-charset
		content, err := ioutil.ReadAll(input)
		if err != nil {
			return nil, err
		}
		return bytes.NewReader(bytes.ToUpper(content)), nil
	}
	return nil, fmt.Errorf("unhandled charset %q", charset)
}
header, err = dec.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=")
if err != nil {
	panic(err)
}
fmt.Println(header)
Output:

Éric <eric@example.org>, Anaïs <anais@example.org>
¡Hola, señor!
HELLO WORLD!

type WordEncoder

type WordEncoder byte

A WordEncoder is a RFC 2047 encoded-word encoder.

func (WordEncoder) Encode

func (e WordEncoder) Encode(charset, s string) string

Encode returns the encoded-word form of s. If s is ASCII without special characters, it is returned unchanged. The provided charset is the IANA charset name of s. It is case insensitive.

Example
fmt.Println(QEncoding.Encode("utf-8", "¡Hola, señor!"))
fmt.Println(QEncoding.Encode("utf-8", "Hello!"))
fmt.Println(BEncoding.Encode("UTF-8", "¡Hola, señor!"))
fmt.Println(QEncoding.Encode("ISO-8859-1", "Caf\xE9"))
Output:

=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=
Hello!
=?UTF-8?b?wqFIb2xhLCBzZcOxb3Ih?=
=?ISO-8859-1?q?Caf=E9?=

type Writer

type Writer struct {
	// Binary mode treats the writer's input as pure binary and processes end of
	// line bytes as binary data.
	Binary bool
	// contains filtered or unexported fields
}

A Writer is a quoted-printable writer that implements io.WriteCloser.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer that writes to w.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the Writer, flushing any unwritten data to the underlying io.Writer, but does not close the underlying io.Writer.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write encodes p using quoted-printable encoding and writes it to the underlying io.Writer. It limits line length to 76 characters. The encoded bytes are not necessarily flushed until the Writer is closed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL