cli: Support reading input from stdin
This commit is contained in:
parent
df89d27a4e
commit
c82b26b540
2 changed files with 26 additions and 15 deletions
15
README.org
15
README.org
|
@ -14,12 +14,15 @@ Please note
|
||||||
** command line
|
** command line
|
||||||
#+begin_src bash
|
#+begin_src bash
|
||||||
$ go-org
|
$ go-org
|
||||||
USAGE: org COMMAND [ARGS]
|
Usage: go-org COMMAND [ARGS]...
|
||||||
- org render FILE OUTPUT_FORMAT
|
Commands:
|
||||||
OUTPUT_FORMAT: org, html, html-chroma
|
- render [FILE] FORMAT
|
||||||
- org blorg init
|
FORMAT: org, html, html-chroma
|
||||||
- org blorg build
|
Instead of specifying a file, org mode content can also be passed on stdin
|
||||||
- org blorg serve
|
- blorg
|
||||||
|
- blorg init
|
||||||
|
- blorg build
|
||||||
|
- blorg serve
|
||||||
#+end_src
|
#+end_src
|
||||||
** as a library
|
** as a library
|
||||||
see [[https://github.com/niklasfasching/go-org/blob/master/main.go][main.go]] and hugo [[https://github.com/gohugoio/hugo/blob/master/markup/org/convert.go][org/convert.go]]
|
see [[https://github.com/niklasfasching/go-org/blob/master/main.go][main.go]] and hugo [[https://github.com/gohugoio/hugo/blob/master/markup/org/convert.go][org/convert.go]]
|
||||||
|
|
26
main.go
26
main.go
|
@ -1,8 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -18,8 +18,9 @@ import (
|
||||||
|
|
||||||
var usage = `Usage: go-org COMMAND [ARGS]...
|
var usage = `Usage: go-org COMMAND [ARGS]...
|
||||||
Commands:
|
Commands:
|
||||||
- render FILE FORMAT
|
- render [FILE] FORMAT
|
||||||
FORMAT: org, html, html-chroma
|
FORMAT: org, html, html-chroma
|
||||||
|
Instead of specifying a file, org mode content can also be passed on stdin
|
||||||
- blorg
|
- blorg
|
||||||
- blorg init
|
- blorg init
|
||||||
- blorg build
|
- blorg build
|
||||||
|
@ -78,15 +79,22 @@ func runBlorg(args []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func render(args []string) {
|
func render(args []string) {
|
||||||
if len(args) < 2 {
|
r, path, format := io.Reader(nil), "", ""
|
||||||
|
if fi, err := os.Stdin.Stat(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
} else if fi.Mode()&os.ModeCharDevice == 0 {
|
||||||
|
r, path, format = os.Stdin, "./STDIN", args[0]
|
||||||
|
} else if len(args) == 2 {
|
||||||
|
f, err := os.Open(args[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
r, path, format = f, args[0], args[1]
|
||||||
|
} else {
|
||||||
log.Fatal(usage)
|
log.Fatal(usage)
|
||||||
}
|
}
|
||||||
path, format := args[0], args[1]
|
d := org.New().Parse(r, path)
|
||||||
bs, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
d := org.New().Parse(bytes.NewReader(bs), path)
|
|
||||||
write := func(w org.Writer) {
|
write := func(w org.Writer) {
|
||||||
out, err := d.Write(w)
|
out, err := d.Write(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue