doc

package
v0.5.0-beta.5 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package doc imports documents into scripts: plain text and Markdown, EPUB, DOCX and subtitles (SRT/WebVTT) are built in; PDF is plugged in by the app (PDFKit through the Swift helper) with Register. It also writes chapter cue sheets. Subtitle parsing and writing live in doc/subtitle.

Every importer is bounded by Limits (file size, decompressed size, ZIP entries, output length) so a hostile file cannot exhaust memory.

Index

Constants

View Source
const (
	DefaultMaxFileBytes    = 64 << 20
	DefaultMaxUncompressed = 256 << 20
	DefaultMaxEntries      = 20000
	DefaultMaxChars        = 5_000_000
)

Defaults.

View Source
const MaxHeadingChars = 60

MaxHeadingChars is the longest line plain-text chapter detection treats as a heading.

Variables

View Source
var (
	ErrUnsupported = errors.New("doc: unsupported format")
	ErrTooLarge    = errors.New("doc: file too large")
	ErrNoText      = errors.New("doc: no text found")
	ErrCorrupt     = errors.New("doc: file is damaged or not of its format")
)

Errors. Importers wrap them with detail; test with errors.Is.

Functions

func CueSheet

func CueSheet(marks []Mark, untitled func(n int) string) string

CueSheet writes the chapter cue sheet: one "HH:MM:SS<TAB>Title" line per mark, no trailing newline. Tabs and line breaks in titles collapse to spaces; an untitled chapter n (1-based) is named by untitled, or "Chapter n" when untitled is nil.

func Extensions

func Extensions() []string

Formats lists the extensions that have an extractor, for an open dialog.

func IsChapterHeading

func IsChapterHeading(line string) bool

IsChapterHeading reports whether a line of plain text looks like a chapter heading: "# Title", or a short line such as "Chapter 1", "CHAPTER IV: The End", "Prologue", "第一章 开始", "第3話", "제1장".

func MarkdownText

func MarkdownText(src string) string

MarkdownText converts Markdown to an AudioSmith script: "# " headings stay chapter headings, deeper headings become paragraphs, and code, images, link targets, emphasis marks, HTML, tables' pipes and list bullets are removed.

func Register

func Register(f Format, exts []string, x Extractor)

Register installs (or replaces) the extractor for a format and the file extensions (".pdf") that select it.

Types

type Chapter

type Chapter struct {
	Title string
	Text  string
}

Chapter is a titled run of text. Text holds paragraphs separated by one blank line, without the title.

func Chapterize

func Chapterize(s string) []Chapter

Chapterize splits plain text into chapters at headings (see IsChapterHeading). Text before the first heading becomes an untitled chapter. Runs of blank lines collapse to one.

type Document

type Document struct {
	Format   Format
	Title    string // from metadata, "" when unknown
	Author   string
	Language string // BCP-47 from metadata, "" when unknown
	Chapters []Chapter
}

Document is an imported document.

func Open

func Open(ctx context.Context, path string, lim Limits) (*Document, error)

Open imports the file at path, choosing the extractor by extension.

func Read

func Read(ctx context.Context, f Format, r io.ReaderAt, size int64, lim Limits) (*Document, error)

Read imports size bytes from r as format f.

func (*Document) Chars

func (d *Document) Chars() int

Chars is the number of runes in all chapter texts and titles.

func (*Document) Script

func (d *Document) Script() string

Script renders the document as an AudioSmith script: "# Title" before every titled chapter, paragraphs separated by blank lines. An untitled chapter has no heading (it continues the previous one when read).

func (*Document) Text

func (d *Document) Text() string

Text is the document's text without chapter headings.

type Extractor

type Extractor interface {
	Extract(ctx context.Context, r io.ReaderAt, size int64, lim Limits) (*Document, error)
}

Extractor reads one format. Implementations must honour lim (already defaulted) and ctx, and return ErrNoText when nothing readable is found. A PDF extractor plugged in from the app can call Chapterize on the text it extracts.

type ExtractorFunc

type ExtractorFunc func(ctx context.Context, r io.ReaderAt, size int64, lim Limits) (*Document, error)

ExtractorFunc adapts a function to Extractor.

func (ExtractorFunc) Extract

func (f ExtractorFunc) Extract(ctx context.Context, r io.ReaderAt, size int64, lim Limits) (*Document, error)

type Format

type Format string

Format identifies an input format.

const (
	TXT      Format = "txt"
	Markdown Format = "md"
	EPUB     Format = "epub"
	DOCX     Format = "docx"
	PDF      Format = "pdf"
	SRT      Format = "srt"
	VTT      Format = "vtt"
)

func FormatOf

func FormatOf(name string) (Format, bool)

FormatOf returns the format registered for a file name's extension.

type Limits

type Limits struct {
	MaxFileBytes    int64 // the input file; default 64 MiB
	MaxUncompressed int64 // total bytes inflated from a ZIP container; default 256 MiB
	MaxEntries      int   // ZIP entries; default 20 000
	MaxChars        int   // runes of text in the result; default 5 000 000
}

Limits bound an import. Zero fields take the defaults below; a negative field means no limit.

type Mark

type Mark struct {
	Title string
	Start time.Duration
}

Mark is a chapter start in rendered audio.

func MarksFromDurations

func MarksFromDurations(chs []RenderedChapter) []Mark

MarksFromDurations turns rendered chapter durations into start marks, summing in whole milliseconds and skipping failed chapters.

type RenderedChapter

type RenderedChapter struct {
	Title    string
	Duration time.Duration
	OK       bool
}

RenderedChapter is a chapter's outcome in a render: OK is false for a failed chapter, which adds no time and no cue.

Directories

Path Synopsis
Package subtitle parses and writes subtitles: SRT and WebVTT in both directions, ASS (with karaoke word highlighting) for writing, and Netflix-style segmentation of timed words into readable cues.
Package subtitle parses and writes subtitles: SRT and WebVTT in both directions, ASS (with karaoke word highlighting) for writing, and Netflix-style segmentation of timed words into readable cues.
Package textenc decodes text files of unknown encoding to UTF-8: BOMs, UTF-16 without a BOM, and the legacy encodings scripts arrive in (GB18030/GBK, Big5, Shift_JIS, EUC-KR, Windows-1252).
Package textenc decodes text files of unknown encoding to UTF-8: BOMs, UTF-16 without a BOM, and the legacy encodings scripts arrive in (GB18030/GBK, Big5, Shift_JIS, EUC-KR, Windows-1252).

Jump to

Keyboard shortcuts

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