manus-tracker

tracker for scientific manuscripts
git clone git://src.adamsgaard.dk/manus-tracker # fast
git clone https://src.adamsgaard.dk/manus-tracker.git # slow
Log | Files | Refs | README Back to index

report.go (3449B)


      1 package scan
      2 
      3 import (
      4 	"fmt"
      5 	"os"
      6 	"path/filepath"
      7 	"slices"
      8 
      9 	"manus/bib"
     10 )
     11 
     12 // SharedBib is the common bibliography in the manuscript root.
     13 const SharedBib = "BIBnew.bib"
     14 
     15 // BibFiles resolves the bib files a manuscript uses. A file missing from
     16 // src but present in root (the copied BIBnew.bib) resolves to root.
     17 func BibFiles(root, src string, names []string) []string {
     18 	var out []string
     19 	for _, n := range names {
     20 		p := filepath.Join(src, n)
     21 		if filepath.Base(n) == SharedBib || !exists(p) {
     22 			if q := filepath.Join(root, filepath.Base(n)); exists(q) {
     23 				p = q
     24 			}
     25 		}
     26 		if !slices.Contains(out, p) {
     27 			out = append(out, p)
     28 		}
     29 	}
     30 	return out
     31 }
     32 
     33 type BibReport struct {
     34 	Files   []string
     35 	Errors  []string
     36 	Cited   int
     37 	Missing []string
     38 	Unused  []string
     39 	NoDOI   []string
     40 }
     41 
     42 // CheckBib compares cite keys against the manuscript's bib files. Unused
     43 // entries are reported for manuscript-local files only.
     44 func CheckBib(root, src string, tex TeX) BibReport {
     45 	r := BibReport{Files: BibFiles(root, src, tex.Bibs), Cited: len(tex.Cites)}
     46 	all := map[string]*bib.Entry{}
     47 	local := map[string]bool{}
     48 	for _, f := range r.Files {
     49 		es, err := bib.ParseFile(f)
     50 		if err != nil {
     51 			r.Errors = append(r.Errors, fmt.Sprintf("%s: %v", filepath.Base(f), err))
     52 		}
     53 		isShared := filepath.Dir(f) == filepath.Clean(root)
     54 		for _, e := range es {
     55 			if _, ok := all[e.Key]; !ok {
     56 				all[e.Key] = e
     57 			}
     58 			if !isShared {
     59 				local[e.Key] = true
     60 			}
     61 		}
     62 	}
     63 	cited := map[string]bool{}
     64 	for _, k := range tex.Cites {
     65 		cited[k] = true
     66 		e, ok := all[k]
     67 		switch {
     68 		case !ok:
     69 			r.Missing = append(r.Missing, k)
     70 		case e.DOI() == "" && e.Type == "article":
     71 			r.NoDOI = append(r.NoDOI, k)
     72 		}
     73 	}
     74 	for k := range local {
     75 		if !cited[k] {
     76 			r.Unused = append(r.Unused, k)
     77 		}
     78 	}
     79 	slices.Sort(r.Unused)
     80 	return r
     81 }
     82 
     83 // Report gathers all previews for one manuscript.
     84 type Report struct {
     85 	Git       *Git
     86 	Targets   []string
     87 	Figures   []Figure
     88 	PDF       *PDF
     89 	TeX       TeX
     90 	Bib       *BibReport
     91 	Conflicts []string
     92 }
     93 
     94 // Inspect builds a report; commits limits the git log length.
     95 func Inspect(root, dir, src, main, repo string, commits int) Report {
     96 	r := Report{
     97 		Git:       GitInfo(repo, commits),
     98 		Targets:   MakeTargets(src),
     99 		Figures:   Figures(src),
    100 		PDF:       PDFStatus(src, main),
    101 		Conflicts: Conflicts(dir),
    102 	}
    103 	if filepath.Ext(main) == ".tex" {
    104 		r.TeX = ParseTeX(src, main)
    105 		br := CheckBib(root, src, r.TeX)
    106 		r.Bib = &br
    107 	}
    108 	return r
    109 }
    110 
    111 // Flags summarises problems worth attention.
    112 func (r Report) Flags() []string {
    113 	var f []string
    114 	if r.Git != nil && r.Git.Dirty > 0 {
    115 		f = append(f, fmt.Sprintf("dirty:%d", r.Git.Dirty))
    116 	}
    117 	if r.Git != nil && r.Git.Ahead > 0 {
    118 		f = append(f, fmt.Sprintf("unpushed:%d", r.Git.Ahead))
    119 	}
    120 	if r.PDF != nil && r.PDF.Missing {
    121 		f = append(f, "no-pdf")
    122 	} else if r.PDF != nil && r.PDF.Stale {
    123 		f = append(f, "stale-pdf")
    124 	}
    125 	if r.Bib != nil && len(r.Bib.Missing) > 0 {
    126 		f = append(f, fmt.Sprintf("missing-refs:%d", len(r.Bib.Missing)))
    127 	}
    128 	if len(r.TeX.Todos) > 0 {
    129 		f = append(f, fmt.Sprintf("todos:%d", len(r.TeX.Todos)))
    130 	}
    131 	if len(r.Conflicts) > 0 {
    132 		f = append(f, fmt.Sprintf("conflicts:%d", len(r.Conflicts)))
    133 	}
    134 	return f
    135 }
    136 
    137 // Dirs lists non-hidden subdirectories of root.
    138 func Dirs(root string) []string {
    139 	entries, _ := os.ReadDir(root)
    140 	var out []string
    141 	for _, e := range entries {
    142 		if e.IsDir() && e.Name()[0] != '.' {
    143 			out = append(out, e.Name())
    144 		}
    145 	}
    146 	return out
    147 }