-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quick Fix, Issue occured due to git pull
- Loading branch information
Anonymous Indian
committed
Aug 16, 2021
1 parent
40a6cef
commit 31fc417
Showing
5 changed files
with
112 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package telegraph | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"strings" | ||
|
||
"golang.org/x/net/html" | ||
) | ||
|
||
func ContentFormat(data interface{}) (n []Node, err error) { | ||
var dst *html.Node | ||
|
||
switch src := data.(type) { | ||
case string: | ||
dst, err = html.Parse(strings.NewReader(src)) | ||
case []byte: | ||
dst, err = html.Parse(bytes.NewReader(src)) | ||
case io.Reader: | ||
dst, err = html.Parse(src) | ||
default: | ||
return nil, errors.New("INVALID DATA TYPE") | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
n = append(n, domToNode(dst.FirstChild)) | ||
|
||
return n, nil | ||
} | ||
|
||
func domToNode(domNode *html.Node) interface{} { | ||
if domNode.Type == html.TextNode { | ||
return domNode.Data | ||
} | ||
|
||
if domNode.Type != html.ElementNode { | ||
return nil | ||
} | ||
|
||
nodeElement := new(NodeElement) | ||
|
||
switch strings.ToLower(domNode.Data) { | ||
case "a", "aside", "b", "blockquote", "br", "code", "em", "figcaption", "figure", "h3", "h4", "hr", "i", | ||
"iframe", "img", "li", "ol", "p", "pre", "s", "strong", "u", "ul", "video": | ||
nodeElement.Tag = domNode.Data | ||
|
||
for i := range domNode.Attr { | ||
switch strings.ToLower(domNode.Attr[i].Key) { | ||
case "href", "src": | ||
nodeElement.Attrs = map[string]string{domNode.Attr[i].Key: domNode.Attr[i].Val} | ||
default: | ||
continue | ||
} | ||
} | ||
} | ||
|
||
for child := domNode.FirstChild; child != nil; child = child.NextSibling { | ||
nodeElement.Children = append(nodeElement.Children, domToNode(child)) | ||
} | ||
|
||
return nodeElement | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/anonyindian/telegraph-go | ||
|
||
go 1.16 | ||
|
||
require golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d |
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,7 @@ | ||
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c= | ||
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
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