~kornellapacz/gmnigit

ref: 203f45445dc1f40c91a62e27cf5120e55f6a6091 gmnigit/files.go -rw-r--r-- 3.0 KiB
203f4544 — Omar Polo update mimetype to 1.3.1 + go mod tidy 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main

import (
	"fmt"
	"io/fs"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"

	gemini "git.tdem.in/tdemin/gmnhg"
	"github.com/gabriel-vasile/mimetype"
	"github.com/go-git/go-git/v5"
)

type fileType int

const (
	dir fileType = iota
	txt
	bin
)

func createLink(file fs.FileInfo, t fileType) string {
	var link, perm string

	switch t {
	case txt:
		link = "%s.txt%s %s"
	case dir:
		link = "%s/%s %s/"
	case bin:
		link = "%s%s %s"
	}

	if showPermissions {
		perm = " " + file.Mode().String()
	}

	return fmt.Sprintf("=> "+link+"\n", file.Name(), perm, file.Name())
}

func isBinary(detectedMIME *mimetype.MIME) bool {
	for mime := detectedMIME; mime != nil; mime = mime.Parent() {
		if mime.Is("text/plain") {
			return false
		}
	}

	return true
}

func arrangeDirectory(root, path string) {
	var indexContent string

	files, err := ioutil.ReadDir(filepath.Join(root, path))
	check(err)

	indexContent += fmt.Sprintf("# Tree\n\nPath: %s/\n\n", path)

	for _, file := range files {
		if file.IsDir() {
			indexContent += createLink(file, dir)
			arrangeDirectory(root, filepath.Join(path, file.Name()))
			continue
		}

		filePath := filepath.Join(root, path, file.Name())

		if file.Mode()&os.ModeSymlink != 0 {
			dest, err := os.Readlink(filePath)
			check(err)

			symlink, err := os.Create(filePath + ".txt")
			check(err)
			symlink.WriteString("symlink to: " + dest)

			check(symlink.Close())
			indexContent += createLink(file, txt)
			continue
		}

		detectedMIME, err := mimetype.DetectFile(filePath)
		check(err)

		if isBinary(detectedMIME) {
			indexContent += createLink(file, bin)
			continue
		}

		check(os.Rename(filePath, filePath+".txt"))
		indexContent += createLink(file, txt)
	}

	index, err := os.Create(filepath.Join(root, path, "index.gmi"))
	check(err)
	index.WriteString(indexContent)
	check(index.Close())
}

func createBrowsableTree() {
	treePath := filepath.Join(distPath, treeSubPath)

	_, err := git.PlainClone(treePath, false, &git.CloneOptions{
		URL: repositoryPath,
	})

	check(err)
	check(os.RemoveAll(filepath.Join(treePath, ".git")))

	arrangeDirectory(treePath, "")
}

func createIndexFile() {
	indexFile, err := os.Create(filepath.Join(distPath, "index.gmi"))
	check(err)

	url := strings.TrimSpace(cloneURL)
	if url != "" {
		descr := "Command to clone this repository"
		fmt.Fprintf(indexFile, "```%s\ngit clone %s\n```\n\n", descr, url)
	}

	indexFile.WriteString("=> " + commitsSubPath + "/\n")

	if generateRefs {
		indexFile.WriteString("=> " + refsSubPath + "/\n")
	}

	indexFile.WriteString("=> " + treeSubPath + "/\n\n")

	// convert README.md to gemini and write it to index.gmi in repository root
	text, err := os.ReadFile(filepath.Join(distPath, treeSubPath, "README.md.txt"))

	if err != nil {
		if os.IsNotExist(err) {
			log.Println("README.md not found in your repository")

			check(indexFile.Close())
			return
		}

		log.Fatal(err)
	}

	check(err)
	geminiContent, _, err := gemini.RenderMarkdown(text, gemini.WithMetadata)
	check(err)

	indexFile.Write(geminiContent)

	check(indexFile.Close())
}