示例#1
0
func (n *node) String() string {
	var buffer = new(bytes.Buffer)

	html.Render(buffer, n.toNode())

	return string(buffer.Bytes())
}
示例#2
0
func ExtractTagi(reader io.Reader) (io.Reader, error) {
	root, err := html.Parse(reader)

	if err != nil {
		return nil, err
	}

	var r = toNode(root)

	defer r.Dispose()

	var sp = r.descendant(Id("singlePage"))

	if sp == nil {
		return nil, errors.New("singlePage not found \n" + r.String())
	}

	var p = sp.descendants(Tag("p"))

	if p == nil {
		return nil, errors.New("p's not found \n" + r.String())
	}

	var buffer = new(bytes.Buffer)

	for _, node := range p {
		html.Render(buffer, node.toNode())
		buffer.WriteByte('\n')
	}

	return buffer, nil
}
示例#3
0
func TestMalformed(t *testing.T) {
	// some mal-formed html
	type test struct {
		src string
		exp string
	}
	tests := []test{
		{`<p><span>Foo</i></p>`, `<html><head></head><body><p><span>Foo</span></p></body></html>`},
	}

	for _, test := range tests {
		r := bytes.NewBufferString(test.src)
		nodes, err := html.ParseFragment(r, nil)
		if err != nil {
			t.Fatal(err)
		}

		b := new(bytes.Buffer)
		for _, v := range nodes {
			err := html.Render(b, v)
			if err != nil {
				t.Error(err)
			}
		}
		if b.String() != test.exp {
			t.Errorf("expected %q, got %q", test.exp, b)
		}
	}
}
示例#4
0
// Html() gets the HTML contents of the first element in the set of matched
// elements. It includes text and comment nodes.
func (this *Selection) Html() (ret string, e error) {
	// Since there is no .innerHtml, the HTML content must be re-created from
	// the nodes usint html.Render().
	var buf bytes.Buffer

	if len(this.Nodes) > 0 {
		for c := this.Nodes[0].FirstChild; c != nil; c = c.NextSibling {
			e = html.Render(&buf, c)
			if e != nil {
				return
			}
		}
		ret = buf.String()
	}

	return
}
示例#5
0
func ExtractBlickOld(reader io.Reader) (io.Reader, error) {
	root, err := html.Parse(reader)

	if err != nil {
		return nil, err
	}

	var r = toNode(root)
	defer r.Dispose()

	var art = r.descendant(Class("article"))
	if art == nil {
		return nil, errors.New("article not found \n" + r.String())
	}

	var buffer = new(bytes.Buffer)
	html.Render(buffer, art.toNode())

	return buffer, nil
}
示例#6
0
文件: render.go 项目: dunmatt/gats
func RenderTemplateFile(filename string, data interface{}, out io.Writer) error {
	rootNode, err := parseFile(filename)
	if err != nil {
		return err
	}
	cont := makeContext(data, nil)
	template := goquery.NewDocumentFromNode(rootNode).Find("html") // wrap goquery around the DOM

	handleGatsRemoves(template)           // take out everything that definitely won't be shown
	err = handleGatsTranscludes(template) // transclude in all the sub-templates
	if err != nil {
		return err
	}
	err = fillInTemplate(template, cont) // process the template
	handleGatsOmitTag(template)          // make sure this one comes last, it can interfere with other attributes
	if err == nil {
		html.Render(out, rootNode) // render the DOM back to html and send it off
	}
	return err
}
示例#7
0
func feed(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		if strings.HasPrefix(r.Header.Get("Accept"), "application/json") {
			feedId := r.URL.Path[len("/feed/"):]
			feed := &dbFeed{}

			// u := "http://loc-blog.de/rss.php?blog_id=5"

			session := db.session.Copy()
			c := session.DB("test").C("feeds")

			numResults, err := c.FindId(bson.ObjectIdHex(feedId)).Count()
			if err != nil {
				panic(err)
			}

			if numResults == 0 {
				// feed = insertFeed(u)
			} else {
				err = c.FindId(bson.ObjectIdHex(feedId)).One(&feed)
				if err != nil {
					panic(err)
				}
			}

			u, _ := url.Parse(feed.Url)

			for i, _ := range feed.Feed.Items {
				doc, err := html.Parse(strings.NewReader(feed.Feed.Items[i].Content))
				if err != nil {
					log.Fatal(err)
				}
				var f func(*html.Node, *url.URL)
				f = func(n *html.Node, u *url.URL) {
					if n.Type == html.ElementNode && n.Data == "img" {
						for i, _ := range n.Attr {
							if n.Attr[i].Key == "src" {
								u2, _ := url.Parse(n.Attr[i].Val)
								if !u2.IsAbs() {
									u2.Scheme = u.Scheme
									u2.Host = u.Host
								}
								if !strings.HasPrefix(u2.Path, "/") {
									u2.Path = "/" + u2.Path
								}
								n.Attr[i].Val = u2.String()
								break
							}
						}
					}
					if n.Type == html.ElementNode && n.Data == "a" {
						found := false
						for i, _ := range n.Attr {
							if n.Attr[i].Key == "target" {
								n.Attr[i].Val = "_blank"
								found = true
								break
							}
						}
						if !found {
							attr := new(html.Attribute)
							attr.Key = "target"
							attr.Val = "_blank"
							n.Attr = append(n.Attr, *attr)
						}
					}
					for c := n.FirstChild; c != nil; c = c.NextSibling {
						f(c, u)
					}
				}
				f(doc, u)
				var wr bytes.Buffer
				html.Render(&wr, doc)
				feed.Feed.Items[i].Content = wr.String()
			}

			respJSON, _ := json.Marshal(feed)
			fmt.Fprint(w, string(respJSON))
		} else {
			indexHandler(w, r)
		}
	} else if r.Method == "POST" {
		body, _ := ioutil.ReadAll(r.Body)
		feed := new(dbFeed)
		_ = json.Unmarshal(body, feed)
		log.Printf("Add Feed: %v", feed.Url)
		respJSON, _ := json.Marshal(insertFeed(feed.Url))
		fmt.Fprint(w, string(respJSON))
	}
}