-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (87 loc) · 2.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation -framework AppKit
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
void copyToClipboard(const char* html) {
@autoreleasepool {
NSString *htmlString = [NSString stringWithUTF8String:html];
NSData *htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
};
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData:htmlData
options:options
documentAttributes:nil
error:nil];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:@[attributedString]];
}
}
*/
import "C"
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"unsafe"
)
func convertMarkdownToHTML(markdown string) (string, error) {
cmd := exec.Command("pandoc", "-f", "markdown", "-t", "html")
cmd.Stdin = bytes.NewBufferString(markdown)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("error running pandoc: %v - %s", err, stderr.String())
}
return out.String(), nil
}
func copyToClipboardGo(html string) error {
// Format as complete HTML document
doc := fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid black; padding: 8px; }
</style>
</head>
<body>
%s
</body>
</html>`, html)
cstr := C.CString(doc)
defer C.free(unsafe.Pointer(cstr))
C.copyToClipboard(cstr)
return nil
}
func main() {
input, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading from stdin: %v\n", err)
os.Exit(1)
}
if len(input) == 0 {
fmt.Fprintf(os.Stderr, "No input received on stdin\n")
os.Exit(1)
}
html, err := convertMarkdownToHTML(string(input))
if err != nil {
fmt.Fprintf(os.Stderr, "Error converting markdown: %v\n", err)
os.Exit(1)
}
if err := copyToClipboardGo(html); err != nil {
fmt.Fprintf(os.Stderr, "Error copying to clipboard: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Content copied to clipboard successfully!\n")
}