/
gomd.go
133 lines (114 loc) · 3.27 KB
/
gomd.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
package main
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"time"
"github.com/GeertJohan/go.rice"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
"github.com/labstack/echo/middleware"
"github.com/nochso/gomd/eol"
"github.com/toqueteos/webbrowser"
"gopkg.in/alecthomas/kingpin.v2"
)
type InputArgs struct {
Port *int
File *string
}
var args = InputArgs{
Port: kingpin.Flag("port", "Listening port used by webserver").Short('p').Default("1110").Int(),
File: kingpin.Arg("file", "Markdown file").String(),
}
type EditorView struct {
File string
Content string
LineEndings map[int]string
CurrentLineEnding eol.LineEnding
}
func NewEditorView(filepath string, content string) *EditorView {
return &EditorView{
File: filepath,
Content: content,
LineEndings: eol.Descriptions,
}
}
func main() {
// Parse command line arguments
kingpin.Version("0.0.1")
kingpin.Parse()
// Prepare (optionally) embedded resources
templateBox := rice.MustFindBox("template")
staticHTTPBox := rice.MustFindBox("static").HTTPBox()
staticServer := http.StripPrefix("/static/", http.FileServer(staticHTTPBox))
e := echo.New()
t := &Template{
templates: template.Must(template.New("base").Parse(templateBox.MustString("base.html"))),
}
e.SetRenderer(t)
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/static/*", standard.WrapHandler(staticServer))
edit := e.Group("/edit")
edit.Get("/*", EditHandler)
edit.Post("/*", EditHandlerPost)
go WaitForServer()
e.Run(standard.New(fmt.Sprintf("127.0.0.1:%d", *args.Port)))
}
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func EditHandler(c echo.Context) error {
var ev *EditorView
ev, ok := c.Get("editorView").(*EditorView)
if !ok {
log.Println("reading file")
filepath := c.P(0)
content, err := ioutil.ReadFile(filepath)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unable to read requested file")
}
ev = NewEditorView(filepath, string(content))
ev.CurrentLineEnding = eol.DetectDefault(ev.Content, eol.OSDefault())
log.Println(ev.CurrentLineEnding.Description())
}
return c.Render(http.StatusOK, "base", ev)
}
func EditHandlerPost(c echo.Context) error {
filepath := c.P(0)
eolIndex, _ := strconv.Atoi(c.FormValue("eol"))
content := c.FormValue("content")
convertedContent, err := eol.LineEnding(eolIndex).Apply(content)
if err != nil {
convertedContent = content
log.Println("Error while converting EOL. Saving without conversion.")
}
ioutil.WriteFile(filepath, []byte(convertedContent), 0644)
c.Set("editorView", NewEditorView(filepath, content))
return EditHandler(c)
}
func WaitForServer() {
log.Printf("Waiting for listener on port %d", *args.Port)
url := fmt.Sprintf("http://localhost:%d/edit/%s", *args.Port, url.QueryEscape(*args.File))
for {
time.Sleep(time.Millisecond * 50)
resp, err := http.Get(url)
if err != nil {
continue
}
resp.Body.Close()
break
}
log.Println("Opening " + url)
if err := webbrowser.Open(url); err != nil {
log.Printf("Possible error while opening browser: %s", err)
}
}