2 // Program to massage the master doc file to spit out a bunch of little files
3 // for the Ruby build script...
6 // (C) 2016 Underground Software
12 #include <sys/stat.h> // For mkdir()
13 #include <sys/types.h>
16 // Global variables (ZOMG YOU HIDEOUS BASTARD!!)
17 char buffer[1024000], keyword[1024], token[1024];
18 char title[1024], shortTitle[1024], inclFile[1024], style[1024];
19 int level = 0, lastLevel = -1;
20 int lineCount = 0, startLine, sectionLineCount;
21 int part = 0, chapter = 0, subchapter = 0;
22 bool first = true, nomove = false;
23 FILE * newFile = NULL;
24 char level1File[1024], level2File[1024], level3File[1024], temp[1024], temp2[1024];
25 char partFN[1024], chapterFN[1024], scLink[4096];
28 void MakeFilename(char * fn)
32 for(int i=0; i<l; i++)
36 else if (fn[i] >= 'A' && fn[i] <= 'Z')
38 else if (fn[i] >= 'a' && fn[i] <= 'z')
40 else if (fn[i] >= '0' && fn[i] <= '9')
49 // There's probably posix function to do this, if you find it, feel free to
52 void CopyFile(char * from, char * to)
54 FILE * fromFile = fopen(from, "r");
58 printf("\n\nCould not open file '%s' for reading! Aborting!!\n", from);
62 FILE * toFile = fopen(to, "w");
66 printf("\n\nCould not open file '%s' for writing! Aborting!!\n", to);
70 fseek(fromFile, 0, SEEK_END);
71 long length = ftell(fromFile);
74 for(long i=0; i<length; i++)
75 fputc(fgetc(fromFile), toFile);
82 bool ParseHeader(FILE * file)
86 // If we hit the EOF before finishing, something went horribly wrong
90 fgets(buffer, 1023999, file);
93 // If we're seeing the end of header sentinel, return; we're done.
94 if (strncmp(buffer, "---", 3) == 0)
97 sscanf(buffer, "%[^:]: %[^\n]", keyword, token);
99 if (strcmp(keyword, "title") == 0)
101 strcpy(title, token);
103 else if (strcmp(keyword, "part") == 0)
106 int len = strlen(token);
111 level = 1, chapter++, subchapter = 0;
113 level = 2, subchapter++;
115 level = -1; // Something went wrong
117 else if (strcmp(keyword, "include") == 0)
119 strcpy(inclFile, token);
121 else if (strcmp(keyword, "menu_title") == 0)
123 strcpy(shortTitle, token);
125 else if (strcmp(keyword, "style") == 0)
127 strcpy(style, token);
129 else if (strcmp(keyword, "exclude") == 0)
131 // Don't care about the token, just the keyword
135 printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
138 // If we end up here, something went horribly wrong...
143 int main(int argc, char * argv[])
145 char roman[21][10] = { "0", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX" };
147 FILE * master = fopen("master-doc.txt", "r");
151 printf("Could not open master doc (master-doc.txt) file!\n");
155 FILE * toc = fopen("_manual/00_toc.html", "w");
159 printf("Could not open TOC file!\n");
164 fprintf(toc, "---\n");
165 fprintf(toc, "title: Ardour Table of Contents\n");
166 fprintf(toc, "---\n\n");
168 while (!feof(master))
170 fgets(buffer, 1023999, master);
173 // Look for start of file marker
174 if (strncmp(buffer, "---", 3) == 0)
180 sectionLineCount = lineCount - startLine;
181 printf(" (%d lines)\n", sectionLineCount);
184 // Reset the "short" title, include file & sytle
190 if (!ParseHeader(master))
192 printf("Something went horribly wrong with the header parsing! Aborting!\n");
196 // We hit the end of our keyword block, now do something
199 printf("\nPart %s: ", roman[part]);
201 printf("\n\tCh. %d: ", chapter);
207 if (strlen(inclFile) > 0)
208 printf(" [%s]", inclFile);
210 startLine = lineCount;
212 // Add appropriate suffix chapter content if needed
213 if ((lastLevel == 1) && (level == 2))
217 fprintf(newFile, "\n{%% children %%}\n\n");
221 // If it didn't stream, it was a included file, so append to
223 sprintf(temp2, "%s.html", level2File);
224 FILE * tfp = fopen(temp2, "a");
225 fprintf(tfp, "\n{%% children %%}\n\n");
230 // Close any previously opened files...
239 // Parts don't have any content...
242 sprintf(level1File, "_manual/%02d_%s", part, temp);
243 mkdir(level1File, 0777);
245 // Set up the "part" level of TOC link
246 sprintf(partFN, "%s", temp);
247 fprintf(toc, "<h2>Part %s: %s</h2>\n", roman[part], title);
249 // Make the file expected at this level...
250 sprintf(temp, "%s.html", level1File);
251 FILE * tfp = fopen(temp, "w");
252 fprintf(tfp, "---\n");
253 fprintf(tfp, "title: %s\n", title);
255 if (strlen(shortTitle) > 0)
256 fprintf(tfp, "menu_title: %s\n", shortTitle);
258 fprintf(tfp, "---\n");
259 fprintf(tfp, "\n{%% children %%}\n\n");
266 sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
267 mkdir(level2File, 0777);
269 // Set up the "chapter" part of the TOC link
270 sprintf(chapterFN, "%s", temp);
271 fprintf(toc, " <p id=chapter>Ch. %d: <a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
273 // If there's an include file, copy it...
274 if (strlen(inclFile) > 0)
276 sprintf(temp, "include/%s", inclFile);
277 sprintf(temp2, "%s.html", level2File);
278 CopyFile(temp, temp2);
282 // Make the file expected at this level...
283 sprintf(temp, "%s.html", level2File);
284 newFile = fopen(temp, "w");
285 fprintf(newFile, "---\n");
286 fprintf(newFile, "title: %s\n", title);
288 if (strlen(shortTitle) > 0)
289 fprintf(newFile, "menu_title: %s\n", shortTitle);
291 fprintf(newFile, "---\n\n");
298 sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
300 // Make the file expected at this level...
303 sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
304 fprintf(toc, " <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
306 // If there's an include file, copy it...
307 if (strlen(inclFile) > 0)
309 sprintf(temp, "include/%s", inclFile);
310 CopyFile(temp, level3File);
314 // Otherwise, stream it out of the master document
315 newFile = fopen(level3File, "w");
316 fprintf(newFile, "---\n");
317 fprintf(newFile, "title: %s\n", title);
319 if (strlen(shortTitle) > 0)
320 fprintf(newFile, "menu_title: %s\n", shortTitle);
322 if (strlen(style) > 0)
323 fprintf(newFile, "style: %s\n", style);
325 fprintf(newFile, "---\n\n");
331 if (((level == 1) || (level == 2)) && (newFile != NULL))
332 fprintf(newFile, "%s", buffer);
335 // Make sure no spurious garbage signals a false positive
339 printf("\n\nProcessed %i lines.\n", lineCount);