Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <string.h>
- #define BUFFER_SIZE 240
- const int special_tags_count = 3;
- const char *special_tags[] = {
- "#HIDE",
- "//HIDE",
- "/*HIDE*/",
- };
- bool special_tag(char *line) {
- for(int i = 0; i < special_tags_count; i++) {
- if(strncmp(line, special_tags[i], 5) == 0) {
- return true;
- }
- }
- return false;
- }
- // return whether the first three characters are ```
- bool code_block_delimiter(char *line) {
- return (line[0] == '`' && line[1] == '`' && line[2] =='`');
- }
- int main(int argc, char *argv[]) {
- if(argc < 3) {
- printf("Improper format, use two arguments file in and file out\n"
- "or use three arguments file in file out and parameters "
- "to pass to pandoc\n");
- return -1;
- }
- FILE *in, *out;
- in = fopen(argv[1], "r");
- if(in == NULL) {
- printf("Error opening file\n");
- return -1;
- }
- out = fopen(argv[2], "w");
- char current_line[BUFFER_SIZE], previous_line[BUFFER_SIZE];
- while(fgets(current_line, BUFFER_SIZE, in) != NULL) {
- if(code_block_delimiter(current_line)) {
- // save current line and get next line
- strcpy(previous_line, current_line);
- if(fgets(current_line, BUFFER_SIZE, in) != NULL) {
- // if you get a special tag keep reading until
- // the end of block string ```
- if(special_tag(current_line)) {
- while(fgets(current_line, BUFFER_SIZE, in) != NULL) {
- if(code_block_delimiter(current_line)) {
- break;
- }
- }
- // if you did not get a special tag this is a regular
- // code block, so print the previous line and then
- // then current line
- } else {
- fprintf(out, "%s", previous_line);
- fprintf(out, "%s", current_line);
- }
- }
- } else {
- // regular line, copy to output file
- fprintf(out, "%s", current_line);
- }
- }
- fclose(in);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement