本文整理汇总了Golang中golang.org/x/net/html.Render函数的典型用法代码示例。如果您正苦于以下问题:Golang Render函数的具体用法?Golang Render怎么用?Golang Render使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Render函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: CleanHTML
// CleanHTML ...
func CleanHTML(htm string) string {
root := clean_html(htm)
var writer = &bytes.Buffer{}
err := html.Render(writer, root)
panic_error(err)
return writer.String()
}
开发者ID:heartszhang,项目名称:haidian,代码行数:8,代码来源:html.go
示例2: pruneFiltered
// pruneFiltered phrase-scans children of n that match fpr.Selector, and adds
// to toDelete those that should be removed according to fpr.Threshold and acls.
func (c *config) pruneFiltered(n *html.Node, fpr filteredPruningRule, acls map[string]bool, toDelete map[*html.Node]bool) {
for child := n.FirstChild; child != nil; child = child.NextSibling {
if toDelete[child] {
continue
}
remove := false
if fpr.Selector.Selector(child) {
buf := new(bytes.Buffer)
html.Render(buf, child)
tally := make(map[rule]int)
c.scanContent(buf.Bytes(), "text/html", "utf-8", tally)
scores := c.categoryScores(tally)
categories := significantCategories(scores, fpr.Threshold)
rule, _ := c.ChooseACLCategoryAction(acls, categories, "allow", "block", "block-invisible")
remove = rule.Action == "block" || rule.Action == "block-invisible"
}
if remove {
toDelete[child] = true
} else {
c.pruneFiltered(child, fpr, acls, toDelete)
}
}
}
开发者ID:vishnuvaradaraj,项目名称:redwood,代码行数:27,代码来源:prune.go
示例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]
}
开发者ID:velour,项目名称:feedme,代码行数:37,代码来源:webfeed.go
示例4: Process
func (p Cleanup) Process(f parser.Feed) parser.Feed {
p.logger.Infof("Cleaning up feed '%s'\n", f.Title)
for i := range f.Articles {
f.Articles[i].Description = strings.TrimSpace(f.Articles[i].Description)
if nodes, err := html.ParseFragment(strings.NewReader(f.Articles[i].Description), nil); err == nil {
if nodesCleanup(nodes) {
if len(nodes) == 0 {
break
}
buf := util.BufferPool.GetBuffer()
defer util.BufferPool.Put(buf)
for _, n := range nodes {
err = html.Render(buf, n)
if err != nil {
break
}
}
content := buf.String()
// net/http tries to provide valid html, adding html, head and body tags
content = content[strings.Index(content, "<body>")+6 : strings.LastIndex(content, "</body>")]
f.Articles[i].Description = content
}
}
}
return f
}
开发者ID:urandom,项目名称:readeef,代码行数:33,代码来源:cleanup.go
示例5: 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()
}
开发者ID:jcgregorio,项目名称:piccolo,代码行数:8,代码来源:main.go
示例6: getArticle
func getArticle(data []byte) string {
r := bytes.NewReader(data)
doc, _ := html.Parse(r)
// Tags
doc = removeNegativeCandidates(doc)
doc = removeNegativeMatches(doc)
doc = getBodyElement(doc)
// Attributes
doc = removeNegativeAttributeMatches(doc)
doc, _ = retriveMainRole(doc)
doc = removeNonMainContent(doc)
doc = clearClassesAndIDs(doc)
c := calcContent(doc)
var buff bytes.Buffer
html.Render(&buff, doc)
articlestr := buff.String()
articlestr = utils.RemoveNewLines(articlestr)
articlestr = utils.ReplaceTabsWithASpace(articlestr)
articlestr = utils.TrimSpaces(articlestr)
if float64(c)/float64(len(articlestr)) < 0.2 {
// At least 20% of the article should be text
return ""
}
return articlestr
}
开发者ID:hygerth,项目名称:brooklet,代码行数:25,代码来源:siteparser.go
示例7: loadXpath
func loadXpath(response *http.Response, xpath string) ([]byte, error) {
body, err := ioutil.ReadAll(response.Body)
panicError(err)
// Parse body to see if login worked
// reader := strings.NewReader(body)
root, err := html.Parse(bytes.NewBuffer(body))
if err != nil {
return nil, err
}
var b bytes.Buffer
html.Render(&b, root)
fixedHtml := b
// body = bytes.NewReader(fixedHtml)
xmlroot, xmlerr := xmlpath.ParseHTML(bytes.NewReader(fixedHtml.Bytes()))
if xmlerr != nil {
return nil, xmlerr
}
path := xmlpath.MustCompile(xpath)
if value, ok := path.Bytes(xmlroot); ok {
return value, nil
}
return nil, errors.New("Could not find xpath")
}
开发者ID:Fapiko,项目名称:simunomics-frontend,代码行数:29,代码来源:service.go
示例8: getRootNode
func getRootNode(urlToOpen string) *xmlpath.Node {
req, err := http.NewRequest("GET", urlToOpen, nil)
if err != nil {
log("error1: " + err.Error())
} else {
response, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
log("Error 3: " + err.Error())
} else {
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
log("Error 2: " + err.Error())
} else {
root, err := html.Parse(bytes.NewReader(content))
if err != nil {
log("Parse error: " + err.Error())
}
var b bytes.Buffer
html.Render(&b, root)
fixedHtml := b.String()
reader := strings.NewReader(fixedHtml)
rootNode, err := xmlpath.ParseHTML(reader)
if err != nil {
log("Error 4: " + err.Error())
} else {
return rootNode
}
}
}
}
return nil
}
开发者ID:Pinperepette,项目名称:opparis,代码行数:35,代码来源:cosmiccat.twitterbot.go
示例9: stripHTML
//strip the <!DOCTYPE html> and <html> tag
func stripHTML(filename string) error {
targetHTML, err := os.OpenFile(filename, os.O_RDWR, 0666)
if err != nil {
return err
}
defer targetHTML.Close()
target, err := html.Parse(targetHTML)
if err != nil {
return err
}
//node for <head>
headNode := target.FirstChild.NextSibling.FirstChild
err = targetHTML.Truncate(0)
if err != nil {
return err
}
_, err = targetHTML.Seek(0, 0)
if err != nil {
return err
}
for node := headNode; node != nil; node = node.NextSibling {
err = html.Render(targetHTML, node)
if err != nil {
return err
}
}
return nil
}
开发者ID:caesarxuchao,项目名称:caesarxuchao.github.io,代码行数:30,代码来源:api-reference-process.go
示例10: Test2
func Test2(t *testing.T) {
lg, lge := loghttp.Logger(nil, nil)
doc, err := html.Parse(strings.NewReader(testDocs[0]))
if err != nil {
lge(err)
return
}
removeCommentsAndIntertagWhitespace(NdX{doc, 0})
breakoutImagesFromAnchorTrees(doc)
removeCommentsAndIntertagWhitespace(NdX{doc, 0})
reIndent(doc, 0)
var b bytes.Buffer
err = html.Render(&b, doc)
lge(err)
if b.String() != testDocs[1] {
t.Errorf("output unexpted")
}
osutilpb.Bytes2File("outp1_inp.html", []byte(testDocs[0]))
osutilpb.Dom2File("outp2_got.html", doc)
osutilpb.Bytes2File("outp3_want.html", []byte(testDocs[1]))
lg("end")
}
开发者ID:aarzilli,项目名称:tools,代码行数:29,代码来源:t_breakout_imgs_from_a_trees_test.go
示例11: SpoonerizeHTML
func SpoonerizeHTML(r io.Reader, extraHTML string) io.ReadCloser {
doc, _ := html.Parse(r)
var f func(*html.Node)
f = func(n *html.Node) {
switch n.Type {
case html.TextNode:
n.Data = string(Spoonerize([]byte(n.Data)))
case html.ElementNode:
switch n.DataAtom {
case atom.Style, atom.Script:
return
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
if n.DataAtom == atom.Body {
if extraHTML != "" {
nodes, _ := html.ParseFragment(bytes.NewBufferString(extraHTML), n)
for _, node := range nodes {
n.AppendChild(node)
}
}
}
}
f(doc)
d := &bufferCloser{}
html.Render(d, doc)
return d
}
开发者ID:freeformz,项目名称:spoonerizer,代码行数:33,代码来源:html.go
示例12: RenderClean
// RenderClean renders the provided HTML node and returns it as a string. It is
// a convenience function for html.Render.
func RenderClean(node *html.Node) (htmlStr string, err error) {
w := new(bytes.Buffer)
err = html.Render(w, node)
if err != nil {
return "", err
}
return string(w.Bytes()), nil
}
开发者ID:mewkiz,项目名称:pkg,代码行数:10,代码来源:htmlutil.go
示例13: createIndexMinHTMLFile
func createIndexMinHTMLFile(document *html.Node, dir string) {
wrtr, err := os.Create(filepath.Join(dir, "index.min.html"))
if err != nil {
log.Fatalf("Error: could not open file for write: %v", err)
}
defer closeFile(wrtr, true)
html.Render(wrtr, document)
}
开发者ID:membase,项目名称:ns_server,代码行数:8,代码来源:minify.go
示例14: saveChapter
func (this *EpubMaker) saveChapter(root *html.Node, chapters []Chapter) {
if !this.blank {
buf := new(bytes.Buffer)
html.Render(buf, root)
this.book.AddChapter(chapters, buf.Bytes())
this.blank = true
}
}
开发者ID:ssor,项目名称:makeepub,代码行数:8,代码来源:make.go
示例15: Dom2File
// Dom2File writes DOM to file
func Dom2File(fn string, node *html.Node) {
lg, _ := loghttp.BuffLoggerUniversal(nil, nil)
var b bytes.Buffer
err := html.Render(&b, node)
lg(err)
Bytes2File(fn, b.Bytes())
}
开发者ID:aarzilli,项目名称:tools,代码行数:9,代码来源:file_io.go
示例16: renderNode
func renderNode(node *html.Node) (string, error) {
var buf bytes.Buffer
err := html.Render(&buf, node)
if err != nil {
return "", err
}
return buf.String(), nil
}
开发者ID:get3w,项目名称:get3w,代码行数:8,代码来源:utils.go
示例17: Render
// Render nodes to a writer.
// nil nodes are skipped.
func (s Siblings) Render(w io.Writer) error {
doc := &html.Node{
Type: html.DocumentNode,
}
first, last := s.convert(doc)
doc.FirstChild = first
doc.LastChild = last
return html.Render(w, doc)
}
开发者ID:arnehormann,项目名称:hck,代码行数:11,代码来源:nodes.go
示例18: renderTree
func renderTree(node *html.Node) (string, error) {
var b bytes.Buffer
err := html.Render(&b, node)
if err != nil {
return "", err
}
return b.String(), nil
}
开发者ID:brandur,项目名称:singularity,代码行数:9,代码来源:toc.go
示例19: buildPlain
func buildPlain(n *html.Node) (s string, err error) {
buf := new(bytes.Buffer)
err = html.Render(buf, n)
if err != nil {
return
}
s = buf.String()
return
}
开发者ID:lufia,项目名称:news,代码行数:9,代码来源:atom.go
示例20: 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
}
开发者ID:kadmiy,项目名称:go-html-transform,代码行数:9,代码来源:h5.go
注:本文中的golang.org/x/net/html.Render函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论