Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1282)

Unified Diff: experimental/webtry/webtry.go

Issue 656463002: add support for skfiddle width/height options (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add new db creation commands to the design documnt Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « experimental/webtry/templates/workspace.html ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: experimental/webtry/webtry.go
diff --git a/experimental/webtry/webtry.go b/experimental/webtry/webtry.go
index 947eca7aa0d5d9ba7e544fa8aa3d6d6184841e17..e7269bbf03835f1774acb33e51ce28799146d56d 100644
--- a/experimental/webtry/webtry.go
+++ b/experimental/webtry/webtry.go
@@ -157,6 +157,7 @@ func init() {
indexTemplate, err = htemplate.ParseFiles(
filepath.Join(cwd, "templates/index.html"),
filepath.Join(cwd, "templates/titlebar.html"),
+ filepath.Join(cwd, "templates/sidebar.html"),
filepath.Join(cwd, "templates/content.html"),
filepath.Join(cwd, "templates/headercommon.html"),
filepath.Join(cwd, "templates/footercommon.html"),
@@ -176,6 +177,7 @@ func init() {
recentTemplate, err = htemplate.ParseFiles(
filepath.Join(cwd, "templates/recent.html"),
filepath.Join(cwd, "templates/titlebar.html"),
+ filepath.Join(cwd, "templates/sidebar.html"),
filepath.Join(cwd, "templates/headercommon.html"),
filepath.Join(cwd, "templates/footercommon.html"),
)
@@ -185,6 +187,7 @@ func init() {
workspaceTemplate, err = htemplate.ParseFiles(
filepath.Join(cwd, "templates/workspace.html"),
filepath.Join(cwd, "templates/titlebar.html"),
+ filepath.Join(cwd, "templates/sidebar.html"),
filepath.Join(cwd, "templates/content.html"),
filepath.Join(cwd, "templates/headercommon.html"),
filepath.Join(cwd, "templates/footercommon.html"),
@@ -252,6 +255,8 @@ func init() {
code TEXT DEFAULT '' NOT NULL,
create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
hash CHAR(64) DEFAULT '' NOT NULL,
+ width INTEGER DEFAULT 256 NOT NULL,
+ height INTEGER DEFAULT 256 NOT NULL,
source_image_id INTEGER DEFAULT 0 NOT NULL,
PRIMARY KEY(hash)
@@ -275,6 +280,8 @@ func init() {
name CHAR(64) DEFAULT '' NOT NULL,
create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
hash CHAR(64) DEFAULT '' NOT NULL,
+ width INTEGER DEFAULT 256 NOT NULL,
+ height INTEGER DEFAULT 256 NOT NULL,
hidden INTEGER DEFAULT 0 NOT NULL,
source_image_id INTEGER DEFAULT 0 NOT NULL,
@@ -345,6 +352,8 @@ type Titlebar struct {
type userCode struct {
Code string
Hash string
+ Width int
+ Height int
Source int
Titlebar Titlebar
}
@@ -362,17 +371,22 @@ func writeTemplate(filename string, t *template.Template, context interface{}) e
// expandToFile expands the template and writes the result to the file.
func expandToFile(filename string, code string, t *template.Template) error {
- return writeTemplate(filename, t, userCode{Code: code, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}})
+ return writeTemplate(filename, t, userCode{
+ Code: code,
+ Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo},
+ })
}
// expandCode expands the template into a file and calculates the MD5 hash.
-func expandCode(code string, source int) (string, error) {
+// We include the width and height here so that a single hash can capture
+// both the code and the supplied width/height parameters.
+func expandCode(code string, source int, width, height int) (string, error) {
// in order to support fonts in the chroot jail, we need to make sure
// we're using portable typefaces.
// TODO(humper): Make this more robust, supporting things like setTypeface
inputCodeLines := strings.Split(code, "\n")
- outputCodeLines := []string{"DECLARE_bool(portableFonts);"}
+ outputCodeLines := []string{"DECLARE_bool(portableFonts);", fmt.Sprintf("// WxH: %d, %d", width, height)}
for _, line := range inputCodeLines {
outputCodeLines = append(outputCodeLines, line)
if strings.HasPrefix(strings.TrimSpace(line), "SkPaint p") {
@@ -451,15 +465,15 @@ func reportTryError(w http.ResponseWriter, r *http.Request, err error, message,
w.Write(resp)
}
-func writeToDatabase(hash string, code string, workspaceName string, source int) {
+func writeToDatabase(hash string, code string, workspaceName string, source int, width, height int) {
if db == nil {
return
}
- if _, err := db.Exec("INSERT INTO webtry (code, hash, source_image_id) VALUES(?, ?, ?)", code, hash, source); err != nil {
+ if _, err := db.Exec("INSERT INTO webtry (code, hash, width, height, source_image_id) VALUES(?, ?, ?, ?, ?)", code, hash, width, height, source); err != nil {
log.Printf("ERROR: Failed to insert code into database: %q\n", err)
}
if workspaceName != "" {
- if _, err := db.Exec("INSERT INTO workspacetry (name, hash, source_image_id) VALUES(?, ?, ?)", workspaceName, hash, source); err != nil {
+ if _, err := db.Exec("INSERT INTO workspacetry (name, hash, width, height, source_image_id) VALUES(?, ?, ?, ?, ?)", workspaceName, hash, width, height, source); err != nil {
log.Printf("ERROR: Failed to insert into workspacetry table: %q\n", err)
}
}
@@ -599,6 +613,8 @@ type Workspace struct {
Name string
Code string
Hash string
+ Width int
+ Height int
Source int
Tries []Try
Titlebar Titlebar
@@ -621,14 +637,16 @@ func newWorkspace() (string, error) {
}
// getCode returns the code for a given hash, or the empty string if not found.
-func getCode(hash string) (string, int, error) {
+func getCode(hash string) (string, int, int, int, error) {
code := ""
+ width := 0
+ height := 0
source := 0
- if err := db.QueryRow("SELECT code, source_image_id FROM webtry WHERE hash=?", hash).Scan(&code, &source); err != nil {
+ if err := db.QueryRow("SELECT code, width, height, source_image_id FROM webtry WHERE hash=?", hash).Scan(&code, &width, &height, &source); err != nil {
log.Printf("ERROR: Code for hash is missing: %q\n", err)
- return code, source, err
+ return code, width, height, source, err
}
- return code, source, nil
+ return code, width, height, source, nil
}
func workspaceHandler(w http.ResponseWriter, r *http.Request) {
@@ -657,15 +675,19 @@ func workspaceHandler(w http.ResponseWriter, r *http.Request) {
}
var code string
var hash string
+ var width int
+ var height int
source := 0
if len(tries) == 0 {
code = DEFAULT_SAMPLE
+ width = 256
+ height = 256
} else {
hash = tries[len(tries)-1].Hash
- code, source, _ = getCode(hash)
+ code, width, height, source, _ = getCode(hash)
}
w.Header().Set("Content-Type", "text/html")
- if err := workspaceTemplate.Execute(w, Workspace{Tries: tries, Code: code, Name: name, Hash: hash, Source: source, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil {
+ if err := workspaceTemplate.Execute(w, Workspace{Tries: tries, Code: code, Name: name, Hash: hash, Width: width, Height: height, Source: source, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil {
log.Printf("ERROR: Failed to expand template: %q\n", err)
}
} else if r.Method == "POST" {
@@ -691,6 +713,8 @@ func hasPreProcessor(code string) bool {
type TryRequest struct {
Code string `json:"code"`
+ Width int `json:"width"`
+ Height int `json:"height"`
Name string `json:"name"` // Optional name of the workspace the code is in.
Source int `json:"source"` // ID of the source image, 0 if none.
}
@@ -713,14 +737,14 @@ func iframeHandler(w http.ResponseWriter, r *http.Request) {
return
}
var code string
- code, source, err := getCode(hash)
+ code, width, height, source, err := getCode(hash)
if err != nil {
http.NotFound(w, r)
return
}
// Expand the template.
w.Header().Set("Content-Type", "text/html")
- if err := iframeTemplate.Execute(w, userCode{Code: code, Hash: hash, Source: source}); err != nil {
+ if err := iframeTemplate.Execute(w, userCode{Code: code, Width: width, Height: height, Hash: hash, Source: source}); err != nil {
log.Printf("ERROR: Failed to expand template: %q\n", err)
}
}
@@ -728,6 +752,8 @@ func iframeHandler(w http.ResponseWriter, r *http.Request) {
type TryInfo struct {
Hash string `json:"hash"`
Code string `json:"code"`
+ Width int `json:"width"`
+ Height int `json:"height"`
Source int `json:"source"`
}
@@ -744,7 +770,7 @@ func tryInfoHandler(w http.ResponseWriter, r *http.Request) {
return
}
hash := match[1]
- code, source, err := getCode(hash)
+ code, width, height, source, err := getCode(hash)
if err != nil {
http.NotFound(w, r)
return
@@ -752,6 +778,8 @@ func tryInfoHandler(w http.ResponseWriter, r *http.Request) {
m := TryInfo{
Hash: hash,
Code: code,
+ Width: width,
+ Height: height,
Source: source,
}
resp, err := json.Marshal(m)
@@ -776,6 +804,8 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
code := DEFAULT_SAMPLE
source := 0
+ width := 256
+ height := 256
match := directLink.FindStringSubmatch(r.URL.Path)
var hash string
if len(match) == 2 && r.URL.Path != "/" {
@@ -785,14 +815,14 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Update 'code' with the code found in the database.
- if err := db.QueryRow("SELECT code, source_image_id FROM webtry WHERE hash=?", hash).Scan(&code, &source); err != nil {
+ if err := db.QueryRow("SELECT code, width, height, source_image_id FROM webtry WHERE hash=?", hash).Scan(&code, &width, &height, &source); err != nil {
http.NotFound(w, r)
return
}
}
// Expand the template.
w.Header().Set("Content-Type", "text/html")
- if err := indexTemplate.Execute(w, userCode{Code: code, Hash: hash, Source: source, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil {
+ if err := indexTemplate.Execute(w, userCode{Code: code, Hash: hash, Source: source, Width: width, Height: height, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil {
log.Printf("ERROR: Failed to expand template: %q\n", err)
}
} else if r.Method == "POST" {
@@ -818,23 +848,23 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
reportTryError(w, r, err, "Preprocessor macros aren't allowed.", "")
return
}
- hash, err := expandCode(LineNumbers(request.Code), request.Source)
+ hash, err := expandCode(LineNumbers(request.Code), request.Source, request.Width, request.Height)
if err != nil {
reportTryError(w, r, err, "Failed to write the code to compile.", hash)
return
}
- writeToDatabase(hash, request.Code, request.Name, request.Source)
+ writeToDatabase(hash, request.Code, request.Name, request.Source, request.Width, request.Height)
err = expandGyp(hash)
if err != nil {
reportTryError(w, r, err, "Failed to write the gyp file.", hash)
return
}
- cmd := "scripts/fiddle_wrapper " + hash
+ cmd := fmt.Sprintf("scripts/fiddle_wrapper %s --width %d --height %d", hash, request.Width, request.Height)
if *useChroot {
cmd = "schroot -c webtry --directory=/ -- /skia_build/skia/experimental/webtry/" + cmd
}
if request.Source > 0 {
- cmd += fmt.Sprintf(" image-%d.png", request.Source)
+ cmd += fmt.Sprintf(" --source image-%d.png", request.Source)
}
message, err := doCmd(cmd)
« no previous file with comments | « experimental/webtry/templates/workspace.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698