Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit cd5c8e2

Browse files
mmcloughlindmitshur
authored andcommitted
gosrc: handle ".background" in presentations
The current implementation does not resolve background images, therefore they do not display on talksapp. This change handles .background in the same way as .image and .iframe. Fixes #541 Change-Id: I309688fb95bc407fb59e8eb4c5e5ea2f3c54b184 Reviewed-on: https://go-review.googlesource.com/c/133235 Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent fdc9e01 commit cd5c8e2

File tree

8 files changed

+433
-2
lines changed

8 files changed

+433
-2
lines changed

gosrc/present.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type presBuilder struct {
2525
fetch func(fnames []string) ([]*File, error)
2626
}
2727

28-
var assetPat = regexp.MustCompile(`(?m)^\.(play|code|image|iframe|html)\s+(?:-\S+\s+)*(\S+)`)
28+
var assetPat = regexp.MustCompile(`(?m)^\.(play|code|image|background|iframe|html)\s+(?:-\S+\s+)*(\S+)`)
2929

3030
func (b *presBuilder) build() (*Presentation, error) {
3131
var data []byte
@@ -34,7 +34,7 @@ func (b *presBuilder) build() (*Presentation, error) {
3434
for _, m := range assetPat.FindAllSubmatchIndex(b.data, -1) {
3535
name := filepath.Clean(string(b.data[m[4]:m[5]]))
3636
switch string(b.data[m[2]:m[3]]) {
37-
case "iframe", "image":
37+
case "iframe", "image", "background":
3838
data = append(data, b.data[i:m[4]]...)
3939
data = append(data, b.resolveURL(name)...)
4040
case "html":

gosrc/present_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package gosrc
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"os"
7+
"testing"
8+
9+
"golang.org/x/tools/present"
10+
)
11+
12+
// Verify that the output of presBuilder is still a valid presentation.
13+
func TestPresentationBuilderValidOutput(t *testing.T) {
14+
os.Chdir("testdata")
15+
defer os.Chdir("..")
16+
17+
filename := "sample.slide"
18+
data, err := ioutil.ReadFile(filename)
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
23+
// Confirm the slide is valid to begin with.
24+
_, err = present.Parse(bytes.NewReader(data), "testing", 0)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
// Transform the presentation.
30+
b := presBuilder{
31+
filename: filename,
32+
data: data,
33+
resolveURL: func(fname string) string { return fname },
34+
fetch: func(fnames []string) ([]*File, error) { return nil, nil },
35+
}
36+
37+
p, err := b.build()
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
output := p.Files[filename]
43+
if len(output) == 0 {
44+
t.Fatal("presentation builder produced no output")
45+
}
46+
47+
// Confirm the output is still valid.
48+
_, err = present.Parse(bytes.NewReader(output), "testing", 0)
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
}
53+
54+
func TestPresentationBuilderTransforms(t *testing.T) {
55+
resolveURL := func(fname string) string {
56+
return "https://resolved.com/" + fname
57+
}
58+
59+
fetch := func(fnames []string) ([]*File, error) {
60+
var files []*File
61+
for _, fname := range fnames {
62+
files = append(files, &File{
63+
Name: fname,
64+
Data: []byte("data"),
65+
})
66+
}
67+
return files, nil
68+
}
69+
70+
cases := []struct {
71+
Name string
72+
Input string
73+
Expect string
74+
Fetched []string
75+
}{
76+
{
77+
Name: "image",
78+
Input: ".image blah.jpg _ 42",
79+
Expect: ".image https://resolved.com/blah.jpg _ 42",
80+
},
81+
{
82+
Name: "background",
83+
Input: ".background blah.jpg",
84+
Expect: ".background https://resolved.com/blah.jpg",
85+
},
86+
{
87+
Name: "iframe",
88+
Input: ".iframe iframe.html 200 300",
89+
Expect: ".iframe https://resolved.com/iframe.html 200 300",
90+
},
91+
{
92+
Name: "html",
93+
Input: ".html embed.html",
94+
Expect: "\nERROR: .html not supported\n",
95+
},
96+
{
97+
Name: "code",
98+
Input: ".code hello.go /start/,/end/",
99+
Expect: ".code hello.go /start/,/end/",
100+
Fetched: []string{"hello.go"},
101+
},
102+
{
103+
Name: "play",
104+
Input: ".play hello.go",
105+
Expect: ".play hello.go",
106+
Fetched: []string{"hello.go"},
107+
},
108+
}
109+
110+
for _, c := range cases {
111+
t.Run(c.Name, func(t *testing.T) {
112+
b := presBuilder{
113+
filename: "snippet.slide",
114+
data: []byte(c.Input),
115+
resolveURL: resolveURL,
116+
fetch: fetch,
117+
}
118+
119+
p, err := b.build()
120+
if err != nil {
121+
t.Fatal(err)
122+
}
123+
124+
output := p.Files["snippet.slide"]
125+
if !bytes.Equal([]byte(c.Expect), output) {
126+
t.Fatalf("bad output: got '%s' expect '%s", string(output), c.Expect)
127+
}
128+
129+
for _, fname := range c.Fetched {
130+
if _, ok := p.Files[fname]; !ok {
131+
t.Fatalf("file %s not fetched", fname)
132+
}
133+
}
134+
})
135+
}
136+
}

gosrc/testdata/embed.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h3 style="color: red;">Embedded HTML</h3>

gosrc/testdata/gopher.jpg

20 KB
Loading

gosrc/testdata/gopher.svg

Lines changed: 177 additions & 0 deletions
Loading

gosrc/testdata/hello.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// START HELLO OMIT
7+
fmt.Println("Hello, World!") // HLhello
8+
// END HELLO OMIT
9+
}

gosrc/testdata/iframe.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<html>
2+
<head>
3+
<title>Iframe Content</title>
4+
</head>
5+
<body>
6+
<h1>Hello from an <code>iframe</code></h1>
7+
8+
<p>Lorem ipsum dolor amet hot chicken man bun in excepteur tofu. Plaid
9+
vegan dolor gochujang bitters vexillologist pok pok ad poke. Banh mi
10+
deserunt sriracha in, four dollar toast biodiesel ugh cloud bread
11+
humblebrag organic. VHS venmo readymade subway tile, art party green
12+
juice mumblecore pariatur post-ironic lomo slow-carb portland
13+
shoreditch unicorn.</p>
14+
15+
<p>Put a bird on it YOLO ea tilde salvia occaecat shoreditch. Dolore
16+
locavore humblebrag pop-up ut sunt aliqua shaman cillum meh XOXO
17+
biodiesel copper mug mumblecore cliche. Deserunt portland mollit
18+
selfies, anim deep v tattooed tilde bespoke chambray hashtag
19+
gluten-free chicharrones vinyl plaid. Thundercats aesthetic subway
20+
tile lyft consectetur. Slow-carb schlitz quinoa kogi godard
21+
post-ironic fam asymmetrical gluten-free deep v irony. Quis snackwave
22+
velit unicorn kale chips, cronut typewriter deserunt helvetica
23+
keffiyeh yr messenger bag.</p>
24+
</body>
25+
</html>

gosrc/testdata/sample.slide

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
Sample Presentation
2+
Demonstration of present tool features
3+
15:04 2 Jan 2006
4+
Tags: foo, bar, baz
5+
6+
Author Name
7+
Job title, Company
8+
9+
http://url/
10+
@twitter_name
11+
12+
* Introduction
13+
14+
Presentation intended to demonstrate all `present` features.
15+
16+
* Text
17+
18+
Some Text
19+
20+
- bullets
21+
- more bullets
22+
23+
Some More text
24+
25+
Preformatted text
26+
is indented (however you like)
27+
28+
: Presenter notes
29+
30+
.link https://golang.org Link to golang.org
31+
32+
_italic_
33+
*bold*
34+
`program`
35+
Markup—_especially_italic_text_—can easily be overused.
36+
_Why_use_scoped__ptr_? Use plain ***ptr* instead.
37+
38+
* Image
39+
40+
.image gopher.jpg _ 400
41+
.caption [[https://github.com/egonelbre/gophers][Public Domain Gophers]] provided by [[http://egonelbre.com/][Egon Elbre]]
42+
43+
* Vector Graphics
44+
45+
.image gopher.svg _ 400
46+
47+
* Background
48+
49+
.background gopher.jpg
50+
51+
* Code
52+
53+
Hello World in Go!
54+
55+
.code hello.go
56+
57+
We can extract the main function and highlight interesting parts
58+
59+
.code hello.go /func main/,/^}/ HLhello
60+
61+
Note that lines with `OMIT` are excluded
62+
63+
.code hello.go /START HELLO/,/END HELLO/
64+
65+
* Play
66+
67+
We can even embed playable code
68+
69+
.play hello.go /func main/,/^}/
70+
71+
* HTML
72+
73+
We can include arbitrary HTML from files:
74+
75+
.html embed.html
76+
77+
* Iframe
78+
79+
Or we can embed entire pages as iframes
80+
81+
.iframe iframe.html 400 600
82+
83+
#.link http://foo label

0 commit comments

Comments
 (0)