Brief names, sizes and themes for charts (#118)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
ae4865a028
commit
62d6b3fb9a
10 changed files with 256 additions and 22 deletions
|
|
@ -101,6 +101,9 @@ are to items the same person holds; another's is shown as a bare id.
|
|||
| Chart | A drawing of the kin around one person, the subject, from the register: rows by generation and the edges between them (`internal/chart`), drawn by `genealogy` to a directory with a `review.html` to judge by eye |
|
||||
| Hourglass chart | Ancestors above the subject, descendants below, the subject's row with siblings in birth order and spouses beside them: a drop-line chart, each couple joined by a bar and their children hung from it; influences as dashed curves as wide as their weight |
|
||||
| Generation | One row of a chart, numbered from the subject: −1 parents, +1 children |
|
||||
| Brief name | The shortest form a chart may call someone: the record's `short`, else `names.Brief` of the name, the name before any comma or bracket cut to its first word and surname (story 017) |
|
||||
| Size | How much of a name a chart's boxes carry: `brief` or `full` (`genealogy -size`) |
|
||||
| Theme | One drawing of a chart: `boxes`, the drop-line chart (`genealogy -theme`); the vine is #99 |
|
||||
| Event | A dated, placed moment: birth, death, burial, marriage |
|
||||
| Album, Photo | Pictures we may publish, with credit |
|
||||
| Resource | A document about the person the site publishes |
|
||||
|
|
|
|||
1
changes/118.added.md
Normal file
1
changes/118.added.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
Charts are brief by default: a record's `short`, else `names.Brief` of its name (the name before any comma or bracket, cut to first word and surname); `genealogy -size full` draws names as pages link them, and `-theme boxes[,…]` draws each subject in every theme named as `<id>-hourglass-<theme>.svg`, laid out together on the review page (#118)
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
// genealogy draws charts of the kin around people in the register, to a
|
||||
// directory, with a page laying them out to judge by eye (story 014).
|
||||
//
|
||||
// genealogy [-archive DIR | -people DIR] [-up N] [-down N] [-out DIR] ID...
|
||||
// genealogy [-archive DIR | -people DIR] [-up N] [-down N] [-size brief|full] [-theme boxes[,…]] [-out DIR] ID...
|
||||
//
|
||||
// The hourglass chart of each subject, ancestors up generations above and
|
||||
// descendants down generations below, as <id>-hourglass.svg, and
|
||||
// review.html naming them all. -people reads a bare register directory
|
||||
// (people/<dir>/person.yaml) rather than an archive instance.
|
||||
// descendants down generations below, in each theme named, as
|
||||
// <id>-hourglass-<theme>.svg, and review.html laying them out. -people
|
||||
// reads a bare register directory (people/<dir>/person.yaml) rather than
|
||||
// an archive instance.
|
||||
package main
|
||||
|
||||
import (
|
||||
|
|
@ -17,6 +18,8 @@ import (
|
|||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/archive"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/chart"
|
||||
|
|
@ -43,6 +46,8 @@ func run(args []string, stdout io.Writer) error {
|
|||
people := fs.String("people", "", "a bare register directory (people/<dir>/person.yaml) to read instead of the archive")
|
||||
up := fs.Int("up", 2, "generations of ancestors")
|
||||
down := fs.Int("down", 2, "generations of descendants")
|
||||
size := fs.String("size", string(chart.Brief), "how much of a name a box carries: brief or full")
|
||||
theme := fs.String("theme", "boxes", "the drawings, comma-separated: boxes")
|
||||
out := fs.String("out", "charts", "output directory")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return err
|
||||
|
|
@ -50,6 +55,20 @@ func run(args []string, stdout io.Writer) error {
|
|||
if fs.NArg() == 0 {
|
||||
return fmt.Errorf("no subject: give the ids of the people to chart")
|
||||
}
|
||||
opts := chart.Options{Up: *up, Down: *down, Size: chart.Size(*size)}
|
||||
if opts.Size != chart.Brief && opts.Size != chart.Full {
|
||||
return fmt.Errorf("size %q: want %s or %s", *size, chart.Brief, chart.Full)
|
||||
}
|
||||
var draw []drawing
|
||||
for _, name := range strings.Split(*theme, ",") {
|
||||
fn, ok := themes[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("theme %q: want one of boxes", name)
|
||||
}
|
||||
if !slices.ContainsFunc(draw, func(d drawing) bool { return d.name == name }) {
|
||||
draw = append(draw, drawing{name, fn})
|
||||
}
|
||||
}
|
||||
reg, err := load(*archiveDir, *people)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -72,11 +91,16 @@ func run(args []string, stdout io.Writer) error {
|
|||
}
|
||||
var page []reviewed
|
||||
for _, p := range subjects {
|
||||
name := p.ID.String() + "-hourglass.svg"
|
||||
if err := os.WriteFile(filepath.Join(*out, name), chart.SVG(chart.Hourglass(reg, p, *up, *down)), 0o644); err != nil {
|
||||
return err
|
||||
c := chart.Hourglass(reg, p, opts)
|
||||
r := reviewed{Name: p.Name, Years: p.Years()}
|
||||
for _, d := range draw {
|
||||
name := p.ID.String() + "-hourglass-" + d.name + ".svg"
|
||||
if err := os.WriteFile(filepath.Join(*out, name), d.fn(c), 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Files = append(r.Files, name)
|
||||
}
|
||||
page = append(page, reviewed{Name: p.Name, Years: p.Years(), File: name})
|
||||
page = append(page, r)
|
||||
}
|
||||
var review bytes.Buffer
|
||||
if err := reviewPage.Execute(&review, page); err != nil {
|
||||
|
|
@ -85,7 +109,7 @@ func run(args []string, stdout io.Writer) error {
|
|||
if err := os.WriteFile(filepath.Join(*out, "review.html"), review.Bytes(), 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Fprintf(stdout, "%d charts → %s/review.html\n", len(page), *out)
|
||||
_, err = fmt.Fprintf(stdout, "%d charts → %s/review.html\n", len(page)*len(draw), *out)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -101,8 +125,17 @@ func load(archiveDir, people string) (*person.Register, error) {
|
|||
return person.Load(filepath.Join(archiveDir, "people"))
|
||||
}
|
||||
|
||||
// A drawing is a theme: one way of drawing a chart.
|
||||
type drawing struct {
|
||||
name string
|
||||
fn func(chart.Chart) []byte
|
||||
}
|
||||
|
||||
var themes = map[string]func(chart.Chart) []byte{"boxes": chart.SVG}
|
||||
|
||||
type reviewed struct {
|
||||
Name, Years, File string
|
||||
Name, Years string
|
||||
Files []string // one per theme
|
||||
}
|
||||
|
||||
var reviewPage = template.Must(template.New("review").Parse(`<!doctype html>
|
||||
|
|
@ -116,12 +149,13 @@ body { font-family: system-ui, sans-serif; margin: 2rem; color-scheme: light dar
|
|||
h2 { font-weight: normal; margin: 2rem 0 .5rem; }
|
||||
h2 small { color: #777; }
|
||||
figure { margin: 0; overflow-x: auto; }
|
||||
.themes { display: flex; flex-wrap: wrap; gap: 2rem; align-items: flex-start; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Charts to review</h1>
|
||||
{{range .}}<h2>{{.Name}} {{with .Years}}<small>{{.}}</small>{{end}}</h2>
|
||||
<figure><img src="{{.File}}" alt="The hourglass chart of {{.Name}}"></figure>
|
||||
{{range .}}{{$name := .Name}}<h2>{{.Name}} {{with .Years}}<small>{{.}}</small>{{end}}</h2>
|
||||
<div class="themes">{{range .Files}}<figure><img src="{{.}}" alt="The hourglass chart of {{$name}}"></figure>{{end}}</div>
|
||||
{{end}}</body>
|
||||
</html>
|
||||
`))
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ func TestRunWritesAChartPerSubjectAndTheReviewPage(t *testing.T) {
|
|||
if err := run([]string{"-people", fixtures, "-out", out, "0", "3"}, &stdout); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"0-hourglass.svg", "3-hourglass.svg", "review.html"} {
|
||||
for _, name := range []string{"0-hourglass-boxes.svg", "3-hourglass-boxes.svg", "review.html"} {
|
||||
if _, err := os.Stat(filepath.Join(out, name)); err != nil {
|
||||
t.Errorf("%s not written: %v", name, err)
|
||||
}
|
||||
}
|
||||
review, _ := os.ReadFile(filepath.Join(out, "review.html"))
|
||||
for _, want := range []string{"0-hourglass.svg", "3-hourglass.svg", "Victoria Example", "Jean Example"} {
|
||||
for _, want := range []string{"0-hourglass-boxes.svg", "3-hourglass-boxes.svg", "Victoria Example", "Jean Example"} {
|
||||
if !strings.Contains(string(review), want) {
|
||||
t.Errorf("review.html lacks %q", want)
|
||||
}
|
||||
|
|
@ -45,3 +45,35 @@ func TestRunRefusesAnUnknownSubjectBeforeWriting(t *testing.T) {
|
|||
t.Error("run() with no subject = nil, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunRefusesAnUnknownThemeOrSizeBeforeWriting(t *testing.T) {
|
||||
for _, args := range [][]string{{"-theme", "vine"}, {"-theme", "boxes,vine"}, {"-size", "wide"}} {
|
||||
out := filepath.Join(t.TempDir(), "charts")
|
||||
err := run(append([]string{"-people", fixtures, "-out", out}, append(args, "0")...), &bytes.Buffer{})
|
||||
if err == nil || !strings.Contains(err.Error(), args[1][strings.LastIndex(args[1], ",")+1:]) {
|
||||
t.Errorf("run(%v) error = %v, want one naming %s", args, err, args[1])
|
||||
}
|
||||
if _, statErr := os.Stat(out); !os.IsNotExist(statErr) {
|
||||
t.Errorf("run(%v): %s exists, want nothing written", args, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunDrawsEachThemeOnce(t *testing.T) {
|
||||
out := filepath.Join(t.TempDir(), "charts")
|
||||
if err := run([]string{"-people", fixtures, "-out", out, "-theme", "boxes,boxes", "-size", "full", "8"}, &bytes.Buffer{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, _ := os.ReadDir(out)
|
||||
var names []string
|
||||
for _, e := range entries {
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
if got := strings.Join(names, " "); got != "8-hourglass-boxes.svg review.html" {
|
||||
t.Errorf("files = %q, want the theme once and the review page", got)
|
||||
}
|
||||
svg, _ := os.ReadFile(filepath.Join(out, "8-hourglass-boxes.svg"))
|
||||
if !strings.Contains(string(svg), "Robert Henry Example, 2nd of Elsewhere") {
|
||||
t.Errorf("-size full: chart lacks the full name")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
76
docs/stories/017-brief-names-sizes-and-themes-for-charts.md
Normal file
76
docs/stories/017-brief-names-sizes-and-themes-for-charts.md
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
# 017 — Brief names, sizes and themes for charts (#118)
|
||||
|
||||
**Status:** built
|
||||
**Roadmap:** towards #99, the genealogical vine: the plumbing a second
|
||||
drawing of a chart needs
|
||||
|
||||
## Why
|
||||
|
||||
A chart has little room for a name, and a vine (#99) less. Records with
|
||||
a `short` draw well; those without draw their full name, "John
|
||||
Drummond, 10th of Megginch, 15th Baron Strange", and the box has to be
|
||||
as wide as the longest. A chart should be brief by default and full on
|
||||
request, and the command should be able to draw the same chart in more
|
||||
than one theme side by side, so a new drawing can be judged against the
|
||||
boxes.
|
||||
|
||||
## Vocabulary
|
||||
|
||||
| Term | Meaning |
|
||||
|---|---|
|
||||
| Brief name | The shortest form a chart may call someone: the record's `short`, else `names.Brief` of the name: the name before any comma or bracket, cut to its first word and its surname (the last word with any lowercase particles before it) |
|
||||
| Size | How much of a name a chart's boxes carry: `brief` (the brief name) or `full` (the `short`, else the whole name, as pages link) |
|
||||
| Theme | One drawing of a chart: `boxes`, the drop-line chart of story 014; the vine is #99 |
|
||||
|
||||
## What changes
|
||||
|
||||
- `names.Brief(name)`: the brief form of a name by the rule above.
|
||||
- `chart.Hourglass(reg, subject, opts)`: `Options{Up, Down, Size}`
|
||||
replaces the two ints; the box's Label is by Size.
|
||||
- `genealogy` gains `-size brief|full` (brief) and `-theme boxes[,…]`
|
||||
(boxes), writing `<id>-hourglass-<theme>.svg` per subject and theme
|
||||
and laying each subject's themes out together on `review.html`; an
|
||||
unknown theme is refused before anything is written.
|
||||
|
||||
## Acceptance
|
||||
|
||||
| Name | Brief |
|
||||
|---|---|
|
||||
| Victoria Alexandrina Drummond | Victoria Drummond |
|
||||
| Geraldine Margaret Drummond (née Tyssen-Amherst) | Geraldine Drummond |
|
||||
| John Drummond, 10th of Megginch, 15th Baron Strange | John Drummond |
|
||||
| Vincent van Gogh | Vincent van Gogh |
|
||||
| Ludwig van Beethoven | Ludwig van Beethoven |
|
||||
| Mary | Mary |
|
||||
| 李白 | 李白 |
|
||||
| Charles Simpson Borjes | Charles Borjes |
|
||||
|
||||
| Record | Size | Label |
|
||||
|---|---|---|
|
||||
| `short` Bob | brief or full | Bob |
|
||||
| no `short`, "Robert Henry Example, 2nd of Elsewhere" | brief | Robert Example |
|
||||
| no `short`, "Robert Henry Example, 2nd of Elsewhere" | full | Robert Henry Example, 2nd of Elsewhere |
|
||||
|
||||
| Invocation | Outcome |
|
||||
|---|---|
|
||||
| `-theme boxes 0` | `0-hourglass-boxes.svg`, `review.html` |
|
||||
| `-theme boxes,boxes 0` | the theme once |
|
||||
| `-theme vine 0` | refused naming `vine`, nothing written (until #99) |
|
||||
| `-size wide 0` | refused naming `wide` |
|
||||
|
||||
## Out of scope
|
||||
|
||||
- The vine theme (#99, the next story).
|
||||
- Wrapping a name over two lines.
|
||||
|
||||
## Decisions
|
||||
|
||||
- A brief name is derived by rule only when the record has no `short`:
|
||||
a rule that misfires on a name is put right by data, not code.
|
||||
|
||||
## Done when
|
||||
|
||||
- [x] tests express the acceptance tables and pass
|
||||
- [x] README: vocabulary (Brief name, Size, Theme)
|
||||
- [x] changelog entry
|
||||
- [ ] issue closed with a link to the release
|
||||
|
|
@ -7,6 +7,7 @@ package chart
|
|||
import (
|
||||
"sort"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/names"
|
||||
"git.bytestone.uk/hum3/life-weaver/person"
|
||||
"git.bytestone.uk/hum3/life-weaver/pid"
|
||||
)
|
||||
|
|
@ -67,16 +68,31 @@ type Edge struct {
|
|||
Weight float64 // InfluencedBy only, above 0 and up to 1
|
||||
}
|
||||
|
||||
// Hourglass places the subject's ancestors up generations above them and
|
||||
// their descendants down generations below, a drop-line chart: the
|
||||
// Size is how much of a name a chart's boxes carry.
|
||||
type Size string
|
||||
|
||||
const (
|
||||
Brief Size = "brief" // the record's short, else names.Brief of the name; the zero Size
|
||||
Full Size = "full" // the record's short, else the whole name, as pages link
|
||||
)
|
||||
|
||||
// Options shape an hourglass chart: generations up and down, and the size.
|
||||
type Options struct {
|
||||
Up, Down int
|
||||
Size Size
|
||||
}
|
||||
|
||||
// Hourglass places the subject's ancestors opts.Up generations above them
|
||||
// and their descendants opts.Down generations below, a drop-line chart: the
|
||||
// subject's own row holds their siblings in birth order with their spouses
|
||||
// beside them; each parent is centred over their own ancestors and each
|
||||
// couple over their children. Siblings' children and descendants' spouses
|
||||
// are not drawn. The subject's influences are edges to the influence where
|
||||
// they are placed as kin, else a box after the parents; an ancestor beyond
|
||||
// the chart's reach is left out, influence or not.
|
||||
func Hourglass(reg *person.Register, subject *person.Person, up, down int) Chart {
|
||||
b := &builder{reg: reg, rows: map[int][]Box{}, placed: map[pid.ID]bool{}}
|
||||
func Hourglass(reg *person.Register, subject *person.Person, opts Options) Chart {
|
||||
up, down := opts.Up, opts.Down
|
||||
b := &builder{reg: reg, size: opts.Size, rows: map[int][]Box{}, placed: map[pid.ID]bool{}}
|
||||
f := reg.Family(subject)
|
||||
|
||||
// Row 0: siblings in birth order, the subject among them, spouses right after the subject.
|
||||
|
|
@ -160,6 +176,7 @@ func Hourglass(reg *person.Register, subject *person.Person, up, down int) Chart
|
|||
|
||||
type builder struct {
|
||||
reg *person.Register
|
||||
size Size
|
||||
rows map[int][]Box
|
||||
placed map[pid.ID]bool
|
||||
boxes map[pid.ID]Box
|
||||
|
|
@ -172,12 +189,21 @@ func (b *builder) place(p *person.Person, gen int, x float64, role Role) {
|
|||
if b.boxes == nil {
|
||||
b.boxes = map[pid.ID]Box{}
|
||||
}
|
||||
box := Box{ID: p.ID, Label: p.Label(), Years: p.Years(), Gender: p.Gender, Role: role, X: x}
|
||||
box := Box{ID: p.ID, Label: b.label(p), Years: p.Years(), Gender: p.Gender, Role: role, X: x}
|
||||
b.rows[gen] = append(b.rows[gen], box)
|
||||
b.placed[p.ID] = true
|
||||
b.boxes[p.ID] = box
|
||||
}
|
||||
|
||||
// label is p's name at the builder's size: the record's short if it has
|
||||
// one, else the whole name at Full and names.Brief of it otherwise.
|
||||
func (b *builder) label(p *person.Person) string {
|
||||
if p.Short != "" || b.size == Full {
|
||||
return p.Label()
|
||||
}
|
||||
return names.Brief(p.Name)
|
||||
}
|
||||
|
||||
// ancestor reports whether id is an ancestor of p at any depth.
|
||||
func (b *builder) ancestor(p *person.Person, id pid.ID, seen map[pid.ID]bool) bool {
|
||||
for _, parent := range b.reg.Family(p).Parents {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,26 @@ func hourglass(t *testing.T, reg *person.Register, subject pid.ID, up, down int)
|
|||
if !ok {
|
||||
t.Fatalf("no record %s", subject)
|
||||
}
|
||||
return Hourglass(reg, p, up, down)
|
||||
return Hourglass(reg, p, Options{Up: up, Down: down})
|
||||
}
|
||||
|
||||
// A box's label is by size: the record's short either way, else the brief
|
||||
// name or, at full size, the whole name (Bob's record has no short).
|
||||
func TestHourglassLabelBySize(t *testing.T) {
|
||||
reg := load(t)
|
||||
ann, _ := reg.Get(7)
|
||||
for _, c := range []struct {
|
||||
size Size
|
||||
bob, jean string
|
||||
}{{Brief, "Robert Example", "Jean"}, {"", "Robert Example", "Jean"}, {Full, "Robert Henry Example, 2nd of Elsewhere", "Jean"}} {
|
||||
ch := Hourglass(reg, ann, Options{Up: 1, Size: c.size})
|
||||
if got := box(ch, 8).Label; got != c.bob {
|
||||
t.Errorf("size %q: Bob's label = %q, want %q", c.size, got, c.bob)
|
||||
}
|
||||
if got := box(ch, 3).Label; got != c.jean {
|
||||
t.Errorf("size %q: Jean's label = %q, want %q", c.size, got, c.jean)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// rows writes a chart's rows as "gen: ids left to right | gen: …".
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
name: Bob Example
|
||||
short: Bob
|
||||
name: Robert Henry Example, 2nd of Elsewhere
|
||||
gender: male
|
||||
|
|
|
|||
|
|
@ -106,3 +106,28 @@ func Keys(name string) []string {
|
|||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// Brief is the shortest form a chart may call someone by (story 017): the
|
||||
// name before any comma or bracket (titles and a maiden name follow
|
||||
// those), cut to its first word and its surname, the last word with any
|
||||
// lowercase particles before it: "Vincent van Gogh" keeps its van. A
|
||||
// name of one word is itself.
|
||||
func Brief(name string) string {
|
||||
if i := strings.IndexAny(name, ",("); i >= 0 {
|
||||
name = name[:i]
|
||||
}
|
||||
words := strings.Fields(name)
|
||||
if len(words) <= 2 {
|
||||
return strings.Join(words, " ")
|
||||
}
|
||||
surname := len(words) - 1
|
||||
for surname > 1 && isLower(words[surname-1]) {
|
||||
surname--
|
||||
}
|
||||
return words[0] + " " + strings.Join(words[surname:], " ")
|
||||
}
|
||||
|
||||
func isLower(word string) bool {
|
||||
r := []rune(word)[0]
|
||||
return unicode.IsLetter(r) && unicode.IsLower(r)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,3 +98,21 @@ func TestKey(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrief(t *testing.T) {
|
||||
for _, c := range []struct{ in, want string }{
|
||||
{"Victoria Alexandrina Drummond", "Victoria Drummond"},
|
||||
{"Geraldine Margaret Drummond (née Tyssen-Amherst)", "Geraldine Drummond"},
|
||||
{"John Drummond, 10th of Megginch, 15th Baron Strange", "John Drummond"},
|
||||
{"Vincent van Gogh", "Vincent van Gogh"},
|
||||
{"Ludwig van Beethoven", "Ludwig van Beethoven"},
|
||||
{"Mary", "Mary"},
|
||||
{"李白", "李白"},
|
||||
{"Charles Simpson Borjes", "Charles Borjes"},
|
||||
{" Mary Smith ", "Mary Smith"},
|
||||
} {
|
||||
if got := Brief(c.in); got != c.want {
|
||||
t.Errorf("Brief(%q) = %q, want %q", c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue