Created
May 3, 2017 10:25
-
-
Save JonathanMH/5a046f0b4e6a7fed5ffd2a7dc6551302 to your computer and use it in GitHub Desktop.
Golang script for compiling markdown to HTML and copying to the clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"runtime" | |
"github.com/russross/blackfriday" | |
) | |
func main(){ | |
arch := runtime.GOOS | |
if len(os.Args) != 2 { | |
log.Fatal("Usage: mdclip filename.md") | |
} | |
arg := os.Args[1] | |
fp,err := filepath.Abs(arg); | |
if err != nil { | |
log.Fatal(err) | |
} | |
dat, err := ioutil.ReadFile(fp) | |
if err != nil { | |
log.Fatal(err) | |
} | |
output := blackfriday.MarkdownBasic([]byte(dat)) | |
toClipboard( output, arch ) | |
} | |
func toClipboard( output []byte, arch string ){ | |
var copyBin string | |
// Mac "OS" | |
if arch == "darwin" { | |
copyBin = "pbcopy" | |
} | |
// Linux | |
if arch == "linux" { | |
copyBin = "xclip" | |
} | |
copyCmd := exec.Command(copyBin, "-selection", "c") | |
in, err := copyCmd.StdinPipe() | |
if err != nil { | |
log.Fatal(err) | |
} | |
if err := copyCmd.Start(); err != nil { | |
log.Fatal(err) | |
} | |
if _, err := in.Write([]byte(output)); err != nil { | |
log.Fatal(err) | |
} | |
if err := in.Close(); err != nil { | |
log.Fatal(err) | |
} | |
copyCmd.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment