~artemis/cap

51e16ca1011b73859ed07878fc5596be1c029b7f — Artémis 1 year, 1 month ago fb1c383 feat/search-index-builder
wip: implemented a SHITTY AND DUMMY AND HORRIBLE json encode to produce
a nljson of all the files in the tree, with a proper tagged union layout
3 files changed, 126 insertions(+), 2 deletions(-)

M builder.go
M domain/types.go
M handler.go
M builder.go => builder.go +6 -0
@@ 145,6 145,12 @@ func Build(root string, args *Args) (*Config, error) {
		return cfg, err
	}

	// 8b. Invoke the search indexer
	err = fb.HandleSearchIndex(&mainSection)
	if err != nil {
		return cfg, err
	}

	// 9. Analyse the file index for broken content
	for _, link := range index.FoundLinks {
		if strings.HasPrefix(link.PointsTo, "/") {

M domain/types.go => domain/types.go +2 -2
@@ 34,7 34,7 @@ type Feed struct {
	RelativePath string
	Slug         string
	Permalink    string
	Section      *Section
	Section      *Section `json:"-"`

	Title         string
	Description   string


@@ 51,7 51,7 @@ type Page struct {
	Path         string // Absolute path
	RelativePath string // Relative path, relative to the content root
	Permalink    string
	Section      *Section // The section the page is in
	Section      *Section `json:"-"` // The section the page is in

	Title           string
	Summary         string

M handler.go => handler.go +118 -0
@@ 3,6 3,7 @@ package main
import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/url"


@@ 398,3 399,120 @@ func (f *FsBuilder) HandleBuildMetadata() error {
	})
	return err
}

func (fb *FsBuilder) HandleSearchIndex(entry *domain.Section) error {
	timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	domain.L.Debug("Executing search index building")
	cmd := exec.CommandContext(timeoutCtx, "tee", "nl.json")
	writer, err := cmd.StdinPipe()
	if err != nil {
		return err
	}
	err = cmd.Start()
	if err != nil {
		return err
	}

	type BullshitToMarshal struct {
		Kind  string
		Value interface{}
	}

	sectionsToBrowse := []*domain.Section{entry}
browser:
	for {
		var section *domain.Section
		var rest []*domain.Section
		if len(sectionsToBrowse) > 0 {
			section = sectionsToBrowse[0]
		}
		if len(sectionsToBrowse) > 1 {
			rest = sectionsToBrowse[1:]
		}
		if section == nil {
			break browser
		}
		sectionsToBrowse = append(rest, section.Subsections...)

		if section.Page != nil {
			encoded, err := json.Marshal(BullshitToMarshal{Kind: "page", Value: section.Page})
			if err != nil {
				return err
			}
			_, err = writer.Write(encoded)
			if err != nil {
				return err
			}
			_, err = writer.Write([]byte{'\n'})
			if err != nil {
				return err
			}
		}

		for _, page := range section.Pages {
			encoded, err := json.Marshal(BullshitToMarshal{Kind: "page", Value: page})
			if err != nil {
				return err
			}
			_, err = writer.Write(encoded)
			if err != nil {
				return err
			}
			_, err = writer.Write([]byte{'\n'})
			if err != nil {
				return err
			}
		}

		for _, page := range section.Feeds {
			encoded, err := json.Marshal(BullshitToMarshal{Kind: "feed", Value: page})
			if err != nil {
				return err
			}
			_, err = writer.Write(encoded)
			if err != nil {
				return err
			}
			_, err = writer.Write([]byte{'\n'})
			if err != nil {
				return err
			}
		}

		for _, page := range section.Media {
			encoded, err := json.Marshal(BullshitToMarshal{Kind: "media", Value: page})
			if err != nil {
				return err
			}
			_, err = writer.Write(encoded)
			if err != nil {
				return err
			}
			_, err = writer.Write([]byte{'\n'})
			if err != nil {
				return err
			}
		}

		for _, page := range section.Files {
			encoded, err := json.Marshal(BullshitToMarshal{Kind: "file", Value: page})
			if err != nil {
				return err
			}
			_, err = writer.Write(encoded)
			if err != nil {
				return err
			}
			_, err = writer.Write([]byte{'\n'})
			if err != nil {
				return err
			}
		}
	}
	writer.Close()

	// Yes this is horrible. Let's hope i'll finish the refactor soon.
	return cmd.Wait()
}