katana-parser的安装参考github官网。
下面是一个解析css的demo:
复制代码
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#include <stdio.h> #include <stdlib.h> #include <sys/timeb.h> #include "katana.h" /* g++ css-parser.cpp `pkg-config --cflags --libs katana` ./a.out test.css eg : test.css: hr {color: sienna;} p {margin-left: 20px;color:red;} body {background-image: url("images/back40.gif");} div {color:black;} */ #define StopWatchBegin(begin) struct timeb t1; ftime(&t1); #define StopWatchEnd(begin) struct timeb t2; ftime(&t2); printf("<" #begin "> costs %dms.n", (t2.millitm - t1.millitm)); void apply_rule(KatanaArray *Properties) { for (long i = 0; i < Properties->length; i++) { auto prop = (KatanaDeclaration *)Properties->data[i]; printf("Set property "%s" with %d valuesn", prop->property, prop->values->length); for (long v = 0; v < prop->values->length; v++) { auto value = (KatanaValue *)prop->values->data[v]; // printf("unit is %dn", value->unit); switch (value->unit) { case KATANA_VALUE_URI: { printf("properties value is : %sn", value->string); break; } case KATANA_VALUE_IDENT: { printf("properties value is : %sn", value->string); break; } } } } } void parse_rule(KatanaRule *Rule) { if (Rule == NULL) { exit; } switch (Rule->type) { case KatanaRuleStyle: { auto sr = (KatanaStyleRule *)Rule; for (long i = 0; i < sr->selectors->length; ++i) { auto sel = (KatanaSelector *)sr->selectors->data[i]; switch (sel->match) { case KatanaSelectorMatchTag: printf("Processing selector is: %sn", (sel->tag) ? sel->tag->local : "UNNAMED"); apply_rule(sr->declarations); break; case KatanaSelectorMatchId: break; case KatanaSelectorMatchClass: printf("Processing class selector: %sn", (sel->data) ? sel->data->value : "UNNAMED"); break; case KatanaSelectorMatchPseudoClass: // E.g. a:link break; case KatanaSelectorMatchPseudoElement: break; case KatanaSelectorMatchPagePseudoClass: break; case KatanaSelectorMatchAttributeExact: break; case KatanaSelectorMatchAttributeSet: break; case KatanaSelectorMatchAttributeList: break; case KatanaSelectorMatchAttributeHyphen: break; case KatanaSelectorMatchAttributeContain: break; case KatanaSelectorMatchAttributeBegin: break; case KatanaSelectorMatchAttributeEnd: break; case KatanaSelectorMatchUnknown: break; } } } } } void dump_stylesheet(const char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) { printf("File %s not found!n", filename); exit(0); } StopWatchBegin(KatanaParseFile); KatanaOutput *output = katana_parse_in(fp); StopWatchEnd(KatanaParseFile); KatanaStylesheet *sheet = output->stylesheet; printf("sheet->imports:%dn", sheet->imports.length); printf("sheet->rules:%dn", sheet->rules.length); printf("======================n"); for (long i = 0; i < sheet->rules.length; ++i) { if (sheet->rules.data[i]) { KatanaRule *item = (KatanaRule *)sheet->rules.data[i]; printf("name : %sn", item->name); parse_rule(item); printf("======================n"); } } // katana_dump_output(output); katana_destroy_output(output); } int main(int argc, const char *argv[]) { if (argc != 2) { printf("Usage: css_dump <CSS filename>.n"); exit(0); } dump_stylesheet(argv[1]); return 0; }
最后
以上就是贪玩太阳最近收集整理的关于使用katana-parser解析css文件的全部内容,更多相关使用katana-parser解析css文件内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复