{ "extension": ".h", "source": "#include \n#include \n#include \n\n#define RESET \"\\033[0m\"\n#define BOLD \"\\033[1m\"\n#define ITALIC \"\\033[3m\"\n#define FG_YELLOW \"\\033[33m\"\n#define FG_BLUE \"\\033[34m\"\n#define FG_CYAN \"\\033[36m\"\n\nint is_keyword(const char *word) {\n const char *keywords[] = {\"int\", \"float\", \"double\", \"char\", \"void\", \"if\", \"else\", \"while\", \"for\", \"return\", \"struct\", \"printf\"};\n for (size_t i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {\n if (strcmp(word, keywords[i]) == 0) {\n return 1;\n }\n }\n return 0;\n}\n\nvoid highlight_code(const char *code) {\n const char *ptr = code;\n char buffer[256];\n size_t index = 0;\n\n while (*ptr) {\n if ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z') || (*ptr == '_')) {\n while ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z') || (*ptr >= '0' && *ptr <= '9') || (*ptr == '_')) {\n buffer[index++] = *ptr++;\n }\n buffer[index] = '\\0';\n\n if (is_keyword(buffer)) {\n printf(FG_BLUE \"%s\" RESET, buffer);\n } else {\n printf(\"%s\", buffer);\n }\n index = 0;\n } else if (*ptr >= '0' && *ptr <= '9') {\n while (*ptr >= '0' && *ptr <= '9') {\n buffer[index++] = *ptr++;\n }\n buffer[index] = '\\0';\n printf(FG_CYAN \"%s\" RESET, buffer);\n index = 0;\n } else {\n putchar(*ptr);\n ptr++;\n }\n }\n}\n\nvoid parse_markdown_to_ansi(const char *markdown) {\n const char *ptr = markdown;\n bool inside_code = false;\n\n while (*ptr) {\n if (*ptr == '`') {\n inside_code = !inside_code;\n if (inside_code) {\n printf(FG_YELLOW);\n } else {\n printf(RESET);\n }\n ptr++;\n continue;\n }\n\n if (inside_code) {\n char code_buffer[256];\n size_t index = 0;\n\n while (*ptr && *ptr != '`') {\n code_buffer[index++] = *ptr++;\n }\n code_buffer[index] = '\\0';\n highlight_code(code_buffer);\n } else {\n if (strncmp(ptr, \"**\", 2) == 0) {\n printf(BOLD);\n ptr += 2;\n while (*ptr && strncmp(ptr, \"**\", 2) != 0) putchar(*ptr++);\n if (*ptr == '*' && *(ptr + 1) == '*') ptr += 2;\n printf(RESET);\n }\n else if (*ptr == '*' && (ptr == markdown || *(ptr - 1) != '*')) {\n printf(ITALIC);\n ptr++;\n while (*ptr && *ptr != '*') putchar(*ptr++);\n if (*ptr == '*') ptr++;\n printf(RESET);\n }\n else if (strncmp(ptr, \"### \", 4) == 0) {\n printf(BOLD FG_YELLOW);\n ptr += 4;\n while (*ptr && *ptr != '\\n') putchar(*ptr++);\n printf(RESET \"\\n\");\n } else if (strncmp(ptr, \"## \", 3) == 0) {\n printf(BOLD FG_YELLOW);\n ptr += 3;\n while (*ptr && *ptr != '\\n') putchar(*ptr++);\n printf(RESET \"\\n\");\n } else if (strncmp(ptr, \"# \", 2) == 0) {\n printf(BOLD FG_YELLOW);\n ptr += 2;\n while (*ptr && *ptr != '\\n') putchar(*ptr++);\n printf(RESET \"\\n\");\n } else {\n putchar(*ptr);\n ptr++;\n }\n }\n }\n}", "review": "# 7\n\n## Bugs\n- No clear handling or error checking for strings longer than the buffer size (256 characters).\n- Potential issues with markdown parsing states if code markers are irregularly placed.\n- No handling for nested formatting (e.g., bold within italic).\n\n## Optimizations\n- Use `fgets` or other safer string handling functions to manage buffer overflow risks.\n- Improve parsing efficiency by using a state machine or enhanced regex-like parsing for markdown.\n- Use `strstr` for specific tokens to simplify comparisons and eliminate repeated `strncmp` checks.\n- Consider dynamic memory allocation for buffers to handle long code sequences dynamically rather than a fixed size.\n\n## Good points\n- Logical separation of functionality with `is_keyword()` and `highlight_code()`.\n- Effective usage of ANSI escape sequences for terminal output formatting.\n- Good handling of basic code keyword parsing and markdown formatting.\n- Simple and easy-to-understand flow of text parsing and processing.\n\n## Summary\nThe code provides basic functionality to parse markdown and highlight syntax with ANSI escape codes for terminal output. It captures markdown features such as headers and emphasis effectively, and it correctly identifies certain programming keywords. However, the code has limitations in buffer handling and does not account for complex formatting scenarios. Enhancements could be made to improve safety and optimize parsing techniques.\n\n## Open source alternatives\n- [Pandoc](https://pandoc.org/): Converts markdown to various formats with custom styles.\n- [Markdown-it](https://github.com/markdown-it/markdown-it): A fast markdown parser for JavaScript, robust in handling markdown text.\n- [Pygments](https://pygments.org/): A more comprehensive syntax highlighting tool often used in combination with markdown parsers.", "filename": "markdown.h", "path": "markdown.h", "directory": "", "grade": 7, "size": 3537, "line_count": 115 }