/
pagehelpers.go
150 lines (134 loc) · 4.14 KB
/
pagehelpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package onthefly
import (
"strconv"
"strings"
)
type (
// Various function signatures for handling requests
SimpleWebHandle (func(string) string)
TemplateValues map[string]string
)
// Create a blank HTML5 page
func NewHTML5Page(titleText string) *Page {
page := NewPage(titleText, "<!doctype html>")
html := page.root.AddNewTag("html")
head := html.AddNewTag("head")
title := head.AddNewTag("title")
title.AddContent(titleText)
html.AddNewTag("body")
return page
}
// Create a blank HTML5 page that links with Angular.JS
func NewAngularPage(titleText, angularVersion string) *Page {
page := NewPage(titleText, "<!doctype html>")
html := page.root.AddNewTag("html")
html.AddSingularAttrib("ng-app")
head := html.AddNewTag("head")
title := head.AddNewTag("title")
title.AddContent(titleText)
html.AddNewTag("body")
// Must be added after head has been added
page.LinkToJS("//ajax.googleapis.com/ajax/libs/angularjs/" + angularVersion + "/angular.min.js")
return page
}
// Set the margins of the body
func (page *Page) SetMargin(em int) (*Tag, error) {
value := strconv.Itoa(em) + "em"
return page.bodyAttr("margin", value)
}
// Disable scrollbars. Needed when using "<!doctype html>" together with fullscreen canvas/webgl
func (page *Page) NoScrollbars() (*Tag, error) {
return page.bodyAttr("overflow", "hidden")
}
// Prepare for a canvas/webgl tag that covers the entire page
func (page *Page) FullCanvas() {
page.SetMargin(0)
// overflow:hidden
page.NoScrollbars()
// Inline CSS
page.AddStyle("canvas { width: 100%; height: 100%; }")
}
// Set one of the CSS styles of the body
func (page *Page) bodyAttr(key, value string) (*Tag, error) {
tag, err := page.root.GetTag("body")
if err == nil {
tag.AddStyle(key, value)
}
return tag, err
}
// Set the foreground and background color of the body
func (page *Page) SetColor(fgColor string, bgColor string) (*Tag, error) {
tag, err := page.root.GetTag("body")
if err == nil {
tag.AddStyle("color", fgColor)
tag.AddStyle("background-color", bgColor)
}
return tag, err
}
// Set the font family
func (page *Page) SetFontFamily(fontFamily string) (*Tag, error) {
return page.bodyAttr("font-family", fontFamily)
}
// Add a box, for testing
func (page *Page) addBox(id string, rounded bool) (*Tag, error) {
tag, err := page.root.GetTag("body")
if err == nil {
return tag.AddBox(id, rounded, "0.9em", "Speaks browser so you don't have to", "white", "black", "3em"), nil
}
return tag, err
}
// Link a page up with a CSS file
// Takes the url to a CSS file as a string
// The given page must have a "head" tag for this to work
// Returns an error if no "head" tag is found, or nil
func (page *Page) LinkToCSS(cssurl string) error {
head, err := page.GetTag("head")
if err == nil {
link := head.AddNewTag("link")
link.AddAttrib("rel", "stylesheet")
link.AddAttrib("href", cssurl)
link.AddAttrib("type", "text/css")
}
return err
}
// Link a page up with a Favicon file
// Takes the url to a favicon file as a string
// The given page must have a "head" tag for this to work
// Returns an error if no "head" tag is found, or nil
func (page *Page) LinkToFavicon(favurl string) error {
head, err := page.root.GetTag("head")
if err == nil {
link := head.AddNewTag("link")
link.AddAttrib("rel", "shortcut icon")
link.AddAttrib("href", favurl)
}
return err
}
// Takes a charset, for example UTF-8, and creates a <meta> tag in <head>
func (page *Page) MetaCharset(charset string) error {
// Add a meta tag
head, err := page.GetTag("head")
if err == nil {
meta := head.AddNewTag("meta")
meta.AddAttrib("http-equiv", "Content-Type")
meta.AddAttrib("content", "text/html; charset="+charset)
}
return err
}
// Link to Google Fonts
func (page *Page) LinkToGoogleFont(name string) error {
url := "http://fonts.googleapis.com/css?family="
// Replace space with +, if needed
if strings.Contains(name, " ") {
url += strings.Replace(name, " ", "+", -1)
} else {
url += name
}
// Link to the CSS for the given font name
return page.LinkToCSS(url)
}
// Add javascript to the header and specify UTF-8 as the charset
func AddHeader(page *Page, js string) {
page.MetaCharset("UTF-8")
AddScriptToHeader(page, js)
}