Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/glacials/winter. a thoughtful static website generator for long-lasting content twos.dev/winter
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940// Package document supplies a static website generator.// It has these goals://// 1. New content must be easy to edit. Old content must be hard to break.// 2. Be replaceable. Special syntax should look fine as plaintext if moved elsewhere.//// Winter is part of the [TadiWeb].//// # Easy to edit, hard to break//// To accomplish its first goal, Winter employs two mechanisms.//// First, content managed by Winter is either warm or cold.// Warm content is welcome to be synchronized into a Winter-managed directory by external tools,// such as your mobile text editor or note-taking app of choice.// These external tools and their synchronization steps are not provided by Winter,// and they are not required to use it,// but Winter's design goals assume they exist.// Warm content is content you are actively working on.// It is not ready to be listed anywhere,// but it is published to a page on your website so you can view it in context and share it with friends for review.//// Cold content is finished content. [Cool URIs don't change.]// When your warm content is ready to be published,// you should freeze it://// winter freeze src/cold/my_post.md//// This converts it into cold content.// The most important difference between the two is that your synchronization tools never, ever touch cold content.// Sychronization tools, jobs, and scripts are run frequently and probably aren't your most robust pipelines.// To limit the blast radius of any bugs they experience,// content that you don't expect to edit often must be moved away from the directories they control.// This is cold content.//// Cold content is also guaranteed by Winter's quality checks.// When a piece of content becomes cold, Winter commits its URL to memory and ensures that URL stays accessible in future runs.// If ever a URL that once was accessible becomes inaccessible, Winter alerts you and prevents that build from succeeding.// This protects cold content from you, your tools, and from Winter's current and future versions.//// # No lock-in//// To accomplish its second goal, Winter provides cherries on top of Markdown.// However, using the same Markdown with any other static website generator results in no degradation.// Just like Markdown itself, Winter Markdown looks great when not parsed.//// # Galleries//// A block containing only images is treated as a gallery,// with the images placed in a responsive grid.// Images can be clicked to zoom in or out.//// // //// # Image captions//// A block of all-italic text immediately below an image or gallery is treated as a caption,// and given a special visual treatment and accessibility structure.//// // //// _My cat and dog like to play with each other._//// # Photos as first-class citizens//// For photographers, Winter supports photographs as first-class citizens.// EXIF data is automatically extracted and can be displayed using template variables.// Photos organized into galleries display next to each other neatly,// with a built-in lightbox.//// # Photo EXIF safety//// Any photos Winter processes that have GPS data embedded in them are loudly rejected,// failing the build.//// # Reduced load times//// Images and references to them are automatically converted into WebP format and several thumbnails are generated for each.// Each image renders with [<img srcset>] to ensure only the smallest possible image that saturates the display density is loaded.//// # LaTeX//// Surround text in $dollar signs$ to render LaTeX,// implemented via [KaTeX].//// $\LaTeX$ users rejoice!//// # Tables of contents//// A table of contents can be requested by setting the toc variable to true in frontmatter.// Tables of contents are always rendered immediately above the first level-2 heading.//// ---// toc: true// ---//// # My Article//// (table of contents will be rendered here)//// ## My Cat//// ...//// ## My Dog//// ...//// # Syntax highlighting//// Fenced code blocks that specify a language are syntax-highlighted.//// ```go// package main// func main() {// fmt.Println("I'm syntax highlighted!")// }// ```//// # External links in new tabs//// Any links that navigate to external websites will automatically have a target=_blank set during generation.//// # Dark mode images//// If an image's extensionless filename ends in "-dark" or "-light",// and another image exists at the same path but with the opposite suffix,// the correct one will be rendered to the user based on their light-/dark-mode preference.//// [<img srcset>]: https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images// [Cool URIs don't change.]: https://www.w3.org/Provider/Style/URI// [KaTeX]: https://katex.org// [TadiWeb]: https://www.tadiweb.compackage document // import "twos.dev/winter/document"
import ( "bytes" "errors" "fmt" "io" "log/slog" "net/url" "os" "path/filepath" "regexp" "sort" "strings"
"github.com/yargevad/filepathx")
const ( AppName = "winter"
htmlSuffix = ".html" markdownSuffix = ".md" orgSuffix = ".org" templateSuffix = ".html.tmpl")
var ( galName = regexp.MustCompile( `img\/.+\/(.+)/.*`, )
// ignorePaths are file path regex patterns that should not be treated as documents to be generated, // even though they are in source directories. // The regex is a full match so must start with ^ and end with $. // // TODO: Migrate some of these to a directory blocklist instead. ignorePaths = map[*regexp.Regexp]struct{}{ regexp.MustCompile(`(^\.)|(^.*\/\.)\.[^\/]*$`): {}, regexp.MustCompile(`^(.*\/)?#.*$`): {}, regexp.MustCompile(`^(.*\/)?README.md$`): {}, regexp.MustCompile(`^(.*\/)?.DS_Store$`): {}, regexp.MustCompile(`^(.*\/)?imgcontainer.html.tmpl$`): {}, regexp.MustCompile(`^(.*\/)?text_document.html.tmpl$`): {}, // regexp.MustCompile(`^(.*\/)?_gallery.html.tmpl$`): {}, // regexp.MustCompile(`^(.*\/)?_icon.html.tmpl$`): {}, // regexp.MustCompile(`^(.*\/)?_imgs.html.tmpl$`): {}, // regexp.MustCompile(`^(.*\/)?_nav.html.tmpl$`): {}, // regexp.MustCompile(`^(.*\/)?_subtoc.html.tmpl$`): {}, // regexp.MustCompile(`^(.*\/)?_toc.html.tmpl$`): {}, // regexp.MustCompile(`^(.*\/)?_videos.html.tmpl$`): {}, } // ignoreDirs are file path directories from which no documents should be generated. ignoreDirs = map[string]struct{}{ "src/templates/": {}, } pad = newPadder())
type ErrNotTracked struct{ path string }
func (err ErrNotTracked) Error() string { return fmt.Sprintf( "%s is not tracked; restart to track", err.path, )}
// Substructure is a graph of documents on the website.type Substructure struct { // cfg holds user preferences, specified by winter.yml. cfg *Config // devURL is [twos.dev/winter.Substructure.cfg.Development.URL] unmarshaled into a [net/url.URL]. devURL *url.URL // docs holds the documents known to the substructure. docs *documents // galleries is a map of gallery name to slice of galleries in that gallery. galleries map[string][]*img}
// NewSubstructure returns a substructure with the given configuration.// Upon initialization, a substructure is the result of a discovery phase of content on the filesystem.// Further calls are needed to build the full graph of content and render it to HTML.func NewSubstructure(cfg *Config) (*Substructure, error) { devURL, err := url.Parse(cfg.Development.URL) if err != nil { return nil, fmt.Errorf( "cannot parse config development.url %q: %w", cfg.Development.URL, err, ) } s := Substructure{ cfg: cfg, devURL: devURL, docs: &documents{}, } return &s, s.discover()}
// DocumentCount returns the number of documents known to the substructure.func (s *Substructure) DocumentCount() int { if s.docs == nil { return 0 } return len(s.docs.All)}
// ImageCount returns the total number of images across all galleries.func (s *Substructure) ImageCount() int { total := 0 for _, imgs := range s.galleries { total += len(imgs) } return total}
// Build builds doc.//// To also build downstream dependencies, use [Rebuild] instead.func (s *Substructure) Build(doc Document) error { if _, ok := doc.(*StaticDocument); ok { return s.buildWWW(doc) } if err := s.load(doc); err != nil { return err } return s.render(doc)}
func (s *Substructure) load(doc Document) error { r, err := os.Open(doc.Metadata().SourcePath) if err != nil { return fmt.Errorf( "cannot read %q for building %q: %w", doc.Metadata().SourcePath, doc.Metadata().Title, err, ) } defer r.Close() if err := doc.Load(r); err != nil { return fmt.Errorf( "cannot load %q for building %q: %w", doc.Metadata().SourcePath, doc.Metadata().Title, err, ) } return nil}
func (s *Substructure) render(doc Document) error { if err := s.buildWWW(doc); err != nil { return err } if err := s.buildGemini(doc); err != nil { return err }
return nil}
func (s *Substructure) buildWWW(doc Document) error { dest := filepath.Join(s.cfg.Dist, doc.Metadata().WebPath) slog.Debug(fmt.Sprintf(" → %s", pad(dest))) if staticDoc, ok := doc.(*StaticDocument); ok { if err := copyFileIfChanged(staticDoc.SourcePath, dest); err != nil { return fmt.Errorf( "cannot copy static file %q to %q: %w", staticDoc.SourcePath, dest, err, ) } return nil } if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil { return fmt.Errorf("cannot make directory structure for %q: %w", dest, err) } var buf bytes.Buffer if err := doc.Render(&buf); err != nil { return fmt.Errorf( "cannot render %q for building: %w", doc.Metadata().SourcePath, err, ) } if err := writeFileIfChanged(dest, buf.Bytes(), 0o644); err != nil { return fmt.Errorf( "cannot build %q (web path %q) into %q: %w", doc.Metadata().SourcePath, doc.Metadata().WebPath, dest, err, ) } return nil}
func (s *Substructure) buildGemini(doc Document) error { if doc.Metadata().GeminiPath == "" { slog.Debug( fmt.Sprintf( "Skipping building %s into Gemini file", doc.Metadata().SourcePath, ), ) return nil } dest := filepath.Join(s.cfg.Dist, doc.Metadata().GeminiPath) slog.Debug(fmt.Sprintf(" → %s", pad(dest))) if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil { return fmt.Errorf("cannot make directory structure for %q: %w", dest, err) } var buf bytes.Buffer if gr, ok := doc.(GeminiRenderer); ok { if err := gr.RenderGemini(&buf); err != nil { return fmt.Errorf( "cannot render %q for building: %w", doc.Metadata().SourcePath, err, ) } } if err := writeFileIfChanged(dest, buf.Bytes(), 0o644); err != nil { return fmt.Errorf( "cannot build %q into %s: %w", doc.Metadata().SourcePath, filepath.Join(dest, doc.Metadata().WebPath), err, ) } return nil}
func writeFileIfChanged(path string, contents []byte, mode os.FileMode) error { existing, err := os.ReadFile(path) if err == nil && bytes.Equal(existing, contents) { return nil } if err != nil && !errors.Is(err, os.ErrNotExist) { return err } if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { return err } tmp, err := os.CreateTemp(filepath.Dir(path), ".winter-write-*") if err != nil { return err } tmpPath := tmp.Name() defer os.Remove(tmpPath) if err := tmp.Chmod(mode); err != nil { _ = tmp.Close() return err } if _, err := tmp.Write(contents); err != nil { _ = tmp.Close() return err } if err := tmp.Close(); err != nil { return err } return os.Rename(tmpPath, path)}
func copyFileIfChanged(source, destination string) error { sourceHash, err := fileHash(source) if err != nil { return err } if _, err := os.Stat(destination); err == nil { destinationHash, err := fileHash(destination) if err != nil { return err } if sourceHash == destinationHash { return nil } } else if !errors.Is(err, os.ErrNotExist) { return err } if err := os.MkdirAll(filepath.Dir(destination), 0o755); err != nil { return err } src, err := os.Open(source) if err != nil { return err } defer src.Close() tmp, err := os.CreateTemp(filepath.Dir(destination), ".winter-copy-*") if err != nil { return err } tmpPath := tmp.Name() defer os.Remove(tmpPath) if _, err := io.Copy(tmp, src); err != nil { _ = tmp.Close() return err } if err := tmp.Close(); err != nil { return err } return os.Rename(tmpPath, destination)}
// DocBySourcePath returns the document that originated from the file at path.//// If no such document exists, ok is false.func (s *Substructure) DocBySourcePath(path string) (doc Document, ok bool) { for _, doc := range s.docs.All { if filepath.Clean(doc.Metadata().SourcePath) == filepath.Clean(path) { return doc, true } } return nil, false}
// ExecuteAll builds all documents known to the substructure,// as well as any site-scoped non-documents such as RSS feeds.func (s *Substructure) ExecuteAll(dist string) error { manifest, err := loadBuildManifest(dist) if err != nil { return err } manifest.markAllOrphaned()
builtIMGs := map[string]*img{} for _, gallery := range s.galleries { for _, im := range gallery { if prev, ok := builtIMGs[im.WebPath]; ok { return fmt.Errorf( "both %s (%T) and %q (%T) wanted to build to %q/%q; remove one", im.SourcePath, im, prev.SourcePath, prev, s.cfg.Production.URL, im.WebPath, ) } hash, err := fileHash(im.SourcePath) if err != nil { return err } if err := im.loadMetadataFromSource(); err != nil { return wrapErrorf(err, "cannot load metadata for %q", im.SourcePath) } if err := im.loadThumbnailMetadata(); err != nil { return fmt.Errorf("cannot load thumbnails for %q: %w", im.SourcePath, err) } outputs := imageOutputPaths(im) if manifest.isFresh(im.SourcePath, hash, imageGeneratorKey, outputs, dist) { slog.Debug(fmt.Sprintf("Reusing image %s.", im.SourcePath)) manifest.record(im.SourcePath, hash, imageGeneratorKey, "image", outputs) builtIMGs[im.WebPath] = im continue } slog.Info(fmt.Sprintf("Building image %s.", im.SourcePath)) srcf, err := os.Open(im.SourcePath) if err != nil { return fmt.Errorf("cannot open image: %w", err) } dest := filepath.Join(dist, im.WebPath) if err := im.Load(srcf); err != nil { _ = srcf.Close() return wrapErrorf(err, "cannot load image") } if err := srcf.Close(); err != nil { return fmt.Errorf("cannot close image %q: %w", im.SourcePath, err) } if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil { return fmt.Errorf( "cannot make gallery dir %q: %w", filepath.Dir(dest), err, ) } destf, err := os.CreateTemp(filepath.Dir(dest), ".winter-image-*") if err != nil { return fmt.Errorf( "cannot write image %q to %q during ExecuteAll: %w", im.SourcePath, dest, err, ) } tmpDest := destf.Name() defer os.Remove(tmpDest) renderErr := im.Render(destf) closeErr := destf.Close() if renderErr != nil { return renderErr } if closeErr != nil { return fmt.Errorf("cannot close image %q: %w", dest, closeErr) } if err := os.Rename(tmpDest, dest); err != nil { return fmt.Errorf("cannot publish image %q to %q: %w", im.SourcePath, dest, err) } manifest.record(im.SourcePath, hash, imageGeneratorKey, "image", imageOutputPaths(im)) builtIMGs[im.WebPath] = im } }
// Load every text document before rendering any of them. Templates that // list posts, drafts, or galleries then see the complete metadata graph, // while each document only needs to be rendered once. for _, doc := range s.docs.All { if _, ok := doc.(*StaticDocument); ok { continue } if err := s.load(doc); err != nil { return fmt.Errorf("cannot load %q during ExecuteAll: %w", doc.Metadata().SourcePath, err) } } sort.Sort(s.docs)
builtDocs := map[string]Document{} for _, doc := range s.docs.All { if prev, ok := builtDocs[doc.Metadata().WebPath]; ok { return fmt.Errorf( "both %s (%T) and %s (%T) wanted to build to %s/%s; remove one", doc.Metadata().SourcePath, doc, prev.Metadata().SourcePath, prev, s.cfg.Production.URL, doc.Metadata().WebPath, ) } hash, err := fileHash(doc.Metadata().SourcePath) if err != nil { return err } outputs := outputPaths(doc) if _, ok := doc.(*StaticDocument); ok { if manifest.isFresh(doc.Metadata().SourcePath, hash, staticGeneratorKey, outputs, dist) { manifest.record(doc.Metadata().SourcePath, hash, staticGeneratorKey, "static", outputs) builtDocs[doc.Metadata().WebPath] = doc continue } if err := s.Build(doc); err != nil { return fmt.Errorf("cannot build static file %q: %w", doc.Metadata().SourcePath, err) } manifest.record(doc.Metadata().SourcePath, hash, staticGeneratorKey, "static", outputs) } else { if err := s.render(doc); err != nil { return fmt.Errorf("cannot render %q during ExecuteAll: %w", doc.Metadata().SourcePath, err) } manifest.record(doc.Metadata().SourcePath, hash, documentGeneratorKey, "document", outputs) } builtDocs[doc.Metadata().WebPath] = doc }
if err := s.writefeed(); err != nil { return fmt.Errorf("cannot generate feed: %w", err) } if err := s.validateURIsDidNotChange(dist); err != nil { return err } return manifest.save(dist)}
// Rebuild rebuilds the document or template at the given path.// Then, it rebuilds any downstream dependencies.//// If src isn't known to the substructure, Rebuild no-ops and returns no error.func (s *Substructure) Rebuild(src string) error { slog.Debug(fmt.Sprintf("%s ↓", src)) if doc, ok := s.DocBySourcePath(src); ok { if err := s.Build(doc); err != nil { return fmt.Errorf("cannot retrieve doc at %q: %w", src, err) } } else { slog.Debug(" + Tracking new file.") if err := s.discoverAtPath(src); err != nil { return fmt.Errorf("cannot add document to in-flight substructure: %w", err) } } for _, doc := range s.docs.All { if doc.DependsOn(src) && doc.Metadata().SourcePath != src { if err := s.Build(doc); err != nil { return fmt.Errorf( "cannot build %q (dependent of %q): %w", doc.Metadata().SourcePath, src, err, ) } } } return nil}
// add adds the given document to the substructure,// removing any old versions in the process.func (s *Substructure) add(doc Document) { s.docs.addOrUpdate(doc)}
// addIMG adds the given image to the substructure,// removing any old versions in the process.func (s *Substructure) addIMG(im *img) error { if s.galleries == nil { s.galleries = map[string][]*img{} } match := galName.FindStringSubmatch(im.WebPath) if len(match) < 1 { return fmt.Errorf("cannot find a gallery name in photo path %q", im.WebPath) } name := match[1] s.galleries[name] = append(s.galleries[name], im) return nil}
// discover clears the substructure of any known documents and discovers all documents from scratch on the filesystem.func (s *Substructure) discover() error { slog.Debug("Starting discovery.") paths := append(s.cfg.Src, "src") for _, path := range paths { if err := s.discoverAtPath(path); err != nil { return err } } if err := s.discoverStatic("public"); err != nil { return err } return nil}
// discoverAtPath discovers all documents in or at the given path glob.func (s *Substructure) discoverAtPath(path string) error { if err := s.discoverGalleries(path); err != nil { return err } if err := s.discoverHTML(path); err != nil { return err } if err := s.discoverMarkdown(path); err != nil { return err } if err := s.discoverOrg(path); err != nil { return err } // TODO: Calling discoverTemplates once misses *.html.tmpl files. // Calling it twice doesn't. Fix? if err := s.discoverTemplates(path); err != nil { return err } if err := s.discoverTemplates(path); err != nil { return err } sort.Sort(s.docs) return nil}
// discoverGalleries adds all documents matching galleryGlobs in or at the given path glob to the substructure.func (s *Substructure) discoverGalleries(src string) error { var files []string for _, g := range galleryGlobs { f, err := filepathx.Glob(filepath.Join(src, g)) if err != nil { return err } files = append(files, f...) } sort.Sort(sort.Reverse((sort.StringSlice(files)))) for _, src := range files { if shouldIgnore(src) { return nil } slog.Debug(fmt.Sprintf("+ %s", src)) im, err := NewIMG(src, s.cfg) if err != nil { return fmt.Errorf("cannot create gallery document from %s: %w", src, err) } if err := s.addIMG(im); err != nil { return err } }
return nil}
// discoverHTML adds all *.html documents in or at the given path glob to the substructure.func (s *Substructure) discoverHTML(path string) error { var htmlFiles []string if stat, err := os.Stat(path); err != nil { if !os.IsNotExist(err) { return err } } else if stat.IsDir() { htmlf, err := filepathx.Glob(filepath.Join(path, "**", fmt.Sprintf("*%s", htmlSuffix))) if err != nil { return err } htmlFiles = append(htmlFiles, htmlf...) } else if strings.HasSuffix(path, htmlSuffix) { htmlFiles = append(htmlFiles, path) } else { return nil }
for _, src := range htmlFiles { if shouldIgnore(src) { continue } slog.Debug(fmt.Sprintf("+ %s", src)) meta := NewMetadata(src, tmplPath) s.add( NewHTMLDocument(src, meta, NewTemplateDocument(src, meta, s.docs, s.galleries, nil), ), ) } for _, d := range s.docs.All { if p, _, ok := strings.Cut(d.Metadata().WebPath, "_"); ok { parent, ok := s.DocBySourcePath(p) if ok { d.Metadata().ParentFilename = parent.Metadata().WebPath } } }
return nil}
// discoverMarkdown adds all *.md documents in or at the given path glob to the substructure.func (s *Substructure) discoverMarkdown(path string) error { var mdFiles []string if stat, err := os.Stat(path); err != nil { if !os.IsNotExist(err) { return err } } else if stat.IsDir() { mdFiles, err = filepathx.Glob(filepath.Join(path, "**", fmt.Sprintf("*%s", markdownSuffix))) if err != nil { return err } } else if strings.HasSuffix(path, markdownSuffix) { mdFiles = append(mdFiles, path) }
for _, src := range mdFiles { if shouldIgnore(src) { continue } slog.Debug(fmt.Sprintf("+ %s", src)) meta := NewMetadata(src, tmplPath) s.add( NewMarkdownDocument(src, meta, map[Document]struct{}{ NewHTMLDocument(src, meta, NewTemplateDocument(src, meta, s.docs, s.galleries, nil), ): {}, NewGeminiDocument(src, meta): {}, }, ), ) }
return nil}
// discoverOrg adds all *.org documents in or at the given path glob to the substructure.func (s *Substructure) discoverOrg(path string) error { // TODO: Allow looking in user's org directory. // TODO: Allow rendering only a subsection of an org file. var orgFiles []string if stat, err := os.Stat(path); err != nil { if !os.IsNotExist(err) { return err } } else if stat.IsDir() { orgFiles, err = filepathx.Glob(filepath.Join(path, "**", fmt.Sprintf("*%s", orgSuffix))) if err != nil { return err } } else if strings.HasSuffix(path, orgSuffix) { orgFiles = append(orgFiles, path) }
for _, src := range orgFiles { if shouldIgnore(src) { continue } slog.Debug(fmt.Sprintf("+ %s", src)) meta := NewMetadata(src, tmplPath) s.add( NewOrgDocument(src, meta, NewHTMLDocument(src, meta, NewTemplateDocument(src, meta, s.docs, s.galleries, nil), ), ), ) }
return nil}
// galleryGlobs are the relative path components with which to discover images.// Each is appended to the path supplied to discoverPhotos and used to perform a glob.//// The glob supports double asterisks, which mean "any character, including a path separator".// Otherwise, syntax is identical to that of [filepath.Glob].var galleryGlobs []string = []string{ "img/**/*.[jJ][pP][gG]", "img/**/*.[jJ][pP][eE][gG]",}
// discoverStatic adds all documents in or at the given path glob to the substructure.func (s *Substructure) discoverStatic(path string) error { var staticFiles []string if stat, err := os.Stat(path); err != nil { if !os.IsNotExist(err) { return err } } else if stat.IsDir() { staticFiles, err = filepathx.Glob(filepath.Join(path, "**", "*")) if err != nil { return err } } else { staticFiles = append(staticFiles, path) }
for _, src := range staticFiles { if shouldIgnore(src) { continue } if stat, err := os.Stat(src); err != nil { return err } else if stat.IsDir() { continue } slog.Debug(fmt.Sprintf("+ %s", src)) webPath, err := filepath.Rel(path, src) if err != nil { return fmt.Errorf("cannot determine desired web path of %q: %w", src, err) } s.add(NewStaticDocument(src, webPath)) }
return nil}
// discoverTemplates adds all *.html.tmpl documents in or at the given path glob to the substructure.func (s *Substructure) discoverTemplates(path string) error { var tmplFiles []string if stat, err := os.Stat(path); err != nil { if !os.IsNotExist(err) { return err } } else if stat.IsDir() { tmplf, err := filepathx.Glob(filepath.Join(path, "**", fmt.Sprintf("*%s", templateSuffix))) if err != nil { return err } tmplFiles = append(tmplFiles, tmplf...) } else if strings.HasSuffix(path, templateSuffix) { tmplFiles = append(tmplFiles, path) }
for _, src := range tmplFiles { if shouldIgnore(src) { continue } slog.Debug(fmt.Sprintf("+ %s", src)) meta := NewMetadata(src, tmplPath) s.add( NewHTMLDocument(src, meta, NewTemplateDocument(src, meta, s.docs, s.galleries, nil), ), ) } for _, d := range s.docs.All { if p, _, ok := strings.Cut(d.Metadata().WebPath, "_"); ok { parent, ok := s.DocBySourcePath(p) if ok { d.Metadata().ParentFilename = parent.Metadata().WebPath } } }
return nil}
// shouldIgnore returns true if the given file path should not be built into the substructure,// or false otherwise.func shouldIgnore(src string) bool { for dir := range ignoreDirs { if strings.HasPrefix(filepath.Clean(src), filepath.Clean(dir)) { return true } } for r := range ignorePaths { if r.MatchString(src) { return true } } return false}