-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Mickaël Menu <[email protected]>
- Loading branch information
1 parent
70e1626
commit 7dabb92
Showing
42 changed files
with
2,391 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package content | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/readium/go-toolkit/pkg/content/element" | ||
"github.com/readium/go-toolkit/pkg/content/iterator" | ||
) | ||
|
||
type Content interface { | ||
Text(separator *string) (string, error) // Extracts the full raw text, or returns null if no text content can be found. | ||
Iterator() iterator.Iterator // Creates a new iterator for this content. | ||
Elements() ([]element.Element, error) // Returns all the elements as a list. | ||
} | ||
|
||
// Extracts the full raw text, or returns null if no text content can be found. | ||
func ContentText(content Content, separator *string) (string, error) { | ||
sep := "\n" | ||
if separator != nil { | ||
sep = *separator | ||
} | ||
var sb strings.Builder | ||
els, err := content.Elements() | ||
if err != nil { | ||
return "", err | ||
} | ||
for _, el := range els { | ||
if txel, ok := el.(element.TextualElement); ok { | ||
txt := txel.Text() | ||
if txt != "" { | ||
sb.WriteString(txel.Text()) | ||
sb.WriteString(sep) | ||
} | ||
} | ||
} | ||
return strings.TrimSuffix(sb.String(), sep), nil | ||
} | ||
|
||
func ContentElements(content Content) ([]element.Element, error) { | ||
var elements []element.Element | ||
it := content.Iterator() | ||
for { | ||
hasNext, err := it.HasNext() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !hasNext { | ||
break | ||
} | ||
elements = append(elements, it.Next()) | ||
} | ||
return elements, nil | ||
} |
Oops, something went wrong.