Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
127 lines (109 loc) · 2.63 KB
/
Copy pathmain.go
File metadata and controls
127 lines (109 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"flag"
"image"
"image/png"
"io/ioutil"
"log"
"os"
"github.com/lysrt/bro/css"
"github.com/lysrt/bro/html"
"github.com/lysrt/bro/html/lexer"
"github.com/lysrt/bro/html/parser"
"github.com/lysrt/bro/layout"
"github.com/lysrt/bro/paint"
"github.com/lysrt/bro/style"
)
func main() {
var (
htmlInput string
cssInput string
pngOutput string
)
flag.StringVar(&htmlInput, "html", "input.html", "-html input.html")
flag.StringVar(&cssInput, "css", "input.css", "-css input.css")
flag.StringVar(&pngOutput, "o", "out.png", "-o out.png")
flag.Parse()
var (
domNodes *html.Node
styleSheet *css.Stylesheet
)
//
// 1. Construct the DOM tree
//
htmlFile, err := os.Open(htmlInput)
if err != nil {
log.Fatalf("cannot open HTML file: %q", err)
}
// domNodes, err = html.Parse(htmlFile)
b, err := ioutil.ReadAll(htmlFile)
if err != nil {
log.Fatalf("cannot read HTML gile: %q", err)
}
l := lexer.New(string(b))
p := parser.New(l)
domNodes = p.Parse()
if len(p.Errors()) > 0 {
log.Println("cannot parse HTML file")
for _, e := range p.Errors() {
log.Printf("%q (l: %d, c: %d)\n", e, e.Token.Line, e.Token.LinePosition)
}
log.Fatal()
}
htmlFile.Close()
// dom.Parcour(domNodes)
//
// 2. Parse the CSS to a *Stylesheet
//
cssFile, err := os.Open(cssInput)
if err != nil {
log.Fatalf("cannot open stylesheet: %q", err)
}
parser := css.NewParser(cssFile)
cssFile.Close()
styleSheet = parser.ParseStylesheet()
if len(parser.Errors()) > 0 {
for _, e := range parser.Errors() {
log.Printf("parsing error: %q\n", e)
}
}
// fmt.Println(styleSheet)
//
// 3. Decorate the DOM to generate the Style Tree
//
styleTree := style.GenerateStyleTree(domNodes, styleSheet)
// fmt.Println(styleTree)
//
// 4.1 Build the Layout Tree
//
layoutTree := layout.GenerateLayoutTree(styleTree)
// fmt.Println(layoutTree)
//
// 4.2 Parcour the layout tree to compute boxes dimensions
//
viewport := layout.Dimensions{Content: layout.Rect{Width: 300}}
layoutTree.Layout(viewport)
// fmt.Println(layoutTree)
//
// 5. Paint the output from the Layout Tree
//
pixels, err := paint.Paint(layoutTree)
if err != nil {
log.Fatalf("cannot paint from layout tree: %q", err)
}
//fmt.Println(pixels)
writeOutput(pngOutput, pixels)
}
func writeOutput(outputFileName string, pixels image.Image) {
f, err := os.Create(outputFileName)
if err != nil {
log.Fatalf("cannot open output file: %q", err)
}
if err := png.Encode(f, pixels); err != nil {
f.Close()
log.Fatalf("cannot encode PNG: %q", err)
}
if err := f.Close(); err != nil {
log.Fatalf("cannot close output file: %q", err)
}
}