~kornellapacz/gmnigit

2452840f21be6fb4a675bc8a103e4be35a78e783 — Korneliusz Łapacz 2 years ago 699ac44
create index.gmi after transforming all files
1 files changed, 17 insertions(+), 13 deletions(-)

M files.go
M files.go => files.go +17 -13
@@ 1,6 1,7 @@
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"


@@ 22,24 23,22 @@ func isBinary(detectedMIME *mimetype.MIME) bool {
	return true
}

func createDirectoryIndex(treeRootPath, path string) {
	files, err := ioutil.ReadDir(filepath.Join(treeRootPath, path))
	check(err)
func arrangeDirectory(root, path string) {
	var indexContent string

	index, err := os.Create(filepath.Join(treeRootPath, path, "index.gmi"))
	files, err := ioutil.ReadDir(filepath.Join(root, path))
	check(err)
	defer index.Close()

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

	for _, file := range files {
		if file.IsDir() {
			index.WriteString("=> " + file.Name() + "/\n")
			createDirectoryIndex(treeRootPath, filepath.Join(path, file.Name()))
			indexContent += fmt.Sprintf("=> %s/\n", file.Name())
			arrangeDirectory(root, filepath.Join(path, file.Name()))
			continue
		}

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

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


@@ 50,7 49,7 @@ func createDirectoryIndex(treeRootPath, path string) {
			symlink.WriteString("symlink to: " + dest)

			check(symlink.Close())
			index.WriteString("=> " + file.Name() + ".txt " + file.Name() + "\n")
			indexContent += fmt.Sprintf("=> %s.txt %s\n", file.Name(), file.Name())
			continue
		}



@@ 58,13 57,18 @@ func createDirectoryIndex(treeRootPath, path string) {
		check(err)

		if isBinary(detectedMIME) {
			index.WriteString("=> " + file.Name() + "\n")
			indexContent += fmt.Sprintf("=> %s\n", file.Name())
			continue
		}

		check(os.Rename(filePath, filePath+".txt"))
		index.WriteString("=> " + file.Name() + ".txt " + file.Name() + "\n")
		indexContent += fmt.Sprintf("=> %s.txt %s\n", file.Name(), file.Name())
	}

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

func createBrowsableTree(distPath, repositoryPath string) {


@@ 77,7 81,7 @@ func createBrowsableTree(distPath, repositoryPath string) {
	check(err)
	check(os.RemoveAll(filepath.Join(treePath, ".git")))

	createDirectoryIndex(treePath, "")
	arrangeDirectory(treePath, "")
}

func createIndexFile(distPath, repositoryPath string) {