当前位置: 首页>>代码示例>>Golang>>正文


Golang template.HTMLEscape函数代码示例

本文整理汇总了Golang中html/template.HTMLEscape函数的典型用法代码示例。如果您正苦于以下问题:Golang HTMLEscape函数的具体用法?Golang HTMLEscape怎么用?Golang HTMLEscape使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了HTMLEscape函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: formatCode

func formatCode(src []byte, annotations []doc.TypeAnnotation) htemp.HTML {

	// Collect comment positions.
	var (
		comments []doc.TypeAnnotation
		s        scanner.Scanner
	)
	fset := token.NewFileSet()
	file := fset.AddFile("", fset.Base(), len(src))
	s.Init(file, src, nil, scanner.ScanComments)
commentLoop:
	for {
		pos, tok, lit := s.Scan()
		switch tok {
		case token.EOF:
			break commentLoop
		case token.COMMENT:
			p := file.Offset(pos)
			comments = append(comments, doc.TypeAnnotation{Pos: p, End: p + len(lit)})
		}
	}

	// Merge type annotations and comments without modifying the caller's slice
	// of annoations.
	switch {
	case len(comments) == 0:
		// nothing to do
	case len(annotations) == 0:
		annotations = comments
	default:
		annotations = append(comments, annotations...)
		sort.Sort(sortByPos(annotations))
	}

	var buf bytes.Buffer
	last := 0
	for _, a := range annotations {
		htemp.HTMLEscape(&buf, src[last:a.Pos])
		if a.Name != "" {
			p := a.ImportPath
			if p != "" {
				p = "/" + p
			}
			buf.WriteString(`<a href="`)
			buf.WriteString(escapePath(p))
			buf.WriteByte('#')
			buf.WriteString(escapePath(a.Name))
			buf.WriteString(`">`)
			htemp.HTMLEscape(&buf, src[a.Pos:a.End])
			buf.WriteString(`</a>`)
		} else {
			buf.WriteString(`<span class="com">`)
			htemp.HTMLEscape(&buf, src[a.Pos:a.End])
			buf.WriteString(`</span>`)
		}
		last = a.End
	}
	htemp.HTMLEscape(&buf, src[last:])
	return htemp.HTML(buf.String())
}
开发者ID:sbryant,项目名称:gopkgdoc,代码行数:60,代码来源:template.go

示例2: renderElement

func renderElement(element interface{}, contextChain []interface{}, buf io.Writer) error {
	switch elem := element.(type) {
	case *textElement:
		buf.Write(elem.text)
	case *varElement:
		defer func() {
			if r := recover(); r != nil {
				fmt.Printf("Panic while looking up %q: %s\n", elem.name, r)
			}
		}()
		val, err := lookup(contextChain, elem.name, AllowMissingVariables)
		if err != nil {
			return err
		}

		if val.IsValid() {
			if elem.raw {
				fmt.Fprint(buf, val.Interface())
			} else {
				s := fmt.Sprint(val.Interface())
				template.HTMLEscape(buf, []byte(s))
			}
		}
	case *sectionElement:
		if err := renderSection(elem, contextChain, buf); err != nil {
			return err
		}
	case *Template:
		if err := elem.renderTemplate(contextChain, buf); err != nil {
			return err
		}
	}
	return nil
}
开发者ID:ae0000,项目名称:mustache,代码行数:34,代码来源:mustache.go

示例3: login

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method:", r.Method)
	if r.Method == "GET" {
		crutime := time.Now().Unix()
		fmt.Println("crutime = ", crutime)
		h := md5.New()
		s := strconv.FormatInt(crutime, 10)
		fmt.Println("s = ", s)
		io.WriteString(h, s)
		fmt.Println("h's md5 = ", h.Sum(nil))
		token := fmt.Sprintf("%x", h.Sum(nil))
		t, _ := template.ParseFiles("login.gtpl")
		t.Execute(w, token)
	} else {
		r.ParseForm()
		token := r.Form.Get("token")
		if token != "" {
			fmt.Println("token is ", token)
		} else {
			fmt.Println("token is not exists ")
		}
		fmt.Println("username length:", len(r.Form["username"][0]))
		fmt.Println("username:", template.HTMLEscapeString(r.Form.Get("username")))
		fmt.Println("password:", template.HTMLEscapeString(r.Form.Get("password")))
		template.HTMLEscape(w, []byte(r.Form.Get("username")))
	}
}
开发者ID:vinniey,项目名称:findme.biz,代码行数:27,代码来源:http.go

