#include #include #include #define RESET "\033[0m" #define BOLD "\033[1m" #define ITALIC "\033[3m" #define FG_YELLOW "\033[33m" #define FG_BLUE "\033[34m" #define FG_CYAN "\033[36m" int is_keyword(const char *word) { const char *keywords[] = {"int", "float", "double", "char", "void", "if", "else", "while", "for", "return", "struct", "printf"}; for (size_t i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) { if (strcmp(word, keywords[i]) == 0) { return 1; } } return 0; } void highlight_code(const char *code) { const char *ptr = code; char buffer[256]; size_t index = 0; while (*ptr) { if ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z') || (*ptr == '_')) { while ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z') || (*ptr >= '0' && *ptr <= '9') || (*ptr == '_')) { buffer[index++] = *ptr++; } buffer[index] = '\0'; if (is_keyword(buffer)) { printf(FG_BLUE "%s" RESET, buffer); } else { printf("%s", buffer); } index = 0; } else if (*ptr >= '0' && *ptr <= '9') { while (*ptr >= '0' && *ptr <= '9') { buffer[index++] = *ptr++; } buffer[index] = '\0'; printf(FG_CYAN "%s" RESET, buffer); index = 0; } else { putchar(*ptr); ptr++; } } } void parse_markdown_to_ansi(const char *markdown) { const char *ptr = markdown; bool inside_code = false; while (*ptr) { if (*ptr == '`') { inside_code = !inside_code; if (inside_code) { printf(FG_YELLOW); } else { printf(RESET); } ptr++; continue; } if (inside_code) { char code_buffer[256]; size_t index = 0; while (*ptr && *ptr != '`') { code_buffer[index++] = *ptr++; } code_buffer[index] = '\0'; highlight_code(code_buffer); } else { if (strncmp(ptr, "**", 2) == 0) { printf(BOLD); ptr += 2; while (*ptr && strncmp(ptr, "**", 2) != 0) putchar(*ptr++); if (*ptr == '*' && *(ptr + 1) == '*') ptr += 2; printf(RESET); } else if (*ptr == '*' && (ptr == markdown || *(ptr - 1) != '*')) { printf(ITALIC); ptr++; while (*ptr && *ptr != '*') putchar(*ptr++); if (*ptr == '*') ptr++; printf(RESET); } else if (strncmp(ptr, "### ", 4) == 0) { printf(BOLD FG_YELLOW); ptr += 4; while (*ptr && *ptr != '\n') putchar(*ptr++); printf(RESET "\n"); } else if (strncmp(ptr, "## ", 3) == 0) { printf(BOLD FG_YELLOW); ptr += 3; while (*ptr && *ptr != '\n') putchar(*ptr++); printf(RESET "\n"); } else if (strncmp(ptr, "# ", 2) == 0) { printf(BOLD FG_YELLOW); ptr += 2; while (*ptr && *ptr != '\n') putchar(*ptr++); printf(RESET "\n"); } else { putchar(*ptr); ptr++; } } } }