Line data Source code
1 : package designdoc
2 :
3 : import (
4 : _ "embed"
5 : "errors"
6 : "fmt"
7 : "strings"
8 : "text/template"
9 : )
10 :
11 : // statusSectionTmplSrc は状況テーブルの日本語ラベルを持つ雛形。
12 : // 日本語ドキュメントの体裁を維持するため、リテラルを Go ソースから外して埋め込みで持つ。
13 : //
14 : //go:embed status_section.tmpl
15 : var statusSectionTmplSrc string
16 :
17 : // statusSectionTmpl は埋め込んだ雛形を解析したもの。名前付きテンプレートを個別に実行する。
18 : var statusSectionTmpl = template.Must(template.New("status_section").Parse(statusSectionTmplSrc))
19 :
20 : // statusDisplayOrder は状況テーブルで status を並べる順。着手対象を上に置く。
21 : var statusDisplayOrder = []Status{
22 : StatusInProgress, StatusAccepted, StatusDraft, StatusDone, StatusSuperseded, StatusDropped,
23 : }
24 :
25 : // errTemplateNotFound は名前付きテンプレートが雛形に無いときに返す
26 : var errTemplateNotFound = errors.New("template not found")
27 :
28 : // renderLabel は名前付きテンプレートを data で実行した文字列を返す。
29 : // テンプレートは埋め込みでゴールデンテストが担保するため、実行時に失敗し得ない。
30 : func renderLabel(name string, data any) string {
31 1 : if statusSectionTmpl.Lookup(name) == nil {
32 1 : panic(fmt.Errorf("%w: %q", errTemplateNotFound, name))
33 : }
34 1 : var b strings.Builder
35 1 : if err := statusSectionTmpl.ExecuteTemplate(&b, name, data); err != nil {
36 0 : panic(err)
37 : }
38 :
39 1 : return b.String()
40 : }
41 :
42 : // RenderStatusSection は README に埋め込む未完了ドキュメントの一覧を Markdown で返す。
43 : // docs は表示したい順に並んでいる前提。載せるのはアクションが要る status、すなわち Status.IsOpen が
44 : // true のものに限る。status 別の件数テーブルは出さない。done が増えるたびに変わり、ブランチ間で
45 : // 頻繁に衝突するため。
46 : func RenderStatusSection(docs []*Document) string {
47 1 : var b strings.Builder
48 1 : b.WriteString(renderLabel("header", nil))
49 1 : found := false
50 1 : for _, s := range statusDisplayOrder {
51 1 : if !s.IsOpen() {
52 1 : continue
53 : }
54 1 : for _, d := range docs {
55 1 : if d.Front.Status != s {
56 1 : continue
57 : }
58 1 : found = true
59 1 : fmt.Fprintf(&b, "| %s | %s | %s | %s |\n",
60 1 : d.Front.Status, titleCell(d), progressCell(d), strings.Join(d.Front.Tags, ", "))
61 : }
62 : }
63 1 : if !found {
64 1 : b.WriteString(renderLabel("empty", nil))
65 1 : }
66 :
67 1 : return b.String()
68 : }
69 :
70 : // titleCell はドキュメントのセルを返す。パスがあればタイトルをファイルへのリンクにする。
71 : // タイトルが空なら空セルにせずパスで代替し、追跡できるようにする。
72 : func titleCell(d *Document) string {
73 1 : label := d.Title
74 1 : if label == "" {
75 1 : label = d.Path
76 1 : }
77 1 : if label == "" {
78 1 : return renderLabel("notitle", nil)
79 1 : }
80 1 : if d.Path != "" {
81 1 : return fmt.Sprintf("[%s](%s)", label, d.Path)
82 1 : }
83 :
84 1 : return label
85 : }
86 :
87 : // progressCell は進捗のセルを返す。分母は done+open で、見送りは分母から外して別に添える。
88 : func progressCell(d *Document) string {
89 1 : if !d.HasProgress {
90 1 : return "-"
91 1 : }
92 :
93 1 : s := fmt.Sprintf("%d/%d", d.DoneTasks, d.DoneTasks+d.OpenTasks)
94 1 : if d.SkippedTasks > 0 {
95 1 : s += renderLabel("skipped", d.SkippedTasks)
96 1 : }
97 :
98 1 : return s
99 : }
|