Exemplo n.º 1
0
func scrapbooksHandler(w http.ResponseWriter, r *http.Request) {
	scrapbookName, scrapName := chop(strings.TrimPrefix(r.URL.Path, "/scrapbooks/"), "/")
	if scrapbookName == "" {
		scrapbooksIndexHandler(w, r)
		return
	}

	tmpl := FindTemplate("scrapbook")
	if tmpl == nil {
		httpError(w, "cannot find scrapbook template")
		return
	}

	var action string
	var saveError error

	scrap := scrapFromRequest(r, scrapbookName)
	if scrap == nil {
		scrap, saveError = scrapFromFile(scrapbookName, scrapName)
		action = "refresh"
	} else {
		action = r.FormValue("action")
	}

	var content template.HTML

	if scrap != nil {
		switch action {
		case "save":
			saveError = scrap.save(scrapbookName)
		case "refresh":
			path, saveError := scrap.tempSave()
			defer os.Remove(path)
			if saveError == nil {
				page := syntax.NewPageFromPath(scrap.Name, path)
				content = page.Render(false, true)
			}
		case "delete":
			saveError = scrap.delete()
			scrap = nil
		}
	}

	if err := tmpl.Execute(w, struct {
		ScrapbookName string
		Scrap         *Scrap

		Content template.HTML

		Scraps []*Scrap

		SaveError error

		Version string
		Page    map[string]string
		Site    map[string]string
	}{
		ScrapbookName: scrapbookName,
		Scrap:         scrap,

		Content: content,

		Scraps: listScraps(scrapbookName),

		SaveError: saveError,

		Version: VERSION,
		Page:    map[string]string{"Title": "Scrapbooks"},
		Site:    settings.Site,
	}); err != nil {
		httpError(w, "error executing template: %s", err)
	}
}
Exemplo n.º 2
0
// pageHanders is the main handler responsible for pages. It also handles two special cases:
// - If ?admin was specified at the end of URL, it shows original query section and SQL substitution underneath every table/chart.
// - If ?source was specified at the end of an URL, it shows just the original markdown file, without any processing (to use
// with in examples.grafikql.com).
func pageHandler(w http.ResponseWriter, r *http.Request) {
	name := r.URL.Path[1:]
	if *loghttp {
		log.Printf("HTTP GET %s\n", name)
	}
	if forbiddenLink(name) {
		httpError(w, "cannot access %s", name)
		return
	}
	path, err := findFile(RootDir, name)
	if err != nil {
		httpError(w, "no such page %s: %s", name, err)
		return
	}
	r.ParseForm()
	_, admin := r.Form["admin"]
	_, source := r.Form["source"]
	_, nocache := r.Form["nocache"]
	page := syntax.NewPageFromPath(name, path)
	if page.Err != nil {
		httpError(w, "cannot parse page %s: %s", name, page.Err)
		return
	}
	var tmpl *template.Template
	if layout := page.Params()["Layout"]; layout != "" {
		tmpl = FindTemplate(layout)
		if tmpl == nil {
			log.Printf("cannot find template " + layout)
		}
	}
	if tmpl == nil {
		tmpl = pageTemplate
	}
	if source {
		err = tmpl.Execute(w, struct {
			Content template.HTML
			Version string
			Site    map[string]string
			Page    map[string]string
		}{
			Content: template.HTML("<pre>") + page.RenderSource() + template.HTML("</pre>"),
			Version: fmt.Sprintf("%s [%s]", RELEASE, VERSION),
			Site:    settings.Site,
			Page:    page.Params(),
		})
	} else {
		err = tmpl.Execute(w, struct {
			Content template.HTML
			Version string
			Admin   bool
			Site    map[string]string
			Page    map[string]string
		}{
			Content: page.Render(admin, nocache),
			Version: fmt.Sprintf("%s [%s]", RELEASE, VERSION),
			Admin:   admin,
			Site:    settings.Site,
			Page:    page.Params(),
		})
	}
	if err != nil {
		log.Printf("error generating template: %s", err)
	}
}