Skip to content

Commit 2743cf1

Browse files
committed
Initial commit
0 parents  commit 2743cf1

File tree

14 files changed

+728
-0
lines changed

14 files changed

+728
-0
lines changed

.circleci/config.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Golang CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-go/ for more details
4+
version: 2
5+
jobs:
6+
build:
7+
docker:
8+
- image: circleci/golang:1.12.7
9+
steps:
10+
- checkout
11+
- run: go get -u github.com/mitchellh/gox
12+
- run:
13+
name: compile
14+
command: |
15+
gox -os="linux darwin windows" -arch="amd64" -output="dist/decolink_{{.OS}}_{{.Arch}}"
16+
cd dist/ && gzip -k *
17+
- persist_to_workspace:
18+
root: ./
19+
paths:
20+
- dist
21+
- template
22+
alfred:
23+
docker:
24+
- image: cibuilds/github:0.12.2
25+
steps:
26+
- attach_workspace:
27+
at: ./
28+
- run:
29+
name: "Create alfred workflow"
30+
command: |
31+
cp ./dist/decolink_darwin_amd64 ./template/alfred/
32+
export VERSION=${CIRCLE_TAG}
33+
eval "echo \"$(< ./template/alfred/info.plist)\"" > ./template/alfred/info.plist
34+
zip -j ./dist/decolink.alfredworkflow ./template/alfred/*
35+
- persist_to_workspace:
36+
root: ./
37+
paths:
38+
- dist
39+
github-release:
40+
docker:
41+
- image: cibuilds/github:0.12.2
42+
steps:
43+
- attach_workspace:
44+
at: ./
45+
- run:
46+
name: "Publish Release on GitHub"
47+
command: |
48+
find dist -type f -not -name '*gz' -not -name '*alfredworkflow' -delete
49+
ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -delete ${CIRCLE_TAG} ./dist
50+
workflows:
51+
version: 2
52+
build-n-deploy:
53+
jobs:
54+
- build:
55+
filters:
56+
branches:
57+
ignore: /.*/
58+
tags:
59+
only: /^v\d+\.\d+\.\d+$/
60+
- alfred:
61+
requires:
62+
- build
63+
filters:
64+
branches:
65+
ignore: /.*/
66+
tags:
67+
only: /^v\d+\.\d+\.\d+$/
68+
- github-release:
69+
requires:
70+
- build
71+
- alfred
72+
filters:
73+
branches:
74+
ignore: /.*/
75+
tags:
76+
only: /^v\d+\.\d+\.\d+$/

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
template/alfred/decolink_darwin_amd64

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# decolink
2+
3+
Docoration link to markdown
4+
5+
## Usage
6+
7+
CLI
8+
9+
```
10+
$ decolink https://daringfireball.net/projects/markdown/
11+
12+
[Daring Fireball: Markdown](https://daringfireball.net/projects/markdown/)
13+
```
14+
.

cmd/root.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/seapy/decolink/internal"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
)
11+
12+
// Parser ...
13+
var Parser string
14+
15+
// rootCmd represents the base command when called without any subcommands
16+
var rootCmd = &cobra.Command{
17+
Use: "decolink",
18+
Short: "URL to link with title for markdown(or another)",
19+
Long: `URL to link with site title. currently only support markdown. For example:
20+
21+
./decolink https://www.github.com
22+
->
23+
`,
24+
Run: func(cmd *cobra.Command, args []string) {
25+
internal.Init(args)
26+
},
27+
}
28+
29+
// Execute adds all child commands to the root command and sets flags appropriately.
30+
// This is called by main.main(). It only needs to happen once to the rootCmd.
31+
func Execute() {
32+
if err := rootCmd.Execute(); err != nil {
33+
fmt.Println(err)
34+
os.Exit(1)
35+
}
36+
}
37+
38+
func init() {
39+
rootCmd.PersistentFlags().StringVarP(&Parser, "parser", "p", "goquery", "choose parser")
40+
viper.BindPFlag("parser", rootCmd.PersistentFlags().Lookup("parser"))
41+
}

go.mod

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module github.com/seapy/decolink
2+
3+
go 1.12
4+
5+
require (
6+
github.com/PuerkitoBio/goquery v1.5.0
7+
github.com/chromedp/chromedp v0.3.1
8+
github.com/magiconair/properties v1.8.1 // indirect
9+
github.com/pelletier/go-toml v1.4.0 // indirect
10+
github.com/spf13/afero v1.2.2 // indirect
11+
github.com/spf13/cobra v0.0.5
12+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
13+
github.com/spf13/viper v1.4.0
14+
github.com/stretchr/testify v1.3.0 // indirect
15+
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
16+
golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa // indirect
17+
golang.org/x/text v0.3.2 // indirect
18+
)

go.sum

Lines changed: 186 additions & 0 deletions
Large diffs are not rendered by default.

internal/init.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package internal
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/viper"
7+
)
8+
9+
// Init ...
10+
func Init(args []string) {
11+
parser := viper.Get("parser").(string)
12+
if parser == "chrome" {
13+
parserChrome := ParserChrome{}
14+
fmt.Println(parserChrome.GetLink(args[0]))
15+
} else {
16+
parserGoquery := ParserGoquery{}
17+
fmt.Println(parserGoquery.GetLink(args[0]))
18+
}
19+
}

internal/parser_chrome.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package internal
2+
3+
import (
4+
"context"
5+
"log"
6+
"strings"
7+
8+
"github.com/chromedp/chromedp"
9+
)
10+
11+
// ParserChrome parse html using chrome headless
12+
type ParserChrome struct {
13+
}
14+
15+
// GetLink ...
16+
func (g *ParserChrome) GetLink(u string) string {
17+
ctx, cancel := chromedp.NewContext(context.Background())
18+
defer cancel()
19+
20+
var title string
21+
var siteName string
22+
var ok bool
23+
err := chromedp.Run(ctx,
24+
chromedp.Navigate(u),
25+
chromedp.Text("head > title", &title, chromedp.ByQuery),
26+
chromedp.AttributeValue("meta[property='og:site_name']", "content", &siteName, &ok, chromedp.ByQuery),
27+
)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
return ToLink(strings.TrimSpace(title), siteName, u)
32+
}

internal/parser_goquery.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package internal
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"strings"
7+
8+
"github.com/PuerkitoBio/goquery"
9+
)
10+
11+
// ParserGoquery parse html using goquery
12+
type ParserGoquery struct {
13+
}
14+
15+
// GetLink ...
16+
func (g *ParserGoquery) GetLink(u string) string {
17+
resp, err := http.Get(u)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
22+
defer resp.Body.Close()
23+
24+
doc, err := goquery.NewDocumentFromReader(resp.Body)
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
29+
var siteName string
30+
title := doc.Find("head title").Text()
31+
doc.Find("head meta").Each(func(i int, s *goquery.Selection) {
32+
op, _ := s.Attr("property")
33+
con, _ := s.Attr("content")
34+
if op == "og:site_name" {
35+
siteName = con
36+
}
37+
})
38+
return ToLink(strings.TrimSpace(title), siteName, u)
39+
}

internal/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package internal
2+
3+
import "fmt"
4+
5+
// ToLink ...
6+
func ToLink(t string, s string, url string) string {
7+
if s != "" {
8+
t = fmt.Sprintf("%s - %s", t, s)
9+
}
10+
return fmt.Sprintf("[%s](%s)", t, url)
11+
}

0 commit comments

Comments
 (0)