mdtoc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. mdtoc.c
  3. Markdown TOC Generator
  4. LPC804 PLU Project
  5. Copyright (c) 2015, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #define TOC_ENTRIES 512
  33. #define TOC_ENTRY_SIZE 1024
  34. char toc_start[] = "[tocstart]: # (toc start)";
  35. char toc_end[] = "[tocend]: # (toc end)";
  36. char toc[TOC_ENTRIES][TOC_ENTRY_SIZE];
  37. int toc_level[TOC_ENTRIES];
  38. int toc_cnt = 0;
  39. int existing_toc_found = 0;
  40. char *getentry(const char *s)
  41. {
  42. static char buf[TOC_ENTRY_SIZE];
  43. char *t = buf;
  44. for(;;)
  45. {
  46. if ( *s == '\0' )
  47. break;
  48. if ( *s >= ' ' )
  49. {
  50. *t++ = *s++;
  51. }
  52. else
  53. {
  54. s++;
  55. }
  56. }
  57. *t = '\0';
  58. return buf;
  59. }
  60. char *getref(const char *s)
  61. {
  62. static char buf[TOC_ENTRY_SIZE];
  63. char *t = buf;
  64. for(;;)
  65. {
  66. if ( *s == '\0' )
  67. break;
  68. if ( *s == ' ' )
  69. {
  70. *t++ = '-';
  71. s++;
  72. }
  73. else if ( *s >= '0' && *s <= '9' )
  74. {
  75. *t++ = *s++;
  76. }
  77. else if ( *s < 'A' )
  78. {
  79. s++;
  80. }
  81. else
  82. {
  83. *t = tolower(*s);
  84. t++;
  85. s++;
  86. }
  87. }
  88. *t = '\0';
  89. return buf;
  90. }
  91. void add_toc(int level, char *entry)
  92. {
  93. if ( toc_cnt >= TOC_ENTRIES )
  94. return;
  95. strncpy( toc[toc_cnt], entry, TOC_ENTRY_SIZE);
  96. toc[toc_cnt][TOC_ENTRY_SIZE-1] = '\0';
  97. toc_level[toc_cnt] = level;
  98. toc_cnt++;
  99. // printf("%d: %s [%s](#%s)\n", level, entry, getentry(entry), getref(entry));
  100. }
  101. void read_md_fp(FILE *in, FILE *out)
  102. {
  103. static char buf[TOC_ENTRY_SIZE];
  104. char *s;
  105. int is_inside_toc = 0;
  106. int is_start_of_file = 1;
  107. for(;;)
  108. {
  109. s = fgets(buf, TOC_ENTRY_SIZE, in);
  110. if ( s == NULL )
  111. return;
  112. if ( is_inside_toc != 0 )
  113. {
  114. if ( strncmp(s, toc_end, strlen(toc_end)) == 0 )
  115. {
  116. is_inside_toc = 0;
  117. }
  118. }
  119. else
  120. {
  121. if ( buf[0] == '\0' || buf[0] == '\n' )
  122. {
  123. /* skip empty lines at the beginning of the file */
  124. if ( is_start_of_file == 0 )
  125. fputs(buf, out);
  126. }
  127. else if ( strncmp(s, toc_start, strlen(toc_start)) == 0 )
  128. {
  129. existing_toc_found = 1;
  130. is_inside_toc = 1;
  131. }
  132. else if ( strncmp(s, "# ", 2) == 0 )
  133. {
  134. is_start_of_file = 0;
  135. add_toc(1, s+2);
  136. fputs(buf, out);
  137. }
  138. else if ( strncmp(s, "## ", 3) == 0 )
  139. {
  140. is_start_of_file = 0;
  141. add_toc(2, s+3);
  142. fputs(buf, out);
  143. }
  144. else
  145. {
  146. is_start_of_file = 0;
  147. fputs(buf, out);
  148. }
  149. }
  150. }
  151. }
  152. void read_md(const char *in_name, const char *out_name)
  153. {
  154. FILE *in;
  155. FILE *out;
  156. in = fopen(in_name, "r");
  157. if ( in == NULL )
  158. {
  159. perror(in_name);
  160. return;
  161. }
  162. out = fopen(out_name, "w");
  163. if ( out == NULL )
  164. {
  165. perror(out_name);
  166. return;
  167. }
  168. read_md_fp(in, out);
  169. fclose(in);
  170. fclose(out);
  171. }
  172. void copy_to_md_fp(FILE *tmp, FILE *out)
  173. {
  174. static char buf[TOC_ENTRY_SIZE];
  175. char *s;
  176. int i, j;
  177. fprintf(out, "\n");
  178. fprintf(out, "%s\n", toc_start);
  179. fprintf(out, "\n");
  180. for( i = 0; i < toc_cnt; i++ )
  181. {
  182. for( j = 0; j < toc_level[i]; j++)
  183. fprintf(out, " ");
  184. fprintf(out, "* [%s](#%s)\n", getentry(toc[i]), getref(toc[i]));
  185. }
  186. fprintf(out, "\n");
  187. fprintf(out, "%s\n", toc_end);
  188. fprintf(out, "\n");
  189. for(;;)
  190. {
  191. s = fgets(buf, TOC_ENTRY_SIZE, tmp);
  192. if ( s == NULL )
  193. return;
  194. fputs(buf, out);
  195. }
  196. }
  197. void copy_to_md(const char *tmp_name, const char *out_name)
  198. {
  199. FILE *tmp;
  200. FILE *out;
  201. tmp = fopen(tmp_name, "r");
  202. if ( tmp == NULL )
  203. {
  204. perror(tmp_name);
  205. return;
  206. }
  207. out = fopen(out_name, "w");
  208. if ( out == NULL )
  209. {
  210. perror(out_name);
  211. return;
  212. }
  213. copy_to_md_fp(tmp, out);
  214. fclose(tmp);
  215. fclose(out);
  216. }
  217. int main(int argc, char **argv)
  218. {
  219. if ( argc <= 1 )
  220. {
  221. printf("%s [markdown files]\n", argv[0]);
  222. return 1;
  223. }
  224. argv++;
  225. while( *argv != NULL )
  226. {
  227. if ( strstr( *argv, "_Footer.md") == NULL &&
  228. strstr( *argv, "_Sidebar.md") == NULL )
  229. {
  230. toc_cnt = 0;
  231. existing_toc_found = 0;
  232. read_md(*argv, "tmp.md");
  233. if ( toc_cnt != 0 )
  234. {
  235. copy_to_md("tmp.md", *argv);
  236. }
  237. if ( toc_cnt == 0 )
  238. {
  239. printf("%s: nothing changed (no headings found)\n", *argv);
  240. }
  241. else
  242. {
  243. if ( existing_toc_found == 0 )
  244. {
  245. if ( toc_cnt == 1 )
  246. {
  247. printf("%s: added %d toc entry\n", *argv, toc_cnt);
  248. }
  249. else
  250. {
  251. printf("%s: added %d toc entries\n", *argv, toc_cnt);
  252. }
  253. }
  254. else
  255. {
  256. if ( toc_cnt == 1 )
  257. {
  258. printf("%s: replaced %d toc entry\n", *argv, toc_cnt);
  259. }
  260. else
  261. {
  262. printf("%s: replaced %d toc entries\n", *argv, toc_cnt);
  263. }
  264. }
  265. } /* no toc required */
  266. } /* skip system .md files */
  267. argv++;
  268. }
  269. return 0;
  270. }