html2text

package module
v0.0.0-...-6aa2d97 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2020 License: MIT Imports: 7 Imported by: 0

README

html2text. Forked.

Documentation Build Status Report Card Exago Exago


html2text

Documentation Build Status Report Card

Converts HTML into text

Introduction

Ensure your emails are readable by all!

Turns HTML into raw text, useful for sending fancy HTML emails with a equivalently nicely formatted TXT document as a fallback (e.g. for people who don't allow HTML emails or have other display issues).

html2text is a simple golang package for rendering HTML into plaintext.

There are still lots of improvements to be had, but FWIW this has worked fine for my [basic] HTML-2-text needs.

It requires go 1.x or newer ;)

Download the package

go get github.com/jaytaylor/html2text

Example usage

package main

import (
	"fmt"

	"github.com/jaytaylor/html2text"
)

func main() {
	inputHtml := `
          <html>
            <head>
              <title>My Mega Service</title>
              <link rel=\"stylesheet\" href=\"main.css\">
              <style type=\"text/css\">body { color: #fff; }</style>
            </head>

            <body>
              <div class="logo">
                <a href="http://mymegaservice.com/"><img src="/logo-image.jpg" alt="Mega Service"/></a>
              </div>

              <h1>Welcome to your new account on my service!</h1>

              <p>
                  Here is some more information:

                  <ul>
                      <li>Link 1: <a href="https://example.com">Example.com</a></li>
                      <li>Link 2: <a href="https://example2.com">Example2.com</a></li>
                      <li>Something else</li>
                  </ul>
              </p>
            </body>
          </html>
	`

	text, err := html2text.FromString(inputHtml)
	if err != nil {
		panic(err)
	}
	fmt.Println(text)
}

Output:

Mega Service ( http://mymegaservice.com/ )

******************************************
Welcome to your new account on my service!
******************************************

Here is some more information:

* Link 1: Example.com ( https://example.com )
* Link 2: Example2.com ( https://example2.com )
* Something else

Unit-tests

Running the unit-tests is straightforward and standard:

go test

License

Permissive MIT license.

Contact

You are more than welcome to open issues and send pull requests if you find a bug or want a new feature.

If you appreciate this library please feel free to drop me a line and tell me! It's always nice to hear from people who have benefitted from my work.

Email: jay at (my github username).com

Twitter: @jtaylor

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromHtmlNode

func FromHtmlNode(doc *html.Node) (string, error)

FromHtmlNode renders text output from a pre-parsed HTML document.

func FromReader

func FromReader(reader io.Reader) (string, error)

FromReader renders text output after parsing HTML for the specified io.Reader.

func FromReaderWithRenderer

func FromReaderWithRenderer(reader io.Reader, n NodeRenderer) (string, error)

FromReaderWithRenderer like FromReader but with optional 'n' for custom node.

func FromString

func FromString(input string) (string, error)

FromString parses HTML from the input string, then renders the text form.

Example
inputHtml := `
	  <html>
		<head>
		  <title>My Mega Service</title>
		  <link rel=\"stylesheet\" href=\"main.css\">
		  <style type=\"text/css\">body { color: #fff; }</style>
		</head>

		<body>
		  <div class="logo">
			<a href="http://mymegaservice.com/"><img src="/logo-image.jpg" alt="Mega Service"/></a>
		  </div>

		  <h1>Welcome to your new account on my service!</h1>

		  <p>
			  Here is some more information:

			  <ul>
				  <li>Link 1: <a href="https://example.com">Example.com</a></li>
				  <li>Link 2: <a href="https://example2.com">Example2.com</a></li>
				  <li>Something else</li>
			  </ul>
		  </p>
		</body>
	  </html>
	`

text, err := FromString(inputHtml)
if err != nil {
	panic(err)
}
fmt.Println(text)
Output:

Mega Service ( http://mymegaservice.com/ )

******************************************
Welcome to your new account on my service!
******************************************

Here is some more information:

* Link 1: Example.com ( https://example.com )
* Link 2: Example2.com ( https://example2.com )
* Something else

func FromStringWithRenderer

func FromStringWithRenderer(input string, n NodeRenderer) (string, error)

FromStringWithRenderer like FromString but with optional 'n' for custom node.

func LoopChildren

func LoopChildren(node *html.Node, d DefaultRenderer) error

LoopChildren is helper for custom node that need default render engine.

Types

type DefaultRenderer

type DefaultRenderer interface {
	DefaultRender(node *html.Node) error
}

DefaultRenderer pass to custom node to trigger default render engine.

type DefaultRendererWriter

type DefaultRendererWriter interface {
	DefaultRenderer
	io.Writer
}

DefaultRendererWriter implements both DefaultRenderer and io.Writer.

type NodeRenderer

type NodeRenderer interface {
	NodeRender(node *html.Node, d DefaultRendererWriter) (next *html.Node, err error)
}

NodeRenderer take html 'node' and convert to plain text by writing to 'd'. 'd' can also be used to handle html node using the default method. Return non-nil 'err' if any error. Return non-nil 'next' if there is any child node that need to be handled by default renderer else return nil 'next' to indicate done for this 'node'.

Jump to

Keyboard shortcuts

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