Documentation ¶
Overview ¶
Package swimmy is a package that fetch and process URL Info for embedding external site information as card or outputting as JSON. First: swimmy fetch url information (html document and text document). Second: swimmy sanitize html contents and parse it in order to get the information of webpage.
Index ¶
- Constants
- Variables
- func CPolicy() *bluemonday.Policy
- func CommentifyString(input string) string
- func DefaultClasses() map[string]string
- func DefaultTemplate() *template.Template
- func EscapeBytes(str []byte) string
- func ExecLicenseTextTemp(templateStr string, data interface{}) (string, error)
- func Fetch(url string) (string, string, []byte, error)
- func Init()
- func IsExistFilePath(val string) (bool, error)
- func IsFilePath(val string) (bool, error)
- func IsPlainTextContentType(ctype string) bool
- func ParseTime(timeStr string) (*time.Time, string, error)
- func ReadList(listPath string) ([]string, error)
- func Sanitize(htmlContent string, policy ...*bluemonday.Policy) string
- func ShowLicense() string
- func TPolicy() *bluemonday.Policy
- func TakeMarkedUpText(ct *html.Tokenizer, tagName []byte) string
- func WriteCurrentString(tokenizer *html.Tokenizer, tokenType html.TokenType, sb *strings.Builder)
- func WriteHTML(pd *PageData, cb *CardBuilder, w io.Writer, messageWriter io.Writer, ...) error
- func WriteJSON(pd *PageData, w io.Writer, messageWriter io.Writer, hasPrev bool) error
- type CardBuilder
- type ContentFetcher
- type FetchError
- type ImageData
- type OpenGraphProtocol
- type PageData
- type PageDataBuilder
Constants ¶
const ( //IsNotURLError represents error occur from input is not URL IsNotURLError = iota //BadEncodeError represents url contents is not encoded with encode methods that swimmy can handle BadEncodeError //InvalidContentTypeError represents content is not html or text InvalidContentTypeError //StatusError is StatusError StatusError )
Variables ¶
var IDCount int
IDCount count of PageData's ID
Functions ¶
func CommentifyString ¶
CommentifyString commentify string inspired by cobra's commentifyString
func DefaultClasses ¶
DefaultClasses return default classNames in card as map
func DefaultTemplate ¶
DefaultTemplate return swimmy's default template
func ExecLicenseTextTemp ¶
ExecLicenseTextTemp exec template using templateStr and data
func Init ¶
func Init()
Init is initialize function. If you want to use default variables, use this function.
func IsExistFilePath ¶
IsExistFilePath validate whether val is exist filepath or not and confirm that it exist and it is not directory.
func IsPlainTextContentType ¶
IsPlainTextContentType return whether given contentType represents text/plain or not
func Sanitize ¶
func Sanitize(htmlContent string, policy ...*bluemonday.Policy) string
Sanitize sanitize html or txt with blueMonday
func TakeMarkedUpText ¶
TakeMarkedUpText is take marked-up text between begin tag and end tag
func WriteCurrentString ¶
WriteCurrentString write string of now tag or text to strings.Builder
Types ¶
type CardBuilder ¶
CardBuilder build card string from pagedata
var DefaultCardBuilder *CardBuilder
DefaultCardBuilder is swimmy's default CardDataBuilder
func NewCardBuilder ¶
func NewCardBuilder(cardtemplate *template.Template, classnames map[string]string) *CardBuilder
NewCardBuilder create a empty instance of CardBuilder and return it
func (*CardBuilder) Execute ¶
func (cb *CardBuilder) Execute(pd *PageData, w io.Writer) error
Execute build card by execute html template.
func (*CardBuilder) WriteCardHTML ¶
func (cb *CardBuilder) WriteCardHTML(pd *PageData, w io.Writer, minify bool)
WriteCardHTML write card html tag.
type ContentFetcher ¶
ContentFetcher fetch net content in Fetch(url string)
var DefaultContentFetcher *ContentFetcher
DefaultContentFetcher is swimmy's defaultContentFetcher
func NewContentFetcher ¶
func NewContentFetcher(HTTPClient *http.Client) *ContentFetcher
NewContentFetcher create new instance of ContentFetcher
type FetchError ¶ added in v1.0.1
type FetchError struct { ErrorType int // contains filtered or unexported fields }
FetchError is error struct for fetch
func NewFetchError ¶ added in v1.0.1
func NewFetchError(t int, s string) *FetchError
NewFetchError create fetch error
func (*FetchError) Error ¶ added in v1.0.1
func (fe *FetchError) Error() string
Error is implement of fetcherror for error interface
type ImageData ¶
type ImageData struct { URL string `json:"URL"` SecureURL string `json:"SecureURL"` FormatType string `json:"FormatType"` AltText string `json:"AltText"` Width int `json:"Width"` Height int `json:"Height"` }
ImageData storage properties of image
func CreateImageData ¶
CreateImageData return new instance of ImageData
func NewImageData ¶
func NewImageData() *ImageData
NewImageData return new initialized(emply) instance of ImageData
type OpenGraphProtocol ¶
type OpenGraphProtocol struct { URL string `json:"URL"` SiteName string `json:"SiteName"` Title string `json:"Title"` Description string `json:"Description"` Locale string `json:"Locale"` Type string `json:"Type"` OgImage *ImageData `json:"OgImage"` TwitterImage *ImageData `json:"TwitterImage"` TwitterID string `json:"TwitterID"` UpdatedTime *time.Time `json:"UpdatedTime"` OtherAttrs map[string]string `json:"OtherAttrs"` OtherInfo map[string]string `json:"OtherInfo"` }
OpenGraphProtocol is strage for open graph protocol. OpenGraphProtocol in swimmy is only for creating data for embedding in website, so it does not storage video and music.
func (*OpenGraphProtocol) Set ¶
func (ogp *OpenGraphProtocol) Set(nameAttr, contentAttr string)
Set set meta values to ogp fields. contentAttr is assumed after sanitizing.
type PageData ¶
type PageData struct { URL string `json:"URL"` ID int `json:"ID"` CannonicalURL string `json:"CannonicalURL"` ContentType string `json:"ContentType"` Title string `json:"Title"` Description string `json:"Description"` FaviconURL []string `json:"FaviconURL"` OGP *OpenGraphProtocol `json:"OGP"` }
PageData is a struct for storage data(information) of web page specified with url in order to create embed card or json data
func BuildPageData ¶
BuildPageData build pagedata on base pagedata
func ErrorPageData ¶ added in v1.0.1
ErrorPageData return pagedata if get err in fetch.
func FetchAndBuildPageData ¶
FetchAndBuildPageData fetch information about url and build pagedata
func NewPageData ¶
NewPageData return new instance of PageData
func (*PageData) ComplementBasicFields ¶
func (pd *PageData) ComplementBasicFields()
ComplementBasicFields complement pagedata basic fields if some basic field is empty.
func (*PageData) IsPlainText ¶
IsPlainText return whether pagedata is text/plain or not.
type PageDataBuilder ¶
type PageDataBuilder struct { PreSanitizePolicy *bluemonday.Policy TagContentSanitizePolicy *bluemonday.Policy }
PageDataBuilder is processer for creating pagedata
var DefaultPageDataBuilder *PageDataBuilder
DefaultPageDataBuilder is swimmy's default PageDataBuilder
func NewPageDataBuilder ¶
func NewPageDataBuilder(PrePolicy, tagContentPolicy *bluemonday.Policy) *PageDataBuilder
NewPageDataBuilder generate New instance of PageDataBuilder
func (*PageDataBuilder) BuildPageData ¶
func (p *PageDataBuilder) BuildPageData(url string, ctype string, htmlContent string) *PageData
BuildPageData parse html content, retrieve tag info and fill PageData. Before parsing, Parse sanitize html content with its SanitizePolicy.
func (*PageDataBuilder) Sanitize ¶
func (p *PageDataBuilder) Sanitize(htmlContent string) string
Sanitize sanitize html content with p's sanitize policy.
func (*PageDataBuilder) TagContentSanitize ¶
func (p *PageDataBuilder) TagContentSanitize(str string) string
TagContentSanitize sanitize content of tag