-
Notifications
You must be signed in to change notification settings - Fork 1
/
larave_blade_beautifier.c
119 lines (106 loc) · 4.32 KB
/
larave_blade_beautifier.c
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
// Requirements: Pass blade file to be beautified as argument
// All the tags for which indents have to applied should open and close in a new line
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
if (argc < 2)
return 0;
FILE *readfile, *outfile;
int spaces = 0, i;
char * line = NULL;
size_t len = 0;
ssize_t read;
char spa[128];
strcpy(spa, "");
int lineno = 0;
int divcount=0, ifcount=0, ulcount=0, fieldsetcount=0,tablecount=0, tdcount=0, trcount=0, selectcount=0, spancount= 0, formcount=0, forcount=0, sectioncount = 0, stylecount = 0;
readfile = fopen(argv[1], "r");
outfile = fopen("output.txt", "w");
while ((read = getline(&line, &len, readfile)) != -1) {
lineno++;
if (!strncmp(line, "</div", 5) || !strncmp(line, "@end", 4) || !strncmp(line, "@else", 5) || !strncmp(line, "</ul", 4) || !strncmp(line, "</fieldset", 10) || !strncmp(line, "</table", 7) || !strncmp(line, "</tr", 4) || !strncmp(line, "</td ", 4) || !strncmp(line, "</table", 7) || !strncmp(line, "</select", 7) || !strncmp(line, "</span", 6) || !strncmp(line, "</form", 6) || !strncmp(line, "</style", 7))
{
spaces -= 4;
strcpy(spa, "");
for (i=0; i<spaces; i++)
sprintf(spa, "%s ", spa);
}
if (!strncmp(line, "</div", 5))
divcount--;
if (!strncmp(line, "@endif", 6))
ifcount--;
if ( !strncmp(line, "@endfor", 7))
forcount--;
if ( !strncmp(line, "@endsection", 11))
sectioncount--;
if ( !strncmp(line, "</style", 7))
stylecount--;
if ( !strncmp(line, "</ul", 4))
ulcount--;
if (!strncmp(line, "</fieldset", 10))
fieldsetcount--;
if (!strncmp(line, "</table", 7))
tablecount--;
if (!strncmp(line, "</tr", 4))
trcount--;
if ( !strncmp(line, "</td ", 4))
tdcount--;
if (!strncmp(line, "</select", 7))
selectcount--;
if (!strncmp(line, "</span", 6))
spancount--;
if (!strncmp(line, "</form", 6))
formcount--;
if (spaces < 0)
{
printf("CRITICAL: Negative spacing at line %d\n\n", lineno);
break;
}
fprintf(outfile, "%s%s", spa, line);
if (!strncmp(line, "<div", 4) || !strncmp(line, "@if", 3) || !strncmp(line, "@section", 8) || !strncmp(line, "@else", 5) || !strncmp(line, "@for", 4) || !strncmp(line, "<ul", 3) || !strncmp(line, "<fieldset", 9) || !strncmp(line, "<table", 6) || !strncmp(line, "<tr", 3) || !strncmp(line, "<td", 3) || !strncmp(line, "<select", 7) || !strncmp(line, "<style", 6) || !strncmp(line, "<span", 5) || !strncmp(line, "<form", 5))
{
spaces += 4;
strcpy(spa, "");
for (i=0; i<spaces; i++)
sprintf(spa, "%s ", spa);
}
if (!strncmp(line, "<div", 4))
divcount++;
if (!strncmp(line, "@if", 3))
ifcount++;
if ( !strncmp(line, "@for", 4))
forcount++;
if ( !strncmp(line, "@section", 8))
sectioncount++;
if (!strncmp(line, "<ul", 3))
ulcount++;
if (!strncmp(line, "<fieldset", 9))
fieldsetcount++;
if ( !strncmp(line, "<table", 6))
tablecount++;
if (!strncmp(line, "<tr", 3) )
trcount++;
if ( !strncmp(line, "<td", 3) )
tdcount++;
if ( !strncmp(line, "<select", 7))
selectcount++;
if ( !strncmp(line, "<span", 5))
spancount++;
if ( !strncmp(line, "<form", 5))
formcount++;
if ( !strncmp(line, "<style", 6))
stylecount++;
if (spaces < 0)
{
printf("CRITICAL: Negative spacing at line %d", lineno);
return 0;
}
free(line);
line = NULL;
}
printf("EXTRA TAG STATS:\n divcount=%d\n ifcount=%d\n ulcount=%d\n fieldsetcount=%d\n tablecount=%d\n tdcount=%d\n trcount=%d\n selectcount=%d\n spancount= %d\n formcount=%d\n forcount=%d\n sectioncount=%d\n stylecount=%d\n", divcount, ifcount, ulcount, fieldsetcount, tablecount, tdcount, trcount, selectcount, spancount, formcount, forcount, sectioncount, stylecount);
fclose(readfile);
fclose(outfile);
}