Skip to content

Commit

Permalink
files
Browse files Browse the repository at this point in the history
  • Loading branch information
danfragoso committed Jul 29, 2019
0 parents commit 336ad6a
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
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>
25 changes: 25 additions & 0 deletions index.html
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>
70 changes: 70 additions & 0 deletions ketchup/ketchup.go
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
}
22 changes: 22 additions & 0 deletions main.go
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)
}
60 changes: 60 additions & 0 deletions mustard/mustard.go
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()
}
26 changes: 26 additions & 0 deletions sauce/sauce.go
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),
}
}
Binary file added thdwb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 336ad6a

Please sign in to comment.