/
main.go
228 lines (205 loc) · 5.03 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"bytes"
"fmt"
"go/build"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"time"
"github.com/antage/eventsource"
"github.com/fsnotify/fsnotify"
scrapbox "github.com/hhatto/go-scrapbox-parser"
rst "github.com/hhatto/gorst"
"github.com/shurcooL/github_flavored_markdown"
"github.com/skratchdot/open-golang/open"
cli "github.com/urfave/cli/v2"
)
const VERSION string = "0.1"
const indexTemplateHTML = `<!doctype html>
<html>
<head>
<meta charset='utf-8'/>
<title>{{.Filename}} - {{.Dirname}}</title>
<link rel="stylesheet" type="text/css" href="/static/github.min.css" />
{{if .IsScrapbox}}
<link rel="stylesheet" type="text/css" href="/static/scrapbox.css" />
{{end}}
<style>
pre { font-family: Consolas, 'Courier New', Courier, Monaco, monospace; }
code { font-family: Consolas, 'Courier New', Courier, Monaco, monospace; }
</style>
<script>
function startup() {
var eventList = document.getElementById("eventList");
var evtSource = new EventSource("/events");
// Start listening on the event source
evtSource.addEventListener("cre", function(e) {
eventList.removeChild(eventList.childNodes[0]);
var newElement = document.createElement("div");
newElement.innerHTML = e.data;
eventList.appendChild(newElement);
}, false);
}
</script>
</head>
<body onload="startup()">
<div class="container markdown-body">
<div id="eventList">
</div>
</div>
</body>
</html>
`
var targetFileName string
var gChan chan string
func getContentString(filename string) (output string, err error) {
ext := filepath.Ext(filename)
var input []byte
if ext == ".md" {
if input, err = ioutil.ReadFile(targetFileName); err != nil {
log.Println("indexHandler: ", err)
return "", err
}
o := github_flavored_markdown.Markdown(input)
outputBuffer := bytes.NewBuffer(o)
output = outputBuffer.String()
} else if ext == ".sb" || ext == ".scrapbox" {
// scrapbox
p := scrapbox.NewParser()
input, err := os.Open(filename)
if err != nil {
log.Printf("%v", err)
return "", err
}
defer input.Close()
o := p.ToHTML(input)
outputBuffer := bytes.NewBuffer(o)
output = outputBuffer.String()
} else {
// rst
input, err := os.Open(filename)
if err != nil {
log.Printf("os.Open: %v", err)
return "", err
}
defer input.Close()
var buf bytes.Buffer
p := rst.NewParser(nil)
html := rst.ToHTML(&buf)
p.ReStructuredText(input, html)
output = buf.String()
}
return output, err
}
func fileWatcher(ch chan string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("fsnotify.NewWatcher: %v", err)
}
defer watcher.Close()
err = watcher.Add(targetFileName)
if err != nil {
log.Fatalf("watcher.Add: %v", err)
}
for {
select {
case event := <-watcher.Events:
if event.Name != targetFileName {
continue
}
if event.Op&fsnotify.Write != fsnotify.Write &&
event.Op&fsnotify.Create != fsnotify.Create {
continue
}
log.Println("modified file:", event, event.Name)
output, _ := getContentString(event.Name)
ch <- output
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.New("index").Parse(indexTemplateHTML)
if err != nil {
log.Println("indexHandler: ", err)
return
}
output, err := getContentString(targetFileName)
if err != nil {
return
}
var indexObj struct {
Filename string
Dirname string
IsScrapbox bool
}
indexObj.Filename = filepath.Base(targetFileName)
indexObj.Dirname = filepath.Dir(targetFileName)
ext := filepath.Ext(targetFileName)
if ext == ".sb" || ext == ".scrapbox" {
indexObj.IsScrapbox = true
}
err = t.Execute(w, indexObj)
if err != nil {
log.Println("indexHandler: ", err)
}
gChan <- output
}
func execCmd(c *cli.Context) error {
if c.Args().Len() < 1 {
fmt.Println("usage: ftcat FILE")
return nil
}
ch := make(chan string)
gChan = make(chan string)
targetFileName = c.Args().Get(0)
/* open webbrowser */
open.Start("http://0.0.0.0:8089")
go fileWatcher(ch)
/* for static files */
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
staticFilePath := path.Join(gopath, "src/github.com/hhatto/ftcat/static")
fs := http.FileServer(http.Dir(staticFilePath))
http.Handle("/static/", http.StripPrefix("/static/", fs))
/* index */
http.HandleFunc("/", indexHandler)
/* server sent events */
es := eventsource.New(nil, nil)
defer es.Close()
http.Handle("/events", es)
/* message broker */
go func() {
id := 1
for {
select {
case n := <-ch:
es.SendEventMessage(n, "cre", strconv.Itoa(id))
id++
case n := <-gChan:
time.Sleep(300 * time.Millisecond)
es.SendEventMessage(n, "cre", strconv.Itoa(id))
id++
}
}
}()
log.Fatal(http.ListenAndServe(":8089", nil))
return nil
}
func main() {
app := cli.NewApp()
app.Name = "ftcat"
app.Version = VERSION
app.Usage = "markdown live previewer"
app.Action = execCmd
app.Run(os.Args)
}