-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.c
More file actions
133 lines (106 loc) · 4.07 KB
/
html.c
File metadata and controls
133 lines (106 loc) · 4.07 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
128
129
130
131
132
133
#include "syscalls/syscalls.h"
#include "alloc/allocate.h"
#include "files/helpers.h"
#include "data/format/scanner/scanner.h"
#include "input_keycodes.h"
#include "data/struct/linked_list.h"
#include "doc.h"
uintptr_t total_size = 0;
node_info interpret_tag(string_slice tag){
if (slice_lit_match(tag, "p", true))
return (node_info){doc_text_body,doc_gen_text,.fg_color = 0xFF000000, .sizing_rule = size_fit };
if (slice_lit_match(tag, "h1", true))
return (node_info){doc_text_title,doc_gen_text,.fg_color = 0xFF000000, .sizing_rule = size_fit };
if (slice_lit_match(tag, "h2", true))
return (node_info){doc_text_subtitle,doc_gen_text,.fg_color = 0xFF000000, .sizing_rule = size_fit };
if (slice_lit_match(tag, "h3", true))
return (node_info){doc_text_heading,doc_gen_text,.fg_color = 0xFF000000, .sizing_rule = size_fit };
if (slice_lit_match(tag, "h4", true))
return (node_info){doc_text_subheading,doc_gen_text,.fg_color = 0xFF000000, .sizing_rule = size_fit };
if (slice_lit_match(tag, "h5", true))
return (node_info){doc_text_footnote,doc_gen_text,.fg_color = 0xFF000000, .sizing_rule = size_fit };
if (slice_lit_match(tag, "h6", true))
return (node_info){doc_text_caption,doc_gen_text,.fg_color = 0xFF000000, .sizing_rule = size_fit };
if (slice_lit_match(tag, "script", true)){
in_case_of_js_break_glass();
}
return (node_info){};
}
document_node* emit_content(string_slice slice, node_info info){
document_node* node = zalloc(sizeof(document_node));
total_size += sizeof(document_node);
node->content = slice;
node->info = info;
return node;
}
document_node* parse_tag(Scanner *s){
document_node* node = zalloc(sizeof(document_node));
total_size += sizeof(document_node);
node->children = linked_list_create();
scan_to(s, '<');
string_slice open = scan_to(s, '>');
uint32_t in_pos = s->pos;
open.length--;
node->info = interpret_tag(open);
if (node->info.type == doc_text_none){
print("Unknown tag %v",open);
return node;
}
uint32_t pos = s->pos;
scan_to(s, '<');
linked_list_push(node->children, emit_content((string_slice){(char*)s->buf + in_pos, s->pos - in_pos - 1},node->info));
while (scan_peek(s) != '/'){
s->pos--;
linked_list_push(node->children, parse_tag(s));
in_pos = s->pos;
scan_to(s, '<');
linked_list_push(node->children, emit_content((string_slice){(char*)s->buf + in_pos, s->pos - in_pos - 1},node->info));
}
string_slice close = scan_to(s, '>');
if (*(char*)close.data != '/'){
s->pos = pos;
parse_tag(s);
}
close.data++;
close.length -= 2;
if (!slices_equal(open, close, true)){
print("Wrong tag buddy");
return node;
}
return node;
}
int main(int argc, char* argv[]){
print("Ola mundo");
size_t file_size = 0;
char *file = read_full_file("/resources/index.html", &file_size);
draw_ctx ctx = {};
ctx.width = 1920;
ctx.height = 1080;
request_draw_ctx(&ctx);
Scanner s = scanner_make(file, file_size);
document_node *root = zalloc(sizeof(document_node));
total_size += sizeof(document_node);
root->children = linked_list_create();
root->info.sizing_rule = size_fit;
root->info.general_type = doc_gen_layout;
root->info.type = doc_layout_vertical;
document_data doc = (document_data){
.root = root
};
print("total memory used: %i",total_size);
while (!scan_eof(&s)){
linked_list_push(root->children, parse_tag(&s));
}
layout_document((gpu_rect){0,0,ctx.width,ctx.height}, doc);
debug_document(doc);
while (!should_close_ctx()){
begin_drawing(&ctx);
fb_clear(&ctx, 0xFFFFFFFF);
render_document(&ctx,doc);
commit_draw_ctx(&ctx);
kbd_event ev = {};
if (read_event(&ev) && ev.key == KEY_ESC) return 0;
}
destroy_draw_ctx(&ctx);
return 0;
}