| index c3c3986..45aa560 100644 |
| --- a/internal/docs/preview.go |
| +++ b/internal/docs/preview.go |
| @@ -3,7 +3,9 @@ package docs |
| import ( |
| "context" |
| "errors" |
| + "strings" |
| "sync" |
| + "unicode/utf8" |
| |
| "github.com/carlosframework/docs/internal/export" |
| ) |
| @@ -86,15 +88,19 @@ func bars(lines []export.LineBox) []previewBar { |
| return out |
| } |
| |
| -// previews is the document list's thumbnail cache. |
| +// previews is the document list's thumbnail cache, and the cache of |
| +// each row's opening words. |
| // |
| // IN MEMORY, AND NOT IN THE DATABASE — the one decision in this file |
| // worth arguing with, so here is the argument. A previewBar carries no |
| // characters: it is four numbers, and the document's words never leave |
| -// export.PageOne (see LineBox's own note). So this is not plaintext, |
| -// and writing it to SQLite would not put document text at rest. |
| +// export.PageOne (see LineBox's own note). An excerpt is the |
| +// document's own words, so writing it to SQLite WOULD put text at |
| +// rest, outside the seal; it is the running instance holding what it |
| +// already holds whenever it replays a document, and nothing more. |
| // |
| -// It is still not written down, for a reason that outlives that. Spec |
| +// The bars are not written down either, for a reason that outlives |
| +// that. Spec |
| // §2 seals content at rest and §2's boundary test greps raw SQLite |
| // bytes for a fixture's known plaintext. A preview column would be a |
| // second thing to remember to seal when sealing lands, and that grep |
| @@ -143,6 +149,9 @@ type previewEntry struct { |
| // never read — get answered from bars being nil either way — so it |
| // recorded a distinction nothing acted on. |
| bars []previewBar |
| + // excerpt is the document's opening words (see excerpt), "" when it |
| + // has none. Cached and settled with the bars, from the same parse. |
| + excerpt string |
| } |
| |
| // maxPreviews bounds the map. |
| @@ -164,19 +173,19 @@ const maxPreviews = 512 |
| |
| func newPreviews() *previews { return &previews{by: map[int64]previewEntry{}} } |
| |
| -// get returns the cached bars for doc at its current updated_at, and |
| -// whether there was a usable entry. |
| -func (p *previews) get(id int64, updatedAtNanos int64) ([]previewBar, bool) { |
| +// get returns the cached entry for doc at its current updated_at, and |
| +// whether there was a usable one. |
| +func (p *previews) get(id int64, updatedAtNanos int64) (previewEntry, bool) { |
| p.mu.Lock() |
| defer p.mu.Unlock() |
| e, ok := p.by[id] |
| if !ok || e.updatedAtNanos != updatedAtNanos { |
| - return nil, false |
| + return previewEntry{}, false |
| } |
| - return e.bars, true |
| + return e, true |
| } |
| |
| -func (p *previews) put(id int64, updatedAtNanos int64, b []previewBar) { |
| +func (p *previews) put(id int64, updatedAtNanos int64, e previewEntry) { |
| p.mu.Lock() |
| defer p.mu.Unlock() |
| if _, replacing := p.by[id]; !replacing { |
| @@ -187,7 +196,8 @@ func (p *previews) put(id int64, updatedAtNanos int64, b []previewBar) { |
| } |
| } |
| } |
| - p.by[id] = previewEntry{updatedAtNanos: updatedAtNanos, bars: b} |
| + e.updatedAtNanos = updatedAtNanos |
| + p.by[id] = e |
| } |
| |
| // thumbnail lays out one document's first page and caches the result. |
| @@ -210,20 +220,20 @@ func (p *previews) put(id int64, updatedAtNanos int64, b []previewBar) { |
| // timeout and a busy database are the same shape. Those say nothing |
| // about the document, so they are logged at a lower level and left |
| // uncached, and the next render simply tries again. |
| -func (h *handlers) thumbnail(ctx context.Context, doc *Document) []previewBar { |
| +func (h *handlers) thumbnail(ctx context.Context, doc *Document) previewEntry { |
| nanos := doc.UpdatedAt.UnixNano() |
| |
| // remember records a settled "draw nothing" for this version. A |
| // context that is already done means the answer is about the |
| // request, not the document, so nothing is written down. |
| - remember := func(what string, err error) []previewBar { |
| + remember := func(what string, err error) previewEntry { |
| if ctx.Err() != nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { |
| h.logger.Debug("docs: list thumbnail abandoned with the request", "doc_id", doc.ID, "stage", what, "err", err) |
| - return nil |
| + return previewEntry{} |
| } |
| h.logger.Error("docs: "+what+" for a list thumbnail", "doc_id", doc.ID, "err", err) |
| - h.previews.put(doc.ID, nanos, nil) |
| - return nil |
| + h.previews.put(doc.ID, nanos, previewEntry{}) |
| + return previewEntry{} |
| } |
| |
| structure, err := h.store.JSON(ctx, doc) |
| @@ -246,9 +256,51 @@ func (h *handlers) thumbnail(ctx context.Context, doc *Document) []previewBar { |
| return remember("parse structure", err) |
| } |
| |
| - b := bars(export.PageOne(parsed)) |
| - h.previews.put(doc.ID, nanos, b) |
| - return b |
| + e := previewEntry{bars: bars(export.PageOne(parsed)), excerpt: excerpt(parsed.Root)} |
| + h.previews.put(doc.ID, nanos, e) |
| + return e |
| +} |
| + |
| +// excerptRunes is about two lines of a list row, enough that the one |
| +// line the row shows is always full; the stylesheet does the cutting. |
| +const excerptRunes = 240 |
| + |
| +// excerpt is a document's opening words, for the line under its title |
| +// in the list: the text of its blocks in order, headings skipped (a |
| +// heading is usually the title again, or a section name that says less |
| +// than the paragraph under it), joined with spaces and cut at a word |
| +// near excerptRunes. "" when the document has no body text. |
| +func excerpt(root export.Node) string { |
| + var b strings.Builder |
| + var walk func(n export.Node) bool |
| + walk = func(n export.Node) bool { |
| + if n.Type == "heading" { |
| + return true |
| + } |
| + if n.IsText() { |
| + b.WriteString(n.Text) |
| + return utf8.RuneCountInString(b.String()) < excerptRunes |
| + } |
| + for _, c := range n.Content { |
| + if !walk(c) { |
| + return false |
| + } |
| + } |
| + if n.Type != "doc" && b.Len() > 0 && !strings.HasSuffix(b.String(), " ") { |
| + b.WriteByte(' ') |
| + } |
| + return true |
| + } |
| + walk(root) |
| + text := strings.Join(strings.Fields(b.String()), " ") |
| + if utf8.RuneCountInString(text) <= excerptRunes { |
| + return text |
| + } |
| + cut := []rune(text)[:excerptRunes] |
| + if i := strings.LastIndexByte(string(cut), ' '); i > excerptRunes/2 { |
| + return string(cut)[:i] + "…" |
| + } |
| + return string(cut) + "…" |
| } |
| |
| // thumbnailBudget is how many documents one list render will lay out |
| @@ -282,11 +334,11 @@ const thumbnailBudget = 40 |
| func (h *handlers) thumbnailer(ctx context.Context) func(*Document) thumbView { |
| budget := thumbnailBudget |
| return func(d *Document) thumbView { |
| - if b, ok := h.previews.get(d.ID, d.UpdatedAt.UnixNano()); ok { |
| - return newThumbView(b) |
| + if e, ok := h.previews.get(d.ID, d.UpdatedAt.UnixNano()); ok { |
| + return newThumbView(e) |
| } |
| if budget <= 0 { |
| - return newThumbView(nil) |
| + return newThumbView(previewEntry{}) |
| } |
| budget-- |
| return newThumbView(h.thumbnail(ctx, d)) |
| @@ -305,8 +357,12 @@ func (h *handlers) thumbnailer(ctx context.Context) func(*Document) thumbView { |
| type thumbView struct { |
| Bars []previewBar |
| W, H int |
| + // Excerpt is the document's opening words, the line under a list |
| + // row's title. It travels with the bars because it comes from the |
| + // same parse and lives in the same cache entry. |
| + Excerpt string |
| } |
| |
| -func newThumbView(b []previewBar) thumbView { |
| - return thumbView{Bars: b, W: int(export.PageWidthPt), H: int(export.PageHeightPt)} |
| +func newThumbView(e previewEntry) thumbView { |
| + return thumbView{Bars: e.bars, W: int(export.PageWidthPt), H: int(export.PageHeightPt), Excerpt: e.excerpt} |
| } |