Compare commits
2 Commits
1617715b93
...
ad9b786f45
Author | SHA1 | Date | |
---|---|---|---|
ad9b786f45 | |||
38ad750001 |
69
main.go
69
main.go
@ -1,14 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/gosimple/slug"
|
"github.com/gosimple/slug"
|
||||||
@ -88,7 +91,7 @@ func processModifiedFile(filePath, directory, destDir string) {
|
|||||||
fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
|
fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
|
||||||
slug := slug.Make(fileName)
|
slug := slug.Make(fileName)
|
||||||
|
|
||||||
// Creating destiny directory
|
// Creating destination directory
|
||||||
newDir := filepath.Join(destDir, slug)
|
newDir := filepath.Join(destDir, slug)
|
||||||
err := os.MkdirAll(newDir, os.ModePerm)
|
err := os.MkdirAll(newDir, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -96,8 +99,25 @@ func processModifiedFile(filePath, directory, destDir string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy modified file to the new destiny
|
// Copy modified file to the new destination
|
||||||
newFilePath := filepath.Join(newDir, "index.md")
|
newFilePath := filepath.Join(newDir, "index.md")
|
||||||
|
|
||||||
|
// Check if the file already has frontmatter
|
||||||
|
hasFrontmatter, err := checkFrontmatter(filePath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error checking frontmatter:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the file doesn't have frontmatter, add it
|
||||||
|
if !hasFrontmatter {
|
||||||
|
err = addFrontmatter(filePath, fileName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error adding frontmatter:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = copyFile(filePath, newFilePath)
|
err = copyFile(filePath, newFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error copying file:", err)
|
fmt.Println("Error copying file:", err)
|
||||||
@ -112,6 +132,51 @@ func processModifiedFile(filePath, directory, destDir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to check if the file already has frontmatter
|
||||||
|
func checkFrontmatter(filePath string) (bool, error) {
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if line == "---" {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to add frontmatter to the file
|
||||||
|
func addFrontmatter(filePath, title string) error {
|
||||||
|
// Read the content of the original file
|
||||||
|
content, err := ioutil.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new temporary file with the frontmatter added
|
||||||
|
tempFilePath := filePath + ".tmp"
|
||||||
|
frontmatter := fmt.Sprintf("---\ntitle: %s\ndate: %s\ndraft: false\ntags: micropost\n---\n\n", title, time.Now().Format("2006-01-02 15:04:05 -0700"))
|
||||||
|
err = ioutil.WriteFile(tempFilePath, []byte(frontmatter+string(content)), os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the original file with the temporary file
|
||||||
|
fmt.Println("Rename:", tempFilePath, filePath)
|
||||||
|
err = os.Rename(tempFilePath, filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func copyFile(src, dst string) error {
|
func copyFile(src, dst string) error {
|
||||||
sourceFile, err := os.Open(src)
|
sourceFile, err := os.Open(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user