コード例 #1
0
ファイル: http.go プロジェクト: speedata/ltxdoc
func editCommandHandler(w http.ResponseWriter, r *http.Request) {
	requestedPackage := r.FormValue("package")
	if requestedPackage == "" {
		requestedPackage = mux.Vars(r)["package"]
	}
	requestedCommand := r.FormValue("command")
	if requestedCommand == "" {
		requestedCommand = mux.Vars(r)["command"]
	}

	if editToken(r) == "" {
		http.Redirect(w, r, "/cmd/"+escapeurl(requestedCommand), http.StatusUnauthorized)
		return
	}

	var cmd *ltxref.Command
	cmd = latexref.GetCommandFromPackage(requestedCommand, requestedPackage)
	if cmd == nil {
		fmt.Println("Command not found")
		return
	}

	switch r.Method {
	case "POST":
		cmd.ShortDescription["en"] = r.FormValue("shortdesc")
		cmd.Description["en"] = template.HTML(r.FormValue("description"))
		cmd.Label = strings.Split(r.FormValue("tags"), ",")
		cmd.Level = r.FormValue("level")
		cmd.Variant = nil
		variantcount, err := strconv.Atoi(r.FormValue("maxvarpanelcount"))
		if err != nil {
			fmt.Println(err)
			return
		}
		for i := 1; i <= variantcount; i++ {
			v := ltxref.NewVariant()
			v.Name = r.FormValue(fmt.Sprintf("name%d", i))
			v.Description["en"] = template.HTML(r.FormValue(fmt.Sprintf("variant%d", i)))
			numarguments, err := strconv.Atoi(r.FormValue(fmt.Sprintf("argcount%d", i)))
			if err != nil {
				fmt.Println(err)
				return
			}
			for arg := 1; arg <= numarguments; arg++ {
				a := ltxref.NewArgument()
				a.Name = r.FormValue(fmt.Sprintf("v%da%dname", i, arg))
				a.Optional = r.FormValue(fmt.Sprintf("v%da%doptional", i, arg)) == "on"

				tmp, err := strconv.Atoi(r.FormValue(fmt.Sprintf("v%da%dtype", i, arg)))
				if err != nil {
					fmt.Println(err)
					return
				}
				a.Type = ltxref.Argumenttype(tmp)
				v.Arguments = append(v.Arguments, a)
			}
			cmd.Variant = append(cmd.Variant, *v)
		}
		var path string
		if requestedPackage != "" {
			path = "/pkg/" + escapeurl(requestedPackage) + "/cmd/" + escapeurl(requestedCommand)
		} else {
			path = "/cmd/" + escapeurl(requestedCommand)
		}
		saveXML()
		http.Redirect(w, r, path+"?edit="+editToken(r), http.StatusSeeOther)
		return
	case "GET":
		backlink := &url.URL{}
		if requestedPackage == "" {
			backlink.Path = "/cmd/" + requestedCommand
		} else {
			backlink.Path = "/pkg/" + requestedPackage + "/cmd/" + requestedCommand
		}
		addKeyValueToUrl(backlink, "edit", r.FormValue("edit"))

		data := struct {
			Backlink     string
			Command      *ltxref.Command
			XMLUrl       string
			PlainTextUrl string
			Package      string
			Edit         string
			Tags         []string
		}{
			Backlink:     backlink.String(),
			Command:      cmd,
			Edit:         editToken(r),
			XMLUrl:       addXMLFormatString(r.URL),
			Package:      requestedPackage,
			PlainTextUrl: addTXTFormatString(r.URL),
			Tags:         latexref.Tags(),
		}
		err := tpl.ExecuteTemplate(w, "editcommand", data)
		if err != nil {
			fmt.Println(err)
		}
	}
}
コード例 #2
0
ファイル: http.go プロジェクト: speedata/ltxdoc
func editEnvironmentHandler(w http.ResponseWriter, r *http.Request) {
	requestedPackage := ""
	requestedEnvironment := r.FormValue("environment")
	if requestedEnvironment == "" {
		requestedEnvironment = mux.Vars(r)["environment"]
	}

	if editToken(r) == "" {
		http.Redirect(w, r, "/env/"+escapeurl(requestedEnvironment), http.StatusUnauthorized)
		return
	}

	var env *ltxref.Environment
	env = latexref.GetEnvironmentWithName(requestedEnvironment)
	if env == nil {
		http.Redirect(w, r, "/", http.StatusSeeOther)

	}
	switch r.Method {
	case "POST":
		env.ShortDescription["en"] = r.FormValue("shortdesc")
		env.Description["en"] = template.HTML(r.FormValue("description"))
		env.Label = strings.Split(r.FormValue("tags"), ",")
		env.Level = r.FormValue("level")
		env.Variant = nil
		variantcount, err := strconv.Atoi(r.FormValue("maxvarpanelcount"))
		if err != nil {
			fmt.Println(err)
			return
		}
		for i := 1; i <= variantcount; i++ {
			v := ltxref.NewVariant()
			v.Name = r.FormValue(fmt.Sprintf("name%d", i))
			v.Description["en"] = template.HTML(r.FormValue(fmt.Sprintf("variant%d", i)))
			numarguments, err := strconv.Atoi(r.FormValue(fmt.Sprintf("argcount%d", i)))
			if err != nil {
				fmt.Println(err)
				return
			}
			for arg := 1; arg <= numarguments; arg++ {
				a := ltxref.NewArgument()
				a.Name = r.FormValue(fmt.Sprintf("v%da%dname", i, arg))
				a.Optional = r.FormValue(fmt.Sprintf("v%da%doptional", i, arg)) == "on"

				tmp, err := strconv.Atoi(r.FormValue(fmt.Sprintf("v%da%dtype", i, arg)))
				if err != nil {
					fmt.Println(err)
					return
				}
				a.Type = ltxref.Argumenttype(tmp)
				v.Arguments = append(v.Arguments, a)
			}
			env.Variant = append(env.Variant, *v)

		}
		saveXML()
		http.Redirect(w, r, "/env/"+escapeurl(requestedEnvironment)+"?edit="+editToken(r), http.StatusSeeOther)
		return

	case "GET":
		backlink := &url.URL{}
		if requestedPackage == "" {
			backlink.Path = "/env/" + requestedEnvironment
		} else {
			backlink.Path = "/pkg/" + requestedPackage + "/env/" + requestedEnvironment
		}
		addKeyValueToUrl(backlink, "edit", r.FormValue("edit"))

		data := struct {
			Backlink     string
			Environment  *ltxref.Environment
			XMLUrl       string
			PlainTextUrl string
			Edit         string
			Tags         []string
		}{
			Backlink:     backlink.String(),
			Environment:  env,
			Edit:         editToken(r),
			XMLUrl:       addXMLFormatString(r.URL),
			PlainTextUrl: addTXTFormatString(r.URL),
			Tags:         latexref.Tags(),
		}
		err := tpl.ExecuteTemplate(w, "editenvironment", data)
		if err != nil {
			fmt.Println(err)
		}

	}
}