~artemis/cap

4ff118ebaefc4ad2e9729b1a723b06d136a5c306 — Artemis 1 year, 7 months ago a27fa70 v0.3.6
fix: pathbuilding in redirects is absolutely shit
please never do this again ever, future Me
1 files changed, 34 insertions(+), 24 deletions(-)

M loader.go
M loader.go => loader.go +34 -24
@@ 6,6 6,7 @@ import (
	"io/fs"
	"net/url"
	"os"
	"path"
	"path/filepath"
	"sort"
	"strings"


@@ 71,13 72,13 @@ func ListFiles(root string, idx *domain.Index) (domain.Section, error) {
	currentSection := &res

	// Walking through the project's root
	err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
	err := filepath.Walk(root, func(fsPath string, info fs.FileInfo, err error) error {
		if err != nil {
			return err
		}
		name := filepath.Base(path)
		path, _ = filepath.Abs(path)
		relativeToRoot, _ := filepath.Rel(absoluteRoot, path)
		name := filepath.Base(fsPath)
		fsPath, _ = filepath.Abs(fsPath)
		relativeToRoot, _ := filepath.Rel(absoluteRoot, fsPath)

		// Ignoring hidden files
		components := util.PathGetSegments(relativeToRoot)


@@ 106,29 107,29 @@ func ListFiles(root string, idx *domain.Index) (domain.Section, error) {
		if info.IsDir() {
			slug := name
			sub := &domain.Section{
				Path:         path,
				Path:         fsPath,
				RelativePath: relativeToRoot,
				Slug:         slug,
				Permalink:    filepath.Join(currentSection.Permalink, slug),
				Permalink:    path.Join(currentSection.Permalink, slug),
				Parent:       currentSection,
			}

			// Making it the new currently active section
			currentSection.Subsections = append(currentSection.Subsections, sub)
			currentSection = sub
			idx.Files[path] = domain.ObjectTypeSection
			idx.Files[fsPath] = domain.ObjectTypeSection
			return nil
		}

		ext := filepath.Ext(path)
		ext := filepath.Ext(fsPath)
		// If the file is a page (gemini, markdown)
		if util.Includes(ext, ".gmi", ".md", ".html") {
			page, err := LoadPage(idx, currentSection, path)
			page, err := LoadPage(idx, currentSection, fsPath)
			if err != nil {
				return err
			}
			page.RelativePath = relativeToRoot
			idx.Files[path] = domain.ObjectTypePage
			idx.Files[fsPath] = domain.ObjectTypePage

			name = strings.TrimSuffix(info.Name(), ext)
			if name == "index" {


@@ 184,7 185,7 @@ func ListFiles(root string, idx *domain.Index) (domain.Section, error) {
			}

			currentSection.Files = append(currentSection.Files, &domain.ContentFile{
				Path:         path,
				Path:         fsPath,
				RelativePath: relativeToRoot,
			})



@@ 193,7 194,7 @@ func ListFiles(root string, idx *domain.Index) (domain.Section, error) {

		// If the file is a RSS feed
		if strings.HasSuffix(name, ".feed.toml") {
			feed, err := LoadFeed(currentSection, path)
			feed, err := LoadFeed(currentSection, fsPath)
			if err != nil {
				return fmt.Errorf("in %s\n%s", relativeToRoot, err.Error())
			}


@@ 209,12 210,12 @@ func ListFiles(root string, idx *domain.Index) (domain.Section, error) {
		// If the file is a known media file
		if util.Includes(ext, ".jpg", ".jpeg", ".png", ".webp") {
			currentSection.Media = append(currentSection.Media, &domain.ContentFile{
				Path:         path,
				Path:         fsPath,
				RelativePath: relativeToRoot,
				Kind:         domain.ContentFileTypeMedia,
			})

			idx.Files[path] = domain.ObjectTypeMedia
			idx.Files[fsPath] = domain.ObjectTypeMedia

			return nil
		}


@@ 226,11 227,11 @@ func ListFiles(root string, idx *domain.Index) (domain.Section, error) {

		// Else, everything is a generic file, meant to be copied over
		currentSection.Files = append(currentSection.Files, &domain.ContentFile{
			Path:         path,
			Path:         fsPath,
			RelativePath: relativeToRoot,
		})

		idx.Files[path] = domain.ObjectTypeFile
		idx.Files[fsPath] = domain.ObjectTypeFile

		return nil
	})


@@ 425,9 426,9 @@ func LoadRedirect(
	}
	// Building a non-fs page
	page := domain.Page{
		Slug:         strings.TrimSuffix(filepath.Base(capsuleSrc), filepath.Ext(capsuleSrc)),
		Slug:         strings.TrimSuffix(filepath.Base(capsuleSrc), path.Ext(capsuleSrc)),
		RelativePath: relativeToRoot,
		Permalink:    "/" + strings.TrimSuffix(relativeToRoot, filepath.Ext(relativeToRoot)),
		Permalink:    "/" + strings.ReplaceAll(strings.TrimSuffix(relativeToRoot, path.Ext(relativeToRoot)), "\\", "/"),
		Title:        redirect.Title,
		Content:      strings.TrimSpace(contentBuf.String()),
		Format:       domain.FileFormatGemini,


@@ 450,15 451,24 @@ func LoadRedirect(
	// Exploring the section tree to get to the right section level,
	// creating new sections as needed
	pointedSection := &rootSection
	targetPermalink := filepath.Dir(page.Permalink)
	targetPermalink := path.Dir(page.Permalink)
	permalinkChunk := targetPermalink
	for {
		permalinkChunk = strings.TrimPrefix(permalinkChunk, pointedSection.Slug+"/")
		/*
			TODO:
			Dear past Me,

			please never write this again ever

			thanks
			ps: binding fs paths to a virtual URI tree is shit, and managing a virtual URI tree is horrible to do so... don't?
		*/
		if pointedSection.Permalink == targetPermalink {
			break
		}
		permalinkChunk = strings.TrimPrefix(permalinkChunk, pointedSection.Slug+"/")

		permalinkChunks := strings.Split(permalinkChunk, string(os.PathSeparator))
		permalinkChunks := strings.Split(permalinkChunk, "/")
		if len(permalinkChunks) == 0 {
			return fmt.Errorf("failed to find the next root slug for the redirect page\n"+
				"Pointed section permalink: %s\n"+


@@ 476,10 486,10 @@ func LoadRedirect(
		)
		if nextSection == nil {
			newSection := &domain.Section{
				Path:         filepath.Join(pointedSection.Path, nextSectionSlug),
				RelativePath: filepath.Join(pointedSection.RelativePath, nextSectionSlug),
				Path:         path.Join(pointedSection.Path, nextSectionSlug),
				RelativePath: path.Join(pointedSection.RelativePath, nextSectionSlug),
				Slug:         nextSectionSlug,
				Permalink:    filepath.Join(pointedSection.Permalink, nextSectionSlug),
				Permalink:    path.Join(pointedSection.Permalink, nextSectionSlug),
				Parent:       pointedSection,
			}
			pointedSection.Subsections = append(pointedSection.Subsections, newSection)