Просмотр исходного кода

Add support for video (youtube) posts

Lucas Stadler лет назад: 9
Родитель
Сommit
cf77fe8f5b
2 измененных файлов с 37 добавлено и 0 удалено
  1. 34 0
      go/blog/blog.go
  2. 3 0
      go/blog/blog.yaml

+ 34 - 0
go/blog/blog.go

9
	"html/template"
9
	"html/template"
10
	"io"
10
	"io"
11
	"io/ioutil"
11
	"io/ioutil"
12
	"net/url"
12
	"os"
13
	"os"
14
	"strings"
13
15
14
	"github.com/russross/blackfriday"
16
	"github.com/russross/blackfriday"
15
	"gopkg.in/yaml.v2"
17
	"gopkg.in/yaml.v2"
125
			err = songTmpl.Execute(os.Stdout, post)
127
			err = songTmpl.Execute(os.Stdout, post)
126
		case "text":
128
		case "text":
127
			err = textTmpl.Execute(os.Stdout, post)
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
		default:
145
		default:
129
			fmt.Fprintf(os.Stderr, "Error: no output for type '%s'\n", post.Type)
146
			fmt.Fprintf(os.Stderr, "Error: no output for type '%s'\n", post.Type)
130
			os.Exit(1)
147
			os.Exit(1)
232
</article>
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
func exit(err error) {
269
func exit(err error) {
236
	fmt.Fprintf(os.Stderr, "Error: %s\n", err)
270
	fmt.Fprintf(os.Stderr, "Error: %s\n", err)
237
	os.Exit(1)
271
	os.Exit(1)

+ 3 - 0
go/blog/blog.yaml

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