kepub.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Package kepub converts a finished EPUB into a Kobo KEPUB.
  2. //
  3. // Kobo devices open files ending in .kepub.epub with their own WebKit-based
  4. // renderer instead of Adobe's RMSDK, which gives faster page turns, reading
  5. // statistics and more reliable highlights. kepubify does the conversion: it
  6. // wraps each sentence in a koboSpan (which the statistics and highlights
  7. // need) and adds Kobo's style hooks.
  8. package kepub
  9. import (
  10. "archive/zip"
  11. "bytes"
  12. "context"
  13. "fmt"
  14. "github.com/pgaskin/kepubify/v4/kepub"
  15. )
  16. // Convert returns epub converted to a KEPUB.
  17. func Convert(ctx context.Context, epub []byte) ([]byte, error) {
  18. zr, err := zip.NewReader(bytes.NewReader(epub), int64(len(epub)))
  19. if err != nil {
  20. return nil, fmt.Errorf("reading the EPUB: %w", err)
  21. }
  22. c := kepub.NewConverterWithOptions(
  23. // Hyphenation helps justified text on the Clara HD's 6" screen.
  24. kepub.ConverterOptionHyphenate(true),
  25. // Smartypants is left off: it rewrites quotes and dashes, which
  26. // would change the article's text.
  27. )
  28. var out bytes.Buffer
  29. if err := c.Convert(ctx, &out, zr); err != nil {
  30. return nil, fmt.Errorf("converting to KEPUB: %w", err)
  31. }
  32. return out.Bytes(), nil
  33. }