You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.4 KiB

package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"git.r23s.eu/wojciech/gm2html/blockparser"
)
func main() {
flag.Usage = func() {
fmt.Println("usage:")
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 := flag.Args()[0]
outputPath := flag.Args()[1]
inputFile, err := os.Open(inputPath)
if err != nil {
fmt.Printf("Couldn't open the input file: %s\n", err.Error())
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 {
fmt.Println(err)
os.Exit(-2)
return
}
for {
block, err := bp.Next()
if err != nil {
fmt.Println(err)
os.Exit(-2)
return
}
if block == nil {
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
}
}
}