Browse Source

cli: header and footer support

master
Wojciech Kwolek 2 months ago
parent
commit
baf00b7a59
1 changed files with 69 additions and 7 deletions
  1. +69
    -7
      main.go

+ 69
- 7
main.go View File

@ -1,22 +1,31 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"git.r23s.eu/wojciech/gm2html/blockparser"
)
func main() {
if len(os.Args) != 3 {
flag.Usage = func() {
fmt.Println("usage:")
fmt.Printf(" %s input.gmi output.html\n", os.Args[0])
fmt.Printf(" %s [-header=path/to/header.html -footer=path/to/footer.html] input.gmi output.html\n", os.Args[0])
}
header := flag.String("header", "", "A path to a file, whose contents will be prepended to the output file.")
footer := flag.String("footer", "", "A path to a file, whose contents will be appended to the output file.")
flag.Parse()
if flag.NArg() != 2 {
flag.Usage()
os.Exit(-1)
return
}
inputPath := os.Args[1]
outputPath := os.Args[2]
inputPath := flag.Args()[0]
outputPath := flag.Args()[1]
inputFile, err := os.Open(inputPath)
if err != nil {
@ -24,12 +33,52 @@ func main() {
os.Exit(-2)
return
}
defer inputFile.Close()
outputFile, err := os.Create(outputPath)
if err != nil {
fmt.Printf("Couldn't open the output file: %s\n", err.Error())
os.Exit(-2)
return
}
defer outputFile.Close()
var headerFile *os.File = nil
if *header != "" {
headerFile, err = os.Open(*header)
if err != nil {
fmt.Printf("Couldn't open the header file: %s\n", err.Error())
os.Exit(-2)
return
}
defer headerFile.Close()
}
var footerFile *os.File = nil
if *footer != "" {
footerFile, err = os.Open(*footer)
if err != nil {
fmt.Printf("Couldn't open the footer file: %s\n", err.Error())
os.Exit(-2)
return
}
defer footerFile.Close()
}
if headerFile != nil {
header, err := ioutil.ReadAll(headerFile)
if err != nil {
fmt.Printf("An error occured while reading the header: %s\n", err.Error())
os.Exit(-2)
return
}
_, err = outputFile.Write(header)
if err != nil {
fmt.Printf("An error occured while writing the header: %s\n", err.Error())
os.Exit(-2)
return
}
}
bp, err := blockparser.NewBlockParser(inputFile)
if err != nil {
@ -46,12 +95,25 @@ func main() {
return
}
if block == nil {
inputFile.Close()
outputFile.Close()
return
break
}
fmt.Fprint(outputFile, block.RenderHTML())
fmt.Fprint(outputFile, "\n")
}
if footerFile != nil {
footer, err := ioutil.ReadAll(footerFile)
if err != nil {
fmt.Printf("An error occured while reading the footer: %s\n", err.Error())
os.Exit(-2)
return
}
_, err = outputFile.Write(footer)
if err != nil {
fmt.Printf("An error occured while writing the footer: %s\n", err.Error())
os.Exit(-2)
return
}
}
}

Loading…
Cancel
Save