add to transmet button

This commit is contained in:
Dan Ballard 2015-05-13 23:12:39 -07:00
parent 6ccf2a6898
commit b45c66c94e
5 changed files with 51 additions and 20 deletions

View File

@ -5,5 +5,6 @@
"Username": "transmet", "Username": "transmet",
"Password": "asdfasdf" "Password": "asdfasdf"
}, },
"Port": "8001" "Port": "8001",
"Url": "http://localhost:8001/"
} }

View File

@ -23,6 +23,7 @@ type Config struct {
Password string Password string
} }
Port string Port string
Url string
} }
const ( const (

View File

@ -114,6 +114,25 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
func getUrlTitle(url string) string {
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error looking up link", url, ":", err)
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading link", url, ":", err)
} else {
re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)</ *[Tt][Ii][Tt][Ll][Ee] *>")
title := re.FindStringSubmatch(string(body))
if title != nil {
return strings.TrimSpace(title[1])
}
}
}
return ""
}
// ?url= // ?url=
func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
categories.LoadCategories(db) categories.LoadCategories(db)
@ -136,28 +155,17 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
} }
var url = r.URL.Query().Get("url") var url = r.URL.Query().Get("url")
reHttp := regexp.MustCompile("^http://") reHttp := regexp.MustCompile("^https?://")
if url != "" && ! reHttp.Match([]byte(url)) { if url != "" && ! reHttp.Match([]byte(url)) {
url = "http://" + url url = "http://" + url
} }
resp, err := http.Get(url)
if err != nil { title = r.URL.Query().Get("title")
fmt.Println("Error looking up link", url, ":", err) if title == "" && url != "" {
} else { title = getUrlTitle(url)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading link", url, ":", err)
} else {
re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)</ *[Tt][Ii][Tt][Ll][Ee] *>")
title := re.FindStringSubmatch(string(body))
if title != nil {
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": strings.TrimSpace(title[1])})
return
}
}
} }
ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "categories": categories.CategoriesTree, "flashes": flashes}) ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title})
} }
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
@ -208,7 +216,7 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User
fmt.Println("Exec err: ", err) fmt.Println("Exec err: ", err)
} }
ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf}) ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf, "url": config.Url})
} }
func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {

View File

@ -30,6 +30,7 @@
</div> </div>
<div class="collapse navbar-collapse"> <div class="collapse navbar-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><a href="/add">add</a></li>
</ul> </ul>
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
{{if .user}} {{if .user}}

View File

@ -4,12 +4,32 @@
<form class="form-add" action="/" method="post" role="form" class="container col-form"> <form class="form-add" action="/" method="post" role="form" class="container col-form">
<div class="row"> <div class="row">
<div class="col-xs-10"></div><div class="col-xs-2"> <div class="col-xs-2">
<a class="btn btn-lg btn-primary btn-block" href="{{template "launch-add" .}}">Add to transmet</a>
</div>
<div class="col-xs-4"></div>
<div class="col-xs-4"></div>
<div class="col-xs-2">
<input class="btn btn-lg btn-primary btn-block" type="submit" value="Export" /> <input class="btn btn-lg btn-primary btn-block" type="submit" value="Export" />
</div> </div>
<div class="col-xs-6">
Drag this link to bookmark bar and click anywhere to add a link
</div>
<div class="col-xs-6">
Click this to mark the current queue of news items as exported (clearing them)
</div>
<div class="col-xs-12">&nbsp;</div>
</div> </div>
</form> </form>
<textarea class="form-control" name="template" placeholder="Template" rows="16" cols="80">{{.template}}</textarea> <textarea class="form-control" name="template" placeholder="Template" rows="16" cols="80">{{.template}}</textarea>
{{end}}
{{define "launch-add"}}
javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='{{.url}}add',l=d.location,e=encodeURIComponent,u=f+'?url='+e(l.href)+'&title='+e(d.title);a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();void(0)
{{end}} {{end}}