├── .gitignore ├── Makefile ├── README.markdown ├── minify ├── minify.c ├── minify.h ├── split.c ├── split.h ├── trim.c └── trim.h └── tests ├── tests.c ├── tests_minify.c ├── tests_minify.h ├── tests_split.c ├── tests_split.h ├── tests_trim.c └── tests_trim.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | a.out* 3 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | @gcc -g -ansi -Wall minify/*.c tests/*.c && ./a.out 3 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | #nginx-minifier 2 | 3 | This is going to be a nginx module for HTML, JS and CSS minifying. 4 | 5 | ##running tests 6 | 7 | Just run: 8 | 9 | $ make test 10 | -------------------------------------------------------------------------------- /minify/minify.c: -------------------------------------------------------------------------------- 1 | #include "minify.h" 2 | #include "trim.h" 3 | 4 | char * minify(const char *inputHtml) 5 | { 6 | return trim(inputHtml); 7 | } 8 | -------------------------------------------------------------------------------- /minify/minify.h: -------------------------------------------------------------------------------- 1 | char *minify(const char *inputHtml); 2 | -------------------------------------------------------------------------------- /minify/split.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int countChar(const char *string, const char character) 5 | { 6 | int i, counter = 0, length = strlen(string); 7 | for (i = 0; i < length; i++) 8 | { 9 | if (*(string + i) == character) 10 | { 11 | counter++; 12 | } 13 | } 14 | return counter; 15 | } 16 | 17 | char ** split(const char *string) 18 | { 19 | int currentLineCounter = 0, linesCounter = 0, lineSize = 100; 20 | int stringLength = strlen(string); 21 | 22 | char ** output = (char **)malloc(countChar(string, '\n') * sizeof(char *)); 23 | 24 | char *currentLine = (char *)malloc(lineSize * sizeof(char)); 25 | int character = *string; 26 | *string++; 27 | 28 | while (character != 0) 29 | { 30 | if (character == '\n') 31 | { 32 | *(currentLine + currentLineCounter) = 0; 33 | int l = strlen(currentLine); 34 | if (l < lineSize) 35 | { 36 | currentLine = realloc(currentLine, l * sizeof(char)); 37 | } 38 | 39 | *(output + linesCounter) = currentLine; 40 | linesCounter++; 41 | 42 | lineSize = 100; 43 | currentLine = (char *)malloc(lineSize * sizeof(char)); 44 | currentLineCounter = 0; 45 | } 46 | else 47 | { 48 | *(currentLine + currentLineCounter) = character; 49 | currentLineCounter++; 50 | } 51 | 52 | if (currentLineCounter > (lineSize - 2)) 53 | { 54 | lineSize += 50; 55 | currentLine = (char *)realloc(currentLine, lineSize * sizeof(char *)); 56 | } 57 | 58 | character = *string; 59 | *string++; 60 | } 61 | 62 | return output; 63 | } 64 | -------------------------------------------------------------------------------- /minify/split.h: -------------------------------------------------------------------------------- 1 | int countChar(const char *string, const char character); 2 | 3 | char **split(const char *string); 4 | -------------------------------------------------------------------------------- /minify/trim.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "trim.h" 4 | 5 | char * trim(const char *inputString) 6 | { 7 | register char *end; 8 | register int length; 9 | 10 | while (isspace(*inputString)) *inputString++; 11 | length = strlen(inputString); 12 | 13 | while (*inputString && length) 14 | { 15 | end = inputString + length - 1; 16 | if (isspace(*end)) 17 | *end = '\0'; 18 | else 19 | break; 20 | length = strlen(inputString); 21 | } 22 | 23 | return inputString; 24 | } 25 | -------------------------------------------------------------------------------- /minify/trim.h: -------------------------------------------------------------------------------- 1 | char * trim(const char *inputString); 2 | -------------------------------------------------------------------------------- /tests/tests.c: -------------------------------------------------------------------------------- 1 | #include "tests_minify.h" 2 | #include "tests_split.h" 3 | #include "tests_trim.h" 4 | 5 | int main () 6 | { 7 | /* Minify */ 8 | testMinifySimpleHtml(); 9 | testMinifyHtmlWithSpace(); 10 | testMinifyMultilineHtml(); 11 | 12 | /* Trim */ 13 | testTrimStringWithSpacesOnlyAtTheBeggining(); 14 | testTrimStringWithSpacesAtEnd(); 15 | 16 | /* Split */ 17 | testSplitString(); 18 | 19 | /* Count */ 20 | testCountCharOnAString(); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /tests/tests_minify.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "tests_minify.h" 6 | #include "../minify/minify.h" 7 | 8 | void testMinifySimpleHtml() 9 | { 10 | char *string = (char *)malloc(1000 * sizeof(char)); 11 | strcpy(string, "

