Esempio n. 1
0
func ShowHelpFor(cmdname, filename string) {
	var err error
	if filename != "" {
		latexref, err = ltxref.ReadXMLFile(filename)
	} else {
		latexref, err = ltxref.ReadXMLData(MustAsset("ltxref.xml"))
	}
	if err != nil {
		fmt.Println(err)
		return
	}

	commands := latexref.FilterCommands(cmdname, "", false)
	for _, cmd := range commands {
		cmd.ToString(os.Stdout)
	}

	return
}
Esempio n. 2
0
func StartHTTPD(httpaddress, filename string, allowEdit bool) {
	file, err := os.Open("edittokens.txt")
	edittokens = make(map[string]string)
	if err != nil {
		allowEdit = false
	} else {
		defer file.Close()

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			kv := strings.Split(scanner.Text(), "=")
			if len(kv) == 2 {
				edittokens[kv[1]] = kv[0]
			}
		}

		if err := scanner.Err(); err != nil {
			fmt.Println(err)
		}
	}

	editMode = allowEdit

	funcMap := template.FuncMap{
		"urlescape": func(in string) string {
			return escapeurl(in)
		},
		"addone": func(in int) int {
			return in + 1
		},
		"showargument": func(in ltxref.Argumenttype) template.HTML {
			var ret string
			switch in {
			case ltxref.MANDARG:
				ret = "<tt>{...}</tt>"
			case ltxref.MANDLIST:
				ret = "<tt>{...,...,...}</tt>"
			case ltxref.OPTARG:
				ret = "<tt>[...]</tt>"
			case ltxref.OPTLIST:
				ret = "<tt>[...,...,...]</tt>"
			case ltxref.TODIMENORSPREADDIMEN:
				ret = "<tt>to</tt> <i>‹dimen›</i> or <tt>spread</tt> ‹<i>dimen</i>›"
			case ltxref.KEYVALLIST:
				ret = "<tt>[..=..,..=..,..=..]</tt>"
			case ltxref.PAREN:
				ret = "(...)"
			default:
				ret = "??"
			}
			return template.HTML(ret)
		},
		"buildurl": func(inlist ...string) string {
			u := &url.URL{}
			u.Path = inlist[0]
			for i := 1; i < len(inlist); i++ {
				if inlist[i+1] != "" {
					addKeyValueToUrl(u, inlist[i], inlist[i+1])
				}
				i++
			}
			return u.String()
		},
	}

	maintemplate := string(MustAsset("templates/main.html"))
	edittemplate := string(MustAsset("templates/edit.html"))
	detailtemplate := string(MustAsset("templates/commanddetail.html"))
	layouttemplate := string(MustAsset("templates/layout.html"))

	// var err error
	tpl = template.Must(template.New("main.html").Funcs(funcMap).Parse(maintemplate))
	template.Must(tpl.Parse(detailtemplate))
	template.Must(tpl.Parse(layouttemplate))
	template.Must(tpl.Parse(edittemplate))

	if filename != "" {
		latexref, err = ltxref.ReadXMLFile(filename)
	} else {
		latexref, err = ltxref.ReadXMLData(MustAsset("ltxref.xml"))
	}
	if err != nil {
		fmt.Println(err)
		return
	}

	r := mux.NewRouter()
	r.HandleFunc("/", mainHandler)
	r.HandleFunc("/editcmd", editCommandHandler)
	r.HandleFunc("/addcommand", addCommandHandler).Methods("POST")
	r.HandleFunc("/addenvironment", addEnvironmentHandler).Methods("POST")
	r.HandleFunc("/adddocumentclass", addDocumentClassHandler).Methods("POST")
	r.HandleFunc("/addpackage", addPackageHandler).Methods("POST")
	r.HandleFunc("/editcmd/{command}", editCommandHandler)
	r.HandleFunc("/editenv/{environment}", editEnvironmentHandler)
	r.HandleFunc("/editdc/{documentclass}", editDocumentClassHandler)
	r.HandleFunc("/editpackage/{package}", editPackageHandler)
	r.HandleFunc("/editpkgcmd/{package}/{command}", editCommandHandler)
	r.HandleFunc("/cmd/{command}", commandDetailHandler)
	r.HandleFunc("/class/{documentclass}", documentclassDetailHandler)
	r.HandleFunc("/env/{environment}", environmentDetailHandler)
	r.HandleFunc("/pkg/{package}", packageDetailHandler)
	r.HandleFunc("/pkg/{package}/cmd/{command}", commandDetailHandler)
	r.PathPrefix("/assets/").Handler(http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "httproot"}))
	http.Handle("/", r)
	fmt.Println("Listening on", httpaddress)
	fmt.Println(http.ListenAndServe(httpaddress, nil))
}