-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 336ad6a
Showing
7 changed files
with
206 additions
and
0 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,3 @@ | ||
<p align="center"> | ||
<img src="thdwb.png?raw=true"></img> | ||
</p> |
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,25 @@ | ||
<html> | ||
|
||
<head> | ||
<title>Hello, world!</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Hello, world!</h1> | ||
<p>Lorem ipsum, dolor sit amet.</p> | ||
<div> | ||
<p>Lorem ipsum, dolor sit amet.</p> | ||
<div> | ||
DIV | ||
<h2> | ||
opa | ||
</h2> | ||
</div> | ||
</div> | ||
<div> | ||
<p>Lorem ipsum, dolor sit amet.</p> | ||
<h1>Hello, world!</h1> | ||
</div> | ||
</body> | ||
|
||
</html> |
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,70 @@ | ||
package ketchup | ||
|
||
import "regexp" | ||
import "strings" | ||
|
||
var xmlTag = regexp.MustCompile(`(\<\w+\>)|(\<//?\w+\>\\?)`) | ||
var clTag = regexp.MustCompile(`\<\/\w+\>`) | ||
var tagContent = regexp.MustCompile(`(.+?)\<\/`) | ||
|
||
type DOM_Node struct { | ||
Element string `json:"element"` | ||
Content string `json:"content"` | ||
Children []*DOM_Node `json:"children"` | ||
parent *DOM_Node | ||
} | ||
|
||
func ParseHTML(document string) *DOM_Node { | ||
DOM_Tree := &DOM_Node{ | ||
Element: "root", | ||
Content: "THDWB", | ||
Children: []*DOM_Node{}, | ||
parent: nil, | ||
} | ||
|
||
lastNode := DOM_Tree | ||
parseDocument := xmlTag.MatchString(document) | ||
document = strings.ReplaceAll(document, "\n", "") | ||
|
||
for parseDocument == true { | ||
var currentNode *DOM_Node | ||
|
||
currentTag := xmlTag.FindString(document) | ||
currentTagIndex := xmlTag.FindStringIndex(document) | ||
|
||
if clTag.MatchString(currentTag) { | ||
contentStringMatch := tagContent.FindStringSubmatch(document) | ||
contentString := "" | ||
|
||
if len(contentStringMatch) > 1 { | ||
contentString = contentStringMatch[1] | ||
} | ||
|
||
if clTag.MatchString(contentString) { | ||
lastNode.Content = "" | ||
} else { | ||
lastNode.Content = strings.TrimSpace(contentString) | ||
} | ||
|
||
lastNode = lastNode.parent | ||
} else { | ||
currentNode = &DOM_Node{ | ||
Element: strings.Trim(currentTag, "></"), | ||
Content: "", | ||
Children: []*DOM_Node{}, | ||
parent: lastNode, | ||
} | ||
|
||
lastNode.Children = append(lastNode.Children, currentNode) | ||
lastNode = currentNode | ||
} | ||
|
||
document = document[currentTagIndex[1] : len(document)] | ||
|
||
if !xmlTag.MatchString(document) { | ||
parseDocument = false | ||
} | ||
} | ||
|
||
return DOM_Tree | ||
} |
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,22 @@ | ||
package main | ||
|
||
import "os" | ||
import "fmt" | ||
import "encoding/json" | ||
|
||
import "github.com/danfragoso/thdwb/mustard" | ||
import "github.com/danfragoso/thdwb/ketchup" | ||
import "github.com/danfragoso/thdwb/sauce" | ||
|
||
func main() { | ||
url := os.Args[1] | ||
|
||
resource := sauce.GetResource(url) | ||
bodyString := string(resource.Body) | ||
|
||
DOM_Tree := ketchup.ParseHTML(bodyString) | ||
js, err := json.MarshalIndent(DOM_Tree, "", " ") | ||
fmt.Println(err) | ||
fmt.Println(string(js)) | ||
mustard.RenderDOM(DOM_Tree) | ||
} |
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,60 @@ | ||
package mustard | ||
|
||
import "github.com/gotk3/gotk3/cairo" | ||
import "github.com/gotk3/gotk3/gdk" | ||
import "github.com/gotk3/gotk3/gtk" | ||
import "github.com/danfragoso/thdwb/ketchup" | ||
import "log" | ||
|
||
const KEY_LEFT uint = 65361 | ||
const KEY_UP uint = 65362 | ||
const KEY_RIGHT uint = 65363 | ||
const KEY_DOWN uint = 65364 | ||
|
||
func RenderDOM(DOM_Tree *ketchup.DOM_Node) { | ||
gtk.Init(nil) | ||
|
||
browserWindow, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL) | ||
drawingArea, _ := gtk.DrawingAreaNew() | ||
|
||
browserWindow.Add(drawingArea) | ||
|
||
header, err := gtk.HeaderBarNew() | ||
if err != nil { | ||
log.Fatal("Could not create header bar:", err) | ||
} | ||
|
||
header.SetShowCloseButton(true) | ||
header.SetTitle(DOM_Tree.Content) | ||
header.SetSubtitle("thdwb") | ||
|
||
browserWindow.SetTitlebar(header) | ||
browserWindow.Connect("destroy", gtk.MainQuit) | ||
browserWindow.ShowAll() | ||
|
||
unitSize := 20.0 | ||
x := 0.0 | ||
y := 0.0 | ||
keyMap := map[uint]func(){ | ||
KEY_LEFT: func() { x-- }, | ||
KEY_UP: func() { y-- }, | ||
KEY_RIGHT: func() { x++ }, | ||
KEY_DOWN: func() { y++ }, | ||
} | ||
|
||
drawingArea.Connect("draw", func(drawingArea *gtk.DrawingArea, cr *cairo.Context) { | ||
cr.SetSourceRGB(0, 0, 0) | ||
cr.Rectangle(x*unitSize, y*unitSize, unitSize, unitSize) | ||
cr.Fill() | ||
}) | ||
|
||
browserWindow.Connect("key-press-event", func(browserWindow *gtk.Window, ev *gdk.Event) { | ||
keyEvent := &gdk.EventKey{ev} | ||
if move, found := keyMap[keyEvent.KeyVal()]; found { | ||
move() | ||
browserWindow.QueueDraw() | ||
} | ||
}) | ||
|
||
gtk.Main() | ||
} |
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,26 @@ | ||
package sauce | ||
|
||
import "io/ioutil" | ||
import "net/http" | ||
|
||
type Resource struct { | ||
Body string | ||
Code int | ||
} | ||
|
||
func GetResource(url string) *Resource { | ||
res, err := http.Get(url) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer res.Body.Close() | ||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return &Resource{ | ||
Body: string(body), | ||
} | ||
} |