コード例 #1
0
ファイル: client.go プロジェクト: BrettBukowski/how
// For every <a> in the selection, replace with a MD-style link.
// Stuffs the results into the supplied channel.
func fetchFormattedResult(l page.Page, results chan<- string) {
	doc, _ := l.Fetch()
	answer := doc.Find(".answer")

	if answer.Nodes == nil {
		results <- fmt.Sprintf("No answers given for <%s>", l.Url)
		return
	}

	selection := answer.First().Find(".post-text")
	text := selection.Text()

	selection.Find("a").Each(func(i int, s *goquery.Selection) {
		href, _ := s.Attr("href")
		linkText := s.Text()

		var mdPage = []string{
			"[",
			linkText,
			"](",
			href,
			")",
		}

		if strings.EqualFold(linkText, href) {
			mdPage = []string{
				"<", linkText, ">",
			}
		}

		text = strings.Replace(text, linkText, strings.Join(mdPage, ""), 1)
	})
	results <- linkHeading(l) + text
}
コード例 #2
0
ファイル: client.go プロジェクト: BrettBukowski/how
// Gleam result links out of Google's search result page.
func extractPages(doc *goquery.Document, numberToExtract int) []page.Page {
	var links []page.Page

	doc.Find("#res a").Each(func(i int, s *goquery.Selection) {
		href, _ := s.Attr("href")

		if acceptResultUrl(href) {
			resultPage := page.Page{href, s.Text()}
			resultPage.NormalizeResultUrl()
			links = append(links, resultPage)
		}
	})

	numberOfLinks := len(links)

	if numberToExtract > numberOfLinks {
		numberToExtract = numberOfLinks - 1
	}

	if numberToExtract <= 0 {
		return links
	}

	return links[0:numberToExtract]
}