Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tokenizer_colorize_high_level.c « myhtml « examples - github.com/lexborisov/Modest.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05ee28feadaaa8aed6eefc8e921a0651416db701 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 Copyright (C) 2015-2016 Alexander Borisov
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
 Author: lex.borisov@gmail.com (Alexander Borisov)
*/

#include <stdio.h>
#include <stdlib.h>

#include <myhtml/api.h>

#include "example.h"

struct res_html {
    char  *html;
    size_t size;
};

struct res_html load_html_file(const char* filename)
{
    FILE *fh = fopen(filename, "rb");
    if(fh == NULL) {
        fprintf(stderr, "Can't open html file: %s\n", filename);
        exit(EXIT_FAILURE);
    }
    
    if(fseek(fh, 0L, SEEK_END) != 0) {
        fprintf(stderr, "Can't set position (fseek) in file: %s\n", filename);
        exit(EXIT_FAILURE);
    }
    
    long size = ftell(fh);
    
    if(fseek(fh, 0L, SEEK_SET) != 0) {
        fprintf(stderr, "Can't set position (fseek) in file: %s\n", filename);
        exit(EXIT_FAILURE);
    }
    
    if(size <= 0) {
        fprintf(stderr, "Can't get file size or file is empty: %s\n", filename);
        exit(EXIT_FAILURE);
    }
    
    char *html = (char*)malloc(size + 1);
    if(html == NULL) {
        fprintf(stderr, "Can't allocate mem for html file: %s\n", filename);
        exit(EXIT_FAILURE);
    }
    
    size_t nread = fread(html, 1, size, fh);
    if (nread != size) {
        fprintf(stderr, "could not read %ld bytes (" MyCORE_FMT_Z " bytes done)\n", size, nread);
        exit(EXIT_FAILURE);
    }

    fclose(fh);
    
    struct res_html res = {html, (size_t)size};
    return res;
}

void colorize_print(mycore_incoming_buffer_t *inc_buf, size_t begin, size_t length, const char* color)
{
    if(length) {
        inc_buf = mycore_incoming_buffer_find_by_position(inc_buf, begin);
        
        size_t between_begin  = (begin - mycore_incoming_buffer_offset(inc_buf));
        const char* between_data = mycore_incoming_buffer_data(inc_buf);
        
        printf("%s%.*s\e[0m", color, (int)length, &between_data[between_begin]);
    }
}

size_t colorize_print_attributes(myhtml_tree_t* tree, myhtml_tree_attr_t* attr, mycore_incoming_buffer_t *inc_buf, size_t last_pos)
{
    while(attr)
    {
        myhtml_position_t key_pos = myhtml_attribute_key_raw_position(attr);
        myhtml_position_t value_pos = myhtml_attribute_value_raw_position(attr);
        
        if(key_pos.length)
        {
            /* print <div[ ]key=value> */
            if(last_pos < key_pos.begin)
                colorize_print(inc_buf, last_pos, (key_pos.begin - last_pos), "\e[31m");
            
            /* print <div [key]=value> */
            colorize_print(inc_buf, key_pos.begin, key_pos.length, "\e[33m");
            
            /* get/check max position */
            if((key_pos.begin + key_pos.length) > last_pos)
                last_pos = key_pos.begin + key_pos.length;
        }
        else {
            /* print <div[ ]value> */
            if(value_pos.length && last_pos < value_pos.begin)
                colorize_print(inc_buf, last_pos, (value_pos.begin - last_pos), "\e[31m");
        }
        
        if(value_pos.length)
        {
            /* print <div key[=]value> */
            if(key_pos.length) {
                size_t between_begin = key_pos.begin + key_pos.length;
                colorize_print(inc_buf, between_begin, (value_pos.begin - between_begin), "\e[31m");
            }
            
            /* print <div key=[value]> */
            colorize_print(inc_buf, value_pos.begin, value_pos.length, "\e[34m");
            
            /* get/check max position */
            if(value_pos.begin + value_pos.length > last_pos)
                last_pos = value_pos.begin + value_pos.length;
        }
        
        attr = myhtml_attribute_next(attr);
    }
    
    return last_pos;
}

void * colorize_callback_before_token_done(myhtml_tree_t* tree, myhtml_token_node_t* token, void* ctx)
{
    mycore_incoming_buffer_t *inc_buf = myhtml_tree_incoming_buffer_first(tree);
    
    myhtml_position_t token_pos = myhtml_token_node_raw_pasition(token);
    myhtml_position_t token_element_pos = myhtml_token_node_element_pasition(token);
    
    size_t last_pos = token_pos.begin + token_pos.length;
    
    switch (myhtml_token_node_tag_id(token)) {
        case MyHTML_TAG__DOCTYPE: {
            /* print [<!DOCTYPE] */
            colorize_print(inc_buf, token_element_pos.begin, (token_pos.begin - token_element_pos.begin), "\e[37m");
            
            colorize_print(inc_buf, token_pos.begin, token_pos.length, "\e[37m");
            
            /* print [>] */
            colorize_print(inc_buf, last_pos, ((token_element_pos.begin + token_element_pos.length) - last_pos), "\e[37m");
            break;
        }
        case MyHTML_TAG__TEXT: {
            colorize_print(inc_buf, token_pos.begin, token_pos.length, "\e[0m");
            break;
        }
        case MyHTML_TAG__COMMENT: {
            /* print [<!--] */
            colorize_print(inc_buf, token_element_pos.begin, (token_pos.begin - token_element_pos.begin), "\e[32m");
            
            colorize_print(inc_buf, token_pos.begin, token_pos.length, "\e[32m");
            
            /* print [-->] */
            colorize_print(inc_buf, last_pos, ((token_element_pos.begin + token_element_pos.length) - last_pos), "\e[32m");
            break;
        }
        default: {
            /* print [<]div> */
            colorize_print(inc_buf, token_element_pos.begin, (token_pos.begin - token_element_pos.begin), "\e[31m");
            
            /* print <[div]> */
            colorize_print(inc_buf, token_pos.begin, token_pos.length, "\e[31m");
            
            if(myhtml_token_node_attribute_first(token))
                last_pos = colorize_print_attributes(tree, myhtml_token_node_attribute_first(token), inc_buf, last_pos);
            
            /* print <div[>] */
            colorize_print(inc_buf, last_pos, ((token_element_pos.begin + token_element_pos.length) - last_pos), "\e[31m");
            
            break;
        }
    }
    
    return ctx;
}

int main(int argc, const char * argv[])
{
    const char* path;

    if (argc == 2) {
        path = argv[1];
    }
    else {
        printf("Bad ARGV!\nUse: tokenizer_colorize_high_level <path_to_html_file>\n");
        exit(EXIT_FAILURE);
    }
    
    struct res_html res = load_html_file(path);
    
    // basic init
    myhtml_t* myhtml = myhtml_create();
    myhtml_init(myhtml, MyHTML_OPTIONS_DEFAULT, 1, 0);
    
    // init tree
    myhtml_tree_t* tree = myhtml_tree_create();
    myhtml_tree_init(tree, myhtml);
    
    myhtml_callback_before_token_done_set(tree, colorize_callback_before_token_done, NULL);
    
    // parse html
    myhtml_parse(tree, MyENCODING_UTF_8, res.html, res.size);
    
    printf("\n");
    
    myhtml_tree_destroy(tree);
    myhtml_destroy(myhtml);
    
    free(res.html);
    
    return 0;
}