working version of sequential numbering in batches
added version to build
This commit is contained in:
parent
6b151120f3
commit
7e3518c314
42
html2gmi.go
42
html2gmi.go
@ -1,35 +1,39 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
flag "github.com/spf13/pflag"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/LukeEmmet/html2gemini"
|
||||||
|
flag "github.com/spf13/pflag"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"github.com/LukeEmmet/html2gemini"
|
|
||||||
"bufio"
|
|
||||||
"io"
|
|
||||||
"errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var version = "0.2.1"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
output = flag.StringP("output", "o", "", "Output path. Otherwise uses stdout\n")
|
output = flag.StringP("output", "o", "", "Output path. Otherwise uses stdout\n")
|
||||||
input = flag.StringP("input", "i", "", "Input path. Otherwise uses stdin\n")
|
input = flag.StringP("input", "i", "", "Input path. Otherwise uses stdin\n")
|
||||||
|
citationStart = flag.IntP("citationStart", "c", 1, "Start citations from this index")
|
||||||
|
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")
|
||||||
)
|
)
|
||||||
|
|
||||||
func check(e error) {
|
func check(e error) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveFile(contents []byte, path string) {
|
func saveFile(contents []byte, path string) {
|
||||||
d1 := []byte(contents)
|
d1 := contents
|
||||||
err := ioutil.WriteFile(path, d1, 0644)
|
err := ioutil.WriteFile(path, d1, 0644)
|
||||||
check(err)
|
check(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readStdin () (string) {
|
func readStdin() string {
|
||||||
// based on https://flaviocopes.com/go-shell-pipes/
|
// based on https://flaviocopes.com/go-shell-pipes/
|
||||||
reader := bufio.NewReader(os.Stdin) //default size is 4096 apparently
|
reader := bufio.NewReader(os.Stdin) //default size is 4096 apparently
|
||||||
var output []rune
|
var output []rune
|
||||||
@ -55,15 +59,15 @@ func getInput() (string, error) {
|
|||||||
// we have a pipe input
|
// we have a pipe input
|
||||||
inputHtml = readStdin()
|
inputHtml = readStdin()
|
||||||
|
|
||||||
} else if (*input != "") {
|
} else if *input != "" {
|
||||||
//get the input file from the command line
|
//get the input file from the command line
|
||||||
dat, err := ioutil.ReadFile(*input)
|
dat, err := ioutil.ReadFile(*input)
|
||||||
check(err)
|
check(err)
|
||||||
inputHtml = string(dat)
|
inputHtml = string(dat)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//we shouldnt get here
|
//we shouldn't get here
|
||||||
return "", errors.New("Invalid option for input - use -i <path> or pipe to stdin")
|
return "", errors.New("invalid option for input - use -i <path> or pipe to stdin")
|
||||||
}
|
}
|
||||||
|
|
||||||
return inputHtml, nil
|
return inputHtml, nil
|
||||||
@ -74,16 +78,26 @@ func main() {
|
|||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
if *verFlag {
|
||||||
|
fmt.Println("html2gmi " + version)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
//get the input from commandline or stdin
|
//get the input from commandline or stdin
|
||||||
inputHtml, err:= getInput()
|
inputHtml, err := getInput()
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
//convert html to gmi
|
//convert html to gmi
|
||||||
text, err := html2text.FromString(inputHtml, html2text.Options{PrettyTables: true, GeminiCitationStyleLinks: true})
|
options := html2gemini.NewOptions()
|
||||||
|
options.PrettyTables = true
|
||||||
|
options.CitationStart = *citationStart
|
||||||
|
options.LinkEmitFrequency = *linkEmitFrequency
|
||||||
|
|
||||||
|
text, err := html2gemini.FromString(inputHtml, *options)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
//process the output
|
//process the output
|
||||||
if (*output == "") {
|
if *output == "" {
|
||||||
fmt.Print(text)
|
fmt.Print(text)
|
||||||
} else {
|
} else {
|
||||||
//save to the specified output
|
//save to the specified output
|
||||||
|
Loading…
x
Reference in New Issue
Block a user