commit e3569bdbaa3abca850d161bc8c2e54a74bfa0220
parent 4b011f0ce269c7725ff29f5b4d60059b233363b3
Author: Tyler Clark <tyler.clark@foxide.xyz>
Date: Sun, 26 Apr 2026 01:37:15 -0400
Working on getting the RSS generator up and running.
This patch doesn't completely implement the RSS generation, but it does work
towards that direction. The way it works is a bit wasteful, and might need to be
re-addressed in the future.
Diffstat:
| M | main.go | | | 84 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- |
1 file changed, 68 insertions(+), 16 deletions(-)
diff --git a/main.go b/main.go
@@ -6,21 +6,24 @@
/* DONE: Recursively mirroring dir structure of source files */
/* DONE: Prepend and append header and footer on HTML files */
/* DONE: Ignore non-valid file types and directories in the src */
+/* DONE: Make the template file not be static */
/* TODO: RSS generator built in */
/* TODO: Sitemap generator */
+/* TODO: Add some better exit codes that actually make sense */
/*****************************************************************/
/* OPTIONAL ARGUMENTS: */
-/* TODO -j: Set how many concurrent jobs can run */
-/* PROG -h: Display help */
+/* TODO -j: Set how many concurrent jobs can run | DIFFICULT */
+/* DONE -h: Display help */
/* TODO -r: Do not generate RSS feed */
/* TODO -s: Do not generate sitemap */
+/* DONE -t: Use template file for HTML */
/* DONE -v: Show program version number */
/*****************************************************************/
package main
import (
- "fmt" // For formatting some outputs
+ "fmt" // For formatting some outputs
"os" // File system and arguments
"os/exec" // For running OS operations
"path" // For removing and detecting file extensions
@@ -30,9 +33,8 @@ import (
/* Version Number */
const version = "0.0.4: Almost"
-
/* This is a test function to try to copy a directory */
-func DirSetup(source string, destination, invalidDir string) error {
+func DirSetup(source string, destination, invalidDir, template string) error {
srcInfo, err := os.Stat(source)
if os.IsNotExist(err) {
@@ -68,21 +70,58 @@ func DirSetup(source string, destination, invalidDir string) error {
}
if entry.IsDir() {
- err = DirSetup(srcPath, dstPath, invalidDir)
+ err = DirSetup(srcPath, dstPath, invalidDir, template)
if err != nil {
return err
}
} else {
- err = ConvertFile(source, entry.Name(), destination)
+ err = ConvertFile(source, entry.Name(), destination, template)
+ if err != nil {
+ return err
+ }
+ /* Creating the RSS file that will be appended for each article */
+ //rssFile, err := os.OpenFile(destination + "/" + "rss.xml", os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
return err
}
+
+ rssFile := destination + "/" + "rss.xml"
+ openRSS, err := os.OpenFile(destination + "/" + "rss.xml", os.O_CREATE|os.O_WRONLY, 0664)
+ if err != nil {
+ panic(err)
+ }
+ rssHeader := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\"\n<channel>\n"
+ _, err = openRSS.WriteString(rssHeader)
+ if err != nil {
+ panic(err)
+ }
+ rssGen(source, entry.Name(), rssFile)
}
}
return nil
}
+func rssGen(filepath, filename, destination string) {
+
+ cmd := exec.Command("pandoc", "-thtml", filepath + "/" + filename)
+ rssFile, err := os.OpenFile(destination, os.O_APPEND|os.O_WRONLY, 0664)
+ if err != nil {
+ panic(err)
+ }
+
+ output, err := cmd.Output()
+ if err != nil {
+ fmt.Println("Error:", err)
+ panic(err)
+ }
+ _, err = rssFile.WriteString(string(output))
+ if err != nil {
+ panic(err)
+ }
+
+}
+
func RemoveExt(filename string) string {
return filename[:len(filename)-len(path.Ext(filename))]
}
@@ -92,13 +131,12 @@ func RemoveExt(filename string) string {
/* need to be maintained over time as pandoc changes. The function will then put the output string */
/* Into the corresponding file */
-/* DONE: Add functionality to ignore non valid file types */
-func ConvertFile(filepath, filename, destination string) error {
+func ConvertFile(filepath, filename, destination, template string) error {
validFile := path.Ext(filename)
/* Skipping invalid file types */
- /* Add support for .org files later */
+ /* TODO: Add support for .org files */
if validFile != ".md" {
fmt.Println("Skipping file:", filename)
return nil
@@ -106,7 +144,13 @@ func ConvertFile(filepath, filename, destination string) error {
fmt.Println("Converting following file to HTML:", filename)
oFile := RemoveExt(filename)
- cmd := exec.Command("pandoc", "-s", "--template", "template.html", filepath + "/" + filename)
+ cmd := exec.Command("pandoc", "-s", "--template", template, filepath + "/" + filename)
+
+ /* Changing execution command if template is NULL */
+ if template == os.DevNull {
+ cmd = exec.Command("pandoc", filepath + "/" + filename)
+ }
+
output, err := cmd.Output()
if err != nil {
fmt.Println("Error:", err)
@@ -127,16 +171,21 @@ func ConvertFile(filepath, filename, destination string) error {
}
func CallHelp() {
- println("Usage Information:")
- println("gengo (-h:-v) [source directory] [destination directory]")
- println("-h: Prints this help menu")
- println("-v: Prints version information for gengo")
+ fmt.Println("Usage: gengo [OPTION]... SOURCE DEST")
+ /* Redundant newline is purposeful */
+ fmt.Println("Automatically convert valid files into HTML using Pandoc.\n")
+ fmt.Println(" -h, --help: Print this help screen")
+ fmt.Println(" -i, --ignore: Don't process directories that match a string")
+ fmt.Println(" -t, --template: Use existing HTML template (Pandoc formatting)")
+ fmt.Println(" -v, --version: Prints version information for gengo")
}
func ArgParser (argument[] string) {
cArgs := len(argument)
+ /* Setting some default values in case user does not want to use these options */
ignoreDir := os.DevNull
+ template := os.DevNull
var srcDir, destDir string
for i := 0; i < cArgs; i++ {
@@ -147,6 +196,9 @@ func ArgParser (argument[] string) {
case "-i", "--ignore":
ignoreDir = argument[i + 1]
i++
+ case "-t", "--template":
+ template = argument[i + 1]
+ i++
case "-v", "--version":
println("Version:", version)
os.Exit(0)
@@ -163,7 +215,7 @@ func ArgParser (argument[] string) {
i++
}
}
- DirSetup(srcDir, destDir, ignoreDir)
+ DirSetup(srcDir, destDir, ignoreDir, template)
}
func main() {