diff --git a/src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia.go b/src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia.go
index a0ecd38..3c540bf 100644
--- a/src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia.go
+++ b/src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia.go
@@ -7,15 +7,18 @@ import (
"io/ioutil"
"net/http"
"net/url"
+ "strconv"
"strings"
log "github.com/Sirupsen/logrus"
+ "github.com/jaytaylor/html2text"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
)
// ServiceType of the Wikipedia service
const ServiceType = "wikipedia"
+const maxExtractLength = 1024 // Max length of extract string in bytes
var httpClient = &http.Client{}
@@ -81,9 +84,25 @@ func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID str
}, nil
}
+ extractText, err := html2text.FromString(searchResultPage.Extract)
+ if err != nil {
+ return gomatrix.TextMessage{
+ MsgType: "m.notice",
+ Body: "Failed to convert extract to plain text - " + err.Error(),
+ }, nil
+ }
+
+ // Truncate the extract text, if necessary
+ if len(extractText) > maxExtractLength {
+ extractText = extractText[:maxExtractLength] + "..."
+ }
+
+ // Add a link to the bottom of the extract
+ extractText += "\n" + "http://en.wikipedia.org/?curid=" + strconv.FormatInt(searchResultPage.PageID, 10)
+
return gomatrix.TextMessage{
MsgType: "m.notice",
- Body: searchResultPage.Extract,
+ Body: extractText,
}, nil
}
@@ -122,12 +141,18 @@ func (s *Service) text2Wikipedia(query string) (*wikipediaPage, error) {
// log.Info(response2String(res))
if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil {
return nil, fmt.Errorf("ERROR - %s", err.Error())
- } else if len(searchResults.Pages) < 1 {
+ } else if len(searchResults.Query.Pages) < 1 {
return nil, fmt.Errorf("No articles found")
}
- // Return only the first search result
- return &searchResults.Pages[0], nil
+ // Return only the first search result with an extract
+ for _, page := range searchResults.Query.Pages {
+ if page.Extract != "" {
+ return &page, nil
+ }
+ }
+
+ return nil, fmt.Errorf("No articles with extracts found")
}
// response2String returns a string representation of an HTTP response body
diff --git a/vendor/manifest b/vendor/manifest
index 3070089..1bf7484 100644
--- a/vendor/manifest
+++ b/vendor/manifest
@@ -129,6 +129,12 @@
"revision": "d02018f006d98f58512bf3adfc171d88d17626df",
"branch": "master"
},
+ {
+ "importpath": "github.com/jaytaylor/html2text",
+ "repository": "https://github.com/jaytaylor/html2text",
+ "revision": "24f9b0f63599c6bbecc3b773636b54f8d302db67",
+ "branch": "master"
+ },
{
"importpath": "github.com/matrix-org/dugong",
"repository": "https://github.com/matrix-org/dugong",
diff --git a/vendor/src/github.com/jaytaylor/html2text/LICENSE b/vendor/src/github.com/jaytaylor/html2text/LICENSE
new file mode 100644
index 0000000..24dc4ab
--- /dev/null
+++ b/vendor/src/github.com/jaytaylor/html2text/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 Jay Taylor
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/vendor/src/github.com/jaytaylor/html2text/README.md b/vendor/src/github.com/jaytaylor/html2text/README.md
new file mode 100644
index 0000000..ac11247
--- /dev/null
+++ b/vendor/src/github.com/jaytaylor/html2text/README.md
@@ -0,0 +1,116 @@
+# html2text
+
+[![Documentation](https://godoc.org/github.com/jaytaylor/html2text?status.svg)](https://godoc.org/github.com/jaytaylor/html2text)
+[![Build Status](https://travis-ci.org/jaytaylor/html2text.svg?branch=master)](https://travis-ci.org/jaytaylor/html2text)
+[![Report Card](https://goreportcard.com/badge/github.com/jaytaylor/html2text)](https://goreportcard.com/report/github.com/jaytaylor/html2text)
+
+### Converts HTML into text
+
+
+## Introduction
+
+Ensure your emails are readable by all!
+
+Turns HTML into raw text, useful for sending fancy HTML emails with a equivalently nicely formatted TXT document as a fallback (e.g. for people who don't allow HTML emails or have other display issues).
+
+html2text is a simple golang package for rendering HTML into plaintext.
+
+There are still lots of improvements to be had, but FWIW this has worked fine for my [basic] HTML-2-text needs.
+
+It requires go 1.x or newer ;)
+
+
+## Download the package
+
+```bash
+go get github.com/jaytaylor/html2text
+```
+
+## Example usage
+
+```go
+package main
+
+import (
+ "fmt"
+
+ "github.com/jaytaylor/html2text"
+)
+
+func main() {
+ inputHtml := `
+
+
Lorem ipsum Commodo id consectetur pariatur ea occaecat minim aliqua ad sit consequat quis ex commodo Duis incididunt eu mollit consectetur fugiat voluptate dolore in pariatur in commodo occaecat Ut occaecat velit esse labore aute quis commodo non sit dolore officia Excepteur cillum amet cupidatat culpa velit labore ullamco dolore mollit elit in aliqua dolor irure do
",
+ "> \n> Lorem ipsum Commodo id consectetur pariatur ea occaecat minim aliqua ad\n> sit consequat quis ex commodo Duis incididunt eu mollit consectetur fugiat\n> voluptate dolore in pariatur in commodo occaecat Ut occaecat velit esse\n> labore aute quis commodo non sit dolore officia Excepteur cillum amet\n> cupidatat culpa velit labore ullamco dolore mollit elit in aliqua dolor\n> irure do",
+ },
+ {
+ "