You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.6 KiB

  1. # html2text
  2. [![Documentation](https://godoc.org/github.com/jaytaylor/html2text?status.svg)](https://godoc.org/github.com/jaytaylor/html2text)
  3. [![Build Status](https://travis-ci.org/jaytaylor/html2text.svg?branch=master)](https://travis-ci.org/jaytaylor/html2text)
  4. [![Report Card](https://goreportcard.com/badge/github.com/jaytaylor/html2text)](https://goreportcard.com/report/github.com/jaytaylor/html2text)
  5. ### Converts HTML into text
  6. ## Introduction
  7. html2text is a simple golang package for rendering HTML into plaintext.
  8. There are still lots of improvements to be had, but FWIW this has worked fine for my [basic] HTML-2-text needs.
  9. It requires go 1.x or newer ;)
  10. ## Download the package
  11. ```bash
  12. go get github.com/jaytaylor/html2text
  13. ```
  14. ## Example usage
  15. ```go
  16. package main
  17. import (
  18. "fmt"
  19. "github.com/jaytaylor/html2text"
  20. )
  21. func main() {
  22. inputHtml := `
  23. <html>
  24. <head>
  25. <title>My Mega Service</title>
  26. <link rel=\"stylesheet\" href=\"main.css\">
  27. <style type=\"text/css\">body { color: #fff; }</style>
  28. </head>
  29. <body>
  30. <div class="logo">
  31. <a href="http://mymegaservice.com/"><img src="/logo-image.jpg" alt="Mega Service"/></a>
  32. </div>
  33. <h1>Welcome to your new account on my service!</h1>
  34. <p>
  35. Here is some more information:
  36. <ul>
  37. <li>Link 1: <a href="https://example.com">Example.com</a></li>
  38. <li>Link 2: <a href="https://example2.com">Example2.com</a></li>
  39. <li>Something else</li>
  40. </ul>
  41. </p>
  42. </body>
  43. </html>
  44. `
  45. text, err := html2text.FromString(inputHtml)
  46. if err != nil {
  47. panic(err)
  48. }
  49. fmt.Println(text)
  50. }
  51. ```
  52. Output:
  53. ```
  54. Mega Service ( http://mymegaservice.com/ )
  55. ******************************************
  56. Welcome to your new account on my service!
  57. ******************************************
  58. Here is some more information:
  59. * Link 1: Example.com ( https://example.com )
  60. * Link 2: Example2.com ( https://example2.com )
  61. * Something else
  62. ```
  63. ## Unit-tests
  64. Running the unit-tests is straightforward and standard:
  65. ```bash
  66. go test
  67. ```
  68. # License
  69. Permissive MIT license.
  70. ## Contact
  71. You are more than welcome to open issues and send pull requests if you find a bug or want a new feature.
  72. 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.
  73. Email: jay at (my github username).com
  74. Twitter: [@jtaylor](https://twitter.com/jtaylor)