示例4: Example_escape

func Example_escape() {
	const s = `"Fran & Freddie's Diner" <[email protected]>`
	v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<[email protected]>`}

	fmt.Println(template.HTMLEscapeString(s))
	template.HTMLEscape(os.Stdout, []byte(s))
	fmt.Fprintln(os.Stdout, "")
	fmt.Println(template.HTMLEscaper(v...))

	fmt.Println(template.JSEscapeString(s))
	template.JSEscape(os.Stdout, []byte(s))
	fmt.Fprintln(os.Stdout, "")
	fmt.Println(template.JSEscaper(v...))

	fmt.Println(template.URLQueryEscaper(v...))

	// Output:
	// &#34;Fran &amp; Freddie&#39;s Diner&#34; &lt;[email protected]&gt;
	// &#34;Fran &amp; Freddie&#39;s Diner&#34; &lt;[email protected]&gt;
	// &#34;Fran &amp; Freddie&#39;s Diner&#34;32&lt;[email protected]&gt;
	// \"Fran & Freddie\'s Diner\" \[email protected]\x3E
	// \"Fran & Freddie\'s Diner\" \[email protected]\x3E
	// \"Fran & Freddie\'s Diner\"32\[email protected]\x3E
	// %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E

}
开发者ID:wheelcomplex,项目名称:go-1,代码行数:26,代码来源:example_test.go

示例5: login

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method: ", r.Method)
	if r.Method == "GET" {
		curtime := time.Now().Unix()
		h := md5.New()
		io.WriteString(h, strconv.FormatInt(curtime, 10))
		token := fmt.Sprintf("%x", h.Sum(nil))
		t, _ := template.ParseFiles("login.html")
		t.Execute(w, token)
	} else {
		r.ParseForm()
		token := r.Form.Get("token")
		if token != "" {
			fmt.Println("token is ok")
		} else {
			fmt.Println("token is error")
		}
		slice := []string{"apple", "pear", "banana"}
		log.Println(r.Form.Get("fruit"))
		for _, v := range slice {
			if v == r.Form.Get("fruit") {
				fmt.Println(v)
			}

		}
		log.Println("username: ", r.Form["username"])
		log.Println("password: ", r.Form["password"])
		template.HTMLEscape(w, []byte(r.Form.Get("username")))
	}
}
开发者ID:szqh97,项目名称:test,代码行数:30,代码来源:web.go

示例6: renderElement

func renderElement(element interface{}, contextChain []interface{}, buf io.Writer) {
	switch elem := element.(type) {
	case string:
		buf.Write([]byte(element.(string)))
	case *textElement:
		buf.Write(elem.text)
	case *varElement:
		defer func() {
			if r := recover(); r != nil {
				fmt.Printf("Panic while looking up %q: %s\n", elem.name, r)
			}
		}()
		val := lookup(contextChain, elem.name)

		if val.IsValid() {
			if elem.raw {
				fmt.Fprint(buf, val.Interface())
			} else {
				s := fmt.Sprint(val.Interface())
				template.HTMLEscape(buf, []byte(s))
			}
		}
	case *sectionElement:
		renderSection(elem, contextChain, buf)
	case *Template:
		elem.renderTemplate(contextChain, buf)
	}
}
开发者ID:improbable-io,项目名称:mustache,代码行数:28,代码来源:mustache.go

示例7: login

func login(w ResponseWriter, r *Request) {
	Println("方法:", r.Method)
	if r.Method == "GET" {
		crutime := time.Now().Unix()
		h := md5.New()
		io.WriteString(h, strconv.FormatInt(crutime, 10))
		token := Sprintf("%x", h.Sum(nil))

		t, _ := template.ParseFiles("login.html")
		t.Execute(w, token)
	} else {
		r.ParseForm()
		token := r.Form.Get("token")
		if token != "" {
			Println("标识:", token)
			// 验证合法性
		} else {
			Println("标识:未获取")
			// 报错
		}
		Println(r)
		Println("用户名长度:", len(r.Form["username"][0]))
		Println("用户名:", template.HTMLEscapeString(r.Form.Get("username")))
		Println("密码:", template.HTMLEscapeString(r.Form.Get("password")))
		template.HTMLEscape(w, []byte(r.Form.Get("username")))
	}
}
开发者ID:kingfree,项目名称:haut,代码行数:27,代码来源:web.go

示例8: login

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method: ", r.Method)
	if r.Method == "GET" {
		cruTime := time.Now().Unix()
		h := md5.New()
		io.WriteString(h, strconv.FormatInt(cruTime, 10))
		token := fmt.Sprintf("%x", h.Sum(nil))

		t, _ := template.ParseFiles("04-02-03-duplicate-prevention.gtpl")
		t.Execute(w, token)

	} else {
		// log in request
		r.ParseForm()
		token := r.Form.Get("token")

		if token != "" {
			// check token validity
			fmt.Println("TODO: check if the token is valid: %s\n", token)
		} else {
			// give error if no token
			fmt.Println("TODO: handle error as token is not valid!")
		}

		fmt.Printf("Username length: %v\n", len(r.Form["username"][0]))
		fmt.Printf("Username       : %v\n", template.HTMLEscapeString(r.Form.Get("username")))
		fmt.Printf("password       : %v\n", template.HTMLEscapeString(r.Form.Get("password")))
		template.HTMLEscape(w, []byte(r.Form.Get("username")))
	}
}
开发者ID:agilecreativity,项目名称:go-playground,代码行数:30,代码来源:04-02-03-duplicate-prevention.go

示例9: login

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("Method", r.Method)

	if r.Method == "GET" {
		crutime := time.Now().Unix()
		h := md5.New()

		io.WriteString(h, strconv.FormatInt(crutime, 10))
		token := fmt.Sprintf("%x", h.Sum(nil))

		t, _ := template.ParseFiles("login.gtpl")

		t.Execute(w, token)
	} else {
		r.ParseForm()

		token := r.Form.Get("token")

		if token != "" {
			// check token validity
		} else {
			// give error if no token
		}

		fmt.Println("username length:", len(r.Form["username"][0]))
		fmt.Println("username:", template.HTMLEscapeString(r.Form.Get("username")))
		fmt.Println("password:", template.HTMLEscapeString(r.Form.Get("password")))

		template.HTMLEscape(w, []byte(r.Form.Get("username")))
	}
}
开发者ID:chizhovdee,项目名称:GoHttp,代码行数:31,代码来源:server.go

示例10: login

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method:", r.Method) //获取请求的方法
	if r.Method == "GET" {
		crutime := time.Now().Unix()
		h := md5.New()
		io.WriteString(h, strconv.FormatInt(crutime, 10))
		token := fmt.Sprintf("%x", h.Sum(nil))
		fmt.Println("token", token)
		t, _ := template.ParseFiles("login.gtpl")
		t.Execute(w, token)
	} else {
		//请求的是登陆数据,那么执行登陆的逻辑判断
		r.ParseForm()
		token := r.Form.Get("token")
		if token != "" {
			//验证 token 的合法性
		} else {
			//不存在 token 报错
		}
		fmt.Println("username length:", len(r.Form["username"][0]))
		fmt.Println("username:",
			template.HTMLEscapeString(r.Form.Get("username"))) //输出到服务器端
		fmt.Println("password:",
			template.HTMLEscapeString(r.Form.Get("password")))
		template.HTMLEscape(w, []byte(r.Form.Get("username"))) //输出到客户端
	}
}
开发者ID:NicholeGit,项目名称:go,代码行数:27,代码来源:main.go

示例11: writeText

// Write text to w; optionally html-escaped.
func writeText(w io.Writer, text []byte, html bool) {
	if html {
		template.HTMLEscape(w, text)
		return
	}
	w.Write(text)
}
开发者ID:robryk,项目名称:camlistore,代码行数:8,代码来源:camweb.go

示例12: codeFn

func codeFn(c doc.Code, typ *doc.Type) htemp.HTML {
	var buf bytes.Buffer
	last := 0
	src := []byte(c.Text)
	for _, a := range c.Annotations {
		htemp.HTMLEscape(&buf, src[last:a.Pos])
		switch a.Kind {
		case doc.PackageLinkAnnotation:
			p := "/" + c.Paths[a.PathIndex]
			buf.WriteString(`<a href="`)
			buf.WriteString(escapePath(p))
			buf.WriteString(`">`)
			htemp.HTMLEscape(&buf, src[a.Pos:a.End])
			buf.WriteString(`</a>`)
		case doc.ExportLinkAnnotation, doc.BuiltinAnnotation:
			var p string
			if a.Kind == doc.BuiltinAnnotation {
				p = "/builtin"
			} else if a.PathIndex >= 0 {
				p = "/" + c.Paths[a.PathIndex]
			}
			n := src[a.Pos:a.End]
			n = n[bytes.LastIndex(n, period)+1:]
			buf.WriteString(`<a href="`)
			buf.WriteString(escapePath(p))
			buf.WriteByte('#')
			buf.WriteString(escapePath(string(n)))
			buf.WriteString(`">`)
			htemp.HTMLEscape(&buf, src[a.Pos:a.End])
			buf.WriteString(`</a>`)
		case doc.CommentAnnotation:
			buf.WriteString(`<span class="com">`)
			htemp.HTMLEscape(&buf, src[a.Pos:a.End])
			buf.WriteString(`</span>`)
		case doc.AnchorAnnotation:
			buf.WriteString(`<span id="`)
			if typ != nil {
				htemp.HTMLEscape(&buf, []byte(typ.Name))
				buf.WriteByte('.')
			}
			htemp.HTMLEscape(&buf, src[a.Pos:a.End])
			buf.WriteString(`">`)
			htemp.HTMLEscape(&buf, src[a.Pos:a.End])
			buf.WriteString(`</span>`)
		default:
			htemp.HTMLEscape(&buf, src[a.Pos:a.End])
		}
		last = int(a.End)
	}
	htemp.HTMLEscape(&buf, src[last:])
	return htemp.HTML(buf.String())
}
开发者ID:AlekSi,项目名称:gddo,代码行数:52,代码来源:template.go

示例13: TestHTMLEscape

func TestHTMLEscape(t *testing.T) {
	const s = `"Fran & Freddie's Diner" <[email protected]>`
	v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<[email protected]>`}

	fmt.Println(template.HTMLEscapeString(s))
	template.HTMLEscape(os.Stdout, []byte(s))
	fmt.Fprint(os.Stdout, "")

	fmt.Println(template.JSEscapeString(s))
	fmt.Println(template.JSEscaper(v...))
	fmt.Println(template.URLQueryEscaper(v...))
}
开发者ID:gavinzhs,项目名称:testgo,代码行数:12,代码来源:template_HTML_test.go

示例14: login

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method: ", r.Method)
	if r.Method == "GET" {
		t, _ := template.ParseFiles("login.gtpl")
		t.Execute(w, nil)
	} else {
		r.ParseForm()
		fmt.Println("username:",
			template.HTMLEscapeString(r.Form.Get("username")))
		fmt.Println("password:",
			template.HTMLEscapeString(r.Form.Get("password")))
		template.HTMLEscape(w, []byte(r.Form.Get("username")))
	}
}
开发者ID:ASMlover,项目名称:study,代码行数:14,代码来源:web.go

示例15: villagePreWriteHandler

func villagePreWriteHandler(w http.ResponseWriter, r *http.Request) {
	c := gae.NewContext(r)
	g := goon.FromContext(c)
	u := user.Current(c)
	preWriteView := view.PreWriteView{}
	buf := new(bytes.Buffer)
	template.HTMLEscape(buf, []byte(r.FormValue("comment")))
	t := buf.String()
	preWriteView.Text = strings.Replace(t, "\n", "<br>", -1)
	preWriteView.HiddenText = r.FormValue("comment")
	commentType := r.FormValue("commentType")
	characterID := r.FormValue("characterID")
	preWriteView.CharacterID = characterID
	if commentType == "personal" {
		preWriteView.IsPersonal = true
	} else if commentType == "whisper" {
		preWriteView.IsWhisper = true
	} else if commentType == "graveyard" {
		preWriteView.IsGraveyard = true
	} else {
		preWriteView.IsPublic = true
	}
	no, err := strconv.ParseInt(r.FormValue("vno"), 10, 64)
	if err != nil || len(preWriteView.Text) <= 5 || user.Current(c) == nil || len(preWriteView.Text) > 1000 {
		bad(w)
		return
	}
	preWriteView.VillageNo = no
	village := Village{No: no}
	if err := g.Get(&village); err != nil {
		bad(w)
		return
	}
	vKey := g.Key(village)
	person := Person{UserID: u.ID, ParentKey: vKey, CharacterID: characterID}
	if err := g.Get(&person); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	preWriteView.Face = person.Face
	preWriteView.Author = person.Name
	if err = prewriteTmpl.ExecuteTemplate(w, "base", preWriteView); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
开发者ID:Blurmint199,项目名称:Lycos,代码行数:45,代码来源:village.go


注:本文中的html/template.HTMLEscape函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。