Hello

"); 12 | assert(strcmp(minify(string), "

Hello

") == 0); 13 | free(string); 14 | } 15 | 16 | void testMinifyHtmlWithSpace() 17 | { 18 | char *string = (char *)malloc(1000 * sizeof(char)); 19 | strcpy(string, "

Hello world

"); 20 | assert(strcmp(minify(string), "

Hello world

") == 0); 21 | free(string); 22 | } 23 | 24 | void testMinifyMultilineHtml() 25 | { 26 | char *string = (char *)malloc(1000 * sizeof(char)); 27 | strcpy(string, "\n\n

Hello world!

\n \n"); 28 | assert(strcmp(minify(string), "

Hello world!

") == 0); 29 | free(string); 30 | } 31 | -------------------------------------------------------------------------------- /tests/tests_minify.h: -------------------------------------------------------------------------------- 1 | void testMinifySimpleHtml(); 2 | 3 | void testMinifyHtmlWithSpace(); 4 | 5 | void testMinifyMultilineHtml(); 6 | -------------------------------------------------------------------------------- /tests/tests_split.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "tests_split.h" 6 | #include "../minify/split.h" 7 | 8 | int areStringArraysEqual(char **array1, char **array2, int length) 9 | { 10 | int i, j; 11 | for (i = 0; i < length; i++) 12 | { 13 | char *string1 = *(array1 + i); 14 | char *string2 = *(array2 + i); 15 | int stringLength = strlen(string1); 16 | for (j = 0; j < stringLength; j++) 17 | { 18 | if (*(string1 + j) != *(string2 + j)) 19 | { 20 | return 0; 21 | } 22 | } 23 | } 24 | 25 | return 1; 26 | } 27 | 28 | void testCountCharOnAString() 29 | { 30 | char *string = (char *)malloc(10 * sizeof(char)); 31 | strcpy(string, "sweet home"); 32 | assert(3 == countChar(string, 'e')); 33 | assert(1 == countChar(string, 'w')); 34 | } 35 | 36 | void testSplitString() 37 | { 38 | char *input = (char *)malloc(100 * sizeof(char)); 39 | strcpy(input, "First line\nSecond line\nThird line"); 40 | 41 | char **expected = (char **)malloc(3 * sizeof(char *)); 42 | *(expected+0) = (char *)malloc(11 * sizeof(char)); 43 | *(expected+1) = (char *)malloc(12 * sizeof(char)); 44 | *(expected+2) = (char *)malloc(11 * sizeof(char)); 45 | 46 | strcpy(*(expected+0), "First line"); 47 | strcpy(*(expected+1), "Second line"); 48 | strcpy(*(expected+2), "Third line"); 49 | 50 | assert(areStringArraysEqual(expected, split(input), 3)); 51 | free(input); 52 | 53 | int i; 54 | for (i = 0; i < 3; i++) 55 | { 56 | free(*(expected + i)); 57 | } 58 | free(*expected); 59 | } 60 | -------------------------------------------------------------------------------- /tests/tests_split.h: -------------------------------------------------------------------------------- 1 | void testCountCharOnAString(); 2 | 3 | void testSplitString(); 4 | -------------------------------------------------------------------------------- /tests/tests_trim.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "tests_trim.h" 6 | #include "../minify/trim.h" 7 | 8 | void testTrimStringWithSpacesOnlyAtTheBeggining() 9 | { 10 | char *inputString = (char *)malloc(100 * sizeof(char)); 11 | strcpy(inputString, " hello world!"); 12 | assert(strcmp(trim(inputString), "hello world!") == 0); 13 | } 14 | 15 | void testTrimStringWithSpacesAtEnd() 16 | { 17 | char *inputString = (char *)malloc(100 * sizeof(char)); 18 | strcpy(inputString, "hello world! "); 19 | assert(strcmp(trim(inputString), "hello world!") == 0); 20 | } 21 | -------------------------------------------------------------------------------- /tests/tests_trim.h: -------------------------------------------------------------------------------- 1 | void testTrimStringWithSpacesOnlyAtTheBeggining(); 2 | 3 | void testTrimStringWithSpacesAtEnd(); 4 | --------------------------------------------------------------------------------