-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (92 loc) · 2.74 KB
/
main.go
File metadata and controls
103 lines (92 loc) · 2.74 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Function to parse the aliases file and create a map of aliases
func parseAliases() map[string]string {
aliases := make(map[string]string)
if data, err := ioutil.ReadFile("aliases.pz"); err == nil {
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
parts := strings.SplitN(line, " ", 2)
if len(parts) == 2 {
alias := parts[0]
value := parts[1]
aliases[alias] = value
}
}
}
} else {
fmt.Println("Aliases file not found.")
}
return aliases
}
// Function to parse the user-specified .pzp file and replace aliases with values
func generateHTML(filePath string, aliases map[string]string) string {
htmlOutput := ""
if data, err := ioutil.ReadFile(filePath); err == nil {
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
parts := strings.SplitN(line, " ", 2)
alias := parts[0]
if value, ok := aliases[alias]; ok {
if len(parts) > 1 {
replacements := strings.Split(parts[1], ";;")
for i, replacement := range replacements {
value = strings.Replace(value, fmt.Sprintf("{{%d}}", i), replacement, -1)
}
}
htmlOutput += value + "\n" // Add a new line after each line of output
}
}
}
} else {
fmt.Println("Input file not found.")
}
return htmlOutput
}
// Function to replace the placeholder in the template file and save it as the output file
func generatePost(templateFile, outputFile, htmlContent string) {
if templateData, err := ioutil.ReadFile(templateFile); err == nil {
template := string(templateData)
output := strings.Replace(template, "{{replace:me:here}}", htmlContent, -1)
doc, err := goquery.NewDocumentFromReader(strings.NewReader(output))
if err == nil {
formattedOutput, _ := doc.Html()
if err := ioutil.WriteFile(outputFile, []byte(formattedOutput), 0644); err == nil {
fmt.Println("Post generated and saved successfully.")
} else {
fmt.Println("Error saving the output file:", err)
}
} else {
fmt.Println("Error parsing the output HTML:", err)
}
} else {
fmt.Println("Template file not found.")
}
}
// Main function
func main() {
if len(os.Args) < 3 {
fmt.Println("Insufficient arguments.")
fmt.Println("Usage: ./postzer path/to/example.pzp path/to/myfirstpost.html [path/to/custom-template.html]")
return
}
aliases := parseAliases()
inputFile := os.Args[1]
outputFile := os.Args[2]
templateFile := "init-template.html"
if len(os.Args) > 3 {
templateFile = os.Args[3]
}
htmlOutput := generateHTML(inputFile, aliases)
generatePost(templateFile, outputFile, htmlOutput)
}