--- /dev/null
+//
+// Program to massage the master doc file to spit out a bunch of little files
+// for the Ruby build script...
+//
+// by James Hammons
+// (C) 2016 Underground Software
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h> // For mkdir()
+#include <sys/types.h>
+
+
+// Global variables (ZOMG YOU HIDEOUS BASTARD!!)
+char buffer[1024000], keyword[1024], token[1024];
+char title[1024], shortTitle[1024], inclFile[1024], style[1024];
+int level = 0, lastLevel = -1;
+int lineCount = 0, startLine, sectionLineCount;
+int part = 0, chapter = 0, subchapter = 0;
+bool first = true, nomove = false;
+FILE * newFile = NULL;
+char level1File[1024], level2File[1024], level3File[1024], temp[1024], temp2[1024];
+char partFN[1024], chapterFN[1024], scLink[4096];
+
+
+void MakeFilename(char * fn)
+{
+ int l = strlen(fn);
+
+ for(int i=0; i<l; i++)
+ {
+ if (fn[i] == ' ')
+ fn[i] = '-';
+ else if (fn[i] >= 'A' && fn[i] <= 'Z')
+ fn[i] |= 0x20;
+ else if (fn[i] >= 'a' && fn[i] <= 'z')
+ ;
+ else if (fn[i] >= '0' && fn[i] <= '9')
+ ;
+ else
+ fn[i] = '_';
+ }
+}
+
+
+//
+// There's probably posix function to do this, if you find it, feel free to
+// nuke this crap
+//
+void CopyFile(char * from, char * to)
+{
+ FILE * fromFile = fopen(from, "r");
+
+ if (fromFile == NULL)
+ {
+ printf("\n\nCould not open file '%s' for reading! Aborting!!\n", from);
+ exit(1);
+ }
+
+ FILE * toFile = fopen(to, "w");
+
+ if (toFile == NULL)
+ {
+ printf("\n\nCould not open file '%s' for writing! Aborting!!\n", to);
+ exit(1);
+ }
+
+ fseek(fromFile, 0, SEEK_END);
+ long length = ftell(fromFile);
+ rewind(fromFile);
+
+ for(long i=0; i<length; i++)
+ fputc(fgetc(fromFile), toFile);
+
+ fclose(toFile);
+ fclose(fromFile);
+}
+
+
+bool ParseHeader(FILE * file)
+{
+ while (true)
+ {
+ // If we hit the EOF before finishing, something went horribly wrong
+ if (feof(file))
+ break;
+
+ fgets(buffer, 1023999, file);
+ lineCount++;
+
+ // If we're seeing the end of header sentinel, return; we're done.
+ if (strncmp(buffer, "---", 3) == 0)
+ return true;
+
+ sscanf(buffer, "%[^:]: %[^\n]", keyword, token);
+
+ if (strcmp(keyword, "title") == 0)
+ {
+ strcpy(title, token);
+ }
+ else if (strcmp(keyword, "part") == 0)
+ {
+ lastLevel = level;
+ int len = strlen(token);
+
+ if (len == 4)
+ level = 0, part++;
+ else if (len == 7)
+ level = 1, chapter++, subchapter = 0;
+ else if (len == 10)
+ level = 2, subchapter++;
+ else
+ level = -1; // Something went wrong
+ }
+ else if (strcmp(keyword, "include") == 0)
+ {
+ strcpy(inclFile, token);
+ }
+ else if (strcmp(keyword, "menu_title") == 0)
+ {
+ strcpy(shortTitle, token);
+ }
+ else if (strcmp(keyword, "style") == 0)
+ {
+ strcpy(style, token);
+ }
+ else if (strcmp(keyword, "exclude") == 0)
+ {
+ // Don't care about the token, just the keyword
+ nomove = true;
+ }
+ else
+ printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
+ }
+
+ // If we end up here, something went horribly wrong...
+ return false;
+}
+
+
+int main(int argc, char * argv[])
+{
+ 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" };
+
+ FILE * master = fopen("master-doc.txt", "r");
+
+ if (master == NULL)
+ {
+ printf("Could not open master doc (master-doc.txt) file!\n");
+ return -1;
+ }
+
+ FILE * toc = fopen("_manual.munge/00_toc.html", "w");
+
+ if (toc == NULL)
+ {
+ printf("Could not open TOC file!\n");
+ fclose(master);
+ return -1;
+ }
+
+ fprintf(toc, "---\n");
+ fprintf(toc, "title: Ardour Table of Contents\n");
+ fprintf(toc, "---\n\n");
+
+ while (!feof(master))
+ {
+ fgets(buffer, 1023999, master);
+ lineCount++;
+
+ // Look for start of file marker
+ if (strncmp(buffer, "---", 3) == 0)
+ {
+ if (first)
+ first = false;
+ else
+ {
+ sectionLineCount = lineCount - startLine;
+ printf(" (%d lines)\n", sectionLineCount);
+ }
+
+ // Reset the "short" title, include file & sytle
+ shortTitle[0] = 0;
+ inclFile[0] = 0;
+ style[0] = 0;
+ nomove = false;
+
+ if (!ParseHeader(master))
+ {
+ printf("Something went horribly wrong with the header parsing! Aborting!\n");
+ break;
+ }
+
+ // We hit the end of our keyword block, now do something
+ // about it... :-P
+ if (level == 0)
+ printf("\nPart %s: ", roman[part]);
+ else if (level == 1)
+ printf("\n\tCh. %d: ", chapter);
+ else if (level == 2)
+ printf("\t\t");
+
+ printf("%s", title);
+
+ if (strlen(inclFile) > 0)
+ printf(" [%s]", inclFile);
+
+ startLine = lineCount;
+
+ // Add appropriate suffix chapter content if needed
+ if ((lastLevel == 1) && (level == 2))
+ {
+ if (newFile != NULL)
+ {
+ fprintf(newFile, "\n{%% children %%}\n\n");
+ }
+ else
+ {
+ // If it didn't stream, it was a included file, so append to
+ // it directly...
+ sprintf(temp2, "%s.html", level2File);
+ FILE * tfp = fopen(temp2, "a");
+ fprintf(tfp, "\n{%% children %%}\n\n");
+ fclose(tfp);
+ }
+ }
+
+ // Close any previously opened files...
+ if (newFile != NULL)
+ {
+ fclose(newFile);
+ newFile = NULL;
+ }
+
+ if (level == 0)
+ {
+ // Parts don't have any content...
+ strcpy(temp, title);
+ MakeFilename(temp);
+ sprintf(level1File, "_manual.munge/%02d_%s", part, temp);
+ mkdir(level1File, 0777);
+
+ // Set up the "part" level of TOC link
+ sprintf(partFN, "%s", temp);
+ fprintf(toc, "<h2>Part %s: %s</h2>\n", roman[part], title);
+
+ // Make the file expected at this level...
+ sprintf(temp, "%s.html", level1File);
+ FILE * tfp = fopen(temp, "w");
+ fprintf(tfp, "---\n");
+ fprintf(tfp, "title: %s\n", title);
+
+ if (strlen(shortTitle) > 0)
+ fprintf(tfp, "menu_title: %s\n", shortTitle);
+
+ fprintf(tfp, "---\n");
+ fprintf(tfp, "\n{%% children %%}\n\n");
+ fclose(tfp);
+ }
+ else if (level == 1)
+ {
+ strcpy(temp, title);
+ MakeFilename(temp);
+ sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
+ mkdir(level2File, 0777);
+
+ // Set up the "chapter" part of the TOC link
+ sprintf(chapterFN, "%s", temp);
+ fprintf(toc, " <p id=chapter>Ch. %d: <a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
+
+ // If there's an include file, copy it...
+ if (strlen(inclFile) > 0)
+ {
+ sprintf(temp, "include/%s", inclFile);
+ sprintf(temp2, "%s.html", level2File);
+ CopyFile(temp, temp2);
+ }
+ else
+ {
+ // Make the file expected at this level...
+ sprintf(temp, "%s.html", level2File);
+ newFile = fopen(temp, "w");
+ fprintf(newFile, "---\n");
+ fprintf(newFile, "title: %s\n", title);
+
+ if (strlen(shortTitle) > 0)
+ fprintf(newFile, "menu_title: %s\n", shortTitle);
+
+ fprintf(newFile, "---\n\n");
+ }
+ }
+ else if (level == 2)
+ {
+ strcpy(temp, title);
+ MakeFilename(temp);
+ sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
+
+ // Make the file expected at this level...
+
+ // Make the link
+ sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
+ fprintf(toc, " <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
+
+ // If there's an include file, copy it...
+ if (strlen(inclFile) > 0)
+ {
+ sprintf(temp, "include/%s", inclFile);
+ CopyFile(temp, level3File);
+ }
+ else
+ {
+ // Otherwise, stream it out of the master document
+ newFile = fopen(level3File, "w");
+ fprintf(newFile, "---\n");
+ fprintf(newFile, "title: %s\n", title);
+
+ if (strlen(shortTitle) > 0)
+ fprintf(newFile, "menu_title: %s\n", shortTitle);
+
+ if (strlen(style) > 0)
+ fprintf(newFile, "style: %s\n", style);
+
+ fprintf(newFile, "---\n\n");
+ }
+ }
+ }
+ else
+ {
+ if (((level == 1) || (level == 2)) && (newFile != NULL))
+ fprintf(newFile, "%s", buffer);
+ }
+
+ // Make sure no spurious garbage signals a false positive
+ buffer[0] = 0;
+ }
+
+ printf("\n\nProcessed %i lines.\n", lineCount);
+
+ fclose(master);
+ fclose(toc);
+
+ if (newFile)
+ fclose(newFile);
+
+ return 0;
+}
+