Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define BUFFER_SIZE 240
  6.  
  7. const int special_tags_count = 3;
  8.  
  9. const char *special_tags[] = {
  10. "#HIDE",
  11. "//HIDE",
  12. "/*HIDE*/",
  13. };
  14.  
  15.  
  16. bool special_tag(char *line) {
  17. for(int i = 0; i < special_tags_count; i++) {
  18. if(strncmp(line, special_tags[i], 5) == 0) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24.  
  25. // return whether the first three characters are ```
  26. bool code_block_delimiter(char *line) {
  27. return (line[0] == '`' && line[1] == '`' && line[2] =='`');
  28. }
  29.  
  30. int main(int argc, char *argv[]) {
  31. if(argc < 3) {
  32. printf("Improper format, use two arguments file in and file out\n"
  33. "or use three arguments file in file out and parameters "
  34. "to pass to pandoc\n");
  35. return -1;
  36. }
  37.  
  38. FILE *in, *out;
  39. in = fopen(argv[1], "r");
  40. if(in == NULL) {
  41. printf("Error opening file\n");
  42. return -1;
  43. }
  44.  
  45. out = fopen(argv[2], "w");
  46.  
  47. char current_line[BUFFER_SIZE], previous_line[BUFFER_SIZE];
  48.  
  49. while(fgets(current_line, BUFFER_SIZE, in) != NULL) {
  50.  
  51. if(code_block_delimiter(current_line)) {
  52. // save current line and get next line
  53. strcpy(previous_line, current_line);
  54. if(fgets(current_line, BUFFER_SIZE, in) != NULL) {
  55.  
  56. // if you get a special tag keep reading until
  57. // the end of block string ```
  58. if(special_tag(current_line)) {
  59. while(fgets(current_line, BUFFER_SIZE, in) != NULL) {
  60. if(code_block_delimiter(current_line)) {
  61. break;
  62. }
  63. }
  64. // if you did not get a special tag this is a regular
  65. // code block, so print the previous line and then
  66. // then current line
  67. } else {
  68. fprintf(out, "%s", previous_line);
  69. fprintf(out, "%s", current_line);
  70. }
  71. }
  72. } else {
  73. // regular line, copy to output file
  74. fprintf(out, "%s", current_line);
  75. }
  76. }
  77.  
  78. fclose(in);
  79. fclose(out);
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement