本文整理匯總了Golang中code/google/com/p/go/net/html.Render函數的典型用法代碼示例。如果您正苦於以下問題:Golang Render函數的具體用法?Golang Render怎麽用?Golang Render使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Render函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: finalizeEntity
// Archive / upload final entity HTML
func finalizeEntity(entity *Entity, doc *html.Node, entDir string) {
log.Println("Entity tmp directory: ", entDir)
var finalHTML bytes.Buffer
bl := html.Render(&finalHTML, doc)
if bl != nil {
log.Println(bl)
}
err := StoreHTML(finalHTML, entDir)
if err != nil {
log.Println("Error in StoreHTML: ", err)
}
zipName, err := ArchiveFinalFiles(entDir)
if err != nil {
log.Println("Error archiving files: ", err)
}
err = Update(entity, bson.M{"uuid": entity.UUID}, bson.M{"$set": bson.M{"status": UploadingStatus}})
if err != nil {
log.Println("Error updating entity: ", err)
}
awsLink, err := UploadEntity(zipName, entity)
if err != nil {
log.Println("Error uploading final files: ", err)
}
err = Update(entity, bson.M{"uuid": entity.UUID}, bson.M{"$set": bson.M{"aws_link": awsLink, "status": CompleteStatus}})
if err != nil {
log.Println("Error updating entity: ", err)
}
}
示例2: FetchFullDescription
func FetchFullDescription(link string) string {
res, err := http.Get(link)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
doc, err := html.Parse(strings.NewReader(string(body)))
content := ""
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "section" {
for _, a := range n.Attr {
if a.Key == "class" && a.Val == "entry-content cf" {
var buf bytes.Buffer
html.Render(&buf, n)
content = buf.String()
break
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
return content
}
示例3: fixHtml
// FixHtml parses bytes as HTML and returns well-formed HTML if the parse
// was successful, or escaped HTML, if not.
func fixHtml(linkUrl string, wild []byte) (well []byte) {
n, err := html.Parse(bytes.NewReader(wild))
if err != nil {
return []byte(html.EscapeString(string(wild)))
}
fixImgs(linkUrl, n)
defer func() {
if err := recover(); err == bytes.ErrTooLarge {
well = []byte(html.EscapeString(string(wild)))
} else if err != nil {
panic(err)
}
}()
buf := bytes.NewBuffer(make([]byte, 0, len(wild)*2))
if err := html.Render(buf, n); err != nil {
return []byte(html.EscapeString(string(wild)))
}
well = buf.Bytes()
openBody := []byte("<body>")
i := bytes.Index(well, openBody)
if i < 0 {
return []byte(html.EscapeString(string(wild)))
}
well = well[i+len(openBody):]
closeBody := []byte("</body>")
i = bytes.Index(well, closeBody)
if i < 0 {
return []byte(html.EscapeString(string(wild)))
}
return well[:i]
}
示例4: StrFromNodes
// StrFromNodes returns the string of the rendered html.Nodes.
func StrFromNodes(nodes []*html.Node) string {
buf := bytes.NewBuffer([]byte{})
for _, h := range nodes {
html.Render(buf, h)
}
return buf.String()
}
示例5: String
func (n *node) String() string {
var buf bytes.Buffer
if err := html.Render(&buf, n.node); err != nil {
panic(err)
}
return buf.String()
}
示例6: DomTree2HTML
func DomTree2HTML(DOMTree []*html.Node) {
HTML5 := bytes.NewBuffer([]byte{})
for _, node := range DOMTree {
html.Render(HTML5, node)
}
fmt.Printf("Node: %s", HTML5)
}
示例7: Redact
func Redact(r io.Reader) (string, error) {
doc, err := html.Parse(r)
if err != nil {
return "", err
}
var f func(*html.Node)
f = func(n *html.Node) {
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode {
switch c.Data {
case "style", "script", "head", "meta":
n.RemoveChild(c)
return
case "img":
for i, attr := range c.Attr {
if attr.Key == "src" {
c.Attr[i].Key = "data-redacted-src"
}
}
}
} else if c.Type == html.CommentNode {
n.RemoveChild(c)
return
}
f(c)
}
}
f(doc)
buf := bytes.NewBufferString("")
err = html.Render(buf, doc)
return buf.String(), err
}
示例8: renderHtml
func renderHtml(tree *html.Node, t *testing.T) string {
var wr bytes.Buffer
if err := html.Render(&wr, tree); err != nil {
t.Errorf("html rendering error: %s", err.Error())
}
return wr.String()
}
示例9: Print
func (p Xpath) Print(w io.Writer, n *html.Node) error {
node, err := p.Parse(n)
if err != nil {
return err
}
html.Render(w, node)
return nil
}
示例10: html_write_file
func html_write_file(article *html.Node, dir string) (string, error) {
f, err := ioutil.TempFile(dir, "html.")
if err != nil {
return "", err
}
defer f.Close()
err = html.Render(f, article)
return f.Name(), err
}
示例11: renderHTML
func renderHTML(options Options, file HTMLFile) string {
handle := must(os.Open(file.File.LocalPath)).(*os.File)
defer handle.Close()
doc := must(html.Parse(handle)).(*html.Node)
var f func(*html.Node)
f = func(n *html.Node) {
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
if n.Type == html.ElementNode {
switch n.Data {
case "script":
for i, a := range n.Attr {
if a.Key == "src" {
for _, dep := range file.Deps {
if dep.InstPath == a.Val {
n.Attr[i].Val = formatHref(dep.File.UploadedPath)
break
}
}
}
}
case "link":
stylesheet := false
for _, a := range n.Attr {
if a.Key == "rel" {
stylesheet = a.Val == "stylesheet"
break
}
}
if !stylesheet {
return
}
for i, a := range n.Attr {
if a.Key == "href" {
for _, dep := range file.Deps {
if dep.InstPath == a.Val {
n.Attr[i].Val = formatHref(dep.File.UploadedPath)
break
}
}
}
}
}
}
}
f(doc)
buf := bytes.NewBuffer([]byte{})
panicIf(html.Render(buf, doc))
return buf.String()
}
示例12: RenderNodes
func RenderNodes(w io.Writer, ns []*html.Node) error {
for _, n := range ns {
err := html.Render(w, n)
if err != nil {
return err
}
}
return nil
}
示例13: WriteHtmlFile2
// write html.Node to tmp file
// return tmp_filename, utf-8 encoded
func WriteHtmlFile2(doc *html.Node) (string, error) {
of, err := ioutil.TempFile(tmp_dir, prefix)
if err != nil {
return "", err
}
defer of.Close()
html.Render(of, doc)
return of.Name(), nil
}
示例14: write_file
func write_file(doc *html.Node, temp string) (string, error) {
of, err := ioutil.TempFile(temp, "html.")
if err != nil {
return "", err
}
defer of.Close()
html.Render(of, doc)
return of.Name(), nil
}
示例15: writeXpaths
func writeXpaths(w io.Writer, doc *html.Node, xpath []string) error {
for _, p := range xpath {
c, err := NewXpath(p).Parse(doc)
if err != nil {
return err
}
html.Render(w, c)
}
return nil
}