2020-07-31 12:08:27 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-13 13:38:23 +02:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/LukeEmmet/html2gemini"
|
|
|
|
flag "github.com/spf13/pflag"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-07-31 12:08:27 +02:00
|
|
|
)
|
|
|
|
|
2020-08-26 21:37:04 +02:00
|
|
|
var version = "0.2.3"
|
2020-08-13 13:38:23 +02:00
|
|
|
|
2020-07-31 12:08:27 +02:00
|
|
|
var (
|
2020-08-21 09:01:57 +02:00
|
|
|
output = flag.StringP("output", "o", "", "Output path. Otherwise uses stdout")
|
|
|
|
input = flag.StringP("input", "i", "", "Input path. Otherwise uses stdin")
|
2020-08-13 13:38:23 +02:00
|
|
|
citationStart = flag.IntP("citationStart", "c", 1, "Start citations from this index")
|
2020-08-26 21:37:04 +02:00
|
|
|
citationMarkers = flag.BoolP("citationMarkers", "m", false, "Use footnote style citation markers")
|
|
|
|
numberedLinks = flag.BoolP("numberedLinks", "n", false, "Number the links")
|
2020-08-13 13:38:23 +02:00
|
|
|
linkEmitFrequency = flag.IntP("linkEmitFrequency", "l", 2, "Emit gathered links through the document after this number of paragraphs")
|
|
|
|
verFlag = flag.BoolP("version", "v", false, "Find out what version of html2gmi you're running")
|
2020-07-31 12:08:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func check(e error) {
|
2020-08-13 13:38:23 +02:00
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
2020-07-31 12:08:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveFile(contents []byte, path string) {
|
2020-08-13 13:38:23 +02:00
|
|
|
d1 := contents
|
|
|
|
err := ioutil.WriteFile(path, d1, 0644)
|
|
|
|
check(err)
|
2020-07-31 12:08:27 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 13:38:23 +02:00
|
|
|
func readStdin() string {
|
|
|
|
// based on https://flaviocopes.com/go-shell-pipes/
|
|
|
|
reader := bufio.NewReader(os.Stdin) //default size is 4096 apparently
|
|
|
|
var output []rune
|
2020-07-31 12:11:24 +02:00
|
|
|
|
2020-08-13 13:38:23 +02:00
|
|
|
for {
|
|
|
|
input, _, err := reader.ReadRune()
|
|
|
|
if err != nil && err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
output = append(output, input)
|
|
|
|
}
|
2020-07-31 12:08:27 +02:00
|
|
|
|
2020-08-13 13:38:23 +02:00
|
|
|
return string(output)
|
2020-07-31 12:08:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getInput() (string, error) {
|
2020-08-13 13:38:23 +02:00
|
|
|
var inputHtml string
|
|
|
|
|
|
|
|
info, err := os.Stdin.Stat()
|
|
|
|
check(err)
|
|
|
|
|
2020-08-26 21:37:04 +02:00
|
|
|
if *input != "" {
|
|
|
|
//get the input file from the command line
|
|
|
|
dat, err := ioutil.ReadFile(*input)
|
|
|
|
check(err)
|
|
|
|
inputHtml = string(dat)
|
|
|
|
} else if info.Mode()&os.ModeNamedPipe != 0 {
|
2020-08-13 13:38:23 +02:00
|
|
|
// we have a pipe input
|
|
|
|
inputHtml = readStdin()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//we shouldn't get here
|
|
|
|
return "", errors.New("invalid option for input - use -i <path> or pipe to stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputHtml, nil
|
2020-07-31 12:08:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-08-13 13:38:23 +02:00
|
|
|
var inputHtml string
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *verFlag {
|
|
|
|
fmt.Println("html2gmi " + version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//get the input from commandline or stdin
|
|
|
|
inputHtml, err := getInput()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
//convert html to gmi
|
|
|
|
options := html2gemini.NewOptions()
|
|
|
|
options.PrettyTables = true
|
|
|
|
options.CitationStart = *citationStart
|
|
|
|
options.LinkEmitFrequency = *linkEmitFrequency
|
2020-08-25 23:39:30 +02:00
|
|
|
options.CitationMarkers = *citationMarkers
|
2020-08-26 21:37:04 +02:00
|
|
|
options.NumberedLinks = *numberedLinks
|
|
|
|
|
|
|
|
//use slightly nicer Unicode borders, otherwise can use +,|,-
|
|
|
|
options.PrettyTablesOptions.CenterSeparator = "┼"
|
|
|
|
options.PrettyTablesOptions.ColumnSeparator = "│"
|
|
|
|
options.PrettyTablesOptions.RowSeparator = "─"
|
|
|
|
|
|
|
|
//dont use an extra line to separate header from body, but
|
|
|
|
//do separate each row visually
|
|
|
|
options.PrettyTablesOptions.HeaderLine = false
|
|
|
|
options.PrettyTablesOptions.RowLine = true
|
|
|
|
|
2020-08-13 13:38:23 +02:00
|
|
|
text, err := html2gemini.FromString(inputHtml, *options)
|
2020-08-25 23:39:30 +02:00
|
|
|
|
2020-08-13 13:38:23 +02:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
//process the output
|
|
|
|
if *output == "" {
|
2020-08-26 21:37:04 +02:00
|
|
|
fmt.Print(text + "\n") //terminate with a new line
|
2020-08-13 13:38:23 +02:00
|
|
|
} else {
|
|
|
|
//save to the specified output
|
2020-08-27 08:54:47 +02:00
|
|
|
gmiBytes := []byte(text + "\n")
|
2020-08-13 13:38:23 +02:00
|
|
|
saveFile(gmiBytes, *output)
|
|
|
|
}
|
2020-07-31 12:08:27 +02:00
|
|
|
}
|