Pārlūkot izejas kodu

Add support for video (youtube) posts

Lucas Stadler 9 gadi atpakaļ
vecāks
revīzija
cf77fe8f5b
2 mainītis faili ar 37 papildinājumiem un 0 dzēšanām
  1. 34 0
      go/blog/blog.go
  2. 3 0
      go/blog/blog.yaml

+ 34 - 0
go/blog/blog.go

@ -9,7 +9,9 @@ import (
9 9
	"html/template"
10 10
	"io"
11 11
	"io/ioutil"
12
	"net/url"
12 13
	"os"
14
	"strings"
13 15
14 16
	"github.com/russross/blackfriday"
15 17
	"gopkg.in/yaml.v2"
@ -125,6 +127,21 @@ func main() {
125 127
			err = songTmpl.Execute(os.Stdout, post)
126 128
		case "text":
127 129
			err = textTmpl.Execute(os.Stdout, post)
130
		case "video":
131
			if !strings.HasPrefix(post.URL, "https://www.youtube.com/watch") {
132
				exit(fmt.Errorf("can't handle video '%s'", post.URL))
133
			}
134
135
			u, err := url.Parse(post.URL)
136
			if err != nil {
137
				exit(fmt.Errorf("invalid video url '%s'", post.URL))
138
			}
139
			id := u.Query().Get("v")
140
			if id == "" {
141
				exit(fmt.Errorf("invalid video url '%s'", post.URL))
142
			}
143
			post.URL = id
144
			err = videoTmpl.Execute(os.Stdout, post)
128 145
		default:
129 146
			fmt.Fprintf(os.Stderr, "Error: no output for type '%s'\n", post.Type)
130 147
			os.Exit(1)
@ -232,6 +249,23 @@ var textTmpl = template.Must(template.New("text").
232 249
</article>
233 250
`))
234 251
252
var videoTmpl = template.Must(template.New("video").
253
	Funcs(funcs).Parse(`
254
<article id="{{ .Id }}" class="video">
255
	{{- if .Title }}
256
	<header>
257
		<h1>{{ .Title }}</h1>
258
		{{- if .Date }}<time>{{ .Date }}</time>{{ end -}}
259
	</header>
260
	{{- end }}
261
	<iframe width="560" height="315" src="https://www.youtube.com/embed/{{ .URL }}" frameborder="0" allowfullscreen></iframe>
262
	{{- if .Content }}
263
264
	{{ markdown .Content }}
265
	{{- end -}}
266
</article>
267
`))
268
235 269
func exit(err error) {
236 270
	fmt.Fprintf(os.Stderr, "Error: %s\n", err)
237 271
	os.Exit(1)

+ 3 - 0
go/blog/blog.yaml

@ -60,3 +60,6 @@
60 60
    are optional.
61 61
  date: "2016-09-25"
62 62
  type: text
63
- url: https://www.youtube.com/watch?v=yLuOzNeHw5I
64
  content: Lights, by Archive.
65
  type: video